Skip to content

Commit b96de9e

Browse files
committed
Remove unchecked_add/sub/mul/shl/shr from CTFE/cg_ssa/cg_clif
1 parent bd71c26 commit b96de9e

File tree

3 files changed

+9
-91
lines changed

3 files changed

+9
-91
lines changed

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -472,25 +472,11 @@ fn codegen_regular_intrinsic_call<'tcx>(
472472
ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
473473
}
474474

475-
sym::unchecked_add
476-
| sym::unchecked_sub
477-
| sym::unchecked_mul
478-
| sym::exact_div
479-
| sym::unchecked_shl
480-
| sym::unchecked_shr => {
475+
sym::exact_div => {
481476
intrinsic_args!(fx, args => (x, y); intrinsic);
482477

483-
// FIXME trap on overflow
484-
let bin_op = match intrinsic {
485-
sym::unchecked_add => BinOp::Add,
486-
sym::unchecked_sub => BinOp::Sub,
487-
sym::unchecked_mul => BinOp::Mul,
488-
sym::exact_div => BinOp::Div,
489-
sym::unchecked_shl => BinOp::Shl,
490-
sym::unchecked_shr => BinOp::Shr,
491-
_ => unreachable!(),
492-
};
493-
let res = crate::num::codegen_int_binop(fx, bin_op, x, y);
478+
// FIXME trap on inexact
479+
let res = crate::num::codegen_int_binop(fx, BinOp::Div, x, y);
494480
ret.write_cvalue(fx, res);
495481
}
496482
sym::saturating_add | sym::saturating_sub => {

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+6-43
Original file line numberDiff line numberDiff line change
@@ -211,52 +211,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
211211
args[1].val.unaligned_volatile_store(bx, dst);
212212
return;
213213
}
214-
| sym::unchecked_shl
215-
| sym::unchecked_shr
216-
| sym::unchecked_add
217-
| sym::unchecked_sub
218-
| sym::unchecked_mul
219-
| sym::exact_div => {
214+
sym::exact_div => {
220215
let ty = arg_tys[0];
221216
match int_type_width_signed(ty, bx.tcx()) {
222-
Some((_width, signed)) => match name {
223-
sym::exact_div => {
224-
if signed {
225-
bx.exactsdiv(args[0].immediate(), args[1].immediate())
226-
} else {
227-
bx.exactudiv(args[0].immediate(), args[1].immediate())
228-
}
229-
}
230-
sym::unchecked_shl => bx.shl(args[0].immediate(), args[1].immediate()),
231-
sym::unchecked_shr => {
232-
if signed {
233-
bx.ashr(args[0].immediate(), args[1].immediate())
234-
} else {
235-
bx.lshr(args[0].immediate(), args[1].immediate())
236-
}
237-
}
238-
sym::unchecked_add => {
239-
if signed {
240-
bx.unchecked_sadd(args[0].immediate(), args[1].immediate())
241-
} else {
242-
bx.unchecked_uadd(args[0].immediate(), args[1].immediate())
243-
}
244-
}
245-
sym::unchecked_sub => {
246-
if signed {
247-
bx.unchecked_ssub(args[0].immediate(), args[1].immediate())
248-
} else {
249-
bx.unchecked_usub(args[0].immediate(), args[1].immediate())
250-
}
251-
}
252-
sym::unchecked_mul => {
253-
if signed {
254-
bx.unchecked_smul(args[0].immediate(), args[1].immediate())
255-
} else {
256-
bx.unchecked_umul(args[0].immediate(), args[1].immediate())
257-
}
217+
Some((_width, signed)) => {
218+
if signed {
219+
bx.exactsdiv(args[0].immediate(), args[1].immediate())
220+
} else {
221+
bx.exactudiv(args[0].immediate(), args[1].immediate())
258222
}
259-
_ => bug!(),
260223
},
261224
None => {
262225
bx.tcx().sess.emit_err(InvalidMonomorphization::BasicIntegerType { span, name, ty });

compiler/rustc_const_eval/src/interpret/intrinsics.rs

-31
Original file line numberDiff line numberDiff line change
@@ -234,37 +234,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
234234
let r = self.read_immediate(&args[1])?;
235235
self.exact_div(&l, &r, dest)?;
236236
}
237-
sym::unchecked_shl
238-
| sym::unchecked_shr
239-
| sym::unchecked_add
240-
| sym::unchecked_sub
241-
| sym::unchecked_mul => {
242-
let l = self.read_immediate(&args[0])?;
243-
let r = self.read_immediate(&args[1])?;
244-
let bin_op = match intrinsic_name {
245-
sym::unchecked_shl => BinOp::Shl,
246-
sym::unchecked_shr => BinOp::Shr,
247-
sym::unchecked_add => BinOp::Add,
248-
sym::unchecked_sub => BinOp::Sub,
249-
sym::unchecked_mul => BinOp::Mul,
250-
_ => bug!(),
251-
};
252-
let (val, overflowed, _ty) = self.overflowing_binary_op(bin_op, &l, &r)?;
253-
if overflowed {
254-
let layout = self.layout_of(substs.type_at(0))?;
255-
let r_val = r.to_scalar().to_bits(layout.size)?;
256-
if let sym::unchecked_shl | sym::unchecked_shr = intrinsic_name {
257-
throw_ub_custom!(
258-
fluent::const_eval_overflow_shift,
259-
val = r_val,
260-
name = intrinsic_name
261-
);
262-
} else {
263-
throw_ub_custom!(fluent::const_eval_overflow, name = intrinsic_name);
264-
}
265-
}
266-
self.write_scalar(val, dest)?;
267-
}
268237
sym::rotate_left | sym::rotate_right => {
269238
// rotate_left: (X << (S % BW)) | (X >> ((BW - S) % BW))
270239
// rotate_right: (X << ((BW - S) % BW)) | (X >> (S % BW))

0 commit comments

Comments
 (0)