[Bug middle-end/88401] New: -Wshift-overflow only works for const variables

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[Bug middle-end/88401] New: -Wshift-overflow only works for const variables

law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88401

            Bug ID: 88401
           Summary: -Wshift-overflow only works for const variables
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: skvadrik at gmail dot com
  Target Milestone: ---

Consider this program (1.cc):


#include <stdint.h>

void foo ()
{
  int32_t x = -32768;
  x << 31;

  const int32_t y = -32768;
  y << 31;
}


GCC detects shift overflow on y, but not on x:

$ g++ -c 1.cc -Wshift-overflow=2 --std=c++11 -O3
1.cc: In function ‘void foo()’:
1.cc:9:5: warning: result of ‘(-32768 << 31)’ requires 47 bits to represent,
but ‘int’ only has 32 bits [-Wshift-overflow=]
   y << 31;
   ~~^~~~~


It seems that very basic constant propagation would deduce x = -32768.
Reply | Threaded
Open this post in threaded view
|

[Bug middle-end/88401] -Wshift-overflow only works for const variables

law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88401

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
It is a front-end warning, so there is no constant propagation possible.
You can use -fsanitize=shift to detect this stuff at runtime.
Reply | Threaded
Open this post in threaded view
|

[Bug middle-end/88401] -Wshift-overflow only works for const variables

law at redhat dot com
In reply to this post by law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88401

--- Comment #2 from Ulya <skvadrik at gmail dot com> ---
(In reply to Jakub Jelinek from comment #1)
> It is a front-end warning, so there is no constant propagation possible.
> You can use -fsanitize=shift to detect this stuff at runtime.

Ok, understood. Maybe someday it can be moved to middle-end.
Reply | Threaded
Open this post in threaded view
|

[Bug middle-end/88401] -Wshift-overflow only works for const variables

law at redhat dot com
In reply to this post by law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88401

Ulya <skvadrik at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Ulya <skvadrik at gmail dot com> ---
The warning needs to be re-implemented in middle-end to be able to use constant
propagation.
Reply | Threaded
Open this post in threaded view
|

[Bug middle-end/88401] -Wshift-overflow only works for const variables

law at redhat dot com
In reply to this post by law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88401

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
That is not that easy, because what is considered invalid heavily depends on
the FE (and standard version), e.g. the above is completely valid in C++20.

Furthermore, warning too late in the middle-end could very well mean warning
about stuff that is propagated into statements after jump threading etc. and
that are dead and not something user actually wrote.

As I said, if you want accurate diagnostics, use the runtime one.
Reply | Threaded
Open this post in threaded view
|

[Bug middle-end/88401] -Wshift-overflow only works for const variables

law at redhat dot com
In reply to this post by law at redhat dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88401

--- Comment #5 from Ulya <skvadrik at gmail dot com> ---
(In reply to Jakub Jelinek from comment #4)

Right, I see.
Bugzilla forced me to add the previous comment when I changed the status. ;)