ObjCARCUtil.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ObjCARCUtil.h - ObjC ARC Utility Functions ---------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. /// This file defines ARC utility functions which are used by various parts of
  15. /// the compiler.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ANALYSIS_OBJCARCUTIL_H
  19. #define LLVM_ANALYSIS_OBJCARCUTIL_H
  20. #include "llvm/Analysis/ObjCARCInstKind.h"
  21. #include "llvm/IR/Function.h"
  22. #include "llvm/IR/InstrTypes.h"
  23. #include "llvm/IR/LLVMContext.h"
  24. namespace llvm {
  25. namespace objcarc {
  26. inline const char *getRVMarkerModuleFlagStr() {
  27. return "clang.arc.retainAutoreleasedReturnValueMarker";
  28. }
  29. inline bool hasAttachedCallOpBundle(const CallBase *CB) {
  30. // Ignore the bundle if the return type is void. Global optimization passes
  31. // can turn the called function's return type to void. That should happen only
  32. // if the call doesn't return and the call to @llvm.objc.clang.arc.noop.use
  33. // no longer consumes the function return or is deleted. In that case, it's
  34. // not necessary to emit the marker instruction or calls to the ARC runtime
  35. // functions.
  36. return !CB->getFunctionType()->getReturnType()->isVoidTy() &&
  37. CB->getOperandBundle(LLVMContext::OB_clang_arc_attachedcall)
  38. .has_value();
  39. }
  40. /// This function returns operand bundle clang_arc_attachedcall's argument,
  41. /// which is the address of the ARC runtime function.
  42. inline std::optional<Function *> getAttachedARCFunction(const CallBase *CB) {
  43. auto B = CB->getOperandBundle(LLVMContext::OB_clang_arc_attachedcall);
  44. if (!B)
  45. return std::nullopt;
  46. return cast<Function>(B->Inputs[0]);
  47. }
  48. /// Check whether the function is retainRV/unsafeClaimRV.
  49. inline bool isRetainOrClaimRV(ARCInstKind Kind) {
  50. return Kind == ARCInstKind::RetainRV || Kind == ARCInstKind::UnsafeClaimRV;
  51. }
  52. /// This function returns the ARCInstKind of the function attached to operand
  53. /// bundle clang_arc_attachedcall. It returns std::nullopt if the call doesn't
  54. /// have the operand bundle or the operand is null. Otherwise it returns either
  55. /// RetainRV or UnsafeClaimRV.
  56. inline ARCInstKind getAttachedARCFunctionKind(const CallBase *CB) {
  57. std::optional<Function *> Fn = getAttachedARCFunction(CB);
  58. if (!Fn)
  59. return ARCInstKind::None;
  60. auto FnClass = GetFunctionClass(*Fn);
  61. assert(isRetainOrClaimRV(FnClass) && "unexpected ARC runtime function");
  62. return FnClass;
  63. }
  64. } // end namespace objcarc
  65. } // end namespace llvm
  66. #endif
  67. #ifdef __GNUC__
  68. #pragma GCC diagnostic pop
  69. #endif