LLVM  10.0.0
LoopRotationUtils.cpp
Go to the documentation of this file.
1 //===----------------- LoopRotationUtils.cpp -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides utilities to convert a loop into a loop with bottom test.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/LoopPass.h"
29 #include "llvm/IR/CFG.h"
31 #include "llvm/IR/Dominators.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/IR/Module.h"
36 #include "llvm/Support/Debug.h"
43 using namespace llvm;
44 
45 #define DEBUG_TYPE "loop-rotate"
46 
47 STATISTIC(NumRotated, "Number of loops rotated");
48 
49 namespace {
50 /// A simple loop rotation transformation.
51 class LoopRotate {
52  const unsigned MaxHeaderSize;
53  LoopInfo *LI;
54  const TargetTransformInfo *TTI;
55  AssumptionCache *AC;
56  DominatorTree *DT;
57  ScalarEvolution *SE;
58  MemorySSAUpdater *MSSAU;
59  const SimplifyQuery &SQ;
60  bool RotationOnly;
61  bool IsUtilMode;
62 
63 public:
64  LoopRotate(unsigned MaxHeaderSize, LoopInfo *LI,
65  const TargetTransformInfo *TTI, AssumptionCache *AC,
67  const SimplifyQuery &SQ, bool RotationOnly, bool IsUtilMode)
68  : MaxHeaderSize(MaxHeaderSize), LI(LI), TTI(TTI), AC(AC), DT(DT), SE(SE),
69  MSSAU(MSSAU), SQ(SQ), RotationOnly(RotationOnly),
70  IsUtilMode(IsUtilMode) {}
71  bool processLoop(Loop *L);
72 
73 private:
74  bool rotateLoop(Loop *L, bool SimplifiedLatch);
75  bool simplifyLoopLatch(Loop *L);
76 };
77 } // end anonymous namespace
78 
79 /// Insert (K, V) pair into the ValueToValueMap, and verify the key did not
80 /// previously exist in the map, and the value was inserted.
82  bool Inserted = VM.insert({K, V}).second;
83  assert(Inserted);
84  (void)Inserted;
85 }
86 /// RewriteUsesOfClonedInstructions - We just cloned the instructions from the
87 /// old header into the preheader. If there were uses of the values produced by
88 /// these instruction that were outside of the loop, we have to insert PHI nodes
89 /// to merge the two values. Do this now.
91  BasicBlock *OrigPreheader,
93  SmallVectorImpl<PHINode*> *InsertedPHIs) {
94  // Remove PHI node entries that are no longer live.
95  BasicBlock::iterator I, E = OrigHeader->end();
96  for (I = OrigHeader->begin(); PHINode *PN = dyn_cast<PHINode>(I); ++I)
97  PN->removeIncomingValue(PN->getBasicBlockIndex(OrigPreheader));
98 
99  // Now fix up users of the instructions in OrigHeader, inserting PHI nodes
100  // as necessary.
101  SSAUpdater SSA(InsertedPHIs);
102  for (I = OrigHeader->begin(); I != E; ++I) {
103  Value *OrigHeaderVal = &*I;
104 
105  // If there are no uses of the value (e.g. because it returns void), there
106  // is nothing to rewrite.
107  if (OrigHeaderVal->use_empty())
108  continue;
109 
110  Value *OrigPreHeaderVal = ValueMap.lookup(OrigHeaderVal);
111 
112  // The value now exits in two versions: the initial value in the preheader
113  // and the loop "next" value in the original header.
114  SSA.Initialize(OrigHeaderVal->getType(), OrigHeaderVal->getName());
115  SSA.AddAvailableValue(OrigHeader, OrigHeaderVal);
116  SSA.AddAvailableValue(OrigPreheader, OrigPreHeaderVal);
117 
118  // Visit each use of the OrigHeader instruction.
119  for (Value::use_iterator UI = OrigHeaderVal->use_begin(),
120  UE = OrigHeaderVal->use_end();
121  UI != UE;) {
122  // Grab the use before incrementing the iterator.
123  Use &U = *UI;
124 
125  // Increment the iterator before removing the use from the list.
126  ++UI;
127 
128  // SSAUpdater can't handle a non-PHI use in the same block as an
129  // earlier def. We can easily handle those cases manually.
130  Instruction *UserInst = cast<Instruction>(U.getUser());
131  if (!isa<PHINode>(UserInst)) {
132  BasicBlock *UserBB = UserInst->getParent();
133 
134  // The original users in the OrigHeader are already using the
135  // original definitions.
136  if (UserBB == OrigHeader)
137  continue;
138 
139  // Users in the OrigPreHeader need to use the value to which the
140  // original definitions are mapped.
141  if (UserBB == OrigPreheader) {
142  U = OrigPreHeaderVal;
143  continue;
144  }
145  }
146 
147  // Anything else can be handled by SSAUpdater.
148  SSA.RewriteUse(U);
149  }
150 
151  // Replace MetadataAsValue(ValueAsMetadata(OrigHeaderVal)) uses in debug
152  // intrinsics.
154  llvm::findDbgValues(DbgValues, OrigHeaderVal);
155  for (auto &DbgValue : DbgValues) {
156  // The original users in the OrigHeader are already using the original
157  // definitions.
158  BasicBlock *UserBB = DbgValue->getParent();
159  if (UserBB == OrigHeader)
160  continue;
161 
162  // Users in the OrigPreHeader need to use the value to which the
163  // original definitions are mapped and anything else can be handled by
164  // the SSAUpdater. To avoid adding PHINodes, check if the value is
165  // available in UserBB, if not substitute undef.
166  Value *NewVal;
167  if (UserBB == OrigPreheader)
168  NewVal = OrigPreHeaderVal;
169  else if (SSA.HasValueForBlock(UserBB))
170  NewVal = SSA.GetValueInMiddleOfBlock(UserBB);
171  else
172  NewVal = UndefValue::get(OrigHeaderVal->getType());
173  DbgValue->setOperand(0,
174  MetadataAsValue::get(OrigHeaderVal->getContext(),
175  ValueAsMetadata::get(NewVal)));
176  }
177  }
178 }
179 
180 // Look for a phi which is only used outside the loop (via a LCSSA phi)
181 // in the exit from the header. This means that rotating the loop can
182 // remove the phi.
184  BasicBlock *Header = L->getHeader();
185  BasicBlock *HeaderExit = Header->getTerminator()->getSuccessor(0);
186  if (L->contains(HeaderExit))
187  HeaderExit = Header->getTerminator()->getSuccessor(1);
188 
189  for (auto &Phi : Header->phis()) {
190  // Look for uses of this phi in the loop/via exits other than the header.
191  if (llvm::any_of(Phi.users(), [HeaderExit](const User *U) {
192  return cast<Instruction>(U)->getParent() != HeaderExit;
193  }))
194  continue;
195  return true;
196  }
197 
198  return false;
199 }
200 
201 /// Rotate loop LP. Return true if the loop is rotated.
202 ///
203 /// \param SimplifiedLatch is true if the latch was just folded into the final
204 /// loop exit. In this case we may want to rotate even though the new latch is
205 /// now an exiting branch. This rotation would have happened had the latch not
206 /// been simplified. However, if SimplifiedLatch is false, then we avoid
207 /// rotating loops in which the latch exits to avoid excessive or endless
208 /// rotation. LoopRotate should be repeatable and converge to a canonical
209 /// form. This property is satisfied because simplifying the loop latch can only
210 /// happen once across multiple invocations of the LoopRotate pass.
211 bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) {
212  // If the loop has only one block then there is not much to rotate.
213  if (L->getBlocks().size() == 1)
214  return false;
215 
216  BasicBlock *OrigHeader = L->getHeader();
217  BasicBlock *OrigLatch = L->getLoopLatch();
218 
219  BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
220  if (!BI || BI->isUnconditional())
221  return false;
222 
223  // If the loop header is not one of the loop exiting blocks then
224  // either this loop is already rotated or it is not
225  // suitable for loop rotation transformations.
226  if (!L->isLoopExiting(OrigHeader))
227  return false;
228 
229  // If the loop latch already contains a branch that leaves the loop then the
230  // loop is already rotated.
231  if (!OrigLatch)
232  return false;
233 
234  // Rotate if either the loop latch does *not* exit the loop, or if the loop
235  // latch was just simplified. Or if we think it will be profitable.
236  if (L->isLoopExiting(OrigLatch) && !SimplifiedLatch && IsUtilMode == false &&
238  return false;
239 
240  // Check size of original header and reject loop if it is very big or we can't
241  // duplicate blocks inside it.
242  {
244  CodeMetrics::collectEphemeralValues(L, AC, EphValues);
245 
247  Metrics.analyzeBasicBlock(OrigHeader, *TTI, EphValues);
248  if (Metrics.notDuplicatable) {
249  LLVM_DEBUG(
250  dbgs() << "LoopRotation: NOT rotating - contains non-duplicatable"
251  << " instructions: ";
252  L->dump());
253  return false;
254  }
255  if (Metrics.convergent) {
256  LLVM_DEBUG(dbgs() << "LoopRotation: NOT rotating - contains convergent "
257  "instructions: ";
258  L->dump());
259  return false;
260  }
261  if (Metrics.NumInsts > MaxHeaderSize)
262  return false;
263  }
264 
265  // Now, this loop is suitable for rotation.
266  BasicBlock *OrigPreheader = L->getLoopPreheader();
267 
268  // If the loop could not be converted to canonical form, it must have an
269  // indirectbr in it, just give up.
270  if (!OrigPreheader || !L->hasDedicatedExits())
271  return false;
272 
273  // Anything ScalarEvolution may know about this loop or the PHI nodes
274  // in its header will soon be invalidated. We should also invalidate
275  // all outer loops because insertion and deletion of blocks that happens
276  // during the rotation may violate invariants related to backedge taken
277  // infos in them.
278  if (SE)
279  SE->forgetTopmostLoop(L);
280 
281  LLVM_DEBUG(dbgs() << "LoopRotation: rotating "; L->dump());
282  if (MSSAU && VerifyMemorySSA)
283  MSSAU->getMemorySSA()->verifyMemorySSA();
284 
285  // Find new Loop header. NewHeader is a Header's one and only successor
286  // that is inside loop. Header's other successor is outside the
287  // loop. Otherwise loop is not suitable for rotation.
288  BasicBlock *Exit = BI->getSuccessor(0);
289  BasicBlock *NewHeader = BI->getSuccessor(1);
290  if (L->contains(Exit))
291  std::swap(Exit, NewHeader);
292  assert(NewHeader && "Unable to determine new loop header");
293  assert(L->contains(NewHeader) && !L->contains(Exit) &&
294  "Unable to determine loop header and exit blocks");
295 
296  // This code assumes that the new header has exactly one predecessor.
297  // Remove any single-entry PHI nodes in it.
298  assert(NewHeader->getSinglePredecessor() &&
299  "New header doesn't have one pred!");
300  FoldSingleEntryPHINodes(NewHeader);
301 
302  // Begin by walking OrigHeader and populating ValueMap with an entry for
303  // each Instruction.
304  BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
305  ValueToValueMapTy ValueMap, ValueMapMSSA;
306 
307  // For PHI nodes, the value available in OldPreHeader is just the
308  // incoming value from OldPreHeader.
309  for (; PHINode *PN = dyn_cast<PHINode>(I); ++I)
310  InsertNewValueIntoMap(ValueMap, PN,
311  PN->getIncomingValueForBlock(OrigPreheader));
312 
313  // For the rest of the instructions, either hoist to the OrigPreheader if
314  // possible or create a clone in the OldPreHeader if not.
315  Instruction *LoopEntryBranch = OrigPreheader->getTerminator();
316 
317  // Record all debug intrinsics preceding LoopEntryBranch to avoid duplication.
318  using DbgIntrinsicHash =
319  std::pair<std::pair<Value *, DILocalVariable *>, DIExpression *>;
320  auto makeHash = [](DbgVariableIntrinsic *D) -> DbgIntrinsicHash {
321  return {{D->getVariableLocation(), D->getVariable()}, D->getExpression()};
322  };
324  for (auto I = std::next(OrigPreheader->rbegin()), E = OrigPreheader->rend();
325  I != E; ++I) {
326  if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&*I))
327  DbgIntrinsics.insert(makeHash(DII));
328  else
329  break;
330  }
331 
332  while (I != E) {
333  Instruction *Inst = &*I++;
334 
335  // If the instruction's operands are invariant and it doesn't read or write
336  // memory, then it is safe to hoist. Doing this doesn't change the order of
337  // execution in the preheader, but does prevent the instruction from
338  // executing in each iteration of the loop. This means it is safe to hoist
339  // something that might trap, but isn't safe to hoist something that reads
340  // memory (without proving that the loop doesn't write).
341  if (L->hasLoopInvariantOperands(Inst) && !Inst->mayReadFromMemory() &&
342  !Inst->mayWriteToMemory() && !Inst->isTerminator() &&
343  !isa<DbgInfoIntrinsic>(Inst) && !isa<AllocaInst>(Inst)) {
344  Inst->moveBefore(LoopEntryBranch);
345  continue;
346  }
347 
348  // Otherwise, create a duplicate of the instruction.
349  Instruction *C = Inst->clone();
350 
351  // Eagerly remap the operands of the instruction.
352  RemapInstruction(C, ValueMap,
354 
355  // Avoid inserting the same intrinsic twice.
356  if (auto *DII = dyn_cast<DbgVariableIntrinsic>(C))
357  if (DbgIntrinsics.count(makeHash(DII))) {
358  C->deleteValue();
359  continue;
360  }
361 
362  // With the operands remapped, see if the instruction constant folds or is
363  // otherwise simplifyable. This commonly occurs because the entry from PHI
364  // nodes allows icmps and other instructions to fold.
365  Value *V = SimplifyInstruction(C, SQ);
366  if (V && LI->replacementPreservesLCSSAForm(C, V)) {
367  // If so, then delete the temporary instruction and stick the folded value
368  // in the map.
369  InsertNewValueIntoMap(ValueMap, Inst, V);
370  if (!C->mayHaveSideEffects()) {
371  C->deleteValue();
372  C = nullptr;
373  }
374  } else {
375  InsertNewValueIntoMap(ValueMap, Inst, C);
376  }
377  if (C) {
378  // Otherwise, stick the new instruction into the new block!
379  C->setName(Inst->getName());
380  C->insertBefore(LoopEntryBranch);
381 
382  if (auto *II = dyn_cast<IntrinsicInst>(C))
383  if (II->getIntrinsicID() == Intrinsic::assume)
384  AC->registerAssumption(II);
385  // MemorySSA cares whether the cloned instruction was inserted or not, and
386  // not whether it can be remapped to a simplified value.
387  if (MSSAU)
388  InsertNewValueIntoMap(ValueMapMSSA, Inst, C);
389  }
390  }
391 
392  // Along with all the other instructions, we just cloned OrigHeader's
393  // terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's
394  // successors by duplicating their incoming values for OrigHeader.
395  for (BasicBlock *SuccBB : successors(OrigHeader))
396  for (BasicBlock::iterator BI = SuccBB->begin();
397  PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
398  PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreheader);
399 
400  // Now that OrigPreHeader has a clone of OrigHeader's terminator, remove
401  // OrigPreHeader's old terminator (the original branch into the loop), and
402  // remove the corresponding incoming values from the PHI nodes in OrigHeader.
403  LoopEntryBranch->eraseFromParent();
404 
405  // Update MemorySSA before the rewrite call below changes the 1:1
406  // instruction:cloned_instruction_or_value mapping.
407  if (MSSAU) {
408  InsertNewValueIntoMap(ValueMapMSSA, OrigHeader, OrigPreheader);
409  MSSAU->updateForClonedBlockIntoPred(OrigHeader, OrigPreheader,
410  ValueMapMSSA);
411  }
412 
413  SmallVector<PHINode*, 2> InsertedPHIs;
414  // If there were any uses of instructions in the duplicated block outside the
415  // loop, update them, inserting PHI nodes as required
416  RewriteUsesOfClonedInstructions(OrigHeader, OrigPreheader, ValueMap,
417  &InsertedPHIs);
418 
419  // Attach dbg.value intrinsics to the new phis if that phi uses a value that
420  // previously had debug metadata attached. This keeps the debug info
421  // up-to-date in the loop body.
422  if (!InsertedPHIs.empty())
423  insertDebugValuesForPHIs(OrigHeader, InsertedPHIs);
424 
425  // NewHeader is now the header of the loop.
426  L->moveToHeader(NewHeader);
427  assert(L->getHeader() == NewHeader && "Latch block is our new header");
428 
429  // Inform DT about changes to the CFG.
430  if (DT) {
431  // The OrigPreheader branches to the NewHeader and Exit now. Then, inform
432  // the DT about the removed edge to the OrigHeader (that got removed).
434  Updates.push_back({DominatorTree::Insert, OrigPreheader, Exit});
435  Updates.push_back({DominatorTree::Insert, OrigPreheader, NewHeader});
436  Updates.push_back({DominatorTree::Delete, OrigPreheader, OrigHeader});
437  DT->applyUpdates(Updates);
438 
439  if (MSSAU) {
440  MSSAU->applyUpdates(Updates, *DT);
441  if (VerifyMemorySSA)
442  MSSAU->getMemorySSA()->verifyMemorySSA();
443  }
444  }
445 
446  // At this point, we've finished our major CFG changes. As part of cloning
447  // the loop into the preheader we've simplified instructions and the
448  // duplicated conditional branch may now be branching on a constant. If it is
449  // branching on a constant and if that constant means that we enter the loop,
450  // then we fold away the cond branch to an uncond branch. This simplifies the
451  // loop in cases important for nested loops, and it also means we don't have
452  // to split as many edges.
453  BranchInst *PHBI = cast<BranchInst>(OrigPreheader->getTerminator());
454  assert(PHBI->isConditional() && "Should be clone of BI condbr!");
455  if (!isa<ConstantInt>(PHBI->getCondition()) ||
456  PHBI->getSuccessor(cast<ConstantInt>(PHBI->getCondition())->isZero()) !=
457  NewHeader) {
458  // The conditional branch can't be folded, handle the general case.
459  // Split edges as necessary to preserve LoopSimplify form.
460 
461  // Right now OrigPreHeader has two successors, NewHeader and ExitBlock, and
462  // thus is not a preheader anymore.
463  // Split the edge to form a real preheader.
464  BasicBlock *NewPH = SplitCriticalEdge(
465  OrigPreheader, NewHeader,
466  CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA());
467  NewPH->setName(NewHeader->getName() + ".lr.ph");
468 
469  // Preserve canonical loop form, which means that 'Exit' should have only
470  // one predecessor. Note that Exit could be an exit block for multiple
471  // nested loops, causing both of the edges to now be critical and need to
472  // be split.
473  SmallVector<BasicBlock *, 4> ExitPreds(pred_begin(Exit), pred_end(Exit));
474  bool SplitLatchEdge = false;
475  for (BasicBlock *ExitPred : ExitPreds) {
476  // We only need to split loop exit edges.
477  Loop *PredLoop = LI->getLoopFor(ExitPred);
478  if (!PredLoop || PredLoop->contains(Exit) ||
479  ExitPred->getTerminator()->isIndirectTerminator())
480  continue;
481  SplitLatchEdge |= L->getLoopLatch() == ExitPred;
482  BasicBlock *ExitSplit = SplitCriticalEdge(
483  ExitPred, Exit,
484  CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA());
485  ExitSplit->moveBefore(Exit);
486  }
487  assert(SplitLatchEdge &&
488  "Despite splitting all preds, failed to split latch exit?");
489  } else {
490  // We can fold the conditional branch in the preheader, this makes things
491  // simpler. The first step is to remove the extra edge to the Exit block.
492  Exit->removePredecessor(OrigPreheader, true /*preserve LCSSA*/);
493  BranchInst *NewBI = BranchInst::Create(NewHeader, PHBI);
494  NewBI->setDebugLoc(PHBI->getDebugLoc());
495  PHBI->eraseFromParent();
496 
497  // With our CFG finalized, update DomTree if it is available.
498  if (DT) DT->deleteEdge(OrigPreheader, Exit);
499 
500  // Update MSSA too, if available.
501  if (MSSAU)
502  MSSAU->removeEdge(OrigPreheader, Exit);
503  }
504 
505  assert(L->getLoopPreheader() && "Invalid loop preheader after loop rotation");
506  assert(L->getLoopLatch() && "Invalid loop latch after loop rotation");
507 
508  if (MSSAU && VerifyMemorySSA)
509  MSSAU->getMemorySSA()->verifyMemorySSA();
510 
511  // Now that the CFG and DomTree are in a consistent state again, try to merge
512  // the OrigHeader block into OrigLatch. This will succeed if they are
513  // connected by an unconditional branch. This is just a cleanup so the
514  // emitted code isn't too gross in this common case.
516  MergeBlockIntoPredecessor(OrigHeader, &DTU, LI, MSSAU);
517 
518  if (MSSAU && VerifyMemorySSA)
519  MSSAU->getMemorySSA()->verifyMemorySSA();
520 
521  LLVM_DEBUG(dbgs() << "LoopRotation: into "; L->dump());
522 
523  ++NumRotated;
524  return true;
525 }
526 
527 /// Determine whether the instructions in this range may be safely and cheaply
528 /// speculated. This is not an important enough situation to develop complex
529 /// heuristics. We handle a single arithmetic instruction along with any type
530 /// conversions.
532  BasicBlock::iterator End, Loop *L) {
533  bool seenIncrement = false;
534  bool MultiExitLoop = false;
535 
536  if (!L->getExitingBlock())
537  MultiExitLoop = true;
538 
539  for (BasicBlock::iterator I = Begin; I != End; ++I) {
540 
542  return false;
543 
544  if (isa<DbgInfoIntrinsic>(I))
545  continue;
546 
547  switch (I->getOpcode()) {
548  default:
549  return false;
550  case Instruction::GetElementPtr:
551  // GEPs are cheap if all indices are constant.
552  if (!cast<GEPOperator>(I)->hasAllConstantIndices())
553  return false;
554  // fall-thru to increment case
556  case Instruction::Add:
557  case Instruction::Sub:
558  case Instruction::And:
559  case Instruction::Or:
560  case Instruction::Xor:
561  case Instruction::Shl:
562  case Instruction::LShr:
563  case Instruction::AShr: {
564  Value *IVOpnd =
565  !isa<Constant>(I->getOperand(0))
566  ? I->getOperand(0)
567  : !isa<Constant>(I->getOperand(1)) ? I->getOperand(1) : nullptr;
568  if (!IVOpnd)
569  return false;
570 
571  // If increment operand is used outside of the loop, this speculation
572  // could cause extra live range interference.
573  if (MultiExitLoop) {
574  for (User *UseI : IVOpnd->users()) {
575  auto *UserInst = cast<Instruction>(UseI);
576  if (!L->contains(UserInst))
577  return false;
578  }
579  }
580 
581  if (seenIncrement)
582  return false;
583  seenIncrement = true;
584  break;
585  }
586  case Instruction::Trunc:
587  case Instruction::ZExt:
588  case Instruction::SExt:
589  // ignore type conversions
590  break;
591  }
592  }
593  return true;
594 }
595 
596 /// Fold the loop tail into the loop exit by speculating the loop tail
597 /// instructions. Typically, this is a single post-increment. In the case of a
598 /// simple 2-block loop, hoisting the increment can be much better than
599 /// duplicating the entire loop header. In the case of loops with early exits,
600 /// rotation will not work anyway, but simplifyLoopLatch will put the loop in
601 /// canonical form so downstream passes can handle it.
602 ///
603 /// I don't believe this invalidates SCEV.
604 bool LoopRotate::simplifyLoopLatch(Loop *L) {
605  BasicBlock *Latch = L->getLoopLatch();
606  if (!Latch || Latch->hasAddressTaken())
607  return false;
608 
609  BranchInst *Jmp = dyn_cast<BranchInst>(Latch->getTerminator());
610  if (!Jmp || !Jmp->isUnconditional())
611  return false;
612 
613  BasicBlock *LastExit = Latch->getSinglePredecessor();
614  if (!LastExit || !L->isLoopExiting(LastExit))
615  return false;
616 
617  BranchInst *BI = dyn_cast<BranchInst>(LastExit->getTerminator());
618  if (!BI)
619  return false;
620 
621  if (!shouldSpeculateInstrs(Latch->begin(), Jmp->getIterator(), L))
622  return false;
623 
624  LLVM_DEBUG(dbgs() << "Folding loop latch " << Latch->getName() << " into "
625  << LastExit->getName() << "\n");
626 
628  MergeBlockIntoPredecessor(Latch, &DTU, LI, MSSAU, nullptr,
629  /*PredecessorWithTwoSuccessors=*/true);
630 
631  if (MSSAU && VerifyMemorySSA)
632  MSSAU->getMemorySSA()->verifyMemorySSA();
633 
634  return true;
635 }
636 
637 /// Rotate \c L, and return true if any modification was made.
638 bool LoopRotate::processLoop(Loop *L) {
639  // Save the loop metadata.
640  MDNode *LoopMD = L->getLoopID();
641 
642  bool SimplifiedLatch = false;
643 
644  // Simplify the loop latch before attempting to rotate the header
645  // upward. Rotation may not be needed if the loop tail can be folded into the
646  // loop exit.
647  if (!RotationOnly)
648  SimplifiedLatch = simplifyLoopLatch(L);
649 
650  bool MadeChange = rotateLoop(L, SimplifiedLatch);
651  assert((!MadeChange || L->isLoopExiting(L->getLoopLatch())) &&
652  "Loop latch should be exiting after loop-rotate.");
653 
654  // Restore the loop metadata.
655  // NB! We presume LoopRotation DOESN'T ADD its own metadata.
656  if ((MadeChange || SimplifiedLatch) && LoopMD)
657  L->setLoopID(LoopMD);
658 
659  return MadeChange || SimplifiedLatch;
660 }
661 
662 
663 /// The utility to convert a loop into a loop with bottom test.
666  ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
667  const SimplifyQuery &SQ, bool RotationOnly = true,
668  unsigned Threshold = unsigned(-1),
669  bool IsUtilMode = true) {
670  if (MSSAU && VerifyMemorySSA)
671  MSSAU->getMemorySSA()->verifyMemorySSA();
672  LoopRotate LR(Threshold, LI, TTI, AC, DT, SE, MSSAU, SQ, RotationOnly,
673  IsUtilMode);
674  if (MSSAU && VerifyMemorySSA)
675  MSSAU->getMemorySSA()->verifyMemorySSA();
676 
677  return LR.processLoop(L);
678 }
static void collectEphemeralValues(const Loop *L, AssumptionCache *AC, SmallPtrSetImpl< const Value *> &EphValues)
Collect a loop&#39;s ephemeral values (those used only by an assume or similar intrinsics in the loop)...
Definition: CodeMetrics.cpp:70
uint64_t CallInst * C
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
use_iterator use_end()
Definition: Value.h:367
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:38
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:209
bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition: MemorySSA.cpp:84
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: ValueMap.h:173
This is the interface for a simple mod/ref and alias analysis over globals.
bool convergent
True if this function contains a call to a convergent function.
Definition: CodeMetrics.h:47
void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type &#39;Ty&#39;.
Definition: SSAUpdater.cpp:53
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
Definition: LoopInfoImpl.h:85
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Notify the BasicBlock that the predecessor Pred is no longer able to reach it.
Definition: BasicBlock.cpp:308
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value...
Definition: SSAUpdater.cpp:71
void moveToHeader(BlockT *BB)
This method is used to move BB (which must be part of this loop) to be the loop header of the loop (t...
Definition: LoopInfo.h:423
The main scalar evolution driver.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:160
bool mayWriteToMemory() const
Return true if this instruction may modify memory.
static bool shouldSpeculateInstrs(BasicBlock::iterator Begin, BasicBlock::iterator End, Loop *L)
Determine whether the instructions in this range may be safely and cheaply speculated.
A cache of @llvm.assume calls within a function.
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:744
bool isTerminator() const
Definition: Instruction.h:128
void deleteValue()
Delete a pointer to a generic Value.
Definition: Value.cpp:99
unsigned second
BasicBlock * getSuccessor(unsigned i) const
STATISTIC(NumFunctions, "Total number of functions")
Metadata node.
Definition: Metadata.h:872
reverse_iterator rend()
Definition: BasicBlock.h:280
reverse_iterator rbegin()
Definition: BasicBlock.h:278
Value * getCondition() const
const Instruction * 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:144
bool notDuplicatable
True if this function cannot be duplicated.
Definition: CodeMetrics.h:44
bool hasLoopInvariantOperands(const Instruction *I) const
Return true if all the operands of the specified instruction are loop invariant.
Definition: LoopInfo.cpp:68
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:273
bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr, bool PredecessorWithTwoSuccessors=false)
Attempts to merge a block into its predecessor, if possible.
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
This is the interface for a SCEV-based alias analysis.
MemorySSA * getMemorySSA() const
Get handle on MemorySSA.
Option class for critical edge splitting.
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:286
BlockT * getHeader() const
Definition: LoopInfo.h:105
void insertDebugValuesForPHIs(BasicBlock *BB, SmallVectorImpl< PHINode *> &InsertedPHIs)
Propagate dbg.value intrinsics through the newly inserted PHIs.
Definition: Local.cpp:1458
Instruction * clone() const
Create a copy of &#39;this&#39; instruction that is identical in all ways except the following: ...
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
Definition: Use.cpp:40
void findDbgValues(SmallVectorImpl< DbgValueInst *> &DbgValues, Value *V)
Finds the llvm.dbg.value intrinsics describing a value.
Definition: Local.cpp:1524
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
Definition: LoopInfo.cpp:514
This is the common base class for debug info intrinsics for variables.
Definition: IntrinsicInst.h:88
Memory SSA
Definition: MemorySSA.cpp:66
BasicBlock * SplitCriticalEdge(Instruction *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
If this edge is a critical edge, insert a new node to split the critical edge.
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: ValueMap.h:165
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition: ValueMapper.h:72
use_iterator_impl< Use > use_iterator
Definition: Value.h:352
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:105
Value * GetValueInMiddleOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live in the middle of the specified block...
Definition: SSAUpdater.cpp:99
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:328
void dump() const
Definition: LoopInfo.cpp:648
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:240
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
Definition: Instruction.cpp:73
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
bool isLoopExiting(const BlockT *BB) const
True if terminator in the block can branch to another block that is outside of the current loop...
Definition: LoopInfo.h:208
static bool shouldRotateLoopExitingLatch(Loop *L)
Conditional or Unconditional Branch instruction.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Value * getIncomingValueForBlock(const BasicBlock *BB) const
Machine Trace Metrics
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
Definition: Instruction.h:582
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:112
void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI, const SmallPtrSetImpl< const Value *> &EphValues)
Add information about a block to the current state.
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:1189
bool LoopRotation(Loop *L, LoopInfo *LI, const TargetTransformInfo *TTI, AssumptionCache *AC, DominatorTree *DT, ScalarEvolution *SE, MemorySSAUpdater *MSSAU, const SimplifyQuery &SQ, bool RotationOnly, unsigned Threshold, bool IsUtilMode)
Convert a loop into a loop with bottom test.
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:115
self_iterator getIterator()
Definition: ilist_node.h:81
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1514
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc.
Definition: BasicBlock.h:396
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:115
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:443
void verifyMemorySSA() const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
Definition: MemorySSA.cpp:1872
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
iterator end()
Definition: BasicBlock.h:275
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:348
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Module.h This file contains the declarations for the Module class.
Utility to calculate the size and a few similar metrics for a set of basic blocks.
Definition: CodeMetrics.h:32
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
bool isConditional() const
DWARF expression.
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Implements a dense probed hash-table based set with some number of buckets stored inline...
Definition: DenseSet.h:267
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
iterator_range< user_iterator > users()
Definition: Value.h:420
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM...
Definition: ValueMapper.h:251
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
void FoldSingleEntryPHINodes(BasicBlock *BB, MemoryDependenceResults *MemDep=nullptr)
We know that BB has one predecessor.
If this flag is set, the remapper ignores missing function-local entries (Argument, Instruction, BasicBlock) that are not in the value map.
Definition: ValueMapper.h:90
use_iterator use_begin()
Definition: Value.h:359
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:550
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
Definition: LoopInfo.cpp:490
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:331
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:509
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
Definition: LoopInfo.h:154
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:215
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
static void RewriteUsesOfClonedInstructions(BasicBlock *OrigHeader, BasicBlock *OrigPreheader, ValueToValueMapTy &ValueMap, SmallVectorImpl< PHINode *> *InsertedPHIs)
RewriteUsesOfClonedInstructions - We just cloned the instructions from the old header into the prehea...
#define I(x, y, z)
Definition: MD5.cpp:58
bool mayReadFromMemory() const
Return true if this instruction may read memory.
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:332
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition: BasicBlock.h:329
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:91
bool isUnconditional() const
static void InsertNewValueIntoMap(ValueToValueMapTy &VM, Value *K, Value *V)
Insert (K, V) pair into the ValueToValueMap, and verify the key did not previously exist in the map...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM Value Representation.
Definition: Value.h:74
succ_range successors(Instruction *I)
Definition: CFG.h:259
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:279
static const Function * getParent(const Value *V)
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Definition: Instruction.cpp:86
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
This is the interface for LLVM&#39;s primary stateless and local alias analysis.
This pass exposes codegen information to IR-level passes.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
unsigned NumInsts
Number of instructions in the analyzed blocks.
Definition: CodeMetrics.h:53
void RewriteUse(Use &U)
Rewrite a use of the symbolic value.
Definition: SSAUpdater.cpp:189
Value * SimplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:49
void moveBefore(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it into the function that MovePos lives ...
Definition: BasicBlock.cpp:127
bool use_empty() const
Definition: Value.h:343
const BasicBlock * getParent() const
Definition: Instruction.h:66
bool HasValueForBlock(BasicBlock *BB) const
Return true if the SSAUpdater already has a value for the specified block.
Definition: SSAUpdater.cpp:62