[llvm] ceae108 - [GlobalISel] [AArch64] Support ROTR shift amount masking in SelectShiftMask (#226579)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 11 02:26:06 PDT 2026
Author: Deepak Shirke
Date: 2026-10-11T10:26:01+01:00
New Revision: ceae108847553f924985c9747b542a30068a6704
URL: https://github.com/llvm/llvm-project/commit/ceae108847553f924985c9747b542a30068a6704
DIFF: https://github.com/llvm/llvm-project/commit/ceae108847553f924985c9747b542a30068a6704.diff
LOG: [GlobalISel] [AArch64] Support ROTR shift amount masking in SelectShiftMask (#226579)
The RORV instructions already use the Shift multiclass, so they inherit
the shiftMask32/shiftMask64 ComplexPattern patterns added in the earlier
patches, giving them AND/ADD/SUB/NEG/MVN shift-amount optimization for
free.
This patch adds one additional pattern for the 32-bit rotate case, where
GlobalISel legalizes the shift amount with a zext to i64. The pattern
peeks through the zext to apply the 32-bit shift-amount masking:
(i32 (rotr GPR32, (i64 (zext (i32 shiftMask32)))))
This completes the set of shift-amount optimizations (AND mask, ADD/SUB
mod, NEG, MVN, ROTR) needed to eventually replace tryShiftAmountMod, as
tracked in #224245.
Depends on https://github.com/llvm/llvm-project/pull/226273
Added:
Modified:
llvm/lib/Target/AArch64/AArch64InstrFormats.td
llvm/test/CodeGen/AArch64/shift-mod.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/AArch64/AArch64InstrFormats.td b/llvm/lib/Target/AArch64/AArch64InstrFormats.td
index 007197285fec0..1b334a605b600 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrFormats.td
+++ b/llvm/lib/Target/AArch64/AArch64InstrFormats.td
@@ -2815,6 +2815,11 @@ multiclass Shift<bits<2> shift_type, string asm, SDNode OpNode> {
(!cast<Instruction>(NAME # "Wr") GPR32:$Rn, shiftMask32:$Rm)>;
def : Pat<(i64 (OpNode GPR64:$Rn, shiftMask64:$Rm)),
(!cast<Instruction>(NAME # "Xr") GPR64:$Rn, shiftMask64:$Rm)>;
+
+ // For 32-bit rotates the shift amount is legalized with a zext to i64;
+ // peek through it and apply the 32-bit shift-amount masking.
+ def : Pat<(i32 (OpNode GPR32:$Rn, (i64 (zext (i32 shiftMask32:$Rm))))),
+ (!cast<Instruction>(NAME # "Wr") GPR32:$Rn, shiftMask32:$Rm)>;
}
class ShiftAlias<string asm, Instruction inst, RegisterClass regtype>
diff --git a/llvm/test/CodeGen/AArch64/shift-mod.ll b/llvm/test/CodeGen/AArch64/shift-mod.ll
index e631ee6d53df8..2b00fe09ab322 100644
--- a/llvm/test/CodeGen/AArch64/shift-mod.ll
+++ b/llvm/test/CodeGen/AArch64/shift-mod.ll
@@ -336,3 +336,26 @@ define i64 @lshr_i64_sub63(i64 %x, i64 %amt) {
%r = lshr i64 %x, %sub
ret i64 %r
}
+
+; Test rotate with masked/modified shift amount.
+define i32 @rotr_i32_and(i32 %x, i32 %amt) {
+; CHECK-LABEL: rotr_i32_and:
+; CHECK: // %bb.0:
+; CHECK-NEXT: ror w0, w0, w1
+; CHECK-NEXT: ret
+ %m = and i32 %amt, 31
+ %r = call i32 @llvm.fshr.i32(i32 %x, i32 %x, i32 %m)
+ ret i32 %r
+}
+
+define i32 @rotr_i32_sub(i32 %x, i32 %amt) {
+; CHECK-LABEL: rotr_i32_sub:
+; CHECK: // %bb.0:
+; CHECK-NEXT: neg w8, w1
+; CHECK-NEXT: ror w0, w0, w8
+; CHECK-NEXT: ret
+ %s = sub i32 64, %amt
+ %r = call i32 @llvm.fshr.i32(i32 %x, i32 %x, i32 %s)
+ ret i32 %r
+}
+
More information about the llvm-commits
mailing list