1 //===---- CodePreparation.cpp - Code preparation for Scop Detection -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The Polly code preparation pass is executed before SCoP detection. Its
11 // currently only splits the entry block of the SCoP to make room for alloc
12 // instructions as they are generated during code generation.
14 // XXX: In the future, we should remove the need for this pass entirely and
15 // instead add this spitting to the code generation pass.
17 //===----------------------------------------------------------------------===//
19 #include "polly/CodePreparation.h"
20 #include "polly/LinkAllPasses.h"
21 #include "polly/ScopDetection.h"
22 #include "polly/Support/ScopHelper.h"
23 #include "llvm/Analysis/DominanceFrontier.h"
24 #include "llvm/Analysis/LoopInfo.h"
25 #include "llvm/Analysis/RegionInfo.h"
26 #include "llvm/Analysis/ScalarEvolution.h"
27 #include "llvm/Transforms/Utils/Local.h"
30 using namespace polly
;
34 /// Prepare the IR for the scop detection.
36 class CodePreparation
: public FunctionPass
{
37 CodePreparation(const CodePreparation
&) = delete;
38 const CodePreparation
&operator=(const CodePreparation
&) = delete;
48 explicit CodePreparation() : FunctionPass(ID
) {}
51 /// @name FunctionPass interface.
53 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const;
54 virtual void releaseMemory();
55 virtual bool runOnFunction(Function
&F
);
56 virtual void print(raw_ostream
&OS
, const Module
*) const;
61 PreservedAnalyses
CodePreparationPass::run(Function
&F
,
62 FunctionAnalysisManager
&FAM
) {
64 // Find first non-alloca instruction. Every basic block has a non-alloca
65 // instruction, as every well formed basic block has a terminator.
66 auto &EntryBlock
= F
.getEntryBlock();
67 BasicBlock::iterator I
= EntryBlock
.begin();
68 while (isa
<AllocaInst
>(I
))
71 auto &DT
= FAM
.getResult
<DominatorTreeAnalysis
>(F
);
72 auto &LI
= FAM
.getResult
<LoopAnalysis
>(F
);
74 // splitBlock updates DT, LI and RI.
75 splitEntryBlockForAlloca(&EntryBlock
, &DT
, &LI
, nullptr);
78 PA
.preserve
<DominatorTreeAnalysis
>();
79 PA
.preserve
<LoopAnalysis
>();
83 void CodePreparation::clear() {}
85 CodePreparation::~CodePreparation() { clear(); }
87 void CodePreparation::getAnalysisUsage(AnalysisUsage
&AU
) const {
88 AU
.addRequired
<LoopInfoWrapperPass
>();
89 AU
.addRequired
<ScalarEvolutionWrapperPass
>();
91 AU
.addPreserved
<LoopInfoWrapperPass
>();
92 AU
.addPreserved
<RegionInfoPass
>();
93 AU
.addPreserved
<DominatorTreeWrapperPass
>();
94 AU
.addPreserved
<DominanceFrontierWrapperPass
>();
97 bool CodePreparation::runOnFunction(Function
&F
) {
101 LI
= &getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
102 SE
= &getAnalysis
<ScalarEvolutionWrapperPass
>().getSE();
104 splitEntryBlockForAlloca(&F
.getEntryBlock(), this);
109 void CodePreparation::releaseMemory() { clear(); }
111 void CodePreparation::print(raw_ostream
&OS
, const Module
*) const {}
113 char CodePreparation::ID
= 0;
114 char &polly::CodePreparationID
= CodePreparation::ID
;
116 Pass
*polly::createCodePreparationPass() { return new CodePreparation(); }
118 INITIALIZE_PASS_BEGIN(CodePreparation
, "polly-prepare",
119 "Polly - Prepare code for polly", false, false)
120 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
)
121 INITIALIZE_PASS_END(CodePreparation
, "polly-prepare",
122 "Polly - Prepare code for polly", false, false)