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

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 18 09:27:25 PST 2024


================
@@ -1781,6 +1781,54 @@ static Value *foldSelectInstWithICmpConst(SelectInst &SI, ICmpInst *ICI,
   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,
+                                          IRBuilderBase &Builder) {
+  CmpPredicate Pred;
+  Value *A, *B;
+
+  if (!match(FalseVal, m_ICmp(Pred, m_Value(A), m_Value(B))))
+    return nullptr;
+
+  Type *Ty = A->getType();
+
+  if (Ty->isPtrOrPtrVectorTy())
+    return nullptr;
+
+  // make sure `CmpLHS` is on the LHS of `FalseVal`.
+  if (CmpLHS == B) {
+    std::swap(A, B);
+    Pred = CmpInst::getSwappedPredicate(Pred);
+  }
+
+  if (CmpLHS != A)
+    return nullptr;
----------------
goldsteinn wrote:

Probably simpler to do:
```
if(!match(FalseVal, m_c_ICmp(Pred, m_Specific(CmpLHS), m_Value(B)))
  return nullptr;
```

Then you can drop the manual swapping and replace A with CmpLHS below.

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


More information about the llvm-commits mailing list