blob: 40a02735d1b7c51350e45738997c591abd572420 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- MustExecute.h - Is an instruction known to execute--------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// Contains a collection of routines for determining if a given instruction is
11/// guaranteed to execute if a given point in control flow is reached. The most
12/// common example is an instruction within a loop being provably executed if we
Andrew Scullcdfcccc2018-10-05 20:58:37 +010013/// branch to the header of it's containing loop.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010014///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_ANALYSIS_MUSTEXECUTE_H
18#define LLVM_ANALYSIS_MUSTEXECUTE_H
19
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/Analysis/EHPersonalities.h"
22#include "llvm/Analysis/LoopInfo.h"
23#include "llvm/IR/BasicBlock.h"
24#include "llvm/IR/Dominators.h"
25#include "llvm/IR/Instruction.h"
26
27namespace llvm {
28
29class Instruction;
30class DominatorTree;
31class Loop;
32
Andrew Scullcdfcccc2018-10-05 20:58:37 +010033/// Captures loop safety information.
Andrew Scull0372a572018-11-16 15:47:06 +000034/// It keep information for loop blocks may throw exception or otherwise
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010035/// exit abnormaly on any iteration of the loop which might actually execute
36/// at runtime. The primary way to consume this infromation is via
37/// isGuaranteedToExecute below, but some callers bailout or fallback to
38/// alternate reasoning if a loop contains any implicit control flow.
Andrew Scull0372a572018-11-16 15:47:06 +000039/// NOTE: LoopSafetyInfo contains cached information regarding loops and their
40/// particular blocks. This information is only dropped on invocation of
41/// computeLoopSafetyInfo. If the loop or any of its block is deleted, or if
42/// any thrower instructions have been added or removed from them, or if the
43/// control flow has changed, or in case of other meaningful modifications, the
44/// LoopSafetyInfo needs to be recomputed. If a meaningful modifications to the
45/// loop were made and the info wasn't recomputed properly, the behavior of all
46/// methods except for computeLoopSafetyInfo is undefined.
47class LoopSafetyInfo {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048 bool MayThrow = false; // The current loop contains an instruction which
49 // may throw.
50 bool HeaderMayThrow = false; // Same as previous, but specific to loop header
Andrew Scull0372a572018-11-16 15:47:06 +000051
52 /// Collect all blocks from \p CurLoop which lie on all possible paths from
53 /// the header of \p CurLoop (inclusive) to BB (exclusive) into the set
54 /// \p Predecessors. If \p BB is the header, \p Predecessors will be empty.
55 void collectTransitivePredecessors(
56 const Loop *CurLoop, const BasicBlock *BB,
57 SmallPtrSetImpl<const BasicBlock *> &Predecessors) const;
58
59public:
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060 // Used to update funclet bundle operands.
61 DenseMap<BasicBlock *, ColorVector> BlockColors;
62
Andrew Scull0372a572018-11-16 15:47:06 +000063 /// Returns true iff the header block of the loop for which this info is
64 /// calculated contains an instruction that may throw or otherwise exit
65 /// abnormally.
66 bool headerMayThrow() const;
67
68 /// Returns true iff any block of the loop for which this info is contains an
69 /// instruction that may throw or otherwise exit abnormally.
70 bool anyBlockMayThrow() const;
71
72 /// Return true if we must reach the block \p BB under assumption that the
73 /// loop \p CurLoop is entered and no instruction throws or otherwise exits
74 /// abnormally.
75 bool allLoopPathsLeadToBlock(const Loop *CurLoop, const BasicBlock *BB,
76 const DominatorTree *DT) const;
77
78 /// Computes safety information for a loop checks loop body & header for
79 /// the possibility of may throw exception, it takes LoopSafetyInfo and loop
80 /// as argument. Updates safety information in LoopSafetyInfo argument.
81 /// Note: This is defined to clear and reinitialize an already initialized
82 /// LoopSafetyInfo. Some callers rely on this fact.
83 void computeLoopSafetyInfo(Loop *);
84
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010085 LoopSafetyInfo() = default;
86};
87
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010088/// Returns true if the instruction in a loop is guaranteed to execute at least
89/// once (under the assumption that the loop is entered).
90bool isGuaranteedToExecute(const Instruction &Inst, const DominatorTree *DT,
91 const Loop *CurLoop,
92 const LoopSafetyInfo *SafetyInfo);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010093
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010094}
95
96#endif