[llvm] [IndVars] Teach widenLoopCompare to use sext if narrow IV is positive and other operand is already sext. (PR #142703)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 4 09:24:02 PDT 2025


================
@@ -1630,6 +1630,12 @@ bool WidenIV::widenLoopCompare(WidenIV::NarrowIVDefUse DU) {
 
   // Widen the other operand of the compare, if necessary.
   if (CastWidth < IVWidth) {
+    // If the narrow IV is always postive and the other operand is sext, widen
----------------
preames wrote:

To check my understanding, this comment basically says that icmp PRED (zext nonneg X), (zext Y) == icmp PRED (zext nonneg X), (sext Y)?

Thinking through this...

If the high bit of X is set, then this is poison.

If the high bit of X is not set, then there are two cases:
1) The high bit of Y is not set, zext == sext by definition.
2) The high bit of Y is set, zext and sext produce different values.  Neither can be equal to zext Y (because it would be poison).  

I can buy this for equality, but does this hold for inequality?  In particular, icmp slt (zext nonneg X), ext(255) gives different results doesn't it?




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


More information about the llvm-commits mailing list