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

            Bug ID: 114651
           Summary: Missed optimization: (c - y) + ((a - c) - (b - y)) =>
                    a-b
           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 the computation of 'z' in the following code can be
optimized to 'a-b'.

https://godbolt.org/z/hKT8YT6MW

int x, y, z;
void func(int a, int b, int c){
    x = b;
    y = c + a / b;
    z = (c - y) + ((a - c) - (b - y));
}

But GCC -O3 (-fwrapv):
  _1 = a_10(D) / b_8(D);
  _2 = _1 + c_11(D);
  y = _2;
  # DEBUG BEGIN_STMT
  _4 = a_10(D) - c_11(D);
  _17 = _4 - b_8(D);
  _6 = _2 + _17;
  _3 = _6 - _1;
  z = _3;


Expected code:
  _1 = a_7(D) / b_8(D);
  _2 = _1 + c_9(D);
  y = _2;
  # DEBUG BEGIN_STMT
  _17 = a_7(D) - b_8(D);
  z = _17;

Thank you very much for your time and effort! We look forward to hearing from
you.

Reply via email to