2011-06-16 22:22:37 +02:00
|
|
|
//===- MachineBranchProbabilityInfo.cpp - Machine Branch Probability Info -===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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-16 22:22:37 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This analysis uses probability info stored in Machine Basic Blocks.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2013-01-02 12:36:10 +01: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-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2019-11-15 00:15:48 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2011-06-16 22:22:37 +02: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)
|
|
|
|
|
2021-05-07 20:15:43 +02:00
|
|
|
namespace llvm {
|
2016-06-15 05:03:30 +02: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 01:48:36 +02:00
|
|
|
|
2016-06-15 00:27:17 +02:00
|
|
|
cl::opt<unsigned> ProfileLikelyProb(
|
|
|
|
"profile-likely-prob",
|
2016-06-15 05:03:30 +02:00
|
|
|
cl::desc("branch probability threshold in percentage to be considered"
|
|
|
|
" very likely when profile is available"),
|
2016-06-15 00:27:17 +02:00
|
|
|
cl::init(51), cl::Hidden);
|
2021-05-07 20:15:43 +02:00
|
|
|
} // namespace llvm
|
2016-06-15 00:27:17 +02:00
|
|
|
|
2011-06-16 22:22:37 +02: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-13 22:15:01 +01:00
|
|
|
MachineBranchProbabilityInfo::MachineBranchProbabilityInfo()
|
|
|
|
: ImmutablePass(ID) {
|
|
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
|
|
|
initializeMachineBranchProbabilityInfoPass(Registry);
|
|
|
|
}
|
|
|
|
|
2016-06-04 01:48:36 +02:00
|
|
|
void MachineBranchProbabilityInfo::anchor() {}
|
2011-12-20 03:50:00 +01:00
|
|
|
|
2015-12-01 06:29:22 +01:00
|
|
|
BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
|
|
|
|
const MachineBasicBlock *Src,
|
|
|
|
MachineBasicBlock::const_succ_iterator Dst) const {
|
|
|
|
return Src->getSuccProbability(Dst);
|
2011-06-16 22:22:37 +02:00
|
|
|
}
|
|
|
|
|
2015-12-01 06:29:22 +01:00
|
|
|
BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
|
|
|
|
const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
|
2012-08-21 00:01:38 +02:00
|
|
|
// This is a linear search. Try to use the const_succ_iterator version when
|
|
|
|
// possible.
|
2016-08-12 05:55:06 +02:00
|
|
|
return getEdgeProbability(Src, find(Src->successors(), Dst));
|
2012-08-21 00:01:38 +02:00
|
|
|
}
|
|
|
|
|
2016-06-04 01:48:36 +02:00
|
|
|
bool MachineBranchProbabilityInfo::isEdgeHot(
|
|
|
|
const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
|
|
|
|
BranchProbability HotProb(StaticLikelyProb, 100);
|
2015-12-01 06:29:22 +01:00
|
|
|
return getEdgeProbability(Src, Dst) > HotProb;
|
2011-06-16 22:22:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock *
|
|
|
|
MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const {
|
2015-12-01 06:29:22 +01:00
|
|
|
auto MaxProb = BranchProbability::getZero();
|
2014-04-14 02:51:57 +02:00
|
|
|
MachineBasicBlock *MaxSucc = nullptr;
|
2011-06-16 22:22:37 +02:00
|
|
|
for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
|
|
|
|
E = MBB->succ_end(); I != E; ++I) {
|
2015-12-01 06:29:22 +01:00
|
|
|
auto Prob = getEdgeProbability(MBB, I);
|
|
|
|
if (Prob > MaxProb) {
|
|
|
|
MaxProb = Prob;
|
2011-11-14 09:55:59 +01:00
|
|
|
MaxSucc = *I;
|
2011-06-16 22:22:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-04 01:48:36 +02:00
|
|
|
BranchProbability HotProb(StaticLikelyProb, 100);
|
2015-12-01 06:29:22 +01:00
|
|
|
if (getEdgeProbability(MBB, MaxSucc) >= HotProb)
|
2011-06-16 22:22:37 +02:00
|
|
|
return MaxSucc;
|
|
|
|
|
2014-04-14 02:51:57 +02:00
|
|
|
return nullptr;
|
2011-06-16 22:22:37 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 19:01:32 +01:00
|
|
|
raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
|
|
|
|
raw_ostream &OS, const MachineBasicBlock *Src,
|
|
|
|
const MachineBasicBlock *Dst) const {
|
2011-06-16 22:22:37 +02:00
|
|
|
|
|
|
|
const BranchProbability Prob = getEdgeProbability(Src, Dst);
|
2017-12-04 18:18:51 +01:00
|
|
|
OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst)
|
2014-03-25 19:01:32 +01:00
|
|
|
<< " probability is " << Prob
|
2011-06-16 22:22:37 +02:00
|
|
|
<< (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
|
|
|
|
|
|
|
|
return OS;
|
|
|
|
}
|