11
11
//
12
12
// ===----------------------------------------------------------------------===//
13
13
14
+ #include " llvm/Transforms/Scalar/LoopDataPrefetch.h"
15
+
14
16
#define DEBUG_TYPE " loop-data-prefetch"
15
17
#include " llvm/ADT/DepthFirstIterator.h"
16
18
#include " llvm/ADT/Statistic.h"
@@ -59,80 +61,89 @@ static cl::opt<unsigned> MaxPrefetchIterationsAhead(
59
61
60
62
STATISTIC (NumPrefetches, " Number of prefetches inserted" );
61
63
62
- namespace llvm {
63
- void initializeLoopDataPrefetchPass (PassRegistry&);
64
- }
65
-
66
64
namespace {
67
65
68
- class LoopDataPrefetch : public FunctionPass {
69
- public:
70
- static char ID; // Pass ID, replacement for typeid
71
- LoopDataPrefetch () : FunctionPass(ID) {
72
- initializeLoopDataPrefetchPass (*PassRegistry::getPassRegistry ());
73
- }
66
+ // / Loop prefetch implementation class.
67
+ class LoopDataPrefetch {
68
+ public:
69
+ LoopDataPrefetch (AssumptionCache *AC, LoopInfo *LI, ScalarEvolution *SE,
70
+ const TargetTransformInfo *TTI,
71
+ OptimizationRemarkEmitter *ORE)
72
+ : AC(AC), LI(LI), SE(SE), TTI(TTI), ORE(ORE) {}
74
73
75
- void getAnalysisUsage (AnalysisUsage &AU) const override {
76
- AU.addRequired <AssumptionCacheTracker>();
77
- AU.addPreserved <DominatorTreeWrapperPass>();
78
- AU.addRequired <LoopInfoWrapperPass>();
79
- AU.addPreserved <LoopInfoWrapperPass>();
80
- AU.addRequired <OptimizationRemarkEmitterWrapperPass>();
81
- AU.addRequired <ScalarEvolutionWrapperPass>();
82
- // FIXME: For some reason, preserving SE here breaks LSR (even if
83
- // this pass changes nothing).
84
- // AU.addPreserved<ScalarEvolutionWrapperPass>();
85
- AU.addRequired <TargetTransformInfoWrapperPass>();
86
- }
74
+ bool run ();
87
75
88
- bool runOnFunction (Function &F) override ;
76
+ private:
77
+ bool runOnLoop (Loop *L);
89
78
90
- private:
91
- bool runOnLoop (Loop *L);
79
+ // / \brief Check if the the stride of the accesses is large enough to
80
+ // / warrant a prefetch.
81
+ bool isStrideLargeEnough (const SCEVAddRecExpr *AR);
92
82
93
- // / \brief Check if the the stride of the accesses is large enough to
94
- // / warrant a prefetch.
95
- bool isStrideLargeEnough (const SCEVAddRecExpr *AR);
83
+ unsigned getMinPrefetchStride () {
84
+ if (MinPrefetchStride.getNumOccurrences () > 0 )
85
+ return MinPrefetchStride;
86
+ return TTI->getMinPrefetchStride ();
87
+ }
96
88
97
- unsigned getMinPrefetchStride () {
98
- if (MinPrefetchStride .getNumOccurrences () > 0 )
99
- return MinPrefetchStride ;
100
- return TTI->getMinPrefetchStride ();
101
- }
89
+ unsigned getPrefetchDistance () {
90
+ if (PrefetchDistance .getNumOccurrences () > 0 )
91
+ return PrefetchDistance ;
92
+ return TTI->getPrefetchDistance ();
93
+ }
102
94
103
- unsigned getPrefetchDistance () {
104
- if (PrefetchDistance .getNumOccurrences () > 0 )
105
- return PrefetchDistance ;
106
- return TTI->getPrefetchDistance ();
107
- }
95
+ unsigned getMaxPrefetchIterationsAhead () {
96
+ if (MaxPrefetchIterationsAhead .getNumOccurrences () > 0 )
97
+ return MaxPrefetchIterationsAhead ;
98
+ return TTI->getMaxPrefetchIterationsAhead ();
99
+ }
108
100
109
- unsigned getMaxPrefetchIterationsAhead () {
110
- if (MaxPrefetchIterationsAhead.getNumOccurrences () > 0 )
111
- return MaxPrefetchIterationsAhead;
112
- return TTI->getMaxPrefetchIterationsAhead ();
113
- }
101
+ AssumptionCache *AC;
102
+ LoopInfo *LI;
103
+ ScalarEvolution *SE;
104
+ const TargetTransformInfo *TTI;
105
+ OptimizationRemarkEmitter *ORE;
106
+ };
107
+
108
+ // / Legacy class for inserting loop data prefetches.
109
+ class LoopDataPrefetchLegacyPass : public FunctionPass {
110
+ public:
111
+ static char ID; // Pass ID, replacement for typeid
112
+ LoopDataPrefetchLegacyPass () : FunctionPass(ID) {
113
+ initializeLoopDataPrefetchLegacyPassPass (*PassRegistry::getPassRegistry ());
114
+ }
114
115
115
- AssumptionCache *AC;
116
- LoopInfo *LI;
117
- ScalarEvolution *SE;
118
- const TargetTransformInfo *TTI;
119
- const DataLayout *DL;
120
- OptimizationRemarkEmitter *ORE;
116
+ void getAnalysisUsage (AnalysisUsage &AU) const override {
117
+ AU.addRequired <AssumptionCacheTracker>();
118
+ AU.addPreserved <DominatorTreeWrapperPass>();
119
+ AU.addRequired <LoopInfoWrapperPass>();
120
+ AU.addPreserved <LoopInfoWrapperPass>();
121
+ AU.addRequired <OptimizationRemarkEmitterWrapperPass>();
122
+ AU.addRequired <ScalarEvolutionWrapperPass>();
123
+ // FIXME: For some reason, preserving SE here breaks LSR (even if
124
+ // this pass changes nothing).
125
+ // AU.addPreserved<ScalarEvolutionWrapperPass>();
126
+ AU.addRequired <TargetTransformInfoWrapperPass>();
127
+ }
128
+
129
+ bool runOnFunction (Function &F) override ;
121
130
};
122
131
}
123
132
124
- char LoopDataPrefetch ::ID = 0 ;
125
- INITIALIZE_PASS_BEGIN (LoopDataPrefetch , " loop-data-prefetch" ,
133
+ char LoopDataPrefetchLegacyPass ::ID = 0 ;
134
+ INITIALIZE_PASS_BEGIN (LoopDataPrefetchLegacyPass , " loop-data-prefetch" ,
126
135
" Loop Data Prefetch" , false , false )
127
136
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
128
137
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
129
138
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
130
139
INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
131
140
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
132
- INITIALIZE_PASS_END(LoopDataPrefetch , " loop-data-prefetch" ,
141
+ INITIALIZE_PASS_END(LoopDataPrefetchLegacyPass , " loop-data-prefetch" ,
133
142
" Loop Data Prefetch" , false , false )
134
143
135
- FunctionPass *llvm::createLoopDataPrefetchPass() { return new LoopDataPrefetch (); }
144
+ FunctionPass *llvm::createLoopDataPrefetchPass() {
145
+ return new LoopDataPrefetchLegacyPass ();
146
+ }
136
147
137
148
bool LoopDataPrefetch::isStrideLargeEnough (const SCEVAddRecExpr *AR) {
138
149
unsigned TargetMinStride = getMinPrefetchStride ();
@@ -150,17 +161,46 @@ bool LoopDataPrefetch::isStrideLargeEnough(const SCEVAddRecExpr *AR) {
150
161
return TargetMinStride <= AbsStride;
151
162
}
152
163
153
- bool LoopDataPrefetch::runOnFunction (Function &F) {
164
+ PreservedAnalyses LoopDataPrefetchPass::run (Function &F,
165
+ FunctionAnalysisManager &AM) {
166
+ LoopInfo *LI = &AM.getResult <LoopAnalysis>(F);
167
+ ScalarEvolution *SE = &AM.getResult <ScalarEvolutionAnalysis>(F);
168
+ AssumptionCache *AC = &AM.getResult <AssumptionAnalysis>(F);
169
+ OptimizationRemarkEmitter *ORE =
170
+ &AM.getResult <OptimizationRemarkEmitterAnalysis>(F);
171
+ const TargetTransformInfo *TTI = &AM.getResult <TargetIRAnalysis>(F);
172
+
173
+ LoopDataPrefetch LDP (AC, LI, SE, TTI, ORE);
174
+ bool Changed = LDP.run ();
175
+
176
+ if (Changed) {
177
+ PreservedAnalyses PA;
178
+ PA.preserve <DominatorTreeAnalysis>();
179
+ PA.preserve <LoopAnalysis>();
180
+ return PA;
181
+ }
182
+
183
+ return PreservedAnalyses::all ();
184
+ }
185
+
186
+ bool LoopDataPrefetchLegacyPass::runOnFunction (Function &F) {
154
187
if (skipFunction (F))
155
188
return false ;
156
189
157
- LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo ();
158
- SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE ();
159
- DL = &F.getParent ()->getDataLayout ();
160
- AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache (F);
161
- ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE ();
162
- TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI (F);
190
+ LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo ();
191
+ ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE ();
192
+ AssumptionCache *AC =
193
+ &getAnalysis<AssumptionCacheTracker>().getAssumptionCache (F);
194
+ OptimizationRemarkEmitter *ORE =
195
+ &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE ();
196
+ const TargetTransformInfo *TTI =
197
+ &getAnalysis<TargetTransformInfoWrapperPass>().getTTI (F);
198
+
199
+ LoopDataPrefetch LDP (AC, LI, SE, TTI, ORE);
200
+ return LDP.run ();
201
+ }
163
202
203
+ bool LoopDataPrefetch::run () {
164
204
// If PrefetchDistance is not set, don't run the pass. This gives an
165
205
// opportunity for targets to run this pass for selected subtargets only
166
206
// (whose TTI sets PrefetchDistance).
0 commit comments