[SimplifyCFG] Fix bootstrap failure after r280220

We check that a sinking candidate is used by only one PHI node during our legality checks. However for instructions that are used by other sinking candidates our heuristic is less conservative. This can result in a candidate actually being illegal when we come to sink it because of how we sunk a predecessor. Do the used-by-only-one-PHI checks again during sinking to ensure we don't crash.

llvm-svn: 280228
This commit is contained in:
James Molloy 2016-08-31 12:33:48 +00:00
parent f2392f69d5
commit b7efa6c227
2 changed files with 44 additions and 5 deletions

View File

@ -1444,7 +1444,7 @@ static bool canSinkInstructions(
// Assuming canSinkLastInstruction(Blocks) has returned true, sink the last // Assuming canSinkLastInstruction(Blocks) has returned true, sink the last
// instruction of every block in Blocks to their common successor, commoning // instruction of every block in Blocks to their common successor, commoning
// into one instruction. // into one instruction.
static void sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) { static bool sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
auto *BBEnd = Blocks[0]->getTerminator()->getSuccessor(0); auto *BBEnd = Blocks[0]->getTerminator()->getSuccessor(0);
// canSinkLastInstruction returning true guarantees that every block has at // canSinkLastInstruction returning true guarantees that every block has at
@ -1453,9 +1453,22 @@ static void sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
for (auto *BB : Blocks) for (auto *BB : Blocks)
Insts.push_back(BB->getTerminator()->getPrevNode()); Insts.push_back(BB->getTerminator()->getPrevNode());
// We don't need to do any checking here; canSinkLastInstruction should have // The only checking we need to do now is that all users of all instructions
// done it all for us. // are the same PHI node. canSinkLastInstruction should have checked this but
// it is slightly over-aggressive - it gets confused by commutative instructions
// so double-check it here.
Instruction *I0 = Insts.front(); Instruction *I0 = Insts.front();
if (!isa<StoreInst>(I0)) {
auto *PNUse = dyn_cast<PHINode>(*I0->user_begin());
if (!all_of(Insts, [&PNUse](const Instruction *I) -> bool {
auto *U = cast<Instruction>(*I->user_begin());
return U == PNUse || U->getParent() == I->getParent();
}))
return false;
}
// We don't need to do any more checking here; canSinkLastInstruction should
// have done it all for us.
SmallVector<Value*, 4> NewOperands; SmallVector<Value*, 4> NewOperands;
for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) { for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) {
// This check is different to that in canSinkLastInstruction. There, we // This check is different to that in canSinkLastInstruction. There, we
@ -1507,6 +1520,8 @@ static void sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
for (auto *I : Insts) for (auto *I : Insts)
if (I != I0) if (I != I0)
I->eraseFromParent(); I->eraseFromParent();
return true;
} }
namespace { namespace {
@ -1639,7 +1654,7 @@ static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
LockstepReverseIterator LRI(UnconditionalPreds); LockstepReverseIterator LRI(UnconditionalPreds);
while (LRI.isValid() && while (LRI.isValid() &&
canSinkInstructions(*LRI, PHIOperands)) { canSinkInstructions(*LRI, PHIOperands)) {
DEBUG(dbgs() << "SINK: instruction can be sunk: " << (*LRI)[0] << "\n"); DEBUG(dbgs() << "SINK: instruction can be sunk: " << *(*LRI)[0] << "\n");
InstructionsToSink.insert((*LRI).begin(), (*LRI).end()); InstructionsToSink.insert((*LRI).begin(), (*LRI).end());
++ScanIdx; ++ScanIdx;
--LRI; --LRI;
@ -1698,7 +1713,8 @@ static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
break; break;
} }
sinkLastInstruction(UnconditionalPreds); if (!sinkLastInstruction(UnconditionalPreds))
return Changed;
NumSinkCommons++; NumSinkCommons++;
Changed = true; Changed = true;
} }

View File

@ -560,6 +560,29 @@ if.end:
; CHECK: store ; CHECK: store
; CHECK: store ; CHECK: store
define zeroext i1 @test19(i1 zeroext %flag, i32* %i4, i32* %m, i32* %n) {
entry:
br i1 %flag, label %if.then, label %if.else
if.then:
%tmp1 = load i32, i32* %i4
%tmp2 = add i32 %tmp1, -1
store i32 %tmp2, i32* %i4
br label %if.end
if.else:
%tmp3 = load i32, i32* %m
%tmp4 = load i32, i32* %n
%tmp5 = add i32 %tmp3, %tmp4
store i32 %tmp5, i32* %i4
br label %if.end
if.end:
ret i1 true
}
; No checks for test19 - just ensure it doesn't crash!
; CHECK: !0 = !{!1, !1, i64 0} ; CHECK: !0 = !{!1, !1, i64 0}
; CHECK: !1 = !{!"float", !2} ; CHECK: !1 = !{!"float", !2}
; CHECK: !2 = !{!"an example type tree"} ; CHECK: !2 = !{!"an example type tree"}