blob: d9bdf5cbb09b53d9691c4b6bf3ff44b1409651ca [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"
Dan Gohman4207d6a2010-02-10 20:42:37 +000024#include "llvm/Assembly/AsmAnnotationWriter.h"
Dan Gohman81db61a2009-05-12 02:17:14 +000025#include "llvm/ADT/STLExtras.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
28#include <algorithm>
29using namespace llvm;
30
31char IVUsers::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000032INITIALIZE_PASS(IVUsers, "iv-users", "Induction Variable Users", false, true);
Dan Gohman81db61a2009-05-12 02:17:14 +000033
34Pass *llvm::createIVUsersPass() {
35 return new IVUsers();
36}
37
Dan Gohmaneaa40ff2010-08-29 16:40:03 +000038/// findInterestingAddRec - Test whether the given expression is interesting.
39/// Return the addrec with the current loop which makes it interesting, or
40/// null if it is not interesting.
41const SCEVAddRecExpr *IVUsers::findInterestingAddRec(const SCEV *S) const {
Dan Gohman448db1c2010-04-07 22:27:08 +000042 // An addrec is interesting if it's affine or if it has an interesting start.
43 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
44 // Keep things simple. Don't touch loop-variant strides.
Dan Gohmanb3cdb0e2010-04-09 01:22:56 +000045 if (AR->getLoop() == L)
Dan Gohmaneaa40ff2010-08-29 16:40:03 +000046 return AR;
47 // We don't yet know how to do effective SCEV expansions for addrecs
48 // with interesting steps.
49 if (findInterestingAddRec(AR->getStepRecurrence(*SE)))
50 return 0;
51 // Otherwise recurse to see if the start value is interesting.
52 return findInterestingAddRec(AR->getStart());
Dan Gohman448db1c2010-04-07 22:27:08 +000053 }
Dan Gohman81db61a2009-05-12 02:17:14 +000054
Dan Gohmanbbc1da82010-08-17 22:50:37 +000055 // An add is interesting if exactly one of its operands is interesting.
Dan Gohman448db1c2010-04-07 22:27:08 +000056 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
57 for (SCEVAddExpr::op_iterator OI = Add->op_begin(), OE = Add->op_end();
58 OI != OE; ++OI)
Dan Gohmaneaa40ff2010-08-29 16:40:03 +000059 if (const SCEVAddRecExpr *AR = findInterestingAddRec(*OI))
60 return AR;
61 return 0;
Dan Gohman448db1c2010-04-07 22:27:08 +000062 }
Dan Gohman81db61a2009-05-12 02:17:14 +000063
Dan Gohman448db1c2010-04-07 22:27:08 +000064 // Nothing else is interesting here.
Dan Gohmaneaa40ff2010-08-29 16:40:03 +000065 return 0;
66}
67
68bool IVUsers::isInterestingUser(const Instruction *User) const {
69 // Void and FP expressions cannot be reduced.
70 if (!SE->isSCEVable(User->getType()))
71 return false;
72
73 // LSR is not APInt clean, do not touch integers bigger than 64-bits.
74 if (SE->getTypeSizeInBits(User->getType()) > 64)
75 return false;
76
77 // Don't descend into PHI nodes outside the current loop.
78 if (LI->getLoopFor(User->getParent()) != L &&
79 isa<PHINode>(User))
80 return false;
81
82 // Otherwise, it may be interesting.
83 return true;
Dan Gohman81db61a2009-05-12 02:17:14 +000084}
85
86/// AddUsersIfInteresting - Inspect the specified instruction. If it is a
87/// reducible SCEV, recursively add its users to the IVUsesByStride set and
88/// return true. Otherwise, return false.
Dan Gohmaneaa40ff2010-08-29 16:40:03 +000089void IVUsers::AddUsersIfInteresting(Instruction *I) {
90 // Stop if we've seen this before.
Dan Gohman81db61a2009-05-12 02:17:14 +000091 if (!Processed.insert(I))
Dan Gohmaneaa40ff2010-08-29 16:40:03 +000092 return;
Dan Gohman81db61a2009-05-12 02:17:14 +000093
Dan Gohmaneaa40ff2010-08-29 16:40:03 +000094 // If this PHI node is not SCEVable, ignore it.
95 if (!SE->isSCEVable(I->getType()))
96 return;
Dan Gohman81db61a2009-05-12 02:17:14 +000097
Dan Gohmaneaa40ff2010-08-29 16:40:03 +000098 // If this PHI node is not an addrec for this loop, ignore it.
99 const SCEVAddRecExpr *Expr = findInterestingAddRec(SE->getSCEV(I));
100 if (!Expr)
101 return;
Jim Grosbach97200e42009-11-19 02:05:44 +0000102
Dan Gohmaneaa40ff2010-08-29 16:40:03 +0000103 // Walk the def-use graph.
104 SmallVector<std::pair<Instruction *, const SCEVAddRecExpr *>, 16> Worklist;
105 Worklist.push_back(std::make_pair(I, Expr));
106 do {
107 std::pair<Instruction *, const SCEVAddRecExpr *> P =
108 Worklist.pop_back_val();
109 Instruction *Op = P.first;
110 const SCEVAddRecExpr *OpAR = P.second;
Dan Gohman81db61a2009-05-12 02:17:14 +0000111
Dan Gohmaneaa40ff2010-08-29 16:40:03 +0000112 // Visit Op's users.
113 SmallPtrSet<Instruction *, 8> VisitedUsers;
114 for (Value::use_iterator UI = Op->use_begin(), E = Op->use_end();
115 UI != E; ++UI) {
116 // Don't visit any individual user more than once.
117 Instruction *User = cast<Instruction>(*UI);
118 if (!VisitedUsers.insert(User))
119 continue;
Dan Gohman81db61a2009-05-12 02:17:14 +0000120
Dan Gohmaneaa40ff2010-08-29 16:40:03 +0000121 // If it's an affine addrec (which we can pretty safely re-expand) inside
122 // the loop, or a potentially non-affine addrec outside the loop (which
123 // we can evaluate outside of the loop), follow it.
124 if (OpAR->isAffine() || !L->contains(User)) {
125 if (isInterestingUser(User)) {
126 const SCEV *UserExpr = SE->getSCEV(User);
127
128 if (const SCEVAddRecExpr *AR = findInterestingAddRec(UserExpr)) {
129 // Interesting. Keep searching.
130 if (Processed.insert(User))
131 Worklist.push_back(std::make_pair(User, AR));
132 continue;
133 }
134 }
Dan Gohman81db61a2009-05-12 02:17:14 +0000135 }
Dan Gohman81db61a2009-05-12 02:17:14 +0000136
Dan Gohmaneaa40ff2010-08-29 16:40:03 +0000137 // Otherwise, this is the point where the def-use chain
138 // becomes uninteresting. Call it an IV User.
139 AddUser(User, Op);
Dan Gohman81db61a2009-05-12 02:17:14 +0000140 }
Dan Gohmaneaa40ff2010-08-29 16:40:03 +0000141 } while (!Worklist.empty());
Dan Gohman81db61a2009-05-12 02:17:14 +0000142}
143
Dan Gohmanc0564542010-04-19 21:48:58 +0000144IVStrideUse &IVUsers::AddUser(Instruction *User, Value *Operand) {
145 IVUses.push_back(new IVStrideUse(this, User, Operand));
Dan Gohmaneaa40ff2010-08-29 16:40:03 +0000146 IVStrideUse &NewUse = IVUses.back();
147
148 // Auto-detect and remember post-inc loops for this expression.
149 const SCEV *S = SE->getSCEV(Operand);
150 (void)TransformForPostIncUse(NormalizeAutodetect,
151 S, User, Operand,
152 NewUse.PostIncLoops,
153 *SE, *DT);
154 return NewUse;
Evan Cheng586f69a2009-11-12 07:35:05 +0000155}
156
Dan Gohman81db61a2009-05-12 02:17:14 +0000157IVUsers::IVUsers()
Owen Anderson90c579d2010-08-06 18:33:48 +0000158 : LoopPass(ID) {
Dan Gohman81db61a2009-05-12 02:17:14 +0000159}
160
161void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const {
162 AU.addRequired<LoopInfo>();
163 AU.addRequired<DominatorTree>();
164 AU.addRequired<ScalarEvolution>();
165 AU.setPreservesAll();
166}
167
168bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
169
170 L = l;
171 LI = &getAnalysis<LoopInfo>();
172 DT = &getAnalysis<DominatorTree>();
173 SE = &getAnalysis<ScalarEvolution>();
174
175 // Find all uses of induction variables in this loop, and categorize
176 // them by stride. Start by finding all of the PHI nodes in the header for
177 // this loop. If they are induction variables, inspect their uses.
178 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
Dan Gohmaneaa40ff2010-08-29 16:40:03 +0000179 AddUsersIfInteresting(I);
Dan Gohman81db61a2009-05-12 02:17:14 +0000180
181 return false;
182}
183
Dan Gohman81db61a2009-05-12 02:17:14 +0000184void IVUsers::print(raw_ostream &OS, const Module *M) const {
185 OS << "IV Users for loop ";
186 WriteAsOperand(OS, L->getHeader(), false);
187 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
188 OS << " with backedge-taken count "
189 << *SE->getBackedgeTakenCount(L);
190 }
191 OS << ":\n";
192
Dan Gohman3f46a3a2010-03-01 17:49:51 +0000193 // Use a default AssemblyAnnotationWriter to suppress the default info
Dan Gohman04025772010-02-14 02:48:58 +0000194 // comments, which aren't relevant here.
195 AssemblyAnnotationWriter Annotator;
Dan Gohman572645c2010-02-12 10:34:29 +0000196 for (ilist<IVStrideUse>::const_iterator UI = IVUses.begin(),
197 E = IVUses.end(); UI != E; ++UI) {
198 OS << " ";
199 WriteAsOperand(OS, UI->getOperandValToReplace(), false);
Dan Gohmanc0564542010-04-19 21:48:58 +0000200 OS << " = " << *getReplacementExpr(*UI);
Dan Gohman448db1c2010-04-07 22:27:08 +0000201 for (PostIncLoopSet::const_iterator
202 I = UI->PostIncLoops.begin(),
203 E = UI->PostIncLoops.end(); I != E; ++I) {
204 OS << " (post-inc with loop ";
205 WriteAsOperand(OS, (*I)->getHeader(), false);
206 OS << ")";
207 }
Dan Gohman572645c2010-02-12 10:34:29 +0000208 OS << " in ";
209 UI->getUser()->print(OS, &Annotator);
210 OS << '\n';
Dan Gohman81db61a2009-05-12 02:17:14 +0000211 }
212}
213
Dan Gohman81db61a2009-05-12 02:17:14 +0000214void IVUsers::dump() const {
David Greene63c45602009-12-23 20:20:46 +0000215 print(dbgs());
Dan Gohman81db61a2009-05-12 02:17:14 +0000216}
217
218void IVUsers::releaseMemory() {
Evan Cheng04149f72009-12-17 09:39:49 +0000219 Processed.clear();
Dan Gohman6bec5bb2009-12-18 00:06:20 +0000220 IVUses.clear();
Dan Gohman81db61a2009-05-12 02:17:14 +0000221}
222
Dan Gohmanc0564542010-04-19 21:48:58 +0000223/// getReplacementExpr - Return a SCEV expression which computes the
224/// value of the OperandValToReplace.
225const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &IU) const {
226 return SE->getSCEV(IU.getOperandValToReplace());
227}
228
229/// getExpr - Return the expression for the use.
230const SCEV *IVUsers::getExpr(const IVStrideUse &IU) const {
231 return
232 TransformForPostIncUse(Normalize, getReplacementExpr(IU),
233 IU.getUser(), IU.getOperandValToReplace(),
234 const_cast<PostIncLoopSet &>(IU.getPostIncLoops()),
235 *SE, *DT);
236}
237
Dan Gohman448db1c2010-04-07 22:27:08 +0000238static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) {
239 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
240 if (AR->getLoop() == L)
241 return AR;
242 return findAddRecForLoop(AR->getStart(), L);
243 }
244
245 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
246 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
247 I != E; ++I)
248 if (const SCEVAddRecExpr *AR = findAddRecForLoop(*I, L))
249 return AR;
250 return 0;
251 }
252
253 return 0;
254}
255
Dan Gohmanc0564542010-04-19 21:48:58 +0000256const SCEV *IVUsers::getStride(const IVStrideUse &IU, const Loop *L) const {
257 if (const SCEVAddRecExpr *AR = findAddRecForLoop(getExpr(IU), L))
258 return AR->getStepRecurrence(*SE);
Dan Gohman448db1c2010-04-07 22:27:08 +0000259 return 0;
260}
261
262void IVStrideUse::transformToPostInc(const Loop *L) {
Dan Gohman448db1c2010-04-07 22:27:08 +0000263 PostIncLoops.insert(L);
264}
265
Dan Gohman81db61a2009-05-12 02:17:14 +0000266void IVStrideUse::deleted() {
267 // Remove this user from the list.
Dan Gohman572645c2010-02-12 10:34:29 +0000268 Parent->IVUses.erase(this);
Dan Gohman81db61a2009-05-12 02:17:14 +0000269 // this now dangles!
270}