https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87313
Bug ID: 87313 Summary: attribute malloc not used for alias analysis when it could be Product: gcc Version: 9.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: --- Attribute malloc is documented as: This tells the compiler that a function is malloc-like, i.e., that the pointer P returned by the function cannot alias any other pointer valid when the function returns, and moreover no pointers to valid objects occur in any storage addressed by P. The test case below shows that although GCC takes advantage of this property to eliminate impossible tests when calling __builtin_malloc it doesn't do the same when calling a user-defined function declared with the attribute. $ cat x.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout x.c void f (int **p) { int *x = *p; int **q = __builtin_malloc (sizeof (int*)); *q = 0; // *q cannot be equal to *p prior to the assignment if (x != *p) // folded to false __builtin_abort (); } __attribute__ ((malloc)) void* g (int); void h (int **p) { int *x = *p; int **q = g (sizeof (int*)); *q = 0; // *q cannot be equal to *p prior to the assignment if (x != *p) // not folded __builtin_abort (); } ;; Function f (f, funcdef_no=0, decl_uid=1906, cgraph_uid=1, symbol_order=0) f (int * * p) { <bb 2> [local count: 1073741824]: return; } ;; Function h (h, funcdef_no=1, decl_uid=1913, cgraph_uid=2, symbol_order=1) h (int * * p) { int * x; int * _1; <bb 2> [local count: 1073741824]: x_4 = *p_3(D); g (8); _1 = *p_3(D); if (_1 != x_4) goto <bb 3>; [0.00%] else goto <bb 4>; [99.96%] <bb 3> [count: 0]: __builtin_abort (); <bb 4> [local count: 1073312328]: return; }