2011-06-17 04:22:37 +08:00
|
|
|
//===- MachineBranchProbabilityInfo.cpp - Machine Branch Probability Info -===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-06-17 04:22:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This analysis uses probability info stored in Machine Basic Blocks.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2011-06-17 04:22:37 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob",
|
|
|
|
"Machine Branch Probability Analysis", false, true)
|
|
|
|
INITIALIZE_PASS_END(MachineBranchProbabilityInfo, "machine-branch-prob",
|
|
|
|
"Machine Branch Probability Analysis", false, true)
|
|
|
|
|
2016-06-15 11:03:30 +08:00
|
|
|
cl::opt<unsigned>
|
|
|
|
StaticLikelyProb("static-likely-prob",
|
|
|
|
cl::desc("branch probability threshold in percentage"
|
|
|
|
"to be considered very likely"),
|
|
|
|
cl::init(80), cl::Hidden);
|
2016-06-04 07:48:36 +08:00
|
|
|
|
2016-06-15 06:27:17 +08:00
|
|
|
cl::opt<unsigned> ProfileLikelyProb(
|
|
|
|
"profile-likely-prob",
|
2016-06-15 11:03:30 +08:00
|
|
|
cl::desc("branch probability threshold in percentage to be considered"
|
|
|
|
" very likely when profile is available"),
|
2016-06-15 06:27:17 +08:00
|
|
|
cl::init(51), cl::Hidden);
|
|
|
|
|
2011-06-17 04:22:37 +08:00
|
|
|
char MachineBranchProbabilityInfo::ID = 0;
|
|
|
|
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
MachineBranchProbabilityInfo::MachineBranchProbabilityInfo()
|
|
|
|
: ImmutablePass(ID) {
|
|
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
|
|
|
initializeMachineBranchProbabilityInfoPass(Registry);
|
|
|
|
}
|
|
|
|
|
2016-06-04 07:48:36 +08:00
|
|
|
void MachineBranchProbabilityInfo::anchor() {}
|
2011-12-20 10:50:00 +08:00
|
|
|
|
2015-12-01 13:29:22 +08:00
|
|
|
BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
|
|
|
|
const MachineBasicBlock *Src,
|
|
|
|
MachineBasicBlock::const_succ_iterator Dst) const {
|
|
|
|
return Src->getSuccProbability(Dst);
|
2011-06-17 04:22:37 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 13:29:22 +08:00
|
|
|
BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
|
|
|
|
const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
|
2012-08-21 06:01:38 +08:00
|
|
|
// This is a linear search. Try to use the const_succ_iterator version when
|
|
|
|
// possible.
|
2016-08-12 11:55:06 +08:00
|
|
|
return getEdgeProbability(Src, find(Src->successors(), Dst));
|
2012-08-21 06:01:38 +08:00
|
|
|
}
|
|
|
|
|
2016-06-04 07:48:36 +08:00
|
|
|
bool MachineBranchProbabilityInfo::isEdgeHot(
|
|
|
|
const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
|
|
|
|
BranchProbability HotProb(StaticLikelyProb, 100);
|
2015-12-01 13:29:22 +08:00
|
|
|
return getEdgeProbability(Src, Dst) > HotProb;
|
2011-06-17 04:22:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock *
|
|
|
|
MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const {
|
2015-12-01 13:29:22 +08:00
|
|
|
auto MaxProb = BranchProbability::getZero();
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineBasicBlock *MaxSucc = nullptr;
|
2011-06-17 04:22:37 +08:00
|
|
|
for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
|
|
|
|
E = MBB->succ_end(); I != E; ++I) {
|
2015-12-01 13:29:22 +08:00
|
|
|
auto Prob = getEdgeProbability(MBB, I);
|
|
|
|
if (Prob > MaxProb) {
|
|
|
|
MaxProb = Prob;
|
2011-11-14 16:55:59 +08:00
|
|
|
MaxSucc = *I;
|
2011-06-17 04:22:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-04 07:48:36 +08:00
|
|
|
BranchProbability HotProb(StaticLikelyProb, 100);
|
2015-12-01 13:29:22 +08:00
|
|
|
if (getEdgeProbability(MBB, MaxSucc) >= HotProb)
|
2011-06-17 04:22:37 +08:00
|
|
|
return MaxSucc;
|
|
|
|
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2011-06-17 04:22:37 +08:00
|
|
|
}
|
|
|
|
|
2014-03-26 02:01:32 +08:00
|
|
|
raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
|
|
|
|
raw_ostream &OS, const MachineBasicBlock *Src,
|
|
|
|
const MachineBasicBlock *Dst) const {
|
2011-06-17 04:22:37 +08:00
|
|
|
|
|
|
|
const BranchProbability Prob = getEdgeProbability(Src, Dst);
|
2017-12-05 01:18:51 +08:00
|
|
|
OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst)
|
2014-03-26 02:01:32 +08:00
|
|
|
<< " probability is " << Prob
|
2011-06-17 04:22:37 +08:00
|
|
|
<< (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
|
|
|
|
|
|
|
|
return OS;
|
|
|
|
}
|