22
33use crate :: prelude:: * ;
44
5- pub ( crate ) fn bin_op_to_intcc ( bin_op : BinOp , signed : bool ) -> Option < IntCC > {
5+ pub ( crate ) fn bin_op_to_intcc ( bin_op : BinOp , signed : bool ) -> IntCC {
66 use BinOp :: * ;
77 use IntCC :: * ;
8- Some ( match bin_op {
8+ match bin_op {
99 Eq => Equal ,
1010 Lt => {
1111 if signed {
@@ -36,8 +36,8 @@ pub(crate) fn bin_op_to_intcc(bin_op: BinOp, signed: bool) -> Option<IntCC> {
3636 UnsignedGreaterThan
3737 }
3838 }
39- _ => return None ,
40- } )
39+ _ => unreachable ! ( ) ,
40+ }
4141}
4242
4343fn codegen_three_way_compare < ' tcx > (
@@ -48,8 +48,8 @@ fn codegen_three_way_compare<'tcx>(
4848) -> CValue < ' tcx > {
4949 // This emits `(lhs > rhs) - (lhs < rhs)`, which is cranelift's preferred form per
5050 // <https://github.com/bytecodealliance/wasmtime/blob/8052bb9e3b792503b225f2a5b2ba3bc023bff462/cranelift/codegen/src/prelude_opt.isle#L41-L47>
51- let gt_cc = crate :: num:: bin_op_to_intcc ( BinOp :: Gt , signed) . unwrap ( ) ;
52- let lt_cc = crate :: num:: bin_op_to_intcc ( BinOp :: Lt , signed) . unwrap ( ) ;
51+ let gt_cc = crate :: num:: bin_op_to_intcc ( BinOp :: Gt , signed) ;
52+ let lt_cc = crate :: num:: bin_op_to_intcc ( BinOp :: Lt , signed) ;
5353 let gt = fx. bcx . ins ( ) . icmp ( gt_cc, lhs, rhs) ;
5454 let lt = fx. bcx . ins ( ) . icmp ( lt_cc, lhs, rhs) ;
5555 let val = fx. bcx . ins ( ) . isub ( gt, lt) ;
@@ -63,11 +63,7 @@ fn codegen_compare_bin_op<'tcx>(
6363 lhs : Value ,
6464 rhs : Value ,
6565) -> CValue < ' tcx > {
66- if bin_op == BinOp :: Cmp {
67- return codegen_three_way_compare ( fx, signed, lhs, rhs) ;
68- }
69-
70- let intcc = crate :: num:: bin_op_to_intcc ( bin_op, signed) . unwrap ( ) ;
66+ let intcc = crate :: num:: bin_op_to_intcc ( bin_op, signed) ;
7167 let val = fx. bcx . ins ( ) . icmp ( intcc, lhs, rhs) ;
7268 CValue :: by_val ( val, fx. layout_of ( fx. tcx . types . bool ) )
7369}
@@ -79,7 +75,7 @@ pub(crate) fn codegen_binop<'tcx>(
7975 in_rhs : CValue < ' tcx > ,
8076) -> CValue < ' tcx > {
8177 match bin_op {
82- BinOp :: Eq | BinOp :: Lt | BinOp :: Le | BinOp :: Ne | BinOp :: Ge | BinOp :: Gt | BinOp :: Cmp => {
78+ BinOp :: Eq | BinOp :: Lt | BinOp :: Le | BinOp :: Ne | BinOp :: Ge | BinOp :: Gt => {
8379 match in_lhs. layout ( ) . ty . kind ( ) {
8480 ty:: Bool | ty:: Uint ( _) | ty:: Int ( _) | ty:: Char => {
8581 let signed = type_sign ( in_lhs. layout ( ) . ty ) ;
@@ -91,6 +87,16 @@ pub(crate) fn codegen_binop<'tcx>(
9187 _ => { }
9288 }
9389 }
90+ BinOp :: Cmp => match in_lhs. layout ( ) . ty . kind ( ) {
91+ ty:: Bool | ty:: Uint ( _) | ty:: Int ( _) | ty:: Char => {
92+ let signed = type_sign ( in_lhs. layout ( ) . ty ) ;
93+ let lhs = in_lhs. load_scalar ( fx) ;
94+ let rhs = in_rhs. load_scalar ( fx) ;
95+
96+ return codegen_three_way_compare ( fx, signed, lhs, rhs) ;
97+ }
98+ _ => { }
99+ } ,
94100 _ => { }
95101 }
96102
@@ -357,14 +363,12 @@ pub(crate) fn codegen_float_binop<'tcx>(
357363 _ => bug ! ( ) ,
358364 } ;
359365
360- let ret_val = fx. lib_call (
366+ fx. lib_call (
361367 name,
362368 vec ! [ AbiParam :: new( ty) , AbiParam :: new( ty) ] ,
363369 vec ! [ AbiParam :: new( ty) ] ,
364370 & [ lhs, rhs] ,
365- ) [ 0 ] ;
366-
367- return CValue :: by_val ( ret_val, in_lhs. layout ( ) ) ;
371+ ) [ 0 ]
368372 }
369373 BinOp :: Eq | BinOp :: Lt | BinOp :: Le | BinOp :: Ne | BinOp :: Ge | BinOp :: Gt => {
370374 let fltcc = match bin_op {
@@ -431,13 +435,9 @@ pub(crate) fn codegen_ptr_binop<'tcx>(
431435 BinOp :: Lt | BinOp :: Le | BinOp :: Ge | BinOp :: Gt => {
432436 let ptr_eq = fx. bcx . ins ( ) . icmp ( IntCC :: Equal , lhs_ptr, rhs_ptr) ;
433437
434- let ptr_cmp =
435- fx. bcx . ins ( ) . icmp ( bin_op_to_intcc ( bin_op, false ) . unwrap ( ) , lhs_ptr, rhs_ptr) ;
436- let extra_cmp = fx. bcx . ins ( ) . icmp (
437- bin_op_to_intcc ( bin_op, false ) . unwrap ( ) ,
438- lhs_extra,
439- rhs_extra,
440- ) ;
438+ let ptr_cmp = fx. bcx . ins ( ) . icmp ( bin_op_to_intcc ( bin_op, false ) , lhs_ptr, rhs_ptr) ;
439+ let extra_cmp =
440+ fx. bcx . ins ( ) . icmp ( bin_op_to_intcc ( bin_op, false ) , lhs_extra, rhs_extra) ;
441441
442442 fx. bcx . ins ( ) . select ( ptr_eq, extra_cmp, ptr_cmp)
443443 }
0 commit comments