1 //===- ObjCARC.cpp - ObjC ARC Optimization --------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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
20 // This file also defines a simple ARC-aware AliasAnalysis.
22 // WARNING: This file knows about certain library functions. It recognizes them
23 // by name, and hardwires knowledge of their semantics.
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.
29 //===----------------------------------------------------------------------===//
31 #define DEBUG_TYPE "objc-arc"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/raw_ostream.h"
38 // A handy option to enable/disable all optimizations in this file.
39 static cl::opt<bool> EnableARCOpts("enable-objc-arc-opts", cl::init(true));
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
46 /// MapVector - An associative container with fast insertion-order
47 /// (deterministic) iteration over its elements. Plus the special
49 template<class KeyT, class ValueT>
51 /// Map - Map keys to indices in Vector.
52 typedef DenseMap<KeyT, size_t> MapTy;
55 /// Vector - Keys and values.
56 typedef std::vector<std::pair<KeyT, ValueT> > VectorTy;
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(); }
69 assert(Vector.size() >= Map.size()); // May differ due to blotting.
70 for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
72 assert(I->second < Vector.size());
73 assert(Vector[I->second].first == I->first);
75 for (typename VectorTy::const_iterator I = Vector.begin(),
76 E = Vector.end(); I != E; ++I)
78 (Map.count(I->first) &&
79 Map[I->first] == size_t(I - Vector.begin())));
83 ValueT &operator[](const KeyT &Arg) {
84 std::pair<typename MapTy::iterator, bool> Pair =
85 Map.insert(std::make_pair(Arg, size_t(0)));
87 size_t Num = Vector.size();
88 Pair.first->second = Num;
89 Vector.push_back(std::make_pair(Arg, ValueT()));
90 return Vector[Num].second;
92 return Vector[Pair.first->second].second;
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)));
100 size_t Num = Vector.size();
101 Pair.first->second = Num;
102 Vector.push_back(InsertPair);
103 return std::make_pair(Vector.begin() + Num, true);
105 return std::make_pair(Vector.begin() + Pair.first->second, false);
108 const_iterator find(const KeyT &Key) const {
109 typename MapTy::const_iterator It = Map.find(Key);
110 if (It == Map.end()) return Vector.end();
111 return Vector.begin() + It->second;
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
118 void blot(const KeyT &Key) {
119 typename MapTy::iterator It = Map.find(Key);
120 if (It == Map.end()) return;
121 Vector[It->second].first = KeyT();
132 //===----------------------------------------------------------------------===//
134 //===----------------------------------------------------------------------===//
136 #include "llvm/ADT/StringSwitch.h"
137 #include "llvm/Analysis/ValueTracking.h"
138 #include "llvm/IR/Intrinsics.h"
139 #include "llvm/IR/Module.h"
140 #include "llvm/Support/CallSite.h"
141 #include "llvm/Transforms/Utils/Local.h"
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)
164 IC_StoreStrong, ///< objc_storeStrong (derived)
165 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
172 /// IsPotentialUse - Test whether the given value is possible a
173 /// reference-counted pointer.
174 static 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))
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())
184 // 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
189 PointerType *Ty = dyn_cast<PointerType>(Op->getType());
192 // Conservatively assume anything else is a potential use.
196 /// GetCallSiteClass - Helper for GetInstructionClass. Determines what kind
197 /// of construct CS is.
198 static InstructionClass GetCallSiteClass(ImmutableCallSite CS) {
199 for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
201 if (IsPotentialUse(*I))
202 return CS.onlyReadsMemory() ? IC_User : IC_CallOrUser;
204 return CS.onlyReadsMemory() ? IC_None : IC_Call;
207 /// GetFunctionClass - Determine if F is one of the special known Functions.
208 /// If it isn't, return IC_CallOrUser.
209 static InstructionClass GetFunctionClass(const Function *F) {
210 Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
214 return StringSwitch<InstructionClass>(F->getName())
215 .Case("objc_autoreleasePoolPush", IC_AutoreleasepoolPush)
216 .Default(IC_CallOrUser);
219 const Argument *A0 = AI++;
221 // Argument is a pointer.
222 if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) {
223 Type *ETy = PTy->getElementType();
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);
243 if (PointerType *Pte = dyn_cast<PointerType>(ETy))
244 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);
252 // Two arguments, first is i8**.
253 const Argument *A1 = AI++;
255 if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
256 if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
257 if (Pte->getElementType()->isIntegerTy(8))
258 if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
259 Type *ETy1 = PTy1->getElementType();
260 // 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)
265 .Case("objc_storeStrong", IC_StoreStrong)
266 .Default(IC_CallOrUser);
267 // Second argument is i8**.
268 if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
269 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);
277 return IC_CallOrUser;
280 /// GetInstructionClass - Determine what kind of construct V is.
281 static 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)
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()) {
301 case Intrinsic::returnaddress: case Intrinsic::frameaddress:
302 case Intrinsic::stacksave: case Intrinsic::stackrestore:
303 case Intrinsic::vastart: case Intrinsic::vacopy: case Intrinsic::vaend:
304 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:
312 // 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.
320 return GetCallSiteClass(CI);
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:
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)))
354 // For anything else, check all the operands.
355 // 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.
359 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
361 if (IsPotentialUse(*OI))
366 // Otherwise, it's totally inert for ARC purposes.
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.
373 static 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;
381 // Otherwise, be conservative.
382 return isa<InvokeInst>(V) ? IC_CallOrUser : IC_User;
385 /// IsRetain - Test if the given class is objc_retain or
387 static bool IsRetain(InstructionClass Class) {
388 return Class == IC_Retain ||
389 Class == IC_RetainRV;
392 /// IsAutorelease - Test if the given class is objc_autorelease or
394 static bool IsAutorelease(InstructionClass Class) {
395 return Class == IC_Autorelease ||
396 Class == IC_AutoreleaseRV;
399 /// IsForwarding - Test if the given class represents instructions which return
400 /// their argument verbatim.
401 static 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;
412 /// IsNoopOnNull - Test if the given class represents instructions which do
413 /// nothing if passed a null pointer.
414 static 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;
423 /// IsAlwaysTail - Test if the given class represents instructions which are
424 /// always safe to mark with the "tail" keyword.
425 static 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;
433 /// IsNoThrow - Test if the given class represents instructions which are always
434 /// safe to mark with the nounwind attribute..
435 static bool IsNoThrow(InstructionClass Class) {
436 // objc_retainBlock is not nounwind because it calls user copy constructors
437 // which could theoretically throw.
438 return Class == IC_Retain ||
439 Class == IC_RetainRV ||
440 Class == IC_Release ||
441 Class == IC_Autorelease ||
442 Class == IC_AutoreleaseRV ||
443 Class == IC_AutoreleasepoolPush ||
444 Class == IC_AutoreleasepoolPop;
447 /// EraseInstruction - Erase the given instruction. Many ObjC calls return their
448 /// argument verbatim, so if it's such a call and the return value has users,
449 /// replace them with the argument value.
450 static void EraseInstruction(Instruction *CI) {
451 Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
453 bool Unused = CI->use_empty();
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);
462 CI->eraseFromParent();
465 RecursivelyDeleteTriviallyDeadInstructions(OldArg);
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.
471 static const Value *GetUnderlyingObjCPtr(const Value *V) {
473 V = GetUnderlyingObject(V);
474 if (!IsForwarding(GetBasicInstructionClass(V)))
476 V = cast<CallInst>(V)->getArgOperand(0);
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.
485 static const Value *StripPointerCastsAndObjCCalls(const Value *V) {
487 V = V->stripPointerCasts();
488 if (!IsForwarding(GetBasicInstructionClass(V)))
490 V = cast<CallInst>(V)->getArgOperand(0);
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.
498 static Value *StripPointerCastsAndObjCCalls(Value *V) {
500 V = V->stripPointerCasts();
501 if (!IsForwarding(GetBasicInstructionClass(V)))
503 V = cast<CallInst>(V)->getArgOperand(0);
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.
511 static Value *GetObjCArg(Value *Inst) {
512 return StripPointerCastsAndObjCCalls(cast<CallInst>(Inst)->getArgOperand(0));
515 /// IsObjCIdentifiedObject - This is similar to AliasAnalysis'
516 /// isObjCIdentifiedObject, except that it uses special knowledge of
517 /// ObjC conventions...
518 static 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) ||
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)) {
531 // 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())
535 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_"))
550 /// FindSingleUseIdentifiedObject - This is similar to
551 /// StripPointerCastsAndObjCCalls but it stops as soon as it finds a value
552 /// with multiple uses.
553 static 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))
568 // 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.
570 if (IsObjCIdentifiedObject(Arg)) {
571 for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
574 if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg)
584 /// ModuleHasARC - Test if the given module looks interesting to run ARC
586 static bool ModuleHasARC(const Module &M) {
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");
607 /// 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
611 static bool DoesObjCBlockEscape(const Value *BlockPtr) {
612 // Walk the def-use chains.
613 SmallVector<const Value *, 4> Worklist;
614 Worklist.push_back(BlockPtr);
616 const Value *V = Worklist.pop_back_val();
617 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
619 const User *UUser = *UI;
620 // Special - Use by a call (callee or argument) is not considered
622 switch (GetBasicInstructionClass(UUser)) {
627 case IC_AutoreleaseRV:
628 // These special functions make copies of their pointer arguments.
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);
639 // Use by a load is not an escape.
640 if (isa<LoadInst>(UUser))
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())
648 // Regular calls and other stuff are not considered escapes.
651 // Otherwise, conservatively assume an escape.
654 } while (!Worklist.empty());
660 //===----------------------------------------------------------------------===//
661 // ARC AliasAnalysis.
662 //===----------------------------------------------------------------------===//
664 #include "llvm/Analysis/AliasAnalysis.h"
665 #include "llvm/Analysis/Passes.h"
666 #include "llvm/Pass.h"
669 /// ObjCARCAliasAnalysis - This is a simple alias analysis
670 /// implementation that uses knowledge of ARC constructs to answer queries.
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 {
678 static char ID; // Class identification, replacement for typeinfo
679 ObjCARCAliasAnalysis() : ImmutablePass(ID) {
680 initializeObjCARCAliasAnalysisPass(*PassRegistry::getPassRegistry());
684 virtual void initializePass() {
685 InitializeAliasAnalysis(this);
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)
694 return static_cast<AliasAnalysis *>(this);
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);
708 } // End of anonymous namespace
710 // Register this pass...
711 char ObjCARCAliasAnalysis::ID = 0;
712 INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis, "objc-arc-aa",
713 "ObjC-ARC-Based Alias Analysis", false, true, false)
715 ImmutablePass *llvm::createObjCARCAliasAnalysisPass() {
716 return new ObjCARCAliasAnalysis();
720 ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
721 AU.setPreservesAll();
722 AliasAnalysis::getAnalysisUsage(AU);
725 AliasAnalysis::AliasResult
726 ObjCARCAliasAnalysis::alias(const Location &LocA, const Location &LocB) {
728 return AliasAnalysis::alias(LocA, LocB);
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);
735 AliasAnalysis::alias(Location(SA, LocA.Size, LocA.TBAATag),
736 Location(SB, LocB.Size, LocB.TBAATag));
737 if (Result != MayAlias)
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)
752 // If that failed, fail. We don't need to chain here, since that's covered
753 // by the earlier precise query.
758 ObjCARCAliasAnalysis::pointsToConstantMemory(const Location &Loc,
761 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
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),
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);
774 return AliasAnalysis::pointsToConstantMemory(Location(U), OrLocal);
776 // If that failed, fail. We don't need to chain here, since that's covered
777 // by the earlier precise query.
781 AliasAnalysis::ModRefBehavior
782 ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
783 // We have nothing to do. Just chain to the next AliasAnalysis.
784 return AliasAnalysis::getModRefBehavior(CS);
787 AliasAnalysis::ModRefBehavior
788 ObjCARCAliasAnalysis::getModRefBehavior(const Function *F) {
790 return AliasAnalysis::getModRefBehavior(F);
792 switch (GetFunctionClass(F)) {
794 return DoesNotAccessMemory;
799 return AliasAnalysis::getModRefBehavior(F);
802 AliasAnalysis::ModRefResult
803 ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS, const Location &Loc) {
805 return AliasAnalysis::getModRefInfo(CS, Loc);
807 switch (GetBasicInstructionClass(CS.getInstruction())) {
811 case IC_AutoreleaseRV:
813 case IC_AutoreleasepoolPush:
814 case IC_FusedRetainAutorelease:
815 case IC_FusedRetainAutoreleaseRV:
816 // These functions don't access any memory visible to the compiler.
817 // Note that this doesn't include objc_retainBlock, because it updates
818 // pointers when it copies block data.
824 return AliasAnalysis::getModRefInfo(CS, Loc);
827 AliasAnalysis::ModRefResult
828 ObjCARCAliasAnalysis::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);
835 //===----------------------------------------------------------------------===//
837 //===----------------------------------------------------------------------===//
839 #include "llvm/Support/InstIterator.h"
840 #include "llvm/Transforms/Scalar.h"
843 /// ObjCARCExpand - Early ARC transformations.
844 class ObjCARCExpand : public FunctionPass {
845 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
846 virtual bool doInitialization(Module &M);
847 virtual bool runOnFunction(Function &F);
849 /// Run - A flag indicating whether this optimization pass should run.
854 ObjCARCExpand() : FunctionPass(ID) {
855 initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
860 char ObjCARCExpand::ID = 0;
861 INITIALIZE_PASS(ObjCARCExpand,
862 "objc-arc-expand", "ObjC ARC expansion", false, false)
864 Pass *llvm::createObjCARCExpandPass() {
865 return new ObjCARCExpand();
868 void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
869 AU.setPreservesCFG();
872 bool ObjCARCExpand::doInitialization(Module &M) {
873 Run = ModuleHasARC(M);
877 bool ObjCARCExpand::runOnFunction(Function &F) {
881 // If nothing in the Module uses ARC, don't do anything.
885 bool Changed = false;
887 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
888 Instruction *Inst = &*I;
890 DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
892 switch (GetBasicInstructionClass(Inst)) {
896 case IC_AutoreleaseRV:
897 case IC_FusedRetainAutorelease:
898 case IC_FusedRetainAutoreleaseRV: {
899 // 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
902 // emitted here. We'll redo them in the contract pass.
904 Value *Value = cast<CallInst>(Inst)->getArgOperand(0);
905 DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n"
906 " New = " << *Value << "\n");
907 Inst->replaceAllUsesWith(Value);
915 DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
920 //===----------------------------------------------------------------------===//
921 // ARC autorelease pool elimination.
922 //===----------------------------------------------------------------------===//
924 #include "llvm/ADT/STLExtras.h"
925 #include "llvm/IR/Constants.h"
928 /// ObjCARCAPElim - Autorelease pool elimination.
929 class ObjCARCAPElim : public ModulePass {
930 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
931 virtual bool runOnModule(Module &M);
933 static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0);
934 static bool OptimizeBB(BasicBlock *BB);
938 ObjCARCAPElim() : ModulePass(ID) {
939 initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
944 char ObjCARCAPElim::ID = 0;
945 INITIALIZE_PASS(ObjCARCAPElim,
947 "ObjC ARC autorelease pool elimination",
950 Pass *llvm::createObjCARCAPElimPass() {
951 return new ObjCARCAPElim();
954 void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
955 AU.setPreservesCFG();
958 /// MayAutorelease - Interprocedurally determine if calls made by the
959 /// given call site can possibly produce autoreleases.
960 bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) {
961 if (const Function *Callee = CS.getCalledFunction()) {
962 if (Callee->isDeclaration() || Callee->mayBeOverridden())
964 for (Function::const_iterator I = Callee->begin(), E = Callee->end();
966 const BasicBlock *BB = I;
967 for (BasicBlock::const_iterator J = BB->begin(), F = BB->end();
969 if (ImmutableCallSite JCS = ImmutableCallSite(J))
970 // This recursion depth limit is arbitrary. It's just great
971 // enough to cover known interesting testcases.
973 !JCS.onlyReadsMemory() &&
974 MayAutorelease(JCS, Depth + 1))
983 bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) {
984 bool Changed = false;
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:
993 case IC_AutoreleasepoolPop:
994 // If this pop matches a push and nothing in between can autorelease,
996 if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) {
998 DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop autorelease pair:\n"
999 << " Pop: " << *Inst << "\n"
1000 << " Push: " << *Push << "\n");
1001 Inst->eraseFromParent();
1002 Push->eraseFromParent();
1007 if (MayAutorelease(ImmutableCallSite(Inst)))
1018 bool ObjCARCAPElim::runOnModule(Module &M) {
1022 // If nothing in the Module uses ARC, don't do anything.
1023 if (!ModuleHasARC(M))
1026 // Find the llvm.global_ctors variable, as the first step in
1027 // 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.
1032 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
1036 assert(GV->hasDefinitiveInitializer() &&
1037 "llvm.global_ctors is uncooperative!");
1039 bool Changed = false;
1041 // 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();
1046 // llvm.global_ctors is an array of pairs where the second members
1047 // are constructor functions.
1048 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.
1053 // Only look at function definitions.
1054 if (F->isDeclaration())
1056 // Only look at functions with one basic block.
1057 if (llvm::next(F->begin()) != F->end())
1059 // Ok, a single-block constructor function definition. Try to optimize it.
1060 Changed |= OptimizeBB(F->begin());
1066 //===----------------------------------------------------------------------===//
1067 // ARC optimization.
1068 //===----------------------------------------------------------------------===//
1070 // TODO: On code like this:
1073 // stuff_that_cannot_release()
1074 // objc_autorelease(%x)
1075 // stuff_that_cannot_release()
1077 // stuff_that_cannot_release()
1078 // objc_autorelease(%x)
1080 // The second retain and autorelease can be deleted.
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.
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
1094 // TODO: OptimizeSequences could generalized to be Interprocedural.
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.
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.
1104 // TODO: The pointer returned from objc_loadWeakRetained is retained.
1106 // TODO: Delete release+retain pairs (rare).
1108 #include "llvm/ADT/SmallPtrSet.h"
1109 #include "llvm/ADT/Statistic.h"
1110 #include "llvm/IR/LLVMContext.h"
1111 #include "llvm/Support/CFG.h"
1113 STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
1114 STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
1115 STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
1116 STATISTIC(NumRets, "Number of return value forwarding "
1117 "retain+autoreleaes eliminated");
1118 STATISTIC(NumRRs, "Number of retain+release paths eliminated");
1119 STATISTIC(NumPeeps, "Number of calls peephole-optimized");
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 {
1128 typedef std::pair<const Value *, const Value *> ValuePairTy;
1129 typedef DenseMap<ValuePairTy, bool> CachedResultsTy;
1130 CachedResultsTy CachedResults;
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);
1136 void operator=(const ProvenanceAnalysis &) LLVM_DELETED_FUNCTION;
1137 ProvenanceAnalysis(const ProvenanceAnalysis &) LLVM_DELETED_FUNCTION;
1140 ProvenanceAnalysis() {}
1142 void setAA(AliasAnalysis *aa) { AA = aa; }
1144 AliasAnalysis *getAA() const { return AA; }
1146 bool related(const Value *A, const Value *B);
1149 CachedResults.clear();
1154 bool 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))
1158 if (A->getCondition() == SB->getCondition())
1159 return related(A->getTrueValue(), SB->getTrueValue()) ||
1160 related(A->getFalseValue(), SB->getFalseValue());
1162 // Check both arms of the Select node individually.
1163 return related(A->getTrueValue(), B) ||
1164 related(A->getFalseValue(), B);
1167 bool 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))))
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))
1188 // All of the arms checked out.
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).
1194 static bool isStoredObjCPointer(const Value *P) {
1195 SmallPtrSet<const Value *, 8> Visited;
1196 SmallVector<const Value *, 8> Worklist;
1197 Worklist.push_back(P);
1200 P = Worklist.pop_back_val();
1201 for (Value::const_use_iterator UI = P->use_begin(), UE = P->use_end();
1203 const User *Ur = *UI;
1204 if (isa<StoreInst>(Ur)) {
1205 if (UI.getOperandNo() == 0)
1206 // The pointer is stored.
1208 // The pointed is stored through.
1211 if (isa<CallInst>(Ur))
1212 // The pointer is passed as an argument, ignore this.
1214 if (isa<PtrToIntInst>(P))
1215 // Assume the worst.
1217 if (Visited.insert(Ur))
1218 Worklist.push_back(Ur);
1220 } while (!Worklist.empty());
1222 // Everything checked out.
1226 bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B) {
1227 // Skip past provenance pass-throughs.
1228 A = GetUnderlyingObjCPtr(A);
1229 B = GetUnderlyingObjCPtr(B);
1235 // Ask regular AliasAnalysis, for a first approximation.
1236 switch (AA->alias(A, B)) {
1237 case AliasAnalysis::NoAlias:
1239 case AliasAnalysis::MustAlias:
1240 case AliasAnalysis::PartialAlias:
1242 case AliasAnalysis::MayAlias:
1246 bool AIsIdentified = IsObjCIdentifiedObject(A);
1247 bool BIsIdentified = IsObjCIdentifiedObject(B);
1249 // An ObjC-Identified object can't alias a load if it is never locally stored.
1250 if (AIsIdentified) {
1251 // Check for an obvious escape.
1252 if (isa<LoadInst>(B))
1253 return isStoredObjCPointer(A);
1254 if (BIsIdentified) {
1255 // 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.
1261 } else if (BIsIdentified) {
1262 // Check for an obvious escape.
1263 if (isa<LoadInst>(A))
1264 return isStoredObjCPointer(B);
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);
1281 bool 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));
1289 return Pair.first->second;
1291 bool Result = relatedCheck(A, B);
1292 CachedResults[ValuePairTy(A, B)] = Result;
1297 // Sequence - A sequence of states that a pointer may go through in which an
1298 // objc_retain and objc_release are actually needed.
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
1310 static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
1314 if (A == S_None || B == S_None)
1317 if (A > B) std::swap(A, B);
1319 // Choose the side which is further along in the sequence.
1320 if ((A == S_Retain || A == S_CanRelease) &&
1321 (B == S_CanRelease || B == S_Use))
1324 // Choose the side which is further along in the sequence.
1325 if ((A == S_Use || A == S_CanRelease) &&
1326 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
1328 // If both sides are releases, choose the more conservative one.
1329 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
1331 if (A == S_Release && B == S_MovableRelease)
1339 /// RRInfo - Unidirectional information about either a
1340 /// retain-decrement-use-release sequence or release-use-decrement-retain
1341 /// reverese sequence.
1343 /// 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.
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.
1354 /// KnownSafe is true when either of these conditions is satisfied.
1357 /// IsRetainBlock - True if the Calls are objc_retainBlock calls (as
1358 /// opposed to objc_retain calls).
1361 /// IsTailCallRelease - True of the objc_release calls are all marked
1362 /// with the "tail" keyword.
1363 bool IsTailCallRelease;
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;
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;
1373 /// ReverseInsertPts - The set of optimal insert positions for
1374 /// moving calls in the opposite sequence.
1375 SmallPtrSet<Instruction *, 2> ReverseInsertPts;
1378 KnownSafe(false), IsRetainBlock(false),
1379 IsTailCallRelease(false),
1380 ReleaseMetadata(0) {}
1386 void RRInfo::clear() {
1388 IsRetainBlock = false;
1389 IsTailCallRelease = false;
1390 ReleaseMetadata = 0;
1392 ReverseInsertPts.clear();
1396 /// PtrState - This class summarizes several per-pointer runtime properties
1397 /// which are propogated through the flow graph.
1399 /// KnownPositiveRefCount - True if the reference count is known to
1401 bool KnownPositiveRefCount;
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
1408 /// Seq - The current position in the sequence.
1412 /// RRI - Unidirectional information about the current sequence.
1413 /// TODO: Encapsulate this better.
1416 PtrState() : KnownPositiveRefCount(false), Partial(false),
1419 void SetKnownPositiveRefCount() {
1420 KnownPositiveRefCount = true;
1423 void ClearRefCount() {
1424 KnownPositiveRefCount = false;
1427 bool IsKnownIncremented() const {
1428 return KnownPositiveRefCount;
1431 void SetSeq(Sequence NewSeq) {
1435 Sequence GetSeq() const {
1439 void ClearSequenceProgress() {
1440 ResetSequenceProgress(S_None);
1443 void ResetSequenceProgress(Sequence NewSeq) {
1449 void Merge(const PtrState &Other, bool TopDown);
1454 PtrState::Merge(const PtrState &Other, bool TopDown) {
1455 Seq = MergeSeqs(Seq, Other.Seq, TopDown);
1456 KnownPositiveRefCount = KnownPositiveRefCount && Other.KnownPositiveRefCount;
1458 // We can't merge a plain objc_retain with an objc_retainBlock.
1459 if (RRI.IsRetainBlock != Other.RRI.IsRetainBlock)
1462 // If we're not in a sequence (anymore), drop all associated state.
1463 if (Seq == S_None) {
1466 } else if (Partial || Other.Partial) {
1467 // 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.
1471 ClearSequenceProgress();
1473 // Conservatively merge the ReleaseMetadata information.
1474 if (RRI.ReleaseMetadata != Other.RRI.ReleaseMetadata)
1475 RRI.ReleaseMetadata = 0;
1477 RRI.KnownSafe = RRI.KnownSafe && Other.RRI.KnownSafe;
1478 RRI.IsTailCallRelease = RRI.IsTailCallRelease &&
1479 Other.RRI.IsTailCallRelease;
1480 RRI.Calls.insert(Other.RRI.Calls.begin(), Other.RRI.Calls.end());
1482 // Merge the insert point sets. If there are any differences,
1483 // that makes this a partial merge.
1484 Partial = RRI.ReverseInsertPts.size() != Other.RRI.ReverseInsertPts.size();
1485 for (SmallPtrSet<Instruction *, 2>::const_iterator
1486 I = Other.RRI.ReverseInsertPts.begin(),
1487 E = Other.RRI.ReverseInsertPts.end(); I != E; ++I)
1488 Partial |= RRI.ReverseInsertPts.insert(*I);
1493 /// BBState - Per-BasicBlock state.
1495 /// TopDownPathCount - The number of unique control paths from the entry
1496 /// which can reach this block.
1497 unsigned TopDownPathCount;
1499 /// BottomUpPathCount - The number of unique control paths to exits
1500 /// from this block.
1501 unsigned BottomUpPathCount;
1503 /// MapTy - A type for PerPtrTopDown and PerPtrBottomUp.
1504 typedef MapVector<const Value *, PtrState> MapTy;
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;
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;
1514 /// 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;
1520 BBState() : TopDownPathCount(0), BottomUpPathCount(0) {}
1522 typedef MapTy::iterator ptr_iterator;
1523 typedef MapTy::const_iterator ptr_const_iterator;
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();
1530 ptr_const_iterator top_down_ptr_end() const {
1531 return PerPtrTopDown.end();
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();
1539 ptr_const_iterator bottom_up_ptr_end() const {
1540 return PerPtrBottomUp.end();
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; }
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; }
1551 PtrState &getPtrTopDownState(const Value *Arg) {
1552 return PerPtrTopDown[Arg];
1555 PtrState &getPtrBottomUpState(const Value *Arg) {
1556 return PerPtrBottomUp[Arg];
1559 void clearBottomUpPointers() {
1560 PerPtrBottomUp.clear();
1563 void clearTopDownPointers() {
1564 PerPtrTopDown.clear();
1567 void InitFromPred(const BBState &Other);
1568 void InitFromSucc(const BBState &Other);
1569 void MergePred(const BBState &Other);
1570 void MergeSucc(const BBState &Other);
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 {
1576 assert(TopDownPathCount != 0);
1577 assert(BottomUpPathCount != 0);
1578 return TopDownPathCount * BottomUpPathCount;
1581 // Specialized CFG utilities.
1582 typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
1583 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(); }
1588 void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
1589 void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
1591 bool isExit() const { return Succs.empty(); }
1595 void BBState::InitFromPred(const BBState &Other) {
1596 PerPtrTopDown = Other.PerPtrTopDown;
1597 TopDownPathCount = Other.TopDownPathCount;
1600 void BBState::InitFromSucc(const BBState &Other) {
1601 PerPtrBottomUp = Other.PerPtrBottomUp;
1602 BottomUpPathCount = Other.BottomUpPathCount;
1605 /// MergePred - The top-down traversal uses this to merge information about
1606 /// predecessors to form the initial state for a new block.
1607 void 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;
1612 // Check for overflow. If we have overflow, fall back to conservative behavior.
1613 if (TopDownPathCount < Other.TopDownPathCount) {
1614 clearTopDownPointers();
1618 // 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
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,
1628 // For each entry in our set, if the other set doesn't have an entry with the
1629 // 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);
1636 /// MergeSucc - The bottom-up traversal uses this to merge information about
1637 /// successors to form the initial state for a new block.
1638 void 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;
1643 // Check for overflow. If we have overflow, fall back to conservative behavior.
1644 if (BottomUpPathCount < Other.BottomUpPathCount) {
1645 clearBottomUpPointers();
1649 // 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,
1659 // For each entry in our set, if the other set doesn't have an entry
1660 // 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);
1668 /// ObjCARCOpt - The main ARC optimization pass.
1669 class ObjCARCOpt : public FunctionPass {
1671 ProvenanceAnalysis PA;
1673 /// Run - A flag indicating whether this optimization pass should run.
1676 /// 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,
1680 *RetainCallee, *RetainBlockCallee, *AutoreleaseCallee;
1682 /// UsedInThisFunciton - Flags which determine whether each of the
1683 /// interesting runtine functions is in fact used in the current function.
1684 unsigned UsedInThisFunction;
1686 /// ImpreciseReleaseMDKind - The Metadata Kind for clang.imprecise_release
1688 unsigned ImpreciseReleaseMDKind;
1690 /// CopyOnEscapeMDKind - The Metadata Kind for clang.arc.copy_on_escape
1692 unsigned CopyOnEscapeMDKind;
1694 /// NoObjCARCExceptionsMDKind - The Metadata Kind for
1695 /// clang.arc.no_objc_arc_exceptions metadata.
1696 unsigned NoObjCARCExceptionsMDKind;
1698 Constant *getRetainRVCallee(Module *M);
1699 Constant *getAutoreleaseRVCallee(Module *M);
1700 Constant *getReleaseCallee(Module *M);
1701 Constant *getRetainCallee(Module *M);
1702 Constant *getRetainBlockCallee(Module *M);
1703 Constant *getAutoreleaseCallee(Module *M);
1705 bool IsRetainBlockOptimizable(const Instruction *Inst);
1707 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);
1712 void CheckForCFGHazards(const BasicBlock *BB,
1713 DenseMap<const BasicBlock *, BBState> &BBStates,
1714 BBState &MyStates) const;
1715 bool VisitInstructionBottomUp(Instruction *Inst,
1717 MapVector<Value *, RRInfo> &Retains,
1719 bool VisitBottomUp(BasicBlock *BB,
1720 DenseMap<const BasicBlock *, BBState> &BBStates,
1721 MapVector<Value *, RRInfo> &Retains);
1722 bool VisitInstructionTopDown(Instruction *Inst,
1723 DenseMap<Value *, RRInfo> &Releases,
1725 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);
1733 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
1734 MapVector<Value *, RRInfo> &Retains,
1735 DenseMap<Value *, RRInfo> &Releases,
1736 SmallVectorImpl<Instruction *> &DeadInsts,
1739 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
1740 MapVector<Value *, RRInfo> &Retains,
1741 DenseMap<Value *, RRInfo> &Releases,
1744 void OptimizeWeakCalls(Function &F);
1746 bool OptimizeSequences(Function &F);
1748 void OptimizeReturns(Function &F);
1750 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
1751 virtual bool doInitialization(Module &M);
1752 virtual bool runOnFunction(Function &F);
1753 virtual void releaseMemory();
1757 ObjCARCOpt() : FunctionPass(ID) {
1758 initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
1763 char ObjCARCOpt::ID = 0;
1764 INITIALIZE_PASS_BEGIN(ObjCARCOpt,
1765 "objc-arc", "ObjC ARC optimization", false, false)
1766 INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
1767 INITIALIZE_PASS_END(ObjCARCOpt,
1768 "objc-arc", "ObjC ARC optimization", false, false)
1770 Pass *llvm::createObjCARCOptPass() {
1771 return new ObjCARCOpt();
1774 void 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();
1781 bool 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))
1788 // If the pointer "escapes" (not including being used in a call),
1789 // the copy may be needed.
1790 if (DoesObjCBlockEscape(Inst))
1793 // Otherwise, it's not needed.
1797 Constant *ObjCARCOpt::getRetainRVCallee(Module *M) {
1798 if (!RetainRVCallee) {
1799 LLVMContext &C = M->getContext();
1800 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
1801 Type *Params[] = { I8X };
1802 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
1803 AttributeSet Attribute =
1804 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
1805 Attribute::get(C, Attribute::NoUnwind));
1807 M->getOrInsertFunction("objc_retainAutoreleasedReturnValue", FTy,
1810 return RetainRVCallee;
1813 Constant *ObjCARCOpt::getAutoreleaseRVCallee(Module *M) {
1814 if (!AutoreleaseRVCallee) {
1815 LLVMContext &C = M->getContext();
1816 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
1817 Type *Params[] = { I8X };
1818 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
1819 AttributeSet Attribute =
1820 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
1821 Attribute::get(C, Attribute::NoUnwind));
1822 AutoreleaseRVCallee =
1823 M->getOrInsertFunction("objc_autoreleaseReturnValue", FTy,
1826 return AutoreleaseRVCallee;
1829 Constant *ObjCARCOpt::getReleaseCallee(Module *M) {
1830 if (!ReleaseCallee) {
1831 LLVMContext &C = M->getContext();
1832 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
1833 AttributeSet Attribute =
1834 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
1835 Attribute::get(C, Attribute::NoUnwind));
1837 M->getOrInsertFunction(
1839 FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
1842 return ReleaseCallee;
1845 Constant *ObjCARCOpt::getRetainCallee(Module *M) {
1846 if (!RetainCallee) {
1847 LLVMContext &C = M->getContext();
1848 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
1849 AttributeSet Attribute =
1850 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
1851 Attribute::get(C, Attribute::NoUnwind));
1853 M->getOrInsertFunction(
1855 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
1858 return RetainCallee;
1861 Constant *ObjCARCOpt::getRetainBlockCallee(Module *M) {
1862 if (!RetainBlockCallee) {
1863 LLVMContext &C = M->getContext();
1864 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
1865 // objc_retainBlock is not nounwind because it calls user copy constructors
1866 // which could theoretically throw.
1868 M->getOrInsertFunction(
1870 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
1873 return RetainBlockCallee;
1876 Constant *ObjCARCOpt::getAutoreleaseCallee(Module *M) {
1877 if (!AutoreleaseCallee) {
1878 LLVMContext &C = M->getContext();
1879 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
1880 AttributeSet Attribute =
1881 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
1882 Attribute::get(C, Attribute::NoUnwind));
1884 M->getOrInsertFunction(
1886 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
1889 return AutoreleaseCallee;
1892 /// IsPotentialUse - Test whether the given value is possible a
1893 /// reference-counted pointer, including tests which utilize AliasAnalysis.
1894 static bool IsPotentialUse(const Value *Op, AliasAnalysis &AA) {
1895 // First make the rudimentary check.
1896 if (!IsPotentialUse(Op))
1899 // Objects in constant memory are not reference-counted.
1900 if (AA.pointsToConstantMemory(Op))
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()))
1908 // Otherwise assume the worst.
1912 /// CanAlterRefCount - Test whether the given instruction can result in a
1913 /// reference count modification (positive or negative) for the pointer's
1916 CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
1917 ProvenanceAnalysis &PA, InstructionClass Class) {
1919 case IC_Autorelease:
1920 case IC_AutoreleaseRV:
1922 // These operations never directly modify a reference count.
1927 ImmutableCallSite CS = static_cast<const Value *>(Inst);
1928 assert(CS && "Only calls can alter reference counts!");
1930 // See if AliasAnalysis can help us with the call.
1931 AliasAnalysis::ModRefBehavior MRB = PA.getAA()->getModRefBehavior(CS);
1932 if (AliasAnalysis::onlyReadsMemory(MRB))
1934 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
1935 for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
1937 const Value *Op = *I;
1938 if (IsPotentialUse(Op, *PA.getAA()) && PA.related(Ptr, Op))
1944 // Assume the worst.
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.
1951 CanUse(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)
1957 // Consider various instructions which may have pointer arguments which are
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.
1963 if (!IsPotentialUse(ICI->getOperand(1), *PA.getAA()))
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;
1970 if (IsPotentialUse(Op, *PA.getAA()) && PA.related(Ptr, Op))
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
1980 return IsPotentialUse(Op, *PA.getAA()) && PA.related(Op, Ptr);
1983 // Check each operand for a match.
1984 for (User::const_op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
1986 const Value *Op = *OI;
1987 if (IsPotentialUse(Op, *PA.getAA()) && PA.related(Ptr, Op))
1993 /// CanInterruptRV - Test whether the given instruction can autorelease
1994 /// any pointer or cause an autoreleasepool pop.
1996 CanInterruptRV(InstructionClass Class) {
1998 case IC_AutoreleasepoolPop:
2001 case IC_Autorelease:
2002 case IC_AutoreleaseRV:
2003 case IC_FusedRetainAutorelease:
2004 case IC_FusedRetainAutoreleaseRV:
2012 /// DependenceKind - There are several kinds of dependence-like concepts in
2014 enum DependenceKind {
2015 NeedsPositiveRetainCount,
2016 AutoreleasePoolBoundary,
2017 CanChangeRetainCount,
2018 RetainAutoreleaseDep, ///< Blocks objc_retainAutorelease.
2019 RetainAutoreleaseRVDep, ///< Blocks objc_retainAutoreleaseReturnValue.
2020 RetainRVDep ///< Blocks objc_retainAutoreleasedReturnValue.
2024 /// Depends - Test if there can be dependencies on Inst through Arg. This
2025 /// function only tests dependencies relevant for removing pairs of calls.
2027 Depends(DependenceKind Flavor, Instruction *Inst, const Value *Arg,
2028 ProvenanceAnalysis &PA) {
2029 // If we've reached the definition of Arg, stop.
2034 case NeedsPositiveRetainCount: {
2035 InstructionClass Class = GetInstructionClass(Inst);
2037 case IC_AutoreleasepoolPop:
2038 case IC_AutoreleasepoolPush:
2042 return CanUse(Inst, Arg, PA, Class);
2046 case AutoreleasePoolBoundary: {
2047 InstructionClass Class = GetInstructionClass(Inst);
2049 case IC_AutoreleasepoolPop:
2050 case IC_AutoreleasepoolPush:
2051 // These mark the end and begin of an autorelease pool scope.
2054 // Nothing else does this.
2059 case CanChangeRetainCount: {
2060 InstructionClass Class = GetInstructionClass(Inst);
2062 case IC_AutoreleasepoolPop:
2063 // Conservatively assume this can decrement any count.
2065 case IC_AutoreleasepoolPush:
2069 return CanAlterRefCount(Inst, Arg, PA, Class);
2073 case RetainAutoreleaseDep:
2074 switch (GetBasicInstructionClass(Inst)) {
2075 case IC_AutoreleasepoolPop:
2076 case IC_AutoreleasepoolPush:
2077 // Don't merge an objc_autorelease with an objc_retain inside a different
2078 // autoreleasepool scope.
2082 // Check for a retain of the same pointer for merging.
2083 return GetObjCArg(Inst) == Arg;
2085 // Nothing else matters for objc_retainAutorelease formation.
2089 case RetainAutoreleaseRVDep: {
2090 InstructionClass Class = GetBasicInstructionClass(Inst);
2094 // Check for a retain of the same pointer for merging.
2095 return GetObjCArg(Inst) == Arg;
2097 // Anything that can autorelease interrupts
2098 // retainAutoreleaseReturnValue formation.
2099 return CanInterruptRV(Class);
2104 return CanInterruptRV(GetBasicInstructionClass(Inst));
2107 llvm_unreachable("Invalid dependence flavor");
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?
2114 FindDependencies(DependenceKind Flavor,
2116 BasicBlock *StartBB, Instruction *StartInst,
2117 SmallPtrSet<Instruction *, 4> &DependingInstructions,
2118 SmallPtrSet<const BasicBlock *, 4> &Visited,
2119 ProvenanceAnalysis &PA) {
2120 BasicBlock::iterator StartPos = StartInst;
2122 SmallVector<std::pair<BasicBlock *, BasicBlock::iterator>, 4> Worklist;
2123 Worklist.push_back(std::make_pair(StartBB, StartPos));
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();
2131 if (LocalStartPos == StartBBBegin) {
2132 pred_iterator PI(LocalStartBB), PE(LocalStartBB, false);
2134 // If we've reached the function entry, produce a null dependence.
2135 DependingInstructions.insert(0);
2137 // Add the predecessors to the worklist.
2139 BasicBlock *PredBB = *PI;
2140 if (Visited.insert(PredBB))
2141 Worklist.push_back(std::make_pair(PredBB, PredBB->end()));
2142 } while (++PI != PE);
2146 Instruction *Inst = --LocalStartPos;
2147 if (Depends(Flavor, Inst, Arg, PA)) {
2148 DependingInstructions.insert(Inst);
2152 } while (!Worklist.empty());
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
2157 for (SmallPtrSet<const BasicBlock *, 4>::const_iterator I = Visited.begin(),
2158 E = Visited.end(); I != E; ++I) {
2159 const BasicBlock *BB = *I;
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));
2173 static bool isNullOrUndef(const Value *V) {
2174 return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
2177 static bool isNoopInstruction(const Instruction *I) {
2178 return isa<BitCastInst>(I) ||
2179 (isa<GetElementPtrInst>(I) &&
2180 cast<GetElementPtrInst>(I)->hasAllZeroIndices());
2183 /// OptimizeRetainCall - Turn objc_retain into
2184 /// objc_retainAutoreleasedReturnValue if the operand is a return value.
2186 ObjCARCOpt::OptimizeRetainCall(Function &F, Instruction *Retain) {
2187 ImmutableCallSite CS(GetObjCArg(Retain));
2188 const Instruction *Call = CS.getInstruction();
2190 if (Call->getParent() != Retain->getParent()) return;
2192 // Check that the call is next to the retain.
2193 BasicBlock::const_iterator I = Call;
2195 while (isNoopInstruction(I)) ++I;
2199 // Turn it to an objc_retainAutoreleasedReturnValue..
2203 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainCall: Transforming "
2204 "objc_retainAutoreleasedReturnValue => "
2205 "objc_retain since the operand is not a return value.\n"
2207 << *Retain << "\n");
2209 cast<CallInst>(Retain)->setCalledFunction(getRetainRVCallee(F.getParent()));
2211 DEBUG(dbgs() << " New: "
2212 << *Retain << "\n");
2215 /// OptimizeRetainRVCall - Turn objc_retainAutoreleasedReturnValue into
2216 /// 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.
2219 ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
2220 // Check for the argument being from an immediately preceding call or invoke.
2221 const Value *Arg = GetObjCArg(RetainRV);
2222 ImmutableCallSite CS(Arg);
2223 if (const Instruction *Call = CS.getInstruction()) {
2224 if (Call->getParent() == RetainRV->getParent()) {
2225 BasicBlock::const_iterator I = Call;
2227 while (isNoopInstruction(I)) ++I;
2228 if (&*I == RetainRV)
2230 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
2231 BasicBlock *RetainRVParent = RetainRV->getParent();
2232 if (II->getNormalDest() == RetainRVParent) {
2233 BasicBlock::const_iterator I = RetainRVParent->begin();
2234 while (isNoopInstruction(I)) ++I;
2235 if (&*I == RetainRV)
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();
2245 do --I; while (I != Begin && isNoopInstruction(I));
2246 if (GetBasicInstructionClass(I) == IC_AutoreleaseRV &&
2247 GetObjCArg(I) == Arg) {
2251 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainRVCall: Erasing " << *I << "\n"
2252 << " Erasing " << *RetainRV
2255 EraseInstruction(I);
2256 EraseInstruction(RetainRV);
2261 // Turn it to a plain objc_retain.
2265 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainRVCall: Transforming "
2266 "objc_retainAutoreleasedReturnValue => "
2267 "objc_retain since the operand is not a return value.\n"
2269 << *RetainRV << "\n");
2271 cast<CallInst>(RetainRV)->setCalledFunction(getRetainCallee(F.getParent()));
2273 DEBUG(dbgs() << " New: "
2274 << *RetainRV << "\n");
2279 /// OptimizeAutoreleaseRVCall - Turn objc_autoreleaseReturnValue into
2280 /// objc_autorelease if the result is not used as a return value.
2282 ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV) {
2283 // Check for a return of the pointer value.
2284 const Value *Ptr = GetObjCArg(AutoreleaseRV);
2285 SmallVector<const Value *, 2> Users;
2286 Users.push_back(Ptr);
2288 Ptr = Users.pop_back_val();
2289 for (Value::const_use_iterator UI = Ptr->use_begin(), UE = Ptr->use_end();
2291 const User *I = *UI;
2292 if (isa<ReturnInst>(I) || GetBasicInstructionClass(I) == IC_RetainRV)
2294 if (isa<BitCastInst>(I))
2297 } while (!Users.empty());
2302 DEBUG(dbgs() << "ObjCARCOpt::OptimizeAutoreleaseRVCall: Transforming "
2303 "objc_autoreleaseReturnValue => "
2304 "objc_autorelease since its operand is not used as a return "
2307 << *AutoreleaseRV << "\n");
2309 cast<CallInst>(AutoreleaseRV)->
2310 setCalledFunction(getAutoreleaseCallee(F.getParent()));
2312 DEBUG(dbgs() << " New: "
2313 << *AutoreleaseRV << "\n");
2317 /// OptimizeIndividualCalls - Visit each call, one at a time, and make
2318 /// simplifications without doing any additional analysis.
2319 void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
2320 // Reset all the flags in preparation for recomputing them.
2321 UsedInThisFunction = 0;
2323 // Visit all objc_* calls in F.
2324 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2325 Instruction *Inst = &*I++;
2327 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Visiting: " <<
2330 InstructionClass Class = GetBasicInstructionClass(Inst);
2335 // Delete no-op casts. These function calls have special semantics, but
2336 // the semantics are entirely implemented via lowering in the front-end,
2337 // so by the time they reach the optimizer, they are just no-op calls
2338 // which return their argument.
2340 // There are gray areas here, as the ability to cast reference-counted
2341 // pointers to raw void* and back allows code to break ARC assumptions,
2342 // however these are currently considered to be unimportant.
2346 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Erasing no-op cast:"
2347 " " << *Inst << "\n");
2348 EraseInstruction(Inst);
2351 // If the pointer-to-weak-pointer is null, it's undefined behavior.
2354 case IC_LoadWeakRetained:
2356 case IC_DestroyWeak: {
2357 CallInst *CI = cast<CallInst>(Inst);
2358 if (isNullOrUndef(CI->getArgOperand(0))) {
2360 Type *Ty = CI->getArgOperand(0)->getType();
2361 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
2362 Constant::getNullValue(Ty),
2364 CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
2365 CI->eraseFromParent();
2372 CallInst *CI = cast<CallInst>(Inst);
2373 if (isNullOrUndef(CI->getArgOperand(0)) ||
2374 isNullOrUndef(CI->getArgOperand(1))) {
2376 Type *Ty = CI->getArgOperand(0)->getType();
2377 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
2378 Constant::getNullValue(Ty),
2380 CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
2381 CI->eraseFromParent();
2387 OptimizeRetainCall(F, Inst);
2390 if (OptimizeRetainRVCall(F, Inst))
2393 case IC_AutoreleaseRV:
2394 OptimizeAutoreleaseRVCall(F, Inst);
2398 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
2399 if (IsAutorelease(Class) && Inst->use_empty()) {
2400 CallInst *Call = cast<CallInst>(Inst);
2401 const Value *Arg = Call->getArgOperand(0);
2402 Arg = FindSingleUseIdentifiedObject(Arg);
2407 // Create the declaration lazily.
2408 LLVMContext &C = Inst->getContext();
2410 CallInst::Create(getReleaseCallee(F.getParent()),
2411 Call->getArgOperand(0), "", Call);
2412 NewCall->setMetadata(ImpreciseReleaseMDKind,
2413 MDNode::get(C, ArrayRef<Value *>()));
2414 EraseInstruction(Call);
2420 // For functions which can never be passed stack arguments, add
2422 if (IsAlwaysTail(Class)) {
2424 cast<CallInst>(Inst)->setTailCall();
2427 // Set nounwind as needed.
2428 if (IsNoThrow(Class)) {
2430 cast<CallInst>(Inst)->setDoesNotThrow();
2433 if (!IsNoopOnNull(Class)) {
2434 UsedInThisFunction |= 1 << Class;
2438 const Value *Arg = GetObjCArg(Inst);
2440 // ARC calls with null are no-ops. Delete them.
2441 if (isNullOrUndef(Arg)) {
2444 EraseInstruction(Inst);
2448 // Keep track of which of retain, release, autorelease, and retain_block
2449 // are actually present in this function.
2450 UsedInThisFunction |= 1 << Class;
2452 // If Arg is a PHI, and one or more incoming values to the
2453 // PHI are null, and the call is control-equivalent to the PHI, and there
2454 // are no relevant side effects between the PHI and the call, the call
2455 // could be pushed up to just those paths with non-null incoming values.
2456 // For now, don't bother splitting critical edges for this.
2457 SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
2458 Worklist.push_back(std::make_pair(Inst, Arg));
2460 std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
2464 const PHINode *PN = dyn_cast<PHINode>(Arg);
2467 // Determine if the PHI has any null operands, or any incoming
2469 bool HasNull = false;
2470 bool HasCriticalEdges = false;
2471 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2473 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
2474 if (isNullOrUndef(Incoming))
2476 else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
2477 .getNumSuccessors() != 1) {
2478 HasCriticalEdges = true;
2482 // If we have null operands and no critical edges, optimize.
2483 if (!HasCriticalEdges && HasNull) {
2484 SmallPtrSet<Instruction *, 4> DependingInstructions;
2485 SmallPtrSet<const BasicBlock *, 4> Visited;
2487 // Check that there is nothing that cares about the reference
2488 // count between the call and the phi.
2491 case IC_RetainBlock:
2492 // These can always be moved up.
2495 // These can't be moved across things that care about the retain
2497 FindDependencies(NeedsPositiveRetainCount, Arg,
2498 Inst->getParent(), Inst,
2499 DependingInstructions, Visited, PA);
2501 case IC_Autorelease:
2502 // These can't be moved across autorelease pool scope boundaries.
2503 FindDependencies(AutoreleasePoolBoundary, Arg,
2504 Inst->getParent(), Inst,
2505 DependingInstructions, Visited, PA);
2508 case IC_AutoreleaseRV:
2509 // Don't move these; the RV optimization depends on the autoreleaseRV
2510 // being tail called, and the retainRV being immediately after a call
2511 // (which might still happen if we get lucky with codegen layout, but
2512 // it's not worth taking the chance).
2515 llvm_unreachable("Invalid dependence flavor");
2518 if (DependingInstructions.size() == 1 &&
2519 *DependingInstructions.begin() == PN) {
2522 // Clone the call into each predecessor that has a non-null value.
2523 CallInst *CInst = cast<CallInst>(Inst);
2524 Type *ParamTy = CInst->getArgOperand(0)->getType();
2525 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2527 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
2528 if (!isNullOrUndef(Incoming)) {
2529 CallInst *Clone = cast<CallInst>(CInst->clone());
2530 Value *Op = PN->getIncomingValue(i);
2531 Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
2532 if (Op->getType() != ParamTy)
2533 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
2534 Clone->setArgOperand(0, Op);
2535 Clone->insertBefore(InsertPos);
2536 Worklist.push_back(std::make_pair(Clone, Incoming));
2539 // Erase the original call.
2540 EraseInstruction(CInst);
2544 } while (!Worklist.empty());
2546 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Finished Queue.\n\n");
2551 /// CheckForCFGHazards - Check for critical edges, loop boundaries, irreducible
2552 /// control flow, or other CFG structures where moving code across the edge
2553 /// would result in it being executed more.
2555 ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
2556 DenseMap<const BasicBlock *, BBState> &BBStates,
2557 BBState &MyStates) const {
2558 // If any top-down local-use or possible-dec has a succ which is earlier in
2559 // the sequence, forget it.
2560 for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
2561 E = MyStates.top_down_ptr_end(); I != E; ++I)
2562 switch (I->second.GetSeq()) {
2565 const Value *Arg = I->first;
2566 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
2567 bool SomeSuccHasSame = false;
2568 bool AllSuccsHaveSame = true;
2569 PtrState &S = I->second;
2570 succ_const_iterator SI(TI), SE(TI, false);
2572 // If the terminator is an invoke marked with the
2573 // clang.arc.no_objc_arc_exceptions metadata, the unwind edge can be
2574 // ignored, for ARC purposes.
2575 if (isa<InvokeInst>(TI) && TI->getMetadata(NoObjCARCExceptionsMDKind))
2578 for (; SI != SE; ++SI) {
2579 Sequence SuccSSeq = S_None;
2580 bool SuccSRRIKnownSafe = false;
2581 // If VisitBottomUp has pointer information for this successor, take
2582 // what we know about it.
2583 DenseMap<const BasicBlock *, BBState>::iterator BBI =
2585 assert(BBI != BBStates.end());
2586 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
2587 SuccSSeq = SuccS.GetSeq();
2588 SuccSRRIKnownSafe = SuccS.RRI.KnownSafe;
2591 case S_CanRelease: {
2592 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
2593 S.ClearSequenceProgress();
2599 SomeSuccHasSame = true;
2603 case S_MovableRelease:
2604 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
2605 AllSuccsHaveSame = false;
2608 llvm_unreachable("bottom-up pointer in retain state!");
2611 // If the state at the other end of any of the successor edges
2612 // matches the current state, require all edges to match. This
2613 // guards against loops in the middle of a sequence.
2614 if (SomeSuccHasSame && !AllSuccsHaveSame)
2615 S.ClearSequenceProgress();
2618 case S_CanRelease: {
2619 const Value *Arg = I->first;
2620 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
2621 bool SomeSuccHasSame = false;
2622 bool AllSuccsHaveSame = true;
2623 PtrState &S = I->second;
2624 succ_const_iterator SI(TI), SE(TI, false);
2626 // If the terminator is an invoke marked with the
2627 // clang.arc.no_objc_arc_exceptions metadata, the unwind edge can be
2628 // ignored, for ARC purposes.
2629 if (isa<InvokeInst>(TI) && TI->getMetadata(NoObjCARCExceptionsMDKind))
2632 for (; SI != SE; ++SI) {
2633 Sequence SuccSSeq = S_None;
2634 bool SuccSRRIKnownSafe = false;
2635 // If VisitBottomUp has pointer information for this successor, take
2636 // what we know about it.
2637 DenseMap<const BasicBlock *, BBState>::iterator BBI =
2639 assert(BBI != BBStates.end());
2640 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
2641 SuccSSeq = SuccS.GetSeq();
2642 SuccSRRIKnownSafe = SuccS.RRI.KnownSafe;
2645 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
2646 S.ClearSequenceProgress();
2652 SomeSuccHasSame = true;
2656 case S_MovableRelease:
2658 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
2659 AllSuccsHaveSame = false;
2662 llvm_unreachable("bottom-up pointer in retain state!");
2665 // If the state at the other end of any of the successor edges
2666 // matches the current state, require all edges to match. This
2667 // guards against loops in the middle of a sequence.
2668 if (SomeSuccHasSame && !AllSuccsHaveSame)
2669 S.ClearSequenceProgress();
2676 ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
2678 MapVector<Value *, RRInfo> &Retains,
2679 BBState &MyStates) {
2680 bool NestingDetected = false;
2681 InstructionClass Class = GetInstructionClass(Inst);
2682 const Value *Arg = 0;
2686 Arg = GetObjCArg(Inst);
2688 PtrState &S = MyStates.getPtrBottomUpState(Arg);
2690 // If we see two releases in a row on the same pointer. If so, make
2691 // a note, and we'll cicle back to revisit it after we've
2692 // hopefully eliminated the second release, which may allow us to
2693 // eliminate the first release too.
2694 // Theoretically we could implement removal of nested retain+release
2695 // pairs by making PtrState hold a stack of states, but this is
2696 // simple and avoids adding overhead for the non-nested case.
2697 if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease)
2698 NestingDetected = true;
2700 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
2701 S.ResetSequenceProgress(ReleaseMetadata ? S_MovableRelease : S_Release);
2702 S.RRI.ReleaseMetadata = ReleaseMetadata;
2703 S.RRI.KnownSafe = S.IsKnownIncremented();
2704 S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
2705 S.RRI.Calls.insert(Inst);
2707 S.SetKnownPositiveRefCount();
2710 case IC_RetainBlock:
2711 // An objc_retainBlock call with just a use may need to be kept,
2712 // because it may be copying a block from the stack to the heap.
2713 if (!IsRetainBlockOptimizable(Inst))
2718 Arg = GetObjCArg(Inst);
2720 PtrState &S = MyStates.getPtrBottomUpState(Arg);
2721 S.SetKnownPositiveRefCount();
2723 switch (S.GetSeq()) {
2726 case S_MovableRelease:
2728 S.RRI.ReverseInsertPts.clear();
2731 // Don't do retain+release tracking for IC_RetainRV, because it's
2732 // better to let it remain as the first instruction after a call.
2733 if (Class != IC_RetainRV) {
2734 S.RRI.IsRetainBlock = Class == IC_RetainBlock;
2735 Retains[Inst] = S.RRI;
2737 S.ClearSequenceProgress();
2742 llvm_unreachable("bottom-up pointer in retain state!");
2744 return NestingDetected;
2746 case IC_AutoreleasepoolPop:
2747 // Conservatively, clear MyStates for all known pointers.
2748 MyStates.clearBottomUpPointers();
2749 return NestingDetected;
2750 case IC_AutoreleasepoolPush:
2752 // These are irrelevant.
2753 return NestingDetected;
2758 // Consider any other possible effects of this instruction on each
2759 // pointer being tracked.
2760 for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(),
2761 ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) {
2762 const Value *Ptr = MI->first;
2764 continue; // Handled above.
2765 PtrState &S = MI->second;
2766 Sequence Seq = S.GetSeq();
2768 // Check for possible releases.
2769 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
2773 S.SetSeq(S_CanRelease);
2777 case S_MovableRelease:
2782 llvm_unreachable("bottom-up pointer in retain state!");
2786 // Check for possible direct uses.
2789 case S_MovableRelease:
2790 if (CanUse(Inst, Ptr, PA, Class)) {
2791 assert(S.RRI.ReverseInsertPts.empty());
2792 // If this is an invoke instruction, we're scanning it as part of
2793 // one of its successor blocks, since we can't insert code after it
2794 // in its own block, and we don't want to split critical edges.
2795 if (isa<InvokeInst>(Inst))
2796 S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
2798 S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
2800 } else if (Seq == S_Release &&
2801 (Class == IC_User || Class == IC_CallOrUser)) {
2802 // Non-movable releases depend on any possible objc pointer use.
2804 assert(S.RRI.ReverseInsertPts.empty());
2805 // As above; handle invoke specially.
2806 if (isa<InvokeInst>(Inst))
2807 S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
2809 S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
2813 if (CanUse(Inst, Ptr, PA, Class))
2821 llvm_unreachable("bottom-up pointer in retain state!");
2825 return NestingDetected;
2829 ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
2830 DenseMap<const BasicBlock *, BBState> &BBStates,
2831 MapVector<Value *, RRInfo> &Retains) {
2832 bool NestingDetected = false;
2833 BBState &MyStates = BBStates[BB];
2835 // Merge the states from each successor to compute the initial state
2836 // for the current block.
2837 BBState::edge_iterator SI(MyStates.succ_begin()),
2838 SE(MyStates.succ_end());
2840 const BasicBlock *Succ = *SI;
2841 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
2842 assert(I != BBStates.end());
2843 MyStates.InitFromSucc(I->second);
2845 for (; SI != SE; ++SI) {
2847 I = BBStates.find(Succ);
2848 assert(I != BBStates.end());
2849 MyStates.MergeSucc(I->second);
2853 // Visit all the instructions, bottom-up.
2854 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
2855 Instruction *Inst = llvm::prior(I);
2857 // Invoke instructions are visited as part of their successors (below).
2858 if (isa<InvokeInst>(Inst))
2861 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
2864 // If there's a predecessor with an invoke, visit the invoke as if it were
2865 // part of this block, since we can't insert code after an invoke in its own
2866 // block, and we don't want to split critical edges.
2867 for (BBState::edge_iterator PI(MyStates.pred_begin()),
2868 PE(MyStates.pred_end()); PI != PE; ++PI) {
2869 BasicBlock *Pred = *PI;
2870 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
2871 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
2874 return NestingDetected;
2878 ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
2879 DenseMap<Value *, RRInfo> &Releases,
2880 BBState &MyStates) {
2881 bool NestingDetected = false;
2882 InstructionClass Class = GetInstructionClass(Inst);
2883 const Value *Arg = 0;
2886 case IC_RetainBlock:
2887 // An objc_retainBlock call with just a use may need to be kept,
2888 // because it may be copying a block from the stack to the heap.
2889 if (!IsRetainBlockOptimizable(Inst))
2894 Arg = GetObjCArg(Inst);
2896 PtrState &S = MyStates.getPtrTopDownState(Arg);
2898 // Don't do retain+release tracking for IC_RetainRV, because it's
2899 // better to let it remain as the first instruction after a call.
2900 if (Class != IC_RetainRV) {
2901 // If we see two retains in a row on the same pointer. If so, make
2902 // a note, and we'll cicle back to revisit it after we've
2903 // hopefully eliminated the second retain, which may allow us to
2904 // eliminate the first retain too.
2905 // Theoretically we could implement removal of nested retain+release
2906 // pairs by making PtrState hold a stack of states, but this is
2907 // simple and avoids adding overhead for the non-nested case.
2908 if (S.GetSeq() == S_Retain)
2909 NestingDetected = true;
2911 S.ResetSequenceProgress(S_Retain);
2912 S.RRI.IsRetainBlock = Class == IC_RetainBlock;
2913 S.RRI.KnownSafe = S.IsKnownIncremented();
2914 S.RRI.Calls.insert(Inst);
2917 S.SetKnownPositiveRefCount();
2919 // A retain can be a potential use; procede to the generic checking
2924 Arg = GetObjCArg(Inst);
2926 PtrState &S = MyStates.getPtrTopDownState(Arg);
2929 switch (S.GetSeq()) {
2932 S.RRI.ReverseInsertPts.clear();
2935 S.RRI.ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
2936 S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
2937 Releases[Inst] = S.RRI;
2938 S.ClearSequenceProgress();
2944 case S_MovableRelease:
2945 llvm_unreachable("top-down pointer in release state!");
2949 case IC_AutoreleasepoolPop:
2950 // Conservatively, clear MyStates for all known pointers.
2951 MyStates.clearTopDownPointers();
2952 return NestingDetected;
2953 case IC_AutoreleasepoolPush:
2955 // These are irrelevant.
2956 return NestingDetected;
2961 // Consider any other possible effects of this instruction on each
2962 // pointer being tracked.
2963 for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
2964 ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
2965 const Value *Ptr = MI->first;
2967 continue; // Handled above.
2968 PtrState &S = MI->second;
2969 Sequence Seq = S.GetSeq();
2971 // Check for possible releases.
2972 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
2976 S.SetSeq(S_CanRelease);
2977 assert(S.RRI.ReverseInsertPts.empty());
2978 S.RRI.ReverseInsertPts.insert(Inst);
2980 // One call can't cause a transition from S_Retain to S_CanRelease
2981 // and S_CanRelease to S_Use. If we've made the first transition,
2990 case S_MovableRelease:
2991 llvm_unreachable("top-down pointer in release state!");
2995 // Check for possible direct uses.
2998 if (CanUse(Inst, Ptr, PA, Class))
3007 case S_MovableRelease:
3008 llvm_unreachable("top-down pointer in release state!");
3012 return NestingDetected;
3016 ObjCARCOpt::VisitTopDown(BasicBlock *BB,
3017 DenseMap<const BasicBlock *, BBState> &BBStates,
3018 DenseMap<Value *, RRInfo> &Releases) {
3019 bool NestingDetected = false;
3020 BBState &MyStates = BBStates[BB];
3022 // Merge the states from each predecessor to compute the initial state
3023 // for the current block.
3024 BBState::edge_iterator PI(MyStates.pred_begin()),
3025 PE(MyStates.pred_end());
3027 const BasicBlock *Pred = *PI;
3028 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
3029 assert(I != BBStates.end());
3030 MyStates.InitFromPred(I->second);
3032 for (; PI != PE; ++PI) {
3034 I = BBStates.find(Pred);
3035 assert(I != BBStates.end());
3036 MyStates.MergePred(I->second);
3040 // Visit all the instructions, top-down.
3041 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
3042 Instruction *Inst = I;
3043 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
3046 CheckForCFGHazards(BB, BBStates, MyStates);
3047 return NestingDetected;
3051 ComputePostOrders(Function &F,
3052 SmallVectorImpl<BasicBlock *> &PostOrder,
3053 SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
3054 unsigned NoObjCARCExceptionsMDKind,
3055 DenseMap<const BasicBlock *, BBState> &BBStates) {
3056 /// Visited - The visited set, for doing DFS walks.
3057 SmallPtrSet<BasicBlock *, 16> Visited;
3059 // Do DFS, computing the PostOrder.
3060 SmallPtrSet<BasicBlock *, 16> OnStack;
3061 SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
3063 // Functions always have exactly one entry block, and we don't have
3064 // any other block that we treat like an entry block.
3065 BasicBlock *EntryBB = &F.getEntryBlock();
3066 BBState &MyStates = BBStates[EntryBB];
3067 MyStates.SetAsEntry();
3068 TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
3069 SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
3070 Visited.insert(EntryBB);
3071 OnStack.insert(EntryBB);
3074 BasicBlock *CurrBB = SuccStack.back().first;
3075 TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
3076 succ_iterator SE(TI, false);
3078 // If the terminator is an invoke marked with the
3079 // clang.arc.no_objc_arc_exceptions metadata, the unwind edge can be
3080 // ignored, for ARC purposes.
3081 if (isa<InvokeInst>(TI) && TI->getMetadata(NoObjCARCExceptionsMDKind))
3084 while (SuccStack.back().second != SE) {
3085 BasicBlock *SuccBB = *SuccStack.back().second++;
3086 if (Visited.insert(SuccBB)) {
3087 TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
3088 SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
3089 BBStates[CurrBB].addSucc(SuccBB);
3090 BBState &SuccStates = BBStates[SuccBB];
3091 SuccStates.addPred(CurrBB);
3092 OnStack.insert(SuccBB);
3096 if (!OnStack.count(SuccBB)) {
3097 BBStates[CurrBB].addSucc(SuccBB);
3098 BBStates[SuccBB].addPred(CurrBB);
3101 OnStack.erase(CurrBB);
3102 PostOrder.push_back(CurrBB);
3103 SuccStack.pop_back();
3104 } while (!SuccStack.empty());
3108 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
3109 // Functions may have many exits, and there also blocks which we treat
3110 // as exits due to ignored edges.
3111 SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
3112 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
3113 BasicBlock *ExitBB = I;
3114 BBState &MyStates = BBStates[ExitBB];
3115 if (!MyStates.isExit())
3118 MyStates.SetAsExit();
3120 PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
3121 Visited.insert(ExitBB);
3122 while (!PredStack.empty()) {
3123 reverse_dfs_next_succ:
3124 BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
3125 while (PredStack.back().second != PE) {
3126 BasicBlock *BB = *PredStack.back().second++;
3127 if (Visited.insert(BB)) {
3128 PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
3129 goto reverse_dfs_next_succ;
3132 ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
3137 // Visit - Visit the function both top-down and bottom-up.
3139 ObjCARCOpt::Visit(Function &F,
3140 DenseMap<const BasicBlock *, BBState> &BBStates,
3141 MapVector<Value *, RRInfo> &Retains,
3142 DenseMap<Value *, RRInfo> &Releases) {
3144 // Use reverse-postorder traversals, because we magically know that loops
3145 // will be well behaved, i.e. they won't repeatedly call retain on a single
3146 // pointer without doing a release. We can't use the ReversePostOrderTraversal
3147 // class here because we want the reverse-CFG postorder to consider each
3148 // function exit point, and we want to ignore selected cycle edges.
3149 SmallVector<BasicBlock *, 16> PostOrder;
3150 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
3151 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
3152 NoObjCARCExceptionsMDKind,
3155 // Use reverse-postorder on the reverse CFG for bottom-up.
3156 bool BottomUpNestingDetected = false;
3157 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
3158 ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
3160 BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
3162 // Use reverse-postorder for top-down.
3163 bool TopDownNestingDetected = false;
3164 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
3165 PostOrder.rbegin(), E = PostOrder.rend();
3167 TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
3169 return TopDownNestingDetected && BottomUpNestingDetected;
3172 /// MoveCalls - Move the calls in RetainsToMove and ReleasesToMove.
3173 void ObjCARCOpt::MoveCalls(Value *Arg,
3174 RRInfo &RetainsToMove,
3175 RRInfo &ReleasesToMove,
3176 MapVector<Value *, RRInfo> &Retains,
3177 DenseMap<Value *, RRInfo> &Releases,
3178 SmallVectorImpl<Instruction *> &DeadInsts,
3180 Type *ArgTy = Arg->getType();
3181 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
3183 // Insert the new retain and release calls.
3184 for (SmallPtrSet<Instruction *, 2>::const_iterator
3185 PI = ReleasesToMove.ReverseInsertPts.begin(),
3186 PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
3187 Instruction *InsertPt = *PI;
3188 Value *MyArg = ArgTy == ParamTy ? Arg :
3189 new BitCastInst(Arg, ParamTy, "", InsertPt);
3191 CallInst::Create(RetainsToMove.IsRetainBlock ?
3192 getRetainBlockCallee(M) : getRetainCallee(M),
3193 MyArg, "", InsertPt);
3194 Call->setDoesNotThrow();
3195 if (RetainsToMove.IsRetainBlock)
3196 Call->setMetadata(CopyOnEscapeMDKind,
3197 MDNode::get(M->getContext(), ArrayRef<Value *>()));
3199 Call->setTailCall();
3201 for (SmallPtrSet<Instruction *, 2>::const_iterator
3202 PI = RetainsToMove.ReverseInsertPts.begin(),
3203 PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
3204 Instruction *InsertPt = *PI;
3205 Value *MyArg = ArgTy == ParamTy ? Arg :
3206 new BitCastInst(Arg, ParamTy, "", InsertPt);
3207 CallInst *Call = CallInst::Create(getReleaseCallee(M), MyArg,
3209 // Attach a clang.imprecise_release metadata tag, if appropriate.
3210 if (MDNode *M = ReleasesToMove.ReleaseMetadata)
3211 Call->setMetadata(ImpreciseReleaseMDKind, M);
3212 Call->setDoesNotThrow();
3213 if (ReleasesToMove.IsTailCallRelease)
3214 Call->setTailCall();
3217 // Delete the original retain and release calls.
3218 for (SmallPtrSet<Instruction *, 2>::const_iterator
3219 AI = RetainsToMove.Calls.begin(),
3220 AE = RetainsToMove.Calls.end(); AI != AE; ++AI) {
3221 Instruction *OrigRetain = *AI;
3222 Retains.blot(OrigRetain);
3223 DeadInsts.push_back(OrigRetain);
3225 for (SmallPtrSet<Instruction *, 2>::const_iterator
3226 AI = ReleasesToMove.Calls.begin(),
3227 AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) {
3228 Instruction *OrigRelease = *AI;
3229 Releases.erase(OrigRelease);
3230 DeadInsts.push_back(OrigRelease);
3234 /// PerformCodePlacement - Identify pairings between the retains and releases,
3235 /// and delete and/or move them.
3237 ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
3239 MapVector<Value *, RRInfo> &Retains,
3240 DenseMap<Value *, RRInfo> &Releases,
3242 bool AnyPairsCompletelyEliminated = false;
3243 RRInfo RetainsToMove;
3244 RRInfo ReleasesToMove;
3245 SmallVector<Instruction *, 4> NewRetains;
3246 SmallVector<Instruction *, 4> NewReleases;
3247 SmallVector<Instruction *, 8> DeadInsts;
3249 // Visit each retain.
3250 for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
3251 E = Retains.end(); I != E; ++I) {
3252 Value *V = I->first;
3253 if (!V) continue; // blotted
3255 Instruction *Retain = cast<Instruction>(V);
3256 Value *Arg = GetObjCArg(Retain);
3258 // If the object being released is in static or stack storage, we know it's
3259 // not being managed by ObjC reference counting, so we can delete pairs
3260 // regardless of what possible decrements or uses lie between them.
3261 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
3263 // A constant pointer can't be pointing to an object on the heap. It may
3264 // be reference-counted, but it won't be deleted.
3265 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
3266 if (const GlobalVariable *GV =
3267 dyn_cast<GlobalVariable>(
3268 StripPointerCastsAndObjCCalls(LI->getPointerOperand())))
3269 if (GV->isConstant())
3272 // If a pair happens in a region where it is known that the reference count
3273 // is already incremented, we can similarly ignore possible decrements.
3274 bool KnownSafeTD = true, KnownSafeBU = true;
3276 // Connect the dots between the top-down-collected RetainsToMove and
3277 // bottom-up-collected ReleasesToMove to form sets of related calls.
3278 // This is an iterative process so that we connect multiple releases
3279 // to multiple retains if needed.
3280 unsigned OldDelta = 0;
3281 unsigned NewDelta = 0;
3282 unsigned OldCount = 0;
3283 unsigned NewCount = 0;
3284 bool FirstRelease = true;
3285 bool FirstRetain = true;
3286 NewRetains.push_back(Retain);
3288 for (SmallVectorImpl<Instruction *>::const_iterator
3289 NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
3290 Instruction *NewRetain = *NI;
3291 MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain);
3292 assert(It != Retains.end());
3293 const RRInfo &NewRetainRRI = It->second;
3294 KnownSafeTD &= NewRetainRRI.KnownSafe;
3295 for (SmallPtrSet<Instruction *, 2>::const_iterator
3296 LI = NewRetainRRI.Calls.begin(),
3297 LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) {
3298 Instruction *NewRetainRelease = *LI;
3299 DenseMap<Value *, RRInfo>::const_iterator Jt =
3300 Releases.find(NewRetainRelease);
3301 if (Jt == Releases.end())
3303 const RRInfo &NewRetainReleaseRRI = Jt->second;
3304 assert(NewRetainReleaseRRI.Calls.count(NewRetain));
3305 if (ReleasesToMove.Calls.insert(NewRetainRelease)) {
3307 BBStates[NewRetainRelease->getParent()].GetAllPathCount();
3309 // Merge the ReleaseMetadata and IsTailCallRelease values.
3311 ReleasesToMove.ReleaseMetadata =
3312 NewRetainReleaseRRI.ReleaseMetadata;
3313 ReleasesToMove.IsTailCallRelease =
3314 NewRetainReleaseRRI.IsTailCallRelease;
3315 FirstRelease = false;
3317 if (ReleasesToMove.ReleaseMetadata !=
3318 NewRetainReleaseRRI.ReleaseMetadata)
3319 ReleasesToMove.ReleaseMetadata = 0;
3320 if (ReleasesToMove.IsTailCallRelease !=
3321 NewRetainReleaseRRI.IsTailCallRelease)
3322 ReleasesToMove.IsTailCallRelease = false;
3325 // Collect the optimal insertion points.
3327 for (SmallPtrSet<Instruction *, 2>::const_iterator
3328 RI = NewRetainReleaseRRI.ReverseInsertPts.begin(),
3329 RE = NewRetainReleaseRRI.ReverseInsertPts.end();
3331 Instruction *RIP = *RI;
3332 if (ReleasesToMove.ReverseInsertPts.insert(RIP))
3333 NewDelta -= BBStates[RIP->getParent()].GetAllPathCount();
3335 NewReleases.push_back(NewRetainRelease);
3340 if (NewReleases.empty()) break;
3342 // Back the other way.
3343 for (SmallVectorImpl<Instruction *>::const_iterator
3344 NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
3345 Instruction *NewRelease = *NI;
3346 DenseMap<Value *, RRInfo>::const_iterator It =
3347 Releases.find(NewRelease);
3348 assert(It != Releases.end());
3349 const RRInfo &NewReleaseRRI = It->second;
3350 KnownSafeBU &= NewReleaseRRI.KnownSafe;
3351 for (SmallPtrSet<Instruction *, 2>::const_iterator
3352 LI = NewReleaseRRI.Calls.begin(),
3353 LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) {
3354 Instruction *NewReleaseRetain = *LI;
3355 MapVector<Value *, RRInfo>::const_iterator Jt =
3356 Retains.find(NewReleaseRetain);
3357 if (Jt == Retains.end())
3359 const RRInfo &NewReleaseRetainRRI = Jt->second;
3360 assert(NewReleaseRetainRRI.Calls.count(NewRelease));
3361 if (RetainsToMove.Calls.insert(NewReleaseRetain)) {
3362 unsigned PathCount =
3363 BBStates[NewReleaseRetain->getParent()].GetAllPathCount();
3364 OldDelta += PathCount;
3365 OldCount += PathCount;
3367 // Merge the IsRetainBlock values.
3369 RetainsToMove.IsRetainBlock = NewReleaseRetainRRI.IsRetainBlock;
3370 FirstRetain = false;
3371 } else if (ReleasesToMove.IsRetainBlock !=
3372 NewReleaseRetainRRI.IsRetainBlock)
3373 // It's not possible to merge the sequences if one uses
3374 // objc_retain and the other uses objc_retainBlock.
3377 // Collect the optimal insertion points.
3379 for (SmallPtrSet<Instruction *, 2>::const_iterator
3380 RI = NewReleaseRetainRRI.ReverseInsertPts.begin(),
3381 RE = NewReleaseRetainRRI.ReverseInsertPts.end();
3383 Instruction *RIP = *RI;
3384 if (RetainsToMove.ReverseInsertPts.insert(RIP)) {
3385 PathCount = BBStates[RIP->getParent()].GetAllPathCount();
3386 NewDelta += PathCount;
3387 NewCount += PathCount;
3390 NewRetains.push_back(NewReleaseRetain);
3394 NewReleases.clear();
3395 if (NewRetains.empty()) break;
3398 // If the pointer is known incremented or nested, we can safely delete the
3399 // pair regardless of what's between them.
3400 if (KnownSafeTD || KnownSafeBU) {
3401 RetainsToMove.ReverseInsertPts.clear();
3402 ReleasesToMove.ReverseInsertPts.clear();
3405 // Determine whether the new insertion points we computed preserve the
3406 // balance of retain and release calls through the program.
3407 // TODO: If the fully aggressive solution isn't valid, try to find a
3408 // less aggressive solution which is.
3413 // Determine whether the original call points are balanced in the retain and
3414 // release calls through the program. If not, conservatively don't touch
3416 // TODO: It's theoretically possible to do code motion in this case, as
3417 // long as the existing imbalances are maintained.
3421 // Ok, everything checks out and we're all set. Let's move some code!
3423 assert(OldCount != 0 && "Unreachable code?");
3424 AnyPairsCompletelyEliminated = NewCount == 0;
3425 NumRRs += OldCount - NewCount;
3426 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
3427 Retains, Releases, DeadInsts, M);
3430 NewReleases.clear();
3432 RetainsToMove.clear();
3433 ReleasesToMove.clear();
3436 // Now that we're done moving everything, we can delete the newly dead
3437 // instructions, as we no longer need them as insert points.
3438 while (!DeadInsts.empty())
3439 EraseInstruction(DeadInsts.pop_back_val());
3441 return AnyPairsCompletelyEliminated;
3444 /// OptimizeWeakCalls - Weak pointer optimizations.
3445 void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
3446 // First, do memdep-style RLE and S2L optimizations. We can't use memdep
3447 // itself because it uses AliasAnalysis and we need to do provenance
3449 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
3450 Instruction *Inst = &*I++;
3452 DEBUG(dbgs() << "ObjCARCOpt::OptimizeWeakCalls: Visiting: " << *Inst <<
3455 InstructionClass Class = GetBasicInstructionClass(Inst);
3456 if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained)
3459 // Delete objc_loadWeak calls with no users.
3460 if (Class == IC_LoadWeak && Inst->use_empty()) {
3461 Inst->eraseFromParent();
3465 // TODO: For now, just look for an earlier available version of this value
3466 // within the same block. Theoretically, we could do memdep-style non-local
3467 // analysis too, but that would want caching. A better approach would be to
3468 // use the technique that EarlyCSE uses.
3469 inst_iterator Current = llvm::prior(I);
3470 BasicBlock *CurrentBB = Current.getBasicBlockIterator();
3471 for (BasicBlock::iterator B = CurrentBB->begin(),
3472 J = Current.getInstructionIterator();
3474 Instruction *EarlierInst = &*llvm::prior(J);
3475 InstructionClass EarlierClass = GetInstructionClass(EarlierInst);
3476 switch (EarlierClass) {
3478 case IC_LoadWeakRetained: {
3479 // If this is loading from the same pointer, replace this load's value
3481 CallInst *Call = cast<CallInst>(Inst);
3482 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
3483 Value *Arg = Call->getArgOperand(0);
3484 Value *EarlierArg = EarlierCall->getArgOperand(0);
3485 switch (PA.getAA()->alias(Arg, EarlierArg)) {
3486 case AliasAnalysis::MustAlias:
3488 // If the load has a builtin retain, insert a plain retain for it.
3489 if (Class == IC_LoadWeakRetained) {
3491 CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
3495 // Zap the fully redundant load.
3496 Call->replaceAllUsesWith(EarlierCall);
3497 Call->eraseFromParent();
3499 case AliasAnalysis::MayAlias:
3500 case AliasAnalysis::PartialAlias:
3502 case AliasAnalysis::NoAlias:
3509 // If this is storing to the same pointer and has the same size etc.
3510 // replace this load's value with the stored value.
3511 CallInst *Call = cast<CallInst>(Inst);
3512 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
3513 Value *Arg = Call->getArgOperand(0);
3514 Value *EarlierArg = EarlierCall->getArgOperand(0);
3515 switch (PA.getAA()->alias(Arg, EarlierArg)) {
3516 case AliasAnalysis::MustAlias:
3518 // If the load has a builtin retain, insert a plain retain for it.
3519 if (Class == IC_LoadWeakRetained) {
3521 CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
3525 // Zap the fully redundant load.
3526 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
3527 Call->eraseFromParent();
3529 case AliasAnalysis::MayAlias:
3530 case AliasAnalysis::PartialAlias:
3532 case AliasAnalysis::NoAlias:
3539 // TOOD: Grab the copied value.
3541 case IC_AutoreleasepoolPush:
3544 // Weak pointers are only modified through the weak entry points
3545 // (and arbitrary calls, which could call the weak entry points).
3548 // Anything else could modify the weak pointer.
3555 // Then, for each destroyWeak with an alloca operand, check to see if
3556 // the alloca and all its users can be zapped.
3557 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
3558 Instruction *Inst = &*I++;
3559 InstructionClass Class = GetBasicInstructionClass(Inst);
3560 if (Class != IC_DestroyWeak)
3563 CallInst *Call = cast<CallInst>(Inst);
3564 Value *Arg = Call->getArgOperand(0);
3565 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
3566 for (Value::use_iterator UI = Alloca->use_begin(),
3567 UE = Alloca->use_end(); UI != UE; ++UI) {
3568 const Instruction *UserInst = cast<Instruction>(*UI);
3569 switch (GetBasicInstructionClass(UserInst)) {
3572 case IC_DestroyWeak:
3579 for (Value::use_iterator UI = Alloca->use_begin(),
3580 UE = Alloca->use_end(); UI != UE; ) {
3581 CallInst *UserInst = cast<CallInst>(*UI++);
3582 switch (GetBasicInstructionClass(UserInst)) {
3585 // These functions return their second argument.
3586 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
3588 case IC_DestroyWeak:
3592 llvm_unreachable("alloca really is used!");
3594 UserInst->eraseFromParent();
3596 Alloca->eraseFromParent();
3601 DEBUG(dbgs() << "ObjCARCOpt::OptimizeWeakCalls: Finished List.\n\n");
3605 /// OptimizeSequences - Identify program paths which execute sequences of
3606 /// retains and releases which can be eliminated.
3607 bool ObjCARCOpt::OptimizeSequences(Function &F) {
3608 /// Releases, Retains - These are used to store the results of the main flow
3609 /// analysis. These use Value* as the key instead of Instruction* so that the
3610 /// map stays valid when we get around to rewriting code and calls get
3611 /// replaced by arguments.
3612 DenseMap<Value *, RRInfo> Releases;
3613 MapVector<Value *, RRInfo> Retains;
3615 /// BBStates, This is used during the traversal of the function to track the
3616 /// states for each identified object at each block.
3617 DenseMap<const BasicBlock *, BBState> BBStates;
3619 // Analyze the CFG of the function, and all instructions.
3620 bool NestingDetected = Visit(F, BBStates, Retains, Releases);
3623 return PerformCodePlacement(BBStates, Retains, Releases, F.getParent()) &&
3627 /// OptimizeReturns - Look for this pattern:
3629 /// %call = call i8* @something(...)
3630 /// %2 = call i8* @objc_retain(i8* %call)
3631 /// %3 = call i8* @objc_autorelease(i8* %2)
3634 /// And delete the retain and autorelease.
3636 /// Otherwise if it's just this:
3638 /// %3 = call i8* @objc_autorelease(i8* %2)
3641 /// convert the autorelease to autoreleaseRV.
3642 void ObjCARCOpt::OptimizeReturns(Function &F) {
3643 if (!F.getReturnType()->isPointerTy())
3646 SmallPtrSet<Instruction *, 4> DependingInstructions;
3647 SmallPtrSet<const BasicBlock *, 4> Visited;
3648 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
3649 BasicBlock *BB = FI;
3650 ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
3652 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Visiting: " << *Ret << "\n");
3656 const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0));
3657 FindDependencies(NeedsPositiveRetainCount, Arg,
3658 BB, Ret, DependingInstructions, Visited, PA);
3659 if (DependingInstructions.size() != 1)
3663 CallInst *Autorelease =
3664 dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3667 InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
3668 if (!IsAutorelease(AutoreleaseClass))
3670 if (GetObjCArg(Autorelease) != Arg)
3673 DependingInstructions.clear();
3676 // Check that there is nothing that can affect the reference
3677 // count between the autorelease and the retain.
3678 FindDependencies(CanChangeRetainCount, Arg,
3679 BB, Autorelease, DependingInstructions, Visited, PA);
3680 if (DependingInstructions.size() != 1)
3685 dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3687 // Check that we found a retain with the same argument.
3689 !IsRetain(GetBasicInstructionClass(Retain)) ||
3690 GetObjCArg(Retain) != Arg)
3693 DependingInstructions.clear();
3696 // Convert the autorelease to an autoreleaseRV, since it's
3697 // returning the value.
3698 if (AutoreleaseClass == IC_Autorelease) {
3699 Autorelease->setCalledFunction(getAutoreleaseRVCallee(F.getParent()));
3700 AutoreleaseClass = IC_AutoreleaseRV;
3703 // Check that there is nothing that can affect the reference
3704 // count between the retain and the call.
3705 // Note that Retain need not be in BB.
3706 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
3707 DependingInstructions, Visited, PA);
3708 if (DependingInstructions.size() != 1)
3713 dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3715 // Check that the pointer is the return value of the call.
3716 if (!Call || Arg != Call)
3719 // Check that the call is a regular call.
3720 InstructionClass Class = GetBasicInstructionClass(Call);
3721 if (Class != IC_CallOrUser && Class != IC_Call)
3724 // If so, we can zap the retain and autorelease.
3727 EraseInstruction(Retain);
3728 EraseInstruction(Autorelease);
3734 DependingInstructions.clear();
3738 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Finished List.\n\n");
3742 bool ObjCARCOpt::doInitialization(Module &M) {
3746 // If nothing in the Module uses ARC, don't do anything.
3747 Run = ModuleHasARC(M);
3751 // Identify the imprecise release metadata kind.
3752 ImpreciseReleaseMDKind =
3753 M.getContext().getMDKindID("clang.imprecise_release");
3754 CopyOnEscapeMDKind =
3755 M.getContext().getMDKindID("clang.arc.copy_on_escape");
3756 NoObjCARCExceptionsMDKind =
3757 M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
3759 // Intuitively, objc_retain and others are nocapture, however in practice
3760 // they are not, because they return their argument value. And objc_release
3761 // calls finalizers which can have arbitrary side effects.
3763 // These are initialized lazily.
3765 AutoreleaseRVCallee = 0;
3768 RetainBlockCallee = 0;
3769 AutoreleaseCallee = 0;
3774 bool ObjCARCOpt::runOnFunction(Function &F) {
3778 // If nothing in the Module uses ARC, don't do anything.
3784 PA.setAA(&getAnalysis<AliasAnalysis>());
3786 // This pass performs several distinct transformations. As a compile-time aid
3787 // when compiling code that isn't ObjC, skip these if the relevant ObjC
3788 // library functions aren't declared.
3790 // Preliminary optimizations. This also computs UsedInThisFunction.
3791 OptimizeIndividualCalls(F);
3793 // Optimizations for weak pointers.
3794 if (UsedInThisFunction & ((1 << IC_LoadWeak) |
3795 (1 << IC_LoadWeakRetained) |
3796 (1 << IC_StoreWeak) |
3797 (1 << IC_InitWeak) |
3798 (1 << IC_CopyWeak) |
3799 (1 << IC_MoveWeak) |
3800 (1 << IC_DestroyWeak)))
3801 OptimizeWeakCalls(F);
3803 // Optimizations for retain+release pairs.
3804 if (UsedInThisFunction & ((1 << IC_Retain) |
3805 (1 << IC_RetainRV) |
3806 (1 << IC_RetainBlock)))
3807 if (UsedInThisFunction & (1 << IC_Release))
3808 // Run OptimizeSequences until it either stops making changes or
3809 // no retain+release pair nesting is detected.
3810 while (OptimizeSequences(F)) {}
3812 // Optimizations if objc_autorelease is used.
3813 if (UsedInThisFunction & ((1 << IC_Autorelease) |
3814 (1 << IC_AutoreleaseRV)))
3820 void ObjCARCOpt::releaseMemory() {
3824 //===----------------------------------------------------------------------===//
3826 //===----------------------------------------------------------------------===//
3828 // TODO: ObjCARCContract could insert PHI nodes when uses aren't
3829 // dominated by single calls.
3831 #include "llvm/Analysis/Dominators.h"
3832 #include "llvm/IR/InlineAsm.h"
3833 #include "llvm/IR/Operator.h"
3835 STATISTIC(NumStoreStrongs, "Number objc_storeStrong calls formed");
3838 /// ObjCARCContract - Late ARC optimizations. These change the IR in a way
3839 /// that makes it difficult to be analyzed by ObjCARCOpt, so it's run late.
3840 class ObjCARCContract : public FunctionPass {
3844 ProvenanceAnalysis PA;
3846 /// Run - A flag indicating whether this optimization pass should run.
3849 /// StoreStrongCallee, etc. - Declarations for ObjC runtime
3850 /// functions, for use in creating calls to them. These are initialized
3851 /// lazily to avoid cluttering up the Module with unused declarations.
3852 Constant *StoreStrongCallee,
3853 *RetainAutoreleaseCallee, *RetainAutoreleaseRVCallee;
3855 /// RetainRVMarker - The inline asm string to insert between calls and
3856 /// RetainRV calls to make the optimization work on targets which need it.
3857 const MDString *RetainRVMarker;
3859 /// StoreStrongCalls - The set of inserted objc_storeStrong calls. If
3860 /// at the end of walking the function we have found no alloca
3861 /// instructions, these calls can be marked "tail".
3862 SmallPtrSet<CallInst *, 8> StoreStrongCalls;
3864 Constant *getStoreStrongCallee(Module *M);
3865 Constant *getRetainAutoreleaseCallee(Module *M);
3866 Constant *getRetainAutoreleaseRVCallee(Module *M);
3868 bool ContractAutorelease(Function &F, Instruction *Autorelease,
3869 InstructionClass Class,
3870 SmallPtrSet<Instruction *, 4>
3871 &DependingInstructions,
3872 SmallPtrSet<const BasicBlock *, 4>
3875 void ContractRelease(Instruction *Release,
3876 inst_iterator &Iter);
3878 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
3879 virtual bool doInitialization(Module &M);
3880 virtual bool runOnFunction(Function &F);
3884 ObjCARCContract() : FunctionPass(ID) {
3885 initializeObjCARCContractPass(*PassRegistry::getPassRegistry());
3890 char ObjCARCContract::ID = 0;
3891 INITIALIZE_PASS_BEGIN(ObjCARCContract,
3892 "objc-arc-contract", "ObjC ARC contraction", false, false)
3893 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
3894 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
3895 INITIALIZE_PASS_END(ObjCARCContract,
3896 "objc-arc-contract", "ObjC ARC contraction", false, false)
3898 Pass *llvm::createObjCARCContractPass() {
3899 return new ObjCARCContract();
3902 void ObjCARCContract::getAnalysisUsage(AnalysisUsage &AU) const {
3903 AU.addRequired<AliasAnalysis>();
3904 AU.addRequired<DominatorTree>();
3905 AU.setPreservesCFG();
3908 Constant *ObjCARCContract::getStoreStrongCallee(Module *M) {
3909 if (!StoreStrongCallee) {
3910 LLVMContext &C = M->getContext();
3911 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
3912 Type *I8XX = PointerType::getUnqual(I8X);
3913 Type *Params[] = { I8XX, I8X };
3915 AttributeSet Attribute = AttributeSet()
3916 .addAttr(M->getContext(), AttributeSet::FunctionIndex,
3917 Attribute::get(C, Attribute::NoUnwind))
3918 .addAttr(M->getContext(), 1, Attribute::get(C, Attribute::NoCapture));
3921 M->getOrInsertFunction(
3923 FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
3926 return StoreStrongCallee;
3929 Constant *ObjCARCContract::getRetainAutoreleaseCallee(Module *M) {
3930 if (!RetainAutoreleaseCallee) {
3931 LLVMContext &C = M->getContext();
3932 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
3933 Type *Params[] = { I8X };
3934 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
3935 AttributeSet Attribute =
3936 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
3937 Attribute::get(C, Attribute::NoUnwind));
3938 RetainAutoreleaseCallee =
3939 M->getOrInsertFunction("objc_retainAutorelease", FTy, Attribute);
3941 return RetainAutoreleaseCallee;
3944 Constant *ObjCARCContract::getRetainAutoreleaseRVCallee(Module *M) {
3945 if (!RetainAutoreleaseRVCallee) {
3946 LLVMContext &C = M->getContext();
3947 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
3948 Type *Params[] = { I8X };
3949 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
3950 AttributeSet Attribute =
3951 AttributeSet().addAttr(M->getContext(), AttributeSet::FunctionIndex,
3952 Attribute::get(C, Attribute::NoUnwind));
3953 RetainAutoreleaseRVCallee =
3954 M->getOrInsertFunction("objc_retainAutoreleaseReturnValue", FTy,
3957 return RetainAutoreleaseRVCallee;
3960 /// ContractAutorelease - Merge an autorelease with a retain into a fused call.
3962 ObjCARCContract::ContractAutorelease(Function &F, Instruction *Autorelease,
3963 InstructionClass Class,
3964 SmallPtrSet<Instruction *, 4>
3965 &DependingInstructions,
3966 SmallPtrSet<const BasicBlock *, 4>
3968 const Value *Arg = GetObjCArg(Autorelease);
3970 // Check that there are no instructions between the retain and the autorelease
3971 // (such as an autorelease_pop) which may change the count.
3972 CallInst *Retain = 0;
3973 if (Class == IC_AutoreleaseRV)
3974 FindDependencies(RetainAutoreleaseRVDep, Arg,
3975 Autorelease->getParent(), Autorelease,
3976 DependingInstructions, Visited, PA);
3978 FindDependencies(RetainAutoreleaseDep, Arg,
3979 Autorelease->getParent(), Autorelease,
3980 DependingInstructions, Visited, PA);
3983 if (DependingInstructions.size() != 1) {
3984 DependingInstructions.clear();
3988 Retain = dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3989 DependingInstructions.clear();
3992 GetBasicInstructionClass(Retain) != IC_Retain ||
3993 GetObjCArg(Retain) != Arg)
3999 if (Class == IC_AutoreleaseRV)
4000 Retain->setCalledFunction(getRetainAutoreleaseRVCallee(F.getParent()));
4002 Retain->setCalledFunction(getRetainAutoreleaseCallee(F.getParent()));
4004 EraseInstruction(Autorelease);
4008 /// ContractRelease - Attempt to merge an objc_release with a store, load, and
4009 /// objc_retain to form an objc_storeStrong. This can be a little tricky because
4010 /// the instructions don't always appear in order, and there may be unrelated
4011 /// intervening instructions.
4012 void ObjCARCContract::ContractRelease(Instruction *Release,
4013 inst_iterator &Iter) {
4014 LoadInst *Load = dyn_cast<LoadInst>(GetObjCArg(Release));
4015 if (!Load || !Load->isSimple()) return;
4017 // For now, require everything to be in one basic block.
4018 BasicBlock *BB = Release->getParent();
4019 if (Load->getParent() != BB) return;
4021 // Walk down to find the store and the release, which may be in either order.
4022 BasicBlock::iterator I = Load, End = BB->end();
4024 AliasAnalysis::Location Loc = AA->getLocation(Load);
4025 StoreInst *Store = 0;
4026 bool SawRelease = false;
4027 for (; !Store || !SawRelease; ++I) {
4031 Instruction *Inst = I;
4032 if (Inst == Release) {
4037 InstructionClass Class = GetBasicInstructionClass(Inst);
4039 // Unrelated retains are harmless.
4040 if (IsRetain(Class))
4044 // The store is the point where we're going to put the objc_storeStrong,
4045 // so make sure there are no uses after it.
4046 if (CanUse(Inst, Load, PA, Class))
4048 } else if (AA->getModRefInfo(Inst, Loc) & AliasAnalysis::Mod) {
4049 // We are moving the load down to the store, so check for anything
4050 // else which writes to the memory between the load and the store.
4051 Store = dyn_cast<StoreInst>(Inst);
4052 if (!Store || !Store->isSimple()) return;
4053 if (Store->getPointerOperand() != Loc.Ptr) return;
4057 Value *New = StripPointerCastsAndObjCCalls(Store->getValueOperand());
4059 // Walk up to find the retain.
4061 BasicBlock::iterator Begin = BB->begin();
4062 while (I != Begin && GetBasicInstructionClass(I) != IC_Retain)
4064 Instruction *Retain = I;
4065 if (GetBasicInstructionClass(Retain) != IC_Retain) return;
4066 if (GetObjCArg(Retain) != New) return;
4071 LLVMContext &C = Release->getContext();
4072 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
4073 Type *I8XX = PointerType::getUnqual(I8X);
4075 Value *Args[] = { Load->getPointerOperand(), New };
4076 if (Args[0]->getType() != I8XX)
4077 Args[0] = new BitCastInst(Args[0], I8XX, "", Store);
4078 if (Args[1]->getType() != I8X)
4079 Args[1] = new BitCastInst(Args[1], I8X, "", Store);
4080 CallInst *StoreStrong =
4081 CallInst::Create(getStoreStrongCallee(BB->getParent()->getParent()),
4083 StoreStrong->setDoesNotThrow();
4084 StoreStrong->setDebugLoc(Store->getDebugLoc());
4086 // We can't set the tail flag yet, because we haven't yet determined
4087 // whether there are any escaping allocas. Remember this call, so that
4088 // we can set the tail flag once we know it's safe.
4089 StoreStrongCalls.insert(StoreStrong);
4091 if (&*Iter == Store) ++Iter;
4092 Store->eraseFromParent();
4093 Release->eraseFromParent();
4094 EraseInstruction(Retain);
4095 if (Load->use_empty())
4096 Load->eraseFromParent();
4099 bool ObjCARCContract::doInitialization(Module &M) {
4100 // If nothing in the Module uses ARC, don't do anything.
4101 Run = ModuleHasARC(M);
4105 // These are initialized lazily.
4106 StoreStrongCallee = 0;
4107 RetainAutoreleaseCallee = 0;
4108 RetainAutoreleaseRVCallee = 0;
4110 // Initialize RetainRVMarker.
4112 if (NamedMDNode *NMD =
4113 M.getNamedMetadata("clang.arc.retainAutoreleasedReturnValueMarker"))
4114 if (NMD->getNumOperands() == 1) {
4115 const MDNode *N = NMD->getOperand(0);
4116 if (N->getNumOperands() == 1)
4117 if (const MDString *S = dyn_cast<MDString>(N->getOperand(0)))
4124 bool ObjCARCContract::runOnFunction(Function &F) {
4128 // If nothing in the Module uses ARC, don't do anything.
4133 AA = &getAnalysis<AliasAnalysis>();
4134 DT = &getAnalysis<DominatorTree>();
4136 PA.setAA(&getAnalysis<AliasAnalysis>());
4138 // Track whether it's ok to mark objc_storeStrong calls with the "tail"
4139 // keyword. Be conservative if the function has variadic arguments.
4140 // It seems that functions which "return twice" are also unsafe for the
4141 // "tail" argument, because they are setjmp, which could need to
4142 // return to an earlier stack state.
4143 bool TailOkForStoreStrongs = !F.isVarArg() &&
4144 !F.callsFunctionThatReturnsTwice();
4146 // For ObjC library calls which return their argument, replace uses of the
4147 // argument with uses of the call return value, if it dominates the use. This
4148 // reduces register pressure.
4149 SmallPtrSet<Instruction *, 4> DependingInstructions;
4150 SmallPtrSet<const BasicBlock *, 4> Visited;
4151 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
4152 Instruction *Inst = &*I++;
4154 DEBUG(dbgs() << "ObjCARCContract: Visiting: " << *Inst << "\n");
4156 // Only these library routines return their argument. In particular,
4157 // objc_retainBlock does not necessarily return its argument.
4158 InstructionClass Class = GetBasicInstructionClass(Inst);
4161 case IC_FusedRetainAutorelease:
4162 case IC_FusedRetainAutoreleaseRV:
4164 case IC_Autorelease:
4165 case IC_AutoreleaseRV:
4166 if (ContractAutorelease(F, Inst, Class, DependingInstructions, Visited))
4170 // If we're compiling for a target which needs a special inline-asm
4171 // marker to do the retainAutoreleasedReturnValue optimization,
4173 if (!RetainRVMarker)
4175 BasicBlock::iterator BBI = Inst;
4176 BasicBlock *InstParent = Inst->getParent();
4178 // Step up to see if the call immediately precedes the RetainRV call.
4179 // If it's an invoke, we have to cross a block boundary. And we have
4180 // to carefully dodge no-op instructions.
4182 if (&*BBI == InstParent->begin()) {
4183 BasicBlock *Pred = InstParent->getSinglePredecessor();
4185 goto decline_rv_optimization;
4186 BBI = Pred->getTerminator();
4190 } while (isNoopInstruction(BBI));
4192 if (&*BBI == GetObjCArg(Inst)) {
4193 DEBUG(dbgs() << "ObjCARCContract: Adding inline asm marker for "
4194 "retainAutoreleasedReturnValue optimization.\n");
4197 InlineAsm::get(FunctionType::get(Type::getVoidTy(Inst->getContext()),
4198 /*isVarArg=*/false),
4199 RetainRVMarker->getString(),
4200 /*Constraints=*/"", /*hasSideEffects=*/true);
4201 CallInst::Create(IA, "", Inst);
4203 decline_rv_optimization:
4207 // objc_initWeak(p, null) => *p = null
4208 CallInst *CI = cast<CallInst>(Inst);
4209 if (isNullOrUndef(CI->getArgOperand(1))) {
4211 ConstantPointerNull::get(cast<PointerType>(CI->getType()));
4213 new StoreInst(Null, CI->getArgOperand(0), CI);
4215 DEBUG(dbgs() << "OBJCARCContract: Old = " << *CI << "\n"
4216 << " New = " << *Null << "\n");
4218 CI->replaceAllUsesWith(Null);
4219 CI->eraseFromParent();
4224 ContractRelease(Inst, I);
4227 // Be conservative if the function has any alloca instructions.
4228 // Technically we only care about escaping alloca instructions,
4229 // but this is sufficient to handle some interesting cases.
4230 if (isa<AllocaInst>(Inst))
4231 TailOkForStoreStrongs = false;
4237 DEBUG(dbgs() << "ObjCARCContract: Finished List.\n\n");
4239 // Don't use GetObjCArg because we don't want to look through bitcasts
4240 // and such; to do the replacement, the argument must have type i8*.
4241 const Value *Arg = cast<CallInst>(Inst)->getArgOperand(0);
4243 // If we're compiling bugpointed code, don't get in trouble.
4244 if (!isa<Instruction>(Arg) && !isa<Argument>(Arg))
4246 // Look through the uses of the pointer.
4247 for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
4249 Use &U = UI.getUse();
4250 unsigned OperandNo = UI.getOperandNo();
4251 ++UI; // Increment UI now, because we may unlink its element.
4253 // If the call's return value dominates a use of the call's argument
4254 // value, rewrite the use to use the return value. We check for
4255 // reachability here because an unreachable call is considered to
4256 // trivially dominate itself, which would lead us to rewriting its
4257 // argument in terms of its return value, which would lead to
4258 // infinite loops in GetObjCArg.
4259 if (DT->isReachableFromEntry(U) && DT->dominates(Inst, U)) {
4261 Instruction *Replacement = Inst;
4262 Type *UseTy = U.get()->getType();
4263 if (PHINode *PHI = dyn_cast<PHINode>(U.getUser())) {
4264 // For PHI nodes, insert the bitcast in the predecessor block.
4265 unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo);
4266 BasicBlock *BB = PHI->getIncomingBlock(ValNo);
4267 if (Replacement->getType() != UseTy)
4268 Replacement = new BitCastInst(Replacement, UseTy, "",
4270 // While we're here, rewrite all edges for this PHI, rather
4271 // than just one use at a time, to minimize the number of
4272 // bitcasts we emit.
4273 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
4274 if (PHI->getIncomingBlock(i) == BB) {
4275 // Keep the UI iterator valid.
4276 if (&PHI->getOperandUse(
4277 PHINode::getOperandNumForIncomingValue(i)) ==
4280 PHI->setIncomingValue(i, Replacement);
4283 if (Replacement->getType() != UseTy)
4284 Replacement = new BitCastInst(Replacement, UseTy, "",
4285 cast<Instruction>(U.getUser()));
4291 // If Arg is a no-op casted pointer, strip one level of casts and iterate.
4292 if (const BitCastInst *BI = dyn_cast<BitCastInst>(Arg))
4293 Arg = BI->getOperand(0);
4294 else if (isa<GEPOperator>(Arg) &&
4295 cast<GEPOperator>(Arg)->hasAllZeroIndices())
4296 Arg = cast<GEPOperator>(Arg)->getPointerOperand();
4297 else if (isa<GlobalAlias>(Arg) &&
4298 !cast<GlobalAlias>(Arg)->mayBeOverridden())
4299 Arg = cast<GlobalAlias>(Arg)->getAliasee();
4305 // If this function has no escaping allocas or suspicious vararg usage,
4306 // objc_storeStrong calls can be marked with the "tail" keyword.
4307 if (TailOkForStoreStrongs)
4308 for (SmallPtrSet<CallInst *, 8>::iterator I = StoreStrongCalls.begin(),
4309 E = StoreStrongCalls.end(); I != E; ++I)
4310 (*I)->setTailCall();
4311 StoreStrongCalls.clear();