llvm.org GIT mirror llvm / 254e234
[InstSimplify] remove quadratic time looping (PR42771) The test case from: https://bugs.llvm.org/show_bug.cgi?id=42771 ...shows a ~30x slowdown caused by the awkward loop iteration (rL207302) that is seemingly done just to avoid invalidating the instruction iterator. We can instead delay instruction deletion until we reach the end of the block (or we could delay until we reach the end of all blocks). There's a test diff here for a degenerate case with llvm.assume that is not meaningful in itself, but serves to verify this change in logic. This change probably doesn't result in much overall compile-time improvement because we call '-instsimplify' as a standalone pass only once in the standard -O2 opt pipeline currently. Differential Revision: https://reviews.llvm.org/D65336 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@367173 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjay Patel a month ago
2 changed file(s) with 21 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
3232 bool Changed = false;
3333
3434 do {
35 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
36 // Here be subtlety: the iterator must be incremented before the loop
37 // body (not sure why), so a range-for loop won't work here.
38 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
39 Instruction *I = &*BI++;
40 // The first time through the loop ToSimplify is empty and we try to
41 // simplify all instructions. On later iterations ToSimplify is not
35 for (BasicBlock &BB : F) {
36 SmallVector DeadInstsInBB;
37 for (Instruction &I : BB) {
38 // The first time through the loop, ToSimplify is empty and we try to
39 // simplify all instructions. On later iterations, ToSimplify is not
4240 // empty and we only bother simplifying instructions that are in it.
43 if (!ToSimplify->empty() && !ToSimplify->count(I))
41 if (!ToSimplify->empty() && !ToSimplify->count(&I))
4442 continue;
4543
46 // Don't waste time simplifying unused instructions.
47 if (!I->use_empty()) {
48 if (Value *V = SimplifyInstruction(I, SQ, ORE)) {
44 // Don't waste time simplifying dead/unused instructions.
45 if (isInstructionTriviallyDead(&I)) {
46 DeadInstsInBB.push_back(&I);
47 } else if (!I.use_empty()) {
48 if (Value *V = SimplifyInstruction(&I, SQ, ORE)) {
4949 // Mark all uses for resimplification next time round the loop.
50 for (User *U : I->users())
50 for (User *U : I.users())
5151 Next->insert(cast(U));
52 I->replaceAllUsesWith(V);
52 I.replaceAllUsesWith(V);
5353 ++NumSimplified;
5454 Changed = true;
55 // A call can get simplified, but it may not be trivially dead.
56 if (isInstructionTriviallyDead(&I))
57 DeadInstsInBB.push_back(&I);
5558 }
5659 }
57 if (RecursivelyDeleteTriviallyDeadInstructions(I, SQ.TLI)) {
58 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
59 // instruction, so simply incrementing the iterator does not work.
60 // When instructions get deleted re-iterate instead.
61 BI = BB->begin();
62 BE = BB->end();
63 Changed = true;
64 }
6560 }
61 RecursivelyDeleteTriviallyDeadInstructions(DeadInstsInBB, SQ.TLI);
6662 }
6763
6864 // Place the list of instructions to simplify on the next loop iteration
3232 }
3333
3434 ; Similar to above: there's no way to know which assumption is truthful,
35 ; so just don't crash. The second icmp+assume gets processed later, so that
36 ; determines the return value.
35 ; so just don't crash.
3736
3837 define i8 @conflicting_assumptions(i8 %x) !dbg !10 {
3938 ; CHECK-LABEL: @conflicting_assumptions(
39 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X:%.*]], 1, !dbg !10
4040 ; CHECK-NEXT: call void @llvm.assume(i1 false)
41 ; CHECK-NEXT: [[COND2:%.*]] = icmp eq i8 %x, 4
41 ; CHECK-NEXT: [[COND2:%.*]] = icmp eq i8 [[X]], 4
4242 ; CHECK-NEXT: call void @llvm.assume(i1 [[COND2]])
43 ; CHECK-NEXT: ret i8 5
43 ; CHECK-NEXT: ret i8 [[ADD]]
4444 ;
4545 %add = add i8 %x, 1, !dbg !11
4646 %cond1 = icmp eq i8 %x, 3