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