================ @@ -1898,6 +1882,60 @@ static Instruction *foldSelectICmpEq(SelectInst &SI, ICmpInst *ICI, return nullptr; } +/// Fold `X Pred C1 ? X BOp C2 : C1 BOp C2` to `min/max(X, C1) BOp C2`. +/// 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; + + unsigned Opcode = BOp->getOpcode(); + + if (Instruction::isIntDivRem(Opcode)) ---------------- veera-sivarajan wrote:
Yeah, it's a heuristic. I've added a comment. https://github.com/llvm/llvm-project/pull/116888 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits