Add InEdges (edges from header to the loop) in Loop Branch Heuristics, so
authorJakub Staszak <jstaszak@apple.com>
Thu, 28 Jul 2011 21:33:46 +0000 (21:33 +0000)
committerJakub Staszak <jstaszak@apple.com>
Thu, 28 Jul 2011 21:33:46 +0000 (21:33 +0000)
there is no frequency difference whether condition is in the header or in
the latch.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@136398 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/BranchProbabilityInfo.cpp

index e39cd221b5a70a02fde274833d11a8fd1465fad8..5ac2d814a00dbcc2ea7c5ce07e4bb0e686664ad7 100644 (file)
@@ -205,6 +205,9 @@ void BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) {
 
   SmallVector<BasicBlock *, 8> BackEdges;
   SmallVector<BasicBlock *, 8> ExitingEdges;
+  SmallVector<BasicBlock *, 8> InEdges; // Edges from header to the loop.
+
+  bool isHeader = BB == L->getHeader();
 
   for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
     BasicBlock *Succ = *I;
@@ -213,6 +216,8 @@ void BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) {
       ExitingEdges.push_back(Succ);
     else if (Succ == L->getHeader())
       BackEdges.push_back(Succ);
+    else if (isHeader)
+      InEdges.push_back(Succ);
   }
 
   if (uint32_t numBackEdges = BackEdges.size()) {
@@ -227,6 +232,18 @@ void BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) {
     }
   }
 
+  if (uint32_t numInEdges = InEdges.size()) {
+    uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
+    if (inWeight < NORMAL_WEIGHT)
+      inWeight = NORMAL_WEIGHT;
+
+    for (SmallVector<BasicBlock *, 8>::iterator EI = InEdges.begin(),
+         EE = InEdges.end(); EI != EE; ++EI) {
+      BasicBlock *Back = *EI;
+      BP->setEdgeWeight(BB, Back, inWeight);
+    }
+  }
+
   uint32_t numExitingEdges = ExitingEdges.size();
   if (uint32_t numNonExitingEdges = numSuccs - numExitingEdges) {
     uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numNonExitingEdges;