1 //===- ObjCARCAPElim.cpp - ObjC ARC Optimization --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file defines ObjC ARC optimizations. ARC stands for Automatic
12 /// Reference Counting and is a system for managing reference counts for objects
15 /// This specific file implements optimizations which remove extraneous
16 /// autorelease pools.
18 /// WARNING: This file knows about certain library functions. It recognizes them
19 /// by name, and hardwires knowledge of their semantics.
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.
25 //===----------------------------------------------------------------------===//
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
34 using namespace llvm::objcarc;
36 #define DEBUG_TYPE "objc-arc-ap-elim"
39 /// \brief Autorelease pool elimination.
40 class ObjCARCAPElim : public ModulePass {
41 void getAnalysisUsage(AnalysisUsage &AU) const override;
42 bool runOnModule(Module &M) override;
44 static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0);
45 static bool OptimizeBB(BasicBlock *BB);
49 ObjCARCAPElim() : ModulePass(ID) {
50 initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
55 char ObjCARCAPElim::ID = 0;
56 INITIALIZE_PASS(ObjCARCAPElim,
58 "ObjC ARC autorelease pool elimination",
61 Pass *llvm::createObjCARCAPElimPass() {
62 return new ObjCARCAPElim();
65 void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
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())
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.
81 !JCS.onlyReadsMemory() &&
82 MayAutorelease(JCS, Depth + 1))
91 bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) {
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:
101 case ARCInstKind::AutoreleasepoolPop:
102 // If this pop matches a push and nothing in between can autorelease,
104 if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) {
106 DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop "
107 "autorelease pair:\n"
108 " Pop: " << *Inst << "\n"
109 << " Push: " << *Push << "\n");
110 Inst->eraseFromParent();
111 Push->eraseFromParent();
115 case ARCInstKind::CallOrUser:
116 if (MayAutorelease(ImmutableCallSite(Inst)))
127 bool ObjCARCAPElim::runOnModule(Module &M) {
131 // If nothing in the Module uses ARC, don't do anything.
132 if (!ModuleHasARC(M))
135 // Find the llvm.global_ctors variable, as the first step in
136 // identifying the global constructors. In theory, unnecessary autorelease
137 // pools could occur anywhere, but in practice it's pretty rare. Global
138 // ctors are a place where autorelease pools get inserted automatically,
139 // so it's pretty common for them to be unnecessary, and it's pretty
140 // profitable to eliminate them.
141 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
145 assert(GV->hasDefinitiveInitializer() &&
146 "llvm.global_ctors is uncooperative!");
148 bool Changed = false;
150 // Dig the constructor functions out of GV's initializer.
151 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
152 for (User::op_iterator OI = Init->op_begin(), OE = Init->op_end();
155 // llvm.global_ctors is an array of three-field structs where the second
156 // members are constructor functions.
157 Function *F = dyn_cast<Function>(cast<ConstantStruct>(Op)->getOperand(1));
158 // If the user used a constructor function with the wrong signature and
159 // it got bitcasted or whatever, look the other way.
162 // Only look at function definitions.
163 if (F->isDeclaration())
165 // Only look at functions with one basic block.
166 if (std::next(F->begin()) != F->end())
168 // Ok, a single-block constructor function definition. Try to optimize it.
169 Changed |= OptimizeBB(&F->front());