1 //===-lto.cpp - LLVM Link Time Optimizer ----------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
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 "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"
48 llvm::LinkTimeOptimizer
*createLLVMOptimizer()
50 llvm::LTO
*l
= new llvm::LTO();
54 /// If symbol is not used then make it internal and let optimizer takes
56 void LLVMSymbol::mayBeNotUsed() {
57 gv
->setLinkage(GlobalValue::InternalLinkage
);
60 // Map LLVM LinkageType to LTO LinakgeType
61 static LTOLinkageTypes
62 getLTOLinkageType(GlobalValue
*v
)
65 if (v
->hasExternalLinkage())
66 lt
= LTOExternalLinkage
;
67 else if (v
->hasLinkOnceLinkage())
68 lt
= LTOLinkOnceLinkage
;
69 else if (v
->hasWeakLinkage())
72 // Otherwise it is internal linkage for link time optimizer
73 lt
= LTOInternalLinkage
;
77 // Find exeternal symbols referenced by VALUE. This is a recursive function.
79 findExternalRefs(Value
*value
, std::set
<std::string
> &references
,
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
));
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
);
96 /// If Module with InputFilename is available then remove it from allModules
97 /// and call delete on it.
99 LTO::removeModule (const std::string
&InputFilename
)
101 NameToModuleMap::iterator pos
= allModules
.find(InputFilename
.c_str());
102 if (pos
== allModules
.end())
105 Module
*m
= pos
->second
;
106 allModules
.erase(pos
);
110 /// InputFilename is a LLVM bytecode file. If Module with InputFilename is
111 /// available then return it. Otherwise parseInputFilename.
113 LTO::getModule(const std::string
&InputFilename
)
117 NameToModuleMap::iterator pos
= allModules
.find(InputFilename
.c_str());
118 if (pos
!= allModules
.end())
119 m
= allModules
[InputFilename
.c_str()];
121 if (MemoryBuffer
*Buffer
122 = MemoryBuffer::getFile(&InputFilename
[0], InputFilename
.size())) {
123 m
= ParseBitcodeFile(Buffer
);
126 allModules
[InputFilename
.c_str()] = m
;
131 /// InputFilename is a LLVM bytecode file. Reade this bytecode file and
132 /// set corresponding target triplet string.
134 LTO::getTargetTriple(const std::string
&InputFilename
,
135 std::string
&targetTriple
)
137 Module
*m
= getModule(InputFilename
);
139 targetTriple
= m
->getTargetTriple();
142 /// InputFilename is a LLVM bytecode file. Read it using bytecode 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.
147 LTO::readLLVMObjectFile(const std::string
&InputFilename
,
148 NameToSymbolMap
&symbols
,
149 std::set
<std::string
> &references
)
151 Module
*m
= getModule(InputFilename
);
153 return LTO_READ_FAILURE
;
155 // Collect Target info
159 return LTO_READ_FAILURE
;
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
);
166 for (Module::iterator f
= m
->begin(), e
= m
->end(); f
!= e
; ++f
) {
168 LTOLinkageTypes lt
= getLTOLinkageType(f
);
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
),
176 symbols
[newSymbol
->getMangledName()] = newSymbol
;
177 allSymbols
[newSymbol
->getMangledName()] = newSymbol
;
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();
184 for (unsigned count
= 0, total
= i
->getNumOperands();
185 count
!= total
; ++count
)
186 findExternalRefs(i
->getOperand(count
), references
, mangler
);
189 for (Module::global_iterator v
= m
->global_begin(), e
= m
->global_end();
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
;
201 for (unsigned count
= 0, total
= v
->getNumOperands();
202 count
!= total
; ++count
)
203 findExternalRefs(v
->getOperand(count
), references
, mangler
);
208 return LTO_READ_SUCCESS
;
211 /// Get TargetMachine.
212 /// Use module M to find appropriate Target.
214 LTO::getTarget (Module
*M
) {
220 const TargetMachineRegistry::Entry
* March
=
221 TargetMachineRegistry::getClosestStaticTargetForModule(*M
, Err
);
227 std::string Features
;
228 Target
= March
->CtorFn(*M
, Features
);
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.
236 LTO::optimize(Module
*M
, std::ostream
&Out
,
237 std::vector
<const char *> &exportList
)
239 // Instantiate the pass manager to organize the passes.
242 // Collect Target info
246 return LTO_NO_TARGET
;
248 // Start off with a verification pass.
249 Passes
.add(createVerifierPass());
251 // Add an appropriate TargetData instance for this module...
252 Passes
.add(new TargetData(*Target
->getTargetData()));
254 // Internalize symbols if export list is nonemty
255 if (!exportList
.empty())
256 Passes
.add(createInternalizePass(exportList
));
258 // Now that we internalized some globals, see if we can hack on them!
259 Passes
.add(createGlobalOptimizerPass());
261 // Linking modules together can lead to duplicated global constants, only
262 // keep one copy of each constant...
263 Passes
.add(createConstantMergePass());
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
268 Passes
.add(createStripSymbolsPass());
270 // Propagate constants at call sites into the functions they call.
271 Passes
.add(createIPConstantPropagationPass());
273 // Remove unused arguments from functions...
274 Passes
.add(createDeadArgEliminationPass());
276 Passes
.add(createFunctionInliningPass()); // Inline small functions
278 Passes
.add(createPruneEHPass()); // Remove dead EH info
280 Passes
.add(createGlobalDCEPass()); // Remove dead functions
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());
286 // The IPO passes may leave cruft around. Clean up after them.
287 Passes
.add(createInstructionCombiningPass());
289 Passes
.add(createScalarReplAggregatesPass()); // Break up allocas
291 // Run a few AA driven optimizations here and now, to cleanup the code.
292 Passes
.add(createGlobalsModRefPass()); // IP alias analysis
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
299 // Cleanup and simplify the code after the scalar optimizations.
300 Passes
.add(createInstructionCombiningPass());
302 // Delete basic blocks, which optimization passes may have killed...
303 Passes
.add(createCFGSimplificationPass());
305 // Now that we have optimized the program, discard unreachable functions...
306 Passes
.add(createGlobalDCEPass());
308 // Make sure everything is still good.
309 Passes
.add(createVerifierPass());
311 FunctionPassManager
*CodeGenPasses
=
312 new FunctionPassManager(new ExistingModuleProvider(M
));
314 CodeGenPasses
->add(new TargetData(*Target
->getTargetData()));
316 MachineCodeEmitter
*MCE
= 0;
318 switch (Target
->addPassesToEmitFile(*CodeGenPasses
, Out
,
319 TargetMachine::AssemblyFile
, true)) {
321 case FileModel::Error
:
322 return LTO_WRITE_FAILURE
;
323 case FileModel::AsmFile
:
325 case FileModel::MachOFile
:
326 MCE
= AddMachOWriter(*CodeGenPasses
, Out
, *Target
);
328 case FileModel::ElfFile
:
329 MCE
= AddELFWriter(*CodeGenPasses
, Out
, *Target
);
333 if (Target
->addPassesToEmitFileFinish(*CodeGenPasses
, MCE
, true))
334 return LTO_WRITE_FAILURE
;
336 // Run our queue of passes all at once now, efficiently.
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
);
345 CodeGenPasses
->doFinalization();
347 return LTO_OPT_SUCCESS
;
350 ///Link all modules together and optimize them using IPO. Generate
351 /// native object file using OutputFilename
352 /// Return appropriate LTOStatus.
354 LTO::optimizeModules(const std::string
&OutputFilename
,
355 std::vector
<const char *> &exportList
,
356 std::string
&targetTriple
,
358 const char *FinalOutputFilename
)
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.
374 sys::Path
FinalOutputPath(FinalOutputFilename
);
375 FinalOutputPath
.eraseSuffix();
378 std::string
tempFileName(FinalOutputPath
.c_str());
379 tempFileName
+= "0.bc";
380 std::ofstream
Out(tempFileName
.c_str(), io_mode
);
381 WriteBitcodeToFile(bigOne
, Out
);
384 // Strip leading underscore because it was added to match names
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();
395 sys::Path TempDir
= sys::Path::GetTemporaryDirectory(&ErrMsg
);
396 if (TempDir
.isEmpty()) {
397 cerr
<< "lto: " << ErrMsg
<< "\n";
398 return LTO_WRITE_FAILURE
;
400 sys::Path
tmpAsmFilePath(TempDir
);
401 if (!tmpAsmFilePath
.appendComponent("lto")) {
402 cerr
<< "lto: " << ErrMsg
<< "\n";
403 TempDir
.eraseFromDisk(true);
404 return LTO_WRITE_FAILURE
;
406 if (tmpAsmFilePath
.createTemporaryFileOnDisk(&ErrMsg
)) {
407 cerr
<< "lto: " << ErrMsg
<< "\n";
408 TempDir
.eraseFromDisk(true);
409 return LTO_WRITE_FAILURE
;
411 sys::RemoveFileOnSignal(tmpAsmFilePath
);
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);
419 return LTO_WRITE_FAILURE
;
422 enum LTOStatus status
= optimize(bigOne
, asmFile
, exportList
);
424 if (status
!= LTO_OPT_SUCCESS
) {
425 tmpAsmFilePath
.eraseFromDisk();
426 TempDir
.eraseFromDisk(true);
431 std::string
tempFileName(FinalOutputPath
.c_str());
432 tempFileName
+= "1.bc";
433 std::ofstream
Out(tempFileName
.c_str(), io_mode
);
434 WriteBitcodeToFile(bigOne
, Out
);
437 targetTriple
= bigOne
->getTargetTriple();
439 // Run GCC to assemble and link the program into native code.
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");
447 tmpAsmFilePath
.eraseFromDisk();
448 TempDir
.eraseFromDisk(true);
449 return LTO_ASM_FAILURE
;
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());
462 if (sys::Program::ExecuteAndWait(gcc
, &args
[0], 0, 0, 1, 0, &ErrMsg
)) {
463 cerr
<< "lto: " << ErrMsg
<< "\n";
464 return LTO_ASM_FAILURE
;
467 tmpAsmFilePath
.eraseFromDisk();
468 TempDir
.eraseFromDisk(true);
470 return LTO_OPT_SUCCESS
;
473 void LTO::printVersion() {
474 cl::PrintVersionMessage();
477 /// Unused pure-virtual destructor. Must remain empty.
478 LinkTimeOptimizer::~LinkTimeOptimizer() {}
480 /// Destruct LTO. Delete all modules, symbols and target.
483 for (std::vector
<Module
*>::iterator itr
= modules
.begin(), e
= modules
.end();
489 for (NameToSymbolMap::iterator itr
= allSymbols
.begin(), e
= allSymbols
.end();