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 => {
27 return None; // FIXME implement checked versions
29 BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
31 let res = if checked {
33 fx.easy_call("__rust_i128_mulo", &[lhs, rhs], out_ty)
35 fx.easy_call("__rust_u128_mulo", &[lhs, rhs], out_ty)
38 let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
39 fx.easy_call("__multi3", &[lhs, rhs], val_ty)
46 Some(fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128))
48 Some(fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128))
54 Some(fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128))
56 Some(fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128))
59 BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
61 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
62 let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
64 let res = match bin_op {
66 let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb);
67 let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
68 fx.bcx.ins().band(lsb_eq, msb_eq)
71 let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb);
72 let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb);
73 fx.bcx.ins().bor(lsb_ne, msb_ne)
81 let cc = match (bin_op, is_signed) {
82 (BinOp::Ge, false) => IntCC::UnsignedGreaterThanOrEqual,
83 (BinOp::Gt, false) => IntCC::UnsignedGreaterThan,
84 (BinOp::Lt, false) => IntCC::UnsignedLessThan,
85 (BinOp::Le, false) => IntCC::UnsignedLessThanOrEqual,
87 (BinOp::Ge, true) => IntCC::SignedGreaterThanOrEqual,
88 (BinOp::Gt, true) => IntCC::SignedGreaterThan,
89 (BinOp::Lt, true) => IntCC::SignedLessThan,
90 (BinOp::Le, true) => IntCC::SignedLessThanOrEqual,
94 let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
95 let lsb_cc = fx.bcx.ins().icmp(cc, lhs_lsb, rhs_lsb);
96 let msb_cc = fx.bcx.ins().icmp(cc, lhs_msb, rhs_msb);
98 fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
102 let res = fx.bcx.ins().bint(types::I8, res);
103 let res = CValue::by_val(res, fx.layout_of(fx.tcx.types.bool));
106 BinOp::Shl | BinOp::Shr => {
107 let is_overflow = if checked {
110 // FIXME support non 128bit rhs
111 /*let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
112 let rhs_msb_gt_0 = fx.bcx.ins().icmp_imm(IntCC::NotEqual, rhs_msb, 0);
113 let rhs_lsb_ge_128 = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThan, rhs_lsb, 127);
114 let is_overflow = fx.bcx.ins().bor(rhs_msb_gt_0, rhs_lsb_ge_128);*/
115 let is_overflow = fx.bcx.ins().bconst(types::B1, false);
117 Some(fx.bcx.ins().bint(types::I8, is_overflow))
122 // Optimize `val >> 64`, because compiler_builtins uses it to deconstruct an 128bit
123 // integer into its lsb and msb.
124 // https://github.com/rust-lang-nursery/compiler-builtins/blob/79a6a1603d5672cbb9187ff41ff4d9b5048ac1cb/src/int/mod.rs#L217
125 if let Some(64) = resolve_value_imm(fx.bcx.func, rhs_val) {
126 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
127 let all_zeros = fx.bcx.ins().iconst(types::I64, 0);
128 let val = match (bin_op, is_signed) {
129 (BinOp::Shr, false) => {
130 let val = fx.bcx.ins().iconcat(lhs_msb, all_zeros);
131 Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.u128)))
133 (BinOp::Shr, true) => {
134 let sign = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, lhs_msb, 0);
135 let all_ones = fx.bcx.ins().iconst(types::I64, u64::max_value() as i64);
136 let all_sign_bits = fx.bcx.ins().select(sign, all_zeros, all_ones);
138 let val = fx.bcx.ins().iconcat(lhs_msb, all_sign_bits);
139 Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.i128)))
142 let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
143 let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb);
144 Some(CValue::by_val(val, fx.layout_of(val_ty)))
148 if let Some(val) = val {
149 if let Some(is_overflow) = is_overflow {
150 let val = val.load_scalar(fx);
151 return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
158 let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
159 let truncated_rhs = CValue::by_val(truncated_rhs, fx.layout_of(fx.tcx.types.u32));
160 let val = match (bin_op, is_signed) {
161 (BinOp::Shl, false) => {
162 fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
164 (BinOp::Shl, true) => {
165 fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
167 (BinOp::Shr, false) => {
168 fx.easy_call("__lshrti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
170 (BinOp::Shr, true) => {
171 fx.easy_call("__ashrti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
173 (_, _) => unreachable!(),
175 if let Some(is_overflow) = is_overflow {
176 let val = val.load_scalar(fx);
177 Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))