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;
|
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
#define DEBUG_TYPE "machine-block-freq"
|
2014-04-22 10:02:50 +08:00
|
|
|
|
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.")));
|
2017-01-29 09:57:02 +08: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 08:49:33 +08:00
|
|
|
|
2017-02-03 05:29:17 +08: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 12:07:03 +08:00
|
|
|
extern cl::opt<std::string> ViewBlockFreqFuncName;
|
2017-02-03 05:29:17 +08:00
|
|
|
// Command line option to specify hot frequency threshold.
|
|
|
|
// Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc=
|
2016-06-28 20:34:44 +08:00
|
|
|
extern cl::opt<unsigned> ViewHotFreqPercent;
|
2016-06-22 10:12:54 +08:00
|
|
|
|
2017-01-29 09:57:02 +08:00
|
|
|
static GVDAGType getGVDT() {
|
|
|
|
if (ViewBlockLayoutWithBFI != GVDT_None)
|
|
|
|
return ViewBlockLayoutWithBFI;
|
|
|
|
|
|
|
|
return ViewMachineBlockFreqPropagationDAG;
|
|
|
|
}
|
|
|
|
|
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)
|
2017-01-29 09:57:02 +08:00
|
|
|
: MBFIDOTGraphTraitsBase(isSimple), CurFunc(nullptr), LayoutOrderMap() {}
|
|
|
|
|
|
|
|
const MachineFunction *CurFunc;
|
|
|
|
DenseMap<const MachineBasicBlock *, int> LayoutOrderMap;
|
2013-12-03 08:49:33 +08:00
|
|
|
|
|
|
|
std::string getNodeLabel(const MachineBasicBlock *Node,
|
|
|
|
const MachineBlockFrequencyInfo *Graph) {
|
2017-01-29 09:57:02 +08: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 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
|
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE,
|
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)
|
2017-05-26 05:26:32 +08:00
|
|
|
INITIALIZE_PASS_END(MachineBlockFrequencyInfo, DEBUG_TYPE,
|
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
|
|
|
}
|
|
|
|
|
2017-02-15 01:21:09 +08:00
|
|
|
void MachineBlockFrequencyInfo::calculate(
|
|
|
|
const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
|
|
|
|
const MachineLoopInfo &MLI) {
|
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);
|
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))) {
|
2017-02-16 03:21:04 +08:00
|
|
|
view("MachineBlockFrequencyDAGS." + F.getName());
|
2013-12-03 08:49:33 +08:00
|
|
|
}
|
2017-02-15 01:21:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
|
|
|
|
MachineBranchProbabilityInfo &MBPI =
|
|
|
|
getAnalysis<MachineBranchProbabilityInfo>();
|
|
|
|
MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
|
|
|
|
calculate(F, MBPI, MLI);
|
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.
|
2017-02-16 03:21:04 +08: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 08:49:33 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|