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