[llvm] [X86] SimplifyDemandedBitsForTargetNode: add X86ISD::BZHI handling (PR #177881)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 26 09:54:40 PST 2026


================
@@ -45456,6 +45440,63 @@ bool X86TargetLowering::SimplifyDemandedBitsForTargetNode(
 
     break;
   }
+  case X86ISD::BZHI: {
+    SDValue Op0 = Op.getOperand(0); // src
+    SDValue Op1 = Op.getOperand(1); // mask
+
+    // Rule 1: Only the bottom 8 bits of the mask are required.
+    // Track an upper bound on mask[7:0] so we can apply Rule 2.
+    uint64_t MaxMask8 = 255;
+
+    if (auto *Cst1 = dyn_cast<ConstantSDNode>(Op1)) {
+      // NOTE: SimplifyDemandedBits won't do this for constants.
+      uint64_t Val1 = Cst1->getZExtValue();
+      uint64_t MaskedVal1 = Val1 & 0xFF;
+
+      if (MaskedVal1 != Val1) {
+        SDLoc DL(Op);
+        EVT MaskVT = Op1.getValueType();
+        SDValue NewMask = TLO.DAG.getConstant(MaskedVal1, DL, MaskVT);
+        return TLO.CombineTo(
+            Op, TLO.DAG.getNode(X86ISD::BZHI, DL, VT, Op0, NewMask));
+      }
+
+      MaxMask8 = MaskedVal1;
+    } else {
+      unsigned MaskBW = Op1.getValueType().getSizeInBits();
+      APInt MaskDemand = APInt::getLowBitsSet(MaskBW, 8);
+
+      KnownBits Known1;
+      if (SimplifyDemandedBits(Op1, MaskDemand, Known1, TLO, Depth + 1))
+        return true;
+
+      // Compute an upper bound on mask[7:0].
+      KnownBits MaskBits = Known1.extractBits(8, 0);
----------------
RKSimon wrote:

trunc might be quicker

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


More information about the llvm-commits mailing list