[llvm-commits] [llvm] r40692 - in /llvm/trunk: include/llvm/Analysis/MemoryDependenceAnalysis.h lib/Analysis/MemoryDependenceAnalysis.cpp lib/Transforms/Scalar/GVN.cpp

Owen Anderson resistor at mac.com
Wed Aug 1 17:01:54 CDT 2007


Author: resistor
Date: Wed Aug  1 17:01:54 2007
New Revision: 40692

URL: http://llvm.org/viewvc/llvm-project?rev=40692&view=rev
Log:
Make non-local memdep not be recursive, and fix a bug on 403.gcc that this exposed.

Modified:
    llvm/trunk/include/llvm/Analysis/MemoryDependenceAnalysis.h
    llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp
    llvm/trunk/lib/Transforms/Scalar/GVN.cpp

Modified: llvm/trunk/include/llvm/Analysis/MemoryDependenceAnalysis.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/MemoryDependenceAnalysis.h?rev=40692&r1=40691&r2=40692&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Analysis/MemoryDependenceAnalysis.h (original)
+++ llvm/trunk/include/llvm/Analysis/MemoryDependenceAnalysis.h Wed Aug  1 17:01:54 2007
@@ -43,9 +43,8 @@
   
     Instruction* getCallSiteDependency(CallSite C, Instruction* start,
                                        bool local = true);
-    bool nonLocalHelper(Instruction* query, BasicBlock* block,
-                        DenseMap<BasicBlock*, Value*>& resp,
-                        SmallPtrSet<BasicBlock*, 4>& visited);
+    void nonLocalHelper(Instruction* query, BasicBlock* block,
+                        DenseMap<BasicBlock*, Value*>& resp);
   public:
     
     static Instruction* NonLocal;
@@ -74,7 +73,7 @@
     Instruction* getDependency(Instruction* query, Instruction* start = 0,
                                BasicBlock* block = 0);
     
-    bool getNonLocalDependency(Instruction* query,
+    void getNonLocalDependency(Instruction* query,
                                DenseMap<BasicBlock*, Value*>& resp);
     
     /// removeInstruction - Remove an instruction from the dependence analysis,

Modified: llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp?rev=40692&r1=40691&r2=40692&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp Wed Aug  1 17:01:54 2007
@@ -101,63 +101,62 @@
   return NonLocal;
 }
 
-bool MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
+void MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
                                               BasicBlock* block,
-                                              DenseMap<BasicBlock*, Value*>& resp,
-                                              SmallPtrSet<BasicBlock*, 4>& visited) {
-  if (resp.count(block))
-    return resp[block] != None;
+                                              DenseMap<BasicBlock*, Value*>& resp) {
+  SmallPtrSet<BasicBlock*, 4> visited;
+  SmallVector<BasicBlock*, 4> stack;
+  stack.push_back(block);
   
-  Instruction* localDep = getDependency(query, 0, block);
-  if (localDep != NonLocal) {
-    resp.insert(std::make_pair(block, localDep));
-    return true;
+  while (!stack.empty()) {
+    BasicBlock* BB = stack.back();
+    
+    visited.insert(BB);
+    
+    if (resp.count(BB)) {
+      stack.pop_back();
+      continue;
+    }
+    
+    if (BB != block) {
+      Instruction* localDep = getDependency(query, 0, BB);
+      if (localDep != NonLocal) {
+        resp.insert(std::make_pair(BB, localDep));
+        continue;
+      }
+    }
+    
+    bool predOnStack = false;
+    bool inserted = false;
+    for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
+         PI != PE; ++PI)
+      if (!visited.count(*PI)) {
+        stack.push_back(*PI);
+        inserted = true;
+      } else
+        predOnStack = true;
+    
+    if (inserted)
+      continue;
+    else if (!inserted && !predOnStack) {
+      resp.insert(std::make_pair(BB, None));
+    } else if (!inserted && predOnStack){
+      resp.insert(std::make_pair(BB, NonLocal));
+    }
+    
+    stack.pop_back();
   }
-  
-  visited.insert(block);
-  
-  bool inserted = false;
-  bool predOnStack = false;
-  for (pred_iterator PI = pred_begin(block), PE = pred_end(block);
-       PI != PE; ++PI)
-    if (!visited.count(*PI))
-      inserted |= nonLocalHelper(query, *PI, resp, visited);
-    else
-      predOnStack = true;
-
-  visited.erase(block);
-
-  if (!inserted && !predOnStack)
-    resp.insert(std::make_pair(block, None));
-  else if (inserted && predOnStack)
-    resp.insert(std::make_pair(block, NonLocal));
-  
-  return inserted;
 }
 
-bool MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query,
+void MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query,
                                                      DenseMap<BasicBlock*, Value*>& resp) {
   Instruction* localDep = getDependency(query);
   if (localDep != NonLocal) {
     resp.insert(std::make_pair(query->getParent(), localDep));
-    return true;
+    return;
   }
   
-  bool inserted = false;
-  SmallPtrSet<BasicBlock*, 4> visited;
-  visited.insert(query->getParent());
-  
-  BasicBlock* parent = query->getParent();
-  for (pred_iterator PI = pred_begin(parent), PE = pred_end(parent);
-       PI != PE; ++PI) {
-    if (!visited.count(*PI))
-      inserted |= nonLocalHelper(query, *PI, resp, visited);
-  }
-  
-  if (!inserted)
-    resp.insert(std::make_pair(query->getParent(), None));
-  
-  return inserted;
+  nonLocalHelper(query, query->getParent(), resp);
 }
 
 /// getDependency - Return the instruction on which a memory operation

Modified: llvm/trunk/lib/Transforms/Scalar/GVN.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/GVN.cpp?rev=40692&r1=40691&r2=40692&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/GVN.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/GVN.cpp Wed Aug  1 17:01:54 2007
@@ -721,8 +721,9 @@
   Value *&V = Phis[BB];
   if (V) return V;
   
-  if (std::distance(pred_begin(BB), pred_end(BB)) == 1)
-    return V = GetValueForBlock(*pred_begin(BB), orig, Phis);
+  BasicBlock* singlePred = BB->getSinglePredecessor();
+  if (singlePred)
+    return V = GetValueForBlock(singlePred, orig, Phis);
   
   // Otherwise, the idom is the loop, so we need to insert a PHI node.  Do so
   // now, then get values to fill in the incoming values for the PHI.
@@ -750,6 +751,16 @@
     
     MD.removeInstruction(PN);
     PN->replaceAllUsesWith(first);
+    
+    SmallVector<BasicBlock*, 4> toRemove;
+    for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
+         E = Phis.end(); I != E; ++I)
+      if (I->second == PN)
+        toRemove.push_back(I->first);
+    for (SmallVector<BasicBlock*, 4>::iterator I = toRemove.begin(),
+         E= toRemove.end(); I != E; ++I)
+      Phis[*I] = first;
+    
     PN->eraseFromParent();
     
     Phis[BB] = first;
@@ -764,9 +775,7 @@
   MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
   
   DenseMap<BasicBlock*, Value*> deps;
-  bool ret = MD.getNonLocalDependency(L, deps);
-  if (!ret)
-    return false;
+  MD.getNonLocalDependency(L, deps);
   
   DenseMap<BasicBlock*, Value*> repl;
   for (DenseMap<BasicBlock*, Value*>::iterator I = deps.begin(), E = deps.end();





More information about the llvm-commits mailing list