diff --git a/include/swift/SILOptimizer/Analysis/AliasAnalysis.h b/include/swift/SILOptimizer/Analysis/AliasAnalysis.h index 191aad627f957..efc4c8eef1110 100644 --- a/include/swift/SILOptimizer/Analysis/AliasAnalysis.h +++ b/include/swift/SILOptimizer/Analysis/AliasAnalysis.h @@ -112,12 +112,19 @@ class AliasAnalysis : public SILAnalysis { /// The computeMemoryBehavior() method uses this map to cache queries. llvm::DenseMap MemoryBehaviorCache; - /// The AliasAnalysis/MemoryBehavior cache can't directly map a pair of - /// ValueBase pointers to alias/memorybehavior results because we'd like to - /// be able to remove deleted pointers without having to scan the whole map. - /// So, instead of storing pointers we map pointers to indices and store the - /// indices. - ValueEnumerator ValueBaseToIndex; + /// The AliasAnalysis cache can't directly map a pair of ValueBase pointers + /// to alias results because we'd like to be able to remove deleted pointers + /// without having to scan the whole map. So, instead of storing pointers we + /// map pointers to indices and store the indices. + ValueEnumerator AliasValueBaseToIndex; + + /// Same as AliasValueBaseToIndex, map a pointer to the indices for + /// MemoryBehaviorCache. + /// + /// NOTE: we do not use the same ValueEnumerator for the alias cache, + /// as when either cache is cleared, we can not clear the ValueEnumerator + /// because doing so could give rise to collisions in the other cache. + ValueEnumerator MemoryBehaviorValueBaseToIndex; AliasResult aliasAddressProjection(SILValue V1, SILValue V2, SILValue O1, SILValue O2); @@ -134,7 +141,8 @@ class AliasAnalysis : public SILAnalysis { // The pointer I is going away. We can't scan the whole cache and remove // all of the occurrences of the pointer. Instead we remove the pointer // from the cache the translates pointers to indices. - ValueBaseToIndex.invalidateValue(I); + AliasValueBaseToIndex.invalidateValue(I); + MemoryBehaviorValueBaseToIndex.invalidateValue(I); } virtual bool needsNotifications() override { return true; } diff --git a/lib/SILOptimizer/Analysis/AliasAnalysis.cpp b/lib/SILOptimizer/Analysis/AliasAnalysis.cpp index 19b0829312c92..39f580fe63366 100644 --- a/lib/SILOptimizer/Analysis/AliasAnalysis.cpp +++ b/lib/SILOptimizer/Analysis/AliasAnalysis.cpp @@ -539,7 +539,7 @@ AliasResult AliasAnalysis::alias(SILValue V1, SILValue V2, // Flush the cache if the size of the cache is too large. if (AliasCache.size() > AliasAnalysisMaxCacheSize) { AliasCache.clear(); - ValueBaseToIndex.clear(); + AliasValueBaseToIndex.clear(); } // Calculate the aliasing result and store it in the cache. @@ -710,8 +710,8 @@ SILAnalysis *swift::createAliasAnalysis(SILModule *M) { AliasKeyTy AliasAnalysis::toAliasKey(SILValue V1, SILValue V2, SILType Type1, SILType Type2) { - size_t idx1 = ValueBaseToIndex.getIndex(V1.getDef()); - size_t idx2 = ValueBaseToIndex.getIndex(V2.getDef()); + size_t idx1 = AliasValueBaseToIndex.getIndex(V1.getDef()); + size_t idx2 = AliasValueBaseToIndex.getIndex(V2.getDef()); unsigned R1 = V1.getResultNumber(); unsigned R2 = V2.getResultNumber(); void *t1 = Type1.getOpaqueValue(); diff --git a/lib/SILOptimizer/Analysis/MemoryBehavior.cpp b/lib/SILOptimizer/Analysis/MemoryBehavior.cpp index 6534850165f7a..ba98e6357ffaa 100644 --- a/lib/SILOptimizer/Analysis/MemoryBehavior.cpp +++ b/lib/SILOptimizer/Analysis/MemoryBehavior.cpp @@ -313,7 +313,7 @@ AliasAnalysis::computeMemoryBehavior(SILInstruction *Inst, SILValue V, // Flush the cache if the size of the cache is too large. if (MemoryBehaviorCache.size() > MemoryBehaviorAnalysisMaxCacheSize) { MemoryBehaviorCache.clear(); - ValueBaseToIndex.clear(); + MemoryBehaviorValueBaseToIndex.clear(); } // Calculate the aliasing result and store it in the cache. @@ -333,8 +333,8 @@ AliasAnalysis::computeMemoryBehaviorInner(SILInstruction *Inst, SILValue V, MemBehaviorKeyTy AliasAnalysis::toMemoryBehaviorKey(SILValue V1, SILValue V2, RetainObserveKind M) { - size_t idx1 = ValueBaseToIndex.getIndex(V1.getDef()); - size_t idx2 = ValueBaseToIndex.getIndex(V2.getDef()); + size_t idx1 = MemoryBehaviorValueBaseToIndex.getIndex(V1.getDef()); + size_t idx2 = MemoryBehaviorValueBaseToIndex.getIndex(V2.getDef()); unsigned R2 = V2.getResultNumber(); return {idx1, idx2, R2, M}; }