clean up logic, convert std::set to SmallPtrSet, handle the case

when all 2-entry phis are simplified away.

llvm-svn: 121760
This commit is contained in:
Chris Lattner 2010-12-14 07:41:39 +00:00
parent 5546da68bb
commit 9ac168d0ab
1 changed files with 61 additions and 56 deletions

View File

@ -199,7 +199,7 @@ static Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
/// non-trapping. If both are true, the instruction is inserted into the set /// non-trapping. If both are true, the instruction is inserted into the set
/// and true is returned. /// and true is returned.
static bool DominatesMergePoint(Value *V, BasicBlock *BB, static bool DominatesMergePoint(Value *V, BasicBlock *BB,
std::set<Instruction*> *AggressiveInsts) { SmallPtrSet<Instruction*, 4> *AggressiveInsts) {
Instruction *I = dyn_cast<Instruction>(V); Instruction *I = dyn_cast<Instruction>(V);
if (!I) { if (!I) {
// Non-instructions all dominate instructions, but not all constantexprs // Non-instructions all dominate instructions, but not all constantexprs
@ -217,10 +217,15 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
// If this instruction is defined in a block that contains an unconditional // If this instruction is defined in a block that contains an unconditional
// branch to BB, then it must be in the 'conditional' part of the "if // branch to BB, then it must be in the 'conditional' part of the "if
// statement". // statement". If not, it definitely dominates the region.
if (BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator())) BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator());
if (BI->isUnconditional() && BI->getSuccessor(0) == BB) { if (BI == 0 || BI->isConditional() || BI->getSuccessor(0) != BB)
if (!AggressiveInsts) return false; return true;
// If we aren't allowing aggressive promotion anymore, then don't consider
// instructions in the 'if region'.
if (AggressiveInsts == 0) return false;
// Okay, it looks like the instruction IS in the "condition". Check to // Okay, it looks like the instruction IS in the "condition". Check to
// see if it's a cheap instruction to unconditionally compute, and if it // see if it's a cheap instruction to unconditionally compute, and if it
// only uses stuff defined outside of the condition. If so, hoist it out. // only uses stuff defined outside of the condition. If so, hoist it out.
@ -229,17 +234,13 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
switch (I->getOpcode()) { switch (I->getOpcode()) {
default: return false; // Cannot hoist this out safely. default: return false; // Cannot hoist this out safely.
case Instruction::Load: { case Instruction::Load:
// We have to check to make sure there are no instructions before the // We have to check to make sure there are no instructions before the
// load in its basic block, as we are going to hoist the loop out to // load in its basic block, as we are going to hoist the load out to its
// its predecessor. // predecessor.
BasicBlock::iterator IP = PBB->begin(); if (PBB->getFirstNonPHIOrDbg() != I)
while (isa<DbgInfoIntrinsic>(IP))
IP++;
if (IP != BasicBlock::iterator(I))
return false; return false;
break; break;
}
case Instruction::Add: case Instruction::Add:
case Instruction::Sub: case Instruction::Sub:
case Instruction::And: case Instruction::And:
@ -259,8 +260,6 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
return false; return false;
// Okay, it's safe to do this! Remember this instruction. // Okay, it's safe to do this! Remember this instruction.
AggressiveInsts->insert(I); AggressiveInsts->insert(I);
}
return true; return true;
} }
@ -1164,7 +1163,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD) {
// Loop over the PHI's seeing if we can promote them all to select // Loop over the PHI's seeing if we can promote them all to select
// instructions. While we are at it, keep track of the instructions // instructions. While we are at it, keep track of the instructions
// that need to be moved to the dominating block. // that need to be moved to the dominating block.
std::set<Instruction*> AggressiveInsts; SmallPtrSet<Instruction*, 4> AggressiveInsts;
BasicBlock::iterator AfterPHIIt = BB->begin(); BasicBlock::iterator AfterPHIIt = BB->begin();
while (isa<PHINode>(AfterPHIIt)) { while (isa<PHINode>(AfterPHIIt)) {
@ -1179,17 +1178,23 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD) {
return false; return false;
} }
// If we folded the the first phi, PN dangles at this point. Refresh it. If
// we ran out of PHIs then we simplified them all.
PN = dyn_cast<PHINode>(BB->begin());
if (PN == 0) return true;
// If we all PHI nodes are promotable, check to make sure that all // If we all PHI nodes are promotable, check to make sure that all
// instructions in the predecessor blocks can be promoted as well. If // instructions in the predecessor blocks can be promoted as well. If
// not, we won't be able to get rid of the control flow, so it's not // not, we won't be able to get rid of the control flow, so it's not
// worth promoting to select instructions. // worth promoting to select instructions.
BasicBlock *DomBlock = 0, *IfBlock1 = 0, *IfBlock2 = 0; BasicBlock *DomBlock = 0;
PN = cast<PHINode>(BB->begin()); BasicBlock *IfBlock1 = PN->getIncomingBlock(0);
BasicBlock *Pred = PN->getIncomingBlock(0); BasicBlock *IfBlock2 = PN->getIncomingBlock(1);
if (cast<BranchInst>(Pred->getTerminator())->isUnconditional()) { if (cast<BranchInst>(IfBlock1->getTerminator())->isConditional()) {
IfBlock1 = Pred; IfBlock1 = 0;
DomBlock = *pred_begin(Pred); } else {
for (BasicBlock::iterator I = Pred->begin(); !isa<TerminatorInst>(I); ++I) DomBlock = *pred_begin(IfBlock1);
for (BasicBlock::iterator I = IfBlock1->begin();!isa<TerminatorInst>(I);++I)
if (!AggressiveInsts.count(I) && !isa<DbgInfoIntrinsic>(I)) { if (!AggressiveInsts.count(I) && !isa<DbgInfoIntrinsic>(I)) {
// This is not an aggressive instruction that we can promote. // This is not an aggressive instruction that we can promote.
// Because of this, we won't be able to get rid of the control // Because of this, we won't be able to get rid of the control
@ -1198,11 +1203,11 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD) {
} }
} }
Pred = PN->getIncomingBlock(1); if (cast<BranchInst>(IfBlock2->getTerminator())->isConditional()) {
if (cast<BranchInst>(Pred->getTerminator())->isUnconditional()) { IfBlock2 = 0;
IfBlock2 = Pred; } else {
DomBlock = *pred_begin(Pred); DomBlock = *pred_begin(IfBlock2);
for (BasicBlock::iterator I = Pred->begin(); !isa<TerminatorInst>(I); ++I) for (BasicBlock::iterator I = IfBlock2->begin();!isa<TerminatorInst>(I);++I)
if (!AggressiveInsts.count(I) && !isa<DbgInfoIntrinsic>(I)) { if (!AggressiveInsts.count(I) && !isa<DbgInfoIntrinsic>(I)) {
// This is not an aggressive instruction that we can promote. // This is not an aggressive instruction that we can promote.
// Because of this, we won't be able to get rid of the control // Because of this, we won't be able to get rid of the control