Add a TargetMachine local MCRegisterInfo and MCInstrInfo so that
[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/IR/DataLayout.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/CodeGen.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include <cassert>
23 #include <string>
24
25 namespace llvm {
26
27 class InstrItineraryData;
28 class GlobalValue;
29 class Mangler;
30 class MCAsmInfo;
31 class MCCodeGenInfo;
32 class MCContext;
33 class MCInstrInfo;
34 class MCRegisterInfo;
35 class MCSymbol;
36 class Target;
37 class DataLayout;
38 class TargetLibraryInfo;
39 class TargetFrameLowering;
40 class TargetIRAnalysis;
41 class TargetIntrinsicInfo;
42 class TargetLowering;
43 class TargetPassConfig;
44 class TargetRegisterInfo;
45 class TargetSelectionDAGInfo;
46 class TargetSubtargetInfo;
47 class TargetTransformInfo;
48 class formatted_raw_ostream;
49 class raw_ostream;
50 class TargetLoweringObjectFile;
51
52 // The old pass manager infrastructure is hidden in a legacy namespace now.
53 namespace legacy {
54 class PassManagerBase;
55 }
56 using legacy::PassManagerBase;
57
58 //===----------------------------------------------------------------------===//
59 ///
60 /// TargetMachine - Primary interface to the complete machine description for
61 /// the target machine.  All target-specific information should be accessible
62 /// through this interface.
63 ///
64 class TargetMachine {
65   TargetMachine(const TargetMachine &) = delete;
66   void operator=(const TargetMachine &) = delete;
67 protected: // Can only create subclasses.
68   TargetMachine(const Target &T, StringRef DataLayoutString,
69                 StringRef TargetTriple, StringRef CPU, StringRef FS,
70                 const TargetOptions &Options);
71
72   /// TheTarget - The Target that this machine was created for.
73   const Target &TheTarget;
74
75   /// DataLayout - For ABI type size and alignment.
76   const DataLayout DL;
77
78   /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
79   /// feature strings the TargetMachine instance is created with.
80   std::string TargetTriple;
81   std::string TargetCPU;
82   std::string TargetFS;
83
84   /// CodeGenInfo - Low level target information such as relocation model.
85   /// Non-const to allow resetting optimization level per-function.
86   MCCodeGenInfo *CodeGenInfo;
87
88   /// AsmInfo - Contains target specific asm information.
89   ///
90   const MCAsmInfo *AsmInfo;
91   const MCRegisterInfo *MRI;
92   const MCInstrInfo *MII;
93
94   unsigned RequireStructuredCFG : 1;
95
96 public:
97   mutable TargetOptions Options;
98
99   virtual ~TargetMachine();
100
101   const Target &getTarget() const { return TheTarget; }
102
103   StringRef getTargetTriple() const { return TargetTriple; }
104   StringRef getTargetCPU() const { return TargetCPU; }
105   StringRef getTargetFeatureString() const { return TargetFS; }
106
107   /// getSubtargetImpl - virtual method implemented by subclasses that returns
108   /// a reference to that target's TargetSubtargetInfo-derived member variable.
109   virtual const TargetSubtargetInfo *getSubtargetImpl() const {
110     return nullptr;
111   }
112   virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
113     return getSubtargetImpl();
114   }
115   virtual TargetLoweringObjectFile *getObjFileLowering() const {
116     return nullptr;
117   }
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.
122   template<typename STC> const STC &getSubtarget() const {
123     return *static_cast<const STC*>(getSubtargetImpl());
124   }
125   template <typename STC> const STC &getSubtarget(const Function &) const {
126     return *static_cast<const STC*>(getSubtargetImpl());
127   }
128
129   /// getDataLayout - This method returns a pointer to the DataLayout for
130   /// the target. It should be unchanging for every subtarget.
131   const DataLayout *getDataLayout() const { return &DL; }
132
133   /// \brief Reset the target options based on the function's attributes.
134   // FIXME: Remove TargetOptions that affect per-function code generation
135   // from TargetMachine.
136   void resetTargetOptions(const Function &F) const;
137
138   /// getMCAsmInfo - Return target specific asm information.
139   ///
140   const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
141   const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
142   const MCInstrInfo *getMCInstrInfo() const { return MII; }
143
144   /// getIntrinsicInfo - If intrinsic information is available, return it.  If
145   /// not, return null.
146   ///
147   virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
148     return nullptr;
149   }
150
151   bool requiresStructuredCFG() const { return RequireStructuredCFG; }
152   void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
153
154   /// getRelocationModel - Returns the code generation relocation model. The
155   /// choices are static, PIC, and dynamic-no-pic, and target default.
156   Reloc::Model getRelocationModel() const;
157
158   /// getCodeModel - Returns the code model. The choices are small, kernel,
159   /// medium, large, and target default.
160   CodeModel::Model getCodeModel() const;
161
162   /// getTLSModel - Returns the TLS model which should be used for the given
163   /// global variable.
164   TLSModel::Model getTLSModel(const GlobalValue *GV) const;
165
166   /// getOptLevel - Returns the optimization level: None, Less,
167   /// Default, or Aggressive.
168   CodeGenOpt::Level getOptLevel() const;
169
170   /// \brief Overrides the optimization level.
171   void setOptLevel(CodeGenOpt::Level Level) const;
172
173   void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
174
175   bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
176
177   /// Returns the default value of asm verbosity.
178   ///
179   bool getAsmVerbosityDefault() const {
180     return Options.MCOptions.AsmVerbose;
181   }
182
183   bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
184
185   /// Return true if data objects should be emitted into their own section,
186   /// corresponds to -fdata-sections.
187   bool getDataSections() const {
188     return Options.DataSections;
189   }
190
191   /// Return true if functions should be emitted into their own section,
192   /// corresponding to -ffunction-sections.
193   bool getFunctionSections() const {
194     return Options.FunctionSections;
195   }
196
197   /// \brief Get a \c TargetIRAnalysis appropriate for the target.
198   ///
199   /// This is used to construct the new pass manager's target IR analysis pass,
200   /// set up appropriately for this target machine. Even the old pass manager
201   /// uses this to answer queries about the IR.
202   virtual TargetIRAnalysis getTargetIRAnalysis();
203
204   /// CodeGenFileType - These enums are meant to be passed into
205   /// addPassesToEmitFile to indicate what type of file to emit, and returned by
206   /// it to indicate what type of file could actually be made.
207   enum CodeGenFileType {
208     CGFT_AssemblyFile,
209     CGFT_ObjectFile,
210     CGFT_Null         // Do not emit any output.
211   };
212
213   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
214   /// specified file emitted.  Typically this will involve several steps of code
215   /// generation.  This method should return true if emission of this file type
216   /// is not supported, or false on success.
217   virtual bool addPassesToEmitFile(PassManagerBase &,
218                                    formatted_raw_ostream &,
219                                    CodeGenFileType,
220                                    bool /*DisableVerify*/ = true,
221                                    AnalysisID /*StartAfter*/ = nullptr,
222                                    AnalysisID /*StopAfter*/ = nullptr) {
223     return true;
224   }
225
226   /// addPassesToEmitMC - Add passes to the specified pass manager to get
227   /// machine code emitted with the MCJIT. This method returns true if machine
228   /// code is not supported. It fills the MCContext Ctx pointer which can be
229   /// used to build custom MCStreamer.
230   ///
231   virtual bool addPassesToEmitMC(PassManagerBase &,
232                                  MCContext *&,
233                                  raw_ostream &,
234                                  bool /*DisableVerify*/ = true) {
235     return true;
236   }
237
238   void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
239                          Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
240   MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
241 };
242
243 /// LLVMTargetMachine - This class describes a target machine that is
244 /// implemented with the LLVM target-independent code generator.
245 ///
246 class LLVMTargetMachine : public TargetMachine {
247 protected: // Can only create subclasses.
248   LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
249                     StringRef TargetTriple, StringRef CPU, StringRef FS,
250                     TargetOptions Options, Reloc::Model RM, CodeModel::Model CM,
251                     CodeGenOpt::Level OL);
252
253   void initAsmInfo();
254 public:
255   /// \brief Get a TargetIRAnalysis implementation for the target.
256   ///
257   /// This analysis will produce a TTI result which uses the common code
258   /// generator to answer queries about the IR.
259   TargetIRAnalysis getTargetIRAnalysis() override;
260
261   /// createPassConfig - Create a pass configuration object to be used by
262   /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
263   virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
264
265   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
266   /// specified file emitted.  Typically this will involve several steps of code
267   /// generation.
268   bool addPassesToEmitFile(PassManagerBase &PM, formatted_raw_ostream &Out,
269                            CodeGenFileType FileType, bool DisableVerify = true,
270                            AnalysisID StartAfter = nullptr,
271                            AnalysisID StopAfter = nullptr) override;
272
273   /// addPassesToEmitMC - Add passes to the specified pass manager to get
274   /// machine code emitted with the MCJIT. This method returns true if machine
275   /// code is not supported. It fills the MCContext Ctx pointer which can be
276   /// used to build custom MCStreamer.
277   ///
278   bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
279                          raw_ostream &OS, bool DisableVerify = true) override;
280 };
281
282 } // End llvm namespace
283
284 #endif