Skip to content

Commit

Permalink
Introduce MachineBranchProbabilityInfo class, which has similar API to
Browse files Browse the repository at this point in the history
BranchProbabilityInfo (expect setEdgeWeight which is not available here).
Branch Weights are kept in MachineBasicBlocks. To turn off this analysis
set -use-mbpi=false.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@133184 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Jakub Staszak committed Jun 16, 2011
1 parent 1300f30 commit 7cc2b07
Show file tree
Hide file tree
Showing 12 changed files with 356 additions and 27 deletions.
5 changes: 5 additions & 0 deletions include/llvm/Analysis/BranchProbabilityInfo.h
Expand Up @@ -25,6 +25,11 @@ class raw_ostream;
class BranchProbabilityInfo : public FunctionPass {

// Default weight value. Used when we don't have information about the edge.
// TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
// the successors have a weight yet. But it doesn't make sense when providing
// weight to an edge that may have siblings with non-zero weights. This can
// be handled various ways, but it's probably fine for an edge with unknown
// weight to just "inherit" the non-zero weight of an adjacent successor.
static const uint32_t DEFAULT_WEIGHT = 16;

typedef std::pair<BasicBlock *, BasicBlock *> Edge;
Expand Down
3 changes: 2 additions & 1 deletion include/llvm/CodeGen/FunctionLoweringInfo.h
Expand Up @@ -24,6 +24,7 @@
#ifndef NDEBUG
#include "llvm/ADT/SmallSet.h"
#endif
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/CodeGen/ValueTypes.h"
#include "llvm/CodeGen/ISDOpcodes.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
Expand Down Expand Up @@ -57,7 +58,7 @@ class FunctionLoweringInfo {
const Function *Fn;
MachineFunction *MF;
MachineRegisterInfo *RegInfo;

BranchProbabilityInfo *BPI;
/// CanLowerReturn - true iff the function's return value can be lowered to
/// registers.
bool CanLowerReturn;
Expand Down
44 changes: 37 additions & 7 deletions include/llvm/CodeGen/MachineBasicBlock.h
Expand Up @@ -16,6 +16,7 @@

#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/ADT/GraphTraits.h"
#include "llvm/Support/DataTypes.h"
#include <functional>

namespace llvm {
Expand All @@ -27,6 +28,7 @@ class MCSymbol;
class SlotIndexes;
class StringRef;
class raw_ostream;
class MachineBranchProbabilityInfo;

template <>
struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
Expand Down Expand Up @@ -63,12 +65,19 @@ class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
const BasicBlock *BB;
int Number;
MachineFunction *xParent;

/// Predecessors/Successors - Keep track of the predecessor / successor
/// basicblocks.
std::vector<MachineBasicBlock *> Predecessors;
std::vector<MachineBasicBlock *> Successors;


/// Weights - Keep track of the weights to the successors. This vector
/// has the same order as Successors, or it is empty if we don't use it
/// (disable optimization).
std::vector<uint32_t> Weights;
typedef std::vector<uint32_t>::iterator weight_iterator;

/// LiveIns - Keep track of the physical registers that are livein of
/// the basicblock.
std::vector<unsigned> LiveIns;
Expand Down Expand Up @@ -244,11 +253,13 @@ class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
void updateTerminator();

// Machine-CFG mutators

/// addSuccessor - Add succ as a successor of this MachineBasicBlock.
/// The Predecessors list of succ is automatically updated.
/// The Predecessors list of succ is automatically updated. WEIGHT
/// parameter is stored in Weights list and it may be used by
/// MachineBranchProbabilityInfo analysis to calculate branch probability.
///
void addSuccessor(MachineBasicBlock *succ);
void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0);

/// removeSuccessor - Remove successor from the successors list of this
/// MachineBasicBlock. The Predecessors list of succ is automatically updated.
Expand All @@ -260,7 +271,12 @@ class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
/// updated. Return the iterator to the element after the one removed.
///
succ_iterator removeSuccessor(succ_iterator I);


/// replaceSuccessor - Replace successor OLD with NEW and update weight info.
///
void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);


/// transferSuccessors - Transfers all the successors from MBB to this
/// machine basic block (i.e., copies all the successors fromMBB and
/// remove all the successors from fromMBB).
Expand Down Expand Up @@ -396,8 +412,22 @@ class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
/// getSymbol - Return the MCSymbol for this basic block.
///
MCSymbol *getSymbol() const;

private: // Methods used to maintain doubly linked list of blocks...


private:
/// getWeightIterator - Return weight iterator corresponding to the I
/// successor iterator.
weight_iterator getWeightIterator(succ_iterator I);

friend class MachineBranchProbabilityInfo;

/// getSuccWeight - Return weight of the edge from this block to MBB. This
/// method should NOT be called directly, but by using getEdgeWeight method
/// from MachineBranchProbabilityInfo class.
uint32_t getSuccWeight(MachineBasicBlock *succ);


// Methods used to maintain doubly linked list of blocks...
friend struct ilist_traits<MachineBasicBlock>;

// Machine-CFG mutators
Expand Down
77 changes: 77 additions & 0 deletions include/llvm/CodeGen/MachineBranchProbabilityInfo.h
@@ -0,0 +1,77 @@

//==- MachineBranchProbabilityInfo.h - Machine Branch Probability Analysis -==//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass is used to evaluate branch probabilties on machine basic blocks.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
#define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H

#include "llvm/Pass.h"
#include "llvm/Support/BranchProbability.h"
#include <climits>

namespace llvm {

class raw_ostream;

class MachineBranchProbabilityInfo : public ImmutablePass {

// Default weight value. Used when we don't have information about the edge.
// TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
// the successors have a weight yet. But it doesn't make sense when providing
// weight to an edge that may have siblings with non-zero weights. This can
// be handled various ways, but it's probably fine for an edge with unknown
// weight to just "inherit" the non-zero weight of an adjacent successor.
static const uint32_t DEFAULT_WEIGHT = 16;

// Get sum of the block successors' weights.
uint32_t getSumForBlock(MachineBasicBlock *MBB) const;

public:
static char ID;

MachineBranchProbabilityInfo() : ImmutablePass(ID) {
PassRegistry &Registry = *PassRegistry::getPassRegistry();
initializeMachineBranchProbabilityInfoPass(Registry);
}

void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
}

// Return edge weight. If we don't have any informations about it - return
// DEFAULT_WEIGHT.
uint32_t getEdgeWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst) const;

// A 'Hot' edge is an edge which probability is >= 80%.
bool isEdgeHot(MachineBasicBlock *Src, MachineBasicBlock *Dst) const;

// Return a hot successor for the block BB or null if there isn't one.
MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) const;

