2014-04-12 07:20:58 +08:00
|
|
|
//===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
|
2011-07-17 04:23:20 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-07-17 04:23:20 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Loops should be simplified before this analysis.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-26 03:25:40 +08:00
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
2017-08-25 05:21:39 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/iterator.h"
|
2014-04-12 07:20:58 +08:00
|
|
|
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
|
2017-08-25 05:21:39 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.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"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2017-08-25 05:21:39 +08:00
|
|
|
#include "llvm/Pass.h"
|
2013-12-03 08:49:33 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2017-08-25 05:21:39 +08:00
|
|
|
#include <string>
|
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-08-25 05:21:39 +08:00
|
|
|
|
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-08-25 05:21:39 +08:00
|
|
|
|
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-08-26 08:31:00 +08:00
|
|
|
static cl::opt<bool> PrintMachineBlockFreq(
|
|
|
|
"print-machine-bfi", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Print the machine block frequency info."));
|
|
|
|
|
|
|
|
// Command line option to specify the name of the function for block frequency
|
|
|
|
// dump. Defined in Analysis/BlockFrequencyInfo.cpp.
|
|
|
|
extern cl::opt<std::string> PrintBlockFreqFuncName;
|
|
|
|
|
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 *> {
|
2017-08-25 05:21:39 +08:00
|
|
|
using NodeRef = const MachineBasicBlock *;
|
|
|
|
using ChildIteratorType = MachineBasicBlock::const_succ_iterator;
|
|
|
|
using nodes_iterator = pointer_iterator<MachineFunction::const_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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-25 05:21:39 +08:00
|
|
|
using MBFIDOTGraphTraitsBase =
|
|
|
|
BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
|
|
|
|
MachineBranchProbabilityInfo>;
|
|
|
|
|
2016-06-22 07:36:12 +08:00
|
|
|
template <>
|
|
|
|
struct DOTGraphTraits<MachineBlockFrequencyInfo *>
|
2016-06-28 11:41:29 +08:00
|
|
|
: public MBFIDOTGraphTraitsBase {
|
2017-08-25 05:21:39 +08:00
|
|
|
const MachineFunction *CurFunc = nullptr;
|
2017-01-29 09:57:02 +08:00
|
|
|
DenseMap<const MachineBasicBlock *, int> LayoutOrderMap;
|
2013-12-03 08:49:33 +08:00
|
|
|
|
2017-08-25 05:21:39 +08:00
|
|
|
explicit DOTGraphTraits(bool isSimple = false)
|
|
|
|
: MBFIDOTGraphTraitsBase(isSimple) {}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-29 03:35:34 +08:00
|
|
|
MachineBlockFrequencyInfo::MachineBlockFrequencyInfo(
|
|
|
|
MachineFunction &F,
|
|
|
|
MachineBranchProbabilityInfo &MBPI,
|
|
|
|
MachineLoopInfo &MLI) : MachineFunctionPass(ID) {
|
|
|
|
calculate(F, MBPI, MLI);
|
|
|
|
}
|
|
|
|
|
2017-08-25 05:21:39 +08:00
|
|
|
MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default;
|
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-08-26 08:31:00 +08:00
|
|
|
if (PrintMachineBlockFreq &&
|
|
|
|
(PrintBlockFreqFuncName.empty() ||
|
|
|
|
F.getName().equals(PrintBlockFreqFuncName))) {
|
|
|
|
MBFI->print(dbgs());
|
|
|
|
}
|
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 {
|
2017-12-16 06:22:58 +08:00
|
|
|
const Function &F = MBFI->getFunction()->getFunction();
|
|
|
|
return MBFI ? MBFI->getBlockProfileCount(F, MBB) : None;
|
2016-06-23 03:26:44 +08:00
|
|
|
}
|
|
|
|
|
2016-08-02 10:15:45 +08:00
|
|
|
Optional<uint64_t>
|
|
|
|
MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
|
2017-12-16 06:22:58 +08:00
|
|
|
const Function &F = MBFI->getFunction()->getFunction();
|
|
|
|
return MBFI ? MBFI->getProfileCountFromFreq(F, Freq) : None;
|
2016-08-02 10:15:45 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 06:26:51 +08:00
|
|
|
bool
|
|
|
|
MachineBlockFrequencyInfo::isIrrLoopHeader(const MachineBasicBlock *MBB) {
|
|
|
|
assert(MBFI && "Expected analysis to be available");
|
|
|
|
return MBFI->isIrrLoopHeader(MBB);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|