Index: lib/Target/WebAssembly/WebAssemblyInstrFloat.td =================================================================== --- lib/Target/WebAssembly/WebAssemblyInstrFloat.td +++ lib/Target/WebAssembly/WebAssemblyInstrFloat.td @@ -24,15 +24,13 @@ defm TRUNC : UnaryFP; defm NEARESTINT : UnaryFP; -/* - * TODO(jfb): Add the following for 32-bit and 64-bit. - * - * float32.eq: compare equal - * float32.lt: less than - * float32.le: less than or equal - * float32.gt: greater than - * float32.ge: greater than or equal - */ +// FIXME: fold unordered operations to ordered. +defm EQ : ComparisonFP; +defm NE : ComparisonFP; +defm LT : ComparisonFP; +defm LE : ComparisonFP; +defm GT : ComparisonFP; +defm GE : ComparisonFP; defm SQRT : UnaryFP; Index: lib/Target/WebAssembly/WebAssemblyInstrFormats.td =================================================================== --- lib/Target/WebAssembly/WebAssemblyInstrFormats.td +++ lib/Target/WebAssembly/WebAssemblyInstrFormats.td @@ -53,3 +53,15 @@ def _F64 : I<(outs Float64:$dst), (ins Float64:$lhs, Float64:$rhs), [(set Float64:$dst, (node Float64:$lhs, Float64:$rhs))]>; } +multiclass ComparisonInt { + def _I32 : I<(outs Int32:$dst), (ins Int32:$lhs, Int32:$rhs), + [(set Int32:$dst, (setcc Int32:$lhs, Int32:$rhs, cond))]>; + def _I64 : I<(outs Int32:$dst), (ins Int64:$lhs, Int64:$rhs), + [(set Int32:$dst, (setcc Int64:$lhs, Int64:$rhs, cond))]>; +} +multiclass ComparisonFP { + def _F32 : I<(outs Int32:$dst), (ins Float32:$lhs, Float32:$rhs), + [(set Int32:$dst, (node Float32:$lhs, Float32:$rhs))]>; + def _F64 : I<(outs Int32:$dst), (ins Float64:$lhs, Float64:$rhs), + [(set Int32:$dst, (node Float64:$lhs, Float64:$rhs))]>; +} Index: lib/Target/WebAssembly/WebAssemblyInstrInteger.td =================================================================== --- lib/Target/WebAssembly/WebAssemblyInstrInteger.td +++ lib/Target/WebAssembly/WebAssemblyInstrInteger.td @@ -26,19 +26,16 @@ defm SHR : BinaryInt; defm SAR : BinaryInt; -/* - * TODO(jfb): Add the following for 32-bit and 64-bit. - * - * int32.eq: signed-less compare equal - * int32.slt: signed less than - * int32.sle: signed less than or equal - * int32.ult: unsigned less than - * int32.ule: unsigned less than or equal - * int32.sgt: signed greater than - * int32.sge: signed greater than or equal - * int32.ugt: unsigned greater than - * int32.uge: unsigned greater than or equal - */ +defm EQ : ComparisonInt; +defm NE : ComparisonInt; +defm SLT : ComparisonInt; +defm SLE : ComparisonInt; +defm ULT : ComparisonInt; +defm ULE : ComparisonInt; +defm SGT : ComparisonInt; +defm SGE : ComparisonInt; +defm UGT : ComparisonInt; +defm UGE : ComparisonInt; defm CLZ : UnaryInt; defm CTZ : UnaryInt; Index: test/CodeGen/WebAssembly/comparisons_i32.ll =================================================================== --- /dev/null +++ test/CodeGen/WebAssembly/comparisons_i32.ll @@ -0,0 +1,91 @@ +; RUN: llc < %s -asm-verbose=false | FileCheck %s + +; Test that basic 32-bit integer comparison operations assemble as expected. + +target datalayout = "e-p:32:32-i64:64-v128:8:128-n32:64-S128" +target triple = "wasm32-unknown-unknown" + +; CHECK-LABEL: eq_i32: +; 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) +define i32 @eq_i32(i32 %x, i32 %y) { + %a = icmp eq i32 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ne_i32: +; CHECK: (setlocal @2 (ne @1 @0)) +define i32 @ne_i32(i32 %x, i32 %y) { + %a = icmp ne i32 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: slt_i32: +; CHECK: (setlocal @2 (slt @1 @0)) +define i32 @slt_i32(i32 %x, i32 %y) { + %a = icmp slt i32 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: sle_i32: +; CHECK: (setlocal @2 (sle @1 @0)) +define i32 @sle_i32(i32 %x, i32 %y) { + %a = icmp sle i32 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ult_i32: +; CHECK: (setlocal @2 (ult @1 @0)) +define i32 @ult_i32(i32 %x, i32 %y) { + %a = icmp ult i32 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ule_i32: +; CHECK: (setlocal @2 (ule @1 @0)) +define i32 @ule_i32(i32 %x, i32 %y) { + %a = icmp ule i32 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: sgt_i32: +; CHECK: (setlocal @2 (sgt @1 @0)) +define i32 @sgt_i32(i32 %x, i32 %y) { + %a = icmp sgt i32 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: sge_i32: +; CHECK: (setlocal @2 (sge @1 @0)) +define i32 @sge_i32(i32 %x, i32 %y) { + %a = icmp sge i32 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ugt_i32: +; CHECK: (setlocal @2 (ugt @1 @0)) +define i32 @ugt_i32(i32 %x, i32 %y) { + %a = icmp ugt i32 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: uge_i32: +; CHECK: (setlocal @2 (uge @1 @0)) +define i32 @uge_i32(i32 %x, i32 %y) { + %a = icmp uge i32 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} Index: test/CodeGen/WebAssembly/comparisons_i64.ll =================================================================== --- /dev/null +++ test/CodeGen/WebAssembly/comparisons_i64.ll @@ -0,0 +1,91 @@ +; RUN: llc < %s -asm-verbose=false | FileCheck %s + +; Test that basic 64-bit integer comparison operations assemble as expected. + +target datalayout = "e-p:32:32-i64:64-v128:8:128-n32:64-S128" +target triple = "wasm32-unknown-unknown" + +; CHECK-LABEL: eq_i64: +; 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) +define i32 @eq_i64(i64 %x, i64 %y) { + %a = icmp eq i64 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ne_i64: +; CHECK: (setlocal @2 (ne @1 @0)) +define i32 @ne_i64(i64 %x, i64 %y) { + %a = icmp ne i64 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: slt_i64: +; CHECK: (setlocal @2 (slt @1 @0)) +define i32 @slt_i64(i64 %x, i64 %y) { + %a = icmp slt i64 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: sle_i64: +; CHECK: (setlocal @2 (sle @1 @0)) +define i32 @sle_i64(i64 %x, i64 %y) { + %a = icmp sle i64 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ult_i64: +; CHECK: (setlocal @2 (ult @1 @0)) +define i32 @ult_i64(i64 %x, i64 %y) { + %a = icmp ult i64 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ule_i64: +; CHECK: (setlocal @2 (ule @1 @0)) +define i32 @ule_i64(i64 %x, i64 %y) { + %a = icmp ule i64 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: sgt_i64: +; CHECK: (setlocal @2 (sgt @1 @0)) +define i32 @sgt_i64(i64 %x, i64 %y) { + %a = icmp sgt i64 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: sge_i64: +; CHECK: (setlocal @2 (sge @1 @0)) +define i32 @sge_i64(i64 %x, i64 %y) { + %a = icmp sge i64 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ugt_i64: +; CHECK: (setlocal @2 (ugt @1 @0)) +define i32 @ugt_i64(i64 %x, i64 %y) { + %a = icmp ugt i64 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: uge_i64: +; CHECK: (setlocal @2 (uge @1 @0)) +define i32 @uge_i64(i64 %x, i64 %y) { + %a = icmp uge i64 %x, %y + %b = zext i1 %a to i32 + ret i32 %b +}