public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/50082] New: -Wstrict-overflow mishandles typedef
@ 2011-08-15  8:01 eggert at gnu dot org
  2011-08-15 11:05 ` [Bug c/50082] " rguenth at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: eggert at gnu dot org @ 2011-08-15  8:01 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50082

             Bug #: 50082
           Summary: -Wstrict-overflow mishandles typedef
    Classification: Unclassified
           Product: gcc
           Version: 4.6.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: eggert@gnu.org


Created attachment 25011
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=25011
Sample program illustrating bogus warning

(I ran into this problem when building a test version of GNU Emacs.)

"gcc -Wstrict-overflow -O2" sometimes issues a bogus warning when an
int is declared via a typedef rather than using a bare 'int' keyword.

Compile the attached file r.c with "gcc -Wstrict-overflow -O2 -S r.c".
There will be a diagnostic:

  r.c: In function 'insert_cache_boundary_bad':
  r.c:19:6: warning: assuming signed overflow does not occur when reducing
constant in comparison [-Wstrict-overflow]

r.c has two functions that are nearly identical.  The only difference
is that 'insert_cache_boundary_ok' uses plain 'int', whereas
'insert_cache_boundary_bad' uses 'foo' which is declared via "typedef
int foo;".  The ..._ok function does not produce the bogus warning,
the ..._bad function does.  (Both functions compile into the same
machine code.)

The diagnostic is bogus, since GCC can clearly deduce that signed
overflow is impossible here: 'i' must be nonnegative at the point of
the complaint, and 'i - 1' cannot possibly overflow.

For reference, here is the output of "gcc -v -save-temps
-Wstrict-overflow -O2 -S r.c".

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/home/eggert/opt/Linux-x86_64/gcc-4.6.1/libexec/gcc/x86_64-unknown-linux-gnu/4.6.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.6.1/configure
--prefix=/home/eggert/opt/Linux-x86_64/gcc-4.6.1 --disable-nls
Thread model: posix
gcc version 4.6.1 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstrict-overflow' '-O2' '-S'
'-mtune=generic' '-march=x86-64'

/home/eggert/opt/Linux-x86_64/gcc-4.6.1/libexec/gcc/x86_64-unknown-linux-gnu/4.6.1/cc1
-E -quiet -v r.c -mtune=generic -march=x86-64 -Wstrict-overflow -O2
-fpch-preprocess -o r.i
ignoring nonexistent directory
"/home/eggert/opt/Linux-x86_64/gcc-4.6.1/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../x86_64-unknown-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:

/home/eggert/opt/Linux-x86_64/gcc-4.6.1/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/include
 /usr/local/include
 /home/eggert/opt/Linux-x86_64/gcc-4.6.1/include

/home/eggert/opt/Linux-x86_64/gcc-4.6.1/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstrict-overflow' '-O2' '-S'
'-mtune=generic' '-march=x86-64'

/home/eggert/opt/Linux-x86_64/gcc-4.6.1/libexec/gcc/x86_64-unknown-linux-gnu/4.6.1/cc1
-fpreprocessed r.i -quiet -dumpbase r.c -mtune=generic -march=x86-64 -auxbase r
-O2 -Wstrict-overflow -version -o r.s
GNU C (GCC) version 4.6.1 (x86_64-unknown-linux-gnu)
    compiled by GNU C version 4.6.1, GMP version 4.3.1, MPFR version 2.4.2, MPC
version 0.8.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C (GCC) version 4.6.1 (x86_64-unknown-linux-gnu)
    compiled by GNU C version 4.6.1, GMP version 4.3.1, MPFR version 2.4.2, MPC
version 0.8.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: b0a4f47793c670c0e36c6193e4fd175b
r.c: In function 'insert_cache_boundary_bad':
r.c:19:6: warning: assuming signed overflow does not occur when reducing
constant in comparison [-Wstrict-overflow]
COMPILER_PATH=/home/eggert/opt/Linux-x86_64/gcc-4.6.1/libexec/gcc/x86_64-unknown-linux-gnu/4.6.1/:/home/eggert/opt/Linux-x86_64/gcc-4.6.1/libexec/gcc/x86_64-unknown-linux-gnu/4.6.1/:/home/eggert/opt/Linux-x86_64/gcc-4.6.1/libexec/gcc/x86_64-unknown-linux-gnu/:/home/eggert/opt/Linux-x86_64/gcc-4.6.1/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/:/home/eggert/opt/Linux-x86_64/gcc-4.6.1/lib/gcc/x86_64-unknown-linux-gnu/
LIBRARY_PATH=/home/eggert/opt/Linux-x86_64/gcc-4.6.1/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/:/home/eggert/opt/Linux-x86_64/gcc-4.6.1/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/home/eggert/opt/Linux-x86_64/gcc-4.6.1/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstrict-overflow' '-O2' '-S'
'-mtune=generic' '-march=x86-64'


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

* [Bug c/50082] -Wstrict-overflow mishandles typedef
  2011-08-15  8:01 [Bug c/50082] New: -Wstrict-overflow mishandles typedef eggert at gnu dot org
@ 2011-08-15 11:05 ` rguenth at gcc dot gnu.org
  2011-08-15 14:48 ` ian at airs dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-08-15 11:05 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50082

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011-08-15
                 CC|                            |iant at google dot com
     Ever Confirmed|0                           |1

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-08-15 11:04:12 UTC ---
> The diagnostic is bogus, since GCC can clearly deduce that signed
> overflow is impossible here: 'i' must be nonnegative at the point of
> the complaint, and 'i - 1' cannot possibly overflow.

well, the warning machinery does not use ranges (ha, surprise!).  And
you probably just fool some coalescing with the typedef which hides the
issue.  Yep, in the int case we fold the comparison to i <= gap_start
very early (and do not warn), while in the typedef case we have

  if ((foo) ((int) i + -1) < gap_start)

so there seem to be some STRIP_NOPS missing during early folding.

Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c    (revision 177757)
+++ gcc/fold-const.c    (working copy)
@@ -8935,7 +8935,7 @@ fold_comparison (location_t loc, enum tr
       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
     }

-  tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
+  tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
   if (tem)
     return tem;

"fixes" it.  Even though we apply overflow knowledge with that folding,
we only warn then with -Wstrict-overflow=5 (as documented).

Ian - it seems the defering of overflow warnings in forwprop and
then undefering does not apply the same restrictions as warning
directly?  Or rather we should in most of the cases pass 0 instead
of some warning-code to fold_undefer_overflow_warnings?  I think you
added that argument to tree-ssa-forwprop.c - do you remember why?

I'm testing the above patch anyway.


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

* [Bug c/50082] -Wstrict-overflow mishandles typedef
  2011-08-15  8:01 [Bug c/50082] New: -Wstrict-overflow mishandles typedef eggert at gnu dot org
  2011-08-15 11:05 ` [Bug c/50082] " rguenth at gcc dot gnu.org
