LLVM API Documentation
00001 //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===// 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 Link Time Optimization library. This library is 00011 // intended to be used by linker to optimize code at link time. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/LTO/LTOCodeGenerator.h" 00016 #include "llvm/ADT/StringExtras.h" 00017 #include "llvm/Analysis/Passes.h" 00018 #include "llvm/Bitcode/ReaderWriter.h" 00019 #include "llvm/CodeGen/RuntimeLibcalls.h" 00020 #include "llvm/Config/config.h" 00021 #include "llvm/IR/Constants.h" 00022 #include "llvm/IR/DataLayout.h" 00023 #include "llvm/IR/DerivedTypes.h" 00024 #include "llvm/IR/DiagnosticInfo.h" 00025 #include "llvm/IR/DiagnosticPrinter.h" 00026 #include "llvm/IR/LLVMContext.h" 00027 #include "llvm/IR/Mangler.h" 00028 #include "llvm/IR/Module.h" 00029 #include "llvm/IR/Verifier.h" 00030 #include "llvm/InitializePasses.h" 00031 #include "llvm/LTO/LTOModule.h" 00032 #include "llvm/Linker/Linker.h" 00033 #include "llvm/MC/MCAsmInfo.h" 00034 #include "llvm/MC/MCContext.h" 00035 #include "llvm/MC/SubtargetFeature.h" 00036 #include "llvm/PassManager.h" 00037 #include "llvm/Support/CommandLine.h" 00038 #include "llvm/Support/FileSystem.h" 00039 #include "llvm/Support/FormattedStream.h" 00040 #include "llvm/Support/Host.h" 00041 #include "llvm/Support/MemoryBuffer.h" 00042 #include "llvm/Support/Signals.h" 00043 #include "llvm/Support/TargetRegistry.h" 00044 #include "llvm/Support/TargetSelect.h" 00045 #include "llvm/Support/ToolOutputFile.h" 00046 #include "llvm/Support/raw_ostream.h" 00047 #include "llvm/Target/TargetLibraryInfo.h" 00048 #include "llvm/Target/TargetLowering.h" 00049 #include "llvm/Target/TargetOptions.h" 00050 #include "llvm/Target/TargetRegisterInfo.h" 00051 #include "llvm/Target/TargetSubtargetInfo.h" 00052 #include "llvm/Transforms/IPO.h" 00053 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 00054 #include "llvm/Transforms/ObjCARC.h" 00055 #include <system_error> 00056 using namespace llvm; 00057 00058 const char* LTOCodeGenerator::getVersionString() { 00059 #ifdef LLVM_VERSION_INFO 00060 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO; 00061 #else 00062 return PACKAGE_NAME " version " PACKAGE_VERSION; 00063 #endif 00064 } 00065 00066 LTOCodeGenerator::LTOCodeGenerator() 00067 : Context(getGlobalContext()), IRLinker(new Module("ld-temp.o", Context)), 00068 TargetMach(nullptr), EmitDwarfDebugInfo(false), 00069 ScopeRestrictionsDone(false), CodeModel(LTO_CODEGEN_PIC_MODEL_DEFAULT), 00070 DiagHandler(nullptr), DiagContext(nullptr) { 00071 initializeLTOPasses(); 00072 } 00073 00074 LTOCodeGenerator::~LTOCodeGenerator() { 00075 delete TargetMach; 00076 TargetMach = nullptr; 00077 00078 IRLinker.deleteModule(); 00079 00080 for (std::vector<char *>::iterator I = CodegenOptions.begin(), 00081 E = CodegenOptions.end(); 00082 I != E; ++I) 00083 free(*I); 00084 } 00085 00086 // Initialize LTO passes. Please keep this funciton in sync with 00087 // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO 00088 // passes are initialized. 00089 void LTOCodeGenerator::initializeLTOPasses() { 00090 PassRegistry &R = *PassRegistry::getPassRegistry(); 00091 00092 initializeInternalizePassPass(R); 00093 initializeIPSCCPPass(R); 00094 initializeGlobalOptPass(R); 00095 initializeConstantMergePass(R); 00096 initializeDAHPass(R); 00097 initializeInstCombinerPass(R); 00098 initializeSimpleInlinerPass(R); 00099 initializePruneEHPass(R); 00100 initializeGlobalDCEPass(R); 00101 initializeArgPromotionPass(R); 00102 initializeJumpThreadingPass(R); 00103 initializeSROAPass(R); 00104 initializeSROA_DTPass(R); 00105 initializeSROA_SSAUpPass(R); 00106 initializeFunctionAttrsPass(R); 00107 initializeGlobalsModRefPass(R); 00108 initializeLICMPass(R); 00109 initializeMergedLoadStoreMotionPass(R); 00110 initializeGVNPass(R); 00111 initializeMemCpyOptPass(R); 00112 initializeDCEPass(R); 00113 initializeCFGSimplifyPassPass(R); 00114 } 00115 00116 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) { 00117 bool ret = IRLinker.linkInModule(&mod->getModule(), &errMsg); 00118 00119 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs(); 00120 for (int i = 0, e = undefs.size(); i != e; ++i) 00121 AsmUndefinedRefs[undefs[i]] = 1; 00122 00123 return !ret; 00124 } 00125 00126 void LTOCodeGenerator::setTargetOptions(TargetOptions options) { 00127 Options = options; 00128 } 00129 00130 void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) { 00131 switch (debug) { 00132 case LTO_DEBUG_MODEL_NONE: 00133 EmitDwarfDebugInfo = false; 00134 return; 00135 00136 case LTO_DEBUG_MODEL_DWARF: 00137 EmitDwarfDebugInfo = true; 00138 return; 00139 } 00140 llvm_unreachable("Unknown debug format!"); 00141 } 00142 00143 void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) { 00144 switch (model) { 00145 case LTO_CODEGEN_PIC_MODEL_STATIC: 00146 case LTO_CODEGEN_PIC_MODEL_DYNAMIC: 00147 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: 00148 case LTO_CODEGEN_PIC_MODEL_DEFAULT: 00149 CodeModel = model; 00150 return; 00151 } 00152 llvm_unreachable("Unknown PIC model!"); 00153 } 00154 00155 bool LTOCodeGenerator::writeMergedModules(const char *path, 00156 std::string &errMsg) { 00157 if (!determineTarget(errMsg)) 00158 return false; 00159 00160 // mark which symbols can not be internalized 00161 applyScopeRestrictions(); 00162 00163 // create output file 00164 std::error_code EC; 00165 tool_output_file Out(path, EC, sys::fs::F_None); 00166 if (EC) { 00167 errMsg = "could not open bitcode file for writing: "; 00168 errMsg += path; 00169 return false; 00170 } 00171 00172 // write bitcode to it 00173 WriteBitcodeToFile(IRLinker.getModule(), Out.os()); 00174 Out.os().close(); 00175 00176 if (Out.os().has_error()) { 00177 errMsg = "could not write bitcode file: "; 00178 errMsg += path; 00179 Out.os().clear_error(); 00180 return false; 00181 } 00182 00183 Out.keep(); 00184 return true; 00185 } 00186 00187 bool LTOCodeGenerator::compile_to_file(const char** name, 00188 bool disableOpt, 00189 bool disableInline, 00190 bool disableGVNLoadPRE, 00191 std::string& errMsg) { 00192 // make unique temp .o file to put generated object file 00193 SmallString<128> Filename; 00194 int FD; 00195 std::error_code EC = 00196 sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename); 00197 if (EC) { 00198 errMsg = EC.message(); 00199 return false; 00200 } 00201 00202 // generate object file 00203 tool_output_file objFile(Filename.c_str(), FD); 00204 00205 bool genResult = generateObjectFile(objFile.os(), disableOpt, disableInline, 00206 disableGVNLoadPRE, errMsg); 00207 objFile.os().close(); 00208 if (objFile.os().has_error()) { 00209 objFile.os().clear_error(); 00210 sys::fs::remove(Twine(Filename)); 00211 return false; 00212 } 00213 00214 objFile.keep(); 00215 if (!genResult) { 00216 sys::fs::remove(Twine(Filename)); 00217 return false; 00218 } 00219 00220 NativeObjectPath = Filename.c_str(); 00221 *name = NativeObjectPath.c_str(); 00222 return true; 00223 } 00224 00225 const void* LTOCodeGenerator::compile(size_t* length, 00226 bool disableOpt, 00227 bool disableInline, 00228 bool disableGVNLoadPRE, 00229 std::string& errMsg) { 00230 const char *name; 00231 if (!compile_to_file(&name, disableOpt, disableInline, disableGVNLoadPRE, 00232 errMsg)) 00233 return nullptr; 00234 00235 // read .o file into memory buffer 00236 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = 00237 MemoryBuffer::getFile(name, -1, false); 00238 if (std::error_code EC = BufferOrErr.getError()) { 00239 errMsg = EC.message(); 00240 sys::fs::remove(NativeObjectPath); 00241 return nullptr; 00242 } 00243 NativeObjectFile = std::move(*BufferOrErr); 00244 00245 // remove temp files 00246 sys::fs::remove(NativeObjectPath); 00247 00248 // return buffer, unless error 00249 if (!NativeObjectFile) 00250 return nullptr; 00251 *length = NativeObjectFile->getBufferSize(); 00252 return NativeObjectFile->getBufferStart(); 00253 } 00254 00255 bool LTOCodeGenerator::determineTarget(std::string &errMsg) { 00256 if (TargetMach) 00257 return true; 00258 00259 std::string TripleStr = IRLinker.getModule()->getTargetTriple(); 00260 if (TripleStr.empty()) 00261 TripleStr = sys::getDefaultTargetTriple(); 00262 llvm::Triple Triple(TripleStr); 00263 00264 // create target machine from info for merged modules 00265 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg); 00266 if (!march) 00267 return false; 00268 00269 // The relocation model is actually a static member of TargetMachine and 00270 // needs to be set before the TargetMachine is instantiated. 00271 Reloc::Model RelocModel = Reloc::Default; 00272 switch (CodeModel) { 00273 case LTO_CODEGEN_PIC_MODEL_STATIC: 00274 RelocModel = Reloc::Static; 00275 break; 00276 case LTO_CODEGEN_PIC_MODEL_DYNAMIC: 00277 RelocModel = Reloc::PIC_; 00278 break; 00279 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: 00280 RelocModel = Reloc::DynamicNoPIC; 00281 break; 00282 case LTO_CODEGEN_PIC_MODEL_DEFAULT: 00283 // RelocModel is already the default, so leave it that way. 00284 break; 00285 } 00286 00287 // Construct LTOModule, hand over ownership of module and target. Use MAttr as 00288 // the default set of features. 00289 SubtargetFeatures Features(MAttr); 00290 Features.getDefaultSubtargetFeatures(Triple); 00291 std::string FeatureStr = Features.getString(); 00292 // Set a default CPU for Darwin triples. 00293 if (MCpu.empty() && Triple.isOSDarwin()) { 00294 if (Triple.getArch() == llvm::Triple::x86_64) 00295 MCpu = "core2"; 00296 else if (Triple.getArch() == llvm::Triple::x86) 00297 MCpu = "yonah"; 00298 else if (Triple.getArch() == llvm::Triple::aarch64) 00299 MCpu = "cyclone"; 00300 } 00301 00302 TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options, 00303 RelocModel, CodeModel::Default, 00304 CodeGenOpt::Aggressive); 00305 return true; 00306 } 00307 00308 void LTOCodeGenerator:: 00309 applyRestriction(GlobalValue &GV, 00310 ArrayRef<StringRef> Libcalls, 00311 std::vector<const char*> &MustPreserveList, 00312 SmallPtrSetImpl<GlobalValue*> &AsmUsed, 00313 Mangler &Mangler) { 00314 // There are no restrictions to apply to declarations. 00315 if (GV.isDeclaration()) 00316 return; 00317 00318 // There is nothing more restrictive than private linkage. 00319 if (GV.hasPrivateLinkage()) 00320 return; 00321 00322 SmallString<64> Buffer; 00323 TargetMach->getNameWithPrefix(Buffer, &GV, Mangler); 00324 00325 if (MustPreserveSymbols.count(Buffer)) 00326 MustPreserveList.push_back(GV.getName().data()); 00327 if (AsmUndefinedRefs.count(Buffer)) 00328 AsmUsed.insert(&GV); 00329 00330 // Conservatively append user-supplied runtime library functions to 00331 // llvm.compiler.used. These could be internalized and deleted by 00332 // optimizations like -globalopt, causing problems when later optimizations 00333 // add new library calls (e.g., llvm.memset => memset and printf => puts). 00334 // Leave it to the linker to remove any dead code (e.g. with -dead_strip). 00335 if (isa<Function>(GV) && 00336 std::binary_search(Libcalls.begin(), Libcalls.end(), GV.getName())) 00337 AsmUsed.insert(&GV); 00338 } 00339 00340 static void findUsedValues(GlobalVariable *LLVMUsed, 00341 SmallPtrSetImpl<GlobalValue*> &UsedValues) { 00342 if (!LLVMUsed) return; 00343 00344 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer()); 00345 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) 00346 if (GlobalValue *GV = 00347 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts())) 00348 UsedValues.insert(GV); 00349 } 00350 00351 static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls, 00352 const TargetLibraryInfo& TLI, 00353 const TargetLowering *Lowering) 00354 { 00355 // TargetLibraryInfo has info on C runtime library calls on the current 00356 // target. 00357 for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs); 00358 I != E; ++I) { 00359 LibFunc::Func F = static_cast<LibFunc::Func>(I); 00360 if (TLI.has(F)) 00361 Libcalls.push_back(TLI.getName(F)); 00362 } 00363 00364 // TargetLowering has info on library calls that CodeGen expects to be 00365 // available, both from the C runtime and compiler-rt. 00366 if (Lowering) 00367 for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL); 00368 I != E; ++I) 00369 if (const char *Name 00370 = Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I))) 00371 Libcalls.push_back(Name); 00372 00373 array_pod_sort(Libcalls.begin(), Libcalls.end()); 00374 Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()), 00375 Libcalls.end()); 00376 } 00377 00378 void LTOCodeGenerator::applyScopeRestrictions() { 00379 if (ScopeRestrictionsDone) 00380 return; 00381 Module *mergedModule = IRLinker.getModule(); 00382 00383 // Start off with a verification pass. 00384 PassManager passes; 00385 passes.add(createVerifierPass()); 00386 passes.add(createDebugInfoVerifierPass()); 00387 00388 // mark which symbols can not be internalized 00389 Mangler Mangler(TargetMach->getSubtargetImpl()->getDataLayout()); 00390 std::vector<const char*> MustPreserveList; 00391 SmallPtrSet<GlobalValue*, 8> AsmUsed; 00392 std::vector<StringRef> Libcalls; 00393 TargetLibraryInfo TLI(Triple(TargetMach->getTargetTriple())); 00394 accumulateAndSortLibcalls( 00395 Libcalls, TLI, TargetMach->getSubtargetImpl()->getTargetLowering()); 00396 00397 for (Module::iterator f = mergedModule->begin(), 00398 e = mergedModule->end(); f != e; ++f) 00399 applyRestriction(*f, Libcalls, MustPreserveList, AsmUsed, Mangler); 00400 for (Module::global_iterator v = mergedModule->global_begin(), 00401 e = mergedModule->global_end(); v != e; ++v) 00402 applyRestriction(*v, Libcalls, MustPreserveList, AsmUsed, Mangler); 00403 for (Module::alias_iterator a = mergedModule->alias_begin(), 00404 e = mergedModule->alias_end(); a != e; ++a) 00405 applyRestriction(*a, Libcalls, MustPreserveList, AsmUsed, Mangler); 00406 00407 GlobalVariable *LLVMCompilerUsed = 00408 mergedModule->getGlobalVariable("llvm.compiler.used"); 00409 findUsedValues(LLVMCompilerUsed, AsmUsed); 00410 if (LLVMCompilerUsed) 00411 LLVMCompilerUsed->eraseFromParent(); 00412 00413 if (!AsmUsed.empty()) { 00414 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context); 00415 std::vector<Constant*> asmUsed2; 00416 for (auto *GV : AsmUsed) { 00417 Constant *c = ConstantExpr::getBitCast(GV, i8PTy); 00418 asmUsed2.push_back(c); 00419 } 00420 00421 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size()); 00422 LLVMCompilerUsed = 00423 new llvm::GlobalVariable(*mergedModule, ATy, false, 00424 llvm::GlobalValue::AppendingLinkage, 00425 llvm::ConstantArray::get(ATy, asmUsed2), 00426 "llvm.compiler.used"); 00427 00428 LLVMCompilerUsed->setSection("llvm.metadata"); 00429 } 00430 00431 passes.add(createInternalizePass(MustPreserveList)); 00432 00433 // apply scope restrictions 00434 passes.run(*mergedModule); 00435 00436 ScopeRestrictionsDone = true; 00437 } 00438 00439 /// Optimize merged modules using various IPO passes 00440 bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, 00441 bool DisableOpt, 00442 bool DisableInline, 00443 bool DisableGVNLoadPRE, 00444 std::string &errMsg) { 00445 if (!this->determineTarget(errMsg)) 00446 return false; 00447 00448 Module *mergedModule = IRLinker.getModule(); 00449 00450 // Mark which symbols can not be internalized 00451 this->applyScopeRestrictions(); 00452 00453 // Instantiate the pass manager to organize the passes. 00454 PassManager passes; 00455 00456 // Add an appropriate DataLayout instance for this module... 00457 mergedModule->setDataLayout(TargetMach->getSubtargetImpl()->getDataLayout()); 00458 00459 Triple TargetTriple(TargetMach->getTargetTriple()); 00460 PassManagerBuilder PMB; 00461 PMB.DisableGVNLoadPRE = DisableGVNLoadPRE; 00462 if (!DisableInline) 00463 PMB.Inliner = createFunctionInliningPass(); 00464 PMB.LibraryInfo = new TargetLibraryInfo(TargetTriple); 00465 if (DisableOpt) 00466 PMB.OptLevel = 0; 00467 PMB.VerifyInput = true; 00468 PMB.VerifyOutput = true; 00469 00470 PMB.populateLTOPassManager(passes, TargetMach); 00471 00472 PassManager codeGenPasses; 00473 00474 codeGenPasses.add(new DataLayoutPass()); 00475 00476 formatted_raw_ostream Out(out); 00477 00478 // If the bitcode files contain ARC code and were compiled with optimization, 00479 // the ObjCARCContractPass must be run, so do it unconditionally here. 00480 codeGenPasses.add(createObjCARCContractPass()); 00481 00482 if (TargetMach->addPassesToEmitFile(codeGenPasses, Out, 00483 TargetMachine::CGFT_ObjectFile)) { 00484 errMsg = "target file type not supported"; 00485 return false; 00486 } 00487 00488 // Run our queue of passes all at once now, efficiently. 00489 passes.run(*mergedModule); 00490 00491 // Run the code generator, and write assembly file 00492 codeGenPasses.run(*mergedModule); 00493 00494 return true; 00495 } 00496 00497 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging 00498 /// LTO problems. 00499 void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) { 00500 for (std::pair<StringRef, StringRef> o = getToken(options); 00501 !o.first.empty(); o = getToken(o.second)) { 00502 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add 00503 // that. 00504 if (CodegenOptions.empty()) 00505 CodegenOptions.push_back(strdup("libLLVMLTO")); 00506 CodegenOptions.push_back(strdup(o.first.str().c_str())); 00507 } 00508 } 00509 00510 void LTOCodeGenerator::parseCodeGenDebugOptions() { 00511 // if options were requested, set them 00512 if (!CodegenOptions.empty()) 00513 cl::ParseCommandLineOptions(CodegenOptions.size(), 00514 const_cast<char **>(&CodegenOptions[0])); 00515 } 00516 00517 void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI, 00518 void *Context) { 00519 ((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI); 00520 } 00521 00522 void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) { 00523 // Map the LLVM internal diagnostic severity to the LTO diagnostic severity. 00524 lto_codegen_diagnostic_severity_t Severity; 00525 switch (DI.getSeverity()) { 00526 case DS_Error: 00527 Severity = LTO_DS_ERROR; 00528 break; 00529 case DS_Warning: 00530 Severity = LTO_DS_WARNING; 00531 break; 00532 case DS_Remark: 00533 Severity = LTO_DS_REMARK; 00534 break; 00535 case DS_Note: 00536 Severity = LTO_DS_NOTE; 00537 break; 00538 } 00539 // Create the string that will be reported to the external diagnostic handler. 00540 std::string MsgStorage; 00541 raw_string_ostream Stream(MsgStorage); 00542 DiagnosticPrinterRawOStream DP(Stream); 00543 DI.print(DP); 00544 Stream.flush(); 00545 00546 // If this method has been called it means someone has set up an external 00547 // diagnostic handler. Assert on that. 00548 assert(DiagHandler && "Invalid diagnostic handler"); 00549 (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext); 00550 } 00551 00552 void 00553 LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler, 00554 void *Ctxt) { 00555 this->DiagHandler = DiagHandler; 00556 this->DiagContext = Ctxt; 00557 if (!DiagHandler) 00558 return Context.setDiagnosticHandler(nullptr, nullptr); 00559 // Register the LTOCodeGenerator stub in the LLVMContext to forward the 00560 // diagnostic to the external DiagHandler. 00561 Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this); 00562 }