Skip to content

Commit 5f5f040

Browse files
committed
step2
Another imp All order
1 parent 815aa31 commit 5f5f040

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15581,6 +15581,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1558115581
Predicate = CmpInst::getSwappedPredicate(Predicate);
1558215582
}
1558315583

15584+
unsigned StartSize = RewriteMap.size();
1558415585
// Check for a condition of the form (-C1 + X < C2). InstCombine will
1558515586
// create this form when combining two checks of the form (X u< C2 + C1) and
1558615587
// (X >=u C1).
@@ -15931,6 +15932,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1593115932
// Guards.RewriteMap. Conditions are processed in reverse order, so the
1593215933
// earliest conditions is processed first. This ensures the SCEVs with the
1593315934
// shortest dependency chains are constructed first.
15935+
SmallVector<std::tuple<CmpInst::Predicate, const SCEV *, const SCEV *>> ToProcess;
1593415936
for (auto [Term, EnterIfTrue] : reverse(Terms)) {
1593515937
SmallVector<Value *, 8> Worklist;
1593615938
SmallPtrSet<Value *, 8> Visited;
@@ -15945,7 +15947,9 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1594515947
EnterIfTrue ? Cmp->getPredicate() : Cmp->getInversePredicate();
1594615948
const auto *LHS = SE.getSCEV(Cmp->getOperand(0));
1594715949
const auto *RHS = SE.getSCEV(Cmp->getOperand(1));
15948-
CollectCondition(Predicate, LHS, RHS, Guards.RewriteMap);
15950+
// dbgs() << "\n" << *Cmp << "\n";
15951+
15952+
ToProcess.emplace_back(Predicate, LHS, RHS);
1594915953
continue;
1595015954
}
1595115955

@@ -15958,6 +15962,31 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
1595815962
}
1595915963
}
1596015964

15965+
ToProcess = to_vector(ToProcess);
15966+
sort(ToProcess, [&](const auto &A, const auto &B) {
15967+
15968+
const SCEV *X, *Y;
15969+
const auto &[APred, ALHS , ARHS] = A;
15970+
const auto &[BPred, BLHS , BRHS] = B;
15971+
bool AIsDiv = SE.matchURem(ALHS, X, Y) && ARHS->isZero();
15972+
bool BIsDiv = SE.matchURem(BLHS, X, Y) && BRHS->isZero();
15973+
return BIsDiv < AIsDiv;
15974+
/*bool AConst = isa<SCEVConstant>(ARHS);*/
15975+
/*// bool AEq = !(APred == CmpInst::ICMP_EQ || APred == CmpInst::ICMP_NE);*/
15976+
/*bool BConst = isa<SCEVConstant>(BRHS);*/
15977+
/*//bool BEq = !(BPred == CmpInst::ICMP_EQ || BPred == CmpInst::ICMP_NE);*/
15978+
/*return std::tie(AConst) < std::tie(BConst);*/
15979+
});
15980+
for (const auto &[Predicate, LHS, RHS]: ToProcess) {
15981+
CollectCondition(Predicate, LHS, RHS, Guards.RewriteMap);
15982+
}
15983+
15984+
/* for (const auto &[Predicate, LHS, RHS]: reverse(ToProcess)) {*/
15985+
/*CollectCondition(Predicate, LHS, RHS, Guards.RewriteMap);*/
15986+
/*}*/
15987+
15988+
15989+
1596115990
// Let the rewriter preserve NUW/NSW flags if the unsigned/signed ranges of
1596215991
// the replacement expressions are contained in the ranges of the replaced
1596315992
// expressions.
@@ -16096,6 +16125,11 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
1609616125
if (RewriteMap.empty())
1609716126
return Expr;
1609816127

16128+
/* dbgs() << "RWM\n";*/
16129+
/*for (const auto &[From, To] : RewriteMap) {*/
16130+
/*dbgs() << "From " << *From << " to " << *To << "\n";*/
16131+
16132+
/*}*/
1609916133
SCEVLoopGuardRewriter Rewriter(SE, *this);
1610016134
return Rewriter.visit(Expr);
1610116135
}

