1 //===- ObjCARC.h - ObjC ARC Optimization --------------*- mode: c++ -*-----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 /// WARNING: This file knows about certain library functions. It recognizes them
15 /// by name, and hardwires knowledge of their semantics.
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.
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_TRANSFORMS_SCALAR_OBJCARC_H
24 #define LLVM_TRANSFORMS_SCALAR_OBJCARC_H
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/Passes.h"
29 #include "llvm/Analysis/ValueTracking.h"
30 #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"
40 /// \brief A handy option to enable/disable all ARC Optimizations.
41 extern bool EnableARCOpts;
43 /// \brief Test if the given module looks interesting to run ARC optimization
45 static inline bool ModuleHasARC(const Module &M) {
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");
66 /// \enum InstructionClass
67 /// \brief A simple classification for instructions.
68 enum 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
94 raw_ostream &operator<<(raw_ostream &OS, const InstructionClass Class);
96 /// \brief Test if the given class is objc_retain or equivalent.
97 static inline bool IsRetain(InstructionClass Class) {
98 return Class == IC_Retain ||
102 /// \brief Test if the given class is objc_autorelease or equivalent.
103 static inline bool IsAutorelease(InstructionClass Class) {
104 return Class == IC_Autorelease ||
105 Class == IC_AutoreleaseRV;
108 /// \brief Test if the given class represents instructions which return their
109 /// argument verbatim.
110 static 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;
121 /// \brief Test if the given class represents instructions which do nothing if
122 /// passed a null pointer.
123 static 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;
132 /// \brief Test if the given class represents instructions which are always safe
133 /// to mark with the "tail" keyword.
134 static 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;
141 /// \brief Test if the given class represents instructions which are never safe
142 /// to mark with the "tail" keyword.
143 static 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
149 return Class == IC_Autorelease;
152 /// \brief Test if the given class represents instructions which are always safe
153 /// to mark with the nounwind attribute.
154 static 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;
166 /// \brief Determine if F is one of the special known Functions. If it isn't,
167 /// return IC_CallOrUser.
168 InstructionClass GetFunctionClass(const Function *F);
170 /// \brief Determine which objc runtime call instruction class V belongs to.
172 /// This is similar to GetInstructionClass except that it only detects objc
173 /// runtime calls. This allows it to be faster.
175 static 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;
183 // Otherwise, be conservative.
184 return isa<InvokeInst>(V) ? IC_CallOrUser : IC_User;
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.
191 static inline const Value *GetUnderlyingObjCPtr(const Value *V) {
193 V = GetUnderlyingObject(V);
194 if (!IsForwarding(GetBasicInstructionClass(V)))
196 V = cast<CallInst>(V)->getArgOperand(0);
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.
205 static inline const Value *StripPointerCastsAndObjCCalls(const Value *V) {
207 V = V->stripPointerCasts();
208 if (!IsForwarding(GetBasicInstructionClass(V)))
210 V = cast<CallInst>(V)->getArgOperand(0);
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.
218 static inline Value *StripPointerCastsAndObjCCalls(Value *V) {
220 V = V->stripPointerCasts();
221 if (!IsForwarding(GetBasicInstructionClass(V)))
223 V = cast<CallInst>(V)->getArgOperand(0);
229 } // end namespace objcarc
230 } // end namespace llvm
232 #endif // LLVM_TRANSFORMS_SCALAR_OBJCARC_H