[InstCombine] fold abs(X)/X to cmp+select
authorSanjay Patel <spatel@rotateright.com>
Sun, 16 Aug 2020 19:42:06 +0000 (15:42 -0400)
committerSanjay Patel <spatel@rotateright.com>
Mon, 17 Aug 2020 12:01:28 +0000 (08:01 -0400)
commite6b6787d01e9ea6338b5b51c6e3ba1b903876b3a
tree20dfd38551eae892d925cdf0dd0be61985110c02
parent61512ddd2d57fc33464bda477dab04829266faa1
[InstCombine] fold abs(X)/X to cmp+select

The backend can convert the select-of-constants to
bit-hack shift+logic if desirable.

https://alive2.llvm.org/ce/z/pgJT6E

  define i8 @src(i8 %x) {
  %0:
    %a = abs i8 %x, 1
    %d = sdiv i8 %x, %a
    ret i8 %d
  }
  =>
  define i8 @tgt(i8 %x) {
  %0:
    %cond = icmp sgt i8 %x, 255
    %r = select i1 %cond, i8 1, i8 255
    ret i8 %r
  }
  Transformation seems to be correct!
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/test/Transforms/InstCombine/sdiv-canonicalize.ll