CaptureTracking.cpp revision 88990248d3bfb2f265fcf27f8a032ac0eb14d09f
1//===--- CaptureTracking.cpp - Determine whether a pointer is captured ----===//
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//
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.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Analysis/CaptureTracking.h"
20using namespace llvm;
21
22namespace {
23  struct SimpleCaptureTracker {
24    explicit SimpleCaptureTracker(bool ReturnCaptures)
25      : ReturnCaptures(ReturnCaptures), Captured(false) {}
26
27    void tooManyUses() { Captured = true; }
28
29    bool shouldExplore(Use *U) { return true; }
30
31    bool captured(Instruction *I) {
32      if (isa<ReturnInst>(I) && !ReturnCaptures)
33	return false;
34
35      Captured = true;
36      return true;
37    }
38
39    bool ReturnCaptures;
40
41    bool Captured;
42  };
43}
44
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.
52bool 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.
58  (void)StoreCaptures;
59
60  SimpleCaptureTracker SCT(ReturnCaptures);
61  PointerMayBeCaptured(V, SCT);
62  return SCT.Captured;
63}
64