-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[Support] Use CTLog2 from PointerLikeTypeTraits.h (NFC) #157790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Support] Use CTLog2 from PointerLikeTypeTraits.h (NFC) #157790
Conversation
This patch replaces ConstantLog2 with CTLog2. ConstantLog2 only operates on alignment values, making the two interchangeable in this context. CTLog2 also has the benefit of a static_assert that ensures its parameter is a power of two.
|
@llvm/pr-subscribers-llvm-support @llvm/pr-subscribers-clang Author: Kazu Hirata (kazutakahirata) ChangesThis patch replaces ConstantLog2 with CTLog2. ConstantLog2 only Full diff: https://github.com/llvm/llvm-project/pull/157790.diff 2 Files Affected:
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 23a0996f02725..d2b872458ae82 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -1038,7 +1038,7 @@ class Expr : public ValueStmt {
// PointerLikeTypeTraits is specialized so it can be used with a forward-decl of
// Expr. Verify that we got it right.
static_assert(llvm::PointerLikeTypeTraits<Expr *>::NumLowBitsAvailable <=
- llvm::detail::ConstantLog2<alignof(Expr)>::value,
+ llvm::CTLog2<alignof(Expr)>(),
"PointerLikeTypeTraits<Expr*> assumes too much alignment.");
using ConstantExprKind = Expr::ConstantExprKind;
diff --git a/llvm/include/llvm/Support/PointerLikeTypeTraits.h b/llvm/include/llvm/Support/PointerLikeTypeTraits.h
index 1b15f930bd87d..0ac919fd9c0b9 100644
--- a/llvm/include/llvm/Support/PointerLikeTypeTraits.h
+++ b/llvm/include/llvm/Support/PointerLikeTypeTraits.h
@@ -15,8 +15,8 @@
#define LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
#include "llvm/Support/DataTypes.h"
+#include "llvm/Support/MathExtras.h"
#include <cassert>
-#include <type_traits>
namespace llvm {
@@ -25,12 +25,6 @@ namespace llvm {
template <typename T> struct PointerLikeTypeTraits;
namespace detail {
-/// A tiny meta function to compute the log2 of a compile time constant.
-template <size_t N>
-struct ConstantLog2
- : std::integral_constant<size_t, ConstantLog2<N / 2>::value + 1> {};
-template <> struct ConstantLog2<1> : std::integral_constant<size_t, 0> {};
-
// Provide a trait to check if T is pointer-like.
template <typename T, typename U = void> struct HasPointerLikeTypeTraits {
static const bool value = false;
@@ -57,8 +51,7 @@ template <typename T> struct PointerLikeTypeTraits<T *> {
static inline void *getAsVoidPointer(T *P) { return P; }
static inline T *getFromVoidPointer(void *P) { return static_cast<T *>(P); }
- static constexpr int NumLowBitsAvailable =
- detail::ConstantLog2<alignof(T)>::value;
+ static constexpr int NumLowBitsAvailable = CTLog2<alignof(T)>();
};
template <> struct PointerLikeTypeTraits<void *> {
@@ -123,8 +116,7 @@ template <> struct PointerLikeTypeTraits<uintptr_t> {
/// potentially use alignment attributes on functions to satisfy that.
template <int Alignment, typename FunctionPointerT>
struct FunctionPointerLikeTypeTraits {
- static constexpr int NumLowBitsAvailable =
- detail::ConstantLog2<Alignment>::value;
+ static constexpr int NumLowBitsAvailable = CTLog2<Alignment>();
static inline void *getAsVoidPointer(FunctionPointerT P) {
assert((reinterpret_cast<uintptr_t>(P) &
~((uintptr_t)-1 << NumLowBitsAvailable)) == 0 &&
|
arsenm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConstantLog2 name is better though
@arsenm Yeah, |
This patch replaces ConstantLog2 with CTLog2. ConstantLog2 only
operates on alignment values, making the two interchangeable in this
context. CTLog2 also has the benefit of a static_assert that ensures
its parameter is a power of two.