[C++11] More 'nullptr' conversion. In some cases just using a boolean check instead...
[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/Pass.h"
19 #include "llvm/Support/CodeGen.h"
20 #include "llvm/Target/TargetOptions.h"
21 #include <cassert>
22 #include <string>
23
24 namespace llvm {
25
26 class InstrItineraryData;
27 class JITCodeEmitter;
28 class GlobalValue;
29 class Mangler;
30 class MCAsmInfo;
31 class MCCodeGenInfo;
32 class MCContext;
33 class MCSymbol;
34 class Target;
35 class DataLayout;
36 class TargetLibraryInfo;
37 class TargetFrameLowering;
38 class TargetInstrInfo;
39 class TargetIntrinsicInfo;
40 class TargetJITInfo;
41 class TargetLowering;
42 class TargetPassConfig;
43 class TargetRegisterInfo;
44 class TargetSelectionDAGInfo;
45 class TargetSubtargetInfo;
46 class ScalarTargetTransformInfo;
47 class VectorTargetTransformInfo;
48 class formatted_raw_ostream;
49 class raw_ostream;
50
51 // The old pass manager infrastructure is hidden in a legacy namespace now.
52 namespace legacy {
53 class PassManagerBase;
54 }
55 using legacy::PassManagerBase;
56
57 //===----------------------------------------------------------------------===//
58 ///
59 /// TargetMachine - Primary interface to the complete machine description for
60 /// the target machine.  All target-specific information should be accessible
61 /// through this interface.
62 ///
63 class TargetMachine {
64   TargetMachine(const TargetMachine &) LLVM_DELETED_FUNCTION;
65   void operator=(const TargetMachine &) LLVM_DELETED_FUNCTION;
66 protected: // Can only create subclasses.
67   TargetMachine(const Target &T, StringRef TargetTriple,
68                 StringRef CPU, StringRef FS, const TargetOptions &Options);
69
70   /// TheTarget - The Target that this machine was created for.
71   const Target &TheTarget;
72
73   /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
74   /// feature strings the TargetMachine instance is created with.
75   std::string TargetTriple;
76   std::string TargetCPU;
77   std::string TargetFS;
78
79   /// CodeGenInfo - Low level target information such as relocation model.
80   /// Non-const to allow resetting optimization level per-function.
81   MCCodeGenInfo *CodeGenInfo;
82
83   /// AsmInfo - Contains target specific asm information.
84   ///
85   const MCAsmInfo *AsmInfo;
86
87   unsigned MCRelaxAll : 1;
88   unsigned MCNoExecStack : 1;
89   unsigned MCSaveTempLabels : 1;
90   unsigned MCUseCFI : 1;
91   unsigned MCUseDwarfDirectory : 1;
92   unsigned RequireStructuredCFG : 1;
93
94 public:
95   virtual ~TargetMachine();
96
97   const Target &getTarget() const { return TheTarget; }
98
99   const StringRef getTargetTriple() const { return TargetTriple; }
100   const StringRef getTargetCPU() const { return TargetCPU; }
101   const StringRef getTargetFeatureString() const { return TargetFS; }
102
103   /// getSubtargetImpl - virtual method implemented by subclasses that returns
104   /// a reference to that target's TargetSubtargetInfo-derived member variable.
105   virtual const TargetSubtargetInfo *getSubtargetImpl() const {
106     return nullptr;
107   }
108
109   mutable TargetOptions Options;
110
111   /// \brief Reset the target options based on the function's attributes.
112   void resetTargetOptions(const MachineFunction *MF) const;
113
114   // Interfaces to the major aspects of target machine information:
115   //
116   // -- Instruction opcode and operand information
117   // -- Pipelines and scheduling information
118   // -- Stack frame information
119   // -- Selection DAG lowering information
120   //
121   // N.B. These objects may change during compilation. It's not safe to cache
122   // them between functions.
123   virtual const TargetInstrInfo  *getInstrInfo() const { return nullptr; }
124   virtual const TargetFrameLowering *getFrameLowering() const {
125     return nullptr;
126   }
127   virtual const TargetLowering *getTargetLowering() const { return nullptr; }
128   virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const {
129     return nullptr;
130   }
131   virtual const DataLayout *getDataLayout() const { return nullptr; }
132
133   /// getMCAsmInfo - Return target specific asm information.
134   ///
135   const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
136
137   /// getSubtarget - This method returns a pointer to the specified type of
138   /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
139   /// returned is of the correct type.
140   template<typename STC> const STC &getSubtarget() const {
141     return *static_cast<const STC*>(getSubtargetImpl());
142   }
143
144   /// getRegisterInfo - If register information is available, return it.  If
145   /// not, return null.  This is kept separate from RegInfo until RegInfo has
146   /// details of graph coloring register allocation removed from it.
147   ///
148   virtual const TargetRegisterInfo *getRegisterInfo() const { return nullptr; }
149
150   /// getIntrinsicInfo - If intrinsic information is available, return it.  If
151   /// not, return null.
152   ///
153   virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return nullptr;}
154
155   /// getJITInfo - If this target supports a JIT, return information for it,
156   /// otherwise return null.
157   ///
158   virtual TargetJITInfo *getJITInfo() { return nullptr; }
159
160   /// getInstrItineraryData - Returns instruction itinerary data for the target
161   /// or specific subtarget.
162   ///
163   virtual const InstrItineraryData *getInstrItineraryData() const {
164     return nullptr;
165   }
166
167   bool requiresStructuredCFG() const { return RequireStructuredCFG; }
168   void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
169
170   /// hasMCRelaxAll - Check whether all machine code instructions should be
171   /// relaxed.
172   bool hasMCRelaxAll() const { return MCRelaxAll; }
173
174   /// setMCRelaxAll - Set whether all machine code instructions should be
175   /// relaxed.
176   void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
177
178   /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
179   /// (i.e., not treated as temporary).
180   bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
181
182   /// setMCSaveTempLabels - Set whether temporary labels will be preserved
183   /// (i.e., not treated as temporary).
184   void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
185
186   /// hasMCNoExecStack - Check whether an executable stack is not needed.
187   bool hasMCNoExecStack() const { return MCNoExecStack; }
188
189   /// setMCNoExecStack - Set whether an executabel stack is not needed.
190   void setMCNoExecStack(bool Value) { MCNoExecStack = Value; }
191
192   /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives.
193   bool hasMCUseCFI() const { return MCUseCFI; }
194
195   /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives.
196   void setMCUseCFI(bool Value) { MCUseCFI = Value; }
197
198   /// hasMCUseDwarfDirectory - Check whether we should use .file directives with
199   /// explicit directories.
200   bool hasMCUseDwarfDirectory() const { return MCUseDwarfDirectory; }
201
202   /// setMCUseDwarfDirectory - Set whether all we should use .file directives
203   /// with explicit directories.
204   void setMCUseDwarfDirectory(bool Value) { MCUseDwarfDirectory = Value; }
205
206   /// getRelocationModel - Returns the code generation relocation model. The
207   /// choices are static, PIC, and dynamic-no-pic, and target default.
208   Reloc::Model getRelocationModel() const;
209
210   /// getCodeModel - Returns the code model. The choices are small, kernel,
211   /// medium, large, and target default.
212   CodeModel::Model getCodeModel() const;
213
214   /// getTLSModel - Returns the TLS model which should be used for the given
215   /// global variable.
216   TLSModel::Model getTLSModel(const GlobalValue *GV) const;
217
218   /// getOptLevel - Returns the optimization level: None, Less,
219   /// Default, or Aggressive.
220   CodeGenOpt::Level getOptLevel() const;
221
222   /// \brief Overrides the optimization level.
223   void setOptLevel(CodeGenOpt::Level Level) const;
224
225   void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
226
227   bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
228
229   /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
230   ///
231   static bool getAsmVerbosityDefault();
232
233   /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
234   /// is false.
235   static void setAsmVerbosityDefault(bool);
236
237   /// getDataSections - Return true if data objects should be emitted into their
238   /// own section, corresponds to -fdata-sections.
239   static bool getDataSections();
240
241   /// getFunctionSections - Return true if functions should be emitted into
242   /// their own section, corresponding to -ffunction-sections.
243   static bool getFunctionSections();
244
245   /// setDataSections - Set if the data are emit into separate sections.
246   static void setDataSections(bool);
247
248   /// setFunctionSections - Set if the functions are emit into separate
249   /// sections.
250   static void setFunctionSections(bool);
251
252   /// \brief Register analysis passes for this target with a pass manager.
253   virtual void addAnalysisPasses(PassManagerBase &) {}
254
255   /// CodeGenFileType - These enums are meant to be passed into
256   /// addPassesToEmitFile to indicate what type of file to emit, and returned by
257   /// it to indicate what type of file could actually be made.
258   enum CodeGenFileType {
259     CGFT_AssemblyFile,
260     CGFT_ObjectFile,
261     CGFT_Null         // Do not emit any output.
262   };
263
264   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
265   /// specified file emitted.  Typically this will involve several steps of code
266   /// generation.  This method should return true if emission of this file type
267   /// is not supported, or false on success.
268   virtual bool addPassesToEmitFile(PassManagerBase &,
269                                    formatted_raw_ostream &,
270                                    CodeGenFileType,
271                                    bool /*DisableVerify*/ = true,
272                                    AnalysisID /*StartAfter*/ = nullptr,
273                                    AnalysisID /*StopAfter*/ = nullptr) {
274     return true;
275   }
276
277   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
278   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
279   /// actually outputting the machine code and resolving things like the address
280   /// of functions.  This method returns true if machine code emission is
281   /// not supported.
282   ///
283   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
284                                           JITCodeEmitter &,
285                                           bool /*DisableVerify*/ = true) {
286     return true;
287   }
288
289   /// addPassesToEmitMC - Add passes to the specified pass manager to get
290   /// machine code emitted with the MCJIT. This method returns true if machine
291   /// code is not supported. It fills the MCContext Ctx pointer which can be
292   /// used to build custom MCStreamer.
293   ///
294   virtual bool addPassesToEmitMC(PassManagerBase &,
295                                  MCContext *&,
296                                  raw_ostream &,
297                                  bool /*DisableVerify*/ = true) {
298     return true;
299   }
300
301   void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
302                          Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
303   MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
304 };
305
306 /// LLVMTargetMachine - This class describes a target machine that is
307 /// implemented with the LLVM target-independent code generator.
308 ///
309 class LLVMTargetMachine : public TargetMachine {
310 protected: // Can only create subclasses.
311   LLVMTargetMachine(const Target &T, StringRef TargetTriple,
312                     StringRef CPU, StringRef FS, TargetOptions Options,
313                     Reloc::Model RM, CodeModel::Model CM,
314                     CodeGenOpt::Level OL);
315
316   void initAsmInfo();
317 public:
318   /// \brief Register analysis passes for this target with a pass manager.
319   ///
320   /// This registers target independent analysis passes.
321   void addAnalysisPasses(PassManagerBase &PM) override;
322
323   /// createPassConfig - Create a pass configuration object to be used by
324   /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
325   virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
326
327   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
328   /// specified file emitted.  Typically this will involve several steps of code
329   /// generation.
330   bool addPassesToEmitFile(PassManagerBase &PM, formatted_raw_ostream &Out,
331                            CodeGenFileType FileType, bool DisableVerify = true,
332                            AnalysisID StartAfter = nullptr,
333                            AnalysisID StopAfter = nullptr) override;
334
335   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
336   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
337   /// actually outputting the machine code and resolving things like the address
338   /// of functions.  This method returns true if machine code emission is
339   /// not supported.
340   ///
341   bool addPassesToEmitMachineCode(PassManagerBase &PM, JITCodeEmitter &MCE,
342                                   bool DisableVerify = true) override;
343
344   /// addPassesToEmitMC - Add passes to the specified pass manager to get
345   /// machine code emitted with the MCJIT. This method returns true if machine
346   /// code is not supported. It fills the MCContext Ctx pointer which can be
347   /// used to build custom MCStreamer.
348   ///
349   bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
350                          raw_ostream &OS, bool DisableVerify = true) override;
351
352   /// addCodeEmitter - This pass should be overridden by the target to add a
353   /// code emitter, if supported.  If this is not supported, 'true' should be
354   /// returned.
355   virtual bool addCodeEmitter(PassManagerBase &,
356                               JITCodeEmitter &) {
357     return true;
358   }
359 };
360
361 } // End llvm namespace
362
363 #endif