1 //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "LTOModule.h"
16 #include "LTOCodeGenerator.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Linker.h"
22 #include "llvm/Module.h"
23 #include "llvm/ModuleProvider.h"
24 #include "llvm/PassManager.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/Analysis/Passes.h"
27 #include "llvm/Analysis/LoopPass.h"
28 #include "llvm/Analysis/Verifier.h"
29 #include "llvm/Bitcode/ReaderWriter.h"
30 #include "llvm/CodeGen/FileWriters.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Mangler.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/StandardPasses.h"
35 #include "llvm/Support/SystemUtils.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/System/Signals.h"
38 #include "llvm/Target/SubtargetFeature.h"
39 #include "llvm/Target/TargetOptions.h"
40 #include "llvm/Target/TargetAsmInfo.h"
41 #include "llvm/Target/TargetData.h"
42 #include "llvm/Target/TargetMachine.h"
43 #include "llvm/Target/TargetMachineRegistry.h"
44 #include "llvm/Target/TargetSelect.h"
45 #include "llvm/Transforms/IPO.h"
46 #include "llvm/Transforms/Scalar.h"
47 #include "llvm/Config/config.h"
58 static cl::opt<bool> DisableInline("disable-inlining",
59 cl::desc("Do not run the inliner pass"));
62 const char* LTOCodeGenerator::getVersionString()
64 #ifdef LLVM_VERSION_INFO
65 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
67 return PACKAGE_NAME " version " PACKAGE_VERSION;
72 LTOCodeGenerator::LTOCodeGenerator()
73 : _linker("LinkTimeOptimizer", "ld-temp.o"), _target(NULL),
74 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
75 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
76 _nativeObjectFile(NULL), _gccPath(NULL), _assemblerPath(NULL)
78 InitializeAllTargets();
79 InitializeAllAsmPrinters();
83 LTOCodeGenerator::~LTOCodeGenerator()
86 delete _nativeObjectFile;
91 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
93 return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
97 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
100 case LTO_DEBUG_MODEL_NONE:
101 _emitDwarfDebugInfo = false;
104 case LTO_DEBUG_MODEL_DWARF:
105 _emitDwarfDebugInfo = true;
108 errMsg = "unknown debug format";
113 bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
117 case LTO_CODEGEN_PIC_MODEL_STATIC:
118 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
119 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
123 errMsg = "unknown pic model";
127 void LTOCodeGenerator::setGccPath(const char* path)
131 _gccPath = new sys::Path(path);
134 void LTOCodeGenerator::setAssemblerPath(const char* path)
136 if ( _assemblerPath )
137 delete _assemblerPath;
138 _assemblerPath = new sys::Path(path);
141 void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
143 _mustPreserveSymbols[sym] = 1;
147 bool LTOCodeGenerator::writeMergedModules(const char* path, std::string& errMsg)
149 if ( this->determineTarget(errMsg) )
152 // mark which symbols can not be internalized
153 this->applyScopeRestrictions();
155 // create output file
156 std::ofstream out(path, std::ios_base::out|std::ios::trunc|std::ios::binary);
158 errMsg = "could not open bitcode file for writing: ";
163 // write bitcode to it
164 WriteBitcodeToFile(_linker.getModule(), out);
166 errMsg = "could not write bitcode file: ";
175 const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
177 // make unique temp .s file to put generated assembly code
178 sys::Path uniqueAsmPath("lto-llvm.s");
179 if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) )
181 sys::RemoveFileOnSignal(uniqueAsmPath);
183 // generate assembly code
184 bool genResult = false;
186 raw_fd_ostream asmFile(uniqueAsmPath.c_str(), false, errMsg);
189 genResult = this->generateAssemblyCode(asmFile, errMsg);
192 if ( uniqueAsmPath.exists() )
193 uniqueAsmPath.eraseFromDisk();
197 // make unique temp .o file to put generated object file
198 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
199 if ( uniqueObjPath.createTemporaryFileOnDisk(true, &errMsg) ) {
200 if ( uniqueAsmPath.exists() )
201 uniqueAsmPath.eraseFromDisk();
204 sys::RemoveFileOnSignal(uniqueObjPath);
206 // assemble the assembly code
207 const std::string& uniqueObjStr = uniqueObjPath.toString();
208 bool asmResult = this->assemble(uniqueAsmPath.toString(),
209 uniqueObjStr, errMsg);
211 // remove old buffer if compile() called twice
212 delete _nativeObjectFile;
214 // read .o file into memory buffer
215 _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
219 uniqueAsmPath.eraseFromDisk();
220 uniqueObjPath.eraseFromDisk();
222 // return buffer, unless error
223 if ( _nativeObjectFile == NULL )
225 *length = _nativeObjectFile->getBufferSize();
226 return _nativeObjectFile->getBufferStart();
230 bool LTOCodeGenerator::assemble(const std::string& asmPath,
231 const std::string& objPath, std::string& errMsg)
234 bool needsCompilerOptions = true;
235 if ( _assemblerPath ) {
236 tool = *_assemblerPath;
237 needsCompilerOptions = false;
239 else if ( _gccPath ) {
242 // find compiler driver
243 tool = sys::Program::FindProgramByName("gcc");
244 if ( tool.isEmpty() ) {
245 errMsg = "can't locate gcc";
250 // build argument list
251 std::vector<const char*> args;
252 std::string targetTriple = _linker.getModule()->getTargetTriple();
253 args.push_back(tool.c_str());
254 if ( targetTriple.find("darwin") != std::string::npos ) {
255 // darwin specific command line options
256 if (strncmp(targetTriple.c_str(), "i386-apple-", 11) == 0) {
257 args.push_back("-arch");
258 args.push_back("i386");
260 else if (strncmp(targetTriple.c_str(), "x86_64-apple-", 13) == 0) {
261 args.push_back("-arch");
262 args.push_back("x86_64");
264 else if (strncmp(targetTriple.c_str(), "powerpc-apple-", 14) == 0) {
265 args.push_back("-arch");
266 args.push_back("ppc");
268 else if (strncmp(targetTriple.c_str(), "powerpc64-apple-", 16) == 0) {
269 args.push_back("-arch");
270 args.push_back("ppc64");
272 else if (strncmp(targetTriple.c_str(), "arm-apple-", 10) == 0) {
273 args.push_back("-arch");
274 args.push_back("arm");
276 else if ((strncmp(targetTriple.c_str(), "armv4t-apple-", 13) == 0) ||
277 (strncmp(targetTriple.c_str(), "thumbv4t-apple-", 15) == 0)) {
278 args.push_back("-arch");
279 args.push_back("armv4t");
281 else if ((strncmp(targetTriple.c_str(), "armv5-apple-", 12) == 0) ||
282 (strncmp(targetTriple.c_str(), "armv5e-apple-", 13) == 0) ||
283 (strncmp(targetTriple.c_str(), "thumbv5-apple-", 14) == 0) ||
284 (strncmp(targetTriple.c_str(), "thumbv5e-apple-", 15) == 0)) {
285 args.push_back("-arch");
286 args.push_back("armv5");
288 else if ((strncmp(targetTriple.c_str(), "armv6-apple-", 12) == 0) ||
289 (strncmp(targetTriple.c_str(), "thumbv6-apple-", 14) == 0)) {
290 args.push_back("-arch");
291 args.push_back("armv6");
293 else if ((strncmp(targetTriple.c_str(), "armv7-apple-", 12) == 0) ||
294 (strncmp(targetTriple.c_str(), "thumbv7-apple-", 14) == 0)) {
295 args.push_back("-arch");
296 args.push_back("armv7");
298 // add -static to assembler command line when code model requires
299 if ( (_assemblerPath != NULL) && (_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) )
300 args.push_back("-static");
302 if ( needsCompilerOptions ) {
303 args.push_back("-c");
304 args.push_back("-x");
305 args.push_back("assembler");
307 args.push_back("-o");
308 args.push_back(objPath.c_str());
309 args.push_back(asmPath.c_str());
313 if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) {
314 errMsg = "error in assembly";
317 return false; // success
322 bool LTOCodeGenerator::determineTarget(std::string& errMsg)
324 if ( _target == NULL ) {
325 // create target machine from info for merged modules
326 Module* mergedModule = _linker.getModule();
327 const TargetMachineRegistry::entry* march =
328 TargetMachineRegistry::getClosestStaticTargetForModule(
329 *mergedModule, errMsg);
333 // The relocation model is actually a static member of TargetMachine
334 // and needs to be set before the TargetMachine is instantiated.
335 switch( _codeModel ) {
336 case LTO_CODEGEN_PIC_MODEL_STATIC:
337 TargetMachine::setRelocationModel(Reloc::Static);
339 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
340 TargetMachine::setRelocationModel(Reloc::PIC_);
342 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
343 TargetMachine::setRelocationModel(Reloc::DynamicNoPIC);
347 // construct LTModule, hand over ownership of module and target
348 std::string FeatureStr =
349 getFeatureString(_linker.getModule()->getTargetTriple().c_str());
350 _target = march->CtorFn(*mergedModule, FeatureStr.c_str());
355 void LTOCodeGenerator::applyScopeRestrictions()
357 if ( !_scopeRestrictionsDone ) {
358 Module* mergedModule = _linker.getModule();
360 // Start off with a verification pass.
362 passes.add(createVerifierPass());
364 // mark which symbols can not be internalized
365 if ( !_mustPreserveSymbols.empty() ) {
366 Mangler mangler(*mergedModule,
367 _target->getTargetAsmInfo()->getGlobalPrefix());
368 std::vector<const char*> mustPreserveList;
369 for (Module::iterator f = mergedModule->begin(),
370 e = mergedModule->end(); f != e; ++f) {
371 if ( !f->isDeclaration()
372 && _mustPreserveSymbols.count(mangler.getValueName(f)) )
373 mustPreserveList.push_back(::strdup(f->getName().c_str()));
375 for (Module::global_iterator v = mergedModule->global_begin(),
376 e = mergedModule->global_end(); v != e; ++v) {
377 if ( !v->isDeclaration()
378 && _mustPreserveSymbols.count(mangler.getValueName(v)) )
379 mustPreserveList.push_back(::strdup(v->getName().c_str()));
381 passes.add(createInternalizePass(mustPreserveList));
383 // apply scope restrictions
384 passes.run(*mergedModule);
386 _scopeRestrictionsDone = true;
390 /// Optimize merged modules using various IPO passes
391 bool LTOCodeGenerator::generateAssemblyCode(raw_ostream& out,
394 if ( this->determineTarget(errMsg) )
397 // mark which symbols can not be internalized
398 this->applyScopeRestrictions();
400 Module* mergedModule = _linker.getModule();
402 // If target supports exception handling then enable it now.
403 if ( _target->getTargetAsmInfo()->doesSupportExceptionHandling() )
404 llvm::ExceptionHandling = true;
406 // if options were requested, set them
407 if ( !_codegenOptions.empty() )
408 cl::ParseCommandLineOptions(_codegenOptions.size(),
409 (char**)&_codegenOptions[0]);
411 // Instantiate the pass manager to organize the passes.
414 // Start off with a verification pass.
415 passes.add(createVerifierPass());
417 // Add an appropriate TargetData instance for this module...
418 passes.add(new TargetData(*_target->getTargetData()));
420 createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline,
421 /*VerifyEach=*/ false);
423 // Make sure everything is still good.
424 passes.add(createVerifierPass());
426 FunctionPassManager* codeGenPasses =
427 new FunctionPassManager(new ExistingModuleProvider(mergedModule));
429 codeGenPasses->add(new TargetData(*_target->getTargetData()));
431 MachineCodeEmitter* mce = NULL;
433 switch (_target->addPassesToEmitFile(*codeGenPasses, out,
434 TargetMachine::AssemblyFile,
435 CodeGenOpt::Aggressive)) {
436 case FileModel::MachOFile:
437 mce = AddMachOWriter(*codeGenPasses, out, *_target);
439 case FileModel::ElfFile:
440 mce = AddELFWriter(*codeGenPasses, out, *_target);
442 case FileModel::AsmFile:
444 case FileModel::Error:
445 case FileModel::None:
446 errMsg = "target file type not supported";
450 if (_target->addPassesToEmitFileFinish(*codeGenPasses, mce,
451 CodeGenOpt::Aggressive)) {
452 errMsg = "target does not support generation of this file type";
456 // Run our queue of passes all at once now, efficiently.
457 passes.run(*mergedModule);
459 // Run the code generator, and write assembly file
460 codeGenPasses->doInitialization();
462 for (Module::iterator
463 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
464 if (!it->isDeclaration())
465 codeGenPasses->run(*it);
467 codeGenPasses->doFinalization();
468 return false; // success
472 /// Optimize merged modules using various IPO passes
473 void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
475 std::string ops(options);
476 for (std::string o = getToken(ops); !o.empty(); o = getToken(ops)) {
477 // ParseCommandLineOptions() expects argv[0] to be program name.
479 if ( _codegenOptions.empty() )
480 _codegenOptions.push_back("libLTO");
481 _codegenOptions.push_back(strdup(o.c_str()));