LLVM API Documentation

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/CaptureTracking.h"
00023 #include "llvm/Analysis/CFG.h"
00024 #include "llvm/IR/CallSite.h"
00025 #include "llvm/IR/Constants.h"
00026 #include "llvm/IR/Dominators.h"
00027 #include "llvm/IR/Instructions.h"
00028 
00029 using namespace llvm;
00030 
00031 CaptureTracker::~CaptureTracker() {}
00032 
00033 bool CaptureTracker::shouldExplore(const Use *U) { return true; }
00034 
00035 namespace {
00036   struct SimpleCaptureTracker : public CaptureTracker {
00037     explicit SimpleCaptureTracker(bool ReturnCaptures)
00038       : ReturnCaptures(ReturnCaptures), Captured(false) {}
00039 
00040     void tooManyUses() override { Captured = true; }
00041 
00042     bool captured(const Use *U) override {
00043       if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures)
00044         return false;
00045 
00046       Captured = true;
00047       return true;
00048     }
00049 
00050     bool ReturnCaptures;
00051 
00052     bool Captured;
00053   };
00054 
00055   /// Only find pointer captures which happen before the given instruction. Uses
00056   /// the dominator tree to determine whether one instruction is before another.
00057   /// Only support the case where the Value is defined in the same basic block
00058   /// as the given instruction and the use.
00059   struct CapturesBefore : public CaptureTracker {
00060     CapturesBefore(bool ReturnCaptures, const Instruction *I, DominatorTree *DT,
00061                    bool IncludeI)
00062       : BeforeHere(I), DT(DT), ReturnCaptures(ReturnCaptures),
00063         IncludeI(IncludeI), Captured(false) {}
00064 
00065     void tooManyUses() override { Captured = true; }
00066 
00067     bool shouldExplore(const Use *U) override {
00068       Instruction *I = cast<Instruction>(U->getUser());
00069       if (BeforeHere == I && !IncludeI)
00070         return false;
00071 
00072       BasicBlock *BB = I->getParent();
00073       // We explore this usage only if the usage can reach "BeforeHere".
00074       // If use is not reachable from entry, there is no need to explore.
00075       if (BeforeHere != I && !DT->isReachableFromEntry(BB))
00076         return false;
00077       // If the value is defined in the same basic block as use and BeforeHere,
00078       // there is no need to explore the use if BeforeHere dominates use.
00079       // Check whether there is a path from I to BeforeHere.
00080       if (BeforeHere != I && DT->dominates(BeforeHere, I) &&
00081           !isPotentiallyReachable(I, BeforeHere, DT))
00082         return false;
00083       return true;
00084     }
00085 
00086     bool captured(const Use *U) override {
00087       if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures)
00088         return false;
00089 
00090       Instruction *I = cast<Instruction>(U->getUser());
00091       if (BeforeHere == I && !IncludeI)
00092         return false;
00093 
00094       BasicBlock *BB = I->getParent();
00095       // Same logic as in shouldExplore.
00096       if (BeforeHere != I && !DT->isReachableFromEntry(BB))
00097         return false;
00098       if (BeforeHere != I && DT->dominates(BeforeHere, I) &&
00099           !isPotentiallyReachable(I, BeforeHere, DT))
00100         return false;
00101       Captured = true;
00102       return true;
00103     }
00104 
00105     const Instruction *BeforeHere;
00106     DominatorTree *DT;
00107 
00108     bool ReturnCaptures;
00109     bool IncludeI;
00110 
00111     bool Captured;
00112   };
00113 }
00114 
00115 /// PointerMayBeCaptured - Return true if this pointer value may be captured
00116 /// by the enclosing function (which is required to exist).  This routine can
00117 /// be expensive, so consider caching the results.  The boolean ReturnCaptures
00118 /// specifies whether returning the value (or part of it) from the function
00119 /// counts as capturing it or not.  The boolean StoreCaptures specified whether
00120 /// storing the value (or part of it) into memory anywhere automatically
00121 /// counts as capturing it or not.
00122 bool llvm::PointerMayBeCaptured(const Value *V,
00123                                 bool ReturnCaptures, bool StoreCaptures) {
00124   assert(!isa<GlobalValue>(V) &&
00125          "It doesn't make sense to ask whether a global is captured.");
00126 
00127   // TODO: If StoreCaptures is not true, we could do Fancy analysis
00128   // to determine whether this store is not actually an escape point.
00129   // In that case, BasicAliasAnalysis should be updated as well to
00130   // take advantage of this.
00131   (void)StoreCaptures;
00132 
00133   SimpleCaptureTracker SCT(ReturnCaptures);
00134   PointerMayBeCaptured(V, &SCT);
00135   return SCT.Captured;
00136 }
00137 
00138 /// PointerMayBeCapturedBefore - Return true if this pointer value may be
00139 /// captured by the enclosing function (which is required to exist). If a
00140 /// DominatorTree is provided, only captures which happen before the given
00141 /// instruction are considered. This routine can be expensive, so consider
00142 /// caching the results.  The boolean ReturnCaptures specifies whether
00143 /// returning the value (or part of it) from the function counts as capturing
00144 /// it or not.  The boolean StoreCaptures specified whether storing the value
00145 /// (or part of it) into memory anywhere automatically counts as capturing it
00146 /// or not.
00147 bool llvm::PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures,
00148                                       bool StoreCaptures, const Instruction *I,
00149                                       DominatorTree *DT, bool IncludeI) {
00150   assert(!isa<GlobalValue>(V) &&
00151          "It doesn't make sense to ask whether a global is captured.");
00152 
00153   if (!DT)
00154     return PointerMayBeCaptured(V, ReturnCaptures, StoreCaptures);
00155 
00156   // TODO: See comment in PointerMayBeCaptured regarding what could be done
00157   // with StoreCaptures.
00158 
00159   CapturesBefore CB(ReturnCaptures, I, DT, IncludeI);
00160   PointerMayBeCaptured(V, &CB);
00161   return CB.Captured;
00162 }
00163 
00164 /// TODO: Write a new FunctionPass AliasAnalysis so that it can keep
00165 /// a cache. Then we can move the code from BasicAliasAnalysis into
00166 /// that path, and remove this threshold.
00167 static int const Threshold = 20;
00168 
00169 void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker) {
00170   assert(V->getType()->isPointerTy() && "Capture is for pointers only!");
00171   SmallVector<const Use *, Threshold> Worklist;
00172   SmallSet<const Use *, Threshold> Visited;
00173   int Count = 0;
00174 
00175   for (const Use &U : V->uses()) {
00176     // If there are lots of uses, conservatively say that the value
00177     // is captured to avoid taking too much compile time.
00178     if (Count++ >= Threshold)
00179       return Tracker->tooManyUses();
00180 
00181     if (!Tracker->shouldExplore(&U)) continue;
00182     Visited.insert(&U);
00183     Worklist.push_back(&U);
00184   }
00185 
00186   while (!Worklist.empty()) {
00187     const Use *U = Worklist.pop_back_val();
00188     Instruction *I = cast<Instruction>(U->getUser());
00189     V = U->get();
00190 
00191     switch (I->getOpcode()) {
00192     case Instruction::Call:
00193     case Instruction::Invoke: {
00194       CallSite CS(I);
00195       // Not captured if the callee is readonly, doesn't return a copy through
00196       // its return value and doesn't unwind (a readonly function can leak bits
00197       // by throwing an exception or not depending on the input value).
00198       if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy())
00199         break;
00200 
00201       // Not captured if only passed via 'nocapture' arguments.  Note that
00202       // calling a function pointer does not in itself cause the pointer to
00203       // be captured.  This is a subtle point considering that (for example)
00204       // the callee might return its own address.  It is analogous to saying
00205       // that loading a value from a pointer does not cause the pointer to be
00206       // captured, even though the loaded value might be the pointer itself
00207       // (think of self-referential objects).
00208       CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
00209       for (CallSite::arg_iterator A = B; A != E; ++A)
00210         if (A->get() == V && !CS.doesNotCapture(A - B))
00211           // The parameter is not marked 'nocapture' - captured.
00212           if (Tracker->captured(U))
00213             return;
00214       break;
00215     }
00216     case Instruction::Load:
00217       // Loading from a pointer does not cause it to be captured.
00218       break;
00219     case Instruction::VAArg:
00220       // "va-arg" from a pointer does not cause it to be captured.
00221       break;
00222     case Instruction::Store:
00223       if (V == I->getOperand(0))
00224         // Stored the pointer - conservatively assume it may be captured.
00225         if (Tracker->captured(U))
00226           return;
00227       // Storing to the pointee does not cause the pointer to be captured.
00228       break;
00229     case Instruction::BitCast:
00230     case Instruction::GetElementPtr:
00231     case Instruction::PHI:
00232     case Instruction::Select:
00233     case Instruction::AddrSpaceCast:
00234       // The original value is not captured via this if the new value isn't.
00235       Count = 0;
00236       for (Use &UU : I->uses()) {
00237         // If there are lots of uses, conservatively say that the value
00238         // is captured to avoid taking too much compile time.
00239         if (Count++ >= Threshold)
00240           return Tracker->tooManyUses();
00241 
00242         if (Visited.insert(&UU))
00243           if (Tracker->shouldExplore(&UU))
00244             Worklist.push_back(&UU);
00245       }
00246       break;
00247     case Instruction::ICmp:
00248       // Don't count comparisons of a no-alias return value against null as
00249       // captures. This allows us to ignore comparisons of malloc results
00250       // with null, for example.
00251       if (ConstantPointerNull *CPN =
00252           dyn_cast<ConstantPointerNull>(I->getOperand(1)))
00253         if (CPN->getType()->getAddressSpace() == 0)
00254           if (isNoAliasCall(V->stripPointerCasts()))
00255             break;
00256       // Otherwise, be conservative. There are crazy ways to capture pointers
00257       // using comparisons.
00258       if (Tracker->captured(U))
00259         return;
00260       break;
00261     default:
00262       // Something else - be conservative and say it is captured.
00263       if (Tracker->captured(U))
00264         return;
00265       break;
00266     }
00267   }
00268 
00269   // All uses examined.
00270 }