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