1 //===-- SystemZSubtarget.cpp - SystemZ subtarget information --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "SystemZSubtarget.h"
10 #include "MCTargetDesc/SystemZMCTargetDesc.h"
11 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
12 #include "llvm/IR/GlobalValue.h"
13 #include "llvm/Target/TargetMachine.h"
14 
15 using namespace llvm;
16 
17 #define DEBUG_TYPE "systemz-subtarget"
18 
19 #define GET_SUBTARGETINFO_TARGET_DESC
20 #define GET_SUBTARGETINFO_CTOR
21 #include "SystemZGenSubtargetInfo.inc"
22 
23 static cl::opt<bool> UseSubRegLiveness(
24     "systemz-subreg-liveness",
25     cl::desc("Enable subregister liveness tracking for SystemZ (experimental)"),
26     cl::Hidden);
27 
28 // Pin the vtable to this file.
29 void SystemZSubtarget::anchor() {}
30 
31 SystemZSubtarget &SystemZSubtarget::initializeSubtargetDependencies(
32     StringRef CPU, StringRef TuneCPU, StringRef FS) {
33   if (CPU.empty())
34     CPU = "generic";
35   if (TuneCPU.empty())
36     TuneCPU = CPU;
37   // Parse features string.
38   ParseSubtargetFeatures(CPU, TuneCPU, FS);
39 
40   // -msoft-float implies -mno-vx.
41   if (HasSoftFloat)
42     HasVector = false;
43 
44   // -mno-vx implicitly disables all vector-related features.
45   if (!HasVector) {
46     HasVectorEnhancements1 = false;
47     HasVectorEnhancements2 = false;
48     HasVectorPackedDecimal = false;
49     HasVectorPackedDecimalEnhancement = false;
50     HasVectorPackedDecimalEnhancement2 = false;
51   }
52 
53   return *this;
54 }
55 
56 SystemZCallingConventionRegisters *
57 SystemZSubtarget::initializeSpecialRegisters() {
58   if (isTargetXPLINK64())
59     return new SystemZXPLINK64Registers;
60   else if (isTargetELF())
61     return new SystemZELFRegisters;
62   else {
63     llvm_unreachable("Invalid Calling Convention. Cannot initialize Special "
64                      "Call Registers!");
65   }
66 }
67 
68 SystemZSubtarget::SystemZSubtarget(const Triple &TT, const std::string &CPU,
69                                    const std::string &TuneCPU,
70                                    const std::string &FS,
71                                    const TargetMachine &TM)
72     : SystemZGenSubtargetInfo(TT, CPU, TuneCPU, FS), TargetTriple(TT),
73       SpecialRegisters(initializeSpecialRegisters()),
74       InstrInfo(initializeSubtargetDependencies(CPU, TuneCPU, FS)),
75       TLInfo(TM, *this), FrameLowering(SystemZFrameLowering::create(*this)) {}
76 
77 bool SystemZSubtarget::enableSubRegLiveness() const {
78   return UseSubRegLiveness;
79 }
80 
81 bool SystemZSubtarget::isAddressedViaADA(const GlobalValue *GV) const {
82   if (const auto *GO = dyn_cast<GlobalObject>(GV)) {
83     // A R/O variable is placed in code section. If the R/O variable has as
84     // least two byte alignment, then generated code can use relative
85     // instructions to address the variable. Otherwise, use the ADA to address
86     // the variable.
87     if (GO->getAlignment() & 0x1) {
88       return true;
89     }
90 
91     // getKindForGlobal only works with definitions
92     if (GO->isDeclaration()) {
93       return true;
94     }
95 
96     // check AvailableExternallyLinkage here as getKindForGlobal() asserts
97     if (GO->hasAvailableExternallyLinkage()) {
98       return true;
99     }
100 
101     SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(
102         GO, TLInfo.getTargetMachine());
103     if (!GOKind.isReadOnly()) {
104       return true;
105     }
106 
107     return false; // R/O variable with multiple of 2 byte alignment
108   }
109   return true;
110 }
111 
112 bool SystemZSubtarget::isPC32DBLSymbol(const GlobalValue *GV,
113                                        CodeModel::Model CM) const {
114   if (isTargetzOS())
115     return !isAddressedViaADA(GV);
116 
117   // PC32DBL accesses require the low bit to be clear.
118   //
119   // FIXME: Explicitly check for functions: the datalayout is currently
120   // missing information about function pointers.
121   const DataLayout &DL = GV->getParent()->getDataLayout();
122   if (GV->getPointerAlignment(DL) == 1 && !GV->getValueType()->isFunctionTy())
123     return false;
124 
125   // For the small model, all locally-binding symbols are in range.
126   if (CM == CodeModel::Small)
127     return TLInfo.getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
128 
129   // For Medium and above, assume that the symbol is not within the 4GB range.
130   // Taking the address of locally-defined text would be OK, but that
131   // case isn't easy to detect.
132   return false;
133 }
134