Skip to content

Commit 5b4071c

Browse files
authored
[clang][bytecode] Explicitly truncate in IntegralAP::from() (#112683)
Add Integral::toAPInt(), which truncates to the given BitWidth, similar to the toAPSInt() we already have.
1 parent 53d89ef commit 5b4071c

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

clang/lib/AST/ByteCode/Integral.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,14 @@ template <unsigned Bits, bool Signed> class Integral final {
122122
APSInt toAPSInt() const {
123123
return APSInt(APInt(Bits, static_cast<uint64_t>(V), Signed), !Signed);
124124
}
125-
APSInt toAPSInt(unsigned NumBits) const {
125+
APSInt toAPSInt(unsigned BitWidth) const { return APSInt(toAPInt(BitWidth)); }
126+
APInt toAPInt(unsigned BitWidth) const {
126127
if constexpr (Signed)
127-
return APSInt(toAPSInt().sextOrTrunc(NumBits), !Signed);
128+
return APInt(Bits, static_cast<uint64_t>(V), Signed)
129+
.sextOrTrunc(BitWidth);
128130
else
129-
return APSInt(toAPSInt().zextOrTrunc(NumBits), !Signed);
131+
return APInt(Bits, static_cast<uint64_t>(V), Signed)
132+
.zextOrTrunc(BitWidth);
130133
}
131134
APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); }
132135

clang/lib/AST/ByteCode/IntegralAP.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,7 @@ template <bool Signed> class IntegralAP final {
112112

113113
template <unsigned Bits, bool InputSigned>
114114
static IntegralAP from(Integral<Bits, InputSigned> I, unsigned BitWidth) {
115-
// TODO: Avoid implicit trunc?
116-
// See https://github.com/llvm/llvm-project/issues/112510.
117-
APInt Copy = APInt(BitWidth, static_cast<uint64_t>(I), InputSigned,
118-
/*implicitTrunc=*/true);
119-
120-
return IntegralAP<Signed>(Copy);
115+
return IntegralAP<Signed>(I.toAPInt(BitWidth));
121116
}
122117

123118
static IntegralAP zero(int32_t BitWidth) {

0 commit comments

Comments
 (0)