Skip to content
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

[RISC-V][ISel] Remove redundant czero.eqz like 'czero.eqz a0, a0, a0' #90208

Merged
merged 2 commits into from
Apr 28, 2024

Conversation

zengdage
Copy link
Contributor

In RISC-V ISel,the instruction czero.eqz a0, a0, a0 is meaningless,I think it need to be removed. So I upload the patch to do this job. When the condition is setcc falseValue, 0, seteq, the select instruction don't need to add a czero.eqz instruciton.

@llvmbot
Copy link
Collaborator

llvmbot commented Apr 26, 2024

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

Author: Zhijin Zeng (zengdage)

Changes

In RISC-V ISel,the instruction czero.eqz a0, a0, a0 is meaningless,I think it need to be removed. So I upload the patch to do this job. When the condition is setcc falseValue, 0, seteq, the select instruction don't need to add a czero.eqz instruciton.


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

2 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVISelLowering.cpp (+11)
  • (modified) llvm/test/CodeGen/RISCV/select.ll (+55)
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 769c465d56f984..422f6610ce8126 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -7503,6 +7503,17 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
       return DAG.getNode(ISD::ADD, DL, VT, CMOV, RHSVal);
     }
 
+    // c = setcc f, 0, seteq
+    // (select c, t, f) -> (or f, (czero_nez t, f))
+    if (CondV.getOpcode() == ISD::SETCC &&
+        ISD::SETEQ == cast<CondCodeSDNode>(CondV.getOperand(2))->get()) {
+      SDValue LHS = CondV.getOperand(0);
+      SDValue RHS = CondV.getOperand(1);
+      if (isNullConstant(RHS) && LHS == FalseV)
+        return DAG.getNode(
+            ISD::OR, DL, VT, FalseV,
+            DAG.getNode(RISCVISD::CZERO_NEZ, DL, VT, TrueV, LHS));
+    }
     // (select c, t, f) -> (or (czero_eqz t, c), (czero_nez f, c))
     // Unless we have the short forward branch optimization.
     if (!Subtarget.hasConditionalMoveFusion())
diff --git a/llvm/test/CodeGen/RISCV/select.ll b/llvm/test/CodeGen/RISCV/select.ll
index e07e52091e9e71..6efbbe0baf5375 100644
--- a/llvm/test/CodeGen/RISCV/select.ll
+++ b/llvm/test/CodeGen/RISCV/select.ll
@@ -1858,3 +1858,58 @@ define i32 @select_cst6(i1 zeroext %cond) {
   %ret = select i1 %cond, i32 2049, i32 2047
   ret i32 %ret
 }
+
+@select_redundant_czero_eqz_data = global i32 0, align 4
+
+define void @select_redundant_czero_eqz(ptr %0, ptr %1) {
+; RV32IM-LABEL: select_redundant_czero_eqz:
+; RV32IM:       # %bb.0:
+; RV32IM-NEXT:    bnez a0, .LBB49_2
+; RV32IM-NEXT:  # %bb.1:
+; RV32IM-NEXT:    lui a0, %hi(select_redundant_czero_eqz_data)
+; RV32IM-NEXT:    addi a0, a0, %lo(select_redundant_czero_eqz_data)
+; RV32IM-NEXT:  .LBB49_2:
+; RV32IM-NEXT:    sw a0, 0(a1)
+; RV32IM-NEXT:    ret
+;
+; RV64IM-LABEL: select_redundant_czero_eqz:
+; RV64IM:       # %bb.0:
+; RV64IM-NEXT:    bnez a0, .LBB49_2
+; RV64IM-NEXT:  # %bb.1:
+; RV64IM-NEXT:    lui a0, %hi(select_redundant_czero_eqz_data)
+; RV64IM-NEXT:    addi a0, a0, %lo(select_redundant_czero_eqz_data)
+; RV64IM-NEXT:  .LBB49_2:
+; RV64IM-NEXT:    sd a0, 0(a1)
+; RV64IM-NEXT:    ret
+;
+; RV64IMXVTCONDOPS-LABEL: select_redundant_czero_eqz:
+; RV64IMXVTCONDOPS:       # %bb.0:
+; RV64IMXVTCONDOPS-NEXT:    lui a2, %hi(select_redundant_czero_eqz_data)
+; RV64IMXVTCONDOPS-NEXT:    addi a2, a2, %lo(select_redundant_czero_eqz_data)
+; RV64IMXVTCONDOPS-NEXT:    vt.maskcn a2, a2, a0
+; RV64IMXVTCONDOPS-NEXT:    or a0, a0, a2
+; RV64IMXVTCONDOPS-NEXT:    sd a0, 0(a1)
+; RV64IMXVTCONDOPS-NEXT:    ret
+;
+; RV32IMZICOND-LABEL: select_redundant_czero_eqz:
+; RV32IMZICOND:       # %bb.0:
+; RV32IMZICOND-NEXT:    lui a2, %hi(select_redundant_czero_eqz_data)
+; RV32IMZICOND-NEXT:    addi a2, a2, %lo(select_redundant_czero_eqz_data)
+; RV32IMZICOND-NEXT:    czero.nez a2, a2, a0
+; RV32IMZICOND-NEXT:    or a0, a0, a2
+; RV32IMZICOND-NEXT:    sw a0, 0(a1)
+; RV32IMZICOND-NEXT:    ret
+;
+; RV64IMZICOND-LABEL: select_redundant_czero_eqz:
+; RV64IMZICOND:       # %bb.0:
+; RV64IMZICOND-NEXT:    lui a2, %hi(select_redundant_czero_eqz_data)
+; RV64IMZICOND-NEXT:    addi a2, a2, %lo(select_redundant_czero_eqz_data)
+; RV64IMZICOND-NEXT:    czero.nez a2, a2, a0
+; RV64IMZICOND-NEXT:    or a0, a0, a2
+; RV64IMZICOND-NEXT:    sd a0, 0(a1)
+; RV64IMZICOND-NEXT:    ret
+  %3 = icmp eq ptr %0, null
+  %4 = select i1 %3, ptr @select_redundant_czero_eqz_data, ptr %0
+  store ptr %4, ptr %1, align 8
+  ret void
+}

