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"
27 // This function from the C API is deprecated. We still supports it using a
28 // private method on the TargetMachine for now. But it needs to be friended and
29 // so we forward declare it here.
30 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T);
35 class InstrItineraryData;
38 class MachineFunctionInitializer;
44 class MCSubtargetInfo;
48 class TargetLibraryInfo;
49 class TargetFrameLowering;
50 class TargetIRAnalysis;
51 class TargetIntrinsicInfo;
53 class TargetPassConfig;
54 class TargetRegisterInfo;
55 class TargetSelectionDAGInfo;
56 class TargetSubtargetInfo;
57 class TargetTransformInfo;
58 class formatted_raw_ostream;
60 class raw_pwrite_stream;
61 class TargetLoweringObjectFile;
63 // The old pass manager infrastructure is hidden in a legacy namespace now.
65 class PassManagerBase;
67 using legacy::PassManagerBase;
69 //===----------------------------------------------------------------------===//
71 /// Primary interface to the complete machine description for the target
72 /// machine. All target-specific information should be accessible through this
76 TargetMachine(const TargetMachine &) = delete;
77 void operator=(const TargetMachine &) = delete;
78 protected: // Can only create subclasses.
79 TargetMachine(const Target &T, StringRef DataLayoutString,
80 const Triple &TargetTriple, StringRef CPU, StringRef FS,
81 const TargetOptions &Options);
83 /// The Target that this machine was created for.
84 const Target &TheTarget;
86 /// DataLayout for the target: keep ABI type size and alignment.
88 /// The DataLayout is created based on the string representation provided
89 /// during construction. It is kept here only to avoid reparsing the string
90 /// but should not really be used during compilation, because it has an
91 /// internal cache that is context specific.
94 /// Triple string, CPU name, and target feature strings the TargetMachine
95 /// instance is created with.
97 std::string TargetCPU;
100 /// Low level target information such as relocation model. Non-const to
101 /// allow resetting optimization level per-function.
102 MCCodeGenInfo *CodeGenInfo;
104 /// Contains target specific asm information.
105 const MCAsmInfo *AsmInfo;
107 const MCRegisterInfo *MRI;
108 const MCInstrInfo *MII;
109 const MCSubtargetInfo *STI;
111 unsigned RequireStructuredCFG : 1;
113 /// This API is here to support the C API, deprecated in 3.7 release.
114 /// This should never be used outside of legacy existing client.
115 const DataLayout &getDataLayout() const { return DL; }
116 friend struct LLVMOpaqueTargetData * ::LLVMGetTargetMachineData(
117 LLVMTargetMachineRef T);
120 mutable TargetOptions Options;
122 virtual ~TargetMachine();
124 const Target &getTarget() const { return TheTarget; }
126 const Triple &getTargetTriple() const { return TargetTriple; }
127 StringRef getTargetCPU() const { return TargetCPU; }
128 StringRef getTargetFeatureString() const { return TargetFS; }
130 /// Virtual method implemented by subclasses that returns a reference to that
131 /// target's TargetSubtargetInfo-derived member variable.
132 virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
135 virtual TargetLoweringObjectFile *getObjFileLowering() const {
139 /// This method returns a pointer to the specified type of
140 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
141 /// returned is of the correct type.
142 template <typename STC> const STC &getSubtarget(const Function &F) const {
143 return *static_cast<const STC*>(getSubtargetImpl(F));
146 /// Create a DataLayout.
147 const DataLayout createDataLayout() const { return DL; }
149 /// Test if a DataLayout if compatible with the CodeGen for this target.
151 /// The LLVM Module owns a DataLayout that is used for the target independent
152 /// optimizations and code generation. This hook provides a target specific
153 /// check on the validity of this DataLayout.
154 bool isCompatibleDataLayout(const DataLayout &Candidate) const {
155 return DL == Candidate;
158 /// Get the pointer size for this target.
160 /// This is the only time the DataLayout in the TargetMachine is used.
161 unsigned getPointerSize() const { return DL.getPointerSize(); }
163 /// \brief Reset the target options based on the function's attributes.
164 // FIXME: Remove TargetOptions that affect per-function code generation
165 // from TargetMachine.
166 void resetTargetOptions(const Function &F) const;
168 /// Return target specific asm information.
169 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
171 const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
172 const MCInstrInfo *getMCInstrInfo() const { return MII; }
173 const MCSubtargetInfo *getMCSubtargetInfo() const { return STI; }
175 /// If intrinsic information is available, return it. If not, return null.
176 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
180 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
181 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
183 /// Returns the code generation relocation model. The choices are static, PIC,
184 /// and dynamic-no-pic, and target default.
185 Reloc::Model getRelocationModel() const;
187 /// Returns the code model. The choices are small, kernel, medium, large, and
189 CodeModel::Model getCodeModel() const;
191 /// Returns the TLS model which should be used for the given global variable.
192 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
194 /// Returns the optimization level: None, Less, Default, or Aggressive.
195 CodeGenOpt::Level getOptLevel() const;
197 /// \brief Overrides the optimization level.
198 void setOptLevel(CodeGenOpt::Level Level) const;
200 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
202 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
204 /// Returns the default value of asm verbosity.
206 bool getAsmVerbosityDefault() const {
207 return Options.MCOptions.AsmVerbose;
210 bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
212 /// Return true if data objects should be emitted into their own section,
213 /// corresponds to -fdata-sections.
214 bool getDataSections() const {
215 return Options.DataSections;
218 /// Return true if functions should be emitted into their own section,
219 /// corresponding to -ffunction-sections.
220 bool getFunctionSections() const {
221 return Options.FunctionSections;
224 /// \brief Get a \c TargetIRAnalysis appropriate for the target.
226 /// This is used to construct the new pass manager's target IR analysis pass,
227 /// set up appropriately for this target machine. Even the old pass manager
228 /// uses this to answer queries about the IR.
229 virtual TargetIRAnalysis getTargetIRAnalysis();
231 /// These enums are meant to be passed into addPassesToEmitFile to indicate
232 /// what type of file to emit, and returned by it to indicate what type of
233 /// file could actually be made.
234 enum CodeGenFileType {
237 CGFT_Null // Do not emit any output.
240 /// Add passes to the specified pass manager to get the specified file
241 /// emitted. Typically this will involve several steps of code generation.
242 /// This method should return true if emission of this file type is not
243 /// supported, or false on success.
244 virtual bool addPassesToEmitFile(
245 PassManagerBase &, raw_pwrite_stream &, CodeGenFileType,
246 bool /*DisableVerify*/ = true, AnalysisID /*StartBefore*/ = nullptr,
247 AnalysisID /*StartAfter*/ = nullptr, AnalysisID /*StopAfter*/ = nullptr,
248 MachineFunctionInitializer * /*MFInitializer*/ = nullptr) {
252 /// Add passes to the specified pass manager to get machine code emitted with
253 /// the MCJIT. This method returns true if machine code is not supported. It
254 /// fills the MCContext Ctx pointer which can be used to build custom
257 virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&,
259 bool /*DisableVerify*/ = true) {
263 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
264 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
265 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
268 /// This class describes a target machine that is implemented with the LLVM
269 /// target-independent code generator.
271 class LLVMTargetMachine : public TargetMachine {
272 protected: // Can only create subclasses.
273 LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
274 const Triple &TargetTriple, StringRef CPU, StringRef FS,
275 TargetOptions Options, Reloc::Model RM, CodeModel::Model CM,
276 CodeGenOpt::Level OL);
280 /// \brief Get a TargetIRAnalysis implementation for the target.
282 /// This analysis will produce a TTI result which uses the common code
283 /// generator to answer queries about the IR.
284 TargetIRAnalysis getTargetIRAnalysis() override;
286 /// Create a pass configuration object to be used by addPassToEmitX methods
287 /// for generating a pipeline of CodeGen passes.
288 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
290 /// Add passes to the specified pass manager to get the specified file
291 /// emitted. Typically this will involve several steps of code generation.
292 bool addPassesToEmitFile(
293 PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType,
294 bool DisableVerify = true, AnalysisID StartBefore = nullptr,
295 AnalysisID StartAfter = nullptr, AnalysisID StopAfter = nullptr,
296 MachineFunctionInitializer *MFInitializer = nullptr) override;
298 /// Add passes to the specified pass manager to get machine code emitted with
299 /// the MCJIT. This method returns true if machine code is not supported. It
300 /// fills the MCContext Ctx pointer which can be used to build custom
302 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
303 raw_pwrite_stream &OS,
304 bool DisableVerify = true) override;
307 } // End llvm namespace