Skip to content

[ConstraintElim] Support decrementing inductions with step -1. #68644

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
Oct 10, 2023
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
20 changes: 20 additions & 0 deletions llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,26 @@ void State::addInfoForInductions(BasicBlock &BB) {
else
return;

// Handle negative steps.
if (StepOffset.isNegative()) {
// TODO: Extend to allow steps > -1.
if (!(-StepOffset).isOne())
return;

// AR may wrap.
// Add StartValue >= PN conditional on B <= StartValue which guarantees that
// the loop exits before wrapping with a step of -1.
WorkList.push_back(FactOrCheck::getConditionFact(
DTN, CmpInst::ICMP_UGE, StartValue, PN,
ConditionTy(CmpInst::ICMP_ULE, B, StartValue)));
// Add PN > B conditional on B <= StartValue which guarantees that the loop
// exits when reaching B with a step of -1.
WorkList.push_back(FactOrCheck::getConditionFact(
DTN, CmpInst::ICMP_UGT, PN, B,
ConditionTy(CmpInst::ICMP_ULE, B, StartValue)));
return;
}

// Make sure AR either steps by 1 or that the value we compare against is a
// GEP based on the same start value and all offsets are a multiple of the
// step size, to guarantee that the induction will reach the value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ define void @add_rec_decreasing_cond_true_constant(i8 noundef %len) {
; CHECK-NEXT: [[CMP2_NOT:%.*]] = icmp eq i8 [[K_0]], 0
; CHECK-NEXT: br i1 [[CMP2_NOT]], label [[EXIT:%.*]], label [[LOOP_LATCH]]
; CHECK: loop.latch:
; CHECK-NEXT: [[CMP_NOT_I:%.*]] = icmp ult i8 [[K_0]], 5
; CHECK-NEXT: call void @use(i1 [[CMP_NOT_I]])
; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[K_DEC]] = add i8 [[K_0]], -1
; CHECK-NEXT: br label [[LOOP_HEADER]]
; CHECK: exit:
Expand Down Expand Up @@ -87,8 +86,7 @@ define void @add_rec_decreasing_cond_true_start_signed_positive(i8 noundef %star
; CHECK-NEXT: [[CMP2_NOT:%.*]] = icmp eq i8 [[K_0]], 0
; CHECK-NEXT: br i1 [[CMP2_NOT]], label [[EXIT:%.*]], label [[LOOP_LATCH]]
; CHECK: loop.latch:
; CHECK-NEXT: [[CMP_NOT_I:%.*]] = icmp ult i8 [[K_0]], [[START]]
; CHECK-NEXT: call void @use(i1 [[CMP_NOT_I]])
; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[K_DEC]] = add i8 [[K_0]], -1
; CHECK-NEXT: br label [[LOOP_HEADER]]
; CHECK: exit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ define void @inner_add_rec_decreasing(i32 noundef %len) {
; CHECK-NEXT: [[CMP2_NOT:%.*]] = icmp eq i32 [[K_0]], 0
; CHECK-NEXT: br i1 [[CMP2_NOT]], label [[OUTER_LATCH]], label [[INNER_LATCH]]
; CHECK: inner.latch:
; CHECK-NEXT: [[CMP_NOT_I:%.*]] = icmp ult i32 [[K_0]], [[LEN]]
; CHECK-NEXT: call void @use(i1 [[CMP_NOT_I]])
; CHECK-NEXT: call void @use(i1 true)
; CHECK-NEXT: [[K_DEC]] = add i32 [[K_0]], -1
; CHECK-NEXT: br label [[INNER_HEADER]]
; CHECK: outer.latch:
Expand Down
10 changes: 2 additions & 8 deletions llvm/test/Transforms/PhaseOrdering/loop-access-checks.ll
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ declare void @abort()

define void @monkey(ptr noundef %arr, i32 noundef %len) {
; CHECK-LABEL: define void @monkey
; CHECK-SAME: (ptr nocapture noundef [[ARR:%.*]], i32 noundef [[LEN:%.*]]) local_unnamed_addr {
; CHECK-SAME: (ptr nocapture noundef [[ARR:%.*]], i32 noundef [[LEN:%.*]]) local_unnamed_addr #[[ATTR1:[0-9]+]] {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP8:%.*]] = icmp ugt i32 [[LEN]], 1
; CHECK-NEXT: br i1 [[CMP8]], label [[FOR_BODY4_PREHEADER:%.*]], label [[FOR_COND_CLEANUP:%.*]]
Expand All @@ -284,13 +284,7 @@ define void @monkey(ptr noundef %arr, i32 noundef %len) {
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[INC]], [[LEN]]
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY4_PREHEADER]], label [[FOR_COND_CLEANUP]]
; CHECK: for.body4:
; CHECK-NEXT: [[K_07:%.*]] = phi i32 [ [[DEC:%.*]], [[AT_EXIT:%.*]] ], [ [[I_09]], [[FOR_BODY4_PREHEADER]] ]
; CHECK-NEXT: [[CMP_NOT_I:%.*]] = icmp ult i32 [[K_07]], [[LEN]]
; CHECK-NEXT: br i1 [[CMP_NOT_I]], label [[AT_EXIT]], label [[IF_THEN_I:%.*]]
; CHECK: if.then.i:
; CHECK-NEXT: tail call void @abort()
; CHECK-NEXT: unreachable
; CHECK: at.exit:
; CHECK-NEXT: [[K_07:%.*]] = phi i32 [ [[DEC:%.*]], [[FOR_BODY4]] ], [ [[I_09]], [[FOR_BODY4_PREHEADER]] ]
; CHECK-NEXT: [[IDX_EXT_I:%.*]] = zext i32 [[K_07]] to i64
; CHECK-NEXT: [[ADD_PTR_I:%.*]] = getelementptr inbounds i32, ptr [[ARR]], i64 [[IDX_EXT_I]]
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ADD_PTR_I]], align 4
Expand Down