]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/codegen_i128.rs
Auto merge of #82102 - nagisa:nagisa/fix-dwo-name, r=davidtwco
[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, 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             // 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)))
110                     }
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);
115
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)))
118                     }
119                     (BinOp::Shl, _) => {
120                         let val_ty = if is_signed {
121                             fx.tcx.types.i128
122                         } else {
123                             fx.tcx.types.u128
124                         };
125                         let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb);
126                         Some(CValue::by_val(val, fx.layout_of(val_ty)))
127                     }
128                     _ => None,
129                 };
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)));
135                     } else {
136                         return Some(val);
137                     }
138                 }
139             }
140
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)
146                 }
147                 (BinOp::Shl, true) => {
148                     fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
149                 }
150                 (BinOp::Shr, false) => {
151                     fx.easy_call("__lshrti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
152                 }
153                 (BinOp::Shr, true) => {
154                     fx.easy_call("__ashrti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
155                 }
156                 (_, _) => unreachable!(),
157             };
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)))
162             } else {
163                 Some(val)
164             }
165         }
166     }
167 }