Skip to content

Commit ddba98d

Browse files
committed
[sil-combine] Enable mark_dependence simplifications.
1 parent 2fad943 commit ddba98d

File tree

2 files changed

+58
-41
lines changed

2 files changed

+58
-41
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,49 +2126,64 @@ visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
21262126
}
21272127

21282128
SILInstruction *SILCombiner::visitMarkDependenceInst(MarkDependenceInst *mdi) {
2129-
if (mdi->getFunction()->hasOwnership())
2130-
return nullptr;
2129+
auto base = lookThroughOwnershipInsts(mdi->getBase());
21312130

21322131
// Simplify the base operand of a MarkDependenceInst to eliminate unnecessary
21332132
// instructions that aren't adding value.
21342133
//
21352134
// Conversions to Optional.Some(x) often happen here, this isn't important
21362135
// for us, we can just depend on 'x' directly.
2137-
if (auto *eiBase = dyn_cast<EnumInst>(mdi->getBase())) {
2138-
if (eiBase->hasOperand() && eiBase->hasOneUse()) {
2139-
mdi->setBase(eiBase->getOperand());
2140-
eraseInstFromFunction(*eiBase);
2141-
return mdi;
2136+
if (auto *eiBase = dyn_cast<EnumInst>(base)) {
2137+
if (eiBase->hasOperand()) {
2138+
auto *use = &mdi->getOperandRef(MarkDependenceInst::Base);
2139+
OwnershipReplaceSingleUseHelper helper(ownershipFixupContext,
2140+
use, eiBase->getOperand());
2141+
if (helper) {
2142+
helper.perform();
2143+
tryEliminateOnlyOwnershipUsedForwardingInst(eiBase, instModCallbacks);
2144+
return mdi;
2145+
}
21422146
}
21432147
}
2144-
2148+
21452149
// Conversions from a class to AnyObject also happen a lot, we can just depend
21462150
// on the class reference.
2147-
if (auto *ier = dyn_cast<InitExistentialRefInst>(mdi->getBase())) {
2148-
mdi->setBase(ier->getOperand());
2149-
if (ier->use_empty())
2150-
eraseInstFromFunction(*ier);
2151-
return mdi;
2151+
if (auto *ier = dyn_cast<InitExistentialRefInst>(base)) {
2152+
auto *use = &mdi->getOperandRef(MarkDependenceInst::Base);
2153+
OwnershipReplaceSingleUseHelper helper(ownershipFixupContext,
2154+
use, ier->getOperand());
2155+
if (helper) {
2156+
helper.perform();
2157+
tryEliminateOnlyOwnershipUsedForwardingInst(ier, instModCallbacks);
2158+
return mdi;
2159+
}
21522160
}
21532161

21542162
// Conversions from a class to AnyObject also happen a lot, we can just depend
21552163
// on the class reference.
2156-
if (auto *oeri = dyn_cast<OpenExistentialRefInst>(mdi->getBase())) {
2157-
mdi->setBase(oeri->getOperand());
2158-
if (oeri->use_empty())
2159-
eraseInstFromFunction(*oeri);
2160-
return mdi;
2164+
if (auto *oeri = dyn_cast<OpenExistentialRefInst>(base)) {
2165+
auto *use = &mdi->getOperandRef(MarkDependenceInst::Base);
2166+
OwnershipReplaceSingleUseHelper helper(ownershipFixupContext,
2167+
use, oeri->getOperand());
2168+
if (helper) {
2169+
helper.perform();
2170+
tryEliminateOnlyOwnershipUsedForwardingInst(oeri, instModCallbacks);
2171+
return mdi;
2172+
}
21612173
}
21622174

21632175
// Sometimes due to specialization/builtins, we can get a mark_dependence
21642176
// whose base is a trivial typed object. In such a case, the mark_dependence
21652177
// does not have a meaning, so just eliminate it.
21662178
{
2167-
SILType baseType = mdi->getBase()->getType();
2168-
if (baseType.isObject() && baseType.isTrivial(*mdi->getFunction())) {
2169-
SILValue value = mdi->getValue();
2170-
mdi->replaceAllUsesWith(value);
2171-
return eraseInstFromFunction(*mdi);
2179+
SILType baseType = base->getType();
2180+
if (baseType.isObject()) {
2181+
if ((hasOwnership() && base.getOwnershipKind() == OwnershipKind::None) ||
2182+
baseType.isTrivial(*mdi->getFunction())) {
2183+
SILValue value = mdi->getValue();
2184+
replaceInstUsesWith(*mdi, value);
2185+
return eraseInstFromFunction(*mdi);
2186+
}
21722187
}
21732188
}
21742189

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4139,13 +4139,14 @@ bb2(%5 : @owned $MyErrorType):
41394139
throw %5 : $MyErrorType
41404140
}
41414141

4142-
// CHECK-LABEL: sil [ossa] @mark_dependence_base
4143-
// XHECK: bb0(
4144-
// XHECK-NOT: init_existential_ref
4145-
// XHECK-NOT: enum
4146-
// XHECK-NEXT: mark_dependence
4147-
// XHECK-NEXT: load
4148-
// XHECK: return
4142+
// CHECK-LABEL: sil [ossa] @mark_dependence_base :
4143+
// CHECK: bb0(
4144+
// CHECK-NOT: init_existential_ref
4145+
// CHECK-NOT: enum
4146+
// CHECK: mark_dependence %0 : $*Builtin.Int64 on {{%.*}} : $B
4147+
// CHECK-NOT: init_existential_ref
4148+
// CHECK-NOT: enum
4149+
// CHECK: } // end sil function 'mark_dependence_base'
41494150
sil [ossa] @mark_dependence_base : $@convention(thin) (@inout Builtin.Int64, @owned B) -> Builtin.Int64 {
41504151
bb0(%0 : $*Builtin.Int64, %1 : @owned $B):
41514152
%x = init_existential_ref %1 : $B : $B, $AnyObject
@@ -4157,10 +4158,10 @@ bb0(%0 : $*Builtin.Int64, %1 : @owned $B):
41574158
}
41584159

41594160
// CHECK-LABEL: sil [ossa] @mark_dependence_trivial_object_base :
4160-
// XHECK: bb0(
4161-
// XHECK-NEXT: copy_value
4162-
// XHECK-NEXT: return
4163-
// XHECK: } // end sil function 'mark_dependence_trivial_object_base'
4161+
// CHECK: bb0(
4162+
// CHECK-NEXT: copy_value
4163+
// CHECK-NEXT: return
4164+
// CHECK: } // end sil function 'mark_dependence_trivial_object_base'
41644165
sil [ossa] @mark_dependence_trivial_object_base : $@convention(thin) (@guaranteed B) -> @owned B {
41654166
bb0(%0 : @guaranteed $B):
41664167
%0a = copy_value %0 : $B
@@ -4171,13 +4172,14 @@ bb0(%0 : @guaranteed $B):
41714172

41724173
protocol _NSArrayCore {}
41734174

4174-
// CHECK-LABEL: sil [ossa] @mark_dependence_base2
4175-
// XHECK: bb0(
4176-
// XHECK-NOT: open_existential_ref
4177-
// XHECK-NOT: enum
4178-
// XHECK-NEXT: mark_dependence
4179-
// XHECK-NEXT: load
4180-
// XHECK: return
4175+
// CHECK-LABEL: sil [ossa] @mark_dependence_base2 :
4176+
// CHECK: bb0(
4177+
// CHECK-NOT: open_existential_ref
4178+
// CHECK-NOT: enum
4179+
// CHECK: mark_dependence %0 : $*Builtin.Int64 on {{%.*}} : $B
4180+
// CHECK-NOT: open_existential_ref
4181+
// CHECK-NOT: enum
4182+
// CHECK: } // end sil function 'mark_dependence_base2'
41814183
sil [ossa] @mark_dependence_base2 : $@convention(thin) (@inout Builtin.Int64, @owned B) -> Builtin.Int64 {
41824184
bb0(%0 : $*Builtin.Int64, %1 : @owned $B):
41834185
%2 = init_existential_ref %1 : $B : $B, $AnyObject

0 commit comments

Comments
 (0)