[mips] The naming convention for private labels is ABI dependant.
[oota-llvm.git] / lib / Target / Mips / MipsTargetMachine.cpp
1 //===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implements the info about Mips target spec.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MipsTargetMachine.h"
15 #include "Mips.h"
16 #include "Mips16FrameLowering.h"
17 #include "Mips16ISelDAGToDAG.h"
18 #include "Mips16ISelLowering.h"
19 #include "Mips16InstrInfo.h"
20 #include "MipsFrameLowering.h"
21 #include "MipsInstrInfo.h"
22 #include "MipsSEFrameLowering.h"
23 #include "MipsSEISelDAGToDAG.h"
24 #include "MipsSEISelLowering.h"
25 #include "MipsSEInstrInfo.h"
26 #include "MipsTargetObjectFile.h"
27 #include "MCTargetDesc/MipsMCAsmInfo.h"
28 #include "llvm/Analysis/TargetTransformInfo.h"
29 #include "llvm/CodeGen/Passes.h"
30 #include "llvm/IR/LegacyPassManager.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Transforms/Scalar.h"
35
36 using namespace llvm;
37
38 #define DEBUG_TYPE "mips"
39
40 extern "C" void LLVMInitializeMipsTarget() {
41   // Register the target.
42   RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
43   RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
44   RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
45   RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
46 }
47
48 static std::string computeDataLayout(StringRef TT, StringRef CPU,
49                                      const TargetOptions &Options,
50                                      bool isLittle) {
51   std::string Ret = "";
52   MipsABIInfo ABI =
53       MipsABIInfo::computeTargetABI(Triple(TT), CPU, Options.MCOptions);
54
55   // There are both little and big endian mips.
56   if (isLittle)
57     Ret += "e";
58   else
59     Ret += "E";
60
61   Ret += "-m:m";
62
63   // Pointers are 32 bit on some ABIs.
64   if (!ABI.IsN64())
65     Ret += "-p:32:32";
66
67   // 8 and 16 bit integers only need no have natural alignment, but try to
68   // align them to 32 bits. 64 bit integers have natural alignment.
69   Ret += "-i8:8:32-i16:16:32-i64:64";
70
71   // 32 bit registers are always available and the stack is at least 64 bit
72   // aligned. On N64 64 bit registers are also available and the stack is
73   // 128 bit aligned.
74   if (ABI.IsN64() || ABI.IsN32())
75     Ret += "-n32:64-S128";
76   else
77     Ret += "-n32-S64";
78
79   return Ret;
80 }
81
82 // On function prologue, the stack is created by decrementing
83 // its pointer. Once decremented, all references are done with positive
84 // offset from the stack/frame pointer, using StackGrowsUp enables
85 // an easier handling.
86 // Using CodeModel::Large enables different CALL behavior.
87 MipsTargetMachine::MipsTargetMachine(const Target &T, StringRef TT,
88                                      StringRef CPU, StringRef FS,
89                                      const TargetOptions &Options,
90                                      Reloc::Model RM, CodeModel::Model CM,
91                                      CodeGenOpt::Level OL, bool isLittle)
92     : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
93                         CPU, FS, Options, RM, CM, OL),
94       isLittle(isLittle), TLOF(make_unique<MipsTargetObjectFile>()),
95       ABI(MipsABIInfo::computeTargetABI(Triple(TT), CPU, Options.MCOptions)),
96       Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this),
97       NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
98                         isLittle, *this),
99       Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
100                       isLittle, *this) {
101   Subtarget = &DefaultSubtarget;
102   initAsmInfo();
103 }
104
105 MipsTargetMachine::~MipsTargetMachine() {}
106
107 void MipsebTargetMachine::anchor() { }
108
109 MipsebTargetMachine::
110 MipsebTargetMachine(const Target &T, StringRef TT,
111                     StringRef CPU, StringRef FS, const TargetOptions &Options,
112                     Reloc::Model RM, CodeModel::Model CM,
113                     CodeGenOpt::Level OL)
114   : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
115
116 void MipselTargetMachine::anchor() { }
117
118 MipselTargetMachine::
119 MipselTargetMachine(const Target &T, StringRef TT,
120                     StringRef CPU, StringRef FS, const TargetOptions &Options,
121                     Reloc::Model RM, CodeModel::Model CM,
122                     CodeGenOpt::Level OL)
123   : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
124
125 const MipsSubtarget *
126 MipsTargetMachine::getSubtargetImpl(const Function &F) const {
127   Attribute CPUAttr = F.getFnAttribute("target-cpu");
128   Attribute FSAttr = F.getFnAttribute("target-features");
129
130   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
131                         ? CPUAttr.getValueAsString().str()
132                         : TargetCPU;
133   std::string FS = !FSAttr.hasAttribute(Attribute::None)
134                        ? FSAttr.getValueAsString().str()
135                        : TargetFS;
136   bool hasMips16Attr =
137       !F.getFnAttribute("mips16").hasAttribute(Attribute::None);
138   bool hasNoMips16Attr =
139       !F.getFnAttribute("nomips16").hasAttribute(Attribute::None);
140
141   // FIXME: This is related to the code below to reset the target options,
142   // we need to know whether or not the soft float flag is set on the
143   // function, so we can enable it as a subtarget feature.
144   bool softFloat =
145       F.hasFnAttribute("use-soft-float") &&
146       F.getFnAttribute("use-soft-float").getValueAsString() == "true";
147
148   if (hasMips16Attr)
149     FS += FS.empty() ? "+mips16" : ",+mips16";
150   else if (hasNoMips16Attr)
151     FS += FS.empty() ? "-mips16" : ",-mips16";
152   if (softFloat)
153     FS += FS.empty() ? "+soft-float" : ",+soft-float";
154
155   auto &I = SubtargetMap[CPU + FS];
156   if (!I) {
157     // This needs to be done before we create a new subtarget since any
158     // creation will depend on the TM and the code generation flags on the
159     // function that reside in TargetOptions.
160     resetTargetOptions(F);
161     I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle, *this);
162   }
163   return I.get();
164 }
165
166 void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
167   DEBUG(dbgs() << "resetSubtarget\n");
168
169   Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction()));
170   MF->setSubtarget(Subtarget);
171   return;
172 }
173
174 namespace {
175 /// Mips Code Generator Pass Configuration Options.
176 class MipsPassConfig : public TargetPassConfig {
177 public:
178   MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
179     : TargetPassConfig(TM, PM) {
180     // The current implementation of long branch pass requires a scratch
181     // register ($at) to be available before branch instructions. Tail merging
182     // can break this requirement, so disable it when long branch pass is
183     // enabled.
184     EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
185   }
186
187   MipsTargetMachine &getMipsTargetMachine() const {
188     return getTM<MipsTargetMachine>();
189   }
190
191   const MipsSubtarget &getMipsSubtarget() const {
192     return *getMipsTargetMachine().getSubtargetImpl();
193   }
194
195   void addIRPasses() override;
196   bool addInstSelector() override;
197   void addMachineSSAOptimization() override;
198   void addPreEmitPass() override;
199
200   void addPreRegAlloc() override;
201
202 };
203 } // namespace
204
205 TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
206   return new MipsPassConfig(this, PM);
207 }
208
209 void MipsPassConfig::addIRPasses() {
210   TargetPassConfig::addIRPasses();
211   addPass(createAtomicExpandPass(&getMipsTargetMachine()));
212   if (getMipsSubtarget().os16())
213     addPass(createMipsOs16Pass(getMipsTargetMachine()));
214   if (getMipsSubtarget().inMips16HardFloat())
215     addPass(createMips16HardFloatPass(getMipsTargetMachine()));
216 }
217 // Install an instruction selector pass using
218 // the ISelDag to gen Mips code.
219 bool MipsPassConfig::addInstSelector() {
220   addPass(createMipsModuleISelDagPass(getMipsTargetMachine()));
221   addPass(createMips16ISelDag(getMipsTargetMachine()));
222   addPass(createMipsSEISelDag(getMipsTargetMachine()));
223   return false;
224 }
225
226 void MipsPassConfig::addMachineSSAOptimization() {
227   addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
228   TargetPassConfig::addMachineSSAOptimization();
229 }
230
231 void MipsPassConfig::addPreRegAlloc() {
232   if (getOptLevel() == CodeGenOpt::None)
233     addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
234 }
235
236 TargetIRAnalysis MipsTargetMachine::getTargetIRAnalysis() {
237   return TargetIRAnalysis([this](Function &F) {
238     if (Subtarget->allowMixed16_32()) {
239       DEBUG(errs() << "No Target Transform Info Pass Added\n");
240       // FIXME: This is no longer necessary as the TTI returned is per-function.
241       return TargetTransformInfo(getDataLayout());
242     }
243
244     DEBUG(errs() << "Target Transform Info Pass Added\n");
245     return TargetTransformInfo(BasicTTIImpl(this, F));
246   });
247 }
248
249 // Implemented by targets that want to run passes immediately before
250 // machine code is emitted. return true if -print-machineinstrs should
251 // print out the code after the passes.
252 void MipsPassConfig::addPreEmitPass() {
253   MipsTargetMachine &TM = getMipsTargetMachine();
254   addPass(createMipsDelaySlotFillerPass(TM));
255   addPass(createMipsLongBranchPass(TM));
256   addPass(createMipsConstantIslandPass(TM));
257 }