Skip to content

Commit 971aa23

Browse files
committed
[AliasAnalysis] Remove the AliasAnalysis Cache.
The AliasAnalysis is unsafe. When we delete instructions we don't have a good mechanism for invalidating the cache and we don't want instructions to explicitly invalidate AliasAnalysis after every deletion of an instruction. Morever, the alias analysis cache _always_ misses. Removing the cache did not change the build time of the standard library at all.
1 parent 00e1685 commit 971aa23

File tree

2 files changed

+4
-41
lines changed

2 files changed

+4
-41
lines changed

Diff for: include/swift/SILAnalysis/AliasAnalysis.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,11 @@ class AliasAnalysis : public SILAnalysis {
5656
};
5757

5858
private:
59-
using AliasCacheKey = std::pair<SILValue, SILValue>;
60-
llvm::DenseMap<AliasCacheKey, AliasResult> AliasCache;
6159
SILModule *Mod;
6260
SideEffectAnalysis *SEA;
6361

6462
using MemoryBehavior = SILInstruction::MemoryBehavior;
6563

66-
AliasResult cacheValue(AliasCacheKey Key, AliasResult Result);
67-
6864
AliasResult aliasAddressProjection(SILValue V1, SILValue V2,
6965
SILValue O1, SILValue O2);
7066

@@ -154,7 +150,7 @@ class AliasAnalysis : public SILAnalysis {
154150
return MemoryBehavior::MayHaveSideEffects == B;
155151
}
156152

157-
virtual void invalidate(SILAnalysis::InvalidationKind K) { AliasCache.clear(); }
153+
virtual void invalidate(SILAnalysis::InvalidationKind K) { }
158154

159155
virtual void invalidate(SILFunction *, SILAnalysis::InvalidationKind K) {
160156
invalidate(K);

Diff for: lib/SILAnalysis/AliasAnalysis.cpp

+3-36
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ static inline bool shouldRunBasicAA() {
7676

7777
#endif
7878

79-
static llvm::cl::opt<bool>
80-
CacheAAResults("cache-aa-results",
81-
llvm::cl::desc("Should AA results be cached"),
82-
llvm::cl::init(true));
83-
8479
//===----------------------------------------------------------------------===//
8580
// Utilities
8681
//===----------------------------------------------------------------------===//
@@ -605,9 +600,7 @@ static bool typesMayAlias(SILType T1, SILType T2, SILType TBAA1Ty,
605600
AliasResult AliasAnalysis::alias(SILValue V1, SILValue V2,
606601
SILType TBAAType1,
607602
SILType TBAAType2) {
608-
auto Result = aliasInner(V1, V2, TBAAType1, TBAAType2);
609-
AliasCache.clear();
610-
return Result;
603+
return aliasInner(V1, V2, TBAAType1, TBAAType2);
611604
}
612605

613606
/// The main AA entry point. Performs various analyses on V1, V2 in an attempt
@@ -644,24 +637,6 @@ AliasResult AliasAnalysis::aliasInner(SILValue V1, SILValue V2,
644637
DEBUG(llvm::dbgs() << " After Cast Stripping V1:" << *V1.getDef());
645638
DEBUG(llvm::dbgs() << " After Cast Stripping V2:" << *V2.getDef());
646639

647-
// Create a key to lookup if we have already computed an alias result for V1,
648-
// V2. Canonicalize our cache keys so that the pointer with the lower address
649-
// is always the first element of the pair. This ensures we do not pollute our
650-
// cache with two entries with the same key, albeit with the key's fields
651-
// swapped.
652-
auto Key = V1 < V2? std::make_pair(V1, V2) : std::make_pair(V2, V1);
653-
654-
// If we find our key in the cache, just return the alias result.
655-
if (CacheAAResults) {
656-
auto Pair = AliasCache.find(Key);
657-
if (Pair != AliasCache.end()) {
658-
DEBUG(llvm::dbgs() << " Found value in the cache: " << Pair->second
659-
<< "\n");
660-
661-
return Pair->second;
662-
}
663-
}
664-
665640
// Ok, we need to actually compute an Alias Analysis result for V1, V2. Begin
666641
// by finding the "base" of V1, V2 by stripping off all casts and GEPs.
667642
SILValue O1 = getUnderlyingObject(V1);
@@ -672,7 +647,7 @@ AliasResult AliasAnalysis::aliasInner(SILValue V1, SILValue V2,
672647
// If O1 and O2 do not equal, see if we can prove that they can not be the
673648
// same object. If we can, return No Alias.
674649
if (O1 != O2 && aliasUnequalObjects(O1, O2))
675-
return cacheValue(Key, AliasResult::NoAlias);
650+
return AliasResult::NoAlias;
676651

677652
// Ok, either O1, O2 are the same or we could not prove anything based off of
678653
// their inequality. Now we climb up use-def chains and attempt to do tricks
@@ -690,10 +665,9 @@ AliasResult AliasAnalysis::aliasInner(SILValue V1, SILValue V2,
690665
if (Projection::isAddrProjection(V1)) {
691666
AliasResult Result = aliasAddressProjection(V1, V2, O1, O2);
692667
if (Result != AliasResult::MayAlias)
693-
return cacheValue(Key, Result);
668+
return Result;
694669
}
695670

696-
697671
// We could not prove anything. Be conservative and return that V1, V2 may
698672
// alias.
699673
return AliasResult::MayAlias;
@@ -727,13 +701,6 @@ bool swift::isLetPointer(SILValue V) {
727701
return false;
728702
}
729703

730-
AliasResult AliasAnalysis::cacheValue(AliasCacheKey Key, AliasResult Result) {
731-
if (!CacheAAResults)
732-
return Result;
733-
DEBUG(llvm::dbgs() << "Caching Alias Result: " << Result << "\n" << Key.first
734-
<< Key.second << "\n");
735-
return AliasCache[Key] = Result;
736-
}
737704

738705
void AliasAnalysis::initialize(SILPassManager *PM) {
739706
SEA = PM->getAnalysis<SideEffectAnalysis>();

0 commit comments

Comments
 (0)