1 //===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the TargetMachine and LLVMTargetMachine classes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TARGET_TARGETMACHINE_H
15 #define LLVM_TARGET_TARGETMACHINE_H
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"
22 #include "llvm/Target/TargetOptions.h"
28 class InstrItineraryData;
31 class MachineFunctionInitializer;
37 class MCSubtargetInfo;
41 class TargetLibraryInfo;
42 class TargetFrameLowering;
43 class TargetIRAnalysis;
44 class TargetIntrinsicInfo;
46 class TargetPassConfig;
47 class TargetRegisterInfo;
48 class TargetSelectionDAGInfo;
49 class TargetSubtargetInfo;
50 class TargetTransformInfo;
51 class formatted_raw_ostream;
53 class raw_pwrite_stream;
54 class TargetLoweringObjectFile;
56 // The old pass manager infrastructure is hidden in a legacy namespace now.
58 class PassManagerBase;
60 using legacy::PassManagerBase;
62 //===----------------------------------------------------------------------===//
64 /// Primary interface to the complete machine description for the target
65 /// machine. All target-specific information should be accessible through this
69 TargetMachine(const TargetMachine &) = delete;
70 void operator=(const TargetMachine &) = delete;
71 protected: // Can only create subclasses.
72 TargetMachine(const Target &T, StringRef DataLayoutString,
73 const Triple &TargetTriple, StringRef CPU, StringRef FS,
74 const TargetOptions &Options);
76 /// The Target that this machine was created for.
77 const Target &TheTarget;
79 /// For ABI type size and alignment.
82 /// Triple string, CPU name, and target feature strings the TargetMachine
83 /// instance is created with.
85 std::string TargetCPU;
88 /// Low level target information such as relocation model. Non-const to
89 /// allow resetting optimization level per-function.
90 MCCodeGenInfo *CodeGenInfo;
92 /// Contains target specific asm information.
93 const MCAsmInfo *AsmInfo;
95 const MCRegisterInfo *MRI;
96 const MCInstrInfo *MII;
97 const MCSubtargetInfo *STI;
99 unsigned RequireStructuredCFG : 1;
102 mutable TargetOptions Options;
104 virtual ~TargetMachine();
106 const Target &getTarget() const { return TheTarget; }
108 const Triple &getTargetTriple() const { return TargetTriple; }
109 StringRef getTargetCPU() const { return TargetCPU; }
110 StringRef getTargetFeatureString() const { return TargetFS; }
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 {
117 virtual TargetLoweringObjectFile *getObjFileLowering() const {
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));
128 /// Deprecated in 3.7, will be removed in 3.8. Use createDataLayout() instead.
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; }
134 /// Create a DataLayout.
135 const DataLayout createDataLayout() const { return DL; }
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;
142 /// Return target specific asm information.
143 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
145 const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
146 const MCInstrInfo *getMCInstrInfo() const { return MII; }
147 const MCSubtargetInfo *getMCSubtargetInfo() const { return STI; }
149 /// If intrinsic information is available, return it. If not, return null.
150 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
154 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
155 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
157 /// Returns the code generation relocation model. The choices are static, PIC,
158 /// and dynamic-no-pic, and target default.
159 Reloc::Model getRelocationModel() const;
161 /// Returns the code model. The choices are small, kernel, medium, large, and
163 CodeModel::Model getCodeModel() const;
165 /// Returns the TLS model which should be used for the given global variable.
166 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
168 /// Returns the optimization level: None, Less, Default, or Aggressive.
169 CodeGenOpt::Level getOptLevel() const;
171 /// \brief Overrides the optimization level.
172 void setOptLevel(CodeGenOpt::Level Level) const;
174 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
176 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
178 /// Returns the default value of asm verbosity.
180 bool getAsmVerbosityDefault() const {
181 return Options.MCOptions.AsmVerbose;
184 bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
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;
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;
198 /// \brief Get a \c TargetIRAnalysis appropriate for the target.
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.
203 virtual TargetIRAnalysis getTargetIRAnalysis();
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.
208 enum CodeGenFileType {
211 CGFT_Null // Do not emit any output.
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) {
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
231 virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&,
233 bool /*DisableVerify*/ = true) {
237 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
238 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
239 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
242 /// This class describes a target machine that is implemented with the LLVM
243 /// target-independent code generator.
245 class LLVMTargetMachine : public TargetMachine {
246 protected: // Can only create subclasses.
247 LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
248 const Triple &TargetTriple, StringRef CPU, StringRef FS,
249 TargetOptions Options, Reloc::Model RM, CodeModel::Model CM,
250 CodeGenOpt::Level OL);
254 /// \brief Get a TargetIRAnalysis implementation for the target.
256 /// This analysis will produce a TTI result which uses the common code
257 /// generator to answer queries about the IR.
258 TargetIRAnalysis getTargetIRAnalysis() override;
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);
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;
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
276 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
277 raw_pwrite_stream &OS,
278 bool DisableVerify = true) override;
281 } // End llvm namespace