@@ -2126,49 +2126,64 @@ visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
2126
2126
}
2127
2127
2128
2128
SILInstruction *SILCombiner::visitMarkDependenceInst (MarkDependenceInst *mdi) {
2129
- if (mdi->getFunction ()->hasOwnership ())
2130
- return nullptr ;
2129
+ auto base = lookThroughOwnershipInsts (mdi->getBase ());
2131
2130
2132
2131
// Simplify the base operand of a MarkDependenceInst to eliminate unnecessary
2133
2132
// instructions that aren't adding value.
2134
2133
//
2135
2134
// Conversions to Optional.Some(x) often happen here, this isn't important
2136
2135
// 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
+ }
2142
2146
}
2143
2147
}
2144
-
2148
+
2145
2149
// Conversions from a class to AnyObject also happen a lot, we can just depend
2146
2150
// 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
+ }
2152
2160
}
2153
2161
2154
2162
// Conversions from a class to AnyObject also happen a lot, we can just depend
2155
2163
// 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
+ }
2161
2173
}
2162
2174
2163
2175
// Sometimes due to specialization/builtins, we can get a mark_dependence
2164
2176
// whose base is a trivial typed object. In such a case, the mark_dependence
2165
2177
// does not have a meaning, so just eliminate it.
2166
2178
{
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
+ }
2172
2187
}
2173
2188
}
2174
2189
0 commit comments