CaptureTracking.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. namespace llvm {
  21. class Value;
  22. class Use;
  23. class DataLayout;
  24. class Instruction;
  25. class DominatorTree;
  26. class LoopInfo;
  27. class Function;
  28. /// getDefaultMaxUsesToExploreForCaptureTracking - Return default value of
  29. /// the maximal number of uses to explore before giving up. It is used by
  30. /// PointerMayBeCaptured family analysis.
  31. unsigned getDefaultMaxUsesToExploreForCaptureTracking();
  32. /// PointerMayBeCaptured - Return true if this pointer value may be captured
  33. /// by the enclosing function (which is required to exist). This routine can
  34. /// be expensive, so consider caching the results. The boolean ReturnCaptures
  35. /// specifies whether returning the value (or part of it) from the function
  36. /// counts as capturing it or not. The boolean StoreCaptures specified
  37. /// whether storing the value (or part of it) into memory anywhere
  38. /// automatically counts as capturing it or not.
  39. /// MaxUsesToExplore specifies how many uses the analysis should explore for
  40. /// one value before giving up due too "too many uses". If MaxUsesToExplore
  41. /// is zero, a default value is assumed.
  42. bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures,
  43. bool StoreCaptures,
  44. unsigned MaxUsesToExplore = 0);
  45. /// PointerMayBeCapturedBefore - Return true if this pointer value may be
  46. /// captured by the enclosing function (which is required to exist). If a
  47. /// DominatorTree is provided, only captures which happen before the given
  48. /// instruction are considered. This routine can be expensive, so consider
  49. /// caching the results. The boolean ReturnCaptures specifies whether
  50. /// returning the value (or part of it) from the function counts as capturing
  51. /// it or not. The boolean StoreCaptures specified whether storing the value
  52. /// (or part of it) into memory anywhere automatically counts as capturing it
  53. /// or not. Captures by the provided instruction are considered if the
  54. /// final parameter is true.
  55. /// MaxUsesToExplore specifies how many uses the analysis should explore for
  56. /// one value before giving up due too "too many uses". If MaxUsesToExplore
  57. /// is zero, a default value is assumed.
  58. bool PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures,
  59. bool StoreCaptures, const Instruction *I,
  60. const DominatorTree *DT,
  61. bool IncludeI = false,
  62. unsigned MaxUsesToExplore = 0,
  63. const LoopInfo *LI = nullptr);
  64. // Returns the 'earliest' instruction that captures \p V in \F. An instruction
  65. // A is considered earlier than instruction B, if A dominates B. If 2 escapes
  66. // do not dominate each other, the terminator of the common dominator is
  67. // chosen. If not all uses can be analyzed, the earliest escape is set to
  68. // the first instruction in the function entry block. If \p V does not escape,
  69. // nullptr is returned. Note that the caller of the function has to ensure
  70. // that the instruction the result value is compared against is not in a
  71. // cycle.
  72. Instruction *FindEarliestCapture(const Value *V, Function &F,
  73. bool ReturnCaptures, bool StoreCaptures,
  74. const DominatorTree &DT,
  75. unsigned MaxUsesToExplore = 0);
  76. /// This callback is used in conjunction with PointerMayBeCaptured. In
  77. /// addition to the interface here, you'll need to provide your own getters
  78. /// to see whether anything was captured.
  79. struct CaptureTracker {
  80. virtual ~CaptureTracker();
  81. /// tooManyUses - The depth of traversal has breached a limit. There may be
  82. /// capturing instructions that will not be passed into captured().
  83. virtual void tooManyUses() = 0;
  84. /// shouldExplore - This is the use of a value derived from the pointer.
  85. /// To prune the search (ie., assume that none of its users could possibly
  86. /// capture) return false. To search it, return true.
  87. ///
  88. /// U->getUser() is always an Instruction.
  89. virtual bool shouldExplore(const Use *U);
  90. /// captured - Information about the pointer was captured by the user of
  91. /// use U. Return true to stop the traversal or false to continue looking
  92. /// for more capturing instructions.
  93. virtual bool captured(const Use *U) = 0;
  94. /// isDereferenceableOrNull - Overload to allow clients with additional
  95. /// knowledge about pointer dereferenceability to provide it and thereby
  96. /// avoid conservative responses when a pointer is compared to null.
  97. virtual bool isDereferenceableOrNull(Value *O, const DataLayout &DL);
  98. };
  99. /// PointerMayBeCaptured - Visit the value and the values derived from it and
  100. /// find values which appear to be capturing the pointer value. This feeds
  101. /// results into and is controlled by the CaptureTracker object.
  102. /// MaxUsesToExplore specifies how many uses the analysis should explore for
  103. /// one value before giving up due too "too many uses". If MaxUsesToExplore
  104. /// is zero, a default value is assumed.
  105. void PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
  106. unsigned MaxUsesToExplore = 0);
  107. /// Returns true if the pointer is to a function-local object that never
  108. /// escapes from the function.
  109. bool isNonEscapingLocalObject(
  110. const Value *V,
  111. SmallDenseMap<const Value *, bool, 8> *IsCapturedCache = nullptr);
  112. } // end namespace llvm
  113. #endif
  114. #ifdef __GNUC__
  115. #pragma GCC diagnostic pop
  116. #endif