LLVM  10.0.0
MustExecute.cpp
Go to the documentation of this file.
1 //===- MustExecute.cpp - Printer for isGuaranteedToExecute ----------------===//
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 
11 #include "llvm/Analysis/CFG.h"
13 #include "llvm/Analysis/LoopInfo.h"
14 #include "llvm/Analysis/Passes.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/InstIterator.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/InitializePasses.h"
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "must-execute"
30 
33  return BlockColors;
34 }
35 
37  ColorVector &ColorsForNewBlock = BlockColors[New];
38  ColorVector &ColorsForOldBlock = BlockColors[Old];
39  ColorsForNewBlock = ColorsForOldBlock;
40 }
41 
43  (void)BB;
44  return anyBlockMayThrow();
45 }
46 
48  return MayThrow;
49 }
50 
52  assert(CurLoop != nullptr && "CurLoop can't be null");
53  BasicBlock *Header = CurLoop->getHeader();
54  // Iterate over header and compute safety info.
55  HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header);
56  MayThrow = HeaderMayThrow;
57  // Iterate over loop instructions and compute safety info.
58  // Skip header as it has been computed and stored in HeaderMayThrow.
59  // The first block in loopinfo.Blocks is guaranteed to be the header.
60  assert(Header == *CurLoop->getBlocks().begin() &&
61  "First block must be header");
62  for (Loop::block_iterator BB = std::next(CurLoop->block_begin()),
63  BBE = CurLoop->block_end();
64  (BB != BBE) && !MayThrow; ++BB)
66 
67  computeBlockColors(CurLoop);
68 }
69 
71  return ICF.hasICF(BB);
72 }
73 
75  return MayThrow;
76 }
77 
79  assert(CurLoop != nullptr && "CurLoop can't be null");
80  ICF.clear();
81  MW.clear();
82  MayThrow = false;
83  // Figure out the fact that at least one block may throw.
84  for (auto &BB : CurLoop->blocks())
85  if (ICF.hasICF(&*BB)) {
86  MayThrow = true;
87  break;
88  }
89  computeBlockColors(CurLoop);
90 }
91 
93  const BasicBlock *BB) {
94  ICF.insertInstructionTo(Inst, BB);
95  MW.insertInstructionTo(Inst, BB);
96 }
97 
99  ICF.removeInstruction(Inst);
100  MW.removeInstruction(Inst);
101 }
102 
104  // Compute funclet colors if we might sink/hoist in a function with a funclet
105  // personality routine.
106  Function *Fn = CurLoop->getHeader()->getParent();
107  if (Fn->hasPersonalityFn())
108  if (Constant *PersonalityFn = Fn->getPersonalityFn())
109  if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn)))
110  BlockColors = colorEHFunclets(*Fn);
111 }
112 
113 /// Return true if we can prove that the given ExitBlock is not reached on the
114 /// first iteration of the given loop. That is, the backedge of the loop must
115 /// be executed before the ExitBlock is executed in any dynamic execution trace.
116 static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock,
117  const DominatorTree *DT,
118  const Loop *CurLoop) {
119  auto *CondExitBlock = ExitBlock->getSinglePredecessor();
120  if (!CondExitBlock)
121  // expect unique exits
122  return false;
123  assert(CurLoop->contains(CondExitBlock) && "meaning of exit block");
124  auto *BI = dyn_cast<BranchInst>(CondExitBlock->getTerminator());
125  if (!BI || !BI->isConditional())
126  return false;
127  // If condition is constant and false leads to ExitBlock then we always
128  // execute the true branch.
129  if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition()))
130  return BI->getSuccessor(Cond->getZExtValue() ? 1 : 0) == ExitBlock;
131  auto *Cond = dyn_cast<CmpInst>(BI->getCondition());
132  if (!Cond)
133  return false;
134  // todo: this would be a lot more powerful if we used scev, but all the
135  // plumbing is currently missing to pass a pointer in from the pass
136  // Check for cmp (phi [x, preheader] ...), y where (pred x, y is known
137  auto *LHS = dyn_cast<PHINode>(Cond->getOperand(0));
138  auto *RHS = Cond->getOperand(1);
139  if (!LHS || LHS->getParent() != CurLoop->getHeader())
140  return false;
141  auto DL = ExitBlock->getModule()->getDataLayout();
142  auto *IVStart = LHS->getIncomingValueForBlock(CurLoop->getLoopPreheader());
143  auto *SimpleValOrNull = SimplifyCmpInst(Cond->getPredicate(),
144  IVStart, RHS,
145  {DL, /*TLI*/ nullptr,
146  DT, /*AC*/ nullptr, BI});
147  auto *SimpleCst = dyn_cast_or_null<Constant>(SimpleValOrNull);
148  if (!SimpleCst)
149  return false;
150  if (ExitBlock == BI->getSuccessor(0))
151  return SimpleCst->isZeroValue();
152  assert(ExitBlock == BI->getSuccessor(1) && "implied by above");
153  return SimpleCst->isAllOnesValue();
154 }
155 
156 /// Collect all blocks from \p CurLoop which lie on all possible paths from
157 /// the header of \p CurLoop (inclusive) to BB (exclusive) into the set
158 /// \p Predecessors. If \p BB is the header, \p Predecessors will be empty.
160  const Loop *CurLoop, const BasicBlock *BB,
161  SmallPtrSetImpl<const BasicBlock *> &Predecessors) {
162  assert(Predecessors.empty() && "Garbage in predecessors set?");
163  assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
164  if (BB == CurLoop->getHeader())
165  return;
167  for (auto *Pred : predecessors(BB)) {
168  Predecessors.insert(Pred);
169  WorkList.push_back(Pred);
170  }
171  while (!WorkList.empty()) {
172  auto *Pred = WorkList.pop_back_val();
173  assert(CurLoop->contains(Pred) && "Should only reach loop blocks!");
174  // We are not interested in backedges and we don't want to leave loop.
175  if (Pred == CurLoop->getHeader())
176  continue;
177  // TODO: If BB lies in an inner loop of CurLoop, this will traverse over all
178  // blocks of this inner loop, even those that are always executed AFTER the
179  // BB. It may make our analysis more conservative than it could be, see test
180  // @nested and @nested_no_throw in test/Analysis/MustExecute/loop-header.ll.
181  // We can ignore backedge of all loops containing BB to get a sligtly more
182  // optimistic result.
183  for (auto *PredPred : predecessors(Pred))
184  if (Predecessors.insert(PredPred).second)
185  WorkList.push_back(PredPred);
186  }
187 }
188 
190  const BasicBlock *BB,
191  const DominatorTree *DT) const {
192  assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
193 
194  // Fast path: header is always reached once the loop is entered.
195  if (BB == CurLoop->getHeader())
196  return true;
197 
198  // Collect all transitive predecessors of BB in the same loop. This set will
199  // be a subset of the blocks within the loop.
201  collectTransitivePredecessors(CurLoop, BB, Predecessors);
202 
203  // Make sure that all successors of, all predecessors of BB which are not
204  // dominated by BB, are either:
205  // 1) BB,
206  // 2) Also predecessors of BB,
207  // 3) Exit blocks which are not taken on 1st iteration.
208  // Memoize blocks we've already checked.
209  SmallPtrSet<const BasicBlock *, 4> CheckedSuccessors;
210  for (auto *Pred : Predecessors) {
211  // Predecessor block may throw, so it has a side exit.
212  if (blockMayThrow(Pred))
213  return false;
214 
215  // BB dominates Pred, so if Pred runs, BB must run.
216  // This is true when Pred is a loop latch.
217  if (DT->dominates(BB, Pred))
218  continue;
219 
220  for (auto *Succ : successors(Pred))
221  if (CheckedSuccessors.insert(Succ).second &&
222  Succ != BB && !Predecessors.count(Succ))
223  // By discharging conditions that are not executed on the 1st iteration,
224  // we guarantee that *at least* on the first iteration all paths from
225  // header that *may* execute will lead us to the block of interest. So
226  // that if we had virtually peeled one iteration away, in this peeled
227  // iteration the set of predecessors would contain only paths from
228  // header to BB without any exiting edges that may execute.
229  //
230  // TODO: We only do it for exiting edges currently. We could use the
231  // same function to skip some of the edges within the loop if we know
232  // that they will not be taken on the 1st iteration.
233  //
234  // TODO: If we somehow know the number of iterations in loop, the same
235  // check may be done for any arbitrary N-th iteration as long as N is
236  // not greater than minimum number of iterations in this loop.
237  if (CurLoop->contains(Succ) ||
238  !CanProveNotTakenFirstIteration(Succ, DT, CurLoop))
239  return false;
240  }
241 
242  // All predecessors can only lead us to BB.
243  return true;
244 }
245 
246 /// Returns true if the instruction in a loop is guaranteed to execute at least
247 /// once.
249  const DominatorTree *DT,
250  const Loop *CurLoop) const {
251  // If the instruction is in the header block for the loop (which is very
252  // common), it is always guaranteed to dominate the exit blocks. Since this
253  // is a common case, and can save some work, check it now.
254  if (Inst.getParent() == CurLoop->getHeader())
255  // If there's a throw in the header block, we can't guarantee we'll reach
256  // Inst unless we can prove that Inst comes before the potential implicit
257  // exit. At the moment, we use a (cheap) hack for the common case where
258  // the instruction of interest is the first one in the block.
259  return !HeaderMayThrow ||
260  Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
261 
262  // If there is a path from header to exit or latch that doesn't lead to our
263  // instruction's block, return false.
264  return allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT);
265 }
266 
268  const DominatorTree *DT,
269  const Loop *CurLoop) const {
270  return !ICF.isDominatedByICFIFromSameBlock(&Inst) &&
271  allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT);
272 }
273 
275  const Loop *CurLoop) const {
276  assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
277 
278  // Fast path: there are no instructions before header.
279  if (BB == CurLoop->getHeader())
280  return true;
281 
282  // Collect all transitive predecessors of BB in the same loop. This set will
283  // be a subset of the blocks within the loop.
285  collectTransitivePredecessors(CurLoop, BB, Predecessors);
286  // Find if there any instruction in either predecessor that could write
287  // to memory.
288  for (auto *Pred : Predecessors)
289  if (MW.mayWriteToMemory(Pred))
290  return false;
291  return true;
292 }
293 
295  const Loop *CurLoop) const {
296  auto *BB = I.getParent();
297  assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
298  return !MW.isDominatedByMemoryWriteFromSameBlock(&I) &&
299  doesNotWriteMemoryBefore(BB, CurLoop);
300 }
301 
302 namespace {
303  struct MustExecutePrinter : public FunctionPass {
304 
305  static char ID; // Pass identification, replacement for typeid
306  MustExecutePrinter() : FunctionPass(ID) {
308  }
309  void getAnalysisUsage(AnalysisUsage &AU) const override {
310  AU.setPreservesAll();
313  }
314  bool runOnFunction(Function &F) override;
315  };
316  struct MustBeExecutedContextPrinter : public ModulePass {
317  static char ID;
318 
319  MustBeExecutedContextPrinter() : ModulePass(ID) {
321  }
322  void getAnalysisUsage(AnalysisUsage &AU) const override {
323  AU.setPreservesAll();
324  }
325  bool runOnModule(Module &M) override;
326  };
327 }
328 
329 char MustExecutePrinter::ID = 0;
330 INITIALIZE_PASS_BEGIN(MustExecutePrinter, "print-mustexecute",
331  "Instructions which execute on loop entry", false, true)
334 INITIALIZE_PASS_END(MustExecutePrinter, "print-mustexecute",
335  "Instructions which execute on loop entry", false, true)
336 
338  return new MustExecutePrinter();
339 }
340 
343  MustBeExecutedContextPrinter, "print-must-be-executed-contexts",
344  "print the must-be-executed-contexed for all instructions", false, true)
348 INITIALIZE_PASS_END(MustBeExecutedContextPrinter,
349  "print-must-be-executed-contexts",
350  "print the must-be-executed-contexed for all instructions",
351  false, true)
352 
354  return new MustBeExecutedContextPrinter();
355 }
356 
357 bool MustBeExecutedContextPrinter::runOnModule(Module &M) {
358  // We provide non-PM analysis here because the old PM doesn't like to query
359  // function passes from a module pass.
363 
364  GetterTy<LoopInfo> LIGetter = [&](const Function &F) {
365  DominatorTree *DT = new DominatorTree(const_cast<Function &>(F));
366  LoopInfo *LI = new LoopInfo(*DT);
367  DTs.push_back(DT);
368  LIs.push_back(LI);
369  return LI;
370  };
371  GetterTy<PostDominatorTree> PDTGetter = [&](const Function &F) {
372  PostDominatorTree *PDT = new PostDominatorTree(const_cast<Function &>(F));
373  PDTs.push_back(PDT);
374  return PDT;
375  };
376  MustBeExecutedContextExplorer Explorer(true, LIGetter, PDTGetter);
377  for (Function &F : M) {
378  for (Instruction &I : instructions(F)) {
379  dbgs() << "-- Explore context of: " << I << "\n";
380  for (const Instruction *CI : Explorer.range(&I))
381  dbgs() << " [F: " << CI->getFunction()->getName() << "] " << *CI
382  << "\n";
383  }
384  }
385 
389  return false;
390 }
391 
392 static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) {
393  // TODO: merge these two routines. For the moment, we display the best
394  // result obtained by *either* implementation. This is a bit unfair since no
395  // caller actually gets the full power at the moment.
397  LSI.computeLoopSafetyInfo(L);
398  return LSI.isGuaranteedToExecute(I, DT, L) ||
400 }
401 
402 namespace {
403 /// An assembly annotator class to print must execute information in
404 /// comments.
405 class MustExecuteAnnotatedWriter : public AssemblyAnnotationWriter {
407 
408 public:
409  MustExecuteAnnotatedWriter(const Function &F,
410  DominatorTree &DT, LoopInfo &LI) {
411  for (auto &I: instructions(F)) {
412  Loop *L = LI.getLoopFor(I.getParent());
413  while (L) {
414  if (isMustExecuteIn(I, L, &DT)) {
415  MustExec[&I].push_back(L);
416  }
417  L = L->getParentLoop();
418  };
419  }
420  }
421  MustExecuteAnnotatedWriter(const Module &M,
422  DominatorTree &DT, LoopInfo &LI) {
423  for (auto &F : M)
424  for (auto &I: instructions(F)) {
425  Loop *L = LI.getLoopFor(I.getParent());
426  while (L) {
427  if (isMustExecuteIn(I, L, &DT)) {
428  MustExec[&I].push_back(L);
429  }
430  L = L->getParentLoop();
431  };
432  }
433  }
434 
435 
436  void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
437  if (!MustExec.count(&V))
438  return;
439 
440  const auto &Loops = MustExec.lookup(&V);
441  const auto NumLoops = Loops.size();
442  if (NumLoops > 1)
443  OS << " ; (mustexec in " << NumLoops << " loops: ";
444  else
445  OS << " ; (mustexec in: ";
446 
447  bool first = true;
448  for (const Loop *L : Loops) {
449  if (!first)
450  OS << ", ";
451  first = false;
452  OS << L->getHeader()->getName();
453  }
454  OS << ")";
455  }
456 };
457 } // namespace
458 
460  auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
461  auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
462 
463  MustExecuteAnnotatedWriter Writer(F, DT, LI);
464  F.print(dbgs(), &Writer);
465 
466  return false;
467 }
468 
469 /// Return true if \p L might be an endless loop.
470 static bool maybeEndlessLoop(const Loop &L) {
471  if (L.getHeader()->getParent()->hasFnAttribute(Attribute::WillReturn))
472  return false;
473  // TODO: Actually try to prove it is not.
474  // TODO: If maybeEndlessLoop is going to be expensive, cache it.
475  return true;
476 }
477 
478 static bool mayContainIrreducibleControl(const Function &F, const LoopInfo *LI) {
479  if (!LI)
480  return false;
481  using RPOTraversal = ReversePostOrderTraversal<const Function *>;
482  RPOTraversal FuncRPOT(&F);
483  return !containsIrreducibleCFG<const BasicBlock *, const RPOTraversal,
484  const LoopInfo>(FuncRPOT, *LI);
485 }
486 
487 /// Lookup \p Key in \p Map and return the result, potentially after
488 /// initializing the optional through \p Fn(\p args).
489 template <typename K, typename V, typename FnTy, typename... ArgsTy>
491  FnTy &&Fn, ArgsTy&&... args) {
492  Optional<V> &OptVal = Map[Key];
493  if (!OptVal.hasValue())
494  OptVal = Fn(std::forward<ArgsTy>(args)...);
495  return OptVal.getValue();
496 }
497 
498 const BasicBlock *
500  const LoopInfo *LI = LIGetter(*InitBB->getParent());
501  const PostDominatorTree *PDT = PDTGetter(*InitBB->getParent());
502 
503  LLVM_DEBUG(dbgs() << "\tFind forward join point for " << InitBB->getName()
504  << (LI ? " [LI]" : "") << (PDT ? " [PDT]" : ""));
505 
506  const Function &F = *InitBB->getParent();
507  const Loop *L = LI ? LI->getLoopFor(InitBB) : nullptr;
508  const BasicBlock *HeaderBB = L ? L->getHeader() : InitBB;
509  bool WillReturnAndNoThrow = (F.hasFnAttribute(Attribute::WillReturn) ||
510  (L && !maybeEndlessLoop(*L))) &&
511  F.doesNotThrow();
512  LLVM_DEBUG(dbgs() << (L ? " [in loop]" : "")
513  << (WillReturnAndNoThrow ? " [WillReturn] [NoUnwind]" : "")
514  << "\n");
515 
516  // Determine the adjacent blocks in the given direction but exclude (self)
517  // loops under certain circumstances.
519  for (const BasicBlock *SuccBB : successors(InitBB)) {
520  bool IsLatch = SuccBB == HeaderBB;
521  // Loop latches are ignored in forward propagation if the loop cannot be
522  // endless and may not throw: control has to go somewhere.
523  if (!WillReturnAndNoThrow || !IsLatch)
524  Worklist.push_back(SuccBB);
525  }
526  LLVM_DEBUG(dbgs() << "\t\t#Worklist: " << Worklist.size() << "\n");
527 
528  // If there are no other adjacent blocks, there is no join point.
529  if (Worklist.empty())
530  return nullptr;
531 
532  // If there is one adjacent block, it is the join point.
533  if (Worklist.size() == 1)
534  return Worklist[0];
535 
536  // Try to determine a join block through the help of the post-dominance
537  // tree. If no tree was provided, we perform simple pattern matching for one
538  // block conditionals and one block loops only.
539  const BasicBlock *JoinBB = nullptr;
540  if (PDT)
541  if (const auto *InitNode = PDT->getNode(InitBB))
542  if (const auto *IDomNode = InitNode->getIDom())
543  JoinBB = IDomNode->getBlock();
544 
545  if (!JoinBB && Worklist.size() == 2) {
546  const BasicBlock *Succ0 = Worklist[0];
547  const BasicBlock *Succ1 = Worklist[1];
548  const BasicBlock *Succ0UniqueSucc = Succ0->getUniqueSuccessor();
549  const BasicBlock *Succ1UniqueSucc = Succ1->getUniqueSuccessor();
550  if (Succ0UniqueSucc == InitBB) {
551  // InitBB -> Succ0 -> InitBB
552  // InitBB -> Succ1 = JoinBB
553  JoinBB = Succ1;
554  } else if (Succ1UniqueSucc == InitBB) {
555  // InitBB -> Succ1 -> InitBB
556  // InitBB -> Succ0 = JoinBB
557  JoinBB = Succ0;
558  } else if (Succ0 == Succ1UniqueSucc) {
559  // InitBB -> Succ0 = JoinBB
560  // InitBB -> Succ1 -> Succ0 = JoinBB
561  JoinBB = Succ0;
562  } else if (Succ1 == Succ0UniqueSucc) {
563  // InitBB -> Succ0 -> Succ1 = JoinBB
564  // InitBB -> Succ1 = JoinBB
565  JoinBB = Succ1;
566  } else if (Succ0UniqueSucc == Succ1UniqueSucc) {
567  // InitBB -> Succ0 -> JoinBB
568  // InitBB -> Succ1 -> JoinBB
569  JoinBB = Succ0UniqueSucc;
570  }
571  }
572 
573  if (!JoinBB && L)
574  JoinBB = L->getUniqueExitBlock();
575 
576  if (!JoinBB)
577  return nullptr;
578 
579  LLVM_DEBUG(dbgs() << "\t\tJoin block candidate: " << JoinBB->getName() << "\n");
580 
581  // In forward direction we check if control will for sure reach JoinBB from
582  // InitBB, thus it can not be "stopped" along the way. Ways to "stop" control
583  // are: infinite loops and instructions that do not necessarily transfer
584  // execution to their successor. To check for them we traverse the CFG from
585  // the adjacent blocks to the JoinBB, looking at all intermediate blocks.
586 
587  // If we know the function is "will-return" and "no-throw" there is no need
588  // for futher checks.
589  if (!F.hasFnAttribute(Attribute::WillReturn) || !F.doesNotThrow()) {
590 
591  auto BlockTransfersExecutionToSuccessor = [](const BasicBlock *BB) {
593  };
594 
596  while (!Worklist.empty()) {
597  const BasicBlock *ToBB = Worklist.pop_back_val();
598  if (ToBB == JoinBB)
599  continue;
600 
601  // Make sure all loops in-between are finite.
602  if (!Visited.insert(ToBB).second) {
603  if (!F.hasFnAttribute(Attribute::WillReturn)) {
604  if (!LI)
605  return nullptr;
606 
607  bool MayContainIrreducibleControl = getOrCreateCachedOptional(
608  &F, IrreducibleControlMap, mayContainIrreducibleControl, F, LI);
609  if (MayContainIrreducibleControl)
610  return nullptr;
611 
612  const Loop *L = LI->getLoopFor(ToBB);
613  if (L && maybeEndlessLoop(*L))
614  return nullptr;
615  }
616 
617  continue;
618  }
619 
620  // Make sure the block has no instructions that could stop control
621  // transfer.
622  bool TransfersExecution = getOrCreateCachedOptional(
623  ToBB, BlockTransferMap, BlockTransfersExecutionToSuccessor, ToBB);
624  if (!TransfersExecution)
625  return nullptr;
626 
627  for (const BasicBlock *AdjacentBB : successors(ToBB))
628  Worklist.push_back(AdjacentBB);
629  }
630  }
631 
632  LLVM_DEBUG(dbgs() << "\tJoin block: " << JoinBB->getName() << "\n");
633  return JoinBB;
634 }
635 
636 const Instruction *
638  MustBeExecutedIterator &It, const Instruction *PP) {
639  if (!PP)
640  return PP;
641  LLVM_DEBUG(dbgs() << "Find next instruction for " << *PP << "\n");
642 
643  // If we explore only inside a given basic block we stop at terminators.
644  if (!ExploreInterBlock && PP->isTerminator()) {
645  LLVM_DEBUG(dbgs() << "\tReached terminator in intra-block mode, done\n");
646  return nullptr;
647  }
648 
649  // If we do not traverse the call graph we check if we can make progress in
650  // the current function. First, check if the instruction is guaranteed to
651  // transfer execution to the successor.
652  bool TransfersExecution = isGuaranteedToTransferExecutionToSuccessor(PP);
653  if (!TransfersExecution)
654  return nullptr;
655 
656  // If this is not a terminator we know that there is a single instruction
657  // after this one that is executed next if control is transfered. If not,
658  // we can try to go back to a call site we entered earlier. If none exists, we
659  // do not know any instruction that has to be executd next.
660  if (!PP->isTerminator()) {
661  const Instruction *NextPP = PP->getNextNode();
662  LLVM_DEBUG(dbgs() << "\tIntermediate instruction does transfer control\n");
663  return NextPP;
664  }
665 
666  // Finally, we have to handle terminators, trivial ones first.
667  assert(PP->isTerminator() && "Expected a terminator!");
668 
669  // A terminator without a successor is not handled yet.
670  if (PP->getNumSuccessors() == 0) {
671  LLVM_DEBUG(dbgs() << "\tUnhandled terminator\n");
672  return nullptr;
673  }
674 
675  // A terminator with a single successor, we will continue at the beginning of
676  // that one.
677  if (PP->getNumSuccessors() == 1) {
678  LLVM_DEBUG(
679  dbgs() << "\tUnconditional terminator, continue with successor\n");
680  return &PP->getSuccessor(0)->front();
681  }
682 
683  // Multiple successors mean we need to find the join point where control flow
684  // converges again. We use the findForwardJoinPoint helper function with
685  // information about the function and helper analyses, if available.
686  if (const BasicBlock *JoinBB = findForwardJoinPoint(PP->getParent()))
687  return &JoinBB->front();
688 
689  LLVM_DEBUG(dbgs() << "\tNo join point found\n");
690  return nullptr;
691 }
692 
694  MustBeExecutedContextExplorer &Explorer, const Instruction *I)
695  : Explorer(Explorer), CurInst(I) {
696  reset(I);
697 }
698 
699 void MustBeExecutedIterator::reset(const Instruction *I) {
700  CurInst = I;
701  Visited.clear();
702  Visited.insert(I);
703 }
704 
705 const Instruction *MustBeExecutedIterator::advance() {
706  assert(CurInst && "Cannot advance an end iterator!");
707  const Instruction *Next =
708  Explorer.getMustBeExecutedNextInstruction(*this, CurInst);
709  if (Next && !Visited.insert(Next).second)
710  Next = nullptr;
711  return Next;
712 }
virtual void computeLoopSafetyInfo(const Loop *CurLoop)
Computes safety information for a loop checks loop body & header for the possibility of may throw exc...
Definition: MustExecute.cpp:51
void DeleteContainerPointers(Container &C)
For a container of pointers, deletes the pointers and then clears the container.
Definition: STLExtras.h:1145
void insertInstructionTo(const Instruction *Inst, const BasicBlock *BB)
Inform the safety info that we are planning to insert a new instruction Inst into the basic block BB...
Definition: MustExecute.cpp:92
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:722
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:288
virtual bool blockMayThrow(const BasicBlock *BB) const =0
Returns true iff the block BB potentially may throw exception.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock, const DominatorTree *DT, const Loop *CurLoop)
Return true if we can prove that the given ExitBlock is not reached on the first iteration of the giv...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:160
bool isTerminator() const
Definition: Instruction.h:128
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:323
virtual bool anyBlockMayThrow() const =0
Returns true iff any block of the loop for which this info is contains an instruction that may throw ...
virtual bool blockMayThrow(const BasicBlock *BB) const
Returns true iff the block BB potentially may throw exception.
Definition: MustExecute.cpp:70
void initializeMustBeExecutedContextPrinterPass(PassRegistry &)
BasicBlock * getSuccessor(unsigned i) const
F(f)
block Block Frequency true
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
Definition: TinyPtrVector.h:30
Simple and conservative implementation of LoopSafetyInfo that can give false-positive answers to its ...
Definition: MustExecute.h:109
AnalysisUsage & addRequired()
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:140
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
Hexagon Hardware Loops
virtual void computeLoopSafetyInfo(const Loop *CurLoop)
Computes safety information for a loop checks loop body & header for the possibility of may throw exc...
Definition: MustExecute.cpp:78
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:939
Contains a collection of routines for determining if a given instruction is guaranteed to execute if ...
virtual bool anyBlockMayThrow() const
Returns true iff any block of the loop for which this info is contains an instruction that may throw ...
Definition: MustExecute.cpp:74
BlockT * getHeader() const
Definition: LoopInfo.h:105
static void collectTransitivePredecessors(const Loop *CurLoop, const BasicBlock *BB, SmallPtrSetImpl< const BasicBlock *> &Predecessors)
Collect all blocks from CurLoop which lie on all possible paths from the header of CurLoop (inclusive...
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch, catchpad/ret, and cleanuppad/ret.
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI)
Return true if the control flow in RPOTraversal is irreducible.
Definition: CFG.h:145
llvm::iterator_range< iterator > range(const Instruction *PP)
}
Definition: MustExecute.h:424
Must be executed iterators visit stretches of instructions that are guaranteed to be executed togethe...
Definition: MustExecute.h:271
Key
PAL metadata keys.
Value * SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
virtual bool isGuaranteedToExecute(const Instruction &Inst, const DominatorTree *DT, const Loop *CurLoop) const
Returns true if the instruction in a loop is guaranteed to execute at least once. ...
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:255
void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW=nullptr, bool ShouldPreserveUseListOrder=false, bool IsForDebug=false) const
Print the function to an output stream with an optional AssemblyAnnotationWriter. ...
Definition: AsmWriter.cpp:4222
const Instruction * getMustBeExecutedNextInstruction(MustBeExecutedIterator &It, const Instruction *PP)
Return the next instruction that is guaranteed to be executed after PP.
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:743
void copyColors(BasicBlock *New, BasicBlock *Old)
Copy colors of block Old into the block New.
Definition: MustExecute.cpp:36
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
FunctionPass * createMustExecutePrinter()
static bool runOnFunction(Function &F, bool PostInlining)
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
virtual bool isGuaranteedToExecute(const Instruction &Inst, const DominatorTree *DT, const Loop *CurLoop) const
Returns true if the instruction in a loop is guaranteed to execute at least once (under the assumptio...
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:240
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
Conditional or Unconditional Branch instruction.
This is an important base class in LLVM.
Definition: Constant.h:41
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:91
const Instruction & front() const
Definition: BasicBlock.h:285
const BasicBlock * findForwardJoinPoint(const BasicBlock *InitBB)
Find the next join point from InitBB in forward direction.
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:370
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
Represent the analysis usage information of a pass.
MustBeExecutedIterator(const MustBeExecutedIterator &Other)
Definition: MustExecute.h:283
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
INITIALIZE_PASS_BEGIN(MustExecutePrinter, "print-mustexecute", "Instructions which execute on loop entry", false, true) INITIALIZE_PASS_END(MustExecutePrinter
BlockT * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:131
size_t size() const
Definition: SmallVector.h:52
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
unsigned first
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:115
bool allLoopPathsLeadToBlock(const Loop *CurLoop, const BasicBlock *BB, const DominatorTree *DT) const
Return true if we must reach the block BB under assumption that the loop CurLoop is entered...
void removeInstruction(const Instruction *Inst)
Inform safety info that we are planning to remove the instruction Inst from its block.
Definition: MustExecute.cpp:98
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:443
bool doesNotWriteMemoryBefore(const BasicBlock *BB, const Loop *CurLoop) const
Returns true if we could not execute a memory-modifying instruction before we enter BB under assumpti...
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:249
Module.h This file contains the declarations for the Module class.
static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT)
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
void computeBlockColors(const Loop *CurLoop)
Computes block colors.
pred_range predecessors(BasicBlock *BB)
Definition: CFG.h:124
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void setPreservesAll()
Set by analyses that do not transform their input at all.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
A "must be executed context" for a given program point PP is the set of instructions, potentially before and after PP, that are executed always when PP is reached.
Definition: MustExecute.h:373
LoopT * getParentLoop() const
Definition: LoopInfo.h:106
bool hasValue() const
Definition: Optional.h:259
print mustexecute
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
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass * createMustBeExecutedContextPrinter()
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:222
static V getOrCreateCachedOptional(K Key, DenseMap< K, Optional< V >> &Map, FnTy &&Fn, ArgsTy &&... args)
Lookup Key in Map and return the result, potentially after initializing the optional through Fn(args)...
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
block_iterator block_end() const
Definition: LoopInfo.h:160
virtual bool blockMayThrow(const BasicBlock *BB) const
Returns true iff the block BB potentially may throw exception.
Definition: MustExecute.cpp:42
bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:145
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:185
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.cpp:1484
succ_range successors(Instruction *I)
Definition: CFG.h:259
The legacy pass manager&#39;s analysis pass to compute loop information.
Definition: LoopInfo.h:1219
print Instructions which execute on loop entry
inst_range instructions(Function *F)
Definition: InstIterator.h:133
void initializeMustExecutePrinterPass(PassRegistry &)
const Instruction * getFirstNonPHIOrDbg() const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic...
Definition: BasicBlock.cpp:203
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:259
virtual bool anyBlockMayThrow() const
Returns true iff any block of the loop for which this info is contains an instruction that may throw ...
Definition: MustExecute.cpp:47
DenseMap< BasicBlock *, ColorVector > colorEHFunclets(Function &F)
If an EH funclet personality is in use (see isFuncletEHPersonality), this will recompute which blocks...
#define LLVM_DEBUG(X)
Definition: Debug.h:122
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:161
block_iterator block_begin() const
Definition: LoopInfo.h:159
static bool maybeEndlessLoop(const Loop &L)
Return true if L might be an endless loop.
const BasicBlock * getParent() const
Definition: Instruction.h:66
static bool mayContainIrreducibleControl(const Function &F, const LoopInfo *LI)
const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
Definition: BasicBlock.cpp:283
const DenseMap< BasicBlock *, ColorVector > & getBlockColors() const
Returns block colors map that is used to update funclet operand bundles.
Definition: MustExecute.cpp:32