public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/106842] New: misleading warning : iteration X invokes undefined behavior
@ 2022-09-05 21:43 franckbehaghel_gcc at protonmail dot com
  2022-09-05 22:12 ` [Bug tree-optimization/106842] [12 Regression] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: franckbehaghel_gcc at protonmail dot com @ 2022-09-05 21:43 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106842
           Summary: misleading warning : iteration X invokes undefined
                    behavior
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: franckbehaghel_gcc at protonmail dot com
  Target Milestone: ---

Hello,

I get the following misleading warning with gcc 12.2 with -O2/-O3.
11.3 seems fine.

gcc -O2 main.c 
main.c: In function ‘main’:
main.c:16:38: warning: iteration 9 invokes undefined behavior
[-Waggressive-loop-optimizations]
   16 |            for(int64_t k =0; k<i1 ; k++)
      |                                     ~^~
main.c:16:31: note: within this loop
   16 |            for(int64_t k =0; k<i1 ; k++)
      |                              ~^~~

cat main.c
#include "stdio.h"
#include "stdint.h"

int main(int argc, char** argv)
{
   int64_t i1=0;
   int64_t i3=0,i2=0; // warning with this declaration order 
   //int64_t i2=0,i3=0; // but fine (no warning) with this order

   for ( i1 = 0; i1<10 ; i1++)
   {
       for ( ; i2<10 ;i2++ )         
           printf("L2\n");

       for ( ; i3<10 ; i3++ )
           for(int64_t k =0; k<i1 ; k++)
               printf("L3\n");
   }

   printf("i1 %lu i2 %lu i3 %lu\n",i1,i2,i3);  
   return 0;
}


Could someone reproduce ?
The weirdest part of this warning : it depends on the index declaration order.
Even if gcc has a bad(?) way of computing internal range/validity to deduce
such issues, I would expect it in both order.

Regards,

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

* [Bug tree-optimization/106842] [12 Regression] misleading warning : iteration X invokes undefined behavior
  2022-09-05 21:43 [Bug c/106842] New: misleading warning : iteration X invokes undefined behavior franckbehaghel_gcc at protonmail dot com
@ 2022-09-05 22:12 ` pinskia at gcc dot gnu.org
  2022-09-05 22:20 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-05 22:12 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to work|                            |11.3.0, 13.0
   Target Milestone|---                         |12.3
            Summary|misleading warning :        |[12 Regression] misleading
                   |iteration X invokes         |warning : iteration X
                   |undefined behavior          |invokes undefined behavior
      Known to fail|                            |12.1.0, 12.2.0

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

* [Bug tree-optimization/106842] [12 Regression] misleading warning : iteration X invokes undefined behavior
  2022-09-05 21:43 [Bug c/106842] New: misleading warning : iteration X invokes undefined behavior franckbehaghel_gcc at protonmail dot com
  2022-09-05 22:12 ` [Bug tree-optimization/106842] [12 Regression] " pinskia at gcc dot gnu.org
@ 2022-09-05 22:20 ` pinskia at gcc dot gnu.org
  2022-09-06  4:51 ` marxin at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-05 22:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The IR difference comes from PRE.
In the case where the warning happens:

find_duplicates: <bb 12> duplicate of <bb 23>
find_duplicates: <bb 14> duplicate of <bb 18>
find_duplicates: <bb 16> duplicate of <bb 20>

vs where it does not happen:

find_duplicates: <bb 15> duplicate of <bb 20>
find_duplicates: <bb 13> duplicate of <bb 17>

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

* [Bug tree-optimization/106842] [12 Regression] misleading warning : iteration X invokes undefined behavior
  2022-09-05 21:43 [Bug c/106842] New: misleading warning : iteration X invokes undefined behavior franckbehaghel_gcc at protonmail dot com
  2022-09-05 22:12 ` [Bug tree-optimization/106842] [12 Regression] " pinskia at gcc dot gnu.org
  2022-09-05 22:20 ` pinskia at gcc dot gnu.org
@ 2022-09-06  4:51 ` marxin at gcc dot gnu.org
  2022-09-06  4:58 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-09-06  4:51 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-09-06
                 CC|                            |marxin at gcc dot gnu.org
           Keywords|needs-bisection             |

