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 TargetLoweringObjectFile;
53 // The old pass manager infrastructure is hidden in a legacy namespace now.
55 class PassManagerBase;
57 using legacy::PassManagerBase;
59 //===----------------------------------------------------------------------===//
61 /// Primary interface to the complete machine description for the target
62 /// machine. All target-specific information should be accessible through this
66 TargetMachine(const TargetMachine &) = delete;
67 void operator=(const TargetMachine &) = delete;
68 protected: // Can only create subclasses.
69 TargetMachine(const Target &T, StringRef DataLayoutString,
70 StringRef TargetTriple, StringRef CPU, StringRef FS,
71 const TargetOptions &Options);
73 /// The Target that this machine was created for.
74 const Target &TheTarget;
76 /// For ABI type size and alignment.
79 /// Triple string, CPU name, and target feature strings the TargetMachine
80 /// instance is created with.
81 std::string TargetTriple;
82 std::string TargetCPU;
85 /// Low level target information such as relocation model. Non-const to
86 /// allow resetting optimization level per-function.
87 MCCodeGenInfo *CodeGenInfo;
89 /// Contains target specific asm information.
90 const MCAsmInfo *AsmInfo;
92 const MCRegisterInfo *MRI;
93 const MCInstrInfo *MII;
94 const MCSubtargetInfo *STI;
96 unsigned RequireStructuredCFG : 1;
99 mutable TargetOptions Options;
101 virtual ~TargetMachine();
103 const Target &getTarget() const { return TheTarget; }
105 StringRef getTargetTriple() const { return TargetTriple; }
106 StringRef getTargetCPU() const { return TargetCPU; }
107 StringRef getTargetFeatureString() const { return TargetFS; }
109 /// Virtual method implemented by subclasses that returns a reference to that
110 /// target's TargetSubtargetInfo-derived member variable.
111 virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
114 virtual TargetLoweringObjectFile *getObjFileLowering() const {
118 /// This method returns a pointer to the specified type of
119 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
120 /// returned is of the correct type.
121 template <typename STC> const STC &getSubtarget(const Function &F) const {
122 return *static_cast<const STC*>(getSubtargetImpl(F));
125 /// This method returns a pointer to the DataLayout for the target. It should
126 /// 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 /// Return target specific asm information.
135 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
137 const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
138 const MCInstrInfo *getMCInstrInfo() const { return MII; }
139 const MCSubtargetInfo *getMCSubtargetInfo() const { return STI; }
141 /// If intrinsic information is available, return it. If not, return null.
142 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
146 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
147 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
149 /// Returns the code generation relocation model. The choices are static, PIC,
150 /// and dynamic-no-pic, and target default.
151 Reloc::Model getRelocationModel() const;
153 /// Returns the code model. The choices are small, kernel, medium, large, and
155 CodeModel::Model getCodeModel() const;
157 /// Returns the TLS model which should be used for the given global variable.
158 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
160 /// Returns the optimization level: None, Less, Default, or Aggressive.
161 CodeGenOpt::Level getOptLevel() const;
163 /// \brief Overrides the optimization level.
164 void setOptLevel(CodeGenOpt::Level Level) const;
166 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
168 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
170 /// Returns the default value of asm verbosity.
172 bool getAsmVerbosityDefault() const {
173 return Options.MCOptions.AsmVerbose;
176 bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
178 /// Return true if data objects should be emitted into their own section,
179 /// corresponds to -fdata-sections.
180 bool getDataSections() const {
181 return Options.DataSections;
184 /// Return true if functions should be emitted into their own section,
185 /// corresponding to -ffunction-sections.
186 bool getFunctionSections() const {
187 return Options.FunctionSections;
190 /// \brief Get a \c TargetIRAnalysis appropriate for the target.
192 /// This is used to construct the new pass manager's target IR analysis pass,
193 /// set up appropriately for this target machine. Even the old pass manager
194 /// uses this to answer queries about the IR.
195 virtual TargetIRAnalysis getTargetIRAnalysis();
197 /// These enums are meant to be passed into addPassesToEmitFile to indicate
198 /// what type of file to emit, and returned by it to indicate what type of
199 /// file could actually be made.
200 enum CodeGenFileType {
203 CGFT_Null // Do not emit any output.
206 /// Add passes to the specified pass manager to get the specified file
207 /// emitted. Typically this will involve several steps of code generation.
208 /// This method should return true if emission of this file type is not
209 /// supported, or false on success.
210 virtual bool addPassesToEmitFile(PassManagerBase &,
211 formatted_raw_ostream &,
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 &,
227 bool /*DisableVerify*/ = true) {
231 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
232 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
233 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
236 /// This class describes a target machine that is implemented with the LLVM
237 /// target-independent code generator.
239 class LLVMTargetMachine : public TargetMachine {
240 protected: // Can only create subclasses.
241 LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
242 StringRef TargetTriple, StringRef CPU, StringRef FS,
243 TargetOptions Options, Reloc::Model RM, CodeModel::Model CM,
244 CodeGenOpt::Level OL);
248 /// \brief Get a TargetIRAnalysis implementation for the target.
250 /// This analysis will produce a TTI result which uses the common code
251 /// generator to answer queries about the IR.
252 TargetIRAnalysis getTargetIRAnalysis() override;
254 /// Create a pass configuration object to be used by addPassToEmitX methods
255 /// for generating a pipeline of CodeGen passes.
256 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
258 /// Add passes to the specified pass manager to get the specified file
259 /// emitted. Typically this will involve several steps of code generation.
260 bool addPassesToEmitFile(PassManagerBase &PM, formatted_raw_ostream &Out,
261 CodeGenFileType FileType, bool DisableVerify = true,
262 AnalysisID StartAfter = nullptr,
263 AnalysisID StopAfter = nullptr) override;
265 /// Add passes to the specified pass manager to get machine code emitted with
266 /// the MCJIT. This method returns true if machine code is not supported. It
267 /// fills the MCContext Ctx pointer which can be used to build custom
269 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
270 raw_ostream &OS, bool DisableVerify = true) override;
273 } // End llvm namespace