14 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYIMPL_H
15 #define LLVM_ANALYSIS_BLOCKFREQUENCYIMPL_H
32 class BlockFrequencyInfo;
33 class MachineBlockFrequencyInfo;
40 template<
class BlockT,
class FunctionT,
class BlockProbInfoT>
51 const uint32_t EntryFreq;
53 std::string getBlockName(
BasicBlock *BB)
const {
63 ss <<
" derived from LLVM BB " << BB->
getName();
70 DEBUG(
dbgs() <<
"Frequency(" << getBlockName(BB) <<
") = " << Freq <<
"\n");
84 DEBUG(
dbgs() <<
"Frequency(" << getBlockName(BB) <<
") += " << Freq
85 <<
" --> " << Freqs[BB] <<
"\n");
89 std::vector<BlockT *> POT;
100 typedef typename std::vector<BlockT *>::iterator pot_iterator;
103 pot_iterator pot_begin() {
return POT.begin(); }
104 pot_iterator pot_end() {
return POT.end(); }
106 rpot_iterator rpot_begin() {
return POT.rbegin(); }
107 rpot_iterator rpot_end() {
return POT.rend(); }
109 rpot_iterator rpot_at(BlockT *BB) {
110 rpot_iterator
I = rpot_begin();
111 unsigned idx = RPO.
lookup(BB);
121 bool isBackedge(BlockT *Src, BlockT *Dst)
const {
122 unsigned a = RPO.
lookup(Src);
125 unsigned b = RPO.
lookup(Dst);
126 assert(b &&
"Destination block should be reachable");
132 BlockT *getSingleBlockPred(BlockT *BB) {
133 typename GT::ChildIteratorType
149 void doBlock(BlockT *BB, BlockT *LoopHead,
152 DEBUG(
dbgs() <<
"doBlock(" << getBlockName(BB) <<
")\n");
155 if (BB == LoopHead) {
156 setBlockFreq(BB, EntryFreq);
160 if(BlockT *Pred = getSingleBlockPred(BB)) {
161 if (BlocksInLoop.
count(Pred))
162 setBlockFreq(BB, getEdgeFreq(Pred, BB));
167 bool isInLoop =
false;
168 bool isLoopHead =
false;
170 for (
typename GT::ChildIteratorType
176 if (isBackedge(Pred, BB)) {
178 }
else if (BlocksInLoop.
count(Pred)) {
179 incBlockFreq(BB, getEdgeFreq(Pred, BB));
195 assert(I != LoopExitProb.
end() &&
"Loop header missing from table");
196 Freqs[BB] /= I->second;
197 DEBUG(
dbgs() <<
"Loop header scaled to " << Freqs[BB] <<
".\n");
201 void doLoop(BlockT *Head, BlockT *Tail) {
202 DEBUG(
dbgs() <<
"doLoop(" << getBlockName(Head) <<
", "
203 << getBlockName(Tail) <<
")\n");
207 for (rpot_iterator
I = rpot_at(Head), E = rpot_at(Tail); ; ++
I) {
209 doBlock(BB, Head, BlocksInLoop);
218 for (
typename GT::ChildIteratorType
224 if (isBackedge(Pred, Head))
225 BackFreq += getEdgeFreq(Pred, Head);
238 uint64_t D = std::max(
getBlockFreq(Head).getFrequency(), UINT64_C(1));
249 if (D > UINT32_MAX) {
257 LoopExitProb.
insert(std::make_pair(Head, LEP));
258 DEBUG(
dbgs() <<
"LoopExitProb[" << getBlockName(Head) <<
"] = " << LEP
259 <<
" from 1 - " << BackFreq <<
" / " <<
getBlockFreq(Head)
268 void doFunction(FunctionT *fn, BlockProbInfoT *bpi) {
275 LoopExitProb.
clear();
278 BlockT *EntryBlock = fn->begin();
280 std::copy(
po_begin(EntryBlock),
po_end(EntryBlock), std::back_inserter(POT));
283 for (rpot_iterator
I = rpot_begin(), E = rpot_end();
I != E; ++
I) {
290 for (pot_iterator
I = pot_begin(), E = pot_end();
I != E; ++
I) {
292 BlockT *LastTail = 0;
295 for (
typename GT::ChildIteratorType
296 PI = GraphTraits< Inverse<BlockT *> >::child_begin(BB),
297 PE = GraphTraits< Inverse<BlockT *> >::child_end(BB);
301 if (isBackedge(Pred, BB) && (!LastTail || RPO[Pred] > RPO[LastTail]))
306 doLoop(BB, LastTail);
311 doLoop(*(rpot_begin()), *(pot_begin()));
319 if (I != Freqs.end())
325 OS <<
"\n\n---- Block Freqs ----\n";
326 for (
typename FunctionT::iterator
I = Fn->begin(), E = Fn->end();
I != E;) {
335 <<
" = " << getEdgeFreq(BB, Succ) <<
"\n";
std::string str() const
str - Get the contents as an std::string.
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
StringRef getName() const
static error_code advance(T &it, size_t Val)
po_iterator< T > po_begin(T G)
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, std::size_t >::type countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the most significant bit to the least stopping at the first 1...
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
BlockFrequency getBlockFreq(const BlockT *BB) const
getBlockFreq - Return block frequency. Return 0 if we don't have it.
const BasicBlock * getBasicBlock() const
LLVM Basic Block Representation.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void print(raw_ostream &OS) const
block Branch Probability Basic Block static false std::string getBlockName(MachineBasicBlock *BB)
Helper to print the name of a MBB.
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
po_iterator< T > po_end(T G)
std::reverse_iterator< const_iterator > reverse_iterator
ValueT lookup(const KeyT &Val) const
iterator find(const KeyT &Val)