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