[llvm] [InstCombine] Fold (X == 0 ? Y : 0) | X to X == 0 ? Y : X (PR #138373)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Sat May 3 02:51:07 PDT 2025
================
@@ -3575,6 +3575,41 @@ static Value *foldOrOfInversions(BinaryOperator &I,
return nullptr;
}
+// Optimize patterns where an OR operation combines a select-based zero check
+// with its condition value. This handles both scalar and vector types.
+//
+// Given:
+// (X == 0 ? Y : 0) | X --> X == 0 ? Y : X
+// X | (X == 0 ? Y : 0) --> X == 0 ? Y : X
+//
+// Also handles cases where X might be wrapped in zero/sign extensions.
+static Instruction *foldOrOfSelectZero(BinaryOperator &BO, Value *Op0,
+ Value *Op1) {
+ CmpPredicate Pred;
+ Value *X, *Y;
+
+ // Check both operand orders to handle commutative OR
+ for (Value *SelVal : {Op0, Op1}) {
+ // The other operand in the OR operation (potentially X or extended X)
+ Value *Other = (SelVal == Op0) ? Op1 : Op0;
+
+ // Attempt to match the select pattern:
+ // select(icmp eq X, 0), Y, 0
+ // Where X might be:
+ // - Original value
+ // - Zero extended value (zext)
+ // - Sign extended value (sext)
+ if (match(SelVal, m_Select(m_c_ICmp(Pred, m_Value(X), m_Zero()), m_Value(Y),
----------------
nikic wrote:
```suggestion
if (match(SelVal, m_Select(m_ICmp(Pred, m_Value(X), m_Zero()), m_Value(Y),
```
icmp constant is always on RHS.
https://github.com/llvm/llvm-project/pull/138373
More information about the llvm-commits
mailing list