blob: d6a8f48b5f6934bd9f50a34eb6a3279fb47d0952 [file] [log] [blame]
Adam Nemet9d9cb272016-02-18 21:38:19 +00001//===-------- LoopDataPrefetch.cpp - Loop Data Prefetching Pass -----------===//
Hal Finkele5aaf3f2015-02-20 05:08:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a Loop Data Prefetching Pass.
11//
12//===----------------------------------------------------------------------===//
13
Adam Nemet7cf9b1b2016-02-18 21:37:12 +000014#define DEBUG_TYPE "loop-data-prefetch"
Hal Finkele5aaf3f2015-02-20 05:08:21 +000015#include "llvm/Transforms/Scalar.h"
Hal Finkela9fceb82015-04-10 15:05:02 +000016#include "llvm/ADT/DepthFirstIterator.h"
Hal Finkele5aaf3f2015-02-20 05:08:21 +000017#include "llvm/ADT/Statistic.h"
18#include "llvm/Analysis/AssumptionCache.h"
19#include "llvm/Analysis/CodeMetrics.h"
20#include "llvm/Analysis/InstructionSimplify.h"
21#include "llvm/Analysis/LoopInfo.h"
22#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000023#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Hal Finkele5aaf3f2015-02-20 05:08:21 +000024#include "llvm/Analysis/ScalarEvolutionExpander.h"
25#include "llvm/Analysis/ScalarEvolutionExpressions.h"
26#include "llvm/Analysis/TargetTransformInfo.h"
27#include "llvm/Analysis/ValueTracking.h"
28#include "llvm/IR/CFG.h"
29#include "llvm/IR/Dominators.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/IntrinsicInst.h"
32#include "llvm/IR/Module.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
35#include "llvm/Transforms/Utils/BasicBlockUtils.h"
36#include "llvm/Transforms/Utils/Local.h"
37#include "llvm/Transforms/Utils/ValueMapper.h"
38using namespace llvm;
39
40// By default, we limit this to creating 16 PHIs (which is a little over half
41// of the allocatable register set).
42static cl::opt<bool>
Adam Nemet7cf9b1b2016-02-18 21:37:12 +000043PrefetchWrites("loop-prefetch-writes", cl::Hidden, cl::init(false),
Hal Finkele5aaf3f2015-02-20 05:08:21 +000044 cl::desc("Prefetch write addresses"));
45
Adam Nemet1428d412016-03-29 23:45:52 +000046static cl::opt<unsigned>
47 PrefetchDistance("prefetch-distance",
48 cl::desc("Number of instructions to prefetch ahead"),
49 cl::Hidden);
50
51static cl::opt<unsigned>
52 MinPrefetchStride("min-prefetch-stride",
53 cl::desc("Min stride to add prefetches"), cl::Hidden);
54
55static cl::opt<unsigned> MaxPrefetchIterationsAhead(
56 "max-prefetch-iters-ahead",
57 cl::desc("Max number of iterations to prefetch ahead"), cl::Hidden);
58
Adam Nemet34785ec2016-03-09 05:33:21 +000059STATISTIC(NumPrefetches, "Number of prefetches inserted");
60
Hal Finkele5aaf3f2015-02-20 05:08:21 +000061namespace llvm {
Adam Nemet7cf9b1b2016-02-18 21:37:12 +000062 void initializeLoopDataPrefetchPass(PassRegistry&);
Hal Finkele5aaf3f2015-02-20 05:08:21 +000063}
64
65namespace {
66
Adam Nemet7cf9b1b2016-02-18 21:37:12 +000067 class LoopDataPrefetch : public FunctionPass {
Hal Finkele5aaf3f2015-02-20 05:08:21 +000068 public:
69 static char ID; // Pass ID, replacement for typeid
Adam Nemet7cf9b1b2016-02-18 21:37:12 +000070 LoopDataPrefetch() : FunctionPass(ID) {
71 initializeLoopDataPrefetchPass(*PassRegistry::getPassRegistry());
Hal Finkele5aaf3f2015-02-20 05:08:21 +000072 }
73
74 void getAnalysisUsage(AnalysisUsage &AU) const override {
75 AU.addRequired<AssumptionCacheTracker>();
76 AU.addPreserved<DominatorTreeWrapperPass>();
77 AU.addRequired<LoopInfoWrapperPass>();
78 AU.addPreserved<LoopInfoWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +000079 AU.addRequired<ScalarEvolutionWrapperPass>();
Hal Finkele5aaf3f2015-02-20 05:08:21 +000080 // FIXME: For some reason, preserving SE here breaks LSR (even if
81 // this pass changes nothing).
Chandler Carruth2f1fd162015-08-17 02:08:17 +000082 // AU.addPreserved<ScalarEvolutionWrapperPass>();
Hal Finkele5aaf3f2015-02-20 05:08:21 +000083 AU.addRequired<TargetTransformInfoWrapperPass>();
84 }
85
86 bool runOnFunction(Function &F) override;
Adam Nemet85fba392016-03-29 22:40:02 +000087
88 private:
Hal Finkele5aaf3f2015-02-20 05:08:21 +000089 bool runOnLoop(Loop *L);
90
Adam Nemet6d8beec2016-03-18 00:27:38 +000091 /// \brief Check if the the stride of the accesses is large enough to
92 /// warrant a prefetch.
93 bool isStrideLargeEnough(const SCEVAddRecExpr *AR);
94
Adam Nemet1428d412016-03-29 23:45:52 +000095 unsigned getMinPrefetchStride() {
96 if (MinPrefetchStride.getNumOccurrences() > 0)
97 return MinPrefetchStride;
98 return TTI->getMinPrefetchStride();
99 }
100
101 unsigned getPrefetchDistance() {
102 if (PrefetchDistance.getNumOccurrences() > 0)
103 return PrefetchDistance;
104 return TTI->getPrefetchDistance();
105 }
106
107 unsigned getMaxPrefetchIterationsAhead() {
108 if (MaxPrefetchIterationsAhead.getNumOccurrences() > 0)
109 return MaxPrefetchIterationsAhead;
110 return TTI->getMaxPrefetchIterationsAhead();
111 }
112
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000113 AssumptionCache *AC;
114 LoopInfo *LI;
115 ScalarEvolution *SE;
116 const TargetTransformInfo *TTI;
117 const DataLayout *DL;
118 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000119}
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000120
Adam Nemet7cf9b1b2016-02-18 21:37:12 +0000121char LoopDataPrefetch::ID = 0;
122INITIALIZE_PASS_BEGIN(LoopDataPrefetch, "loop-data-prefetch",
123 "Loop Data Prefetch", false, false)
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000124INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
125INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
126INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000127INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Adam Nemet7cf9b1b2016-02-18 21:37:12 +0000128INITIALIZE_PASS_END(LoopDataPrefetch, "loop-data-prefetch",
129 "Loop Data Prefetch", false, false)
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000130
Adam Nemet9d9cb272016-02-18 21:38:19 +0000131FunctionPass *llvm::createLoopDataPrefetchPass() { return new LoopDataPrefetch(); }
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000132
Adam Nemet6d8beec2016-03-18 00:27:38 +0000133bool LoopDataPrefetch::isStrideLargeEnough(const SCEVAddRecExpr *AR) {
Adam Nemet1428d412016-03-29 23:45:52 +0000134 unsigned TargetMinStride = getMinPrefetchStride();
Adam Nemet6d8beec2016-03-18 00:27:38 +0000135 // No need to check if any stride goes.
136 if (TargetMinStride <= 1)
137 return true;
138
139 const auto *ConstStride = dyn_cast<SCEVConstant>(AR->getStepRecurrence(*SE));
140 // If MinStride is set, don't prefetch unless we can ensure that stride is
141 // larger.
142 if (!ConstStride)
143 return false;
144
145 unsigned AbsStride = std::abs(ConstStride->getAPInt().getSExtValue());
146 return TargetMinStride <= AbsStride;
147}
148
Adam Nemet7cf9b1b2016-02-18 21:37:12 +0000149bool LoopDataPrefetch::runOnFunction(Function &F) {
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000150 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000151 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Mehdi Amini46a43552015-03-04 18:43:29 +0000152 DL = &F.getParent()->getDataLayout();
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000153 AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
154 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
155
Adam Nemetbb3680b2016-03-07 18:35:42 +0000156 // If PrefetchDistance is not set, don't run the pass. This gives an
157 // opportunity for targets to run this pass for selected subtargets only
158 // (whose TTI sets PrefetchDistance).
Adam Nemet1428d412016-03-29 23:45:52 +0000159 if (getPrefetchDistance() == 0)
Adam Nemetbb3680b2016-03-07 18:35:42 +0000160 return false;
Adam Nemetaf761102016-01-21 18:28:36 +0000161 assert(TTI->getCacheLineSize() && "Cache line size is not set for target");
162
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000163 bool MadeChange = false;
164
Hal Finkel5551f252015-04-12 17:18:56 +0000165 for (auto I = LI->begin(), IE = LI->end(); I != IE; ++I)
166 for (auto L = df_begin(*I), LE = df_end(*I); L != LE; ++L)
167 MadeChange |= runOnLoop(*L);
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000168
169 return MadeChange;
170}
171
Adam Nemet7cf9b1b2016-02-18 21:37:12 +0000172bool LoopDataPrefetch::runOnLoop(Loop *L) {
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000173 bool MadeChange = false;
174
175 // Only prefetch in the inner-most loop
176 if (!L->empty())
177 return MadeChange;
178
179 SmallPtrSet<const Value *, 32> EphValues;
180 CodeMetrics::collectEphemeralValues(L, AC, EphValues);
181
182 // Calculate the number of iterations ahead to prefetch
183 CodeMetrics Metrics;
184 for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
185 I != IE; ++I) {
186
187 // If the loop already has prefetches, then assume that the user knows
188 // what he or she is doing and don't add any more.
189 for (BasicBlock::iterator J = (*I)->begin(), JE = (*I)->end();
190 J != JE; ++J)
191 if (CallInst *CI = dyn_cast<CallInst>(J))
192 if (Function *F = CI->getCalledFunction())
193 if (F->getIntrinsicID() == Intrinsic::prefetch)
194 return MadeChange;
195
196 Metrics.analyzeBasicBlock(*I, *TTI, EphValues);
197 }
198 unsigned LoopSize = Metrics.NumInsts;
199 if (!LoopSize)
200 LoopSize = 1;
201
Adam Nemet1428d412016-03-29 23:45:52 +0000202 unsigned ItersAhead = getPrefetchDistance() / LoopSize;
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000203 if (!ItersAhead)
204 ItersAhead = 1;
205
Adam Nemet1428d412016-03-29 23:45:52 +0000206 if (ItersAhead > getMaxPrefetchIterationsAhead())
Adam Nemet709e3042016-03-18 00:27:43 +0000207 return MadeChange;
208
Adam Nemet34785ec2016-03-09 05:33:21 +0000209 DEBUG(dbgs() << "Prefetching " << ItersAhead
210 << " iterations ahead (loop size: " << LoopSize << ") in "
211 << L->getHeader()->getParent()->getName() << ": " << *L);
212
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000213 SmallVector<std::pair<Instruction *, const SCEVAddRecExpr *>, 16> PrefLoads;
214 for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
215 I != IE; ++I) {
216 for (BasicBlock::iterator J = (*I)->begin(), JE = (*I)->end();
217 J != JE; ++J) {
218 Value *PtrValue;
219 Instruction *MemI;
220
221 if (LoadInst *LMemI = dyn_cast<LoadInst>(J)) {
222 MemI = LMemI;
223 PtrValue = LMemI->getPointerOperand();
224 } else if (StoreInst *SMemI = dyn_cast<StoreInst>(J)) {
225 if (!PrefetchWrites) continue;
226 MemI = SMemI;
227 PtrValue = SMemI->getPointerOperand();
228 } else continue;
229
230 unsigned PtrAddrSpace = PtrValue->getType()->getPointerAddressSpace();
231 if (PtrAddrSpace)
232 continue;
233
234 if (L->isLoopInvariant(PtrValue))
235 continue;
236
237 const SCEV *LSCEV = SE->getSCEV(PtrValue);
238 const SCEVAddRecExpr *LSCEVAddRec = dyn_cast<SCEVAddRecExpr>(LSCEV);
239 if (!LSCEVAddRec)
240 continue;
241
Adam Nemet6d8beec2016-03-18 00:27:38 +0000242 // Check if the the stride of the accesses is large enough to warrant a
243 // prefetch.
244 if (!isStrideLargeEnough(LSCEVAddRec))
245 continue;
246
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000247 // We don't want to double prefetch individual cache lines. If this load
248 // is known to be within one cache line of some other load that has
249 // already been prefetched, then don't prefetch this one as well.
250 bool DupPref = false;
251 for (SmallVector<std::pair<Instruction *, const SCEVAddRecExpr *>,
252 16>::iterator K = PrefLoads.begin(), KE = PrefLoads.end();
253 K != KE; ++K) {
254 const SCEV *PtrDiff = SE->getMinusSCEV(LSCEVAddRec, K->second);
255 if (const SCEVConstant *ConstPtrDiff =
256 dyn_cast<SCEVConstant>(PtrDiff)) {
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +0000257 int64_t PD = std::abs(ConstPtrDiff->getValue()->getSExtValue());
Adam Nemetaf761102016-01-21 18:28:36 +0000258 if (PD < (int64_t) TTI->getCacheLineSize()) {
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000259 DupPref = true;
260 break;
261 }
262 }
263 }
264 if (DupPref)
265 continue;
266
267 const SCEV *NextLSCEV = SE->getAddExpr(LSCEVAddRec, SE->getMulExpr(
268 SE->getConstant(LSCEVAddRec->getType(), ItersAhead),
269 LSCEVAddRec->getStepRecurrence(*SE)));
270 if (!isSafeToExpand(NextLSCEV, *SE))
271 continue;
272
273 PrefLoads.push_back(std::make_pair(MemI, LSCEVAddRec));
274
275 Type *I8Ptr = Type::getInt8PtrTy((*I)->getContext(), PtrAddrSpace);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000276 SCEVExpander SCEVE(*SE, J->getModule()->getDataLayout(), "prefaddr");
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000277 Value *PrefPtrValue = SCEVE.expandCodeFor(NextLSCEV, I8Ptr, MemI);
278
279 IRBuilder<> Builder(MemI);
280 Module *M = (*I)->getParent()->getParent();
281 Type *I32 = Type::getInt32Ty((*I)->getContext());
282 Value *PrefetchFunc = Intrinsic::getDeclaration(M, Intrinsic::prefetch);
David Blaikieff6409d2015-05-18 22:13:54 +0000283 Builder.CreateCall(
284 PrefetchFunc,
285 {PrefPtrValue,
286 ConstantInt::get(I32, MemI->mayReadFromMemory() ? 0 : 1),
287 ConstantInt::get(I32, 3), ConstantInt::get(I32, 1)});
Adam Nemet34785ec2016-03-09 05:33:21 +0000288 ++NumPrefetches;
289 DEBUG(dbgs() << " Access: " << *PtrValue << ", SCEV: " << *LSCEV
290 << "\n");
Hal Finkele5aaf3f2015-02-20 05:08:21 +0000291
292 MadeChange = true;
293 }
294 }
295
296 return MadeChange;
297}
298