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