Mercurial > projects > ldc
annotate gen/optimizer.cpp @ 1172:b3887714b735
Small cleanup: remove mars.h #include
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Sun, 29 Mar 2009 16:47:19 +0200 |
parents | 461a85f0db31 |
children | cc1efa23030a |
rev | line source |
---|---|
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
1 #include "gen/optimizer.h" |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
2 |
125 | 3 #include "llvm/PassManager.h" |
4 #include "llvm/LinkAllPasses.h" | |
5 #include "llvm/Analysis/LoopPass.h" | |
6 #include "llvm/Target/TargetData.h" | |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
7 #include "llvm/Support/CommandLine.h" |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
8 #include "llvm/Support/PassNameParser.h" |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
9 |
1172
b3887714b735
Small cleanup: remove mars.h #include
Frits van Bommel <fvbommel wxs.nl>
parents:
1171
diff
changeset
|
10 #include "root.h" // error() |
125 | 11 |
12 using namespace llvm; | |
13 | |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
14 // Allow the user to specify specific optimizations to run. |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
15 static cl::list<const PassInfo*, bool, PassNameParser> |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
16 passList( |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
17 cl::desc("Running specific optimizations:"), |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
18 cl::Hidden // to clean up --help output |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
19 ); |
125 | 20 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
21 static cl::opt<char> optimizeLevel( |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
22 cl::desc("Setting the optimization level:"), |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
23 cl::ZeroOrMore, |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
24 cl::values( |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
25 clEnumValN(2, "O", "Equivalent to -O2"), |
1171
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
26 clEnumValN(0, "O0", "No optimizations (default)"), |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
27 clEnumValN(1, "O1", "Simple optimizations"), |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
28 clEnumValN(2, "O2", "Good optimizations"), |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
29 clEnumValN(3, "O3", "Aggressive optimizations"), |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
30 clEnumValN(4, "O4", "Link-time optimization"), // not implemented? |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
31 clEnumValN(5, "O5", "Link-time optimization"), // not implemented? |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
32 clEnumValEnd), |
1171
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
33 cl::init(0)); |
125 | 34 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
35 static cl::opt<bool> enableInlining("enable-inlining", |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
36 cl::desc("Enable function inlining (in -O<N>, if given)"), |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
37 cl::ZeroOrMore, |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
38 cl::init(false)); |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
39 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
40 // Some accessors for the linker: (llvm-ld version only, currently unused?) |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
41 bool doInline() { |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
42 return enableInlining; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
43 } |
131 | 44 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
45 int optLevel() { |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
46 return optimizeLevel; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
47 } |
125 | 48 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
49 bool optimize() { |
1171
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
50 return optimizeLevel || enableInlining || passList.empty(); |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
51 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
52 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
53 // this function inserts some or all of the std-compile-opts passes depending on the |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
54 // optimization level given. |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
55 static void addPassesForOptLevel(PassManager& pm) { |
1171
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
56 // -O1 |
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
57 if (optimizeLevel >= 1) |
131 | 58 { |
59 //pm.add(createStripDeadPrototypesPass()); | |
60 pm.add(createGlobalDCEPass()); | |
125 | 61 pm.add(createRaiseAllocationsPass()); |
62 pm.add(createCFGSimplificationPass()); | |
63 pm.add(createPromoteMemoryToRegisterPass()); | |
129
8096ba7082db
[svn r133] Fixed some problems with inlining not happening :P
lindquist
parents:
125
diff
changeset
|
64 pm.add(createGlobalOptimizerPass()); |
8096ba7082db
[svn r133] Fixed some problems with inlining not happening :P
lindquist
parents:
125
diff
changeset
|
65 pm.add(createGlobalDCEPass()); |
125 | 66 } |
67 | |
131 | 68 // -O2 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
69 if (optimizeLevel >= 2) |
125 | 70 { |
71 pm.add(createIPConstantPropagationPass()); | |
72 pm.add(createDeadArgEliminationPass()); | |
73 pm.add(createInstructionCombiningPass()); | |
607
9526b29ae342
Fixed the optimizer thing, since llvm PR 2800 is already fixed, users need to upgrade LLVM to latest svn.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
605
diff
changeset
|
74 pm.add(createCFGSimplificationPass()); |
125 | 75 pm.add(createPruneEHPass()); |
76 } | |
77 | |
131 | 78 // -inline |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
79 if (enableInlining) { |
125 | 80 pm.add(createFunctionInliningPass()); |
81 } | |
82 | |
131 | 83 // -O3 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
84 if (optimizeLevel >= 3) |
125 | 85 { |
86 pm.add(createArgumentPromotionPass()); | |
87 pm.add(createTailDuplicationPass()); | |
88 pm.add(createInstructionCombiningPass()); | |
89 pm.add(createCFGSimplificationPass()); | |
90 pm.add(createScalarReplAggregatesPass()); | |
91 pm.add(createInstructionCombiningPass()); | |
92 pm.add(createCondPropagationPass()); | |
93 | |
94 pm.add(createTailCallEliminationPass()); | |
95 pm.add(createCFGSimplificationPass()); | |
96 pm.add(createReassociatePass()); | |
97 pm.add(createLoopRotatePass()); | |
98 pm.add(createLICMPass()); | |
99 pm.add(createLoopUnswitchPass()); | |
100 pm.add(createInstructionCombiningPass()); | |
101 pm.add(createIndVarSimplifyPass()); | |
102 pm.add(createLoopUnrollPass()); | |
103 pm.add(createInstructionCombiningPass()); | |
104 pm.add(createGVNPass()); | |
105 pm.add(createSCCPPass()); | |
106 | |
107 pm.add(createInstructionCombiningPass()); | |
108 pm.add(createCondPropagationPass()); | |
109 | |
110 pm.add(createDeadStoreEliminationPass()); | |
111 pm.add(createAggressiveDCEPass()); | |
112 pm.add(createCFGSimplificationPass()); | |
113 pm.add(createSimplifyLibCallsPass()); | |
114 pm.add(createDeadTypeEliminationPass()); | |
115 pm.add(createConstantMergePass()); | |
116 } | |
117 | |
131 | 118 // level -O4 and -O5 are linktime optimizations |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
119 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
120 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
121 ////////////////////////////////////////////////////////////////////////////////////////// |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
122 // This function runs optimization passes based on command line arguments. |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
123 // Returns true if any optimization passes were invoked. |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
124 bool ldc_optimize_module(llvm::Module* m) |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
125 { |
1171
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
126 if (!enableInlining && optimizeLevel == 0 && passList.empty()) |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
127 return false; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
128 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
129 PassManager pm; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
130 pm.add(new TargetData(m)); |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
131 |
1171
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
132 bool optimize = optimizeLevel != 0 || enableInlining; |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
133 |
1171
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
134 unsigned optPos = optimizeLevel != 0 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
135 ? optimizeLevel.getPosition() |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
136 : enableInlining.getPosition(); |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
137 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
138 for (size_t i = 0; i < passList.size(); i++) { |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
139 // insert -O<N> / -enable-inlining in right position |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
140 if (optimize && optPos < passList.getPosition(i)) { |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
141 addPassesForOptLevel(pm); |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
142 optimize = false; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
143 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
144 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
145 const PassInfo* pass = passList[i]; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
146 if (PassInfo::NormalCtor_t ctor = pass->getNormalCtor()) { |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
147 pm.add(ctor()); |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
148 } else { |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
149 const char* arg = pass->getPassArgument(); // may return null |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
150 if (arg) |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
151 error("Can't create pass '-%s' (%s)", arg, pass->getPassName()); |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
152 else |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
153 error("Can't create pass (%s)", pass->getPassName()); |
1172
b3887714b735
Small cleanup: remove mars.h #include
Frits van Bommel <fvbommel wxs.nl>
parents:
1171
diff
changeset
|
154 assert(0); // Should be unreachable; root.h:error() calls exit() |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
155 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
156 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
157 // insert -O<N> / -enable-inlining if specified at the end, |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
158 if (optimize) |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
159 addPassesForOptLevel(pm); |
125 | 160 |
131 | 161 pm.run(*m); |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
162 return true; |
125 | 163 } |