Add a new interface to allow IR-level passes to access codegen-specific information.
[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/Pass.h"
18 #include "llvm/Support/CodeGen.h"
19 #include "llvm/Target/TargetOptions.h"
20 #include "llvm/TargetTransformInfo.h"
21 #include "llvm/Target/TargetTransformImpl.h"
22 #include "llvm/ADT/StringRef.h"
23 #include <cassert>
24 #include <string>
25
26 namespace llvm {
27
28 class InstrItineraryData;
29 class JITCodeEmitter;
30 class GlobalValue;
31 class MCAsmInfo;
32 class MCCodeGenInfo;
33 class MCContext;
34 class PassManagerBase;
35 class Target;
36 class DataLayout;
37 class TargetELFWriterInfo;
38 class TargetFrameLowering;
39 class TargetInstrInfo;
40 class TargetIntrinsicInfo;
41 class TargetJITInfo;
42 class TargetLowering;
43 class TargetPassConfig;
44 class TargetRegisterInfo;
45 class TargetSelectionDAGInfo;
46 class TargetSubtargetInfo;
47 class formatted_raw_ostream;
48 class raw_ostream;
49
50 //===----------------------------------------------------------------------===//
51 ///
52 /// TargetMachine - Primary interface to the complete machine description for
53 /// the target machine.  All target-specific information should be accessible
54 /// through this interface.
55 ///
56 class TargetMachine {
57   TargetMachine(const TargetMachine &) LLVM_DELETED_FUNCTION;
58   void operator=(const TargetMachine &) LLVM_DELETED_FUNCTION;
59 protected: // Can only create subclasses.
60   TargetMachine(const Target &T, StringRef TargetTriple,
61                 StringRef CPU, StringRef FS, const TargetOptions &Options);
62
63   /// getSubtargetImpl - virtual method implemented by subclasses that returns
64   /// a reference to that target's TargetSubtargetInfo-derived member variable.
65   virtual const TargetSubtargetInfo *getSubtargetImpl() const { return 0; }
66
67   /// TheTarget - The Target that this machine was created for.
68   const Target &TheTarget;
69
70   /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
71   /// feature strings the TargetMachine instance is created with.
72   std::string TargetTriple;
73   std::string TargetCPU;
74   std::string TargetFS;
75
76   /// CodeGenInfo - Low level target information such as relocation model.
77   const MCCodeGenInfo *CodeGenInfo;
78
79   /// AsmInfo - Contains target specific asm information.
80   ///
81   const MCAsmInfo *AsmInfo;
82
83   unsigned MCRelaxAll : 1;
84   unsigned MCNoExecStack : 1;
85   unsigned MCSaveTempLabels : 1;
86   unsigned MCUseLoc : 1;
87   unsigned MCUseCFI : 1;
88   unsigned MCUseDwarfDirectory : 1;
89
90 public:
91   virtual ~TargetMachine();
92
93   const Target &getTarget() const { return TheTarget; }
94
95   const StringRef getTargetTriple() const { return TargetTriple; }
96   const StringRef getTargetCPU() const { return TargetCPU; }
97   const StringRef getTargetFeatureString() const { return TargetFS; }
98
99   TargetOptions Options;
100
101   // Interfaces to the major aspects of target machine information:
102   // -- Instruction opcode and operand information
103   // -- Pipelines and scheduling information
104   // -- Stack frame information
105   // -- Selection DAG lowering information
106   //
107   virtual const TargetInstrInfo         *getInstrInfo() const { return 0; }
108   virtual const TargetFrameLowering *getFrameLowering() const { return 0; }
109   virtual const TargetLowering    *getTargetLowering() const { return 0; }
110   virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const{ return 0; }
111   virtual const DataLayout             *getDataLayout() const { return 0; }
112   virtual const ScalarTargetTransformInfo*
113   getScalarTargetTransformInfo() const { return 0; }
114   virtual const VectorTargetTransformInfo*
115   getVectorTargetTransformInfo() const { return 0; }
116
117   /// getMCAsmInfo - Return target specific asm information.
118   ///
119   const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
120
121   /// getSubtarget - This method returns a pointer to the specified type of
122   /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
123   /// returned is of the correct type.
124   template<typename STC> const STC &getSubtarget() const {
125     return *static_cast<const STC*>(getSubtargetImpl());
126   }
127
128   /// getRegisterInfo - If register information is available, return it.  If
129   /// not, return null.  This is kept separate from RegInfo until RegInfo has
130   /// details of graph coloring register allocation removed from it.
131   ///
132   virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
133
134   /// getIntrinsicInfo - If intrinsic information is available, return it.  If
135   /// not, return null.
136   ///
137   virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
138
139   /// getJITInfo - If this target supports a JIT, return information for it,
140   /// otherwise return null.
141   ///
142   virtual TargetJITInfo *getJITInfo() { return 0; }
143
144   /// getInstrItineraryData - Returns instruction itinerary data for the target
145   /// or specific subtarget.
146   ///
147   virtual const InstrItineraryData *getInstrItineraryData() const {
148     return 0;
149   }
150
151   /// getELFWriterInfo - If this target supports an ELF writer, return
152   /// information for it, otherwise return null.
153   ///
154   virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
155
156   /// hasMCRelaxAll - Check whether all machine code instructions should be
157   /// relaxed.
158   bool hasMCRelaxAll() const { return MCRelaxAll; }
159
160   /// setMCRelaxAll - Set whether all machine code instructions should be
161   /// relaxed.
162   void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
163
164   /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
165   /// (i.e., not treated as temporary).
166   bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
167
168   /// setMCSaveTempLabels - Set whether temporary labels will be preserved
169   /// (i.e., not treated as temporary).
170   void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
171
172   /// hasMCNoExecStack - Check whether an executable stack is not needed.
173   bool hasMCNoExecStack() const { return MCNoExecStack; }
174
175   /// setMCNoExecStack - Set whether an executabel stack is not needed.
176   void setMCNoExecStack(bool Value) { MCNoExecStack = Value; }
177
178   /// hasMCUseLoc - Check whether we should use dwarf's .loc directive.
179   bool hasMCUseLoc() const { return MCUseLoc; }
180
181   /// setMCUseLoc - Set whether all we should use dwarf's .loc directive.
182   void setMCUseLoc(bool Value) { MCUseLoc = Value; }
183
184   /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives.
185   bool hasMCUseCFI() const { return MCUseCFI; }
186
187   /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives.
188   void setMCUseCFI(bool Value) { MCUseCFI = Value; }
189
190   /// hasMCUseDwarfDirectory - Check whether we should use .file directives with
191   /// explicit directories.
192   bool hasMCUseDwarfDirectory() const { return MCUseDwarfDirectory; }
193
194   /// setMCUseDwarfDirectory - Set whether all we should use .file directives
195   /// with explicit directories.
196   void setMCUseDwarfDirectory(bool Value) { MCUseDwarfDirectory = Value; }
197
198   /// getRelocationModel - Returns the code generation relocation model. The
199   /// choices are static, PIC, and dynamic-no-pic, and target default.
200   Reloc::Model getRelocationModel() const;
201
202   /// getCodeModel - Returns the code model. The choices are small, kernel,
203   /// medium, large, and target default.
204   CodeModel::Model getCodeModel() const;
205
206   /// getTLSModel - Returns the TLS model which should be used for the given
207   /// global variable.
208   TLSModel::Model getTLSModel(const GlobalValue *GV) const;
209
210   /// getOptLevel - Returns the optimization level: None, Less,
211   /// Default, or Aggressive.
212   CodeGenOpt::Level getOptLevel() const;
213
214   void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
215
216   bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
217
218   /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
219   ///
220   static bool getAsmVerbosityDefault();
221
222   /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
223   /// is false.
224   static void setAsmVerbosityDefault(bool);
225
226   /// getDataSections - Return true if data objects should be emitted into their
227   /// own section, corresponds to -fdata-sections.
228   static bool getDataSections();
229
230   /// getFunctionSections - Return true if functions should be emitted into
231   /// their own section, corresponding to -ffunction-sections.
232   static bool getFunctionSections();
233
234   /// setDataSections - Set if the data are emit into separate sections.
235   static void setDataSections(bool);
236
237   /// setFunctionSections - Set if the functions are emit into separate
238   /// sections.
239   static void setFunctionSections(bool);
240
241   /// CodeGenFileType - These enums are meant to be passed into
242   /// addPassesToEmitFile to indicate what type of file to emit, and returned by
243   /// it to indicate what type of file could actually be made.
244   enum CodeGenFileType {
245     CGFT_AssemblyFile,
246     CGFT_ObjectFile,
247     CGFT_Null         // Do not emit any output.
248   };
249
250   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
251   /// specified file emitted.  Typically this will involve several steps of code
252   /// generation.  This method should return true if emission of this file type
253   /// is not supported, or false on success.
254   virtual bool addPassesToEmitFile(PassManagerBase &,
255                                    formatted_raw_ostream &,
256                                    CodeGenFileType,
257                                    bool /*DisableVerify*/ = true,
258                                    AnalysisID StartAfter = 0,
259                                    AnalysisID StopAfter = 0) {
260     return true;
261   }
262
263   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
264   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
265   /// actually outputting the machine code and resolving things like the address
266   /// of functions.  This method returns true if machine code emission is
267   /// not supported.
268   ///
269   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
270                                           JITCodeEmitter &,
271                                           bool /*DisableVerify*/ = true) {
272     return true;
273   }
274
275   /// addPassesToEmitMC - Add passes to the specified pass manager to get
276   /// machine code emitted with the MCJIT. This method returns true if machine
277   /// code is not supported. It fills the MCContext Ctx pointer which can be
278   /// used to build custom MCStreamer.
279   ///
280   virtual bool addPassesToEmitMC(PassManagerBase &,
281                                  MCContext *&,
282                                  raw_ostream &,
283                                  bool /*DisableVerify*/ = true) {
284     return true;
285   }
286 };
287
288 /// LLVMTargetMachine - This class describes a target machine that is
289 /// implemented with the LLVM target-independent code generator.
290 ///
291 class LLVMTargetMachine : public TargetMachine {
292 protected: // Can only create subclasses.
293   LLVMTargetMachine(const Target &T, StringRef TargetTriple,
294                     StringRef CPU, StringRef FS, TargetOptions Options,
295                     Reloc::Model RM, CodeModel::Model CM,
296                     CodeGenOpt::Level OL);
297
298 public:
299   /// createPassConfig - Create a pass configuration object to be used by
300   /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
301   virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
302
303   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
304   /// specified file emitted.  Typically this will involve several steps of code
305   /// generation.
306   virtual bool addPassesToEmitFile(PassManagerBase &PM,
307                                    formatted_raw_ostream &Out,
308                                    CodeGenFileType FileType,
309                                    bool DisableVerify = true,
310                                    AnalysisID StartAfter = 0,
311                                    AnalysisID StopAfter = 0);
312
313   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
314   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
315   /// actually outputting the machine code and resolving things like the address
316   /// of functions.  This method returns true if machine code emission is
317   /// not supported.
318   ///
319   virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
320                                           JITCodeEmitter &MCE,
321                                           bool DisableVerify = true);
322
323   /// addPassesToEmitMC - Add passes to the specified pass manager to get
324   /// machine code emitted with the MCJIT. This method returns true if machine
325   /// code is not supported. It fills the MCContext Ctx pointer which can be
326   /// used to build custom MCStreamer.
327   ///
328   virtual bool addPassesToEmitMC(PassManagerBase &PM,
329                                  MCContext *&Ctx,
330                                  raw_ostream &OS,
331                                  bool DisableVerify = true);
332
333   /// addCodeEmitter - This pass should be overridden by the target to add a
334   /// code emitter, if supported.  If this is not supported, 'true' should be
335   /// returned.
336   virtual bool addCodeEmitter(PassManagerBase &,
337                               JITCodeEmitter &) {
338     return true;
339   }
340 };
341
342 } // End llvm namespace
343
344 #endif