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/Target/TargetInstrItineraries.h"
24 class TargetSubtarget;
25 class TargetInstrInfo;
26 class TargetIntrinsicInfo;
29 class TargetFrameInfo;
30 class MachineCodeEmitter;
32 class TargetRegisterInfo;
34 class PassManagerBase;
37 class TargetMachOWriterInfo;
38 class TargetELFWriterInfo;
41 // Relocation model types.
46 PIC_, // Cannot be named PIC due to collision with -DPIC
72 // Code generation optimization level.
73 namespace CodeGenOpt {
81 //===----------------------------------------------------------------------===//
83 /// TargetMachine - Primary interface to the complete machine description for
84 /// the target machine. All target-specific information should be accessible
85 /// through this interface.
88 TargetMachine(const TargetMachine &); // DO NOT IMPLEMENT
89 void operator=(const TargetMachine &); // DO NOT IMPLEMENT
90 protected: // Can only create subclasses.
91 TargetMachine() : AsmInfo(0) { }
93 /// getSubtargetImpl - virtual method implemented by subclasses that returns
94 /// a reference to that target's TargetSubtarget-derived member variable.
95 virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
97 /// AsmInfo - Contains target specific asm information.
99 mutable const TargetAsmInfo *AsmInfo;
101 /// createTargetAsmInfo - Create a new instance of target specific asm
103 virtual const TargetAsmInfo *createTargetAsmInfo() const { return 0; }
106 virtual ~TargetMachine();
108 /// getModuleMatchQuality - This static method should be implemented by
109 /// targets to indicate how closely they match the specified module. This is
110 /// used by the LLC tool to determine which target to use when an explicit
111 /// -march option is not specified. If a target returns zero, it will never
112 /// be chosen without an explicit -march option.
113 static unsigned getModuleMatchQuality(const Module &) { return 0; }
115 /// getJITMatchQuality - This static method should be implemented by targets
116 /// that provide JIT capabilities to indicate how suitable they are for
117 /// execution on the current host. If a value of 0 is returned, the target
118 /// will not be used unless an explicit -march option is used.
119 static unsigned getJITMatchQuality() { return 0; }
121 // Interfaces to the major aspects of target machine information:
122 // -- Instruction opcode and operand information
123 // -- Pipelines and scheduling information
124 // -- Stack frame information
125 // -- Selection DAG lowering information
127 virtual const TargetInstrInfo *getInstrInfo() const { return 0; }
128 virtual const TargetFrameInfo *getFrameInfo() const { return 0; }
129 virtual TargetLowering *getTargetLowering() const { return 0; }
130 virtual const TargetData *getTargetData() const { return 0; }
132 /// getTargetAsmInfo - Return target specific asm information.
134 const TargetAsmInfo *getTargetAsmInfo() const {
135 if (!AsmInfo) AsmInfo = createTargetAsmInfo();
139 /// getSubtarget - This method returns a pointer to the specified type of
140 /// TargetSubtarget. In debug builds, it verifies that the object being
141 /// returned is of the correct type.
142 template<typename STC> const STC &getSubtarget() const {
143 const TargetSubtarget *TST = getSubtargetImpl();
144 assert(TST && dynamic_cast<const STC*>(TST) &&
145 "Not the right kind of subtarget!");
146 return *static_cast<const STC*>(TST);
149 /// getRegisterInfo - If register information is available, return it. If
150 /// not, return null. This is kept separate from RegInfo until RegInfo has
151 /// details of graph coloring register allocation removed from it.
153 virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
155 /// getIntrinsicInfo - If intrinsic information is available, return it. If
156 /// not, return null.
158 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
160 /// getJITInfo - If this target supports a JIT, return information for it,
161 /// otherwise return null.
163 virtual TargetJITInfo *getJITInfo() { return 0; }
165 /// getInstrItineraryData - Returns instruction itinerary data for the target
166 /// or specific subtarget.
168 virtual const InstrItineraryData getInstrItineraryData() const {
169 return InstrItineraryData();
172 /// getMachOWriterInfo - If this target supports a Mach-O writer, return
173 /// information for it, otherwise return null.
175 virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
177 /// getELFWriterInfo - If this target supports an ELF writer, return
178 /// information for it, otherwise return null.
180 virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
182 /// getRelocationModel - Returns the code generation relocation model. The
183 /// choices are static, PIC, and dynamic-no-pic, and target default.
184 static Reloc::Model getRelocationModel();
186 /// setRelocationModel - Sets the code generation relocation model.
188 static void setRelocationModel(Reloc::Model Model);
190 /// getCodeModel - Returns the code model. The choices are small, kernel,
191 /// medium, large, and target default.
192 static CodeModel::Model getCodeModel();
194 /// setCodeModel - Sets the code model.
196 static void setCodeModel(CodeModel::Model Model);
198 /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
200 static bool getAsmVerbosityDefault();
202 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
204 static void setAsmVerbosityDefault(bool);
206 /// CodeGenFileType - These enums are meant to be passed into
207 /// addPassesToEmitFile to indicate what type of file to emit.
208 enum CodeGenFileType {
209 AssemblyFile, ObjectFile, DynamicLibrary
212 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
213 /// on this target. User flag overrides.
214 virtual bool getEnableTailMergeDefault() const { return true; }
216 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
217 /// specified file emitted. Typically this will involve several steps of code
218 /// generation. If Fast is set to true, the code generator should emit code
219 /// as fast as possible, though the generated code may be less efficient.
220 /// This method should return FileModel::Error if emission of this file type
221 /// is not supported.
223 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &,
227 return FileModel::None;
230 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
231 /// to be split up (e.g., to add an object writer pass), this method can be
232 /// used to finish up adding passes to emit the file, if necessary.
234 virtual bool addPassesToEmitFileFinish(PassManagerBase &,
235 MachineCodeEmitter *,
240 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
241 /// to be split up (e.g., to add an object writer pass), this method can be
242 /// used to finish up adding passes to emit the file, if necessary.
244 virtual bool addPassesToEmitFileFinish(PassManagerBase &,
250 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
251 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
252 /// actually outputting the machine code and resolving things like the address
253 /// of functions. This method returns true if machine code emission is
256 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
257 MachineCodeEmitter &,
262 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
263 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
264 /// actually outputting the machine code and resolving things like the address
265 /// of functions. This method returns true if machine code emission is
268 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
274 /// addPassesToEmitWholeFile - This method can be implemented by targets that
275 /// require having the entire module at once. This is not recommended, do not
277 virtual bool WantsWholeFile() const { return false; }
278 virtual bool addPassesToEmitWholeFile(PassManager &, raw_ostream &,
285 /// LLVMTargetMachine - This class describes a target machine that is
286 /// implemented with the LLVM target-independent code generator.
288 class LLVMTargetMachine : public TargetMachine {
289 protected: // Can only create subclasses.
290 LLVMTargetMachine() { }
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);
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 fast
302 /// as possible, though the generated code may be less efficient. This method
303 /// should return FileModel::Error if emission of this file type is not
306 /// The default implementation of this method adds components from the
307 /// LLVM retargetable code generator, invoking the methods below to get
308 /// target-specific passes in standard locations.
310 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
312 CodeGenFileType FileType,
315 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
316 /// to be split up (e.g., to add an object writer pass), this method can be
317 /// used to finish up adding passes to emit the file, if necessary.
319 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
320 MachineCodeEmitter *MCE,
323 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
324 /// to be split up (e.g., to add an object writer pass), this method can be
325 /// used to finish up adding passes to emit the file, if necessary.
327 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
331 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
332 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
333 /// actually outputting the machine code and resolving things like the address
334 /// of functions. This method returns true if machine code emission is
337 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
338 MachineCodeEmitter &MCE,
341 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
342 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
343 /// actually outputting the machine code and resolving things like the address
344 /// of functions. This method returns true if machine code emission is
347 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
351 /// Target-Independent Code Generator Pass Configuration Options.
353 /// addInstSelector - This method should add any "last minute" LLVM->LLVM
354 /// passes, then install an instruction selector pass, which converts from
355 /// LLVM code to machine instructions.
356 virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) {
360 /// addPreRegAllocPasses - This method may be implemented by targets that want
361 /// to run passes immediately before register allocation. This should return
362 /// true if -print-machineinstrs should print after these passes.
363 virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
367 /// addPostRegAllocPasses - This method may be implemented by targets that
368 /// want to run passes after register allocation but before prolog-epilog
369 /// insertion. This should return true if -print-machineinstrs should print
370 /// after these passes.
371 virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
375 /// addPreEmitPass - This pass may be implemented by targets that want to run
376 /// passes immediately before machine code is emitted. This should return
377 /// true if -print-machineinstrs should print out the code after the passes.
378 virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
383 /// addAssemblyEmitter - This pass should be overridden by the target to add
384 /// the asmprinter, if asm emission is supported. If this is not supported,
385 /// 'true' should be returned.
386 virtual bool addAssemblyEmitter(PassManagerBase &, CodeGenOpt::Level,
387 bool /* VerboseAsmDefault */, raw_ostream &) {
391 /// addCodeEmitter - This pass should be overridden by the target to add a
392 /// code emitter, if supported. If this is not supported, 'true' should be
393 /// returned. If DumpAsm is true, the generated assembly is printed to cerr.
394 virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
395 bool /*DumpAsm*/, MachineCodeEmitter &) {
399 /// addCodeEmitter - This pass should be overridden by the target to add a
400 /// code emitter, if supported. If this is not supported, 'true' should be
401 /// returned. If DumpAsm is true, the generated assembly is printed to cerr.
402 virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
403 bool /*DumpAsm*/, JITCodeEmitter &) {
407 /// addSimpleCodeEmitter - This pass should be overridden by the target to add
408 /// a code emitter (without setting flags), if supported. If this is not
409 /// supported, 'true' should be returned. If DumpAsm is true, the generated
410 /// assembly is printed to cerr.
411 virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
412 bool /*DumpAsm*/, MachineCodeEmitter &) {
416 /// addSimpleCodeEmitter - This pass should be overridden by the target to add
417 /// a code emitter (without setting flags), if supported. If this is not
418 /// supported, 'true' should be returned. If DumpAsm is true, the generated
419 /// assembly is printed to cerr.
420 virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
421 bool /*DumpAsm*/, JITCodeEmitter &) {
425 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
426 /// on this target. User flag overrides.
427 virtual bool getEnableTailMergeDefault() const { return true; }
430 } // End llvm namespace