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/Pass.h"
19 #include "llvm/Support/CodeGen.h"
20 #include "llvm/Target/TargetOptions.h"
26 class InstrItineraryData;
35 class TargetLibraryInfo;
36 class TargetFrameLowering;
37 class TargetIntrinsicInfo;
39 class TargetPassConfig;
40 class TargetRegisterInfo;
41 class TargetSelectionDAGInfo;
42 class TargetSubtargetInfo;
43 class formatted_raw_ostream;
45 class TargetLoweringObjectFile;
47 // The old pass manager infrastructure is hidden in a legacy namespace now.
49 class PassManagerBase;
51 using legacy::PassManagerBase;
53 //===----------------------------------------------------------------------===//
55 /// TargetMachine - Primary interface to the complete machine description for
56 /// the target machine. All target-specific information should be accessible
57 /// through this interface.
60 TargetMachine(const TargetMachine &) LLVM_DELETED_FUNCTION;
61 void operator=(const TargetMachine &) LLVM_DELETED_FUNCTION;
62 protected: // Can only create subclasses.
63 TargetMachine(const Target &T, StringRef TargetTriple,
64 StringRef CPU, StringRef FS, const TargetOptions &Options);
66 /// TheTarget - The Target that this machine was created for.
67 const Target &TheTarget;
69 /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
70 /// feature strings the TargetMachine instance is created with.
71 std::string TargetTriple;
72 std::string TargetCPU;
75 /// CodeGenInfo - Low level target information such as relocation model.
76 /// Non-const to allow resetting optimization level per-function.
77 MCCodeGenInfo *CodeGenInfo;
79 /// AsmInfo - Contains target specific asm information.
81 const MCAsmInfo *AsmInfo;
83 unsigned RequireStructuredCFG : 1;
86 mutable TargetOptions Options;
88 virtual ~TargetMachine();
90 const Target &getTarget() const { return TheTarget; }
92 StringRef getTargetTriple() const { return TargetTriple; }
93 StringRef getTargetCPU() const { return TargetCPU; }
94 StringRef getTargetFeatureString() const { return TargetFS; }
96 /// getSubtargetImpl - virtual method implemented by subclasses that returns
97 /// a reference to that target's TargetSubtargetInfo-derived member variable.
98 virtual const TargetSubtargetInfo *getSubtargetImpl() const {
101 virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
102 return getSubtargetImpl();
104 virtual TargetLoweringObjectFile *getObjFileLowering() const {
108 /// getSubtarget - This method returns a pointer to the specified type of
109 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
110 /// returned is of the correct type.
111 template<typename STC> const STC &getSubtarget() const {
112 return *static_cast<const STC*>(getSubtargetImpl());
114 template <typename STC> const STC &getSubtarget(const Function *) const {
115 return *static_cast<const STC*>(getSubtargetImpl());
118 /// getDataLayout - This method returns a pointer to the DataLayout for
119 /// the target. It should be unchanging for every subtarget.
120 virtual const DataLayout *getDataLayout() const {
124 /// \brief Reset the target options based on the function's attributes.
125 // FIXME: Remove TargetOptions that affect per-function code generation
126 // from TargetMachine.
127 void resetTargetOptions(const Function &F) const;
129 /// getMCAsmInfo - Return target specific asm information.
131 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
133 /// getIntrinsicInfo - If intrinsic information is available, return it. If
134 /// not, return null.
136 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
140 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
141 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
143 /// getRelocationModel - Returns the code generation relocation model. The
144 /// choices are static, PIC, and dynamic-no-pic, and target default.
145 Reloc::Model getRelocationModel() const;
147 /// getCodeModel - Returns the code model. The choices are small, kernel,
148 /// medium, large, and target default.
149 CodeModel::Model getCodeModel() const;
151 /// getTLSModel - Returns the TLS model which should be used for the given
153 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
155 /// getOptLevel - Returns the optimization level: None, Less,
156 /// Default, or Aggressive.
157 CodeGenOpt::Level getOptLevel() const;
159 /// \brief Overrides the optimization level.
160 void setOptLevel(CodeGenOpt::Level Level) const;
162 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
164 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
166 /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
168 bool getAsmVerbosityDefault() const ;
170 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
172 void setAsmVerbosityDefault(bool);
174 /// getDataSections - Return true if data objects should be emitted into their
175 /// own section, corresponds to -fdata-sections.
176 bool getDataSections() const;
178 /// getFunctionSections - Return true if functions should be emitted into
179 /// their own section, corresponding to -ffunction-sections.
180 bool getFunctionSections() const;
182 /// setDataSections - Set if the data are emit into separate sections.
183 void setDataSections(bool);
185 /// setFunctionSections - Set if the functions are emit into separate
187 void setFunctionSections(bool);
189 /// \brief Register analysis passes for this target with a pass manager.
190 virtual void addAnalysisPasses(PassManagerBase &) {}
192 /// CodeGenFileType - These enums are meant to be passed into
193 /// addPassesToEmitFile to indicate what type of file to emit, and returned by
194 /// it to indicate what type of file could actually be made.
195 enum CodeGenFileType {
198 CGFT_Null // Do not emit any output.
201 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
202 /// specified file emitted. Typically this will involve several steps of code
203 /// generation. This method should return true if emission of this file type
204 /// is not supported, or false on success.
205 virtual bool addPassesToEmitFile(PassManagerBase &,
206 formatted_raw_ostream &,
208 bool /*DisableVerify*/ = true,
209 AnalysisID /*StartAfter*/ = nullptr,
210 AnalysisID /*StopAfter*/ = nullptr) {
214 /// addPassesToEmitMC - Add passes to the specified pass manager to get
215 /// machine code emitted with the MCJIT. This method returns true if machine
216 /// code is not supported. It fills the MCContext Ctx pointer which can be
217 /// used to build custom MCStreamer.
219 virtual bool addPassesToEmitMC(PassManagerBase &,
222 bool /*DisableVerify*/ = true) {
226 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
227 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
228 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
231 /// LLVMTargetMachine - This class describes a target machine that is
232 /// implemented with the LLVM target-independent code generator.
234 class LLVMTargetMachine : public TargetMachine {
235 protected: // Can only create subclasses.
236 LLVMTargetMachine(const Target &T, StringRef TargetTriple,
237 StringRef CPU, StringRef FS, TargetOptions Options,
238 Reloc::Model RM, CodeModel::Model CM,
239 CodeGenOpt::Level OL);
243 /// \brief Register analysis passes for this target with a pass manager.
245 /// This registers target independent analysis passes.
246 void addAnalysisPasses(PassManagerBase &PM) override;
248 /// createPassConfig - Create a pass configuration object to be used by
249 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
250 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
252 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
253 /// specified file emitted. Typically this will involve several steps of code
255 bool addPassesToEmitFile(PassManagerBase &PM, formatted_raw_ostream &Out,
256 CodeGenFileType FileType, bool DisableVerify = true,
257 AnalysisID StartAfter = nullptr,
258 AnalysisID StopAfter = nullptr) override;
260 /// addPassesToEmitMC - Add passes to the specified pass manager to get
261 /// machine code emitted with the MCJIT. This method returns true if machine
262 /// code is not supported. It fills the MCContext Ctx pointer which can be
263 /// used to build custom MCStreamer.
265 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
266 raw_ostream &OS, bool DisableVerify = true) override;
269 } // End llvm namespace