LLVM  13.0.0git
PtrState.h
Go to the documentation of this file.
1 //===- PtrState.h - ARC State for a Ptr -------------------------*- 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 //
9 // This file contains declarations for the ARC state associated with a ptr. It
10 // is only used by the ARC Sequence Dataflow computation. By separating this
11 // from the actual dataflow, it is easier to consider the mechanics of the ARC
12 // optimization separate from the actual predicates being used.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_PTRSTATE_H
17 #define LLVM_LIB_TRANSFORMS_OBJCARC_PTRSTATE_H
18 
19 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/Support/Compiler.h"
22 
23 namespace llvm {
24 
25 class BasicBlock;
26 class Instruction;
27 class MDNode;
28 class raw_ostream;
29 class Value;
30 
31 namespace objcarc {
32 
33 class ARCMDKindCache;
34 class BundledRetainClaimRVs;
35 class ProvenanceAnalysis;
36 
37 /// \enum Sequence
38 ///
39 /// A sequence of states that a pointer may go through in which an
40 /// objc_retain and objc_release are actually needed.
41 enum Sequence {
43  S_Retain, ///< objc_retain(x).
44  S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement.
45  S_Use, ///< any use of x.
46  S_Stop, ///< code motion is stopped.
47  S_MovableRelease ///< objc_release(x), !clang.imprecise_release.
48 };
49 
52 
53 /// Unidirectional information about either a
54 /// retain-decrement-use-release sequence or release-use-decrement-retain
55 /// reverse sequence.
56 struct RRInfo {
57  /// After an objc_retain, the reference count of the referenced
58  /// object is known to be positive. Similarly, before an objc_release, the
59  /// reference count of the referenced object is known to be positive. If
60  /// there are retain-release pairs in code regions where the retain count
61  /// is known to be positive, they can be eliminated, regardless of any side
62  /// effects between them.
63  ///
64  /// Also, a retain+release pair nested within another retain+release
65  /// pair all on the known same pointer value can be eliminated, regardless
66  /// of any intervening side effects.
67  ///
68  /// KnownSafe is true when either of these conditions is satisfied.
69  bool KnownSafe = false;
70 
71  /// True of the objc_release calls are all marked with the "tail" keyword.
72  bool IsTailCallRelease = false;
73 
74  /// If the Calls are objc_release calls and they all have a
75  /// clang.imprecise_release tag, this is the metadata tag.
76  MDNode *ReleaseMetadata = nullptr;
77 
78  /// For a top-down sequence, the set of objc_retains or
79  /// objc_retainBlocks. For bottom-up, the set of objc_releases.
81 
82  /// The set of optimal insert positions for moving calls in the opposite
83  /// sequence.
85 
86  /// If this is true, we cannot perform code motion but can still remove
87  /// retain/release pairs.
88  bool CFGHazardAfflicted = false;
89 
90  RRInfo() = default;
91 
92  void clear();
93 
94  /// Conservatively merge the two RRInfo. Returns true if a partial merge has
95  /// occurred, false otherwise.
96  bool Merge(const RRInfo &Other);
97 };
98 
99 /// This class summarizes several per-pointer runtime properties which
100 /// are propagated through the flow graph.
101 class PtrState {
102 protected:
103  /// True if the reference count is known to be incremented.
104  bool KnownPositiveRefCount = false;
105 
106  /// True if we've seen an opportunity for partial RR elimination, such as
107  /// pushing calls into a CFG triangle or into one side of a CFG diamond.
108  bool Partial = false;
109 
110  /// The current position in the sequence.
111  unsigned char Seq : 8;
112 
113  /// Unidirectional information about the current sequence.
115 
117 
118 public:
119  bool IsKnownSafe() const { return RRI.KnownSafe; }
120 
121  void SetKnownSafe(const bool NewValue) { RRI.KnownSafe = NewValue; }
122 
123  bool IsTailCallRelease() const { return RRI.IsTailCallRelease; }
124 
125  void SetTailCallRelease(const bool NewValue) {
126  RRI.IsTailCallRelease = NewValue;
127  }
128 
130  return RRI.ReleaseMetadata != nullptr;
131  }
132 
133  const MDNode *GetReleaseMetadata() const { return RRI.ReleaseMetadata; }
134 
135  void SetReleaseMetadata(MDNode *NewValue) { RRI.ReleaseMetadata = NewValue; }
136 
137  bool IsCFGHazardAfflicted() const { return RRI.CFGHazardAfflicted; }
138 
139  void SetCFGHazardAfflicted(const bool NewValue) {
140  RRI.CFGHazardAfflicted = NewValue;
141  }
142 
145 
147 
148  void SetSeq(Sequence NewSeq);
149 
150  Sequence GetSeq() const { return static_cast<Sequence>(Seq); }
151 
153 
154  void ResetSequenceProgress(Sequence NewSeq);
155  void Merge(const PtrState &Other, bool TopDown);
156 
157  void InsertCall(Instruction *I) { RRI.Calls.insert(I); }
158 
160 
162 
163  bool HasReverseInsertPts() const { return !RRI.ReverseInsertPts.empty(); }
164 
165  const RRInfo &GetRRInfo() const { return RRI; }
166 };
167 
169  BottomUpPtrState() = default;
170 
171  /// (Re-)Initialize this bottom up pointer returning true if we detected a
172  /// pointer with nested releases.
173  bool InitBottomUp(ARCMDKindCache &Cache, Instruction *I);
174 
175  /// Return true if this set of releases can be paired with a release. Modifies
176  /// state appropriately to reflect that the matching occurred if it is
177  /// successful.
178  ///
179  /// It is assumed that one has already checked that the RCIdentity of the
180  /// retain and the RCIdentity of this ptr state are the same.
181  bool MatchWithRetain();
182 
183  void HandlePotentialUse(BasicBlock *BB, Instruction *Inst, const Value *Ptr,
184  ProvenanceAnalysis &PA, ARCInstKind Class);
185  bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr,
186  ProvenanceAnalysis &PA, ARCInstKind Class);
187 };
188 
190  TopDownPtrState() = default;
191 
192  /// (Re-)Initialize this bottom up pointer returning true if we detected a
193  /// pointer with nested releases.
195 
196  /// Return true if this set of retains can be paired with the given
197  /// release. Modifies state appropriately to reflect that the matching
198  /// occurred.
200 
201  void HandlePotentialUse(Instruction *Inst, const Value *Ptr,
202  ProvenanceAnalysis &PA, ARCInstKind Class);
203 
204  bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr,
205  ProvenanceAnalysis &PA, ARCInstKind Class,
206  const BundledRetainClaimRVs &BundledRVs);
207 };
208 
209 } // end namespace objcarc
210 
211 } // end namespace llvm
212 
213 #endif // LLVM_LIB_TRANSFORMS_OBJCARC_PTRSTATE_H
llvm::objcarc::BundledRetainClaimRVs
Definition: ObjCARC.h:106
llvm::objcarc::operator<<
raw_ostream & operator<<(raw_ostream &OS, const ARCInstKind Class)
Definition: ObjCARCInstKind.cpp:28
llvm
Definition: AllocatorList.h:23
llvm::objcarc::Sequence
Sequence
Definition: PtrState.h:41
llvm::objcarc::PtrState::GetReleaseMetadata
const MDNode * GetReleaseMetadata() const
Definition: PtrState.h:133
llvm::objcarc::PtrState::GetRRInfo
const RRInfo & GetRRInfo() const
Definition: PtrState.h:165
llvm::objcarc::PtrState::SetReleaseMetadata
void SetReleaseMetadata(MDNode *NewValue)
Definition: PtrState.h:135
llvm::objcarc::S_Use
@ S_Use
any use of x.
Definition: PtrState.h:45
llvm::objcarc::PtrState::Partial
bool Partial
True if we've seen an opportunity for partial RR elimination, such as pushing calls into a CFG triang...
Definition: PtrState.h:108
llvm::objcarc::PtrState::Seq
unsigned char Seq
The current position in the sequence.
Definition: PtrState.h:111
llvm::objcarc::PtrState::HasReverseInsertPts
bool HasReverseInsertPts() const
Definition: PtrState.h:163
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
llvm::objcarc::RRInfo::RRInfo
RRInfo()=default
llvm::objcarc::TopDownPtrState::MatchWithRelease
bool MatchWithRelease(ARCMDKindCache &Cache, Instruction *Release)
Return true if this set of retains can be paired with the given release.
Definition: PtrState.cpp:349
llvm::objcarc::ProvenanceAnalysis
This is similar to BasicAliasAnalysis, and it uses many of the same techniques, except it uses specia...
Definition: ProvenanceAnalysis.h:51
LLVM_ATTRIBUTE_UNUSED
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:188
llvm::objcarc::PtrState::SetTailCallRelease
void SetTailCallRelease(const bool NewValue)
Definition: PtrState.h:125
llvm::objcarc::PtrState::InsertCall
void InsertCall(Instruction *I)
Definition: PtrState.h:157
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::objcarc::PtrState::SetKnownPositiveRefCount
void SetKnownPositiveRefCount()
Definition: PtrState.cpp:124
llvm::objcarc::RRInfo
Unidirectional information about either a retain-decrement-use-release sequence or release-use-decrem...
Definition: PtrState.h:56
llvm::objcarc::TopDownPtrState::TopDownPtrState
TopDownPtrState()=default
llvm::objcarc::PtrState::IsCFGHazardAfflicted
bool IsCFGHazardAfflicted() const
Definition: PtrState.h:137
llvm::objcarc::PtrState::ResetSequenceProgress
void ResetSequenceProgress(Sequence NewSeq)
Definition: PtrState.cpp:140
llvm::objcarc::RRInfo::KnownSafe
bool KnownSafe
After an objc_retain, the reference count of the referenced object is known to be positive.
Definition: PtrState.h:69
llvm::Instruction
Definition: Instruction.h:45
llvm::objcarc::PtrState::InsertReverseInsertPt
void InsertReverseInsertPt(Instruction *I)
Definition: PtrState.h:159
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::objcarc::BottomUpPtrState
Definition: PtrState.h:168
llvm::objcarc::PtrState::ClearSequenceProgress
void ClearSequenceProgress()
Definition: PtrState.h:152
SmallPtrSet.h
llvm::objcarc::S_None
@ S_None
Definition: PtrState.h:42
llvm::objcarc::S_Retain
@ S_Retain
objc_retain(x).
Definition: PtrState.h:43
llvm::objcarc::RRInfo::ReverseInsertPts
SmallPtrSet< Instruction *, 2 > ReverseInsertPts
The set of optimal insert positions for moving calls in the opposite sequence.
Definition: PtrState.h:84
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::objcarc::ARCInstKind
ARCInstKind
Definition: ObjCARCInstKind.h:28
llvm::objcarc::BottomUpPtrState::BottomUpPtrState
BottomUpPtrState()=default
llvm::objcarc::PtrState::IsTailCallRelease
bool IsTailCallRelease() const
Definition: PtrState.h:123
llvm::objcarc::TopDownPtrState::HandlePotentialUse
void HandlePotentialUse(Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
Definition: PtrState.cpp:416
llvm::objcarc::PtrState::IsTrackingImpreciseReleases
bool IsTrackingImpreciseReleases() const
Definition: PtrState.h:129
llvm::objcarc::S_Stop
@ S_Stop
code motion is stopped.
Definition: PtrState.h:46
llvm::objcarc::BottomUpPtrState::HandlePotentialAlterRefCount
bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
Definition: PtrState.cpp:226
llvm::objcarc::PtrState::SetCFGHazardAfflicted
void SetCFGHazardAfflicted(const bool NewValue)
Definition: PtrState.h:139
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::objcarc::PtrState::PtrState
PtrState()
Definition: PtrState.h:116
llvm::objcarc::PtrState::Merge
void Merge(const PtrState &Other, bool TopDown)
Definition: PtrState.cpp:147
ObjCARCInstKind.h
llvm::objcarc::RRInfo::IsTailCallRelease
bool IsTailCallRelease
True of the objc_release calls are all marked with the "tail" keyword.
Definition: PtrState.h:72
llvm::objcarc::BottomUpPtrState::HandlePotentialUse
void HandlePotentialUse(BasicBlock *BB, Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
Definition: PtrState.cpp:253
llvm::objcarc::S_MovableRelease
@ S_MovableRelease
objc_release(x), !clang.imprecise_release.
Definition: PtrState.h:47
llvm::objcarc::PtrState::SetSeq
void SetSeq(Sequence NewSeq)
Definition: PtrState.cpp:134
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:64
llvm::MDNode
Metadata node.
Definition: Metadata.h:897
llvm::objcarc::RRInfo::CFGHazardAfflicted
bool CFGHazardAfflicted
If this is true, we cannot perform code motion but can still remove retain/release pairs.
Definition: PtrState.h:88
llvm::objcarc::ARCMDKindCache
A cache of MDKinds used by various ARC optimizations.
Definition: ObjCARCAnalysisUtils.h:226
llvm::objcarc::PtrState::IsKnownSafe
bool IsKnownSafe() const
Definition: PtrState.h:119
llvm::objcarc::RRInfo::ReleaseMetadata
MDNode * ReleaseMetadata
If the Calls are objc_release calls and they all have a clang.imprecise_release tag,...
Definition: PtrState.h:76
Compiler.h
llvm::objcarc::RRInfo::clear
void clear()
Definition: PtrState.cpp:90
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::objcarc::PtrState::GetSeq
Sequence GetSeq() const
Definition: PtrState.h:150
llvm::objcarc::RRInfo::Calls
SmallPtrSet< Instruction *, 2 > Calls
For a top-down sequence, the set of objc_retains or objc_retainBlocks.
Definition: PtrState.h:80
llvm::objcarc::PtrState::KnownPositiveRefCount
bool KnownPositiveRefCount
True if the reference count is known to be incremented.
Definition: PtrState.h:104
llvm::objcarc::PtrState::ClearReverseInsertPts
void ClearReverseInsertPts()
Definition: PtrState.h:161
llvm::objcarc::PtrState::RRI
RRInfo RRI
Unidirectional information about the current sequence.
Definition: PtrState.h:114
llvm::objcarc::BottomUpPtrState::MatchWithRetain
bool MatchWithRetain()
Return true if this set of releases can be paired with a release.
Definition: PtrState.cpp:203
llvm::objcarc::TopDownPtrState
Definition: PtrState.h:189
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::objcarc::BottomUpPtrState::InitBottomUp
bool InitBottomUp(ARCMDKindCache &Cache, Instruction *I)
(Re-)Initialize this bottom up pointer returning true if we detected a pointer with nested releases.
Definition: PtrState.cpp:174
llvm::objcarc::PtrState::ClearKnownPositiveRefCount
void ClearKnownPositiveRefCount()
Definition: PtrState.cpp:129
llvm::objcarc::PtrState::SetKnownSafe
void SetKnownSafe(const bool NewValue)
Definition: PtrState.h:121
llvm::objcarc::TopDownPtrState::HandlePotentialAlterRefCount
bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class, const BundledRetainClaimRVs &BundledRVs)
Definition: PtrState.cpp:377
llvm::InliningAdvisorMode::Release
@ Release
llvm::objcarc::S_CanRelease
@ S_CanRelease
foo(x) – x could possibly see a ref count decrement.
Definition: PtrState.h:44
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::objcarc::PtrState
This class summarizes several per-pointer runtime properties which are propagated through the flow gr...
Definition: PtrState.h:101
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::objcarc::PtrState::HasKnownPositiveRefCount
bool HasKnownPositiveRefCount() const
Definition: PtrState.h:146
llvm::objcarc::RRInfo::Merge
bool Merge(const RRInfo &Other)
Conservatively merge the two RRInfo.
Definition: PtrState.cpp:99
llvm::objcarc::TopDownPtrState::InitTopDown
bool InitTopDown(ARCInstKind Kind, Instruction *I)
(Re-)Initialize this bottom up pointer returning true if we detected a pointer with nested releases.
Definition: PtrState.cpp:324