1 //===- ARCInstKind.cpp - ObjC ARC Optimization ----------------------------===//
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 several utility functions used by various ARC
11 /// optimizations which are IMHO too big to be in a header file.
12 ///
13 /// WARNING: This file knows about certain library functions. It recognizes them
14 /// by name, and hardwires knowledge of their semantics.
15 ///
16 /// WARNING: This file knows about how certain Objective-C library functions are
17 /// used. Naive LLVM IR transformations which would otherwise be
18 /// behavior-preserving may break these assumptions.
19 ///
20 //===----------------------------------------------------------------------===//
21 
22 #include "ObjCARC.h"
23 #include "llvm/IR/Intrinsics.h"
24 
25 using namespace llvm;
26 using namespace llvm::objcarc;
27 
operator <<(raw_ostream & OS,const ARCInstKind Class)28 raw_ostream &llvm::objcarc::operator<<(raw_ostream &OS,
29                                        const ARCInstKind Class) {
30   switch (Class) {
31   case ARCInstKind::Retain:
32     return OS << "ARCInstKind::Retain";
33   case ARCInstKind::RetainRV:
34     return OS << "ARCInstKind::RetainRV";
35   case ARCInstKind::RetainBlock:
36     return OS << "ARCInstKind::RetainBlock";
37   case ARCInstKind::Release:
38     return OS << "ARCInstKind::Release";
39   case ARCInstKind::Autorelease:
40     return OS << "ARCInstKind::Autorelease";
41   case ARCInstKind::AutoreleaseRV:
42     return OS << "ARCInstKind::AutoreleaseRV";
43   case ARCInstKind::AutoreleasepoolPush:
44     return OS << "ARCInstKind::AutoreleasepoolPush";
45   case ARCInstKind::AutoreleasepoolPop:
46     return OS << "ARCInstKind::AutoreleasepoolPop";
47   case ARCInstKind::NoopCast:
48     return OS << "ARCInstKind::NoopCast";
49   case ARCInstKind::FusedRetainAutorelease:
50     return OS << "ARCInstKind::FusedRetainAutorelease";
51   case ARCInstKind::FusedRetainAutoreleaseRV:
52     return OS << "ARCInstKind::FusedRetainAutoreleaseRV";
53   case ARCInstKind::LoadWeakRetained:
54     return OS << "ARCInstKind::LoadWeakRetained";
55   case ARCInstKind::StoreWeak:
56     return OS << "ARCInstKind::StoreWeak";
57   case ARCInstKind::InitWeak:
58     return OS << "ARCInstKind::InitWeak";
59   case ARCInstKind::LoadWeak:
60     return OS << "ARCInstKind::LoadWeak";
61   case ARCInstKind::MoveWeak:
62     return OS << "ARCInstKind::MoveWeak";
63   case ARCInstKind::CopyWeak:
64     return OS << "ARCInstKind::CopyWeak";
65   case ARCInstKind::DestroyWeak:
66     return OS << "ARCInstKind::DestroyWeak";
67   case ARCInstKind::StoreStrong:
68     return OS << "ARCInstKind::StoreStrong";
69   case ARCInstKind::CallOrUser:
70     return OS << "ARCInstKind::CallOrUser";
71   case ARCInstKind::Call:
72     return OS << "ARCInstKind::Call";
73   case ARCInstKind::User:
74     return OS << "ARCInstKind::User";
75   case ARCInstKind::IntrinsicUser:
76     return OS << "ARCInstKind::IntrinsicUser";
77   case ARCInstKind::None:
78     return OS << "ARCInstKind::None";
79   }
80   llvm_unreachable("Unknown instruction class!");
81 }
82 
GetFunctionClass(const Function * F)83 ARCInstKind llvm::objcarc::GetFunctionClass(const Function *F) {
84   Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
85 
86   // No (mandatory) arguments.
87   if (AI == AE)
88     return StringSwitch<ARCInstKind>(F->getName())
89         .Case("objc_autoreleasePoolPush", ARCInstKind::AutoreleasepoolPush)
90         .Case("clang.arc.use", ARCInstKind::IntrinsicUser)
91         .Default(ARCInstKind::CallOrUser);
92 
93   // One argument.
94   const Argument *A0 = AI++;
95   if (AI == AE)
96     // Argument is a pointer.
97     if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) {
98       Type *ETy = PTy->getElementType();
99       // Argument is i8*.
100       if (ETy->isIntegerTy(8))
101         return StringSwitch<ARCInstKind>(F->getName())
102             .Case("objc_retain", ARCInstKind::Retain)
103             .Case("objc_retainAutoreleasedReturnValue", ARCInstKind::RetainRV)
104             .Case("objc_retainBlock", ARCInstKind::RetainBlock)
105             .Case("objc_release", ARCInstKind::Release)
106             .Case("objc_autorelease", ARCInstKind::Autorelease)
107             .Case("objc_autoreleaseReturnValue", ARCInstKind::AutoreleaseRV)
108             .Case("objc_autoreleasePoolPop", ARCInstKind::AutoreleasepoolPop)
109             .Case("objc_retainedObject", ARCInstKind::NoopCast)
110             .Case("objc_unretainedObject", ARCInstKind::NoopCast)
111             .Case("objc_unretainedPointer", ARCInstKind::NoopCast)
112             .Case("objc_retain_autorelease",
113                   ARCInstKind::FusedRetainAutorelease)
114             .Case("objc_retainAutorelease", ARCInstKind::FusedRetainAutorelease)
115             .Case("objc_retainAutoreleaseReturnValue",
116                   ARCInstKind::FusedRetainAutoreleaseRV)
117             .Case("objc_sync_enter", ARCInstKind::User)
118             .Case("objc_sync_exit", ARCInstKind::User)
119             .Default(ARCInstKind::CallOrUser);
120 
121       // Argument is i8**
122       if (PointerType *Pte = dyn_cast<PointerType>(ETy))
123         if (Pte->getElementType()->isIntegerTy(8))
124           return StringSwitch<ARCInstKind>(F->getName())
125               .Case("objc_loadWeakRetained", ARCInstKind::LoadWeakRetained)
126               .Case("objc_loadWeak", ARCInstKind::LoadWeak)
127               .Case("objc_destroyWeak", ARCInstKind::DestroyWeak)
128               .Default(ARCInstKind::CallOrUser);
129     }
130 
131   // Two arguments, first is i8**.
132   const Argument *A1 = AI++;
133   if (AI == AE)
134     if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
135       if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
136         if (Pte->getElementType()->isIntegerTy(8))
137           if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
138             Type *ETy1 = PTy1->getElementType();
139             // Second argument is i8*
140             if (ETy1->isIntegerTy(8))
141               return StringSwitch<ARCInstKind>(F->getName())
142                   .Case("objc_storeWeak", ARCInstKind::StoreWeak)
143                   .Case("objc_initWeak", ARCInstKind::InitWeak)
144                   .Case("objc_storeStrong", ARCInstKind::StoreStrong)
145                   .Default(ARCInstKind::CallOrUser);
146             // Second argument is i8**.
147             if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
148               if (Pte1->getElementType()->isIntegerTy(8))
149                 return StringSwitch<ARCInstKind>(F->getName())
150                     .Case("objc_moveWeak", ARCInstKind::MoveWeak)
151                     .Case("objc_copyWeak", ARCInstKind::CopyWeak)
152                     // Ignore annotation calls. This is important to stop the
153                     // optimizer from treating annotations as uses which would
154                     // make the state of the pointers they are attempting to
155                     // elucidate to be incorrect.
156                     .Case("llvm.arc.annotation.topdown.bbstart",
157                           ARCInstKind::None)
158                     .Case("llvm.arc.annotation.topdown.bbend",
159                           ARCInstKind::None)
160                     .Case("llvm.arc.annotation.bottomup.bbstart",
161                           ARCInstKind::None)
162                     .Case("llvm.arc.annotation.bottomup.bbend",
163                           ARCInstKind::None)
164                     .Default(ARCInstKind::CallOrUser);
165           }
166 
167   // Anything else.
168   return ARCInstKind::CallOrUser;
169 }
170 
171 // A whitelist of intrinsics that we know do not use objc pointers or decrement
172 // ref counts.
isInertIntrinsic(unsigned ID)173 static bool isInertIntrinsic(unsigned ID) {
174   // TODO: Make this into a covered switch.
175   switch (ID) {
176   case Intrinsic::returnaddress:
177   case Intrinsic::frameaddress:
178   case Intrinsic::stacksave:
179   case Intrinsic::stackrestore:
180   case Intrinsic::vastart:
181   case Intrinsic::vacopy:
182   case Intrinsic::vaend:
183   case Intrinsic::objectsize:
184   case Intrinsic::prefetch:
185   case Intrinsic::stackprotector:
186   case Intrinsic::eh_return_i32:
187   case Intrinsic::eh_return_i64:
188   case Intrinsic::eh_typeid_for:
189   case Intrinsic::eh_dwarf_cfa:
190   case Intrinsic::eh_sjlj_lsda:
191   case Intrinsic::eh_sjlj_functioncontext:
192   case Intrinsic::init_trampoline:
193   case Intrinsic::adjust_trampoline:
194   case Intrinsic::lifetime_start:
195   case Intrinsic::lifetime_end:
196   case Intrinsic::invariant_start:
197   case Intrinsic::invariant_end:
198   // Don't let dbg info affect our results.
199   case Intrinsic::dbg_declare:
200   case Intrinsic::dbg_value:
201     // Short cut: Some intrinsics obviously don't use ObjC pointers.
202     return true;
203   default:
204     return false;
205   }
206 }
207 
208 // A whitelist of intrinsics that we know do not use objc pointers or decrement
209 // ref counts.
isUseOnlyIntrinsic(unsigned ID)210 static bool isUseOnlyIntrinsic(unsigned ID) {
211   // We are conservative and even though intrinsics are unlikely to touch
212   // reference counts, we white list them for safety.
213   //
214   // TODO: Expand this into a covered switch. There is a lot more here.
215   switch (ID) {
216   case Intrinsic::memcpy:
217   case Intrinsic::memmove:
218   case Intrinsic::memset:
219     return true;
220   default:
221     return false;
222   }
223 }
224 
225 /// \brief Determine what kind of construct V is.
GetARCInstKind(const Value * V)226 ARCInstKind llvm::objcarc::GetARCInstKind(const Value *V) {
227   if (const Instruction *I = dyn_cast<Instruction>(V)) {
228     // Any instruction other than bitcast and gep with a pointer operand have a
229     // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
230     // to a subsequent use, rather than using it themselves, in this sense.
231     // As a short cut, several other opcodes are known to have no pointer
232     // operands of interest. And ret is never followed by a release, so it's
233     // not interesting to examine.
234     switch (I->getOpcode()) {
235     case Instruction::Call: {
236       const CallInst *CI = cast<CallInst>(I);
237       // See if we have a function that we know something about.
238       if (const Function *F = CI->getCalledFunction()) {
239         ARCInstKind Class = GetFunctionClass(F);
240         if (Class != ARCInstKind::CallOrUser)
241           return Class;
242         unsigned ID = F->getIntrinsicID();
243         if (isInertIntrinsic(ID))
244           return ARCInstKind::None;
245         if (isUseOnlyIntrinsic(ID))
246           return ARCInstKind::User;
247       }
248 
249       // Otherwise, be conservative.
250       return GetCallSiteClass(CI);
251     }
252     case Instruction::Invoke:
253       // Otherwise, be conservative.
254       return GetCallSiteClass(cast<InvokeInst>(I));
255     case Instruction::BitCast:
256     case Instruction::GetElementPtr:
257     case Instruction::Select:
258     case Instruction::PHI:
259     case Instruction::Ret:
260     case Instruction::Br:
261     case Instruction::Switch:
262     case Instruction::IndirectBr:
263     case Instruction::Alloca:
264     case Instruction::VAArg:
265     case Instruction::Add:
266     case Instruction::FAdd:
267     case Instruction::Sub:
268     case Instruction::FSub:
269     case Instruction::Mul:
270     case Instruction::FMul:
271     case Instruction::SDiv:
272     case Instruction::UDiv:
273     case Instruction::FDiv:
274     case Instruction::SRem:
275     case Instruction::URem:
276     case Instruction::FRem:
277     case Instruction::Shl:
278     case Instruction::LShr:
279     case Instruction::AShr:
280     case Instruction::And:
281     case Instruction::Or:
282     case Instruction::Xor:
283     case Instruction::SExt:
284     case Instruction::ZExt:
285     case Instruction::Trunc:
286     case Instruction::IntToPtr:
287     case Instruction::FCmp:
288     case Instruction::FPTrunc:
289     case Instruction::FPExt:
290     case Instruction::FPToUI:
291     case Instruction::FPToSI:
292     case Instruction::UIToFP:
293     case Instruction::SIToFP:
294     case Instruction::InsertElement:
295     case Instruction::ExtractElement:
296     case Instruction::ShuffleVector:
297     case Instruction::ExtractValue:
298       break;
299     case Instruction::ICmp:
300       // Comparing a pointer with null, or any other constant, isn't an
301       // interesting use, because we don't care what the pointer points to, or
302       // about the values of any other dynamic reference-counted pointers.
303       if (IsPotentialRetainableObjPtr(I->getOperand(1)))
304         return ARCInstKind::User;
305       break;
306     default:
307       // For anything else, check all the operands.
308       // Note that this includes both operands of a Store: while the first
309       // operand isn't actually being dereferenced, it is being stored to
310       // memory where we can no longer track who might read it and dereference
311       // it, so we have to consider it potentially used.
312       for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
313            OI != OE; ++OI)
314         if (IsPotentialRetainableObjPtr(*OI))
315           return ARCInstKind::User;
316     }
317   }
318 
319   // Otherwise, it's totally inert for ARC purposes.
320   return ARCInstKind::None;
321 }
322 
323 /// \brief Test if the given class is a kind of user.
IsUser(ARCInstKind Class)324 bool llvm::objcarc::IsUser(ARCInstKind Class) {
325   switch (Class) {
326   case ARCInstKind::User:
327   case ARCInstKind::CallOrUser:
328   case ARCInstKind::IntrinsicUser:
329     return true;
330   case ARCInstKind::Retain:
331   case ARCInstKind::RetainRV:
332   case ARCInstKind::RetainBlock:
333   case ARCInstKind::Release:
334   case ARCInstKind::Autorelease:
335   case ARCInstKind::AutoreleaseRV:
336   case ARCInstKind::AutoreleasepoolPush:
337   case ARCInstKind::AutoreleasepoolPop:
338   case ARCInstKind::NoopCast:
339   case ARCInstKind::FusedRetainAutorelease:
340   case ARCInstKind::FusedRetainAutoreleaseRV:
341   case ARCInstKind::LoadWeakRetained:
342   case ARCInstKind::StoreWeak:
343   case ARCInstKind::InitWeak:
344   case ARCInstKind::LoadWeak:
345   case ARCInstKind::MoveWeak:
346   case ARCInstKind::CopyWeak:
347   case ARCInstKind::DestroyWeak:
348   case ARCInstKind::StoreStrong:
349   case ARCInstKind::Call:
350   case ARCInstKind::None:
351     return false;
352   }
353   llvm_unreachable("covered switch isn't covered?");
354 }
355 
356 /// \brief Test if the given class is objc_retain or equivalent.
IsRetain(ARCInstKind Class)357 bool llvm::objcarc::IsRetain(ARCInstKind Class) {
358   switch (Class) {
359   case ARCInstKind::Retain:
360   case ARCInstKind::RetainRV:
361     return true;
362   // I believe we treat retain block as not a retain since it can copy its
363   // block.
364   case ARCInstKind::RetainBlock:
365   case ARCInstKind::Release:
366   case ARCInstKind::Autorelease:
367   case ARCInstKind::AutoreleaseRV:
368   case ARCInstKind::AutoreleasepoolPush:
369   case ARCInstKind::AutoreleasepoolPop:
370   case ARCInstKind::NoopCast:
371   case ARCInstKind::FusedRetainAutorelease:
372   case ARCInstKind::FusedRetainAutoreleaseRV:
373   case ARCInstKind::LoadWeakRetained:
374   case ARCInstKind::StoreWeak:
375   case ARCInstKind::InitWeak:
376   case ARCInstKind::LoadWeak:
377   case ARCInstKind::MoveWeak:
378   case ARCInstKind::CopyWeak:
379   case ARCInstKind::DestroyWeak:
380   case ARCInstKind::StoreStrong:
381   case ARCInstKind::IntrinsicUser:
382   case ARCInstKind::CallOrUser:
383   case ARCInstKind::Call:
384   case ARCInstKind::User:
385   case ARCInstKind::None:
386     return false;
387   }
388   llvm_unreachable("covered switch isn't covered?");
389 }
390 
391 /// \brief Test if the given class is objc_autorelease or equivalent.
IsAutorelease(ARCInstKind Class)392 bool llvm::objcarc::IsAutorelease(ARCInstKind Class) {
393   switch (Class) {
394   case ARCInstKind::Autorelease:
395   case ARCInstKind::AutoreleaseRV:
396     return true;
397   case ARCInstKind::Retain:
398   case ARCInstKind::RetainRV:
399   case ARCInstKind::RetainBlock:
400   case ARCInstKind::Release:
401   case ARCInstKind::AutoreleasepoolPush:
402   case ARCInstKind::AutoreleasepoolPop:
403   case ARCInstKind::NoopCast:
404   case ARCInstKind::FusedRetainAutorelease:
405   case ARCInstKind::FusedRetainAutoreleaseRV:
406   case ARCInstKind::LoadWeakRetained:
407   case ARCInstKind::StoreWeak:
408   case ARCInstKind::InitWeak:
409   case ARCInstKind::LoadWeak:
410   case ARCInstKind::MoveWeak:
411   case ARCInstKind::CopyWeak:
412   case ARCInstKind::DestroyWeak:
413   case ARCInstKind::StoreStrong:
414   case ARCInstKind::IntrinsicUser:
415   case ARCInstKind::CallOrUser:
416   case ARCInstKind::Call:
417   case ARCInstKind::User:
418   case ARCInstKind::None:
419     return false;
420   }
421   llvm_unreachable("covered switch isn't covered?");
422 }
423 
424 /// \brief Test if the given class represents instructions which return their
425 /// argument verbatim.
IsForwarding(ARCInstKind Class)426 bool llvm::objcarc::IsForwarding(ARCInstKind Class) {
427   switch (Class) {
428   case ARCInstKind::Retain:
429   case ARCInstKind::RetainRV:
430   case ARCInstKind::Autorelease:
431   case ARCInstKind::AutoreleaseRV:
432   case ARCInstKind::NoopCast:
433     return true;
434   case ARCInstKind::RetainBlock:
435   case ARCInstKind::Release:
436   case ARCInstKind::AutoreleasepoolPush:
437   case ARCInstKind::AutoreleasepoolPop:
438   case ARCInstKind::FusedRetainAutorelease:
439   case ARCInstKind::FusedRetainAutoreleaseRV:
440   case ARCInstKind::LoadWeakRetained:
441   case ARCInstKind::StoreWeak:
442   case ARCInstKind::InitWeak:
443   case ARCInstKind::LoadWeak:
444   case ARCInstKind::MoveWeak:
445   case ARCInstKind::CopyWeak:
446   case ARCInstKind::DestroyWeak:
447   case ARCInstKind::StoreStrong:
448   case ARCInstKind::IntrinsicUser:
449   case ARCInstKind::CallOrUser:
450   case ARCInstKind::Call:
451   case ARCInstKind::User:
452   case ARCInstKind::None:
453     return false;
454   }
455   llvm_unreachable("covered switch isn't covered?");
456 }
457 
458 /// \brief Test if the given class represents instructions which do nothing if
459 /// passed a null pointer.
IsNoopOnNull(ARCInstKind Class)460 bool llvm::objcarc::IsNoopOnNull(ARCInstKind Class) {
461   switch (Class) {
462   case ARCInstKind::Retain:
463   case ARCInstKind::RetainRV:
464   case ARCInstKind::Release:
465   case ARCInstKind::Autorelease:
466   case ARCInstKind::AutoreleaseRV:
467   case ARCInstKind::RetainBlock:
468     return true;
469   case ARCInstKind::AutoreleasepoolPush:
470   case ARCInstKind::AutoreleasepoolPop:
471   case ARCInstKind::FusedRetainAutorelease:
472   case ARCInstKind::FusedRetainAutoreleaseRV:
473   case ARCInstKind::LoadWeakRetained:
474   case ARCInstKind::StoreWeak:
475   case ARCInstKind::InitWeak:
476   case ARCInstKind::LoadWeak:
477   case ARCInstKind::MoveWeak:
478   case ARCInstKind::CopyWeak:
479   case ARCInstKind::DestroyWeak:
480   case ARCInstKind::StoreStrong:
481   case ARCInstKind::IntrinsicUser:
482   case ARCInstKind::CallOrUser:
483   case ARCInstKind::Call:
484   case ARCInstKind::User:
485   case ARCInstKind::None:
486   case ARCInstKind::NoopCast:
487     return false;
488   }
489   llvm_unreachable("covered switch isn't covered?");
490 }
491 
492 /// \brief Test if the given class represents instructions which are always safe
493 /// to mark with the "tail" keyword.
IsAlwaysTail(ARCInstKind Class)494 bool llvm::objcarc::IsAlwaysTail(ARCInstKind Class) {
495   // ARCInstKind::RetainBlock may be given a stack argument.
496   switch (Class) {
497   case ARCInstKind::Retain:
498   case ARCInstKind::RetainRV:
499   case ARCInstKind::AutoreleaseRV:
500     return true;
501   case ARCInstKind::Release:
502   case ARCInstKind::Autorelease:
503   case ARCInstKind::RetainBlock:
504   case ARCInstKind::AutoreleasepoolPush:
505   case ARCInstKind::AutoreleasepoolPop:
506   case ARCInstKind::FusedRetainAutorelease:
507   case ARCInstKind::FusedRetainAutoreleaseRV:
508   case ARCInstKind::LoadWeakRetained:
509   case ARCInstKind::StoreWeak:
510   case ARCInstKind::InitWeak:
511   case ARCInstKind::LoadWeak:
512   case ARCInstKind::MoveWeak:
513   case ARCInstKind::CopyWeak:
514   case ARCInstKind::DestroyWeak:
515   case ARCInstKind::StoreStrong:
516   case ARCInstKind::IntrinsicUser:
517   case ARCInstKind::CallOrUser:
518   case ARCInstKind::Call:
519   case ARCInstKind::User:
520   case ARCInstKind::None:
521   case ARCInstKind::NoopCast:
522     return false;
523   }
524   llvm_unreachable("covered switch isn't covered?");
525 }
526 
527 /// \brief Test if the given class represents instructions which are never safe
528 /// to mark with the "tail" keyword.
IsNeverTail(ARCInstKind Class)529 bool llvm::objcarc::IsNeverTail(ARCInstKind Class) {
530   /// It is never safe to tail call objc_autorelease since by tail calling
531   /// objc_autorelease: fast autoreleasing causing our object to be potentially
532   /// reclaimed from the autorelease pool which violates the semantics of
533   /// __autoreleasing types in ARC.
534   switch (Class) {
535   case ARCInstKind::Autorelease:
536     return true;
537   case ARCInstKind::Retain:
538   case ARCInstKind::RetainRV:
539   case ARCInstKind::AutoreleaseRV:
540   case ARCInstKind::Release:
541   case ARCInstKind::RetainBlock:
542   case ARCInstKind::AutoreleasepoolPush:
543   case ARCInstKind::AutoreleasepoolPop:
544   case ARCInstKind::FusedRetainAutorelease:
545   case ARCInstKind::FusedRetainAutoreleaseRV:
546   case ARCInstKind::LoadWeakRetained:
547   case ARCInstKind::StoreWeak:
548   case ARCInstKind::InitWeak:
549   case ARCInstKind::LoadWeak:
550   case ARCInstKind::MoveWeak:
551   case ARCInstKind::CopyWeak:
552   case ARCInstKind::DestroyWeak:
553   case ARCInstKind::StoreStrong:
554   case ARCInstKind::IntrinsicUser:
555   case ARCInstKind::CallOrUser:
556   case ARCInstKind::Call:
557   case ARCInstKind::User:
558   case ARCInstKind::None:
559   case ARCInstKind::NoopCast:
560     return false;
561   }
562   llvm_unreachable("covered switch isn't covered?");
563 }
564 
565 /// \brief Test if the given class represents instructions which are always safe
566 /// to mark with the nounwind attribute.
IsNoThrow(ARCInstKind Class)567 bool llvm::objcarc::IsNoThrow(ARCInstKind Class) {
568   // objc_retainBlock is not nounwind because it calls user copy constructors
569   // which could theoretically throw.
570   switch (Class) {
571   case ARCInstKind::Retain:
572   case ARCInstKind::RetainRV:
573   case ARCInstKind::Release:
574   case ARCInstKind::Autorelease:
575   case ARCInstKind::AutoreleaseRV:
576   case ARCInstKind::AutoreleasepoolPush:
577   case ARCInstKind::AutoreleasepoolPop:
578     return true;
579   case ARCInstKind::RetainBlock:
580   case ARCInstKind::FusedRetainAutorelease:
581   case ARCInstKind::FusedRetainAutoreleaseRV:
582   case ARCInstKind::LoadWeakRetained:
583   case ARCInstKind::StoreWeak:
584   case ARCInstKind::InitWeak:
585   case ARCInstKind::LoadWeak:
586   case ARCInstKind::MoveWeak:
587   case ARCInstKind::CopyWeak:
588   case ARCInstKind::DestroyWeak:
589   case ARCInstKind::StoreStrong:
590   case ARCInstKind::IntrinsicUser:
591   case ARCInstKind::CallOrUser:
592   case ARCInstKind::Call:
593   case ARCInstKind::User:
594   case ARCInstKind::None:
595   case ARCInstKind::NoopCast:
596     return false;
597   }
598   llvm_unreachable("covered switch isn't covered?");
599 }
600 
601 /// Test whether the given instruction can autorelease any pointer or cause an
602 /// autoreleasepool pop.
603 ///
604 /// This means that it *could* interrupt the RV optimization.
CanInterruptRV(ARCInstKind Class)605 bool llvm::objcarc::CanInterruptRV(ARCInstKind Class) {
606   switch (Class) {
607   case ARCInstKind::AutoreleasepoolPop:
608   case ARCInstKind::CallOrUser:
609   case ARCInstKind::Call:
610   case ARCInstKind::Autorelease:
611   case ARCInstKind::AutoreleaseRV:
612   case ARCInstKind::FusedRetainAutorelease:
613   case ARCInstKind::FusedRetainAutoreleaseRV:
614     return true;
615   case ARCInstKind::Retain:
616   case ARCInstKind::RetainRV:
617   case ARCInstKind::Release:
618   case ARCInstKind::AutoreleasepoolPush:
619   case ARCInstKind::RetainBlock:
620   case ARCInstKind::LoadWeakRetained:
621   case ARCInstKind::StoreWeak:
622   case ARCInstKind::InitWeak:
623   case ARCInstKind::LoadWeak:
624   case ARCInstKind::MoveWeak:
625   case ARCInstKind::CopyWeak:
626   case ARCInstKind::DestroyWeak:
627   case ARCInstKind::StoreStrong:
628   case ARCInstKind::IntrinsicUser:
629   case ARCInstKind::User:
630   case ARCInstKind::None:
631   case ARCInstKind::NoopCast:
632     return false;
633   }
634   llvm_unreachable("covered switch isn't covered?");
635 }
636 
CanDecrementRefCount(ARCInstKind Kind)637 bool llvm::objcarc::CanDecrementRefCount(ARCInstKind Kind) {
638   switch (Kind) {
639   case ARCInstKind::Retain:
640   case ARCInstKind::RetainRV:
641   case ARCInstKind::Autorelease:
642   case ARCInstKind::AutoreleaseRV:
643   case ARCInstKind::NoopCast:
644   case ARCInstKind::FusedRetainAutorelease:
645   case ARCInstKind::FusedRetainAutoreleaseRV:
646   case ARCInstKind::IntrinsicUser:
647   case ARCInstKind::User:
648   case ARCInstKind::None:
649     return false;
650 
651   // The cases below are conservative.
652 
653   // RetainBlock can result in user defined copy constructors being called
654   // implying releases may occur.
655   case ARCInstKind::RetainBlock:
656   case ARCInstKind::Release:
657   case ARCInstKind::AutoreleasepoolPush:
658   case ARCInstKind::AutoreleasepoolPop:
659   case ARCInstKind::LoadWeakRetained:
660   case ARCInstKind::StoreWeak:
661   case ARCInstKind::InitWeak:
662   case ARCInstKind::LoadWeak:
663   case ARCInstKind::MoveWeak:
664   case ARCInstKind::CopyWeak:
665   case ARCInstKind::DestroyWeak:
666   case ARCInstKind::StoreStrong:
667   case ARCInstKind::CallOrUser:
668   case ARCInstKind::Call:
669     return true;
670   }
671 
672   llvm_unreachable("covered switch isn't covered?");
673 }
674