2014-04-12 07:20:58 +08:00
|
|
|
//===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
|
2011-07-17 04:23:20 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Loops should be simplified before this analysis.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-26 03:25:40 +08:00
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
2014-04-12 07:20:58 +08:00
|
|
|
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
|
2011-07-17 04:23:20 +08:00
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
2014-04-22 01:57:07 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/InitializePasses.h"
|
2013-12-03 08:49:33 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2016-06-23 00:04:51 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2013-12-03 08:49:33 +08:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2016-06-23 00:04:51 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-07-17 04:23:20 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "block-freq"
|
|
|
|
|
2013-12-03 08:49:33 +08:00
|
|
|
#ifndef NDEBUG
|
2016-06-22 07:36:12 +08:00
|
|
|
|
|
|
|
static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
|
|
|
|
"view-machine-block-freq-propagation-dags", cl::Hidden,
|
|
|
|
cl::desc("Pop up a window to show a dag displaying how machine block "
|
|
|
|
"frequencies propagate through the CFG."),
|
|
|
|
cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
|
|
|
|
clEnumValN(GVDT_Fraction, "fraction",
|
|
|
|
"display a graph using the "
|
|
|
|
"fractional block frequency representation."),
|
|
|
|
clEnumValN(GVDT_Integer, "integer",
|
|
|
|
"display a graph using the raw "
|
|
|
|
"integer fractional block frequency representation."),
|
2016-06-23 03:26:44 +08:00
|
|
|
clEnumValN(GVDT_Count, "count", "display a graph using the real "
|
2016-10-09 03:41:06 +08:00
|
|
|
"profile count if available.")));
|
2013-12-03 08:49:33 +08:00
|
|
|
|
2016-06-28 12:07:03 +08:00
|
|
|
extern cl::opt<std::string> ViewBlockFreqFuncName;
|
2016-06-28 20:34:44 +08:00
|
|
|
extern cl::opt<unsigned> ViewHotFreqPercent;
|
2016-06-22 10:12:54 +08:00
|
|
|
|
2013-12-03 08:49:33 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
2016-06-22 07:36:12 +08:00
|
|
|
template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
|
2016-08-18 04:07:29 +08:00
|
|
|
typedef const MachineBasicBlock *NodeRef;
|
2013-12-03 08:49:33 +08:00
|
|
|
typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
|
2016-08-20 05:20:13 +08:00
|
|
|
typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator;
|
2013-12-03 08:49:33 +08:00
|
|
|
|
2016-09-01 00:48:13 +08:00
|
|
|
static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
|
2015-10-10 03:23:20 +08:00
|
|
|
return &G->getFunction()->front();
|
2013-12-03 08:49:33 +08:00
|
|
|
}
|
2013-12-04 04:21:17 +08:00
|
|
|
|
2016-08-23 05:09:30 +08:00
|
|
|
static ChildIteratorType child_begin(const NodeRef N) {
|
2013-12-03 08:49:33 +08:00
|
|
|
return N->succ_begin();
|
|
|
|
}
|
2013-12-04 04:21:17 +08:00
|
|
|
|
2016-08-23 05:09:30 +08:00
|
|
|
static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
|
2013-12-04 04:21:17 +08:00
|
|
|
|
2013-12-03 08:49:33 +08:00
|
|
|
static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
|
2016-08-20 05:20:13 +08:00
|
|
|
return nodes_iterator(G->getFunction()->begin());
|
2013-12-03 08:49:33 +08:00
|
|
|
}
|
2013-12-04 04:21:17 +08:00
|
|
|
|
2013-12-03 08:49:33 +08:00
|
|
|
static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
|
2016-08-20 05:20:13 +08:00
|
|
|
return nodes_iterator(G->getFunction()->end());
|
2013-12-03 08:49:33 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-28 11:41:29 +08:00
|
|
|
typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
|
|
|
|
MachineBranchProbabilityInfo>
|
|
|
|
MBFIDOTGraphTraitsBase;
|
2016-06-22 07:36:12 +08:00
|
|
|
template <>
|
|
|
|
struct DOTGraphTraits<MachineBlockFrequencyInfo *>
|
2016-06-28 11:41:29 +08:00
|
|
|
: public MBFIDOTGraphTraitsBase {
|
2016-06-22 07:36:12 +08:00
|
|
|
explicit DOTGraphTraits(bool isSimple = false)
|
2016-06-28 11:41:29 +08:00
|
|
|
: MBFIDOTGraphTraitsBase(isSimple) {}
|
2013-12-03 08:49:33 +08:00
|
|
|
|
|
|
|
std::string getNodeLabel(const MachineBasicBlock *Node,
|
|
|
|
const MachineBlockFrequencyInfo *Graph) {
|
2016-06-28 11:41:29 +08:00
|
|
|
return MBFIDOTGraphTraitsBase::getNodeLabel(
|
|
|
|
Node, Graph, ViewMachineBlockFreqPropagationDAG);
|
2013-12-03 08:49:33 +08:00
|
|
|
}
|
2016-06-28 11:41:29 +08:00
|
|
|
|
2016-06-28 14:58:21 +08:00
|
|
|
std::string getNodeAttributes(const MachineBasicBlock *Node,
|
|
|
|
const MachineBlockFrequencyInfo *Graph) {
|
|
|
|
return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
|
|
|
|
ViewHotFreqPercent);
|
|
|
|
}
|
|
|
|
|
2016-06-28 11:41:29 +08:00
|
|
|
std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
|
|
|
|
const MachineBlockFrequencyInfo *MBFI) {
|
2016-06-28 14:58:21 +08:00
|
|
|
return MBFIDOTGraphTraitsBase::getEdgeAttributes(
|
|
|
|
Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
|
2016-06-23 00:04:51 +08:00
|
|
|
}
|
2013-12-03 08:49:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
#endif
|
|
|
|
|
2011-07-26 03:25:40 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
|
2011-07-17 04:23:20 +08:00
|
|
|
"Machine Block Frequency Analysis", true, true)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
|
2014-04-22 01:57:07 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
|
2011-07-26 03:25:40 +08:00
|
|
|
INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
|
2011-07-17 04:23:20 +08:00
|
|
|
"Machine Block Frequency Analysis", true, true)
|
|
|
|
|
2011-07-26 03:25:40 +08:00
|
|
|
char MachineBlockFrequencyInfo::ID = 0;
|
2011-07-17 04:23:20 +08:00
|
|
|
|
2016-06-22 07:36:12 +08:00
|
|
|
MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
|
|
|
|
: MachineFunctionPass(ID) {
|
2011-07-26 03:25:40 +08:00
|
|
|
initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
|
2011-07-17 04:23:20 +08:00
|
|
|
}
|
|
|
|
|
2014-03-26 02:01:38 +08:00
|
|
|
MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
|
2011-07-17 04:23:20 +08:00
|
|
|
|
2011-07-26 03:25:40 +08:00
|
|
|
void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
|
2011-07-17 04:23:20 +08:00
|
|
|
AU.addRequired<MachineBranchProbabilityInfo>();
|
2014-04-22 01:57:07 +08:00
|
|
|
AU.addRequired<MachineLoopInfo>();
|
2011-07-17 04:23:20 +08:00
|
|
|
AU.setPreservesAll();
|
2011-07-22 06:59:09 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
2011-07-17 04:23:20 +08:00
|
|
|
}
|
|
|
|
|
2011-07-26 03:25:40 +08:00
|
|
|
bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
|
2013-12-04 04:21:17 +08:00
|
|
|
MachineBranchProbabilityInfo &MBPI =
|
2014-04-22 01:57:07 +08:00
|
|
|
getAnalysis<MachineBranchProbabilityInfo>();
|
|
|
|
MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
|
2014-03-26 02:01:38 +08:00
|
|
|
if (!MBFI)
|
|
|
|
MBFI.reset(new ImplType);
|
2015-07-16 03:58:26 +08:00
|
|
|
MBFI->calculate(F, MBPI, MLI);
|
2013-12-03 08:49:33 +08:00
|
|
|
#ifndef NDEBUG
|
2016-06-22 10:12:54 +08:00
|
|
|
if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
|
2016-06-28 12:07:03 +08:00
|
|
|
(ViewBlockFreqFuncName.empty() ||
|
|
|
|
F.getName().equals(ViewBlockFreqFuncName))) {
|
2013-12-03 08:49:33 +08:00
|
|
|
view();
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-17 04:23:20 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-26 02:01:38 +08:00
|
|
|
void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
|
|
|
|
|
2013-12-03 08:49:33 +08:00
|
|
|
/// Pop up a ghostview window with the current block frequency propagation
|
|
|
|
/// rendered using dot.
|
|
|
|
void MachineBlockFrequencyInfo::view() const {
|
|
|
|
// This code is only for debugging.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
|
|
|
|
"MachineBlockFrequencyDAGs");
|
|
|
|
#else
|
2013-12-04 04:21:17 +08:00
|
|
|
errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
|
2016-06-22 07:36:12 +08:00
|
|
|
"on systems with Graphviz or gv!\n";
|
2013-12-03 08:49:33 +08:00
|
|
|
#endif // NDEBUG
|
|
|
|
}
|
|
|
|
|
2016-06-22 07:36:12 +08:00
|
|
|
BlockFrequency
|
|
|
|
MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
|
2014-03-26 02:01:38 +08:00
|
|
|
return MBFI ? MBFI->getBlockFreq(MBB) : 0;
|
2011-07-17 04:23:20 +08:00
|
|
|
}
|
2013-12-03 08:49:33 +08:00
|
|
|
|
2016-06-23 03:26:44 +08:00
|
|
|
Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
|
|
|
|
const MachineBasicBlock *MBB) const {
|
|
|
|
const Function *F = MBFI->getFunction()->getFunction();
|
|
|
|
return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
|
|
|
|
}
|
|
|
|
|
2016-08-02 10:15:45 +08:00
|
|
|
Optional<uint64_t>
|
|
|
|
MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
|
|
|
|
const Function *F = MBFI->getFunction()->getFunction();
|
|
|
|
return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None;
|
|
|
|
}
|
|
|
|
|
2014-03-26 02:01:32 +08:00
|
|
|
const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
|
2014-04-22 01:57:07 +08:00
|
|
|
return MBFI ? MBFI->getFunction() : nullptr;
|
2013-12-03 08:49:33 +08:00
|
|
|
}
|
2013-12-14 08:06:03 +08:00
|
|
|
|
2016-06-28 08:15:45 +08:00
|
|
|
const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
|
|
|
|
return MBFI ? &MBFI->getBPI() : nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-14 08:06:03 +08:00
|
|
|
raw_ostream &
|
|
|
|
MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
|
|
|
|
const BlockFrequency Freq) const {
|
2014-03-26 02:01:38 +08:00
|
|
|
return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
|
2013-12-14 08:06:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
raw_ostream &
|
|
|
|
MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
|
|
|
|
const MachineBasicBlock *MBB) const {
|
2014-03-26 02:01:38 +08:00
|
|
|
return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
|
2013-12-14 08:06:03 +08:00
|
|
|
}
|
|
|
|
|
2013-12-14 10:37:38 +08:00
|
|
|
uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
|
2014-03-26 02:01:38 +08:00
|
|
|
return MBFI ? MBFI->getEntryFreq() : 0;
|
2013-12-14 08:06:03 +08:00
|
|
|
}
|