[IRCE][NFCI] Refactor parseRangeCheckICmp
authorMax Kazantsev <mkazantsev@azul.com>
Fri, 21 Apr 2023 10:16:59 +0000 (17:16 +0700)
committerMax Kazantsev <mkazantsev@azul.com>
Fri, 21 Apr 2023 10:32:51 +0000 (17:32 +0700)
Simplify parseRangeCheckICmp:
- If RHS is loop-variant, swap LHS/RHS and swap predicate
- all checks are either IV >(=) const or IV <(=) RHS (maybe not const)

Patch by Aleksandr Popov!

Differential Revision: https://reviews.llvm.org/D148720

llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp

index dfd460c6b7198599876d0ca166237333fcbd2044..62e3cf6403430323ecca495f58bf2a64deff4abd 100644 (file)
@@ -264,46 +264,37 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
   Value *LHS = ICI->getOperand(0);
   Value *RHS = ICI->getOperand(1);
 
+  // Canonicalize to the `Index Pred Invariant` comparison
+  if (IsLoopInvariant(LHS)) {
+    std::swap(LHS, RHS);
+    Pred = CmpInst::getSwappedPredicate(Pred);
+  } else if (!IsLoopInvariant(RHS))
+    // Both LHS and RHS are loop variant
+    return false;
+
   switch (Pred) {
   default:
     return false;
 
-  case ICmpInst::ICMP_SLE:
-    std::swap(LHS, RHS);
-    [[fallthrough]];
   case ICmpInst::ICMP_SGE:
     if (match(RHS, m_ConstantInt<0>())) {
       Index = SE.getSCEV(LHS);
-      return true; // Lower.
+      return true;
     }
     return false;
 
-  case ICmpInst::ICMP_SLT:
-    std::swap(LHS, RHS);
-    [[fallthrough]];
   case ICmpInst::ICMP_SGT:
     if (match(RHS, m_ConstantInt<-1>())) {
       Index = SE.getSCEV(LHS);
-      return true; // Lower.
-    }
-
-    if (IsLoopInvariant(LHS)) {
-      Index = SE.getSCEV(RHS);
-      End = SE.getSCEV(LHS);
-      return true; // Upper.
+      return true;
     }
     return false;
 
+  case ICmpInst::ICMP_SLT:
   case ICmpInst::ICMP_ULT:
-    std::swap(LHS, RHS);
-    [[fallthrough]];
-  case ICmpInst::ICMP_UGT:
-    if (IsLoopInvariant(LHS)) {
-      Index = SE.getSCEV(RHS);
-      End = SE.getSCEV(LHS);
-      return true; // Both lower and upper.
-    }
-    return false;
+    Index = SE.getSCEV(LHS);
+    End = SE.getSCEV(RHS);
+    return true;
   }
 
   llvm_unreachable("default clause returns!");