[llvm] [InstCombine] Fold or(shl(fshr/fshl(a, b, C), C), and(b, mask)) to b (PR #200109)

Yuyang Zhang via llvm-commits llvm-commits at lists.llvm.org
Wed May 27 21:51:02 PDT 2026


https://github.com/yuyzhang512 updated https://github.com/llvm/llvm-project/pull/200109

>From b6a751163a8e966262a7aa99261fde4256c498ab Mon Sep 17 00:00:00 2001
From: yuyzhang512 <yuyzhang at amd.com>
Date: Thu, 28 May 2026 04:41:36 +0000
Subject: [PATCH] [InstCombine] Fold or(shl(fshr/fshl(a,b,C),C), and(b,mask))
 to b

Add a pattern match in visitOr to recognize the algebraic identity:
  or(shl(fshr(a, b, C), C), and(b, (1<<C)-1)) -> b
  or(shl(fshl(a, b, BW-C), C), and(b, (1<<C)-1)) -> b

The shl by C zeros out the low C bits of the funnel shift result,
leaving only the upper bits of b. The and extracts the low C bits
of b. Their or reconstructs the original value b.

SimplifyDemandedBits already handles single-use cases, but when
the funnel shift has multiple uses (e.g. AMDGPU TDM descriptor
packing), the demanded-bits analysis cannot simplify. This explicit
pattern match enables GVN to prove equality between the
reconstructed value and the original, eliminating redundant loop
phi chains in GEMM kernels.
---
 .../InstCombine/InstCombineAndOrXor.cpp       |  41 +++++
 .../or-funnel-shift-reconstruct.ll            | 156 ++++++++++++++++++
 2 files changed, 197 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/or-funnel-shift-reconstruct.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 5f9fecc7c5675..63bbdeff76b27 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3124,6 +3124,44 @@ static Instruction *matchFunnelShift(Instruction &Or, InstCombinerImpl &IC) {
   return nullptr;
 }
 
+/// Match or(shl(fshr(a, b, C), C), and(b, (1<<C)-1)) -> b
+/// Also handles fshl variant where fshl amount == BW - C.
+/// shl(fshr(a, b, C), C) clears the lower C bits of b (the 'a' contribution
+/// shifts out entirely: (a << (BW-C)) << C = a << BW = 0), and or-ing with the
+/// masked lower C bits reconstructs b.
+static Value *matchFunnelShiftReconstruction(Instruction &Or) {
+  Value *Op0 = Or.getOperand(0), *Op1 = Or.getOperand(1);
+  Type *Ty = Or.getType();
+  unsigned BW = Ty->getScalarSizeInBits();
+
+  for (unsigned I = 0; I < 2; ++I) {
+    Value *ShlOp = I == 0 ? Op0 : Op1;
+    Value *AndOp = I == 0 ? Op1 : Op0;
+
+    Value *FShVal, *B;
+    const APInt *ShlAmt, *AndMask, *FShAmt;
+    if (!match(ShlOp, m_Shl(m_Value(FShVal), m_APInt(ShlAmt))) ||
+        !match(AndOp, m_And(m_Value(B), m_APInt(AndMask))))
+      continue;
+
+    unsigned C = ShlAmt->getZExtValue();
+    if (C == 0 || C >= BW || *AndMask != APInt::getLowBitsSet(BW, C))
+      continue;
+
+    Value *A;
+    // fshr(a, b, C): shl(fshr(a, b, C), C) = (b >> C) << C
+    if (match(FShVal, m_FShr(m_Value(A), m_Specific(B), m_APInt(FShAmt))) &&
+        *FShAmt == C)
+      return B;
+
+    // fshl(a, b, BW-C): equivalent to fshr(a, b, C)
+    if (match(FShVal, m_FShl(m_Value(A), m_Specific(B), m_APInt(FShAmt))) &&
+        *FShAmt == BW - C)
+      return B;
+  }
+  return nullptr;
+}
+
 /// Attempt to combine or(zext(x),shl(zext(y),bw/2) concat packing patterns.
 static Value *matchOrConcat(Instruction &Or, InstCombiner::BuilderTy &Builder) {
   assert(Or.getOpcode() == Instruction::Or && "bswap requires an 'or'");
@@ -4160,6 +4198,9 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
   if (Instruction *Funnel = matchFunnelShift(I, *this))
     return Funnel;
 
+  if (Value *V = matchFunnelShiftReconstruction(I))
+    return replaceInstUsesWith(I, V);
+
   if (Value *Concat = matchOrConcat(I, Builder))
     return replaceInstUsesWith(I, Concat);
 
diff --git a/llvm/test/Transforms/InstCombine/or-funnel-shift-reconstruct.ll b/llvm/test/Transforms/InstCombine/or-funnel-shift-reconstruct.ll
new file mode 100644
index 0000000000000..dc84d89bdb5ac
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/or-funnel-shift-reconstruct.ll
@@ -0,0 +1,156 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+declare i32 @llvm.fshr.i32(i32, i32, i32)
+declare i32 @llvm.fshl.i32(i32, i32, i32)
+
+; or(shl(fshr(a, b, 8), 8), and(b, 255)) -> b
+define i32 @or_shl_fshr_and(i32 %a, i32 %b) {
+; CHECK-LABEL: @or_shl_fshr_and(
+; CHECK-NEXT:    ret i32 [[B:%.*]]
+;
+  %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 8)
+  %shl = shl i32 %fshr, 8
+  %mask = and i32 %b, 255
+  %or = or i32 %shl, %mask
+  ret i32 %or
+}
+
+; Commuted: or(and(b, 255), shl(fshr(a, b, 8), 8)) -> b
+define i32 @or_shl_fshr_and_commuted(i32 %a, i32 %b) {
+; CHECK-LABEL: @or_shl_fshr_and_commuted(
+; CHECK-NEXT:    ret i32 [[B:%.*]]
+;
+  %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 8)
+  %shl = shl i32 %fshr, 8
+  %mask = and i32 %b, 255
+  %or = or i32 %mask, %shl
+  ret i32 %or
+}
+
+; fshl variant: or(shl(fshl(a, b, 24), 8), and(b, 255)) -> b
+; fshl(a, b, 24) == fshr(a, b, 8) for i32
+define i32 @or_shl_fshl_and(i32 %a, i32 %b) {
+; CHECK-LABEL: @or_shl_fshl_and(
+; CHECK-NEXT:    ret i32 [[B:%.*]]
+;
+  %fshl = call i32 @llvm.fshl.i32(i32 %a, i32 %b, i32 24)
+  %shl = shl i32 %fshl, 8
+  %mask = and i32 %b, 255
+  %or = or i32 %shl, %mask
+  ret i32 %or
+}
+
+; c=16: or(shl(fshr(a, b, 16), 16), and(b, 0xFFFF)) -> b
+define i32 @or_shl_fshr_and_c16(i32 %a, i32 %b) {
+; CHECK-LABEL: @or_shl_fshr_and_c16(
+; CHECK-NEXT:    ret i32 [[B:%.*]]
+;
+  %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 16)
+  %shl = shl i32 %fshr, 16
+  %mask = and i32 %b, 65535
+  %or = or i32 %shl, %mask
+  ret i32 %or
+}
+
+; fshl c=16: fshl(a, b, 16) == fshr(a, b, 16) for i32
+define i32 @or_shl_fshl_and_c16(i32 %a, i32 %b) {
+; CHECK-LABEL: @or_shl_fshl_and_c16(
+; CHECK-NEXT:    ret i32 [[B:%.*]]
+;
+  %fshl = call i32 @llvm.fshl.i32(i32 %a, i32 %b, i32 16)
+  %shl = shl i32 %fshl, 16
+  %mask = and i32 %b, 65535
+  %or = or i32 %shl, %mask
+  ret i32 %or
+}
+
+; Multi-use: fshr has other uses — fold should still apply
+define i32 @or_shl_fshr_and_multiuse(i32 %a, i32 %b, ptr %p) {
+; CHECK-LABEL: @or_shl_fshr_and_multiuse(
+; CHECK-NEXT:    [[FSHR:%.*]] = call i32 @llvm.fshl.i32(i32 [[A:%.*]], i32 [[B:%.*]], i32 24)
+; CHECK-NEXT:    store i32 [[FSHR]], ptr [[P:%.*]], align 4
+; CHECK-NEXT:    ret i32 [[B]]
+;
+  %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 8)
+  store i32 %fshr, ptr %p
+  %shl = shl i32 %fshr, 8
+  %mask = and i32 %b, 255
+  %or = or i32 %shl, %mask
+  ret i32 %or
+}
+
+; Multi-use fshl: the key case from AMDGPU TDM descriptor packing
+define i32 @or_shl_fshl_and_multiuse_c16(i32 %a, i32 %b, ptr %p) {
+; CHECK-LABEL: @or_shl_fshl_and_multiuse_c16(
+; CHECK-NEXT:    [[FSHL:%.*]] = call i32 @llvm.fshl.i32(i32 [[A:%.*]], i32 [[B:%.*]], i32 16)
+; CHECK-NEXT:    store i32 [[FSHL]], ptr [[P:%.*]], align 4
+; CHECK-NEXT:    ret i32 [[B]]
+;
+  %fshl = call i32 @llvm.fshl.i32(i32 %a, i32 %b, i32 16)
+  store i32 %fshl, ptr %p
+  %shl = shl i32 %fshl, 16
+  %mask = and i32 %b, 65535
+  %or = or i32 %shl, %mask
+  ret i32 %or
+}
+
+; Negative: shift amounts don't match (shl=16 vs fshr=8)
+define i32 @neg_mismatch_shift(i32 %a, i32 %b) {
+; CHECK-LABEL: @neg_mismatch_shift(
+; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[B:%.*]], 8
+; CHECK-NEXT:    [[SHL:%.*]] = and i32 [[TMP1]], -65536
+; CHECK-NEXT:    [[MASK:%.*]] = and i32 [[B]], 65535
+; CHECK-NEXT:    [[OR:%.*]] = or disjoint i32 [[SHL]], [[MASK]]
+; CHECK-NEXT:    ret i32 [[OR]]
+;
+  %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 8)
+  %shl = shl i32 %fshr, 16
+  %mask = and i32 %b, 65535
+  %or = or i32 %shl, %mask
+  ret i32 %or
+}
+
+; Negative: mask doesn't match shift amount
+define i32 @neg_mismatch_mask(i32 %a, i32 %b) {
+; CHECK-LABEL: @neg_mismatch_mask(
+; CHECK-NEXT:    [[OR:%.*]] = and i32 [[B:%.*]], -129
+; CHECK-NEXT:    ret i32 [[OR]]
+;
+  %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 8)
+  %shl = shl i32 %fshr, 8
+  %mask = and i32 %b, 127
+  %or = or i32 %shl, %mask
+  ret i32 %or
+}
+
+; Negative: fshr second operand != and operand
+define i32 @neg_different_b(i32 %a, i32 %b, i32 %c) {
+; CHECK-LABEL: @neg_different_b(
+; CHECK-NEXT:    [[FSHR:%.*]] = and i32 [[B:%.*]], -256
+; CHECK-NEXT:    [[MASK:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[OR:%.*]] = or disjoint i32 [[FSHR]], [[MASK]]
+; CHECK-NEXT:    ret i32 [[OR]]
+;
+  %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 8)
+  %shl = shl i32 %fshr, 8
+  %mask = and i32 %c, 255
+  %or = or i32 %shl, %mask
+  ret i32 %or
+}
+
+; Negative: fshl amount wrong (should be BW-C=24 for C=8)
+define i32 @neg_fshl_wrong_amount(i32 %a, i32 %b) {
+; CHECK-LABEL: @neg_fshl_wrong_amount(
+; CHECK-NEXT:    [[FSHL:%.*]] = call i32 @llvm.fshl.i32(i32 [[A:%.*]], i32 [[B:%.*]], i32 8)
+; CHECK-NEXT:    [[SHL:%.*]] = shl i32 [[FSHL]], 8
+; CHECK-NEXT:    [[MASK:%.*]] = and i32 [[B]], 255
+; CHECK-NEXT:    [[OR:%.*]] = or disjoint i32 [[SHL]], [[MASK]]
+; CHECK-NEXT:    ret i32 [[OR]]
+;
+  %fshl = call i32 @llvm.fshl.i32(i32 %a, i32 %b, i32 8)
+  %shl = shl i32 %fshl, 8
+  %mask = and i32 %b, 255
+  %or = or i32 %shl, %mask
+  ret i32 %or
+}



More information about the llvm-commits mailing list