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 /// DataLayout for the target: keep ABI type size and alignment.
81 /// The DataLayout is created based on the string representation provided
82 /// during construction. It is kept here only to avoid reparsing the string
83 /// but should not really be used during compilation, because it has an
84 /// internal cache that is context specific.
87 /// Triple string, CPU name, and target feature strings the TargetMachine
88 /// instance is created with.
90 std::string TargetCPU;
93 /// Low level target information such as relocation model. Non-const to
94 /// allow resetting optimization level per-function.
95 MCCodeGenInfo *CodeGenInfo;
97 /// Contains target specific asm information.
98 const MCAsmInfo *AsmInfo;
100 const MCRegisterInfo *MRI;
101 const MCInstrInfo *MII;
102 const MCSubtargetInfo *STI;
104 unsigned RequireStructuredCFG : 1;
107 mutable TargetOptions Options;
109 virtual ~TargetMachine();
111 const Target &getTarget() const { return TheTarget; }
113 const Triple &getTargetTriple() const { return TargetTriple; }
114 StringRef getTargetCPU() const { return TargetCPU; }
115 StringRef getTargetFeatureString() const { return TargetFS; }
117 /// Virtual method implemented by subclasses that returns a reference to that
118 /// target's TargetSubtargetInfo-derived member variable.
119 virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
122 virtual TargetLoweringObjectFile *getObjFileLowering() const {
126 /// This method returns a pointer to the specified type of
127 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
128 /// returned is of the correct type.
129 template <typename STC> const STC &getSubtarget(const Function &F) const {
130 return *static_cast<const STC*>(getSubtargetImpl(F));
133 /// Create a DataLayout.
134 const DataLayout createDataLayout() const { return DL; }
136 /// Get the pointer size for this target.
138 /// This is the only time the DataLayout in the TargetMachine is used.
139 unsigned getPointerSize() const { return DL.getPointerSize(); }
141 /// \brief Reset the target options based on the function's attributes.
142 // FIXME: Remove TargetOptions that affect per-function code generation
143 // from TargetMachine.
144 void resetTargetOptions(const Function &F) const;
146 /// Return target specific asm information.
147 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
149 const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
150 const MCInstrInfo *getMCInstrInfo() const { return MII; }
151 const MCSubtargetInfo *getMCSubtargetInfo() const { return STI; }
153 /// If intrinsic information is available, return it. If not, return null.
154 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
158 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
159 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
161 /// Returns the code generation relocation model. The choices are static, PIC,
162 /// and dynamic-no-pic, and target default.
163 Reloc::Model getRelocationModel() const;
165 /// Returns the code model. The choices are small, kernel, medium, large, and
167 CodeModel::Model getCodeModel() const;
169 /// Returns the TLS model which should be used for the given global variable.
170 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
172 /// Returns the optimization level: None, Less, Default, or Aggressive.
173 CodeGenOpt::Level getOptLevel() const;
175 /// \brief Overrides the optimization level.
176 void setOptLevel(CodeGenOpt::Level Level) const;
178 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
180 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
182 /// Returns the default value of asm verbosity.
184 bool getAsmVerbosityDefault() const {
185 return Options.MCOptions.AsmVerbose;
188 bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
190 /// Return true if data objects should be emitted into their own section,
191 /// corresponds to -fdata-sections.
192 bool getDataSections() const {
193 return Options.DataSections;
196 /// Return true if functions should be emitted into their own section,
197 /// corresponding to -ffunction-sections.
198 bool getFunctionSections() const {
199 return Options.FunctionSections;
202 /// \brief Get a \c TargetIRAnalysis appropriate for the target.
204 /// This is used to construct the new pass manager's target IR analysis pass,
205 /// set up appropriately for this target machine. Even the old pass manager
206 /// uses this to answer queries about the IR.
207 virtual TargetIRAnalysis getTargetIRAnalysis();
209 /// These enums are meant to be passed into addPassesToEmitFile to indicate
210 /// what type of file to emit, and returned by it to indicate what type of
211 /// file could actually be made.
212 enum CodeGenFileType {
215 CGFT_Null // Do not emit any output.
218 /// Add passes to the specified pass manager to get the specified file
219 /// emitted. Typically this will involve several steps of code generation.
220 /// This method should return true if emission of this file type is not
221 /// supported, or false on success.
222 virtual bool addPassesToEmitFile(
223 PassManagerBase &, raw_pwrite_stream &, CodeGenFileType,
224 bool /*DisableVerify*/ = true, AnalysisID /*StartBefore*/ = nullptr,
225 AnalysisID /*StartAfter*/ = nullptr, AnalysisID /*StopAfter*/ = nullptr,
226 MachineFunctionInitializer * /*MFInitializer*/ = nullptr) {
230 /// Add passes to the specified pass manager to get machine code emitted with
231 /// the MCJIT. This method returns true if machine code is not supported. It
232 /// fills the MCContext Ctx pointer which can be used to build custom
235 virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&,
237 bool /*DisableVerify*/ = true) {
241 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
242 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
243 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
246 /// This class describes a target machine that is implemented with the LLVM
247 /// target-independent code generator.
249 class LLVMTargetMachine : public TargetMachine {
250 protected: // Can only create subclasses.
251 LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
252 const Triple &TargetTriple, StringRef CPU, StringRef FS,
253 TargetOptions Options, Reloc::Model RM, CodeModel::Model CM,
254 CodeGenOpt::Level OL);
258 /// \brief Get a TargetIRAnalysis implementation for the target.
260 /// This analysis will produce a TTI result which uses the common code
261 /// generator to answer queries about the IR.
262 TargetIRAnalysis getTargetIRAnalysis() override;
264 /// Create a pass configuration object to be used by addPassToEmitX methods
265 /// for generating a pipeline of CodeGen passes.
266 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
268 /// Add passes to the specified pass manager to get the specified file
269 /// emitted. Typically this will involve several steps of code generation.
270 bool addPassesToEmitFile(
271 PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType,
272 bool DisableVerify = true, AnalysisID StartBefore = nullptr,
273 AnalysisID StartAfter = nullptr, AnalysisID StopAfter = nullptr,
274 MachineFunctionInitializer *MFInitializer = nullptr) override;
276 /// Add passes to the specified pass manager to get machine code emitted with
277 /// the MCJIT. This method returns true if machine code is not supported. It
278 /// fills the MCContext Ctx pointer which can be used to build custom
280 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
281 raw_pwrite_stream &OS,
282 bool DisableVerify = true) override;
285 } // End llvm namespace