Index: lib/Target/WebAssembly/WebAssemblyISelLowering.cpp =================================================================== --- lib/Target/WebAssembly/WebAssemblyISelLowering.cpp +++ lib/Target/WebAssembly/WebAssemblyISelLowering.cpp @@ -92,6 +92,8 @@ 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); Index: lib/Target/WebAssembly/WebAssemblyInstrFloat.td =================================================================== --- lib/Target/WebAssembly/WebAssemblyInstrFloat.td +++ lib/Target/WebAssembly/WebAssemblyInstrFloat.td @@ -35,6 +35,22 @@ defm GT : ComparisonFP; defm GE : ComparisonFP; +// Floating-point comparisons supported via other comparisons. +// 32-bit: +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)>; +// 64-bit: +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; /* Index: test/CodeGen/WebAssembly/comparisons_f32.ll =================================================================== --- test/CodeGen/WebAssembly/comparisons_f32.ll +++ 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 @@ 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 +} Index: test/CodeGen/WebAssembly/comparisons_f64.ll =================================================================== --- test/CodeGen/WebAssembly/comparisons_f64.ll +++ 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 @@ 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 +} Index: test/CodeGen/WebAssembly/comparisons_i32.ll =================================================================== --- test/CodeGen/WebAssembly/comparisons_i32.ll +++ test/CodeGen/WebAssembly/comparisons_i32.ll @@ -9,9 +9,7 @@ ; 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 Index: test/CodeGen/WebAssembly/comparisons_i64.ll =================================================================== --- test/CodeGen/WebAssembly/comparisons_i64.ll +++ test/CodeGen/WebAssembly/comparisons_i64.ll @@ -9,9 +9,7 @@ ; 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