From 8cbdf4b97536909c577ee0636bab5851b3a6dc42 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Mon, 20 Oct 2025 20:59:29 -0700 Subject: [PATCH] [ADT] Simplify AddInteger overloads in FoldingSetNodeID (NFC) This patch simplifies the AddInteger overloads by introducing AddIntegerImpl, a helper function to handle all cases, both 32-bit and 64-bit cases. --- llvm/include/llvm/ADT/FoldingSet.h | 32 +++++++++++++----------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/llvm/include/llvm/ADT/FoldingSet.h b/llvm/include/llvm/ADT/FoldingSet.h index 82a88c440ff24..675b5c6a35bbc 100644 --- a/llvm/include/llvm/ADT/FoldingSet.h +++ b/llvm/include/llvm/ADT/FoldingSet.h @@ -332,6 +332,14 @@ class FoldingSetNodeID { /// Use a SmallVector to avoid a heap allocation in the common case. SmallVector Bits; + template void AddIntegerImpl(T I) { + static_assert(std::is_integral_v && sizeof(T) <= sizeof(unsigned) * 2, + "T must be an integer type no wider than 64 bits"); + Bits.push_back(static_cast(I)); + if constexpr (sizeof(unsigned) < sizeof(T)) + Bits.push_back(static_cast(I) >> 32); + } + public: FoldingSetNodeID() = default; @@ -348,24 +356,12 @@ class FoldingSetNodeID { "unexpected pointer size"); AddInteger(reinterpret_cast(Ptr)); } - void AddInteger(signed I) { Bits.push_back(I); } - void AddInteger(unsigned I) { Bits.push_back(I); } - void AddInteger(long I) { AddInteger((unsigned long)I); } - void AddInteger(unsigned long I) { - if (sizeof(long) == sizeof(int)) - AddInteger(unsigned(I)); - else if (sizeof(long) == sizeof(long long)) { - AddInteger((unsigned long long)I); - } else { - llvm_unreachable("unexpected sizeof(long)"); - } - } - void AddInteger(long long I) { AddInteger((unsigned long long)I); } - void AddInteger(unsigned long long I) { - AddInteger(unsigned(I)); - AddInteger(unsigned(I >> 32)); - } - + void AddInteger(signed I) { AddIntegerImpl(I); } + void AddInteger(unsigned I) { AddIntegerImpl(I); } + void AddInteger(long I) { AddIntegerImpl(I); } + void AddInteger(unsigned long I) { AddIntegerImpl(I); } + void AddInteger(long long I) { AddIntegerImpl(I); } + void AddInteger(unsigned long long I) { AddIntegerImpl(I); } void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); } LLVM_ABI void AddString(StringRef String); LLVM_ABI void AddNodeID(const FoldingSetNodeID &ID);