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/Analysis/CaptureTracking.h"
00020 #include "llvm/Instructions.h"
00021 #include "llvm/Value.h"
00022 #include "llvm/ADT/SmallSet.h"
00023 #include "llvm/ADT/SmallVector.h"
00024 #include "llvm/Support/CallSite.h"
00025 using namespace llvm;
00026 
00027 /// PointerMayBeCaptured - Return true if this pointer value may be captured
00028 /// by the enclosing function (which is required to exist).  This routine can
00029 /// be expensive, so consider caching the results.  The boolean ReturnCaptures
00030 /// specifies whether returning the value (or part of it) from the function
00031 /// counts as capturing it or not.
00032 bool llvm::PointerMayBeCaptured(const Value *V, bool ReturnCaptures) {
00033   assert(isa<PointerType>(V->getType()) && "Capture is for pointers only!");
00034   SmallVector<Use*, 16> Worklist;
00035   SmallSet<Use*, 16> Visited;
00036 
00037   for (Value::use_const_iterator UI = V->use_begin(), UE = V->use_end();
00038        UI != UE; ++UI) {
00039     Use *U = &UI.getUse();
00040     Visited.insert(U);
00041     Worklist.push_back(U);
00042   }
00043 
00044   while (!Worklist.empty()) {
00045     Use *U = Worklist.pop_back_val();
00046     Instruction *I = cast<Instruction>(U->getUser());
00047     V = U->get();
00048 
00049     switch (I->getOpcode()) {
00050     case Instruction::Call:
00051     case Instruction::Invoke: {
00052       CallSite CS = CallSite::get(I);
00053       // Not captured if the callee is readonly and doesn't return a copy
00054       // through its return value.
00055       if (CS.onlyReadsMemory() && I->getType() == Type::VoidTy)
00056         break;
00057 
00058       // Not captured if only passed via 'nocapture' arguments.  Note that
00059       // calling a function pointer does not in itself cause the pointer to
00060       // be captured.  This is a subtle point considering that (for example)
00061       // the callee might return its own address.  It is analogous to saying
00062       // that loading a value from a pointer does not cause the pointer to be
00063       // captured, even though the loaded value might be the pointer itself
00064       // (think of self-referential objects).
00065       CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
00066       for (CallSite::arg_iterator A = B; A != E; ++A)
00067         if (A->get() == V && !CS.paramHasAttr(A - B + 1, Attribute::NoCapture))
00068           // The parameter is not marked 'nocapture' - captured.
00069           return true;
00070       // Only passed via 'nocapture' arguments, or is the called function - not
00071       // captured.
00072       break;
00073     }
00074     case Instruction::Free:
00075       // Freeing a pointer does not cause it to be captured.
00076       break;
00077     case Instruction::Load:
00078       // Loading from a pointer does not cause it to be captured.
00079       break;
00080     case Instruction::Ret:
00081       if (ReturnCaptures)
00082         return true;
00083       break;
00084     case Instruction::Store:
00085       if (V == I->getOperand(0))
00086         // Stored the pointer - it may be captured.
00087         return true;
00088       // Storing to the pointee does not cause the pointer to be captured.
00089       break;
00090     case Instruction::BitCast:
00091     case Instruction::GetElementPtr:
00092     case Instruction::PHI:
00093     case Instruction::Select:
00094       // The original value is not captured via this if the new value isn't.
00095       for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end();
00096            UI != UE; ++UI) {
00097         Use *U = &UI.getUse();
00098         if (Visited.insert(U))
00099           Worklist.push_back(U);
00100       }
00101       break;
00102     default:
00103       // Something else - be conservative and say it is captured.
00104       return true;
00105     }
00106   }
00107 
00108   // All uses examined - not captured.
00109   return false;
00110 }