[llvm-commits] [llvm] r109424 - in /llvm/trunk: include/llvm/Analysis/LazyValueInfo.h lib/Analysis/LazyValueInfo.cpp lib/Transforms/Scalar/JumpThreading.cpp

Owen Anderson resistor at mac.com
Mon Jul 26 11:48:03 PDT 2010


Author: resistor
Date: Mon Jul 26 13:48:03 2010
New Revision: 109424

URL: http://llvm.org/viewvc/llvm-project?rev=109424&view=rev
Log:
Add an initial implementation of LazyValueInfo updating for JumpThreading.  Disabled for now.

Modified:
    llvm/trunk/include/llvm/Analysis/LazyValueInfo.h
    llvm/trunk/lib/Analysis/LazyValueInfo.cpp
    llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp

Modified: llvm/trunk/include/llvm/Analysis/LazyValueInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/LazyValueInfo.h?rev=109424&r1=109423&r2=109424&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/LazyValueInfo.h (original)
+++ llvm/trunk/include/llvm/Analysis/LazyValueInfo.h Mon Jul 26 13:48:03 2010
@@ -57,6 +57,10 @@
   /// constant on the specified edge.  Return null if not.
   Constant *getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB);
   
+  /// threadEdge - Inform the analysis cache that we have threaded an edge from
+  /// PredBB to OldSucc to be from PredBB to NewSucc instead.
+  void threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, BasicBlock *NewSucc);
+  
   
   // Implementation boilerplate.
   

Modified: llvm/trunk/lib/Analysis/LazyValueInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/LazyValueInfo.cpp?rev=109424&r1=109423&r2=109424&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/LazyValueInfo.cpp (original)
+++ llvm/trunk/lib/Analysis/LazyValueInfo.cpp Mon Jul 26 13:48:03 2010
@@ -217,6 +217,11 @@
     /// ValueCache - This is all of the cached information for all values,
     /// mapped from Value* to key information.
     DenseMap<Value*, ValueCacheEntryTy> ValueCache;
+    
+    /// OverDefinedCache - This tracks, on a per-block basis, the set of 
+    /// values that are over-defined at the end of that block.  This is required
+    /// for cache updating.
+    DenseMap<BasicBlock*, std::set<Value*> > OverDefinedCache;
   public:
     
     /// getValueInBlock - This is the query interface to determine the lattice
@@ -226,6 +231,11 @@
     /// getValueOnEdge - This is the query interface to determine the lattice
     /// value for the specified Value* that is true on the specified edge.
     LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB);
+    
+    /// threadEdge - This is the update interface to inform the cache that an
+    /// edge from PredBB to OldSucc has been threaded to be from PredBB to
+    /// NewSucc.
+    void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
   };
 } // end anonymous namespace
 
@@ -270,12 +280,17 @@
     /// This is all of the cached information about this value.
     ValueCacheEntryTy &Cache;
     
+    /// This tracks, for each block, what values are overdefined.
+    DenseMap<BasicBlock*, std::set<Value*> > &OverDefinedCache;
+    
     ///  NewBlocks - This is a mapping of the new BasicBlocks which have been
     /// added to cache but that are not in sorted order.
     DenseMap<BasicBlock*, LVILatticeVal> NewBlockInfo;
   public:
     
-    LVIQuery(Value *V, ValueCacheEntryTy &VC) : Val(V), Cache(VC) {
+    LVIQuery(Value *V, ValueCacheEntryTy &VC,
+             DenseMap<BasicBlock*, std::set<Value*> > &ODC)
+      : Val(V), Cache(VC), OverDefinedCache(ODC) {
     }
 
     ~LVIQuery() {
@@ -306,6 +321,11 @@
       array_pod_sort(Cache.begin(), Cache.end(),
                      BlockCacheEntryComparator::Compare);
       
+      for (DenseMap<BasicBlock*, LVILatticeVal>::iterator
+           I = NewBlockInfo.begin(), E = NewBlockInfo.end(); I != E; ++I) {
+        if (I->second.isOverdefined())
+          OverDefinedCache[I->first].insert(Val);
+      }
     }
 
     LVILatticeVal getBlockValue(BasicBlock *BB);
@@ -474,7 +494,8 @@
   DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
         << BB->getName() << "'\n");
   
