61167c4ad753d1bf8d25563a581d15597958cc31
[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 MCAsmInfo;
25 class TargetData;
26 class TargetSubtarget;
27 class TargetInstrInfo;
28 class TargetIntrinsicInfo;
29 class TargetJITInfo;
30 class TargetLowering;
31 class TargetSelectionDAGInfo;
32 class TargetFrameInfo;
33 class JITCodeEmitter;
34 class MCContext;
35 class TargetRegisterInfo;
36 class PassManagerBase;
37 class PassManager;
38 class Pass;
39 class TargetELFWriterInfo;
40 class formatted_raw_ostream;
41
42 // Relocation model types.
43 namespace Reloc {
44   enum Model {
45     Default,
46     Static,
47     PIC_,         // Cannot be named PIC due to collision with -DPIC
48     DynamicNoPIC
49   };
50 }
51
52 // Code model types.
53 namespace CodeModel {
54   enum Model {
55     Default,
56     Small,
57     Kernel,
58     Medium,
59     Large
60   };
61 }
62
63 // Code generation optimization level.
64 namespace CodeGenOpt {
65   enum Level {
66     None,        // -O0
67     Less,        // -O1
68     Default,     // -O2, -Os
69     Aggressive   // -O3
70   };
71 }
72
73 //===----------------------------------------------------------------------===//
74 ///
75 /// TargetMachine - Primary interface to the complete machine description for
76 /// the target machine.  All target-specific information should be accessible
77 /// through this interface.
78 ///
79 class TargetMachine {
80   TargetMachine(const TargetMachine &);   // DO NOT IMPLEMENT
81   void operator=(const TargetMachine &);  // DO NOT IMPLEMENT
82 protected: // Can only create subclasses.
83   TargetMachine(const Target &);
84
85   /// getSubtargetImpl - virtual method implemented by subclasses that returns
86   /// a reference to that target's TargetSubtarget-derived member variable.
87   virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
88
89   /// TheTarget - The Target that this machine was created for.
90   const Target &TheTarget;
91   
92   /// AsmInfo - Contains target specific asm information.
93   ///
94   const MCAsmInfo *AsmInfo;
95   
96 public:
97   virtual ~TargetMachine();
98
99   const Target &getTarget() const { return TheTarget; }
100
101   // Interfaces to the major aspects of target machine information:
102   // -- Instruction opcode and operand information
103   // -- Pipelines and scheduling information
104   // -- Stack frame information
105   // -- Selection DAG lowering information
106   //
107   virtual const TargetInstrInfo        *getInstrInfo() const { return 0; }
108   virtual const TargetFrameInfo        *getFrameInfo() const { return 0; }
109   virtual       TargetLowering    *getTargetLowering() const { return 0; }
110   virtual       TargetSelectionDAGInfo *getSelectionDAGInfo() const{ return 0; }
111   virtual const TargetData            *getTargetData() const { return 0; }
112   
113   /// getMCAsmInfo - Return target specific asm information.
114   ///
115   const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
116   
117   /// getSubtarget - This method returns a pointer to the specified type of
118   /// TargetSubtarget.  In debug builds, it verifies that the object being
119   /// returned is of the correct type.
120   template<typename STC> const STC &getSubtarget() const {
121     return *static_cast<const STC*>(getSubtargetImpl());
122   }
123
124   /// getRegisterInfo - If register information is available, return it.  If
125   /// not, return null.  This is kept separate from RegInfo until RegInfo has
126   /// details of graph coloring register allocation removed from it.
127   ///
128   virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
129   
130   /// getIntrinsicInfo - If intrinsic information is available, return it.  If
131   /// not, return null.
132   ///
133   virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
134
135   /// getJITInfo - If this target supports a JIT, return information for it,
136   /// otherwise return null.
137   ///
138   virtual TargetJITInfo *getJITInfo() { return 0; }
139   
140   /// getInstrItineraryData - Returns instruction itinerary data for the target
141   /// or specific subtarget.
142   ///
143   virtual const InstrItineraryData getInstrItineraryData() const {  
144     return InstrItineraryData();
145   }
146
147   /// getELFWriterInfo - If this target supports an ELF writer, return
148   /// information for it, otherwise return null.
149   /// 
150   virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
151
152   /// getRelocationModel - Returns the code generation relocation model. The
153   /// choices are static, PIC, and dynamic-no-pic, and target default.
154   static Reloc::Model getRelocationModel();
155
156   /// setRelocationModel - Sets the code generation relocation model.
157   ///
158   static void setRelocationModel(Reloc::Model Model);
159
160   /// getCodeModel - Returns the code model. The choices are small, kernel,
161   /// medium, large, and target default.
162   static CodeModel::Model getCodeModel();
163
164   /// setCodeModel - Sets the code model.
165   ///
166   static void setCodeModel(CodeModel::Model Model);
167
168   /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
169   ///
170   static bool getAsmVerbosityDefault();
171
172   /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
173   /// is false.
174   static void setAsmVerbosityDefault(bool);
175
176   /// getDataSections - Return true if data objects should be emitted into their
177   /// own section, corresponds to -fdata-sections.
178   static bool getDataSections();
179
180   /// getFunctionSections - Return true if functions should be emitted into
181   /// their own section, corresponding to -ffunction-sections.
182   static bool getFunctionSections();
183
184   /// setDataSections - Set if the data are emit into separate sections.
185   static void setDataSections(bool);
186
187   /// setFunctionSections - Set if the functions are emit into separate
188   /// sections.
189   static void setFunctionSections(bool);
190
191   /// CodeGenFileType - These enums are meant to be passed into
192   /// addPassesToEmitFile to indicate what type of file to emit, and returned by
193   /// it to indicate what type of file could actually be made.
194   enum CodeGenFileType {
195     CGFT_AssemblyFile,
196     CGFT_ObjectFile,
197     CGFT_Null         // Do not emit any output.
198   };
199
200   /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
201   /// on this target.  User flag overrides.
202   virtual bool getEnableTailMergeDefault() const { return true; }
203
204   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
205   /// specified file emitted.  Typically this will involve several steps of code
206   /// generation.  This method should return true if emission of this file type
207   /// is not supported, or false on success.
208   virtual bool addPassesToEmitFile(PassManagerBase &,
209                                    formatted_raw_ostream &,
210                                    CodeGenFileType,
211                                    CodeGenOpt::Level,
212                                    bool = true) {
213     return true;
214   }
215
216   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
217   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
218   /// actually outputting the machine code and resolving things like the address
219   /// of functions.  This method returns true if machine code emission is
220   /// not supported.
221   ///
222   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
223                                           JITCodeEmitter &,
224                                           CodeGenOpt::Level,
225                                           bool = true) {
226     return true;
227   }
228
229   /// addPassesToEmitWholeFile - This method can be implemented by targets that 
230   /// require having the entire module at once.  This is not recommended, do not
231   /// use this.
232   virtual bool WantsWholeFile() const { return false; }
233   virtual bool addPassesToEmitWholeFile(PassManager &, formatted_raw_ostream &,
234                                         CodeGenFileType,
235                                         CodeGenOpt::Level,
236                                         bool = true) {
237     return true;
238   }
239 };
240
241 /// LLVMTargetMachine - This class describes a target machine that is
242 /// implemented with the LLVM target-independent code generator.
243 ///
244 class LLVMTargetMachine : public TargetMachine {
245   std::string TargetTriple;
246
247 protected: // Can only create subclasses.
248   LLVMTargetMachine(const Target &T, const std::string &TargetTriple);
249   
250 private:
251   /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
252   /// both emitting to assembly files or machine code output.
253   ///
254   bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level,
255                               bool DisableVerify, MCContext *&OutCtx);
256
257   virtual void setCodeModelForJIT();
258   virtual void setCodeModelForStatic();
259   
260 public:
261   
262   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
263   /// specified file emitted.  Typically this will involve several steps of code
264   /// generation.  If OptLevel is None, the code generator should emit code as
265   /// fast as possible, though the generated code may be less efficient.
266   virtual bool addPassesToEmitFile(PassManagerBase &PM,
267                                    formatted_raw_ostream &Out,
268                                    CodeGenFileType FileType,
269                                    CodeGenOpt::Level,
270                                    bool DisableVerify = true);
271   
272   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
273   /// get machine code emitted.  This uses a JITCodeEmitter 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 &PM,
279                                           JITCodeEmitter &MCE,
280                                           CodeGenOpt::Level,
281                                           bool DisableVerify = true);
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 &, CodeGenOpt::Level) {
289     return true;
290   }
291
292   /// addPreRegAlloc - This method may be implemented by targets that want to
293   /// run passes immediately before register allocation. This should return
294   /// true if -print-machineinstrs should print after these passes.
295   virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
296     return false;
297   }
298
299   /// addPostRegAlloc - This method may be implemented by targets that want
300   /// 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 &, CodeGenOpt::Level) {
304     return false;
305   }
306
307   /// addPreSched2 - This method may be implemented by targets that want to
308   /// run passes after prolog-epilog insertion and before the second instruction
309   /// scheduling pass.  This should return true if -print-machineinstrs should
310   /// print after these passes.
311   virtual bool addPreSched2(PassManagerBase &, CodeGenOpt::Level) {
312     return false;
313   }
314   
315   /// addPreEmitPass - This pass may be implemented by targets that want to run
316   /// passes immediately before machine code is emitted.  This should return
317   /// true if -print-machineinstrs should print out the code after the passes.
318   virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
319     return false;
320   }
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.
326   virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
327                               JITCodeEmitter &) {
328     return true;
329   }
330
331   /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
332   /// on this target.  User flag overrides.
333   virtual bool getEnableTailMergeDefault() const { return true; }
334 };
335
336 } // End llvm namespace
337
338 #endif