comparison lib/Transforms/ObjCARC/ObjCARCOpts.cpp @ 95:afa8332a0e37 LLVM3.8

LLVM 3.8
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Tue, 13 Oct 2015 17:48:58 +0900
parents 60c9769439b8
children 7d135dc70f03
comparison
equal deleted inserted replaced
84:f3e34b893a5f 95:afa8332a0e37
24 /// 24 ///
25 //===----------------------------------------------------------------------===// 25 //===----------------------------------------------------------------------===//
26 26
27 #include "ObjCARC.h" 27 #include "ObjCARC.h"
28 #include "ARCRuntimeEntryPoints.h" 28 #include "ARCRuntimeEntryPoints.h"
29 #include "BlotMapVector.h"
29 #include "DependencyAnalysis.h" 30 #include "DependencyAnalysis.h"
30 #include "ObjCARCAliasAnalysis.h"
31 #include "ProvenanceAnalysis.h" 31 #include "ProvenanceAnalysis.h"
32 #include "PtrState.h"
32 #include "llvm/ADT/DenseMap.h" 33 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/DenseSet.h" 34 #include "llvm/ADT/DenseSet.h"
34 #include "llvm/ADT/STLExtras.h" 35 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/ADT/SmallPtrSet.h" 36 #include "llvm/ADT/SmallPtrSet.h"
36 #include "llvm/ADT/Statistic.h" 37 #include "llvm/ADT/Statistic.h"
38 #include "llvm/Analysis/ObjCARCAliasAnalysis.h"
37 #include "llvm/IR/CFG.h" 39 #include "llvm/IR/CFG.h"
38 #include "llvm/IR/IRBuilder.h" 40 #include "llvm/IR/IRBuilder.h"
39 #include "llvm/IR/LLVMContext.h" 41 #include "llvm/IR/LLVMContext.h"
40 #include "llvm/Support/Debug.h" 42 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/raw_ostream.h" 43 #include "llvm/Support/raw_ostream.h"
43 using namespace llvm; 45 using namespace llvm;
44 using namespace llvm::objcarc; 46 using namespace llvm::objcarc;
45 47
46 #define DEBUG_TYPE "objc-arc-opts" 48 #define DEBUG_TYPE "objc-arc-opts"
47 49
48 /// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific.
49 /// @{
50
51 namespace {
52 /// \brief An associative container with fast insertion-order (deterministic)
53 /// iteration over its elements. Plus the special blot operation.
54 template<class KeyT, class ValueT>
55 class MapVector {
56 /// Map keys to indices in Vector.
57 typedef DenseMap<KeyT, size_t> MapTy;
58 MapTy Map;
59
60 typedef std::vector<std::pair<KeyT, ValueT> > VectorTy;
61 /// Keys and values.
62 VectorTy Vector;
63
64 public:
65 typedef typename VectorTy::iterator iterator;
66 typedef typename VectorTy::const_iterator const_iterator;
67 iterator begin() { return Vector.begin(); }
68 iterator end() { return Vector.end(); }
69 const_iterator begin() const { return Vector.begin(); }
70 const_iterator end() const { return Vector.end(); }
71
72 #ifdef XDEBUG
73 ~MapVector() {
74 assert(Vector.size() >= Map.size()); // May differ due to blotting.
75 for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
76 I != E; ++I) {
77 assert(I->second < Vector.size());
78 assert(Vector[I->second].first == I->first);
79 }
80 for (typename VectorTy::const_iterator I = Vector.begin(),
81 E = Vector.end(); I != E; ++I)
82 assert(!I->first ||
83 (Map.count(I->first) &&
84 Map[I->first] == size_t(I - Vector.begin())));
85 }
86 #endif
87
88 ValueT &operator[](const KeyT &Arg) {
89 std::pair<typename MapTy::iterator, bool> Pair =
90 Map.insert(std::make_pair(Arg, size_t(0)));
91 if (Pair.second) {
92 size_t Num = Vector.size();
93 Pair.first->second = Num;
94 Vector.push_back(std::make_pair(Arg, ValueT()));
95 return Vector[Num].second;
96 }
97 return Vector[Pair.first->second].second;
98 }
99
100 std::pair<iterator, bool>
101 insert(const std::pair<KeyT, ValueT> &InsertPair) {
102 std::pair<typename MapTy::iterator, bool> Pair =
103 Map.insert(std::make_pair(InsertPair.first, size_t(0)));
104 if (Pair.second) {
105 size_t Num = Vector.size();
106 Pair.first->second = Num;
107 Vector.push_back(InsertPair);
108 return std::make_pair(Vector.begin() + Num, true);
109 }
110 return std::make_pair(Vector.begin() + Pair.first->second, false);
111 }
112
113 iterator find(const KeyT &Key) {
114 typename MapTy::iterator It = Map.find(Key);
115 if (It == Map.end()) return Vector.end();
116 return Vector.begin() + It->second;
117 }
118
119 const_iterator find(const KeyT &Key) const {
120 typename MapTy::const_iterator It = Map.find(Key);
121 if (It == Map.end()) return Vector.end();
122 return Vector.begin() + It->second;
123 }
124
125 /// This is similar to erase, but instead of removing the element from the
126 /// vector, it just zeros out the key in the vector. This leaves iterators
127 /// intact, but clients must be prepared for zeroed-out keys when iterating.
128 void blot(const KeyT &Key) {
129 typename MapTy::iterator It = Map.find(Key);
130 if (It == Map.end()) return;
131 Vector[It->second].first = KeyT();
132 Map.erase(It);
133 }
134
135 void clear() {
136 Map.clear();
137 Vector.clear();
138 }
139 };
140 }
141
142 /// @}
143 ///
144 /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC. 50 /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
145 /// @{ 51 /// @{
146 52
147 /// \brief This is similar to StripPointerCastsAndObjCCalls but it stops as soon 53 /// \brief This is similar to GetRCIdentityRoot but it stops as soon
148 /// as it finds a value with multiple uses. 54 /// as it finds a value with multiple uses.
149 static const Value *FindSingleUseIdentifiedObject(const Value *Arg) { 55 static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
150 if (Arg->hasOneUse()) { 56 if (Arg->hasOneUse()) {
151 if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg)) 57 if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
152 return FindSingleUseIdentifiedObject(BC->getOperand(0)); 58 return FindSingleUseIdentifiedObject(BC->getOperand(0));
153 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg)) 59 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
154 if (GEP->hasAllZeroIndices()) 60 if (GEP->hasAllZeroIndices())
155 return FindSingleUseIdentifiedObject(GEP->getPointerOperand()); 61 return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
156 if (IsForwarding(GetBasicInstructionClass(Arg))) 62 if (IsForwarding(GetBasicARCInstKind(Arg)))
157 return FindSingleUseIdentifiedObject( 63 return FindSingleUseIdentifiedObject(
158 cast<CallInst>(Arg)->getArgOperand(0)); 64 cast<CallInst>(Arg)->getArgOperand(0));
159 if (!IsObjCIdentifiedObject(Arg)) 65 if (!IsObjCIdentifiedObject(Arg))
160 return nullptr; 66 return nullptr;
161 return Arg; 67 return Arg;
163 69
164 // If we found an identifiable object but it has multiple uses, but they are 70 // If we found an identifiable object but it has multiple uses, but they are
165 // trivial uses, we can still consider this to be a single-use value. 71 // trivial uses, we can still consider this to be a single-use value.
166 if (IsObjCIdentifiedObject(Arg)) { 72 if (IsObjCIdentifiedObject(Arg)) {
167 for (const User *U : Arg->users()) 73 for (const User *U : Arg->users())
168 if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg) 74 if (!U->use_empty() || GetRCIdentityRoot(U) != Arg)
169 return nullptr; 75 return nullptr;
170 76
171 return Arg; 77 return Arg;
172 } 78 }
173 79
175 } 81 }
176 82
177 /// This is a wrapper around getUnderlyingObjCPtr along the lines of 83 /// This is a wrapper around getUnderlyingObjCPtr along the lines of
178 /// GetUnderlyingObjects except that it returns early when it sees the first 84 /// GetUnderlyingObjects except that it returns early when it sees the first
179 /// alloca. 85 /// alloca.
180 static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V) { 86 static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V,
87 const DataLayout &DL) {
181 SmallPtrSet<const Value *, 4> Visited; 88 SmallPtrSet<const Value *, 4> Visited;
182 SmallVector<const Value *, 4> Worklist; 89 SmallVector<const Value *, 4> Worklist;
183 Worklist.push_back(V); 90 Worklist.push_back(V);
184 do { 91 do {
185 const Value *P = Worklist.pop_back_val(); 92 const Value *P = Worklist.pop_back_val();
186 P = GetUnderlyingObjCPtr(P); 93 P = GetUnderlyingObjCPtr(P, DL);
187 94
188 if (isa<AllocaInst>(P)) 95 if (isa<AllocaInst>(P))
189 return true; 96 return true;
190 97
191 if (!Visited.insert(P).second) 98 if (!Visited.insert(P).second)
196 Worklist.push_back(SI->getFalseValue()); 103 Worklist.push_back(SI->getFalseValue());
197 continue; 104 continue;
198 } 105 }
199 106
200 if (const PHINode *PN = dyn_cast<const PHINode>(P)) { 107 if (const PHINode *PN = dyn_cast<const PHINode>(P)) {
201 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 108 for (Value *IncValue : PN->incoming_values())
202 Worklist.push_back(PN->getIncomingValue(i)); 109 Worklist.push_back(IncValue);
203 continue; 110 continue;
204 } 111 }
205 } while (!Worklist.empty()); 112 } while (!Worklist.empty());
206 113
207 return false; 114 return false;
268 STATISTIC(NumReleasesAfterOpt, 175 STATISTIC(NumReleasesAfterOpt,
269 "Number of releases after optimization"); 176 "Number of releases after optimization");
270 #endif 177 #endif
271 178
272 namespace { 179 namespace {
273 /// \enum Sequence
274 ///
275 /// \brief A sequence of states that a pointer may go through in which an
276 /// objc_retain and objc_release are actually needed.
277 enum Sequence {
278 S_None,
279 S_Retain, ///< objc_retain(x).
280 S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement.
281 S_Use, ///< any use of x.
282 S_Stop, ///< like S_Release, but code motion is stopped.
283 S_Release, ///< objc_release(x).
284 S_MovableRelease ///< objc_release(x), !clang.imprecise_release.
285 };
286
287 raw_ostream &operator<<(raw_ostream &OS, const Sequence S)
288 LLVM_ATTRIBUTE_UNUSED;
289 raw_ostream &operator<<(raw_ostream &OS, const Sequence S) {
290 switch (S) {
291 case S_None:
292 return OS << "S_None";
293 case S_Retain:
294 return OS << "S_Retain";
295 case S_CanRelease:
296 return OS << "S_CanRelease";
297 case S_Use:
298 return OS << "S_Use";
299 case S_Release:
300 return OS << "S_Release";
301 case S_MovableRelease:
302 return OS << "S_MovableRelease";
303 case S_Stop:
304 return OS << "S_Stop";
305 }
306 llvm_unreachable("Unknown sequence type.");
307 }
308 }
309
310 static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
311 // The easy cases.
312 if (A == B)
313 return A;
314 if (A == S_None || B == S_None)
315 return S_None;
316
317 if (A > B) std::swap(A, B);
318 if (TopDown) {
319 // Choose the side which is further along in the sequence.
320 if ((A == S_Retain || A == S_CanRelease) &&
321 (B == S_CanRelease || B == S_Use))
322 return B;
323 } else {
324 // Choose the side which is further along in the sequence.
325 if ((A == S_Use || A == S_CanRelease) &&
326 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
327 return A;
328 // If both sides are releases, choose the more conservative one.
329 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
330 return A;
331 if (A == S_Release && B == S_MovableRelease)
332 return A;
333 }
334
335 return S_None;
336 }
337
338 namespace {
339 /// \brief Unidirectional information about either a
340 /// retain-decrement-use-release sequence or release-use-decrement-retain
341 /// reverse sequence.
342 struct RRInfo {
343 /// After an objc_retain, the reference count of the referenced
344 /// object is known to be positive. Similarly, before an objc_release, the
345 /// reference count of the referenced object is known to be positive. If
346 /// there are retain-release pairs in code regions where the retain count
347 /// is known to be positive, they can be eliminated, regardless of any side
348 /// effects between them.
349 ///
350 /// Also, a retain+release pair nested within another retain+release
351 /// pair all on the known same pointer value can be eliminated, regardless
352 /// of any intervening side effects.
353 ///
354 /// KnownSafe is true when either of these conditions is satisfied.
355 bool KnownSafe;
356
357 /// True of the objc_release calls are all marked with the "tail" keyword.
358 bool IsTailCallRelease;
359
360 /// If the Calls are objc_release calls and they all have a
361 /// clang.imprecise_release tag, this is the metadata tag.
362 MDNode *ReleaseMetadata;
363
364 /// For a top-down sequence, the set of objc_retains or
365 /// objc_retainBlocks. For bottom-up, the set of objc_releases.
366 SmallPtrSet<Instruction *, 2> Calls;
367
368 /// The set of optimal insert positions for moving calls in the opposite
369 /// sequence.
370 SmallPtrSet<Instruction *, 2> ReverseInsertPts;
371
372 /// If this is true, we cannot perform code motion but can still remove
373 /// retain/release pairs.
374 bool CFGHazardAfflicted;
375
376 RRInfo() :
377 KnownSafe(false), IsTailCallRelease(false), ReleaseMetadata(nullptr),
378 CFGHazardAfflicted(false) {}
379
380 void clear();
381
382 /// Conservatively merge the two RRInfo. Returns true if a partial merge has
383 /// occurred, false otherwise.
384 bool Merge(const RRInfo &Other);
385
386 };
387 }
388
389 void RRInfo::clear() {
390 KnownSafe = false;
391 IsTailCallRelease = false;
392 ReleaseMetadata = nullptr;
393 Calls.clear();
394 ReverseInsertPts.clear();
395 CFGHazardAfflicted = false;
396 }
397
398 bool RRInfo::Merge(const RRInfo &Other) {
399 // Conservatively merge the ReleaseMetadata information.
400 if (ReleaseMetadata != Other.ReleaseMetadata)
401 ReleaseMetadata = nullptr;
402
403 // Conservatively merge the boolean state.
404 KnownSafe &= Other.KnownSafe;
405 IsTailCallRelease &= Other.IsTailCallRelease;
406 CFGHazardAfflicted |= Other.CFGHazardAfflicted;
407
408 // Merge the call sets.
409 Calls.insert(Other.Calls.begin(), Other.Calls.end());
410
411 // Merge the insert point sets. If there are any differences,
412 // that makes this a partial merge.
413 bool Partial = ReverseInsertPts.size() != Other.ReverseInsertPts.size();
414 for (Instruction *Inst : Other.ReverseInsertPts)
415 Partial |= ReverseInsertPts.insert(Inst).second;
416 return Partial;
417 }
418
419 namespace {
420 /// \brief This class summarizes several per-pointer runtime properties which
421 /// are propogated through the flow graph.
422 class PtrState {
423 /// True if the reference count is known to be incremented.
424 bool KnownPositiveRefCount;
425
426 /// True if we've seen an opportunity for partial RR elimination, such as
427 /// pushing calls into a CFG triangle or into one side of a CFG diamond.
428 bool Partial;
429
430 /// The current position in the sequence.
431 unsigned char Seq : 8;
432
433 /// Unidirectional information about the current sequence.
434 RRInfo RRI;
435
436 public:
437 PtrState() : KnownPositiveRefCount(false), Partial(false),
438 Seq(S_None) {}
439
440
441 bool IsKnownSafe() const {
442 return RRI.KnownSafe;
443 }
444
445 void SetKnownSafe(const bool NewValue) {
446 RRI.KnownSafe = NewValue;
447 }
448
449 bool IsTailCallRelease() const {
450 return RRI.IsTailCallRelease;
451 }
452
453 void SetTailCallRelease(const bool NewValue) {
454 RRI.IsTailCallRelease = NewValue;
455 }
456
457 bool IsTrackingImpreciseReleases() const {
458 return RRI.ReleaseMetadata != nullptr;
459 }
460
461 const MDNode *GetReleaseMetadata() const {
462 return RRI.ReleaseMetadata;
463 }
464
465 void SetReleaseMetadata(MDNode *NewValue) {
466 RRI.ReleaseMetadata = NewValue;
467 }
468
469 bool IsCFGHazardAfflicted() const {
470 return RRI.CFGHazardAfflicted;
471 }
472
473 void SetCFGHazardAfflicted(const bool NewValue) {
474 RRI.CFGHazardAfflicted = NewValue;
475 }
476
477 void SetKnownPositiveRefCount() {
478 DEBUG(dbgs() << "Setting Known Positive.\n");
479 KnownPositiveRefCount = true;
480 }
481
482 void ClearKnownPositiveRefCount() {
483 DEBUG(dbgs() << "Clearing Known Positive.\n");
484 KnownPositiveRefCount = false;
485 }
486
487 bool HasKnownPositiveRefCount() const {
488 return KnownPositiveRefCount;
489 }
490
491 void SetSeq(Sequence NewSeq) {
492 DEBUG(dbgs() << "Old: " << Seq << "; New: " << NewSeq << "\n");
493 Seq = NewSeq;
494 }
495
496 Sequence GetSeq() const {
497 return static_cast<Sequence>(Seq);
498 }
499
500 void ClearSequenceProgress() {
501 ResetSequenceProgress(S_None);
502 }
503
504 void ResetSequenceProgress(Sequence NewSeq) {
505 DEBUG(dbgs() << "Resetting sequence progress.\n");
506 SetSeq(NewSeq);
507 Partial = false;
508 RRI.clear();
509 }
510
511 void Merge(const PtrState &Other, bool TopDown);
512
513 void InsertCall(Instruction *I) {
514 RRI.Calls.insert(I);
515 }
516
517 void InsertReverseInsertPt(Instruction *I) {
518 RRI.ReverseInsertPts.insert(I);
519 }
520
521 void ClearReverseInsertPts() {
522 RRI.ReverseInsertPts.clear();
523 }
524
525 bool HasReverseInsertPts() const {
526 return !RRI.ReverseInsertPts.empty();
527 }
528
529 const RRInfo &GetRRInfo() const {
530 return RRI;
531 }
532 };
533 }
534
535 void
536 PtrState::Merge(const PtrState &Other, bool TopDown) {
537 Seq = MergeSeqs(GetSeq(), Other.GetSeq(), TopDown);
538 KnownPositiveRefCount &= Other.KnownPositiveRefCount;
539
540 // If we're not in a sequence (anymore), drop all associated state.
541 if (Seq == S_None) {
542 Partial = false;
543 RRI.clear();
544 } else if (Partial || Other.Partial) {
545 // If we're doing a merge on a path that's previously seen a partial
546 // merge, conservatively drop the sequence, to avoid doing partial
547 // RR elimination. If the branch predicates for the two merge differ,
548 // mixing them is unsafe.
549 ClearSequenceProgress();
550 } else {
551 // Otherwise merge the other PtrState's RRInfo into our RRInfo. At this
552 // point, we know that currently we are not partial. Stash whether or not
553 // the merge operation caused us to undergo a partial merging of reverse
554 // insertion points.
555 Partial = RRI.Merge(Other.RRI);
556 }
557 }
558
559 namespace {
560 /// \brief Per-BasicBlock state. 180 /// \brief Per-BasicBlock state.
561 class BBState { 181 class BBState {
562 /// The number of unique control paths from the entry which can reach this 182 /// The number of unique control paths from the entry which can reach this
563 /// block. 183 /// block.
564 unsigned TopDownPathCount; 184 unsigned TopDownPathCount;
565 185
566 /// The number of unique control paths to exits from this block. 186 /// The number of unique control paths to exits from this block.
567 unsigned BottomUpPathCount; 187 unsigned BottomUpPathCount;
568 188
569 /// A type for PerPtrTopDown and PerPtrBottomUp.
570 typedef MapVector<const Value *, PtrState> MapTy;
571
572 /// The top-down traversal uses this to record information known about a 189 /// The top-down traversal uses this to record information known about a
573 /// pointer at the bottom of each block. 190 /// pointer at the bottom of each block.
574 MapTy PerPtrTopDown; 191 BlotMapVector<const Value *, TopDownPtrState> PerPtrTopDown;
575 192
576 /// The bottom-up traversal uses this to record information known about a 193 /// The bottom-up traversal uses this to record information known about a
577 /// pointer at the top of each block. 194 /// pointer at the top of each block.
578 MapTy PerPtrBottomUp; 195 BlotMapVector<const Value *, BottomUpPtrState> PerPtrBottomUp;
579 196
580 /// Effective predecessors of the current block ignoring ignorable edges and 197 /// Effective predecessors of the current block ignoring ignorable edges and
581 /// ignored backedges. 198 /// ignored backedges.
582 SmallVector<BasicBlock *, 2> Preds; 199 SmallVector<BasicBlock *, 2> Preds;
200
583 /// Effective successors of the current block ignoring ignorable edges and 201 /// Effective successors of the current block ignoring ignorable edges and
584 /// ignored backedges. 202 /// ignored backedges.
585 SmallVector<BasicBlock *, 2> Succs; 203 SmallVector<BasicBlock *, 2> Succs;
586 204
587 public: 205 public:
588 static const unsigned OverflowOccurredValue; 206 static const unsigned OverflowOccurredValue;
589 207
590 BBState() : TopDownPathCount(0), BottomUpPathCount(0) { } 208 BBState() : TopDownPathCount(0), BottomUpPathCount(0) { }
591 209
592 typedef MapTy::iterator ptr_iterator; 210 typedef decltype(PerPtrTopDown)::iterator top_down_ptr_iterator;
593 typedef MapTy::const_iterator ptr_const_iterator; 211 typedef decltype(PerPtrTopDown)::const_iterator const_top_down_ptr_iterator;
594 212
595 ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); } 213 top_down_ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
596 ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); } 214 top_down_ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
597 ptr_const_iterator top_down_ptr_begin() const { 215 const_top_down_ptr_iterator top_down_ptr_begin() const {
598 return PerPtrTopDown.begin(); 216 return PerPtrTopDown.begin();
599 } 217 }
600 ptr_const_iterator top_down_ptr_end() const { 218 const_top_down_ptr_iterator top_down_ptr_end() const {
601 return PerPtrTopDown.end(); 219 return PerPtrTopDown.end();
602 } 220 }
603 221 bool hasTopDownPtrs() const {
604 ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); } 222 return !PerPtrTopDown.empty();
605 ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); } 223 }
606 ptr_const_iterator bottom_up_ptr_begin() const { 224
225 typedef decltype(PerPtrBottomUp)::iterator bottom_up_ptr_iterator;
226 typedef decltype(
227 PerPtrBottomUp)::const_iterator const_bottom_up_ptr_iterator;
228
229 bottom_up_ptr_iterator bottom_up_ptr_begin() {
607 return PerPtrBottomUp.begin(); 230 return PerPtrBottomUp.begin();
608 } 231 }
609 ptr_const_iterator bottom_up_ptr_end() const { 232 bottom_up_ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
233 const_bottom_up_ptr_iterator bottom_up_ptr_begin() const {
234 return PerPtrBottomUp.begin();
235 }
236 const_bottom_up_ptr_iterator bottom_up_ptr_end() const {
610 return PerPtrBottomUp.end(); 237 return PerPtrBottomUp.end();
238 }
239 bool hasBottomUpPtrs() const {
240 return !PerPtrBottomUp.empty();
611 } 241 }
612 242
613 /// Mark this block as being an entry block, which has one path from the 243 /// Mark this block as being an entry block, which has one path from the
614 /// entry by definition. 244 /// entry by definition.
615 void SetAsEntry() { TopDownPathCount = 1; } 245 void SetAsEntry() { TopDownPathCount = 1; }
619 void SetAsExit() { BottomUpPathCount = 1; } 249 void SetAsExit() { BottomUpPathCount = 1; }
620 250
621 /// Attempt to find the PtrState object describing the top down state for 251 /// Attempt to find the PtrState object describing the top down state for
622 /// pointer Arg. Return a new initialized PtrState describing the top down 252 /// pointer Arg. Return a new initialized PtrState describing the top down
623 /// state for Arg if we do not find one. 253 /// state for Arg if we do not find one.
624 PtrState &getPtrTopDownState(const Value *Arg) { 254 TopDownPtrState &getPtrTopDownState(const Value *Arg) {
625 return PerPtrTopDown[Arg]; 255 return PerPtrTopDown[Arg];
626 } 256 }
627 257
628 /// Attempt to find the PtrState object describing the bottom up state for 258 /// Attempt to find the PtrState object describing the bottom up state for
629 /// pointer Arg. Return a new initialized PtrState describing the bottom up 259 /// pointer Arg. Return a new initialized PtrState describing the bottom up
630 /// state for Arg if we do not find one. 260 /// state for Arg if we do not find one.
631 PtrState &getPtrBottomUpState(const Value *Arg) { 261 BottomUpPtrState &getPtrBottomUpState(const Value *Arg) {
632 return PerPtrBottomUp[Arg]; 262 return PerPtrBottomUp[Arg];
633 } 263 }
634 264
635 /// Attempt to find the PtrState object describing the bottom up state for 265 /// Attempt to find the PtrState object describing the bottom up state for
636 /// pointer Arg. 266 /// pointer Arg.
637 ptr_iterator findPtrBottomUpState(const Value *Arg) { 267 bottom_up_ptr_iterator findPtrBottomUpState(const Value *Arg) {
638 return PerPtrBottomUp.find(Arg); 268 return PerPtrBottomUp.find(Arg);
639 } 269 }
640 270
641 void clearBottomUpPointers() { 271 void clearBottomUpPointers() {
642 PerPtrBottomUp.clear(); 272 PerPtrBottomUp.clear();
683 }; 313 };
684 314
685 const unsigned BBState::OverflowOccurredValue = 0xffffffff; 315 const unsigned BBState::OverflowOccurredValue = 0xffffffff;
686 } 316 }
687 317
318 namespace llvm {
319 raw_ostream &operator<<(raw_ostream &OS,
320 BBState &BBState) LLVM_ATTRIBUTE_UNUSED;
321 }
322
688 void BBState::InitFromPred(const BBState &Other) { 323 void BBState::InitFromPred(const BBState &Other) {
689 PerPtrTopDown = Other.PerPtrTopDown; 324 PerPtrTopDown = Other.PerPtrTopDown;
690 TopDownPathCount = Other.TopDownPathCount; 325 TopDownPathCount = Other.TopDownPathCount;
691 } 326 }
692 327
722 } 357 }
723 358
724 // For each entry in the other set, if our set has an entry with the same key, 359 // For each entry in the other set, if our set has an entry with the same key,
725 // merge the entries. Otherwise, copy the entry and merge it with an empty 360 // merge the entries. Otherwise, copy the entry and merge it with an empty
726 // entry. 361 // entry.
727 for (ptr_const_iterator MI = Other.top_down_ptr_begin(), 362 for (auto MI = Other.top_down_ptr_begin(), ME = Other.top_down_ptr_end();
728 ME = Other.top_down_ptr_end(); MI != ME; ++MI) { 363 MI != ME; ++MI) {
729 std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI); 364 auto Pair = PerPtrTopDown.insert(*MI);
730 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second, 365 Pair.first->second.Merge(Pair.second ? TopDownPtrState() : MI->second,
731 /*TopDown=*/true); 366 /*TopDown=*/true);
732 } 367 }
733 368
734 // For each entry in our set, if the other set doesn't have an entry with the 369 // For each entry in our set, if the other set doesn't have an entry with the
735 // same key, force it to merge with an empty entry. 370 // same key, force it to merge with an empty entry.
736 for (ptr_iterator MI = top_down_ptr_begin(), 371 for (auto MI = top_down_ptr_begin(), ME = top_down_ptr_end(); MI != ME; ++MI)
737 ME = top_down_ptr_end(); MI != ME; ++MI)
738 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end()) 372 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
739 MI->second.Merge(PtrState(), /*TopDown=*/true); 373 MI->second.Merge(TopDownPtrState(), /*TopDown=*/true);
740 } 374 }
741 375
742 /// The bottom-up traversal uses this to merge information about successors to 376 /// The bottom-up traversal uses this to merge information about successors to
743 /// form the initial state for a new block. 377 /// form the initial state for a new block.
744 void BBState::MergeSucc(const BBState &Other) { 378 void BBState::MergeSucc(const BBState &Other) {
766 } 400 }
767 401
768 // For each entry in the other set, if our set has an entry with the 402 // For each entry in the other set, if our set has an entry with the
769 // same key, merge the entries. Otherwise, copy the entry and merge 403 // same key, merge the entries. Otherwise, copy the entry and merge
770 // it with an empty entry. 404 // it with an empty entry.
771 for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(), 405 for (auto MI = Other.bottom_up_ptr_begin(), ME = Other.bottom_up_ptr_end();
772 ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) { 406 MI != ME; ++MI) {
773 std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI); 407 auto Pair = PerPtrBottomUp.insert(*MI);
774 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second, 408 Pair.first->second.Merge(Pair.second ? BottomUpPtrState() : MI->second,
775 /*TopDown=*/false); 409 /*TopDown=*/false);
776 } 410 }
777 411
778 // For each entry in our set, if the other set doesn't have an entry 412 // For each entry in our set, if the other set doesn't have an entry
779 // with the same key, force it to merge with an empty entry. 413 // with the same key, force it to merge with an empty entry.
780 for (ptr_iterator MI = bottom_up_ptr_begin(), 414 for (auto MI = bottom_up_ptr_begin(), ME = bottom_up_ptr_end(); MI != ME;
781 ME = bottom_up_ptr_end(); MI != ME; ++MI) 415 ++MI)
782 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end()) 416 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
783 MI->second.Merge(PtrState(), /*TopDown=*/false); 417 MI->second.Merge(BottomUpPtrState(), /*TopDown=*/false);
784 } 418 }
785 419
786 // Only enable ARC Annotations if we are building a debug version of 420 raw_ostream &llvm::operator<<(raw_ostream &OS, BBState &BBInfo) {
787 // libObjCARCOpts. 421 // Dump the pointers we are tracking.
788 #ifndef NDEBUG 422 OS << " TopDown State:\n";
789 #define ARC_ANNOTATIONS 423 if (!BBInfo.hasTopDownPtrs()) {
790 #endif 424 DEBUG(llvm::dbgs() << " NONE!\n");
791 425 } else {
792 // Define some macros along the lines of DEBUG and some helper functions to make 426 for (auto I = BBInfo.top_down_ptr_begin(), E = BBInfo.top_down_ptr_end();
793 // it cleaner to create annotations in the source code and to no-op when not 427 I != E; ++I) {
794 // building in debug mode. 428 const PtrState &P = I->second;
795 #ifdef ARC_ANNOTATIONS 429 OS << " Ptr: " << *I->first
796 430 << "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false")
797 #include "llvm/Support/CommandLine.h" 431 << "\n ImpreciseRelease: "
798 432 << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
799 /// Enable/disable ARC sequence annotations. 433 << " HasCFGHazards: "
800 static cl::opt<bool> 434 << (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
801 EnableARCAnnotations("enable-objc-arc-annotations", cl::init(false), 435 << " KnownPositive: "
802 cl::desc("Enable emission of arc data flow analysis " 436 << (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
803 "annotations")); 437 << " Seq: "
804 static cl::opt<bool> 438 << P.GetSeq() << "\n";
805 DisableCheckForCFGHazards("disable-objc-arc-checkforcfghazards", cl::init(false), 439 }
806 cl::desc("Disable check for cfg hazards when " 440 }
807 "annotating")); 441
808 static cl::opt<std::string> 442 OS << " BottomUp State:\n";
809 ARCAnnotationTargetIdentifier("objc-arc-annotation-target-identifier", 443 if (!BBInfo.hasBottomUpPtrs()) {
810 cl::init(""), 444 DEBUG(llvm::dbgs() << " NONE!\n");
811 cl::desc("filter out all data flow annotations " 445 } else {
812 "but those that apply to the given " 446 for (auto I = BBInfo.bottom_up_ptr_begin(), E = BBInfo.bottom_up_ptr_end();
813 "target llvm identifier.")); 447 I != E; ++I) {
814 448 const PtrState &P = I->second;
815 /// This function appends a unique ARCAnnotationProvenanceSourceMDKind id to an 449 OS << " Ptr: " << *I->first
816 /// instruction so that we can track backwards when post processing via the llvm 450 << "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false")
817 /// arc annotation processor tool. If the function is an 451 << "\n ImpreciseRelease: "
818 static MDString *AppendMDNodeToSourcePtr(unsigned NodeId, 452 << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
819 Value *Ptr) { 453 << " HasCFGHazards: "
820 MDString *Hash = nullptr; 454 << (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
821 455 << " KnownPositive: "
822 // If pointer is a result of an instruction and it does not have a source 456 << (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
823 // MDNode it, attach a new MDNode onto it. If pointer is a result of 457 << " Seq: "
824 // an instruction and does have a source MDNode attached to it, return a 458 << P.GetSeq() << "\n";
825 // reference to said Node. Otherwise just return 0. 459 }
826 if (Instruction *Inst = dyn_cast<Instruction>(Ptr)) { 460 }
827 MDNode *Node; 461
828 if (!(Node = Inst->getMetadata(NodeId))) { 462 return OS;
829 // We do not have any node. Generate and attatch the hash MDString to the 463 }
830 // instruction.
831
832 // We just use an MDString to ensure that this metadata gets written out
833 // of line at the module level and to provide a very simple format
834 // encoding the information herein. Both of these makes it simpler to
835 // parse the annotations by a simple external program.
836 std::string Str;
837 raw_string_ostream os(Str);
838 os << "(" << Inst->getParent()->getParent()->getName() << ",%"
839 << Inst->getName() << ")";
840
841 Hash = MDString::get(Inst->getContext(), os.str());
842 Inst->setMetadata(NodeId, MDNode::get(Inst->getContext(),Hash));
843 } else {
844 // We have a node. Grab its hash and return it.
845 assert(Node->getNumOperands() == 1 &&
846 "An ARCAnnotationProvenanceSourceMDKind can only have 1 operand.");
847 Hash = cast<MDString>(Node->getOperand(0));
848 }
849 } else if (Argument *Arg = dyn_cast<Argument>(Ptr)) {
850 std::string str;
851 raw_string_ostream os(str);
852 os << "(" << Arg->getParent()->getName() << ",%" << Arg->getName()
853 << ")";
854 Hash = MDString::get(Arg->getContext(), os.str());
855 }
856
857 return Hash;
858 }
859
860 static std::string SequenceToString(Sequence A) {
861 std::string str;
862 raw_string_ostream os(str);
863 os << A;
864 return os.str();
865 }
866
867 /// Helper function to change a Sequence into a String object using our overload
868 /// for raw_ostream so we only have printing code in one location.
869 static MDString *SequenceToMDString(LLVMContext &Context,
870 Sequence A) {
871 return MDString::get(Context, SequenceToString(A));
872 }
873
874 /// A simple function to generate a MDNode which describes the change in state
875 /// for Value *Ptr caused by Instruction *Inst.
876 static void AppendMDNodeToInstForPtr(unsigned NodeId,
877 Instruction *Inst,
878 Value *Ptr,
879 MDString *PtrSourceMDNodeID,
880 Sequence OldSeq,
881 Sequence NewSeq) {
882 MDNode *Node = nullptr;
883 Metadata *tmp[3] = {PtrSourceMDNodeID,
884 SequenceToMDString(Inst->getContext(), OldSeq),
885 SequenceToMDString(Inst->getContext(), NewSeq)};
886 Node = MDNode::get(Inst->getContext(), tmp);
887
888 Inst->setMetadata(NodeId, Node);
889 }
890
891 /// Add to the beginning of the basic block llvm.ptr.annotations which show the
892 /// state of a pointer at the entrance to a basic block.
893 static void GenerateARCBBEntranceAnnotation(const char *Name, BasicBlock *BB,
894 Value *Ptr, Sequence Seq) {
895 // If we have a target identifier, make sure that we match it before
896 // continuing.
897 if(!ARCAnnotationTargetIdentifier.empty() &&
898 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
899 return;
900
901 Module *M = BB->getParent()->getParent();
902 LLVMContext &C = M->getContext();
903 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
904 Type *I8XX = PointerType::getUnqual(I8X);
905 Type *Params[] = {I8XX, I8XX};
906 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), Params,
907 /*isVarArg=*/false);
908 Constant *Callee = M->getOrInsertFunction(Name, FTy);
909
910 IRBuilder<> Builder(BB, BB->getFirstInsertionPt());
911
912 Value *PtrName;
913 StringRef Tmp = Ptr->getName();
914 if (nullptr == (PtrName = M->getGlobalVariable(Tmp, true))) {
915 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
916 Tmp + "_STR");
917 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
918 cast<Constant>(ActualPtrName), Tmp);
919 }
920
921 Value *S;
922 std::string SeqStr = SequenceToString(Seq);
923 if (nullptr == (S = M->getGlobalVariable(SeqStr, true))) {
924 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
925 SeqStr + "_STR");
926 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
927 cast<Constant>(ActualPtrName), SeqStr);
928 }
929
930 Builder.CreateCall2(Callee, PtrName, S);
931 }
932
933 /// Add to the end of the basic block llvm.ptr.annotations which show the state
934 /// of the pointer at the bottom of the basic block.
935 static void GenerateARCBBTerminatorAnnotation(const char *Name, BasicBlock *BB,
936 Value *Ptr, Sequence Seq) {
937 // If we have a target identifier, make sure that we match it before emitting
938 // an annotation.
939 if(!ARCAnnotationTargetIdentifier.empty() &&
940 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
941 return;
942
943 Module *M = BB->getParent()->getParent();
944 LLVMContext &C = M->getContext();
945 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
946 Type *I8XX = PointerType::getUnqual(I8X);
947 Type *Params[] = {I8XX, I8XX};
948 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), Params,
949 /*isVarArg=*/false);
950 Constant *Callee = M->getOrInsertFunction(Name, FTy);
951
952 IRBuilder<> Builder(BB, std::prev(BB->end()));
953
954 Value *PtrName;
955 StringRef Tmp = Ptr->getName();
956 if (nullptr == (PtrName = M->getGlobalVariable(Tmp, true))) {
957 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
958 Tmp + "_STR");
959 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
960 cast<Constant>(ActualPtrName), Tmp);
961 }
962
963 Value *S;
964 std::string SeqStr = SequenceToString(Seq);
965 if (nullptr == (S = M->getGlobalVariable(SeqStr, true))) {
966 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
967 SeqStr + "_STR");
968 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
969 cast<Constant>(ActualPtrName), SeqStr);
970 }
971 Builder.CreateCall2(Callee, PtrName, S);
972 }
973
974 /// Adds a source annotation to pointer and a state change annotation to Inst
975 /// referencing the source annotation and the old/new state of pointer.
976 static void GenerateARCAnnotation(unsigned InstMDId,
977 unsigned PtrMDId,
978 Instruction *Inst,
979 Value *Ptr,
980 Sequence OldSeq,
981 Sequence NewSeq) {
982 if (EnableARCAnnotations) {
983 // If we have a target identifier, make sure that we match it before
984 // emitting an annotation.
985 if(!ARCAnnotationTargetIdentifier.empty() &&
986 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
987 return;
988
989 // First generate the source annotation on our pointer. This will return an
990 // MDString* if Ptr actually comes from an instruction implying we can put
991 // in a source annotation. If AppendMDNodeToSourcePtr returns 0 (i.e. NULL),
992 // then we know that our pointer is from an Argument so we put a reference
993 // to the argument number.
994 //
995 // The point of this is to make it easy for the
996 // llvm-arc-annotation-processor tool to cross reference where the source
997 // pointer is in the LLVM IR since the LLVM IR parser does not submit such
998 // information via debug info for backends to use (since why would anyone
999 // need such a thing from LLVM IR besides in non-standard cases
1000 // [i.e. this]).
1001 MDString *SourcePtrMDNode =
1002 AppendMDNodeToSourcePtr(PtrMDId, Ptr);
1003 AppendMDNodeToInstForPtr(InstMDId, Inst, Ptr, SourcePtrMDNode, OldSeq,
1004 NewSeq);
1005 }
1006 }
1007
1008 // The actual interface for accessing the above functionality is defined via
1009 // some simple macros which are defined below. We do this so that the user does
1010 // not need to pass in what metadata id is needed resulting in cleaner code and
1011 // additionally since it provides an easy way to conditionally no-op all
1012 // annotation support in a non-debug build.
1013
1014 /// Use this macro to annotate a sequence state change when processing
1015 /// instructions bottom up,
1016 #define ANNOTATE_BOTTOMUP(inst, ptr, old, new) \
1017 GenerateARCAnnotation(ARCAnnotationBottomUpMDKind, \
1018 ARCAnnotationProvenanceSourceMDKind, (inst), \
1019 const_cast<Value*>(ptr), (old), (new))
1020 /// Use this macro to annotate a sequence state change when processing
1021 /// instructions top down.
1022 #define ANNOTATE_TOPDOWN(inst, ptr, old, new) \
1023 GenerateARCAnnotation(ARCAnnotationTopDownMDKind, \
1024 ARCAnnotationProvenanceSourceMDKind, (inst), \
1025 const_cast<Value*>(ptr), (old), (new))
1026
1027 #define ANNOTATE_BB(_states, _bb, _name, _type, _direction) \
1028 do { \
1029 if (EnableARCAnnotations) { \
1030 for(BBState::ptr_const_iterator I = (_states)._direction##_ptr_begin(), \
1031 E = (_states)._direction##_ptr_end(); I != E; ++I) { \
1032 Value *Ptr = const_cast<Value*>(I->first); \
1033 Sequence Seq = I->second.GetSeq(); \
1034 GenerateARCBB ## _type ## Annotation(_name, (_bb), Ptr, Seq); \
1035 } \
1036 } \
1037 } while (0)
1038
1039 #define ANNOTATE_BOTTOMUP_BBSTART(_states, _basicblock) \
1040 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbstart", \
1041 Entrance, bottom_up)
1042 #define ANNOTATE_BOTTOMUP_BBEND(_states, _basicblock) \
1043 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbend", \
1044 Terminator, bottom_up)
1045 #define ANNOTATE_TOPDOWN_BBSTART(_states, _basicblock) \
1046 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbstart", \
1047 Entrance, top_down)
1048 #define ANNOTATE_TOPDOWN_BBEND(_states, _basicblock) \
1049 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbend", \
1050 Terminator, top_down)
1051
1052 #else // !ARC_ANNOTATION
1053 // If annotations are off, noop.
1054 #define ANNOTATE_BOTTOMUP(inst, ptr, old, new)
1055 #define ANNOTATE_TOPDOWN(inst, ptr, old, new)
1056 #define ANNOTATE_BOTTOMUP_BBSTART(states, basicblock)
1057 #define ANNOTATE_BOTTOMUP_BBEND(states, basicblock)
1058 #define ANNOTATE_TOPDOWN_BBSTART(states, basicblock)
1059 #define ANNOTATE_TOPDOWN_BBEND(states, basicblock)
1060 #endif // !ARC_ANNOTATION
1061 464
1062 namespace { 465 namespace {
466
1063 /// \brief The main ARC optimization pass. 467 /// \brief The main ARC optimization pass.
1064 class ObjCARCOpt : public FunctionPass { 468 class ObjCARCOpt : public FunctionPass {
1065 bool Changed; 469 bool Changed;
1066 ProvenanceAnalysis PA; 470 ProvenanceAnalysis PA;
471
472 /// A cache of references to runtime entry point constants.
1067 ARCRuntimeEntryPoints EP; 473 ARCRuntimeEntryPoints EP;
474
475 /// A cache of MDKinds that can be passed into other functions to propagate
476 /// MDKind identifiers.
477 ARCMDKindCache MDKindCache;
1068 478
1069 // This is used to track if a pointer is stored into an alloca. 479 // This is used to track if a pointer is stored into an alloca.
1070 DenseSet<const Value *> MultiOwnersSet; 480 DenseSet<const Value *> MultiOwnersSet;
1071 481
1072 /// A flag indicating whether this optimization pass should run. 482 /// A flag indicating whether this optimization pass should run.
1073 bool Run; 483 bool Run;
1074 484
1075 /// Flags which determine whether each of the interesting runtine functions 485 /// Flags which determine whether each of the interesting runtime functions
1076 /// is in fact used in the current function. 486 /// is in fact used in the current function.
1077 unsigned UsedInThisFunction; 487 unsigned UsedInThisFunction;
1078 488
1079 /// The Metadata Kind for clang.imprecise_release metadata.
1080 unsigned ImpreciseReleaseMDKind;
1081
1082 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
1083 unsigned CopyOnEscapeMDKind;
1084
1085 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
1086 unsigned NoObjCARCExceptionsMDKind;
1087
1088 #ifdef ARC_ANNOTATIONS
1089 /// The Metadata Kind for llvm.arc.annotation.bottomup metadata.
1090 unsigned ARCAnnotationBottomUpMDKind;
1091 /// The Metadata Kind for llvm.arc.annotation.topdown metadata.
1092 unsigned ARCAnnotationTopDownMDKind;
1093 /// The Metadata Kind for llvm.arc.annotation.provenancesource metadata.
1094 unsigned ARCAnnotationProvenanceSourceMDKind;
1095 #endif // ARC_ANNOATIONS
1096
1097 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV); 489 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
1098 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV, 490 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1099 InstructionClass &Class); 491 ARCInstKind &Class);
1100 void OptimizeIndividualCalls(Function &F); 492 void OptimizeIndividualCalls(Function &F);
1101 493
1102 void CheckForCFGHazards(const BasicBlock *BB, 494 void CheckForCFGHazards(const BasicBlock *BB,
1103 DenseMap<const BasicBlock *, BBState> &BBStates, 495 DenseMap<const BasicBlock *, BBState> &BBStates,
1104 BBState &MyStates) const; 496 BBState &MyStates) const;
1105 bool VisitInstructionBottomUp(Instruction *Inst, 497 bool VisitInstructionBottomUp(Instruction *Inst, BasicBlock *BB,
1106 BasicBlock *BB, 498 BlotMapVector<Value *, RRInfo> &Retains,
1107 MapVector<Value *, RRInfo> &Retains,
1108 BBState &MyStates); 499 BBState &MyStates);
1109 bool VisitBottomUp(BasicBlock *BB, 500 bool VisitBottomUp(BasicBlock *BB,
1110 DenseMap<const BasicBlock *, BBState> &BBStates, 501 DenseMap<const BasicBlock *, BBState> &BBStates,
1111 MapVector<Value *, RRInfo> &Retains); 502 BlotMapVector<Value *, RRInfo> &Retains);
1112 bool VisitInstructionTopDown(Instruction *Inst, 503 bool VisitInstructionTopDown(Instruction *Inst,
1113 DenseMap<Value *, RRInfo> &Releases, 504 DenseMap<Value *, RRInfo> &Releases,
1114 BBState &MyStates); 505 BBState &MyStates);
1115 bool VisitTopDown(BasicBlock *BB, 506 bool VisitTopDown(BasicBlock *BB,
1116 DenseMap<const BasicBlock *, BBState> &BBStates, 507 DenseMap<const BasicBlock *, BBState> &BBStates,
1117 DenseMap<Value *, RRInfo> &Releases); 508 DenseMap<Value *, RRInfo> &Releases);
1118 bool Visit(Function &F, 509 bool Visit(Function &F, DenseMap<const BasicBlock *, BBState> &BBStates,
1119 DenseMap<const BasicBlock *, BBState> &BBStates, 510 BlotMapVector<Value *, RRInfo> &Retains,
1120 MapVector<Value *, RRInfo> &Retains,
1121 DenseMap<Value *, RRInfo> &Releases); 511 DenseMap<Value *, RRInfo> &Releases);
1122 512
1123 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove, 513 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
1124 MapVector<Value *, RRInfo> &Retains, 514 BlotMapVector<Value *, RRInfo> &Retains,
1125 DenseMap<Value *, RRInfo> &Releases, 515 DenseMap<Value *, RRInfo> &Releases,
1126 SmallVectorImpl<Instruction *> &DeadInsts, 516 SmallVectorImpl<Instruction *> &DeadInsts, Module *M);
1127 Module *M); 517
1128 518 bool
1129 bool ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> &BBStates, 519 PairUpRetainsAndReleases(DenseMap<const BasicBlock *, BBState> &BBStates,
1130 MapVector<Value *, RRInfo> &Retains, 520 BlotMapVector<Value *, RRInfo> &Retains,
1131 DenseMap<Value *, RRInfo> &Releases, 521 DenseMap<Value *, RRInfo> &Releases, Module *M,
1132 Module *M, 522 SmallVectorImpl<Instruction *> &NewRetains,
1133 SmallVectorImpl<Instruction *> &NewRetains, 523 SmallVectorImpl<Instruction *> &NewReleases,
1134 SmallVectorImpl<Instruction *> &NewReleases, 524 SmallVectorImpl<Instruction *> &DeadInsts,
1135 SmallVectorImpl<Instruction *> &DeadInsts, 525 RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
1136 RRInfo &RetainsToMove, 526 Value *Arg, bool KnownSafe,
1137 RRInfo &ReleasesToMove, 527 bool &AnyPairsCompletelyEliminated);
1138 Value *Arg,
1139 bool KnownSafe,
1140 bool &AnyPairsCompletelyEliminated);
1141 528
1142 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates, 529 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
1143 MapVector<Value *, RRInfo> &Retains, 530 BlotMapVector<Value *, RRInfo> &Retains,
1144 DenseMap<Value *, RRInfo> &Releases, 531 DenseMap<Value *, RRInfo> &Releases, Module *M);
1145 Module *M);
1146 532
1147 void OptimizeWeakCalls(Function &F); 533 void OptimizeWeakCalls(Function &F);
1148 534
1149 bool OptimizeSequences(Function &F); 535 bool OptimizeSequences(Function &F);
1150 536
1168 } 554 }
1169 555
1170 char ObjCARCOpt::ID = 0; 556 char ObjCARCOpt::ID = 0;
1171 INITIALIZE_PASS_BEGIN(ObjCARCOpt, 557 INITIALIZE_PASS_BEGIN(ObjCARCOpt,
1172 "objc-arc", "ObjC ARC optimization", false, false) 558 "objc-arc", "ObjC ARC optimization", false, false)
1173 INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis) 559 INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
1174 INITIALIZE_PASS_END(ObjCARCOpt, 560 INITIALIZE_PASS_END(ObjCARCOpt,
1175 "objc-arc", "ObjC ARC optimization", false, false) 561 "objc-arc", "ObjC ARC optimization", false, false)
1176 562
1177 Pass *llvm::createObjCARCOptPass() { 563 Pass *llvm::createObjCARCOptPass() {
1178 return new ObjCARCOpt(); 564 return new ObjCARCOpt();
1179 } 565 }
1180 566
1181 void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const { 567 void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
1182 AU.addRequired<ObjCARCAliasAnalysis>(); 568 AU.addRequired<ObjCARCAAWrapperPass>();
1183 AU.addRequired<AliasAnalysis>(); 569 AU.addRequired<AAResultsWrapperPass>();
1184 // ARC optimization doesn't currently split critical edges. 570 // ARC optimization doesn't currently split critical edges.
1185 AU.setPreservesCFG(); 571 AU.setPreservesCFG();
1186 } 572 }
1187 573
1188 /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is 574 /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
1189 /// not a return value. Or, if it can be paired with an 575 /// not a return value. Or, if it can be paired with an
1190 /// objc_autoreleaseReturnValue, delete the pair and return true. 576 /// objc_autoreleaseReturnValue, delete the pair and return true.
1191 bool 577 bool
1192 ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) { 578 ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
1193 // Check for the argument being from an immediately preceding call or invoke. 579 // Check for the argument being from an immediately preceding call or invoke.
1194 const Value *Arg = GetObjCArg(RetainRV); 580 const Value *Arg = GetArgRCIdentityRoot(RetainRV);
1195 ImmutableCallSite CS(Arg); 581 ImmutableCallSite CS(Arg);
1196 if (const Instruction *Call = CS.getInstruction()) { 582 if (const Instruction *Call = CS.getInstruction()) {
1197 if (Call->getParent() == RetainRV->getParent()) { 583 if (Call->getParent() == RetainRV->getParent()) {
1198 BasicBlock::const_iterator I = Call; 584 BasicBlock::const_iterator I = Call;
1199 ++I; 585 ++I;
1214 // Check for being preceded by an objc_autoreleaseReturnValue on the same 600 // Check for being preceded by an objc_autoreleaseReturnValue on the same
1215 // pointer. In this case, we can delete the pair. 601 // pointer. In this case, we can delete the pair.
1216 BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin(); 602 BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
1217 if (I != Begin) { 603 if (I != Begin) {
1218 do --I; while (I != Begin && IsNoopInstruction(I)); 604 do --I; while (I != Begin && IsNoopInstruction(I));
1219 if (GetBasicInstructionClass(I) == IC_AutoreleaseRV && 605 if (GetBasicARCInstKind(I) == ARCInstKind::AutoreleaseRV &&
1220 GetObjCArg(I) == Arg) { 606 GetArgRCIdentityRoot(I) == Arg) {
1221 Changed = true; 607 Changed = true;
1222 ++NumPeeps; 608 ++NumPeeps;
1223 609
1224 DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n" 610 DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n"
1225 << "Erasing " << *RetainRV << "\n"); 611 << "Erasing " << *RetainRV << "\n");
1236 622
1237 DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => " 623 DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
1238 "objc_retain since the operand is not a return value.\n" 624 "objc_retain since the operand is not a return value.\n"
1239 "Old = " << *RetainRV << "\n"); 625 "Old = " << *RetainRV << "\n");
1240 626
1241 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Retain); 627 Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Retain);
1242 cast<CallInst>(RetainRV)->setCalledFunction(NewDecl); 628 cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
1243 629
1244 DEBUG(dbgs() << "New = " << *RetainRV << "\n"); 630 DEBUG(dbgs() << "New = " << *RetainRV << "\n");
1245 631
1246 return false; 632 return false;
1247 } 633 }
1248 634
1249 /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not 635 /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
1250 /// used as a return value. 636 /// used as a return value.
1251 void 637 void ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F,
1252 ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV, 638 Instruction *AutoreleaseRV,
1253 InstructionClass &Class) { 639 ARCInstKind &Class) {
1254 // Check for a return of the pointer value. 640 // Check for a return of the pointer value.
1255 const Value *Ptr = GetObjCArg(AutoreleaseRV); 641 const Value *Ptr = GetArgRCIdentityRoot(AutoreleaseRV);
1256 SmallVector<const Value *, 2> Users; 642 SmallVector<const Value *, 2> Users;
1257 Users.push_back(Ptr); 643 Users.push_back(Ptr);
1258 do { 644 do {
1259 Ptr = Users.pop_back_val(); 645 Ptr = Users.pop_back_val();
1260 for (const User *U : Ptr->users()) { 646 for (const User *U : Ptr->users()) {
1261 if (isa<ReturnInst>(U) || GetBasicInstructionClass(U) == IC_RetainRV) 647 if (isa<ReturnInst>(U) || GetBasicARCInstKind(U) == ARCInstKind::RetainRV)
1262 return; 648 return;
1263 if (isa<BitCastInst>(U)) 649 if (isa<BitCastInst>(U))
1264 Users.push_back(U); 650 Users.push_back(U);
1265 } 651 }
1266 } while (!Users.empty()); 652 } while (!Users.empty());
1272 "objc_autorelease since its operand is not used as a return " 658 "objc_autorelease since its operand is not used as a return "
1273 "value.\n" 659 "value.\n"
1274 "Old = " << *AutoreleaseRV << "\n"); 660 "Old = " << *AutoreleaseRV << "\n");
1275 661
1276 CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV); 662 CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
1277 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Autorelease); 663 Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Autorelease);
1278 AutoreleaseRVCI->setCalledFunction(NewDecl); 664 AutoreleaseRVCI->setCalledFunction(NewDecl);
1279 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease. 665 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
1280 Class = IC_Autorelease; 666 Class = ARCInstKind::Autorelease;
1281 667
1282 DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n"); 668 DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
1283 669
1284 } 670 }
1285 671
1292 678
1293 // Visit all objc_* calls in F. 679 // Visit all objc_* calls in F.
1294 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { 680 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
1295 Instruction *Inst = &*I++; 681 Instruction *Inst = &*I++;
1296 682
1297 InstructionClass Class = GetBasicInstructionClass(Inst); 683 ARCInstKind Class = GetBasicARCInstKind(Inst);
1298 684
1299 DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n"); 685 DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
1300 686
1301 switch (Class) { 687 switch (Class) {
1302 default: break; 688 default: break;
1307 // which return their argument. 693 // which return their argument.
1308 // 694 //
1309 // There are gray areas here, as the ability to cast reference-counted 695 // There are gray areas here, as the ability to cast reference-counted
1310 // pointers to raw void* and back allows code to break ARC assumptions, 696 // pointers to raw void* and back allows code to break ARC assumptions,
1311 // however these are currently considered to be unimportant. 697 // however these are currently considered to be unimportant.
1312 case IC_NoopCast: 698 case ARCInstKind::NoopCast:
1313 Changed = true; 699 Changed = true;
1314 ++NumNoops; 700 ++NumNoops;
1315 DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n"); 701 DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
1316 EraseInstruction(Inst); 702 EraseInstruction(Inst);
1317 continue; 703 continue;
1318 704
1319 // If the pointer-to-weak-pointer is null, it's undefined behavior. 705 // If the pointer-to-weak-pointer is null, it's undefined behavior.
1320 case IC_StoreWeak: 706 case ARCInstKind::StoreWeak:
1321 case IC_LoadWeak: 707 case ARCInstKind::LoadWeak:
1322 case IC_LoadWeakRetained: 708 case ARCInstKind::LoadWeakRetained:
1323 case IC_InitWeak: 709 case ARCInstKind::InitWeak:
1324 case IC_DestroyWeak: { 710 case ARCInstKind::DestroyWeak: {
1325 CallInst *CI = cast<CallInst>(Inst); 711 CallInst *CI = cast<CallInst>(Inst);
1326 if (IsNullOrUndef(CI->getArgOperand(0))) { 712 if (IsNullOrUndef(CI->getArgOperand(0))) {
1327 Changed = true; 713 Changed = true;
1328 Type *Ty = CI->getArgOperand(0)->getType(); 714 Type *Ty = CI->getArgOperand(0)->getType();
1329 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()), 715 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1336 CI->eraseFromParent(); 722 CI->eraseFromParent();
1337 continue; 723 continue;
1338 } 724 }
1339 break; 725 break;
1340 } 726 }
1341 case IC_CopyWeak: 727 case ARCInstKind::CopyWeak:
1342 case IC_MoveWeak: { 728 case ARCInstKind::MoveWeak: {
1343 CallInst *CI = cast<CallInst>(Inst); 729 CallInst *CI = cast<CallInst>(Inst);
1344 if (IsNullOrUndef(CI->getArgOperand(0)) || 730 if (IsNullOrUndef(CI->getArgOperand(0)) ||
1345 IsNullOrUndef(CI->getArgOperand(1))) { 731 IsNullOrUndef(CI->getArgOperand(1))) {
1346 Changed = true; 732 Changed = true;
1347 Type *Ty = CI->getArgOperand(0)->getType(); 733 Type *Ty = CI->getArgOperand(0)->getType();
1357 CI->eraseFromParent(); 743 CI->eraseFromParent();
1358 continue; 744 continue;
1359 } 745 }
1360 break; 746 break;
1361 } 747 }
1362 case IC_RetainRV: 748 case ARCInstKind::RetainRV:
1363 if (OptimizeRetainRVCall(F, Inst)) 749 if (OptimizeRetainRVCall(F, Inst))
1364 continue; 750 continue;
1365 break; 751 break;
1366 case IC_AutoreleaseRV: 752 case ARCInstKind::AutoreleaseRV:
1367 OptimizeAutoreleaseRVCall(F, Inst, Class); 753 OptimizeAutoreleaseRVCall(F, Inst, Class);
1368 break; 754 break;
1369 } 755 }
1370 756
1371 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused. 757 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
1378 ++NumAutoreleases; 764 ++NumAutoreleases;
1379 765
1380 // Create the declaration lazily. 766 // Create the declaration lazily.
1381 LLVMContext &C = Inst->getContext(); 767 LLVMContext &C = Inst->getContext();
1382 768
1383 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release); 769 Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
1384 CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "", 770 CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
1385 Call); 771 Call);
1386 NewCall->setMetadata(ImpreciseReleaseMDKind, MDNode::get(C, None)); 772 NewCall->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease),
773 MDNode::get(C, None));
1387 774
1388 DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) " 775 DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
1389 "since x is otherwise unused.\nOld: " << *Call << "\nNew: " 776 "since x is otherwise unused.\nOld: " << *Call << "\nNew: "
1390 << *NewCall << "\n"); 777 << *NewCall << "\n");
1391 778
1392 EraseInstruction(Call); 779 EraseInstruction(Call);
1393 Inst = NewCall; 780 Inst = NewCall;
1394 Class = IC_Release; 781 Class = ARCInstKind::Release;
1395 } 782 }
1396 } 783 }
1397 784
1398 // For functions which can never be passed stack arguments, add 785 // For functions which can never be passed stack arguments, add
1399 // a tail keyword. 786 // a tail keyword.
1420 << "\n"); 807 << "\n");
1421 cast<CallInst>(Inst)->setDoesNotThrow(); 808 cast<CallInst>(Inst)->setDoesNotThrow();
1422 } 809 }
1423 810
1424 if (!IsNoopOnNull(Class)) { 811 if (!IsNoopOnNull(Class)) {
1425 UsedInThisFunction |= 1 << Class; 812 UsedInThisFunction |= 1 << unsigned(Class);
1426 continue; 813 continue;
1427 } 814 }
1428 815
1429 const Value *Arg = GetObjCArg(Inst); 816 const Value *Arg = GetArgRCIdentityRoot(Inst);
1430 817
1431 // ARC calls with null are no-ops. Delete them. 818 // ARC calls with null are no-ops. Delete them.
1432 if (IsNullOrUndef(Arg)) { 819 if (IsNullOrUndef(Arg)) {
1433 Changed = true; 820 Changed = true;
1434 ++NumNoops; 821 ++NumNoops;
1438 continue; 825 continue;
1439 } 826 }
1440 827
1441 // Keep track of which of retain, release, autorelease, and retain_block 828 // Keep track of which of retain, release, autorelease, and retain_block
1442 // are actually present in this function. 829 // are actually present in this function.
1443 UsedInThisFunction |= 1 << Class; 830 UsedInThisFunction |= 1 << unsigned(Class);
1444 831
1445 // If Arg is a PHI, and one or more incoming values to the 832 // If Arg is a PHI, and one or more incoming values to the
1446 // PHI are null, and the call is control-equivalent to the PHI, and there 833 // PHI are null, and the call is control-equivalent to the PHI, and there
1447 // are no relevant side effects between the PHI and the call, the call 834 // are no relevant side effects between the PHI and the call, the call
1448 // could be pushed up to just those paths with non-null incoming values. 835 // could be pushed up to just those paths with non-null incoming values.
1461 // critical edges. 848 // critical edges.
1462 bool HasNull = false; 849 bool HasNull = false;
1463 bool HasCriticalEdges = false; 850 bool HasCriticalEdges = false;
1464 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 851 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1465 Value *Incoming = 852 Value *Incoming =
1466 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i)); 853 GetRCIdentityRoot(PN->getIncomingValue(i));
1467 if (IsNullOrUndef(Incoming)) 854 if (IsNullOrUndef(Incoming))
1468 HasNull = true; 855 HasNull = true;
1469 else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back()) 856 else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
1470 .getNumSuccessors() != 1) { 857 .getNumSuccessors() != 1) {
1471 HasCriticalEdges = true; 858 HasCriticalEdges = true;
1478 SmallPtrSet<const BasicBlock *, 4> Visited; 865 SmallPtrSet<const BasicBlock *, 4> Visited;
1479 866
1480 // Check that there is nothing that cares about the reference 867 // Check that there is nothing that cares about the reference
1481 // count between the call and the phi. 868 // count between the call and the phi.
1482 switch (Class) { 869 switch (Class) {
1483 case IC_Retain: 870 case ARCInstKind::Retain:
1484 case IC_RetainBlock: 871 case ARCInstKind::RetainBlock:
1485 // These can always be moved up. 872 // These can always be moved up.
1486 break; 873 break;
1487 case IC_Release: 874 case ARCInstKind::Release:
1488 // These can't be moved across things that care about the retain 875 // These can't be moved across things that care about the retain
1489 // count. 876 // count.
1490 FindDependencies(NeedsPositiveRetainCount, Arg, 877 FindDependencies(NeedsPositiveRetainCount, Arg,
1491 Inst->getParent(), Inst, 878 Inst->getParent(), Inst,
1492 DependingInstructions, Visited, PA); 879 DependingInstructions, Visited, PA);
1493 break; 880 break;
1494 case IC_Autorelease: 881 case ARCInstKind::Autorelease:
1495 // These can't be moved across autorelease pool scope boundaries. 882 // These can't be moved across autorelease pool scope boundaries.
1496 FindDependencies(AutoreleasePoolBoundary, Arg, 883 FindDependencies(AutoreleasePoolBoundary, Arg,
1497 Inst->getParent(), Inst, 884 Inst->getParent(), Inst,
1498 DependingInstructions, Visited, PA); 885 DependingInstructions, Visited, PA);
1499 break; 886 break;
1500 case IC_RetainRV: 887 case ARCInstKind::RetainRV:
1501 case IC_AutoreleaseRV: 888 case ARCInstKind::AutoreleaseRV:
1502 // Don't move these; the RV optimization depends on the autoreleaseRV 889 // Don't move these; the RV optimization depends on the autoreleaseRV
1503 // being tail called, and the retainRV being immediately after a call 890 // being tail called, and the retainRV being immediately after a call
1504 // (which might still happen if we get lucky with codegen layout, but 891 // (which might still happen if we get lucky with codegen layout, but
1505 // it's not worth taking the chance). 892 // it's not worth taking the chance).
1506 continue; 893 continue;
1515 // Clone the call into each predecessor that has a non-null value. 902 // Clone the call into each predecessor that has a non-null value.
1516 CallInst *CInst = cast<CallInst>(Inst); 903 CallInst *CInst = cast<CallInst>(Inst);
1517 Type *ParamTy = CInst->getArgOperand(0)->getType(); 904 Type *ParamTy = CInst->getArgOperand(0)->getType();
1518 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 905 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1519 Value *Incoming = 906 Value *Incoming =
1520 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i)); 907 GetRCIdentityRoot(PN->getIncomingValue(i));
1521 if (!IsNullOrUndef(Incoming)) { 908 if (!IsNullOrUndef(Incoming)) {
1522 CallInst *Clone = cast<CallInst>(CInst->clone()); 909 CallInst *Clone = cast<CallInst>(CInst->clone());
1523 Value *Op = PN->getIncomingValue(i); 910 Value *Op = PN->getIncomingValue(i);
1524 Instruction *InsertPos = &PN->getIncomingBlock(i)->back(); 911 Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
1525 if (Op->getType() != ParamTy) 912 if (Op->getType() != ParamTy)
1545 932
1546 /// If we have a top down pointer in the S_Use state, make sure that there are 933 /// If we have a top down pointer in the S_Use state, make sure that there are
1547 /// no CFG hazards by checking the states of various bottom up pointers. 934 /// no CFG hazards by checking the states of various bottom up pointers.
1548 static void CheckForUseCFGHazard(const Sequence SuccSSeq, 935 static void CheckForUseCFGHazard(const Sequence SuccSSeq,
1549 const bool SuccSRRIKnownSafe, 936 const bool SuccSRRIKnownSafe,
1550 PtrState &S, 937 TopDownPtrState &S,
1551 bool &SomeSuccHasSame, 938 bool &SomeSuccHasSame,
1552 bool &AllSuccsHaveSame, 939 bool &AllSuccsHaveSame,
1553 bool &NotAllSeqEqualButKnownSafe, 940 bool &NotAllSeqEqualButKnownSafe,
1554 bool &ShouldContinue) { 941 bool &ShouldContinue) {
1555 switch (SuccSSeq) { 942 switch (SuccSSeq) {
1583 /// If we have a Top Down pointer in the S_CanRelease state, make sure that 970 /// If we have a Top Down pointer in the S_CanRelease state, make sure that
1584 /// there are no CFG hazards by checking the states of various bottom up 971 /// there are no CFG hazards by checking the states of various bottom up
1585 /// pointers. 972 /// pointers.
1586 static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq, 973 static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
1587 const bool SuccSRRIKnownSafe, 974 const bool SuccSRRIKnownSafe,
1588 PtrState &S, 975 TopDownPtrState &S,
1589 bool &SomeSuccHasSame, 976 bool &SomeSuccHasSame,
1590 bool &AllSuccsHaveSame, 977 bool &AllSuccsHaveSame,
1591 bool &NotAllSeqEqualButKnownSafe) { 978 bool &NotAllSeqEqualButKnownSafe) {
1592 switch (SuccSSeq) { 979 switch (SuccSSeq) {
1593 case S_CanRelease: 980 case S_CanRelease:
1616 ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB, 1003 ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
1617 DenseMap<const BasicBlock *, BBState> &BBStates, 1004 DenseMap<const BasicBlock *, BBState> &BBStates,
1618 BBState &MyStates) const { 1005 BBState &MyStates) const {
1619 // If any top-down local-use or possible-dec has a succ which is earlier in 1006 // If any top-down local-use or possible-dec has a succ which is earlier in
1620 // the sequence, forget it. 1007 // the sequence, forget it.
1621 for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(), 1008 for (auto I = MyStates.top_down_ptr_begin(), E = MyStates.top_down_ptr_end();
1622 E = MyStates.top_down_ptr_end(); I != E; ++I) { 1009 I != E; ++I) {
1623 PtrState &S = I->second; 1010 TopDownPtrState &S = I->second;
1624 const Sequence Seq = I->second.GetSeq(); 1011 const Sequence Seq = I->second.GetSeq();
1625 1012
1626 // We only care about S_Retain, S_CanRelease, and S_Use. 1013 // We only care about S_Retain, S_CanRelease, and S_Use.
1627 if (Seq == S_None) 1014 if (Seq == S_None)
1628 continue; 1015 continue;
1644 // If VisitBottomUp has pointer information for this successor, take 1031 // If VisitBottomUp has pointer information for this successor, take
1645 // what we know about it. 1032 // what we know about it.
1646 const DenseMap<const BasicBlock *, BBState>::iterator BBI = 1033 const DenseMap<const BasicBlock *, BBState>::iterator BBI =
1647 BBStates.find(*SI); 1034 BBStates.find(*SI);
1648 assert(BBI != BBStates.end()); 1035 assert(BBI != BBStates.end());
1649 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg); 1036 const BottomUpPtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1650 const Sequence SuccSSeq = SuccS.GetSeq(); 1037 const Sequence SuccSSeq = SuccS.GetSeq();
1651 1038
1652 // If bottom up, the pointer is in an S_None state, clear the sequence 1039 // If bottom up, the pointer is in an S_None state, clear the sequence
1653 // progress since the sequence in the bottom up state finished 1040 // progress since the sequence in the bottom up state finished
1654 // suggesting a mismatch in between retains/releases. This is true for 1041 // suggesting a mismatch in between retains/releases. This is true for
1703 S.SetCFGHazardAfflicted(true); 1090 S.SetCFGHazardAfflicted(true);
1704 } 1091 }
1705 } 1092 }
1706 } 1093 }
1707 1094
1708 bool 1095 bool ObjCARCOpt::VisitInstructionBottomUp(
1709 ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst, 1096 Instruction *Inst, BasicBlock *BB, BlotMapVector<Value *, RRInfo> &Retains,
1710 BasicBlock *BB, 1097 BBState &MyStates) {
1711 MapVector<Value *, RRInfo> &Retains,
1712 BBState &MyStates) {
1713 bool NestingDetected = false; 1098 bool NestingDetected = false;
1714 InstructionClass Class = GetInstructionClass(Inst); 1099 ARCInstKind Class = GetARCInstKind(Inst);
1715 const Value *Arg = nullptr; 1100 const Value *Arg = nullptr;
1716 1101
1717 DEBUG(dbgs() << "Class: " << Class << "\n"); 1102 DEBUG(dbgs() << " Class: " << Class << "\n");
1718 1103
1719 switch (Class) { 1104 switch (Class) {
1720 case IC_Release: { 1105 case ARCInstKind::Release: {
1721 Arg = GetObjCArg(Inst); 1106 Arg = GetArgRCIdentityRoot(Inst);
1722 1107
1723 PtrState &S = MyStates.getPtrBottomUpState(Arg); 1108 BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
1724 1109 NestingDetected |= S.InitBottomUp(MDKindCache, Inst);
1725 // If we see two releases in a row on the same pointer. If so, make
1726 // a note, and we'll cicle back to revisit it after we've
1727 // hopefully eliminated the second release, which may allow us to
1728 // eliminate the first release too.
1729 // Theoretically we could implement removal of nested retain+release
1730 // pairs by making PtrState hold a stack of states, but this is
1731 // simple and avoids adding overhead for the non-nested case.
1732 if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) {
1733 DEBUG(dbgs() << "Found nested releases (i.e. a release pair)\n");
1734 NestingDetected = true;
1735 }
1736
1737 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
1738 Sequence NewSeq = ReleaseMetadata ? S_MovableRelease : S_Release;
1739 ANNOTATE_BOTTOMUP(Inst, Arg, S.GetSeq(), NewSeq);
1740 S.ResetSequenceProgress(NewSeq);
1741 S.SetReleaseMetadata(ReleaseMetadata);
1742 S.SetKnownSafe(S.HasKnownPositiveRefCount());
1743 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
1744 S.InsertCall(Inst);
1745 S.SetKnownPositiveRefCount();
1746 break; 1110 break;
1747 } 1111 }
1748 case IC_RetainBlock: 1112 case ARCInstKind::RetainBlock:
1749 // In OptimizeIndividualCalls, we have strength reduced all optimizable 1113 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1750 // objc_retainBlocks to objc_retains. Thus at this point any 1114 // objc_retainBlocks to objc_retains. Thus at this point any
1751 // objc_retainBlocks that we see are not optimizable. 1115 // objc_retainBlocks that we see are not optimizable.
1752 break; 1116 break;
1753 case IC_Retain: 1117 case ARCInstKind::Retain:
1754 case IC_RetainRV: { 1118 case ARCInstKind::RetainRV: {
1755 Arg = GetObjCArg(Inst); 1119 Arg = GetArgRCIdentityRoot(Inst);
1756 1120 BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
1757 PtrState &S = MyStates.getPtrBottomUpState(Arg); 1121 if (S.MatchWithRetain()) {
1758 S.SetKnownPositiveRefCount(); 1122 // Don't do retain+release tracking for ARCInstKind::RetainRV, because
1759 1123 // it's better to let it remain as the first instruction after a call.
1760 Sequence OldSeq = S.GetSeq(); 1124 if (Class != ARCInstKind::RetainRV) {
1761 switch (OldSeq) { 1125 DEBUG(llvm::dbgs() << " Matching with: " << *Inst << "\n");
1762 case S_Stop:
1763 case S_Release:
1764 case S_MovableRelease:
1765 case S_Use:
1766 // If OldSeq is not S_Use or OldSeq is S_Use and we are tracking an
1767 // imprecise release, clear our reverse insertion points.
1768 if (OldSeq != S_Use || S.IsTrackingImpreciseReleases())
1769 S.ClearReverseInsertPts();
1770 // FALL THROUGH
1771 case S_CanRelease:
1772 // Don't do retain+release tracking for IC_RetainRV, because it's
1773 // better to let it remain as the first instruction after a call.
1774 if (Class != IC_RetainRV)
1775 Retains[Inst] = S.GetRRInfo(); 1126 Retains[Inst] = S.GetRRInfo();
1127 }
1776 S.ClearSequenceProgress(); 1128 S.ClearSequenceProgress();
1777 break; 1129 }
1778 case S_None:
1779 break;
1780 case S_Retain:
1781 llvm_unreachable("bottom-up pointer in retain state!");
1782 }
1783 ANNOTATE_BOTTOMUP(Inst, Arg, OldSeq, S.GetSeq());
1784 // A retain moving bottom up can be a use. 1130 // A retain moving bottom up can be a use.
1785 break; 1131 break;
1786 } 1132 }
1787 case IC_AutoreleasepoolPop: 1133 case ARCInstKind::AutoreleasepoolPop:
1788 // Conservatively, clear MyStates for all known pointers. 1134 // Conservatively, clear MyStates for all known pointers.
1789 MyStates.clearBottomUpPointers(); 1135 MyStates.clearBottomUpPointers();
1790 return NestingDetected; 1136 return NestingDetected;
1791 case IC_AutoreleasepoolPush: 1137 case ARCInstKind::AutoreleasepoolPush:
1792 case IC_None: 1138 case ARCInstKind::None:
1793 // These are irrelevant. 1139 // These are irrelevant.
1794 return NestingDetected; 1140 return NestingDetected;
1795 case IC_User: 1141 case ARCInstKind::User:
1796 // If we have a store into an alloca of a pointer we are tracking, the 1142 // If we have a store into an alloca of a pointer we are tracking, the
1797 // pointer has multiple owners implying that we must be more conservative. 1143 // pointer has multiple owners implying that we must be more conservative.
1798 // 1144 //
1799 // This comes up in the context of a pointer being ``KnownSafe''. In the 1145 // This comes up in the context of a pointer being ``KnownSafe''. In the
1800 // presence of a block being initialized, the frontend will emit the 1146 // presence of a block being initialized, the frontend will emit the
1804 // one direction, will match the inner retain on the original pointer with 1150 // one direction, will match the inner retain on the original pointer with
1805 // the guard release on the original pointer. This is fixed by ensuring that 1151 // the guard release on the original pointer. This is fixed by ensuring that
1806 // in the presence of allocas we only unconditionally remove pointers if 1152 // in the presence of allocas we only unconditionally remove pointers if
1807 // both our retain and our release are KnownSafe. 1153 // both our retain and our release are KnownSafe.
1808 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 1154 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1809 if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand())) { 1155 const DataLayout &DL = BB->getModule()->getDataLayout();
1810 BBState::ptr_iterator I = MyStates.findPtrBottomUpState( 1156 if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand(), DL)) {
1811 StripPointerCastsAndObjCCalls(SI->getValueOperand())); 1157 auto I = MyStates.findPtrBottomUpState(
1158 GetRCIdentityRoot(SI->getValueOperand()));
1812 if (I != MyStates.bottom_up_ptr_end()) 1159 if (I != MyStates.bottom_up_ptr_end())
1813 MultiOwnersSet.insert(I->first); 1160 MultiOwnersSet.insert(I->first);
1814 } 1161 }
1815 } 1162 }
1816 break; 1163 break;
1818 break; 1165 break;
1819 } 1166 }
1820 1167
1821 // Consider any other possible effects of this instruction on each 1168 // Consider any other possible effects of this instruction on each
1822 // pointer being tracked. 1169 // pointer being tracked.
1823 for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(), 1170 for (auto MI = MyStates.bottom_up_ptr_begin(),
1824 ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) { 1171 ME = MyStates.bottom_up_ptr_end();
1172 MI != ME; ++MI) {
1825 const Value *Ptr = MI->first; 1173 const Value *Ptr = MI->first;
1826 if (Ptr == Arg) 1174 if (Ptr == Arg)
1827 continue; // Handled above. 1175 continue; // Handled above.
1828 PtrState &S = MI->second; 1176 BottomUpPtrState &S = MI->second;
1829 Sequence Seq = S.GetSeq(); 1177
1830 1178 if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class))
1831 // Check for possible releases. 1179 continue;
1832 if (CanAlterRefCount(Inst, Ptr, PA, Class)) { 1180
1833 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr 1181 S.HandlePotentialUse(BB, Inst, Ptr, PA, Class);
1834 << "\n");
1835 S.ClearKnownPositiveRefCount();
1836 switch (Seq) {
1837 case S_Use:
1838 S.SetSeq(S_CanRelease);
1839 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S.GetSeq());
1840 continue;
1841 case S_CanRelease:
1842 case S_Release:
1843 case S_MovableRelease:
1844 case S_Stop:
1845 case S_None:
1846 break;
1847 case S_Retain:
1848 llvm_unreachable("bottom-up pointer in retain state!");
1849 }
1850 }
1851
1852 // Check for possible direct uses.
1853 switch (Seq) {
1854 case S_Release:
1855 case S_MovableRelease:
1856 if (CanUse(Inst, Ptr, PA, Class)) {
1857 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
1858 << "\n");
1859 assert(!S.HasReverseInsertPts());
1860 // If this is an invoke instruction, we're scanning it as part of
1861 // one of its successor blocks, since we can't insert code after it
1862 // in its own block, and we don't want to split critical edges.
1863 if (isa<InvokeInst>(Inst))
1864 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
1865 else
1866 S.InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst)));
1867 S.SetSeq(S_Use);
1868 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
1869 } else if (Seq == S_Release && IsUser(Class)) {
1870 DEBUG(dbgs() << "PreciseReleaseUse: Seq: " << Seq << "; " << *Ptr
1871 << "\n");
1872 // Non-movable releases depend on any possible objc pointer use.
1873 S.SetSeq(S_Stop);
1874 ANNOTATE_BOTTOMUP(Inst, Ptr, S_Release, S_Stop);
1875 assert(!S.HasReverseInsertPts());
1876 // As above; handle invoke specially.
1877 if (isa<InvokeInst>(Inst))
1878 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
1879 else
1880 S.InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst)));
1881 }
1882 break;
1883 case S_Stop:
1884 if (CanUse(Inst, Ptr, PA, Class)) {
1885 DEBUG(dbgs() << "PreciseStopUse: Seq: " << Seq << "; " << *Ptr
1886 << "\n");
1887 S.SetSeq(S_Use);
1888 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
1889 }
1890 break;
1891 case S_CanRelease:
1892 case S_Use:
1893 case S_None:
1894 break;
1895 case S_Retain:
1896 llvm_unreachable("bottom-up pointer in retain state!");
1897 }
1898 } 1182 }
1899 1183
1900 return NestingDetected; 1184 return NestingDetected;
1901 } 1185 }
1902 1186
1903 bool 1187 bool ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
1904 ObjCARCOpt::VisitBottomUp(BasicBlock *BB, 1188 DenseMap<const BasicBlock *, BBState> &BBStates,
1905 DenseMap<const BasicBlock *, BBState> &BBStates, 1189 BlotMapVector<Value *, RRInfo> &Retains) {
1906 MapVector<Value *, RRInfo> &Retains) {
1907 1190
1908 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n"); 1191 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
1909 1192
1910 bool NestingDetected = false; 1193 bool NestingDetected = false;
1911 BBState &MyStates = BBStates[BB]; 1194 BBState &MyStates = BBStates[BB];
1926 assert(I != BBStates.end()); 1209 assert(I != BBStates.end());
1927 MyStates.MergeSucc(I->second); 1210 MyStates.MergeSucc(I->second);
1928 } 1211 }
1929 } 1212 }
1930 1213
1931 // If ARC Annotations are enabled, output the current state of pointers at the 1214 DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB] << "\n"
1932 // bottom of the basic block. 1215 << "Performing Dataflow:\n");
1933 ANNOTATE_BOTTOMUP_BBEND(MyStates, BB);
1934 1216
1935 // Visit all the instructions, bottom-up. 1217 // Visit all the instructions, bottom-up.
1936 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) { 1218 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
1937 Instruction *Inst = std::prev(I); 1219 Instruction *Inst = std::prev(I);
1938 1220
1939 // Invoke instructions are visited as part of their successors (below). 1221 // Invoke instructions are visited as part of their successors (below).
1940 if (isa<InvokeInst>(Inst)) 1222 if (isa<InvokeInst>(Inst))
1941 continue; 1223 continue;
1942 1224
1943 DEBUG(dbgs() << "Visiting " << *Inst << "\n"); 1225 DEBUG(dbgs() << " Visiting " << *Inst << "\n");
1944 1226
1945 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates); 1227 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
1946 } 1228 }
1947 1229
1948 // If there's a predecessor with an invoke, visit the invoke as if it were 1230 // If there's a predecessor with an invoke, visit the invoke as if it were
1953 BasicBlock *Pred = *PI; 1235 BasicBlock *Pred = *PI;
1954 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back())) 1236 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
1955 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates); 1237 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
1956 } 1238 }
1957 1239
1958 // If ARC Annotations are enabled, output the current state of pointers at the 1240 DEBUG(llvm::dbgs() << "\nFinal State:\n" << BBStates[BB] << "\n");
1959 // top of the basic block.
1960 ANNOTATE_BOTTOMUP_BBSTART(MyStates, BB);
1961 1241
1962 return NestingDetected; 1242 return NestingDetected;
1963 } 1243 }
1964 1244
1965 bool 1245 bool
1966 ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst, 1246 ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
1967 DenseMap<Value *, RRInfo> &Releases, 1247 DenseMap<Value *, RRInfo> &Releases,
1968 BBState &MyStates) { 1248 BBState &MyStates) {
1969 bool NestingDetected = false; 1249 bool NestingDetected = false;
1970 InstructionClass Class = GetInstructionClass(Inst); 1250 ARCInstKind Class = GetARCInstKind(Inst);
1971 const Value *Arg = nullptr; 1251 const Value *Arg = nullptr;
1972 1252
1253 DEBUG(llvm::dbgs() << " Class: " << Class << "\n");
1254
1973 switch (Class) { 1255 switch (Class) {
1974 case IC_RetainBlock: 1256 case ARCInstKind::RetainBlock:
1975 // In OptimizeIndividualCalls, we have strength reduced all optimizable 1257 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1976 // objc_retainBlocks to objc_retains. Thus at this point any 1258 // objc_retainBlocks to objc_retains. Thus at this point any
1977 // objc_retainBlocks that we see are not optimizable. 1259 // objc_retainBlocks that we see are not optimizable. We need to break since
1260 // a retain can be a potential use.
1978 break; 1261 break;
1979 case IC_Retain: 1262 case ARCInstKind::Retain:
1980 case IC_RetainRV: { 1263 case ARCInstKind::RetainRV: {
1981 Arg = GetObjCArg(Inst); 1264 Arg = GetArgRCIdentityRoot(Inst);
1982 1265 TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
1983 PtrState &S = MyStates.getPtrTopDownState(Arg); 1266 NestingDetected |= S.InitTopDown(Class, Inst);
1984 1267 // A retain can be a potential use; proceed to the generic checking
1985 // Don't do retain+release tracking for IC_RetainRV, because it's
1986 // better to let it remain as the first instruction after a call.
1987 if (Class != IC_RetainRV) {
1988 // If we see two retains in a row on the same pointer. If so, make
1989 // a note, and we'll cicle back to revisit it after we've
1990 // hopefully eliminated the second retain, which may allow us to
1991 // eliminate the first retain too.
1992 // Theoretically we could implement removal of nested retain+release
1993 // pairs by making PtrState hold a stack of states, but this is
1994 // simple and avoids adding overhead for the non-nested case.
1995 if (S.GetSeq() == S_Retain)
1996 NestingDetected = true;
1997
1998 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_Retain);
1999 S.ResetSequenceProgress(S_Retain);
2000 S.SetKnownSafe(S.HasKnownPositiveRefCount());
2001 S.InsertCall(Inst);
2002 }
2003
2004 S.SetKnownPositiveRefCount();
2005
2006 // A retain can be a potential use; procede to the generic checking
2007 // code below. 1268 // code below.
2008 break; 1269 break;
2009 } 1270 }
2010 case IC_Release: { 1271 case ARCInstKind::Release: {
2011 Arg = GetObjCArg(Inst); 1272 Arg = GetArgRCIdentityRoot(Inst);
2012 1273 TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
2013 PtrState &S = MyStates.getPtrTopDownState(Arg); 1274 // Try to form a tentative pair in between this release instruction and the
2014 S.ClearKnownPositiveRefCount(); 1275 // top down pointers that we are tracking.
2015 1276 if (S.MatchWithRelease(MDKindCache, Inst)) {
2016 Sequence OldSeq = S.GetSeq(); 1277 // If we succeed, copy S's RRInfo into the Release -> {Retain Set
2017 1278 // Map}. Then we clear S.
2018 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind); 1279 DEBUG(llvm::dbgs() << " Matching with: " << *Inst << "\n");
2019
2020 switch (OldSeq) {
2021 case S_Retain:
2022 case S_CanRelease:
2023 if (OldSeq == S_Retain || ReleaseMetadata != nullptr)
2024 S.ClearReverseInsertPts();
2025 // FALL THROUGH
2026 case S_Use:
2027 S.SetReleaseMetadata(ReleaseMetadata);
2028 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
2029 Releases[Inst] = S.GetRRInfo(); 1280 Releases[Inst] = S.GetRRInfo();
2030 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_None);
2031 S.ClearSequenceProgress(); 1281 S.ClearSequenceProgress();
2032 break;
2033 case S_None:
2034 break;
2035 case S_Stop:
2036 case S_Release:
2037 case S_MovableRelease:
2038 llvm_unreachable("top-down pointer in release state!");
2039 } 1282 }
2040 break; 1283 break;
2041 } 1284 }
2042 case IC_AutoreleasepoolPop: 1285 case ARCInstKind::AutoreleasepoolPop:
2043 // Conservatively, clear MyStates for all known pointers. 1286 // Conservatively, clear MyStates for all known pointers.
2044 MyStates.clearTopDownPointers(); 1287 MyStates.clearTopDownPointers();
2045 return NestingDetected; 1288 return false;
2046 case IC_AutoreleasepoolPush: 1289 case ARCInstKind::AutoreleasepoolPush:
2047 case IC_None: 1290 case ARCInstKind::None:
2048 // These are irrelevant. 1291 // These can not be uses of
2049 return NestingDetected; 1292 return false;
2050 default: 1293 default:
2051 break; 1294 break;
2052 } 1295 }
2053 1296
2054 // Consider any other possible effects of this instruction on each 1297 // Consider any other possible effects of this instruction on each
2055 // pointer being tracked. 1298 // pointer being tracked.
2056 for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(), 1299 for (auto MI = MyStates.top_down_ptr_begin(),
2057 ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) { 1300 ME = MyStates.top_down_ptr_end();
1301 MI != ME; ++MI) {
2058 const Value *Ptr = MI->first; 1302 const Value *Ptr = MI->first;
2059 if (Ptr == Arg) 1303 if (Ptr == Arg)
2060 continue; // Handled above. 1304 continue; // Handled above.
2061 PtrState &S = MI->second; 1305 TopDownPtrState &S = MI->second;
2062 Sequence Seq = S.GetSeq(); 1306 if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class))
2063 1307 continue;
2064 // Check for possible releases. 1308
2065 if (CanAlterRefCount(Inst, Ptr, PA, Class)) { 1309 S.HandlePotentialUse(Inst, Ptr, PA, Class);
2066 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
2067 << "\n");
2068 S.ClearKnownPositiveRefCount();
2069 switch (Seq) {
2070 case S_Retain:
2071 S.SetSeq(S_CanRelease);
2072 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_CanRelease);
2073 assert(!S.HasReverseInsertPts());
2074 S.InsertReverseInsertPt(Inst);
2075
2076 // One call can't cause a transition from S_Retain to S_CanRelease
2077 // and S_CanRelease to S_Use. If we've made the first transition,
2078 // we're done.
2079 continue;
2080 case S_Use:
2081 case S_CanRelease:
2082 case S_None:
2083 break;
2084 case S_Stop:
2085 case S_Release:
2086 case S_MovableRelease:
2087 llvm_unreachable("top-down pointer in release state!");
2088 }
2089 }
2090
2091 // Check for possible direct uses.
2092 switch (Seq) {
2093 case S_CanRelease:
2094 if (CanUse(Inst, Ptr, PA, Class)) {
2095 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
2096 << "\n");
2097 S.SetSeq(S_Use);
2098 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_Use);
2099 }
2100 break;
2101 case S_Retain:
2102 case S_Use:
2103 case S_None:
2104 break;
2105 case S_Stop:
2106 case S_Release:
2107 case S_MovableRelease:
2108 llvm_unreachable("top-down pointer in release state!");
2109 }
2110 } 1310 }
2111 1311
2112 return NestingDetected; 1312 return NestingDetected;
2113 } 1313 }
2114 1314
2136 assert(I != BBStates.end()); 1336 assert(I != BBStates.end());
2137 MyStates.MergePred(I->second); 1337 MyStates.MergePred(I->second);
2138 } 1338 }
2139 } 1339 }
2140 1340
2141 // If ARC Annotations are enabled, output the current state of pointers at the 1341 DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB] << "\n"
2142 // top of the basic block. 1342 << "Performing Dataflow:\n");
2143 ANNOTATE_TOPDOWN_BBSTART(MyStates, BB);
2144 1343
2145 // Visit all the instructions, top-down. 1344 // Visit all the instructions, top-down.
2146 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 1345 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
2147 Instruction *Inst = I; 1346 Instruction *Inst = I;
2148 1347
2149 DEBUG(dbgs() << "Visiting " << *Inst << "\n"); 1348 DEBUG(dbgs() << " Visiting " << *Inst << "\n");
2150 1349
2151 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates); 1350 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
2152 } 1351 }
2153 1352
2154 // If ARC Annotations are enabled, output the current state of pointers at the 1353 DEBUG(llvm::dbgs() << "\nState Before Checking for CFG Hazards:\n"
2155 // bottom of the basic block. 1354 << BBStates[BB] << "\n\n");
2156 ANNOTATE_TOPDOWN_BBEND(MyStates, BB);
2157
2158 #ifdef ARC_ANNOTATIONS
2159 if (!(EnableARCAnnotations && DisableCheckForCFGHazards))
2160 #endif
2161 CheckForCFGHazards(BB, BBStates, MyStates); 1355 CheckForCFGHazards(BB, BBStates, MyStates);
1356 DEBUG(llvm::dbgs() << "Final State:\n" << BBStates[BB] << "\n");
2162 return NestingDetected; 1357 return NestingDetected;
2163 } 1358 }
2164 1359
2165 static void 1360 static void
2166 ComputePostOrders(Function &F, 1361 ComputePostOrders(Function &F,
2242 } 1437 }
2243 } 1438 }
2244 } 1439 }
2245 1440
2246 // Visit the function both top-down and bottom-up. 1441 // Visit the function both top-down and bottom-up.
2247 bool 1442 bool ObjCARCOpt::Visit(Function &F,
2248 ObjCARCOpt::Visit(Function &F, 1443 DenseMap<const BasicBlock *, BBState> &BBStates,
2249 DenseMap<const BasicBlock *, BBState> &BBStates, 1444 BlotMapVector<Value *, RRInfo> &Retains,
2250 MapVector<Value *, RRInfo> &Retains, 1445 DenseMap<Value *, RRInfo> &Releases) {
2251 DenseMap<Value *, RRInfo> &Releases) {
2252 1446
2253 // Use reverse-postorder traversals, because we magically know that loops 1447 // Use reverse-postorder traversals, because we magically know that loops
2254 // will be well behaved, i.e. they won't repeatedly call retain on a single 1448 // will be well behaved, i.e. they won't repeatedly call retain on a single
2255 // pointer without doing a release. We can't use the ReversePostOrderTraversal 1449 // pointer without doing a release. We can't use the ReversePostOrderTraversal
2256 // class here because we want the reverse-CFG postorder to consider each 1450 // class here because we want the reverse-CFG postorder to consider each
2257 // function exit point, and we want to ignore selected cycle edges. 1451 // function exit point, and we want to ignore selected cycle edges.
2258 SmallVector<BasicBlock *, 16> PostOrder; 1452 SmallVector<BasicBlock *, 16> PostOrder;
2259 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder; 1453 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
2260 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder, 1454 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
2261 NoObjCARCExceptionsMDKind, 1455 MDKindCache.get(ARCMDKindID::NoObjCARCExceptions),
2262 BBStates); 1456 BBStates);
2263 1457
2264 // Use reverse-postorder on the reverse CFG for bottom-up. 1458 // Use reverse-postorder on the reverse CFG for bottom-up.
2265 bool BottomUpNestingDetected = false; 1459 bool BottomUpNestingDetected = false;
2266 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I = 1460 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
2277 1471
2278 return TopDownNestingDetected && BottomUpNestingDetected; 1472 return TopDownNestingDetected && BottomUpNestingDetected;
2279 } 1473 }
2280 1474
2281 /// Move the calls in RetainsToMove and ReleasesToMove. 1475 /// Move the calls in RetainsToMove and ReleasesToMove.
2282 void ObjCARCOpt::MoveCalls(Value *Arg, 1476 void ObjCARCOpt::MoveCalls(Value *Arg, RRInfo &RetainsToMove,
2283 RRInfo &RetainsToMove,
2284 RRInfo &ReleasesToMove, 1477 RRInfo &ReleasesToMove,
2285 MapVector<Value *, RRInfo> &Retains, 1478 BlotMapVector<Value *, RRInfo> &Retains,
2286 DenseMap<Value *, RRInfo> &Releases, 1479 DenseMap<Value *, RRInfo> &Releases,
2287 SmallVectorImpl<Instruction *> &DeadInsts, 1480 SmallVectorImpl<Instruction *> &DeadInsts,
2288 Module *M) { 1481 Module *M) {
2289 Type *ArgTy = Arg->getType(); 1482 Type *ArgTy = Arg->getType();
2290 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext())); 1483 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
2293 1486
2294 // Insert the new retain and release calls. 1487 // Insert the new retain and release calls.
2295 for (Instruction *InsertPt : ReleasesToMove.ReverseInsertPts) { 1488 for (Instruction *InsertPt : ReleasesToMove.ReverseInsertPts) {
2296 Value *MyArg = ArgTy == ParamTy ? Arg : 1489 Value *MyArg = ArgTy == ParamTy ? Arg :
2297 new BitCastInst(Arg, ParamTy, "", InsertPt); 1490 new BitCastInst(Arg, ParamTy, "", InsertPt);
2298 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain); 1491 Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
2299 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt); 1492 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
2300 Call->setDoesNotThrow(); 1493 Call->setDoesNotThrow();
2301 Call->setTailCall(); 1494 Call->setTailCall();
2302 1495
2303 DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n" 1496 DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n"
2304 "At insertion point: " << *InsertPt << "\n"); 1497 "At insertion point: " << *InsertPt << "\n");
2305 } 1498 }
2306 for (Instruction *InsertPt : RetainsToMove.ReverseInsertPts) { 1499 for (Instruction *InsertPt : RetainsToMove.ReverseInsertPts) {
2307 Value *MyArg = ArgTy == ParamTy ? Arg : 1500 Value *MyArg = ArgTy == ParamTy ? Arg :
2308 new BitCastInst(Arg, ParamTy, "", InsertPt); 1501 new BitCastInst(Arg, ParamTy, "", InsertPt);
2309 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release); 1502 Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
2310 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt); 1503 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
2311 // Attach a clang.imprecise_release metadata tag, if appropriate. 1504 // Attach a clang.imprecise_release metadata tag, if appropriate.
2312 if (MDNode *M = ReleasesToMove.ReleaseMetadata) 1505 if (MDNode *M = ReleasesToMove.ReleaseMetadata)
2313 Call->setMetadata(ImpreciseReleaseMDKind, M); 1506 Call->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease), M);
2314 Call->setDoesNotThrow(); 1507 Call->setDoesNotThrow();
2315 if (ReleasesToMove.IsTailCallRelease) 1508 if (ReleasesToMove.IsTailCallRelease)
2316 Call->setTailCall(); 1509 Call->setTailCall();
2317 1510
2318 DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n" 1511 DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n"
2331 DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n"); 1524 DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
2332 } 1525 }
2333 1526
2334 } 1527 }
2335 1528
2336 bool 1529 bool ObjCARCOpt::PairUpRetainsAndReleases(
2337 ObjCARCOpt::ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> 1530 DenseMap<const BasicBlock *, BBState> &BBStates,
2338 &BBStates, 1531 BlotMapVector<Value *, RRInfo> &Retains,
2339 MapVector<Value *, RRInfo> &Retains, 1532 DenseMap<Value *, RRInfo> &Releases, Module *M,
2340 DenseMap<Value *, RRInfo> &Releases, 1533 SmallVectorImpl<Instruction *> &NewRetains,
2341 Module *M, 1534 SmallVectorImpl<Instruction *> &NewReleases,
2342 SmallVectorImpl<Instruction *> &NewRetains, 1535 SmallVectorImpl<Instruction *> &DeadInsts, RRInfo &RetainsToMove,
2343 SmallVectorImpl<Instruction *> &NewReleases, 1536 RRInfo &ReleasesToMove, Value *Arg, bool KnownSafe,
2344 SmallVectorImpl<Instruction *> &DeadInsts, 1537 bool &AnyPairsCompletelyEliminated) {
2345 RRInfo &RetainsToMove,
2346 RRInfo &ReleasesToMove,
2347 Value *Arg,
2348 bool KnownSafe,
2349 bool &AnyPairsCompletelyEliminated) {
2350 // If a pair happens in a region where it is known that the reference count 1538 // If a pair happens in a region where it is known that the reference count
2351 // is already incremented, we can similarly ignore possible decrements unless 1539 // is already incremented, we can similarly ignore possible decrements unless
2352 // we are dealing with a retainable object with multiple provenance sources. 1540 // we are dealing with a retainable object with multiple provenance sources.
2353 bool KnownSafeTD = true, KnownSafeBU = true; 1541 bool KnownSafeTD = true, KnownSafeBU = true;
2354 bool MultipleOwners = false; 1542 bool MultipleOwners = false;
2365 bool FirstRelease = true; 1553 bool FirstRelease = true;
2366 for (;;) { 1554 for (;;) {
2367 for (SmallVectorImpl<Instruction *>::const_iterator 1555 for (SmallVectorImpl<Instruction *>::const_iterator
2368 NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) { 1556 NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
2369 Instruction *NewRetain = *NI; 1557 Instruction *NewRetain = *NI;
2370 MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain); 1558 auto It = Retains.find(NewRetain);
2371 assert(It != Retains.end()); 1559 assert(It != Retains.end());
2372 const RRInfo &NewRetainRRI = It->second; 1560 const RRInfo &NewRetainRRI = It->second;
2373 KnownSafeTD &= NewRetainRRI.KnownSafe; 1561 KnownSafeTD &= NewRetainRRI.KnownSafe;
2374 MultipleOwners = 1562 MultipleOwners =
2375 MultipleOwners || MultiOwnersSet.count(GetObjCArg(NewRetain)); 1563 MultipleOwners || MultiOwnersSet.count(GetArgRCIdentityRoot(NewRetain));
2376 for (Instruction *NewRetainRelease : NewRetainRRI.Calls) { 1564 for (Instruction *NewRetainRelease : NewRetainRRI.Calls) {
2377 DenseMap<Value *, RRInfo>::const_iterator Jt = 1565 auto Jt = Releases.find(NewRetainRelease);
2378 Releases.find(NewRetainRelease);
2379 if (Jt == Releases.end()) 1566 if (Jt == Releases.end())
2380 return false; 1567 return false;
2381 const RRInfo &NewRetainReleaseRRI = Jt->second; 1568 const RRInfo &NewRetainReleaseRRI = Jt->second;
2382 1569
2383 // If the release does not have a reference to the retain as well, 1570 // If the release does not have a reference to the retain as well,
2442 1629
2443 // Back the other way. 1630 // Back the other way.
2444 for (SmallVectorImpl<Instruction *>::const_iterator 1631 for (SmallVectorImpl<Instruction *>::const_iterator
2445 NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) { 1632 NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
2446 Instruction *NewRelease = *NI; 1633 Instruction *NewRelease = *NI;
2447 DenseMap<Value *, RRInfo>::const_iterator It = 1634 auto It = Releases.find(NewRelease);
2448 Releases.find(NewRelease);
2449 assert(It != Releases.end()); 1635 assert(It != Releases.end());
2450 const RRInfo &NewReleaseRRI = It->second; 1636 const RRInfo &NewReleaseRRI = It->second;
2451 KnownSafeBU &= NewReleaseRRI.KnownSafe; 1637 KnownSafeBU &= NewReleaseRRI.KnownSafe;
2452 CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted; 1638 CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
2453 for (Instruction *NewReleaseRetain : NewReleaseRRI.Calls) { 1639 for (Instruction *NewReleaseRetain : NewReleaseRRI.Calls) {
2454 MapVector<Value *, RRInfo>::const_iterator Jt = 1640 auto Jt = Retains.find(NewReleaseRetain);
2455 Retains.find(NewReleaseRetain);
2456 if (Jt == Retains.end()) 1641 if (Jt == Retains.end())
2457 return false; 1642 return false;
2458 const RRInfo &NewReleaseRetainRRI = Jt->second; 1643 const RRInfo &NewReleaseRetainRRI = Jt->second;
2459 1644
2460 // If the retain does not have a reference to the release as well, 1645 // If the retain does not have a reference to the release as well,
2502 } 1687 }
2503 NewReleases.clear(); 1688 NewReleases.clear();
2504 if (NewRetains.empty()) break; 1689 if (NewRetains.empty()) break;
2505 } 1690 }
2506 1691
2507 // If the pointer is known incremented in 1 direction and we do not have 1692 // We can only remove pointers if we are known safe in both directions.
2508 // MultipleOwners, we can safely remove the retain/releases. Otherwise we need 1693 bool UnconditionallySafe = KnownSafeTD && KnownSafeBU;
2509 // to be known safe in both directions.
2510 bool UnconditionallySafe = (KnownSafeTD && KnownSafeBU) ||
2511 ((KnownSafeTD || KnownSafeBU) && !MultipleOwners);
2512 if (UnconditionallySafe) { 1694 if (UnconditionallySafe) {
2513 RetainsToMove.ReverseInsertPts.clear(); 1695 RetainsToMove.ReverseInsertPts.clear();
2514 ReleasesToMove.ReverseInsertPts.clear(); 1696 ReleasesToMove.ReverseInsertPts.clear();
2515 NewCount = 0; 1697 NewCount = 0;
2516 } else { 1698 } else {
2536 // TODO: It's theoretically possible to do code motion in this case, as 1718 // TODO: It's theoretically possible to do code motion in this case, as
2537 // long as the existing imbalances are maintained. 1719 // long as the existing imbalances are maintained.
2538 if (OldDelta != 0) 1720 if (OldDelta != 0)
2539 return false; 1721 return false;
2540 1722
2541 #ifdef ARC_ANNOTATIONS
2542 // Do not move calls if ARC annotations are requested.
2543 if (EnableARCAnnotations)
2544 return false;
2545 #endif // ARC_ANNOTATIONS
2546
2547 Changed = true; 1723 Changed = true;
2548 assert(OldCount != 0 && "Unreachable code?"); 1724 assert(OldCount != 0 && "Unreachable code?");
2549 NumRRs += OldCount - NewCount; 1725 NumRRs += OldCount - NewCount;
2550 // Set to true if we completely removed any RR pairs. 1726 // Set to true if we completely removed any RR pairs.
2551 AnyPairsCompletelyEliminated = NewCount == 0; 1727 AnyPairsCompletelyEliminated = NewCount == 0;
2554 return true; 1730 return true;
2555 } 1731 }
2556 1732
2557 /// Identify pairings between the retains and releases, and delete and/or move 1733 /// Identify pairings between the retains and releases, and delete and/or move
2558 /// them. 1734 /// them.
2559 bool 1735 bool ObjCARCOpt::PerformCodePlacement(
2560 ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState> 1736 DenseMap<const BasicBlock *, BBState> &BBStates,
2561 &BBStates, 1737 BlotMapVector<Value *, RRInfo> &Retains,
2562 MapVector<Value *, RRInfo> &Retains, 1738 DenseMap<Value *, RRInfo> &Releases, Module *M) {
2563 DenseMap<Value *, RRInfo> &Releases,
2564 Module *M) {
2565 DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n"); 1739 DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
2566 1740
2567 bool AnyPairsCompletelyEliminated = false; 1741 bool AnyPairsCompletelyEliminated = false;
2568 RRInfo RetainsToMove; 1742 RRInfo RetainsToMove;
2569 RRInfo ReleasesToMove; 1743 RRInfo ReleasesToMove;
2570 SmallVector<Instruction *, 4> NewRetains; 1744 SmallVector<Instruction *, 4> NewRetains;
2571 SmallVector<Instruction *, 4> NewReleases; 1745 SmallVector<Instruction *, 4> NewReleases;
2572 SmallVector<Instruction *, 8> DeadInsts; 1746 SmallVector<Instruction *, 8> DeadInsts;
2573 1747
2574 // Visit each retain. 1748 // Visit each retain.
2575 for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(), 1749 for (BlotMapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
2576 E = Retains.end(); I != E; ++I) { 1750 E = Retains.end();
1751 I != E; ++I) {
2577 Value *V = I->first; 1752 Value *V = I->first;
2578 if (!V) continue; // blotted 1753 if (!V) continue; // blotted
2579 1754
2580 Instruction *Retain = cast<Instruction>(V); 1755 Instruction *Retain = cast<Instruction>(V);
2581 1756
2582 DEBUG(dbgs() << "Visiting: " << *Retain << "\n"); 1757 DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
2583 1758
2584 Value *Arg = GetObjCArg(Retain); 1759 Value *Arg = GetArgRCIdentityRoot(Retain);
2585 1760
2586 // If the object being released is in static or stack storage, we know it's 1761 // If the object being released is in static or stack storage, we know it's
2587 // not being managed by ObjC reference counting, so we can delete pairs 1762 // not being managed by ObjC reference counting, so we can delete pairs
2588 // regardless of what possible decrements or uses lie between them. 1763 // regardless of what possible decrements or uses lie between them.
2589 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg); 1764 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
2591 // A constant pointer can't be pointing to an object on the heap. It may 1766 // A constant pointer can't be pointing to an object on the heap. It may
2592 // be reference-counted, but it won't be deleted. 1767 // be reference-counted, but it won't be deleted.
2593 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg)) 1768 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
2594 if (const GlobalVariable *GV = 1769 if (const GlobalVariable *GV =
2595 dyn_cast<GlobalVariable>( 1770 dyn_cast<GlobalVariable>(
2596 StripPointerCastsAndObjCCalls(LI->getPointerOperand()))) 1771 GetRCIdentityRoot(LI->getPointerOperand())))
2597 if (GV->isConstant()) 1772 if (GV->isConstant())
2598 KnownSafe = true; 1773 KnownSafe = true;
2599 1774
2600 // Connect the dots between the top-down-collected RetainsToMove and 1775 // Connect the dots between the top-down-collected RetainsToMove and
2601 // bottom-up-collected ReleasesToMove to form sets of related calls. 1776 // bottom-up-collected ReleasesToMove to form sets of related calls.
2602 NewRetains.push_back(Retain); 1777 NewRetains.push_back(Retain);
2603 bool PerformMoveCalls = 1778 bool PerformMoveCalls = PairUpRetainsAndReleases(
2604 ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains, 1779 BBStates, Retains, Releases, M, NewRetains, NewReleases, DeadInsts,
2605 NewReleases, DeadInsts, RetainsToMove, 1780 RetainsToMove, ReleasesToMove, Arg, KnownSafe,
2606 ReleasesToMove, Arg, KnownSafe, 1781 AnyPairsCompletelyEliminated);
2607 AnyPairsCompletelyEliminated);
2608 1782
2609 if (PerformMoveCalls) { 1783 if (PerformMoveCalls) {
2610 // Ok, everything checks out and we're all set. Let's move/delete some 1784 // Ok, everything checks out and we're all set. Let's move/delete some
2611 // code! 1785 // code!
2612 MoveCalls(Arg, RetainsToMove, ReleasesToMove, 1786 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
2638 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { 1812 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2639 Instruction *Inst = &*I++; 1813 Instruction *Inst = &*I++;
2640 1814
2641 DEBUG(dbgs() << "Visiting: " << *Inst << "\n"); 1815 DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
2642 1816
2643 InstructionClass Class = GetBasicInstructionClass(Inst); 1817 ARCInstKind Class = GetBasicARCInstKind(Inst);
2644 if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained) 1818 if (Class != ARCInstKind::LoadWeak &&
1819 Class != ARCInstKind::LoadWeakRetained)
2645 continue; 1820 continue;
2646 1821
2647 // Delete objc_loadWeak calls with no users. 1822 // Delete objc_loadWeak calls with no users.
2648 if (Class == IC_LoadWeak && Inst->use_empty()) { 1823 if (Class == ARCInstKind::LoadWeak && Inst->use_empty()) {
2649 Inst->eraseFromParent(); 1824 Inst->eraseFromParent();
2650 continue; 1825 continue;
2651 } 1826 }
2652 1827
2653 // TODO: For now, just look for an earlier available version of this value 1828 // TODO: For now, just look for an earlier available version of this value
2658 BasicBlock *CurrentBB = Current.getBasicBlockIterator(); 1833 BasicBlock *CurrentBB = Current.getBasicBlockIterator();
2659 for (BasicBlock::iterator B = CurrentBB->begin(), 1834 for (BasicBlock::iterator B = CurrentBB->begin(),
2660 J = Current.getInstructionIterator(); 1835 J = Current.getInstructionIterator();
2661 J != B; --J) { 1836 J != B; --J) {
2662 Instruction *EarlierInst = &*std::prev(J); 1837 Instruction *EarlierInst = &*std::prev(J);
2663 InstructionClass EarlierClass = GetInstructionClass(EarlierInst); 1838 ARCInstKind EarlierClass = GetARCInstKind(EarlierInst);
2664 switch (EarlierClass) { 1839 switch (EarlierClass) {
2665 case IC_LoadWeak: 1840 case ARCInstKind::LoadWeak:
2666 case IC_LoadWeakRetained: { 1841 case ARCInstKind::LoadWeakRetained: {
2667 // If this is loading from the same pointer, replace this load's value 1842 // If this is loading from the same pointer, replace this load's value
2668 // with that one. 1843 // with that one.
2669 CallInst *Call = cast<CallInst>(Inst); 1844 CallInst *Call = cast<CallInst>(Inst);
2670 CallInst *EarlierCall = cast<CallInst>(EarlierInst); 1845 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2671 Value *Arg = Call->getArgOperand(0); 1846 Value *Arg = Call->getArgOperand(0);
2672 Value *EarlierArg = EarlierCall->getArgOperand(0); 1847 Value *EarlierArg = EarlierCall->getArgOperand(0);
2673 switch (PA.getAA()->alias(Arg, EarlierArg)) { 1848 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2674 case AliasAnalysis::MustAlias: 1849 case MustAlias:
2675 Changed = true; 1850 Changed = true;
2676 // If the load has a builtin retain, insert a plain retain for it. 1851 // If the load has a builtin retain, insert a plain retain for it.
2677 if (Class == IC_LoadWeakRetained) { 1852 if (Class == ARCInstKind::LoadWeakRetained) {
2678 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain); 1853 Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
2679 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call); 1854 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
2680 CI->setTailCall(); 1855 CI->setTailCall();
2681 } 1856 }
2682 // Zap the fully redundant load. 1857 // Zap the fully redundant load.
2683 Call->replaceAllUsesWith(EarlierCall); 1858 Call->replaceAllUsesWith(EarlierCall);
2684 Call->eraseFromParent(); 1859 Call->eraseFromParent();
2685 goto clobbered; 1860 goto clobbered;
2686 case AliasAnalysis::MayAlias: 1861 case MayAlias:
2687 case AliasAnalysis::PartialAlias: 1862 case PartialAlias:
2688 goto clobbered; 1863 goto clobbered;
2689 case AliasAnalysis::NoAlias: 1864 case NoAlias:
2690 break; 1865 break;
2691 } 1866 }
2692 break; 1867 break;
2693 } 1868 }
2694 case IC_StoreWeak: 1869 case ARCInstKind::StoreWeak:
2695 case IC_InitWeak: { 1870 case ARCInstKind::InitWeak: {
2696 // If this is storing to the same pointer and has the same size etc. 1871 // If this is storing to the same pointer and has the same size etc.
2697 // replace this load's value with the stored value. 1872 // replace this load's value with the stored value.
2698 CallInst *Call = cast<CallInst>(Inst); 1873 CallInst *Call = cast<CallInst>(Inst);
2699 CallInst *EarlierCall = cast<CallInst>(EarlierInst); 1874 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2700 Value *Arg = Call->getArgOperand(0); 1875 Value *Arg = Call->getArgOperand(0);
2701 Value *EarlierArg = EarlierCall->getArgOperand(0); 1876 Value *EarlierArg = EarlierCall->getArgOperand(0);
2702 switch (PA.getAA()->alias(Arg, EarlierArg)) { 1877 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2703 case AliasAnalysis::MustAlias: 1878 case MustAlias:
2704 Changed = true; 1879 Changed = true;
2705 // If the load has a builtin retain, insert a plain retain for it. 1880 // If the load has a builtin retain, insert a plain retain for it.
2706 if (Class == IC_LoadWeakRetained) { 1881 if (Class == ARCInstKind::LoadWeakRetained) {
2707 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain); 1882 Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
2708 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call); 1883 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
2709 CI->setTailCall(); 1884 CI->setTailCall();
2710 } 1885 }
2711 // Zap the fully redundant load. 1886 // Zap the fully redundant load.
2712 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1)); 1887 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
2713 Call->eraseFromParent(); 1888 Call->eraseFromParent();
2714 goto clobbered; 1889 goto clobbered;
2715 case AliasAnalysis::MayAlias: 1890 case MayAlias:
2716 case AliasAnalysis::PartialAlias: 1891 case PartialAlias:
2717 goto clobbered; 1892 goto clobbered;
2718 case AliasAnalysis::NoAlias: 1893 case NoAlias:
2719 break; 1894 break;
2720 } 1895 }
2721 break; 1896 break;
2722 } 1897 }
2723 case IC_MoveWeak: 1898 case ARCInstKind::MoveWeak:
2724 case IC_CopyWeak: 1899 case ARCInstKind::CopyWeak:
2725 // TOOD: Grab the copied value. 1900 // TOOD: Grab the copied value.
2726 goto clobbered; 1901 goto clobbered;
2727 case IC_AutoreleasepoolPush: 1902 case ARCInstKind::AutoreleasepoolPush:
2728 case IC_None: 1903 case ARCInstKind::None:
2729 case IC_IntrinsicUser: 1904 case ARCInstKind::IntrinsicUser:
2730 case IC_User: 1905 case ARCInstKind::User:
2731 // Weak pointers are only modified through the weak entry points 1906 // Weak pointers are only modified through the weak entry points
2732 // (and arbitrary calls, which could call the weak entry points). 1907 // (and arbitrary calls, which could call the weak entry points).
2733 break; 1908 break;
2734 default: 1909 default:
2735 // Anything else could modify the weak pointer. 1910 // Anything else could modify the weak pointer.
2741 1916
2742 // Then, for each destroyWeak with an alloca operand, check to see if 1917 // Then, for each destroyWeak with an alloca operand, check to see if
2743 // the alloca and all its users can be zapped. 1918 // the alloca and all its users can be zapped.
2744 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { 1919 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2745 Instruction *Inst = &*I++; 1920 Instruction *Inst = &*I++;
2746 InstructionClass Class = GetBasicInstructionClass(Inst); 1921 ARCInstKind Class = GetBasicARCInstKind(Inst);
2747 if (Class != IC_DestroyWeak) 1922 if (Class != ARCInstKind::DestroyWeak)
2748 continue; 1923 continue;
2749 1924
2750 CallInst *Call = cast<CallInst>(Inst); 1925 CallInst *Call = cast<CallInst>(Inst);
2751 Value *Arg = Call->getArgOperand(0); 1926 Value *Arg = Call->getArgOperand(0);
2752 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) { 1927 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
2753 for (User *U : Alloca->users()) { 1928 for (User *U : Alloca->users()) {
2754 const Instruction *UserInst = cast<Instruction>(U); 1929 const Instruction *UserInst = cast<Instruction>(U);
2755 switch (GetBasicInstructionClass(UserInst)) { 1930 switch (GetBasicARCInstKind(UserInst)) {
2756 case IC_InitWeak: 1931 case ARCInstKind::InitWeak:
2757 case IC_StoreWeak: 1932 case ARCInstKind::StoreWeak:
2758 case IC_DestroyWeak: 1933 case ARCInstKind::DestroyWeak:
2759 continue; 1934 continue;
2760 default: 1935 default:
2761 goto done; 1936 goto done;
2762 } 1937 }
2763 } 1938 }
2764 Changed = true; 1939 Changed = true;
2765 for (auto UI = Alloca->user_begin(), UE = Alloca->user_end(); UI != UE;) { 1940 for (auto UI = Alloca->user_begin(), UE = Alloca->user_end(); UI != UE;) {
2766 CallInst *UserInst = cast<CallInst>(*UI++); 1941 CallInst *UserInst = cast<CallInst>(*UI++);
2767 switch (GetBasicInstructionClass(UserInst)) { 1942 switch (GetBasicARCInstKind(UserInst)) {
2768 case IC_InitWeak: 1943 case ARCInstKind::InitWeak:
2769 case IC_StoreWeak: 1944 case ARCInstKind::StoreWeak:
2770 // These functions return their second argument. 1945 // These functions return their second argument.
2771 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1)); 1946 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
2772 break; 1947 break;
2773 case IC_DestroyWeak: 1948 case ARCInstKind::DestroyWeak:
2774 // No return value. 1949 // No return value.
2775 break; 1950 break;
2776 default: 1951 default:
2777 llvm_unreachable("alloca really is used!"); 1952 llvm_unreachable("alloca really is used!");
2778 } 1953 }
2790 // Releases, Retains - These are used to store the results of the main flow 1965 // Releases, Retains - These are used to store the results of the main flow
2791 // analysis. These use Value* as the key instead of Instruction* so that the 1966 // analysis. These use Value* as the key instead of Instruction* so that the
2792 // map stays valid when we get around to rewriting code and calls get 1967 // map stays valid when we get around to rewriting code and calls get
2793 // replaced by arguments. 1968 // replaced by arguments.
2794 DenseMap<Value *, RRInfo> Releases; 1969 DenseMap<Value *, RRInfo> Releases;
2795 MapVector<Value *, RRInfo> Retains; 1970 BlotMapVector<Value *, RRInfo> Retains;
2796 1971
2797 // This is used during the traversal of the function to track the 1972 // This is used during the traversal of the function to track the
2798 // states for each identified object at each block. 1973 // states for each identified object at each block.
2799 DenseMap<const BasicBlock *, BBState> BBStates; 1974 DenseMap<const BasicBlock *, BBState> BBStates;
2800 1975
2823 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain, 1998 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
2824 DepInsts, Visited, PA); 1999 DepInsts, Visited, PA);
2825 if (DepInsts.size() != 1) 2000 if (DepInsts.size() != 1)
2826 return false; 2001 return false;
2827 2002
2828 CallInst *Call = 2003 auto *Call = dyn_cast_or_null<CallInst>(*DepInsts.begin());
2829 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2830 2004
2831 // Check that the pointer is the return value of the call. 2005 // Check that the pointer is the return value of the call.
2832 if (!Call || Arg != Call) 2006 if (!Call || Arg != Call)
2833 return false; 2007 return false;
2834 2008
2835 // Check that the call is a regular call. 2009 // Check that the call is a regular call.
2836 InstructionClass Class = GetBasicInstructionClass(Call); 2010 ARCInstKind Class = GetBasicARCInstKind(Call);
2837 if (Class != IC_CallOrUser && Class != IC_Call) 2011 if (Class != ARCInstKind::CallOrUser && Class != ARCInstKind::Call)
2838 return false; 2012 return false;
2839 2013
2840 return true; 2014 return true;
2841 } 2015 }
2842 2016
2852 FindDependencies(CanChangeRetainCount, Arg, 2026 FindDependencies(CanChangeRetainCount, Arg,
2853 BB, Autorelease, DepInsts, Visited, PA); 2027 BB, Autorelease, DepInsts, Visited, PA);
2854 if (DepInsts.size() != 1) 2028 if (DepInsts.size() != 1)
2855 return nullptr; 2029 return nullptr;
2856 2030
2857 CallInst *Retain = 2031 auto *Retain = dyn_cast_or_null<CallInst>(*DepInsts.begin());
2858 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2859 2032
2860 // Check that we found a retain with the same argument. 2033 // Check that we found a retain with the same argument.
2861 if (!Retain || 2034 if (!Retain || !IsRetain(GetBasicARCInstKind(Retain)) ||
2862 !IsRetain(GetBasicInstructionClass(Retain)) || 2035 GetArgRCIdentityRoot(Retain) != Arg) {
2863 GetObjCArg(Retain) != Arg) {
2864 return nullptr; 2036 return nullptr;
2865 } 2037 }
2866 2038
2867 return Retain; 2039 return Retain;
2868 } 2040 }
2879 FindDependencies(NeedsPositiveRetainCount, Arg, 2051 FindDependencies(NeedsPositiveRetainCount, Arg,
2880 BB, Ret, DepInsts, V, PA); 2052 BB, Ret, DepInsts, V, PA);
2881 if (DepInsts.size() != 1) 2053 if (DepInsts.size() != 1)
2882 return nullptr; 2054 return nullptr;
2883 2055
2884 CallInst *Autorelease = 2056 auto *Autorelease = dyn_cast_or_null<CallInst>(*DepInsts.begin());
2885 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2886 if (!Autorelease) 2057 if (!Autorelease)
2887 return nullptr; 2058 return nullptr;
2888 InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease); 2059 ARCInstKind AutoreleaseClass = GetBasicARCInstKind(Autorelease);
2889 if (!IsAutorelease(AutoreleaseClass)) 2060 if (!IsAutorelease(AutoreleaseClass))
2890 return nullptr; 2061 return nullptr;
2891 if (GetObjCArg(Autorelease) != Arg) 2062 if (GetArgRCIdentityRoot(Autorelease) != Arg)
2892 return nullptr; 2063 return nullptr;
2893 2064
2894 return Autorelease; 2065 return Autorelease;
2895 } 2066 }
2896 2067
2917 DEBUG(dbgs() << "Visiting: " << *Ret << "\n"); 2088 DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
2918 2089
2919 if (!Ret) 2090 if (!Ret)
2920 continue; 2091 continue;
2921 2092
2922 const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0)); 2093 const Value *Arg = GetRCIdentityRoot(Ret->getOperand(0));
2923 2094
2924 // Look for an ``autorelease'' instruction that is a predecessor of Ret and 2095 // Look for an ``autorelease'' instruction that is a predecessor of Ret and
2925 // dependent on Arg such that there are no instructions dependent on Arg 2096 // dependent on Arg such that there are no instructions dependent on Arg
2926 // that need a positive ref count in between the autorelease and Ret. 2097 // that need a positive ref count in between the autorelease and Ret.
2927 CallInst *Autorelease = 2098 CallInst *Autorelease =
2972 llvm::Statistic &NumReleases = 2143 llvm::Statistic &NumReleases =
2973 AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt; 2144 AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt;
2974 2145
2975 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { 2146 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2976 Instruction *Inst = &*I++; 2147 Instruction *Inst = &*I++;
2977 switch (GetBasicInstructionClass(Inst)) { 2148 switch (GetBasicARCInstKind(Inst)) {
2978 default: 2149 default:
2979 break; 2150 break;
2980 case IC_Retain: 2151 case ARCInstKind::Retain:
2981 ++NumRetains; 2152 ++NumRetains;
2982 break; 2153 break;
2983 case IC_Release: 2154 case ARCInstKind::Release:
2984 ++NumReleases; 2155 ++NumReleases;
2985 break; 2156 break;
2986 } 2157 }
2987 } 2158 }
2988 } 2159 }
2995 // If nothing in the Module uses ARC, don't do anything. 2166 // If nothing in the Module uses ARC, don't do anything.
2996 Run = ModuleHasARC(M); 2167 Run = ModuleHasARC(M);
2997 if (!Run) 2168 if (!Run)
2998 return false; 2169 return false;
2999 2170
3000 // Identify the imprecise release metadata kind.
3001 ImpreciseReleaseMDKind =
3002 M.getContext().getMDKindID("clang.imprecise_release");
3003 CopyOnEscapeMDKind =
3004 M.getContext().getMDKindID("clang.arc.copy_on_escape");
3005 NoObjCARCExceptionsMDKind =
3006 M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
3007 #ifdef ARC_ANNOTATIONS
3008 ARCAnnotationBottomUpMDKind =
3009 M.getContext().getMDKindID("llvm.arc.annotation.bottomup");
3010 ARCAnnotationTopDownMDKind =
3011 M.getContext().getMDKindID("llvm.arc.annotation.topdown");
3012 ARCAnnotationProvenanceSourceMDKind =
3013 M.getContext().getMDKindID("llvm.arc.annotation.provenancesource");
3014 #endif // ARC_ANNOTATIONS
3015
3016 // Intuitively, objc_retain and others are nocapture, however in practice 2171 // Intuitively, objc_retain and others are nocapture, however in practice
3017 // they are not, because they return their argument value. And objc_release 2172 // they are not, because they return their argument value. And objc_release
3018 // calls finalizers which can have arbitrary side effects. 2173 // calls finalizers which can have arbitrary side effects.
2174 MDKindCache.init(&M);
3019 2175
3020 // Initialize our runtime entry point cache. 2176 // Initialize our runtime entry point cache.
3021 EP.Initialize(&M); 2177 EP.init(&M);
3022 2178
3023 return false; 2179 return false;
3024 } 2180 }
3025 2181
3026 bool ObjCARCOpt::runOnFunction(Function &F) { 2182 bool ObjCARCOpt::runOnFunction(Function &F) {
3034 Changed = false; 2190 Changed = false;
3035 2191
3036 DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>" 2192 DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>"
3037 "\n"); 2193 "\n");
3038 2194
3039 PA.setAA(&getAnalysis<AliasAnalysis>()); 2195 PA.setAA(&getAnalysis<AAResultsWrapperPass>().getAAResults());
3040 2196
3041 #ifndef NDEBUG 2197 #ifndef NDEBUG
3042 if (AreStatisticsEnabled()) { 2198 if (AreStatisticsEnabled()) {
3043 GatherStatistics(F, false); 2199 GatherStatistics(F, false);
3044 } 2200 }
3050 2206
3051 // Preliminary optimizations. This also computes UsedInThisFunction. 2207 // Preliminary optimizations. This also computes UsedInThisFunction.
3052 OptimizeIndividualCalls(F); 2208 OptimizeIndividualCalls(F);
3053 2209
3054 // Optimizations for weak pointers. 2210 // Optimizations for weak pointers.
3055 if (UsedInThisFunction & ((1 << IC_LoadWeak) | 2211 if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::LoadWeak)) |
3056 (1 << IC_LoadWeakRetained) | 2212 (1 << unsigned(ARCInstKind::LoadWeakRetained)) |
3057 (1 << IC_StoreWeak) | 2213 (1 << unsigned(ARCInstKind::StoreWeak)) |
3058 (1 << IC_InitWeak) | 2214 (1 << unsigned(ARCInstKind::InitWeak)) |
3059 (1 << IC_CopyWeak) | 2215 (1 << unsigned(ARCInstKind::CopyWeak)) |
3060 (1 << IC_MoveWeak) | 2216 (1 << unsigned(ARCInstKind::MoveWeak)) |
3061 (1 << IC_DestroyWeak))) 2217 (1 << unsigned(ARCInstKind::DestroyWeak))))
3062 OptimizeWeakCalls(F); 2218 OptimizeWeakCalls(F);
3063 2219
3064 // Optimizations for retain+release pairs. 2220 // Optimizations for retain+release pairs.
3065 if (UsedInThisFunction & ((1 << IC_Retain) | 2221 if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Retain)) |
3066 (1 << IC_RetainRV) | 2222 (1 << unsigned(ARCInstKind::RetainRV)) |
3067 (1 << IC_RetainBlock))) 2223 (1 << unsigned(ARCInstKind::RetainBlock))))
3068 if (UsedInThisFunction & (1 << IC_Release)) 2224 if (UsedInThisFunction & (1 << unsigned(ARCInstKind::Release)))
3069 // Run OptimizeSequences until it either stops making changes or 2225 // Run OptimizeSequences until it either stops making changes or
3070 // no retain+release pair nesting is detected. 2226 // no retain+release pair nesting is detected.
3071 while (OptimizeSequences(F)) {} 2227 while (OptimizeSequences(F)) {}
3072 2228
3073 // Optimizations if objc_autorelease is used. 2229 // Optimizations if objc_autorelease is used.
3074 if (UsedInThisFunction & ((1 << IC_Autorelease) | 2230 if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Autorelease)) |
3075 (1 << IC_AutoreleaseRV))) 2231 (1 << unsigned(ARCInstKind::AutoreleaseRV))))
3076 OptimizeReturns(F); 2232 OptimizeReturns(F);
3077 2233
3078 // Gather statistics after optimization. 2234 // Gather statistics after optimization.
3079 #ifndef NDEBUG 2235 #ifndef NDEBUG
3080 if (AreStatisticsEnabled()) { 2236 if (AreStatisticsEnabled()) {