Skip to content

Commit

Permalink
Improve code emitted for inserting padding before unsized field.
Browse files Browse the repository at this point in the history
Hat-tip to eddyb for the appropriate bit-trickery here.
  • Loading branch information
pnkfelix committed Jul 29, 2015
1 parent 26f4ebe commit 21be094
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/librustc_trans/trans/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,21 +475,13 @@ pub fn size_and_align_of_dst<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t: Ty<'tcx>, in
//
// `size + ((size & (align-1)) ? align : 0)`
//
// Currently I am emulating the above via:
// emulated via the semi-standard fast bit trick:
//
// `size + ((size & (align-1)) * align-(size & (align-1)))`
//
// because I am not sure which is cheaper between a branch
// or a multiply.

let mask = Sub(bcx, align, C_uint(bcx.ccx(), 1_u64), dbloc);
let lowbits = And(bcx, size, mask, DebugLoc::None);
let nonzero = ICmp(bcx, llvm::IntNE, lowbits, C_uint(bcx.ccx(), 0_u64), dbloc);
let add_size = Mul(bcx,
ZExt(bcx, nonzero, Type::i64(bcx.ccx())),
Sub(bcx, align, lowbits, dbloc),
dbloc);
let size = Add(bcx, size, add_size, dbloc);
// `(size + (align-1)) & !align`

let addend = Sub(bcx, align, C_uint(bcx.ccx(), 1_u64), dbloc);
let size = And(
bcx, Add(bcx, size, addend, dbloc), Neg(bcx, align, dbloc), dbloc);

(size, align)
}
Expand Down

0 comments on commit 21be094

Please sign in to comment.