The current capture tracking implementation always considers a store instruction to capture the given use. The example below shows a situation where this is overly pessimistic. The current implementation considers %ptr to be captured because it's the operand of the second store.
define void @sample() { entry: %ptr = alloca i32 store i32 1, i32* %ptr %ptrtoptr = alloca i32* store i32* %ptr, i32** %ptrtoptr ret void }
This patch adds a new capture tracker implementation called OptimisticCaptureTracker. It can be used through a slightly modified interface that expects alias analysis results as an additional argument. If storeCaptures is set to true or no AA is provided, the OptimisticCaptureTracker behaves exactly as the current SimpleCaptureTracker. Otherwise, it only consider %ptr to escape in the previous example if it is stored to either a global or to one of the arguments of the function.
An Argument is always a Value, so this cast/check should never be necessary.