xref: /aosp_15_r20/external/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- SystemZTargetMachine.cpp - Define TargetMachine for SystemZ -------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker 
10*9880d681SAndroid Build Coastguard Worker #include "SystemZTargetMachine.h"
11*9880d681SAndroid Build Coastguard Worker #include "SystemZTargetTransformInfo.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/TargetPassConfig.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/TargetRegistry.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
17*9880d681SAndroid Build Coastguard Worker 
18*9880d681SAndroid Build Coastguard Worker using namespace llvm;
19*9880d681SAndroid Build Coastguard Worker 
20*9880d681SAndroid Build Coastguard Worker extern cl::opt<bool> MISchedPostRA;
LLVMInitializeSystemZTarget()21*9880d681SAndroid Build Coastguard Worker extern "C" void LLVMInitializeSystemZTarget() {
22*9880d681SAndroid Build Coastguard Worker   // Register the target.
23*9880d681SAndroid Build Coastguard Worker   RegisterTargetMachine<SystemZTargetMachine> X(TheSystemZTarget);
24*9880d681SAndroid Build Coastguard Worker }
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker // Determine whether we use the vector ABI.
UsesVectorABI(StringRef CPU,StringRef FS)27*9880d681SAndroid Build Coastguard Worker static bool UsesVectorABI(StringRef CPU, StringRef FS) {
28*9880d681SAndroid Build Coastguard Worker   // We use the vector ABI whenever the vector facility is avaiable.
29*9880d681SAndroid Build Coastguard Worker   // This is the case by default if CPU is z13 or later, and can be
30*9880d681SAndroid Build Coastguard Worker   // overridden via "[+-]vector" feature string elements.
31*9880d681SAndroid Build Coastguard Worker   bool VectorABI = true;
32*9880d681SAndroid Build Coastguard Worker   if (CPU.empty() || CPU == "generic" ||
33*9880d681SAndroid Build Coastguard Worker       CPU == "z10" || CPU == "z196" || CPU == "zEC12")
34*9880d681SAndroid Build Coastguard Worker     VectorABI = false;
35*9880d681SAndroid Build Coastguard Worker 
36*9880d681SAndroid Build Coastguard Worker   SmallVector<StringRef, 3> Features;
37*9880d681SAndroid Build Coastguard Worker   FS.split(Features, ',', -1, false /* KeepEmpty */);
38*9880d681SAndroid Build Coastguard Worker   for (auto &Feature : Features) {
39*9880d681SAndroid Build Coastguard Worker     if (Feature == "vector" || Feature == "+vector")
40*9880d681SAndroid Build Coastguard Worker       VectorABI = true;
41*9880d681SAndroid Build Coastguard Worker     if (Feature == "-vector")
42*9880d681SAndroid Build Coastguard Worker       VectorABI = false;
43*9880d681SAndroid Build Coastguard Worker   }
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker   return VectorABI;
46*9880d681SAndroid Build Coastguard Worker }
47*9880d681SAndroid Build Coastguard Worker 
computeDataLayout(const Triple & TT,StringRef CPU,StringRef FS)48*9880d681SAndroid Build Coastguard Worker static std::string computeDataLayout(const Triple &TT, StringRef CPU,
49*9880d681SAndroid Build Coastguard Worker                                      StringRef FS) {
50*9880d681SAndroid Build Coastguard Worker   bool VectorABI = UsesVectorABI(CPU, FS);
51*9880d681SAndroid Build Coastguard Worker   std::string Ret = "";
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker   // Big endian.
54*9880d681SAndroid Build Coastguard Worker   Ret += "E";
55*9880d681SAndroid Build Coastguard Worker 
56*9880d681SAndroid Build Coastguard Worker   // Data mangling.
57*9880d681SAndroid Build Coastguard Worker   Ret += DataLayout::getManglingComponent(TT);
58*9880d681SAndroid Build Coastguard Worker 
59*9880d681SAndroid Build Coastguard Worker   // Make sure that global data has at least 16 bits of alignment by
60*9880d681SAndroid Build Coastguard Worker   // default, so that we can refer to it using LARL.  We don't have any
61*9880d681SAndroid Build Coastguard Worker   // special requirements for stack variables though.
62*9880d681SAndroid Build Coastguard Worker   Ret += "-i1:8:16-i8:8:16";
63*9880d681SAndroid Build Coastguard Worker 
64*9880d681SAndroid Build Coastguard Worker   // 64-bit integers are naturally aligned.
65*9880d681SAndroid Build Coastguard Worker   Ret += "-i64:64";
66*9880d681SAndroid Build Coastguard Worker 
67*9880d681SAndroid Build Coastguard Worker   // 128-bit floats are aligned only to 64 bits.
68*9880d681SAndroid Build Coastguard Worker   Ret += "-f128:64";
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker   // When using the vector ABI, 128-bit vectors are also aligned to 64 bits.
71*9880d681SAndroid Build Coastguard Worker   if (VectorABI)
72*9880d681SAndroid Build Coastguard Worker     Ret += "-v128:64";
73*9880d681SAndroid Build Coastguard Worker 
74*9880d681SAndroid Build Coastguard Worker   // We prefer 16 bits of aligned for all globals; see above.
75*9880d681SAndroid Build Coastguard Worker   Ret += "-a:8:16";
76*9880d681SAndroid Build Coastguard Worker 
77*9880d681SAndroid Build Coastguard Worker   // Integer registers are 32 or 64 bits.
78*9880d681SAndroid Build Coastguard Worker   Ret += "-n32:64";
79*9880d681SAndroid Build Coastguard Worker 
80*9880d681SAndroid Build Coastguard Worker   return Ret;
81*9880d681SAndroid Build Coastguard Worker }
82*9880d681SAndroid Build Coastguard Worker 
getEffectiveRelocModel(Optional<Reloc::Model> RM)83*9880d681SAndroid Build Coastguard Worker static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
84*9880d681SAndroid Build Coastguard Worker   // Static code is suitable for use in a dynamic executable; there is no
85*9880d681SAndroid Build Coastguard Worker   // separate DynamicNoPIC model.
86*9880d681SAndroid Build Coastguard Worker   if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC)
87*9880d681SAndroid Build Coastguard Worker     return Reloc::Static;
88*9880d681SAndroid Build Coastguard Worker   return *RM;
89*9880d681SAndroid Build Coastguard Worker }
90*9880d681SAndroid Build Coastguard Worker 
SystemZTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,CodeModel::Model CM,CodeGenOpt::Level OL)91*9880d681SAndroid Build Coastguard Worker SystemZTargetMachine::SystemZTargetMachine(const Target &T, const Triple &TT,
92*9880d681SAndroid Build Coastguard Worker                                            StringRef CPU, StringRef FS,
93*9880d681SAndroid Build Coastguard Worker                                            const TargetOptions &Options,
94*9880d681SAndroid Build Coastguard Worker                                            Optional<Reloc::Model> RM,
95*9880d681SAndroid Build Coastguard Worker                                            CodeModel::Model CM,
96*9880d681SAndroid Build Coastguard Worker                                            CodeGenOpt::Level OL)
97*9880d681SAndroid Build Coastguard Worker     : LLVMTargetMachine(T, computeDataLayout(TT, CPU, FS), TT, CPU, FS, Options,
98*9880d681SAndroid Build Coastguard Worker                         getEffectiveRelocModel(RM), CM, OL),
99*9880d681SAndroid Build Coastguard Worker       TLOF(make_unique<TargetLoweringObjectFileELF>()),
100*9880d681SAndroid Build Coastguard Worker       Subtarget(TT, CPU, FS, *this) {
101*9880d681SAndroid Build Coastguard Worker   initAsmInfo();
102*9880d681SAndroid Build Coastguard Worker }
103*9880d681SAndroid Build Coastguard Worker 
~SystemZTargetMachine()104*9880d681SAndroid Build Coastguard Worker SystemZTargetMachine::~SystemZTargetMachine() {}
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker namespace {
107*9880d681SAndroid Build Coastguard Worker /// SystemZ Code Generator Pass Configuration Options.
108*9880d681SAndroid Build Coastguard Worker class SystemZPassConfig : public TargetPassConfig {
109*9880d681SAndroid Build Coastguard Worker public:
SystemZPassConfig(SystemZTargetMachine * TM,PassManagerBase & PM)110*9880d681SAndroid Build Coastguard Worker   SystemZPassConfig(SystemZTargetMachine *TM, PassManagerBase &PM)
111*9880d681SAndroid Build Coastguard Worker     : TargetPassConfig(TM, PM) {}
112*9880d681SAndroid Build Coastguard Worker 
getSystemZTargetMachine() const113*9880d681SAndroid Build Coastguard Worker   SystemZTargetMachine &getSystemZTargetMachine() const {
114*9880d681SAndroid Build Coastguard Worker     return getTM<SystemZTargetMachine>();
115*9880d681SAndroid Build Coastguard Worker   }
116*9880d681SAndroid Build Coastguard Worker 
117*9880d681SAndroid Build Coastguard Worker   void addIRPasses() override;
118*9880d681SAndroid Build Coastguard Worker   bool addInstSelector() override;
119*9880d681SAndroid Build Coastguard Worker   void addPreSched2() override;
120*9880d681SAndroid Build Coastguard Worker   void addPreEmitPass() override;
121*9880d681SAndroid Build Coastguard Worker };
122*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
123*9880d681SAndroid Build Coastguard Worker 
addIRPasses()124*9880d681SAndroid Build Coastguard Worker void SystemZPassConfig::addIRPasses() {
125*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None)
126*9880d681SAndroid Build Coastguard Worker     addPass(createSystemZTDCPass());
127*9880d681SAndroid Build Coastguard Worker 
128*9880d681SAndroid Build Coastguard Worker   TargetPassConfig::addIRPasses();
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker 
addInstSelector()131*9880d681SAndroid Build Coastguard Worker bool SystemZPassConfig::addInstSelector() {
132*9880d681SAndroid Build Coastguard Worker   addPass(createSystemZISelDag(getSystemZTargetMachine(), getOptLevel()));
133*9880d681SAndroid Build Coastguard Worker 
134*9880d681SAndroid Build Coastguard Worker  if (getOptLevel() != CodeGenOpt::None)
135*9880d681SAndroid Build Coastguard Worker     addPass(createSystemZLDCleanupPass(getSystemZTargetMachine()));
136*9880d681SAndroid Build Coastguard Worker 
137*9880d681SAndroid Build Coastguard Worker   return false;
138*9880d681SAndroid Build Coastguard Worker }
139*9880d681SAndroid Build Coastguard Worker 
addPreSched2()140*9880d681SAndroid Build Coastguard Worker void SystemZPassConfig::addPreSched2() {
141*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None)
142*9880d681SAndroid Build Coastguard Worker     addPass(&IfConverterID);
143*9880d681SAndroid Build Coastguard Worker }
144*9880d681SAndroid Build Coastguard Worker 
addPreEmitPass()145*9880d681SAndroid Build Coastguard Worker void SystemZPassConfig::addPreEmitPass() {
146*9880d681SAndroid Build Coastguard Worker 
147*9880d681SAndroid Build Coastguard Worker   // Do instruction shortening before compare elimination because some
148*9880d681SAndroid Build Coastguard Worker   // vector instructions will be shortened into opcodes that compare
149*9880d681SAndroid Build Coastguard Worker   // elimination recognizes.
150*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None)
151*9880d681SAndroid Build Coastguard Worker     addPass(createSystemZShortenInstPass(getSystemZTargetMachine()), false);
152*9880d681SAndroid Build Coastguard Worker 
153*9880d681SAndroid Build Coastguard Worker   // We eliminate comparisons here rather than earlier because some
154*9880d681SAndroid Build Coastguard Worker   // transformations can change the set of available CC values and we
155*9880d681SAndroid Build Coastguard Worker   // generally want those transformations to have priority.  This is
156*9880d681SAndroid Build Coastguard Worker   // especially true in the commonest case where the result of the comparison
157*9880d681SAndroid Build Coastguard Worker   // is used by a single in-range branch instruction, since we will then
158*9880d681SAndroid Build Coastguard Worker   // be able to fuse the compare and the branch instead.
159*9880d681SAndroid Build Coastguard Worker   //
160*9880d681SAndroid Build Coastguard Worker   // For example, two-address NILF can sometimes be converted into
161*9880d681SAndroid Build Coastguard Worker   // three-address RISBLG.  NILF produces a CC value that indicates whether
162*9880d681SAndroid Build Coastguard Worker   // the low word is zero, but RISBLG does not modify CC at all.  On the
163*9880d681SAndroid Build Coastguard Worker   // other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG.
164*9880d681SAndroid Build Coastguard Worker   // The CC value produced by NILL isn't useful for our purposes, but the
165*9880d681SAndroid Build Coastguard Worker   // value produced by RISBG can be used for any comparison with zero
166*9880d681SAndroid Build Coastguard Worker   // (not just equality).  So there are some transformations that lose
167*9880d681SAndroid Build Coastguard Worker   // CC values (while still being worthwhile) and others that happen to make
168*9880d681SAndroid Build Coastguard Worker   // the CC result more useful than it was originally.
169*9880d681SAndroid Build Coastguard Worker   //
170*9880d681SAndroid Build Coastguard Worker   // Another reason is that we only want to use BRANCH ON COUNT in cases
171*9880d681SAndroid Build Coastguard Worker   // where we know that the count register is not going to be spilled.
172*9880d681SAndroid Build Coastguard Worker   //
173*9880d681SAndroid Build Coastguard Worker   // Doing it so late makes it more likely that a register will be reused
174*9880d681SAndroid Build Coastguard Worker   // between the comparison and the branch, but it isn't clear whether
175*9880d681SAndroid Build Coastguard Worker   // preventing that would be a win or not.
176*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None)
177*9880d681SAndroid Build Coastguard Worker     addPass(createSystemZElimComparePass(getSystemZTargetMachine()), false);
178*9880d681SAndroid Build Coastguard Worker   addPass(createSystemZLongBranchPass(getSystemZTargetMachine()));
179*9880d681SAndroid Build Coastguard Worker 
180*9880d681SAndroid Build Coastguard Worker   // Do final scheduling after all other optimizations, to get an
181*9880d681SAndroid Build Coastguard Worker   // optimal input for the decoder (branch relaxation must happen
182*9880d681SAndroid Build Coastguard Worker   // after block placement).
183*9880d681SAndroid Build Coastguard Worker   if (getOptLevel() != CodeGenOpt::None) {
184*9880d681SAndroid Build Coastguard Worker     if (MISchedPostRA)
185*9880d681SAndroid Build Coastguard Worker       addPass(&PostMachineSchedulerID);
186*9880d681SAndroid Build Coastguard Worker     else
187*9880d681SAndroid Build Coastguard Worker       addPass(&PostRASchedulerID);
188*9880d681SAndroid Build Coastguard Worker   }
189*9880d681SAndroid Build Coastguard Worker }
190*9880d681SAndroid Build Coastguard Worker 
createPassConfig(PassManagerBase & PM)191*9880d681SAndroid Build Coastguard Worker TargetPassConfig *SystemZTargetMachine::createPassConfig(PassManagerBase &PM) {
192*9880d681SAndroid Build Coastguard Worker   return new SystemZPassConfig(this, PM);
193*9880d681SAndroid Build Coastguard Worker }
194*9880d681SAndroid Build Coastguard Worker 
getTargetIRAnalysis()195*9880d681SAndroid Build Coastguard Worker TargetIRAnalysis SystemZTargetMachine::getTargetIRAnalysis() {
196*9880d681SAndroid Build Coastguard Worker   return TargetIRAnalysis([this](const Function &F) {
197*9880d681SAndroid Build Coastguard Worker     return TargetTransformInfo(SystemZTTIImpl(this, F));
198*9880d681SAndroid Build Coastguard Worker   });
199*9880d681SAndroid Build Coastguard Worker }
200