[llvm] r244779 - WebAssembly: floating-point comparisons

JF Bastien via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 12 10:53:30 PDT 2015


Author: jfb
Date: Wed Aug 12 12:53:29 2015
New Revision: 244779

URL: http://llvm.org/viewvc/llvm-project?rev=244779&view=rev
Log:
WebAssembly: floating-point comparisons

Summary:
D11924 implemented part of the floating-point comparisons, this patch implements the rest:
 * Tell ISelLowering that all booleans are either 0 or 1.
 * Expand the eq/ne/lt/le/gt/ge floating-point comparisons to the canonical ones (similar to what Mips32r6InstrInfo.td does).
 * Add tests for ord/uno.
 * Add tests for ueq/one/ult/ule/ugt/uge.
 * Fix existing comparison tests to remove the (res & 1) code, which setBooleanContents stops from generating.

Reviewers: sunfish

Subscribers: llvm-commits, jfb

Differential Revision: http://reviews.llvm.org/D11970

Modified:
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td
    llvm/trunk/test/CodeGen/WebAssembly/comparisons_f32.ll
    llvm/trunk/test/CodeGen/WebAssembly/comparisons_f64.ll
    llvm/trunk/test/CodeGen/WebAssembly/comparisons_i32.ll
    llvm/trunk/test/CodeGen/WebAssembly/comparisons_i64.ll

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp?rev=244779&r1=244778&r2=244779&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp Wed Aug 12 12:53:29 2015
@@ -92,6 +92,8 @@ int DiagnosticInfoUnsupported::KindID =
 WebAssemblyTargetLowering::WebAssemblyTargetLowering(
     const TargetMachine &TM, const WebAssemblySubtarget &STI)
     : TargetLowering(TM), Subtarget(&STI) {
+  // Booleans always contain 0 or 1.
+  setBooleanContents(ZeroOrOneBooleanContent);
   // WebAssembly does not produce floating-point exceptions on normal floating
   // point operations.
   setHasFloatingPointExceptions(false);

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td?rev=244779&r1=244778&r2=244779&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyInstrFloat.td Wed Aug 12 12:53:29 2015
@@ -35,6 +35,20 @@ defm LE : ComparisonFP<SETOLE>;
 defm GT : ComparisonFP<SETOGT>;
 defm GE : ComparisonFP<SETOGE>;
 
+// Don't care floating-point comparisons, supported via other comparisons.
+def : Pat<(seteq f32:$lhs, f32:$rhs), (EQ_F32 f32:$lhs, f32:$rhs)>;
+def : Pat<(setne f32:$lhs, f32:$rhs), (NE_F32 f32:$lhs, f32:$rhs)>;
+def : Pat<(setlt f32:$lhs, f32:$rhs), (LT_F32 f32:$lhs, f32:$rhs)>;
+def : Pat<(setle f32:$lhs, f32:$rhs), (LE_F32 f32:$lhs, f32:$rhs)>;
+def : Pat<(setgt f32:$lhs, f32:$rhs), (GT_F32 f32:$lhs, f32:$rhs)>;
+def : Pat<(setge f32:$lhs, f32:$rhs), (GE_F32 f32:$lhs, f32:$rhs)>;
+def : Pat<(seteq f64:$lhs, f64:$rhs), (EQ_F64 f64:$lhs, f64:$rhs)>;
+def : Pat<(setne f64:$lhs, f64:$rhs), (NE_F64 f64:$lhs, f64:$rhs)>;
+def : Pat<(setlt f64:$lhs, f64:$rhs), (LT_F64 f64:$lhs, f64:$rhs)>;
+def : Pat<(setle f64:$lhs, f64:$rhs), (LE_F64 f64:$lhs, f64:$rhs)>;
+def : Pat<(setgt f64:$lhs, f64:$rhs), (GT_F64 f64:$lhs, f64:$rhs)>;
+def : Pat<(setge f64:$lhs, f64:$rhs), (GE_F64 f64:$lhs, f64:$rhs)>;
+
 defm SQRT : UnaryFP<fsqrt>;
 
 /*

Modified: llvm/trunk/test/CodeGen/WebAssembly/comparisons_f32.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/comparisons_f32.ll?rev=244779&r1=244778&r2=244779&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/comparisons_f32.ll (original)
+++ llvm/trunk/test/CodeGen/WebAssembly/comparisons_f32.ll Wed Aug 12 12:53:29 2015
@@ -6,15 +6,37 @@
 target datalayout = "e-p:32:32-i64:64-v128:8:128-n32:64-S128"
 target triple = "wasm32-unknown-unknown"
 
-; FIXME: add ord and uno tests.
+; CHECK-LABEL: ord_f32:
+; CHECK-NEXT: (setlocal @0 (argument 0))
+; CHECK-NEXT: (setlocal @1 (argument 1))
+; CHECK-NEXT: (setlocal @2 (eq @1 @1))
+; CHECK-NEXT: (setlocal @3 (eq @0 @0))
+; CHECK-NEXT: (setlocal @4 (and @3 @2))
+; CHECK-NEXT: (return @4)
+define i32 @ord_f32(float %x, float %y) {
+  %a = fcmp ord float %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: uno_f32:
+; CHECK-NEXT: (setlocal @0 (argument 0))
+; CHECK-NEXT: (setlocal @1 (argument 1))
+; CHECK-NEXT: (setlocal @2 (ne @1 @1))
+; CHECK-NEXT: (setlocal @3 (ne @0 @0))
+; CHECK-NEXT: (setlocal @4 (ior @3 @2))
+; CHECK-NEXT: (return @4)
+define i32 @uno_f32(float %x, float %y) {
+  %a = fcmp uno float %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
 
 ; CHECK-LABEL: oeq_f32:
 ; CHECK-NEXT: (setlocal @0 (argument 1))
 ; CHECK-NEXT: (setlocal @1 (argument 0))
 ; CHECK-NEXT: (setlocal @2 (eq @1 @0))
-; CHECK-NEXT: (setlocal @3 (immediate 1))
-; CHECK-NEXT: (setlocal @4 (and @2 @3))
-; CHECK-NEXT: (return @4)
+; CHECK-NEXT: (return @2)
 define i32 @oeq_f32(float %x, float %y) {
   %a = fcmp oeq float %x, %y
   %b = zext i1 %a to i32
@@ -61,5 +83,59 @@ define i32 @oge_f32(float %x, float %y)
   ret i32 %b
 }
 
-; FIXME test other FP comparisons: ueq, one, ult, ule, ugt, uge. They currently
-; are broken and failt to match.
+; Expanded comparisons, which also check for NaN.
+
+; CHECK-LABEL: ueq_f32:
+; CHECK-NEXT: (setlocal @0 (argument 1))
+; CHECK-NEXT: (setlocal @1 (argument 0))
+; CHECK-NEXT: (setlocal @2 (eq @1 @0))
+; CHECK-NEXT: (setlocal @3 (ne @0 @0))
+; CHECK-NEXT: (setlocal @4 (ne @1 @1))
+; CHECK-NEXT: (setlocal @5 (ior @4 @3))
+; CHECK-NEXT: (setlocal @6 (ior @2 @5))
+; CHECK-NEXT: (return @6)
+define i32 @ueq_f32(float %x, float %y) {
+  %a = fcmp ueq float %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: one_f32:
+; CHECK: (setlocal @2 (ne @1 @0))
+define i32 @one_f32(float %x, float %y) {
+  %a = fcmp one float %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: ult_f32:
+; CHECK: (setlocal @2 (lt @1 @0))
+define i32 @ult_f32(float %x, float %y) {
+  %a = fcmp ult float %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: ule_f32:
+; CHECK: (setlocal @2 (le @1 @0))
+define i32 @ule_f32(float %x, float %y) {
+  %a = fcmp ule float %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: ugt_f32:
+; CHECK: (setlocal @2 (gt @1 @0))
+define i32 @ugt_f32(float %x, float %y) {
+  %a = fcmp ugt float %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: uge_f32:
+; CHECK: (setlocal @2 (ge @1 @0))
+define i32 @uge_f32(float %x, float %y) {
+  %a = fcmp uge float %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}

Modified: llvm/trunk/test/CodeGen/WebAssembly/comparisons_f64.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/comparisons_f64.ll?rev=244779&r1=244778&r2=244779&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/comparisons_f64.ll (original)
+++ llvm/trunk/test/CodeGen/WebAssembly/comparisons_f64.ll Wed Aug 12 12:53:29 2015
@@ -6,15 +6,37 @@
 target datalayout = "e-p:32:32-i64:64-v128:8:128-n32:64-S128"
 target triple = "wasm32-unknown-unknown"
 
-; FIXME: add ord and uno tests.
+; CHECK-LABEL: ord_f64:
+; CHECK-NEXT: (setlocal @0 (argument 0))
+; CHECK-NEXT: (setlocal @1 (argument 1))
+; CHECK-NEXT: (setlocal @2 (eq @1 @1))
+; CHECK-NEXT: (setlocal @3 (eq @0 @0))
+; CHECK-NEXT: (setlocal @4 (and @3 @2))
+; CHECK-NEXT: (return @4)
+define i32 @ord_f64(double %x, double %y) {
+  %a = fcmp ord double %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: uno_f64:
+; CHECK-NEXT: (setlocal @0 (argument 0))
+; CHECK-NEXT: (setlocal @1 (argument 1))
+; CHECK-NEXT: (setlocal @2 (ne @1 @1))
+; CHECK-NEXT: (setlocal @3 (ne @0 @0))
+; CHECK-NEXT: (setlocal @4 (ior @3 @2))
+; CHECK-NEXT: (return @4)
+define i32 @uno_f64(double %x, double %y) {
+  %a = fcmp uno double %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
 
 ; CHECK-LABEL: oeq_f64:
 ; CHECK-NEXT: (setlocal @0 (argument 1))
 ; CHECK-NEXT: (setlocal @1 (argument 0))
 ; CHECK-NEXT: (setlocal @2 (eq @1 @0))
-; CHECK-NEXT: (setlocal @3 (immediate 1))
-; CHECK-NEXT: (setlocal @4 (and @2 @3))
-; CHECK-NEXT: (return @4)
+; CHECK-NEXT: (return @2)
 define i32 @oeq_f64(double %x, double %y) {
   %a = fcmp oeq double %x, %y
   %b = zext i1 %a to i32
@@ -61,5 +83,59 @@ define i32 @oge_f64(double %x, double %y
   ret i32 %b
 }
 
-; FIXME test other FP comparisons: ueq, one, ult, ule, ugt, uge. They currently
-; are broken and failt to match.
+; Expanded comparisons, which also check for NaN.
+
+; CHECK-LABEL: ueq_f64:
+; CHECK-NEXT: (setlocal @0 (argument 1))
+; CHECK-NEXT: (setlocal @1 (argument 0))
+; CHECK-NEXT: (setlocal @2 (eq @1 @0))
+; CHECK-NEXT: (setlocal @3 (ne @0 @0))
+; CHECK-NEXT: (setlocal @4 (ne @1 @1))
+; CHECK-NEXT: (setlocal @5 (ior @4 @3))
+; CHECK-NEXT: (setlocal @6 (ior @2 @5))
+; CHECK-NEXT: (return @6)
+define i32 @ueq_f64(double %x, double %y) {
+  %a = fcmp ueq double %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: one_f64:
+; CHECK: (setlocal @2 (ne @1 @0))
+define i32 @one_f64(double %x, double %y) {
+  %a = fcmp one double %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: ult_f64:
+; CHECK: (setlocal @2 (lt @1 @0))
+define i32 @ult_f64(double %x, double %y) {
+  %a = fcmp ult double %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: ule_f64:
+; CHECK: (setlocal @2 (le @1 @0))
+define i32 @ule_f64(double %x, double %y) {
+  %a = fcmp ule double %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: ugt_f64:
+; CHECK: (setlocal @2 (gt @1 @0))
+define i32 @ugt_f64(double %x, double %y) {
+  %a = fcmp ugt double %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}
+
+; CHECK-LABEL: uge_f64:
+; CHECK: (setlocal @2 (ge @1 @0))
+define i32 @uge_f64(double %x, double %y) {
+  %a = fcmp uge double %x, %y
+  %b = zext i1 %a to i32
+  ret i32 %b
+}

Modified: llvm/trunk/test/CodeGen/WebAssembly/comparisons_i32.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/comparisons_i32.ll?rev=244779&r1=244778&r2=244779&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/comparisons_i32.ll (original)
+++ llvm/trunk/test/CodeGen/WebAssembly/comparisons_i32.ll Wed Aug 12 12:53:29 2015
@@ -9,9 +9,7 @@ target triple = "wasm32-unknown-unknown"
 ; CHECK-NEXT: (setlocal @0 (argument 1))
 ; CHECK-NEXT: (setlocal @1 (argument 0))
 ; CHECK-NEXT: (setlocal @2 (eq @1 @0))
-; CHECK-NEXT: (setlocal @3 (immediate 1))
-; CHECK-NEXT: (setlocal @4 (and @2 @3))
-; CHECK-NEXT: (return @4)
+; CHECK-NEXT: (return @2)
 define i32 @eq_i32(i32 %x, i32 %y) {
   %a = icmp eq i32 %x, %y
   %b = zext i1 %a to i32

Modified: llvm/trunk/test/CodeGen/WebAssembly/comparisons_i64.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/comparisons_i64.ll?rev=244779&r1=244778&r2=244779&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/comparisons_i64.ll (original)
+++ llvm/trunk/test/CodeGen/WebAssembly/comparisons_i64.ll Wed Aug 12 12:53:29 2015
@@ -9,9 +9,7 @@ target triple = "wasm32-unknown-unknown"
 ; CHECK-NEXT: (setlocal @0 (argument 1))
 ; CHECK-NEXT: (setlocal @1 (argument 0))
 ; CHECK-NEXT: (setlocal @2 (eq @1 @0))
-; CHECK-NEXT: (setlocal @3 (immediate 1))
-; CHECK-NEXT: (setlocal @4 (and @2 @3))
-; CHECK-NEXT: (return @4)
+; CHECK-NEXT: (return @2)
 define i32 @eq_i64(i64 %x, i64 %y) {
   %a = icmp eq i64 %x, %y
   %b = zext i1 %a to i32




More information about the llvm-commits mailing list