LLVM 23.0.0git
CaptureTracking.h
Go to the documentation of this file.
1//===----- llvm/Analysis/CaptureTracking.h - Pointer capture ----*- C++ -*-===//
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 contains routines that help determine which pointers are captured.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ANALYSIS_CAPTURETRACKING_H
14#define LLVM_ANALYSIS_CAPTURETRACKING_H
15
16#include "llvm/ADT/DenseMap.h"
18#include "llvm/Support/ModRef.h"
19
20namespace llvm {
21
22 class Value;
23 class Use;
24 class CaptureInfo;
25 class DataLayout;
26 class Instruction;
27 class DominatorTree;
28 class LoopInfo;
29 class Function;
30 template <typename Fn> class function_ref;
31
32 /// getDefaultMaxUsesToExploreForCaptureTracking - Return default value of
33 /// the maximal number of uses to explore before giving up. It is used by
34 /// PointerMayBeCaptured family analysis.
36
37 /// PointerMayBeCaptured - Return true if this pointer value may be captured
38 /// by the enclosing function (which is required to exist). This routine can
39 /// be expensive, so consider caching the results. The boolean ReturnCaptures
40 /// specifies whether returning the value (or part of it) from the function
41 /// counts as capturing it or not.
42 /// MaxUsesToExplore specifies how many uses the analysis should explore for
43 /// one value before giving up due too "too many uses". If MaxUsesToExplore
44 /// is zero, a default value is assumed.
45 /// This function only considers captures of the passed value via its def-use
46 /// chain, without considering captures of values it may be based on, or
47 /// implicit captures such as for external globals.
48 LLVM_ABI bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures,
49 unsigned MaxUsesToExplore = 0);
50
51 /// Result of a PointerMayBeCaptured query, which includes the captured
52 /// components for both the case where return is considered a capture, and
53 /// where it isn't.
58
59 /// Return which components of the pointer may be captured. Only consider
60 /// components that are part of \p Mask. Once \p StopFn on the accumulated
61 /// components returns true, the traversal is aborted early. By default, this
62 /// happens when *any* of the components in \p Mask are captured.
63 /// This function only considers captures of the passed value via its def-use
64 /// chain, without considering captures of values it may be based on, or
65 /// implicit captures such as for external globals.
67 const Value *V, CaptureComponents Mask,
69 unsigned MaxUsesToExplore = 0);
70
71 /// PointerMayBeCapturedBefore - Return true if this pointer value may be
72 /// captured by the enclosing function (which is required to exist). If a
73 /// DominatorTree is provided, only captures which happen before the given
74 /// instruction are considered. This routine can be expensive, so consider
75 /// caching the results. The boolean ReturnCaptures specifies whether
76 /// returning the value (or part of it) from the function counts as capturing
77 /// it or not. Captures by the provided instruction are considered if the
78 /// final parameter is true.
79 /// MaxUsesToExplore specifies how many uses the analysis should explore for
80 /// one value before giving up due too "too many uses". If MaxUsesToExplore
81 /// is zero, a default value is assumed.
82 /// This function only considers captures of the passed value via its def-use
83 /// chain, without considering captures of values it may be based on, or
84 /// implicit captures such as for external globals.
85 LLVM_ABI bool PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures,
86 const Instruction *I,
87 const DominatorTree *DT,
88 bool IncludeI = false,
89 unsigned MaxUsesToExplore = 0,
90 const LoopInfo *LI = nullptr);
91
92 /// Return which components of the pointer may be captured on the path to
93 /// \p I. Only consider components that are part of \p Mask. Once \p StopFn
94 /// on the accumulated components returns true, the traversal is aborted
95 /// early. By default, this happens when *any* of the components in \p Mask
96 /// are captured.
97 /// This function only considers captures of the passed value via its def-use
98 /// chain, without considering captures of values it may be based on, or
99 /// implicit captures such as for external globals.
101 const Value *V, bool ReturnCaptures, const Instruction *I,
102 const DominatorTree *DT, bool IncludeI, CaptureComponents Mask,
104 const LoopInfo *LI = nullptr, unsigned MaxUsesToExplore = 0);
105
106 // Returns the 'earliest' instruction that captures \p V in \F, and which
107 // components may be captured (by any use, not necessarily the earliest one).
108 // An instruction A is considered earlier than instruction B, if A dominates
109 // B. If 2 escapes do not dominate each other, the terminator of the common
110 // dominator is chosen. If not all uses can be analyzed, the earliest escape
111 // is set to the first instruction in the function entry block. If \p V does
112 // not escape, nullptr is returned. Note that the caller of the function has
113 // to ensure that the instruction the result value is compared against is
114 // not in a cycle.
115 //
116 // Only consider components that are part of \p Mask.
117 LLVM_ABI std::pair<Instruction *, CaptureResult>
118 FindEarliestCapture(const Value *V, Function &F, const DominatorTree &DT,
119 CaptureComponents Mask, unsigned MaxUsesToExplore = 0);
120
121 /// Capture information for a specific Use.
123 /// Components captured by this use.
125 /// Components captured by the return value of the user of this Use.
127
131
135
136 bool isPassthrough() const {
138 }
139
140 operator CaptureComponents() const { return UseCC | ResultCC; }
141 };
142
143 /// This callback is used in conjunction with PointerMayBeCaptured. In
144 /// addition to the interface here, you'll need to provide your own getters
145 /// to see whether anything was captured.
147 /// Action returned from captures().
148 enum Action {
149 /// Stop the traversal.
151 /// Continue traversal, and also follow the return value of the user if
152 /// it has additional capture components (that is, if it has capture
153 /// components in Ret that are not part of Other).
155 /// Continue traversal, but do not follow the return value of the user,
156 /// even if it has additional capture components. Should only be used if
157 /// captures() has already taken the potential return captures into
158 /// account.
160 };
161
163
164 /// tooManyUses - The depth of traversal has breached a limit. There may be
165 /// capturing instructions that will not be passed into captured().
166 virtual void tooManyUses() = 0;
167
168 /// shouldExplore - This is the use of a value derived from the pointer.
169 /// To prune the search (ie., assume that none of its users could possibly
170 /// capture) return false. To search it, return true.
171 ///
172 /// U->getUser() is always an Instruction.
173 virtual bool shouldExplore(const Use *U);
174
175 /// Use U directly captures CI.UseCC and additionally CI.ResultCC
176 /// through the return value of the user of U.
177 ///
178 /// Return one of Stop, Continue or ContinueIgnoringReturn to control
179 /// further traversal.
180 virtual Action captured(const Use *U, UseCaptureInfo CI) = 0;
181 };
182
183 /// Determine what kind of capture behaviour \p U may exhibit.
184 ///
185 /// The returned UseCaptureInfo contains the components captured directly
186 /// by the use (UseCC) and the components captured through the return value
187 /// of the user (ResultCC).
188 ///
189 /// \p Base is the starting value of the capture analysis, which is
190 /// relevant for address_is_null captures.
192 const Value *Base);
193
194 /// PointerMayBeCaptured - Visit the value and the values derived from it and
195 /// find values which appear to be capturing the pointer value. This feeds
196 /// results into and is controlled by the CaptureTracker object.
197 /// MaxUsesToExplore specifies how many uses the analysis should explore for
198 /// one value before giving up due too "too many uses". If MaxUsesToExplore
199 /// is zero, a default value is assumed.
200 /// This function only considers captures of the passed value via its def-use
201 /// chain, without considering captures of values it may be based on, or
202 /// implicit captures such as for external globals.
203 LLVM_ABI void PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
204 unsigned MaxUsesToExplore = 0);
205} // end namespace llvm
206
207#endif
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Represents which components of the pointer may be captured in which location.
Definition ModRef.h:414
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI unsigned getDefaultMaxUsesToExploreForCaptureTracking()
getDefaultMaxUsesToExploreForCaptureTracking - Return default value of the maximal number of uses to ...
LLVM_ABI bool PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures, const Instruction *I, const DominatorTree *DT, bool IncludeI=false, unsigned MaxUsesToExplore=0, const LoopInfo *LI=nullptr)
PointerMayBeCapturedBefore - Return true if this pointer value may be captured by the enclosing funct...
CaptureComponents
Components of the pointer that may be captured.
Definition ModRef.h:365
LLVM_ABI bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
LLVM_ABI std::pair< Instruction *, CaptureResult > FindEarliestCapture(const Value *V, Function &F, const DominatorTree &DT, CaptureComponents Mask, unsigned MaxUsesToExplore=0)
bool capturesAnything(CaptureComponents CC)
Definition ModRef.h:379
LLVM_ABI UseCaptureInfo DetermineUseCaptureKind(const Use &U, const Value *Base)
Determine what kind of capture behaviour U may exhibit.
bool capturesNothing(CaptureComponents CC)
Definition ModRef.h:375
Result of a PointerMayBeCaptured query, which includes the captured components for both the case wher...
CaptureComponents WithoutRet
CaptureComponents WithRet
This callback is used in conjunction with PointerMayBeCaptured.
virtual bool shouldExplore(const Use *U)
shouldExplore - This is the use of a value derived from the pointer.
Action
Action returned from captures().
@ ContinueIgnoringReturn
Continue traversal, but do not follow the return value of the user, even if it has additional capture...
@ Continue
Continue traversal, and also follow the return value of the user if it has additional capture compone...
@ Stop
Stop the traversal.
virtual Action captured(const Use *U, UseCaptureInfo CI)=0
Use U directly captures CI.UseCC and additionally CI.ResultCC through the return value of the user of...
virtual void tooManyUses()=0
tooManyUses - The depth of traversal has breached a limit.
virtual ~CaptureTracker()
Capture information for a specific Use.
CaptureComponents UseCC
Components captured by this use.
UseCaptureInfo(CaptureComponents UseCC, CaptureComponents ResultCC=CaptureComponents::None)
CaptureComponents ResultCC
Components captured by the return value of the user of this Use.
static UseCaptureInfo passthrough()