CaptureTracking.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===----- llvm/Analysis/CaptureTracking.h - Pointer capture ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains routines that help determine which pointers are captured.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_CAPTURETRACKING_H
  18. #define LLVM_ANALYSIS_CAPTURETRACKING_H
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/STLFunctionalExtras.h"
  21. namespace llvm {
  22. class Value;
  23. class Use;
  24. class DataLayout;
  25. class Instruction;
  26. class DominatorTree;
  27. class LoopInfo;
  28. class Function;
  29. template <typename T> class SmallPtrSetImpl;
  30. /// getDefaultMaxUsesToExploreForCaptureTracking - Return default value of
  31. /// the maximal number of uses to explore before giving up. It is used by
  32. /// PointerMayBeCaptured family analysis.
  33. unsigned getDefaultMaxUsesToExploreForCaptureTracking();
  34. /// PointerMayBeCaptured - Return true if this pointer value may be captured
  35. /// by the enclosing function (which is required to exist). This routine can
  36. /// be expensive, so consider caching the results. The boolean ReturnCaptures
  37. /// specifies whether returning the value (or part of it) from the function
  38. /// counts as capturing it or not. The boolean StoreCaptures specified
  39. /// whether storing the value (or part of it) into memory anywhere
  40. /// automatically counts as capturing it or not.
  41. /// MaxUsesToExplore specifies how many uses the analysis should explore for
  42. /// one value before giving up due too "too many uses". If MaxUsesToExplore
  43. /// is zero, a default value is assumed.
  44. bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures,
  45. bool StoreCaptures, unsigned MaxUsesToExplore = 0);
  46. /// Variant of the above function which accepts a set of Values that are
  47. /// ephemeral and cannot cause pointers to escape.
  48. bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures,
  49. bool StoreCaptures,
  50. const SmallPtrSetImpl<const Value *> &EphValues,
  51. unsigned MaxUsesToExplore = 0);
  52. /// PointerMayBeCapturedBefore - Return true if this pointer value may be
  53. /// captured by the enclosing function (which is required to exist). If a
  54. /// DominatorTree is provided, only captures which happen before the given
  55. /// instruction are considered. This routine can be expensive, so consider
  56. /// caching the results. The boolean ReturnCaptures specifies whether
  57. /// returning the value (or part of it) from the function counts as capturing
  58. /// it or not. The boolean StoreCaptures specified whether storing the value
  59. /// (or part of it) into memory anywhere automatically counts as capturing it
  60. /// or not. Captures by the provided instruction are considered if the
  61. /// final parameter is true.
  62. /// MaxUsesToExplore specifies how many uses the analysis should explore for
  63. /// one value before giving up due too "too many uses". If MaxUsesToExplore
  64. /// is zero, a default value is assumed.
  65. bool PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures,
  66. bool StoreCaptures, const Instruction *I,
  67. const DominatorTree *DT,
  68. bool IncludeI = false,
  69. unsigned MaxUsesToExplore = 0,
  70. const LoopInfo *LI = nullptr);
  71. // Returns the 'earliest' instruction that captures \p V in \F. An instruction
  72. // A is considered earlier than instruction B, if A dominates B. If 2 escapes
  73. // do not dominate each other, the terminator of the common dominator is
  74. // chosen. If not all uses can be analyzed, the earliest escape is set to
  75. // the first instruction in the function entry block. If \p V does not escape,
  76. // nullptr is returned. Note that the caller of the function has to ensure
  77. // that the instruction the result value is compared against is not in a
  78. // cycle.
  79. Instruction *
  80. FindEarliestCapture(const Value *V, Function &F, bool ReturnCaptures,
  81. bool StoreCaptures, const DominatorTree &DT,
  82. const SmallPtrSetImpl<const Value *> &EphValues,
  83. unsigned MaxUsesToExplore = 0);
  84. /// This callback is used in conjunction with PointerMayBeCaptured. In
  85. /// addition to the interface here, you'll need to provide your own getters
  86. /// to see whether anything was captured.
  87. struct CaptureTracker {
  88. virtual ~CaptureTracker();
  89. /// tooManyUses - The depth of traversal has breached a limit. There may be
  90. /// capturing instructions that will not be passed into captured().
  91. virtual void tooManyUses() = 0;
  92. /// shouldExplore - This is the use of a value derived from the pointer.
  93. /// To prune the search (ie., assume that none of its users could possibly
  94. /// capture) return false. To search it, return true.
  95. ///
  96. /// U->getUser() is always an Instruction.
  97. virtual bool shouldExplore(const Use *U);
  98. /// captured - Information about the pointer was captured by the user of
  99. /// use U. Return true to stop the traversal or false to continue looking
  100. /// for more capturing instructions.
  101. virtual bool captured(const Use *U) = 0;
  102. /// isDereferenceableOrNull - Overload to allow clients with additional
  103. /// knowledge about pointer dereferenceability to provide it and thereby
  104. /// avoid conservative responses when a pointer is compared to null.
  105. virtual bool isDereferenceableOrNull(Value *O, const DataLayout &DL);
  106. };
  107. /// Types of use capture kinds, see \p DetermineUseCaptureKind.
  108. enum class UseCaptureKind {
  109. NO_CAPTURE,
  110. MAY_CAPTURE,
  111. PASSTHROUGH,
  112. };
  113. /// Determine what kind of capture behaviour \p U may exhibit.
  114. ///
  115. /// A use can be no-capture, a use can potentially capture, or a use can be
  116. /// passthrough such that the uses of the user or \p U should be inspected.
  117. /// The \p IsDereferenceableOrNull callback is used to rule out capturing for
  118. /// certain comparisons.
  119. UseCaptureKind
  120. DetermineUseCaptureKind(const Use &U,
  121. llvm::function_ref<bool(Value *, const DataLayout &)>
  122. IsDereferenceableOrNull);
  123. /// PointerMayBeCaptured - Visit the value and the values derived from it and
  124. /// find values which appear to be capturing the pointer value. This feeds
  125. /// results into and is controlled by the CaptureTracker object.
  126. /// MaxUsesToExplore specifies how many uses the analysis should explore for
  127. /// one value before giving up due too "too many uses". If MaxUsesToExplore
  128. /// is zero, a default value is assumed.
  129. void PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
  130. unsigned MaxUsesToExplore = 0);
  131. /// Returns true if the pointer is to a function-local object that never
  132. /// escapes from the function.
  133. bool isNonEscapingLocalObject(
  134. const Value *V,
  135. SmallDenseMap<const Value *, bool, 8> *IsCapturedCache = nullptr);
  136. } // end namespace llvm
  137. #endif
  138. #ifdef __GNUC__
  139. #pragma GCC diagnostic pop
  140. #endif