Link to the Compiler Explorer: https://godbolt.org/z/9zY57K3Yh GCC also warns about UB, but I think it is incorrect. The code is well-defined if "h" is equal to "1". Reproducer: unsigned c; int e, f; void g(long h, long j[][9]) { for (long d = 1; d; d += -(h ?: 600000000000000000)) { { int a = j[d][d]; f = h ?: a; } int i = f; { int a = c, b = i; e = b ?: a; } c = e; } } Error: >$ g++ -O3 -c func.cpp func.cpp: In function 'void g(long int, long int (*)[9])': func.cpp:6:18: warning: iteration 7 invokes undefined behavior [-Waggressive-loop-optimizations] 6 | int a = j[d][d]; | ^ func.cpp:4:20: note: within this loop 4 | for (long d = 1; d; d += -(h ?: 600000000000000000)) { | ^ during GIMPLE pass: modref func.cpp:3:6: internal compiler error: in merge, at ipa-modref-tree.cc:176 3 | void g(long h, long j[][9]) { | ^ 0x84f1a9 modref_access_node::merge(modref_access_node const&, bool) /testing/gcc/gcc_src_master/gcc/ipa-modref-tree.cc:176 0x10147cc modref_access_node::try_merge_with(vec<modref_access_node, va_gc, vl_embed>*&, unsigned long) /testing/gcc/gcc_src_master/gcc/ipa-modref-tree.cc:457 0x1016562 modref_access_node::insert(vec<modref_access_node, va_gc, vl_embed>*&, modref_access_node, unsigned long, bool) /testing/gcc/gcc_src_master/gcc/ipa-modref-tree.cc:562 0x101220b modref_ref_node<int>::insert_access(modref_access_node, unsigned long, bool) /testing/gcc/gcc_src_master/gcc/ipa-modref-tree.h:194 0x101220b modref_tree<int>::insert(unsigned int, unsigned int, unsigned int, int, int, modref_access_node, bool) /testing/gcc/gcc_src_master/gcc/ipa-modref-tree.h:444 0x1004eac modref_tree<int>::insert(tree_node*, int, int, modref_access_node const&, bool) /testing/gcc/gcc_src_master/gcc/ipa-modref-tree.h:471 0x1004eac record_access /testing/gcc/gcc_src_master/gcc/ipa-modref.cc:1076 0x1005958 analyze_load /testing/gcc/gcc_src_master/gcc/ipa-modref.cc:1707 0xf57cb4 walk_stmt_load_store_addr_ops(gimple*, void*, bool (*)(gimple*, tree_node*, tree_node*, void*), bool (*)(gimple*, tree_node*, tree_node*, void*), bool (*)(gimple*, tree_node*, tree_node*, void*)) /testing/gcc/gcc_src_master/gcc/gimple-walk.cc:813 0x100ba01 analyze_stmt /testing/gcc/gcc_src_master/gcc/ipa-modref.cc:1788 0x100d1e9 analyze /testing/gcc/gcc_src_master/gcc/ipa-modref.cc:1900 0x100d1e9 analyze_function /testing/gcc/gcc_src_master/gcc/ipa-modref.cc:3220 0x100ecae execute /testing/gcc/gcc_src_master/gcc/ipa-modref.cc:4187 gcc version 13.0.1 20230329 (3be4e43a6a0f429648ea188c8e110b74268fed27)
Confirmed, also ICEs on the 12 branch.
Started with r12-3142-g5c85f29537662f.
The warning is because we unswitch the loop, so we have a separate loop for h != 0 and separate loop for h == 0. And the latter obviously invokes undefined behavior (though I get it for iteration 3 rather than 7).
mine.
GCC 12.3 is being released, retargeting bugs to GCC 12.4.
GCC 12.4 is being released, retargeting bugs to GCC 12.5.