34Pass *llvm::createIVUsersPass() { 35 return new IVUsers(); 36} 37 38/// containsAddRecFromDifferentLoop - Determine whether expression S involves a 39/// subexpression that is an AddRec from a loop other than L. An outer loop 40/// of L is OK, but not an inner loop nor a disjoint loop. 41static bool containsAddRecFromDifferentLoop(const SCEV *S, Loop *L) { 42 // This is very common, put it first. 43 if (isa<SCEVConstant>(S)) 44 return false; 45 if (const SCEVCommutativeExpr *AE = dyn_cast<SCEVCommutativeExpr>(S)) { 46 for (unsigned int i=0; i< AE->getNumOperands(); i++) 47 if (containsAddRecFromDifferentLoop(AE->getOperand(i), L)) 48 return true; 49 return false; 50 } 51 if (const SCEVAddRecExpr *AE = dyn_cast<SCEVAddRecExpr>(S)) { 52 if (const Loop *newLoop = AE->getLoop()) { 53 if (newLoop == L) 54 return false; 55 // if newLoop is an outer loop of L, this is OK. 56 if (!LoopInfo::isNotAlreadyContainedIn(L, newLoop)) 57 return false; 58 } 59 return true; 60 } 61 if (const SCEVUDivExpr *DE = dyn_cast<SCEVUDivExpr>(S)) 62 return containsAddRecFromDifferentLoop(DE->getLHS(), L) || 63 containsAddRecFromDifferentLoop(DE->getRHS(), L); 64#if 0 65 // SCEVSDivExpr has been backed out temporarily, but will be back; we'll 66 // need this when it is. 67 if (const SCEVSDivExpr *DE = dyn_cast<SCEVSDivExpr>(S)) 68 return containsAddRecFromDifferentLoop(DE->getLHS(), L) || 69 containsAddRecFromDifferentLoop(DE->getRHS(), L); 70#endif 71 if (const SCEVCastExpr *CE = dyn_cast<SCEVCastExpr>(S)) 72 return containsAddRecFromDifferentLoop(CE->getOperand(), L); 73 return false; 74} 75 76/// getSCEVStartAndStride - Compute the start and stride of this expression, 77/// returning false if the expression is not a start/stride pair, or true if it 78/// is. The stride must be a loop invariant expression, but the start may be 79/// a mix of loop invariant and loop variant expressions. The start cannot, 80/// however, contain an AddRec from a different loop, unless that loop is an 81/// outer loop of the current loop. 82static bool getSCEVStartAndStride(const SCEV *&SH, Loop *L, Loop *UseLoop, 83 const SCEV *&Start, const SCEV *&Stride, 84 ScalarEvolution *SE, DominatorTree *DT) { 85 const SCEV *TheAddRec = Start; // Initialize to zero. 86 87 // If the outer level is an AddExpr, the operands are all start values except 88 // for a nested AddRecExpr. 89 if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) { 90 for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i) 91 if (const SCEVAddRecExpr *AddRec = 92 dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) { 93 if (AddRec->getLoop() == L) 94 TheAddRec = SE->getAddExpr(AddRec, TheAddRec); 95 else 96 return false; // Nested IV of some sort? 97 } else { 98 Start = SE->getAddExpr(Start, AE->getOperand(i)); 99 } 100 } else if (isa<SCEVAddRecExpr>(SH)) { 101 TheAddRec = SH; 102 } else { 103 return false; // not analyzable. 104 } 105 106 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec); 107 if (!AddRec || AddRec->getLoop() != L) return false; 108 109 // Use getSCEVAtScope to attempt to simplify other loops out of 110 // the picture. 111 const SCEV *AddRecStart = AddRec->getStart(); 112 AddRecStart = SE->getSCEVAtScope(AddRecStart, UseLoop); 113 const SCEV *AddRecStride = AddRec->getStepRecurrence(*SE); 114 115 // FIXME: If Start contains an SCEVAddRecExpr from a different loop, other 116 // than an outer loop of the current loop, reject it. LSR has no concept of 117 // operating on more than one loop at a time so don't confuse it with such 118 // expressions. 119 if (containsAddRecFromDifferentLoop(AddRecStart, L)) 120 return false; 121 122 Start = SE->getAddExpr(Start, AddRecStart); 123 124 // If stride is an instruction, make sure it properly dominates the header. 125 // Otherwise we could end up with a use before def situation. 126 if (!isa<SCEVConstant>(AddRecStride)) { 127 BasicBlock *Header = L->getHeader(); 128 if (!AddRecStride->properlyDominates(Header, DT)) 129 return false; 130 131 DEBUG(errs() << "[" << L->getHeader()->getName() 132 << "] Variable stride: " << *AddRec << "\n"); 133 } 134 135 Stride = AddRecStride; 136 return true; 137} 138 139/// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression 140/// and now we need to decide whether the user should use the preinc or post-inc 141/// value. If this user should use the post-inc version of the IV, return true. 142/// 143/// Choosing wrong here can break dominance properties (if we choose to use the 144/// post-inc value when we cannot) or it can end up adding extra live-ranges to 145/// the loop, resulting in reg-reg copies (if we use the pre-inc value when we 146/// should use the post-inc value). 147static bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV, 148 Loop *L, LoopInfo *LI, DominatorTree *DT, 149 Pass *P) { 150 // If the user is in the loop, use the preinc value. 151 if (L->contains(User->getParent())) return false; 152 153 BasicBlock *LatchBlock = L->getLoopLatch(); 154 if (!LatchBlock) 155 return false; 156 157 // Ok, the user is outside of the loop. If it is dominated by the latch 158 // block, use the post-inc value. 159 if (DT->dominates(LatchBlock, User->getParent())) 160 return true; 161 162 // There is one case we have to be careful of: PHI nodes. These little guys 163 // can live in blocks that are not dominated by the latch block, but (since 164 // their uses occur in the predecessor block, not the block the PHI lives in) 165 // should still use the post-inc value. Check for this case now. 166 PHINode *PN = dyn_cast<PHINode>(User); 167 if (!PN) return false; // not a phi, not dominated by latch block. 168 169 // Look at all of the uses of IV by the PHI node. If any use corresponds to 170 // a block that is not dominated by the latch block, give up and use the 171 // preincremented value. 172 unsigned NumUses = 0; 173 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 174 if (PN->getIncomingValue(i) == IV) { 175 ++NumUses; 176 if (!DT->dominates(LatchBlock, PN->getIncomingBlock(i))) 177 return false; 178 } 179 180 // Okay, all uses of IV by PN are in predecessor blocks that really are 181 // dominated by the latch block. Use the post-incremented value. 182 return true; 183} 184 185/// AddUsersIfInteresting - Inspect the specified instruction. If it is a 186/// reducible SCEV, recursively add its users to the IVUsesByStride set and 187/// return true. Otherwise, return false. 188bool IVUsers::AddUsersIfInteresting(Instruction *I) { 189 if (!SE->isSCEVable(I->getType())) 190 return false; // Void and FP expressions cannot be reduced. 191 192 // LSR is not APInt clean, do not touch integers bigger than 64-bits. 193 if (SE->getTypeSizeInBits(I->getType()) > 64) 194 return false; 195 196 if (!Processed.insert(I)) 197 return true; // Instruction already handled. 198 199 // Get the symbolic expression for this instruction. 200 const SCEV *ISE = SE->getSCEV(I); 201 if (isa<SCEVCouldNotCompute>(ISE)) return false; 202 203 // Get the start and stride for this expression. 204 Loop *UseLoop = LI->getLoopFor(I->getParent()); 205 const SCEV *Start = SE->getIntegerSCEV(0, ISE->getType()); 206 const SCEV *Stride = Start; 207 208 if (!getSCEVStartAndStride(ISE, L, UseLoop, Start, Stride, SE, DT)) 209 return false; // Non-reducible symbolic expression, bail out. 210
| 39Pass *llvm::createIVUsersPass() { 40 return new IVUsers(); 41} 42 43/// containsAddRecFromDifferentLoop - Determine whether expression S involves a 44/// subexpression that is an AddRec from a loop other than L. An outer loop 45/// of L is OK, but not an inner loop nor a disjoint loop. 46static bool containsAddRecFromDifferentLoop(const SCEV *S, Loop *L) { 47 // This is very common, put it first. 48 if (isa<SCEVConstant>(S)) 49 return false; 50 if (const SCEVCommutativeExpr *AE = dyn_cast<SCEVCommutativeExpr>(S)) { 51 for (unsigned int i=0; i< AE->getNumOperands(); i++) 52 if (containsAddRecFromDifferentLoop(AE->getOperand(i), L)) 53 return true; 54 return false; 55 } 56 if (const SCEVAddRecExpr *AE = dyn_cast<SCEVAddRecExpr>(S)) { 57 if (const Loop *newLoop = AE->getLoop()) { 58 if (newLoop == L) 59 return false; 60 // if newLoop is an outer loop of L, this is OK. 61 if (!LoopInfo::isNotAlreadyContainedIn(L, newLoop)) 62 return false; 63 } 64 return true; 65 } 66 if (const SCEVUDivExpr *DE = dyn_cast<SCEVUDivExpr>(S)) 67 return containsAddRecFromDifferentLoop(DE->getLHS(), L) || 68 containsAddRecFromDifferentLoop(DE->getRHS(), L); 69#if 0 70 // SCEVSDivExpr has been backed out temporarily, but will be back; we'll 71 // need this when it is. 72 if (const SCEVSDivExpr *DE = dyn_cast<SCEVSDivExpr>(S)) 73 return containsAddRecFromDifferentLoop(DE->getLHS(), L) || 74 containsAddRecFromDifferentLoop(DE->getRHS(), L); 75#endif 76 if (const SCEVCastExpr *CE = dyn_cast<SCEVCastExpr>(S)) 77 return containsAddRecFromDifferentLoop(CE->getOperand(), L); 78 return false; 79} 80 81/// getSCEVStartAndStride - Compute the start and stride of this expression, 82/// returning false if the expression is not a start/stride pair, or true if it 83/// is. The stride must be a loop invariant expression, but the start may be 84/// a mix of loop invariant and loop variant expressions. The start cannot, 85/// however, contain an AddRec from a different loop, unless that loop is an 86/// outer loop of the current loop. 87static bool getSCEVStartAndStride(const SCEV *&SH, Loop *L, Loop *UseLoop, 88 const SCEV *&Start, const SCEV *&Stride, 89 ScalarEvolution *SE, DominatorTree *DT) { 90 const SCEV *TheAddRec = Start; // Initialize to zero. 91 92 // If the outer level is an AddExpr, the operands are all start values except 93 // for a nested AddRecExpr. 94 if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) { 95 for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i) 96 if (const SCEVAddRecExpr *AddRec = 97 dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) { 98 if (AddRec->getLoop() == L) 99 TheAddRec = SE->getAddExpr(AddRec, TheAddRec); 100 else 101 return false; // Nested IV of some sort? 102 } else { 103 Start = SE->getAddExpr(Start, AE->getOperand(i)); 104 } 105 } else if (isa<SCEVAddRecExpr>(SH)) { 106 TheAddRec = SH; 107 } else { 108 return false; // not analyzable. 109 } 110 111 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec); 112 if (!AddRec || AddRec->getLoop() != L) return false; 113 114 // Use getSCEVAtScope to attempt to simplify other loops out of 115 // the picture. 116 const SCEV *AddRecStart = AddRec->getStart(); 117 AddRecStart = SE->getSCEVAtScope(AddRecStart, UseLoop); 118 const SCEV *AddRecStride = AddRec->getStepRecurrence(*SE); 119 120 // FIXME: If Start contains an SCEVAddRecExpr from a different loop, other 121 // than an outer loop of the current loop, reject it. LSR has no concept of 122 // operating on more than one loop at a time so don't confuse it with such 123 // expressions. 124 if (containsAddRecFromDifferentLoop(AddRecStart, L)) 125 return false; 126 127 Start = SE->getAddExpr(Start, AddRecStart); 128 129 // If stride is an instruction, make sure it properly dominates the header. 130 // Otherwise we could end up with a use before def situation. 131 if (!isa<SCEVConstant>(AddRecStride)) { 132 BasicBlock *Header = L->getHeader(); 133 if (!AddRecStride->properlyDominates(Header, DT)) 134 return false; 135 136 DEBUG(errs() << "[" << L->getHeader()->getName() 137 << "] Variable stride: " << *AddRec << "\n"); 138 } 139 140 Stride = AddRecStride; 141 return true; 142} 143 144/// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression 145/// and now we need to decide whether the user should use the preinc or post-inc 146/// value. If this user should use the post-inc version of the IV, return true. 147/// 148/// Choosing wrong here can break dominance properties (if we choose to use the 149/// post-inc value when we cannot) or it can end up adding extra live-ranges to 150/// the loop, resulting in reg-reg copies (if we use the pre-inc value when we 151/// should use the post-inc value). 152static bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV, 153 Loop *L, LoopInfo *LI, DominatorTree *DT, 154 Pass *P) { 155 // If the user is in the loop, use the preinc value. 156 if (L->contains(User->getParent())) return false; 157 158 BasicBlock *LatchBlock = L->getLoopLatch(); 159 if (!LatchBlock) 160 return false; 161 162 // Ok, the user is outside of the loop. If it is dominated by the latch 163 // block, use the post-inc value. 164 if (DT->dominates(LatchBlock, User->getParent())) 165 return true; 166 167 // There is one case we have to be careful of: PHI nodes. These little guys 168 // can live in blocks that are not dominated by the latch block, but (since 169 // their uses occur in the predecessor block, not the block the PHI lives in) 170 // should still use the post-inc value. Check for this case now. 171 PHINode *PN = dyn_cast<PHINode>(User); 172 if (!PN) return false; // not a phi, not dominated by latch block. 173 174 // Look at all of the uses of IV by the PHI node. If any use corresponds to 175 // a block that is not dominated by the latch block, give up and use the 176 // preincremented value. 177 unsigned NumUses = 0; 178 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 179 if (PN->getIncomingValue(i) == IV) { 180 ++NumUses; 181 if (!DT->dominates(LatchBlock, PN->getIncomingBlock(i))) 182 return false; 183 } 184 185 // Okay, all uses of IV by PN are in predecessor blocks that really are 186 // dominated by the latch block. Use the post-incremented value. 187 return true; 188} 189 190/// AddUsersIfInteresting - Inspect the specified instruction. If it is a 191/// reducible SCEV, recursively add its users to the IVUsesByStride set and 192/// return true. Otherwise, return false. 193bool IVUsers::AddUsersIfInteresting(Instruction *I) { 194 if (!SE->isSCEVable(I->getType())) 195 return false; // Void and FP expressions cannot be reduced. 196 197 // LSR is not APInt clean, do not touch integers bigger than 64-bits. 198 if (SE->getTypeSizeInBits(I->getType()) > 64) 199 return false; 200 201 if (!Processed.insert(I)) 202 return true; // Instruction already handled. 203 204 // Get the symbolic expression for this instruction. 205 const SCEV *ISE = SE->getSCEV(I); 206 if (isa<SCEVCouldNotCompute>(ISE)) return false; 207 208 // Get the start and stride for this expression. 209 Loop *UseLoop = LI->getLoopFor(I->getParent()); 210 const SCEV *Start = SE->getIntegerSCEV(0, ISE->getType()); 211 const SCEV *Stride = Start; 212 213 if (!getSCEVStartAndStride(ISE, L, UseLoop, Start, Stride, SE, DT)) 214 return false; // Non-reducible symbolic expression, bail out. 215
|
211 SmallPtrSet<Instruction *, 4> UniqueUsers; 212 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); 213 UI != E; ++UI) { 214 Instruction *User = cast<Instruction>(*UI); 215 if (!UniqueUsers.insert(User)) 216 continue; 217 218 // Do not infinitely recurse on PHI nodes. 219 if (isa<PHINode>(User) && Processed.count(User)) 220 continue; 221 222 // Descend recursively, but not into PHI nodes outside the current loop. 223 // It's important to see the entire expression outside the loop to get 224 // choices that depend on addressing mode use right, although we won't 225 // consider references ouside the loop in all cases. 226 // If User is already in Processed, we don't want to recurse into it again, 227 // but do want to record a second reference in the same instruction. 228 bool AddUserToIVUsers = false; 229 if (LI->getLoopFor(User->getParent()) != L) { 230 if (isa<PHINode>(User) || Processed.count(User) || 231 !AddUsersIfInteresting(User)) { 232 DEBUG(errs() << "FOUND USER in other loop: " << *User << '\n' 233 << " OF SCEV: " << *ISE << '\n'); 234 AddUserToIVUsers = true; 235 } 236 } else if (Processed.count(User) || 237 !AddUsersIfInteresting(User)) { 238 DEBUG(errs() << "FOUND USER: " << *User << '\n' 239 << " OF SCEV: " << *ISE << '\n'); 240 AddUserToIVUsers = true; 241 } 242 243 if (AddUserToIVUsers) { 244 IVUsersOfOneStride *StrideUses = IVUsesByStride[Stride]; 245 if (!StrideUses) { // First occurrence of this stride? 246 StrideOrder.push_back(Stride); 247 StrideUses = new IVUsersOfOneStride(Stride); 248 IVUses.push_back(StrideUses); 249 IVUsesByStride[Stride] = StrideUses; 250 } 251 252 // Okay, we found a user that we cannot reduce. Analyze the instruction 253 // and decide what to do with it. If we are a use inside of the loop, use 254 // the value before incrementation, otherwise use it after incrementation. 255 if (IVUseShouldUsePostIncValue(User, I, L, LI, DT, this)) { 256 // The value used will be incremented by the stride more than we are 257 // expecting, so subtract this off. 258 const SCEV *NewStart = SE->getMinusSCEV(Start, Stride); 259 StrideUses->addUser(NewStart, User, I); 260 StrideUses->Users.back().setIsUseOfPostIncrementedValue(true); 261 DEBUG(errs() << " USING POSTINC SCEV, START=" << *NewStart<< "\n"); 262 } else { 263 StrideUses->addUser(Start, User, I); 264 } 265 } 266 } 267 return true; 268} 269 270void IVUsers::AddUser(const SCEV *Stride, const SCEV *Offset, 271 Instruction *User, Value *Operand) { 272 IVUsersOfOneStride *StrideUses = IVUsesByStride[Stride]; 273 if (!StrideUses) { // First occurrence of this stride? 274 StrideOrder.push_back(Stride); 275 StrideUses = new IVUsersOfOneStride(Stride); 276 IVUses.push_back(StrideUses); 277 IVUsesByStride[Stride] = StrideUses; 278 } 279 IVUsesByStride[Stride]->addUser(Offset, User, Operand); 280} 281 282IVUsers::IVUsers() 283 : LoopPass(&ID) { 284} 285 286void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const { 287 AU.addRequired<LoopInfo>(); 288 AU.addRequired<DominatorTree>(); 289 AU.addRequired<ScalarEvolution>(); 290 AU.setPreservesAll(); 291} 292 293bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) { 294 295 L = l; 296 LI = &getAnalysis<LoopInfo>(); 297 DT = &getAnalysis<DominatorTree>(); 298 SE = &getAnalysis<ScalarEvolution>(); 299 300 // Find all uses of induction variables in this loop, and categorize 301 // them by stride. Start by finding all of the PHI nodes in the header for 302 // this loop. If they are induction variables, inspect their uses. 303 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) 304 AddUsersIfInteresting(I); 305 306 return false; 307} 308 309/// getReplacementExpr - Return a SCEV expression which computes the 310/// value of the OperandValToReplace of the given IVStrideUse. 311const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &U) const { 312 // Start with zero. 313 const SCEV *RetVal = SE->getIntegerSCEV(0, U.getParent()->Stride->getType()); 314 // Create the basic add recurrence. 315 RetVal = SE->getAddRecExpr(RetVal, U.getParent()->Stride, L); 316 // Add the offset in a separate step, because it may be loop-variant. 317 RetVal = SE->getAddExpr(RetVal, U.getOffset()); 318 // For uses of post-incremented values, add an extra stride to compute 319 // the actual replacement value. 320 if (U.isUseOfPostIncrementedValue()) 321 RetVal = SE->getAddExpr(RetVal, U.getParent()->Stride); 322 // Evaluate the expression out of the loop, if possible. 323 if (!L->contains(U.getUser()->getParent())) { 324 const SCEV *ExitVal = SE->getSCEVAtScope(RetVal, L->getParentLoop()); 325 if (ExitVal->isLoopInvariant(L)) 326 RetVal = ExitVal; 327 } 328 return RetVal; 329} 330 331void IVUsers::print(raw_ostream &OS, const Module *M) const { 332 OS << "IV Users for loop "; 333 WriteAsOperand(OS, L->getHeader(), false); 334 if (SE->hasLoopInvariantBackedgeTakenCount(L)) { 335 OS << " with backedge-taken count " 336 << *SE->getBackedgeTakenCount(L); 337 } 338 OS << ":\n"; 339 340 for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) { 341 std::map<const SCEV *, IVUsersOfOneStride*>::const_iterator SI = 342 IVUsesByStride.find(StrideOrder[Stride]); 343 assert(SI != IVUsesByStride.end() && "Stride doesn't exist!"); 344 OS << " Stride " << *SI->first->getType() << " " << *SI->first << ":\n"; 345 346 for (ilist<IVStrideUse>::const_iterator UI = SI->second->Users.begin(), 347 E = SI->second->Users.end(); UI != E; ++UI) { 348 OS << " "; 349 WriteAsOperand(OS, UI->getOperandValToReplace(), false); 350 OS << " = "; 351 OS << *getReplacementExpr(*UI); 352 if (UI->isUseOfPostIncrementedValue()) 353 OS << " (post-inc)"; 354 OS << " in "; 355 UI->getUser()->print(OS); 356 OS << '\n'; 357 } 358 } 359} 360 361void IVUsers::dump() const { 362 print(errs()); 363} 364 365void IVUsers::releaseMemory() { 366 IVUsesByStride.clear(); 367 StrideOrder.clear(); 368 Processed.clear(); 369} 370 371void IVStrideUse::deleted() { 372 // Remove this user from the list. 373 Parent->Users.erase(this); 374 // this now dangles! 375}
| 221 SmallPtrSet<Instruction *, 4> UniqueUsers; 222 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); 223 UI != E; ++UI) { 224 Instruction *User = cast<Instruction>(*UI); 225 if (!UniqueUsers.insert(User)) 226 continue; 227 228 // Do not infinitely recurse on PHI nodes. 229 if (isa<PHINode>(User) && Processed.count(User)) 230 continue; 231 232 // Descend recursively, but not into PHI nodes outside the current loop. 233 // It's important to see the entire expression outside the loop to get 234 // choices that depend on addressing mode use right, although we won't 235 // consider references ouside the loop in all cases. 236 // If User is already in Processed, we don't want to recurse into it again, 237 // but do want to record a second reference in the same instruction. 238 bool AddUserToIVUsers = false; 239 if (LI->getLoopFor(User->getParent()) != L) { 240 if (isa<PHINode>(User) || Processed.count(User) || 241 !AddUsersIfInteresting(User)) { 242 DEBUG(errs() << "FOUND USER in other loop: " << *User << '\n' 243 << " OF SCEV: " << *ISE << '\n'); 244 AddUserToIVUsers = true; 245 } 246 } else if (Processed.count(User) || 247 !AddUsersIfInteresting(User)) { 248 DEBUG(errs() << "FOUND USER: " << *User << '\n' 249 << " OF SCEV: " << *ISE << '\n'); 250 AddUserToIVUsers = true; 251 } 252 253 if (AddUserToIVUsers) { 254 IVUsersOfOneStride *StrideUses = IVUsesByStride[Stride]; 255 if (!StrideUses) { // First occurrence of this stride? 256 StrideOrder.push_back(Stride); 257 StrideUses = new IVUsersOfOneStride(Stride); 258 IVUses.push_back(StrideUses); 259 IVUsesByStride[Stride] = StrideUses; 260 } 261 262 // Okay, we found a user that we cannot reduce. Analyze the instruction 263 // and decide what to do with it. If we are a use inside of the loop, use 264 // the value before incrementation, otherwise use it after incrementation. 265 if (IVUseShouldUsePostIncValue(User, I, L, LI, DT, this)) { 266 // The value used will be incremented by the stride more than we are 267 // expecting, so subtract this off. 268 const SCEV *NewStart = SE->getMinusSCEV(Start, Stride); 269 StrideUses->addUser(NewStart, User, I); 270 StrideUses->Users.back().setIsUseOfPostIncrementedValue(true); 271 DEBUG(errs() << " USING POSTINC SCEV, START=" << *NewStart<< "\n"); 272 } else { 273 StrideUses->addUser(Start, User, I); 274 } 275 } 276 } 277 return true; 278} 279 280void IVUsers::AddUser(const SCEV *Stride, const SCEV *Offset, 281 Instruction *User, Value *Operand) { 282 IVUsersOfOneStride *StrideUses = IVUsesByStride[Stride]; 283 if (!StrideUses) { // First occurrence of this stride? 284 StrideOrder.push_back(Stride); 285 StrideUses = new IVUsersOfOneStride(Stride); 286 IVUses.push_back(StrideUses); 287 IVUsesByStride[Stride] = StrideUses; 288 } 289 IVUsesByStride[Stride]->addUser(Offset, User, Operand); 290} 291 292IVUsers::IVUsers() 293 : LoopPass(&ID) { 294} 295 296void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const { 297 AU.addRequired<LoopInfo>(); 298 AU.addRequired<DominatorTree>(); 299 AU.addRequired<ScalarEvolution>(); 300 AU.setPreservesAll(); 301} 302 303bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) { 304 305 L = l; 306 LI = &getAnalysis<LoopInfo>(); 307 DT = &getAnalysis<DominatorTree>(); 308 SE = &getAnalysis<ScalarEvolution>(); 309 310 // Find all uses of induction variables in this loop, and categorize 311 // them by stride. Start by finding all of the PHI nodes in the header for 312 // this loop. If they are induction variables, inspect their uses. 313 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) 314 AddUsersIfInteresting(I); 315 316 return false; 317} 318 319/// getReplacementExpr - Return a SCEV expression which computes the 320/// value of the OperandValToReplace of the given IVStrideUse. 321const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &U) const { 322 // Start with zero. 323 const SCEV *RetVal = SE->getIntegerSCEV(0, U.getParent()->Stride->getType()); 324 // Create the basic add recurrence. 325 RetVal = SE->getAddRecExpr(RetVal, U.getParent()->Stride, L); 326 // Add the offset in a separate step, because it may be loop-variant. 327 RetVal = SE->getAddExpr(RetVal, U.getOffset()); 328 // For uses of post-incremented values, add an extra stride to compute 329 // the actual replacement value. 330 if (U.isUseOfPostIncrementedValue()) 331 RetVal = SE->getAddExpr(RetVal, U.getParent()->Stride); 332 // Evaluate the expression out of the loop, if possible. 333 if (!L->contains(U.getUser()->getParent())) { 334 const SCEV *ExitVal = SE->getSCEVAtScope(RetVal, L->getParentLoop()); 335 if (ExitVal->isLoopInvariant(L)) 336 RetVal = ExitVal; 337 } 338 return RetVal; 339} 340 341void IVUsers::print(raw_ostream &OS, const Module *M) const { 342 OS << "IV Users for loop "; 343 WriteAsOperand(OS, L->getHeader(), false); 344 if (SE->hasLoopInvariantBackedgeTakenCount(L)) { 345 OS << " with backedge-taken count " 346 << *SE->getBackedgeTakenCount(L); 347 } 348 OS << ":\n"; 349 350 for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) { 351 std::map<const SCEV *, IVUsersOfOneStride*>::const_iterator SI = 352 IVUsesByStride.find(StrideOrder[Stride]); 353 assert(SI != IVUsesByStride.end() && "Stride doesn't exist!"); 354 OS << " Stride " << *SI->first->getType() << " " << *SI->first << ":\n"; 355 356 for (ilist<IVStrideUse>::const_iterator UI = SI->second->Users.begin(), 357 E = SI->second->Users.end(); UI != E; ++UI) { 358 OS << " "; 359 WriteAsOperand(OS, UI->getOperandValToReplace(), false); 360 OS << " = "; 361 OS << *getReplacementExpr(*UI); 362 if (UI->isUseOfPostIncrementedValue()) 363 OS << " (post-inc)"; 364 OS << " in "; 365 UI->getUser()->print(OS); 366 OS << '\n'; 367 } 368 } 369} 370 371void IVUsers::dump() const { 372 print(errs()); 373} 374 375void IVUsers::releaseMemory() { 376 IVUsesByStride.clear(); 377 StrideOrder.clear(); 378 Processed.clear(); 379} 380 381void IVStrideUse::deleted() { 382 // Remove this user from the list. 383 Parent->Users.erase(this); 384 // this now dangles! 385}
|