Skip to content

Commit c4cc6e4

Browse files
committed
[const-expr] Teach const-expr evaluator about destructures and translate its main test into an ownership test.
1 parent b2870a8 commit c4cc6e4

File tree

2 files changed

+505
-1
lines changed

2 files changed

+505
-1
lines changed

lib/SILOptimizer/Utils/ConstExpr.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,26 @@ SymbolicValue ConstExprFunctionState::computeConstantValue(SILValue value) {
213213
return val;
214214
}
215215

216+
// If this is a destructure_result, then we can return the element being
217+
// extracted.
218+
if (isa<DestructureStructResult>(value) ||
219+
isa<DestructureTupleResult>(value)) {
220+
auto *result = cast<MultipleValueInstructionResult>(value);
221+
SILValue aggValue = result->getParent()->getOperand(0);
222+
auto val = getConstantValue(aggValue);
223+
if (val.isConstant()) {
224+
assert(val.getKind() == SymbolicValue::Aggregate);
225+
return val.getAggregateValue()[result->getIndex()];
226+
}
227+
// Not a const.
228+
return val;
229+
}
230+
216231
// TODO: If this is a single element struct, we can avoid creating an
217232
// aggregate to reduce # allocations. This is extra silly in the case of zero
218233
// element tuples.
219234
if (isa<StructInst>(value) || isa<TupleInst>(value)) {
220-
auto inst = cast<SingleValueInstruction>(value);
235+
auto *inst = cast<SingleValueInstruction>(value);
221236
SmallVector<SymbolicValue, 4> elts;
222237

223238
for (unsigned i = 0, e = inst->getNumOperands(); i != e; ++i) {

0 commit comments

Comments
 (0)