]> git.lizzy.rs Git - rust.git/blob - src/codegen_i128.rs
Directly use Cranelift instructions for 128bit int shifts
[rust.git] / src / codegen_i128.rs
1 //! Replaces 128-bit operators with lang item calls where necessary
2
3 use cranelift_codegen::ir::ArgumentPurpose;
4
5 use crate::prelude::*;
6
7 pub(crate) fn maybe_codegen<'tcx>(
8     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
9     bin_op: BinOp,
10     checked: bool,
11     lhs: CValue<'tcx>,
12     rhs: CValue<'tcx>,
13 ) -> Option<CValue<'tcx>> {
14     if lhs.layout().ty != fx.tcx.types.u128 && lhs.layout().ty != fx.tcx.types.i128 {
15         return None;
16     }
17
18     let lhs_val = lhs.load_scalar(fx);
19     let rhs_val = rhs.load_scalar(fx);
20
21     let is_signed = type_sign(lhs.layout().ty);
22
23     match bin_op {
24         BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
25             assert!(!checked);
26             None
27         }
28         BinOp::Add | BinOp::Sub if !checked => None,
29         BinOp::Mul if !checked => {
30             let val_ty = if is_signed {
31                 fx.tcx.types.i128
32             } else {
33                 fx.tcx.types.u128
34             };
35             Some(fx.easy_call("__multi3", &[lhs, rhs], val_ty))
36         }
37         BinOp::Add | BinOp::Sub | BinOp::Mul => {
38             assert!(checked);
39             let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
40             let out_place = CPlace::new_stack_slot(fx, fx.layout_of(out_ty));
41             let param_types = vec![
42                 AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
43                 AbiParam::new(types::I128),
44                 AbiParam::new(types::I128),
45             ];
46             let args = [
47                 out_place.to_ptr().get_addr(fx),
48                 lhs.load_scalar(fx),
49                 rhs.load_scalar(fx),
50             ];
51             let name = match (bin_op, is_signed) {
52                 (BinOp::Add, false) => "__rust_u128_addo",
53                 (BinOp::Add, true) => "__rust_i128_addo",
54                 (BinOp::Sub, false) => "__rust_u128_subo",
55                 (BinOp::Sub, true) => "__rust_i128_subo",
56                 (BinOp::Mul, false) => "__rust_u128_mulo",
57                 (BinOp::Mul, true) => "__rust_i128_mulo",
58                 _ => unreachable!(),
59             };
60             fx.lib_call(name, param_types, vec![], &args);
61             Some(out_place.to_cvalue(fx))
62         }
63         BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
64         BinOp::Div => {
65             assert!(!checked);
66             if is_signed {
67                 Some(fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128))
68             } else {
69                 Some(fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128))
70             }
71         }
72         BinOp::Rem => {
73             assert!(!checked);
74             if is_signed {
75                 Some(fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128))
76             } else {
77                 Some(fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128))
78             }
79         }
80         BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
81             assert!(!checked);
82             None
83         }
84         BinOp::Shl | BinOp::Shr => {
85             let is_overflow = if checked {
86                 // rhs >= 128
87
88                 // FIXME support non 128bit rhs
89                 /*let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
90                 let rhs_msb_gt_0 = fx.bcx.ins().icmp_imm(IntCC::NotEqual, rhs_msb, 0);
91                 let rhs_lsb_ge_128 = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThan, rhs_lsb, 127);
92                 let is_overflow = fx.bcx.ins().bor(rhs_msb_gt_0, rhs_lsb_ge_128);*/
93                 let is_overflow = fx.bcx.ins().bconst(types::B1, false);
94
95                 Some(fx.bcx.ins().bint(types::I8, is_overflow))
96             } else {
97                 None
98             };
99
100             let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
101             let val = match bin_op {
102                 BinOp::Shl => fx.bcx.ins().ishl(lhs_val, truncated_rhs),
103                 BinOp::Shr => {
104                     if is_signed {
105                         fx.bcx.ins().sshr(lhs_val, truncated_rhs)
106                     } else {
107                         fx.bcx.ins().ushr(lhs_val, truncated_rhs)
108                     }
109                 }
110                 _ => unreachable!(),
111             };
112             if let Some(is_overflow) = is_overflow {
113                 let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
114                 Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
115             } else {
116                 Some(CValue::by_val(val, lhs.layout()))
117             }
118         }
119     }
120 }