https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114719
Bug ID: 114719 Summary: Missed optimization: conditional in loop is an invariant (a%2) Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: 652023330028 at smail dot nju.edu.cn Target Milestone: --- Hello, we noticed that maybe there is a missed optimization for Loop Unswitch. In the following reduced code, the value of 'a%2' is the same each time through the loop. https://godbolt.org/z/49cGaanr8 unsigned a, b; int m,n; void func() { for (unsigned i=0; i < 1000; i++) { if(a%2) b++; a += 2; } } But GCC -O3 -fwrapv: <bb 3> [local count: 1063004408]: # b_lsm.7_10 = PHI <b_lsm.7_20(2), b_lsm.7_34(5)> # b_lsm_flag.8_11 = PHI <0(2), b_lsm_flag.8_35(5)> # ivtmp.13_24 = PHI <a_lsm.6_19(2), ivtmp.13_28(5)> # DEBUG i => NULL # DEBUG BEGIN_STMT _32 = ivtmp.13_24 & 1; if (_32 != 0) goto <bb 4>; [50.00%] else goto <bb 5>; [50.00%] <bb 4> [local count: 531502204]: # DEBUG BEGIN_STMT _33 = b_lsm.7_10 + 1; <bb 5> [local count: 1063004408]: # b_lsm.7_34 = PHI <b_lsm.7_10(3), _33(4)> # b_lsm_flag.8_35 = PHI <b_lsm_flag.8_11(3), 1(4)> # DEBUG BEGIN_STMT # DEBUG BEGIN_STMT # DEBUG i => NULL # DEBUG BEGIN_STMT ivtmp.13_28 = ivtmp.13_24 + 2; if (ivtmp.13_28 != _37) goto <bb 3>; [98.99%] else goto <bb 6>; [1.01%] Thank you very much for your time and effort! We look forward to hearing from you.