[llvm] ae48af5 - [NFC][SCEV] Recognize umin_seq when operand is zext'ed in zero-check

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 16 11:16:16 PST 2022


Author: Roman Lebedev
Date: 2022-02-16T22:16:02+03:00
New Revision: ae48af582b34f571fdfb1a05adda0f3bc9fe5569

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

LOG: [NFC][SCEV] Recognize umin_seq when operand is zext'ed in zero-check

zext(umin(x,y)) == umin(zext(x),zext(y))
zext(x) == 0  ->  x == 0

While it is not a very likely scenario, we probably should not expect
that instcombine already dropped such a redundant zext,
but handle directly. Moreover, perhaps there was no ZExtInst,
and SCEV somehow managed to  pull out said zext out of the SCEV expression.

Added: 
    

Modified: 
    llvm/lib/Analysis/ScalarEvolution.cpp
    llvm/test/Analysis/ScalarEvolution/logical-operations.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 2922ca2588e1..e1b2b12a4df5 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -6007,14 +6007,17 @@ const SCEV *ScalarEvolution::createNodeForSelectOrPHIInstWithICmpInstCond(
     // x == 0 ? 0 : umin_seq(..., x, ...)  ->  umin_seq(x, umin_seq(...))
     // x == 0 ? 0 : umin    (..., umin_seq(..., x, ...), ...)
     //                    ->  umin_seq(x, umin (..., umin_seq(...), ...))
-    if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) &&
-        isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero() &&
+    if (isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero() &&
         isa<ConstantInt>(TrueVal) && cast<ConstantInt>(TrueVal)->isZero()) {
       const SCEV *X = getSCEV(LHS);
-      const SCEV *FalseValExpr = getSCEV(FalseVal);
-      if (SCEVMinMaxExprContains(FalseValExpr, X, scSequentialUMinExpr))
-        return getUMinExpr(getNoopOrZeroExtend(X, I->getType()), FalseValExpr,
-                           /*Sequential=*/true);
+      while (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(X))
+        X = ZExt->getOperand();
+      if (getTypeSizeInBits(X->getType()) <= getTypeSizeInBits(I->getType())) {
+        const SCEV *FalseValExpr = getSCEV(FalseVal);
+        if (SCEVMinMaxExprContains(FalseValExpr, X, scSequentialUMinExpr))
+          return getUMinExpr(getNoopOrZeroExtend(X, I->getType()), FalseValExpr,
+                             /*Sequential=*/true);
+      }
     }
     break;
   default:

diff  --git a/llvm/test/Analysis/ScalarEvolution/logical-operations.ll b/llvm/test/Analysis/ScalarEvolution/logical-operations.ll
index aca7cf37a6f9..f607e89106eb 100644
--- a/llvm/test/Analysis/ScalarEvolution/logical-operations.ll
+++ b/llvm/test/Analysis/ScalarEvolution/logical-operations.ll
@@ -690,7 +690,7 @@ define i8 @umin_seq_x_y_zext_in_iszero(i8 %x, i8 %y) {
 ; CHECK-NEXT:    %umin = call i8 @llvm.umin.i8(i8 %y, i8 %x)
 ; CHECK-NEXT:    --> (%x umin %y) U: full-set S: full-set
 ; CHECK-NEXT:    %r = select i1 %x.is.zero, i8 0, i8 %umin
-; CHECK-NEXT:    --> %r U: full-set S: full-set
+; CHECK-NEXT:    --> (%x umin_seq %y) U: full-set S: full-set
 ; CHECK-NEXT:  Determining loop execution counts for: @umin_seq_x_y_zext_in_iszero
 ;
   %x.wide = zext i8 %x to i32


        


More information about the llvm-commits mailing list