GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
tree-optimization Performance Win

tree-optimization: Remove unused allocations compared against NULL

Optimizes C++ code by removing unnecessary memory allocations that are only checked for NULL, improving generated code.

This change enhances the tree-SSA dead code elimination pass to remove memory allocations whose returned pointers are only used in comparisons with NULL. Previously, such allocations were not removed if the comparison result was assigned to a variable. The update extends the optimization to handle assignments involving ‘ptr == NULL’ or ‘ptr != NULL’ checks, correctly folding them to constants and eliminating the dead allocation and deallocation code, similar to how conditional checks were already handled.

In Details

Extends tree-ssa-dce's ability to eliminate operator new/delete pairs where the allocation result is only checked against NULL. A prior change handled cases where the comparison controlled a branch. This commit also removes the allocation when the comparison result is assigned, e.g., return ptr != 0;. It modifies checks_return_value_of_removable_allocation_p to handle gassign to gcond and ensures eliminate_unnecessary_stmts folds such comparisons to constants. A typo in the fmalloc-dce option name is also corrected.

For Context
tree-ssa-dce
The Dead Code Elimination pass within GCC's SSA (Static Single Assignment) based optimization framework, responsible for removing code that does not affect the program's output.
SSA
Static Single Assignment form, an intermediate representation where every variable is assigned exactly once. This form simplifies many data-flow analyses and optimizations.
gimple
GCC's intermediate representation for statements, designed to be simpler and more amenable to optimization than RTL.
operator new
A C++ operator used to allocate memory dynamically on the heap. std::nothrow version returns nullptr on failure instead of throwing an exception.
operator delete
A C++ operator used to deallocate memory previously allocated with operator new.
Filed Under: optimizationC++memory managementdead code elimination