[PATCH] D87854: [SCEV] Use information from assume for BE taken count.
Florian Hahn via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 17 12:56:04 PDT 2020
fhahn created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
fhahn requested review of this revision.
Fixes PR47247
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D87854
Files:
llvm/lib/Analysis/ScalarEvolution.cpp
llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
Index: llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
+++ llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
@@ -30,7 +30,7 @@
define void @test_guard_and_assume(i32* nocapture readonly %data, i64 %count) {
; CHECK-LABEL: Determining loop execution counts for: @test_guard_and_assume
; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + %count)
-; CHECK-NEXT: Loop %loop: max backedge-taken count is -2
+; CHECK-NEXT: Loop %loop: max backedge-taken count is 3
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + %count)
;
entry:
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -12535,6 +12535,39 @@
}
const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) {
+ auto ApplyCondition = [&](ICmpInst *Cmp) {
+ Value *V = Cmp->getOperand(0);
+ const SCEV *Op0 = getSCEV(Cmp->getOperand(0));
+ const SCEV *Op1 = getSCEV(Cmp->getOperand(1));
+ auto Predicate = Cmp->getPredicate();
+ if (isa<SCEVConstant>(Op0)) {
+ std::swap(Op0, Op1);
+ Predicate = CmpInst::getSwappedPredicate(Predicate);
+ V = Cmp->getOperand(1);
+ }
+ if (!isa<SCEVUnknown>(Op0) || !isa<SCEVConstant>(Op1))
+ return;
+
+ // TODO: use information from more predicates.
+ switch (Predicate) {
+ case CmpInst::ICMP_ULT: {
+ ValueToSCEVMapTy RewriteMap;
+ RewriteMap[V] =
+ getUMinExpr(Op0, getMinusSCEV(Op1, getOne(Op1->getType())));
+ Expr = SCEVParameterRewriter::rewrite(Expr, *this, RewriteMap);
+ break;
+ }
+
+ case CmpInst::ICMP_EQ: {
+ ValueToSCEVMapTy RewriteMap;
+ RewriteMap[V] = getUMaxExpr(Op0, getAddExpr(Op1, getOne(Op1->getType())));
+ Expr = SCEVParameterRewriter::rewrite(Expr, *this, RewriteMap);
+ break;
+ }
+ default:
+ break;
+ }
+ };
// Starting at the loop predecessor, climb up the predecessor chain, as long
// as there are predecessors that can be found that have unique successors
// leading to the original header.
@@ -12553,23 +12586,20 @@
if (!Cmp)
continue;
- // TODO: use information from more predicates.
- switch (Cmp->getPredicate()) {
- case CmpInst::ICMP_ULT: {
- const SCEV *LHS = getSCEV(Cmp->getOperand(0));
- const SCEV *RHS = getSCEV(Cmp->getOperand(1));
- if (isa<SCEVUnknown>(LHS)) {
- ValueToSCEVMapTy RewriteMap;
- RewriteMap[Cmp->getOperand(0)] =
- getUMinExpr(LHS, getMinusSCEV(RHS, getOne(RHS->getType())));
- Expr = SCEVParameterRewriter::rewrite(Expr, *this, RewriteMap);
- }
+ ApplyCondition(Cmp);
+ }
- break;
- }
- default:
- break;
- }
+ for (auto &AssumeVH : AC.assumptions()) {
+ if (!AssumeVH)
+ continue;
+ auto *CI = cast<CallInst>(AssumeVH);
+ if (!DT.dominates(CI, L->getHeader()))
+ continue;
+ auto *Cmp = dyn_cast<ICmpInst>(CI->getOperand(0));
+ if (!Cmp)
+ continue;
+
+ ApplyCondition(Cmp);
}
return Expr;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87854.292597.patch
Type: text/x-patch
Size: 3311 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200917/f2bef5f1/attachment.bin>
More information about the llvm-commits
mailing list