[llvm-commits] [llvm] r50015 - /llvm/trunk/lib/Transforms/Utils/LoopSimplify.cpp

Chris Lattner sabre at nondot.org
Sun Apr 20 17:23:14 PDT 2008


Author: lattner
Date: Sun Apr 20 19:23:14 2008
New Revision: 50015

URL: http://llvm.org/viewvc/llvm-project?rev=50015&view=rev
Log:
simplify code, fit in 80 cols.

Modified:
    llvm/trunk/lib/Transforms/Utils/LoopSimplify.cpp

Modified: llvm/trunk/lib/Transforms/Utils/LoopSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/LoopSimplify.cpp?rev=50015&r1=50014&r2=50015&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/LoopSimplify.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/LoopSimplify.cpp Sun Apr 20 19:23:14 2008
@@ -263,14 +263,15 @@
 /// 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<BasicBlock*> &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 @@
   // 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<PHINode>(I); ) {
+  if (Preds.empty()) {  // Is the loop obviously dead?
+    for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) {
       PHINode *PN = cast<PHINode>(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;
-        }
+      // 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<PHINode>(I); ) {
+    PHINode *PN = cast<PHINode>(I);
+    ++I;
 
-      // 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);
+    // 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<Instruction>(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<DominatorTree>().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<Instruction>(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<DominatorTree>().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<PHINode>(I); ++I) {
-      PHINode *PN = cast<PHINode>(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;





More information about the llvm-commits mailing list