/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

Large files are truncated click here to view the full file

  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()