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"
25 class TargetSubtarget;
26 class TargetInstrInfo;
27 class TargetIntrinsicInfo;
30 class TargetFrameInfo;
31 class MachineCodeEmitter;
33 class ObjectCodeEmitter;
34 class TargetRegisterInfo;
35 class PassManagerBase;
38 class TargetMachOWriterInfo;
39 class TargetELFWriterInfo;
40 class formatted_raw_ostream;
42 // Relocation model types.
47 PIC_, // Cannot be named PIC due to collision with -DPIC
73 // Code generation optimization level.
74 namespace CodeGenOpt {
83 //===----------------------------------------------------------------------===//
85 /// TargetMachine - Primary interface to the complete machine description for
86 /// the target machine. All target-specific information should be accessible
87 /// through this interface.
90 TargetMachine(const TargetMachine &); // DO NOT IMPLEMENT
91 void operator=(const TargetMachine &); // DO NOT IMPLEMENT
92 protected: // Can only create subclasses.
93 TargetMachine(const Target &);
95 /// getSubtargetImpl - virtual method implemented by subclasses that returns
96 /// a reference to that target's TargetSubtarget-derived member variable.
97 virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
99 /// TheTarget - The Target that this machine was created for.
100 const Target &TheTarget;
102 /// AsmInfo - Contains target specific asm information.
104 mutable const TargetAsmInfo *AsmInfo;
106 /// createTargetAsmInfo - Create a new instance of target specific asm
108 virtual const TargetAsmInfo *createTargetAsmInfo() const { return 0; }
111 virtual ~TargetMachine();
113 const Target &getTarget() const { return TheTarget; }
115 // Interfaces to the major aspects of target machine information:
116 // -- Instruction opcode and operand information
117 // -- Pipelines and scheduling information
118 // -- Stack frame information
119 // -- Selection DAG lowering information
121 virtual const TargetInstrInfo *getInstrInfo() const { return 0; }
122 virtual const TargetFrameInfo *getFrameInfo() const { return 0; }
123 virtual TargetLowering *getTargetLowering() const { return 0; }
124 virtual const TargetData *getTargetData() const { return 0; }
126 /// getTargetAsmInfo - Return target specific asm information.
128 const TargetAsmInfo *getTargetAsmInfo() const {
129 if (!AsmInfo) AsmInfo = createTargetAsmInfo();
133 /// getSubtarget - This method returns a pointer to the specified type of
134 /// TargetSubtarget. In debug builds, it verifies that the object being
135 /// returned is of the correct type.
136 template<typename STC> const STC &getSubtarget() const {
137 const TargetSubtarget *TST = getSubtargetImpl();
138 assert(TST && dynamic_cast<const STC*>(TST) &&
139 "Not the right kind of subtarget!");
140 return *static_cast<const STC*>(TST);
143 /// getRegisterInfo - If register information is available, return it. If
144 /// not, return null. This is kept separate from RegInfo until RegInfo has
145 /// details of graph coloring register allocation removed from it.
147 virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
149 /// getIntrinsicInfo - If intrinsic information is available, return it. If
150 /// not, return null.
152 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
154 /// getJITInfo - If this target supports a JIT, return information for it,
155 /// otherwise return null.
157 virtual TargetJITInfo *getJITInfo() { return 0; }
159 /// getInstrItineraryData - Returns instruction itinerary data for the target
160 /// or specific subtarget.
162 virtual const InstrItineraryData getInstrItineraryData() const {
163 return InstrItineraryData();
166 /// getMachOWriterInfo - If this target supports a Mach-O writer, return
167 /// information for it, otherwise return null.
169 virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
171 /// getELFWriterInfo - If this target supports an ELF writer, return
172 /// information for it, otherwise return null.
174 virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
176 /// getRelocationModel - Returns the code generation relocation model. The
177 /// choices are static, PIC, and dynamic-no-pic, and target default.
178 static Reloc::Model getRelocationModel();
180 /// setRelocationModel - Sets the code generation relocation model.
182 static void setRelocationModel(Reloc::Model Model);
184 /// getCodeModel - Returns the code model. The choices are small, kernel,
185 /// medium, large, and target default.
186 static CodeModel::Model getCodeModel();
188 /// setCodeModel - Sets the code model.
190 static void setCodeModel(CodeModel::Model Model);
192 /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
194 static bool getAsmVerbosityDefault();
196 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
198 static void setAsmVerbosityDefault(bool);
200 /// CodeGenFileType - These enums are meant to be passed into
201 /// addPassesToEmitFile to indicate what type of file to emit.
202 enum CodeGenFileType {
203 AssemblyFile, ObjectFile, DynamicLibrary
206 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
207 /// on this target. User flag overrides.
208 virtual bool getEnableTailMergeDefault() const { return true; }
210 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
211 /// specified file emitted. Typically this will involve several steps of code
212 /// generation. If Fast is set to true, the code generator should emit code
213 /// as fast as possible, though the generated code may be less efficient.
214 /// This method should return FileModel::Error if emission of this file type
215 /// is not supported.
217 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &,
218 formatted_raw_ostream &,
221 return FileModel::None;
224 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
225 /// to be split up (e.g., to add an object writer pass), this method can be
226 /// used to finish up adding passes to emit the file, if necessary.
228 virtual bool addPassesToEmitFileFinish(PassManagerBase &,
229 MachineCodeEmitter *,
234 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
235 /// to be split up (e.g., to add an object writer pass), this method can be
236 /// used to finish up adding passes to emit the file, if necessary.
238 virtual bool addPassesToEmitFileFinish(PassManagerBase &,
244 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
245 /// to be split up (e.g., to add an object writer pass), this method can be
246 /// used to finish up adding passes to emit the file, if necessary.
248 virtual bool addPassesToEmitFileFinish(PassManagerBase &,
254 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
255 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
256 /// actually outputting the machine code and resolving things like the address
257 /// of functions. This method returns true if machine code emission is
260 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
261 MachineCodeEmitter &,
266 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
267 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
268 /// actually outputting the machine code and resolving things like the address
269 /// of functions. This method returns true if machine code emission is
272 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
278 /// addPassesToEmitWholeFile - This method can be implemented by targets that
279 /// require having the entire module at once. This is not recommended, do not
281 virtual bool WantsWholeFile() const { return false; }
282 virtual bool addPassesToEmitWholeFile(PassManager &, formatted_raw_ostream &,
289 /// LLVMTargetMachine - This class describes a target machine that is
290 /// implemented with the LLVM target-independent code generator.
292 class LLVMTargetMachine : public TargetMachine {
293 protected: // Can only create subclasses.
294 LLVMTargetMachine(const Target &T) : TargetMachine(T) { }
296 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
297 /// both emitting to assembly files or machine code output.
299 bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level);
303 /// addPassesToEmitFile - Add passes to the specified pass manager to get the
304 /// specified file emitted. Typically this will involve several steps of code
305 /// generation. If OptLevel is None, the code generator should emit code as fast
306 /// as possible, though the generated code may be less efficient. This method
307 /// should return FileModel::Error if emission of this file type is not
310 /// The default implementation of this method adds components from the
311 /// LLVM retargetable code generator, invoking the methods below to get
312 /// target-specific passes in standard locations.
314 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
315 formatted_raw_ostream &Out,
316 CodeGenFileType FileType,
319 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
320 /// to be split up (e.g., to add an object writer pass), this method can be
321 /// used to finish up adding passes to emit the file, if necessary.
323 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
324 MachineCodeEmitter *MCE,
327 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
328 /// to be split up (e.g., to add an object writer pass), this method can be
329 /// used to finish up adding passes to emit the file, if necessary.
331 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
335 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
336 /// to be split up (e.g., to add an object writer pass), this method can be
337 /// used to finish up adding passes to emit the file, if necessary.
339 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
340 ObjectCodeEmitter *OCE,
343 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
344 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
345 /// actually outputting the machine code and resolving things like the address
346 /// of functions. This method returns true if machine code emission is
349 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
350 MachineCodeEmitter &MCE,
353 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
354 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
355 /// actually outputting the machine code and resolving things like the address
356 /// of functions. This method returns true if machine code emission is
359 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
363 /// Target-Independent Code Generator Pass Configuration Options.
365 /// addInstSelector - This method should add any "last minute" LLVM->LLVM
366 /// passes, then install an instruction selector pass, which converts from
367 /// LLVM code to machine instructions.
368 virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) {
372 /// addPreRegAllocPasses - This method may be implemented by targets that want
373 /// to run passes immediately before register allocation. This should return
374 /// true if -print-machineinstrs should print after these passes.
375 virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
379 /// addPostRegAllocPasses - This method may be implemented by targets that
380 /// want to run passes after register allocation but before prolog-epilog
381 /// insertion. This should return true if -print-machineinstrs should print
382 /// after these passes.
383 virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
387 /// addPreEmitPass - This pass may be implemented by targets that want to run
388 /// passes immediately before machine code is emitted. This should return
389 /// true if -print-machineinstrs should print out the code after the passes.
390 virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
395 /// addCodeEmitter - This pass should be overridden by the target to add a
396 /// code emitter, if supported. If this is not supported, 'true' should be
398 virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
399 MachineCodeEmitter &) {
403 /// addCodeEmitter - This pass should be overridden by the target to add a
404 /// code emitter, if supported. If this is not supported, 'true' should be
406 virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
411 /// addSimpleCodeEmitter - This pass should be overridden by the target to add
412 /// a code emitter (without setting flags), if supported. If this is not
413 /// supported, 'true' should be returned.
414 virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
415 MachineCodeEmitter &) {
419 /// addSimpleCodeEmitter - This pass should be overridden by the target to add
420 /// a code emitter (without setting flags), if supported. If this is not
421 /// supported, 'true' should be returned.
422 virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
427 /// addSimpleCodeEmitter - This pass should be overridden by the target to add
428 /// a code emitter (without setting flags), if supported. If this is not
429 /// supported, 'true' should be returned.
430 virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
431 ObjectCodeEmitter &) {
435 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
436 /// on this target. User flag overrides.
437 virtual bool getEnableTailMergeDefault() const { return true; }
439 /// addAssemblyEmitter - Helper function which creates a target specific
440 /// assembly printer, if available.
442 /// \return Returns 'false' on success.
443 bool addAssemblyEmitter(PassManagerBase &, CodeGenOpt::Level,
444 bool /* VerboseAsmDefault */,
445 formatted_raw_ostream &);
448 } // End llvm namespace