2014-04-12 01:20:58 +02:00
|
|
|
//===- BlockFrequencyInfo.h - Block Frequency Analysis ----------*- C++ -*-===//
|
2011-06-23 23:56:59 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Loops should be simplified before this analysis.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-25 21:25:40 +02:00
|
|
|
#ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
|
|
|
|
#define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
|
2011-06-23 23:56:59 +02:00
|
|
|
|
2016-03-23 19:18:26 +01:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2016-05-05 23:13:27 +02:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2011-06-23 23:56:59 +02:00
|
|
|
#include "llvm/Pass.h"
|
2011-07-28 00:05:51 +02:00
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
2011-06-23 23:56:59 +02:00
|
|
|
#include <climits>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class BranchProbabilityInfo;
|
2015-07-15 01:40:50 +02:00
|
|
|
class LoopInfo;
|
2014-04-12 01:21:02 +02:00
|
|
|
template <class BlockT> class BlockFrequencyInfoImpl;
|
2011-06-23 23:56:59 +02:00
|
|
|
|
2014-04-12 01:20:58 +02:00
|
|
|
/// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
|
|
|
|
/// estimate IR basic block frequencies.
|
2015-07-15 01:40:50 +02:00
|
|
|
class BlockFrequencyInfo {
|
2014-04-12 01:21:02 +02:00
|
|
|
typedef BlockFrequencyInfoImpl<BasicBlock> ImplType;
|
2014-03-25 19:01:38 +01:00
|
|
|
std::unique_ptr<ImplType> BFI;
|
2011-06-23 23:56:59 +02:00
|
|
|
|
2016-05-05 23:13:27 +02:00
|
|
|
void operator=(const BlockFrequencyInfo &) = delete;
|
|
|
|
BlockFrequencyInfo(const BlockFrequencyInfo &) = delete;
|
|
|
|
|
2011-06-23 23:56:59 +02:00
|
|
|
public:
|
2015-07-17 01:23:35 +02:00
|
|
|
BlockFrequencyInfo();
|
|
|
|
BlockFrequencyInfo(const Function &F, const BranchProbabilityInfo &BPI,
|
|
|
|
const LoopInfo &LI);
|
2016-05-05 23:13:27 +02:00
|
|
|
BlockFrequencyInfo(BlockFrequencyInfo &&Arg);
|
|
|
|
|
|
|
|
BlockFrequencyInfo &operator=(BlockFrequencyInfo &&RHS);
|
2015-07-17 01:23:35 +02:00
|
|
|
|
2016-07-13 07:01:48 +02:00
|
|
|
~BlockFrequencyInfo();
|
|
|
|
|
2013-11-14 03:27:46 +01:00
|
|
|
const Function *getFunction() const;
|
2016-06-28 05:41:29 +02:00
|
|
|
const BranchProbabilityInfo *getBPI() const;
|
2013-11-14 03:27:46 +01:00
|
|
|
void view() const;
|
2011-06-23 23:56:59 +02:00
|
|
|
|
2011-07-22 04:24:57 +02:00
|
|
|
/// getblockFreq - Return block frequency. Return 0 if we don't have the
|
2013-06-25 15:34:40 +02:00
|
|
|
/// information. Please note that initial frequency is equal to ENTRY_FREQ. It
|
|
|
|
/// means that we should not rely on the value itself, but only on the
|
|
|
|
/// comparison to the other block frequencies. We do this to avoid using of
|
|
|
|
/// floating points.
|
2011-12-20 21:03:10 +01:00
|
|
|
BlockFrequency getBlockFreq(const BasicBlock *BB) const;
|
2013-12-14 01:06:03 +01:00
|
|
|
|
2016-03-23 19:18:26 +01:00
|
|
|
/// \brief Returns the estimated profile count of \p BB.
|
|
|
|
/// This computes the relative block frequency of \p BB and multiplies it by
|
|
|
|
/// the enclosing function's count (if available) and returns the value.
|
|
|
|
Optional<uint64_t> getBlockProfileCount(const BasicBlock *BB) const;
|
|
|
|
|
2016-08-02 04:15:45 +02:00
|
|
|
/// \brief Returns the estimated profile count of \p Freq.
|
|
|
|
/// This uses the frequency \p Freq and multiplies it by
|
|
|
|
/// the enclosing function's count (if available) and returns the value.
|
|
|
|
Optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
|
|
|
|
|
2015-10-15 16:59:40 +02:00
|
|
|
// Set the frequency of the given basic block.
|
|
|
|
void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
|
|
|
|
|
2015-07-15 01:40:50 +02:00
|
|
|
/// calculate - compute block frequency info for the given function.
|
|
|
|
void calculate(const Function &F, const BranchProbabilityInfo &BPI,
|
|
|
|
const LoopInfo &LI);
|
|
|
|
|
2013-12-14 01:06:03 +01:00
|
|
|
// Print the block frequency Freq to OS using the current functions entry
|
|
|
|
// frequency to convert freq into a relative decimal form.
|
|
|
|
raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
|
|
|
|
|
|
|
|
// Convenience method that attempts to look up the frequency associated with
|
|
|
|
// BB and print it to OS.
|
|
|
|
raw_ostream &printBlockFreq(raw_ostream &OS, const BasicBlock *BB) const;
|
|
|
|
|
2013-12-20 23:11:11 +01:00
|
|
|
uint64_t getEntryFreq() const;
|
2015-07-15 01:40:50 +02:00
|
|
|
void releaseMemory();
|
|
|
|
void print(raw_ostream &OS) const;
|
|
|
|
};
|
2013-12-20 23:11:11 +01:00
|
|
|
|
2016-05-05 23:13:27 +02:00
|
|
|
/// \brief Analysis pass which computes \c BlockFrequencyInfo.
|
|
|
|
class BlockFrequencyAnalysis
|
|
|
|
: public AnalysisInfoMixin<BlockFrequencyAnalysis> {
|
|
|
|
friend AnalysisInfoMixin<BlockFrequencyAnalysis>;
|
2016-11-23 18:53:26 +01:00
|
|
|
static AnalysisKey Key;
|
2016-05-05 23:13:27 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// \brief Provide the result typedef for this analysis pass.
|
|
|
|
typedef BlockFrequencyInfo Result;
|
|
|
|
|
2016-07-08 23:24:13 +02:00
|
|
|
/// \brief Run the analysis pass over a function and produce BFI.
|
2016-08-09 02:28:15 +02:00
|
|
|
Result run(Function &F, FunctionAnalysisManager &AM);
|
2016-05-05 23:13:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Printer pass for the \c BlockFrequencyInfo results.
|
|
|
|
class BlockFrequencyPrinterPass
|
|
|
|
: public PassInfoMixin<BlockFrequencyPrinterPass> {
|
|
|
|
raw_ostream &OS;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
|
2016-08-09 02:28:15 +02:00
|
|
|
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
|
2016-05-05 23:13:27 +02:00
|
|
|
};
|
|
|
|
|
2015-07-15 01:40:50 +02:00
|
|
|
/// \brief Legacy analysis pass which computes \c BlockFrequencyInfo.
|
|
|
|
class BlockFrequencyInfoWrapperPass : public FunctionPass {
|
|
|
|
BlockFrequencyInfo BFI;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
BlockFrequencyInfoWrapperPass();
|
|
|
|
~BlockFrequencyInfoWrapperPass() override;
|
|
|
|
|
|
|
|
BlockFrequencyInfo &getBFI() { return BFI; }
|
|
|
|
const BlockFrequencyInfo &getBFI() const { return BFI; }
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
void releaseMemory() override;
|
|
|
|
void print(raw_ostream &OS, const Module *M) const override;
|
2011-06-23 23:56:59 +02:00
|
|
|
};
|
|
|
|
|
2015-06-23 11:49:53 +02:00
|
|
|
}
|
2011-06-23 23:56:59 +02:00
|
|
|
|
|
|
|
#endif
|