1284c1978SDimitry Andric //===-- SystemZTargetMachine.cpp - Define TargetMachine for SystemZ -------===//
2284c1978SDimitry Andric //
3284c1978SDimitry Andric //                     The LLVM Compiler Infrastructure
4284c1978SDimitry Andric //
5284c1978SDimitry Andric // This file is distributed under the University of Illinois Open Source
6284c1978SDimitry Andric // License. See LICENSE.TXT for details.
7284c1978SDimitry Andric //
8284c1978SDimitry Andric //===----------------------------------------------------------------------===//
9284c1978SDimitry Andric 
10db17bf38SDimitry Andric #include "SystemZTargetMachine.h"
117a7e6055SDimitry Andric #include "MCTargetDesc/SystemZMCTargetDesc.h"
127a7e6055SDimitry Andric #include "SystemZ.h"
137a7e6055SDimitry Andric #include "SystemZMachineScheduler.h"
14ff0cc061SDimitry Andric #include "SystemZTargetTransformInfo.h"
157a7e6055SDimitry Andric #include "llvm/ADT/Optional.h"
167a7e6055SDimitry Andric #include "llvm/ADT/STLExtras.h"
17db17bf38SDimitry Andric #include "llvm/ADT/SmallVector.h"
187a7e6055SDimitry Andric #include "llvm/ADT/StringRef.h"
197a7e6055SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
20284c1978SDimitry Andric #include "llvm/CodeGen/Passes.h"
2139d628a0SDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
227a7e6055SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
237a7e6055SDimitry Andric #include "llvm/IR/DataLayout.h"
247a7e6055SDimitry Andric #include "llvm/Support/CodeGen.h"
257a7e6055SDimitry Andric #include "llvm/Support/TargetRegistry.h"
264ba319b5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
277a7e6055SDimitry Andric #include "llvm/Transforms/Scalar.h"
287a7e6055SDimitry Andric #include <string>
29284c1978SDimitry Andric 
30284c1978SDimitry Andric using namespace llvm;
31284c1978SDimitry Andric 
LLVMInitializeSystemZTarget()32284c1978SDimitry Andric extern "C" void LLVMInitializeSystemZTarget() {
33284c1978SDimitry Andric   // Register the target.
34d88c1a5aSDimitry Andric   RegisterTargetMachine<SystemZTargetMachine> X(getTheSystemZTarget());
35284c1978SDimitry Andric }
36284c1978SDimitry Andric 
37ff0cc061SDimitry Andric // Determine whether we use the vector ABI.
UsesVectorABI(StringRef CPU,StringRef FS)38ff0cc061SDimitry Andric static bool UsesVectorABI(StringRef CPU, StringRef FS) {
39ff0cc061SDimitry Andric   // We use the vector ABI whenever the vector facility is avaiable.
40ff0cc061SDimitry Andric   // This is the case by default if CPU is z13 or later, and can be
41ff0cc061SDimitry Andric   // overridden via "[+-]vector" feature string elements.
42ff0cc061SDimitry Andric   bool VectorABI = true;
43ff0cc061SDimitry Andric   if (CPU.empty() || CPU == "generic" ||
44ff0cc061SDimitry Andric       CPU == "z10" || CPU == "z196" || CPU == "zEC12")
45ff0cc061SDimitry Andric     VectorABI = false;
46ff0cc061SDimitry Andric 
47ff0cc061SDimitry Andric   SmallVector<StringRef, 3> Features;
487d523365SDimitry Andric   FS.split(Features, ',', -1, false /* KeepEmpty */);
49ff0cc061SDimitry Andric   for (auto &Feature : Features) {
50ff0cc061SDimitry Andric     if (Feature == "vector" || Feature == "+vector")
51ff0cc061SDimitry Andric       VectorABI = true;
52ff0cc061SDimitry Andric     if (Feature == "-vector")
53ff0cc061SDimitry Andric       VectorABI = false;
54ff0cc061SDimitry Andric   }
55ff0cc061SDimitry Andric 
56ff0cc061SDimitry Andric   return VectorABI;
57ff0cc061SDimitry Andric }
58ff0cc061SDimitry Andric 
computeDataLayout(const Triple & TT,StringRef CPU,StringRef FS)598f0fd8f6SDimitry Andric static std::string computeDataLayout(const Triple &TT, StringRef CPU,
60ff0cc061SDimitry Andric                                      StringRef FS) {
61ff0cc061SDimitry Andric   bool VectorABI = UsesVectorABI(CPU, FS);
627a7e6055SDimitry Andric   std::string Ret;
63ff0cc061SDimitry Andric 
64ff0cc061SDimitry Andric   // Big endian.
65ff0cc061SDimitry Andric   Ret += "E";
66ff0cc061SDimitry Andric 
67ff0cc061SDimitry Andric   // Data mangling.
688f0fd8f6SDimitry Andric   Ret += DataLayout::getManglingComponent(TT);
69ff0cc061SDimitry Andric 
70ff0cc061SDimitry Andric   // Make sure that global data has at least 16 bits of alignment by
71ff0cc061SDimitry Andric   // default, so that we can refer to it using LARL.  We don't have any
72ff0cc061SDimitry Andric   // special requirements for stack variables though.
73ff0cc061SDimitry Andric   Ret += "-i1:8:16-i8:8:16";
74ff0cc061SDimitry Andric 
75ff0cc061SDimitry Andric   // 64-bit integers are naturally aligned.
76ff0cc061SDimitry Andric   Ret += "-i64:64";
77ff0cc061SDimitry Andric 
78ff0cc061SDimitry Andric   // 128-bit floats are aligned only to 64 bits.
79ff0cc061SDimitry Andric   Ret += "-f128:64";
80ff0cc061SDimitry Andric 
81ff0cc061SDimitry Andric   // When using the vector ABI, 128-bit vectors are also aligned to 64 bits.
82ff0cc061SDimitry Andric   if (VectorABI)
83ff0cc061SDimitry Andric     Ret += "-v128:64";
84ff0cc061SDimitry Andric 
85ff0cc061SDimitry Andric   // We prefer 16 bits of aligned for all globals; see above.
86ff0cc061SDimitry Andric   Ret += "-a:8:16";
87ff0cc061SDimitry Andric 
88ff0cc061SDimitry Andric   // Integer registers are 32 or 64 bits.
89ff0cc061SDimitry Andric   Ret += "-n32:64";
90ff0cc061SDimitry Andric 
91ff0cc061SDimitry Andric   return Ret;
92ff0cc061SDimitry Andric }
93ff0cc061SDimitry Andric 
getEffectiveRelocModel(Optional<Reloc::Model> RM)943ca95b02SDimitry Andric static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
953ca95b02SDimitry Andric   // Static code is suitable for use in a dynamic executable; there is no
963ca95b02SDimitry Andric   // separate DynamicNoPIC model.
973ca95b02SDimitry Andric   if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC)
983ca95b02SDimitry Andric     return Reloc::Static;
993ca95b02SDimitry Andric   return *RM;
1003ca95b02SDimitry Andric }
1013ca95b02SDimitry Andric 
1022cab237bSDimitry Andric // For SystemZ we define the models as follows:
1032cab237bSDimitry Andric //
1042cab237bSDimitry Andric // Small:  BRASL can call any function and will use a stub if necessary.
1052cab237bSDimitry Andric //         Locally-binding symbols will always be in range of LARL.
1062cab237bSDimitry Andric //
1072cab237bSDimitry Andric // Medium: BRASL can call any function and will use a stub if necessary.
1082cab237bSDimitry Andric //         GOT slots and locally-defined text will always be in range
1092cab237bSDimitry Andric //         of LARL, but other symbols might not be.
1102cab237bSDimitry Andric //
1112cab237bSDimitry Andric // Large:  Equivalent to Medium for now.
1122cab237bSDimitry Andric //
1132cab237bSDimitry Andric // Kernel: Equivalent to Medium for now.
1142cab237bSDimitry Andric //
1152cab237bSDimitry Andric // This means that any PIC module smaller than 4GB meets the
1162cab237bSDimitry Andric // requirements of Small, so Small seems like the best default there.
1172cab237bSDimitry Andric //
1182cab237bSDimitry Andric // All symbols bind locally in a non-PIC module, so the choice is less
1192cab237bSDimitry Andric // obvious.  There are two cases:
1202cab237bSDimitry Andric //
1212cab237bSDimitry Andric // - When creating an executable, PLTs and copy relocations allow
1222cab237bSDimitry Andric //   us to treat external symbols as part of the executable.
1232cab237bSDimitry Andric //   Any executable smaller than 4GB meets the requirements of Small,
1242cab237bSDimitry Andric //   so that seems like the best default.
1252cab237bSDimitry Andric //
1262cab237bSDimitry Andric // - When creating JIT code, stubs will be in range of BRASL if the
1272cab237bSDimitry Andric //   image is less than 4GB in size.  GOT entries will likewise be
1282cab237bSDimitry Andric //   in range of LARL.  However, the JIT environment has no equivalent
1292cab237bSDimitry Andric //   of copy relocs, so locally-binding data symbols might not be in
1302cab237bSDimitry Andric //   the range of LARL.  We need the Medium model in that case.
131*b5893f02SDimitry Andric static CodeModel::Model
getEffectiveSystemZCodeModel(Optional<CodeModel::Model> CM,Reloc::Model RM,bool JIT)132*b5893f02SDimitry Andric getEffectiveSystemZCodeModel(Optional<CodeModel::Model> CM, Reloc::Model RM,
133*b5893f02SDimitry Andric                              bool JIT) {
134*b5893f02SDimitry Andric   if (CM) {
135*b5893f02SDimitry Andric     if (*CM == CodeModel::Tiny)
136*b5893f02SDimitry Andric       report_fatal_error("Target does not support the tiny CodeModel");
137*b5893f02SDimitry Andric     if (*CM == CodeModel::Kernel)
138*b5893f02SDimitry Andric       report_fatal_error("Target does not support the kernel CodeModel");
1392cab237bSDimitry Andric     return *CM;
140*b5893f02SDimitry Andric   }
1412cab237bSDimitry Andric   if (JIT)
1422cab237bSDimitry Andric     return RM == Reloc::PIC_ ? CodeModel::Small : CodeModel::Medium;
1432cab237bSDimitry Andric   return CodeModel::Small;
1442cab237bSDimitry Andric }
1452cab237bSDimitry Andric 
SystemZTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,Optional<CodeModel::Model> CM,CodeGenOpt::Level OL,bool JIT)1468f0fd8f6SDimitry Andric SystemZTargetMachine::SystemZTargetMachine(const Target &T, const Triple &TT,
147284c1978SDimitry Andric                                            StringRef CPU, StringRef FS,
148284c1978SDimitry Andric                                            const TargetOptions &Options,
1493ca95b02SDimitry Andric                                            Optional<Reloc::Model> RM,
1502cab237bSDimitry Andric                                            Optional<CodeModel::Model> CM,
1512cab237bSDimitry Andric                                            CodeGenOpt::Level OL, bool JIT)
1522cab237bSDimitry Andric     : LLVMTargetMachine(
1532cab237bSDimitry Andric           T, computeDataLayout(TT, CPU, FS), TT, CPU, FS, Options,
1542cab237bSDimitry Andric           getEffectiveRelocModel(RM),
155*b5893f02SDimitry Andric           getEffectiveSystemZCodeModel(CM, getEffectiveRelocModel(RM), JIT),
156*b5893f02SDimitry Andric           OL),
1577a7e6055SDimitry Andric       TLOF(llvm::make_unique<TargetLoweringObjectFileELF>()),
15891bc56edSDimitry Andric       Subtarget(TT, CPU, FS, *this) {
159f785676fSDimitry Andric   initAsmInfo();
160284c1978SDimitry Andric }
161284c1978SDimitry Andric 
1627a7e6055SDimitry Andric SystemZTargetMachine::~SystemZTargetMachine() = default;
16339d628a0SDimitry Andric 
164284c1978SDimitry Andric namespace {
1657a7e6055SDimitry Andric 
166284c1978SDimitry Andric /// SystemZ Code Generator Pass Configuration Options.
167284c1978SDimitry Andric class SystemZPassConfig : public TargetPassConfig {
168284c1978SDimitry Andric public:
SystemZPassConfig(SystemZTargetMachine & TM,PassManagerBase & PM)169f9448bf3SDimitry Andric   SystemZPassConfig(SystemZTargetMachine &TM, PassManagerBase &PM)
170284c1978SDimitry Andric     : TargetPassConfig(TM, PM) {}
171284c1978SDimitry Andric 
getSystemZTargetMachine() const172284c1978SDimitry Andric   SystemZTargetMachine &getSystemZTargetMachine() const {
173284c1978SDimitry Andric     return getTM<SystemZTargetMachine>();
174284c1978SDimitry Andric   }
175284c1978SDimitry Andric 
176d88c1a5aSDimitry Andric   ScheduleDAGInstrs *
createPostMachineScheduler(MachineSchedContext * C) const177d88c1a5aSDimitry Andric   createPostMachineScheduler(MachineSchedContext *C) const override {
1787a7e6055SDimitry Andric     return new ScheduleDAGMI(C,
1797a7e6055SDimitry Andric                              llvm::make_unique<SystemZPostRASchedStrategy>(C),
180d88c1a5aSDimitry Andric                              /*RemoveKillFlags=*/true);
181d88c1a5aSDimitry Andric   }
182d88c1a5aSDimitry Andric 
18391bc56edSDimitry Andric   void addIRPasses() override;
18491bc56edSDimitry Andric   bool addInstSelector() override;
185d88c1a5aSDimitry Andric   bool addILPOpts() override;
18639d628a0SDimitry Andric   void addPreSched2() override;
18739d628a0SDimitry Andric   void addPreEmitPass() override;
188284c1978SDimitry Andric };
1897a7e6055SDimitry Andric 
190284c1978SDimitry Andric } // end anonymous namespace
191284c1978SDimitry Andric 
addIRPasses()192f785676fSDimitry Andric void SystemZPassConfig::addIRPasses() {
193b40b48b8SDimitry Andric   if (getOptLevel() != CodeGenOpt::None) {
1943ca95b02SDimitry Andric     addPass(createSystemZTDCPass());
195b40b48b8SDimitry Andric     addPass(createLoopDataPrefetchPass());
196b40b48b8SDimitry Andric   }
1973ca95b02SDimitry Andric 
198f785676fSDimitry Andric   TargetPassConfig::addIRPasses();
199f785676fSDimitry Andric }
200f785676fSDimitry Andric 
addInstSelector()201284c1978SDimitry Andric bool SystemZPassConfig::addInstSelector() {
202284c1978SDimitry Andric   addPass(createSystemZISelDag(getSystemZTargetMachine(), getOptLevel()));
203ff0cc061SDimitry Andric 
204ff0cc061SDimitry Andric  if (getOptLevel() != CodeGenOpt::None)
205ff0cc061SDimitry Andric     addPass(createSystemZLDCleanupPass(getSystemZTargetMachine()));
206ff0cc061SDimitry Andric 
207284c1978SDimitry Andric   return false;
208284c1978SDimitry Andric }
209284c1978SDimitry Andric 
addILPOpts()210d88c1a5aSDimitry Andric bool SystemZPassConfig::addILPOpts() {
211d88c1a5aSDimitry Andric   addPass(&EarlyIfConverterID);
212d88c1a5aSDimitry Andric   return true;
213d88c1a5aSDimitry Andric }
214d88c1a5aSDimitry Andric 
addPreSched2()21539d628a0SDimitry Andric void SystemZPassConfig::addPreSched2() {
216d88c1a5aSDimitry Andric   addPass(createSystemZExpandPseudoPass(getSystemZTargetMachine()));
217d88c1a5aSDimitry Andric 
2183ca95b02SDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
219f785676fSDimitry Andric     addPass(&IfConverterID);
220f785676fSDimitry Andric }
221f785676fSDimitry Andric 
addPreEmitPass()22239d628a0SDimitry Andric void SystemZPassConfig::addPreEmitPass() {
2237d523365SDimitry Andric   // Do instruction shortening before compare elimination because some
2247d523365SDimitry Andric   // vector instructions will be shortened into opcodes that compare
2257d523365SDimitry Andric   // elimination recognizes.
2267d523365SDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
2277d523365SDimitry Andric     addPass(createSystemZShortenInstPass(getSystemZTargetMachine()), false);
2287d523365SDimitry Andric 
229f785676fSDimitry Andric   // We eliminate comparisons here rather than earlier because some
230f785676fSDimitry Andric   // transformations can change the set of available CC values and we
231f785676fSDimitry Andric   // generally want those transformations to have priority.  This is
232f785676fSDimitry Andric   // especially true in the commonest case where the result of the comparison
233f785676fSDimitry Andric   // is used by a single in-range branch instruction, since we will then
234f785676fSDimitry Andric   // be able to fuse the compare and the branch instead.
235f785676fSDimitry Andric   //
236f785676fSDimitry Andric   // For example, two-address NILF can sometimes be converted into
237f785676fSDimitry Andric   // three-address RISBLG.  NILF produces a CC value that indicates whether
238f785676fSDimitry Andric   // the low word is zero, but RISBLG does not modify CC at all.  On the
239f785676fSDimitry Andric   // other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG.
240f785676fSDimitry Andric   // The CC value produced by NILL isn't useful for our purposes, but the
241f785676fSDimitry Andric   // value produced by RISBG can be used for any comparison with zero
242f785676fSDimitry Andric   // (not just equality).  So there are some transformations that lose
243f785676fSDimitry Andric   // CC values (while still being worthwhile) and others that happen to make
244f785676fSDimitry Andric   // the CC result more useful than it was originally.
245f785676fSDimitry Andric   //
246f785676fSDimitry Andric   // Another reason is that we only want to use BRANCH ON COUNT in cases
247f785676fSDimitry Andric   // where we know that the count register is not going to be spilled.
248f785676fSDimitry Andric   //
249f785676fSDimitry Andric   // Doing it so late makes it more likely that a register will be reused
250f785676fSDimitry Andric   // between the comparison and the branch, but it isn't clear whether
251f785676fSDimitry Andric   // preventing that would be a win or not.
252f785676fSDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
25339d628a0SDimitry Andric     addPass(createSystemZElimComparePass(getSystemZTargetMachine()), false);
254f785676fSDimitry Andric   addPass(createSystemZLongBranchPass(getSystemZTargetMachine()));
2557d523365SDimitry Andric 
2567d523365SDimitry Andric   // Do final scheduling after all other optimizations, to get an
2577d523365SDimitry Andric   // optimal input for the decoder (branch relaxation must happen
2587d523365SDimitry Andric   // after block placement).
259d88c1a5aSDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
2607d523365SDimitry Andric     addPass(&PostMachineSchedulerID);
261f785676fSDimitry Andric }
262f785676fSDimitry Andric 
createPassConfig(PassManagerBase & PM)263284c1978SDimitry Andric TargetPassConfig *SystemZTargetMachine::createPassConfig(PassManagerBase &PM) {
264f9448bf3SDimitry Andric   return new SystemZPassConfig(*this, PM);
265284c1978SDimitry Andric }
266ff0cc061SDimitry Andric 
267da09e106SDimitry Andric TargetTransformInfo
getTargetTransformInfo(const Function & F)268da09e106SDimitry Andric SystemZTargetMachine::getTargetTransformInfo(const Function &F) {
269ff0cc061SDimitry Andric   return TargetTransformInfo(SystemZTTIImpl(this, F));
270ff0cc061SDimitry Andric }
271