diff options
| author | JF Bastien <jfb@google.com> | 2015-08-11 21:02:46 +0000 |
|---|---|---|
| committer | JF Bastien <jfb@google.com> | 2015-08-11 21:02:46 +0000 |
| commit | da06bce8b50d47ca841d694e10c12638157b2093 (patch) | |
| tree | 916d3e9b6fbbc1c5fbe1d350e710dd9a3c42d9b7 | |
| parent | 8c3f9c9868512f2375a86d9eecb447e195c51335 (diff) | |
| download | bcm5719-llvm-da06bce8b50d47ca841d694e10c12638157b2093.tar.gz bcm5719-llvm-da06bce8b50d47ca841d694e10c12638157b2093.zip | |
WebAssembly: implement comparison.
Some of the FP comparisons (ueq, one, ult, ule, ugt, uge) are currently broken, I'll fix them in a follow-up.
Reviewers: sunfish
Subscribers: llvm-commits, jfb
Differential Revision: http://reviews.llvm.org/D11924
llvm-svn: 244665
| -rw-r--r-- | llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp | 11 | ||||
| -rw-r--r-- | llvm/lib/Target/WebAssembly/WebAssemblyInstrFloat.td | 15 | ||||
| -rw-r--r-- | llvm/lib/Target/WebAssembly/WebAssemblyInstrFormats.td | 12 | ||||
| -rw-r--r-- | llvm/lib/Target/WebAssembly/WebAssemblyInstrInteger.td | 23 | ||||
| -rw-r--r-- | llvm/test/CodeGen/WebAssembly/comparisons_f32.ll | 65 | ||||
| -rw-r--r-- | llvm/test/CodeGen/WebAssembly/comparisons_f64.ll | 65 | ||||
| -rw-r--r-- | llvm/test/CodeGen/WebAssembly/comparisons_i32.ll | 91 | ||||
| -rw-r--r-- | llvm/test/CodeGen/WebAssembly/comparisons_i64.ll | 91 |
8 files changed, 348 insertions, 25 deletions
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp index f73500dddc0..bfa442b24a7 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp @@ -110,9 +110,14 @@ WebAssemblyTargetLowering::WebAssemblyTargetLowering( // FIXME: many setOperationAction are missing... - // Don't expand the following types to constant pools. - setOperationAction(ISD::ConstantFP, MVT::f32, Legal); - setOperationAction(ISD::ConstantFP, MVT::f64, Legal); + for (auto T : {MVT::f32, MVT::f64}) { + // Don't expand the floating-point types to constant pools. + setOperationAction(ISD::ConstantFP, T, Legal); + // Expand floating-point comparisons. + for (auto CC : {ISD::SETO, ISD::SETUO, ISD::SETUEQ, ISD::SETONE, + ISD::SETULT, ISD::SETULE, ISD::SETUGT, ISD::SETUGE}) + setCondCodeAction(CC, T, Expand); + } } MVT WebAssemblyTargetLowering::getScalarShiftAmountTy(const DataLayout &DL, diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrFloat.td b/llvm/lib/Target/WebAssembly/WebAssemblyInstrFloat.td index b6c09be83a3..16e5c8eb83d 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrFloat.td +++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrFloat.td @@ -28,15 +28,12 @@ defm COPYSIGN : BinaryFP<fcopysign>; * defm NEARESTINT : UnaryFP<fnearbyint>; */ -/* - * 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 - */ +defm EQ : ComparisonFP<SETOEQ>; +defm NE : ComparisonFP<SETUNE>; +defm LT : ComparisonFP<SETOLT>; +defm LE : ComparisonFP<SETOLE>; +defm GT : ComparisonFP<SETOGT>; +defm GE : ComparisonFP<SETOGE>; defm SQRT : UnaryFP<fsqrt>; diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrFormats.td b/llvm/lib/Target/WebAssembly/WebAssemblyInstrFormats.td index 513c36fa2ec..f4d16d39e64 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrFormats.td +++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrFormats.td @@ -53,3 +53,15 @@ multiclass BinaryFP<SDNode node> { def _F64 : I<(outs Float64:$dst), (ins Float64:$lhs, Float64:$rhs), [(set Float64:$dst, (node Float64:$lhs, Float64:$rhs))]>; } +multiclass ComparisonInt<CondCode cond> { + 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<CondCode cond> { + def _F32 : I<(outs Int32:$dst), (ins Float32:$lhs, Float32:$rhs), + [(set Int32:$dst, (setcc Float32:$lhs, Float32:$rhs, cond))]>; + def _F64 : I<(outs Int32:$dst), (ins Float64:$lhs, Float64:$rhs), + [(set Int32:$dst, (setcc Float64:$lhs, Float64:$rhs, cond))]>; +} diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrInteger.td b/llvm/lib/Target/WebAssembly/WebAssemblyInstrInteger.td index 5f60fe81b1a..cf2c6a38d51 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrInteger.td +++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrInteger.td @@ -26,19 +26,16 @@ defm SHL : BinaryInt<shl>; defm SHR : BinaryInt<srl>; defm SAR : BinaryInt<sra>; -/* - * 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<SETEQ>; +defm NE : ComparisonInt<SETNE>; +defm SLT : ComparisonInt<SETLT>; +defm SLE : ComparisonInt<SETLE>; +defm ULT : ComparisonInt<SETULT>; +defm ULE : ComparisonInt<SETULE>; +defm SGT : ComparisonInt<SETGT>; +defm SGE : ComparisonInt<SETGE>; +defm UGT : ComparisonInt<SETUGT>; +defm UGE : ComparisonInt<SETUGE>; defm CLZ : UnaryInt<ctlz>; defm CTZ : UnaryInt<cttz>; diff --git a/llvm/test/CodeGen/WebAssembly/comparisons_f32.ll b/llvm/test/CodeGen/WebAssembly/comparisons_f32.ll new file mode 100644 index 00000000000..1cb76a4f90f --- /dev/null +++ b/llvm/test/CodeGen/WebAssembly/comparisons_f32.ll @@ -0,0 +1,65 @@ +; RUN: llc < %s -asm-verbose=false | FileCheck %s + +; Test that basic 32-bit floating-point comparison operations assemble as +; expected. + +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: 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) +define i32 @oeq_f32(float %x, float %y) { + %a = fcmp oeq float %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: une_f32: +; CHECK: (setlocal @2 (ne @1 @0)) +define i32 @une_f32(float %x, float %y) { + %a = fcmp une float %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: olt_f32: +; CHECK: (setlocal @2 (lt @1 @0)) +define i32 @olt_f32(float %x, float %y) { + %a = fcmp olt float %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ole_f32: +; CHECK: (setlocal @2 (le @1 @0)) +define i32 @ole_f32(float %x, float %y) { + %a = fcmp ole float %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ogt_f32: +; CHECK: (setlocal @2 (gt @1 @0)) +define i32 @ogt_f32(float %x, float %y) { + %a = fcmp ogt float %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: oge_f32: +; CHECK: (setlocal @2 (ge @1 @0)) +define i32 @oge_f32(float %x, float %y) { + %a = fcmp oge float %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; FIXME test other FP comparisons: ueq, one, ult, ule, ugt, uge. They currently +; are broken and failt to match. diff --git a/llvm/test/CodeGen/WebAssembly/comparisons_f64.ll b/llvm/test/CodeGen/WebAssembly/comparisons_f64.ll new file mode 100644 index 00000000000..446a09badd2 --- /dev/null +++ b/llvm/test/CodeGen/WebAssembly/comparisons_f64.ll @@ -0,0 +1,65 @@ +; RUN: llc < %s -asm-verbose=false | FileCheck %s + +; Test that basic 64-bit floating-point comparison operations assemble as +; expected. + +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: 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) +define i32 @oeq_f64(double %x, double %y) { + %a = fcmp oeq double %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: une_f64: +; CHECK: (setlocal @2 (ne @1 @0)) +define i32 @une_f64(double %x, double %y) { + %a = fcmp une double %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: olt_f64: +; CHECK: (setlocal @2 (lt @1 @0)) +define i32 @olt_f64(double %x, double %y) { + %a = fcmp olt double %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ole_f64: +; CHECK: (setlocal @2 (le @1 @0)) +define i32 @ole_f64(double %x, double %y) { + %a = fcmp ole double %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: ogt_f64: +; CHECK: (setlocal @2 (gt @1 @0)) +define i32 @ogt_f64(double %x, double %y) { + %a = fcmp ogt double %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; CHECK-LABEL: oge_f64: +; CHECK: (setlocal @2 (ge @1 @0)) +define i32 @oge_f64(double %x, double %y) { + %a = fcmp oge double %x, %y + %b = zext i1 %a to i32 + ret i32 %b +} + +; FIXME test other FP comparisons: ueq, one, ult, ule, ugt, uge. They currently +; are broken and failt to match. diff --git a/llvm/test/CodeGen/WebAssembly/comparisons_i32.ll b/llvm/test/CodeGen/WebAssembly/comparisons_i32.ll new file mode 100644 index 00000000000..3dc387f39e9 --- /dev/null +++ b/llvm/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 +} diff --git a/llvm/test/CodeGen/WebAssembly/comparisons_i64.ll b/llvm/test/CodeGen/WebAssembly/comparisons_i64.ll new file mode 100644 index 00000000000..76f331d49af --- /dev/null +++ b/llvm/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 +} |

