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