aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/LoopUtils.cpp
diff options
context:
space:
mode:
authorEvgeniy Brevnov <evgueni.brevnov@gmail.com>2020-01-16 17:34:02 +0700
committerEvgeniy Brevnov <evgueni.brevnov@gmail.com>2020-01-20 16:58:07 +0700
commit10357e1c89b370a18500a8a8d69a68ab72db979e (patch)
tree739a5bea3cbf271f48029fba4c55eddf592aa498 /llvm/lib/Transforms/Utils/LoopUtils.cpp
parent84c4c87e04a48628259e920780623f427a9fd9b1 (diff)
downloadllvm-10357e1c89b370a18500a8a8d69a68ab72db979e.zip
llvm-10357e1c89b370a18500a8a8d69a68ab72db979e.tar.gz
llvm-10357e1c89b370a18500a8a8d69a68ab72db979e.tar.bz2
[LoopUtils] Better accuracy for getLoopEstimatedTripCount.
Summary: Current implementation of getLoopEstimatedTripCount returns 1 iteration less than it should. The reason is that in bottom tested loop first iteration is executed before first back branch is taken. For example for loop with !{!"branch_weights", i32 1 // taken, i32 1 // exit} metadata getLoopEstimatedTripCount gives 1 while actual number of iterations is 2. Reviewers: Ayal, fhahn Reviewed By: Ayal Subscribers: mgorny, hiraditya, zzheng, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D71990
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopUtils.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/LoopUtils.cpp13
1 files changed, 8 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index 054ef17..98e4ca5 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -723,12 +723,15 @@ Optional<unsigned> llvm::getLoopEstimatedTripCount(Loop *L) {
if (LatchBR->getSuccessor(0) != L->getHeader())
std::swap(BackedgeTakenWeight, LatchExitWeight);
- if (!BackedgeTakenWeight || !LatchExitWeight)
- return 0;
+ if (!LatchExitWeight)
+ return None;
- // Divide the count of the backedge by the count of the edge exiting the loop,
- // rounding to nearest.
- return llvm::divideNearest(BackedgeTakenWeight, LatchExitWeight);
+ // Estimated backedge taken count is a ratio of the backedge taken weight by
+ // the the edge exiting weight, rounded to nearest.
+ uint64_t BackedgeTakenCount =
+ llvm::divideNearest(BackedgeTakenWeight, LatchExitWeight);
+ // Estimated trip count is one plus estimated backedge taken count.
+ return BackedgeTakenCount + 1;
}
bool llvm::hasIterationCountInvariantInParent(Loop *InnerLoop,