blob: a6fdc284ce587d2777ec3f8359e20ea18977d8de [file] [log] [blame]
Michael Gottesman08904e32013-01-28 03:28:38 +00001//===- ObjCARC.h - ObjC ARC Optimization --------------*- mode: c++ -*-----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file defines common definitions/declarations used by the ObjC ARC
11/// Optimizer. ARC stands for Automatic Reference Counting and is a system for
12/// managing reference counts for objects in Objective C.
13///
14/// WARNING: This file knows about certain library functions. It recognizes them
15/// by name, and hardwires knowledge of their semantics.
16///
17/// WARNING: This file knows about how certain Objective-C library functions are
18/// used. Naive LLVM IR transformations which would otherwise be
19/// behavior-preserving may break these assumptions.
20///
21//===----------------------------------------------------------------------===//
22
23#ifndef LLVM_TRANSFORMS_SCALAR_OBJCARC_H
24#define LLVM_TRANSFORMS_SCALAR_OBJCARC_H
25
26#include "llvm/ADT/StringSwitch.h"
27#include "llvm/Analysis/AliasAnalysis.h"
28#include "llvm/Analysis/Passes.h"
Michael Gottesman294e7da2013-01-28 05:51:54 +000029#include "llvm/Analysis/ValueTracking.h"
Michael Gottesman08904e32013-01-28 03:28:38 +000030#include "llvm/IR/Module.h"
31#include "llvm/Pass.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/InstIterator.h"
34#include "llvm/Support/raw_ostream.h"
35#include "llvm/Transforms/ObjCARC.h"
36
37namespace llvm {
38namespace objcarc {
39
40/// \brief A handy option to enable/disable all ARC Optimizations.
41extern bool EnableARCOpts;
42
43/// \brief Test if the given module looks interesting to run ARC optimization
44/// on.
45static inline bool ModuleHasARC(const Module &M) {
46 return
47 M.getNamedValue("objc_retain") ||
48 M.getNamedValue("objc_release") ||
49 M.getNamedValue("objc_autorelease") ||
50 M.getNamedValue("objc_retainAutoreleasedReturnValue") ||
51 M.getNamedValue("objc_retainBlock") ||
52 M.getNamedValue("objc_autoreleaseReturnValue") ||
53 M.getNamedValue("objc_autoreleasePoolPush") ||
54 M.getNamedValue("objc_loadWeakRetained") ||
55 M.getNamedValue("objc_loadWeak") ||
56 M.getNamedValue("objc_destroyWeak") ||
57 M.getNamedValue("objc_storeWeak") ||
58 M.getNamedValue("objc_initWeak") ||
59 M.getNamedValue("objc_moveWeak") ||
60 M.getNamedValue("objc_copyWeak") ||
61 M.getNamedValue("objc_retainedObject") ||
62 M.getNamedValue("objc_unretainedObject") ||
63 M.getNamedValue("objc_unretainedPointer");
64}
65
66/// \enum InstructionClass
67/// \brief A simple classification for instructions.
68enum InstructionClass {
69 IC_Retain, ///< objc_retain
70 IC_RetainRV, ///< objc_retainAutoreleasedReturnValue
71 IC_RetainBlock, ///< objc_retainBlock
72 IC_Release, ///< objc_release
73 IC_Autorelease, ///< objc_autorelease
74 IC_AutoreleaseRV, ///< objc_autoreleaseReturnValue
75 IC_AutoreleasepoolPush, ///< objc_autoreleasePoolPush
76 IC_AutoreleasepoolPop, ///< objc_autoreleasePoolPop
77 IC_NoopCast, ///< objc_retainedObject, etc.
78 IC_FusedRetainAutorelease, ///< objc_retainAutorelease
79 IC_FusedRetainAutoreleaseRV, ///< objc_retainAutoreleaseReturnValue
80 IC_LoadWeakRetained, ///< objc_loadWeakRetained (primitive)
81 IC_StoreWeak, ///< objc_storeWeak (primitive)
82 IC_InitWeak, ///< objc_initWeak (derived)
83 IC_LoadWeak, ///< objc_loadWeak (derived)
84 IC_MoveWeak, ///< objc_moveWeak (derived)
85 IC_CopyWeak, ///< objc_copyWeak (derived)
86 IC_DestroyWeak, ///< objc_destroyWeak (derived)
87 IC_StoreStrong, ///< objc_storeStrong (derived)
88 IC_CallOrUser, ///< could call objc_release and/or "use" pointers
89 IC_Call, ///< could call objc_release
90 IC_User, ///< could "use" a pointer
91 IC_None ///< anything else
92};
93
94static raw_ostream &operator<<(raw_ostream &OS, const InstructionClass Class)
95 LLVM_ATTRIBUTE_USED;
96
97static raw_ostream &operator<<(raw_ostream &OS, const InstructionClass Class) {
98 switch (Class) {
99 case IC_Retain:
100 return OS << "IC_Retain";
101 case IC_RetainRV:
102 return OS << "IC_RetainRV";
103 case IC_RetainBlock:
104 return OS << "IC_RetainBlock";
105 case IC_Release:
106 return OS << "IC_Release";
107 case IC_Autorelease:
108 return OS << "IC_Autorelease";
109 case IC_AutoreleaseRV:
110 return OS << "IC_AutoreleaseRV";
111 case IC_AutoreleasepoolPush:
112 return OS << "IC_AutoreleasepoolPush";
113 case IC_AutoreleasepoolPop:
114 return OS << "IC_AutoreleasepoolPop";
115 case IC_NoopCast:
116 return OS << "IC_NoopCast";
117 case IC_FusedRetainAutorelease:
118 return OS << "IC_FusedRetainAutorelease";
119 case IC_FusedRetainAutoreleaseRV:
120 return OS << "IC_FusedRetainAutoreleaseRV";
121 case IC_LoadWeakRetained:
122 return OS << "IC_LoadWeakRetained";
123 case IC_StoreWeak:
124 return OS << "IC_StoreWeak";
125 case IC_InitWeak:
126 return OS << "IC_InitWeak";
127 case IC_LoadWeak:
128 return OS << "IC_LoadWeak";
129 case IC_MoveWeak:
130 return OS << "IC_MoveWeak";
131 case IC_CopyWeak:
132 return OS << "IC_CopyWeak";
133 case IC_DestroyWeak:
134 return OS << "IC_DestroyWeak";
135 case IC_StoreStrong:
136 return OS << "IC_StoreStrong";
137 case IC_CallOrUser:
138 return OS << "IC_CallOrUser";
139 case IC_Call:
140 return OS << "IC_Call";
141 case IC_User:
142 return OS << "IC_User";
143 case IC_None:
144 return OS << "IC_None";
145 }
146 llvm_unreachable("Unknown instruction class!");
147}
148
Michael Gottesman294e7da2013-01-28 05:51:54 +0000149/// \brief Test if the given class is objc_retain or equivalent.
150static inline bool IsRetain(InstructionClass Class) {
151 return Class == IC_Retain ||
152 Class == IC_RetainRV;
153}
154
155/// \brief Test if the given class is objc_autorelease or equivalent.
156static inline bool IsAutorelease(InstructionClass Class) {
157 return Class == IC_Autorelease ||
158 Class == IC_AutoreleaseRV;
159}
160
161/// \brief Test if the given class represents instructions which return their
162/// argument verbatim.
163static inline bool IsForwarding(InstructionClass Class) {
164 // objc_retainBlock technically doesn't always return its argument
165 // verbatim, but it doesn't matter for our purposes here.
166 return Class == IC_Retain ||
167 Class == IC_RetainRV ||
168 Class == IC_Autorelease ||
169 Class == IC_AutoreleaseRV ||
170 Class == IC_RetainBlock ||
171 Class == IC_NoopCast;
172}
173
174/// \brief Test if the given class represents instructions which do nothing if
175/// passed a null pointer.
176static inline bool IsNoopOnNull(InstructionClass Class) {
177 return Class == IC_Retain ||
178 Class == IC_RetainRV ||
179 Class == IC_Release ||
180 Class == IC_Autorelease ||
181 Class == IC_AutoreleaseRV ||
182 Class == IC_RetainBlock;
183}
184
185/// \brief Test if the given class represents instructions which are always safe
186/// to mark with the "tail" keyword.
187static inline bool IsAlwaysTail(InstructionClass Class) {
188 // IC_RetainBlock may be given a stack argument.
189 return Class == IC_Retain ||
190 Class == IC_RetainRV ||
191 Class == IC_AutoreleaseRV;
192}
193
194/// \brief Test if the given class represents instructions which are never safe
195/// to mark with the "tail" keyword.
196static inline bool IsNeverTail(InstructionClass Class) {
197 /// It is never safe to tail call objc_autorelease since by tail calling
198 /// objc_autorelease, we also tail call -[NSObject autorelease] which supports
199 /// fast autoreleasing causing our object to be potentially reclaimed from the
200 /// autorelease pool which violates the semantics of __autoreleasing types in
201 /// ARC.
202 return Class == IC_Autorelease;
203}
204
205/// \brief Test if the given class represents instructions which are always safe
206/// to mark with the nounwind attribute.
207static inline bool IsNoThrow(InstructionClass Class) {
208 // objc_retainBlock is not nounwind because it calls user copy constructors
209 // which could theoretically throw.
210 return Class == IC_Retain ||
211 Class == IC_RetainRV ||
212 Class == IC_Release ||
213 Class == IC_Autorelease ||
214 Class == IC_AutoreleaseRV ||
215 Class == IC_AutoreleasepoolPush ||
216 Class == IC_AutoreleasepoolPop;
217}
Michael Gottesman08904e32013-01-28 03:28:38 +0000218
219/// \brief Determine if F is one of the special known Functions. If it isn't,
220/// return IC_CallOrUser.
Michael Gottesman294e7da2013-01-28 05:51:54 +0000221static InstructionClass GetFunctionClass(const Function *F)
222 LLVM_ATTRIBUTE_USED;
223static InstructionClass GetFunctionClass(const Function *F) {
Michael Gottesman08904e32013-01-28 03:28:38 +0000224 Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
225
226 // No arguments.
227 if (AI == AE)
228 return StringSwitch<InstructionClass>(F->getName())
229 .Case("objc_autoreleasePoolPush", IC_AutoreleasepoolPush)
230 .Default(IC_CallOrUser);
231
232 // One argument.
233 const Argument *A0 = AI++;
234 if (AI == AE)
235 // Argument is a pointer.
236 if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) {
237 Type *ETy = PTy->getElementType();
238 // Argument is i8*.
239 if (ETy->isIntegerTy(8))
240 return StringSwitch<InstructionClass>(F->getName())
241 .Case("objc_retain", IC_Retain)
242 .Case("objc_retainAutoreleasedReturnValue", IC_RetainRV)
243 .Case("objc_retainBlock", IC_RetainBlock)
244 .Case("objc_release", IC_Release)
245 .Case("objc_autorelease", IC_Autorelease)
246 .Case("objc_autoreleaseReturnValue", IC_AutoreleaseRV)
247 .Case("objc_autoreleasePoolPop", IC_AutoreleasepoolPop)
248 .Case("objc_retainedObject", IC_NoopCast)
249 .Case("objc_unretainedObject", IC_NoopCast)
250 .Case("objc_unretainedPointer", IC_NoopCast)
251 .Case("objc_retain_autorelease", IC_FusedRetainAutorelease)
252 .Case("objc_retainAutorelease", IC_FusedRetainAutorelease)
253 .Case("objc_retainAutoreleaseReturnValue",IC_FusedRetainAutoreleaseRV)
254 .Default(IC_CallOrUser);
255
256 // Argument is i8**
257 if (PointerType *Pte = dyn_cast<PointerType>(ETy))
258 if (Pte->getElementType()->isIntegerTy(8))
259 return StringSwitch<InstructionClass>(F->getName())
260 .Case("objc_loadWeakRetained", IC_LoadWeakRetained)
261 .Case("objc_loadWeak", IC_LoadWeak)
262 .Case("objc_destroyWeak", IC_DestroyWeak)
263 .Default(IC_CallOrUser);
264 }
265
266 // Two arguments, first is i8**.
267 const Argument *A1 = AI++;
268 if (AI == AE)
269 if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
270 if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
271 if (Pte->getElementType()->isIntegerTy(8))
272 if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
273 Type *ETy1 = PTy1->getElementType();
274 // Second argument is i8*
275 if (ETy1->isIntegerTy(8))
276 return StringSwitch<InstructionClass>(F->getName())
277 .Case("objc_storeWeak", IC_StoreWeak)
278 .Case("objc_initWeak", IC_InitWeak)
279 .Case("objc_storeStrong", IC_StoreStrong)
280 .Default(IC_CallOrUser);
281 // Second argument is i8**.
282 if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
283 if (Pte1->getElementType()->isIntegerTy(8))
284 return StringSwitch<InstructionClass>(F->getName())
285 .Case("objc_moveWeak", IC_MoveWeak)
286 .Case("objc_copyWeak", IC_CopyWeak)
287 .Default(IC_CallOrUser);
288 }
289
290 // Anything else.
291 return IC_CallOrUser;
292}
293
294/// \brief Determine which objc runtime call instruction class V belongs to.
295///
296/// This is similar to GetInstructionClass except that it only detects objc
297/// runtime calls. This allows it to be faster.
298///
299static inline InstructionClass GetBasicInstructionClass(const Value *V) {
300 if (const CallInst *CI = dyn_cast<CallInst>(V)) {
301 if (const Function *F = CI->getCalledFunction())
302 return GetFunctionClass(F);
303 // Otherwise, be conservative.
304 return IC_CallOrUser;
305 }
306
307 // Otherwise, be conservative.
308 return isa<InvokeInst>(V) ? IC_CallOrUser : IC_User;
309}
310
Michael Gottesman294e7da2013-01-28 05:51:54 +0000311
312/// \brief This is a wrapper around getUnderlyingObject which also knows how to
313/// look through objc_retain and objc_autorelease calls, which we know to return
314/// their argument verbatim.
315static inline const Value *GetUnderlyingObjCPtr(const Value *V) {
316 for (;;) {
317 V = GetUnderlyingObject(V);
318 if (!IsForwarding(GetBasicInstructionClass(V)))
319 break;
320 V = cast<CallInst>(V)->getArgOperand(0);
321 }
322
323 return V;
324}
325
326/// \brief This is a wrapper around Value::stripPointerCasts which also knows
327/// how to look through objc_retain and objc_autorelease calls, which we know to
328/// return their argument verbatim.
329static inline const Value *StripPointerCastsAndObjCCalls(const Value *V) {
330 for (;;) {
331 V = V->stripPointerCasts();
332 if (!IsForwarding(GetBasicInstructionClass(V)))
333 break;
334 V = cast<CallInst>(V)->getArgOperand(0);
335 }
336 return V;
337}
338
339/// \brief This is a wrapper around Value::stripPointerCasts which also knows
340/// how to look through objc_retain and objc_autorelease calls, which we know to
341/// return their argument verbatim.
342static inline Value *StripPointerCastsAndObjCCalls(Value *V) {
343 for (;;) {
344 V = V->stripPointerCasts();
345 if (!IsForwarding(GetBasicInstructionClass(V)))
346 break;
347 V = cast<CallInst>(V)->getArgOperand(0);
348 }
349 return V;
350}
351
352
Michael Gottesman08904e32013-01-28 03:28:38 +0000353} // end namespace objcarc
354} // end namespace llvm
355
356#endif // LLVM_TRANSFORMS_SCALAR_OBJCARC_H