-  LVILatticeVal Result = LVIQuery(V, ValueCache[V]).getBlockValue(BB);
+  LVILatticeVal Result = LVIQuery(V, ValueCache[V], 
+                                  OverDefinedCache).getBlockValue(BB);
   
   DEBUG(dbgs() << "  Result = " << Result << "\n");
   return Result;
@@ -488,14 +509,75 @@
   
   DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
         << FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
+  
   LVILatticeVal Result =
-    LVIQuery(V, ValueCache[V]).getEdgeValue(FromBB, ToBB);
+    LVIQuery(V, ValueCache[V],
+             OverDefinedCache).getEdgeValue(FromBB, ToBB);
   
   DEBUG(dbgs() << "  Result = " << Result << "\n");
   
   return Result;
 }
 
+void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
+                                    BasicBlock *NewSucc) {
+  // When an edge in the graph has been threaded, values that we could not 
+  // determine a value for before (i.e. were marked overdefined) may be possible
+  // to solve now.  We do NOT try to proactively update these values.  Instead,
+  // we clear their entries from the cache, and allow lazy updating to recompute
+  // them when needed.
+  
+  // The updating process is fairly simple: we need to dropped cached info
+  // for all values that were marked overdefined in OldSucc, and for those same
+  // values in any successor of OldSucc (except NewSucc) in which they were
+  // also marked overdefined.
+  std::vector<BasicBlock*> worklist;
+  worklist.push_back(OldSucc);
+  
+  std::set<Value*> ClearSet = OverDefinedCache[OldSucc];
+  LVILatticeVal OverDef;
+  OverDef.markOverdefined();
+  
+  // Use a worklist to perform a depth-first search of OldSucc's successors.
+  // NOTE: We do not need a visited list since any blocks we have already
+  // visited will have had their overdefined markers cleared already, and we
+  // thus won't loop to their successors.
+  while (!worklist.empty()) {
+    BasicBlock *ToUpdate = worklist.back();
+    worklist.pop_back();
+    
+    // Skip blocks only accessible through NewSucc.
+    if (ToUpdate == NewSucc) continue;
+    
+    bool changed = false;
+    std::set<Value*> &CurrentSet = OverDefinedCache[ToUpdate];
+    for (std::set<Value*>::iterator I = ClearSet.begin(),E = ClearSet.end();
+         I != E; ++I) {
+      // If a value was marked overdefined in OldSucc, and is here too...
+      if (CurrentSet.count(*I)) {
+        // Remove it from the caches.
+        ValueCacheEntryTy &Entry = ValueCache[*I];
+        ValueCacheEntryTy::iterator CI =
+          std::lower_bound(Entry.begin(), Entry.end(),
+                           std::make_pair(ToUpdate, OverDef),
+                           BlockCacheEntryComparator());
+        assert(CI != Entry.end() && "Couldn't find entry to update?");
+        Entry.erase(CI);
+
+        CurrentSet.erase(*I);
+
+        // If we removed anything, then we potentially need to update 
+        // blocks successors too.
+        changed = true;
+      }
+    }
+        
+    if (!changed) continue;
+    
+    worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate));
+  }
+}
+
 //===----------------------------------------------------------------------===//
 //                            LazyValueInfo Impl
 //===----------------------------------------------------------------------===//
@@ -579,4 +661,7 @@
   return Unknown;
 }
 
-
+void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
+                               BasicBlock* NewSucc) {
+  getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc);
+}

Modified: llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp?rev=109424&r1=109423&r2=109424&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp Mon Jul 26 13:48:03 2010
@@ -1314,6 +1314,9 @@
         << ", across block:\n    "
         << *BB << "\n");
   
+  if (LVI)
+    LVI->threadEdge(PredBB, BB, SuccBB);
+  
   // We are going to have to map operands from the original BB block to the new
   // copy of the block 'NewBB'.  If there are PHI nodes in BB, evaluate them to
   // account for entry from PredBB.





More information about the llvm-commits mailing list