Skip to content

[AArch64] Funnel Shift now uses rev32/rev64 instructions #136707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 23, 2025

Conversation

jyli0116
Copy link
Contributor

Fixes #130469

Now uses REV32/REV64 instructions to complete operation.

New Output:

G1:
        rev64   v0.4s, v0.4s
        ret
G2:
        rev32   v0.8h, v0.8h
        ret
G3:
        rev16   v0.16b, v0.16b
        ret
G4:
        rev32   v0.4h, v0.4h
        ret
G5:
        rev16   v0.8b, v0.8b
        ret

Old Output:

G1:
        shl     v1.2d, v0.2d, #32
        usra    v1.2d, v0.2d, #32
        mov     v0.16b, v1.16b
        ret

G2:
        shl     v1.4s, v0.4s, #16
        usra    v1.4s, v0.4s, #16
        mov     v0.16b, v1.16b
        ret

G3:
        rev16   v0.16b, v0.16b
        ret

G4:
        shl     v1.2s, v0.2s, #16
        usra    v1.2s, v0.2s, #16
        fmov    d0, d1
        ret

G5:
        rev16   v0.8b, v0.8b
        ret```

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Apr 22, 2025

@llvm/pr-subscribers-backend-aarch64

Author: None (jyli0116)

Changes

Fixes #130469

Now uses REV32/REV64 instructions to complete operation.

New Output:

G1:
        rev64   v0.4s, v0.4s
        ret
G2:
        rev32   v0.8h, v0.8h
        ret
G3:
        rev16   v0.16b, v0.16b
        ret
G4:
        rev32   v0.4h, v0.4h
        ret
G5:
        rev16   v0.8b, v0.8b
        ret

Old Output:

G1:
        shl     v1.2d, v0.2d, #<!-- -->32
        usra    v1.2d, v0.2d, #<!-- -->32
        mov     v0.16b, v1.16b
        ret

G2:
        shl     v1.4s, v0.4s, #<!-- -->16
        usra    v1.4s, v0.4s, #<!-- -->16
        mov     v0.16b, v1.16b
        ret

G3:
        rev16   v0.16b, v0.16b
        ret

G4:
        shl     v1.2s, v0.2s, #<!-- -->16
        usra    v1.2s, v0.2s, #<!-- -->16
        fmov    d0, d1
        ret

G5:
        rev16   v0.8b, v0.8b
        ret```

---
Full diff: https://github.com/llvm/llvm-project/pull/136707.diff


2 Files Affected:

- (modified) llvm/lib/Target/AArch64/AArch64InstrInfo.td (+8) 
- (added) llvm/test/CodeGen/AArch64/aarch64-fshl-rev.ll (+29) 


``````````diff
diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.td b/llvm/lib/Target/AArch64/AArch64InstrInfo.td
index a060a2f597ccd..e7aa9c53a8343 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.td
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.td
@@ -5679,6 +5679,14 @@ def : Pat<(v4i32 (bswap (v4i32 V128:$Rn))),
 def : Pat<(v2i64 (bswap (v2i64 V128:$Rn))),
           (v2i64 (REV64v16i8 (v2i64 V128:$Rn)))>;
 
+// Patterns for funnel shifts to be matched to equivalent REV instructions
+def : Pat<(v2i64 (or (v2i64 (AArch64vshl (v2i64 V128:$Rn), (i32 32))), (v2i64 (AArch64vlshr (v2i64 V128:$Rn), (i32 32))))), 
+          (v2i64 (REV64v4i32 (v2i64 V128:$Rn)))>;
+def : Pat<(v4i32 (or (v4i32 (AArch64vshl (v4i32 V128:$Rn), (i32 16))), (v4i32 (AArch64vlshr (v4i32 V128:$Rn), (i32 16))))), 
+          (v4i32 (REV32v8i16 (v4i32 V128:$Rn)))>;
+def : Pat<(v2i32 (or (v2i32 (AArch64vshl (v2i32 V64:$Rn), (i32 16))), (v2i32 (AArch64vlshr (v2i32 V64:$Rn), (i32 16))))), 
+          (v2i32 (REV32v4i16 (v2i32 V64:$Rn)))>;
+
 //===----------------------------------------------------------------------===//
 // Advanced SIMD three vector instructions.
 //===----------------------------------------------------------------------===//
