ObjCARCInstKind.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. //===- ARCInstKind.cpp - ObjC ARC Optimization ----------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. /// This file defines several utility functions used by various ARC
  10. /// optimizations which are IMHO too big to be in a header file.
  11. ///
  12. /// WARNING: This file knows about certain library functions. It recognizes them
  13. /// by name, and hardwires knowledge of their semantics.
  14. ///
  15. /// WARNING: This file knows about how certain Objective-C library functions are
  16. /// used. Naive LLVM IR transformations which would otherwise be
  17. /// behavior-preserving may break these assumptions.
  18. ///
  19. //===----------------------------------------------------------------------===//
  20. #include "llvm/Analysis/ObjCARCInstKind.h"
  21. #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
  22. #include "llvm/IR/Intrinsics.h"
  23. using namespace llvm;
  24. using namespace llvm::objcarc;
  25. raw_ostream &llvm::objcarc::operator<<(raw_ostream &OS,
  26. const ARCInstKind Class) {
  27. switch (Class) {
  28. case ARCInstKind::Retain:
  29. return OS << "ARCInstKind::Retain";
  30. case ARCInstKind::RetainRV:
  31. return OS << "ARCInstKind::RetainRV";
  32. case ARCInstKind::UnsafeClaimRV:
  33. return OS << "ARCInstKind::UnsafeClaimRV";
  34. case ARCInstKind::RetainBlock:
  35. return OS << "ARCInstKind::RetainBlock";
  36. case ARCInstKind::Release:
  37. return OS << "ARCInstKind::Release";
  38. case ARCInstKind::Autorelease:
  39. return OS << "ARCInstKind::Autorelease";
  40. case ARCInstKind::AutoreleaseRV:
  41. return OS << "ARCInstKind::AutoreleaseRV";
  42. case ARCInstKind::AutoreleasepoolPush:
  43. return OS << "ARCInstKind::AutoreleasepoolPush";
  44. case ARCInstKind::AutoreleasepoolPop:
  45. return OS << "ARCInstKind::AutoreleasepoolPop";
  46. case ARCInstKind::NoopCast:
  47. return OS << "ARCInstKind::NoopCast";
  48. case ARCInstKind::FusedRetainAutorelease:
  49. return OS << "ARCInstKind::FusedRetainAutorelease";
  50. case ARCInstKind::FusedRetainAutoreleaseRV:
  51. return OS << "ARCInstKind::FusedRetainAutoreleaseRV";
  52. case ARCInstKind::LoadWeakRetained:
  53. return OS << "ARCInstKind::LoadWeakRetained";
  54. case ARCInstKind::StoreWeak:
  55. return OS << "ARCInstKind::StoreWeak";
  56. case ARCInstKind::InitWeak:
  57. return OS << "ARCInstKind::InitWeak";
  58. case ARCInstKind::LoadWeak:
  59. return OS << "ARCInstKind::LoadWeak";
  60. case ARCInstKind::MoveWeak:
  61. return OS << "ARCInstKind::MoveWeak";
  62. case ARCInstKind::CopyWeak:
  63. return OS << "ARCInstKind::CopyWeak";
  64. case ARCInstKind::DestroyWeak:
  65. return OS << "ARCInstKind::DestroyWeak";
  66. case ARCInstKind::StoreStrong:
  67. return OS << "ARCInstKind::StoreStrong";
  68. case ARCInstKind::CallOrUser:
  69. return OS << "ARCInstKind::CallOrUser";
  70. case ARCInstKind::Call:
  71. return OS << "ARCInstKind::Call";
  72. case ARCInstKind::User:
  73. return OS << "ARCInstKind::User";
  74. case ARCInstKind::IntrinsicUser:
  75. return OS << "ARCInstKind::IntrinsicUser";
  76. case ARCInstKind::None:
  77. return OS << "ARCInstKind::None";
  78. }
  79. llvm_unreachable("Unknown instruction class!");
  80. }
  81. ARCInstKind llvm::objcarc::GetFunctionClass(const Function *F) {
  82. Intrinsic::ID ID = F->getIntrinsicID();
  83. switch (ID) {
  84. default:
  85. return ARCInstKind::CallOrUser;
  86. case Intrinsic::objc_autorelease:
  87. return ARCInstKind::Autorelease;
  88. case Intrinsic::objc_autoreleasePoolPop:
  89. return ARCInstKind::AutoreleasepoolPop;
  90. case Intrinsic::objc_autoreleasePoolPush:
  91. return ARCInstKind::AutoreleasepoolPush;
  92. case Intrinsic::objc_autoreleaseReturnValue:
  93. return ARCInstKind::AutoreleaseRV;
  94. case Intrinsic::objc_copyWeak:
  95. return ARCInstKind::CopyWeak;
  96. case Intrinsic::objc_destroyWeak:
  97. return ARCInstKind::DestroyWeak;
  98. case Intrinsic::objc_initWeak:
  99. return ARCInstKind::InitWeak;
  100. case Intrinsic::objc_loadWeak:
  101. return ARCInstKind::LoadWeak;
  102. case Intrinsic::objc_loadWeakRetained:
  103. return ARCInstKind::LoadWeakRetained;
  104. case Intrinsic::objc_moveWeak:
  105. return ARCInstKind::MoveWeak;
  106. case Intrinsic::objc_release:
  107. return ARCInstKind::Release;
  108. case Intrinsic::objc_retain:
  109. return ARCInstKind::Retain;
  110. case Intrinsic::objc_retainAutorelease:
  111. return ARCInstKind::FusedRetainAutorelease;
  112. case Intrinsic::objc_retainAutoreleaseReturnValue:
  113. return ARCInstKind::FusedRetainAutoreleaseRV;
  114. case Intrinsic::objc_retainAutoreleasedReturnValue:
  115. return ARCInstKind::RetainRV;
  116. case Intrinsic::objc_retainBlock:
  117. return ARCInstKind::RetainBlock;
  118. case Intrinsic::objc_storeStrong:
  119. return ARCInstKind::StoreStrong;
  120. case Intrinsic::objc_storeWeak:
  121. return ARCInstKind::StoreWeak;
  122. case Intrinsic::objc_clang_arc_use:
  123. return ARCInstKind::IntrinsicUser;
  124. case Intrinsic::objc_unsafeClaimAutoreleasedReturnValue:
  125. return ARCInstKind::UnsafeClaimRV;
  126. case Intrinsic::objc_retainedObject:
  127. return ARCInstKind::NoopCast;
  128. case Intrinsic::objc_unretainedObject:
  129. return ARCInstKind::NoopCast;
  130. case Intrinsic::objc_unretainedPointer:
  131. return ARCInstKind::NoopCast;
  132. case Intrinsic::objc_retain_autorelease:
  133. return ARCInstKind::FusedRetainAutorelease;
  134. case Intrinsic::objc_sync_enter:
  135. return ARCInstKind::User;
  136. case Intrinsic::objc_sync_exit:
  137. return ARCInstKind::User;
  138. case Intrinsic::objc_clang_arc_noop_use:
  139. case Intrinsic::objc_arc_annotation_topdown_bbstart:
  140. case Intrinsic::objc_arc_annotation_topdown_bbend:
  141. case Intrinsic::objc_arc_annotation_bottomup_bbstart:
  142. case Intrinsic::objc_arc_annotation_bottomup_bbend:
  143. // Ignore annotation calls. This is important to stop the
  144. // optimizer from treating annotations as uses which would
  145. // make the state of the pointers they are attempting to
  146. // elucidate to be incorrect.
  147. return ARCInstKind::None;
  148. }
  149. }
  150. // A list of intrinsics that we know do not use objc pointers or decrement
  151. // ref counts.
  152. static bool isInertIntrinsic(unsigned ID) {
  153. // TODO: Make this into a covered switch.
  154. switch (ID) {
  155. case Intrinsic::returnaddress:
  156. case Intrinsic::addressofreturnaddress:
  157. case Intrinsic::frameaddress:
  158. case Intrinsic::stacksave:
  159. case Intrinsic::stackrestore:
  160. case Intrinsic::vastart:
  161. case Intrinsic::vacopy:
  162. case Intrinsic::vaend:
  163. case Intrinsic::objectsize:
  164. case Intrinsic::prefetch:
  165. case Intrinsic::stackprotector:
  166. case Intrinsic::eh_return_i32:
  167. case Intrinsic::eh_return_i64:
  168. case Intrinsic::eh_typeid_for:
  169. case Intrinsic::eh_dwarf_cfa:
  170. case Intrinsic::eh_sjlj_lsda:
  171. case Intrinsic::eh_sjlj_functioncontext:
  172. case Intrinsic::init_trampoline:
  173. case Intrinsic::adjust_trampoline:
  174. case Intrinsic::lifetime_start:
  175. case Intrinsic::lifetime_end:
  176. case Intrinsic::invariant_start:
  177. case Intrinsic::invariant_end:
  178. // Don't let dbg info affect our results.
  179. case Intrinsic::dbg_declare:
  180. case Intrinsic::dbg_value:
  181. case Intrinsic::dbg_label:
  182. // Short cut: Some intrinsics obviously don't use ObjC pointers.
  183. return true;
  184. default:
  185. return false;
  186. }
  187. }
  188. // A list of intrinsics that we know do not use objc pointers or decrement
  189. // ref counts.
  190. static bool isUseOnlyIntrinsic(unsigned ID) {
  191. // We are conservative and even though intrinsics are unlikely to touch
  192. // reference counts, we white list them for safety.
  193. //
  194. // TODO: Expand this into a covered switch. There is a lot more here.
  195. switch (ID) {
  196. case Intrinsic::memcpy:
  197. case Intrinsic::memmove:
  198. case Intrinsic::memset:
  199. return true;
  200. default:
  201. return false;
  202. }
  203. }
  204. /// Determine what kind of construct V is.
  205. ARCInstKind llvm::objcarc::GetARCInstKind(const Value *V) {
  206. if (const Instruction *I = dyn_cast<Instruction>(V)) {
  207. // Any instruction other than bitcast and gep with a pointer operand have a
  208. // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
  209. // to a subsequent use, rather than using it themselves, in this sense.
  210. // As a short cut, several other opcodes are known to have no pointer
  211. // operands of interest. And ret is never followed by a release, so it's
  212. // not interesting to examine.
  213. switch (I->getOpcode()) {
  214. case Instruction::Call: {
  215. const CallInst *CI = cast<CallInst>(I);
  216. // See if we have a function that we know something about.
  217. if (const Function *F = CI->getCalledFunction()) {
  218. ARCInstKind Class = GetFunctionClass(F);
  219. if (Class != ARCInstKind::CallOrUser)
  220. return Class;
  221. Intrinsic::ID ID = F->getIntrinsicID();
  222. if (isInertIntrinsic(ID))
  223. return ARCInstKind::None;
  224. if (isUseOnlyIntrinsic(ID))
  225. return ARCInstKind::User;
  226. }
  227. // Otherwise, be conservative.
  228. return GetCallSiteClass(*CI);
  229. }
  230. case Instruction::Invoke:
  231. // Otherwise, be conservative.
  232. return GetCallSiteClass(cast<InvokeInst>(*I));
  233. case Instruction::BitCast:
  234. case Instruction::GetElementPtr:
  235. case Instruction::Select:
  236. case Instruction::PHI:
  237. case Instruction::Ret:
  238. case Instruction::Br:
  239. case Instruction::Switch:
  240. case Instruction::IndirectBr:
  241. case Instruction::Alloca:
  242. case Instruction::VAArg:
  243. case Instruction::Add:
  244. case Instruction::FAdd:
  245. case Instruction::Sub:
  246. case Instruction::FSub:
  247. case Instruction::Mul:
  248. case Instruction::FMul:
  249. case Instruction::SDiv:
  250. case Instruction::UDiv:
  251. case Instruction::FDiv:
  252. case Instruction::SRem:
  253. case Instruction::URem:
  254. case Instruction::FRem:
  255. case Instruction::Shl:
  256. case Instruction::LShr:
  257. case Instruction::AShr:
  258. case Instruction::And:
  259. case Instruction::Or:
  260. case Instruction::Xor:
  261. case Instruction::SExt:
  262. case Instruction::ZExt:
  263. case Instruction::Trunc:
  264. case Instruction::IntToPtr:
  265. case Instruction::FCmp:
  266. case Instruction::FPTrunc:
  267. case Instruction::FPExt:
  268. case Instruction::FPToUI:
  269. case Instruction::FPToSI:
  270. case Instruction::UIToFP:
  271. case Instruction::SIToFP:
  272. case Instruction::InsertElement:
  273. case Instruction::ExtractElement:
  274. case Instruction::ShuffleVector:
  275. case Instruction::ExtractValue:
  276. break;
  277. case Instruction::ICmp:
  278. // Comparing a pointer with null, or any other constant, isn't an
  279. // interesting use, because we don't care what the pointer points to, or
  280. // about the values of any other dynamic reference-counted pointers.
  281. if (IsPotentialRetainableObjPtr(I->getOperand(1)))
  282. return ARCInstKind::User;
  283. break;
  284. default:
  285. // For anything else, check all the operands.
  286. // Note that this includes both operands of a Store: while the first
  287. // operand isn't actually being dereferenced, it is being stored to
  288. // memory where we can no longer track who might read it and dereference
  289. // it, so we have to consider it potentially used.
  290. for (const Use &U : I->operands())
  291. if (IsPotentialRetainableObjPtr(U))
  292. return ARCInstKind::User;
  293. }
  294. }
  295. // Otherwise, it's totally inert for ARC purposes.
  296. return ARCInstKind::None;
  297. }
  298. /// Test if the given class is a kind of user.
  299. bool llvm::objcarc::IsUser(ARCInstKind Class) {
  300. switch (Class) {
  301. case ARCInstKind::User:
  302. case ARCInstKind::CallOrUser:
  303. case ARCInstKind::IntrinsicUser:
  304. return true;
  305. case ARCInstKind::Retain:
  306. case ARCInstKind::RetainRV:
  307. case ARCInstKind::RetainBlock:
  308. case ARCInstKind::Release:
  309. case ARCInstKind::Autorelease:
  310. case ARCInstKind::AutoreleaseRV:
  311. case ARCInstKind::AutoreleasepoolPush:
  312. case ARCInstKind::AutoreleasepoolPop:
  313. case ARCInstKind::NoopCast:
  314. case ARCInstKind::FusedRetainAutorelease:
  315. case ARCInstKind::FusedRetainAutoreleaseRV:
  316. case ARCInstKind::LoadWeakRetained:
  317. case ARCInstKind::StoreWeak:
  318. case ARCInstKind::InitWeak:
  319. case ARCInstKind::LoadWeak:
  320. case ARCInstKind::MoveWeak:
  321. case ARCInstKind::CopyWeak:
  322. case ARCInstKind::DestroyWeak:
  323. case ARCInstKind::StoreStrong:
  324. case ARCInstKind::Call:
  325. case ARCInstKind::None:
  326. case ARCInstKind::UnsafeClaimRV:
  327. return false;
  328. }
  329. llvm_unreachable("covered switch isn't covered?");
  330. }
  331. /// Test if the given class is objc_retain or equivalent.
  332. bool llvm::objcarc::IsRetain(ARCInstKind Class) {
  333. switch (Class) {
  334. case ARCInstKind::Retain:
  335. case ARCInstKind::RetainRV:
  336. return true;
  337. // I believe we treat retain block as not a retain since it can copy its
  338. // block.
  339. case ARCInstKind::RetainBlock:
  340. case ARCInstKind::Release:
  341. case ARCInstKind::Autorelease:
  342. case ARCInstKind::AutoreleaseRV:
  343. case ARCInstKind::AutoreleasepoolPush:
  344. case ARCInstKind::AutoreleasepoolPop:
  345. case ARCInstKind::NoopCast:
  346. case ARCInstKind::FusedRetainAutorelease:
  347. case ARCInstKind::FusedRetainAutoreleaseRV:
  348. case ARCInstKind::LoadWeakRetained:
  349. case ARCInstKind::StoreWeak:
  350. case ARCInstKind::InitWeak:
  351. case ARCInstKind::LoadWeak:
  352. case ARCInstKind::MoveWeak:
  353. case ARCInstKind::CopyWeak:
  354. case ARCInstKind::DestroyWeak:
  355. case ARCInstKind::StoreStrong:
  356. case ARCInstKind::IntrinsicUser:
  357. case ARCInstKind::CallOrUser:
  358. case ARCInstKind::Call:
  359. case ARCInstKind::User:
  360. case ARCInstKind::None:
  361. case ARCInstKind::UnsafeClaimRV:
  362. return false;
  363. }
  364. llvm_unreachable("covered switch isn't covered?");
  365. }
  366. /// Test if the given class is objc_autorelease or equivalent.
  367. bool llvm::objcarc::IsAutorelease(ARCInstKind Class) {
  368. switch (Class) {
  369. case ARCInstKind::Autorelease:
  370. case ARCInstKind::AutoreleaseRV:
  371. return true;
  372. case ARCInstKind::Retain:
  373. case ARCInstKind::RetainRV:
  374. case ARCInstKind::UnsafeClaimRV:
  375. case ARCInstKind::RetainBlock:
  376. case ARCInstKind::Release:
  377. case ARCInstKind::AutoreleasepoolPush:
  378. case ARCInstKind::AutoreleasepoolPop:
  379. case ARCInstKind::NoopCast:
  380. case ARCInstKind::FusedRetainAutorelease:
  381. case ARCInstKind::FusedRetainAutoreleaseRV:
  382. case ARCInstKind::LoadWeakRetained:
  383. case ARCInstKind::StoreWeak:
  384. case ARCInstKind::InitWeak:
  385. case ARCInstKind::LoadWeak:
  386. case ARCInstKind::MoveWeak:
  387. case ARCInstKind::CopyWeak:
  388. case ARCInstKind::DestroyWeak:
  389. case ARCInstKind::StoreStrong:
  390. case ARCInstKind::IntrinsicUser:
  391. case ARCInstKind::CallOrUser:
  392. case ARCInstKind::Call:
  393. case ARCInstKind::User:
  394. case ARCInstKind::None:
  395. return false;
  396. }
  397. llvm_unreachable("covered switch isn't covered?");
  398. }
  399. /// Test if the given class represents instructions which return their
  400. /// argument verbatim.
  401. bool llvm::objcarc::IsForwarding(ARCInstKind Class) {
  402. switch (Class) {
  403. case ARCInstKind::Retain:
  404. case ARCInstKind::RetainRV:
  405. case ARCInstKind::UnsafeClaimRV:
  406. case ARCInstKind::Autorelease:
  407. case ARCInstKind::AutoreleaseRV:
  408. case ARCInstKind::NoopCast:
  409. return true;
  410. case ARCInstKind::RetainBlock:
  411. case ARCInstKind::Release:
  412. case ARCInstKind::AutoreleasepoolPush:
  413. case ARCInstKind::AutoreleasepoolPop:
  414. case ARCInstKind::FusedRetainAutorelease:
  415. case ARCInstKind::FusedRetainAutoreleaseRV:
  416. case ARCInstKind::LoadWeakRetained:
  417. case ARCInstKind::StoreWeak:
  418. case ARCInstKind::InitWeak:
  419. case ARCInstKind::LoadWeak:
  420. case ARCInstKind::MoveWeak:
  421. case ARCInstKind::CopyWeak:
  422. case ARCInstKind::DestroyWeak:
  423. case ARCInstKind::StoreStrong:
  424. case ARCInstKind::IntrinsicUser:
  425. case ARCInstKind::CallOrUser:
  426. case ARCInstKind::Call:
  427. case ARCInstKind::User:
  428. case ARCInstKind::None:
  429. return false;
  430. }
  431. llvm_unreachable("covered switch isn't covered?");
  432. }
  433. /// Test if the given class represents instructions which do nothing if
  434. /// passed a null pointer.
  435. bool llvm::objcarc::IsNoopOnNull(ARCInstKind Class) {
  436. switch (Class) {
  437. case ARCInstKind::Retain:
  438. case ARCInstKind::RetainRV:
  439. case ARCInstKind::UnsafeClaimRV:
  440. case ARCInstKind::Release:
  441. case ARCInstKind::Autorelease:
  442. case ARCInstKind::AutoreleaseRV:
  443. case ARCInstKind::RetainBlock:
  444. return true;
  445. case ARCInstKind::AutoreleasepoolPush:
  446. case ARCInstKind::AutoreleasepoolPop:
  447. case ARCInstKind::FusedRetainAutorelease:
  448. case ARCInstKind::FusedRetainAutoreleaseRV:
  449. case ARCInstKind::LoadWeakRetained:
  450. case ARCInstKind::StoreWeak:
  451. case ARCInstKind::InitWeak:
  452. case ARCInstKind::LoadWeak:
  453. case ARCInstKind::MoveWeak:
  454. case ARCInstKind::CopyWeak:
  455. case ARCInstKind::DestroyWeak:
  456. case ARCInstKind::StoreStrong:
  457. case ARCInstKind::IntrinsicUser:
  458. case ARCInstKind::CallOrUser:
  459. case ARCInstKind::Call:
  460. case ARCInstKind::User:
  461. case ARCInstKind::None:
  462. case ARCInstKind::NoopCast:
  463. return false;
  464. }
  465. llvm_unreachable("covered switch isn't covered?");
  466. }
  467. /// Test if the given class represents instructions which do nothing if
  468. /// passed a global variable.
  469. bool llvm::objcarc::IsNoopOnGlobal(ARCInstKind Class) {
  470. switch (Class) {
  471. case ARCInstKind::Retain:
  472. case ARCInstKind::RetainRV:
  473. case ARCInstKind::UnsafeClaimRV:
  474. case ARCInstKind::Release:
  475. case ARCInstKind::Autorelease:
  476. case ARCInstKind::AutoreleaseRV:
  477. case ARCInstKind::RetainBlock:
  478. case ARCInstKind::FusedRetainAutorelease:
  479. case ARCInstKind::FusedRetainAutoreleaseRV:
  480. return true;
  481. case ARCInstKind::AutoreleasepoolPush:
  482. case ARCInstKind::AutoreleasepoolPop:
  483. case ARCInstKind::LoadWeakRetained:
  484. case ARCInstKind::StoreWeak:
  485. case ARCInstKind::InitWeak:
  486. case ARCInstKind::LoadWeak:
  487. case ARCInstKind::MoveWeak:
  488. case ARCInstKind::CopyWeak:
  489. case ARCInstKind::DestroyWeak:
  490. case ARCInstKind::StoreStrong:
  491. case ARCInstKind::IntrinsicUser:
  492. case ARCInstKind::CallOrUser:
  493. case ARCInstKind::Call:
  494. case ARCInstKind::User:
  495. case ARCInstKind::None:
  496. case ARCInstKind::NoopCast:
  497. return false;
  498. }
  499. llvm_unreachable("covered switch isn't covered?");
  500. }
  501. /// Test if the given class represents instructions which are always safe
  502. /// to mark with the "tail" keyword.
  503. bool llvm::objcarc::IsAlwaysTail(ARCInstKind Class) {
  504. // ARCInstKind::RetainBlock may be given a stack argument.
  505. switch (Class) {
  506. case ARCInstKind::Retain:
  507. case ARCInstKind::RetainRV:
  508. case ARCInstKind::UnsafeClaimRV:
  509. case ARCInstKind::AutoreleaseRV:
  510. return true;
  511. case ARCInstKind::Release:
  512. case ARCInstKind::Autorelease:
  513. case ARCInstKind::RetainBlock:
  514. case ARCInstKind::AutoreleasepoolPush:
  515. case ARCInstKind::AutoreleasepoolPop:
  516. case ARCInstKind::FusedRetainAutorelease:
  517. case ARCInstKind::FusedRetainAutoreleaseRV:
  518. case ARCInstKind::LoadWeakRetained:
  519. case ARCInstKind::StoreWeak:
  520. case ARCInstKind::InitWeak:
  521. case ARCInstKind::LoadWeak:
  522. case ARCInstKind::MoveWeak:
  523. case ARCInstKind::CopyWeak:
  524. case ARCInstKind::DestroyWeak:
  525. case ARCInstKind::StoreStrong:
  526. case ARCInstKind::IntrinsicUser:
  527. case ARCInstKind::CallOrUser:
  528. case ARCInstKind::Call:
  529. case ARCInstKind::User:
  530. case ARCInstKind::None:
  531. case ARCInstKind::NoopCast:
  532. return false;
  533. }
  534. llvm_unreachable("covered switch isn't covered?");
  535. }
  536. /// Test if the given class represents instructions which are never safe
  537. /// to mark with the "tail" keyword.
  538. bool llvm::objcarc::IsNeverTail(ARCInstKind Class) {
  539. /// It is never safe to tail call objc_autorelease since by tail calling
  540. /// objc_autorelease: fast autoreleasing causing our object to be potentially
  541. /// reclaimed from the autorelease pool which violates the semantics of
  542. /// __autoreleasing types in ARC.
  543. switch (Class) {
  544. case ARCInstKind::Autorelease:
  545. return true;
  546. case ARCInstKind::Retain:
  547. case ARCInstKind::RetainRV:
  548. case ARCInstKind::UnsafeClaimRV:
  549. case ARCInstKind::AutoreleaseRV:
  550. case ARCInstKind::Release:
  551. case ARCInstKind::RetainBlock:
  552. case ARCInstKind::AutoreleasepoolPush:
  553. case ARCInstKind::AutoreleasepoolPop:
  554. case ARCInstKind::FusedRetainAutorelease:
  555. case ARCInstKind::FusedRetainAutoreleaseRV:
  556. case ARCInstKind::LoadWeakRetained:
  557. case ARCInstKind::StoreWeak:
  558. case ARCInstKind::InitWeak:
  559. case ARCInstKind::LoadWeak:
  560. case ARCInstKind::MoveWeak:
  561. case ARCInstKind::CopyWeak:
  562. case ARCInstKind::DestroyWeak:
  563. case ARCInstKind::StoreStrong:
  564. case ARCInstKind::IntrinsicUser:
  565. case ARCInstKind::CallOrUser:
  566. case ARCInstKind::Call:
  567. case ARCInstKind::User:
  568. case ARCInstKind::None:
  569. case ARCInstKind::NoopCast:
  570. return false;
  571. }
  572. llvm_unreachable("covered switch isn't covered?");
  573. }
  574. /// Test if the given class represents instructions which are always safe
  575. /// to mark with the nounwind attribute.
  576. bool llvm::objcarc::IsNoThrow(ARCInstKind Class) {
  577. // objc_retainBlock is not nounwind because it calls user copy constructors
  578. // which could theoretically throw.
  579. switch (Class) {
  580. case ARCInstKind::Retain:
  581. case ARCInstKind::RetainRV:
  582. case ARCInstKind::UnsafeClaimRV:
  583. case ARCInstKind::Release:
  584. case ARCInstKind::Autorelease:
  585. case ARCInstKind::AutoreleaseRV:
  586. case ARCInstKind::AutoreleasepoolPush:
  587. case ARCInstKind::AutoreleasepoolPop:
  588. return true;
  589. case ARCInstKind::RetainBlock:
  590. case ARCInstKind::FusedRetainAutorelease:
  591. case ARCInstKind::FusedRetainAutoreleaseRV:
  592. case ARCInstKind::LoadWeakRetained:
  593. case ARCInstKind::StoreWeak:
  594. case ARCInstKind::InitWeak:
  595. case ARCInstKind::LoadWeak:
  596. case ARCInstKind::MoveWeak:
  597. case ARCInstKind::CopyWeak:
  598. case ARCInstKind::DestroyWeak:
  599. case ARCInstKind::StoreStrong:
  600. case ARCInstKind::IntrinsicUser:
  601. case ARCInstKind::CallOrUser:
  602. case ARCInstKind::Call:
  603. case ARCInstKind::User:
  604. case ARCInstKind::None:
  605. case ARCInstKind::NoopCast:
  606. return false;
  607. }
  608. llvm_unreachable("covered switch isn't covered?");
  609. }
  610. /// Test whether the given instruction can autorelease any pointer or cause an
  611. /// autoreleasepool pop.
  612. ///
  613. /// This means that it *could* interrupt the RV optimization.
  614. bool llvm::objcarc::CanInterruptRV(ARCInstKind Class) {
  615. switch (Class) {
  616. case ARCInstKind::AutoreleasepoolPop:
  617. case ARCInstKind::CallOrUser:
  618. case ARCInstKind::Call:
  619. case ARCInstKind::Autorelease:
  620. case ARCInstKind::AutoreleaseRV:
  621. case ARCInstKind::FusedRetainAutorelease:
  622. case ARCInstKind::FusedRetainAutoreleaseRV:
  623. return true;
  624. case ARCInstKind::Retain:
  625. case ARCInstKind::RetainRV:
  626. case ARCInstKind::UnsafeClaimRV:
  627. case ARCInstKind::Release:
  628. case ARCInstKind::AutoreleasepoolPush:
  629. case ARCInstKind::RetainBlock:
  630. case ARCInstKind::LoadWeakRetained:
  631. case ARCInstKind::StoreWeak:
  632. case ARCInstKind::InitWeak:
  633. case ARCInstKind::LoadWeak:
  634. case ARCInstKind::MoveWeak:
  635. case ARCInstKind::CopyWeak:
  636. case ARCInstKind::DestroyWeak:
  637. case ARCInstKind::StoreStrong:
  638. case ARCInstKind::IntrinsicUser:
  639. case ARCInstKind::User:
  640. case ARCInstKind::None:
  641. case ARCInstKind::NoopCast:
  642. return false;
  643. }
  644. llvm_unreachable("covered switch isn't covered?");
  645. }
  646. bool llvm::objcarc::CanDecrementRefCount(ARCInstKind Kind) {
  647. switch (Kind) {
  648. case ARCInstKind::Retain:
  649. case ARCInstKind::RetainRV:
  650. case ARCInstKind::Autorelease:
  651. case ARCInstKind::AutoreleaseRV:
  652. case ARCInstKind::NoopCast:
  653. case ARCInstKind::FusedRetainAutorelease:
  654. case ARCInstKind::FusedRetainAutoreleaseRV:
  655. case ARCInstKind::IntrinsicUser:
  656. case ARCInstKind::User:
  657. case ARCInstKind::None:
  658. return false;
  659. // The cases below are conservative.
  660. // RetainBlock can result in user defined copy constructors being called
  661. // implying releases may occur.
  662. case ARCInstKind::RetainBlock:
  663. case ARCInstKind::Release:
  664. case ARCInstKind::AutoreleasepoolPush:
  665. case ARCInstKind::AutoreleasepoolPop:
  666. case ARCInstKind::LoadWeakRetained:
  667. case ARCInstKind::StoreWeak:
  668. case ARCInstKind::InitWeak:
  669. case ARCInstKind::LoadWeak:
  670. case ARCInstKind::MoveWeak:
  671. case ARCInstKind::CopyWeak:
  672. case ARCInstKind::DestroyWeak:
  673. case ARCInstKind::StoreStrong:
  674. case ARCInstKind::CallOrUser:
  675. case ARCInstKind::Call:
  676. case ARCInstKind::UnsafeClaimRV:
  677. return true;
  678. }
  679. llvm_unreachable("covered switch isn't covered?");
  680. }