7cbc87d9a8b472b576f5eb29b11a0574267335c1
[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   /// Non-const to allow resetting optimization level per-function.
79   MCCodeGenInfo *CodeGenInfo;
80
81   /// AsmInfo - Contains target specific asm information.
82   ///
83   const MCAsmInfo *AsmInfo;
84
85   unsigned MCRelaxAll : 1;
86   unsigned MCNoExecStack : 1;
87   unsigned MCSaveTempLabels : 1;
88   unsigned MCUseLoc : 1;
89   unsigned MCUseCFI : 1;
90   unsigned MCUseDwarfDirectory : 1;
91   unsigned DebugUseUniqueSections : 1;
92   unsigned RequireStructuredCFG : 1;
93
94 public:
95   virtual ~TargetMachine();
96
97   const Target &getTarget() const { return TheTarget; }
98
99   const StringRef getTargetTriple() const { return TargetTriple; }
100   const StringRef getTargetCPU() const { return TargetCPU; }
101   const StringRef getTargetFeatureString() const { return TargetFS; }
102
103   /// getSubtargetImpl - virtual method implemented by subclasses that returns
104   /// a reference to that target's TargetSubtargetInfo-derived member variable.
105   virtual const TargetSubtargetInfo *getSubtargetImpl() const { return 0; }
106
107   mutable TargetOptions Options;
108
109   /// \brief Reset the target options based on the function's attributes.
110   void resetTargetOptions(const MachineFunction *MF) const;
111
112   // Interfaces to the major aspects of target machine information:
113   //
114   // -- Instruction opcode and operand information
115   // -- Pipelines and scheduling information
116   // -- Stack frame information
117   // -- Selection DAG lowering information
118   //
119   // N.B. These objects may change during compilation. It's not safe to cache
120   // them between functions.
121   virtual const TargetInstrInfo         *getInstrInfo() const { return 0; }
122   virtual const TargetFrameLowering *getFrameLowering() const { return 0; }
123   virtual const TargetLowering    *getTargetLowering() const { return 0; }
124   virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const{ return 0; }
125   virtual const DataLayout             *getDataLayout() const { return 0; }
126
127   /// getMCAsmInfo - Return target specific asm information.
128   ///
129   const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
130
131   /// getSubtarget - This method returns a pointer to the specified type of
132   /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
133   /// returned is of the correct type.
134   template<typename STC> const STC &getSubtarget() const {
135     return *static_cast<const STC*>(getSubtargetImpl());
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 0;
159   }
160
161   bool requiresStructuredCFG() const { return RequireStructuredCFG; }
162   void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
163
164   bool debugUseUniqueSections() const { return DebugUseUniqueSections; }
165   void setDebugUseUniqueSections(bool Value) { DebugUseUniqueSections = Value; }
166
167   /// hasMCRelaxAll - Check whether all machine code instructions should be
168   /// relaxed.
169   bool hasMCRelaxAll() const { return MCRelaxAll; }
170
171   /// setMCRelaxAll - Set whether all machine code instructions should be
172   /// relaxed.
173   void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
174
175   /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
176   /// (i.e., not treated as temporary).
177   bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
178
179   /// setMCSaveTempLabels - Set whether temporary labels will be preserved
180   /// (i.e., not treated as temporary).
181   void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
182
183   /// hasMCNoExecStack - Check whether an executable stack is not needed.
184   bool hasMCNoExecStack() const { return MCNoExecStack; }
185
186   /// setMCNoExecStack - Set whether an executabel stack is not needed.
187   void setMCNoExecStack(bool Value) { MCNoExecStack = Value; }
188
189   /// hasMCUseLoc - Check whether we should use dwarf's .loc directive.
190   bool hasMCUseLoc() const { return MCUseLoc; }
191
192   /// setMCUseLoc - Set whether all we should use dwarf's .loc directive.
193   void setMCUseLoc(bool Value) { MCUseLoc = Value; }
194
195   /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives.
196   bool hasMCUseCFI() const { return MCUseCFI; }
197
198   /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives.
199   void setMCUseCFI(bool Value) { MCUseCFI = Value; }
200
201   /// hasMCUseDwarfDirectory - Check whether we should use .file directives with
202   /// explicit directories.
203   bool hasMCUseDwarfDirectory() const { return MCUseDwarfDirectory; }
204
205   /// setMCUseDwarfDirectory - Set whether all we should use .file directives
206   /// with explicit directories.
207   void setMCUseDwarfDirectory(bool Value) { MCUseDwarfDirectory = Value; }
208
209   /// getRelocationModel - Returns the code generation relocation model. The
210   /// choices are static, PIC, and dynamic-no-pic, and target default.
211   Reloc::Model getRelocationModel() const;
212
213   /// getCodeModel - Returns the code model. The choices are small, kernel,
214   /// medium, large, and target default.
215   CodeModel::Model getCodeModel() const;
216
217   /// getTLSModel - Returns the TLS model which should be used for the given
218   /// global variable.
219   TLSModel::Model getTLSModel(const GlobalValue *GV) const;
220
221   /// getOptLevel - Returns the optimization level: None, Less,
222   /// Default, or Aggressive.
223   CodeGenOpt::Level getOptLevel() const;
224
225   /// \brief Overrides the optimization level.
226   void setOptLevel(CodeGenOpt::Level Level) const;
227
228   void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
229
230   bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
231
232   /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
233   ///
234   static bool getAsmVerbosityDefault();
235
236   /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
237   /// is false.
238   static void setAsmVerbosityDefault(bool);
239
240   /// getDataSections - Return true if data objects should be emitted into their
241   /// own section, corresponds to -fdata-sections.
242   static bool getDataSections();
243
244   /// getFunctionSections - Return true if functions should be emitted into
245   /// their own section, corresponding to -ffunction-sections.
246   static bool getFunctionSections();
247
248   /// setDataSections - Set if the data are emit into separate sections.
249   static void setDataSections(bool);
250
251   /// setFunctionSections - Set if the functions are emit into separate
252   /// sections.
253   static void setFunctionSections(bool);
254
255   /// \brief Register analysis passes for this target with a pass manager.
256   virtual void addAnalysisPasses(PassManagerBase &) {}
257
258   /// CodeGenFileType - These enums are meant to be passed into
259   /// addPassesToEmitFile to indicate what type of file to emit, and returned by
260   /// it to indicate what type of file could actually be made.
261   enum CodeGenFileType {
262     CGFT_AssemblyFile,
263     CGFT_ObjectFile,
264     CGFT_Null         // Do not emit any output.
265   };
266
267   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
268   /// specified file emitted.  Typically this will involve several steps of code
269   /// generation.  This method should return true if emission of this file type
270   /// is not supported, or false on success.
271   virtual bool addPassesToEmitFile(PassManagerBase &,
272                                    formatted_raw_ostream &,
273                                    CodeGenFileType,
274                                    bool /*DisableVerify*/ = true,
275                                    AnalysisID /*StartAfter*/ = 0,
276                                    AnalysisID /*StopAfter*/ = 0) {
277     return true;
278   }
279
280   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
281   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
282   /// actually outputting the machine code and resolving things like the address
283   /// of functions.  This method returns true if machine code emission is
284   /// not supported.
285   ///
286   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
287                                           JITCodeEmitter &,
288                                           bool /*DisableVerify*/ = true) {
289     return true;
290   }
291
292   /// addPassesToEmitMC - Add passes to the specified pass manager to get
293   /// machine code emitted with the MCJIT. This method returns true if machine
294   /// code is not supported. It fills the MCContext Ctx pointer which can be
295   /// used to build custom MCStreamer.
296   ///
297   virtual bool addPassesToEmitMC(PassManagerBase &,
298                                  MCContext *&,
299                                  raw_ostream &,
300                                  bool /*DisableVerify*/ = true) {
301     return true;
302   }
303 };
304
305 /// LLVMTargetMachine - This class describes a target machine that is
306 /// implemented with the LLVM target-independent code generator.
307 ///
308 class LLVMTargetMachine : public TargetMachine {
309 protected: // Can only create subclasses.
310   LLVMTargetMachine(const Target &T, StringRef TargetTriple,
311                     StringRef CPU, StringRef FS, TargetOptions Options,
312                     Reloc::Model RM, CodeModel::Model CM,
313                     CodeGenOpt::Level OL);
314
315   void initAsmInfo();
316 public:
317   /// \brief Register analysis passes for this target with a pass manager.
318   ///
319   /// This registers target independent analysis passes.
320   virtual void addAnalysisPasses(PassManagerBase &PM);
321
322   /// createPassConfig - Create a pass configuration object to be used by
323   /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
324   virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
325
326   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
327   /// specified file emitted.  Typically this will involve several steps of code
328   /// generation.
329   virtual bool addPassesToEmitFile(PassManagerBase &PM,
330                                    formatted_raw_ostream &Out,
331                                    CodeGenFileType FileType,
332                                    bool DisableVerify = true,
333                                    AnalysisID StartAfter = 0,
334                                    AnalysisID StopAfter = 0);
335
336   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
337   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
338   /// actually outputting the machine code and resolving things like the address
339   /// of functions.  This method returns true if machine code emission is
340   /// not supported.
341   ///
342   virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
343                                           JITCodeEmitter &MCE,
344                                           bool DisableVerify = true);
345
346   /// addPassesToEmitMC - Add passes to the specified pass manager to get
347   /// machine code emitted with the MCJIT. This method returns true if machine
348   /// code is not supported. It fills the MCContext Ctx pointer which can be
349   /// used to build custom MCStreamer.
350   ///
351   virtual bool addPassesToEmitMC(PassManagerBase &PM,
352                                  MCContext *&Ctx,
353                                  raw_ostream &OS,
354                                  bool DisableVerify = true);
355
356   /// addCodeEmitter - This pass should be overridden by the target to add a
357   /// code emitter, if supported.  If this is not supported, 'true' should be
358   /// returned.
359   virtual bool addCodeEmitter(PassManagerBase &,
360                               JITCodeEmitter &) {
361     return true;
362   }
363 };
364
365 } // End llvm namespace
366
367 #endif