TargetMachine.h revision fde1b3bb2f15b74c713d98a79fcddaff1ac00dd1
1//===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source 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/Target/TargetInstrItineraries.h" 18#include <cassert> 19#include <string> 20 21namespace llvm { 22 23class TargetAsmInfo; 24class TargetData; 25class TargetSubtarget; 26class TargetInstrInfo; 27class TargetInstrDescriptor; 28class TargetJITInfo; 29class TargetLowering; 30class TargetFrameInfo; 31class MachineCodeEmitter; 32class MRegisterInfo; 33class Module; 34class FunctionPassManager; 35class PassManager; 36class Pass; 37 38// Relocation model types. 39namespace Reloc { 40 enum Model { 41 Default, 42 Static, 43 PIC_, // Cannot be named PIC due to collision with -DPIC 44 DynamicNoPIC 45 }; 46} 47 48// Code model types. 49namespace CodeModel { 50 enum Model { 51 Default, 52 Small, 53 Kernel, 54 Medium, 55 Large 56 }; 57} 58 59//===----------------------------------------------------------------------===// 60/// 61/// TargetMachine - Primary interface to the complete machine description for 62/// the target machine. All target-specific information should be accessible 63/// through this interface. 64/// 65class TargetMachine { 66 TargetMachine(const TargetMachine &); // DO NOT IMPLEMENT 67 void operator=(const TargetMachine &); // DO NOT IMPLEMENT 68protected: // Can only create subclasses. 69 TargetMachine() : AsmInfo(NULL) { } 70 71 /// getSubtargetImpl - virtual method implemented by subclasses that returns 72 /// a reference to that target's TargetSubtarget-derived member variable. 73 virtual const TargetSubtarget *getSubtargetImpl() const { return 0; } 74 75 /// AsmInfo - Contains target specific asm information. 76 /// 77 mutable const TargetAsmInfo *AsmInfo; 78 79 /// createTargetAsmInfo - Create a new instance of target specific asm 80 /// information. 81 virtual const TargetAsmInfo *createTargetAsmInfo() const { return NULL; } 82 83public: 84 virtual ~TargetMachine(); 85 86 /// getModuleMatchQuality - This static method should be implemented by 87 /// targets to indicate how closely they match the specified module. This is 88 /// used by the LLC tool to determine which target to use when an explicit 89 /// -march option is not specified. If a target returns zero, it will never 90 /// be chosen without an explicit -march option. 91 static unsigned getModuleMatchQuality(const Module &M) { return 0; } 92 93 /// getJITMatchQuality - This static method should be implemented by targets 94 /// that provide JIT capabilities to indicate how suitable they are for 95 /// execution on the current host. If a value of 0 is returned, the target 96 /// will not be used unless an explicit -march option is used. 97 static unsigned getJITMatchQuality() { return 0; } 98 99 // Interfaces to the major aspects of target machine information: 100 // -- Instruction opcode and operand information 101 // -- Pipelines and scheduling information 102 // -- Stack frame information 103 // -- Selection DAG lowering information 104 // 105 virtual const TargetInstrInfo *getInstrInfo() const { return 0; } 106 virtual const TargetFrameInfo *getFrameInfo() const { return 0; } 107 virtual TargetLowering *getTargetLowering() const { return 0; } 108 virtual const TargetData *getTargetData() const { return 0; } 109 110 111 /// getTargetAsmInfo - Return target specific asm information. 112 /// 113 const TargetAsmInfo *getTargetAsmInfo() const { 114 if (!AsmInfo) AsmInfo = createTargetAsmInfo(); 115 return AsmInfo; 116 } 117 118 /// getSubtarget - This method returns a pointer to the specified type of 119 /// TargetSubtarget. In debug builds, it verifies that the object being 120 /// returned is of the correct type. 121 template<typename STC> const STC &getSubtarget() const { 122 const TargetSubtarget *TST = getSubtargetImpl(); 123 assert(TST && dynamic_cast<const STC*>(TST) && 124 "Not the right kind of subtarget!"); 125 return *static_cast<const STC*>(TST); 126 } 127 128 /// getRegisterInfo - If register information is available, return it. If 129 /// not, return null. This is kept separate from RegInfo until RegInfo has 130 /// details of graph coloring register allocation removed from it. 131 /// 132 virtual const MRegisterInfo *getRegisterInfo() const { return 0; } 133 134 /// getJITInfo - If this target supports a JIT, return information for it, 135 /// otherwise return null. 136 /// 137 virtual TargetJITInfo *getJITInfo() { return 0; } 138 139 /// getInstrItineraryData - Returns instruction itinerary data for the target 140 /// or specific subtarget. 141 /// 142 virtual const InstrItineraryData getInstrItineraryData() const { 143 return InstrItineraryData(); 144 } 145 146 /// getRelocationModel - Returns the code generation relocation model. The 147 /// choices are static, PIC, and dynamic-no-pic, and target default. 148 static Reloc::Model getRelocationModel(); 149 150 /// setRelocationModel - Sets the code generation relocation model. 151 static void setRelocationModel(Reloc::Model Model); 152 153 /// getCodeModel - Returns the code model. The choices are small, kernel, 154 /// medium, large, and target default. 155 static CodeModel::Model getCodeModel(); 156 157 /// setCodeModel - Sets the code model. 158 static void setCodeModel(CodeModel::Model Model); 159 160 /// CodeGenFileType - These enums are meant to be passed into 161 /// addPassesToEmitFile to indicate what type of file to emit. 162 enum CodeGenFileType { 163 AssemblyFile, ObjectFile, DynamicLibrary 164 }; 165 166 /// addPassesToEmitFile - Add passes to the specified pass manager to get 167 /// the specified file emitted. Typically this will involve several steps of 168 /// code generation. If Fast is set to true, the code generator should emit 169 /// code as fast as possible, without regard for compile time. This method 170 /// should return true if emission of this file type is not supported. 171 /// 172 virtual bool addPassesToEmitFile(FunctionPassManager &PM, std::ostream &Out, 173 CodeGenFileType FileType, bool Fast) { 174 return true; 175 } 176 177 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to 178 /// get machine code emitted. This uses a MachineCodeEmitter object to handle 179 /// actually outputting the machine code and resolving things like the address 180 /// of functions. This method returns true if machine code emission is 181 /// not supported. 182 /// 183 virtual bool addPassesToEmitMachineCode(FunctionPassManager &PM, 184 MachineCodeEmitter &MCE, bool Fast) { 185 return true; 186 } 187 188 189 /// addPassesToEmitWholeFile - This method can be implemented by targets that 190 /// require having the entire module at once. This is not recommended, do not 191 /// use this. 192 virtual bool WantsWholeFile() const { return false; } 193 virtual bool addPassesToEmitWholeFile(PassManager &PM, std::ostream &Out, 194 CodeGenFileType FileType, bool Fast) { 195 return true; 196 } 197}; 198 199/// LLVMTargetMachine - This class describes a target machine that is 200/// implemented with the LLVM target-independent code generator. 201/// 202class LLVMTargetMachine : public TargetMachine { 203protected: // Can only create subclasses. 204 LLVMTargetMachine() { } 205public: 206 207 /// addPassesToEmitFile - Add passes to the specified pass manager to get 208 /// the specified file emitted. Typically this will involve several steps of 209 /// code generation. If Fast is set to true, the code generator should emit 210 /// code as fast as possible, without regard for compile time. This method 211 /// should return true if emission of this file type is not supported. 212 /// 213 /// The default implementation of this method adds components from the 214 /// LLVM retargetable code generator, invoking the methods below to get 215 /// target-specific passes in standard locations. 216 /// 217 virtual bool addPassesToEmitFile(FunctionPassManager &PM, std::ostream &Out, 218 CodeGenFileType FileType, bool Fast); 219 220 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to 221 /// get machine code emitted. This uses a MachineCodeEmitter object to handle 222 /// actually outputting the machine code and resolving things like the address 223 /// of functions. This method returns true if machine code emission is 224 /// not supported. 225 /// 226 virtual bool addPassesToEmitMachineCode(FunctionPassManager &PM, 227 MachineCodeEmitter &MCE, bool Fast); 228 229 /// Target-Independent Code Generator Pass Configuration Options. 230 231 /// addInstSelector - This method should add any "last minute" LLVM->LLVM 232 /// passes, then install an instruction selector pass, which converts from 233 /// LLVM code to machine instructions. 234 virtual bool addInstSelector(FunctionPassManager &PM, bool Fast) { 235 return true; 236 } 237 238 /// addPostRegAllocPasses - This method may be implemented by targets that 239 /// want to run passes after register allocation but before prolog-epilog 240 /// insertion. This should return true if -print-machineinstrs should print 241 /// after these passes. 242 virtual bool addPostRegAlloc(FunctionPassManager &PM, bool Fast) { 243 return false; 244 } 245 246 /// addPreEmitPass - This pass may be implemented by targets that want to run 247 /// passes immediately before machine code is emitted. This should return 248 /// true if -print-machineinstrs should print out the code after the passes. 249 virtual bool addPreEmitPass(FunctionPassManager &PM, bool Fast) { 250 return false; 251 } 252 253 254 /// addAssemblyEmitter - This pass should be overridden by the target to add 255 /// the asmprinter, if asm emission is supported. If this is not supported, 256 /// 'true' should be returned. 257 virtual bool addAssemblyEmitter(FunctionPassManager &PM, bool Fast, 258 std::ostream &Out) { 259 return true; 260 } 261 262 /// addObjectWriter - This pass should be overridden by the target to add 263 /// the object-file writer, if supported. If this is not supported, 264 /// 'true' should be returned. 265 virtual bool addObjectWriter(FunctionPassManager &PM, bool Fast, 266 std::ostream &Out) { 267 return true; 268 } 269 270 /// addCodeEmitter - This pass should be overridden by the target to add a 271 /// code emitter, if supported. If this is not supported, 'true' should be 272 /// returned. 273 virtual bool addCodeEmitter(FunctionPassManager &PM, bool Fast, 274 MachineCodeEmitter &MCE) { 275 return true; 276 } 277}; 278 279} // End llvm namespace 280 281#endif 282