Mercurial > projects > ldc
comparison gen/optimizer.cpp @ 1296:79b201533cf8
Add -verify-each option to ease debugging
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Sun, 03 May 2009 21:58:28 +0200 |
parents | dd84ec4353ce |
children | 42bd767ec5a4 |
comparison
equal
deleted
inserted
replaced
1295:0e79fb40c4d0 | 1296:79b201533cf8 |
---|---|
4 #include "gen/passes/Passes.h" | 4 #include "gen/passes/Passes.h" |
5 | 5 |
6 #include "llvm/PassManager.h" | 6 #include "llvm/PassManager.h" |
7 #include "llvm/LinkAllPasses.h" | 7 #include "llvm/LinkAllPasses.h" |
8 #include "llvm/Analysis/LoopPass.h" | 8 #include "llvm/Analysis/LoopPass.h" |
9 #include "llvm/Analysis/Verifier.h" | |
9 #include "llvm/Target/TargetData.h" | 10 #include "llvm/Target/TargetData.h" |
10 #include "llvm/Support/CommandLine.h" | 11 #include "llvm/Support/CommandLine.h" |
11 #include "llvm/Support/PassNameParser.h" | 12 #include "llvm/Support/PassNameParser.h" |
12 | 13 |
13 #include "root.h" // error() | 14 #include "root.h" // error() |
34 clEnumValN(5, "O5", "Link-time optimization"), // not implemented? | 35 clEnumValN(5, "O5", "Link-time optimization"), // not implemented? |
35 clEnumValEnd), | 36 clEnumValEnd), |
36 cl::init(0)); | 37 cl::init(0)); |
37 | 38 |
38 static cl::opt<bool> | 39 static cl::opt<bool> |
40 verifyEach("verify-each", | |
41 cl::desc("Run verifier after each optimization pass"), | |
42 cl::Hidden, | |
43 cl::ZeroOrMore); | |
44 | |
45 static cl::opt<bool> | |
39 disableLangSpecificPasses("disable-d-passes", | 46 disableLangSpecificPasses("disable-d-passes", |
40 cl::desc("Disable D-specific passes in -O<N>"), | 47 cl::desc("Disable D-specific passes in -O<N>"), |
41 cl::ZeroOrMore); | 48 cl::ZeroOrMore); |
42 | 49 |
43 static cl::opt<bool> | 50 static cl::opt<bool> |
78 | 85 |
79 bool optimize() { | 86 bool optimize() { |
80 return optimizeLevel || doInline() || !passList.empty(); | 87 return optimizeLevel || doInline() || !passList.empty(); |
81 } | 88 } |
82 | 89 |
90 static void addPass(PassManager& pm, Pass* pass) { | |
91 pm.add(pass); | |
92 | |
93 if (verifyEach) pm.add(createVerifierPass()); | |
94 } | |
95 | |
83 // this function inserts some or all of the std-compile-opts passes depending on the | 96 // this function inserts some or all of the std-compile-opts passes depending on the |
84 // optimization level given. | 97 // optimization level given. |
85 static void addPassesForOptLevel(PassManager& pm) { | 98 static void addPassesForOptLevel(PassManager& pm) { |
86 // -O1 | 99 // -O1 |
87 if (optimizeLevel >= 1) | 100 if (optimizeLevel >= 1) |
88 { | 101 { |
89 //pm.add(createStripDeadPrototypesPass()); | 102 //addPass(pm, createStripDeadPrototypesPass()); |
90 pm.add(createGlobalDCEPass()); | 103 addPass(pm, createGlobalDCEPass()); |
91 pm.add(createRaiseAllocationsPass()); | 104 addPass(pm, createRaiseAllocationsPass()); |
92 pm.add(createCFGSimplificationPass()); | 105 addPass(pm, createCFGSimplificationPass()); |
93 if (optimizeLevel == 1) | 106 if (optimizeLevel == 1) |
94 pm.add(createPromoteMemoryToRegisterPass()); | 107 addPass(pm, createPromoteMemoryToRegisterPass()); |
95 else | 108 else |
96 pm.add(createScalarReplAggregatesPass()); | 109 addPass(pm, createScalarReplAggregatesPass()); |
97 pm.add(createGlobalOptimizerPass()); | 110 addPass(pm, createGlobalOptimizerPass()); |
98 pm.add(createGlobalDCEPass()); | 111 addPass(pm, createGlobalDCEPass()); |
99 } | 112 } |
100 | 113 |
101 // -O2 | 114 // -O2 |
102 if (optimizeLevel >= 2) | 115 if (optimizeLevel >= 2) |
103 { | 116 { |
104 pm.add(createIPConstantPropagationPass()); | 117 addPass(pm, createIPConstantPropagationPass()); |
105 pm.add(createDeadArgEliminationPass()); | 118 addPass(pm, createDeadArgEliminationPass()); |
106 pm.add(createInstructionCombiningPass()); | 119 addPass(pm, createInstructionCombiningPass()); |
107 pm.add(createCFGSimplificationPass()); | 120 addPass(pm, createCFGSimplificationPass()); |
108 pm.add(createPruneEHPass()); | 121 addPass(pm, createPruneEHPass()); |
109 | 122 |
110 #ifdef USE_METADATA | 123 #ifdef USE_METADATA |
111 if (!disableLangSpecificPasses && !disableGCToStack) | 124 if (!disableLangSpecificPasses && !disableGCToStack) |
112 pm.add(createGarbageCollect2Stack()); | 125 addPass(pm, createGarbageCollect2Stack()); |
113 #endif | 126 #endif |
114 } | 127 } |
115 | 128 |
116 // -inline | 129 // -inline |
117 if (doInline()) { | 130 if (doInline()) { |
118 pm.add(createFunctionInliningPass()); | 131 addPass(pm, createFunctionInliningPass()); |
119 | 132 |
120 if (optimizeLevel >= 2) { | 133 if (optimizeLevel >= 2) { |
121 // Run some optimizations to clean up after inlining. | 134 // Run some optimizations to clean up after inlining. |
122 pm.add(createScalarReplAggregatesPass()); | 135 addPass(pm, createScalarReplAggregatesPass()); |
123 pm.add(createInstructionCombiningPass()); | 136 addPass(pm, createInstructionCombiningPass()); |
124 | 137 |
125 #ifdef USE_METADATA | 138 #ifdef USE_METADATA |
126 if (!disableLangSpecificPasses && !disableGCToStack) | 139 if (!disableLangSpecificPasses && !disableGCToStack) |
127 pm.add(createGarbageCollect2Stack()); | 140 addPass(pm, createGarbageCollect2Stack()); |
128 #endif | 141 #endif |
129 | 142 |
130 // Inline again, to catch things like foreach delegates | 143 // Inline again, to catch things like foreach delegates |
131 // passed to inlined opApply's where the function wasn't | 144 // passed to inlined opApply's where the function wasn't |
132 // known during the first inliner pass. | 145 // known during the first inliner pass. |
133 pm.add(createFunctionInliningPass()); | 146 addPass(pm, createFunctionInliningPass()); |
134 | 147 |
135 // Run clean-up again. | 148 // Run clean-up again. |
136 pm.add(createScalarReplAggregatesPass()); | 149 addPass(pm, createScalarReplAggregatesPass()); |
137 pm.add(createInstructionCombiningPass()); | 150 addPass(pm, createInstructionCombiningPass()); |
138 | 151 |
139 #ifdef USE_METADATA | 152 #ifdef USE_METADATA |
140 if (!disableLangSpecificPasses && !disableGCToStack) | 153 if (!disableLangSpecificPasses && !disableGCToStack) |
141 pm.add(createGarbageCollect2Stack()); | 154 addPass(pm, createGarbageCollect2Stack()); |
142 #endif | 155 #endif |
143 } | 156 } |
144 } | 157 } |
145 | 158 |
146 if (optimizeLevel >= 2 && !disableLangSpecificPasses) { | 159 if (optimizeLevel >= 2 && !disableLangSpecificPasses) { |
147 if (!disableSimplifyRuntimeCalls) | 160 if (!disableSimplifyRuntimeCalls) |
148 pm.add(createSimplifyDRuntimeCalls()); | 161 addPass(pm, createSimplifyDRuntimeCalls()); |
149 | 162 |
150 #ifdef USE_METADATA | 163 #ifdef USE_METADATA |
151 if (!disableGCToStack) { | 164 if (!disableGCToStack) { |
152 // Run some clean-up after the last GC to stack promotion pass. | 165 // Run some clean-up after the last GC to stack promotion pass. |
153 pm.add(createScalarReplAggregatesPass()); | 166 addPass(pm, createScalarReplAggregatesPass()); |
154 pm.add(createInstructionCombiningPass()); | 167 addPass(pm, createInstructionCombiningPass()); |
155 pm.add(createCFGSimplificationPass()); | 168 addPass(pm, createCFGSimplificationPass()); |
156 } | 169 } |
157 #endif | 170 #endif |
158 } | 171 } |
159 | 172 |
160 // -O3 | 173 // -O3 |
161 if (optimizeLevel >= 3) | 174 if (optimizeLevel >= 3) |
162 { | 175 { |
163 pm.add(createArgumentPromotionPass()); | 176 addPass(pm, createArgumentPromotionPass()); |
164 pm.add(createTailDuplicationPass()); | 177 addPass(pm, createTailDuplicationPass()); |
165 pm.add(createInstructionCombiningPass()); | 178 addPass(pm, createInstructionCombiningPass()); |
166 pm.add(createCFGSimplificationPass()); | 179 addPass(pm, createCFGSimplificationPass()); |
167 pm.add(createScalarReplAggregatesPass()); | 180 addPass(pm, createScalarReplAggregatesPass()); |
168 pm.add(createInstructionCombiningPass()); | 181 addPass(pm, createInstructionCombiningPass()); |
169 pm.add(createCondPropagationPass()); | 182 addPass(pm, createCondPropagationPass()); |
170 | 183 |
171 pm.add(createTailCallEliminationPass()); | 184 addPass(pm, createTailCallEliminationPass()); |
172 pm.add(createCFGSimplificationPass()); | 185 addPass(pm, createCFGSimplificationPass()); |
173 pm.add(createReassociatePass()); | 186 addPass(pm, createReassociatePass()); |
174 pm.add(createLoopRotatePass()); | 187 addPass(pm, createLoopRotatePass()); |
175 pm.add(createLICMPass()); | 188 addPass(pm, createLICMPass()); |
176 pm.add(createLoopUnswitchPass()); | 189 addPass(pm, createLoopUnswitchPass()); |
177 pm.add(createInstructionCombiningPass()); | 190 addPass(pm, createInstructionCombiningPass()); |
178 pm.add(createIndVarSimplifyPass()); | 191 addPass(pm, createIndVarSimplifyPass()); |
179 pm.add(createLoopUnrollPass()); | 192 addPass(pm, createLoopUnrollPass()); |
180 pm.add(createInstructionCombiningPass()); | 193 addPass(pm, createInstructionCombiningPass()); |
181 pm.add(createGVNPass()); | 194 addPass(pm, createGVNPass()); |
182 pm.add(createSCCPPass()); | 195 addPass(pm, createSCCPPass()); |
183 | 196 |
184 pm.add(createInstructionCombiningPass()); | 197 addPass(pm, createInstructionCombiningPass()); |
185 pm.add(createCondPropagationPass()); | 198 addPass(pm, createCondPropagationPass()); |
186 | 199 |
187 pm.add(createDeadStoreEliminationPass()); | 200 addPass(pm, createDeadStoreEliminationPass()); |
188 pm.add(createAggressiveDCEPass()); | 201 addPass(pm, createAggressiveDCEPass()); |
189 pm.add(createCFGSimplificationPass()); | 202 addPass(pm, createCFGSimplificationPass()); |
190 pm.add(createSimplifyLibCallsPass()); | 203 addPass(pm, createSimplifyLibCallsPass()); |
191 pm.add(createDeadTypeEliminationPass()); | 204 addPass(pm, createDeadTypeEliminationPass()); |
192 pm.add(createConstantMergePass()); | 205 addPass(pm, createConstantMergePass()); |
193 } | 206 } |
194 | 207 |
195 // level -O4 and -O5 are linktime optimizations | 208 // level -O4 and -O5 are linktime optimizations |
196 } | 209 } |
197 | 210 |
212 #endif | 225 #endif |
213 return false; | 226 return false; |
214 } | 227 } |
215 | 228 |
216 PassManager pm; | 229 PassManager pm; |
217 pm.add(new TargetData(m)); | 230 |
231 if (verifyEach) pm.add(createVerifierPass()); | |
232 | |
233 addPass(pm, new TargetData(m)); | |
218 | 234 |
219 bool optimize = optimizeLevel != 0 || doInline(); | 235 bool optimize = optimizeLevel != 0 || doInline(); |
220 | 236 |
221 unsigned optPos = optimizeLevel != 0 | 237 unsigned optPos = optimizeLevel != 0 |
222 ? optimizeLevel.getPosition() | 238 ? optimizeLevel.getPosition() |
229 optimize = false; | 245 optimize = false; |
230 } | 246 } |
231 | 247 |
232 const PassInfo* pass = passList[i]; | 248 const PassInfo* pass = passList[i]; |
233 if (PassInfo::NormalCtor_t ctor = pass->getNormalCtor()) { | 249 if (PassInfo::NormalCtor_t ctor = pass->getNormalCtor()) { |
234 pm.add(ctor()); | 250 addPass(pm, ctor()); |
235 } else { | 251 } else { |
236 const char* arg = pass->getPassArgument(); // may return null | 252 const char* arg = pass->getPassArgument(); // may return null |
237 if (arg) | 253 if (arg) |
238 error("Can't create pass '-%s' (%s)", arg, pass->getPassName()); | 254 error("Can't create pass '-%s' (%s)", arg, pass->getPassName()); |
239 else | 255 else |
247 | 263 |
248 #ifdef USE_METADATA | 264 #ifdef USE_METADATA |
249 if (!disableStripMetaData) { | 265 if (!disableStripMetaData) { |
250 // This one is purposely not disabled by disableLangSpecificPasses | 266 // This one is purposely not disabled by disableLangSpecificPasses |
251 // because the code generator will assert if it's not used. | 267 // because the code generator will assert if it's not used. |
252 pm.add(createStripMetaData()); | 268 addPass(pm, createStripMetaData()); |
253 } | 269 } |
254 #endif | 270 #endif |
255 | 271 |
256 pm.run(*m); | 272 pm.run(*m); |
257 return true; | 273 return true; |