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/Constants.h"
23 #include "llvm/DataLayout.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/LLVMContext.h"
26 #include "llvm/Linker.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/SubtargetFeature.h"
30 #include "llvm/Module.h"
31 #include "llvm/PassManager.h"
32 #include "llvm/TargetTransformInfo.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/FormattedStream.h"
35 #include "llvm/Support/Host.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Signals.h"
38 #include "llvm/Support/TargetRegistry.h"
39 #include "llvm/Support/TargetSelect.h"
40 #include "llvm/Support/ToolOutputFile.h"
41 #include "llvm/Support/system_error.h"
42 #include "llvm/Target/Mangler.h"
43 #include "llvm/Target/TargetMachine.h"
44 #include "llvm/Target/TargetOptions.h"
45 #include "llvm/Target/TargetRegisterInfo.h"
46 #include "llvm/Transforms/IPO.h"
47 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
51 DisableInline("disable-inlining", cl::init(false),
52 cl::desc("Do not run the inliner pass"));
55 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
56 cl::desc("Do not run the GVN load PRE pass"));
58 const char* LTOCodeGenerator::getVersionString() {
59 #ifdef LLVM_VERSION_INFO
60 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
62 return PACKAGE_NAME " version " PACKAGE_VERSION;
66 LTOCodeGenerator::LTOCodeGenerator()
67 : _context(getGlobalContext()),
68 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
69 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
70 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
71 _nativeObjectFile(NULL) {
72 InitializeAllTargets();
73 InitializeAllTargetMCs();
74 InitializeAllAsmPrinters();
77 LTOCodeGenerator::~LTOCodeGenerator() {
79 delete _nativeObjectFile;
81 for (std::vector<char*>::iterator I = _codegenOptions.begin(),
82 E = _codegenOptions.end(); I != E; ++I)
86 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
87 bool ret = _linker.LinkInModule(mod->getLLVVMModule(), &errMsg);
89 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
90 for (int i = 0, e = undefs.size(); i != e; ++i)
91 _asmUndefinedRefs[undefs[i]] = 1;
96 bool LTOCodeGenerator::setDebugInfo(lto_debug_model debug,
97 std::string& errMsg) {
99 case LTO_DEBUG_MODEL_NONE:
100 _emitDwarfDebugInfo = false;
103 case LTO_DEBUG_MODEL_DWARF:
104 _emitDwarfDebugInfo = true;
107 llvm_unreachable("Unknown debug format!");
110 bool LTOCodeGenerator::setCodePICModel(lto_codegen_model model,
111 std::string& errMsg) {
113 case LTO_CODEGEN_PIC_MODEL_STATIC:
114 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
115 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
119 llvm_unreachable("Unknown PIC model!");
122 bool LTOCodeGenerator::writeMergedModules(const char *path,
123 std::string &errMsg) {
124 if (determineTarget(errMsg))
127 // mark which symbols can not be internalized
128 applyScopeRestrictions();
130 // create output file
132 tool_output_file Out(path, ErrInfo,
133 raw_fd_ostream::F_Binary);
134 if (!ErrInfo.empty()) {
135 errMsg = "could not open bitcode file for writing: ";
140 // write bitcode to it
141 WriteBitcodeToFile(_linker.getModule(), Out.os());
144 if (Out.os().has_error()) {
145 errMsg = "could not write bitcode file: ";
147 Out.os().clear_error();
155 bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) {
156 // make unique temp .o file to put generated object file
157 sys::PathWithStatus uniqueObjPath("lto-llvm.o");
158 if (uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg)) {
159 uniqueObjPath.eraseFromDisk();
162 sys::RemoveFileOnSignal(uniqueObjPath);
164 // generate object file
165 bool genResult = false;
166 tool_output_file objFile(uniqueObjPath.c_str(), errMsg);
167 if (!errMsg.empty()) {
168 uniqueObjPath.eraseFromDisk();
172 genResult = this->generateObjectFile(objFile.os(), errMsg);
173 objFile.os().close();
174 if (objFile.os().has_error()) {
175 objFile.os().clear_error();
176 uniqueObjPath.eraseFromDisk();
182 uniqueObjPath.eraseFromDisk();
186 _nativeObjectPath = uniqueObjPath.str();
187 *name = _nativeObjectPath.c_str();
191 const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) {
193 if (compile_to_file(&name, errMsg))
196 // remove old buffer if compile() called twice
197 delete _nativeObjectFile;
199 // read .o file into memory buffer
200 OwningPtr<MemoryBuffer> BuffPtr;
201 if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
202 errMsg = ec.message();
203 sys::Path(_nativeObjectPath).eraseFromDisk();
206 _nativeObjectFile = BuffPtr.take();
209 sys::Path(_nativeObjectPath).eraseFromDisk();
211 // return buffer, unless error
212 if (_nativeObjectFile == NULL)
214 *length = _nativeObjectFile->getBufferSize();
215 return _nativeObjectFile->getBufferStart();
218 bool LTOCodeGenerator::determineTarget(std::string& errMsg) {
222 std::string TripleStr = _linker.getModule()->getTargetTriple();
223 if (TripleStr.empty())
224 TripleStr = sys::getDefaultTargetTriple();
225 llvm::Triple Triple(TripleStr);
227 // create target machine from info for merged modules
228 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
232 // The relocation model is actually a static member of TargetMachine and
233 // needs to be set before the TargetMachine is instantiated.
234 Reloc::Model RelocModel = Reloc::Default;
235 switch (_codeModel) {
236 case LTO_CODEGEN_PIC_MODEL_STATIC:
237 RelocModel = Reloc::Static;
239 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
240 RelocModel = Reloc::PIC_;
242 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
243 RelocModel = Reloc::DynamicNoPIC;
247 // construct LTOModule, hand over ownership of module and target
248 SubtargetFeatures Features;
249 Features.getDefaultSubtargetFeatures(Triple);
250 std::string FeatureStr = Features.getString();
251 // Set a default CPU for Darwin triples.
252 if (_mCpu.empty() && Triple.isOSDarwin()) {
253 if (Triple.getArch() == llvm::Triple::x86_64)
255 else if (Triple.getArch() == llvm::Triple::x86)
258 TargetOptions Options;
259 LTOModule::getTargetOptions(Options);
260 _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options,
261 RelocModel, CodeModel::Default,
262 CodeGenOpt::Aggressive);
266 void LTOCodeGenerator::
267 applyRestriction(GlobalValue &GV,
268 std::vector<const char*> &mustPreserveList,
269 SmallPtrSet<GlobalValue*, 8> &asmUsed,
271 SmallString<64> Buffer;
272 mangler.getNameWithPrefix(Buffer, &GV, false);
274 if (GV.isDeclaration())
276 if (_mustPreserveSymbols.count(Buffer))
277 mustPreserveList.push_back(GV.getName().data());
278 if (_asmUndefinedRefs.count(Buffer))
282 static void findUsedValues(GlobalVariable *LLVMUsed,
283 SmallPtrSet<GlobalValue*, 8> &UsedValues) {
284 if (LLVMUsed == 0) return;
286 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
287 if (Inits == 0) return;
289 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
290 if (GlobalValue *GV =
291 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
292 UsedValues.insert(GV);
295 void LTOCodeGenerator::applyScopeRestrictions() {
296 if (_scopeRestrictionsDone) return;
297 Module *mergedModule = _linker.getModule();
299 // Start off with a verification pass.
301 passes.add(createVerifierPass());
303 // mark which symbols can not be internalized
304 MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(),NULL);
305 Mangler mangler(Context, *_target->getDataLayout());
306 std::vector<const char*> mustPreserveList;
307 SmallPtrSet<GlobalValue*, 8> asmUsed;
309 for (Module::iterator f = mergedModule->begin(),
310 e = mergedModule->end(); f != e; ++f)
311 applyRestriction(*f, mustPreserveList, asmUsed, mangler);
312 for (Module::global_iterator v = mergedModule->global_begin(),
313 e = mergedModule->global_end(); v != e; ++v)
314 applyRestriction(*v, mustPreserveList, asmUsed, mangler);
315 for (Module::alias_iterator a = mergedModule->alias_begin(),
316 e = mergedModule->alias_end(); a != e; ++a)
317 applyRestriction(*a, mustPreserveList, asmUsed, mangler);
319 GlobalVariable *LLVMCompilerUsed =
320 mergedModule->getGlobalVariable("llvm.compiler.used");
321 findUsedValues(LLVMCompilerUsed, asmUsed);
322 if (LLVMCompilerUsed)
323 LLVMCompilerUsed->eraseFromParent();
325 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(_context);
326 std::vector<Constant*> asmUsed2;
327 for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = asmUsed.begin(),
328 e = asmUsed.end(); i !=e; ++i) {
329 GlobalValue *GV = *i;
330 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
331 asmUsed2.push_back(c);
334 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
336 new llvm::GlobalVariable(*mergedModule, ATy, false,
337 llvm::GlobalValue::AppendingLinkage,
338 llvm::ConstantArray::get(ATy, asmUsed2),
339 "llvm.compiler.used");
341 LLVMCompilerUsed->setSection("llvm.metadata");
343 passes.add(createInternalizePass(mustPreserveList));
345 // apply scope restrictions
346 passes.run(*mergedModule);
348 _scopeRestrictionsDone = true;
351 /// Optimize merged modules using various IPO passes
352 bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
353 std::string &errMsg) {
354 if (this->determineTarget(errMsg))
357 Module* mergedModule = _linker.getModule();
359 // if options were requested, set them
360 if (!_codegenOptions.empty())
361 cl::ParseCommandLineOptions(_codegenOptions.size(),
362 const_cast<char **>(&_codegenOptions[0]));
364 // mark which symbols can not be internalized
365 this->applyScopeRestrictions();
367 // Instantiate the pass manager to organize the passes.
370 // Start off with a verification pass.
371 passes.add(createVerifierPass());
373 // Add an appropriate DataLayout instance for this module...
374 passes.add(new DataLayout(*_target->getDataLayout()));
375 passes.add(new TargetTransformInfo(_target->getScalarTargetTransformInfo(),
376 _target->getVectorTargetTransformInfo()));
378 // Enabling internalize here would use its AllButMain variant. It
379 // keeps only main if it exists and does nothing for libraries. Instead
380 // we create the pass ourselves with the symbol list provided by the linker.
381 PassManagerBuilder().populateLTOPassManager(passes,
382 /*Internalize=*/false,
386 // Make sure everything is still good.
387 passes.add(createVerifierPass());
389 FunctionPassManager *codeGenPasses = new FunctionPassManager(mergedModule);
391 codeGenPasses->add(new DataLayout(*_target->getDataLayout()));
393 formatted_raw_ostream Out(out);
395 if (_target->addPassesToEmitFile(*codeGenPasses, Out,
396 TargetMachine::CGFT_ObjectFile)) {
397 errMsg = "target file type not supported";
401 // Run our queue of passes all at once now, efficiently.
402 passes.run(*mergedModule);
404 // Run the code generator, and write assembly file
405 codeGenPasses->doInitialization();
407 for (Module::iterator
408 it = mergedModule->begin(), e = mergedModule->end(); it != e; ++it)
409 if (!it->isDeclaration())
410 codeGenPasses->run(*it);
412 codeGenPasses->doFinalization();
413 delete codeGenPasses;
415 return false; // success
418 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
420 void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
421 for (std::pair<StringRef, StringRef> o = getToken(options);
422 !o.first.empty(); o = getToken(o.second)) {
423 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
425 if (_codegenOptions.empty())
426 _codegenOptions.push_back(strdup("libLTO"));
427 _codegenOptions.push_back(strdup(o.first.str().c_str()));