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;
36 class TargetLibraryInfo;
37 class TargetFrameLowering;
38 class TargetInstrInfo;
39 class TargetIntrinsicInfo;
42 class TargetPassConfig;
43 class TargetRegisterInfo;
44 class TargetSelectionDAGInfo;
45 class TargetSubtargetInfo;
46 class ScalarTargetTransformInfo;
47 class VectorTargetTransformInfo;
48 class formatted_raw_ostream;
51 // The old pass manager infrastructure is hidden in a legacy namespace now.
53 class PassManagerBase;
55 using legacy::PassManagerBase;
57 //===----------------------------------------------------------------------===//
59 /// TargetMachine - Primary interface to the complete machine description for
60 /// the target machine. All target-specific information should be accessible
61 /// through this interface.
64 TargetMachine(const TargetMachine &) LLVM_DELETED_FUNCTION;
65 void operator=(const TargetMachine &) LLVM_DELETED_FUNCTION;
66 protected: // Can only create subclasses.
67 TargetMachine(const Target &T, StringRef TargetTriple,
68 StringRef CPU, StringRef FS, const TargetOptions &Options);
70 /// TheTarget - The Target that this machine was created for.
71 const Target &TheTarget;
73 /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
74 /// feature strings the TargetMachine instance is created with.
75 std::string TargetTriple;
76 std::string TargetCPU;
79 /// CodeGenInfo - Low level target information such as relocation model.
80 /// Non-const to allow resetting optimization level per-function.
81 MCCodeGenInfo *CodeGenInfo;
83 /// AsmInfo - Contains target specific asm information.
85 const MCAsmInfo *AsmInfo;
87 unsigned RequireStructuredCFG : 1;
90 virtual ~TargetMachine();
92 const Target &getTarget() const { return TheTarget; }
94 const StringRef getTargetTriple() const { return TargetTriple; }
95 const StringRef getTargetCPU() const { return TargetCPU; }
96 const StringRef getTargetFeatureString() const { return TargetFS; }
98 /// getSubtargetImpl - virtual method implemented by subclasses that returns
99 /// a reference to that target's TargetSubtargetInfo-derived member variable.
100 virtual const TargetSubtargetInfo *getSubtargetImpl() const {
103 virtual TargetSubtargetInfo *getSubtargetImpl() {
104 const TargetMachine *TM = this;
105 return const_cast<TargetSubtargetInfo *>(TM->getSubtargetImpl());
108 mutable TargetOptions Options;
110 /// \brief Reset the target options based on the function's attributes.
111 void resetTargetOptions(const MachineFunction *MF) const;
113 // Interfaces to the major aspects of target machine information:
115 // -- Instruction opcode and operand information
116 // -- Pipelines and scheduling information
117 // -- Stack frame information
118 // -- Selection DAG lowering information
120 // N.B. These objects may change during compilation. It's not safe to cache
121 // them between functions.
122 virtual const TargetInstrInfo *getInstrInfo() const { return nullptr; }
123 virtual const TargetFrameLowering *getFrameLowering() const {
126 virtual const TargetLowering *getTargetLowering() const { return nullptr; }
127 virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const {
130 virtual const DataLayout *getDataLayout() const { return nullptr; }
132 /// getMCAsmInfo - Return target specific asm information.
134 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
136 /// getSubtarget - This method returns a pointer to the specified type of
137 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
138 /// returned is of the correct type.
139 template<typename STC> const STC &getSubtarget() const {
140 return *static_cast<const STC*>(getSubtargetImpl());
143 /// getRegisterInfo - If register information is available, return it. If
144 /// not, return null. This is kept separate from RegInfo until RegInfo has
145 /// details of graph coloring register allocation removed from it.
147 virtual const TargetRegisterInfo *getRegisterInfo() const { return nullptr; }
149 /// getIntrinsicInfo - If intrinsic information is available, return it. If
150 /// not, return null.
152 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return nullptr;}
154 /// getJITInfo - If this target supports a JIT, return information for it,
155 /// otherwise return null.
157 virtual TargetJITInfo *getJITInfo() { return nullptr; }
159 /// getInstrItineraryData - Returns instruction itinerary data for the target
160 /// or specific subtarget.
162 virtual const InstrItineraryData *getInstrItineraryData() const {
166 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
167 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
169 /// getRelocationModel - Returns the code generation relocation model. The
170 /// choices are static, PIC, and dynamic-no-pic, and target default.
171 Reloc::Model getRelocationModel() const;
173 /// getCodeModel - Returns the code model. The choices are small, kernel,
174 /// medium, large, and target default.
175 CodeModel::Model getCodeModel() const;
177 /// getTLSModel - Returns the TLS model which should be used for the given
179 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
181 /// getOptLevel - Returns the optimization level: None, Less,
182 /// Default, or Aggressive.
183 CodeGenOpt::Level getOptLevel() const;
185 /// \brief Overrides the optimization level.
186 void setOptLevel(CodeGenOpt::Level Level) const;
188 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
190 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
192 /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
194 bool getAsmVerbosityDefault() const ;
196 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
198 void setAsmVerbosityDefault(bool);
200 /// getDataSections - Return true if data objects should be emitted into their
201 /// own section, corresponds to -fdata-sections.
202 bool getDataSections() const;
204 /// getFunctionSections - Return true if functions should be emitted into
205 /// their own section, corresponding to -ffunction-sections.
206 bool getFunctionSections() const;
208 /// setDataSections - Set if the data are emit into separate sections.
209 void setDataSections(bool);
211 /// setFunctionSections - Set if the functions are emit into separate
213 void setFunctionSections(bool);
215 /// \brief Register analysis passes for this target with a pass manager.
216 virtual void addAnalysisPasses(PassManagerBase &) {}
218 /// CodeGenFileType - These enums are meant to be passed into
219 /// addPassesToEmitFile to indicate what type of file to emit, and returned by
220 /// it to indicate what type of file could actually be made.
221 enum CodeGenFileType {
224 CGFT_Null // Do not emit any output.
227 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
228 /// specified file emitted. Typically this will involve several steps of code
229 /// generation. This method should return true if emission of this file type
230 /// is not supported, or false on success.
231 virtual bool addPassesToEmitFile(PassManagerBase &,
232 formatted_raw_ostream &,
234 bool /*DisableVerify*/ = true,
235 AnalysisID /*StartAfter*/ = nullptr,
236 AnalysisID /*StopAfter*/ = nullptr) {
240 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
241 /// get machine code emitted. This uses a JITCodeEmitter object to handle
242 /// actually outputting the machine code and resolving things like the address
243 /// of functions. This method returns true if machine code emission is
246 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
248 bool /*DisableVerify*/ = true) {
252 /// addPassesToEmitMC - Add passes to the specified pass manager to get
253 /// machine code emitted with the MCJIT. This method returns true if machine
254 /// code is not supported. It fills the MCContext Ctx pointer which can be
255 /// used to build custom MCStreamer.
257 virtual bool addPassesToEmitMC(PassManagerBase &,
260 bool /*DisableVerify*/ = true) {
264 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
265 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
266 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
269 /// LLVMTargetMachine - This class describes a target machine that is
270 /// implemented with the LLVM target-independent code generator.
272 class LLVMTargetMachine : public TargetMachine {
273 protected: // Can only create subclasses.
274 LLVMTargetMachine(const Target &T, StringRef TargetTriple,
275 StringRef CPU, StringRef FS, TargetOptions Options,
276 Reloc::Model RM, CodeModel::Model CM,
277 CodeGenOpt::Level OL);
281 /// \brief Register analysis passes for this target with a pass manager.
283 /// This registers target independent analysis passes.
284 void addAnalysisPasses(PassManagerBase &PM) override;
286 /// createPassConfig - Create a pass configuration object to be used by
287 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
288 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
290 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
291 /// specified file emitted. Typically this will involve several steps of code
293 bool addPassesToEmitFile(PassManagerBase &PM, formatted_raw_ostream &Out,
294 CodeGenFileType FileType, bool DisableVerify = true,
295 AnalysisID StartAfter = nullptr,
296 AnalysisID StopAfter = nullptr) override;
298 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
299 /// get machine code emitted. This uses a JITCodeEmitter object to handle
300 /// actually outputting the machine code and resolving things like the address
301 /// of functions. This method returns true if machine code emission is
304 bool addPassesToEmitMachineCode(PassManagerBase &PM, JITCodeEmitter &MCE,
305 bool DisableVerify = true) override;
307 /// addPassesToEmitMC - Add passes to the specified pass manager to get
308 /// machine code emitted with the MCJIT. This method returns true if machine
309 /// code is not supported. It fills the MCContext Ctx pointer which can be
310 /// used to build custom MCStreamer.
312 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
313 raw_ostream &OS, bool DisableVerify = true) override;
315 /// addCodeEmitter - This pass should be overridden by the target to add a
316 /// code emitter, if supported. If this is not supported, 'true' should be
318 virtual bool addCodeEmitter(PassManagerBase &,
324 } // End llvm namespace