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