1 //! Replaces 128-bit operators with lang item calls where necessary
3 use cranelift_codegen::ir::ArgumentPurpose;
7 pub(crate) fn maybe_codegen<'tcx>(
8 fx: &mut FunctionCx<'_, 'tcx, impl Module>,
13 ) -> Option<CValue<'tcx>> {
14 if lhs.layout().ty != fx.tcx.types.u128 && lhs.layout().ty != fx.tcx.types.i128 {
18 let lhs_val = lhs.load_scalar(fx);
19 let rhs_val = rhs.load_scalar(fx);
21 let is_signed = type_sign(lhs.layout().ty);
24 BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
28 BinOp::Add | BinOp::Sub if !checked => None,
29 BinOp::Mul if !checked => {
30 let val_ty = if is_signed {
35 Some(fx.easy_call("__multi3", &[lhs, rhs], val_ty))
37 BinOp::Add | BinOp::Sub | BinOp::Mul => {
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),
47 out_place.to_ptr().get_addr(fx),
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",
60 fx.lib_call(name, param_types, vec![], &args);
61 Some(out_place.to_cvalue(fx))
63 BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
67 Some(fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128))
69 Some(fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128))
75 Some(fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128))
77 Some(fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128))
80 BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
84 BinOp::Shl | BinOp::Shr => {
85 let is_overflow = if checked {
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);
95 Some(fx.bcx.ins().bint(types::I8, is_overflow))
100 // Optimize `val >> 64`, because compiler_builtins uses it to deconstruct an 128bit
101 // integer into its lsb and msb.
102 // https://github.com/rust-lang-nursery/compiler-builtins/blob/79a6a1603d5672cbb9187ff41ff4d9b5048ac1cb/src/int/mod.rs#L217
103 if resolve_value_imm(fx.bcx.func, rhs_val) == Some(64) {
104 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
105 let all_zeros = fx.bcx.ins().iconst(types::I64, 0);
106 let val = match (bin_op, is_signed) {
107 (BinOp::Shr, false) => {
108 let val = fx.bcx.ins().iconcat(lhs_msb, all_zeros);
109 Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.u128)))
111 (BinOp::Shr, true) => {
112 let sign = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, lhs_msb, 0);
113 let all_ones = fx.bcx.ins().iconst(types::I64, u64::MAX as i64);
114 let all_sign_bits = fx.bcx.ins().select(sign, all_zeros, all_ones);
116 let val = fx.bcx.ins().iconcat(lhs_msb, all_sign_bits);
117 Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.i128)))
120 let val_ty = if is_signed {
125 let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb);
126 Some(CValue::by_val(val, fx.layout_of(val_ty)))
130 if let Some(val) = val {
131 if let Some(is_overflow) = is_overflow {
132 let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
133 let val = val.load_scalar(fx);
134 return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)));
141 let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
142 let truncated_rhs = CValue::by_val(truncated_rhs, fx.layout_of(fx.tcx.types.u32));
143 let val = match (bin_op, is_signed) {
144 (BinOp::Shl, false) => {
145 fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
147 (BinOp::Shl, true) => {
148 fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
150 (BinOp::Shr, false) => {
151 fx.easy_call("__lshrti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
153 (BinOp::Shr, true) => {
154 fx.easy_call("__ashrti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
156 (_, _) => unreachable!(),
158 if let Some(is_overflow) = is_overflow {
159 let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
160 let val = val.load_scalar(fx);
161 Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))