Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:11

0001 //===----- llvm/Analysis/CaptureTracking.h - Pointer capture ----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains routines that help determine which pointers are captured.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_ANALYSIS_CAPTURETRACKING_H
0014 #define LLVM_ANALYSIS_CAPTURETRACKING_H
0015 
0016 #include "llvm/ADT/DenseMap.h"
0017 
0018 namespace llvm {
0019 
0020   class Value;
0021   class Use;
0022   class DataLayout;
0023   class Instruction;
0024   class DominatorTree;
0025   class LoopInfo;
0026   class Function;
0027   template <typename Fn> class function_ref;
0028 
0029   /// getDefaultMaxUsesToExploreForCaptureTracking - Return default value of
0030   /// the maximal number of uses to explore before giving up. It is used by
0031   /// PointerMayBeCaptured family analysis.
0032   unsigned getDefaultMaxUsesToExploreForCaptureTracking();
0033 
0034   /// PointerMayBeCaptured - Return true if this pointer value may be captured
0035   /// by the enclosing function (which is required to exist).  This routine can
0036   /// be expensive, so consider caching the results.  The boolean ReturnCaptures
0037   /// specifies whether returning the value (or part of it) from the function
0038   /// counts as capturing it or not.  The boolean StoreCaptures specified
0039   /// whether storing the value (or part of it) into memory anywhere
0040   /// automatically counts as capturing it or not.
0041   /// MaxUsesToExplore specifies how many uses the analysis should explore for
0042   /// one value before giving up due too "too many uses". If MaxUsesToExplore
0043   /// is zero, a default value is assumed.
0044   bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures,
0045                             bool StoreCaptures, unsigned MaxUsesToExplore = 0);
0046 
0047   /// PointerMayBeCapturedBefore - Return true if this pointer value may be
0048   /// captured by the enclosing function (which is required to exist). If a
0049   /// DominatorTree is provided, only captures which happen before the given
0050   /// instruction are considered. This routine can be expensive, so consider
0051   /// caching the results.  The boolean ReturnCaptures specifies whether
0052   /// returning the value (or part of it) from the function counts as capturing
0053   /// it or not.  The boolean StoreCaptures specified whether storing the value
0054   /// (or part of it) into memory anywhere automatically counts as capturing it
0055   /// or not. Captures by the provided instruction are considered if the
0056   /// final parameter is true.
0057   /// MaxUsesToExplore specifies how many uses the analysis should explore for
0058   /// one value before giving up due too "too many uses". If MaxUsesToExplore
0059   /// is zero, a default value is assumed.
0060   bool PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures,
0061                                   bool StoreCaptures, const Instruction *I,
0062                                   const DominatorTree *DT,
0063                                   bool IncludeI = false,
0064                                   unsigned MaxUsesToExplore = 0,
0065                                   const LoopInfo *LI = nullptr);
0066 
0067   // Returns the 'earliest' instruction that captures \p V in \F. An instruction
0068   // A is considered earlier than instruction B, if A dominates B. If 2 escapes
0069   // do not dominate each other, the terminator of the common dominator is
0070   // chosen. If not all uses can be analyzed, the earliest escape is set to
0071   // the first instruction in the function entry block. If \p V does not escape,
0072   // nullptr is returned. Note that the caller of the function has to ensure
0073   // that the instruction the result value is compared against is not in a
0074   // cycle.
0075   Instruction *FindEarliestCapture(const Value *V, Function &F,
0076                                    bool ReturnCaptures, bool StoreCaptures,
0077                                    const DominatorTree &DT,
0078                                    unsigned MaxUsesToExplore = 0);
0079 
0080   /// This callback is used in conjunction with PointerMayBeCaptured. In
0081   /// addition to the interface here, you'll need to provide your own getters
0082   /// to see whether anything was captured.
0083   struct CaptureTracker {
0084     virtual ~CaptureTracker();
0085 
0086     /// tooManyUses - The depth of traversal has breached a limit. There may be
0087     /// capturing instructions that will not be passed into captured().
0088     virtual void tooManyUses() = 0;
0089 
0090     /// shouldExplore - This is the use of a value derived from the pointer.
0091     /// To prune the search (ie., assume that none of its users could possibly
0092     /// capture) return false. To search it, return true.
0093     ///
0094     /// U->getUser() is always an Instruction.
0095     virtual bool shouldExplore(const Use *U);
0096 
0097     /// captured - Information about the pointer was captured by the user of
0098     /// use U. Return true to stop the traversal or false to continue looking
0099     /// for more capturing instructions.
0100     virtual bool captured(const Use *U) = 0;
0101 
0102     /// isDereferenceableOrNull - Overload to allow clients with additional
0103     /// knowledge about pointer dereferenceability to provide it and thereby
0104     /// avoid conservative responses when a pointer is compared to null.
0105     virtual bool isDereferenceableOrNull(Value *O, const DataLayout &DL);
0106   };
0107 
0108   /// Types of use capture kinds, see \p DetermineUseCaptureKind.
0109   enum class UseCaptureKind {
0110     NO_CAPTURE,
0111     MAY_CAPTURE,
0112     PASSTHROUGH,
0113   };
0114 
0115   /// Determine what kind of capture behaviour \p U may exhibit.
0116   ///
0117   /// A use can be no-capture, a use can potentially capture, or a use can be
0118   /// passthrough such that the uses of the user or \p U should be inspected.
0119   /// The \p IsDereferenceableOrNull callback is used to rule out capturing for
0120   /// certain comparisons.
0121   UseCaptureKind
0122   DetermineUseCaptureKind(const Use &U,
0123                           llvm::function_ref<bool(Value *, const DataLayout &)>
0124                               IsDereferenceableOrNull);
0125 
0126   /// PointerMayBeCaptured - Visit the value and the values derived from it and
0127   /// find values which appear to be capturing the pointer value. This feeds
0128   /// results into and is controlled by the CaptureTracker object.
0129   /// MaxUsesToExplore specifies how many uses the analysis should explore for
0130   /// one value before giving up due too "too many uses". If MaxUsesToExplore
0131   /// is zero, a default value is assumed.
0132   void PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
0133                             unsigned MaxUsesToExplore = 0);
0134 
0135   /// Returns true if the pointer is to a function-local object that never
0136   /// escapes from the function.
0137   bool isNonEscapingLocalObject(
0138       const Value *V,
0139       SmallDenseMap<const Value *, bool, 8> *IsCapturedCache = nullptr);
0140 } // end namespace llvm
0141 
0142 #endif