7
7
//
8
8
// ===----------------------------------------------------------------------===//
9
9
//
10
- // This file implement the code preparation for Scop detect, which will:
11
- // 1. Translate all PHINodes that not induction variable to memory access,
12
- // this will easier parameter and scalar dependencies checking.
10
+ // The Polly code preparation pass is executed before SCoP detection. Its only
11
+ // use is to translate all PHI nodes that can not be expressed by the code
12
+ // generator into explicit memory dependences. Depending of the code generation
13
+ // strategy different PHI nodes are translated:
14
+ //
15
+ // - indvars based code generation:
16
+ //
17
+ // The indvars based code generation requires explicit canonical induction
18
+ // variables. Such variables are generated before scop detection and
19
+ // also before the code preparation pass. All PHI nodes that are not canonical
20
+ // induction variables are not supported by the indvars based code generation
21
+ // and are consequently translated into explict memory accesses.
22
+ //
23
+ // - scev based code generation:
24
+ //
25
+ // The scev based code generation can code generate all PHI nodes that do not
26
+ // reference parameters within the scop. As the code preparation pass is run
27
+ // before scop detection, we can not check this condition, because without
28
+ // a detected scop, we do not know SCEVUnknowns that appear in the SCEV of
29
+ // a PHI node may later be within or outside of the SCoP. Hence, we follow a
30
+ // heuristic and translate all PHI nodes that are either directly SCEVUnknown
31
+ // or SCEVCouldNotCompute. This will hopefully get most of the PHI nodes that
32
+ // are introduced due to conditional control flow, but not the ones that are
33
+ // referencing loop counters.
34
+ //
35
+ // XXX: In the future, we should remove the need for this pass entirely and
36
+ // instead add support for scalar dependences to ScopInfo and code generation.
13
37
//
14
38
// ===----------------------------------------------------------------------===//
39
+
15
40
#include " polly/LinkAllPasses.h"
16
41
#include " polly/CodeGen/BlockGenerators.h"
17
42
#include " polly/Support/ScopHelper.h"
18
43
19
- #include " llvm/IR/Instruction.h"
20
- #include " llvm/Pass.h"
21
- #include " llvm/Assembly/Writer.h"
22
44
#include " llvm/Analysis/LoopInfo.h"
23
45
#include " llvm/Analysis/RegionInfo.h"
24
- #include " llvm/Analysis/Dominators.h"
25
46
#include " llvm/Analysis/ScalarEvolution.h"
26
- #include " llvm/ADT/DenseMap.h"
27
- #include " llvm/ADT/PointerIntPair.h"
28
- #include " llvm/Support/InstIterator.h"
29
47
#include " llvm/Transforms/Utils/Local.h"
30
48
31
- #define DEBUG_TYPE " polly-code-prep"
32
- #include " llvm/Support/Debug.h"
33
-
34
-
35
49
using namespace llvm ;
36
50
using namespace polly ;
37
51
38
52
namespace {
39
- // ===----------------------------------------------------------------------===//
40
- // / @brief Scop Code Preparation - Perform some transforms to make scop detect
41
- // / easier.
53
+ // / @brief Prepare the IR for the scop detection.
42
54
// /
43
55
class CodePreparation : public FunctionPass {
44
56
// DO NOT IMPLEMENT.
45
57
CodePreparation (const CodePreparation &);
46
58
// DO NOT IMPLEMENT.
47
59
const CodePreparation &operator =(const CodePreparation &);
48
60
49
- // LoopInfo to compute canonical induction variable.
50
61
LoopInfo *LI;
51
62
ScalarEvolution *SE;
52
63
53
- // Clear the context.
54
64
void clear ();
55
65
56
66
bool eliminatePHINodes (Function &F);
@@ -72,66 +82,58 @@ class CodePreparation : public FunctionPass {
72
82
};
73
83
}
74
84
75
- // ===----------------------------------------------------------------------===//
76
- // / CodePreparation implement.
77
-
78
- void CodePreparation::clear () {
79
- }
85
+ void CodePreparation::clear () {}
80
86
81
- CodePreparation::~CodePreparation () {
82
- clear ();
83
- }
87
+ CodePreparation::~CodePreparation () { clear (); }
84
88
85
89
bool CodePreparation::eliminatePHINodes (Function &F) {
86
90
// The PHINodes that will be deleted.
87
- std::vector<PHINode*> PNtoDel ;
91
+ std::vector<PHINode *> PNtoDelete ;
88
92
// The PHINodes that will be preserved.
89
- std::vector<PHINode*> PreservedPNs;
93
+ std::vector<PHINode *> PreservedPNs;
90
94
91
95
// Scan the PHINodes in this function.
92
- for (Function::iterator ibb = F.begin (), ibe = F.end ();
93
- ibb != ibe; ++ibb)
94
- for (BasicBlock::iterator iib = ibb->begin (), iie = ibb->getFirstNonPHI ();
95
- iib != iie; ++iib)
96
- if (PHINode *PN = cast<PHINode>(iib)) {
97
- if (SCEVCodegen) {
98
- if (SE->isSCEVable (PN->getType ())) {
99
- const SCEV *S = SE->getSCEV (PN);
100
- if (!isa<SCEVUnknown>(S) && !isa<SCEVCouldNotCompute>(S)) {
101
- PreservedPNs.push_back (PN);
102
- continue ;
103
- }
96
+ for (Function::iterator BI = F.begin (), BE = F.end (); BI != BE; ++BI)
97
+ for (BasicBlock::iterator II = BI->begin (), IE = BI->getFirstNonPHI ();
98
+ II != IE; ++II) {
99
+ PHINode *PN = cast<PHINode>(II);
100
+ if (SCEVCodegen) {
101
+ if (SE->isSCEVable (PN->getType ())) {
102
+ const SCEV *S = SE->getSCEV (PN);
103
+ if (!isa<SCEVUnknown>(S) && !isa<SCEVCouldNotCompute>(S)) {
104
+ PreservedPNs.push_back (PN);
105
+ continue ;
104
106
}
105
- } else {
106
- if (Loop *L = LI-> getLoopFor (ibb)) {
107
- // Induction variable will be preserved.
108
- if (L-> getCanonicalInductionVariable () == PN) {
109
- PreservedPNs. push_back ( PN);
110
- continue ;
111
- }
107
+ }
108
+ } else {
109
+ if (Loop *L = LI-> getLoopFor (BI)) {
110
+ // Induction variables will be preserved.
111
+ if (L-> getCanonicalInductionVariable () == PN) {
112
+ PreservedPNs. push_back (PN) ;
113
+ continue ;
112
114
}
113
115
}
114
-
115
- // As DemotePHIToStack does not support invoke edges, we preserve
116
- // PHINodes that have invoke edges.
117
- if (hasInvokeEdge (PN))
118
- PreservedPNs.push_back (PN);
119
- else
120
- PNtoDel.push_back (PN);
121
116
}
122
117
123
- if (PNtoDel.empty ())
118
+ // As DemotePHIToStack does not support invoke edges, we preserve
119
+ // PHINodes that have invoke edges.
120
+ if (hasInvokeEdge (PN))
121
+ PreservedPNs.push_back (PN);
122
+ else
123
+ PNtoDelete.push_back (PN);
124
+ }
125
+
126
+ if (PNtoDelete.empty ())
124
127
return false ;
125
128
126
- // Eliminate the PHINodes that not an Induction variable.
127
- while (!PNtoDel.empty ()) {
128
- PHINode *PN = PNtoDel.back ();
129
- PNtoDel.pop_back ();
129
+ while (!PNtoDelete.empty ()) {
130
+ PHINode *PN = PNtoDelete.back ();
131
+ PNtoDelete.pop_back ();
130
132
131
133
DemotePHIToStack (PN);
132
134
}
133
135
134
- // Move all preserved PHINodes to the beginning of the BasicBlock.
136
+ // Move preserved PHINodes to the beginning of the BasicBlock.
135
137
while (!PreservedPNs.empty ()) {
136
138
PHINode *PN = PreservedPNs.back ();
137
139
PreservedPNs.pop_back ();
@@ -167,22 +169,18 @@ bool CodePreparation::runOnFunction(Function &F) {
167
169
return false ;
168
170
}
169
171
170
- void CodePreparation::releaseMemory () {
171
- clear ();
172
- }
172
+ void CodePreparation::releaseMemory () { clear (); }
173
173
174
- void CodePreparation::print (raw_ostream &OS, const Module *) const {
175
- }
174
+ void CodePreparation::print (raw_ostream &OS, const Module *) const {}
176
175
177
176
char CodePreparation::ID = 0 ;
178
177
char &polly::CodePreparationID = CodePreparation::ID;
179
178
179
+ Pass *polly::createCodePreparationPass () { return new CodePreparation (); }
180
+
180
181
INITIALIZE_PASS_BEGIN (CodePreparation, " polly-prepare" ,
181
- " Polly - Prepare code for polly" , false , false )
182
- INITIALIZE_PASS_DEPENDENCY(LoopInfo)
182
+ " Polly - Prepare code for polly" , false , false );
183
+ INITIALIZE_PASS_DEPENDENCY (LoopInfo);
183
184
INITIALIZE_PASS_END (CodePreparation, " polly-prepare" ,
184
- " Polly - Prepare code for polly" , false , false )
185
+ " Polly - Prepare code for polly" , false , false )
185
186
186
- Pass *polly::createCodePreparationPass() {
187
- return new CodePreparation ();
188
- }
0 commit comments