@@ -32,6 +32,7 @@ class DominanceAnalysis;
3232// / This class is a simple wrapper around an identity cache.
3333class RCIdentityFunctionInfo {
3434 llvm::DenseSet<SILArgument *> VisitedArgs;
35+ llvm::DenseMap<SILValue, SILValue> Cache;
3536 DominanceAnalysis *DA;
3637
3738 // / This number is arbitrary and conservative. At some point if compile time
@@ -50,6 +51,16 @@ class RCIdentityFunctionInfo {
5051 // / unchecked_trivial_bit_cast.
5152 void getRCUsers (SILValue V, llvm::SmallVectorImpl<SILInstruction *> &Users);
5253
54+ void handleDeleteNotification (SILInstruction *I) {
55+ // Check the cache. If we don't find it, there is nothing to do.
56+ auto Iter = Cache.find (SILValue (I));
57+ if (Iter == Cache.end ())
58+ return ;
59+
60+ // Then erase Iter from the cache.
61+ Cache.erase (Iter);
62+ }
63+
5364private:
5465 SILValue getRCIdentityRootInner (SILValue V, unsigned RecursionDepth);
5566 SILValue stripRCIdentityPreservingOps (SILValue V, unsigned RecursionDepth);
@@ -84,6 +95,23 @@ class RCIdentityAnalysis : public FunctionAnalysisBase<RCIdentityFunctionInfo> {
8495 return true ;
8596 }
8697
98+ virtual void handleDeleteNotification (ValueBase *V) override {
99+ auto *I = dyn_cast<SILInstruction>(V);
100+ // We are only interesting in SILInstructions.
101+ if (!I || !I->getParentBB ())
102+ return ;
103+
104+ // If the parent function of this instruction was just turned into an
105+ // external declaration, bail. This happens during SILFunction destruction,
106+ // when
107+ SILFunction *F = I->getFunction ();
108+ if (F->isExternalDeclaration ()) {
109+ return ;
110+ }
111+ get (F)->handleDeleteNotification (I);
112+ }
113+
114+ virtual bool needsNotifications () override { return true ; }
87115 };
88116
89117} // end swift namespace
0 commit comments