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;
29 class TargetFrameInfo;
30 class MachineCodeEmitter;
31 class TargetRegisterInfo;
33 class PassManagerBase;
36 class TargetMachOWriterInfo;
37 class TargetELFWriterInfo;
39 // Relocation model types.
44 PIC_, // Cannot be named PIC due to collision with -DPIC
70 //===----------------------------------------------------------------------===//
72 /// TargetMachine - Primary interface to the complete machine description for
73 /// the target machine. All target-specific information should be accessible
74 /// through this interface.
77 TargetMachine(const TargetMachine &); // DO NOT IMPLEMENT
78 void operator=(const TargetMachine &); // DO NOT IMPLEMENT
79 protected: // Can only create subclasses.
80 TargetMachine() : AsmInfo(NULL) { }
82 /// getSubtargetImpl - virtual method implemented by subclasses that returns
83 /// a reference to that target's TargetSubtarget-derived member variable.
84 virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
86 /// AsmInfo - Contains target specific asm information.
88 mutable const TargetAsmInfo *AsmInfo;
90 /// createTargetAsmInfo - Create a new instance of target specific asm
92 virtual const TargetAsmInfo *createTargetAsmInfo() const { return NULL; }
95 virtual ~TargetMachine();
97 /// getModuleMatchQuality - This static method should be implemented by
98 /// targets to indicate how closely they match the specified module. This is
99 /// used by the LLC tool to determine which target to use when an explicit
100 /// -march option is not specified. If a target returns zero, it will never
101 /// be chosen without an explicit -march option.
102 static unsigned getModuleMatchQuality(const Module &M) { return 0; }
104 /// getJITMatchQuality - This static method should be implemented by targets
105 /// that provide JIT capabilities to indicate how suitable they are for
106 /// execution on the current host. If a value of 0 is returned, the target
107 /// will not be used unless an explicit -march option is used.
108 static unsigned getJITMatchQuality() { return 0; }
110 // Interfaces to the major aspects of target machine information:
111 // -- Instruction opcode and operand information
112 // -- Pipelines and scheduling information
113 // -- Stack frame information
114 // -- Selection DAG lowering information
116 virtual const TargetInstrInfo *getInstrInfo() const { return 0; }
117 virtual const TargetFrameInfo *getFrameInfo() const { return 0; }
118 virtual TargetLowering *getTargetLowering() const { return 0; }
119 virtual const TargetData *getTargetData() const { return 0; }
122 /// getTargetAsmInfo - Return target specific asm information.
124 const TargetAsmInfo *getTargetAsmInfo() const {
125 if (!AsmInfo) AsmInfo = createTargetAsmInfo();
129 /// getSubtarget - This method returns a pointer to the specified type of
130 /// TargetSubtarget. 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 const TargetSubtarget *TST = getSubtargetImpl();
134 assert(TST && dynamic_cast<const STC*>(TST) &&
135 "Not the right kind of subtarget!");
136 return *static_cast<const STC*>(TST);
139 /// getRegisterInfo - If register information is available, return it. If
140 /// not, return null. This is kept separate from RegInfo until RegInfo has
141 /// details of graph coloring register allocation removed from it.
143 virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
145 /// getJITInfo - If this target supports a JIT, return information for it,
146 /// otherwise return null.
148 virtual TargetJITInfo *getJITInfo() { return 0; }
150 /// getInstrItineraryData - Returns instruction itinerary data for the target
151 /// or specific subtarget.
153 virtual const InstrItineraryData getInstrItineraryData() const {
154 return InstrItineraryData();
157 /// getMachOWriterInfo - If this target supports a Mach-O writer, return
158 /// information for it, otherwise return null.
160 virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
162 /// getELFWriterInfo - If this target supports an ELF writer, return
163 /// information for it, otherwise return null.
165 virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
167 /// getRelocationModel - Returns the code generation relocation model. The
168 /// choices are static, PIC, and dynamic-no-pic, and target default.
169 static Reloc::Model getRelocationModel();
171 /// setRelocationModel - Sets the code generation relocation model.
172 static void setRelocationModel(Reloc::Model Model);
174 /// getCodeModel - Returns the code model. The choices are small, kernel,
175 /// medium, large, and target default.
176 static CodeModel::Model getCodeModel();
178 /// setCodeModel - Sets the code model.
179 static void setCodeModel(CodeModel::Model Model);
181 /// CodeGenFileType - These enums are meant to be passed into
182 /// addPassesToEmitFile to indicate what type of file to emit.
183 enum CodeGenFileType {
184 AssemblyFile, ObjectFile, DynamicLibrary
187 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
188 /// on this target. User flag overrides.
189 virtual bool getEnableTailMergeDefault() const { return true; }
191 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
192 /// specified file emitted. Typically this will involve several steps of code
193 /// generation. If Fast is set to true, the code generator should emit code
194 /// as fast as possible, though the generated code may be less efficient.
195 /// This method should return FileModel::Error if emission of this file type
196 /// is not supported.
198 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
200 CodeGenFileType FileType,
202 return FileModel::None;
205 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
206 /// to be split up (e.g., to add an object writer pass), this method can be
207 /// used to finish up adding passes to emit the file, if necessary.
209 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
210 MachineCodeEmitter *MCE, bool Fast) {
214 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
215 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
216 /// actually outputting the machine code and resolving things like the address
217 /// of functions. This method returns true if machine code emission is
220 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
221 MachineCodeEmitter &MCE, bool Fast) {
225 /// addPassesToEmitWholeFile - This method can be implemented by targets that
226 /// require having the entire module at once. This is not recommended, do not
228 virtual bool WantsWholeFile() const { return false; }
229 virtual bool addPassesToEmitWholeFile(PassManager &PM, std::ostream &Out,
230 CodeGenFileType FileType, bool Fast) {
235 /// LLVMTargetMachine - This class describes a target machine that is
236 /// implemented with the LLVM target-independent code generator.
238 class LLVMTargetMachine : public TargetMachine {
239 protected: // Can only create subclasses.
240 LLVMTargetMachine() { }
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. If Fast is set to true, the code generator should emit code
246 /// as fast as possible, though the generated code may be less efficient.
247 /// This method should return FileModel::Error if emission of this file type
248 /// is not supported.
250 /// The default implementation of this method adds components from the
251 /// LLVM retargetable code generator, invoking the methods below to get
252 /// target-specific passes in standard locations.
254 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
256 CodeGenFileType FileType,
259 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
260 /// to be split up (e.g., to add an object writer pass), this method can be
261 /// used to finish up adding passes to emit the file, if necessary.
263 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
264 MachineCodeEmitter *MCE, bool Fast);
266 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
267 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
268 /// actually outputting the machine code and resolving things like the address
269 /// of functions. This method returns true if machine code emission is
272 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
273 MachineCodeEmitter &MCE, bool Fast);
275 /// Target-Independent Code Generator Pass Configuration Options.
277 /// addInstSelector - This method should add any "last minute" LLVM->LLVM
278 /// passes, then install an instruction selector pass, which converts from
279 /// LLVM code to machine instructions.
280 virtual bool addInstSelector(PassManagerBase &PM, bool Fast) {
284 /// addPreRegAllocPasses - This method may be implemented by targets that want
285 /// to run passes immediately before register allocation. This should return
286 /// true if -print-machineinstrs should print after these passes.
287 virtual bool addPreRegAlloc(PassManagerBase &PM, bool Fast) {
291 /// addPostRegAllocPasses - This method may be implemented by targets that
292 /// want to run passes after register allocation but before prolog-epilog
293 /// insertion. This should return true if -print-machineinstrs should print
294 /// after these passes.
295 virtual bool addPostRegAlloc(PassManagerBase &PM, bool Fast) {
299 /// addPreEmitPass - This pass may be implemented by targets that want to run
300 /// passes immediately before machine code is emitted. This should return
301 /// true if -print-machineinstrs should print out the code after the passes.
302 virtual bool addPreEmitPass(PassManagerBase &PM, bool Fast) {
307 /// addAssemblyEmitter - This pass should be overridden by the target to add
308 /// the asmprinter, if asm emission is supported. If this is not supported,
309 /// 'true' should be returned.
310 virtual bool addAssemblyEmitter(PassManagerBase &PM, bool Fast,
315 /// addCodeEmitter - This pass should be overridden by the target to add a
316 /// code emitter, if supported. If this is not supported, 'true' should be
317 /// returned. If DumpAsm is true, the generated assembly is printed to cerr.
318 virtual bool addCodeEmitter(PassManagerBase &PM, bool Fast, bool DumpAsm,
319 MachineCodeEmitter &MCE) {
323 /// addSimpleCodeEmitter - This pass should be overridden by the target to add
324 /// a code emitter (without setting flags), if supported. If this is not
325 /// supported, 'true' should be returned. If DumpAsm is true, the generated
326 /// assembly is printed to cerr.
327 virtual bool addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
328 bool DumpAsm, MachineCodeEmitter &MCE) {
332 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
333 /// on this target. User flag overrides.
334 virtual bool getEnableTailMergeDefault() const { return true; }
337 } // End llvm namespace