https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81679
Bug ID: 81679 Summary: use attribute unused on function arguments as an optimization hint Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- This is a proposal for an enhancement to attribute unused in two ways: 1) When attribute unused is specified on a function argument of pointer type in a declaration of a function, GCC could use that as an indication that the argument is, in fact, not used by the implementation of the function and assume that the object the pointer points to is unchanged by the function call. 2) In addition, the attribute could also be used to issue a -Wunused-variable warning when the variable whose address is passed to a function decorated with it is otherwise unused. The test case below shows examples where the possible improvements could be made: $ cat b.c && gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout b.c void f (void* __attribute__ ((unused))); void g (void) { int i = 1; // issue -Wunused-variable here f (&i); } void h (void) { int i = 1; f (&i); if (i != 1) // assume this can not be true __builtin_abort (); } ;; Function g (g, funcdef_no=0, decl_uid=1817, cgraph_uid=0, symbol_order=0) g () { int i; <bb 2> [100.00%] [count: INV]: i = 1; f (&i); i ={v} {CLOBBER}; return; } ;; Function h (h, funcdef_no=1, decl_uid=1821, cgraph_uid=1, symbol_order=1) h () { int i; int i.0_1; <bb 2> [100.00%] [count: INV]: i = 1; f (&i); i.0_1 = i; if (i.0_1 != 1) goto <bb 3>; [0.04%] [count: 0] else goto <bb 4>; [99.96%] [count: INV] <bb 3> [0.04%] [count: 0]: __builtin_abort (); <bb 4> [99.96%] [count: INV]: i ={v} {CLOBBER}; return; }