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