[PATCH] D72029: [LoopUtils][NFC] Minor refactoring in getLoopEstimatedTripCount.

Evgeniy via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 30 23:48:36 PST 2019


ebrevnov created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
ebrevnov added a reviewer: Ayal.
ebrevnov added a child revision: D71990: [LoopUtils] Better accuracy for getLoopEstimatedTripCount..

Split out of https://reviews.llvm.org/D71990. Minor stuff like renaming and introduction of divideNearest utility.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72029

Files:
  llvm/include/llvm/Support/MathExtras.h
  llvm/lib/Transforms/Utils/LoopUtils.cpp


Index: llvm/lib/Transforms/Utils/LoopUtils.cpp
===================================================================
--- llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -715,19 +715,17 @@
   // To estimate the number of times the loop body was executed, we want to
   // know the number of times the backedge was taken, vs. the number of times
   // we exited the loop.
-  uint64_t TrueVal, FalseVal;
-  if (!LatchBR->extractProfMetadata(TrueVal, FalseVal))
+  uint64_t LatchCycleWeight, LatchExitWeight;
+  if (!LatchBR->extractProfMetadata(LatchCycleWeight, LatchExitWeight))
     return None;
 
-  if (!TrueVal || !FalseVal)
+  if (LatchBR->getSuccessor(0) != L->getHeader())
+    std::swap(LatchCycleWeight, LatchExitWeight);
+
+  if (!LatchCycleWeight || !LatchExitWeight)
     return 0;
 
-  // Divide the count of the backedge by the count of the edge exiting the loop,
-  // rounding to nearest.
-  if (LatchBR->getSuccessor(0) == L->getHeader())
-    return (TrueVal + (FalseVal / 2)) / FalseVal;
-  else
-    return (FalseVal + (TrueVal / 2)) / TrueVal;
+  return llvm::divideNearest(LatchCycleWeight, LatchExitWeight);
 }
 
 bool llvm::hasIterationCountInvariantInParent(Loop *InnerLoop,
Index: llvm/include/llvm/Support/MathExtras.h
===================================================================
--- llvm/include/llvm/Support/MathExtras.h
+++ llvm/include/llvm/Support/MathExtras.h
@@ -732,6 +732,11 @@
   return alignTo(Numerator, Denominator) / Denominator;
 }
 
+/// Returns the integer nearest(Numerator / Denominator).
+inline uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator) {
+  return (Numerator + (Denominator / 2)) / Denominator;
+}
+
 /// Returns the largest uint64_t less than or equal to \p Value and is
 /// \p Skew mod \p Align. \p Align must be non-zero
 inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72029.235678.patch
Type: text/x-patch
Size: 1920 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191231/07ee0f21/attachment.bin>


More information about the llvm-commits mailing list