LLVM  4.0.0
Target/TargetMachine.h
Go to the documentation of this file.
1 //===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===//
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 // This file defines the TargetMachine and LLVMTargetMachine classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TARGET_TARGETMACHINE_H
15 #define LLVM_TARGET_TARGETMACHINE_H
16 
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Support/CodeGen.h"
23 #include <string>
24 
25 namespace llvm {
26 
27 class GlobalValue;
28 class MachineFunctionInitializer;
29 class Mangler;
30 class MCAsmInfo;
31 class MCContext;
32 class MCInstrInfo;
33 class MCRegisterInfo;
34 class MCSubtargetInfo;
35 class MCSymbol;
36 class raw_pwrite_stream;
37 class Target;
38 class TargetIntrinsicInfo;
39 class TargetIRAnalysis;
40 class TargetLoweringObjectFile;
41 class TargetPassConfig;
42 class TargetSubtargetInfo;
43 
44 // The old pass manager infrastructure is hidden in a legacy namespace now.
45 namespace legacy {
46 class PassManagerBase;
47 }
48 using legacy::PassManagerBase;
49 
50 //===----------------------------------------------------------------------===//
51 ///
52 /// Primary interface to the complete machine description for the target
53 /// machine. All target-specific information should be accessible through this
54 /// interface.
55 ///
57 protected: // Can only create subclasses.
58  TargetMachine(const Target &T, StringRef DataLayoutString,
59  const Triple &TargetTriple, StringRef CPU, StringRef FS,
60  const TargetOptions &Options);
61 
62  /// The Target that this machine was created for.
63  const Target &TheTarget;
64 
65  /// DataLayout for the target: keep ABI type size and alignment.
66  ///
67  /// The DataLayout is created based on the string representation provided
68  /// during construction. It is kept here only to avoid reparsing the string
69  /// but should not really be used during compilation, because it has an
70  /// internal cache that is context specific.
71  const DataLayout DL;
72 
73  /// Triple string, CPU name, and target feature strings the TargetMachine
74  /// instance is created with.
76  std::string TargetCPU;
77  std::string TargetFS;
78 
82 
83  /// Contains target specific asm information.
85 
87  const MCInstrInfo *MII;
89 
90  unsigned RequireStructuredCFG : 1;
91  unsigned O0WantsFastISel : 1;
92 
93 public:
96 
97  TargetMachine(const TargetMachine &) = delete;
98  void operator=(const TargetMachine &) = delete;
99  virtual ~TargetMachine();
100 
101  const Target &getTarget() const { return TheTarget; }
102 
103  const Triple &getTargetTriple() const { return TargetTriple; }
104  StringRef getTargetCPU() const { return TargetCPU; }
106 
107  /// Virtual method implemented by subclasses that returns a reference to that
108  /// target's TargetSubtargetInfo-derived member variable.
109  virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
110  return nullptr;
111  }
113  return nullptr;
114  }
115 
116  /// This method returns a pointer to the specified type of
117  /// TargetSubtargetInfo. In debug builds, it verifies that the object being
118  /// returned is of the correct type.
119  template <typename STC> const STC &getSubtarget(const Function &F) const {
120  return *static_cast<const STC*>(getSubtargetImpl(F));
121  }
122 
123  /// Create a DataLayout.
124  const DataLayout createDataLayout() const { return DL; }
125 
126  /// Test if a DataLayout if compatible with the CodeGen for this target.
127  ///
128  /// The LLVM Module owns a DataLayout that is used for the target independent
129  /// optimizations and code generation. This hook provides a target specific
130  /// check on the validity of this DataLayout.
131  bool isCompatibleDataLayout(const DataLayout &Candidate) const {
132  return DL == Candidate;
133  }
134 
135  /// Get the pointer size for this target.
136  ///
137  /// This is the only time the DataLayout in the TargetMachine is used.
138  unsigned getPointerSize() const { return DL.getPointerSize(); }
139 
140  /// \brief Reset the target options based on the function's attributes.
141  // FIXME: Remove TargetOptions that affect per-function code generation
142  // from TargetMachine.
143  void resetTargetOptions(const Function &F) const;
144 
145  /// Return target specific asm information.
146  const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
147 
148  const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
149  const MCInstrInfo *getMCInstrInfo() const { return MII; }
150  const MCSubtargetInfo *getMCSubtargetInfo() const { return STI; }
151 
152  /// If intrinsic information is available, return it. If not, return null.
153  virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
154  return nullptr;
155  }
156 
159 
160  /// Returns the code generation relocation model. The choices are static, PIC,
161  /// and dynamic-no-pic, and target default.
163 
164  /// Returns the code model. The choices are small, kernel, medium, large, and
165  /// target default.
167 
168  bool isPositionIndependent() const;
169 
170  bool shouldAssumeDSOLocal(const Module &M, const GlobalValue *GV) const;
171 
172  /// Returns the TLS model which should be used for the given global variable.
173  TLSModel::Model getTLSModel(const GlobalValue *GV) const;
174 
175  /// Returns the optimization level: None, Less, Default, or Aggressive.
177 
178  /// \brief Overrides the optimization level.
180 
181  void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
183  void setO0WantsFastISel(bool Enable) { O0WantsFastISel = Enable; }
184 
185  bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
186 
187  bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
188 
189  /// Return true if data objects should be emitted into their own section,
190  /// corresponds to -fdata-sections.
191  bool getDataSections() const {
192  return Options.DataSections;
193  }
194 
195  /// Return true if functions should be emitted into their own section,
196  /// corresponding to -ffunction-sections.
197  bool getFunctionSections() const {
198  return Options.FunctionSections;
199  }
200 
201  /// \brief Get a \c TargetIRAnalysis appropriate for the target.
202  ///
203  /// This is used to construct the new pass manager's target IR analysis pass,
204  /// set up appropriately for this target machine. Even the old pass manager
205  /// uses this to answer queries about the IR.
207 
208  /// Add target-specific function passes that should be run as early as
209  /// possible in the optimization pipeline. Most TargetMachines have no such
210  /// passes.
211  virtual void addEarlyAsPossiblePasses(PassManagerBase &) {}
212 
213  /// These enums are meant to be passed into addPassesToEmitFile to indicate
214  /// what type of file to emit, and returned by it to indicate what type of
215  /// file could actually be made.
219  CGFT_Null // Do not emit any output.
220  };
221 
222  /// Add passes to the specified pass manager to get the specified file
223  /// emitted. Typically this will involve several steps of code generation.
224  /// This method should return true if emission of this file type is not
225  /// supported, or false on success.
226  virtual bool addPassesToEmitFile(
227  PassManagerBase &, raw_pwrite_stream &, CodeGenFileType,
228  bool /*DisableVerify*/ = true, AnalysisID /*StartBefore*/ = nullptr,
229  AnalysisID /*StartAfter*/ = nullptr, AnalysisID /*StopBefore*/ = nullptr,
230  AnalysisID /*StopAfter*/ = nullptr,
231  MachineFunctionInitializer * /*MFInitializer*/ = nullptr) {
232  return true;
233  }
234 
235  /// Add passes to the specified pass manager to get machine code emitted with
236  /// the MCJIT. This method returns true if machine code is not supported. It
237  /// fills the MCContext Ctx pointer which can be used to build custom
238  /// MCStreamer.
239  ///
240  virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&,
242  bool /*DisableVerify*/ = true) {
243  return true;
244  }
245 
246  /// True if subtarget inserts the final scheduling pass on its own.
247  ///
248  /// Branch relaxation, which must happen after block placement, can
249  /// on some targets (e.g. SystemZ) expose additional post-RA
250  /// scheduling opportunities.
251  virtual bool targetSchedulesPostRAScheduling() const { return false; };
252 
254  Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
255  MCSymbol *getSymbol(const GlobalValue *GV) const;
256 
257  /// True if the target uses physical regs at Prolog/Epilog insertion
258  /// time. If true (most machines), all vregs must be allocated before
259  /// PEI. If false (virtual-register machines), then callee-save register
260  /// spilling and scavenging are not needed or used.
261  virtual bool usesPhysRegsForPEI() const { return true; }
262 };
263 
264 /// This class describes a target machine that is implemented with the LLVM
265 /// target-independent code generator.
266 ///
268 protected: // Can only create subclasses.
269  LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
270  const Triple &TargetTriple, StringRef CPU, StringRef FS,
272  CodeGenOpt::Level OL);
273 
274  void initAsmInfo();
275 public:
276  /// \brief Get a TargetIRAnalysis implementation for the target.
277  ///
278  /// This analysis will produce a TTI result which uses the common code
279  /// generator to answer queries about the IR.
281 
282  /// Create a pass configuration object to be used by addPassToEmitX methods
283  /// for generating a pipeline of CodeGen passes.
284  virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
285 
286  /// Add passes to the specified pass manager to get the specified file
287  /// emitted. Typically this will involve several steps of code generation.
288  bool addPassesToEmitFile(
289  PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType,
290  bool DisableVerify = true, AnalysisID StartBefore = nullptr,
291  AnalysisID StartAfter = nullptr, AnalysisID StopBefore = nullptr,
292  AnalysisID StopAfter = nullptr,
293  MachineFunctionInitializer *MFInitializer = nullptr) override;
294 
295  /// Add passes to the specified pass manager to get machine code emitted with
296  /// the MCJIT. This method returns true if machine code is not supported. It
297  /// fills the MCContext Ctx pointer which can be used to build custom
298  /// MCStreamer.
299  bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
300  raw_pwrite_stream &OS,
301  bool DisableVerify = true) override;
302 };
303 
304 } // end namespace llvm
305 
306 #endif // LLVM_TARGET_TARGETMACHINE_H
bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType, bool DisableVerify=true, AnalysisID StartBefore=nullptr, AnalysisID StartAfter=nullptr, AnalysisID StopBefore=nullptr, AnalysisID StopAfter=nullptr, MachineFunctionInitializer *MFInitializer=nullptr) override
Add passes to the specified pass manager to get the specified file emitted.
TargetMachine(const Target &T, StringRef DataLayoutString, const Triple &TargetTriple, StringRef CPU, StringRef FS, const TargetOptions &Options)
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
StringRef getTargetCPU() const
unsigned PrintMachineCode
PrintMachineCode - This flag is enabled when the -print-machineinstrs option is specified on the comm...
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
Reloc::Model getRelocationModel() const
Returns the code generation relocation model.
virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&, raw_pwrite_stream &, bool=true)
Add passes to the specified pass manager to get machine code emitted with the MCJIT.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
unsigned EnableFastISel
EnableFastISel - This flag enables fast-path instruction selection which trades away generated code q...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with...
MCSymbol * getSymbol(const GlobalValue *GV) const
unsigned DataSections
Emit data into separate sections.
virtual bool usesPhysRegsForPEI() const
True if the target uses physical regs at Prolog/Epilog insertion time.
void setO0WantsFastISel(bool Enable)
Analysis pass providing the TargetTransformInfo.
virtual bool targetSchedulesPostRAScheduling() const
True if subtarget inserts the final scheduling pass on its own.
CodeGenOpt::Level OptLevel
virtual TargetPassConfig * createPassConfig(PassManagerBase &PM)
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
const MCRegisterInfo * getMCRegisterInfo() const
const Triple & getTargetTriple() const
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
virtual TargetLoweringObjectFile * getObjFileLowering() const
Target-Independent Code Generator Pass Configuration Options.
const MCAsmInfo * AsmInfo
Contains target specific asm information.
Context object for machine code objects.
Definition: MCContext.h:51
const MCInstrInfo * getMCInstrInfo() const
#define F(x, y, z)
Definition: MD5.cpp:51
const MCSubtargetInfo * STI
bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, raw_pwrite_stream &OS, bool DisableVerify=true) override
Add passes to the specified pass manager to get machine code emitted with the MCJIT.
unsigned FunctionSections
Emit functions into separate sections.
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
unsigned UniqueSectionNames
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:57
bool isCompatibleDataLayout(const DataLayout &Candidate) const
Test if a DataLayout if compatible with the CodeGen for this target.
CodeModel::Model getCodeModel() const
Returns the code model.
void operator=(const TargetMachine &)=delete
CodeModel::Model CMModel
virtual TargetIRAnalysis getTargetIRAnalysis()
Get a TargetIRAnalysis appropriate for the target.
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
bool isPositionIndependent() const
This class describes a target machine that is implemented with the LLVM target-independent code gener...
const Target & TheTarget
The Target that this machine was created for.
cl::opt< TargetMachine::CodeGenFileType > FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile), cl::desc("Choose a file type (not all types are supported by all targets):"), cl::values(clEnumValN(TargetMachine::CGFT_AssemblyFile,"asm","Emit an assembly ('.s') file"), clEnumValN(TargetMachine::CGFT_ObjectFile,"obj","Emit a native object ('.o') file"), clEnumValN(TargetMachine::CGFT_Null,"null","Emit nothing, for performance testing")))
bool getFunctionSections() const
Return true if functions should be emitted into their own section, corresponding to -ffunction-sectio...
const MCInstrInfo * MII
bool shouldAssumeDSOLocal(const Module &M, const GlobalValue *GV) const
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool getDataSections() const
Return true if data objects should be emitted into their own section, corresponds to -fdata-sections...
TargetIntrinsicInfo - Interface to description of machine instruction set.
const TargetOptions DefaultOptions
virtual const TargetSubtargetInfo * getSubtargetImpl(const Function &) const
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
StringRef getTargetFeatureString() const
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, CodeGenFileType, bool=true, AnalysisID=nullptr, AnalysisID=nullptr, AnalysisID=nullptr, AnalysisID=nullptr, MachineFunctionInitializer *=nullptr)
Add passes to the specified pass manager to get the specified file emitted.
void setRequiresStructuredCFG(bool Value)
void setFastISel(bool Enable)
static const char * Target
LLVMTargetMachine(const Target &T, StringRef DataLayoutString, const Triple &TargetTriple, StringRef CPU, StringRef FS, TargetOptions Options, Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL)
bool getUniqueSectionNames() const
const STC & getSubtarget(const Function &F) const
This method returns a pointer to the specified type of TargetSubtargetInfo.
Target - Wrapper for Target specific information.
const MCRegisterInfo * MRI
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetIntrinsicInfo * getIntrinsicInfo() const
If intrinsic information is available, return it. If not, return null.
void resetTargetOptions(const Function &F) const
Reset the target options based on the function's attributes.
MCSubtargetInfo - Generic base class for all target subtargets.
const MCSubtargetInfo * getMCSubtargetInfo() const
const void * AnalysisID
Definition: Pass.h:46
void setOptLevel(CodeGenOpt::Level Level)
Overrides the optimization level.
const DataLayout createDataLayout() const
Create a DataLayout.
bool shouldPrintMachineCode() const
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
unsigned getPointerSize() const
Get the pointer size for this target.
const DataLayout DL
DataLayout for the target: keep ABI type size and alignment.
LLVM Value Representation.
Definition: Value.h:71
const Target & getTarget() const
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
This interface provides a way to initialize machine functions after they are created by the machine f...
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit...
TargetIRAnalysis getTargetIRAnalysis() override
Get a TargetIRAnalysis implementation for the target.
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size FIXME: The defaults need to be removed once all of the backends/clients are updat...
Definition: DataLayout.cpp:608
bool requiresStructuredCFG() const
virtual void addEarlyAsPossiblePasses(PassManagerBase &)
Add target-specific function passes that should be run as early as possible in the optimization pipel...