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/IR/DataLayout.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/CodeGen.h"
21 #include "llvm/Target/TargetOptions.h"
27 class InstrItineraryData;
38 class TargetLibraryInfo;
39 class TargetFrameLowering;
40 class TargetIRAnalysis;
41 class TargetIntrinsicInfo;
43 class TargetPassConfig;
44 class TargetRegisterInfo;
45 class TargetSelectionDAGInfo;
46 class TargetSubtargetInfo;
47 class TargetTransformInfo;
48 class formatted_raw_ostream;
50 class TargetLoweringObjectFile;
52 // The old pass manager infrastructure is hidden in a legacy namespace now.
54 class PassManagerBase;
56 using legacy::PassManagerBase;
58 //===----------------------------------------------------------------------===//
60 /// TargetMachine - Primary interface to the complete machine description for
61 /// the target machine. All target-specific information should be accessible
62 /// through this interface.
65 TargetMachine(const TargetMachine &) = delete;
66 void operator=(const TargetMachine &) = delete;
67 protected: // Can only create subclasses.
68 TargetMachine(const Target &T, StringRef DataLayoutString,
69 StringRef TargetTriple, StringRef CPU, StringRef FS,
70 const TargetOptions &Options);
72 /// TheTarget - The Target that this machine was created for.
73 const Target &TheTarget;
75 /// DataLayout - For ABI type size and alignment.
78 /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
79 /// feature strings the TargetMachine instance is created with.
80 std::string TargetTriple;
81 std::string TargetCPU;
84 /// CodeGenInfo - Low level target information such as relocation model.
85 /// Non-const to allow resetting optimization level per-function.
86 MCCodeGenInfo *CodeGenInfo;
88 /// AsmInfo - Contains target specific asm information.
90 const MCAsmInfo *AsmInfo;
91 const MCRegisterInfo *MRI;
92 const MCInstrInfo *MII;
94 unsigned RequireStructuredCFG : 1;
97 mutable TargetOptions Options;
99 virtual ~TargetMachine();
101 const Target &getTarget() const { return TheTarget; }
103 StringRef getTargetTriple() const { return TargetTriple; }
104 StringRef getTargetCPU() const { return TargetCPU; }
105 StringRef getTargetFeatureString() const { return TargetFS; }
107 /// getSubtargetImpl - virtual method implemented by subclasses that returns
108 /// a reference to that target's TargetSubtargetInfo-derived member variable.
109 virtual const TargetSubtargetInfo *getSubtargetImpl() const {
112 virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
113 return getSubtargetImpl();
115 virtual TargetLoweringObjectFile *getObjFileLowering() const {
119 /// getSubtarget - This method returns a pointer to the specified type of
120 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
121 /// returned is of the correct type.
122 template<typename STC> const STC &getSubtarget() const {
123 return *static_cast<const STC*>(getSubtargetImpl());
125 template <typename STC> const STC &getSubtarget(const Function &) const {
126 return *static_cast<const STC*>(getSubtargetImpl());
129 /// getDataLayout - This method returns a pointer to the DataLayout for
130 /// the target. It should be unchanging for every subtarget.
131 const DataLayout *getDataLayout() const { return &DL; }
133 /// \brief Reset the target options based on the function's attributes.
134 // FIXME: Remove TargetOptions that affect per-function code generation
135 // from TargetMachine.
136 void resetTargetOptions(const Function &F) const;
138 /// getMCAsmInfo - Return target specific asm information.
140 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
141 const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
142 const MCInstrInfo *getMCInstrInfo() const { return MII; }
144 /// getIntrinsicInfo - If intrinsic information is available, return it. If
145 /// not, return null.
147 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
151 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
152 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
154 /// getRelocationModel - Returns the code generation relocation model. The
155 /// choices are static, PIC, and dynamic-no-pic, and target default.
156 Reloc::Model getRelocationModel() const;
158 /// getCodeModel - Returns the code model. The choices are small, kernel,
159 /// medium, large, and target default.
160 CodeModel::Model getCodeModel() const;
162 /// getTLSModel - Returns the TLS model which should be used for the given
164 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
166 /// getOptLevel - Returns the optimization level: None, Less,
167 /// Default, or Aggressive.
168 CodeGenOpt::Level getOptLevel() const;
170 /// \brief Overrides the optimization level.
171 void setOptLevel(CodeGenOpt::Level Level) const;
173 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
175 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
177 /// Returns the default value of asm verbosity.
179 bool getAsmVerbosityDefault() const {
180 return Options.MCOptions.AsmVerbose;
183 bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
185 /// Return true if data objects should be emitted into their own section,
186 /// corresponds to -fdata-sections.
187 bool getDataSections() const {
188 return Options.DataSections;
191 /// Return true if functions should be emitted into their own section,
192 /// corresponding to -ffunction-sections.
193 bool getFunctionSections() const {
194 return Options.FunctionSections;
197 /// \brief Get a \c TargetIRAnalysis appropriate for the target.
199 /// This is used to construct the new pass manager's target IR analysis pass,
200 /// set up appropriately for this target machine. Even the old pass manager
201 /// uses this to answer queries about the IR.
202 virtual TargetIRAnalysis getTargetIRAnalysis();
204 /// CodeGenFileType - These enums are meant to be passed into
205 /// addPassesToEmitFile to indicate what type of file to emit, and returned by
206 /// it to indicate what type of file could actually be made.
207 enum CodeGenFileType {
210 CGFT_Null // Do not emit any output.
213 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
214 /// specified file emitted. Typically this will involve several steps of code
215 /// generation. This method should return true if emission of this file type
216 /// is not supported, or false on success.
217 virtual bool addPassesToEmitFile(PassManagerBase &,
218 formatted_raw_ostream &,
220 bool /*DisableVerify*/ = true,
221 AnalysisID /*StartAfter*/ = nullptr,
222 AnalysisID /*StopAfter*/ = nullptr) {
226 /// addPassesToEmitMC - Add passes to the specified pass manager to get
227 /// machine code emitted with the MCJIT. This method returns true if machine
228 /// code is not supported. It fills the MCContext Ctx pointer which can be
229 /// used to build custom MCStreamer.
231 virtual bool addPassesToEmitMC(PassManagerBase &,
234 bool /*DisableVerify*/ = true) {
238 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
239 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
240 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
243 /// LLVMTargetMachine - This class describes a target machine that is
244 /// implemented with the LLVM target-independent code generator.
246 class LLVMTargetMachine : public TargetMachine {
247 protected: // Can only create subclasses.
248 LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
249 StringRef TargetTriple, StringRef CPU, StringRef FS,
250 TargetOptions Options, Reloc::Model RM, CodeModel::Model CM,
251 CodeGenOpt::Level OL);
255 /// \brief Get a TargetIRAnalysis implementation for the target.
257 /// This analysis will produce a TTI result which uses the common code
258 /// generator to answer queries about the IR.
259 TargetIRAnalysis getTargetIRAnalysis() override;
261 /// createPassConfig - Create a pass configuration object to be used by
262 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
263 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
265 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
266 /// specified file emitted. Typically this will involve several steps of code
268 bool addPassesToEmitFile(PassManagerBase &PM, formatted_raw_ostream &Out,
269 CodeGenFileType FileType, bool DisableVerify = true,
270 AnalysisID StartAfter = nullptr,
271 AnalysisID StopAfter = nullptr) override;
273 /// addPassesToEmitMC - Add passes to the specified pass manager to get
274 /// machine code emitted with the MCJIT. This method returns true if machine
275 /// code is not supported. It fills the MCContext Ctx pointer which can be
276 /// used to build custom MCStreamer.
278 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
279 raw_ostream &OS, bool DisableVerify = true) override;
282 } // End llvm namespace