ARCRuntimeEntryPoints.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===- ARCRuntimeEntryPoints.h - ObjC ARC Optimization ----------*- 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. //
  9. /// \file
  10. /// This file contains a class ARCRuntimeEntryPoints for use in
  11. /// creating/managing references to entry points to the arc objective c runtime.
  12. ///
  13. /// WARNING: This file knows about certain library functions. It recognizes them
  14. /// by name, and hardwires knowledge of their semantics.
  15. ///
  16. /// WARNING: This file knows about how certain Objective-C library functions are
  17. /// used. Naive LLVM IR transformations which would otherwise be
  18. /// behavior-preserving may break these assumptions.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_ARCRUNTIMEENTRYPOINTS_H
  22. #define LLVM_LIB_TRANSFORMS_OBJCARC_ARCRUNTIMEENTRYPOINTS_H
  23. #include "llvm/IR/Attributes.h"
  24. #include "llvm/IR/Intrinsics.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include <cassert>
  27. namespace llvm {
  28. class Function;
  29. class Module;
  30. namespace objcarc {
  31. enum class ARCRuntimeEntryPointKind {
  32. AutoreleaseRV,
  33. Release,
  34. Retain,
  35. RetainBlock,
  36. Autorelease,
  37. StoreStrong,
  38. RetainRV,
  39. UnsafeClaimRV,
  40. RetainAutorelease,
  41. RetainAutoreleaseRV,
  42. };
  43. /// Declarations for ObjC runtime functions and constants. These are initialized
  44. /// lazily to avoid cluttering up the Module with unused declarations.
  45. class ARCRuntimeEntryPoints {
  46. public:
  47. ARCRuntimeEntryPoints() = default;
  48. void init(Module *M) {
  49. TheModule = M;
  50. AutoreleaseRV = nullptr;
  51. Release = nullptr;
  52. Retain = nullptr;
  53. RetainBlock = nullptr;
  54. Autorelease = nullptr;
  55. StoreStrong = nullptr;
  56. RetainRV = nullptr;
  57. UnsafeClaimRV = nullptr;
  58. RetainAutorelease = nullptr;
  59. RetainAutoreleaseRV = nullptr;
  60. }
  61. Function *get(ARCRuntimeEntryPointKind kind) {
  62. assert(TheModule != nullptr && "Not initialized.");
  63. switch (kind) {
  64. case ARCRuntimeEntryPointKind::AutoreleaseRV:
  65. return getIntrinsicEntryPoint(AutoreleaseRV,
  66. Intrinsic::objc_autoreleaseReturnValue);
  67. case ARCRuntimeEntryPointKind::Release:
  68. return getIntrinsicEntryPoint(Release, Intrinsic::objc_release);
  69. case ARCRuntimeEntryPointKind::Retain:
  70. return getIntrinsicEntryPoint(Retain, Intrinsic::objc_retain);
  71. case ARCRuntimeEntryPointKind::RetainBlock:
  72. return getIntrinsicEntryPoint(RetainBlock, Intrinsic::objc_retainBlock);
  73. case ARCRuntimeEntryPointKind::Autorelease:
  74. return getIntrinsicEntryPoint(Autorelease, Intrinsic::objc_autorelease);
  75. case ARCRuntimeEntryPointKind::StoreStrong:
  76. return getIntrinsicEntryPoint(StoreStrong, Intrinsic::objc_storeStrong);
  77. case ARCRuntimeEntryPointKind::RetainRV:
  78. return getIntrinsicEntryPoint(RetainRV,
  79. Intrinsic::objc_retainAutoreleasedReturnValue);
  80. case ARCRuntimeEntryPointKind::UnsafeClaimRV:
  81. return getIntrinsicEntryPoint(
  82. UnsafeClaimRV, Intrinsic::objc_unsafeClaimAutoreleasedReturnValue);
  83. case ARCRuntimeEntryPointKind::RetainAutorelease:
  84. return getIntrinsicEntryPoint(RetainAutorelease,
  85. Intrinsic::objc_retainAutorelease);
  86. case ARCRuntimeEntryPointKind::RetainAutoreleaseRV:
  87. return getIntrinsicEntryPoint(RetainAutoreleaseRV,
  88. Intrinsic::objc_retainAutoreleaseReturnValue);
  89. }
  90. llvm_unreachable("Switch should be a covered switch.");
  91. }
  92. private:
  93. /// Cached reference to the module which we will insert declarations into.
  94. Module *TheModule = nullptr;
  95. /// Declaration for ObjC runtime function objc_autoreleaseReturnValue.
  96. Function *AutoreleaseRV = nullptr;
  97. /// Declaration for ObjC runtime function objc_release.
  98. Function *Release = nullptr;
  99. /// Declaration for ObjC runtime function objc_retain.
  100. Function *Retain = nullptr;
  101. /// Declaration for ObjC runtime function objc_retainBlock.
  102. Function *RetainBlock = nullptr;
  103. /// Declaration for ObjC runtime function objc_autorelease.
  104. Function *Autorelease = nullptr;
  105. /// Declaration for objc_storeStrong().
  106. Function *StoreStrong = nullptr;
  107. /// Declaration for objc_retainAutoreleasedReturnValue().
  108. Function *RetainRV = nullptr;
  109. /// Declaration for objc_unsafeClaimAutoreleasedReturnValue().
  110. Function *UnsafeClaimRV = nullptr;
  111. /// Declaration for objc_retainAutorelease().
  112. Function *RetainAutorelease = nullptr;
  113. /// Declaration for objc_retainAutoreleaseReturnValue().
  114. Function *RetainAutoreleaseRV = nullptr;
  115. Function *getIntrinsicEntryPoint(Function *&Decl, Intrinsic::ID IntID) {
  116. if (Decl)
  117. return Decl;
  118. return Decl = Intrinsic::getDeclaration(TheModule, IntID);
  119. }
  120. };
  121. } // end namespace objcarc
  122. } // end namespace llvm
  123. #endif // LLVM_LIB_TRANSFORMS_OBJCARC_ARCRUNTIMEENTRYPOINTS_H