Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 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" |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Nick Lewycky | 8899024 | 2011-11-14 22:49:42 +0000 | [diff] [blame^] | 22 | namespace { |
| 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 Gohman | 8456d60 | 2009-12-08 23:59:12 +0000 | [diff] [blame] | 44 | |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 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 |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 49 | /// counts as capturing it or not. The boolean StoreCaptures specified whether |
| 50 | /// storing the value (or part of it) into memory anywhere automatically |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 51 | /// counts as capturing it or not. |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 52 | bool llvm::PointerMayBeCaptured(const Value *V, |
| 53 | bool ReturnCaptures, bool StoreCaptures) { |
Nick Lewycky | 8899024 | 2011-11-14 22:49:42 +0000 | [diff] [blame^] | 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; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 59 | |
Nick Lewycky | 8899024 | 2011-11-14 22:49:42 +0000 | [diff] [blame^] | 60 | SimpleCaptureTracker SCT(ReturnCaptures); |
| 61 | PointerMayBeCaptured(V, SCT); |
| 62 | return SCT.Captured; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 63 | } |