[llvm] [DAGCombiner] infer wrap flags for trunc, use to fold itofp (PR #148729)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 14 14:51:37 PDT 2025


================
@@ -16310,6 +16310,22 @@ SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
     break;
   }
 
+  // Use known bits to apply the nsw/nuw flags to the truncate.
+  const unsigned DestWidth = VT.getScalarSizeInBits();
+  const unsigned SrcWidth = N0.getScalarValueSizeInBits();
+  SDNodeFlags Flags = N->getFlags();
+  if (!N->getFlags().hasNoSignedWrap() &&
+      DAG.ComputeMaxSignificantBits(N0) <= DestWidth)
+    Flags.setNoSignedWrap(true);
+  if (!N->getFlags().hasNoUnsignedWrap() &&
+      DAG.MaskedValueIsZero(N0, APInt::getBitsSetFrom(SrcWidth, DestWidth)))
+    Flags.setNoUnsignedWrap(true);
+
+  if (!(Flags == N->getFlags())) {
+    N->setFlags(Flags);
+    return SDValue(N, 0);
----------------
topperc wrote:

Do we need to add users of N to the worklist?

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


More information about the llvm-commits mailing list