HomePhabricator

[InstCombine] fold abs(X)/X to cmp+select

Authored by spatel on Aug 16 2020, 12:42 PM.

Description

[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!

Details

Committed
spatelAug 17 2020, 5:01 AM
Parents
rG61512ddd2d57: [InstCombine] add tests for sdiv-of-abs; NFC
Branches
Unknown
Tags
Unknown

Event Timeline

xbolva00 added inline comments.
/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1183

More general?

X / (select C, A, X) -> select C, X / A, 1

https://reviews.llvm.org/D71312

spatel added inline comments.Aug 17 2020, 7:56 AM
/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1183

Not quite - we still want to handle that pattern with select, but if we assume that we canonicalize to the intrinsic form, then we need to match this too. Hopefully, we're not too far off from canonicalizing and changing the front-end to produce min/max/abs intrinsics.