ObjCARCExpand.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //===- ObjCARCExpand.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 ObjC ARC optimizations. ARC stands for Automatic
  10. /// Reference Counting and is a system for managing reference counts for objects
  11. /// in Objective C.
  12. ///
  13. /// This specific file deals with early optimizations which perform certain
  14. /// cleanup operations.
  15. ///
  16. /// WARNING: This file knows about certain library functions. It recognizes them
  17. /// by name, and hardwires knowledge of their semantics.
  18. ///
  19. /// WARNING: This file knows about how certain Objective-C library functions are
  20. /// used. Naive LLVM IR transformations which would otherwise be
  21. /// behavior-preserving may break these assumptions.
  22. ///
  23. //===----------------------------------------------------------------------===//
  24. #include "ObjCARC.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/InstIterator.h"
  27. #include "llvm/IR/Instruction.h"
  28. #include "llvm/IR/Instructions.h"
  29. #include "llvm/IR/PassManager.h"
  30. #include "llvm/IR/Value.h"
  31. #include "llvm/InitializePasses.h"
  32. #include "llvm/Pass.h"
  33. #include "llvm/PassRegistry.h"
  34. #include "llvm/Support/Casting.h"
  35. #include "llvm/Support/Debug.h"
  36. #include "llvm/Support/raw_ostream.h"
  37. #include "llvm/Transforms/ObjCARC.h"
  38. #define DEBUG_TYPE "objc-arc-expand"
  39. using namespace llvm;
  40. using namespace llvm::objcarc;
  41. namespace {
  42. static bool runImpl(Function &F) {
  43. if (!EnableARCOpts)
  44. return false;
  45. // If nothing in the Module uses ARC, don't do anything.
  46. if (!ModuleHasARC(*F.getParent()))
  47. return false;
  48. bool Changed = false;
  49. LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName()
  50. << "\n");
  51. for (Instruction &Inst : instructions(&F)) {
  52. LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << Inst << "\n");
  53. switch (GetBasicARCInstKind(&Inst)) {
  54. case ARCInstKind::Retain:
  55. case ARCInstKind::RetainRV:
  56. case ARCInstKind::Autorelease:
  57. case ARCInstKind::AutoreleaseRV:
  58. case ARCInstKind::FusedRetainAutorelease:
  59. case ARCInstKind::FusedRetainAutoreleaseRV: {
  60. // These calls return their argument verbatim, as a low-level
  61. // optimization. However, this makes high-level optimizations
  62. // harder. Undo any uses of this optimization that the front-end
  63. // emitted here. We'll redo them in the contract pass.
  64. Changed = true;
  65. Value *Value = cast<CallInst>(&Inst)->getArgOperand(0);
  66. LLVM_DEBUG(dbgs() << "ObjCARCExpand: Old = " << Inst
  67. << "\n"
  68. " New = "
  69. << *Value << "\n");
  70. Inst.replaceAllUsesWith(Value);
  71. break;
  72. }
  73. default:
  74. break;
  75. }
  76. }
  77. LLVM_DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
  78. return Changed;
  79. }
  80. /// Early ARC transformations.
  81. class ObjCARCExpand : public FunctionPass {
  82. void getAnalysisUsage(AnalysisUsage &AU) const override;
  83. bool runOnFunction(Function &F) override;
  84. public:
  85. static char ID;
  86. ObjCARCExpand() : FunctionPass(ID) {
  87. initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
  88. }
  89. };
  90. } // namespace
  91. char ObjCARCExpand::ID = 0;
  92. INITIALIZE_PASS(ObjCARCExpand, "objc-arc-expand", "ObjC ARC expansion", false,
  93. false)
  94. Pass *llvm::createObjCARCExpandPass() { return new ObjCARCExpand(); }
  95. void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
  96. AU.setPreservesCFG();
  97. }
  98. bool ObjCARCExpand::runOnFunction(Function &F) { return runImpl(F); }
  99. PreservedAnalyses ObjCARCExpandPass::run(Function &F,
  100. FunctionAnalysisManager &AM) {
  101. if (!runImpl(F))
  102. return PreservedAnalyses::all();
  103. PreservedAnalyses PA;
  104. PA.preserveSet<CFGAnalyses>();
  105. return PA;
  106. }