https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111257
Bug ID: 111257 Summary: new signed overflow after vectorizer Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: kristerw at gcc dot gnu.org Target Milestone: --- The vectorizer is not removing the original scalar calculations, and they may overflow after vectorization. This can be seen with int a[8]; void foo(void) { for (int i = 0; i < 8; i++) a[i] = a[i] + 5; } The IR for the loop before vectorization looks like <bb 3> [local count: 954449104]: # i_10 = PHI <i_7(5), 0(2)> # ivtmp_4 = PHI <ivtmp_3(5), 8(2)> _1 = a[i_10]; _2 = _1 + 5; a[i_10] = _2; i_7 = i_10 + 1; ivtmp_3 = ivtmp_4 - 1; if (ivtmp_3 != 0) goto <bb 5>; [87.50%] else goto <bb 4>; [12.50%] <bb 5> [local count: 835156385]: goto <bb 3>; [100.00%] and it is vectorized to <bb 3> [local count: 238585440]: # i_10 = PHI <i_7(5), 0(2)> # ivtmp_4 = PHI <ivtmp_3(5), 8(2)> # vectp_a.4_9 = PHI <vectp_a.4_8(5), &a(2)> # vectp_a.8_16 = PHI <vectp_a.8_17(5), &a(2)> # ivtmp_19 = PHI <ivtmp_20(5), 0(2)> vect__1.6_13 = MEM <vector(4) int> [(int *)vectp_a.4_9]; _1 = a[i_10]; vect__2.7_15 = vect__1.6_13 + { 5, 5, 5, 5 }; _2 = _1 + 5; MEM <vector(4) int> [(int *)vectp_a.8_16] = vect__2.7_15; i_7 = i_10 + 1; ivtmp_3 = ivtmp_4 - 1; vectp_a.4_8 = vectp_a.4_9 + 16; vectp_a.8_17 = vectp_a.8_16 + 16; ivtmp_20 = ivtmp_19 + 1; if (ivtmp_20 < 2) goto <bb 5>; [50.00%] else goto <bb 4>; [50.00%] <bb 5> [local count: 119292723]: goto <bb 3>; [100.00%] This vectorized loop still read _1 from a[i_10] and adds 5 to it, so the second loop iteration will add 5 to the value of a[1]. But the first iteration has already added 5 to a[1], so we are now doing a different calculation compared to the original loop, and this can overflow even if the original did not.