llvm.org GIT mirror llvm / 24c4898
Extracted ObjCARC.cpp into its own library libLLVMObjCARCOpts in preparation for refactoring the ARC Optimizer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173647 91177308-0d34-0410-b5e6-96231b3b80d8 Michael Gottesman 6 years ago
26 changed file(s) with 4718 addition(s) and 4608 deletion(s). Raw diff Collapse all Expand all
2929 /// initializeScalarOpts - Initialize all passes linked into the
3030 /// ScalarOpts library.
3131 void initializeScalarOpts(PassRegistry&);
32
33 /// initializeObjCARCOpts - Initialize all passes linked into the ObjCARCOpts
34 /// library.
35 void initializeObjCARCOpts(PassRegistry&);
3236
3337 /// initializeVectorization - Initialize all passes linked into the
3438 /// Vectorize library.
3030 #include "llvm/IR/Function.h"
3131 #include "llvm/Transforms/IPO.h"
3232 #include "llvm/Transforms/Instrumentation.h"
33 #include "llvm/Transforms/ObjCARC.h"
3334 #include "llvm/Transforms/Scalar.h"
3435 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
3536 #include "llvm/Transforms/Vectorize.h"
0 //===-- ObjCARC.h - ObjCARC Scalar Transformations --------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header file defines prototypes for accessor functions that expose passes
10 // in the ObjCARC Scalar Transformations library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TRANSFORMS_OBJCARC_H
15 #define LLVM_TRANSFORMS_OBJCARC_H
16
17 namespace llvm {
18
19 class Pass;
20
21 //===----------------------------------------------------------------------===//
22 //
23 // ObjCARCAPElim - ObjC ARC autorelease pool elimination.
24 //
25 Pass *createObjCARCAPElimPass();
26
27 //===----------------------------------------------------------------------===//
28 //
29 // ObjCARCExpand - ObjC ARC preliminary simplifications.
30 //
31 Pass *createObjCARCExpandPass();
32
33 //===----------------------------------------------------------------------===//
34 //
35 // ObjCARCContract - Late ObjC ARC cleanups.
36 //
37 Pass *createObjCARCContractPass();
38
39 //===----------------------------------------------------------------------===//
40 //
41 // ObjCARCOpt - ObjC ARC optimization.
42 //
43 Pass *createObjCARCOptPass();
44
45 } // End llvm namespace
46
47 #endif
48
333333
334334 //===----------------------------------------------------------------------===//
335335 //
336 // ObjCARCAPElim - ObjC ARC autorelease pool elimination.
337 //
338 Pass *createObjCARCAPElimPass();
339
340 //===----------------------------------------------------------------------===//
341 //
342 // ObjCARCExpand - ObjC ARC preliminary simplifications.
343 //
344 Pass *createObjCARCExpandPass();
345
346 //===----------------------------------------------------------------------===//
347 //
348 // ObjCARCContract - Late ObjC ARC cleanups.
349 //
350 Pass *createObjCARCContractPass();
351
352 //===----------------------------------------------------------------------===//
353 //
354 // ObjCARCOpt - ObjC ARC optimization.
355 //
356 Pass *createObjCARCOptPass();
357
358 //===----------------------------------------------------------------------===//
359 //
360336 // InstructionSimplifier - Remove redundant instructions.
361337 //
362338 FunctionPass *createInstructionSimplifierPass();
3333 void LLVMInitializeCore(LLVMPassRegistryRef R);
3434 void LLVMInitializeTransformUtils(LLVMPassRegistryRef R);
3535 void LLVMInitializeScalarOpts(LLVMPassRegistryRef R);
36 void LLVMInitializeObjCARCOpts(LLVMPassRegistryRef R);
3637 void LLVMInitializeVectorization(LLVMPassRegistryRef R);
3738 void LLVMInitializeInstCombine(LLVMPassRegistryRef R);
3839 void LLVMInitializeIPO(LLVMPassRegistryRef R);
2121 type = Library
2222 name = CodeGen
2323 parent = Libraries
24 required_libraries = Analysis Core MC Scalar Support Target TransformUtils
24 required_libraries = Analysis Core MC Scalar Support Target TransformUtils ObjCARC
44 add_subdirectory(IPO)
55 add_subdirectory(Vectorize)
66 add_subdirectory(Hello)
7 add_subdirectory(ObjCARC)
1919 name = IPO
2020 parent = Transforms
2121 library_name = ipo
22 required_libraries = Analysis Core IPA InstCombine Scalar Vectorize Support Target TransformUtils
22 required_libraries = Analysis Core IPA InstCombine Scalar Vectorize Support Target TransformUtils ObjCARC
1515 ;===------------------------------------------------------------------------===;
1616
1717 [common]
18 subdirectories = IPO InstCombine Instrumentation Scalar Utils Vectorize
18 subdirectories = IPO InstCombine Instrumentation Scalar Utils Vectorize ObjCARC
1919
2020 [component_0]
2121 type = Group
77 ##===----------------------------------------------------------------------===##
88
99 LEVEL = ../..
10 PARALLEL_DIRS = Utils Instrumentation Scalar InstCombine IPO Vectorize Hello
10 PARALLEL_DIRS = Utils Instrumentation Scalar InstCombine IPO Vectorize Hello ObjCARC
1111
1212 include $(LEVEL)/Makefile.config
1313
0 add_llvm_library(LLVMObjCARCOpts
1 ObjCARC.cpp
2 ObjCARCOpts.cpp
3 )
4
5 add_dependencies(LLVMObjCARCOpts intrinsics_gen)
0 ;===- ./lib/Transforms/ObjCARC/LLVMBuild.txt --- ---------*- Conf -*--===;
1 ;
2 ; The LLVM Compiler Infrastructure
3 ;
4 ; This file is distributed under the University of Illinois Open Source
5 ; License. See LICENSE.TXT for details.
6 ;
7 ;===------------------------------------------------------------------------===;
8 ;
9 ; This is an LLVMBuild description file for the components in this subdirectory.
10 ;
11 ; For more information on the LLVMBuild system, please see:
12 ;
13 ; http://llvm.org/docs/LLVMBuild.html
14 ;
15 ;===------------------------------------------------------------------------===;
16
17 [component_0]
18 type = Library
19 name = ObjCARC
20 parent = Transforms
21 library_name = ObjCARCOpts
22 required_libraries = Analysis Core InstCombine Support Target TransformUtils
0 ##===- lib/Transforms/ObjCARC/Makefile ---------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL = ../../..
10 LIBRARYNAME = LLVMObjCARCOpts
11 BUILD_ARCHIVE = 1
12
13 include $(LEVEL)/Makefile.common
14
0 //===-- ObjCARC.cpp --------------------------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements common infrastructure for libLLVMObjCARCOpts.a, which
10 // implements several scalar transformations over the LLVM intermediate
11 // representation, including the C bindings for that library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Transforms/ObjCARC.h"
16 #include "llvm-c/Initialization.h"
17 #include "llvm/Analysis/Passes.h"
18 #include "llvm/Analysis/Verifier.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/PassManager.h"
22
23 using namespace llvm;
24
25 /// initializeObjCARCOptsPasses - Initialize all passes linked into the
26 /// ObjCARCOpts library.
27 void llvm::initializeObjCARCOpts(PassRegistry &Registry) {
28 initializeObjCARCAliasAnalysisPass(Registry);
29 initializeObjCARCAPElimPass(Registry);
30 initializeObjCARCExpandPass(Registry);
31 initializeObjCARCContractPass(Registry);
32 initializeObjCARCOptPass(Registry);
33 }
34
35 void LLVMInitializeObjCARCOpts(LLVMPassRegistryRef R) {
36 initializeObjCARCOpts(*unwrap(R));
37 }
0 //===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file defines ObjC ARC optimizations. ARC stands for Automatic
10 /// Reference Counting and is a system for managing reference counts for objects
11 /// in Objective C.
12 ///
13 /// The optimizations performed include elimination of redundant, partially
14 /// redundant, and inconsequential reference count operations, elimination of
15 /// redundant weak pointer operations, pattern-matching and replacement of
16 /// low-level operations into higher-level operations, and numerous minor
17 /// simplifications.
18 ///
19 /// This file also defines a simple ARC-aware AliasAnalysis.
20 ///
21 /// WARNING: This file knows about certain library functions. It recognizes them
22 /// by name, and hardwires knowledge of their semantics.
23 ///
24 /// WARNING: This file knows about how certain Objective-C library functions are
25 /// used. Naive LLVM IR transformations which would otherwise be
26 /// behavior-preserving may break these assumptions.
27 ///
28 //===----------------------------------------------------------------------===//
29
30 #define DEBUG_TYPE "objc-arc"
31 #include "llvm/ADT/DenseMap.h"
32 #include "llvm/ADT/SmallPtrSet.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/raw_ostream.h"
36 using namespace llvm;
37
38 /// \brief A handy option to enable/disable all optimizations in this file.
39 static cl::opt EnableARCOpts("enable-objc-arc-opts", cl::init(true));
40
41 /// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific.
42 /// @{
43
44 namespace {
45 /// \brief An associative container with fast insertion-order (deterministic)
46 /// iteration over its elements. Plus the special blot operation.
47 template
48 class MapVector {
49 /// Map keys to indices in Vector.
50 typedef DenseMap MapTy;
51 MapTy Map;
52
53 typedef std::vector > VectorTy;
54 /// Keys and values.
55 VectorTy Vector;
56
57 public:
58 typedef typename VectorTy::iterator iterator;
59 typedef typename VectorTy::const_iterator const_iterator;
60 iterator begin() { return Vector.begin(); }
61 iterator end() { return Vector.end(); }
62 const_iterator begin() const { return Vector.begin(); }
63 const_iterator end() const { return Vector.end(); }
64
65 #ifdef XDEBUG
66 ~MapVector() {
67 assert(Vector.size() >= Map.size()); // May differ due to blotting.
68 for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
69 I != E; ++I) {
70 assert(I->second < Vector.size());
71 assert(Vector[I->second].first == I->first);
72 }
73 for (typename VectorTy::const_iterator I = Vector.begin(),
74 E = Vector.end(); I != E; ++I)
75 assert(!I->first ||
76 (Map.count(I->first) &&
77 Map[I->first] == size_t(I - Vector.begin())));
78 }
79 #endif
80
81 ValueT &operator[](const KeyT &Arg) {
82 std::pair Pair =
83 Map.insert(std::make_pair(Arg, size_t(0)));
84 if (Pair.second) {
85 size_t Num = Vector.size();
86 Pair.first->second = Num;
87 Vector.push_back(std::make_pair(Arg, ValueT()));
88 return Vector[Num].second;
89 }
90 return Vector[Pair.first->second].second;
91 }
92
93 std::pair
94 insert(const std::pair &InsertPair) {
95 std::pair Pair =
96 Map.insert(std::make_pair(InsertPair.first, size_t(0)));
97 if (Pair.second) {
98 size_t Num = Vector.size();
99 Pair.first->second = Num;
100 Vector.push_back(InsertPair);
101 return std::make_pair(Vector.begin() + Num, true);
102 }
103 return std::make_pair(Vector.begin() + Pair.first->second, false);
104 }
105
106 const_iterator find(const KeyT &Key) const {
107 typename MapTy::const_iterator It = Map.find(Key);
108 if (It == Map.end()) return Vector.end();
109 return Vector.begin() + It->second;
110 }
111
112 /// This is similar to erase, but instead of removing the element from the
113 /// vector, it just zeros out the key in the vector. This leaves iterators
114 /// intact, but clients must be prepared for zeroed-out keys when iterating.
115 void blot(const KeyT &Key) {
116 typename MapTy::iterator It = Map.find(Key);
117 if (It == Map.end()) return;
118 Vector[It->second].first = KeyT();
119 Map.erase(It);
120 }
121
122 void clear() {
123 Map.clear();
124 Vector.clear();
125 }
126 };
127 }
128
129 /// @}
130 ///
131 /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
132 /// @{
133
134 #include "llvm/ADT/StringSwitch.h"
135 #include "llvm/Analysis/ValueTracking.h"
136 #include "llvm/IR/Intrinsics.h"
137 #include "llvm/IR/Module.h"
138 #include "llvm/Support/CallSite.h"
139 #include "llvm/Transforms/Utils/Local.h"
140
141 namespace {
142 /// \enum InstructionClass
143 /// \brief A simple classification for instructions.
144 enum InstructionClass {
145 IC_Retain, ///< objc_retain
146 IC_RetainRV, ///< objc_retainAutoreleasedReturnValue
147 IC_RetainBlock, ///< objc_retainBlock
148 IC_Release, ///< objc_release
149 IC_Autorelease, ///< objc_autorelease
150 IC_AutoreleaseRV, ///< objc_autoreleaseReturnValue
151 IC_AutoreleasepoolPush, ///< objc_autoreleasePoolPush
152 IC_AutoreleasepoolPop, ///< objc_autoreleasePoolPop
153 IC_NoopCast, ///< objc_retainedObject, etc.
154 IC_FusedRetainAutorelease, ///< objc_retainAutorelease
155 IC_FusedRetainAutoreleaseRV, ///< objc_retainAutoreleaseReturnValue
156 IC_LoadWeakRetained, ///< objc_loadWeakRetained (primitive)
157 IC_StoreWeak, ///< objc_storeWeak (primitive)
158 IC_InitWeak, ///< objc_initWeak (derived)
159 IC_LoadWeak, ///< objc_loadWeak (derived)
160 IC_MoveWeak, ///< objc_moveWeak (derived)
161 IC_CopyWeak, ///< objc_copyWeak (derived)
162 IC_DestroyWeak, ///< objc_destroyWeak (derived)
163 IC_StoreStrong, ///< objc_storeStrong (derived)
164 IC_CallOrUser, ///< could call objc_release and/or "use" pointers
165 IC_Call, ///< could call objc_release
166 IC_User, ///< could "use" a pointer
167 IC_None ///< anything else
168 };
169
170 raw_ostream &operator<<(raw_ostream &OS, const InstructionClass Class)
171 LLVM_ATTRIBUTE_USED;
172 raw_ostream &operator<<(raw_ostream &OS, const InstructionClass Class) {
173 switch (Class) {
174 case IC_Retain:
175 return OS << "IC_Retain";
176 case IC_RetainRV:
177 return OS << "IC_RetainRV";
178 case IC_RetainBlock:
179 return OS << "IC_RetainBlock";
180 case IC_Release:
181 return OS << "IC_Release";
182 case IC_Autorelease:
183 return OS << "IC_Autorelease";
184 case IC_AutoreleaseRV:
185 return OS << "IC_AutoreleaseRV";
186 case IC_AutoreleasepoolPush:
187 return OS << "IC_AutoreleasepoolPush";
188 case IC_AutoreleasepoolPop:
189 return OS << "IC_AutoreleasepoolPop";
190 case IC_NoopCast:
191 return OS << "IC_NoopCast";
192 case IC_FusedRetainAutorelease:
193 return OS << "IC_FusedRetainAutorelease";
194 case IC_FusedRetainAutoreleaseRV:
195 return OS << "IC_FusedRetainAutoreleaseRV";
196 case IC_LoadWeakRetained:
197 return OS << "IC_LoadWeakRetained";
198 case IC_StoreWeak:
199 return OS << "IC_StoreWeak";
200 case IC_InitWeak:
201 return OS << "IC_InitWeak";
202 case IC_LoadWeak:
203 return OS << "IC_LoadWeak";
204 case IC_MoveWeak:
205 return OS << "IC_MoveWeak";
206 case IC_CopyWeak:
207 return OS << "IC_CopyWeak";
208 case IC_DestroyWeak:
209 return OS << "IC_DestroyWeak";
210 case IC_StoreStrong:
211 return OS << "IC_StoreStrong";
212 case IC_CallOrUser:
213 return OS << "IC_CallOrUser";
214 case IC_Call:
215 return OS << "IC_Call";
216 case IC_User:
217 return OS << "IC_User";
218 case IC_None:
219 return OS << "IC_None";
220 }
221 llvm_unreachable("Unknown instruction class!");
222 }
223 }
224
225 /// \brief Test whether the given value is possible a retainable object pointer.
226 static bool IsPotentialRetainableObjPtr(const Value *Op) {
227 // Pointers to static or stack storage are not valid retainable object pointers.
228 if (isa(Op) || isa(Op))
229 return false;
230 // Special arguments can not be a valid retainable object pointer.
231 if (const Argument *Arg = dyn_cast(Op))
232 if (Arg->hasByValAttr() ||
233 Arg->hasNestAttr() ||
234 Arg->hasStructRetAttr())
235 return false;
236 // Only consider values with pointer types.
237 //
238 // It seemes intuitive to exclude function pointer types as well, since
239 // functions are never retainable object pointers, however clang occasionally
240 // bitcasts retainable object pointers to function-pointer type temporarily.
241 PointerType *Ty = dyn_cast(Op->getType());
242 if (!Ty)
243 return false;
244 // Conservatively assume anything else is a potential retainable object pointer.
245 return true;
246 }
247
248 /// \brief Helper for GetInstructionClass. Determines what kind of construct CS
249 /// is.
250 static InstructionClass GetCallSiteClass(ImmutableCallSite CS) {
251 for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
252 I != E; ++I)
253 if (IsPotentialRetainableObjPtr(*I))
254 return CS.onlyReadsMemory() ? IC_User : IC_CallOrUser;
255
256 return CS.onlyReadsMemory() ? IC_None : IC_Call;
257 }
258
259 /// \brief Determine if F is one of the special known Functions. If it isn't,
260 /// return IC_CallOrUser.
261 static InstructionClass GetFunctionClass(const Function *F) {
262 Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
263
264 // No arguments.
265 if (AI == AE)
266 return StringSwitch(F->getName())
267 .Case("objc_autoreleasePoolPush", IC_AutoreleasepoolPush)
268 .Default(IC_CallOrUser);
269
270 // One argument.
271 const Argument *A0 = AI++;
272 if (AI == AE)
273 // Argument is a pointer.
274 if (PointerType *PTy = dyn_cast(A0->getType())) {
275 Type *ETy = PTy->getElementType();
276 // Argument is i8*.
277 if (ETy->isIntegerTy(8))
278 return StringSwitch(F->getName())
279 .Case("objc_retain", IC_Retain)
280 .Case("objc_retainAutoreleasedReturnValue", IC_RetainRV)
281 .Case("objc_retainBlock", IC_RetainBlock)
282 .Case("objc_release", IC_Release)
283 .Case("objc_autorelease", IC_Autorelease)
284 .Case("objc_autoreleaseReturnValue", IC_AutoreleaseRV)
285 .Case("objc_autoreleasePoolPop", IC_AutoreleasepoolPop)
286 .Case("objc_retainedObject", IC_NoopCast)
287 .Case("objc_unretainedObject", IC_NoopCast)
288 .Case("objc_unretainedPointer", IC_NoopCast)
289 .Case("objc_retain_autorelease", IC_FusedRetainAutorelease)
290 .Case("objc_retainAutorelease", IC_FusedRetainAutorelease)
291 .Case("objc_retainAutoreleaseReturnValue",IC_FusedRetainAutoreleaseRV)
292 .Default(IC_CallOrUser);
293
294 // Argument is i8**
295 if (PointerType *Pte = dyn_cast(ETy))
296 if (Pte->getElementType()->isIntegerTy(8))
297 return StringSwitch(F->getName())
298 .Case("objc_loadWeakRetained", IC_LoadWeakRetained)
299 .Case("objc_loadWeak", IC_LoadWeak)
300 .Case("objc_destroyWeak", IC_DestroyWeak)
301 .Default(IC_CallOrUser);
302 }
303
304 // Two arguments, first is i8**.
305 const Argument *A1 = AI++;
306 if (AI == AE)
307 if (PointerType *PTy = dyn_cast(A0->getType()))
308 if (PointerType *Pte = dyn_cast(PTy->getElementType()))
309 if (Pte->getElementType()->isIntegerTy(8))
310 if (PointerType *PTy1 = dyn_cast(A1->getType())) {
311 Type *ETy1 = PTy1->getElementType();
312 // Second argument is i8*
313 if (ETy1->isIntegerTy(8))
314 return StringSwitch(F->getName())
315 .Case("objc_storeWeak", IC_StoreWeak)
316 .Case("objc_initWeak", IC_InitWeak)
317 .Case("objc_storeStrong", IC_StoreStrong)
318 .Default(IC_CallOrUser);
319 // Second argument is i8**.
320 if (PointerType *Pte1 = dyn_cast(ETy1))
321 if (Pte1->getElementType()->isIntegerTy(8))
322 return StringSwitch(F->getName())
323 .Case("objc_moveWeak", IC_MoveWeak)
324 .Case("objc_copyWeak", IC_CopyWeak)
325 .Default(IC_CallOrUser);
326 }
327
328 // Anything else.
329 return IC_CallOrUser;
330 }
331
332 /// \brief Determine what kind of construct V is.
333 static InstructionClass GetInstructionClass(const Value *V) {
334 if (const Instruction *I = dyn_cast(V)) {
335 // Any instruction other than bitcast and gep with a pointer operand have a
336 // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
337 // to a subsequent use, rather than using it themselves, in this sense.
338 // As a short cut, several other opcodes are known to have no pointer
339 // operands of interest. And ret is never followed by a release, so it's
340 // not interesting to examine.
341 switch (I->getOpcode()) {
342 case Instruction::Call: {
343 const CallInst *CI = cast(I);
344 // Check for calls to special functions.
345 if (const Function *F = CI->getCalledFunction()) {
346 InstructionClass Class = GetFunctionClass(F);
347 if (Class != IC_CallOrUser)
348 return Class;
349
350 // None of the intrinsic functions do objc_release. For intrinsics, the
351 // only question is whether or not they may be users.
352 switch (F->getIntrinsicID()) {
353 case Intrinsic::returnaddress: case Intrinsic::frameaddress:
354 case Intrinsic::stacksave: case Intrinsic::stackrestore:
355 case Intrinsic::vastart: case Intrinsic::vacopy: case Intrinsic::vaend:
356 case Intrinsic::objectsize: case Intrinsic::prefetch:
357 case Intrinsic::stackprotector:
358 case Intrinsic::eh_return_i32: case Intrinsic::eh_return_i64:
359 case Intrinsic::eh_typeid_for: case Intrinsic::eh_dwarf_cfa:
360 case Intrinsic::eh_sjlj_lsda: case Intrinsic::eh_sjlj_functioncontext:
361 case Intrinsic::init_trampoline: case Intrinsic::adjust_trampoline:
362 case Intrinsic::lifetime_start: case Intrinsic::lifetime_end:
363 case Intrinsic::invariant_start: case Intrinsic::invariant_end:
364 // Don't let dbg info affect our results.
365 case Intrinsic::dbg_declare: case Intrinsic::dbg_value:
366 // Short cut: Some intrinsics obviously don't use ObjC pointers.
367 return IC_None;
368 default:
369 break;
370 }
371 }
372 return GetCallSiteClass(CI);
373 }
374 case Instruction::Invoke:
375 return GetCallSiteClass(cast(I));
376 case Instruction::BitCast:
377 case Instruction::GetElementPtr:
378 case Instruction::Select: case Instruction::PHI:
379 case Instruction::Ret: case Instruction::Br:
380 case Instruction::Switch: case Instruction::IndirectBr:
381 case Instruction::Alloca: case Instruction::VAArg:
382 case Instruction::Add: case Instruction::FAdd:
383 case Instruction::Sub: case Instruction::FSub:
384 case Instruction::Mul: case Instruction::FMul:
385 case Instruction::SDiv: case Instruction::UDiv: case Instruction::FDiv:
386 case Instruction::SRem: case Instruction::URem: case Instruction::FRem:
387 case Instruction::Shl: case Instruction::LShr: case Instruction::AShr:
388 case Instruction::And: case Instruction::Or: case Instruction::Xor:
389 case Instruction::SExt: case Instruction::ZExt: case Instruction::Trunc:
390 case Instruction::IntToPtr: case Instruction::FCmp:
391 case Instruction::FPTrunc: case Instruction::FPExt:
392 case Instruction::FPToUI: case Instruction::FPToSI:
393 case Instruction::UIToFP: case Instruction::SIToFP:
394 case Instruction::InsertElement: case Instruction::ExtractElement:
395 case Instruction::ShuffleVector:
396 case Instruction::ExtractValue:
397 break;
398 case Instruction::ICmp:
399 // Comparing a pointer with null, or any other constant, isn't an
400 // interesting use, because we don't care what the pointer points to, or
401 // about the values of any other dynamic reference-counted pointers.
402 if (IsPotentialRetainableObjPtr(I->getOperand(1)))
403 return IC_User;
404 break;
405 default:
406 // For anything else, check all the operands.
407 // Note that this includes both operands of a Store: while the first
408 // operand isn't actually being dereferenced, it is being stored to
409 // memory where we can no longer track who might read it and dereference
410 // it, so we have to consider it potentially used.
411 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
412 OI != OE; ++OI)
413 if (IsPotentialRetainableObjPtr(*OI))
414 return IC_User;
415 }
416 }
417
418 // Otherwise, it's totally inert for ARC purposes.
419 return IC_None;
420 }
421
422 /// \brief Determine which objc runtime call instruction class V belongs to.
423 ///
424 /// This is similar to GetInstructionClass except that it only detects objc
425 /// runtime calls. This allows it to be faster.
426 ///
427 static InstructionClass GetBasicInstructionClass(const Value *V) {
428 if (const CallInst *CI = dyn_cast(V)) {
429 if (const Function *F = CI->getCalledFunction())
430 return GetFunctionClass(F);
431 // Otherwise, be conservative.
432 return IC_CallOrUser;
433 }
434
435 // Otherwise, be conservative.
436 return isa(V) ? IC_CallOrUser : IC_User;
437 }
438
439 /// \brief Test if the given class is objc_retain or equivalent.
440 static bool IsRetain(InstructionClass Class) {
441 return Class == IC_Retain ||
442 Class == IC_RetainRV;
443 }
444
445 /// \brief Test if the given class is objc_autorelease or equivalent.
446 static bool IsAutorelease(InstructionClass Class) {
447 return Class == IC_Autorelease ||
448 Class == IC_AutoreleaseRV;
449 }
450
451 /// \brief Test if the given class represents instructions which return their
452 /// argument verbatim.
453 static bool IsForwarding(InstructionClass Class) {
454 // objc_retainBlock technically doesn't always return its argument
455 // verbatim, but it doesn't matter for our purposes here.
456 return Class == IC_Retain ||
457 Class == IC_RetainRV ||
458 Class == IC_Autorelease ||
459 Class == IC_AutoreleaseRV ||
460 Class == IC_RetainBlock ||
461 Class == IC_NoopCast;
462 }
463
464 /// \brief Test if the given class represents instructions which do nothing if
465 /// passed a null pointer.
466 static bool IsNoopOnNull(InstructionClass Class) {
467 return Class == IC_Retain ||
468 Class == IC_RetainRV ||
469 Class == IC_Release ||
470 Class == IC_Autorelease ||
471 Class == IC_AutoreleaseRV ||
472 Class == IC_RetainBlock;
473 }
474
475 /// \brief Test if the given class represents instructions which are always safe
476 /// to mark with the "tail" keyword.
477 static bool IsAlwaysTail(InstructionClass Class) {
478 // IC_RetainBlock may be given a stack argument.
479 return Class == IC_Retain ||
480 Class == IC_RetainRV ||
481 Class == IC_AutoreleaseRV;
482 }
483
484 /// \brief Test if the given class represents instructions which are never safe
485 /// to mark with the "tail" keyword.
486 static bool IsNeverTail(InstructionClass Class) {
487 /// It is never safe to tail call objc_autorelease since by tail calling
488 /// objc_autorelease, we also tail call -[NSObject autorelease] which supports
489 /// fast autoreleasing causing our object to be potentially reclaimed from the
490 /// autorelease pool which violates the semantics of __autoreleasing types in
491 /// ARC.
492 return Class == IC_Autorelease;
493 }
494
495 /// \brief Test if the given class represents instructions which are always safe
496 /// to mark with the nounwind attribute.
497 static bool IsNoThrow(InstructionClass Class) {
498 // objc_retainBlock is not nounwind because it calls user copy constructors
499 // which could theoretically throw.
500 return Class == IC_Retain ||
501 Class == IC_RetainRV ||
502 Class == IC_Release ||
503 Class == IC_Autorelease ||
504 Class == IC_AutoreleaseRV ||
505 Class == IC_AutoreleasepoolPush ||
506 Class == IC_AutoreleasepoolPop;
507 }
508
509 /// \brief Erase the given instruction.
510 ///
511 /// Many ObjC calls return their argument verbatim,
512 /// so if it's such a call and the return value has users, replace them with the
513 /// argument value.
514 ///
515 static void EraseInstruction(Instruction *CI) {
516 Value *OldArg = cast(CI)->getArgOperand(0);
517
518 bool Unused = CI->use_empty();
519
520 if (!Unused) {
521 // Replace the return value with the argument.
522 assert(IsForwarding(GetBasicInstructionClass(CI)) &&
523 "Can't delete non-forwarding instruction with users!");
524 CI->replaceAllUsesWith(OldArg);
525 }
526
527 CI->eraseFromParent();
528
529 if (Unused)
530 RecursivelyDeleteTriviallyDeadInstructions(OldArg);
531 }
532
533 /// \brief This is a wrapper around getUnderlyingObject which also knows how to
534 /// look through objc_retain and objc_autorelease calls, which we know to return
535 /// their argument verbatim.
536 static const Value *GetUnderlyingObjCPtr(const Value *V) {
537 for (;;) {
538 V = GetUnderlyingObject(V);
539 if (!IsForwarding(GetBasicInstructionClass(V)))
540 break;
541 V = cast(V)->getArgOperand(0);
542 }
543
544 return V;
545 }
546
547 /// \brief This is a wrapper around Value::stripPointerCasts which also knows
548 /// how to look through objc_retain and objc_autorelease calls, which we know to
549 /// return their argument verbatim.
550 static const Value *StripPointerCastsAndObjCCalls(const Value *V) {
551 for (;;) {
552 V = V->stripPointerCasts();
553 if (!IsForwarding(GetBasicInstructionClass(V)))
554 break;
555 V = cast(V)->getArgOperand(0);
556 }
557 return V;
558 }
559
560 /// \brief This is a wrapper around Value::stripPointerCasts which also knows
561 /// how to look through objc_retain and objc_autorelease calls, which we know to
562 /// return their argument verbatim.
563 static Value *StripPointerCastsAndObjCCalls(Value *V) {
564 for (;;) {
565 V = V->stripPointerCasts();
566 if (!IsForwarding(GetBasicInstructionClass(V)))
567 break;
568 V = cast(V)->getArgOperand(0);
569 }
570 return V;
571 }
572
573 /// \brief Assuming the given instruction is one of the special calls such as
574 /// objc_retain or objc_release, return the argument value, stripped of no-op
575 /// casts and forwarding calls.
576 static Value *GetObjCArg(Value *Inst) {
577 return StripPointerCastsAndObjCCalls(cast(Inst)->getArgOperand(0));
578 }
579
580 /// \brief Return true if this value refers to a distinct and identifiable
581 /// object.
582 ///
583 /// This is similar to AliasAnalysis's isIdentifiedObject, except that it uses
584 /// special knowledge of ObjC conventions.
585 static bool IsObjCIdentifiedObject(const Value *V) {
586 // Assume that call results and arguments have their own "provenance".
587 // Constants (including GlobalVariables) and Allocas are never
588 // reference-counted.
589 if (isa(V) || isa(V) ||
590 isa(V) || isa(V) ||
591 isa(V))
592 return true;
593
594 if (const LoadInst *LI = dyn_cast(V)) {
595 const Value *Pointer =
596 StripPointerCastsAndObjCCalls(LI->getPointerOperand());
597 if (const GlobalVariable *GV = dyn_cast(Pointer)) {
598 // A constant pointer can't be pointing to an object on the heap. It may
599 // be reference-counted, but it won't be deleted.
600 if (GV->isConstant())
601 return true;
602 StringRef Name = GV->getName();
603 // These special variables are known to hold values which are not
604 // reference-counted pointers.
605 if (Name.startswith("\01L_OBJC_SELECTOR_REFERENCES_") ||
606 Name.startswith("\01L_OBJC_CLASSLIST_REFERENCES_") ||
607 Name.startswith("\01L_OBJC_CLASSLIST_SUP_REFS_$_") ||
608 Name.startswith("\01L_OBJC_METH_VAR_NAME_") ||
609 Name.startswith("\01l_objc_msgSend_fixup_"))
610 return true;
611 }
612 }
613
614 return false;
615 }
616
617 /// \brief This is similar to StripPointerCastsAndObjCCalls but it stops as soon
618 /// as it finds a value with multiple uses.
619 static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
620 if (Arg->hasOneUse()) {
621 if (const BitCastInst *BC = dyn_cast(Arg))
622 return FindSingleUseIdentifiedObject(BC->getOperand(0));
623 if (const GetElementPtrInst *GEP = dyn_cast(Arg))
624 if (GEP->hasAllZeroIndices())
625 return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
626 if (IsForwarding(GetBasicInstructionClass(Arg)))
627 return FindSingleUseIdentifiedObject(
628 cast(Arg)->getArgOperand(0));
629 if (!IsObjCIdentifiedObject(Arg))
630 return 0;
631 return Arg;
632 }
633
634 // If we found an identifiable object but it has multiple uses, but they are
635 // trivial uses, we can still consider this to be a single-use value.
636 if (IsObjCIdentifiedObject(Arg)) {
637 for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
638 UI != UE; ++UI) {
639 const User *U = *UI;
640 if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg)
641 return 0;
642 }
643
644 return Arg;
645 }
646
647 return 0;
648 }
649
650 /// \brief Test if the given module looks interesting to run ARC optimization
651 /// on.
652 static bool ModuleHasARC(const Module &M) {
653 return
654 M.getNamedValue("objc_retain") ||
655 M.getNamedValue("objc_release") ||
656 M.getNamedValue("objc_autorelease") ||
657 M.getNamedValue("objc_retainAutoreleasedReturnValue") ||
658 M.getNamedValue("objc_retainBlock") ||
659 M.getNamedValue("objc_autoreleaseReturnValue") ||
660 M.getNamedValue("objc_autoreleasePoolPush") ||
661 M.getNamedValue("objc_loadWeakRetained") ||
662 M.getNamedValue("objc_loadWeak") ||
663 M.getNamedValue("objc_destroyWeak") ||
664 M.getNamedValue("objc_storeWeak") ||
665 M.getNamedValue("objc_initWeak") ||
666 M.getNamedValue("objc_moveWeak") ||
667 M.getNamedValue("objc_copyWeak") ||
668 M.getNamedValue("objc_retainedObject") ||
669 M.getNamedValue("objc_unretainedObject") ||
670 M.getNamedValue("objc_unretainedPointer");
671 }
672
673 /// \brief Test whether the given pointer, which is an Objective C block
674 /// pointer, does not "escape".
675 ///
676 /// This differs from regular escape analysis in that a use as an
677 /// argument to a call is not considered an escape.
678 ///
679 static bool DoesObjCBlockEscape(const Value *BlockPtr) {
680
681 DEBUG(dbgs() << "DoesObjCBlockEscape: Target: " << *BlockPtr << "\n");
682
683 // Walk the def-use chains.
684 SmallVector Worklist;
685 Worklist.push_back(BlockPtr);
686
687 // Ensure we do not visit any value twice.
688 SmallPtrSet VisitedSet;
689
690 do {
691 const Value *V = Worklist.pop_back_val();
692
693 DEBUG(dbgs() << "DoesObjCBlockEscape: Visiting: " << *V << "\n");
694
695 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
696 UI != UE; ++UI) {
697 const User *UUser = *UI;
698
699 DEBUG(dbgs() << "DoesObjCBlockEscape: User: " << *UUser << "\n");
700
701 // Special - Use by a call (callee or argument) is not considered
702 // to be an escape.
703 switch (GetBasicInstructionClass(UUser)) {
704 case IC_StoreWeak:
705 case IC_InitWeak:
706 case IC_StoreStrong:
707 case IC_Autorelease:
708 case IC_AutoreleaseRV: {
709 DEBUG(dbgs() << "DoesObjCBlockEscape: User copies pointer arguments. "
710 "Block Escapes!\n");
711 // These special functions make copies of their pointer arguments.
712 return true;
713 }
714 case IC_User:
715 case IC_None:
716 // Use by an instruction which copies the value is an escape if the
717 // result is an escape.
718 if (isa(UUser) || isa(UUser) ||
719 isa(UUser) || isa(UUser)) {
720
721 if (!VisitedSet.insert(UUser)) {
722 DEBUG(dbgs() << "DoesObjCBlockEscape: User copies value. Escapes "
723 "if result escapes. Adding to list.\n");
724 Worklist.push_back(UUser);
725 } else {
726 DEBUG(dbgs() << "DoesObjCBlockEscape: Already visited node.\n");
727 }
728 continue;
729 }
730 // Use by a load is not an escape.
731 if (isa(UUser))
732 continue;
733 // Use by a store is not an escape if the use is the address.
734 if (const StoreInst *SI = dyn_cast(UUser))
735 if (V != SI->getValueOperand())
736 continue;
737 break;
738 default:
739 // Regular calls and other stuff are not considered escapes.
740 continue;
741 }
742 // Otherwise, conservatively assume an escape.
743 DEBUG(dbgs() << "DoesObjCBlockEscape: Assuming block escapes.\n");
744 return true;
745 }
746 } while (!Worklist.empty());
747
748 // No escapes found.
749 DEBUG(dbgs() << "DoesObjCBlockEscape: Block does not escape.\n");
750 return false;
751 }
752
753 /// @}
754 ///
755 /// \defgroup ARCAA Extends alias analysis using ObjC specific knowledge.
756 /// @{
757
758 #include "llvm/Analysis/AliasAnalysis.h"
759 #include "llvm/Analysis/Passes.h"
760 #include "llvm/Pass.h"
761
762 namespace {
763 /// \brief This is a simple alias analysis implementation that uses knowledge
764 /// of ARC constructs to answer queries.
765 ///
766 /// TODO: This class could be generalized to know about other ObjC-specific
767 /// tricks. Such as knowing that ivars in the non-fragile ABI are non-aliasing
768 /// even though their offsets are dynamic.
769 class ObjCARCAliasAnalysis : public ImmutablePass,
770 public AliasAnalysis {
771 public:
772 static char ID; // Class identification, replacement for typeinfo
773 ObjCARCAliasAnalysis() : ImmutablePass(ID) {
774 initializeObjCARCAliasAnalysisPass(*PassRegistry::getPassRegistry());
775 }
776
777 private:
778 virtual void initializePass() {
779 InitializeAliasAnalysis(this);
780 }
781
782 /// This method is used when a pass implements an analysis interface through
783 /// multiple inheritance. If needed, it should override this to adjust the
784 /// this pointer as needed for the specified pass info.
785 virtual void *getAdjustedAnalysisPointer(const void *PI) {
786 if (PI == &AliasAnalysis::ID)
787 return static_cast(this);
788 return this;
789 }
790
791 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
792 virtual AliasResult alias(const Location &LocA, const Location &LocB);
793 virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal);
794 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
795 virtual ModRefBehavior getModRefBehavior(const Function *F);
796 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
797 const Location &Loc);
798 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
799 ImmutableCallSite CS2);
800 };
801 } // End of anonymous namespace
802
803 // Register this pass...
804 char ObjCARCAliasAnalysis::ID = 0;
805 INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis, "objc-arc-aa",
806 "ObjC-ARC-Based Alias Analysis", false, true, false)
807
808 ImmutablePass *llvm::createObjCARCAliasAnalysisPass() {
809 return new ObjCARCAliasAnalysis();
810 }
811
812 void
813 ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
814 AU.setPreservesAll();
815 AliasAnalysis::getAnalysisUsage(AU);
816 }
817
818 AliasAnalysis::AliasResult
819 ObjCARCAliasAnalysis::alias(const Location &LocA, const Location &LocB) {
820 if (!EnableARCOpts)
821 return AliasAnalysis::alias(LocA, LocB);
822
823 // First, strip off no-ops, including ObjC-specific no-ops, and try making a
824 // precise alias query.
825 const Value *SA = StripPointerCastsAndObjCCalls(LocA.Ptr);
826 const Value *SB = StripPointerCastsAndObjCCalls(LocB.Ptr);
827 AliasResult Result =
828 AliasAnalysis::alias(Location(SA, LocA.Size, LocA.TBAATag),
829 Location(SB, LocB.Size, LocB.TBAATag));
830 if (Result != MayAlias)
831 return Result;
832
833 // If that failed, climb to the underlying object, including climbing through
834 // ObjC-specific no-ops, and try making an imprecise alias query.
835 const Value *UA = GetUnderlyingObjCPtr(SA);
836 const Value *UB = GetUnderlyingObjCPtr(SB);
837 if (UA != SA || UB != SB) {
838 Result = AliasAnalysis::alias(Location(UA), Location(UB));
839 // We can't use MustAlias or PartialAlias results here because
840 // GetUnderlyingObjCPtr may return an offsetted pointer value.
841 if (Result == NoAlias)
842 return NoAlias;
843 }
844
845 // If that failed, fail. We don't need to chain here, since that's covered
846 // by the earlier precise query.
847 return MayAlias;
848 }
849
850 bool
851 ObjCARCAliasAnalysis::pointsToConstantMemory(const Location &Loc,
852 bool OrLocal) {
853 if (!EnableARCOpts)
854 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
855
856 // First, strip off no-ops, including ObjC-specific no-ops, and try making
857 // a precise alias query.
858 const Value *S = StripPointerCastsAndObjCCalls(Loc.Ptr);
859 if (AliasAnalysis::pointsToConstantMemory(Location(S, Loc.Size, Loc.TBAATag),
860 OrLocal))
861 return true;
862
863 // If that failed, climb to the underlying object, including climbing through
864 // ObjC-specific no-ops, and try making an imprecise alias query.
865 const Value *U = GetUnderlyingObjCPtr(S);
866 if (U != S)
867 return AliasAnalysis::pointsToConstantMemory(Location(U), OrLocal);
868
869 // If that failed, fail. We don't need to chain here, since that's covered
870 // by the earlier precise query.
871 return false;
872 }
873
874 AliasAnalysis::ModRefBehavior
875 ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
876 // We have nothing to do. Just chain to the next AliasAnalysis.
877 return AliasAnalysis::getModRefBehavior(CS);
878 }
879
880 AliasAnalysis::ModRefBehavior
881 ObjCARCAliasAnalysis::getModRefBehavior(const Function *F) {
882 if (!EnableARCOpts)
883 return AliasAnalysis::getModRefBehavior(F);
884
885 switch (GetFunctionClass(F)) {
886 case IC_NoopCast:
887 return DoesNotAccessMemory;
888 default:
889 break;
890 }
891
892 return AliasAnalysis::getModRefBehavior(F);
893 }
894
895 AliasAnalysis::ModRefResult
896 ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS, const Location &Loc) {
897 if (!EnableARCOpts)
898 return AliasAnalysis::getModRefInfo(CS, Loc);
899
900 switch (GetBasicInstructionClass(CS.getInstruction())) {
901 case IC_Retain:
902 case IC_RetainRV:
903 case IC_Autorelease:
904 case IC_AutoreleaseRV:
905 case IC_NoopCast:
906 case IC_AutoreleasepoolPush:
907 case IC_FusedRetainAutorelease:
908 case IC_FusedRetainAutoreleaseRV:
909 // These functions don't access any memory visible to the compiler.
910 // Note that this doesn't include objc_retainBlock, because it updates
911 // pointers when it copies block data.
912 return NoModRef;
913 default:
914 break;
915 }
916
917 return AliasAnalysis::getModRefInfo(CS, Loc);
918 }
919
920 AliasAnalysis::ModRefResult
921 ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
922 ImmutableCallSite CS2) {
923 // TODO: Theoretically we could check for dependencies between objc_* calls
924 // and OnlyAccessesArgumentPointees calls or other well-behaved calls.
925 return AliasAnalysis::getModRefInfo(CS1, CS2);
926 }
927
928 /// @}
929 ///
930 /// \defgroup ARCExpansion Early ARC Optimizations.
931 /// @{
932
933 #include "llvm/Support/InstIterator.h"
934 #include "llvm/Transforms/ObjCARC.h"
935
936 namespace {
937 /// \brief Early ARC transformations.
938 class ObjCARCExpand : public FunctionPass {
939 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
940 virtual bool doInitialization(Module &M);
941 virtual bool runOnFunction(Function &F);
942
943 /// A flag indicating whether this optimization pass should run.
944 bool Run;
945
946 public:
947 static char ID;
948 ObjCARCExpand() : FunctionPass(ID) {
949 initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
950 }
951 };
952 }
953
954 char ObjCARCExpand::ID = 0;
955 INITIALIZE_PASS(ObjCARCExpand,
956 "objc-arc-expand", "ObjC ARC expansion", false, false)
957
958 Pass *llvm::createObjCARCExpandPass() {
959 return new ObjCARCExpand();
960 }
961
962 void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
963 AU.setPreservesCFG();
964 }
965
966 bool ObjCARCExpand::doInitialization(Module &M) {
967 Run = ModuleHasARC(M);
968 return false;
969 }
970
971 bool ObjCARCExpand::runOnFunction(Function &F) {
972 if (!EnableARCOpts)
973 return false;
974
975 // If nothing in the Module uses ARC, don't do anything.
976 if (!Run)
977 return false;
978
979 bool Changed = false;
980
981 DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName() << "\n");
982
983 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
984 Instruction *Inst = &*I;
985
986 DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
987
988 switch (GetBasicInstructionClass(Inst)) {
989 case IC_Retain:
990 case IC_RetainRV:
991 case IC_Autorelease:
992 case IC_AutoreleaseRV:
993 case IC_FusedRetainAutorelease:
994 case IC_FusedRetainAutoreleaseRV: {
995 // These calls return their argument verbatim, as a low-level
996 // optimization. However, this makes high-level optimizations
997 // harder. Undo any uses of this optimization that the front-end
998 // emitted here. We'll redo them in the contract pass.
999 Changed = true;
1000 Value *Value = cast(Inst)->getArgOperand(0);
1001 DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n"
1002 " New = " << *Value << "\n");
1003 Inst->replaceAllUsesWith(Value);
1004 break;
1005 }
1006 default:
1007 break;
1008 }
1009 }
1010
1011 DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
1012
1013 return Changed;
1014 }
1015
1016 /// @}
1017 ///
1018 /// \defgroup ARCAPElim ARC Autorelease Pool Elimination.
1019 /// @{
1020
1021 #include "llvm/ADT/STLExtras.h"
1022 #include "llvm/IR/Constants.h"
1023
1024 namespace {
1025 /// \brief Autorelease pool elimination.
1026 class ObjCARCAPElim : public ModulePass {
1027 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
1028 virtual bool runOnModule(Module &M);
1029
1030 static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0);
1031 static bool OptimizeBB(BasicBlock *BB);
1032
1033 public:
1034 static char ID;
1035 ObjCARCAPElim() : ModulePass(ID) {
1036 initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
1037 }
1038 };
1039 }
1040
1041 char ObjCARCAPElim::ID = 0;
1042 INITIALIZE_PASS(ObjCARCAPElim,
1043 "objc-arc-apelim",
1044 "ObjC ARC autorelease pool elimination",
1045 false, false)
1046
1047 Pass *llvm::createObjCARCAPElimPass() {
1048 return new ObjCARCAPElim();
1049 }
1050
1051 void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
1052 AU.setPreservesCFG();
1053 }
1054
1055 /// Interprocedurally determine if calls made by the given call site can
1056 /// possibly produce autoreleases.
1057 bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) {
1058 if (const Function *Callee = CS.getCalledFunction()) {
1059 if (Callee->isDeclaration() || Callee->mayBeOverridden())
1060 return true;
1061 for (Function::const_iterator I = Callee->begin(), E = Callee->end();
1062 I != E; ++I) {
1063 const BasicBlock *BB = I;
1064 for (BasicBlock::const_iterator J = BB->begin(), F = BB->end();
1065 J != F; ++J)
1066 if (ImmutableCallSite JCS = ImmutableCallSite(J))
1067 // This recursion depth limit is arbitrary. It's just great
1068 // enough to cover known interesting testcases.
1069 if (Depth < 3 &&
1070 !JCS.onlyReadsMemory() &&
1071 MayAutorelease(JCS, Depth + 1))
1072 return true;
1073 }
1074 return false;
1075 }
1076
1077 return true;
1078 }
1079
1080 bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) {
1081 bool Changed = false;
1082
1083 Instruction *Push = 0;
1084 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1085 Instruction *Inst = I++;
1086 switch (GetBasicInstructionClass(Inst)) {
1087 case IC_AutoreleasepoolPush:
1088 Push = Inst;
1089 break;
1090 case IC_AutoreleasepoolPop:
1091 // If this pop matches a push and nothing in between can autorelease,
1092 // zap the pair.
1093 if (Push && cast(Inst)->getArgOperand(0) == Push) {
1094 Changed = true;
1095 DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop "
1096 "autorelease pair:\n"
1097 " Pop: " << *Inst << "\n"
1098 << " Push: " << *Push << "\n");
1099 Inst->eraseFromParent();
1100 Push->eraseFromParent();
1101 }
1102 Push = 0;
1103 break;
1104 case IC_CallOrUser:
1105 if (MayAutorelease(ImmutableCallSite(Inst)))
1106 Push = 0;
1107 break;
1108 default:
1109 break;
1110 }
1111 }
1112
1113 return Changed;
1114 }
1115
1116 bool ObjCARCAPElim::runOnModule(Module &M) {
1117 if (!EnableARCOpts)
1118 return false;
1119
1120 // If nothing in the Module uses ARC, don't do anything.
1121 if (!ModuleHasARC(M))
1122 return false;
1123
1124 // Find the llvm.global_ctors variable, as the first step in
1125 // identifying the global constructors. In theory, unnecessary autorelease
1126 // pools could occur anywhere, but in practice it's pretty rare. Global
1127 // ctors are a place where autorelease pools get inserted automatically,
1128 // so it's pretty common for them to be unnecessary, and it's pretty
1129 // profitable to eliminate them.
1130 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
1131 if (!GV)
1132 return false;
1133
1134 assert(GV->hasDefinitiveInitializer() &&
1135 "llvm.global_ctors is uncooperative!");
1136
1137 bool Changed = false;
1138
1139 // Dig the constructor functions out of GV's initializer.
1140 ConstantArray *Init = cast(GV->getInitializer());
1141 for (User::op_iterator OI = Init->op_begin(), OE = Init->op_end();
1142 OI != OE; ++OI) {
1143 Value *Op = *OI;
1144 // llvm.global_ctors is an array of pairs where the second members
1145 // are constructor functions.
1146 Function *F = dyn_cast(cast(Op)->getOperand(1));
1147 // If the user used a constructor function with the wrong signature and
1148 // it got bitcasted or whatever, look the other way.
1149 if (!F)
1150 continue;
1151 // Only look at function definitions.
1152 if (F->isDeclaration())
1153 continue;
1154 // Only look at functions with one basic block.
1155 if (llvm::next(F->begin()) != F->end())
1156 continue;
1157 // Ok, a single-block constructor function definition. Try to optimize it.
1158 Changed |= OptimizeBB(F->begin());
1159 }
1160
1161 return Changed;
1162 }
1163
1164 /// @}
1165 ///
1166 /// \defgroup ARCOpt ARC Optimization.
1167 /// @{
1168
1169 // TODO: On code like this:
1170 //
1171 // objc_retain(%x)
1172 // stuff_that_cannot_release()
1173 // objc_autorelease(%x)
1174 // stuff_that_cannot_release()
1175 // objc_retain(%x)
1176 // stuff_that_cannot_release()
1177 // objc_autorelease(%x)
1178 //
1179 // The second retain and autorelease can be deleted.
1180
1181 // TODO: It should be possible to delete
1182 // objc_autoreleasePoolPush and objc_autoreleasePoolPop
1183 // pairs if nothing is actually autoreleased between them. Also, autorelease
1184 // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
1185 // after inlining) can be turned into plain release calls.
1186
1187 // TODO: Critical-edge splitting. If the optimial insertion point is
1188 // a critical edge, the current algorithm has to fail, because it doesn't
1189 // know how to split edges. It should be possible to make the optimizer
1190 // think in terms of edges, rather than blocks, and then split critical
1191 // edges on demand.
1192
1193 // TODO: OptimizeSequences could generalized to be Interprocedural.
1194
1195 // TODO: Recognize that a bunch of other objc runtime calls have
1196 // non-escaping arguments and non-releasing arguments, and may be
1197 // non-autoreleasing.
1198
1199 // TODO: Sink autorelease calls as far as possible. Unfortunately we
1200 // usually can't sink them past other calls, which would be the main
1201 // case where it would be useful.
1202
1203 // TODO: The pointer returned from objc_loadWeakRetained is retained.
1204
1205 // TODO: Delete release+retain pairs (rare).
1206
1207 #include "llvm/ADT/SmallPtrSet.h"
1208 #include "llvm/ADT/Statistic.h"
1209 #include "llvm/IR/LLVMContext.h"
1210 #include "llvm/Support/CFG.h"
1211
1212 STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
1213 STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
1214 STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
1215 STATISTIC(NumRets, "Number of return value forwarding "
1216 "retain+autoreleaes eliminated");
1217 STATISTIC(NumRRs, "Number of retain+release paths eliminated");
1218 STATISTIC(NumPeeps, "Number of calls peephole-optimized");
1219
1220 namespace {
1221 /// \brief This is similar to BasicAliasAnalysis, and it uses many of the same
1222 /// techniques, except it uses special ObjC-specific reasoning about pointer
1223 /// relationships.
1224 ///
1225 /// In this context ``Provenance'' is defined as the history of an object's
1226 /// ownership. Thus ``Provenance Analysis'' is defined by using the notion of
1227 /// an ``independent provenance source'' of a pointer to determine whether or
1228 /// not two pointers have the same provenance source and thus could
1229 /// potentially be related.
1230 class ProvenanceAnalysis {
1231 AliasAnalysis *AA;
1232
1233 typedef std::pair ValuePairTy;
1234 typedef DenseMap CachedResultsTy;
1235 CachedResultsTy CachedResults;
1236
1237 bool relatedCheck(const Value *A, const Value *B);
1238 bool relatedSelect(const SelectInst *A, const Value *B);
1239 bool relatedPHI(const PHINode *A, const Value *B);
1240
1241 void operator=(const ProvenanceAnalysis &) LLVM_DELETED_FUNCTION;
1242 ProvenanceAnalysis(const ProvenanceAnalysis &) LLVM_DELETED_FUNCTION;
1243
1244 public:
1245 ProvenanceAnalysis() {}
1246
1247 void setAA(AliasAnalysis *aa) { AA = aa; }
1248
1249 AliasAnalysis *getAA() const { return AA; }
1250
1251 bool related(const Value *A, const Value *B);
1252
1253 void clear() {
1254 CachedResults.clear();
1255 }
1256 };
1257 }
1258
1259 bool ProvenanceAnalysis::relatedSelect(const SelectInst *A, const Value *B) {
1260 // If the values are Selects with the same condition, we can do a more precise
1261 // check: just check for relations between the values on corresponding arms.
1262 if (const SelectInst *SB = dyn_cast(B))
1263 if (A->getCondition() == SB->getCondition())
1264 return related(A->getTrueValue(), SB->getTrueValue()) ||
1265 related(A->getFalseValue(), SB->getFalseValue());
1266
1267 // Check both arms of the Select node individually.
1268 return related(A->getTrueValue(), B) ||
1269 related(A->getFalseValue(), B);
1270 }
1271
1272 bool ProvenanceAnalysis::relatedPHI(const PHINode *A, const Value *B) {
1273 // If the values are PHIs in the same block, we can do a more precise as well
1274 // as efficient check: just check for relations between the values on
1275 // corresponding edges.
1276 if (const PHINode *PNB = dyn_cast(B))
1277 if (PNB->getParent() == A->getParent()) {
1278 for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i)
1279 if (related(A->getIncomingValue(i),
1280 PNB->getIncomingValueForBlock(A->getIncomingBlock(i))))
1281 return true;
1282 return false;
1283 }
1284
1285 // Check each unique source of the PHI node against B.
1286 SmallPtrSet UniqueSrc;
1287 for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i) {
1288 const Value *PV1 = A->getIncomingValue(i);
1289 if (UniqueSrc.insert(PV1) && related(PV1, B))
1290 return true;
1291 }
1292
1293 // All of the arms checked out.
1294 return false;
1295 }
1296
1297 /// Test if the value of P, or any value covered by its provenance, is ever
1298 /// stored within the function (not counting callees).
1299 static bool isStoredObjCPointer(const Value *P) {
1300 SmallPtrSet Visited;
1301 SmallVector Worklist;
1302 Worklist.push_back(P);
1303 Visited.insert(P);
1304 do {
1305 P = Worklist.pop_back_val();
1306 for (Value::const_use_iterator UI = P->use_begin(), UE = P->use_end();
1307 UI != UE; ++UI) {
1308 const User *Ur = *UI;
1309 if (isa(Ur)) {
1310 if (UI.getOperandNo() == 0)
1311 // The pointer is stored.
1312 return true;
1313 // The pointed is stored through.
1314 continue;
1315 }
1316 if (isa(Ur))
1317 // The pointer is passed as an argument, ignore this.
1318 continue;
1319 if (isa(P))
1320 // Assume the worst.
1321 return true;
1322 if (Visited.insert(Ur))
1323 Worklist.push_back(Ur);
1324 }
1325 } while (!Worklist.empty());
1326
1327 // Everything checked out.
1328 return false;
1329 }
1330
1331 bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B) {
1332 // Skip past provenance pass-throughs.
1333 A = GetUnderlyingObjCPtr(A);
1334 B = GetUnderlyingObjCPtr(B);
1335
1336 // Quick check.
1337 if (A == B)
1338 return true;
1339
1340 // Ask regular AliasAnalysis, for a first approximation.
1341 switch (AA->alias(A, B)) {
1342 case AliasAnalysis::NoAlias:
1343 return false;
1344 case AliasAnalysis::MustAlias:
1345 case AliasAnalysis::PartialAlias:
1346 return true;
1347 case AliasAnalysis::MayAlias:
1348 break;
1349 }
1350
1351 bool AIsIdentified = IsObjCIdentifiedObject(A);
1352 bool BIsIdentified = IsObjCIdentifiedObject(B);
1353
1354 // An ObjC-Identified object can't alias a load if it is never locally stored.
1355 if (AIsIdentified) {
1356 // Check for an obvious escape.
1357 if (isa(B))
1358 return isStoredObjCPointer(A);
1359 if (BIsIdentified) {
1360 // Check for an obvious escape.
1361 if (isa(A))
1362 return isStoredObjCPointer(B);
1363 // Both pointers are identified and escapes aren't an evident problem.
1364 return false;
1365 }
1366 } else if (BIsIdentified) {
1367 // Check for an obvious escape.
1368 if (isa(A))
1369 return isStoredObjCPointer(B);
1370 }
1371
1372 // Special handling for PHI and Select.
1373 if (const PHINode *PN = dyn_cast(A))
1374 return relatedPHI(PN, B);
1375 if (const PHINode *PN = dyn_cast(B))
1376 return relatedPHI(PN, A);
1377 if (const SelectInst *S = dyn_cast(A))
1378 return relatedSelect(S, B);
1379 if (const SelectInst *S = dyn_cast(B))
1380 return relatedSelect(S, A);
1381
1382 // Conservative.
1383 return true;
1384 }
1385
1386 bool ProvenanceAnalysis::related(const Value *A, const Value *B) {
1387 // Begin by inserting a conservative value into the map. If the insertion
1388 // fails, we have the answer already. If it succeeds, leave it there until we
1389 // compute the real answer to guard against recursive queries.
1390 if (A > B) std::swap(A, B);
1391 std::pair Pair =
1392 CachedResults.insert(std::make_pair(ValuePairTy(A, B), true));
1393 if (!Pair.second)
1394 return Pair.first->second;
1395
1396 bool Result = relatedCheck(A, B);
1397 CachedResults[ValuePairTy(A, B)] = Result;
1398 return Result;
1399 }
1400
1401 namespace {
1402 /// \enum Sequence
1403 ///
1404 /// \brief A sequence of states that a pointer may go through in which an
1405 /// objc_retain and objc_release are actually needed.
1406 enum Sequence {
1407 S_None,
1408 S_Retain, ///< objc_retain(x)
1409 S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement
1410 S_Use, ///< any use of x
1411 S_Stop, ///< like S_Release, but code motion is stopped
1412 S_Release, ///< objc_release(x)
1413 S_MovableRelease ///< objc_release(x), !clang.imprecise_release
1414 };
1415 }
1416
1417 static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
1418 // The easy cases.
1419 if (A == B)
1420 return A;
1421 if (A == S_None || B == S_None)
1422 return S_None;
1423
1424 if (A > B) std::swap(A, B);
1425 if (TopDown) {
1426 // Choose the side which is further along in the sequence.
1427 if ((A == S_Retain || A == S_CanRelease) &&
1428 (B == S_CanRelease || B == S_Use))
1429 return B;
1430 } else {
1431 // Choose the side which is further along in the sequence.
1432 if ((A == S_Use || A == S_CanRelease) &&
1433 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
1434 return A;
1435 // If both sides are releases, choose the more conservative one.
1436 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
1437 return A;
1438 if (A == S_Release && B == S_MovableRelease)
1439 return A;
1440 }
1441
1442 return S_None;
1443 }
1444
1445 namespace {
1446 /// \brief Unidirectional information about either a
1447 /// retain-decrement-use-release sequence or release-use-decrement-retain
1448 /// reverese sequence.
1449 struct RRInfo {
1450 /// After an objc_retain, the reference count of the referenced
1451 /// object is known to be positive. Similarly, before an objc_release, the
1452 /// reference count of the referenced object is known to be positive. If
1453 /// there are retain-release pairs in code regions where the retain count
1454 /// is known to be positive, they can be eliminated, regardless of any side
1455 /// effects between them.
1456 ///
1457 /// Also, a retain+release pair nested within another retain+release
1458 /// pair all on the known same pointer value can be eliminated, regardless
1459 /// of any intervening side effects.
1460 ///
1461 /// KnownSafe is true when either of these conditions is satisfied.
1462 bool KnownSafe;
1463
1464 /// True if the Calls are objc_retainBlock calls (as opposed to objc_retain
1465 /// calls).
1466 bool IsRetainBlock;
1467
1468 /// True of the objc_release calls are all marked with the "tail" keyword.
1469 bool IsTailCallRelease;
1470
1471 /// If the Calls are objc_release calls and they all have a
1472 /// clang.imprecise_release tag, this is the metadata tag.
1473 MDNode *ReleaseMetadata;
1474
1475 /// For a top-down sequence, the set of objc_retains or
1476 /// objc_retainBlocks. For bottom-up, the set of objc_releases.
1477 SmallPtrSet Calls;
1478
1479 /// The set of optimal insert positions for moving calls in the opposite
1480 /// sequence.
1481 SmallPtrSet ReverseInsertPts;
1482
1483 RRInfo() :
1484 KnownSafe(false), IsRetainBlock(false),
1485 IsTailCallRelease(false),
1486 ReleaseMetadata(0) {}
1487
1488 void clear();
1489 };
1490 }
1491
1492 void RRInfo::clear() {
1493 KnownSafe = false;
1494 IsRetainBlock = false;
1495 IsTailCallRelease = false;
1496 ReleaseMetadata = 0;
1497 Calls.clear();
1498 ReverseInsertPts.clear();
1499 }
1500
1501 namespace {
1502 /// \brief This class summarizes several per-pointer runtime properties which
1503 /// are propogated through the flow graph.
1504 class PtrState {
1505 /// True if the reference count is known to be incremented.
1506 bool KnownPositiveRefCount;
1507
1508 /// True of we've seen an opportunity for partial RR elimination, such as
1509 /// pushing calls into a CFG triangle or into one side of a CFG diamond.
1510 bool Partial;
1511
1512 /// The current position in the sequence.
1513 Sequence Seq : 8;
1514
1515 public:
1516 /// Unidirectional information about the current sequence.
1517 ///
1518 /// TODO: Encapsulate this better.
1519 RRInfo RRI;
1520
1521 PtrState() : KnownPositiveRefCount(false), Partial(false),
1522 Seq(S_None) {}
1523
1524 void SetKnownPositiveRefCount() {
1525 KnownPositiveRefCount = true;
1526 }
1527
1528 void ClearRefCount() {
1529 KnownPositiveRefCount = false;
1530 }
1531
1532 bool IsKnownIncremented() const {
1533 return KnownPositiveRefCount;
1534 }
1535
1536 void SetSeq(Sequence NewSeq) {
1537 Seq = NewSeq;
1538 }
1539
1540 Sequence GetSeq() const {
1541 return Seq;
1542 }
1543
1544 void ClearSequenceProgress() {
1545 ResetSequenceProgress(S_None);
1546 }
1547
1548 void ResetSequenceProgress(Sequence NewSeq) {
1549 Seq = NewSeq;
1550 Partial = false;
1551 RRI.clear();
1552 }
1553
1554 void Merge(const PtrState &Other, bool TopDown);
1555 };
1556 }
1557
1558 void
1559 PtrState::Merge(const PtrState &Other, bool TopDown) {
1560 Seq = MergeSeqs(Seq, Other.Seq, TopDown);
1561 KnownPositiveRefCount = KnownPositiveRefCount && Other.KnownPositiveRefCount;
1562
1563 // We can't merge a plain objc_retain with an objc_retainBlock.
1564 if (RRI.IsRetainBlock != Other.RRI.IsRetainBlock)
1565 Seq = S_None;
1566
1567 // If we're not in a sequence (anymore), drop all associated state.
1568 if (Seq == S_None) {
1569 Partial = false;
1570 RRI.clear();
1571 } else if (Partial || Other.Partial) {
1572 // If we're doing a merge on a path that's previously seen a partial
1573 // merge, conservatively drop the sequence, to avoid doing partial
1574 // RR elimination. If the branch predicates for the two merge differ,
1575 // mixing them is unsafe.
1576 ClearSequenceProgress();
1577 } else {
1578 // Conservatively merge the ReleaseMetadata information.
1579 if (RRI.ReleaseMetadata != Other.RRI.ReleaseMetadata)
1580 RRI.ReleaseMetadata = 0;
1581
1582 RRI.KnownSafe = RRI.KnownSafe && Other.RRI.KnownSafe;
1583 RRI.IsTailCallRelease = RRI.IsTailCallRelease &&
1584 Other.RRI.IsTailCallRelease;
1585 RRI.Calls.insert(Other.RRI.Calls.begin(), Other.RRI.Calls.end());
1586
1587 // Merge the insert point sets. If there are any differences,
1588 // that makes this a partial merge.
1589 Partial = RRI.ReverseInsertPts.size() != Other.RRI.ReverseInsertPts.size();
1590 for (SmallPtrSet::const_iterator
1591 I = Other.RRI.ReverseInsertPts.begin(),
1592 E = Other.RRI.ReverseInsertPts.end(); I != E; ++I)
1593 Partial |= RRI.ReverseInsertPts.insert(*I);
1594 }
1595 }
1596
1597 namespace {
1598 /// \brief Per-BasicBlock state.
1599 class BBState {
1600 /// The number of unique control paths from the entry which can reach this
1601 /// block.
1602 unsigned TopDownPathCount;
1603
1604 /// The number of unique control paths to exits from this block.
1605 unsigned BottomUpPathCount;
1606
1607 /// A type for PerPtrTopDown and PerPtrBottomUp.
1608 typedef MapVector MapTy;
1609
1610 /// The top-down traversal uses this to record information known about a
1611 /// pointer at the bottom of each block.
1612 MapTy PerPtrTopDown;
1613
1614 /// The bottom-up traversal uses this to record information known about a
1615 /// pointer at the top of each block.
1616 MapTy PerPtrBottomUp;
1617
1618 /// Effective predecessors of the current block ignoring ignorable edges and
1619 /// ignored backedges.
1620 SmallVector Preds;
1621 /// Effective successors of the current block ignoring ignorable edges and
1622 /// ignored backedges.
1623 SmallVector Succs;
1624
1625 public:
1626 BBState() : TopDownPathCount(0), BottomUpPathCount(0) {}
1627
1628 typedef MapTy::iterator ptr_iterator;
1629 typedef MapTy::const_iterator ptr_const_iterator;
1630
1631 ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
1632 ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
1633 ptr_const_iterator top_down_ptr_begin() const {
1634 return PerPtrTopDown.begin();
1635 }
1636 ptr_const_iterator top_down_ptr_end() const {
1637 return PerPtrTopDown.end();
1638 }
1639
1640 ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); }
1641 ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
1642 ptr_const_iterator bottom_up_ptr_begin() const {
1643 return PerPtrBottomUp.begin();
1644 }
1645 ptr_const_iterator bottom_up_ptr_end() const {
1646 return PerPtrBottomUp.end();
1647 }
1648
1649 /// Mark this block as being an entry block, which has one path from the
1650 /// entry by definition.
1651 void SetAsEntry() { TopDownPathCount = 1; }
1652
1653 /// Mark this block as being an exit block, which has one path to an exit by
1654 /// definition.
1655 void SetAsExit() { BottomUpPathCount = 1; }
1656
1657 PtrState &getPtrTopDownState(const Value *Arg) {
1658 return PerPtrTopDown[Arg];
1659 }
1660
1661 PtrState &getPtrBottomUpState(const Value *Arg) {
1662 return PerPtrBottomUp[Arg];
1663 }
1664
1665 void clearBottomUpPointers() {
1666 PerPtrBottomUp.clear();
1667 }
1668
1669 void clearTopDownPointers() {
1670 PerPtrTopDown.clear();
1671 }
1672
1673 void InitFromPred(const BBState &Other);
1674 void InitFromSucc(const BBState &Other);
1675 void MergePred(const BBState &Other);
1676 void MergeSucc(const BBState &Other);
1677
1678 /// Return the number of possible unique paths from an entry to an exit
1679 /// which pass through this block. This is only valid after both the
1680 /// top-down and bottom-up traversals are complete.
1681 unsigned GetAllPathCount() const {
1682 assert(TopDownPathCount != 0);
1683 assert(BottomUpPathCount != 0);
1684 return TopDownPathCount * BottomUpPathCount;
1685 }
1686
1687 // Specialized CFG utilities.
1688 typedef SmallVectorImpl::const_iterator edge_iterator;
1689 edge_iterator pred_begin() { return Preds.begin(); }
1690 edge_iterator pred_end() { return Preds.end(); }
1691 edge_iterator succ_begin() { return Succs.begin(); }
1692 edge_iterator succ_end() { return Succs.end(); }
1693
1694 void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
1695 void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
1696
1697 bool isExit() const { return Succs.empty(); }
1698 };
1699 }
1700
1701 void BBState::InitFromPred(const BBState &Other) {
1702 PerPtrTopDown = Other.PerPtrTopDown;
1703 TopDownPathCount = Other.TopDownPathCount;
1704 }
1705
1706 void BBState::InitFromSucc(const BBState &Other) {
1707 PerPtrBottomUp = Other.PerPtrBottomUp;
1708 BottomUpPathCount = Other.BottomUpPathCount;
1709 }
1710
1711 /// The top-down traversal uses this to merge information about predecessors to
1712 /// form the initial state for a new block.
1713 void BBState::MergePred(const BBState &Other) {
1714 // Other.TopDownPathCount can be 0, in which case it is either dead or a
1715 // loop backedge. Loop backedges are special.
1716 TopDownPathCount += Other.TopDownPathCount;
1717
1718 // Check for overflow. If we have overflow, fall back to conservative
1719 // behavior.
1720 if (TopDownPathCount < Other.TopDownPathCount) {
1721 clearTopDownPointers();
1722 return;
1723 }
1724
1725 // For each entry in the other set, if our set has an entry with the same key,
1726 // merge the entries. Otherwise, copy the entry and merge it with an empty
1727 // entry.
1728 for (ptr_const_iterator MI = Other.top_down_ptr_begin(),
1729 ME = Other.top_down_ptr_end(); MI != ME; ++MI) {
1730 std::pair Pair = PerPtrTopDown.insert(*MI);
1731 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
1732 /*TopDown=*/true);
1733 }
1734
1735 // For each entry in our set, if the other set doesn't have an entry with the
1736 // same key, force it to merge with an empty entry.
1737 for (ptr_iterator MI = top_down_ptr_begin(),
1738 ME = top_down_ptr_end(); MI != ME; ++MI)
1739 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
1740 MI->second.Merge(PtrState(), /*TopDown=*/true);
1741 }
1742
1743 /// The bottom-up traversal uses this to merge information about successors to
1744 /// form the initial state for a new block.
1745 void BBState::MergeSucc(const BBState &Other) {
1746 // Other.BottomUpPathCount can be 0, in which case it is either dead or a
1747 // loop backedge. Loop backedges are special.
1748 BottomUpPathCount += Other.BottomUpPathCount;
1749
1750 // Check for overflow. If we have overflow, fall back to conservative
1751 // behavior.
1752 if (BottomUpPathCount < Other.BottomUpPathCount) {
1753 clearBottomUpPointers();
1754 return;
1755 }
1756
1757 // For each entry in the other set, if our set has an entry with the
1758 // same key, merge the entries. Otherwise, copy the entry and merge
1759 // it with an empty entry.
1760 for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(),
1761 ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) {
1762 std::pair Pair = PerPtrBottomUp.insert(*MI);
1763 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
1764 /*TopDown=*/false);
1765 }
1766
1767 // For each entry in our set, if the other set doesn't have an entry
1768 // with the same key, force it to merge with an empty entry.
1769 for (ptr_iterator MI = bottom_up_ptr_begin(),
1770 ME = bottom_up_ptr_end(); MI != ME; ++MI)
1771 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
1772 MI->second.Merge(PtrState(), /*TopDown=*/false);
1773 }
1774
1775 namespace {
1776 /// \brief The main ARC optimization pass.
1777 class ObjCARCOpt : public FunctionPass {
1778 bool Changed;
1779 ProvenanceAnalysis PA;
1780
1781 /// A flag indicating whether this optimization pass should run.
1782 bool Run;
1783
1784 /// Declarations for ObjC runtime functions, for use in creating calls to
1785 /// them. These are initialized lazily to avoid cluttering up the Module
1786 /// with unused declarations.
1787
1788 /// Declaration for ObjC runtime function
1789 /// objc_retainAutoreleasedReturnValue.
1790 Constant *RetainRVCallee;
1791 /// Declaration for ObjC runtime function objc_autoreleaseReturnValue.
1792 Constant *AutoreleaseRVCallee;
1793 /// Declaration for ObjC runtime function objc_release.
1794 Constant *ReleaseCallee;
1795 /// Declaration for ObjC runtime function objc_retain.
1796 Constant *RetainCallee;
1797 /// Declaration for ObjC runtime function objc_retainBlock.
1798 Constant *RetainBlockCallee;
1799 /// Declaration for ObjC runtime function objc_autorelease.
1800 Constant *AutoreleaseCallee;
1801
1802 /// Flags which determine whether each of the interesting runtine functions
1803 /// is in fact used in the current function.
1804 unsigned UsedInThisFunction;
1805
1806 /// The Metadata Kind for clang.imprecise_release metadata.
1807 unsigned ImpreciseReleaseMDKind;
1808
1809 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
1810 unsigned CopyOnEscapeMDKind;
1811
1812 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
1813 unsigned NoObjCARCExceptionsMDKind;
1814
1815 Constant *getRetainRVCallee(Module *M);
1816 Constant *getAutoreleaseRVCallee(Module *M);
1817 Constant *getReleaseCallee(Module *M);
1818 Constant *getRetainCallee(Module *M);
1819 Constant *getRetainBlockCallee(Module *M);
1820 Constant *getAutoreleaseCallee(Module *M);
1821
1822 bool IsRetainBlockOptimizable(const Instruction *Inst);
1823
1824 void OptimizeRetainCall(Function &F, Instruction *Retain);
1825 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
1826 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1827 InstructionClass &Class);
1828 void OptimizeIndividualCalls(Function &F);
1829
1830 void CheckForCFGHazards(const BasicBlock *BB,
1831 DenseMap &BBStates,
1832 BBState &MyStates) const;
1833 bool VisitInstructionBottomUp(Instruction *Inst,
1834 BasicBlock *BB,
1835 MapVector &Retains,
1836 BBState &MyStates);
1837 bool VisitBottomUp(BasicBlock *BB,
1838 DenseMap &BBStates,
1839 MapVector &Retains);
1840 bool VisitInstructionTopDown(Instruction *Inst,
1841 DenseMap &Releases,
1842 BBState &MyStates);
1843 bool VisitTopDown(BasicBlock *BB,
1844 DenseMap &BBStates,
1845 DenseMap &Releases);
1846 bool Visit(Function &F,
1847 DenseMap &BBStates,
1848 MapVector &Retains,
1849 DenseMap &Releases);
1850
1851 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
1852 MapVector &Retains,
1853 DenseMap &Releases,
1854 SmallVectorImpl &DeadInsts,
1855 Module *M);
1856
1857 bool ConnectTDBUTraversals(DenseMap &BBStates,
1858 MapVector &Retains,
1859 DenseMap &Releases,
1860 Module *M,
1861 SmallVector &NewRetains,
1862 SmallVector &NewReleases,
1863 SmallVector &DeadInsts,
1864 RRInfo &RetainsToMove,
1865 RRInfo &ReleasesToMove,
1866 Value *Arg,
1867 bool KnownSafe,
1868 bool &AnyPairsCompletelyEliminated);
1869
1870 bool PerformCodePlacement(DenseMap &BBStates,
1871 MapVector &Retains,
1872 DenseMap &Releases,
1873 Module *M);
1874
1875 void OptimizeWeakCalls(Function &F);
1876
1877 bool OptimizeSequences(Function &F);
1878
1879 void OptimizeReturns(Function &F);
1880
1881 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
1882 virtual bool doInitialization(Module &M);
1883 virtual bool runOnFunction(Function &F);
1884 virtual void releaseMemory();
1885
1886 public:
1887 static char ID;
1888 ObjCARCOpt() : FunctionPass(ID) {
1889 initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
1890 }
1891 };
1892 }
1893
1894 char ObjCARCOpt::ID = 0;
1895 INITIALIZE_PASS_BEGIN(ObjCARCOpt,
1896 "objc-arc", "ObjC ARC optimization", false, false)
1897 INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
1898 INITIALIZE_PASS_END(ObjCARCOpt,
1899 "objc-arc", "ObjC ARC optimization", false, false)
1900
1901 Pass *llvm::createObjCARCOptPass() {
1902 return new ObjCARCOpt();
1903 }
1904
1905 void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
1906 AU.addRequired();
1907 AU.addRequired();
1908 // ARC optimization doesn't currently split critical edges.
1909 AU.setPreservesCFG();
1910 }
1911
1912 bool ObjCARCOpt::IsRetainBlockOptimizable(const Instruction *Inst) {
1913 // Without the magic metadata tag, we have to assume this might be an
1914 // objc_retainBlock call inserted to convert a block pointer to an id,
1915 // in which case it really is needed.
1916 if (!Inst->getMetadata(CopyOnEscapeMDKind))
1917 return false;
1918
1919 // If the pointer "escapes" (not including being used in a call),
1920 // the copy may be needed.
1921 if (DoesObjCBlockEscape(Inst))
1922 return false;
1923
1924 // Otherwise, it's not needed.
1925 return true;
1926 }
1927
1928 Constant *ObjCARCOpt::getRetainRVCallee(Module *M) {
1929 if (!RetainRVCallee) {
1930 LLVMContext &C = M->getContext();
1931 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
1932 Type *Params[] = { I8X };
1933 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
1934 AttributeSet Attribute =
1935 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
1936 Attribute::NoUnwind);
1937 RetainRVCallee =
1938 M->getOrInsertFunction("objc_retainAutoreleasedReturnValue", FTy,
1939 Attribute);
1940 }
1941 return RetainRVCallee;
1942 }
1943
1944 Constant *ObjCARCOpt::getAutoreleaseRVCallee(Module *M) {
1945 if (!AutoreleaseRVCallee) {
1946 LLVMContext &C = M->getContext();
1947 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
1948 Type *Params[] = { I8X };
1949 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
1950 AttributeSet Attribute =
1951 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
1952 Attribute::NoUnwind);
1953 AutoreleaseRVCallee =
1954 M->getOrInsertFunction("objc_autoreleaseReturnValue", FTy,
1955 Attribute);
1956 }
1957 return AutoreleaseRVCallee;
1958 }
1959
1960 Constant *ObjCARCOpt::getReleaseCallee(Module *M) {
1961 if (!ReleaseCallee) {
1962 LLVMContext &C = M->getContext();
1963 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
1964 AttributeSet Attribute =
1965 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
1966 Attribute::NoUnwind);
1967 ReleaseCallee =
1968 M->getOrInsertFunction(
1969 "objc_release",
1970 FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
1971 Attribute);
1972 }
1973 return ReleaseCallee;
1974 }
1975
1976 Constant *ObjCARCOpt::getRetainCallee(Module *M) {
1977 if (!RetainCallee) {
1978 LLVMContext &C = M->getContext();
1979 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
1980 AttributeSet Attribute =
1981 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
1982 Attribute::NoUnwind);
1983 RetainCallee =
1984 M->getOrInsertFunction(
1985 "objc_retain",
1986 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
1987 Attribute);
1988 }
1989 return RetainCallee;
1990 }
1991
1992 Constant *ObjCARCOpt::getRetainBlockCallee(Module *M) {
1993 if (!RetainBlockCallee) {
1994 LLVMContext &C = M->getContext();
1995 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
1996 // objc_retainBlock is not nounwind because it calls user copy constructors
1997 // which could theoretically throw.
1998 RetainBlockCallee =
1999 M->getOrInsertFunction(
2000 "objc_retainBlock",
2001 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
2002 AttributeSet());
2003 }
2004 return RetainBlockCallee;
2005 }
2006
2007 Constant *ObjCARCOpt::getAutoreleaseCallee(Module *M) {
2008 if (!AutoreleaseCallee) {
2009 LLVMContext &C = M->getContext();
2010 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
2011 AttributeSet Attribute =
2012 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
2013 Attribute::NoUnwind);
2014 AutoreleaseCallee =
2015 M->getOrInsertFunction(
2016 "objc_autorelease",
2017 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
2018 Attribute);
2019 }
2020 return AutoreleaseCallee;
2021 }
2022
2023 /// Test whether the given value is possible a reference-counted pointer,
2024 /// including tests which utilize AliasAnalysis.
2025 static bool IsPotentialRetainableObjPtr(const Value *Op, AliasAnalysis &AA) {
2026 // First make the rudimentary check.
2027 if (!IsPotentialRetainableObjPtr(Op))
2028 return false;
2029
2030 // Objects in constant memory are not reference-counted.
2031 if (AA.pointsToConstantMemory(Op))
2032 return false;
2033
2034 // Pointers in constant memory are not pointing to reference-counted objects.
2035 if (const LoadInst *LI = dyn_cast(Op))
2036 if (AA.pointsToConstantMemory(LI->getPointerOperand()))
2037 return false;
2038
2039 // Otherwise assume the worst.
2040 return true;
2041 }
2042
2043 /// Test whether the given instruction can result in a reference count
2044 /// modification (positive or negative) for the pointer's object.
2045 static bool
2046 CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
2047 ProvenanceAnalysis &PA, InstructionClass Class) {
2048 switch (Class) {
2049 case IC_Autorelease:
2050 case IC_AutoreleaseRV:
2051 case IC_User:
2052 // These operations never directly modify a reference count.
2053 return false;
2054 default: break;
2055 }
2056
2057 ImmutableCallSite CS = static_cast(Inst);
2058 assert(CS && "Only calls can alter reference counts!");
2059
2060 // See if AliasAnalysis can help us with the call.
2061 AliasAnalysis::ModRefBehavior MRB = PA.getAA()->getModRefBehavior(CS);
2062 if (AliasAnalysis::onlyReadsMemory(MRB))
2063 return false;
2064 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
2065 for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
2066 I != E; ++I) {
2067 const Value *Op = *I;
2068 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op))
2069 return true;
2070 }
2071 return false;
2072 }
2073
2074 // Assume the worst.
2075 return true;
2076 }
2077
2078 /// Test whether the given instruction can "use" the given pointer's object in a
2079 /// way that requires the reference count to be positive.
2080 static bool
2081 CanUse(const Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA,
2082 InstructionClass Class) {
2083 // IC_Call operations (as opposed to IC_CallOrUser) never "use" objc pointers.
2084 if (Class == IC_Call)
2085 return false;
2086
2087 // Consider various instructions which may have pointer arguments which are
2088 // not "uses".
2089 if (const ICmpInst *ICI = dyn_cast(Inst)) {
2090 // Comparing a pointer with null, or any other constant, isn't really a use,
2091 // because we don't care what the pointer points to, or about the values
2092 // of any other dynamic reference-counted pointers.
2093 if (!IsPotentialRetainableObjPtr(ICI->getOperand(1), *PA.getAA()))
2094 return false;
2095 } else if (ImmutableCallSite CS = static_cast(Inst)) {
2096 // For calls, just check the arguments (and not the callee operand).
2097 for (ImmutableCallSite::arg_iterator OI = CS.arg_begin(),
2098 OE = CS.arg_end(); OI != OE; ++OI) {
2099 const Value *Op = *OI;
2100 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op))
2101 return true;
2102 }
2103 return false;
2104 } else if (const StoreInst *SI = dyn_cast(Inst)) {
2105 // Special-case stores, because we don't care about the stored value, just
2106 // the store address.
2107 const Value *Op = GetUnderlyingObjCPtr(SI->getPointerOperand());
2108 // If we can't tell what the underlying object was, assume there is a
2109 // dependence.
2110 return IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Op, Ptr);
2111 }
2112
2113 // Check each operand for a match.
2114 for (User::const_op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
2115 OI != OE; ++OI) {
2116 const Value *Op = *OI;
2117 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op))
2118 return true;
2119 }
2120 return false;
2121 }
2122
2123 /// Test whether the given instruction can autorelease any pointer or cause an
2124 /// autoreleasepool pop.
2125 static bool
2126 CanInterruptRV(InstructionClass Class) {
2127 switch (Class) {
2128 case IC_AutoreleasepoolPop:
2129 case IC_CallOrUser:
2130 case IC_Call:
2131 case IC_Autorelease:
2132 case IC_AutoreleaseRV:
2133 case IC_FusedRetainAutorelease:
2134 case IC_FusedRetainAutoreleaseRV:
2135 return true;
2136 default:
2137 return false;
2138 }
2139 }
2140
2141 namespace {
2142 /// \enum DependenceKind
2143 /// \brief Defines different dependence kinds among various ARC constructs.
2144 ///
2145 /// There are several kinds of dependence-like concepts in use here.
2146 ///
2147 enum DependenceKind {
2148 NeedsPositiveRetainCount,
2149 AutoreleasePoolBoundary,
2150 CanChangeRetainCount,
2151 RetainAutoreleaseDep, ///< Blocks objc_retainAutorelease.
2152 RetainAutoreleaseRVDep, ///< Blocks objc_retainAutoreleaseReturnValue.
2153 RetainRVDep ///< Blocks objc_retainAutoreleasedReturnValue.
2154 };
2155 }
2156
2157 /// Test if there can be dependencies on Inst through Arg. This function only
2158 /// tests dependencies relevant for removing pairs of calls.
2159 static bool
2160 Depends(DependenceKind Flavor, Instruction *Inst, const Value *Arg,
2161 ProvenanceAnalysis &PA) {
2162 // If we've reached the definition of Arg, stop.
2163 if (Inst == Arg)
2164 return true;
2165
2166 switch (Flavor) {
2167 case NeedsPositiveRetainCount: {
2168 InstructionClass Class = GetInstructionClass(Inst);
2169 switch (Class) {
2170 case IC_AutoreleasepoolPop:
2171 case IC_AutoreleasepoolPush:
2172 case IC_None:
2173 return false;
2174 default:
2175 return CanUse(Inst, Arg, PA, Class);
2176 }
2177 }
2178
2179 case AutoreleasePoolBoundary: {
2180 InstructionClass Class = GetInstructionClass(Inst);
2181 switch (Class) {
2182 case IC_AutoreleasepoolPop:
2183 case IC_AutoreleasepoolPush:
2184 // These mark the end and begin of an autorelease pool scope.
2185 return true;
2186 default:
2187 // Nothing else does this.
2188 return false;
2189 }
2190 }
2191
2192 case CanChangeRetainCount: {
2193 InstructionClass Class = GetInstructionClass(Inst);
2194 switch (Class) {
2195 case IC_AutoreleasepoolPop:
2196 // Conservatively assume this can decrement any count.
2197 return true;
2198 case IC_AutoreleasepoolPush:
2199 case IC_None:
2200 return false;
2201 default:
2202 return CanAlterRefCount(Inst, Arg, PA, Class);
2203 }
2204 }
2205
2206 case RetainAutoreleaseDep:
2207 switch (GetBasicInstructionClass(Inst)) {
2208 case IC_AutoreleasepoolPop:
2209 case IC_AutoreleasepoolPush:
2210 // Don't merge an objc_autorelease with an objc_retain inside a different
2211 // autoreleasepool scope.
2212 return true;
2213 case IC_Retain:
2214 case IC_RetainRV:
2215 // Check for a retain of the same pointer for merging.
2216 return GetObjCArg(Inst) == Arg;
2217 default:
2218 // Nothing else matters for objc_retainAutorelease formation.
2219 return false;
2220 }
2221
2222 case RetainAutoreleaseRVDep: {
2223 InstructionClass Class = GetBasicInstructionClass(Inst);
2224 switch (Class) {
2225 case IC_Retain:
2226 case IC_RetainRV:
2227 // Check for a retain of the same pointer for merging.
2228 return GetObjCArg(Inst) == Arg;
2229 default:
2230 // Anything that can autorelease interrupts
2231 // retainAutoreleaseReturnValue formation.
2232 return CanInterruptRV(Class);
2233 }
2234 }
2235
2236 case RetainRVDep:
2237 return CanInterruptRV(GetBasicInstructionClass(Inst));
2238 }
2239
2240 llvm_unreachable("Invalid dependence flavor");
2241 }
2242
2243 /// Walk up the CFG from StartPos (which is in StartBB) and find local and
2244 /// non-local dependencies on Arg.
2245 ///
2246 /// TODO: Cache results?
2247 static void
2248 FindDependencies(DependenceKind Flavor,
2249 const Value *Arg,
2250 BasicBlock *StartBB, Instruction *StartInst,
2251 SmallPtrSet &DependingInstructions,
2252 SmallPtrSet &Visited,
2253 ProvenanceAnalysis &PA) {
2254 BasicBlock::iterator StartPos = StartInst;
2255
2256 SmallVector, 4> Worklist;
2257 Worklist.push_back(std::make_pair(StartBB, StartPos));
2258 do {
2259 std::pair Pair =
2260 Worklist.pop_back_val();
2261 BasicBlock *LocalStartBB = Pair.first;
2262 BasicBlock::iterator LocalStartPos = Pair.second;
2263 BasicBlock::iterator StartBBBegin = LocalStartBB->begin();
2264 for (;;) {
2265 if (LocalStartPos == StartBBBegin) {
2266 pred_iterator PI(LocalStartBB), PE(LocalStartBB, false);
2267 if (PI == PE)
2268 // If we've reached the function entry, produce a null dependence.
2269 DependingInstructions.insert(0);
2270 else
2271 // Add the predecessors to the worklist.
2272 do {
2273 BasicBlock *PredBB = *PI;
2274 if (Visited.insert(PredBB))
2275 Worklist.push_back(std::make_pair(PredBB, PredBB->end()));
2276 } while (++PI != PE);
2277 break;
2278 }
2279
2280 Instruction *Inst = --LocalStartPos;
2281 if (Depends(Flavor, Inst, Arg, PA)) {
2282 DependingInstructions.insert(Inst);
2283 break;
2284 }
2285 }
2286 } while (!Worklist.empty());
2287
2288 // Determine whether the original StartBB post-dominates all of the blocks we
2289 // visited. If not, insert a sentinal indicating that most optimizations are
2290 // not safe.
2291 for (SmallPtrSet::const_iterator I = Visited.begin(),
2292 E = Visited.end(); I != E; ++I) {
2293 const BasicBlock *BB = *I;
2294 if (BB == StartBB)
2295 continue;
2296 const TerminatorInst *TI = cast(&BB->back());
2297 for (succ_const_iterator SI(TI), SE(TI, false); SI != SE; ++SI) {
2298 const BasicBlock *Succ = *SI;
2299 if (Succ != StartBB && !Visited.count(Succ)) {
2300 DependingInstructions.insert(reinterpret_cast(-1));
2301 return;
2302 }
2303 }
2304 }
2305 }
2306
2307 static bool isNullOrUndef(const Value *V) {
2308 return isa(V) || isa(V);
2309 }
2310
2311 static bool isNoopInstruction(const Instruction *I) {
2312 return isa(I) ||
2313 (isa(I) &&
2314 cast(I)->hasAllZeroIndices());
2315 }
2316
2317 /// Turn objc_retain into objc_retainAutoreleasedReturnValue if the operand is a
2318 /// return value.
2319 void
2320 ObjCARCOpt::OptimizeRetainCall(Function &F, Instruction *Retain) {
2321 ImmutableCallSite CS(GetObjCArg(Retain));
2322 const Instruction *Call = CS.getInstruction();
2323 if (!Call) return;
2324 if (Call->getParent() != Retain->getParent()) return;
2325
2326 // Check that the call is next to the retain.
2327 BasicBlock::const_iterator I = Call;
2328 ++I;
2329 while (isNoopInstruction(I)) ++I;
2330 if (&*I != Retain)
2331 return;
2332
2333 // Turn it to an objc_retainAutoreleasedReturnValue..
2334 Changed = true;
2335 ++NumPeeps;
2336
2337 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainCall: Transforming "
2338 "objc_retain => objc_retainAutoreleasedReturnValue"
2339 " since the operand is a return value.\n"
2340 " Old: "
2341 << *Retain << "\n");
2342
2343 cast(Retain)->setCalledFunction(getRetainRVCallee(F.getParent()));
2344
2345 DEBUG(dbgs() << " New: "
2346 << *Retain << "\n");
2347 }
2348
2349 /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
2350 /// not a return value. Or, if it can be paired with an
2351 /// objc_autoreleaseReturnValue, delete the pair and return true.
2352 bool
2353 ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
2354 // Check for the argument being from an immediately preceding call or invoke.
2355 const Value *Arg = GetObjCArg(RetainRV);
2356 ImmutableCallSite CS(Arg);
2357 if (const Instruction *Call = CS.getInstruction()) {
2358 if (Call->getParent() == RetainRV->getParent()) {
2359 BasicBlock::const_iterator I = Call;
2360 ++I;
2361 while (isNoopInstruction(I)) ++I;
2362 if (&*I == RetainRV)
2363 return false;
2364 } else if (const InvokeInst *II = dyn_cast(Call)) {
2365 BasicBlock *RetainRVParent = RetainRV->getParent();
2366 if (II->getNormalDest() == RetainRVParent) {
2367 BasicBlock::const_iterator I = RetainRVParent->begin();
2368 while (isNoopInstruction(I)) ++I;
2369 if (&*I == RetainRV)
2370 return false;
2371 }
2372 }
2373 }
2374
2375 // Check for being preceded by an objc_autoreleaseReturnValue on the same
2376 // pointer. In this case, we can delete the pair.
2377 BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
2378 if (I != Begin) {
2379 do --I; while (I != Begin && isNoopInstruction(I));
2380 if (GetBasicInstructionClass(I) == IC_AutoreleaseRV &&
2381 GetObjCArg(I) == Arg) {
2382 Changed = true;
2383 ++NumPeeps;
2384
2385 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainRVCall: Erasing " << *I << "\n"
2386 << " Erasing " << *RetainRV
2387 << "\n");
2388
2389 EraseInstruction(I);
2390 EraseInstruction(RetainRV);
2391 return true;
2392 }
2393 }
2394
2395 // Turn it to a plain objc_retain.
2396 Changed = true;
2397 ++NumPeeps;
2398
2399 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainRVCall: Transforming "
2400 "objc_retainAutoreleasedReturnValue => "
2401 "objc_retain since the operand is not a return value.\n"
2402 " Old: "
2403 << *RetainRV << "\n");
2404
2405 cast(RetainRV)->setCalledFunction(getRetainCallee(F.getParent()));
2406
2407 DEBUG(dbgs() << " New: "
2408 << *RetainRV << "\n");
2409
2410 return false;
2411 }
2412
2413 /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
2414 /// used as a return value.
2415 void
2416 ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
2417 InstructionClass &Class) {
2418 // Check for a return of the pointer value.
2419 const Value *Ptr = GetObjCArg(AutoreleaseRV);
2420 SmallVector Users;
2421 Users.push_back(Ptr);
2422 do {
2423 Ptr = Users.pop_back_val();
2424 for (Value::const_use_iterator UI = Ptr->use_begin(), UE = Ptr->use_end();
2425 UI != UE; ++UI) {
2426 const User *I = *UI;
2427 if (isa(I) || GetBasicInstructionClass(I) == IC_RetainRV)
2428 return;
2429 if (isa(I))
2430 Users.push_back(I);
2431 }
2432 } while (!Users.empty());
2433
2434 Changed = true;
2435 ++NumPeeps;
2436
2437 DEBUG(dbgs() << "ObjCARCOpt::OptimizeAutoreleaseRVCall: Transforming "
2438 "objc_autoreleaseReturnValue => "
2439 "objc_autorelease since its operand is not used as a return "
2440 "value.\n"
2441 " Old: "
2442 << *AutoreleaseRV << "\n");
2443
2444 CallInst *AutoreleaseRVCI = cast(AutoreleaseRV);
2445 AutoreleaseRVCI->
2446 setCalledFunction(getAutoreleaseCallee(F.getParent()));
2447 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
2448 Class = IC_Autorelease;
2449
2450 DEBUG(dbgs() << " New: "
2451 << *AutoreleaseRV << "\n");
2452
2453 }
2454
2455 /// Visit each call, one at a time, and make simplifications without doing any
2456 /// additional analysis.
2457 void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
2458 // Reset all the flags in preparation for recomputing them.
2459 UsedInThisFunction = 0;
2460
2461 // Visit all objc_* calls in F.
2462 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2463 Instruction *Inst = &*I++;
2464
2465 InstructionClass Class = GetBasicInstructionClass(Inst);
2466
2467 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Visiting: Class: "
2468 << Class << "; " << *Inst << "\n");
2469
2470 switch (Class) {
2471 default: break;
2472
2473 // Delete no-op casts. These function calls have special semantics, but
2474 // the semantics are entirely implemented via lowering in the front-end,
2475 // so by the time they reach the optimizer, they are just no-op calls
2476 // which return their argument.
2477 //
2478 // There are gray areas here, as the ability to cast reference-counted
2479 // pointers to raw void* and back allows code to break ARC assumptions,
2480 // however these are currently considered to be unimportant.
2481 case IC_NoopCast:
2482 Changed = true;
2483 ++NumNoops;
2484 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Erasing no-op cast:"
2485 " " << *Inst << "\n");
2486 EraseInstruction(Inst);
2487 continue;
2488
2489 // If the pointer-to-weak-pointer is null, it's undefined behavior.
2490 case IC_StoreWeak:
2491 case IC_LoadWeak:
2492 case IC_LoadWeakRetained:
2493 case IC_InitWeak:
2494 case IC_DestroyWeak: {
2495 CallInst *CI = cast(Inst);
2496 if (isNullOrUndef(CI->getArgOperand(0))) {
2497 Changed = true;
2498 Type *Ty = CI->getArgOperand(0)->getType();
2499 new StoreInst(UndefValue::get(cast(Ty)->getElementType()),
2500 Constant::getNullValue(Ty),
2501 CI);
2502 llvm::Value *NewValue = UndefValue::get(CI->getType());
2503 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: A null "
2504 "pointer-to-weak-pointer is undefined behavior.\n"
2505 " Old = " << *CI <<
2506 "\n New = " <<
2507 *NewValue << "\n");
2508 CI->replaceAllUsesWith(NewValue);
2509 CI->eraseFromParent();
2510 continue;
2511 }
2512 break;
2513 }
2514 case IC_CopyWeak:
2515 case IC_MoveWeak: {
2516 CallInst *CI = cast(Inst);
2517 if (isNullOrUndef(CI->getArgOperand(0)) ||
2518 isNullOrUndef(CI->getArgOperand(1))) {
2519 Changed = true;
2520 Type *Ty = CI->getArgOperand(0)->getType();
2521 new StoreInst(UndefValue::get(cast(Ty)->getElementType()),
2522 Constant::getNullValue(Ty),
2523 CI);
2524
2525 llvm::Value *NewValue = UndefValue::get(CI->getType());
2526 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: A null "
2527 "pointer-to-weak-pointer is undefined behavior.\n"
2528 " Old = " << *CI <<
2529 "\n New = " <<
2530 *NewValue << "\n");
2531
2532 CI->replaceAllUsesWith(NewValue);
2533 CI->eraseFromParent();
2534 continue;
2535 }
2536 break;
2537 }
2538 case IC_Retain:
2539 OptimizeRetainCall(F, Inst);
2540 break;
2541 case IC_RetainRV:
2542 if (OptimizeRetainRVCall(F, Inst))
2543 continue;
2544 break;
2545 case IC_AutoreleaseRV:
2546 OptimizeAutoreleaseRVCall(F, Inst, Class);
2547 break;
2548 }
2549
2550 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
2551 if (IsAutorelease(Class) && Inst->use_empty()) {
2552 CallInst *Call = cast(Inst);
2553 const Value *Arg = Call->getArgOperand(0);
2554 Arg = FindSingleUseIdentifiedObject(Arg);
2555 if (Arg) {
2556 Changed = true;
2557 ++NumAutoreleases;
2558
2559 // Create the declaration lazily.
2560 LLVMContext &C = Inst->getContext();
2561 CallInst *NewCall =
2562 CallInst::Create(getReleaseCallee(F.getParent()),
2563 Call->getArgOperand(0), "", Call);
2564 NewCall->setMetadata(ImpreciseReleaseMDKind,
2565 MDNode::get(C, ArrayRef()));
2566
2567 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Replacing "
2568 "objc_autorelease(x) with objc_release(x) since x is "
2569 "otherwise unused.\n"
2570 " Old: " << *Call <<
2571 "\n New: " <<
2572 *NewCall << "\n");
2573
2574 EraseInstruction(Call);
2575 Inst = NewCall;
2576 Class = IC_Release;
2577 }
2578 }
2579
2580 // For functions which can never be passed stack arguments, add
2581 // a tail keyword.
2582 if (IsAlwaysTail(Class)) {
2583 Changed = true;
2584 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Adding tail keyword"
2585 " to function since it can never be passed stack args: " << *Inst <<
2586 "\n");
2587 cast(Inst)->setTailCall();
2588 }
2589
2590 // Ensure that functions that can never have a "tail" keyword due to the
2591 // semantics of ARC truly do not do so.
2592 if (IsNeverTail(Class)) {
2593 Changed = true;
2594 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Removing tail "
2595 "keyword from function: " << *Inst <<
2596 "\n");
2597 cast(Inst)->setTailCall(false);
2598 }
2599
2600 // Set nounwind as needed.
2601 if (IsNoThrow(Class)) {
2602 Changed = true;
2603 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Found no throw"
2604 " class. Setting nounwind on: " << *Inst << "\n");
2605 cast(Inst)->setDoesNotThrow();
2606 }
2607
2608 if (!IsNoopOnNull(Class)) {
2609 UsedInThisFunction |= 1 << Class;
2610 continue;
2611 }
2612
2613 const Value *Arg = GetObjCArg(Inst);
2614
2615 // ARC calls with null are no-ops. Delete them.
2616 if (isNullOrUndef(Arg)) {
2617 Changed = true;
2618 ++NumNoops;
2619 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: ARC calls with "
2620 " null are no-ops. Erasing: " << *Inst << "\n");
2621 EraseInstruction(Inst);
2622 continue;
2623 }
2624
2625 // Keep track of which of retain, release, autorelease, and retain_block
2626 // are actually present in this function.
2627 UsedInThisFunction |= 1 << Class;
2628
2629 // If Arg is a PHI, and one or more incoming values to the
2630 // PHI are null, and the call is control-equivalent to the PHI, and there
2631 // are no relevant side effects between the PHI and the call, the call
2632 // could be pushed up to just those paths with non-null incoming values.
2633 // For now, don't bother splitting critical edges for this.
2634 SmallVector, 4> Worklist;
2635 Worklist.push_back(std::make_pair(Inst, Arg));
2636 do {
2637 std::pair Pair = Worklist.pop_back_val();
2638 Inst = Pair.first;
2639 Arg = Pair.second;
2640
2641 const PHINode *PN = dyn_cast(Arg);
2642 if (!PN) continue;
2643
2644 // Determine if the PHI has any null operands, or any incoming
2645 // critical edges.
2646 bool HasNull = false;
2647 bool HasCriticalEdges = false;
2648 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2649 Value *Incoming =
2650 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
2651 if (isNullOrUndef(Incoming))
2652 HasNull = true;
2653 else if (cast(PN->getIncomingBlock(i)->back())
2654 .getNumSuccessors() != 1) {
2655 HasCriticalEdges = true;
2656 break;
2657 }
2658 }
2659 // If we have null operands and no critical edges, optimize.
2660 if (!HasCriticalEdges && HasNull) {
2661 SmallPtrSet DependingInstructions;
2662 SmallPtrSet Visited;
2663
2664 // Check that there is nothing that cares about the reference
2665 // count between the call and the phi.
2666 switch (Class) {
2667 case IC_Retain:
2668 case IC_RetainBlock:
2669 // These can always be moved up.
2670 break;
2671 case IC_Release:
2672 // These can't be moved across things that care about the retain
2673 // count.
2674 FindDependencies(NeedsPositiveRetainCount, Arg,
2675 Inst->getParent(), Inst,
2676 DependingInstructions, Visited, PA);
2677 break;
2678 case IC_Autorelease:
2679 // These can't be moved across autorelease pool scope boundaries.
2680 FindDependencies(AutoreleasePoolBoundary, Arg,
2681 Inst->getParent(), Inst,
2682 DependingInstructions, Visited, PA);
2683 break;
2684 case IC_RetainRV:
2685 case IC_AutoreleaseRV:
2686 // Don't move these; the RV optimization depends on the autoreleaseRV
2687 // being tail called, and the retainRV being immediately after a call
2688 // (which might still happen if we get lucky with codegen layout, but
2689 // it's not worth taking the chance).
2690 continue;
2691 default:
2692 llvm_unreachable("Invalid dependence flavor");
2693 }
2694
2695 if (DependingInstructions.size() == 1 &&
2696 *DependingInstructions.begin() == PN) {
2697 Changed = true;
2698 ++NumPartialNoops;
2699 // Clone the call into each predecessor that has a non-null value.
2700 CallInst *CInst = cast(Inst);
2701 Type *ParamTy = CInst->getArgOperand(0)->getType();
2702 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2703 Value *Incoming =
2704 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
2705 if (!isNullOrUndef(Incoming)) {
2706 CallInst *Clone = cast(CInst->clone());
2707 Value *Op = PN->getIncomingValue(i);
2708 Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
2709 if (Op->getType() != ParamTy)
2710 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
2711 Clone->setArgOperand(0, Op);
2712 Clone->insertBefore(InsertPos);
2713
2714 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Cloning "
2715 << *CInst << "\n"
2716 " And inserting "
2717 "clone at " << *InsertPos << "\n");
2718 Worklist.push_back(std::make_pair(Clone, Incoming));
2719 }
2720 }
2721 // Erase the original call.
2722 DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
2723 EraseInstruction(CInst);
2724 continue;
2725 }
2726 }
2727 } while (!Worklist.empty());
2728 }
2729 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Finished List.\n");
2730 }
2731
2732 /// Check for critical edges, loop boundaries, irreducible control flow, or
2733 /// other CFG structures where moving code across the edge would result in it
2734 /// being executed more.
2735 void
2736 ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
2737 DenseMap &BBStates,
2738 BBState &MyStates) const {
2739 // If any top-down local-use or possible-dec has a succ which is earlier in
2740 // the sequence, forget it.
2741 for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
2742 E = MyStates.top_down_ptr_end(); I != E; ++I)
2743 switch (I->second.GetSeq()) {
2744 default: break;
2745 case S_Use: {
2746 const Value *Arg = I->first;
2747 const TerminatorInst *TI = cast(&BB->back());
2748 bool SomeSuccHasSame = false;
2749 bool AllSuccsHaveSame = true;
2750 PtrState &S = I->second;
2751 succ_const_iterator SI(TI), SE(TI, false);
2752
2753 for (; SI != SE; ++SI) {
2754 Sequence SuccSSeq = S_None;
2755 bool SuccSRRIKnownSafe = false;
2756 // If VisitBottomUp has pointer information for this successor, take
2757 // what we know about it.
2758 DenseMap::iterator BBI =
2759 BBStates.find(*SI);
2760 assert(BBI != BBStates.end());
2761 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
2762 SuccSSeq = SuccS.GetSeq();
2763 SuccSRRIKnownSafe = SuccS.RRI.KnownSafe;
2764 switch (SuccSSeq) {
2765 case S_None:
2766 case S_CanRelease: {
2767 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
2768 S.ClearSequenceProgress();
2769 break;
2770 }
2771 continue;
2772 }
2773 case S_Use:
2774 SomeSuccHasSame = true;
2775 break;
2776 case S_Stop:
2777 case S_Release:
2778 case S_MovableRelease:
2779 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
2780 AllSuccsHaveSame = false;
2781 break;
2782 case S_Retain:
2783 llvm_unreachable("bottom-up pointer in retain state!");
2784 }
2785 }
2786 // If the state at the other end of any of the successor edges
2787 // matches the current state, require all edges to match. This
2788 // guards against loops in the middle of a sequence.
2789 if (SomeSuccHasSame && !AllSuccsHaveSame)
2790 S.ClearSequenceProgress();
2791 break;
2792 }
2793 case S_CanRelease: {
2794 const Value *Arg = I->first;
2795 const TerminatorInst *TI = cast(&BB->back());
2796 bool SomeSuccHasSame = false;
2797 bool AllSuccsHaveSame = true;
2798 PtrState &S = I->second;
2799 succ_const_iterator SI(TI), SE(TI, false);
2800
2801 for (; SI != SE; ++SI) {
2802 Sequence SuccSSeq = S_None;
2803 bool SuccSRRIKnownSafe = false;
2804 // If VisitBottomUp has pointer information for this successor, take
2805 // what we know about it.
2806 DenseMap::iterator BBI =
2807 BBStates.find(*SI);
2808 assert(BBI != BBStates.end());
2809 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
2810 SuccSSeq = SuccS.GetSeq();
2811 SuccSRRIKnownSafe = SuccS.RRI.KnownSafe;
2812 switch (SuccSSeq) {
2813 case S_None: {
2814 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
2815 S.ClearSequenceProgress();
2816 break;
2817 }
2818 continue;
2819 }
2820 case S_CanRelease:
2821 SomeSuccHasSame = true;
2822 break;
2823 case S_Stop:
2824 case S_Release:
2825 case S_MovableRelease:
2826 case S_Use:
2827 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
2828 AllSuccsHaveSame = false;
2829 break;
2830 case S_Retain:
2831 llvm_unreachable("bottom-up pointer in retain state!");
2832 }
2833 }
2834 // If the state at the other end of any of the successor edges
2835 // matches the current state, require all edges to match. This
2836 // guards against loops in the middle of a sequence.
2837 if (SomeSuccHasSame && !AllSuccsHaveSame)
2838 S.ClearSequenceProgress();
2839 break;
2840 }
2841 }
2842 }
2843
2844 bool
2845 ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
2846 BasicBlock *BB,
2847 MapVector &Retains,
2848 BBState &MyStates) {
2849 bool NestingDetected = false;
2850 InstructionClass Class = GetInstructionClass(Inst);
2851 const Value *Arg = 0;
2852
2853 switch (Class) {
2854 case IC_Release: {
2855 Arg = GetObjCArg(Inst);
2856
2857 PtrState &S = MyStates.getPtrBottomUpState(Arg);
2858
2859 // If we see two releases in a row on the same pointer. If so, make
2860 // a note, and we'll cicle back to revisit it after we've
2861 // hopefully eliminated the second release, which may allow us to
2862 // eliminate the first release too.
2863 // Theoretically we could implement removal of nested retain+release
2864 // pairs by making PtrState hold a stack of states, but this is
2865 // simple and avoids adding overhead for the non-nested case.
2866 if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) {
2867 DEBUG(dbgs() << "ObjCARCOpt::VisitInstructionBottomUp: Found nested "
2868 "releases (i.e. a release pair)\n");
2869 NestingDetected = true;
2870 }
2871
2872 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
2873 S.ResetSequenceProgress(ReleaseMetadata ? S_MovableRelease : S_Release);
2874 S.RRI.ReleaseMetadata = ReleaseMetadata;
2875 S.RRI.KnownSafe = S.IsKnownIncremented();
2876 S.RRI.IsTailCallRelease = cast(Inst)->isTailCall();
2877 S.RRI.Calls.insert(Inst);
2878
2879 S.SetKnownPositiveRefCount();
2880 break;
2881 }
2882 case IC_RetainBlock:
2883 // An objc_retainBlock call with just a use may need to be kept,
2884 // because it may be copying a block from the stack to the heap.
2885 if (!IsRetainBlockOptimizable(Inst))
2886 break;
2887 // FALLTHROUGH
2888 case IC_Retain:
2889 case IC_RetainRV: {
2890 Arg = GetObjCArg(Inst);
2891
2892 PtrState &S = MyStates.getPtrBottomUpState(Arg);
2893 S.SetKnownPositiveRefCount();
2894
2895 switch (S.GetSeq()) {
2896 case S_Stop:
2897 case S_Release:
2898 case S_MovableRelease:
2899 case S_Use:
2900 S.RRI.ReverseInsertPts.clear();
2901 // FALL THROUGH
2902 case S_CanRelease:
2903 // Don't do retain+release tracking for IC_RetainRV, because it's
2904 // better to let it remain as the first instruction after a call.
2905 if (Class != IC_RetainRV) {
2906 S.RRI.IsRetainBlock = Class == IC_RetainBlock;
2907 Retains[Inst] = S.RRI;
2908 }
2909 S.ClearSequenceProgress();
2910 break;
2911 case S_None:
2912 break;
2913 case S_Retain:
2914 llvm_unreachable("bottom-up pointer in retain state!");
2915 }
2916 return NestingDetected;
2917 }
2918 case IC_AutoreleasepoolPop:
2919 // Conservatively, clear MyStates for all known pointers.
2920 MyStates.clearBottomUpPointers();
2921 return NestingDetected;
2922 case IC_AutoreleasepoolPush:
2923 case IC_None:
2924 // These are irrelevant.
2925 return NestingDetected;
2926 default:
2927 break;
2928 }
2929
2930 // Consider any other possible effects of this instruction on each
2931 // pointer being tracked.
2932 for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(),
2933 ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) {
2934 const Value *Ptr = MI->first;
2935 if (Ptr == Arg)
2936 continue; // Handled above.
2937 PtrState &S = MI->second;
2938 Sequence Seq = S.GetSeq();
2939
2940 // Check for possible releases.
2941 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
2942 S.ClearRefCount();
2943 switch (Seq) {
2944 case S_Use:
2945 S.SetSeq(S_CanRelease);
2946 continue;
2947 case S_CanRelease:
2948 case S_Release:
2949 case S_MovableRelease:
2950 case S_Stop:
2951 case S_None:
2952 break;
2953 case S_Retain:
2954 llvm_unreachable("bottom-up pointer in retain state!");
2955 }
2956 }
2957
2958 // Check for possible direct uses.
2959 switch (Seq) {
2960 case S_Release:
2961 case S_MovableRelease:
2962 if (CanUse(Inst, Ptr, PA, Class)) {
2963 assert(S.RRI.ReverseInsertPts.empty());
2964 // If this is an invoke instruction, we're scanning it as part of
2965 // one of its successor blocks, since we can't insert code after it
2966 // in its own block, and we don't want to split critical edges.
2967 if (isa(Inst))
2968 S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
2969 else
2970 S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
2971 S.SetSeq(S_Use);
2972 } else if (Seq == S_Release &&
2973 (Class == IC_User || Class == IC_CallOrUser)) {
2974 // Non-movable releases depend on any possible objc pointer use.
2975 S.SetSeq(S_Stop);
2976 assert(S.RRI.ReverseInsertPts.empty());
2977 // As above; handle invoke specially.
2978 if (isa(Inst))
2979 S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
2980 else
2981 S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
2982 }
2983 break;
2984 case S_Stop:
2985 if (CanUse(Inst, Ptr, PA, Class))
2986 S.SetSeq(S_Use);
2987 break;
2988 case S_CanRelease:
2989 case S_Use:
2990 case S_None:
2991 break;
2992 case S_Retain:
2993 llvm_unreachable("bottom-up pointer in retain state!");
2994 }
2995 }
2996
2997 return NestingDetected;
2998 }
2999
3000 bool
3001 ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
3002 DenseMap &BBStates,
3003 MapVector &Retains) {
3004 bool NestingDetected = false;
3005 BBState &MyStates = BBStates[BB];
3006
3007 // Merge the states from each successor to compute the initial state
3008 // for the current block.
3009 BBState::edge_iterator SI(MyStates.succ_begin()),
3010 SE(MyStates.succ_end());
3011 if (SI != SE) {
3012 const BasicBlock *Succ = *SI;
3013 DenseMap::iterator I = BBStates.find(Succ);
3014 assert(I != BBStates.end());
3015 MyStates.InitFromSucc(I->second);
3016 ++SI;
3017 for (; SI != SE; ++SI) {
3018 Succ = *SI;
3019 I = BBStates.find(Succ);
3020 assert(I != BBStates.end());
3021 MyStates.MergeSucc(I->second);
3022 }
3023 }
3024
3025 // Visit all the instructions, bottom-up.
3026 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
3027 Instruction *Inst = llvm::prior(I);
3028
3029 // Invoke instructions are visited as part of their successors (below).
3030 if (isa(Inst))
3031 continue;
3032
3033 DEBUG(dbgs() << "ObjCARCOpt::VisitButtonUp: Visiting " << *Inst << "\n");
3034
3035 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
3036 }
3037
3038 // If there's a predecessor with an invoke, visit the invoke as if it were
3039 // part of this block, since we can't insert code after an invoke in its own
3040 // block, and we don't want to split critical edges.
3041 for (BBState::edge_iterator PI(MyStates.pred_begin()),
3042 PE(MyStates.pred_end()); PI != PE; ++PI) {
3043 BasicBlock *Pred = *PI;
3044 if (InvokeInst *II = dyn_cast(&Pred->back()))
3045 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
3046 }
3047
3048 return NestingDetected;
3049 }
3050
3051 bool
3052 ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
3053 DenseMap &Releases,
3054 BBState &MyStates) {
3055 bool NestingDetected = false;
3056 InstructionClass Class = GetInstructionClass(Inst);
3057 const Value *Arg = 0;
3058
3059 switch (Class) {
3060 case IC_RetainBlock:
3061 // An objc_retainBlock call with just a use may need to be kept,
3062 // because it may be copying a block from the stack to the heap.
3063 if (!IsRetainBlockOptimizable(Inst))
3064 break;
3065 // FALLTHROUGH
3066 case IC_Retain:
3067 case IC_RetainRV: {
3068 Arg = GetObjCArg(Inst);
3069
3070 PtrState &S = MyStates.getPtrTopDownState(Arg);
3071
3072 // Don't do retain+release tracking for IC_RetainRV, because it's
3073 // better to let it remain as the first instruction after a call.
3074 if (Class != IC_RetainRV) {
3075 // If we see two retains in a row on the same pointer. If so, make
3076 // a note, and we'll cicle back to revisit it after we've
3077 // hopefully eliminated the second retain, which may allow us to
3078 // eliminate the first retain too.
3079 // Theoretically we could implement removal of nested retain+release
3080 // pairs by making PtrState hold a stack of states, but this is
3081 // simple and avoids adding overhead for the non-nested case.
3082 if (S.GetSeq() == S_Retain)
3083 NestingDetected = true;
3084
3085 S.ResetSequenceProgress(S_Retain);
3086 S.RRI.IsRetainBlock = Class == IC_RetainBlock;
3087 S.RRI.KnownSafe = S.IsKnownIncremented();
3088 S.RRI.Calls.insert(Inst);
3089 }
3090
3091 S.SetKnownPositiveRefCount();
3092
3093 // A retain can be a potential use; procede to the generic checking
3094 // code below.
3095 break;
3096 }
3097 case IC_Release: {
3098 Arg = GetObjCArg(Inst);
3099
3100 PtrState &S = MyStates.getPtrTopDownState(Arg);
3101 S.ClearRefCount();
3102
3103 switch (S.GetSeq()) {
3104 case S_Retain:
3105 case S_CanRelease:
3106 S.RRI.ReverseInsertPts.clear();
3107 // FALL THROUGH
3108 case S_Use:
3109 S.RRI.ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
3110 S.RRI.IsTailCallRelease = cast(Inst)->isTailCall();
3111 Releases[Inst] = S.RRI;
3112 S.ClearSequenceProgress();
3113 break;
3114 case S_None:
3115 break;
3116 case S_Stop:
3117 case S_Release:
3118 case S_MovableRelease:
3119 llvm_unreachable("top-down pointer in release state!");
3120 }
3121 break;
3122 }
3123 case IC_AutoreleasepoolPop:
3124 // Conservatively, clear MyStates for all known pointers.
3125 MyStates.clearTopDownPointers();
3126 return NestingDetected;
3127 case IC_AutoreleasepoolPush:
3128 case IC_None:
3129 // These are irrelevant.
3130 return NestingDetected;
3131 default:
3132 break;
3133 }
3134
3135 // Consider any other possible effects of this instruction on each
3136 // pointer being tracked.
3137 for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
3138 ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
3139 const Value *Ptr = MI->first;
3140 if (Ptr == Arg)
3141 continue; // Handled above.
3142 PtrState &S = MI->second;
3143 Sequence Seq = S.GetSeq();
3144
3145 // Check for possible releases.
3146 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
3147 S.ClearRefCount();
3148 switch (Seq) {
3149 case S_Retain:
3150 S.SetSeq(S_CanRelease);
3151 assert(S.RRI.ReverseInsertPts.empty());
3152 S.RRI.ReverseInsertPts.insert(Inst);
3153
3154 // One call can't cause a transition from S_Retain to S_CanRelease
3155 // and S_CanRelease to S_Use. If we've made the first transition,
3156 // we're done.
3157 continue;
3158 case S_Use:
3159 case S_CanRelease:
3160 case S_None:
3161 break;
3162 case S_Stop:
3163 case S_Release:
3164 case S_MovableRelease:
3165 llvm_unreachable("top-down pointer in release state!");
3166 }
3167 }
3168
3169 // Check for possible direct uses.
3170 switch (Seq) {
3171 case S_CanRelease:
3172 if (CanUse(Inst, Ptr, PA, Class))
3173 S.SetSeq(S_Use);
3174 break;
3175 case S_Retain:
3176 case S_Use:
3177 case S_None:
3178 break;
3179 case S_Stop:
3180 case S_Release:
3181 case S_MovableRelease:
3182 llvm_unreachable("top-down pointer in release state!");
3183 }
3184 }
3185
3186 return NestingDetected;
3187 }
3188
3189 bool
3190 ObjCARCOpt::VisitTopDown(BasicBlock *BB,
3191 DenseMap &BBStates,
3192 DenseMap &Releases) {
3193 bool NestingDetected = false;
3194 BBState &MyStates = BBStates[BB];
3195
3196 // Merge the states from each predecessor to compute the initial state
3197 // for the current block.
3198 BBState::edge_iterator PI(MyStates.pred_begin()),