blockfreq: Implement Pass::releaseMemory()

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>

llvm-svn: 204741
This commit is contained in:
Duncan P. N. Exon Smith 2014-03-25 18:01:38 +00:00
parent 936aef9238
commit 3dbe10503a
4 changed files with 29 additions and 24 deletions

View File

@ -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;
@ -40,6 +41,7 @@ public:
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;

View File

@ -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;
@ -43,6 +43,8 @@ public:
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

View File

@ -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>();
@ -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)
@ -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
@ -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;
}

View File

@ -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>();
@ -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) {
@ -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 {
@ -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;
}