https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101074
Bug ID: 101074 Summary: calloc result not treated as zeroed out Product: gcc Version: 11.1.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: --- GCC folds malloc + bzero (or malloc + memset zero) to calloc and "knows" that the returned memory is zeroed out. But it doesn't seem to understand that the memory returned from a call specifically made to calloc is also zeroed out. In the test case below both functions should be optimized equivalently but only the first produces optimal code, the second is suboptimal. $ cat z.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout z.c struct A { int i; }; void* f (void) { struct A *p = __builtin_malloc (sizeof *p); __builtin_memset (p, 0, sizeof *p); if (p->i) // folded to false __builtin_abort (); return p; } void* g (void) { struct A *p = __builtin_calloc (1, sizeof *p); if (p->i) // not folded __builtin_abort (); return p; } ;; Function f (f, funcdef_no=0, decl_uid=1945, cgraph_uid=1, symbol_order=0) void * f () { struct A * p; <bb 2> [local count: 1073741824]: p_3 = __builtin_calloc (4, 1); [tail call] return p_3; } ;; Function g (g, funcdef_no=1, decl_uid=1949, cgraph_uid=2, symbol_order=1) void * g () { struct A * p; int _1; <bb 2> [local count: 1073741824]: p_4 = __builtin_calloc (1, 4); _1 = p_4->i; if (_1 != 0) goto <bb 3>; [0.00%] else goto <bb 4>; [100.00%] <bb 3> [count: 0]: __builtin_abort (); <bb 4> [local count: 1073741824]: return p_4; }