--- Comment #2 from Martin Liška <marxin at gcc dot gnu.org> ---
Fixed on master with r13-2076-g9594e04e4e2abeb4, started with
r12-5183-ga7753db4a7c071fe.

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

* [Bug tree-optimization/106842] [12 Regression] misleading warning : iteration X invokes undefined behavior
  2022-09-05 21:43 [Bug c/106842] New: misleading warning : iteration X invokes undefined behavior franckbehaghel_gcc at protonmail dot com
                   ` (2 preceding siblings ...)
  2022-09-06  4:51 ` marxin at gcc dot gnu.org
@ 2022-09-06  4:58 ` pinskia at gcc dot gnu.org
  2022-09-06  8:35 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-06  4:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Martin Liška from comment #2)
> Fixed on master with r13-2076-g9594e04e4e2abeb4, started with
> r12-5183-ga7753db4a7c071fe.

Would make sense really but that might mean a latent bug in the code in pre for
the comparing of duplicated bbs when the only difference is decl uids too

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

* [Bug tree-optimization/106842] [12 Regression] misleading warning : iteration X invokes undefined behavior
  2022-09-05 21:43 [Bug c/106842] New: misleading warning : iteration X invokes undefined behavior franckbehaghel_gcc at protonmail dot com
                   ` (3 preceding siblings ...)
  2022-09-06  4:58 ` pinskia at gcc dot gnu.org
@ 2022-09-06  8:35 ` rguenth at gcc dot gnu.org
  2022-09-06  8:36 ` [Bug tree-optimization/106842] [12/13 " rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-09-06  8:35 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |aldyh at gcc dot gnu.org,
                   |                            |rguenth at gcc dot gnu.org
           Keywords|                            |missed-optimization,
                   |                            |wrong-code

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Huh.

static void
infer_loop_bounds_from_signedness (class loop *loop, gimple *stmt)
{
...
  low = lower_bound_in_type (type, type);
  high = upper_bound_in_type (type, type);
  value_range r;
  get_range_query (cfun)->range_of_expr (r, def);
  if (r.kind () == VR_RANGE)
    {
      low = wide_int_to_tree (type, r.lower_bound ());
      high = wide_int_to_tree (type, r.upper_bound ());
    }

  record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);

and record_nonwrapping_iv verifies that the IV stays in [low, high].  So
this essentially means we are verifying range info here.  We have

# RANGE [0, 9]
k_36 = PHI <k_20(18), 0(5)>
# RANGE [1, 9]
k_20 = k_36 + 1;

note the whole code is in a if (0) guarded block and we are executing
ranger_vrp, in the substitute and fold phase (IIRC the old [E]VRP avoided
to substitute/fold in dead code regions?).

somebody needs to double-check if the range on the stmts is wrong, but
depending on how VRP propagation treats unexecutable edges it might be
GIGO anyhow.

The "missed optimization" is that substitute-and-fold considers dead
regions.  "wrong code" is because either the range is bogus or the
niter we derive from it is.

needs a second eye

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

* [Bug tree-optimization/106842] [12/13 Regression] misleading warning : iteration X invokes undefined behavior
  2022-09-05 21:43 [Bug c/106842] New: misleading warning : iteration X invokes undefined behavior franckbehaghel_gcc at protonmail dot com
                   ` (4 preceding siblings ...)
  2022-09-06  8:35 ` rguenth at gcc dot gnu.org
@ 2022-09-06  8:36 ` rguenth at gcc dot gnu.org
  2022-12-20 15:23 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-09-06  8:36 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[12 Regression] misleading  |[12/13 Regression]
                   |warning : iteration X       |misleading warning :
                   |invokes undefined behavior  |iteration X invokes
                   |                            |undefined behavior

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
And for sure the cited rev. doesn't fix anything, in fact it should not have
caused less threading.

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

