Skip to content

Commit

Permalink
[InstCombine] Handle GEP scalar/vector base mismatch (PR55363)
Browse files Browse the repository at this point in the history
30a12f3 switched the type check
to use the GEP result type rather than the GEP operand type.
However, the GEP result types may match even if the operand types
don't, in case GEPs with scalar/vector base and vector index
are compared.

Fixes #55363.
  • Loading branch information
nikic committed May 10, 2022
1 parent f14a1f2 commit d222bab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Expand Up @@ -889,7 +889,8 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
if (PtrBase != GEPRHS->getOperand(0)) {
bool IndicesTheSame =
GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
GEPLHS->getType() == GEPRHS->getType() &&
GEPLHS->getPointerOperand()->getType() ==
GEPRHS->getPointerOperand()->getType() &&
GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType();
if (IndicesTheSame)
for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/Transforms/InstCombine/opaque-ptr.ll
Expand Up @@ -334,6 +334,19 @@ define i1 @compare_geps_same_indices_different_types(ptr %a, ptr %b, i64 %idx) {
ret i1 %c
}

define <4 x i1> @compare_geps_same_indices_scalar_vector_base_mismatch(ptr %ptr, <4 x ptr> %ptrs) {
; CHECK-LABEL: @compare_geps_same_indices_scalar_vector_base_mismatch(
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i16, <4 x ptr> [[PTRS:%.*]], <4 x i64> <i64 1, i64 2, i64 3, i64 4>
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i16, ptr [[PTR:%.*]], <4 x i64> <i64 1, i64 2, i64 3, i64 4>
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <4 x ptr> [[GEP1]], [[GEP2]]
; CHECK-NEXT: ret <4 x i1> [[CMP]]
;
%gep1 = getelementptr i16, <4 x ptr> %ptrs, <4 x i64> <i64 1, i64 2, i64 3, i64 4>
%gep2 = getelementptr i16, ptr %ptr, <4 x i64> <i64 1, i64 2, i64 3, i64 4>
%cmp = icmp eq <4 x ptr> %gep1, %gep2
ret <4 x i1> %cmp
}

define ptr @indexed_compare(ptr %A, i64 %offset) {
; CHECK-LABEL: @indexed_compare(
; CHECK-NEXT: entry:
Expand Down

0 comments on commit d222bab

Please sign in to comment.