[llvm] 9766a0c - [SCEV] Recognize `cond ? i1 0 : i1 y` as `umin_seq ~cond, x`

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 10 06:47:13 PST 2022


Author: Roman Lebedev
Date: 2022-02-10T17:42:55+03:00
New Revision: 9766a0cca00408712fa9a3ed4d2ef319f8e30385

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

LOG: [SCEV] Recognize `cond ? i1 0 : i1 y` as `umin_seq ~cond, x`

By definition, `umin_seq` has the exact same
poison stopping properties the original `select` had:
https://alive2.llvm.org/ce/z/N6XwV-

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 6cd60a9c3a0a..86eeb518f9a1 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -6015,6 +6015,13 @@ const SCEV *ScalarEvolution::createNodeForSelectOrPHIViaUMinSeq(
                                     /*Sequential=*/true));
   }
 
+  // i1 cond ? i1 0 : i1 y  -->  umin_seq ~cond, y
+  if (auto *TrueConst = dyn_cast<ConstantInt>(TrueVal)) {
+    if (TrueConst->isZero())
+      return getUMinExpr(getNotSCEV(getSCEV(Cond)), getSCEV(FalseVal),
+                         /*Sequential=*/true);
+  }
+
   return getUnknown(V);
 }
 

diff  --git a/llvm/test/Analysis/ScalarEvolution/logical-operations.ll b/llvm/test/Analysis/ScalarEvolution/logical-operations.ll
index 19f91c9ebbfc..e1e874737693 100644
--- a/llvm/test/Analysis/ScalarEvolution/logical-operations.ll
+++ b/llvm/test/Analysis/ScalarEvolution/logical-operations.ll
@@ -158,7 +158,7 @@ define i1 @select_false_or_x(i1 %c, i1 %x) {
 ; CHECK-LABEL: 'select_false_or_x'
 ; CHECK-NEXT:  Classifying expressions for: @select_false_or_x
 ; CHECK-NEXT:    %r = select i1 %c, i1 false, i1 %x
-; CHECK-NEXT:    --> %r U: full-set S: full-set
+; CHECK-NEXT:    --> ((true + %c) umin_seq %x) U: full-set S: full-set
 ; CHECK-NEXT:  Determining loop execution counts for: @select_false_or_x
 ;
   %r = select i1 %c, i1 false, i1 %x


        


More information about the llvm-commits mailing list