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