Skip to content

[RISCV] Limit (and (sra x, c2), c1) -> (srli (srai x, c2-c3), c3) isel in some cases. #102034

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 3 commits into from
Aug 7, 2024

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Aug 5, 2024

If x is a shl by 32 and c1 is an simm12, we would prefer to use a SRAIW+ANDI. This prevents selecting the slli to a separate slli instruction.

Fixes regression from #101868

…l in some cases.

If x is a shl by 32 and c1 is an simm12, we would prefer to use a
SRAIW+ANDI. This prevents selecting the slli to a separate slli
instruction.
@llvmbot
Copy link
Member

llvmbot commented Aug 5, 2024

@llvm/pr-subscribers-backend-risc-v

Author: Craig Topper (topperc)

Changes

If x is a shl by 32 and c1 is an simm12, we would prefer to use a SRAIW+ANDI. This prevents selecting the slli to a separate slli instruction.


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

2 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp (+5-1)
  • (modified) llvm/test/CodeGen/RISCV/selectcc-to-shiftand.ll (+2-3)
diff --git a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
index 3dcfeecec1e75..b140d72e36fed 100644
--- a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
@@ -1460,7 +1460,11 @@ void RISCVDAGToDAGISel::Select(SDNode *Node) {
 
       SDValue X = N0.getOperand(0);
 
-      if (isMask_64(C1)) {
+      // Prefer SRAIW + ANDI when possible.
+      bool Skip = C2 > 32 && IsC1ANDI && X.getOpcode() == ISD::SHL &&
+                  isa<ConstantSDNode>(X.getOperand(1)) &&
+                  X.getConstantOperandVal(1) == 32;
+      if (isMask_64(C1) && !Skip) {
         unsigned Leading = XLen - llvm::bit_width(C1);
         if (C2 > Leading) {
           SDNode *SRAI = CurDAG->getMachineNode(
diff --git a/llvm/test/CodeGen/RISCV/selectcc-to-shiftand.ll b/llvm/test/CodeGen/RISCV/selectcc-to-shiftand.ll
index 4749cc656693c..0d96fbfa81279 100644
--- a/llvm/test/CodeGen/RISCV/selectcc-to-shiftand.ll
+++ b/llvm/test/CodeGen/RISCV/selectcc-to-shiftand.ll
@@ -276,9 +276,8 @@ define i64 @sraiw_andi(i32 signext %0, i32 signext %1) nounwind {
 ; RV64-LABEL: sraiw_andi:
 ; RV64:       # %bb.0: # %entry
 ; RV64-NEXT:    add a0, a0, a1
-; RV64-NEXT:    slli a0, a0, 32
-; RV64-NEXT:    srai a0, a0, 2
-; RV64-NEXT:    srli a0, a0, 61
+; RV64-NEXT:    sraiw a0, a0, 31
+; RV64-NEXT:    andi a0, a0, 7
 ; RV64-NEXT:    ret
 entry:
   %3 = add i32 %0, %1

dtcxzyw added a commit to dtcxzyw/llvm-codegen-benchmark that referenced this pull request Aug 6, 2024
@dtcxzyw
Copy link
Member

dtcxzyw commented Aug 6, 2024

Can you rebase this patch over cfd13cb?

/home/dtcxzyw/WorkSpace/Projects/compilers/llvm-project/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp: In member function ‘virtual void llvm::RISCVDAGToDAGISel::Select(llvm::SDNode*)’:
/home/dtcxzyw/WorkSpace/Projects/compilers/llvm-project/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp:1465: error: ‘IsC1ANDI’ was not declared in this scope
 1465 |       bool Skip = C2 > 32 && IsC1ANDI && X.getOpcode() == ISD::SHL &&
      | 
ninja: build stopped: subcommand failed.

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you!

@topperc topperc merged commit 15895da into llvm:main Aug 7, 2024
7 checks passed
@topperc topperc deleted the pr/sra-regression branch August 7, 2024 03:27
@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 8, 2024

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-expensive-checks-debian running on gribozavr4 while building llvm at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/16/builds/3026

Here is the relevant piece of the build log for the reference:

Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'LLVM :: TableGen/SubtargetFeatureUniqueNames.td' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: not /b/1/llvm-clang-x86_64-expensive-checks-debian/build/bin/llvm-tblgen -gen-subtarget -I /b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/../../include /b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td 2>&1 | /b/1/llvm-clang-x86_64-expensive-checks-debian/build/bin/FileCheck /b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td -DFILE=/b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td
+ not /b/1/llvm-clang-x86_64-expensive-checks-debian/build/bin/llvm-tblgen -gen-subtarget -I /b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/../../include /b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td
+ /b/1/llvm-clang-x86_64-expensive-checks-debian/build/bin/FileCheck /b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td -DFILE=/b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td
/b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td:10:11: error: CHECK: expected string not found in input
// CHECK: [[FILE]]:[[@LINE+2]]:5: error: Feature `NameA` already defined.
          ^
<stdin>:1:1: note: scanning from here
/b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td:8:5: error: Feature `NameA` already defined.
^
<stdin>:1:1: note: with "FILE" equal to "/b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames\\.td"
/b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td:8:5: error: Feature `NameA` already defined.
^
<stdin>:1:1: note: with "@LINE+2" equal to "12"
/b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td:8:5: error: Feature `NameA` already defined.
^
<stdin>:1:110: note: possible intended match here
/b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td:8:5: error: Feature `NameA` already defined.
                                                                                                             ^

Input file: <stdin>
Check file: /b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td

-dump-input=help explains the following input dump.

Input was:
<<<<<<
            1: /b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td:8:5: error: Feature `NameA` already defined. 
check:10'0     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
check:10'1                                                                                                                                                                 with "FILE" equal to "/b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames\\.td"
check:10'2                                                                                                                                                                 with "@LINE+2" equal to "12"
check:10'3                                                                                                                  ?                                              possible intended match
            2: def FeatureA : SubtargetFeature<"NameA", "", "", "">; 
check:10'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            3:  ^ 
check:10'0     ~~~
            4: /b/1/llvm-clang-x86_64-expensive-checks-debian/llvm-project/llvm/test/TableGen/SubtargetFeatureUniqueNames.td:12:5: note: Previous definition here. 
check:10'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            5: def FeatureB : SubtargetFeature<"NameA", "", "", "">; 
check:10'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            6:  ^ 
check:10'0     ~~~
>>>>>>

--

...

tclin914 added a commit that referenced this pull request Apr 22, 2025
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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.

4 participants