// Return a probability as a fraction between 0 (0% probability) and
// 1 (100% probability), however the value is never equal to 0, and can be 1
// only iff SRC block has only one successor.
BranchProbability getEdgeProbability(MachineBasicBlock *Src,
MachineBasicBlock *Dst) const;

// Print value between 0 (0% probability) and 1 (100% probability),
// however the value is never equal to 0, and can be 1 only iff SRC block
// has only one successor.
raw_ostream &printEdgeProbability(raw_ostream &OS, MachineBasicBlock *Src,
MachineBasicBlock *Dst) const;
};

}


#endif
1 change: 1 addition & 0 deletions include/llvm/InitializePasses.h
Expand Up @@ -144,6 +144,7 @@ void initializeLowerIntrinsicsPass(PassRegistry&);
void initializeLowerInvokePass(PassRegistry&);
void initializeLowerSetJmpPass(PassRegistry&);
void initializeLowerSwitchPass(PassRegistry&);
void initializeMachineBranchProbabilityInfoPass(PassRegistry&);
void initializeMachineCSEPass(PassRegistry&);
void initializeMachineDominatorTreePass(PassRegistry&);
void initializeMachineLICMPass(PassRegistry&);
Expand Down
2 changes: 1 addition & 1 deletion lib/Analysis/BranchProbabilityInfo.cpp
Expand Up @@ -348,8 +348,8 @@ getEdgeProbability(BasicBlock *Src, BasicBlock *Dst) const {
raw_ostream &
BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
BasicBlock *Dst) const {
BranchProbability Prob = getEdgeProbability(Src, Dst);

const BranchProbability Prob = getEdgeProbability(Src, Dst);
OS << "edge " << Src->getNameStr() << " -> " << Dst->getNameStr()
<< " probability is " << Prob
<< (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
Expand Down
1 change: 1 addition & 0 deletions lib/CodeGen/CMakeLists.txt
Expand Up @@ -33,6 +33,7 @@ add_llvm_library(LLVMCodeGen
LocalStackSlotAllocation.cpp
LowerSubregs.cpp
MachineBasicBlock.cpp
MachineBranchProbabilityInfo.cpp
MachineCSE.cpp
MachineDominators.cpp
MachineFunction.cpp
Expand Down
76 changes: 69 additions & 7 deletions lib/CodeGen/MachineBasicBlock.cpp
Expand Up @@ -339,25 +339,64 @@ void MachineBasicBlock::updateTerminator() {
}
}

void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
Successors.push_back(succ);
succ->addPredecessor(this);
}
void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ, uint32_t weight) {

// If we see non-zero value for the first time it means we actually use Weight
// list, so we fill all Weights with 0's.
if (weight != 0 && Weights.empty())
Weights.resize(Successors.size());

if (weight != 0 || !Weights.empty())
Weights.push_back(weight);

Successors.push_back(succ);
succ->addPredecessor(this);
}

