13#ifndef LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
14#define LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
127 EstimatedBlockWeight(
std::
move(Arg.EstimatedBlockWeight)) {
128 for (
auto &Handle : Handles)
137 Handles = std::move(
RHS.Handles);
138 Probs = std::move(
RHS.Probs);
139 EstimatedBlockWeight = std::move(
RHS.EstimatedBlockWeight);
140 for (
auto &Handle : Handles)
146 FunctionAnalysisManager::Invalidator &);
206 return IsLikely ? LikelyProb : LikelyProb.
getCompl();
237 using SccBlockTypeMaps = std::vector<SccBlockTypeMap>;
240 SccBlockTypeMaps SccBlocks;
252 return getSccBlockType(BB, SccNum) & Header;
257 return getSccBlockType(BB, SccNum) & Exiting;
276 void calculateSccBlockType(
const BasicBlock *BB,
int SccNum);
282 class BasicBlockCallbackVH final :
public CallbackVH {
299 using LoopData = std::pair<Loop *, int>;
303 LLVM_ABI explicit LoopBlock(
const BasicBlock *BB,
const LoopInfo &LI,
304 const SccInfo &SccI);
306 const BasicBlock *getBlock()
const {
return BB; }
308 LoopData getLoopData()
const {
return LD; }
309 Loop *getLoop()
const {
return LD.first; }
310 int getSccNum()
const {
return LD.second; }
312 bool belongsToLoop()
const {
return getLoop() || getSccNum() != -1; }
313 bool belongsToSameLoop(
const LoopBlock &LB)
const {
314 return (LB.getLoop() && getLoop() == LB.getLoop()) ||
315 (LB.getSccNum() != -1 && getSccNum() == LB.getSccNum());
320 LoopData LD = {
nullptr, -1};
324 using LoopEdge = std::pair<const LoopBlock &, const LoopBlock &>;
326 DenseSet<BasicBlockCallbackVH, DenseMapInfo<Value*>> Handles;
330 using Edge = std::pair<const BasicBlock *, unsigned>;
332 DenseMap<Edge, BranchProbability> Probs;
337 const LoopInfo *LI =
nullptr;
340 std::unique_ptr<const SccInfo> SccI;
343 SmallDenseMap<const BasicBlock *, uint32_t> EstimatedBlockWeight;
346 SmallDenseMap<LoopData, uint32_t> EstimatedLoopWeight;
349 LoopBlock getLoopBlock(
const BasicBlock *BB)
const {
350 return LoopBlock(BB, *LI, *SccI);
356 bool isLoopEnteringEdge(
const LoopEdge &Edge)
const;
360 bool isLoopExitingEdge(
const LoopEdge &Edge)
const;
363 bool isLoopEnteringExitingEdge(
const LoopEdge &Edge)
const;
366 bool isLoopBackEdge(
const LoopEdge &Edge)
const;
368 void getLoopEnterBlocks(
const LoopBlock &LB,
369 SmallVectorImpl<BasicBlock *> &Enters)
const;
371 void getLoopExitBlocks(
const LoopBlock &LB,
372 SmallVectorImpl<BasicBlock *> &Exits)
const;
376 std::optional<uint32_t> getEstimatedBlockWeight(
const BasicBlock *BB)
const;
381 std::optional<uint32_t> getEstimatedLoopWeight(
const LoopData &L)
const;
385 std::optional<uint32_t> getEstimatedEdgeWeight(
const LoopEdge &Edge)
const;
390 template <
class IterT>
391 std::optional<uint32_t>
392 getMaxEstimatedEdgeWeight(
const LoopBlock &SrcBB,
399 bool updateEstimatedBlockWeight(LoopBlock &LoopBB, uint32_t BBWeight,
400 SmallVectorImpl<BasicBlock *> &BlockWorkList,
401 SmallVectorImpl<LoopBlock> &LoopWorkList);
405 void propagateEstimatedBlockWeight(
const LoopBlock &LoopBB, DominatorTree *DT,
406 PostDominatorTree *PDT, uint32_t BBWeight,
407 SmallVectorImpl<BasicBlock *> &WorkList,
408 SmallVectorImpl<LoopBlock> &LoopWorkList);
411 std::optional<uint32_t> getInitialEstimatedBlockWeight(
const BasicBlock *BB);
414 void estimateBlockWeights(
const Function &
F, DominatorTree *DT,
415 PostDominatorTree *PDT);
419 bool calcEstimatedHeuristics(
const BasicBlock *BB);
420 bool calcMetadataWeights(
const BasicBlock *BB);
421 bool calcPointerHeuristics(
const BasicBlock *BB);
422 bool calcZeroHeuristics(
const BasicBlock *BB,
const TargetLibraryInfo *TLI);
423 bool calcFloatingPointHeuristics(
const BasicBlock *BB);
468 void releaseMemory()
override;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
This file defines DenseMapInfo traits for DenseMap.
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
static bool runOnFunction(Function &F, bool PostInlining)
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This header defines various interfaces for pass management in LLVM.
Represent the analysis usage information of a pass.
LLVM Basic Block Representation.
Analysis pass which computes BranchProbabilityInfo.
LLVM_ABI BranchProbabilityInfo run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BPI.
BranchProbabilityInfo Result
Provide the result type for this analysis pass.
const BranchProbabilityInfo & getBPI() const
BranchProbabilityInfoWrapperPass()
BranchProbabilityInfo & getBPI()
bool isSCCHeader(const BasicBlock *BB, int SccNum) const
Returns true if BB is a 'header' block in SCC with SccNum ID, false otherwise.
LLVM_ABI void getSccEnterBlocks(int SccNum, SmallVectorImpl< BasicBlock * > &Enters) const
Fills in Enters vector with all such blocks that don't belong to SCC with SccNum ID but there is an e...
bool isSCCExitingBlock(const BasicBlock *BB, int SccNum) const
Returns true if BB is an 'exiting' block in SCC with SccNum ID, false otherwise.
LLVM_ABI SccInfo(const Function &F)
LLVM_ABI void getSccExitBlocks(int SccNum, SmallVectorImpl< BasicBlock * > &Exits) const
Fills in Exits vector with all such blocks that don't belong to SCC with SccNum ID but there is an ed...
LLVM_ABI int getSCCNum(const BasicBlock *BB) const
If BB belongs to some SCC then ID of that SCC is returned, otherwise -1 is returned.
Analysis providing branch probability information.
LLVM_ABI void eraseBlock(const BasicBlock *BB)
Forget analysis results for the given basic block.
LLVM_ABI void setEdgeProbability(const BasicBlock *Src, const SmallVectorImpl< BranchProbability > &Probs)
Set the raw probabilities for all edges from the given block.
BranchProbabilityInfo(const BranchProbabilityInfo &)=delete
LLVM_ABI bool invalidate(Function &, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
static BranchProbability getBranchProbStackProtector(bool IsLikely)
LLVM_ABI void calculate(const Function &F, const LoopInfo &LI, const TargetLibraryInfo *TLI, DominatorTree *DT, PostDominatorTree *PDT)
BranchProbabilityInfo & operator=(BranchProbabilityInfo &&RHS)
BranchProbabilityInfo()=default
LLVM_ABI void releaseMemory()
LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
LLVM_ABI void swapSuccEdgesProbabilities(const BasicBlock *Src)
Swap outgoing edges probabilities for Src with branch terminator.
BranchProbabilityInfo(BranchProbabilityInfo &&Arg)
LLVM_ABI void print(raw_ostream &OS) const
BranchProbabilityInfo(const Function &F, const LoopInfo &LI, const TargetLibraryInfo *TLI=nullptr, DominatorTree *DT=nullptr, PostDominatorTree *PDT=nullptr)
BranchProbabilityInfo & operator=(const BranchProbabilityInfo &)=delete
LLVM_ABI raw_ostream & printEdgeProbability(raw_ostream &OS, const BasicBlock *Src, const BasicBlock *Dst) const
Print an edge's probability.
LLVM_ABI void copyEdgeProbabilities(BasicBlock *Src, BasicBlock *Dst)
Copy outgoing edge probabilities from Src to Dst.
BranchProbabilityPrinterPass(raw_ostream &OS)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
BranchProbability getCompl() const
Value handle with callbacks on RAUW and destruction.
CallbackVH(const CallbackVH &)=default
virtual void deleted()
Callback for Value destruction.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Represents a single loop in the control flow graph.
A Module instance is used to store all the information related to an LLVM module.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
A set of analyses that are preserved following a run of a transformation pass.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Provides information about what library functions are available for the current target.
Value * getValPtr() const
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream.
@ BasicBlock
Various leaf nodes.
This is an optimization pass for GlobalISel generic memory operations.
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
SuccIterator< const Instruction, const BasicBlock > const_succ_iterator
Implement std::hash so that hash_code can be used in STL containers.
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
A CRTP mix-in to automatically provide informational APIs needed for passes.