demonstrate usage of Cases() mapping several strings to the same value; remove traili...
[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 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     None,        // -O0
78     Less,        // -O1
79     Default,     // -O2, -Os
80     Aggressive   // -O3
81   };
82 }
83
84
85 //===----------------------------------------------------------------------===//
86 ///
87 /// TargetMachine - Primary interface to the complete machine description for
88 /// the target machine.  All target-specific information should be accessible
89 /// through this interface.
90 ///
91 class TargetMachine {
92   TargetMachine(const TargetMachine &);   // DO NOT IMPLEMENT
93   void operator=(const TargetMachine &);  // DO NOT IMPLEMENT
94 protected: // Can only create subclasses.
95   TargetMachine(const Target &);
96
97   /// getSubtargetImpl - virtual method implemented by subclasses that returns
98   /// a reference to that target's TargetSubtarget-derived member variable.
99   virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
100
101   /// TheTarget - The Target that this machine was created for.
102   const Target &TheTarget;
103   
104   /// AsmInfo - Contains target specific asm information.
105   ///
106   const MCAsmInfo *AsmInfo;
107   
108 public:
109   virtual ~TargetMachine();
110
111   const Target &getTarget() const { return TheTarget; }
112
113   // Interfaces to the major aspects of target machine information:
114   // -- Instruction opcode and operand information
115   // -- Pipelines and scheduling information
116   // -- Stack frame information
117   // -- Selection DAG lowering information
118   //
119   virtual const TargetInstrInfo        *getInstrInfo() const { return 0; }
120   virtual const TargetFrameInfo        *getFrameInfo() const { return 0; }
121   virtual       TargetLowering    *getTargetLowering() 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     const TargetSubtarget *TST = getSubtargetImpl();
133     assert(TST && dynamic_cast<const STC*>(TST) &&
134            "Not the right kind of subtarget!");
135     return *static_cast<const STC*>(TST);
136   }
137
138   /// getRegisterInfo - If register information is available, return it.  If
139   /// not, return null.  This is kept separate from RegInfo until RegInfo has
140   /// details of graph coloring register allocation removed from it.
141   ///
142   virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
143   
144   /// getIntrinsicInfo - If intrinsic information is available, return it.  If
145   /// not, return null.
146   ///
147   virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
148
149   /// getJITInfo - If this target supports a JIT, return information for it,
150   /// otherwise return null.
151   ///
152   virtual TargetJITInfo *getJITInfo() { return 0; }
153   
154   /// getInstrItineraryData - Returns instruction itinerary data for the target
155   /// or specific subtarget.
156   ///
157   virtual const InstrItineraryData getInstrItineraryData() const {  
158     return InstrItineraryData();
159   }
160
161   /// getMachOWriterInfo - If this target supports a Mach-O writer, return
162   /// information for it, otherwise return null.
163   /// 
164   virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
165
166   /// getELFWriterInfo - If this target supports an ELF writer, return
167   /// information for it, otherwise return null.
168   /// 
169   virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
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   /// CodeGenFileType - These enums are meant to be passed into
196   /// addPassesToEmitFile to indicate what type of file to emit.
197   enum CodeGenFileType {
198     AssemblyFile, ObjectFile, DynamicLibrary
199   };
200
201   /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
202   /// on this target.  User flag overrides.
203   virtual bool getEnableTailMergeDefault() const { return true; }
204
205   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
206   /// specified file emitted.  Typically this will involve several steps of code
207   /// generation.
208   /// This method should return FileModel::Error if emission of this file type
209   /// is not supported.
210   ///
211   virtual FileModel::Model addPassesToEmitFile(PassManagerBase &,
212                                                formatted_raw_ostream &,
213                                                CodeGenFileType,
214                                                CodeGenOpt::Level) {
215     return FileModel::None;
216   }
217
218   /// addPassesToEmitFileFinish - If the passes to emit the specified file had
219   /// to be split up (e.g., to add an object writer pass), this method can be
220   /// used to finish up adding passes to emit the file, if necessary.
221   ///
222   virtual bool addPassesToEmitFileFinish(PassManagerBase &,
223                                          MachineCodeEmitter *,
224                                          CodeGenOpt::Level) {
225     return true;
226   }
227  
228   /// addPassesToEmitFileFinish - If the passes to emit the specified file had
229   /// to be split up (e.g., to add an object writer pass), this method can be
230   /// used to finish up adding passes to emit the file, if necessary.
231   ///
232   virtual bool addPassesToEmitFileFinish(PassManagerBase &,
233                                          JITCodeEmitter *,
234                                          CodeGenOpt::Level) {
235     return true;
236   }
237  
238   /// addPassesToEmitFileFinish - If the passes to emit the specified file had
239   /// to be split up (e.g., to add an object writer pass), this method can be
240   /// used to finish up adding passes to emit the file, if necessary.
241   ///
242   virtual bool addPassesToEmitFileFinish(PassManagerBase &,
243                                          ObjectCodeEmitter *,
244                                          CodeGenOpt::Level) {
245     return true;
246   }
247  
248   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
249   /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
250   /// actually outputting the machine code and resolving things like the address
251   /// of functions.  This method returns true if machine code emission is
252   /// not supported.
253   ///
254   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
255                                           MachineCodeEmitter &,
256                                           CodeGenOpt::Level) {
257     return true;
258   }
259
260   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
261   /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
262   /// actually outputting the machine code and resolving things like the address
263   /// of functions.  This method returns true if machine code emission is
264   /// not supported.
265   ///
266   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
267                                           JITCodeEmitter &,
268                                           CodeGenOpt::Level) {
269     return true;
270   }
271
272   /// addPassesToEmitWholeFile - This method can be implemented by targets that 
273   /// require having the entire module at once.  This is not recommended, do not
274   /// use this.
275   virtual bool WantsWholeFile() const { return false; }
276   virtual bool addPassesToEmitWholeFile(PassManager &, formatted_raw_ostream &,
277                                         CodeGenFileType,
278                                         CodeGenOpt::Level) {
279     return true;
280   }
281 };
282
283 /// LLVMTargetMachine - This class describes a target machine that is
284 /// implemented with the LLVM target-independent code generator.
285 ///
286 class LLVMTargetMachine : public TargetMachine {
287 protected: // Can only create subclasses.
288   LLVMTargetMachine(const Target &T, const std::string &TargetTriple);
289   
290   /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
291   /// both emitting to assembly files or machine code output.
292   ///
293   bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level);
294
295 public:
296   
297   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
298   /// specified file emitted.  Typically this will involve several steps of code
299   /// generation.  If OptLevel is None, the code generator should emit code as fast
300   /// as possible, though the generated code may be less efficient.  This method
301   /// should return FileModel::Error if emission of this file type is not
302   /// supported.
303   ///
304   /// The default implementation of this method adds components from the
305   /// LLVM retargetable code generator, invoking the methods below to get
306   /// target-specific passes in standard locations.
307   ///
308   virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
309                                                formatted_raw_ostream &Out,
310                                                CodeGenFileType FileType,
311                                                CodeGenOpt::Level);
312   
313   /// addPassesToEmitFileFinish - If the passes to emit the specified file had
314   /// to be split up (e.g., to add an object writer pass), this method can be
315   /// used to finish up adding passes to emit the file, if necessary.
316   ///
317   virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
318                                          MachineCodeEmitter *MCE,
319                                          CodeGenOpt::Level);
320  
321   /// addPassesToEmitFileFinish - If the passes to emit the specified file had
322   /// to be split up (e.g., to add an object writer pass), this method can be
323   /// used to finish up adding passes to emit the file, if necessary.
324   ///
325   virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
326                                          JITCodeEmitter *JCE,
327                                          CodeGenOpt::Level);
328  
329   /// addPassesToEmitFileFinish - If the passes to emit the specified file had
330   /// to be split up (e.g., to add an object writer pass), this method can be
331   /// used to finish up adding passes to emit the file, if necessary.
332   ///
333   virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
334                                          ObjectCodeEmitter *OCE,
335                                          CodeGenOpt::Level);
336  
337   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
338   /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
339   /// actually outputting the machine code and resolving things like the address
340   /// of functions.  This method returns true if machine code emission is
341   /// not supported.
342   ///
343   virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
344                                           MachineCodeEmitter &MCE,
345                                           CodeGenOpt::Level);
346   
347   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
348   /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
349   /// actually outputting the machine code and resolving things like the address
350   /// of functions.  This method returns true if machine code emission is
351   /// not supported.
352   ///
353   virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
354                                           JITCodeEmitter &MCE,
355                                           CodeGenOpt::Level);
356   
357   /// Target-Independent Code Generator Pass Configuration Options.
358   
359   /// addInstSelector - This method should add any "last minute" LLVM->LLVM
360   /// passes, then install an instruction selector pass, which converts from
361   /// LLVM code to machine instructions.
362   virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) {
363     return true;
364   }
365
366   /// addPreRegAlloc - This method may be implemented by targets that want to
367   /// run passes immediately before register allocation. This should return
368   /// true if -print-machineinstrs should print after these passes.
369   virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
370     return false;
371   }
372
373   /// addPostRegAlloc - This method may be implemented by targets that want
374   /// to run passes after register allocation but before prolog-epilog
375   /// insertion.  This should return true if -print-machineinstrs should print
376   /// after these passes.
377   virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
378     return false;
379   }
380
381   /// addPreSched2 - This method may be implemented by targets that want to
382   /// run passes after prolog-epilog insertion and before the second instruction
383   /// scheduling pass.  This should return true if -print-machineinstrs should
384   /// print after these passes.
385   virtual bool addPreSched2(PassManagerBase &, CodeGenOpt::Level) {
386     return false;
387   }
388   
389   /// addPreEmitPass - This pass may be implemented by targets that want to run
390   /// passes immediately before machine code is emitted.  This should return
391   /// true if -print-machineinstrs should print out the code after the passes.
392   virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
393     return false;
394   }
395   
396   
397   /// addCodeEmitter - This pass should be overridden by the target to add a
398   /// code emitter, if supported.  If this is not supported, 'true' should be
399   /// returned.
400   virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
401                               MachineCodeEmitter &) {
402     return true;
403   }
404
405   /// addCodeEmitter - This pass should be overridden by the target to add a
406   /// code emitter, if supported.  If this is not supported, 'true' should be
407   /// returned.
408   virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
409                               JITCodeEmitter &) {
410     return true;
411   }
412
413   /// addSimpleCodeEmitter - This pass should be overridden by the target to add
414   /// a code emitter (without setting flags), if supported.  If this is not
415   /// supported, 'true' should be returned.
416   virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
417                                     MachineCodeEmitter &) {
418     return true;
419   }
420
421   /// addSimpleCodeEmitter - This pass should be overridden by the target to add
422   /// a code emitter (without setting flags), if supported.  If this is not
423   /// supported, 'true' should be returned.
424   virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
425                                     JITCodeEmitter &) {
426     return true;
427   }
428
429   /// addSimpleCodeEmitter - This pass should be overridden by the target to add
430   /// a code emitter (without setting flags), if supported.  If this is not
431   /// supported, 'true' should be returned.
432   virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
433                                     ObjectCodeEmitter &) {
434     return true;
435   }
436
437   /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
438   /// on this target.  User flag overrides.
439   virtual bool getEnableTailMergeDefault() const { return true; }
440
441   /// addAssemblyEmitter - Helper function which creates a target specific
442   /// assembly printer, if available.
443   ///
444   /// \return Returns 'false' on success.
445   bool addAssemblyEmitter(PassManagerBase &, CodeGenOpt::Level,
446                           bool /* VerboseAsmDefault */,
447                           formatted_raw_ostream &);
448 };
449
450 } // End llvm namespace
451
452 #endif