[llvm] [SDAG][ComputeNumSignBits] Look through undefined high bits for EXTRACT_VECTOR_ELT in a BUILD_VECTOR (PR #218099)

Usman Nadeem via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 2 09:55:54 PDT 2026


================
@@ -4999,6 +4999,28 @@ unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
       if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
         APInt T = C->getAPIntValue().trunc(VTBits);
         Tmp2 = T.getNumSignBits();
+      } else if (SrcOp.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
+                 SrcOp.getOperand(0).getScalarValueSizeInBits() >= VTBits) {
+        // EXTRACT_VECTOR_ELT can extend the value with high bits undefined. If
+        // this BUILD_VECTOR truncates those undefined bits we can just look
+        // through the SrcOp and query the vector directly.
+        SDValue InVec = SrcOp.getOperand(0);
+        EVT InVecVT = InVec.getValueType();
+        // Not yet implemented for scalable vectors.
+        if (InVecVT.isScalableVector())
+          return 1;
----------------
UsmanNadeem wrote:

With regards to `Tmp2 = 1` since we do `Tmp = std::min(Tmp, Tmp2);`  before the loop ends and then `return Tmp;` the result would still be 1. So just returning early is same behaviour.

Do you think handling handling scalable vectors using `DemandedSrcElts = APInt(1,1)` will be beneficial here. 

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


More information about the llvm-commits mailing list