void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
succ->removePredecessor(this);
succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
assert(I != Successors.end() && "Not a current successor!");

// If Weight list is empty it means we don't use it (disabled optimization).
if (!Weights.empty()) {
weight_iterator WI = getWeightIterator(I);
Weights.erase(WI);
}

Successors.erase(I);
}

MachineBasicBlock::succ_iterator
MachineBasicBlock::removeSuccessor(succ_iterator I) {
assert(I != Successors.end() && "Not a current successor!");

// If Weight list is empty it means we don't use it (disabled optimization).
if (!Weights.empty()) {
weight_iterator WI = getWeightIterator(I);
Weights.erase(WI);
}

(*I)->removePredecessor(this);
return Successors.erase(I);
}

void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old,
MachineBasicBlock *New) {
uint32_t weight = 0;
succ_iterator SI = std::find(Successors.begin(), Successors.end(), Old);

// If Weight list is empty it means we don't use it (disabled optimization).
if (!Weights.empty()) {
weight_iterator WI = getWeightIterator(SI);
weight = *WI;
}

// Update the successor information.
removeSuccessor(SI);
addSuccessor(New, weight);
}

void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
Predecessors.push_back(pred);
}
Expand All @@ -374,7 +413,14 @@ void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) {

while (!fromMBB->succ_empty()) {
MachineBasicBlock *Succ = *fromMBB->succ_begin();
addSuccessor(Succ);
uint32_t weight = 0;


// If Weight list is empty it means we don't use it (disabled optimization).
if (!fromMBB->Weights.empty())
weight = *fromMBB->Weights.begin();

addSuccessor(Succ, weight);
fromMBB->removeSuccessor(Succ);
}
}
Expand Down Expand Up @@ -637,8 +683,7 @@ void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
}

// Update the successor information.
removeSuccessor(Old);
addSuccessor(New);
replaceSuccessor(Old, New);
}

/// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
Expand Down Expand Up @@ -720,6 +765,23 @@ MachineBasicBlock::findDebugLoc(MachineBasicBlock::iterator &MBBI) {
return DL;
}

/// getSuccWeight - Return weight of the edge from this block to MBB.
///
uint32_t MachineBasicBlock::getSuccWeight(MachineBasicBlock *succ) {
succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
return *getWeightIterator(I);
}

/// getWeightIterator - Return wight iterator corresonding to the I successor
/// iterator
MachineBasicBlock::weight_iterator MachineBasicBlock::
getWeightIterator(MachineBasicBlock::succ_iterator I) {
assert(Weights.size() == Successors.size() && "Async weight list!");
size_t index = std::distance(Successors.begin(), I);
assert(index < Weights.size() && "Not a current successor!");
return Weights.begin() + index;
}

void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB,
bool t) {
OS << "BB#" << MBB->getNumber();
Expand Down

0 comments on commit 7cc2b07

Please sign in to comment.