Reorder to keep data and routines separate and to keep a couple of
[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 TargetIntrinsicInfo;
39 class TargetJITInfo;
40 class TargetLowering;
41 class TargetPassConfig;
42 class TargetRegisterInfo;
43 class TargetSelectionDAGInfo;
44 class TargetSubtargetInfo;
45 class ScalarTargetTransformInfo;
46 class VectorTargetTransformInfo;
47 class formatted_raw_ostream;
48 class raw_ostream;
49
50 // The old pass manager infrastructure is hidden in a legacy namespace now.
51 namespace legacy {
52 class PassManagerBase;
53 }
54 using legacy::PassManagerBase;
55
56 //===----------------------------------------------------------------------===//
57 ///
58 /// TargetMachine - Primary interface to the complete machine description for
59 /// the target machine.  All target-specific information should be accessible
60 /// through this interface.
61 ///
62 class TargetMachine {
63   TargetMachine(const TargetMachine &) LLVM_DELETED_FUNCTION;
64   void operator=(const TargetMachine &) LLVM_DELETED_FUNCTION;
65 protected: // Can only create subclasses.
66   TargetMachine(const Target &T, StringRef TargetTriple,
67                 StringRef CPU, StringRef FS, const TargetOptions &Options);
68
69   /// TheTarget - The Target that this machine was created for.
70   const Target &TheTarget;
71
72   /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
73   /// feature strings the TargetMachine instance is created with.
74   std::string TargetTriple;
75   std::string TargetCPU;
76   std::string TargetFS;
77
78   /// CodeGenInfo - Low level target information such as relocation model.
79   /// Non-const to allow resetting optimization level per-function.
80   MCCodeGenInfo *CodeGenInfo;
81
82   /// AsmInfo - Contains target specific asm information.
83   ///
84   const MCAsmInfo *AsmInfo;
85
86   unsigned RequireStructuredCFG : 1;
87
88 public:
89   mutable TargetOptions Options;
90
91   virtual ~TargetMachine();
92
93   const Target &getTarget() const { return TheTarget; }
94
95   const StringRef getTargetTriple() const { return TargetTriple; }
96   const StringRef getTargetCPU() const { return TargetCPU; }
97   const StringRef getTargetFeatureString() const { return TargetFS; }
98
99   /// getSubtargetImpl - virtual method implemented by subclasses that returns
100   /// a reference to that target's TargetSubtargetInfo-derived member variable.
101   virtual const TargetSubtargetInfo *getSubtargetImpl() const {
102     return nullptr;
103   }
104   virtual TargetSubtargetInfo *getSubtargetImpl() {
105     const TargetMachine *TM = this;
106     return const_cast<TargetSubtargetInfo *>(TM->getSubtargetImpl());
107   }
108
109   /// getSubtarget - This method returns a pointer to the specified type of
110   /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
111   /// returned is of the correct type.
112   template<typename STC> const STC &getSubtarget() const {
113     return *static_cast<const STC*>(getSubtargetImpl());
114   }
115
116   /// \brief Reset the target options based on the function's attributes.
117   void resetTargetOptions(const MachineFunction *MF) const;
118
119   /// getMCAsmInfo - Return target specific asm information.
120   ///
121   const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
122
123   /// getIntrinsicInfo - If intrinsic information is available, return it.  If
124   /// not, return null.
125   ///
126   virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
127     return nullptr;
128   }
129
130   bool requiresStructuredCFG() const { return RequireStructuredCFG; }
131   void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
132
133   /// getRelocationModel - Returns the code generation relocation model. The
134   /// choices are static, PIC, and dynamic-no-pic, and target default.
135   Reloc::Model getRelocationModel() const;
136
137   /// getCodeModel - Returns the code model. The choices are small, kernel,
138   /// medium, large, and target default.
139   CodeModel::Model getCodeModel() const;
140
141   /// getTLSModel - Returns the TLS model which should be used for the given
142   /// global variable.
143   TLSModel::Model getTLSModel(const GlobalValue *GV) const;
144
145   /// getOptLevel - Returns the optimization level: None, Less,
146   /// Default, or Aggressive.
147   CodeGenOpt::Level getOptLevel() const;
148
149   /// \brief Overrides the optimization level.
150   void setOptLevel(CodeGenOpt::Level Level) const;
151
152   void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
153
154   bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
155
156   /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
157   ///
158   bool getAsmVerbosityDefault() const ;
159
160   /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
161   /// is false.
162   void setAsmVerbosityDefault(bool);
163
164   /// getDataSections - Return true if data objects should be emitted into their
165   /// own section, corresponds to -fdata-sections.
166   bool getDataSections() const;
167
168   /// getFunctionSections - Return true if functions should be emitted into
169   /// their own section, corresponding to -ffunction-sections.
170   bool getFunctionSections() const;
171
172   /// setDataSections - Set if the data are emit into separate sections.
173   void setDataSections(bool);
174
175   /// setFunctionSections - Set if the functions are emit into separate
176   /// sections.
177   void setFunctionSections(bool);
178
179   /// \brief Register analysis passes for this target with a pass manager.
180   virtual void addAnalysisPasses(PassManagerBase &) {}
181
182   /// CodeGenFileType - These enums are meant to be passed into
183   /// addPassesToEmitFile to indicate what type of file to emit, and returned by
184   /// it to indicate what type of file could actually be made.
185   enum CodeGenFileType {
186     CGFT_AssemblyFile,
187     CGFT_ObjectFile,
188     CGFT_Null         // Do not emit any output.
189   };
190
191   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
192   /// specified file emitted.  Typically this will involve several steps of code
193   /// generation.  This method should return true if emission of this file type
194   /// is not supported, or false on success.
195   virtual bool addPassesToEmitFile(PassManagerBase &,
196                                    formatted_raw_ostream &,
197                                    CodeGenFileType,
198                                    bool /*DisableVerify*/ = true,
199                                    AnalysisID /*StartAfter*/ = nullptr,
200                                    AnalysisID /*StopAfter*/ = nullptr) {
201     return true;
202   }
203
204   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
205   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
206   /// actually outputting the machine code and resolving things like the address
207   /// of functions.  This method returns true if machine code emission is
208   /// not supported.
209   ///
210   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
211                                           JITCodeEmitter &,
212                                           bool /*DisableVerify*/ = true) {
213     return true;
214   }
215
216   /// addPassesToEmitMC - Add passes to the specified pass manager to get
217   /// machine code emitted with the MCJIT. This method returns true if machine
218   /// code is not supported. It fills the MCContext Ctx pointer which can be
219   /// used to build custom MCStreamer.
220   ///
221   virtual bool addPassesToEmitMC(PassManagerBase &,
222                                  MCContext *&,
223                                  raw_ostream &,
224                                  bool /*DisableVerify*/ = true) {
225     return true;
226   }
227
228   void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
229                          Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
230   MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
231 };
232
233 /// LLVMTargetMachine - This class describes a target machine that is
234 /// implemented with the LLVM target-independent code generator.
235 ///
236 class LLVMTargetMachine : public TargetMachine {
237 protected: // Can only create subclasses.
238   LLVMTargetMachine(const Target &T, StringRef TargetTriple,
239                     StringRef CPU, StringRef FS, TargetOptions Options,
240                     Reloc::Model RM, CodeModel::Model CM,
241                     CodeGenOpt::Level OL);
242
243   void initAsmInfo();
244 public:
245   /// \brief Register analysis passes for this target with a pass manager.
246   ///
247   /// This registers target independent analysis passes.
248   void addAnalysisPasses(PassManagerBase &PM) override;
249
250   /// createPassConfig - Create a pass configuration object to be used by
251   /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
252   virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
253
254   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
255   /// specified file emitted.  Typically this will involve several steps of code
256   /// generation.
257   bool addPassesToEmitFile(PassManagerBase &PM, formatted_raw_ostream &Out,
258                            CodeGenFileType FileType, bool DisableVerify = true,
259                            AnalysisID StartAfter = nullptr,
260                            AnalysisID StopAfter = nullptr) override;
261
262   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
263   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
264   /// actually outputting the machine code and resolving things like the address
265   /// of functions.  This method returns true if machine code emission is
266   /// not supported.
267   ///
268   bool addPassesToEmitMachineCode(PassManagerBase &PM, JITCodeEmitter &MCE,
269                                   bool DisableVerify = true) override;
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   bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
277                          raw_ostream &OS, bool DisableVerify = true) override;
278
279   /// addCodeEmitter - This pass should be overridden by the target to add a
280   /// code emitter, if supported.  If this is not supported, 'true' should be
281   /// returned.
282   virtual bool addCodeEmitter(PassManagerBase &,
283                               JITCodeEmitter &) {
284     return true;
285   }
286 };
287
288 } // End llvm namespace
289
290 #endif