1 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // A pass wrapper around the ExtractLoop() scalar transformation to extract each
10 // top-level loop into its own new function. If the loop is the ONLY loop in a
11 // given function, it is not touched. This is a pass most useful for debugging
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/IPO/LoopExtractor.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Analysis/AssumptionCache.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/IR/Dominators.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/PassManager.h"
24 #include "llvm/InitializePasses.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Transforms/IPO.h"
28 #include "llvm/Transforms/Scalar.h"
29 #include "llvm/Transforms/Utils.h"
30 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
31 #include "llvm/Transforms/Utils/CodeExtractor.h"
36 #define DEBUG_TYPE "loop-extract"
38 STATISTIC(NumExtracted
, "Number of loops extracted");
41 struct LoopExtractorLegacyPass
: public ModulePass
{
42 static char ID
; // Pass identification, replacement for typeid
46 explicit LoopExtractorLegacyPass(unsigned NumLoops
= ~0)
47 : ModulePass(ID
), NumLoops(NumLoops
) {
48 initializeLoopExtractorLegacyPassPass(*PassRegistry::getPassRegistry());
51 bool runOnModule(Module
&M
) override
;
53 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
54 AU
.addRequiredID(BreakCriticalEdgesID
);
55 AU
.addRequired
<DominatorTreeWrapperPass
>();
56 AU
.addRequired
<LoopInfoWrapperPass
>();
57 AU
.addPreserved
<LoopInfoWrapperPass
>();
58 AU
.addRequiredID(LoopSimplifyID
);
59 AU
.addUsedIfAvailable
<AssumptionCacheTracker
>();
63 struct LoopExtractor
{
64 explicit LoopExtractor(
66 function_ref
<DominatorTree
&(Function
&)> LookupDomTree
,
67 function_ref
<LoopInfo
&(Function
&)> LookupLoopInfo
,
68 function_ref
<AssumptionCache
*(Function
&)> LookupAssumptionCache
)
69 : NumLoops(NumLoops
), LookupDomTree(LookupDomTree
),
70 LookupLoopInfo(LookupLoopInfo
),
71 LookupAssumptionCache(LookupAssumptionCache
) {}
72 bool runOnModule(Module
&M
);
75 // The number of natural loops to extract from the program into functions.
78 function_ref
<DominatorTree
&(Function
&)> LookupDomTree
;
79 function_ref
<LoopInfo
&(Function
&)> LookupLoopInfo
;
80 function_ref
<AssumptionCache
*(Function
&)> LookupAssumptionCache
;
82 bool runOnFunction(Function
&F
);
84 bool extractLoops(Loop::iterator From
, Loop::iterator To
, LoopInfo
&LI
,
86 bool extractLoop(Loop
*L
, LoopInfo
&LI
, DominatorTree
&DT
);
90 char LoopExtractorLegacyPass::ID
= 0;
91 INITIALIZE_PASS_BEGIN(LoopExtractorLegacyPass
, "loop-extract",
92 "Extract loops into new functions", false, false)
93 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges
)
94 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
95 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
)
96 INITIALIZE_PASS_DEPENDENCY(LoopSimplify
)
97 INITIALIZE_PASS_END(LoopExtractorLegacyPass
, "loop-extract",
98 "Extract loops into new functions", false, false)
101 /// SingleLoopExtractor - For bugpoint.
102 struct SingleLoopExtractor
: public LoopExtractorLegacyPass
{
103 static char ID
; // Pass identification, replacement for typeid
104 SingleLoopExtractor() : LoopExtractorLegacyPass(1) {}
106 } // End anonymous namespace
108 char SingleLoopExtractor::ID
= 0;
109 INITIALIZE_PASS(SingleLoopExtractor
, "loop-extract-single",
110 "Extract at most one loop into a new function", false, false)
112 // createLoopExtractorPass - This pass extracts all natural loops from the
113 // program into a function if it can.
115 Pass
*llvm::createLoopExtractorPass() { return new LoopExtractorLegacyPass(); }
117 bool LoopExtractorLegacyPass::runOnModule(Module
&M
) {
121 bool Changed
= false;
122 auto LookupDomTree
= [this](Function
&F
) -> DominatorTree
& {
123 return this->getAnalysis
<DominatorTreeWrapperPass
>(F
).getDomTree();
125 auto LookupLoopInfo
= [this, &Changed
](Function
&F
) -> LoopInfo
& {
126 return this->getAnalysis
<LoopInfoWrapperPass
>(F
, &Changed
).getLoopInfo();
128 auto LookupACT
= [this](Function
&F
) -> AssumptionCache
* {
129 if (auto *ACT
= this->getAnalysisIfAvailable
<AssumptionCacheTracker
>())
130 return ACT
->lookupAssumptionCache(F
);
133 return LoopExtractor(NumLoops
, LookupDomTree
, LookupLoopInfo
, LookupACT
)
138 bool LoopExtractor::runOnModule(Module
&M
) {
145 bool Changed
= false;
147 // The end of the function list may change (new functions will be added at the
148 // end), so we run from the first to the current last.
149 auto I
= M
.begin(), E
= --M
.end();
153 Changed
|= runOnFunction(F
);
157 // If this is the last function.
166 bool LoopExtractor::runOnFunction(Function
&F
) {
167 // Do not modify `optnone` functions.
174 bool Changed
= false;
175 LoopInfo
&LI
= LookupLoopInfo(F
);
177 // If there are no loops in the function.
181 DominatorTree
&DT
= LookupDomTree(F
);
183 // If there is more than one top-level loop in this function, extract all of
185 if (std::next(LI
.begin()) != LI
.end())
186 return Changed
| extractLoops(LI
.begin(), LI
.end(), LI
, DT
);
188 // Otherwise there is exactly one top-level loop.
189 Loop
*TLL
= *LI
.begin();
191 // If the loop is in LoopSimplify form, then extract it only if this function
192 // is more than a minimal wrapper around the loop.
193 if (TLL
->isLoopSimplifyForm()) {
194 bool ShouldExtractLoop
= false;
196 // Extract the loop if the entry block doesn't branch to the loop header.
197 Instruction
*EntryTI
= F
.getEntryBlock().getTerminator();
198 if (!isa
<BranchInst
>(EntryTI
) ||
199 !cast
<BranchInst
>(EntryTI
)->isUnconditional() ||
200 EntryTI
->getSuccessor(0) != TLL
->getHeader()) {
201 ShouldExtractLoop
= true;
203 // Check to see if any exits from the loop are more than just return
205 SmallVector
<BasicBlock
*, 8> ExitBlocks
;
206 TLL
->getExitBlocks(ExitBlocks
);
207 for (auto *ExitBlock
: ExitBlocks
)
208 if (!isa
<ReturnInst
>(ExitBlock
->getTerminator())) {
209 ShouldExtractLoop
= true;
214 if (ShouldExtractLoop
)
215 return Changed
| extractLoop(TLL
, LI
, DT
);
218 // Okay, this function is a minimal container around the specified loop.
219 // If we extract the loop, we will continue to just keep extracting it
220 // infinitely... so don't extract it. However, if the loop contains any
221 // sub-loops, extract them.
222 return Changed
| extractLoops(TLL
->begin(), TLL
->end(), LI
, DT
);
225 bool LoopExtractor::extractLoops(Loop::iterator From
, Loop::iterator To
,
226 LoopInfo
&LI
, DominatorTree
&DT
) {
227 bool Changed
= false;
228 SmallVector
<Loop
*, 8> Loops
;
230 // Save the list of loops, as it may change.
231 Loops
.assign(From
, To
);
232 for (Loop
*L
: Loops
) {
233 // If LoopSimplify form is not available, stay out of trouble.
234 if (!L
->isLoopSimplifyForm())
237 Changed
|= extractLoop(L
, LI
, DT
);
244 bool LoopExtractor::extractLoop(Loop
*L
, LoopInfo
&LI
, DominatorTree
&DT
) {
245 assert(NumLoops
!= 0);
246 Function
&Func
= *L
->getHeader()->getParent();
247 AssumptionCache
*AC
= LookupAssumptionCache(Func
);
248 CodeExtractorAnalysisCache
CEAC(Func
);
249 CodeExtractor
Extractor(DT
, *L
, false, nullptr, nullptr, AC
);
250 if (Extractor
.extractCodeRegion(CEAC
)) {
259 // createSingleLoopExtractorPass - This pass extracts one natural loop from the
260 // program into a function if it can. This is used by bugpoint.
262 Pass
*llvm::createSingleLoopExtractorPass() {
263 return new SingleLoopExtractor();
266 PreservedAnalyses
LoopExtractorPass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
267 auto &FAM
= AM
.getResult
<FunctionAnalysisManagerModuleProxy
>(M
).getManager();
268 auto LookupDomTree
= [&FAM
](Function
&F
) -> DominatorTree
& {
269 return FAM
.getResult
<DominatorTreeAnalysis
>(F
);
271 auto LookupLoopInfo
= [&FAM
](Function
&F
) -> LoopInfo
& {
272 return FAM
.getResult
<LoopAnalysis
>(F
);
274 auto LookupAssumptionCache
= [&FAM
](Function
&F
) -> AssumptionCache
* {
275 return FAM
.getCachedResult
<AssumptionAnalysis
>(F
);
277 if (!LoopExtractor(NumLoops
, LookupDomTree
, LookupLoopInfo
,
278 LookupAssumptionCache
)
280 return PreservedAnalyses::all();
282 PreservedAnalyses PA
;
283 PA
.preserve
<LoopAnalysis
>();