blob: ed89c8c8fc893a3cf19b34a27573fbec6ebe2411 [file] [log] [blame]
Michael Gottesman6eb95dc2013-07-10 18:49:00 +00001//===- DependencyAnalysis.h - ObjC ARC Optimization ---*- C++ -*-----------===//
Michael Gottesman778138e2013-01-29 03:03:03 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Michael Gottesman778138e2013-01-29 03:03:03 +00006//
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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000022#ifndef LLVM_LIB_TRANSFORMS_OBJCARC_DEPENDENCYANALYSIS_H
23#define LLVM_LIB_TRANSFORMS_OBJCARC_DEPENDENCYANALYSIS_H
Michael Gottesman778138e2013-01-29 03:03:03 +000024
25#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer45275a42016-01-27 18:03:37 +000026#include "llvm/Analysis/ObjCARCInstKind.h"
Michael Gottesman778138e2013-01-29 03:03:03 +000027
28namespace llvm {
29 class BasicBlock;
30 class Instruction;
31 class Value;
32}
33
34namespace llvm {
35namespace objcarc {
36
37class ProvenanceAnalysis;
38
39/// \enum DependenceKind
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000040/// Defines different dependence kinds among various ARC constructs.
Michael Gottesman778138e2013-01-29 03:03:03 +000041///
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,
Craig Topper71b7b682014-08-21 05:55:13 +000056 SmallPtrSetImpl<Instruction *> &DependingInstructions,
57 SmallPtrSetImpl<const BasicBlock *> &Visited,
Michael Gottesman778138e2013-01-29 03:03:03 +000058 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.
Michael Gottesman6f729fa2015-02-19 19:51:32 +000066bool CanUse(const Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA,
67 ARCInstKind Class);
Michael Gottesman778138e2013-01-29 03:03:03 +000068
69/// Test whether the given instruction can result in a reference count
70/// modification (positive or negative) for the pointer's object.
Michael Gottesman6f729fa2015-02-19 19:51:32 +000071bool CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
72 ProvenanceAnalysis &PA, ARCInstKind Class);
Michael Gottesman778138e2013-01-29 03:03:03 +000073
Michael Gottesman5ab64de2015-02-20 00:02:45 +000074/// 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
Michael Gottesman0fc2acc2015-02-20 00:02:49 +000079static inline bool CanDecrementRefCount(const Instruction *Inst,
80 const Value *Ptr,
81 ProvenanceAnalysis &PA) {
82 return CanDecrementRefCount(Inst, Ptr, PA, GetARCInstKind(Inst));
83}
84
Michael Gottesman778138e2013-01-29 03:03:03 +000085} // namespace objcarc
86} // namespace llvm
87
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000088#endif