1 //===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Implements the info about Mips target spec.
11 //===----------------------------------------------------------------------===//
13 #include "MipsTargetMachine.h"
14 #include "MCTargetDesc/MipsABIInfo.h"
15 #include "MCTargetDesc/MipsMCTargetDesc.h"
17 #include "Mips16ISelDAGToDAG.h"
18 #include "MipsSEISelDAGToDAG.h"
19 #include "MipsSubtarget.h"
20 #include "MipsTargetObjectFile.h"
21 #include "TargetInfo/MipsTargetInfo.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Analysis/TargetTransformInfo.h"
26 #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
27 #include "llvm/CodeGen/GlobalISel/Legalizer.h"
28 #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
29 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
30 #include "llvm/CodeGen/BasicTTIImpl.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/Passes.h"
33 #include "llvm/CodeGen/TargetPassConfig.h"
34 #include "llvm/IR/Attributes.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/Support/CodeGen.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/TargetRegistry.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Target/TargetOptions.h"
45 #define DEBUG_TYPE "mips"
47 extern "C" void LLVMInitializeMipsTarget() {
48 // Register the target.
49 RegisterTargetMachine
<MipsebTargetMachine
> X(getTheMipsTarget());
50 RegisterTargetMachine
<MipselTargetMachine
> Y(getTheMipselTarget());
51 RegisterTargetMachine
<MipsebTargetMachine
> A(getTheMips64Target());
52 RegisterTargetMachine
<MipselTargetMachine
> B(getTheMips64elTarget());
54 PassRegistry
*PR
= PassRegistry::getPassRegistry();
55 initializeGlobalISel(*PR
);
56 initializeMipsDelaySlotFillerPass(*PR
);
57 initializeMipsBranchExpansionPass(*PR
);
58 initializeMicroMipsSizeReducePass(*PR
);
59 initializeMipsPreLegalizerCombinerPass(*PR
);
62 static std::string
computeDataLayout(const Triple
&TT
, StringRef CPU
,
63 const TargetOptions
&Options
,
66 MipsABIInfo ABI
= MipsABIInfo::computeTargetABI(TT
, CPU
, Options
.MCOptions
);
68 // There are both little and big endian mips.
79 // Pointers are 32 bit on some ABIs.
83 // 8 and 16 bit integers only need to have natural alignment, but try to
84 // align them to 32 bits. 64 bit integers have natural alignment.
85 Ret
+= "-i8:8:32-i16:16:32-i64:64";
87 // 32 bit registers are always available and the stack is at least 64 bit
88 // aligned. On N64 64 bit registers are also available and the stack is
90 if (ABI
.IsN64() || ABI
.IsN32())
91 Ret
+= "-n32:64-S128";
98 static Reloc::Model
getEffectiveRelocModel(bool JIT
,
99 Optional
<Reloc::Model
> RM
) {
100 if (!RM
.hasValue() || JIT
)
101 return Reloc::Static
;
105 // On function prologue, the stack is created by decrementing
106 // its pointer. Once decremented, all references are done with positive
107 // offset from the stack/frame pointer, using StackGrowsUp enables
108 // an easier handling.
109 // Using CodeModel::Large enables different CALL behavior.
110 MipsTargetMachine::MipsTargetMachine(const Target
&T
, const Triple
&TT
,
111 StringRef CPU
, StringRef FS
,
112 const TargetOptions
&Options
,
113 Optional
<Reloc::Model
> RM
,
114 Optional
<CodeModel::Model
> CM
,
115 CodeGenOpt::Level OL
, bool JIT
,
117 : LLVMTargetMachine(T
, computeDataLayout(TT
, CPU
, Options
, isLittle
), TT
,
118 CPU
, FS
, Options
, getEffectiveRelocModel(JIT
, RM
),
119 getEffectiveCodeModel(CM
, CodeModel::Small
), OL
),
120 isLittle(isLittle
), TLOF(std::make_unique
<MipsTargetObjectFile
>()),
121 ABI(MipsABIInfo::computeTargetABI(TT
, CPU
, Options
.MCOptions
)),
122 Subtarget(nullptr), DefaultSubtarget(TT
, CPU
, FS
, isLittle
, *this,
123 Options
.StackAlignmentOverride
),
124 NoMips16Subtarget(TT
, CPU
, FS
.empty() ? "-mips16" : FS
.str() + ",-mips16",
125 isLittle
, *this, Options
.StackAlignmentOverride
),
126 Mips16Subtarget(TT
, CPU
, FS
.empty() ? "+mips16" : FS
.str() + ",+mips16",
127 isLittle
, *this, Options
.StackAlignmentOverride
) {
128 Subtarget
= &DefaultSubtarget
;
132 MipsTargetMachine::~MipsTargetMachine() = default;
134 void MipsebTargetMachine::anchor() {}
136 MipsebTargetMachine::MipsebTargetMachine(const Target
&T
, const Triple
&TT
,
137 StringRef CPU
, StringRef FS
,
138 const TargetOptions
&Options
,
139 Optional
<Reloc::Model
> RM
,
140 Optional
<CodeModel::Model
> CM
,
141 CodeGenOpt::Level OL
, bool JIT
)
142 : MipsTargetMachine(T
, TT
, CPU
, FS
, Options
, RM
, CM
, OL
, JIT
, false) {}
144 void MipselTargetMachine::anchor() {}
146 MipselTargetMachine::MipselTargetMachine(const Target
&T
, const Triple
&TT
,
147 StringRef CPU
, StringRef FS
,
148 const TargetOptions
&Options
,
149 Optional
<Reloc::Model
> RM
,
150 Optional
<CodeModel::Model
> CM
,
151 CodeGenOpt::Level OL
, bool JIT
)
152 : MipsTargetMachine(T
, TT
, CPU
, FS
, Options
, RM
, CM
, OL
, JIT
, true) {}
154 const MipsSubtarget
*
155 MipsTargetMachine::getSubtargetImpl(const Function
&F
) const {
156 Attribute CPUAttr
= F
.getFnAttribute("target-cpu");
157 Attribute FSAttr
= F
.getFnAttribute("target-features");
159 std::string CPU
= !CPUAttr
.hasAttribute(Attribute::None
)
160 ? CPUAttr
.getValueAsString().str()
162 std::string FS
= !FSAttr
.hasAttribute(Attribute::None
)
163 ? FSAttr
.getValueAsString().str()
166 !F
.getFnAttribute("mips16").hasAttribute(Attribute::None
);
167 bool hasNoMips16Attr
=
168 !F
.getFnAttribute("nomips16").hasAttribute(Attribute::None
);
170 bool HasMicroMipsAttr
=
171 !F
.getFnAttribute("micromips").hasAttribute(Attribute::None
);
172 bool HasNoMicroMipsAttr
=
173 !F
.getFnAttribute("nomicromips").hasAttribute(Attribute::None
);
175 // FIXME: This is related to the code below to reset the target options,
176 // we need to know whether or not the soft float flag is set on the
177 // function, so we can enable it as a subtarget feature.
179 F
.hasFnAttribute("use-soft-float") &&
180 F
.getFnAttribute("use-soft-float").getValueAsString() == "true";
183 FS
+= FS
.empty() ? "+mips16" : ",+mips16";
184 else if (hasNoMips16Attr
)
185 FS
+= FS
.empty() ? "-mips16" : ",-mips16";
186 if (HasMicroMipsAttr
)
187 FS
+= FS
.empty() ? "+micromips" : ",+micromips";
188 else if (HasNoMicroMipsAttr
)
189 FS
+= FS
.empty() ? "-micromips" : ",-micromips";
191 FS
+= FS
.empty() ? "+soft-float" : ",+soft-float";
193 auto &I
= SubtargetMap
[CPU
+ FS
];
195 // This needs to be done before we create a new subtarget since any
196 // creation will depend on the TM and the code generation flags on the
197 // function that reside in TargetOptions.
198 resetTargetOptions(F
);
199 I
= std::make_unique
<MipsSubtarget
>(TargetTriple
, CPU
, FS
, isLittle
, *this,
200 Options
.StackAlignmentOverride
);
205 void MipsTargetMachine::resetSubtarget(MachineFunction
*MF
) {
206 LLVM_DEBUG(dbgs() << "resetSubtarget\n");
208 Subtarget
= &MF
->getSubtarget
<MipsSubtarget
>();
213 /// Mips Code Generator Pass Configuration Options.
214 class MipsPassConfig
: public TargetPassConfig
{
216 MipsPassConfig(MipsTargetMachine
&TM
, PassManagerBase
&PM
)
217 : TargetPassConfig(TM
, PM
) {
218 // The current implementation of long branch pass requires a scratch
219 // register ($at) to be available before branch instructions. Tail merging
220 // can break this requirement, so disable it when long branch pass is
222 EnableTailMerge
= !getMipsSubtarget().enableLongBranchPass();
225 MipsTargetMachine
&getMipsTargetMachine() const {
226 return getTM
<MipsTargetMachine
>();
229 const MipsSubtarget
&getMipsSubtarget() const {
230 return *getMipsTargetMachine().getSubtargetImpl();
233 void addIRPasses() override
;
234 bool addInstSelector() override
;
235 void addPreEmitPass() override
;
236 void addPreRegAlloc() override
;
237 bool addIRTranslator() override
;
238 void addPreLegalizeMachineIR() override
;
239 bool addLegalizeMachineIR() override
;
240 bool addRegBankSelect() override
;
241 bool addGlobalInstructionSelect() override
;
243 std::unique_ptr
<CSEConfigBase
> getCSEConfig() const override
;
246 } // end anonymous namespace
248 TargetPassConfig
*MipsTargetMachine::createPassConfig(PassManagerBase
&PM
) {
249 return new MipsPassConfig(*this, PM
);
252 std::unique_ptr
<CSEConfigBase
> MipsPassConfig::getCSEConfig() const {
253 return getStandardCSEConfigForOpt(TM
->getOptLevel());
256 void MipsPassConfig::addIRPasses() {
257 TargetPassConfig::addIRPasses();
258 addPass(createAtomicExpandPass());
259 if (getMipsSubtarget().os16())
260 addPass(createMipsOs16Pass());
261 if (getMipsSubtarget().inMips16HardFloat())
262 addPass(createMips16HardFloatPass());
264 // Install an instruction selector pass using
265 // the ISelDag to gen Mips code.
266 bool MipsPassConfig::addInstSelector() {
267 addPass(createMipsModuleISelDagPass());
268 addPass(createMips16ISelDag(getMipsTargetMachine(), getOptLevel()));
269 addPass(createMipsSEISelDag(getMipsTargetMachine(), getOptLevel()));
273 void MipsPassConfig::addPreRegAlloc() {
274 addPass(createMipsOptimizePICCallPass());
278 MipsTargetMachine::getTargetTransformInfo(const Function
&F
) {
279 if (Subtarget
->allowMixed16_32()) {
280 LLVM_DEBUG(errs() << "No Target Transform Info Pass Added\n");
281 // FIXME: This is no longer necessary as the TTI returned is per-function.
282 return TargetTransformInfo(F
.getParent()->getDataLayout());
285 LLVM_DEBUG(errs() << "Target Transform Info Pass Added\n");
286 return TargetTransformInfo(BasicTTIImpl(this, F
));
289 // Implemented by targets that want to run passes immediately before
290 // machine code is emitted. return true if -print-machineinstrs should
291 // print out the code after the passes.
292 void MipsPassConfig::addPreEmitPass() {
293 // Expand pseudo instructions that are sensitive to register allocation.
294 addPass(createMipsExpandPseudoPass());
296 // The microMIPS size reduction pass performs instruction reselection for
297 // instructions which can be remapped to a 16 bit instruction.
298 addPass(createMicroMipsSizeReducePass());
300 // The delay slot filler pass can potientially create forbidden slot hazards
301 // for MIPSR6 and therefore it should go before MipsBranchExpansion pass.
302 addPass(createMipsDelaySlotFillerPass());
304 // This pass expands branches and takes care about the forbidden slot hazards.
305 // Expanding branches may potentially create forbidden slot hazards for
306 // MIPSR6, and fixing such hazard may potentially break a branch by extending
307 // its offset out of range. That's why this pass combine these two tasks, and
308 // runs them alternately until one of them finishes without any changes. Only
309 // then we can be sure that all branches are expanded properly and no hazards
311 // Any new pass should go before this pass.
312 addPass(createMipsBranchExpansion());
314 addPass(createMipsConstantIslandPass());
317 bool MipsPassConfig::addIRTranslator() {
318 addPass(new IRTranslator());
322 void MipsPassConfig::addPreLegalizeMachineIR() {
323 addPass(createMipsPreLegalizeCombiner());
326 bool MipsPassConfig::addLegalizeMachineIR() {
327 addPass(new Legalizer());
331 bool MipsPassConfig::addRegBankSelect() {
332 addPass(new RegBankSelect());
336 bool MipsPassConfig::addGlobalInstructionSelect() {
337 addPass(new InstructionSelect());