@ 2011-08-15 14:48 ` ian at airs dot com
  2011-08-15 15:09 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ian at airs dot com @ 2011-08-15 14:48 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50082

Ian Lance Taylor <ian at airs dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ian at airs dot com

--- Comment #2 from Ian Lance Taylor <ian at airs dot com> 2011-08-15 14:25:55 UTC ---
Not sure I understand your question about tree-ssa-forwprop.c.  Are you saying
that overflow warnings are already deferred at that point, so that passing
WARN_STRICT_OVERFLOW_CONDITIONAL causes extraneous warnings to be issued?  Are
overflow warnings already deferred during tree-ssa-forwprop.c?  Because if they
aren't deferred then I don't see why changing the last argument to
fold_undefer_overflow_warnings would make any difference.


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

* [Bug c/50082] -Wstrict-overflow mishandles typedef
  2011-08-15  8:01 [Bug c/50082] New: -Wstrict-overflow mishandles typedef eggert at gnu dot org
  2011-08-15 11:05 ` [Bug c/50082] " rguenth at gcc dot gnu.org
  2011-08-15 14:48 ` ian at airs dot com
@ 2011-08-15 15:09 ` rguenth at gcc dot gnu.org
  2011-08-15 15:48 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-08-15 15:09 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50082

--- Comment #3 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-08-15 15:01:38 UTC ---
Author: rguenth
Date: Mon Aug 15 15:01:33 2011
New Revision: 177762

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=177762
Log:
2011-08-15  Richard Guenther  <rguenther@suse.de>

    PR middle-end/50082
    * fold-const.c (maybe_canonicalize_comparison_1): Properly
    convert the modified operand to the other operand type.
    (fold_comparison): Call maybe_canonicalize_comparison_1 with
    useless conversions stripped from comparison operands.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c


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