diff --git a/llvm/test/CodeGen/AArch64/aarch64-fshl-rev.ll b/llvm/test/CodeGen/AArch64/aarch64-fshl-rev.ll
new file mode 100644
index 0000000000000..503bc307e7777
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/aarch64-fshl-rev.ll
@@ -0,0 +1,29 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=aarch64 %s -o - | FileCheck %s
+
+define <2 x i64> @fhsl_to_rev2i64(<2 x i64> %r) {
+; CHECK-LABEL: fhsl_to_rev2i64:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    rev64 v0.4s, v0.4s
+; CHECK-NEXT:    ret
+    %or = tail call <2 x i64> @llvm.fshl.v2i64(<2 x i64> %r, <2 x i64> %r, <2 x i64> splat (i64 32))
+    ret <2 x i64> %or
+}
+
+define <4 x i32> @fshl_to_rev4i32(<4 x i32> %r) {
+; CHECK-LABEL: fshl_to_rev4i32:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    rev32 v0.8h, v0.8h
+; CHECK-NEXT:    ret
+    %or = tail call <4 x i32> @llvm.fshl.v4i32(<4 x i32> %r, <4 x i32> %r, <4 x i32> splat (i32 16))
+    ret <4 x i32> %or
+}
+
+define <2 x i32> @fshl_to_rev2i32(<2 x i32> %r) {
+; CHECK-LABEL: fshl_to_rev2i32:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    rev32 v0.4h, v0.4h
+; CHECK-NEXT:    ret
+    %or = tail call <2 x i32> @llvm.fshl.v2i32(<2 x i32> %r, <2 x i32> %r, <2 x i32> splat (i32 16))
+    ret <2 x i32> %or
+}

@@ -5679,6 +5679,14 @@ def : Pat<(v4i32 (bswap (v4i32 V128:$Rn))),
def : Pat<(v2i64 (bswap (v2i64 V128:$Rn))),
(v2i64 (REV64v16i8 (v2i64 V128:$Rn)))>;

// Patterns for funnel shifts to be matched to equivalent REV instructions
def : Pat<(v2i64 (or (v2i64 (AArch64vshl (v2i64 V128:$Rn), (i32 32))), (v2i64 (AArch64vlshr (v2i64 V128:$Rn), (i32 32))))),
Copy link
Collaborator

@davemgreen davemgreen Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you split this line to be a little shorter? It doesn't need to be too short, but maybe making the two (v2i64 (AArch64vshl/AArch64vlshr .. will help keep it readable.

Copy link
Collaborator

@davemgreen davemgreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AARch64 can be changed to AArch64 in the title too, but otherwise LGTM.

@jyli0116 jyli0116 changed the title [AARch64] Funnel Shift now uses rev32/rev64 instructions [AArch64] Funnel Shift now uses rev32/rev64 instructions Apr 23, 2025
Copy link
Collaborator

@c-rhodes c-rhodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple of minor comments but otherwise LGTM, cheers

@jyli0116 jyli0116 force-pushed the fshl_to_REV_instr branch from 023dd34 to ac5d7a3 Compare April 23, 2025 10:34
@jyli0116 jyli0116 requested review from c-rhodes and davemgreen April 23, 2025 11:12
Comment on lines 5692 to 5693
def : Pat<(v2i64 (or (v2i64 (AArch64vshl (v2i64 V128:$Rn), (i32 32))),
(v2i64 (AArch64vlshr (v2i64 V128:$Rn), (i32 32))))),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a small nit, but can we do it like:

def : Pat<(v2i64 (or (v2i64 (AArch64vshl (v2i64 V128:$Rn), (i32 32))), 
                     (v2i64 (AArch64vlshr (v2i64 V128:$Rn), (i32 32))))),

Copy link
Collaborator

@davemgreen davemgreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@davemgreen davemgreen merged commit 00934be into llvm:main Apr 23, 2025
6 of 9 checks passed
Copy link

@jyli0116 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@jyli0116 jyli0116 deleted the fshl_to_REV_instr branch April 23, 2025 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[AArch64] does not use rev32/rev64 instructions, resulting in redundant shift operations
4 participants