From: JF Bastien Date: Wed, 12 Aug 2015 17:53:29 +0000 (+0000) Subject: WebAssembly: floating-point comparisons X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=5f53aadd9c9fa073ce5eabc630002b77f9663cf9;p=oota-llvm.git 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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244779 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp b/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp index bfa442b24a7..21a322241a8 100644 --- a/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp +++ b/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp @@ -92,6 +92,8 @@ int DiagnosticInfoUnsupported::KindID = 0; 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); diff --git a/lib/Target/WebAssembly/WebAssemblyInstrFloat.td b/lib/Target/WebAssembly/WebAssemblyInstrFloat.td index 16e5c8eb83d..87d1a98b98e 100644 --- a/lib/Target/WebAssembly/WebAssemblyInstrFloat.td +++ b/lib/Target/WebAssembly/WebAssemblyInstrFloat.td @@ -35,6 +35,20 @@ defm LE : ComparisonFP; defm GT : ComparisonFP; defm GE : ComparisonFP; +// 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; /* diff --git a/test/CodeGen/WebAssembly/comparisons_f32.ll b/test/CodeGen/WebAssembly/comparisons_f32.ll index 1cb76a4f90f..4edb7de4cd8 100644 --- a/test/CodeGen/WebAssembly/comparisons_f32.ll +++ b/test/CodeGen/WebAssembly/comparisons_f32.ll @@ -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 +} diff --git a/test/CodeGen/WebAssembly/comparisons_f64.ll b/test/CodeGen/WebAssembly/comparisons_f64.ll index 446a09badd2..11c1fc582ee 100644 --- a/test/CodeGen/WebAssembly/comparisons_f64.ll +++ b/test/CodeGen/WebAssembly/comparisons_f64.ll @@ -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 +} diff --git a/test/CodeGen/WebAssembly/comparisons_i32.ll b/test/CodeGen/WebAssembly/comparisons_i32.ll index 3dc387f39e9..f45c6ec0a21 100644 --- a/test/CodeGen/WebAssembly/comparisons_i32.ll +++ b/test/CodeGen/WebAssembly/comparisons_i32.ll @@ -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 diff --git a/test/CodeGen/WebAssembly/comparisons_i64.ll b/test/CodeGen/WebAssembly/comparisons_i64.ll index 76f331d49af..f0ff479db58 100644 --- a/test/CodeGen/WebAssembly/comparisons_i64.ll +++ b/test/CodeGen/WebAssembly/comparisons_i64.ll @@ -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