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;
32 class PassManagerBase;
35 class TargetLibraryInfo;
36 class TargetFrameLowering;
37 class TargetInstrInfo;
38 class TargetIntrinsicInfo;
41 class TargetPassConfig;
42 class TargetRegisterInfo;
43 class TargetSelectionDAGInfo;
44 class TargetSubtargetInfo;
45 class ScalarTargetTransformInfo;
46 class VectorTargetTransformInfo;
47 class formatted_raw_ostream;
50 //===----------------------------------------------------------------------===//
52 /// TargetMachine - Primary interface to the complete machine description for
53 /// the target machine. All target-specific information should be accessible
54 /// through this interface.
57 TargetMachine(const TargetMachine &) LLVM_DELETED_FUNCTION;
58 void operator=(const TargetMachine &) LLVM_DELETED_FUNCTION;
59 protected: // Can only create subclasses.
60 TargetMachine(const Target &T, StringRef TargetTriple,
61 StringRef CPU, StringRef FS, const TargetOptions &Options);
63 /// TheTarget - The Target that this machine was created for.
64 const Target &TheTarget;
66 /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
67 /// feature strings the TargetMachine instance is created with.
68 std::string TargetTriple;
69 std::string TargetCPU;
72 /// CodeGenInfo - Low level target information such as relocation model.
73 const MCCodeGenInfo *CodeGenInfo;
75 /// AsmInfo - Contains target specific asm information.
77 const MCAsmInfo *AsmInfo;
79 unsigned MCRelaxAll : 1;
80 unsigned MCNoExecStack : 1;
81 unsigned MCSaveTempLabels : 1;
82 unsigned MCUseLoc : 1;
83 unsigned MCUseCFI : 1;
84 unsigned MCUseDwarfDirectory : 1;
87 virtual ~TargetMachine();
89 const Target &getTarget() const { return TheTarget; }
91 const StringRef getTargetTriple() const { return TargetTriple; }
92 const StringRef getTargetCPU() const { return TargetCPU; }
93 const StringRef getTargetFeatureString() const { return TargetFS; }
95 /// getSubtargetImpl - virtual method implemented by subclasses that returns
96 /// a reference to that target's TargetSubtargetInfo-derived member variable.
97 virtual const TargetSubtargetInfo *getSubtargetImpl() const { return 0; }
99 mutable TargetOptions Options;
101 /// \brief Reset the target options based on the function's attributes.
102 void resetTargetOptions(const MachineFunction *MF) const;
104 // Interfaces to the major aspects of target machine information:
106 // -- Instruction opcode and operand information
107 // -- Pipelines and scheduling information
108 // -- Stack frame information
109 // -- Selection DAG lowering information
111 // N.B. These objects may change during compilation. It's not safe to cache
112 // them between functions.
113 virtual const TargetInstrInfo *getInstrInfo() const { return 0; }
114 virtual const TargetFrameLowering *getFrameLowering() const { return 0; }
115 virtual const TargetLowering *getTargetLowering() const { return 0; }
116 virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const{ return 0; }
117 virtual const DataLayout *getDataLayout() const { return 0; }
119 /// getMCAsmInfo - Return target specific asm information.
121 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
123 /// getSubtarget - This method returns a pointer to the specified type of
124 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
125 /// returned is of the correct type.
126 template<typename STC> const STC &getSubtarget() const {
127 return *static_cast<const STC*>(getSubtargetImpl());
130 /// getRegisterInfo - If register information is available, return it. If
131 /// not, return null. This is kept separate from RegInfo until RegInfo has
132 /// details of graph coloring register allocation removed from it.
134 virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
136 /// getIntrinsicInfo - If intrinsic information is available, return it. If
137 /// not, return null.
139 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
141 /// getJITInfo - If this target supports a JIT, return information for it,
142 /// otherwise return null.
144 virtual TargetJITInfo *getJITInfo() { return 0; }
146 /// getInstrItineraryData - Returns instruction itinerary data for the target
147 /// or specific subtarget.
149 virtual const InstrItineraryData *getInstrItineraryData() const {
153 /// hasMCRelaxAll - Check whether all machine code instructions should be
155 bool hasMCRelaxAll() const { return MCRelaxAll; }
157 /// setMCRelaxAll - Set whether all machine code instructions should be
159 void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
161 /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
162 /// (i.e., not treated as temporary).
163 bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
165 /// setMCSaveTempLabels - Set whether temporary labels will be preserved
166 /// (i.e., not treated as temporary).
167 void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
169 /// hasMCNoExecStack - Check whether an executable stack is not needed.
170 bool hasMCNoExecStack() const { return MCNoExecStack; }
172 /// setMCNoExecStack - Set whether an executabel stack is not needed.
173 void setMCNoExecStack(bool Value) { MCNoExecStack = Value; }
175 /// hasMCUseLoc - Check whether we should use dwarf's .loc directive.
176 bool hasMCUseLoc() const { return MCUseLoc; }
178 /// setMCUseLoc - Set whether all we should use dwarf's .loc directive.
179 void setMCUseLoc(bool Value) { MCUseLoc = Value; }
181 /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives.
182 bool hasMCUseCFI() const { return MCUseCFI; }
184 /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives.
185 void setMCUseCFI(bool Value) { MCUseCFI = Value; }
187 /// hasMCUseDwarfDirectory - Check whether we should use .file directives with
188 /// explicit directories.
189 bool hasMCUseDwarfDirectory() const { return MCUseDwarfDirectory; }
191 /// setMCUseDwarfDirectory - Set whether all we should use .file directives
192 /// with explicit directories.
193 void setMCUseDwarfDirectory(bool Value) { MCUseDwarfDirectory = Value; }
195 /// getRelocationModel - Returns the code generation relocation model. The
196 /// choices are static, PIC, and dynamic-no-pic, and target default.
197 Reloc::Model getRelocationModel() const;
199 /// getCodeModel - Returns the code model. The choices are small, kernel,
200 /// medium, large, and target default.
201 CodeModel::Model getCodeModel() const;
203 /// getTLSModel - Returns the TLS model which should be used for the given
205 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
207 /// getOptLevel - Returns the optimization level: None, Less,
208 /// Default, or Aggressive.
209 CodeGenOpt::Level getOptLevel() const;
211 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
213 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
215 /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
217 static bool getAsmVerbosityDefault();
219 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
221 static void setAsmVerbosityDefault(bool);
223 /// getDataSections - Return true if data objects should be emitted into their
224 /// own section, corresponds to -fdata-sections.
225 static bool getDataSections();
227 /// getFunctionSections - Return true if functions should be emitted into
228 /// their own section, corresponding to -ffunction-sections.
229 static bool getFunctionSections();
231 /// setDataSections - Set if the data are emit into separate sections.
232 static void setDataSections(bool);
234 /// setFunctionSections - Set if the functions are emit into separate
236 static void setFunctionSections(bool);
238 /// \brief Register analysis passes for this target with a pass manager.
239 virtual void addAnalysisPasses(PassManagerBase &) {}
241 /// CodeGenFileType - These enums are meant to be passed into
242 /// addPassesToEmitFile to indicate what type of file to emit, and returned by
243 /// it to indicate what type of file could actually be made.
244 enum CodeGenFileType {
247 CGFT_Null // Do not emit any output.
250 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
251 /// specified file emitted. Typically this will involve several steps of code
252 /// generation. This method should return true if emission of this file type
253 /// is not supported, or false on success.
254 virtual bool addPassesToEmitFile(PassManagerBase &,
255 formatted_raw_ostream &,
257 bool /*DisableVerify*/ = true,
258 AnalysisID /*StartAfter*/ = 0,
259 AnalysisID /*StopAfter*/ = 0) {
263 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
264 /// get machine code emitted. This uses a JITCodeEmitter object to handle
265 /// actually outputting the machine code and resolving things like the address
266 /// of functions. This method returns true if machine code emission is
269 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
271 bool /*DisableVerify*/ = true) {
275 /// addPassesToEmitMC - Add passes to the specified pass manager to get
276 /// machine code emitted with the MCJIT. This method returns true if machine
277 /// code is not supported. It fills the MCContext Ctx pointer which can be
278 /// used to build custom MCStreamer.
280 virtual bool addPassesToEmitMC(PassManagerBase &,
283 bool /*DisableVerify*/ = true) {
288 /// LLVMTargetMachine - This class describes a target machine that is
289 /// implemented with the LLVM target-independent code generator.
291 class LLVMTargetMachine : public TargetMachine {
292 protected: // Can only create subclasses.
293 LLVMTargetMachine(const Target &T, StringRef TargetTriple,
294 StringRef CPU, StringRef FS, TargetOptions Options,
295 Reloc::Model RM, CodeModel::Model CM,
296 CodeGenOpt::Level OL);
300 /// \brief Register analysis passes for this target with a pass manager.
302 /// This registers target independent analysis passes.
303 virtual void addAnalysisPasses(PassManagerBase &PM);
305 /// createPassConfig - Create a pass configuration object to be used by
306 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
307 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
309 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
310 /// specified file emitted. Typically this will involve several steps of code
312 virtual bool addPassesToEmitFile(PassManagerBase &PM,
313 formatted_raw_ostream &Out,
314 CodeGenFileType FileType,
315 bool DisableVerify = true,
316 AnalysisID StartAfter = 0,
317 AnalysisID StopAfter = 0);
319 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
320 /// get machine code emitted. This uses a JITCodeEmitter object to handle
321 /// actually outputting the machine code and resolving things like the address
322 /// of functions. This method returns true if machine code emission is
325 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
327 bool DisableVerify = true);
329 /// addPassesToEmitMC - Add passes to the specified pass manager to get
330 /// machine code emitted with the MCJIT. This method returns true if machine
331 /// code is not supported. It fills the MCContext Ctx pointer which can be
332 /// used to build custom MCStreamer.
334 virtual bool addPassesToEmitMC(PassManagerBase &PM,
337 bool DisableVerify = true);
339 /// addCodeEmitter - This pass should be overridden by the target to add a
340 /// code emitter, if supported. If this is not supported, 'true' should be
342 virtual bool addCodeEmitter(PassManagerBase &,
348 } // End llvm namespace