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