1
Fork 0

Add unsigned_offset_from on pointers

Like we have `add`/`sub` which are the `usize` version of `offset`, this adds the `usize` equivalent of `offset_from`.  Like how `.add(d)` replaced a whole bunch of `.offset(d as isize)`, you can see from the changes here that it's fairly common that code actually knows the order between the pointers and *wants* a `usize`, not an `isize`.

As a bonus, this can do `sub nuw`+`udiv exact`, rather than `sub`+`sdiv exact`, which can be optimized slightly better because it doesn't have to worry about negatives.  That's why the slice iterators weren't using `offset_from`, though I haven't updated that code in this PR because slices are so perf-critical that I'll do it as its own change.

This is an intrinsic, like `offset_from`, so that it can eventually be allowed in CTFE.  It also allows checking the extra safety condition -- see the test confirming that CTFE catches it if you pass the pointers in the wrong order.
This commit is contained in:
Scott McMurray 2022-04-09 01:27:47 -07:00
parent 6dd68402c5
commit 89a18cb600
19 changed files with 265 additions and 25 deletions

View file

@ -713,14 +713,21 @@ fn codegen_regular_intrinsic_call<'tcx>(
ret.write_cvalue(fx, val);
};
ptr_offset_from, (v ptr, v base) {
ptr_offset_from | ptr_offset_from_unsigned, (v ptr, v base) {
let ty = substs.type_at(0);
let isize_layout = fx.layout_of(fx.tcx.types.isize);
let pointee_size: u64 = fx.layout_of(ty).size.bytes();
let diff = fx.bcx.ins().isub(ptr, base);
let diff_bytes = fx.bcx.ins().isub(ptr, base);
// FIXME this can be an exact division.
let val = CValue::by_val(fx.bcx.ins().sdiv_imm(diff, pointee_size as i64), isize_layout);
let diff = if intrinsic == sym::ptr_offset_from_unsigned {
// Because diff_bytes ULT isize::MAX, this would be fine as signed,
// but unsigned is slightly easier to codegen, so might as well.
fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64)
} else {
fx.bcx.ins().sdiv_imm(diff_bytes, pointee_size as i64)
};
let val = CValue::by_val(diff, isize_layout);
ret.write_cvalue(fx, val);
};