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
9 #include "llvm/Analysis/MustExecute.h"
10 #include "llvm/ADT/PostOrderIterator.h"
11 #include "llvm/Analysis/CFG.h"
12 #include "llvm/Analysis/InstructionSimplify.h"
13 #include "llvm/Analysis/LoopInfo.h"
14 #include "llvm/Analysis/Passes.h"
15 #include "llvm/Analysis/PostDominators.h"
16 #include "llvm/Analysis/ValueTracking.h"
17 #include "llvm/IR/AssemblyAnnotationWriter.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/InstIterator.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/PassManager.h"
24 #include "llvm/InitializePasses.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include "llvm/Support/raw_ostream.h"
28
29 using namespace llvm;
30
31 #define DEBUG_TYPE "must-execute"
32
33 const DenseMap<BasicBlock *, ColorVector> &
getBlockColors() const34 LoopSafetyInfo::getBlockColors() const {
35 return BlockColors;
36 }
37
copyColors(BasicBlock * New,BasicBlock * Old)38 void LoopSafetyInfo::copyColors(BasicBlock *New, BasicBlock *Old) {
39 ColorVector &ColorsForNewBlock = BlockColors[New];
40 ColorVector &ColorsForOldBlock = BlockColors[Old];
41 ColorsForNewBlock = ColorsForOldBlock;
42 }
43
blockMayThrow(const BasicBlock * BB) const44 bool SimpleLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const {
45 (void)BB;
46 return anyBlockMayThrow();
47 }
48
anyBlockMayThrow() const49 bool SimpleLoopSafetyInfo::anyBlockMayThrow() const {
50 return MayThrow;
51 }
52
computeLoopSafetyInfo(const Loop * CurLoop)53 void SimpleLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) {
54 assert(CurLoop != nullptr && "CurLoop can't be null");
55 BasicBlock *Header = CurLoop->getHeader();
56 // Iterate over header and compute safety info.
57 HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header);
58 MayThrow = HeaderMayThrow;
59 // Iterate over loop instructions and compute safety info.
60 // Skip header as it has been computed and stored in HeaderMayThrow.
61 // The first block in loopinfo.Blocks is guaranteed to be the header.
62 assert(Header == *CurLoop->getBlocks().begin() &&
63 "First block must be header");
64 for (Loop::block_iterator BB = std::next(CurLoop->block_begin()),
65 BBE = CurLoop->block_end();
66 (BB != BBE) && !MayThrow; ++BB)
67 MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(*BB);
68
69 computeBlockColors(CurLoop);
70 }
71
blockMayThrow(const BasicBlock * BB) const72 bool ICFLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const {
73 return ICF.hasICF(BB);
74 }
75
anyBlockMayThrow() const76 bool ICFLoopSafetyInfo::anyBlockMayThrow() const {
77 return MayThrow;
78 }
79
computeLoopSafetyInfo(const Loop * CurLoop)80 void ICFLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) {
81 assert(CurLoop != nullptr && "CurLoop can't be null");
82 ICF.clear();
83 MW.clear();
84 MayThrow = false;
85 // Figure out the fact that at least one block may throw.
86 for (auto &BB : CurLoop->blocks())
87 if (ICF.hasICF(&*BB)) {
88 MayThrow = true;
89 break;
90 }
91 computeBlockColors(CurLoop);
92 }
93
insertInstructionTo(const Instruction * Inst,const BasicBlock * BB)94 void ICFLoopSafetyInfo::insertInstructionTo(const Instruction *Inst,
95 const BasicBlock *BB) {
96 ICF.insertInstructionTo(Inst, BB);
97 MW.insertInstructionTo(Inst, BB);
98 }
99
removeInstruction(const Instruction * Inst)100 void ICFLoopSafetyInfo::removeInstruction(const Instruction *Inst) {
101 ICF.removeInstruction(Inst);
102 MW.removeInstruction(Inst);
103 }
104
computeBlockColors(const Loop * CurLoop)105 void LoopSafetyInfo::computeBlockColors(const Loop *CurLoop) {
106 // Compute funclet colors if we might sink/hoist in a function with a funclet
107 // personality routine.
108 Function *Fn = CurLoop->getHeader()->getParent();
109 if (Fn->hasPersonalityFn())
110 if (Constant *PersonalityFn = Fn->getPersonalityFn())
111 if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn)))
112 BlockColors = colorEHFunclets(*Fn);
113 }
114
115 /// Return true if we can prove that the given ExitBlock is not reached on the
116 /// first iteration of the given loop. That is, the backedge of the loop must
117 /// be executed before the ExitBlock is executed in any dynamic execution trace.
CanProveNotTakenFirstIteration(const BasicBlock * ExitBlock,const DominatorTree * DT,const Loop * CurLoop)118 static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock,
119 const DominatorTree *DT,
120 const Loop *CurLoop) {
121 auto *CondExitBlock = ExitBlock->getSinglePredecessor();
122 if (!CondExitBlock)
123 // expect unique exits
124 return false;
125 assert(CurLoop->contains(CondExitBlock) && "meaning of exit block");
126 auto *BI = dyn_cast<BranchInst>(CondExitBlock->getTerminator());
127 if (!BI || !BI->isConditional())
128 return false;
129 // If condition is constant and false leads to ExitBlock then we always
130 // execute the true branch.
131 if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition()))
132 return BI->getSuccessor(Cond->getZExtValue() ? 1 : 0) == ExitBlock;
133 auto *Cond = dyn_cast<CmpInst>(BI->getCondition());
134 if (!Cond)
135 return false;
136 // todo: this would be a lot more powerful if we used scev, but all the
137 // plumbing is currently missing to pass a pointer in from the pass
138 // Check for cmp (phi [x, preheader] ...), y where (pred x, y is known
139 auto *LHS = dyn_cast<PHINode>(Cond->getOperand(0));
140 auto *RHS = Cond->getOperand(1);
141 if (!LHS || LHS->getParent() != CurLoop->getHeader())
142 return false;
143 auto DL = ExitBlock->getModule()->getDataLayout();
144 auto *IVStart = LHS->getIncomingValueForBlock(CurLoop->getLoopPreheader());
145 auto *SimpleValOrNull = SimplifyCmpInst(Cond->getPredicate(),
146 IVStart, RHS,
147 {DL, /*TLI*/ nullptr,
148 DT, /*AC*/ nullptr, BI});
149 auto *SimpleCst = dyn_cast_or_null<Constant>(SimpleValOrNull);
150 if (!SimpleCst)
151 return false;
152 if (ExitBlock == BI->getSuccessor(0))
153 return SimpleCst->isZeroValue();
154 assert(ExitBlock == BI->getSuccessor(1) && "implied by above");
155 return SimpleCst->isAllOnesValue();
156 }
157
158 /// Collect all blocks from \p CurLoop which lie on all possible paths from
159 /// the header of \p CurLoop (inclusive) to BB (exclusive) into the set
160 /// \p Predecessors. If \p BB is the header, \p Predecessors will be empty.
collectTransitivePredecessors(const Loop * CurLoop,const BasicBlock * BB,SmallPtrSetImpl<const BasicBlock * > & Predecessors)161 static void collectTransitivePredecessors(
162 const Loop *CurLoop, const BasicBlock *BB,
163 SmallPtrSetImpl<const BasicBlock *> &Predecessors) {
164 assert(Predecessors.empty() && "Garbage in predecessors set?");
165 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
166 if (BB == CurLoop->getHeader())
167 return;
168 SmallVector<const BasicBlock *, 4> WorkList;
169 for (auto *Pred : predecessors(BB)) {
170 Predecessors.insert(Pred);
171 WorkList.push_back(Pred);
172 }
173 while (!WorkList.empty()) {
174 auto *Pred = WorkList.pop_back_val();
175 assert(CurLoop->contains(Pred) && "Should only reach loop blocks!");
176 // We are not interested in backedges and we don't want to leave loop.
177 if (Pred == CurLoop->getHeader())
178 continue;
179 // TODO: If BB lies in an inner loop of CurLoop, this will traverse over all
180 // blocks of this inner loop, even those that are always executed AFTER the
181 // BB. It may make our analysis more conservative than it could be, see test
182 // @nested and @nested_no_throw in test/Analysis/MustExecute/loop-header.ll.
183 // We can ignore backedge of all loops containing BB to get a sligtly more
184 // optimistic result.
185 for (auto *PredPred : predecessors(Pred))
186 if (Predecessors.insert(PredPred).second)
187 WorkList.push_back(PredPred);
188 }
189 }
190
allLoopPathsLeadToBlock(const Loop * CurLoop,const BasicBlock * BB,const DominatorTree * DT) const191 bool LoopSafetyInfo::allLoopPathsLeadToBlock(const Loop *CurLoop,
192 const BasicBlock *BB,
193 const DominatorTree *DT) const {
194 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
195
196 // Fast path: header is always reached once the loop is entered.
197 if (BB == CurLoop->getHeader())
198 return true;
199
200 // Collect all transitive predecessors of BB in the same loop. This set will
201 // be a subset of the blocks within the loop.
202 SmallPtrSet<const BasicBlock *, 4> Predecessors;
203 collectTransitivePredecessors(CurLoop, BB, Predecessors);
204
205 // Make sure that all successors of, all predecessors of BB which are not
206 // dominated by BB, are either:
207 // 1) BB,
208 // 2) Also predecessors of BB,
209 // 3) Exit blocks which are not taken on 1st iteration.
210 // Memoize blocks we've already checked.
211 SmallPtrSet<const BasicBlock *, 4> CheckedSuccessors;
212 for (auto *Pred : Predecessors) {
213 // Predecessor block may throw, so it has a side exit.
214 if (blockMayThrow(Pred))
215 return false;
216
217 // BB dominates Pred, so if Pred runs, BB must run.
218 // This is true when Pred is a loop latch.
219 if (DT->dominates(BB, Pred))
220 continue;
221
222 for (auto *Succ : successors(Pred))
223 if (CheckedSuccessors.insert(Succ).second &&
224 Succ != BB && !Predecessors.count(Succ))
225 // By discharging conditions that are not executed on the 1st iteration,
226 // we guarantee that *at least* on the first iteration all paths from
227 // header that *may* execute will lead us to the block of interest. So
228 // that if we had virtually peeled one iteration away, in this peeled
229 // iteration the set of predecessors would contain only paths from
230 // header to BB without any exiting edges that may execute.
231 //
232 // TODO: We only do it for exiting edges currently. We could use the
233 // same function to skip some of the edges within the loop if we know
234 // that they will not be taken on the 1st iteration.
235 //
236 // TODO: If we somehow know the number of iterations in loop, the same
237 // check may be done for any arbitrary N-th iteration as long as N is
238 // not greater than minimum number of iterations in this loop.
239 if (CurLoop->contains(Succ) ||
240 !CanProveNotTakenFirstIteration(Succ, DT, CurLoop))
241 return false;
242 }
243
244 // All predecessors can only lead us to BB.
245 return true;
246 }
247
248 /// Returns true if the instruction in a loop is guaranteed to execute at least
249 /// once.
isGuaranteedToExecute(const Instruction & Inst,const DominatorTree * DT,const Loop * CurLoop) const250 bool SimpleLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst,
251 const DominatorTree *DT,
252 const Loop *CurLoop) const {
253 // If the instruction is in the header block for the loop (which is very
254 // common), it is always guaranteed to dominate the exit blocks. Since this
255 // is a common case, and can save some work, check it now.
256 if (Inst.getParent() == CurLoop->getHeader())
257 // If there's a throw in the header block, we can't guarantee we'll reach
258 // Inst unless we can prove that Inst comes before the potential implicit
259 // exit. At the moment, we use a (cheap) hack for the common case where
260 // the instruction of interest is the first one in the block.
261 return !HeaderMayThrow ||
262 Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
263
264 // If there is a path from header to exit or latch that doesn't lead to our
265 // instruction's block, return false.
266 return allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT);
267 }
268
isGuaranteedToExecute(const Instruction & Inst,const DominatorTree * DT,const Loop * CurLoop) const269 bool ICFLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst,
270 const DominatorTree *DT,
271 const Loop *CurLoop) const {
272 return !ICF.isDominatedByICFIFromSameBlock(&Inst) &&
273 allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT);
274 }
275
doesNotWriteMemoryBefore(const BasicBlock * BB,const Loop * CurLoop) const276 bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const BasicBlock *BB,
277 const Loop *CurLoop) const {
278 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
279
280 // Fast path: there are no instructions before header.
281 if (BB == CurLoop->getHeader())
282 return true;
283
284 // Collect all transitive predecessors of BB in the same loop. This set will
285 // be a subset of the blocks within the loop.
286 SmallPtrSet<const BasicBlock *, 4> Predecessors;
287 collectTransitivePredecessors(CurLoop, BB, Predecessors);
288 // Find if there any instruction in either predecessor that could write
289 // to memory.
290 for (auto *Pred : Predecessors)
291 if (MW.mayWriteToMemory(Pred))
292 return false;
293 return true;
294 }
295
doesNotWriteMemoryBefore(const Instruction & I,const Loop * CurLoop) const296 bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const Instruction &I,
297 const Loop *CurLoop) const {
298 auto *BB = I.getParent();
299 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
300 return !MW.isDominatedByMemoryWriteFromSameBlock(&I) &&
301 doesNotWriteMemoryBefore(BB, CurLoop);
302 }
303
304 namespace {
305 struct MustExecutePrinter : public FunctionPass {
306
307 static char ID; // Pass identification, replacement for typeid
MustExecutePrinter__anond7bb9b360111::MustExecutePrinter308 MustExecutePrinter() : FunctionPass(ID) {
309 initializeMustExecutePrinterPass(*PassRegistry::getPassRegistry());
310 }
getAnalysisUsage__anond7bb9b360111::MustExecutePrinter311 void getAnalysisUsage(AnalysisUsage &AU) const override {
312 AU.setPreservesAll();
313 AU.addRequired<DominatorTreeWrapperPass>();
314 AU.addRequired<LoopInfoWrapperPass>();
315 }
316 bool runOnFunction(Function &F) override;
317 };
318 struct MustBeExecutedContextPrinter : public ModulePass {
319 static char ID;
320
MustBeExecutedContextPrinter__anond7bb9b360111::MustBeExecutedContextPrinter321 MustBeExecutedContextPrinter() : ModulePass(ID) {
322 initializeMustBeExecutedContextPrinterPass(
323 *PassRegistry::getPassRegistry());
324 }
getAnalysisUsage__anond7bb9b360111::MustBeExecutedContextPrinter325 void getAnalysisUsage(AnalysisUsage &AU) const override {
326 AU.setPreservesAll();
327 }
328 bool runOnModule(Module &M) override;
329 };
330 }
331
332 char MustExecutePrinter::ID = 0;
333 INITIALIZE_PASS_BEGIN(MustExecutePrinter, "print-mustexecute",
334 "Instructions which execute on loop entry", false, true)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)335 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
336 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
337 INITIALIZE_PASS_END(MustExecutePrinter, "print-mustexecute",
338 "Instructions which execute on loop entry", false, true)
339
340 FunctionPass *llvm::createMustExecutePrinter() {
341 return new MustExecutePrinter();
342 }
343
344 char MustBeExecutedContextPrinter::ID = 0;
345 INITIALIZE_PASS_BEGIN(MustBeExecutedContextPrinter,
346 "print-must-be-executed-contexts",
347 "print the must-be-executed-context for all instructions",
348 false, true)
INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)349 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
350 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
351 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
352 INITIALIZE_PASS_END(MustBeExecutedContextPrinter,
353 "print-must-be-executed-contexts",
354 "print the must-be-executed-context for all instructions",
355 false, true)
356
357 ModulePass *llvm::createMustBeExecutedContextPrinter() {
358 return new MustBeExecutedContextPrinter();
359 }
360
runOnModule(Module & M)361 bool MustBeExecutedContextPrinter::runOnModule(Module &M) {
362 // We provide non-PM analysis here because the old PM doesn't like to query
363 // function passes from a module pass.
364 SmallVector<std::unique_ptr<PostDominatorTree>, 8> PDTs;
365 SmallVector<std::unique_ptr<DominatorTree>, 8> DTs;
366 SmallVector<std::unique_ptr<LoopInfo>, 8> LIs;
367
368 GetterTy<LoopInfo> LIGetter = [&](const Function &F) {
369 DTs.push_back(std::make_unique<DominatorTree>(const_cast<Function &>(F)));
370 LIs.push_back(std::make_unique<LoopInfo>(*DTs.back()));
371 return LIs.back().get();
372 };
373 GetterTy<DominatorTree> DTGetter = [&](const Function &F) {
374 DTs.push_back(std::make_unique<DominatorTree>(const_cast<Function&>(F)));
375 return DTs.back().get();
376 };
377 GetterTy<PostDominatorTree> PDTGetter = [&](const Function &F) {
378 PDTs.push_back(
379 std::make_unique<PostDominatorTree>(const_cast<Function &>(F)));
380 return PDTs.back().get();
381 };
382 MustBeExecutedContextExplorer Explorer(
383 /* ExploreInterBlock */ true,
384 /* ExploreCFGForward */ true,
385 /* ExploreCFGBackward */ true, LIGetter, DTGetter, PDTGetter);
386
387 for (Function &F : M) {
388 for (Instruction &I : instructions(F)) {
389 dbgs() << "-- Explore context of: " << I << "\n";
390 for (const Instruction *CI : Explorer.range(&I))
391 dbgs() << " [F: " << CI->getFunction()->getName() << "] " << *CI
392 << "\n";
393 }
394 }
395
396 return false;
397 }
398
isMustExecuteIn(const Instruction & I,Loop * L,DominatorTree * DT)399 static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) {
400 // TODO: merge these two routines. For the moment, we display the best
401 // result obtained by *either* implementation. This is a bit unfair since no
402 // caller actually gets the full power at the moment.
403 SimpleLoopSafetyInfo LSI;
404 LSI.computeLoopSafetyInfo(L);
405 return LSI.isGuaranteedToExecute(I, DT, L) ||
406 isGuaranteedToExecuteForEveryIteration(&I, L);
407 }
408
409 namespace {
410 /// An assembly annotator class to print must execute information in
411 /// comments.
412 class MustExecuteAnnotatedWriter : public AssemblyAnnotationWriter {
413 DenseMap<const Value*, SmallVector<Loop*, 4> > MustExec;
414
415 public:
MustExecuteAnnotatedWriter(const Function & F,DominatorTree & DT,LoopInfo & LI)416 MustExecuteAnnotatedWriter(const Function &F,
417 DominatorTree &DT, LoopInfo &LI) {
418 for (auto &I: instructions(F)) {
419 Loop *L = LI.getLoopFor(I.getParent());
420 while (L) {
421 if (isMustExecuteIn(I, L, &DT)) {
422 MustExec[&I].push_back(L);
423 }
424 L = L->getParentLoop();
425 };
426 }
427 }
MustExecuteAnnotatedWriter(const Module & M,DominatorTree & DT,LoopInfo & LI)428 MustExecuteAnnotatedWriter(const Module &M,
429 DominatorTree &DT, LoopInfo &LI) {
430 for (auto &F : M)
431 for (auto &I: instructions(F)) {
432 Loop *L = LI.getLoopFor(I.getParent());
433 while (L) {
434 if (isMustExecuteIn(I, L, &DT)) {
435 MustExec[&I].push_back(L);
436 }
437 L = L->getParentLoop();
438 };
439 }
440 }
441
442
printInfoComment(const Value & V,formatted_raw_ostream & OS)443 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
444 if (!MustExec.count(&V))
445 return;
446
447 const auto &Loops = MustExec.lookup(&V);
448 const auto NumLoops = Loops.size();
449 if (NumLoops > 1)
450 OS << " ; (mustexec in " << NumLoops << " loops: ";
451 else
452 OS << " ; (mustexec in: ";
453
454 bool first = true;
455 for (const Loop *L : Loops) {
456 if (!first)
457 OS << ", ";
458 first = false;
459 OS << L->getHeader()->getName();
460 }
461 OS << ")";
462 }
463 };
464 } // namespace
465
runOnFunction(Function & F)466 bool MustExecutePrinter::runOnFunction(Function &F) {
467 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
468 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
469
470 MustExecuteAnnotatedWriter Writer(F, DT, LI);
471 F.print(dbgs(), &Writer);
472
473 return false;
474 }
475
476 /// Return true if \p L might be an endless loop.
maybeEndlessLoop(const Loop & L)477 static bool maybeEndlessLoop(const Loop &L) {
478 if (L.getHeader()->getParent()->hasFnAttribute(Attribute::WillReturn))
479 return false;
480 // TODO: Actually try to prove it is not.
481 // TODO: If maybeEndlessLoop is going to be expensive, cache it.
482 return true;
483 }
484
mayContainIrreducibleControl(const Function & F,const LoopInfo * LI)485 bool llvm::mayContainIrreducibleControl(const Function &F, const LoopInfo *LI) {
486 if (!LI)
487 return false;
488 using RPOTraversal = ReversePostOrderTraversal<const Function *>;
489 RPOTraversal FuncRPOT(&F);
490 return containsIrreducibleCFG<const BasicBlock *, const RPOTraversal,
491 const LoopInfo>(FuncRPOT, *LI);
492 }
493
494 /// Lookup \p Key in \p Map and return the result, potentially after
495 /// initializing the optional through \p Fn(\p args).
496 template <typename K, typename V, typename FnTy, typename... ArgsTy>
getOrCreateCachedOptional(K Key,DenseMap<K,Optional<V>> & Map,FnTy && Fn,ArgsTy &&...args)497 static V getOrCreateCachedOptional(K Key, DenseMap<K, Optional<V>> &Map,
498 FnTy &&Fn, ArgsTy&&... args) {
499 Optional<V> &OptVal = Map[Key];
500 if (!OptVal.hasValue())
501 OptVal = Fn(std::forward<ArgsTy>(args)...);
502 return OptVal.getValue();
503 }
504
505 const BasicBlock *
findForwardJoinPoint(const BasicBlock * InitBB)506 MustBeExecutedContextExplorer::findForwardJoinPoint(const BasicBlock *InitBB) {
507 const LoopInfo *LI = LIGetter(*InitBB->getParent());
508 const PostDominatorTree *PDT = PDTGetter(*InitBB->getParent());
509
510 LLVM_DEBUG(dbgs() << "\tFind forward join point for " << InitBB->getName()
511 << (LI ? " [LI]" : "") << (PDT ? " [PDT]" : ""));
512
513 const Function &F = *InitBB->getParent();
514 const Loop *L = LI ? LI->getLoopFor(InitBB) : nullptr;
515 const BasicBlock *HeaderBB = L ? L->getHeader() : InitBB;
516 bool WillReturnAndNoThrow = (F.hasFnAttribute(Attribute::WillReturn) ||
517 (L && !maybeEndlessLoop(*L))) &&
518 F.doesNotThrow();
519 LLVM_DEBUG(dbgs() << (L ? " [in loop]" : "")
520 << (WillReturnAndNoThrow ? " [WillReturn] [NoUnwind]" : "")
521 << "\n");
522
523 // Determine the adjacent blocks in the given direction but exclude (self)
524 // loops under certain circumstances.
525 SmallVector<const BasicBlock *, 8> Worklist;
526 for (const BasicBlock *SuccBB : successors(InitBB)) {
527 bool IsLatch = SuccBB == HeaderBB;
528 // Loop latches are ignored in forward propagation if the loop cannot be
529 // endless and may not throw: control has to go somewhere.
530 if (!WillReturnAndNoThrow || !IsLatch)
531 Worklist.push_back(SuccBB);
532 }
533 LLVM_DEBUG(dbgs() << "\t\t#Worklist: " << Worklist.size() << "\n");
534
535 // If there are no other adjacent blocks, there is no join point.
536 if (Worklist.empty())
537 return nullptr;
538
539 // If there is one adjacent block, it is the join point.
540 if (Worklist.size() == 1)
541 return Worklist[0];
542
543 // Try to determine a join block through the help of the post-dominance
544 // tree. If no tree was provided, we perform simple pattern matching for one
545 // block conditionals and one block loops only.
546 const BasicBlock *JoinBB = nullptr;
547 if (PDT)
548 if (const auto *InitNode = PDT->getNode(InitBB))
549 if (const auto *IDomNode = InitNode->getIDom())
550 JoinBB = IDomNode->getBlock();
551
552 if (!JoinBB && Worklist.size() == 2) {
553 const BasicBlock *Succ0 = Worklist[0];
554 const BasicBlock *Succ1 = Worklist[1];
555 const BasicBlock *Succ0UniqueSucc = Succ0->getUniqueSuccessor();
556 const BasicBlock *Succ1UniqueSucc = Succ1->getUniqueSuccessor();
557 if (Succ0UniqueSucc == InitBB) {
558 // InitBB -> Succ0 -> InitBB
559 // InitBB -> Succ1 = JoinBB
560 JoinBB = Succ1;
561 } else if (Succ1UniqueSucc == InitBB) {
562 // InitBB -> Succ1 -> InitBB
563 // InitBB -> Succ0 = JoinBB
564 JoinBB = Succ0;
565 } else if (Succ0 == Succ1UniqueSucc) {
566 // InitBB -> Succ0 = JoinBB
567 // InitBB -> Succ1 -> Succ0 = JoinBB
568 JoinBB = Succ0;
569 } else if (Succ1 == Succ0UniqueSucc) {
570 // InitBB -> Succ0 -> Succ1 = JoinBB
571 // InitBB -> Succ1 = JoinBB
572 JoinBB = Succ1;
573 } else if (Succ0UniqueSucc == Succ1UniqueSucc) {
574 // InitBB -> Succ0 -> JoinBB
575 // InitBB -> Succ1 -> JoinBB
576 JoinBB = Succ0UniqueSucc;
577 }
578 }
579
580 if (!JoinBB && L)
581 JoinBB = L->getUniqueExitBlock();
582
583 if (!JoinBB)
584 return nullptr;
585
586 LLVM_DEBUG(dbgs() << "\t\tJoin block candidate: " << JoinBB->getName() << "\n");
587
588 // In forward direction we check if control will for sure reach JoinBB from
589 // InitBB, thus it can not be "stopped" along the way. Ways to "stop" control
590 // are: infinite loops and instructions that do not necessarily transfer
591 // execution to their successor. To check for them we traverse the CFG from
592 // the adjacent blocks to the JoinBB, looking at all intermediate blocks.
593
594 // If we know the function is "will-return" and "no-throw" there is no need
595 // for futher checks.
596 if (!F.hasFnAttribute(Attribute::WillReturn) || !F.doesNotThrow()) {
597
598 auto BlockTransfersExecutionToSuccessor = [](const BasicBlock *BB) {
599 return isGuaranteedToTransferExecutionToSuccessor(BB);
600 };
601
602 SmallPtrSet<const BasicBlock *, 16> Visited;
603 while (!Worklist.empty()) {
604 const BasicBlock *ToBB = Worklist.pop_back_val();
605 if (ToBB == JoinBB)
606 continue;
607
608 // Make sure all loops in-between are finite.
609 if (!Visited.insert(ToBB).second) {
610 if (!F.hasFnAttribute(Attribute::WillReturn)) {
611 if (!LI)
612 return nullptr;
613
614 bool MayContainIrreducibleControl = getOrCreateCachedOptional(
615 &F, IrreducibleControlMap, mayContainIrreducibleControl, F, LI);
616 if (MayContainIrreducibleControl)
617 return nullptr;
618
619 const Loop *L = LI->getLoopFor(ToBB);
620 if (L && maybeEndlessLoop(*L))
621 return nullptr;
622 }
623
624 continue;
625 }
626
627 // Make sure the block has no instructions that could stop control
628 // transfer.
629 bool TransfersExecution = getOrCreateCachedOptional(
630 ToBB, BlockTransferMap, BlockTransfersExecutionToSuccessor, ToBB);
631 if (!TransfersExecution)
632 return nullptr;
633
634 for (const BasicBlock *AdjacentBB : successors(ToBB))
635 Worklist.push_back(AdjacentBB);
636 }
637 }
638
639 LLVM_DEBUG(dbgs() << "\tJoin block: " << JoinBB->getName() << "\n");
640 return JoinBB;
641 }
642 const BasicBlock *
findBackwardJoinPoint(const BasicBlock * InitBB)643 MustBeExecutedContextExplorer::findBackwardJoinPoint(const BasicBlock *InitBB) {
644 const LoopInfo *LI = LIGetter(*InitBB->getParent());
645 const DominatorTree *DT = DTGetter(*InitBB->getParent());
646 LLVM_DEBUG(dbgs() << "\tFind backward join point for " << InitBB->getName()
647 << (LI ? " [LI]" : "") << (DT ? " [DT]" : ""));
648
649 // Try to determine a join block through the help of the dominance tree. If no
650 // tree was provided, we perform simple pattern matching for one block
651 // conditionals only.
652 if (DT)
653 if (const auto *InitNode = DT->getNode(InitBB))
654 if (const auto *IDomNode = InitNode->getIDom())
655 return IDomNode->getBlock();
656
657 const Loop *L = LI ? LI->getLoopFor(InitBB) : nullptr;
658 const BasicBlock *HeaderBB = L ? L->getHeader() : nullptr;
659
660 // Determine the predecessor blocks but ignore backedges.
661 SmallVector<const BasicBlock *, 8> Worklist;
662 for (const BasicBlock *PredBB : predecessors(InitBB)) {
663 bool IsBackedge =
664 (PredBB == InitBB) || (HeaderBB == InitBB && L->contains(PredBB));
665 // Loop backedges are ignored in backwards propagation: control has to come
666 // from somewhere.
667 if (!IsBackedge)
668 Worklist.push_back(PredBB);
669 }
670
671 // If there are no other predecessor blocks, there is no join point.
672 if (Worklist.empty())
673 return nullptr;
674
675 // If there is one predecessor block, it is the join point.
676 if (Worklist.size() == 1)
677 return Worklist[0];
678
679 const BasicBlock *JoinBB = nullptr;
680 if (Worklist.size() == 2) {
681 const BasicBlock *Pred0 = Worklist[0];
682 const BasicBlock *Pred1 = Worklist[1];
683 const BasicBlock *Pred0UniquePred = Pred0->getUniquePredecessor();
684 const BasicBlock *Pred1UniquePred = Pred1->getUniquePredecessor();
685 if (Pred0 == Pred1UniquePred) {
686 // InitBB <- Pred0 = JoinBB
687 // InitBB <- Pred1 <- Pred0 = JoinBB
688 JoinBB = Pred0;
689 } else if (Pred1 == Pred0UniquePred) {
690 // InitBB <- Pred0 <- Pred1 = JoinBB
691 // InitBB <- Pred1 = JoinBB
692 JoinBB = Pred1;
693 } else if (Pred0UniquePred == Pred1UniquePred) {
694 // InitBB <- Pred0 <- JoinBB
695 // InitBB <- Pred1 <- JoinBB
696 JoinBB = Pred0UniquePred;
697 }
698 }
699
700 if (!JoinBB && L)
701 JoinBB = L->getHeader();
702
703 // In backwards direction there is no need to show termination of previous
704 // instructions. If they do not terminate, the code afterward is dead, making
705 // any information/transformation correct anyway.
706 return JoinBB;
707 }
708
709 const Instruction *
getMustBeExecutedNextInstruction(MustBeExecutedIterator & It,const Instruction * PP)710 MustBeExecutedContextExplorer::getMustBeExecutedNextInstruction(
711 MustBeExecutedIterator &It, const Instruction *PP) {
712 if (!PP)
713 return PP;
714 LLVM_DEBUG(dbgs() << "Find next instruction for " << *PP << "\n");
715
716 // If we explore only inside a given basic block we stop at terminators.
717 if (!ExploreInterBlock && PP->isTerminator()) {
718 LLVM_DEBUG(dbgs() << "\tReached terminator in intra-block mode, done\n");
719 return nullptr;
720 }
721
722 // If we do not traverse the call graph we check if we can make progress in
723 // the current function. First, check if the instruction is guaranteed to
724 // transfer execution to the successor.
725 bool TransfersExecution = isGuaranteedToTransferExecutionToSuccessor(PP);
726 if (!TransfersExecution)
727 return nullptr;
728
729 // If this is not a terminator we know that there is a single instruction
730 // after this one that is executed next if control is transfered. If not,
731 // we can try to go back to a call site we entered earlier. If none exists, we
732 // do not know any instruction that has to be executd next.
733 if (!PP->isTerminator()) {
734 const Instruction *NextPP = PP->getNextNode();
735 LLVM_DEBUG(dbgs() << "\tIntermediate instruction does transfer control\n");
736 return NextPP;
737 }
738
739 // Finally, we have to handle terminators, trivial ones first.
740 assert(PP->isTerminator() && "Expected a terminator!");
741
742 // A terminator without a successor is not handled yet.
743 if (PP->getNumSuccessors() == 0) {
744 LLVM_DEBUG(dbgs() << "\tUnhandled terminator\n");
745 return nullptr;
746 }
747
748 // A terminator with a single successor, we will continue at the beginning of
749 // that one.
750 if (PP->getNumSuccessors() == 1) {
751 LLVM_DEBUG(
752 dbgs() << "\tUnconditional terminator, continue with successor\n");
753 return &PP->getSuccessor(0)->front();
754 }
755
756 // Multiple successors mean we need to find the join point where control flow
757 // converges again. We use the findForwardJoinPoint helper function with
758 // information about the function and helper analyses, if available.
759 if (const BasicBlock *JoinBB = findForwardJoinPoint(PP->getParent()))
760 return &JoinBB->front();
761
762 LLVM_DEBUG(dbgs() << "\tNo join point found\n");
763 return nullptr;
764 }
765
766 const Instruction *
getMustBeExecutedPrevInstruction(MustBeExecutedIterator & It,const Instruction * PP)767 MustBeExecutedContextExplorer::getMustBeExecutedPrevInstruction(
768 MustBeExecutedIterator &It, const Instruction *PP) {
769 if (!PP)
770 return PP;
771
772 bool IsFirst = !(PP->getPrevNode());
773 LLVM_DEBUG(dbgs() << "Find next instruction for " << *PP
774 << (IsFirst ? " [IsFirst]" : "") << "\n");
775
776 // If we explore only inside a given basic block we stop at the first
777 // instruction.
778 if (!ExploreInterBlock && IsFirst) {
779 LLVM_DEBUG(dbgs() << "\tReached block front in intra-block mode, done\n");
780 return nullptr;
781 }
782
783 // The block and function that contains the current position.
784 const BasicBlock *PPBlock = PP->getParent();
785
786 // If we are inside a block we know what instruction was executed before, the
787 // previous one.
788 if (!IsFirst) {
789 const Instruction *PrevPP = PP->getPrevNode();
790 LLVM_DEBUG(
791 dbgs() << "\tIntermediate instruction, continue with previous\n");
792 // We did not enter a callee so we simply return the previous instruction.
793 return PrevPP;
794 }
795
796 // Finally, we have to handle the case where the program point is the first in
797 // a block but not in the function. We use the findBackwardJoinPoint helper
798 // function with information about the function and helper analyses, if
799 // available.
800 if (const BasicBlock *JoinBB = findBackwardJoinPoint(PPBlock))
801 return &JoinBB->back();
802
803 LLVM_DEBUG(dbgs() << "\tNo join point found\n");
804 return nullptr;
805 }
806
MustBeExecutedIterator(MustBeExecutedContextExplorer & Explorer,const Instruction * I)807 MustBeExecutedIterator::MustBeExecutedIterator(
808 MustBeExecutedContextExplorer &Explorer, const Instruction *I)
809 : Explorer(Explorer), CurInst(I) {
810 reset(I);
811 }
812
reset(const Instruction * I)813 void MustBeExecutedIterator::reset(const Instruction *I) {
814 Visited.clear();
815 resetInstruction(I);
816 }
817
resetInstruction(const Instruction * I)818 void MustBeExecutedIterator::resetInstruction(const Instruction *I) {
819 CurInst = I;
820 Head = Tail = nullptr;
821 Visited.insert({I, ExplorationDirection::FORWARD});
822 Visited.insert({I, ExplorationDirection::BACKWARD});
823 if (Explorer.ExploreCFGForward)
824 Head = I;
825 if (Explorer.ExploreCFGBackward)
826 Tail = I;
827 }
828
advance()829 const Instruction *MustBeExecutedIterator::advance() {
830 assert(CurInst && "Cannot advance an end iterator!");
831 Head = Explorer.getMustBeExecutedNextInstruction(*this, Head);
832 if (Head && Visited.insert({Head, ExplorationDirection ::FORWARD}).second)
833 return Head;
834 Head = nullptr;
835
836 Tail = Explorer.getMustBeExecutedPrevInstruction(*this, Tail);
837 if (Tail && Visited.insert({Tail, ExplorationDirection ::BACKWARD}).second)
838 return Tail;
839 Tail = nullptr;
840 return nullptr;
841 }
842
run(Function & F,FunctionAnalysisManager & AM)843 PreservedAnalyses MustExecutePrinterPass::run(Function &F,
844 FunctionAnalysisManager &AM) {
845 auto &LI = AM.getResult<LoopAnalysis>(F);
846 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
847
848 MustExecuteAnnotatedWriter Writer(F, DT, LI);
849 F.print(OS, &Writer);
850 return PreservedAnalyses::all();
851 }
852
853 PreservedAnalyses
run(Module & M,ModuleAnalysisManager & AM)854 MustBeExecutedContextPrinterPass::run(Module &M, ModuleAnalysisManager &AM) {
855 FunctionAnalysisManager &FAM =
856 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
857 GetterTy<const LoopInfo> LIGetter = [&](const Function &F) {
858 return &FAM.getResult<LoopAnalysis>(const_cast<Function &>(F));
859 };
860 GetterTy<const DominatorTree> DTGetter = [&](const Function &F) {
861 return &FAM.getResult<DominatorTreeAnalysis>(const_cast<Function &>(F));
862 };
863 GetterTy<const PostDominatorTree> PDTGetter = [&](const Function &F) {
864 return &FAM.getResult<PostDominatorTreeAnalysis>(const_cast<Function &>(F));
865 };
866
867 MustBeExecutedContextExplorer Explorer(
868 /* ExploreInterBlock */ true,
869 /* ExploreCFGForward */ true,
870 /* ExploreCFGBackward */ true, LIGetter, DTGetter, PDTGetter);
871
872 for (Function &F : M) {
873 for (Instruction &I : instructions(F)) {
874 OS << "-- Explore context of: " << I << "\n";
875 for (const Instruction *CI : Explorer.range(&I))
876 OS << " [F: " << CI->getFunction()->getName() << "] " << *CI << "\n";
877 }
878 }
879 return PreservedAnalyses::all();
880 }
881