TargetMachine.h revision 98a366d547772010e94609e4584489b3e5ce0043
1//===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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 20namespace llvm { 21 22class TargetAsmInfo; 23class TargetData; 24class TargetSubtarget; 25class TargetInstrInfo; 26class TargetIntrinsicInfo; 27class TargetJITInfo; 28class TargetLowering; 29class TargetFrameInfo; 30class MachineCodeEmitter; 31class TargetRegisterInfo; 32class Module; 33class PassManagerBase; 34class PassManager; 35class Pass; 36class TargetMachOWriterInfo; 37class TargetELFWriterInfo; 38class raw_ostream; 39 40// Relocation model types. 41namespace Reloc { 42 enum Model { 43 Default, 44 Static, 45 PIC_, // Cannot be named PIC due to collision with -DPIC 46 DynamicNoPIC 47 }; 48} 49 50// Code model types. 51namespace CodeModel { 52 enum Model { 53 Default, 54 Small, 55 Kernel, 56 Medium, 57 Large 58 }; 59} 60 61namespace FileModel { 62 enum Model { 63 Error, 64 None, 65 AsmFile, 66 MachOFile, 67 ElfFile 68 }; 69} 70 71// Code generation optimization level. 72namespace CodeGenOpt { 73 enum Level { 74 Default, 75 None, 76 One, 77 Two, 78 Size, 79 Aggressive, 80 LTO 81 }; 82} 83 84//===----------------------------------------------------------------------===// 85/// 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. 89/// 90class TargetMachine { 91 TargetMachine(const TargetMachine &); // DO NOT IMPLEMENT 92 void operator=(const TargetMachine &); // DO NOT IMPLEMENT 93protected: // Can only create subclasses. 94 TargetMachine() : AsmInfo(0) { } 95 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; } 99 100 /// AsmInfo - Contains target specific asm information. 101 /// 102 mutable const TargetAsmInfo *AsmInfo; 103 104 /// createTargetAsmInfo - Create a new instance of target specific asm 105 /// information. 106 virtual const TargetAsmInfo *createTargetAsmInfo() const { return 0; } 107 108public: 109 virtual ~TargetMachine(); 110 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; } 117 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; } 123 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 129 // 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; } 134 135 /// getTargetAsmInfo - Return target specific asm information. 136 /// 137 const TargetAsmInfo *getTargetAsmInfo() const { 138 if (!AsmInfo) AsmInfo = createTargetAsmInfo(); 139 return AsmInfo; 140 } 141 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); 150 } 151 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. 155 /// 156 virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; } 157 158 /// getIntrinsicInfo - If intrinsic information is available, return it. If 159 /// not, return null. 160 /// 161 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; } 162 163 /// getJITInfo - If this target supports a JIT, return information for it, 164 /// otherwise return null. 165 /// 166 virtual TargetJITInfo *getJITInfo() { return 0; } 167 168 /// getInstrItineraryData - Returns instruction itinerary data for the target 169 /// or specific subtarget. 170 /// 171 virtual const InstrItineraryData getInstrItineraryData() const { 172 return InstrItineraryData(); 173 } 174 175 /// getMachOWriterInfo - If this target supports a Mach-O writer, return 176 /// information for it, otherwise return null. 177 /// 178 virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; } 179 180 /// getELFWriterInfo - If this target supports an ELF writer, return 181 /// information for it, otherwise return null. 182 /// 183 virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; } 184 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(); 188 189 /// setRelocationModel - Sets the code generation relocation model. 190 /// 191 static void setRelocationModel(Reloc::Model Model); 192 193 /// getCodeModel - Returns the code model. The choices are small, kernel, 194 /// medium, large, and target default. 195 static CodeModel::Model getCodeModel(); 196 197 /// setCodeModel - Sets the code model. 198 /// 199 static void setCodeModel(CodeModel::Model Model); 200 201 /// getAsmVerbosityDefault - Returns the default value of asm verbosity. 202 /// 203 static bool getAsmVerbosityDefault(); 204 205 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default 206 /// is false. 207 static void setAsmVerbosityDefault(bool); 208 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 213 }; 214 215 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge 216 /// on this target. User flag overrides. 217 virtual bool getEnableTailMergeDefault() const { return true; } 218 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. 225 /// 226 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &, 227 raw_ostream &, 228 CodeGenFileType, 229 CodeGenOpt::Level) { 230 return FileModel::None; 231 } 232 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. 236 /// 237 virtual bool addPassesToEmitFileFinish(PassManagerBase &, 238 MachineCodeEmitter *, 239 CodeGenOpt::Level) { 240 return true; 241 } 242 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 247 /// not supported. 248 /// 249 virtual bool addPassesToEmitMachineCode(PassManagerBase &, 250 MachineCodeEmitter &, 251 CodeGenOpt::Level) { 252 return true; 253 } 254 255 /// addPassesToEmitWholeFile - This method can be implemented by targets that 256 /// require having the entire module at once. This is not recommended, do not 257 /// use this. 258 virtual bool WantsWholeFile() const { return false; } 259 virtual bool addPassesToEmitWholeFile(PassManager &, raw_ostream &, 260 CodeGenFileType, 261 CodeGenOpt::Level) { 262 return true; 263 } 264}; 265 266/// LLVMTargetMachine - This class describes a target machine that is 267/// implemented with the LLVM target-independent code generator. 268/// 269class LLVMTargetMachine : public TargetMachine { 270protected: // Can only create subclasses. 271 LLVMTargetMachine() { } 272 273 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for 274 /// both emitting to assembly files or machine code output. 275 /// 276 bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level); 277 278public: 279 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 285 /// supported. 286 /// 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. 290 /// 291 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM, 292 raw_ostream &Out, 293 CodeGenFileType FileType, 294 CodeGenOpt::Level); 295 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. 299 /// 300 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM, 301 MachineCodeEmitter *MCE, 302 CodeGenOpt::Level); 303 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 308 /// not supported. 309 /// 310 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM, 311 MachineCodeEmitter &MCE, 312 CodeGenOpt::Level); 313 314 /// Target-Independent Code Generator Pass Configuration Options. 315 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) { 320 return true; 321 } 322 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) { 327 return false; 328 } 329 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) { 335 return false; 336 } 337 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) { 342 return false; 343 } 344 345 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 &) { 351 return true; 352 } 353 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 &) { 359 return true; 360 } 361 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 &) { 368 return true; 369 } 370 371 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge 372 /// on this target. User flag overrides. 373 virtual bool getEnableTailMergeDefault() const { return true; } 374}; 375 376} // End llvm namespace 377 378#endif 379