Skip to content

Commit d97e7d9

Browse files
committed
UnsafeGuaranteedPeephole: Also use RCIdentity for matching retains
1 parent 7d86d6f commit d97e7d9

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lib/SILOptimizer/Transforms/UnsafeGuaranteedPeephole.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ static SILBasicBlock::iterator findReleaseToMatchUnsafeGuaranteedValue(
129129
/// sequences.
130130
static bool removeGuaranteedRetainReleasePairs(SILFunction &F,
131131
RCIdentityFunctionInfo &RCIA) {
132+
DEBUG(llvm::dbgs() << "Running on function " << F.getName() << "\n");
132133
bool Changed = false;
133134
for (auto &BB : F) {
134135
auto It = BB.begin(), End = BB.end();
@@ -139,7 +140,7 @@ static bool removeGuaranteedRetainReleasePairs(SILFunction &F,
139140

140141
// Memorize the last retain.
141142
if (isa<StrongRetainInst>(CurInst) || isa<RetainValueInst>(CurInst)) {
142-
LastRetain[CurInst->getOperand(0)] = CurInst;
143+
LastRetain[RCIA.getRCIdentityRoot(CurInst->getOperand(0))] = CurInst;
143144
continue;
144145
}
145146

@@ -151,14 +152,15 @@ static bool removeGuaranteedRetainReleasePairs(SILFunction &F,
151152
continue;
152153

153154
auto Opd = UnsafeGuaranteedI->getOperand(0);
154-
if (!LastRetain.count(Opd)) {
155+
auto RCIdOpd = RCIA.getRCIdentityRoot(UnsafeGuaranteedI->getOperand(0));
156+
if (!LastRetain.count(RCIdOpd)) {
155157
DEBUG(llvm::dbgs() << "LastRetain failed\n");
156158
continue;
157159
}
158160

159161
// This code is very conservative. Check that there is a matching retain
160162
// before the unsafeGuaranteed builtin with only retains inbetween.
161-
auto *LastRetainInst = LastRetain[Opd];
163+
auto *LastRetainInst = LastRetain[RCIdOpd];
162164
auto NextInstIter = std::next(SILBasicBlock::iterator(LastRetainInst));
163165
while (NextInstIter != BB.end() && &*NextInstIter != CurInst &&
164166
(isa<RetainValueInst>(*NextInstIter) ||

test/SILOptimizer/unsafe_guaranteed_peephole.sil

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,36 @@ bb0(%0 : $Foo, %1: $Builtin.Int32):
402402
%17 = tuple ()
403403
return %17 : $()
404404
}
405+
406+
// CHECK-LABEL: sil @testUnsafeGuaranteed_cast2_inst
407+
// CHECK: bb0([[P:%.*]] : $AnyObject
408+
// CHECK: [[C:%.*]] = unchecked_ref_cast [[P]] : $AnyObject to $Foo
409+
// CHECK-NOT: retain
410+
// CHECK-NOT: unsafeGuaranteed
411+
// CHECK: [[U:%.*]] = upcast [[C]] : $Foo to $Base
412+
// CHECK: [[M:%.*]] = class_method [[U]] : $Base, #Base.beep
413+
// CHECK: apply [[M]]([[U]])
414+
// CHECK-NOT: release
415+
// CHECK-NOT: unsafeGuaranteedEnd
416+
// CHECK: [[T:%.*]] = tuple ()
417+
// CHECK: return [[T]]
418+
// CHECK: }
419+
sil @testUnsafeGuaranteed_cast2_inst : $@convention(method) (@guaranteed AnyObject, Builtin.Int32) -> () {
420+
bb0(%0 : $AnyObject, %1: $Builtin.Int32):
421+
%2 = unchecked_ref_cast %0 : $AnyObject to $Foo
422+
strong_retain %0 : $AnyObject
423+
debug_value %0 : $AnyObject
424+
%3 = struct $MyInt(%1 : $Builtin.Int32)
425+
%4 = builtin "unsafeGuaranteed"<Foo>(%2 : $Foo) : $(Foo, Builtin.Int8)
426+
%5 = tuple_extract %4 : $(Foo, Builtin.Int8), 0
427+
%6 = tuple_extract %4 : $(Foo, Builtin.Int8), 1
428+
%7 = upcast %5 : $Foo to $Base
429+
%19 = class_method %7 : $Base, #Base.beep!1 : (Base) -> () -> () , $@convention(method) (@guaranteed Base) -> ()
430+
%20 = apply %19(%7) : $@convention(method) (@guaranteed Base) -> ()
431+
strong_release %7 : $Base
432+
%21 = struct $MyInt(%1 : $Builtin.Int32)
433+
debug_value %5 : $Foo
434+
%16 = builtin "unsafeGuaranteedEnd"(%6 : $Builtin.Int8) : $()
435+
%17 = tuple ()
436+
return %17 : $()
437+
}

0 commit comments

Comments
 (0)