blob: a84dafb5fbd7e4e4ae75b782ea48dacaf70443db [file] [log] [blame]
Duncan Sands8556d2a2009-01-18 12:19:30 +00001//===--- 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"
Duncan Sands8556d2a2009-01-18 12:19:30 +000020using namespace llvm;
21
Nick Lewycky88990242011-11-14 22:49:42 +000022namespace {
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}
Dan Gohman8456d602009-12-08 23:59:12 +000044
Duncan Sands8556d2a2009-01-18 12:19:30 +000045/// 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
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000049/// counts as capturing it or not. The boolean StoreCaptures specified whether
50/// storing the value (or part of it) into memory anywhere automatically
Duncan Sands8556d2a2009-01-18 12:19:30 +000051/// counts as capturing it or not.
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000052bool llvm::PointerMayBeCaptured(const Value *V,
53 bool ReturnCaptures, bool StoreCaptures) {
Nick Lewycky88990242011-11-14 22:49:42 +000054 // 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;
Duncan Sands8556d2a2009-01-18 12:19:30 +000059
Nick Lewycky88990242011-11-14 22:49:42 +000060 SimpleCaptureTracker SCT(ReturnCaptures);
61 PointerMayBeCaptured(V, SCT);
62 return SCT.Captured;
Duncan Sands8556d2a2009-01-18 12:19:30 +000063}