Skip to content

DynamicAPInt: Support APInt constructor. #146301

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

Merged
merged 1 commit into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions llvm/include/llvm/ADT/DynamicAPInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef LLVM_ADT_DYNAMICAPINT_H
#define LLVM_ADT_DYNAMICAPINT_H

#include "llvm/ADT/APInt.h"
#include "llvm/ADT/SlowDynamicAPInt.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
Expand Down Expand Up @@ -116,6 +117,14 @@ class DynamicAPInt {
: ValSmall(Val) {
ValLarge.Val.BitWidth = 0;
}
LLVM_ATTRIBUTE_ALWAYS_INLINE explicit DynamicAPInt(const APInt &Val) {
if (Val.getBitWidth() <= 64) {
ValSmall = Val.getSExtValue();
ValLarge.Val.BitWidth = 0;
} else {
new (&ValLarge) detail::SlowDynamicAPInt(Val);
}
}
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt() : DynamicAPInt(0) {}
LLVM_ATTRIBUTE_ALWAYS_INLINE ~DynamicAPInt() {
if (LLVM_UNLIKELY(isLarge()))
Expand Down
10 changes: 10 additions & 0 deletions llvm/unittests/ADT/DynamicAPIntTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ class TypeNames {
};
TYPED_TEST_SUITE(IntTest, TypeList, TypeNames);

TYPED_TEST(IntTest, ValueInit) {
APInt Large(65, 0, true);
Large.setBit(64);
TypeParam DynLarge(1ll << 63);
EXPECT_EQ(TypeParam(Large), DynLarge + DynLarge);
APInt Small(64, -1, true);
TypeParam DynSmall(Small.getSExtValue());
EXPECT_EQ(TypeParam(Small), DynSmall);
}

TYPED_TEST(IntTest, ops) {
TypeParam Two(2), Five(5), Seven(7), Ten(10);
EXPECT_EQ(Five + Five, Ten);
Expand Down