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