[llvm] [InstCombine] Fold icmp (zext X), (and (trunc Y), Mask) -> icmp X, trunc Y (PR #221189)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 28 11:57:45 PDT 2026


================
@@ -5825,6 +5826,24 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
   if (Value *V = foldShiftIntoShiftInAnotherHandOfAndInICmp(I, SQ, Builder))
     return replaceInstUsesWith(I, V);
 
+  // icmp (zext X), (and (trunc Y), Mask) -> icmp X, trunc Y IFF Mask exactly
+  // covers the bits of X
+  {
+    Value *Y;
+    const APInt *Mask;
+    if (match(I.getOperand(1), m_ZExt(m_Value(X))) &&
+        match(I.getOperand(0),
+              m_OneUse(m_And(m_Trunc(m_Value(Y)), m_APInt(Mask))))) {
----------------
dtcxzyw wrote:

I am fine with the current implementation, since it has covered enough real-world cases.

> also looked at a recent commit in the same file (https://github.com/llvm/llvm-project/commit/6d7e7fbafd8d010a3a4146b24e8cca2c8b7dc831, uitofp of masked truncations) similar and+trunc+mask shape

The difference is that you cannot get a type hint in https://github.com/llvm/llvm-project/commit/6d7e7fbafd8d010a3a4146b24e8cca2c8b7dc831.


https://github.com/llvm/llvm-project/pull/221189


More information about the llvm-commits mailing list