@@ -15,6 +15,21 @@ import SIL
15
15
extension DestructureTupleInst : OnoneSimplifyable , SILCombineSimplifyable {
16
16
func simplify( _ context: SimplifyContext ) {
17
17
18
+ // If the tuple is trivial, replace
19
+ // ```
20
+ // (%1, %2) = destructure_tuple %t
21
+ // ```
22
+ // ->
23
+ // ```
24
+ // %1 = tuple_extract %t, 0
25
+ // %2 = tuple_extract %t, 1
26
+ // ```
27
+ // This canonicalization helps other optimizations to e.g. CSE tuple_extracts.
28
+ //
29
+ if replaceWithTupleExtract ( context) {
30
+ return
31
+ }
32
+
18
33
// Eliminate the redundant instruction pair
19
34
// ```
20
35
// %t = tuple (%0, %1, %2)
@@ -26,11 +41,39 @@ extension DestructureTupleInst : OnoneSimplifyable, SILCombineSimplifyable {
26
41
tryReplaceConstructDestructPair ( construct: tuple, destruct: self , context)
27
42
}
28
43
}
44
+
45
+ private func replaceWithTupleExtract( _ context: SimplifyContext ) -> Bool {
46
+ guard self . tuple. type. isTrivial ( in: parentFunction) else {
47
+ return false
48
+ }
49
+ let builder = Builder ( before: self , context)
50
+ for (elementIdx, result) in results. enumerated ( ) {
51
+ let elementValue = builder. createTupleExtract ( tuple: self . tuple, elementIndex: elementIdx)
52
+ result. uses. replaceAll ( with: elementValue, context)
53
+ }
54
+ context. erase ( instruction: self )
55
+ return true
56
+ }
29
57
}
30
58
31
59
extension DestructureStructInst : OnoneSimplifyable , SILCombineSimplifyable {
32
60
func simplify( _ context: SimplifyContext ) {
33
61
62
+ // If the struct is trivial, replace
63
+ // ```
64
+ // (%1, %2) = destructure_struct %s
65
+ // ```
66
+ // ->
67
+ // ```
68
+ // %1 = struct_extract %s, #S.field0
69
+ // %2 = struct_extract %s, #S.field1
70
+ // ```
71
+ // This canonicalization helps other optimizations to e.g. CSE tuple_extracts.
72
+ //
73
+ if replaceWithStructExtract ( context) {
74
+ return
75
+ }
76
+
34
77
switch self . struct {
35
78
case let str as StructInst :
36
79
// Eliminate the redundant instruction pair
@@ -82,6 +125,19 @@ extension DestructureStructInst : OnoneSimplifyable, SILCombineSimplifyable {
82
125
break
83
126
}
84
127
}
128
+
129
+ private func replaceWithStructExtract( _ context: SimplifyContext ) -> Bool {
130
+ guard self . struct. type. isTrivial ( in: parentFunction) else {
131
+ return false
132
+ }
133
+ let builder = Builder ( before: self , context)
134
+ for (fieldIdx, result) in results. enumerated ( ) {
135
+ let fieldValue = builder. createStructExtract ( struct: self . struct, fieldIndex: fieldIdx)
136
+ result. uses. replaceAll ( with: fieldValue, context)
137
+ }
138
+ context. erase ( instruction: self )
139
+ return true
140
+ }
85
141
}
86
142
87
143
private func tryReplaceConstructDestructPair( construct: SingleValueInstruction ,
0 commit comments