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 "Mips16ISelDAGToDAG.h"
18#include "MipsMachineFunction.h"
19#include "MipsSEISelDAGToDAG.h"
20#include "MipsSubtarget.h"
24#include "llvm/ADT/StringRef.h"
33#include "llvm/CodeGen/Passes.h"
35#include "llvm/IR/Attributes.h"
36#include "llvm/IR/Function.h"
42#include "llvm/Support/Debug.h"
45#include <optional>
46#include <string>
47
48using namespace llvm;
49
50#define DEBUG_TYPE "mips"
51
71
72static std::unique_ptr<TargetLoweringObjectFile>
73createTLOF(const Triple &TT, bool UseSmallSection) {
74 if (TT.isOSBinFormatCOFF())
75 return std::make_unique<TargetLoweringObjectFileCOFF>();
76 return std::make_unique<MipsTargetObjectFile>(UseSmallSection);
77}
78
79static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
80 return RM.value_or(Reloc::Static);
81}
82
83// On function prologue, the stack is created by decrementing
84// its pointer. Once decremented, all references are done with positive
85// offset from the stack/frame pointer, using StackGrowsUp enables
86// an easier handling.
87// Using CodeModel::Large enables different CALL behavior.
89 StringRef CPU, StringRef FS,
91 std::optional<Reloc::Model> RM,
92 std::optional<CodeModel::Model> CM,
93 CodeGenOptLevel OL, bool JIT,
94 bool isLittle)
95 : CodeGenTargetMachineImpl(T, TT, CPU, FS, Options,
97 getEffectiveCodeModel(CM, CodeModel::Small), OL),
98 CLOpts(cl::LibraryOptionsContext::get<MipsOptions>(
99 Options.MCOptions.LibraryOptions)),
100 MCCLOpts(MIPS_MC::getCLOpts(Options.MCOptions)), isLittle(isLittle),
101 IsJIT(JIT), DefaultSubtarget(TT, CPU, FS, Options.MCOptions.getABIName(),
102 isLittle, *this, std::nullopt),
103 TLOF(createTLOF(TT, DefaultSubtarget.useSmallSection())) {
104 initAsmInfo();
105
106 // Mips supports the debug entry values.
108}
109
111
112void MipsebTargetMachine::anchor() {}
113
115 StringRef CPU, StringRef FS,
116 const TargetOptions &Options,
117 std::optional<Reloc::Model> RM,
118 std::optional<CodeModel::Model> CM,
119 CodeGenOptLevel OL, bool JIT)
120 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
121
122void MipselTargetMachine::anchor() {}
123
125 StringRef CPU, StringRef FS,
126 const TargetOptions &Options,
127 std::optional<Reloc::Model> RM,
128 std::optional<CodeModel::Model> CM,
129 CodeGenOptLevel OL, bool JIT)
130 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
131
132const MipsSubtarget *
134 Attribute CPUAttr = F.getFnAttribute("target-cpu");
135 Attribute FSAttr = F.getFnAttribute("target-features");
136
137 std::string CPU =
138 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
139 std::string FS =
140 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
141 bool hasMips16Attr = F.getFnAttribute("mips16").isValid();
142 bool hasNoMips16Attr = F.getFnAttribute("nomips16").isValid();
143
144 bool HasMicroMipsAttr = F.getFnAttribute("micromips").isValid();
145 bool HasNoMicroMipsAttr = F.getFnAttribute("nomicromips").isValid();
146
147 // FIXME: This is related to the code below to reset the target options,
148 // we need to know whether or not the soft float flag is set on the
149 // function, so we can enable it as a subtarget feature.
150 bool softFloat = F.getFnAttribute("use-soft-float").getValueAsBool();
151
152 if (hasMips16Attr)
153 FS += FS.empty() ? "+mips16" : ",+mips16";
154 else if (hasNoMips16Attr)
155 FS += FS.empty() ? "-mips16" : ",-mips16";
156 if (HasMicroMipsAttr)
157 FS += FS.empty() ? "+micromips" : ",+micromips";
158 else if (HasNoMicroMipsAttr)
159 FS += FS.empty() ? "-micromips" : ",-micromips";
160 if (softFloat)
161 FS += FS.empty() ? "+soft-float" : ",+soft-float";
162
163 StringRef ABIName = getTargetABIName(*F.getParent());
164
165 auto &I = SubtargetMap[CPU + FS + ABIName.str()];
166 if (!I) {
167 I = std::make_unique<MipsSubtarget>(
168 TargetTriple, CPU, FS, ABIName, isLittle, *this,
169 MaybeAlign(F.getParent()->getOverrideStackAlignment()));
170 }
171 return I.get();
172}
173
174namespace {
175
176/// Mips Code Generator Pass Configuration Options.
177class MipsPassConfig : public TargetPassConfig {
178public:
179 MipsPassConfig(MipsTargetMachine &TM, PassManagerBase &PM)
180 : TargetPassConfig(TM, PM) {
181 // The current implementation of long branch pass requires a scratch
182 // register ($at) to be available before branch instructions. Tail merging
183 // can break this requirement.
184 EnableTailMerge = false;
185 EnableLoopTermFold = true;
186 }
187
188 MipsTargetMachine &getMipsTargetMachine() const {
190 }
191
192 void addIRPasses() override;
193 bool addInstSelector() override;
194 void addPreEmitPass() override;
195 void addPreRegAlloc() override;
196 bool addIRTranslator() override;
197 void addPreLegalizeMachineIR() override;
198 bool addLegalizeMachineIR() override;
199 void addPreRegBankSelect() override;
200 bool addRegBankSelect() override;
201 bool addGlobalInstructionSelect() override;
202
203 std::unique_ptr<CSEConfigBase> getCSEConfig() const override;
204};
205
206} // end anonymous namespace
207
209 return new MipsPassConfig(*this, PM);
210}
211
212std::unique_ptr<CSEConfigBase> MipsPassConfig::getCSEConfig() const {
213 return getStandardCSEConfigForOpt(TM->getOptLevel());
214}
215
216void MipsPassConfig::addIRPasses() {
219 if (getMipsTargetMachine().getCLOpts().os16)
220 addPass(createMipsOs16Pass());
221 addPass(createMips16HardFloatPass());
222}
223// Install an instruction selector pass using
224// the ISelDag to gen Mips code.
225bool MipsPassConfig::addInstSelector() {
226 addPass(createMips16ISelDag(getMipsTargetMachine(), getOptLevel()));
227 addPass(createMipsSEISelDag(getMipsTargetMachine(), getOptLevel()));
228 return false;
229}
230
231void MipsPassConfig::addPreRegAlloc() {
233}
234
237 LLVM_DEBUG(errs() << "Target Transform Info Pass Added\n");
238 return TargetTransformInfo(std::make_unique<MipsTTIImpl>(this, F));
239}
240
246
247// Implemented by targets that want to run passes immediately before
248// machine code is emitted.
249void MipsPassConfig::addPreEmitPass() {
250 // Expand pseudo instructions that are sensitive to register allocation.
252
253 // The microMIPS size reduction pass performs instruction reselection for
254 // instructions which can be remapped to a 16 bit instruction.
256
257 // This pass inserts a nop instruction between two back-to-back multiplication
258 // instructions when the "mfix4300" flag is passed.
259 if (getMipsTargetMachine().getCLOpts().mfix4300)
260 addPass(createMipsMulMulBugPass());
261
262 // The delay slot filler pass can potientially create forbidden slot hazards
263 // for MIPSR6 and therefore it should go before MipsBranchExpansion pass.
265
266 // This pass expands branches and takes care about the forbidden slot hazards.
267 // Expanding branches may potentially create forbidden slot hazards for
268 // MIPSR6, and fixing such hazard may potentially break a branch by extending
269 // its offset out of range. That's why this pass combine these two tasks, and
270 // runs them alternately until one of them finishes without any changes. Only
271 // then we can be sure that all branches are expanded properly and no hazards
272 // exists.
273 // Any new pass should go before this pass.
274 addPass(createMipsBranchExpansion());
275
277}
278
279bool MipsPassConfig::addIRTranslator() {
280 addPass(new IRTranslatorLegacy(getOptLevel()));
281 return false;
282}
283
284void MipsPassConfig::addPreLegalizeMachineIR() {
286}
287
288bool MipsPassConfig::addLegalizeMachineIR() {
289 addPass(new LegalizerLegacy());
290 return false;
291}
292
293void MipsPassConfig::addPreRegBankSelect() {
294 bool IsOptNone = getOptLevel() == CodeGenOptLevel::None;
295 addPass(createMipsPostLegalizeCombiner(IsOptNone));
296}
297
298bool MipsPassConfig::addRegBankSelect() {
299 addPass(new RegBankSelectLegacy());
300 return false;
301}
302
303bool MipsPassConfig::addGlobalInstructionSelect() {
304 addPass(new InstructionSelectLegacy(getOptLevel()));
305 return false;
306}
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:857
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:215
#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()
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:106
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:266
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)
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
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
void setSupportsDebugEntryValues(bool Enable)
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with.
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::Global with cl::ParseCommandLineOptions.
This namespace contains all of the command line option processing machinery.
Definition LLVMContext.h:35
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createMipsPreLegalizeCombiner()
Target & getTheMips64Target()
FunctionPass * createMipsConstantIslandPass()
Returns a pass that converts branches to long branches.
void initializeMipsPreLegalizerCombinerPass(PassRegistry &)
void initializeMipsBranchExpansionPass(PassRegistry &)
void initializeMipsDelaySlotFillerPass(PassRegistry &)
void initializeMipsAsmPrinterPass(PassRegistry &)
void initializeMipsMulMulBugFixPass(PassRegistry &)
FunctionPass * createMipsOptimizePICCallPass()
Return an OptimizeCall object.
void initializeMipsDAGToDAGISelLegacyPass(PassRegistry &)
LLVM_ABI std::unique_ptr< CSEConfigBase > getStandardCSEConfigForOpt(CodeGenOptLevel Level)
Definition CSEInfo.cpp:85
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.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void initializeMipsPostLegalizerCombinerPass(PassRegistry &)
FunctionPass * createMicroMipsSizeReducePass()
Returns an instance of the MicroMips size reduction pass.
FunctionPass * createMipsSEISelDag(MipsTargetMachine &TM, CodeGenOptLevel OptLevel)
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:227
FunctionPass * createMipsBranchExpansion()
Target & getTheMips64elTarget()
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
FunctionPass * createMipsMulMulBugPass()
void initializeMicroMipsSizeReducePass(PassRegistry &)
LLVM_ABI void initializeGlobalISel(PassRegistry &)
Initialize all passes linked into the GlobalISel library.
Target & getTheMipselTarget()
FunctionPass * createMips16ISelDag(MipsTargetMachine &TM, CodeGenOptLevel OptLevel)
FunctionPass * createMipsPostLegalizeCombiner(bool IsOptNone)
ModulePass * createMips16HardFloatPass()
FunctionPass * createMipsExpandPseudoPass()
createMipsExpandPseudoPass - returns an instance of the pseudo instruction expansion pass.
FunctionPass * createMipsDelaySlotFillerPass()
createMipsDelaySlotFillerPass - Returns a pass that fills in delay slots in Mips MachineFunctions
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()
ModulePass * createMipsOs16Pass()
Definition MipsOs16.cpp:157
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
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,...