[llvm] [AMDGPU] Compute GISel KnownBits for S_BFE instructions (PR #141588)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 5 02:46:49 PDT 2025


================
@@ -16502,12 +16502,68 @@ static void knownBitsForWorkitemID(const GCNSubtarget &ST,
   Known.Zero.setHighBits(llvm::countl_zero(MaxValue));
 }
 
+static void knownBitsForSBFE(const MachineInstr &MI, GISelValueTracking &VT,
+                             KnownBits &Known, const APInt &DemandedElts,
+                             unsigned BFEWidth, bool SExt, unsigned Depth) {
+  const MachineRegisterInfo &MRI = VT.getMachineFunction().getRegInfo();
+  const MachineOperand &Src1 = MI.getOperand(2);
+
+  unsigned Src1Cst = 0;
+  if (Src1.isImm()) {
+    Src1Cst = Src1.getImm();
+  } else if (Src1.isReg()) {
+    auto Cst = getIConstantVRegValWithLookThrough(Src1.getReg(), MRI);
+    if (!Cst)
+      return;
+    Src1Cst = Cst->Value.getZExtValue();
+  } else {
+    return;
+  }
+
+  // Offset is at bits [4:0] for 32 bit, [5:0] for 64 bit.
+  // Width is always [22:16].
+  const unsigned Offset =
+      Src1Cst & maskTrailingOnes<unsigned>((BFEWidth == 32) ? 5 : 6);
+  const unsigned Width = (Src1Cst >> 16) & maskTrailingOnes<unsigned>(6);
+
+  if (Width >= BFEWidth) {
+    assert(false && "Invalid S_BFE");
----------------
jayfoad wrote:

Really I don't think we should be asserting anything. I suggest just using the same mask for Width that you do for Offset. Or test some hardware to see exactly what happens when Width >= 32. Or bail out but without an assertion.

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


More information about the llvm-commits mailing list