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;
93 virtual ~TargetMachine();
95 const Target &getTarget() const { return TheTarget; }
97 const StringRef getTargetTriple() const { return TargetTriple; }
98 const StringRef getTargetCPU() const { return TargetCPU; }
99 const StringRef getTargetFeatureString() const { return TargetFS; }
101 /// getSubtargetImpl - virtual method implemented by subclasses that returns
102 /// a reference to that target's TargetSubtargetInfo-derived member variable.
103 virtual const TargetSubtargetInfo *getSubtargetImpl() const { return 0; }
105 mutable TargetOptions Options;
107 /// \brief Reset the target options based on the function's attributes.
108 void resetTargetOptions(const MachineFunction *MF) const;
110 // Interfaces to the major aspects of target machine information:
112 // -- Instruction opcode and operand information
113 // -- Pipelines and scheduling information
114 // -- Stack frame information
115 // -- Selection DAG lowering information
117 // N.B. These objects may change during compilation. It's not safe to cache
118 // them between functions.
119 virtual const TargetInstrInfo *getInstrInfo() const { return 0; }
120 virtual const TargetFrameLowering *getFrameLowering() const { return 0; }
121 virtual const TargetLowering *getTargetLowering() const { return 0; }
122 virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const{ return 0; }
123 virtual const DataLayout *getDataLayout() const { return 0; }
125 /// getMCAsmInfo - Return target specific asm information.
127 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
129 /// getSubtarget - This method returns a pointer to the specified type of
130 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
131 /// returned is of the correct type.
132 template<typename STC> const STC &getSubtarget() const {
133 return *static_cast<const STC*>(getSubtargetImpl());
136 /// getRegisterInfo - If register information is available, return it. If
137 /// not, return null. This is kept separate from RegInfo until RegInfo has
138 /// details of graph coloring register allocation removed from it.
140 virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
142 /// getIntrinsicInfo - If intrinsic information is available, return it. If
143 /// not, return null.
145 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
147 /// getJITInfo - If this target supports a JIT, return information for it,
148 /// otherwise return null.
150 virtual TargetJITInfo *getJITInfo() { return 0; }
152 /// getInstrItineraryData - Returns instruction itinerary data for the target
153 /// or specific subtarget.
155 virtual const InstrItineraryData *getInstrItineraryData() const {
159 /// hasMCRelaxAll - Check whether all machine code instructions should be
161 bool hasMCRelaxAll() const { return MCRelaxAll; }
163 /// setMCRelaxAll - Set whether all machine code instructions should be
165 void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
167 /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
168 /// (i.e., not treated as temporary).
169 bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
171 /// setMCSaveTempLabels - Set whether temporary labels will be preserved
172 /// (i.e., not treated as temporary).
173 void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
175 /// hasMCNoExecStack - Check whether an executable stack is not needed.
176 bool hasMCNoExecStack() const { return MCNoExecStack; }
178 /// setMCNoExecStack - Set whether an executabel stack is not needed.
179 void setMCNoExecStack(bool Value) { MCNoExecStack = Value; }
181 /// hasMCUseLoc - Check whether we should use dwarf's .loc directive.
182 bool hasMCUseLoc() const { return MCUseLoc; }
184 /// setMCUseLoc - Set whether all we should use dwarf's .loc directive.
185 void setMCUseLoc(bool Value) { MCUseLoc = Value; }
187 /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives.
188 bool hasMCUseCFI() const { return MCUseCFI; }
190 /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives.
191 void setMCUseCFI(bool Value) { MCUseCFI = Value; }
193 /// hasMCUseDwarfDirectory - Check whether we should use .file directives with
194 /// explicit directories.
195 bool hasMCUseDwarfDirectory() const { return MCUseDwarfDirectory; }
197 /// setMCUseDwarfDirectory - Set whether all we should use .file directives
198 /// with explicit directories.
199 void setMCUseDwarfDirectory(bool Value) { MCUseDwarfDirectory = Value; }
201 /// getRelocationModel - Returns the code generation relocation model. The
202 /// choices are static, PIC, and dynamic-no-pic, and target default.
203 Reloc::Model getRelocationModel() const;
205 /// getCodeModel - Returns the code model. The choices are small, kernel,
206 /// medium, large, and target default.
207 CodeModel::Model getCodeModel() const;
209 /// getTLSModel - Returns the TLS model which should be used for the given
211 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
213 /// getOptLevel - Returns the optimization level: None, Less,
214 /// Default, or Aggressive.
215 CodeGenOpt::Level getOptLevel() const;
217 /// \brief Overrides the optimization level.
218 void setOptLevel(CodeGenOpt::Level Level) const;
220 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
222 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
224 /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
226 static bool getAsmVerbosityDefault();
228 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
230 static void setAsmVerbosityDefault(bool);
232 /// getDataSections - Return true if data objects should be emitted into their
233 /// own section, corresponds to -fdata-sections.
234 static bool getDataSections();
236 /// getFunctionSections - Return true if functions should be emitted into
237 /// their own section, corresponding to -ffunction-sections.
238 static bool getFunctionSections();
240 /// setDataSections - Set if the data are emit into separate sections.
241 static void setDataSections(bool);
243 /// setFunctionSections - Set if the functions are emit into separate
245 static void setFunctionSections(bool);
247 /// \brief Register analysis passes for this target with a pass manager.
248 virtual void addAnalysisPasses(PassManagerBase &) {}
250 /// CodeGenFileType - These enums are meant to be passed into
251 /// addPassesToEmitFile to indicate what type of file to emit, and returned by
252 /// it to indicate what type of file could actually be made.
253 enum CodeGenFileType {
256 CGFT_Null // Do not emit any output.
259 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
260 /// specified file emitted. Typically this will involve several steps of code
261 /// generation. This method should return true if emission of this file type
262 /// is not supported, or false on success.
263 virtual bool addPassesToEmitFile(PassManagerBase &,
264 formatted_raw_ostream &,
266 bool /*DisableVerify*/ = true,
267 AnalysisID /*StartAfter*/ = 0,
268 AnalysisID /*StopAfter*/ = 0) {
272 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
273 /// get machine code emitted. This uses a JITCodeEmitter object to handle
274 /// actually outputting the machine code and resolving things like the address
275 /// of functions. This method returns true if machine code emission is
278 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
280 bool /*DisableVerify*/ = true) {
284 /// addPassesToEmitMC - Add passes to the specified pass manager to get
285 /// machine code emitted with the MCJIT. This method returns true if machine
286 /// code is not supported. It fills the MCContext Ctx pointer which can be
287 /// used to build custom MCStreamer.
289 virtual bool addPassesToEmitMC(PassManagerBase &,
292 bool /*DisableVerify*/ = true) {
297 /// LLVMTargetMachine - This class describes a target machine that is
298 /// implemented with the LLVM target-independent code generator.
300 class LLVMTargetMachine : public TargetMachine {
301 protected: // Can only create subclasses.
302 LLVMTargetMachine(const Target &T, StringRef TargetTriple,
303 StringRef CPU, StringRef FS, TargetOptions Options,
304 Reloc::Model RM, CodeModel::Model CM,
305 CodeGenOpt::Level OL);
309 /// \brief Register analysis passes for this target with a pass manager.
311 /// This registers target independent analysis passes.
312 virtual void addAnalysisPasses(PassManagerBase &PM);
314 /// createPassConfig - Create a pass configuration object to be used by
315 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
316 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
318 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
319 /// specified file emitted. Typically this will involve several steps of code
321 virtual bool addPassesToEmitFile(PassManagerBase &PM,
322 formatted_raw_ostream &Out,
323 CodeGenFileType FileType,
324 bool DisableVerify = true,
325 AnalysisID StartAfter = 0,
326 AnalysisID StopAfter = 0);
328 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
329 /// get machine code emitted. This uses a JITCodeEmitter object to handle
330 /// actually outputting the machine code and resolving things like the address
331 /// of functions. This method returns true if machine code emission is
334 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
336 bool DisableVerify = true);
338 /// addPassesToEmitMC - Add passes to the specified pass manager to get
339 /// machine code emitted with the MCJIT. This method returns true if machine
340 /// code is not supported. It fills the MCContext Ctx pointer which can be
341 /// used to build custom MCStreamer.
343 virtual bool addPassesToEmitMC(PassManagerBase &PM,
346 bool DisableVerify = true);
348 /// addCodeEmitter - This pass should be overridden by the target to add a
349 /// code emitter, if supported. If this is not supported, 'true' should be
351 virtual bool addCodeEmitter(PassManagerBase &,
357 } // End llvm namespace