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