37
37
#include " swift/SIL/SILInstruction.h"
38
38
#include " swift/SIL/SILModule.h"
39
39
#include " swift/SILOptimizer/Analysis/ARCAnalysis.h"
40
+ #include " swift/SILOptimizer/Analysis/DominanceAnalysis.h"
40
41
#include " swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
41
42
#include " swift/SILOptimizer/PassManager/Transforms.h"
42
43
#include " swift/SILOptimizer/Utils/Local.h"
@@ -46,9 +47,14 @@ using namespace swift;
46
47
// / Remove retain/release pairs around builtin "unsafeGuaranteed" instruction
47
48
// / sequences.
48
49
static bool removeGuaranteedRetainReleasePairs (SILFunction &F,
49
- RCIdentityFunctionInfo &RCIA) {
50
+ RCIdentityFunctionInfo &RCIA,
51
+ PostDominanceAnalysis *PDA) {
50
52
DEBUG (llvm::dbgs () << " Running on function " << F.getName () << " \n " );
51
53
bool Changed = false ;
54
+
55
+ // Lazily compute post-dominance info only when we really need it.
56
+ PostDominanceInfo *PDI = nullptr ;
57
+
52
58
for (auto &BB : F) {
53
59
auto It = BB.begin (), End = BB.end ();
54
60
llvm::DenseMap<SILValue, SILInstruction *> LastRetain;
@@ -119,6 +125,15 @@ static bool removeGuaranteedRetainReleasePairs(SILFunction &F,
119
125
continue ;
120
126
}
121
127
128
+ if (!PDI)
129
+ PDI = PDA->get (&F);
130
+
131
+ // It needs to post-dominated the end instruction, since we need to remove
132
+ // the release along all paths to exit.
133
+ if (!PDI->properlyDominates (UnsafeGuaranteedEndI, UnsafeGuaranteedI))
134
+ continue ;
135
+
136
+
122
137
// Find the release to match with the unsafeGuaranteedValue.
123
138
auto &UnsafeGuaranteedEndBB = *UnsafeGuaranteedEndI->getParent ();
124
139
auto LastRelease = findReleaseToMatchUnsafeGuaranteedValue (
@@ -165,7 +180,8 @@ class UnsafeGuaranteedPeephole : public swift::SILFunctionTransform {
165
180
166
181
void run () override {
167
182
auto &RCIA = *getAnalysis<RCIdentityAnalysis>()->get (getFunction ());
168
- if (removeGuaranteedRetainReleasePairs (*getFunction (), RCIA))
183
+ auto *PostDominance = getAnalysis<PostDominanceAnalysis>();
184
+ if (removeGuaranteedRetainReleasePairs (*getFunction (), RCIA, PostDominance))
169
185
invalidateAnalysis (SILAnalysis::InvalidationKind::Instructions);
170
186
}
171
187
0 commit comments