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; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 32 | INITIALIZE_PASS(IVUsers, "iv-users", "Induction Variable Users", false, true); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 33 | |
| 34 | Pass *llvm::createIVUsersPass() { |
| 35 | return new IVUsers(); |
| 36 | } |
| 37 | |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 38 | /// 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. |
| 41 | const SCEVAddRecExpr *IVUsers::findInterestingAddRec(const SCEV *S) const { |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 42 | // 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 Gohman | b3cdb0e | 2010-04-09 01:22:56 +0000 | [diff] [blame] | 45 | if (AR->getLoop() == L) |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 46 | 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 Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 53 | } |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 54 | |
Dan Gohman | bbc1da8 | 2010-08-17 22:50:37 +0000 | [diff] [blame] | 55 | // An add is interesting if exactly one of its operands is interesting. |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 56 | 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 Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 59 | if (const SCEVAddRecExpr *AR = findInterestingAddRec(*OI)) |
| 60 | return AR; |
| 61 | return 0; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 62 | } |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 63 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 64 | // Nothing else is interesting here. |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | bool 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 Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 84 | } |
| 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 Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 89 | void IVUsers::AddUsersIfInteresting(Instruction *I) { |
| 90 | // Stop if we've seen this before. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 91 | if (!Processed.insert(I)) |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 92 | return; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 93 | |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 94 | // If this PHI node is not SCEVable, ignore it. |
| 95 | if (!SE->isSCEVable(I->getType())) |
| 96 | return; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 97 | |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 98 | // 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 Grosbach | 97200e4 | 2009-11-19 02:05:44 +0000 | [diff] [blame] | 102 | |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 103 | // 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 Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 111 | |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 112 | // 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 Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 120 | |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 121 | // 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 Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 135 | } |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 136 | |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 137 | // Otherwise, this is the point where the def-use chain |
| 138 | // becomes uninteresting. Call it an IV User. |
| 139 | AddUser(User, Op); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 140 | } |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 141 | } while (!Worklist.empty()); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame] | 144 | IVStrideUse &IVUsers::AddUser(Instruction *User, Value *Operand) { |
| 145 | IVUses.push_back(new IVStrideUse(this, User, Operand)); |
Dan Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 146 | 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 Cheng | 586f69a | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 157 | IVUsers::IVUsers() |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 158 | : LoopPass(ID) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const { |
| 162 | AU.addRequired<LoopInfo>(); |
| 163 | AU.addRequired<DominatorTree>(); |
| 164 | AU.addRequired<ScalarEvolution>(); |
| 165 | AU.setPreservesAll(); |
| 166 | } |
| 167 | |
| 168 | bool 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 Gohman | eaa40ff | 2010-08-29 16:40:03 +0000 | [diff] [blame^] | 179 | AddUsersIfInteresting(I); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 184 | void 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 Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 193 | // Use a default AssemblyAnnotationWriter to suppress the default info |
Dan Gohman | 0402577 | 2010-02-14 02:48:58 +0000 | [diff] [blame] | 194 | // comments, which aren't relevant here. |
| 195 | AssemblyAnnotationWriter Annotator; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 196 | for (ilist<IVStrideUse>::const_iterator UI = IVUses.begin(), |
| 197 | E = IVUses.end(); UI != E; ++UI) { |
| 198 | OS << " "; |
| 199 | WriteAsOperand(OS, UI->getOperandValToReplace(), false); |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame] | 200 | OS << " = " << *getReplacementExpr(*UI); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 201 | 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 Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 208 | OS << " in "; |
| 209 | UI->getUser()->print(OS, &Annotator); |
| 210 | OS << '\n'; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 214 | void IVUsers::dump() const { |
David Greene | 63c4560 | 2009-12-23 20:20:46 +0000 | [diff] [blame] | 215 | print(dbgs()); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void IVUsers::releaseMemory() { |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 219 | Processed.clear(); |
Dan Gohman | 6bec5bb | 2009-12-18 00:06:20 +0000 | [diff] [blame] | 220 | IVUses.clear(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame] | 223 | /// getReplacementExpr - Return a SCEV expression which computes the |
| 224 | /// value of the OperandValToReplace. |
| 225 | const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &IU) const { |
| 226 | return SE->getSCEV(IU.getOperandValToReplace()); |
| 227 | } |
| 228 | |
| 229 | /// getExpr - Return the expression for the use. |
| 230 | const 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 Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 238 | static 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 Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame] | 256 | const 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 Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | void IVStrideUse::transformToPostInc(const Loop *L) { |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 263 | PostIncLoops.insert(L); |
| 264 | } |
| 265 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 266 | void IVStrideUse::deleted() { |
| 267 | // Remove this user from the list. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 268 | Parent->IVUses.erase(this); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 269 | // this now dangles! |
| 270 | } |