[llvm] [GlobalISel] Combine (X >> C) << C to X & ((-1 >> C) << C) (PR #114821)
Dávid Ferenc Szabó via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 6 02:47:15 PST 2024
================
@@ -365,6 +365,13 @@ def bitreverse_lshr : GICombineRule<
MRI.getType(${amt}.getReg())}}); }]),
(apply (G_SHL $d, $val, $amt))>;
+def lsb_bits_clearing_by_shifts_to_and: GICombineRule<
+ (defs root:$root, build_fn_matchinfo:$matchinfo),
+ (match (G_LSHR $d, $src, $amt1),
+ (G_SHL $root, $d, $amt2):$root,
----------------
dfszabo wrote:
I am a bit suprised this is working for case where there is just a single G_CONSTANT. Now the only problem is that this pattern will not match the vector test case.
```diff
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -367,7 +367,9 @@ def bitreverse_lshr : GICombineRule<
def lsb_bits_clearing_by_shifts_to_and: GICombineRule<
(defs root:$root, build_fn_matchinfo:$matchinfo),
- (match (G_LSHR $d, $src, $amt1),
+ (match (G_CONSTANT $amt1, $imm1),
+ (G_CONSTANT $amt2, $imm2),
+ (G_LSHR $d, $src, $amt1),
(G_SHL $root, $d, $amt2):$root,
[{ return Helper.matchLsbClearByShifts(*${root}, ${matchinfo}); }]),
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-shifts-to-and.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-shifts-to-and.mir
index 71236fb980e3..57c96f9fb9e1 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-shifts-to-and.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-shifts-to-and.mir
@@ -124,10 +124,11 @@ body: |
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(p0) = COPY $x0
; CHECK-NEXT: [[COPY1:%[0-9]+]]:_(s32) = COPY $w1
; CHECK-NEXT: %xvec:_(<4 x s32>) = G_BUILD_VECTOR [[COPY1]](s32), [[COPY1]](s32), [[COPY1]](s32), [[COPY1]](s32)
- ; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 -4
- ; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<4 x s32>) = G_BUILD_VECTOR [[C]](s32), [[C]](s32), [[C]](s32), [[C]](s32)
- ; CHECK-NEXT: [[AND:%[0-9]+]]:_(<4 x s32>) = G_AND %xvec, [[BUILD_VECTOR]]
- ; CHECK-NEXT: G_STORE [[AND]](<4 x s32>), [[COPY]](p0) :: (store (<4 x s32>))
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 2
+ ; CHECK-NEXT: %veccst:_(<4 x s32>) = G_BUILD_VECTOR [[C]](s32), [[C]](s32), [[C]](s32), [[C]](s32)
+ ; CHECK-NEXT: [[LSHR:%[0-9]+]]:_(<4 x s32>) = G_LSHR %xvec, %veccst(<4 x s32>)
+ ; CHECK-NEXT: [[SHL:%[0-9]+]]:_(<4 x s32>) = G_SHL [[LSHR]], %veccst(<4 x s32>)
+ ; CHECK-NEXT: G_STORE [[SHL]](<4 x s32>), [[COPY]](p0) :: (store (<4 x s32>))
; CHECK-NEXT: RET_ReallyLR
%0:_(p0) = COPY $x0
%1:_(s32) = COPY $w1
```
https://github.com/llvm/llvm-project/pull/114821
More information about the llvm-commits
mailing list