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