Skip to content

Commit b79007d

Browse files
committed
[IR] Fix accumulateConstantOffset() on zero-index GEP
These are degenerate but not malformed, so make sure we don't crash.
1 parent ba43a10 commit b79007d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

llvm/lib/IR/Operator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ bool GEPOperator::accumulateConstantOffset(
125125
Type *SourceType, ArrayRef<const Value *> Index, const DataLayout &DL,
126126
APInt &Offset, function_ref<bool(Value &, APInt &)> ExternalAnalysis) {
127127
// Fast path for canonical getelementptr i8 form.
128-
if (SourceType->isIntegerTy(8) && !ExternalAnalysis) {
128+
if (SourceType->isIntegerTy(8) && !Index.empty() && !ExternalAnalysis) {
129129
auto *CI = dyn_cast<ConstantInt>(Index.front());
130130
if (CI && CI->getType()->isIntegerTy()) {
131131
Offset += CI->getValue().sextOrTrunc(Offset.getBitWidth());

llvm/unittests/IR/InstructionsTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,20 @@ TEST(InstructionsTest, GEPIndices) {
898898
delete GEPI;
899899
}
900900

901+
TEST(InstructionsTest, ZeroIndexGEP) {
902+
LLVMContext Context;
903+
DataLayout DL;
904+
Type *PtrTy = PointerType::getUnqual(Context);
905+
auto *GEP = GetElementPtrInst::Create(Type::getInt8Ty(Context),
906+
PoisonValue::get(PtrTy), {});
907+
908+
APInt Offset(DL.getIndexTypeSizeInBits(PtrTy), 0);
909+
EXPECT_TRUE(GEP->accumulateConstantOffset(DL, Offset));
910+
EXPECT_TRUE(Offset.isZero());
911+
912+
delete GEP;
913+
}
914+
901915
TEST(InstructionsTest, SwitchInst) {
902916
LLVMContext C;
903917

0 commit comments

Comments
 (0)