Skip to content

Commit 33c8e16

Browse files
committed
SIL optimizer: Support begin_cow_mutation and end_cow_mutation in some optimizations.
Mostly this is about "looking through" a begin_cow_mutation or end_cow_mutation.
1 parent 2403e56 commit 33c8e16

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

Diff for: lib/SIL/Utils/InstructionUtils.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ SILValue swift::stripCastsWithoutMarkDependence(SILValue V) {
128128

129129
auto K = V->getKind();
130130
if (isRCIdentityPreservingCast(K) ||
131-
K == ValueKind::UncheckedTrivialBitCastInst) {
131+
K == ValueKind::UncheckedTrivialBitCastInst ||
132+
K == ValueKind::EndCOWMutationInst) {
132133
V = cast<SingleValueInstruction>(V)->getOperand(0);
133134
continue;
134135
}
@@ -308,7 +309,8 @@ bool swift::onlyAffectsRefCount(SILInstruction *user) {
308309
}
309310

310311
bool swift::mayCheckRefCount(SILInstruction *User) {
311-
return isa<IsUniqueInst>(User) || isa<IsEscapingClosureInst>(User);
312+
return isa<IsUniqueInst>(User) || isa<IsEscapingClosureInst>(User) ||
313+
isa<BeginCOWMutationInst>(User);
312314
}
313315

314316
bool swift::isSanitizerInstrumentation(SILInstruction *Instruction) {

Diff for: lib/SIL/Utils/Projection.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,13 @@ Optional<ProjectionPath> ProjectionPath::getProjectionPath(SILValue Start,
371371

372372
auto Iter = End;
373373
while (Start != Iter) {
374-
Projection AP(Iter);
375-
if (!AP.isValid())
376-
break;
377-
P.Path.push_back(AP);
374+
// end_cow_mutation is not a projection, but we want to "see through" it.
375+
if (!isa<EndCOWMutationInst>(Iter)) {
376+
Projection AP(Iter);
377+
if (!AP.isValid())
378+
break;
379+
P.Path.push_back(AP);
380+
}
378381
Iter = cast<SingleValueInstruction>(*Iter).getOperand(0);
379382
}
380383

Diff for: lib/SILOptimizer/Analysis/EscapeAnalysis.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,7 @@ void EscapeAnalysis::analyzeInstruction(SILInstruction *I,
22852285
case SILInstructionKind::SelectValueInst:
22862286
analyzeSelectInst(cast<SelectValueInst>(I), ConGraph);
22872287
return;
2288+
case SILInstructionKind::EndCOWMutationInst:
22882289
case SILInstructionKind::StructInst:
22892290
case SILInstructionKind::TupleInst:
22902291
case SILInstructionKind::EnumInst: {

Diff for: lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ static bool isZeroLoadFromEmptyCollection(LoadInst *LI) {
699699
case ValueKind::UpcastInst:
700700
case ValueKind::RawPointerToRefInst:
701701
case ValueKind::AddressToPointerInst:
702+
case ValueKind::EndCOWMutationInst:
702703
addr = cast<SingleValueInstruction>(addr)->getOperand(0);
703704
break;
704705
default:

Diff for: lib/SILOptimizer/Transforms/RedundantLoadElimination.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ static bool isRLEInertInstruction(SILInstruction *Inst) {
159159
case SILInstructionKind::CondFailInst:
160160
case SILInstructionKind::IsEscapingClosureInst:
161161
case SILInstructionKind::IsUniqueInst:
162+
case SILInstructionKind::EndCOWMutationInst:
162163
case SILInstructionKind::FixLifetimeInst:
163164
case SILInstructionKind::EndAccessInst:
164165
case SILInstructionKind::SetDeallocatingInst:

Diff for: test/SILOptimizer/redundant_load_elim.sil

+13
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ bb0(%0 : $AB):
157157
return %5 : $Int // id: %15
158158
}
159159

160+
// CHECK-LABEL: sil hidden @load_forward_across_end_cow_mutation
161+
// CHECK-NOT: = load
162+
// CHECK: return %1
163+
sil hidden @load_forward_across_end_cow_mutation : $@convention(thin) (@owned AB, Int) -> Int {
164+
bb0(%0 : $AB, %1 : $Int):
165+
%2 = ref_element_addr %0 : $AB, #AB.value
166+
store %1 to %2 : $*Int
167+
%4 = end_cow_mutation %0 : $AB
168+
%5 = ref_element_addr %4 : $AB, #AB.value
169+
%6 = load %5 : $*Int
170+
return %6 : $Int
171+
}
172+
160173
// CHECK-LABEL: sil hidden @redundant_load_across_fixlifetime_inst
161174
// CHECK: = load
162175
// CHECK-NOT: = load

0 commit comments

Comments
 (0)