Address more comments :)
[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
20 namespace llvm {
21
22 class TargetAsmInfo;
23 class TargetData;
24 class TargetSubtarget;
25 class TargetInstrInfo;
26 class TargetIntrinsicInfo;
27 class TargetJITInfo;
28 class TargetLowering;
29 class TargetFrameInfo;
30 class MachineCodeEmitter;
31 class JITCodeEmitter;
32 class TargetRegisterInfo;
33 class Module;
34 class PassManagerBase;
35 class PassManager;
36 class Pass;
37 class TargetMachOWriterInfo;
38 class TargetELFWriterInfo;
39 class raw_ostream;
40
41 // Relocation model types.
42 namespace Reloc {
43   enum Model {
44     Default,
45     Static,
46     PIC_,         // Cannot be named PIC due to collision with -DPIC
47     DynamicNoPIC
48   };
49 }
50
51 // Code model types.
52 namespace CodeModel {
53   enum Model {
54     Default,
55     Small,
56     Kernel,
57     Medium,
58     Large
59   };
60 }
61
62 namespace FileModel {
63   enum Model {
64     Error,
65     None,
66     AsmFile,
67     MachOFile,
68     ElfFile
69   };
70 }
71
72 // Code generation optimization level.
73 namespace CodeGenOpt {
74   enum Level {
75     Default,
76     None,
77     Aggressive
78   };
79 }
80
81
82 // Possible float ABI settings. Used with FloatABIType in TargetOptions.h.
83 namespace FloatABI {
84   enum ABIType {
85     Default, // Target-specific (either soft of hard depending on triple, etc).
86     Soft, // Soft float.
87     Hard  // Hard float.
88   };
89 }
90
91 //===----------------------------------------------------------------------===//
92 ///
93 /// TargetMachine - Primary interface to the complete machine description for
94 /// the target machine.  All target-specific information should be accessible
95 /// through this interface.
96 ///
97 class TargetMachine {
98   TargetMachine(const TargetMachine &);   // DO NOT IMPLEMENT
99   void operator=(const TargetMachine &);  // DO NOT IMPLEMENT
100 protected: // Can only create subclasses.
101   TargetMachine();
102
103   /// getSubtargetImpl - virtual method implemented by subclasses that returns
104   /// a reference to that target's TargetSubtarget-derived member variable.
105   virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
106   
107   /// AsmInfo - Contains target specific asm information.
108   ///
109   mutable const TargetAsmInfo *AsmInfo;
110   
111   /// createTargetAsmInfo - Create a new instance of target specific asm
112   /// information.
113   virtual const TargetAsmInfo *createTargetAsmInfo() const { return 0; }
114
115 public:
116   virtual ~TargetMachine();
117
118   /// getModuleMatchQuality - This static method should be implemented by
119   /// targets to indicate how closely they match the specified module.  This is
120   /// used by the LLC tool to determine which target to use when an explicit
121   /// -march option is not specified.  If a target returns zero, it will never
122   /// be chosen without an explicit -march option.
123   static unsigned getModuleMatchQuality(const Module &) { return 0; }
124
125   /// getJITMatchQuality - This static method should be implemented by targets
126   /// that provide JIT capabilities to indicate how suitable they are for
127   /// execution on the current host.  If a value of 0 is returned, the target
128   /// will not be used unless an explicit -march option is used.
129   static unsigned getJITMatchQuality() { return 0; }
130
131   // Interfaces to the major aspects of target machine information:
132   // -- Instruction opcode and operand information
133   // -- Pipelines and scheduling information
134   // -- Stack frame information
135   // -- Selection DAG lowering information
136   //
137   virtual const TargetInstrInfo        *getInstrInfo() const { return 0; }
138   virtual const TargetFrameInfo        *getFrameInfo() const { return 0; }
139   virtual       TargetLowering    *getTargetLowering() const { return 0; }
140   virtual const TargetData            *getTargetData() const { return 0; }
141   
142   /// getTargetAsmInfo - Return target specific asm information.
143   ///
144   const TargetAsmInfo *getTargetAsmInfo() const {
145     if (!AsmInfo) AsmInfo = createTargetAsmInfo();
146     return AsmInfo;
147   }
148   
149   /// getSubtarget - This method returns a pointer to the specified type of
150   /// TargetSubtarget.  In debug builds, it verifies that the object being
151   /// returned is of the correct type.
152   template<typename STC> const STC &getSubtarget() const {
153     const TargetSubtarget *TST = getSubtargetImpl();
154     assert(TST && dynamic_cast<const STC*>(TST) &&
155            "Not the right kind of subtarget!");
156     return *static_cast<const STC*>(TST);
157   }
158
159   /// getRegisterInfo - If register information is available, return it.  If
160   /// not, return null.  This is kept separate from RegInfo until RegInfo has
161   /// details of graph coloring register allocation removed from it.
162   ///
163   virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
164   
165   /// getIntrinsicInfo - If intrinsic information is available, return it.  If
166   /// not, return null.
167   ///
168   virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
169
170   /// getJITInfo - If this target supports a JIT, return information for it,
171   /// otherwise return null.
172   ///
173   virtual TargetJITInfo *getJITInfo() { return 0; }
174   
175   /// getInstrItineraryData - Returns instruction itinerary data for the target
176   /// or specific subtarget.
177   ///
178   virtual const InstrItineraryData getInstrItineraryData() const {  
179     return InstrItineraryData();
180   }
181
182   /// getMachOWriterInfo - If this target supports a Mach-O writer, return
183   /// information for it, otherwise return null.
184   /// 
185   virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
186
187   /// getELFWriterInfo - If this target supports an ELF writer, return
188   /// information for it, otherwise return null.
189   /// 
190   virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
191
192   /// getRelocationModel - Returns the code generation relocation model. The
193   /// choices are static, PIC, and dynamic-no-pic, and target default.
194   static Reloc::Model getRelocationModel();
195
196   /// setRelocationModel - Sets the code generation relocation model.
197   ///
198   static void setRelocationModel(Reloc::Model Model);
199
200   /// getCodeModel - Returns the code model. The choices are small, kernel,
201   /// medium, large, and target default.
202   static CodeModel::Model getCodeModel();
203
204   /// setCodeModel - Sets the code model.
205   ///
206   static void setCodeModel(CodeModel::Model Model);
207
208   /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
209   ///
210   static bool getAsmVerbosityDefault();
211
212   /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
213   /// is false.
214   static void setAsmVerbosityDefault(bool);
215
216   /// CodeGenFileType - These enums are meant to be passed into
217   /// addPassesToEmitFile to indicate what type of file to emit.
218   enum CodeGenFileType {
219     AssemblyFile, ObjectFile, DynamicLibrary
220   };
221
222   /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
223   /// on this target.  User flag overrides.
224   virtual bool getEnableTailMergeDefault() const { return true; }
225
226   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
227   /// specified file emitted.  Typically this will involve several steps of code
228   /// generation.  If Fast is set to true, the code generator should emit code
229   /// as fast as possible, though the generated code may be less efficient.
230   /// This method should return FileModel::Error if emission of this file type
231   /// is not supported.
232   ///
233   virtual FileModel::Model addPassesToEmitFile(PassManagerBase &,
234                                                raw_ostream &,
235                                                CodeGenFileType,
236                                                CodeGenOpt::Level) {
237     return FileModel::None;
238   }
239
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.
243   ///
244   virtual bool addPassesToEmitFileFinish(PassManagerBase &,
245                                          MachineCodeEmitter *,
246                                          CodeGenOpt::Level) {
247     return true;
248   }
249  
250   /// addPassesToEmitFileFinish - If the passes to emit the specified file had
251   /// to be split up (e.g., to add an object writer pass), this method can be
252   /// used to finish up adding passes to emit the file, if necessary.
253   ///
254   virtual bool addPassesToEmitFileFinish(PassManagerBase &,
255                                          JITCodeEmitter *,
256                                          CodeGenOpt::Level) {
257     return true;
258   }
259  
260   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
261   /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
262   /// actually outputting the machine code and resolving things like the address
263   /// of functions.  This method returns true if machine code emission is
264   /// not supported.
265   ///
266   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
267                                           MachineCodeEmitter &,
268                                           CodeGenOpt::Level) {
269     return true;
270   }
271
272   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
273   /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
274   /// actually outputting the machine code and resolving things like the address
275   /// of functions.  This method returns true if machine code emission is
276   /// not supported.
277   ///
278   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
279                                           JITCodeEmitter &,
280                                           CodeGenOpt::Level) {
281     return true;
282   }
283
284   /// addPassesToEmitWholeFile - This method can be implemented by targets that 
285   /// require having the entire module at once.  This is not recommended, do not
286   /// use this.
287   virtual bool WantsWholeFile() const { return false; }
288   virtual bool addPassesToEmitWholeFile(PassManager &, raw_ostream &,
289                                         CodeGenFileType,
290                                         CodeGenOpt::Level) {
291     return true;
292   }
293 };
294
295 /// LLVMTargetMachine - This class describes a target machine that is
296 /// implemented with the LLVM target-independent code generator.
297 ///
298 class LLVMTargetMachine : public TargetMachine {
299 protected: // Can only create subclasses.
300   LLVMTargetMachine() { }
301
302   /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
303   /// both emitting to assembly files or machine code output.
304   ///
305   bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level);
306
307 public:
308   
309   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
310   /// specified file emitted.  Typically this will involve several steps of code
311   /// generation.  If OptLevel is None, the code generator should emit code as fast
312   /// as possible, though the generated code may be less efficient.  This method
313   /// should return FileModel::Error if emission of this file type is not
314   /// supported.
315   ///
316   /// The default implementation of this method adds components from the
317   /// LLVM retargetable code generator, invoking the methods below to get
318   /// target-specific passes in standard locations.
319   ///
320   virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
321                                                raw_ostream &Out,
322                                                CodeGenFileType FileType,
323                                                CodeGenOpt::Level);
324   
325   /// addPassesToEmitFileFinish - If the passes to emit the specified file had
326   /// to be split up (e.g., to add an object writer pass), this method can be
327   /// used to finish up adding passes to emit the file, if necessary.
328   ///
329   virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
330                                          MachineCodeEmitter *MCE,
331                                          CodeGenOpt::Level);
332  
333   /// addPassesToEmitFileFinish - If the passes to emit the specified file had
334   /// to be split up (e.g., to add an object writer pass), this method can be
335   /// used to finish up adding passes to emit the file, if necessary.
336   ///
337   virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
338                                          JITCodeEmitter *MCE,
339                                          CodeGenOpt::Level);
340  
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
345   /// not supported.
346   ///
347   virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
348                                           MachineCodeEmitter &MCE,
349                                           CodeGenOpt::Level);
350   
351   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
352   /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
353   /// actually outputting the machine code and resolving things like the address
354   /// of functions.  This method returns true if machine code emission is
355   /// not supported.
356   ///
357   virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
358                                           JITCodeEmitter &MCE,
359                                           CodeGenOpt::Level);
360   
361   /// Target-Independent Code Generator Pass Configuration Options.
362   
363   /// addInstSelector - This method should add any "last minute" LLVM->LLVM
364   /// passes, then install an instruction selector pass, which converts from
365   /// LLVM code to machine instructions.
366   virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) {
367     return true;
368   }
369
370   /// addPreRegAllocPasses - This method may be implemented by targets that want
371   /// to run passes immediately before register allocation. This should return
372   /// true if -print-machineinstrs should print after these passes.
373   virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
374     return false;
375   }
376
377   /// addPostRegAllocPasses - This method may be implemented by targets that
378   /// want to run passes after register allocation but before prolog-epilog
379   /// insertion.  This should return true if -print-machineinstrs should print
380   /// after these passes.
381   virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
382     return false;
383   }
384   
385   /// addPreEmitPass - This pass may be implemented by targets that want to run
386   /// passes immediately before machine code is emitted.  This should return
387   /// true if -print-machineinstrs should print out the code after the passes.
388   virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
389     return false;
390   }
391   
392   
393   /// addAssemblyEmitter - This pass should be overridden by the target to add
394   /// the asmprinter, if asm emission is supported.  If this is not supported,
395   /// 'true' should be returned.
396   virtual bool addAssemblyEmitter(PassManagerBase &, CodeGenOpt::Level,
397                                   bool /* VerboseAsmDefault */, raw_ostream &) {
398     return true;
399   }
400   
401   /// addCodeEmitter - This pass should be overridden by the target to add a
402   /// code emitter, if supported.  If this is not supported, 'true' should be
403   /// returned. If DumpAsm is true, the generated assembly is printed to cerr.
404   virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
405                               bool /*DumpAsm*/, MachineCodeEmitter &) {
406     return true;
407   }
408
409   /// addCodeEmitter - This pass should be overridden by the target to add a
410   /// code emitter, if supported.  If this is not supported, 'true' should be
411   /// returned. If DumpAsm is true, the generated assembly is printed to cerr.
412   virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
413                               bool /*DumpAsm*/, JITCodeEmitter &) {
414     return true;
415   }
416
417   /// addSimpleCodeEmitter - This pass should be overridden by the target to add
418   /// a code emitter (without setting flags), if supported.  If this is not
419   /// supported, 'true' should be returned.  If DumpAsm is true, the generated
420   /// assembly is printed to cerr.
421   virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
422                                     bool /*DumpAsm*/, MachineCodeEmitter &) {
423     return true;
424   }
425
426   /// addSimpleCodeEmitter - This pass should be overridden by the target to add
427   /// a code emitter (without setting flags), if supported.  If this is not
428   /// supported, 'true' should be returned.  If DumpAsm is true, the generated
429   /// assembly is printed to cerr.
430   virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
431                                     bool /*DumpAsm*/, JITCodeEmitter &) {
432     return true;
433   }
434
435   /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
436   /// on this target.  User flag overrides.
437   virtual bool getEnableTailMergeDefault() const { return true; }
438 };
439
440 } // End llvm namespace
441
442 #endif