Skip to content

[InstCombine] Fix GEPNoWrapFlags propagation in foldGEPOfPhi #121572

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
Jan 3, 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
5 changes: 5 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2782,6 +2782,7 @@ static Instruction *foldGEPOfPhi(GetElementPtrInst &GEP, PHINode *PN,
// loop iteration).
if (Op1 == &GEP)
return nullptr;
GEPNoWrapFlags NW = Op1->getNoWrapFlags();

int DI = -1;

Expand Down Expand Up @@ -2838,6 +2839,8 @@ static Instruction *foldGEPOfPhi(GetElementPtrInst &GEP, PHINode *PN,
}
}
}

NW &= Op2->getNoWrapFlags();
}

// If not all GEPs are identical we'll have to create a new PHI node.
Expand All @@ -2847,6 +2850,8 @@ static Instruction *foldGEPOfPhi(GetElementPtrInst &GEP, PHINode *PN,
return nullptr;

auto *NewGEP = cast<GetElementPtrInst>(Op1->clone());
NewGEP->setNoWrapFlags(NW);

if (DI == -1) {
// All the GEPs feeding the PHI are identical. Clone one down into our
// BB so that it can be merged with the current GEP.
Expand Down
58 changes: 58 additions & 0 deletions llvm/test/Transforms/InstCombine/opaque-ptr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,64 @@ join:
ret ptr %gep
}

define ptr @gep_of_phi_of_gep_flags1(i1 %c, ptr %p) {
; CHECK-LABEL: @gep_of_phi_of_gep_flags1(
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
; CHECK: if:
; CHECK-NEXT: br label [[JOIN:%.*]]
; CHECK: else:
; CHECK-NEXT: br label [[JOIN]]
; CHECK: join:
; CHECK-NEXT: [[TMP1:%.*]] = phi i64 [ 4, [[IF]] ], [ 8, [[ELSE]] ]
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[TMP1]]
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[TMP2]], i64 4
; CHECK-NEXT: ret ptr [[GEP]]
;
br i1 %c, label %if, label %else

if:
%gep1 = getelementptr inbounds i32, ptr %p, i64 1
br label %join

else:
%gep2 = getelementptr i32, ptr %p, i64 2
br label %join

join:
%phi = phi ptr [ %gep1, %if ], [ %gep2, %else ]
%gep = getelementptr i32, ptr %phi, i64 1
ret ptr %gep
}

define ptr @gep_of_phi_of_gep_flags2(i1 %c, ptr %p) {
; CHECK-LABEL: @gep_of_phi_of_gep_flags2(
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
; CHECK: if:
; CHECK-NEXT: br label [[JOIN:%.*]]
; CHECK: else:
; CHECK-NEXT: br label [[JOIN]]
; CHECK: join:
; CHECK-NEXT: [[TMP1:%.*]] = phi i64 [ 4, [[IF]] ], [ 8, [[ELSE]] ]
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr nuw i8, ptr [[P:%.*]], i64 [[TMP1]]
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[TMP2]], i64 4
; CHECK-NEXT: ret ptr [[GEP]]
;
br i1 %c, label %if, label %else

if:
%gep1 = getelementptr nuw i32, ptr %p, i64 1
br label %join

else:
%gep2 = getelementptr nuw i32, ptr %p, i64 2
br label %join

join:
%phi = phi ptr [ %gep1, %if ], [ %gep2, %else ]
%gep = getelementptr i32, ptr %phi, i64 1
ret ptr %gep
}

define ptr @gep_of_phi_of_gep_different_type(i1 %c, ptr %p) {
; CHECK-LABEL: @gep_of_phi_of_gep_different_type(
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
Expand Down
Loading