[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
Tue Sep 8 21:14:06 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:

I have added it, there are no test changes but the code path is executed in atleast one test.

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


More information about the llvm-commits mailing list