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

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 10:26:35 PDT 2020


Author: Roman Lebedev
Date: 2020-09-21T20:25:53+03:00
New Revision: 1bb7ab8c4a324aa380bddfc75069e24c19e2bdd0

URL: https://github.com/llvm/llvm-project/commit/1bb7ab8c4a324aa380bddfc75069e24c19e2bdd0
DIFF: https://github.com/llvm/llvm-project/commit/1bb7ab8c4a324aa380bddfc75069e24c19e2bdd0.diff

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 7f38731bf6a5..11e8740d2f50 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -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)));

diff  --git a/llvm/test/Analysis/ScalarEvolution/abs-intrinsic.ll b/llvm/test/Analysis/ScalarEvolution/abs-intrinsic.ll
index daf8460ea60c..ec8666613a78 100644
--- a/llvm/test/Analysis/ScalarEvolution/abs-intrinsic.ll
+++ b/llvm/test/Analysis/ScalarEvolution/abs-intrinsic.ll
@@ -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)


        


More information about the llvm-commits mailing list