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