Skip to content

[InstCombine] Restore splat gep support in OptimizePointerDifference() #143906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2088,8 +2088,6 @@ CommonPointerBase CommonPointerBase::compute(Value *LHS, Value *RHS) {
// Find common base and collect RHS GEPs.
while (true) {
if (Ptrs.contains(RHS)) {
if (LHS->getType() != RHS->getType())
return Base;
Base.Ptr = RHS;
break;
}
Expand Down Expand Up @@ -2132,12 +2130,15 @@ Value *InstCombinerImpl::OptimizePointerDifference(Value *LHS, Value *RHS,
// TODO: We should probably do this even if there is only one GEP.
bool RewriteGEPs = !Base.LHSGEPs.empty() && !Base.RHSGEPs.empty();

Type *IdxTy = DL.getIndexType(Base.Ptr->getType());
Type *IdxTy = DL.getIndexType(LHS->getType());
auto EmitOffsetFromBase = [&](ArrayRef<GEPOperator *> GEPs,
GEPNoWrapFlags NW) -> Value * {
Value *Sum = nullptr;
for (GEPOperator *GEP : reverse(GEPs)) {
Value *Offset = EmitGEPOffset(GEP, RewriteGEPs);
if (Offset->getType() != IdxTy)
Offset = Builder.CreateVectorSplat(
cast<VectorType>(IdxTy)->getElementCount(), Offset);
if (Sum)
Sum = Builder.CreateAdd(Sum, Offset, "", NW.hasNoUnsignedWrap(),
NW.isInBounds());
Expand Down
30 changes: 30 additions & 0 deletions llvm/test/Transforms/InstCombine/sub-gep.ll
Original file line number Diff line number Diff line change
Expand Up @@ -995,3 +995,33 @@ define i64 @multiple_geps_inbounds_nuw(ptr %base, i64 %idx, i64 %idx2) {
%d = sub i64 %i2, %i1
ret i64 %d
}

define <2 x i64> @splat_geps(ptr %base, <2 x i64> %idx1, <2 x i64> %idx2) {
; CHECK-LABEL: @splat_geps(
; CHECK-NEXT: [[D:%.*]] = sub nsw <2 x i64> [[IDX2:%.*]], [[IDX1:%.*]]
; CHECK-NEXT: ret <2 x i64> [[D]]
;
%gep1 = getelementptr inbounds i8, ptr %base, <2 x i64> %idx1
%gep2 = getelementptr inbounds i8, ptr %base, <2 x i64> %idx2
%gep1.int = ptrtoint <2 x ptr> %gep1 to <2 x i64>
%gep2.int = ptrtoint <2 x ptr> %gep2 to <2 x i64>
%d = sub <2 x i64> %gep2.int, %gep1.int
ret <2 x i64> %d
}

define <2 x i64> @splat_geps_multiple(ptr %base, i64 %idx0, <2 x i64> %idx1, <2 x i64> %idx2) {
; CHECK-LABEL: @splat_geps_multiple(
; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <2 x i64> poison, i64 [[IDX0:%.*]], i64 0
; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <2 x i64> [[DOTSPLATINSERT]], <2 x i64> poison, <2 x i32> zeroinitializer
; CHECK-NEXT: [[TMP1:%.*]] = add nsw <2 x i64> [[DOTSPLAT]], [[IDX1:%.*]]
; CHECK-NEXT: [[D:%.*]] = sub nsw <2 x i64> [[IDX2:%.*]], [[TMP1]]
; CHECK-NEXT: ret <2 x i64> [[D]]
;
%gep0 = getelementptr inbounds i8, ptr %base, i64 %idx0
%gep1 = getelementptr inbounds i8, ptr %gep0, <2 x i64> %idx1
%gep2 = getelementptr inbounds i8, ptr %base, <2 x i64> %idx2
%gep1.int = ptrtoint <2 x ptr> %gep1 to <2 x i64>
%gep2.int = ptrtoint <2 x ptr> %gep2 to <2 x i64>
%d = sub <2 x i64> %gep2.int, %gep1.int
ret <2 x i64> %d
}
Loading