[llvm] [llvm][GVN] Propagate `trunc nuw to i1` equalities (PR #143273)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 8 01:19:06 PDT 2025


================
@@ -2579,6 +2579,17 @@ bool GVNPass::propagateEquality(Value *LHS, Value *RHS,
 
       continue;
     }
+
+    // Propagate equality that result from truncation with no unsigned wrap
+    // like (trunc nuw i64 %v to i1) == "true" or (trunc nuw i64 %v to i1) ==
+    // "false"
+    if (auto *Trunc = dyn_cast<TruncInst>(LHS)) {
+      if (Trunc->hasNoUnsignedWrap() && Trunc->getType()->isIntegerTy(1)) {
+        Value *Input = Trunc->getOperand(0);
+        Worklist.push_back({Input,
+                            ConstantInt::get(Input->getType(), IsKnownTrue)});
+      }
+    }
----------------
andjo403 wrote:

as the type of LHS and RHS is asserted to be same above the type check is not needed so this can be simplified to 
```suggestion
    if (match(LHS, m_NUWTrunc(m_Value(A)))) {
      Worklist.emplace_back(A, ConstantInt::get(A->getType(), IsKnownTrue));
      continue;
    }
```

also use a continue to avoid that LHS also match something else if more checks is added later

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


More information about the llvm-commits mailing list