blob: fd514336be8a3f50365bf296ddabc3c0408851f0 [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 Gohman191bd642010-09-01 01:45:53 +000038/// isInteresting - Test whether the given expression is "interesting" when
39/// used by the given expression, within the context of analyzing the
40/// given loop.
41static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L,
42 ScalarEvolution *SE) {
Dan Gohman448db1c2010-04-07 22:27:08 +000043 // An addrec is interesting if it's affine or if it has an interesting start.
44 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
45 // Keep things simple. Don't touch loop-variant strides.
Dan Gohmanb3cdb0e2010-04-09 01:22:56 +000046 if (AR->getLoop() == L)
Dan Gohman191bd642010-09-01 01:45:53 +000047 return AR->isAffine() || !L->contains(I);
48 // Otherwise recurse to see if the start value is interesting, and that
49 // the step value is not interesting, since we don't yet know how to
50 // do effective SCEV expansions for addrecs with interesting steps.
51 return isInteresting(AR->getStart(), I, L, SE) &&
52 !isInteresting(AR->getStepRecurrence(*SE), I, L, SE);
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)) {
Dan Gohman191bd642010-09-01 01:45:53 +000057 bool AnyInterestingYet = false;
Dan Gohman448db1c2010-04-07 22:27:08 +000058 for (SCEVAddExpr::op_iterator OI = Add->op_begin(), OE = Add->op_end();
59 OI != OE; ++OI)
Dan Gohman191bd642010-09-01 01:45:53 +000060 if (isInteresting(*OI, I, L, SE)) {
61 if (AnyInterestingYet)
62 return false;
63 AnyInterestingYet = true;
64 }
65 return AnyInterestingYet;
Dan Gohman448db1c2010-04-07 22:27:08 +000066 }
Dan Gohman81db61a2009-05-12 02:17:14 +000067
Dan Gohman448db1c2010-04-07 22:27:08 +000068 // Nothing else is interesting here.
Dan Gohman191bd642010-09-01 01:45:53 +000069 return false;
Dan Gohman81db61a2009-05-12 02:17:14 +000070}
71
72/// AddUsersIfInteresting - Inspect the specified instruction. If it is a
73/// reducible SCEV, recursively add its users to the IVUsesByStride set and
74/// return true. Otherwise, return false.
Dan Gohman191bd642010-09-01 01:45:53 +000075bool IVUsers::AddUsersIfInteresting(Instruction *I) {
Dan Gohmaneaa40ff2010-08-29 16:40:03 +000076 if (!SE->isSCEVable(I->getType()))
Dan Gohman191bd642010-09-01 01:45:53 +000077 return false; // Void and FP expressions cannot be reduced.
Dan Gohman81db61a2009-05-12 02:17:14 +000078
Dan Gohman191bd642010-09-01 01:45:53 +000079 // LSR is not APInt clean, do not touch integers bigger than 64-bits.
80 if (SE->getTypeSizeInBits(I->getType()) > 64)
81 return false;
Jim Grosbach97200e42009-11-19 02:05:44 +000082
Dan Gohman191bd642010-09-01 01:45:53 +000083 if (!Processed.insert(I))
84 return true; // Instruction already handled.
Dan Gohman81db61a2009-05-12 02:17:14 +000085
Dan Gohman191bd642010-09-01 01:45:53 +000086 // Get the symbolic expression for this instruction.
87 const SCEV *ISE = SE->getSCEV(I);
Dan Gohman81db61a2009-05-12 02:17:14 +000088
Dan Gohman191bd642010-09-01 01:45:53 +000089 // If we've come to an uninteresting expression, stop the traversal and
90 // call this a user.
91 if (!isInteresting(ISE, I, L, SE))
92 return false;
Dan Gohmaneaa40ff2010-08-29 16:40:03 +000093
Dan Gohman191bd642010-09-01 01:45:53 +000094 SmallPtrSet<Instruction *, 4> UniqueUsers;
95 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
96 UI != E; ++UI) {
97 Instruction *User = cast<Instruction>(*UI);
98 if (!UniqueUsers.insert(User))
99 continue;
100
101 // Do not infinitely recurse on PHI nodes.
102 if (isa<PHINode>(User) && Processed.count(User))
103 continue;
104
105 // Descend recursively, but not into PHI nodes outside the current loop.
106 // It's important to see the entire expression outside the loop to get
107 // choices that depend on addressing mode use right, although we won't
108 // consider references outside the loop in all cases.
109 // If User is already in Processed, we don't want to recurse into it again,
110 // but do want to record a second reference in the same instruction.
111 bool AddUserToIVUsers = false;
112 if (LI->getLoopFor(User->getParent()) != L) {
113 if (isa<PHINode>(User) || Processed.count(User) ||
114 !AddUsersIfInteresting(User)) {
115 DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n'
116 << " OF SCEV: " << *ISE << '\n');
117 AddUserToIVUsers = true;
Dan Gohman81db61a2009-05-12 02:17:14 +0000118 }
Dan Gohman191bd642010-09-01 01:45:53 +0000119 } else if (Processed.count(User) ||
120 !AddUsersIfInteresting(User)) {
121 DEBUG(dbgs() << "FOUND USER: " << *User << '\n'
122 << " OF SCEV: " << *ISE << '\n');
123 AddUserToIVUsers = true;
Dan Gohman81db61a2009-05-12 02:17:14 +0000124 }
Dan Gohman191bd642010-09-01 01:45:53 +0000125
126 if (AddUserToIVUsers) {
127 // Okay, we found a user that we cannot reduce.
128 IVUses.push_back(new IVStrideUse(this, User, I));
129 IVStrideUse &NewUse = IVUses.back();
130 // Transform the expression into a normalized form.
131 ISE = TransformForPostIncUse(NormalizeAutodetect,
132 ISE, User, I,
133 NewUse.PostIncLoops,
134 *SE, *DT);
135 DEBUG(dbgs() << " NORMALIZED TO: " << *ISE << '\n');
136 }
137 }
138 return true;
Dan Gohman81db61a2009-05-12 02:17:14 +0000139}
140
Dan Gohmanc0564542010-04-19 21:48:58 +0000141IVStrideUse &IVUsers::AddUser(Instruction *User, Value *Operand) {
142 IVUses.push_back(new IVStrideUse(this, User, Operand));
Dan Gohman191bd642010-09-01 01:45:53 +0000143 return IVUses.back();
Evan Cheng586f69a2009-11-12 07:35:05 +0000144}
145
Dan Gohman81db61a2009-05-12 02:17:14 +0000146IVUsers::IVUsers()
Owen Anderson90c579d2010-08-06 18:33:48 +0000147 : LoopPass(ID) {
Dan Gohman81db61a2009-05-12 02:17:14 +0000148}
149
150void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const {
151 AU.addRequired<LoopInfo>();
152 AU.addRequired<DominatorTree>();
153 AU.addRequired<ScalarEvolution>();
154 AU.setPreservesAll();
155}
156
157bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
158
159 L = l;
160 LI = &getAnalysis<LoopInfo>();
161 DT = &getAnalysis<DominatorTree>();
162 SE = &getAnalysis<ScalarEvolution>();
163
164 // Find all uses of induction variables in this loop, and categorize
165 // them by stride. Start by finding all of the PHI nodes in the header for
166 // this loop. If they are induction variables, inspect their uses.
167 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
Dan Gohman191bd642010-09-01 01:45:53 +0000168 (void)AddUsersIfInteresting(I);
Dan Gohman81db61a2009-05-12 02:17:14 +0000169
170 return false;
171}
172
Dan Gohman81db61a2009-05-12 02:17:14 +0000173void IVUsers::print(raw_ostream &OS, const Module *M) const {
174 OS << "IV Users for loop ";
175 WriteAsOperand(OS, L->getHeader(), false);
176 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
177 OS << " with backedge-taken count "
178 << *SE->getBackedgeTakenCount(L);
179 }
180 OS << ":\n";
181
Dan Gohman3f46a3a2010-03-01 17:49:51 +0000182 // Use a default AssemblyAnnotationWriter to suppress the default info
Dan Gohman04025772010-02-14 02:48:58 +0000183 // comments, which aren't relevant here.
184 AssemblyAnnotationWriter Annotator;
Dan Gohman572645c2010-02-12 10:34:29 +0000185 for (ilist<IVStrideUse>::const_iterator UI = IVUses.begin(),
186 E = IVUses.end(); UI != E; ++UI) {
187 OS << " ";
188 WriteAsOperand(OS, UI->getOperandValToReplace(), false);
Dan Gohmanc0564542010-04-19 21:48:58 +0000189 OS << " = " << *getReplacementExpr(*UI);
Dan Gohman448db1c2010-04-07 22:27:08 +0000190 for (PostIncLoopSet::const_iterator
191 I = UI->PostIncLoops.begin(),
192 E = UI->PostIncLoops.end(); I != E; ++I) {
193 OS << " (post-inc with loop ";
194 WriteAsOperand(OS, (*I)->getHeader(), false);
195 OS << ")";
196 }
Dan Gohman572645c2010-02-12 10:34:29 +0000197 OS << " in ";
198 UI->getUser()->print(OS, &Annotator);
199 OS << '\n';
Dan Gohman81db61a2009-05-12 02:17:14 +0000200 }
201}
202
Dan Gohman81db61a2009-05-12 02:17:14 +0000203void IVUsers::dump() const {
David Greene63c45602009-12-23 20:20:46 +0000204 print(dbgs());
Dan Gohman81db61a2009-05-12 02:17:14 +0000205}
206
207void IVUsers::releaseMemory() {
Evan Cheng04149f72009-12-17 09:39:49 +0000208 Processed.clear();
Dan Gohman6bec5bb2009-12-18 00:06:20 +0000209 IVUses.clear();
Dan Gohman81db61a2009-05-12 02:17:14 +0000210}
211
Dan Gohmanc0564542010-04-19 21:48:58 +0000212/// getReplacementExpr - Return a SCEV expression which computes the
213/// value of the OperandValToReplace.
214const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &IU) const {
215 return SE->getSCEV(IU.getOperandValToReplace());
216}
217
218/// getExpr - Return the expression for the use.
219const SCEV *IVUsers::getExpr(const IVStrideUse &IU) const {
220 return
221 TransformForPostIncUse(Normalize, getReplacementExpr(IU),
222 IU.getUser(), IU.getOperandValToReplace(),
223 const_cast<PostIncLoopSet &>(IU.getPostIncLoops()),
224 *SE, *DT);
225}
226
Dan Gohman448db1c2010-04-07 22:27:08 +0000227static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) {
228 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
229 if (AR->getLoop() == L)
230 return AR;
231 return findAddRecForLoop(AR->getStart(), L);
232 }
233
234 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
235 for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
236 I != E; ++I)
237 if (const SCEVAddRecExpr *AR = findAddRecForLoop(*I, L))
238 return AR;
239 return 0;
240 }
241
242 return 0;
243}
244
Dan Gohmanc0564542010-04-19 21:48:58 +0000245const SCEV *IVUsers::getStride(const IVStrideUse &IU, const Loop *L) const {
246 if (const SCEVAddRecExpr *AR = findAddRecForLoop(getExpr(IU), L))
247 return AR->getStepRecurrence(*SE);
Dan Gohman448db1c2010-04-07 22:27:08 +0000248 return 0;
249}
250
251void IVStrideUse::transformToPostInc(const Loop *L) {
Dan Gohman448db1c2010-04-07 22:27:08 +0000252 PostIncLoops.insert(L);
253}
254
Dan Gohman81db61a2009-05-12 02:17:14 +0000255void IVStrideUse::deleted() {
256 // Remove this user from the list.
Dan Gohman572645c2010-02-12 10:34:29 +0000257 Parent->IVUses.erase(this);
Dan Gohman81db61a2009-05-12 02:17:14 +0000258 // this now dangles!
259}