2014-04-11 23:20:58 +00:00
|
|
|
//===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
|
2011-07-16 20:23:20 +00: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-25 19:25:40 +00:00
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
2014-04-11 23:20:58 +00:00
|
|
|
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
|
2011-07-16 20:23:20 +00:00
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
2014-04-21 17:57:07 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/InitializePasses.h"
|
2013-12-03 00:49:33 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2016-06-22 16:04:51 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
2013-12-03 00:49:33 +00:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2016-06-22 16:04:51 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-07-16 20:23:20 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "block-freq"
|
|
|
|
|
2016-06-21 23:36:12 +00: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-22 19:26:44 +00:00
|
|
|
clEnumValN(GVDT_Count, "count", "display a graph using the real "
|
2016-10-08 19:41:06 +00:00
|
|
|
"profile count if available.")));
|
2017-01-29 01:57:02 +00:00
|
|
|
// Similar option above, but used to control BFI display only after MBP pass
|
|
|
|
cl::opt<GVDAGType> ViewBlockLayoutWithBFI(
|
|
|
|
"view-block-layout-with-bfi", cl::Hidden,
|
|
|
|
cl::desc(
|
|
|
|
"Pop up a window to show a dag displaying MBP layout and associated "
|
|
|
|
"block frequencies of 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."),
|
|
|
|
clEnumValN(GVDT_Count, "count",
|
|
|
|
"display a graph using the real "
|
|
|
|
"profile count if available.")));
|
2013-12-03 00:49:33 +00:00
|
|
|
|
2017-02-02 21:29:17 +00:00
|
|
|
// Command line option to specify the name of the function for CFG dump
|
|
|
|
// Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name=
|
2016-06-28 04:07:03 +00:00
|
|
|
extern cl::opt<std::string> ViewBlockFreqFuncName;
|
2017-02-02 21:29:17 +00:00
|
|
|
// Command line option to specify hot frequency threshold.
|
|
|
|
// Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc=
|
2016-06-28 12:34:44 +00:00
|
|
|
extern cl::opt<unsigned> ViewHotFreqPercent;
|
2016-06-22 02:12:54 +00:00
|
|
|
|
2017-01-29 01:57:02 +00:00
|
|
|
static GVDAGType getGVDT() {
|
|
|
|
if (ViewBlockLayoutWithBFI != GVDT_None)
|
|
|
|
return ViewBlockLayoutWithBFI;
|
|
|
|
|
|
|
|
return ViewMachineBlockFreqPropagationDAG;
|
|
|
|
}
|
|
|
|
|
2013-12-03 00:49:33 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2016-06-21 23:36:12 +00:00
|
|
|
template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
|
2016-08-17 20:07:29 +00:00
|
|
|
typedef const MachineBasicBlock *NodeRef;
|
2013-12-03 00:49:33 +00:00
|
|
|
typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
|
2016-08-19 21:20:13 +00:00
|
|
|
typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator;
|
2013-12-03 00:49:33 +00:00
|
|
|
|
2016-08-31 16:48:13 +00:00
|
|
|
static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
|
2015-10-09 19:23:20 +00:00
|
|
|
return &G->getFunction()->front();
|
2013-12-03 00:49:33 +00:00
|
|
|
}
|
2013-12-03 20:21:17 +00:00
|
|
|
|
2016-08-22 21:09:30 +00:00
|
|
|
static ChildIteratorType child_begin(const NodeRef N) {
|
2013-12-03 00:49:33 +00:00
|
|
|
return N->succ_begin();
|
|
|
|
}
|
2013-12-03 20:21:17 +00:00
|
|
|
|
2016-08-22 21:09:30 +00:00
|
|
|
static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
|
2013-12-03 20:21:17 +00:00
|
|
|
|
2013-12-03 00:49:33 +00:00
|
|
|
static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
|
2016-08-19 21:20:13 +00:00
|
|
|
return nodes_iterator(G->getFunction()->begin());
|
2013-12-03 00:49:33 +00:00
|
|
|
}
|
2013-12-03 20:21:17 +00:00
|
|
|
|
2013-12-03 00:49:33 +00:00
|
|
|
static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
|
2016-08-19 21:20:13 +00:00
|
|
|
return nodes_iterator(G->getFunction()->end());
|
2013-12-03 00:49:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-28 03:41:29 +00:00
|
|
|
typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
|
|
|
|
MachineBranchProbabilityInfo>
|
|
|
|
MBFIDOTGraphTraitsBase;
|
2016-06-21 23:36:12 +00:00
|
|
|
template <>
|
|
|
|
struct DOTGraphTraits<MachineBlockFrequencyInfo *>
|
2016-06-28 03:41:29 +00:00
|
|
|
: public MBFIDOTGraphTraitsBase {
|
2016-06-21 23:36:12 +00:00
|
|
|
explicit DOTGraphTraits(bool isSimple = false)
|
2017-01-29 01:57:02 +00:00
|
|
|
: MBFIDOTGraphTraitsBase(isSimple), CurFunc(nullptr), LayoutOrderMap() {}
|
|
|
|
|
|
|
|
const MachineFunction *CurFunc;
|
|
|
|
DenseMap<const MachineBasicBlock *, int> LayoutOrderMap;
|
2013-12-03 00:49:33 +00:00
|
|
|
|
|
|
|
std::string getNodeLabel(const MachineBasicBlock *Node,
|
|
|
|
const MachineBlockFrequencyInfo *Graph) {
|
2017-01-29 01:57:02 +00:00
|
|
|
|
|
|
|
int layout_order = -1;
|
|
|
|
// Attach additional ordering information if 'isSimple' is false.
|
|
|
|
if (!isSimple()) {
|
|
|
|
const MachineFunction *F = Node->getParent();
|
|
|
|
if (!CurFunc || F != CurFunc) {
|
|
|
|
if (CurFunc)
|
|
|
|
LayoutOrderMap.clear();
|
|
|
|
|
|
|
|
CurFunc = F;
|
|
|
|
int O = 0;
|
|
|
|
for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {
|
|
|
|
LayoutOrderMap[&*MBI] = O;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
layout_order = LayoutOrderMap[Node];
|
|
|
|
}
|
|
|
|
return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(),
|
|
|
|
layout_order);
|
2013-12-03 00:49:33 +00:00
|
|
|
}
|
2016-06-28 03:41:29 +00:00
|
|
|
|
2016-06-28 06:58:21 +00:00
|
|
|
std::string getNodeAttributes(const MachineBasicBlock *Node,
|
|
|
|
const MachineBlockFrequencyInfo *Graph) {
|
|
|
|
return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
|
|
|
|
ViewHotFreqPercent);
|
|
|
|
}
|
|
|
|
|
2016-06-28 03:41:29 +00:00
|
|
|
std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
|
|
|
|
const MachineBlockFrequencyInfo *MBFI) {
|
2016-06-28 06:58:21 +00:00
|
|
|
return MBFIDOTGraphTraitsBase::getEdgeAttributes(
|
|
|
|
Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
|
2016-06-22 16:04:51 +00:00
|
|
|
}
|
2013-12-03 00:49:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2011-07-25 19:25:40 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
|
2011-07-16 20:23:20 +00:00
|
|
|
"Machine Block Frequency Analysis", true, true)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
|
2014-04-21 17:57:07 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
|
2011-07-25 19:25:40 +00:00
|
|
|
INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
|
2011-07-16 20:23:20 +00:00
|
|
|
"Machine Block Frequency Analysis", true, true)
|
|
|
|
|
2011-07-25 19:25:40 +00:00
|
|
|
char MachineBlockFrequencyInfo::ID = 0;
|
2011-07-16 20:23:20 +00:00
|
|
|
|
2016-06-21 23:36:12 +00:00
|
|
|
MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
|
|
|
|
: MachineFunctionPass(ID) {
|
2011-07-25 19:25:40 +00:00
|
|
|
initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
|
2011-07-16 20:23:20 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 18:01:38 +00:00
|
|
|
MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
|
2011-07-16 20:23:20 +00:00
|
|
|
|
2011-07-25 19:25:40 +00:00
|
|
|
void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
|
2011-07-16 20:23:20 +00:00
|
|
|
AU.addRequired<MachineBranchProbabilityInfo>();
|
2014-04-21 17:57:07 +00:00
|
|
|
AU.addRequired<MachineLoopInfo>();
|
2011-07-16 20:23:20 +00:00
|
|
|
AU.setPreservesAll();
|
2011-07-21 22:59:09 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
2011-07-16 20:23:20 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 17:21:09 +00:00
|
|
|
void MachineBlockFrequencyInfo::calculate(
|
|
|
|
const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
|
|
|
|
const MachineLoopInfo &MLI) {
|
2014-03-25 18:01:38 +00:00
|
|
|
if (!MBFI)
|
|
|
|
MBFI.reset(new ImplType);
|
2015-07-15 19:58:26 +00:00
|
|
|
MBFI->calculate(F, MBPI, MLI);
|
2016-06-22 02:12:54 +00:00
|
|
|
if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
|
2016-06-28 04:07:03 +00:00
|
|
|
(ViewBlockFreqFuncName.empty() ||
|
|
|
|
F.getName().equals(ViewBlockFreqFuncName))) {
|
2017-02-15 19:21:04 +00:00
|
|
|
view("MachineBlockFrequencyDAGS." + F.getName());
|
2013-12-03 00:49:33 +00:00
|
|
|
}
|
2017-02-14 17:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
|
|
|
|
MachineBranchProbabilityInfo &MBPI =
|
|
|
|
getAnalysis<MachineBranchProbabilityInfo>();
|
|
|
|
MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
|
|
|
|
calculate(F, MBPI, MLI);
|
2011-07-16 20:23:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-25 18:01:38 +00:00
|
|
|
void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
|
|
|
|
|
2013-12-03 00:49:33 +00:00
|
|
|
/// Pop up a ghostview window with the current block frequency propagation
|
|
|
|
/// rendered using dot.
|
2017-02-15 19:21:04 +00:00
|
|
|
void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const {
|
|
|
|
// This code is only for debugging.
|
|
|
|
ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple);
|
2013-12-03 00:49:33 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 23:36:12 +00:00
|
|
|
BlockFrequency
|
|
|
|
MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
|
2014-03-25 18:01:38 +00:00
|
|
|
return MBFI ? MBFI->getBlockFreq(MBB) : 0;
|
2011-07-16 20:23:20 +00:00
|
|
|
}
|
2013-12-03 00:49:33 +00:00
|
|
|
|
2016-06-22 19:26:44 +00: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 02:15:45 +00: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-25 18:01:32 +00:00
|
|
|
const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
|
2014-04-21 17:57:07 +00:00
|
|
|
return MBFI ? MBFI->getFunction() : nullptr;
|
2013-12-03 00:49:33 +00:00
|
|
|
}
|
2013-12-14 00:06:03 +00:00
|
|
|
|
2016-06-28 00:15:45 +00:00
|
|
|
const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
|
|
|
|
return MBFI ? &MBFI->getBPI() : nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-14 00:06:03 +00:00
|
|
|
raw_ostream &
|
|
|
|
MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
|
|
|
|
const BlockFrequency Freq) const {
|
2014-03-25 18:01:38 +00:00
|
|
|
return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
|
2013-12-14 00:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
raw_ostream &
|
|
|
|
MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
|
|
|
|
const MachineBasicBlock *MBB) const {
|
2014-03-25 18:01:38 +00:00
|
|
|
return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
|
2013-12-14 00:06:03 +00:00
|
|
|
}
|
|
|
|
|
2013-12-14 02:37:38 +00:00
|
|
|
uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
|
2014-03-25 18:01:38 +00:00
|
|
|
return MBFI ? MBFI->getEntryFreq() : 0;
|
2013-12-14 00:06:03 +00:00
|
|
|
}
|