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