/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/include/llvm/Analysis/ObjCARCAnalysisUtils.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- ObjCARCAnalysisUtils.h - ObjC ARC Analysis Utilities -----*- 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 analysis utilities used by the ObjC ARC Optimizer. |
11 | | /// ARC stands for Automatic Reference Counting and is a system for managing |
12 | | /// 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_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H |
24 | | #define LLVM_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H |
25 | | |
26 | | #include "llvm/ADT/Optional.h" |
27 | | #include "llvm/ADT/StringSwitch.h" |
28 | | #include "llvm/Analysis/AliasAnalysis.h" |
29 | | #include "llvm/Analysis/ObjCARCInstKind.h" |
30 | | #include "llvm/Analysis/Passes.h" |
31 | | #include "llvm/Analysis/ValueTracking.h" |
32 | | #include "llvm/IR/CallSite.h" |
33 | | #include "llvm/IR/Constants.h" |
34 | | #include "llvm/IR/InstIterator.h" |
35 | | #include "llvm/IR/LLVMContext.h" |
36 | | #include "llvm/IR/Module.h" |
37 | | #include "llvm/Pass.h" |
38 | | |
39 | | namespace llvm { |
40 | | class raw_ostream; |
41 | | } |
42 | | |
43 | | namespace llvm { |
44 | | namespace objcarc { |
45 | | |
46 | | /// \brief A handy option to enable/disable all ARC Optimizations. |
47 | | extern bool EnableARCOpts; |
48 | | |
49 | | /// \brief Test if the given module looks interesting to run ARC optimization |
50 | | /// on. |
51 | 16.6k | inline bool ModuleHasARC(const Module &M) { |
52 | 16.6k | return |
53 | 16.6k | M.getNamedValue("objc_retain") || |
54 | 16.6k | M.getNamedValue("objc_release") || |
55 | 16.6k | M.getNamedValue("objc_autorelease") || |
56 | 16.6k | M.getNamedValue("objc_retainAutoreleasedReturnValue") || |
57 | 16.6k | M.getNamedValue("objc_unsafeClaimAutoreleasedReturnValue") || |
58 | 16.6k | M.getNamedValue("objc_retainBlock") || |
59 | 16.6k | M.getNamedValue("objc_autoreleaseReturnValue") || |
60 | 16.6k | M.getNamedValue("objc_autoreleasePoolPush") || |
61 | 16.6k | M.getNamedValue("objc_loadWeakRetained") || |
62 | 16.6k | M.getNamedValue("objc_loadWeak") || |
63 | 16.6k | M.getNamedValue("objc_destroyWeak") || |
64 | 16.6k | M.getNamedValue("objc_storeWeak") || |
65 | 16.6k | M.getNamedValue("objc_initWeak") || |
66 | 16.6k | M.getNamedValue("objc_moveWeak") || |
67 | 16.6k | M.getNamedValue("objc_copyWeak") || |
68 | 16.6k | M.getNamedValue("objc_retainedObject") || |
69 | 16.6k | M.getNamedValue("objc_unretainedObject") || |
70 | 16.6k | M.getNamedValue("objc_unretainedPointer") || |
71 | 16.6k | M.getNamedValue("clang.arc.use"); |
72 | 16.6k | } |
73 | | |
74 | | /// \brief This is a wrapper around getUnderlyingObject which also knows how to |
75 | | /// look through objc_retain and objc_autorelease calls, which we know to return |
76 | | /// their argument verbatim. |
77 | | inline const Value *GetUnderlyingObjCPtr(const Value *V, |
78 | 5.95k | const DataLayout &DL) { |
79 | 6.04k | for (;;) { |
80 | 6.04k | V = GetUnderlyingObject(V, DL); |
81 | 6.04k | if (!IsForwarding(GetBasicARCInstKind(V))) |
82 | 5.95k | break; |
83 | 83 | V = cast<CallInst>(V)->getArgOperand(0); |
84 | 83 | } |
85 | 5.95k | |
86 | 5.95k | return V; |
87 | 5.95k | } |
88 | | |
89 | | /// The RCIdentity root of a value \p V is a dominating value U for which |
90 | | /// retaining or releasing U is equivalent to retaining or releasing V. In other |
91 | | /// words, ARC operations on \p V are equivalent to ARC operations on \p U. |
92 | | /// |
93 | | /// We use this in the ARC optimizer to make it easier to match up ARC |
94 | | /// operations by always mapping ARC operations to RCIdentityRoots instead of |
95 | | /// pointers themselves. |
96 | | /// |
97 | | /// The two ways that we see RCIdentical values in ObjC are via: |
98 | | /// |
99 | | /// 1. PointerCasts |
100 | | /// 2. Forwarding Calls that return their argument verbatim. |
101 | | /// |
102 | | /// Thus this function strips off pointer casts and forwarding calls. *NOTE* |
103 | | /// This implies that two RCIdentical values must alias. |
104 | 6.91k | inline const Value *GetRCIdentityRoot(const Value *V) { |
105 | 7.39k | for (;;) { |
106 | 7.39k | V = V->stripPointerCasts(); |
107 | 7.39k | if (!IsForwarding(GetBasicARCInstKind(V))) |
108 | 6.91k | break; |
109 | 475 | V = cast<CallInst>(V)->getArgOperand(0); |
110 | 475 | } |
111 | 6.91k | return V; |
112 | 6.91k | } |
113 | | |
114 | | /// Helper which calls const Value *GetRCIdentityRoot(const Value *V) and just |
115 | | /// casts away the const of the result. For documentation about what an |
116 | | /// RCIdentityRoot (and by extension GetRCIdentityRoot is) look at that |
117 | | /// function. |
118 | 2.42k | inline Value *GetRCIdentityRoot(Value *V) { |
119 | 2.42k | return const_cast<Value *>(GetRCIdentityRoot((const Value *)V)); |
120 | 2.42k | } |
121 | | |
122 | | /// \brief Assuming the given instruction is one of the special calls such as |
123 | | /// objc_retain or objc_release, return the RCIdentity root of the argument of |
124 | | /// the call. |
125 | 2.32k | inline Value *GetArgRCIdentityRoot(Value *Inst) { |
126 | 2.32k | return GetRCIdentityRoot(cast<CallInst>(Inst)->getArgOperand(0)); |
127 | 2.32k | } |
128 | | |
129 | 821 | inline bool IsNullOrUndef(const Value *V) { |
130 | 784 | return isa<ConstantPointerNull>(V) || isa<UndefValue>(V); |
131 | 821 | } |
132 | | |
133 | 64 | inline bool IsNoopInstruction(const Instruction *I) { |
134 | 64 | return isa<BitCastInst>(I) || |
135 | 50 | (isa<GetElementPtrInst>(I) && |
136 | 50 | cast<GetElementPtrInst>(I)->hasAllZeroIndices()); |
137 | 64 | } |
138 | | |
139 | | /// \brief Test whether the given value is possible a retainable object pointer. |
140 | 4.07k | inline bool IsPotentialRetainableObjPtr(const Value *Op) { |
141 | 4.07k | // Pointers to static or stack storage are not valid retainable object |
142 | 4.07k | // pointers. |
143 | 4.07k | if (isa<Constant>(Op) || 4.07k isa<AllocaInst>(Op)2.36k ) |
144 | 1.81k | return false; |
145 | 4.07k | // Special arguments can not be a valid retainable object pointer. |
146 | 2.26k | if (const Argument *2.26k Arg2.26k = dyn_cast<Argument>(Op)) |
147 | 561 | if (561 Arg->hasByValAttr() || |
148 | 561 | Arg->hasInAllocaAttr() || |
149 | 561 | Arg->hasNestAttr() || |
150 | 561 | Arg->hasStructRetAttr()) |
151 | 0 | return false; |
152 | 2.26k | // Only consider values with pointer types. |
153 | 2.26k | // |
154 | 2.26k | // It seemes intuitive to exclude function pointer types as well, since |
155 | 2.26k | // functions are never retainable object pointers, however clang occasionally |
156 | 2.26k | // bitcasts retainable object pointers to function-pointer type temporarily. |
157 | 2.26k | PointerType *Ty = dyn_cast<PointerType>(Op->getType()); |
158 | 2.26k | if (!Ty) |
159 | 108 | return false; |
160 | 2.26k | // Conservatively assume anything else is a potential retainable object |
161 | 2.26k | // pointer. |
162 | 2.15k | return true; |
163 | 4.07k | } |
164 | | |
165 | | inline bool IsPotentialRetainableObjPtr(const Value *Op, |
166 | 1.03k | AliasAnalysis &AA) { |
167 | 1.03k | // First make the rudimentary check. |
168 | 1.03k | if (!IsPotentialRetainableObjPtr(Op)) |
169 | 127 | return false; |
170 | 1.03k | |
171 | 1.03k | // Objects in constant memory are not reference-counted. |
172 | 912 | if (912 AA.pointsToConstantMemory(Op)912 ) |
173 | 0 | return false; |
174 | 912 | |
175 | 912 | // Pointers in constant memory are not pointing to reference-counted objects. |
176 | 912 | if (const LoadInst *912 LI912 = dyn_cast<LoadInst>(Op)) |
177 | 206 | if (206 AA.pointsToConstantMemory(LI->getPointerOperand())206 ) |
178 | 17 | return false; |
179 | 912 | |
180 | 912 | // Otherwise assume the worst. |
181 | 895 | return true; |
182 | 1.03k | } |
183 | | |
184 | | /// \brief Helper for GetARCInstKind. Determines what kind of construct CS |
185 | | /// is. |
186 | 1.07k | inline ARCInstKind GetCallSiteClass(ImmutableCallSite CS) { |
187 | 1.07k | for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
188 | 2.08k | I != E2.08k ; ++I1.01k ) |
189 | 1.52k | if (1.52k IsPotentialRetainableObjPtr(*I)1.52k ) |
190 | 515 | return CS.onlyReadsMemory() ? 515 ARCInstKind::User1 : ARCInstKind::CallOrUser514 ; |
191 | 1.07k | |
192 | 559 | return CS.onlyReadsMemory() ? 559 ARCInstKind::None0 : ARCInstKind::Call559 ; |
193 | 1.07k | } |
194 | | |
195 | | /// \brief Return true if this value refers to a distinct and identifiable |
196 | | /// object. |
197 | | /// |
198 | | /// This is similar to AliasAnalysis's isIdentifiedObject, except that it uses |
199 | | /// special knowledge of ObjC conventions. |
200 | 1.38k | inline bool IsObjCIdentifiedObject(const Value *V) { |
201 | 1.38k | // Assume that call results and arguments have their own "provenance". |
202 | 1.38k | // Constants (including GlobalVariables) and Allocas are never |
203 | 1.38k | // reference-counted. |
204 | 1.38k | if (isa<CallInst>(V) || 1.38k isa<InvokeInst>(V)998 || |
205 | 1.38k | isa<Argument>(V)997 || isa<Constant>(V)764 || |
206 | 638 | isa<AllocaInst>(V)) |
207 | 781 | return true; |
208 | 1.38k | |
209 | 602 | if (const LoadInst *602 LI602 = dyn_cast<LoadInst>(V)) { |
210 | 425 | const Value *Pointer = |
211 | 425 | GetRCIdentityRoot(LI->getPointerOperand()); |
212 | 425 | if (const GlobalVariable *GV425 = dyn_cast<GlobalVariable>(Pointer)) { |
213 | 265 | // A constant pointer can't be pointing to an object on the heap. It may |
214 | 265 | // be reference-counted, but it won't be deleted. |
215 | 265 | if (GV->isConstant()) |
216 | 4 | return true; |
217 | 261 | StringRef Name = GV->getName(); |
218 | 261 | // These special variables are known to hold values which are not |
219 | 261 | // reference-counted pointers. |
220 | 261 | if (Name.startswith("\01l_objc_msgSend_fixup_")) |
221 | 18 | return true; |
222 | 261 | |
223 | 243 | StringRef Section = GV->getSection(); |
224 | 243 | if (Section.find("__message_refs") != StringRef::npos || |
225 | 225 | Section.find("__objc_classrefs") != StringRef::npos || |
226 | 160 | Section.find("__objc_superrefs") != StringRef::npos || |
227 | 142 | Section.find("__objc_methname") != StringRef::npos || |
228 | 124 | Section.find("__cstring") != StringRef::npos) |
229 | 137 | return true; |
230 | 265 | } |
231 | 425 | } |
232 | 602 | |
233 | 443 | return false; |
234 | 1.38k | } |
235 | | |
236 | | enum class ARCMDKindID { |
237 | | ImpreciseRelease, |
238 | | CopyOnEscape, |
239 | | NoObjCARCExceptions, |
240 | | }; |
241 | | |
242 | | /// A cache of MDKinds used by various ARC optimizations. |
243 | | class ARCMDKindCache { |
244 | | Module *M; |
245 | | |
246 | | /// The Metadata Kind for clang.imprecise_release metadata. |
247 | | llvm::Optional<unsigned> ImpreciseReleaseMDKind; |
248 | | |
249 | | /// The Metadata Kind for clang.arc.copy_on_escape metadata. |
250 | | llvm::Optional<unsigned> CopyOnEscapeMDKind; |
251 | | |
252 | | /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata. |
253 | | llvm::Optional<unsigned> NoObjCARCExceptionsMDKind; |
254 | | |
255 | | public: |
256 | 27 | void init(Module *Mod) { |
257 | 27 | M = Mod; |
258 | 27 | ImpreciseReleaseMDKind = NoneType::None; |
259 | 27 | CopyOnEscapeMDKind = NoneType::None; |
260 | 27 | NoObjCARCExceptionsMDKind = NoneType::None; |
261 | 27 | } |
262 | | |
263 | 840 | unsigned get(ARCMDKindID ID) { |
264 | 840 | switch (ID) { |
265 | 649 | case ARCMDKindID::ImpreciseRelease: |
266 | 649 | if (!ImpreciseReleaseMDKind) |
267 | 21 | ImpreciseReleaseMDKind = |
268 | 21 | M->getContext().getMDKindID("clang.imprecise_release"); |
269 | 649 | return *ImpreciseReleaseMDKind; |
270 | 0 | case ARCMDKindID::CopyOnEscape: |
271 | 0 | if (!CopyOnEscapeMDKind) |
272 | 0 | CopyOnEscapeMDKind = |
273 | 0 | M->getContext().getMDKindID("clang.arc.copy_on_escape"); |
274 | 0 | return *CopyOnEscapeMDKind; |
275 | 191 | case ARCMDKindID::NoObjCARCExceptions: |
276 | 191 | if (!NoObjCARCExceptionsMDKind) |
277 | 21 | NoObjCARCExceptionsMDKind = |
278 | 21 | M->getContext().getMDKindID("clang.arc.no_objc_arc_exceptions"); |
279 | 191 | return *NoObjCARCExceptionsMDKind; |
280 | 840 | } |
281 | 0 | llvm_unreachable0 ("Covered switch isn't covered?!"); |
282 | | } |
283 | | }; |
284 | | |
285 | | } // end namespace objcarc |
286 | | } // end namespace llvm |
287 | | |
288 | | #endif |