Skip to content

Commit a2ae6f5

Browse files
committed
[InstCombine] Restore splat gep support in OptimizePointerDifference()
When looking for the common base pointer, support the case where the type changes because the GEP goes from pointer to vector of pointers. This was supported prior to llvm#142958.
1 parent d80ab81 commit a2ae6f5

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,8 +2088,6 @@ CommonPointerBase CommonPointerBase::compute(Value *LHS, Value *RHS) {
20882088
// Find common base and collect RHS GEPs.
20892089
while (true) {
20902090
if (Ptrs.contains(RHS)) {
2091-
if (LHS->getType() != RHS->getType())
2092-
return Base;
20932091
Base.Ptr = RHS;
20942092
break;
20952093
}
@@ -2132,12 +2130,15 @@ Value *InstCombinerImpl::OptimizePointerDifference(Value *LHS, Value *RHS,
21322130
// TODO: We should probably do this even if there is only one GEP.
21332131
bool RewriteGEPs = !Base.LHSGEPs.empty() && !Base.RHSGEPs.empty();
21342132

2135-
Type *IdxTy = DL.getIndexType(Base.Ptr->getType());
2133+
Type *IdxTy = DL.getIndexType(LHS->getType());
21362134
auto EmitOffsetFromBase = [&](ArrayRef<GEPOperator *> GEPs,
21372135
GEPNoWrapFlags NW) -> Value * {
21382136
Value *Sum = nullptr;
21392137
for (GEPOperator *GEP : reverse(GEPs)) {
21402138
Value *Offset = EmitGEPOffset(GEP, RewriteGEPs);
2139+
if (Offset->getType() != IdxTy)
2140+
Offset = Builder.CreateVectorSplat(
2141+
cast<VectorType>(IdxTy)->getElementCount(), Offset);
21412142
if (Sum)
21422143
Sum = Builder.CreateAdd(Sum, Offset, "", NW.hasNoUnsignedWrap(),
21432144
NW.isInBounds());

llvm/test/Transforms/InstCombine/sub-gep.ll

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -998,11 +998,7 @@ define i64 @multiple_geps_inbounds_nuw(ptr %base, i64 %idx, i64 %idx2) {
998998

999999
define <2 x i64> @splat_geps(ptr %base, <2 x i64> %idx1, <2 x i64> %idx2) {
10001000
; CHECK-LABEL: @splat_geps(
1001-
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i8, ptr [[BASE:%.*]], <2 x i64> [[IDX1:%.*]]
1002-
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds i8, ptr [[BASE]], <2 x i64> [[IDX2:%.*]]
1003-
; CHECK-NEXT: [[GEP1_INT:%.*]] = ptrtoint <2 x ptr> [[GEP1]] to <2 x i64>
1004-
; CHECK-NEXT: [[GEP2_INT:%.*]] = ptrtoint <2 x ptr> [[GEP2]] to <2 x i64>
1005-
; CHECK-NEXT: [[D:%.*]] = sub <2 x i64> [[GEP2_INT]], [[GEP1_INT]]
1001+
; CHECK-NEXT: [[D:%.*]] = sub nsw <2 x i64> [[IDX2:%.*]], [[IDX1:%.*]]
10061002
; CHECK-NEXT: ret <2 x i64> [[D]]
10071003
;
10081004
%gep1 = getelementptr inbounds i8, ptr %base, <2 x i64> %idx1
@@ -1015,12 +1011,10 @@ define <2 x i64> @splat_geps(ptr %base, <2 x i64> %idx1, <2 x i64> %idx2) {
10151011

10161012
define <2 x i64> @splat_geps_multiple(ptr %base, i64 %idx0, <2 x i64> %idx1, <2 x i64> %idx2) {
10171013
; CHECK-LABEL: @splat_geps_multiple(
1018-
; CHECK-NEXT: [[GEP0:%.*]] = getelementptr inbounds i8, ptr [[BASE:%.*]], i64 [[IDX0:%.*]]
1019-
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i8, ptr [[GEP0]], <2 x i64> [[IDX1:%.*]]
1020-
; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds i8, ptr [[BASE]], <2 x i64> [[IDX2:%.*]]
1021-
; CHECK-NEXT: [[GEP1_INT:%.*]] = ptrtoint <2 x ptr> [[GEP1]] to <2 x i64>
1022-
; CHECK-NEXT: [[GEP2_INT:%.*]] = ptrtoint <2 x ptr> [[GEP2]] to <2 x i64>
1023-
; CHECK-NEXT: [[D:%.*]] = sub <2 x i64> [[GEP2_INT]], [[GEP1_INT]]
1014+
; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <2 x i64> poison, i64 [[IDX0:%.*]], i64 0
1015+
; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <2 x i64> [[DOTSPLATINSERT]], <2 x i64> poison, <2 x i32> zeroinitializer
1016+
; CHECK-NEXT: [[TMP1:%.*]] = add nsw <2 x i64> [[DOTSPLAT]], [[IDX1:%.*]]
1017+
; CHECK-NEXT: [[D:%.*]] = sub nsw <2 x i64> [[IDX2:%.*]], [[TMP1]]
10241018
; CHECK-NEXT: ret <2 x i64> [[D]]
10251019
;
10261020
%gep0 = getelementptr inbounds i8, ptr %base, i64 %idx0

0 commit comments

Comments
 (0)