Michael Gottesman | 5a8f9e7 | 2013-01-29 05:05:17 +0000 | [diff] [blame] | 1 | //===- ObjCARCAPElim.cpp - ObjC ARC Optimization --------------------------===// |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 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 |
Michael Gottesman | 5a8f9e7 | 2013-01-29 05:05:17 +0000 | [diff] [blame] | 10 | /// |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 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 | /// |
Michael Gottesman | 5a8f9e7 | 2013-01-29 05:05:17 +0000 | [diff] [blame] | 15 | /// This specific file implements optimizations which remove extraneous |
| 16 | /// autorelease pools. |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 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 | |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 27 | #include "ObjCARC.h" |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
| 29 | #include "llvm/IR/Constants.h" |
Michael Gottesman | 13a5f1a | 2013-01-29 04:51:59 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Timur Iskhodzhanov | 5d7ff00 | 2013-01-29 09:09:27 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
| 34 | using namespace llvm::objcarc; |
| 35 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "objc-arc-ap-elim" |
| 37 | |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | /// \brief Autorelease pool elimination. |
| 40 | class ObjCARCAPElim : public ModulePass { |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 41 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 42 | bool runOnModule(Module &M) override; |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 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 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame^] | 53 | } |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 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->isDeclaration() || Callee->mayBeOverridden()) |
| 74 | return true; |
| 75 | for (Function::const_iterator I = Callee->begin(), E = Callee->end(); |
| 76 | I != E; ++I) { |
| 77 | const BasicBlock *BB = I; |
| 78 | for (BasicBlock::const_iterator J = BB->begin(), F = BB->end(); |
| 79 | J != F; ++J) |
| 80 | if (ImmutableCallSite JCS = ImmutableCallSite(J)) |
| 81 | // This recursion depth limit is arbitrary. It's just great |
| 82 | // enough to cover known interesting testcases. |
| 83 | if (Depth < 3 && |
| 84 | !JCS.onlyReadsMemory() && |
| 85 | MayAutorelease(JCS, Depth + 1)) |
| 86 | return true; |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) { |
| 95 | bool Changed = false; |
| 96 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 97 | Instruction *Push = nullptr; |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 98 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 99 | Instruction *Inst = I++; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 100 | switch (GetBasicARCInstKind(Inst)) { |
| 101 | case ARCInstKind::AutoreleasepoolPush: |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 102 | Push = Inst; |
| 103 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 104 | case ARCInstKind::AutoreleasepoolPop: |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 105 | // If this pop matches a push and nothing in between can autorelease, |
| 106 | // zap the pair. |
| 107 | if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) { |
| 108 | Changed = true; |
| 109 | DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop " |
| 110 | "autorelease pair:\n" |
| 111 | " Pop: " << *Inst << "\n" |
| 112 | << " Push: " << *Push << "\n"); |
| 113 | Inst->eraseFromParent(); |
| 114 | Push->eraseFromParent(); |
| 115 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 116 | Push = nullptr; |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 117 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 118 | case ARCInstKind::CallOrUser: |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 119 | if (MayAutorelease(ImmutableCallSite(Inst))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 120 | Push = nullptr; |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 121 | break; |
| 122 | default: |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return Changed; |
| 128 | } |
| 129 | |
| 130 | bool ObjCARCAPElim::runOnModule(Module &M) { |
| 131 | if (!EnableARCOpts) |
| 132 | return false; |
| 133 | |
| 134 | // If nothing in the Module uses ARC, don't do anything. |
| 135 | if (!ModuleHasARC(M)) |
| 136 | return false; |
| 137 | |
| 138 | // Find the llvm.global_ctors variable, as the first step in |
| 139 | // identifying the global constructors. In theory, unnecessary autorelease |
| 140 | // pools could occur anywhere, but in practice it's pretty rare. Global |
| 141 | // ctors are a place where autorelease pools get inserted automatically, |
| 142 | // so it's pretty common for them to be unnecessary, and it's pretty |
| 143 | // profitable to eliminate them. |
| 144 | GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors"); |
| 145 | if (!GV) |
| 146 | return false; |
| 147 | |
| 148 | assert(GV->hasDefinitiveInitializer() && |
| 149 | "llvm.global_ctors is uncooperative!"); |
| 150 | |
| 151 | bool Changed = false; |
| 152 | |
| 153 | // Dig the constructor functions out of GV's initializer. |
| 154 | ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); |
| 155 | for (User::op_iterator OI = Init->op_begin(), OE = Init->op_end(); |
| 156 | OI != OE; ++OI) { |
| 157 | Value *Op = *OI; |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 158 | // llvm.global_ctors is an array of three-field structs where the second |
| 159 | // members are constructor functions. |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 160 | Function *F = dyn_cast<Function>(cast<ConstantStruct>(Op)->getOperand(1)); |
| 161 | // If the user used a constructor function with the wrong signature and |
| 162 | // it got bitcasted or whatever, look the other way. |
| 163 | if (!F) |
| 164 | continue; |
| 165 | // Only look at function definitions. |
| 166 | if (F->isDeclaration()) |
| 167 | continue; |
| 168 | // Only look at functions with one basic block. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 169 | if (std::next(F->begin()) != F->end()) |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 170 | continue; |
| 171 | // Ok, a single-block constructor function definition. Try to optimize it. |
| 172 | Changed |= OptimizeBB(F->begin()); |
| 173 | } |
| 174 | |
| 175 | return Changed; |
| 176 | } |