[llvm] [InstCombine] Fold [su]cmp of no-wrap ops with a common operand (PR #216157)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 15 08:21:37 PDT 2026
================
@@ -1982,6 +1982,119 @@ static Value *foldSinAndCosToSinCos(IntrinsicInst *II, IRBuilderBase &B,
return IsSin ? Sin : Cos;
}
+/// [su]cmp only depends on the relative order of its operands, so applying the
+/// same order-preserving operation to both of them is a no-op. If both operands
+/// are computed by the same non-wrapping (and therefore monotonic) operation
+/// with a common operand, the comparison can be done on the original values:
+/// ucmp(add nuw X, Z, add nuw Y, Z) --> ucmp(X, Y)
+/// scmp(sub nsw Z, X, sub nsw Z, Y) --> scmp(Y, X)
+/// On a successful match \p X and \p Y are set to the new comparison operands,
+/// already swapped if the operation reverses the order.
+static bool matchOrderPreservingCmpOperands(Value *Op0, Value *Op1,
----------------
dtcxzyw wrote:
I'd prefer to reuse this helper function to fold icmps as well. I don't like maintaining similar logic across multiple places.
https://github.com/llvm/llvm-project/blob/2a0c57c535edc30f4f3d469542eef10d497db122/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp#L5344
https://github.com/llvm/llvm-project/blob/2a0c57c535edc30f4f3d469542eef10d497db122/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp#L5505-L5511
https://github.com/llvm/llvm-project/blob/2a0c57c535edc30f4f3d469542eef10d497db122/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp#L5528-L5580
https://github.com/llvm/llvm-project/pull/216157
More information about the llvm-commits
mailing list