LLVM  6.0.0svn
LoopUnroll.cpp
Go to the documentation of this file.
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 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Analysis/LoopPass.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/Support/Debug.h"
42 using namespace llvm;
43 
44 #define DEBUG_TYPE "loop-unroll"
45 
46 // TODO: Should these be here or in LoopUnroll?
47 STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled");
48 STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)");
49 
50 static cl::opt<bool>
51 UnrollRuntimeEpilog("unroll-runtime-epilog", cl::init(false), cl::Hidden,
52  cl::desc("Allow runtime unrolled loops to be unrolled "
53  "with epilog instead of prolog."));
54 
55 static cl::opt<bool>
56 UnrollVerifyDomtree("unroll-verify-domtree", cl::Hidden,
57  cl::desc("Verify domtree after unrolling"),
58 #ifdef NDEBUG
59  cl::init(false)
60 #else
61  cl::init(true)
62 #endif
63  );
64 
65 /// Convert the instruction operands from referencing the current values into
66 /// those specified by VMap.
67 static inline void remapInstruction(Instruction *I,
68  ValueToValueMapTy &VMap) {
69  for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
70  Value *Op = I->getOperand(op);
71 
72  // Unwrap arguments of dbg.value intrinsics.
73  bool Wrapped = false;
74  if (auto *V = dyn_cast<MetadataAsValue>(Op))
75  if (auto *Unwrapped = dyn_cast<ValueAsMetadata>(V->getMetadata())) {
76  Op = Unwrapped->getValue();
77  Wrapped = true;
78  }
79 
80  auto wrap = [&](Value *V) {
81  auto &C = I->getContext();
82  return Wrapped ? MetadataAsValue::get(C, ValueAsMetadata::get(V)) : V;
83  };
84 
85  ValueToValueMapTy::iterator It = VMap.find(Op);
86  if (It != VMap.end())
87  I->setOperand(op, wrap(It->second));
88  }
89 
90  if (PHINode *PN = dyn_cast<PHINode>(I)) {
91  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
92  ValueToValueMapTy::iterator It = VMap.find(PN->getIncomingBlock(i));
93  if (It != VMap.end())
94  PN->setIncomingBlock(i, cast<BasicBlock>(It->second));
95  }
96  }
97 }
98 
99 /// Folds a basic block into its predecessor if it only has one predecessor, and
100 /// that predecessor only has one successor.
101 /// The LoopInfo Analysis that is passed will be kept consistent. If folding is
102 /// successful references to the containing loop must be removed from
103 /// ScalarEvolution by calling ScalarEvolution::forgetLoop because SE may have
104 /// references to the eliminated BB. The argument ForgottenLoops contains a set
105 /// of loops that have already been forgotten to prevent redundant, expensive
106 /// calls to ScalarEvolution::forgetLoop. Returns the new combined block.
107 static BasicBlock *
109  SmallPtrSetImpl<Loop *> &ForgottenLoops,
110  DominatorTree *DT) {
111  // Merge basic blocks into their predecessor if there is only one distinct
112  // pred, and if there is only one distinct successor of the predecessor, and
113  // if there are no PHI nodes.
114  BasicBlock *OnlyPred = BB->getSinglePredecessor();
115  if (!OnlyPred) return nullptr;
116 
117  if (OnlyPred->getTerminator()->getNumSuccessors() != 1)
118  return nullptr;
119 
120  DEBUG(dbgs() << "Merging: " << *BB << "into: " << *OnlyPred);
121 
122  // Resolve any PHI nodes at the start of the block. They are all
123  // guaranteed to have exactly one entry if they exist, unless there are
124  // multiple duplicate (but guaranteed to be equal) entries for the
125  // incoming edges. This occurs when there are multiple edges from
126  // OnlyPred to OnlySucc.
128 
129  // Delete the unconditional branch from the predecessor...
130  OnlyPred->getInstList().pop_back();
131 
132  // Make all PHI nodes that referred to BB now refer to Pred as their
133  // source...
134  BB->replaceAllUsesWith(OnlyPred);
135 
136  // Move all definitions in the successor to the predecessor...
137  OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
138 
139  // OldName will be valid until erased.
140  StringRef OldName = BB->getName();
141 
142  // Erase the old block and update dominator info.
143  if (DT)
144  if (DomTreeNode *DTN = DT->getNode(BB)) {
145  DomTreeNode *PredDTN = DT->getNode(OnlyPred);
146  SmallVector<DomTreeNode *, 8> Children(DTN->begin(), DTN->end());
147  for (auto *DI : Children)
148  DT->changeImmediateDominator(DI, PredDTN);
149 
150  DT->eraseNode(BB);
151  }
152 
153  // ScalarEvolution holds references to loop exit blocks.
154  if (SE) {
155  if (Loop *L = LI->getLoopFor(BB)) {
156  if (ForgottenLoops.insert(L).second)
157  SE->forgetLoop(L);
158  }
159  }
160  LI->removeBlock(BB);
161 
162  // Inherit predecessor's name if it exists...
163  if (!OldName.empty() && !OnlyPred->hasName())
164  OnlyPred->setName(OldName);
165 
166  BB->eraseFromParent();
167 
168  return OnlyPred;
169 }
170 
171 /// Check if unrolling created a situation where we need to insert phi nodes to
172 /// preserve LCSSA form.
173 /// \param Blocks is a vector of basic blocks representing unrolled loop.
174 /// \param L is the outer loop.
175 /// It's possible that some of the blocks are in L, and some are not. In this
176 /// case, if there is a use is outside L, and definition is inside L, we need to
177 /// insert a phi-node, otherwise LCSSA will be broken.
178 /// The function is just a helper function for llvm::UnrollLoop that returns
179 /// true if this situation occurs, indicating that LCSSA needs to be fixed.
180 static bool needToInsertPhisForLCSSA(Loop *L, std::vector<BasicBlock *> Blocks,
181  LoopInfo *LI) {
182  for (BasicBlock *BB : Blocks) {
183  if (LI->getLoopFor(BB) == L)
184  continue;
185  for (Instruction &I : *BB) {
186  for (Use &U : I.operands()) {
187  if (auto Def = dyn_cast<Instruction>(U)) {
188  Loop *DefLoop = LI->getLoopFor(Def->getParent());
189  if (!DefLoop)
190  continue;
191  if (DefLoop->contains(L))
192  return true;
193  }
194  }
195  }
196  }
197  return false;
198 }
199 
200 /// Adds ClonedBB to LoopInfo, creates a new loop for ClonedBB if necessary
201 /// and adds a mapping from the original loop to the new loop to NewLoops.
202 /// Returns nullptr if no new loop was created and a pointer to the
203 /// original loop OriginalBB was part of otherwise.
205  BasicBlock *ClonedBB, LoopInfo *LI,
206  NewLoopsMap &NewLoops) {
207  // Figure out which loop New is in.
208  const Loop *OldLoop = LI->getLoopFor(OriginalBB);
209  assert(OldLoop && "Should (at least) be in the loop being unrolled!");
210 
211  Loop *&NewLoop = NewLoops[OldLoop];
212  if (!NewLoop) {
213  // Found a new sub-loop.
214  assert(OriginalBB == OldLoop->getHeader() &&
215  "Header should be first in RPO");
216 
217  NewLoop = LI->AllocateLoop();
218  Loop *NewLoopParent = NewLoops.lookup(OldLoop->getParentLoop());
219 
220  if (NewLoopParent)
221  NewLoopParent->addChildLoop(NewLoop);
222  else
223  LI->addTopLevelLoop(NewLoop);
224 
225  NewLoop->addBasicBlockToLoop(ClonedBB, *LI);
226  return OldLoop;
227  } else {
228  NewLoop->addBasicBlockToLoop(ClonedBB, *LI);
229  return nullptr;
230  }
231 }
232 
233 /// The function chooses which type of unroll (epilog or prolog) is more
234 /// profitabale.
235 /// Epilog unroll is more profitable when there is PHI that starts from
236 /// constant. In this case epilog will leave PHI start from constant,
237 /// but prolog will convert it to non-constant.
238 ///
239 /// loop:
240 /// PN = PHI [I, Latch], [CI, PreHeader]
241 /// I = foo(PN)
242 /// ...
243 ///
244 /// Epilog unroll case.
245 /// loop:
246 /// PN = PHI [I2, Latch], [CI, PreHeader]
247 /// I1 = foo(PN)
248 /// I2 = foo(I1)
249 /// ...
250 /// Prolog unroll case.
251 /// NewPN = PHI [PrologI, Prolog], [CI, PreHeader]
252 /// loop:
253 /// PN = PHI [I2, Latch], [NewPN, PreHeader]
254 /// I1 = foo(PN)
255 /// I2 = foo(I1)
256 /// ...
257 ///
258 static bool isEpilogProfitable(Loop *L) {
259  BasicBlock *PreHeader = L->getLoopPreheader();
260  BasicBlock *Header = L->getHeader();
261  assert(PreHeader && Header);
262  for (Instruction &BBI : *Header) {
263  PHINode *PN = dyn_cast<PHINode>(&BBI);
264  if (!PN)
265  break;
266  if (isa<ConstantInt>(PN->getIncomingValueForBlock(PreHeader)))
267  return true;
268  }
269  return false;
270 }
271 
272 /// Unroll the given loop by Count. The loop must be in LCSSA form. Unrolling
273 /// can only fail when the loop's latch block is not terminated by a conditional
274 /// branch instruction. However, if the trip count (and multiple) are not known,
275 /// loop unrolling will mostly produce more code that is no faster.
276 ///
277 /// TripCount is the upper bound of the iteration on which control exits
278 /// LatchBlock. Control may exit the loop prior to TripCount iterations either
279 /// via an early branch in other loop block or via LatchBlock terminator. This
280 /// is relaxed from the general definition of trip count which is the number of
281 /// times the loop header executes. Note that UnrollLoop assumes that the loop
282 /// counter test is in LatchBlock in order to remove unnecesssary instances of
283 /// the test. If control can exit the loop from the LatchBlock's terminator
284 /// prior to TripCount iterations, flag PreserveCondBr needs to be set.
285 ///
286 /// PreserveCondBr indicates whether the conditional branch of the LatchBlock
287 /// needs to be preserved. It is needed when we use trip count upper bound to
288 /// fully unroll the loop. If PreserveOnlyFirst is also set then only the first
289 /// conditional branch needs to be preserved.
290 ///
291 /// Similarly, TripMultiple divides the number of times that the LatchBlock may
292 /// execute without exiting the loop.
293 ///
294 /// If AllowRuntime is true then UnrollLoop will consider unrolling loops that
295 /// have a runtime (i.e. not compile time constant) trip count. Unrolling these
296 /// loops require a unroll "prologue" that runs "RuntimeTripCount % Count"
297 /// iterations before branching into the unrolled loop. UnrollLoop will not
298 /// runtime-unroll the loop if computing RuntimeTripCount will be expensive and
299 /// AllowExpensiveTripCount is false.
300 ///
301 /// If we want to perform PGO-based loop peeling, PeelCount is set to the
302 /// number of iterations we want to peel off.
303 ///
304 /// The LoopInfo Analysis that is passed will be kept consistent.
305 ///
306 /// This utility preserves LoopInfo. It will also preserve ScalarEvolution and
307 /// DominatorTree if they are non-null.
309  Loop *L, unsigned Count, unsigned TripCount, bool Force, bool AllowRuntime,
310  bool AllowExpensiveTripCount, bool PreserveCondBr, bool PreserveOnlyFirst,
311  unsigned TripMultiple, unsigned PeelCount, bool UnrollRemainder,
313  OptimizationRemarkEmitter *ORE, bool PreserveLCSSA) {
314 
315  BasicBlock *Preheader = L->getLoopPreheader();
316  if (!Preheader) {
317  DEBUG(dbgs() << " Can't unroll; loop preheader-insertion failed.\n");
319  }
320 
321  BasicBlock *LatchBlock = L->getLoopLatch();
322  if (!LatchBlock) {
323  DEBUG(dbgs() << " Can't unroll; loop exit-block-insertion failed.\n");
325  }
326 
327  // Loops with indirectbr cannot be cloned.
328  if (!L->isSafeToClone()) {
329  DEBUG(dbgs() << " Can't unroll; Loop body cannot be cloned.\n");
331  }
332 
333  // The current loop unroll pass can only unroll loops with a single latch
334  // that's a conditional branch exiting the loop.
335  // FIXME: The implementation can be extended to work with more complicated
336  // cases, e.g. loops with multiple latches.
337  BasicBlock *Header = L->getHeader();
338  BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator());
339 
340  if (!BI || BI->isUnconditional()) {
341  // The loop-rotate pass can be helpful to avoid this in many cases.
342  DEBUG(dbgs() <<
343  " Can't unroll; loop not terminated by a conditional branch.\n");
345  }
346 
347  auto CheckSuccessors = [&](unsigned S1, unsigned S2) {
348  return BI->getSuccessor(S1) == Header && !L->contains(BI->getSuccessor(S2));
349  };
350 
351  if (!CheckSuccessors(0, 1) && !CheckSuccessors(1, 0)) {
352  DEBUG(dbgs() << "Can't unroll; only loops with one conditional latch"
353  " exiting the loop can be unrolled\n");
355  }
356 
357  if (Header->hasAddressTaken()) {
358  // The loop-rotate pass can be helpful to avoid this in many cases.
359  DEBUG(dbgs() <<
360  " Won't unroll loop: address of header block is taken.\n");
362  }
363 
364  if (TripCount != 0)
365  DEBUG(dbgs() << " Trip Count = " << TripCount << "\n");
366  if (TripMultiple != 1)
367  DEBUG(dbgs() << " Trip Multiple = " << TripMultiple << "\n");
368 
369  // Effectively "DCE" unrolled iterations that are beyond the tripcount
370  // and will never be executed.
371  if (TripCount != 0 && Count > TripCount)
372  Count = TripCount;
373 
374  // Don't enter the unroll code if there is nothing to do.
375  if (TripCount == 0 && Count < 2 && PeelCount == 0) {
376  DEBUG(dbgs() << "Won't unroll; almost nothing to do\n");
378  }
379 
380  assert(Count > 0);
381  assert(TripMultiple > 0);
382  assert(TripCount == 0 || TripCount % TripMultiple == 0);
383 
384  // Are we eliminating the loop control altogether?
385  bool CompletelyUnroll = Count == TripCount;
386  SmallVector<BasicBlock *, 4> ExitBlocks;
387  L->getExitBlocks(ExitBlocks);
388  std::vector<BasicBlock*> OriginalLoopBlocks = L->getBlocks();
389 
390  // Go through all exits of L and see if there are any phi-nodes there. We just
391  // conservatively assume that they're inserted to preserve LCSSA form, which
392  // means that complete unrolling might break this form. We need to either fix
393  // it in-place after the transformation, or entirely rebuild LCSSA. TODO: For
394  // now we just recompute LCSSA for the outer loop, but it should be possible
395  // to fix it in-place.
396  bool NeedToFixLCSSA = PreserveLCSSA && CompletelyUnroll &&
397  any_of(ExitBlocks, [](const BasicBlock *BB) {
398  return isa<PHINode>(BB->begin());
399  });
400 
401  // We assume a run-time trip count if the compiler cannot
402  // figure out the loop trip count and the unroll-runtime
403  // flag is specified.
404  bool RuntimeTripCount = (TripCount == 0 && Count > 0 && AllowRuntime);
405 
406  assert((!RuntimeTripCount || !PeelCount) &&
407  "Did not expect runtime trip-count unrolling "
408  "and peeling for the same loop");
409 
410  if (PeelCount) {
411  bool Peeled = peelLoop(L, PeelCount, LI, SE, DT, AC, PreserveLCSSA);
412 
413  // Successful peeling may result in a change in the loop preheader/trip
414  // counts. If we later unroll the loop, we want these to be updated.
415  if (Peeled) {
416  BasicBlock *ExitingBlock = L->getExitingBlock();
417  assert(ExitingBlock && "Loop without exiting block?");
418  Preheader = L->getLoopPreheader();
419  TripCount = SE->getSmallConstantTripCount(L, ExitingBlock);
420  TripMultiple = SE->getSmallConstantTripMultiple(L, ExitingBlock);
421  }
422  }
423 
424  // Loops containing convergent instructions must have a count that divides
425  // their TripMultiple.
426  DEBUG(
427  {
428  bool HasConvergent = false;
429  for (auto &BB : L->blocks())
430  for (auto &I : *BB)
431  if (auto CS = CallSite(&I))
432  HasConvergent |= CS.isConvergent();
433  assert((!HasConvergent || TripMultiple % Count == 0) &&
434  "Unroll count must divide trip multiple if loop contains a "
435  "convergent operation.");
436  });
437 
438  bool EpilogProfitability =
439  UnrollRuntimeEpilog.getNumOccurrences() ? UnrollRuntimeEpilog
440  : isEpilogProfitable(L);
441 
442  if (RuntimeTripCount && TripMultiple % Count != 0 &&
443  !UnrollRuntimeLoopRemainder(L, Count, AllowExpensiveTripCount,
444  EpilogProfitability, UnrollRemainder, LI, SE,
445  DT, AC, PreserveLCSSA)) {
446  if (Force)
447  RuntimeTripCount = false;
448  else {
449  DEBUG(
450  dbgs() << "Wont unroll; remainder loop could not be generated"
451  "when assuming runtime trip count\n");
453  }
454  }
455 
456  // Notify ScalarEvolution that the loop will be substantially changed,
457  // if not outright eliminated.
458  if (SE)
459  SE->forgetLoop(L);
460 
461  // If we know the trip count, we know the multiple...
462  unsigned BreakoutTrip = 0;
463  if (TripCount != 0) {
464  BreakoutTrip = TripCount % Count;
465  TripMultiple = 0;
466  } else {
467  // Figure out what multiple to use.
468  BreakoutTrip = TripMultiple =
469  (unsigned)GreatestCommonDivisor64(Count, TripMultiple);
470  }
471 
472  using namespace ore;
473  // Report the unrolling decision.
474  if (CompletelyUnroll) {
475  DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName()
476  << " with trip count " << TripCount << "!\n");
477  if (ORE)
478  ORE->emit([&]() {
479  return OptimizationRemark(DEBUG_TYPE, "FullyUnrolled", L->getStartLoc(),
480  L->getHeader())
481  << "completely unrolled loop with "
482  << NV("UnrollCount", TripCount) << " iterations";
483  });
484  } else if (PeelCount) {
485  DEBUG(dbgs() << "PEELING loop %" << Header->getName()
486  << " with iteration count " << PeelCount << "!\n");
487  if (ORE)
488  ORE->emit([&]() {
489  return OptimizationRemark(DEBUG_TYPE, "Peeled", L->getStartLoc(),
490  L->getHeader())
491  << " peeled loop by " << NV("PeelCount", PeelCount)
492  << " iterations";
493  });
494  } else {
495  auto DiagBuilder = [&]() {
496  OptimizationRemark Diag(DEBUG_TYPE, "PartialUnrolled", L->getStartLoc(),
497  L->getHeader());
498  return Diag << "unrolled loop by a factor of "
499  << NV("UnrollCount", Count);
500  };
501 
502  DEBUG(dbgs() << "UNROLLING loop %" << Header->getName()
503  << " by " << Count);
504  if (TripMultiple == 0 || BreakoutTrip != TripMultiple) {
505  DEBUG(dbgs() << " with a breakout at trip " << BreakoutTrip);
506  if (ORE)
507  ORE->emit([&]() {
508  return DiagBuilder() << " with a breakout at trip "
509  << NV("BreakoutTrip", BreakoutTrip);
510  });
511  } else if (TripMultiple != 1) {
512  DEBUG(dbgs() << " with " << TripMultiple << " trips per branch");
513  if (ORE)
514  ORE->emit([&]() {
515  return DiagBuilder() << " with " << NV("TripMultiple", TripMultiple)
516  << " trips per branch";
517  });
518  } else if (RuntimeTripCount) {
519  DEBUG(dbgs() << " with run-time trip count");
520  if (ORE)
521  ORE->emit(
522  [&]() { return DiagBuilder() << " with run-time trip count"; });
523  }
524  DEBUG(dbgs() << "!\n");
525  }
526 
527  bool ContinueOnTrue = L->contains(BI->getSuccessor(0));
528  BasicBlock *LoopExit = BI->getSuccessor(ContinueOnTrue);
529 
530  // For the first iteration of the loop, we should use the precloned values for
531  // PHI nodes. Insert associations now.
532  ValueToValueMapTy LastValueMap;
533  std::vector<PHINode*> OrigPHINode;
534  for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
535  OrigPHINode.push_back(cast<PHINode>(I));
536  }
537 
538  std::vector<BasicBlock*> Headers;
539  std::vector<BasicBlock*> Latches;
540  Headers.push_back(Header);
541  Latches.push_back(LatchBlock);
542 
543  // The current on-the-fly SSA update requires blocks to be processed in
544  // reverse postorder so that LastValueMap contains the correct value at each
545  // exit.
546  LoopBlocksDFS DFS(L);
547  DFS.perform(LI);
548 
549  // Stash the DFS iterators before adding blocks to the loop.
550  LoopBlocksDFS::RPOIterator BlockBegin = DFS.beginRPO();
551  LoopBlocksDFS::RPOIterator BlockEnd = DFS.endRPO();
552 
553  std::vector<BasicBlock*> UnrolledLoopBlocks = L->getBlocks();
554 
555  // Loop Unrolling might create new loops. While we do preserve LoopInfo, we
556  // might break loop-simplified form for these loops (as they, e.g., would
557  // share the same exit blocks). We'll keep track of loops for which we can
558  // break this so that later we can re-simplify them.
559  SmallSetVector<Loop *, 4> LoopsToSimplify;
560  for (Loop *SubLoop : *L)
561  LoopsToSimplify.insert(SubLoop);
562 
563  if (Header->getParent()->isDebugInfoForProfiling())
564  for (BasicBlock *BB : L->getBlocks())
565  for (Instruction &I : *BB)
566  if (!isa<DbgInfoIntrinsic>(&I))
567  if (const DILocation *DIL = I.getDebugLoc())
568  I.setDebugLoc(DIL->cloneWithDuplicationFactor(Count));
569 
570  for (unsigned It = 1; It != Count; ++It) {
571  std::vector<BasicBlock*> NewBlocks;
573  NewLoops[L] = L;
574 
575  for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
576  ValueToValueMapTy VMap;
577  BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It));
578  Header->getParent()->getBasicBlockList().push_back(New);
579 
580  assert((*BB != Header || LI->getLoopFor(*BB) == L) &&
581  "Header should not be in a sub-loop");
582  // Tell LI about New.
583  const Loop *OldLoop = addClonedBlockToLoopInfo(*BB, New, LI, NewLoops);
584  if (OldLoop) {
585  LoopsToSimplify.insert(NewLoops[OldLoop]);
586 
587  // Forget the old loop, since its inputs may have changed.
588  if (SE)
589  SE->forgetLoop(OldLoop);
590  }
591 
592  if (*BB == Header)
593  // Loop over all of the PHI nodes in the block, changing them to use
594  // the incoming values from the previous block.
595  for (PHINode *OrigPHI : OrigPHINode) {
596  PHINode *NewPHI = cast<PHINode>(VMap[OrigPHI]);
597  Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock);
598  if (Instruction *InValI = dyn_cast<Instruction>(InVal))
599  if (It > 1 && L->contains(InValI))
600  InVal = LastValueMap[InValI];
601  VMap[OrigPHI] = InVal;
602  New->getInstList().erase(NewPHI);
603  }
604 
605  // Update our running map of newest clones
606  LastValueMap[*BB] = New;
607  for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end();
608  VI != VE; ++VI)
609  LastValueMap[VI->first] = VI->second;
610 
611  // Add phi entries for newly created values to all exit blocks.
612  for (BasicBlock *Succ : successors(*BB)) {
613  if (L->contains(Succ))
614  continue;
615  for (BasicBlock::iterator BBI = Succ->begin();
616  PHINode *phi = dyn_cast<PHINode>(BBI); ++BBI) {
617  Value *Incoming = phi->getIncomingValueForBlock(*BB);
618  ValueToValueMapTy::iterator It = LastValueMap.find(Incoming);
619  if (It != LastValueMap.end())
620  Incoming = It->second;
621  phi->addIncoming(Incoming, New);
622  }
623  }
624  // Keep track of new headers and latches as we create them, so that
625  // we can insert the proper branches later.
626  if (*BB == Header)
627  Headers.push_back(New);
628  if (*BB == LatchBlock)
629  Latches.push_back(New);
630 
631  NewBlocks.push_back(New);
632  UnrolledLoopBlocks.push_back(New);
633 
634  // Update DomTree: since we just copy the loop body, and each copy has a
635  // dedicated entry block (copy of the header block), this header's copy
636  // dominates all copied blocks. That means, dominance relations in the
637  // copied body are the same as in the original body.
638  if (DT) {
639  if (*BB == Header)
640  DT->addNewBlock(New, Latches[It - 1]);
641  else {
642  auto BBDomNode = DT->getNode(*BB);
643  auto BBIDom = BBDomNode->getIDom();
644  BasicBlock *OriginalBBIDom = BBIDom->getBlock();
645  DT->addNewBlock(
646  New, cast<BasicBlock>(LastValueMap[cast<Value>(OriginalBBIDom)]));
647  }
648  }
649  }
650 
651  // Remap all instructions in the most recent iteration
652  for (BasicBlock *NewBlock : NewBlocks) {
653  for (Instruction &I : *NewBlock) {
654  ::remapInstruction(&I, LastValueMap);
655  if (auto *II = dyn_cast<IntrinsicInst>(&I))
656  if (II->getIntrinsicID() == Intrinsic::assume)
657  AC->registerAssumption(II);
658  }
659  }
660  }
661 
662  // Loop over the PHI nodes in the original block, setting incoming values.
663  for (PHINode *PN : OrigPHINode) {
664  if (CompletelyUnroll) {
665  PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader));
666  Header->getInstList().erase(PN);
667  }
668  else if (Count > 1) {
669  Value *InVal = PN->removeIncomingValue(LatchBlock, false);
670  // If this value was defined in the loop, take the value defined by the
671  // last iteration of the loop.
672  if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
673  if (L->contains(InValI))
674  InVal = LastValueMap[InVal];
675  }
676  assert(Latches.back() == LastValueMap[LatchBlock] && "bad last latch");
677  PN->addIncoming(InVal, Latches.back());
678  }
679  }
680 
681  // Now that all the basic blocks for the unrolled iterations are in place,
682  // set up the branches to connect them.
683  for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
684  // The original branch was replicated in each unrolled iteration.
685  BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
686 
687  // The branch destination.
688  unsigned j = (i + 1) % e;
689  BasicBlock *Dest = Headers[j];
690  bool NeedConditional = true;
691 
692  if (RuntimeTripCount && j != 0) {
693  NeedConditional = false;
694  }
695 
696  // For a complete unroll, make the last iteration end with a branch
697  // to the exit block.
698  if (CompletelyUnroll) {
699  if (j == 0)
700  Dest = LoopExit;
701  // If using trip count upper bound to completely unroll, we need to keep
702  // the conditional branch except the last one because the loop may exit
703  // after any iteration.
704  assert(NeedConditional &&
705  "NeedCondition cannot be modified by both complete "
706  "unrolling and runtime unrolling");
707  NeedConditional = (PreserveCondBr && j && !(PreserveOnlyFirst && i != 0));
708  } else if (j != BreakoutTrip && (TripMultiple == 0 || j % TripMultiple != 0)) {
709  // If we know the trip count or a multiple of it, we can safely use an
710  // unconditional branch for some iterations.
711  NeedConditional = false;
712  }
713 
714  if (NeedConditional) {
715  // Update the conditional branch's successor for the following
716  // iteration.
717  Term->setSuccessor(!ContinueOnTrue, Dest);
718  } else {
719  // Remove phi operands at this loop exit
720  if (Dest != LoopExit) {
721  BasicBlock *BB = Latches[i];
722  for (BasicBlock *Succ: successors(BB)) {
723  if (Succ == Headers[i])
724  continue;
725  for (BasicBlock::iterator BBI = Succ->begin();
726  PHINode *Phi = dyn_cast<PHINode>(BBI); ++BBI) {
727  Phi->removeIncomingValue(BB, false);
728  }
729  }
730  }
731  // Replace the conditional branch with an unconditional one.
732  BranchInst::Create(Dest, Term);
733  Term->eraseFromParent();
734  }
735  }
736 
737  // Update dominators of blocks we might reach through exits.
738  // Immediate dominator of such block might change, because we add more
739  // routes which can lead to the exit: we can now reach it from the copied
740  // iterations too.
741  if (DT && Count > 1) {
742  for (auto *BB : OriginalLoopBlocks) {
743  auto *BBDomNode = DT->getNode(BB);
744  SmallVector<BasicBlock *, 16> ChildrenToUpdate;
745  for (auto *ChildDomNode : BBDomNode->getChildren()) {
746  auto *ChildBB = ChildDomNode->getBlock();
747  if (!L->contains(ChildBB))
748  ChildrenToUpdate.push_back(ChildBB);
749  }
750  BasicBlock *NewIDom;
751  if (BB == LatchBlock) {
752  // The latch is special because we emit unconditional branches in
753  // some cases where the original loop contained a conditional branch.
754  // Since the latch is always at the bottom of the loop, if the latch
755  // dominated an exit before unrolling, the new dominator of that exit
756  // must also be a latch. Specifically, the dominator is the first
757  // latch which ends in a conditional branch, or the last latch if
758  // there is no such latch.
759  NewIDom = Latches.back();
760  for (BasicBlock *IterLatch : Latches) {
761  TerminatorInst *Term = IterLatch->getTerminator();
762  if (isa<BranchInst>(Term) && cast<BranchInst>(Term)->isConditional()) {
763  NewIDom = IterLatch;
764  break;
765  }
766  }
767  } else {
768  // The new idom of the block will be the nearest common dominator
769  // of all copies of the previous idom. This is equivalent to the
770  // nearest common dominator of the previous idom and the first latch,
771  // which dominates all copies of the previous idom.
772  NewIDom = DT->findNearestCommonDominator(BB, LatchBlock);
773  }
774  for (auto *ChildBB : ChildrenToUpdate)
775  DT->changeImmediateDominator(ChildBB, NewIDom);
776  }
777  }
778 
779  if (DT && UnrollVerifyDomtree)
780  DT->verifyDomTree();
781 
782  // Merge adjacent basic blocks, if possible.
783  SmallPtrSet<Loop *, 4> ForgottenLoops;
784  for (BasicBlock *Latch : Latches) {
785  BranchInst *Term = cast<BranchInst>(Latch->getTerminator());
786  if (Term->isUnconditional()) {
787  BasicBlock *Dest = Term->getSuccessor(0);
788  if (BasicBlock *Fold =
789  foldBlockIntoPredecessor(Dest, LI, SE, ForgottenLoops, DT)) {
790  // Dest has been folded into Fold. Update our worklists accordingly.
791  std::replace(Latches.begin(), Latches.end(), Dest, Fold);
792  UnrolledLoopBlocks.erase(std::remove(UnrolledLoopBlocks.begin(),
793  UnrolledLoopBlocks.end(), Dest),
794  UnrolledLoopBlocks.end());
795  }
796  }
797  }
798 
799  // Simplify any new induction variables in the partially unrolled loop.
800  if (SE && !CompletelyUnroll && Count > 1) {
802  simplifyLoopIVs(L, SE, DT, LI, DeadInsts);
803 
804  // Aggressively clean up dead instructions that simplifyLoopIVs already
805  // identified. Any remaining should be cleaned up below.
806  while (!DeadInsts.empty())
807  if (Instruction *Inst =
808  dyn_cast_or_null<Instruction>(&*DeadInsts.pop_back_val()))
810  }
811 
812  // At this point, the code is well formed. We now do a quick sweep over the
813  // inserted code, doing constant propagation and dead code elimination as we
814  // go.
815  const DataLayout &DL = Header->getModule()->getDataLayout();
816  const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks();
817  for (BasicBlock *BB : NewLoopBlocks) {
818  for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
819  Instruction *Inst = &*I++;
820 
821  if (Value *V = SimplifyInstruction(Inst, {DL, nullptr, DT, AC}))
822  if (LI->replacementPreservesLCSSAForm(Inst, V))
823  Inst->replaceAllUsesWith(V);
824  if (isInstructionTriviallyDead(Inst))
825  BB->getInstList().erase(Inst);
826  }
827  }
828 
829  // TODO: after peeling or unrolling, previously loop variant conditions are
830  // likely to fold to constants, eagerly propagating those here will require
831  // fewer cleanup passes to be run. Alternatively, a LoopEarlyCSE might be
832  // appropriate.
833 
834  NumCompletelyUnrolled += CompletelyUnroll;
835  ++NumUnrolled;
836 
837  Loop *OuterL = L->getParentLoop();
838  // Update LoopInfo if the loop is completely removed.
839  if (CompletelyUnroll)
840  LI->erase(L);
841 
842  // After complete unrolling most of the blocks should be contained in OuterL.
843  // However, some of them might happen to be out of OuterL (e.g. if they
844  // precede a loop exit). In this case we might need to insert PHI nodes in
845  // order to preserve LCSSA form.
846  // We don't need to check this if we already know that we need to fix LCSSA
847  // form.
848  // TODO: For now we just recompute LCSSA for the outer loop in this case, but
849  // it should be possible to fix it in-place.
850  if (PreserveLCSSA && OuterL && CompletelyUnroll && !NeedToFixLCSSA)
851  NeedToFixLCSSA |= ::needToInsertPhisForLCSSA(OuterL, UnrolledLoopBlocks, LI);
852 
853  // If we have a pass and a DominatorTree we should re-simplify impacted loops
854  // to ensure subsequent analyses can rely on this form. We want to simplify
855  // at least one layer outside of the loop that was unrolled so that any
856  // changes to the parent loop exposed by the unrolling are considered.
857  if (DT) {
858  if (OuterL) {
859  // OuterL includes all loops for which we can break loop-simplify, so
860  // it's sufficient to simplify only it (it'll recursively simplify inner
861  // loops too).
862  if (NeedToFixLCSSA) {
863  // LCSSA must be performed on the outermost affected loop. The unrolled
864  // loop's last loop latch is guaranteed to be in the outermost loop
865  // after LoopInfo's been updated by LoopInfo::erase.
866  Loop *LatchLoop = LI->getLoopFor(Latches.back());
867  Loop *FixLCSSALoop = OuterL;
868  if (!FixLCSSALoop->contains(LatchLoop))
869  while (FixLCSSALoop->getParentLoop() != LatchLoop)
870  FixLCSSALoop = FixLCSSALoop->getParentLoop();
871 
872  formLCSSARecursively(*FixLCSSALoop, *DT, LI, SE);
873  } else if (PreserveLCSSA) {
874  assert(OuterL->isLCSSAForm(*DT) &&
875  "Loops should be in LCSSA form after loop-unroll.");
876  }
877 
878  // TODO: That potentially might be compile-time expensive. We should try
879  // to fix the loop-simplified form incrementally.
880  simplifyLoop(OuterL, DT, LI, SE, AC, PreserveLCSSA);
881  } else {
882  // Simplify loops for which we might've broken loop-simplify form.
883  for (Loop *SubLoop : LoopsToSimplify)
884  simplifyLoop(SubLoop, DT, LI, SE, AC, PreserveLCSSA);
885  }
886  }
887 
888  return CompletelyUnroll ? LoopUnrollResult::FullyUnrolled
890 }
891 
892 /// Given an llvm.loop loop id metadata node, returns the loop hint metadata
893 /// node with the given name (for example, "llvm.loop.unroll.count"). If no
894 /// such metadata node exists, then nullptr is returned.
896  // First operand should refer to the loop id itself.
897  assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
898  assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
899 
900  for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
901  MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
902  if (!MD)
903  continue;
904 
905  MDString *S = dyn_cast<MDString>(MD->getOperand(0));
906  if (!S)
907  continue;
908 
909  if (Name.equals(S->getString()))
910  return MD;
911  }
912  return nullptr;
913 }
uint64_t CallInst * C
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
unsigned getSmallConstantTripCount(const Loop *L)
Returns the maximum trip count of the loop if it is a single-exit loop and we can compute a small max...
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:69
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
Definition: LoopInfoImpl.h:157
unsigned getSmallConstantTripMultiple(const Loop *L)
Returns the largest constant divisor of the trip count of the loop if it is a single-exit loop and we...
DiagnosticInfoOptimizationBase::Argument NV
uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B)
Return the greatest common divisor of the values using Euclid&#39;s algorithm.
Definition: MathExtras.h:555
iterator erase(iterator where)
Definition: ilist.h:280
static void remapInstruction(Instruction *I, ValueToValueMapTy &VMap)
Convert the instruction operands from referencing the current values into those specified by VMap...
Definition: LoopUnroll.cpp:67
static bool needToInsertPhisForLCSSA(Loop *L, std::vector< BasicBlock *> Blocks, LoopInfo *LI)
Check if unrolling created a situation where we need to insert phi nodes to preserve LCSSA form...
Definition: LoopUnroll.cpp:180
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
bool isLCSSAForm(DominatorTree &DT) const
Return true if the Loop is in LCSSA form.
Definition: LoopInfo.cpp:175
The main scalar evolution driver.
static cl::opt< bool > UnrollVerifyDomtree("unroll-verify-domtree", cl::Hidden, cl::desc("Verify domtree after unrolling"), cl::init(true))
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:106
A cache of .assume calls within a function.
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:728
NodeT * findNearestCommonDominator(NodeT *A, NodeT *B) const
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B...
BasicBlock * getSuccessor(unsigned i) const
STATISTIC(NumFunctions, "Total number of functions")
Metadata node.
Definition: Metadata.h:862
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1067
#define op(i)
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:344
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:252
static cl::opt< bool > UnrollRuntimeEpilog("unroll-runtime-epilog", cl::init(false), cl::Hidden, cl::desc("Allow runtime unrolled loops to be unrolled " "with epilog instead of prolog."))
bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
Definition: LCSSA.cpp:332
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
Definition: BasicBlock.cpp:116
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:659
bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, bool PreserveLCSSA)
Simplify each loop in a loop nest recursively.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
void erase(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
Definition: LoopInfo.cpp:654
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:286
RPOIterator endRPO() const
Definition: LoopIterator.h:141
BlockT * getHeader() const
Definition: LoopInfo.h:100
void getExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all of the successor blocks of this loop.
Definition: LoopInfoImpl.h:63
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:142
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
Definition: LoopInfoImpl.h:183
bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT, LoopInfo *LI, SmallVectorImpl< WeakTrackingVH > &Dead)
SimplifyLoopIVs - Simplify users of induction variables within this loop.
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
Definition: LoopInfo.h:710
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
iterator find(const KeyT &Val)
Definition: ValueMap.h:158
static bool isEpilogProfitable(Loop *L)
The function chooses which type of unroll (epilog or prolog) is more profitabale. ...
Definition: LoopUnroll.cpp:258
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:430
Debug location.
void perform(LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
Definition: LoopInfo.cpp:795
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:140
Value * getOperand(unsigned i) const
Definition: User.h:154
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:106
StringRef getString() const
Definition: Metadata.cpp:456
The loop was fully unrolled into straight-line code.
bool isDebugInfoForProfiling() const
Returns true if we should emit debug info for profiling.
Definition: Metadata.cpp:1501
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
void verifyDomTree() const
Verify the correctness of the domtree by re-computing it.
Definition: Dominators.cpp:305
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:54
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:217
bool hasName() const
Definition: Value.h:251
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
Conditional or Unconditional Branch instruction.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node&#39;s...
DomTreeNodeBase * getIDom() const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Value * getIncomingValueForBlock(const BasicBlock *BB) const
bool peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC, bool PreserveLCSSA)
Peel off the first PeelCount iterations of loop L.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:371
const std::vector< BlockT * > & getBlocks() const
Get a list of the basic blocks which make up this loop.
Definition: LoopInfo.h:149
Diagnostic information for applied optimization remarks.
void splice(iterator where, iplist_impl &L2)
Definition: ilist.h:342
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:774
const Instruction & back() const
Definition: BasicBlock.h:266
std::vector< BasicBlock * >::const_reverse_iterator RPOIterator
Definition: LoopIterator.h:102
static BasicBlock * foldBlockIntoPredecessor(BasicBlock *BB, LoopInfo *LI, ScalarEvolution *SE, SmallPtrSetImpl< Loop *> &ForgottenLoops, DominatorTree *DT)
Folds a basic block into its predecessor if it only has one predecessor, and that predecessor only ha...
Definition: LoopUnroll.cpp:108
The loop was partially unrolled – we still have a loop, but with a smaller trip count.
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr)
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:401
bool UnrollRuntimeLoopRemainder(Loop *L, unsigned Count, bool AllowExpensiveTripCount, bool UseEpilogRemainder, bool UnrollRemainder, LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC, bool PreserveLCSSA)
Insert code in the prolog/epilog code when unrolling a loop with a run-time trip-count.
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
Definition: LoopInfo.cpp:345
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
iterator end()
Definition: ValueMap.h:138
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc.
Definition: BasicBlock.h:376
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:317
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:110
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:298
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
Definition: User.h:176
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file. ...
iterator end()
Definition: BasicBlock.h:254
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:341
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
LoopT * AllocateLoop(ArgsTy &&... Args)
Definition: LoopInfo.h:623
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:385
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
void setOperand(unsigned i, Value *Val)
Definition: User.h:159
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
const Loop * addClonedBlockToLoopInfo(BasicBlock *OriginalBB, BasicBlock *ClonedBB, LoopInfo *LI, NewLoopsMap &NewLoops)
Adds ClonedBB to LoopInfo, creates a new loop for ClonedBB if necessary and adds a mapping from the o...
Definition: LoopUnroll.cpp:204
Store the result of a depth first search within basic blocks contained by a single loop...
Definition: LoopIterator.h:98
void push_back(pointer val)
Definition: ilist.h:326
BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr, DebugInfoFinder *DIFinder=nullptr)
CloneBasicBlock - Return a copy of the specified basic block, but without embedding the block into a ...
static void DFS(BasicBlock *Root, SetVector< BasicBlock *> &Set)
#define NDEBUG
Definition: regutils.h:48
void FoldSingleEntryPHINodes(BasicBlock *BB, MemoryDependenceResults *MemDep=nullptr)
We know that BB has one predecessor.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:169
MDNode * GetUnrollMetadata(MDNode *LoopID, StringRef Name)
Given an llvm.loop loop id metadata node, returns the loop hint metadata node with the given name (fo...
Definition: LoopUnroll.cpp:895
LoopT * getParentLoop() const
Definition: LoopInfo.h:101
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:190
void registerAssumption(CallInst *CI)
Add an .assume intrinsic to this function&#39;s cache.
void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
Definition: LoopInfo.h:298
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:61
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:420
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:220
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink &#39;this&#39; from the containing function and delete it.
Definition: BasicBlock.cpp:97
#define I(x, y, z)
Definition: MD5.cpp:58
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
const BasicBlockListType & getBasicBlockList() const
Get the underlying elements of the Function...
Definition: Function.h:565
bool isSafeToClone() const
Return true if the loop body is safe to clone in practice.
Definition: LoopInfo.cpp:198
bool isUnconditional() const
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:181
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction has no side ef...
Definition: Local.cpp:324
LLVM Value Representation.
Definition: Value.h:73
succ_range successors(BasicBlock *BB)
Definition: CFG.h:143
RPOIterator beginRPO() const
Reverse iterate over the cached postorder blocks.
Definition: LoopIterator.h:137
The loop was not modified.
#define DEBUG_TYPE
Definition: LoopUnroll.cpp:44
#define DEBUG(X)
Definition: Debug.h:118
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static Expected< std::string > replace(StringRef S, StringRef From, StringRef To)
A single uniqued string.
Definition: Metadata.h:602
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form...
Definition: LoopInfo.h:794
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:120
void pop_back()
Definition: ilist.h:331
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1073
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:156
Value * SimplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
LoopUnrollResult UnrollLoop(Loop *L, unsigned Count, unsigned TripCount, bool Force, bool AllowRuntime, bool AllowExpensiveTripCount, bool PreserveCondBr, bool PreserveOnlyFirst, unsigned TripMultiple, unsigned PeelCount, bool UnrollRemainder, LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC, OptimizationRemarkEmitter *ORE, bool PreserveLCSSA)
Unroll the given loop by Count.
Definition: LoopUnroll.cpp:308
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:50
The optimization diagnostic interface.
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
Definition: LoopInfo.h:718
iterator begin()
Definition: ValueMap.h:137
LoopUnrollResult
Represents the result of a UnrollLoop invocation.
Definition: UnrollLoop.h:41