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/MC/MCCodeGenInfo.h"
18 #include "llvm/ADT/StringRef.h"
24 class InstrItineraryData;
31 class PassManagerBase;
34 class TargetELFWriterInfo;
35 class TargetFrameLowering;
36 class TargetInstrInfo;
37 class TargetIntrinsicInfo;
40 class TargetRegisterInfo;
41 class TargetSelectionDAGInfo;
42 class TargetSubtargetInfo;
43 class formatted_raw_ostream;
46 // Code generation optimization level.
47 namespace CodeGenOpt {
58 None, // No preference
59 Latency, // Scheduling for shortest total latency.
60 RegPressure, // Scheduling for lowest register pressure.
61 Hybrid, // Scheduling for both latency and register pressure.
62 ILP // Scheduling for ILP in low register pressure mode.
66 //===----------------------------------------------------------------------===//
68 /// TargetMachine - Primary interface to the complete machine description for
69 /// the target machine. All target-specific information should be accessible
70 /// through this interface.
73 TargetMachine(const TargetMachine &); // DO NOT IMPLEMENT
74 void operator=(const TargetMachine &); // DO NOT IMPLEMENT
75 protected: // Can only create subclasses.
76 TargetMachine(const Target &T, StringRef TargetTriple,
77 StringRef CPU, StringRef FS);
79 /// getSubtargetImpl - virtual method implemented by subclasses that returns
80 /// a reference to that target's TargetSubtargetInfo-derived member variable.
81 virtual const TargetSubtargetInfo *getSubtargetImpl() const { return 0; }
83 /// TheTarget - The Target that this machine was created for.
84 const Target &TheTarget;
86 /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
87 /// feature strings the TargetMachine instance is created with.
88 std::string TargetTriple;
89 std::string TargetCPU;
92 /// CodeGenInfo - Low level target information such as relocation model.
93 const MCCodeGenInfo *CodeGenInfo;
95 /// AsmInfo - Contains target specific asm information.
97 const MCAsmInfo *AsmInfo;
99 unsigned MCRelaxAll : 1;
100 unsigned MCNoExecStack : 1;
101 unsigned MCSaveTempLabels : 1;
102 unsigned MCUseLoc : 1;
103 unsigned MCUseCFI : 1;
106 virtual ~TargetMachine();
108 const Target &getTarget() const { return TheTarget; }
110 const StringRef getTargetTriple() const { return TargetTriple; }
111 const StringRef getTargetCPU() const { return TargetCPU; }
112 const StringRef getTargetFeatureString() const { return TargetFS; }
114 // 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 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 TargetData *getTargetData() 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 /// getELFWriterInfo - If this target supports an ELF writer, return
161 /// information for it, otherwise return null.
163 virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
165 /// hasMCRelaxAll - Check whether all machine code instructions should be
167 bool hasMCRelaxAll() const { return MCRelaxAll; }
169 /// setMCRelaxAll - Set whether all machine code instructions should be
171 void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
173 /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
174 /// (i.e., not treated as temporary).
175 bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
177 /// setMCSaveTempLabels - Set whether temporary labels will be preserved
178 /// (i.e., not treated as temporary).
179 void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
181 /// hasMCNoExecStack - Check whether an executable stack is not needed.
182 bool hasMCNoExecStack() const { return MCNoExecStack; }
184 /// setMCNoExecStack - Set whether an executabel stack is not needed.
185 void setMCNoExecStack(bool Value) { MCNoExecStack = Value; }
187 /// hasMCUseLoc - Check whether we should use dwarf's .loc directive.
188 bool hasMCUseLoc() const { return MCUseLoc; }
190 /// setMCUseLoc - Set whether all we should use dwarf's .loc directive.
191 void setMCUseLoc(bool Value) { MCUseLoc = Value; }
193 /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives.
194 bool hasMCUseCFI() const { return MCUseCFI; }
196 /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives.
197 void setMCUseCFI(bool Value) { MCUseCFI = Value; }
199 /// getRelocationModel - Returns the code generation relocation model. The
200 /// choices are static, PIC, and dynamic-no-pic, and target default.
201 Reloc::Model getRelocationModel() const;
203 /// getCodeModel - Returns the code model. The choices are small, kernel,
204 /// medium, large, and target default.
205 CodeModel::Model getCodeModel() const;
207 /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
209 static bool getAsmVerbosityDefault();
211 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
213 static void setAsmVerbosityDefault(bool);
215 /// getDataSections - Return true if data objects should be emitted into their
216 /// own section, corresponds to -fdata-sections.
217 static bool getDataSections();
219 /// getFunctionSections - Return true if functions should be emitted into
220 /// their own section, corresponding to -ffunction-sections.
221 static bool getFunctionSections();
223 /// setDataSections - Set if the data are emit into separate sections.
224 static void setDataSections(bool);
226 /// setFunctionSections - Set if the functions are emit into separate
228 static void setFunctionSections(bool);
230 /// CodeGenFileType - These enums are meant to be passed into
231 /// addPassesToEmitFile to indicate what type of file to emit, and returned by
232 /// it to indicate what type of file could actually be made.
233 enum CodeGenFileType {
236 CGFT_Null // Do not emit any output.
239 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
240 /// on this target. User flag overrides.
241 virtual bool getEnableTailMergeDefault() const { return true; }
243 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
244 /// specified file emitted. Typically this will involve several steps of code
245 /// generation. This method should return true if emission of this file type
246 /// is not supported, or false on success.
247 virtual bool addPassesToEmitFile(PassManagerBase &,
248 formatted_raw_ostream &,
255 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
256 /// get machine code emitted. This uses a JITCodeEmitter object to handle
257 /// actually outputting the machine code and resolving things like the address
258 /// of functions. This method returns true if machine code emission is
261 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
268 /// addPassesToEmitMC - Add passes to the specified pass manager to get
269 /// machine code emitted with the MCJIT. This method returns true if machine
270 /// code is not supported. It fills the MCContext Ctx pointer which can be
271 /// used to build custom MCStreamer.
273 virtual bool addPassesToEmitMC(PassManagerBase &,
282 /// LLVMTargetMachine - This class describes a target machine that is
283 /// implemented with the LLVM target-independent code generator.
285 class LLVMTargetMachine : public TargetMachine {
286 protected: // Can only create subclasses.
287 LLVMTargetMachine(const Target &T, StringRef TargetTriple,
288 StringRef CPU, StringRef FS,
289 Reloc::Model RM, CodeModel::Model CM);
292 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
293 /// both emitting to assembly files or machine code output.
295 bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level,
296 bool DisableVerify, MCContext *&OutCtx);
299 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
300 /// specified file emitted. Typically this will involve several steps of code
301 /// generation. If OptLevel is None, the code generator should emit code as
302 /// fast as possible, though the generated code may be less efficient.
303 virtual bool addPassesToEmitFile(PassManagerBase &PM,
304 formatted_raw_ostream &Out,
305 CodeGenFileType FileType,
307 bool DisableVerify = true);
309 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
310 /// get machine code emitted. This uses a JITCodeEmitter object to handle
311 /// actually outputting the machine code and resolving things like the address
312 /// of functions. This method returns true if machine code emission is
315 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
318 bool DisableVerify = true);
320 /// addPassesToEmitMC - Add passes to the specified pass manager to get
321 /// machine code emitted with the MCJIT. This method returns true if machine
322 /// code is not supported. It fills the MCContext Ctx pointer which can be
323 /// used to build custom MCStreamer.
325 virtual bool addPassesToEmitMC(PassManagerBase &PM,
328 CodeGenOpt::Level OptLevel,
329 bool DisableVerify = true);
331 /// Target-Independent Code Generator Pass Configuration Options.
333 /// addPreISelPasses - This method should add any "last minute" LLVM->LLVM
334 /// passes (which are run just before instruction selector).
335 virtual bool addPreISel(PassManagerBase &, CodeGenOpt::Level) {
339 /// addInstSelector - This method should install an instruction selector pass,
340 /// which converts from LLVM code to machine instructions.
341 virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) {
345 /// addPreRegAlloc - This method may be implemented by targets that want to
346 /// run passes immediately before register allocation. This should return
347 /// true if -print-machineinstrs should print after these passes.
348 virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
352 /// addPostRegAlloc - This method may be implemented by targets that want
353 /// to run passes after register allocation but before prolog-epilog
354 /// insertion. This should return true if -print-machineinstrs should print
355 /// after these passes.
356 virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
360 /// addPreSched2 - This method may be implemented by targets that want to
361 /// run passes after prolog-epilog insertion and before the second instruction
362 /// scheduling pass. This should return true if -print-machineinstrs should
363 /// print after these passes.
364 virtual bool addPreSched2(PassManagerBase &, CodeGenOpt::Level) {
368 /// addPreEmitPass - This pass may be implemented by targets that want to run
369 /// passes immediately before machine code is emitted. This should return
370 /// true if -print-machineinstrs should print out the code after the passes.
371 virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
376 /// addCodeEmitter - This pass should be overridden by the target to add a
377 /// code emitter, if supported. If this is not supported, 'true' should be
379 virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
384 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
385 /// on this target. User flag overrides.
386 virtual bool getEnableTailMergeDefault() const { return true; }
389 } // End llvm namespace