mirror of
https://github.com/apple/swift.git
synced 2026-10-08 12:50:50 +02:00
DeadObjectElimination eliminates dead `alloc_stack` and `alloc_ref` allocations. **Stack allocations** A stack allocation is dead if its stored values never escape and all loads can be forwarded directly to the stored values: ``` %s = alloc_stack $Foo store %v to [init] %s : $*Foo %l = load [take] %s : $*Foo dealloc_stack %s : $*Foo ``` -> ``` // %l is replaced with %v ``` This also handles field-level stores and loads. Destroy instructions for field values whose containing object is destroyed replace the original aggregate destroy: ``` %s = alloc_stack $Foo %fa = struct_element_addr %s : $*Foo, #Foo.x store %x to [init] %fa : $*Int destroy_addr %s : $*Foo dealloc_stack %s : $*Foo ``` -> ``` destroy_value %x : $Int ``` **Heap allocations** A heap allocation is dead if it never escapes and its destructor has no observable side effects. Stored field values are destroyed in place of the object destructor: ``` %obj = alloc_ref $Foo %addr = ref_element_addr %obj : $Foo, #Foo.x store %x to [init] %addr : $*Int destroy_value %obj : $Foo ``` -> ``` destroy_value %x : $Int ``` The pass replaces the old DeadObjectElimination and SILMem2Reg. It combines the advantages of both passes: * The old DeadObjectElimination can only remove an `alloc_stack` or `alloc_ref` if there are no loads from such allocations. * SILMem2Reg can handle loads, but can only remove `alloc_stack`s and not `alloc_ref`s. The new pass can handle loads from both, `alloc_stack` and `alloc_ref`. This means, for example, it can do array element propagation (an array buffer is an `alloc_ref) and, if there are no other uses of the array, completely eliminate the array. Also, the new pass has other improvements compared to the old passes. E.g. it can deal with objects where the individual fields are initialized with separate stores. This change also adds the pass variant MandatoryDeadObjectElimination. It replaces the old PredictableDeadAllocationElimination pass.