31#define DEBUG_TYPE "loop-extract"
33STATISTIC(NumExtracted,
"Number of loops extracted");
36struct LoopExtractorLegacyPass :
public ModulePass {
41 explicit LoopExtractorLegacyPass(
unsigned NumLoops = ~0)
44 bool runOnModule(
Module &M)
override;
46 void getAnalysisUsage(AnalysisUsage &AU)
const override {
57 explicit LoopExtractor(
59 function_ref<DominatorTree &(Function &)> LookupDomTree,
60 function_ref<LoopInfo &(Function &)> LookupLoopInfo,
61 function_ref<AssumptionCache *(Function &)> LookupAssumptionCache)
62 : NumLoops(NumLoops), LookupDomTree(LookupDomTree),
63 LookupLoopInfo(LookupLoopInfo),
64 LookupAssumptionCache(LookupAssumptionCache) {}
65 bool runOnModule(
Module &M);
71 function_ref<DominatorTree &(
Function &)> LookupDomTree;
72 function_ref<LoopInfo &(
Function &)> LookupLoopInfo;
73 function_ref<AssumptionCache *(
Function &)> LookupAssumptionCache;
77 bool extractLoops(Loop::iterator From, Loop::iterator To, LoopInfo &LI,
79 bool extractLoop(Loop *L, LoopInfo &LI, DominatorTree &DT);
83char LoopExtractorLegacyPass::ID = 0;
85 "Extract loops into new functions",
false,
false)
101char SingleLoopExtractor::ID = 0;
103 "Extract at most one loop into a new function",
false,
false)
110bool LoopExtractorLegacyPass::runOnModule(
Module &M) {
115 auto LookupDomTree = [
this](
Function &
F) -> DominatorTree & {
116 return this->getAnalysis<DominatorTreeWrapperPass>(
F).getDomTree();
119 return this->getAnalysis<LoopInfoWrapperPass>(
F, &
Changed).getLoopInfo();
121 auto LookupACT = [
this](
Function &
F) -> AssumptionCache * {
122 if (
auto *ACT = this->getAnalysisIfAvailable<AssumptionCacheTracker>())
123 return ACT->lookupAssumptionCache(
F);
126 return LoopExtractor(NumLoops, LookupDomTree, LookupLoopInfo, LookupACT)
131bool LoopExtractor::runOnModule(
Module &M) {
142 auto I =
M.begin(),
E = --
M.end();
159bool LoopExtractor::runOnFunction(Function &
F) {
168 LoopInfo &LI = LookupLoopInfo(
F);
174 DominatorTree &DT = LookupDomTree(
F);
178 if (std::next(LI.
begin()) != LI.
end())
182 Loop *TLL = *LI.
begin();
187 bool ShouldExtractLoop =
false;
190 Instruction *EntryTI =
F.getEntryBlock().getTerminator();
194 ShouldExtractLoop =
true;
198 SmallVector<BasicBlock *, 8> ExitBlocks;
200 for (
auto *ExitBlock : ExitBlocks)
202 ShouldExtractLoop =
true;
207 if (ShouldExtractLoop)
208 return Changed | extractLoop(TLL, LI, DT);
218bool LoopExtractor::extractLoops(Loop::iterator From, Loop::iterator To,
219 LoopInfo &LI, DominatorTree &DT) {
221 SmallVector<Loop *, 8>
Loops;
224 Loops.assign(From, To);
225 for (Loop *L :
Loops) {
227 if (!
L->isLoopSimplifyForm())
230 Changed |= extractLoop(L, LI, DT);
237bool LoopExtractor::extractLoop(Loop *L, LoopInfo &LI, DominatorTree &DT) {
240 AssumptionCache *AC = LookupAssumptionCache(Func);
241 CodeExtractorAnalysisCache CEAC(Func);
242 CodeExtractor Extractor(
L->getBlocks(), &DT,
false,
nullptr,
nullptr, AC);
243 if (Extractor.extractCodeRegion(CEAC)) {
256 return new SingleLoopExtractor();
270 if (!LoopExtractor(NumLoops, LookupDomTree, LookupLoopInfo,
271 LookupAssumptionCache)
283 OS, MapClassName2PassName);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static bool runOnFunction(Function &F, bool PostInlining)
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
Machine Check Debug Module
FunctionAnalysisManager FAM
#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)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM_ABI 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.
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.
LLVM_ABI 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
The legacy pass manager's analysis pass to compute loop information.
LLVM_ABI void erase(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
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...
A Module instance is used to store all the information related to an LLVM module.
Pass interface - Implemented by all 'passes'.
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.
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
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI char & LoopSimplifyID
LLVM_ABI Pass * createLoopExtractorPass()
createLoopExtractorPass - This pass extracts all natural loops from the program into a function if it...
LLVM_ABI char & BreakCriticalEdgesID
LLVM_ABI Pass * createSingleLoopExtractorPass()
createSingleLoopExtractorPass - This pass extracts one natural loop from the program into a function ...
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...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
A CRTP mix-in to automatically provide informational APIs needed for passes.