Skip to content

Commit 21be094

Browse files
committed
Improve code emitted for inserting padding before unsized field.
Hat-tip to eddyb for the appropriate bit-trickery here.
1 parent 26f4ebe commit 21be094

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

src/librustc_trans/trans/glue.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -475,21 +475,13 @@ pub fn size_and_align_of_dst<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t: Ty<'tcx>, in
475475
//
476476
// `size + ((size & (align-1)) ? align : 0)`
477477
//
478-
// Currently I am emulating the above via:
478+
// emulated via the semi-standard fast bit trick:
479479
//
480-
// `size + ((size & (align-1)) * align-(size & (align-1)))`
481-
//
482-
// because I am not sure which is cheaper between a branch
483-
// or a multiply.
484-
485-
let mask = Sub(bcx, align, C_uint(bcx.ccx(), 1_u64), dbloc);
486-
let lowbits = And(bcx, size, mask, DebugLoc::None);
487-
let nonzero = ICmp(bcx, llvm::IntNE, lowbits, C_uint(bcx.ccx(), 0_u64), dbloc);
488-
let add_size = Mul(bcx,
489-
ZExt(bcx, nonzero, Type::i64(bcx.ccx())),
490-
Sub(bcx, align, lowbits, dbloc),
491-
dbloc);
492-
let size = Add(bcx, size, add_size, dbloc);
480+
// `(size + (align-1)) & !align`
481+
482+
let addend = Sub(bcx, align, C_uint(bcx.ccx(), 1_u64), dbloc);
483+
let size = And(
484+
bcx, Add(bcx, size, addend, dbloc), Neg(bcx, align, dbloc), dbloc);
493485

494486
(size, align)
495487
}

0 commit comments

Comments
 (0)