[llvm] [InstCombine] Evaluate zext nneg as sext where possible (PR #212230)

John Brawn via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 12 06:51:28 PDT 2026


================
@@ -1627,10 +1642,14 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) {
     uint32_t DestBitSize = DestTy->getScalarSizeInBits();
 
     // If the high bits are already filled with zeros, just replace this
-    // cast with the result.
-    if (MaskedValueIsZero(
-            Res, APInt::getHighBitsSet(DestBitSize, DestBitSize - SrcBitsKept),
-            &Zext))
+    // cast with the result. If we've evaluated as a signed expressions then
+    // instead check that the high bits are the sign bit, which we know is zero.
+    if (EvaluateAsSigned
+            ? (ComputeNumSignBits(Res, &Zext) > DestBitSize - SrcBitsKept)
+            : MaskedValueIsZero(
+                  Res,
+                  APInt::getHighBitsSet(DestBitSize, DestBitSize - SrcBitsKept),
+                  &Zext))
----------------
john-brawn-arm wrote:

I've added a test (zext_of_trunc_same_size) that was reduced from the libc++ test failure where removing the ComputeNumSignBits check causes an incorrect transformation.

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


More information about the llvm-commits mailing list