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;
35 class MCSubtargetInfo;
39 class TargetLibraryInfo;
40 class TargetFrameLowering;
41 class TargetIRAnalysis;
42 class TargetIntrinsicInfo;
44 class TargetPassConfig;
45 class TargetRegisterInfo;
46 class TargetSelectionDAGInfo;
47 class TargetSubtargetInfo;
48 class TargetTransformInfo;
49 class formatted_raw_ostream;
51 class raw_pwrite_stream;
52 class TargetLoweringObjectFile;
54 // The old pass manager infrastructure is hidden in a legacy namespace now.
56 class PassManagerBase;
58 using legacy::PassManagerBase;
60 //===----------------------------------------------------------------------===//
62 /// Primary interface to the complete machine description for the target
63 /// machine. All target-specific information should be accessible through this
67 TargetMachine(const TargetMachine &) = delete;
68 void operator=(const TargetMachine &) = delete;
69 protected: // Can only create subclasses.
70 TargetMachine(const Target &T, StringRef DataLayoutString,
71 StringRef TargetTriple, StringRef CPU, StringRef FS,
72 const TargetOptions &Options);
74 /// The Target that this machine was created for.
75 const Target &TheTarget;
77 /// For ABI type size and alignment.
80 /// Triple string, CPU name, and target feature strings the TargetMachine
81 /// instance is created with.
82 std::string TargetTriple;
83 std::string TargetCPU;
86 /// Low level target information such as relocation model. Non-const to
87 /// allow resetting optimization level per-function.
88 MCCodeGenInfo *CodeGenInfo;
90 /// Contains target specific asm information.
91 const MCAsmInfo *AsmInfo;
93 const MCRegisterInfo *MRI;
94 const MCInstrInfo *MII;
95 const MCSubtargetInfo *STI;
97 unsigned RequireStructuredCFG : 1;
100 mutable TargetOptions Options;
102 virtual ~TargetMachine();
104 const Target &getTarget() const { return TheTarget; }
106 StringRef getTargetTriple() const { return TargetTriple; }
107 StringRef getTargetCPU() const { return TargetCPU; }
108 StringRef getTargetFeatureString() const { return TargetFS; }
110 /// Virtual method implemented by subclasses that returns a reference to that
111 /// target's TargetSubtargetInfo-derived member variable.
112 virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
115 virtual TargetLoweringObjectFile *getObjFileLowering() const {
119 /// 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 Function &F) const {
123 return *static_cast<const STC*>(getSubtargetImpl(F));
126 /// This method returns a pointer to the DataLayout for the target. It should
127 /// be unchanging for every subtarget.
128 const DataLayout *getDataLayout() const { return &DL; }
130 /// \brief Reset the target options based on the function's attributes.
131 // FIXME: Remove TargetOptions that affect per-function code generation
132 // from TargetMachine.
133 void resetTargetOptions(const Function &F) const;
135 /// Return target specific asm information.
136 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
138 const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
139 const MCInstrInfo *getMCInstrInfo() const { return MII; }
140 const MCSubtargetInfo *getMCSubtargetInfo() const { return STI; }
142 /// If intrinsic information is available, return it. If not, return null.
143 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
147 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
148 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
150 /// Returns the code generation relocation model. The choices are static, PIC,
151 /// and dynamic-no-pic, and target default.
152 Reloc::Model getRelocationModel() const;
154 /// Returns the code model. The choices are small, kernel, medium, large, and
156 CodeModel::Model getCodeModel() const;
158 /// Returns the TLS model which should be used for the given global variable.
159 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
161 /// Returns the optimization level: None, Less, 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 /// These enums are meant to be passed into addPassesToEmitFile to indicate
199 /// what type of file to emit, and returned by it to indicate what type of
200 /// file could actually be made.
201 enum CodeGenFileType {
204 CGFT_Null // Do not emit any output.
207 /// Add passes to the specified pass manager to get the specified file
208 /// emitted. Typically this will involve several steps of code generation.
209 /// This method should return true if emission of this file type is not
210 /// supported, or false on success.
211 virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &,
213 bool /*DisableVerify*/ = true,
214 AnalysisID /*StartAfter*/ = nullptr,
215 AnalysisID /*StopAfter*/ = nullptr) {
219 /// Add passes to the specified pass manager to get machine code emitted with
220 /// the MCJIT. This method returns true if machine code is not supported. It
221 /// fills the MCContext Ctx pointer which can be used to build custom
224 virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&,
226 bool /*DisableVerify*/ = true) {
230 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
231 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
232 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
235 /// This class describes a target machine that is implemented with the LLVM
236 /// target-independent code generator.
238 class LLVMTargetMachine : public TargetMachine {
239 protected: // Can only create subclasses.
240 LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
241 StringRef TargetTriple, StringRef CPU, StringRef FS,
242 TargetOptions Options, Reloc::Model RM, CodeModel::Model CM,
243 CodeGenOpt::Level OL);
247 /// \brief Get a TargetIRAnalysis implementation for the target.
249 /// This analysis will produce a TTI result which uses the common code
250 /// generator to answer queries about the IR.
251 TargetIRAnalysis getTargetIRAnalysis() override;
253 /// Create a pass configuration object to be used by addPassToEmitX methods
254 /// for generating a pipeline of CodeGen passes.
255 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
257 /// Add passes to the specified pass manager to get the specified file
258 /// emitted. Typically this will involve several steps of code generation.
259 bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
260 CodeGenFileType FileType, bool DisableVerify = true,
261 AnalysisID StartAfter = nullptr,
262 AnalysisID StopAfter = nullptr) override;
264 /// Add passes to the specified pass manager to get machine code emitted with
265 /// the MCJIT. This method returns true if machine code is not supported. It
266 /// fills the MCContext Ctx pointer which can be used to build custom
268 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
269 raw_pwrite_stream &OS,
270 bool DisableVerify = true) override;
273 } // End llvm namespace