ObjCARCInstKind.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //===- ObjCARCInstKind.h - ARC instruction equivalence classes --*- C++ -*-===//
  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. #ifndef LLVM_ANALYSIS_OBJCARCINSTKIND_H
  9. #define LLVM_ANALYSIS_OBJCARCINSTKIND_H
  10. #include "llvm/IR/Instructions.h"
  11. namespace llvm {
  12. namespace objcarc {
  13. /// \enum ARCInstKind
  14. ///
  15. /// Equivalence classes of instructions in the ARC Model.
  16. ///
  17. /// Since we do not have "instructions" to represent ARC concepts in LLVM IR,
  18. /// we instead operate on equivalence classes of instructions.
  19. ///
  20. /// TODO: This should be split into two enums: a runtime entry point enum
  21. /// (possibly united with the ARCRuntimeEntrypoint class) and an enum that deals
  22. /// with effects of instructions in the ARC model (which would handle the notion
  23. /// of a User or CallOrUser).
  24. enum class ARCInstKind {
  25. Retain, ///< objc_retain
  26. RetainRV, ///< objc_retainAutoreleasedReturnValue
  27. ClaimRV, ///< objc_unsafeClaimAutoreleasedReturnValue
  28. RetainBlock, ///< objc_retainBlock
  29. Release, ///< objc_release
  30. Autorelease, ///< objc_autorelease
  31. AutoreleaseRV, ///< objc_autoreleaseReturnValue
  32. AutoreleasepoolPush, ///< objc_autoreleasePoolPush
  33. AutoreleasepoolPop, ///< objc_autoreleasePoolPop
  34. NoopCast, ///< objc_retainedObject, etc.
  35. FusedRetainAutorelease, ///< objc_retainAutorelease
  36. FusedRetainAutoreleaseRV, ///< objc_retainAutoreleaseReturnValue
  37. LoadWeakRetained, ///< objc_loadWeakRetained (primitive)
  38. StoreWeak, ///< objc_storeWeak (primitive)
  39. InitWeak, ///< objc_initWeak (derived)
  40. LoadWeak, ///< objc_loadWeak (derived)
  41. MoveWeak, ///< objc_moveWeak (derived)
  42. CopyWeak, ///< objc_copyWeak (derived)
  43. DestroyWeak, ///< objc_destroyWeak (derived)
  44. StoreStrong, ///< objc_storeStrong (derived)
  45. IntrinsicUser, ///< llvm.objc.clang.arc.use
  46. CallOrUser, ///< could call objc_release and/or "use" pointers
  47. Call, ///< could call objc_release
  48. User, ///< could "use" a pointer
  49. None ///< anything that is inert from an ARC perspective.
  50. };
  51. raw_ostream &operator<<(raw_ostream &OS, const ARCInstKind Class);
  52. /// Test if the given class is a kind of user.
  53. bool IsUser(ARCInstKind Class);
  54. /// Test if the given class is objc_retain or equivalent.
  55. bool IsRetain(ARCInstKind Class);
  56. /// Test if the given class is objc_autorelease or equivalent.
  57. bool IsAutorelease(ARCInstKind Class);
  58. /// Test if the given class represents instructions which return their
  59. /// argument verbatim.
  60. bool IsForwarding(ARCInstKind Class);
  61. /// Test if the given class represents instructions which do nothing if
  62. /// passed a null pointer.
  63. bool IsNoopOnNull(ARCInstKind Class);
  64. /// Test if the given class represents instructions which do nothing if
  65. /// passed a global variable.
  66. bool IsNoopOnGlobal(ARCInstKind Class);
  67. /// Test if the given class represents instructions which are always safe
  68. /// to mark with the "tail" keyword.
  69. bool IsAlwaysTail(ARCInstKind Class);
  70. /// Test if the given class represents instructions which are never safe
  71. /// to mark with the "tail" keyword.
  72. bool IsNeverTail(ARCInstKind Class);
  73. /// Test if the given class represents instructions which are always safe
  74. /// to mark with the nounwind attribute.
  75. bool IsNoThrow(ARCInstKind Class);
  76. /// Test whether the given instruction can autorelease any pointer or cause an
  77. /// autoreleasepool pop.
  78. bool CanInterruptRV(ARCInstKind Class);
  79. /// Determine if F is one of the special known Functions. If it isn't,
  80. /// return ARCInstKind::CallOrUser.
  81. ARCInstKind GetFunctionClass(const Function *F);
  82. /// Determine which objc runtime call instruction class V belongs to.
  83. ///
  84. /// This is similar to GetARCInstKind except that it only detects objc
  85. /// runtime calls. This allows it to be faster.
  86. ///
  87. inline ARCInstKind GetBasicARCInstKind(const Value *V) {
  88. if (const CallInst *CI = dyn_cast<CallInst>(V)) {
  89. if (const Function *F = CI->getCalledFunction())
  90. return GetFunctionClass(F);
  91. // Otherwise, be conservative.
  92. return ARCInstKind::CallOrUser;
  93. }
  94. // Otherwise, be conservative.
  95. return isa<InvokeInst>(V) ? ARCInstKind::CallOrUser : ARCInstKind::User;
  96. }
  97. /// Map V to its ARCInstKind equivalence class.
  98. ARCInstKind GetARCInstKind(const Value *V);
  99. /// Returns false if conservatively we can prove that any instruction mapped to
  100. /// this kind can not decrement ref counts. Returns true otherwise.
  101. bool CanDecrementRefCount(ARCInstKind Kind);
  102. } // end namespace objcarc
  103. } // end namespace llvm
  104. #endif