1 //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===// 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 implements the Link Time Optimization library. This library is 11 // intended to be used by linker to optimize code at link time. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "LTOCodeGenerator.h" 16 #include "LTOModule.h" 17 #include "llvm/Constants.h" 18 #include "llvm/DerivedTypes.h" 19 #include "llvm/Linker.h" 20 #include "llvm/LLVMContext.h" 21 #include "llvm/Module.h" 22 #include "llvm/PassManager.h" 23 #include "llvm/Analysis/Passes.h" 24 #include "llvm/Analysis/Verifier.h" 25 #include "llvm/Bitcode/ReaderWriter.h" 26 #include "llvm/Config/config.h" 27 #include "llvm/MC/MCAsmInfo.h" 28 #include "llvm/MC/MCContext.h" 29 #include "llvm/MC/SubtargetFeature.h" 30 #include "llvm/Target/Mangler.h" 31 #include "llvm/Target/TargetOptions.h" 32 #include "llvm/Target/TargetData.h" 33 #include "llvm/Target/TargetMachine.h" 34 #include "llvm/Target/TargetRegisterInfo.h" 35 #include "llvm/Transforms/IPO.h" 36 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 37 #include "llvm/Support/CommandLine.h" 38 #include "llvm/Support/FormattedStream.h" 39 #include "llvm/Support/MemoryBuffer.h" 40 #include "llvm/Support/ToolOutputFile.h" 41 #include "llvm/Support/Host.h" 42 #include "llvm/Support/Signals.h" 43 #include "llvm/Support/TargetRegistry.h" 44 #include "llvm/Support/TargetSelect.h" 45 #include "llvm/Support/system_error.h" 46 #include "llvm/ADT/StringExtras.h" 47 using namespace llvm; 48 49 static cl::opt<bool> DisableInline("disable-inlining", cl::init(false), 50 cl::desc("Do not run the inliner pass")); 51 52 static cl::opt<bool> DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), 53 cl::desc("Do not run the GVN load PRE pass")); 54 55 const char* LTOCodeGenerator::getVersionString() { 56 #ifdef LLVM_VERSION_INFO 57 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO; 58 #else 59 return PACKAGE_NAME " version " PACKAGE_VERSION; 60 #endif 61 } 62 63 LTOCodeGenerator::LTOCodeGenerator() 64 : _context(getGlobalContext()), 65 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL), 66 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false), 67 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), 68 _nativeObjectFile(NULL) { 69 InitializeAllTargets(); 70 InitializeAllTargetMCs(); 71 InitializeAllAsmPrinters(); 72 } 73 74 LTOCodeGenerator::~LTOCodeGenerator() { 75 delete _target; 76 delete _nativeObjectFile; 77 78 for (std::vector<char*>::iterator I = _codegenOptions.begin(), 79 E = _codegenOptions.end(); I != E; ++I) 80 free(*I); 81 } 82 83 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) { 84 bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg); 85 86 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs(); 87 for (int i = 0, e = undefs.size(); i != e; ++i) 88 _asmUndefinedRefs[undefs[i]] = 1; 89 90 return ret; 91 } 92 93 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, 94 std::string& errMsg) { 95 switch (debug) { 96 case LTO_DEBUG_MODEL_NONE: 97 _emitDwarfDebugInfo = false; 98 return false; 99 100 case LTO_DEBUG_MODEL_DWARF: 101 _emitDwarfDebugInfo = true; 102 return false; 103 } 104 llvm_unreachable("Unknown debug format!"); 105 } 106 107 bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model, 108 std::string& errMsg) { 109 switch (model) { 110 case LTO_CODEGEN_PIC_MODEL_STATIC: 111 case LTO_CODEGEN_PIC_MODEL_DYNAMIC: 112 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: 113 _codeModel = model; 114 return false; 115 } 116 llvm_unreachable("Unknown PIC model!"); 117 } 118 119 bool LTOCodeGenerator::writeMergedModules(const char *path, 120 std::string &errMsg) { 121 if (determineTarget(errMsg)) 122 return true; 123 124 // mark which symbols can not be internalized 125 applyScopeRestrictions(); 126 127 // create output file 128 std::string ErrInfo; 129 tool_output_file Out(path, ErrInfo, 130 raw_fd_ostream::F_Binary); 131 if (!ErrInfo.empty()) { 132 errMsg = "could not open bitcode file for writing: "; 133 errMsg += path; 134 return true; 135 } 136 137 // write bitcode to it 138 WriteBitcodeToFile(_linker.getModule(), Out.os()); 139 Out.os().close(); 140 141 if (Out.os().has_error()) { 142 errMsg = "could not write bitcode file: "; 143 errMsg += path; 144 Out.os().clear_error(); 145 return true; 146 } 147 148 Out.keep(); 149 return false; 150 } 151 152 bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) { 153 // make unique temp .o file to put generated object file 154 sys::PathWithStatus uniqueObjPath("lto-llvm.o"); 155 if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) { 156 uniqueObjPath.eraseFromDisk(); 157 return true; 158 } 159 sys::RemoveFileOnSignal(uniqueObjPath); 160 161 // generate object file 162 bool genResult = false; 163 tool_output_file objFile(uniqueObjPath.c_str(), errMsg); 164 if (!errMsg.empty()) 165 return true; 166 167 genResult = this->generateObjectFile(objFile.os(), errMsg); 168 objFile.os().close(); 169 if (objFile.os().has_error()) { 170 objFile.os().clear_error(); 171 return true; 172 } 173 174 objFile.keep(); 175 if ( genResult ) { 176 uniqueObjPath.eraseFromDisk(); 177 return true; 178 } 179 180 _nativeObjectPath = uniqueObjPath.str(); 181 *name = _nativeObjectPath.c_str(); 182 return false; 183 } 184 185 const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) { 186 const char *name; 187 if (compile_to_file(&name, errMsg)) 188 return NULL; 189 190 // remove old buffer if compile() called twice 191 delete _nativeObjectFile; 192 193 // read .o file into memory buffer 194 OwningPtr<MemoryBuffer> BuffPtr; 195 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) { 196 errMsg = ec.message(); 197 return NULL; 198 } 199 _nativeObjectFile = BuffPtr.take(); 200 201 // remove temp files 202 sys::Path(_nativeObjectPath).eraseFromDisk(); 203 204 // return buffer, unless error 205 if ( _nativeObjectFile == NULL ) 206 return NULL; 207 *length = _nativeObjectFile->getBufferSize(); 208 return _nativeObjectFile->getBufferStart(); 209 } 210 211 bool LTOCodeGenerator::determineTarget(std::string& errMsg) { 212 if ( _target == NULL ) { 213 std::string Triple = _linker.getModule()->getTargetTriple(); 214 if (Triple.empty()) 215 Triple = sys::getDefaultTargetTriple(); 216 217 // create target machine from info for merged modules 218 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg); 219 if ( march == NULL ) 220 return true; 221 222 // The relocation model is actually a static member of TargetMachine and 223 // needs to be set before the TargetMachine is instantiated. 224 Reloc::Model RelocModel = Reloc::Default; 225 switch( _codeModel ) { 226 case LTO_CODEGEN_PIC_MODEL_STATIC: 227 RelocModel = Reloc::Static; 228 break; 229 case LTO_CODEGEN_PIC_MODEL_DYNAMIC: 230 RelocModel = Reloc::PIC_; 231 break; 232 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: 233 RelocModel = Reloc::DynamicNoPIC; 234 break; 235 } 236 237 // construct LTOModule, hand over ownership of module and target 238 SubtargetFeatures Features; 239 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple)); 240 std::string FeatureStr = Features.getString(); 241 TargetOptions Options; 242 _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options, 243 RelocModel); 244 } 245 return false; 246 } 247 248 void LTOCodeGenerator:: 249 applyRestriction(GlobalValue &GV, 250 std::vector<const char*> &mustPreserveList, 251 SmallPtrSet<GlobalValue*, 8> &asmUsed, 252 Mangler &mangler) { 253 SmallString<64> Buffer; 254 mangler.getNameWithPrefix(Buffer, &GV, false); 255 256 if (GV.isDeclaration()) 257 return; 258 if (_mustPreserveSymbols.count(Buffer)) 259 mustPreserveList.push_back(GV.getName().data()); 260 if (_asmUndefinedRefs.count(Buffer)) 261 asmUsed.insert(&GV); 262 } 263 264 static void findUsedValues(GlobalVariable *LLVMUsed, 265 SmallPtrSet<GlobalValue*, 8> &UsedValues) { 266 if (LLVMUsed == 0) return; 267 268 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer()); 269 if (Inits == 0) return; 270 271 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) 272 if (GlobalValue *GV = 273 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts())) 274 UsedValues.insert(GV); 275 } 276 277 void LTOCodeGenerator::applyScopeRestrictions() { 278 if (_scopeRestrictionsDone) return; 279 Module *mergedModule = _linker.getModule(); 280 281 // Start off with a verification pass. 282 PassManager passes; 283 passes.add(createVerifierPass()); 284 285 // mark which symbols can not be internalized 286 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL); 287 Mangler mangler(Context, *_target->getTargetData()); 288 std::vector<const char*> mustPreserveList; 289 SmallPtrSet<GlobalValue*, 8> asmUsed; 290 291 for (Module::iterator f = mergedModule->begin(), 292 e = mergedModule->end(); f != e; ++f) 293 applyRestriction(*f, mustPreserveList, asmUsed, mangler); 294 for (Module::global_iterator v = mergedModule->global_begin(), 295 e = mergedModule->global_end(); v != e; ++v) 296 applyRestriction(*v, mustPreserveList, asmUsed, mangler); 297 for (Module::alias_iterator a = mergedModule->alias_begin(), 298 e = mergedModule->alias_end(); a != e; ++a) 299 applyRestriction(*a, mustPreserveList, asmUsed, mangler); 300 301 GlobalVariable *LLVMCompilerUsed = 302 mergedModule->getGlobalVariable("llvm.compiler.used"); 303 findUsedValues(LLVMCompilerUsed, asmUsed); 304 if (LLVMCompilerUsed) 305 LLVMCompilerUsed->eraseFromParent(); 306 307 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context); 308 std::vector<Constant*> asmUsed2; 309 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(), 310 e = asmUsed.end(); i !=e; ++i) { 311 GlobalValue *GV = *i; 312 Constant *c = ConstantExpr::getBitCast(GV, i8PTy); 313 asmUsed2.push_back(c); 314 } 315 316 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size()); 317 LLVMCompilerUsed = 318 new llvm::GlobalVariable(*mergedModule, ATy, false, 319 llvm::GlobalValue::AppendingLinkage, 320 llvm::ConstantArray::get(ATy, asmUsed2), 321 "llvm.compiler.used"); 322 323 LLVMCompilerUsed->setSection("llvm.metadata"); 324 325 passes.add(createInternalizePass(mustPreserveList)); 326 327 // apply scope restrictions 328 passes.run(*mergedModule); 329 330 _scopeRestrictionsDone = true; 331 } 332 333 /// Optimize merged modules using various IPO passes 334 bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, 335 std::string &errMsg) { 336 if ( this->determineTarget(errMsg) ) 337 return true; 338 339 Module* mergedModule = _linker.getModule(); 340 341 // if options were requested, set them 342 if ( !_codegenOptions.empty() ) 343 cl::ParseCommandLineOptions(_codegenOptions.size(), 344 const_cast<char **>(&_codegenOptions[0])); 345 346 // mark which symbols can not be internalized 347 this->applyScopeRestrictions(); 348 349 // Instantiate the pass manager to organize the passes. 350 PassManager passes; 351 352 // Start off with a verification pass. 353 passes.add(createVerifierPass()); 354 355 // Add an appropriate TargetData instance for this module... 356 passes.add(new TargetData(*_target->getTargetData())); 357 358 // Enabling internalize here would use its AllButMain variant. It 359 // keeps only main if it exists and does nothing for libraries. Instead 360 // we create the pass ourselves with the symbol list provided by the linker. 361 PassManagerBuilder().populateLTOPassManager(passes, /*Internalize=*/false, 362 !DisableInline, 363 DisableGVNLoadPRE); 364 365 // Make sure everything is still good. 366 passes.add(createVerifierPass()); 367 368 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule); 369 370 codeGenPasses->add(new TargetData(*_target->getTargetData())); 371 372 formatted_raw_ostream Out(out); 373 374 if (_target->addPassesToEmitFile(*codeGenPasses, Out, 375 TargetMachine::CGFT_ObjectFile, 376 CodeGenOpt::Aggressive)) { 377 errMsg = "target file type not supported"; 378 return true; 379 } 380 381 // Run our queue of passes all at once now, efficiently. 382 passes.run(*mergedModule); 383 384 // Run the code generator, and write assembly file 385 codeGenPasses->doInitialization(); 386 387 for (Module::iterator 388 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it) 389 if (!it->isDeclaration()) 390 codeGenPasses->run(*it); 391 392 codeGenPasses->doFinalization(); 393 delete codeGenPasses; 394 395 return false; // success 396 } 397 398 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging 399 /// LTO problems. 400 void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) { 401 for (std::pair<StringRef, StringRef> o = getToken(options); 402 !o.first.empty(); o = getToken(o.second)) { 403 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add 404 // that. 405 if ( _codegenOptions.empty() ) 406 _codegenOptions.push_back(strdup("libLTO")); 407 _codegenOptions.push_back(strdup(o.first.str().c_str())); 408 } 409 } 410