]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/codegen_i128.rs
Auto merge of #81942 - the8472:reduce-ui-test-threads, r=Mark-Simulacrum
[rust.git] / compiler / rustc_codegen_cranelift / 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>,
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
15         && lhs.layout().ty != fx.tcx.types.i128
16         && rhs.layout().ty != fx.tcx.types.u128
17         && rhs.layout().ty != fx.tcx.types.i128
18     {
19         return None;
20     }
21
22     let lhs_val = lhs.load_scalar(fx);
23     let rhs_val = rhs.load_scalar(fx);
24
25     let is_signed = type_sign(lhs.layout().ty);
26
27     match bin_op {
28         BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
29             assert!(!checked);
30             None
31         }
32         BinOp::Add | BinOp::Sub if !checked => None,
33         BinOp::Mul if !checked => {
34             let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
35             if fx.tcx.sess.target.is_like_windows {
36                 let ret_place = CPlace::new_stack_slot(fx, lhs.layout());
37                 let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
38                 let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
39                 assert!(lhs_extra.is_none());
40                 assert!(rhs_extra.is_none());
41                 let args =
42                     [ret_place.to_ptr().get_addr(fx), lhs_ptr.get_addr(fx), rhs_ptr.get_addr(fx)];
43                 fx.lib_call(
44                     "__multi3",
45                     vec![
46                         AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
47                         AbiParam::new(pointer_ty(fx.tcx)),
48                         AbiParam::new(pointer_ty(fx.tcx)),
49                     ],
50                     vec![],
51                     &args,
52                 );
53                 Some(ret_place.to_cvalue(fx))
54             } else {
55                 Some(fx.easy_call("__multi3", &[lhs, rhs], val_ty))
56             }
57         }
58         BinOp::Add | BinOp::Sub | BinOp::Mul => {
59             assert!(checked);
60             let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
61             let out_place = CPlace::new_stack_slot(fx, fx.layout_of(out_ty));
62             let (param_types, args) = if fx.tcx.sess.target.is_like_windows {
63                 let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
64                 let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
65                 assert!(lhs_extra.is_none());
66                 assert!(rhs_extra.is_none());
67                 (
68                     vec![
69                         AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
70                         AbiParam::new(pointer_ty(fx.tcx)),
71                         AbiParam::new(pointer_ty(fx.tcx)),
72                     ],
73                     [out_place.to_ptr().get_addr(fx), lhs_ptr.get_addr(fx), rhs_ptr.get_addr(fx)],
74                 )
75             } else {
76                 (
77                     vec![
78                         AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
79                         AbiParam::new(types::I128),
80                         AbiParam::new(types::I128),
81                     ],
82                     [out_place.to_ptr().get_addr(fx), lhs.load_scalar(fx), rhs.load_scalar(fx)],
83                 )
84             };
85             let name = match (bin_op, is_signed) {
86                 (BinOp::Add, false) => "__rust_u128_addo",
87                 (BinOp::Add, true) => "__rust_i128_addo",
88                 (BinOp::Sub, false) => "__rust_u128_subo",
89                 (BinOp::Sub, true) => "__rust_i128_subo",
90                 (BinOp::Mul, false) => "__rust_u128_mulo",
91                 (BinOp::Mul, true) => "__rust_i128_mulo",
92                 _ => unreachable!(),
93             };
94             fx.lib_call(name, param_types, vec![], &args);
95             Some(out_place.to_cvalue(fx))
96         }
97         BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
98         BinOp::Div | BinOp::Rem => {
99             assert!(!checked);
100             let name = match (bin_op, is_signed) {
101                 (BinOp::Div, false) => "__udivti3",
102                 (BinOp::Div, true) => "__divti3",
103                 (BinOp::Rem, false) => "__umodti3",
104                 (BinOp::Rem, true) => "__modti3",
105                 _ => unreachable!(),
106             };
107             if fx.tcx.sess.target.is_like_windows {
108                 let (lhs_ptr, lhs_extra) = lhs.force_stack(fx);
109                 let (rhs_ptr, rhs_extra) = rhs.force_stack(fx);
110                 assert!(lhs_extra.is_none());
111                 assert!(rhs_extra.is_none());
112                 let args = [lhs_ptr.get_addr(fx), rhs_ptr.get_addr(fx)];
113                 let ret = fx.lib_call(
114                     name,
115                     vec![AbiParam::new(pointer_ty(fx.tcx)), AbiParam::new(pointer_ty(fx.tcx))],
116                     vec![AbiParam::new(types::I64X2)],
117                     &args,
118                 )[0];
119                 // FIXME use bitcast instead of store to get from i64x2 to i128
120                 let ret_place = CPlace::new_stack_slot(fx, lhs.layout());
121                 ret_place.to_ptr().store(fx, ret, MemFlags::trusted());
122                 Some(ret_place.to_cvalue(fx))
123             } else {
124                 Some(fx.easy_call(name, &[lhs, rhs], lhs.layout().ty))
125             }
126         }
127         BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
128             assert!(!checked);
129             None
130         }
131         BinOp::Shl | BinOp::Shr => {
132             let is_overflow = if checked {
133                 // rhs >= 128
134
135                 // FIXME support non 128bit rhs
136                 /*let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
137                 let rhs_msb_gt_0 = fx.bcx.ins().icmp_imm(IntCC::NotEqual, rhs_msb, 0);
138                 let rhs_lsb_ge_128 = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThan, rhs_lsb, 127);
139                 let is_overflow = fx.bcx.ins().bor(rhs_msb_gt_0, rhs_lsb_ge_128);*/
140                 let is_overflow = fx.bcx.ins().bconst(types::B1, false);
141
142                 Some(fx.bcx.ins().bint(types::I8, is_overflow))
143             } else {
144                 None
145             };
146
147             let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
148             let val = match bin_op {
149                 BinOp::Shl => fx.bcx.ins().ishl(lhs_val, truncated_rhs),
150                 BinOp::Shr => {
151                     if is_signed {
152                         fx.bcx.ins().sshr(lhs_val, truncated_rhs)
153                     } else {
154                         fx.bcx.ins().ushr(lhs_val, truncated_rhs)
155                     }
156                 }
157                 _ => unreachable!(),
158             };
159             if let Some(is_overflow) = is_overflow {
160                 let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
161                 Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
162             } else {
163                 Some(CValue::by_val(val, lhs.layout()))
164             }
165         }
166     }
167 }