1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 10:42:39 +01:00

[SCEV] Recognize @llvm.abs as smax(x, -x)

As per alive2 (ignoring undef):

----------------------------------------
define i32 @src(i32 %x, i1 %y) {
%0:
  %r = abs i32 %x, 0
  ret i32 %r
}
=>
define i32 @tgt(i32 %x, i1 %y) {
%0:
  %neg_x = mul i32 %x, 4294967295
  %r = smax i32 %x, %neg_x
  ret i32 %r
}
Transformation seems to be correct!

----------------------------------------
define i32 @src(i32 %x, i1 %y) {
%0:
  %r = abs i32 %x, 1
  ret i32 %r
}
=>
define i32 @tgt(i32 %x, i1 %y) {
%0:
  %neg_x = mul nsw i32 %x, 4294967295
  %r = smax i32 %x, %neg_x
  ret i32 %r
}
Transformation seems to be correct!
This commit is contained in:
Roman Lebedev 2020-09-21 17:16:08 +03:00
parent a776b0a84c
commit 33cace0561
2 changed files with 10 additions and 2 deletions

View File

@ -6339,6 +6339,14 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
if (auto *II = dyn_cast<IntrinsicInst>(U)) {
switch (II->getIntrinsicID()) {
case Intrinsic::abs: {
const SCEV *Op = getSCEV(II->getArgOperand(0));
SCEV::NoWrapFlags Flags =
cast<ConstantInt>(II->getArgOperand(1))->isOne()
? SCEV::FlagNSW
: SCEV::FlagAnyWrap;
return getSMaxExpr(Op, getNegativeSCEV(Op, Flags));
}
case Intrinsic::umax:
return getUMaxExpr(getSCEV(II->getArgOperand(0)),
getSCEV(II->getArgOperand(1)));

View File

@ -8,7 +8,7 @@ define i32 @abs_nonsw(i32 %x) {
; CHECK-LABEL: 'abs_nonsw'
; CHECK-NEXT: Classifying expressions for: @abs_nonsw
; CHECK-NEXT: %r = call i32 @llvm.abs.i32(i32 %x, i1 false)
; CHECK-NEXT: --> %r U: full-set S: full-set
; CHECK-NEXT: --> ((-1 * %x) smax %x) U: full-set S: full-set
; CHECK-NEXT: Determining loop execution counts for: @abs_nonsw
;
%r = call i32 @llvm.abs.i32(i32 %x, i1 0)
@ -19,7 +19,7 @@ define i32 @abs_nsw(i32 %x) {
; CHECK-LABEL: 'abs_nsw'
; CHECK-NEXT: Classifying expressions for: @abs_nsw
; CHECK-NEXT: %r = call i32 @llvm.abs.i32(i32 %x, i1 true)
; CHECK-NEXT: --> %r U: [0,-2147483648) S: full-set
; CHECK-NEXT: --> ((-1 * %x)<nsw> smax %x) U: full-set S: full-set
; CHECK-NEXT: Determining loop execution counts for: @abs_nsw
;
%r = call i32 @llvm.abs.i32(i32 %x, i1 1)