1 //===- PtrUseVisitor.h - InstVisitors over a pointers uses ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file provides a collection of visitors which walk the (instruction)
11 /// uses of a pointer. These visitors all provide the same essential behavior
12 /// as an InstVisitor with similar template-based flexibility and
13 /// implementation strategies.
15 /// These can be used, for example, to quickly analyze the uses of an alloca,
16 /// global variable, or function argument.
18 /// FIXME: Provide a variant which doesn't track offsets and is cheaper.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_ANALYSIS_PTRUSEVISITOR_H
23 #define LLVM_ANALYSIS_PTRUSEVISITOR_H
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/DataLayout.h"
29 #include "llvm/InstVisitor.h"
30 #include "llvm/IntrinsicInst.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/GetElementPtrTypeIterator.h"
37 /// \brief Implementation of non-dependent functionality for \c PtrUseVisitor.
39 /// See \c PtrUseVisitor for the public interface and detailed comments about
40 /// usage. This class is just a helper base class which is not templated and
41 /// contains all common code to be shared between different instantiations of
43 class PtrUseVisitorBase {
45 /// \brief This class provides information about the result of a visit.
47 /// After walking all the users (recursively) of a pointer, the basic
48 /// infrastructure records some commonly useful information such as escape
49 /// analysis and whether the visit completed or aborted early.
52 PtrInfo() : AbortedInfo(0, false), EscapedInfo(0, false) {}
54 /// \brief Reset the pointer info, clearing all state.
56 AbortedInfo.setPointer(0);
57 AbortedInfo.setInt(false);
58 EscapedInfo.setPointer(0);
59 EscapedInfo.setInt(false);
62 /// \brief Did we abort the visit early?
63 bool isAborted() const { return AbortedInfo.getInt(); }
65 /// \brief Is the pointer escaped at some point?
66 bool isEscaped() const { return EscapedInfo.getInt(); }
68 /// \brief Get the instruction causing the visit to abort.
69 /// \returns a pointer to the instruction causing the abort if one is
70 /// available; otherwise returns null.
71 Instruction *getAbortingInst() const { return AbortedInfo.getPointer(); }
73 /// \brief Get the instruction causing the pointer to escape.
74 /// \returns a pointer to the instruction which escapes the pointer if one
75 /// is available; otherwise returns null.
76 Instruction *getEscapingInst() const { return EscapedInfo.getPointer(); }
78 /// \brief Mark the visit as aborted. Intended for use in a void return.
79 /// \param I The instruction which caused the visit to abort, if available.
80 void setAborted(Instruction *I = 0) {
81 AbortedInfo.setInt(true);
82 AbortedInfo.setPointer(I);
85 /// \brief Mark the pointer as escaped. Intended for use in a void return.
86 /// \param I The instruction which escapes the pointer, if available.
87 void setEscaped(Instruction *I = 0) {
88 EscapedInfo.setInt(true);
89 EscapedInfo.setPointer(I);
92 /// \brief Mark the pointer as escaped, and the visit as aborted. Intended
93 /// for use in a void return.
94 /// \param I The instruction which both escapes the pointer and aborts the
95 /// visit, if available.
96 void setEscapedAndAborted(Instruction *I = 0) {
102 PointerIntPair<Instruction *, 1, bool> AbortedInfo, EscapedInfo;
106 const DataLayout &DL;
108 /// \name Visitation infrastructure
111 /// \brief The info collected about the pointer being visited thus far.
114 /// \brief A struct of the data needed to visit a particular use.
116 /// This is used to maintain a worklist fo to-visit uses. This is used to
117 /// make the visit be iterative rather than recursive.
119 typedef PointerIntPair<Use *, 1, bool> UseAndIsOffsetKnownPair;
120 UseAndIsOffsetKnownPair UseAndIsOffsetKnown;
124 /// \brief The worklist of to-visit uses.
125 SmallVector<UseToVisit, 8> Worklist;
127 /// \brief A set of visited uses to break cycles in unreachable code.
128 SmallPtrSet<Use *, 8> VisitedUses;
133 /// \name Per-visit state
134 /// This state is reset for each instruction visited.
137 /// \brief The use currently being visited.
140 /// \brief True if we have a known constant offset for the use currently
144 /// \brief The constant offset of the use if that is known.
150 /// Note that the constructor is protected because this class must be a base
151 /// class, we can't create instances directly of this class.
152 PtrUseVisitorBase(const DataLayout &DL) : DL(DL) {}
154 /// \brief Enqueue the users of this instruction in the visit worklist.
156 /// This will visit the users with the same offset of the current visit
157 /// (including an unknown offset if that is the current state).
158 void enqueueUsers(Instruction &I);
160 /// \brief Walk the operands of a GEP and adjust the offset as appropriate.
162 /// This routine does the heavy lifting of the pointer walk by computing
163 /// offsets and looking through GEPs.
164 bool adjustOffsetForGEP(GetElementPtrInst &GEPI);
166 } // end namespace detail
168 /// \brief A base class for visitors over the uses of a pointer value.
170 /// Once constructed, a user can call \c visit on a pointer value, and this
171 /// will walk its uses and visit each instruction using an InstVisitor. It also
172 /// provides visit methods which will recurse through any pointer-to-pointer
173 /// transformations such as GEPs and bitcasts.
175 /// During the visit, the current Use* being visited is available to the
176 /// subclass, as well as the current offset from the original base pointer if
179 /// The recursive visit of uses is accomplished with a worklist, so the only
180 /// ordering guarantee is that an instruction is visited before any uses of it
181 /// are visited. Note that this does *not* mean before any of its users are
182 /// visited! This is because users can be visited multiple times due to
183 /// multiple, different uses of pointers derived from the same base.
185 /// A particular Use will only be visited once, but a User may be visited
186 /// multiple times, once per Use. This visits may notably have different
189 /// All visit methods on the underlying InstVisitor return a boolean. This
190 /// return short-circuits the visit, stopping it immediately.
192 /// FIXME: Generalize this for all values rather than just instructions.
193 template <typename DerivedT>
194 class PtrUseVisitor : protected InstVisitor<DerivedT>,
195 public detail::PtrUseVisitorBase {
196 friend class InstVisitor<DerivedT>;
197 typedef InstVisitor<DerivedT> Base;
200 PtrUseVisitor(const DataLayout &DL) : PtrUseVisitorBase(DL) {}
202 /// \brief Recursively visit the uses of the given pointer.
203 /// \returns An info struct about the pointer. See \c PtrInfo for details.
204 PtrInfo visitPtr(Instruction &I) {
205 // This must be a pointer type. Get an integer type suitable to hold
206 // offsets on this pointer.
207 // FIXME: Support a vector of pointers.
208 assert(I.getType()->isPointerTy());
209 IntegerType *IntPtrTy = cast<IntegerType>(DL.getIntPtrType(I.getType()));
210 IsOffsetKnown = true;
211 Offset = APInt(IntPtrTy->getBitWidth(), 0);
214 // Enqueue the uses of this pointer.
217 // Visit all the uses off the worklist until it is empty.
218 while (!Worklist.empty()) {
219 UseToVisit ToVisit = Worklist.pop_back_val();
220 U = ToVisit.UseAndIsOffsetKnown.getPointer();
221 IsOffsetKnown = ToVisit.UseAndIsOffsetKnown.getInt();
223 Offset = llvm_move(ToVisit.Offset);
225 Instruction *I = cast<Instruction>(U->getUser());
226 static_cast<DerivedT*>(this)->visit(I);
234 void visitStoreInst(StoreInst &SI) {
235 if (SI.getValueOperand() == U->get())
239 void visitBitCastInst(BitCastInst &BC) {
243 void visitPtrToIntInst(PtrToIntInst &I) {
247 void visitGetElementPtrInst(GetElementPtrInst &GEPI) {
248 if (GEPI.use_empty())
251 // If we can't walk the GEP, clear the offset.
252 if (!adjustOffsetForGEP(GEPI)) {
253 IsOffsetKnown = false;
257 // Enqueue the users now that the offset has been adjusted.
261 // No-op intrinsics which we know don't escape the pointer to to logic in
262 // some other function.
263 void visitDbgInfoIntrinsic(DbgInfoIntrinsic &I) {}
264 void visitMemIntrinsic(MemIntrinsic &I) {}
265 void visitIntrinsicInst(IntrinsicInst &II) {
266 switch (II.getIntrinsicID()) {
268 return Base::visitIntrinsicInst(II);
270 case Intrinsic::lifetime_start:
271 case Intrinsic::lifetime_end:
272 return; // No-op intrinsics.
276 // Generically, arguments to calls and invokes escape the pointer to some
277 // other function. Mark that.
278 void visitCallSite(CallSite CS) {
279 PI.setEscaped(CS.getInstruction());
280 Base::visitCallSite(CS);