https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102556
Bug ID: 102556
Summary: equality comparison of a [static N] parameter to null
not folded
Product: gcc
Version: 12.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: ---
The C99 [static N] array notation in a function parameter indicates that the
caller must provide as an argument an array with at least N element.
Therefore, in the body of the function, the parameter may be assumed to be
nonnull, the same way as if it had been declared with attribute nonnull.
The test case below shows that GCC fails to take advantage of this to fold
pointless comparisons of such parameters to null, even though it does make use
of the equivalent guarantee provided by the attribute.
In contrast, Clang folds the expression in both functions to false.
$ cat z.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout z.c
__attribute__ ((nonnull)) int f (int *a)
{
return a == 0; // folded to false with a warning (good)
}
int g (int a[static 1])
{
return a == 0; // not folded, missing warning
}
z.c: In function ‘f’:
z.c:3:12: warning: ‘nonnull’ argument ‘a’ compared to NULL [-Wnonnull-compare]
3 | return a == 0; // folded to false with a warning (good)
| ~~^~~~
;; Function f (f, funcdef_no=0, decl_uid=1978, cgraph_uid=1, symbol_order=0)
__attribute__((nonnull))
int f (int * a)
{
<bb 2> [local count: 1073741824]:
return 0;
}
;; Function g (g, funcdef_no=1, decl_uid=1981, cgraph_uid=2, symbol_order=1)
__attribute__((access ("^0[s1]", )))
int g (int * a)
{
_Bool _1;
int _3;
<bb 2> [local count: 1073741824]:
_1 = a_2(D) == 0B;
_3 = (int) _1;
return _3;
}