[SCEV] Recognize @llvm.abs as smax(x, -x)
authorRoman Lebedev <lebedev.ri@gmail.com>
Mon, 21 Sep 2020 14:16:08 +0000 (17:16 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Mon, 21 Sep 2020 17:25:53 +0000 (20:25 +0300)
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!

llvm/lib/Analysis/ScalarEvolution.cpp
llvm/test/Analysis/ScalarEvolution/abs-intrinsic.ll

index 7f38731..11e8740 100644 (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)));
index daf8460..ec86666 100644 (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)