blob: 9643d16cd5afad2085c55bb6f677325669e36153 [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;
32static RegisterPass<IVUsers>
33X("iv-users", "Induction Variable Users", false, true);
34
35Pass *llvm::createIVUsersPass() {
36 return new IVUsers();
37}
38
Dan Gohman448db1c2010-04-07 22:27:08 +000039/// isInteresting - Test whether the given expression is "interesting" when
40/// used by the given expression, within the context of analyzing the
41/// given loop.
42static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L) {
43 // Anything loop-invariant is interesting.
44 if (!isa<SCEVUnknown>(S) && S->isLoopInvariant(L))
Dan Gohman81db61a2009-05-12 02:17:14 +000045 return true;
46
Dan Gohman448db1c2010-04-07 22:27:08 +000047 // An addrec is interesting if it's affine or if it has an interesting start.
48 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
49 // Keep things simple. Don't touch loop-variant strides.
Dan Gohmanb3cdb0e2010-04-09 01:22:56 +000050 if (AR->getLoop() == L)
51 return AR->isAffine() || !L->contains(I);
Dan Gohman448db1c2010-04-07 22:27:08 +000052 // Otherwise recurse to see if the start value is interesting.
53 return isInteresting(AR->getStart(), I, L);
54 }
Dan Gohman81db61a2009-05-12 02:17:14 +000055
Dan Gohman448db1c2010-04-07 22:27:08 +000056 // An add is interesting if any of its operands is.
57 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
58 for (SCEVAddExpr::op_iterator OI = Add->op_begin(), OE = Add->op_end();
59 OI != OE; ++OI)
60 if (isInteresting(*OI, I, L))
61 return true;
62 return false;
63 }
Dan Gohman81db61a2009-05-12 02:17:14 +000064
Dan Gohman448db1c2010-04-07 22:27:08 +000065 // Nothing else is interesting here.
66 return false;
Dan Gohman81db61a2009-05-12 02:17:14 +000067}
68
69/// AddUsersIfInteresting - Inspect the specified instruction. If it is a
70/// reducible SCEV, recursively add its users to the IVUsesByStride set and
71/// return true. Otherwise, return false.
72bool IVUsers::AddUsersIfInteresting(Instruction *I) {
73 if (!SE->isSCEVable(I->getType()))
74 return false; // Void and FP expressions cannot be reduced.
75
76 // LSR is not APInt clean, do not touch integers bigger than 64-bits.
77 if (SE->getTypeSizeInBits(I->getType()) > 64)
78 return false;
79
80 if (!Processed.insert(I))
81 return true; // Instruction already handled.
82
83 // Get the symbolic expression for this instruction.
Dan Gohman0bba49c2009-07-07 17:06:11 +000084 const SCEV *ISE = SE->getSCEV(I);
Dan Gohman81db61a2009-05-12 02:17:14 +000085
Dan Gohman448db1c2010-04-07 22:27:08 +000086 // If we've come to an uninteresting expression, stop the traversal and
87 // call this a user.
88 if (!isInteresting(ISE, I, L))
Jim Grosbach97200e42009-11-19 02:05:44 +000089 return false;
90
Dan Gohman81db61a2009-05-12 02:17:14 +000091 SmallPtrSet<Instruction *, 4> UniqueUsers;
92 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
93 UI != E; ++UI) {
94 Instruction *User = cast<Instruction>(*UI);
95 if (!UniqueUsers.insert(User))
96 continue;
97
98 // Do not infinitely recurse on PHI nodes.
99 if (isa<PHINode>(User) && Processed.count(User))
100 continue;
101
102 // Descend recursively, but not into PHI nodes outside the current loop.
103 // It's important to see the entire expression outside the loop to get
104 // choices that depend on addressing mode use right, although we won't
Dan Gohman3f46a3a2010-03-01 17:49:51 +0000105 // consider references outside the loop in all cases.
Dan Gohman81db61a2009-05-12 02:17:14 +0000106 // If User is already in Processed, we don't want to recurse into it again,
107 // but do want to record a second reference in the same instruction.
108 bool AddUserToIVUsers = false;
109 if (LI->getLoopFor(User->getParent()) != L) {
110 if (isa<PHINode>(User) || Processed.count(User) ||
111 !AddUsersIfInteresting(User)) {
David Greene63c45602009-12-23 20:20:46 +0000112 DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n'
Chris Lattnerbdff5482009-08-23 04:37:46 +0000113 << " OF SCEV: " << *ISE << '\n');
Dan Gohman81db61a2009-05-12 02:17:14 +0000114 AddUserToIVUsers = true;
115 }
116 } else if (Processed.count(User) ||
117 !AddUsersIfInteresting(User)) {
David Greene63c45602009-12-23 20:20:46 +0000118 DEBUG(dbgs() << "FOUND USER: " << *User << '\n'
Chris Lattnerbdff5482009-08-23 04:37:46 +0000119 << " OF SCEV: " << *ISE << '\n');
Dan Gohman81db61a2009-05-12 02:17:14 +0000120 AddUserToIVUsers = true;
121 }
122
123 if (AddUserToIVUsers) {
Dan Gohman448db1c2010-04-07 22:27:08 +0000124 // Okay, we found a user that we cannot reduce.
125 IVUses.push_back(new IVStrideUse(this, ISE, User, I));
126 IVStrideUse &NewUse = IVUses.back();
127 // Transform the expression into a normalized form.
128 NewUse.Expr =
129 TransformForPostIncUse(NormalizeAutodetect, NewUse.Expr,
130 User, I,
131 NewUse.PostIncLoops,
132 *SE, *DT);
133 DEBUG(dbgs() << " NORMALIZED TO: " << *NewUse.Expr << '\n');
Dan Gohman81db61a2009-05-12 02:17:14 +0000134 }
135 }
136 return true;
137}
138
Dan Gohman448db1c2010-04-07 22:27:08 +0000139IVStrideUse &IVUsers::AddUser(const SCEV *Expr,
Dan Gohman572645c2010-02-12 10:34:29 +0000140 Instruction *User, Value *Operand) {
Dan Gohman448db1c2010-04-07 22:27:08 +0000141 IVUses.push_back(new IVStrideUse(this, Expr, User, Operand));
Dan Gohman572645c2010-02-12 10:34:29 +0000142 return IVUses.back();
Evan Cheng586f69a2009-11-12 07:35:05 +0000143}
144
Dan Gohman81db61a2009-05-12 02:17:14 +0000145IVUsers::IVUsers()
146 : LoopPass(&ID) {
147}
148
149void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const {
150 AU.addRequired<LoopInfo>();
151 AU.addRequired<DominatorTree>();
152 AU.addRequired<ScalarEvolution>();
153 AU.setPreservesAll();
154}
155
156bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
157
158 L = l;
159 LI = &getAnalysis<LoopInfo>();
160 DT = &getAnalysis<DominatorTree>();
161 SE = &getAnalysis<ScalarEvolution>();
162
163 // Find all uses of induction variables in this loop, and categorize
164 // them by stride. Start by finding all of the PHI nodes in the header for
165 // this loop. If they are induction variables, inspect their uses.
166 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
167 AddUsersIfInteresting(I);
168
169 return false;
170}
171
172/// getReplacementExpr - Return a SCEV expression which computes the
173/// value of the OperandValToReplace of the given IVStrideUse.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000174const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &U) const {
Dan Gohman448db1c2010-04-07 22:27:08 +0000175 PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(U.PostIncLoops);
176 return TransformForPostIncUse(Denormalize, U.getExpr(),
177 U.getUser(), U.getOperandValToReplace(),
178 Loops, *SE, *DT);
Dan Gohmanbafbbdd2010-01-19 21:55:32 +0000179}
180
Dan Gohman81db61a2009-05-12 02:17:14 +0000181void IVUsers::print(raw_ostream &OS, const Module *M) const {
182 OS << "IV Users for loop ";
183 WriteAsOperand(OS, L->getHeader(), false);
184 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
185 OS << " with backedge-taken count "
186 << *SE->getBackedgeTakenCount(L);
187 }
188 OS << ":\n";
189
Dan Gohman3f46a3a2010-03-01 17:49:51 +0000190 // Use a default AssemblyAnnotationWriter to suppress the default info
Dan Gohman04025772010-02-14 02:48:58 +0000191 // comments, which aren't relevant here.
192 AssemblyAnnotationWriter Annotator;
Dan Gohman572645c2010-02-12 10:34:29 +0000193 for (ilist<IVStrideUse>::const_iterator UI = IVUses.begin(),
194 E = IVUses.end(); UI != E; ++UI) {
195 OS << " ";
196 WriteAsOperand(OS, UI->getOperandValToReplace(), false);
197 OS << " = "
198 << *getReplacementExpr(*UI);
Dan Gohman448db1c2010-04-07 22:27:08 +0000199 for (PostIncLoopSet::const_iterator
200 I = UI->PostIncLoops.begin(),
201 E = UI->PostIncLoops.end(); I != E; ++I) {
202 OS << " (post-inc with loop ";
203 WriteAsOperand(OS, (*I)->getHeader(), false);
204 OS << ")";
205 }
Dan Gohman572645c2010-02-12 10:34:29 +0000206 OS << " in ";
207 UI->getUser()->print(OS, &Annotator);
208 OS << '\n';
Dan Gohman81db61a2009-05-12 02:17:14 +0000209 }
210}
211
Dan Gohman81db61a2009-05-12 02:17:14 +0000212void IVUsers::dump() const {
David Greene63c45602009-12-23 20:20:46 +0000213 print(dbgs());
Dan Gohman81db61a2009-05-12 02:17:14 +0000214}
215
216void IVUsers::releaseMemory() {
Evan Cheng04149f72009-12-17 09:39:49 +0000217 Processed.clear();
Dan Gohman6bec5bb2009-12-18 00:06:20 +0000218 IVUses.clear();
Dan Gohman81db61a2009-05-12 02:17:14 +0000219}
220
Dan Gohman448db1c2010-04-07 22:27:08 +0000221static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) {
222 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
223 if (AR->getLoop() == L)
224 return AR;
225 return findAddRecForLoop(AR->getStart(), L);
226 }
227
228 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
229 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
230 I != E; ++I)
231 if (const SCEVAddRecExpr *AR = findAddRecForLoop(*I, L))
232 return AR;
233 return 0;
234 }
235
236 return 0;
237}
238
239const SCEV *IVStrideUse::getStride(const Loop *L) const {
240 if (const SCEVAddRecExpr *AR = findAddRecForLoop(getExpr(), L))
241 return AR->getStepRecurrence(*Parent->SE);
242 return 0;
243}
244
245void IVStrideUse::transformToPostInc(const Loop *L) {
246 PostIncLoopSet Loops;
247 Loops.insert(L);
248 Expr = TransformForPostIncUse(Normalize, Expr,
249 getUser(), getOperandValToReplace(),
250 Loops, *Parent->SE, *Parent->DT);
251 PostIncLoops.insert(L);
252}
253
Dan Gohman81db61a2009-05-12 02:17:14 +0000254void IVStrideUse::deleted() {
255 // Remove this user from the list.
Dan Gohman572645c2010-02-12 10:34:29 +0000256 Parent->IVUses.erase(this);
Dan Gohman81db61a2009-05-12 02:17:14 +0000257 // this now dangles!
258}