Skip to content

Commit a88653a

Browse files
[LLVM][IR] When evaluating GEP offsets don't assume ConstantInt is a scalar. (#117162)
1 parent 140df02 commit a88653a

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ bool GEPOperator::accumulateConstantOffset(
126126
APInt &Offset, function_ref<bool(Value &, APInt &)> ExternalAnalysis) {
127127
// Fast path for canonical getelementptr i8 form.
128128
if (SourceType->isIntegerTy(8) && !ExternalAnalysis) {
129-
if (auto *CI = dyn_cast<ConstantInt>(Index.front())) {
129+
auto *CI = dyn_cast<ConstantInt>(Index.front());
130+
if (CI && CI->getType()->isIntegerTy()) {
130131
Offset += CI->getValue().sextOrTrunc(Offset.getBitWidth());
131132
return true;
132133
}
@@ -165,7 +166,8 @@ bool GEPOperator::accumulateConstantOffset(
165166
Value *V = GTI.getOperand();
166167
StructType *STy = GTI.getStructTypeOrNull();
167168
// Handle ConstantInt if possible.
168-
if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
169+
auto *ConstOffset = dyn_cast<ConstantInt>(V);
170+
if (ConstOffset && ConstOffset->getType()->isIntegerTy()) {
169171
if (ConstOffset->isZero())
170172
continue;
171173
// if the type is scalable and the constant is not zero (vscale * n * 0 =
@@ -226,7 +228,8 @@ bool GEPOperator::collectOffset(
226228
Value *V = GTI.getOperand();
227229
StructType *STy = GTI.getStructTypeOrNull();
228230
// Handle ConstantInt if possible.
229-
if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
231+
auto *ConstOffset = dyn_cast<ConstantInt>(V);
232+
if (ConstOffset && ConstOffset->getType()->isIntegerTy()) {
230233
if (ConstOffset->isZero())
231234
continue;
232235
// If the type is scalable and the constant is not zero (vscale * n * 0 =

llvm/test/Transforms/GVN/opaque-ptr.ll

Lines changed: 9 additions & 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=gvn < %s | FileCheck %s
3+
; RUN: opt -S -passes=gvn -use-constant-int-for-fixed-length-splat < %s | FileCheck %s
34

45
declare void @use(ptr)
56
declare void @use.i32(i32)
@@ -58,6 +59,10 @@ define void @gep_cse_offset_canonicalization(ptr %p, i64 %idx, i64 %idx2) {
5859
; CHECK-NEXT: call void @use(ptr [[GEP6]])
5960
; CHECK-NEXT: call void @use(ptr [[GEP6_SAME]])
6061
; CHECK-NEXT: call void @use(ptr [[GEP6_DIFFERENT]])
62+
; CHECK-NEXT: %gep7 = getelementptr <16 x i32>, ptr %p, i64 1
63+
; CHECK-NEXT: %gep7.different = getelementptr <16 x i32>, ptr %p, <16 x i64> splat (i64 1)
64+
; CHECK-NEXT: call void @use(ptr %gep7)
65+
; CHECK-NEXT: call void @use(<16 x ptr> %gep7.different)
6166
; CHECK-NEXT: ret void
6267
;
6368
%gep1 = getelementptr i64, ptr %p, i64 1
@@ -101,6 +106,10 @@ define void @gep_cse_offset_canonicalization(ptr %p, i64 %idx, i64 %idx2) {
101106
call void @use(ptr %gep6)
102107
call void @use(ptr %gep6.same)
103108
call void @use(ptr %gep6.different)
109+
%gep7 = getelementptr <16 x i32>, ptr %p, i64 1
110+
%gep7.different = getelementptr <16 x i32>, ptr %p, <16 x i64> splat (i64 1)
111+
call void @use(ptr %gep7)
112+
call void @use(<16 x ptr> %gep7.different)
104113
ret void
105114
}
106115

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)