summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Chatelet <gchatelet@google.com>2019-10-16 13:06:17 +0000
committerGuillaume Chatelet <gchatelet@google.com>2019-10-16 13:06:17 +0000
commit2f6da767f13b8fd81f840c211d405fea32ac9db7 (patch)
tree4302b4faa68648b667b396b5efac7b3962638a6d
parent0caee2762086f6f3bb5657c1d7798df6b4789337 (diff)
downloadbcm5719-llvm-2f6da767f13b8fd81f840c211d405fea32ac9db7.tar.gz
bcm5719-llvm-2f6da767f13b8fd81f840c211d405fea32ac9db7.zip
[Alignment][NFC] Optimize alignTo
Summary: A small optimization suggested by jakehehrlich@ in D64790. Reviewers: jakehehrlich, courbet Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D69023 llvm-svn: 375000
-rw-r--r--llvm/include/llvm/Support/Alignment.h12
1 files changed, 11 insertions, 1 deletions
diff --git a/llvm/include/llvm/Support/Alignment.h b/llvm/include/llvm/Support/Alignment.h
index d86f4cce429..6925cb52d00 100644
--- a/llvm/include/llvm/Support/Alignment.h
+++ b/llvm/include/llvm/Support/Alignment.h
@@ -161,7 +161,17 @@ inline bool isAddrAligned(Align Lhs, const void *Addr) {
/// 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 - 1)` 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 - 1);
}
/// Returns a multiple of A needed to store `Size` bytes.
OpenPOWER on IntegriCloud