LLVM API Documentation
00001 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===// 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 implements the LLVMTargetMachine class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Target/TargetMachine.h" 00015 #include "llvm/PassManager.h" 00016 #include "llvm/Pass.h" 00017 #include "llvm/Assembly/PrintModulePass.h" 00018 #include "llvm/CodeGen/AsmPrinter.h" 00019 #include "llvm/CodeGen/Passes.h" 00020 #include "llvm/CodeGen/GCStrategy.h" 00021 #include "llvm/CodeGen/MachineFunctionAnalysis.h" 00022 #include "llvm/Target/TargetOptions.h" 00023 #include "llvm/Target/TargetAsmInfo.h" 00024 #include "llvm/Target/TargetRegistry.h" 00025 #include "llvm/Transforms/Scalar.h" 00026 #include "llvm/Support/CommandLine.h" 00027 #include "llvm/Support/FormattedStream.h" 00028 using namespace llvm; 00029 00030 namespace llvm { 00031 bool EnableFastISel; 00032 } 00033 00034 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden, 00035 cl::desc("Print LLVM IR produced by the loop-reduce pass")); 00036 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden, 00037 cl::desc("Print LLVM IR input to isel pass")); 00038 static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden, 00039 cl::desc("Dump emitter generated instructions as assembly")); 00040 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden, 00041 cl::desc("Dump garbage collector data")); 00042 static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden, 00043 cl::desc("Verify generated machine code"), 00044 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL)); 00045 00046 // When this works it will be on by default. 00047 static cl::opt<bool> 00048 DisablePostRAScheduler("disable-post-RA-scheduler", 00049 cl::desc("Disable scheduling after register allocation"), 00050 cl::init(true)); 00051 00052 // Enable or disable FastISel. Both options are needed, because 00053 // FastISel is enabled by default with -fast, and we wish to be 00054 // able to enable or disable fast-isel independently from -fast. 00055 static cl::opt<cl::boolOrDefault> 00056 EnableFastISelOption("fast-isel", cl::Hidden, 00057 cl::desc("Enable the experimental \"fast\" instruction selector")); 00058 00059 00060 LLVMTargetMachine::LLVMTargetMachine(const Target &T, 00061 const std::string &TargetTriple) 00062 : TargetMachine(T) { 00063 AsmInfo = T.createAsmInfo(TargetTriple); 00064 } 00065 00066 00067 00068 FileModel::Model 00069 LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, 00070 formatted_raw_ostream &Out, 00071 CodeGenFileType FileType, 00072 CodeGenOpt::Level OptLevel) { 00073 // Add common CodeGen passes. 00074 if (addCommonCodeGenPasses(PM, OptLevel)) 00075 return FileModel::Error; 00076 00077 // Fold redundant debug labels. 00078 PM.add(createDebugLabelFoldingPass()); 00079 00080 if (PrintMachineCode) 00081 PM.add(createMachineFunctionPrinterPass(cerr)); 00082 00083 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode) 00084 PM.add(createMachineFunctionPrinterPass(cerr)); 00085 00086 if (OptLevel != CodeGenOpt::None) 00087 PM.add(createCodePlacementOptPass()); 00088 00089 switch (FileType) { 00090 default: 00091 break; 00092 case TargetMachine::AssemblyFile: 00093 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out)) 00094 return FileModel::Error; 00095 return FileModel::AsmFile; 00096 case TargetMachine::ObjectFile: 00097 if (getMachOWriterInfo()) 00098 return FileModel::MachOFile; 00099 else if (getELFWriterInfo()) 00100 return FileModel::ElfFile; 00101 } 00102 00103 return FileModel::Error; 00104 } 00105 00106 bool LLVMTargetMachine::addAssemblyEmitter(PassManagerBase &PM, 00107 CodeGenOpt::Level OptLevel, 00108 bool Verbose, 00109 formatted_raw_ostream &Out) { 00110 FunctionPass *Printer = 00111 getTarget().createAsmPrinter(Out, *this, getTargetAsmInfo(), Verbose); 00112 if (!Printer) 00113 return true; 00114 00115 PM.add(Printer); 00116 return false; 00117 } 00118 00119 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to 00120 /// be split up (e.g., to add an object writer pass), this method can be used to 00121 /// finish up adding passes to emit the file, if necessary. 00122 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM, 00123 MachineCodeEmitter *MCE, 00124 CodeGenOpt::Level OptLevel) { 00125 if (MCE) 00126 addSimpleCodeEmitter(PM, OptLevel, *MCE); 00127 if (PrintEmittedAsm) 00128 addAssemblyEmitter(PM, OptLevel, true, ferrs()); 00129 00130 PM.add(createGCInfoDeleter()); 00131 00132 return false; // success! 00133 } 00134 00135 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to 00136 /// be split up (e.g., to add an object writer pass), this method can be used to 00137 /// finish up adding passes to emit the file, if necessary. 00138 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM, 00139 JITCodeEmitter *JCE, 00140 CodeGenOpt::Level OptLevel) { 00141 if (JCE) 00142 addSimpleCodeEmitter(PM, OptLevel, *JCE); 00143 if (PrintEmittedAsm) 00144 addAssemblyEmitter(PM, OptLevel, true, ferrs()); 00145 00146 PM.add(createGCInfoDeleter()); 00147 00148 return false; // success! 00149 } 00150 00151 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to 00152 /// be split up (e.g., to add an object writer pass), this method can be used to 00153 /// finish up adding passes to emit the file, if necessary. 00154 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM, 00155 ObjectCodeEmitter *OCE, 00156 CodeGenOpt::Level OptLevel) { 00157 if (OCE) 00158 addSimpleCodeEmitter(PM, OptLevel, *OCE); 00159 if (PrintEmittedAsm) 00160 addAssemblyEmitter(PM, OptLevel, true, ferrs()); 00161 00162 PM.add(createGCInfoDeleter()); 00163 00164 return false; // success! 00165 } 00166 00167 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to 00168 /// get machine code emitted. This uses a MachineCodeEmitter object to handle 00169 /// actually outputting the machine code and resolving things like the address 00170 /// of functions. This method should returns true if machine code emission is 00171 /// not supported. 00172 /// 00173 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM, 00174 MachineCodeEmitter &MCE, 00175 CodeGenOpt::Level OptLevel) { 00176 // Add common CodeGen passes. 00177 if (addCommonCodeGenPasses(PM, OptLevel)) 00178 return true; 00179 00180 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode) 00181 PM.add(createMachineFunctionPrinterPass(cerr)); 00182 00183 addCodeEmitter(PM, OptLevel, MCE); 00184 if (PrintEmittedAsm) 00185 addAssemblyEmitter(PM, OptLevel, true, ferrs()); 00186 00187 PM.add(createGCInfoDeleter()); 00188 00189 return false; // success! 00190 } 00191 00192 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to 00193 /// get machine code emitted. This uses a MachineCodeEmitter object to handle 00194 /// actually outputting the machine code and resolving things like the address 00195 /// of functions. This method should returns true if machine code emission is 00196 /// not supported. 00197 /// 00198 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM, 00199 JITCodeEmitter &JCE, 00200 CodeGenOpt::Level OptLevel) { 00201 // Add common CodeGen passes. 00202 if (addCommonCodeGenPasses(PM, OptLevel)) 00203 return true; 00204 00205 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode) 00206 PM.add(createMachineFunctionPrinterPass(cerr)); 00207 00208 addCodeEmitter(PM, OptLevel, JCE); 00209 if (PrintEmittedAsm) 00210 addAssemblyEmitter(PM, OptLevel, true, ferrs()); 00211 00212 PM.add(createGCInfoDeleter()); 00213 00214 return false; // success! 00215 } 00216 00217 static void printAndVerify(PassManagerBase &PM, 00218 bool allowDoubleDefs = false) { 00219 if (PrintMachineCode) 00220 PM.add(createMachineFunctionPrinterPass(cerr)); 00221 00222 if (VerifyMachineCode) 00223 PM.add(createMachineVerifierPass(allowDoubleDefs)); 00224 } 00225 00226 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both 00227 /// emitting to assembly files or machine code output. 00228 /// 00229 bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, 00230 CodeGenOpt::Level OptLevel) { 00231 // Standard LLVM-Level Passes. 00232 00233 // Run loop strength reduction before anything else. 00234 if (OptLevel != CodeGenOpt::None) { 00235 PM.add(createLoopStrengthReducePass(getTargetLowering())); 00236 if (PrintLSR) 00237 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs())); 00238 } 00239 00240 // Turn exception handling constructs into something the code generators can 00241 // handle. 00242 switch (getTargetAsmInfo()->getExceptionHandlingType()) 00243 { 00244 case ExceptionHandling::SjLj: 00245 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both 00246 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None)); 00247 PM.add(createSjLjEHPass(getTargetLowering())); 00248 break; 00249 case ExceptionHandling::Dwarf: 00250 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None)); 00251 break; 00252 case ExceptionHandling::None: 00253 PM.add(createLowerInvokePass(getTargetLowering())); 00254 break; 00255 } 00256 00257 PM.add(createGCLoweringPass()); 00258 00259 // Make sure that no unreachable blocks are instruction selected. 00260 PM.add(createUnreachableBlockEliminationPass()); 00261 00262 if (OptLevel != CodeGenOpt::None) 00263 PM.add(createCodeGenPreparePass(getTargetLowering())); 00264 00265 PM.add(createStackProtectorPass(getTargetLowering())); 00266 00267 if (PrintISelInput) 00268 PM.add(createPrintFunctionPass("\n\n" 00269 "*** Final LLVM Code input to ISel ***\n", 00270 &errs())); 00271 00272 // Standard Lower-Level Passes. 00273 00274 // Set up a MachineFunction for the rest of CodeGen to work on. 00275 PM.add(new MachineFunctionAnalysis(*this, OptLevel)); 00276 00277 // Enable FastISel with -fast, but allow that to be overridden. 00278 if (EnableFastISelOption == cl::BOU_TRUE || 00279 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE)) 00280 EnableFastISel = true; 00281 00282 // Ask the target for an isel. 00283 if (addInstSelector(PM, OptLevel)) 00284 return true; 00285 00286 // Print the instruction selected machine code... 00287 printAndVerify(PM, /* allowDoubleDefs= */ true); 00288 00289 if (OptLevel != CodeGenOpt::None) { 00290 PM.add(createMachineLICMPass()); 00291 PM.add(createMachineSinkingPass()); 00292 printAndVerify(PM, /* allowDoubleDefs= */ true); 00293 } 00294 00295 // Run pre-ra passes. 00296 if (addPreRegAlloc(PM, OptLevel)) 00297 printAndVerify(PM, /* allowDoubleDefs= */ true); 00298 00299 // Perform register allocation. 00300 PM.add(createRegisterAllocator()); 00301 00302 // Perform stack slot coloring. 00303 if (OptLevel != CodeGenOpt::None) 00304 // FIXME: Re-enable coloring with register when it's capable of adding 00305 // kill markers. 00306 PM.add(createStackSlotColoringPass(false)); 00307 00308 printAndVerify(PM); // Print the register-allocated code 00309 00310 // Run post-ra passes. 00311 if (addPostRegAlloc(PM, OptLevel)) 00312 printAndVerify(PM); 00313 00314 PM.add(createLowerSubregsPass()); 00315 printAndVerify(PM); 00316 00317 // Insert prolog/epilog code. Eliminate abstract frame index references... 00318 PM.add(createPrologEpilogCodeInserter()); 00319 printAndVerify(PM); 00320 00321 // Second pass scheduler. 00322 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) { 00323 PM.add(createPostRAScheduler()); 00324 printAndVerify(PM); 00325 } 00326 00327 // Branch folding must be run after regalloc and prolog/epilog insertion. 00328 if (OptLevel != CodeGenOpt::None) { 00329 PM.add(createBranchFoldingPass(getEnableTailMergeDefault())); 00330 printAndVerify(PM); 00331 } 00332 00333 PM.add(createGCMachineCodeAnalysisPass()); 00334 printAndVerify(PM); 00335 00336 if (PrintGCInfo) 00337 PM.add(createGCInfoPrinter(*cerr)); 00338 00339 return false; 00340 }