[llvm] [InstCombine] Fold `(x < y) ? -1 : zext(x != y)` into `u/scmp(x,y)` (PR #101049)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 16 05:50:09 PDT 2024
================
@@ -3558,6 +3558,55 @@ static Instruction *foldBitCeil(SelectInst &SI, IRBuilderBase &Builder) {
Masked);
}
+// This function tries to fold the following operations:
+// (x < y) ? -1 : zext(x != y)
+// (x > y) ? 1 : sext(x != y)
+// Into ucmp/scmp(x, y), where signedness is determined by the signedness
+// of the comparison in the original sequence
+Instruction *InstCombinerImpl::foldSelectToCmp(SelectInst &SI) {
+ Value *TV = SI.getTrueValue();
+ Value *FV = SI.getFalseValue();
+
+ ICmpInst::Predicate Pred;
+ Value *LHS, *RHS;
+ if (!match(SI.getCondition(), m_ICmp(Pred, m_Value(LHS), m_Value(RHS))))
+ return nullptr;
+
+ if (!LHS->getType()->isIntOrIntVectorTy())
+ return nullptr;
+
+ // Try to swap operands and the predicate. We need to be careful when doing
+ // so because two of the patterns have opposite predicates, so use the
+ // constant inside select to determine if swapping operands would be
+ // beneficial to us.
+ if ((ICmpInst::isGT(Pred) && match(TV, m_AllOnes())) ||
+ (ICmpInst::isLT(Pred) && match(TV, m_One())) || ICmpInst::isLE(Pred)) {
----------------
nikic wrote:
The isLE case here shouldn't be relevant anymore.
https://github.com/llvm/llvm-project/pull/101049
More information about the llvm-commits
mailing list