36
36
#include " swift/SIL/SILFunction.h"
37
37
#include " swift/SIL/SILInstruction.h"
38
38
#include " swift/SIL/SILModule.h"
39
+ #include " swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
39
40
#include " swift/SILOptimizer/PassManager/Transforms.h"
40
41
#include " swift/SILOptimizer/Utils/Local.h"
41
42
@@ -92,18 +93,21 @@ getSingleUnsafeGuaranteedValueResult(BuiltinInst *BI) {
92
93
// / strong_release %6 : $Foo // Ignore.
93
94
// / %12 = builtin "unsafeGuaranteedEnd"(%6 : $Builtin.Int8) : $()
94
95
// /
95
- static SILBasicBlock::iterator
96
- findReleaseToMatchUnsafeGuaranteedValue ( SILInstruction *UnsafeGuaranteedEndI,
97
- SILValue UnsafeGuaranteedValue,
98
- SILBasicBlock &BB) {
96
+ static SILBasicBlock::iterator findReleaseToMatchUnsafeGuaranteedValue (
97
+ SILInstruction *UnsafeGuaranteedEndI, SILValue UnsafeGuaranteedValue ,
98
+ SILBasicBlock &BB, RCIdentityFunctionInfo &RCIA) {
99
+
99
100
auto UnsafeGuaranteedEndIIt = SILBasicBlock::iterator (UnsafeGuaranteedEndI);
100
101
if (UnsafeGuaranteedEndIIt == BB.begin ())
101
102
return BB.end ();
102
103
auto LastReleaseIt = std::prev (UnsafeGuaranteedEndIIt);
104
+ auto UnsafeGuaranteedRCIdentityRoot =
105
+ RCIA.getRCIdentityRoot (UnsafeGuaranteedValue);
103
106
while (LastReleaseIt != BB.begin () &&
104
107
(((isa<StrongReleaseInst>(*LastReleaseIt) ||
105
108
isa<ReleaseValueInst>(*LastReleaseIt)) &&
106
- LastReleaseIt->getOperand (0 ) != UnsafeGuaranteedValue &&
109
+ RCIA.getRCIdentityRoot (LastReleaseIt->getOperand (0 )) !=
110
+ UnsafeGuaranteedRCIdentityRoot &&
107
111
LastReleaseIt->getOperand (0 ) !=
108
112
cast<SILInstruction>(UnsafeGuaranteedValue)->getOperand (0 )) ||
109
113
!LastReleaseIt->mayHaveSideEffects () ||
@@ -112,7 +116,8 @@ findReleaseToMatchUnsafeGuaranteedValue(SILInstruction *UnsafeGuaranteedEndI,
112
116
--LastReleaseIt;
113
117
if ((!isa<StrongReleaseInst>(*LastReleaseIt) &&
114
118
!isa<ReleaseValueInst>(*LastReleaseIt)) ||
115
- (LastReleaseIt->getOperand (0 ) != UnsafeGuaranteedValue &&
119
+ (RCIA.getRCIdentityRoot (LastReleaseIt->getOperand (0 )) !=
120
+ UnsafeGuaranteedRCIdentityRoot &&
116
121
LastReleaseIt->getOperand (0 ) !=
117
122
cast<SILInstruction>(UnsafeGuaranteedValue)->getOperand (0 ))) {
118
123
return BB.end ();
@@ -122,7 +127,8 @@ findReleaseToMatchUnsafeGuaranteedValue(SILInstruction *UnsafeGuaranteedEndI,
122
127
123
128
// / Remove retain/release pairs around builtin "unsafeGuaranteed" instruction
124
129
// / sequences.
125
- static bool removeGuaranteedRetainReleasePairs (SILFunction &F) {
130
+ static bool removeGuaranteedRetainReleasePairs (SILFunction &F,
131
+ RCIdentityFunctionInfo &RCIA) {
126
132
bool Changed = false ;
127
133
for (auto &BB : F) {
128
134
auto It = BB.begin (), End = BB.end ();
@@ -216,7 +222,8 @@ static bool removeGuaranteedRetainReleasePairs(SILFunction &F) {
216
222
// Find the release to match with the unsafeGuaranteedValue.
217
223
auto &UnsafeGuaranteedEndBB = *UnsafeGuaranteedEndI->getParent ();
218
224
auto LastReleaseIt = findReleaseToMatchUnsafeGuaranteedValue (
219
- UnsafeGuaranteedEndI, UnsafeGuaranteedValue, UnsafeGuaranteedEndBB);
225
+ UnsafeGuaranteedEndI, UnsafeGuaranteedValue, UnsafeGuaranteedEndBB,
226
+ RCIA);
220
227
if (LastReleaseIt == UnsafeGuaranteedEndBB.end ()) {
221
228
DEBUG (llvm::dbgs () << " no release before unsafeGuaranteedEnd found\n " );
222
229
continue ;
@@ -258,7 +265,8 @@ namespace {
258
265
class UnsafeGuaranteedPeephole : public swift ::SILFunctionTransform {
259
266
260
267
void run () override {
261
- if (removeGuaranteedRetainReleasePairs (*getFunction ()))
268
+ auto &RCIA = *getAnalysis<RCIdentityAnalysis>()->get (getFunction ());
269
+ if (removeGuaranteedRetainReleasePairs (*getFunction (), RCIA))
262
270
invalidateAnalysis (SILAnalysis::InvalidationKind::Instructions);
263
271
}
264
272
0 commit comments