mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
AliasAnalysis: make AliasAnalysis a function analysis and simplify the cache keys
Instead of caching alias results globally for the module, make AliasAnalysis a FunctionAnalysisBase which caches the alias results per function.
Why?
* So far the result caches could only grow. They were reset when they reached a certain size. This was not ideal. Now, they are invalidated whenever the function changes.
* It was not possible to actually invalidate an alias analysis result. This is required, for example in TempRValueOpt and TempLValueOpt (so far it was done manually with invalidateInstruction).
* Type based alias analysis results were also cached for the whole module, while it is actually dependent on the function, because it depends on the function's resilience expansion. This was a potential bug.
I also added a new PassManager API to directly get a function-base analysis:
getAnalysis(SILFunction *f)
The second change of this commit is the removal of the instruction-index indirection for the cache keys. Now the cache keys directly work on instruction pointers instead of instruction indices. This reduces the number of hash table lookups for a cache lookup from 3 to 1.
This indirection was needed to avoid dangling instruction pointers in the cache keys. But this is not needed anymore, because of the new delayed instruction deletion mechanism.
This commit is contained in:
@@ -31,12 +31,6 @@
|
||||
using namespace swift;
|
||||
|
||||
|
||||
// The AliasAnalysis Cache must not grow beyond this size.
|
||||
// We limit the size of the AA cache to 2**14 because we want to limit the
|
||||
// memory usage of this cache.
|
||||
static const int AliasAnalysisMaxCacheSize = 16384;
|
||||
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// AA Debugging
|
||||
//===----------------------------------------------------------------------===//
|
||||
@@ -538,27 +532,6 @@ bool AliasAnalysis::typesMayAlias(SILType T1, SILType T2,
|
||||
return MA;
|
||||
}
|
||||
|
||||
void AliasAnalysis::handleDeleteNotification(SILNode *node) {
|
||||
// The pointer 'node' 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 index caches.
|
||||
|
||||
if (auto *value = dyn_cast<ValueBase>(node))
|
||||
ValueToIndex.invalidateValue(value);
|
||||
|
||||
if (auto *inst = dyn_cast<SILInstruction>(node)) {
|
||||
InstructionToIndex.invalidateValue(inst);
|
||||
|
||||
// When a MultipleValueInstruction is deleted, we have to invalidate all
|
||||
// the instruction results.
|
||||
if (auto *mvi = dyn_cast<MultipleValueInstruction>(inst)) {
|
||||
for (unsigned idx = 0, end = mvi->getNumResults(); idx < end; ++idx) {
|
||||
ValueToIndex.invalidateValue(mvi->getResult(idx));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Entry Points
|
||||
//===----------------------------------------------------------------------===//
|
||||
@@ -567,7 +540,8 @@ void AliasAnalysis::handleDeleteNotification(SILNode *node) {
|
||||
/// to disambiguate the two values.
|
||||
AliasResult AliasAnalysis::alias(SILValue V1, SILValue V2,
|
||||
SILType TBAAType1, SILType TBAAType2) {
|
||||
AliasKeyTy Key = toAliasKey(V1, V2, TBAAType1, TBAAType2);
|
||||
AliasCacheKey Key = {V1, V2, TBAAType1.getOpaqueValue(),
|
||||
TBAAType2.getOpaqueValue()};
|
||||
|
||||
// Check if we've already computed this result.
|
||||
auto It = AliasCache.find(Key);
|
||||
@@ -575,11 +549,6 @@ AliasResult AliasAnalysis::alias(SILValue V1, SILValue V2,
|
||||
return It->second;
|
||||
}
|
||||
|
||||
// Flush the cache if the size of the cache is too large.
|
||||
if (AliasCache.size() > AliasAnalysisMaxCacheSize) {
|
||||
AliasCache.clear();
|
||||
}
|
||||
|
||||
// Calculate the aliasing result and store it in the cache.
|
||||
auto Result = aliasInner(V1, V2, TBAAType1, TBAAType2);
|
||||
AliasCache[Key] = Result;
|
||||
@@ -807,24 +776,35 @@ bool AliasAnalysis::mayValueReleaseInterfereWithInstruction(
|
||||
return EA->mayReleaseReferenceContent(releasedReference, accessedPointer);
|
||||
}
|
||||
|
||||
void AliasAnalysis::initialize(SILPassManager *PM) {
|
||||
SEA = PM->getAnalysis<SideEffectAnalysis>();
|
||||
EA = PM->getAnalysis<EscapeAnalysis>();
|
||||
}
|
||||
namespace {
|
||||
|
||||
class AliasAnalysisContainer : public FunctionAnalysisBase<AliasAnalysis> {
|
||||
SideEffectAnalysis *SEA = nullptr;
|
||||
EscapeAnalysis *EA = nullptr;
|
||||
|
||||
public:
|
||||
AliasAnalysisContainer() : FunctionAnalysisBase(SILAnalysisKind::Alias) {}
|
||||
|
||||
virtual bool shouldInvalidate(SILAnalysis::InvalidationKind K) override {
|
||||
return K & InvalidationKind::Instructions;
|
||||
}
|
||||
|
||||
// Computes loop information for the given function using dominance
|
||||
// information.
|
||||
virtual std::unique_ptr<AliasAnalysis>
|
||||
newFunctionAnalysis(SILFunction *F) override {
|
||||
assert(EA && SEA && "dependent analysis not initialized");
|
||||
return std::make_unique<AliasAnalysis>(SEA, EA);
|
||||
}
|
||||
|
||||
virtual void initialize(SILPassManager *PM) override {
|
||||
SEA = PM->getAnalysis<SideEffectAnalysis>();
|
||||
EA = PM->getAnalysis<EscapeAnalysis>();
|
||||
}
|
||||
};
|
||||
|
||||
} // end anonymous namespace
|
||||
|
||||
SILAnalysis *swift::createAliasAnalysis(SILModule *M) {
|
||||
return new AliasAnalysis(M);
|
||||
}
|
||||
|
||||
AliasKeyTy AliasAnalysis::toAliasKey(SILValue V1, SILValue V2,
|
||||
SILType Type1, SILType Type2) {
|
||||
ValueIndexTy idx1 = ValueToIndex.getIndex(V1);
|
||||
assert(idx1 != std::numeric_limits<ValueIndexTy>::max() &&
|
||||
"~0 index reserved for empty/tombstone keys");
|
||||
ValueIndexTy idx2 = ValueToIndex.getIndex(V2);
|
||||
assert(idx2 != std::numeric_limits<ValueIndexTy>::max() &&
|
||||
"~0 index reserved for empty/tombstone keys");
|
||||
void *t1 = Type1.getOpaqueValue();
|
||||
void *t2 = Type2.getOpaqueValue();
|
||||
return {idx1, idx2, t1, t2};
|
||||
return new AliasAnalysisContainer();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user