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