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;
34 class TargetLibraryInfo;
35 class TargetFrameLowering;
36 class TargetInstrInfo;
37 class TargetIntrinsicInfo;
40 class TargetPassConfig;
41 class TargetRegisterInfo;
42 class TargetSelectionDAGInfo;
43 class TargetSubtargetInfo;
44 class ScalarTargetTransformInfo;
45 class VectorTargetTransformInfo;
46 class formatted_raw_ostream;
49 // The old pass manager infrastructure is hidden in a legacy namespace now.
51 class PassManagerBase;
53 using legacy::PassManagerBase;
55 //===----------------------------------------------------------------------===//
57 /// TargetMachine - Primary interface to the complete machine description for
58 /// the target machine. All target-specific information should be accessible
59 /// through this interface.
62 TargetMachine(const TargetMachine &) LLVM_DELETED_FUNCTION;
63 void operator=(const TargetMachine &) LLVM_DELETED_FUNCTION;
64 protected: // Can only create subclasses.
65 TargetMachine(const Target &T, StringRef TargetTriple,
66 StringRef CPU, StringRef FS, const TargetOptions &Options);
68 /// TheTarget - The Target that this machine was created for.
69 const Target &TheTarget;
71 /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
72 /// feature strings the TargetMachine instance is created with.
73 std::string TargetTriple;
74 std::string TargetCPU;
77 /// CodeGenInfo - Low level target information such as relocation model.
78 /// Non-const to allow resetting optimization level per-function.
79 MCCodeGenInfo *CodeGenInfo;
81 /// AsmInfo - Contains target specific asm information.
83 const MCAsmInfo *AsmInfo;
85 unsigned MCRelaxAll : 1;
86 unsigned MCNoExecStack : 1;
87 unsigned MCSaveTempLabels : 1;
88 unsigned MCUseLoc : 1;
89 unsigned MCUseCFI : 1;
90 unsigned MCUseDwarfDirectory : 1;
91 unsigned RequireStructuredCFG : 1;
94 virtual ~TargetMachine();
96 const Target &getTarget() const { return TheTarget; }
98 const StringRef getTargetTriple() const { return TargetTriple; }
99 const StringRef getTargetCPU() const { return TargetCPU; }
100 const StringRef getTargetFeatureString() const { return TargetFS; }
102 /// getSubtargetImpl - virtual method implemented by subclasses that returns
103 /// a reference to that target's TargetSubtargetInfo-derived member variable.
104 virtual const TargetSubtargetInfo *getSubtargetImpl() const { return 0; }
106 mutable TargetOptions Options;
108 /// \brief Reset the target options based on the function's attributes.
109 void resetTargetOptions(const MachineFunction *MF) const;
111 // Interfaces to the major aspects of target machine information:
113 // -- Instruction opcode and operand information
114 // -- Pipelines and scheduling information
115 // -- Stack frame information
116 // -- Selection DAG lowering information
118 // N.B. These objects may change during compilation. It's not safe to cache
119 // them between functions.
120 virtual const TargetInstrInfo *getInstrInfo() const { return 0; }
121 virtual const TargetFrameLowering *getFrameLowering() const { return 0; }
122 virtual const TargetLowering *getTargetLowering() const { return 0; }
123 virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const{ return 0; }
124 virtual const DataLayout *getDataLayout() const { return 0; }
126 /// getMCAsmInfo - Return target specific asm information.
128 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
130 /// getSubtarget - This method returns a pointer to the specified type of
131 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
132 /// returned is of the correct type.
133 template<typename STC> const STC &getSubtarget() const {
134 return *static_cast<const STC*>(getSubtargetImpl());
137 /// getRegisterInfo - If register information is available, return it. If
138 /// not, return null. This is kept separate from RegInfo until RegInfo has
139 /// details of graph coloring register allocation removed from it.
141 virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
143 /// getIntrinsicInfo - If intrinsic information is available, return it. If
144 /// not, return null.
146 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
148 /// getJITInfo - If this target supports a JIT, return information for it,
149 /// otherwise return null.
151 virtual TargetJITInfo *getJITInfo() { return 0; }
153 /// getInstrItineraryData - Returns instruction itinerary data for the target
154 /// or specific subtarget.
156 virtual const InstrItineraryData *getInstrItineraryData() const {
160 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
161 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
163 /// hasMCRelaxAll - Check whether all machine code instructions should be
165 bool hasMCRelaxAll() const { return MCRelaxAll; }
167 /// setMCRelaxAll - Set whether all machine code instructions should be
169 void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
171 /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
172 /// (i.e., not treated as temporary).
173 bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
175 /// setMCSaveTempLabels - Set whether temporary labels will be preserved
176 /// (i.e., not treated as temporary).
177 void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
179 /// hasMCNoExecStack - Check whether an executable stack is not needed.
180 bool hasMCNoExecStack() const { return MCNoExecStack; }
182 /// setMCNoExecStack - Set whether an executabel stack is not needed.
183 void setMCNoExecStack(bool Value) { MCNoExecStack = Value; }
185 /// hasMCUseLoc - Check whether we should use dwarf's .loc directive.
186 bool hasMCUseLoc() const { return MCUseLoc; }
188 /// setMCUseLoc - Set whether all we should use dwarf's .loc directive.
189 void setMCUseLoc(bool Value) { MCUseLoc = Value; }
191 /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives.
192 bool hasMCUseCFI() const { return MCUseCFI; }
194 /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives.
195 void setMCUseCFI(bool Value) { MCUseCFI = Value; }
197 /// hasMCUseDwarfDirectory - Check whether we should use .file directives with
198 /// explicit directories.
199 bool hasMCUseDwarfDirectory() const { return MCUseDwarfDirectory; }
201 /// setMCUseDwarfDirectory - Set whether all we should use .file directives
202 /// with explicit directories.
203 void setMCUseDwarfDirectory(bool Value) { MCUseDwarfDirectory = Value; }
205 /// getRelocationModel - Returns the code generation relocation model. The
206 /// choices are static, PIC, and dynamic-no-pic, and target default.
207 Reloc::Model getRelocationModel() const;
209 /// getCodeModel - Returns the code model. The choices are small, kernel,
210 /// medium, large, and target default.
211 CodeModel::Model getCodeModel() const;
213 /// getTLSModel - Returns the TLS model which should be used for the given
215 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
217 /// getOptLevel - Returns the optimization level: None, Less,
218 /// Default, or Aggressive.
219 CodeGenOpt::Level getOptLevel() const;
221 /// \brief Overrides the optimization level.
222 void setOptLevel(CodeGenOpt::Level Level) const;
224 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
226 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
228 /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
230 static bool getAsmVerbosityDefault();
232 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
234 static void setAsmVerbosityDefault(bool);
236 /// getDataSections - Return true if data objects should be emitted into their
237 /// own section, corresponds to -fdata-sections.
238 static bool getDataSections();
240 /// getFunctionSections - Return true if functions should be emitted into
241 /// their own section, corresponding to -ffunction-sections.
242 static bool getFunctionSections();
244 /// setDataSections - Set if the data are emit into separate sections.
245 static void setDataSections(bool);
247 /// setFunctionSections - Set if the functions are emit into separate
249 static void setFunctionSections(bool);
251 /// \brief Register analysis passes for this target with a pass manager.
252 virtual void addAnalysisPasses(PassManagerBase &) {}
254 /// CodeGenFileType - These enums are meant to be passed into
255 /// addPassesToEmitFile to indicate what type of file to emit, and returned by
256 /// it to indicate what type of file could actually be made.
257 enum CodeGenFileType {
260 CGFT_Null // Do not emit any output.
263 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
264 /// specified file emitted. Typically this will involve several steps of code
265 /// generation. This method should return true if emission of this file type
266 /// is not supported, or false on success.
267 virtual bool addPassesToEmitFile(PassManagerBase &,
268 formatted_raw_ostream &,
270 bool /*DisableVerify*/ = true,
271 AnalysisID /*StartAfter*/ = 0,
272 AnalysisID /*StopAfter*/ = 0) {
276 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
277 /// get machine code emitted. This uses a JITCodeEmitter object to handle
278 /// actually outputting the machine code and resolving things like the address
279 /// of functions. This method returns true if machine code emission is
282 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
284 bool /*DisableVerify*/ = true) {
288 /// addPassesToEmitMC - Add passes to the specified pass manager to get
289 /// machine code emitted with the MCJIT. This method returns true if machine
290 /// code is not supported. It fills the MCContext Ctx pointer which can be
291 /// used to build custom MCStreamer.
293 virtual bool addPassesToEmitMC(PassManagerBase &,
296 bool /*DisableVerify*/ = true) {
301 /// LLVMTargetMachine - This class describes a target machine that is
302 /// implemented with the LLVM target-independent code generator.
304 class LLVMTargetMachine : public TargetMachine {
305 protected: // Can only create subclasses.
306 LLVMTargetMachine(const Target &T, StringRef TargetTriple,
307 StringRef CPU, StringRef FS, TargetOptions Options,
308 Reloc::Model RM, CodeModel::Model CM,
309 CodeGenOpt::Level OL);
313 /// \brief Register analysis passes for this target with a pass manager.
315 /// This registers target independent analysis passes.
316 virtual void addAnalysisPasses(PassManagerBase &PM);
318 /// createPassConfig - Create a pass configuration object to be used by
319 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
320 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
322 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
323 /// specified file emitted. Typically this will involve several steps of code
325 virtual bool addPassesToEmitFile(PassManagerBase &PM,
326 formatted_raw_ostream &Out,
327 CodeGenFileType FileType,
328 bool DisableVerify = true,
329 AnalysisID StartAfter = 0,
330 AnalysisID StopAfter = 0);
332 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
333 /// get machine code emitted. This uses a JITCodeEmitter object to handle
334 /// actually outputting the machine code and resolving things like the address
335 /// of functions. This method returns true if machine code emission is
338 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
340 bool DisableVerify = true);
342 /// addPassesToEmitMC - Add passes to the specified pass manager to get
343 /// machine code emitted with the MCJIT. This method returns true if machine
344 /// code is not supported. It fills the MCContext Ctx pointer which can be
345 /// used to build custom MCStreamer.
347 virtual bool addPassesToEmitMC(PassManagerBase &PM,
350 bool DisableVerify = true);
352 /// addCodeEmitter - This pass should be overridden by the target to add a
353 /// code emitter, if supported. If this is not supported, 'true' should be
355 virtual bool addCodeEmitter(PassManagerBase &,
361 } // End llvm namespace