[NFC][SCEV] `createNodeForSelectOrPHIViaUMinSeq()`: refactor `i1 cond ? i1 x : i1...
authorRoman Lebedev <lebedev.ri@gmail.com>
Thu, 10 Feb 2022 14:21:44 +0000 (17:21 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Thu, 10 Feb 2022 14:42:56 +0000 (17:42 +0300)
While that effectively concludes i1 select handling,
that boolean restriction can be lifted later.

llvm/lib/Analysis/ScalarEvolution.cpp

index 5271275..86d0bb1 100644 (file)
@@ -5994,20 +5994,23 @@ const SCEV *ScalarEvolution::createNodeForSelectOrPHIViaUMinSeq(
       !FalseVal->getType()->isIntegerTy(1))
     return getUnknown(V);
 
-  // i1 cond ? i1 x : i1 C  -->  C + (umin_seq cond, x + C)
-  if (isa<ConstantInt>(FalseVal)) {
-    const SCEV *C = getSCEV(FalseVal);
-    return getAddExpr(C, getUMinExpr(getSCEV(Cond),
-                                     getAddExpr(C, getSCEV(TrueVal)),
-                                     /*Sequential=*/true));
-  }
-
-  // i1 cond ? i1 C : i1 y  -->  C + (umin_seq ~cond, y + C)
-  if (isa<ConstantInt>(TrueVal)) {
-    const SCEV *C = getSCEV(TrueVal);
-    return getAddExpr(C, getUMinExpr(getNotSCEV(getSCEV(Cond)),
-                                     getAddExpr(C, getSCEV(FalseVal)),
-                                     /*Sequential=*/true));
+  // i1 cond ? i1 x : i1 C  -->  C + (umin_seq  cond, x + C)
+  // i1 cond ? i1 C : i1 x  -->  C + (umin_seq ~cond, x + C)
+  if (isa<ConstantInt>(TrueVal) || isa<ConstantInt>(FalseVal)) {
+    const SCEV *CondExpr = getSCEV(Cond);
+    const SCEV *TrueExpr = getSCEV(TrueVal);
+    const SCEV *FalseExpr = getSCEV(FalseVal);
+    const SCEV *X, *C;
+    if (isa<ConstantInt>(TrueVal)) {
+      CondExpr = getNotSCEV(CondExpr);
+      X = FalseExpr;
+      C = TrueExpr;
+    } else {
+      X = TrueExpr;
+      C = FalseExpr;
+    }
+    return getAddExpr(
+        C, getUMinExpr(CondExpr, getAddExpr(C, X), /*Sequential=*/true));
   }
 
   return getUnknown(V);