[llvm] [SelectionDAG] add ISD::BITCAST handling for isGuaranteedNotToBeUndefOrPoison (PR #161745)

Yi-Chi Lee via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 2 15:42:28 PDT 2025


https://github.com/yichi170 created https://github.com/llvm/llvm-project/pull/161745

Add `ISD::BITCAST` handling in isGuaranteedNotToBeUndefOrPoison by rescaling the demanded elements mask to match the source type, rather than conservatively testing all elements.


Solves #161512.

>From 8ad7d9a779d52d70c08e86048534a363db1ff951 Mon Sep 17 00:00:00 2001
From: Yi-Chi Lee <yichi170 at gmail.com>
Date: Thu, 2 Oct 2025 17:31:10 -0500
Subject: [PATCH] [SelectionDAG] add ISD::BITCAST handling for
 isGuaranteedNotToBeUndefOrPoison

---
 .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 95f53fe0bfdba..deab1a919b952 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -5636,6 +5636,30 @@ bool SelectionDAG::isGuaranteedNotToBeUndefOrPoison(SDValue Op,
            });
   }
 
+  case ISD::BITCAST: {
+    SDValue Src = Op.getOperand(0);
+    EVT SrcVT = Src.getValueType();
+    EVT TgtVT = Op.getValueType();
+
+    // Case 1: Scalar -> Vector, or Scalar -> Scalar
+    if (!SrcVT.isVector()) {
+      APInt DemandedSrcElts(1, 1);
+      return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
+                                              Depth + 1);
+    }
+
+    // Case 2: Vector -> Scalar
+    if (SrcVT.isVector() && !TgtVT.isVector())
+      return isGuaranteedNotToBeUndefOrPoison(Src, PoisonOnly, Depth + 1);
+
+    // Case 3: Vector -> Vector
+    unsigned NumSrcElts = SrcVT.getVectorNumElements();
+    APInt ScaledDemandedElts = APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
+
+    return isGuaranteedNotToBeUndefOrPoison(Src, ScaledDemandedElts, PoisonOnly,
+                                            Depth + 1);
+  }
+
     // TODO: Search for noundef attributes from library functions.
 
     // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.



More information about the llvm-commits mailing list