[llvm] [InstSimplify] `A == MIN_INT ? B != MIN_INT : A < B` to `A < B` (PR #120177)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 16 20:08:09 PST 2024


================
@@ -4608,6 +4608,45 @@ static Value *simplifySelectWithEquivalence(Value *CmpLHS, Value *CmpRHS,
   return nullptr;
 }
 
+/// `A == MIN_INT ? B != MIN_INT : A < B` --> `A < B`
+/// `A == MAX_INT ? B != MAX_INT : A > B` --> `A > B`
+static Value *foldSelectWithExtremeEqCond(Value *CmpLHS, Value *CmpRHS,
+                                          Value *TrueVal, Value *FalseVal) {
+  CmpPredicate Pred;
+  Value *A, *B;
+
+  if (!match(FalseVal, m_ICmp(Pred, m_Value(A), m_Value(B))))
+    return nullptr;
+
+  // make sure `CmpLHS` is on the LHS of `FalseVal`.
+  if (match(CmpLHS, m_Specific(B))) {
+    std::swap(A, B);
+    Pred = CmpInst::getSwappedPredicate(Pred);
+  }
+
+  APInt C;
+  unsigned NumBits = A->getType()->getScalarSizeInBits();
----------------
dtcxzyw wrote:

Should bail out on pointer types.


https://github.com/llvm/llvm-project/pull/120177


More information about the llvm-commits mailing list