1 //===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements the info about Mips target spec.
12 //===----------------------------------------------------------------------===//
14 #include "MipsTargetMachine.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 "llvm/Analysis/TargetTransformInfo.h"
28 #include "llvm/CodeGen/Passes.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/TargetRegistry.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Transforms/Scalar.h"
37 #define DEBUG_TYPE "mips"
39 extern "C" void LLVMInitializeMipsTarget() {
40 // Register the target.
41 RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
42 RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
43 RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
44 RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
47 static std::string computeDataLayout(const Triple &TT, StringRef CPU,
48 const TargetOptions &Options,
51 MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions);
53 // There are both little and big endian mips.
61 // Pointers are 32 bit on some ABIs.
65 // 8 and 16 bit integers only need no have natural alignment, but try to
66 // align them to 32 bits. 64 bit integers have natural alignment.
67 Ret += "-i8:8:32-i16:16:32-i64:64";
69 // 32 bit registers are always available and the stack is at least 64 bit
70 // aligned. On N64 64 bit registers are also available and the stack is
72 if (ABI.IsN64() || ABI.IsN32())
73 Ret += "-n32:64-S128";
80 // On function prologue, the stack is created by decrementing
81 // its pointer. Once decremented, all references are done with positive
82 // offset from the stack/frame pointer, using StackGrowsUp enables
83 // an easier handling.
84 // Using CodeModel::Large enables different CALL behavior.
85 MipsTargetMachine::MipsTargetMachine(const Target &T, StringRef TT,
86 StringRef CPU, StringRef FS,
87 const TargetOptions &Options,
88 Reloc::Model RM, CodeModel::Model CM,
89 CodeGenOpt::Level OL, bool isLittle)
90 : LLVMTargetMachine(T,
91 computeDataLayout(Triple(TT), CPU, Options, isLittle),
92 TT, CPU, FS, Options, RM, CM, OL),
93 isLittle(isLittle), TLOF(make_unique<MipsTargetObjectFile>()),
94 ABI(MipsABIInfo::computeTargetABI(Triple(TT), CPU, Options.MCOptions)),
96 DefaultSubtarget(Triple(TT), CPU, FS, isLittle, *this),
97 NoMips16Subtarget(Triple(TT), CPU,
98 FS.empty() ? "-mips16" : FS.str() + ",-mips16",
100 Mips16Subtarget(Triple(TT), CPU,
101 FS.empty() ? "+mips16" : FS.str() + ",+mips16", isLittle,
103 Subtarget = &DefaultSubtarget;
107 MipsTargetMachine::~MipsTargetMachine() {}
109 void MipsebTargetMachine::anchor() { }
111 MipsebTargetMachine::
112 MipsebTargetMachine(const Target &T, StringRef TT,
113 StringRef CPU, StringRef FS, const TargetOptions &Options,
114 Reloc::Model RM, CodeModel::Model CM,
115 CodeGenOpt::Level OL)
116 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
118 void MipselTargetMachine::anchor() { }
120 MipselTargetMachine::
121 MipselTargetMachine(const Target &T, StringRef TT,
122 StringRef CPU, StringRef FS, const TargetOptions &Options,
123 Reloc::Model RM, CodeModel::Model CM,
124 CodeGenOpt::Level OL)
125 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
127 const MipsSubtarget *
128 MipsTargetMachine::getSubtargetImpl(const Function &F) const {
129 Attribute CPUAttr = F.getFnAttribute("target-cpu");
130 Attribute FSAttr = F.getFnAttribute("target-features");
132 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
133 ? CPUAttr.getValueAsString().str()
135 std::string FS = !FSAttr.hasAttribute(Attribute::None)
136 ? FSAttr.getValueAsString().str()
139 !F.getFnAttribute("mips16").hasAttribute(Attribute::None);
140 bool hasNoMips16Attr =
141 !F.getFnAttribute("nomips16").hasAttribute(Attribute::None);
143 // FIXME: This is related to the code below to reset the target options,
144 // we need to know whether or not the soft float flag is set on the
145 // function, so we can enable it as a subtarget feature.
147 F.hasFnAttribute("use-soft-float") &&
148 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
151 FS += FS.empty() ? "+mips16" : ",+mips16";
152 else if (hasNoMips16Attr)
153 FS += FS.empty() ? "-mips16" : ",-mips16";
155 FS += FS.empty() ? "+soft-float" : ",+soft-float";
157 auto &I = SubtargetMap[CPU + FS];
159 // This needs to be done before we create a new subtarget since any
160 // creation will depend on the TM and the code generation flags on the
161 // function that reside in TargetOptions.
162 resetTargetOptions(F);
163 I = llvm::make_unique<MipsSubtarget>(Triple(TargetTriple), CPU, FS,
169 void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
170 DEBUG(dbgs() << "resetSubtarget\n");
172 Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction()));
173 MF->setSubtarget(Subtarget);
178 /// Mips Code Generator Pass Configuration Options.
179 class MipsPassConfig : public TargetPassConfig {
181 MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
182 : TargetPassConfig(TM, PM) {
183 // The current implementation of long branch pass requires a scratch
184 // register ($at) to be available before branch instructions. Tail merging
185 // can break this requirement, so disable it when long branch pass is
187 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
190 MipsTargetMachine &getMipsTargetMachine() const {
191 return getTM<MipsTargetMachine>();
194 const MipsSubtarget &getMipsSubtarget() const {
195 return *getMipsTargetMachine().getSubtargetImpl();
198 void addIRPasses() override;
199 bool addInstSelector() override;
200 void addMachineSSAOptimization() override;
201 void addPreEmitPass() override;
203 void addPreRegAlloc() override;
208 TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
209 return new MipsPassConfig(this, PM);
212 void MipsPassConfig::addIRPasses() {
213 TargetPassConfig::addIRPasses();
214 addPass(createAtomicExpandPass(&getMipsTargetMachine()));
215 if (getMipsSubtarget().os16())
216 addPass(createMipsOs16Pass(getMipsTargetMachine()));
217 if (getMipsSubtarget().inMips16HardFloat())
218 addPass(createMips16HardFloatPass(getMipsTargetMachine()));
220 // Install an instruction selector pass using
221 // the ISelDag to gen Mips code.
222 bool MipsPassConfig::addInstSelector() {
223 addPass(createMipsModuleISelDagPass(getMipsTargetMachine()));
224 addPass(createMips16ISelDag(getMipsTargetMachine()));
225 addPass(createMipsSEISelDag(getMipsTargetMachine()));
229 void MipsPassConfig::addMachineSSAOptimization() {
230 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
231 TargetPassConfig::addMachineSSAOptimization();
234 void MipsPassConfig::addPreRegAlloc() {
235 if (getOptLevel() == CodeGenOpt::None)
236 addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
239 TargetIRAnalysis MipsTargetMachine::getTargetIRAnalysis() {
240 return TargetIRAnalysis([this](Function &F) {
241 if (Subtarget->allowMixed16_32()) {
242 DEBUG(errs() << "No Target Transform Info Pass Added\n");
243 // FIXME: This is no longer necessary as the TTI returned is per-function.
244 return TargetTransformInfo(getDataLayout());
247 DEBUG(errs() << "Target Transform Info Pass Added\n");
248 return TargetTransformInfo(BasicTTIImpl(this, F));
252 // Implemented by targets that want to run passes immediately before
253 // machine code is emitted. return true if -print-machineinstrs should
254 // print out the code after the passes.
255 void MipsPassConfig::addPreEmitPass() {
256 MipsTargetMachine &TM = getMipsTargetMachine();
257 addPass(createMipsDelaySlotFillerPass(TM));
258 addPass(createMipsLongBranchPass(TM));
259 addPass(createMipsConstantIslandPass(TM));