31#define DEBUG_TYPE "loop-extract"
33STATISTIC(NumExtracted,
"Number of loops extracted");
36struct LoopExtractorLegacyPass :
public ModulePass {
41 explicit LoopExtractorLegacyPass(
unsigned NumLoops = ~0)
59 explicit LoopExtractor(
64 : NumLoops(NumLoops), LookupDomTree(LookupDomTree),
65 LookupLoopInfo(LookupLoopInfo),
66 LookupAssumptionCache(LookupAssumptionCache) {}
67 bool runOnModule(
Module &M);
85char LoopExtractorLegacyPass::ID = 0;
87 "Extract loops into new functions",
false,
false)
103char SingleLoopExtractor::ID = 0;
105 "Extract at most one loop into a new function",
false,
false)
112bool LoopExtractorLegacyPass::runOnModule(
Module &M) {
116 bool Changed =
false;
118 return this->getAnalysis<DominatorTreeWrapperPass>(
F).getDomTree();
121 return this->getAnalysis<LoopInfoWrapperPass>(
F, &Changed).getLoopInfo();
124 if (
auto *ACT = this->getAnalysisIfAvailable<AssumptionCacheTracker>())
125 return ACT->lookupAssumptionCache(
F);
128 return LoopExtractor(NumLoops, LookupDomTree, LookupLoopInfo, LookupACT)
133bool LoopExtractor::runOnModule(
Module &M) {
140 bool Changed =
false;
144 auto I =
M.begin(), E = --
M.end();
161bool LoopExtractor::runOnFunction(
Function &
F) {
169 bool Changed =
false;
180 if (std::next(LI.
begin()) != LI.
end())
181 return Changed | extractLoops(LI.
begin(), LI.
end(), LI, DT);
189 bool ShouldExtractLoop =
false;
192 Instruction *EntryTI =
F.getEntryBlock().getTerminator();
193 if (!isa<BranchInst>(EntryTI) ||
194 !cast<BranchInst>(EntryTI)->isUnconditional() ||
196 ShouldExtractLoop =
true;
202 for (
auto *ExitBlock : ExitBlocks)
203 if (!isa<ReturnInst>(ExitBlock->getTerminator())) {
204 ShouldExtractLoop =
true;
209 if (ShouldExtractLoop)
210 return Changed | extractLoop(TLL, LI, DT);
217 return Changed | extractLoops(TLL->
begin(), TLL->
end(), LI, DT);
222 bool Changed =
false;
229 if (!
L->isLoopSimplifyForm())
232 Changed |= extractLoop(L, LI, DT);
244 CodeExtractor Extractor(
L->getBlocks(), &DT,
false,
nullptr,
nullptr, AC);
245 if (Extractor.extractCodeRegion(CEAC)) {
258 return new SingleLoopExtractor();
272 if (!LoopExtractor(NumLoops, LookupDomTree, LookupLoopInfo,
273 LookupAssumptionCache)
285 OS, MapClassName2PassName);
BlockVerifier::State From
static bool runOnFunction(Function &F, bool PostInlining)
Module.h This file contains the declarations for the Module class.
FunctionAnalysisManager FAM
This header defines various interfaces for pass management in LLVM.
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
A container for analyses that lazily runs them and caches their results.
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 & addRequiredID(const void *ID)
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
Analysis pass which computes a DominatorTree.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
Analysis pass that exposes the LoopInfo for a function.
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all of the successor blocks of this loop.
BlockT * getHeader() const
std::vector< Loop * >::const_iterator iterator
The legacy pass manager's analysis pass to compute loop information.
void erase(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
Represents a single loop in the control flow graph.
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to,...
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
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.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
NodeAddr< FuncNode * > Func
This is an optimization pass for GlobalISel generic memory operations.
void initializeLoopExtractorLegacyPassPass(PassRegistry &)
Pass * createLoopExtractorPass()
createLoopExtractorPass - This pass extracts all natural loops from the program into a function if it...
Pass * createSingleLoopExtractorPass()
createSingleLoopExtractorPass - This pass extracts one natural loop from the program into a function ...
char & BreakCriticalEdgesID
A CRTP mix-in to automatically provide informational APIs needed for passes.