1 //===- DependencyAnalysis.h - ObjC ARC Optimization ---*- C++ -*-----------===//
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 /// \file
9 ///
10 /// This file declares special dependency analysis routines used in Objective C
11 /// ARC Optimizations.
12 ///
13 /// WARNING: This file knows about certain library functions. It recognizes them
14 /// by name, and hardwires knowledge of their semantics.
15 ///
16 /// WARNING: This file knows about how certain Objective-C library functions are
17 /// used. Naive LLVM IR transformations which would otherwise be
18 /// behavior-preserving may break these assumptions.
19 ///
20 //===----------------------------------------------------------------------===//
21 
22 #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_DEPENDENCYANALYSIS_H
23 #define LLVM_LIB_TRANSFORMS_OBJCARC_DEPENDENCYANALYSIS_H
24 
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/Analysis/ObjCARCInstKind.h"
27 
28 namespace llvm {
29   class BasicBlock;
30   class Instruction;
31   class Value;
32 }
33 
34 namespace llvm {
35 namespace objcarc {
36 
37 class ProvenanceAnalysis;
38 
39 /// \enum DependenceKind
40 /// Defines different dependence kinds among various ARC constructs.
41 ///
42 /// There are several kinds of dependence-like concepts in use here.
43 ///
44 enum DependenceKind {
45   NeedsPositiveRetainCount,
46   AutoreleasePoolBoundary,
47   CanChangeRetainCount,
48   RetainAutoreleaseDep,       ///< Blocks objc_retainAutorelease.
49   RetainAutoreleaseRVDep      ///< Blocks objc_retainAutoreleaseReturnValue.
50 };
51 
52 /// Find dependent instructions. If there is exactly one dependent instruction,
53 /// return it. Otherwise, return null.
54 llvm::Instruction *findSingleDependency(DependenceKind Flavor, const Value *Arg,
55                                         BasicBlock *StartBB,
56                                         Instruction *StartInst,
57                                         ProvenanceAnalysis &PA);
58 
59 bool
60 Depends(DependenceKind Flavor, Instruction *Inst, const Value *Arg,
61         ProvenanceAnalysis &PA);
62 
63 /// Test whether the given instruction can "use" the given pointer's object in a
64 /// way that requires the reference count to be positive.
65 bool CanUse(const Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA,
66             ARCInstKind Class);
67 
68 /// Test whether the given instruction can result in a reference count
69 /// modification (positive or negative) for the pointer's object.
70 bool CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
71                       ProvenanceAnalysis &PA, ARCInstKind Class);
72 
73 /// Returns true if we can not conservatively prove that Inst can not decrement
74 /// the reference count of Ptr. Returns false if we can.
75 bool CanDecrementRefCount(const Instruction *Inst, const Value *Ptr,
76                           ProvenanceAnalysis &PA, ARCInstKind Class);
77 
78 static inline bool CanDecrementRefCount(const Instruction *Inst,
79                                         const Value *Ptr,
80                                         ProvenanceAnalysis &PA) {
81   return CanDecrementRefCount(Inst, Ptr, PA, GetARCInstKind(Inst));
82 }
83 
84 } // namespace objcarc
85 } // namespace llvm
86 
87 #endif
88