LLVM  7.0.0svn
MipsTargetMachine.cpp
Go to the documentation of this file.
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"
17 #include "Mips.h"
18 #include "Mips16ISelDAGToDAG.h"
19 #include "MipsSEISelDAGToDAG.h"
20 #include "MipsSubtarget.h"
21 #include "MipsTargetObjectFile.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/StringRef.h"
32 #include "llvm/CodeGen/Passes.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"
41 #include <string>
42 
43 using namespace llvm;
44 
45 #define DEBUG_TYPE "mips"
46 
47 extern "C" void LLVMInitializeMipsTarget() {
48  // Register the target.
53 
59 }
60 
61 static std::string computeDataLayout(const Triple &TT, StringRef CPU,
62  const TargetOptions &Options,
63  bool isLittle) {
64  std::string Ret;
65  MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions);
66 
67  // There are both little and big endian mips.
68  if (isLittle)
69  Ret += "e";
70  else
71  Ret += "E";
72 
73  if (ABI.IsO32())
74  Ret += "-m:m";
75  else
76  Ret += "-m:e";
77 
78  // Pointers are 32 bit on some ABIs.
79  if (!ABI.IsN64())
80  Ret += "-p:32:32";
81 
82  // 8 and 16 bit integers only need to have natural alignment, but try to
83  // align them to 32 bits. 64 bit integers have natural alignment.
84  Ret += "-i8:8:32-i16:16:32-i64:64";
85 
86  // 32 bit registers are always available and the stack is at least 64 bit
87  // aligned. On N64 64 bit registers are also available and the stack is
88  // 128 bit aligned.
89  if (ABI.IsN64() || ABI.IsN32())
90  Ret += "-n32:64-S128";
91  else
92  Ret += "-n32-S64";
93 
94  return Ret;
95 }
96 
99  if (!RM.hasValue() || JIT)
100  return Reloc::Static;
101  return *RM;
102 }
103 
105  if (CM)
106  return *CM;
107  return CodeModel::Small;
108 }
109 
110 // On function prologue, the stack is created by decrementing
111 // its pointer. Once decremented, all references are done with positive
112 // offset from the stack/frame pointer, using StackGrowsUp enables
113 // an easier handling.
114 // Using CodeModel::Large enables different CALL behavior.
116  StringRef CPU, StringRef FS,
117  const TargetOptions &Options,
120  CodeGenOpt::Level OL, bool JIT,
121  bool isLittle)
122  : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
123  CPU, FS, Options, getEffectiveRelocModel(JIT, RM),
124  getEffectiveCodeModel(CM), OL),
125  isLittle(isLittle), TLOF(llvm::make_unique<MipsTargetObjectFile>()),
126  ABI(MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions)),
127  Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this,
128  Options.StackAlignmentOverride),
129  NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
130  isLittle, *this, Options.StackAlignmentOverride),
131  Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
132  isLittle, *this, Options.StackAlignmentOverride) {
133  Subtarget = &DefaultSubtarget;
134  initAsmInfo();
135 }
136 
138 
139 void MipsebTargetMachine::anchor() {}
140 
142  StringRef CPU, StringRef FS,
143  const TargetOptions &Options,
146  CodeGenOpt::Level OL, bool JIT)
147  : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
148 
149 void MipselTargetMachine::anchor() {}
150 
152  StringRef CPU, StringRef FS,
153  const TargetOptions &Options,
156  CodeGenOpt::Level OL, bool JIT)
157  : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
158 
159 const MipsSubtarget *
161  Attribute CPUAttr = F.getFnAttribute("target-cpu");
162  Attribute FSAttr = F.getFnAttribute("target-features");
163 
164  std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
165  ? CPUAttr.getValueAsString().str()
166  : TargetCPU;
167  std::string FS = !FSAttr.hasAttribute(Attribute::None)
168  ? FSAttr.getValueAsString().str()
169  : TargetFS;
170  bool hasMips16Attr =
172  bool hasNoMips16Attr =
174 
175  bool HasMicroMipsAttr =
177  bool HasNoMicroMipsAttr =
178  !F.getFnAttribute("nomicromips").hasAttribute(Attribute::None);
179 
180  // FIXME: This is related to the code below to reset the target options,
181  // we need to know whether or not the soft float flag is set on the
182  // function, so we can enable it as a subtarget feature.
183  bool softFloat =
184  F.hasFnAttribute("use-soft-float") &&
185  F.getFnAttribute("use-soft-float").getValueAsString() == "true";
186 
187  if (hasMips16Attr)
188  FS += FS.empty() ? "+mips16" : ",+mips16";
189  else if (hasNoMips16Attr)
190  FS += FS.empty() ? "-mips16" : ",-mips16";
191  if (HasMicroMipsAttr)
192  FS += FS.empty() ? "+micromips" : ",+micromips";
193  else if (HasNoMicroMipsAttr)
194  FS += FS.empty() ? "-micromips" : ",-micromips";
195  if (softFloat)
196  FS += FS.empty() ? "+soft-float" : ",+soft-float";
197 
198  auto &I = SubtargetMap[CPU + FS];
199  if (!I) {
200  // This needs to be done before we create a new subtarget since any
201  // creation will depend on the TM and the code generation flags on the
202  // function that reside in TargetOptions.
204  I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle, *this,
206  }
207  return I.get();
208 }
209 
211  LLVM_DEBUG(dbgs() << "resetSubtarget\n");
212 
213  Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(MF->getFunction()));
214  MF->setSubtarget(Subtarget);
215 }
216 
217 namespace {
218 
219 /// Mips Code Generator Pass Configuration Options.
220 class MipsPassConfig : public TargetPassConfig {
221 public:
222  MipsPassConfig(MipsTargetMachine &TM, PassManagerBase &PM)
223  : TargetPassConfig(TM, PM) {
224  // The current implementation of long branch pass requires a scratch
225  // register ($at) to be available before branch instructions. Tail merging
226  // can break this requirement, so disable it when long branch pass is
227  // enabled.
228  EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
229  }
230 
231  MipsTargetMachine &getMipsTargetMachine() const {
232  return getTM<MipsTargetMachine>();
233  }
234 
235  const MipsSubtarget &getMipsSubtarget() const {
236  return *getMipsTargetMachine().getSubtargetImpl();
237  }
238 
239  void addIRPasses() override;
240  bool addInstSelector() override;
241  void addPreEmitPass() override;
242  void addPreRegAlloc() override;
243  bool addIRTranslator() override;
244  bool addLegalizeMachineIR() override;
245  bool addRegBankSelect() override;
246  bool addGlobalInstructionSelect() override;
247 };
248 
249 } // end anonymous namespace
250 
252  return new MipsPassConfig(*this, PM);
253 }
254 
255 void MipsPassConfig::addIRPasses() {
257  addPass(createAtomicExpandPass());
258  if (getMipsSubtarget().os16())
259  addPass(createMipsOs16Pass());
260  if (getMipsSubtarget().inMips16HardFloat())
261  addPass(createMips16HardFloatPass());
262 }
263 // Install an instruction selector pass using
264 // the ISelDag to gen Mips code.
265 bool MipsPassConfig::addInstSelector() {
266  addPass(createMipsModuleISelDagPass());
267  addPass(createMips16ISelDag(getMipsTargetMachine(), getOptLevel()));
268  addPass(createMipsSEISelDag(getMipsTargetMachine(), getOptLevel()));
269  return false;
270 }
271 
272 void MipsPassConfig::addPreRegAlloc() {
274 }
275 
278  if (Subtarget->allowMixed16_32()) {
279  LLVM_DEBUG(errs() << "No Target Transform Info Pass Added\n");
280  // FIXME: This is no longer necessary as the TTI returned is per-function.
282  }
283 
284  LLVM_DEBUG(errs() << "Target Transform Info Pass Added\n");
285  return TargetTransformInfo(BasicTTIImpl(this, F));
286 }
287 
288 // Implemented by targets that want to run passes immediately before
289 // machine code is emitted. return true if -print-machineinstrs should
290 // print out the code after the passes.
291 void MipsPassConfig::addPreEmitPass() {
293 
294  // The delay slot filler pass can potientially create forbidden slot hazards
295  // for MIPSR6 and therefore it should go before MipsBranchExpansion pass.
297 
298  // This pass expands branches and takes care about the forbidden slot hazards.
299  // Expanding branches may potentially create forbidden slot hazards for
300  // MIPSR6, and fixing such hazard may potentially break a branch by extending
301  // its offset out of range. That's why this pass combine these two tasks, and
302  // runs them alternately until one of them finishes without any changes. Only
303  // then we can be sure that all branches are expanded properly and no hazards
304  // exists.
305  // Any new pass should go before this pass.
306  addPass(createMipsBranchExpansion());
307 
308  addPass(createMipsConstantIslandPass());
309 }
310 
311 bool MipsPassConfig::addIRTranslator() {
312  addPass(new IRTranslator());
313  return false;
314 }
315 
316 bool MipsPassConfig::addLegalizeMachineIR() {
317  addPass(new Legalizer());
318  return false;
319 }
320 
321 bool MipsPassConfig::addRegBankSelect() {
322  addPass(new RegBankSelect());
323  return false;
324 }
325 
326 bool MipsPassConfig::addGlobalInstructionSelect() {
327  addPass(new InstructionSelect());
328  return false;
329 }
void initializeMipsBranchExpansionPass(PassRegistry &)
MipsebTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT)
static ARMBaseTargetMachine::ARMABI computeTargetABI(const Triple &TT, StringRef CPU, const TargetOptions &Options)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
MCTargetOptions MCOptions
Machine level options.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
FunctionPass * createMipsSEISelDag(MipsTargetMachine &TM, CodeGenOpt::Level OptLevel)
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with...
Definition: TargetMachine.h:78
FunctionPass * createMipsBranchExpansion()
Target & getTheMipselTarget()
FunctionPass * createMipsDelaySlotFillerPass()
createMipsDelaySlotFillerPass - Returns a pass that fills in delay slots in Mips MachineFunctions ...
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:307
void resetSubtarget(MachineFunction *MF)
Reset the subtarget for the Mips target.
F(f)
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&... args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
Definition: STLExtras.h:1056
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
void resetTargetOptions(const Function &F) const
Reset the target options based on the function&#39;s attributes.
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
This file contains the simple types necessary to represent the attributes associated with functions a...
No attributes have been set.
Definition: Attributes.h:72
bool IsN32() const
Definition: MipsABIInfo.h:43
Target-Independent Code Generator Pass Configuration Options.
bool IsN64() const
Definition: MipsABIInfo.h:44
RegisterTargetMachine - Helper template for registering a target machine implementation, for use in the target machine initialization function.
Target & getTheMips64Target()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
static CodeModel::Model getEffectiveCodeModel(Optional< CodeModel::Model > CM)
ModulePass * createMipsOs16Pass()
Definition: MipsOs16.cpp:160
const MipsSubtarget * getSubtargetImpl() const
static std::string computeDataLayout(const Triple &TT, StringRef CPU, const TargetOptions &Options, bool isLittle)
bool hasAttribute(AttrKind Val) const
Return true if the attribute is present.
Definition: Attributes.cpp:202
Target & getTheMips64elTarget()
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
Concrete BasicTTIImpl that can be used if no further customization is needed.
static Reloc::Model getEffectiveRelocModel(Optional< Reloc::Model > RM)
This pass implements the reg bank selector pass used in the GlobalISel pipeline.
Definition: RegBankSelect.h:91
FunctionPass * createMipsOptimizePICCallPass()
Return an OptimizeCall object.
~MipsTargetMachine() override
void setSubtarget(const TargetSubtargetInfo *ST)
This class describes a target machine that is implemented with the LLVM target-independent code gener...
bool IsO32() const
Definition: MipsABIInfo.h:42
FunctionPass * createMipsConstantIslandPass()
Returns a pass that converts branches to long branches.
This file provides a helper that implements much of the TTI interface in terms of the target-independ...
FunctionPass * createMips16ISelDag(MipsTargetMachine &TM, CodeGenOpt::Level OptLevel)
ModulePass * createMips16HardFloatPass()
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
unsigned StackAlignmentOverride
StackAlignmentOverride - Override default stack alignment for target.
MipselTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT)
const Function & getFunction() const
Return the LLVM function that this machine code represents.
This pass is responsible for selecting generic machine instructions to target-specific instructions...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
Target - Wrapper for Target specific information.
std::string TargetCPU
Definition: TargetMachine.h:79
void initializeMicroMipsSizeReducePass(PassRegistry &)
Target & getTheMipsTarget()
FunctionPass * createMicroMipsSizeReducePass()
Returns an instance of the MicroMips size reduction pass.
Basic Alias true
bool hasValue() const
Definition: Optional.h:183
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:195
TargetOptions Options
Definition: TargetMachine.h:98
#define I(x, y, z)
Definition: MD5.cpp:58
FunctionPass * createMipsModuleISelDagPass()
MipsTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT, bool isLittle)
std::string TargetFS
Definition: TargetMachine.h:80
This file declares the IRTranslator pass.
static MipsABIInfo computeTargetABI(const Triple &TT, StringRef CPU, const MCTargetOptions &Options)
Definition: MipsABIInfo.cpp:50
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:565
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:317
TargetTransformInfo getTargetTransformInfo(const Function &F) override
Get a TargetTransformInfo implementation for the target.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:39
This pass exposes codegen information to IR-level passes.
void initializeMipsDelaySlotFillerPass(PassRegistry &)
#define LLVM_DEBUG(X)
Definition: Debug.h:119
FunctionPass * createAtomicExpandPass()
void initializeGlobalISel(PassRegistry &)
Initialize all passes linked into the GlobalISel library.
Definition: GlobalISel.cpp:19
void LLVMInitializeMipsTarget()