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