[llvm] r375000 - [Alignment][NFC] Optimize alignTo
Guillaume Chatelet via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 16 06:06:17 PDT 2019
Author: gchatelet
Date: Wed Oct 16 06:06:17 2019
New Revision: 375000
URL: http://llvm.org/viewvc/llvm-project?rev=375000&view=rev
Log:
[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
Modified:
llvm/trunk/include/llvm/Support/Alignment.h
Modified: llvm/trunk/include/llvm/Support/Alignment.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Alignment.h?rev=375000&r1=374999&r2=375000&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Alignment.h (original)
+++ llvm/trunk/include/llvm/Support/Alignment.h Wed Oct 16 06:06:17 2019
@@ -161,7 +161,17 @@ inline bool isAddrAligned(Align Lhs, con
/// 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.
More information about the llvm-commits
mailing list