[PATCH] D69023: [Alignment][NFC] Optimize alignTo
Guillaume Chatelet via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 16 01:57:39 PDT 2019
gchatelet created this revision.
gchatelet added reviewers: jakehehrlich, courbet.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
A small optimization suggested by jakehehrlich@ in D64790 <https://reviews.llvm.org/D64790>.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D69023
Files:
llvm/include/llvm/Support/Alignment.h
Index: llvm/include/llvm/Support/Alignment.h
===================================================================
--- llvm/include/llvm/Support/Alignment.h
+++ llvm/include/llvm/Support/Alignment.h
@@ -161,7 +161,17 @@
/// Returns a multiple of A needed to store `Size` bytes.
inline uint64_t alignTo(uint64_t Size, Align A) {
- return (Size + A.value() - 1) / A.value() * A.value();
+ const uint64_t value = A.value();
+ // The following line is equivalent to `(Size + value - 1) / value * value`.
+
+ // The division followed by a multiplication can be thought of as a right
+ // shift followed by a left shift which zeros out the extra bits produced in
+ // the bump; `-value` is a mask where all those bits being zeroed out are just
+ // zero.
+
+ // Most compilers can generate this code but the pattern may be missed when
+ // multiple functions gets inlined.
+ return (Size + value - 1) & -value;
}
/// Returns a multiple of A needed to store `Size` bytes.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69023.225172.patch
Type: text/x-patch
Size: 981 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191016/9d26afaa/attachment.bin>
More information about the llvm-commits
mailing list