@zengdage zengdage force-pushed the remove-reducant-czeroeqz branch 2 times, most recently from a1e96bf to ced5571 Compare April 26, 2024 14:57
Signed-off-by: Zhijin Zeng <zhijin.zeng@spacemit.com>
Signed-off-by: Zhijin Zeng <zhijin.zeng@spacemit.com>
@zengdage
Copy link
Contributor Author

Sorry, is anyone know why the CI buildkite failed ? I can't run the build command ./.ci/monolithic-linux.sh "bolt;clang;clang-tools-extra;flang;lld;llvm;mlir;polly" "check-bolt check-clang check-clang-tools check-flang check-lld check-llvm check-mlir check-polly" in my pc, and don't understand the error log as follow:

FAIL: BOLT :: RISCV/unnamed-sym-no-entry.c (55 of 431)

******************** TEST 'BOLT :: RISCV/unnamed-sym-no-entry.c' FAILED ********************
Exit Code: 1
Command Output (stderr):
RUN: at line 6: /usr/bin/clang  --target=x86_64-linux -fuse-ld=lld -Wl,--unresolved-symbols=ignore-all --target=riscv64 -nostdlib -ffreestanding -Wl,--emit-relocs -g -Wl,-q -o /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/tools/bolt/test/RISCV/Output/unnamed-sym-no-entry.c.tmp /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c

+ /usr/bin/clang --target=x86_64-linux -fuse-ld=lld -Wl,--unresolved-symbols=ignore-all --target=riscv64 -nostdlib -ffreestanding -Wl,--emit-relocs -g -Wl,-q -o /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/tools/bolt/test/RISCV/Output/unnamed-sym-no-entry.c.tmp /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c

/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/bin/llvm-readelf -s /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/tools/bolt/test/RISCV/Output/unnamed-sym-no-entry.c.tmp | /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/bin/FileCheck /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c --check-prefix=CHECK-ELF

/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/bin/FileCheck /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c --check-prefix=CHECK-ELF

/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/bin/llvm-readelf -s /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/tools/bolt/test/RISCV/Output/unnamed-sym-no-entry.c.tmp

/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c:11:19: error: CHECK-ELF-DAG: expected string not found in input

