LLVM  mainline
CaptureTracking.cpp
Go to the documentation of this file.
00001 //===--- CaptureTracking.cpp - Determine whether a pointer is captured ----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains routines that help determine which pointers are captured.
00011 // A pointer value is captured if the function makes a copy of any part of the
00012 // pointer that outlives the call.  Not being captured means, more or less, that
00013 // the pointer is only dereferenced and not stored in a global.  Returning part
00014 // of the pointer as the function return value may or may not count as capturing
00015 // the pointer, depending on the context.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "llvm/ADT/SmallSet.h"
00020 #include "llvm/ADT/SmallVector.h"
00021 #include "llvm/Analysis/AliasAnalysis.h"
00022 #include "llvm/Analysis/CFG.h"
00023 #include "llvm/Analysis/CaptureTracking.h"
00024 #include "llvm/Analysis/OrderedBasicBlock.h"
00025 #include "llvm/IR/CallSite.h"
00026 #include "llvm/IR/Constants.h"
00027 #include "llvm/IR/Dominators.h"
00028 #include "llvm/IR/Instructions.h"
00029 
00030 using namespace llvm;
00031 
00032 CaptureTracker::~CaptureTracker() {}
00033 
00034 bool CaptureTracker::shouldExplore(const Use *U) { return true; }
00035 
00036 namespace {
00037   struct SimpleCaptureTracker : public CaptureTracker {
00038     explicit SimpleCaptureTracker(bool ReturnCaptures)
00039       : ReturnCaptures(ReturnCaptures), Captured(false) {}
00040 
00041     void tooManyUses() override { Captured = true; }
00042 
00043     bool captured(const Use *U) override {
00044       if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures)
00045         return false;
00046 
00047       Captured = true;
00048       return true;
00049     }
00050 
00051     bool ReturnCaptures;
00052 
00053     bool Captured;
00054   };
00055 
00056   /// Only find pointer captures which happen before the given instruction. Uses
00057   /// the dominator tree to determine whether one instruction is before another.
00058   /// Only support the case where the Value is defined in the same basic block
00059   /// as the given instruction and the use.
00060   struct CapturesBefore : public CaptureTracker {
00061 
00062     CapturesBefore(bool ReturnCaptures, const Instruction *I, DominatorTree *DT,
00063                    bool IncludeI, OrderedBasicBlock *IC)
00064       : OrderedBB(IC), BeforeHere(I), DT(DT),
00065         ReturnCaptures(ReturnCaptures), IncludeI(IncludeI), Captured(false) {}
00066 
00067     void tooManyUses() override { Captured = true; }
00068 
00069     bool isSafeToPrune(Instruction *I) {
00070       BasicBlock *BB = I->getParent();
00071       // We explore this usage only if the usage can reach "BeforeHere".
00072       // If use is not reachable from entry, there is no need to explore.
00073       if (BeforeHere != I && !DT->isReachableFromEntry(BB))
00074         return true;
00075 
00076       // Compute the case where both instructions are inside the same basic
00077       // block. Since instructions in the same BB as BeforeHere are numbered in
00078       // 'OrderedBB', avoid using 'dominates' and 'isPotentiallyReachable'
00079       // which are very expensive for large basic blocks.
00080       if (BB == BeforeHere->getParent()) {
00081         // 'I' dominates 'BeforeHere' => not safe to prune.
00082         //
00083         // The value defined by an invoke dominates an instruction only
00084         // if it dominates every instruction in UseBB. A PHI is dominated only
00085         // if the instruction dominates every possible use in the UseBB. Since
00086         // UseBB == BB, avoid pruning.
00087         if (isa<InvokeInst>(BeforeHere) || isa<PHINode>(I) || I == BeforeHere)
00088           return false;
00089         if (!OrderedBB->dominates(BeforeHere, I))
00090           return false;
00091 
00092         // 'BeforeHere' comes before 'I', it's safe to prune if we also
00093         // guarantee that 'I' never reaches 'BeforeHere' through a back-edge or
00094         // by its successors, i.e, prune if:
00095         //
00096         //  (1) BB is an entry block or have no sucessors.
00097         //  (2) There's no path coming back through BB sucessors.
00098         if (BB == &BB->getParent()->getEntryBlock() ||
00099             !BB->getTerminator()->getNumSuccessors())
00100           return true;
00101 
00102         SmallVector<BasicBlock*, 32> Worklist;
00103         Worklist.append(succ_begin(BB), succ_end(BB));
00104         return !isPotentiallyReachableFromMany(Worklist, BB, DT);
00105       }
00106 
00107       // If the value is defined in the same basic block as use and BeforeHere,
00108       // there is no need to explore the use if BeforeHere dominates use.
00109       // Check whether there is a path from I to BeforeHere.
00110       if (BeforeHere != I && DT->dominates(BeforeHere, I) &&
00111           !isPotentiallyReachable(I, BeforeHere, DT))
00112         return true;
00113 
00114       return false;
00115     }
00116 
00117     bool shouldExplore(const Use *U) override {
00118       Instruction *I = cast<Instruction>(U->getUser());
00119 
00120       if (BeforeHere == I && !IncludeI)
00121         return false;
00122 
00123       if (isSafeToPrune(I))
00124         return false;
00125 
00126       return true;
00127     }
00128 
00129     bool captured(const Use *U) override {
00130       if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures)
00131         return false;
00132 
00133       if (!shouldExplore(U))
00134         return false;
00135 
00136       Captured = true;
00137       return true;
00138     }
00139 
00140     OrderedBasicBlock *OrderedBB;
00141     const Instruction *BeforeHere;
00142     DominatorTree *DT;
00143 
00144     bool ReturnCaptures;
00145     bool IncludeI;
00146 
00147     bool Captured;
00148   };
00149 }
00150 
00151 /// PointerMayBeCaptured - Return true if this pointer value may be captured
00152 /// by the enclosing function (which is required to exist).  This routine can
00153 /// be expensive, so consider caching the results.  The boolean ReturnCaptures
00154 /// specifies whether returning the value (or part of it) from the function
00155 /// counts as capturing it or not.  The boolean StoreCaptures specified whether
00156 /// storing the value (or part of it) into memory anywhere automatically
00157 /// counts as capturing it or not.
00158 bool llvm::PointerMayBeCaptured(const Value *V,
00159                                 bool ReturnCaptures, bool StoreCaptures) {
00160   assert(!isa<GlobalValue>(V) &&
00161          "It doesn't make sense to ask whether a global is captured.");
00162 
00163   // TODO: If StoreCaptures is not true, we could do Fancy analysis
00164   // to determine whether this store is not actually an escape point.
00165   // In that case, BasicAliasAnalysis should be updated as well to
00166   // take advantage of this.
00167   (void)StoreCaptures;
00168 
00169   SimpleCaptureTracker SCT(ReturnCaptures);
00170   PointerMayBeCaptured(V, &SCT);
00171   return SCT.Captured;
00172 }
00173 
00174 /// PointerMayBeCapturedBefore - Return true if this pointer value may be
00175 /// captured by the enclosing function (which is required to exist). If a
00176 /// DominatorTree is provided, only captures which happen before the given
00177 /// instruction are considered. This routine can be expensive, so consider
00178 /// caching the results.  The boolean ReturnCaptures specifies whether
00179 /// returning the value (or part of it) from the function counts as capturing
00180 /// it or not.  The boolean StoreCaptures specified whether storing the value
00181 /// (or part of it) into memory anywhere automatically counts as capturing it
00182 /// or not. A ordered basic block \p OBB can be used in order to speed up
00183 /// queries about relative order among instructions in the same basic block.
00184 bool llvm::PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures,
00185                                       bool StoreCaptures, const Instruction *I,
00186                                       DominatorTree *DT, bool IncludeI,
00187                                       OrderedBasicBlock *OBB) {
00188   assert(!isa<GlobalValue>(V) &&
00189          "It doesn't make sense to ask whether a global is captured.");
00190   bool UseNewOBB = OBB == nullptr;
00191 
00192   if (!DT)
00193     return PointerMayBeCaptured(V, ReturnCaptures, StoreCaptures);
00194   if (UseNewOBB)
00195     OBB = new OrderedBasicBlock(I->getParent());
00196 
00197   // TODO: See comment in PointerMayBeCaptured regarding what could be done
00198   // with StoreCaptures.
00199 
00200   CapturesBefore CB(ReturnCaptures, I, DT, IncludeI, OBB);
00201   PointerMayBeCaptured(V, &CB);
00202 
00203   if (UseNewOBB)
00204     delete OBB;
00205   return CB.Captured;
00206 }
00207 
00208 /// TODO: Write a new FunctionPass AliasAnalysis so that it can keep
00209 /// a cache. Then we can move the code from BasicAliasAnalysis into
00210 /// that path, and remove this threshold.
00211 static int const Threshold = 20;
00212 
00213 void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker) {
00214   assert(V->getType()->isPointerTy() && "Capture is for pointers only!");
00215   SmallVector<const Use *, Threshold> Worklist;
00216   SmallSet<const Use *, Threshold> Visited;
00217   int Count = 0;
00218 
00219   for (const Use &U : V->uses()) {
00220     // If there are lots of uses, conservatively say that the value
00221     // is captured to avoid taking too much compile time.
00222     if (Count++ >= Threshold)
00223       return Tracker->tooManyUses();
00224 
00225     if (!Tracker->shouldExplore(&U)) continue;
00226     Visited.insert(&U);
00227     Worklist.push_back(&U);
00228   }
00229 
00230   while (!Worklist.empty()) {
00231     const Use *U = Worklist.pop_back_val();
00232     Instruction *I = cast<Instruction>(U->getUser());
00233     V = U->get();
00234 
00235     switch (I->getOpcode()) {
00236     case Instruction::Call:
00237     case Instruction::Invoke: {
00238       CallSite CS(I);
00239       // Not captured if the callee is readonly, doesn't return a copy through
00240       // its return value and doesn't unwind (a readonly function can leak bits
00241       // by throwing an exception or not depending on the input value).
00242       if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy())
00243         break;
00244 
00245       // Not captured if only passed via 'nocapture' arguments.  Note that
00246       // calling a function pointer does not in itself cause the pointer to
00247       // be captured.  This is a subtle point considering that (for example)
00248       // the callee might return its own address.  It is analogous to saying
00249       // that loading a value from a pointer does not cause the pointer to be
00250       // captured, even though the loaded value might be the pointer itself
00251       // (think of self-referential objects).
00252       CallSite::data_operand_iterator B =
00253         CS.data_operands_begin(), E = CS.data_operands_end();
00254       for (CallSite::data_operand_iterator A = B; A != E; ++A)
00255         if (A->get() == V && !CS.doesNotCapture(A - B))
00256           // The parameter is not marked 'nocapture' - captured.
00257           if (Tracker->captured(U))
00258             return;
00259       break;
00260     }
00261     case Instruction::Load:
00262       // Loading from a pointer does not cause it to be captured.
00263       break;
00264     case Instruction::VAArg:
00265       // "va-arg" from a pointer does not cause it to be captured.
00266       break;
00267     case Instruction::Store:
00268       if (V == I->getOperand(0))
00269         // Stored the pointer - conservatively assume it may be captured.
00270         if (Tracker->captured(U))
00271           return;
00272       // Storing to the pointee does not cause the pointer to be captured.
00273       break;
00274     case Instruction::BitCast:
00275     case Instruction::GetElementPtr:
00276     case Instruction::PHI:
00277     case Instruction::Select:
00278     case Instruction::AddrSpaceCast:
00279       // The original value is not captured via this if the new value isn't.
00280       Count = 0;
00281       for (Use &UU : I->uses()) {
00282         // If there are lots of uses, conservatively say that the value
00283         // is captured to avoid taking too much compile time.
00284         if (Count++ >= Threshold)
00285           return Tracker->tooManyUses();
00286 
00287         if (Visited.insert(&UU).second)
00288           if (Tracker->shouldExplore(&UU))
00289             Worklist.push_back(&UU);
00290       }
00291       break;
00292     case Instruction::ICmp:
00293       // Don't count comparisons of a no-alias return value against null as
00294       // captures. This allows us to ignore comparisons of malloc results
00295       // with null, for example.
00296       if (ConstantPointerNull *CPN =
00297           dyn_cast<ConstantPointerNull>(I->getOperand(1)))
00298         if (CPN->getType()->getAddressSpace() == 0)
00299           if (isNoAliasCall(V->stripPointerCasts()))
00300             break;
00301       // Otherwise, be conservative. There are crazy ways to capture pointers
00302       // using comparisons.
00303       if (Tracker->captured(U))
00304         return;
00305       break;
00306     default:
00307       // Something else - be conservative and say it is captured.
00308       if (Tracker->captured(U))
00309         return;
00310       break;
00311     }
00312   }
00313 
00314   // All uses examined.
00315 }