ObjCARCOpts.cpp 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  1. //===- ObjCARCOpts.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. /// This file defines ObjC ARC optimizations. ARC stands for Automatic
  11. /// Reference Counting and is a system for managing reference counts for objects
  12. /// 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, and numerous minor simplifications.
  17. ///
  18. /// WARNING: This file knows about certain library functions. It recognizes them
  19. /// by name, and hardwires knowledge of their semantics.
  20. ///
  21. /// WARNING: This file knows about how certain Objective-C library functions are
  22. /// used. Naive LLVM IR transformations which would otherwise be
  23. /// behavior-preserving may break these assumptions.
  24. ///
  25. //===----------------------------------------------------------------------===//
  26. #include "ObjCARC.h"
  27. #include "ARCRuntimeEntryPoints.h"
  28. #include "BlotMapVector.h"
  29. #include "DependencyAnalysis.h"
  30. #include "ObjCARCAliasAnalysis.h"
  31. #include "ProvenanceAnalysis.h"
  32. #include "PtrState.h"
  33. #include "llvm/ADT/DenseMap.h"
  34. #include "llvm/ADT/DenseSet.h"
  35. #include "llvm/ADT/STLExtras.h"
  36. #include "llvm/ADT/SmallPtrSet.h"
  37. #include "llvm/ADT/Statistic.h"
  38. #include "llvm/IR/CFG.h"
  39. #include "llvm/IR/IRBuilder.h"
  40. #include "llvm/IR/LLVMContext.h"
  41. #include "llvm/Support/Debug.h"
  42. #include "llvm/Support/raw_ostream.h"
  43. using namespace llvm;
  44. using namespace llvm::objcarc;
  45. #define DEBUG_TYPE "objc-arc-opts"
  46. /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
  47. /// @{
  48. /// \brief This is similar to GetRCIdentityRoot but it stops as soon
  49. /// as it finds a value with multiple uses.
  50. static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
  51. if (Arg->hasOneUse()) {
  52. if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
  53. return FindSingleUseIdentifiedObject(BC->getOperand(0));
  54. if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
  55. if (GEP->hasAllZeroIndices())
  56. return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
  57. if (IsForwarding(GetBasicARCInstKind(Arg)))
  58. return FindSingleUseIdentifiedObject(
  59. cast<CallInst>(Arg)->getArgOperand(0));
  60. if (!IsObjCIdentifiedObject(Arg))
  61. return nullptr;
  62. return Arg;
  63. }
  64. // If we found an identifiable object but it has multiple uses, but they are
  65. // trivial uses, we can still consider this to be a single-use value.
  66. if (IsObjCIdentifiedObject(Arg)) {
  67. for (const User *U : Arg->users())
  68. if (!U->use_empty() || GetRCIdentityRoot(U) != Arg)
  69. return nullptr;
  70. return Arg;
  71. }
  72. return nullptr;
  73. }
  74. /// This is a wrapper around getUnderlyingObjCPtr along the lines of
  75. /// GetUnderlyingObjects except that it returns early when it sees the first
  76. /// alloca.
  77. static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V,
  78. const DataLayout &DL) {
  79. SmallPtrSet<const Value *, 4> Visited;
  80. SmallVector<const Value *, 4> Worklist;
  81. Worklist.push_back(V);
  82. do {
  83. const Value *P = Worklist.pop_back_val();
  84. P = GetUnderlyingObjCPtr(P, DL);
  85. if (isa<AllocaInst>(P))
  86. return true;
  87. if (!Visited.insert(P).second)
  88. continue;
  89. if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) {
  90. Worklist.push_back(SI->getTrueValue());
  91. Worklist.push_back(SI->getFalseValue());
  92. continue;
  93. }
  94. if (const PHINode *PN = dyn_cast<const PHINode>(P)) {
  95. for (Value *IncValue : PN->incoming_values())
  96. Worklist.push_back(IncValue);
  97. continue;
  98. }
  99. } while (!Worklist.empty());
  100. return false;
  101. }
  102. /// @}
  103. ///
  104. /// \defgroup ARCOpt ARC Optimization.
  105. /// @{
  106. // TODO: On code like this:
  107. //
  108. // objc_retain(%x)
  109. // stuff_that_cannot_release()
  110. // objc_autorelease(%x)
  111. // stuff_that_cannot_release()
  112. // objc_retain(%x)
  113. // stuff_that_cannot_release()
  114. // objc_autorelease(%x)
  115. //
  116. // The second retain and autorelease can be deleted.
  117. // TODO: It should be possible to delete
  118. // objc_autoreleasePoolPush and objc_autoreleasePoolPop
  119. // pairs if nothing is actually autoreleased between them. Also, autorelease
  120. // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
  121. // after inlining) can be turned into plain release calls.
  122. // TODO: Critical-edge splitting. If the optimial insertion point is
  123. // a critical edge, the current algorithm has to fail, because it doesn't
  124. // know how to split edges. It should be possible to make the optimizer
  125. // think in terms of edges, rather than blocks, and then split critical
  126. // edges on demand.
  127. // TODO: OptimizeSequences could generalized to be Interprocedural.
  128. // TODO: Recognize that a bunch of other objc runtime calls have
  129. // non-escaping arguments and non-releasing arguments, and may be
  130. // non-autoreleasing.
  131. // TODO: Sink autorelease calls as far as possible. Unfortunately we
  132. // usually can't sink them past other calls, which would be the main
  133. // case where it would be useful.
  134. // TODO: The pointer returned from objc_loadWeakRetained is retained.
  135. // TODO: Delete release+retain pairs (rare).
  136. STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
  137. STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
  138. STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
  139. STATISTIC(NumRets, "Number of return value forwarding "
  140. "retain+autoreleases eliminated");
  141. STATISTIC(NumRRs, "Number of retain+release paths eliminated");
  142. STATISTIC(NumPeeps, "Number of calls peephole-optimized");
  143. #ifndef NDEBUG
  144. STATISTIC(NumRetainsBeforeOpt,
  145. "Number of retains before optimization");
  146. STATISTIC(NumReleasesBeforeOpt,
  147. "Number of releases before optimization");
  148. STATISTIC(NumRetainsAfterOpt,
  149. "Number of retains after optimization");
  150. STATISTIC(NumReleasesAfterOpt,
  151. "Number of releases after optimization");
  152. #endif
  153. namespace {
  154. /// \brief Per-BasicBlock state.
  155. class BBState {
  156. /// The number of unique control paths from the entry which can reach this
  157. /// block.
  158. unsigned TopDownPathCount;
  159. /// The number of unique control paths to exits from this block.
  160. unsigned BottomUpPathCount;
  161. /// The top-down traversal uses this to record information known about a
  162. /// pointer at the bottom of each block.
  163. BlotMapVector<const Value *, TopDownPtrState> PerPtrTopDown;
  164. /// The bottom-up traversal uses this to record information known about a
  165. /// pointer at the top of each block.
  166. BlotMapVector<const Value *, BottomUpPtrState> PerPtrBottomUp;
  167. /// Effective predecessors of the current block ignoring ignorable edges and
  168. /// ignored backedges.
  169. SmallVector<BasicBlock *, 2> Preds;
  170. /// Effective successors of the current block ignoring ignorable edges and
  171. /// ignored backedges.
  172. SmallVector<BasicBlock *, 2> Succs;
  173. public:
  174. static const unsigned OverflowOccurredValue;
  175. BBState() : TopDownPathCount(0), BottomUpPathCount(0) { }
  176. typedef decltype(PerPtrTopDown)::iterator top_down_ptr_iterator;
  177. typedef decltype(PerPtrTopDown)::const_iterator const_top_down_ptr_iterator;
  178. top_down_ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
  179. top_down_ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
  180. const_top_down_ptr_iterator top_down_ptr_begin() const {
  181. return PerPtrTopDown.begin();
  182. }
  183. const_top_down_ptr_iterator top_down_ptr_end() const {
  184. return PerPtrTopDown.end();
  185. }
  186. bool hasTopDownPtrs() const {
  187. return !PerPtrTopDown.empty();
  188. }
  189. typedef decltype(PerPtrBottomUp)::iterator bottom_up_ptr_iterator;
  190. typedef decltype(
  191. PerPtrBottomUp)::const_iterator const_bottom_up_ptr_iterator;
  192. bottom_up_ptr_iterator bottom_up_ptr_begin() {
  193. return PerPtrBottomUp.begin();
  194. }
  195. bottom_up_ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
  196. const_bottom_up_ptr_iterator bottom_up_ptr_begin() const {
  197. return PerPtrBottomUp.begin();
  198. }
  199. const_bottom_up_ptr_iterator bottom_up_ptr_end() const {
  200. return PerPtrBottomUp.end();
  201. }
  202. bool hasBottomUpPtrs() const {
  203. return !PerPtrBottomUp.empty();
  204. }
  205. /// Mark this block as being an entry block, which has one path from the
  206. /// entry by definition.
  207. void SetAsEntry() { TopDownPathCount = 1; }
  208. /// Mark this block as being an exit block, which has one path to an exit by
  209. /// definition.
  210. void SetAsExit() { BottomUpPathCount = 1; }
  211. /// Attempt to find the PtrState object describing the top down state for
  212. /// pointer Arg. Return a new initialized PtrState describing the top down
  213. /// state for Arg if we do not find one.
  214. TopDownPtrState &getPtrTopDownState(const Value *Arg) {
  215. return PerPtrTopDown[Arg];
  216. }
  217. /// Attempt to find the PtrState object describing the bottom up state for
  218. /// pointer Arg. Return a new initialized PtrState describing the bottom up
  219. /// state for Arg if we do not find one.
  220. BottomUpPtrState &getPtrBottomUpState(const Value *Arg) {
  221. return PerPtrBottomUp[Arg];
  222. }
  223. /// Attempt to find the PtrState object describing the bottom up state for
  224. /// pointer Arg.
  225. bottom_up_ptr_iterator findPtrBottomUpState(const Value *Arg) {
  226. return PerPtrBottomUp.find(Arg);
  227. }
  228. void clearBottomUpPointers() {
  229. PerPtrBottomUp.clear();
  230. }
  231. void clearTopDownPointers() {
  232. PerPtrTopDown.clear();
  233. }
  234. void InitFromPred(const BBState &Other);
  235. void InitFromSucc(const BBState &Other);
  236. void MergePred(const BBState &Other);
  237. void MergeSucc(const BBState &Other);
  238. /// Compute the number of possible unique paths from an entry to an exit
  239. /// which pass through this block. This is only valid after both the
  240. /// top-down and bottom-up traversals are complete.
  241. ///
  242. /// Returns true if overflow occurred. Returns false if overflow did not
  243. /// occur.
  244. bool GetAllPathCountWithOverflow(unsigned &PathCount) const {
  245. if (TopDownPathCount == OverflowOccurredValue ||
  246. BottomUpPathCount == OverflowOccurredValue)
  247. return true;
  248. unsigned long long Product =
  249. (unsigned long long)TopDownPathCount*BottomUpPathCount;
  250. // Overflow occurred if any of the upper bits of Product are set or if all
  251. // the lower bits of Product are all set.
  252. return (Product >> 32) ||
  253. ((PathCount = Product) == OverflowOccurredValue);
  254. }
  255. // Specialized CFG utilities.
  256. typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
  257. edge_iterator pred_begin() const { return Preds.begin(); }
  258. edge_iterator pred_end() const { return Preds.end(); }
  259. edge_iterator succ_begin() const { return Succs.begin(); }
  260. edge_iterator succ_end() const { return Succs.end(); }
  261. void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
  262. void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
  263. bool isExit() const { return Succs.empty(); }
  264. };
  265. const unsigned BBState::OverflowOccurredValue = 0xffffffff;
  266. }
  267. namespace llvm {
  268. raw_ostream &operator<<(raw_ostream &OS,
  269. BBState &BBState) LLVM_ATTRIBUTE_UNUSED;
  270. }
  271. void BBState::InitFromPred(const BBState &Other) {
  272. PerPtrTopDown = Other.PerPtrTopDown;
  273. TopDownPathCount = Other.TopDownPathCount;
  274. }
  275. void BBState::InitFromSucc(const BBState &Other) {
  276. PerPtrBottomUp = Other.PerPtrBottomUp;
  277. BottomUpPathCount = Other.BottomUpPathCount;
  278. }
  279. /// The top-down traversal uses this to merge information about predecessors to
  280. /// form the initial state for a new block.
  281. void BBState::MergePred(const BBState &Other) {
  282. if (TopDownPathCount == OverflowOccurredValue)
  283. return;
  284. // Other.TopDownPathCount can be 0, in which case it is either dead or a
  285. // loop backedge. Loop backedges are special.
  286. TopDownPathCount += Other.TopDownPathCount;
  287. // In order to be consistent, we clear the top down pointers when by adding
  288. // TopDownPathCount becomes OverflowOccurredValue even though "true" overflow
  289. // has not occurred.
  290. if (TopDownPathCount == OverflowOccurredValue) {
  291. clearTopDownPointers();
  292. return;
  293. }
  294. // Check for overflow. If we have overflow, fall back to conservative
  295. // behavior.
  296. if (TopDownPathCount < Other.TopDownPathCount) {
  297. TopDownPathCount = OverflowOccurredValue;
  298. clearTopDownPointers();
  299. return;
  300. }
  301. // For each entry in the other set, if our set has an entry with the same key,
  302. // merge the entries. Otherwise, copy the entry and merge it with an empty
  303. // entry.
  304. for (auto MI = Other.top_down_ptr_begin(), ME = Other.top_down_ptr_end();
  305. MI != ME; ++MI) {
  306. auto Pair = PerPtrTopDown.insert(*MI);
  307. Pair.first->second.Merge(Pair.second ? TopDownPtrState() : MI->second,
  308. /*TopDown=*/true);
  309. }
  310. // For each entry in our set, if the other set doesn't have an entry with the
  311. // same key, force it to merge with an empty entry.
  312. for (auto MI = top_down_ptr_begin(), ME = top_down_ptr_end(); MI != ME; ++MI)
  313. if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
  314. MI->second.Merge(TopDownPtrState(), /*TopDown=*/true);
  315. }
  316. /// The bottom-up traversal uses this to merge information about successors to
  317. /// form the initial state for a new block.
  318. void BBState::MergeSucc(const BBState &Other) {
  319. if (BottomUpPathCount == OverflowOccurredValue)
  320. return;
  321. // Other.BottomUpPathCount can be 0, in which case it is either dead or a
  322. // loop backedge. Loop backedges are special.
  323. BottomUpPathCount += Other.BottomUpPathCount;
  324. // In order to be consistent, we clear the top down pointers when by adding
  325. // BottomUpPathCount becomes OverflowOccurredValue even though "true" overflow
  326. // has not occurred.
  327. if (BottomUpPathCount == OverflowOccurredValue) {
  328. clearBottomUpPointers();
  329. return;
  330. }
  331. // Check for overflow. If we have overflow, fall back to conservative
  332. // behavior.
  333. if (BottomUpPathCount < Other.BottomUpPathCount) {
  334. BottomUpPathCount = OverflowOccurredValue;
  335. clearBottomUpPointers();
  336. return;
  337. }
  338. // For each entry in the other set, if our set has an entry with the
  339. // same key, merge the entries. Otherwise, copy the entry and merge
  340. // it with an empty entry.
  341. for (auto MI = Other.bottom_up_ptr_begin(), ME = Other.bottom_up_ptr_end();
  342. MI != ME; ++MI) {
  343. auto Pair = PerPtrBottomUp.insert(*MI);
  344. Pair.first->second.Merge(Pair.second ? BottomUpPtrState() : MI->second,
  345. /*TopDown=*/false);
  346. }
  347. // For each entry in our set, if the other set doesn't have an entry
  348. // with the same key, force it to merge with an empty entry.
  349. for (auto MI = bottom_up_ptr_begin(), ME = bottom_up_ptr_end(); MI != ME;
  350. ++MI)
  351. if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
  352. MI->second.Merge(BottomUpPtrState(), /*TopDown=*/false);
  353. }
  354. raw_ostream &llvm::operator<<(raw_ostream &OS, BBState &BBInfo) {
  355. // Dump the pointers we are tracking.
  356. OS << " TopDown State:\n";
  357. if (!BBInfo.hasTopDownPtrs()) {
  358. DEBUG(llvm::dbgs() << " NONE!\n");
  359. } else {
  360. for (auto I = BBInfo.top_down_ptr_begin(), E = BBInfo.top_down_ptr_end();
  361. I != E; ++I) {
  362. const PtrState &P = I->second;
  363. OS << " Ptr: " << *I->first
  364. << "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false")
  365. << "\n ImpreciseRelease: "
  366. << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
  367. << " HasCFGHazards: "
  368. << (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
  369. << " KnownPositive: "
  370. << (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
  371. << " Seq: "
  372. << P.GetSeq() << "\n";
  373. }
  374. }
  375. OS << " BottomUp State:\n";
  376. if (!BBInfo.hasBottomUpPtrs()) {
  377. DEBUG(llvm::dbgs() << " NONE!\n");
  378. } else {
  379. for (auto I = BBInfo.bottom_up_ptr_begin(), E = BBInfo.bottom_up_ptr_end();
  380. I != E; ++I) {
  381. const PtrState &P = I->second;
  382. OS << " Ptr: " << *I->first
  383. << "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false")
  384. << "\n ImpreciseRelease: "
  385. << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
  386. << " HasCFGHazards: "
  387. << (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
  388. << " KnownPositive: "
  389. << (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
  390. << " Seq: "
  391. << P.GetSeq() << "\n";
  392. }
  393. }
  394. return OS;
  395. }
  396. namespace {
  397. /// \brief The main ARC optimization pass.
  398. class ObjCARCOpt : public FunctionPass {
  399. bool Changed;
  400. ProvenanceAnalysis PA;
  401. /// A cache of references to runtime entry point constants.
  402. ARCRuntimeEntryPoints EP;
  403. /// A cache of MDKinds that can be passed into other functions to propagate
  404. /// MDKind identifiers.
  405. ARCMDKindCache MDKindCache;
  406. // This is used to track if a pointer is stored into an alloca.
  407. DenseSet<const Value *> MultiOwnersSet;
  408. /// A flag indicating whether this optimization pass should run.
  409. bool Run;
  410. /// Flags which determine whether each of the interesting runtine functions
  411. /// is in fact used in the current function.
  412. unsigned UsedInThisFunction;
  413. bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
  414. void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
  415. ARCInstKind &Class);
  416. void OptimizeIndividualCalls(Function &F);
  417. void CheckForCFGHazards(const BasicBlock *BB,
  418. DenseMap<const BasicBlock *, BBState> &BBStates,
  419. BBState &MyStates) const;
  420. bool VisitInstructionBottomUp(Instruction *Inst, BasicBlock *BB,
  421. BlotMapVector<Value *, RRInfo> &Retains,
  422. BBState &MyStates);
  423. bool VisitBottomUp(BasicBlock *BB,
  424. DenseMap<const BasicBlock *, BBState> &BBStates,
  425. BlotMapVector<Value *, RRInfo> &Retains);
  426. bool VisitInstructionTopDown(Instruction *Inst,
  427. DenseMap<Value *, RRInfo> &Releases,
  428. BBState &MyStates);
  429. bool VisitTopDown(BasicBlock *BB,
  430. DenseMap<const BasicBlock *, BBState> &BBStates,
  431. DenseMap<Value *, RRInfo> &Releases);
  432. bool Visit(Function &F, DenseMap<const BasicBlock *, BBState> &BBStates,
  433. BlotMapVector<Value *, RRInfo> &Retains,
  434. DenseMap<Value *, RRInfo> &Releases);
  435. void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
  436. BlotMapVector<Value *, RRInfo> &Retains,
  437. DenseMap<Value *, RRInfo> &Releases,
  438. SmallVectorImpl<Instruction *> &DeadInsts, Module *M);
  439. bool
  440. PairUpRetainsAndReleases(DenseMap<const BasicBlock *, BBState> &BBStates,
  441. BlotMapVector<Value *, RRInfo> &Retains,
  442. DenseMap<Value *, RRInfo> &Releases, Module *M,
  443. SmallVectorImpl<Instruction *> &NewRetains,
  444. SmallVectorImpl<Instruction *> &NewReleases,
  445. SmallVectorImpl<Instruction *> &DeadInsts,
  446. RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
  447. Value *Arg, bool KnownSafe,
  448. bool &AnyPairsCompletelyEliminated);
  449. bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
  450. BlotMapVector<Value *, RRInfo> &Retains,
  451. DenseMap<Value *, RRInfo> &Releases, Module *M);
  452. void OptimizeWeakCalls(Function &F);
  453. bool OptimizeSequences(Function &F);
  454. void OptimizeReturns(Function &F);
  455. #ifndef NDEBUG
  456. void GatherStatistics(Function &F, bool AfterOptimization = false);
  457. #endif
  458. void getAnalysisUsage(AnalysisUsage &AU) const override;
  459. bool doInitialization(Module &M) override;
  460. bool runOnFunction(Function &F) override;
  461. void releaseMemory() override;
  462. public:
  463. static char ID;
  464. ObjCARCOpt() : FunctionPass(ID) {
  465. initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
  466. }
  467. };
  468. }
  469. char ObjCARCOpt::ID = 0;
  470. INITIALIZE_PASS_BEGIN(ObjCARCOpt,
  471. "objc-arc", "ObjC ARC optimization", false, false)
  472. INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
  473. INITIALIZE_PASS_END(ObjCARCOpt,
  474. "objc-arc", "ObjC ARC optimization", false, false)
  475. Pass *llvm::createObjCARCOptPass() {
  476. return new ObjCARCOpt();
  477. }
  478. void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
  479. AU.addRequired<ObjCARCAliasAnalysis>();
  480. AU.addRequired<AliasAnalysis>();
  481. // ARC optimization doesn't currently split critical edges.
  482. AU.setPreservesCFG();
  483. }
  484. /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
  485. /// not a return value. Or, if it can be paired with an
  486. /// objc_autoreleaseReturnValue, delete the pair and return true.
  487. bool
  488. ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
  489. // Check for the argument being from an immediately preceding call or invoke.
  490. const Value *Arg = GetArgRCIdentityRoot(RetainRV);
  491. ImmutableCallSite CS(Arg);
  492. if (const Instruction *Call = CS.getInstruction()) {
  493. if (Call->getParent() == RetainRV->getParent()) {
  494. BasicBlock::const_iterator I = Call;
  495. ++I;
  496. while (IsNoopInstruction(I)) ++I;
  497. if (&*I == RetainRV)
  498. return false;
  499. } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
  500. BasicBlock *RetainRVParent = RetainRV->getParent();
  501. if (II->getNormalDest() == RetainRVParent) {
  502. BasicBlock::const_iterator I = RetainRVParent->begin();
  503. while (IsNoopInstruction(I)) ++I;
  504. if (&*I == RetainRV)
  505. return false;
  506. }
  507. }
  508. }
  509. // Check for being preceded by an objc_autoreleaseReturnValue on the same
  510. // pointer. In this case, we can delete the pair.
  511. BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
  512. if (I != Begin) {
  513. do --I; while (I != Begin && IsNoopInstruction(I));
  514. if (GetBasicARCInstKind(I) == ARCInstKind::AutoreleaseRV &&
  515. GetArgRCIdentityRoot(I) == Arg) {
  516. Changed = true;
  517. ++NumPeeps;
  518. DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n"
  519. << "Erasing " << *RetainRV << "\n");
  520. EraseInstruction(I);
  521. EraseInstruction(RetainRV);
  522. return true;
  523. }
  524. }
  525. // Turn it to a plain objc_retain.
  526. Changed = true;
  527. ++NumPeeps;
  528. DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
  529. "objc_retain since the operand is not a return value.\n"
  530. "Old = " << *RetainRV << "\n");
  531. Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Retain);
  532. cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
  533. DEBUG(dbgs() << "New = " << *RetainRV << "\n");
  534. return false;
  535. }
  536. /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
  537. /// used as a return value.
  538. void ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F,
  539. Instruction *AutoreleaseRV,
  540. ARCInstKind &Class) {
  541. // Check for a return of the pointer value.
  542. const Value *Ptr = GetArgRCIdentityRoot(AutoreleaseRV);
  543. SmallVector<const Value *, 2> Users;
  544. Users.push_back(Ptr);
  545. do {
  546. Ptr = Users.pop_back_val();
  547. for (const User *U : Ptr->users()) {
  548. if (isa<ReturnInst>(U) || GetBasicARCInstKind(U) == ARCInstKind::RetainRV)
  549. return;
  550. if (isa<BitCastInst>(U))
  551. Users.push_back(U);
  552. }
  553. } while (!Users.empty());
  554. Changed = true;
  555. ++NumPeeps;
  556. DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => "
  557. "objc_autorelease since its operand is not used as a return "
  558. "value.\n"
  559. "Old = " << *AutoreleaseRV << "\n");
  560. CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
  561. Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Autorelease);
  562. AutoreleaseRVCI->setCalledFunction(NewDecl);
  563. AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
  564. Class = ARCInstKind::Autorelease;
  565. DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
  566. }
  567. /// Visit each call, one at a time, and make simplifications without doing any
  568. /// additional analysis.
  569. void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
  570. DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
  571. // Reset all the flags in preparation for recomputing them.
  572. UsedInThisFunction = 0;
  573. // Visit all objc_* calls in F.
  574. for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
  575. Instruction *Inst = &*I++;
  576. ARCInstKind Class = GetBasicARCInstKind(Inst);
  577. DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
  578. switch (Class) {
  579. default: break;
  580. // Delete no-op casts. These function calls have special semantics, but
  581. // the semantics are entirely implemented via lowering in the front-end,
  582. // so by the time they reach the optimizer, they are just no-op calls
  583. // which return their argument.
  584. //
  585. // There are gray areas here, as the ability to cast reference-counted
  586. // pointers to raw void* and back allows code to break ARC assumptions,
  587. // however these are currently considered to be unimportant.
  588. case ARCInstKind::NoopCast:
  589. Changed = true;
  590. ++NumNoops;
  591. DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
  592. EraseInstruction(Inst);
  593. continue;
  594. // If the pointer-to-weak-pointer is null, it's undefined behavior.
  595. case ARCInstKind::StoreWeak:
  596. case ARCInstKind::LoadWeak:
  597. case ARCInstKind::LoadWeakRetained:
  598. case ARCInstKind::InitWeak:
  599. case ARCInstKind::DestroyWeak: {
  600. CallInst *CI = cast<CallInst>(Inst);
  601. if (IsNullOrUndef(CI->getArgOperand(0))) {
  602. Changed = true;
  603. Type *Ty = CI->getArgOperand(0)->getType();
  604. new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
  605. Constant::getNullValue(Ty),
  606. CI);
  607. llvm::Value *NewValue = UndefValue::get(CI->getType());
  608. DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
  609. "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
  610. CI->replaceAllUsesWith(NewValue);
  611. CI->eraseFromParent();
  612. continue;
  613. }
  614. break;
  615. }
  616. case ARCInstKind::CopyWeak:
  617. case ARCInstKind::MoveWeak: {
  618. CallInst *CI = cast<CallInst>(Inst);
  619. if (IsNullOrUndef(CI->getArgOperand(0)) ||
  620. IsNullOrUndef(CI->getArgOperand(1))) {
  621. Changed = true;
  622. Type *Ty = CI->getArgOperand(0)->getType();
  623. new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
  624. Constant::getNullValue(Ty),
  625. CI);
  626. llvm::Value *NewValue = UndefValue::get(CI->getType());
  627. DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
  628. "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
  629. CI->replaceAllUsesWith(NewValue);
  630. CI->eraseFromParent();
  631. continue;
  632. }
  633. break;
  634. }
  635. case ARCInstKind::RetainRV:
  636. if (OptimizeRetainRVCall(F, Inst))
  637. continue;
  638. break;
  639. case ARCInstKind::AutoreleaseRV:
  640. OptimizeAutoreleaseRVCall(F, Inst, Class);
  641. break;
  642. }
  643. // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
  644. if (IsAutorelease(Class) && Inst->use_empty()) {
  645. CallInst *Call = cast<CallInst>(Inst);
  646. const Value *Arg = Call->getArgOperand(0);
  647. Arg = FindSingleUseIdentifiedObject(Arg);
  648. if (Arg) {
  649. Changed = true;
  650. ++NumAutoreleases;
  651. // Create the declaration lazily.
  652. LLVMContext &C = Inst->getContext();
  653. Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
  654. CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
  655. Call);
  656. NewCall->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease),
  657. MDNode::get(C, None));
  658. DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
  659. "since x is otherwise unused.\nOld: " << *Call << "\nNew: "
  660. << *NewCall << "\n");
  661. EraseInstruction(Call);
  662. Inst = NewCall;
  663. Class = ARCInstKind::Release;
  664. }
  665. }
  666. // For functions which can never be passed stack arguments, add
  667. // a tail keyword.
  668. if (IsAlwaysTail(Class)) {
  669. Changed = true;
  670. DEBUG(dbgs() << "Adding tail keyword to function since it can never be "
  671. "passed stack args: " << *Inst << "\n");
  672. cast<CallInst>(Inst)->setTailCall();
  673. }
  674. // Ensure that functions that can never have a "tail" keyword due to the
  675. // semantics of ARC truly do not do so.
  676. if (IsNeverTail(Class)) {
  677. Changed = true;
  678. DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst <<
  679. "\n");
  680. cast<CallInst>(Inst)->setTailCall(false);
  681. }
  682. // Set nounwind as needed.
  683. if (IsNoThrow(Class)) {
  684. Changed = true;
  685. DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst
  686. << "\n");
  687. cast<CallInst>(Inst)->setDoesNotThrow();
  688. }
  689. if (!IsNoopOnNull(Class)) {
  690. UsedInThisFunction |= 1 << unsigned(Class);
  691. continue;
  692. }
  693. const Value *Arg = GetArgRCIdentityRoot(Inst);
  694. // ARC calls with null are no-ops. Delete them.
  695. if (IsNullOrUndef(Arg)) {
  696. Changed = true;
  697. ++NumNoops;
  698. DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst
  699. << "\n");
  700. EraseInstruction(Inst);
  701. continue;
  702. }
  703. // Keep track of which of retain, release, autorelease, and retain_block
  704. // are actually present in this function.
  705. UsedInThisFunction |= 1 << unsigned(Class);
  706. // If Arg is a PHI, and one or more incoming values to the
  707. // PHI are null, and the call is control-equivalent to the PHI, and there
  708. // are no relevant side effects between the PHI and the call, the call
  709. // could be pushed up to just those paths with non-null incoming values.
  710. // For now, don't bother splitting critical edges for this.
  711. SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
  712. Worklist.push_back(std::make_pair(Inst, Arg));
  713. do {
  714. std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
  715. Inst = Pair.first;
  716. Arg = Pair.second;
  717. const PHINode *PN = dyn_cast<PHINode>(Arg);
  718. if (!PN) continue;
  719. // Determine if the PHI has any null operands, or any incoming
  720. // critical edges.
  721. bool HasNull = false;
  722. bool HasCriticalEdges = false;
  723. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  724. Value *Incoming =
  725. GetRCIdentityRoot(PN->getIncomingValue(i));
  726. if (IsNullOrUndef(Incoming))
  727. HasNull = true;
  728. else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
  729. .getNumSuccessors() != 1) {
  730. HasCriticalEdges = true;
  731. break;
  732. }
  733. }
  734. // If we have null operands and no critical edges, optimize.
  735. if (!HasCriticalEdges && HasNull) {
  736. SmallPtrSet<Instruction *, 4> DependingInstructions;
  737. SmallPtrSet<const BasicBlock *, 4> Visited;
  738. // Check that there is nothing that cares about the reference
  739. // count between the call and the phi.
  740. switch (Class) {
  741. case ARCInstKind::Retain:
  742. case ARCInstKind::RetainBlock:
  743. // These can always be moved up.
  744. break;
  745. case ARCInstKind::Release:
  746. // These can't be moved across things that care about the retain
  747. // count.
  748. FindDependencies(NeedsPositiveRetainCount, Arg,
  749. Inst->getParent(), Inst,
  750. DependingInstructions, Visited, PA);
  751. break;
  752. case ARCInstKind::Autorelease:
  753. // These can't be moved across autorelease pool scope boundaries.
  754. FindDependencies(AutoreleasePoolBoundary, Arg,
  755. Inst->getParent(), Inst,
  756. DependingInstructions, Visited, PA);
  757. break;
  758. case ARCInstKind::RetainRV:
  759. case ARCInstKind::AutoreleaseRV:
  760. // Don't move these; the RV optimization depends on the autoreleaseRV
  761. // being tail called, and the retainRV being immediately after a call
  762. // (which might still happen if we get lucky with codegen layout, but
  763. // it's not worth taking the chance).
  764. continue;
  765. default:
  766. llvm_unreachable("Invalid dependence flavor");
  767. }
  768. if (DependingInstructions.size() == 1 &&
  769. *DependingInstructions.begin() == PN) {
  770. Changed = true;
  771. ++NumPartialNoops;
  772. // Clone the call into each predecessor that has a non-null value.
  773. CallInst *CInst = cast<CallInst>(Inst);
  774. Type *ParamTy = CInst->getArgOperand(0)->getType();
  775. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  776. Value *Incoming =
  777. GetRCIdentityRoot(PN->getIncomingValue(i));
  778. if (!IsNullOrUndef(Incoming)) {
  779. CallInst *Clone = cast<CallInst>(CInst->clone());
  780. Value *Op = PN->getIncomingValue(i);
  781. Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
  782. if (Op->getType() != ParamTy)
  783. Op = new BitCastInst(Op, ParamTy, "", InsertPos);
  784. Clone->setArgOperand(0, Op);
  785. Clone->insertBefore(InsertPos);
  786. DEBUG(dbgs() << "Cloning "
  787. << *CInst << "\n"
  788. "And inserting clone at " << *InsertPos << "\n");
  789. Worklist.push_back(std::make_pair(Clone, Incoming));
  790. }
  791. }
  792. // Erase the original call.
  793. DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
  794. EraseInstruction(CInst);
  795. continue;
  796. }
  797. }
  798. } while (!Worklist.empty());
  799. }
  800. }
  801. /// If we have a top down pointer in the S_Use state, make sure that there are
  802. /// no CFG hazards by checking the states of various bottom up pointers.
  803. static void CheckForUseCFGHazard(const Sequence SuccSSeq,
  804. const bool SuccSRRIKnownSafe,
  805. TopDownPtrState &S,
  806. bool &SomeSuccHasSame,
  807. bool &AllSuccsHaveSame,
  808. bool &NotAllSeqEqualButKnownSafe,
  809. bool &ShouldContinue) {
  810. switch (SuccSSeq) {
  811. case S_CanRelease: {
  812. if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) {
  813. S.ClearSequenceProgress();
  814. break;
  815. }
  816. S.SetCFGHazardAfflicted(true);
  817. ShouldContinue = true;
  818. break;
  819. }
  820. case S_Use:
  821. SomeSuccHasSame = true;
  822. break;
  823. case S_Stop:
  824. case S_Release:
  825. case S_MovableRelease:
  826. if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
  827. AllSuccsHaveSame = false;
  828. else
  829. NotAllSeqEqualButKnownSafe = true;
  830. break;
  831. case S_Retain:
  832. llvm_unreachable("bottom-up pointer in retain state!");
  833. case S_None:
  834. llvm_unreachable("This should have been handled earlier.");
  835. }
  836. }
  837. /// If we have a Top Down pointer in the S_CanRelease state, make sure that
  838. /// there are no CFG hazards by checking the states of various bottom up
  839. /// pointers.
  840. static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
  841. const bool SuccSRRIKnownSafe,
  842. TopDownPtrState &S,
  843. bool &SomeSuccHasSame,
  844. bool &AllSuccsHaveSame,
  845. bool &NotAllSeqEqualButKnownSafe) {
  846. switch (SuccSSeq) {
  847. case S_CanRelease:
  848. SomeSuccHasSame = true;
  849. break;
  850. case S_Stop:
  851. case S_Release:
  852. case S_MovableRelease:
  853. case S_Use:
  854. if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
  855. AllSuccsHaveSame = false;
  856. else
  857. NotAllSeqEqualButKnownSafe = true;
  858. break;
  859. case S_Retain:
  860. llvm_unreachable("bottom-up pointer in retain state!");
  861. case S_None:
  862. llvm_unreachable("This should have been handled earlier.");
  863. }
  864. }
  865. /// Check for critical edges, loop boundaries, irreducible control flow, or
  866. /// other CFG structures where moving code across the edge would result in it
  867. /// being executed more.
  868. void
  869. ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
  870. DenseMap<const BasicBlock *, BBState> &BBStates,
  871. BBState &MyStates) const {
  872. // If any top-down local-use or possible-dec has a succ which is earlier in
  873. // the sequence, forget it.
  874. for (auto I = MyStates.top_down_ptr_begin(), E = MyStates.top_down_ptr_end();
  875. I != E; ++I) {
  876. TopDownPtrState &S = I->second;
  877. const Sequence Seq = I->second.GetSeq();
  878. // We only care about S_Retain, S_CanRelease, and S_Use.
  879. if (Seq == S_None)
  880. continue;
  881. // Make sure that if extra top down states are added in the future that this
  882. // code is updated to handle it.
  883. assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) &&
  884. "Unknown top down sequence state.");
  885. const Value *Arg = I->first;
  886. const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
  887. bool SomeSuccHasSame = false;
  888. bool AllSuccsHaveSame = true;
  889. bool NotAllSeqEqualButKnownSafe = false;
  890. succ_const_iterator SI(TI), SE(TI, false);
  891. for (; SI != SE; ++SI) {
  892. // If VisitBottomUp has pointer information for this successor, take
  893. // what we know about it.
  894. const DenseMap<const BasicBlock *, BBState>::iterator BBI =
  895. BBStates.find(*SI);
  896. assert(BBI != BBStates.end());
  897. const BottomUpPtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
  898. const Sequence SuccSSeq = SuccS.GetSeq();
  899. // If bottom up, the pointer is in an S_None state, clear the sequence
  900. // progress since the sequence in the bottom up state finished
  901. // suggesting a mismatch in between retains/releases. This is true for
  902. // all three cases that we are handling here: S_Retain, S_Use, and
  903. // S_CanRelease.
  904. if (SuccSSeq == S_None) {
  905. S.ClearSequenceProgress();
  906. continue;
  907. }
  908. // If we have S_Use or S_CanRelease, perform our check for cfg hazard
  909. // checks.
  910. const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe();
  911. // *NOTE* We do not use Seq from above here since we are allowing for
  912. // S.GetSeq() to change while we are visiting basic blocks.
  913. switch(S.GetSeq()) {
  914. case S_Use: {
  915. bool ShouldContinue = false;
  916. CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame,
  917. AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
  918. ShouldContinue);
  919. if (ShouldContinue)
  920. continue;
  921. break;
  922. }
  923. case S_CanRelease: {
  924. CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S,
  925. SomeSuccHasSame, AllSuccsHaveSame,
  926. NotAllSeqEqualButKnownSafe);
  927. break;
  928. }
  929. case S_Retain:
  930. case S_None:
  931. case S_Stop:
  932. case S_Release:
  933. case S_MovableRelease:
  934. break;
  935. }
  936. }
  937. // If the state at the other end of any of the successor edges
  938. // matches the current state, require all edges to match. This
  939. // guards against loops in the middle of a sequence.
  940. if (SomeSuccHasSame && !AllSuccsHaveSame) {
  941. S.ClearSequenceProgress();
  942. } else if (NotAllSeqEqualButKnownSafe) {
  943. // If we would have cleared the state foregoing the fact that we are known
  944. // safe, stop code motion. This is because whether or not it is safe to
  945. // remove RR pairs via KnownSafe is an orthogonal concept to whether we
  946. // are allowed to perform code motion.
  947. S.SetCFGHazardAfflicted(true);
  948. }
  949. }
  950. }
  951. bool ObjCARCOpt::VisitInstructionBottomUp(
  952. Instruction *Inst, BasicBlock *BB, BlotMapVector<Value *, RRInfo> &Retains,
  953. BBState &MyStates) {
  954. bool NestingDetected = false;
  955. ARCInstKind Class = GetARCInstKind(Inst);
  956. const Value *Arg = nullptr;
  957. DEBUG(dbgs() << " Class: " << Class << "\n");
  958. switch (Class) {
  959. case ARCInstKind::Release: {
  960. Arg = GetArgRCIdentityRoot(Inst);
  961. BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
  962. NestingDetected |= S.InitBottomUp(MDKindCache, Inst);
  963. break;
  964. }
  965. case ARCInstKind::RetainBlock:
  966. // In OptimizeIndividualCalls, we have strength reduced all optimizable
  967. // objc_retainBlocks to objc_retains. Thus at this point any
  968. // objc_retainBlocks that we see are not optimizable.
  969. break;
  970. case ARCInstKind::Retain:
  971. case ARCInstKind::RetainRV: {
  972. Arg = GetArgRCIdentityRoot(Inst);
  973. BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
  974. if (S.MatchWithRetain()) {
  975. // Don't do retain+release tracking for ARCInstKind::RetainRV, because
  976. // it's better to let it remain as the first instruction after a call.
  977. if (Class != ARCInstKind::RetainRV) {
  978. DEBUG(llvm::dbgs() << " Matching with: " << *Inst << "\n");
  979. Retains[Inst] = S.GetRRInfo();
  980. }
  981. S.ClearSequenceProgress();
  982. }
  983. // A retain moving bottom up can be a use.
  984. break;
  985. }
  986. case ARCInstKind::AutoreleasepoolPop:
  987. // Conservatively, clear MyStates for all known pointers.
  988. MyStates.clearBottomUpPointers();
  989. return NestingDetected;
  990. case ARCInstKind::AutoreleasepoolPush:
  991. case ARCInstKind::None:
  992. // These are irrelevant.
  993. return NestingDetected;
  994. case ARCInstKind::User:
  995. // If we have a store into an alloca of a pointer we are tracking, the
  996. // pointer has multiple owners implying that we must be more conservative.
  997. //
  998. // This comes up in the context of a pointer being ``KnownSafe''. In the
  999. // presence of a block being initialized, the frontend will emit the
  1000. // objc_retain on the original pointer and the release on the pointer loaded
  1001. // from the alloca. The optimizer will through the provenance analysis
  1002. // realize that the two are related, but since we only require KnownSafe in
  1003. // one direction, will match the inner retain on the original pointer with
  1004. // the guard release on the original pointer. This is fixed by ensuring that
  1005. // in the presence of allocas we only unconditionally remove pointers if
  1006. // both our retain and our release are KnownSafe.
  1007. if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
  1008. const DataLayout &DL = BB->getModule()->getDataLayout();
  1009. if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand(), DL)) {
  1010. auto I = MyStates.findPtrBottomUpState(
  1011. GetRCIdentityRoot(SI->getValueOperand()));
  1012. if (I != MyStates.bottom_up_ptr_end())
  1013. MultiOwnersSet.insert(I->first);
  1014. }
  1015. }
  1016. break;
  1017. default:
  1018. break;
  1019. }
  1020. // Consider any other possible effects of this instruction on each
  1021. // pointer being tracked.
  1022. for (auto MI = MyStates.bottom_up_ptr_begin(),
  1023. ME = MyStates.bottom_up_ptr_end();
  1024. MI != ME; ++MI) {
  1025. const Value *Ptr = MI->first;
  1026. if (Ptr == Arg)
  1027. continue; // Handled above.
  1028. BottomUpPtrState &S = MI->second;
  1029. if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class))
  1030. continue;
  1031. S.HandlePotentialUse(BB, Inst, Ptr, PA, Class);
  1032. }
  1033. return NestingDetected;
  1034. }
  1035. bool ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
  1036. DenseMap<const BasicBlock *, BBState> &BBStates,
  1037. BlotMapVector<Value *, RRInfo> &Retains) {
  1038. DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
  1039. bool NestingDetected = false;
  1040. BBState &MyStates = BBStates[BB];
  1041. // Merge the states from each successor to compute the initial state
  1042. // for the current block.
  1043. BBState::edge_iterator SI(MyStates.succ_begin()),
  1044. SE(MyStates.succ_end());
  1045. if (SI != SE) {
  1046. const BasicBlock *Succ = *SI;
  1047. DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
  1048. assert(I != BBStates.end());
  1049. MyStates.InitFromSucc(I->second);
  1050. ++SI;
  1051. for (; SI != SE; ++SI) {
  1052. Succ = *SI;
  1053. I = BBStates.find(Succ);
  1054. assert(I != BBStates.end());
  1055. MyStates.MergeSucc(I->second);
  1056. }
  1057. }
  1058. DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB] << "\n"
  1059. << "Performing Dataflow:\n");
  1060. // Visit all the instructions, bottom-up.
  1061. for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
  1062. Instruction *Inst = std::prev(I);
  1063. // Invoke instructions are visited as part of their successors (below).
  1064. if (isa<InvokeInst>(Inst))
  1065. continue;
  1066. DEBUG(dbgs() << " Visiting " << *Inst << "\n");
  1067. NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
  1068. }
  1069. // If there's a predecessor with an invoke, visit the invoke as if it were
  1070. // part of this block, since we can't insert code after an invoke in its own
  1071. // block, and we don't want to split critical edges.
  1072. for (BBState::edge_iterator PI(MyStates.pred_begin()),
  1073. PE(MyStates.pred_end()); PI != PE; ++PI) {
  1074. BasicBlock *Pred = *PI;
  1075. if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
  1076. NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
  1077. }
  1078. DEBUG(llvm::dbgs() << "\nFinal State:\n" << BBStates[BB] << "\n");
  1079. return NestingDetected;
  1080. }
  1081. bool
  1082. ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
  1083. DenseMap<Value *, RRInfo> &Releases,
  1084. BBState &MyStates) {
  1085. bool NestingDetected = false;
  1086. ARCInstKind Class = GetARCInstKind(Inst);
  1087. const Value *Arg = nullptr;
  1088. DEBUG(llvm::dbgs() << " Class: " << Class << "\n");
  1089. switch (Class) {
  1090. case ARCInstKind::RetainBlock:
  1091. // In OptimizeIndividualCalls, we have strength reduced all optimizable
  1092. // objc_retainBlocks to objc_retains. Thus at this point any
  1093. // objc_retainBlocks that we see are not optimizable. We need to break since
  1094. // a retain can be a potential use.
  1095. break;
  1096. case ARCInstKind::Retain:
  1097. case ARCInstKind::RetainRV: {
  1098. Arg = GetArgRCIdentityRoot(Inst);
  1099. TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
  1100. NestingDetected |= S.InitTopDown(Class, Inst);
  1101. // A retain can be a potential use; procede to the generic checking
  1102. // code below.
  1103. break;
  1104. }
  1105. case ARCInstKind::Release: {
  1106. Arg = GetArgRCIdentityRoot(Inst);
  1107. TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
  1108. // Try to form a tentative pair in between this release instruction and the
  1109. // top down pointers that we are tracking.
  1110. if (S.MatchWithRelease(MDKindCache, Inst)) {
  1111. // If we succeed, copy S's RRInfo into the Release -> {Retain Set
  1112. // Map}. Then we clear S.
  1113. DEBUG(llvm::dbgs() << " Matching with: " << *Inst << "\n");
  1114. Releases[Inst] = S.GetRRInfo();
  1115. S.ClearSequenceProgress();
  1116. }
  1117. break;
  1118. }
  1119. case ARCInstKind::AutoreleasepoolPop:
  1120. // Conservatively, clear MyStates for all known pointers.
  1121. MyStates.clearTopDownPointers();
  1122. return false;
  1123. case ARCInstKind::AutoreleasepoolPush:
  1124. case ARCInstKind::None:
  1125. // These can not be uses of
  1126. return false;
  1127. default:
  1128. break;
  1129. }
  1130. // Consider any other possible effects of this instruction on each
  1131. // pointer being tracked.
  1132. for (auto MI = MyStates.top_down_ptr_begin(),
  1133. ME = MyStates.top_down_ptr_end();
  1134. MI != ME; ++MI) {
  1135. const Value *Ptr = MI->first;
  1136. if (Ptr == Arg)
  1137. continue; // Handled above.
  1138. TopDownPtrState &S = MI->second;
  1139. if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class))
  1140. continue;
  1141. S.HandlePotentialUse(Inst, Ptr, PA, Class);
  1142. }
  1143. return NestingDetected;
  1144. }
  1145. bool
  1146. ObjCARCOpt::VisitTopDown(BasicBlock *BB,
  1147. DenseMap<const BasicBlock *, BBState> &BBStates,
  1148. DenseMap<Value *, RRInfo> &Releases) {
  1149. DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n");
  1150. bool NestingDetected = false;
  1151. BBState &MyStates = BBStates[BB];
  1152. // Merge the states from each predecessor to compute the initial state
  1153. // for the current block.
  1154. BBState::edge_iterator PI(MyStates.pred_begin()),
  1155. PE(MyStates.pred_end());
  1156. if (PI != PE) {
  1157. const BasicBlock *Pred = *PI;
  1158. DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
  1159. assert(I != BBStates.end());
  1160. MyStates.InitFromPred(I->second);
  1161. ++PI;
  1162. for (; PI != PE; ++PI) {
  1163. Pred = *PI;
  1164. I = BBStates.find(Pred);
  1165. assert(I != BBStates.end());
  1166. MyStates.MergePred(I->second);
  1167. }
  1168. }
  1169. DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB] << "\n"
  1170. << "Performing Dataflow:\n");
  1171. // Visit all the instructions, top-down.
  1172. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
  1173. Instruction *Inst = I;
  1174. DEBUG(dbgs() << " Visiting " << *Inst << "\n");
  1175. NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
  1176. }
  1177. DEBUG(llvm::dbgs() << "\nState Before Checking for CFG Hazards:\n"
  1178. << BBStates[BB] << "\n\n");
  1179. CheckForCFGHazards(BB, BBStates, MyStates);
  1180. DEBUG(llvm::dbgs() << "Final State:\n" << BBStates[BB] << "\n");
  1181. return NestingDetected;
  1182. }
  1183. static void
  1184. ComputePostOrders(Function &F,
  1185. SmallVectorImpl<BasicBlock *> &PostOrder,
  1186. SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
  1187. unsigned NoObjCARCExceptionsMDKind,
  1188. DenseMap<const BasicBlock *, BBState> &BBStates) {
  1189. /// The visited set, for doing DFS walks.
  1190. SmallPtrSet<BasicBlock *, 16> Visited;
  1191. // Do DFS, computing the PostOrder.
  1192. SmallPtrSet<BasicBlock *, 16> OnStack;
  1193. SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
  1194. // Functions always have exactly one entry block, and we don't have
  1195. // any other block that we treat like an entry block.
  1196. BasicBlock *EntryBB = &F.getEntryBlock();
  1197. BBState &MyStates = BBStates[EntryBB];
  1198. MyStates.SetAsEntry();
  1199. TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
  1200. SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
  1201. Visited.insert(EntryBB);
  1202. OnStack.insert(EntryBB);
  1203. do {
  1204. dfs_next_succ:
  1205. BasicBlock *CurrBB = SuccStack.back().first;
  1206. TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
  1207. succ_iterator SE(TI, false);
  1208. while (SuccStack.back().second != SE) {
  1209. BasicBlock *SuccBB = *SuccStack.back().second++;
  1210. if (Visited.insert(SuccBB).second) {
  1211. TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
  1212. SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
  1213. BBStates[CurrBB].addSucc(SuccBB);
  1214. BBState &SuccStates = BBStates[SuccBB];
  1215. SuccStates.addPred(CurrBB);
  1216. OnStack.insert(SuccBB);
  1217. goto dfs_next_succ;
  1218. }
  1219. if (!OnStack.count(SuccBB)) {
  1220. BBStates[CurrBB].addSucc(SuccBB);
  1221. BBStates[SuccBB].addPred(CurrBB);
  1222. }
  1223. }
  1224. OnStack.erase(CurrBB);
  1225. PostOrder.push_back(CurrBB);
  1226. SuccStack.pop_back();
  1227. } while (!SuccStack.empty());
  1228. Visited.clear();
  1229. // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
  1230. // Functions may have many exits, and there also blocks which we treat
  1231. // as exits due to ignored edges.
  1232. SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
  1233. for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
  1234. BasicBlock *ExitBB = I;
  1235. BBState &MyStates = BBStates[ExitBB];
  1236. if (!MyStates.isExit())
  1237. continue;
  1238. MyStates.SetAsExit();
  1239. PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
  1240. Visited.insert(ExitBB);
  1241. while (!PredStack.empty()) {
  1242. reverse_dfs_next_succ:
  1243. BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
  1244. while (PredStack.back().second != PE) {
  1245. BasicBlock *BB = *PredStack.back().second++;
  1246. if (Visited.insert(BB).second) {
  1247. PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
  1248. goto reverse_dfs_next_succ;
  1249. }
  1250. }
  1251. ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
  1252. }
  1253. }
  1254. }
  1255. // Visit the function both top-down and bottom-up.
  1256. bool ObjCARCOpt::Visit(Function &F,
  1257. DenseMap<const BasicBlock *, BBState> &BBStates,
  1258. BlotMapVector<Value *, RRInfo> &Retains,
  1259. DenseMap<Value *, RRInfo> &Releases) {
  1260. // Use reverse-postorder traversals, because we magically know that loops
  1261. // will be well behaved, i.e. they won't repeatedly call retain on a single
  1262. // pointer without doing a release. We can't use the ReversePostOrderTraversal
  1263. // class here because we want the reverse-CFG postorder to consider each
  1264. // function exit point, and we want to ignore selected cycle edges.
  1265. SmallVector<BasicBlock *, 16> PostOrder;
  1266. SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
  1267. ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
  1268. MDKindCache.get(ARCMDKindID::NoObjCARCExceptions),
  1269. BBStates);
  1270. // Use reverse-postorder on the reverse CFG for bottom-up.
  1271. bool BottomUpNestingDetected = false;
  1272. for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
  1273. ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
  1274. I != E; ++I)
  1275. BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
  1276. // Use reverse-postorder for top-down.
  1277. bool TopDownNestingDetected = false;
  1278. for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
  1279. PostOrder.rbegin(), E = PostOrder.rend();
  1280. I != E; ++I)
  1281. TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
  1282. return TopDownNestingDetected && BottomUpNestingDetected;
  1283. }
  1284. /// Move the calls in RetainsToMove and ReleasesToMove.
  1285. void ObjCARCOpt::MoveCalls(Value *Arg, RRInfo &RetainsToMove,
  1286. RRInfo &ReleasesToMove,
  1287. BlotMapVector<Value *, RRInfo> &Retains,
  1288. DenseMap<Value *, RRInfo> &Releases,
  1289. SmallVectorImpl<Instruction *> &DeadInsts,
  1290. Module *M) {
  1291. Type *ArgTy = Arg->getType();
  1292. Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
  1293. DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n");
  1294. // Insert the new retain and release calls.
  1295. for (Instruction *InsertPt : ReleasesToMove.ReverseInsertPts) {
  1296. Value *MyArg = ArgTy == ParamTy ? Arg :
  1297. new BitCastInst(Arg, ParamTy, "", InsertPt);
  1298. Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
  1299. CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
  1300. Call->setDoesNotThrow();
  1301. Call->setTailCall();
  1302. DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n"
  1303. "At insertion point: " << *InsertPt << "\n");
  1304. }
  1305. for (Instruction *InsertPt : RetainsToMove.ReverseInsertPts) {
  1306. Value *MyArg = ArgTy == ParamTy ? Arg :
  1307. new BitCastInst(Arg, ParamTy, "", InsertPt);
  1308. Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
  1309. CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
  1310. // Attach a clang.imprecise_release metadata tag, if appropriate.
  1311. if (MDNode *M = ReleasesToMove.ReleaseMetadata)
  1312. Call->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease), M);
  1313. Call->setDoesNotThrow();
  1314. if (ReleasesToMove.IsTailCallRelease)
  1315. Call->setTailCall();
  1316. DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n"
  1317. "At insertion point: " << *InsertPt << "\n");
  1318. }
  1319. // Delete the original retain and release calls.
  1320. for (Instruction *OrigRetain : RetainsToMove.Calls) {
  1321. Retains.blot(OrigRetain);
  1322. DeadInsts.push_back(OrigRetain);
  1323. DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n");
  1324. }
  1325. for (Instruction *OrigRelease : ReleasesToMove.Calls) {
  1326. Releases.erase(OrigRelease);
  1327. DeadInsts.push_back(OrigRelease);
  1328. DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
  1329. }
  1330. }
  1331. bool ObjCARCOpt::PairUpRetainsAndReleases(
  1332. DenseMap<const BasicBlock *, BBState> &BBStates,
  1333. BlotMapVector<Value *, RRInfo> &Retains,
  1334. DenseMap<Value *, RRInfo> &Releases, Module *M,
  1335. SmallVectorImpl<Instruction *> &NewRetains,
  1336. SmallVectorImpl<Instruction *> &NewReleases,
  1337. SmallVectorImpl<Instruction *> &DeadInsts, RRInfo &RetainsToMove,
  1338. RRInfo &ReleasesToMove, Value *Arg, bool KnownSafe,
  1339. bool &AnyPairsCompletelyEliminated) {
  1340. // If a pair happens in a region where it is known that the reference count
  1341. // is already incremented, we can similarly ignore possible decrements unless
  1342. // we are dealing with a retainable object with multiple provenance sources.
  1343. bool KnownSafeTD = true, KnownSafeBU = true;
  1344. bool MultipleOwners = false;
  1345. bool CFGHazardAfflicted = false;
  1346. // Connect the dots between the top-down-collected RetainsToMove and
  1347. // bottom-up-collected ReleasesToMove to form sets of related calls.
  1348. // This is an iterative process so that we connect multiple releases
  1349. // to multiple retains if needed.
  1350. unsigned OldDelta = 0;
  1351. unsigned NewDelta = 0;
  1352. unsigned OldCount = 0;
  1353. unsigned NewCount = 0;
  1354. bool FirstRelease = true;
  1355. for (;;) {
  1356. for (SmallVectorImpl<Instruction *>::const_iterator
  1357. NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
  1358. Instruction *NewRetain = *NI;
  1359. auto It = Retains.find(NewRetain);
  1360. assert(It != Retains.end());
  1361. const RRInfo &NewRetainRRI = It->second;
  1362. KnownSafeTD &= NewRetainRRI.KnownSafe;
  1363. MultipleOwners =
  1364. MultipleOwners || MultiOwnersSet.count(GetArgRCIdentityRoot(NewRetain));
  1365. for (Instruction *NewRetainRelease : NewRetainRRI.Calls) {
  1366. auto Jt = Releases.find(NewRetainRelease);
  1367. if (Jt == Releases.end())
  1368. return false;
  1369. const RRInfo &NewRetainReleaseRRI = Jt->second;
  1370. // If the release does not have a reference to the retain as well,
  1371. // something happened which is unaccounted for. Do not do anything.
  1372. //
  1373. // This can happen if we catch an additive overflow during path count
  1374. // merging.
  1375. if (!NewRetainReleaseRRI.Calls.count(NewRetain))
  1376. return false;
  1377. if (ReleasesToMove.Calls.insert(NewRetainRelease).second) {
  1378. // If we overflow when we compute the path count, don't remove/move
  1379. // anything.
  1380. const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()];
  1381. unsigned PathCount = BBState::OverflowOccurredValue;
  1382. if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
  1383. return false;
  1384. assert(PathCount != BBState::OverflowOccurredValue &&
  1385. "PathCount at this point can not be "
  1386. "OverflowOccurredValue.");
  1387. OldDelta -= PathCount;
  1388. // Merge the ReleaseMetadata and IsTailCallRelease values.
  1389. if (FirstRelease) {
  1390. ReleasesToMove.ReleaseMetadata =
  1391. NewRetainReleaseRRI.ReleaseMetadata;
  1392. ReleasesToMove.IsTailCallRelease =
  1393. NewRetainReleaseRRI.IsTailCallRelease;
  1394. FirstRelease = false;
  1395. } else {
  1396. if (ReleasesToMove.ReleaseMetadata !=
  1397. NewRetainReleaseRRI.ReleaseMetadata)
  1398. ReleasesToMove.ReleaseMetadata = nullptr;
  1399. if (ReleasesToMove.IsTailCallRelease !=
  1400. NewRetainReleaseRRI.IsTailCallRelease)
  1401. ReleasesToMove.IsTailCallRelease = false;
  1402. }
  1403. // Collect the optimal insertion points.
  1404. if (!KnownSafe)
  1405. for (Instruction *RIP : NewRetainReleaseRRI.ReverseInsertPts) {
  1406. if (ReleasesToMove.ReverseInsertPts.insert(RIP).second) {
  1407. // If we overflow when we compute the path count, don't
  1408. // remove/move anything.
  1409. const BBState &RIPBBState = BBStates[RIP->getParent()];
  1410. PathCount = BBState::OverflowOccurredValue;
  1411. if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
  1412. return false;
  1413. assert(PathCount != BBState::OverflowOccurredValue &&
  1414. "PathCount at this point can not be "
  1415. "OverflowOccurredValue.");
  1416. NewDelta -= PathCount;
  1417. }
  1418. }
  1419. NewReleases.push_back(NewRetainRelease);
  1420. }
  1421. }
  1422. }
  1423. NewRetains.clear();
  1424. if (NewReleases.empty()) break;
  1425. // Back the other way.
  1426. for (SmallVectorImpl<Instruction *>::const_iterator
  1427. NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
  1428. Instruction *NewRelease = *NI;
  1429. auto It = Releases.find(NewRelease);
  1430. assert(It != Releases.end());
  1431. const RRInfo &NewReleaseRRI = It->second;
  1432. KnownSafeBU &= NewReleaseRRI.KnownSafe;
  1433. CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
  1434. for (Instruction *NewReleaseRetain : NewReleaseRRI.Calls) {
  1435. auto Jt = Retains.find(NewReleaseRetain);
  1436. if (Jt == Retains.end())
  1437. return false;
  1438. const RRInfo &NewReleaseRetainRRI = Jt->second;
  1439. // If the retain does not have a reference to the release as well,
  1440. // something happened which is unaccounted for. Do not do anything.
  1441. //
  1442. // This can happen if we catch an additive overflow during path count
  1443. // merging.
  1444. if (!NewReleaseRetainRRI.Calls.count(NewRelease))
  1445. return false;
  1446. if (RetainsToMove.Calls.insert(NewReleaseRetain).second) {
  1447. // If we overflow when we compute the path count, don't remove/move
  1448. // anything.
  1449. const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()];
  1450. unsigned PathCount = BBState::OverflowOccurredValue;
  1451. if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
  1452. return false;
  1453. assert(PathCount != BBState::OverflowOccurredValue &&
  1454. "PathCount at this point can not be "
  1455. "OverflowOccurredValue.");
  1456. OldDelta += PathCount;
  1457. OldCount += PathCount;
  1458. // Collect the optimal insertion points.
  1459. if (!KnownSafe)
  1460. for (Instruction *RIP : NewReleaseRetainRRI.ReverseInsertPts) {
  1461. if (RetainsToMove.ReverseInsertPts.insert(RIP).second) {
  1462. // If we overflow when we compute the path count, don't
  1463. // remove/move anything.
  1464. const BBState &RIPBBState = BBStates[RIP->getParent()];
  1465. PathCount = BBState::OverflowOccurredValue;
  1466. if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
  1467. return false;
  1468. assert(PathCount != BBState::OverflowOccurredValue &&
  1469. "PathCount at this point can not be "
  1470. "OverflowOccurredValue.");
  1471. NewDelta += PathCount;
  1472. NewCount += PathCount;
  1473. }
  1474. }
  1475. NewRetains.push_back(NewReleaseRetain);
  1476. }
  1477. }
  1478. }
  1479. NewReleases.clear();
  1480. if (NewRetains.empty()) break;
  1481. }
  1482. // We can only remove pointers if we are known safe in both directions.
  1483. bool UnconditionallySafe = KnownSafeTD && KnownSafeBU;
  1484. if (UnconditionallySafe) {
  1485. RetainsToMove.ReverseInsertPts.clear();
  1486. ReleasesToMove.ReverseInsertPts.clear();
  1487. NewCount = 0;
  1488. } else {
  1489. // Determine whether the new insertion points we computed preserve the
  1490. // balance of retain and release calls through the program.
  1491. // TODO: If the fully aggressive solution isn't valid, try to find a
  1492. // less aggressive solution which is.
  1493. if (NewDelta != 0)
  1494. return false;
  1495. // At this point, we are not going to remove any RR pairs, but we still are
  1496. // able to move RR pairs. If one of our pointers is afflicted with
  1497. // CFGHazards, we cannot perform such code motion so exit early.
  1498. const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() ||
  1499. ReleasesToMove.ReverseInsertPts.size();
  1500. if (CFGHazardAfflicted && WillPerformCodeMotion)
  1501. return false;
  1502. }
  1503. // Determine whether the original call points are balanced in the retain and
  1504. // release calls through the program. If not, conservatively don't touch
  1505. // them.
  1506. // TODO: It's theoretically possible to do code motion in this case, as
  1507. // long as the existing imbalances are maintained.
  1508. if (OldDelta != 0)
  1509. return false;
  1510. Changed = true;
  1511. assert(OldCount != 0 && "Unreachable code?");
  1512. NumRRs += OldCount - NewCount;
  1513. // Set to true if we completely removed any RR pairs.
  1514. AnyPairsCompletelyEliminated = NewCount == 0;
  1515. // We can move calls!
  1516. return true;
  1517. }
  1518. /// Identify pairings between the retains and releases, and delete and/or move
  1519. /// them.
  1520. bool ObjCARCOpt::PerformCodePlacement(
  1521. DenseMap<const BasicBlock *, BBState> &BBStates,
  1522. BlotMapVector<Value *, RRInfo> &Retains,
  1523. DenseMap<Value *, RRInfo> &Releases, Module *M) {
  1524. DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
  1525. bool AnyPairsCompletelyEliminated = false;
  1526. RRInfo RetainsToMove;
  1527. RRInfo ReleasesToMove;
  1528. SmallVector<Instruction *, 4> NewRetains;
  1529. SmallVector<Instruction *, 4> NewReleases;
  1530. SmallVector<Instruction *, 8> DeadInsts;
  1531. // Visit each retain.
  1532. for (BlotMapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
  1533. E = Retains.end();
  1534. I != E; ++I) {
  1535. Value *V = I->first;
  1536. if (!V) continue; // blotted
  1537. Instruction *Retain = cast<Instruction>(V);
  1538. DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
  1539. Value *Arg = GetArgRCIdentityRoot(Retain);
  1540. // If the object being released is in static or stack storage, we know it's
  1541. // not being managed by ObjC reference counting, so we can delete pairs
  1542. // regardless of what possible decrements or uses lie between them.
  1543. bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
  1544. // A constant pointer can't be pointing to an object on the heap. It may
  1545. // be reference-counted, but it won't be deleted.
  1546. if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
  1547. if (const GlobalVariable *GV =
  1548. dyn_cast<GlobalVariable>(
  1549. GetRCIdentityRoot(LI->getPointerOperand())))
  1550. if (GV->isConstant())
  1551. KnownSafe = true;
  1552. // Connect the dots between the top-down-collected RetainsToMove and
  1553. // bottom-up-collected ReleasesToMove to form sets of related calls.
  1554. NewRetains.push_back(Retain);
  1555. bool PerformMoveCalls = PairUpRetainsAndReleases(
  1556. BBStates, Retains, Releases, M, NewRetains, NewReleases, DeadInsts,
  1557. RetainsToMove, ReleasesToMove, Arg, KnownSafe,
  1558. AnyPairsCompletelyEliminated);
  1559. if (PerformMoveCalls) {
  1560. // Ok, everything checks out and we're all set. Let's move/delete some
  1561. // code!
  1562. MoveCalls(Arg, RetainsToMove, ReleasesToMove,
  1563. Retains, Releases, DeadInsts, M);
  1564. }
  1565. // Clean up state for next retain.
  1566. NewReleases.clear();
  1567. NewRetains.clear();
  1568. RetainsToMove.clear();
  1569. ReleasesToMove.clear();
  1570. }
  1571. // Now that we're done moving everything, we can delete the newly dead
  1572. // instructions, as we no longer need them as insert points.
  1573. while (!DeadInsts.empty())
  1574. EraseInstruction(DeadInsts.pop_back_val());
  1575. return AnyPairsCompletelyEliminated;
  1576. }
  1577. /// Weak pointer optimizations.
  1578. void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
  1579. DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n");
  1580. // First, do memdep-style RLE and S2L optimizations. We can't use memdep
  1581. // itself because it uses AliasAnalysis and we need to do provenance
  1582. // queries instead.
  1583. for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
  1584. Instruction *Inst = &*I++;
  1585. DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
  1586. ARCInstKind Class = GetBasicARCInstKind(Inst);
  1587. if (Class != ARCInstKind::LoadWeak &&
  1588. Class != ARCInstKind::LoadWeakRetained)
  1589. continue;
  1590. // Delete objc_loadWeak calls with no users.
  1591. if (Class == ARCInstKind::LoadWeak && Inst->use_empty()) {
  1592. Inst->eraseFromParent();
  1593. continue;
  1594. }
  1595. // TODO: For now, just look for an earlier available version of this value
  1596. // within the same block. Theoretically, we could do memdep-style non-local
  1597. // analysis too, but that would want caching. A better approach would be to
  1598. // use the technique that EarlyCSE uses.
  1599. inst_iterator Current = std::prev(I);
  1600. BasicBlock *CurrentBB = Current.getBasicBlockIterator();
  1601. for (BasicBlock::iterator B = CurrentBB->begin(),
  1602. J = Current.getInstructionIterator();
  1603. J != B; --J) {
  1604. Instruction *EarlierInst = &*std::prev(J);
  1605. ARCInstKind EarlierClass = GetARCInstKind(EarlierInst);
  1606. switch (EarlierClass) {
  1607. case ARCInstKind::LoadWeak:
  1608. case ARCInstKind::LoadWeakRetained: {
  1609. // If this is loading from the same pointer, replace this load's value
  1610. // with that one.
  1611. CallInst *Call = cast<CallInst>(Inst);
  1612. CallInst *EarlierCall = cast<CallInst>(EarlierInst);
  1613. Value *Arg = Call->getArgOperand(0);
  1614. Value *EarlierArg = EarlierCall->getArgOperand(0);
  1615. switch (PA.getAA()->alias(Arg, EarlierArg)) {
  1616. case MustAlias:
  1617. Changed = true;
  1618. // If the load has a builtin retain, insert a plain retain for it.
  1619. if (Class == ARCInstKind::LoadWeakRetained) {
  1620. Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
  1621. CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
  1622. CI->setTailCall();
  1623. }
  1624. // Zap the fully redundant load.
  1625. Call->replaceAllUsesWith(EarlierCall);
  1626. Call->eraseFromParent();
  1627. goto clobbered;
  1628. case MayAlias:
  1629. case PartialAlias:
  1630. goto clobbered;
  1631. case NoAlias:
  1632. break;
  1633. }
  1634. break;
  1635. }
  1636. case ARCInstKind::StoreWeak:
  1637. case ARCInstKind::InitWeak: {
  1638. // If this is storing to the same pointer and has the same size etc.
  1639. // replace this load's value with the stored value.
  1640. CallInst *Call = cast<CallInst>(Inst);
  1641. CallInst *EarlierCall = cast<CallInst>(EarlierInst);
  1642. Value *Arg = Call->getArgOperand(0);
  1643. Value *EarlierArg = EarlierCall->getArgOperand(0);
  1644. switch (PA.getAA()->alias(Arg, EarlierArg)) {
  1645. case MustAlias:
  1646. Changed = true;
  1647. // If the load has a builtin retain, insert a plain retain for it.
  1648. if (Class == ARCInstKind::LoadWeakRetained) {
  1649. Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
  1650. CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
  1651. CI->setTailCall();
  1652. }
  1653. // Zap the fully redundant load.
  1654. Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
  1655. Call->eraseFromParent();
  1656. goto clobbered;
  1657. case MayAlias:
  1658. case PartialAlias:
  1659. goto clobbered;
  1660. case NoAlias:
  1661. break;
  1662. }
  1663. break;
  1664. }
  1665. case ARCInstKind::MoveWeak:
  1666. case ARCInstKind::CopyWeak:
  1667. // TOOD: Grab the copied value.
  1668. goto clobbered;
  1669. case ARCInstKind::AutoreleasepoolPush:
  1670. case ARCInstKind::None:
  1671. case ARCInstKind::IntrinsicUser:
  1672. case ARCInstKind::User:
  1673. // Weak pointers are only modified through the weak entry points
  1674. // (and arbitrary calls, which could call the weak entry points).
  1675. break;
  1676. default:
  1677. // Anything else could modify the weak pointer.
  1678. goto clobbered;
  1679. }
  1680. }
  1681. clobbered:;
  1682. }
  1683. // Then, for each destroyWeak with an alloca operand, check to see if
  1684. // the alloca and all its users can be zapped.
  1685. for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
  1686. Instruction *Inst = &*I++;
  1687. ARCInstKind Class = GetBasicARCInstKind(Inst);
  1688. if (Class != ARCInstKind::DestroyWeak)
  1689. continue;
  1690. CallInst *Call = cast<CallInst>(Inst);
  1691. Value *Arg = Call->getArgOperand(0);
  1692. if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
  1693. for (User *U : Alloca->users()) {
  1694. const Instruction *UserInst = cast<Instruction>(U);
  1695. switch (GetBasicARCInstKind(UserInst)) {
  1696. case ARCInstKind::InitWeak:
  1697. case ARCInstKind::StoreWeak:
  1698. case ARCInstKind::DestroyWeak:
  1699. continue;
  1700. default:
  1701. goto done;
  1702. }
  1703. }
  1704. Changed = true;
  1705. for (auto UI = Alloca->user_begin(), UE = Alloca->user_end(); UI != UE;) {
  1706. CallInst *UserInst = cast<CallInst>(*UI++);
  1707. switch (GetBasicARCInstKind(UserInst)) {
  1708. case ARCInstKind::InitWeak:
  1709. case ARCInstKind::StoreWeak:
  1710. // These functions return their second argument.
  1711. UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
  1712. break;
  1713. case ARCInstKind::DestroyWeak:
  1714. // No return value.
  1715. break;
  1716. default:
  1717. llvm_unreachable("alloca really is used!");
  1718. }
  1719. UserInst->eraseFromParent();
  1720. }
  1721. Alloca->eraseFromParent();
  1722. done:;
  1723. }
  1724. }
  1725. }
  1726. /// Identify program paths which execute sequences of retains and releases which
  1727. /// can be eliminated.
  1728. bool ObjCARCOpt::OptimizeSequences(Function &F) {
  1729. // Releases, Retains - These are used to store the results of the main flow
  1730. // analysis. These use Value* as the key instead of Instruction* so that the
  1731. // map stays valid when we get around to rewriting code and calls get
  1732. // replaced by arguments.
  1733. DenseMap<Value *, RRInfo> Releases;
  1734. BlotMapVector<Value *, RRInfo> Retains;
  1735. // This is used during the traversal of the function to track the
  1736. // states for each identified object at each block.
  1737. DenseMap<const BasicBlock *, BBState> BBStates;
  1738. // Analyze the CFG of the function, and all instructions.
  1739. bool NestingDetected = Visit(F, BBStates, Retains, Releases);
  1740. // Transform.
  1741. bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
  1742. Releases,
  1743. F.getParent());
  1744. // Cleanup.
  1745. MultiOwnersSet.clear();
  1746. return AnyPairsCompletelyEliminated && NestingDetected;
  1747. }
  1748. /// Check if there is a dependent call earlier that does not have anything in
  1749. /// between the Retain and the call that can affect the reference count of their
  1750. /// shared pointer argument. Note that Retain need not be in BB.
  1751. static bool
  1752. HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain,
  1753. SmallPtrSetImpl<Instruction *> &DepInsts,
  1754. SmallPtrSetImpl<const BasicBlock *> &Visited,
  1755. ProvenanceAnalysis &PA) {
  1756. FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
  1757. DepInsts, Visited, PA);
  1758. if (DepInsts.size() != 1)
  1759. return false;
  1760. auto *Call = dyn_cast_or_null<CallInst>(*DepInsts.begin());
  1761. // Check that the pointer is the return value of the call.
  1762. if (!Call || Arg != Call)
  1763. return false;
  1764. // Check that the call is a regular call.
  1765. ARCInstKind Class = GetBasicARCInstKind(Call);
  1766. if (Class != ARCInstKind::CallOrUser && Class != ARCInstKind::Call)
  1767. return false;
  1768. return true;
  1769. }
  1770. /// Find a dependent retain that precedes the given autorelease for which there
  1771. /// is nothing in between the two instructions that can affect the ref count of
  1772. /// Arg.
  1773. static CallInst *
  1774. FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB,
  1775. Instruction *Autorelease,
  1776. SmallPtrSetImpl<Instruction *> &DepInsts,
  1777. SmallPtrSetImpl<const BasicBlock *> &Visited,
  1778. ProvenanceAnalysis &PA) {
  1779. FindDependencies(CanChangeRetainCount, Arg,
  1780. BB, Autorelease, DepInsts, Visited, PA);
  1781. if (DepInsts.size() != 1)
  1782. return nullptr;
  1783. auto *Retain = dyn_cast_or_null<CallInst>(*DepInsts.begin());
  1784. // Check that we found a retain with the same argument.
  1785. if (!Retain || !IsRetain(GetBasicARCInstKind(Retain)) ||
  1786. GetArgRCIdentityRoot(Retain) != Arg) {
  1787. return nullptr;
  1788. }
  1789. return Retain;
  1790. }
  1791. /// Look for an ``autorelease'' instruction dependent on Arg such that there are
  1792. /// no instructions dependent on Arg that need a positive ref count in between
  1793. /// the autorelease and the ret.
  1794. static CallInst *
  1795. FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB,
  1796. ReturnInst *Ret,
  1797. SmallPtrSetImpl<Instruction *> &DepInsts,
  1798. SmallPtrSetImpl<const BasicBlock *> &V,
  1799. ProvenanceAnalysis &PA) {
  1800. FindDependencies(NeedsPositiveRetainCount, Arg,
  1801. BB, Ret, DepInsts, V, PA);
  1802. if (DepInsts.size() != 1)
  1803. return nullptr;
  1804. auto *Autorelease = dyn_cast_or_null<CallInst>(*DepInsts.begin());
  1805. if (!Autorelease)
  1806. return nullptr;
  1807. ARCInstKind AutoreleaseClass = GetBasicARCInstKind(Autorelease);
  1808. if (!IsAutorelease(AutoreleaseClass))
  1809. return nullptr;
  1810. if (GetArgRCIdentityRoot(Autorelease) != Arg)
  1811. return nullptr;
  1812. return Autorelease;
  1813. }
  1814. /// Look for this pattern:
  1815. /// \code
  1816. /// %call = call i8* @something(...)
  1817. /// %2 = call i8* @objc_retain(i8* %call)
  1818. /// %3 = call i8* @objc_autorelease(i8* %2)
  1819. /// ret i8* %3
  1820. /// \endcode
  1821. /// And delete the retain and autorelease.
  1822. void ObjCARCOpt::OptimizeReturns(Function &F) {
  1823. if (!F.getReturnType()->isPointerTy())
  1824. return;
  1825. DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n");
  1826. SmallPtrSet<Instruction *, 4> DependingInstructions;
  1827. SmallPtrSet<const BasicBlock *, 4> Visited;
  1828. for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
  1829. BasicBlock *BB = FI;
  1830. ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
  1831. DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
  1832. if (!Ret)
  1833. continue;
  1834. const Value *Arg = GetRCIdentityRoot(Ret->getOperand(0));
  1835. // Look for an ``autorelease'' instruction that is a predecessor of Ret and
  1836. // dependent on Arg such that there are no instructions dependent on Arg
  1837. // that need a positive ref count in between the autorelease and Ret.
  1838. CallInst *Autorelease =
  1839. FindPredecessorAutoreleaseWithSafePath(Arg, BB, Ret,
  1840. DependingInstructions, Visited,
  1841. PA);
  1842. DependingInstructions.clear();
  1843. Visited.clear();
  1844. if (!Autorelease)
  1845. continue;
  1846. CallInst *Retain =
  1847. FindPredecessorRetainWithSafePath(Arg, BB, Autorelease,
  1848. DependingInstructions, Visited, PA);
  1849. DependingInstructions.clear();
  1850. Visited.clear();
  1851. if (!Retain)
  1852. continue;
  1853. // Check that there is nothing that can affect the reference count
  1854. // between the retain and the call. Note that Retain need not be in BB.
  1855. bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain,
  1856. DependingInstructions,
  1857. Visited, PA);
  1858. DependingInstructions.clear();
  1859. Visited.clear();
  1860. if (!HasSafePathToCall)
  1861. continue;
  1862. // If so, we can zap the retain and autorelease.
  1863. Changed = true;
  1864. ++NumRets;
  1865. DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: "
  1866. << *Autorelease << "\n");
  1867. EraseInstruction(Retain);
  1868. EraseInstruction(Autorelease);
  1869. }
  1870. }
  1871. #ifndef NDEBUG
  1872. void
  1873. ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) {
  1874. llvm::Statistic &NumRetains =
  1875. AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt;
  1876. llvm::Statistic &NumReleases =
  1877. AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt;
  1878. for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
  1879. Instruction *Inst = &*I++;
  1880. switch (GetBasicARCInstKind(Inst)) {
  1881. default:
  1882. break;
  1883. case ARCInstKind::Retain:
  1884. ++NumRetains;
  1885. break;
  1886. case ARCInstKind::Release:
  1887. ++NumReleases;
  1888. break;
  1889. }
  1890. }
  1891. }
  1892. #endif
  1893. bool ObjCARCOpt::doInitialization(Module &M) {
  1894. if (!EnableARCOpts)
  1895. return false;
  1896. // If nothing in the Module uses ARC, don't do anything.
  1897. Run = ModuleHasARC(M);
  1898. if (!Run)
  1899. return false;
  1900. // Intuitively, objc_retain and others are nocapture, however in practice
  1901. // they are not, because they return their argument value. And objc_release
  1902. // calls finalizers which can have arbitrary side effects.
  1903. MDKindCache.init(&M);
  1904. // Initialize our runtime entry point cache.
  1905. EP.init(&M);
  1906. return false;
  1907. }
  1908. bool ObjCARCOpt::runOnFunction(Function &F) {
  1909. if (!EnableARCOpts)
  1910. return false;
  1911. // If nothing in the Module uses ARC, don't do anything.
  1912. if (!Run)
  1913. return false;
  1914. Changed = false;
  1915. DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>"
  1916. "\n");
  1917. PA.setAA(&getAnalysis<AliasAnalysis>());
  1918. #ifndef NDEBUG
  1919. if (AreStatisticsEnabled()) {
  1920. GatherStatistics(F, false);
  1921. }
  1922. #endif
  1923. // This pass performs several distinct transformations. As a compile-time aid
  1924. // when compiling code that isn't ObjC, skip these if the relevant ObjC
  1925. // library functions aren't declared.
  1926. // Preliminary optimizations. This also computes UsedInThisFunction.
  1927. OptimizeIndividualCalls(F);
  1928. // Optimizations for weak pointers.
  1929. if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::LoadWeak)) |
  1930. (1 << unsigned(ARCInstKind::LoadWeakRetained)) |
  1931. (1 << unsigned(ARCInstKind::StoreWeak)) |
  1932. (1 << unsigned(ARCInstKind::InitWeak)) |
  1933. (1 << unsigned(ARCInstKind::CopyWeak)) |
  1934. (1 << unsigned(ARCInstKind::MoveWeak)) |
  1935. (1 << unsigned(ARCInstKind::DestroyWeak))))
  1936. OptimizeWeakCalls(F);
  1937. // Optimizations for retain+release pairs.
  1938. if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Retain)) |
  1939. (1 << unsigned(ARCInstKind::RetainRV)) |
  1940. (1 << unsigned(ARCInstKind::RetainBlock))))
  1941. if (UsedInThisFunction & (1 << unsigned(ARCInstKind::Release)))
  1942. // Run OptimizeSequences until it either stops making changes or
  1943. // no retain+release pair nesting is detected.
  1944. while (OptimizeSequences(F)) {}
  1945. // Optimizations if objc_autorelease is used.
  1946. if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Autorelease)) |
  1947. (1 << unsigned(ARCInstKind::AutoreleaseRV))))
  1948. OptimizeReturns(F);
  1949. // Gather statistics after optimization.
  1950. #ifndef NDEBUG
  1951. if (AreStatisticsEnabled()) {
  1952. GatherStatistics(F, true);
  1953. }
  1954. #endif
  1955. DEBUG(dbgs() << "\n");
  1956. return Changed;
  1957. }
  1958. void ObjCARCOpt::releaseMemory() {
  1959. PA.clear();
  1960. }
  1961. /// @}
  1962. ///