Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 1 | //===- ObjCARCOpts.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 | /// 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 | /// The optimizations performed include elimination of redundant, partially |
| 15 | /// redundant, and inconsequential reference count operations, elimination of |
| 16 | /// redundant weak pointer operations, pattern-matching and replacement of |
| 17 | /// low-level operations into higher-level operations, and numerous minor |
| 18 | /// simplifications. |
| 19 | /// |
| 20 | /// This file also defines a simple ARC-aware AliasAnalysis. |
| 21 | /// |
| 22 | /// WARNING: This file knows about certain library functions. It recognizes them |
| 23 | /// by name, and hardwires knowledge of their semantics. |
| 24 | /// |
| 25 | /// WARNING: This file knows about how certain Objective-C library functions are |
| 26 | /// used. Naive LLVM IR transformations which would otherwise be |
| 27 | /// behavior-preserving may break these assumptions. |
| 28 | /// |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | #define DEBUG_TYPE "objc-arc-ap-elim" |
| 32 | #include "ObjCARC.h" |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" |
| 34 | #include "llvm/IR/Constants.h" |
Michael Gottesman | 13a5f1a | 2013-01-29 04:51:59 +0000 | [diff] [blame^] | 35 | #include "llvm/Support/Debug.h" |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace llvm; |
| 38 | using namespace llvm::objcarc; |
| 39 | |
| 40 | namespace { |
| 41 | /// \brief Autorelease pool elimination. |
| 42 | class ObjCARCAPElim : public ModulePass { |
| 43 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 44 | virtual bool runOnModule(Module &M); |
| 45 | |
| 46 | static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0); |
| 47 | static bool OptimizeBB(BasicBlock *BB); |
| 48 | |
| 49 | public: |
| 50 | static char ID; |
| 51 | ObjCARCAPElim() : ModulePass(ID) { |
| 52 | initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry()); |
| 53 | } |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | char ObjCARCAPElim::ID = 0; |
| 58 | INITIALIZE_PASS(ObjCARCAPElim, |
| 59 | "objc-arc-apelim", |
| 60 | "ObjC ARC autorelease pool elimination", |
| 61 | false, false) |
| 62 | |
| 63 | Pass *llvm::createObjCARCAPElimPass() { |
| 64 | return new ObjCARCAPElim(); |
| 65 | } |
| 66 | |
| 67 | void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const { |
| 68 | AU.setPreservesCFG(); |
| 69 | } |
| 70 | |
| 71 | /// Interprocedurally determine if calls made by the given call site can |
| 72 | /// possibly produce autoreleases. |
| 73 | bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) { |
| 74 | if (const Function *Callee = CS.getCalledFunction()) { |
| 75 | if (Callee->isDeclaration() || Callee->mayBeOverridden()) |
| 76 | return true; |
| 77 | for (Function::const_iterator I = Callee->begin(), E = Callee->end(); |
| 78 | I != E; ++I) { |
| 79 | const BasicBlock *BB = I; |
| 80 | for (BasicBlock::const_iterator J = BB->begin(), F = BB->end(); |
| 81 | J != F; ++J) |
| 82 | if (ImmutableCallSite JCS = ImmutableCallSite(J)) |
| 83 | // This recursion depth limit is arbitrary. It's just great |
| 84 | // enough to cover known interesting testcases. |
| 85 | if (Depth < 3 && |
| 86 | !JCS.onlyReadsMemory() && |
| 87 | MayAutorelease(JCS, Depth + 1)) |
| 88 | return true; |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) { |
| 97 | bool Changed = false; |
| 98 | |
| 99 | Instruction *Push = 0; |
| 100 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 101 | Instruction *Inst = I++; |
| 102 | switch (GetBasicInstructionClass(Inst)) { |
| 103 | case IC_AutoreleasepoolPush: |
| 104 | Push = Inst; |
| 105 | break; |
| 106 | case IC_AutoreleasepoolPop: |
| 107 | // If this pop matches a push and nothing in between can autorelease, |
| 108 | // zap the pair. |
| 109 | if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) { |
| 110 | Changed = true; |
| 111 | DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop " |
| 112 | "autorelease pair:\n" |
| 113 | " Pop: " << *Inst << "\n" |
| 114 | << " Push: " << *Push << "\n"); |
| 115 | Inst->eraseFromParent(); |
| 116 | Push->eraseFromParent(); |
| 117 | } |
| 118 | Push = 0; |
| 119 | break; |
| 120 | case IC_CallOrUser: |
| 121 | if (MayAutorelease(ImmutableCallSite(Inst))) |
| 122 | Push = 0; |
| 123 | break; |
| 124 | default: |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return Changed; |
| 130 | } |
| 131 | |
| 132 | bool ObjCARCAPElim::runOnModule(Module &M) { |
| 133 | if (!EnableARCOpts) |
| 134 | return false; |
| 135 | |
| 136 | // If nothing in the Module uses ARC, don't do anything. |
| 137 | if (!ModuleHasARC(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 pairs where the second members |
| 161 | // 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 (llvm::next(F->begin()) != F->end()) |
| 172 | continue; |
| 173 | // Ok, a single-block constructor function definition. Try to optimize it. |
| 174 | Changed |= OptimizeBB(F->begin()); |
| 175 | } |
| 176 | |
| 177 | return Changed; |
| 178 | } |