Skip to content

Commit 5bbb604

Browse files
committedAug 27, 2019
[InstCombine] Disable some portions of foldGEPICmp for GEPs that return a vector of pointers. Fix other portions.
llvm-svn: 370114
1 parent c894c6c commit 5bbb604

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed
 

‎llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

+26-11
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,10 @@ getAsConstantIndexedAddress(Value *V, const DataLayout &DL) {
832832
static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS,
833833
ICmpInst::Predicate Cond,
834834
const DataLayout &DL) {
835+
// FIXME: Support vector of pointers.
836+
if (GEPLHS->getType()->isVectorTy())
837+
return nullptr;
838+
835839
if (!GEPLHS->hasAllConstantIndices())
836840
return nullptr;
837841

@@ -882,7 +886,9 @@ Instruction *InstCombiner::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
882886
RHS = RHS->stripPointerCasts();
883887

884888
Value *PtrBase = GEPLHS->getOperand(0);
885-
if (PtrBase == RHS && GEPLHS->isInBounds()) {
889+
// FIXME: Support vector pointer GEPs.
890+
if (PtrBase == RHS && GEPLHS->isInBounds() &&
891+
!GEPLHS->getType()->isVectorTy()) {
886892
// ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
887893
// This transformation (ignoring the base and scales) is valid because we
888894
// know pointers can't overflow since the gep is inbounds. See if we can
@@ -894,10 +900,12 @@ Instruction *InstCombiner::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
894900
Offset = EmitGEPOffset(GEPLHS);
895901
return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
896902
Constant::getNullValue(Offset->getType()));
897-
} else if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) &&
898-
isa<Constant>(RHS) && cast<Constant>(RHS)->isNullValue() &&
899-
!NullPointerIsDefined(I.getFunction(),
900-
RHS->getType()->getPointerAddressSpace())) {
903+
}
904+
905+
if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) &&
906+
isa<Constant>(RHS) && cast<Constant>(RHS)->isNullValue() &&
907+
!NullPointerIsDefined(I.getFunction(),
908+
RHS->getType()->getPointerAddressSpace())) {
901909
// For most address spaces, an allocation can't be placed at null, but null
902910
// itself is treated as a 0 size allocation in the in bounds rules. Thus,
903911
// the only valid inbounds address derived from null, is null itself.
@@ -945,11 +953,13 @@ Instruction *InstCombiner::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
945953
// If we're comparing GEPs with two base pointers that only differ in type
946954
// and both GEPs have only constant indices or just one use, then fold
947955
// the compare with the adjusted indices.
956+
// FIXME: Support vector of pointers.
948957
if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
949958
(GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
950959
(GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
951960
PtrBase->stripPointerCasts() ==
952-
GEPRHS->getOperand(0)->stripPointerCasts()) {
961+
GEPRHS->getOperand(0)->stripPointerCasts() &&
962+
!GEPLHS->getType()->isVectorTy()) {
953963
Value *LOffset = EmitGEPOffset(GEPLHS);
954964
Value *ROffset = EmitGEPOffset(GEPRHS);
955965

@@ -993,15 +1003,20 @@ Instruction *InstCombiner::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
9931003
unsigned DiffOperand = 0; // The operand that differs.
9941004
for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
9951005
if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
996-
if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
997-
GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
1006+
Type *LHSType = GEPLHS->getOperand(i)->getType();
1007+
Type *RHSType = GEPRHS->getOperand(i)->getType();
1008+
// FIXME: Better support for vector of pointers.
1009+
if (LHSType->getPrimitiveSizeInBits() !=
1010+
RHSType->getPrimitiveSizeInBits() ||
1011+
(GEPLHS->getType()->isVectorTy() &&
1012+
(!LHSType->isVectorTy() || !RHSType->isVectorTy()))) {
9981013
// Irreconcilable differences.
9991014
NumDifferences = 2;
10001015
break;
1001-
} else {
1002-
if (NumDifferences++) break;
1003-
DiffOperand = i;
10041016
}
1017+
1018+
if (NumDifferences++) break;
1019+
DiffOperand = i;
10051020
}
10061021

10071022
if (NumDifferences == 0) // SAME GEP?

‎llvm/test/Transforms/InstCombine/getelementptr.ll

+15
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,21 @@ define <2 x i1> @test13_vector2(i64 %X, <2 x %S*> %P) nounwind {
202202
ret <2 x i1> %C
203203
}
204204

205+
; This is a test of icmp + shl nuw in disguise - 4611... is 0x3fff...
206+
define <2 x i1> @test13_vector3(i64 %X, <2 x %S*> %P) nounwind {
207+
; CHECK-LABEL: @test13_vector3(
208+
; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <2 x i64> undef, i64 [[X:%.*]], i32 0
209+
; CHECK-NEXT: [[TMP1:%.*]] = shl nuw <2 x i64> [[DOTSPLATINSERT]], <i64 2, i64 undef>
210+
; CHECK-NEXT: [[A_IDX:%.*]] = shufflevector <2 x i64> [[TMP1]], <2 x i64> undef, <2 x i32> zeroinitializer
211+
; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i64> [[A_IDX]], <i64 4, i64 4>
212+
; CHECK-NEXT: ret <2 x i1> [[C]]
213+
;
214+
%A = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> zeroinitializer, <2 x i32> <i32 1, i32 1>, i64 %X
215+
%B = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> <i64 0, i64 0>, <2 x i32> <i32 1, i32 1>, i64 1
216+
%C = icmp eq <2 x i32*> %A, %B
217+
ret <2 x i1> %C
218+
}
219+
205220
define i1 @test13_as1(i16 %X, %S addrspace(1)* %P) {
206221
; CHECK-LABEL: @test13_as1(
207222
; CHECK-NEXT: %C = icmp eq i16 %X, -1

0 commit comments

Comments
 (0)
Please sign in to comment.