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