public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/102556] New: equality comparison of a [static N] parameter to null not folded
@ 2021-10-01 16:38 msebor at gcc dot gnu.org
  2022-12-17 12:55 ` [Bug tree-optimization/102556] " roman.zilka at gmail dot com
  2023-08-03 21:04 ` muecker at gwdg dot de
  0 siblings, 2 replies; 3+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-10-01 16:38 UTC (permalink / raw)
  To: gcc-bugs

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;

}

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug tree-optimization/102556] equality comparison of a [static N] parameter to null not folded
  2021-10-01 16:38 [Bug tree-optimization/102556] New: equality comparison of a [static N] parameter to null not folded msebor at gcc dot gnu.org
@ 2022-12-17 12:55 ` roman.zilka at gmail dot com
  2023-08-03 21:04 ` muecker at gwdg dot de
  1 sibling, 0 replies; 3+ messages in thread
From: roman.zilka at gmail dot com @ 2022-12-17 12:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102556

Roman Žilka <roman.zilka at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |roman.zilka at gmail dot com

--- Comment #1 from Roman Žilka <roman.zilka at gmail dot com> ---
This'd be helpful.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug tree-optimization/102556] equality comparison of a [static N] parameter to null not folded
  2021-10-01 16:38 [Bug tree-optimization/102556] New: equality comparison of a [static N] parameter to null not folded msebor at gcc dot gnu.org
  2022-12-17 12:55 ` [Bug tree-optimization/102556] " roman.zilka at gmail dot com
@ 2023-08-03 21:04 ` muecker at gwdg dot de
  1 sibling, 0 replies; 3+ messages in thread
From: muecker at gwdg dot de @ 2023-08-03 21:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102556

Martin Uecker <muecker at gwdg dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |muecker at gwdg dot de

--- Comment #2 from Martin Uecker <muecker at gwdg dot de> ---
PATCH

https://gcc.gnu.org/pipermail/gcc-patches/2023-July/625559.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-08-03 21:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-01 16:38 [Bug tree-optimization/102556] New: equality comparison of a [static N] parameter to null not folded msebor at gcc dot gnu.org
2022-12-17 12:55 ` [Bug tree-optimization/102556] " roman.zilka at gmail dot com
2023-08-03 21:04 ` muecker at gwdg dot de

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).