LLVM  5.0.0
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"
28 #include "llvm/CodeGen/Passes.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/Support/CodeGen.h"
33 #include "llvm/Support/Debug.h"
37 #include <string>
38 
39 using namespace llvm;
40 
41 #define DEBUG_TYPE "mips"
42 
43 extern "C" void LLVMInitializeMipsTarget() {
44  // Register the target.
49 }
50 
51 static std::string computeDataLayout(const Triple &TT, StringRef CPU,
52  const TargetOptions &Options,
53  bool isLittle) {
54  std::string Ret;
55  MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions);
56 
57  // There are both little and big endian mips.
58  if (isLittle)
59  Ret += "e";
60  else
61  Ret += "E";
62 
63  if (ABI.IsO32())
64  Ret += "-m:m";
65  else
66  Ret += "-m:e";
67 
68  // Pointers are 32 bit on some ABIs.
69  if (!ABI.IsN64())
70  Ret += "-p:32:32";
71 
72  // 8 and 16 bit integers only need to have natural alignment, but try to
73  // align them to 32 bits. 64 bit integers have natural alignment.
74  Ret += "-i8:8:32-i16:16:32-i64:64";
75 
76  // 32 bit registers are always available and the stack is at least 64 bit
77  // aligned. On N64 64 bit registers are also available and the stack is
78  // 128 bit aligned.
79  if (ABI.IsN64() || ABI.IsN32())
80  Ret += "-n32:64-S128";
81  else
82  Ret += "-n32-S64";
83 
84  return Ret;
85 }
86 
89  if (!RM.hasValue() || CM == CodeModel::JITDefault)
90  return Reloc::Static;
91  return *RM;
92 }
93 
94 // On function prologue, the stack is created by decrementing
95 // its pointer. Once decremented, all references are done with positive
96 // offset from the stack/frame pointer, using StackGrowsUp enables
97 // an easier handling.
98 // Using CodeModel::Large enables different CALL behavior.
100  StringRef CPU, StringRef FS,
101  const TargetOptions &Options,
104  bool isLittle)
105  : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
106  CPU, FS, Options, getEffectiveRelocModel(CM, RM), CM,
107  OL),
108  isLittle(isLittle), TLOF(llvm::make_unique<MipsTargetObjectFile>()),
109  ABI(MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions)),
110  Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this),
111  NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
112  isLittle, *this),
113  Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
114  isLittle, *this) {
115  Subtarget = &DefaultSubtarget;
116  initAsmInfo();
117 }
118 
120 
121 void MipsebTargetMachine::anchor() {}
122 
124  StringRef CPU, StringRef FS,
125  const TargetOptions &Options,
127  CodeModel::Model CM,
129  : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
130 
131 void MipselTargetMachine::anchor() {}
132 
134  StringRef CPU, StringRef FS,
135  const TargetOptions &Options,
137  CodeModel::Model CM,
139  : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
140 
141 const MipsSubtarget *
143  Attribute CPUAttr = F.getFnAttribute("target-cpu");
144  Attribute FSAttr = F.getFnAttribute("target-features");
145 
146  std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
147  ? CPUAttr.getValueAsString().str()
148  : TargetCPU;
149  std::string FS = !FSAttr.hasAttribute(Attribute::None)
150  ? FSAttr.getValueAsString().str()
151  : TargetFS;
152  bool hasMips16Attr =
154  bool hasNoMips16Attr =
156 
157  bool HasMicroMipsAttr =
159  bool HasNoMicroMipsAttr =
160  !F.getFnAttribute("nomicromips").hasAttribute(Attribute::None);
161 
162  // FIXME: This is related to the code below to reset the target options,
163  // we need to know whether or not the soft float flag is set on the
164  // function, so we can enable it as a subtarget feature.
165  bool softFloat =
166  F.hasFnAttribute("use-soft-float") &&
167  F.getFnAttribute("use-soft-float").getValueAsString() == "true";
168 
169  if (hasMips16Attr)
170  FS += FS.empty() ? "+mips16" : ",+mips16";
171  else if (hasNoMips16Attr)
172  FS += FS.empty() ? "-mips16" : ",-mips16";
173  if (HasMicroMipsAttr)
174  FS += FS.empty() ? "+micromips" : ",+micromips";
175  else if (HasNoMicroMipsAttr)
176  FS += FS.empty() ? "-micromips" : ",-micromips";
177  if (softFloat)
178  FS += FS.empty() ? "+soft-float" : ",+soft-float";
179 
180  auto &I = SubtargetMap[CPU + FS];
181  if (!I) {
182  // This needs to be done before we create a new subtarget since any
183  // creation will depend on the TM and the code generation flags on the
184  // function that reside in TargetOptions.
186  I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle,
187  *this);
188  }
189  return I.get();
190 }
191 
193  DEBUG(dbgs() << "resetSubtarget\n");
194 
195  Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction()));
196  MF->setSubtarget(Subtarget);
197 }
198 
199 namespace {
200 
201 /// Mips Code Generator Pass Configuration Options.
202 class MipsPassConfig : public TargetPassConfig {
203 public:
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, so disable it when long branch pass is
209  // enabled.
210  EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
211  }
212 
213  MipsTargetMachine &getMipsTargetMachine() const {
214  return getTM<MipsTargetMachine>();
215  }
216 
217  const MipsSubtarget &getMipsSubtarget() const {
218  return *getMipsTargetMachine().getSubtargetImpl();
219  }
220 
221  void addIRPasses() override;
222  bool addInstSelector() override;
223  void addPreEmitPass() override;
224  void addPreRegAlloc() override;
225 };
226 
227 } // end anonymous namespace
228 
230  return new MipsPassConfig(*this, PM);
231 }
232 
233 void MipsPassConfig::addIRPasses() {
235  addPass(createAtomicExpandPass());
236  if (getMipsSubtarget().os16())
237  addPass(createMipsOs16Pass());
238  if (getMipsSubtarget().inMips16HardFloat())
239  addPass(createMips16HardFloatPass());
240 }
241 // Install an instruction selector pass using
242 // the ISelDag to gen Mips code.
243 bool MipsPassConfig::addInstSelector() {
244  addPass(createMipsModuleISelDagPass());
245  addPass(createMips16ISelDag(getMipsTargetMachine(), getOptLevel()));
246  addPass(createMipsSEISelDag(getMipsTargetMachine(), getOptLevel()));
247  return false;
248 }
249 
250 void MipsPassConfig::addPreRegAlloc() {
252 }
253 
255  return TargetIRAnalysis([this](const Function &F) {
256  if (Subtarget->allowMixed16_32()) {
257  DEBUG(errs() << "No Target Transform Info Pass Added\n");
258  // FIXME: This is no longer necessary as the TTI returned is per-function.
260  }
261 
262  DEBUG(errs() << "Target Transform Info Pass Added\n");
263  return TargetTransformInfo(BasicTTIImpl(this, F));
264  });
265 }
266 
267 // Implemented by targets that want to run passes immediately before
268 // machine code is emitted. return true if -print-machineinstrs should
269 // print out the code after the passes.
270 void MipsPassConfig::addPreEmitPass() {
272 
273  // The delay slot filler pass can potientially create forbidden slot (FS)
274  // hazards for MIPSR6 which the hazard schedule pass (HSP) will fix. Any
275  // (new) pass that creates compact branches after the HSP must handle FS
276  // hazards itself or be pipelined before the HSP.
278  addPass(createMipsHazardSchedule());
279  addPass(createMipsLongBranchPass());
280  addPass(createMipsConstantIslandPass());
281 }
static ARMBaseTargetMachine::ARMABI computeTargetABI(const Triple &TT, StringRef CPU, const TargetOptions &Options)
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
MCTargetOptions MCOptions
Machine level options.
bool hasValue() const
Definition: Optional.h:133
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:75
FunctionPass * createMicroMipsSizeReductionPass()
Returns an instance of the MicroMips size reduction pass.
Target & getTheMipselTarget()
Analysis pass providing the TargetTransformInfo.
FunctionPass * createMipsDelaySlotFillerPass()
createMipsDelaySlotFillerPass - Returns a pass that fills in delay slots in Mips MachineFunctions ...
void resetSubtarget(MachineFunction *MF)
Reset the subtarget for the Mips target.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:262
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
bool hasAttribute(AttrKind Val) const
Return true if the attribute is present.
Definition: Attributes.cpp:202
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
This file contains the simple types necessary to represent the attributes associated with functions a...
No attributes have been set.
Definition: Attributes.h:72
Target-Independent Code Generator Pass Configuration Options.
#define F(x, y, z)
Definition: MD5.cpp:55
Target & getTheMips64Target()
MipsebTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, CodeModel::Model CM, CodeGenOpt::Level OL)
FunctionPass * createMipsHazardSchedule()
Returns a pass that clears pipeline hazards.
ModulePass * createMipsOs16Pass()
Definition: MipsOs16.cpp:158
static std::string computeDataLayout(const Triple &TT, StringRef CPU, const TargetOptions &Options, bool isLittle)
Function Alias Analysis false
Target & getTheMips64elTarget()
Concrete BasicTTIImpl that can be used if no further customization is needed.
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:943
static Reloc::Model getEffectiveRelocModel(Optional< Reloc::Model > RM)
FunctionPass * createMipsOptimizePICCallPass()
Return an OptimizeCall object.
~MipsTargetMachine() override
bool IsN32() const
Definition: MipsABIInfo.h:43
#define A
Definition: LargeTest.cpp:12
void setSubtarget(const TargetSubtargetInfo *ST)
bool IsN64() const
Definition: MipsABIInfo.h:44
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This class describes a target machine that is implemented with the LLVM target-independent code gener...
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()
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
#define B
Definition: LargeTest.cpp:24
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
TargetIRAnalysis getTargetIRAnalysis() override
Get a TargetIRAnalysis implementation for the target.
bool IsO32() const
Definition: MipsABIInfo.h:42
MipselTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, CodeModel::Model CM, CodeGenOpt::Level OL)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Target - Wrapper for Target specific information.
std::string TargetCPU
Definition: TargetMachine.h:76
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
Target & getTheMipsTarget()
Basic Alias true
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:361
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:254
#define I(x, y, z)
Definition: MD5.cpp:58
void resetTargetOptions(const Function &F) const
Reset the target options based on the function's attributes.
FunctionPass * createMipsLongBranchPass()
createMipsLongBranchPass - Returns a pass that converts branches to long branches.
FunctionPass * createMipsModuleISelDagPass()
const MipsSubtarget * getSubtargetImpl() const
bool allowMixed16_32() const
std::string TargetFS
Definition: TargetMachine.h:77
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:195
MipsTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, CodeModel::Model CM, CodeGenOpt::Level OL, bool isLittle)
static MipsABIInfo computeTargetABI(const Triple &TT, StringRef CPU, const MCTargetOptions &Options)
Definition: MipsABIInfo.cpp:50
RegisterTargetMachine - Helper template for registering a target machine implementation, for use in the target machine initialization function.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:545
#define DEBUG(X)
Definition: Debug.h:118
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
This pass exposes codegen information to IR-level passes.
FunctionPass * createAtomicExpandPass()
void LLVMInitializeMipsTarget()