DependencyAnalysis.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //===- DependencyAnalysis.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. /// \file
  10. ///
  11. /// This file defines special dependency analysis routines used in Objective C
  12. /// ARC Optimizations.
  13. ///
  14. /// WARNING: This file knows about certain library functions. It recognizes them
  15. /// by name, and hardwires knowledge of their semantics.
  16. ///
  17. /// WARNING: This file knows about how certain Objective-C library functions are
  18. /// used. Naive LLVM IR transformations which would otherwise be
  19. /// behavior-preserving may break these assumptions.
  20. ///
  21. //===----------------------------------------------------------------------===//
  22. #include "ObjCARC.h"
  23. #include "DependencyAnalysis.h"
  24. #include "ProvenanceAnalysis.h"
  25. #include "llvm/IR/CFG.h"
  26. using namespace llvm;
  27. using namespace llvm::objcarc;
  28. #define DEBUG_TYPE "objc-arc-dependency"
  29. /// Test whether the given instruction can result in a reference count
  30. /// modification (positive or negative) for the pointer's object.
  31. bool llvm::objcarc::CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
  32. ProvenanceAnalysis &PA,
  33. ARCInstKind Class) {
  34. switch (Class) {
  35. case ARCInstKind::Autorelease:
  36. case ARCInstKind::AutoreleaseRV:
  37. case ARCInstKind::IntrinsicUser:
  38. case ARCInstKind::User:
  39. // These operations never directly modify a reference count.
  40. return false;
  41. default: break;
  42. }
  43. ImmutableCallSite CS(Inst);
  44. assert(CS && "Only calls can alter reference counts!");
  45. // See if AliasAnalysis can help us with the call.
  46. AliasAnalysis::ModRefBehavior MRB = PA.getAA()->getModRefBehavior(CS);
  47. if (AliasAnalysis::onlyReadsMemory(MRB))
  48. return false;
  49. if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
  50. const DataLayout &DL = Inst->getModule()->getDataLayout();
  51. for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
  52. I != E; ++I) {
  53. const Value *Op = *I;
  54. if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
  55. PA.related(Ptr, Op, DL))
  56. return true;
  57. }
  58. return false;
  59. }
  60. // Assume the worst.
  61. return true;
  62. }
  63. bool llvm::objcarc::CanDecrementRefCount(const Instruction *Inst,
  64. const Value *Ptr,
  65. ProvenanceAnalysis &PA,
  66. ARCInstKind Class) {
  67. // First perform a quick check if Class can not touch ref counts.
  68. if (!CanDecrementRefCount(Class))
  69. return false;
  70. // Otherwise, just use CanAlterRefCount for now.
  71. return CanAlterRefCount(Inst, Ptr, PA, Class);
  72. }
  73. /// Test whether the given instruction can "use" the given pointer's object in a
  74. /// way that requires the reference count to be positive.
  75. bool llvm::objcarc::CanUse(const Instruction *Inst, const Value *Ptr,
  76. ProvenanceAnalysis &PA, ARCInstKind Class) {
  77. // ARCInstKind::Call operations (as opposed to
  78. // ARCInstKind::CallOrUser) never "use" objc pointers.
  79. if (Class == ARCInstKind::Call)
  80. return false;
  81. const DataLayout &DL = Inst->getModule()->getDataLayout();
  82. // Consider various instructions which may have pointer arguments which are
  83. // not "uses".
  84. if (const ICmpInst *ICI = dyn_cast<ICmpInst>(Inst)) {
  85. // Comparing a pointer with null, or any other constant, isn't really a use,
  86. // because we don't care what the pointer points to, or about the values
  87. // of any other dynamic reference-counted pointers.
  88. if (!IsPotentialRetainableObjPtr(ICI->getOperand(1), *PA.getAA()))
  89. return false;
  90. } else if (auto CS = ImmutableCallSite(Inst)) {
  91. // For calls, just check the arguments (and not the callee operand).
  92. for (ImmutableCallSite::arg_iterator OI = CS.arg_begin(),
  93. OE = CS.arg_end(); OI != OE; ++OI) {
  94. const Value *Op = *OI;
  95. if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
  96. PA.related(Ptr, Op, DL))
  97. return true;
  98. }
  99. return false;
  100. } else if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
  101. // Special-case stores, because we don't care about the stored value, just
  102. // the store address.
  103. const Value *Op = GetUnderlyingObjCPtr(SI->getPointerOperand(), DL);
  104. // If we can't tell what the underlying object was, assume there is a
  105. // dependence.
  106. return IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
  107. PA.related(Op, Ptr, DL);
  108. }
  109. // Check each operand for a match.
  110. for (User::const_op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
  111. OI != OE; ++OI) {
  112. const Value *Op = *OI;
  113. if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op, DL))
  114. return true;
  115. }
  116. return false;
  117. }
  118. /// Test if there can be dependencies on Inst through Arg. This function only
  119. /// tests dependencies relevant for removing pairs of calls.
  120. bool
  121. llvm::objcarc::Depends(DependenceKind Flavor, Instruction *Inst,
  122. const Value *Arg, ProvenanceAnalysis &PA) {
  123. // If we've reached the definition of Arg, stop.
  124. if (Inst == Arg)
  125. return true;
  126. switch (Flavor) {
  127. case NeedsPositiveRetainCount: {
  128. ARCInstKind Class = GetARCInstKind(Inst);
  129. switch (Class) {
  130. case ARCInstKind::AutoreleasepoolPop:
  131. case ARCInstKind::AutoreleasepoolPush:
  132. case ARCInstKind::None:
  133. return false;
  134. default:
  135. return CanUse(Inst, Arg, PA, Class);
  136. }
  137. }
  138. case AutoreleasePoolBoundary: {
  139. ARCInstKind Class = GetARCInstKind(Inst);
  140. switch (Class) {
  141. case ARCInstKind::AutoreleasepoolPop:
  142. case ARCInstKind::AutoreleasepoolPush:
  143. // These mark the end and begin of an autorelease pool scope.
  144. return true;
  145. default:
  146. // Nothing else does this.
  147. return false;
  148. }
  149. }
  150. case CanChangeRetainCount: {
  151. ARCInstKind Class = GetARCInstKind(Inst);
  152. switch (Class) {
  153. case ARCInstKind::AutoreleasepoolPop:
  154. // Conservatively assume this can decrement any count.
  155. return true;
  156. case ARCInstKind::AutoreleasepoolPush:
  157. case ARCInstKind::None:
  158. return false;
  159. default:
  160. return CanAlterRefCount(Inst, Arg, PA, Class);
  161. }
  162. }
  163. case RetainAutoreleaseDep:
  164. switch (GetBasicARCInstKind(Inst)) {
  165. case ARCInstKind::AutoreleasepoolPop:
  166. case ARCInstKind::AutoreleasepoolPush:
  167. // Don't merge an objc_autorelease with an objc_retain inside a different
  168. // autoreleasepool scope.
  169. return true;
  170. case ARCInstKind::Retain:
  171. case ARCInstKind::RetainRV:
  172. // Check for a retain of the same pointer for merging.
  173. return GetArgRCIdentityRoot(Inst) == Arg;
  174. default:
  175. // Nothing else matters for objc_retainAutorelease formation.
  176. return false;
  177. }
  178. case RetainAutoreleaseRVDep: {
  179. ARCInstKind Class = GetBasicARCInstKind(Inst);
  180. switch (Class) {
  181. case ARCInstKind::Retain:
  182. case ARCInstKind::RetainRV:
  183. // Check for a retain of the same pointer for merging.
  184. return GetArgRCIdentityRoot(Inst) == Arg;
  185. default:
  186. // Anything that can autorelease interrupts
  187. // retainAutoreleaseReturnValue formation.
  188. return CanInterruptRV(Class);
  189. }
  190. }
  191. case RetainRVDep:
  192. return CanInterruptRV(GetBasicARCInstKind(Inst));
  193. }
  194. llvm_unreachable("Invalid dependence flavor");
  195. }
  196. /// Walk up the CFG from StartPos (which is in StartBB) and find local and
  197. /// non-local dependencies on Arg.
  198. ///
  199. /// TODO: Cache results?
  200. void
  201. llvm::objcarc::FindDependencies(DependenceKind Flavor,
  202. const Value *Arg,
  203. BasicBlock *StartBB, Instruction *StartInst,
  204. SmallPtrSetImpl<Instruction *> &DependingInsts,
  205. SmallPtrSetImpl<const BasicBlock *> &Visited,
  206. ProvenanceAnalysis &PA) {
  207. BasicBlock::iterator StartPos = StartInst;
  208. SmallVector<std::pair<BasicBlock *, BasicBlock::iterator>, 4> Worklist;
  209. Worklist.push_back(std::make_pair(StartBB, StartPos));
  210. do {
  211. std::pair<BasicBlock *, BasicBlock::iterator> Pair =
  212. Worklist.pop_back_val();
  213. BasicBlock *LocalStartBB = Pair.first;
  214. BasicBlock::iterator LocalStartPos = Pair.second;
  215. BasicBlock::iterator StartBBBegin = LocalStartBB->begin();
  216. for (;;) {
  217. if (LocalStartPos == StartBBBegin) {
  218. pred_iterator PI(LocalStartBB), PE(LocalStartBB, false);
  219. if (PI == PE)
  220. // If we've reached the function entry, produce a null dependence.
  221. DependingInsts.insert(nullptr);
  222. else
  223. // Add the predecessors to the worklist.
  224. do {
  225. BasicBlock *PredBB = *PI;
  226. if (Visited.insert(PredBB).second)
  227. Worklist.push_back(std::make_pair(PredBB, PredBB->end()));
  228. } while (++PI != PE);
  229. break;
  230. }
  231. Instruction *Inst = --LocalStartPos;
  232. if (Depends(Flavor, Inst, Arg, PA)) {
  233. DependingInsts.insert(Inst);
  234. break;
  235. }
  236. }
  237. } while (!Worklist.empty());
  238. // Determine whether the original StartBB post-dominates all of the blocks we
  239. // visited. If not, insert a sentinal indicating that most optimizations are
  240. // not safe.
  241. for (const BasicBlock *BB : Visited) {
  242. if (BB == StartBB)
  243. continue;
  244. const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
  245. for (succ_const_iterator SI(TI), SE(TI, false); SI != SE; ++SI) {
  246. const BasicBlock *Succ = *SI;
  247. if (Succ != StartBB && !Visited.count(Succ)) {
  248. DependingInsts.insert(reinterpret_cast<Instruction *>(-1));
  249. return;
  250. }
  251. }
  252. }
  253. }