blockfreq: Implement Pass::releaseMemory()
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Tue, 25 Mar 2014 18:01:38 +0000 (18:01 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Tue, 25 Mar 2014 18:01:38 +0000 (18:01 +0000)
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

include/llvm/Analysis/BlockFrequencyInfo.h
include/llvm/CodeGen/MachineBlockFrequencyInfo.h
lib/Analysis/BlockFrequencyInfo.cpp
lib/CodeGen/MachineBlockFrequencyInfo.cpp

index 66eabb4b8525790f70bb1fb274c5876f7fa5c548..2f701d9064d669885ca1e87654c7fd12ebdc6ab1 100644 (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;
index 0539e35252da973e70757978fcb54a5485546a89..f3ef87cbbc473e0747f54a4209c76e8c9238d408 100644 (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
index e52a0f8a8ee6be499cd17511805670ed57c1e70a..63049a56019846c5246b3ca0d74b2f64b09af953 100644 (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;
 }
index efb0664833901dfc591f503c6bc4c114ca5b0c82..13203d55c467931c325c4544e3d7a28332ead8ee 100644 (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;
 }