ObjCARCAPElim.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //===- ObjCARCAPElim.cpp - ObjC ARC Optimization --------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. /// \file
  10. ///
  11. /// This file defines ObjC ARC optimizations. ARC stands for Automatic
  12. /// Reference Counting and is a system for managing reference counts for objects
  13. /// in Objective C.
  14. ///
  15. /// This specific file implements optimizations which remove extraneous
  16. /// autorelease pools.
  17. ///
  18. /// WARNING: This file knows about certain library functions. It recognizes them
  19. /// by name, and hardwires knowledge of their semantics.
  20. ///
  21. /// WARNING: This file knows about how certain Objective-C library functions are
  22. /// used. Naive LLVM IR transformations which would otherwise be
  23. /// behavior-preserving may break these assumptions.
  24. ///
  25. //===----------------------------------------------------------------------===//
  26. #include "ObjCARC.h"
  27. #include "llvm/ADT/STLExtras.h"
  28. #include "llvm/IR/Constants.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. using namespace llvm;
  32. using namespace llvm::objcarc;
  33. #define DEBUG_TYPE "objc-arc-ap-elim"
  34. namespace {
  35. /// \brief Autorelease pool elimination.
  36. class ObjCARCAPElim : public ModulePass {
  37. void getAnalysisUsage(AnalysisUsage &AU) const override;
  38. bool runOnModule(Module &M) override;
  39. static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0);
  40. static bool OptimizeBB(BasicBlock *BB);
  41. public:
  42. static char ID;
  43. ObjCARCAPElim() : ModulePass(ID) {
  44. initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
  45. }
  46. };
  47. }
  48. char ObjCARCAPElim::ID = 0;
  49. INITIALIZE_PASS(ObjCARCAPElim,
  50. "objc-arc-apelim",
  51. "ObjC ARC autorelease pool elimination",
  52. false, false)
  53. Pass *llvm::createObjCARCAPElimPass() {
  54. return new ObjCARCAPElim();
  55. }
  56. void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
  57. AU.setPreservesCFG();
  58. }
  59. /// Interprocedurally determine if calls made by the given call site can
  60. /// possibly produce autoreleases.
  61. bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) {
  62. if (const Function *Callee = CS.getCalledFunction()) {
  63. if (Callee->isDeclaration() || Callee->mayBeOverridden())
  64. return true;
  65. for (Function::const_iterator I = Callee->begin(), E = Callee->end();
  66. I != E; ++I) {
  67. const BasicBlock *BB = I;
  68. for (BasicBlock::const_iterator J = BB->begin(), F = BB->end();
  69. J != F; ++J)
  70. if (ImmutableCallSite JCS = ImmutableCallSite(J))
  71. // This recursion depth limit is arbitrary. It's just great
  72. // enough to cover known interesting testcases.
  73. if (Depth < 3 &&
  74. !JCS.onlyReadsMemory() &&
  75. MayAutorelease(JCS, Depth + 1))
  76. return true;
  77. }
  78. return false;
  79. }
  80. return true;
  81. }
  82. bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) {
  83. bool Changed = false;
  84. Instruction *Push = nullptr;
  85. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
  86. Instruction *Inst = I++;
  87. switch (GetBasicARCInstKind(Inst)) {
  88. case ARCInstKind::AutoreleasepoolPush:
  89. Push = Inst;
  90. break;
  91. case ARCInstKind::AutoreleasepoolPop:
  92. // If this pop matches a push and nothing in between can autorelease,
  93. // zap the pair.
  94. if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) {
  95. Changed = true;
  96. DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop "
  97. "autorelease pair:\n"
  98. " Pop: " << *Inst << "\n"
  99. << " Push: " << *Push << "\n");
  100. Inst->eraseFromParent();
  101. Push->eraseFromParent();
  102. }
  103. Push = nullptr;
  104. break;
  105. case ARCInstKind::CallOrUser:
  106. if (MayAutorelease(ImmutableCallSite(Inst)))
  107. Push = nullptr;
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. return Changed;
  114. }
  115. bool ObjCARCAPElim::runOnModule(Module &M) {
  116. if (!EnableARCOpts)
  117. return false;
  118. // If nothing in the Module uses ARC, don't do anything.
  119. if (!ModuleHasARC(M))
  120. return false;
  121. // Find the llvm.global_ctors variable, as the first step in
  122. // identifying the global constructors. In theory, unnecessary autorelease
  123. // pools could occur anywhere, but in practice it's pretty rare. Global
  124. // ctors are a place where autorelease pools get inserted automatically,
  125. // so it's pretty common for them to be unnecessary, and it's pretty
  126. // profitable to eliminate them.
  127. GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
  128. if (!GV)
  129. return false;
  130. assert(GV->hasDefinitiveInitializer() &&
  131. "llvm.global_ctors is uncooperative!");
  132. bool Changed = false;
  133. // Dig the constructor functions out of GV's initializer.
  134. ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
  135. for (User::op_iterator OI = Init->op_begin(), OE = Init->op_end();
  136. OI != OE; ++OI) {
  137. Value *Op = *OI;
  138. // llvm.global_ctors is an array of three-field structs where the second
  139. // members are constructor functions.
  140. Function *F = dyn_cast<Function>(cast<ConstantStruct>(Op)->getOperand(1));
  141. // If the user used a constructor function with the wrong signature and
  142. // it got bitcasted or whatever, look the other way.
  143. if (!F)
  144. continue;
  145. // Only look at function definitions.
  146. if (F->isDeclaration())
  147. continue;
  148. // Only look at functions with one basic block.
  149. if (std::next(F->begin()) != F->end())
  150. continue;
  151. // Ok, a single-block constructor function definition. Try to optimize it.
  152. Changed |= OptimizeBB(F->begin());
  153. }
  154. return Changed;
  155. }