[PATCH] D20991: Set machine block placement hot prob threshold for both static and runtime profile.

Dehao Chen via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 9 13:11:46 PDT 2016


danielcdh updated this revision to Diff 60231.
danielcdh added a comment.

Handle the triangle-shape CFG.


http://reviews.llvm.org/D20991

Files:
  lib/CodeGen/MachineBlockPlacement.cpp
  lib/CodeGen/MachineBranchProbabilityInfo.cpp

Index: lib/CodeGen/MachineBranchProbabilityInfo.cpp
===================================================================
--- lib/CodeGen/MachineBranchProbabilityInfo.cpp
+++ lib/CodeGen/MachineBranchProbabilityInfo.cpp
@@ -29,6 +29,12 @@
     cl::desc("branch probability threshold to be considered very likely"),
     cl::init(80), cl::Hidden);
 
+cl::opt<unsigned> ProfileLikelyProb(
+    "profile-likely-prob",
+    cl::desc("branch probability threshold to be considered very likely "
+             "when profile is available"),
+    cl::init(51), cl::Hidden);
+
 char MachineBranchProbabilityInfo::ID = 0;
 
 void MachineBranchProbabilityInfo::anchor() {}
Index: lib/CodeGen/MachineBlockPlacement.cpp
===================================================================
--- lib/CodeGen/MachineBlockPlacement.cpp
+++ lib/CodeGen/MachineBlockPlacement.cpp
@@ -117,6 +117,7 @@
                                       cl::init(1), cl::Hidden);
 
 extern cl::opt<unsigned> StaticLikelyProb;
+extern cl::opt<unsigned> ProfileLikelyProb;
 
 namespace {
 class BlockChain;
@@ -407,7 +408,10 @@
 MachineBlockPlacement::selectBestSuccessor(MachineBasicBlock *BB,
                                            BlockChain &Chain,
                                            const BlockFilterSet *BlockFilter) {
-  const BranchProbability HotProb(StaticLikelyProb, 100);
+  const BranchProbability HotProb(
+      BB->getParent()->getFunction()->getEntryCount() ? ProfileLikelyProb
+                                                      : StaticLikelyProb,
+      100);
 
   MachineBasicBlock *BestSucc = nullptr;
   auto BestProb = BranchProbability::getZero();
@@ -454,6 +458,28 @@
     BranchProbability SuccProb;
     uint32_t SuccProbN = MBPI->getEdgeProbability(BB, Succ).getNumerator();
     uint32_t SuccProbD = AdjustedSumProb.getNumerator();
+
+    // When evaluating cost for the following CFG:
+    //  A
+    //  | \
+    //  |  B
+    //  | /
+    //  C
+    // Assume that the only cost is the taken jump instruction
+    // The cost of layout ABC = P(AC)
+    // The cost of layout ACB = P(AB) * 2
+    // We compare P(AB)*2 with P(AC) to find the best successor of A.
+    if (Successors.size() == 2) {
+      MachineBasicBlock *OtherSucc = NULL;
+      for (MachineBasicBlock *S : Successors)
+        if (S != Succ) {
+          OtherSucc = S;
+          break;
+        }
+      if (Succ->succ_size() == 1 && *Succ->succ_begin() == OtherSucc)
+        SuccProbN *= 2;
+    }
+
     if (SuccProbN >= SuccProbD)
       SuccProb = BranchProbability::getOne();
     else


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20991.60231.patch
Type: text/x-patch
Size: 2574 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160609/f0d281bd/attachment.bin>


More information about the llvm-commits mailing list