1 //===--- CaptureTracking.cpp - Determine whether a pointer is captured ----===//
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 contains routines that help determine which pointers are captured.
11 // A pointer value is captured if the function makes a copy of any part of the
12 // pointer that outlives the call. Not being captured means, more or less, that
13 // the pointer is only dereferenced and not stored in a global. Returning part
14 // of the pointer as the function return value may or may not count as capturing
15 // the pointer, depending on the context.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Analysis/CaptureTracking.h"
23 struct SimpleCaptureTracker {
24 explicit SimpleCaptureTracker(bool ReturnCaptures)
25 : ReturnCaptures(ReturnCaptures), Captured(false) {}
27 void tooManyUses() { Captured = true; }
29 bool shouldExplore(Use *U) { return true; }
31 bool captured(Instruction *I) {
32 if (isa<ReturnInst>(I) && !ReturnCaptures)
45 /// PointerMayBeCaptured - Return true if this pointer value may be captured
46 /// by the enclosing function (which is required to exist). This routine can
47 /// be expensive, so consider caching the results. The boolean ReturnCaptures
48 /// specifies whether returning the value (or part of it) from the function
49 /// counts as capturing it or not. The boolean StoreCaptures specified whether
50 /// storing the value (or part of it) into memory anywhere automatically
51 /// counts as capturing it or not.
52 bool llvm::PointerMayBeCaptured(const Value *V,
53 bool ReturnCaptures, bool StoreCaptures) {
54 // TODO: If StoreCaptures is not true, we could do Fancy analysis
55 // to determine whether this store is not actually an escape point.
56 // In that case, BasicAliasAnalysis should be updated as well to
57 // take advantage of this.
60 SimpleCaptureTracker SCT(ReturnCaptures);
61 PointerMayBeCaptured(V, SCT);