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 /// TargetMachine - Primary interface to the complete machine description for
62 /// the target machine. All target-specific information should be accessible
63 /// through this interface.
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 /// TheTarget - The Target that this machine was created for.
74 const Target &TheTarget;
76 /// DataLayout - For ABI type size and alignment.
79 /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
80 /// feature strings the TargetMachine instance is created with.
81 std::string TargetTriple;
82 std::string TargetCPU;
85 /// CodeGenInfo - Low level target information such as relocation model.
86 /// Non-const to allow resetting optimization level per-function.
87 MCCodeGenInfo *CodeGenInfo;
89 /// AsmInfo - Contains target specific asm information.
91 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 /// getSubtargetImpl - virtual method implemented by subclasses that returns
110 /// a reference to that target's TargetSubtargetInfo-derived member variable.
111 virtual const TargetSubtargetInfo *getSubtargetImpl() const {
114 virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
115 return getSubtargetImpl();
117 virtual TargetLoweringObjectFile *getObjFileLowering() const {
121 /// getSubtarget - This method returns a pointer to the specified type of
122 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
123 /// returned is of the correct type.
124 template<typename STC> const STC &getSubtarget() const {
125 return *static_cast<const STC*>(getSubtargetImpl());
127 template <typename STC> const STC &getSubtarget(const Function &) const {
128 return *static_cast<const STC*>(getSubtargetImpl());
131 /// getDataLayout - This method returns a pointer to the DataLayout for
132 /// the target. It should be unchanging for every subtarget.
133 const DataLayout *getDataLayout() const { return &DL; }
135 /// \brief Reset the target options based on the function's attributes.
136 // FIXME: Remove TargetOptions that affect per-function code generation
137 // from TargetMachine.
138 void resetTargetOptions(const Function &F) const;
140 /// getMCAsmInfo - Return target specific asm information.
142 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
143 const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
144 const MCInstrInfo *getMCInstrInfo() const { return MII; }
145 const MCSubtargetInfo *getMCSubtargetInfo() const { return STI; }
147 /// getIntrinsicInfo - If intrinsic information is available, return it. If
148 /// not, return null.
150 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
154 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
155 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
157 /// getRelocationModel - Returns the code generation relocation model. The
158 /// choices are static, PIC, and dynamic-no-pic, and target default.
159 Reloc::Model getRelocationModel() const;
161 /// getCodeModel - Returns the code model. The choices are small, kernel,
162 /// medium, large, and target default.
163 CodeModel::Model getCodeModel() const;
165 /// getTLSModel - Returns the TLS model which should be used for the given
167 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
169 /// getOptLevel - Returns the optimization level: None, Less,
170 /// Default, or Aggressive.
171 CodeGenOpt::Level getOptLevel() const;
173 /// \brief Overrides the optimization level.
174 void setOptLevel(CodeGenOpt::Level Level) const;
176 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
178 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
180 /// Returns the default value of asm verbosity.
182 bool getAsmVerbosityDefault() const {
183 return Options.MCOptions.AsmVerbose;
186 bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
188 /// Return true if data objects should be emitted into their own section,
189 /// corresponds to -fdata-sections.
190 bool getDataSections() const {
191 return Options.DataSections;
194 /// Return true if functions should be emitted into their own section,
195 /// corresponding to -ffunction-sections.
196 bool getFunctionSections() const {
197 return Options.FunctionSections;
200 /// \brief Get a \c TargetIRAnalysis appropriate for the target.
202 /// This is used to construct the new pass manager's target IR analysis pass,
203 /// set up appropriately for this target machine. Even the old pass manager
204 /// uses this to answer queries about the IR.
205 virtual TargetIRAnalysis getTargetIRAnalysis();
207 /// CodeGenFileType - These enums are meant to be passed into
208 /// addPassesToEmitFile to indicate what type of file to emit, and returned by
209 /// it to indicate what type of file could actually be made.
210 enum CodeGenFileType {
213 CGFT_Null // Do not emit any output.
216 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
217 /// specified file emitted. Typically this will involve several steps of code
218 /// generation. This method should return true if emission of this file type
219 /// is not supported, or false on success.
220 virtual bool addPassesToEmitFile(PassManagerBase &,
221 formatted_raw_ostream &,
223 bool /*DisableVerify*/ = true,
224 AnalysisID /*StartAfter*/ = nullptr,
225 AnalysisID /*StopAfter*/ = nullptr) {
229 /// addPassesToEmitMC - Add passes to the specified pass manager to get
230 /// machine code emitted with the MCJIT. This method returns true if machine
231 /// code is not supported. It fills the MCContext Ctx pointer which can be
232 /// used to build custom MCStreamer.
234 virtual bool addPassesToEmitMC(PassManagerBase &,
237 bool /*DisableVerify*/ = true) {
241 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
242 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
243 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
246 /// LLVMTargetMachine - This class describes a target machine that is
247 /// implemented with the LLVM target-independent code generator.
249 class LLVMTargetMachine : public TargetMachine {
250 protected: // Can only create subclasses.
251 LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
252 StringRef TargetTriple, StringRef CPU, StringRef FS,
253 TargetOptions Options, Reloc::Model RM, CodeModel::Model CM,
254 CodeGenOpt::Level OL);
258 /// \brief Get a TargetIRAnalysis implementation for the target.
260 /// This analysis will produce a TTI result which uses the common code
261 /// generator to answer queries about the IR.
262 TargetIRAnalysis getTargetIRAnalysis() override;
264 /// createPassConfig - Create a pass configuration object to be used by
265 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
266 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
268 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
269 /// specified file emitted. Typically this will involve several steps of code
271 bool addPassesToEmitFile(PassManagerBase &PM, formatted_raw_ostream &Out,
272 CodeGenFileType FileType, bool DisableVerify = true,
273 AnalysisID StartAfter = nullptr,
274 AnalysisID StopAfter = nullptr) override;
276 /// addPassesToEmitMC - Add passes to the specified pass manager to get
277 /// machine code emitted with the MCJIT. This method returns true if machine
278 /// code is not supported. It fills the MCContext Ctx pointer which can be
279 /// used to build custom MCStreamer.
281 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
282 raw_ostream &OS, bool DisableVerify = true) override;
285 } // End llvm namespace