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