1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
//===- Optimize.cpp - Optimize a complete program -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements all optimization of the linked module for llvm-ld.
//
//===----------------------------------------------------------------------===//
#include "llvm/Module.h"
#include "llvm/PassManager.h"
#include "llvm/Analysis/LoadValueNumbering.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/System/DynamicLibrary.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Support/PassNameParser.h"
#include "llvm/Support/PluginLoader.h"
#include <iostream>
using namespace llvm;
// Pass Name Options as generated by the PassNameParser
static cl::list<const PassInfo*, bool, PassNameParser>
OptimizationList(cl::desc("Optimizations available:"));
// Optimization Enumeration
enum OptimizationLevels {
OPT_FAST_COMPILE = 1,
OPT_SIMPLE = 2,
OPT_AGGRESSIVE = 3,
OPT_LINK_TIME = 4,
OPT_AGGRESSIVE_LINK_TIME = 5
};
// Optimization Options
static cl::opt<OptimizationLevels> OptLevel(
cl::desc("Choose level of optimization to apply:"),
cl::init(OPT_FAST_COMPILE), cl::values(
clEnumValN(OPT_FAST_COMPILE,"O0",
"An alias for the -O1 option."),
clEnumValN(OPT_FAST_COMPILE,"O1",
"Optimize for linking speed, not execution speed."),
clEnumValN(OPT_SIMPLE,"O2",
"Perform only required/minimal optimizations"),
clEnumValN(OPT_AGGRESSIVE,"O3",
"An alias for the -O2 option."),
clEnumValN(OPT_LINK_TIME,"O4",
"Perform standard link time optimizations"),
clEnumValN(OPT_AGGRESSIVE_LINK_TIME,"O5",
"Perform aggressive link time optimizations"),
clEnumValEnd
)
);
static cl::opt<bool> DisableInline("disable-inlining",
cl::desc("Do not run the inliner pass"));
static cl::opt<bool>
DisableOptimizations("disable-opt",
cl::desc("Do not run any optimization passes"));
static cl::opt<bool> DisableInternalize("disable-internalize",
cl::desc("Do not mark all symbols as internal"));
static cl::opt<bool> VerifyEach("verify-each",
cl::desc("Verify intermediate results of all passes"));
static cl::opt<bool> Strip("s",
cl::desc("Strip symbol info from executable"));
static cl::alias ExportDynamic("export-dynamic",
cl::aliasopt(DisableInternalize),
cl::desc("Alias for -disable-internalize"));
// A utility function that adds a pass to the pass manager but will also add
// a verifier pass after if we're supposed to verify.
static inline void addPass(PassManager &PM, Pass *P) {
// Add the pass to the pass manager...
PM.add(P);
// If we are verifying all of the intermediate steps, add the verifier...
if (VerifyEach)
PM.add(createVerifierPass());
}
namespace llvm {
/// Optimize - Perform link time optimizations. This will run the scalar
/// optimizations, any loaded plugin-optimization modules, and then the
/// inter-procedural optimizations if applicable.
void Optimize(Module* M) {
// Instantiate the pass manager to organize the passes.
PassManager Passes;
// If we're verifying, start off with a verification pass.
if (VerifyEach)
Passes.add(createVerifierPass());
// Add an appropriate TargetData instance for this module...
addPass(Passes, new TargetData(M));
if (!DisableOptimizations) {
// Now that composite has been compiled, scan through the module, looking
// for a main function. If main is defined, mark all other functions
// internal.
addPass(Passes, createInternalizePass(!DisableInternalize));
// Now that we internalized some globals, see if we can hack on them!
addPass(Passes, createGlobalOptimizerPass());
// Linking modules together can lead to duplicated global constants, only
// keep one copy of each constant...
addPass(Passes, createConstantMergePass());
// If the -s command line option was specified, strip the symbols out of the
// resulting program to make it smaller. -s is a GLD option that we are
// supporting.
if (Strip)
addPass(Passes, createStripSymbolsPass());
// Propagate constants at call sites into the functions they call.
addPass(Passes, createIPConstantPropagationPass());
// Remove unused arguments from functions...
addPass(Passes, createDeadArgEliminationPass());
if (!DisableInline)
addPass(Passes, createFunctionInliningPass()); // Inline small functions
addPass(Passes, createPruneEHPass()); // Remove dead EH info
addPass(Passes, createGlobalDCEPass()); // Remove dead functions
// If we didn't decide to inline a function, check to see if we can
// transform it to pass arguments by value instead of by reference.
addPass(Passes, createArgumentPromotionPass());
// The IPO passes may leave cruft around. Clean up after them.
addPass(Passes, createInstructionCombiningPass());
addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
// Run a few AA driven optimizations, to cleanup the code.
addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
addPass(Passes, createLICMPass()); // Hoist loop invariants
addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
addPass(Passes, createGCSEPass()); // Remove common subexprs
addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
// Cleanup and simplify the code after the scalar optimizations.
addPass(Passes, createInstructionCombiningPass());
// Delete basic blocks, which optimization passes may have killed...
addPass(Passes, createCFGSimplificationPass());
// Now that we have optimized the program, discard unreachable functions...
addPass(Passes, createGlobalDCEPass());
}
// Create a new optimization pass for each one specified on the command line
std::auto_ptr<TargetMachine> target;
for (unsigned i = 0; i < OptimizationList.size(); ++i) {
const PassInfo *Opt = OptimizationList[i];
if (Opt->getNormalCtor())
addPass(Passes, Opt->getNormalCtor()());
else
std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName()
<< "\n";
}
// The user's passes may leave cruft around. Clean up after them them but
// only if we haven't got DisableOptimizations set
if (!DisableOptimizations) {
addPass(Passes, createInstructionCombiningPass());
addPass(Passes, createCFGSimplificationPass());
addPass(Passes, createGlobalDCEPass());
}
// Make sure everything is still good.
Passes.add(createVerifierPass());
// Run our queue of passes all at once now, efficiently.
Passes.run(*M);
}
}
|