23#ifndef LLVM_ADT_GENERICCYCLEIMPL_H
24#define LLVM_ADT_GENERICCYCLEIMPL_H
31#define DEBUG_TYPE "generic-cycle-impl"
35template <
typename ContextT>
47template <
typename ContextT>
50 if (!ExitBlocksCache.empty()) {
51 TmpStorage.
append(ExitBlocksCache.begin(), ExitBlocksCache.end());
55 size_t NumExitBlocks = 0;
59 for (
size_t Idx = NumExitBlocks, End = ExitBlocksCache.size(); Idx < End;
61 BlockT *Succ = ExitBlocksCache[Idx];
63 auto ExitEndIt = ExitBlocksCache.begin() + NumExitBlocks;
64 if (std::find(ExitBlocksCache.begin(), ExitEndIt, Succ) == ExitEndIt)
65 ExitBlocksCache[NumExitBlocks++] = Succ;
69 ExitBlocksCache.resize(NumExitBlocks);
72 TmpStorage.
append(ExitBlocksCache.begin(), ExitBlocksCache.end());
75template <
typename ContextT>
88template <
typename ContextT>
100 if (!Predecessor->isLegalToHoistInto())
106template <
typename ContextT>
117 if (Out && Out != Pred)
129 assert(!Blocks.empty() &&
"Cycle cannot be empty.");
132 assert(Blocks.insert(BB).second);
134 assert(!Entries.empty() &&
"Cycle must have one or more entries.");
138 assert(Entries.insert(Entry).second);
155 "Cycle block has no in-cycle successors!");
159 "Cycle block has no in-cycle predecessors!");
166 if (Entries.contains(BB)) {
167 assert(!OutsideCyclePreds.empty() &&
"Entry is unreachable!");
168 }
else if (!OutsideCyclePreds.empty()) {
172 BlockT *EntryBB = &BB->getParent()->front();
174 assert(!OutsideCyclePreds.contains(CB) &&
175 "Non-entry block reachable from outside!");
178 "Cycle contains function entry block!");
184 dbgs() <<
"The following blocks are unreachable in the cycle:\n ";
186 for (
auto *BB : Blocks) {
187 if (!VisitedBBs.
count(BB)) {
189 BB->printAsOperand(
dbgs());
203template <
typename ContextT>
207 for (GenericCycle *Child :
children()) {
209 for (
BlockT *BB : Child->blocks()) {
211 "Cycle does not contain all the blocks of a subcycle!");
213 assert(Child->Depth == Depth + 1);
219 "Cycle is not a subcycle of its parent!");
220 assert(ParentCycle->TopLevelCycle == TopLevelCycle &&
221 "Top level cycle of parent cycle must be the same");
223 assert(TopLevelCycle ==
this &&
224 "Cycle without parent must be top-level cycle");
230template <
typename ContextT>
class GenericCycleInfoCompute {
231 using BlockT =
typename ContextT::BlockT;
232 using FunctionT =
typename ContextT::FunctionT;
234 using CycleT =
typename CycleInfoT::CycleT;
243 explicit DFSInfo(
unsigned Start) : Start(Start) {}
245 explicit operator bool()
const {
return Start; }
249 bool isAncestorOf(
const DFSInfo &
Other)
const {
250 return Start <=
Other.Start &&
Other.End <= End;
258 GenericCycleInfoCompute(
const GenericCycleInfoCompute &) =
delete;
259 GenericCycleInfoCompute &operator=(
const GenericCycleInfoCompute &) =
delete;
261 DFSInfo getDFSInfo(BlockT *
B)
const {
263 return BlockDFSInfo[
Number];
266 DFSInfo &getOrInsertDFSInfo(BlockT *
B) {
268 return BlockDFSInfo[
Number];
274 void run(FunctionT *
F);
279 void dfs(FunctionT *
F, BlockT *EntryBlock);
282template <
typename ContextT>
286 return Cycle ?
Cycle->TopLevelCycle :
nullptr;
289template <
typename ContextT>
290void GenericCycleInfo<ContextT>::moveTopLevelCycleToNewParent(CycleT *NewParent,
292 assert((!Child->ParentCycle && !NewParent->ParentCycle) &&
293 "NewParent and Child must be both top level cycle!\n");
294 auto &CurrentContainer =
295 Child->ParentCycle ? Child->ParentCycle->Children : TopLevelCycles;
297 return Child == Ptr.get();
299 assert(Pos != CurrentContainer.end());
300 NewParent->Children.push_back(std::move(*Pos));
301 *Pos = std::move(CurrentContainer.back());
302 CurrentContainer.pop_back();
303 Child->ParentCycle = NewParent;
304 Child->TopLevelCycle = NewParent;
306 Cycle->TopLevelCycle = NewParent;
308 NewParent->Blocks.insert_range(Child->blocks());
309 NewParent->clearCache();
313template <
typename ContextT>
314void GenericCycleInfo<ContextT>::verifyBlockNumberEpoch(
318 "CycleInfo used with outdated block number epoch");
321template <
typename ContextT>
322void GenericCycleInfo<ContextT>::addToBlockMap(BlockT *
Block, CycleT *
Cycle) {
324 verifyBlockNumberEpoch(
Block->getParent());
329template <
typename ContextT>
333 if (
Number >= BlockMap.size())
343 while (ParentCycle) {
346 ParentCycle =
Cycle->getParentCycle();
353template <
typename ContextT>
356 LLVM_DEBUG(
errs() <<
"Entry block: " << Info.Context.print(EntryBlock)
362 for (BlockT *HeaderCandidate :
llvm::reverse(BlockPreorder)) {
363 const DFSInfo CandidateInfo = getDFSInfo(HeaderCandidate);
366 const DFSInfo PredDFSInfo = getDFSInfo(Pred);
369 if (CandidateInfo.isAncestorOf(PredDFSInfo))
372 if (Worklist.
empty()) {
378 << Info.Context.print(HeaderCandidate) <<
"\n");
379 std::unique_ptr<CycleT> NewCycle = std::make_unique<CycleT>();
380 NewCycle->appendEntry(HeaderCandidate);
381 NewCycle->appendBlock(HeaderCandidate);
382 Info.addToBlockMap(HeaderCandidate, NewCycle.get());
387 auto ProcessPredecessors = [&](BlockT *
Block) {
390 bool IsEntry =
false;
392 const DFSInfo PredDFSInfo = getDFSInfo(Pred);
393 if (CandidateInfo.isAncestorOf(PredDFSInfo)) {
395 }
else if (!PredDFSInfo) {
406 NewCycle->appendEntry(
Block);
414 if (
Block == HeaderCandidate)
420 if (
auto *BlockParent = Info.getTopLevelParentCycle(
Block)) {
423 if (BlockParent != NewCycle.get()) {
425 <<
"discovered child cycle "
426 << Info.Context.print(BlockParent->getHeader()) <<
"\n");
428 Info.moveTopLevelCycleToNewParent(NewCycle.get(), BlockParent);
430 for (
auto *ChildEntry : BlockParent->entries())
431 ProcessPredecessors(ChildEntry);
434 <<
"known child cycle "
435 << Info.Context.print(BlockParent->getHeader()) <<
"\n");
438 Info.addToBlockMap(
Block, NewCycle.get());
440 NewCycle->Blocks.insert(
Block);
441 ProcessPredecessors(
Block);
443 }
while (!Worklist.
empty());
445 Info.TopLevelCycles.push_back(std::move(NewCycle));
449 for (
auto *TLC : Info.toplevel_cycles()) {
451 << Info.Context.print(TLC->getHeader()) <<
"\n");
453 TLC->ParentCycle =
nullptr;
459template <
typename ContextT>
468template <
typename ContextT>
469void GenericCycleInfoCompute<ContextT>::dfs(FunctionT *
F, BlockT *EntryBlock) {
472 unsigned Counter = 0;
480 DFSInfo &Info = getOrInsertDFSInfo(
Block);
481 if (Info.Start == 0) {
482 Info.Start = ++Counter;
489 << TraverseStack.
size() <<
"\n");
494 BlockPreorder.push_back(
Block);
498 if (DFSTreeStack.
back() == TraverseStack.
size()) {
507 }
while (!TraverseStack.
empty());
511 errs() <<
"Preorder:\n";
512 for (
int i = 0, e = BlockPreorder.size(); i != e; ++i) {
513 errs() <<
" " << Info.Context.print(BlockPreorder[i]) <<
": " << i <<
"\n";
520 TopLevelCycles.clear();
525template <
typename ContextT>
528 Context = ContextT(&
F);
537template <
typename ContextT>
554template <
typename ContextT>
557 verifyBlockNumberEpoch(
Block->getParent());
559 return Number < BlockMap.size() ? BlockMap[
Number] :
nullptr;
566template <
typename ContextT>
575 while (
A->getDepth() >
B->getDepth())
576 A =
A->getParentCycle();
577 while (
B->getDepth() >
A->getDepth())
578 B =
B->getParentCycle();
584 A =
A->getParentCycle();
585 B =
B->getParentCycle();
595template <
typename ContextT>
606template <
typename ContextT>
611 return Cycle->getDepth();
618template <
typename ContextT>
628 Cycle->verifyCycle();
630 Cycle->verifyCycleNest();
634 assert(CycleInBlockMap !=
nullptr);
648template <
typename ContextT>
652 for (
unsigned I = 0;
I <
Cycle->Depth; ++
I)
655 Out <<
Cycle->print(Context) <<
'\n';
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static const Function * getParent(const Value *V)
bbsections Prepares for basic block by splitting functions into clusters of basic blocks
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseSet and SmallDenseSet classes.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
Find all cycles in a control-flow graph, including irreducible loops.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Implements a dense probed hash-table based set.
GenericCycleInfoCompute(CycleInfoT &Info)
void run(FunctionT *F)
Main function of the cycle info computations.
static void updateDepth(CycleT *SubTree)
Recompute depth values of SubTree and all descendants.
Cycle information for a function.
typename SSAContext::FunctionT FunctionT
void verify() const
Verify that the entire cycle tree well-formed.
void addBlockToCycle(BlockT *Block, CycleT *Cycle)
Assumes that Cycle is the innermost cycle containing Block.
iterator_range< const_toplevel_iterator > toplevel_cycles() const
CycleT * getTopLevelParentCycle(const BlockT *Block) const
friend class GenericCycleInfoCompute
void print(raw_ostream &Out) const
Print the cycle info.
CycleT * getSmallestCommonCycle(CycleT *A, CycleT *B) const
Find the innermost cycle containing both given cycles.
void clear()
Reset the object to its initial state.
GenericCycle< ContextT > CycleT
void compute(FunctionT &F)
Compute the cycle info for a function.
void splitCriticalEdge(BlockT *Pred, BlockT *Succ, BlockT *New)
unsigned getCycleDepth(const BlockT *Block) const
get the depth for the cycle which containing a given block.
void verifyCycleNest(bool VerifyFull=false) const
Methods for debug and self-test.
typename ContextT::BlockT BlockT
CycleT * getCycle(const BlockT *Block) const
Find the innermost cycle containing a given block.
BlockT * getHeader() const
bool isReducible() const
Whether the cycle is a natural loop.
void getExitingBlocks(SmallVectorImpl< BlockT * > &TmpStorage) const
Return all blocks of this cycle that have successor outside of this cycle.
void verifyCycle() const
Verify that this is actually a well-formed cycle in the CFG.
iterator_range< const_entry_iterator > entries() const
void verifyCycleNest() const
Verify the parent-child relations of this cycle.
BlockT * getCyclePreheader() const
Return the preheader block for this cycle.
void getExitBlocks(SmallVectorImpl< BlockT * > &TmpStorage) const
Return all of the successor blocks of this cycle.
BlockT * getCyclePredecessor() const
If the cycle has exactly one entry with exactly one predecessor, return it, otherwise return nullptr.
bool contains(const BlockT *Block) const
Return whether Block is contained in the cycle.
typename ContextT::BlockT BlockT
size_t getNumBlocks() const
A helper class to return the specified delimiter string after the first invocation of operator String...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
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)
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
auto successors(const MachineBasicBlock *BB)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto succ_size(const MachineBasicBlock *BB)
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
iterator_range< typename GraphTraits< Inverse< GraphType > >::ChildIteratorType > inverse_children(const typename GraphTraits< GraphType >::NodeRef &G)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
auto predecessors(const MachineBasicBlock *BB)
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
iterator_range< df_iterator< T > > depth_first(const T &G)
std::pair< iterator, bool > insert(NodeRef N)