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"
@@ -60,22 +61,33 @@ static cl::opt<bool> DisableI2pP2iOpt(
60
61
std::optional<TypeSize>
61
62
AllocaInst::getAllocationSize (const DataLayout &DL) const {
62
63
TypeSize Size = DL.getTypeAllocSize (getAllocatedType ());
63
- if (isArrayAllocation ()) {
64
- auto *C = dyn_cast<ConstantInt>(getArraySize ());
65
- if (!C)
66
- return std::nullopt;
67
- assert (!Size.isScalable () && " Array elements cannot have a scalable size" );
68
- Size *= C->getZExtValue ();
64
+ if (!isArrayAllocation ()) {
65
+ return Size;
69
66
}
70
- return Size;
67
+ auto *C = dyn_cast<ConstantInt>(getArraySize ());
68
+ if (!C)
69
+ return std::nullopt;
70
+ assert (!Size.isScalable () && " Array elements cannot have a scalable size" );
71
+ auto checkedProd =
72
+ checkedMulUnsigned (Size.getKnownMinValue (), C->getZExtValue ());
73
+ if (!checkedProd) {
74
+ return std::nullopt;
75
+ }
76
+ return TypeSize::getFixed (*checkedProd);
71
77
}
72
78
73
79
std::optional<TypeSize>
74
80
AllocaInst::getAllocationSizeInBits (const DataLayout &DL) const {
75
81
std::optional<TypeSize> Size = getAllocationSize (DL);
76
- if (Size)
77
- return *Size * 8 ;
78
- return std::nullopt;
82
+ if (!Size) {
83
+ return std::nullopt;
84
+ }
85
+ auto CheckedProd = checkedMulUnsigned ((*Size).getKnownMinValue (),
86
+ static_cast <TypeSize::ScalarTy>(8 ));
87
+ if (!CheckedProd) {
88
+ return std::nullopt;
89
+ }
90
+ return TypeSize::get (*CheckedProd, (*Size).isScalable ());
79
91
}
80
92
81
93
// ===----------------------------------------------------------------------===//
0 commit comments