[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:
parent
ba43a102a9
commit
b79007d8a6
@ -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());
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user