]> git.lizzy.rs Git - rust.git/blob - src/codegen_i128.rs
Workaround for missing icmp{,_imm}.i128 legalizations
[rust.git] / src / codegen_i128.rs
1 //! Replaces 128-bit operators with lang item calls
2
3 use crate::prelude::*;
4
5 pub fn maybe_codegen<'a, 'tcx>(
6     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
7     bin_op: BinOp,
8     checked: bool,
9     lhs: CValue<'tcx>,
10     rhs: CValue<'tcx>,
11 ) -> Option<CValue<'tcx>> {
12     if lhs.layout().ty != fx.tcx.types.u128 && lhs.layout().ty != fx.tcx.types.i128 {
13         return None;
14     }
15
16     let lhs_val = lhs.load_scalar(fx);
17     let rhs_val = rhs.load_scalar(fx);
18
19     let is_signed = type_sign(lhs.layout().ty);
20
21     match bin_op {
22         BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
23             assert!(!checked);
24             return None;
25         }
26         BinOp::Add | BinOp::Sub if !checked => return None,
27         BinOp::Add => {
28             let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
29             return Some(if is_signed {
30                 fx.easy_call("__rust_i128_addo", &[lhs, rhs], out_ty)
31             } else {
32                 fx.easy_call("__rust_u128_addo", &[lhs, rhs], out_ty)
33             })
34         }
35         BinOp::Sub => {
36             let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
37             return Some(if is_signed {
38                 fx.easy_call("__rust_i128_subo", &[lhs, rhs], out_ty)
39             } else {
40                 fx.easy_call("__rust_u128_subo", &[lhs, rhs], out_ty)
41             })
42         }
43         BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
44         BinOp::Mul => {
45             let res = if checked {
46                 let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
47                 if is_signed {
48                     fx.easy_call("__rust_i128_mulo", &[lhs, rhs], out_ty)
49                 } else {
50                     fx.easy_call("__rust_u128_mulo", &[lhs, rhs], out_ty)
51                 }
52             } else {
53                 let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
54                 fx.easy_call("__multi3", &[lhs, rhs], val_ty)
55             };
56             return Some(res);
57         }
58         BinOp::Div => {
59             assert!(!checked);
60             if is_signed {
61                 Some(fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128))
62             } else {
63                 Some(fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128))
64             }
65         }
66         BinOp::Rem => {
67             assert!(!checked);
68             if is_signed {
69                 Some(fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128))
70             } else {
71                 Some(fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128))
72             }
73         }
74         BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
75             assert!(!checked);
76             return None;
77         }
78         BinOp::Shl | BinOp::Shr => {
79             let is_overflow = if checked {
80                 // rhs >= 128
81
82                 // FIXME support non 128bit rhs
83                 /*let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
84                 let rhs_msb_gt_0 = fx.bcx.ins().icmp_imm(IntCC::NotEqual, rhs_msb, 0);
85                 let rhs_lsb_ge_128 = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThan, rhs_lsb, 127);
86                 let is_overflow = fx.bcx.ins().bor(rhs_msb_gt_0, rhs_lsb_ge_128);*/
87                 let is_overflow = fx.bcx.ins().bconst(types::B1, false);
88
89                 Some(fx.bcx.ins().bint(types::I8, is_overflow))
90             } else {
91                 None
92             };
93
94             // Optimize `val >> 64`, because compiler_builtins uses it to deconstruct an 128bit
95             // integer into its lsb and msb.
96             // https://github.com/rust-lang-nursery/compiler-builtins/blob/79a6a1603d5672cbb9187ff41ff4d9b5048ac1cb/src/int/mod.rs#L217
97             if let Some(64) = resolve_value_imm(fx.bcx.func, rhs_val) {
98                 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
99                 let all_zeros = fx.bcx.ins().iconst(types::I64, 0);
100                 let val = match (bin_op, is_signed) {
101                     (BinOp::Shr, false) => {
102                         let val = fx.bcx.ins().iconcat(lhs_msb, all_zeros);
103                         Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.u128)))
104                     }
105                     (BinOp::Shr, true) => {
106                         let sign = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, lhs_msb, 0);
107                         let all_ones = fx.bcx.ins().iconst(types::I64, u64::max_value() as i64);
108                         let all_sign_bits = fx.bcx.ins().select(sign, all_zeros, all_ones);
109
110                         let val = fx.bcx.ins().iconcat(lhs_msb, all_sign_bits);
111                         Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.i128)))
112                     }
113                     (BinOp::Shl, _) => {
114                         let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
115                         let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb);
116                         Some(CValue::by_val(val, fx.layout_of(val_ty)))
117                     }
118                     _ => None
119                 };
120                 if let Some(val) = val {
121                     if let Some(is_overflow) = is_overflow {
122                         let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
123                         let val = val.load_scalar(fx);
124                         return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
125                     } else {
126                         return Some(val);
127                     }
128                 }
129             }
130
131             let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
132             let truncated_rhs = CValue::by_val(truncated_rhs, fx.layout_of(fx.tcx.types.u32));
133             let val = match (bin_op, is_signed) {
134                 (BinOp::Shl, false) => {
135                     fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
136                 }
137                 (BinOp::Shl, true) => {
138                     fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
139                 }
140                 (BinOp::Shr, false) => {
141                     fx.easy_call("__lshrti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
142                 }
143                 (BinOp::Shr, true) => {
144                     fx.easy_call("__ashrti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
145                 }
146                 (_, _) => unreachable!(),
147             };
148             if let Some(is_overflow) = is_overflow {
149                 let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
150                 let val = val.load_scalar(fx);
151                 Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
152             } else {
153                 Some(val)
154             }
155         }
156     }
157 }