Rename TargetSubtarget to TargetSubtargetInfo for consistency.
[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 <cassert>
18 #include <string>
19
20 namespace llvm {
21
22 class InstrItineraryData;
23 class JITCodeEmitter;
24 class MCAsmInfo;
25 class MCContext;
26 class Pass;
27 class PassManager;
28 class PassManagerBase;
29 class Target;
30 class TargetData;
31 class TargetELFWriterInfo;
32 class TargetFrameLowering;
33 class TargetInstrInfo;
34 class TargetIntrinsicInfo;
35 class TargetJITInfo;
36 class TargetLowering;
37 class TargetRegisterInfo;
38 class TargetSelectionDAGInfo;
39 class TargetSubtargetInfo;
40 class formatted_raw_ostream;
41 class 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 // Code generation optimization level.
65 namespace CodeGenOpt {
66   enum Level {
67     None,        // -O0
68     Less,        // -O1
69     Default,     // -O2, -Os
70     Aggressive   // -O3
71   };
72 }
73
74 namespace Sched {
75   enum Preference {
76     None,             // No preference
77     Latency,          // Scheduling for shortest total latency.
78     RegPressure,      // Scheduling for lowest register pressure.
79     Hybrid,           // Scheduling for both latency and register pressure.
80     ILP               // Scheduling for ILP in low register pressure mode.
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 TargetSubtargetInfo-derived member variable.
98   virtual const TargetSubtargetInfo *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   const MCAsmInfo *AsmInfo;
106
107   unsigned MCRelaxAll : 1;
108   unsigned MCNoExecStack : 1;
109   unsigned MCSaveTempLabels : 1;
110   unsigned MCUseLoc : 1;
111   unsigned MCUseCFI : 1;
112
113 public:
114   virtual ~TargetMachine();
115
116   const Target &getTarget() const { return TheTarget; }
117
118   // Interfaces to the major aspects of target machine information:
119   // -- Instruction opcode and operand information
120   // -- Pipelines and scheduling information
121   // -- Stack frame information
122   // -- Selection DAG lowering information
123   //
124   virtual const TargetInstrInfo         *getInstrInfo() const { return 0; }
125   virtual const TargetFrameLowering *getFrameLowering() const { return 0; }
126   virtual const TargetLowering    *getTargetLowering() const { return 0; }
127   virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const{ return 0; }
128   virtual const TargetData             *getTargetData() const { return 0; }
129
130   /// getMCAsmInfo - Return target specific asm information.
131   ///
132   const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
133
134   /// getSubtarget - This method returns a pointer to the specified type of
135   /// TargetSubtargetInfo.  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     return *static_cast<const STC*>(getSubtargetImpl());
139   }
140
141   /// getRegisterInfo - If register information is available, return it.  If
142   /// not, return null.  This is kept separate from RegInfo until RegInfo has
143   /// details of graph coloring register allocation removed from it.
144   ///
145   virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
146
147   /// getIntrinsicInfo - If intrinsic information is available, return it.  If
148   /// not, return null.
149   ///
150   virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
151
152   /// getJITInfo - If this target supports a JIT, return information for it,
153   /// otherwise return null.
154   ///
155   virtual TargetJITInfo *getJITInfo() { return 0; }
156
157   /// getInstrItineraryData - Returns instruction itinerary data for the target
158   /// or specific subtarget.
159   ///
160   virtual const InstrItineraryData *getInstrItineraryData() const {
161     return 0;
162   }
163
164   /// getELFWriterInfo - If this target supports an ELF writer, return
165   /// information for it, otherwise return null.
166   ///
167   virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
168
169   /// hasMCRelaxAll - Check whether all machine code instructions should be
170   /// relaxed.
171   bool hasMCRelaxAll() const { return MCRelaxAll; }
172
173   /// setMCRelaxAll - Set whether all machine code instructions should be
174   /// relaxed.
175   void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
176
177   /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
178   /// (i.e., not treated as temporary).
179   bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
180
181   /// setMCSaveTempLabels - Set whether temporary labels will be preserved
182   /// (i.e., not treated as temporary).
183   void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
184
185   /// hasMCNoExecStack - Check whether an executable stack is not needed.
186   bool hasMCNoExecStack() const { return MCNoExecStack; }
187
188   /// setMCNoExecStack - Set whether an executabel stack is not needed.
189   void setMCNoExecStack(bool Value) { MCNoExecStack = Value; }
190
191   /// hasMCUseLoc - Check whether we should use dwarf's .loc directive.
192   bool hasMCUseLoc() const { return MCUseLoc; }
193
194   /// setMCUseLoc - Set whether all we should use dwarf's .loc directive.
195   void setMCUseLoc(bool Value) { MCUseLoc = Value; }
196
197   /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives.
198   bool hasMCUseCFI() const { return MCUseCFI; }
199
200   /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives.
201   void setMCUseCFI(bool Value) { MCUseCFI = Value; }
202
203   /// getRelocationModel - Returns the code generation relocation model. The
204   /// choices are static, PIC, and dynamic-no-pic, and target default.
205   static Reloc::Model getRelocationModel();
206
207   /// setRelocationModel - Sets the code generation relocation model.
208   ///
209   static void setRelocationModel(Reloc::Model Model);
210
211   /// getCodeModel - Returns the code model. The choices are small, kernel,
212   /// medium, large, and target default.
213   static CodeModel::Model getCodeModel();
214
215   /// setCodeModel - Sets the code model.
216   ///
217   static void setCodeModel(CodeModel::Model Model);
218
219   /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
220   ///
221   static bool getAsmVerbosityDefault();
222
223   /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
224   /// is false.
225   static void setAsmVerbosityDefault(bool);
226
227   /// getDataSections - Return true if data objects should be emitted into their
228   /// own section, corresponds to -fdata-sections.
229   static bool getDataSections();
230
231   /// getFunctionSections - Return true if functions should be emitted into
232   /// their own section, corresponding to -ffunction-sections.
233   static bool getFunctionSections();
234
235   /// setDataSections - Set if the data are emit into separate sections.
236   static void setDataSections(bool);
237
238   /// setFunctionSections - Set if the functions are emit into separate
239   /// sections.
240   static void setFunctionSections(bool);
241
242   /// CodeGenFileType - These enums are meant to be passed into
243   /// addPassesToEmitFile to indicate what type of file to emit, and returned by
244   /// it to indicate what type of file could actually be made.
245   enum CodeGenFileType {
246     CGFT_AssemblyFile,
247     CGFT_ObjectFile,
248     CGFT_Null         // Do not emit any output.
249   };
250
251   /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
252   /// on this target.  User flag overrides.
253   virtual bool getEnableTailMergeDefault() const { return true; }
254
255   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
256   /// specified file emitted.  Typically this will involve several steps of code
257   /// generation.  This method should return true if emission of this file type
258   /// is not supported, or false on success.
259   virtual bool addPassesToEmitFile(PassManagerBase &,
260                                    formatted_raw_ostream &,
261                                    CodeGenFileType,
262                                    CodeGenOpt::Level,
263                                    bool = true) {
264     return true;
265   }
266
267   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
268   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
269   /// actually outputting the machine code and resolving things like the address
270   /// of functions.  This method returns true if machine code emission is
271   /// not supported.
272   ///
273   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
274                                           JITCodeEmitter &,
275                                           CodeGenOpt::Level,
276                                           bool = true) {
277     return true;
278   }
279
280   /// addPassesToEmitMC - Add passes to the specified pass manager to get
281   /// machine code emitted with the MCJIT. This method returns true if machine
282   /// code is not supported. It fills the MCContext Ctx pointer which can be
283   /// used to build custom MCStreamer.
284   ///
285   virtual bool addPassesToEmitMC(PassManagerBase &,
286                                  MCContext *&,
287                                  raw_ostream &,
288                                  CodeGenOpt::Level,
289                                  bool = true) {
290     return true;
291   }
292 };
293
294 /// LLVMTargetMachine - This class describes a target machine that is
295 /// implemented with the LLVM target-independent code generator.
296 ///
297 class LLVMTargetMachine : public TargetMachine {
298   std::string TargetTriple;
299
300 protected: // Can only create subclasses.
301   LLVMTargetMachine(const Target &T, const std::string &TargetTriple);
302
303 private:
304   /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
305   /// both emitting to assembly files or machine code output.
306   ///
307   bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level,
308                               bool DisableVerify, MCContext *&OutCtx);
309
310   virtual void setCodeModelForJIT();
311   virtual void setCodeModelForStatic();
312
313 public:
314
315   const std::string &getTargetTriple() const { return TargetTriple; }
316
317   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
318   /// specified file emitted.  Typically this will involve several steps of code
319   /// generation.  If OptLevel is None, the code generator should emit code as
320   /// fast as possible, though the generated code may be less efficient.
321   virtual bool addPassesToEmitFile(PassManagerBase &PM,
322                                    formatted_raw_ostream &Out,
323                                    CodeGenFileType FileType,
324                                    CodeGenOpt::Level,
325                                    bool DisableVerify = true);
326
327   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
328   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
329   /// actually outputting the machine code and resolving things like the address
330   /// of functions.  This method returns true if machine code emission is
331   /// not supported.
332   ///
333   virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
334                                           JITCodeEmitter &MCE,
335                                           CodeGenOpt::Level,
336                                           bool DisableVerify = true);
337
338   /// addPassesToEmitMC - Add passes to the specified pass manager to get
339   /// machine code emitted with the MCJIT. This method returns true if machine
340   /// code is not supported. It fills the MCContext Ctx pointer which can be
341   /// used to build custom MCStreamer.
342   ///
343   virtual bool addPassesToEmitMC(PassManagerBase &PM,
344                                  MCContext *&Ctx,
345                                  raw_ostream &OS,
346                                  CodeGenOpt::Level OptLevel,
347                                  bool DisableVerify = true);
348
349   /// Target-Independent Code Generator Pass Configuration Options.
350
351   /// addPreISelPasses - This method should add any "last minute" LLVM->LLVM
352   /// passes (which are run just before instruction selector).
353   virtual bool addPreISel(PassManagerBase &, CodeGenOpt::Level) {
354     return true;
355   }
356
357   /// addInstSelector - This method should install an instruction selector pass,
358   /// which converts from LLVM code to machine instructions.
359   virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) {
360     return true;
361   }
362
363   /// addPreRegAlloc - This method may be implemented by targets that want to
364   /// run passes immediately before register allocation. This should return
365   /// true if -print-machineinstrs should print after these passes.
366   virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
367     return false;
368   }
369
370   /// addPostRegAlloc - This method may be implemented by targets that want
371   /// to run passes after register allocation but before prolog-epilog
372   /// insertion.  This should return true if -print-machineinstrs should print
373   /// after these passes.
374   virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
375     return false;
376   }
377
378   /// addPreSched2 - This method may be implemented by targets that want to
379   /// run passes after prolog-epilog insertion and before the second instruction
380   /// scheduling pass.  This should return true if -print-machineinstrs should
381   /// print after these passes.
382   virtual bool addPreSched2(PassManagerBase &, CodeGenOpt::Level) {
383     return false;
384   }
385
386   /// addPreEmitPass - This pass may be implemented by targets that want to run
387   /// passes immediately before machine code is emitted.  This should return
388   /// true if -print-machineinstrs should print out the code after the passes.
389   virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
390     return false;
391   }
392
393
394   /// addCodeEmitter - This pass should be overridden by the target to add a
395   /// code emitter, if supported.  If this is not supported, 'true' should be
396   /// returned.
397   virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
398                               JITCodeEmitter &) {
399     return true;
400   }
401
402   /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
403   /// on this target.  User flag overrides.
404   virtual bool getEnableTailMergeDefault() const { return true; }
405 };
406
407 } // End llvm namespace
408
409 #endif