CaptureTracking.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. //===--- CaptureTracking.cpp - Determine whether a pointer is captured ----===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains routines that help determine which pointers are captured.
  10. // A pointer value is captured if the function makes a copy of any part of the
  11. // pointer that outlives the call. Not being captured means, more or less, that
  12. // the pointer is only dereferenced and not stored in a global. Returning part
  13. // of the pointer as the function return value may or may not count as capturing
  14. // the pointer, depending on the context.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "llvm/Analysis/CaptureTracking.h"
  18. #include "llvm/ADT/SmallSet.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/Statistic.h"
  21. #include "llvm/Analysis/AliasAnalysis.h"
  22. #include "llvm/Analysis/CFG.h"
  23. #include "llvm/Analysis/ValueTracking.h"
  24. #include "llvm/IR/Constants.h"
  25. #include "llvm/IR/Dominators.h"
  26. #include "llvm/IR/Instructions.h"
  27. #include "llvm/IR/IntrinsicInst.h"
  28. #include "llvm/Support/CommandLine.h"
  29. using namespace llvm;
  30. #define DEBUG_TYPE "capture-tracking"
  31. STATISTIC(NumCaptured, "Number of pointers maybe captured");
  32. STATISTIC(NumNotCaptured, "Number of pointers not captured");
  33. STATISTIC(NumCapturedBefore, "Number of pointers maybe captured before");
  34. STATISTIC(NumNotCapturedBefore, "Number of pointers not captured before");
  35. /// The default value for MaxUsesToExplore argument. It's relatively small to
  36. /// keep the cost of analysis reasonable for clients like BasicAliasAnalysis,
  37. /// where the results can't be cached.
  38. /// TODO: we should probably introduce a caching CaptureTracking analysis and
  39. /// use it where possible. The caching version can use much higher limit or
  40. /// don't have this cap at all.
  41. static cl::opt<unsigned>
  42. DefaultMaxUsesToExplore("capture-tracking-max-uses-to-explore", cl::Hidden,
  43. cl::desc("Maximal number of uses to explore."),
  44. cl::init(20));
  45. unsigned llvm::getDefaultMaxUsesToExploreForCaptureTracking() {
  46. return DefaultMaxUsesToExplore;
  47. }
  48. CaptureTracker::~CaptureTracker() {}
  49. bool CaptureTracker::shouldExplore(const Use *U) { return true; }
  50. bool CaptureTracker::isDereferenceableOrNull(Value *O, const DataLayout &DL) {
  51. // An inbounds GEP can either be a valid pointer (pointing into
  52. // or to the end of an allocation), or be null in the default
  53. // address space. So for an inbounds GEP there is no way to let
  54. // the pointer escape using clever GEP hacking because doing so
  55. // would make the pointer point outside of the allocated object
  56. // and thus make the GEP result a poison value. Similarly, other
  57. // dereferenceable pointers cannot be manipulated without producing
  58. // poison.
  59. if (auto *GEP = dyn_cast<GetElementPtrInst>(O))
  60. if (GEP->isInBounds())
  61. return true;
  62. bool CanBeNull, CanBeFreed;
  63. return O->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
  64. }
  65. namespace {
  66. struct SimpleCaptureTracker : public CaptureTracker {
  67. explicit SimpleCaptureTracker(bool ReturnCaptures)
  68. : ReturnCaptures(ReturnCaptures) {}
  69. void tooManyUses() override { Captured = true; }
  70. bool captured(const Use *U) override {
  71. if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures)
  72. return false;
  73. Captured = true;
  74. return true;
  75. }
  76. bool ReturnCaptures;
  77. bool Captured = false;
  78. };
  79. /// Only find pointer captures which happen before the given instruction. Uses
  80. /// the dominator tree to determine whether one instruction is before another.
  81. /// Only support the case where the Value is defined in the same basic block
  82. /// as the given instruction and the use.
  83. struct CapturesBefore : public CaptureTracker {
  84. CapturesBefore(bool ReturnCaptures, const Instruction *I,
  85. const DominatorTree *DT, bool IncludeI, const LoopInfo *LI)
  86. : BeforeHere(I), DT(DT), ReturnCaptures(ReturnCaptures),
  87. IncludeI(IncludeI), LI(LI) {}
  88. void tooManyUses() override { Captured = true; }
  89. bool isSafeToPrune(Instruction *I) {
  90. if (BeforeHere == I)
  91. return !IncludeI;
  92. // We explore this usage only if the usage can reach "BeforeHere".
  93. // If use is not reachable from entry, there is no need to explore.
  94. if (!DT->isReachableFromEntry(I->getParent()))
  95. return true;
  96. // Check whether there is a path from I to BeforeHere.
  97. return !isPotentiallyReachable(I, BeforeHere, nullptr, DT, LI);
  98. }
  99. bool captured(const Use *U) override {
  100. Instruction *I = cast<Instruction>(U->getUser());
  101. if (isa<ReturnInst>(I) && !ReturnCaptures)
  102. return false;
  103. // Check isSafeToPrune() here rather than in shouldExplore() to avoid
  104. // an expensive reachability query for every instruction we look at.
  105. // Instead we only do one for actual capturing candidates.
  106. if (isSafeToPrune(I))
  107. return false;
  108. Captured = true;
  109. return true;
  110. }
  111. const Instruction *BeforeHere;
  112. const DominatorTree *DT;
  113. bool ReturnCaptures;
  114. bool IncludeI;
  115. bool Captured = false;
  116. const LoopInfo *LI;
  117. };
  118. /// Find the 'earliest' instruction before which the pointer is known not to
  119. /// be captured. Here an instruction A is considered earlier than instruction
  120. /// B, if A dominates B. If 2 escapes do not dominate each other, the
  121. /// terminator of the common dominator is chosen. If not all uses cannot be
  122. /// analyzed, the earliest escape is set to the first instruction in the
  123. /// function entry block.
  124. // NOTE: Users have to make sure instructions compared against the earliest
  125. // escape are not in a cycle.
  126. struct EarliestCaptures : public CaptureTracker {
  127. EarliestCaptures(bool ReturnCaptures, Function &F, const DominatorTree &DT)
  128. : DT(DT), ReturnCaptures(ReturnCaptures), F(F) {}
  129. void tooManyUses() override {
  130. Captured = true;
  131. EarliestCapture = &*F.getEntryBlock().begin();
  132. }
  133. bool captured(const Use *U) override {
  134. Instruction *I = cast<Instruction>(U->getUser());
  135. if (isa<ReturnInst>(I) && !ReturnCaptures)
  136. return false;
  137. if (!EarliestCapture) {
  138. EarliestCapture = I;
  139. } else if (EarliestCapture->getParent() == I->getParent()) {
  140. if (I->comesBefore(EarliestCapture))
  141. EarliestCapture = I;
  142. } else {
  143. BasicBlock *CurrentBB = I->getParent();
  144. BasicBlock *EarliestBB = EarliestCapture->getParent();
  145. if (DT.dominates(EarliestBB, CurrentBB)) {
  146. // EarliestCapture already comes before the current use.
  147. } else if (DT.dominates(CurrentBB, EarliestBB)) {
  148. EarliestCapture = I;
  149. } else {
  150. // Otherwise find the nearest common dominator and use its terminator.
  151. auto *NearestCommonDom =
  152. DT.findNearestCommonDominator(CurrentBB, EarliestBB);
  153. EarliestCapture = NearestCommonDom->getTerminator();
  154. }
  155. }
  156. Captured = true;
  157. // Return false to continue analysis; we need to see all potential
  158. // captures.
  159. return false;
  160. }
  161. Instruction *EarliestCapture = nullptr;
  162. const DominatorTree &DT;
  163. bool ReturnCaptures;
  164. bool Captured = false;
  165. Function &F;
  166. };
  167. }
  168. /// PointerMayBeCaptured - Return true if this pointer value may be captured
  169. /// by the enclosing function (which is required to exist). This routine can
  170. /// be expensive, so consider caching the results. The boolean ReturnCaptures
  171. /// specifies whether returning the value (or part of it) from the function
  172. /// counts as capturing it or not. The boolean StoreCaptures specified whether
  173. /// storing the value (or part of it) into memory anywhere automatically
  174. /// counts as capturing it or not.
  175. bool llvm::PointerMayBeCaptured(const Value *V,
  176. bool ReturnCaptures, bool StoreCaptures,
  177. unsigned MaxUsesToExplore) {
  178. assert(!isa<GlobalValue>(V) &&
  179. "It doesn't make sense to ask whether a global is captured.");
  180. // TODO: If StoreCaptures is not true, we could do Fancy analysis
  181. // to determine whether this store is not actually an escape point.
  182. // In that case, BasicAliasAnalysis should be updated as well to
  183. // take advantage of this.
  184. (void)StoreCaptures;
  185. SimpleCaptureTracker SCT(ReturnCaptures);
  186. PointerMayBeCaptured(V, &SCT, MaxUsesToExplore);
  187. if (SCT.Captured)
  188. ++NumCaptured;
  189. else
  190. ++NumNotCaptured;
  191. return SCT.Captured;
  192. }
  193. /// PointerMayBeCapturedBefore - Return true if this pointer value may be
  194. /// captured by the enclosing function (which is required to exist). If a
  195. /// DominatorTree is provided, only captures which happen before the given
  196. /// instruction are considered. This routine can be expensive, so consider
  197. /// caching the results. The boolean ReturnCaptures specifies whether
  198. /// returning the value (or part of it) from the function counts as capturing
  199. /// it or not. The boolean StoreCaptures specified whether storing the value
  200. /// (or part of it) into memory anywhere automatically counts as capturing it
  201. /// or not.
  202. bool llvm::PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures,
  203. bool StoreCaptures, const Instruction *I,
  204. const DominatorTree *DT, bool IncludeI,
  205. unsigned MaxUsesToExplore,
  206. const LoopInfo *LI) {
  207. assert(!isa<GlobalValue>(V) &&
  208. "It doesn't make sense to ask whether a global is captured.");
  209. if (!DT)
  210. return PointerMayBeCaptured(V, ReturnCaptures, StoreCaptures,
  211. MaxUsesToExplore);
  212. // TODO: See comment in PointerMayBeCaptured regarding what could be done
  213. // with StoreCaptures.
  214. CapturesBefore CB(ReturnCaptures, I, DT, IncludeI, LI);
  215. PointerMayBeCaptured(V, &CB, MaxUsesToExplore);
  216. if (CB.Captured)
  217. ++NumCapturedBefore;
  218. else
  219. ++NumNotCapturedBefore;
  220. return CB.Captured;
  221. }
  222. Instruction *llvm::FindEarliestCapture(const Value *V, Function &F,
  223. bool ReturnCaptures, bool StoreCaptures,
  224. const DominatorTree &DT,
  225. unsigned MaxUsesToExplore) {
  226. assert(!isa<GlobalValue>(V) &&
  227. "It doesn't make sense to ask whether a global is captured.");
  228. EarliestCaptures CB(ReturnCaptures, F, DT);
  229. PointerMayBeCaptured(V, &CB, MaxUsesToExplore);
  230. if (CB.Captured)
  231. ++NumCapturedBefore;
  232. else
  233. ++NumNotCapturedBefore;
  234. return CB.EarliestCapture;
  235. }
  236. void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
  237. unsigned MaxUsesToExplore) {
  238. assert(V->getType()->isPointerTy() && "Capture is for pointers only!");
  239. if (MaxUsesToExplore == 0)
  240. MaxUsesToExplore = DefaultMaxUsesToExplore;
  241. SmallVector<const Use *, 20> Worklist;
  242. Worklist.reserve(getDefaultMaxUsesToExploreForCaptureTracking());
  243. SmallSet<const Use *, 20> Visited;
  244. auto AddUses = [&](const Value *V) {
  245. unsigned Count = 0;
  246. for (const Use &U : V->uses()) {
  247. // If there are lots of uses, conservatively say that the value
  248. // is captured to avoid taking too much compile time.
  249. if (Count++ >= MaxUsesToExplore) {
  250. Tracker->tooManyUses();
  251. return false;
  252. }
  253. if (!Visited.insert(&U).second)
  254. continue;
  255. if (!Tracker->shouldExplore(&U))
  256. continue;
  257. Worklist.push_back(&U);
  258. }
  259. return true;
  260. };
  261. if (!AddUses(V))
  262. return;
  263. while (!Worklist.empty()) {
  264. const Use *U = Worklist.pop_back_val();
  265. Instruction *I = cast<Instruction>(U->getUser());
  266. switch (I->getOpcode()) {
  267. case Instruction::Call:
  268. case Instruction::Invoke: {
  269. auto *Call = cast<CallBase>(I);
  270. // Not captured if the callee is readonly, doesn't return a copy through
  271. // its return value and doesn't unwind (a readonly function can leak bits
  272. // by throwing an exception or not depending on the input value).
  273. if (Call->onlyReadsMemory() && Call->doesNotThrow() &&
  274. Call->getType()->isVoidTy())
  275. break;
  276. // The pointer is not captured if returned pointer is not captured.
  277. // NOTE: CaptureTracking users should not assume that only functions
  278. // marked with nocapture do not capture. This means that places like
  279. // getUnderlyingObject in ValueTracking or DecomposeGEPExpression
  280. // in BasicAA also need to know about this property.
  281. if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(Call,
  282. true)) {
  283. if (!AddUses(Call))
  284. return;
  285. break;
  286. }
  287. // Volatile operations effectively capture the memory location that they
  288. // load and store to.
  289. if (auto *MI = dyn_cast<MemIntrinsic>(Call))
  290. if (MI->isVolatile())
  291. if (Tracker->captured(U))
  292. return;
  293. // Calling a function pointer does not in itself cause the pointer to
  294. // be captured. This is a subtle point considering that (for example)
  295. // the callee might return its own address. It is analogous to saying
  296. // that loading a value from a pointer does not cause the pointer to be
  297. // captured, even though the loaded value might be the pointer itself
  298. // (think of self-referential objects).
  299. if (Call->isCallee(U))
  300. break;
  301. // Not captured if only passed via 'nocapture' arguments.
  302. if (Call->isDataOperand(U) &&
  303. !Call->doesNotCapture(Call->getDataOperandNo(U))) {
  304. // The parameter is not marked 'nocapture' - captured.
  305. if (Tracker->captured(U))
  306. return;
  307. }
  308. break;
  309. }
  310. case Instruction::Load:
  311. // Volatile loads make the address observable.
  312. if (cast<LoadInst>(I)->isVolatile())
  313. if (Tracker->captured(U))
  314. return;
  315. break;
  316. case Instruction::VAArg:
  317. // "va-arg" from a pointer does not cause it to be captured.
  318. break;
  319. case Instruction::Store:
  320. // Stored the pointer - conservatively assume it may be captured.
  321. // Volatile stores make the address observable.
  322. if (U->getOperandNo() == 0 || cast<StoreInst>(I)->isVolatile())
  323. if (Tracker->captured(U))
  324. return;
  325. break;
  326. case Instruction::AtomicRMW: {
  327. // atomicrmw conceptually includes both a load and store from
  328. // the same location.
  329. // As with a store, the location being accessed is not captured,
  330. // but the value being stored is.
  331. // Volatile stores make the address observable.
  332. auto *ARMWI = cast<AtomicRMWInst>(I);
  333. if (U->getOperandNo() == 1 || ARMWI->isVolatile())
  334. if (Tracker->captured(U))
  335. return;
  336. break;
  337. }
  338. case Instruction::AtomicCmpXchg: {
  339. // cmpxchg conceptually includes both a load and store from
  340. // the same location.
  341. // As with a store, the location being accessed is not captured,
  342. // but the value being stored is.
  343. // Volatile stores make the address observable.
  344. auto *ACXI = cast<AtomicCmpXchgInst>(I);
  345. if (U->getOperandNo() == 1 || U->getOperandNo() == 2 ||
  346. ACXI->isVolatile())
  347. if (Tracker->captured(U))
  348. return;
  349. break;
  350. }
  351. case Instruction::BitCast:
  352. case Instruction::GetElementPtr:
  353. case Instruction::PHI:
  354. case Instruction::Select:
  355. case Instruction::AddrSpaceCast:
  356. // The original value is not captured via this if the new value isn't.
  357. if (!AddUses(I))
  358. return;
  359. break;
  360. case Instruction::ICmp: {
  361. unsigned Idx = U->getOperandNo();
  362. unsigned OtherIdx = 1 - Idx;
  363. if (auto *CPN = dyn_cast<ConstantPointerNull>(I->getOperand(OtherIdx))) {
  364. // Don't count comparisons of a no-alias return value against null as
  365. // captures. This allows us to ignore comparisons of malloc results
  366. // with null, for example.
  367. if (CPN->getType()->getAddressSpace() == 0)
  368. if (isNoAliasCall(U->get()->stripPointerCasts()))
  369. break;
  370. if (!I->getFunction()->nullPointerIsDefined()) {
  371. auto *O = I->getOperand(Idx)->stripPointerCastsSameRepresentation();
  372. // Comparing a dereferenceable_or_null pointer against null cannot
  373. // lead to pointer escapes, because if it is not null it must be a
  374. // valid (in-bounds) pointer.
  375. if (Tracker->isDereferenceableOrNull(O, I->getModule()->getDataLayout()))
  376. break;
  377. }
  378. }
  379. // Comparison against value stored in global variable. Given the pointer
  380. // does not escape, its value cannot be guessed and stored separately in a
  381. // global variable.
  382. auto *LI = dyn_cast<LoadInst>(I->getOperand(OtherIdx));
  383. if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
  384. break;
  385. // Otherwise, be conservative. There are crazy ways to capture pointers
  386. // using comparisons.
  387. if (Tracker->captured(U))
  388. return;
  389. break;
  390. }
  391. default:
  392. // Something else - be conservative and say it is captured.
  393. if (Tracker->captured(U))
  394. return;
  395. break;
  396. }
  397. }
  398. // All uses examined.
  399. }
  400. bool llvm::isNonEscapingLocalObject(
  401. const Value *V, SmallDenseMap<const Value *, bool, 8> *IsCapturedCache) {
  402. SmallDenseMap<const Value *, bool, 8>::iterator CacheIt;
  403. if (IsCapturedCache) {
  404. bool Inserted;
  405. std::tie(CacheIt, Inserted) = IsCapturedCache->insert({V, false});
  406. if (!Inserted)
  407. // Found cached result, return it!
  408. return CacheIt->second;
  409. }
  410. // If this is an identified function-local object, check to see if it escapes.
  411. if (isIdentifiedFunctionLocal(V)) {
  412. // Set StoreCaptures to True so that we can assume in our callers that the
  413. // pointer is not the result of a load instruction. Currently
  414. // PointerMayBeCaptured doesn't have any special analysis for the
  415. // StoreCaptures=false case; if it did, our callers could be refined to be
  416. // more precise.
  417. auto Ret = !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
  418. if (IsCapturedCache)
  419. CacheIt->second = Ret;
  420. return Ret;
  421. }
  422. return false;
  423. }