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

--- Comment #8 from Paweł Bylica <chfast at gmail dot com> ---
Created attachment 65985
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65985&action=edit
Testcase reduced from intx (-O3, cunrolli)

Another instance, at -O3 from cunrolli. The attached testcase (reduced from the
intx library) warns with GCC 15.2, 16.2 and trunk:

    $ g++ -std=c++20 -O3 -c pr121143-intx.cpp
    pr121143-intx.cpp:170:8: warning: iteration 2305843009213693951 invokes
undefined behavior [-Waggressive-loop-optimizations]

The loop is in submul(), which is only called on the multi-word division path
of udivrem(), taken when the divisor has more than one significant word. The
only divisor is the constant 10, so the path is dead. (The arithmetic left
after reduction is not meaningful; only this dispatch matters.) There is no
warning at -O2 or with -fdisable-tree-cunrolli.

To answer comment 7: yes, the dead loop is removed later. In the GCC 16.2
cunrolli dump:
- the first iteration completely unrolls the loop counting the divisor words
(line 120), but the VN after it does not fold if (_32 == 1);
- the second iteration then analyzes the loop in the dead path, gets "Loop 5
iterates 18446744073709551615 times" and warns;
- the VN after the second iteration replaces _32 with 1 and removes the dead
edge.

PR112767 was the same pattern with SCCP.

Reply via email to