lto.cpp revision 08fb05c3ac440979021f508bfee073359be46f7e
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 implementes 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/LinkTimeOptimizer.h"
41#include <fstream>
42#include <iostream>
43
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{
343  if (modules.empty())
344    return LTO_NO_WORK;
345
346  std::ios::openmode io_mode =
347    std::ios::out | std::ios::trunc | std::ios::binary;
348  std::string *errMsg = NULL;
349  Module *bigOne = modules[0];
350  Linker theLinker("LinkTimeOptimizer", bigOne, false);
351  for (unsigned i = 1, e = modules.size(); i != e; ++i)
352    if (theLinker.LinkModules(bigOne, modules[i], errMsg))
353      return LTO_MODULE_MERGE_FAILURE;
354
355#if 0
356  // Enable this when -save-temps is used
357  std::ofstream Out("big.bc", io_mode);
358  WriteBytecodeToFile(bigOne, Out, true);
359#endif
360
361  // Strip leading underscore because it was added to match names
362  // seen by linker.
363  for (unsigned i = 0, e = exportList.size(); i != e; ++i) {
364    const char *name = exportList[i];
365    NameToSymbolMap::iterator itr = allSymbols.find(name);
366    if (itr != allSymbols.end())
367      exportList[i] = allSymbols[name]->getName();
368  }
369
370
371  std::string ErrMsg;
372  sys::Path TempDir = sys::Path::GetTemporaryDirectory(&ErrMsg);
373  if (TempDir.isEmpty()) {
374    std::cerr << "lto: " << ErrMsg << "\n";
375    return LTO_WRITE_FAILURE;
376  }
377  sys::Path tmpAsmFilePath(TempDir);
378  if (!tmpAsmFilePath.appendComponent("lto")) {
379    std::cerr << "lto: " << ErrMsg << "\n";
380    TempDir.eraseFromDisk(true);
381    return LTO_WRITE_FAILURE;
382  }
383  if (tmpAsmFilePath.createTemporaryFileOnDisk(&ErrMsg)) {
384    std::cerr << "lto: " << ErrMsg << "\n";
385    TempDir.eraseFromDisk(true);
386    return LTO_WRITE_FAILURE;
387  }
388  sys::RemoveFileOnSignal(tmpAsmFilePath);
389
390  std::ofstream asmFile(tmpAsmFilePath.c_str(), io_mode);
391  if (!asmFile.is_open() || asmFile.bad()) {
392    if (tmpAsmFilePath.exists()) {
393      tmpAsmFilePath.eraseFromDisk();
394      TempDir.eraseFromDisk(true);
395    }
396    return LTO_WRITE_FAILURE;
397  }
398
399  enum LTOStatus status = optimize(bigOne, asmFile, exportList);
400  asmFile.close();
401  if (status != LTO_OPT_SUCCESS) {
402    tmpAsmFilePath.eraseFromDisk();
403    TempDir.eraseFromDisk(true);
404    return status;
405  }
406
407  targetTriple = bigOne->getTargetTriple();
408
409  // Run GCC to assemble and link the program into native code.
410  //
411  // Note:
412  //  We can't just assemble and link the file with the system assembler
413  //  and linker because we don't know where to put the _start symbol.
414  //  GCC mysteriously knows how to do it.
415  const sys::Path gcc = sys::Program::FindProgramByName("gcc");
416  if (gcc.isEmpty()) {
417    tmpAsmFilePath.eraseFromDisk();
418    TempDir.eraseFromDisk(true);
419    return LTO_ASM_FAILURE;
420  }
421
422  std::vector<const char*> args;
423  args.push_back(gcc.c_str());
424  args.push_back("-c");
425  args.push_back("-x");
426  args.push_back("assembler");
427  args.push_back("-o");
428  args.push_back(OutputFilename.c_str());
429  args.push_back(tmpAsmFilePath.c_str());
430  args.push_back(0);
431
432  if (sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 1, &ErrMsg)) {
433    std::cerr << "lto: " << ErrMsg << "\n";
434    return LTO_ASM_FAILURE;
435  }
436
437  tmpAsmFilePath.eraseFromDisk();
438  TempDir.eraseFromDisk(true);
439
440  return LTO_OPT_SUCCESS;
441}
442
443/// Destruct LTO. Delete all modules, symbols and target.
444LTO::~LTO() {
445
446  for (std::vector<Module *>::iterator itr = modules.begin(), e = modules.end();
447       itr != e; ++itr)
448    delete *itr;
449
450  modules.clear();
451
452  for (NameToSymbolMap::iterator itr = allSymbols.begin(), e = allSymbols.end();
453       itr != e; ++itr)
454    delete itr->second;
455
456  allSymbols.clear();
457
458  delete Target;
459}
460