[llvm-commits] [llvm] r100147 - in /llvm/trunk: include/llvm/Transforms/Utils/SSAUpdater.h lib/Transforms/Utils/SSAUpdater.cpp

Bob Wilson bob.wilson at apple.com
Thu Apr 1 16:05:58 PDT 2010


Author: bwilson
Date: Thu Apr  1 18:05:58 2010
New Revision: 100147

URL: http://llvm.org/viewvc/llvm-project?rev=100147&view=rev
Log:
Rewrite another SSAUpdater function to avoid recursion.

Modified:
    llvm/trunk/include/llvm/Transforms/Utils/SSAUpdater.h
    llvm/trunk/lib/Transforms/Utils/SSAUpdater.cpp

Modified: llvm/trunk/include/llvm/Transforms/Utils/SSAUpdater.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/SSAUpdater.h?rev=100147&r1=100146&r2=100147&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/Utils/SSAUpdater.h (original)
+++ llvm/trunk/include/llvm/Transforms/Utils/SSAUpdater.h Thu Apr  1 18:05:58 2010
@@ -108,8 +108,8 @@
   void FindPHIPlacement(BasicBlock *BB, BBInfo *Info, bool &Changed,
                         unsigned Counter);
   void FindAvailableVal(BasicBlock *BB, BBInfo *Info, unsigned Counter);
-  void FindExistingPHI(BasicBlock *BB, BBInfo *Info);
-  bool CheckIfPHIMatches(BasicBlock *BB, BBInfo *Info, Value *Val);
+  void FindExistingPHI(BasicBlock *BB);
+  bool CheckIfPHIMatches(PHINode *PHI);
   void RecordMatchingPHI(PHINode *PHI);
   void ClearPHITags(PHINode *PHI);
 

Modified: llvm/trunk/lib/Transforms/Utils/SSAUpdater.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SSAUpdater.cpp?rev=100147&r1=100146&r2=100147&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SSAUpdater.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SSAUpdater.cpp Thu Apr  1 18:05:58 2010
@@ -366,7 +366,7 @@
   PHINode *NewPHI = 0;
   if (Info->DefBB == BB) {
     // Look for an existing PHI.
-    FindExistingPHI(BB, Info);
+    FindExistingPHI(BB);
     if (!Info->AvailableVal) {
       NewPHI = PHINode::Create(PrototypeValue->getType(),
                                PrototypeValue->getName(), &BB->front());
@@ -401,11 +401,11 @@
 
 /// FindExistingPHI - Look through the PHI nodes in a block to see if any of
 /// them match what is needed.
-void SSAUpdater::FindExistingPHI(BasicBlock *BB, BBInfo *Info) {
+void SSAUpdater::FindExistingPHI(BasicBlock *BB) {
   PHINode *SomePHI;
   for (BasicBlock::iterator It = BB->begin();
        (SomePHI = dyn_cast<PHINode>(It)); ++It) {
-    if (CheckIfPHIMatches(BB, Info, SomePHI)) {
+    if (CheckIfPHIMatches(SomePHI)) {
       RecordMatchingPHI(SomePHI);
       break;
     }
@@ -413,40 +413,54 @@
   }
 }
 
-/// CheckIfPHIMatches - Check if Val is a PHI node in block BB that matches
-/// the placement and values in the BBMap.
-bool SSAUpdater::CheckIfPHIMatches(BasicBlock *BB, BBInfo *Info, Value *Val) {
-  if (Info->AvailableVal)
-    return Val == Info->AvailableVal;
-
-  // Check if Val is a PHI in this block.
-  PHINode *PHI = dyn_cast<PHINode>(Val);
-  if (!PHI || PHI->getParent() != BB)
-    return false;
-
-  // If this block has already been visited, check if this PHI matches.
-  if (Info->PHITag)
-    return PHI == Info->PHITag;
-  Info->PHITag = PHI;
-  bool IsMatch = true;
-
-  // Iterate through the predecessors.
+/// CheckIfPHIMatches - Check if a PHI node matches the placement and values
+/// in the BBMap.
+bool SSAUpdater::CheckIfPHIMatches(PHINode *PHI) {
   BBMapTy *BBMap = getBBMap(BM);
-  for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) {
-    BasicBlock *Pred = PHI->getIncomingBlock(i);
-    Value *IncomingVal = PHI->getIncomingValue(i);
-    BBInfo *PredInfo = (*BBMap)[Pred];
-    // Skip to the nearest preceding definition.
-    if (PredInfo->DefBB != Pred) {
-      Pred = PredInfo->DefBB;
-      PredInfo = (*BBMap)[Pred];
-    }
-    if (!CheckIfPHIMatches(Pred, PredInfo, IncomingVal)) {
-      IsMatch = false;
-      break;
+  SmallVector<PHINode*, 20> WorkList;
+  WorkList.push_back(PHI);
+
+  // Mark that the block containing this PHI has been visited.
+  (*BBMap)[PHI->getParent()]->PHITag = PHI;
+
+  while (!WorkList.empty()) {
+    PHI = WorkList.pop_back_val();
+
+    // Iterate through the PHI's incoming values.
+    for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) {
+      Value *IncomingVal = PHI->getIncomingValue(i);
+      BasicBlock *Pred = PHI->getIncomingBlock(i);
+      BBInfo *PredInfo = (*BBMap)[Pred];
+      // Skip to the nearest preceding definition.
+      if (PredInfo->DefBB != Pred) {
+        Pred = PredInfo->DefBB;
+        PredInfo = (*BBMap)[Pred];
+      }
+
+      // Check if it matches the expected value.
+      if (PredInfo->AvailableVal) {
+        if (IncomingVal == PredInfo->AvailableVal)
+          continue;
+        return false;
+      }
+
+      // Check if the value is a PHI in the correct block.
+      PHINode *IncomingPHIVal = dyn_cast<PHINode>(IncomingVal);
+      if (!IncomingPHIVal || IncomingPHIVal->getParent() != Pred)
+        return false;
+
+      // If this block has already been visited, check if this PHI matches.
+      if (PredInfo->PHITag) {
+        if (IncomingPHIVal == PredInfo->PHITag)
+          continue;
+        return false;
+      }
+      PredInfo->PHITag = IncomingPHIVal;
+
+      WorkList.push_back(IncomingPHIVal);
     }
   }
-  return IsMatch;
+  return true;
 }
 
 /// RecordMatchingPHI - For a PHI node that matches, record it and its input





More information about the llvm-commits mailing list