* [Bug c/50082] -Wstrict-overflow mishandles typedef
  2011-08-15  8:01 [Bug c/50082] New: -Wstrict-overflow mishandles typedef eggert at gnu dot org
                   ` (2 preceding siblings ...)
  2011-08-15 15:09 ` rguenth at gcc dot gnu.org
@ 2011-08-15 15:48 ` rguenth at gcc dot gnu.org
  2011-08-15 20:04 ` iant at google dot com
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-08-15 15:48 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50082

--- Comment #4 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-08-15 15:31:37 UTC ---
(In reply to comment #2)
> Not sure I understand your question about tree-ssa-forwprop.c.  Are you saying
> that overflow warnings are already deferred at that point, so that passing
> WARN_STRICT_OVERFLOW_CONDITIONAL causes extraneous warnings to be issued?  Are
> overflow warnings already deferred during tree-ssa-forwprop.c?  Because if they
> aren't deferred then I don't see why changing the last argument to
> fold_undefer_overflow_warnings would make any difference.

No, but the warning is defered as WARN_STRICT_OVERFLOW_MAGNITUDE and then
re-issued as WARN_STRICT_OVERFLOW_CONDITIONAL.  I'm not sure why forwprop
"raises" the severity forcefully - maybe you'd remember?  Maybe you
remember why you added the ability to forcefully override?

The issue is hidden now on trunk, still visible on the 4.6 branch and
with obfuscating the testcase to

void
insert_cache_boundary_ok (int gap_start, int i)
{
  int tem;
  if (i < 0)
    __builtin_abort ();

  tem = i - 1;
  if (tem < gap_start)
    __builtin_abort ();
}

also on trunk.  It should warn for -Wstrict-overflow=5 but not for
-Wstrict-overflow=1.

I suppose the forwprop code wants to force a warning at -Wstrict-overflow=1
if the conditional becomes optimized to a constant at compile-time?


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

* [Bug c/50082] -Wstrict-overflow mishandles typedef
  2011-08-15  8:01 [Bug c/50082] New: -Wstrict-overflow mishandles typedef eggert at gnu dot org
                   ` (3 preceding siblings ...)
  2011-08-15 15:48 ` rguenth at gcc dot gnu.org
@ 2011-08-15 20:04 ` iant at google dot com
  2011-08-16  9:19 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: iant at google dot com @ 2011-08-15 20:04 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50082

--- Comment #5 from iant at google dot com <iant at google dot com> 2011-08-15 19:39:08 UTC ---
"rguenth at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> writes:

> I suppose the forwprop code wants to force a warning at -Wstrict-overflow=1
> if the conditional becomes optimized to a constant at compile-time?

Yes.  But perhaps it is overly aggressive--are these conditionals being
optimized to a constant?  Maybe the condition for the first argument to
fold_undefer_overflow_warnings needs to be fixed.


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

* [Bug c/50082] -Wstrict-overflow mishandles typedef
  2011-08-15  8:01 [Bug c/50082] New: -Wstrict-overflow mishandles typedef eggert at gnu dot org
                   ` (4 preceding siblings ...)
  2011-08-15 20:04 ` iant at google dot com
@ 2011-08-16  9:19 ` rguenth at gcc dot gnu.org
  2011-08-16 11:47 ` [Bug tree-optimization/50082] " rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-08-16  9:19 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50082

--- Comment #6 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-08-16 09:10:12 UTC ---
(In reply to comment #5)
> "rguenth at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> writes:
> 
> > I suppose the forwprop code wants to force a warning at -Wstrict-overflow=1
> > if the conditional becomes optimized to a constant at compile-time?
> 
> Yes.  But perhaps it is overly aggressive--are these conditionals being
> optimized to a constant?  Maybe the condition for the first argument to
> fold_undefer_overflow_warnings needs to be fixed.

In this case not.  forwprop doesn't do anything more fancy than calling
fold, so fold should already handle optimizing to a constant specially, no?
So I guess the first argument to fold_undefer_overflow_warnings should be
zero?  I'll be posting a patch and CC you.


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

* [Bug tree-optimization/50082] -Wstrict-overflow mishandles typedef
  2011-08-15  8:01 [Bug c/50082] New: -Wstrict-overflow mishandles typedef eggert at gnu dot org
                   ` (5 preceding siblings ...)
  2011-08-16  9:19 ` rguenth at gcc dot gnu.org
@ 2011-08-16 11:47 ` rguenth at gcc dot gnu.org
  2011-08-16 15:36 ` rguenth at gcc dot gnu.org
  2011-08-16 16:21 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-08-16 11:47 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50082

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
          Component|c                           |tree-optimization
         AssignedTo|unassigned at gcc dot       |rguenth at gcc dot gnu.org
                   |gnu.org                     |

--- Comment #7 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-08-16 11:29:41 UTC ---
Patch posted, thus mine.


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

* [Bug tree-optimization/50082] -Wstrict-overflow mishandles typedef
  2011-08-15  8:01 [Bug c/50082] New: -Wstrict-overflow mishandles typedef eggert at gnu dot org
                   ` (6 preceding siblings ...)
  2011-08-16 11:47 ` [Bug tree-optimization/50082] " rguenth at gcc dot gnu.org
@ 2011-08-16 15:36 ` rguenth at gcc dot gnu.org
  2011-08-16 16:21 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-08-16 15:36 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50082

--- Comment #8 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-08-16 15:32:21 UTC ---
Author: rguenth
Date: Tue Aug 16 15:32:17 2011
New Revision: 177788

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=177788
Log:
2011-08-16  Richard GUenther  <rguenther@suse.de>

    PR tree-optimization/50082
    * tree-ssa-forwprop.c (combine_cond_expr_cond): Handle overflow
    warnings here, instead of ...
    (ssa_forward_propagate_and_combine): ... here.
    (forward_propagate_into_comparison_1): Adjust.
    (forward_propagate_into_comparison): Likewise.
    (forward_propagate_into_gimple_cond): Likewise.
    (forward_propagate_into_cond): Likewise.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/tree-ssa-forwprop.c


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

* [Bug tree-optimization/50082] -Wstrict-overflow mishandles typedef
  2011-08-15  8:01 [Bug c/50082] New: -Wstrict-overflow mishandles typedef eggert at gnu dot org
                   ` (7 preceding siblings ...)
  2011-08-16 15:36 ` rguenth at gcc dot gnu.org
@ 2011-08-16 16:21 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-08-16 16:21 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50082

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |4.7.0

--- Comment #9 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-08-16 15:35:04 UTC ---
Fixed for 4.7.0.


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

end of thread, other threads:[~2011-08-16 15:36 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-15  8:01 [Bug c/50082] New: -Wstrict-overflow mishandles typedef eggert at gnu dot org
2011-08-15 11:05 ` [Bug c/50082] " rguenth at gcc dot gnu.org
2011-08-15 14:48 ` ian at airs dot com
2011-08-15 15:09 ` rguenth at gcc dot gnu.org
2011-08-15 15:48 ` rguenth at gcc dot gnu.org
2011-08-15 20:04 ` iant at google dot com
2011-08-16  9:19 ` rguenth at gcc dot gnu.org
2011-08-16 11:47 ` [Bug tree-optimization/50082] " rguenth at gcc dot gnu.org
2011-08-16 15:36 ` rguenth at gcc dot gnu.org
2011-08-16 16:21 ` rguenth 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).