34 if (findCallInst(*BB.getTerminator()) ||
llvm::any_of(BB, findCallInst))
50 assert(BB !=
nullptr &&
"Traversing Null BB to find calls?");
53 auto CalledValue =
Call->getCalledOperand()->stripPointerCasts();
73size_t BlockFreqQuery::numBBToGet(
size_t numBB) {
81 return (numBB / 2) + (numBB / 4);
91 PB.registerFunctionAnalyses(
FAM);
93 auto IBBs = findBBwithCalls(
F);
100 for (
const auto I : IBBs)
101 BBFreqs.
push_back({
I, BFI.getBlockFreq(
I).getFrequency()});
103 assert(IBBs.size() == BBFreqs.
size() &&
"BB Count Mismatch");
105 llvm::sort(BBFreqs, [](
decltype(BBFreqs)::const_reference BBF,
106 decltype(BBFreqs)::const_reference BBS) {
107 return BBF.second > BBS.second ?
true :
false;
111 auto Topk = numBBToGet(BBFreqs.
size());
113 for (
size_t i = 0; i < Topk; i++)
116 assert(!Calles.
empty() &&
"Running Analysis on Function with no calls?");
118 CallerAndCalles.
insert({
F.getName(), std::move(Calles)});
120 return CallerAndCalles;
124std::size_t SequenceBBQuery::getHottestBlocks(std::size_t TotalBlocks) {
125 if (TotalBlocks == 1)
127 return TotalBlocks / 2;
132SequenceBBQuery::rearrangeBB(
const Function &
F,
const BlockListTy &BBList) {
139 assert(RearrangedBBSet.size() == BBList.size() &&
140 "BasicBlock missing while rearranging?");
141 return RearrangedBBSet;
144void SequenceBBQuery::traverseToEntryBlock(
const BasicBlock *AtBB,
145 const BlockListTy &CallerBlocks,
146 const BackEdgesInfoTy &BackEdgesInfo,
151 if (!Itr->second.Upward)
153 Itr->second.Upward =
false;
156 WalkDirection BlockHint;
157 BlockHint.Upward =
false;
160 BlockHint.CallerBlock =
true;
171 DenseSet<const BasicBlock *> PredSkipNodes;
175 for (
auto &
I : BackEdgesInfo)
176 if (
I.second == AtBB)
180 for (; PIt != EIt; ++PIt)
183 traverseToEntryBlock(*PIt, CallerBlocks, BackEdgesInfo, BPI,
187void SequenceBBQuery::traverseToExitBlock(
const BasicBlock *AtBB,
188 const BlockListTy &CallerBlocks,
189 const BackEdgesInfoTy &BackEdgesInfo,
190 const BranchProbabilityInfo *BPI,
194 if (!Itr->second.Downward)
196 Itr->second.Downward =
false;
199 WalkDirection BlockHint;
200 BlockHint.Downward =
false;
203 BlockHint.CallerBlock =
true;
212 DenseSet<const BasicBlock *> SuccSkipNodes;
216 for (
auto &
I : BackEdgesInfo)
218 SuccSkipNodes.
insert(
I.second);
220 for (; PIt != EIt; ++PIt)
222 traverseToExitBlock(*PIt, CallerBlocks, BackEdgesInfo, BPI,
230SequenceBBQuery::queryCFG(Function &
F,
const BlockListTy &CallerBlocks) {
244 for (
const auto I : CallerBlocks)
245 BBFreqs.push_back({
I, BFI.getBlockFreq(
I).getFrequency()});
247 llvm::sort(BBFreqs, [](
decltype(BBFreqs)::const_reference Bbf,
248 decltype(BBFreqs)::const_reference Bbs) {
249 return Bbf.second > Bbs.second;
254 HotBlocksRef.drop_back(BBFreqs.size() - getHottestBlocks(BBFreqs.size()));
256 BranchProbabilityInfo *BPI =
263 for (
auto I : HotBlocksRef) {
264 traverseToEntryBlock(
I.first, CallerBlocks, BackEdgesInfo, BPI,
266 traverseToExitBlock(
I.first, CallerBlocks, BackEdgesInfo, BPI,
272 if (
I.second.CallerBlock)
273 MinCallerBlocks.push_back(std::move(
I.first));
275 return rearrangeBB(
F, MinCallerBlocks);
285 CallerBlocks = findBBwithCalls(
F);
286 if (CallerBlocks.
empty())
290 SequencedBlocks = rearrangeBB(
F, CallerBlocks);
292 SequencedBlocks = queryCFG(
F, CallerBlocks);
294 for (
const auto *BB : SequencedBlocks)
297 CallerAndCalles.
insert({
F.getName(), std::move(Calles)});
298 return CallerAndCalles;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
SmallPtrSet< const BasicBlock *, 8 > VisitedBlocks
This file defines the DenseMap class.
This header defines various interfaces for pass management in LLVM.
static const Function * getCalledFunction(const Value *V)
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
This file defines the SmallVector class.
Contains the Analyses and Result Interpretation to select likely functions to Speculatively compile b...
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Analysis pass which computes BlockFrequencyInfo.
Analysis providing branch probability information.
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.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Implements a dense probed hash-table based set.
This class provides access to building LLVM's passes.
LLVM_ABI void registerFunctionAnalyses(FunctionAnalysisManager &FAM)
Registers all available function analysis passes.
iterator find(ConstPtrType Ptr) const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
std::pair< iterator, bool > insert(const ValueT &V)
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
LLVM_ABI ResultTy operator()(Function &F)
LLVM_ABI ResultTy operator()(Function &F)
SmallVector< std::pair< const BasicBlock *, uint64_t >, 8 > BlockFreqInfoTy
DenseMap< const BasicBlock *, WalkDirection > VisitedBlocksInfoTy
SmallVector< const BasicBlock *, 8 > BlockListTy
SmallVector< std::pair< const BasicBlock *, const BasicBlock * >, 8 > BackEdgesInfoTy
std::optional< DenseMap< StringRef, DenseSet< StringRef > > > ResultTy
LLVM_ABI bool isStraightLine(const Function &F)
LLVM_ABI void findCalles(const BasicBlock *, DenseSet< StringRef > &)
Interfaces for registering analysis passes, producing common pass manager configurations,...
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
auto pred_end(const MachineBasicBlock *BB)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
PredIterator< const BasicBlock, Value::const_user_iterator > const_pred_iterator
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
void sort(IteratorTy Start, IteratorTy End)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
ArrayRef(const T &OneElt) -> ArrayRef< T >
auto pred_begin(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Instruction::const_succ_iterator const_succ_iterator
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI void FindFunctionBackedges(const Function &F, SmallVectorImpl< std::pair< const BasicBlock *, const BasicBlock * > > &Result)
Analyze the specified function to find all of the loop backedges in the function and return them.