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