1 //===- ObjCARCOpts.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 //===----------------------------------------------------------------------===//
10 /// This file defines ObjC ARC optimizations. ARC stands for Automatic
11 /// Reference Counting and is a system for managing reference counts for objects
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
20 /// This file also defines a simple ARC-aware AliasAnalysis.
22 /// WARNING: This file knows about certain library functions. It recognizes them
23 /// by name, and hardwires knowledge of their semantics.
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.
29 //===----------------------------------------------------------------------===//
31 #define DEBUG_TYPE "objc-arc-ap-elim"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/IR/Constants.h"
38 using namespace llvm::objcarc;
41 /// \brief Autorelease pool elimination.
42 class ObjCARCAPElim : public ModulePass {
43 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
44 virtual bool runOnModule(Module &M);
46 static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0);
47 static bool OptimizeBB(BasicBlock *BB);
51 ObjCARCAPElim() : ModulePass(ID) {
52 initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
57 char ObjCARCAPElim::ID = 0;
58 INITIALIZE_PASS(ObjCARCAPElim,
60 "ObjC ARC autorelease pool elimination",
63 Pass *llvm::createObjCARCAPElimPass() {
64 return new ObjCARCAPElim();
67 void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
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())
77 for (Function::const_iterator I = Callee->begin(), E = Callee->end();
79 const BasicBlock *BB = I;
80 for (BasicBlock::const_iterator J = BB->begin(), F = BB->end();
82 if (ImmutableCallSite JCS = ImmutableCallSite(J))
83 // This recursion depth limit is arbitrary. It's just great
84 // enough to cover known interesting testcases.
86 !JCS.onlyReadsMemory() &&
87 MayAutorelease(JCS, Depth + 1))
96 bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) {
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:
106 case IC_AutoreleasepoolPop:
107 // If this pop matches a push and nothing in between can autorelease,
109 if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) {
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();
121 if (MayAutorelease(ImmutableCallSite(Inst)))
132 bool ObjCARCAPElim::runOnModule(Module &M) {
136 // If nothing in the Module uses ARC, don't do anything.
137 if (!ModuleHasARC(M))
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");
150 assert(GV->hasDefinitiveInitializer() &&
151 "llvm.global_ctors is uncooperative!");
153 bool Changed = false;
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();
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.
167 // Only look at function definitions.
168 if (F->isDeclaration())
170 // Only look at functions with one basic block.
171 if (llvm::next(F->begin()) != F->end())
173 // Ok, a single-block constructor function definition. Try to optimize it.
174 Changed |= OptimizeBB(F->begin());