diff --git a/llvm/include/llvm/Analysis/BlockFrequencyInfo.h b/llvm/include/llvm/Analysis/BlockFrequencyInfo.h index 66eabb4b8525..2f701d9064d6 100644 --- a/llvm/include/llvm/Analysis/BlockFrequencyInfo.h +++ b/llvm/include/llvm/Analysis/BlockFrequencyInfo.h @@ -27,8 +27,9 @@ class BlockFrequencyImpl; /// BlockFrequencyInfo pass uses BlockFrequencyImpl implementation to estimate /// IR basic block frequencies. class BlockFrequencyInfo : public FunctionPass { - - BlockFrequencyImpl *BFI; + typedef BlockFrequencyImpl + ImplType; + std::unique_ptr 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; diff --git a/llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h b/llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h index 0539e35252da..f3ef87cbbc47 100644 --- a/llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h +++ b/llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h @@ -28,9 +28,9 @@ class BlockFrequencyImpl; /// MachineBlockFrequencyInfo pass uses BlockFrequencyImpl implementation to estimate /// machine basic block frequencies. class MachineBlockFrequencyInfo : public MachineFunctionPass { - - BlockFrequencyImpl *MBFI; + typedef BlockFrequencyImpl ImplType; + std::unique_ptr 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 diff --git a/llvm/lib/Analysis/BlockFrequencyInfo.cpp b/llvm/lib/Analysis/BlockFrequencyInfo.cpp index e52a0f8a8ee6..63049a560198 100644 --- a/llvm/lib/Analysis/BlockFrequencyInfo.cpp +++ b/llvm/lib/Analysis/BlockFrequencyInfo.cpp @@ -114,12 +114,9 @@ char BlockFrequencyInfo::ID = 0; BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) { initializeBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); - BFI = new BlockFrequencyImpl(); } -BlockFrequencyInfo::~BlockFrequencyInfo() { - delete BFI; -} +BlockFrequencyInfo::~BlockFrequencyInfo() {} void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { AU.addRequired(); @@ -128,6 +125,8 @@ void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { bool BlockFrequencyInfo::runOnFunction(Function &F) { BranchProbabilityInfo &BPI = getAnalysis(); + 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; } diff --git a/llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp b/llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp index efb066483390..13203d55c467 100644 --- a/llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp +++ b/llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp @@ -121,13 +121,9 @@ char MachineBlockFrequencyInfo::ID = 0; MachineBlockFrequencyInfo:: MachineBlockFrequencyInfo() :MachineFunctionPass(ID) { initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); - MBFI = new BlockFrequencyImpl(); } -MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() { - delete MBFI; -} +MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {} void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { AU.addRequired(); @@ -138,6 +134,8 @@ void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { MachineBranchProbabilityInfo &MBPI = getAnalysis(); + 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; }