CHECK-ELF-DAG: [[#%x,START]] {{.*}} NOTYPE LOCAL DEFAULT [[#SECTION]] .L0 {{$}}

^
<stdin>:1:1: note: scanning from here

<stdin>:1:1: note: with "%x,START" equal to "11120"

<stdin>:1:1: note: with "SECTION" equal to "1"

<stdin>:4:20: note: possible intended match here

0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 
Input file: <stdin>

Check file: /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c

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

Input was:
<<<<<<
1:  
dag:11'0     X error: no match found
dag:11'1      with "%x,START" equal to "11120"
dag:11'2       with "SECTION" equal to "1"
2: Symbol table '.symtab' contains 32 entries: 
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3:  Num: Value Size Type Bind Vis Ndx Name 
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4:  0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND  
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dag:11'3                        ?                              possible intended match
5:  1: 0000000000000000 0 FILE LOCAL DEFAULT ABS unnamed-sym-no-entry.c 
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6:  2: 0000000000011120 0 NOTYPE LOCAL DEFAULT 1  
11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7:  3: 0000000000011120 0 NOTYPE LOCAL DEFAULT 1  
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8:  4: 0000000000011120 0 NOTYPE LOCAL DEFAULT 1  
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9:  5: 0000000000011122 0 NOTYPE LOCAL DEFAULT 1  
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
********************

@dtcxzyw
Copy link
Member

dtcxzyw commented Apr 27, 2024

Sorry, is anyone know why the CI buildkite failed ? I can't run the build command ./.ci/monolithic-linux.sh "bolt;clang;clang-tools-extra;flang;lld;llvm;mlir;polly" "check-bolt check-clang check-clang-tools check-flang check-lld check-llvm check-mlir check-polly" in my pc, and don't understand the error log as follow:

FAIL: BOLT :: RISCV/unnamed-sym-no-entry.c (55 of 431)

******************** TEST 'BOLT :: RISCV/unnamed-sym-no-entry.c' FAILED ********************
Exit Code: 1
Command Output (stderr):
RUN: at line 6: /usr/bin/clang  --target=x86_64-linux -fuse-ld=lld -Wl,--unresolved-symbols=ignore-all --target=riscv64 -nostdlib -ffreestanding -Wl,--emit-relocs -g -Wl,-q -o /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/tools/bolt/test/RISCV/Output/unnamed-sym-no-entry.c.tmp /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c

+ /usr/bin/clang --target=x86_64-linux -fuse-ld=lld -Wl,--unresolved-symbols=ignore-all --target=riscv64 -nostdlib -ffreestanding -Wl,--emit-relocs -g -Wl,-q -o /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/tools/bolt/test/RISCV/Output/unnamed-sym-no-entry.c.tmp /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c

/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/bin/llvm-readelf -s /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/tools/bolt/test/RISCV/Output/unnamed-sym-no-entry.c.tmp | /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/bin/FileCheck /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c --check-prefix=CHECK-ELF

/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/bin/FileCheck /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c --check-prefix=CHECK-ELF

/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/bin/llvm-readelf -s /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/build/tools/bolt/test/RISCV/Output/unnamed-sym-no-entry.c.tmp

/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c:11:19: error: CHECK-ELF-DAG: expected string not found in input

CHECK-ELF-DAG: [[#%x,START]] {{.*}} NOTYPE LOCAL DEFAULT [[#SECTION]] .L0 {{$}}

^
<stdin>:1:1: note: scanning from here

<stdin>:1:1: note: with "%x,START" equal to "11120"

<stdin>:1:1: note: with "SECTION" equal to "1"

<stdin>:4:20: note: possible intended match here

0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 
Input file: <stdin>

Check file: /var/lib/buildkite-agent/builds/linux-56-59b8f5d88-6mhbj-1/llvm-project/github-pull-requests/bolt/test/RISCV/unnamed-sym-no-entry.c

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

Input was:
<<<<<<
1:  
dag:11'0     X error: no match found
dag:11'1      with "%x,START" equal to "11120"
dag:11'2       with "SECTION" equal to "1"
2: Symbol table '.symtab' contains 32 entries: 
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3:  Num: Value Size Type Bind Vis Ndx Name 
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4:  0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND  
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dag:11'3                        ?                              possible intended match
5:  1: 0000000000000000 0 FILE LOCAL DEFAULT ABS unnamed-sym-no-entry.c 
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6:  2: 0000000000011120 0 NOTYPE LOCAL DEFAULT 1  
11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7:  3: 0000000000011120 0 NOTYPE LOCAL DEFAULT 1  
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8:  4: 0000000000011120 0 NOTYPE LOCAL DEFAULT 1  
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9:  5: 0000000000011122 0 NOTYPE LOCAL DEFAULT 1  
dag:11'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
********************

It is caused by #89693, which has been reverted. So don't worry about this :)

dtcxzyw added a commit to dtcxzyw/llvm-codegen-benchmark that referenced this pull request Apr 27, 2024
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.

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.

@dtcxzyw dtcxzyw merged commit 37eb9c9 into llvm:main Apr 28, 2024
3 of 4 checks passed
@dtcxzyw
Copy link
Member

dtcxzyw commented Apr 28, 2024

In RISC-V ISel,the instruction czero.eqz a0, a0, a0 is meaningless,I think it need to be removed. So I upload the patch to do this job. When the condition is setcc falseValue, 0, seteq, the select instruction don't need to add a czero.eqz instruciton.

Be careful not to include the Chinese punctuation in your PR description :)

@zengdage
Copy link
Contributor Author

In RISC-V ISel,the instruction czero.eqz a0, a0, a0 is meaningless,I think it need to be removed. So I upload the patch to do this job. When the condition is setcc falseValue, 0, seteq, the select instruction don't need to add a czero.eqz instruciton.

Be careful not to include the Chinese punctuation in your PR description :)

Thank you. I'll be careful next time.

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.

None yet

6 participants