]> git.lizzy.rs Git - rust.git/blob - src/codegen_i128.rs
Calculate sign in trans{,_checked}_int_binop instead of caller
[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     out_ty: Ty<'tcx>,
12 ) -> Option<CValue<'tcx>> {
13     if lhs.layout().ty != fx.tcx.types.u128 && lhs.layout().ty != fx.tcx.types.i128 {
14         return None;
15     }
16
17     let lhs_val = lhs.load_scalar(fx);
18     let rhs_val = rhs.load_scalar(fx);
19
20     let is_signed = type_sign(lhs.layout().ty);
21
22     match bin_op {
23         BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
24             assert!(!checked);
25             return None;
26         }
27         BinOp::Add | BinOp::Sub if !checked => return None,
28         BinOp::Add => {
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             return Some(if is_signed {
37                 fx.easy_call("__rust_i128_subo", &[lhs, rhs], out_ty)
38             } else {
39                 fx.easy_call("__rust_u128_subo", &[lhs, rhs], out_ty)
40             })
41         }
42         BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
43         BinOp::Mul => {
44             let res = if checked {
45                 if is_signed {
46                     fx.easy_call("__rust_i128_mulo", &[lhs, rhs], out_ty)
47                 } else {
48                     fx.easy_call("__rust_u128_mulo", &[lhs, rhs], out_ty)
49                 }
50             } else {
51                 let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
52                 fx.easy_call("__multi3", &[lhs, rhs], val_ty)
53             };
54             return Some(res);
55         }
56         BinOp::Div => {
57             assert!(!checked);
58             if is_signed {
59                 Some(fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128))
60             } else {
61                 Some(fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128))
62             }
63         }
64         BinOp::Rem => {
65             assert!(!checked);
66             if is_signed {
67                 Some(fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128))
68             } else {
69                 Some(fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128))
70             }
71         }
72         BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
73             assert!(!checked);
74             let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
75             let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
76
77             let res = match bin_op {
78                 BinOp::Eq => {
79                     let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb);
80                     let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
81                     fx.bcx.ins().band(lsb_eq, msb_eq)
82                 }
83                 BinOp::Ne => {
84                     let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb);
85                     let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb);
86                     fx.bcx.ins().bor(lsb_ne, msb_ne)
87                 }
88                 _ => {
89                     // if msb_eq {
90                     //     lsb_cc
91                     // } else {
92                     //     msb_cc
93                     // }
94                     let cc = match (bin_op, is_signed) {
95                         (BinOp::Ge, false) => IntCC::UnsignedGreaterThanOrEqual,
96                         (BinOp::Gt, false) => IntCC::UnsignedGreaterThan,
97                         (BinOp::Lt, false) => IntCC::UnsignedLessThan,
98                         (BinOp::Le, false) => IntCC::UnsignedLessThanOrEqual,
99
100                         (BinOp::Ge, true) => IntCC::SignedGreaterThanOrEqual,
101                         (BinOp::Gt, true) => IntCC::SignedGreaterThan,
102                         (BinOp::Lt, true) => IntCC::SignedLessThan,
103                         (BinOp::Le, true) => IntCC::SignedLessThanOrEqual,
104                         _ => unreachable!(),
105                     };
106
107                     let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
108                     let lsb_cc = fx.bcx.ins().icmp(cc, lhs_lsb, rhs_lsb);
109                     let msb_cc = fx.bcx.ins().icmp(cc, lhs_msb, rhs_msb);
110
111                     fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
112                 }
113             };
114
115             let res = fx.bcx.ins().bint(types::I8, res);
116             let res = CValue::by_val(res, fx.layout_of(fx.tcx.types.bool));
117             return Some(res);
118         }
119         BinOp::Shl | BinOp::Shr => {
120             let is_overflow = if checked {
121                 // rhs >= 128
122
123                 // FIXME support non 128bit rhs
124                 /*let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
125                 let rhs_msb_gt_0 = fx.bcx.ins().icmp_imm(IntCC::NotEqual, rhs_msb, 0);
126                 let rhs_lsb_ge_128 = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThan, rhs_lsb, 127);
127                 let is_overflow = fx.bcx.ins().bor(rhs_msb_gt_0, rhs_lsb_ge_128);*/
128                 let is_overflow = fx.bcx.ins().bconst(types::B1, false);
129
130                 Some(fx.bcx.ins().bint(types::I8, is_overflow))
131             } else {
132                 None
133             };
134
135             // Optimize `val >> 64`, because compiler_builtins uses it to deconstruct an 128bit
136             // integer into its lsb and msb.
137             // https://github.com/rust-lang-nursery/compiler-builtins/blob/79a6a1603d5672cbb9187ff41ff4d9b5048ac1cb/src/int/mod.rs#L217
138             if let Some(64) = resolve_value_imm(fx.bcx.func, rhs_val) {
139                 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
140                 let all_zeros = fx.bcx.ins().iconst(types::I64, 0);
141                 let val = match (bin_op, is_signed) {
142                     (BinOp::Shr, false) => {
143                         let val = fx.bcx.ins().iconcat(lhs_msb, all_zeros);
144                         Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.u128)))
145                     }
146                     (BinOp::Shr, true) => {
147                         let sign = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, lhs_msb, 0);
148                         let all_ones = fx.bcx.ins().iconst(types::I64, u64::max_value() as i64);
149                         let all_sign_bits = fx.bcx.ins().select(sign, all_zeros, all_ones);
150
151                         let val = fx.bcx.ins().iconcat(lhs_msb, all_sign_bits);
152                         Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.i128)))
153                     }
154                     (BinOp::Shl, _) => {
155                         let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
156                         let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb);
157                         Some(CValue::by_val(val, fx.layout_of(val_ty)))
158                     }
159                     _ => None
160                 };
161                 if let Some(val) = val {
162                     if let Some(is_overflow) = is_overflow {
163                         let val = val.load_scalar(fx);
164                         return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
165                     } else {
166                         return Some(val);
167                     }
168                 }
169             }
170
171             let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
172             let truncated_rhs = CValue::by_val(truncated_rhs, fx.layout_of(fx.tcx.types.u32));
173             let val = match (bin_op, is_signed) {
174                 (BinOp::Shl, false) => {
175                     fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
176                 }
177                 (BinOp::Shl, true) => {
178                     fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
179                 }
180                 (BinOp::Shr, false) => {
181                     fx.easy_call("__lshrti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
182                 }
183                 (BinOp::Shr, true) => {
184                     fx.easy_call("__ashrti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
185                 }
186                 (_, _) => unreachable!(),
187             };
188             if let Some(is_overflow) = is_overflow {
189                 let val = val.load_scalar(fx);
190                 Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
191             } else {
192                 Some(val)
193             }
194         }
195     }
196 }