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;
36 class TargetLibraryInfo;
37 class TargetFrameLowering;
38 class TargetIRAnalysis;
39 class TargetIntrinsicInfo;
41 class TargetPassConfig;
42 class TargetRegisterInfo;
43 class TargetSelectionDAGInfo;
44 class TargetSubtargetInfo;
45 class TargetTransformInfo;
46 class formatted_raw_ostream;
48 class TargetLoweringObjectFile;
50 // The old pass manager infrastructure is hidden in a legacy namespace now.
52 class PassManagerBase;
54 using legacy::PassManagerBase;
56 //===----------------------------------------------------------------------===//
58 /// TargetMachine - Primary interface to the complete machine description for
59 /// the target machine. All target-specific information should be accessible
60 /// through this interface.
63 TargetMachine(const TargetMachine &) = delete;
64 void operator=(const TargetMachine &) = delete;
65 protected: // Can only create subclasses.
66 TargetMachine(const Target &T, StringRef DataLayoutString,
67 StringRef TargetTriple, StringRef CPU, StringRef FS,
68 const TargetOptions &Options);
70 /// TheTarget - The Target that this machine was created for.
71 const Target &TheTarget;
73 /// DataLayout - For ABI type size and alignment.
76 /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
77 /// feature strings the TargetMachine instance is created with.
78 std::string TargetTriple;
79 std::string TargetCPU;
82 /// CodeGenInfo - Low level target information such as relocation model.
83 /// Non-const to allow resetting optimization level per-function.
84 MCCodeGenInfo *CodeGenInfo;
86 /// AsmInfo - Contains target specific asm information.
88 const MCAsmInfo *AsmInfo;
90 unsigned RequireStructuredCFG : 1;
93 mutable TargetOptions Options;
95 virtual ~TargetMachine();
97 const Target &getTarget() const { return TheTarget; }
99 StringRef getTargetTriple() const { return TargetTriple; }
100 StringRef getTargetCPU() const { return TargetCPU; }
101 StringRef getTargetFeatureString() const { return TargetFS; }
103 /// getSubtargetImpl - virtual method implemented by subclasses that returns
104 /// a reference to that target's TargetSubtargetInfo-derived member variable.
105 virtual const TargetSubtargetInfo *getSubtargetImpl() const {
108 virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
109 return getSubtargetImpl();
111 virtual TargetLoweringObjectFile *getObjFileLowering() const {
115 /// getSubtarget - This method returns a pointer to the specified type of
116 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
117 /// returned is of the correct type.
118 template<typename STC> const STC &getSubtarget() const {
119 return *static_cast<const STC*>(getSubtargetImpl());
121 template <typename STC> const STC &getSubtarget(const Function &) const {
122 return *static_cast<const STC*>(getSubtargetImpl());
125 /// getDataLayout - This method returns a pointer to the DataLayout for
126 /// the target. It should be unchanging for every subtarget.
127 const DataLayout *getDataLayout() const { return &DL; }
129 /// \brief Reset the target options based on the function's attributes.
130 // FIXME: Remove TargetOptions that affect per-function code generation
131 // from TargetMachine.
132 void resetTargetOptions(const Function &F) const;
134 /// getMCAsmInfo - Return target specific asm information.
136 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
138 /// getIntrinsicInfo - If intrinsic information is available, return it. If
139 /// not, return null.
141 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
145 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
146 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
148 /// getRelocationModel - Returns the code generation relocation model. The
149 /// choices are static, PIC, and dynamic-no-pic, and target default.
150 Reloc::Model getRelocationModel() const;
152 /// getCodeModel - Returns the code model. The choices are small, kernel,
153 /// medium, large, and target default.
154 CodeModel::Model getCodeModel() const;
156 /// getTLSModel - Returns the TLS model which should be used for the given
158 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
160 /// getOptLevel - Returns the optimization level: None, Less,
161 /// Default, or Aggressive.
162 CodeGenOpt::Level getOptLevel() const;
164 /// \brief Overrides the optimization level.
165 void setOptLevel(CodeGenOpt::Level Level) const;
167 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
169 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
171 /// Returns the default value of asm verbosity.
173 bool getAsmVerbosityDefault() const {
174 return Options.MCOptions.AsmVerbose;
177 bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
179 /// Return true if data objects should be emitted into their own section,
180 /// corresponds to -fdata-sections.
181 bool getDataSections() const {
182 return Options.DataSections;
185 /// Return true if functions should be emitted into their own section,
186 /// corresponding to -ffunction-sections.
187 bool getFunctionSections() const {
188 return Options.FunctionSections;
191 /// \brief Get a \c TargetIRAnalysis appropriate for the target.
193 /// This is used to construct the new pass manager's target IR analysis pass,
194 /// set up appropriately for this target machine. Even the old pass manager
195 /// uses this to answer queries about the IR.
196 virtual TargetIRAnalysis getTargetIRAnalysis();
198 /// CodeGenFileType - These enums are meant to be passed into
199 /// addPassesToEmitFile to indicate what type of file to emit, and returned by
200 /// it to indicate what type of file could actually be made.
201 enum CodeGenFileType {
204 CGFT_Null // Do not emit any output.
207 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
208 /// specified file emitted. Typically this will involve several steps of code
209 /// generation. This method should return true if emission of this file type
210 /// is not supported, or false on success.
211 virtual bool addPassesToEmitFile(PassManagerBase &,
212 formatted_raw_ostream &,
214 bool /*DisableVerify*/ = true,
215 AnalysisID /*StartAfter*/ = nullptr,
216 AnalysisID /*StopAfter*/ = nullptr) {
220 /// addPassesToEmitMC - Add passes to the specified pass manager to get
221 /// machine code emitted with the MCJIT. This method returns true if machine
222 /// code is not supported. It fills the MCContext Ctx pointer which can be
223 /// used to build custom MCStreamer.
225 virtual bool addPassesToEmitMC(PassManagerBase &,
228 bool /*DisableVerify*/ = true) {
232 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
233 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
234 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
237 /// LLVMTargetMachine - This class describes a target machine that is
238 /// implemented with the LLVM target-independent code generator.
240 class LLVMTargetMachine : public TargetMachine {
241 protected: // Can only create subclasses.
242 LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
243 StringRef TargetTriple, StringRef CPU, StringRef FS,
244 TargetOptions Options, Reloc::Model RM, CodeModel::Model CM,
245 CodeGenOpt::Level OL);
249 /// \brief Get a TargetIRAnalysis implementation for the target.
251 /// This analysis will produce a TTI result which uses the common code
252 /// generator to answer queries about the IR.
253 TargetIRAnalysis getTargetIRAnalysis() override;
255 /// createPassConfig - Create a pass configuration object to be used by
256 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
257 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
259 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
260 /// specified file emitted. Typically this will involve several steps of code
262 bool addPassesToEmitFile(PassManagerBase &PM, formatted_raw_ostream &Out,
263 CodeGenFileType FileType, bool DisableVerify = true,
264 AnalysisID StartAfter = nullptr,
265 AnalysisID StopAfter = nullptr) override;
267 /// addPassesToEmitMC - Add passes to the specified pass manager to get
268 /// machine code emitted with the MCJIT. This method returns true if machine
269 /// code is not supported. It fills the MCContext Ctx pointer which can be
270 /// used to build custom MCStreamer.
272 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
273 raw_ostream &OS, bool DisableVerify = true) override;
276 } // End llvm namespace