Introduce a pass to remove redundant copy statements from the IR.
func() { %source = alloc() write_to(%source) %destination = alloc() copy(%source, %destination) dealloc(%source) return %destination }
Output:
func(){ %source = alloc() write_to(%source) return %source }
In the above case we can remove the copy op, and replace all uses of
destination with source, and other redundant allocations and
deallocations subject to the following constraints.
Constraints:
- There should not exist any users of destination before the copy op.
- There should not be any write operations on source and destination after copy op.
Also, we can Remove copy op when there are no uses of destination after the copy op. In this case, we only remove the copy op, and we CANNOT replace all uses of source with destination. For example,
%source = alloc() %destination = alloc() write_to(%destination) copy(%source, %destination) dealloc(%source) dealloc(%destination)
Output:
%source = alloc() %destination = alloc() write_to(%destination) dealloc(%source) dealloc(%destination)