LLVM 24.0.0git
MipsTargetMachine.cpp
Go to the documentation of this file.
1//===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Implements the info about Mips target spec.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MipsTargetMachine.h"
16#include "Mips.h"
17#include "MipsMachineFunction.h"
18#include "MipsSubtarget.h"
22#include "llvm/ADT/StringRef.h"
31#include "llvm/CodeGen/Passes.h"
33#include "llvm/IR/Attributes.h"
34#include "llvm/IR/Function.h"
40#include "llvm/Support/Debug.h"
43#include <optional>
44#include <string>
45
46using namespace llvm;
47
48#define DEBUG_TYPE "mips"
49
70
71static std::unique_ptr<TargetLoweringObjectFile>
72createTLOF(const Triple &TT, bool UseSmallSection) {
73 if (TT.isOSBinFormatCOFF())
74 return std::make_unique<TargetLoweringObjectFileCOFF>();
75 return std::make_unique<MipsTargetObjectFile>(UseSmallSection);
76}
77
78static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
79 return RM.value_or(Reloc::Static);
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.
88 StringRef CPU, StringRef FS,
90 std::optional<Reloc::Model> RM,
91 std::optional<CodeModel::Model> CM,
92 CodeGenOptLevel OL, bool JIT,
93 bool isLittle)
94 : CodeGenTargetMachineImpl(T, TT, CPU, FS, Options,
96 getEffectiveCodeModel(CM, CodeModel::Small), OL),
97 CLOpts(cl::LibraryOptionsContext::get<MipsOptions>(
98 Options.MCOptions.LibraryOptions)),
99 MCCLOpts(MIPS_MC::getCLOpts(Options.MCOptions)), isLittle(isLittle),
100 IsJIT(JIT), DefaultSubtarget(TT, CPU, FS, Options.MCOptions.getABIName(),
101 isLittle, *this, std::nullopt),
102 TLOF(createTLOF(TT, DefaultSubtarget.useSmallSection())) {
103 initAsmInfo();
104
105 // Mips supports the debug entry values.
107}
108
110
111void MipsebTargetMachine::anchor() {}
112
114 StringRef CPU, StringRef FS,
115 const TargetOptions &Options,
116 std::optional<Reloc::Model> RM,
117 std::optional<CodeModel::Model> CM,
118 CodeGenOptLevel OL, bool JIT)
119 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
120
121void MipselTargetMachine::anchor() {}
122
124 StringRef CPU, StringRef FS,
125 const TargetOptions &Options,
126 std::optional<Reloc::Model> RM,
127 std::optional<CodeModel::Model> CM,
128 CodeGenOptLevel OL, bool JIT)
129 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
130
131void NanoMipsTargetMachine::anchor() {}
132
134 StringRef CPU, StringRef FS,
135 const TargetOptions &Options,
136 std::optional<Reloc::Model> RM,
137 std::optional<CodeModel::Model> CM,
138 CodeGenOptLevel OL, bool JIT)
139 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
140
141const MipsSubtarget *
143 AttributeSet FnAttrs = F.getAttributes().getFnAttrs();
144 const Module *M = F.getParent();
145 if (const MipsSubtarget *ST = LastSubtarget.lookup(FnAttrs, M))
146 return ST;
147
148 SubtargetAttrs Attrs;
149 Attrs.CPU = FnAttrs.getAttribute("target-cpu");
150 Attrs.FS = FnAttrs.getAttribute("target-features");
151 Attrs.Mips16 = FnAttrs.getAttribute("mips16");
152 Attrs.NoMips16 = FnAttrs.getAttribute("nomips16");
153 Attrs.MicroMips = FnAttrs.getAttribute("micromips");
154 Attrs.NoMicroMips = FnAttrs.getAttribute("nomicromips");
155 Attrs.SoftFloat = FnAttrs.getAttribute("use-soft-float");
156 Attrs.ABIName = getTargetABIName(*M);
157
158 if (const MipsSubtarget *ST = LastSubtarget.lookup(FnAttrs, M, Attrs))
159 return ST;
160
161 std::string CPU =
162 Attrs.CPU.isValid() ? Attrs.CPU.getValueAsString().str() : TargetCPU;
163 std::string FS =
164 Attrs.FS.isValid() ? Attrs.FS.getValueAsString().str() : TargetFS;
165 bool hasMips16Attr = Attrs.Mips16.isValid();
166 bool hasNoMips16Attr = Attrs.NoMips16.isValid();
167
168 bool HasMicroMipsAttr = Attrs.MicroMips.isValid();
169 bool HasNoMicroMipsAttr = Attrs.NoMicroMips.isValid();
170
171 // FIXME: This is related to the code below to reset the target options,
172 // we need to know whether or not the soft float flag is set on the
173 // function, so we can enable it as a subtarget feature.
174 bool softFloat = Attrs.SoftFloat.getValueAsBool();
175 if (getTargetTriple().isNanoMips())
176 softFloat = true;
177
178 if (hasMips16Attr)
179 FS += FS.empty() ? "+mips16" : ",+mips16";
180 else if (hasNoMips16Attr)
181 FS += FS.empty() ? "-mips16" : ",-mips16";
182 if (HasMicroMipsAttr)
183 FS += FS.empty() ? "+micromips" : ",+micromips";
184 else if (HasNoMicroMipsAttr)
185 FS += FS.empty() ? "-micromips" : ",-micromips";
186 if (softFloat)
187 FS += FS.empty() ? "+soft-float" : ",+soft-float";
188
189 auto &I = SubtargetMap[CPU + FS + Attrs.ABIName.str()];
190 if (!I) {
191 I = std::make_unique<MipsSubtarget>(
192 TargetTriple, CPU, FS, Attrs.ABIName, isLittle, *this,
193 MaybeAlign(F.getParent()->getOverrideStackAlignment()));
194 }
195 LastSubtarget.set(FnAttrs, M, Attrs, I.get());
196 return I.get();
197}
198
199namespace {
200
201/// Mips Code Generator Pass Configuration Options.
202class MipsPassConfig : public TargetPassConfig {
203public:
204 MipsPassConfig(MipsTargetMachine &TM, PassManagerBase &PM)
205 : TargetPassConfig(TM, PM) {
206 // The current implementation of long branch pass requires a scratch
207 // register ($at) to be available before branch instructions. Tail merging
208 // can break this requirement.
209 EnableTailMerge = false;
210 EnableLoopTermFold = true;
211 }
212
213 MipsTargetMachine &getMipsTargetMachine() const {
215 }
216
217 void addIRPasses() override;
218 bool addInstSelector() override;
219 void addPreEmitPass() override;
220 void addPreRegAlloc() override;
221 bool addIRTranslator() override;
222 void addPreLegalizeMachineIR() override;
223 bool addLegalizeMachineIR() override;
224 void addPreRegBankSelect() override;
225 bool addRegBankSelect() override;
226 bool addGlobalInstructionSelect() override;
227
228 std::unique_ptr<CSEConfigBase> getCSEConfig() const override;
229};
230
231} // end anonymous namespace
232
234 return new MipsPassConfig(*this, PM);
235}
236
237std::unique_ptr<CSEConfigBase> MipsPassConfig::getCSEConfig() const {
238 return getStandardCSEConfigForOpt(TM->getOptLevel());
239}
240
241void MipsPassConfig::addIRPasses() {
244 if (getMipsTargetMachine().getCLOpts().os16)
245 addPass(createMipsOs16LegacyPass());
247}
248// Install an instruction selector pass using
249// the ISelDag to gen Mips code.
250bool MipsPassConfig::addInstSelector() {
251 addPass(createMips16ISelDAGLegacyPass(getMipsTargetMachine(), getOptLevel()));
252 addPass(createMipsSEISelDAGLegacyPass(getMipsTargetMachine(), getOptLevel()));
253 return false;
254}
255
256void MipsPassConfig::addPreRegAlloc() {
258}
259
262 LLVM_DEBUG(errs() << "Target Transform Info Pass Added\n");
263 return TargetTransformInfo(std::make_unique<MipsTTIImpl>(this, F));
264}
265
271
272// Implemented by targets that want to run passes immediately before
273// machine code is emitted.
274void MipsPassConfig::addPreEmitPass() {
275 // Expand pseudo instructions that are sensitive to register allocation.
277
278 // The microMIPS size reduction pass performs instruction reselection for
279 // instructions which can be remapped to a 16 bit instruction.
281
282 // This pass inserts a nop instruction between two back-to-back multiplication
283 // instructions when the "mfix4300" flag is passed.
284 if (getMipsTargetMachine().getCLOpts().mfix4300)
286
287 // The delay slot filler pass can potientially create forbidden slot hazards
288 // for MIPSR6 and therefore it should go before MipsBranchExpansion pass.
290
291 // This pass expands branches and takes care about the forbidden slot hazards.
292 // Expanding branches may potentially create forbidden slot hazards for
293 // MIPSR6, and fixing such hazard may potentially break a branch by extending
294 // its offset out of range. That's why this pass combine these two tasks, and
295 // runs them alternately until one of them finishes without any changes. Only
296 // then we can be sure that all branches are expanded properly and no hazards
297 // exists.
298 // Any new pass should go before this pass.
300
302}
303
304bool MipsPassConfig::addIRTranslator() {
305 addPass(new IRTranslatorLegacy(getOptLevel()));
306 return false;
307}
308
309void MipsPassConfig::addPreLegalizeMachineIR() {
311}
312
313bool MipsPassConfig::addLegalizeMachineIR() {
314 addPass(new LegalizerLegacy());
315 return false;
316}
317
318void MipsPassConfig::addPreRegBankSelect() {
319 bool IsOptNone = getOptLevel() == CodeGenOptLevel::None;
321}
322
323bool MipsPassConfig::addRegBankSelect() {
324 addPass(new RegBankSelectLegacy());
325 return false;
326}
327
328bool MipsPassConfig::addGlobalInstructionSelect() {
329 addPass(new InstructionSelectLegacy(getOptLevel()));
330 return false;
331}
static std::unique_ptr< TargetLoweringObjectFile > createTLOF(const Triple &TT)
static Reloc::Model getEffectiveRelocModel()
This file contains the simple types necessary to represent the attributes associated with functions a...
This file provides a helper that implements much of the TTI interface in terms of the target-independ...
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Provides analysis for continuously CSEing during GISel passes.
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
This file declares the IRTranslator pass.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeMipsTarget()
This file describes the interface of the MachineFunctionPass responsible for assigning the generic vi...
const GCNTargetMachine & getTM(const GCNSubtarget *STI)
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Target-Independent Code Generator Pass Configuration Options pass.
This pass exposes codegen information to IR-level passes.
static std::unique_ptr< TargetLoweringObjectFile > createTLOF()
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:448
LLVM_ABI Attribute getAttribute(Attribute::AttrKind Kind) const
Return the attribute object.
CodeGenTargetMachineImpl(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM, CodeGenOptLevel OL)
This pass is responsible for selecting generic machine instructions to target-specific instructions.
TargetTransformInfo getTargetTransformInfo(const Function &F) const override
Get a TargetTransformInfo implementation for the target.
MachineFunctionInfo * createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F, const TargetSubtargetInfo *STI) const override
Create the target's instance of MachineFunctionInfo.
MipsTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM, CodeGenOptLevel OL, bool JIT, bool isLittle)
const MipsOptions & getCLOpts() const
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
~MipsTargetMachine() override
const MipsSubtarget * getSubtargetImpl(const Function &F) const override
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
MipsebTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM, CodeGenOptLevel OL, bool JIT)
MipselTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM, CodeGenOptLevel OL, bool JIT)
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
NanoMipsTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM, CodeGenOptLevel OL, bool JIT)
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This pass implements the reg bank selector pass used in the GlobalISel pipeline.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
void setSupportsDebugEntryValues(bool Enable)
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with.
const Triple & getTargetTriple() const
StringRef getTargetABIName(const Module &M) const
Returns the effective target ABI name: the "target-abi" module flag if present, otherwise the -target...
std::unique_ptr< const MCSubtargetInfo > STI
TargetOptions Options
Target-Independent Code Generator Pass Configuration Options.
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
TargetSubtargetInfo - Generic base class for all target subtargets.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
Registers T with cl::parseCommandLineOptions.
This namespace contains all of the command line option processing machinery.
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createMipsPostLegalizeCombinerLegacyPass(bool IsOptNone)
FunctionPass * createMipsBranchExpansionLegacyPass()
FunctionPass * createMipsSEISelDAGLegacyPass(MipsTargetMachine &TM, CodeGenOptLevel OptLevel)
Target & getTheMips64Target()
void initializeMipsAsmPrinterPass(PassRegistry &)
void initializeMicroMipsSizeReduceLegacyPass(PassRegistry &)
FunctionPass * createMipsPreLegalizeCombinerLegacyPass()
void initializeMipsDelaySlotFillerLegacyPass(PassRegistry &)
void initializeMipsDAGToDAGISelLegacyPass(PassRegistry &)
Target & getTheNanoMipsTarget()
LLVM_ABI std::unique_ptr< CSEConfigBase > getStandardCSEConfigForOpt(CodeGenOptLevel Level)
Definition CSEInfo.cpp:85
FunctionPass * createMipsExpandPseudoLegacyPass()
createMipsExpandPseudoPass - returns an instance of the pseudo instruction expansion pass.
void initializeMipsPostLegalizerCombinerLegacyPass(PassRegistry &)
static Reloc::Model getEffectiveRelocModel(std::optional< Reloc::Model > RM)
CodeModel::Model getEffectiveCodeModel(std::optional< CodeModel::Model > CM, CodeModel::Model Default)
Helper method for getting the code model, returning Default if CM does not have a value.
ModulePass * createMipsOs16LegacyPass()
Definition MipsOs16.cpp:163
FunctionPass * createMips16ISelDAGLegacyPass(MipsTargetMachine &TM, CodeGenOptLevel OptLevel)
void initializeMipsMulMulBugFixLegacyPass(PassRegistry &)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
FunctionPass * createMipsOptimizePICCallLegacyPass()
Return an OptimizeCall object.
FunctionPass * createMipsConstantIslandLegacyPass()
Returns a pass that converts branches to long branches.
void initializeMipsPreLegalizerCombinerLegacyPass(PassRegistry &)
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:280
Target & getTheMips64elTarget()
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI void initializeGlobalISel(PassRegistry &)
Initialize all passes linked into the GlobalISel library.
ModulePass * createMips16HardFloatLegacyPass()
Target & getTheMipselTarget()
FunctionPass * createMipsDelaySlotFillerLegacyPass()
createMipsDelaySlotFillerPass - Returns a pass that fills in delay slots in Mips MachineFunctions
FunctionPass * createMicroMipsSizeReduceLegacyPass()
Returns an instance of the MicroMips size reduction pass.
LLVM_ABI FunctionPass * createAtomicExpandLegacyPass()
AtomicExpandPass - At IR level this pass replace atomic instructions with __atomic_* library calls,...
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
Target & getTheMipsTarget()
void initializeMipsBranchExpansionLegacyPass(PassRegistry &)
FunctionPass * createMipsMulMulBugFixLegacyPass()
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
static FuncInfoTy * create(BumpPtrAllocator &Allocator, const Function &F, const SubtargetTy *STI)
Factory function: default behavior is to call new using the supplied allocator.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:111
RegisterTargetMachine - Helper template for registering a target machine implementation,...