85b009afe88fbf762e9a22cb3fe91ccc754a4fd7
[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 Mangler;
30 class MCAsmInfo;
31 class MCCodeGenInfo;
32 class MCContext;
33 class MCSymbol;
34 class Target;
35 class DataLayout;
36 class TargetLibraryInfo;
37 class TargetFrameLowering;
38 class TargetInstrInfo;
39 class TargetIntrinsicInfo;
40 class TargetJITInfo;
41 class TargetLowering;
42 class TargetPassConfig;
43 class TargetRegisterInfo;
44 class TargetSelectionDAGInfo;
45 class TargetSubtargetInfo;
46 class ScalarTargetTransformInfo;
47 class VectorTargetTransformInfo;
48 class formatted_raw_ostream;
49 class raw_ostream;
50
51 // The old pass manager infrastructure is hidden in a legacy namespace now.
52 namespace legacy {
53 class PassManagerBase;
54 }
55 using legacy::PassManagerBase;
56
57 //===----------------------------------------------------------------------===//
58 ///
59 /// TargetMachine - Primary interface to the complete machine description for
60 /// the target machine.  All target-specific information should be accessible
61 /// through this interface.
62 ///
63 class TargetMachine {
64   TargetMachine(const TargetMachine &) LLVM_DELETED_FUNCTION;
65   void operator=(const TargetMachine &) LLVM_DELETED_FUNCTION;
66 protected: // Can only create subclasses.
67   TargetMachine(const Target &T, StringRef TargetTriple,
68                 StringRef CPU, StringRef FS, const TargetOptions &Options);
69
70   /// TheTarget - The Target that this machine was created for.
71   const Target &TheTarget;
72
73   /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
74   /// feature strings the TargetMachine instance is created with.
75   std::string TargetTriple;
76   std::string TargetCPU;
77   std::string TargetFS;
78
79   /// CodeGenInfo - Low level target information such as relocation model.
80   /// Non-const to allow resetting optimization level per-function.
81   MCCodeGenInfo *CodeGenInfo;
82
83   /// AsmInfo - Contains target specific asm information.
84   ///
85   const MCAsmInfo *AsmInfo;
86
87   unsigned MCRelaxAll : 1;
88   unsigned MCNoExecStack : 1;
89   unsigned MCSaveTempLabels : 1;
90   unsigned MCUseCFI : 1;
91   unsigned MCUseDwarfDirectory : 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   /// hasMCRelaxAll - Check whether all machine code instructions should be
165   /// relaxed.
166   bool hasMCRelaxAll() const { return MCRelaxAll; }
167
168   /// setMCRelaxAll - Set whether all machine code instructions should be
169   /// relaxed.
170   void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
171
172   /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
173   /// (i.e., not treated as temporary).
174   bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
175
176   /// setMCSaveTempLabels - Set whether temporary labels will be preserved
177   /// (i.e., not treated as temporary).
178   void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
179
180   /// hasMCNoExecStack - Check whether an executable stack is not needed.
181   bool hasMCNoExecStack() const { return MCNoExecStack; }
182
183   /// setMCNoExecStack - Set whether an executabel stack is not needed.
184   void setMCNoExecStack(bool Value) { MCNoExecStack = 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   /// \brief Overrides the optimization level.
217   void setOptLevel(CodeGenOpt::Level Level) const;
218
219   void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
220
221   bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
222
223   /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
224   ///
225   static bool getAsmVerbosityDefault();
226
227   /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
228   /// is false.
229   static void setAsmVerbosityDefault(bool);
230
231   /// getDataSections - Return true if data objects should be emitted into their
232   /// own section, corresponds to -fdata-sections.
233   static bool getDataSections();
234
235   /// getFunctionSections - Return true if functions should be emitted into
236   /// their own section, corresponding to -ffunction-sections.
237   static bool getFunctionSections();
238
239   /// setDataSections - Set if the data are emit into separate sections.
240   static void setDataSections(bool);
241
242   /// setFunctionSections - Set if the functions are emit into separate
243   /// sections.
244   static void setFunctionSections(bool);
245
246   /// \brief Register analysis passes for this target with a pass manager.
247   virtual void addAnalysisPasses(PassManagerBase &) {}
248
249   /// CodeGenFileType - These enums are meant to be passed into
250   /// addPassesToEmitFile to indicate what type of file to emit, and returned by
251   /// it to indicate what type of file could actually be made.
252   enum CodeGenFileType {
253     CGFT_AssemblyFile,
254     CGFT_ObjectFile,
255     CGFT_Null         // Do not emit any output.
256   };
257
258   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
259   /// specified file emitted.  Typically this will involve several steps of code
260   /// generation.  This method should return true if emission of this file type
261   /// is not supported, or false on success.
262   virtual bool addPassesToEmitFile(PassManagerBase &,
263                                    formatted_raw_ostream &,
264                                    CodeGenFileType,
265                                    bool /*DisableVerify*/ = true,
266                                    AnalysisID /*StartAfter*/ = 0,
267                                    AnalysisID /*StopAfter*/ = 0) {
268     return true;
269   }
270
271   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
272   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
273   /// actually outputting the machine code and resolving things like the address
274   /// of functions.  This method returns true if machine code emission is
275   /// not supported.
276   ///
277   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
278                                           JITCodeEmitter &,
279                                           bool /*DisableVerify*/ = true) {
280     return true;
281   }
282
283   /// addPassesToEmitMC - Add passes to the specified pass manager to get
284   /// machine code emitted with the MCJIT. This method returns true if machine
285   /// code is not supported. It fills the MCContext Ctx pointer which can be
286   /// used to build custom MCStreamer.
287   ///
288   virtual bool addPassesToEmitMC(PassManagerBase &,
289                                  MCContext *&,
290                                  raw_ostream &,
291                                  bool /*DisableVerify*/ = true) {
292     return true;
293   }
294
295   void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
296                          Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
297   MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
298 };
299
300 /// LLVMTargetMachine - This class describes a target machine that is
301 /// implemented with the LLVM target-independent code generator.
302 ///
303 class LLVMTargetMachine : public TargetMachine {
304 protected: // Can only create subclasses.
305   LLVMTargetMachine(const Target &T, StringRef TargetTriple,
306                     StringRef CPU, StringRef FS, TargetOptions Options,
307                     Reloc::Model RM, CodeModel::Model CM,
308                     CodeGenOpt::Level OL);
309
310   void initAsmInfo();
311 public:
312   /// \brief Register analysis passes for this target with a pass manager.
313   ///
314   /// This registers target independent analysis passes.
315   virtual void addAnalysisPasses(PassManagerBase &PM);
316
317   /// createPassConfig - Create a pass configuration object to be used by
318   /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
319   virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
320
321   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
322   /// specified file emitted.  Typically this will involve several steps of code
323   /// generation.
324   virtual bool addPassesToEmitFile(PassManagerBase &PM,
325                                    formatted_raw_ostream &Out,
326                                    CodeGenFileType FileType,
327                                    bool DisableVerify = true,
328                                    AnalysisID StartAfter = 0,
329                                    AnalysisID StopAfter = 0);
330
331   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
332   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
333   /// actually outputting the machine code and resolving things like the address
334   /// of functions.  This method returns true if machine code emission is
335   /// not supported.
336   ///
337   virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
338                                           JITCodeEmitter &MCE,
339                                           bool DisableVerify = true);
340
341   /// addPassesToEmitMC - Add passes to the specified pass manager to get
342   /// machine code emitted with the MCJIT. This method returns true if machine
343   /// code is not supported. It fills the MCContext Ctx pointer which can be
344   /// used to build custom MCStreamer.
345   ///
346   virtual bool addPassesToEmitMC(PassManagerBase &PM,
347                                  MCContext *&Ctx,
348                                  raw_ostream &OS,
349                                  bool DisableVerify = true);
350
351   /// addCodeEmitter - This pass should be overridden by the target to add a
352   /// code emitter, if supported.  If this is not supported, 'true' should be
353   /// returned.
354   virtual bool addCodeEmitter(PassManagerBase &,
355                               JITCodeEmitter &) {
356     return true;
357   }
358 };
359
360 } // End llvm namespace
361
362 #endif