File tree Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change 37
37
#include " llvm/IR/Value.h"
38
38
#include " llvm/Support/AtomicOrdering.h"
39
39
#include " llvm/Support/Casting.h"
40
+ #include " llvm/Support/CheckedArithmetic.h"
40
41
#include " llvm/Support/ErrorHandling.h"
41
42
#include " llvm/Support/MathExtras.h"
42
43
#include " llvm/Support/ModRef.h"
@@ -65,17 +66,25 @@ AllocaInst::getAllocationSize(const DataLayout &DL) const {
65
66
if (!C)
66
67
return std::nullopt;
67
68
assert (!Size .isScalable () && " Array elements cannot have a scalable size" );
68
- Size *= C->getZExtValue ();
69
+ auto CheckedProd =
70
+ checkedMulUnsigned (Size .getKnownMinValue (), C->getZExtValue ());
71
+ if (!CheckedProd)
72
+ return std::nullopt;
73
+ return TypeSize::getFixed (*CheckedProd);
69
74
}
70
75
return Size ;
71
76
}
72
77
73
78
std::optional<TypeSize>
74
79
AllocaInst::getAllocationSizeInBits (const DataLayout &DL) const {
75
80
std::optional<TypeSize> Size = getAllocationSize (DL);
76
- if (Size )
77
- return *Size * 8 ;
78
- return std::nullopt;
81
+ if (!Size )
82
+ return std::nullopt;
83
+ auto CheckedProd = checkedMulUnsigned (Size ->getKnownMinValue (),
84
+ static_cast <TypeSize::ScalarTy>(8 ));
85
+ if (!CheckedProd)
86
+ return std::nullopt;
87
+ return TypeSize::get (*CheckedProd, Size ->isScalable ());
79
88
}
80
89
81
90
// ===----------------------------------------------------------------------===//
Original file line number Diff line number Diff line change @@ -1750,6 +1750,7 @@ TEST(InstructionsTest, AllocaInst) {
1750
1750
%F = alloca [2 x half]
1751
1751
%G = alloca [2 x [3 x i128]]
1752
1752
%H = alloca %T
1753
+ %I = alloca i32, i64 9223372036854775807
1753
1754
ret void
1754
1755
}
1755
1756
)" );
@@ -1766,6 +1767,7 @@ TEST(InstructionsTest, AllocaInst) {
1766
1767
AllocaInst &F = cast<AllocaInst>(*It++);
1767
1768
AllocaInst &G = cast<AllocaInst>(*It++);
1768
1769
AllocaInst &H = cast<AllocaInst>(*It++);
1770
+ AllocaInst &I = cast<AllocaInst>(*It++);
1769
1771
EXPECT_EQ (A.getAllocationSizeInBits (DL), TypeSize::getFixed (32 ));
1770
1772
EXPECT_EQ (B.getAllocationSizeInBits (DL), TypeSize::getFixed (128 ));
1771
1773
EXPECT_FALSE (C.getAllocationSizeInBits (DL));
@@ -1774,6 +1776,7 @@ TEST(InstructionsTest, AllocaInst) {
1774
1776
EXPECT_EQ (F.getAllocationSizeInBits (DL), TypeSize::getFixed (32 ));
1775
1777
EXPECT_EQ (G.getAllocationSizeInBits (DL), TypeSize::getFixed (768 ));
1776
1778
EXPECT_EQ (H.getAllocationSizeInBits (DL), TypeSize::getFixed (160 ));
1779
+ EXPECT_FALSE (I.getAllocationSizeInBits (DL));
1777
1780
}
1778
1781
1779
1782
TEST (InstructionsTest, InsertAtBegin) {
You can’t perform that action at this time.
0 commit comments