[llvm] b79007d - [IR] Fix accumulateConstantOffset() on zero-index GEP
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 4 08:18:08 PST 2024
Author: Nikita Popov
Date: 2024-12-04T17:17:58+01:00
New Revision: b79007d8a6fed51ec2e06aeaec31968122cfcd09
URL: https://github.com/llvm/llvm-project/commit/b79007d8a6fed51ec2e06aeaec31968122cfcd09
DIFF: https://github.com/llvm/llvm-project/commit/b79007d8a6fed51ec2e06aeaec31968122cfcd09.diff
LOG: [IR] Fix accumulateConstantOffset() on zero-index GEP
These are degenerate but not malformed, so make sure we don't
crash.
Added:
Modified:
llvm/lib/IR/Operator.cpp
llvm/unittests/IR/InstructionsTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/IR/Operator.cpp b/llvm/lib/IR/Operator.cpp
index 740ad4a8c0a3cd..39e5463cb6fc31 100644
--- a/llvm/lib/IR/Operator.cpp
+++ b/llvm/lib/IR/Operator.cpp
@@ -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());
diff --git a/llvm/unittests/IR/InstructionsTest.cpp b/llvm/unittests/IR/InstructionsTest.cpp
index b4dbc4ed435aad..a46f3ca5ab5ede 100644
--- a/llvm/unittests/IR/InstructionsTest.cpp
+++ b/llvm/unittests/IR/InstructionsTest.cpp
@@ -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;
More information about the llvm-commits
mailing list