@@ -87,7 +87,10 @@ private func optimizeObjectAllocation(allocRef: AllocRefInstBase, _ context: Fun
87
87
return nil
88
88
}
89
89
90
- guard let ( storesToClassFields, storesToTailElements) = getInitialization ( of: allocRef, ignore: endOfInitInst) else {
90
+ guard let ( storesToClassFields, storesToTailElements) = getInitialization ( of: allocRef,
91
+ ignore: endOfInitInst,
92
+ context) else
93
+ {
91
94
return nil
92
95
}
93
96
@@ -136,7 +139,8 @@ private func findEndOfInitialization(of object: Value, canStoreToGlobal: Bool) -
136
139
return nil
137
140
}
138
141
139
- private func getInitialization( of allocRef: AllocRefInstBase , ignore ignoreInst: Instruction )
142
+ private func getInitialization( of allocRef: AllocRefInstBase , ignore ignoreInst: Instruction ,
143
+ _ context: FunctionPassContext )
140
144
-> ( storesToClassFields: [ StoreInst ] , storesToTailElements: [ StoreInst ] ) ?
141
145
{
142
146
guard let numTailElements = allocRef. numTailElements else {
@@ -154,7 +158,7 @@ private func getInitialization(of allocRef: AllocRefInstBase, ignore ignoreInst:
154
158
let tailCount = numTailElements != 0 ? numTailElements * allocRef. numStoresPerTailElement : 0
155
159
var tailStores = Array < StoreInst ? > ( repeating: nil , count: tailCount)
156
160
157
- if !findInitStores( of: allocRef, & fieldStores, & tailStores, ignore: ignoreInst) {
161
+ if !findInitStores( of: allocRef, & fieldStores, & tailStores, ignore: ignoreInst, context ) {
158
162
return nil
159
163
}
160
164
@@ -168,7 +172,9 @@ private func getInitialization(of allocRef: AllocRefInstBase, ignore ignoreInst:
168
172
private func findInitStores( of object: Value ,
169
173
_ fieldStores: inout [ StoreInst ? ] ,
170
174
_ tailStores: inout [ StoreInst ? ] ,
171
- ignore ignoreInst: Instruction ) -> Bool {
175
+ ignore ignoreInst: Instruction ,
176
+ _ context: FunctionPassContext ) -> Bool
177
+ {
172
178
for use in object. uses {
173
179
let user = use. instruction
174
180
switch user {
@@ -177,15 +183,15 @@ private func findInitStores(of object: Value,
177
183
is MoveValueInst ,
178
184
is EndInitLetRefInst ,
179
185
is BeginBorrowInst :
180
- if !findInitStores( of: user as! SingleValueInstruction , & fieldStores, & tailStores, ignore: ignoreInst) {
186
+ if !findInitStores( of: user as! SingleValueInstruction , & fieldStores, & tailStores, ignore: ignoreInst, context ) {
181
187
return false
182
188
}
183
189
case let rea as RefElementAddrInst :
184
- if !findStores( inUsesOf: rea, index: rea. fieldIndex, stores: & fieldStores) {
190
+ if !findStores( inUsesOf: rea, index: rea. fieldIndex, stores: & fieldStores, context ) {
185
191
return false
186
192
}
187
193
case let rta as RefTailAddrInst :
188
- if !findStores( toTailAddress: rta, tailElementIndex: 0 , stores: & tailStores) {
194
+ if !findStores( toTailAddress: rta, tailElementIndex: 0 , stores: & tailStores, context ) {
189
195
return false
190
196
}
191
197
case ignoreInst,
@@ -200,7 +206,8 @@ private func findInitStores(of object: Value,
200
206
return true
201
207
}
202
208
203
- private func findStores( toTailAddress tailAddr: Value , tailElementIndex: Int , stores: inout [ StoreInst ? ] ) -> Bool {
209
+ private func findStores( toTailAddress tailAddr: Value , tailElementIndex: Int , stores: inout [ StoreInst ? ] ,
210
+ _ context: FunctionPassContext ) -> Bool {
204
211
for use in tailAddr. uses {
205
212
switch use. instruction {
206
213
case let indexAddr as IndexAddrInst :
@@ -209,26 +216,26 @@ private func findStores(toTailAddress tailAddr: Value, tailElementIndex: Int, st
209
216
{
210
217
return false
211
218
}
212
- if !findStores( toTailAddress: indexAddr, tailElementIndex: tailElementIndex + tailIdx, stores: & stores) {
219
+ if !findStores( toTailAddress: indexAddr, tailElementIndex: tailElementIndex + tailIdx, stores: & stores, context ) {
213
220
return false
214
221
}
215
222
case let tea as TupleElementAddrInst :
216
223
// The tail elements are tuples. There is a separate store for each tuple element.
217
224
let numTupleElements = tea. tuple. type. tupleElements. count
218
225
let tupleIdx = tea. fieldIndex
219
- if !findStores( inUsesOf: tea, index: tailElementIndex * numTupleElements + tupleIdx, stores: & stores) {
226
+ if !findStores( inUsesOf: tea, index: tailElementIndex * numTupleElements + tupleIdx, stores: & stores, context ) {
220
227
return false
221
228
}
222
229
case let atp as AddressToPointerInst :
223
- if !findStores( toTailAddress: atp, tailElementIndex: tailElementIndex, stores: & stores) {
230
+ if !findStores( toTailAddress: atp, tailElementIndex: tailElementIndex, stores: & stores, context ) {
224
231
return false
225
232
}
226
233
case let mdi as MarkDependenceInst :
227
- if !findStores( toTailAddress: mdi, tailElementIndex: tailElementIndex, stores: & stores) {
234
+ if !findStores( toTailAddress: mdi, tailElementIndex: tailElementIndex, stores: & stores, context ) {
228
235
return false
229
236
}
230
237
case let pta as PointerToAddressInst :
231
- if !findStores( toTailAddress: pta, tailElementIndex: tailElementIndex, stores: & stores) {
238
+ if !findStores( toTailAddress: pta, tailElementIndex: tailElementIndex, stores: & stores, context ) {
232
239
return false
233
240
}
234
241
case let store as StoreInst :
@@ -237,7 +244,7 @@ private func findStores(toTailAddress tailAddr: Value, tailElementIndex: Int, st
237
244
// Just to be on the safe side..
238
245
return false
239
246
}
240
- if !handleStore( store, index: tailElementIndex, stores: & stores) {
247
+ if !handleStore( store, index: tailElementIndex, stores: & stores, context ) {
241
248
return false
242
249
}
243
250
default :
@@ -249,10 +256,12 @@ private func findStores(toTailAddress tailAddr: Value, tailElementIndex: Int, st
249
256
return true
250
257
}
251
258
252
- private func findStores( inUsesOf address: Value , index: Int , stores: inout [ StoreInst ? ] ) -> Bool {
259
+ private func findStores( inUsesOf address: Value , index: Int , stores: inout [ StoreInst ? ] ,
260
+ _ context: FunctionPassContext ) -> Bool
261
+ {
253
262
for use in address. uses {
254
263
if let store = use. instruction as? StoreInst {
255
- if !handleStore( store, index: index, stores: & stores) {
264
+ if !handleStore( store, index: index, stores: & stores, context ) {
256
265
return false
257
266
}
258
267
} else if !isValidUseOfObject( use) {
@@ -262,9 +271,11 @@ private func findStores(inUsesOf address: Value, index: Int, stores: inout [Stor
262
271
return true
263
272
}
264
273
265
- private func handleStore( _ store: StoreInst , index: Int , stores: inout [ StoreInst ? ] ) -> Bool {
274
+ private func handleStore( _ store: StoreInst , index: Int , stores: inout [ StoreInst ? ] ,
275
+ _ context: FunctionPassContext ) -> Bool
276
+ {
266
277
if index >= 0 && index < stores. count,
267
- store. source. isValidGlobalInitValue,
278
+ store. source. isValidGlobalInitValue ( context ) ,
268
279
stores [ index] == nil {
269
280
stores [ index] = store
270
281
return true
0 commit comments