]> git.lizzy.rs Git - rust.git/commitdiff
Improve code emitted for inserting padding before unsized field.
authorFelix S. Klock II <pnkfelix@pnkfx.org>
Wed, 29 Jul 2015 20:18:39 +0000 (22:18 +0200)
committerFelix S. Klock II <pnkfelix@pnkfx.org>
Wed, 29 Jul 2015 20:18:39 +0000 (22:18 +0200)
Hat-tip to eddyb for the appropriate bit-trickery here.

src/librustc_trans/trans/glue.rs

index 91b20f0b9ded0a2dbe1a9ea14b39ca61e86d4ea8..18fedda49193cfcabbf8bd28384dc360c7c59128 100644 (file)
@@ -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)
         }