[IR] Fix accumulateConstantOffset() on zero-index GEP

These are degenerate but not malformed, so make sure we don't
crash.
This commit is contained in:
Nikita Popov 2024-12-04 17:14:48 +01:00
parent ba43a102a9
commit b79007d8a6
2 changed files with 15 additions and 1 deletions

View File

@ -125,7 +125,7 @@ bool GEPOperator::accumulateConstantOffset(
Type *SourceType, ArrayRef<const Value *> Index, const DataLayout &DL,
APInt &Offset, function_ref<bool(Value &, APInt &)> ExternalAnalysis) {
// Fast path for canonical getelementptr i8 form.
if (SourceType->isIntegerTy(8) && !ExternalAnalysis) {
if (SourceType->isIntegerTy(8) && !Index.empty() && !ExternalAnalysis) {
auto *CI = dyn_cast<ConstantInt>(Index.front());
if (CI && CI->getType()->isIntegerTy()) {
Offset += CI->getValue().sextOrTrunc(Offset.getBitWidth());

View File

@ -898,6 +898,20 @@ TEST(InstructionsTest, GEPIndices) {
delete GEPI;
}
TEST(InstructionsTest, ZeroIndexGEP) {
LLVMContext Context;
DataLayout DL;
Type *PtrTy = PointerType::getUnqual(Context);
auto *GEP = GetElementPtrInst::Create(Type::getInt8Ty(Context),
PoisonValue::get(PtrTy), {});
APInt Offset(DL.getIndexTypeSizeInBits(PtrTy), 0);
EXPECT_TRUE(GEP->accumulateConstantOffset(DL, Offset));
EXPECT_TRUE(Offset.isZero());
delete GEP;
}
TEST(InstructionsTest, SwitchInst) {
LLVMContext C;