Skip to content

Commit

Permalink
blockfreq: Implement Pass::releaseMemory()
Browse files Browse the repository at this point in the history
Implement Pass::releaseMemory() in BlockFrequencyInfo and
MachineBlockFrequencyInfo.  Just delete the private implementation when
not in use.  Switch to a std::unique_ptr to make the logic more clear.

<rdar://problem/14292693>

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204741 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
dexonsmith committed Mar 25, 2014
1 parent 8451e1b commit 27e1ca8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
6 changes: 4 additions & 2 deletions include/llvm/Analysis/BlockFrequencyInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class BlockFrequencyImpl;
/// BlockFrequencyInfo pass uses BlockFrequencyImpl implementation to estimate
/// IR basic block frequencies.
class BlockFrequencyInfo : public FunctionPass {

BlockFrequencyImpl<BasicBlock, Function, BranchProbabilityInfo> *BFI;
typedef BlockFrequencyImpl<BasicBlock, Function, BranchProbabilityInfo>
ImplType;
std::unique_ptr<ImplType> BFI;

public:
static char ID;
Expand All @@ -40,6 +41,7 @@ class BlockFrequencyInfo : public FunctionPass {
void getAnalysisUsage(AnalysisUsage &AU) const override;

bool runOnFunction(Function &F) override;
void releaseMemory() override;
void print(raw_ostream &O, const Module *M) const override;
const Function *getFunction() const;
void view() const;
Expand Down
8 changes: 5 additions & 3 deletions include/llvm/CodeGen/MachineBlockFrequencyInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class BlockFrequencyImpl;
/// MachineBlockFrequencyInfo pass uses BlockFrequencyImpl implementation to estimate
/// machine basic block frequencies.
class MachineBlockFrequencyInfo : public MachineFunctionPass {

BlockFrequencyImpl<MachineBasicBlock, MachineFunction,
MachineBranchProbabilityInfo> *MBFI;
typedef BlockFrequencyImpl<MachineBasicBlock, MachineFunction,
MachineBranchProbabilityInfo> ImplType;
std::unique_ptr<ImplType> MBFI;

public:
static char ID;
Expand All @@ -43,6 +43,8 @@ class MachineBlockFrequencyInfo : public MachineFunctionPass {

bool runOnMachineFunction(MachineFunction &F) override;

void releaseMemory() override;

/// getblockFreq - Return block frequency. Return 0 if we don't have the
/// information. Please note that initial frequency is equal to 1024. It means
/// that we should not rely on the value itself, but only on the comparison to
Expand Down
19 changes: 10 additions & 9 deletions lib/Analysis/BlockFrequencyInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,9 @@ char BlockFrequencyInfo::ID = 0;

BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) {
initializeBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
BFI = new BlockFrequencyImpl<BasicBlock, Function, BranchProbabilityInfo>();
}

BlockFrequencyInfo::~BlockFrequencyInfo() {
delete BFI;
}
BlockFrequencyInfo::~BlockFrequencyInfo() {}

void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<BranchProbabilityInfo>();
Expand All @@ -128,6 +125,8 @@ void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {

bool BlockFrequencyInfo::runOnFunction(Function &F) {
BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
if (!BFI)
BFI.reset(new ImplType);
BFI->doFunction(&F, &BPI);
#ifndef NDEBUG
if (ViewBlockFreqPropagationDAG != GVDT_None)
Expand All @@ -136,12 +135,14 @@ bool BlockFrequencyInfo::runOnFunction(Function &F) {
return false;
}

void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }

void BlockFrequencyInfo::print(raw_ostream &O, const Module *) const {
if (BFI) BFI->print(O);
}

BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
return BFI->getBlockFreq(BB);
return BFI ? BFI->getBlockFreq(BB) : 0;
}

/// Pop up a ghostview window with the current block frequency propagation
Expand All @@ -157,20 +158,20 @@ void BlockFrequencyInfo::view() const {
}

const Function *BlockFrequencyInfo::getFunction() const {
return BFI->Fn;
return BFI ? BFI->Fn : nullptr;
}

raw_ostream &BlockFrequencyInfo::
printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
return BFI->printBlockFreq(OS, Freq);
return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
}

raw_ostream &
BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
const BasicBlock *BB) const {
return BFI->printBlockFreq(OS, BB);
return BFI ? BFI->printBlockFreq(OS, BB) : OS;
}

uint64_t BlockFrequencyInfo::getEntryFreq() const {
return BFI->getEntryFreq();
return BFI ? BFI->getEntryFreq() : 0;
}
20 changes: 10 additions & 10 deletions lib/CodeGen/MachineBlockFrequencyInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,9 @@ char MachineBlockFrequencyInfo::ID = 0;
MachineBlockFrequencyInfo::
MachineBlockFrequencyInfo() :MachineFunctionPass(ID) {
initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
MBFI = new BlockFrequencyImpl<MachineBasicBlock, MachineFunction,
MachineBranchProbabilityInfo>();
}

MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {
delete MBFI;
}
MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}

void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<MachineBranchProbabilityInfo>();
Expand All @@ -138,6 +134,8 @@ void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
MachineBranchProbabilityInfo &MBPI =
getAnalysis<MachineBranchProbabilityInfo>();
if (!MBFI)
MBFI.reset(new ImplType);
MBFI->doFunction(&F, &MBPI);
#ifndef NDEBUG
if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
Expand All @@ -147,6 +145,8 @@ bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
return false;
}

void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }

/// Pop up a ghostview window with the current block frequency propagation
/// rendered using dot.
void MachineBlockFrequencyInfo::view() const {
Expand All @@ -162,25 +162,25 @@ void MachineBlockFrequencyInfo::view() const {

BlockFrequency MachineBlockFrequencyInfo::
getBlockFreq(const MachineBasicBlock *MBB) const {
return MBFI->getBlockFreq(MBB);
return MBFI ? MBFI->getBlockFreq(MBB) : 0;
}

const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
return MBFI->Fn;
return MBFI ? MBFI->Fn : nullptr;
}

raw_ostream &
MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
const BlockFrequency Freq) const {
return MBFI->printBlockFreq(OS, Freq);
return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
}

raw_ostream &
MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
const MachineBasicBlock *MBB) const {
return MBFI->printBlockFreq(OS, MBB);
return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
}

uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
return MBFI->getEntryFreq();
return MBFI ? MBFI->getEntryFreq() : 0;
}

0 comments on commit 27e1ca8

Please sign in to comment.