Skip to content

Commit

Permalink
[LazyBFI] Split out and templatize LazyBlockFrequencyInfo, NFC
Browse files Browse the repository at this point in the history
This will be used by the LazyMachineBFI pass.

Differential Revision: https://reviews.llvm.org/D29834

llvm-svn: 295071
  • Loading branch information
anemet committed Feb 14, 2017
1 parent a109dd1 commit 24984e1
Showing 1 changed file with 52 additions and 47 deletions.
99 changes: 52 additions & 47 deletions llvm/include/llvm/Analysis/LazyBlockFrequencyInfo.h
Expand Up @@ -27,6 +27,53 @@ class BranchProbabilityInfo;
class Function;
class LoopInfo;

/// Wraps a BFI to allow lazy computation of the block frequencies.
///
/// A pass that only conditionally uses BFI can uncondtionally require the
/// analysis without paying for the overhead if BFI doesn't end up being used.
template <typename FunctionT, typename BranchProbabilityInfoPassT,
typename LoopInfoT, typename BlockFrequencyInfoT>
class LazyBlockFrequencyInfo {
public:
LazyBlockFrequencyInfo()
: Calculated(false), F(nullptr), BPIPass(nullptr), LI(nullptr) {}

/// Set up the per-function input.
void setAnalysis(const FunctionT *F, BranchProbabilityInfoPassT *BPIPass,
const LoopInfoT *LI) {
this->F = F;
this->BPIPass = BPIPass;
this->LI = LI;
}

/// Retrieve the BFI with the block frequencies computed.
BlockFrequencyInfoT &getCalculated() {
if (!Calculated) {
assert(F && BPIPass && LI && "call setAnalysis");
BFI.calculate(*F, BPIPass->getBPI(), *LI);
Calculated = true;
}
return BFI;
}

const BlockFrequencyInfoT &getCalculated() const {
return const_cast<LazyBlockFrequencyInfo *>(this)->getCalculated();
}

void releaseMemory() {
BFI.releaseMemory();
Calculated = false;
setAnalysis(nullptr, nullptr, nullptr);
}

private:
BlockFrequencyInfoT BFI;
bool Calculated;
const FunctionT *F;
BranchProbabilityInfoPassT *BPIPass;
const LoopInfoT *LI;
};

/// \brief This is an alternative analysis pass to
/// BlockFrequencyInfoWrapperPass. The difference is that with this pass the
/// block frequencies are not computed when the analysis pass is executed but
Expand All @@ -49,54 +96,12 @@ class LoopInfo;
///
/// Note that it is expected that we wouldn't need this functionality for the
/// new PM since with the new PM, analyses are executed on demand.
class LazyBlockFrequencyInfoPass : public FunctionPass {

/// Wraps a BFI to allow lazy computation of the block frequencies.
///
/// A pass that only conditionally uses BFI can uncondtionally require the
/// analysis without paying for the overhead if BFI doesn't end up being used.
class LazyBlockFrequencyInfo {
public:
LazyBlockFrequencyInfo()
: Calculated(false), F(nullptr), BPIPass(nullptr), LI(nullptr) {}

/// Set up the per-function input.
void setAnalysis(const Function *F, LazyBranchProbabilityInfoPass *BPIPass,
const LoopInfo *LI) {
this->F = F;
this->BPIPass = BPIPass;
this->LI = LI;
}

/// Retrieve the BFI with the block frequencies computed.
BlockFrequencyInfo &getCalculated() {
if (!Calculated) {
assert(F && BPIPass && LI && "call setAnalysis");
BFI.calculate(*F, BPIPass->getBPI(), *LI);
Calculated = true;
}
return BFI;
}

const BlockFrequencyInfo &getCalculated() const {
return const_cast<LazyBlockFrequencyInfo *>(this)->getCalculated();
}

void releaseMemory() {
BFI.releaseMemory();
Calculated = false;
setAnalysis(nullptr, nullptr, nullptr);
}

private:
BlockFrequencyInfo BFI;
bool Calculated;
const Function *F;
LazyBranchProbabilityInfoPass *BPIPass;
const LoopInfo *LI;
};

LazyBlockFrequencyInfo LBFI;
class LazyBlockFrequencyInfoPass : public FunctionPass {
private:
LazyBlockFrequencyInfo<Function, LazyBranchProbabilityInfoPass, LoopInfo,
BlockFrequencyInfo>
LBFI;

public:
static char ID;
Expand Down

0 comments on commit 24984e1

Please sign in to comment.