diff options
| author | Sanjay Patel <spatel@rotateright.com> | 2019-07-27 14:05:51 +0000 | 
|---|---|---|
| committer | Sanjay Patel <spatel@rotateright.com> | 2019-07-27 14:05:51 +0000 | 
| commit | 02b9e45a7e4b815ca23608adad957ed1c7f8d03b (patch) | |
| tree | eb3224c0a7fec269d638cab7e11490e0ad4b29ce /llvm/lib/Transforms | |
| parent | 353a8484731b7b9ed79ae9d511659960976054f5 (diff) | |
| download | bcm5719-llvm-02b9e45a7e4b815ca23608adad957ed1c7f8d03b.tar.gz bcm5719-llvm-02b9e45a7e4b815ca23608adad957ed1c7f8d03b.zip | |
[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
llvm-svn: 367173
Diffstat (limited to 'llvm/lib/Transforms')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/InstSimplifyPass.cpp | 38 | 
1 files changed, 17 insertions, 21 deletions
| diff --git a/llvm/lib/Transforms/Scalar/InstSimplifyPass.cpp b/llvm/lib/Transforms/Scalar/InstSimplifyPass.cpp index 6616364ab20..c31d0e74641 100644 --- a/llvm/lib/Transforms/Scalar/InstSimplifyPass.cpp +++ b/llvm/lib/Transforms/Scalar/InstSimplifyPass.cpp @@ -33,37 +33,33 @@ static bool runImpl(Function &F, const SimplifyQuery &SQ,    bool Changed = false;    do { -    for (BasicBlock *BB : depth_first(&F.getEntryBlock())) { -      // Here be subtlety: the iterator must be incremented before the loop -      // body (not sure why), so a range-for loop won't work here. -      for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { -        Instruction *I = &*BI++; -        // The first time through the loop ToSimplify is empty and we try to -        // simplify all instructions.  On later iterations ToSimplify is not +    for (BasicBlock &BB : F) { +      SmallVector<Instruction *, 8> DeadInstsInBB; +      for (Instruction &I : BB) { +        // The first time through the loop, ToSimplify is empty and we try to +        // simplify all instructions. On later iterations, ToSimplify is not          // empty and we only bother simplifying instructions that are in it. -        if (!ToSimplify->empty() && !ToSimplify->count(I)) +        if (!ToSimplify->empty() && !ToSimplify->count(&I))            continue; -        // Don't waste time simplifying unused instructions. -        if (!I->use_empty()) { -          if (Value *V = SimplifyInstruction(I, SQ, ORE)) { +        // Don't waste time simplifying dead/unused instructions. +        if (isInstructionTriviallyDead(&I)) { +          DeadInstsInBB.push_back(&I); +        } else if (!I.use_empty()) { +          if (Value *V = SimplifyInstruction(&I, SQ, ORE)) {              // Mark all uses for resimplification next time round the loop. -            for (User *U : I->users()) +            for (User *U : I.users())                Next->insert(cast<Instruction>(U)); -            I->replaceAllUsesWith(V); +            I.replaceAllUsesWith(V);              ++NumSimplified;              Changed = true; +            // A call can get simplified, but it may not be trivially dead. +            if (isInstructionTriviallyDead(&I)) +              DeadInstsInBB.push_back(&I);            }          } -        if (RecursivelyDeleteTriviallyDeadInstructions(I, SQ.TLI)) { -          // RecursivelyDeleteTriviallyDeadInstruction can remove more than one -          // instruction, so simply incrementing the iterator does not work. -          // When instructions get deleted re-iterate instead. -          BI = BB->begin(); -          BE = BB->end(); -          Changed = true; -        }        } +      RecursivelyDeleteTriviallyDeadInstructions(DeadInstsInBB, SQ.TLI);      }      // Place the list of instructions to simplify on the next loop iteration | 

