Skip to content

Commit 04a1a72

Browse files
committed
[mandatory-combine] Eliminate trivially dead instructions aggressively when compiling with opts.
When originally made, the thought behind mandatory combine was that at -Onone, we are at the end of the pipeline so we are going to be destroying our module anyways, so there is no reason to delete dead instructions. We are also running it at -O, -Osize, so why not eliminate these instructions when we prepare for sil optimizations! So I put in some conditional code that causes us to delete trivial instructions as we set up our worklist and if one pops off the worklist during traversal. The reason why I am doing this is that I realized that I do not want to use SILGenCleanup to test InstSimplify code. In fact, I do not want any of my RAUW code to be emitted by SILGenCleanup since I don't want to disturb the codegen of the early passes so I don't have to update a bunch of tests = (. So instead, I am going to use mandatory combine for this purpose and doing this cleans up the tests a little bit. It also will just eliminate a bunch of early unnecessary code as well.
1 parent 8f1b353 commit 04a1a72

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/SILOptimizer/Mandatory/MandatoryCombine.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ void MandatoryCombiner::addReachableCodeToWorklist(SILFunction &function) {
197197
blockAlreadyAddedToWorklist.insert(firstBlock);
198198
}
199199

200+
bool compilingWithOptimization = function.getEffectiveOptimizationMode() !=
201+
OptimizationMode::NoOptimization;
200202
while (!blockWorklist.empty()) {
201203
auto *block = blockWorklist.pop_back_val();
202204

@@ -205,6 +207,10 @@ void MandatoryCombiner::addReachableCodeToWorklist(SILFunction &function) {
205207
++iterator;
206208

207209
if (isInstructionTriviallyDead(instruction)) {
210+
if (compilingWithOptimization) {
211+
instruction->replaceAllUsesOfAllResultsWithUndef();
212+
instruction->eraseFromParent();
213+
}
208214
continue;
209215
}
210216

@@ -228,12 +234,23 @@ bool MandatoryCombiner::doOneIteration(SILFunction &function,
228234
addReachableCodeToWorklist(function);
229235
MandatoryCombineCanonicalize mcCanonicialize(worklist);
230236

237+
bool compilingWithOptimization = function.getEffectiveOptimizationMode() !=
238+
OptimizationMode::NoOptimization;
239+
231240
while (!worklist.isEmpty()) {
232241
auto *instruction = worklist.pop_back_val();
233242
if (instruction == nullptr) {
234243
continue;
235244
}
236245

246+
if (compilingWithOptimization) {
247+
if (isInstructionTriviallyDead(instruction)) {
248+
worklist.eraseInstFromFunction(*instruction);
249+
madeChange = true;
250+
continue;
251+
}
252+
}
253+
237254
if (mcCanonicialize.tryCanonicalize(instruction)) {
238255
madeChange = true;
239256
continue;

0 commit comments

Comments
 (0)