Fix incorrect optimization of conditional dynamic allocation in PTA
Corrects a bug where DSE incorrectly removed memcpy calls for dynamically allocated memory returned from functions.
A regression in the Pointer Tangling Analysis (PTA) pass incorrectly eliminates calls to memcpy when the destination is dynamically allocated and the allocation is conditional. This fix ensures that the analysis correctly handles cases where the dynamic allocation’s result is returned from a function, preventing premature optimization that could violate program semantics.
In Details
In the Pointer Tangling Analysis (PTA) pass, specifically within tree-ssa-structalias.cc, a bug in set_uids_in_ptset caused memcpy calls to be incorrectly eliminated. This occurred when the destination of memcpy originated from a conditional dynamic allocation (e.g., malloc) and this value was ultimately returned from the function. The fix ensures that ANYTHING in the ESCAPED_RETURN solution is treated correctly, mirroring the handling of explicit heap variables, thus preserving the integrity of aliasing information.
- Pointer Tangling Analysis (PTA)
- A static analysis technique in compilers used to determine the possible memory locations that a pointer can refer to. This information is vital for many optimizations, especially those involving memory access and aliasing.
- DSE (Dead Store Elimination)
- A compiler optimization pass that removes assignments to variables whose values are never used later in the program. In this context, it incorrectly eliminated a
memcpycall. - memcpy
- A standard C library function used to copy a block of memory from one location to another. It's often a target for compiler optimizations.
- dynamic allocation
- Memory allocation performed at runtime, typically using functions like
malloc,calloc, orrealloc. This memory is distinct from statically or automatically allocated memory. - tree-ssa-structalias.cc
- A GCC source file containing parts of the Pointer Tangling Analysis (PTA) and related alias analysis optimizations, operating on the Static Single Assignment (SSA) form of the code.
- ESCAPED_RETURN
- A representation within PTA indicating that a pointer or value returned from a function might escape, meaning it could be accessed or modified by code outside the function's immediate scope. 'ANYTHING' is a broad placeholder for such escaped values.