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