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 "LTOCodeGenerator.h"
16 #include "LTOModule.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Analysis/Passes.h"
19 #include "llvm/Analysis/Verifier.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
21 #include "llvm/Config/config.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/Linker.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/MC/MCContext.h"
30 #include "llvm/MC/SubtargetFeature.h"
31 #include "llvm/PassManager.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/FormattedStream.h"
34 #include "llvm/Support/Host.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/Signals.h"
37 #include "llvm/Support/TargetRegistry.h"
38 #include "llvm/Support/TargetSelect.h"
39 #include "llvm/Support/ToolOutputFile.h"
40 #include "llvm/Support/system_error.h"
41 #include "llvm/Target/Mangler.h"
42 #include "llvm/Target/TargetMachine.h"
43 #include "llvm/Target/TargetOptions.h"
44 #include "llvm/Target/TargetRegisterInfo.h"
45 #include "llvm/Transforms/IPO.h"
46 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
50 DisableOpt("disable-opt", cl::init(false),
51 cl::desc("Do not run any optimization passes"));
54 DisableInline("disable-inlining", cl::init(false),
55 cl::desc("Do not run the inliner pass"));
58 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
59 cl::desc("Do not run the GVN load PRE pass"));
61 const char* LTOCodeGenerator::getVersionString() {
62 #ifdef LLVM_VERSION_INFO
63 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
65 return PACKAGE_NAME " version " PACKAGE_VERSION;
69 LTOCodeGenerator::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 InitializeAllTargets();
76 InitializeAllTargetMCs();
77 InitializeAllAsmPrinters();
80 LTOCodeGenerator::~LTOCodeGenerator() {
82 delete _nativeObjectFile;
84 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
85 E = _codegenOptions.end(); I != E; ++I)
89 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
90 bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
92 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
93 for (int i = 0, e = undefs.size(); i != e; ++i)
94 _asmUndefinedRefs[undefs[i]] = 1;
99 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug,
100 std::string& errMsg) {
102 case LTO_DEBUG_MODEL_NONE:
103 _emitDwarfDebugInfo = false;
106 case LTO_DEBUG_MODEL_DWARF:
107 _emitDwarfDebugInfo = true;
110 llvm_unreachable("Unknown debug format!");
113 bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
114 std::string& errMsg) {
116 case LTO_CODEGEN_PIC_MODEL_STATIC:
117 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
118 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
122 llvm_unreachable("Unknown PIC model!");
125 bool LTOCodeGenerator::writeMergedModules(const char *path,
126 std::string &errMsg) {
127 if (determineTarget(errMsg))
130 // mark which symbols can not be internalized
131 applyScopeRestrictions();
133 // create output file
135 tool_output_file Out(path, ErrInfo,
136 raw_fd_ostream::F_Binary);
137 if (!ErrInfo.empty()) {
138 errMsg = "could not open bitcode file for writing: ";
143 // write bitcode to it
144 WriteBitcodeToFile(_linker.getModule(), Out.os());
147 if (Out.os().has_error()) {
148 errMsg = "could not write bitcode file: ";
150 Out.os().clear_error();
158 bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
159 // make unique temp .o file to put generated object file
160 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
161 if (uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg)) {
162 uniqueObjPath.eraseFromDisk();
165 sys::RemoveFileOnSignal(uniqueObjPath);
167 // generate object file
168 bool genResult = false;
169 tool_output_file objFile(uniqueObjPath.c_str(), errMsg);
170 if (!errMsg.empty()) {
171 uniqueObjPath.eraseFromDisk();
175 genResult = this->generateObjectFile(objFile.os(), errMsg);
176 objFile.os().close();
177 if (objFile.os().has_error()) {
178 objFile.os().clear_error();
179 uniqueObjPath.eraseFromDisk();
185 uniqueObjPath.eraseFromDisk();
189 _nativeObjectPath = uniqueObjPath.str();
190 *name = _nativeObjectPath.c_str();
194 const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
196 if (compile_to_file(&name, errMsg))
199 // remove old buffer if compile() called twice
200 delete _nativeObjectFile;
202 // read .o file into memory buffer
203 OwningPtr<MemoryBuffer> BuffPtr;
204 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
205 errMsg = ec.message();
206 sys::Path(_nativeObjectPath).eraseFromDisk();
209 _nativeObjectFile = BuffPtr.take();
212 sys::Path(_nativeObjectPath).eraseFromDisk();
214 // return buffer, unless error
215 if (_nativeObjectFile == NULL)
217 *length = _nativeObjectFile->getBufferSize();
218 return _nativeObjectFile->getBufferStart();
221 bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
225 std::string TripleStr = _linker.getModule()->getTargetTriple();
226 if (TripleStr.empty())
227 TripleStr = sys::getDefaultTargetTriple();
228 llvm::Triple Triple(TripleStr);
230 // create target machine from info for merged modules
231 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
235 // The relocation model is actually a static member of TargetMachine and
236 // needs to be set before the TargetMachine is instantiated.
237 Reloc::Model RelocModel = Reloc::Default;
238 switch (_codeModel) {
239 case LTO_CODEGEN_PIC_MODEL_STATIC:
240 RelocModel = Reloc::Static;
242 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
243 RelocModel = Reloc::PIC_;
245 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
246 RelocModel = Reloc::DynamicNoPIC;
250 // construct LTOModule, hand over ownership of module and target
251 SubtargetFeatures Features;
252 Features.getDefaultSubtargetFeatures(Triple);
253 std::string FeatureStr = Features.getString();
254 // Set a default CPU for Darwin triples.
255 if (_mCpu.empty() && Triple.isOSDarwin()) {
256 if (Triple.getArch() == llvm::Triple::x86_64)
258 else if (Triple.getArch() == llvm::Triple::x86)
261 TargetOptions Options;
262 LTOModule::getTargetOptions(Options);
263 _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options,
264 RelocModel, CodeModel::Default,
265 CodeGenOpt::Aggressive);
269 void LTOCodeGenerator::
270 applyRestriction(GlobalValue &GV,
271 std::vector<const char*> &mustPreserveList,
272 SmallPtrSet<GlobalValue*, 8> &asmUsed,
274 SmallString<64> Buffer;
275 mangler.getNameWithPrefix(Buffer, &GV, false);
277 if (GV.isDeclaration())
279 if (_mustPreserveSymbols.count(Buffer))
280 mustPreserveList.push_back(GV.getName().data());
281 if (_asmUndefinedRefs.count(Buffer))
285 static void findUsedValues(GlobalVariable *LLVMUsed,
286 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
287 if (LLVMUsed == 0) return;
289 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
290 if (Inits == 0) return;
292 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
293 if (GlobalValue *GV =
294 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
295 UsedValues.insert(GV);
298 void LTOCodeGenerator::applyScopeRestrictions() {
299 if (_scopeRestrictionsDone) return;
300 Module *mergedModule = _linker.getModule();
302 // Start off with a verification pass.
304 passes.add(createVerifierPass());
306 // mark which symbols can not be internalized
307 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
308 Mangler mangler(Context, *_target->getDataLayout());
309 std::vector<const char*> mustPreserveList;
310 SmallPtrSet<GlobalValue*, 8> asmUsed;
312 for (Module::iterator f = mergedModule->begin(),
313 e = mergedModule->end(); f != e; ++f)
314 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
315 for (Module::global_iterator v = mergedModule->global_begin(),
316 e = mergedModule->global_end(); v != e; ++v)
317 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
318 for (Module::alias_iterator a = mergedModule->alias_begin(),
319 e = mergedModule->alias_end(); a != e; ++a)
320 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
322 GlobalVariable *LLVMCompilerUsed =
323 mergedModule->getGlobalVariable("llvm.compiler.used");
324 findUsedValues(LLVMCompilerUsed, asmUsed);
325 if (LLVMCompilerUsed)
326 LLVMCompilerUsed->eraseFromParent();
328 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
329 std::vector<Constant*> asmUsed2;
330 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
331 e = asmUsed.end(); i !=e; ++i) {
332 GlobalValue *GV = *i;
333 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
334 asmUsed2.push_back(c);
337 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
339 new llvm::GlobalVariable(*mergedModule, ATy, false,
340 llvm::GlobalValue::AppendingLinkage,
341 llvm::ConstantArray::get(ATy, asmUsed2),
342 "llvm.compiler.used");
344 LLVMCompilerUsed->setSection("llvm.metadata");
346 passes.add(createInternalizePass(mustPreserveList));
348 // apply scope restrictions
349 passes.run(*mergedModule);
351 _scopeRestrictionsDone = true;
354 /// Optimize merged modules using various IPO passes
355 bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
356 std::string &errMsg) {
357 if (this->determineTarget(errMsg))
360 Module* mergedModule = _linker.getModule();
362 // if options were requested, set them
363 if (!_codegenOptions.empty())
364 cl::ParseCommandLineOptions(_codegenOptions.size(),
365 const_cast<char **>(&_codegenOptions[0]));
367 // mark which symbols can not be internalized
368 this->applyScopeRestrictions();
370 // Instantiate the pass manager to organize the passes.
373 // Start off with a verification pass.
374 passes.add(createVerifierPass());
376 // Add an appropriate DataLayout instance for this module...
377 passes.add(new DataLayout(*_target->getDataLayout()));
378 _target->addAnalysisPasses(passes);
380 // Enabling internalize here would use its AllButMain variant. It
381 // keeps only main if it exists and does nothing for libraries. Instead
382 // we create the pass ourselves with the symbol list provided by the linker.
384 PassManagerBuilder().populateLTOPassManager(passes,
385 /*Internalize=*/false,
390 // Make sure everything is still good.
391 passes.add(createVerifierPass());
393 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
395 codeGenPasses->add(new DataLayout(*_target->getDataLayout()));
396 _target->addAnalysisPasses(*codeGenPasses);
398 formatted_raw_ostream Out(out);
400 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
401 TargetMachine::CGFT_ObjectFile)) {
402 errMsg = "target file type not supported";
406 // Run our queue of passes all at once now, efficiently.
407 passes.run(*mergedModule);
409 // Run the code generator, and write assembly file
410 codeGenPasses->doInitialization();
412 for (Module::iterator
413 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
414 if (!it->isDeclaration())
415 codeGenPasses->run(*it);
417 codeGenPasses->doFinalization();
418 delete codeGenPasses;
420 return false; // success
423 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
425 void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
426 for (std::pair<StringRef, StringRef> o = getToken(options);
427 !o.first.empty(); o = getToken(o.second)) {
428 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
430 if (_codegenOptions.empty())
431 _codegenOptions.push_back(strdup("libLTO"));
432 _codegenOptions.push_back(strdup(o.first.str().c_str()));