Skip to content

[RISCV] Don't select sh{1,2,3}add if shl doesn't have one use #143351

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 2 commits into from
Jun 10, 2025

Conversation

tclin914
Copy link
Contributor

@tclin914 tclin914 commented Jun 9, 2025

Try to fix #130829 (review). There's no benefit if shl doesn't have one use.

There's no benefit if shl doesn't have one use.
@llvmbot
Copy link
Member

llvmbot commented Jun 9, 2025

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

Author: Jim Lin (tclin914)

Changes

Try to fix #130829 (review). There's no benefit if shl doesn't have one use.


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

2 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVISelLowering.cpp (+3)
  • (modified) llvm/test/CodeGen/RISCV/reassoc-shl-addi-add.ll (+11-12)
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 1cbd3f4233eee..4cf9a9fb7f969 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -14887,6 +14887,9 @@ static SDValue combineShlAddIAddImpl(SDNode *N, SDValue AddI, SDValue Other,
   if (!sd_match(SHLVal, m_BinOp(ISD::SHL, m_Value(), m_ConstInt(VShift))))
     return SDValue();
 
+  if (!SHLVal.hasOneUse())
+    return SDValue();
+
   if (VShift.slt(1) || VShift.sgt(3))
     return SDValue();
 
diff --git a/llvm/test/CodeGen/RISCV/reassoc-shl-addi-add.ll b/llvm/test/CodeGen/RISCV/reassoc-shl-addi-add.ll
index 88ab1c0c3eaef..baa9011802dac 100644
--- a/llvm/test/CodeGen/RISCV/reassoc-shl-addi-add.ll
+++ b/llvm/test/CodeGen/RISCV/reassoc-shl-addi-add.ll
@@ -8,10 +8,10 @@ declare i32 @callee(i32 noundef, i32 noundef, i32 noundef, i32 noundef)
 define void @t1(i32 noundef %a, i32 noundef %b, i32 noundef %c, i32 noundef %d) #0 {
 ; CHECK-LABEL: t1:
 ; CHECK:       # %bb.0: # %entry
-; CHECK-NEXT:    sh2add a2, a0, a2
-; CHECK-NEXT:    sh2add a1, a0, a1
-; CHECK-NEXT:    addi a1, a1, 45
-; CHECK-NEXT:    addi a2, a2, 45
+; CHECK-NEXT:    slli a4, a0, 2
+; CHECK-NEXT:    addi a4, a4, 45
+; CHECK-NEXT:    add a1, a4, a1
+; CHECK-NEXT:    add a2, a4, a2
 ; CHECK-NEXT:    sh2add a3, a0, a3
 ; CHECK-NEXT:    mv a0, a1
 ; CHECK-NEXT:    tail callee
@@ -133,12 +133,11 @@ entry:
 define void @t8(i32 noundef %a, i32 noundef %b, i32 noundef %c, i32 noundef %d) #0 {
 ; CHECK-LABEL: t8:
 ; CHECK:       # %bb.0: # %entry
-; CHECK-NEXT:    sh3add a2, a0, a2
-; CHECK-NEXT:    sh3add a1, a0, a1
 ; CHECK-NEXT:    lui a4, 1
 ; CHECK-NEXT:    addi a4, a4, 1307
-; CHECK-NEXT:    add a1, a1, a4
-; CHECK-NEXT:    add a2, a2, a4
+; CHECK-NEXT:    sh3add a4, a0, a4
+; CHECK-NEXT:    add a1, a4, a1
+; CHECK-NEXT:    add a2, a4, a2
 ; CHECK-NEXT:    sh3add a3, a0, a3
 ; CHECK-NEXT:    mv a0, a1
 ; CHECK-NEXT:    tail callee
@@ -155,10 +154,10 @@ entry:
 define void @t9(i32 noundef %a, i32 noundef %b, i32 noundef %c, i32 noundef %d) #0 {
 ; CHECK-LABEL: t9:
 ; CHECK:       # %bb.0: # %entry
-; CHECK-NEXT:    sh2add a2, a0, a2
-; CHECK-NEXT:    sh2add a1, a0, a1
-; CHECK-NEXT:    addi a1, a1, -42
-; CHECK-NEXT:    addi a2, a2, -42
+; CHECK-NEXT:    slli a4, a0, 2
+; CHECK-NEXT:    addi a4, a4, -42
+; CHECK-NEXT:    add a1, a4, a1
+; CHECK-NEXT:    add a2, a4, a2
 ; CHECK-NEXT:    sh2add a3, a0, a3
 ; CHECK-NEXT:    mv a0, a1
 ; CHECK-NEXT:    tail callee

Copy link
Member

@mshockwave mshockwave left a comment

Choose a reason for hiding this comment

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

@stefanp-synopsys do you have any comment on #130829 (review) ?

Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@wangpc-pp wangpc-pp left a comment

Choose a reason for hiding this comment

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

LGTM in general.

@tclin914 tclin914 merged commit 6881c7d into llvm:main Jun 10, 2025
5 of 7 checks passed
@tclin914 tclin914 deleted the xandesperf-reassoc branch June 10, 2025 05:34
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 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.

5 participants