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/Target/TargetInstrItineraries.h"
24 class TargetSubtarget;
25 class TargetInstrInfo;
26 class TargetIntrinsicInfo;
29 class TargetFrameInfo;
30 class MachineCodeEmitter;
31 class TargetRegisterInfo;
33 class PassManagerBase;
36 class TargetMachOWriterInfo;
37 class TargetELFWriterInfo;
40 // Relocation model types.
45 PIC_, // Cannot be named PIC due to collision with -DPIC
71 // Code generation optimization level.
72 namespace CodeGenOpt {
84 //===----------------------------------------------------------------------===//
86 /// TargetMachine - Primary interface to the complete machine description for
87 /// the target machine. All target-specific information should be accessible
88 /// through this interface.
91 TargetMachine(const TargetMachine &); // DO NOT IMPLEMENT
92 void operator=(const TargetMachine &); // DO NOT IMPLEMENT
93 protected: // Can only create subclasses.
94 TargetMachine() : AsmInfo(0) { }
96 /// getSubtargetImpl - virtual method implemented by subclasses that returns
97 /// a reference to that target's TargetSubtarget-derived member variable.
98 virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
100 /// AsmInfo - Contains target specific asm information.
102 mutable const TargetAsmInfo *AsmInfo;
104 /// createTargetAsmInfo - Create a new instance of target specific asm
106 virtual const TargetAsmInfo *createTargetAsmInfo() const { return 0; }
109 virtual ~TargetMachine();
111 /// getModuleMatchQuality - This static method should be implemented by
112 /// targets to indicate how closely they match the specified module. This is
113 /// used by the LLC tool to determine which target to use when an explicit
114 /// -march option is not specified. If a target returns zero, it will never
115 /// be chosen without an explicit -march option.
116 static unsigned getModuleMatchQuality(const Module &) { return 0; }
118 /// getJITMatchQuality - This static method should be implemented by targets
119 /// that provide JIT capabilities to indicate how suitable they are for
120 /// execution on the current host. If a value of 0 is returned, the target
121 /// will not be used unless an explicit -march option is used.
122 static unsigned getJITMatchQuality() { return 0; }
124 // Interfaces to the major aspects of target machine information:
125 // -- Instruction opcode and operand information
126 // -- Pipelines and scheduling information
127 // -- Stack frame information
128 // -- Selection DAG lowering information
130 virtual const TargetInstrInfo *getInstrInfo() const { return 0; }
131 virtual const TargetFrameInfo *getFrameInfo() const { return 0; }
132 virtual TargetLowering *getTargetLowering() const { return 0; }
133 virtual const TargetData *getTargetData() const { return 0; }
135 /// getTargetAsmInfo - Return target specific asm information.
137 const TargetAsmInfo *getTargetAsmInfo() const {
138 if (!AsmInfo) AsmInfo = createTargetAsmInfo();
142 /// getSubtarget - This method returns a pointer to the specified type of
143 /// TargetSubtarget. In debug builds, it verifies that the object being
144 /// returned is of the correct type.
145 template<typename STC> const STC &getSubtarget() const {
146 const TargetSubtarget *TST = getSubtargetImpl();
147 assert(TST && dynamic_cast<const STC*>(TST) &&
148 "Not the right kind of subtarget!");
149 return *static_cast<const STC*>(TST);
152 /// getRegisterInfo - If register information is available, return it. If
153 /// not, return null. This is kept separate from RegInfo until RegInfo has
154 /// details of graph coloring register allocation removed from it.
156 virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
158 /// getIntrinsicInfo - If intrinsic information is available, return it. If
159 /// not, return null.
161 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
163 /// getJITInfo - If this target supports a JIT, return information for it,
164 /// otherwise return null.
166 virtual TargetJITInfo *getJITInfo() { return 0; }
168 /// getInstrItineraryData - Returns instruction itinerary data for the target
169 /// or specific subtarget.
171 virtual const InstrItineraryData getInstrItineraryData() const {
172 return InstrItineraryData();
175 /// getMachOWriterInfo - If this target supports a Mach-O writer, return
176 /// information for it, otherwise return null.
178 virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
180 /// getELFWriterInfo - If this target supports an ELF writer, return
181 /// information for it, otherwise return null.
183 virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
185 /// getRelocationModel - Returns the code generation relocation model. The
186 /// choices are static, PIC, and dynamic-no-pic, and target default.
187 static Reloc::Model getRelocationModel();
189 /// setRelocationModel - Sets the code generation relocation model.
191 static void setRelocationModel(Reloc::Model Model);
193 /// getCodeModel - Returns the code model. The choices are small, kernel,
194 /// medium, large, and target default.
195 static CodeModel::Model getCodeModel();
197 /// setCodeModel - Sets the code model.
199 static void setCodeModel(CodeModel::Model Model);
201 /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
203 static bool getAsmVerbosityDefault();
205 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
207 static void setAsmVerbosityDefault(bool);
209 /// CodeGenFileType - These enums are meant to be passed into
210 /// addPassesToEmitFile to indicate what type of file to emit.
211 enum CodeGenFileType {
212 AssemblyFile, ObjectFile, DynamicLibrary
215 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
216 /// on this target. User flag overrides.
217 virtual bool getEnableTailMergeDefault() const { return true; }
219 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
220 /// specified file emitted. Typically this will involve several steps of code
221 /// generation. If Fast is set to true, the code generator should emit code
222 /// as fast as possible, though the generated code may be less efficient.
223 /// This method should return FileModel::Error if emission of this file type
224 /// is not supported.
226 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &,
230 return FileModel::None;
233 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
234 /// to be split up (e.g., to add an object writer pass), this method can be
235 /// used to finish up adding passes to emit the file, if necessary.
237 virtual bool addPassesToEmitFileFinish(PassManagerBase &,
238 MachineCodeEmitter *,
243 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
244 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
245 /// actually outputting the machine code and resolving things like the address
246 /// of functions. This method returns true if machine code emission is
249 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
250 MachineCodeEmitter &,
255 /// addPassesToEmitWholeFile - This method can be implemented by targets that
256 /// require having the entire module at once. This is not recommended, do not
258 virtual bool WantsWholeFile() const { return false; }
259 virtual bool addPassesToEmitWholeFile(PassManager &, raw_ostream &,
266 /// LLVMTargetMachine - This class describes a target machine that is
267 /// implemented with the LLVM target-independent code generator.
269 class LLVMTargetMachine : public TargetMachine {
270 protected: // Can only create subclasses.
271 LLVMTargetMachine() { }
273 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
274 /// both emitting to assembly files or machine code output.
276 bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level);
280 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
281 /// specified file emitted. Typically this will involve several steps of code
282 /// generation. If OptLevel is None, the code generator should emit code as fast
283 /// as possible, though the generated code may be less efficient. This method
284 /// should return FileModel::Error if emission of this file type is not
287 /// The default implementation of this method adds components from the
288 /// LLVM retargetable code generator, invoking the methods below to get
289 /// target-specific passes in standard locations.
291 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
293 CodeGenFileType FileType,
296 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
297 /// to be split up (e.g., to add an object writer pass), this method can be
298 /// used to finish up adding passes to emit the file, if necessary.
300 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
301 MachineCodeEmitter *MCE,
304 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
305 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
306 /// actually outputting the machine code and resolving things like the address
307 /// of functions. This method returns true if machine code emission is
310 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
311 MachineCodeEmitter &MCE,
314 /// Target-Independent Code Generator Pass Configuration Options.
316 /// addInstSelector - This method should add any "last minute" LLVM->LLVM
317 /// passes, then install an instruction selector pass, which converts from
318 /// LLVM code to machine instructions.
319 virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) {
323 /// addPreRegAllocPasses - This method may be implemented by targets that want
324 /// to run passes immediately before register allocation. This should return
325 /// true if -print-machineinstrs should print after these passes.
326 virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
330 /// addPostRegAllocPasses - This method may be implemented by targets that
331 /// want to run passes after register allocation but before prolog-epilog
332 /// insertion. This should return true if -print-machineinstrs should print
333 /// after these passes.
334 virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
338 /// addPreEmitPass - This pass may be implemented by targets that want to run
339 /// passes immediately before machine code is emitted. This should return
340 /// true if -print-machineinstrs should print out the code after the passes.
341 virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
346 /// addAssemblyEmitter - This pass should be overridden by the target to add
347 /// the asmprinter, if asm emission is supported. If this is not supported,
348 /// 'true' should be returned.
349 virtual bool addAssemblyEmitter(PassManagerBase &, CodeGenOpt::Level,
350 bool /* VerboseAsmDefault */, raw_ostream &) {
354 /// addCodeEmitter - This pass should be overridden by the target to add a
355 /// code emitter, if supported. If this is not supported, 'true' should be
356 /// returned. If DumpAsm is true, the generated assembly is printed to cerr.
357 virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
358 bool /*DumpAsm*/, MachineCodeEmitter &) {
362 /// addSimpleCodeEmitter - This pass should be overridden by the target to add
363 /// a code emitter (without setting flags), if supported. If this is not
364 /// supported, 'true' should be returned. If DumpAsm is true, the generated
365 /// assembly is printed to cerr.
366 virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
367 bool /*DumpAsm*/, MachineCodeEmitter &) {
371 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
372 /// on this target. User flag overrides.
373 virtual bool getEnableTailMergeDefault() const { return true; }
376 } // End llvm namespace