ARCRuntimeEntryPoints.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. RetainAutorelease,
  40. RetainAutoreleaseRV,
  41. };
  42. /// Declarations for ObjC runtime functions and constants. These are initialized
  43. /// lazily to avoid cluttering up the Module with unused declarations.
  44. class ARCRuntimeEntryPoints {
  45. public:
  46. ARCRuntimeEntryPoints() = default;
  47. void init(Module *M) {
  48. TheModule = M;
  49. AutoreleaseRV = nullptr;
  50. Release = nullptr;
  51. Retain = nullptr;
  52. RetainBlock = nullptr;
  53. Autorelease = nullptr;
  54. StoreStrong = nullptr;
  55. RetainRV = nullptr;
  56. RetainAutorelease = nullptr;
  57. RetainAutoreleaseRV = nullptr;
  58. }
  59. Function *get(ARCRuntimeEntryPointKind kind) {
  60. assert(TheModule != nullptr && "Not initialized.");
  61. switch (kind) {
  62. case ARCRuntimeEntryPointKind::AutoreleaseRV:
  63. return getIntrinsicEntryPoint(AutoreleaseRV,
  64. Intrinsic::objc_autoreleaseReturnValue);
  65. case ARCRuntimeEntryPointKind::Release:
  66. return getIntrinsicEntryPoint(Release, Intrinsic::objc_release);
  67. case ARCRuntimeEntryPointKind::Retain:
  68. return getIntrinsicEntryPoint(Retain, Intrinsic::objc_retain);
  69. case ARCRuntimeEntryPointKind::RetainBlock:
  70. return getIntrinsicEntryPoint(RetainBlock, Intrinsic::objc_retainBlock);
  71. case ARCRuntimeEntryPointKind::Autorelease:
  72. return getIntrinsicEntryPoint(Autorelease, Intrinsic::objc_autorelease);
  73. case ARCRuntimeEntryPointKind::StoreStrong:
  74. return getIntrinsicEntryPoint(StoreStrong, Intrinsic::objc_storeStrong);
  75. case ARCRuntimeEntryPointKind::RetainRV:
  76. return getIntrinsicEntryPoint(RetainRV,
  77. Intrinsic::objc_retainAutoreleasedReturnValue);
  78. case ARCRuntimeEntryPointKind::RetainAutorelease:
  79. return getIntrinsicEntryPoint(RetainAutorelease,
  80. Intrinsic::objc_retainAutorelease);
  81. case ARCRuntimeEntryPointKind::RetainAutoreleaseRV:
  82. return getIntrinsicEntryPoint(RetainAutoreleaseRV,
  83. Intrinsic::objc_retainAutoreleaseReturnValue);
  84. }
  85. llvm_unreachable("Switch should be a covered switch.");
  86. }
  87. private:
  88. /// Cached reference to the module which we will insert declarations into.
  89. Module *TheModule = nullptr;
  90. /// Declaration for ObjC runtime function objc_autoreleaseReturnValue.
  91. Function *AutoreleaseRV = nullptr;
  92. /// Declaration for ObjC runtime function objc_release.
  93. Function *Release = nullptr;
  94. /// Declaration for ObjC runtime function objc_retain.
  95. Function *Retain = nullptr;
  96. /// Declaration for ObjC runtime function objc_retainBlock.
  97. Function *RetainBlock = nullptr;
  98. /// Declaration for ObjC runtime function objc_autorelease.
  99. Function *Autorelease = nullptr;
  100. /// Declaration for objc_storeStrong().
  101. Function *StoreStrong = nullptr;
  102. /// Declaration for objc_retainAutoreleasedReturnValue().
  103. Function *RetainRV = nullptr;
  104. /// Declaration for objc_retainAutorelease().
  105. Function *RetainAutorelease = nullptr;
  106. /// Declaration for objc_retainAutoreleaseReturnValue().
  107. Function *RetainAutoreleaseRV = nullptr;
  108. Function *getIntrinsicEntryPoint(Function *&Decl, Intrinsic::ID IntID) {
  109. if (Decl)
  110. return Decl;
  111. return Decl = Intrinsic::getDeclaration(TheModule, IntID);
  112. }
  113. };
  114. } // end namespace objcarc
  115. } // end namespace llvm
  116. #endif // LLVM_LIB_TRANSFORMS_OBJCARC_ARCRUNTIMEENTRYPOINTS_H