2011-06-17 04:22:37 +08:00
|
|
|
//===- MachineBranchProbabilityInfo.cpp - Machine Branch Probability Info -===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
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)
|
|
|
|
|
|
|
|
char MachineBranchProbabilityInfo::ID = 0;
|
|
|
|
|
2011-12-20 10:50:00 +08:00
|
|
|
void MachineBranchProbabilityInfo::anchor() { }
|
|
|
|
|
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.
|
2015-12-01 13:29:22 +08:00
|
|
|
return getEdgeProbability(Src,
|
|
|
|
std::find(Src->succ_begin(), Src->succ_end(), Dst));
|
2012-08-21 06:01:38 +08:00
|
|
|
}
|
|
|
|
|
2014-03-26 02:01:32 +08:00
|
|
|
bool
|
|
|
|
MachineBranchProbabilityInfo::isEdgeHot(const MachineBasicBlock *Src,
|
|
|
|
const MachineBasicBlock *Dst) const {
|
2011-06-17 04:22:37 +08:00
|
|
|
// Hot probability is at least 4/5 = 80%
|
2015-12-01 13:29:22 +08:00
|
|
|
static BranchProbability HotProb(4, 5);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 13:29:22 +08:00
|
|
|
static BranchProbability HotProb(4, 5);
|
|
|
|
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);
|
|
|
|
OS << "edge MBB#" << Src->getNumber() << " -> MBB#" << Dst->getNumber()
|
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;
|
|
|
|
}
|