Link to the Compiler Explorer: https://godbolt.org/z/zKqcna3vn Reproducer: #include <stdio.h> unsigned long long int var_4 = 42; unsigned long long int var_14 = 10; unsigned char var_16 = 1; unsigned short var_18 = 0; short var_75; inline const int &foo(const int &b, const int &c) { return b < c ? c : b; } inline unsigned long long &bar(unsigned long long &b, unsigned long long &c) { return !c ? c : b; } void test(unsigned long long var_4, unsigned long long var_14, unsigned char var_16, unsigned short var_18) __attribute__((noipa)); void test(unsigned long long var_4, unsigned long long var_14, unsigned char var_16, unsigned short var_18) { for (bool h = 0; h < (bool)foo(var_16 ? -7 : 4, var_4 ? var_4 : var_18); h = 2) var_75 = bar(var_4, var_14); } int main() { test(var_4, var_14, var_16, var_18); printf("%hd\n", var_75); if (var_75 != 42) __builtin_abort(); } Error: >$ g++ -O0 small.cpp && ./a.out 42 >$ g++ -O2 small.cpp && ./a.out 0 Aborted (core dumped) gcc version 13.0.1 20230329 (3be4e43a6a0f429648ea188c8e110b74268fed27)
Confirmed.
Started with r13-5348-gc29d85359add80.
Mine then.
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>: https://gcc.gnu.org/g:1d0ba4467dd9cad11eb9ff547442e3ce6292b892 commit r13-6942-g1d0ba4467dd9cad11eb9ff547442e3ce6292b892 Author: Richard Biener <rguenther@suse.de> Date: Thu Mar 30 11:32:19 2023 +0200 tree-optimization/109342 - wrong code with edge equivalences in VN The following testcase shows a problem in how we query valitity for equivalences on edges when the edge is a backedge and thus refering to a block thats later in the iteration order we use for VN. That causes the dominated_by_p_w_unex helper to look at edge executable state that's not yet computed and thus still at optimistic not executable state. The following makes sure to use a plain dominance check in these cases. PR tree-optimization/109342 * tree-ssa-sccvn.cc (vn_nary_op_get_predicated_value): New overload for edge. When that edge is a backedge use dominated_by_p directly. * g++.dg/torture/pr109342.C: New testcase.
Fixed.