1 //===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the TargetMachine and LLVMTargetMachine classes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TARGET_TARGETMACHINE_H
15 #define LLVM_TARGET_TARGETMACHINE_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/CodeGen.h"
20 #include "llvm/Target/TargetOptions.h"
26 class InstrItineraryData;
36 class TargetLibraryInfo;
37 class TargetFrameLowering;
38 class TargetInstrInfo;
39 class TargetIntrinsicInfo;
42 class TargetPassConfig;
43 class TargetRegisterInfo;
44 class TargetSelectionDAGInfo;
45 class TargetSubtargetInfo;
46 class ScalarTargetTransformInfo;
47 class VectorTargetTransformInfo;
48 class formatted_raw_ostream;
51 // The old pass manager infrastructure is hidden in a legacy namespace now.
53 class PassManagerBase;
55 using legacy::PassManagerBase;
57 //===----------------------------------------------------------------------===//
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.
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);
70 /// TheTarget - The Target that this machine was created for.
71 const Target &TheTarget;
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;
79 /// CodeGenInfo - Low level target information such as relocation model.
80 /// Non-const to allow resetting optimization level per-function.
81 MCCodeGenInfo *CodeGenInfo;
83 /// AsmInfo - Contains target specific asm information.
85 const MCAsmInfo *AsmInfo;
87 unsigned RequireStructuredCFG : 1;
90 virtual ~TargetMachine();
92 const Target &getTarget() const { return TheTarget; }
94 const StringRef getTargetTriple() const { return TargetTriple; }
95 const StringRef getTargetCPU() const { return TargetCPU; }
96 const StringRef getTargetFeatureString() const { return TargetFS; }
98 /// getSubtargetImpl - virtual method implemented by subclasses that returns
99 /// a reference to that target's TargetSubtargetInfo-derived member variable.
100 virtual const TargetSubtargetInfo *getSubtargetImpl() const {
104 mutable TargetOptions Options;
106 /// \brief Reset the target options based on the function's attributes.
107 void resetTargetOptions(const MachineFunction *MF) const;
109 // Interfaces to the major aspects of target machine information:
111 // -- Instruction opcode and operand information
112 // -- Pipelines and scheduling information
113 // -- Stack frame information
114 // -- Selection DAG lowering information
116 // N.B. These objects may change during compilation. It's not safe to cache
117 // them between functions.
118 virtual const TargetInstrInfo *getInstrInfo() const { return nullptr; }
119 virtual const TargetFrameLowering *getFrameLowering() const {
122 virtual const TargetLowering *getTargetLowering() const { return nullptr; }
123 virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const {
126 virtual const DataLayout *getDataLayout() const { return nullptr; }
128 /// getMCAsmInfo - Return target specific asm information.
130 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
132 /// getSubtarget - This method returns a pointer to the specified type of
133 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
134 /// returned is of the correct type.
135 template<typename STC> const STC &getSubtarget() const {
136 return *static_cast<const STC*>(getSubtargetImpl());
139 /// getRegisterInfo - If register information is available, return it. If
140 /// not, return null. This is kept separate from RegInfo until RegInfo has
141 /// details of graph coloring register allocation removed from it.
143 virtual const TargetRegisterInfo *getRegisterInfo() const { return nullptr; }
145 /// getIntrinsicInfo - If intrinsic information is available, return it. If
146 /// not, return null.
148 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return nullptr;}
150 /// getJITInfo - If this target supports a JIT, return information for it,
151 /// otherwise return null.
153 virtual TargetJITInfo *getJITInfo() { return nullptr; }
155 /// getInstrItineraryData - Returns instruction itinerary data for the target
156 /// or specific subtarget.
158 virtual const InstrItineraryData *getInstrItineraryData() const {
162 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
163 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
165 /// hasMCRelaxAll - Check whether all machine code instructions should be
167 bool hasMCRelaxAll() const { return Options.MCOptions.MCRelaxAll; }
169 /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
170 /// (i.e., not treated as temporary).
171 bool hasMCSaveTempLabels() const { return Options.MCOptions.MCSaveTempLabels; }
173 /// hasMCNoExecStack - Check whether an executable stack is not needed.
174 bool hasMCNoExecStack() const { return Options.MCOptions.MCNoExecStack; }
176 /// hasMCUseDwarfDirectory - Check whether we should use .file directives with
177 /// explicit directories.
178 bool hasMCUseDwarfDirectory() const { return Options.MCOptions.MCUseDwarfDirectory; }
180 /// getRelocationModel - Returns the code generation relocation model. The
181 /// choices are static, PIC, and dynamic-no-pic, and target default.
182 Reloc::Model getRelocationModel() const;
184 /// getCodeModel - Returns the code model. The choices are small, kernel,
185 /// medium, large, and target default.
186 CodeModel::Model getCodeModel() const;
188 /// getTLSModel - Returns the TLS model which should be used for the given
190 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
192 /// getOptLevel - Returns the optimization level: None, Less,
193 /// Default, or Aggressive.
194 CodeGenOpt::Level getOptLevel() const;
196 /// \brief Overrides the optimization level.
197 void setOptLevel(CodeGenOpt::Level Level) const;
199 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
201 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
203 /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
205 static bool getAsmVerbosityDefault();
207 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
209 static void setAsmVerbosityDefault(bool);
211 /// getDataSections - Return true if data objects should be emitted into their
212 /// own section, corresponds to -fdata-sections.
213 static bool getDataSections();
215 /// getFunctionSections - Return true if functions should be emitted into
216 /// their own section, corresponding to -ffunction-sections.
217 static bool getFunctionSections();
219 /// setDataSections - Set if the data are emit into separate sections.
220 static void setDataSections(bool);
222 /// setFunctionSections - Set if the functions are emit into separate
224 static void setFunctionSections(bool);
226 /// \brief Register analysis passes for this target with a pass manager.
227 virtual void addAnalysisPasses(PassManagerBase &) {}
229 /// CodeGenFileType - These enums are meant to be passed into
230 /// addPassesToEmitFile to indicate what type of file to emit, and returned by
231 /// it to indicate what type of file could actually be made.
232 enum CodeGenFileType {
235 CGFT_Null // Do not emit any output.
238 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
239 /// specified file emitted. Typically this will involve several steps of code
240 /// generation. This method should return true if emission of this file type
241 /// is not supported, or false on success.
242 virtual bool addPassesToEmitFile(PassManagerBase &,
243 formatted_raw_ostream &,
245 bool /*DisableVerify*/ = true,
246 AnalysisID /*StartAfter*/ = nullptr,
247 AnalysisID /*StopAfter*/ = nullptr) {
251 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
252 /// get machine code emitted. This uses a JITCodeEmitter object to handle
253 /// actually outputting the machine code and resolving things like the address
254 /// of functions. This method returns true if machine code emission is
257 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
259 bool /*DisableVerify*/ = true) {
263 /// addPassesToEmitMC - Add passes to the specified pass manager to get
264 /// machine code emitted with the MCJIT. This method returns true if machine
265 /// code is not supported. It fills the MCContext Ctx pointer which can be
266 /// used to build custom MCStreamer.
268 virtual bool addPassesToEmitMC(PassManagerBase &,
271 bool /*DisableVerify*/ = true) {
275 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
276 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
277 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
280 /// LLVMTargetMachine - This class describes a target machine that is
281 /// implemented with the LLVM target-independent code generator.
283 class LLVMTargetMachine : public TargetMachine {
284 protected: // Can only create subclasses.
285 LLVMTargetMachine(const Target &T, StringRef TargetTriple,
286 StringRef CPU, StringRef FS, TargetOptions Options,
287 Reloc::Model RM, CodeModel::Model CM,
288 CodeGenOpt::Level OL);
292 /// \brief Register analysis passes for this target with a pass manager.
294 /// This registers target independent analysis passes.
295 void addAnalysisPasses(PassManagerBase &PM) override;
297 /// createPassConfig - Create a pass configuration object to be used by
298 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
299 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
301 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
302 /// specified file emitted. Typically this will involve several steps of code
304 bool addPassesToEmitFile(PassManagerBase &PM, formatted_raw_ostream &Out,
305 CodeGenFileType FileType, bool DisableVerify = true,
306 AnalysisID StartAfter = nullptr,
307 AnalysisID StopAfter = nullptr) override;
309 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
310 /// get machine code emitted. This uses a JITCodeEmitter object to handle
311 /// actually outputting the machine code and resolving things like the address
312 /// of functions. This method returns true if machine code emission is
315 bool addPassesToEmitMachineCode(PassManagerBase &PM, JITCodeEmitter &MCE,
316 bool DisableVerify = true) override;
318 /// addPassesToEmitMC - Add passes to the specified pass manager to get
319 /// machine code emitted with the MCJIT. This method returns true if machine
320 /// code is not supported. It fills the MCContext Ctx pointer which can be
321 /// used to build custom MCStreamer.
323 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
324 raw_ostream &OS, bool DisableVerify = true) override;
326 /// addCodeEmitter - This pass should be overridden by the target to add a
327 /// code emitter, if supported. If this is not supported, 'true' should be
329 virtual bool addCodeEmitter(PassManagerBase &,
335 } // End llvm namespace