187419Sdes//===- DependencyAnalysis.h - ObjC ARC Optimization ---*- C++ -*-----------===//
287419Sdes//
387419Sdes// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
487419Sdes// See https://llvm.org/LICENSE.txt for license information.
587419Sdes// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
687419Sdes//
787419Sdes//===----------------------------------------------------------------------===//
8110608Sdes/// \file
987419Sdes///
1087419Sdes/// This file declares special dependency analysis routines used in Objective C
11170510Syar/// ARC Optimizations.
1287423Sdes///
13110608Sdes/// WARNING: This file knows about certain library functions. It recognizes them
14110608Sdes/// by name, and hardwires knowledge of their semantics.
15110608Sdes///
16110608Sdes/// WARNING: This file knows about how certain Objective-C library functions are
17110608Sdes/// used. Naive LLVM IR transformations which would otherwise be
18110608Sdes/// 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
28namespace llvm {
29  class BasicBlock;
30  class Instruction;
31  class Value;
32}
33
34namespace llvm {
35namespace objcarc {
36
37class 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///
44enum DependenceKind {
45  NeedsPositiveRetainCount,
46  AutoreleasePoolBoundary,
47  CanChangeRetainCount,
48  RetainAutoreleaseDep,       ///< Blocks objc_retainAutorelease.
49  RetainAutoreleaseRVDep,     ///< Blocks objc_retainAutoreleaseReturnValue.
50  RetainRVDep                 ///< Blocks objc_retainAutoreleasedReturnValue.
51};
52
53void FindDependencies(DependenceKind Flavor,
54                      const Value *Arg,
55                      BasicBlock *StartBB, Instruction *StartInst,
56                      SmallPtrSetImpl<Instruction *> &DependingInstructions,
57                      SmallPtrSetImpl<const BasicBlock *> &Visited,
58                      ProvenanceAnalysis &PA);
59
60bool
61Depends(DependenceKind Flavor, Instruction *Inst, const Value *Arg,
62        ProvenanceAnalysis &PA);
63
64/// Test whether the given instruction can "use" the given pointer's object in a
65/// way that requires the reference count to be positive.
66bool CanUse(const Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA,
67            ARCInstKind Class);
68
69/// Test whether the given instruction can result in a reference count
70/// modification (positive or negative) for the pointer's object.
71bool CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
72                      ProvenanceAnalysis &PA, ARCInstKind Class);
73
74/// Returns true if we can not conservatively prove that Inst can not decrement
75/// the reference count of Ptr. Returns false if we can.
76bool CanDecrementRefCount(const Instruction *Inst, const Value *Ptr,
77                          ProvenanceAnalysis &PA, ARCInstKind Class);
78
79static inline bool CanDecrementRefCount(const Instruction *Inst,
80                                        const Value *Ptr,
81                                        ProvenanceAnalysis &PA) {
82  return CanDecrementRefCount(Inst, Ptr, PA, GetARCInstKind(Inst));
83}
84
85} // namespace objcarc
86} // namespace llvm
87
88#endif
89