Skip to content

Commit

Permalink
Automerge: [InstCombine] Fix GEPNoWrapFlags propagation in `foldGEPOf…
Browse files Browse the repository at this point in the history
…Phi` (#121572)

Closes llvm/llvm-project#121459.
  • Loading branch information
dtcxzyw authored and github-actions[bot] committed Jan 10, 2025
2 parents 6d6a264 + a4d9240 commit 939cd20
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
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

0 comments on commit 939cd20

Please sign in to comment.