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 MCRelaxAll : 1;
88 unsigned MCNoExecStack : 1;
89 unsigned MCSaveTempLabels : 1;
90 unsigned MCUseCFI : 1;
91 unsigned MCUseDwarfDirectory : 1;
92 unsigned RequireStructuredCFG : 1;
95 virtual ~TargetMachine();
97 const Target &getTarget() const { return TheTarget; }
99 const StringRef getTargetTriple() const { return TargetTriple; }
100 const StringRef getTargetCPU() const { return TargetCPU; }
101 const StringRef getTargetFeatureString() const { return TargetFS; }
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 { return 0; }
107 mutable TargetOptions Options;
109 /// \brief Reset the target options based on the function's attributes.
110 void resetTargetOptions(const MachineFunction *MF) const;
112 // Interfaces to the major aspects of target machine information:
114 // -- Instruction opcode and operand information
115 // -- Pipelines and scheduling information
116 // -- Stack frame information
117 // -- Selection DAG lowering information
119 // N.B. These objects may change during compilation. It's not safe to cache
120 // them between functions.
121 virtual const TargetInstrInfo *getInstrInfo() const { return 0; }
122 virtual const TargetFrameLowering *getFrameLowering() const { return 0; }
123 virtual const TargetLowering *getTargetLowering() const { return 0; }
124 virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const{ return 0; }
125 virtual const DataLayout *getDataLayout() const { return 0; }
127 /// getMCAsmInfo - Return target specific asm information.
129 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
131 /// getSubtarget - This method returns a pointer to the specified type of
132 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
133 /// returned is of the correct type.
134 template<typename STC> const STC &getSubtarget() const {
135 return *static_cast<const STC*>(getSubtargetImpl());
138 /// getRegisterInfo - If register information is available, return it. If
139 /// not, return null. This is kept separate from RegInfo until RegInfo has
140 /// details of graph coloring register allocation removed from it.
142 virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
144 /// getIntrinsicInfo - If intrinsic information is available, return it. If
145 /// not, return null.
147 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
149 /// getJITInfo - If this target supports a JIT, return information for it,
150 /// otherwise return null.
152 virtual TargetJITInfo *getJITInfo() { return 0; }
154 /// getInstrItineraryData - Returns instruction itinerary data for the target
155 /// or specific subtarget.
157 virtual const InstrItineraryData *getInstrItineraryData() const {
161 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
162 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
164 /// hasMCRelaxAll - Check whether all machine code instructions should be
166 bool hasMCRelaxAll() const { return MCRelaxAll; }
168 /// setMCRelaxAll - Set whether all machine code instructions should be
170 void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
172 /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
173 /// (i.e., not treated as temporary).
174 bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
176 /// setMCSaveTempLabels - Set whether temporary labels will be preserved
177 /// (i.e., not treated as temporary).
178 void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
180 /// hasMCNoExecStack - Check whether an executable stack is not needed.
181 bool hasMCNoExecStack() const { return MCNoExecStack; }
183 /// setMCNoExecStack - Set whether an executabel stack is not needed.
184 void setMCNoExecStack(bool Value) { MCNoExecStack = Value; }
186 /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives.
187 bool hasMCUseCFI() const { return MCUseCFI; }
189 /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives.
190 void setMCUseCFI(bool Value) { MCUseCFI = Value; }
192 /// hasMCUseDwarfDirectory - Check whether we should use .file directives with
193 /// explicit directories.
194 bool hasMCUseDwarfDirectory() const { return MCUseDwarfDirectory; }
196 /// setMCUseDwarfDirectory - Set whether all we should use .file directives
197 /// with explicit directories.
198 void setMCUseDwarfDirectory(bool Value) { MCUseDwarfDirectory = Value; }
200 /// getRelocationModel - Returns the code generation relocation model. The
201 /// choices are static, PIC, and dynamic-no-pic, and target default.
202 Reloc::Model getRelocationModel() const;
204 /// getCodeModel - Returns the code model. The choices are small, kernel,
205 /// medium, large, and target default.
206 CodeModel::Model getCodeModel() const;
208 /// getTLSModel - Returns the TLS model which should be used for the given
210 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
212 /// getOptLevel - Returns the optimization level: None, Less,
213 /// Default, or Aggressive.
214 CodeGenOpt::Level getOptLevel() const;
216 /// \brief Overrides the optimization level.
217 void setOptLevel(CodeGenOpt::Level Level) const;
219 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
221 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
223 /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
225 static bool getAsmVerbosityDefault();
227 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
229 static void setAsmVerbosityDefault(bool);
231 /// getDataSections - Return true if data objects should be emitted into their
232 /// own section, corresponds to -fdata-sections.
233 static bool getDataSections();
235 /// getFunctionSections - Return true if functions should be emitted into
236 /// their own section, corresponding to -ffunction-sections.
237 static bool getFunctionSections();
239 /// setDataSections - Set if the data are emit into separate sections.
240 static void setDataSections(bool);
242 /// setFunctionSections - Set if the functions are emit into separate
244 static void setFunctionSections(bool);
246 /// \brief Register analysis passes for this target with a pass manager.
247 virtual void addAnalysisPasses(PassManagerBase &) {}
249 /// CodeGenFileType - These enums are meant to be passed into
250 /// addPassesToEmitFile to indicate what type of file to emit, and returned by
251 /// it to indicate what type of file could actually be made.
252 enum CodeGenFileType {
255 CGFT_Null // Do not emit any output.
258 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
259 /// specified file emitted. Typically this will involve several steps of code
260 /// generation. This method should return true if emission of this file type
261 /// is not supported, or false on success.
262 virtual bool addPassesToEmitFile(PassManagerBase &,
263 formatted_raw_ostream &,
265 bool /*DisableVerify*/ = true,
266 AnalysisID /*StartAfter*/ = 0,
267 AnalysisID /*StopAfter*/ = 0) {
271 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
272 /// get machine code emitted. This uses a JITCodeEmitter object to handle
273 /// actually outputting the machine code and resolving things like the address
274 /// of functions. This method returns true if machine code emission is
277 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
279 bool /*DisableVerify*/ = true) {
283 /// addPassesToEmitMC - Add passes to the specified pass manager to get
284 /// machine code emitted with the MCJIT. This method returns true if machine
285 /// code is not supported. It fills the MCContext Ctx pointer which can be
286 /// used to build custom MCStreamer.
288 virtual bool addPassesToEmitMC(PassManagerBase &,
291 bool /*DisableVerify*/ = true) {
295 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
296 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
297 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
300 /// LLVMTargetMachine - This class describes a target machine that is
301 /// implemented with the LLVM target-independent code generator.
303 class LLVMTargetMachine : public TargetMachine {
304 protected: // Can only create subclasses.
305 LLVMTargetMachine(const Target &T, StringRef TargetTriple,
306 StringRef CPU, StringRef FS, TargetOptions Options,
307 Reloc::Model RM, CodeModel::Model CM,
308 CodeGenOpt::Level OL);
312 /// \brief Register analysis passes for this target with a pass manager.
314 /// This registers target independent analysis passes.
315 virtual void addAnalysisPasses(PassManagerBase &PM);
317 /// createPassConfig - Create a pass configuration object to be used by
318 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
319 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
321 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
322 /// specified file emitted. Typically this will involve several steps of code
324 virtual bool addPassesToEmitFile(PassManagerBase &PM,
325 formatted_raw_ostream &Out,
326 CodeGenFileType FileType,
327 bool DisableVerify = true,
328 AnalysisID StartAfter = 0,
329 AnalysisID StopAfter = 0);
331 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
332 /// get machine code emitted. This uses a JITCodeEmitter object to handle
333 /// actually outputting the machine code and resolving things like the address
334 /// of functions. This method returns true if machine code emission is
337 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
339 bool DisableVerify = true);
341 /// addPassesToEmitMC - Add passes to the specified pass manager to get
342 /// machine code emitted with the MCJIT. This method returns true if machine
343 /// code is not supported. It fills the MCContext Ctx pointer which can be
344 /// used to build custom MCStreamer.
346 virtual bool addPassesToEmitMC(PassManagerBase &PM,
349 bool DisableVerify = true);
351 /// addCodeEmitter - This pass should be overridden by the target to add a
352 /// code emitter, if supported. If this is not supported, 'true' should be
354 virtual bool addCodeEmitter(PassManagerBase &,
360 } // End llvm namespace