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
This commit is contained in:
Dan Gohman 2008-06-22 20:44:02 +00:00
parent 9b78763fce
commit cbfe5bbe88
2 changed files with 38 additions and 20 deletions

View File

@ -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<PHINode>(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<PHINode>(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<Instruction>(*PN->use_begin());
if (BO && (isa<BinaryOperator>(BO) || isa<CmpInst>(BO))) {
if (BO->hasOneUse() && PN == *(BO->use_begin())) {
DeadInsts.insert(BO);
// Break the cycle, then delete the PHI.
for (Instruction *J = dyn_cast<Instruction>(*PN->use_begin());
J && J->hasOneUse() && !J->mayWriteToMemory();
J = dyn_cast<Instruction>(*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;
}
}
}

View File

@ -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
}