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

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


Author: Roman Lebedev
Date: 2020-09-21T20:25:54+03:00
New Revision: 64e2cb7e9605995d2efb625203cbd96db1404812

URL: https://github.com/llvm/llvm-project/commit/64e2cb7e9605995d2efb625203cbd96db1404812
DIFF: https://github.com/llvm/llvm-project/commit/64e2cb7e9605995d2efb625203cbd96db1404812.diff

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

Added: 
    

Modified: 
    llvm/lib/Analysis/ScalarEvolution.cpp
    llvm/test/Analysis/ScalarEvolution/saturating-intrinsics.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 47422035ee89..b82132d1d90c 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -6365,6 +6365,16 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
         const SCEV *ClampedY = getUMinExpr(X, Y);
         return getMinusSCEV(X, ClampedY, SCEV::FlagNUW);
       }
+      case Intrinsic::uadd_sat: {
+        const SCEV *X = getSCEV(II->getArgOperand(0));
+        const SCEV *Y = getSCEV(II->getArgOperand(1));
+        const SCEV *ClampedX = getUMinExpr(
+            X, getMinusSCEV(
+                   getConstant(cast<ConstantInt>(
+                       Constant::getAllOnesValue(II->getType()))),
+                   Y, (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW)));
+        return getAddExpr(ClampedX, Y, SCEV::FlagNUW);
+      }
       default:
         break;
       }

diff  --git a/llvm/test/Analysis/ScalarEvolution/saturating-intrinsics.ll b/llvm/test/Analysis/ScalarEvolution/saturating-intrinsics.ll
index 0a054eea1491..287c85151be1 100644
--- a/llvm/test/Analysis/ScalarEvolution/saturating-intrinsics.ll
+++ b/llvm/test/Analysis/ScalarEvolution/saturating-intrinsics.ll
@@ -13,7 +13,7 @@ define i32 @uadd_sat(i32 %x, i32 %y) {
 ; CHECK-LABEL: 'uadd_sat'
 ; CHECK-NEXT:  Classifying expressions for: @uadd_sat
 ; CHECK-NEXT:    %z = call i32 @llvm.uadd.sat.i32(i32 %x, i32 %y)
-; CHECK-NEXT:    --> %z U: full-set S: full-set
+; CHECK-NEXT:    --> (((-1 + (-1 * %y)) umin %x) + %y)<nuw> U: full-set S: full-set
 ; CHECK-NEXT:  Determining loop execution counts for: @uadd_sat
 ;
   %z = call i32 @llvm.uadd.sat.i32(i32 %x, i32 %y)


        


More information about the llvm-commits mailing list