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