/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/MachineBranchProbabilityInfo.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- MachineBranchProbabilityInfo.cpp - Machine Branch Probability Info -===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This analysis uses probability info stored in Machine Basic Blocks. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
14 | | #include "llvm/CodeGen/MachineBasicBlock.h" |
15 | | #include "llvm/IR/Instructions.h" |
16 | | #include "llvm/Support/Debug.h" |
17 | | #include "llvm/Support/raw_ostream.h" |
18 | | |
19 | | using namespace llvm; |
20 | | |
21 | 102k | INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob", |
22 | 102k | "Machine Branch Probability Analysis", false, true) |
23 | 102k | INITIALIZE_PASS_END(MachineBranchProbabilityInfo, "machine-branch-prob", |
24 | | "Machine Branch Probability Analysis", false, true) |
25 | | |
26 | | cl::opt<unsigned> |
27 | | StaticLikelyProb("static-likely-prob", |
28 | | cl::desc("branch probability threshold in percentage" |
29 | | "to be considered very likely"), |
30 | | cl::init(80), cl::Hidden); |
31 | | |
32 | | cl::opt<unsigned> ProfileLikelyProb( |
33 | | "profile-likely-prob", |
34 | | cl::desc("branch probability threshold in percentage to be considered" |
35 | | " very likely when profile is available"), |
36 | | cl::init(51), cl::Hidden); |
37 | | |
38 | | char MachineBranchProbabilityInfo::ID = 0; |
39 | | |
40 | 0 | void MachineBranchProbabilityInfo::anchor() {} |
41 | | |
42 | | BranchProbability MachineBranchProbabilityInfo::getEdgeProbability( |
43 | | const MachineBasicBlock *Src, |
44 | 23.6M | MachineBasicBlock::const_succ_iterator Dst) const { |
45 | 23.6M | return Src->getSuccProbability(Dst); |
46 | 23.6M | } |
47 | | |
48 | | BranchProbability MachineBranchProbabilityInfo::getEdgeProbability( |
49 | 10.0M | const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const { |
50 | 10.0M | // This is a linear search. Try to use the const_succ_iterator version when |
51 | 10.0M | // possible. |
52 | 10.0M | return getEdgeProbability(Src, find(Src->successors(), Dst)); |
53 | 10.0M | } |
54 | | |
55 | | bool MachineBranchProbabilityInfo::isEdgeHot( |
56 | 0 | const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const { |
57 | 0 | BranchProbability HotProb(StaticLikelyProb, 100); |
58 | 0 | return getEdgeProbability(Src, Dst) > HotProb; |
59 | 0 | } |
60 | | |
61 | | MachineBasicBlock * |
62 | 0 | MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const { |
63 | 0 | auto MaxProb = BranchProbability::getZero(); |
64 | 0 | MachineBasicBlock *MaxSucc = nullptr; |
65 | 0 | for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), |
66 | 0 | E = MBB->succ_end(); I != E; ++I) { |
67 | 0 | auto Prob = getEdgeProbability(MBB, I); |
68 | 0 | if (Prob > MaxProb) { |
69 | 0 | MaxProb = Prob; |
70 | 0 | MaxSucc = *I; |
71 | 0 | } |
72 | 0 | } |
73 | 0 |
|
74 | 0 | BranchProbability HotProb(StaticLikelyProb, 100); |
75 | 0 | if (getEdgeProbability(MBB, MaxSucc) >= HotProb) |
76 | 0 | return MaxSucc; |
77 | 0 | |
78 | 0 | return nullptr; |
79 | 0 | } |
80 | | |
81 | | raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability( |
82 | | raw_ostream &OS, const MachineBasicBlock *Src, |
83 | 0 | const MachineBasicBlock *Dst) const { |
84 | 0 |
|
85 | 0 | const BranchProbability Prob = getEdgeProbability(Src, Dst); |
86 | 0 | OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst) |
87 | 0 | << " probability is " << Prob |
88 | 0 | << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n"); |
89 | 0 |
|
90 | 0 | return OS; |
91 | 0 | } |