https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117370
--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jan Hubicka <hubi...@gcc.gnu.org>: https://gcc.gnu.org/g:7828dc070510f8f5c06765858815fa6e5d2d1ec6 commit r15-5255-g7828dc070510f8f5c06765858815fa6e5d2d1ec6 Author: Jan Hubicka <hubi...@ucw.cz> Date: Thu Nov 14 17:01:12 2024 +0100 Remove allocations which are used only for NULL pointer check and free Extend tree-ssa-dse to remove memory allocations that are used only to check that return value is non-NULL and freed. New -fmalloc-dce flag can be used to control malloc/free removal. I ended up copying what -fallocation-dse does so -fmalloc-dce=1 enables malloc/free removal provided return value is unused otherwise and -fmalloc-dce=2 allows additional NULL pointer checks which it folds to non-NULL direction. I also added compensation for the gcc.dg/analyzer/pr101837.c testcase and added testcase that std::nothrow variant of operator new is now optimized way. With the -fmalloc-dce=n I can also add a level which emits runtime check for half of address space and calloc overflow if it seems useful, but perhaps incrementally. Adding size parameter tracking is not that hard (I posted WIP patch for that). gcc/ChangeLog: PR tree-optimization/117370 * common.opt: Add -fmalloc-dce. * common.opt.urls: Update. * doc/invoke.texi: Document it; also add missing -flifetime-dse entry. * tree-ssa-dce.cc (is_removable_allocation_p): Break out from ... (mark_stmt_if_obviously_necessary): ... here; also check that operator new satisfies gimple_call_from_new_or_delete. (checks_return_value_of_removable_allocation_p): New Function. (mark_all_reaching_defs_necessary_1): add missing case for STRDUP and STRNDUP (propagate_necessity): Use is_removable_allocation_p and checks_return_value_of_removable_allocation_p. (eliminate_unnecessary_stmts): Update conditionals that use removed allocation; use is_removable_allocation_p. gcc/testsuite/ChangeLog: * g++.dg/cdce3.C: Disable allocation dce. * g++.dg/tree-ssa/pr19476-1.C: Likewise. * g++.dg/tree-ssa/pr19476-2.C: Likewise. * g++.dg/tree-ssa/pr19476-3.C: Likewise. * g++.dg/tree-ssa/pr19476-4.C: Likewise. * gcc.dg/analyzer/pr101837.c: Disable malloc dce. * gcc.dg/tree-ssa/pr19831-3.c: Update. * gfortran.dg/pr68078.f90: Disable malloc DCE.