[llvm] [InstCombine] Fold `X > C2 ? X + C1 : C2 + C1` to `max(X, C2) + C1` (PR #116888)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 19 15:39:11 PST 2024
================
@@ -1898,6 +1898,44 @@ static Instruction *foldSelectICmpEq(SelectInst &SI, ICmpInst *ICI,
return nullptr;
}
+// Turn (select (Cmp X C1) (BOp X C2) C3)
+// -> (select (Cmp X C1) (BOp X C2) (BOp C1 C2))
+// -> (BOp (select (Cmp X C1) X C1) C2)
+// iff C3 == BOp C1 C2
+// This allows for better canonicalization.
+static Instruction *canonicalizeConstToBOp(SelectInst &SI, Value *TrueVal,
+ Value *FalseVal,
+ InstCombinerImpl &IC) {
+ Value *CondVal = SI.getCondition();
+
+ BinaryOperator *BOp;
+ Constant *C1, *C2, *C3;
+ Value *X;
+ ICmpInst::Predicate Predicate;
+
+ if (!match(CondVal, m_c_ICmp(Predicate, m_Value(X), m_Constant(C1))))
+ return nullptr;
+
+ if (!((match(TrueVal, m_BinOp(BOp)) && match(FalseVal, m_Constant(C3))) ||
+ (match(FalseVal, m_BinOp(BOp)) && match(TrueVal, m_Constant(C3)))))
+ return nullptr;
+
+ if (!match(BOp, m_OneUse(m_c_BinOp(m_Specific(X), m_Constant(C2)))))
+ return nullptr;
+
+ if (!(ICmpInst::isRelational(Predicate) &&
+ C3 == ConstantFoldBinaryOpOperands(BOp->getOpcode(), C1, C2,
+ SI.getDataLayout())))
+ return nullptr;
+
+ Instruction *NewBO =
+ BinaryOperator::CreateWithCopiedFlags(BOp->getOpcode(), C1, C2, BOp);
+ IC.InsertNewInstBefore(NewBO, SI.getIterator());
----------------
veera-sivarajan wrote:
A binary operator is manually created and inserted because the equivalent IRBuilder method, `CreateBinOp()`, constant folds the operands.
But the operands should not be folded to allow `foldSelectOpOp()` to detect the common operand.
https://github.com/llvm/llvm-project/pull/116888
More information about the llvm-commits
mailing list