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