PageRenderTime 80ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 1ms

/3rd_party/llvm/lib/Transforms/Scalar/ObjCARC.cpp

https://code.google.com/p/softart/
C++ | 4200 lines | 2932 code | 476 blank | 792 comment | 735 complexity | aa4c947e6cc65c3b84e595323f9f8c9a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, JSON, MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, BSD-2-Clause
  1. //===- ObjCARC.cpp - ObjC ARC Optimization --------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines ObjC ARC optimizations. ARC stands for
  11. // Automatic Reference Counting and is a system for managing reference counts
  12. // for objects in Objective C.
  13. //
  14. // The optimizations performed include elimination of redundant, partially
  15. // redundant, and inconsequential reference count operations, elimination of
  16. // redundant weak pointer operations, pattern-matching and replacement of
  17. // low-level operations into higher-level operations, and numerous minor
  18. // simplifications.
  19. //
  20. // This file also defines a simple ARC-aware AliasAnalysis.
  21. //
  22. // WARNING: This file knows about certain library functions. It recognizes them
  23. // by name, and hardwires knowledge of their semantics.
  24. //
  25. // WARNING: This file knows about how certain Objective-C library functions are
  26. // used. Naive LLVM IR transformations which would otherwise be
  27. // behavior-preserving may break these assumptions.
  28. //
  29. //===----------------------------------------------------------------------===//
  30. #define DEBUG_TYPE "objc-arc"
  31. #include "llvm/Support/CommandLine.h"
  32. #include "llvm/ADT/DenseMap.h"
  33. using namespace llvm;
  34. // A handy option to enable/disable all optimizations in this file.
  35. static cl::opt<bool> EnableARCOpts("enable-objc-arc-opts", cl::init(true));
  36. //===----------------------------------------------------------------------===//
  37. // Misc. Utilities
  38. //===----------------------------------------------------------------------===//
  39. namespace {
  40. /// MapVector - An associative container with fast insertion-order
  41. /// (deterministic) iteration over its elements. Plus the special
  42. /// blot operation.
  43. template<class KeyT, class ValueT>
  44. class MapVector {
  45. /// Map - Map keys to indices in Vector.
  46. typedef DenseMap<KeyT, size_t> MapTy;
  47. MapTy Map;
  48. /// Vector - Keys and values.
  49. typedef std::vector<std::pair<KeyT, ValueT> > VectorTy;
  50. VectorTy Vector;
  51. public:
  52. typedef typename VectorTy::iterator iterator;
  53. typedef typename VectorTy::const_iterator const_iterator;
  54. iterator begin() { return Vector.begin(); }
  55. iterator end() { return Vector.end(); }
  56. const_iterator begin() const { return Vector.begin(); }
  57. const_iterator end() const { return Vector.end(); }
  58. #ifdef XDEBUG
  59. ~MapVector() {
  60. assert(Vector.size() >= Map.size()); // May differ due to blotting.
  61. for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
  62. I != E; ++I) {
  63. assert(I->second < Vector.size());
  64. assert(Vector[I->second].first == I->first);
  65. }
  66. for (typename VectorTy::const_iterator I = Vector.begin(),
  67. E = Vector.end(); I != E; ++I)
  68. assert(!I->first ||
  69. (Map.count(I->first) &&
  70. Map[I->first] == size_t(I - Vector.begin())));
  71. }
  72. #endif
  73. ValueT &operator[](const KeyT &Arg) {
  74. std::pair<typename MapTy::iterator, bool> Pair =
  75. Map.insert(std::make_pair(Arg, size_t(0)));
  76. if (Pair.second) {
  77. size_t Num = Vector.size();
  78. Pair.first->second = Num;
  79. Vector.push_back(std::make_pair(Arg, ValueT()));
  80. return Vector[Num].second;
  81. }
  82. return Vector[Pair.first->second].second;
  83. }
  84. std::pair<iterator, bool>
  85. insert(const std::pair<KeyT, ValueT> &InsertPair) {
  86. std::pair<typename MapTy::iterator, bool> Pair =
  87. Map.insert(std::make_pair(InsertPair.first, size_t(0)));
  88. if (Pair.second) {
  89. size_t Num = Vector.size();
  90. Pair.first->second = Num;
  91. Vector.push_back(InsertPair);
  92. return std::make_pair(Vector.begin() + Num, true);
  93. }
  94. return std::make_pair(Vector.begin() + Pair.first->second, false);
  95. }
  96. const_iterator find(const KeyT &Key) const {
  97. typename MapTy::const_iterator It = Map.find(Key);
  98. if (It == Map.end()) return Vector.end();
  99. return Vector.begin() + It->second;
  100. }
  101. /// blot - This is similar to erase, but instead of removing the element
  102. /// from the vector, it just zeros out the key in the vector. This leaves
  103. /// iterators intact, but clients must be prepared for zeroed-out keys when
  104. /// iterating.
  105. void blot(const KeyT &Key) {
  106. typename MapTy::iterator It = Map.find(Key);
  107. if (It == Map.end()) return;
  108. Vector[It->second].first = KeyT();
  109. Map.erase(It);
  110. }
  111. void clear() {
  112. Map.clear();
  113. Vector.clear();
  114. }
  115. };
  116. }
  117. //===----------------------------------------------------------------------===//
  118. // ARC Utilities.
  119. //===----------------------------------------------------------------------===//
  120. #include "llvm/Intrinsics.h"
  121. #include "llvm/Module.h"
  122. #include "llvm/Analysis/ValueTracking.h"
  123. #include "llvm/Transforms/Utils/Local.h"
  124. #include "llvm/Support/CallSite.h"
  125. #include "llvm/ADT/StringSwitch.h"
  126. namespace {
  127. /// InstructionClass - A simple classification for instructions.
  128. enum InstructionClass {
  129. IC_Retain, ///< objc_retain
  130. IC_RetainRV, ///< objc_retainAutoreleasedReturnValue
  131. IC_RetainBlock, ///< objc_retainBlock
  132. IC_Release, ///< objc_release
  133. IC_Autorelease, ///< objc_autorelease
  134. IC_AutoreleaseRV, ///< objc_autoreleaseReturnValue
  135. IC_AutoreleasepoolPush, ///< objc_autoreleasePoolPush
  136. IC_AutoreleasepoolPop, ///< objc_autoreleasePoolPop
  137. IC_NoopCast, ///< objc_retainedObject, etc.
  138. IC_FusedRetainAutorelease, ///< objc_retainAutorelease
  139. IC_FusedRetainAutoreleaseRV, ///< objc_retainAutoreleaseReturnValue
  140. IC_LoadWeakRetained, ///< objc_loadWeakRetained (primitive)
  141. IC_StoreWeak, ///< objc_storeWeak (primitive)
  142. IC_InitWeak, ///< objc_initWeak (derived)
  143. IC_LoadWeak, ///< objc_loadWeak (derived)
  144. IC_MoveWeak, ///< objc_moveWeak (derived)
  145. IC_CopyWeak, ///< objc_copyWeak (derived)
  146. IC_DestroyWeak, ///< objc_destroyWeak (derived)
  147. IC_StoreStrong, ///< objc_storeStrong (derived)
  148. IC_CallOrUser, ///< could call objc_release and/or "use" pointers
  149. IC_Call, ///< could call objc_release
  150. IC_User, ///< could "use" a pointer
  151. IC_None ///< anything else
  152. };
  153. }
  154. /// IsPotentialUse - Test whether the given value is possible a
  155. /// reference-counted pointer.
  156. static bool IsPotentialUse(const Value *Op) {
  157. // Pointers to static or stack storage are not reference-counted pointers.
  158. if (isa<Constant>(Op) || isa<AllocaInst>(Op))
  159. return false;
  160. // Special arguments are not reference-counted.
  161. if (const Argument *Arg = dyn_cast<Argument>(Op))
  162. if (Arg->hasByValAttr() ||
  163. Arg->hasNestAttr() ||
  164. Arg->hasStructRetAttr())
  165. return false;
  166. // Only consider values with pointer types.
  167. // It seemes intuitive to exclude function pointer types as well, since
  168. // functions are never reference-counted, however clang occasionally
  169. // bitcasts reference-counted pointers to function-pointer type
  170. // temporarily.
  171. PointerType *Ty = dyn_cast<PointerType>(Op->getType());
  172. if (!Ty)
  173. return false;
  174. // Conservatively assume anything else is a potential use.
  175. return true;
  176. }
  177. /// GetCallSiteClass - Helper for GetInstructionClass. Determines what kind
  178. /// of construct CS is.
  179. static InstructionClass GetCallSiteClass(ImmutableCallSite CS) {
  180. for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
  181. I != E; ++I)
  182. if (IsPotentialUse(*I))
  183. return CS.onlyReadsMemory() ? IC_User : IC_CallOrUser;
  184. return CS.onlyReadsMemory() ? IC_None : IC_Call;
  185. }
  186. /// GetFunctionClass - Determine if F is one of the special known Functions.
  187. /// If it isn't, return IC_CallOrUser.
  188. static InstructionClass GetFunctionClass(const Function *F) {
  189. Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
  190. // No arguments.
  191. if (AI == AE)
  192. return StringSwitch<InstructionClass>(F->getName())
  193. .Case("objc_autoreleasePoolPush", IC_AutoreleasepoolPush)
  194. .Default(IC_CallOrUser);
  195. // One argument.
  196. const Argument *A0 = AI++;
  197. if (AI == AE)
  198. // Argument is a pointer.
  199. if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) {
  200. Type *ETy = PTy->getElementType();
  201. // Argument is i8*.
  202. if (ETy->isIntegerTy(8))
  203. return StringSwitch<InstructionClass>(F->getName())
  204. .Case("objc_retain", IC_Retain)
  205. .Case("objc_retainAutoreleasedReturnValue", IC_RetainRV)
  206. .Case("objc_retainBlock", IC_RetainBlock)
  207. .Case("objc_release", IC_Release)
  208. .Case("objc_autorelease", IC_Autorelease)
  209. .Case("objc_autoreleaseReturnValue", IC_AutoreleaseRV)
  210. .Case("objc_autoreleasePoolPop", IC_AutoreleasepoolPop)
  211. .Case("objc_retainedObject", IC_NoopCast)
  212. .Case("objc_unretainedObject", IC_NoopCast)
  213. .Case("objc_unretainedPointer", IC_NoopCast)
  214. .Case("objc_retain_autorelease", IC_FusedRetainAutorelease)
  215. .Case("objc_retainAutorelease", IC_FusedRetainAutorelease)
  216. .Case("objc_retainAutoreleaseReturnValue",IC_FusedRetainAutoreleaseRV)
  217. .Default(IC_CallOrUser);
  218. // Argument is i8**
  219. if (PointerType *Pte = dyn_cast<PointerType>(ETy))
  220. if (Pte->getElementType()->isIntegerTy(8))
  221. return StringSwitch<InstructionClass>(F->getName())
  222. .Case("objc_loadWeakRetained", IC_LoadWeakRetained)
  223. .Case("objc_loadWeak", IC_LoadWeak)
  224. .Case("objc_destroyWeak", IC_DestroyWeak)
  225. .Default(IC_CallOrUser);
  226. }
  227. // Two arguments, first is i8**.
  228. const Argument *A1 = AI++;
  229. if (AI == AE)
  230. if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
  231. if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
  232. if (Pte->getElementType()->isIntegerTy(8))
  233. if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
  234. Type *ETy1 = PTy1->getElementType();
  235. // Second argument is i8*
  236. if (ETy1->isIntegerTy(8))
  237. return StringSwitch<InstructionClass>(F->getName())
  238. .Case("objc_storeWeak", IC_StoreWeak)
  239. .Case("objc_initWeak", IC_InitWeak)
  240. .Case("objc_storeStrong", IC_StoreStrong)
  241. .Default(IC_CallOrUser);
  242. // Second argument is i8**.
  243. if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
  244. if (Pte1->getElementType()->isIntegerTy(8))
  245. return StringSwitch<InstructionClass>(F->getName())
  246. .Case("objc_moveWeak", IC_MoveWeak)
  247. .Case("objc_copyWeak", IC_CopyWeak)
  248. .Default(IC_CallOrUser);
  249. }
  250. // Anything else.
  251. return IC_CallOrUser;
  252. }
  253. /// GetInstructionClass - Determine what kind of construct V is.
  254. static InstructionClass GetInstructionClass(const Value *V) {
  255. if (const Instruction *I = dyn_cast<Instruction>(V)) {
  256. // Any instruction other than bitcast and gep with a pointer operand have a
  257. // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
  258. // to a subsequent use, rather than using it themselves, in this sense.
  259. // As a short cut, several other opcodes are known to have no pointer
  260. // operands of interest. And ret is never followed by a release, so it's
  261. // not interesting to examine.
  262. switch (I->getOpcode()) {
  263. case Instruction::Call: {
  264. const CallInst *CI = cast<CallInst>(I);
  265. // Check for calls to special functions.
  266. if (const Function *F = CI->getCalledFunction()) {
  267. InstructionClass Class = GetFunctionClass(F);
  268. if (Class != IC_CallOrUser)
  269. return Class;
  270. // None of the intrinsic functions do objc_release. For intrinsics, the
  271. // only question is whether or not they may be users.
  272. switch (F->getIntrinsicID()) {
  273. case Intrinsic::returnaddress: case Intrinsic::frameaddress:
  274. case Intrinsic::stacksave: case Intrinsic::stackrestore:
  275. case Intrinsic::vastart: case Intrinsic::vacopy: case Intrinsic::vaend:
  276. case Intrinsic::objectsize: case Intrinsic::prefetch:
  277. case Intrinsic::stackprotector:
  278. case Intrinsic::eh_return_i32: case Intrinsic::eh_return_i64:
  279. case Intrinsic::eh_typeid_for: case Intrinsic::eh_dwarf_cfa:
  280. case Intrinsic::eh_sjlj_lsda: case Intrinsic::eh_sjlj_functioncontext:
  281. case Intrinsic::init_trampoline: case Intrinsic::adjust_trampoline:
  282. case Intrinsic::lifetime_start: case Intrinsic::lifetime_end:
  283. case Intrinsic::invariant_start: case Intrinsic::invariant_end:
  284. // Don't let dbg info affect our results.
  285. case Intrinsic::dbg_declare: case Intrinsic::dbg_value:
  286. // Short cut: Some intrinsics obviously don't use ObjC pointers.
  287. return IC_None;
  288. default:
  289. break;
  290. }
  291. }
  292. return GetCallSiteClass(CI);
  293. }
  294. case Instruction::Invoke:
  295. return GetCallSiteClass(cast<InvokeInst>(I));
  296. case Instruction::BitCast:
  297. case Instruction::GetElementPtr:
  298. case Instruction::Select: case Instruction::PHI:
  299. case Instruction::Ret: case Instruction::Br:
  300. case Instruction::Switch: case Instruction::IndirectBr:
  301. case Instruction::Alloca: case Instruction::VAArg:
  302. case Instruction::Add: case Instruction::FAdd:
  303. case Instruction::Sub: case Instruction::FSub:
  304. case Instruction::Mul: case Instruction::FMul:
  305. case Instruction::SDiv: case Instruction::UDiv: case Instruction::FDiv:
  306. case Instruction::SRem: case Instruction::URem: case Instruction::FRem:
  307. case Instruction::Shl: case Instruction::LShr: case Instruction::AShr:
  308. case Instruction::And: case Instruction::Or: case Instruction::Xor:
  309. case Instruction::SExt: case Instruction::ZExt: case Instruction::Trunc:
  310. case Instruction::IntToPtr: case Instruction::FCmp:
  311. case Instruction::FPTrunc: case Instruction::FPExt:
  312. case Instruction::FPToUI: case Instruction::FPToSI:
  313. case Instruction::UIToFP: case Instruction::SIToFP:
  314. case Instruction::InsertElement: case Instruction::ExtractElement:
  315. case Instruction::ShuffleVector:
  316. case Instruction::ExtractValue:
  317. break;
  318. case Instruction::ICmp:
  319. // Comparing a pointer with null, or any other constant, isn't an
  320. // interesting use, because we don't care what the pointer points to, or
  321. // about the values of any other dynamic reference-counted pointers.
  322. if (IsPotentialUse(I->getOperand(1)))
  323. return IC_User;
  324. break;
  325. default:
  326. // For anything else, check all the operands.
  327. // Note that this includes both operands of a Store: while the first
  328. // operand isn't actually being dereferenced, it is being stored to
  329. // memory where we can no longer track who might read it and dereference
  330. // it, so we have to consider it potentially used.
  331. for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
  332. OI != OE; ++OI)
  333. if (IsPotentialUse(*OI))
  334. return IC_User;
  335. }
  336. }
  337. // Otherwise, it's totally inert for ARC purposes.
  338. return IC_None;
  339. }
  340. /// GetBasicInstructionClass - Determine what kind of construct V is. This is
  341. /// similar to GetInstructionClass except that it only detects objc runtine
  342. /// calls. This allows it to be faster.
  343. static InstructionClass GetBasicInstructionClass(const Value *V) {
  344. if (const CallInst *CI = dyn_cast<CallInst>(V)) {
  345. if (const Function *F = CI->getCalledFunction())
  346. return GetFunctionClass(F);
  347. // Otherwise, be conservative.
  348. return IC_CallOrUser;
  349. }
  350. // Otherwise, be conservative.
  351. return isa<InvokeInst>(V) ? IC_CallOrUser : IC_User;
  352. }
  353. /// IsRetain - Test if the given class is objc_retain or
  354. /// equivalent.
  355. static bool IsRetain(InstructionClass Class) {
  356. return Class == IC_Retain ||
  357. Class == IC_RetainRV;
  358. }
  359. /// IsAutorelease - Test if the given class is objc_autorelease or
  360. /// equivalent.
  361. static bool IsAutorelease(InstructionClass Class) {
  362. return Class == IC_Autorelease ||
  363. Class == IC_AutoreleaseRV;
  364. }
  365. /// IsForwarding - Test if the given class represents instructions which return
  366. /// their argument verbatim.
  367. static bool IsForwarding(InstructionClass Class) {
  368. // objc_retainBlock technically doesn't always return its argument
  369. // verbatim, but it doesn't matter for our purposes here.
  370. return Class == IC_Retain ||
  371. Class == IC_RetainRV ||
  372. Class == IC_Autorelease ||
  373. Class == IC_AutoreleaseRV ||
  374. Class == IC_RetainBlock ||
  375. Class == IC_NoopCast;
  376. }
  377. /// IsNoopOnNull - Test if the given class represents instructions which do
  378. /// nothing if passed a null pointer.
  379. static bool IsNoopOnNull(InstructionClass Class) {
  380. return Class == IC_Retain ||
  381. Class == IC_RetainRV ||
  382. Class == IC_Release ||
  383. Class == IC_Autorelease ||
  384. Class == IC_AutoreleaseRV ||
  385. Class == IC_RetainBlock;
  386. }
  387. /// IsAlwaysTail - Test if the given class represents instructions which are
  388. /// always safe to mark with the "tail" keyword.
  389. static bool IsAlwaysTail(InstructionClass Class) {
  390. // IC_RetainBlock may be given a stack argument.
  391. return Class == IC_Retain ||
  392. Class == IC_RetainRV ||
  393. Class == IC_Autorelease ||
  394. Class == IC_AutoreleaseRV;
  395. }
  396. /// IsNoThrow - Test if the given class represents instructions which are always
  397. /// safe to mark with the nounwind attribute..
  398. static bool IsNoThrow(InstructionClass Class) {
  399. // objc_retainBlock is not nounwind because it calls user copy constructors
  400. // which could theoretically throw.
  401. return Class == IC_Retain ||
  402. Class == IC_RetainRV ||
  403. Class == IC_Release ||
  404. Class == IC_Autorelease ||
  405. Class == IC_AutoreleaseRV ||
  406. Class == IC_AutoreleasepoolPush ||
  407. Class == IC_AutoreleasepoolPop;
  408. }
  409. /// EraseInstruction - Erase the given instruction. Many ObjC calls return their
  410. /// argument verbatim, so if it's such a call and the return value has users,
  411. /// replace them with the argument value.
  412. static void EraseInstruction(Instruction *CI) {
  413. Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
  414. bool Unused = CI->use_empty();
  415. if (!Unused) {
  416. // Replace the return value with the argument.
  417. assert(IsForwarding(GetBasicInstructionClass(CI)) &&
  418. "Can't delete non-forwarding instruction with users!");
  419. CI->replaceAllUsesWith(OldArg);
  420. }
  421. CI->eraseFromParent();
  422. if (Unused)
  423. RecursivelyDeleteTriviallyDeadInstructions(OldArg);
  424. }
  425. /// GetUnderlyingObjCPtr - This is a wrapper around getUnderlyingObject which
  426. /// also knows how to look through objc_retain and objc_autorelease calls, which
  427. /// we know to return their argument verbatim.
  428. static const Value *GetUnderlyingObjCPtr(const Value *V) {
  429. for (;;) {
  430. V = GetUnderlyingObject(V);
  431. if (!IsForwarding(GetBasicInstructionClass(V)))
  432. break;
  433. V = cast<CallInst>(V)->getArgOperand(0);
  434. }
  435. return V;
  436. }
  437. /// StripPointerCastsAndObjCCalls - This is a wrapper around
  438. /// Value::stripPointerCasts which also knows how to look through objc_retain
  439. /// and objc_autorelease calls, which we know to return their argument verbatim.
  440. static const Value *StripPointerCastsAndObjCCalls(const Value *V) {
  441. for (;;) {
  442. V = V->stripPointerCasts();
  443. if (!IsForwarding(GetBasicInstructionClass(V)))
  444. break;
  445. V = cast<CallInst>(V)->getArgOperand(0);
  446. }
  447. return V;
  448. }
  449. /// StripPointerCastsAndObjCCalls - This is a wrapper around
  450. /// Value::stripPointerCasts which also knows how to look through objc_retain
  451. /// and objc_autorelease calls, which we know to return their argument verbatim.
  452. static Value *StripPointerCastsAndObjCCalls(Value *V) {
  453. for (;;) {
  454. V = V->stripPointerCasts();
  455. if (!IsForwarding(GetBasicInstructionClass(V)))
  456. break;
  457. V = cast<CallInst>(V)->getArgOperand(0);
  458. }
  459. return V;
  460. }
  461. /// GetObjCArg - Assuming the given instruction is one of the special calls such
  462. /// as objc_retain or objc_release, return the argument value, stripped of no-op
  463. /// casts and forwarding calls.
  464. static Value *GetObjCArg(Value *Inst) {
  465. return StripPointerCastsAndObjCCalls(cast<CallInst>(Inst)->getArgOperand(0));
  466. }
  467. /// IsObjCIdentifiedObject - This is similar to AliasAnalysis'
  468. /// isObjCIdentifiedObject, except that it uses special knowledge of
  469. /// ObjC conventions...
  470. static bool IsObjCIdentifiedObject(const Value *V) {
  471. // Assume that call results and arguments have their own "provenance".
  472. // Constants (including GlobalVariables) and Allocas are never
  473. // reference-counted.
  474. if (isa<CallInst>(V) || isa<InvokeInst>(V) ||
  475. isa<Argument>(V) || isa<Constant>(V) ||
  476. isa<AllocaInst>(V))
  477. return true;
  478. if (const LoadInst *LI = dyn_cast<LoadInst>(V)) {
  479. const Value *Pointer =
  480. StripPointerCastsAndObjCCalls(LI->getPointerOperand());
  481. if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Pointer)) {
  482. // A constant pointer can't be pointing to an object on the heap. It may
  483. // be reference-counted, but it won't be deleted.
  484. if (GV->isConstant())
  485. return true;
  486. StringRef Name = GV->getName();
  487. // These special variables are known to hold values which are not
  488. // reference-counted pointers.
  489. if (Name.startswith("\01L_OBJC_SELECTOR_REFERENCES_") ||
  490. Name.startswith("\01L_OBJC_CLASSLIST_REFERENCES_") ||
  491. Name.startswith("\01L_OBJC_CLASSLIST_SUP_REFS_$_") ||
  492. Name.startswith("\01L_OBJC_METH_VAR_NAME_") ||
  493. Name.startswith("\01l_objc_msgSend_fixup_"))
  494. return true;
  495. }
  496. }
  497. return false;
  498. }
  499. /// FindSingleUseIdentifiedObject - This is similar to
  500. /// StripPointerCastsAndObjCCalls but it stops as soon as it finds a value
  501. /// with multiple uses.
  502. static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
  503. if (Arg->hasOneUse()) {
  504. if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
  505. return FindSingleUseIdentifiedObject(BC->getOperand(0));
  506. if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
  507. if (GEP->hasAllZeroIndices())
  508. return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
  509. if (IsForwarding(GetBasicInstructionClass(Arg)))
  510. return FindSingleUseIdentifiedObject(
  511. cast<CallInst>(Arg)->getArgOperand(0));
  512. if (!IsObjCIdentifiedObject(Arg))
  513. return 0;
  514. return Arg;
  515. }
  516. // If we found an identifiable object but it has multiple uses, but they are
  517. // trivial uses, we can still consider this to be a single-use value.
  518. if (IsObjCIdentifiedObject(Arg)) {
  519. for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
  520. UI != UE; ++UI) {
  521. const User *U = *UI;
  522. if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg)
  523. return 0;
  524. }
  525. return Arg;
  526. }
  527. return 0;
  528. }
  529. /// ModuleHasARC - Test if the given module looks interesting to run ARC
  530. /// optimization on.
  531. static bool ModuleHasARC(const Module &M) {
  532. return
  533. M.getNamedValue("objc_retain") ||
  534. M.getNamedValue("objc_release") ||
  535. M.getNamedValue("objc_autorelease") ||
  536. M.getNamedValue("objc_retainAutoreleasedReturnValue") ||
  537. M.getNamedValue("objc_retainBlock") ||
  538. M.getNamedValue("objc_autoreleaseReturnValue") ||
  539. M.getNamedValue("objc_autoreleasePoolPush") ||
  540. M.getNamedValue("objc_loadWeakRetained") ||
  541. M.getNamedValue("objc_loadWeak") ||
  542. M.getNamedValue("objc_destroyWeak") ||
  543. M.getNamedValue("objc_storeWeak") ||
  544. M.getNamedValue("objc_initWeak") ||
  545. M.getNamedValue("objc_moveWeak") ||
  546. M.getNamedValue("objc_copyWeak") ||
  547. M.getNamedValue("objc_retainedObject") ||
  548. M.getNamedValue("objc_unretainedObject") ||
  549. M.getNamedValue("objc_unretainedPointer");
  550. }
  551. /// DoesObjCBlockEscape - Test whether the given pointer, which is an
  552. /// Objective C block pointer, does not "escape". This differs from regular
  553. /// escape analysis in that a use as an argument to a call is not considered
  554. /// an escape.
  555. static bool DoesObjCBlockEscape(const Value *BlockPtr) {
  556. // Walk the def-use chains.
  557. SmallVector<const Value *, 4> Worklist;
  558. Worklist.push_back(BlockPtr);
  559. do {
  560. const Value *V = Worklist.pop_back_val();
  561. for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
  562. UI != UE; ++UI) {
  563. const User *UUser = *UI;
  564. // Special - Use by a call (callee or argument) is not considered
  565. // to be an escape.
  566. switch (GetBasicInstructionClass(UUser)) {
  567. case IC_StoreWeak:
  568. case IC_InitWeak:
  569. case IC_StoreStrong:
  570. case IC_Autorelease:
  571. case IC_AutoreleaseRV:
  572. // These special functions make copies of their pointer arguments.
  573. return true;
  574. case IC_User:
  575. case IC_None:
  576. // Use by an instruction which copies the value is an escape if the
  577. // result is an escape.
  578. if (isa<BitCastInst>(UUser) || isa<GetElementPtrInst>(UUser) ||
  579. isa<PHINode>(UUser) || isa<SelectInst>(UUser)) {
  580. Worklist.push_back(UUser);
  581. continue;
  582. }
  583. // Use by a load is not an escape.
  584. if (isa<LoadInst>(UUser))
  585. continue;
  586. // Use by a store is not an escape if the use is the address.
  587. if (const StoreInst *SI = dyn_cast<StoreInst>(UUser))
  588. if (V != SI->getValueOperand())
  589. continue;
  590. break;
  591. default:
  592. // Regular calls and other stuff are not considered escapes.
  593. continue;
  594. }
  595. // Otherwise, conservatively assume an escape.
  596. return true;
  597. }
  598. } while (!Worklist.empty());
  599. // No escapes found.
  600. return false;
  601. }
  602. //===----------------------------------------------------------------------===//
  603. // ARC AliasAnalysis.
  604. //===----------------------------------------------------------------------===//
  605. #include "llvm/Pass.h"
  606. #include "llvm/Analysis/AliasAnalysis.h"
  607. #include "llvm/Analysis/Passes.h"
  608. namespace {
  609. /// ObjCARCAliasAnalysis - This is a simple alias analysis
  610. /// implementation that uses knowledge of ARC constructs to answer queries.
  611. ///
  612. /// TODO: This class could be generalized to know about other ObjC-specific
  613. /// tricks. Such as knowing that ivars in the non-fragile ABI are non-aliasing
  614. /// even though their offsets are dynamic.
  615. class ObjCARCAliasAnalysis : public ImmutablePass,
  616. public AliasAnalysis {
  617. public:
  618. static char ID; // Class identification, replacement for typeinfo
  619. ObjCARCAliasAnalysis() : ImmutablePass(ID) {
  620. initializeObjCARCAliasAnalysisPass(*PassRegistry::getPassRegistry());
  621. }
  622. private:
  623. virtual void initializePass() {
  624. InitializeAliasAnalysis(this);
  625. }
  626. /// getAdjustedAnalysisPointer - This method is used when a pass implements
  627. /// an analysis interface through multiple inheritance. If needed, it
  628. /// should override this to adjust the this pointer as needed for the
  629. /// specified pass info.
  630. virtual void *getAdjustedAnalysisPointer(const void *PI) {
  631. if (PI == &AliasAnalysis::ID)
  632. return static_cast<AliasAnalysis *>(this);
  633. return this;
  634. }
  635. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  636. virtual AliasResult alias(const Location &LocA, const Location &LocB);
  637. virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal);
  638. virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
  639. virtual ModRefBehavior getModRefBehavior(const Function *F);
  640. virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
  641. const Location &Loc);
  642. virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
  643. ImmutableCallSite CS2);
  644. };
  645. } // End of anonymous namespace
  646. // Register this pass...
  647. char ObjCARCAliasAnalysis::ID = 0;
  648. INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis, "objc-arc-aa",
  649. "ObjC-ARC-Based Alias Analysis", false, true, false)
  650. ImmutablePass *llvm::createObjCARCAliasAnalysisPass() {
  651. return new ObjCARCAliasAnalysis();
  652. }
  653. void
  654. ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
  655. AU.setPreservesAll();
  656. AliasAnalysis::getAnalysisUsage(AU);
  657. }
  658. AliasAnalysis::AliasResult
  659. ObjCARCAliasAnalysis::alias(const Location &LocA, const Location &LocB) {
  660. if (!EnableARCOpts)
  661. return AliasAnalysis::alias(LocA, LocB);
  662. // First, strip off no-ops, including ObjC-specific no-ops, and try making a
  663. // precise alias query.
  664. const Value *SA = StripPointerCastsAndObjCCalls(LocA.Ptr);
  665. const Value *SB = StripPointerCastsAndObjCCalls(LocB.Ptr);
  666. AliasResult Result =
  667. AliasAnalysis::alias(Location(SA, LocA.Size, LocA.TBAATag),
  668. Location(SB, LocB.Size, LocB.TBAATag));
  669. if (Result != MayAlias)
  670. return Result;
  671. // If that failed, climb to the underlying object, including climbing through
  672. // ObjC-specific no-ops, and try making an imprecise alias query.
  673. const Value *UA = GetUnderlyingObjCPtr(SA);
  674. const Value *UB = GetUnderlyingObjCPtr(SB);
  675. if (UA != SA || UB != SB) {
  676. Result = AliasAnalysis::alias(Location(UA), Location(UB));
  677. // We can't use MustAlias or PartialAlias results here because
  678. // GetUnderlyingObjCPtr may return an offsetted pointer value.
  679. if (Result == NoAlias)
  680. return NoAlias;
  681. }
  682. // If that failed, fail. We don't need to chain here, since that's covered
  683. // by the earlier precise query.
  684. return MayAlias;
  685. }
  686. bool
  687. ObjCARCAliasAnalysis::pointsToConstantMemory(const Location &Loc,
  688. bool OrLocal) {
  689. if (!EnableARCOpts)
  690. return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
  691. // First, strip off no-ops, including ObjC-specific no-ops, and try making
  692. // a precise alias query.
  693. const Value *S = StripPointerCastsAndObjCCalls(Loc.Ptr);
  694. if (AliasAnalysis::pointsToConstantMemory(Location(S, Loc.Size, Loc.TBAATag),
  695. OrLocal))
  696. return true;
  697. // If that failed, climb to the underlying object, including climbing through
  698. // ObjC-specific no-ops, and try making an imprecise alias query.
  699. const Value *U = GetUnderlyingObjCPtr(S);
  700. if (U != S)
  701. return AliasAnalysis::pointsToConstantMemory(Location(U), OrLocal);
  702. // If that failed, fail. We don't need to chain here, since that's covered
  703. // by the earlier precise query.
  704. return false;
  705. }
  706. AliasAnalysis::ModRefBehavior
  707. ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
  708. // We have nothing to do. Just chain to the next AliasAnalysis.
  709. return AliasAnalysis::getModRefBehavior(CS);
  710. }
  711. AliasAnalysis::ModRefBehavior
  712. ObjCARCAliasAnalysis::getModRefBehavior(const Function *F) {
  713. if (!EnableARCOpts)
  714. return AliasAnalysis::getModRefBehavior(F);
  715. switch (GetFunctionClass(F)) {
  716. case IC_NoopCast:
  717. return DoesNotAccessMemory;
  718. default:
  719. break;
  720. }
  721. return AliasAnalysis::getModRefBehavior(F);
  722. }
  723. AliasAnalysis::ModRefResult
  724. ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS, const Location &Loc) {
  725. if (!EnableARCOpts)
  726. return AliasAnalysis::getModRefInfo(CS, Loc);
  727. switch (GetBasicInstructionClass(CS.getInstruction())) {
  728. case IC_Retain:
  729. case IC_RetainRV:
  730. case IC_Autorelease:
  731. case IC_AutoreleaseRV:
  732. case IC_NoopCast:
  733. case IC_AutoreleasepoolPush:
  734. case IC_FusedRetainAutorelease:
  735. case IC_FusedRetainAutoreleaseRV:
  736. // These functions don't access any memory visible to the compiler.
  737. // Note that this doesn't include objc_retainBlock, because it updates
  738. // pointers when it copies block data.
  739. return NoModRef;
  740. default:
  741. break;
  742. }
  743. return AliasAnalysis::getModRefInfo(CS, Loc);
  744. }
  745. AliasAnalysis::ModRefResult
  746. ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
  747. ImmutableCallSite CS2) {
  748. // TODO: Theoretically we could check for dependencies between objc_* calls
  749. // and OnlyAccessesArgumentPointees calls or other well-behaved calls.
  750. return AliasAnalysis::getModRefInfo(CS1, CS2);
  751. }
  752. //===----------------------------------------------------------------------===//
  753. // ARC expansion.
  754. //===----------------------------------------------------------------------===//
  755. #include "llvm/Support/InstIterator.h"
  756. #include "llvm/Transforms/Scalar.h"
  757. namespace {
  758. /// ObjCARCExpand - Early ARC transformations.
  759. class ObjCARCExpand : public FunctionPass {
  760. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  761. virtual bool doInitialization(Module &M);
  762. virtual bool runOnFunction(Function &F);
  763. /// Run - A flag indicating whether this optimization pass should run.
  764. bool Run;
  765. public:
  766. static char ID;
  767. ObjCARCExpand() : FunctionPass(ID) {
  768. initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
  769. }
  770. };
  771. }
  772. char ObjCARCExpand::ID = 0;
  773. INITIALIZE_PASS(ObjCARCExpand,
  774. "objc-arc-expand", "ObjC ARC expansion", false, false)
  775. Pass *llvm::createObjCARCExpandPass() {
  776. return new ObjCARCExpand();
  777. }
  778. void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
  779. AU.setPreservesCFG();
  780. }
  781. bool ObjCARCExpand::doInitialization(Module &M) {
  782. Run = ModuleHasARC(M);
  783. return false;
  784. }
  785. bool ObjCARCExpand::runOnFunction(Function &F) {
  786. if (!EnableARCOpts)
  787. return false;
  788. // If nothing in the Module uses ARC, don't do anything.
  789. if (!Run)
  790. return false;
  791. bool Changed = false;
  792. for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
  793. Instruction *Inst = &*I;
  794. switch (GetBasicInstructionClass(Inst)) {
  795. case IC_Retain:
  796. case IC_RetainRV:
  797. case IC_Autorelease:
  798. case IC_AutoreleaseRV:
  799. case IC_FusedRetainAutorelease:
  800. case IC_FusedRetainAutoreleaseRV:
  801. // These calls return their argument verbatim, as a low-level
  802. // optimization. However, this makes high-level optimizations
  803. // harder. Undo any uses of this optimization that the front-end
  804. // emitted here. We'll redo them in the contract pass.
  805. Changed = true;
  806. Inst->replaceAllUsesWith(cast<CallInst>(Inst)->getArgOperand(0));
  807. break;
  808. default:
  809. break;
  810. }
  811. }
  812. return Changed;
  813. }
  814. //===----------------------------------------------------------------------===//
  815. // ARC autorelease pool elimination.
  816. //===----------------------------------------------------------------------===//
  817. #include "llvm/Constants.h"
  818. #include "llvm/ADT/STLExtras.h"
  819. namespace {
  820. /// ObjCARCAPElim - Autorelease pool elimination.
  821. class ObjCARCAPElim : public ModulePass {
  822. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  823. virtual bool runOnModule(Module &M);
  824. static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0);
  825. static bool OptimizeBB(BasicBlock *BB);
  826. public:
  827. static char ID;
  828. ObjCARCAPElim() : ModulePass(ID) {
  829. initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
  830. }
  831. };
  832. }
  833. char ObjCARCAPElim::ID = 0;
  834. INITIALIZE_PASS(ObjCARCAPElim,
  835. "objc-arc-apelim",
  836. "ObjC ARC autorelease pool elimination",
  837. false, false)
  838. Pass *llvm::createObjCARCAPElimPass() {
  839. return new ObjCARCAPElim();
  840. }
  841. void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
  842. AU.setPreservesCFG();
  843. }
  844. /// MayAutorelease - Interprocedurally determine if calls made by the
  845. /// given call site can possibly produce autoreleases.
  846. bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) {
  847. if (const Function *Callee = CS.getCalledFunction()) {
  848. if (Callee->isDeclaration() || Callee->mayBeOverridden())
  849. return true;
  850. for (Function::const_iterator I = Callee->begin(), E = Callee->end();
  851. I != E; ++I) {
  852. const BasicBlock *BB = I;
  853. for (BasicBlock::const_iterator J = BB->begin(), F = BB->end();
  854. J != F; ++J)
  855. if (ImmutableCallSite JCS = ImmutableCallSite(J))
  856. // This recursion depth limit is arbitrary. It's just great
  857. // enough to cover known interesting testcases.
  858. if (Depth < 3 &&
  859. !JCS.onlyReadsMemory() &&
  860. MayAutorelease(JCS, Depth + 1))
  861. return true;
  862. }
  863. return false;
  864. }
  865. return true;
  866. }
  867. bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) {
  868. bool Changed = false;
  869. Instruction *Push = 0;
  870. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
  871. Instruction *Inst = I++;
  872. switch (GetBasicInstructionClass(Inst)) {
  873. case IC_AutoreleasepoolPush:
  874. Push = Inst;
  875. break;
  876. case IC_AutoreleasepoolPop:
  877. // If this pop matches a push and nothing in between can autorelease,
  878. // zap the pair.
  879. if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) {
  880. Changed = true;
  881. Inst->eraseFromParent();
  882. Push->eraseFromParent();
  883. }
  884. Push = 0;
  885. break;
  886. case IC_CallOrUser:
  887. if (MayAutorelease(ImmutableCallSite(Inst)))
  888. Push = 0;
  889. break;
  890. default:
  891. break;
  892. }
  893. }
  894. return Changed;
  895. }
  896. bool ObjCARCAPElim::runOnModule(Module &M) {
  897. if (!EnableARCOpts)
  898. return false;
  899. // If nothing in the Module uses ARC, don't do anything.
  900. if (!ModuleHasARC(M))
  901. return false;
  902. // Find the llvm.global_ctors variable, as the first step in
  903. // identifying the global constructors. In theory, unnecessary autorelease
  904. // pools could occur anywhere, but in practice it's pretty rare. Global
  905. // ctors are a place where autorelease pools get inserted automatically,
  906. // so it's pretty common for them to be unnecessary, and it's pretty
  907. // profitable to eliminate them.
  908. GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
  909. if (!GV)
  910. return false;
  911. assert(GV->hasDefinitiveInitializer() &&
  912. "llvm.global_ctors is uncooperative!");
  913. bool Changed = false;
  914. // Dig the constructor functions out of GV's initializer.
  915. ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
  916. for (User::op_iterator OI = Init->op_begin(), OE = Init->op_end();
  917. OI != OE; ++OI) {
  918. Value *Op = *OI;
  919. // llvm.global_ctors is an array of pairs where the second members
  920. // are constructor functions.
  921. Function *F = dyn_cast<Function>(cast<ConstantStruct>(Op)->getOperand(1));
  922. // If the user used a constructor function with the wrong signature and
  923. // it got bitcasted or whatever, look the other way.
  924. if (!F)
  925. continue;
  926. // Only look at function definitions.
  927. if (F->isDeclaration())
  928. continue;
  929. // Only look at functions with one basic block.
  930. if (llvm::next(F->begin()) != F->end())
  931. continue;
  932. // Ok, a single-block constructor function definition. Try to optimize it.
  933. Changed |= OptimizeBB(F->begin());
  934. }
  935. return Changed;
  936. }
  937. //===----------------------------------------------------------------------===//
  938. // ARC optimization.
  939. //===----------------------------------------------------------------------===//
  940. // TODO: On code like this:
  941. //
  942. // objc_retain(%x)
  943. // stuff_that_cannot_release()
  944. // objc_autorelease(%x)
  945. // stuff_that_cannot_release()
  946. // objc_retain(%x)
  947. // stuff_that_cannot_release()
  948. // objc_autorelease(%x)
  949. //
  950. // The second retain and autorelease can be deleted.
  951. // TODO: It should be possible to delete
  952. // objc_autoreleasePoolPush and objc_autoreleasePoolPop
  953. // pairs if nothing is actually autoreleased between them. Also, autorelease
  954. // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
  955. // after inlining) can be turned into plain release calls.
  956. // TODO: Critical-edge splitting. If the optimial insertion point is
  957. // a critical edge, the current algorithm has to fail, because it doesn't
  958. // know how to split edges. It should be possible to make the optimizer
  959. // think in terms of edges, rather than blocks, and then split critical
  960. // edges on demand.
  961. // TODO: OptimizeSequences could generalized to be Interprocedural.
  962. // TODO: Recognize that a bunch of other objc runtime calls have
  963. // non-escaping arguments and non-releasing arguments, and may be
  964. // non-autoreleasing.
  965. // TODO: Sink autorelease calls as far as possible. Unfortunately we
  966. // usually can't sink them past other calls, which would be the main
  967. // case where it would be useful.
  968. // TODO: The pointer returned from objc_loadWeakRetained is retained.
  969. // TODO: Delete release+retain pairs (rare).
  970. #include "llvm/LLVMContext.h"
  971. #include "llvm/Support/CFG.h"
  972. #include "llvm/ADT/Statistic.h"
  973. #include "llvm/ADT/SmallPtrSet.h"
  974. STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
  975. STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
  976. STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
  977. STATISTIC(NumRets, "Number of return value forwarding "
  978. "retain+autoreleaes eliminated");
  979. STATISTIC(NumRRs, "Number of retain+release paths eliminated");
  980. STATISTIC(NumPeeps, "Number of calls peephole-optimized");
  981. namespace {
  982. /// ProvenanceAnalysis - This is similar to BasicAliasAnalysis, and it
  983. /// uses many of the same techniques, except it uses special ObjC-specific
  984. /// reasoning about pointer relationships.
  985. class ProvenanceAnalysis {
  986. AliasAnalysis *AA;
  987. typedef std::pair<const Value *, const Value *> ValuePairTy;
  988. typedef DenseMap<ValuePairTy, bool> CachedResultsTy;
  989. CachedResultsTy CachedResults;
  990. bool relatedCheck(const Value *A, const Value *B);
  991. bool relatedSelect(const SelectInst *A, const Value *B);
  992. bool relatedPHI(const PHINode *A, const Value *B);
  993. // Do not implement.
  994. void operator=(const ProvenanceAnalysis &);
  995. ProvenanceAnalysis(const ProvenanceAnalysis &);
  996. public:
  997. ProvenanceAnalysis() {}
  998. void setAA(AliasAnalysis *aa) { AA = aa; }
  999. AliasAnalysis *getAA() const { return AA; }
  1000. bool related(const Value *A, const Value *B);
  1001. void clear() {
  1002. CachedResults.clear();
  1003. }
  1004. };
  1005. }
  1006. bool ProvenanceAnalysis::relatedSelect(const SelectInst *A, const Value *B) {
  1007. // If the values are Selects with the same condition, we can do a more precise
  1008. // check: just check for relations between the values on corresponding arms.
  1009. if (const SelectInst *SB = dyn_cast<SelectInst>(B))
  1010. if (A->getCondition() == SB->getCondition())
  1011. return related(A->getTrueValue(), SB->getTrueValue()) ||
  1012. related(A->getFalseValue(), SB->getFalseValue());
  1013. // Check both arms of the Select node individually.
  1014. return related(A->getTrueValue(), B) ||
  1015. related(A->getFalseValue(), B);
  1016. }
  1017. bool ProvenanceAnalysis::relatedPHI(const PHINode *A, const Value *B) {
  1018. // If the values are PHIs in the same block, we can do a more precise as well
  1019. // as efficient check: just check for relations between the values on
  1020. // corresponding edges.
  1021. if (const PHINode *PNB = dyn_cast<PHINode>(B))
  1022. if (PNB->getParent() == A->getParent()) {
  1023. for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i)
  1024. if (related(A->getIncomingValue(i),
  1025. PNB->getIncomingValueForBlock(A->getIncomingBlock(i))))
  1026. return true;
  1027. return false;
  1028. }
  1029. // Check each unique source of the PHI node against B.
  1030. SmallPtrSet<const Value *, 4> UniqueSrc;
  1031. for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i) {
  1032. const Value *PV1 = A->getIncomingValue(i);
  1033. if (UniqueSrc.insert(PV1) && related(PV1, B))
  1034. return true;
  1035. }
  1036. // All of the arms checked out.
  1037. return false;
  1038. }
  1039. /// isStoredObjCPointer - Test if the value of P, or any value covered by its
  1040. /// provenance, is ever stored within the function (not counting callees).
  1041. static bool isStoredObjCPointer(const Value *P) {
  1042. SmallPtrSet<const Value *, 8> Visited;
  1043. SmallVector<const Value *, 8> Worklist;
  1044. Worklist.push_back(P);
  1045. Visited.insert(P);
  1046. do {
  1047. P = Worklist.pop_back_val();
  1048. for (Value::const_use_iterator UI = P->use_begin(), UE = P->use_end();
  1049. UI != UE; ++UI) {
  1050. const User *Ur = *UI;
  1051. if (isa<StoreInst>(Ur)) {
  1052. if (UI.getOperandNo() == 0)
  1053. // The pointer is stored.
  1054. return true;
  1055. // The pointed is stored through.
  1056. continue;
  1057. }
  1058. if (isa<CallInst>(Ur))
  1059. // The pointer is passed as an argument, ignore this.
  1060. continue;
  1061. if (isa<PtrToIntInst>(P))
  1062. // Assume the worst.
  1063. return true;
  1064. if (Visited.insert(Ur))
  1065. Worklist.push_back(Ur);
  1066. }
  1067. } while (!Worklist.empty());
  1068. // Everything checked out.
  1069. return false;
  1070. }
  1071. bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B) {
  1072. // Skip past provenance pass-throughs.
  1073. A = GetUnderlyingObjCPtr(A);
  1074. B = GetUnderlyingObjCPtr(B);
  1075. // Quick check.
  1076. if (A == B)
  1077. return true;
  1078. // Ask regular AliasAnalysis, for a first approximation.
  1079. switch (AA->alias(A, B)) {
  1080. case AliasAnalysis::NoAlias:
  1081. return false;
  1082. case AliasAnalysis::MustAlias:
  1083. case AliasAnalysis::PartialAlias:
  1084. return true;
  1085. case AliasAnalysis::MayAlias:
  1086. break;
  1087. }
  1088. bool AIsIdentified = IsObjCIdentifiedObject(A);
  1089. bool BIsIdentified = IsObjCIdentifiedObject(B);
  1090. // An ObjC-Identified object can't alias a load if it is never locally stored.
  1091. if (AIsIdentified) {
  1092. if (BIsIdentified) {
  1093. // If both pointers have provenance, they can be directly compared.
  1094. if (A != B)
  1095. return false;
  1096. } else {
  1097. if (isa<LoadInst>(B))
  1098. return isStoredObjCPointer(A);
  1099. }
  1100. } else {
  1101. if (BIsIdentified && isa<LoadInst>(A))
  1102. return isStoredObjCPointer(B);
  1103. }
  1104. // Special handling for PHI and Select.
  1105. if (const PHINode *PN = dyn_cast<PHINode>(A))
  1106. return relatedPHI(PN, B);
  1107. if (const PHINode *PN = dyn_cast<PHINode>(B))
  1108. return relatedPHI(PN, A);
  1109. if (const SelectInst *S = dyn_cast<SelectInst>(A))
  1110. return relatedSelect(S, B);
  1111. if (const SelectInst *S = dyn_cast<SelectInst>(B))
  1112. return relatedSelect(S, A);
  1113. // Conservative.
  1114. return true;
  1115. }
  1116. bool ProvenanceAnalysis::related(const Value *A, const Value *B) {
  1117. // Begin by inserting a conservative value into the map. If the insertion
  1118. // fails, we have the answer already. If it succeeds, leave it there until we
  1119. // compute the real answer to guard against recursive queries.
  1120. if (A > B) std::swap(A, B);
  1121. std::pair<CachedResultsTy::iterator, bool> Pair =
  1122. CachedResults.insert(std::make_pair(ValuePairTy(A, B), true));
  1123. if (!Pair.second)
  1124. return Pair.first->second;
  1125. bool Result = relatedCheck(A, B);
  1126. CachedResults[ValuePairTy(A, B)] = Result;
  1127. return Result;
  1128. }
  1129. namespace {
  1130. // Sequence - A sequence of states that a pointer may go through in which an
  1131. // objc_retain and objc_release are actually needed.
  1132. enum Sequence {
  1133. S_None,
  1134. S_Retain, ///< objc_retain(x)
  1135. S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement
  1136. S_Use, ///< any use of x
  1137. S_Stop, ///< like S_Release, but code motion is stopped
  1138. S_Release, ///< objc_release(x)
  1139. S_MovableRelease ///< objc_release(x), !clang.imprecise_release
  1140. };
  1141. }
  1142. static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
  1143. // The easy cases.
  1144. if (A == B)
  1145. return A;
  1146. if (A == S_None || B == S_None)
  1147. return S_None;
  1148. if (A > B) std::swap(A, B);
  1149. if (TopDown) {
  1150. // Choose the side which is further along in the sequence.
  1151. if ((A == S_Retain || A == S_CanRelease) &&
  1152. (B == S_CanRelease || B == S_Use))
  1153. return B;
  1154. } else {
  1155. // Choose the side which is further along in the sequence.
  1156. if ((A == S_Use || A == S_CanRelease) &&
  1157. (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
  1158. return A;
  1159. // If both sides are releases, choose the more conservative one.
  1160. if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
  1161. return A;
  1162. if (A == S_Release && B == S_MovableRelease)
  1163. return A;
  1164. }
  1165. return S_None;
  1166. }
  1167. namespace {
  1168. /// RRInfo - Unidirectional information about either a
  1169. /// retain-decrement-use-release sequence or release-use-decrement-retain
  1170. /// reverese sequence.
  1171. struct RRInfo {
  1172. /// KnownSafe - After an objc_retain, the reference count of the referenced
  1173. /// object is known to be positive. Similarly, before an objc_release, the
  1174. /// reference count of the referenced object is known to be positive. If
  1175. /// there are retain-release pairs in code regions where the retain count
  1176. /// is known to be positive, they can be eliminated, regardless of any side
  1177. /// effects between them.
  1178. ///
  1179. /// Also, a retain+release pair nested within another retain+release
  1180. /// pair all on the known same pointer value can be eliminated, regardless
  1181. /// of any intervening side effects.
  1182. ///
  1183. /// KnownSafe is true when either of these conditions is satisfied.
  1184. bool KnownSafe;
  1185. /// IsRetainBlock - True if the Calls are objc_retainBlock calls (as
  1186. /// opposed to objc_retain calls).
  1187. bool IsRetainBlock;
  1188. /// IsTailCallRelease - True of the objc_release calls are all marked
  1189. /// with the "tail" keyword.
  1190. bool IsTailCallRelease;
  1191. /// ReleaseMetadata - If the Calls are objc_release calls and they all have
  1192. /// a clang.imprecise_release tag, this is the metadata tag.
  1193. MDNode *ReleaseMetadata;
  1194. /// Calls - For a top-down sequence, the set of objc_retains or
  1195. /// objc_retainBlocks. For bottom-up, the set of objc_releases.
  1196. SmallPtrSet<Instruction *, 2> Calls;
  1197. /// ReverseInsertPts - The set of optimal insert positions for
  1198. /// moving calls in the opposite sequence.
  1199. SmallPtrSet<Instruction *, 2> ReverseInsertPts;
  1200. RRInfo() :
  1201. KnownSafe(false), IsRetainBlock(false),
  1202. IsTailCallRelease(false),
  1203. ReleaseMetadata(0) {}
  1204. void clear();
  1205. };
  1206. }
  1207. void RRInfo::clear() {
  1208. KnownSafe = false;
  1209. IsRetainBlock = false;
  1210. IsTailCallRelease = false;
  1211. ReleaseMetadata = 0;
  1212. Calls.clear();
  1213. ReverseInsertPts.clear();
  1214. }
  1215. namespace {
  1216. /// PtrState - This class summarizes several per-pointer runtime properties
  1217. /// which are propogated through the flow graph.
  1218. class PtrState {
  1219. /// NestCount - The known minimum level of retain+release nesting.
  1220. unsigned NestCount;
  1221. /// KnownPositiveRefCount - True if the reference count is known to
  1222. /// be incremented.
  1223. bool KnownPositiveRefCount;
  1224. /// Partial - True of we've seen an opportunity for partial RR elimination,
  1225. /// such as pushing calls into a CFG triangle or into one side of a
  1226. /// CFG diamond.
  1227. bool Partial;
  1228. /// Seq - The current position in the sequence.
  1229. Sequence Seq : 8;
  1230. public:
  1231. /// RRI - Unidirectional information about the current sequence.
  1232. /// TODO: Encapsulate this better.
  1233. RRInfo RRI;
  1234. PtrState() : NestCount(0), KnownPositiveRefCount(false), Partial(false),
  1235. Seq(S_None) {}
  1236. void SetKnownPositiveRefCount() {
  1237. KnownPositiveRefCount = true;
  1238. }
  1239. void ClearRefCount() {
  1240. KnownPositiveRefCount = false;
  1241. }
  1242. bool IsKnownIncremented() const {
  1243. return KnownPositiveRefCount;
  1244. }
  1245. void IncrementNestCount() {
  1246. if (NestCount != UINT_MAX) ++NestCount;
  1247. }
  1248. void DecrementNestCount() {
  1249. if (NestCount != 0) --NestCount;
  1250. }
  1251. bool IsKnownNested() const {
  1252. return NestCount > 0;
  1253. }
  1254. void SetSeq(Sequence NewSeq) {
  1255. Seq = NewSeq;
  1256. }
  1257. Sequence GetSeq() const {
  1258. return Seq;
  1259. }
  1260. void ClearSequenceProgress() {
  1261. ResetSequenceProgress(S_None);
  1262. }
  1263. void ResetSequenceProgress(Sequence NewSeq) {
  1264. Seq = NewSeq;
  1265. Partial = false;
  1266. RRI.clear();
  1267. }
  1268. void Merge(const PtrState &Other, bool TopDown);
  1269. };
  1270. }
  1271. void
  1272. PtrState::Merge(const PtrState &Other, bool TopDown) {
  1273. Seq = MergeSeqs(Seq, Other.Seq, TopDown);
  1274. KnownPositiveRefCount = KnownPositiveRefCount && Other.KnownPositiveRefCount;
  1275. NestCount = std::min(NestCount, Other.NestCount);
  1276. // We can't merge a plain objc_retain with an objc_retainBlock.
  1277. if (RRI.IsRetainBlock != Other.RRI.IsRetainBlock)
  1278. Seq = S_None;
  1279. // If we're not in a sequence (anymore), drop all associated state.
  1280. if (Seq == S_None) {
  1281. Partial = false;
  1282. RRI.clear();
  1283. } else if (Partial || Other.Partial) {
  1284. // If we're doing a merge on a path that's previously seen a partial
  1285. // merge, conservatively drop the sequence, to avoid doing partial
  1286. // RR elimination. If the branch predicates for the two merge differ,
  1287. // mixing them is unsafe.
  1288. ClearSequenceProgress();
  1289. } else {
  1290. // Conservatively merge the ReleaseMetadata information.
  1291. if (RRI.ReleaseMetadata != Other.RRI.ReleaseMetadata)
  1292. RRI.ReleaseMetadata = 0;
  1293. RRI.KnownSafe = RRI.KnownSafe && Other.RRI.KnownSafe;
  1294. RRI.IsTailCallRelease = RRI.IsTailCallRelease &&
  1295. Other.RRI.IsTailCallRelease;
  1296. RRI.Calls.insert(Other.RRI.Calls.begin(), Other.RRI.Calls.end());
  1297. // Merge the insert point sets. If there are any differences,
  1298. // that makes this a partial merge.
  1299. Partial = RRI.ReverseInsertPts.size() != Other.RRI.ReverseInsertPts.size();
  1300. for (SmallPtrSet<Instruction *, 2>::const_iterator
  1301. I = Other.RRI.ReverseInsertPts.begin(),
  1302. E = Other.RRI.ReverseInsertPts.end(); I != E; ++I)
  1303. Partial |= RRI.ReverseInsertPts.insert(*I);
  1304. }
  1305. }
  1306. namespace {
  1307. /// BBState - Per-BasicBlock state.
  1308. class BBState {
  1309. /// TopDownPathCount - The number of unique control paths from the entry
  1310. /// which can reach this block.
  1311. unsigned TopDownPathCount;
  1312. /// BottomUpPathCount - The number of unique control paths to exits
  1313. /// from this block.
  1314. unsigned BottomUpPathCount;
  1315. /// MapTy - A type for PerPtrTopDown and PerPtrBottomUp.
  1316. typedef MapVector<const Value *, PtrState> MapTy;
  1317. /// PerPtrTopDown - The top-down traversal uses this to record information
  1318. /// known about a pointer at the bottom of each block.
  1319. MapTy PerPtrTopDown;
  1320. /// PerPtrBottomUp - The bottom-up traversal uses this to record information
  1321. /// known about a pointer at the top of each block.
  1322. MapTy PerPtrBottomUp;
  1323. /// Preds, Succs - Effective successors and predecessors of the current
  1324. /// block (this ignores ignorable edges and ignored backedges).
  1325. SmallVector<BasicBlock *, 2> Preds;
  1326. SmallVector<BasicBlock *, 2> Succs;
  1327. public:
  1328. BBState() : TopDownPathCount(0), BottomUpPathCount(0) {}
  1329. typedef MapTy::iterator ptr_iterator;
  1330. typedef MapTy::const_iterator ptr_const_iterator;
  1331. ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
  1332. ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
  1333. ptr_const_iterator top_down_ptr_begin() const {
  1334. return PerPtrTopDown.begin();
  1335. }
  1336. ptr_const_iterator top_down_ptr_end() const {
  1337. return PerPtrTopDown.end();
  1338. }
  1339. ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); }
  1340. ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
  1341. ptr_const_iterator bottom_up_ptr_begin() const {
  1342. return PerPtrBottomUp.begin();
  1343. }
  1344. ptr_const_iterator bottom_up_ptr_end() const {
  1345. return PerPtrBottomUp.end();
  1346. }
  1347. /// SetAsEntry - Mark this block as being an entry block, which has one
  1348. /// path from the entry by definition.
  1349. void SetAsEntry() { TopDownPathCount = 1; }
  1350. /// SetAsExit - Mark this block as being an exit block, which has one
  1351. /// path to an exit by definition.
  1352. void SetAsExit() { BottomUpPathCount = 1; }
  1353. PtrState &getPtrTopDownState(const Value *Arg) {
  1354. return PerPtrTopDown[Arg];
  1355. }
  1356. PtrState &getPtrBottomUpState(const Value *Arg) {
  1357. return PerPtrBottomUp[Arg];
  1358. }
  1359. void clearBottomUpPointers() {
  1360. PerPtrBottomUp.clear();
  1361. }
  1362. void clearTopDownPointers() {
  1363. PerPtrTopDown.clear();
  1364. }
  1365. void InitFromPred(const BBState &Other);
  1366. void InitFromSucc(const BBState &Other);
  1367. void MergePred(const BBState &Other);
  1368. void MergeSucc(const BBState &Other);
  1369. /// GetAllPathCount - Return the number of possible unique paths from an
  1370. /// entry to an exit which pass through this block. This is only valid
  1371. /// after both the top-down and bottom-up traversals are complete.
  1372. unsigned GetAllPathCount() const {
  1373. assert(TopDownPathCount != 0);
  1374. assert(BottomUpPathCount != 0);
  1375. return TopDownPathCount * BottomUpPathCount;
  1376. }
  1377. // Specialized CFG utilities.
  1378. typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
  1379. edge_iterator pred_begin() { return Preds.begin(); }
  1380. edge_iterator pred_end() { return Preds.end(); }
  1381. edge_iterator succ_begin() { return Succs.begin(); }
  1382. edge_iterator succ_end() { return Succs.end(); }
  1383. void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
  1384. void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
  1385. bool isExit() const { return Succs.empty(); }
  1386. };
  1387. }
  1388. void BBState::InitFromPred(const BBState &Other) {
  1389. PerPtrTopDown = Other.PerPtrTopDown;
  1390. TopDownPathCount = Other.TopDownPathCount;
  1391. }
  1392. void BBState::InitFromSucc(const BBState &Other) {
  1393. PerPtrBottomUp = Other.PerPtrBottomUp;
  1394. BottomUpPathCount = Other.BottomUpPathCount;
  1395. }
  1396. /// MergePred - The top-down traversal uses this to merge information about
  1397. /// predecessors to form the initial state for a new block.
  1398. void BBState::MergePred(const BBState &Other) {
  1399. // Other.TopDownPathCount can be 0, in which case it is either dead or a
  1400. // loop backedge. Loop backedges are special.
  1401. TopDownPathCount += Other.TopDownPathCount;
  1402. // For each entry in the other set, if our set has an entry with the same key,
  1403. // merge the entries. Otherwise, copy the entry and merge it with an empty
  1404. // entry.
  1405. for (ptr_const_iterator MI = Other.top_down_ptr_begin(),
  1406. ME = Other.top_down_ptr_end(); MI != ME; ++MI) {
  1407. std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI);
  1408. Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
  1409. /*TopDown=*/true);
  1410. }
  1411. // For each entry in our set, if the other set doesn't have an entry with the
  1412. // same key, force it to merge with an empty entry.
  1413. for (ptr_iterator MI = top_down_ptr_begin(),
  1414. ME = top_down_ptr_end(); MI != ME; ++MI)
  1415. if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
  1416. MI->second.Merge(PtrState(), /*TopDown=*/true);
  1417. }
  1418. /// MergeSucc - The bottom-up traversal uses this to merge information about
  1419. /// successors to form the initial state for a new block.
  1420. void BBState::MergeSucc(const BBState &Other) {
  1421. // Other.BottomUpPathCount can be 0, in which case it is either dead or a
  1422. // loop backedge. Loop backedges are special.
  1423. BottomUpPathCount += Other.BottomUpPathCount;
  1424. // For each entry in the other set, if our set has an entry with the
  1425. // same key, merge the entries. Otherwise, copy the entry and merge
  1426. // it with an empty entry.
  1427. for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(),
  1428. ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) {
  1429. std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI);
  1430. Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
  1431. /*TopDown=*/false);
  1432. }
  1433. // For each entry in our set, if the other set doesn't have an entry
  1434. // with the same key, force it to merge with an empty entry.
  1435. for (ptr_iterator MI = bottom_up_ptr_begin(),
  1436. ME = bottom_up_ptr_end(); MI != ME; ++MI)
  1437. if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
  1438. MI->second.Merge(PtrState(), /*TopDown=*/false);
  1439. }
  1440. namespace {
  1441. /// ObjCARCOpt - The main ARC optimization pass.
  1442. class ObjCARCOpt : public FunctionPass {
  1443. bool Changed;
  1444. ProvenanceAnalysis PA;
  1445. /// Run - A flag indicating whether this optimization pass should run.
  1446. bool Run;
  1447. /// RetainRVCallee, etc. - Declarations for ObjC runtime
  1448. /// functions, for use in creating calls to them. These are initialized
  1449. /// lazily to avoid cluttering up the Module with unused declarations.
  1450. Constant *RetainRVCallee, *AutoreleaseRVCallee, *ReleaseCallee,
  1451. *RetainCallee, *RetainBlockCallee, *AutoreleaseCallee;
  1452. /// UsedInThisFunciton - Flags which determine whether each of the
  1453. /// interesting runtine functions is in fact used in the current function.
  1454. unsigned UsedInThisFunction;
  1455. /// ImpreciseReleaseMDKind - The Metadata Kind for clang.imprecise_release
  1456. /// metadata.
  1457. unsigned ImpreciseReleaseMDKind;
  1458. /// CopyOnEscapeMDKind - The Metadata Kind for clang.arc.copy_on_escape
  1459. /// metadata.
  1460. unsigned CopyOnEscapeMDKind;
  1461. /// NoObjCARCExceptionsMDKind - The Metadata Kind for
  1462. /// clang.arc.no_objc_arc_exceptions metadata.
  1463. unsigned NoObjCARCExceptionsMDKind;
  1464. Constant *getRetainRVCallee(Module *M);
  1465. Constant *getAutoreleaseRVCallee(Module *M);
  1466. Constant *getReleaseCallee(Module *M);
  1467. Constant *getRetainCallee(Module *M);
  1468. Constant *getRetainBlockCallee(Module *M);
  1469. Constant *getAutoreleaseCallee(Module *M);
  1470. bool IsRetainBlockOptimizable(const Instruction *Inst);
  1471. void OptimizeRetainCall(Function &F, Instruction *Retain);
  1472. bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
  1473. void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV);
  1474. void OptimizeIndividualCalls(Function &F);
  1475. void CheckForCFGHazards(const BasicBlock *BB,
  1476. DenseMap<const BasicBlock *, BBState> &BBStates,
  1477. BBState &MyStates) const;
  1478. bool VisitInstructionBottomUp(Instruction *Inst,
  1479. BasicBlock *BB,
  1480. MapVector<Value *, RRInfo> &Retains,
  1481. BBState &MyStates);
  1482. bool VisitBottomUp(BasicBlock *BB,
  1483. DenseMap<const BasicBlock *, BBState> &BBStates,
  1484. MapVector<Value *, RRInfo> &Retains);
  1485. bool VisitInstructionTopDown(Instruction *Inst,
  1486. DenseMap<Value *, RRInfo> &Releases,
  1487. BBState &MyStates);
  1488. bool VisitTopDown(BasicBlock *BB,
  1489. DenseMap<const BasicBlock *, BBState> &BBStates,
  1490. DenseMap<Value *, RRInfo> &Releases);
  1491. bool Visit(Function &F,
  1492. DenseMap<const BasicBlock *, BBState> &BBStates,
  1493. MapVector<Value *, RRInfo> &Retains,
  1494. DenseMap<Value *, RRInfo> &Releases);
  1495. void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
  1496. MapVector<Value *, RRInfo> &Retains,
  1497. DenseMap<Value *, RRInfo> &Releases,
  1498. SmallVectorImpl<Instruction *> &DeadInsts,
  1499. Module *M);
  1500. bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
  1501. MapVector<Value *, RRInfo> &Retains,
  1502. DenseMap<Value *, RRInfo> &Releases,
  1503. Module *M);
  1504. void OptimizeWeakCalls(Function &F);
  1505. bool OptimizeSequences(Function &F);
  1506. void OptimizeReturns(Function &F);
  1507. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  1508. virtual bool doInitialization(Module &M);
  1509. virtual bool runOnFunction(Function &F);
  1510. virtual void releaseMemory();
  1511. public:
  1512. static char ID;
  1513. ObjCARCOpt() : FunctionPass(ID) {
  1514. initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
  1515. }
  1516. };
  1517. }
  1518. char ObjCARCOpt::ID = 0;
  1519. INITIALIZE_PASS_BEGIN(ObjCARCOpt,
  1520. "objc-arc", "ObjC ARC optimization", false, false)
  1521. INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
  1522. INITIALIZE_PASS_END(ObjCARCOpt,
  1523. "objc-arc", "ObjC ARC optimization", false, false)
  1524. Pass *llvm::createObjCARCOptPass() {
  1525. return new ObjCARCOpt();
  1526. }
  1527. void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
  1528. AU.addRequired<ObjCARCAliasAnalysis>();
  1529. AU.addRequired<AliasAnalysis>();
  1530. // ARC optimization doesn't currently split critical edges.
  1531. AU.setPreservesCFG();
  1532. }
  1533. bool ObjCARCOpt::IsRetainBlockOptimizable(const Instruction *Inst) {
  1534. // Without the magic metadata tag, we have to assume this might be an
  1535. // objc_retainBlock call inserted to convert a block pointer to an id,
  1536. // in which case it really is needed.
  1537. if (!Inst->getMetadata(CopyOnEscapeMDKind))
  1538. return false;
  1539. // If the pointer "escapes" (not including being used in a call),
  1540. // the copy may be needed.
  1541. if (DoesObjCBlockEscape(Inst))
  1542. return false;
  1543. // Otherwise, it's not needed.
  1544. return true;
  1545. }
  1546. Constant *ObjCARCOpt::getRetainRVCallee(Module *M) {
  1547. if (!RetainRVCallee) {
  1548. LLVMContext &C = M->getContext();
  1549. Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
  1550. Type *Params[] = { I8X };
  1551. FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
  1552. AttrListPtr Attributes = AttrListPtr().addAttr(~0u, Attribute::NoUnwind);
  1553. RetainRVCallee =
  1554. M->getOrInsertFunction("objc_retainAutoreleasedReturnValue", FTy,
  1555. Attributes);
  1556. }
  1557. return RetainRVCallee;
  1558. }
  1559. Constant *ObjCARCOpt::getAutoreleaseRVCallee(Module *M) {
  1560. if (!AutoreleaseRVCallee) {
  1561. LLVMContext &C = M->getContext();
  1562. Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
  1563. Type *Params[] = { I8X };
  1564. FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
  1565. AttrListPtr Attributes = AttrListPtr().addAttr(~0u, Attribute::NoUnwind);
  1566. AutoreleaseRVCallee =
  1567. M->getOrInsertFunction("objc_autoreleaseReturnValue", FTy,
  1568. Attributes);
  1569. }
  1570. return AutoreleaseRVCallee;
  1571. }
  1572. Constant *ObjCARCOpt::getReleaseCallee(Module *M) {
  1573. if (!ReleaseCallee) {
  1574. LLVMContext &C = M->getContext();
  1575. Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
  1576. AttrListPtr Attributes = AttrListPtr().addAttr(~0u, Attribute::NoUnwind);
  1577. ReleaseCallee =
  1578. M->getOrInsertFunction(
  1579. "objc_release",
  1580. FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
  1581. Attributes);
  1582. }
  1583. return ReleaseCallee;
  1584. }
  1585. Constant *ObjCARCOpt::getRetainCallee(Module *M) {
  1586. if (!RetainCallee) {
  1587. LLVMContext &C = M->getContext();
  1588. Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
  1589. AttrListPtr Attributes = AttrListPtr().addAttr(~0u, Attribute::NoUnwind);
  1590. RetainCallee =
  1591. M->getOrInsertFunction(
  1592. "objc_retain",
  1593. FunctionType::get(Params[0], Params, /*isVarArg=*/false),
  1594. Attributes);
  1595. }
  1596. return RetainCallee;
  1597. }
  1598. Constant *ObjCARCOpt::getRetainBlockCallee(Module *M) {
  1599. if (!RetainBlockCallee) {
  1600. LLVMContext &C = M->getContext();
  1601. Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
  1602. // objc_retainBlock is not nounwind because it calls user copy constructors
  1603. // which could theoretically throw.
  1604. RetainBlockCallee =
  1605. M->getOrInsertFunction(
  1606. "objc_retainBlock",
  1607. FunctionType::get(Params[0], Params, /*isVarArg=*/false),
  1608. AttrListPtr());
  1609. }
  1610. return RetainBlockCallee;
  1611. }
  1612. Constant *ObjCARCOpt::getAutoreleaseCallee(Module *M) {
  1613. if (!AutoreleaseCallee) {
  1614. LLVMContext &C = M->getContext();
  1615. Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
  1616. AttrListPtr Attributes = AttrListPtr().addAttr(~0u, Attribute::NoUnwind);
  1617. AutoreleaseCallee =
  1618. M->getOrInsertFunction(
  1619. "objc_autorelease",
  1620. FunctionType::get(Params[0], Params, /*isVarArg=*/false),
  1621. Attributes);
  1622. }
  1623. return AutoreleaseCallee;
  1624. }
  1625. /// CanAlterRefCount - Test whether the given instruction can result in a
  1626. /// reference count modification (positive or negative) for the pointer's
  1627. /// object.
  1628. static bool
  1629. CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
  1630. ProvenanceAnalysis &PA, InstructionClass Class) {
  1631. switch (Class) {
  1632. case IC_Autorelease:
  1633. case IC_AutoreleaseRV:
  1634. case IC_User:
  1635. // These operations never directly modify a reference count.
  1636. return false;
  1637. default: break;
  1638. }
  1639. ImmutableCallSite CS = static_cast<const Value *>(Inst);
  1640. assert(CS && "Only calls can alter reference counts!");
  1641. // See if AliasAnalysis can help us with the call.
  1642. AliasAnalysis::ModRefBehavior MRB = PA.getAA()->getModRefBehavior(CS);
  1643. if (AliasAnalysis::onlyReadsMemory(MRB))
  1644. return false;
  1645. if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
  1646. for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
  1647. I != E; ++I) {
  1648. const Value *Op = *I;
  1649. if (IsPotentialUse(Op) && PA.related(Ptr, Op))
  1650. return true;
  1651. }
  1652. return false;
  1653. }
  1654. // Assume the worst.
  1655. return true;
  1656. }
  1657. /// CanUse - Test whether the given instruction can "use" the given pointer's
  1658. /// object in a way that requires the reference count to be positive.
  1659. static bool
  1660. CanUse(const Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA,
  1661. InstructionClass Class) {
  1662. // IC_Call operations (as opposed to IC_CallOrUser) never "use" objc pointers.
  1663. if (Class == IC_Call)
  1664. return false;
  1665. // Consider various instructions which may have pointer arguments which are
  1666. // not "uses".
  1667. if (const ICmpInst *ICI = dyn_cast<ICmpInst>(Inst)) {
  1668. // Comparing a pointer with null, or any other constant, isn't really a use,
  1669. // because we don't care what the pointer points to, or about the values
  1670. // of any other dynamic reference-counted pointers.
  1671. if (!IsPotentialUse(ICI->getOperand(1)))
  1672. return false;
  1673. } else if (ImmutableCallSite CS = static_cast<const Value *>(Inst)) {
  1674. // For calls, just check the arguments (and not the callee operand).
  1675. for (ImmutableCallSite::arg_iterator OI = CS.arg_begin(),
  1676. OE = CS.arg_end(); OI != OE; ++OI) {
  1677. const Value *Op = *OI;
  1678. if (IsPotentialUse(Op) && PA.related(Ptr, Op))
  1679. return true;
  1680. }
  1681. return false;
  1682. } else if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
  1683. // Special-case stores, because we don't care about the stored value, just
  1684. // the store address.
  1685. const Value *Op = GetUnderlyingObjCPtr(SI->getPointerOperand());
  1686. // If we can't tell what the underlying object was, assume there is a
  1687. // dependence.
  1688. return IsPotentialUse(Op) && PA.related(Op, Ptr);
  1689. }
  1690. // Check each operand for a match.
  1691. for (User::const_op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
  1692. OI != OE; ++OI) {
  1693. const Value *Op = *OI;
  1694. if (IsPotentialUse(Op) && PA.related(Ptr, Op))
  1695. return true;
  1696. }
  1697. return false;
  1698. }
  1699. /// CanInterruptRV - Test whether the given instruction can autorelease
  1700. /// any pointer or cause an autoreleasepool pop.
  1701. static bool
  1702. CanInterruptRV(InstructionClass Class) {
  1703. switch (Class) {
  1704. case IC_AutoreleasepoolPop:
  1705. case IC_CallOrUser:
  1706. case IC_Call:
  1707. case IC_Autorelease:
  1708. case IC_AutoreleaseRV:
  1709. case IC_FusedRetainAutorelease:
  1710. case IC_FusedRetainAutoreleaseRV:
  1711. return true;
  1712. default:
  1713. return false;
  1714. }
  1715. }
  1716. namespace {
  1717. /// DependenceKind - There are several kinds of dependence-like concepts in
  1718. /// use here.
  1719. enum DependenceKind {
  1720. NeedsPositiveRetainCount,
  1721. AutoreleasePoolBoundary,
  1722. CanChangeRetainCount,
  1723. RetainAutoreleaseDep, ///< Blocks objc_retainAutorelease.
  1724. RetainAutoreleaseRVDep, ///< Blocks objc_retainAutoreleaseReturnValue.
  1725. RetainRVDep ///< Blocks objc_retainAutoreleasedReturnValue.
  1726. };
  1727. }
  1728. /// Depends - Test if there can be dependencies on Inst through Arg. This
  1729. /// function only tests dependencies relevant for removing pairs of calls.
  1730. static bool
  1731. Depends(DependenceKind Flavor, Instruction *Inst, const Value *Arg,
  1732. ProvenanceAnalysis &PA) {
  1733. // If we've reached the definition of Arg, stop.
  1734. if (Inst == Arg)
  1735. return true;
  1736. switch (Flavor) {
  1737. case NeedsPositiveRetainCount: {
  1738. InstructionClass Class = GetInstructionClass(Inst);
  1739. switch (Class) {
  1740. case IC_AutoreleasepoolPop:
  1741. case IC_AutoreleasepoolPush:
  1742. case IC_None:
  1743. return false;
  1744. default:
  1745. return CanUse(Inst, Arg, PA, Class);
  1746. }
  1747. }
  1748. case AutoreleasePoolBoundary: {
  1749. InstructionClass Class = GetInstructionClass(Inst);
  1750. switch (Class) {
  1751. case IC_AutoreleasepoolPop:
  1752. case IC_AutoreleasepoolPush:
  1753. // These mark the end and begin of an autorelease pool scope.
  1754. return true;
  1755. default:
  1756. // Nothing else does this.
  1757. return false;
  1758. }
  1759. }
  1760. case CanChangeRetainCount: {
  1761. InstructionClass Class = GetInstructionClass(Inst);
  1762. switch (Class) {
  1763. case IC_AutoreleasepoolPop:
  1764. // Conservatively assume this can decrement any count.
  1765. return true;
  1766. case IC_AutoreleasepoolPush:
  1767. case IC_None:
  1768. return false;
  1769. default:
  1770. return CanAlterRefCount(Inst, Arg, PA, Class);
  1771. }
  1772. }
  1773. case RetainAutoreleaseDep:
  1774. switch (GetBasicInstructionClass(Inst)) {
  1775. case IC_AutoreleasepoolPop:
  1776. case IC_AutoreleasepoolPush:
  1777. // Don't merge an objc_autorelease with an objc_retain inside a different
  1778. // autoreleasepool scope.
  1779. return true;
  1780. case IC_Retain:
  1781. case IC_RetainRV:
  1782. // Check for a retain of the same pointer for merging.
  1783. return GetObjCArg(Inst) == Arg;
  1784. default:
  1785. // Nothing else matters for objc_retainAutorelease formation.
  1786. return false;
  1787. }
  1788. case RetainAutoreleaseRVDep: {
  1789. InstructionClass Class = GetBasicInstructionClass(Inst);
  1790. switch (Class) {
  1791. case IC_Retain:
  1792. case IC_RetainRV:
  1793. // Check for a retain of the same pointer for merging.
  1794. return GetObjCArg(Inst) == Arg;
  1795. default:
  1796. // Anything that can autorelease interrupts
  1797. // retainAutoreleaseReturnValue formation.
  1798. return CanInterruptRV(Class);
  1799. }
  1800. }
  1801. case RetainRVDep:
  1802. return CanInterruptRV(GetBasicInstructionClass(Inst));
  1803. }
  1804. llvm_unreachable("Invalid dependence flavor");
  1805. }
  1806. /// FindDependencies - Walk up the CFG from StartPos (which is in StartBB) and
  1807. /// find local and non-local dependencies on Arg.
  1808. /// TODO: Cache results?
  1809. static void
  1810. FindDependencies(DependenceKind Flavor,
  1811. const Value *Arg,
  1812. BasicBlock *StartBB, Instruction *StartInst,
  1813. SmallPtrSet<Instruction *, 4> &DependingInstructions,
  1814. SmallPtrSet<const BasicBlock *, 4> &Visited,
  1815. ProvenanceAnalysis &PA) {
  1816. BasicBlock::iterator StartPos = StartInst;
  1817. SmallVector<std::pair<BasicBlock *, BasicBlock::iterator>, 4> Worklist;
  1818. Worklist.push_back(std::make_pair(StartBB, StartPos));
  1819. do {
  1820. std::pair<BasicBlock *, BasicBlock::iterator> Pair =
  1821. Worklist.pop_back_val();
  1822. BasicBlock *LocalStartBB = Pair.first;
  1823. BasicBlock::iterator LocalStartPos = Pair.second;
  1824. BasicBlock::iterator StartBBBegin = LocalStartBB->begin();
  1825. for (;;) {
  1826. if (LocalStartPos == StartBBBegin) {
  1827. pred_iterator PI(LocalStartBB), PE(LocalStartBB, false);
  1828. if (PI == PE)
  1829. // If we've reached the function entry, produce a null dependence.
  1830. DependingInstructions.insert(0);
  1831. else
  1832. // Add the predecessors to the worklist.
  1833. do {
  1834. BasicBlock *PredBB = *PI;
  1835. if (Visited.insert(PredBB))
  1836. Worklist.push_back(std::make_pair(PredBB, PredBB->end()));
  1837. } while (++PI != PE);
  1838. break;
  1839. }
  1840. Instruction *Inst = --LocalStartPos;
  1841. if (Depends(Flavor, Inst, Arg, PA)) {
  1842. DependingInstructions.insert(Inst);
  1843. break;
  1844. }
  1845. }
  1846. } while (!Worklist.empty());
  1847. // Determine whether the original StartBB post-dominates all of the blocks we
  1848. // visited. If not, insert a sentinal indicating that most optimizations are
  1849. // not safe.
  1850. for (SmallPtrSet<const BasicBlock *, 4>::const_iterator I = Visited.begin(),
  1851. E = Visited.end(); I != E; ++I) {
  1852. const BasicBlock *BB = *I;
  1853. if (BB == StartBB)
  1854. continue;
  1855. const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
  1856. for (succ_const_iterator SI(TI), SE(TI, false); SI != SE; ++SI) {
  1857. const BasicBlock *Succ = *SI;
  1858. if (Succ != StartBB && !Visited.count(Succ)) {
  1859. DependingInstructions.insert(reinterpret_cast<Instruction *>(-1));
  1860. return;
  1861. }
  1862. }
  1863. }
  1864. }
  1865. static bool isNullOrUndef(const Value *V) {
  1866. return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
  1867. }
  1868. static bool isNoopInstruction(const Instruction *I) {
  1869. return isa<BitCastInst>(I) ||
  1870. (isa<GetElementPtrInst>(I) &&
  1871. cast<GetElementPtrInst>(I)->hasAllZeroIndices());
  1872. }
  1873. /// OptimizeRetainCall - Turn objc_retain into
  1874. /// objc_retainAutoreleasedReturnValue if the operand is a return value.
  1875. void
  1876. ObjCARCOpt::OptimizeRetainCall(Function &F, Instruction *Retain) {
  1877. ImmutableCallSite CS(GetObjCArg(Retain));
  1878. const Instruction *Call = CS.getInstruction();
  1879. if (!Call) return;
  1880. if (Call->getParent() != Retain->getParent()) return;
  1881. // Check that the call is next to the retain.
  1882. BasicBlock::const_iterator I = Call;
  1883. ++I;
  1884. while (isNoopInstruction(I)) ++I;
  1885. if (&*I != Retain)
  1886. return;
  1887. // Turn it to an objc_retainAutoreleasedReturnValue..
  1888. Changed = true;
  1889. ++NumPeeps;
  1890. cast<CallInst>(Retain)->setCalledFunction(getRetainRVCallee(F.getParent()));
  1891. }
  1892. /// OptimizeRetainRVCall - Turn objc_retainAutoreleasedReturnValue into
  1893. /// objc_retain if the operand is not a return value. Or, if it can be paired
  1894. /// with an objc_autoreleaseReturnValue, delete the pair and return true.
  1895. bool
  1896. ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
  1897. // Check for the argument being from an immediately preceding call or invoke.
  1898. const Value *Arg = GetObjCArg(RetainRV);
  1899. ImmutableCallSite CS(Arg);
  1900. if (const Instruction *Call = CS.getInstruction()) {
  1901. if (Call->getParent() == RetainRV->getParent()) {
  1902. BasicBlock::const_iterator I = Call;
  1903. ++I;
  1904. while (isNoopInstruction(I)) ++I;
  1905. if (&*I == RetainRV)
  1906. return false;
  1907. } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
  1908. BasicBlock *RetainRVParent = RetainRV->getParent();
  1909. if (II->getNormalDest() == RetainRVParent) {
  1910. BasicBlock::const_iterator I = RetainRVParent->begin();
  1911. while (isNoopInstruction(I)) ++I;
  1912. if (&*I == RetainRV)
  1913. return false;
  1914. }
  1915. }
  1916. }
  1917. // Check for being preceded by an objc_autoreleaseReturnValue on the same
  1918. // pointer. In this case, we can delete the pair.
  1919. BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
  1920. if (I != Begin) {
  1921. do --I; while (I != Begin && isNoopInstruction(I));
  1922. if (GetBasicInstructionClass(I) == IC_AutoreleaseRV &&
  1923. GetObjCArg(I) == Arg) {
  1924. Changed = true;
  1925. ++NumPeeps;
  1926. EraseInstruction(I);
  1927. EraseInstruction(RetainRV);
  1928. return true;
  1929. }
  1930. }
  1931. // Turn it to a plain objc_retain.
  1932. Changed = true;
  1933. ++NumPeeps;
  1934. cast<CallInst>(RetainRV)->setCalledFunction(getRetainCallee(F.getParent()));
  1935. return false;
  1936. }
  1937. /// OptimizeAutoreleaseRVCall - Turn objc_autoreleaseReturnValue into
  1938. /// objc_autorelease if the result is not used as a return value.
  1939. void
  1940. ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV) {
  1941. // Check for a return of the pointer value.
  1942. const Value *Ptr = GetObjCArg(AutoreleaseRV);
  1943. SmallVector<const Value *, 2> Users;
  1944. Users.push_back(Ptr);
  1945. do {
  1946. Ptr = Users.pop_back_val();
  1947. for (Value::const_use_iterator UI = Ptr->use_begin(), UE = Ptr->use_end();
  1948. UI != UE; ++UI) {
  1949. const User *I = *UI;
  1950. if (isa<ReturnInst>(I) || GetBasicInstructionClass(I) == IC_RetainRV)
  1951. return;
  1952. if (isa<BitCastInst>(I))
  1953. Users.push_back(I);
  1954. }
  1955. } while (!Users.empty());
  1956. Changed = true;
  1957. ++NumPeeps;
  1958. cast<CallInst>(AutoreleaseRV)->
  1959. setCalledFunction(getAutoreleaseCallee(F.getParent()));
  1960. }
  1961. /// OptimizeIndividualCalls - Visit each call, one at a time, and make
  1962. /// simplifications without doing any additional analysis.
  1963. void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
  1964. // Reset all the flags in preparation for recomputing them.
  1965. UsedInThisFunction = 0;
  1966. // Visit all objc_* calls in F.
  1967. for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
  1968. Instruction *Inst = &*I++;
  1969. InstructionClass Class = GetBasicInstructionClass(Inst);
  1970. switch (Class) {
  1971. default: break;
  1972. // Delete no-op casts. These function calls have special semantics, but
  1973. // the semantics are entirely implemented via lowering in the front-end,
  1974. // so by the time they reach the optimizer, they are just no-op calls
  1975. // which return their argument.
  1976. //
  1977. // There are gray areas here, as the ability to cast reference-counted
  1978. // pointers to raw void* and back allows code to break ARC assumptions,
  1979. // however these are currently considered to be unimportant.
  1980. case IC_NoopCast:
  1981. Changed = true;
  1982. ++NumNoops;
  1983. EraseInstruction(Inst);
  1984. continue;
  1985. // If the pointer-to-weak-pointer is null, it's undefined behavior.
  1986. case IC_StoreWeak:
  1987. case IC_LoadWeak:
  1988. case IC_LoadWeakRetained:
  1989. case IC_InitWeak:
  1990. case IC_DestroyWeak: {
  1991. CallInst *CI = cast<CallInst>(Inst);
  1992. if (isNullOrUndef(CI->getArgOperand(0))) {
  1993. Changed = true;
  1994. Type *Ty = CI->getArgOperand(0)->getType();
  1995. new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
  1996. Constant::getNullValue(Ty),
  1997. CI);
  1998. CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
  1999. CI->eraseFromParent();
  2000. continue;
  2001. }
  2002. break;
  2003. }
  2004. case IC_CopyWeak:
  2005. case IC_MoveWeak: {
  2006. CallInst *CI = cast<CallInst>(Inst);
  2007. if (isNullOrUndef(CI->getArgOperand(0)) ||
  2008. isNullOrUndef(CI->getArgOperand(1))) {
  2009. Changed = true;
  2010. Type *Ty = CI->getArgOperand(0)->getType();
  2011. new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
  2012. Constant::getNullValue(Ty),
  2013. CI);
  2014. CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
  2015. CI->eraseFromParent();
  2016. continue;
  2017. }
  2018. break;
  2019. }
  2020. case IC_Retain:
  2021. OptimizeRetainCall(F, Inst);
  2022. break;
  2023. case IC_RetainRV:
  2024. if (OptimizeRetainRVCall(F, Inst))
  2025. continue;
  2026. break;
  2027. case IC_AutoreleaseRV:
  2028. OptimizeAutoreleaseRVCall(F, Inst);
  2029. break;
  2030. }
  2031. // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
  2032. if (IsAutorelease(Class) && Inst->use_empty()) {
  2033. CallInst *Call = cast<CallInst>(Inst);
  2034. const Value *Arg = Call->getArgOperand(0);
  2035. Arg = FindSingleUseIdentifiedObject(Arg);
  2036. if (Arg) {
  2037. Changed = true;
  2038. ++NumAutoreleases;
  2039. // Create the declaration lazily.
  2040. LLVMContext &C = Inst->getContext();
  2041. CallInst *NewCall =
  2042. CallInst::Create(getReleaseCallee(F.getParent()),
  2043. Call->getArgOperand(0), "", Call);
  2044. NewCall->setMetadata(ImpreciseReleaseMDKind,
  2045. MDNode::get(C, ArrayRef<Value *>()));
  2046. EraseInstruction(Call);
  2047. Inst = NewCall;
  2048. Class = IC_Release;
  2049. }
  2050. }
  2051. // For functions which can never be passed stack arguments, add
  2052. // a tail keyword.
  2053. if (IsAlwaysTail(Class)) {
  2054. Changed = true;
  2055. cast<CallInst>(Inst)->setTailCall();
  2056. }
  2057. // Set nounwind as needed.
  2058. if (IsNoThrow(Class)) {
  2059. Changed = true;
  2060. cast<CallInst>(Inst)->setDoesNotThrow();
  2061. }
  2062. if (!IsNoopOnNull(Class)) {
  2063. UsedInThisFunction |= 1 << Class;
  2064. continue;
  2065. }
  2066. const Value *Arg = GetObjCArg(Inst);
  2067. // ARC calls with null are no-ops. Delete them.
  2068. if (isNullOrUndef(Arg)) {
  2069. Changed = true;
  2070. ++NumNoops;
  2071. EraseInstruction(Inst);
  2072. continue;
  2073. }
  2074. // Keep track of which of retain, release, autorelease, and retain_block
  2075. // are actually present in this function.
  2076. UsedInThisFunction |= 1 << Class;
  2077. // If Arg is a PHI, and one or more incoming values to the
  2078. // PHI are null, and the call is control-equivalent to the PHI, and there
  2079. // are no relevant side effects between the PHI and the call, the call
  2080. // could be pushed up to just those paths with non-null incoming values.
  2081. // For now, don't bother splitting critical edges for this.
  2082. SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
  2083. Worklist.push_back(std::make_pair(Inst, Arg));
  2084. do {
  2085. std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
  2086. Inst = Pair.first;
  2087. Arg = Pair.second;
  2088. const PHINode *PN = dyn_cast<PHINode>(Arg);
  2089. if (!PN) continue;
  2090. // Determine if the PHI has any null operands, or any incoming
  2091. // critical edges.
  2092. bool HasNull = false;
  2093. bool HasCriticalEdges = false;
  2094. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  2095. Value *Incoming =
  2096. StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
  2097. if (isNullOrUndef(Incoming))
  2098. HasNull = true;
  2099. else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
  2100. .getNumSuccessors() != 1) {
  2101. HasCriticalEdges = true;
  2102. break;
  2103. }
  2104. }
  2105. // If we have null operands and no critical edges, optimize.
  2106. if (!HasCriticalEdges && HasNull) {
  2107. SmallPtrSet<Instruction *, 4> DependingInstructions;
  2108. SmallPtrSet<const BasicBlock *, 4> Visited;
  2109. // Check that there is nothing that cares about the reference
  2110. // count between the call and the phi.
  2111. switch (Class) {
  2112. case IC_Retain:
  2113. case IC_RetainBlock:
  2114. // These can always be moved up.
  2115. break;
  2116. case IC_Release:
  2117. // These can't be moved across things that care about the retain
  2118. // count.
  2119. FindDependencies(NeedsPositiveRetainCount, Arg,
  2120. Inst->getParent(), Inst,
  2121. DependingInstructions, Visited, PA);
  2122. break;
  2123. case IC_Autorelease:
  2124. // These can't be moved across autorelease pool scope boundaries.
  2125. FindDependencies(AutoreleasePoolBoundary, Arg,
  2126. Inst->getParent(), Inst,
  2127. DependingInstructions, Visited, PA);
  2128. break;
  2129. case IC_RetainRV:
  2130. case IC_AutoreleaseRV:
  2131. // Don't move these; the RV optimization depends on the autoreleaseRV
  2132. // being tail called, and the retainRV being immediately after a call
  2133. // (which might still happen if we get lucky with codegen layout, but
  2134. // it's not worth taking the chance).
  2135. continue;
  2136. default:
  2137. llvm_unreachable("Invalid dependence flavor");
  2138. }
  2139. if (DependingInstructions.size() == 1 &&
  2140. *DependingInstructions.begin() == PN) {
  2141. Changed = true;
  2142. ++NumPartialNoops;
  2143. // Clone the call into each predecessor that has a non-null value.
  2144. CallInst *CInst = cast<CallInst>(Inst);
  2145. Type *ParamTy = CInst->getArgOperand(0)->getType();
  2146. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  2147. Value *Incoming =
  2148. StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
  2149. if (!isNullOrUndef(Incoming)) {
  2150. CallInst *Clone = cast<CallInst>(CInst->clone());
  2151. Value *Op = PN->getIncomingValue(i);
  2152. Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
  2153. if (Op->getType() != ParamTy)
  2154. Op = new BitCastInst(Op, ParamTy, "", InsertPos);
  2155. Clone->setArgOperand(0, Op);
  2156. Clone->insertBefore(InsertPos);
  2157. Worklist.push_back(std::make_pair(Clone, Incoming));
  2158. }
  2159. }
  2160. // Erase the original call.
  2161. EraseInstruction(CInst);
  2162. continue;
  2163. }
  2164. }
  2165. } while (!Worklist.empty());
  2166. }
  2167. }
  2168. /// CheckForCFGHazards - Check for critical edges, loop boundaries, irreducible
  2169. /// control flow, or other CFG structures where moving code across the edge
  2170. /// would result in it being executed more.
  2171. void
  2172. ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
  2173. DenseMap<const BasicBlock *, BBState> &BBStates,
  2174. BBState &MyStates) const {
  2175. // If any top-down local-use or possible-dec has a succ which is earlier in
  2176. // the sequence, forget it.
  2177. for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
  2178. E = MyStates.top_down_ptr_end(); I != E; ++I)
  2179. switch (I->second.GetSeq()) {
  2180. default: break;
  2181. case S_Use: {
  2182. const Value *Arg = I->first;
  2183. const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
  2184. bool SomeSuccHasSame = false;
  2185. bool AllSuccsHaveSame = true;
  2186. PtrState &S = I->second;
  2187. succ_const_iterator SI(TI), SE(TI, false);
  2188. // If the terminator is an invoke marked with the
  2189. // clang.arc.no_objc_arc_exceptions metadata, the unwind edge can be
  2190. // ignored, for ARC purposes.
  2191. if (isa<InvokeInst>(TI) && TI->getMetadata(NoObjCARCExceptionsMDKind))
  2192. --SE;
  2193. for (; SI != SE; ++SI) {
  2194. Sequence SuccSSeq = S_None;
  2195. bool SuccSRRIKnownSafe = false;
  2196. // If VisitBottomUp has pointer information for this successor, take
  2197. // what we know about it.
  2198. DenseMap<const BasicBlock *, BBState>::iterator BBI =
  2199. BBStates.find(*SI);
  2200. assert(BBI != BBStates.end());
  2201. const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
  2202. SuccSSeq = SuccS.GetSeq();
  2203. SuccSRRIKnownSafe = SuccS.RRI.KnownSafe;
  2204. switch (SuccSSeq) {
  2205. case S_None:
  2206. case S_CanRelease: {
  2207. if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
  2208. S.ClearSequenceProgress();
  2209. break;
  2210. }
  2211. continue;
  2212. }
  2213. case S_Use:
  2214. SomeSuccHasSame = true;
  2215. break;
  2216. case S_Stop:
  2217. case S_Release:
  2218. case S_MovableRelease:
  2219. if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
  2220. AllSuccsHaveSame = false;
  2221. break;
  2222. case S_Retain:
  2223. llvm_unreachable("bottom-up pointer in retain state!");
  2224. }
  2225. }
  2226. // If the state at the other end of any of the successor edges
  2227. // matches the current state, require all edges to match. This
  2228. // guards against loops in the middle of a sequence.
  2229. if (SomeSuccHasSame && !AllSuccsHaveSame)
  2230. S.ClearSequenceProgress();
  2231. break;
  2232. }
  2233. case S_CanRelease: {
  2234. const Value *Arg = I->first;
  2235. const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
  2236. bool SomeSuccHasSame = false;
  2237. bool AllSuccsHaveSame = true;
  2238. PtrState &S = I->second;
  2239. succ_const_iterator SI(TI), SE(TI, false);
  2240. // If the terminator is an invoke marked with the
  2241. // clang.arc.no_objc_arc_exceptions metadata, the unwind edge can be
  2242. // ignored, for ARC purposes.
  2243. if (isa<InvokeInst>(TI) && TI->getMetadata(NoObjCARCExceptionsMDKind))
  2244. --SE;
  2245. for (; SI != SE; ++SI) {
  2246. Sequence SuccSSeq = S_None;
  2247. bool SuccSRRIKnownSafe = false;
  2248. // If VisitBottomUp has pointer information for this successor, take
  2249. // what we know about it.
  2250. DenseMap<const BasicBlock *, BBState>::iterator BBI =
  2251. BBStates.find(*SI);
  2252. assert(BBI != BBStates.end());
  2253. const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
  2254. SuccSSeq = SuccS.GetSeq();
  2255. SuccSRRIKnownSafe = SuccS.RRI.KnownSafe;
  2256. switch (SuccSSeq) {
  2257. case S_None: {
  2258. if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
  2259. S.ClearSequenceProgress();
  2260. break;
  2261. }
  2262. continue;
  2263. }
  2264. case S_CanRelease:
  2265. SomeSuccHasSame = true;
  2266. break;
  2267. case S_Stop:
  2268. case S_Release:
  2269. case S_MovableRelease:
  2270. case S_Use:
  2271. if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
  2272. AllSuccsHaveSame = false;
  2273. break;
  2274. case S_Retain:
  2275. llvm_unreachable("bottom-up pointer in retain state!");
  2276. }
  2277. }
  2278. // If the state at the other end of any of the successor edges
  2279. // matches the current state, require all edges to match. This
  2280. // guards against loops in the middle of a sequence.
  2281. if (SomeSuccHasSame && !AllSuccsHaveSame)
  2282. S.ClearSequenceProgress();
  2283. break;
  2284. }
  2285. }
  2286. }
  2287. bool
  2288. ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
  2289. BasicBlock *BB,
  2290. MapVector<Value *, RRInfo> &Retains,
  2291. BBState &MyStates) {
  2292. bool NestingDetected = false;
  2293. InstructionClass Class = GetInstructionClass(Inst);
  2294. const Value *Arg = 0;
  2295. switch (Class) {
  2296. case IC_Release: {
  2297. Arg = GetObjCArg(Inst);
  2298. PtrState &S = MyStates.getPtrBottomUpState(Arg);
  2299. // If we see two releases in a row on the same pointer. If so, make
  2300. // a note, and we'll cicle back to revisit it after we've
  2301. // hopefully eliminated the second release, which may allow us to
  2302. // eliminate the first release too.
  2303. // Theoretically we could implement removal of nested retain+release
  2304. // pairs by making PtrState hold a stack of states, but this is
  2305. // simple and avoids adding overhead for the non-nested case.
  2306. if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease)
  2307. NestingDetected = true;
  2308. MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
  2309. S.ResetSequenceProgress(ReleaseMetadata ? S_MovableRelease : S_Release);
  2310. S.RRI.ReleaseMetadata = ReleaseMetadata;
  2311. S.RRI.KnownSafe = S.IsKnownNested() || S.IsKnownIncremented();
  2312. S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
  2313. S.RRI.Calls.insert(Inst);
  2314. S.IncrementNestCount();
  2315. break;
  2316. }
  2317. case IC_RetainBlock:
  2318. // An objc_retainBlock call with just a use may need to be kept,
  2319. // because it may be copying a block from the stack to the heap.
  2320. if (!IsRetainBlockOptimizable(Inst))
  2321. break;
  2322. // FALLTHROUGH
  2323. case IC_Retain:
  2324. case IC_RetainRV: {
  2325. Arg = GetObjCArg(Inst);
  2326. PtrState &S = MyStates.getPtrBottomUpState(Arg);
  2327. S.SetKnownPositiveRefCount();
  2328. S.DecrementNestCount();
  2329. switch (S.GetSeq()) {
  2330. case S_Stop:
  2331. case S_Release:
  2332. case S_MovableRelease:
  2333. case S_Use:
  2334. S.RRI.ReverseInsertPts.clear();
  2335. // FALL THROUGH
  2336. case S_CanRelease:
  2337. // Don't do retain+release tracking for IC_RetainRV, because it's
  2338. // better to let it remain as the first instruction after a call.
  2339. if (Class != IC_RetainRV) {
  2340. S.RRI.IsRetainBlock = Class == IC_RetainBlock;
  2341. Retains[Inst] = S.RRI;
  2342. }
  2343. S.ClearSequenceProgress();
  2344. break;
  2345. case S_None:
  2346. break;
  2347. case S_Retain:
  2348. llvm_unreachable("bottom-up pointer in retain state!");
  2349. }
  2350. return NestingDetected;
  2351. }
  2352. case IC_AutoreleasepoolPop:
  2353. // Conservatively, clear MyStates for all known pointers.
  2354. MyStates.clearBottomUpPointers();
  2355. return NestingDetected;
  2356. case IC_AutoreleasepoolPush:
  2357. case IC_None:
  2358. // These are irrelevant.
  2359. return NestingDetected;
  2360. default:
  2361. break;
  2362. }
  2363. // Consider any other possible effects of this instruction on each
  2364. // pointer being tracked.
  2365. for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(),
  2366. ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) {
  2367. const Value *Ptr = MI->first;
  2368. if (Ptr == Arg)
  2369. continue; // Handled above.
  2370. PtrState &S = MI->second;
  2371. Sequence Seq = S.GetSeq();
  2372. // Check for possible releases.
  2373. if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
  2374. S.ClearRefCount();
  2375. switch (Seq) {
  2376. case S_Use:
  2377. S.SetSeq(S_CanRelease);
  2378. continue;
  2379. case S_CanRelease:
  2380. case S_Release:
  2381. case S_MovableRelease:
  2382. case S_Stop:
  2383. case S_None:
  2384. break;
  2385. case S_Retain:
  2386. llvm_unreachable("bottom-up pointer in retain state!");
  2387. }
  2388. }
  2389. // Check for possible direct uses.
  2390. switch (Seq) {
  2391. case S_Release:
  2392. case S_MovableRelease:
  2393. if (CanUse(Inst, Ptr, PA, Class)) {
  2394. assert(S.RRI.ReverseInsertPts.empty());
  2395. // If this is an invoke instruction, we're scanning it as part of
  2396. // one of its successor blocks, since we can't insert code after it
  2397. // in its own block, and we don't want to split critical edges.
  2398. if (isa<InvokeInst>(Inst))
  2399. S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
  2400. else
  2401. S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
  2402. S.SetSeq(S_Use);
  2403. } else if (Seq == S_Release &&
  2404. (Class == IC_User || Class == IC_CallOrUser)) {
  2405. // Non-movable releases depend on any possible objc pointer use.
  2406. S.SetSeq(S_Stop);
  2407. assert(S.RRI.ReverseInsertPts.empty());
  2408. // As above; handle invoke specially.
  2409. if (isa<InvokeInst>(Inst))
  2410. S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
  2411. else
  2412. S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
  2413. }
  2414. break;
  2415. case S_Stop:
  2416. if (CanUse(Inst, Ptr, PA, Class))
  2417. S.SetSeq(S_Use);
  2418. break;
  2419. case S_CanRelease:
  2420. case S_Use:
  2421. case S_None:
  2422. break;
  2423. case S_Retain:
  2424. llvm_unreachable("bottom-up pointer in retain state!");
  2425. }
  2426. }
  2427. return NestingDetected;
  2428. }
  2429. bool
  2430. ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
  2431. DenseMap<const BasicBlock *, BBState> &BBStates,
  2432. MapVector<Value *, RRInfo> &Retains) {
  2433. bool NestingDetected = false;
  2434. BBState &MyStates = BBStates[BB];
  2435. // Merge the states from each successor to compute the initial state
  2436. // for the current block.
  2437. for (BBState::edge_iterator SI(MyStates.succ_begin()),
  2438. SE(MyStates.succ_end()); SI != SE; ++SI) {
  2439. const BasicBlock *Succ = *SI;
  2440. DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
  2441. assert(I != BBStates.end());
  2442. MyStates.InitFromSucc(I->second);
  2443. ++SI;
  2444. for (; SI != SE; ++SI) {
  2445. Succ = *SI;
  2446. I = BBStates.find(Succ);
  2447. assert(I != BBStates.end());
  2448. MyStates.MergeSucc(I->second);
  2449. }
  2450. break;
  2451. }
  2452. // Visit all the instructions, bottom-up.
  2453. for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
  2454. Instruction *Inst = llvm::prior(I);
  2455. // Invoke instructions are visited as part of their successors (below).
  2456. if (isa<InvokeInst>(Inst))
  2457. continue;
  2458. NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
  2459. }
  2460. // If there's a predecessor with an invoke, visit the invoke as if it were
  2461. // part of this block, since we can't insert code after an invoke in its own
  2462. // block, and we don't want to split critical edges.
  2463. for (BBState::edge_iterator PI(MyStates.pred_begin()),
  2464. PE(MyStates.pred_end()); PI != PE; ++PI) {
  2465. BasicBlock *Pred = *PI;
  2466. if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
  2467. NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
  2468. }
  2469. return NestingDetected;
  2470. }
  2471. bool
  2472. ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
  2473. DenseMap<Value *, RRInfo> &Releases,
  2474. BBState &MyStates) {
  2475. bool NestingDetected = false;
  2476. InstructionClass Class = GetInstructionClass(Inst);
  2477. const Value *Arg = 0;
  2478. switch (Class) {
  2479. case IC_RetainBlock:
  2480. // An objc_retainBlock call with just a use may need to be kept,
  2481. // because it may be copying a block from the stack to the heap.
  2482. if (!IsRetainBlockOptimizable(Inst))
  2483. break;
  2484. // FALLTHROUGH
  2485. case IC_Retain:
  2486. case IC_RetainRV: {
  2487. Arg = GetObjCArg(Inst);
  2488. PtrState &S = MyStates.getPtrTopDownState(Arg);
  2489. // Don't do retain+release tracking for IC_RetainRV, because it's
  2490. // better to let it remain as the first instruction after a call.
  2491. if (Class != IC_RetainRV) {
  2492. // If we see two retains in a row on the same pointer. If so, make
  2493. // a note, and we'll cicle back to revisit it after we've
  2494. // hopefully eliminated the second retain, which may allow us to
  2495. // eliminate the first retain too.
  2496. // Theoretically we could implement removal of nested retain+release
  2497. // pairs by making PtrState hold a stack of states, but this is
  2498. // simple and avoids adding overhead for the non-nested case.
  2499. if (S.GetSeq() == S_Retain)
  2500. NestingDetected = true;
  2501. S.ResetSequenceProgress(S_Retain);
  2502. S.RRI.IsRetainBlock = Class == IC_RetainBlock;
  2503. // Don't check S.IsKnownIncremented() here because it's not sufficient.
  2504. S.RRI.KnownSafe = S.IsKnownNested();
  2505. S.RRI.Calls.insert(Inst);
  2506. }
  2507. S.IncrementNestCount();
  2508. // A retain can be a potential use; procede to the generic checking
  2509. // code below.
  2510. break;
  2511. }
  2512. case IC_Release: {
  2513. Arg = GetObjCArg(Inst);
  2514. PtrState &S = MyStates.getPtrTopDownState(Arg);
  2515. S.DecrementNestCount();
  2516. switch (S.GetSeq()) {
  2517. case S_Retain:
  2518. case S_CanRelease:
  2519. S.RRI.ReverseInsertPts.clear();
  2520. // FALL THROUGH
  2521. case S_Use:
  2522. S.RRI.ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
  2523. S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
  2524. Releases[Inst] = S.RRI;
  2525. S.ClearSequenceProgress();
  2526. break;
  2527. case S_None:
  2528. break;
  2529. case S_Stop:
  2530. case S_Release:
  2531. case S_MovableRelease:
  2532. llvm_unreachable("top-down pointer in release state!");
  2533. }
  2534. break;
  2535. }
  2536. case IC_AutoreleasepoolPop:
  2537. // Conservatively, clear MyStates for all known pointers.
  2538. MyStates.clearTopDownPointers();
  2539. return NestingDetected;
  2540. case IC_AutoreleasepoolPush:
  2541. case IC_None:
  2542. // These are irrelevant.
  2543. return NestingDetected;
  2544. default:
  2545. break;
  2546. }
  2547. // Consider any other possible effects of this instruction on each
  2548. // pointer being tracked.
  2549. for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
  2550. ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
  2551. const Value *Ptr = MI->first;
  2552. if (Ptr == Arg)
  2553. continue; // Handled above.
  2554. PtrState &S = MI->second;
  2555. Sequence Seq = S.GetSeq();
  2556. // Check for possible releases.
  2557. if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
  2558. S.ClearRefCount();
  2559. switch (Seq) {
  2560. case S_Retain:
  2561. S.SetSeq(S_CanRelease);
  2562. assert(S.RRI.ReverseInsertPts.empty());
  2563. S.RRI.ReverseInsertPts.insert(Inst);
  2564. // One call can't cause a transition from S_Retain to S_CanRelease
  2565. // and S_CanRelease to S_Use. If we've made the first transition,
  2566. // we're done.
  2567. continue;
  2568. case S_Use:
  2569. case S_CanRelease:
  2570. case S_None:
  2571. break;
  2572. case S_Stop:
  2573. case S_Release:
  2574. case S_MovableRelease:
  2575. llvm_unreachable("top-down pointer in release state!");
  2576. }
  2577. }
  2578. // Check for possible direct uses.
  2579. switch (Seq) {
  2580. case S_CanRelease:
  2581. if (CanUse(Inst, Ptr, PA, Class))
  2582. S.SetSeq(S_Use);
  2583. break;
  2584. case S_Retain:
  2585. case S_Use:
  2586. case S_None:
  2587. break;
  2588. case S_Stop:
  2589. case S_Release:
  2590. case S_MovableRelease:
  2591. llvm_unreachable("top-down pointer in release state!");
  2592. }
  2593. }
  2594. return NestingDetected;
  2595. }
  2596. bool
  2597. ObjCARCOpt::VisitTopDown(BasicBlock *BB,
  2598. DenseMap<const BasicBlock *, BBState> &BBStates,
  2599. DenseMap<Value *, RRInfo> &Releases) {
  2600. bool NestingDetected = false;
  2601. BBState &MyStates = BBStates[BB];
  2602. // Merge the states from each predecessor to compute the initial state
  2603. // for the current block.
  2604. for (BBState::edge_iterator PI(MyStates.pred_begin()),
  2605. PE(MyStates.pred_end()); PI != PE; ++PI) {
  2606. const BasicBlock *Pred = *PI;
  2607. DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
  2608. assert(I != BBStates.end());
  2609. MyStates.InitFromPred(I->second);
  2610. ++PI;
  2611. for (; PI != PE; ++PI) {
  2612. Pred = *PI;
  2613. I = BBStates.find(Pred);
  2614. assert(I != BBStates.end());
  2615. MyStates.MergePred(I->second);
  2616. }
  2617. break;
  2618. }
  2619. // Visit all the instructions, top-down.
  2620. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
  2621. Instruction *Inst = I;
  2622. NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
  2623. }
  2624. CheckForCFGHazards(BB, BBStates, MyStates);
  2625. return NestingDetected;
  2626. }
  2627. static void
  2628. ComputePostOrders(Function &F,
  2629. SmallVectorImpl<BasicBlock *> &PostOrder,
  2630. SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
  2631. unsigned NoObjCARCExceptionsMDKind,
  2632. DenseMap<const BasicBlock *, BBState> &BBStates) {
  2633. /// Visited - The visited set, for doing DFS walks.
  2634. SmallPtrSet<BasicBlock *, 16> Visited;
  2635. // Do DFS, computing the PostOrder.
  2636. SmallPtrSet<BasicBlock *, 16> OnStack;
  2637. SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
  2638. // Functions always have exactly one entry block, and we don't have
  2639. // any other block that we treat like an entry block.
  2640. BasicBlock *EntryBB = &F.getEntryBlock();
  2641. BBState &MyStates = BBStates[EntryBB];
  2642. MyStates.SetAsEntry();
  2643. TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
  2644. SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
  2645. Visited.insert(EntryBB);
  2646. OnStack.insert(EntryBB);
  2647. do {
  2648. dfs_next_succ:
  2649. BasicBlock *CurrBB = SuccStack.back().first;
  2650. TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
  2651. succ_iterator SE(TI, false);
  2652. // If the terminator is an invoke marked with the
  2653. // clang.arc.no_objc_arc_exceptions metadata, the unwind edge can be
  2654. // ignored, for ARC purposes.
  2655. if (isa<InvokeInst>(TI) && TI->getMetadata(NoObjCARCExceptionsMDKind))
  2656. --SE;
  2657. while (SuccStack.back().second != SE) {
  2658. BasicBlock *SuccBB = *SuccStack.back().second++;
  2659. if (Visited.insert(SuccBB)) {
  2660. TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
  2661. SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
  2662. BBStates[CurrBB].addSucc(SuccBB);
  2663. BBState &SuccStates = BBStates[SuccBB];
  2664. SuccStates.addPred(CurrBB);
  2665. OnStack.insert(SuccBB);
  2666. goto dfs_next_succ;
  2667. }
  2668. if (!OnStack.count(SuccBB)) {
  2669. BBStates[CurrBB].addSucc(SuccBB);
  2670. BBStates[SuccBB].addPred(CurrBB);
  2671. }
  2672. }
  2673. OnStack.erase(CurrBB);
  2674. PostOrder.push_back(CurrBB);
  2675. SuccStack.pop_back();
  2676. } while (!SuccStack.empty());
  2677. Visited.clear();
  2678. // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
  2679. // Functions may have many exits, and there also blocks which we treat
  2680. // as exits due to ignored edges.
  2681. SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
  2682. for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
  2683. BasicBlock *ExitBB = I;
  2684. BBState &MyStates = BBStates[ExitBB];
  2685. if (!MyStates.isExit())
  2686. continue;
  2687. MyStates.SetAsExit();
  2688. PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
  2689. Visited.insert(ExitBB);
  2690. while (!PredStack.empty()) {
  2691. reverse_dfs_next_succ:
  2692. BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
  2693. while (PredStack.back().second != PE) {
  2694. BasicBlock *BB = *PredStack.back().second++;
  2695. if (Visited.insert(BB)) {
  2696. PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
  2697. goto reverse_dfs_next_succ;
  2698. }
  2699. }
  2700. ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
  2701. }
  2702. }
  2703. }
  2704. // Visit - Visit the function both top-down and bottom-up.
  2705. bool
  2706. ObjCARCOpt::Visit(Function &F,
  2707. DenseMap<const BasicBlock *, BBState> &BBStates,
  2708. MapVector<Value *, RRInfo> &Retains,
  2709. DenseMap<Value *, RRInfo> &Releases) {
  2710. // Use reverse-postorder traversals, because we magically know that loops
  2711. // will be well behaved, i.e. they won't repeatedly call retain on a single
  2712. // pointer without doing a release. We can't use the ReversePostOrderTraversal
  2713. // class here because we want the reverse-CFG postorder to consider each
  2714. // function exit point, and we want to ignore selected cycle edges.
  2715. SmallVector<BasicBlock *, 16> PostOrder;
  2716. SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
  2717. ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
  2718. NoObjCARCExceptionsMDKind,
  2719. BBStates);
  2720. // Use reverse-postorder on the reverse CFG for bottom-up.
  2721. bool BottomUpNestingDetected = false;
  2722. for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
  2723. ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
  2724. I != E; ++I)
  2725. BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
  2726. // Use reverse-postorder for top-down.
  2727. bool TopDownNestingDetected = false;
  2728. for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
  2729. PostOrder.rbegin(), E = PostOrder.rend();
  2730. I != E; ++I)
  2731. TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
  2732. return TopDownNestingDetected && BottomUpNestingDetected;
  2733. }
  2734. /// MoveCalls - Move the calls in RetainsToMove and ReleasesToMove.
  2735. void ObjCARCOpt::MoveCalls(Value *Arg,
  2736. RRInfo &RetainsToMove,
  2737. RRInfo &ReleasesToMove,
  2738. MapVector<Value *, RRInfo> &Retains,
  2739. DenseMap<Value *, RRInfo> &Releases,
  2740. SmallVectorImpl<Instruction *> &DeadInsts,
  2741. Module *M) {
  2742. Type *ArgTy = Arg->getType();
  2743. Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
  2744. // Insert the new retain and release calls.
  2745. for (SmallPtrSet<Instruction *, 2>::const_iterator
  2746. PI = ReleasesToMove.ReverseInsertPts.begin(),
  2747. PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
  2748. Instruction *InsertPt = *PI;
  2749. Value *MyArg = ArgTy == ParamTy ? Arg :
  2750. new BitCastInst(Arg, ParamTy, "", InsertPt);
  2751. CallInst *Call =
  2752. CallInst::Create(RetainsToMove.IsRetainBlock ?
  2753. getRetainBlockCallee(M) : getRetainCallee(M),
  2754. MyArg, "", InsertPt);
  2755. Call->setDoesNotThrow();
  2756. if (RetainsToMove.IsRetainBlock)
  2757. Call->setMetadata(CopyOnEscapeMDKind,
  2758. MDNode::get(M->getContext(), ArrayRef<Value *>()));
  2759. else
  2760. Call->setTailCall();
  2761. }
  2762. for (SmallPtrSet<Instruction *, 2>::const_iterator
  2763. PI = RetainsToMove.ReverseInsertPts.begin(),
  2764. PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
  2765. Instruction *InsertPt = *PI;
  2766. Value *MyArg = ArgTy == ParamTy ? Arg :
  2767. new BitCastInst(Arg, ParamTy, "", InsertPt);
  2768. CallInst *Call = CallInst::Create(getReleaseCallee(M), MyArg,
  2769. "", InsertPt);
  2770. // Attach a clang.imprecise_release metadata tag, if appropriate.
  2771. if (MDNode *M = ReleasesToMove.ReleaseMetadata)
  2772. Call->setMetadata(ImpreciseReleaseMDKind, M);
  2773. Call->setDoesNotThrow();
  2774. if (ReleasesToMove.IsTailCallRelease)
  2775. Call->setTailCall();
  2776. }
  2777. // Delete the original retain and release calls.
  2778. for (SmallPtrSet<Instruction *, 2>::const_iterator
  2779. AI = RetainsToMove.Calls.begin(),
  2780. AE = RetainsToMove.Calls.end(); AI != AE; ++AI) {
  2781. Instruction *OrigRetain = *AI;
  2782. Retains.blot(OrigRetain);
  2783. DeadInsts.push_back(OrigRetain);
  2784. }
  2785. for (SmallPtrSet<Instruction *, 2>::const_iterator
  2786. AI = ReleasesToMove.Calls.begin(),
  2787. AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) {
  2788. Instruction *OrigRelease = *AI;
  2789. Releases.erase(OrigRelease);
  2790. DeadInsts.push_back(OrigRelease);
  2791. }
  2792. }
  2793. /// PerformCodePlacement - Identify pairings between the retains and releases,
  2794. /// and delete and/or move them.
  2795. bool
  2796. ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
  2797. &BBStates,
  2798. MapVector<Value *, RRInfo> &Retains,
  2799. DenseMap<Value *, RRInfo> &Releases,
  2800. Module *M) {
  2801. bool AnyPairsCompletelyEliminated = false;
  2802. RRInfo RetainsToMove;
  2803. RRInfo ReleasesToMove;
  2804. SmallVector<Instruction *, 4> NewRetains;
  2805. SmallVector<Instruction *, 4> NewReleases;
  2806. SmallVector<Instruction *, 8> DeadInsts;
  2807. // Visit each retain.
  2808. for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
  2809. E = Retains.end(); I != E; ++I) {
  2810. Value *V = I->first;
  2811. if (!V) continue; // blotted
  2812. Instruction *Retain = cast<Instruction>(V);
  2813. Value *Arg = GetObjCArg(Retain);
  2814. // If the object being released is in static or stack storage, we know it's
  2815. // not being managed by ObjC reference counting, so we can delete pairs
  2816. // regardless of what possible decrements or uses lie between them.
  2817. bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
  2818. // A constant pointer can't be pointing to an object on the heap. It may
  2819. // be reference-counted, but it won't be deleted.
  2820. if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
  2821. if (const GlobalVariable *GV =
  2822. dyn_cast<GlobalVariable>(
  2823. StripPointerCastsAndObjCCalls(LI->getPointerOperand())))
  2824. if (GV->isConstant())
  2825. KnownSafe = true;
  2826. // If a pair happens in a region where it is known that the reference count
  2827. // is already incremented, we can similarly ignore possible decrements.
  2828. bool KnownSafeTD = true, KnownSafeBU = true;
  2829. // Connect the dots between the top-down-collected RetainsToMove and
  2830. // bottom-up-collected ReleasesToMove to form sets of related calls.
  2831. // This is an iterative process so that we connect multiple releases
  2832. // to multiple retains if needed.
  2833. unsigned OldDelta = 0;
  2834. unsigned NewDelta = 0;
  2835. unsigned OldCount = 0;
  2836. unsigned NewCount = 0;
  2837. bool FirstRelease = true;
  2838. bool FirstRetain = true;
  2839. NewRetains.push_back(Retain);
  2840. for (;;) {
  2841. for (SmallVectorImpl<Instruction *>::const_iterator
  2842. NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
  2843. Instruction *NewRetain = *NI;
  2844. MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain);
  2845. assert(It != Retains.end());
  2846. const RRInfo &NewRetainRRI = It->second;
  2847. KnownSafeTD &= NewRetainRRI.KnownSafe;
  2848. for (SmallPtrSet<Instruction *, 2>::const_iterator
  2849. LI = NewRetainRRI.Calls.begin(),
  2850. LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) {
  2851. Instruction *NewRetainRelease = *LI;
  2852. DenseMap<Value *, RRInfo>::const_iterator Jt =
  2853. Releases.find(NewRetainRelease);
  2854. if (Jt == Releases.end())
  2855. goto next_retain;
  2856. const RRInfo &NewRetainReleaseRRI = Jt->second;
  2857. assert(NewRetainReleaseRRI.Calls.count(NewRetain));
  2858. if (ReleasesToMove.Calls.insert(NewRetainRelease)) {
  2859. OldDelta -=
  2860. BBStates[NewRetainRelease->getParent()].GetAllPathCount();
  2861. // Merge the ReleaseMetadata and IsTailCallRelease values.
  2862. if (FirstRelease) {
  2863. ReleasesToMove.ReleaseMetadata =
  2864. NewRetainReleaseRRI.ReleaseMetadata;
  2865. ReleasesToMove.IsTailCallRelease =
  2866. NewRetainReleaseRRI.IsTailCallRelease;
  2867. FirstRelease = false;
  2868. } else {
  2869. if (ReleasesToMove.ReleaseMetadata !=
  2870. NewRetainReleaseRRI.ReleaseMetadata)
  2871. ReleasesToMove.ReleaseMetadata = 0;
  2872. if (ReleasesToMove.IsTailCallRelease !=
  2873. NewRetainReleaseRRI.IsTailCallRelease)
  2874. ReleasesToMove.IsTailCallRelease = false;
  2875. }
  2876. // Collect the optimal insertion points.
  2877. if (!KnownSafe)
  2878. for (SmallPtrSet<Instruction *, 2>::const_iterator
  2879. RI = NewRetainReleaseRRI.ReverseInsertPts.begin(),
  2880. RE = NewRetainReleaseRRI.ReverseInsertPts.end();
  2881. RI != RE; ++RI) {
  2882. Instruction *RIP = *RI;
  2883. if (ReleasesToMove.ReverseInsertPts.insert(RIP))
  2884. NewDelta -= BBStates[RIP->getParent()].GetAllPathCount();
  2885. }
  2886. NewReleases.push_back(NewRetainRelease);
  2887. }
  2888. }
  2889. }
  2890. NewRetains.clear();
  2891. if (NewReleases.empty()) break;
  2892. // Back the other way.
  2893. for (SmallVectorImpl<Instruction *>::const_iterator
  2894. NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
  2895. Instruction *NewRelease = *NI;
  2896. DenseMap<Value *, RRInfo>::const_iterator It =
  2897. Releases.find(NewRelease);
  2898. assert(It != Releases.end());
  2899. const RRInfo &NewReleaseRRI = It->second;
  2900. KnownSafeBU &= NewReleaseRRI.KnownSafe;
  2901. for (SmallPtrSet<Instruction *, 2>::const_iterator
  2902. LI = NewReleaseRRI.Calls.begin(),
  2903. LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) {
  2904. Instruction *NewReleaseRetain = *LI;
  2905. MapVector<Value *, RRInfo>::const_iterator Jt =
  2906. Retains.find(NewReleaseRetain);
  2907. if (Jt == Retains.end())
  2908. goto next_retain;
  2909. const RRInfo &NewReleaseRetainRRI = Jt->second;
  2910. assert(NewReleaseRetainRRI.Calls.count(NewRelease));
  2911. if (RetainsToMove.Calls.insert(NewReleaseRetain)) {
  2912. unsigned PathCount =
  2913. BBStates[NewReleaseRetain->getParent()].GetAllPathCount();
  2914. OldDelta += PathCount;
  2915. OldCount += PathCount;
  2916. // Merge the IsRetainBlock values.
  2917. if (FirstRetain) {
  2918. RetainsToMove.IsRetainBlock = NewReleaseRetainRRI.IsRetainBlock;
  2919. FirstRetain = false;
  2920. } else if (ReleasesToMove.IsRetainBlock !=
  2921. NewReleaseRetainRRI.IsRetainBlock)
  2922. // It's not possible to merge the sequences if one uses
  2923. // objc_retain and the other uses objc_retainBlock.
  2924. goto next_retain;
  2925. // Collect the optimal insertion points.
  2926. if (!KnownSafe)
  2927. for (SmallPtrSet<Instruction *, 2>::const_iterator
  2928. RI = NewReleaseRetainRRI.ReverseInsertPts.begin(),
  2929. RE = NewReleaseRetainRRI.ReverseInsertPts.end();
  2930. RI != RE; ++RI) {
  2931. Instruction *RIP = *RI;
  2932. if (RetainsToMove.ReverseInsertPts.insert(RIP)) {
  2933. PathCount = BBStates[RIP->getParent()].GetAllPathCount();
  2934. NewDelta += PathCount;
  2935. NewCount += PathCount;
  2936. }
  2937. }
  2938. NewRetains.push_back(NewReleaseRetain);
  2939. }
  2940. }
  2941. }
  2942. NewReleases.clear();
  2943. if (NewRetains.empty()) break;
  2944. }
  2945. // If the pointer is known incremented or nested, we can safely delete the
  2946. // pair regardless of what's between them.
  2947. if (KnownSafeTD || KnownSafeBU) {
  2948. RetainsToMove.ReverseInsertPts.clear();
  2949. ReleasesToMove.ReverseInsertPts.clear();
  2950. NewCount = 0;
  2951. } else {
  2952. // Determine whether the new insertion points we computed preserve the
  2953. // balance of retain and release calls through the program.
  2954. // TODO: If the fully aggressive solution isn't valid, try to find a
  2955. // less aggressive solution which is.
  2956. if (NewDelta != 0)
  2957. goto next_retain;
  2958. }
  2959. // Determine whether the original call points are balanced in the retain and
  2960. // release calls through the program. If not, conservatively don't touch
  2961. // them.
  2962. // TODO: It's theoretically possible to do code motion in this case, as
  2963. // long as the existing imbalances are maintained.
  2964. if (OldDelta != 0)
  2965. goto next_retain;
  2966. // Ok, everything checks out and we're all set. Let's move some code!
  2967. Changed = true;
  2968. assert(OldCount != 0 && "Unreachable code?");
  2969. AnyPairsCompletelyEliminated = NewCount == 0;
  2970. NumRRs += OldCount - NewCount;
  2971. MoveCalls(Arg, RetainsToMove, ReleasesToMove,
  2972. Retains, Releases, DeadInsts, M);
  2973. next_retain:
  2974. NewReleases.clear();
  2975. NewRetains.clear();
  2976. RetainsToMove.clear();
  2977. ReleasesToMove.clear();
  2978. }
  2979. // Now that we're done moving everything, we can delete the newly dead
  2980. // instructions, as we no longer need them as insert points.
  2981. while (!DeadInsts.empty())
  2982. EraseInstruction(DeadInsts.pop_back_val());
  2983. return AnyPairsCompletelyEliminated;
  2984. }
  2985. /// OptimizeWeakCalls - Weak pointer optimizations.
  2986. void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
  2987. // First, do memdep-style RLE and S2L optimizations. We can't use memdep
  2988. // itself because it uses AliasAnalysis and we need to do provenance
  2989. // queries instead.
  2990. for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
  2991. Instruction *Inst = &*I++;
  2992. InstructionClass Class = GetBasicInstructionClass(Inst);
  2993. if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained)
  2994. continue;
  2995. // Delete objc_loadWeak calls with no users.
  2996. if (Class == IC_LoadWeak && Inst->use_empty()) {
  2997. Inst->eraseFromParent();
  2998. continue;
  2999. }
  3000. // TODO: For now, just look for an earlier available version of this value
  3001. // within the same block. Theoretically, we could do memdep-style non-local
  3002. // analysis too, but that would want caching. A better approach would be to
  3003. // use the technique that EarlyCSE uses.
  3004. inst_iterator Current = llvm::prior(I);
  3005. BasicBlock *CurrentBB = Current.getBasicBlockIterator();
  3006. for (BasicBlock::iterator B = CurrentBB->begin(),
  3007. J = Current.getInstructionIterator();
  3008. J != B; --J) {
  3009. Instruction *EarlierInst = &*llvm::prior(J);
  3010. InstructionClass EarlierClass = GetInstructionClass(EarlierInst);
  3011. switch (EarlierClass) {
  3012. case IC_LoadWeak:
  3013. case IC_LoadWeakRetained: {
  3014. // If this is loading from the same pointer, replace this load's value
  3015. // with that one.
  3016. CallInst *Call = cast<CallInst>(Inst);
  3017. CallInst *EarlierCall = cast<CallInst>(EarlierInst);
  3018. Value *Arg = Call->getArgOperand(0);
  3019. Value *EarlierArg = EarlierCall->getArgOperand(0);
  3020. switch (PA.getAA()->alias(Arg, EarlierArg)) {
  3021. case AliasAnalysis::MustAlias:
  3022. Changed = true;
  3023. // If the load has a builtin retain, insert a plain retain for it.
  3024. if (Class == IC_LoadWeakRetained) {
  3025. CallInst *CI =
  3026. CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
  3027. "", Call);
  3028. CI->setTailCall();
  3029. }
  3030. // Zap the fully redundant load.
  3031. Call->replaceAllUsesWith(EarlierCall);
  3032. Call->eraseFromParent();
  3033. goto clobbered;
  3034. case AliasAnalysis::MayAlias:
  3035. case AliasAnalysis::PartialAlias:
  3036. goto clobbered;
  3037. case AliasAnalysis::NoAlias:
  3038. break;
  3039. }
  3040. break;
  3041. }
  3042. case IC_StoreWeak:
  3043. case IC_InitWeak: {
  3044. // If this is storing to the same pointer and has the same size etc.
  3045. // replace this load's value with the stored value.
  3046. CallInst *Call = cast<CallInst>(Inst);
  3047. CallInst *EarlierCall = cast<CallInst>(EarlierInst);
  3048. Value *Arg = Call->getArgOperand(0);
  3049. Value *EarlierArg = EarlierCall->getArgOperand(0);
  3050. switch (PA.getAA()->alias(Arg, EarlierArg)) {
  3051. case AliasAnalysis::MustAlias:
  3052. Changed = true;
  3053. // If the load has a builtin retain, insert a plain retain for it.
  3054. if (Class == IC_LoadWeakRetained) {
  3055. CallInst *CI =
  3056. CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
  3057. "", Call);
  3058. CI->setTailCall();
  3059. }
  3060. // Zap the fully redundant load.
  3061. Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
  3062. Call->eraseFromParent();
  3063. goto clobbered;
  3064. case AliasAnalysis::MayAlias:
  3065. case AliasAnalysis::PartialAlias:
  3066. goto clobbered;
  3067. case AliasAnalysis::NoAlias:
  3068. break;
  3069. }
  3070. break;
  3071. }
  3072. case IC_MoveWeak:
  3073. case IC_CopyWeak:
  3074. // TOOD: Grab the copied value.
  3075. goto clobbered;
  3076. case IC_AutoreleasepoolPush:
  3077. case IC_None:
  3078. case IC_User:
  3079. // Weak pointers are only modified through the weak entry points
  3080. // (and arbitrary calls, which could call the weak entry points).
  3081. break;
  3082. default:
  3083. // Anything else could modify the weak pointer.
  3084. goto clobbered;
  3085. }
  3086. }
  3087. clobbered:;
  3088. }
  3089. // Then, for each destroyWeak with an alloca operand, check to see if
  3090. // the alloca and all its users can be zapped.
  3091. for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
  3092. Instruction *Inst = &*I++;
  3093. InstructionClass Class = GetBasicInstructionClass(Inst);
  3094. if (Class != IC_DestroyWeak)
  3095. continue;
  3096. CallInst *Call = cast<CallInst>(Inst);
  3097. Value *Arg = Call->getArgOperand(0);
  3098. if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
  3099. for (Value::use_iterator UI = Alloca->use_begin(),
  3100. UE = Alloca->use_end(); UI != UE; ++UI) {
  3101. const Instruction *UserInst = cast<Instruction>(*UI);
  3102. switch (GetBasicInstructionClass(UserInst)) {
  3103. case IC_InitWeak:
  3104. case IC_StoreWeak:
  3105. case IC_DestroyWeak:
  3106. continue;
  3107. default:
  3108. goto done;
  3109. }
  3110. }
  3111. Changed = true;
  3112. for (Value::use_iterator UI = Alloca->use_begin(),
  3113. UE = Alloca->use_end(); UI != UE; ) {
  3114. CallInst *UserInst = cast<CallInst>(*UI++);
  3115. switch (GetBasicInstructionClass(UserInst)) {
  3116. case IC_InitWeak:
  3117. case IC_StoreWeak:
  3118. // These functions return their second argument.
  3119. UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
  3120. break;
  3121. case IC_DestroyWeak:
  3122. // No return value.
  3123. break;
  3124. default:
  3125. llvm_unreachable("alloca really is used!");
  3126. }
  3127. UserInst->eraseFromParent();
  3128. }
  3129. Alloca->eraseFromParent();
  3130. done:;
  3131. }
  3132. }
  3133. }
  3134. /// OptimizeSequences - Identify program paths which execute sequences of
  3135. /// retains and releases which can be eliminated.
  3136. bool ObjCARCOpt::OptimizeSequences(Function &F) {
  3137. /// Releases, Retains - These are used to store the results of the main flow
  3138. /// analysis. These use Value* as the key instead of Instruction* so that the
  3139. /// map stays valid when we get around to rewriting code and calls get
  3140. /// replaced by arguments.
  3141. DenseMap<Value *, RRInfo> Releases;
  3142. MapVector<Value *, RRInfo> Retains;
  3143. /// BBStates, This is used during the traversal of the function to track the
  3144. /// states for each identified object at each block.
  3145. DenseMap<const BasicBlock *, BBState> BBStates;
  3146. // Analyze the CFG of the function, and all instructions.
  3147. bool NestingDetected = Visit(F, BBStates, Retains, Releases);
  3148. // Transform.
  3149. return PerformCodePlacement(BBStates, Retains, Releases, F.getParent()) &&
  3150. NestingDetected;
  3151. }
  3152. /// OptimizeReturns - Look for this pattern:
  3153. ///
  3154. /// %call = call i8* @something(...)
  3155. /// %2 = call i8* @objc_retain(i8* %call)
  3156. /// %3 = call i8* @objc_autorelease(i8* %2)
  3157. /// ret i8* %3
  3158. ///
  3159. /// And delete the retain and autorelease.
  3160. ///
  3161. /// Otherwise if it's just this:
  3162. ///
  3163. /// %3 = call i8* @objc_autorelease(i8* %2)
  3164. /// ret i8* %3
  3165. ///
  3166. /// convert the autorelease to autoreleaseRV.
  3167. void ObjCARCOpt::OptimizeReturns(Function &F) {
  3168. if (!F.getReturnType()->isPointerTy())
  3169. return;
  3170. SmallPtrSet<Instruction *, 4> DependingInstructions;
  3171. SmallPtrSet<const BasicBlock *, 4> Visited;
  3172. for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
  3173. BasicBlock *BB = FI;
  3174. ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
  3175. if (!Ret) continue;
  3176. const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0));
  3177. FindDependencies(NeedsPositiveRetainCount, Arg,
  3178. BB, Ret, DependingInstructions, Visited, PA);
  3179. if (DependingInstructions.size() != 1)
  3180. goto next_block;
  3181. {
  3182. CallInst *Autorelease =
  3183. dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
  3184. if (!Autorelease)
  3185. goto next_block;
  3186. InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
  3187. if (!IsAutorelease(AutoreleaseClass))
  3188. goto next_block;
  3189. if (GetObjCArg(Autorelease) != Arg)
  3190. goto next_block;
  3191. DependingInstructions.clear();
  3192. Visited.clear();
  3193. // Check that there is nothing that can affect the reference
  3194. // count between the autorelease and the retain.
  3195. FindDependencies(CanChangeRetainCount, Arg,
  3196. BB, Autorelease, DependingInstructions, Visited, PA);
  3197. if (DependingInstructions.size() != 1)
  3198. goto next_block;
  3199. {
  3200. CallInst *Retain =
  3201. dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
  3202. // Check that we found a retain with the same argument.
  3203. if (!Retain ||
  3204. !IsRetain(GetBasicInstructionClass(Retain)) ||
  3205. GetObjCArg(Retain) != Arg)
  3206. goto next_block;
  3207. DependingInstructions.clear();
  3208. Visited.clear();
  3209. // Convert the autorelease to an autoreleaseRV, since it's
  3210. // returning the value.
  3211. if (AutoreleaseClass == IC_Autorelease) {
  3212. Autorelease->setCalledFunction(getAutoreleaseRVCallee(F.getParent()));
  3213. AutoreleaseClass = IC_AutoreleaseRV;
  3214. }
  3215. // Check that there is nothing that can affect the reference
  3216. // count between the retain and the call.
  3217. // Note that Retain need not be in BB.
  3218. FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
  3219. DependingInstructions, Visited, PA);
  3220. if (DependingInstructions.size() != 1)
  3221. goto next_block;
  3222. {
  3223. CallInst *Call =
  3224. dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
  3225. // Check that the pointer is the return value of the call.
  3226. if (!Call || Arg != Call)
  3227. goto next_block;
  3228. // Check that the call is a regular call.
  3229. InstructionClass Class = GetBasicInstructionClass(Call);
  3230. if (Class != IC_CallOrUser && Class != IC_Call)
  3231. goto next_block;
  3232. // If so, we can zap the retain and autorelease.
  3233. Changed = true;
  3234. ++NumRets;
  3235. EraseInstruction(Retain);
  3236. EraseInstruction(Autorelease);
  3237. }
  3238. }
  3239. }
  3240. next_block:
  3241. DependingInstructions.clear();
  3242. Visited.clear();
  3243. }
  3244. }
  3245. bool ObjCARCOpt::doInitialization(Module &M) {
  3246. if (!EnableARCOpts)
  3247. return false;
  3248. // If nothing in the Module uses ARC, don't do anything.
  3249. Run = ModuleHasARC(M);
  3250. if (!Run)
  3251. return false;
  3252. // Identify the imprecise release metadata kind.
  3253. ImpreciseReleaseMDKind =
  3254. M.getContext().getMDKindID("clang.imprecise_release");
  3255. CopyOnEscapeMDKind =
  3256. M.getContext().getMDKindID("clang.arc.copy_on_escape");
  3257. NoObjCARCExceptionsMDKind =
  3258. M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
  3259. // Intuitively, objc_retain and others are nocapture, however in practice
  3260. // they are not, because they return their argument value. And objc_release
  3261. // calls finalizers which can have arbitrary side effects.
  3262. // These are initialized lazily.
  3263. RetainRVCallee = 0;
  3264. AutoreleaseRVCallee = 0;
  3265. ReleaseCallee = 0;
  3266. RetainCallee = 0;
  3267. RetainBlockCallee = 0;
  3268. AutoreleaseCallee = 0;
  3269. return false;
  3270. }
  3271. bool ObjCARCOpt::runOnFunction(Function &F) {
  3272. if (!EnableARCOpts)
  3273. return false;
  3274. // If nothing in the Module uses ARC, don't do anything.
  3275. if (!Run)
  3276. return false;
  3277. Changed = false;
  3278. PA.setAA(&getAnalysis<AliasAnalysis>());
  3279. // This pass performs several distinct transformations. As a compile-time aid
  3280. // when compiling code that isn't ObjC, skip these if the relevant ObjC
  3281. // library functions aren't declared.
  3282. // Preliminary optimizations. This also computs UsedInThisFunction.
  3283. OptimizeIndividualCalls(F);
  3284. // Optimizations for weak pointers.
  3285. if (UsedInThisFunction & ((1 << IC_LoadWeak) |
  3286. (1 << IC_LoadWeakRetained) |
  3287. (1 << IC_StoreWeak) |
  3288. (1 << IC_InitWeak) |
  3289. (1 << IC_CopyWeak) |
  3290. (1 << IC_MoveWeak) |
  3291. (1 << IC_DestroyWeak)))
  3292. OptimizeWeakCalls(F);
  3293. // Optimizations for retain+release pairs.
  3294. if (UsedInThisFunction & ((1 << IC_Retain) |
  3295. (1 << IC_RetainRV) |
  3296. (1 << IC_RetainBlock)))
  3297. if (UsedInThisFunction & (1 << IC_Release))
  3298. // Run OptimizeSequences until it either stops making changes or
  3299. // no retain+release pair nesting is detected.
  3300. while (OptimizeSequences(F)) {}
  3301. // Optimizations if objc_autorelease is used.
  3302. if (UsedInThisFunction & ((1 << IC_Autorelease) |
  3303. (1 << IC_AutoreleaseRV)))
  3304. OptimizeReturns(F);
  3305. return Changed;
  3306. }
  3307. void ObjCARCOpt::releaseMemory() {
  3308. PA.clear();
  3309. }
  3310. //===----------------------------------------------------------------------===//
  3311. // ARC contraction.
  3312. //===----------------------------------------------------------------------===//
  3313. // TODO: ObjCARCContract could insert PHI nodes when uses aren't
  3314. // dominated by single calls.
  3315. #include "llvm/Operator.h"
  3316. #include "llvm/InlineAsm.h"
  3317. #include "llvm/Analysis/Dominators.h"
  3318. STATISTIC(NumStoreStrongs, "Number objc_storeStrong calls formed");
  3319. namespace {
  3320. /// ObjCARCContract - Late ARC optimizations. These change the IR in a way
  3321. /// that makes it difficult to be analyzed by ObjCARCOpt, so it's run late.
  3322. class ObjCARCContract : public FunctionPass {
  3323. bool Changed;
  3324. AliasAnalysis *AA;
  3325. DominatorTree *DT;
  3326. ProvenanceAnalysis PA;
  3327. /// Run - A flag indicating whether this optimization pass should run.
  3328. bool Run;
  3329. /// StoreStrongCallee, etc. - Declarations for ObjC runtime
  3330. /// functions, for use in creating calls to them. These are initialized
  3331. /// lazily to avoid cluttering up the Module with unused declarations.
  3332. Constant *StoreStrongCallee,
  3333. *RetainAutoreleaseCallee, *RetainAutoreleaseRVCallee;
  3334. /// RetainRVMarker - The inline asm string to insert between calls and
  3335. /// RetainRV calls to make the optimization work on targets which need it.
  3336. const MDString *RetainRVMarker;
  3337. /// StoreStrongCalls - The set of inserted objc_storeStrong calls. If
  3338. /// at the end of walking the function we have found no alloca
  3339. /// instructions, these calls can be marked "tail".
  3340. SmallPtrSet<CallInst *, 8> StoreStrongCalls;
  3341. Constant *getStoreStrongCallee(Module *M);
  3342. Constant *getRetainAutoreleaseCallee(Module *M);
  3343. Constant *getRetainAutoreleaseRVCallee(Module *M);
  3344. bool ContractAutorelease(Function &F, Instruction *Autorelease,
  3345. InstructionClass Class,
  3346. SmallPtrSet<Instruction *, 4>
  3347. &DependingInstructions,
  3348. SmallPtrSet<const BasicBlock *, 4>
  3349. &Visited);
  3350. void ContractRelease(Instruction *Release,
  3351. inst_iterator &Iter);
  3352. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  3353. virtual bool doInitialization(Module &M);
  3354. virtual bool runOnFunction(Function &F);
  3355. public:
  3356. static char ID;
  3357. ObjCARCContract() : FunctionPass(ID) {
  3358. initializeObjCARCContractPass(*PassRegistry::getPassRegistry());
  3359. }
  3360. };
  3361. }
  3362. char ObjCARCContract::ID = 0;
  3363. INITIALIZE_PASS_BEGIN(ObjCARCContract,
  3364. "objc-arc-contract", "ObjC ARC contraction", false, false)
  3365. INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
  3366. INITIALIZE_PASS_DEPENDENCY(DominatorTree)
  3367. INITIALIZE_PASS_END(ObjCARCContract,
  3368. "objc-arc-contract", "ObjC ARC contraction", false, false)
  3369. Pass *llvm::createObjCARCContractPass() {
  3370. return new ObjCARCContract();
  3371. }
  3372. void ObjCARCContract::getAnalysisUsage(AnalysisUsage &AU) const {
  3373. AU.addRequired<AliasAnalysis>();
  3374. AU.addRequired<DominatorTree>();
  3375. AU.setPreservesCFG();
  3376. }
  3377. Constant *ObjCARCContract::getStoreStrongCallee(Module *M) {
  3378. if (!StoreStrongCallee) {
  3379. LLVMContext &C = M->getContext();
  3380. Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
  3381. Type *I8XX = PointerType::getUnqual(I8X);
  3382. Type *Params[] = { I8XX, I8X };
  3383. AttrListPtr Attributes = AttrListPtr()
  3384. .addAttr(~0u, Attribute::NoUnwind)
  3385. .addAttr(1, Attribute::NoCapture);
  3386. StoreStrongCallee =
  3387. M->getOrInsertFunction(
  3388. "objc_storeStrong",
  3389. FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
  3390. Attributes);
  3391. }
  3392. return StoreStrongCallee;
  3393. }
  3394. Constant *ObjCARCContract::getRetainAutoreleaseCallee(Module *M) {
  3395. if (!RetainAutoreleaseCallee) {
  3396. LLVMContext &C = M->getContext();
  3397. Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
  3398. Type *Params[] = { I8X };
  3399. FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
  3400. AttrListPtr Attributes = AttrListPtr().addAttr(~0u, Attribute::NoUnwind);
  3401. RetainAutoreleaseCallee =
  3402. M->getOrInsertFunction("objc_retainAutorelease", FTy, Attributes);
  3403. }
  3404. return RetainAutoreleaseCallee;
  3405. }
  3406. Constant *ObjCARCContract::getRetainAutoreleaseRVCallee(Module *M) {
  3407. if (!RetainAutoreleaseRVCallee) {
  3408. LLVMContext &C = M->getContext();
  3409. Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
  3410. Type *Params[] = { I8X };
  3411. FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
  3412. AttrListPtr Attributes = AttrListPtr().addAttr(~0u, Attribute::NoUnwind);
  3413. RetainAutoreleaseRVCallee =
  3414. M->getOrInsertFunction("objc_retainAutoreleaseReturnValue", FTy,
  3415. Attributes);
  3416. }
  3417. return RetainAutoreleaseRVCallee;
  3418. }
  3419. /// ContractAutorelease - Merge an autorelease with a retain into a fused call.
  3420. bool
  3421. ObjCARCContract::ContractAutorelease(Function &F, Instruction *Autorelease,
  3422. InstructionClass Class,
  3423. SmallPtrSet<Instruction *, 4>
  3424. &DependingInstructions,
  3425. SmallPtrSet<const BasicBlock *, 4>
  3426. &Visited) {
  3427. const Value *Arg = GetObjCArg(Autorelease);
  3428. // Check that there are no instructions between the retain and the autorelease
  3429. // (such as an autorelease_pop) which may change the count.
  3430. CallInst *Retain = 0;
  3431. if (Class == IC_AutoreleaseRV)
  3432. FindDependencies(RetainAutoreleaseRVDep, Arg,
  3433. Autorelease->getParent(), Autorelease,
  3434. DependingInstructions, Visited, PA);
  3435. else
  3436. FindDependencies(RetainAutoreleaseDep, Arg,
  3437. Autorelease->getParent(), Autorelease,
  3438. DependingInstructions, Visited, PA);
  3439. Visited.clear();
  3440. if (DependingInstructions.size() != 1) {
  3441. DependingInstructions.clear();
  3442. return false;
  3443. }
  3444. Retain = dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
  3445. DependingInstructions.clear();
  3446. if (!Retain ||
  3447. GetBasicInstructionClass(Retain) != IC_Retain ||
  3448. GetObjCArg(Retain) != Arg)
  3449. return false;
  3450. Changed = true;
  3451. ++NumPeeps;
  3452. if (Class == IC_AutoreleaseRV)
  3453. Retain->setCalledFunction(getRetainAutoreleaseRVCallee(F.getParent()));
  3454. else
  3455. Retain->setCalledFunction(getRetainAutoreleaseCallee(F.getParent()));
  3456. EraseInstruction(Autorelease);
  3457. return true;
  3458. }
  3459. /// ContractRelease - Attempt to merge an objc_release with a store, load, and
  3460. /// objc_retain to form an objc_storeStrong. This can be a little tricky because
  3461. /// the instructions don't always appear in order, and there may be unrelated
  3462. /// intervening instructions.
  3463. void ObjCARCContract::ContractRelease(Instruction *Release,
  3464. inst_iterator &Iter) {
  3465. LoadInst *Load = dyn_cast<LoadInst>(GetObjCArg(Release));
  3466. if (!Load || !Load->isSimple()) return;
  3467. // For now, require everything to be in one basic block.
  3468. BasicBlock *BB = Release->getParent();
  3469. if (Load->getParent() != BB) return;
  3470. // Walk down to find the store and the release, which may be in either order.
  3471. BasicBlock::iterator I = Load, End = BB->end();
  3472. ++I;
  3473. AliasAnalysis::Location Loc = AA->getLocation(Load);
  3474. StoreInst *Store = 0;
  3475. bool SawRelease = false;
  3476. for (; !Store || !SawRelease; ++I) {
  3477. if (I == End)
  3478. return;
  3479. Instruction *Inst = I;
  3480. if (Inst == Release) {
  3481. SawRelease = true;
  3482. continue;
  3483. }
  3484. InstructionClass Class = GetBasicInstructionClass(Inst);
  3485. // Unrelated retains are harmless.
  3486. if (IsRetain(Class))
  3487. continue;
  3488. if (Store) {
  3489. // The store is the point where we're going to put the objc_storeStrong,
  3490. // so make sure there are no uses after it.
  3491. if (CanUse(Inst, Load, PA, Class))
  3492. return;
  3493. } else if (AA->getModRefInfo(Inst, Loc) & AliasAnalysis::Mod) {
  3494. // We are moving the load down to the store, so check for anything
  3495. // else which writes to the memory between the load and the store.
  3496. Store = dyn_cast<StoreInst>(Inst);
  3497. if (!Store || !Store->isSimple()) return;
  3498. if (Store->getPointerOperand() != Loc.Ptr) return;
  3499. }
  3500. }
  3501. Value *New = StripPointerCastsAndObjCCalls(Store->getValueOperand());
  3502. // Walk up to find the retain.
  3503. I = Store;
  3504. BasicBlock::iterator Begin = BB->begin();
  3505. while (I != Begin && GetBasicInstructionClass(I) != IC_Retain)
  3506. --I;
  3507. Instruction *Retain = I;
  3508. if (GetBasicInstructionClass(Retain) != IC_Retain) return;
  3509. if (GetObjCArg(Retain) != New) return;
  3510. Changed = true;
  3511. ++NumStoreStrongs;
  3512. LLVMContext &C = Release->getContext();
  3513. Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
  3514. Type *I8XX = PointerType::getUnqual(I8X);
  3515. Value *Args[] = { Load->getPointerOperand(), New };
  3516. if (Args[0]->getType() != I8XX)
  3517. Args[0] = new BitCastInst(Args[0], I8XX, "", Store);
  3518. if (Args[1]->getType() != I8X)
  3519. Args[1] = new BitCastInst(Args[1], I8X, "", Store);
  3520. CallInst *StoreStrong =
  3521. CallInst::Create(getStoreStrongCallee(BB->getParent()->getParent()),
  3522. Args, "", Store);
  3523. StoreStrong->setDoesNotThrow();
  3524. StoreStrong->setDebugLoc(Store->getDebugLoc());
  3525. // We can't set the tail flag yet, because we haven't yet determined
  3526. // whether there are any escaping allocas. Remember this call, so that
  3527. // we can set the tail flag once we know it's safe.
  3528. StoreStrongCalls.insert(StoreStrong);
  3529. if (&*Iter == Store) ++Iter;
  3530. Store->eraseFromParent();
  3531. Release->eraseFromParent();
  3532. EraseInstruction(Retain);
  3533. if (Load->use_empty())
  3534. Load->eraseFromParent();
  3535. }
  3536. bool ObjCARCContract::doInitialization(Module &M) {
  3537. // If nothing in the Module uses ARC, don't do anything.
  3538. Run = ModuleHasARC(M);
  3539. if (!Run)
  3540. return false;
  3541. // These are initialized lazily.
  3542. StoreStrongCallee = 0;
  3543. RetainAutoreleaseCallee = 0;
  3544. RetainAutoreleaseRVCallee = 0;
  3545. // Initialize RetainRVMarker.
  3546. RetainRVMarker = 0;
  3547. if (NamedMDNode *NMD =
  3548. M.getNamedMetadata("clang.arc.retainAutoreleasedReturnValueMarker"))
  3549. if (NMD->getNumOperands() == 1) {
  3550. const MDNode *N = NMD->getOperand(0);
  3551. if (N->getNumOperands() == 1)
  3552. if (const MDString *S = dyn_cast<MDString>(N->getOperand(0)))
  3553. RetainRVMarker = S;
  3554. }
  3555. return false;
  3556. }
  3557. bool ObjCARCContract::runOnFunction(Function &F) {
  3558. if (!EnableARCOpts)
  3559. return false;
  3560. // If nothing in the Module uses ARC, don't do anything.
  3561. if (!Run)
  3562. return false;
  3563. Changed = false;
  3564. AA = &getAnalysis<AliasAnalysis>();
  3565. DT = &getAnalysis<DominatorTree>();
  3566. PA.setAA(&getAnalysis<AliasAnalysis>());
  3567. // Track whether it's ok to mark objc_storeStrong calls with the "tail"
  3568. // keyword. Be conservative if the function has variadic arguments.
  3569. // It seems that functions which "return twice" are also unsafe for the
  3570. // "tail" argument, because they are setjmp, which could need to
  3571. // return to an earlier stack state.
  3572. bool TailOkForStoreStrongs = !F.isVarArg() &&
  3573. !F.callsFunctionThatReturnsTwice();
  3574. // For ObjC library calls which return their argument, replace uses of the
  3575. // argument with uses of the call return value, if it dominates the use. This
  3576. // reduces register pressure.
  3577. SmallPtrSet<Instruction *, 4> DependingInstructions;
  3578. SmallPtrSet<const BasicBlock *, 4> Visited;
  3579. for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
  3580. Instruction *Inst = &*I++;
  3581. // Only these library routines return their argument. In particular,
  3582. // objc_retainBlock does not necessarily return its argument.
  3583. InstructionClass Class = GetBasicInstructionClass(Inst);
  3584. switch (Class) {
  3585. case IC_Retain:
  3586. case IC_FusedRetainAutorelease:
  3587. case IC_FusedRetainAutoreleaseRV:
  3588. break;
  3589. case IC_Autorelease:
  3590. case IC_AutoreleaseRV:
  3591. if (ContractAutorelease(F, Inst, Class, DependingInstructions, Visited))
  3592. continue;
  3593. break;
  3594. case IC_RetainRV: {
  3595. // If we're compiling for a target which needs a special inline-asm
  3596. // marker to do the retainAutoreleasedReturnValue optimization,
  3597. // insert it now.
  3598. if (!RetainRVMarker)
  3599. break;
  3600. BasicBlock::iterator BBI = Inst;
  3601. BasicBlock *InstParent = Inst->getParent();
  3602. // Step up to see if the call immediately precedes the RetainRV call.
  3603. // If it's an invoke, we have to cross a block boundary. And we have
  3604. // to carefully dodge no-op instructions.
  3605. do {
  3606. if (&*BBI == InstParent->begin()) {
  3607. BasicBlock *Pred = InstParent->getSinglePredecessor();
  3608. if (!Pred)
  3609. goto decline_rv_optimization;
  3610. BBI = Pred->getTerminator();
  3611. break;
  3612. }
  3613. --BBI;
  3614. } while (isNoopInstruction(BBI));
  3615. if (&*BBI == GetObjCArg(Inst)) {
  3616. Changed = true;
  3617. InlineAsm *IA =
  3618. InlineAsm::get(FunctionType::get(Type::getVoidTy(Inst->getContext()),
  3619. /*isVarArg=*/false),
  3620. RetainRVMarker->getString(),
  3621. /*Constraints=*/"", /*hasSideEffects=*/true);
  3622. CallInst::Create(IA, "", Inst);
  3623. }
  3624. decline_rv_optimization:
  3625. break;
  3626. }
  3627. case IC_InitWeak: {
  3628. // objc_initWeak(p, null) => *p = null
  3629. CallInst *CI = cast<CallInst>(Inst);
  3630. if (isNullOrUndef(CI->getArgOperand(1))) {
  3631. Value *Null =
  3632. ConstantPointerNull::get(cast<PointerType>(CI->getType()));
  3633. Changed = true;
  3634. new StoreInst(Null, CI->getArgOperand(0), CI);
  3635. CI->replaceAllUsesWith(Null);
  3636. CI->eraseFromParent();
  3637. }
  3638. continue;
  3639. }
  3640. case IC_Release:
  3641. ContractRelease(Inst, I);
  3642. continue;
  3643. case IC_User:
  3644. // Be conservative if the function has any alloca instructions.
  3645. // Technically we only care about escaping alloca instructions,
  3646. // but this is sufficient to handle some interesting cases.
  3647. if (isa<AllocaInst>(Inst))
  3648. TailOkForStoreStrongs = false;
  3649. continue;
  3650. default:
  3651. continue;
  3652. }
  3653. // Don't use GetObjCArg because we don't want to look through bitcasts
  3654. // and such; to do the replacement, the argument must have type i8*.
  3655. const Value *Arg = cast<CallInst>(Inst)->getArgOperand(0);
  3656. for (;;) {
  3657. // If we're compiling bugpointed code, don't get in trouble.
  3658. if (!isa<Instruction>(Arg) && !isa<Argument>(Arg))
  3659. break;
  3660. // Look through the uses of the pointer.
  3661. for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
  3662. UI != UE; ) {
  3663. Use &U = UI.getUse();
  3664. unsigned OperandNo = UI.getOperandNo();
  3665. ++UI; // Increment UI now, because we may unlink its element.
  3666. // If the call's return value dominates a use of the call's argument
  3667. // value, rewrite the use to use the return value. We check for
  3668. // reachability here because an unreachable call is considered to
  3669. // trivially dominate itself, which would lead us to rewriting its
  3670. // argument in terms of its return value, which would lead to
  3671. // infinite loops in GetObjCArg.
  3672. if (DT->isReachableFromEntry(U) && DT->dominates(Inst, U)) {
  3673. Changed = true;
  3674. Instruction *Replacement = Inst;
  3675. Type *UseTy = U.get()->getType();
  3676. if (PHINode *PHI = dyn_cast<PHINode>(U.getUser())) {
  3677. // For PHI nodes, insert the bitcast in the predecessor block.
  3678. unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo);
  3679. BasicBlock *BB = PHI->getIncomingBlock(ValNo);
  3680. if (Replacement->getType() != UseTy)
  3681. Replacement = new BitCastInst(Replacement, UseTy, "",
  3682. &BB->back());
  3683. // While we're here, rewrite all edges for this PHI, rather
  3684. // than just one use at a time, to minimize the number of
  3685. // bitcasts we emit.
  3686. for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
  3687. if (PHI->getIncomingBlock(i) == BB) {
  3688. // Keep the UI iterator valid.
  3689. if (&PHI->getOperandUse(
  3690. PHINode::getOperandNumForIncomingValue(i)) ==
  3691. &UI.getUse())
  3692. ++UI;
  3693. PHI->setIncomingValue(i, Replacement);
  3694. }
  3695. } else {
  3696. if (Replacement->getType() != UseTy)
  3697. Replacement = new BitCastInst(Replacement, UseTy, "",
  3698. cast<Instruction>(U.getUser()));
  3699. U.set(Replacement);
  3700. }
  3701. }
  3702. }
  3703. // If Arg is a no-op casted pointer, strip one level of casts and iterate.
  3704. if (const BitCastInst *BI = dyn_cast<BitCastInst>(Arg))
  3705. Arg = BI->getOperand(0);
  3706. else if (isa<GEPOperator>(Arg) &&
  3707. cast<GEPOperator>(Arg)->hasAllZeroIndices())
  3708. Arg = cast<GEPOperator>(Arg)->getPointerOperand();
  3709. else if (isa<GlobalAlias>(Arg) &&
  3710. !cast<GlobalAlias>(Arg)->mayBeOverridden())
  3711. Arg = cast<GlobalAlias>(Arg)->getAliasee();
  3712. else
  3713. break;
  3714. }
  3715. }
  3716. // If this function has no escaping allocas or suspicious vararg usage,
  3717. // objc_storeStrong calls can be marked with the "tail" keyword.
  3718. if (TailOkForStoreStrongs)
  3719. for (SmallPtrSet<CallInst *, 8>::iterator I = StoreStrongCalls.begin(),
  3720. E = StoreStrongCalls.end(); I != E; ++I)
  3721. (*I)->setTailCall();
  3722. StoreStrongCalls.clear();
  3723. return Changed;
  3724. }