Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[optimization] gcc generate better code than clang base on value range propagation #54735

Open
vfdff opened this issue Apr 4, 2022 · 2 comments · May be fixed by #66668
Open

[optimization] gcc generate better code than clang base on value range propagation #54735

vfdff opened this issue Apr 4, 2022 · 2 comments · May be fixed by #66668

Comments

@vfdff
Copy link
Contributor

vfdff commented Apr 4, 2022

after fix the case #54132, the clang still need be improved for some more complex case (see detail on https://godbolt.org/z/xb9Mo85WM ), such as:

#include <stdlib.h>

int my_abs (int x, int y) {
  return x>y ? abs (x-y+1): 0;
}
  • clang's code:
my_abs: // @my_abs
  subs w8, w0, w1
  b.le .LBB0_2
  mvn w9, w8
  cmn w8, #1
  csinc w0, w9, w8, lt
  ret
.LBB0_2:
  mov w0, wzr
  ret
  • gcc's code:
my_abs:
  subs w0, w0, w1
  csinc w0, wzr, w0, le
  ret
@vfdff
Copy link
Contributor Author

vfdff commented Aug 9, 2023

the issue still exist, https://alive2.llvm.org/ce/z/ZTU2ct

vfdff added a commit to vfdff/llvm-project that referenced this issue Dec 30, 2023
x-y+1 is positive when x > y, so abs (x-y+1) --> x-y+1

Fixes llvm#54735
vfdff added a commit to vfdff/llvm-project that referenced this issue Dec 30, 2023
x -nsw y < C is false when x > y and C <= 0
Alive2 proof for sgt, sge, ugt, uge : https://alive2.llvm.org/ce/z/tupvfi

Fixes llvm#54735
vfdff added a commit to vfdff/llvm-project that referenced this issue Jun 1, 2024
x-y+1 is positive when x > y, so abs (x-y+1) --> x-y+1

Fixes llvm#54735
vfdff added a commit to vfdff/llvm-project that referenced this issue Jun 1, 2024
x -nsw y < -C is false when x > y and C >= 0
Alive2 proof for sgt, sge, ugt, uge : https://alive2.llvm.org/ce/z/tupvfi

Fixes llvm#54735
vfdff added a commit to vfdff/llvm-project that referenced this issue Jun 5, 2024
x-y+1 is positive when x > y, so abs (x-y+1) --> x-y+1

Fixes llvm#54735
vfdff added a commit to vfdff/llvm-project that referenced this issue Jun 5, 2024
x -nsw y < -C is false when x > y and C >= 0
Alive2 proof for sgt, sge : https://alive2.llvm.org/ce/z/tupvfi
Note: It only really makes sense in the context of signed comparison for
      "X - Y must be positive if X >= Y and no overflow".

Fixes llvm#54735
vfdff added a commit to vfdff/llvm-project that referenced this issue Jun 6, 2024
x-y+1 is positive when x > y, so abs (x-y+1) --> x-y+1

Fixes llvm#54735
vfdff added a commit to vfdff/llvm-project that referenced this issue Jun 6, 2024
x -nsw y < -C is false when x > y and C >= 0
Alive2 proof for sgt, sge : https://alive2.llvm.org/ce/z/tupvfi
Note: It only really makes sense in the context of signed comparison for
      "X - Y must be positive if X >= Y and no overflow".

Fixes llvm#54735
@vfdff
Copy link
Contributor Author

vfdff commented Jun 7, 2024

  • base on the llvm15. it transform the abs into a select, and the newest upstream don't try this
IC: Replacing   %call = call i32 @abs(i32 noundef %add) #2
    with   %1 = select i1 %0, i32 %neg, i32 %add

This is because the new upstream transform call i32 @abs into call i32 @llvm.abs.i32 at the begin of the optimazation, which casues entry into different branches for II = dyn_cast<IntrinsicInst>(CI) in function LibCallSimplifier::optimizeCall. This changes from llvm17 to llvm18, https://gcc.godbolt.org/z/8jx4d13Wo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants