blob: 2a8a50e451f98489280684153cdadda7c4d69498 [file] [log] [blame]
John McCall9fbd3182011-06-15 23:37:01 +00001//===- ObjCARC.cpp - ObjC ARC Optimization --------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines ObjC ARC optimizations. ARC stands for
11// Automatic Reference Counting and is a system for managing reference counts
12// for objects in Objective C.
13//
14// The optimizations performed include elimination of redundant, partially
15// redundant, and inconsequential reference count operations, elimination of
16// redundant weak pointer operations, pattern-matching and replacement of
17// low-level operations into higher-level operations, and numerous minor
18// simplifications.
19//
20// This file also defines a simple ARC-aware AliasAnalysis.
21//
22// WARNING: This file knows about certain library functions. It recognizes them
Chris Lattner55dc5c72012-05-27 19:37:05 +000023// by name, and hardwires knowledge of their semantics.
John McCall9fbd3182011-06-15 23:37:01 +000024//
25// WARNING: This file knows about how certain Objective-C library functions are
26// used. Naive LLVM IR transformations which would otherwise be
27// behavior-preserving may break these assumptions.
28//
29//===----------------------------------------------------------------------===//
30
31#define DEBUG_TYPE "objc-arc"
John McCall9fbd3182011-06-15 23:37:01 +000032#include "llvm/ADT/DenseMap.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000033#include "llvm/Support/CommandLine.h"
Chandler Carruth58a2cbe2013-01-02 10:22:59 +000034#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000035#include "llvm/Support/raw_ostream.h"
John McCall9fbd3182011-06-15 23:37:01 +000036using namespace llvm;
37
38// A handy option to enable/disable all optimizations in this file.
39static cl::opt<bool> EnableARCOpts("enable-objc-arc-opts", cl::init(true));
40
41//===----------------------------------------------------------------------===//
42// Misc. Utilities
43//===----------------------------------------------------------------------===//
44
45namespace {
46 /// MapVector - An associative container with fast insertion-order
47 /// (deterministic) iteration over its elements. Plus the special
48 /// blot operation.
49 template<class KeyT, class ValueT>
50 class MapVector {
51 /// Map - Map keys to indices in Vector.
52 typedef DenseMap<KeyT, size_t> MapTy;
53 MapTy Map;
54
55 /// Vector - Keys and values.
56 typedef std::vector<std::pair<KeyT, ValueT> > VectorTy;
57 VectorTy Vector;
58
59 public:
60 typedef typename VectorTy::iterator iterator;
61 typedef typename VectorTy::const_iterator const_iterator;
62 iterator begin() { return Vector.begin(); }
63 iterator end() { return Vector.end(); }
64 const_iterator begin() const { return Vector.begin(); }
65 const_iterator end() const { return Vector.end(); }
66
67#ifdef XDEBUG
68 ~MapVector() {
69 assert(Vector.size() >= Map.size()); // May differ due to blotting.
70 for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
71 I != E; ++I) {
72 assert(I->second < Vector.size());
73 assert(Vector[I->second].first == I->first);
74 }
75 for (typename VectorTy::const_iterator I = Vector.begin(),
76 E = Vector.end(); I != E; ++I)
77 assert(!I->first ||
78 (Map.count(I->first) &&
79 Map[I->first] == size_t(I - Vector.begin())));
80 }
81#endif
82
Dan Gohman22cc4cc2012-03-02 01:13:53 +000083 ValueT &operator[](const KeyT &Arg) {
John McCall9fbd3182011-06-15 23:37:01 +000084 std::pair<typename MapTy::iterator, bool> Pair =
85 Map.insert(std::make_pair(Arg, size_t(0)));
86 if (Pair.second) {
Dan Gohman22cc4cc2012-03-02 01:13:53 +000087 size_t Num = Vector.size();
88 Pair.first->second = Num;
John McCall9fbd3182011-06-15 23:37:01 +000089 Vector.push_back(std::make_pair(Arg, ValueT()));
Dan Gohman22cc4cc2012-03-02 01:13:53 +000090 return Vector[Num].second;
John McCall9fbd3182011-06-15 23:37:01 +000091 }
92 return Vector[Pair.first->second].second;
93 }
94
95 std::pair<iterator, bool>
96 insert(const std::pair<KeyT, ValueT> &InsertPair) {
97 std::pair<typename MapTy::iterator, bool> Pair =
98 Map.insert(std::make_pair(InsertPair.first, size_t(0)));
99 if (Pair.second) {
Dan Gohman22cc4cc2012-03-02 01:13:53 +0000100 size_t Num = Vector.size();
101 Pair.first->second = Num;
John McCall9fbd3182011-06-15 23:37:01 +0000102 Vector.push_back(InsertPair);
Dan Gohman22cc4cc2012-03-02 01:13:53 +0000103 return std::make_pair(Vector.begin() + Num, true);
John McCall9fbd3182011-06-15 23:37:01 +0000104 }
105 return std::make_pair(Vector.begin() + Pair.first->second, false);
106 }
107
Dan Gohman22cc4cc2012-03-02 01:13:53 +0000108 const_iterator find(const KeyT &Key) const {
John McCall9fbd3182011-06-15 23:37:01 +0000109 typename MapTy::const_iterator It = Map.find(Key);
110 if (It == Map.end()) return Vector.end();
111 return Vector.begin() + It->second;
112 }
113
114 /// blot - This is similar to erase, but instead of removing the element
115 /// from the vector, it just zeros out the key in the vector. This leaves
116 /// iterators intact, but clients must be prepared for zeroed-out keys when
117 /// iterating.
Dan Gohman22cc4cc2012-03-02 01:13:53 +0000118 void blot(const KeyT &Key) {
John McCall9fbd3182011-06-15 23:37:01 +0000119 typename MapTy::iterator It = Map.find(Key);
120 if (It == Map.end()) return;
121 Vector[It->second].first = KeyT();
122 Map.erase(It);
123 }
124
125 void clear() {
126 Map.clear();
127 Vector.clear();
128 }
129 };
130}
131
132//===----------------------------------------------------------------------===//
133// ARC Utilities.
134//===----------------------------------------------------------------------===//
135
Chandler Carruthd04a8d42012-12-03 16:50:05 +0000136#include "llvm/ADT/StringSwitch.h"
137#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +0000138#include "llvm/IR/Intrinsics.h"
139#include "llvm/IR/Module.h"
Dan Gohman0daef3d2012-05-08 23:39:44 +0000140#include "llvm/Support/CallSite.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +0000141#include "llvm/Transforms/Utils/Local.h"
Dan Gohman0daef3d2012-05-08 23:39:44 +0000142
John McCall9fbd3182011-06-15 23:37:01 +0000143namespace {
144 /// InstructionClass - A simple classification for instructions.
145 enum InstructionClass {
146 IC_Retain, ///< objc_retain
147 IC_RetainRV, ///< objc_retainAutoreleasedReturnValue
148 IC_RetainBlock, ///< objc_retainBlock
149 IC_Release, ///< objc_release
150 IC_Autorelease, ///< objc_autorelease
151 IC_AutoreleaseRV, ///< objc_autoreleaseReturnValue
152 IC_AutoreleasepoolPush, ///< objc_autoreleasePoolPush
153 IC_AutoreleasepoolPop, ///< objc_autoreleasePoolPop
154 IC_NoopCast, ///< objc_retainedObject, etc.
155 IC_FusedRetainAutorelease, ///< objc_retainAutorelease
156 IC_FusedRetainAutoreleaseRV, ///< objc_retainAutoreleaseReturnValue
157 IC_LoadWeakRetained, ///< objc_loadWeakRetained (primitive)
158 IC_StoreWeak, ///< objc_storeWeak (primitive)
159 IC_InitWeak, ///< objc_initWeak (derived)
160 IC_LoadWeak, ///< objc_loadWeak (derived)
161 IC_MoveWeak, ///< objc_moveWeak (derived)
162 IC_CopyWeak, ///< objc_copyWeak (derived)
163 IC_DestroyWeak, ///< objc_destroyWeak (derived)
Dan Gohman44234772012-04-13 18:28:58 +0000164 IC_StoreStrong, ///< objc_storeStrong (derived)
John McCall9fbd3182011-06-15 23:37:01 +0000165 IC_CallOrUser, ///< could call objc_release and/or "use" pointers
166 IC_Call, ///< could call objc_release
167 IC_User, ///< could "use" a pointer
168 IC_None ///< anything else
169 };
170}
171
172/// IsPotentialUse - Test whether the given value is possible a
173/// reference-counted pointer.
174static bool IsPotentialUse(const Value *Op) {
175 // Pointers to static or stack storage are not reference-counted pointers.
176 if (isa<Constant>(Op) || isa<AllocaInst>(Op))
177 return false;
178 // Special arguments are not reference-counted.
179 if (const Argument *Arg = dyn_cast<Argument>(Op))
180 if (Arg->hasByValAttr() ||
181 Arg->hasNestAttr() ||
182 Arg->hasStructRetAttr())
183 return false;
Dan Gohmanf9096e42011-12-14 19:10:53 +0000184 // Only consider values with pointer types.
185 // It seemes intuitive to exclude function pointer types as well, since
186 // functions are never reference-counted, however clang occasionally
187 // bitcasts reference-counted pointers to function-pointer type
188 // temporarily.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000189 PointerType *Ty = dyn_cast<PointerType>(Op->getType());
Dan Gohmanf9096e42011-12-14 19:10:53 +0000190 if (!Ty)
John McCall9fbd3182011-06-15 23:37:01 +0000191 return false;
192 // Conservatively assume anything else is a potential use.
193 return true;
194}
195
196/// GetCallSiteClass - Helper for GetInstructionClass. Determines what kind
197/// of construct CS is.
198static InstructionClass GetCallSiteClass(ImmutableCallSite CS) {
199 for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
200 I != E; ++I)
201 if (IsPotentialUse(*I))
202 return CS.onlyReadsMemory() ? IC_User : IC_CallOrUser;
203
204 return CS.onlyReadsMemory() ? IC_None : IC_Call;
205}
206
207/// GetFunctionClass - Determine if F is one of the special known Functions.
208/// If it isn't, return IC_CallOrUser.
209static InstructionClass GetFunctionClass(const Function *F) {
210 Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
211
212 // No arguments.
213 if (AI == AE)
214 return StringSwitch<InstructionClass>(F->getName())
215 .Case("objc_autoreleasePoolPush", IC_AutoreleasepoolPush)
216 .Default(IC_CallOrUser);
217
218 // One argument.
219 const Argument *A0 = AI++;
220 if (AI == AE)
221 // Argument is a pointer.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000222 if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) {
223 Type *ETy = PTy->getElementType();
John McCall9fbd3182011-06-15 23:37:01 +0000224 // Argument is i8*.
225 if (ETy->isIntegerTy(8))
226 return StringSwitch<InstructionClass>(F->getName())
227 .Case("objc_retain", IC_Retain)
228 .Case("objc_retainAutoreleasedReturnValue", IC_RetainRV)
229 .Case("objc_retainBlock", IC_RetainBlock)
230 .Case("objc_release", IC_Release)
231 .Case("objc_autorelease", IC_Autorelease)
232 .Case("objc_autoreleaseReturnValue", IC_AutoreleaseRV)
233 .Case("objc_autoreleasePoolPop", IC_AutoreleasepoolPop)
234 .Case("objc_retainedObject", IC_NoopCast)
235 .Case("objc_unretainedObject", IC_NoopCast)
236 .Case("objc_unretainedPointer", IC_NoopCast)
237 .Case("objc_retain_autorelease", IC_FusedRetainAutorelease)
238 .Case("objc_retainAutorelease", IC_FusedRetainAutorelease)
239 .Case("objc_retainAutoreleaseReturnValue",IC_FusedRetainAutoreleaseRV)
240 .Default(IC_CallOrUser);
241
242 // Argument is i8**
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000243 if (PointerType *Pte = dyn_cast<PointerType>(ETy))
John McCall9fbd3182011-06-15 23:37:01 +0000244 if (Pte->getElementType()->isIntegerTy(8))
245 return StringSwitch<InstructionClass>(F->getName())
246 .Case("objc_loadWeakRetained", IC_LoadWeakRetained)
247 .Case("objc_loadWeak", IC_LoadWeak)
248 .Case("objc_destroyWeak", IC_DestroyWeak)
249 .Default(IC_CallOrUser);
250 }
251
252 // Two arguments, first is i8**.
253 const Argument *A1 = AI++;
254 if (AI == AE)
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000255 if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
256 if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
John McCall9fbd3182011-06-15 23:37:01 +0000257 if (Pte->getElementType()->isIntegerTy(8))
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000258 if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
259 Type *ETy1 = PTy1->getElementType();
John McCall9fbd3182011-06-15 23:37:01 +0000260 // Second argument is i8*
261 if (ETy1->isIntegerTy(8))
262 return StringSwitch<InstructionClass>(F->getName())
263 .Case("objc_storeWeak", IC_StoreWeak)
264 .Case("objc_initWeak", IC_InitWeak)
Dan Gohman44234772012-04-13 18:28:58 +0000265 .Case("objc_storeStrong", IC_StoreStrong)
John McCall9fbd3182011-06-15 23:37:01 +0000266 .Default(IC_CallOrUser);
267 // Second argument is i8**.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000268 if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
John McCall9fbd3182011-06-15 23:37:01 +0000269 if (Pte1->getElementType()->isIntegerTy(8))
270 return StringSwitch<InstructionClass>(F->getName())
271 .Case("objc_moveWeak", IC_MoveWeak)
272 .Case("objc_copyWeak", IC_CopyWeak)
273 .Default(IC_CallOrUser);
274 }
275
276 // Anything else.
277 return IC_CallOrUser;
278}
279
280/// GetInstructionClass - Determine what kind of construct V is.
281static InstructionClass GetInstructionClass(const Value *V) {
282 if (const Instruction *I = dyn_cast<Instruction>(V)) {
283 // Any instruction other than bitcast and gep with a pointer operand have a
284 // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
285 // to a subsequent use, rather than using it themselves, in this sense.
286 // As a short cut, several other opcodes are known to have no pointer
287 // operands of interest. And ret is never followed by a release, so it's
288 // not interesting to examine.
289 switch (I->getOpcode()) {
290 case Instruction::Call: {
291 const CallInst *CI = cast<CallInst>(I);
292 // Check for calls to special functions.
293 if (const Function *F = CI->getCalledFunction()) {
294 InstructionClass Class = GetFunctionClass(F);
295 if (Class != IC_CallOrUser)
296 return Class;
297
298 // None of the intrinsic functions do objc_release. For intrinsics, the
299 // only question is whether or not they may be users.
300 switch (F->getIntrinsicID()) {
John McCall9fbd3182011-06-15 23:37:01 +0000301 case Intrinsic::returnaddress: case Intrinsic::frameaddress:
302 case Intrinsic::stacksave: case Intrinsic::stackrestore:
303 case Intrinsic::vastart: case Intrinsic::vacopy: case Intrinsic::vaend:
Dan Gohman0daef3d2012-05-08 23:39:44 +0000304 case Intrinsic::objectsize: case Intrinsic::prefetch:
305 case Intrinsic::stackprotector:
306 case Intrinsic::eh_return_i32: case Intrinsic::eh_return_i64:
307 case Intrinsic::eh_typeid_for: case Intrinsic::eh_dwarf_cfa:
308 case Intrinsic::eh_sjlj_lsda: case Intrinsic::eh_sjlj_functioncontext:
309 case Intrinsic::init_trampoline: case Intrinsic::adjust_trampoline:
310 case Intrinsic::lifetime_start: case Intrinsic::lifetime_end:
311 case Intrinsic::invariant_start: case Intrinsic::invariant_end:
John McCall9fbd3182011-06-15 23:37:01 +0000312 // Don't let dbg info affect our results.
313 case Intrinsic::dbg_declare: case Intrinsic::dbg_value:
314 // Short cut: Some intrinsics obviously don't use ObjC pointers.
315 return IC_None;
316 default:
Dan Gohman0daef3d2012-05-08 23:39:44 +0000317 break;
John McCall9fbd3182011-06-15 23:37:01 +0000318 }
319 }
320 return GetCallSiteClass(CI);
321 }
322 case Instruction::Invoke:
323 return GetCallSiteClass(cast<InvokeInst>(I));
324 case Instruction::BitCast:
325 case Instruction::GetElementPtr:
326 case Instruction::Select: case Instruction::PHI:
327 case Instruction::Ret: case Instruction::Br:
328 case Instruction::Switch: case Instruction::IndirectBr:
329 case Instruction::Alloca: case Instruction::VAArg:
330 case Instruction::Add: case Instruction::FAdd:
331 case Instruction::Sub: case Instruction::FSub:
332 case Instruction::Mul: case Instruction::FMul:
333 case Instruction::SDiv: case Instruction::UDiv: case Instruction::FDiv:
334 case Instruction::SRem: case Instruction::URem: case Instruction::FRem:
335 case Instruction::Shl: case Instruction::LShr: case Instruction::AShr:
336 case Instruction::And: case Instruction::Or: case Instruction::Xor:
337 case Instruction::SExt: case Instruction::ZExt: case Instruction::Trunc:
338 case Instruction::IntToPtr: case Instruction::FCmp:
339 case Instruction::FPTrunc: case Instruction::FPExt:
340 case Instruction::FPToUI: case Instruction::FPToSI:
341 case Instruction::UIToFP: case Instruction::SIToFP:
342 case Instruction::InsertElement: case Instruction::ExtractElement:
343 case Instruction::ShuffleVector:
344 case Instruction::ExtractValue:
345 break;
346 case Instruction::ICmp:
347 // Comparing a pointer with null, or any other constant, isn't an
348 // interesting use, because we don't care what the pointer points to, or
349 // about the values of any other dynamic reference-counted pointers.
350 if (IsPotentialUse(I->getOperand(1)))
351 return IC_User;
352 break;
353 default:
354 // For anything else, check all the operands.
Dan Gohmand4464602011-08-22 17:29:37 +0000355 // Note that this includes both operands of a Store: while the first
356 // operand isn't actually being dereferenced, it is being stored to
357 // memory where we can no longer track who might read it and dereference
358 // it, so we have to consider it potentially used.
John McCall9fbd3182011-06-15 23:37:01 +0000359 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
360 OI != OE; ++OI)
361 if (IsPotentialUse(*OI))
362 return IC_User;
363 }
364 }
365
366 // Otherwise, it's totally inert for ARC purposes.
367 return IC_None;
368}
369
370/// GetBasicInstructionClass - Determine what kind of construct V is. This is
371/// similar to GetInstructionClass except that it only detects objc runtine
372/// calls. This allows it to be faster.
373static InstructionClass GetBasicInstructionClass(const Value *V) {
374 if (const CallInst *CI = dyn_cast<CallInst>(V)) {
375 if (const Function *F = CI->getCalledFunction())
376 return GetFunctionClass(F);
377 // Otherwise, be conservative.
378 return IC_CallOrUser;
379 }
380
381 // Otherwise, be conservative.
Dan Gohman2f6263c2012-01-17 20:52:24 +0000382 return isa<InvokeInst>(V) ? IC_CallOrUser : IC_User;
John McCall9fbd3182011-06-15 23:37:01 +0000383}
384
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000385/// IsRetain - Test if the given class is objc_retain or
John McCall9fbd3182011-06-15 23:37:01 +0000386/// equivalent.
387static bool IsRetain(InstructionClass Class) {
388 return Class == IC_Retain ||
389 Class == IC_RetainRV;
390}
391
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000392/// IsAutorelease - Test if the given class is objc_autorelease or
John McCall9fbd3182011-06-15 23:37:01 +0000393/// equivalent.
394static bool IsAutorelease(InstructionClass Class) {
395 return Class == IC_Autorelease ||
396 Class == IC_AutoreleaseRV;
397}
398
399/// IsForwarding - Test if the given class represents instructions which return
400/// their argument verbatim.
401static bool IsForwarding(InstructionClass Class) {
402 // objc_retainBlock technically doesn't always return its argument
403 // verbatim, but it doesn't matter for our purposes here.
404 return Class == IC_Retain ||
405 Class == IC_RetainRV ||
406 Class == IC_Autorelease ||
407 Class == IC_AutoreleaseRV ||
408 Class == IC_RetainBlock ||
409 Class == IC_NoopCast;
410}
411
412/// IsNoopOnNull - Test if the given class represents instructions which do
413/// nothing if passed a null pointer.
414static bool IsNoopOnNull(InstructionClass Class) {
415 return Class == IC_Retain ||
416 Class == IC_RetainRV ||
417 Class == IC_Release ||
418 Class == IC_Autorelease ||
419 Class == IC_AutoreleaseRV ||
420 Class == IC_RetainBlock;
421}
422
423/// IsAlwaysTail - Test if the given class represents instructions which are
424/// always safe to mark with the "tail" keyword.
425static bool IsAlwaysTail(InstructionClass Class) {
426 // IC_RetainBlock may be given a stack argument.
427 return Class == IC_Retain ||
428 Class == IC_RetainRV ||
429 Class == IC_Autorelease ||
430 Class == IC_AutoreleaseRV;
431}
432
433/// IsNoThrow - Test if the given class represents instructions which are always
434/// safe to mark with the nounwind attribute..
435static bool IsNoThrow(InstructionClass Class) {
Dan Gohman1d2fd752011-09-14 18:33:34 +0000436 // objc_retainBlock is not nounwind because it calls user copy constructors
437 // which could theoretically throw.
John McCall9fbd3182011-06-15 23:37:01 +0000438 return Class == IC_Retain ||
439 Class == IC_RetainRV ||
John McCall9fbd3182011-06-15 23:37:01 +0000440 Class == IC_Release ||
441 Class == IC_Autorelease ||
442 Class == IC_AutoreleaseRV ||
443 Class == IC_AutoreleasepoolPush ||
444 Class == IC_AutoreleasepoolPop;
445}
446
Dan Gohman447989c2012-04-27 18:56:31 +0000447/// EraseInstruction - Erase the given instruction. Many ObjC calls return their
John McCall9fbd3182011-06-15 23:37:01 +0000448/// argument verbatim, so if it's such a call and the return value has users,
449/// replace them with the argument value.
450static void EraseInstruction(Instruction *CI) {
451 Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
452
453 bool Unused = CI->use_empty();
454
455 if (!Unused) {
456 // Replace the return value with the argument.
457 assert(IsForwarding(GetBasicInstructionClass(CI)) &&
458 "Can't delete non-forwarding instruction with users!");
459 CI->replaceAllUsesWith(OldArg);
460 }
461
462 CI->eraseFromParent();
463
464 if (Unused)
465 RecursivelyDeleteTriviallyDeadInstructions(OldArg);
466}
467
468/// GetUnderlyingObjCPtr - This is a wrapper around getUnderlyingObject which
469/// also knows how to look through objc_retain and objc_autorelease calls, which
470/// we know to return their argument verbatim.
471static const Value *GetUnderlyingObjCPtr(const Value *V) {
472 for (;;) {
473 V = GetUnderlyingObject(V);
474 if (!IsForwarding(GetBasicInstructionClass(V)))
475 break;
476 V = cast<CallInst>(V)->getArgOperand(0);
477 }
478
479 return V;
480}
481
482/// StripPointerCastsAndObjCCalls - This is a wrapper around
483/// Value::stripPointerCasts which also knows how to look through objc_retain
484/// and objc_autorelease calls, which we know to return their argument verbatim.
485static const Value *StripPointerCastsAndObjCCalls(const Value *V) {
486 for (;;) {
487 V = V->stripPointerCasts();
488 if (!IsForwarding(GetBasicInstructionClass(V)))
489 break;
490 V = cast<CallInst>(V)->getArgOperand(0);
491 }
492 return V;
493}
494
495/// StripPointerCastsAndObjCCalls - This is a wrapper around
496/// Value::stripPointerCasts which also knows how to look through objc_retain
497/// and objc_autorelease calls, which we know to return their argument verbatim.
498static Value *StripPointerCastsAndObjCCalls(Value *V) {
499 for (;;) {
500 V = V->stripPointerCasts();
501 if (!IsForwarding(GetBasicInstructionClass(V)))
502 break;
503 V = cast<CallInst>(V)->getArgOperand(0);
504 }
505 return V;
506}
507
508/// GetObjCArg - Assuming the given instruction is one of the special calls such
509/// as objc_retain or objc_release, return the argument value, stripped of no-op
510/// casts and forwarding calls.
511static Value *GetObjCArg(Value *Inst) {
512 return StripPointerCastsAndObjCCalls(cast<CallInst>(Inst)->getArgOperand(0));
513}
514
515/// IsObjCIdentifiedObject - This is similar to AliasAnalysis'
516/// isObjCIdentifiedObject, except that it uses special knowledge of
517/// ObjC conventions...
518static bool IsObjCIdentifiedObject(const Value *V) {
519 // Assume that call results and arguments have their own "provenance".
520 // Constants (including GlobalVariables) and Allocas are never
521 // reference-counted.
522 if (isa<CallInst>(V) || isa<InvokeInst>(V) ||
523 isa<Argument>(V) || isa<Constant>(V) ||
524 isa<AllocaInst>(V))
525 return true;
526
527 if (const LoadInst *LI = dyn_cast<LoadInst>(V)) {
528 const Value *Pointer =
529 StripPointerCastsAndObjCCalls(LI->getPointerOperand());
530 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Pointer)) {
Dan Gohman1b31ea82011-08-22 17:29:11 +0000531 // A constant pointer can't be pointing to an object on the heap. It may
532 // be reference-counted, but it won't be deleted.
533 if (GV->isConstant())
534 return true;
John McCall9fbd3182011-06-15 23:37:01 +0000535 StringRef Name = GV->getName();
536 // These special variables are known to hold values which are not
537 // reference-counted pointers.
538 if (Name.startswith("\01L_OBJC_SELECTOR_REFERENCES_") ||
539 Name.startswith("\01L_OBJC_CLASSLIST_REFERENCES_") ||
540 Name.startswith("\01L_OBJC_CLASSLIST_SUP_REFS_$_") ||
541 Name.startswith("\01L_OBJC_METH_VAR_NAME_") ||
542 Name.startswith("\01l_objc_msgSend_fixup_"))
543 return true;
544 }
545 }
546
547 return false;
548}
549
550/// FindSingleUseIdentifiedObject - This is similar to
551/// StripPointerCastsAndObjCCalls but it stops as soon as it finds a value
552/// with multiple uses.
553static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
554 if (Arg->hasOneUse()) {
555 if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
556 return FindSingleUseIdentifiedObject(BC->getOperand(0));
557 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
558 if (GEP->hasAllZeroIndices())
559 return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
560 if (IsForwarding(GetBasicInstructionClass(Arg)))
561 return FindSingleUseIdentifiedObject(
562 cast<CallInst>(Arg)->getArgOperand(0));
563 if (!IsObjCIdentifiedObject(Arg))
564 return 0;
565 return Arg;
566 }
567
Dan Gohman0daef3d2012-05-08 23:39:44 +0000568 // If we found an identifiable object but it has multiple uses, but they are
569 // trivial uses, we can still consider this to be a single-use value.
John McCall9fbd3182011-06-15 23:37:01 +0000570 if (IsObjCIdentifiedObject(Arg)) {
571 for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
572 UI != UE; ++UI) {
573 const User *U = *UI;
574 if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg)
575 return 0;
576 }
577
578 return Arg;
579 }
580
581 return 0;
582}
583
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +0000584/// ModuleHasARC - Test if the given module looks interesting to run ARC
585/// optimization on.
586static bool ModuleHasARC(const Module &M) {
587 return
588 M.getNamedValue("objc_retain") ||
589 M.getNamedValue("objc_release") ||
590 M.getNamedValue("objc_autorelease") ||
591 M.getNamedValue("objc_retainAutoreleasedReturnValue") ||
592 M.getNamedValue("objc_retainBlock") ||
593 M.getNamedValue("objc_autoreleaseReturnValue") ||
594 M.getNamedValue("objc_autoreleasePoolPush") ||
595 M.getNamedValue("objc_loadWeakRetained") ||
596 M.getNamedValue("objc_loadWeak") ||
597 M.getNamedValue("objc_destroyWeak") ||
598 M.getNamedValue("objc_storeWeak") ||
599 M.getNamedValue("objc_initWeak") ||
600 M.getNamedValue("objc_moveWeak") ||
601 M.getNamedValue("objc_copyWeak") ||
602 M.getNamedValue("objc_retainedObject") ||
603 M.getNamedValue("objc_unretainedObject") ||
604 M.getNamedValue("objc_unretainedPointer");
605}
606
Dan Gohman79522dc2012-01-13 00:39:07 +0000607/// DoesObjCBlockEscape - Test whether the given pointer, which is an
608/// Objective C block pointer, does not "escape". This differs from regular
609/// escape analysis in that a use as an argument to a call is not considered
610/// an escape.
611static bool DoesObjCBlockEscape(const Value *BlockPtr) {
612 // Walk the def-use chains.
613 SmallVector<const Value *, 4> Worklist;
614 Worklist.push_back(BlockPtr);
615 do {
616 const Value *V = Worklist.pop_back_val();
617 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
618 UI != UE; ++UI) {
619 const User *UUser = *UI;
620 // Special - Use by a call (callee or argument) is not considered
621 // to be an escape.
Dan Gohman44234772012-04-13 18:28:58 +0000622 switch (GetBasicInstructionClass(UUser)) {
623 case IC_StoreWeak:
624 case IC_InitWeak:
625 case IC_StoreStrong:
626 case IC_Autorelease:
627 case IC_AutoreleaseRV:
628 // These special functions make copies of their pointer arguments.
629 return true;
630 case IC_User:
631 case IC_None:
632 // Use by an instruction which copies the value is an escape if the
633 // result is an escape.
634 if (isa<BitCastInst>(UUser) || isa<GetElementPtrInst>(UUser) ||
635 isa<PHINode>(UUser) || isa<SelectInst>(UUser)) {
636 Worklist.push_back(UUser);
637 continue;
638 }
639 // Use by a load is not an escape.
640 if (isa<LoadInst>(UUser))
641 continue;
642 // Use by a store is not an escape if the use is the address.
643 if (const StoreInst *SI = dyn_cast<StoreInst>(UUser))
644 if (V != SI->getValueOperand())
645 continue;
646 break;
647 default:
648 // Regular calls and other stuff are not considered escapes.
Dan Gohman79522dc2012-01-13 00:39:07 +0000649 continue;
650 }
Dan Gohmana3b08d62012-02-13 22:57:02 +0000651 // Otherwise, conservatively assume an escape.
Dan Gohman79522dc2012-01-13 00:39:07 +0000652 return true;
653 }
654 } while (!Worklist.empty());
655
656 // No escapes found.
657 return false;
658}
659
John McCall9fbd3182011-06-15 23:37:01 +0000660//===----------------------------------------------------------------------===//
661// ARC AliasAnalysis.
662//===----------------------------------------------------------------------===//
663
John McCall9fbd3182011-06-15 23:37:01 +0000664#include "llvm/Analysis/AliasAnalysis.h"
665#include "llvm/Analysis/Passes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +0000666#include "llvm/Pass.h"
John McCall9fbd3182011-06-15 23:37:01 +0000667
668namespace {
669 /// ObjCARCAliasAnalysis - This is a simple alias analysis
670 /// implementation that uses knowledge of ARC constructs to answer queries.
671 ///
672 /// TODO: This class could be generalized to know about other ObjC-specific
673 /// tricks. Such as knowing that ivars in the non-fragile ABI are non-aliasing
674 /// even though their offsets are dynamic.
675 class ObjCARCAliasAnalysis : public ImmutablePass,
676 public AliasAnalysis {
677 public:
678 static char ID; // Class identification, replacement for typeinfo
679 ObjCARCAliasAnalysis() : ImmutablePass(ID) {
680 initializeObjCARCAliasAnalysisPass(*PassRegistry::getPassRegistry());
681 }
682
683 private:
684 virtual void initializePass() {
685 InitializeAliasAnalysis(this);
686 }
687
688 /// getAdjustedAnalysisPointer - This method is used when a pass implements
689 /// an analysis interface through multiple inheritance. If needed, it
690 /// should override this to adjust the this pointer as needed for the
691 /// specified pass info.
692 virtual void *getAdjustedAnalysisPointer(const void *PI) {
693 if (PI == &AliasAnalysis::ID)
Dan Gohman447989c2012-04-27 18:56:31 +0000694 return static_cast<AliasAnalysis *>(this);
John McCall9fbd3182011-06-15 23:37:01 +0000695 return this;
696 }
697
698 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
699 virtual AliasResult alias(const Location &LocA, const Location &LocB);
700 virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal);
701 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
702 virtual ModRefBehavior getModRefBehavior(const Function *F);
703 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
704 const Location &Loc);
705 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
706 ImmutableCallSite CS2);
707 };
708} // End of anonymous namespace
709
710// Register this pass...
711char ObjCARCAliasAnalysis::ID = 0;
712INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis, "objc-arc-aa",
713 "ObjC-ARC-Based Alias Analysis", false, true, false)
714
715ImmutablePass *llvm::createObjCARCAliasAnalysisPass() {
716 return new ObjCARCAliasAnalysis();
717}
718
719void
720ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
721 AU.setPreservesAll();
722 AliasAnalysis::getAnalysisUsage(AU);
723}
724
725AliasAnalysis::AliasResult
726ObjCARCAliasAnalysis::alias(const Location &LocA, const Location &LocB) {
727 if (!EnableARCOpts)
728 return AliasAnalysis::alias(LocA, LocB);
729
730 // First, strip off no-ops, including ObjC-specific no-ops, and try making a
731 // precise alias query.
732 const Value *SA = StripPointerCastsAndObjCCalls(LocA.Ptr);
733 const Value *SB = StripPointerCastsAndObjCCalls(LocB.Ptr);
734 AliasResult Result =
735 AliasAnalysis::alias(Location(SA, LocA.Size, LocA.TBAATag),
736 Location(SB, LocB.Size, LocB.TBAATag));
737 if (Result != MayAlias)
738 return Result;
739
740 // If that failed, climb to the underlying object, including climbing through
741 // ObjC-specific no-ops, and try making an imprecise alias query.
742 const Value *UA = GetUnderlyingObjCPtr(SA);
743 const Value *UB = GetUnderlyingObjCPtr(SB);
744 if (UA != SA || UB != SB) {
745 Result = AliasAnalysis::alias(Location(UA), Location(UB));
746 // We can't use MustAlias or PartialAlias results here because
747 // GetUnderlyingObjCPtr may return an offsetted pointer value.
748 if (Result == NoAlias)
749 return NoAlias;
750 }
751
752 // If that failed, fail. We don't need to chain here, since that's covered
753 // by the earlier precise query.
754 return MayAlias;
755}
756
757bool
758ObjCARCAliasAnalysis::pointsToConstantMemory(const Location &Loc,
759 bool OrLocal) {
760 if (!EnableARCOpts)
761 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
762
763 // First, strip off no-ops, including ObjC-specific no-ops, and try making
764 // a precise alias query.
765 const Value *S = StripPointerCastsAndObjCCalls(Loc.Ptr);
766 if (AliasAnalysis::pointsToConstantMemory(Location(S, Loc.Size, Loc.TBAATag),
767 OrLocal))
768 return true;
769
770 // If that failed, climb to the underlying object, including climbing through
771 // ObjC-specific no-ops, and try making an imprecise alias query.
772 const Value *U = GetUnderlyingObjCPtr(S);
773 if (U != S)
774 return AliasAnalysis::pointsToConstantMemory(Location(U), OrLocal);
775
776 // If that failed, fail. We don't need to chain here, since that's covered
777 // by the earlier precise query.
778 return false;
779}
780
781AliasAnalysis::ModRefBehavior
782ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
783 // We have nothing to do. Just chain to the next AliasAnalysis.
784 return AliasAnalysis::getModRefBehavior(CS);
785}
786
787AliasAnalysis::ModRefBehavior
788ObjCARCAliasAnalysis::getModRefBehavior(const Function *F) {
789 if (!EnableARCOpts)
790 return AliasAnalysis::getModRefBehavior(F);
791
792 switch (GetFunctionClass(F)) {
793 case IC_NoopCast:
794 return DoesNotAccessMemory;
795 default:
796 break;
797 }
798
799 return AliasAnalysis::getModRefBehavior(F);
800}
801
802AliasAnalysis::ModRefResult
803ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS, const Location &Loc) {
804 if (!EnableARCOpts)
805 return AliasAnalysis::getModRefInfo(CS, Loc);
806
807 switch (GetBasicInstructionClass(CS.getInstruction())) {
808 case IC_Retain:
809 case IC_RetainRV:
John McCall9fbd3182011-06-15 23:37:01 +0000810 case IC_Autorelease:
811 case IC_AutoreleaseRV:
812 case IC_NoopCast:
813 case IC_AutoreleasepoolPush:
814 case IC_FusedRetainAutorelease:
815 case IC_FusedRetainAutoreleaseRV:
816 // These functions don't access any memory visible to the compiler.
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000817 // Note that this doesn't include objc_retainBlock, because it updates
Dan Gohman21104822011-09-14 18:13:00 +0000818 // pointers when it copies block data.
John McCall9fbd3182011-06-15 23:37:01 +0000819 return NoModRef;
820 default:
821 break;
822 }
823
824 return AliasAnalysis::getModRefInfo(CS, Loc);
825}
826
827AliasAnalysis::ModRefResult
828ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
829 ImmutableCallSite CS2) {
830 // TODO: Theoretically we could check for dependencies between objc_* calls
831 // and OnlyAccessesArgumentPointees calls or other well-behaved calls.
832 return AliasAnalysis::getModRefInfo(CS1, CS2);
833}
834
835//===----------------------------------------------------------------------===//
836// ARC expansion.
837//===----------------------------------------------------------------------===//
838
839#include "llvm/Support/InstIterator.h"
840#include "llvm/Transforms/Scalar.h"
841
842namespace {
843 /// ObjCARCExpand - Early ARC transformations.
844 class ObjCARCExpand : public FunctionPass {
845 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +0000846 virtual bool doInitialization(Module &M);
John McCall9fbd3182011-06-15 23:37:01 +0000847 virtual bool runOnFunction(Function &F);
848
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +0000849 /// Run - A flag indicating whether this optimization pass should run.
850 bool Run;
851
John McCall9fbd3182011-06-15 23:37:01 +0000852 public:
853 static char ID;
854 ObjCARCExpand() : FunctionPass(ID) {
855 initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
856 }
857 };
858}
859
860char ObjCARCExpand::ID = 0;
861INITIALIZE_PASS(ObjCARCExpand,
862 "objc-arc-expand", "ObjC ARC expansion", false, false)
863
864Pass *llvm::createObjCARCExpandPass() {
865 return new ObjCARCExpand();
866}
867
868void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
869 AU.setPreservesCFG();
870}
871
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +0000872bool ObjCARCExpand::doInitialization(Module &M) {
873 Run = ModuleHasARC(M);
874 return false;
875}
876
John McCall9fbd3182011-06-15 23:37:01 +0000877bool ObjCARCExpand::runOnFunction(Function &F) {
878 if (!EnableARCOpts)
879 return false;
880
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +0000881 // If nothing in the Module uses ARC, don't do anything.
882 if (!Run)
883 return false;
884
John McCall9fbd3182011-06-15 23:37:01 +0000885 bool Changed = false;
886
887 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
888 Instruction *Inst = &*I;
Michael Gottesman8f22c8b2013-01-01 16:05:48 +0000889
890 DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
891
John McCall9fbd3182011-06-15 23:37:01 +0000892 switch (GetBasicInstructionClass(Inst)) {
893 case IC_Retain:
894 case IC_RetainRV:
895 case IC_Autorelease:
896 case IC_AutoreleaseRV:
897 case IC_FusedRetainAutorelease:
Michael Gottesmana6e23cc2013-01-01 16:05:54 +0000898 case IC_FusedRetainAutoreleaseRV: {
John McCall9fbd3182011-06-15 23:37:01 +0000899 // These calls return their argument verbatim, as a low-level
900 // optimization. However, this makes high-level optimizations
901 // harder. Undo any uses of this optimization that the front-end
Dan Gohmand6bf2012012-04-13 18:57:48 +0000902 // emitted here. We'll redo them in the contract pass.
John McCall9fbd3182011-06-15 23:37:01 +0000903 Changed = true;
Michael Gottesmana6e23cc2013-01-01 16:05:54 +0000904 Value *Value = cast<CallInst>(Inst)->getArgOperand(0);
905 DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n"
906 " New = " << *Value << "\n");
907 Inst->replaceAllUsesWith(Value);
John McCall9fbd3182011-06-15 23:37:01 +0000908 break;
Michael Gottesmana6e23cc2013-01-01 16:05:54 +0000909 }
John McCall9fbd3182011-06-15 23:37:01 +0000910 default:
911 break;
912 }
913 }
Michael Gottesman8f22c8b2013-01-01 16:05:48 +0000914
Michael Gottesmanec21e2a2013-01-03 08:09:27 +0000915 DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
Michael Gottesman8f22c8b2013-01-01 16:05:48 +0000916
John McCall9fbd3182011-06-15 23:37:01 +0000917 return Changed;
918}
919
920//===----------------------------------------------------------------------===//
Dan Gohman2f6263c2012-01-17 20:52:24 +0000921// ARC autorelease pool elimination.
922//===----------------------------------------------------------------------===//
923
Dan Gohman0daef3d2012-05-08 23:39:44 +0000924#include "llvm/ADT/STLExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +0000925#include "llvm/IR/Constants.h"
Dan Gohman1dae3e92012-01-18 21:19:38 +0000926
Dan Gohman2f6263c2012-01-17 20:52:24 +0000927namespace {
928 /// ObjCARCAPElim - Autorelease pool elimination.
929 class ObjCARCAPElim : public ModulePass {
930 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
931 virtual bool runOnModule(Module &M);
932
Dan Gohman447989c2012-04-27 18:56:31 +0000933 static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0);
934 static bool OptimizeBB(BasicBlock *BB);
Dan Gohman2f6263c2012-01-17 20:52:24 +0000935
936 public:
937 static char ID;
938 ObjCARCAPElim() : ModulePass(ID) {
939 initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
940 }
941 };
942}
943
944char ObjCARCAPElim::ID = 0;
945INITIALIZE_PASS(ObjCARCAPElim,
946 "objc-arc-apelim",
947 "ObjC ARC autorelease pool elimination",
948 false, false)
949
950Pass *llvm::createObjCARCAPElimPass() {
951 return new ObjCARCAPElim();
952}
953
954void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
955 AU.setPreservesCFG();
956}
957
958/// MayAutorelease - Interprocedurally determine if calls made by the
959/// given call site can possibly produce autoreleases.
Dan Gohman447989c2012-04-27 18:56:31 +0000960bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) {
961 if (const Function *Callee = CS.getCalledFunction()) {
Dan Gohman2f6263c2012-01-17 20:52:24 +0000962 if (Callee->isDeclaration() || Callee->mayBeOverridden())
963 return true;
Dan Gohman447989c2012-04-27 18:56:31 +0000964 for (Function::const_iterator I = Callee->begin(), E = Callee->end();
Dan Gohman2f6263c2012-01-17 20:52:24 +0000965 I != E; ++I) {
Dan Gohman447989c2012-04-27 18:56:31 +0000966 const BasicBlock *BB = I;
967 for (BasicBlock::const_iterator J = BB->begin(), F = BB->end();
968 J != F; ++J)
969 if (ImmutableCallSite JCS = ImmutableCallSite(J))
Dan Gohman2f77bbd2012-01-18 21:24:45 +0000970 // This recursion depth limit is arbitrary. It's just great
971 // enough to cover known interesting testcases.
972 if (Depth < 3 &&
973 !JCS.onlyReadsMemory() &&
974 MayAutorelease(JCS, Depth + 1))
Dan Gohman2f6263c2012-01-17 20:52:24 +0000975 return true;
976 }
977 return false;
978 }
979
980 return true;
981}
982
983bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) {
984 bool Changed = false;
985
986 Instruction *Push = 0;
987 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
988 Instruction *Inst = I++;
989 switch (GetBasicInstructionClass(Inst)) {
990 case IC_AutoreleasepoolPush:
991 Push = Inst;
992 break;
993 case IC_AutoreleasepoolPop:
994 // If this pop matches a push and nothing in between can autorelease,
995 // zap the pair.
996 if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) {
997 Changed = true;
Michael Gottesman5c0ae472013-01-04 21:29:57 +0000998 DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop autorelease pair:\n"
Michael Gottesmandf379f42013-01-03 08:09:17 +0000999 << " Pop: " << *Inst << "\n"
1000 << " Push: " << *Push << "\n");
Dan Gohman2f6263c2012-01-17 20:52:24 +00001001 Inst->eraseFromParent();
1002 Push->eraseFromParent();
1003 }
1004 Push = 0;
1005 break;
1006 case IC_CallOrUser:
Dan Gohman447989c2012-04-27 18:56:31 +00001007 if (MayAutorelease(ImmutableCallSite(Inst)))
Dan Gohman2f6263c2012-01-17 20:52:24 +00001008 Push = 0;
1009 break;
1010 default:
1011 break;
1012 }
1013 }
1014
1015 return Changed;
1016}
1017
1018bool ObjCARCAPElim::runOnModule(Module &M) {
1019 if (!EnableARCOpts)
1020 return false;
1021
1022 // If nothing in the Module uses ARC, don't do anything.
1023 if (!ModuleHasARC(M))
1024 return false;
1025
Dan Gohman1dae3e92012-01-18 21:19:38 +00001026 // Find the llvm.global_ctors variable, as the first step in
Dan Gohmand6bf2012012-04-13 18:57:48 +00001027 // identifying the global constructors. In theory, unnecessary autorelease
1028 // pools could occur anywhere, but in practice it's pretty rare. Global
1029 // ctors are a place where autorelease pools get inserted automatically,
1030 // so it's pretty common for them to be unnecessary, and it's pretty
1031 // profitable to eliminate them.
Dan Gohman1dae3e92012-01-18 21:19:38 +00001032 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
1033 if (!GV)
1034 return false;
1035
1036 assert(GV->hasDefinitiveInitializer() &&
1037 "llvm.global_ctors is uncooperative!");
1038
Dan Gohman2f6263c2012-01-17 20:52:24 +00001039 bool Changed = false;
1040
Dan Gohman1dae3e92012-01-18 21:19:38 +00001041 // Dig the constructor functions out of GV's initializer.
1042 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
1043 for (User::op_iterator OI = Init->op_begin(), OE = Init->op_end();
1044 OI != OE; ++OI) {
1045 Value *Op = *OI;
1046 // llvm.global_ctors is an array of pairs where the second members
1047 // are constructor functions.
Dan Gohman3b5b2a22012-04-18 22:24:33 +00001048 Function *F = dyn_cast<Function>(cast<ConstantStruct>(Op)->getOperand(1));
1049 // If the user used a constructor function with the wrong signature and
1050 // it got bitcasted or whatever, look the other way.
1051 if (!F)
1052 continue;
Dan Gohman2f6263c2012-01-17 20:52:24 +00001053 // Only look at function definitions.
1054 if (F->isDeclaration())
1055 continue;
Dan Gohman2f6263c2012-01-17 20:52:24 +00001056 // Only look at functions with one basic block.
1057 if (llvm::next(F->begin()) != F->end())
1058 continue;
1059 // Ok, a single-block constructor function definition. Try to optimize it.
1060 Changed |= OptimizeBB(F->begin());
1061 }
1062
1063 return Changed;
1064}
1065
1066//===----------------------------------------------------------------------===//
John McCall9fbd3182011-06-15 23:37:01 +00001067// ARC optimization.
1068//===----------------------------------------------------------------------===//
1069
1070// TODO: On code like this:
1071//
1072// objc_retain(%x)
1073// stuff_that_cannot_release()
1074// objc_autorelease(%x)
1075// stuff_that_cannot_release()
1076// objc_retain(%x)
1077// stuff_that_cannot_release()
1078// objc_autorelease(%x)
1079//
1080// The second retain and autorelease can be deleted.
1081
1082// TODO: It should be possible to delete
1083// objc_autoreleasePoolPush and objc_autoreleasePoolPop
1084// pairs if nothing is actually autoreleased between them. Also, autorelease
1085// calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
1086// after inlining) can be turned into plain release calls.
1087
1088// TODO: Critical-edge splitting. If the optimial insertion point is
1089// a critical edge, the current algorithm has to fail, because it doesn't
1090// know how to split edges. It should be possible to make the optimizer
1091// think in terms of edges, rather than blocks, and then split critical
1092// edges on demand.
1093
1094// TODO: OptimizeSequences could generalized to be Interprocedural.
1095
1096// TODO: Recognize that a bunch of other objc runtime calls have
1097// non-escaping arguments and non-releasing arguments, and may be
1098// non-autoreleasing.
1099
1100// TODO: Sink autorelease calls as far as possible. Unfortunately we
1101// usually can't sink them past other calls, which would be the main
1102// case where it would be useful.
1103
Dan Gohmane6d5e882011-08-19 00:26:36 +00001104// TODO: The pointer returned from objc_loadWeakRetained is retained.
1105
1106// TODO: Delete release+retain pairs (rare).
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +00001107
Chandler Carruthd04a8d42012-12-03 16:50:05 +00001108#include "llvm/ADT/SmallPtrSet.h"
1109#include "llvm/ADT/Statistic.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +00001110#include "llvm/IR/LLVMContext.h"
John McCall9fbd3182011-06-15 23:37:01 +00001111#include "llvm/Support/CFG.h"
John McCall9fbd3182011-06-15 23:37:01 +00001112
1113STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
1114STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
1115STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
1116STATISTIC(NumRets, "Number of return value forwarding "
1117 "retain+autoreleaes eliminated");
1118STATISTIC(NumRRs, "Number of retain+release paths eliminated");
1119STATISTIC(NumPeeps, "Number of calls peephole-optimized");
1120
1121namespace {
1122 /// ProvenanceAnalysis - This is similar to BasicAliasAnalysis, and it
1123 /// uses many of the same techniques, except it uses special ObjC-specific
1124 /// reasoning about pointer relationships.
1125 class ProvenanceAnalysis {
1126 AliasAnalysis *AA;
1127
1128 typedef std::pair<const Value *, const Value *> ValuePairTy;
1129 typedef DenseMap<ValuePairTy, bool> CachedResultsTy;
1130 CachedResultsTy CachedResults;
1131
1132 bool relatedCheck(const Value *A, const Value *B);
1133 bool relatedSelect(const SelectInst *A, const Value *B);
1134 bool relatedPHI(const PHINode *A, const Value *B);
1135
Craig Topperc2945e42012-09-18 02:01:41 +00001136 void operator=(const ProvenanceAnalysis &) LLVM_DELETED_FUNCTION;
1137 ProvenanceAnalysis(const ProvenanceAnalysis &) LLVM_DELETED_FUNCTION;
John McCall9fbd3182011-06-15 23:37:01 +00001138
1139 public:
1140 ProvenanceAnalysis() {}
1141
1142 void setAA(AliasAnalysis *aa) { AA = aa; }
1143
1144 AliasAnalysis *getAA() const { return AA; }
1145
1146 bool related(const Value *A, const Value *B);
1147
1148 void clear() {
1149 CachedResults.clear();
1150 }
1151 };
1152}
1153
1154bool ProvenanceAnalysis::relatedSelect(const SelectInst *A, const Value *B) {
1155 // If the values are Selects with the same condition, we can do a more precise
1156 // check: just check for relations between the values on corresponding arms.
1157 if (const SelectInst *SB = dyn_cast<SelectInst>(B))
Dan Gohman447989c2012-04-27 18:56:31 +00001158 if (A->getCondition() == SB->getCondition())
1159 return related(A->getTrueValue(), SB->getTrueValue()) ||
1160 related(A->getFalseValue(), SB->getFalseValue());
John McCall9fbd3182011-06-15 23:37:01 +00001161
1162 // Check both arms of the Select node individually.
Dan Gohman447989c2012-04-27 18:56:31 +00001163 return related(A->getTrueValue(), B) ||
1164 related(A->getFalseValue(), B);
John McCall9fbd3182011-06-15 23:37:01 +00001165}
1166
1167bool ProvenanceAnalysis::relatedPHI(const PHINode *A, const Value *B) {
1168 // If the values are PHIs in the same block, we can do a more precise as well
1169 // as efficient check: just check for relations between the values on
1170 // corresponding edges.
1171 if (const PHINode *PNB = dyn_cast<PHINode>(B))
1172 if (PNB->getParent() == A->getParent()) {
1173 for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i)
1174 if (related(A->getIncomingValue(i),
1175 PNB->getIncomingValueForBlock(A->getIncomingBlock(i))))
1176 return true;
1177 return false;
1178 }
1179
1180 // Check each unique source of the PHI node against B.
1181 SmallPtrSet<const Value *, 4> UniqueSrc;
1182 for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i) {
1183 const Value *PV1 = A->getIncomingValue(i);
1184 if (UniqueSrc.insert(PV1) && related(PV1, B))
1185 return true;
1186 }
1187
1188 // All of the arms checked out.
1189 return false;
1190}
1191
1192/// isStoredObjCPointer - Test if the value of P, or any value covered by its
1193/// provenance, is ever stored within the function (not counting callees).
1194static bool isStoredObjCPointer(const Value *P) {
1195 SmallPtrSet<const Value *, 8> Visited;
1196 SmallVector<const Value *, 8> Worklist;
1197 Worklist.push_back(P);
1198 Visited.insert(P);
1199 do {
1200 P = Worklist.pop_back_val();
1201 for (Value::const_use_iterator UI = P->use_begin(), UE = P->use_end();
1202 UI != UE; ++UI) {
1203 const User *Ur = *UI;
1204 if (isa<StoreInst>(Ur)) {
1205 if (UI.getOperandNo() == 0)
1206 // The pointer is stored.
1207 return true;
1208 // The pointed is stored through.
1209 continue;
1210 }
1211 if (isa<CallInst>(Ur))
1212 // The pointer is passed as an argument, ignore this.
1213 continue;
1214 if (isa<PtrToIntInst>(P))
1215 // Assume the worst.
1216 return true;
1217 if (Visited.insert(Ur))
1218 Worklist.push_back(Ur);
1219 }
1220 } while (!Worklist.empty());
1221
1222 // Everything checked out.
1223 return false;
1224}
1225
1226bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B) {
1227 // Skip past provenance pass-throughs.
1228 A = GetUnderlyingObjCPtr(A);
1229 B = GetUnderlyingObjCPtr(B);
1230
1231 // Quick check.
1232 if (A == B)
1233 return true;
1234
1235 // Ask regular AliasAnalysis, for a first approximation.
1236 switch (AA->alias(A, B)) {
1237 case AliasAnalysis::NoAlias:
1238 return false;
1239 case AliasAnalysis::MustAlias:
1240 case AliasAnalysis::PartialAlias:
1241 return true;
1242 case AliasAnalysis::MayAlias:
1243 break;
1244 }
1245
1246 bool AIsIdentified = IsObjCIdentifiedObject(A);
1247 bool BIsIdentified = IsObjCIdentifiedObject(B);
1248
1249 // An ObjC-Identified object can't alias a load if it is never locally stored.
1250 if (AIsIdentified) {
Dan Gohman230768b2012-09-04 23:16:20 +00001251 // Check for an obvious escape.
1252 if (isa<LoadInst>(B))
1253 return isStoredObjCPointer(A);
John McCall9fbd3182011-06-15 23:37:01 +00001254 if (BIsIdentified) {
Dan Gohman230768b2012-09-04 23:16:20 +00001255 // Check for an obvious escape.
1256 if (isa<LoadInst>(A))
1257 return isStoredObjCPointer(B);
1258 // Both pointers are identified and escapes aren't an evident problem.
1259 return false;
John McCall9fbd3182011-06-15 23:37:01 +00001260 }
Dan Gohman230768b2012-09-04 23:16:20 +00001261 } else if (BIsIdentified) {
1262 // Check for an obvious escape.
1263 if (isa<LoadInst>(A))
John McCall9fbd3182011-06-15 23:37:01 +00001264 return isStoredObjCPointer(B);
1265 }
1266
1267 // Special handling for PHI and Select.
1268 if (const PHINode *PN = dyn_cast<PHINode>(A))
1269 return relatedPHI(PN, B);
1270 if (const PHINode *PN = dyn_cast<PHINode>(B))
1271 return relatedPHI(PN, A);
1272 if (const SelectInst *S = dyn_cast<SelectInst>(A))
1273 return relatedSelect(S, B);
1274 if (const SelectInst *S = dyn_cast<SelectInst>(B))
1275 return relatedSelect(S, A);
1276
1277 // Conservative.
1278 return true;
1279}
1280
1281bool ProvenanceAnalysis::related(const Value *A, const Value *B) {
1282 // Begin by inserting a conservative value into the map. If the insertion
1283 // fails, we have the answer already. If it succeeds, leave it there until we
1284 // compute the real answer to guard against recursive queries.
1285 if (A > B) std::swap(A, B);
1286 std::pair<CachedResultsTy::iterator, bool> Pair =
1287 CachedResults.insert(std::make_pair(ValuePairTy(A, B), true));
1288 if (!Pair.second)
1289 return Pair.first->second;
1290
1291 bool Result = relatedCheck(A, B);
1292 CachedResults[ValuePairTy(A, B)] = Result;
1293 return Result;
1294}
1295
1296namespace {
1297 // Sequence - A sequence of states that a pointer may go through in which an
1298 // objc_retain and objc_release are actually needed.
1299 enum Sequence {
1300 S_None,
1301 S_Retain, ///< objc_retain(x)
1302 S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement
1303 S_Use, ///< any use of x
1304 S_Stop, ///< like S_Release, but code motion is stopped
1305 S_Release, ///< objc_release(x)
1306 S_MovableRelease ///< objc_release(x), !clang.imprecise_release
1307 };
1308}
1309
1310static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
1311 // The easy cases.
1312 if (A == B)
1313 return A;
1314 if (A == S_None || B == S_None)
1315 return S_None;
1316
John McCall9fbd3182011-06-15 23:37:01 +00001317 if (A > B) std::swap(A, B);
1318 if (TopDown) {
1319 // Choose the side which is further along in the sequence.
Dan Gohmana7f7db22011-08-12 00:26:31 +00001320 if ((A == S_Retain || A == S_CanRelease) &&
1321 (B == S_CanRelease || B == S_Use))
John McCall9fbd3182011-06-15 23:37:01 +00001322 return B;
1323 } else {
1324 // Choose the side which is further along in the sequence.
1325 if ((A == S_Use || A == S_CanRelease) &&
Dan Gohmana7f7db22011-08-12 00:26:31 +00001326 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
John McCall9fbd3182011-06-15 23:37:01 +00001327 return A;
1328 // If both sides are releases, choose the more conservative one.
1329 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
1330 return A;
1331 if (A == S_Release && B == S_MovableRelease)
1332 return A;
1333 }
1334
1335 return S_None;
1336}
1337
1338namespace {
1339 /// RRInfo - Unidirectional information about either a
1340 /// retain-decrement-use-release sequence or release-use-decrement-retain
1341 /// reverese sequence.
1342 struct RRInfo {
Dan Gohmane6d5e882011-08-19 00:26:36 +00001343 /// KnownSafe - After an objc_retain, the reference count of the referenced
1344 /// object is known to be positive. Similarly, before an objc_release, the
1345 /// reference count of the referenced object is known to be positive. If
1346 /// there are retain-release pairs in code regions where the retain count
1347 /// is known to be positive, they can be eliminated, regardless of any side
1348 /// effects between them.
1349 ///
1350 /// Also, a retain+release pair nested within another retain+release
1351 /// pair all on the known same pointer value can be eliminated, regardless
1352 /// of any intervening side effects.
1353 ///
1354 /// KnownSafe is true when either of these conditions is satisfied.
1355 bool KnownSafe;
John McCall9fbd3182011-06-15 23:37:01 +00001356
1357 /// IsRetainBlock - True if the Calls are objc_retainBlock calls (as
1358 /// opposed to objc_retain calls).
1359 bool IsRetainBlock;
1360
1361 /// IsTailCallRelease - True of the objc_release calls are all marked
1362 /// with the "tail" keyword.
1363 bool IsTailCallRelease;
1364
1365 /// ReleaseMetadata - If the Calls are objc_release calls and they all have
1366 /// a clang.imprecise_release tag, this is the metadata tag.
1367 MDNode *ReleaseMetadata;
1368
1369 /// Calls - For a top-down sequence, the set of objc_retains or
1370 /// objc_retainBlocks. For bottom-up, the set of objc_releases.
1371 SmallPtrSet<Instruction *, 2> Calls;
1372
1373 /// ReverseInsertPts - The set of optimal insert positions for
1374 /// moving calls in the opposite sequence.
1375 SmallPtrSet<Instruction *, 2> ReverseInsertPts;
1376
1377 RRInfo() :
Dan Gohman79522dc2012-01-13 00:39:07 +00001378 KnownSafe(false), IsRetainBlock(false),
Dan Gohman50ade652012-04-25 00:50:46 +00001379 IsTailCallRelease(false),
John McCall9fbd3182011-06-15 23:37:01 +00001380 ReleaseMetadata(0) {}
1381
1382 void clear();
1383 };
1384}
1385
1386void RRInfo::clear() {
Dan Gohmane6d5e882011-08-19 00:26:36 +00001387 KnownSafe = false;
John McCall9fbd3182011-06-15 23:37:01 +00001388 IsRetainBlock = false;
1389 IsTailCallRelease = false;
1390 ReleaseMetadata = 0;
1391 Calls.clear();
1392 ReverseInsertPts.clear();
1393}
1394
1395namespace {
1396 /// PtrState - This class summarizes several per-pointer runtime properties
1397 /// which are propogated through the flow graph.
1398 class PtrState {
Dan Gohman50ade652012-04-25 00:50:46 +00001399 /// KnownPositiveRefCount - True if the reference count is known to
1400 /// be incremented.
1401 bool KnownPositiveRefCount;
1402
1403 /// Partial - True of we've seen an opportunity for partial RR elimination,
1404 /// such as pushing calls into a CFG triangle or into one side of a
1405 /// CFG diamond.
1406 bool Partial;
John McCall9fbd3182011-06-15 23:37:01 +00001407
1408 /// Seq - The current position in the sequence.
Dan Gohman0daef3d2012-05-08 23:39:44 +00001409 Sequence Seq : 8;
John McCall9fbd3182011-06-15 23:37:01 +00001410
1411 public:
1412 /// RRI - Unidirectional information about the current sequence.
1413 /// TODO: Encapsulate this better.
1414 RRInfo RRI;
1415
Dan Gohman230768b2012-09-04 23:16:20 +00001416 PtrState() : KnownPositiveRefCount(false), Partial(false),
Dan Gohman0daef3d2012-05-08 23:39:44 +00001417 Seq(S_None) {}
John McCall9fbd3182011-06-15 23:37:01 +00001418
Dan Gohman50ade652012-04-25 00:50:46 +00001419 void SetKnownPositiveRefCount() {
1420 KnownPositiveRefCount = true;
Dan Gohmana7f7db22011-08-12 00:26:31 +00001421 }
1422
Dan Gohman50ade652012-04-25 00:50:46 +00001423 void ClearRefCount() {
1424 KnownPositiveRefCount = false;
John McCall9fbd3182011-06-15 23:37:01 +00001425 }
1426
John McCall9fbd3182011-06-15 23:37:01 +00001427 bool IsKnownIncremented() const {
Dan Gohman50ade652012-04-25 00:50:46 +00001428 return KnownPositiveRefCount;
John McCall9fbd3182011-06-15 23:37:01 +00001429 }
1430
1431 void SetSeq(Sequence NewSeq) {
1432 Seq = NewSeq;
1433 }
1434
John McCall9fbd3182011-06-15 23:37:01 +00001435 Sequence GetSeq() const {
1436 return Seq;
1437 }
1438
1439 void ClearSequenceProgress() {
Dan Gohman50ade652012-04-25 00:50:46 +00001440 ResetSequenceProgress(S_None);
1441 }
1442
1443 void ResetSequenceProgress(Sequence NewSeq) {
1444 Seq = NewSeq;
1445 Partial = false;
John McCall9fbd3182011-06-15 23:37:01 +00001446 RRI.clear();
1447 }
1448
1449 void Merge(const PtrState &Other, bool TopDown);
1450 };
1451}
1452
1453void
1454PtrState::Merge(const PtrState &Other, bool TopDown) {
1455 Seq = MergeSeqs(Seq, Other.Seq, TopDown);
Dan Gohman50ade652012-04-25 00:50:46 +00001456 KnownPositiveRefCount = KnownPositiveRefCount && Other.KnownPositiveRefCount;
John McCall9fbd3182011-06-15 23:37:01 +00001457
1458 // We can't merge a plain objc_retain with an objc_retainBlock.
1459 if (RRI.IsRetainBlock != Other.RRI.IsRetainBlock)
1460 Seq = S_None;
1461
Dan Gohman90b8bcd2011-10-17 18:48:25 +00001462 // If we're not in a sequence (anymore), drop all associated state.
John McCall9fbd3182011-06-15 23:37:01 +00001463 if (Seq == S_None) {
Dan Gohman50ade652012-04-25 00:50:46 +00001464 Partial = false;
John McCall9fbd3182011-06-15 23:37:01 +00001465 RRI.clear();
Dan Gohman50ade652012-04-25 00:50:46 +00001466 } else if (Partial || Other.Partial) {
Dan Gohman90b8bcd2011-10-17 18:48:25 +00001467 // If we're doing a merge on a path that's previously seen a partial
1468 // merge, conservatively drop the sequence, to avoid doing partial
1469 // RR elimination. If the branch predicates for the two merge differ,
1470 // mixing them is unsafe.
Dan Gohman50ade652012-04-25 00:50:46 +00001471 ClearSequenceProgress();
John McCall9fbd3182011-06-15 23:37:01 +00001472 } else {
1473 // Conservatively merge the ReleaseMetadata information.
1474 if (RRI.ReleaseMetadata != Other.RRI.ReleaseMetadata)
1475 RRI.ReleaseMetadata = 0;
1476
Dan Gohmane6d5e882011-08-19 00:26:36 +00001477 RRI.KnownSafe = RRI.KnownSafe && Other.RRI.KnownSafe;
Dan Gohman0daef3d2012-05-08 23:39:44 +00001478 RRI.IsTailCallRelease = RRI.IsTailCallRelease &&
1479 Other.RRI.IsTailCallRelease;
John McCall9fbd3182011-06-15 23:37:01 +00001480 RRI.Calls.insert(Other.RRI.Calls.begin(), Other.RRI.Calls.end());
Dan Gohman90b8bcd2011-10-17 18:48:25 +00001481
1482 // Merge the insert point sets. If there are any differences,
1483 // that makes this a partial merge.
Dan Gohman0daef3d2012-05-08 23:39:44 +00001484 Partial = RRI.ReverseInsertPts.size() != Other.RRI.ReverseInsertPts.size();
Dan Gohman90b8bcd2011-10-17 18:48:25 +00001485 for (SmallPtrSet<Instruction *, 2>::const_iterator
1486 I = Other.RRI.ReverseInsertPts.begin(),
1487 E = Other.RRI.ReverseInsertPts.end(); I != E; ++I)
Dan Gohman50ade652012-04-25 00:50:46 +00001488 Partial |= RRI.ReverseInsertPts.insert(*I);
John McCall9fbd3182011-06-15 23:37:01 +00001489 }
1490}
1491
1492namespace {
1493 /// BBState - Per-BasicBlock state.
1494 class BBState {
1495 /// TopDownPathCount - The number of unique control paths from the entry
1496 /// which can reach this block.
1497 unsigned TopDownPathCount;
1498
1499 /// BottomUpPathCount - The number of unique control paths to exits
1500 /// from this block.
1501 unsigned BottomUpPathCount;
1502
1503 /// MapTy - A type for PerPtrTopDown and PerPtrBottomUp.
1504 typedef MapVector<const Value *, PtrState> MapTy;
1505
1506 /// PerPtrTopDown - The top-down traversal uses this to record information
1507 /// known about a pointer at the bottom of each block.
1508 MapTy PerPtrTopDown;
1509
1510 /// PerPtrBottomUp - The bottom-up traversal uses this to record information
1511 /// known about a pointer at the top of each block.
1512 MapTy PerPtrBottomUp;
1513
Dan Gohmaneeeb7752012-04-24 22:53:18 +00001514 /// Preds, Succs - Effective successors and predecessors of the current
1515 /// block (this ignores ignorable edges and ignored backedges).
1516 SmallVector<BasicBlock *, 2> Preds;
1517 SmallVector<BasicBlock *, 2> Succs;
1518
John McCall9fbd3182011-06-15 23:37:01 +00001519 public:
1520 BBState() : TopDownPathCount(0), BottomUpPathCount(0) {}
1521
1522 typedef MapTy::iterator ptr_iterator;
1523 typedef MapTy::const_iterator ptr_const_iterator;
1524
1525 ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
1526 ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
1527 ptr_const_iterator top_down_ptr_begin() const {
1528 return PerPtrTopDown.begin();
1529 }
1530 ptr_const_iterator top_down_ptr_end() const {
1531 return PerPtrTopDown.end();
1532 }
1533
1534 ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); }
1535 ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
1536 ptr_const_iterator bottom_up_ptr_begin() const {
1537 return PerPtrBottomUp.begin();
1538 }
1539 ptr_const_iterator bottom_up_ptr_end() const {
1540 return PerPtrBottomUp.end();
1541 }
1542
1543 /// SetAsEntry - Mark this block as being an entry block, which has one
1544 /// path from the entry by definition.
1545 void SetAsEntry() { TopDownPathCount = 1; }
1546
1547 /// SetAsExit - Mark this block as being an exit block, which has one
1548 /// path to an exit by definition.
1549 void SetAsExit() { BottomUpPathCount = 1; }
1550
1551 PtrState &getPtrTopDownState(const Value *Arg) {
1552 return PerPtrTopDown[Arg];
1553 }
1554
1555 PtrState &getPtrBottomUpState(const Value *Arg) {
1556 return PerPtrBottomUp[Arg];
1557 }
1558
1559 void clearBottomUpPointers() {
Evan Chenga81388f2011-08-04 18:40:26 +00001560 PerPtrBottomUp.clear();
John McCall9fbd3182011-06-15 23:37:01 +00001561 }
1562
1563 void clearTopDownPointers() {
1564 PerPtrTopDown.clear();
1565 }
1566
1567 void InitFromPred(const BBState &Other);
1568 void InitFromSucc(const BBState &Other);
1569 void MergePred(const BBState &Other);
1570 void MergeSucc(const BBState &Other);
1571
1572 /// GetAllPathCount - Return the number of possible unique paths from an
1573 /// entry to an exit which pass through this block. This is only valid
1574 /// after both the top-down and bottom-up traversals are complete.
1575 unsigned GetAllPathCount() const {
Dan Gohmaneeeb7752012-04-24 22:53:18 +00001576 assert(TopDownPathCount != 0);
1577 assert(BottomUpPathCount != 0);
John McCall9fbd3182011-06-15 23:37:01 +00001578 return TopDownPathCount * BottomUpPathCount;
1579 }
Dan Gohmana7f7db22011-08-12 00:26:31 +00001580
Dan Gohmaneeeb7752012-04-24 22:53:18 +00001581 // Specialized CFG utilities.
Dan Gohman447989c2012-04-27 18:56:31 +00001582 typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
Dan Gohmaneeeb7752012-04-24 22:53:18 +00001583 edge_iterator pred_begin() { return Preds.begin(); }
1584 edge_iterator pred_end() { return Preds.end(); }
1585 edge_iterator succ_begin() { return Succs.begin(); }
1586 edge_iterator succ_end() { return Succs.end(); }
1587
1588 void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
1589 void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
1590
1591 bool isExit() const { return Succs.empty(); }
John McCall9fbd3182011-06-15 23:37:01 +00001592 };
1593}
1594
1595void BBState::InitFromPred(const BBState &Other) {
1596 PerPtrTopDown = Other.PerPtrTopDown;
1597 TopDownPathCount = Other.TopDownPathCount;
1598}
1599
1600void BBState::InitFromSucc(const BBState &Other) {
1601 PerPtrBottomUp = Other.PerPtrBottomUp;
1602 BottomUpPathCount = Other.BottomUpPathCount;
1603}
1604
1605/// MergePred - The top-down traversal uses this to merge information about
1606/// predecessors to form the initial state for a new block.
1607void BBState::MergePred(const BBState &Other) {
1608 // Other.TopDownPathCount can be 0, in which case it is either dead or a
1609 // loop backedge. Loop backedges are special.
1610 TopDownPathCount += Other.TopDownPathCount;
1611
Dan Gohman0d1bc5f2012-09-12 20:45:17 +00001612 // Check for overflow. If we have overflow, fall back to conservative behavior.
1613 if (TopDownPathCount < Other.TopDownPathCount) {
1614 clearTopDownPointers();
1615 return;
1616 }
1617
John McCall9fbd3182011-06-15 23:37:01 +00001618 // For each entry in the other set, if our set has an entry with the same key,
1619 // merge the entries. Otherwise, copy the entry and merge it with an empty
1620 // entry.
1621 for (ptr_const_iterator MI = Other.top_down_ptr_begin(),
1622 ME = Other.top_down_ptr_end(); MI != ME; ++MI) {
1623 std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI);
1624 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
1625 /*TopDown=*/true);
1626 }
1627
Dan Gohmanfa7eed12011-08-11 21:06:32 +00001628 // For each entry in our set, if the other set doesn't have an entry with the
John McCall9fbd3182011-06-15 23:37:01 +00001629 // same key, force it to merge with an empty entry.
1630 for (ptr_iterator MI = top_down_ptr_begin(),
1631 ME = top_down_ptr_end(); MI != ME; ++MI)
1632 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
1633 MI->second.Merge(PtrState(), /*TopDown=*/true);
1634}
1635
1636/// MergeSucc - The bottom-up traversal uses this to merge information about
1637/// successors to form the initial state for a new block.
1638void BBState::MergeSucc(const BBState &Other) {
1639 // Other.BottomUpPathCount can be 0, in which case it is either dead or a
1640 // loop backedge. Loop backedges are special.
1641 BottomUpPathCount += Other.BottomUpPathCount;
1642
Dan Gohman0d1bc5f2012-09-12 20:45:17 +00001643 // Check for overflow. If we have overflow, fall back to conservative behavior.
1644 if (BottomUpPathCount < Other.BottomUpPathCount) {
1645 clearBottomUpPointers();
1646 return;
1647 }
1648
John McCall9fbd3182011-06-15 23:37:01 +00001649 // For each entry in the other set, if our set has an entry with the
1650 // same key, merge the entries. Otherwise, copy the entry and merge
1651 // it with an empty entry.
1652 for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(),
1653 ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) {
1654 std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI);
1655 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
1656 /*TopDown=*/false);
1657 }
1658
Dan Gohmanfa7eed12011-08-11 21:06:32 +00001659 // For each entry in our set, if the other set doesn't have an entry
John McCall9fbd3182011-06-15 23:37:01 +00001660 // with the same key, force it to merge with an empty entry.
1661 for (ptr_iterator MI = bottom_up_ptr_begin(),
1662 ME = bottom_up_ptr_end(); MI != ME; ++MI)
1663 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
1664 MI->second.Merge(PtrState(), /*TopDown=*/false);
1665}
1666
1667namespace {
1668 /// ObjCARCOpt - The main ARC optimization pass.
1669 class ObjCARCOpt : public FunctionPass {
1670 bool Changed;
1671 ProvenanceAnalysis PA;
1672
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +00001673 /// Run - A flag indicating whether this optimization pass should run.
1674 bool Run;
1675
John McCall9fbd3182011-06-15 23:37:01 +00001676 /// RetainRVCallee, etc. - Declarations for ObjC runtime
1677 /// functions, for use in creating calls to them. These are initialized
1678 /// lazily to avoid cluttering up the Module with unused declarations.
1679 Constant *RetainRVCallee, *AutoreleaseRVCallee, *ReleaseCallee,
Dan Gohman44280692011-07-22 22:29:21 +00001680 *RetainCallee, *RetainBlockCallee, *AutoreleaseCallee;
John McCall9fbd3182011-06-15 23:37:01 +00001681
1682 /// UsedInThisFunciton - Flags which determine whether each of the
1683 /// interesting runtine functions is in fact used in the current function.
1684 unsigned UsedInThisFunction;
1685
1686 /// ImpreciseReleaseMDKind - The Metadata Kind for clang.imprecise_release
1687 /// metadata.
1688 unsigned ImpreciseReleaseMDKind;
1689
Dan Gohman62e5b402011-12-12 18:20:00 +00001690 /// CopyOnEscapeMDKind - The Metadata Kind for clang.arc.copy_on_escape
Dan Gohmana974bea2011-10-17 22:53:25 +00001691 /// metadata.
1692 unsigned CopyOnEscapeMDKind;
1693
Dan Gohmandbe266b2012-02-17 18:59:53 +00001694 /// NoObjCARCExceptionsMDKind - The Metadata Kind for
1695 /// clang.arc.no_objc_arc_exceptions metadata.
1696 unsigned NoObjCARCExceptionsMDKind;
1697
John McCall9fbd3182011-06-15 23:37:01 +00001698 Constant *getRetainRVCallee(Module *M);
1699 Constant *getAutoreleaseRVCallee(Module *M);
1700 Constant *getReleaseCallee(Module *M);
1701 Constant *getRetainCallee(Module *M);
Dan Gohman44280692011-07-22 22:29:21 +00001702 Constant *getRetainBlockCallee(Module *M);
John McCall9fbd3182011-06-15 23:37:01 +00001703 Constant *getAutoreleaseCallee(Module *M);
1704
Dan Gohman79522dc2012-01-13 00:39:07 +00001705 bool IsRetainBlockOptimizable(const Instruction *Inst);
1706
John McCall9fbd3182011-06-15 23:37:01 +00001707 void OptimizeRetainCall(Function &F, Instruction *Retain);
1708 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
1709 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV);
1710 void OptimizeIndividualCalls(Function &F);
1711
1712 void CheckForCFGHazards(const BasicBlock *BB,
1713 DenseMap<const BasicBlock *, BBState> &BBStates,
1714 BBState &MyStates) const;
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00001715 bool VisitInstructionBottomUp(Instruction *Inst,
Dan Gohmanfbab4a82012-03-23 17:47:54 +00001716 BasicBlock *BB,
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00001717 MapVector<Value *, RRInfo> &Retains,
1718 BBState &MyStates);
John McCall9fbd3182011-06-15 23:37:01 +00001719 bool VisitBottomUp(BasicBlock *BB,
1720 DenseMap<const BasicBlock *, BBState> &BBStates,
1721 MapVector<Value *, RRInfo> &Retains);
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00001722 bool VisitInstructionTopDown(Instruction *Inst,
1723 DenseMap<Value *, RRInfo> &Releases,
1724 BBState &MyStates);
John McCall9fbd3182011-06-15 23:37:01 +00001725 bool VisitTopDown(BasicBlock *BB,
1726 DenseMap<const BasicBlock *, BBState> &BBStates,
1727 DenseMap<Value *, RRInfo> &Releases);
1728 bool Visit(Function &F,
1729 DenseMap<const BasicBlock *, BBState> &BBStates,
1730 MapVector<Value *, RRInfo> &Retains,
1731 DenseMap<Value *, RRInfo> &Releases);
1732
1733 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
1734 MapVector<Value *, RRInfo> &Retains,
1735 DenseMap<Value *, RRInfo> &Releases,
Dan Gohman44280692011-07-22 22:29:21 +00001736 SmallVectorImpl<Instruction *> &DeadInsts,
1737 Module *M);
John McCall9fbd3182011-06-15 23:37:01 +00001738
1739 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
1740 MapVector<Value *, RRInfo> &Retains,
Dan Gohman44280692011-07-22 22:29:21 +00001741 DenseMap<Value *, RRInfo> &Releases,
1742 Module *M);
John McCall9fbd3182011-06-15 23:37:01 +00001743
1744 void OptimizeWeakCalls(Function &F);
1745
1746 bool OptimizeSequences(Function &F);
1747
1748 void OptimizeReturns(Function &F);
1749
1750 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
1751 virtual bool doInitialization(Module &M);
1752 virtual bool runOnFunction(Function &F);
1753 virtual void releaseMemory();
1754
1755 public:
1756 static char ID;
1757 ObjCARCOpt() : FunctionPass(ID) {
1758 initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
1759 }
1760 };
1761}
1762
1763char ObjCARCOpt::ID = 0;
1764INITIALIZE_PASS_BEGIN(ObjCARCOpt,
1765 "objc-arc", "ObjC ARC optimization", false, false)
1766INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
1767INITIALIZE_PASS_END(ObjCARCOpt,
1768 "objc-arc", "ObjC ARC optimization", false, false)
1769
1770Pass *llvm::createObjCARCOptPass() {
1771 return new ObjCARCOpt();
1772}
1773
1774void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
1775 AU.addRequired<ObjCARCAliasAnalysis>();
1776 AU.addRequired<AliasAnalysis>();
1777 // ARC optimization doesn't currently split critical edges.
1778 AU.setPreservesCFG();
1779}
1780
Dan Gohman79522dc2012-01-13 00:39:07 +00001781bool ObjCARCOpt::IsRetainBlockOptimizable(const Instruction *Inst) {
1782 // Without the magic metadata tag, we have to assume this might be an
1783 // objc_retainBlock call inserted to convert a block pointer to an id,
1784 // in which case it really is needed.
1785 if (!Inst->getMetadata(CopyOnEscapeMDKind))
1786 return false;
1787
1788 // If the pointer "escapes" (not including being used in a call),
1789 // the copy may be needed.
1790 if (DoesObjCBlockEscape(Inst))
1791 return false;
1792
1793 // Otherwise, it's not needed.
1794 return true;
1795}
1796
John McCall9fbd3182011-06-15 23:37:01 +00001797Constant *ObjCARCOpt::getRetainRVCallee(Module *M) {
1798 if (!RetainRVCallee) {
1799 LLVMContext &C = M->getContext();
Jay Foad5fdd6c82011-07-12 14:06:48 +00001800 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
Dan Gohman0daef3d2012-05-08 23:39:44 +00001801 Type *Params[] = { I8X };
1802 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
Bill Wendling034b94b2012-12-19 07:18:57 +00001803 AttributeSet Attribute =
Bill Wendling99faa3b2012-12-07 23:16:57 +00001804 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00001805 Attribute::get(C, Attribute::NoUnwind));
John McCall9fbd3182011-06-15 23:37:01 +00001806 RetainRVCallee =
1807 M->getOrInsertFunction("objc_retainAutoreleasedReturnValue", FTy,
Bill Wendling034b94b2012-12-19 07:18:57 +00001808 Attribute);
John McCall9fbd3182011-06-15 23:37:01 +00001809 }
1810 return RetainRVCallee;
1811}
1812
1813Constant *ObjCARCOpt::getAutoreleaseRVCallee(Module *M) {
1814 if (!AutoreleaseRVCallee) {
1815 LLVMContext &C = M->getContext();
Jay Foad5fdd6c82011-07-12 14:06:48 +00001816 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
Dan Gohman0daef3d2012-05-08 23:39:44 +00001817 Type *Params[] = { I8X };
1818 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
Bill Wendling034b94b2012-12-19 07:18:57 +00001819 AttributeSet Attribute =
Bill Wendling99faa3b2012-12-07 23:16:57 +00001820 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00001821 Attribute::get(C, Attribute::NoUnwind));
John McCall9fbd3182011-06-15 23:37:01 +00001822 AutoreleaseRVCallee =
1823 M->getOrInsertFunction("objc_autoreleaseReturnValue", FTy,
Bill Wendling034b94b2012-12-19 07:18:57 +00001824 Attribute);
John McCall9fbd3182011-06-15 23:37:01 +00001825 }
1826 return AutoreleaseRVCallee;
1827}
1828
1829Constant *ObjCARCOpt::getReleaseCallee(Module *M) {
1830 if (!ReleaseCallee) {
1831 LLVMContext &C = M->getContext();
Dan Gohman0daef3d2012-05-08 23:39:44 +00001832 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
Bill Wendling034b94b2012-12-19 07:18:57 +00001833 AttributeSet Attribute =
Bill Wendling99faa3b2012-12-07 23:16:57 +00001834 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00001835 Attribute::get(C, Attribute::NoUnwind));
John McCall9fbd3182011-06-15 23:37:01 +00001836 ReleaseCallee =
1837 M->getOrInsertFunction(
1838 "objc_release",
1839 FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
Bill Wendling034b94b2012-12-19 07:18:57 +00001840 Attribute);
John McCall9fbd3182011-06-15 23:37:01 +00001841 }
1842 return ReleaseCallee;
1843}
1844
1845Constant *ObjCARCOpt::getRetainCallee(Module *M) {
1846 if (!RetainCallee) {
1847 LLVMContext &C = M->getContext();
Dan Gohman0daef3d2012-05-08 23:39:44 +00001848 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
Bill Wendling034b94b2012-12-19 07:18:57 +00001849 AttributeSet Attribute =
Bill Wendling99faa3b2012-12-07 23:16:57 +00001850 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00001851 Attribute::get(C, Attribute::NoUnwind));
John McCall9fbd3182011-06-15 23:37:01 +00001852 RetainCallee =
1853 M->getOrInsertFunction(
1854 "objc_retain",
1855 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
Bill Wendling034b94b2012-12-19 07:18:57 +00001856 Attribute);
John McCall9fbd3182011-06-15 23:37:01 +00001857 }
1858 return RetainCallee;
1859}
1860
Dan Gohman44280692011-07-22 22:29:21 +00001861Constant *ObjCARCOpt::getRetainBlockCallee(Module *M) {
1862 if (!RetainBlockCallee) {
1863 LLVMContext &C = M->getContext();
Dan Gohman0daef3d2012-05-08 23:39:44 +00001864 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
Dan Gohman1d2fd752011-09-14 18:33:34 +00001865 // objc_retainBlock is not nounwind because it calls user copy constructors
1866 // which could theoretically throw.
Dan Gohman44280692011-07-22 22:29:21 +00001867 RetainBlockCallee =
1868 M->getOrInsertFunction(
1869 "objc_retainBlock",
1870 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
Bill Wendling99faa3b2012-12-07 23:16:57 +00001871 AttributeSet());
Dan Gohman44280692011-07-22 22:29:21 +00001872 }
1873 return RetainBlockCallee;
1874}
1875
John McCall9fbd3182011-06-15 23:37:01 +00001876Constant *ObjCARCOpt::getAutoreleaseCallee(Module *M) {
1877 if (!AutoreleaseCallee) {
1878 LLVMContext &C = M->getContext();
Dan Gohman0daef3d2012-05-08 23:39:44 +00001879 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
Bill Wendling034b94b2012-12-19 07:18:57 +00001880 AttributeSet Attribute =
Bill Wendling99faa3b2012-12-07 23:16:57 +00001881 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00001882 Attribute::get(C, Attribute::NoUnwind));
John McCall9fbd3182011-06-15 23:37:01 +00001883 AutoreleaseCallee =
1884 M->getOrInsertFunction(
1885 "objc_autorelease",
1886 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
Bill Wendling034b94b2012-12-19 07:18:57 +00001887 Attribute);
John McCall9fbd3182011-06-15 23:37:01 +00001888 }
1889 return AutoreleaseCallee;
1890}
1891
Dan Gohman230768b2012-09-04 23:16:20 +00001892/// IsPotentialUse - Test whether the given value is possible a
1893/// reference-counted pointer, including tests which utilize AliasAnalysis.
1894static bool IsPotentialUse(const Value *Op, AliasAnalysis &AA) {
1895 // First make the rudimentary check.
1896 if (!IsPotentialUse(Op))
1897 return false;
1898
1899 // Objects in constant memory are not reference-counted.
1900 if (AA.pointsToConstantMemory(Op))
1901 return false;
1902
1903 // Pointers in constant memory are not pointing to reference-counted objects.
1904 if (const LoadInst *LI = dyn_cast<LoadInst>(Op))
1905 if (AA.pointsToConstantMemory(LI->getPointerOperand()))
1906 return false;
1907
1908 // Otherwise assume the worst.
1909 return true;
1910}
1911
John McCall9fbd3182011-06-15 23:37:01 +00001912/// CanAlterRefCount - Test whether the given instruction can result in a
1913/// reference count modification (positive or negative) for the pointer's
1914/// object.
1915static bool
1916CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
1917 ProvenanceAnalysis &PA, InstructionClass Class) {
1918 switch (Class) {
1919 case IC_Autorelease:
1920 case IC_AutoreleaseRV:
1921 case IC_User:
1922 // These operations never directly modify a reference count.
1923 return false;
1924 default: break;
1925 }
1926
1927 ImmutableCallSite CS = static_cast<const Value *>(Inst);
1928 assert(CS && "Only calls can alter reference counts!");
1929
1930 // See if AliasAnalysis can help us with the call.
1931 AliasAnalysis::ModRefBehavior MRB = PA.getAA()->getModRefBehavior(CS);
1932 if (AliasAnalysis::onlyReadsMemory(MRB))
1933 return false;
1934 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
1935 for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
1936 I != E; ++I) {
1937 const Value *Op = *I;
Dan Gohman230768b2012-09-04 23:16:20 +00001938 if (IsPotentialUse(Op, *PA.getAA()) && PA.related(Ptr, Op))
John McCall9fbd3182011-06-15 23:37:01 +00001939 return true;
1940 }
1941 return false;
1942 }
1943
1944 // Assume the worst.
1945 return true;
1946}
1947
1948/// CanUse - Test whether the given instruction can "use" the given pointer's
1949/// object in a way that requires the reference count to be positive.
1950static bool
1951CanUse(const Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA,
1952 InstructionClass Class) {
1953 // IC_Call operations (as opposed to IC_CallOrUser) never "use" objc pointers.
1954 if (Class == IC_Call)
1955 return false;
1956
1957 // Consider various instructions which may have pointer arguments which are
1958 // not "uses".
1959 if (const ICmpInst *ICI = dyn_cast<ICmpInst>(Inst)) {
1960 // Comparing a pointer with null, or any other constant, isn't really a use,
1961 // because we don't care what the pointer points to, or about the values
1962 // of any other dynamic reference-counted pointers.
Dan Gohman230768b2012-09-04 23:16:20 +00001963 if (!IsPotentialUse(ICI->getOperand(1), *PA.getAA()))
John McCall9fbd3182011-06-15 23:37:01 +00001964 return false;
1965 } else if (ImmutableCallSite CS = static_cast<const Value *>(Inst)) {
1966 // For calls, just check the arguments (and not the callee operand).
1967 for (ImmutableCallSite::arg_iterator OI = CS.arg_begin(),
1968 OE = CS.arg_end(); OI != OE; ++OI) {
1969 const Value *Op = *OI;
Dan Gohman230768b2012-09-04 23:16:20 +00001970 if (IsPotentialUse(Op, *PA.getAA()) && PA.related(Ptr, Op))
John McCall9fbd3182011-06-15 23:37:01 +00001971 return true;
1972 }
1973 return false;
1974 } else if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1975 // Special-case stores, because we don't care about the stored value, just
1976 // the store address.
1977 const Value *Op = GetUnderlyingObjCPtr(SI->getPointerOperand());
1978 // If we can't tell what the underlying object was, assume there is a
1979 // dependence.
Dan Gohman230768b2012-09-04 23:16:20 +00001980 return IsPotentialUse(Op, *PA.getAA()) && PA.related(Op, Ptr);
John McCall9fbd3182011-06-15 23:37:01 +00001981 }
1982
1983 // Check each operand for a match.
1984 for (User::const_op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
1985 OI != OE; ++OI) {
1986 const Value *Op = *OI;
Dan Gohman230768b2012-09-04 23:16:20 +00001987 if (IsPotentialUse(Op, *PA.getAA()) && PA.related(Ptr, Op))
John McCall9fbd3182011-06-15 23:37:01 +00001988 return true;
1989 }
1990 return false;
1991}
1992
1993/// CanInterruptRV - Test whether the given instruction can autorelease
1994/// any pointer or cause an autoreleasepool pop.
1995static bool
1996CanInterruptRV(InstructionClass Class) {
1997 switch (Class) {
1998 case IC_AutoreleasepoolPop:
1999 case IC_CallOrUser:
2000 case IC_Call:
2001 case IC_Autorelease:
2002 case IC_AutoreleaseRV:
2003 case IC_FusedRetainAutorelease:
2004 case IC_FusedRetainAutoreleaseRV:
2005 return true;
2006 default:
2007 return false;
2008 }
2009}
2010
2011namespace {
2012 /// DependenceKind - There are several kinds of dependence-like concepts in
2013 /// use here.
2014 enum DependenceKind {
2015 NeedsPositiveRetainCount,
Dan Gohman511568d2012-04-13 00:59:57 +00002016 AutoreleasePoolBoundary,
John McCall9fbd3182011-06-15 23:37:01 +00002017 CanChangeRetainCount,
2018 RetainAutoreleaseDep, ///< Blocks objc_retainAutorelease.
2019 RetainAutoreleaseRVDep, ///< Blocks objc_retainAutoreleaseReturnValue.
2020 RetainRVDep ///< Blocks objc_retainAutoreleasedReturnValue.
2021 };
2022}
2023
2024/// Depends - Test if there can be dependencies on Inst through Arg. This
2025/// function only tests dependencies relevant for removing pairs of calls.
2026static bool
2027Depends(DependenceKind Flavor, Instruction *Inst, const Value *Arg,
2028 ProvenanceAnalysis &PA) {
2029 // If we've reached the definition of Arg, stop.
2030 if (Inst == Arg)
2031 return true;
2032
2033 switch (Flavor) {
2034 case NeedsPositiveRetainCount: {
2035 InstructionClass Class = GetInstructionClass(Inst);
2036 switch (Class) {
2037 case IC_AutoreleasepoolPop:
2038 case IC_AutoreleasepoolPush:
2039 case IC_None:
2040 return false;
2041 default:
2042 return CanUse(Inst, Arg, PA, Class);
2043 }
2044 }
2045
Dan Gohman511568d2012-04-13 00:59:57 +00002046 case AutoreleasePoolBoundary: {
2047 InstructionClass Class = GetInstructionClass(Inst);
2048 switch (Class) {
2049 case IC_AutoreleasepoolPop:
2050 case IC_AutoreleasepoolPush:
2051 // These mark the end and begin of an autorelease pool scope.
2052 return true;
2053 default:
2054 // Nothing else does this.
2055 return false;
2056 }
2057 }
2058
John McCall9fbd3182011-06-15 23:37:01 +00002059 case CanChangeRetainCount: {
2060 InstructionClass Class = GetInstructionClass(Inst);
2061 switch (Class) {
2062 case IC_AutoreleasepoolPop:
2063 // Conservatively assume this can decrement any count.
2064 return true;
2065 case IC_AutoreleasepoolPush:
2066 case IC_None:
2067 return false;
2068 default:
2069 return CanAlterRefCount(Inst, Arg, PA, Class);
2070 }
2071 }
2072
2073 case RetainAutoreleaseDep:
2074 switch (GetBasicInstructionClass(Inst)) {
2075 case IC_AutoreleasepoolPop:
Dan Gohman511568d2012-04-13 00:59:57 +00002076 case IC_AutoreleasepoolPush:
John McCall9fbd3182011-06-15 23:37:01 +00002077 // Don't merge an objc_autorelease with an objc_retain inside a different
2078 // autoreleasepool scope.
2079 return true;
2080 case IC_Retain:
2081 case IC_RetainRV:
2082 // Check for a retain of the same pointer for merging.
2083 return GetObjCArg(Inst) == Arg;
2084 default:
2085 // Nothing else matters for objc_retainAutorelease formation.
2086 return false;
2087 }
John McCall9fbd3182011-06-15 23:37:01 +00002088
2089 case RetainAutoreleaseRVDep: {
2090 InstructionClass Class = GetBasicInstructionClass(Inst);
2091 switch (Class) {
2092 case IC_Retain:
2093 case IC_RetainRV:
2094 // Check for a retain of the same pointer for merging.
2095 return GetObjCArg(Inst) == Arg;
2096 default:
2097 // Anything that can autorelease interrupts
2098 // retainAutoreleaseReturnValue formation.
2099 return CanInterruptRV(Class);
2100 }
John McCall9fbd3182011-06-15 23:37:01 +00002101 }
2102
2103 case RetainRVDep:
2104 return CanInterruptRV(GetBasicInstructionClass(Inst));
2105 }
2106
2107 llvm_unreachable("Invalid dependence flavor");
John McCall9fbd3182011-06-15 23:37:01 +00002108}
2109
2110/// FindDependencies - Walk up the CFG from StartPos (which is in StartBB) and
2111/// find local and non-local dependencies on Arg.
2112/// TODO: Cache results?
2113static void
2114FindDependencies(DependenceKind Flavor,
2115 const Value *Arg,
2116 BasicBlock *StartBB, Instruction *StartInst,
2117 SmallPtrSet<Instruction *, 4> &DependingInstructions,
2118 SmallPtrSet<const BasicBlock *, 4> &Visited,
2119 ProvenanceAnalysis &PA) {
2120 BasicBlock::iterator StartPos = StartInst;
2121
2122 SmallVector<std::pair<BasicBlock *, BasicBlock::iterator>, 4> Worklist;
2123 Worklist.push_back(std::make_pair(StartBB, StartPos));
2124 do {
2125 std::pair<BasicBlock *, BasicBlock::iterator> Pair =
2126 Worklist.pop_back_val();
2127 BasicBlock *LocalStartBB = Pair.first;
2128 BasicBlock::iterator LocalStartPos = Pair.second;
2129 BasicBlock::iterator StartBBBegin = LocalStartBB->begin();
2130 for (;;) {
2131 if (LocalStartPos == StartBBBegin) {
2132 pred_iterator PI(LocalStartBB), PE(LocalStartBB, false);
2133 if (PI == PE)
2134 // If we've reached the function entry, produce a null dependence.
2135 DependingInstructions.insert(0);
2136 else
2137 // Add the predecessors to the worklist.
2138 do {
2139 BasicBlock *PredBB = *PI;
2140 if (Visited.insert(PredBB))
2141 Worklist.push_back(std::make_pair(PredBB, PredBB->end()));
2142 } while (++PI != PE);
2143 break;
2144 }
2145
2146 Instruction *Inst = --LocalStartPos;
2147 if (Depends(Flavor, Inst, Arg, PA)) {
2148 DependingInstructions.insert(Inst);
2149 break;
2150 }
2151 }
2152 } while (!Worklist.empty());
2153
2154 // Determine whether the original StartBB post-dominates all of the blocks we
2155 // visited. If not, insert a sentinal indicating that most optimizations are
2156 // not safe.
2157 for (SmallPtrSet<const BasicBlock *, 4>::const_iterator I = Visited.begin(),
2158 E = Visited.end(); I != E; ++I) {
2159 const BasicBlock *BB = *I;
2160 if (BB == StartBB)
2161 continue;
2162 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
2163 for (succ_const_iterator SI(TI), SE(TI, false); SI != SE; ++SI) {
2164 const BasicBlock *Succ = *SI;
2165 if (Succ != StartBB && !Visited.count(Succ)) {
2166 DependingInstructions.insert(reinterpret_cast<Instruction *>(-1));
2167 return;
2168 }
2169 }
2170 }
2171}
2172
2173static bool isNullOrUndef(const Value *V) {
2174 return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
2175}
2176
2177static bool isNoopInstruction(const Instruction *I) {
2178 return isa<BitCastInst>(I) ||
2179 (isa<GetElementPtrInst>(I) &&
2180 cast<GetElementPtrInst>(I)->hasAllZeroIndices());
2181}
2182
2183/// OptimizeRetainCall - Turn objc_retain into
2184/// objc_retainAutoreleasedReturnValue if the operand is a return value.
2185void
2186ObjCARCOpt::OptimizeRetainCall(Function &F, Instruction *Retain) {
Dan Gohman447989c2012-04-27 18:56:31 +00002187 ImmutableCallSite CS(GetObjCArg(Retain));
2188 const Instruction *Call = CS.getInstruction();
John McCall9fbd3182011-06-15 23:37:01 +00002189 if (!Call) return;
2190 if (Call->getParent() != Retain->getParent()) return;
2191
2192 // Check that the call is next to the retain.
Dan Gohman447989c2012-04-27 18:56:31 +00002193 BasicBlock::const_iterator I = Call;
John McCall9fbd3182011-06-15 23:37:01 +00002194 ++I;
2195 while (isNoopInstruction(I)) ++I;
2196 if (&*I != Retain)
2197 return;
2198
2199 // Turn it to an objc_retainAutoreleasedReturnValue..
2200 Changed = true;
2201 ++NumPeeps;
Michael Gottesman715f6a62013-01-04 21:30:38 +00002202
2203 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainCall: Transforming "
2204 "objc_retainAutoreleasedReturnValue => "
2205 "objc_retain since the operand is not a return value.\n"
2206 " Old: "
2207 << *Retain << "\n");
2208
John McCall9fbd3182011-06-15 23:37:01 +00002209 cast<CallInst>(Retain)->setCalledFunction(getRetainRVCallee(F.getParent()));
Michael Gottesman715f6a62013-01-04 21:30:38 +00002210
2211 DEBUG(dbgs() << " New: "
2212 << *Retain << "\n");
John McCall9fbd3182011-06-15 23:37:01 +00002213}
2214
2215/// OptimizeRetainRVCall - Turn objc_retainAutoreleasedReturnValue into
Dan Gohman447989c2012-04-27 18:56:31 +00002216/// objc_retain if the operand is not a return value. Or, if it can be paired
2217/// with an objc_autoreleaseReturnValue, delete the pair and return true.
John McCall9fbd3182011-06-15 23:37:01 +00002218bool
2219ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
Dan Gohman6fedb3c2012-03-23 18:09:00 +00002220 // Check for the argument being from an immediately preceding call or invoke.
Dan Gohman447989c2012-04-27 18:56:31 +00002221 const Value *Arg = GetObjCArg(RetainRV);
2222 ImmutableCallSite CS(Arg);
2223 if (const Instruction *Call = CS.getInstruction()) {
John McCall9fbd3182011-06-15 23:37:01 +00002224 if (Call->getParent() == RetainRV->getParent()) {
Dan Gohman447989c2012-04-27 18:56:31 +00002225 BasicBlock::const_iterator I = Call;
John McCall9fbd3182011-06-15 23:37:01 +00002226 ++I;
2227 while (isNoopInstruction(I)) ++I;
2228 if (&*I == RetainRV)
2229 return false;
Dan Gohman447989c2012-04-27 18:56:31 +00002230 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Dan Gohman6fedb3c2012-03-23 18:09:00 +00002231 BasicBlock *RetainRVParent = RetainRV->getParent();
2232 if (II->getNormalDest() == RetainRVParent) {
Dan Gohman447989c2012-04-27 18:56:31 +00002233 BasicBlock::const_iterator I = RetainRVParent->begin();
Dan Gohman6fedb3c2012-03-23 18:09:00 +00002234 while (isNoopInstruction(I)) ++I;
2235 if (&*I == RetainRV)
2236 return false;
2237 }
John McCall9fbd3182011-06-15 23:37:01 +00002238 }
Dan Gohman6fedb3c2012-03-23 18:09:00 +00002239 }
John McCall9fbd3182011-06-15 23:37:01 +00002240
2241 // Check for being preceded by an objc_autoreleaseReturnValue on the same
2242 // pointer. In this case, we can delete the pair.
2243 BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
2244 if (I != Begin) {
2245 do --I; while (I != Begin && isNoopInstruction(I));
2246 if (GetBasicInstructionClass(I) == IC_AutoreleaseRV &&
2247 GetObjCArg(I) == Arg) {
2248 Changed = true;
2249 ++NumPeeps;
Michael Gottesman87a0f022013-01-05 17:55:35 +00002250
2251 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainRVCall: Erasing " << *I << "\n"
2252 << " Erasing " << *RetainRV
2253 << "\n");
2254
John McCall9fbd3182011-06-15 23:37:01 +00002255 EraseInstruction(I);
2256 EraseInstruction(RetainRV);
2257 return true;
2258 }
2259 }
2260
2261 // Turn it to a plain objc_retain.
2262 Changed = true;
2263 ++NumPeeps;
Michael Gottesman36e4bc42013-01-05 17:55:42 +00002264
2265 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainRVCall: Transforming "
2266 "objc_retainAutoreleasedReturnValue => "
2267 "objc_retain since the operand is not a return value.\n"
2268 " Old: "
2269 << *RetainRV << "\n");
2270
John McCall9fbd3182011-06-15 23:37:01 +00002271 cast<CallInst>(RetainRV)->setCalledFunction(getRetainCallee(F.getParent()));
Michael Gottesman36e4bc42013-01-05 17:55:42 +00002272
2273 DEBUG(dbgs() << " New: "
2274 << *RetainRV << "\n");
2275
John McCall9fbd3182011-06-15 23:37:01 +00002276 return false;
2277}
2278
2279/// OptimizeAutoreleaseRVCall - Turn objc_autoreleaseReturnValue into
2280/// objc_autorelease if the result is not used as a return value.
2281void
2282ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV) {
2283 // Check for a return of the pointer value.
2284 const Value *Ptr = GetObjCArg(AutoreleaseRV);
Dan Gohman126a54f2011-08-12 00:36:31 +00002285 SmallVector<const Value *, 2> Users;
2286 Users.push_back(Ptr);
2287 do {
2288 Ptr = Users.pop_back_val();
2289 for (Value::const_use_iterator UI = Ptr->use_begin(), UE = Ptr->use_end();
2290 UI != UE; ++UI) {
2291 const User *I = *UI;
2292 if (isa<ReturnInst>(I) || GetBasicInstructionClass(I) == IC_RetainRV)
2293 return;
2294 if (isa<BitCastInst>(I))
2295 Users.push_back(I);
2296 }
2297 } while (!Users.empty());
John McCall9fbd3182011-06-15 23:37:01 +00002298
2299 Changed = true;
2300 ++NumPeeps;
2301 cast<CallInst>(AutoreleaseRV)->
2302 setCalledFunction(getAutoreleaseCallee(F.getParent()));
2303}
2304
2305/// OptimizeIndividualCalls - Visit each call, one at a time, and make
2306/// simplifications without doing any additional analysis.
2307void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
2308 // Reset all the flags in preparation for recomputing them.
2309 UsedInThisFunction = 0;
2310
2311 // Visit all objc_* calls in F.
2312 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2313 Instruction *Inst = &*I++;
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00002314
Michael Gottesman5c0ae472013-01-04 21:29:57 +00002315 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Visiting: " <<
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00002316 *Inst << "\n");
2317
John McCall9fbd3182011-06-15 23:37:01 +00002318 InstructionClass Class = GetBasicInstructionClass(Inst);
2319
2320 switch (Class) {
2321 default: break;
2322
2323 // Delete no-op casts. These function calls have special semantics, but
2324 // the semantics are entirely implemented via lowering in the front-end,
2325 // so by the time they reach the optimizer, they are just no-op calls
2326 // which return their argument.
2327 //
2328 // There are gray areas here, as the ability to cast reference-counted
2329 // pointers to raw void* and back allows code to break ARC assumptions,
2330 // however these are currently considered to be unimportant.
2331 case IC_NoopCast:
2332 Changed = true;
2333 ++NumNoops;
2334 EraseInstruction(Inst);
2335 continue;
2336
2337 // If the pointer-to-weak-pointer is null, it's undefined behavior.
2338 case IC_StoreWeak:
2339 case IC_LoadWeak:
2340 case IC_LoadWeakRetained:
2341 case IC_InitWeak:
2342 case IC_DestroyWeak: {
2343 CallInst *CI = cast<CallInst>(Inst);
2344 if (isNullOrUndef(CI->getArgOperand(0))) {
Dan Gohmand6bf2012012-04-13 18:57:48 +00002345 Changed = true;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002346 Type *Ty = CI->getArgOperand(0)->getType();
John McCall9fbd3182011-06-15 23:37:01 +00002347 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
2348 Constant::getNullValue(Ty),
2349 CI);
2350 CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
2351 CI->eraseFromParent();
2352 continue;
2353 }
2354 break;
2355 }
2356 case IC_CopyWeak:
2357 case IC_MoveWeak: {
2358 CallInst *CI = cast<CallInst>(Inst);
2359 if (isNullOrUndef(CI->getArgOperand(0)) ||
2360 isNullOrUndef(CI->getArgOperand(1))) {
Dan Gohmand6bf2012012-04-13 18:57:48 +00002361 Changed = true;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002362 Type *Ty = CI->getArgOperand(0)->getType();
John McCall9fbd3182011-06-15 23:37:01 +00002363 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
2364 Constant::getNullValue(Ty),
2365 CI);
2366 CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
2367 CI->eraseFromParent();
2368 continue;
2369 }
2370 break;
2371 }
2372 case IC_Retain:
2373 OptimizeRetainCall(F, Inst);
2374 break;
2375 case IC_RetainRV:
2376 if (OptimizeRetainRVCall(F, Inst))
2377 continue;
2378 break;
2379 case IC_AutoreleaseRV:
2380 OptimizeAutoreleaseRVCall(F, Inst);
2381 break;
2382 }
2383
2384 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
2385 if (IsAutorelease(Class) && Inst->use_empty()) {
2386 CallInst *Call = cast<CallInst>(Inst);
2387 const Value *Arg = Call->getArgOperand(0);
2388 Arg = FindSingleUseIdentifiedObject(Arg);
2389 if (Arg) {
2390 Changed = true;
2391 ++NumAutoreleases;
2392
2393 // Create the declaration lazily.
2394 LLVMContext &C = Inst->getContext();
2395 CallInst *NewCall =
2396 CallInst::Create(getReleaseCallee(F.getParent()),
2397 Call->getArgOperand(0), "", Call);
2398 NewCall->setMetadata(ImpreciseReleaseMDKind,
2399 MDNode::get(C, ArrayRef<Value *>()));
2400 EraseInstruction(Call);
2401 Inst = NewCall;
2402 Class = IC_Release;
2403 }
2404 }
2405
2406 // For functions which can never be passed stack arguments, add
2407 // a tail keyword.
2408 if (IsAlwaysTail(Class)) {
2409 Changed = true;
2410 cast<CallInst>(Inst)->setTailCall();
2411 }
2412
2413 // Set nounwind as needed.
2414 if (IsNoThrow(Class)) {
2415 Changed = true;
2416 cast<CallInst>(Inst)->setDoesNotThrow();
2417 }
2418
2419 if (!IsNoopOnNull(Class)) {
2420 UsedInThisFunction |= 1 << Class;
2421 continue;
2422 }
2423
2424 const Value *Arg = GetObjCArg(Inst);
2425
2426 // ARC calls with null are no-ops. Delete them.
2427 if (isNullOrUndef(Arg)) {
2428 Changed = true;
2429 ++NumNoops;
2430 EraseInstruction(Inst);
2431 continue;
2432 }
2433
2434 // Keep track of which of retain, release, autorelease, and retain_block
2435 // are actually present in this function.
2436 UsedInThisFunction |= 1 << Class;
2437
2438 // If Arg is a PHI, and one or more incoming values to the
2439 // PHI are null, and the call is control-equivalent to the PHI, and there
2440 // are no relevant side effects between the PHI and the call, the call
2441 // could be pushed up to just those paths with non-null incoming values.
2442 // For now, don't bother splitting critical edges for this.
2443 SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
2444 Worklist.push_back(std::make_pair(Inst, Arg));
2445 do {
2446 std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
2447 Inst = Pair.first;
2448 Arg = Pair.second;
2449
2450 const PHINode *PN = dyn_cast<PHINode>(Arg);
2451 if (!PN) continue;
2452
2453 // Determine if the PHI has any null operands, or any incoming
2454 // critical edges.
2455 bool HasNull = false;
2456 bool HasCriticalEdges = false;
2457 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2458 Value *Incoming =
2459 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
2460 if (isNullOrUndef(Incoming))
2461 HasNull = true;
2462 else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
2463 .getNumSuccessors() != 1) {
2464 HasCriticalEdges = true;
2465 break;
2466 }
2467 }
2468 // If we have null operands and no critical edges, optimize.
2469 if (!HasCriticalEdges && HasNull) {
2470 SmallPtrSet<Instruction *, 4> DependingInstructions;
2471 SmallPtrSet<const BasicBlock *, 4> Visited;
2472
2473 // Check that there is nothing that cares about the reference
2474 // count between the call and the phi.
Dan Gohman511568d2012-04-13 00:59:57 +00002475 switch (Class) {
2476 case IC_Retain:
2477 case IC_RetainBlock:
2478 // These can always be moved up.
2479 break;
2480 case IC_Release:
Dan Gohman0daef3d2012-05-08 23:39:44 +00002481 // These can't be moved across things that care about the retain
2482 // count.
Dan Gohman511568d2012-04-13 00:59:57 +00002483 FindDependencies(NeedsPositiveRetainCount, Arg,
2484 Inst->getParent(), Inst,
2485 DependingInstructions, Visited, PA);
2486 break;
2487 case IC_Autorelease:
2488 // These can't be moved across autorelease pool scope boundaries.
2489 FindDependencies(AutoreleasePoolBoundary, Arg,
2490 Inst->getParent(), Inst,
2491 DependingInstructions, Visited, PA);
2492 break;
2493 case IC_RetainRV:
2494 case IC_AutoreleaseRV:
2495 // Don't move these; the RV optimization depends on the autoreleaseRV
2496 // being tail called, and the retainRV being immediately after a call
2497 // (which might still happen if we get lucky with codegen layout, but
2498 // it's not worth taking the chance).
2499 continue;
2500 default:
2501 llvm_unreachable("Invalid dependence flavor");
2502 }
2503
John McCall9fbd3182011-06-15 23:37:01 +00002504 if (DependingInstructions.size() == 1 &&
2505 *DependingInstructions.begin() == PN) {
2506 Changed = true;
2507 ++NumPartialNoops;
2508 // Clone the call into each predecessor that has a non-null value.
2509 CallInst *CInst = cast<CallInst>(Inst);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002510 Type *ParamTy = CInst->getArgOperand(0)->getType();
John McCall9fbd3182011-06-15 23:37:01 +00002511 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2512 Value *Incoming =
2513 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
2514 if (!isNullOrUndef(Incoming)) {
2515 CallInst *Clone = cast<CallInst>(CInst->clone());
2516 Value *Op = PN->getIncomingValue(i);
2517 Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
2518 if (Op->getType() != ParamTy)
2519 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
2520 Clone->setArgOperand(0, Op);
2521 Clone->insertBefore(InsertPos);
2522 Worklist.push_back(std::make_pair(Clone, Incoming));
2523 }
2524 }
2525 // Erase the original call.
2526 EraseInstruction(CInst);
2527 continue;
2528 }
2529 }
2530 } while (!Worklist.empty());
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00002531
Michael Gottesman5c0ae472013-01-04 21:29:57 +00002532 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Finished Queue.\n\n");
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00002533
John McCall9fbd3182011-06-15 23:37:01 +00002534 }
2535}
2536
2537/// CheckForCFGHazards - Check for critical edges, loop boundaries, irreducible
2538/// control flow, or other CFG structures where moving code across the edge
2539/// would result in it being executed more.
2540void
2541ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
2542 DenseMap<const BasicBlock *, BBState> &BBStates,
2543 BBState &MyStates) const {
2544 // If any top-down local-use or possible-dec has a succ which is earlier in
2545 // the sequence, forget it.
Dan Gohman22cc4cc2012-03-02 01:13:53 +00002546 for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
John McCall9fbd3182011-06-15 23:37:01 +00002547 E = MyStates.top_down_ptr_end(); I != E; ++I)
2548 switch (I->second.GetSeq()) {
2549 default: break;
2550 case S_Use: {
2551 const Value *Arg = I->first;
2552 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
2553 bool SomeSuccHasSame = false;
2554 bool AllSuccsHaveSame = true;
Dan Gohman22cc4cc2012-03-02 01:13:53 +00002555 PtrState &S = I->second;
Dan Gohmandbe266b2012-02-17 18:59:53 +00002556 succ_const_iterator SI(TI), SE(TI, false);
2557
2558 // If the terminator is an invoke marked with the
2559 // clang.arc.no_objc_arc_exceptions metadata, the unwind edge can be
2560 // ignored, for ARC purposes.
2561 if (isa<InvokeInst>(TI) && TI->getMetadata(NoObjCARCExceptionsMDKind))
2562 --SE;
2563
2564 for (; SI != SE; ++SI) {
Dan Gohman70e29682012-03-02 01:26:46 +00002565 Sequence SuccSSeq = S_None;
2566 bool SuccSRRIKnownSafe = false;
Dan Gohman0daef3d2012-05-08 23:39:44 +00002567 // If VisitBottomUp has pointer information for this successor, take
2568 // what we know about it.
Dan Gohman447989c2012-04-27 18:56:31 +00002569 DenseMap<const BasicBlock *, BBState>::iterator BBI =
2570 BBStates.find(*SI);
2571 assert(BBI != BBStates.end());
2572 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
2573 SuccSSeq = SuccS.GetSeq();
2574 SuccSRRIKnownSafe = SuccS.RRI.KnownSafe;
Dan Gohman70e29682012-03-02 01:26:46 +00002575 switch (SuccSSeq) {
John McCall9fbd3182011-06-15 23:37:01 +00002576 case S_None:
Dan Gohmana7f7db22011-08-12 00:26:31 +00002577 case S_CanRelease: {
Dan Gohman70e29682012-03-02 01:26:46 +00002578 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
Dan Gohmana7f7db22011-08-12 00:26:31 +00002579 S.ClearSequenceProgress();
Dan Gohman70e29682012-03-02 01:26:46 +00002580 break;
2581 }
Dan Gohmana7f7db22011-08-12 00:26:31 +00002582 continue;
2583 }
John McCall9fbd3182011-06-15 23:37:01 +00002584 case S_Use:
2585 SomeSuccHasSame = true;
2586 break;
2587 case S_Stop:
2588 case S_Release:
2589 case S_MovableRelease:
Dan Gohman70e29682012-03-02 01:26:46 +00002590 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
Dan Gohmana7f7db22011-08-12 00:26:31 +00002591 AllSuccsHaveSame = false;
John McCall9fbd3182011-06-15 23:37:01 +00002592 break;
2593 case S_Retain:
2594 llvm_unreachable("bottom-up pointer in retain state!");
2595 }
Dan Gohmana7f7db22011-08-12 00:26:31 +00002596 }
John McCall9fbd3182011-06-15 23:37:01 +00002597 // If the state at the other end of any of the successor edges
2598 // matches the current state, require all edges to match. This
2599 // guards against loops in the middle of a sequence.
2600 if (SomeSuccHasSame && !AllSuccsHaveSame)
Dan Gohmana7f7db22011-08-12 00:26:31 +00002601 S.ClearSequenceProgress();
Dan Gohman2e68beb2011-12-12 18:13:53 +00002602 break;
John McCall9fbd3182011-06-15 23:37:01 +00002603 }
2604 case S_CanRelease: {
2605 const Value *Arg = I->first;
2606 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
2607 bool SomeSuccHasSame = false;
2608 bool AllSuccsHaveSame = true;
Dan Gohman22cc4cc2012-03-02 01:13:53 +00002609 PtrState &S = I->second;
Dan Gohmandbe266b2012-02-17 18:59:53 +00002610 succ_const_iterator SI(TI), SE(TI, false);
2611
2612 // If the terminator is an invoke marked with the
2613 // clang.arc.no_objc_arc_exceptions metadata, the unwind edge can be
2614 // ignored, for ARC purposes.
2615 if (isa<InvokeInst>(TI) && TI->getMetadata(NoObjCARCExceptionsMDKind))
2616 --SE;
2617
2618 for (; SI != SE; ++SI) {
Dan Gohman70e29682012-03-02 01:26:46 +00002619 Sequence SuccSSeq = S_None;
2620 bool SuccSRRIKnownSafe = false;
Dan Gohman0daef3d2012-05-08 23:39:44 +00002621 // If VisitBottomUp has pointer information for this successor, take
2622 // what we know about it.
Dan Gohman447989c2012-04-27 18:56:31 +00002623 DenseMap<const BasicBlock *, BBState>::iterator BBI =
2624 BBStates.find(*SI);
2625 assert(BBI != BBStates.end());
2626 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
2627 SuccSSeq = SuccS.GetSeq();
2628 SuccSRRIKnownSafe = SuccS.RRI.KnownSafe;
Dan Gohman70e29682012-03-02 01:26:46 +00002629 switch (SuccSSeq) {
Dan Gohmana7f7db22011-08-12 00:26:31 +00002630 case S_None: {
Dan Gohman70e29682012-03-02 01:26:46 +00002631 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
Dan Gohmana7f7db22011-08-12 00:26:31 +00002632 S.ClearSequenceProgress();
Dan Gohman70e29682012-03-02 01:26:46 +00002633 break;
2634 }
Dan Gohmana7f7db22011-08-12 00:26:31 +00002635 continue;
2636 }
John McCall9fbd3182011-06-15 23:37:01 +00002637 case S_CanRelease:
2638 SomeSuccHasSame = true;
2639 break;
2640 case S_Stop:
2641 case S_Release:
2642 case S_MovableRelease:
2643 case S_Use:
Dan Gohman70e29682012-03-02 01:26:46 +00002644 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
Dan Gohmana7f7db22011-08-12 00:26:31 +00002645 AllSuccsHaveSame = false;
John McCall9fbd3182011-06-15 23:37:01 +00002646 break;
2647 case S_Retain:
2648 llvm_unreachable("bottom-up pointer in retain state!");
2649 }
Dan Gohmana7f7db22011-08-12 00:26:31 +00002650 }
John McCall9fbd3182011-06-15 23:37:01 +00002651 // If the state at the other end of any of the successor edges
2652 // matches the current state, require all edges to match. This
2653 // guards against loops in the middle of a sequence.
2654 if (SomeSuccHasSame && !AllSuccsHaveSame)
Dan Gohmana7f7db22011-08-12 00:26:31 +00002655 S.ClearSequenceProgress();
Dan Gohman2e68beb2011-12-12 18:13:53 +00002656 break;
John McCall9fbd3182011-06-15 23:37:01 +00002657 }
2658 }
2659}
2660
2661bool
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002662ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
Dan Gohmanfbab4a82012-03-23 17:47:54 +00002663 BasicBlock *BB,
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002664 MapVector<Value *, RRInfo> &Retains,
2665 BBState &MyStates) {
2666 bool NestingDetected = false;
2667 InstructionClass Class = GetInstructionClass(Inst);
2668 const Value *Arg = 0;
2669
2670 switch (Class) {
2671 case IC_Release: {
2672 Arg = GetObjCArg(Inst);
2673
2674 PtrState &S = MyStates.getPtrBottomUpState(Arg);
2675
2676 // If we see two releases in a row on the same pointer. If so, make
2677 // a note, and we'll cicle back to revisit it after we've
2678 // hopefully eliminated the second release, which may allow us to
2679 // eliminate the first release too.
2680 // Theoretically we could implement removal of nested retain+release
2681 // pairs by making PtrState hold a stack of states, but this is
2682 // simple and avoids adding overhead for the non-nested case.
2683 if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease)
2684 NestingDetected = true;
2685
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002686 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
Dan Gohman50ade652012-04-25 00:50:46 +00002687 S.ResetSequenceProgress(ReleaseMetadata ? S_MovableRelease : S_Release);
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002688 S.RRI.ReleaseMetadata = ReleaseMetadata;
Dan Gohman230768b2012-09-04 23:16:20 +00002689 S.RRI.KnownSafe = S.IsKnownIncremented();
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002690 S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
2691 S.RRI.Calls.insert(Inst);
2692
Dan Gohman230768b2012-09-04 23:16:20 +00002693 S.SetKnownPositiveRefCount();
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002694 break;
2695 }
2696 case IC_RetainBlock:
2697 // An objc_retainBlock call with just a use may need to be kept,
2698 // because it may be copying a block from the stack to the heap.
2699 if (!IsRetainBlockOptimizable(Inst))
2700 break;
2701 // FALLTHROUGH
2702 case IC_Retain:
2703 case IC_RetainRV: {
2704 Arg = GetObjCArg(Inst);
2705
2706 PtrState &S = MyStates.getPtrBottomUpState(Arg);
Dan Gohman50ade652012-04-25 00:50:46 +00002707 S.SetKnownPositiveRefCount();
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002708
2709 switch (S.GetSeq()) {
2710 case S_Stop:
2711 case S_Release:
2712 case S_MovableRelease:
2713 case S_Use:
2714 S.RRI.ReverseInsertPts.clear();
2715 // FALL THROUGH
2716 case S_CanRelease:
2717 // Don't do retain+release tracking for IC_RetainRV, because it's
2718 // better to let it remain as the first instruction after a call.
2719 if (Class != IC_RetainRV) {
2720 S.RRI.IsRetainBlock = Class == IC_RetainBlock;
2721 Retains[Inst] = S.RRI;
2722 }
2723 S.ClearSequenceProgress();
2724 break;
2725 case S_None:
2726 break;
2727 case S_Retain:
2728 llvm_unreachable("bottom-up pointer in retain state!");
2729 }
2730 return NestingDetected;
2731 }
2732 case IC_AutoreleasepoolPop:
2733 // Conservatively, clear MyStates for all known pointers.
2734 MyStates.clearBottomUpPointers();
2735 return NestingDetected;
2736 case IC_AutoreleasepoolPush:
2737 case IC_None:
2738 // These are irrelevant.
2739 return NestingDetected;
2740 default:
2741 break;
2742 }
2743
2744 // Consider any other possible effects of this instruction on each
2745 // pointer being tracked.
2746 for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(),
2747 ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) {
2748 const Value *Ptr = MI->first;
2749 if (Ptr == Arg)
2750 continue; // Handled above.
2751 PtrState &S = MI->second;
2752 Sequence Seq = S.GetSeq();
2753
2754 // Check for possible releases.
2755 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
Dan Gohman50ade652012-04-25 00:50:46 +00002756 S.ClearRefCount();
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002757 switch (Seq) {
2758 case S_Use:
2759 S.SetSeq(S_CanRelease);
2760 continue;
2761 case S_CanRelease:
2762 case S_Release:
2763 case S_MovableRelease:
2764 case S_Stop:
2765 case S_None:
2766 break;
2767 case S_Retain:
2768 llvm_unreachable("bottom-up pointer in retain state!");
2769 }
2770 }
2771
2772 // Check for possible direct uses.
2773 switch (Seq) {
2774 case S_Release:
2775 case S_MovableRelease:
2776 if (CanUse(Inst, Ptr, PA, Class)) {
2777 assert(S.RRI.ReverseInsertPts.empty());
Dan Gohmanfbab4a82012-03-23 17:47:54 +00002778 // If this is an invoke instruction, we're scanning it as part of
2779 // one of its successor blocks, since we can't insert code after it
2780 // in its own block, and we don't want to split critical edges.
2781 if (isa<InvokeInst>(Inst))
2782 S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
2783 else
Francois Pichetb54a5ed2012-03-24 01:36:37 +00002784 S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002785 S.SetSeq(S_Use);
2786 } else if (Seq == S_Release &&
2787 (Class == IC_User || Class == IC_CallOrUser)) {
2788 // Non-movable releases depend on any possible objc pointer use.
2789 S.SetSeq(S_Stop);
2790 assert(S.RRI.ReverseInsertPts.empty());
Dan Gohmanfbab4a82012-03-23 17:47:54 +00002791 // As above; handle invoke specially.
2792 if (isa<InvokeInst>(Inst))
2793 S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
2794 else
Francois Pichetb54a5ed2012-03-24 01:36:37 +00002795 S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002796 }
2797 break;
2798 case S_Stop:
2799 if (CanUse(Inst, Ptr, PA, Class))
2800 S.SetSeq(S_Use);
2801 break;
2802 case S_CanRelease:
2803 case S_Use:
2804 case S_None:
2805 break;
2806 case S_Retain:
2807 llvm_unreachable("bottom-up pointer in retain state!");
2808 }
2809 }
2810
2811 return NestingDetected;
2812}
2813
2814bool
John McCall9fbd3182011-06-15 23:37:01 +00002815ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
2816 DenseMap<const BasicBlock *, BBState> &BBStates,
2817 MapVector<Value *, RRInfo> &Retains) {
2818 bool NestingDetected = false;
2819 BBState &MyStates = BBStates[BB];
2820
2821 // Merge the states from each successor to compute the initial state
2822 // for the current block.
Dan Gohman40e46602012-08-27 18:31:36 +00002823 BBState::edge_iterator SI(MyStates.succ_begin()),
2824 SE(MyStates.succ_end());
2825 if (SI != SE) {
Dan Gohmaneeeb7752012-04-24 22:53:18 +00002826 const BasicBlock *Succ = *SI;
2827 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
2828 assert(I != BBStates.end());
2829 MyStates.InitFromSucc(I->second);
2830 ++SI;
2831 for (; SI != SE; ++SI) {
2832 Succ = *SI;
2833 I = BBStates.find(Succ);
2834 assert(I != BBStates.end());
2835 MyStates.MergeSucc(I->second);
2836 }
Dan Gohmandbe266b2012-02-17 18:59:53 +00002837 }
John McCall9fbd3182011-06-15 23:37:01 +00002838
2839 // Visit all the instructions, bottom-up.
2840 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
2841 Instruction *Inst = llvm::prior(I);
Dan Gohmanfbab4a82012-03-23 17:47:54 +00002842
2843 // Invoke instructions are visited as part of their successors (below).
2844 if (isa<InvokeInst>(Inst))
2845 continue;
2846
2847 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
2848 }
2849
Dan Gohman447989c2012-04-27 18:56:31 +00002850 // If there's a predecessor with an invoke, visit the invoke as if it were
2851 // part of this block, since we can't insert code after an invoke in its own
2852 // block, and we don't want to split critical edges.
Dan Gohmaneeeb7752012-04-24 22:53:18 +00002853 for (BBState::edge_iterator PI(MyStates.pred_begin()),
2854 PE(MyStates.pred_end()); PI != PE; ++PI) {
Dan Gohmanfbab4a82012-03-23 17:47:54 +00002855 BasicBlock *Pred = *PI;
Dan Gohman447989c2012-04-27 18:56:31 +00002856 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
2857 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002858 }
John McCall9fbd3182011-06-15 23:37:01 +00002859
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002860 return NestingDetected;
2861}
John McCall9fbd3182011-06-15 23:37:01 +00002862
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002863bool
2864ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
2865 DenseMap<Value *, RRInfo> &Releases,
2866 BBState &MyStates) {
2867 bool NestingDetected = false;
2868 InstructionClass Class = GetInstructionClass(Inst);
2869 const Value *Arg = 0;
John McCall9fbd3182011-06-15 23:37:01 +00002870
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002871 switch (Class) {
2872 case IC_RetainBlock:
2873 // An objc_retainBlock call with just a use may need to be kept,
2874 // because it may be copying a block from the stack to the heap.
2875 if (!IsRetainBlockOptimizable(Inst))
2876 break;
2877 // FALLTHROUGH
2878 case IC_Retain:
2879 case IC_RetainRV: {
2880 Arg = GetObjCArg(Inst);
2881
2882 PtrState &S = MyStates.getPtrTopDownState(Arg);
2883
2884 // Don't do retain+release tracking for IC_RetainRV, because it's
2885 // better to let it remain as the first instruction after a call.
2886 if (Class != IC_RetainRV) {
2887 // If we see two retains in a row on the same pointer. If so, make
John McCall9fbd3182011-06-15 23:37:01 +00002888 // a note, and we'll cicle back to revisit it after we've
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002889 // hopefully eliminated the second retain, which may allow us to
2890 // eliminate the first retain too.
John McCall9fbd3182011-06-15 23:37:01 +00002891 // Theoretically we could implement removal of nested retain+release
2892 // pairs by making PtrState hold a stack of states, but this is
2893 // simple and avoids adding overhead for the non-nested case.
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002894 if (S.GetSeq() == S_Retain)
John McCall9fbd3182011-06-15 23:37:01 +00002895 NestingDetected = true;
2896
Dan Gohman50ade652012-04-25 00:50:46 +00002897 S.ResetSequenceProgress(S_Retain);
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002898 S.RRI.IsRetainBlock = Class == IC_RetainBlock;
Dan Gohman230768b2012-09-04 23:16:20 +00002899 S.RRI.KnownSafe = S.IsKnownIncremented();
John McCall9fbd3182011-06-15 23:37:01 +00002900 S.RRI.Calls.insert(Inst);
John McCall9fbd3182011-06-15 23:37:01 +00002901 }
John McCall9fbd3182011-06-15 23:37:01 +00002902
Dan Gohman230768b2012-09-04 23:16:20 +00002903 S.SetKnownPositiveRefCount();
Dan Gohmanc72d3be2012-07-23 19:27:31 +00002904
2905 // A retain can be a potential use; procede to the generic checking
2906 // code below.
2907 break;
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002908 }
2909 case IC_Release: {
2910 Arg = GetObjCArg(Inst);
2911
2912 PtrState &S = MyStates.getPtrTopDownState(Arg);
Dan Gohman230768b2012-09-04 23:16:20 +00002913 S.ClearRefCount();
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002914
2915 switch (S.GetSeq()) {
2916 case S_Retain:
2917 case S_CanRelease:
2918 S.RRI.ReverseInsertPts.clear();
2919 // FALL THROUGH
2920 case S_Use:
2921 S.RRI.ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
2922 S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
2923 Releases[Inst] = S.RRI;
2924 S.ClearSequenceProgress();
2925 break;
2926 case S_None:
2927 break;
2928 case S_Stop:
2929 case S_Release:
2930 case S_MovableRelease:
2931 llvm_unreachable("top-down pointer in release state!");
2932 }
2933 break;
2934 }
2935 case IC_AutoreleasepoolPop:
2936 // Conservatively, clear MyStates for all known pointers.
2937 MyStates.clearTopDownPointers();
2938 return NestingDetected;
2939 case IC_AutoreleasepoolPush:
2940 case IC_None:
2941 // These are irrelevant.
2942 return NestingDetected;
2943 default:
2944 break;
2945 }
2946
2947 // Consider any other possible effects of this instruction on each
2948 // pointer being tracked.
2949 for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
2950 ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
2951 const Value *Ptr = MI->first;
2952 if (Ptr == Arg)
2953 continue; // Handled above.
2954 PtrState &S = MI->second;
2955 Sequence Seq = S.GetSeq();
2956
2957 // Check for possible releases.
2958 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
Dan Gohman50ade652012-04-25 00:50:46 +00002959 S.ClearRefCount();
John McCall9fbd3182011-06-15 23:37:01 +00002960 switch (Seq) {
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002961 case S_Retain:
2962 S.SetSeq(S_CanRelease);
2963 assert(S.RRI.ReverseInsertPts.empty());
2964 S.RRI.ReverseInsertPts.insert(Inst);
2965
2966 // One call can't cause a transition from S_Retain to S_CanRelease
2967 // and S_CanRelease to S_Use. If we've made the first transition,
2968 // we're done.
2969 continue;
John McCall9fbd3182011-06-15 23:37:01 +00002970 case S_Use:
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002971 case S_CanRelease:
John McCall9fbd3182011-06-15 23:37:01 +00002972 case S_None:
2973 break;
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002974 case S_Stop:
2975 case S_Release:
2976 case S_MovableRelease:
2977 llvm_unreachable("top-down pointer in release state!");
John McCall9fbd3182011-06-15 23:37:01 +00002978 }
2979 }
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00002980
2981 // Check for possible direct uses.
2982 switch (Seq) {
2983 case S_CanRelease:
2984 if (CanUse(Inst, Ptr, PA, Class))
2985 S.SetSeq(S_Use);
2986 break;
2987 case S_Retain:
2988 case S_Use:
2989 case S_None:
2990 break;
2991 case S_Stop:
2992 case S_Release:
2993 case S_MovableRelease:
2994 llvm_unreachable("top-down pointer in release state!");
2995 }
John McCall9fbd3182011-06-15 23:37:01 +00002996 }
2997
2998 return NestingDetected;
2999}
3000
3001bool
3002ObjCARCOpt::VisitTopDown(BasicBlock *BB,
3003 DenseMap<const BasicBlock *, BBState> &BBStates,
3004 DenseMap<Value *, RRInfo> &Releases) {
3005 bool NestingDetected = false;
3006 BBState &MyStates = BBStates[BB];
3007
3008 // Merge the states from each predecessor to compute the initial state
3009 // for the current block.
Dan Gohman40e46602012-08-27 18:31:36 +00003010 BBState::edge_iterator PI(MyStates.pred_begin()),
3011 PE(MyStates.pred_end());
3012 if (PI != PE) {
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003013 const BasicBlock *Pred = *PI;
3014 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
3015 assert(I != BBStates.end());
3016 MyStates.InitFromPred(I->second);
3017 ++PI;
3018 for (; PI != PE; ++PI) {
3019 Pred = *PI;
3020 I = BBStates.find(Pred);
3021 assert(I != BBStates.end());
3022 MyStates.MergePred(I->second);
3023 }
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003024 }
John McCall9fbd3182011-06-15 23:37:01 +00003025
3026 // Visit all the instructions, top-down.
3027 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
3028 Instruction *Inst = I;
Dan Gohmanc7f5c6e2012-03-22 18:24:56 +00003029 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
John McCall9fbd3182011-06-15 23:37:01 +00003030 }
3031
3032 CheckForCFGHazards(BB, BBStates, MyStates);
3033 return NestingDetected;
3034}
3035
Dan Gohman59a1c932011-12-12 19:42:25 +00003036static void
3037ComputePostOrders(Function &F,
3038 SmallVectorImpl<BasicBlock *> &PostOrder,
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003039 SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
3040 unsigned NoObjCARCExceptionsMDKind,
3041 DenseMap<const BasicBlock *, BBState> &BBStates) {
Dan Gohman59a1c932011-12-12 19:42:25 +00003042 /// Visited - The visited set, for doing DFS walks.
3043 SmallPtrSet<BasicBlock *, 16> Visited;
3044
3045 // Do DFS, computing the PostOrder.
3046 SmallPtrSet<BasicBlock *, 16> OnStack;
3047 SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003048
3049 // Functions always have exactly one entry block, and we don't have
3050 // any other block that we treat like an entry block.
Dan Gohman59a1c932011-12-12 19:42:25 +00003051 BasicBlock *EntryBB = &F.getEntryBlock();
Dan Gohman0daef3d2012-05-08 23:39:44 +00003052 BBState &MyStates = BBStates[EntryBB];
3053 MyStates.SetAsEntry();
3054 TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
3055 SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
Dan Gohman59a1c932011-12-12 19:42:25 +00003056 Visited.insert(EntryBB);
3057 OnStack.insert(EntryBB);
3058 do {
3059 dfs_next_succ:
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003060 BasicBlock *CurrBB = SuccStack.back().first;
3061 TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
3062 succ_iterator SE(TI, false);
Dan Gohman0daef3d2012-05-08 23:39:44 +00003063
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003064 // If the terminator is an invoke marked with the
3065 // clang.arc.no_objc_arc_exceptions metadata, the unwind edge can be
3066 // ignored, for ARC purposes.
3067 if (isa<InvokeInst>(TI) && TI->getMetadata(NoObjCARCExceptionsMDKind))
3068 --SE;
3069
3070 while (SuccStack.back().second != SE) {
3071 BasicBlock *SuccBB = *SuccStack.back().second++;
3072 if (Visited.insert(SuccBB)) {
Dan Gohman0daef3d2012-05-08 23:39:44 +00003073 TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
3074 SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003075 BBStates[CurrBB].addSucc(SuccBB);
Dan Gohman0daef3d2012-05-08 23:39:44 +00003076 BBState &SuccStates = BBStates[SuccBB];
3077 SuccStates.addPred(CurrBB);
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003078 OnStack.insert(SuccBB);
Dan Gohman59a1c932011-12-12 19:42:25 +00003079 goto dfs_next_succ;
3080 }
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003081
3082 if (!OnStack.count(SuccBB)) {
3083 BBStates[CurrBB].addSucc(SuccBB);
3084 BBStates[SuccBB].addPred(CurrBB);
3085 }
Dan Gohman59a1c932011-12-12 19:42:25 +00003086 }
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003087 OnStack.erase(CurrBB);
3088 PostOrder.push_back(CurrBB);
3089 SuccStack.pop_back();
Dan Gohman59a1c932011-12-12 19:42:25 +00003090 } while (!SuccStack.empty());
3091
3092 Visited.clear();
3093
Dan Gohman59a1c932011-12-12 19:42:25 +00003094 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003095 // Functions may have many exits, and there also blocks which we treat
3096 // as exits due to ignored edges.
3097 SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
3098 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
3099 BasicBlock *ExitBB = I;
3100 BBState &MyStates = BBStates[ExitBB];
3101 if (!MyStates.isExit())
3102 continue;
3103
Dan Gohman447989c2012-04-27 18:56:31 +00003104 MyStates.SetAsExit();
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003105
3106 PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
Dan Gohman59a1c932011-12-12 19:42:25 +00003107 Visited.insert(ExitBB);
3108 while (!PredStack.empty()) {
3109 reverse_dfs_next_succ:
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003110 BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
3111 while (PredStack.back().second != PE) {
Dan Gohman59a1c932011-12-12 19:42:25 +00003112 BasicBlock *BB = *PredStack.back().second++;
Dan Gohman59a1c932011-12-12 19:42:25 +00003113 if (Visited.insert(BB)) {
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003114 PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
Dan Gohman59a1c932011-12-12 19:42:25 +00003115 goto reverse_dfs_next_succ;
3116 }
3117 }
3118 ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
3119 }
3120 }
3121}
3122
John McCall9fbd3182011-06-15 23:37:01 +00003123// Visit - Visit the function both top-down and bottom-up.
3124bool
3125ObjCARCOpt::Visit(Function &F,
3126 DenseMap<const BasicBlock *, BBState> &BBStates,
3127 MapVector<Value *, RRInfo> &Retains,
3128 DenseMap<Value *, RRInfo> &Releases) {
Dan Gohman59a1c932011-12-12 19:42:25 +00003129
3130 // Use reverse-postorder traversals, because we magically know that loops
3131 // will be well behaved, i.e. they won't repeatedly call retain on a single
3132 // pointer without doing a release. We can't use the ReversePostOrderTraversal
3133 // class here because we want the reverse-CFG postorder to consider each
3134 // function exit point, and we want to ignore selected cycle edges.
3135 SmallVector<BasicBlock *, 16> PostOrder;
3136 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003137 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
3138 NoObjCARCExceptionsMDKind,
3139 BBStates);
Dan Gohman59a1c932011-12-12 19:42:25 +00003140
3141 // Use reverse-postorder on the reverse CFG for bottom-up.
John McCall9fbd3182011-06-15 23:37:01 +00003142 bool BottomUpNestingDetected = false;
Dan Gohmanb48ef3a2011-08-18 21:27:42 +00003143 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
Dan Gohman59a1c932011-12-12 19:42:25 +00003144 ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
3145 I != E; ++I)
3146 BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
John McCall9fbd3182011-06-15 23:37:01 +00003147
Dan Gohman59a1c932011-12-12 19:42:25 +00003148 // Use reverse-postorder for top-down.
John McCall9fbd3182011-06-15 23:37:01 +00003149 bool TopDownNestingDetected = false;
Dan Gohman59a1c932011-12-12 19:42:25 +00003150 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
3151 PostOrder.rbegin(), E = PostOrder.rend();
3152 I != E; ++I)
3153 TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
John McCall9fbd3182011-06-15 23:37:01 +00003154
3155 return TopDownNestingDetected && BottomUpNestingDetected;
3156}
3157
3158/// MoveCalls - Move the calls in RetainsToMove and ReleasesToMove.
3159void ObjCARCOpt::MoveCalls(Value *Arg,
3160 RRInfo &RetainsToMove,
3161 RRInfo &ReleasesToMove,
3162 MapVector<Value *, RRInfo> &Retains,
3163 DenseMap<Value *, RRInfo> &Releases,
Dan Gohman44280692011-07-22 22:29:21 +00003164 SmallVectorImpl<Instruction *> &DeadInsts,
3165 Module *M) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003166 Type *ArgTy = Arg->getType();
Dan Gohman44280692011-07-22 22:29:21 +00003167 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
John McCall9fbd3182011-06-15 23:37:01 +00003168
3169 // Insert the new retain and release calls.
3170 for (SmallPtrSet<Instruction *, 2>::const_iterator
3171 PI = ReleasesToMove.ReverseInsertPts.begin(),
3172 PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
3173 Instruction *InsertPt = *PI;
3174 Value *MyArg = ArgTy == ParamTy ? Arg :
3175 new BitCastInst(Arg, ParamTy, "", InsertPt);
3176 CallInst *Call =
3177 CallInst::Create(RetainsToMove.IsRetainBlock ?
Dan Gohman44280692011-07-22 22:29:21 +00003178 getRetainBlockCallee(M) : getRetainCallee(M),
John McCall9fbd3182011-06-15 23:37:01 +00003179 MyArg, "", InsertPt);
3180 Call->setDoesNotThrow();
Dan Gohman79522dc2012-01-13 00:39:07 +00003181 if (RetainsToMove.IsRetainBlock)
Dan Gohmana974bea2011-10-17 22:53:25 +00003182 Call->setMetadata(CopyOnEscapeMDKind,
3183 MDNode::get(M->getContext(), ArrayRef<Value *>()));
Dan Gohman79522dc2012-01-13 00:39:07 +00003184 else
John McCall9fbd3182011-06-15 23:37:01 +00003185 Call->setTailCall();
3186 }
3187 for (SmallPtrSet<Instruction *, 2>::const_iterator
3188 PI = RetainsToMove.ReverseInsertPts.begin(),
3189 PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
Dan Gohmanfbab4a82012-03-23 17:47:54 +00003190 Instruction *InsertPt = *PI;
3191 Value *MyArg = ArgTy == ParamTy ? Arg :
3192 new BitCastInst(Arg, ParamTy, "", InsertPt);
3193 CallInst *Call = CallInst::Create(getReleaseCallee(M), MyArg,
3194 "", InsertPt);
3195 // Attach a clang.imprecise_release metadata tag, if appropriate.
3196 if (MDNode *M = ReleasesToMove.ReleaseMetadata)
3197 Call->setMetadata(ImpreciseReleaseMDKind, M);
3198 Call->setDoesNotThrow();
3199 if (ReleasesToMove.IsTailCallRelease)
3200 Call->setTailCall();
John McCall9fbd3182011-06-15 23:37:01 +00003201 }
3202
3203 // Delete the original retain and release calls.
3204 for (SmallPtrSet<Instruction *, 2>::const_iterator
3205 AI = RetainsToMove.Calls.begin(),
3206 AE = RetainsToMove.Calls.end(); AI != AE; ++AI) {
3207 Instruction *OrigRetain = *AI;
3208 Retains.blot(OrigRetain);
3209 DeadInsts.push_back(OrigRetain);
3210 }
3211 for (SmallPtrSet<Instruction *, 2>::const_iterator
3212 AI = ReleasesToMove.Calls.begin(),
3213 AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) {
3214 Instruction *OrigRelease = *AI;
3215 Releases.erase(OrigRelease);
3216 DeadInsts.push_back(OrigRelease);
3217 }
3218}
3219
Dan Gohmand6bf2012012-04-13 18:57:48 +00003220/// PerformCodePlacement - Identify pairings between the retains and releases,
3221/// and delete and/or move them.
John McCall9fbd3182011-06-15 23:37:01 +00003222bool
3223ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
3224 &BBStates,
3225 MapVector<Value *, RRInfo> &Retains,
Dan Gohman44280692011-07-22 22:29:21 +00003226 DenseMap<Value *, RRInfo> &Releases,
3227 Module *M) {
John McCall9fbd3182011-06-15 23:37:01 +00003228 bool AnyPairsCompletelyEliminated = false;
3229 RRInfo RetainsToMove;
3230 RRInfo ReleasesToMove;
3231 SmallVector<Instruction *, 4> NewRetains;
3232 SmallVector<Instruction *, 4> NewReleases;
3233 SmallVector<Instruction *, 8> DeadInsts;
3234
Dan Gohmand6bf2012012-04-13 18:57:48 +00003235 // Visit each retain.
John McCall9fbd3182011-06-15 23:37:01 +00003236 for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
Dan Gohman597fece2011-09-29 22:25:23 +00003237 E = Retains.end(); I != E; ++I) {
3238 Value *V = I->first;
John McCall9fbd3182011-06-15 23:37:01 +00003239 if (!V) continue; // blotted
3240
3241 Instruction *Retain = cast<Instruction>(V);
3242 Value *Arg = GetObjCArg(Retain);
3243
Dan Gohman79522dc2012-01-13 00:39:07 +00003244 // If the object being released is in static or stack storage, we know it's
John McCall9fbd3182011-06-15 23:37:01 +00003245 // not being managed by ObjC reference counting, so we can delete pairs
3246 // regardless of what possible decrements or uses lie between them.
Dan Gohman79522dc2012-01-13 00:39:07 +00003247 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
Dan Gohman0daef3d2012-05-08 23:39:44 +00003248
Dan Gohman1b31ea82011-08-22 17:29:11 +00003249 // A constant pointer can't be pointing to an object on the heap. It may
3250 // be reference-counted, but it won't be deleted.
3251 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
3252 if (const GlobalVariable *GV =
3253 dyn_cast<GlobalVariable>(
3254 StripPointerCastsAndObjCCalls(LI->getPointerOperand())))
3255 if (GV->isConstant())
3256 KnownSafe = true;
3257
John McCall9fbd3182011-06-15 23:37:01 +00003258 // If a pair happens in a region where it is known that the reference count
3259 // is already incremented, we can similarly ignore possible decrements.
Dan Gohmane6d5e882011-08-19 00:26:36 +00003260 bool KnownSafeTD = true, KnownSafeBU = true;
John McCall9fbd3182011-06-15 23:37:01 +00003261
3262 // Connect the dots between the top-down-collected RetainsToMove and
3263 // bottom-up-collected ReleasesToMove to form sets of related calls.
3264 // This is an iterative process so that we connect multiple releases
3265 // to multiple retains if needed.
3266 unsigned OldDelta = 0;
3267 unsigned NewDelta = 0;
3268 unsigned OldCount = 0;
3269 unsigned NewCount = 0;
3270 bool FirstRelease = true;
3271 bool FirstRetain = true;
3272 NewRetains.push_back(Retain);
3273 for (;;) {
3274 for (SmallVectorImpl<Instruction *>::const_iterator
3275 NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
3276 Instruction *NewRetain = *NI;
3277 MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain);
3278 assert(It != Retains.end());
3279 const RRInfo &NewRetainRRI = It->second;
Dan Gohmane6d5e882011-08-19 00:26:36 +00003280 KnownSafeTD &= NewRetainRRI.KnownSafe;
John McCall9fbd3182011-06-15 23:37:01 +00003281 for (SmallPtrSet<Instruction *, 2>::const_iterator
3282 LI = NewRetainRRI.Calls.begin(),
3283 LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) {
3284 Instruction *NewRetainRelease = *LI;
3285 DenseMap<Value *, RRInfo>::const_iterator Jt =
3286 Releases.find(NewRetainRelease);
3287 if (Jt == Releases.end())
3288 goto next_retain;
3289 const RRInfo &NewRetainReleaseRRI = Jt->second;
3290 assert(NewRetainReleaseRRI.Calls.count(NewRetain));
3291 if (ReleasesToMove.Calls.insert(NewRetainRelease)) {
3292 OldDelta -=
3293 BBStates[NewRetainRelease->getParent()].GetAllPathCount();
3294
3295 // Merge the ReleaseMetadata and IsTailCallRelease values.
3296 if (FirstRelease) {
3297 ReleasesToMove.ReleaseMetadata =
3298 NewRetainReleaseRRI.ReleaseMetadata;
3299 ReleasesToMove.IsTailCallRelease =
3300 NewRetainReleaseRRI.IsTailCallRelease;
3301 FirstRelease = false;
3302 } else {
3303 if (ReleasesToMove.ReleaseMetadata !=
3304 NewRetainReleaseRRI.ReleaseMetadata)
3305 ReleasesToMove.ReleaseMetadata = 0;
3306 if (ReleasesToMove.IsTailCallRelease !=
3307 NewRetainReleaseRRI.IsTailCallRelease)
3308 ReleasesToMove.IsTailCallRelease = false;
3309 }
3310
3311 // Collect the optimal insertion points.
3312 if (!KnownSafe)
3313 for (SmallPtrSet<Instruction *, 2>::const_iterator
3314 RI = NewRetainReleaseRRI.ReverseInsertPts.begin(),
3315 RE = NewRetainReleaseRRI.ReverseInsertPts.end();
3316 RI != RE; ++RI) {
3317 Instruction *RIP = *RI;
3318 if (ReleasesToMove.ReverseInsertPts.insert(RIP))
3319 NewDelta -= BBStates[RIP->getParent()].GetAllPathCount();
3320 }
3321 NewReleases.push_back(NewRetainRelease);
3322 }
3323 }
3324 }
3325 NewRetains.clear();
3326 if (NewReleases.empty()) break;
3327
3328 // Back the other way.
3329 for (SmallVectorImpl<Instruction *>::const_iterator
3330 NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
3331 Instruction *NewRelease = *NI;
3332 DenseMap<Value *, RRInfo>::const_iterator It =
3333 Releases.find(NewRelease);
3334 assert(It != Releases.end());
3335 const RRInfo &NewReleaseRRI = It->second;
Dan Gohmane6d5e882011-08-19 00:26:36 +00003336 KnownSafeBU &= NewReleaseRRI.KnownSafe;
John McCall9fbd3182011-06-15 23:37:01 +00003337 for (SmallPtrSet<Instruction *, 2>::const_iterator
3338 LI = NewReleaseRRI.Calls.begin(),
3339 LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) {
3340 Instruction *NewReleaseRetain = *LI;
3341 MapVector<Value *, RRInfo>::const_iterator Jt =
3342 Retains.find(NewReleaseRetain);
3343 if (Jt == Retains.end())
3344 goto next_retain;
3345 const RRInfo &NewReleaseRetainRRI = Jt->second;
3346 assert(NewReleaseRetainRRI.Calls.count(NewRelease));
3347 if (RetainsToMove.Calls.insert(NewReleaseRetain)) {
3348 unsigned PathCount =
3349 BBStates[NewReleaseRetain->getParent()].GetAllPathCount();
3350 OldDelta += PathCount;
3351 OldCount += PathCount;
3352
3353 // Merge the IsRetainBlock values.
3354 if (FirstRetain) {
3355 RetainsToMove.IsRetainBlock = NewReleaseRetainRRI.IsRetainBlock;
3356 FirstRetain = false;
3357 } else if (ReleasesToMove.IsRetainBlock !=
3358 NewReleaseRetainRRI.IsRetainBlock)
3359 // It's not possible to merge the sequences if one uses
3360 // objc_retain and the other uses objc_retainBlock.
3361 goto next_retain;
3362
3363 // Collect the optimal insertion points.
3364 if (!KnownSafe)
3365 for (SmallPtrSet<Instruction *, 2>::const_iterator
3366 RI = NewReleaseRetainRRI.ReverseInsertPts.begin(),
3367 RE = NewReleaseRetainRRI.ReverseInsertPts.end();
3368 RI != RE; ++RI) {
3369 Instruction *RIP = *RI;
3370 if (RetainsToMove.ReverseInsertPts.insert(RIP)) {
3371 PathCount = BBStates[RIP->getParent()].GetAllPathCount();
3372 NewDelta += PathCount;
3373 NewCount += PathCount;
3374 }
3375 }
3376 NewRetains.push_back(NewReleaseRetain);
3377 }
3378 }
3379 }
3380 NewReleases.clear();
3381 if (NewRetains.empty()) break;
3382 }
3383
Dan Gohmane6d5e882011-08-19 00:26:36 +00003384 // If the pointer is known incremented or nested, we can safely delete the
3385 // pair regardless of what's between them.
3386 if (KnownSafeTD || KnownSafeBU) {
John McCall9fbd3182011-06-15 23:37:01 +00003387 RetainsToMove.ReverseInsertPts.clear();
3388 ReleasesToMove.ReverseInsertPts.clear();
3389 NewCount = 0;
Dan Gohmana7f7db22011-08-12 00:26:31 +00003390 } else {
3391 // Determine whether the new insertion points we computed preserve the
3392 // balance of retain and release calls through the program.
3393 // TODO: If the fully aggressive solution isn't valid, try to find a
3394 // less aggressive solution which is.
3395 if (NewDelta != 0)
3396 goto next_retain;
John McCall9fbd3182011-06-15 23:37:01 +00003397 }
3398
3399 // Determine whether the original call points are balanced in the retain and
3400 // release calls through the program. If not, conservatively don't touch
3401 // them.
3402 // TODO: It's theoretically possible to do code motion in this case, as
3403 // long as the existing imbalances are maintained.
3404 if (OldDelta != 0)
3405 goto next_retain;
3406
John McCall9fbd3182011-06-15 23:37:01 +00003407 // Ok, everything checks out and we're all set. Let's move some code!
3408 Changed = true;
Dan Gohmaneeeb7752012-04-24 22:53:18 +00003409 assert(OldCount != 0 && "Unreachable code?");
3410 AnyPairsCompletelyEliminated = NewCount == 0;
John McCall9fbd3182011-06-15 23:37:01 +00003411 NumRRs += OldCount - NewCount;
Dan Gohman44280692011-07-22 22:29:21 +00003412 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
3413 Retains, Releases, DeadInsts, M);
John McCall9fbd3182011-06-15 23:37:01 +00003414
3415 next_retain:
3416 NewReleases.clear();
3417 NewRetains.clear();
3418 RetainsToMove.clear();
3419 ReleasesToMove.clear();
3420 }
3421
3422 // Now that we're done moving everything, we can delete the newly dead
3423 // instructions, as we no longer need them as insert points.
3424 while (!DeadInsts.empty())
3425 EraseInstruction(DeadInsts.pop_back_val());
3426
3427 return AnyPairsCompletelyEliminated;
3428}
3429
3430/// OptimizeWeakCalls - Weak pointer optimizations.
3431void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
3432 // First, do memdep-style RLE and S2L optimizations. We can't use memdep
3433 // itself because it uses AliasAnalysis and we need to do provenance
3434 // queries instead.
3435 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
3436 Instruction *Inst = &*I++;
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00003437
Michael Gottesman5c0ae472013-01-04 21:29:57 +00003438 DEBUG(dbgs() << "ObjCARCOpt::OptimizeWeakCalls: Visiting: " << *Inst <<
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00003439 "\n");
3440
John McCall9fbd3182011-06-15 23:37:01 +00003441 InstructionClass Class = GetBasicInstructionClass(Inst);
3442 if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained)
3443 continue;
3444
3445 // Delete objc_loadWeak calls with no users.
3446 if (Class == IC_LoadWeak && Inst->use_empty()) {
3447 Inst->eraseFromParent();
3448 continue;
3449 }
3450
3451 // TODO: For now, just look for an earlier available version of this value
3452 // within the same block. Theoretically, we could do memdep-style non-local
3453 // analysis too, but that would want caching. A better approach would be to
3454 // use the technique that EarlyCSE uses.
3455 inst_iterator Current = llvm::prior(I);
3456 BasicBlock *CurrentBB = Current.getBasicBlockIterator();
3457 for (BasicBlock::iterator B = CurrentBB->begin(),
3458 J = Current.getInstructionIterator();
3459 J != B; --J) {
3460 Instruction *EarlierInst = &*llvm::prior(J);
3461 InstructionClass EarlierClass = GetInstructionClass(EarlierInst);
3462 switch (EarlierClass) {
3463 case IC_LoadWeak:
3464 case IC_LoadWeakRetained: {
3465 // If this is loading from the same pointer, replace this load's value
3466 // with that one.
3467 CallInst *Call = cast<CallInst>(Inst);
3468 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
3469 Value *Arg = Call->getArgOperand(0);
3470 Value *EarlierArg = EarlierCall->getArgOperand(0);
3471 switch (PA.getAA()->alias(Arg, EarlierArg)) {
3472 case AliasAnalysis::MustAlias:
3473 Changed = true;
3474 // If the load has a builtin retain, insert a plain retain for it.
3475 if (Class == IC_LoadWeakRetained) {
3476 CallInst *CI =
3477 CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
3478 "", Call);
3479 CI->setTailCall();
3480 }
3481 // Zap the fully redundant load.
3482 Call->replaceAllUsesWith(EarlierCall);
3483 Call->eraseFromParent();
3484 goto clobbered;
3485 case AliasAnalysis::MayAlias:
3486 case AliasAnalysis::PartialAlias:
3487 goto clobbered;
3488 case AliasAnalysis::NoAlias:
3489 break;
3490 }
3491 break;
3492 }
3493 case IC_StoreWeak:
3494 case IC_InitWeak: {
3495 // If this is storing to the same pointer and has the same size etc.
3496 // replace this load's value with the stored value.
3497 CallInst *Call = cast<CallInst>(Inst);
3498 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
3499 Value *Arg = Call->getArgOperand(0);
3500 Value *EarlierArg = EarlierCall->getArgOperand(0);
3501 switch (PA.getAA()->alias(Arg, EarlierArg)) {
3502 case AliasAnalysis::MustAlias:
3503 Changed = true;
3504 // If the load has a builtin retain, insert a plain retain for it.
3505 if (Class == IC_LoadWeakRetained) {
3506 CallInst *CI =
3507 CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
3508 "", Call);
3509 CI->setTailCall();
3510 }
3511 // Zap the fully redundant load.
3512 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
3513 Call->eraseFromParent();
3514 goto clobbered;
3515 case AliasAnalysis::MayAlias:
3516 case AliasAnalysis::PartialAlias:
3517 goto clobbered;
3518 case AliasAnalysis::NoAlias:
3519 break;
3520 }
3521 break;
3522 }
3523 case IC_MoveWeak:
3524 case IC_CopyWeak:
3525 // TOOD: Grab the copied value.
3526 goto clobbered;
3527 case IC_AutoreleasepoolPush:
3528 case IC_None:
3529 case IC_User:
3530 // Weak pointers are only modified through the weak entry points
3531 // (and arbitrary calls, which could call the weak entry points).
3532 break;
3533 default:
3534 // Anything else could modify the weak pointer.
3535 goto clobbered;
3536 }
3537 }
3538 clobbered:;
3539 }
3540
3541 // Then, for each destroyWeak with an alloca operand, check to see if
3542 // the alloca and all its users can be zapped.
3543 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
3544 Instruction *Inst = &*I++;
3545 InstructionClass Class = GetBasicInstructionClass(Inst);
3546 if (Class != IC_DestroyWeak)
3547 continue;
3548
3549 CallInst *Call = cast<CallInst>(Inst);
3550 Value *Arg = Call->getArgOperand(0);
3551 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
3552 for (Value::use_iterator UI = Alloca->use_begin(),
3553 UE = Alloca->use_end(); UI != UE; ++UI) {
Dan Gohman447989c2012-04-27 18:56:31 +00003554 const Instruction *UserInst = cast<Instruction>(*UI);
John McCall9fbd3182011-06-15 23:37:01 +00003555 switch (GetBasicInstructionClass(UserInst)) {
3556 case IC_InitWeak:
3557 case IC_StoreWeak:
3558 case IC_DestroyWeak:
3559 continue;
3560 default:
3561 goto done;
3562 }
3563 }
3564 Changed = true;
3565 for (Value::use_iterator UI = Alloca->use_begin(),
3566 UE = Alloca->use_end(); UI != UE; ) {
3567 CallInst *UserInst = cast<CallInst>(*UI++);
Dan Gohmance5d8b02012-05-18 22:17:29 +00003568 switch (GetBasicInstructionClass(UserInst)) {
3569 case IC_InitWeak:
3570 case IC_StoreWeak:
3571 // These functions return their second argument.
3572 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
3573 break;
3574 case IC_DestroyWeak:
3575 // No return value.
3576 break;
3577 default:
Dan Gohman4c8f9092012-05-21 17:41:28 +00003578 llvm_unreachable("alloca really is used!");
Dan Gohmance5d8b02012-05-18 22:17:29 +00003579 }
John McCall9fbd3182011-06-15 23:37:01 +00003580 UserInst->eraseFromParent();
3581 }
3582 Alloca->eraseFromParent();
3583 done:;
3584 }
3585 }
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00003586
Michael Gottesman5c0ae472013-01-04 21:29:57 +00003587 DEBUG(dbgs() << "ObjCARCOpt::OptimizeWeakCalls: Finished List.\n\n");
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00003588
John McCall9fbd3182011-06-15 23:37:01 +00003589}
3590
3591/// OptimizeSequences - Identify program paths which execute sequences of
3592/// retains and releases which can be eliminated.
3593bool ObjCARCOpt::OptimizeSequences(Function &F) {
3594 /// Releases, Retains - These are used to store the results of the main flow
3595 /// analysis. These use Value* as the key instead of Instruction* so that the
3596 /// map stays valid when we get around to rewriting code and calls get
3597 /// replaced by arguments.
3598 DenseMap<Value *, RRInfo> Releases;
3599 MapVector<Value *, RRInfo> Retains;
3600
3601 /// BBStates, This is used during the traversal of the function to track the
3602 /// states for each identified object at each block.
3603 DenseMap<const BasicBlock *, BBState> BBStates;
3604
3605 // Analyze the CFG of the function, and all instructions.
3606 bool NestingDetected = Visit(F, BBStates, Retains, Releases);
3607
3608 // Transform.
Dan Gohman44280692011-07-22 22:29:21 +00003609 return PerformCodePlacement(BBStates, Retains, Releases, F.getParent()) &&
3610 NestingDetected;
John McCall9fbd3182011-06-15 23:37:01 +00003611}
3612
3613/// OptimizeReturns - Look for this pattern:
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +00003614/// \code
John McCall9fbd3182011-06-15 23:37:01 +00003615/// %call = call i8* @something(...)
3616/// %2 = call i8* @objc_retain(i8* %call)
3617/// %3 = call i8* @objc_autorelease(i8* %2)
3618/// ret i8* %3
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +00003619/// \endcode
John McCall9fbd3182011-06-15 23:37:01 +00003620/// And delete the retain and autorelease.
3621///
3622/// Otherwise if it's just this:
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +00003623/// \code
John McCall9fbd3182011-06-15 23:37:01 +00003624/// %3 = call i8* @objc_autorelease(i8* %2)
3625/// ret i8* %3
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +00003626/// \endcode
John McCall9fbd3182011-06-15 23:37:01 +00003627/// convert the autorelease to autoreleaseRV.
3628void ObjCARCOpt::OptimizeReturns(Function &F) {
3629 if (!F.getReturnType()->isPointerTy())
3630 return;
3631
3632 SmallPtrSet<Instruction *, 4> DependingInstructions;
3633 SmallPtrSet<const BasicBlock *, 4> Visited;
3634 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
3635 BasicBlock *BB = FI;
3636 ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00003637
Michael Gottesman5c0ae472013-01-04 21:29:57 +00003638 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Visiting: " << *Ret << "\n");
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00003639
John McCall9fbd3182011-06-15 23:37:01 +00003640 if (!Ret) continue;
3641
3642 const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0));
3643 FindDependencies(NeedsPositiveRetainCount, Arg,
3644 BB, Ret, DependingInstructions, Visited, PA);
3645 if (DependingInstructions.size() != 1)
3646 goto next_block;
3647
3648 {
3649 CallInst *Autorelease =
3650 dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3651 if (!Autorelease)
3652 goto next_block;
Dan Gohman0daef3d2012-05-08 23:39:44 +00003653 InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
John McCall9fbd3182011-06-15 23:37:01 +00003654 if (!IsAutorelease(AutoreleaseClass))
3655 goto next_block;
3656 if (GetObjCArg(Autorelease) != Arg)
3657 goto next_block;
3658
3659 DependingInstructions.clear();
3660 Visited.clear();
3661
3662 // Check that there is nothing that can affect the reference
3663 // count between the autorelease and the retain.
3664 FindDependencies(CanChangeRetainCount, Arg,
3665 BB, Autorelease, DependingInstructions, Visited, PA);
3666 if (DependingInstructions.size() != 1)
3667 goto next_block;
3668
3669 {
3670 CallInst *Retain =
3671 dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3672
3673 // Check that we found a retain with the same argument.
3674 if (!Retain ||
3675 !IsRetain(GetBasicInstructionClass(Retain)) ||
3676 GetObjCArg(Retain) != Arg)
3677 goto next_block;
3678
3679 DependingInstructions.clear();
3680 Visited.clear();
3681
3682 // Convert the autorelease to an autoreleaseRV, since it's
3683 // returning the value.
3684 if (AutoreleaseClass == IC_Autorelease) {
3685 Autorelease->setCalledFunction(getAutoreleaseRVCallee(F.getParent()));
3686 AutoreleaseClass = IC_AutoreleaseRV;
3687 }
3688
3689 // Check that there is nothing that can affect the reference
3690 // count between the retain and the call.
Dan Gohman27e06662011-09-29 22:27:34 +00003691 // Note that Retain need not be in BB.
3692 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
John McCall9fbd3182011-06-15 23:37:01 +00003693 DependingInstructions, Visited, PA);
3694 if (DependingInstructions.size() != 1)
3695 goto next_block;
3696
3697 {
3698 CallInst *Call =
3699 dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3700
3701 // Check that the pointer is the return value of the call.
3702 if (!Call || Arg != Call)
3703 goto next_block;
3704
3705 // Check that the call is a regular call.
3706 InstructionClass Class = GetBasicInstructionClass(Call);
3707 if (Class != IC_CallOrUser && Class != IC_Call)
3708 goto next_block;
3709
3710 // If so, we can zap the retain and autorelease.
3711 Changed = true;
3712 ++NumRets;
3713 EraseInstruction(Retain);
3714 EraseInstruction(Autorelease);
3715 }
3716 }
3717 }
3718
3719 next_block:
3720 DependingInstructions.clear();
3721 Visited.clear();
3722 }
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00003723
Michael Gottesman5c0ae472013-01-04 21:29:57 +00003724 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Finished List.\n\n");
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00003725
John McCall9fbd3182011-06-15 23:37:01 +00003726}
3727
3728bool ObjCARCOpt::doInitialization(Module &M) {
3729 if (!EnableARCOpts)
3730 return false;
3731
Dan Gohmand6bf2012012-04-13 18:57:48 +00003732 // If nothing in the Module uses ARC, don't do anything.
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +00003733 Run = ModuleHasARC(M);
3734 if (!Run)
3735 return false;
3736
John McCall9fbd3182011-06-15 23:37:01 +00003737 // Identify the imprecise release metadata kind.
3738 ImpreciseReleaseMDKind =
3739 M.getContext().getMDKindID("clang.imprecise_release");
Dan Gohmana974bea2011-10-17 22:53:25 +00003740 CopyOnEscapeMDKind =
3741 M.getContext().getMDKindID("clang.arc.copy_on_escape");
Dan Gohmandbe266b2012-02-17 18:59:53 +00003742 NoObjCARCExceptionsMDKind =
3743 M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
John McCall9fbd3182011-06-15 23:37:01 +00003744
John McCall9fbd3182011-06-15 23:37:01 +00003745 // Intuitively, objc_retain and others are nocapture, however in practice
3746 // they are not, because they return their argument value. And objc_release
Dan Gohman447989c2012-04-27 18:56:31 +00003747 // calls finalizers which can have arbitrary side effects.
John McCall9fbd3182011-06-15 23:37:01 +00003748
3749 // These are initialized lazily.
3750 RetainRVCallee = 0;
3751 AutoreleaseRVCallee = 0;
3752 ReleaseCallee = 0;
3753 RetainCallee = 0;
Dan Gohman44280692011-07-22 22:29:21 +00003754 RetainBlockCallee = 0;
John McCall9fbd3182011-06-15 23:37:01 +00003755 AutoreleaseCallee = 0;
3756
3757 return false;
3758}
3759
3760bool ObjCARCOpt::runOnFunction(Function &F) {
3761 if (!EnableARCOpts)
3762 return false;
3763
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +00003764 // If nothing in the Module uses ARC, don't do anything.
3765 if (!Run)
3766 return false;
3767
John McCall9fbd3182011-06-15 23:37:01 +00003768 Changed = false;
3769
3770 PA.setAA(&getAnalysis<AliasAnalysis>());
3771
3772 // This pass performs several distinct transformations. As a compile-time aid
3773 // when compiling code that isn't ObjC, skip these if the relevant ObjC
3774 // library functions aren't declared.
3775
3776 // Preliminary optimizations. This also computs UsedInThisFunction.
3777 OptimizeIndividualCalls(F);
3778
3779 // Optimizations for weak pointers.
3780 if (UsedInThisFunction & ((1 << IC_LoadWeak) |
3781 (1 << IC_LoadWeakRetained) |
3782 (1 << IC_StoreWeak) |
3783 (1 << IC_InitWeak) |
3784 (1 << IC_CopyWeak) |
3785 (1 << IC_MoveWeak) |
3786 (1 << IC_DestroyWeak)))
3787 OptimizeWeakCalls(F);
3788
3789 // Optimizations for retain+release pairs.
3790 if (UsedInThisFunction & ((1 << IC_Retain) |
3791 (1 << IC_RetainRV) |
3792 (1 << IC_RetainBlock)))
3793 if (UsedInThisFunction & (1 << IC_Release))
3794 // Run OptimizeSequences until it either stops making changes or
3795 // no retain+release pair nesting is detected.
3796 while (OptimizeSequences(F)) {}
3797
3798 // Optimizations if objc_autorelease is used.
Dan Gohman0daef3d2012-05-08 23:39:44 +00003799 if (UsedInThisFunction & ((1 << IC_Autorelease) |
3800 (1 << IC_AutoreleaseRV)))
John McCall9fbd3182011-06-15 23:37:01 +00003801 OptimizeReturns(F);
3802
3803 return Changed;
3804}
3805
3806void ObjCARCOpt::releaseMemory() {
3807 PA.clear();
3808}
3809
3810//===----------------------------------------------------------------------===//
3811// ARC contraction.
3812//===----------------------------------------------------------------------===//
3813
3814// TODO: ObjCARCContract could insert PHI nodes when uses aren't
3815// dominated by single calls.
3816
John McCall9fbd3182011-06-15 23:37:01 +00003817#include "llvm/Analysis/Dominators.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +00003818#include "llvm/IR/InlineAsm.h"
3819#include "llvm/IR/Operator.h"
John McCall9fbd3182011-06-15 23:37:01 +00003820
3821STATISTIC(NumStoreStrongs, "Number objc_storeStrong calls formed");
3822
3823namespace {
3824 /// ObjCARCContract - Late ARC optimizations. These change the IR in a way
3825 /// that makes it difficult to be analyzed by ObjCARCOpt, so it's run late.
3826 class ObjCARCContract : public FunctionPass {
3827 bool Changed;
3828 AliasAnalysis *AA;
3829 DominatorTree *DT;
3830 ProvenanceAnalysis PA;
3831
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +00003832 /// Run - A flag indicating whether this optimization pass should run.
3833 bool Run;
3834
John McCall9fbd3182011-06-15 23:37:01 +00003835 /// StoreStrongCallee, etc. - Declarations for ObjC runtime
3836 /// functions, for use in creating calls to them. These are initialized
3837 /// lazily to avoid cluttering up the Module with unused declarations.
3838 Constant *StoreStrongCallee,
3839 *RetainAutoreleaseCallee, *RetainAutoreleaseRVCallee;
3840
3841 /// RetainRVMarker - The inline asm string to insert between calls and
3842 /// RetainRV calls to make the optimization work on targets which need it.
3843 const MDString *RetainRVMarker;
3844
Dan Gohman0cdece42012-01-19 19:14:36 +00003845 /// StoreStrongCalls - The set of inserted objc_storeStrong calls. If
3846 /// at the end of walking the function we have found no alloca
3847 /// instructions, these calls can be marked "tail".
Dan Gohman0daef3d2012-05-08 23:39:44 +00003848 SmallPtrSet<CallInst *, 8> StoreStrongCalls;
Dan Gohman0cdece42012-01-19 19:14:36 +00003849
John McCall9fbd3182011-06-15 23:37:01 +00003850 Constant *getStoreStrongCallee(Module *M);
3851 Constant *getRetainAutoreleaseCallee(Module *M);
3852 Constant *getRetainAutoreleaseRVCallee(Module *M);
3853
3854 bool ContractAutorelease(Function &F, Instruction *Autorelease,
3855 InstructionClass Class,
3856 SmallPtrSet<Instruction *, 4>
3857 &DependingInstructions,
3858 SmallPtrSet<const BasicBlock *, 4>
3859 &Visited);
3860
3861 void ContractRelease(Instruction *Release,
3862 inst_iterator &Iter);
3863
3864 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
3865 virtual bool doInitialization(Module &M);
3866 virtual bool runOnFunction(Function &F);
3867
3868 public:
3869 static char ID;
3870 ObjCARCContract() : FunctionPass(ID) {
3871 initializeObjCARCContractPass(*PassRegistry::getPassRegistry());
3872 }
3873 };
3874}
3875
3876char ObjCARCContract::ID = 0;
3877INITIALIZE_PASS_BEGIN(ObjCARCContract,
3878 "objc-arc-contract", "ObjC ARC contraction", false, false)
3879INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
3880INITIALIZE_PASS_DEPENDENCY(DominatorTree)
3881INITIALIZE_PASS_END(ObjCARCContract,
3882 "objc-arc-contract", "ObjC ARC contraction", false, false)
3883
3884Pass *llvm::createObjCARCContractPass() {
3885 return new ObjCARCContract();
3886}
3887
3888void ObjCARCContract::getAnalysisUsage(AnalysisUsage &AU) const {
3889 AU.addRequired<AliasAnalysis>();
3890 AU.addRequired<DominatorTree>();
3891 AU.setPreservesCFG();
3892}
3893
3894Constant *ObjCARCContract::getStoreStrongCallee(Module *M) {
3895 if (!StoreStrongCallee) {
3896 LLVMContext &C = M->getContext();
Jay Foad5fdd6c82011-07-12 14:06:48 +00003897 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
3898 Type *I8XX = PointerType::getUnqual(I8X);
Dan Gohman0daef3d2012-05-08 23:39:44 +00003899 Type *Params[] = { I8XX, I8X };
John McCall9fbd3182011-06-15 23:37:01 +00003900
Bill Wendling034b94b2012-12-19 07:18:57 +00003901 AttributeSet Attribute = AttributeSet()
Bill Wendling99faa3b2012-12-07 23:16:57 +00003902 .addAttr(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00003903 Attribute::get(C, Attribute::NoUnwind))
3904 .addAttr(M->getContext(), 1, Attribute::get(C, Attribute::NoCapture));
John McCall9fbd3182011-06-15 23:37:01 +00003905
3906 StoreStrongCallee =
3907 M->getOrInsertFunction(
3908 "objc_storeStrong",
3909 FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
Bill Wendling034b94b2012-12-19 07:18:57 +00003910 Attribute);
John McCall9fbd3182011-06-15 23:37:01 +00003911 }
3912 return StoreStrongCallee;
3913}
3914
3915Constant *ObjCARCContract::getRetainAutoreleaseCallee(Module *M) {
3916 if (!RetainAutoreleaseCallee) {
3917 LLVMContext &C = M->getContext();
Jay Foad5fdd6c82011-07-12 14:06:48 +00003918 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
Dan Gohman0daef3d2012-05-08 23:39:44 +00003919 Type *Params[] = { I8X };
3920 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
Bill Wendling034b94b2012-12-19 07:18:57 +00003921 AttributeSet Attribute =
Bill Wendling99faa3b2012-12-07 23:16:57 +00003922 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00003923 Attribute::get(C, Attribute::NoUnwind));
John McCall9fbd3182011-06-15 23:37:01 +00003924 RetainAutoreleaseCallee =
Bill Wendling034b94b2012-12-19 07:18:57 +00003925 M->getOrInsertFunction("objc_retainAutorelease", FTy, Attribute);
John McCall9fbd3182011-06-15 23:37:01 +00003926 }
3927 return RetainAutoreleaseCallee;
3928}
3929
3930Constant *ObjCARCContract::getRetainAutoreleaseRVCallee(Module *M) {
3931 if (!RetainAutoreleaseRVCallee) {
3932 LLVMContext &C = M->getContext();
Jay Foad5fdd6c82011-07-12 14:06:48 +00003933 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
Dan Gohman0daef3d2012-05-08 23:39:44 +00003934 Type *Params[] = { I8X };
3935 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
Bill Wendling034b94b2012-12-19 07:18:57 +00003936 AttributeSet Attribute =
Bill Wendling99faa3b2012-12-07 23:16:57 +00003937 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +00003938 Attribute::get(C, Attribute::NoUnwind));
John McCall9fbd3182011-06-15 23:37:01 +00003939 RetainAutoreleaseRVCallee =
3940 M->getOrInsertFunction("objc_retainAutoreleaseReturnValue", FTy,
Bill Wendling034b94b2012-12-19 07:18:57 +00003941 Attribute);
John McCall9fbd3182011-06-15 23:37:01 +00003942 }
3943 return RetainAutoreleaseRVCallee;
3944}
3945
Dan Gohman447989c2012-04-27 18:56:31 +00003946/// ContractAutorelease - Merge an autorelease with a retain into a fused call.
John McCall9fbd3182011-06-15 23:37:01 +00003947bool
3948ObjCARCContract::ContractAutorelease(Function &F, Instruction *Autorelease,
3949 InstructionClass Class,
3950 SmallPtrSet<Instruction *, 4>
3951 &DependingInstructions,
3952 SmallPtrSet<const BasicBlock *, 4>
3953 &Visited) {
3954 const Value *Arg = GetObjCArg(Autorelease);
3955
3956 // Check that there are no instructions between the retain and the autorelease
3957 // (such as an autorelease_pop) which may change the count.
3958 CallInst *Retain = 0;
3959 if (Class == IC_AutoreleaseRV)
3960 FindDependencies(RetainAutoreleaseRVDep, Arg,
3961 Autorelease->getParent(), Autorelease,
3962 DependingInstructions, Visited, PA);
3963 else
3964 FindDependencies(RetainAutoreleaseDep, Arg,
3965 Autorelease->getParent(), Autorelease,
3966 DependingInstructions, Visited, PA);
3967
3968 Visited.clear();
3969 if (DependingInstructions.size() != 1) {
3970 DependingInstructions.clear();
3971 return false;
3972 }
3973
3974 Retain = dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3975 DependingInstructions.clear();
3976
3977 if (!Retain ||
3978 GetBasicInstructionClass(Retain) != IC_Retain ||
3979 GetObjCArg(Retain) != Arg)
3980 return false;
3981
3982 Changed = true;
3983 ++NumPeeps;
3984
3985 if (Class == IC_AutoreleaseRV)
3986 Retain->setCalledFunction(getRetainAutoreleaseRVCallee(F.getParent()));
3987 else
3988 Retain->setCalledFunction(getRetainAutoreleaseCallee(F.getParent()));
3989
3990 EraseInstruction(Autorelease);
3991 return true;
3992}
3993
3994/// ContractRelease - Attempt to merge an objc_release with a store, load, and
3995/// objc_retain to form an objc_storeStrong. This can be a little tricky because
3996/// the instructions don't always appear in order, and there may be unrelated
3997/// intervening instructions.
3998void ObjCARCContract::ContractRelease(Instruction *Release,
3999 inst_iterator &Iter) {
4000 LoadInst *Load = dyn_cast<LoadInst>(GetObjCArg(Release));
Eli Friedman2bc3d522011-09-12 20:23:13 +00004001 if (!Load || !Load->isSimple()) return;
John McCall9fbd3182011-06-15 23:37:01 +00004002
4003 // For now, require everything to be in one basic block.
4004 BasicBlock *BB = Release->getParent();
4005 if (Load->getParent() != BB) return;
4006
Dan Gohman4670dac2012-05-08 23:34:08 +00004007 // Walk down to find the store and the release, which may be in either order.
Dan Gohman95b8cf12012-05-09 23:08:33 +00004008 BasicBlock::iterator I = Load, End = BB->end();
John McCall9fbd3182011-06-15 23:37:01 +00004009 ++I;
4010 AliasAnalysis::Location Loc = AA->getLocation(Load);
Dan Gohman4670dac2012-05-08 23:34:08 +00004011 StoreInst *Store = 0;
4012 bool SawRelease = false;
4013 for (; !Store || !SawRelease; ++I) {
Dan Gohman95b8cf12012-05-09 23:08:33 +00004014 if (I == End)
4015 return;
4016
Dan Gohman4670dac2012-05-08 23:34:08 +00004017 Instruction *Inst = I;
4018 if (Inst == Release) {
4019 SawRelease = true;
4020 continue;
4021 }
4022
4023 InstructionClass Class = GetBasicInstructionClass(Inst);
4024
4025 // Unrelated retains are harmless.
4026 if (IsRetain(Class))
4027 continue;
4028
4029 if (Store) {
4030 // The store is the point where we're going to put the objc_storeStrong,
4031 // so make sure there are no uses after it.
4032 if (CanUse(Inst, Load, PA, Class))
4033 return;
4034 } else if (AA->getModRefInfo(Inst, Loc) & AliasAnalysis::Mod) {
4035 // We are moving the load down to the store, so check for anything
4036 // else which writes to the memory between the load and the store.
4037 Store = dyn_cast<StoreInst>(Inst);
4038 if (!Store || !Store->isSimple()) return;
4039 if (Store->getPointerOperand() != Loc.Ptr) return;
4040 }
4041 }
John McCall9fbd3182011-06-15 23:37:01 +00004042
4043 Value *New = StripPointerCastsAndObjCCalls(Store->getValueOperand());
4044
4045 // Walk up to find the retain.
4046 I = Store;
4047 BasicBlock::iterator Begin = BB->begin();
4048 while (I != Begin && GetBasicInstructionClass(I) != IC_Retain)
4049 --I;
4050 Instruction *Retain = I;
4051 if (GetBasicInstructionClass(Retain) != IC_Retain) return;
4052 if (GetObjCArg(Retain) != New) return;
4053
4054 Changed = true;
4055 ++NumStoreStrongs;
4056
4057 LLVMContext &C = Release->getContext();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004058 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
4059 Type *I8XX = PointerType::getUnqual(I8X);
John McCall9fbd3182011-06-15 23:37:01 +00004060
4061 Value *Args[] = { Load->getPointerOperand(), New };
4062 if (Args[0]->getType() != I8XX)
4063 Args[0] = new BitCastInst(Args[0], I8XX, "", Store);
4064 if (Args[1]->getType() != I8X)
4065 Args[1] = new BitCastInst(Args[1], I8X, "", Store);
4066 CallInst *StoreStrong =
4067 CallInst::Create(getStoreStrongCallee(BB->getParent()->getParent()),
Jay Foada3efbb12011-07-15 08:37:34 +00004068 Args, "", Store);
John McCall9fbd3182011-06-15 23:37:01 +00004069 StoreStrong->setDoesNotThrow();
4070 StoreStrong->setDebugLoc(Store->getDebugLoc());
4071
Dan Gohman0cdece42012-01-19 19:14:36 +00004072 // We can't set the tail flag yet, because we haven't yet determined
4073 // whether there are any escaping allocas. Remember this call, so that
4074 // we can set the tail flag once we know it's safe.
4075 StoreStrongCalls.insert(StoreStrong);
4076
John McCall9fbd3182011-06-15 23:37:01 +00004077 if (&*Iter == Store) ++Iter;
4078 Store->eraseFromParent();
4079 Release->eraseFromParent();
4080 EraseInstruction(Retain);
4081 if (Load->use_empty())
4082 Load->eraseFromParent();
4083}
4084
4085bool ObjCARCContract::doInitialization(Module &M) {
Dan Gohmand6bf2012012-04-13 18:57:48 +00004086 // If nothing in the Module uses ARC, don't do anything.
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +00004087 Run = ModuleHasARC(M);
4088 if (!Run)
4089 return false;
4090
John McCall9fbd3182011-06-15 23:37:01 +00004091 // These are initialized lazily.
4092 StoreStrongCallee = 0;
4093 RetainAutoreleaseCallee = 0;
4094 RetainAutoreleaseRVCallee = 0;
4095
4096 // Initialize RetainRVMarker.
4097 RetainRVMarker = 0;
4098 if (NamedMDNode *NMD =
4099 M.getNamedMetadata("clang.arc.retainAutoreleasedReturnValueMarker"))
4100 if (NMD->getNumOperands() == 1) {
4101 const MDNode *N = NMD->getOperand(0);
4102 if (N->getNumOperands() == 1)
4103 if (const MDString *S = dyn_cast<MDString>(N->getOperand(0)))
4104 RetainRVMarker = S;
4105 }
4106
4107 return false;
4108}
4109
4110bool ObjCARCContract::runOnFunction(Function &F) {
4111 if (!EnableARCOpts)
4112 return false;
4113
Dan Gohmanc4bcd4d2011-06-20 23:20:43 +00004114 // If nothing in the Module uses ARC, don't do anything.
4115 if (!Run)
4116 return false;
4117
John McCall9fbd3182011-06-15 23:37:01 +00004118 Changed = false;
4119 AA = &getAnalysis<AliasAnalysis>();
4120 DT = &getAnalysis<DominatorTree>();
4121
4122 PA.setAA(&getAnalysis<AliasAnalysis>());
4123
Dan Gohman0cdece42012-01-19 19:14:36 +00004124 // Track whether it's ok to mark objc_storeStrong calls with the "tail"
4125 // keyword. Be conservative if the function has variadic arguments.
4126 // It seems that functions which "return twice" are also unsafe for the
4127 // "tail" argument, because they are setjmp, which could need to
4128 // return to an earlier stack state.
Dan Gohman0daef3d2012-05-08 23:39:44 +00004129 bool TailOkForStoreStrongs = !F.isVarArg() &&
4130 !F.callsFunctionThatReturnsTwice();
Dan Gohman0cdece42012-01-19 19:14:36 +00004131
John McCall9fbd3182011-06-15 23:37:01 +00004132 // For ObjC library calls which return their argument, replace uses of the
4133 // argument with uses of the call return value, if it dominates the use. This
4134 // reduces register pressure.
4135 SmallPtrSet<Instruction *, 4> DependingInstructions;
4136 SmallPtrSet<const BasicBlock *, 4> Visited;
4137 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
4138 Instruction *Inst = &*I++;
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00004139
4140 DEBUG(dbgs() << "ObjCARCContract: Visiting: " << *Inst << "\n");
4141
John McCall9fbd3182011-06-15 23:37:01 +00004142 // Only these library routines return their argument. In particular,
4143 // objc_retainBlock does not necessarily return its argument.
4144 InstructionClass Class = GetBasicInstructionClass(Inst);
4145 switch (Class) {
4146 case IC_Retain:
4147 case IC_FusedRetainAutorelease:
4148 case IC_FusedRetainAutoreleaseRV:
4149 break;
4150 case IC_Autorelease:
4151 case IC_AutoreleaseRV:
4152 if (ContractAutorelease(F, Inst, Class, DependingInstructions, Visited))
4153 continue;
4154 break;
4155 case IC_RetainRV: {
4156 // If we're compiling for a target which needs a special inline-asm
4157 // marker to do the retainAutoreleasedReturnValue optimization,
4158 // insert it now.
4159 if (!RetainRVMarker)
4160 break;
4161 BasicBlock::iterator BBI = Inst;
Dan Gohman58fb3402012-06-25 19:47:37 +00004162 BasicBlock *InstParent = Inst->getParent();
4163
4164 // Step up to see if the call immediately precedes the RetainRV call.
4165 // If it's an invoke, we have to cross a block boundary. And we have
4166 // to carefully dodge no-op instructions.
4167 do {
4168 if (&*BBI == InstParent->begin()) {
4169 BasicBlock *Pred = InstParent->getSinglePredecessor();
4170 if (!Pred)
4171 goto decline_rv_optimization;
4172 BBI = Pred->getTerminator();
4173 break;
4174 }
4175 --BBI;
4176 } while (isNoopInstruction(BBI));
4177
John McCall9fbd3182011-06-15 23:37:01 +00004178 if (&*BBI == GetObjCArg(Inst)) {
Michael Gottesman50652cd2013-01-03 07:32:41 +00004179 DEBUG(dbgs() << "ObjCARCContract: Adding inline asm marker for "
Michael Gottesman5c0ae472013-01-04 21:29:57 +00004180 "retainAutoreleasedReturnValue optimization.\n");
Dan Gohmand6bf2012012-04-13 18:57:48 +00004181 Changed = true;
John McCall9fbd3182011-06-15 23:37:01 +00004182 InlineAsm *IA =
4183 InlineAsm::get(FunctionType::get(Type::getVoidTy(Inst->getContext()),
4184 /*isVarArg=*/false),
4185 RetainRVMarker->getString(),
4186 /*Constraints=*/"", /*hasSideEffects=*/true);
4187 CallInst::Create(IA, "", Inst);
4188 }
Dan Gohman58fb3402012-06-25 19:47:37 +00004189 decline_rv_optimization:
John McCall9fbd3182011-06-15 23:37:01 +00004190 break;
4191 }
4192 case IC_InitWeak: {
4193 // objc_initWeak(p, null) => *p = null
4194 CallInst *CI = cast<CallInst>(Inst);
4195 if (isNullOrUndef(CI->getArgOperand(1))) {
4196 Value *Null =
4197 ConstantPointerNull::get(cast<PointerType>(CI->getType()));
4198 Changed = true;
4199 new StoreInst(Null, CI->getArgOperand(0), CI);
Michael Gottesman1ebbdcf2013-01-03 07:32:53 +00004200
4201 DEBUG(dbgs() << "OBJCARCContract: Old = " << *CI << "\n"
4202 << " New = " << *Null << "\n");
4203
John McCall9fbd3182011-06-15 23:37:01 +00004204 CI->replaceAllUsesWith(Null);
4205 CI->eraseFromParent();
4206 }
4207 continue;
4208 }
4209 case IC_Release:
4210 ContractRelease(Inst, I);
4211 continue;
Dan Gohman0cdece42012-01-19 19:14:36 +00004212 case IC_User:
4213 // Be conservative if the function has any alloca instructions.
4214 // Technically we only care about escaping alloca instructions,
4215 // but this is sufficient to handle some interesting cases.
4216 if (isa<AllocaInst>(Inst))
4217 TailOkForStoreStrongs = false;
4218 continue;
John McCall9fbd3182011-06-15 23:37:01 +00004219 default:
4220 continue;
4221 }
4222
Michael Gottesmanec21e2a2013-01-03 08:09:27 +00004223 DEBUG(dbgs() << "ObjCARCContract: Finished List.\n\n");
Michael Gottesman8f22c8b2013-01-01 16:05:48 +00004224
John McCall9fbd3182011-06-15 23:37:01 +00004225 // Don't use GetObjCArg because we don't want to look through bitcasts
4226 // and such; to do the replacement, the argument must have type i8*.
4227 const Value *Arg = cast<CallInst>(Inst)->getArgOperand(0);
4228 for (;;) {
4229 // If we're compiling bugpointed code, don't get in trouble.
4230 if (!isa<Instruction>(Arg) && !isa<Argument>(Arg))
4231 break;
4232 // Look through the uses of the pointer.
4233 for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
4234 UI != UE; ) {
4235 Use &U = UI.getUse();
4236 unsigned OperandNo = UI.getOperandNo();
4237 ++UI; // Increment UI now, because we may unlink its element.
Dan Gohmand6bf2012012-04-13 18:57:48 +00004238
4239 // If the call's return value dominates a use of the call's argument
4240 // value, rewrite the use to use the return value. We check for
4241 // reachability here because an unreachable call is considered to
4242 // trivially dominate itself, which would lead us to rewriting its
4243 // argument in terms of its return value, which would lead to
4244 // infinite loops in GetObjCArg.
Dan Gohman0daef3d2012-05-08 23:39:44 +00004245 if (DT->isReachableFromEntry(U) && DT->dominates(Inst, U)) {
Rafael Espindola2453dff2012-03-15 15:52:59 +00004246 Changed = true;
4247 Instruction *Replacement = Inst;
4248 Type *UseTy = U.get()->getType();
Dan Gohman6c189ec2012-04-13 01:08:28 +00004249 if (PHINode *PHI = dyn_cast<PHINode>(U.getUser())) {
Rafael Espindola2453dff2012-03-15 15:52:59 +00004250 // For PHI nodes, insert the bitcast in the predecessor block.
Dan Gohman0daef3d2012-05-08 23:39:44 +00004251 unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo);
4252 BasicBlock *BB = PHI->getIncomingBlock(ValNo);
Rafael Espindola2453dff2012-03-15 15:52:59 +00004253 if (Replacement->getType() != UseTy)
4254 Replacement = new BitCastInst(Replacement, UseTy, "",
4255 &BB->back());
Dan Gohmand6bf2012012-04-13 18:57:48 +00004256 // While we're here, rewrite all edges for this PHI, rather
4257 // than just one use at a time, to minimize the number of
4258 // bitcasts we emit.
Dan Gohman447989c2012-04-27 18:56:31 +00004259 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
Rafael Espindola2453dff2012-03-15 15:52:59 +00004260 if (PHI->getIncomingBlock(i) == BB) {
4261 // Keep the UI iterator valid.
4262 if (&PHI->getOperandUse(
4263 PHINode::getOperandNumForIncomingValue(i)) ==
4264 &UI.getUse())
4265 ++UI;
4266 PHI->setIncomingValue(i, Replacement);
4267 }
4268 } else {
4269 if (Replacement->getType() != UseTy)
Dan Gohman6c189ec2012-04-13 01:08:28 +00004270 Replacement = new BitCastInst(Replacement, UseTy, "",
4271 cast<Instruction>(U.getUser()));
Rafael Espindola2453dff2012-03-15 15:52:59 +00004272 U.set(Replacement);
John McCall9fbd3182011-06-15 23:37:01 +00004273 }
Rafael Espindola2453dff2012-03-15 15:52:59 +00004274 }
John McCall9fbd3182011-06-15 23:37:01 +00004275 }
4276
Dan Gohman447989c2012-04-27 18:56:31 +00004277 // If Arg is a no-op casted pointer, strip one level of casts and iterate.
John McCall9fbd3182011-06-15 23:37:01 +00004278 if (const BitCastInst *BI = dyn_cast<BitCastInst>(Arg))
4279 Arg = BI->getOperand(0);
4280 else if (isa<GEPOperator>(Arg) &&
4281 cast<GEPOperator>(Arg)->hasAllZeroIndices())
4282 Arg = cast<GEPOperator>(Arg)->getPointerOperand();
4283 else if (isa<GlobalAlias>(Arg) &&
4284 !cast<GlobalAlias>(Arg)->mayBeOverridden())
4285 Arg = cast<GlobalAlias>(Arg)->getAliasee();
4286 else
4287 break;
4288 }
4289 }
4290
Dan Gohman0cdece42012-01-19 19:14:36 +00004291 // If this function has no escaping allocas or suspicious vararg usage,
4292 // objc_storeStrong calls can be marked with the "tail" keyword.
4293 if (TailOkForStoreStrongs)
Dan Gohman0daef3d2012-05-08 23:39:44 +00004294 for (SmallPtrSet<CallInst *, 8>::iterator I = StoreStrongCalls.begin(),
Dan Gohman0cdece42012-01-19 19:14:36 +00004295 E = StoreStrongCalls.end(); I != E; ++I)
4296 (*I)->setTailCall();
4297 StoreStrongCalls.clear();
4298
John McCall9fbd3182011-06-15 23:37:01 +00004299 return Changed;
4300}