• 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/Support/CodeGen.h"
18 #include "llvm/Target/TargetOptions.h"
19 #include "llvm/ADT/StringRef.h"
20 #include <cassert>
21 #include <string>
22 
23 namespace llvm {
24 
25 class InstrItineraryData;
26 class JITCodeEmitter;
27 class GlobalValue;
28 class MCAsmInfo;
29 class MCCodeGenInfo;
30 class MCContext;
31 class PassManagerBase;
32 class Target;
33 class TargetData;
34 class TargetELFWriterInfo;
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 formatted_raw_ostream;
45 class raw_ostream;
46 
47 //===----------------------------------------------------------------------===//
48 ///
49 /// TargetMachine - Primary interface to the complete machine description for
50 /// the target machine.  All target-specific information should be accessible
51 /// through this interface.
52 ///
53 class TargetMachine {
54   TargetMachine(const TargetMachine &);   // DO NOT IMPLEMENT
55   void operator=(const TargetMachine &);  // DO NOT IMPLEMENT
56 protected: // Can only create subclasses.
57   TargetMachine(const Target &T, StringRef TargetTriple,
58                 StringRef CPU, StringRef FS, const TargetOptions &Options);
59 
60   /// getSubtargetImpl - virtual method implemented by subclasses that returns
61   /// a reference to that target's TargetSubtargetInfo-derived member variable.
getSubtargetImpl()62   virtual const TargetSubtargetInfo *getSubtargetImpl() const { return 0; }
63 
64   /// TheTarget - The Target that this machine was created for.
65   const Target &TheTarget;
66 
67   /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
68   /// feature strings the TargetMachine instance is created with.
69   std::string TargetTriple;
70   std::string TargetCPU;
71   std::string TargetFS;
72 
73   /// CodeGenInfo - Low level target information such as relocation model.
74   const MCCodeGenInfo *CodeGenInfo;
75 
76   /// AsmInfo - Contains target specific asm information.
77   ///
78   const MCAsmInfo *AsmInfo;
79 
80   unsigned MCRelaxAll : 1;
81   unsigned MCNoExecStack : 1;
82   unsigned MCSaveTempLabels : 1;
83   unsigned MCUseLoc : 1;
84   unsigned MCUseCFI : 1;
85   unsigned MCUseDwarfDirectory : 1;
86 
87 public:
88   virtual ~TargetMachine();
89 
getTarget()90   const Target &getTarget() const { return TheTarget; }
91 
getTargetTriple()92   const StringRef getTargetTriple() const { return TargetTriple; }
getTargetCPU()93   const StringRef getTargetCPU() const { return TargetCPU; }
getTargetFeatureString()94   const StringRef getTargetFeatureString() const { return TargetFS; }
95 
96   TargetOptions Options;
97 
98   // Interfaces to the major aspects of target machine information:
99   // -- Instruction opcode and operand information
100   // -- Pipelines and scheduling information
101   // -- Stack frame information
102   // -- Selection DAG lowering information
103   //
getInstrInfo()104   virtual const TargetInstrInfo         *getInstrInfo() const { return 0; }
getFrameLowering()105   virtual const TargetFrameLowering *getFrameLowering() const { return 0; }
getTargetLowering()106   virtual const TargetLowering    *getTargetLowering() const { return 0; }
getSelectionDAGInfo()107   virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const{ return 0; }
getTargetData()108   virtual const TargetData             *getTargetData() const { return 0; }
109 
110   /// getMCAsmInfo - Return target specific asm information.
111   ///
getMCAsmInfo()112   const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
113 
114   /// getSubtarget - This method returns a pointer to the specified type of
115   /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
116   /// returned is of the correct type.
getSubtarget()117   template<typename STC> const STC &getSubtarget() const {
118     return *static_cast<const STC*>(getSubtargetImpl());
119   }
120 
121   /// getRegisterInfo - If register information is available, return it.  If
122   /// not, return null.  This is kept separate from RegInfo until RegInfo has
123   /// details of graph coloring register allocation removed from it.
124   ///
getRegisterInfo()125   virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
126 
127   /// getIntrinsicInfo - If intrinsic information is available, return it.  If
128   /// not, return null.
129   ///
getIntrinsicInfo()130   virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
131 
132   /// getJITInfo - If this target supports a JIT, return information for it,
133   /// otherwise return null.
134   ///
getJITInfo()135   virtual TargetJITInfo *getJITInfo() { return 0; }
136 
137   /// getInstrItineraryData - Returns instruction itinerary data for the target
138   /// or specific subtarget.
139   ///
getInstrItineraryData()140   virtual const InstrItineraryData *getInstrItineraryData() const {
141     return 0;
142   }
143 
144   /// getELFWriterInfo - If this target supports an ELF writer, return
145   /// information for it, otherwise return null.
146   ///
getELFWriterInfo()147   virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
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   /// 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     return true;
252   }
253 
254   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
255   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
256   /// actually outputting the machine code and resolving things like the address
257   /// of functions.  This method returns true if machine code emission is
258   /// not supported.
259   ///
260   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
261                                           JITCodeEmitter &,
262                                           bool /*DisableVerify*/ = true) {
263     return true;
264   }
265 
266   /// addPassesToEmitMC - Add passes to the specified pass manager to get
267   /// machine code emitted with the MCJIT. This method returns true if machine
268   /// code is not supported. It fills the MCContext Ctx pointer which can be
269   /// used to build custom MCStreamer.
270   ///
271   virtual bool addPassesToEmitMC(PassManagerBase &,
272                                  MCContext *&,
273                                  raw_ostream &,
274                                  bool /*DisableVerify*/ = true) {
275     return true;
276   }
277 };
278 
279 /// LLVMTargetMachine - This class describes a target machine that is
280 /// implemented with the LLVM target-independent code generator.
281 ///
282 class LLVMTargetMachine : public TargetMachine {
283 protected: // Can only create subclasses.
284   LLVMTargetMachine(const Target &T, StringRef TargetTriple,
285                     StringRef CPU, StringRef FS, TargetOptions Options,
286                     Reloc::Model RM, CodeModel::Model CM,
287                     CodeGenOpt::Level OL);
288 
289 public:
290   /// createPassConfig - Create a pass configuration object to be used by
291   /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
292   virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
293 
294   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
295   /// specified file emitted.  Typically this will involve several steps of code
296   /// generation.
297   virtual bool addPassesToEmitFile(PassManagerBase &PM,
298                                    formatted_raw_ostream &Out,
299                                    CodeGenFileType FileType,
300                                    bool DisableVerify = true);
301 
302   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
303   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
304   /// actually outputting the machine code and resolving things like the address
305   /// of functions.  This method returns true if machine code emission is
306   /// not supported.
307   ///
308   virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
309                                           JITCodeEmitter &MCE,
310                                           bool DisableVerify = true);
311 
312   /// addPassesToEmitMC - Add passes to the specified pass manager to get
313   /// machine code emitted with the MCJIT. This method returns true if machine
314   /// code is not supported. It fills the MCContext Ctx pointer which can be
315   /// used to build custom MCStreamer.
316   ///
317   virtual bool addPassesToEmitMC(PassManagerBase &PM,
318                                  MCContext *&Ctx,
319                                  raw_ostream &OS,
320                                  bool DisableVerify = true);
321 
322   /// addCodeEmitter - This pass should be overridden by the target to add a
323   /// code emitter, if supported.  If this is not supported, 'true' should be
324   /// returned.
addCodeEmitter(PassManagerBase &,JITCodeEmitter &)325   virtual bool addCodeEmitter(PassManagerBase &,
326                               JITCodeEmitter &) {
327     return true;
328   }
329 };
330 
331 } // End llvm namespace
332 
333 #endif
334