public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/103725] New: warning: assuming signed overflow does not occur when simplifying conditional to constant
@ 2021-12-15  4:16 f.heckenbach@fh-soft.de
  2021-12-18  8:49 ` [Bug tree-optimization/103725] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: f.heckenbach@fh-soft.de @ 2021-12-15  4:16 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103725
           Summary: warning: assuming signed overflow does not occur when
                    simplifying conditional to constant
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: f.heckenbach@fh-soft.de
  Target Milestone: ---

% cat test.c  
int d = 0, b = 8, a[8][8];

struct S
{
  int c;
};

void foo (int *c)
{
  if (b >= 0)
    {
      if (*c) d = *c;
      if (*c < 0) d = *c;
    }
}

int main ()
{
  struct S i = { 0 };
  for (int e = 0; e < 11; e++, i.c++)
    {
      d = a[i.c >= b ? i.c - b : i.c][i.c + 1 >= b ? i.c + 1 - b : i.c + 1];
      foo (&i.c);
    }
}
% gcc -O2 -Wstrict-overflow test.c 
test.c: In function 'main':
test.c:22:66: warning: assuming signed overflow does not occur when simplifying
conditional to constant [-Wstrict-overflow]
   22 |       d = a[i.c >= b ? i.c - b : i.c][i.c + 1 >= b ? i.c + 1 - b : i.c
+ 1];
      |                                      
~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~

This is not the same as https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103724 (in
fact, that was a collateral damage while bisecting this one), though they may
be internally related since this warning also depends only many circumstances,
plus a few more (like, there must be a struct, a plain integer won't cause the
warning; and e and i.c must be distinct although they count in lockstep).

To a user, the warning is worrying since none of the visible conditionals
should be able to be simplified to a constant.

What seems to be happening, as far as I can tell from looking at the generated
code, is that GCC internally splits cases and then simplifies conditions in one
of them (since i.c >= b implies i.c + 1 >= b, unless signed overflow which
seems to explain the cause of the warning).

But the user normally doesn't see this and gets the impression one of their
conditions is wrongly simplified. This, and the fact that the warning doesn't
seem very robust in the first place (i.e. depends on many circumstances), seems
to make the whole "-Wstrict-overflow" option a bit less than useful, IMHO.

I get another spurious warning like this in a more complex piece of my code,
and it also seems to depend on many circumstances, so I can't easily provide a
reproducible example, but it may be a similar situation like this one in the
end. (And these were the only ones I got, i.e. it didn't actually help me find
a real bug in my code.)

Since it also doesn't seem possible to suppress the warning locally with a
pragma (since it's generated at a late stage, I suppose), I think I'll have to
disable it again. (I had re-enabled it after I heard
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70455 was fixed. It may work
better now, but still too mysterious to actually help me.)

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

end of thread, other threads:[~2024-03-11  4:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-15  4:16 [Bug c/103725] New: warning: assuming signed overflow does not occur when simplifying conditional to constant f.heckenbach@fh-soft.de
2021-12-18  8:49 ` [Bug tree-optimization/103725] " pinskia at gcc dot gnu.org
2021-12-18  8:57 ` [Bug tree-optimization/103725] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
2022-01-04 11:26 ` rguenth at gcc dot gnu.org
2022-01-18 14:01 ` rguenth at gcc dot gnu.org
2022-05-27  9:46 ` [Bug tree-optimization/103725] [10/11/12/13 " rguenth at gcc dot gnu.org
2022-06-28 10:47 ` jakub at gcc dot gnu.org
2023-03-10 19:58 ` pinskia at gcc dot gnu.org
2023-07-07 10:41 ` [Bug tree-optimization/103725] [11/12/13/14 " rguenth at gcc dot gnu.org
2024-03-11  4:26 ` [Bug tree-optimization/103725] [11/12 " law at gcc dot gnu.org

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).