lto.cpp revision 17be6791b8b22b36850340a44a6f05de5c3cbf85
1//===-lto.cpp - LLVM Link Time Optimizer ----------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Devang Patel and is distributed under
6// the University of Illinois Open Source 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 "llvm/Module.h"
16#include "llvm/PassManager.h"
17#include "llvm/Linker.h"
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/SymbolTable.h"
21#include "llvm/Bytecode/Reader.h"
22#include "llvm/Bytecode/Writer.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/FileUtilities.h"
25#include "llvm/Support/SystemUtils.h"
26#include "llvm/Support/Mangler.h"
27#include "llvm/System/Program.h"
28#include "llvm/System/Signals.h"
29#include "llvm/Analysis/Passes.h"
30#include "llvm/Analysis/Verifier.h"
31#include "llvm/Target/SubtargetFeature.h"
32#include "llvm/Target/TargetData.h"
33#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetMachineRegistry.h"
35#include "llvm/Target/TargetAsmInfo.h"
36#include "llvm/Transforms/IPO.h"
37#include "llvm/Transforms/Scalar.h"
38#include "llvm/Analysis/LoadValueNumbering.h"
39#include "llvm/Support/MathExtras.h"
40#include "llvm/Support/Streams.h"
41#include "llvm/LinkTimeOptimizer.h"
42#include <fstream>
43#include <ostream>
44using namespace llvm;
45
46extern "C"
47llvm::LinkTimeOptimizer *createLLVMOptimizer()
48{
49  llvm::LTO *l = new llvm::LTO();
50  return l;
51}
52
53
54
55/// If symbol is not used then make it internal and let optimizer takes
56/// care of it.
57void LLVMSymbol::mayBeNotUsed() {
58  gv->setLinkage(GlobalValue::InternalLinkage);
59}
60
61// Map LLVM LinkageType to LTO LinakgeType
62static LTOLinkageTypes
63getLTOLinkageType(GlobalValue *v)
64{
65  LTOLinkageTypes lt;
66  if (v->hasExternalLinkage())
67    lt = LTOExternalLinkage;
68  else if (v->hasLinkOnceLinkage())
69    lt = LTOLinkOnceLinkage;
70  else if (v->hasWeakLinkage())
71    lt = LTOWeakLinkage;
72  else
73    // Otherwise it is internal linkage for link time optimizer
74    lt = LTOInternalLinkage;
75  return lt;
76}
77
78// Find exeternal symbols referenced by VALUE. This is a recursive function.
79static void
80findExternalRefs(Value *value, std::set<std::string> &references,
81                 Mangler &mangler) {
82
83  if (GlobalValue *gv = dyn_cast<GlobalValue>(value)) {
84    LTOLinkageTypes lt = getLTOLinkageType(gv);
85    if (lt != LTOInternalLinkage && strncmp (gv->getName().c_str(), "llvm.", 5))
86      references.insert(mangler.getValueName(gv));
87  }
88
89  // GlobalValue, even with InternalLinkage type, may have operands with
90  // ExternalLinkage type. Do not ignore these operands.
91  if (Constant *c = dyn_cast<Constant>(value))
92    // Handle ConstantExpr, ConstantStruct, ConstantArry etc..
93    for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
94      findExternalRefs(c->getOperand(i), references, mangler);
95}
96
97/// If Module with InputFilename is available then remove it from allModules
98/// and call delete on it.
99void
100LTO::removeModule (const std::string &InputFilename)
101{
102  NameToModuleMap::iterator pos = allModules.find(InputFilename.c_str());
103  if (pos == allModules.end())
104    return;
105
106  Module *m = pos->second;
107  allModules.erase(pos);
108  delete m;
109}
110
111/// InputFilename is a LLVM bytecode file. If Module with InputFilename is
112/// available then return it. Otherwise parseInputFilename.
113Module *
114LTO::getModule(const std::string &InputFilename)
115{
116  Module *m = NULL;
117
118  NameToModuleMap::iterator pos = allModules.find(InputFilename.c_str());
119  if (pos != allModules.end())
120    m = allModules[InputFilename.c_str()];
121  else {
122    m = ParseBytecodeFile(InputFilename);
123    allModules[InputFilename.c_str()] = m;
124  }
125  return m;
126}
127
128/// InputFilename is a LLVM bytecode file. Reade this bytecode file and
129/// set corresponding target triplet string.
130void
131LTO::getTargetTriple(const std::string &InputFilename,
132                     std::string &targetTriple)
133{
134  Module *m = getModule(InputFilename);
135  if (m)
136    targetTriple = m->getTargetTriple();
137}
138
139/// InputFilename is a LLVM bytecode file. Read it using bytecode reader.
140/// Collect global functions and symbol names in symbols vector.
141/// Collect external references in references vector.
142/// Return LTO_READ_SUCCESS if there is no error.
143enum LTOStatus
144LTO::readLLVMObjectFile(const std::string &InputFilename,
145                        NameToSymbolMap &symbols,
146                        std::set<std::string> &references)
147{
148  Module *m = getModule(InputFilename);
149  if (!m)
150    return LTO_READ_FAILURE;
151
152  // Collect Target info
153  getTarget(m);
154
155  if (!Target)
156    return LTO_READ_FAILURE;
157
158  // Use mangler to add GlobalPrefix to names to match linker names.
159  // FIXME : Instead of hard coding "-" use GlobalPrefix.
160  Mangler mangler(*m, Target->getTargetAsmInfo()->getGlobalPrefix());
161  modules.push_back(m);
162
163  for (Module::iterator f = m->begin(), e = m->end(); f != e; ++f) {
164
165    LTOLinkageTypes lt = getLTOLinkageType(f);
166
167    if (!f->isExternal() && lt != LTOInternalLinkage
168        && strncmp (f->getName().c_str(), "llvm.", 5)) {
169      int alignment = ( 16 > f->getAlignment() ? 16 : f->getAlignment());
170      LLVMSymbol *newSymbol = new LLVMSymbol(lt, f, f->getName(),
171                                             mangler.getValueName(f),
172                                             Log2_32(alignment));
173      symbols[newSymbol->getMangledName()] = newSymbol;
174      allSymbols[newSymbol->getMangledName()] = newSymbol;
175    }
176
177    // Collect external symbols referenced by this function.
178    for (Function::iterator b = f->begin(), fe = f->end(); b != fe; ++b)
179      for (BasicBlock::iterator i = b->begin(), be = b->end();
180           i != be; ++i)
181        for (unsigned count = 0, total = i->getNumOperands();
182             count != total; ++count)
183          findExternalRefs(i->getOperand(count), references, mangler);
184  }
185
186  for (Module::global_iterator v = m->global_begin(), e = m->global_end();
187       v !=  e; ++v) {
188    LTOLinkageTypes lt = getLTOLinkageType(v);
189    if (!v->isExternal() && lt != LTOInternalLinkage
190        && strncmp (v->getName().c_str(), "llvm.", 5)) {
191      const TargetData *TD = Target->getTargetData();
192      LLVMSymbol *newSymbol = new LLVMSymbol(lt, v, v->getName(),
193                                             mangler.getValueName(v),
194                                             TD->getPreferredAlignmentLog(v));
195      symbols[newSymbol->getMangledName()] = newSymbol;
196      allSymbols[newSymbol->getMangledName()] = newSymbol;
197
198      for (unsigned count = 0, total = v->getNumOperands();
199           count != total; ++count)
200        findExternalRefs(v->getOperand(count), references, mangler);
201
202    }
203  }
204
205  return LTO_READ_SUCCESS;
206}
207
208/// Get TargetMachine.
209/// Use module M to find appropriate Target.
210void
211LTO::getTarget (Module *M) {
212
213  if (Target)
214    return;
215
216  std::string Err;
217  const TargetMachineRegistry::Entry* March =
218    TargetMachineRegistry::getClosestStaticTargetForModule(*M, Err);
219
220  if (March == 0)
221    return;
222
223  // Create target
224  std::string Features;
225  Target = March->CtorFn(*M, Features);
226}
227
228/// Optimize module M using various IPO passes. Use exportList to
229/// internalize selected symbols. Target platform is selected
230/// based on information available to module M. No new target
231/// features are selected.
232enum LTOStatus
233LTO::optimize(Module *M, std::ostream &Out,
234              std::vector<const char *> &exportList)
235{
236  // Instantiate the pass manager to organize the passes.
237  PassManager Passes;
238
239  // Collect Target info
240  getTarget(M);
241
242  if (!Target)
243    return LTO_NO_TARGET;
244
245  // Start off with a verification pass.
246  Passes.add(createVerifierPass());
247
248  // Add an appropriate TargetData instance for this module...
249  Passes.add(new TargetData(*Target->getTargetData()));
250
251  // Often if the programmer does not specify proper prototypes for the
252  // functions they are calling, they end up calling a vararg version of the
253  // function that does not get a body filled in (the real function has typed
254  // arguments).  This pass merges the two functions.
255  Passes.add(createFunctionResolvingPass());
256
257  // Internalize symbols if export list is nonemty
258  if (!exportList.empty())
259    Passes.add(createInternalizePass(exportList));
260
261  // Now that we internalized some globals, see if we can hack on them!
262  Passes.add(createGlobalOptimizerPass());
263
264  // Linking modules together can lead to duplicated global constants, only
265  // keep one copy of each constant...
266  Passes.add(createConstantMergePass());
267
268  // If the -s command line option was specified, strip the symbols out of the
269  // resulting program to make it smaller.  -s is a GLD option that we are
270  // supporting.
271  Passes.add(createStripSymbolsPass());
272
273  // Propagate constants at call sites into the functions they call.
274  Passes.add(createIPConstantPropagationPass());
275
276  // Remove unused arguments from functions...
277  Passes.add(createDeadArgEliminationPass());
278
279  Passes.add(createFunctionInliningPass()); // Inline small functions
280
281  Passes.add(createPruneEHPass());            // Remove dead EH info
282
283  Passes.add(createGlobalDCEPass());          // Remove dead functions
284
285  // If we didn't decide to inline a function, check to see if we can
286  // transform it to pass arguments by value instead of by reference.
287  Passes.add(createArgumentPromotionPass());
288
289  // The IPO passes may leave cruft around.  Clean up after them.
290  Passes.add(createInstructionCombiningPass());
291
292  Passes.add(createScalarReplAggregatesPass()); // Break up allocas
293
294  // Run a few AA driven optimizations here and now, to cleanup the code.
295  Passes.add(createGlobalsModRefPass());      // IP alias analysis
296
297  Passes.add(createLICMPass());               // Hoist loop invariants
298  Passes.add(createLoadValueNumberingPass()); // GVN for load instrs
299  Passes.add(createGCSEPass());               // Remove common subexprs
300  Passes.add(createDeadStoreEliminationPass()); // Nuke dead stores
301
302  // Cleanup and simplify the code after the scalar optimizations.
303  Passes.add(createInstructionCombiningPass());
304
305  // Delete basic blocks, which optimization passes may have killed...
306  Passes.add(createCFGSimplificationPass());
307
308  // Now that we have optimized the program, discard unreachable functions...
309  Passes.add(createGlobalDCEPass());
310
311  // Make sure everything is still good.
312  Passes.add(createVerifierPass());
313
314  FunctionPassManager *CodeGenPasses =
315    new FunctionPassManager(new ExistingModuleProvider(M));
316
317  CodeGenPasses->add(new TargetData(*Target->getTargetData()));
318  Target->addPassesToEmitFile(*CodeGenPasses, Out, TargetMachine::AssemblyFile,
319                              true);
320
321  // Run our queue of passes all at once now, efficiently.
322  Passes.run(*M);
323
324  // Run the code generator, if present.
325  CodeGenPasses->doInitialization();
326  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
327    if (!I->isExternal())
328      CodeGenPasses->run(*I);
329  }
330  CodeGenPasses->doFinalization();
331
332  return LTO_OPT_SUCCESS;
333}
334
335///Link all modules together and optimize them using IPO. Generate
336/// native object file using OutputFilename
337/// Return appropriate LTOStatus.
338enum LTOStatus
339LTO::optimizeModules(const std::string &OutputFilename,
340                     std::vector<const char *> &exportList,
341                     std::string &targetTriple,
342                     bool saveTemps,
343                     const char *FinalOutputFilename)
344{
345  if (modules.empty())
346    return LTO_NO_WORK;
347
348  std::ios::openmode io_mode =
349    std::ios::out | std::ios::trunc | std::ios::binary;
350  std::string *errMsg = NULL;
351  Module *bigOne = modules[0];
352  Linker theLinker("LinkTimeOptimizer", bigOne, false);
353  for (unsigned i = 1, e = modules.size(); i != e; ++i)
354    if (theLinker.LinkModules(bigOne, modules[i], errMsg))
355      return LTO_MODULE_MERGE_FAILURE;
356  //  all modules have been handed off to the linker.
357  modules.clear();
358
359  sys::Path FinalOutputPath(FinalOutputFilename);
360  FinalOutputPath.eraseSuffix();
361
362  if (saveTemps) {
363    std::string tempFileName(FinalOutputPath.c_str());
364    tempFileName += "0.bc";
365    std::ofstream Out(tempFileName.c_str(), io_mode);
366    OStream L(Out);
367    WriteBytecodeToFile(bigOne, L);
368  }
369
370  // Strip leading underscore because it was added to match names
371  // seen by linker.
372  for (unsigned i = 0, e = exportList.size(); i != e; ++i) {
373    const char *name = exportList[i];
374    NameToSymbolMap::iterator itr = allSymbols.find(name);
375    if (itr != allSymbols.end())
376      exportList[i] = allSymbols[name]->getName();
377  }
378
379
380  std::string ErrMsg;
381  sys::Path TempDir = sys::Path::GetTemporaryDirectory(&ErrMsg);
382  if (TempDir.isEmpty()) {
383    cerr << "lto: " << ErrMsg << "\n";
384    return LTO_WRITE_FAILURE;
385  }
386  sys::Path tmpAsmFilePath(TempDir);
387  if (!tmpAsmFilePath.appendComponent("lto")) {
388    cerr << "lto: " << ErrMsg << "\n";
389    TempDir.eraseFromDisk(true);
390    return LTO_WRITE_FAILURE;
391  }
392  if (tmpAsmFilePath.createTemporaryFileOnDisk(&ErrMsg)) {
393    cerr << "lto: " << ErrMsg << "\n";
394    TempDir.eraseFromDisk(true);
395    return LTO_WRITE_FAILURE;
396  }
397  sys::RemoveFileOnSignal(tmpAsmFilePath);
398
399  std::ofstream asmFile(tmpAsmFilePath.c_str(), io_mode);
400  if (!asmFile.is_open() || asmFile.bad()) {
401    if (tmpAsmFilePath.exists()) {
402      tmpAsmFilePath.eraseFromDisk();
403      TempDir.eraseFromDisk(true);
404    }
405    return LTO_WRITE_FAILURE;
406  }
407
408  enum LTOStatus status = optimize(bigOne, asmFile, exportList);
409  asmFile.close();
410  if (status != LTO_OPT_SUCCESS) {
411    tmpAsmFilePath.eraseFromDisk();
412    TempDir.eraseFromDisk(true);
413    return status;
414  }
415
416  if (saveTemps) {
417    std::string tempFileName(FinalOutputPath.c_str());
418    tempFileName += "1.bc";
419    std::ofstream Out(tempFileName.c_str(), io_mode);
420    OStream L(Out);
421    WriteBytecodeToFile(bigOne, L);
422  }
423
424  targetTriple = bigOne->getTargetTriple();
425
426  // Run GCC to assemble and link the program into native code.
427  //
428  // Note:
429  //  We can't just assemble and link the file with the system assembler
430  //  and linker because we don't know where to put the _start symbol.
431  //  GCC mysteriously knows how to do it.
432  const sys::Path gcc = sys::Program::FindProgramByName("gcc");
433  if (gcc.isEmpty()) {
434    tmpAsmFilePath.eraseFromDisk();
435    TempDir.eraseFromDisk(true);
436    return LTO_ASM_FAILURE;
437  }
438
439  std::vector<const char*> args;
440  args.push_back(gcc.c_str());
441  args.push_back("-c");
442  args.push_back("-x");
443  args.push_back("assembler");
444  args.push_back("-o");
445  args.push_back(OutputFilename.c_str());
446  args.push_back(tmpAsmFilePath.c_str());
447  args.push_back(0);
448
449  if (sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 1, &ErrMsg)) {
450    cerr << "lto: " << ErrMsg << "\n";
451    return LTO_ASM_FAILURE;
452  }
453
454  tmpAsmFilePath.eraseFromDisk();
455  TempDir.eraseFromDisk(true);
456
457  return LTO_OPT_SUCCESS;
458}
459
460/// Unused pure-virtual destructor. Must remain empty.
461LinkTimeOptimizer::~LinkTimeOptimizer() {}
462
463/// Destruct LTO. Delete all modules, symbols and target.
464LTO::~LTO() {
465
466  for (std::vector<Module *>::iterator itr = modules.begin(), e = modules.end();
467       itr != e; ++itr)
468    delete *itr;
469
470  modules.clear();
471
472  for (NameToSymbolMap::iterator itr = allSymbols.begin(), e = allSymbols.end();
473       itr != e; ++itr)
474    delete itr->second;
475
476  allSymbols.clear();
477
478  delete Target;
479}
480