https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97605
Bug ID: 97605 Summary: unused conditionally freed allocation not eliminated Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- In the test program below GCC eliminates the unnecessary pair of malloc/free in f() but it doesn't perform the likely much more impactful optimization in g() whose body could be transformed into: void* g (int i) { if (i) return 0; return malloc (1); } $ cat q.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout q.c typedef __SIZE_TYPE__ size_t; extern void free (void*); extern void* malloc (size_t); void f (int i) { void *p = malloc (1); // eliminated free (p); // ditto } void* g (int i) { void *p = malloc (1); if (i) { free (p); return 0; } return p; } ;; Function f (f, funcdef_no=0, decl_uid=1936, cgraph_uid=1, symbol_order=0) f (int i) { <bb 2> [local count: 1073741824]: return; } ;; Function g (g, funcdef_no=1, decl_uid=1940, cgraph_uid=2, symbol_order=1) Removing basic block 5 g (int i) { void * p; void * _1; <bb 2> [local count: 1073741824]: p_5 = malloc (1); if (i_6(D) != 0) goto <bb 3>; [9.39%] else goto <bb 4>; [90.61%] <bb 3> [local count: 100824360]: free (p_5); <bb 4> [local count: 1073741824]: # _1 = PHI <0B(3), p_5(2)> return _1; }