Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1 | //===- 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 Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/LoopPass.h" |
| 23 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Dan Gohman | 4207d6a | 2010-02-10 20:42:37 +0000 | [diff] [blame] | 24 | #include "llvm/Assembly/AsmAnnotationWriter.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/STLExtras.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include <algorithm> |
| 29 | using namespace llvm; |
| 30 | |
| 31 | char IVUsers::ID = 0; |
| 32 | static RegisterPass<IVUsers> |
| 33 | X("iv-users", "Induction Variable Users", false, true); |
| 34 | |
| 35 | Pass *llvm::createIVUsersPass() { |
| 36 | return new IVUsers(); |
| 37 | } |
| 38 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 39 | /// CollectSubexprs - Split S into subexpressions which can be pulled out into |
| 40 | /// separate registers. |
| 41 | static void CollectSubexprs(const SCEV *S, |
| 42 | SmallVectorImpl<const SCEV *> &Ops, |
| 43 | ScalarEvolution &SE) { |
| 44 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 45 | // Break out add operands. |
| 46 | for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end(); |
| 47 | I != E; ++I) |
| 48 | CollectSubexprs(*I, Ops, SE); |
| 49 | return; |
| 50 | } else if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 51 | // Split a non-zero base out of an addrec. |
| 52 | if (!AR->getStart()->isZero()) { |
| 53 | CollectSubexprs(AR->getStart(), Ops, SE); |
| 54 | CollectSubexprs(SE.getAddRecExpr(SE.getIntegerSCEV(0, AR->getType()), |
| 55 | AR->getStepRecurrence(SE), |
| 56 | AR->getLoop()), Ops, SE); |
| 57 | return; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 58 | } |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 59 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 60 | |
| 61 | // Otherwise use the value itself. |
| 62 | Ops.push_back(S); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 65 | /// isInteresting - Test whether the given expression is "interesting" when |
| 66 | /// used by the given expression, within the context of analyzing the |
| 67 | /// given loop. |
| 68 | static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L) { |
| 69 | // Anything loop-invariant is interesting. |
| 70 | if (!isa<SCEVUnknown>(S) && S->isLoopInvariant(L)) |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 71 | return true; |
| 72 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 73 | // An addrec is interesting if it's affine or if it has an interesting start. |
| 74 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 75 | // Keep things simple. Don't touch loop-variant strides. |
Dan Gohman | b3cdb0e | 2010-04-09 01:22:56 +0000 | [diff] [blame^] | 76 | if (AR->getLoop() == L) |
| 77 | return AR->isAffine() || !L->contains(I); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 78 | // Otherwise recurse to see if the start value is interesting. |
| 79 | return isInteresting(AR->getStart(), I, L); |
| 80 | } |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 81 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 82 | // An add is interesting if any of its operands is. |
| 83 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 84 | for (SCEVAddExpr::op_iterator OI = Add->op_begin(), OE = Add->op_end(); |
| 85 | OI != OE; ++OI) |
| 86 | if (isInteresting(*OI, I, L)) |
| 87 | return true; |
| 88 | return false; |
| 89 | } |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 90 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 91 | // Nothing else is interesting here. |
| 92 | return false; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /// AddUsersIfInteresting - Inspect the specified instruction. If it is a |
| 96 | /// reducible SCEV, recursively add its users to the IVUsesByStride set and |
| 97 | /// return true. Otherwise, return false. |
| 98 | bool IVUsers::AddUsersIfInteresting(Instruction *I) { |
| 99 | if (!SE->isSCEVable(I->getType())) |
| 100 | return false; // Void and FP expressions cannot be reduced. |
| 101 | |
| 102 | // LSR is not APInt clean, do not touch integers bigger than 64-bits. |
| 103 | if (SE->getTypeSizeInBits(I->getType()) > 64) |
| 104 | return false; |
| 105 | |
| 106 | if (!Processed.insert(I)) |
| 107 | return true; // Instruction already handled. |
| 108 | |
| 109 | // Get the symbolic expression for this instruction. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 110 | const SCEV *ISE = SE->getSCEV(I); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 111 | if (isa<SCEVCouldNotCompute>(ISE)) return false; |
| 112 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 113 | // If we've come to an uninteresting expression, stop the traversal and |
| 114 | // call this a user. |
| 115 | if (!isInteresting(ISE, I, L)) |
Jim Grosbach | 97200e4 | 2009-11-19 02:05:44 +0000 | [diff] [blame] | 116 | return false; |
| 117 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 118 | SmallPtrSet<Instruction *, 4> UniqueUsers; |
| 119 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 120 | UI != E; ++UI) { |
| 121 | Instruction *User = cast<Instruction>(*UI); |
| 122 | if (!UniqueUsers.insert(User)) |
| 123 | continue; |
| 124 | |
| 125 | // Do not infinitely recurse on PHI nodes. |
| 126 | if (isa<PHINode>(User) && Processed.count(User)) |
| 127 | continue; |
| 128 | |
| 129 | // Descend recursively, but not into PHI nodes outside the current loop. |
| 130 | // It's important to see the entire expression outside the loop to get |
| 131 | // choices that depend on addressing mode use right, although we won't |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 132 | // consider references outside the loop in all cases. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 133 | // If User is already in Processed, we don't want to recurse into it again, |
| 134 | // but do want to record a second reference in the same instruction. |
| 135 | bool AddUserToIVUsers = false; |
| 136 | if (LI->getLoopFor(User->getParent()) != L) { |
| 137 | if (isa<PHINode>(User) || Processed.count(User) || |
| 138 | !AddUsersIfInteresting(User)) { |
David Greene | 63c4560 | 2009-12-23 20:20:46 +0000 | [diff] [blame] | 139 | DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n' |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 140 | << " OF SCEV: " << *ISE << '\n'); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 141 | AddUserToIVUsers = true; |
| 142 | } |
| 143 | } else if (Processed.count(User) || |
| 144 | !AddUsersIfInteresting(User)) { |
David Greene | 63c4560 | 2009-12-23 20:20:46 +0000 | [diff] [blame] | 145 | DEBUG(dbgs() << "FOUND USER: " << *User << '\n' |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 146 | << " OF SCEV: " << *ISE << '\n'); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 147 | AddUserToIVUsers = true; |
| 148 | } |
| 149 | |
| 150 | if (AddUserToIVUsers) { |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 151 | // Okay, we found a user that we cannot reduce. |
| 152 | IVUses.push_back(new IVStrideUse(this, ISE, User, I)); |
| 153 | IVStrideUse &NewUse = IVUses.back(); |
| 154 | // Transform the expression into a normalized form. |
| 155 | NewUse.Expr = |
| 156 | TransformForPostIncUse(NormalizeAutodetect, NewUse.Expr, |
| 157 | User, I, |
| 158 | NewUse.PostIncLoops, |
| 159 | *SE, *DT); |
| 160 | DEBUG(dbgs() << " NORMALIZED TO: " << *NewUse.Expr << '\n'); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 166 | IVStrideUse &IVUsers::AddUser(const SCEV *Expr, |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 167 | Instruction *User, Value *Operand) { |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 168 | IVUses.push_back(new IVStrideUse(this, Expr, User, Operand)); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 169 | return IVUses.back(); |
Evan Cheng | 586f69a | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 172 | IVUsers::IVUsers() |
| 173 | : LoopPass(&ID) { |
| 174 | } |
| 175 | |
| 176 | void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const { |
| 177 | AU.addRequired<LoopInfo>(); |
| 178 | AU.addRequired<DominatorTree>(); |
| 179 | AU.addRequired<ScalarEvolution>(); |
| 180 | AU.setPreservesAll(); |
| 181 | } |
| 182 | |
| 183 | bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) { |
| 184 | |
| 185 | L = l; |
| 186 | LI = &getAnalysis<LoopInfo>(); |
| 187 | DT = &getAnalysis<DominatorTree>(); |
| 188 | SE = &getAnalysis<ScalarEvolution>(); |
| 189 | |
| 190 | // Find all uses of induction variables in this loop, and categorize |
| 191 | // them by stride. Start by finding all of the PHI nodes in the header for |
| 192 | // this loop. If they are induction variables, inspect their uses. |
| 193 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) |
| 194 | AddUsersIfInteresting(I); |
| 195 | |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | /// getReplacementExpr - Return a SCEV expression which computes the |
| 200 | /// value of the OperandValToReplace of the given IVStrideUse. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 201 | const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &U) const { |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 202 | PostIncLoopSet &Loops = const_cast<PostIncLoopSet &>(U.PostIncLoops); |
| 203 | return TransformForPostIncUse(Denormalize, U.getExpr(), |
| 204 | U.getUser(), U.getOperandValToReplace(), |
| 205 | Loops, *SE, *DT); |
Dan Gohman | bafbbdd | 2010-01-19 21:55:32 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 208 | void IVUsers::print(raw_ostream &OS, const Module *M) const { |
| 209 | OS << "IV Users for loop "; |
| 210 | WriteAsOperand(OS, L->getHeader(), false); |
| 211 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 212 | OS << " with backedge-taken count " |
| 213 | << *SE->getBackedgeTakenCount(L); |
| 214 | } |
| 215 | OS << ":\n"; |
| 216 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 217 | // Use a default AssemblyAnnotationWriter to suppress the default info |
Dan Gohman | 0402577 | 2010-02-14 02:48:58 +0000 | [diff] [blame] | 218 | // comments, which aren't relevant here. |
| 219 | AssemblyAnnotationWriter Annotator; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 220 | for (ilist<IVStrideUse>::const_iterator UI = IVUses.begin(), |
| 221 | E = IVUses.end(); UI != E; ++UI) { |
| 222 | OS << " "; |
| 223 | WriteAsOperand(OS, UI->getOperandValToReplace(), false); |
| 224 | OS << " = " |
| 225 | << *getReplacementExpr(*UI); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 226 | for (PostIncLoopSet::const_iterator |
| 227 | I = UI->PostIncLoops.begin(), |
| 228 | E = UI->PostIncLoops.end(); I != E; ++I) { |
| 229 | OS << " (post-inc with loop "; |
| 230 | WriteAsOperand(OS, (*I)->getHeader(), false); |
| 231 | OS << ")"; |
| 232 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 233 | OS << " in "; |
| 234 | UI->getUser()->print(OS, &Annotator); |
| 235 | OS << '\n'; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 239 | void IVUsers::dump() const { |
David Greene | 63c4560 | 2009-12-23 20:20:46 +0000 | [diff] [blame] | 240 | print(dbgs()); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void IVUsers::releaseMemory() { |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 244 | Processed.clear(); |
Dan Gohman | 6bec5bb | 2009-12-18 00:06:20 +0000 | [diff] [blame] | 245 | IVUses.clear(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 248 | static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) { |
| 249 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 250 | if (AR->getLoop() == L) |
| 251 | return AR; |
| 252 | return findAddRecForLoop(AR->getStart(), L); |
| 253 | } |
| 254 | |
| 255 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 256 | for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end(); |
| 257 | I != E; ++I) |
| 258 | if (const SCEVAddRecExpr *AR = findAddRecForLoop(*I, L)) |
| 259 | return AR; |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | const SCEV *IVStrideUse::getStride(const Loop *L) const { |
| 267 | if (const SCEVAddRecExpr *AR = findAddRecForLoop(getExpr(), L)) |
| 268 | return AR->getStepRecurrence(*Parent->SE); |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | void IVStrideUse::transformToPostInc(const Loop *L) { |
| 273 | PostIncLoopSet Loops; |
| 274 | Loops.insert(L); |
| 275 | Expr = TransformForPostIncUse(Normalize, Expr, |
| 276 | getUser(), getOperandValToReplace(), |
| 277 | Loops, *Parent->SE, *Parent->DT); |
| 278 | PostIncLoops.insert(L); |
| 279 | } |
| 280 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 281 | void IVStrideUse::deleted() { |
| 282 | // Remove this user from the list. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 283 | Parent->IVUses.erase(this); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 284 | // this now dangles! |
| 285 | } |