HomePhabricator

[SCEV] Recognize @llvm.uadd.sat as `%y + umin(%x, (-1 - %y))`

Authored by lebedev.ri on Sep 21 2020, 9:42 AM.

Description

[SCEV] Recognize @llvm.uadd.sat as %y + umin(%x, (-1 - %y))


define i32 @src(i32 %x, i32 %y) {
%0:

%r = uadd_sat i32 %x, %y
ret i32 %r

}

>

define i32 @tgt(i32 %x, i32 %y) {
%0:

%t0 = sub nsw nuw i32 4294967295, %y
%t1 = umin i32 %x, %t0
%r = add nuw i32 %t1, %y
ret i32 %r

}
Transformation seems to be correct!

The alternative, naive, lowering could be the following,
although i don't think it's better,
thought it will likely be needed for sadd/ssub/*shl:


define i32 @src(i32 %x, i32 %y) {
%0:

%r = uadd_sat i32 %x, %y
ret i32 %r

}

>

define i32 @tgt(i32 %x, i32 %y) {
%0:

%t0 = zext i32 %x to i33
%t1 = zext i32 %y to i33
%t2 = add nuw i33 %t0, %t1
%t3 = zext i32 4294967295 to i33
%t4 = umin i33 %t2, %t3
%r = trunc i33 %t4 to i32
ret i32 %r

}
Transformation seems to be correct!

Details

Committed
lebedev.riSep 21 2020, 10:25 AM
Parents
rGfedc9549d50d: [SCEV] Recognize @llvm.usub.sat as `%x - (umin %x, %y)`
Branches
Unknown
Tags
Unknown