[clang] [llvm] [InstCombine] Fold `X Pred C2 ? X BOp C1 : C2 BOp C1` to `min/max(X, C2) BOp C1` (PR #116888)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 29 17:35:54 PST 2024
================
@@ -1898,6 +1882,56 @@ static Instruction *foldSelectICmpEq(SelectInst &SI, ICmpInst *ICI,
return nullptr;
}
+/// Fold `X Pred C2 ? X BOp C1 : C2 BOp C1` to `min/max(X, C2) BOp C1`.
+/// This allows for better canonicalization.
+static Value *foldSelectWithConstOpToBinOp(ICmpInst *Cmp, Value *TrueVal,
+ Value *FalseVal,
+ IRBuilderBase &Builder) {
+ BinaryOperator *BOp;
+ Constant *C1, *C2, *C3;
+ Value *X;
+ ICmpInst::Predicate Predicate;
+
+ if (!match(Cmp, m_ICmp(Predicate, m_Value(X), m_Constant(C1))))
+ return nullptr;
+
+ if (!ICmpInst::isRelational(Predicate))
+ return nullptr;
+
+ if (match(TrueVal, m_Constant())) {
+ std::swap(FalseVal, TrueVal);
+ Predicate = ICmpInst::getInversePredicate(Predicate);
+ }
+
+ if (!match(TrueVal, m_BinOp(BOp)) || !match(FalseVal, m_Constant(C3)))
+ return nullptr;
+
+ if (!match(BOp, m_OneUse(m_BinOp(m_Specific(X), m_Constant(C2)))))
+ return nullptr;
+
+ Value *RHS;
+ SelectPatternFlavor SPF;
+ unsigned Opcode = BOp->getOpcode();
----------------
veera-sivarajan wrote:
No, it does not help Rust's inclusive ranges and I cannot think of any other cases where it would be always profitable.
https://github.com/llvm/llvm-project/pull/116888
More information about the cfe-commits
mailing list