Skip to content

Commit 88973a3

Browse files
committed
Swift Optimizer: add simplification for tuple_extract
Replace tuple_extract(tuple(x)) -> x
1 parent 5a3ab6e commit 88973a3

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ swift_compiler_sources(Optimizer
1818
SimplifyGlobalValue.swift
1919
SimplifyStrongRetainRelease.swift
2020
SimplifyStructExtract.swift
21+
SimplifyTupleExtract.swift
2122
SimplifyUncheckedEnumData.swift)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===--- SimplifyTupleExtract.swift ---------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SIL
14+
15+
extension TupleExtractInst : OnoneSimplifyable {
16+
func simplify(_ context: SimplifyContext) {
17+
18+
// Replace tuple_extract(tuple(x)) -> x
19+
20+
guard let tupleInst = tuple as? TupleInst else {
21+
return
22+
}
23+
context.tryReplaceRedundantInstructionPair(first: tupleInst, second: self,
24+
with: tupleInst.operands[fieldIndex].value)
25+
}
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification -simplify-instruction=tuple_extract | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
5+
import Swift
6+
import Builtin
7+
8+
// CHECK-LABEL: sil @tuple_extract_first_field
9+
// CHECK: return %0
10+
// CHECK: } // end sil function 'tuple_extract_first_field'
11+
sil @tuple_extract_first_field : $@convention(thin) (@owned String, @guaranteed String) -> @owned String {
12+
bb0(%0 : $String, %1 : $String):
13+
%2 = tuple (%0 : $String, %1 : $String)
14+
%3 = tuple_extract %2 : $(String, String), 0
15+
return %3 : $String
16+
}
17+
18+
// CHECK-LABEL: sil @tuple_extract_second_field
19+
// CHECK: return %1
20+
// CHECK: } // end sil function 'tuple_extract_second_field'
21+
sil @tuple_extract_second_field : $@convention(thin) (@guaranteed String, @owned String) -> @owned String {
22+
bb0(%0 : $String, %1 : $String):
23+
%2 = tuple (%0 : $String, %1 : $String)
24+
%3 = tuple_extract %2 : $(String, String), 1
25+
return %3 : $String
26+
}
27+
28+
// CHECK-LABEL: sil [ossa] @tuple_extract_ossa
29+
// CHECK: %2 = copy_value %0
30+
// CHECK: return %2
31+
// CHECK: } // end sil function 'tuple_extract_ossa'
32+
sil [ossa] @tuple_extract_ossa : $@convention(thin) (@guaranteed String, @guaranteed String) -> @owned String {
33+
bb0(%0 : @guaranteed $String, %1 : @guaranteed $String):
34+
%2 = tuple (%0 : $String, %1 : $String)
35+
%3 = tuple_extract %2 : $(String, String), 0
36+
%4 = copy_value %3 : $String
37+
return %4 : $String
38+
}
39+

0 commit comments

Comments
 (0)