llvm/test/Transforms/LoopUnroll/peel-last-iteration-with-guards.ll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,22 @@ define i32 @peel_with_guard_known_nonnegative_2(ptr %x, i32 %w) {
7979
; CHECK-NEXT: [[WIDE_TRIP_COUNT:%.*]] = zext nneg i32 [[W]] to i64
8080
; CHECK-NEXT: br label %[[LOOP_HEADER:.*]]
8181
; CHECK: [[LOOP_HEADER]]:
82-
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, %[[PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP_LATCH:.*]] ]
83-
; CHECK-NEXT: [[RED:%.*]] = phi i32 [ 0, %[[PH]] ], [ [[ADD:%.*]], %[[LOOP_LATCH]] ]
84-
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i64 [[IV]], [[TMP0]]
85-
; CHECK-NEXT: br i1 [[CMP1]], label %[[IF_THEN:.*]], label %[[LOOP_LATCH]]
82+
; CHECK-NEXT: [[TMP5:%.*]] = phi i64 [ 0, %[[PH]] ], [ [[IV_NEXT_PEEL:%.*]], %[[LOOP_LATCH:.*]] ]
83+
; CHECK-NEXT: [[TMP6:%.*]] = phi i32 [ 0, %[[PH]] ], [ [[ADD_PEEL:%.*]], %[[LOOP_LATCH]] ]
84+
; CHECK-NEXT: [[CMP1_PEEL:%.*]] = icmp eq i64 [[TMP5]], [[TMP0]]
85+
; CHECK-NEXT: br i1 [[CMP1_PEEL]], label %[[IF_THEN:.*]], label %[[LOOP_LATCH]]
8686
; CHECK: [[IF_THEN]]:
8787
; CHECK-NEXT: tail call void @foo()
8888
; CHECK-NEXT: br label %[[LOOP_LATCH]]
8989
; CHECK: [[LOOP_LATCH]]:
90-
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds nuw i32, ptr [[X]], i64 [[IV]]
91-
; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
92-
; CHECK-NEXT: [[ADD]] = add nsw i32 [[TMP1]], [[RED]]
93-
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
94-
; CHECK-NEXT: [[EC:%.*]] = icmp eq i64 [[IV_NEXT]], [[WIDE_TRIP_COUNT]]
95-
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT_LOOPEXIT:.*]], label %[[LOOP_HEADER]]
90+
; CHECK-NEXT: [[ARRAYIDX_PEEL:%.*]] = getelementptr inbounds nuw i32, ptr [[X]], i64 [[TMP5]]
91+
; CHECK-NEXT: [[TMP7:%.*]] = load i32, ptr [[ARRAYIDX_PEEL]], align 4
92+
; CHECK-NEXT: [[ADD_PEEL]] = add nsw i32 [[TMP7]], [[TMP6]]
93+
; CHECK-NEXT: [[IV_NEXT_PEEL]] = add nuw nsw i64 [[TMP5]], 1
94+
; CHECK-NEXT: [[EC_PEEL:%.*]] = icmp eq i64 [[IV_NEXT_PEEL]], [[WIDE_TRIP_COUNT]]
95+
; CHECK-NEXT: br i1 [[EC_PEEL]], label %[[EXIT_LOOPEXIT:.*]], label %[[LOOP_HEADER]]
9696
; CHECK: [[EXIT_LOOPEXIT]]:
97-
; CHECK-NEXT: [[ADD_LCSSA:%.*]] = phi i32 [ [[ADD]], %[[LOOP_LATCH]] ]
97+
; CHECK-NEXT: [[ADD_LCSSA:%.*]] = phi i32 [ [[ADD_PEEL]], %[[LOOP_LATCH]] ]
9898
; CHECK-NEXT: br label %[[EXIT]]
9999
; CHECK: [[EXIT]]:
100100
; CHECK-NEXT: [[RED_LCSSA:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[ADD_LCSSA]], %[[EXIT_LOOPEXIT]] ]

0 commit comments

Comments
 (0)