lto.cpp revision 304d5f2edc954a3ef9904ea6d8f9421f40c9abe4
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/System/Program.h"
27#include "llvm/System/Signals.h"
28#include "llvm/Analysis/Passes.h"
29#include "llvm/Analysis/Verifier.h"
30#include "llvm/Target/SubtargetFeature.h"
31#include "llvm/Target/TargetData.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/Target/TargetMachineRegistry.h"
34#include "llvm/Transforms/IPO.h"
35#include "llvm/Transforms/Scalar.h"
36#include "llvm/Analysis/LoadValueNumbering.h"
37#include "llvm/LinkTimeOptimizer.h"
38#include <fstream>
39#include <iostream>
40
41using namespace llvm;
42
43extern "C"
44llvm::LinkTimeOptimizer *createLLVMOptimizer()
45{
46  llvm::LinkTimeOptimizer *l = new llvm::LinkTimeOptimizer();
47  return l;
48}
49
50
51
52/// If symbol is not used then make it internal and let optimizer takes
53/// care of it.
54void LLVMSymbol::mayBeNotUsed() {
55  gv->setLinkage(GlobalValue::InternalLinkage);
56}
57
58// Helper routine
59// FIXME : Take advantage of GlobalPrefix from AsmPrinter
60static const char *addUnderscore(const char *name) {
61  size_t namelen = strlen(name);
62  char *symName = (char*)malloc(namelen+2);
63  symName[0] = '_';
64  strcpy(&symName[1], name);
65  return symName;
66}
67
68// Map LLVM LinkageType to LTO LinakgeType
69static LTOLinkageTypes
70getLTOLinkageType(GlobalValue *v)
71{
72  LTOLinkageTypes lt;
73  if (v->hasExternalLinkage())
74    lt = LTOExternalLinkage;
75  else if (v->hasLinkOnceLinkage())
76    lt = LTOLinkOnceLinkage;
77  else if (v->hasWeakLinkage())
78    lt = LTOWeakLinkage;
79  else
80    // Otherwise it is internal linkage for link time optimizer
81    lt = LTOInternalLinkage;
82  return lt;
83}
84
85// Find exeternal symbols referenced by VALUE. This is a recursive function.
86static void
87findExternalRefs(Value *value, std::set<const char *> &references) {
88
89  if (GlobalValue *gv = dyn_cast<GlobalValue>(value)) {
90    LTOLinkageTypes lt = getLTOLinkageType(gv);
91    if (lt != LTOInternalLinkage && strncmp (gv->getName().c_str(), "llvm.", 5))
92      references.insert(addUnderscore(gv->getName().c_str()));
93  }
94  else if (Constant *c = dyn_cast<Constant>(value))
95    // Handle ConstantExpr, ConstantStruct, ConstantArry etc..
96    for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i)
97      findExternalRefs(c->getOperand(i), references);
98}
99
100/// InputFilename is a LLVM bytecode file. Read it using bytecode reader.
101/// Collect global functions and symbol names in symbols vector.
102/// Collect external references in references vector.
103/// Return LTO_READ_SUCCESS if there is no error.
104enum LTOStatus
105LinkTimeOptimizer::readLLVMObjectFile(const std::string &InputFilename,
106				      NameToSymbolMap &symbols,
107				      std::set<const char *> &references)
108{
109  Module *m = ParseBytecodeFile(InputFilename);
110  if (!m)
111    return LTO_READ_FAILURE;
112
113  modules.push_back(m);
114
115  for (Module::iterator f = m->begin(), e = m->end(); f != e; ++f) {
116
117    LTOLinkageTypes lt = getLTOLinkageType(f);
118
119    if (!f->isExternal() && lt != LTOInternalLinkage
120	&& strncmp (f->getName().c_str(), "llvm.", 5)) {
121      const char *name = addUnderscore(f->getName().c_str());
122      LLVMSymbol *newSymbol = new LLVMSymbol(lt, f);
123      symbols[name] = newSymbol;
124      allSymbols[name] = newSymbol;
125    }
126
127    // Collect external symbols referenced by this function.
128    for (Function::iterator b = f->begin(), fe = f->end(); b != fe; ++b)
129      for (BasicBlock::iterator i = b->begin(), be = b->end();
130	   i != be; ++i)
131	for (unsigned count = 0, total = i->getNumOperands();
132	     count != total; ++count)
133	  findExternalRefs(i->getOperand(count), references);
134  }
135
136  for (Module::global_iterator v = m->global_begin(), e = m->global_end();
137       v !=  e; ++v) {
138    LTOLinkageTypes lt = getLTOLinkageType(v);
139    if (!v->isExternal() && lt != LTOInternalLinkage
140	&& strncmp (v->getName().c_str(), "llvm.", 5)) {
141      const char *name = addUnderscore(v->getName().c_str());
142      LLVMSymbol *newSymbol = new LLVMSymbol(lt,v);
143      symbols[name] = newSymbol;
144
145      for (unsigned count = 0, total = v->getNumOperands();
146	   count != total; ++count)
147	findExternalRefs(v->getOperand(count), references);
148
149    }
150  }
151
152  return LTO_READ_SUCCESS;
153}
154
155/// Optimize module M using various IPO passes. Use exportList to
156/// internalize selected symbols. Target platform is selected
157/// based on information available to module M. No new target
158/// features are selected.
159static enum LTOStatus lto_optimize(Module *M, std::ostream &Out,
160				   std::vector<const char *> &exportList)
161{
162  // Instantiate the pass manager to organize the passes.
163  PassManager Passes;
164
165  // Collect Target info
166  std::string Err;
167  const TargetMachineRegistry::Entry* March =
168    TargetMachineRegistry::getClosestStaticTargetForModule(*M, Err);
169
170  if (March == 0)
171    return LTO_NO_TARGET;
172
173  // Create target
174  std::string Features;
175  std::auto_ptr<TargetMachine> target(March->CtorFn(*M, Features));
176  if (!target.get())
177    return LTO_NO_TARGET;
178
179  TargetMachine &Target = *target.get();
180
181  // Start off with a verification pass.
182  Passes.add(createVerifierPass());
183
184  // Add an appropriate TargetData instance for this module...
185  Passes.add(new TargetData(*Target.getTargetData()));
186
187  // Often if the programmer does not specify proper prototypes for the
188  // functions they are calling, they end up calling a vararg version of the
189  // function that does not get a body filled in (the real function has typed
190  // arguments).  This pass merges the two functions.
191  Passes.add(createFunctionResolvingPass());
192
193  // Internalize symbols if export list is nonemty
194  if (!exportList.empty())
195    Passes.add(createInternalizePass(exportList));
196
197  // Now that we internalized some globals, see if we can hack on them!
198  Passes.add(createGlobalOptimizerPass());
199
200  // Linking modules together can lead to duplicated global constants, only
201  // keep one copy of each constant...
202  Passes.add(createConstantMergePass());
203
204  // If the -s command line option was specified, strip the symbols out of the
205  // resulting program to make it smaller.  -s is a GLD option that we are
206  // supporting.
207  Passes.add(createStripSymbolsPass());
208
209  // Propagate constants at call sites into the functions they call.
210  Passes.add(createIPConstantPropagationPass());
211
212  // Remove unused arguments from functions...
213  Passes.add(createDeadArgEliminationPass());
214
215  Passes.add(createFunctionInliningPass()); // Inline small functions
216
217  Passes.add(createPruneEHPass());            // Remove dead EH info
218
219  Passes.add(createGlobalDCEPass());          // Remove dead functions
220
221  // If we didn't decide to inline a function, check to see if we can
222  // transform it to pass arguments by value instead of by reference.
223  Passes.add(createArgumentPromotionPass());
224
225  // The IPO passes may leave cruft around.  Clean up after them.
226  Passes.add(createInstructionCombiningPass());
227
228  Passes.add(createScalarReplAggregatesPass()); // Break up allocas
229
230  // Run a few AA driven optimizations here and now, to cleanup the code.
231  Passes.add(createGlobalsModRefPass());      // IP alias analysis
232
233  Passes.add(createLICMPass());               // Hoist loop invariants
234  Passes.add(createLoadValueNumberingPass()); // GVN for load instrs
235  Passes.add(createGCSEPass());               // Remove common subexprs
236  Passes.add(createDeadStoreEliminationPass()); // Nuke dead stores
237
238  // Cleanup and simplify the code after the scalar optimizations.
239  Passes.add(createInstructionCombiningPass());
240
241  // Delete basic blocks, which optimization passes may have killed...
242  Passes.add(createCFGSimplificationPass());
243
244  // Now that we have optimized the program, discard unreachable functions...
245  Passes.add(createGlobalDCEPass());
246
247  // Make sure everything is still good.
248  Passes.add(createVerifierPass());
249
250  Target.addPassesToEmitFile(Passes, Out, TargetMachine::AssemblyFile, true);
251
252  // Run our queue of passes all at once now, efficiently.
253  Passes.run(*M);
254
255  return LTO_OPT_SUCCESS;
256}
257
258///Link all modules together and optimize them using IPO. Generate
259/// native object file using OutputFilename
260/// Return appropriate LTOStatus.
261enum LTOStatus
262LinkTimeOptimizer::optimizeModules(const std::string &OutputFilename,
263				   std::vector<const char *> &exportList)
264{
265  if (modules.empty())
266    return LTO_NO_WORK;
267
268  std::ios::openmode io_mode =
269    std::ios::out | std::ios::trunc | std::ios::binary;
270  std::string *errMsg = NULL;
271  Module *bigOne = modules[0];
272  Linker theLinker("LinkTimeOptimizer", bigOne, false);
273  for (unsigned i = 1, e = modules.size(); i != e; ++i)
274    if (theLinker.LinkModules(bigOne, modules[i], errMsg))
275      return LTO_MODULE_MERGE_FAILURE;
276
277#if 0
278  // Enable this when -save-temps is used
279  std::ofstream Out("big.bc", io_mode);
280  WriteBytecodeToFile(bigOne, Out, true);
281#endif
282
283  // Strip leading underscore because it was added to match names
284  // seen by linker.
285  for (unsigned i = 0, e = exportList.size(); i != e; ++i) {
286    const char *name = exportList[i];
287    if (strlen(name) > 2 && name[0] == '_')
288      exportList[i] = &name[1];
289  }
290
291  sys::Path tmpAsmFilePath("/tmp/");
292  tmpAsmFilePath.createTemporaryFileOnDisk();
293  sys::RemoveFileOnSignal(tmpAsmFilePath);
294
295  std::ofstream asmFile(tmpAsmFilePath.c_str(), io_mode);
296  if (!asmFile.is_open() || asmFile.bad()) {
297    if (tmpAsmFilePath.exists())
298      tmpAsmFilePath.eraseFromDisk();
299    return LTO_WRITE_FAILURE;
300  }
301
302  enum LTOStatus status = lto_optimize(bigOne, asmFile, exportList);
303  asmFile.close();
304  if (status != LTO_OPT_SUCCESS) {
305    tmpAsmFilePath.eraseFromDisk();
306    return status;
307  }
308
309  // Run GCC to assemble and link the program into native code.
310  //
311  // Note:
312  //  We can't just assemble and link the file with the system assembler
313  //  and linker because we don't know where to put the _start symbol.
314  //  GCC mysteriously knows how to do it.
315  const sys::Path gcc = FindExecutable("gcc", "/");
316  if (gcc.isEmpty()) {
317    tmpAsmFilePath.eraseFromDisk();
318    return LTO_ASM_FAILURE;
319  }
320
321  std::vector<const char*> args;
322  args.push_back(gcc.c_str());
323  args.push_back("-c");
324  args.push_back("-x");
325  args.push_back("assembler");
326  args.push_back("-o");
327  args.push_back(OutputFilename.c_str());
328  args.push_back(tmpAsmFilePath.c_str());
329  args.push_back(0);
330
331  int R1 = sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 1);
332
333  tmpAsmFilePath.eraseFromDisk();
334
335  return LTO_OPT_SUCCESS;
336}
337