blob: b341dd807508c20e31c7d250876e1db86eb16f1a [file] [log] [blame]
Michael Gottesman5a8f9e72013-01-29 05:05:17 +00001//===- ObjCARCAPElim.cpp - ObjC ARC Optimization --------------------------===//
Michael Gottesmanfa0939f2013-01-28 04:12:07 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Michael Gottesmanfa0939f2013-01-28 04:12:07 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Michael Gottesman5a8f9e72013-01-29 05:05:17 +00009///
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000010/// This file defines ObjC ARC optimizations. ARC stands for Automatic
11/// Reference Counting and is a system for managing reference counts for objects
12/// in Objective C.
13///
Michael Gottesman5a8f9e72013-01-29 05:05:17 +000014/// This specific file implements optimizations which remove extraneous
15/// autorelease pools.
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000016///
17/// WARNING: This file knows about certain library functions. It recognizes them
18/// by name, and hardwires knowledge of their semantics.
19///
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.
23///
24//===----------------------------------------------------------------------===//
25
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000026#include "ObjCARC.h"
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000027#include "llvm/ADT/STLExtras.h"
28#include "llvm/IR/Constants.h"
Michael Gottesman13a5f1a2013-01-29 04:51:59 +000029#include "llvm/Support/Debug.h"
Timur Iskhodzhanov5d7ff002013-01-29 09:09:27 +000030#include "llvm/Support/raw_ostream.h"
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000031
32using namespace llvm;
33using namespace llvm::objcarc;
34
Chandler Carruth964daaa2014-04-22 02:55:47 +000035#define DEBUG_TYPE "objc-arc-ap-elim"
36
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000037namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000038 /// Autorelease pool elimination.
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000039 class ObjCARCAPElim : public ModulePass {
Craig Topper3e4c6972014-03-05 09:10:37 +000040 void getAnalysisUsage(AnalysisUsage &AU) const override;
41 bool runOnModule(Module &M) override;
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000042
43 static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0);
44 static bool OptimizeBB(BasicBlock *BB);
45
46 public:
47 static char ID;
48 ObjCARCAPElim() : ModulePass(ID) {
49 initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
50 }
51 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000052}
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000053
54char ObjCARCAPElim::ID = 0;
55INITIALIZE_PASS(ObjCARCAPElim,
56 "objc-arc-apelim",
57 "ObjC ARC autorelease pool elimination",
58 false, false)
59
60Pass *llvm::createObjCARCAPElimPass() {
61 return new ObjCARCAPElim();
62}
63
64void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
65 AU.setPreservesCFG();
66}
67
68/// Interprocedurally determine if calls made by the given call site can
69/// possibly produce autoreleases.
70bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) {
71 if (const Function *Callee = CS.getCalledFunction()) {
Sanjoy Das5ce32722016-04-08 00:48:30 +000072 if (!Callee->hasExactDefinition())
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000073 return true;
Duncan P. N. Exon Smith1e59a662015-10-19 23:20:14 +000074 for (const BasicBlock &BB : *Callee) {
75 for (const Instruction &I : BB)
76 if (ImmutableCallSite JCS = ImmutableCallSite(&I))
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000077 // This recursion depth limit is arbitrary. It's just great
78 // enough to cover known interesting testcases.
79 if (Depth < 3 &&
80 !JCS.onlyReadsMemory() &&
81 MayAutorelease(JCS, Depth + 1))
82 return true;
83 }
84 return false;
85 }
86
87 return true;
88}
89
90bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) {
91 bool Changed = false;
92
Craig Topperf40110f2014-04-25 05:29:35 +000093 Instruction *Push = nullptr;
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000094 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Duncan P. N. Exon Smith1e59a662015-10-19 23:20:14 +000095 Instruction *Inst = &*I++;
Michael Gottesman6f729fa2015-02-19 19:51:32 +000096 switch (GetBasicARCInstKind(Inst)) {
97 case ARCInstKind::AutoreleasepoolPush:
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000098 Push = Inst;
99 break;
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000100 case ARCInstKind::AutoreleasepoolPop:
Michael Gottesmanfa0939f2013-01-28 04:12:07 +0000101 // If this pop matches a push and nothing in between can autorelease,
102 // zap the pair.
103 if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) {
104 Changed = true;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000105 LLVM_DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop "
106 "autorelease pair:\n"
107 " Pop: "
108 << *Inst << "\n"
109 << " Push: " << *Push
110 << "\n");
Michael Gottesmanfa0939f2013-01-28 04:12:07 +0000111 Inst->eraseFromParent();
112 Push->eraseFromParent();
113 }
Craig Topperf40110f2014-04-25 05:29:35 +0000114 Push = nullptr;
Michael Gottesmanfa0939f2013-01-28 04:12:07 +0000115 break;
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000116 case ARCInstKind::CallOrUser:
Michael Gottesmanfa0939f2013-01-28 04:12:07 +0000117 if (MayAutorelease(ImmutableCallSite(Inst)))
Craig Topperf40110f2014-04-25 05:29:35 +0000118 Push = nullptr;
Michael Gottesmanfa0939f2013-01-28 04:12:07 +0000119 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
Andrew Kayloraa641a52016-04-22 22:06:11 +0000136 if (skipModule(M))
137 return false;
138
Michael Gottesmanfa0939f2013-01-28 04:12:07 +0000139 // Find the llvm.global_ctors variable, as the first step in
140 // identifying the global constructors. In theory, unnecessary autorelease
141 // pools could occur anywhere, but in practice it's pretty rare. Global
142 // ctors are a place where autorelease pools get inserted automatically,
143 // so it's pretty common for them to be unnecessary, and it's pretty
144 // profitable to eliminate them.
145 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
146 if (!GV)
147 return false;
148
149 assert(GV->hasDefinitiveInitializer() &&
150 "llvm.global_ctors is uncooperative!");
151
152 bool Changed = false;
153
154 // Dig the constructor functions out of GV's initializer.
155 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
156 for (User::op_iterator OI = Init->op_begin(), OE = Init->op_end();
157 OI != OE; ++OI) {
158 Value *Op = *OI;
Reid Klecknerfceb76f2014-05-16 20:39:27 +0000159 // llvm.global_ctors is an array of three-field structs where the second
160 // members are constructor functions.
Michael Gottesmanfa0939f2013-01-28 04:12:07 +0000161 Function *F = dyn_cast<Function>(cast<ConstantStruct>(Op)->getOperand(1));
162 // If the user used a constructor function with the wrong signature and
163 // it got bitcasted or whatever, look the other way.
164 if (!F)
165 continue;
166 // Only look at function definitions.
167 if (F->isDeclaration())
168 continue;
169 // Only look at functions with one basic block.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000170 if (std::next(F->begin()) != F->end())
Michael Gottesmanfa0939f2013-01-28 04:12:07 +0000171 continue;
172 // Ok, a single-block constructor function definition. Try to optimize it.
Duncan P. N. Exon Smith1e59a662015-10-19 23:20:14 +0000173 Changed |= OptimizeBB(&F->front());
Michael Gottesmanfa0939f2013-01-28 04:12:07 +0000174 }
175
176 return Changed;
177}