From cbfe5bbe88f5f2ee03a388585112f7609c8151ad Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sun, 22 Jun 2008 20:44:02 +0000 Subject: [PATCH] Improve LSR's dead-phi detection to handle use-def cycles with more than two nodes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@52617 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/LoopStrengthReduce.cpp | 37 +++++++++---------- .../Transforms/LoopStrengthReduce/dead-phi.ll | 21 +++++++++++ 2 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 test/Transforms/LoopStrengthReduce/dead-phi.ll diff --git a/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/lib/Transforms/Scalar/LoopStrengthReduce.cpp index dd459f56906..d825ea789b5 100644 --- a/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -1810,31 +1810,28 @@ bool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager &LPM) { DeleteTriviallyDeadInstructions(DeadInsts); BasicBlock::iterator I = L->getHeader()->begin(); - PHINode *PN; - while ((PN = dyn_cast(I))) { - ++I; // Preincrement iterator to avoid invalidating it when deleting PN. - - // At this point, we know that we have killed one or more GEP - // instructions. It is worth checking to see if the cann indvar is also - // dead, so that we can remove it as well. The requirements for the cann - // indvar to be considered dead are: - // 1. the cann indvar has one use - // 2. the use is an add instruction - // 3. the add has one use - // 4. the add is used by the cann indvar - // If all four cases above are true, then we can remove both the add and - // the cann indvar. + while (PHINode *PN = dyn_cast(I++)) { + // At this point, we know that we have killed one or more IV users. + // It is worth checking to see if the cann indvar is also + // dead, so that we can remove it as well. + // + // We can remove a PHI if it is on a cycle in the def-use graph + // where each node in the cycle has degree one, i.e. only one use, + // and is an instruction with no side effects. + // // FIXME: this needs to eliminate an induction variable even if it's being // compared against some value to decide loop termination. if (PN->hasOneUse()) { - Instruction *BO = dyn_cast(*PN->use_begin()); - if (BO && (isa(BO) || isa(BO))) { - if (BO->hasOneUse() && PN == *(BO->use_begin())) { - DeadInsts.insert(BO); - // Break the cycle, then delete the PHI. + for (Instruction *J = dyn_cast(*PN->use_begin()); + J && J->hasOneUse() && !J->mayWriteToMemory(); + J = dyn_cast(*J->use_begin())) { + // If we find the original PHI, we've discovered a cycle. + if (J == PN) { + // Break the cycle and mark the PHI for deletion. SE->deleteValueFromRecords(PN); PN->replaceAllUsesWith(UndefValue::get(PN->getType())); - PN->eraseFromParent(); + DeadInsts.insert(PN); + break; } } } diff --git a/test/Transforms/LoopStrengthReduce/dead-phi.ll b/test/Transforms/LoopStrengthReduce/dead-phi.ll new file mode 100644 index 00000000000..a6aafa911da --- /dev/null +++ b/test/Transforms/LoopStrengthReduce/dead-phi.ll @@ -0,0 +1,21 @@ +; RUN: llvm-as < %s | opt -loop-reduce | llvm-dis | grep phi | count 1 + +define void @foo(i32 %n) { +entry: + br label %loop + +loop: + %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] + + ; These three instructions form an isolated cycle and can be deleted. + %j = phi i32 [ 0, %entry ], [ %j.y, %loop ] + %j.x = add i32 %j, 1 + %j.y = mul i32 %j.x, 2 + + %i.next = add i32 %i, 1 + %c = icmp ne i32 %i.next, %n + br i1 %c, label %loop, label %exit + +exit: + ret void +} -- 2.34.1