* [Bug tree-optimization/106842] [12/13 Regression] misleading warning : iteration X invokes undefined behavior
  2022-09-05 21:43 [Bug c/106842] New: misleading warning : iteration X invokes undefined behavior franckbehaghel_gcc at protonmail dot com
                   ` (5 preceding siblings ...)
  2022-09-06  8:36 ` [Bug tree-optimization/106842] [12/13 " rguenth at gcc dot gnu.org
@ 2022-12-20 15:23 ` rguenth at gcc dot gnu.org
  2023-05-08 12:25 ` [Bug tree-optimization/106842] [12/13/14 " rguenth at gcc dot gnu.org
  2024-03-15  0:50 ` [Bug tree-optimization/106842] [12 " law at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-12-20 15:23 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2
                 CC|                            |amacleod at redhat dot com

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
So I can confirm that the following avoids the bogus diagnostic on the GCC 12
branch but it will ICE when doing -fdump-tree-vrp-details like

t.c: In function 'main':
t.c:4:5: internal compiler error: tree check: expected class 'type', have
'exceptional' (error_mark) in to_wide, at tree.h:6138
    4 | int main(int argc, char** argv)
      |     ^~~~
0x1889919 tree_class_check_failed(tree_node const*, tree_code_class, char
const*, int, char const*)
        /home/rguenther/src/gcc-12-branch/gcc/tree.cc:8869
0xae7151 tree_class_check(tree_node*, tree_code_class, char const*, int, char
const*)
        /home/rguenther/src/gcc-12-branch/gcc/tree.h:3580
0xb68f93 wi::to_wide(tree_node const*)
        /home/rguenther/src/gcc-12-branch/gcc/tree.h:6138
0xcdbc11 irange::upper_bound(unsigned int) const
        /home/rguenther/src/gcc-12-branch/gcc/value-range.h:535
0xcdbc81 irange::upper_bound() const
        /home/rguenther/src/gcc-12-branch/gcc/value-range.h:545
0x2ab34d3 operator_not_equal::op1_range(irange&, tree_node*, irange const&,
irange const&, tree_code) const
        /home/rguenther/src/gcc-12-branch/gcc/range-op.cc:713
0x2952cfd gimple_range_calc_op1(irange&, gimple const*, irange const&, irange
const&)
        /home/rguenther/src/gcc-12-branch/gcc/gimple-range-gori.cc:77
0x29558a8 gori_compute::compute_operand1_range(irange&, gimple*, irange const&,
tree_node*, fur_source&)
        /home/rguenther/src/gcc-12-branch/gcc/gimple-range-gori.cc:1024
0x295493c gori_compute::compute_operand_range(irange&, gimple*, irange const&,
tree_node*, fur_source&)
        /home/rguenther/src/gcc-12-branch/gcc/gimple-range-gori.cc:718
0x29566a2 gori_compute::outgoing_edge_range_p(irange&, edge_def*, tree_node*,
range_query&)
        /home/rguenther/src/gcc-12-branch/gcc/gimple-range-gori.cc:1271
0x294639e ranger_cache::range_on_edge(irange&, edge_def*, tree_node*)
        /home/rguenther/src/gcc-12-branch/gcc/gimple-range-cache.cc:1083
0x2942993 gimple_ranger::dump_bb(_IO_FILE*, basic_block_def*)
        /home/rguenther/src/gcc-12-branch/gcc/gimple-range.cc:550
0x2942c11 gimple_ranger::dump(_IO_FILE*)
        /home/rguenther/src/gcc-12-branch/gcc/gimple-range.cc:590
0x186618d execute_ranger_vrp(function*, bool)
        /home/rguenther/src/gcc-12-branch/gcc/tree-vrp.cc:4343

because we dump (and invoke range compute!) for unreachable blocks which
eventually runs into code with not up-to-date SSA form.  The patch should
save compile-time by not folding/substituting in regions that are not
reachable.

diff --git a/gcc/tree-ssa-propagate.cc b/gcc/tree-ssa-propagate.cc
index 976b035eeec..e669fc9b7cf 100644
--- a/gcc/tree-ssa-propagate.cc
+++ b/gcc/tree-ssa-propagate.cc
@@ -664,7 +664,7 @@ class substitute_and_fold_dom_walker : public dom_walker
 public:
     substitute_and_fold_dom_walker (cdi_direction direction,
                                    class substitute_and_fold_engine *engine)
-       : dom_walker (direction),
+       : dom_walker (direction, REACHABLE_BLOCKS),
           something_changed (false),
          substitute_and_fold_engine (engine)
     {
@@ -952,7 +952,7 @@ substitute_and_fold_dom_walker::before_dom_children
(basic_block bb)

   something_changed |= substitute_and_fold_engine->propagate_into_phi_args
(bb);

-  return NULL;
+  return find_taken_edge (bb, NULL_TREE);
 }

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

* [Bug tree-optimization/106842] [12/13/14 Regression] misleading warning : iteration X invokes undefined behavior
  2022-09-05 21:43 [Bug c/106842] New: misleading warning : iteration X invokes undefined behavior franckbehaghel_gcc at protonmail dot com
                   ` (6 preceding siblings ...)
  2022-12-20 15:23 ` rguenth at gcc dot gnu.org
@ 2023-05-08 12:25 ` rguenth at gcc dot gnu.org
  2024-03-15  0:50 ` [Bug tree-optimization/106842] [12 " law at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-05-08 12:25 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|12.3                        |12.4

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 12.3 is being released, retargeting bugs to GCC 12.4.

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

* [Bug tree-optimization/106842] [12 Regression] misleading warning : iteration X invokes undefined behavior
  2022-09-05 21:43 [Bug c/106842] New: misleading warning : iteration X invokes undefined behavior franckbehaghel_gcc at protonmail dot com
                   ` (7 preceding siblings ...)
  2023-05-08 12:25 ` [Bug tree-optimization/106842] [12/13/14 " rguenth at gcc dot gnu.org
@ 2024-03-15  0:50 ` law at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: law at gcc dot gnu.org @ 2024-03-15  0:50 UTC (permalink / raw)
  To: gcc-bugs

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

Jeffrey A. Law <law at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[12/13/14 Regression]       |[12 Regression] misleading
                   |misleading warning :        |warning : iteration X
                   |iteration X invokes         |invokes undefined behavior
                   |undefined behavior          |
                 CC|                            |law at gcc dot gnu.org

--- Comment #8 from Jeffrey A. Law <law at gcc dot gnu.org> ---
Fixed in gcc-13.

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

end of thread, other threads:[~2024-03-15  0:50 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-05 21:43 [Bug c/106842] New: misleading warning : iteration X invokes undefined behavior franckbehaghel_gcc at protonmail dot com
2022-09-05 22:12 ` [Bug tree-optimization/106842] [12 Regression] " pinskia at gcc dot gnu.org
2022-09-05 22:20 ` pinskia at gcc dot gnu.org
2022-09-06  4:51 ` marxin at gcc dot gnu.org
2022-09-06  4:58 ` pinskia at gcc dot gnu.org
2022-09-06  8:35 ` rguenth at gcc dot gnu.org
2022-09-06  8:36 ` [Bug tree-optimization/106842] [12/13 " rguenth at gcc dot gnu.org
2022-12-20 15:23 ` rguenth at gcc dot gnu.org
2023-05-08 12:25 ` [Bug tree-optimization/106842] [12/13/14 " rguenth at gcc dot gnu.org
2024-03-15  0:50 ` [Bug tree-optimization/106842] [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).