[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
Thu May 28 00:26:04 PDT 2026
https://github.com/yuyzhang512 updated https://github.com/llvm/llvm-project/pull/200109
>From f318b0df428ffafc563828c969bd7cda2ee9783b 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 shl(fshr/fshl(a,b,C),C) to and(b, -1 << C)
Add a pattern match in visitShl to recognize:
shl(fshr(a, b, C), C) -> and(b, -1 << C)
shl(fshl(a, b, BW-C), C) -> and(b, -1 << C)
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 fold
enables existing or(and,and) -> b optimizations to fire, which
in turn lets GVN eliminate redundant loop phi chains.
---
.../InstCombine/InstCombineShifts.cpp | 14 ++
.../Transforms/InstCombine/shl-fshr-fshl.ll | 179 ++++++++++++++++++
2 files changed, 193 insertions(+)
create mode 100644 llvm/test/Transforms/InstCombine/shl-fshr-fshl.ll
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 3547a79df1355..09beae79d5698 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -1188,6 +1188,20 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
}
+ // shl(fshr(a, b, C), C) --> and(b, -1 << C)
+ // shl(fshl(a, b, BW-C), C) --> and(b, -1 << C)
+ {
+ const APInt *FShAmt;
+ Value *A, *B;
+ if ((match(Op0, m_FShr(m_Value(A), m_Value(B), m_APInt(FShAmt))) &&
+ *FShAmt == ShAmtC) ||
+ (match(Op0, m_FShl(m_Value(A), m_Value(B), m_APInt(FShAmt))) &&
+ *FShAmt == BitWidth - ShAmtC)) {
+ APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC));
+ return BinaryOperator::CreateAnd(B, ConstantInt::get(Ty, Mask));
+ }
+ }
+
const APInt *C1;
if (match(Op0, m_Exact(m_Shr(m_Value(X), m_APInt(C1)))) &&
C1->ult(BitWidth)) {
diff --git a/llvm/test/Transforms/InstCombine/shl-fshr-fshl.ll b/llvm/test/Transforms/InstCombine/shl-fshr-fshl.ll
new file mode 100644
index 0000000000000..9dcd3dc9b3beb
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/shl-fshr-fshl.ll
@@ -0,0 +1,179 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+; shl(fshr(a, b, C), C) -> and(b, -1 << C)
+define i32 @shl_fshr_c8(i32 %a, i32 %b) {
+; CHECK-LABEL: @shl_fshr_c8(
+; CHECK-NEXT: [[SHL:%.*]] = and i32 [[B:%.*]], -256
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 8)
+ %shl = shl i32 %fshr, 8
+ ret i32 %shl
+}
+
+; shl(fshl(a, b, BW-C), C) -> and(b, -1 << C)
+; fshl(a, b, 24) == fshr(a, b, 8) for i32
+define i32 @shl_fshl_c8(i32 %a, i32 %b) {
+; CHECK-LABEL: @shl_fshl_c8(
+; CHECK-NEXT: [[SHL:%.*]] = and i32 [[B:%.*]], -256
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %fshl = call i32 @llvm.fshl.i32(i32 %a, i32 %b, i32 24)
+ %shl = shl i32 %fshl, 8
+ ret i32 %shl
+}
+
+; Multi-use: fshr has other uses — fold should still apply
+define i32 @shl_fshr_c8_multiuse(i32 %a, i32 %b, ptr %p) {
+; CHECK-LABEL: @shl_fshr_c8_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: [[SHL:%.*]] = and i32 [[B]], -256
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 8)
+ store i32 %fshr, ptr %p
+ %shl = shl i32 %fshr, 8
+ ret i32 %shl
+}
+
+; Multi-use fshl
+define i32 @shl_fshl_c16_multiuse(i32 %a, i32 %b, ptr %p) {
+; CHECK-LABEL: @shl_fshl_c16_multiuse(
+; 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: [[SHL:%.*]] = and i32 [[B]], -65536
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %fshl = call i32 @llvm.fshl.i32(i32 %a, i32 %b, i32 16)
+ store i32 %fshl, ptr %p
+ %shl = shl i32 %fshl, 16
+ ret i32 %shl
+}
+
+; c=16: shl(fshr(a, b, 16), 16) -> and(b, -65536)
+define i32 @shl_fshr_c16(i32 %a, i32 %b) {
+; CHECK-LABEL: @shl_fshr_c16(
+; CHECK-NEXT: [[SHL:%.*]] = and i32 [[B:%.*]], -65536
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 16)
+ %shl = shl i32 %fshr, 16
+ ret i32 %shl
+}
+
+; Boundary: c=1
+define i32 @shl_fshr_c1(i32 %a, i32 %b) {
+; CHECK-LABEL: @shl_fshr_c1(
+; CHECK-NEXT: [[SHL:%.*]] = and i32 [[B:%.*]], -2
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 1)
+ %shl = shl i32 %fshr, 1
+ ret i32 %shl
+}
+
+; Boundary: c=31
+define i32 @shl_fshr_c31(i32 %a, i32 %b) {
+; CHECK-LABEL: @shl_fshr_c31(
+; CHECK-NEXT: [[SHL:%.*]] = and i32 [[B:%.*]], -2147483648
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 31)
+ %shl = shl i32 %fshr, 31
+ ret i32 %shl
+}
+
+; Vector: <2 x i32>, fshr, c=8
+define <2 x i32> @shl_fshr_v2i32_c8(<2 x i32> %a, <2 x i32> %b) {
+; CHECK-LABEL: @shl_fshr_v2i32_c8(
+; CHECK-NEXT: [[SHL:%.*]] = and <2 x i32> [[B:%.*]], splat (i32 -256)
+; CHECK-NEXT: ret <2 x i32> [[SHL]]
+;
+ %fshr = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i32> splat (i32 8))
+ %shl = shl <2 x i32> %fshr, splat (i32 8)
+ ret <2 x i32> %shl
+}
+
+; Vector: <2 x i32>, fshl, c=8 (fshl amount = 24)
+define <2 x i32> @shl_fshl_v2i32_c8(<2 x i32> %a, <2 x i32> %b) {
+; CHECK-LABEL: @shl_fshl_v2i32_c8(
+; CHECK-NEXT: [[SHL:%.*]] = and <2 x i32> [[B:%.*]], splat (i32 -256)
+; CHECK-NEXT: ret <2 x i32> [[SHL]]
+;
+ %fshl = call <2 x i32> @llvm.fshl.v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i32> splat (i32 24))
+ %shl = shl <2 x i32> %fshl, splat (i32 8)
+ ret <2 x i32> %shl
+}
+
+; Vector multi-use: <2 x i32>, fshr, c=8
+define <2 x i32> @shl_fshr_v2i32_c8_multiuse(<2 x i32> %a, <2 x i32> %b, ptr %p) {
+; CHECK-LABEL: @shl_fshr_v2i32_c8_multiuse(
+; CHECK-NEXT: [[FSHR:%.*]] = call <2 x i32> @llvm.fshl.v2i32(<2 x i32> [[A:%.*]], <2 x i32> [[B:%.*]], <2 x i32> splat (i32 24))
+; CHECK-NEXT: store <2 x i32> [[FSHR]], ptr [[P:%.*]], align 8
+; CHECK-NEXT: [[SHL:%.*]] = and <2 x i32> [[B]], splat (i32 -256)
+; CHECK-NEXT: ret <2 x i32> [[SHL]]
+;
+ %fshr = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i32> splat (i32 8))
+ store <2 x i32> %fshr, ptr %p
+ %shl = shl <2 x i32> %fshr, splat (i32 8)
+ ret <2 x i32> %shl
+}
+
+; Negative: shift amounts don't match (shl=16 vs fshr=8)
+define i32 @neg_shl_fshr_mismatch(i32 %a, i32 %b) {
+; CHECK-LABEL: @neg_shl_fshr_mismatch(
+; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[B:%.*]], 8
+; CHECK-NEXT: [[SHL:%.*]] = and i32 [[TMP1]], -65536
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %fshr = call i32 @llvm.fshr.i32(i32 %a, i32 %b, i32 8)
+ %shl = shl i32 %fshr, 16
+ ret i32 %shl
+}
+
+; Negative: fshl amount wrong (should be BW-C=24 for C=8)
+define i32 @neg_shl_fshl_wrong_amount(i32 %a, i32 %b) {
+; CHECK-LABEL: @neg_shl_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: ret i32 [[SHL]]
+;
+ %fshl = call i32 @llvm.fshl.i32(i32 %a, i32 %b, i32 8)
+ %shl = shl i32 %fshl, 8
+ ret i32 %shl
+}
+
+; End-to-end: or(shl(fshr(a,b,8),8), and(b,255)) -> b
+; After shl(fshr) -> and, existing or(and,and) fold handles the rest
+define i32 @or_shl_fshr_and_e2e(i32 %a, i32 %b) {
+; CHECK-LABEL: @or_shl_fshr_and_e2e(
+; 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
+}
+
+; End-to-end multi-use: the AMDGPU TDM case
+define i32 @or_shl_fshr_and_e2e_multiuse(i32 %a, i32 %b, ptr %p) {
+; CHECK-LABEL: @or_shl_fshr_and_e2e_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
+}
+
+declare i32 @llvm.fshr.i32(i32, i32, i32)
+declare i32 @llvm.fshl.i32(i32, i32, i32)
+declare <2 x i32> @llvm.fshr.v2i32(<2 x i32>, <2 x i32>, <2 x i32>)
+declare <2 x i32> @llvm.fshl.v2i32(<2 x i32>, <2 x i32>, <2 x i32>)
More information about the llvm-commits
mailing list