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"
25 class TargetSubtarget;
26 class TargetInstrInfo;
27 class TargetInstrDescriptor;
30 class TargetFrameInfo;
31 class MachineCodeEmitter;
34 class FunctionPassManager;
37 class TargetMachOWriterInfo;
38 class TargetELFWriterInfo;
40 // Relocation model types.
45 PIC_, // Cannot be named PIC due to collision with -DPIC
71 //===----------------------------------------------------------------------===//
73 /// TargetMachine - Primary interface to the complete machine description for
74 /// the target machine. All target-specific information should be accessible
75 /// through this interface.
78 TargetMachine(const TargetMachine &); // DO NOT IMPLEMENT
79 void operator=(const TargetMachine &); // DO NOT IMPLEMENT
80 protected: // Can only create subclasses.
81 TargetMachine() : AsmInfo(NULL) { }
83 /// getSubtargetImpl - virtual method implemented by subclasses that returns
84 /// a reference to that target's TargetSubtarget-derived member variable.
85 virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
87 /// AsmInfo - Contains target specific asm information.
89 mutable const TargetAsmInfo *AsmInfo;
91 /// createTargetAsmInfo - Create a new instance of target specific asm
93 virtual const TargetAsmInfo *createTargetAsmInfo() const { return NULL; }
96 virtual ~TargetMachine();
98 /// getModuleMatchQuality - This static method should be implemented by
99 /// targets to indicate how closely they match the specified module. This is
100 /// used by the LLC tool to determine which target to use when an explicit
101 /// -march option is not specified. If a target returns zero, it will never
102 /// be chosen without an explicit -march option.
103 static unsigned getModuleMatchQuality(const Module &M) { return 0; }
105 /// getJITMatchQuality - This static method should be implemented by targets
106 /// that provide JIT capabilities to indicate how suitable they are for
107 /// execution on the current host. If a value of 0 is returned, the target
108 /// will not be used unless an explicit -march option is used.
109 static unsigned getJITMatchQuality() { return 0; }
111 // 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 virtual const TargetInstrInfo *getInstrInfo() const { return 0; }
118 virtual const TargetFrameInfo *getFrameInfo() const { return 0; }
119 virtual TargetLowering *getTargetLowering() const { return 0; }
120 virtual const TargetData *getTargetData() const { return 0; }
123 /// getTargetAsmInfo - Return target specific asm information.
125 const TargetAsmInfo *getTargetAsmInfo() const {
126 if (!AsmInfo) AsmInfo = createTargetAsmInfo();
130 /// getSubtarget - This method returns a pointer to the specified type of
131 /// TargetSubtarget. 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 const TargetSubtarget *TST = getSubtargetImpl();
135 assert(TST && dynamic_cast<const STC*>(TST) &&
136 "Not the right kind of subtarget!");
137 return *static_cast<const STC*>(TST);
140 /// getRegisterInfo - If register information is available, return it. If
141 /// not, return null. This is kept separate from RegInfo until RegInfo has
142 /// details of graph coloring register allocation removed from it.
144 virtual const MRegisterInfo *getRegisterInfo() const { return 0; }
146 /// getJITInfo - If this target supports a JIT, return information for it,
147 /// otherwise return null.
149 virtual TargetJITInfo *getJITInfo() { return 0; }
151 /// getInstrItineraryData - Returns instruction itinerary data for the target
152 /// or specific subtarget.
154 virtual const InstrItineraryData getInstrItineraryData() const {
155 return InstrItineraryData();
158 /// getMachOWriterInfo - If this target supports a Mach-O writer, return
159 /// information for it, otherwise return null.
161 virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
163 /// getELFWriterInfo - If this target supports an ELF writer, return
164 /// information for it, otherwise return null.
166 virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
168 /// getRelocationModel - Returns the code generation relocation model. The
169 /// choices are static, PIC, and dynamic-no-pic, and target default.
170 static Reloc::Model getRelocationModel();
172 /// setRelocationModel - Sets the code generation relocation model.
173 static void setRelocationModel(Reloc::Model Model);
175 /// getCodeModel - Returns the code model. The choices are small, kernel,
176 /// medium, large, and target default.
177 static CodeModel::Model getCodeModel();
179 /// setCodeModel - Sets the code model.
180 static void setCodeModel(CodeModel::Model Model);
182 /// CodeGenFileType - These enums are meant to be passed into
183 /// addPassesToEmitFile to indicate what type of file to emit.
184 enum CodeGenFileType {
185 AssemblyFile, ObjectFile, DynamicLibrary
188 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
189 /// on this target. User flag overrides.
190 virtual bool getEnableTailMergeDefault() const { return true; }
192 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
193 /// specified file emitted. Typically this will involve several steps of code
194 /// generation. If Fast is set to true, the code generator should emit code
195 /// as fast as possible, though the generated code may be less efficient.
196 /// This method should return FileModel::Error if emission of this file type
197 /// is not supported.
199 virtual FileModel::Model addPassesToEmitFile(FunctionPassManager &PM,
201 CodeGenFileType FileType,
203 return FileModel::None;
206 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
207 /// to be split up (e.g., to add an object writer pass), this method can be
208 /// used to finish up adding passes to emit the file, if necessary.
210 virtual bool addPassesToEmitFileFinish(FunctionPassManager &PM,
211 MachineCodeEmitter *MCE, bool Fast) {
215 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
216 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
217 /// actually outputting the machine code and resolving things like the address
218 /// of functions. This method returns true if machine code emission is
221 virtual bool addPassesToEmitMachineCode(FunctionPassManager &PM,
222 MachineCodeEmitter &MCE, bool Fast) {
226 /// addPassesToEmitWholeFile - This method can be implemented by targets that
227 /// require having the entire module at once. This is not recommended, do not
229 virtual bool WantsWholeFile() const { return false; }
230 virtual bool addPassesToEmitWholeFile(PassManager &PM, std::ostream &Out,
231 CodeGenFileType FileType, bool Fast) {
236 /// LLVMTargetMachine - This class describes a target machine that is
237 /// implemented with the LLVM target-independent code generator.
239 class LLVMTargetMachine : public TargetMachine {
240 protected: // Can only create subclasses.
241 LLVMTargetMachine() { }
244 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
245 /// specified file emitted. Typically this will involve several steps of code
246 /// generation. If Fast is set to true, the code generator should emit code
247 /// as fast as possible, though the generated code may be less efficient.
248 /// This method should return FileModel::Error if emission of this file type
249 /// is not supported.
251 /// The default implementation of this method adds components from the
252 /// LLVM retargetable code generator, invoking the methods below to get
253 /// target-specific passes in standard locations.
255 virtual FileModel::Model addPassesToEmitFile(FunctionPassManager &PM,
257 CodeGenFileType FileType,
260 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
261 /// to be split up (e.g., to add an object writer pass), this method can be
262 /// used to finish up adding passes to emit the file, if necessary.
264 virtual bool addPassesToEmitFileFinish(FunctionPassManager &PM,
265 MachineCodeEmitter *MCE, bool Fast);
267 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
268 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
269 /// actually outputting the machine code and resolving things like the address
270 /// of functions. This method returns true if machine code emission is
273 virtual bool addPassesToEmitMachineCode(FunctionPassManager &PM,
274 MachineCodeEmitter &MCE, bool Fast);
276 /// Target-Independent Code Generator Pass Configuration Options.
278 /// addInstSelector - This method should add any "last minute" LLVM->LLVM
279 /// passes, then install an instruction selector pass, which converts from
280 /// LLVM code to machine instructions.
281 virtual bool addInstSelector(FunctionPassManager &PM, bool Fast) {
285 /// addPostRegAllocPasses - This method may be implemented by targets that
286 /// want to run passes after register allocation but before prolog-epilog
287 /// insertion. This should return true if -print-machineinstrs should print
288 /// after these passes.
289 virtual bool addPostRegAlloc(FunctionPassManager &PM, bool Fast) {
293 /// addPreEmitPass - This pass may be implemented by targets that want to run
294 /// passes immediately before machine code is emitted. This should return
295 /// true if -print-machineinstrs should print out the code after the passes.
296 virtual bool addPreEmitPass(FunctionPassManager &PM, bool Fast) {
301 /// addAssemblyEmitter - This pass should be overridden by the target to add
302 /// the asmprinter, if asm emission is supported. If this is not supported,
303 /// 'true' should be returned.
304 virtual bool addAssemblyEmitter(FunctionPassManager &PM, bool Fast,
309 /// addCodeEmitter - This pass should be overridden by the target to add a
310 /// code emitter, if supported. If this is not supported, 'true' should be
311 /// returned. If DumpAsm is true, the generated assembly is printed to cerr.
312 virtual bool addCodeEmitter(FunctionPassManager &PM, bool Fast, bool DumpAsm,
313 MachineCodeEmitter &MCE) {
317 /// addSimpleCodeEmitter - This pass should be overridden by the target to add
318 /// a code emitter (without setting flags), if supported. If this is not
319 /// supported, 'true' should be returned. If DumpAsm is true, the generated
320 /// assembly is printed to cerr.
321 virtual bool addSimpleCodeEmitter(FunctionPassManager &PM, bool Fast,
322 bool DumpAsm, MachineCodeEmitter &MCE) {
326 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
327 /// on this target. User flag overrides.
328 virtual bool getEnableTailMergeDefault() const { return true; }
331 } // End llvm namespace