blob: f693c3fb385f3b9ecbd516959ac1dc699619fde9 [file] [log] [blame]
Michael Gottesman5a8f9e72013-01-29 05:05:17 +00001//===- ObjCARCAPElim.cpp - ObjC ARC Optimization --------------------------===//
Michael Gottesmanfa0939f2013-01-28 04:12:07 +00002//
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 Gottesman5a8f9e72013-01-29 05:05:17 +000010///
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000011/// 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 Gottesman5a8f9e72013-01-29 05:05:17 +000015/// This specific file implements optimizations which remove extraneous
16/// autorelease pools.
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000017///
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
27#define DEBUG_TYPE "objc-arc-ap-elim"
28#include "ObjCARC.h"
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000029#include "llvm/ADT/STLExtras.h"
30#include "llvm/IR/Constants.h"
Michael Gottesman13a5f1a2013-01-29 04:51:59 +000031#include "llvm/Support/Debug.h"
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000032
33using namespace llvm;
34using namespace llvm::objcarc;
35
36namespace {
37 /// \brief Autorelease pool elimination.
38 class ObjCARCAPElim : public ModulePass {
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
40 virtual bool runOnModule(Module &M);
41
42 static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0);
43 static bool OptimizeBB(BasicBlock *BB);
44
45 public:
46 static char ID;
47 ObjCARCAPElim() : ModulePass(ID) {
48 initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
49 }
50 };
51}
52
53char ObjCARCAPElim::ID = 0;
54INITIALIZE_PASS(ObjCARCAPElim,
55 "objc-arc-apelim",
56 "ObjC ARC autorelease pool elimination",
57 false, false)
58
59Pass *llvm::createObjCARCAPElimPass() {
60 return new ObjCARCAPElim();
61}
62
63void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
64 AU.setPreservesCFG();
65}
66
67/// Interprocedurally determine if calls made by the given call site can
68/// possibly produce autoreleases.
69bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) {
70 if (const Function *Callee = CS.getCalledFunction()) {
71 if (Callee->isDeclaration() || Callee->mayBeOverridden())
72 return true;
73 for (Function::const_iterator I = Callee->begin(), E = Callee->end();
74 I != E; ++I) {
75 const BasicBlock *BB = I;
76 for (BasicBlock::const_iterator J = BB->begin(), F = BB->end();
77 J != F; ++J)
78 if (ImmutableCallSite JCS = ImmutableCallSite(J))
79 // This recursion depth limit is arbitrary. It's just great
80 // enough to cover known interesting testcases.
81 if (Depth < 3 &&
82 !JCS.onlyReadsMemory() &&
83 MayAutorelease(JCS, Depth + 1))
84 return true;
85 }
86 return false;
87 }
88
89 return true;
90}
91
92bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) {
93 bool Changed = false;
94
95 Instruction *Push = 0;
96 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
97 Instruction *Inst = I++;
98 switch (GetBasicInstructionClass(Inst)) {
99 case IC_AutoreleasepoolPush:
100 Push = Inst;
101 break;
102 case IC_AutoreleasepoolPop:
103 // If this pop matches a push and nothing in between can autorelease,
104 // zap the pair.
105 if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) {
106 Changed = true;
107 DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop "
108 "autorelease pair:\n"
109 " Pop: " << *Inst << "\n"
110 << " Push: " << *Push << "\n");
111 Inst->eraseFromParent();
112 Push->eraseFromParent();
113 }
114 Push = 0;
115 break;
116 case IC_CallOrUser:
117 if (MayAutorelease(ImmutableCallSite(Inst)))
118 Push = 0;
119 break;
120 default:
121 break;
122 }
123 }
124
125 return Changed;
126}
127
128bool ObjCARCAPElim::runOnModule(Module &M) {
129 if (!EnableARCOpts)
130 return false;
131
132 // If nothing in the Module uses ARC, don't do anything.
133 if (!ModuleHasARC(M))
134 return false;
135
136 // Find the llvm.global_ctors variable, as the first step in
137 // identifying the global constructors. In theory, unnecessary autorelease
138 // pools could occur anywhere, but in practice it's pretty rare. Global
139 // ctors are a place where autorelease pools get inserted automatically,
140 // so it's pretty common for them to be unnecessary, and it's pretty
141 // profitable to eliminate them.
142 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
143 if (!GV)
144 return false;
145
146 assert(GV->hasDefinitiveInitializer() &&
147 "llvm.global_ctors is uncooperative!");
148
149 bool Changed = false;
150
151 // Dig the constructor functions out of GV's initializer.
152 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
153 for (User::op_iterator OI = Init->op_begin(), OE = Init->op_end();
154 OI != OE; ++OI) {
155 Value *Op = *OI;
156 // llvm.global_ctors is an array of pairs where the second members
157 // are constructor functions.
158 Function *F = dyn_cast<Function>(cast<ConstantStruct>(Op)->getOperand(1));
159 // If the user used a constructor function with the wrong signature and
160 // it got bitcasted or whatever, look the other way.
161 if (!F)
162 continue;
163 // Only look at function definitions.
164 if (F->isDeclaration())
165 continue;
166 // Only look at functions with one basic block.
167 if (llvm::next(F->begin()) != F->end())
168 continue;
169 // Ok, a single-block constructor function definition. Try to optimize it.
170 Changed |= OptimizeBB(F->begin());
171 }
172
173 return Changed;
174}