Skip to content

Commit 7dd06b3

Browse files
[ADT] Simplify AddInteger overloads in FoldingSetNodeID (NFC) (#164753)
This patch simplifies the AddInteger overloads by introducing AddIntegerImpl, a helper function to handle all cases, both 32-bit and 64-bit cases.
1 parent df970d5 commit 7dd06b3

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

llvm/include/llvm/ADT/FoldingSet.h

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ class FoldingSetNodeID {
332332
/// Use a SmallVector to avoid a heap allocation in the common case.
333333
SmallVector<unsigned, 32> Bits;
334334

335+
template <typename T> void AddIntegerImpl(T I) {
336+
static_assert(std::is_integral_v<T> && sizeof(T) <= sizeof(unsigned) * 2,
337+
"T must be an integer type no wider than 64 bits");
338+
Bits.push_back(static_cast<unsigned>(I));
339+
if constexpr (sizeof(unsigned) < sizeof(T))
340+
Bits.push_back(static_cast<unsigned long long>(I) >> 32);
341+
}
342+
335343
public:
336344
FoldingSetNodeID() = default;
337345

@@ -348,24 +356,12 @@ class FoldingSetNodeID {
348356
"unexpected pointer size");
349357
AddInteger(reinterpret_cast<uintptr_t>(Ptr));
350358
}
351-
void AddInteger(signed I) { Bits.push_back(I); }
352-
void AddInteger(unsigned I) { Bits.push_back(I); }
353-
void AddInteger(long I) { AddInteger((unsigned long)I); }
354-
void AddInteger(unsigned long I) {
355-
if (sizeof(long) == sizeof(int))
356-
AddInteger(unsigned(I));
357-
else if (sizeof(long) == sizeof(long long)) {
358-
AddInteger((unsigned long long)I);
359-
} else {
360-
llvm_unreachable("unexpected sizeof(long)");
361-
}
362-
}
363-
void AddInteger(long long I) { AddInteger((unsigned long long)I); }
364-
void AddInteger(unsigned long long I) {
365-
AddInteger(unsigned(I));
366-
AddInteger(unsigned(I >> 32));
367-
}
368-
359+
void AddInteger(signed I) { AddIntegerImpl(I); }
360+
void AddInteger(unsigned I) { AddIntegerImpl(I); }
361+
void AddInteger(long I) { AddIntegerImpl(I); }
362+
void AddInteger(unsigned long I) { AddIntegerImpl(I); }
363+
void AddInteger(long long I) { AddIntegerImpl(I); }
364+
void AddInteger(unsigned long long I) { AddIntegerImpl(I); }
369365
void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); }
370366
LLVM_ABI void AddString(StringRef String);
371367
LLVM_ABI void AddNodeID(const FoldingSetNodeID &ID);

0 commit comments

Comments
 (0)