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 | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 39 | /// isInteresting - Test whether the given expression is "interesting" when |
| 40 | /// used by the given expression, within the context of analyzing the |
| 41 | /// given loop. |
| 42 | static 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 Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 45 | return true; |
| 46 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 47 | // 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 Gohman | b3cdb0e | 2010-04-09 01:22:56 +0000 | [diff] [blame] | 50 | if (AR->getLoop() == L) |
| 51 | return AR->isAffine() || !L->contains(I); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 52 | // Otherwise recurse to see if the start value is interesting. |
| 53 | return isInteresting(AR->getStart(), I, L); |
| 54 | } |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 55 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 56 | // 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 Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 64 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 65 | // Nothing else is interesting here. |
| 66 | return false; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 67 | } |
| 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. |
| 72 | bool 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 Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 84 | const SCEV *ISE = SE->getSCEV(I); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 85 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 86 | // If we've come to an uninteresting expression, stop the traversal and |
| 87 | // call this a user. |
| 88 | if (!isInteresting(ISE, I, L)) |
Jim Grosbach | 97200e4 | 2009-11-19 02:05:44 +0000 | [diff] [blame] | 89 | return false; |
| 90 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 91 | 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 Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 105 | // consider references outside the loop in all cases. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 106 | // 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 Greene | 63c4560 | 2009-12-23 20:20:46 +0000 | [diff] [blame] | 112 | DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n' |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 113 | << " OF SCEV: " << *ISE << '\n'); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 114 | AddUserToIVUsers = true; |
| 115 | } |
| 116 | } else if (Processed.count(User) || |
| 117 | !AddUsersIfInteresting(User)) { |
David Greene | 63c4560 | 2009-12-23 20:20:46 +0000 | [diff] [blame] | 118 | DEBUG(dbgs() << "FOUND USER: " << *User << '\n' |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 119 | << " OF SCEV: " << *ISE << '\n'); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 120 | AddUserToIVUsers = true; |
| 121 | } |
| 122 | |
| 123 | if (AddUserToIVUsers) { |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 124 | // Okay, we found a user that we cannot reduce. |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame^] | 125 | IVUses.push_back(new IVStrideUse(this, User, I)); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 126 | IVStrideUse &NewUse = IVUses.back(); |
| 127 | // Transform the expression into a normalized form. |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame^] | 128 | ISE = TransformForPostIncUse(NormalizeAutodetect, |
| 129 | ISE, User, I, |
| 130 | NewUse.PostIncLoops, |
| 131 | *SE, *DT); |
| 132 | DEBUG(dbgs() << " NORMALIZED TO: " << *ISE << '\n'); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame^] | 138 | IVStrideUse &IVUsers::AddUser(Instruction *User, Value *Operand) { |
| 139 | IVUses.push_back(new IVStrideUse(this, User, Operand)); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 140 | return IVUses.back(); |
Evan Cheng | 586f69a | 2009-11-12 07:35:05 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 143 | IVUsers::IVUsers() |
| 144 | : LoopPass(&ID) { |
| 145 | } |
| 146 | |
| 147 | void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const { |
| 148 | AU.addRequired<LoopInfo>(); |
| 149 | AU.addRequired<DominatorTree>(); |
| 150 | AU.addRequired<ScalarEvolution>(); |
| 151 | AU.setPreservesAll(); |
| 152 | } |
| 153 | |
| 154 | bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) { |
| 155 | |
| 156 | L = l; |
| 157 | LI = &getAnalysis<LoopInfo>(); |
| 158 | DT = &getAnalysis<DominatorTree>(); |
| 159 | SE = &getAnalysis<ScalarEvolution>(); |
| 160 | |
| 161 | // Find all uses of induction variables in this loop, and categorize |
| 162 | // them by stride. Start by finding all of the PHI nodes in the header for |
| 163 | // this loop. If they are induction variables, inspect their uses. |
| 164 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) |
Dan Gohman | e521ccb | 2010-04-11 19:30:19 +0000 | [diff] [blame] | 165 | (void)AddUsersIfInteresting(I); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 166 | |
| 167 | return false; |
| 168 | } |
| 169 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 170 | void IVUsers::print(raw_ostream &OS, const Module *M) const { |
| 171 | OS << "IV Users for loop "; |
| 172 | WriteAsOperand(OS, L->getHeader(), false); |
| 173 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 174 | OS << " with backedge-taken count " |
| 175 | << *SE->getBackedgeTakenCount(L); |
| 176 | } |
| 177 | OS << ":\n"; |
| 178 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 179 | // Use a default AssemblyAnnotationWriter to suppress the default info |
Dan Gohman | 0402577 | 2010-02-14 02:48:58 +0000 | [diff] [blame] | 180 | // comments, which aren't relevant here. |
| 181 | AssemblyAnnotationWriter Annotator; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 182 | for (ilist<IVStrideUse>::const_iterator UI = IVUses.begin(), |
| 183 | E = IVUses.end(); UI != E; ++UI) { |
| 184 | OS << " "; |
| 185 | WriteAsOperand(OS, UI->getOperandValToReplace(), false); |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame^] | 186 | OS << " = " << *getReplacementExpr(*UI); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 187 | for (PostIncLoopSet::const_iterator |
| 188 | I = UI->PostIncLoops.begin(), |
| 189 | E = UI->PostIncLoops.end(); I != E; ++I) { |
| 190 | OS << " (post-inc with loop "; |
| 191 | WriteAsOperand(OS, (*I)->getHeader(), false); |
| 192 | OS << ")"; |
| 193 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 194 | OS << " in "; |
| 195 | UI->getUser()->print(OS, &Annotator); |
| 196 | OS << '\n'; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 200 | void IVUsers::dump() const { |
David Greene | 63c4560 | 2009-12-23 20:20:46 +0000 | [diff] [blame] | 201 | print(dbgs()); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void IVUsers::releaseMemory() { |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 205 | Processed.clear(); |
Dan Gohman | 6bec5bb | 2009-12-18 00:06:20 +0000 | [diff] [blame] | 206 | IVUses.clear(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame^] | 209 | /// getReplacementExpr - Return a SCEV expression which computes the |
| 210 | /// value of the OperandValToReplace. |
| 211 | const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &IU) const { |
| 212 | return SE->getSCEV(IU.getOperandValToReplace()); |
| 213 | } |
| 214 | |
| 215 | /// getExpr - Return the expression for the use. |
| 216 | const SCEV *IVUsers::getExpr(const IVStrideUse &IU) const { |
| 217 | return |
| 218 | TransformForPostIncUse(Normalize, getReplacementExpr(IU), |
| 219 | IU.getUser(), IU.getOperandValToReplace(), |
| 220 | const_cast<PostIncLoopSet &>(IU.getPostIncLoops()), |
| 221 | *SE, *DT); |
| 222 | } |
| 223 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 224 | static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) { |
| 225 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 226 | if (AR->getLoop() == L) |
| 227 | return AR; |
| 228 | return findAddRecForLoop(AR->getStart(), L); |
| 229 | } |
| 230 | |
| 231 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 232 | for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end(); |
| 233 | I != E; ++I) |
| 234 | if (const SCEVAddRecExpr *AR = findAddRecForLoop(*I, L)) |
| 235 | return AR; |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Dan Gohman | c056454 | 2010-04-19 21:48:58 +0000 | [diff] [blame^] | 242 | const SCEV *IVUsers::getStride(const IVStrideUse &IU, const Loop *L) const { |
| 243 | if (const SCEVAddRecExpr *AR = findAddRecForLoop(getExpr(IU), L)) |
| 244 | return AR->getStepRecurrence(*SE); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | void IVStrideUse::transformToPostInc(const Loop *L) { |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 249 | PostIncLoops.insert(L); |
| 250 | } |
| 251 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 252 | void IVStrideUse::deleted() { |
| 253 | // Remove this user from the list. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 254 | Parent->IVUses.erase(this); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 255 | // this now dangles! |
| 256 | } |