LoopUnroll.cpp revision a305ffb65becc1031abbf85e70aec8fb3c337986
1//===-- UnrollLoop.cpp - Loop unrolling utilities -------------------------===// 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 some loop unrolling utilities. It does not define any 11// actual pass or policy, but provides a single function to perform loop 12// unrolling. 13// 14// The process of unrolling can produce extraneous basic blocks linked with 15// unconditional branches. This will be corrected in the future. 16// 17//===----------------------------------------------------------------------===// 18 19#define DEBUG_TYPE "loop-unroll" 20#include "llvm/Transforms/Utils/UnrollLoop.h" 21#include "llvm/ADT/Statistic.h" 22#include "llvm/Analysis/InstructionSimplify.h" 23#include "llvm/Analysis/LoopIterator.h" 24#include "llvm/Analysis/LoopPass.h" 25#include "llvm/Analysis/ScalarEvolution.h" 26#include "llvm/IR/BasicBlock.h" 27#include "llvm/Support/Debug.h" 28#include "llvm/Support/raw_ostream.h" 29#include "llvm/Transforms/Utils/BasicBlockUtils.h" 30#include "llvm/Transforms/Utils/Cloning.h" 31#include "llvm/Transforms/Utils/Local.h" 32#include "llvm/Transforms/Utils/SimplifyIndVar.h" 33using namespace llvm; 34 35// TODO: Should these be here or in LoopUnroll? 36STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled"); 37STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)"); 38 39/// RemapInstruction - Convert the instruction operands from referencing the 40/// current values into those specified by VMap. 41static inline void RemapInstruction(Instruction *I, 42 ValueToValueMapTy &VMap) { 43 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { 44 Value *Op = I->getOperand(op); 45 ValueToValueMapTy::iterator It = VMap.find(Op); 46 if (It != VMap.end()) 47 I->setOperand(op, It->second); 48 } 49 50 if (PHINode *PN = dyn_cast<PHINode>(I)) { 51 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 52 ValueToValueMapTy::iterator It = VMap.find(PN->getIncomingBlock(i)); 53 if (It != VMap.end()) 54 PN->setIncomingBlock(i, cast<BasicBlock>(It->second)); 55 } 56 } 57} 58 59/// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it 60/// only has one predecessor, and that predecessor only has one successor. 61/// The LoopInfo Analysis that is passed will be kept consistent. 62/// Returns the new combined block. 63static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI, 64 LPPassManager *LPM) { 65 // Merge basic blocks into their predecessor if there is only one distinct 66 // pred, and if there is only one distinct successor of the predecessor, and 67 // if there are no PHI nodes. 68 BasicBlock *OnlyPred = BB->getSinglePredecessor(); 69 if (!OnlyPred) return 0; 70 71 if (OnlyPred->getTerminator()->getNumSuccessors() != 1) 72 return 0; 73 74 DEBUG(dbgs() << "Merging: " << *BB << "into: " << *OnlyPred); 75 76 // Resolve any PHI nodes at the start of the block. They are all 77 // guaranteed to have exactly one entry if they exist, unless there are 78 // multiple duplicate (but guaranteed to be equal) entries for the 79 // incoming edges. This occurs when there are multiple edges from 80 // OnlyPred to OnlySucc. 81 FoldSingleEntryPHINodes(BB); 82 83 // Delete the unconditional branch from the predecessor... 84 OnlyPred->getInstList().pop_back(); 85 86 // Make all PHI nodes that referred to BB now refer to Pred as their 87 // source... 88 BB->replaceAllUsesWith(OnlyPred); 89 90 // Move all definitions in the successor to the predecessor... 91 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList()); 92 93 StringRef OldName = BB->getName(); 94 95 // Erase basic block from the function... 96 97 // ScalarEvolution holds references to loop exit blocks. 98 if (LPM) { 99 if (ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>()) { 100 if (Loop *L = LI->getLoopFor(BB)) 101 SE->forgetLoop(L); 102 } 103 } 104 LI->removeBlock(BB); 105 BB->eraseFromParent(); 106 107 // Inherit predecessor's name if it exists... 108 if (!OldName.empty() && !OnlyPred->hasName()) 109 OnlyPred->setName(OldName); 110 111 return OnlyPred; 112} 113 114/// Unroll the given loop by Count. The loop must be in LCSSA form. Returns true 115/// if unrolling was successful, or false if the loop was unmodified. Unrolling 116/// can only fail when the loop's latch block is not terminated by a conditional 117/// branch instruction. However, if the trip count (and multiple) are not known, 118/// loop unrolling will mostly produce more code that is no faster. 119/// 120/// TripCount is generally defined as the number of times the loop header 121/// executes. UnrollLoop relaxes the definition to permit early exits: here 122/// TripCount is the iteration on which control exits LatchBlock if no early 123/// exits were taken. Note that UnrollLoop assumes that the loop counter test 124/// terminates LatchBlock in order to remove unnecesssary instances of the 125/// test. In other words, control may exit the loop prior to TripCount 126/// iterations via an early branch, but control may not exit the loop from the 127/// LatchBlock's terminator prior to TripCount iterations. 128/// 129/// Similarly, TripMultiple divides the number of times that the LatchBlock may 130/// execute without exiting the loop. 131/// 132/// The LoopInfo Analysis that is passed will be kept consistent. 133/// 134/// If a LoopPassManager is passed in, and the loop is fully removed, it will be 135/// removed from the LoopPassManager as well. LPM can also be NULL. 136/// 137/// This utility preserves LoopInfo. If DominatorTree or ScalarEvolution are 138/// available it must also preserve those analyses. 139bool llvm::UnrollLoop(Loop *L, unsigned Count, unsigned TripCount, 140 bool AllowRuntime, unsigned TripMultiple, 141 LoopInfo *LI, LPPassManager *LPM) { 142 BasicBlock *Preheader = L->getLoopPreheader(); 143 if (!Preheader) { 144 DEBUG(dbgs() << " Can't unroll; loop preheader-insertion failed.\n"); 145 return false; 146 } 147 148 BasicBlock *LatchBlock = L->getLoopLatch(); 149 if (!LatchBlock) { 150 DEBUG(dbgs() << " Can't unroll; loop exit-block-insertion failed.\n"); 151 return false; 152 } 153 154 // Loops with indirectbr cannot be cloned. 155 if (!L->isSafeToClone()) { 156 DEBUG(dbgs() << " Can't unroll; Loop body cannot be cloned.\n"); 157 return false; 158 } 159 160 BasicBlock *Header = L->getHeader(); 161 BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator()); 162 163 if (!BI || BI->isUnconditional()) { 164 // The loop-rotate pass can be helpful to avoid this in many cases. 165 DEBUG(dbgs() << 166 " Can't unroll; loop not terminated by a conditional branch.\n"); 167 return false; 168 } 169 170 if (Header->hasAddressTaken()) { 171 // The loop-rotate pass can be helpful to avoid this in many cases. 172 DEBUG(dbgs() << 173 " Won't unroll loop: address of header block is taken.\n"); 174 return false; 175 } 176 177 if (TripCount != 0) 178 DEBUG(dbgs() << " Trip Count = " << TripCount << "\n"); 179 if (TripMultiple != 1) 180 DEBUG(dbgs() << " Trip Multiple = " << TripMultiple << "\n"); 181 182 // Effectively "DCE" unrolled iterations that are beyond the tripcount 183 // and will never be executed. 184 if (TripCount != 0 && Count > TripCount) 185 Count = TripCount; 186 187 // Don't enter the unroll code if there is nothing to do. This way we don't 188 // need to support "partial unrolling by 1". 189 if (TripCount == 0 && Count < 2) 190 return false; 191 192 assert(Count > 0); 193 assert(TripMultiple > 0); 194 assert(TripCount == 0 || TripCount % TripMultiple == 0); 195 196 // Are we eliminating the loop control altogether? 197 bool CompletelyUnroll = Count == TripCount; 198 199 // We assume a run-time trip count if the compiler cannot 200 // figure out the loop trip count and the unroll-runtime 201 // flag is specified. 202 bool RuntimeTripCount = (TripCount == 0 && Count > 0 && AllowRuntime); 203 204 if (RuntimeTripCount && !UnrollRuntimeLoopProlog(L, Count, LI, LPM)) 205 return false; 206 207 // Notify ScalarEvolution that the loop will be substantially changed, 208 // if not outright eliminated. 209 if (LPM) { 210 ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>(); 211 if (SE) 212 SE->forgetLoop(L); 213 } 214 215 // If we know the trip count, we know the multiple... 216 unsigned BreakoutTrip = 0; 217 if (TripCount != 0) { 218 BreakoutTrip = TripCount % Count; 219 TripMultiple = 0; 220 } else { 221 // Figure out what multiple to use. 222 BreakoutTrip = TripMultiple = 223 (unsigned)GreatestCommonDivisor64(Count, TripMultiple); 224 } 225 226 if (CompletelyUnroll) { 227 DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName() 228 << " with trip count " << TripCount << "!\n"); 229 } else { 230 DEBUG(dbgs() << "UNROLLING loop %" << Header->getName() 231 << " by " << Count); 232 if (TripMultiple == 0 || BreakoutTrip != TripMultiple) { 233 DEBUG(dbgs() << " with a breakout at trip " << BreakoutTrip); 234 } else if (TripMultiple != 1) { 235 DEBUG(dbgs() << " with " << TripMultiple << " trips per branch"); 236 } else if (RuntimeTripCount) { 237 DEBUG(dbgs() << " with run-time trip count"); 238 } 239 DEBUG(dbgs() << "!\n"); 240 } 241 242 bool ContinueOnTrue = L->contains(BI->getSuccessor(0)); 243 BasicBlock *LoopExit = BI->getSuccessor(ContinueOnTrue); 244 245 // For the first iteration of the loop, we should use the precloned values for 246 // PHI nodes. Insert associations now. 247 ValueToValueMapTy LastValueMap; 248 std::vector<PHINode*> OrigPHINode; 249 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { 250 OrigPHINode.push_back(cast<PHINode>(I)); 251 } 252 253 std::vector<BasicBlock*> Headers; 254 std::vector<BasicBlock*> Latches; 255 Headers.push_back(Header); 256 Latches.push_back(LatchBlock); 257 258 // The current on-the-fly SSA update requires blocks to be processed in 259 // reverse postorder so that LastValueMap contains the correct value at each 260 // exit. 261 LoopBlocksDFS DFS(L); 262 DFS.perform(LI); 263 264 // Stash the DFS iterators before adding blocks to the loop. 265 LoopBlocksDFS::RPOIterator BlockBegin = DFS.beginRPO(); 266 LoopBlocksDFS::RPOIterator BlockEnd = DFS.endRPO(); 267 268 for (unsigned It = 1; It != Count; ++It) { 269 std::vector<BasicBlock*> NewBlocks; 270 271 for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) { 272 ValueToValueMapTy VMap; 273 BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It)); 274 Header->getParent()->getBasicBlockList().push_back(New); 275 276 // Loop over all of the PHI nodes in the block, changing them to use the 277 // incoming values from the previous block. 278 if (*BB == Header) 279 for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) { 280 PHINode *NewPHI = cast<PHINode>(VMap[OrigPHINode[i]]); 281 Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock); 282 if (Instruction *InValI = dyn_cast<Instruction>(InVal)) 283 if (It > 1 && L->contains(InValI)) 284 InVal = LastValueMap[InValI]; 285 VMap[OrigPHINode[i]] = InVal; 286 New->getInstList().erase(NewPHI); 287 } 288 289 // Update our running map of newest clones 290 LastValueMap[*BB] = New; 291 for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end(); 292 VI != VE; ++VI) 293 LastValueMap[VI->first] = VI->second; 294 295 L->addBasicBlockToLoop(New, LI->getBase()); 296 297 // Add phi entries for newly created values to all exit blocks. 298 for (succ_iterator SI = succ_begin(*BB), SE = succ_end(*BB); 299 SI != SE; ++SI) { 300 if (L->contains(*SI)) 301 continue; 302 for (BasicBlock::iterator BBI = (*SI)->begin(); 303 PHINode *phi = dyn_cast<PHINode>(BBI); ++BBI) { 304 Value *Incoming = phi->getIncomingValueForBlock(*BB); 305 ValueToValueMapTy::iterator It = LastValueMap.find(Incoming); 306 if (It != LastValueMap.end()) 307 Incoming = It->second; 308 phi->addIncoming(Incoming, New); 309 } 310 } 311 // Keep track of new headers and latches as we create them, so that 312 // we can insert the proper branches later. 313 if (*BB == Header) 314 Headers.push_back(New); 315 if (*BB == LatchBlock) 316 Latches.push_back(New); 317 318 NewBlocks.push_back(New); 319 } 320 321 // Remap all instructions in the most recent iteration 322 for (unsigned i = 0; i < NewBlocks.size(); ++i) 323 for (BasicBlock::iterator I = NewBlocks[i]->begin(), 324 E = NewBlocks[i]->end(); I != E; ++I) 325 ::RemapInstruction(I, LastValueMap); 326 } 327 328 // Loop over the PHI nodes in the original block, setting incoming values. 329 for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) { 330 PHINode *PN = OrigPHINode[i]; 331 if (CompletelyUnroll) { 332 PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader)); 333 Header->getInstList().erase(PN); 334 } 335 else if (Count > 1) { 336 Value *InVal = PN->removeIncomingValue(LatchBlock, false); 337 // If this value was defined in the loop, take the value defined by the 338 // last iteration of the loop. 339 if (Instruction *InValI = dyn_cast<Instruction>(InVal)) { 340 if (L->contains(InValI)) 341 InVal = LastValueMap[InVal]; 342 } 343 assert(Latches.back() == LastValueMap[LatchBlock] && "bad last latch"); 344 PN->addIncoming(InVal, Latches.back()); 345 } 346 } 347 348 // Now that all the basic blocks for the unrolled iterations are in place, 349 // set up the branches to connect them. 350 for (unsigned i = 0, e = Latches.size(); i != e; ++i) { 351 // The original branch was replicated in each unrolled iteration. 352 BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator()); 353 354 // The branch destination. 355 unsigned j = (i + 1) % e; 356 BasicBlock *Dest = Headers[j]; 357 bool NeedConditional = true; 358 359 if (RuntimeTripCount && j != 0) { 360 NeedConditional = false; 361 } 362 363 // For a complete unroll, make the last iteration end with a branch 364 // to the exit block. 365 if (CompletelyUnroll && j == 0) { 366 Dest = LoopExit; 367 NeedConditional = false; 368 } 369 370 // If we know the trip count or a multiple of it, we can safely use an 371 // unconditional branch for some iterations. 372 if (j != BreakoutTrip && (TripMultiple == 0 || j % TripMultiple != 0)) { 373 NeedConditional = false; 374 } 375 376 if (NeedConditional) { 377 // Update the conditional branch's successor for the following 378 // iteration. 379 Term->setSuccessor(!ContinueOnTrue, Dest); 380 } else { 381 // Remove phi operands at this loop exit 382 if (Dest != LoopExit) { 383 BasicBlock *BB = Latches[i]; 384 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); 385 SI != SE; ++SI) { 386 if (*SI == Headers[i]) 387 continue; 388 for (BasicBlock::iterator BBI = (*SI)->begin(); 389 PHINode *Phi = dyn_cast<PHINode>(BBI); ++BBI) { 390 Phi->removeIncomingValue(BB, false); 391 } 392 } 393 } 394 // Replace the conditional branch with an unconditional one. 395 BranchInst::Create(Dest, Term); 396 Term->eraseFromParent(); 397 } 398 } 399 400 // Merge adjacent basic blocks, if possible. 401 for (unsigned i = 0, e = Latches.size(); i != e; ++i) { 402 BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator()); 403 if (Term->isUnconditional()) { 404 BasicBlock *Dest = Term->getSuccessor(0); 405 if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI, LPM)) 406 std::replace(Latches.begin(), Latches.end(), Dest, Fold); 407 } 408 } 409 410 if (LPM) { 411 // FIXME: Reconstruct dom info, because it is not preserved properly. 412 // Incrementally updating domtree after loop unrolling would be easy. 413 if (DominatorTree *DT = LPM->getAnalysisIfAvailable<DominatorTree>()) 414 DT->runOnFunction(*L->getHeader()->getParent()); 415 416 // Simplify any new induction variables in the partially unrolled loop. 417 ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>(); 418 if (SE && !CompletelyUnroll) { 419 SmallVector<WeakVH, 16> DeadInsts; 420 simplifyLoopIVs(L, SE, LPM, DeadInsts); 421 422 // Aggressively clean up dead instructions that simplifyLoopIVs already 423 // identified. Any remaining should be cleaned up below. 424 while (!DeadInsts.empty()) 425 if (Instruction *Inst = 426 dyn_cast_or_null<Instruction>(&*DeadInsts.pop_back_val())) 427 RecursivelyDeleteTriviallyDeadInstructions(Inst); 428 } 429 } 430 // At this point, the code is well formed. We now do a quick sweep over the 431 // inserted code, doing constant propagation and dead code elimination as we 432 // go. 433 const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks(); 434 for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(), 435 BBE = NewLoopBlocks.end(); BB != BBE; ++BB) 436 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ) { 437 Instruction *Inst = I++; 438 439 if (isInstructionTriviallyDead(Inst)) 440 (*BB)->getInstList().erase(Inst); 441 else if (Value *V = SimplifyInstruction(Inst)) 442 if (LI->replacementPreservesLCSSAForm(Inst, V)) { 443 Inst->replaceAllUsesWith(V); 444 (*BB)->getInstList().erase(Inst); 445 } 446 } 447 448 NumCompletelyUnrolled += CompletelyUnroll; 449 ++NumUnrolled; 450 // Remove the loop from the LoopPassManager if it's completely removed. 451 if (CompletelyUnroll && LPM != NULL) 452 LPM->deleteLoopFromQueue(L); 453 454 return true; 455} 456