[clang] [llvm] [Support] Use CTLog2 from PointerLikeTypeTraits.h (NFC) (PR #157790)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 9 21:10:14 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/157790
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.
>From a87fcccf276f06675edb918220f1d6039d2cd137 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 9 Sep 2025 19:35:55 -0700
Subject: [PATCH] [Support] Use CTLog2 from PointerLikeTypeTraits.h (NFC)
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.
---
clang/include/clang/AST/Expr.h | 2 +-
llvm/include/llvm/Support/PointerLikeTypeTraits.h | 14 +++-----------
2 files changed, 4 insertions(+), 12 deletions(-)
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 &&
More information about the llvm-commits
mailing list