@@ -87,7 +87,10 @@ private func optimizeObjectAllocation(allocRef: AllocRefInstBase, _ context: Fun
8787 return nil
8888 }
8989
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+ {
9194 return nil
9295 }
9396
@@ -136,7 +139,8 @@ private func findEndOfInitialization(of object: Value, canStoreToGlobal: Bool) -
136139 return nil
137140}
138141
139- private func getInitialization( of allocRef: AllocRefInstBase , ignore ignoreInst: Instruction )
142+ private func getInitialization( of allocRef: AllocRefInstBase , ignore ignoreInst: Instruction ,
143+ _ context: FunctionPassContext )
140144 -> ( storesToClassFields: [ StoreInst ] , storesToTailElements: [ StoreInst ] ) ?
141145{
142146 guard let numTailElements = allocRef. numTailElements else {
@@ -154,7 +158,7 @@ private func getInitialization(of allocRef: AllocRefInstBase, ignore ignoreInst:
154158 let tailCount = numTailElements != 0 ? numTailElements * allocRef. numStoresPerTailElement : 0
155159 var tailStores = Array < StoreInst ? > ( repeating: nil , count: tailCount)
156160
157- if !findInitStores( of: allocRef, & fieldStores, & tailStores, ignore: ignoreInst) {
161+ if !findInitStores( of: allocRef, & fieldStores, & tailStores, ignore: ignoreInst, context ) {
158162 return nil
159163 }
160164
@@ -168,7 +172,9 @@ private func getInitialization(of allocRef: AllocRefInstBase, ignore ignoreInst:
168172private func findInitStores( of object: Value ,
169173 _ fieldStores: inout [ StoreInst ? ] ,
170174 _ tailStores: inout [ StoreInst ? ] ,
171- ignore ignoreInst: Instruction ) -> Bool {
175+ ignore ignoreInst: Instruction ,
176+ _ context: FunctionPassContext ) -> Bool
177+ {
172178 for use in object. uses {
173179 let user = use. instruction
174180 switch user {
@@ -177,15 +183,15 @@ private func findInitStores(of object: Value,
177183 is MoveValueInst ,
178184 is EndInitLetRefInst ,
179185 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 ) {
181187 return false
182188 }
183189 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 ) {
185191 return false
186192 }
187193 case let rta as RefTailAddrInst :
188- if !findStores( toTailAddress: rta, tailElementIndex: 0 , stores: & tailStores) {
194+ if !findStores( toTailAddress: rta, tailElementIndex: 0 , stores: & tailStores, context ) {
189195 return false
190196 }
191197 case ignoreInst,
@@ -200,7 +206,8 @@ private func findInitStores(of object: Value,
200206 return true
201207}
202208
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 {
204211 for use in tailAddr. uses {
205212 switch use. instruction {
206213 case let indexAddr as IndexAddrInst :
@@ -209,26 +216,26 @@ private func findStores(toTailAddress tailAddr: Value, tailElementIndex: Int, st
209216 {
210217 return false
211218 }
212- if !findStores( toTailAddress: indexAddr, tailElementIndex: tailElementIndex + tailIdx, stores: & stores) {
219+ if !findStores( toTailAddress: indexAddr, tailElementIndex: tailElementIndex + tailIdx, stores: & stores, context ) {
213220 return false
214221 }
215222 case let tea as TupleElementAddrInst :
216223 // The tail elements are tuples. There is a separate store for each tuple element.
217224 let numTupleElements = tea. tuple. type. tupleElements. count
218225 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 ) {
220227 return false
221228 }
222229 case let atp as AddressToPointerInst :
223- if !findStores( toTailAddress: atp, tailElementIndex: tailElementIndex, stores: & stores) {
230+ if !findStores( toTailAddress: atp, tailElementIndex: tailElementIndex, stores: & stores, context ) {
224231 return false
225232 }
226233 case let mdi as MarkDependenceInst :
227- if !findStores( toTailAddress: mdi, tailElementIndex: tailElementIndex, stores: & stores) {
234+ if !findStores( toTailAddress: mdi, tailElementIndex: tailElementIndex, stores: & stores, context ) {
228235 return false
229236 }
230237 case let pta as PointerToAddressInst :
231- if !findStores( toTailAddress: pta, tailElementIndex: tailElementIndex, stores: & stores) {
238+ if !findStores( toTailAddress: pta, tailElementIndex: tailElementIndex, stores: & stores, context ) {
232239 return false
233240 }
234241 case let store as StoreInst :
@@ -237,7 +244,7 @@ private func findStores(toTailAddress tailAddr: Value, tailElementIndex: Int, st
237244 // Just to be on the safe side..
238245 return false
239246 }
240- if !handleStore( store, index: tailElementIndex, stores: & stores) {
247+ if !handleStore( store, index: tailElementIndex, stores: & stores, context ) {
241248 return false
242249 }
243250 default :
@@ -249,10 +256,12 @@ private func findStores(toTailAddress tailAddr: Value, tailElementIndex: Int, st
249256 return true
250257}
251258
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+ {
253262 for use in address. uses {
254263 if let store = use. instruction as? StoreInst {
255- if !handleStore( store, index: index, stores: & stores) {
264+ if !handleStore( store, index: index, stores: & stores, context ) {
256265 return false
257266 }
258267 } else if !isValidUseOfObject( use) {
@@ -262,9 +271,11 @@ private func findStores(inUsesOf address: Value, index: Int, stores: inout [Stor
262271 return true
263272}
264273
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+ {
266277 if index >= 0 && index < stores. count,
267- store. source. isValidGlobalInitValue,
278+ store. source. isValidGlobalInitValue ( context ) ,
268279 stores [ index] == nil {
269280 stores [ index] = store
270281 return true
0 commit comments