[llvm] [WebAssembly] improve getRegForPromotedValue to avoid meanless value copy (PR #80469)

Congcong Cai via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 2 17:50:35 PST 2024


https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/80469

>From 2607a7b6a00a28e36debd14e7e64c0c54e5290e4 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sat, 3 Feb 2024 01:51:10 +0800
Subject: [PATCH 1/2] [WebAssembly] improve getRegForPromotedValue to avoid
 meanless value copy

---
 .../WebAssembly/WebAssemblyFastISel.cpp       |  4 ++
 .../CodeGen/WebAssembly/suboptimal-compare.ll | 43 +++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 llvm/test/CodeGen/WebAssembly/suboptimal-compare.ll

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
index 7f0140a5e8c66..1c62290704fe4 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
@@ -559,6 +559,8 @@ unsigned WebAssemblyFastISel::getRegForUnsignedValue(const Value *V) {
   Register VReg = getRegForValue(V);
   if (VReg == 0)
     return 0;
+  if (From == To)
+    return VReg;
   return zeroExtend(VReg, V, From, To);
 }
 
@@ -568,6 +570,8 @@ unsigned WebAssemblyFastISel::getRegForSignedValue(const Value *V) {
   Register VReg = getRegForValue(V);
   if (VReg == 0)
     return 0;
+  if (From == To)
+    return VReg;
   return signExtend(VReg, V, From, To);
 }
 
diff --git a/llvm/test/CodeGen/WebAssembly/suboptimal-compare.ll b/llvm/test/CodeGen/WebAssembly/suboptimal-compare.ll
new file mode 100644
index 0000000000000..243494cee320c
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/suboptimal-compare.ll
@@ -0,0 +1,43 @@
+; RUN: llc < %s -fast-isel -O0 | FileCheck %s
+
+target triple = "wasm32-unknown-unknown"
+
+; CHECK-LABEL: gh_80052:                               # @gh_80052
+; CHECK-NEXT: .functype	gh_80052 (i32) -> (i32)
+; CHECK-NEXT: .local  	i32, i32, i32, i32, i32, i32
+; CHECK:      i32.const	0
+; CHECK-NEXT: local.set	1
+; CHECK-NEXT: local.get	0
+; CHECK-NEXT: local.get	1
+; CHECK-NEXT: i32.eq  
+; CHECK-NEXT: local.set	2
+; CHECK-NEXT: i32.const	1
+; CHECK-NEXT: local.set	3
+; CHECK-NEXT: local.get	2
+; CHECK-NEXT: local.get	3
+; CHECK-NEXT: i32.and 
+; CHECK-NEXT: local.set	4
+; CHECK-NEXT: block   	
+; CHECK-NEXT:   local.get	4
+; CHECK-NEXT:   i32.eqz
+; CHECK-NEXT:   br_if   	0                               # 0: down to label0
+; CHECK:        i32.const	0
+; CHECK-NEXT:   local.set	5
+; CHECK-NEXT:   local.get	5
+; CHECK-NEXT:   return
+; CHECK-NEXT: .LBB0_2:                                # %BB03
+; CHECK-NEXT: end_block                               # label0:
+; CHECK-NEXT: i32.const	1
+; CHECK-NEXT: local.set	6
+; CHECK-NEXT: local.get	6
+; CHECK-NEXT: return
+; CHECK-NEXT: end_function
+define i1 @gh_80052(ptr) {
+BB01:
+    %eq = icmp eq ptr %0, null
+    br i1 %eq, label %BB02, label %BB03
+BB02:
+    ret i1 0
+BB03:
+    ret i1 1
+}

>From 66548f4a2ad0d1581019005f4cd56ac997741764 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sat, 3 Feb 2024 09:50:11 +0800
Subject: [PATCH 2/2] fix

---
 llvm/test/CodeGen/WebAssembly/suboptimal-compare.ll | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/test/CodeGen/WebAssembly/suboptimal-compare.ll b/llvm/test/CodeGen/WebAssembly/suboptimal-compare.ll
index 243494cee320c..fadeb397a884c 100644
--- a/llvm/test/CodeGen/WebAssembly/suboptimal-compare.ll
+++ b/llvm/test/CodeGen/WebAssembly/suboptimal-compare.ll
@@ -2,8 +2,8 @@
 
 target triple = "wasm32-unknown-unknown"
 
-; CHECK-LABEL: gh_80052:                               # @gh_80052
-; CHECK-NEXT: .functype	gh_80052 (i32) -> (i32)
+; CHECK-LABEL: gh_80053:                               # @gh_80053
+; CHECK-NEXT: .functype	gh_80053 (i32) -> (i32)
 ; CHECK-NEXT: .local  	i32, i32, i32, i32, i32, i32
 ; CHECK:      i32.const	0
 ; CHECK-NEXT: local.set	1
@@ -32,7 +32,7 @@ target triple = "wasm32-unknown-unknown"
 ; CHECK-NEXT: local.get	6
 ; CHECK-NEXT: return
 ; CHECK-NEXT: end_function
-define i1 @gh_80052(ptr) {
+define i1 @gh_80053(ptr) {
 BB01:
     %eq = icmp eq ptr %0, null
     br i1 %eq, label %BB02, label %BB03



More information about the llvm-commits mailing list