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"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Linker.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Module.h"
23 #include "llvm/PassManager.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/Triple.h"
26 #include "llvm/Analysis/Passes.h"
27 #include "llvm/Bitcode/ReaderWriter.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/MC/MCContext.h"
30 #include "llvm/Target/Mangler.h"
31 #include "llvm/Target/SubtargetFeature.h"
32 #include "llvm/Target/TargetOptions.h"
33 #include "llvm/Target/TargetData.h"
34 #include "llvm/Target/TargetMachine.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/StandardPasses.h"
41 #include "llvm/Support/SystemUtils.h"
42 #include "llvm/Support/ToolOutputFile.h"
43 #include "llvm/System/Host.h"
44 #include "llvm/System/Program.h"
45 #include "llvm/System/Signals.h"
46 #include "llvm/Config/config.h"
54 static cl::opt<bool> DisableInline("disable-inlining",
55 cl::desc("Do not run the inliner pass"));
58 const char* LTOCodeGenerator::getVersionString()
60 #ifdef LLVM_VERSION_INFO
61 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
63 return PACKAGE_NAME " version " PACKAGE_VERSION;
68 LTOCodeGenerator::LTOCodeGenerator()
69 : _context(getGlobalContext()),
70 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
71 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
72 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
73 _nativeObjectFile(NULL), _assemblerPath(NULL)
75 InitializeAllTargets();
76 InitializeAllAsmPrinters();
79 LTOCodeGenerator::~LTOCodeGenerator()
82 delete _nativeObjectFile;
87 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg)
89 return _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
93 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug, std::string& errMsg)
96 case LTO_DEBUG_MODEL_NONE:
97 _emitDwarfDebugInfo = false;
100 case LTO_DEBUG_MODEL_DWARF:
101 _emitDwarfDebugInfo = true;
104 errMsg = "unknown debug format";
109 bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
113 case LTO_CODEGEN_PIC_MODEL_STATIC:
114 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
115 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
119 errMsg = "unknown pic model";
123 void LTOCodeGenerator::setCpu(const char* mCpu)
128 void LTOCodeGenerator::setAssemblerPath(const char* path)
130 if ( _assemblerPath )
131 delete _assemblerPath;
132 _assemblerPath = new sys::Path(path);
135 void LTOCodeGenerator::setAssemblerArgs(const char** args, int nargs)
137 for (int i = 0; i < nargs; ++i) {
138 const char *arg = args[i];
139 _assemblerArgs.push_back(arg);
143 void LTOCodeGenerator::addMustPreserveSymbol(const char* sym)
145 _mustPreserveSymbols[sym] = 1;
149 bool LTOCodeGenerator::writeMergedModules(const char *path,
150 std::string &errMsg) {
151 if (determineTarget(errMsg))
154 // mark which symbols can not be internalized
155 applyScopeRestrictions();
157 // create output file
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: ";
167 // write bitcode to it
168 WriteBitcodeToFile(_linker.getModule(), Out.os());
171 if (Out.os().has_error()) {
172 errMsg = "could not write bitcode file: ";
174 Out.os().clear_error();
183 const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
185 // make unique temp .s file to put generated assembly code
186 sys::Path uniqueAsmPath("lto-llvm.s");
187 if ( uniqueAsmPath.createTemporaryFileOnDisk(true, &errMsg) )
189 sys::RemoveFileOnSignal(uniqueAsmPath);
191 // generate assembly code
192 bool genResult = false;
194 tool_output_file asmFile(uniqueAsmPath.c_str(), errMsg);
197 genResult = this->generateAssemblyCode(asmFile.os(), errMsg);
198 asmFile.os().close();
199 if (asmFile.os().has_error()) {
200 asmFile.os().clear_error();
206 uniqueAsmPath.eraseFromDisk();
210 // make unique temp .o file to put generated object file
211 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
212 if ( uniqueObjPath.createTemporaryFileOnDisk(true, &errMsg) ) {
213 uniqueAsmPath.eraseFromDisk();
216 sys::RemoveFileOnSignal(uniqueObjPath);
218 // assemble the assembly code
219 const std::string& uniqueObjStr = uniqueObjPath.str();
220 bool asmResult = this->assemble(uniqueAsmPath.str(), uniqueObjStr, errMsg);
222 // remove old buffer if compile() called twice
223 delete _nativeObjectFile;
225 // read .o file into memory buffer
226 _nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(),&errMsg);
230 uniqueAsmPath.eraseFromDisk();
231 uniqueObjPath.eraseFromDisk();
233 // return buffer, unless error
234 if ( _nativeObjectFile == NULL )
236 *length = _nativeObjectFile->getBufferSize();
237 return _nativeObjectFile->getBufferStart();
241 bool LTOCodeGenerator::assemble(const std::string& asmPath,
242 const std::string& objPath, std::string& errMsg)
245 bool needsCompilerOptions = true;
246 if ( _assemblerPath ) {
247 tool = *_assemblerPath;
248 needsCompilerOptions = false;
250 // find compiler driver
251 tool = sys::Program::FindProgramByName("gcc");
252 if ( tool.isEmpty() ) {
253 errMsg = "can't locate gcc";
258 // build argument list
259 std::vector<const char*> args;
260 llvm::Triple targetTriple(_linker.getModule()->getTargetTriple());
261 const char *arch = targetTriple.getArchNameForAssembler();
263 args.push_back(tool.c_str());
265 if (targetTriple.getOS() == Triple::Darwin) {
266 // darwin specific command line options
268 args.push_back("-arch");
269 args.push_back(arch);
271 // add -static to assembler command line when code model requires
272 if ( (_assemblerPath != NULL) &&
273 (_codeModel == LTO_CODEGEN_PIC_MODEL_STATIC) )
274 args.push_back("-static");
276 if ( needsCompilerOptions ) {
277 args.push_back("-c");
278 args.push_back("-x");
279 args.push_back("assembler");
281 for (std::vector<std::string>::iterator I = _assemblerArgs.begin(),
282 E = _assemblerArgs.end(); I != E; ++I) {
283 args.push_back(I->c_str());
286 args.push_back("-o");
287 args.push_back(objPath.c_str());
288 args.push_back(asmPath.c_str());
292 if ( sys::Program::ExecuteAndWait(tool, &args[0], 0, 0, 0, 0, &errMsg) ) {
293 errMsg = "error in assembly";
296 return false; // success
301 bool LTOCodeGenerator::determineTarget(std::string& errMsg)
303 if ( _target == NULL ) {
304 std::string Triple = _linker.getModule()->getTargetTriple();
306 Triple = sys::getHostTriple();
308 // create target machine from info for merged modules
309 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
313 // The relocation model is actually a static member of TargetMachine
314 // and needs to be set before the TargetMachine is instantiated.
315 switch( _codeModel ) {
316 case LTO_CODEGEN_PIC_MODEL_STATIC:
317 TargetMachine::setRelocationModel(Reloc::Static);
319 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
320 TargetMachine::setRelocationModel(Reloc::PIC_);
322 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
323 TargetMachine::setRelocationModel(Reloc::DynamicNoPIC);
327 // construct LTModule, hand over ownership of module and target
328 SubtargetFeatures Features;
329 Features.getDefaultSubtargetFeatures(_mCpu, llvm::Triple(Triple));
330 std::string FeatureStr = Features.getString();
331 _target = march->createTargetMachine(Triple, FeatureStr);
336 void LTOCodeGenerator::applyScopeRestrictions() {
337 if (_scopeRestrictionsDone) return;
338 Module *mergedModule = _linker.getModule();
340 // Start off with a verification pass.
342 passes.add(createVerifierPass());
344 // mark which symbols can not be internalized
345 if (!_mustPreserveSymbols.empty()) {
346 MCContext Context(*_target->getMCAsmInfo());
347 Mangler mangler(Context, *_target->getTargetData());
348 std::vector<const char*> mustPreserveList;
349 for (Module::iterator f = mergedModule->begin(),
350 e = mergedModule->end(); f != e; ++f) {
351 if (!f->isDeclaration() &&
352 _mustPreserveSymbols.count(mangler.getNameWithPrefix(f)))
353 mustPreserveList.push_back(::strdup(f->getNameStr().c_str()));
355 for (Module::global_iterator v = mergedModule->global_begin(),
356 e = mergedModule->global_end(); v != e; ++v) {
357 if (!v->isDeclaration() &&
358 _mustPreserveSymbols.count(mangler.getNameWithPrefix(v)))
359 mustPreserveList.push_back(::strdup(v->getNameStr().c_str()));
361 passes.add(createInternalizePass(mustPreserveList));
364 // apply scope restrictions
365 passes.run(*mergedModule);
367 _scopeRestrictionsDone = true;
370 /// Optimize merged modules using various IPO passes
371 bool LTOCodeGenerator::generateAssemblyCode(raw_ostream& out,
374 if ( this->determineTarget(errMsg) )
377 // mark which symbols can not be internalized
378 this->applyScopeRestrictions();
380 Module* mergedModule = _linker.getModule();
382 // if options were requested, set them
383 if ( !_codegenOptions.empty() )
384 cl::ParseCommandLineOptions(_codegenOptions.size(),
385 const_cast<char **>(&_codegenOptions[0]));
387 // Instantiate the pass manager to organize the passes.
390 // Start off with a verification pass.
391 passes.add(createVerifierPass());
393 // Add an appropriate TargetData instance for this module...
394 passes.add(new TargetData(*_target->getTargetData()));
396 createStandardLTOPasses(&passes, /*Internalize=*/ false, !DisableInline,
397 /*VerifyEach=*/ false);
399 // Make sure everything is still good.
400 passes.add(createVerifierPass());
402 FunctionPassManager* codeGenPasses = new FunctionPassManager(mergedModule);
404 codeGenPasses->add(new TargetData(*_target->getTargetData()));
406 formatted_raw_ostream Out(out);
408 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
409 TargetMachine::CGFT_AssemblyFile,
410 CodeGenOpt::Aggressive)) {
411 errMsg = "target file type not supported";
415 // Run our queue of passes all at once now, efficiently.
416 passes.run(*mergedModule);
418 // Run the code generator, and write assembly file
419 codeGenPasses->doInitialization();
421 for (Module::iterator
422 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
423 if (!it->isDeclaration())
424 codeGenPasses->run(*it);
426 codeGenPasses->doFinalization();
428 return false; // success
432 /// Optimize merged modules using various IPO passes
433 void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
435 for (std::pair<StringRef, StringRef> o = getToken(options);
436 !o.first.empty(); o = getToken(o.second)) {
437 // ParseCommandLineOptions() expects argv[0] to be program name.
439 if ( _codegenOptions.empty() )
440 _codegenOptions.push_back("libLTO");
441 _codegenOptions.push_back(strdup(o.first.str().c_str()));