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