Fix an infinite loop on 300.twolf.
authorOwen Anderson <resistor@mac.com>
Wed, 25 Jul 2007 22:03:06 +0000 (22:03 +0000)
committerOwen Anderson <resistor@mac.com>
Wed, 25 Jul 2007 22:03:06 +0000 (22:03 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40497 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/GVN.cpp
test/Transforms/GVN/2007-07-25-InfiniteLoop.ll [new file with mode: 0644]

index a08ee68ca62ff6c993897bb7999426925067ef49..eb12abe243c906ecf8ab20541aa411aa1ad8d603 100644 (file)
@@ -651,7 +651,8 @@ namespace {
                             SmallVector<Instruction*, 4>& toErase);
     bool processNonLocalLoad(LoadInst* L, SmallVector<Instruction*, 4>& toErase);
     Value *performPHIConstruction(BasicBlock *BB, LoadInst* orig,
-                                  DenseMap<BasicBlock*, Value*> &Phis);
+                                  DenseMap<BasicBlock*, Value*> &Phis,
+                                  SmallPtrSet<BasicBlock*, 4>& visited);
     void dump(DenseMap<BasicBlock*, Value*>& d);
   };
   
@@ -706,7 +707,8 @@ void GVN::dump(DenseMap<BasicBlock*, Value*>& d) {
 
 
 Value *GVN::performPHIConstruction(BasicBlock *BB, LoadInst* orig,
-                                   DenseMap<BasicBlock*, Value*> &Phis) {
+                                   DenseMap<BasicBlock*, Value*> &Phis,
+                                   SmallPtrSet<BasicBlock*, 4>& visited) {
   DenseMap<BasicBlock*, Value*>::iterator DI = Phis.find(BB);
   if (DI != Phis.end())
     return DI->second;
@@ -719,17 +721,25 @@ Value *GVN::performPHIConstruction(BasicBlock *BB, LoadInst* orig,
       Phis.insert(std::make_pair(BB, DI->second));
       return DI->second;
     } else {
-      Value* domV = performPHIConstruction(*pred_begin(BB), orig, Phis);
+      visited.insert(BB);
+      Value* domV = performPHIConstruction(*pred_begin(BB), orig, Phis, visited);
+      visited.erase(BB);
+      
       Phis.insert(std::make_pair(BB, domV));
       return domV;
     }
   } else {
     PHINode *PN = new PHINode(orig->getType(), orig->getName()+".rle", BB->begin());
     PN->reserveOperandSpace(numPreds);
-                                 
+    
+    visited.insert(BB);
     // Fill in the incoming values for the block.
     for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
-      PN->addIncoming(performPHIConstruction(*PI, orig, Phis), *PI);
+      if (!visited.count(*PI))
+        PN->addIncoming(performPHIConstruction(*PI, orig, Phis, visited), *PI);
+      else
+        PN->addIncoming(PN, *PI);
+    visited.erase(BB);
     
     bool all_same = PN->getNumIncomingValues() != 1;
     Value* first = PN->getIncomingValue(0);
@@ -772,7 +782,8 @@ bool GVN::processNonLocalLoad(LoadInst* L, SmallVector<Instruction*, 4>& toErase
       return false;
     }
   
-  Value* v = performPHIConstruction(L->getParent(), L, repl);
+  SmallPtrSet<BasicBlock*, 4> visited;
+  Value* v = performPHIConstruction(L->getParent(), L, repl, visited);
   
   MD.removeInstruction(L);
   L->replaceAllUsesWith(v);
diff --git a/test/Transforms/GVN/2007-07-25-InfiniteLoop.ll b/test/Transforms/GVN/2007-07-25-InfiniteLoop.ll
new file mode 100644 (file)
index 0000000..50eaf2f
--- /dev/null
@@ -0,0 +1,14 @@
+; RUN: llvm-as < %s | opt -gvn | llvm-dis
+
+       %struct.INT2 = type { i32, i32 }
+@blkshifts = external global %struct.INT2*             ; <%struct.INT2**> [#uses=2]
+
+define i32 @xcompact() {
+entry:
+       store %struct.INT2* null, %struct.INT2** @blkshifts, align 4
+       br label %bb
+
+bb:            ; preds = %bb, %entry
+       %tmp10 = load %struct.INT2** @blkshifts, align 4                ; <%struct.INT2*> [#uses=0]
+       br label %bb
+}