1 //===---- CodePreparation.cpp - Code preparation for Scop Detection -------===//
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 // The Polly code preparation pass is executed before SCoP detection. Its
10 // currently only splits the entry block of the SCoP to make room for alloc
11 // instructions as they are generated during code generation.
13 // XXX: In the future, we should remove the need for this pass entirely and
14 // instead add this spitting to the code generation pass.
16 //===----------------------------------------------------------------------===//
18 #include "polly/CodePreparation.h"
19 #include "polly/LinkAllPasses.h"
20 #include "polly/Support/ScopHelper.h"
21 #include "llvm/Analysis/DominanceFrontier.h"
22 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/Analysis/RegionInfo.h"
24 #include "llvm/Analysis/ScalarEvolution.h"
25 #include "llvm/InitializePasses.h"
28 using namespace polly
;
32 /// Prepare the IR for the scop detection.
34 class CodePreparation final
: public FunctionPass
{
35 CodePreparation(const CodePreparation
&) = delete;
36 const CodePreparation
&operator=(const CodePreparation
&) = delete;
46 explicit CodePreparation() : FunctionPass(ID
) {}
49 /// @name FunctionPass interface.
51 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
52 void releaseMemory() override
;
53 bool runOnFunction(Function
&F
) override
;
54 void print(raw_ostream
&OS
, const Module
*) const override
;
59 PreservedAnalyses
CodePreparationPass::run(Function
&F
,
60 FunctionAnalysisManager
&FAM
) {
62 // Find first non-alloca instruction. Every basic block has a non-alloca
63 // instruction, as every well formed basic block has a terminator.
64 auto &EntryBlock
= F
.getEntryBlock();
65 BasicBlock::iterator I
= EntryBlock
.begin();
66 while (isa
<AllocaInst
>(I
))
69 auto &DT
= FAM
.getResult
<DominatorTreeAnalysis
>(F
);
70 auto &LI
= FAM
.getResult
<LoopAnalysis
>(F
);
72 // splitBlock updates DT, LI and RI.
73 splitEntryBlockForAlloca(&EntryBlock
, &DT
, &LI
, nullptr);
76 PA
.preserve
<DominatorTreeAnalysis
>();
77 PA
.preserve
<LoopAnalysis
>();
81 void CodePreparation::clear() {}
83 CodePreparation::~CodePreparation() { clear(); }
85 void CodePreparation::getAnalysisUsage(AnalysisUsage
&AU
) const {
86 AU
.addRequired
<LoopInfoWrapperPass
>();
87 AU
.addRequired
<ScalarEvolutionWrapperPass
>();
89 AU
.addPreserved
<LoopInfoWrapperPass
>();
90 AU
.addPreserved
<RegionInfoPass
>();
91 AU
.addPreserved
<DominatorTreeWrapperPass
>();
92 AU
.addPreserved
<DominanceFrontierWrapperPass
>();
95 bool CodePreparation::runOnFunction(Function
&F
) {
99 LI
= &getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
100 SE
= &getAnalysis
<ScalarEvolutionWrapperPass
>().getSE();
102 splitEntryBlockForAlloca(&F
.getEntryBlock(), this);
107 void CodePreparation::releaseMemory() { clear(); }
109 void CodePreparation::print(raw_ostream
&OS
, const Module
*) const {}
111 char CodePreparation::ID
= 0;
112 char &polly::CodePreparationID
= CodePreparation::ID
;
114 Pass
*polly::createCodePreparationPass() { return new CodePreparation(); }
116 INITIALIZE_PASS_BEGIN(CodePreparation
, "polly-prepare",
117 "Polly - Prepare code for polly", false, false)
118 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
)
119 INITIALIZE_PASS_END(CodePreparation
, "polly-prepare",
120 "Polly - Prepare code for polly", false, false)