diff --git a/include/llvm/Analysis/LazyValueInfo.h b/include/llvm/Analysis/LazyValueInfo.h index 566788daa61..f59b2e35322 100644 --- a/include/llvm/Analysis/LazyValueInfo.h +++ b/include/llvm/Analysis/LazyValueInfo.h @@ -57,6 +57,10 @@ public: /// 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. diff --git a/lib/Analysis/LazyValueInfo.cpp b/lib/Analysis/LazyValueInfo.cpp index 4a6885e0e46..a72faca6847 100644 --- a/lib/Analysis/LazyValueInfo.cpp +++ b/lib/Analysis/LazyValueInfo.cpp @@ -217,6 +217,11 @@ namespace { /// ValueCache - This is all of the cached information for all values, /// mapped from Value* to key information. DenseMap 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 > OverDefinedCache; public: /// getValueInBlock - This is the query interface to determine the lattice @@ -226,6 +231,11 @@ namespace { /// 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 @@ namespace { /// This is all of the cached information about this value. ValueCacheEntryTy &Cache; + /// This tracks, for each block, what values are overdefined. + DenseMap > &OverDefinedCache; + /// NewBlocks - This is a mapping of the new BasicBlocks which have been /// added to cache but that are not in sorted order. DenseMap NewBlockInfo; public: - LVIQuery(Value *V, ValueCacheEntryTy &VC) : Val(V), Cache(VC) { + LVIQuery(Value *V, ValueCacheEntryTy &VC, + DenseMap > &ODC) + : Val(V), Cache(VC), OverDefinedCache(ODC) { } ~LVIQuery() { @@ -306,6 +321,11 @@ namespace { array_pod_sort(Cache.begin(), Cache.end(), BlockCacheEntryComparator::Compare); + for (DenseMap::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 @@ LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) { 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 @@ getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) { 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 worklist; + worklist.push_back(OldSucc); + + std::set 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 &CurrentSet = OverDefinedCache[ToUpdate]; + for (std::set::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 @@ LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, return Unknown; } - +void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, + BasicBlock* NewSucc) { + getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc); +} diff --git a/lib/Transforms/Scalar/JumpThreading.cpp b/lib/Transforms/Scalar/JumpThreading.cpp index b07b5d90898..5dabcf0551f 100644 --- a/lib/Transforms/Scalar/JumpThreading.cpp +++ b/lib/Transforms/Scalar/JumpThreading.cpp @@ -1314,6 +1314,9 @@ bool JumpThreading::ThreadEdge(BasicBlock *BB, << ", 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.