; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-keep-registers | FileCheck %s

; Test that basic 32-bit floating-point comparison operations assemble as
; expected.

target triple = "wasm32-unknown-unknown"

define i32 @ord_f32(float %x, float %y) {
; CHECK-LABEL: ord_f32:
; CHECK:         .functype ord_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push4=, 0
; CHECK-NEXT:    local.get $push3=, 0
; CHECK-NEXT:    f32.eq $push1=, $pop4, $pop3
; CHECK-NEXT:    local.get $push6=, 1
; CHECK-NEXT:    local.get $push5=, 1
; CHECK-NEXT:    f32.eq $push0=, $pop6, $pop5
; CHECK-NEXT:    i32.and $push2=, $pop1, $pop0
; CHECK-NEXT:    return $pop2
  %a = fcmp ord float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @uno_f32(float %x, float %y) {
; CHECK-LABEL: uno_f32:
; CHECK:         .functype uno_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push4=, 0
; CHECK-NEXT:    local.get $push3=, 0
; CHECK-NEXT:    f32.ne $push1=, $pop4, $pop3
; CHECK-NEXT:    local.get $push6=, 1
; CHECK-NEXT:    local.get $push5=, 1
; CHECK-NEXT:    f32.ne $push0=, $pop6, $pop5
; CHECK-NEXT:    i32.or $push2=, $pop1, $pop0
; CHECK-NEXT:    return $pop2
  %a = fcmp uno float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @oeq_f32(float %x, float %y) {
; CHECK-LABEL: oeq_f32:
; CHECK:         .functype oeq_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.eq $push0=, $pop2, $pop1
; CHECK-NEXT:    return $pop0
  %a = fcmp oeq float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @une_f32(float %x, float %y) {
; CHECK-LABEL: une_f32:
; CHECK:         .functype une_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.ne $push0=, $pop2, $pop1
; CHECK-NEXT:    return $pop0
  %a = fcmp une float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @olt_f32(float %x, float %y) {
; CHECK-LABEL: olt_f32:
; CHECK:         .functype olt_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.lt $push0=, $pop2, $pop1
; CHECK-NEXT:    return $pop0
  %a = fcmp olt float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @ole_f32(float %x, float %y) {
; CHECK-LABEL: ole_f32:
; CHECK:         .functype ole_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.le $push0=, $pop2, $pop1
; CHECK-NEXT:    return $pop0
  %a = fcmp ole float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @ogt_f32(float %x, float %y) {
; CHECK-LABEL: ogt_f32:
; CHECK:         .functype ogt_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.gt $push0=, $pop2, $pop1
; CHECK-NEXT:    return $pop0
  %a = fcmp ogt float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @oge_f32(float %x, float %y) {
; CHECK-LABEL: oge_f32:
; CHECK:         .functype oge_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.ge $push0=, $pop2, $pop1
; CHECK-NEXT:    return $pop0
  %a = fcmp oge float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

; Expanded comparisons, which also check for NaN.
; These simply rely on SDAG's Expand cond code action.

define i32 @ueq_f32(float %x, float %y) {
; CHECK-LABEL: ueq_f32:
; CHECK:         .functype ueq_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push6=, 0
; CHECK-NEXT:    local.get $push5=, 1
; CHECK-NEXT:    f32.gt $push1=, $pop6, $pop5
; CHECK-NEXT:    local.get $push8=, 0
; CHECK-NEXT:    local.get $push7=, 1
; CHECK-NEXT:    f32.lt $push0=, $pop8, $pop7
; CHECK-NEXT:    i32.or $push2=, $pop1, $pop0
; CHECK-NEXT:    i32.const $push3=, 1
; CHECK-NEXT:    i32.xor $push4=, $pop2, $pop3
; CHECK-NEXT:    return $pop4
  %a = fcmp ueq float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @one_f32(float %x, float %y) {
; CHECK-LABEL: one_f32:
; CHECK:         .functype one_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push4=, 0
; CHECK-NEXT:    local.get $push3=, 1
; CHECK-NEXT:    f32.gt $push1=, $pop4, $pop3
; CHECK-NEXT:    local.get $push6=, 0
; CHECK-NEXT:    local.get $push5=, 1
; CHECK-NEXT:    f32.lt $push0=, $pop6, $pop5
; CHECK-NEXT:    i32.or $push2=, $pop1, $pop0
; CHECK-NEXT:    return $pop2
  %a = fcmp one float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @ult_f32(float %x, float %y) {
; CHECK-LABEL: ult_f32:
; CHECK:         .functype ult_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push4=, 0
; CHECK-NEXT:    local.get $push3=, 1
; CHECK-NEXT:    f32.ge $push0=, $pop4, $pop3
; CHECK-NEXT:    i32.const $push1=, 1
; CHECK-NEXT:    i32.xor $push2=, $pop0, $pop1
; CHECK-NEXT:    return $pop2
  %a = fcmp ult float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @ule_f32(float %x, float %y) {
; CHECK-LABEL: ule_f32:
; CHECK:         .functype ule_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push4=, 0
; CHECK-NEXT:    local.get $push3=, 1
; CHECK-NEXT:    f32.gt $push0=, $pop4, $pop3
; CHECK-NEXT:    i32.const $push1=, 1
; CHECK-NEXT:    i32.xor $push2=, $pop0, $pop1
; CHECK-NEXT:    return $pop2
  %a = fcmp ule float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @ugt_f32(float %x, float %y) {
; CHECK-LABEL: ugt_f32:
; CHECK:         .functype ugt_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push4=, 0
; CHECK-NEXT:    local.get $push3=, 1
; CHECK-NEXT:    f32.le $push0=, $pop4, $pop3
; CHECK-NEXT:    i32.const $push1=, 1
; CHECK-NEXT:    i32.xor $push2=, $pop0, $pop1
; CHECK-NEXT:    return $pop2
  %a = fcmp ugt float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define i32 @uge_f32(float %x, float %y) {
; CHECK-LABEL: uge_f32:
; CHECK:         .functype uge_f32 (f32, f32) -> (i32)
; CHECK-NEXT:  # %bb.0:
; CHECK-NEXT:    local.get $push4=, 0
; CHECK-NEXT:    local.get $push3=, 1
; CHECK-NEXT:    f32.lt $push0=, $pop4, $pop3
; CHECK-NEXT:    i32.const $push1=, 1
; CHECK-NEXT:    i32.xor $push2=, $pop0, $pop1
; CHECK-NEXT:    return $pop2
  %a = fcmp uge float %x, %y
  %b = zext i1 %a to i32
  ret i32 %b
}

define void @olt_f32_branch(float %a, float %b) {
; CHECK-LABEL: olt_f32_branch:
; CHECK:         .functype olt_f32_branch (f32, f32) -> ()
; CHECK-NEXT:  # %bb.0: # %entry
; CHECK-NEXT:    block
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.lt $push0=, $pop2, $pop1
; CHECK-NEXT:    i32.eqz $push3=, $pop0
; CHECK-NEXT:    br_if 0, $pop3 # 0: down to label0
; CHECK-NEXT:  # %bb.1: # %if.then
; CHECK-NEXT:    call call1
; CHECK-NEXT:  .LBB14_2: # %if.end
; CHECK-NEXT:    end_block # label0:
; CHECK-NEXT:    return
entry:
  %cmp = fcmp olt float %a, %b
  br i1 %cmp, label %if.then, label %if.end

if.then:
  tail call void @call1()
  br label %if.end

if.end:
  ret void
}

define void @ole_f32_branch(float %a, float %b) {
; CHECK-LABEL: ole_f32_branch:
; CHECK:         .functype ole_f32_branch (f32, f32) -> ()
; CHECK-NEXT:  # %bb.0: # %entry
; CHECK-NEXT:    block
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.le $push0=, $pop2, $pop1
; CHECK-NEXT:    i32.eqz $push3=, $pop0
; CHECK-NEXT:    br_if 0, $pop3 # 0: down to label1
; CHECK-NEXT:  # %bb.1: # %if.then
; CHECK-NEXT:    call call1
; CHECK-NEXT:  .LBB15_2: # %if.end
; CHECK-NEXT:    end_block # label1:
; CHECK-NEXT:    return
entry:
  %cmp = fcmp ole float %a, %b
  br i1 %cmp, label %if.then, label %if.end

if.then:
  tail call void @call1()
  br label %if.end

if.end:
  ret void
}

define void @ugt_f32_branch(float %a, float %b) {
; CHECK-LABEL: ugt_f32_branch:
; CHECK:         .functype ugt_f32_branch (f32, f32) -> ()
; CHECK-NEXT:  # %bb.0: # %entry
; CHECK-NEXT:    block
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.le $push0=, $pop2, $pop1
; CHECK-NEXT:    i32.eqz $push3=, $pop0
; CHECK-NEXT:    br_if 0, $pop3 # 0: down to label2
; CHECK-NEXT:  # %bb.1: # %if.then
; CHECK-NEXT:    call call1
; CHECK-NEXT:  .LBB16_2: # %if.end
; CHECK-NEXT:    end_block # label2:
; CHECK-NEXT:    return
entry:
  %cmp = fcmp ugt float %a, %b
  br i1 %cmp, label %if.end, label %if.then

if.then:
  tail call void @call1()
  br label %if.end

if.end:
  ret void
}

define void @ogt_f32_branch(float %a, float %b) {
; CHECK-LABEL: ogt_f32_branch:
; CHECK:         .functype ogt_f32_branch (f32, f32) -> ()
; CHECK-NEXT:  # %bb.0: # %entry
; CHECK-NEXT:    block
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.gt $push0=, $pop2, $pop1
; CHECK-NEXT:    i32.eqz $push3=, $pop0
; CHECK-NEXT:    br_if 0, $pop3 # 0: down to label3
; CHECK-NEXT:  # %bb.1: # %if.then
; CHECK-NEXT:    call call1
; CHECK-NEXT:  .LBB17_2: # %if.end
; CHECK-NEXT:    end_block # label3:
; CHECK-NEXT:    return
entry:
  %cmp = fcmp ogt float %a, %b
  br i1 %cmp, label %if.then, label %if.end

if.then:
  tail call void @call1()
  br label %if.end

if.end:
  ret void
}

define void @ult_f32_branch(float %a, float %b) {
; CHECK-LABEL: ult_f32_branch:
; CHECK:         .functype ult_f32_branch (f32, f32) -> ()
; CHECK-NEXT:  # %bb.0: # %entry
; CHECK-NEXT:    block
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.ge $push0=, $pop2, $pop1
; CHECK-NEXT:    i32.eqz $push3=, $pop0
; CHECK-NEXT:    br_if 0, $pop3 # 0: down to label4
; CHECK-NEXT:  # %bb.1: # %if.then
; CHECK-NEXT:    call call1
; CHECK-NEXT:  .LBB18_2: # %if.end
; CHECK-NEXT:    end_block # label4:
; CHECK-NEXT:    return
entry:
  %cmp = fcmp ult float %a, %b
  br i1 %cmp, label %if.end, label %if.then

if.then:
  tail call void @call1()
  br label %if.end

if.end:
  ret void
}

define void @ule_f32_branch(float %a, float %b) {
; CHECK-LABEL: ule_f32_branch:
; CHECK:         .functype ule_f32_branch (f32, f32) -> ()
; CHECK-NEXT:  # %bb.0: # %entry
; CHECK-NEXT:    block
; CHECK-NEXT:    local.get $push2=, 0
; CHECK-NEXT:    local.get $push1=, 1
; CHECK-NEXT:    f32.ge $push0=, $pop2, $pop1
; CHECK-NEXT:    i32.eqz $push3=, $pop0
; CHECK-NEXT:    br_if 0, $pop3 # 0: down to label5
; CHECK-NEXT:  # %bb.1: # %if.then
; CHECK-NEXT:    call call1
; CHECK-NEXT:  .LBB19_2: # %if.end
; CHECK-NEXT:    end_block # label5:
; CHECK-NEXT:    return
entry:
  %cmp = fcmp ult float %a, %b
  br i1 %cmp, label %if.end, label %if.then

if.then:
  tail call void @call1()
  br label %if.end

if.end:
  ret void
}

define void @xor_zext_switch(float %a, float %b) {
; CHECK-LABEL: xor_zext_switch:
; CHECK:         .functype xor_zext_switch (f32, f32) -> ()
; CHECK-NEXT:  # %bb.0: # %entry
; CHECK-NEXT:    block
; CHECK-NEXT:    i32.const $push1=, 0
; CHECK-NEXT:    br_if 0, $pop1 # 0: down to label6
; CHECK-NEXT:  # %bb.1: # %entry
; CHECK-NEXT:    block
; CHECK-NEXT:    block
; CHECK-NEXT:    local.get $push3=, 0
; CHECK-NEXT:    local.get $push2=, 1
; CHECK-NEXT:    f32.ge $push0=, $pop3, $pop2
; CHECK-NEXT:    br_table $pop0, 0, 1, 0 # 0: down to label8
; CHECK-NEXT:    # 1: down to label7
; CHECK-NEXT:  .LBB20_2: # %sw.bb.1
; CHECK-NEXT:    end_block # label8:
; CHECK-NEXT:    call foo1
; CHECK-NEXT:    return
; CHECK-NEXT:  .LBB20_3: # %sw.bb.2
; CHECK-NEXT:    end_block # label7:
; CHECK-NEXT:    call foo2
; CHECK-NEXT:  .LBB20_4: # %exit
; CHECK-NEXT:    end_block # label6:
; CHECK-NEXT:    return
entry:
  %cmp = fcmp ult float %a, %b
  %zext = zext i1 %cmp to i32
  %xor = xor i32 %zext, 1
  switch i32 %xor, label %exit [
    i32 0, label %sw.bb.1
    i32 1, label %sw.bb.2
  ]

sw.bb.1:
  tail call void @foo1()
  br label %exit

sw.bb.2:
  tail call void @foo2()
  br label %exit

exit:
  ret void
}

define void @xor_add_switch(float %a, float %b) {
; CHECK-LABEL: xor_add_switch:
; CHECK:         .functype xor_add_switch (f32, f32) -> ()
; CHECK-NEXT:  # %bb.0: # %entry
; CHECK-NEXT:    block
; CHECK-NEXT:    block
; CHECK-NEXT:    block
; CHECK-NEXT:    block
; CHECK-NEXT:    local.get $push8=, 0
; CHECK-NEXT:    local.get $push7=, 1
; CHECK-NEXT:    f32.ge $push1=, $pop8, $pop7
; CHECK-NEXT:    i32.const $push2=, 1
; CHECK-NEXT:    i32.xor $push3=, $pop1, $pop2
; CHECK-NEXT:    i32.const $push6=, 1
; CHECK-NEXT:    i32.add $push4=, $pop3, $pop6
; CHECK-NEXT:    i32.const $push5=, 1
; CHECK-NEXT:    i32.xor $push0=, $pop4, $pop5
; CHECK-NEXT:    br_table $pop0, 0, 1, 2, 3 # 0: down to label12
; CHECK-NEXT:    # 1: down to label11
; CHECK-NEXT:    # 2: down to label10
; CHECK-NEXT:    # 3: down to label9
; CHECK-NEXT:  .LBB21_1: # %sw.bb.1
; CHECK-NEXT:    end_block # label12:
; CHECK-NEXT:    call foo1
; CHECK-NEXT:    return
; CHECK-NEXT:  .LBB21_2: # %sw.bb.2
; CHECK-NEXT:    end_block # label11:
; CHECK-NEXT:    call foo2
; CHECK-NEXT:    return
; CHECK-NEXT:  .LBB21_3: # %sw.bb.3
; CHECK-NEXT:    end_block # label10:
; CHECK-NEXT:    call foo3
; CHECK-NEXT:  .LBB21_4: # %exit
; CHECK-NEXT:    end_block # label9:
; CHECK-NEXT:    return
entry:
  %cmp = fcmp ult float %a, %b
  %zext = zext i1 %cmp to i32
  %add = add nsw nuw i32 %zext, 1
  %xor = xor i32 %add, 1
  switch i32 %xor, label %exit [
    i32 0, label %sw.bb.1
    i32 1, label %sw.bb.2
    i32 2, label %sw.bb.3
  ]

sw.bb.1:
  tail call void @foo1()
  br label %exit

sw.bb.2:
  tail call void @foo2()
  br label %exit

sw.bb.3:
  tail call void @foo3()
  br label %exit

exit:
  ret void
}

declare void @foo1()
declare void @foo2()
declare void @foo3()
declare void @call1()
