1 //===- ObjCARCAPElim.cpp - ObjC ARC Optimization --------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
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 /// This specific file implements optimizations which remove extraneous
15 /// autorelease pools.
17 /// WARNING: This file knows about certain library functions. It recognizes them
18 /// by name, and hardwires knowledge of their semantics.
20 /// WARNING: This file knows about how certain Objective-C library functions are
21 /// used. Naive LLVM IR transformations which would otherwise be
22 /// behavior-preserving may break these assumptions.
24 //===----------------------------------------------------------------------===//
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/PassManager.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Transforms/ObjCARC.h"
36 using namespace llvm::objcarc
;
38 #define DEBUG_TYPE "objc-arc-ap-elim"
42 /// Interprocedurally determine if calls made by the given call site can
43 /// possibly produce autoreleases.
44 bool MayAutorelease(const CallBase
&CB
, unsigned Depth
= 0) {
45 if (const Function
*Callee
= CB
.getCalledFunction()) {
46 if (!Callee
->hasExactDefinition())
48 for (const BasicBlock
&BB
: *Callee
) {
49 for (const Instruction
&I
: BB
)
50 if (const CallBase
*JCB
= dyn_cast
<CallBase
>(&I
))
51 // This recursion depth limit is arbitrary. It's just great
52 // enough to cover known interesting testcases.
53 if (Depth
< 3 && !JCB
->onlyReadsMemory() &&
54 MayAutorelease(*JCB
, Depth
+ 1))
63 bool OptimizeBB(BasicBlock
*BB
) {
66 Instruction
*Push
= nullptr;
67 for (BasicBlock::iterator I
= BB
->begin(), E
= BB
->end(); I
!= E
; ) {
68 Instruction
*Inst
= &*I
++;
69 switch (GetBasicARCInstKind(Inst
)) {
70 case ARCInstKind::AutoreleasepoolPush
:
73 case ARCInstKind::AutoreleasepoolPop
:
74 // If this pop matches a push and nothing in between can autorelease,
76 if (Push
&& cast
<CallInst
>(Inst
)->getArgOperand(0) == Push
) {
78 LLVM_DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop "
84 Inst
->eraseFromParent();
85 Push
->eraseFromParent();
89 case ARCInstKind::CallOrUser
:
90 if (MayAutorelease(cast
<CallBase
>(*Inst
)))
101 bool runImpl(Module
&M
) {
105 // If nothing in the Module uses ARC, don't do anything.
106 if (!ModuleHasARC(M
))
108 // Find the llvm.global_ctors variable, as the first step in
109 // identifying the global constructors. In theory, unnecessary autorelease
110 // pools could occur anywhere, but in practice it's pretty rare. Global
111 // ctors are a place where autorelease pools get inserted automatically,
112 // so it's pretty common for them to be unnecessary, and it's pretty
113 // profitable to eliminate them.
114 GlobalVariable
*GV
= M
.getGlobalVariable("llvm.global_ctors");
118 assert(GV
->hasDefinitiveInitializer() &&
119 "llvm.global_ctors is uncooperative!");
121 bool Changed
= false;
123 // Dig the constructor functions out of GV's initializer.
124 ConstantArray
*Init
= cast
<ConstantArray
>(GV
->getInitializer());
125 for (User::op_iterator OI
= Init
->op_begin(), OE
= Init
->op_end();
128 // llvm.global_ctors is an array of three-field structs where the second
129 // members are constructor functions.
130 Function
*F
= dyn_cast
<Function
>(cast
<ConstantStruct
>(Op
)->getOperand(1));
131 // If the user used a constructor function with the wrong signature and
132 // it got bitcasted or whatever, look the other way.
135 // Only look at function definitions.
136 if (F
->isDeclaration())
138 // Only look at functions with one basic block.
139 if (std::next(F
->begin()) != F
->end())
141 // Ok, a single-block constructor function definition. Try to optimize it.
142 Changed
|= OptimizeBB(&F
->front());
148 /// Autorelease pool elimination.
149 class ObjCARCAPElim
: public ModulePass
{
150 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
151 bool runOnModule(Module
&M
) override
;
155 ObjCARCAPElim() : ModulePass(ID
) {
156 initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
161 char ObjCARCAPElim::ID
= 0;
162 INITIALIZE_PASS(ObjCARCAPElim
, "objc-arc-apelim",
163 "ObjC ARC autorelease pool elimination", false, false)
165 Pass
*llvm::createObjCARCAPElimPass() { return new ObjCARCAPElim(); }
167 void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage
&AU
) const {
168 AU
.setPreservesCFG();
171 bool ObjCARCAPElim::runOnModule(Module
&M
) {
177 PreservedAnalyses
ObjCARCAPElimPass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
179 return PreservedAnalyses::all();
180 PreservedAnalyses PA
;
181 PA
.preserveSet
<CFGAnalyses
>();