Skip to content

SILCombine: remove release_value of a trivial enum #35776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,7 @@ SILInstruction *SILCombiner::visitIndexAddrInst(IndexAddrInst *IA) {
/// Walks over all fields of an aggregate and checks if a reference count
/// operation for \p value is required. This differs from a simple `isTrivial`
/// check, because it treats a value_to_bridge_object instruction as "trivial".
/// It can also handle non-trivial enums with trivial cases.
static bool isTrivial(SILValue value, SILFunction *function) {
SmallVector<ValueBase *, 32> workList;
SmallPtrSet<ValueBase *, 16> visited;
Expand All @@ -1017,6 +1018,11 @@ static bool isTrivial(SILValue value, SILFunction *function) {
}
continue;
}
if (auto *en = dyn_cast<EnumInst>(v)) {
if (en->hasOperand() && visited.insert(en->getOperand()).second)
workList.push_back(en->getOperand());
continue;
}
return false;
}
return true;
Expand Down
43 changes: 43 additions & 0 deletions test/SILOptimizer/sil_combine.sil
Original file line number Diff line number Diff line change
Expand Up @@ -3855,6 +3855,49 @@ bb0(%0 : $UInt64):
return %7 : $()
}

enum EnumWithPayload {
case A(UInt64)
case B(AnyObject)
case C
}

// CHECK-LABEL: sil @optimize_arc_with_trivial_payload_enum
// CHECK: bb0(%0 : $UInt64):
// CHECK-NEXT: tuple
// CHECK-NEXT: return
// CHECK: } // end sil function 'optimize_arc_with_trivial_payload_enum'
sil @optimize_arc_with_trivial_payload_enum : $@convention(thin) (UInt64) -> () {
bb0(%0 : $UInt64):
%1 = enum $EnumWithPayload, #EnumWithPayload.A!enumelt, %0 : $UInt64
release_value %1 : $EnumWithPayload
%7 = tuple ()
return %7 : $()
}

// CHECK-LABEL: sil @optimize_arc_with_trivial_enum
// CHECK: bb0:
// CHECK-NEXT: tuple
// CHECK-NEXT: return
// CHECK: } // end sil function 'optimize_arc_with_trivial_enum'
sil @optimize_arc_with_trivial_enum : $@convention(thin) () -> () {
bb0:
%1 = enum $EnumWithPayload, #EnumWithPayload.C!enumelt
release_value %1 : $EnumWithPayload
%7 = tuple ()
return %7 : $()
}

// CHECK-LABEL: sil @dont_remove_release_of_nontrivial_enum
// CHECK: strong_release %0
// CHECK: } // end sil function 'dont_remove_release_of_nontrivial_enum'
sil @dont_remove_release_of_nontrivial_enum : $@convention(thin) (@guaranteed AnyObject) -> () {
bb0(%0 : $AnyObject):
%1 = enum $EnumWithPayload, #EnumWithPayload.B!enumelt, %0 : $AnyObject
release_value %1 : $EnumWithPayload
%7 = tuple ()
return %7 : $()
}

// CHECK-LABEL: sil @optimize_stringObject_bit_operations
// CHECK: bb0:
// CHECK-NEXT: %0 = integer_literal $Builtin.Int64, 4611686018427387904
Expand Down