38e377d1002e897e9c416055c33da5efa0d070ad
[oota-llvm.git] / include / llvm / Target / TargetMachine.h
1 //===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the TargetMachine and LLVMTargetMachine classes.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TARGET_TARGETMACHINE_H
15 #define LLVM_TARGET_TARGETMACHINE_H
16
17 #include "llvm/Target/TargetInstrItineraries.h"
18 #include <cassert>
19 #include <iosfwd>
20
21 namespace llvm {
22
23 class TargetAsmInfo;
24 class TargetData;
25 class TargetSubtarget;
26 class TargetInstrInfo;
27 class TargetJITInfo;
28 class TargetLowering;
29 class TargetFrameInfo;
30 class MachineCodeEmitter;
31 class TargetRegisterInfo;
32 class Module;
33 class PassManagerBase;
34 class PassManager;
35 class Pass;
36 class TargetMachOWriterInfo;
37 class TargetELFWriterInfo;
38 class raw_ostream;
39
40 // Relocation model types.
41 namespace Reloc {
42   enum Model {
43     Default,
44     Static,
45     PIC_,         // Cannot be named PIC due to collision with -DPIC
46     DynamicNoPIC
47   };
48 }
49
50 // Code model types.
51 namespace CodeModel {
52   enum Model {
53     Default,
54     Small,
55     Kernel,
56     Medium,
57     Large
58   };
59 }
60
61 namespace FileModel {
62   enum Model {
63     Error,
64     None,
65     AsmFile,
66     MachOFile,
67     ElfFile
68   };
69 }
70
71 //===----------------------------------------------------------------------===//
72 ///
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.
76 ///
77 class TargetMachine {
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) { }
82
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; }
86   
87   /// AsmInfo - Contains target specific asm information.
88   ///
89   mutable const TargetAsmInfo *AsmInfo;
90   
91   /// createTargetAsmInfo - Create a new instance of target specific asm
92   /// information.
93   virtual const TargetAsmInfo *createTargetAsmInfo() const { return NULL; }
94
95 public:
96   virtual ~TargetMachine();
97
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 &) { return 0; }
104
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; }
110
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
116   //
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; }
121   
122   
123   /// getTargetAsmInfo - Return target specific asm information.
124   ///
125   const TargetAsmInfo *getTargetAsmInfo() const {
126     if (!AsmInfo) AsmInfo = createTargetAsmInfo();
127     return AsmInfo;
128   }
129   
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);
138   }
139
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.
143   ///
144   virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
145
146   /// getJITInfo - If this target supports a JIT, return information for it,
147   /// otherwise return null.
148   ///
149   virtual TargetJITInfo *getJITInfo() { return 0; }
150   
151   /// getInstrItineraryData - Returns instruction itinerary data for the target
152   /// or specific subtarget.
153   ///
154   virtual const InstrItineraryData getInstrItineraryData() const {  
155     return InstrItineraryData();
156   }
157
158   /// getMachOWriterInfo - If this target supports a Mach-O writer, return
159   /// information for it, otherwise return null.
160   /// 
161   virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
162
163   /// getELFWriterInfo - If this target supports an ELF writer, return
164   /// information for it, otherwise return null.
165   /// 
166   virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
167
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();
171
172   /// setRelocationModel - Sets the code generation relocation model.
173   static void setRelocationModel(Reloc::Model Model);
174
175   /// getCodeModel - Returns the code model. The choices are small, kernel,
176   /// medium, large, and target default.
177   static CodeModel::Model getCodeModel();
178
179   /// setCodeModel - Sets the code model.
180   static void setCodeModel(CodeModel::Model Model);
181
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
186   };
187
188   /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
189   /// on this target.  User flag overrides.
190   virtual bool getEnableTailMergeDefault() const { return true; }
191
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.
198   ///
199   virtual FileModel::Model addPassesToEmitFile(PassManagerBase &,
200                                                raw_ostream &,
201                                                CodeGenFileType,
202                                                bool /*Fast*/) {
203     return FileModel::None;
204   }
205
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.
209   ///
210   virtual bool addPassesToEmitFileFinish(PassManagerBase &,
211                                          MachineCodeEmitter *, bool /*Fast*/) {
212     return true;
213   }
214  
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
219   /// not supported.
220   ///
221   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
222                                           MachineCodeEmitter &,
223                                           bool /*Fast*/) {
224     return true;
225   }
226
227   /// addPassesToEmitWholeFile - This method can be implemented by targets that 
228   /// require having the entire module at once.  This is not recommended, do not
229   /// use this.
230   virtual bool WantsWholeFile() const { return false; }
231   virtual bool addPassesToEmitWholeFile(PassManager &, raw_ostream &,
232                                         CodeGenFileType, bool /*Fast*/) {
233     return true;
234   }
235 };
236
237 /// LLVMTargetMachine - This class describes a target machine that is
238 /// implemented with the LLVM target-independent code generator.
239 ///
240 class LLVMTargetMachine : public TargetMachine {
241 protected: // Can only create subclasses.
242   LLVMTargetMachine() { }
243
244   /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
245   /// both emitting to assembly files or machine code output.
246   ///
247   bool addCommonCodeGenPasses(PassManagerBase &, bool /*Fast*/);
248
249 public:
250   
251   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
252   /// specified file emitted.  Typically this will involve several steps of code
253   /// generation.  If Fast is set to true, the code generator should emit code
254   /// as fast as possible, though the generated code may be less efficient.
255   /// This method should return FileModel::Error if emission of this file type
256   /// is not supported.
257   ///
258   /// The default implementation of this method adds components from the
259   /// LLVM retargetable code generator, invoking the methods below to get
260   /// target-specific passes in standard locations.
261   ///
262   virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
263                                                raw_ostream &Out,
264                                                CodeGenFileType FileType,
265                                                bool Fast);
266   
267   /// addPassesToEmitFileFinish - If the passes to emit the specified file had
268   /// to be split up (e.g., to add an object writer pass), this method can be
269   /// used to finish up adding passes to emit the file, if necessary.
270   ///
271   virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
272                                          MachineCodeEmitter *MCE, bool Fast);
273  
274   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
275   /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
276   /// actually outputting the machine code and resolving things like the address
277   /// of functions.  This method returns true if machine code emission is
278   /// not supported.
279   ///
280   virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
281                                           MachineCodeEmitter &MCE, bool Fast);
282   
283   /// Target-Independent Code Generator Pass Configuration Options.
284   
285   /// addInstSelector - This method should add any "last minute" LLVM->LLVM
286   /// passes, then install an instruction selector pass, which converts from
287   /// LLVM code to machine instructions.
288   virtual bool addInstSelector(PassManagerBase &, bool /*Fast*/) {
289     return true;
290   }
291
292   /// addPreRegAllocPasses - This method may be implemented by targets that want
293   /// to run passes immediately before register allocation. This should return
294   /// true if -print-machineinstrs should print after these passes.
295   virtual bool addPreRegAlloc(PassManagerBase &, bool /*Fast*/) {
296     return false;
297   }
298
299   /// addPostRegAllocPasses - This method may be implemented by targets that
300   /// want to run passes after register allocation but before prolog-epilog
301   /// insertion.  This should return true if -print-machineinstrs should print
302   /// after these passes.
303   virtual bool addPostRegAlloc(PassManagerBase &, bool /*Fast*/) {
304     return false;
305   }
306   
307   /// addPreEmitPass - This pass may be implemented by targets that want to run
308   /// passes immediately before machine code is emitted.  This should return
309   /// true if -print-machineinstrs should print out the code after the passes.
310   virtual bool addPreEmitPass(PassManagerBase &, bool /*Fast*/) {
311     return false;
312   }
313   
314   
315   /// addAssemblyEmitter - This pass should be overridden by the target to add
316   /// the asmprinter, if asm emission is supported.  If this is not supported,
317   /// 'true' should be returned.
318   virtual bool addAssemblyEmitter(PassManagerBase &, bool /*Fast*/, 
319                                   raw_ostream &) {
320     return true;
321   }
322   
323   /// addCodeEmitter - This pass should be overridden by the target to add a
324   /// code emitter, if supported.  If this is not supported, 'true' should be
325   /// returned. If DumpAsm is true, the generated assembly is printed to cerr.
326   virtual bool addCodeEmitter(PassManagerBase &, bool /*Fast*/,
327                               bool /*DumpAsm*/, MachineCodeEmitter &) {
328     return true;
329   }
330
331   /// addSimpleCodeEmitter - This pass should be overridden by the target to add
332   /// a code emitter (without setting flags), if supported.  If this is not
333   /// supported, 'true' should be returned.  If DumpAsm is true, the generated
334   /// assembly is printed to cerr.
335   virtual bool addSimpleCodeEmitter(PassManagerBase &, bool /*Fast*/,
336                                     bool /*DumpAsm*/, MachineCodeEmitter &) {
337     return true;
338   }
339
340   /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
341   /// on this target.  User flag overrides.
342   virtual bool getEnableTailMergeDefault() const { return true; }
343 };
344
345 } // End llvm namespace
346
347 #endif