78#define DEBUG_TYPE "fix-irreducible"
102char FixIrreducible::ID = 0;
107 "Convert irreducible control-flow into natural loops",
121 auto &CandidateLoops = ParentLoop ? ParentLoop->getSubLoopsVector()
122 : LI.getTopLevelLoopsVector();
126 auto FirstChild = std::partition(
127 CandidateLoops.begin(), CandidateLoops.end(), [&](
Loop *L) {
128 return L == NewLoop || !Blocks.contains(L->getHeader());
131 CandidateLoops.erase(FirstChild, CandidateLoops.end());
133 for (
Loop *Child : ChildLoops) {
134 LLVM_DEBUG(
dbgs() <<
"child loop: " << Child->getHeader()->getName()
139 if (Headers.count(Child->getHeader())) {
140 for (
auto *BB : Child->blocks()) {
141 if (LI.getLoopFor(BB) != Child)
143 LI.changeLoopFor(BB, NewLoop);
147 std::vector<Loop *> GrandChildLoops;
148 std::swap(GrandChildLoops, Child->getSubLoopsVector());
149 for (
auto *GrandChildLoop : GrandChildLoops) {
150 GrandChildLoop->setParentLoop(
nullptr);
151 NewLoop->addChildLoop(GrandChildLoop);
158 Child->setParentLoop(
nullptr);
159 NewLoop->addChildLoop(Child);
173 for (
auto *
H : Headers) {
179 for (
auto *
H : Headers) {
186 dbgs() <<
"Found predecessors:";
187 for (
auto P : Predecessors) {
188 dbgs() <<
" " <<
P->getName();
198#if defined(EXPENSIVE_CHECKS)
199 assert(DT.
verify(DominatorTree::VerificationLevel::Full));
201 assert(DT.
verify(DominatorTree::VerificationLevel::Fast));
217 for (
auto *
G : GuardBlocks) {
219 NewLoop->addBasicBlockToLoop(
G, LI);
224 NewLoop->addBlockEntry(BB);
230 LLVM_DEBUG(
dbgs() <<
"added block from child: " << BB->getName() <<
"\n");
234 << NewLoop->getHeader()->getName() <<
"\n");
238 NewLoop->verifyLoop();
242#if defined(EXPENSIVE_CHECKS)
269template <
class Graph>
271 bool Changed =
false;
272 for (
auto Scc =
scc_begin(
G); !Scc.isAtEnd(); ++Scc) {
277 for (
auto N : *Scc) {
305 if (Headers.
size() == 1) {
307 LLVM_DEBUG(
dbgs() <<
"Natural loop with a single header: skipped\n");
317 LLVM_DEBUG(
dbgs() <<
"===== Fix irreducible control-flow in function: "
318 <<
F.getName() <<
"\n");
320 bool Changed =
false;
330 while (!WorkList.
empty()) {
333 << L->getHeader()->getName() <<
"\n");
337 WorkList.
append(L->begin(), L->end());
343bool FixIrreducible::runOnFunction(
Function &
F) {
344 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
345 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
DenseMap< Block *, BlockRelaxAux > Blocks
static void createNaturalLoop(LoopInfo &LI, DominatorTree &DT, Function *F, SetVector< BasicBlock * > &Blocks, SetVector< BasicBlock * > &Headers)
static bool FixIrreducibleImpl(Function &F, LoopInfo &LI, DominatorTree &DT)
fix Convert irreducible control flow into natural static false void reconnectChildLoops(LoopInfo &LI, Loop *ParentLoop, Loop *NewLoop, SetVector< BasicBlock * > &Blocks, SetVector< BasicBlock * > &Headers)
static BasicBlock * unwrapBlock(BasicBlock *B)
fix Convert irreducible control flow into natural loops
static bool makeReducible(LoopInfo &LI, DominatorTree &DT, Graph &&G)
static void createNaturalLoopInternal(LoopInfo &LI, DominatorTree &DT, Loop *ParentLoop, SetVector< BasicBlock * > &Blocks, SetVector< BasicBlock * > &Headers)
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequiredID(const void *ID)
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
Analysis pass which computes a DominatorTree.
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
FunctionPass class - This class is used to implement most global optimizations.
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Analysis pass that exposes the LoopInfo for a function.
void verifyLoop() const
Verify loop structure.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
void verify(const DominatorTreeBase< BlockT, false > &DomTree) const
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
LoopT * AllocateLoop(ArgsTy &&...Args)
bool isLoopHeader(const BlockT *BB) const
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
The legacy pass manager's analysis pass to compute loop information.
Represents a single loop in the control flow graph.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void preserve()
Mark an analysis as preserved.
A vector that has set insertion semantics.
size_type size() const
Determine the number of elements in the SetVector.
const value_type & front() const
Return the first element of the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
void append_range(Container &C, Range &&R)
Wrapper function to append a range to a container.
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
auto reverse(ContainerTy &&C)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createFixIrreduciblePass()
void initializeFixIrreduciblePass(PassRegistry &)
BasicBlock * CreateControlFlowHub(DomTreeUpdater *DTU, SmallVectorImpl< BasicBlock * > &GuardBlocks, const SetVector< BasicBlock * > &Predecessors, const SetVector< BasicBlock * > &Successors, const StringRef Prefix, std::optional< unsigned > MaxControlFlowBooleans=std::nullopt)
Given a set of incoming and outgoing blocks, create a "hub" such that every edge from an incoming blo...
auto predecessors(const MachineBasicBlock *BB)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
std::pair< const Loop *, BasicBlock * > NodeRef