[llvm] r254104 - [WebAssembly] Fold setne and seteq comparisons into selects.

Dan Gohman via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 25 14:13:48 PST 2015


Author: djg
Date: Wed Nov 25 16:13:48 2015
New Revision: 254104

URL: http://llvm.org/viewvc/llvm-project?rev=254104&view=rev
Log:
[WebAssembly] Fold setne and seteq comparisons into selects.

Modified:
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrInteger.td
    llvm/trunk/test/CodeGen/WebAssembly/select.ll

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td?rev=254104&r1=254103&r2=254104&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td Wed Nov 25 16:13:48 2015
@@ -73,3 +73,17 @@ def SELECT_F64 : I<(outs F64:$dst), (ins
                    "f64.select\t$dst, $cond, $lhs, $rhs">;
 
 } // Defs = [ARGUMENTS]
+
+// ISD::SELECT requires its operand to conform to getBooleanContents, but
+// WebAssembly's select interprets any non-zero value as true, so we can fold
+// a setne with 0 into a select.
+def : Pat<(select (i32 (setne I32:$cond, 0)), F32:$lhs, F32:$rhs),
+          (SELECT_F32 I32:$cond, F32:$lhs, F32:$rhs)>;
+def : Pat<(select (i32 (setne I32:$cond, 0)), F64:$lhs, F64:$rhs),
+          (SELECT_F64 I32:$cond, F64:$lhs, F64:$rhs)>;
+
+// And again, this time with seteq instead of setne and the arms reversed.
+def : Pat<(select (i32 (seteq I32:$cond, 0)), F32:$lhs, F32:$rhs),
+          (SELECT_F32 I32:$cond, F32:$rhs, F32:$lhs)>;
+def : Pat<(select (i32 (seteq I32:$cond, 0)), F64:$lhs, F64:$rhs),
+          (SELECT_F64 I32:$cond, F64:$rhs, F64:$lhs)>;

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrInteger.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrInteger.td?rev=254104&r1=254103&r2=254104&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrInteger.td (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrInteger.td Wed Nov 25 16:13:48 2015
@@ -63,3 +63,17 @@ def SELECT_I64 : I<(outs I64:$dst), (ins
                    "i64.select\t$dst, $cond, $lhs, $rhs">;
 
 } // Defs = [ARGUMENTS]
+
+// ISD::SELECT requires its operand to conform to getBooleanContents, but
+// WebAssembly's select interprets any non-zero value as true, so we can fold
+// a setne with 0 into a select.
+def : Pat<(select (i32 (setne I32:$cond, 0)), I32:$lhs, I32:$rhs),
+          (SELECT_I32 I32:$cond, I32:$lhs, I32:$rhs)>;
+def : Pat<(select (i32 (setne I32:$cond, 0)), I64:$lhs, I64:$rhs),
+          (SELECT_I64 I32:$cond, I64:$lhs, I64:$rhs)>;
+
+// And again, this time with seteq instead of setne and the arms reversed.
+def : Pat<(select (i32 (seteq I32:$cond, 0)), I32:$lhs, I32:$rhs),
+          (SELECT_I32 I32:$cond, I32:$rhs, I32:$lhs)>;
+def : Pat<(select (i32 (seteq I32:$cond, 0)), I64:$lhs, I64:$rhs),
+          (SELECT_I64 I32:$cond, I64:$rhs, I64:$lhs)>;

Modified: llvm/trunk/test/CodeGen/WebAssembly/select.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/select.ll?rev=254104&r1=254103&r2=254104&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/select.ll (original)
+++ llvm/trunk/test/CodeGen/WebAssembly/select.ll Wed Nov 25 16:13:48 2015
@@ -6,38 +6,130 @@
 target datalayout = "e-p:32:32-i64:64-n32:64-S128"
 target triple = "wasm32-unknown-unknown"
 
-; CHECK-LABEL: select_i32:
-; CHECK: i32.eq $push[[NUM1:[0-9]+]]=, $2, $pop[[NUM0:[0-9]+]]{{$}}
-; CHECK: i32.select $push{{[0-9]+}}=, $pop[[NUM1]], $0, $1{{$}}
-define i32 @select_i32(i32 %a, i32 %b, i32 %cond) {
-  %cc = icmp eq i32 %cond, 0
-  %result = select i1 %cc, i32 %a, i32 %b
-  ret i32 %result
-}
-
-; CHECK-LABEL: select_i64:
-; CHECK: i32.eq $push[[NUM1:[0-9]+]]=, $2, $pop[[NUM0:[0-9]+]]{{$}}
-; CHECK: i64.select $push{{[0-9]+}}=, $pop[[NUM1]], $0, $1{{$}}
-define i64 @select_i64(i64 %a, i64 %b, i32 %cond) {
-  %cc = icmp eq i32 %cond, 0
-  %result = select i1 %cc, i64 %a, i64 %b
-  ret i64 %result
-}
-
-; CHECK-LABEL: select_f32:
-; CHECK: i32.eq $push[[NUM1:[0-9]+]]=, $2, $pop[[NUM0:[0-9]+]]{{$}}
-; CHECK: f32.select $push{{[0-9]+}}=, $pop[[NUM1]], $0, $1{{$}}
-define float @select_f32(float %a, float %b, i32 %cond) {
-  %cc = icmp eq i32 %cond, 0
-  %result = select i1 %cc, float %a, float %b
-  ret float %result
-}
-
-; CHECK-LABEL: select_f64:
-; CHECK: i32.eq $push[[NUM1:[0-9]+]]=, $2, $pop[[NUM0:[0-9]+]]{{$}}
-; CHECK: f64.select $push{{[0-9]+}}=, $pop[[NUM1]], $0, $1{{$}}
-define double @select_f64(double %a, double %b, i32 %cond) {
-  %cc = icmp eq i32 %cond, 0
-  %result = select i1 %cc, double %a, double %b
-  ret double %result
+; CHECK-LABEL: select_i32_bool:
+; CHECK-NEXT: .param     i32, i32, i32{{$}}
+; CHECK-NEXT: .result    i32{{$}}
+; CHECK-NEXT: i32.select $push0=, $0, $1, $2{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define i32 @select_i32_bool(i1 zeroext %a, i32 %b, i32 %c) {
+  %cond = select i1 %a, i32 %b, i32 %c
+  ret i32 %cond
+}
+
+; CHECK-LABEL: select_i32_eq:
+; CHECK-NEXT: .param     i32, i32, i32{{$}}
+; CHECK-NEXT: .result    i32{{$}}
+; CHECK-NEXT: i32.select $push0=, $0, $2, $1{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define i32 @select_i32_eq(i32 %a, i32 %b, i32 %c) {
+  %cmp = icmp eq i32 %a, 0
+  %cond = select i1 %cmp, i32 %b, i32 %c
+  ret i32 %cond
+}
+
+; CHECK-LABEL: select_i32_ne:
+; CHECK-NEXT: .param     i32, i32, i32{{$}}
+; CHECK-NEXT: .result    i32{{$}}
+; CHECK-NEXT: i32.select $push0=, $0, $1, $2{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define i32 @select_i32_ne(i32 %a, i32 %b, i32 %c) {
+  %cmp = icmp ne i32 %a, 0
+  %cond = select i1 %cmp, i32 %b, i32 %c
+  ret i32 %cond
+}
+
+; CHECK-LABEL: select_i64_bool:
+; CHECK-NEXT: .param     i32, i64, i64{{$}}
+; CHECK-NEXT: .result    i64{{$}}
+; CHECK-NEXT: i64.select $push0=, $0, $1, $2{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define i64 @select_i64_bool(i1 zeroext %a, i64 %b, i64 %c) {
+  %cond = select i1 %a, i64 %b, i64 %c
+  ret i64 %cond
+}
+
+; CHECK-LABEL: select_i64_eq:
+; CHECK-NEXT: .param     i32, i64, i64{{$}}
+; CHECK-NEXT: .result    i64{{$}}
+; CHECK-NEXT: i64.select $push0=, $0, $2, $1{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define i64 @select_i64_eq(i32 %a, i64 %b, i64 %c) {
+  %cmp = icmp eq i32 %a, 0
+  %cond = select i1 %cmp, i64 %b, i64 %c
+  ret i64 %cond
+}
+
+; CHECK-LABEL: select_i64_ne:
+; CHECK-NEXT: .param     i32, i64, i64{{$}}
+; CHECK-NEXT: .result    i64{{$}}
+; CHECK-NEXT: i64.select $push0=, $0, $1, $2{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define i64 @select_i64_ne(i32 %a, i64 %b, i64 %c) {
+  %cmp = icmp ne i32 %a, 0
+  %cond = select i1 %cmp, i64 %b, i64 %c
+  ret i64 %cond
+}
+
+; CHECK-LABEL: select_f32_bool:
+; CHECK-NEXT: .param     i32, f32, f32{{$}}
+; CHECK-NEXT: .result    f32{{$}}
+; CHECK-NEXT: f32.select $push0=, $0, $1, $2{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define float @select_f32_bool(i1 zeroext %a, float %b, float %c) {
+  %cond = select i1 %a, float %b, float %c
+  ret float %cond
+}
+
+; CHECK-LABEL: select_f32_eq:
+; CHECK-NEXT: .param     i32, f32, f32{{$}}
+; CHECK-NEXT: .result    f32{{$}}
+; CHECK-NEXT: f32.select $push0=, $0, $2, $1{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define float @select_f32_eq(i32 %a, float %b, float %c) {
+  %cmp = icmp eq i32 %a, 0
+  %cond = select i1 %cmp, float %b, float %c
+  ret float %cond
+}
+
+; CHECK-LABEL: select_f32_ne:
+; CHECK-NEXT: .param     i32, f32, f32{{$}}
+; CHECK-NEXT: .result    f32{{$}}
+; CHECK-NEXT: f32.select $push0=, $0, $1, $2{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define float @select_f32_ne(i32 %a, float %b, float %c) {
+  %cmp = icmp ne i32 %a, 0
+  %cond = select i1 %cmp, float %b, float %c
+  ret float %cond
+}
+
+; CHECK-LABEL: select_f64_bool:
+; CHECK-NEXT: .param     i32, f64, f64{{$}}
+; CHECK-NEXT: .result    f64{{$}}
+; CHECK-NEXT: f64.select $push0=, $0, $1, $2{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define double @select_f64_bool(i1 zeroext %a, double %b, double %c) {
+  %cond = select i1 %a, double %b, double %c
+  ret double %cond
+}
+
+; CHECK-LABEL: select_f64_eq:
+; CHECK-NEXT: .param     i32, f64, f64{{$}}
+; CHECK-NEXT: .result    f64{{$}}
+; CHECK-NEXT: f64.select $push0=, $0, $2, $1{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define double @select_f64_eq(i32 %a, double %b, double %c) {
+  %cmp = icmp eq i32 %a, 0
+  %cond = select i1 %cmp, double %b, double %c
+  ret double %cond
+}
+
+; CHECK-LABEL: select_f64_ne:
+; CHECK-NEXT: .param     i32, f64, f64{{$}}
+; CHECK-NEXT: .result    f64{{$}}
+; CHECK-NEXT: f64.select $push0=, $0, $1, $2{{$}}
+; CHECK-NEXT: return     $pop0{{$}}
+define double @select_f64_ne(i32 %a, double %b, double %c) {
+  %cmp = icmp ne i32 %a, 0
+  %cond = select i1 %cmp, double %b, double %c
+  ret double %cond
 }




More information about the llvm-commits mailing list