LLVM  3.7.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 <cassert>
24 #include <string>
25 
26 namespace llvm {
27 
28 class InstrItineraryData;
29 class GlobalValue;
30 class Mangler;
31 class MachineFunctionInitializer;
32 class MCAsmInfo;
33 class MCCodeGenInfo;
34 class MCContext;
35 class MCInstrInfo;
36 class MCRegisterInfo;
37 class MCSubtargetInfo;
38 class MCSymbol;
39 class Target;
40 class DataLayout;
41 class TargetLibraryInfo;
42 class TargetFrameLowering;
43 class TargetIRAnalysis;
44 class TargetIntrinsicInfo;
45 class TargetLowering;
46 class TargetPassConfig;
47 class TargetRegisterInfo;
48 class TargetSelectionDAGInfo;
49 class TargetSubtargetInfo;
50 class TargetTransformInfo;
51 class formatted_raw_ostream;
52 class raw_ostream;
53 class raw_pwrite_stream;
54 class TargetLoweringObjectFile;
55 
56 // The old pass manager infrastructure is hidden in a legacy namespace now.
57 namespace legacy {
58 class PassManagerBase;
59 }
60 using legacy::PassManagerBase;
61 
62 //===----------------------------------------------------------------------===//
63 ///
64 /// Primary interface to the complete machine description for the target
65 /// machine. All target-specific information should be accessible through this
66 /// interface.
67 ///
69  TargetMachine(const TargetMachine &) = delete;
70  void operator=(const TargetMachine &) = delete;
71 protected: // Can only create subclasses.
72  TargetMachine(const Target &T, StringRef DataLayoutString,
74  const TargetOptions &Options);
75 
76  /// The Target that this machine was created for.
77  const Target &TheTarget;
78 
79  /// For ABI type size and alignment.
80  const DataLayout DL;
81 
82  /// Triple string, CPU name, and target feature strings the TargetMachine
83  /// instance is created with.
85  std::string TargetCPU;
86  std::string TargetFS;
87 
88  /// Low level target information such as relocation model. Non-const to
89  /// allow resetting optimization level per-function.
91 
92  /// Contains target specific asm information.
94 
96  const MCInstrInfo *MII;
98 
99  unsigned RequireStructuredCFG : 1;
100 
101 public:
103 
104  virtual ~TargetMachine();
105 
106  const Target &getTarget() const { return TheTarget; }
107 
108  const Triple &getTargetTriple() const { return TargetTriple; }
109  StringRef getTargetCPU() const { return TargetCPU; }
111 
112  /// Virtual method implemented by subclasses that returns a reference to that
113  /// target's TargetSubtargetInfo-derived member variable.
114  virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
115  return nullptr;
116  }
118  return nullptr;
119  }
120 
121  /// This method returns a pointer to the specified type of
122  /// TargetSubtargetInfo. In debug builds, it verifies that the object being
123  /// returned is of the correct type.
124  template <typename STC> const STC &getSubtarget(const Function &F) const {
125  return *static_cast<const STC*>(getSubtargetImpl(F));
126  }
127 
128  /// Deprecated in 3.7, will be removed in 3.8. Use createDataLayout() instead.
129  ///
130  /// This method returns a pointer to the DataLayout for the target. It should
131  /// be unchanging for every subtarget.
132  const DataLayout *getDataLayout() const { return &DL; }
133 
134  /// Create a DataLayout.
135  const DataLayout createDataLayout() const { return DL; }
136 
137  /// \brief Reset the target options based on the function's attributes.
138  // FIXME: Remove TargetOptions that affect per-function code generation
139  // from TargetMachine.
140  void resetTargetOptions(const Function &F) const;
141 
142  /// Return target specific asm information.
143  const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
144 
145  const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
146  const MCInstrInfo *getMCInstrInfo() const { return MII; }
147  const MCSubtargetInfo *getMCSubtargetInfo() const { return STI; }
148 
149  /// If intrinsic information is available, return it. If not, return null.
150  virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
151  return nullptr;
152  }
153 
156 
157  /// Returns the code generation relocation model. The choices are static, PIC,
158  /// and dynamic-no-pic, and target default.
160 
161  /// Returns the code model. The choices are small, kernel, medium, large, and
162  /// target default.
164 
165  /// Returns the TLS model which should be used for the given global variable.
166  TLSModel::Model getTLSModel(const GlobalValue *GV) const;
167 
168  /// Returns the optimization level: None, Less, Default, or Aggressive.
170 
171  /// \brief Overrides the optimization level.
172  void setOptLevel(CodeGenOpt::Level Level) const;
173 
174  void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
175 
176  bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
177 
178  /// Returns the default value of asm verbosity.
179  ///
180  bool getAsmVerbosityDefault() const {
181  return Options.MCOptions.AsmVerbose;
182  }
183 
184  bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
185 
186  /// Return true if data objects should be emitted into their own section,
187  /// corresponds to -fdata-sections.
188  bool getDataSections() const {
189  return Options.DataSections;
190  }
191 
192  /// Return true if functions should be emitted into their own section,
193  /// corresponding to -ffunction-sections.
194  bool getFunctionSections() const {
195  return Options.FunctionSections;
196  }
197 
198  /// \brief Get a \c TargetIRAnalysis appropriate for the target.
199  ///
200  /// This is used to construct the new pass manager's target IR analysis pass,
201  /// set up appropriately for this target machine. Even the old pass manager
202  /// uses this to answer queries about the IR.
204 
205  /// These enums are meant to be passed into addPassesToEmitFile to indicate
206  /// what type of file to emit, and returned by it to indicate what type of
207  /// file could actually be made.
211  CGFT_Null // Do not emit any output.
212  };
213 
214  /// Add passes to the specified pass manager to get the specified file
215  /// emitted. Typically this will involve several steps of code generation.
216  /// This method should return true if emission of this file type is not
217  /// supported, or false on success.
218  virtual bool addPassesToEmitFile(
219  PassManagerBase &, raw_pwrite_stream &, CodeGenFileType,
220  bool /*DisableVerify*/ = true, AnalysisID /*StartBefore*/ = nullptr,
221  AnalysisID /*StartAfter*/ = nullptr, AnalysisID /*StopAfter*/ = nullptr,
222  MachineFunctionInitializer * /*MFInitializer*/ = nullptr) {
223  return true;
224  }
225 
226  /// Add passes to the specified pass manager to get machine code emitted with
227  /// the MCJIT. This method returns true if machine code is not supported. It
228  /// fills the MCContext Ctx pointer which can be used to build custom
229  /// MCStreamer.
230  ///
231  virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&,
233  bool /*DisableVerify*/ = true) {
234  return true;
235  }
236 
238  Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
239  MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
240 };
241 
242 /// This class describes a target machine that is implemented with the LLVM
243 /// target-independent code generator.
244 ///
246 protected: // Can only create subclasses.
247  LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
250  CodeGenOpt::Level OL);
251 
252  void initAsmInfo();
253 public:
254  /// \brief Get a TargetIRAnalysis implementation for the target.
255  ///
256  /// This analysis will produce a TTI result which uses the common code
257  /// generator to answer queries about the IR.
259 
260  /// Create a pass configuration object to be used by addPassToEmitX methods
261  /// for generating a pipeline of CodeGen passes.
262  virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
263 
264  /// Add passes to the specified pass manager to get the specified file
265  /// emitted. Typically this will involve several steps of code generation.
266  bool addPassesToEmitFile(
267  PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType,
268  bool DisableVerify = true, AnalysisID StartBefore = nullptr,
269  AnalysisID StartAfter = nullptr, AnalysisID StopAfter = nullptr,
270  MachineFunctionInitializer *MFInitializer = nullptr) override;
271 
272  /// Add passes to the specified pass manager to get machine code emitted with
273  /// the MCJIT. This method returns true if machine code is not supported. It
274  /// fills the MCContext Ctx pointer which can be used to build custom
275  /// MCStreamer.
276  bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
277  raw_pwrite_stream &OS,
278  bool DisableVerify = true) override;
279 };
280 
281 } // End llvm namespace
282 
283 #endif
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
StringRef getTargetCPU() const
unsigned PrintMachineCode
PrintMachineCode - This flag is enabled when the -print-machineinstrs option is specified on the comm...
Definition: TargetOptions.h:83
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
MCTargetOptions MCOptions
Machine level options.
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...
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with...
unsigned DataSections
Emit data into separate sections.
Analysis pass providing the TargetTransformInfo.
F(f)
virtual TargetPassConfig * createPassConfig(PassManagerBase &PM)
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
Definition: Passes.cpp:249
const MCRegisterInfo * getMCRegisterInfo() const
const Triple & getTargetTriple() const
void setOptLevel(CodeGenOpt::Level Level) const
Overrides the optimization level.
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:48
const MCInstrInfo * getMCInstrInfo() const
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:58
CodeModel::Model getCodeModel() const
Returns the code model.
virtual TargetIRAnalysis getTargetIRAnalysis()
Get a TargetIRAnalysis appropriate for the target.
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
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.
bool getFunctionSections() const
Return true if functions should be emitted into their own section, corresponding to -ffunction-sectio...
const MCInstrInfo * MII
MCSymbol * getSymbol(const GlobalValue *GV, Mangler &Mang) 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.
virtual const TargetSubtargetInfo * getSubtargetImpl(const Function &) const
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
StringRef getTargetFeatureString() const
const DataLayout * getDataLayout() const
Deprecated in 3.7, will be removed in 3.8.
MCCodeGenInfo * CodeGenInfo
Low level target information such as relocation model.
void setRequiresStructuredCFG(bool Value)
void setFastISel(bool Enable)
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.
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, CodeGenFileType, bool=true, AnalysisID=nullptr, AnalysisID=nullptr, AnalysisID=nullptr, MachineFunctionInitializer *=nullptr)
Add passes to the specified pass manager to get the specified file emitted.
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.
cl::opt< std::string > StopAfter("stop-after", cl::desc("Stop compilation after a specific pass"), cl::value_desc("pass-name"), cl::init(""))
cl::opt< std::string > StartAfter("start-after", cl::desc("Resume compilation after a specific pass"), cl::value_desc("pass-name"), cl::init(""))
const MCSubtargetInfo * getMCSubtargetInfo() const
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"), clEnumValEnd))
const void * AnalysisID
Definition: Pass.h:47
bool getAsmVerbosityDefault() const
Returns the default value of asm verbosity.
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:321
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
const DataLayout DL
For ABI type size and alignment.
LLVM Value Representation.
Definition: Value.h:69
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:40
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.
bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType, bool DisableVerify=true, AnalysisID StartBefore=nullptr, AnalysisID StartAfter=nullptr, AnalysisID StopAfter=nullptr, MachineFunctionInitializer *MFInitializer=nullptr) override
Add passes to the specified pass manager to get the specified file emitted.
bool requiresStructuredCFG() const