blob: 854de74e8775d5354184bb2ad1984e8826d321cf [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
Michael Gottesman5ed40af2013-01-28 06:39:31 +000094raw_ostream &operator<<(raw_ostream &OS, const InstructionClass Class);
Michael Gottesman08904e32013-01-28 03:28:38 +000095
Michael Gottesman294e7da2013-01-28 05:51:54 +000096/// \brief Test if the given class is objc_retain or equivalent.
97static inline bool IsRetain(InstructionClass Class) {
98 return Class == IC_Retain ||
99 Class == IC_RetainRV;
100}
101
102/// \brief Test if the given class is objc_autorelease or equivalent.
103static inline bool IsAutorelease(InstructionClass Class) {
104 return Class == IC_Autorelease ||
105 Class == IC_AutoreleaseRV;
106}
107
108/// \brief Test if the given class represents instructions which return their
109/// argument verbatim.
110static inline bool IsForwarding(InstructionClass Class) {
111 // objc_retainBlock technically doesn't always return its argument
112 // verbatim, but it doesn't matter for our purposes here.
113 return Class == IC_Retain ||
114 Class == IC_RetainRV ||
115 Class == IC_Autorelease ||
116 Class == IC_AutoreleaseRV ||
117 Class == IC_RetainBlock ||
118 Class == IC_NoopCast;
119}
120
121/// \brief Test if the given class represents instructions which do nothing if
122/// passed a null pointer.
123static inline bool IsNoopOnNull(InstructionClass Class) {
124 return Class == IC_Retain ||
125 Class == IC_RetainRV ||
126 Class == IC_Release ||
127 Class == IC_Autorelease ||
128 Class == IC_AutoreleaseRV ||
129 Class == IC_RetainBlock;
130}
131
132/// \brief Test if the given class represents instructions which are always safe
133/// to mark with the "tail" keyword.
134static inline bool IsAlwaysTail(InstructionClass Class) {
135 // IC_RetainBlock may be given a stack argument.
136 return Class == IC_Retain ||
137 Class == IC_RetainRV ||
138 Class == IC_AutoreleaseRV;
139}
140
141/// \brief Test if the given class represents instructions which are never safe
142/// to mark with the "tail" keyword.
143static inline bool IsNeverTail(InstructionClass Class) {
144 /// It is never safe to tail call objc_autorelease since by tail calling
145 /// objc_autorelease, we also tail call -[NSObject autorelease] which supports
146 /// fast autoreleasing causing our object to be potentially reclaimed from the
147 /// autorelease pool which violates the semantics of __autoreleasing types in
148 /// ARC.
149 return Class == IC_Autorelease;
150}
151
152/// \brief Test if the given class represents instructions which are always safe
153/// to mark with the nounwind attribute.
154static inline bool IsNoThrow(InstructionClass Class) {
155 // objc_retainBlock is not nounwind because it calls user copy constructors
156 // which could theoretically throw.
157 return Class == IC_Retain ||
158 Class == IC_RetainRV ||
159 Class == IC_Release ||
160 Class == IC_Autorelease ||
161 Class == IC_AutoreleaseRV ||
162 Class == IC_AutoreleasepoolPush ||
163 Class == IC_AutoreleasepoolPop;
164}
Michael Gottesman08904e32013-01-28 03:28:38 +0000165
166/// \brief Determine if F is one of the special known Functions. If it isn't,
167/// return IC_CallOrUser.
Michael Gottesman5ed40af2013-01-28 06:39:31 +0000168InstructionClass GetFunctionClass(const Function *F);
Michael Gottesman08904e32013-01-28 03:28:38 +0000169
170/// \brief Determine which objc runtime call instruction class V belongs to.
171///
172/// This is similar to GetInstructionClass except that it only detects objc
173/// runtime calls. This allows it to be faster.
174///
175static inline InstructionClass GetBasicInstructionClass(const Value *V) {
176 if (const CallInst *CI = dyn_cast<CallInst>(V)) {
177 if (const Function *F = CI->getCalledFunction())
178 return GetFunctionClass(F);
179 // Otherwise, be conservative.
180 return IC_CallOrUser;
181 }
182
183 // Otherwise, be conservative.
184 return isa<InvokeInst>(V) ? IC_CallOrUser : IC_User;
185}
186
Michael Gottesman294e7da2013-01-28 05:51:54 +0000187
188/// \brief This is a wrapper around getUnderlyingObject which also knows how to
189/// look through objc_retain and objc_autorelease calls, which we know to return
190/// their argument verbatim.
191static inline const Value *GetUnderlyingObjCPtr(const Value *V) {
192 for (;;) {
193 V = GetUnderlyingObject(V);
194 if (!IsForwarding(GetBasicInstructionClass(V)))
195 break;
196 V = cast<CallInst>(V)->getArgOperand(0);
197 }
198
199 return V;
200}
201
202/// \brief This is a wrapper around Value::stripPointerCasts which also knows
203/// how to look through objc_retain and objc_autorelease calls, which we know to
204/// return their argument verbatim.
205static inline const Value *StripPointerCastsAndObjCCalls(const Value *V) {
206 for (;;) {
207 V = V->stripPointerCasts();
208 if (!IsForwarding(GetBasicInstructionClass(V)))
209 break;
210 V = cast<CallInst>(V)->getArgOperand(0);
211 }
212 return V;
213}
214
215/// \brief This is a wrapper around Value::stripPointerCasts which also knows
216/// how to look through objc_retain and objc_autorelease calls, which we know to
217/// return their argument verbatim.
218static inline Value *StripPointerCastsAndObjCCalls(Value *V) {
219 for (;;) {
220 V = V->stripPointerCasts();
221 if (!IsForwarding(GetBasicInstructionClass(V)))
222 break;
223 V = cast<CallInst>(V)->getArgOperand(0);
224 }
225 return V;
226}
227
228
Michael Gottesman08904e32013-01-28 03:28:38 +0000229} // end namespace objcarc
230} // end namespace llvm
231
232#endif // LLVM_TRANSFORMS_SCALAR_OBJCARC_H