Skip to content

Commit eb0abc2

Browse files
[LLVM][IR] When evaluting GEP offsets don't assume ConstantInt is a scalar.
1 parent 2e60048 commit eb0abc2

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
881881
Type *IntIdxTy = DL.getIndexType(Ptr->getType());
882882

883883
for (unsigned i = 1, e = Ops.size(); i != e; ++i)
884-
if (!isa<ConstantInt>(Ops[i]))
884+
if (!isa<ConstantInt>(Ops[i]) || !Ops[i]->getType()->isIntegerTy())
885885
return nullptr;
886886

887887
unsigned BitWidth = DL.getTypeSizeInBits(IntIdxTy);

llvm/lib/IR/Operator.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ bool GEPOperator::accumulateConstantOffset(
127127
// Fast path for canonical getelementptr i8 form.
128128
if (SourceType->isIntegerTy(8) && !ExternalAnalysis) {
129129
if (auto *CI = dyn_cast<ConstantInt>(Index.front())) {
130-
Offset += CI->getValue().sextOrTrunc(Offset.getBitWidth());
131-
return true;
130+
if (CI->getType()->isIntegerTy()) {
131+
Offset += CI->getValue().sextOrTrunc(Offset.getBitWidth());
132+
return true;
133+
}
132134
}
133135
return false;
134136
}
@@ -163,28 +165,30 @@ bool GEPOperator::accumulateConstantOffset(
163165
Value *V = GTI.getOperand();
164166
StructType *STy = GTI.getStructTypeOrNull();
165167
// Handle ConstantInt if possible.
166-
if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
167-
if (ConstOffset->isZero())
168-
continue;
169-
// if the type is scalable and the constant is not zero (vscale * n * 0 =
170-
// 0) bailout.
171-
if (ScalableType)
172-
return false;
173-
// Handle a struct index, which adds its field offset to the pointer.
174-
if (STy) {
175-
unsigned ElementIdx = ConstOffset->getZExtValue();
176-
const StructLayout *SL = DL.getStructLayout(STy);
177-
// Element offset is in bytes.
178-
if (!AccumulateOffset(
179-
APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)),
180-
1))
168+
if (V->getType()->isIntegerTy()) {
169+
if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
170+
if (ConstOffset->isZero())
171+
continue;
172+
// if the type is scalable and the constant is not zero (vscale * n * 0
173+
// = 0) bailout.
174+
if (ScalableType)
175+
return false;
176+
// Handle a struct index, which adds its field offset to the pointer.
177+
if (STy) {
178+
unsigned ElementIdx = ConstOffset->getZExtValue();
179+
const StructLayout *SL = DL.getStructLayout(STy);
180+
// Element offset is in bytes.
181+
if (!AccumulateOffset(
182+
APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)),
183+
1))
184+
return false;
185+
continue;
186+
}
187+
if (!AccumulateOffset(ConstOffset->getValue(),
188+
GTI.getSequentialElementStride(DL)))
181189
return false;
182190
continue;
183191
}
184-
if (!AccumulateOffset(ConstOffset->getValue(),
185-
GTI.getSequentialElementStride(DL)))
186-
return false;
187-
continue;
188192
}
189193

190194
// The operand is not constant, check if an external analysis was provided.

llvm/test/Transforms/InstCombine/gep-vector-indices.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt -passes=instcombine %s -S | FileCheck %s
3+
; RUN: opt -passes=instcombine -use-constant-int-for-fixed-length-splat %s -S | FileCheck %s
34

45
define ptr @vector_splat_indices_v2i64_ext0(ptr %a) {
56
; CHECK-LABEL: @vector_splat_indices_v2i64_ext0(

llvm/test/Transforms/InstSimplify/gep.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt -S -passes=instsimplify < %s | FileCheck %s
3+
; RUN: opt -S -passes=instsimplify -use-constant-int-for-fixed-length-splat < %s | FileCheck %s
34

45
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
56

0 commit comments

Comments
 (0)