From 34093a634accd3c6a9594588b792b7dd24b50473 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 21 Apr 2008 00:23:14 +0000 Subject: simplify code, fit in 80 cols. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50015 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/LoopSimplify.cpp | 132 +++++++++++++++++----------------- 1 file changed, 67 insertions(+), 65 deletions(-) (limited to 'lib/Transforms/Utils/LoopSimplify.cpp') diff --git a/lib/Transforms/Utils/LoopSimplify.cpp b/lib/Transforms/Utils/LoopSimplify.cpp index e6f5e7c39c..f3e6417f44 100644 --- a/lib/Transforms/Utils/LoopSimplify.cpp +++ b/lib/Transforms/Utils/LoopSimplify.cpp @@ -263,14 +263,15 @@ ReprocessLoop: /// SplitBlockPredecessors - Split the specified block into two blocks. We want /// to move the predecessors specified in the Preds list to point to the new /// block, leaving the remaining predecessors pointing to BB. This method -/// updates the SSA PHINode's, but no other analyses. +/// updates the SSA PHINode's and AliasAnalysis, but no other analyses. /// BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB, const char *Suffix, const std::vector &Preds) { // Create new basic block, insert right before the original block... - BasicBlock *NewBB = BasicBlock::Create(BB->getName()+Suffix, BB->getParent(), BB); + BasicBlock *NewBB = + BasicBlock::Create(BB->getName()+Suffix, BB->getParent(), BB); // The preheader first gets an unconditional branch to the loop header... BranchInst *BI = BranchInst::Create(BB, NewBB); @@ -281,78 +282,79 @@ BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB, // into the PHI nodes for the new edge. If the loop is not dead, we move the // incoming edges in BB into new PHI nodes in NewBB. // - if (!Preds.empty()) { // Is the loop not obviously dead? - // Check to see if the values being merged into the new block need PHI - // nodes. If so, insert them. - for (BasicBlock::iterator I = BB->begin(); isa(I); ) { + if (Preds.empty()) { // Is the loop obviously dead? + for (BasicBlock::iterator I = BB->begin(); isa(I); ++I) { PHINode *PN = cast(I); - ++I; - - // Check to see if all of the values coming in are the same. If so, we - // don't need to create a new PHI node. - Value *InVal = PN->getIncomingValueForBlock(Preds[0]); - for (unsigned i = 1, e = Preds.size(); i != e; ++i) - if (InVal != PN->getIncomingValueForBlock(Preds[i])) { - InVal = 0; - break; - } - - // If the values coming into the block are not the same, we need a PHI. - if (InVal == 0) { - // Create the new PHI node, insert it into NewBB at the end of the block - PHINode *NewPHI = PHINode::Create(PN->getType(), PN->getName()+".ph", BI); - if (AA) AA->copyValue(PN, NewPHI); + // Insert dummy values as the incoming value... + PN->addIncoming(Constant::getNullValue(PN->getType()), NewBB); + } + return NewBB; + } + + // Check to see if the values being merged into the new block need PHI + // nodes. If so, insert them. + for (BasicBlock::iterator I = BB->begin(); isa(I); ) { + PHINode *PN = cast(I); + ++I; - // Move all of the edges from blocks outside the loop to the new PHI - for (unsigned i = 0, e = Preds.size(); i != e; ++i) { - Value *V = PN->removeIncomingValue(Preds[i], false); - NewPHI->addIncoming(V, Preds[i]); - } - InVal = NewPHI; - } else { - // Remove all of the edges coming into the PHI nodes from outside of the - // block. - for (unsigned i = 0, e = Preds.size(); i != e; ++i) - PN->removeIncomingValue(Preds[i], false); + // Check to see if all of the values coming in are the same. If so, we + // don't need to create a new PHI node. + Value *InVal = PN->getIncomingValueForBlock(Preds[0]); + for (unsigned i = 1, e = Preds.size(); i != e; ++i) + if (InVal != PN->getIncomingValueForBlock(Preds[i])) { + InVal = 0; + break; } - // Add an incoming value to the PHI node in the loop for the preheader - // edge. - PN->addIncoming(InVal, NewBB); - - // Can we eliminate this phi node now? - if (Value *V = PN->hasConstantValue(true)) { - Instruction *I = dyn_cast(V); - // If I is in NewBB, the Dominator call will fail, because NewBB isn't - // registered in DominatorTree yet. Handle this case explicitly. - if (!I || (I->getParent() != NewBB && - getAnalysis().dominates(I, PN))) { - PN->replaceAllUsesWith(V); - if (AA) AA->deleteValue(PN); - BB->getInstList().erase(PN); - } + // If the values coming into the block are not the same, we need a PHI. + if (InVal == 0) { + // Create the new PHI node, insert it into NewBB at the end of the block + PHINode *NewPHI = + PHINode::Create(PN->getType(), PN->getName()+".ph", BI); + if (AA) AA->copyValue(PN, NewPHI); + + // Move all of the edges from blocks outside the loop to the new PHI + for (unsigned i = 0, e = Preds.size(); i != e; ++i) { + Value *V = PN->removeIncomingValue(Preds[i], false); + NewPHI->addIncoming(V, Preds[i]); } + InVal = NewPHI; + } else { + // Remove all of the edges coming into the PHI nodes from outside of the + // block. + for (unsigned i = 0, e = Preds.size(); i != e; ++i) + PN->removeIncomingValue(Preds[i], false); } - // Now that the PHI nodes are updated, actually move the edges from - // Preds to point to NewBB instead of BB. - // - for (unsigned i = 0, e = Preds.size(); i != e; ++i) { - TerminatorInst *TI = Preds[i]->getTerminator(); - for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) - if (TI->getSuccessor(s) == BB) - TI->setSuccessor(s, NewBB); - - if (Preds[i]->getUnwindDest() == BB) - Preds[i]->setUnwindDest(NewBB); + // Add an incoming value to the PHI node in the loop for the preheader + // edge. + PN->addIncoming(InVal, NewBB); + + // Can we eliminate this phi node now? + if (Value *V = PN->hasConstantValue(true)) { + Instruction *I = dyn_cast(V); + // If I is in NewBB, the Dominator call will fail, because NewBB isn't + // registered in DominatorTree yet. Handle this case explicitly. + if (!I || (I->getParent() != NewBB && + getAnalysis().dominates(I, PN))) { + PN->replaceAllUsesWith(V); + if (AA) AA->deleteValue(PN); + BB->getInstList().erase(PN); + } } + } - } else { // Otherwise the loop is dead... - for (BasicBlock::iterator I = BB->begin(); isa(I); ++I) { - PHINode *PN = cast(I); - // Insert dummy values as the incoming value... - PN->addIncoming(Constant::getNullValue(PN->getType()), NewBB); - } + // Now that the PHI nodes are updated, actually move the edges from + // Preds to point to NewBB instead of BB. + // + for (unsigned i = 0, e = Preds.size(); i != e; ++i) { + TerminatorInst *TI = Preds[i]->getTerminator(); + for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) + if (TI->getSuccessor(s) == BB) + TI->setSuccessor(s, NewBB); + + if (Preds[i]->getUnwindDest() == BB) + Preds[i]->setUnwindDest(NewBB); } return NewBB; -- cgit v1.2.3