CaptureTracking.h 5.2 KB

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