Skip to content

[InstCombine] Preserve flags for difference of gep chains #143488

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 1 commit into from
Jun 10, 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
10 changes: 6 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2146,12 +2146,14 @@ Value *InstCombinerImpl::OptimizePointerDifference(Value *LHS, Value *RHS,
bool RewriteGEPs = !Base.LHSGEPs.empty() && !Base.RHSGEPs.empty();

Type *IdxTy = DL.getIndexType(Base.Ptr->getType());
auto EmitOffsetFromBase = [&](ArrayRef<GEPOperator *> GEPs) -> Value * {
auto EmitOffsetFromBase = [&](ArrayRef<GEPOperator *> GEPs,
GEPNoWrapFlags NW) -> Value * {
Value *Sum = nullptr;
for (GEPOperator *GEP : reverse(GEPs)) {
Value *Offset = EmitGEPOffset(GEP, RewriteGEPs);
if (Sum)
Sum = Builder.CreateAdd(Sum, Offset);
Sum = Builder.CreateAdd(Sum, Offset, "", NW.hasNoUnsignedWrap(),
NW.isInBounds());
else
Sum = Offset;
}
Expand All @@ -2160,8 +2162,8 @@ Value *InstCombinerImpl::OptimizePointerDifference(Value *LHS, Value *RHS,
return Sum;
};

Value *Result = EmitOffsetFromBase(Base.LHSGEPs);
Value *Offset2 = EmitOffsetFromBase(Base.RHSGEPs);
Value *Result = EmitOffsetFromBase(Base.LHSGEPs, Base.LHSNW);
Value *Offset2 = EmitOffsetFromBase(Base.RHSGEPs, Base.RHSNW);

// If this is a single inbounds GEP and the original sub was nuw,
// then the final multiplication is also nuw.
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/Transforms/InstCombine/sub-gep.ll
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ define i64 @multiple_geps_two_chains_gep_base(ptr %base, i64 %base.idx, i64 %idx

define i64 @multiple_geps_inbounds(ptr %base, i64 %idx, i64 %idx2) {
; CHECK-LABEL: @multiple_geps_inbounds(
; CHECK-NEXT: [[D:%.*]] = add i64 [[IDX:%.*]], [[IDX2:%.*]]
; CHECK-NEXT: [[D:%.*]] = add nsw i64 [[IDX:%.*]], [[IDX2:%.*]]
; CHECK-NEXT: ret i64 [[D]]
;
%p2 = getelementptr inbounds i8, ptr %base, i64 %idx
Expand All @@ -971,7 +971,7 @@ define i64 @multiple_geps_nusw(ptr %base, i64 %idx, i64 %idx2) {

define i64 @multiple_geps_nuw(ptr %base, i64 %idx, i64 %idx2) {
; CHECK-LABEL: @multiple_geps_nuw(
; CHECK-NEXT: [[D:%.*]] = add i64 [[IDX:%.*]], [[IDX2:%.*]]
; CHECK-NEXT: [[D:%.*]] = add nuw i64 [[IDX:%.*]], [[IDX2:%.*]]
; CHECK-NEXT: ret i64 [[D]]
;
%p2 = getelementptr nuw i8, ptr %base, i64 %idx
Expand All @@ -985,7 +985,7 @@ define i64 @multiple_geps_nuw(ptr %base, i64 %idx, i64 %idx2) {

define i64 @multiple_geps_inbounds_nuw(ptr %base, i64 %idx, i64 %idx2) {
; CHECK-LABEL: @multiple_geps_inbounds_nuw(
; CHECK-NEXT: [[D:%.*]] = add i64 [[IDX:%.*]], [[IDX2:%.*]]
; CHECK-NEXT: [[D:%.*]] = add nuw nsw i64 [[IDX:%.*]], [[IDX2:%.*]]
; CHECK-NEXT: ret i64 [[D]]
;
%p2 = getelementptr inbounds nuw i8, ptr %base, i64 %idx
Expand Down
Loading