LLVM API Documentation
00001 //===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the TargetMachine and LLVMTargetMachine classes. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_TARGET_TARGETMACHINE_H 00015 #define LLVM_TARGET_TARGETMACHINE_H 00016 00017 #include "llvm/Target/TargetInstrItineraries.h" 00018 #include <cassert> 00019 #include <string> 00020 00021 namespace llvm { 00022 00023 class Target; 00024 class TargetAsmInfo; 00025 class TargetData; 00026 class TargetSubtarget; 00027 class TargetInstrInfo; 00028 class TargetIntrinsicInfo; 00029 class TargetJITInfo; 00030 class TargetLowering; 00031 class TargetFrameInfo; 00032 class MachineCodeEmitter; 00033 class JITCodeEmitter; 00034 class ObjectCodeEmitter; 00035 class TargetRegisterInfo; 00036 class PassManagerBase; 00037 class PassManager; 00038 class Pass; 00039 class TargetMachOWriterInfo; 00040 class TargetELFWriterInfo; 00041 class formatted_raw_ostream; 00042 00043 // Relocation model types. 00044 namespace Reloc { 00045 enum Model { 00046 Default, 00047 Static, 00048 PIC_, // Cannot be named PIC due to collision with -DPIC 00049 DynamicNoPIC 00050 }; 00051 } 00052 00053 // Code model types. 00054 namespace CodeModel { 00055 enum Model { 00056 Default, 00057 Small, 00058 Kernel, 00059 Medium, 00060 Large 00061 }; 00062 } 00063 00064 namespace FileModel { 00065 enum Model { 00066 Error, 00067 None, 00068 AsmFile, 00069 MachOFile, 00070 ElfFile 00071 }; 00072 } 00073 00074 // Code generation optimization level. 00075 namespace CodeGenOpt { 00076 enum Level { 00077 Default, 00078 None, 00079 Aggressive 00080 }; 00081 } 00082 00083 00084 //===----------------------------------------------------------------------===// 00085 /// 00086 /// TargetMachine - Primary interface to the complete machine description for 00087 /// the target machine. All target-specific information should be accessible 00088 /// through this interface. 00089 /// 00090 class TargetMachine { 00091 TargetMachine(const TargetMachine &); // DO NOT IMPLEMENT 00092 void operator=(const TargetMachine &); // DO NOT IMPLEMENT 00093 protected: // Can only create subclasses. 00094 TargetMachine(const Target &); 00095 00096 /// getSubtargetImpl - virtual method implemented by subclasses that returns 00097 /// a reference to that target's TargetSubtarget-derived member variable. 00098 virtual const TargetSubtarget *getSubtargetImpl() const { return 0; } 00099 00100 /// TheTarget - The Target that this machine was created for. 00101 const Target &TheTarget; 00102 00103 /// AsmInfo - Contains target specific asm information. 00104 /// 00105 const TargetAsmInfo *AsmInfo; 00106 00107 public: 00108 virtual ~TargetMachine(); 00109 00110 const Target &getTarget() const { return TheTarget; } 00111 00112 // Interfaces to the major aspects of target machine information: 00113 // -- Instruction opcode and operand information 00114 // -- Pipelines and scheduling information 00115 // -- Stack frame information 00116 // -- Selection DAG lowering information 00117 // 00118 virtual const TargetInstrInfo *getInstrInfo() const { return 0; } 00119 virtual const TargetFrameInfo *getFrameInfo() const { return 0; } 00120 virtual TargetLowering *getTargetLowering() const { return 0; } 00121 virtual const TargetData *getTargetData() const { return 0; } 00122 00123 /// getTargetAsmInfo - Return target specific asm information. 00124 /// 00125 const TargetAsmInfo *getTargetAsmInfo() const { return AsmInfo; } 00126 00127 /// getSubtarget - This method returns a pointer to the specified type of 00128 /// TargetSubtarget. In debug builds, it verifies that the object being 00129 /// returned is of the correct type. 00130 template<typename STC> const STC &getSubtarget() const { 00131 const TargetSubtarget *TST = getSubtargetImpl(); 00132 assert(TST && dynamic_cast<const STC*>(TST) && 00133 "Not the right kind of subtarget!"); 00134 return *static_cast<const STC*>(TST); 00135 } 00136 00137 /// getRegisterInfo - If register information is available, return it. If 00138 /// not, return null. This is kept separate from RegInfo until RegInfo has 00139 /// details of graph coloring register allocation removed from it. 00140 /// 00141 virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; } 00142 00143 /// getIntrinsicInfo - If intrinsic information is available, return it. If 00144 /// not, return null. 00145 /// 00146 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; } 00147 00148 /// getJITInfo - If this target supports a JIT, return information for it, 00149 /// otherwise return null. 00150 /// 00151 virtual TargetJITInfo *getJITInfo() { return 0; } 00152 00153 /// getInstrItineraryData - Returns instruction itinerary data for the target 00154 /// or specific subtarget. 00155 /// 00156 virtual const InstrItineraryData getInstrItineraryData() const { 00157 return InstrItineraryData(); 00158 } 00159 00160 /// getMachOWriterInfo - If this target supports a Mach-O writer, return 00161 /// information for it, otherwise return null. 00162 /// 00163 virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; } 00164 00165 /// getELFWriterInfo - If this target supports an ELF writer, return 00166 /// information for it, otherwise return null. 00167 /// 00168 virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; } 00169 00170 /// getRelocationModel - Returns the code generation relocation model. The 00171 /// choices are static, PIC, and dynamic-no-pic, and target default. 00172 static Reloc::Model getRelocationModel(); 00173 00174 /// setRelocationModel - Sets the code generation relocation model. 00175 /// 00176 static void setRelocationModel(Reloc::Model Model); 00177 00178 /// getCodeModel - Returns the code model. The choices are small, kernel, 00179 /// medium, large, and target default. 00180 static CodeModel::Model getCodeModel(); 00181 00182 /// setCodeModel - Sets the code model. 00183 /// 00184 static void setCodeModel(CodeModel::Model Model); 00185 00186 /// getAsmVerbosityDefault - Returns the default value of asm verbosity. 00187 /// 00188 static bool getAsmVerbosityDefault(); 00189 00190 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default 00191 /// is false. 00192 static void setAsmVerbosityDefault(bool); 00193 00194 /// CodeGenFileType - These enums are meant to be passed into 00195 /// addPassesToEmitFile to indicate what type of file to emit. 00196 enum CodeGenFileType { 00197 AssemblyFile, ObjectFile, DynamicLibrary 00198 }; 00199 00200 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge 00201 /// on this target. User flag overrides. 00202 virtual bool getEnableTailMergeDefault() const { return true; } 00203 00204 /// addPassesToEmitFile - Add passes to the specified pass manager to get the 00205 /// specified file emitted. Typically this will involve several steps of code 00206 /// generation. 00207 /// This method should return FileModel::Error if emission of this file type 00208 /// is not supported. 00209 /// 00210 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &, 00211 formatted_raw_ostream &, 00212 CodeGenFileType, 00213 CodeGenOpt::Level) { 00214 return FileModel::None; 00215 } 00216 00217 /// addPassesToEmitFileFinish - If the passes to emit the specified file had 00218 /// to be split up (e.g., to add an object writer pass), this method can be 00219 /// used to finish up adding passes to emit the file, if necessary. 00220 /// 00221 virtual bool addPassesToEmitFileFinish(PassManagerBase &, 00222 MachineCodeEmitter *, 00223 CodeGenOpt::Level) { 00224 return true; 00225 } 00226 00227 /// addPassesToEmitFileFinish - If the passes to emit the specified file had 00228 /// to be split up (e.g., to add an object writer pass), this method can be 00229 /// used to finish up adding passes to emit the file, if necessary. 00230 /// 00231 virtual bool addPassesToEmitFileFinish(PassManagerBase &, 00232 JITCodeEmitter *, 00233 CodeGenOpt::Level) { 00234 return true; 00235 } 00236 00237 /// addPassesToEmitFileFinish - If the passes to emit the specified file had 00238 /// to be split up (e.g., to add an object writer pass), this method can be 00239 /// used to finish up adding passes to emit the file, if necessary. 00240 /// 00241 virtual bool addPassesToEmitFileFinish(PassManagerBase &, 00242 ObjectCodeEmitter *, 00243 CodeGenOpt::Level) { 00244 return true; 00245 } 00246 00247 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to 00248 /// get machine code emitted. This uses a MachineCodeEmitter object to handle 00249 /// actually outputting the machine code and resolving things like the address 00250 /// of functions. This method returns true if machine code emission is 00251 /// not supported. 00252 /// 00253 virtual bool addPassesToEmitMachineCode(PassManagerBase &, 00254 MachineCodeEmitter &, 00255 CodeGenOpt::Level) { 00256 return true; 00257 } 00258 00259 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to 00260 /// get machine code emitted. This uses a MachineCodeEmitter object to handle 00261 /// actually outputting the machine code and resolving things like the address 00262 /// of functions. This method returns true if machine code emission is 00263 /// not supported. 00264 /// 00265 virtual bool addPassesToEmitMachineCode(PassManagerBase &, 00266 JITCodeEmitter &, 00267 CodeGenOpt::Level) { 00268 return true; 00269 } 00270 00271 /// addPassesToEmitWholeFile - This method can be implemented by targets that 00272 /// require having the entire module at once. This is not recommended, do not 00273 /// use this. 00274 virtual bool WantsWholeFile() const { return false; } 00275 virtual bool addPassesToEmitWholeFile(PassManager &, formatted_raw_ostream &, 00276 CodeGenFileType, 00277 CodeGenOpt::Level) { 00278 return true; 00279 } 00280 }; 00281 00282 /// LLVMTargetMachine - This class describes a target machine that is 00283 /// implemented with the LLVM target-independent code generator. 00284 /// 00285 class LLVMTargetMachine : public TargetMachine { 00286 protected: // Can only create subclasses. 00287 LLVMTargetMachine(const Target &T, const std::string &TargetTriple); 00288 00289 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for 00290 /// both emitting to assembly files or machine code output. 00291 /// 00292 bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level); 00293 00294 public: 00295 00296 /// addPassesToEmitFile - Add passes to the specified pass manager to get the 00297 /// specified file emitted. Typically this will involve several steps of code 00298 /// generation. If OptLevel is None, the code generator should emit code as fast 00299 /// as possible, though the generated code may be less efficient. This method 00300 /// should return FileModel::Error if emission of this file type is not 00301 /// supported. 00302 /// 00303 /// The default implementation of this method adds components from the 00304 /// LLVM retargetable code generator, invoking the methods below to get 00305 /// target-specific passes in standard locations. 00306 /// 00307 virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM, 00308 formatted_raw_ostream &Out, 00309 CodeGenFileType FileType, 00310 CodeGenOpt::Level); 00311 00312 /// addPassesToEmitFileFinish - If the passes to emit the specified file had 00313 /// to be split up (e.g., to add an object writer pass), this method can be 00314 /// used to finish up adding passes to emit the file, if necessary. 00315 /// 00316 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM, 00317 MachineCodeEmitter *MCE, 00318 CodeGenOpt::Level); 00319 00320 /// addPassesToEmitFileFinish - If the passes to emit the specified file had 00321 /// to be split up (e.g., to add an object writer pass), this method can be 00322 /// used to finish up adding passes to emit the file, if necessary. 00323 /// 00324 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM, 00325 JITCodeEmitter *JCE, 00326 CodeGenOpt::Level); 00327 00328 /// addPassesToEmitFileFinish - If the passes to emit the specified file had 00329 /// to be split up (e.g., to add an object writer pass), this method can be 00330 /// used to finish up adding passes to emit the file, if necessary. 00331 /// 00332 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM, 00333 ObjectCodeEmitter *OCE, 00334 CodeGenOpt::Level); 00335 00336 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to 00337 /// get machine code emitted. This uses a MachineCodeEmitter object to handle 00338 /// actually outputting the machine code and resolving things like the address 00339 /// of functions. This method returns true if machine code emission is 00340 /// not supported. 00341 /// 00342 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM, 00343 MachineCodeEmitter &MCE, 00344 CodeGenOpt::Level); 00345 00346 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to 00347 /// get machine code emitted. This uses a MachineCodeEmitter object to handle 00348 /// actually outputting the machine code and resolving things like the address 00349 /// of functions. This method returns true if machine code emission is 00350 /// not supported. 00351 /// 00352 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM, 00353 JITCodeEmitter &MCE, 00354 CodeGenOpt::Level); 00355 00356 /// Target-Independent Code Generator Pass Configuration Options. 00357 00358 /// addInstSelector - This method should add any "last minute" LLVM->LLVM 00359 /// passes, then install an instruction selector pass, which converts from 00360 /// LLVM code to machine instructions. 00361 virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) { 00362 return true; 00363 } 00364 00365 /// addPreRegAllocPasses - This method may be implemented by targets that want 00366 /// to run passes immediately before register allocation. This should return 00367 /// true if -print-machineinstrs should print after these passes. 00368 virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) { 00369 return false; 00370 } 00371 00372 /// addPostRegAllocPasses - This method may be implemented by targets that 00373 /// want to run passes after register allocation but before prolog-epilog 00374 /// insertion. This should return true if -print-machineinstrs should print 00375 /// after these passes. 00376 virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) { 00377 return false; 00378 } 00379 00380 /// addPreEmitPass - This pass may be implemented by targets that want to run 00381 /// passes immediately before machine code is emitted. This should return 00382 /// true if -print-machineinstrs should print out the code after the passes. 00383 virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) { 00384 return false; 00385 } 00386 00387 00388 /// addCodeEmitter - This pass should be overridden by the target to add a 00389 /// code emitter, if supported. If this is not supported, 'true' should be 00390 /// returned. 00391 virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level, 00392 MachineCodeEmitter &) { 00393 return true; 00394 } 00395 00396 /// addCodeEmitter - This pass should be overridden by the target to add a 00397 /// code emitter, if supported. If this is not supported, 'true' should be 00398 /// returned. 00399 virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level, 00400 JITCodeEmitter &) { 00401 return true; 00402 } 00403 00404 /// addSimpleCodeEmitter - This pass should be overridden by the target to add 00405 /// a code emitter (without setting flags), if supported. If this is not 00406 /// supported, 'true' should be returned. 00407 virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level, 00408 MachineCodeEmitter &) { 00409 return true; 00410 } 00411 00412 /// addSimpleCodeEmitter - This pass should be overridden by the target to add 00413 /// a code emitter (without setting flags), if supported. If this is not 00414 /// supported, 'true' should be returned. 00415 virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level, 00416 JITCodeEmitter &) { 00417 return true; 00418 } 00419 00420 /// addSimpleCodeEmitter - This pass should be overridden by the target to add 00421 /// a code emitter (without setting flags), if supported. If this is not 00422 /// supported, 'true' should be returned. 00423 virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level, 00424 ObjectCodeEmitter &) { 00425 return true; 00426 } 00427 00428 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge 00429 /// on this target. User flag overrides. 00430 virtual bool getEnableTailMergeDefault() const { return true; } 00431 00432 /// addAssemblyEmitter - Helper function which creates a target specific 00433 /// assembly printer, if available. 00434 /// 00435 /// \return Returns 'false' on success. 00436 bool addAssemblyEmitter(PassManagerBase &, CodeGenOpt::Level, 00437 bool /* VerboseAsmDefault */, 00438 formatted_raw_ostream &); 00439 }; 00440 00441 } // End llvm namespace 00442 00443 #endif