Skip to content

Commit

Permalink
[InstCombine] Preserve all gep flags in dependent IV fold
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jun 19, 2024
1 parent 44cff96 commit 6efba06
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ static Value *foldDependentIVs(PHINode &PN, IRBuilderBase &Builder) {
if (!BO) {
auto *GEP = cast<GEPOperator>(IvNext);
return Builder.CreateGEP(GEP->getSourceElementType(), Start, Iv2, "",
cast<GEPOperator>(IvNext)->isInBounds());
cast<GEPOperator>(IvNext)->getNoWrapFlags());
}

assert(BO->isCommutative() && "Must be commutative");
Expand Down
35 changes: 33 additions & 2 deletions llvm/test/Transforms/InstCombine/dependent-ivs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,39 @@ exit:
ret void
}

define void @ptr_iv_no_inbounds(ptr %base, i64 %end) {
; CHECK-LABEL: define void @ptr_iv_no_inbounds(
define void @ptr_iv_nuw(ptr %base, i64 %end) {
; CHECK-LABEL: define void @ptr_iv_nuw(
; CHECK-SAME: ptr [[BASE:%.*]], i64 [[END:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT: [[IV_PTR:%.*]] = getelementptr nuw i8, ptr [[BASE]], i64 [[IV]]
; CHECK-NEXT: call void @use.p0(ptr [[IV_PTR]])
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 4
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[IV_NEXT]], [[END]]
; CHECK-NEXT: br i1 [[CMP]], label [[EXIT:%.*]], label [[LOOP]]
; CHECK: exit:
; CHECK-NEXT: ret void
;
entry:
br label %loop

loop:
%iv.ptr = phi ptr [ %iv.ptr.next, %loop ], [ %base, %entry ]
%iv = phi i64 [ %iv.next, %loop ], [ 0, %entry ]
call void @use.p0(ptr %iv.ptr)
%iv.next = add nuw nsw i64 %iv, 4
%iv.ptr.next = getelementptr nuw i8, ptr %base, i64 %iv.next
%cmp = icmp eq i64 %iv.next, %end
br i1 %cmp, label %exit, label %loop

exit:
ret void
}

define void @ptr_iv_no_flags(ptr %base, i64 %end) {
; CHECK-LABEL: define void @ptr_iv_no_flags(
; CHECK-SAME: ptr [[BASE:%.*]], i64 [[END:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP:%.*]]
Expand Down

0 comments on commit 6efba06

Please sign in to comment.