143#define DEBUG_TYPE "fix-irreducible"
166char FixIrreducible::ID = 0;
171 "Convert irreducible control-flow into natural loops",
176 "Convert irreducible control-flow into natural loops",
189 LI.takeChildrenIf(ParentLoop, [&](
Loop *L) {
190 return NewLoop != L &&
194 for (
Loop *Child : ChildLoops) {
195 LLVM_DEBUG(
dbgs() <<
"child loop: " << Child->getHeader()->getName()
199 if (Child->getHeader() == OldHeader) {
200 for (
auto *BB : Child->blocks()) {
201 if (LI.getLoopFor(BB) != Child)
203 LI.changeLoopFor(BB, NewLoop);
207 for (
Loop *GrandChildLoop :
208 LI.takeChildrenIf(Child, [](
const Loop *) { return true; }))
209 NewLoop->addChildLoop(GrandChildLoop);
215 NewLoop->addChildLoop(Child);
227 if (ParentLoop && ParentLoop->
getHeader() == CycleHeader)
243 for (
auto *
G : GuardBlocks) {
244 LLVM_DEBUG(
dbgs() <<
"added guard block to loop: " <<
G->getName() <<
"\n");
245 NewLoop->addBasicBlockToLoop(
G, LI);
249 NewLoop->addBlockEntry(BB);
255 LLVM_DEBUG(
dbgs() <<
"added block from child: " << BB->getName() <<
"\n");
259 << NewLoop->getHeader()->getName() <<
"\n");
264 NewLoop->verifyLoop();
294 assert(Term->getSuccessor(0) == Header);
300 BasicBlock *Succ0 = Branch->getSuccessor(0) == Header ? Header :
nullptr;
301 BasicBlock *Succ1 = Branch->getSuccessor(1) == Header ? Header :
nullptr;
311 for (
unsigned I = 0;
I < Term->getNumSuccessors(); ++
I) {
324 "unsupported block terminator: fix-irreducible "
325 "only supports br, callbr, and switch instructions");
330 Predecessors.
clear();
342 Succ0 = CI.
contains(
C, Succ0) ? Succ0 :
nullptr;
349 Succ0 = CI.
contains(
C, Succ0) ? Succ0 :
nullptr;
351 Succ1 = CI.
contains(
C, Succ1) ? Succ1 :
nullptr;
360 for (
unsigned I = 0;
I < Term->getNumSuccessors(); ++
I) {
364 auto It = MultiBrTargets.
find(Succ);
366 (It != MultiBrTargets.
end()) ? It->second :
nullptr;
370 if (!ExistingTarget) {
372 MultiBrTargets[Succ] = NewSucc;
380 "unsupported block terminator: fix-irreducible "
381 "only supports br, callbr, and switch instructions");
398 CHub.
finalize(&DTU, GuardBlocks,
"irr");
399#if defined(EXPENSIVE_CHECKS)
400 assert(DT.
verify(DominatorTree::VerificationLevel::Full));
402 assert(DT.
verify(DominatorTree::VerificationLevel::Fast));
410 for (
auto *
G : GuardBlocks) {
427 LLVM_DEBUG(
dbgs() <<
"===== Fix irreducible control-flow in function: "
428 <<
F.getName() <<
"\n");
437#if defined(EXPENSIVE_CHECKS)
447bool FixIrreducible::runOnFunction(
Function &
F) {
448 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
449 LoopInfo *LI = LIWP ? &LIWP->getLoopInfo() :
nullptr;
450 auto &CI = getAnalysis<CycleInfoWrapperPass>().getResult();
451 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file declares an analysis pass that computes CycleInfo for LLVM IR, specialized from GenericCycl...
This file defines the DenseMap class.
static bool runOnFunction(Function &F, bool PostInlining)
fix Convert irreducible control flow into natural static false void reconnectChildLoops(LoopInfo &LI, Loop *ParentLoop, Loop *NewLoop, BasicBlock *OldHeader)
static bool FixIrreducibleImpl(Function &F, CycleInfo &CI, DominatorTree &DT, LoopInfo *LI)
static void updateLoopInfo(CycleInfo &CI, LoopInfo &LI, CycleRef C, ArrayRef< BasicBlock * > GuardBlocks)
static bool fixIrreducible(CycleRef C, CycleInfo &CI, DominatorTree &DT, LoopInfo *LI)
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
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.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
LLVM Basic Block Representation.
Conditional Branch instruction.
Analysis pass which computes a CycleInfo.
Legacy analysis pass which computes a CycleInfo.
Opaque handle to a cycle within a GenericCycleInfo that wraps the cycle's preorder index.
iterator find(const_arg_type_t< KeyT > Val)
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.
FunctionPass class - This class is used to implement most global optimizations.
void verify() const
Verify that the entire cycle tree well-formed.
auto cycles() const
All cycles in forest preorder.
void verifyCycle(CycleRef C) const
Verify that C is actually a well-formed cycle in the CFG.
bool isReducible(CycleRef C) const
CycleRef getParentCycle(CycleRef C) const
void print(raw_ostream &Out) const
Print the cycle info.
ArrayRef< BlockT * > getEntries(CycleRef C) const
void setSingleEntry(CycleRef C, BlockT *Block)
void addBlockToCycle(BlockT *Block, CycleRef C)
Assumes that C is the innermost cycle containing Block.
ArrayRef< BlockT * > getBlocks(CycleRef C) const
Return the blocks of C, including those of nested cycles.
bool contains(CycleRef Outer, CycleRef Inner) const
Returns true iff Outer contains Inner. O(1). Non-strict.
BlockT * getHeader(CycleRef C) const
Analysis pass that exposes the LoopInfo for a function.
void verifyLoop() const
Verify loop structure.
BlockT * getHeader() const
void print(raw_ostream &OS, bool Verbose=false, bool PrintNested=true, unsigned Depth=0) const
Print loop with all the BBs inside it.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
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 * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
void changeLoopFor(const BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
The legacy pass manager's analysis pass to compute loop information.
Represents a single loop in the control flow graph.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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.
PreservedAnalyses & preserve()
Mark an analysis as preserved.
A vector that has set insertion semantics.
void clear()
Completely clear the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Unconditional Branch instruction.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI BasicBlock * SplitMultiBrEdge(BasicBlock *MultiBrBlock, BasicBlock *Succ, unsigned SuccIdx, BasicBlock *BrTarget=nullptr, DomTreeUpdater *DTU=nullptr, CycleInfo *CI=nullptr, LoopInfo *LI=nullptr, bool *UpdatedLI=nullptr)
Create a new intermediate target block for a callbr or switch edge.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI FunctionPass * createFixIrreduciblePass()
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...
LLVM_ABI Printable printBasicBlock(const BasicBlock *BB)
Print BasicBlock BB as an operand or print "<nullptr>" if BB is a nullptr.
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI void initializeFixIrreduciblePass(PassRegistry &)
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Given a set of branch descriptors [BB, Succ0, Succ1], create a "hub" such that the control flow from ...
void addBranch(BasicBlock *BB, BasicBlock *Succ0, BasicBlock *Succ1=nullptr)
LLVM_ABI std::pair< BasicBlock *, bool > finalize(DomTreeUpdater *DTU, SmallVectorImpl< BasicBlock * > &GuardBlocks, const StringRef Prefix, std::optional< unsigned > MaxControlFlowBooleans=std::nullopt)
Return the unified loop exit block and a flag indicating if the CFG was changed at all.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)