blob: 92f00273a2cb86dc29af8c949495a392f3055750 [file] [log] [blame]
Dan Gohman81db61a2009-05-12 02:17:14 +00001//===- IVUsers.cpp - Induction Variable Users -------------------*- C++ -*-===//
2//
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 bookkeeping for "interesting" users of expressions
11// computed from induction variables.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "iv-users"
16#include "llvm/Analysis/IVUsers.h"
17#include "llvm/Constants.h"
18#include "llvm/Instructions.h"
19#include "llvm/Type.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Analysis/Dominators.h"
Dan Gohman81db61a2009-05-12 02:17:14 +000022#include "llvm/Analysis/LoopPass.h"
23#include "llvm/Analysis/ScalarEvolutionExpressions.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
27#include <algorithm>
28using namespace llvm;
29
30char IVUsers::ID = 0;
31static RegisterPass<IVUsers>
32X("iv-users", "Induction Variable Users", false, true);
33
34Pass *llvm::createIVUsersPass() {
35 return new IVUsers();
36}
37
38/// containsAddRecFromDifferentLoop - Determine whether expression S involves a
39/// subexpression that is an AddRec from a loop other than L. An outer loop
40/// of L is OK, but not an inner loop nor a disjoint loop.
Dan Gohman0bba49c2009-07-07 17:06:11 +000041static bool containsAddRecFromDifferentLoop(const SCEV *S, Loop *L) {
Dan Gohman81db61a2009-05-12 02:17:14 +000042 // This is very common, put it first.
43 if (isa<SCEVConstant>(S))
44 return false;
45 if (const SCEVCommutativeExpr *AE = dyn_cast<SCEVCommutativeExpr>(S)) {
46 for (unsigned int i=0; i< AE->getNumOperands(); i++)
47 if (containsAddRecFromDifferentLoop(AE->getOperand(i), L))
48 return true;
49 return false;
50 }
51 if (const SCEVAddRecExpr *AE = dyn_cast<SCEVAddRecExpr>(S)) {
52 if (const Loop *newLoop = AE->getLoop()) {
53 if (newLoop == L)
54 return false;
55 // if newLoop is an outer loop of L, this is OK.
Dan Gohman92329c72009-12-18 01:24:09 +000056 if (newLoop->contains(L))
Dan Gohman81db61a2009-05-12 02:17:14 +000057 return false;
58 }
59 return true;
60 }
61 if (const SCEVUDivExpr *DE = dyn_cast<SCEVUDivExpr>(S))
62 return containsAddRecFromDifferentLoop(DE->getLHS(), L) ||
63 containsAddRecFromDifferentLoop(DE->getRHS(), L);
64#if 0
65 // SCEVSDivExpr has been backed out temporarily, but will be back; we'll
66 // need this when it is.
67 if (const SCEVSDivExpr *DE = dyn_cast<SCEVSDivExpr>(S))
68 return containsAddRecFromDifferentLoop(DE->getLHS(), L) ||
69 containsAddRecFromDifferentLoop(DE->getRHS(), L);
70#endif
71 if (const SCEVCastExpr *CE = dyn_cast<SCEVCastExpr>(S))
72 return containsAddRecFromDifferentLoop(CE->getOperand(), L);
73 return false;
74}
75
76/// getSCEVStartAndStride - Compute the start and stride of this expression,
77/// returning false if the expression is not a start/stride pair, or true if it
78/// is. The stride must be a loop invariant expression, but the start may be
79/// a mix of loop invariant and loop variant expressions. The start cannot,
80/// however, contain an AddRec from a different loop, unless that loop is an
81/// outer loop of the current loop.
Dan Gohman0bba49c2009-07-07 17:06:11 +000082static bool getSCEVStartAndStride(const SCEV *&SH, Loop *L, Loop *UseLoop,
83 const SCEV *&Start, const SCEV *&Stride,
Dan Gohman81db61a2009-05-12 02:17:14 +000084 ScalarEvolution *SE, DominatorTree *DT) {
Dan Gohman0bba49c2009-07-07 17:06:11 +000085 const SCEV *TheAddRec = Start; // Initialize to zero.
Dan Gohman81db61a2009-05-12 02:17:14 +000086
87 // If the outer level is an AddExpr, the operands are all start values except
88 // for a nested AddRecExpr.
89 if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) {
90 for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i)
91 if (const SCEVAddRecExpr *AddRec =
92 dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) {
93 if (AddRec->getLoop() == L)
94 TheAddRec = SE->getAddExpr(AddRec, TheAddRec);
95 else
96 return false; // Nested IV of some sort?
97 } else {
98 Start = SE->getAddExpr(Start, AE->getOperand(i));
99 }
Dan Gohman81db61a2009-05-12 02:17:14 +0000100 } else if (isa<SCEVAddRecExpr>(SH)) {
101 TheAddRec = SH;
102 } else {
103 return false; // not analyzable.
104 }
105
106 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec);
107 if (!AddRec || AddRec->getLoop() != L) return false;
108
109 // Use getSCEVAtScope to attempt to simplify other loops out of
110 // the picture.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000111 const SCEV *AddRecStart = AddRec->getStart();
Dan Gohman743e41e2009-06-15 18:38:59 +0000112 AddRecStart = SE->getSCEVAtScope(AddRecStart, UseLoop);
Dan Gohman0bba49c2009-07-07 17:06:11 +0000113 const SCEV *AddRecStride = AddRec->getStepRecurrence(*SE);
Dan Gohman81db61a2009-05-12 02:17:14 +0000114
115 // FIXME: If Start contains an SCEVAddRecExpr from a different loop, other
116 // than an outer loop of the current loop, reject it. LSR has no concept of
117 // operating on more than one loop at a time so don't confuse it with such
118 // expressions.
119 if (containsAddRecFromDifferentLoop(AddRecStart, L))
120 return false;
121
Dan Gohman81db61a2009-05-12 02:17:14 +0000122 Start = SE->getAddExpr(Start, AddRecStart);
123
Dan Gohman00cb6732009-09-27 15:30:00 +0000124 // If stride is an instruction, make sure it properly dominates the header.
Dan Gohman743e41e2009-06-15 18:38:59 +0000125 // Otherwise we could end up with a use before def situation.
126 if (!isa<SCEVConstant>(AddRecStride)) {
Dan Gohman00cb6732009-09-27 15:30:00 +0000127 BasicBlock *Header = L->getHeader();
128 if (!AddRecStride->properlyDominates(Header, DT))
Dan Gohman81db61a2009-05-12 02:17:14 +0000129 return false;
130
Dan Gohman30733292010-01-09 18:17:45 +0000131 DEBUG(dbgs() << "[";
132 WriteAsOperand(dbgs(), L->getHeader(), /*PrintType=*/false);
133 dbgs() << "] Variable stride: " << *AddRec << "\n");
Dan Gohman81db61a2009-05-12 02:17:14 +0000134 }
135
Dan Gohman743e41e2009-06-15 18:38:59 +0000136 Stride = AddRecStride;
Dan Gohman81db61a2009-05-12 02:17:14 +0000137 return true;
138}
139
140/// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression
141/// and now we need to decide whether the user should use the preinc or post-inc
142/// value. If this user should use the post-inc version of the IV, return true.
143///
144/// Choosing wrong here can break dominance properties (if we choose to use the
145/// post-inc value when we cannot) or it can end up adding extra live-ranges to
146/// the loop, resulting in reg-reg copies (if we use the pre-inc value when we
147/// should use the post-inc value).
148static bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV,
149 Loop *L, LoopInfo *LI, DominatorTree *DT,
150 Pass *P) {
151 // If the user is in the loop, use the preinc value.
Dan Gohman92329c72009-12-18 01:24:09 +0000152 if (L->contains(User)) return false;
Dan Gohman81db61a2009-05-12 02:17:14 +0000153
154 BasicBlock *LatchBlock = L->getLoopLatch();
Dan Gohmana6a4aae2009-11-05 19:41:37 +0000155 if (!LatchBlock)
156 return false;
Dan Gohman81db61a2009-05-12 02:17:14 +0000157
158 // Ok, the user is outside of the loop. If it is dominated by the latch
159 // block, use the post-inc value.
160 if (DT->dominates(LatchBlock, User->getParent()))
161 return true;
162
163 // There is one case we have to be careful of: PHI nodes. These little guys
164 // can live in blocks that are not dominated by the latch block, but (since
165 // their uses occur in the predecessor block, not the block the PHI lives in)
166 // should still use the post-inc value. Check for this case now.
167 PHINode *PN = dyn_cast<PHINode>(User);
168 if (!PN) return false; // not a phi, not dominated by latch block.
169
170 // Look at all of the uses of IV by the PHI node. If any use corresponds to
171 // a block that is not dominated by the latch block, give up and use the
172 // preincremented value.
173 unsigned NumUses = 0;
174 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
175 if (PN->getIncomingValue(i) == IV) {
176 ++NumUses;
177 if (!DT->dominates(LatchBlock, PN->getIncomingBlock(i)))
178 return false;
179 }
180
181 // Okay, all uses of IV by PN are in predecessor blocks that really are
182 // dominated by the latch block. Use the post-incremented value.
183 return true;
184}
185
186/// AddUsersIfInteresting - Inspect the specified instruction. If it is a
187/// reducible SCEV, recursively add its users to the IVUsesByStride set and
188/// return true. Otherwise, return false.
189bool IVUsers::AddUsersIfInteresting(Instruction *I) {
190 if (!SE->isSCEVable(I->getType()))
191 return false; // Void and FP expressions cannot be reduced.
192
193 // LSR is not APInt clean, do not touch integers bigger than 64-bits.
194 if (SE->getTypeSizeInBits(I->getType()) > 64)
195 return false;
196
197 if (!Processed.insert(I))
198 return true; // Instruction already handled.
199
200 // Get the symbolic expression for this instruction.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000201 const SCEV *ISE = SE->getSCEV(I);
Dan Gohman81db61a2009-05-12 02:17:14 +0000202 if (isa<SCEVCouldNotCompute>(ISE)) return false;
203
204 // Get the start and stride for this expression.
205 Loop *UseLoop = LI->getLoopFor(I->getParent());
Dan Gohman0bba49c2009-07-07 17:06:11 +0000206 const SCEV *Start = SE->getIntegerSCEV(0, ISE->getType());
207 const SCEV *Stride = Start;
Dan Gohman81db61a2009-05-12 02:17:14 +0000208
Dan Gohman4e8a9852009-06-18 16:54:06 +0000209 if (!getSCEVStartAndStride(ISE, L, UseLoop, Start, Stride, SE, DT))
Dan Gohman81db61a2009-05-12 02:17:14 +0000210 return false; // Non-reducible symbolic expression, bail out.
211
Jim Grosbach97200e42009-11-19 02:05:44 +0000212 // Keep things simple. Don't touch loop-variant strides.
Dan Gohman92329c72009-12-18 01:24:09 +0000213 if (!Stride->isLoopInvariant(L) && L->contains(I))
Jim Grosbach97200e42009-11-19 02:05:44 +0000214 return false;
215
Dan Gohman81db61a2009-05-12 02:17:14 +0000216 SmallPtrSet<Instruction *, 4> UniqueUsers;
217 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
218 UI != E; ++UI) {
219 Instruction *User = cast<Instruction>(*UI);
220 if (!UniqueUsers.insert(User))
221 continue;
222
223 // Do not infinitely recurse on PHI nodes.
224 if (isa<PHINode>(User) && Processed.count(User))
225 continue;
226
227 // Descend recursively, but not into PHI nodes outside the current loop.
228 // It's important to see the entire expression outside the loop to get
229 // choices that depend on addressing mode use right, although we won't
230 // consider references ouside the loop in all cases.
231 // If User is already in Processed, we don't want to recurse into it again,
232 // but do want to record a second reference in the same instruction.
233 bool AddUserToIVUsers = false;
234 if (LI->getLoopFor(User->getParent()) != L) {
235 if (isa<PHINode>(User) || Processed.count(User) ||
236 !AddUsersIfInteresting(User)) {
David Greene63c45602009-12-23 20:20:46 +0000237 DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n'
Chris Lattnerbdff5482009-08-23 04:37:46 +0000238 << " OF SCEV: " << *ISE << '\n');
Dan Gohman81db61a2009-05-12 02:17:14 +0000239 AddUserToIVUsers = true;
240 }
241 } else if (Processed.count(User) ||
242 !AddUsersIfInteresting(User)) {
David Greene63c45602009-12-23 20:20:46 +0000243 DEBUG(dbgs() << "FOUND USER: " << *User << '\n'
Chris Lattnerbdff5482009-08-23 04:37:46 +0000244 << " OF SCEV: " << *ISE << '\n');
Dan Gohman81db61a2009-05-12 02:17:14 +0000245 AddUserToIVUsers = true;
246 }
247
248 if (AddUserToIVUsers) {
249 IVUsersOfOneStride *StrideUses = IVUsesByStride[Stride];
250 if (!StrideUses) { // First occurrence of this stride?
251 StrideOrder.push_back(Stride);
252 StrideUses = new IVUsersOfOneStride(Stride);
253 IVUses.push_back(StrideUses);
254 IVUsesByStride[Stride] = StrideUses;
255 }
256
257 // Okay, we found a user that we cannot reduce. Analyze the instruction
258 // and decide what to do with it. If we are a use inside of the loop, use
259 // the value before incrementation, otherwise use it after incrementation.
260 if (IVUseShouldUsePostIncValue(User, I, L, LI, DT, this)) {
261 // The value used will be incremented by the stride more than we are
262 // expecting, so subtract this off.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000263 const SCEV *NewStart = SE->getMinusSCEV(Start, Stride);
Dan Gohman4e8a9852009-06-18 16:54:06 +0000264 StrideUses->addUser(NewStart, User, I);
Dan Gohman81db61a2009-05-12 02:17:14 +0000265 StrideUses->Users.back().setIsUseOfPostIncrementedValue(true);
David Greene63c45602009-12-23 20:20:46 +0000266 DEBUG(dbgs() << " USING POSTINC SCEV, START=" << *NewStart<< "\n");
Dan Gohman81db61a2009-05-12 02:17:14 +0000267 } else {
Dan Gohman4e8a9852009-06-18 16:54:06 +0000268 StrideUses->addUser(Start, User, I);
Dan Gohman81db61a2009-05-12 02:17:14 +0000269 }
270 }
271 }
272 return true;
273}
274
Evan Cheng586f69a2009-11-12 07:35:05 +0000275void IVUsers::AddUser(const SCEV *Stride, const SCEV *Offset,
276 Instruction *User, Value *Operand) {
277 IVUsersOfOneStride *StrideUses = IVUsesByStride[Stride];
278 if (!StrideUses) { // First occurrence of this stride?
279 StrideOrder.push_back(Stride);
280 StrideUses = new IVUsersOfOneStride(Stride);
281 IVUses.push_back(StrideUses);
282 IVUsesByStride[Stride] = StrideUses;
283 }
284 IVUsesByStride[Stride]->addUser(Offset, User, Operand);
285}
286
Dan Gohman81db61a2009-05-12 02:17:14 +0000287IVUsers::IVUsers()
288 : LoopPass(&ID) {
289}
290
291void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const {
292 AU.addRequired<LoopInfo>();
293 AU.addRequired<DominatorTree>();
294 AU.addRequired<ScalarEvolution>();
295 AU.setPreservesAll();
296}
297
298bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
299
300 L = l;
301 LI = &getAnalysis<LoopInfo>();
302 DT = &getAnalysis<DominatorTree>();
303 SE = &getAnalysis<ScalarEvolution>();
304
305 // Find all uses of induction variables in this loop, and categorize
306 // them by stride. Start by finding all of the PHI nodes in the header for
307 // this loop. If they are induction variables, inspect their uses.
308 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
309 AddUsersIfInteresting(I);
310
311 return false;
312}
313
314/// getReplacementExpr - Return a SCEV expression which computes the
315/// value of the OperandValToReplace of the given IVStrideUse.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000316const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &U) const {
Dan Gohman81db61a2009-05-12 02:17:14 +0000317 // Start with zero.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000318 const SCEV *RetVal = SE->getIntegerSCEV(0, U.getParent()->Stride->getType());
Dan Gohman81db61a2009-05-12 02:17:14 +0000319 // Create the basic add recurrence.
320 RetVal = SE->getAddRecExpr(RetVal, U.getParent()->Stride, L);
321 // Add the offset in a separate step, because it may be loop-variant.
322 RetVal = SE->getAddExpr(RetVal, U.getOffset());
323 // For uses of post-incremented values, add an extra stride to compute
324 // the actual replacement value.
325 if (U.isUseOfPostIncrementedValue())
326 RetVal = SE->getAddExpr(RetVal, U.getParent()->Stride);
327 // Evaluate the expression out of the loop, if possible.
Dan Gohman92329c72009-12-18 01:24:09 +0000328 if (!L->contains(U.getUser())) {
Dan Gohman0bba49c2009-07-07 17:06:11 +0000329 const SCEV *ExitVal = SE->getSCEVAtScope(RetVal, L->getParentLoop());
Dan Gohman743e41e2009-06-15 18:38:59 +0000330 if (ExitVal->isLoopInvariant(L))
Dan Gohman81db61a2009-05-12 02:17:14 +0000331 RetVal = ExitVal;
332 }
Dan Gohman81db61a2009-05-12 02:17:14 +0000333 return RetVal;
334}
335
Dan Gohmanbafbbdd2010-01-19 21:55:32 +0000336/// getCanonicalExpr - Return a SCEV expression which computes the
337/// value of the SCEV of the given IVStrideUse, ignoring the
338/// isUseOfPostIncrementedValue flag.
339const SCEV *IVUsers::getCanonicalExpr(const IVStrideUse &U) const {
340 // Start with zero.
341 const SCEV *RetVal = SE->getIntegerSCEV(0, U.getParent()->Stride->getType());
342 // Create the basic add recurrence.
343 RetVal = SE->getAddRecExpr(RetVal, U.getParent()->Stride, L);
344 // Add the offset in a separate step, because it may be loop-variant.
345 RetVal = SE->getAddExpr(RetVal, U.getOffset());
346 return RetVal;
347}
348
Dan Gohman81db61a2009-05-12 02:17:14 +0000349void IVUsers::print(raw_ostream &OS, const Module *M) const {
350 OS << "IV Users for loop ";
351 WriteAsOperand(OS, L->getHeader(), false);
352 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
353 OS << " with backedge-taken count "
354 << *SE->getBackedgeTakenCount(L);
355 }
356 OS << ":\n";
357
358 for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) {
Dan Gohman0bba49c2009-07-07 17:06:11 +0000359 std::map<const SCEV *, IVUsersOfOneStride*>::const_iterator SI =
Dan Gohman81db61a2009-05-12 02:17:14 +0000360 IVUsesByStride.find(StrideOrder[Stride]);
361 assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
362 OS << " Stride " << *SI->first->getType() << " " << *SI->first << ":\n";
363
364 for (ilist<IVStrideUse>::const_iterator UI = SI->second->Users.begin(),
365 E = SI->second->Users.end(); UI != E; ++UI) {
366 OS << " ";
367 WriteAsOperand(OS, UI->getOperandValToReplace(), false);
368 OS << " = ";
369 OS << *getReplacementExpr(*UI);
370 if (UI->isUseOfPostIncrementedValue())
371 OS << " (post-inc)";
372 OS << " in ";
373 UI->getUser()->print(OS);
Dan Gohmand9ef1a82009-07-14 00:32:49 +0000374 OS << '\n';
Dan Gohman81db61a2009-05-12 02:17:14 +0000375 }
376 }
377}
378
Dan Gohman81db61a2009-05-12 02:17:14 +0000379void IVUsers::dump() const {
David Greene63c45602009-12-23 20:20:46 +0000380 print(dbgs());
Dan Gohman81db61a2009-05-12 02:17:14 +0000381}
382
383void IVUsers::releaseMemory() {
384 IVUsesByStride.clear();
385 StrideOrder.clear();
Evan Cheng04149f72009-12-17 09:39:49 +0000386 Processed.clear();
Dan Gohman6bec5bb2009-12-18 00:06:20 +0000387 IVUses.clear();
Dan Gohman81db61a2009-05-12 02:17:14 +0000388}
389
390void IVStrideUse::deleted() {
391 // Remove this user from the list.
392 Parent->Users.erase(this);
393 // this now dangles!
394}