Files
Aidan Hall 51dec85971 Simplify tuple_pack_element_addr with scalar_pack_index
If a tuple_pack_element_addr uses a scalar_pack_index rather than a
dynamic_pack_index, we can replace it with tuple_element_addr, since the
specific tuple element accessed is statically known.
2026-05-21 18:00:40 +01:00

41 lines
1.5 KiB
Swift

//===--- SimplifyTuple.swift ---------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import AST
import SIL
// Replace a tuple_pack_element_addr_inst with a scalar_pack_index index operand
// with tuple_element_addr, if its tuple type contains no pack expansions.
//
// %tuple = alloc_stack $(Int, Int)
// %idx = scalar_pack_index 0 of $Pack{Int, Int}
// %addr = tuple_pack_element_addr %idx of %tuple as $*Int
//
// is transformed to:
//
// %tuple = alloc_stack $(Int, Int)
// %addr = tuple_element_addr 0 of %tuple as $*Int
//
extension TuplePackElementAddrInst : Simplifiable, SILCombineSimplifiable {
func simplify(_ context: SimplifyContext) {
guard !self.tupleOperand.value.type.tupleContainsPackExpansionType,
let scalarIndex = self.indexOperand.value as? ScalarPackIndexInst
else {
return
}
let builder = Builder(before: self, context)
let tupleElementAddr = builder.createTupleElementAddr(tupleAddress: self.tupleOperand.value, elementIndex: scalarIndex.componentIndex)
self.replace(with: tupleElementAddr, context)
}
}