1 //! Replaces 128-bit operators with lang item calls
5 pub fn maybe_codegen<'a, 'tcx>(
6 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
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);
22 BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
26 BinOp::Add | BinOp::Sub if !checked => return None,
28 return Some(if is_signed {
29 fx.easy_call("__rust_i128_addo", &[lhs, rhs], out_ty)
31 fx.easy_call("__rust_u128_addo", &[lhs, rhs], out_ty)
35 return Some(if is_signed {
36 fx.easy_call("__rust_i128_subo", &[lhs, rhs], out_ty)
38 fx.easy_call("__rust_u128_subo", &[lhs, rhs], out_ty)
41 BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
43 let res = if checked {
45 fx.easy_call("__rust_i128_mulo", &[lhs, rhs], out_ty)
47 fx.easy_call("__rust_u128_mulo", &[lhs, rhs], out_ty)
50 let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
51 fx.easy_call("__multi3", &[lhs, rhs], val_ty)
58 Some(fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128))
60 Some(fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128))
66 Some(fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128))
68 Some(fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128))
71 BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
73 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
74 let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
76 let res = match bin_op {
78 let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb);
79 let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
80 fx.bcx.ins().band(lsb_eq, msb_eq)
83 let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb);
84 let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb);
85 fx.bcx.ins().bor(lsb_ne, msb_ne)
93 let cc = match (bin_op, is_signed) {
94 (BinOp::Ge, false) => IntCC::UnsignedGreaterThanOrEqual,
95 (BinOp::Gt, false) => IntCC::UnsignedGreaterThan,
96 (BinOp::Lt, false) => IntCC::UnsignedLessThan,
97 (BinOp::Le, false) => IntCC::UnsignedLessThanOrEqual,
99 (BinOp::Ge, true) => IntCC::SignedGreaterThanOrEqual,
100 (BinOp::Gt, true) => IntCC::SignedGreaterThan,
101 (BinOp::Lt, true) => IntCC::SignedLessThan,
102 (BinOp::Le, true) => IntCC::SignedLessThanOrEqual,
106 let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
107 let lsb_cc = fx.bcx.ins().icmp(cc, lhs_lsb, rhs_lsb);
108 let msb_cc = fx.bcx.ins().icmp(cc, lhs_msb, rhs_msb);
110 fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
114 let res = fx.bcx.ins().bint(types::I8, res);
115 let res = CValue::by_val(res, fx.layout_of(fx.tcx.types.bool));
118 BinOp::Shl | BinOp::Shr => {
119 let is_overflow = if checked {
122 // FIXME support non 128bit rhs
123 /*let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
124 let rhs_msb_gt_0 = fx.bcx.ins().icmp_imm(IntCC::NotEqual, rhs_msb, 0);
125 let rhs_lsb_ge_128 = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThan, rhs_lsb, 127);
126 let is_overflow = fx.bcx.ins().bor(rhs_msb_gt_0, rhs_lsb_ge_128);*/
127 let is_overflow = fx.bcx.ins().bconst(types::B1, false);
129 Some(fx.bcx.ins().bint(types::I8, is_overflow))
134 // Optimize `val >> 64`, because compiler_builtins uses it to deconstruct an 128bit
135 // integer into its lsb and msb.
136 // https://github.com/rust-lang-nursery/compiler-builtins/blob/79a6a1603d5672cbb9187ff41ff4d9b5048ac1cb/src/int/mod.rs#L217
137 if let Some(64) = resolve_value_imm(fx.bcx.func, rhs_val) {
138 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
139 let all_zeros = fx.bcx.ins().iconst(types::I64, 0);
140 let val = match (bin_op, is_signed) {
141 (BinOp::Shr, false) => {
142 let val = fx.bcx.ins().iconcat(lhs_msb, all_zeros);
143 Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.u128)))
145 (BinOp::Shr, true) => {
146 let sign = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, lhs_msb, 0);
147 let all_ones = fx.bcx.ins().iconst(types::I64, u64::max_value() as i64);
148 let all_sign_bits = fx.bcx.ins().select(sign, all_zeros, all_ones);
150 let val = fx.bcx.ins().iconcat(lhs_msb, all_sign_bits);
151 Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.i128)))
154 let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
155 let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb);
156 Some(CValue::by_val(val, fx.layout_of(val_ty)))
160 if let Some(val) = val {
161 if let Some(is_overflow) = is_overflow {
162 let val = val.load_scalar(fx);
163 return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
170 let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
171 let truncated_rhs = CValue::by_val(truncated_rhs, fx.layout_of(fx.tcx.types.u32));
172 let val = match (bin_op, is_signed) {
173 (BinOp::Shl, false) => {
174 fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
176 (BinOp::Shl, true) => {
177 fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
179 (BinOp::Shr, false) => {
180 fx.easy_call("__lshrti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
182 (BinOp::Shr, true) => {
183 fx.easy_call("__ashrti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
185 (_, _) => unreachable!(),
187 if let Some(is_overflow) = is_overflow {
188 let val = val.load_scalar(fx);
189 Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))