339542ed2fd623c5d95f618b08ca106413ffcf94
[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 "Mips16HardFloat.h"
18 #include "Mips16ISelDAGToDAG.h"
19 #include "Mips16ISelLowering.h"
20 #include "Mips16InstrInfo.h"
21 #include "MipsFrameLowering.h"
22 #include "MipsInstrInfo.h"
23 #include "MipsModuleISelDAGToDAG.h"
24 #include "MipsOs16.h"
25 #include "MipsSEFrameLowering.h"
26 #include "MipsSEISelDAGToDAG.h"
27 #include "MipsSEISelLowering.h"
28 #include "MipsSEInstrInfo.h"
29 #include "MipsTargetObjectFile.h"
30 #include "llvm/Analysis/TargetTransformInfo.h"
31 #include "llvm/CodeGen/Passes.h"
32 #include "llvm/PassManager.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/TargetRegistry.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Transforms/Scalar.h"
37 using namespace llvm;
38
39 #define DEBUG_TYPE "mips"
40
41 extern "C" void LLVMInitializeMipsTarget() {
42   // Register the target.
43   RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
44   RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
45   RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
46   RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
47 }
48
49 // On function prologue, the stack is created by decrementing
50 // its pointer. Once decremented, all references are done with positive
51 // offset from the stack/frame pointer, using StackGrowsUp enables
52 // an easier handling.
53 // Using CodeModel::Large enables different CALL behavior.
54 MipsTargetMachine::MipsTargetMachine(const Target &T, StringRef TT,
55                                      StringRef CPU, StringRef FS,
56                                      const TargetOptions &Options,
57                                      Reloc::Model RM, CodeModel::Model CM,
58                                      CodeGenOpt::Level OL, bool isLittle)
59     : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
60       isLittle(isLittle), TLOF(make_unique<MipsTargetObjectFile>()),
61       ABI(MipsABIInfo::computeTargetABI(Triple(TT), CPU, Options.MCOptions)),
62       Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this),
63       NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
64                         isLittle, *this),
65       Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
66                       isLittle, *this) {
67   Subtarget = &DefaultSubtarget;
68   initAsmInfo();
69 }
70
71 MipsTargetMachine::~MipsTargetMachine() {}
72
73 void MipsebTargetMachine::anchor() { }
74
75 MipsebTargetMachine::
76 MipsebTargetMachine(const Target &T, StringRef TT,
77                     StringRef CPU, StringRef FS, const TargetOptions &Options,
78                     Reloc::Model RM, CodeModel::Model CM,
79                     CodeGenOpt::Level OL)
80   : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
81
82 void MipselTargetMachine::anchor() { }
83
84 MipselTargetMachine::
85 MipselTargetMachine(const Target &T, StringRef TT,
86                     StringRef CPU, StringRef FS, const TargetOptions &Options,
87                     Reloc::Model RM, CodeModel::Model CM,
88                     CodeGenOpt::Level OL)
89   : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
90
91 const MipsSubtarget *
92 MipsTargetMachine::getSubtargetImpl(const Function &F) const {
93   AttributeSet FnAttrs = F.getAttributes();
94   Attribute CPUAttr =
95       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
96   Attribute FSAttr =
97       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
98
99   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
100                         ? CPUAttr.getValueAsString().str()
101                         : TargetCPU;
102   std::string FS = !FSAttr.hasAttribute(Attribute::None)
103                        ? FSAttr.getValueAsString().str()
104                        : TargetFS;
105   bool hasMips16Attr =
106       !FnAttrs.getAttribute(AttributeSet::FunctionIndex, "mips16")
107            .hasAttribute(Attribute::None);
108   bool hasNoMips16Attr =
109       !FnAttrs.getAttribute(AttributeSet::FunctionIndex, "nomips16")
110            .hasAttribute(Attribute::None);
111
112   // FIXME: This is related to the code below to reset the target options,
113   // we need to know whether or not the soft float flag is set on the
114   // function before we can generate a subtarget. We also need to use
115   // it as a key for the subtarget since that can be the only difference
116   // between two functions.
117   Attribute SFAttr =
118       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "use-soft-float");
119   bool softFloat = !SFAttr.hasAttribute(Attribute::None)
120                        ? SFAttr.getValueAsString() == "true"
121                        : Options.UseSoftFloat;
122
123   if (hasMips16Attr)
124     FS += FS.empty() ? "+mips16" : ",+mips16";
125   else if (hasNoMips16Attr)
126     FS += FS.empty() ? "-mips16" : ",-mips16";
127
128   auto &I = SubtargetMap[CPU + FS + (softFloat ? "use-soft-float=true"
129                                                : "use-soft-float=false")];
130   if (!I) {
131     // This needs to be done before we create a new subtarget since any
132     // creation will depend on the TM and the code generation flags on the
133     // function that reside in TargetOptions.
134     resetTargetOptions(F);
135     I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle, *this);
136   }
137   return I.get();
138 }
139
140 void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
141   DEBUG(dbgs() << "resetSubtarget\n");
142
143   Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction()));
144   MF->setSubtarget(Subtarget);
145   return;
146 }
147
148 namespace {
149 /// Mips Code Generator Pass Configuration Options.
150 class MipsPassConfig : public TargetPassConfig {
151 public:
152   MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
153     : TargetPassConfig(TM, PM) {
154     // The current implementation of long branch pass requires a scratch
155     // register ($at) to be available before branch instructions. Tail merging
156     // can break this requirement, so disable it when long branch pass is
157     // enabled.
158     EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
159   }
160
161   MipsTargetMachine &getMipsTargetMachine() const {
162     return getTM<MipsTargetMachine>();
163   }
164
165   const MipsSubtarget &getMipsSubtarget() const {
166     return *getMipsTargetMachine().getSubtargetImpl();
167   }
168
169   void addIRPasses() override;
170   bool addInstSelector() override;
171   void addMachineSSAOptimization() override;
172   void addPreEmitPass() override;
173
174   void addPreRegAlloc() override;
175
176 };
177 } // namespace
178
179 TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
180   return new MipsPassConfig(this, PM);
181 }
182
183 void MipsPassConfig::addIRPasses() {
184   TargetPassConfig::addIRPasses();
185   addPass(createAtomicExpandPass(&getMipsTargetMachine()));
186   if (getMipsSubtarget().os16())
187     addPass(createMipsOs16(getMipsTargetMachine()));
188   if (getMipsSubtarget().inMips16HardFloat())
189     addPass(createMips16HardFloat(getMipsTargetMachine()));
190 }
191 // Install an instruction selector pass using
192 // the ISelDag to gen Mips code.
193 bool MipsPassConfig::addInstSelector() {
194   addPass(createMipsModuleISelDag(getMipsTargetMachine()));
195   addPass(createMips16ISelDag(getMipsTargetMachine()));
196   addPass(createMipsSEISelDag(getMipsTargetMachine()));
197   return false;
198 }
199
200 void MipsPassConfig::addMachineSSAOptimization() {
201   addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
202   TargetPassConfig::addMachineSSAOptimization();
203 }
204
205 void MipsPassConfig::addPreRegAlloc() {
206   if (getOptLevel() == CodeGenOpt::None)
207     addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
208 }
209
210 void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
211   if (Subtarget->allowMixed16_32()) {
212     DEBUG(errs() << "No ");
213     //FIXME: The Basic Target Transform Info
214     // pass needs to become a function pass instead of
215     // being an immutable pass and then this method as it exists now
216     // would be unnecessary.
217     PM.add(createNoTargetTransformInfoPass());
218   } else
219     LLVMTargetMachine::addAnalysisPasses(PM);
220   DEBUG(errs() << "Target Transform Info Pass Added\n");
221 }
222
223 // Implemented by targets that want to run passes immediately before
224 // machine code is emitted. return true if -print-machineinstrs should
225 // print out the code after the passes.
226 void MipsPassConfig::addPreEmitPass() {
227   MipsTargetMachine &TM = getMipsTargetMachine();
228   addPass(createMipsDelaySlotFillerPass(TM));
229   addPass(createMipsLongBranchPass(TM));
230   addPass(createMipsConstantIslandPass(TM));
231 }