LLVM  7.0.0svn
ObjCARCInstKind.cpp
Go to the documentation of this file.
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 
23 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/IR/Intrinsics.h"
26 
27 using namespace llvm;
28 using namespace llvm::objcarc;
29 
31  const ARCInstKind Class) {
32  switch (Class) {
34  return OS << "ARCInstKind::Retain";
36  return OS << "ARCInstKind::RetainRV";
38  return OS << "ARCInstKind::ClaimRV";
40  return OS << "ARCInstKind::RetainBlock";
42  return OS << "ARCInstKind::Release";
44  return OS << "ARCInstKind::Autorelease";
46  return OS << "ARCInstKind::AutoreleaseRV";
48  return OS << "ARCInstKind::AutoreleasepoolPush";
50  return OS << "ARCInstKind::AutoreleasepoolPop";
52  return OS << "ARCInstKind::NoopCast";
54  return OS << "ARCInstKind::FusedRetainAutorelease";
56  return OS << "ARCInstKind::FusedRetainAutoreleaseRV";
58  return OS << "ARCInstKind::LoadWeakRetained";
60  return OS << "ARCInstKind::StoreWeak";
62  return OS << "ARCInstKind::InitWeak";
64  return OS << "ARCInstKind::LoadWeak";
66  return OS << "ARCInstKind::MoveWeak";
68  return OS << "ARCInstKind::CopyWeak";
70  return OS << "ARCInstKind::DestroyWeak";
72  return OS << "ARCInstKind::StoreStrong";
74  return OS << "ARCInstKind::CallOrUser";
75  case ARCInstKind::Call:
76  return OS << "ARCInstKind::Call";
77  case ARCInstKind::User:
78  return OS << "ARCInstKind::User";
80  return OS << "ARCInstKind::IntrinsicUser";
81  case ARCInstKind::None:
82  return OS << "ARCInstKind::None";
83  }
84  llvm_unreachable("Unknown instruction class!");
85 }
86 
88  Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
89 
90  // No (mandatory) arguments.
91  if (AI == AE)
93  .Case("objc_autoreleasePoolPush", ARCInstKind::AutoreleasepoolPush)
94  .Case("clang.arc.use", ARCInstKind::IntrinsicUser)
96 
97  // One argument.
98  const Argument *A0 = &*AI++;
99  if (AI == AE) {
100  // Argument is a pointer.
101  PointerType *PTy = dyn_cast<PointerType>(A0->getType());
102  if (!PTy)
104 
105  Type *ETy = PTy->getElementType();
106  // Argument is i8*.
107  if (ETy->isIntegerTy(8))
109  .Case("objc_retain", ARCInstKind::Retain)
110  .Case("objc_retainAutoreleasedReturnValue", ARCInstKind::RetainRV)
111  .Case("objc_unsafeClaimAutoreleasedReturnValue", ARCInstKind::ClaimRV)
112  .Case("objc_retainBlock", ARCInstKind::RetainBlock)
113  .Case("objc_release", ARCInstKind::Release)
114  .Case("objc_autorelease", ARCInstKind::Autorelease)
115  .Case("objc_autoreleaseReturnValue", ARCInstKind::AutoreleaseRV)
116  .Case("objc_autoreleasePoolPop", ARCInstKind::AutoreleasepoolPop)
117  .Case("objc_retainedObject", ARCInstKind::NoopCast)
118  .Case("objc_unretainedObject", ARCInstKind::NoopCast)
119  .Case("objc_unretainedPointer", ARCInstKind::NoopCast)
120  .Case("objc_retain_autorelease", ARCInstKind::FusedRetainAutorelease)
121  .Case("objc_retainAutorelease", ARCInstKind::FusedRetainAutorelease)
122  .Case("objc_retainAutoreleaseReturnValue",
124  .Case("objc_sync_enter", ARCInstKind::User)
125  .Case("objc_sync_exit", ARCInstKind::User)
127 
128  // Argument is i8**
129  if (PointerType *Pte = dyn_cast<PointerType>(ETy))
130  if (Pte->getElementType()->isIntegerTy(8))
132  .Case("objc_loadWeakRetained", ARCInstKind::LoadWeakRetained)
133  .Case("objc_loadWeak", ARCInstKind::LoadWeak)
134  .Case("objc_destroyWeak", ARCInstKind::DestroyWeak)
136 
137  // Anything else with one argument.
139  }
140 
141  // Two arguments, first is i8**.
142  const Argument *A1 = &*AI++;
143  if (AI == AE)
144  if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
145  if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
146  if (Pte->getElementType()->isIntegerTy(8))
147  if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
148  Type *ETy1 = PTy1->getElementType();
149  // Second argument is i8*
150  if (ETy1->isIntegerTy(8))
152  .Case("objc_storeWeak", ARCInstKind::StoreWeak)
153  .Case("objc_initWeak", ARCInstKind::InitWeak)
154  .Case("objc_storeStrong", ARCInstKind::StoreStrong)
156  // Second argument is i8**.
157  if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
158  if (Pte1->getElementType()->isIntegerTy(8))
160  .Case("objc_moveWeak", ARCInstKind::MoveWeak)
161  .Case("objc_copyWeak", ARCInstKind::CopyWeak)
162  // Ignore annotation calls. This is important to stop the
163  // optimizer from treating annotations as uses which would
164  // make the state of the pointers they are attempting to
165  // elucidate to be incorrect.
166  .Case("llvm.arc.annotation.topdown.bbstart",
168  .Case("llvm.arc.annotation.topdown.bbend",
170  .Case("llvm.arc.annotation.bottomup.bbstart",
172  .Case("llvm.arc.annotation.bottomup.bbend",
175  }
176 
177  // Anything else.
179 }
180 
181 // A whitelist of intrinsics that we know do not use objc pointers or decrement
182 // ref counts.
183 static bool isInertIntrinsic(unsigned ID) {
184  // TODO: Make this into a covered switch.
185  switch (ID) {
186  case Intrinsic::returnaddress:
187  case Intrinsic::addressofreturnaddress:
188  case Intrinsic::frameaddress:
189  case Intrinsic::stacksave:
190  case Intrinsic::stackrestore:
191  case Intrinsic::vastart:
192  case Intrinsic::vacopy:
193  case Intrinsic::vaend:
194  case Intrinsic::objectsize:
195  case Intrinsic::prefetch:
196  case Intrinsic::stackprotector:
197  case Intrinsic::eh_return_i32:
198  case Intrinsic::eh_return_i64:
199  case Intrinsic::eh_typeid_for:
200  case Intrinsic::eh_dwarf_cfa:
201  case Intrinsic::eh_sjlj_lsda:
202  case Intrinsic::eh_sjlj_functioncontext:
203  case Intrinsic::init_trampoline:
204  case Intrinsic::adjust_trampoline:
205  case Intrinsic::lifetime_start:
206  case Intrinsic::lifetime_end:
207  case Intrinsic::invariant_start:
208  case Intrinsic::invariant_end:
209  // Don't let dbg info affect our results.
210  case Intrinsic::dbg_declare:
211  case Intrinsic::dbg_value:
212  case Intrinsic::dbg_label:
213  // Short cut: Some intrinsics obviously don't use ObjC pointers.
214  return true;
215  default:
216  return false;
217  }
218 }
219 
220 // A whitelist of intrinsics that we know do not use objc pointers or decrement
221 // ref counts.
222 static bool isUseOnlyIntrinsic(unsigned ID) {
223  // We are conservative and even though intrinsics are unlikely to touch
224  // reference counts, we white list them for safety.
225  //
226  // TODO: Expand this into a covered switch. There is a lot more here.
227  switch (ID) {
228  case Intrinsic::memcpy:
229  case Intrinsic::memmove:
230  case Intrinsic::memset:
231  return true;
232  default:
233  return false;
234  }
235 }
236 
237 /// Determine what kind of construct V is.
239  if (const Instruction *I = dyn_cast<Instruction>(V)) {
240  // Any instruction other than bitcast and gep with a pointer operand have a
241  // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
242  // to a subsequent use, rather than using it themselves, in this sense.
243  // As a short cut, several other opcodes are known to have no pointer
244  // operands of interest. And ret is never followed by a release, so it's
245  // not interesting to examine.
246  switch (I->getOpcode()) {
247  case Instruction::Call: {
248  const CallInst *CI = cast<CallInst>(I);
249  // See if we have a function that we know something about.
250  if (const Function *F = CI->getCalledFunction()) {
251  ARCInstKind Class = GetFunctionClass(F);
252  if (Class != ARCInstKind::CallOrUser)
253  return Class;
254  Intrinsic::ID ID = F->getIntrinsicID();
255  if (isInertIntrinsic(ID))
256  return ARCInstKind::None;
257  if (isUseOnlyIntrinsic(ID))
258  return ARCInstKind::User;
259  }
260 
261  // Otherwise, be conservative.
262  return GetCallSiteClass(CI);
263  }
264  case Instruction::Invoke:
265  // Otherwise, be conservative.
266  return GetCallSiteClass(cast<InvokeInst>(I));
267  case Instruction::BitCast:
268  case Instruction::GetElementPtr:
269  case Instruction::Select:
270  case Instruction::PHI:
271  case Instruction::Ret:
272  case Instruction::Br:
273  case Instruction::Switch:
274  case Instruction::IndirectBr:
275  case Instruction::Alloca:
276  case Instruction::VAArg:
277  case Instruction::Add:
278  case Instruction::FAdd:
279  case Instruction::Sub:
280  case Instruction::FSub:
281  case Instruction::Mul:
282  case Instruction::FMul:
283  case Instruction::SDiv:
284  case Instruction::UDiv:
285  case Instruction::FDiv:
286  case Instruction::SRem:
287  case Instruction::URem:
288  case Instruction::FRem:
289  case Instruction::Shl:
290  case Instruction::LShr:
291  case Instruction::AShr:
292  case Instruction::And:
293  case Instruction::Or:
294  case Instruction::Xor:
295  case Instruction::SExt:
296  case Instruction::ZExt:
297  case Instruction::Trunc:
298  case Instruction::IntToPtr:
299  case Instruction::FCmp:
300  case Instruction::FPTrunc:
301  case Instruction::FPExt:
302  case Instruction::FPToUI:
303  case Instruction::FPToSI:
304  case Instruction::UIToFP:
305  case Instruction::SIToFP:
306  case Instruction::InsertElement:
307  case Instruction::ExtractElement:
308  case Instruction::ShuffleVector:
309  case Instruction::ExtractValue:
310  break;
311  case Instruction::ICmp:
312  // Comparing a pointer with null, or any other constant, isn't an
313  // interesting use, because we don't care what the pointer points to, or
314  // about the values of any other dynamic reference-counted pointers.
315  if (IsPotentialRetainableObjPtr(I->getOperand(1)))
316  return ARCInstKind::User;
317  break;
318  default:
319  // For anything else, check all the operands.
320  // Note that this includes both operands of a Store: while the first
321  // operand isn't actually being dereferenced, it is being stored to
322  // memory where we can no longer track who might read it and dereference
323  // it, so we have to consider it potentially used.
324  for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
325  OI != OE; ++OI)
327  return ARCInstKind::User;
328  }
329  }
330 
331  // Otherwise, it's totally inert for ARC purposes.
332  return ARCInstKind::None;
333 }
334 
335 /// Test if the given class is a kind of user.
337  switch (Class) {
338  case ARCInstKind::User:
341  return true;
342  case ARCInstKind::Retain:
361  case ARCInstKind::Call:
362  case ARCInstKind::None:
364  return false;
365  }
366  llvm_unreachable("covered switch isn't covered?");
367 }
368 
369 /// Test if the given class is objc_retain or equivalent.
371  switch (Class) {
372  case ARCInstKind::Retain:
374  return true;
375  // I believe we treat retain block as not a retain since it can copy its
376  // block.
396  case ARCInstKind::Call:
397  case ARCInstKind::User:
398  case ARCInstKind::None:
400  return false;
401  }
402  llvm_unreachable("covered switch isn't covered?");
403 }
404 
405 /// Test if the given class is objc_autorelease or equivalent.
407  switch (Class) {
410  return true;
411  case ARCInstKind::Retain:
431  case ARCInstKind::Call:
432  case ARCInstKind::User:
433  case ARCInstKind::None:
434  return false;
435  }
436  llvm_unreachable("covered switch isn't covered?");
437 }
438 
439 /// Test if the given class represents instructions which return their
440 /// argument verbatim.
442  switch (Class) {
443  case ARCInstKind::Retain:
449  return true;
466  case ARCInstKind::Call:
467  case ARCInstKind::User:
468  case ARCInstKind::None:
469  return false;
470  }
471  llvm_unreachable("covered switch isn't covered?");
472 }
473 
474 /// Test if the given class represents instructions which do nothing if
475 /// passed a null pointer.
477  switch (Class) {
478  case ARCInstKind::Retain:
485  return true;
500  case ARCInstKind::Call:
501  case ARCInstKind::User:
502  case ARCInstKind::None:
504  return false;
505  }
506  llvm_unreachable("covered switch isn't covered?");
507 }
508 
509 /// Test if the given class represents instructions which are always safe
510 /// to mark with the "tail" keyword.
512  // ARCInstKind::RetainBlock may be given a stack argument.
513  switch (Class) {
514  case ARCInstKind::Retain:
518  return true;
536  case ARCInstKind::Call:
537  case ARCInstKind::User:
538  case ARCInstKind::None:
540  return false;
541  }
542  llvm_unreachable("covered switch isn't covered?");
543 }
544 
545 /// Test if the given class represents instructions which are never safe
546 /// to mark with the "tail" keyword.
548  /// It is never safe to tail call objc_autorelease since by tail calling
549  /// objc_autorelease: fast autoreleasing causing our object to be potentially
550  /// reclaimed from the autorelease pool which violates the semantics of
551  /// __autoreleasing types in ARC.
552  switch (Class) {
554  return true;
555  case ARCInstKind::Retain:
575  case ARCInstKind::Call:
576  case ARCInstKind::User:
577  case ARCInstKind::None:
579  return false;
580  }
581  llvm_unreachable("covered switch isn't covered?");
582 }
583 
584 /// Test if the given class represents instructions which are always safe
585 /// to mark with the nounwind attribute.
587  // objc_retainBlock is not nounwind because it calls user copy constructors
588  // which could theoretically throw.
589  switch (Class) {
590  case ARCInstKind::Retain:
598  return true;
612  case ARCInstKind::Call:
613  case ARCInstKind::User:
614  case ARCInstKind::None:
616  return false;
617  }
618  llvm_unreachable("covered switch isn't covered?");
619 }
620 
621 /// Test whether the given instruction can autorelease any pointer or cause an
622 /// autoreleasepool pop.
623 ///
624 /// This means that it *could* interrupt the RV optimization.
626  switch (Class) {
629  case ARCInstKind::Call:
634  return true;
635  case ARCInstKind::Retain:
650  case ARCInstKind::User:
651  case ARCInstKind::None:
653  return false;
654  }
655  llvm_unreachable("covered switch isn't covered?");
656 }
657 
659  switch (Kind) {
660  case ARCInstKind::Retain:
668  case ARCInstKind::User:
669  case ARCInstKind::None:
670  return false;
671 
672  // The cases below are conservative.
673 
674  // RetainBlock can result in user defined copy constructors being called
675  // implying releases may occur.
689  case ARCInstKind::Call:
691  return true;
692  }
693 
694  llvm_unreachable("covered switch isn't covered?");
695 }
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
ARCInstKind GetARCInstKind(const Value *V)
Map V to its ARCInstKind equivalence class.
objc_destroyWeak (derived)
bool IsUser(ARCInstKind Class)
Test if the given class is a kind of user.
This class represents an incoming formal argument to a Function.
Definition: Argument.h:30
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
objc_loadWeakRetained (primitive)
could call objc_release and/or "use" pointers
objc_loadWeak (derived)
This class represents a function call, abstracting a target machine&#39;s calling convention.
objc_retainedObject, etc.
arg_iterator arg_end()
Definition: Function.h:666
F(f)
could call objc_release
loop data prefetch
objc_moveWeak (derived)
bool IsNoopOnNull(ARCInstKind Class)
Test if the given class represents instructions which do nothing if passed a null pointer...
bool IsForwarding(ARCInstKind Class)
Test if the given class represents instructions which return their argument verbatim.
objc_autoreleaseReturnValue
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(T Value)
Definition: StringSwitch.h:203
objc_retainAutoreleasedReturnValue
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
bool IsPotentialRetainableObjPtr(const Value *Op)
Test whether the given value is possible a retainable object pointer.
bool IsAlwaysTail(ARCInstKind Class)
Test if the given class represents instructions which are always safe to mark with the "tail" keyword...
Class to represent pointers.
Definition: DerivedTypes.h:467
raw_ostream & operator<<(raw_ostream &OS, const ARCInstKind Class)
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
objc_initWeak (derived)
ARCInstKind GetFunctionClass(const Function *F)
Determine if F is one of the special known Functions.
arg_iterator arg_begin()
Definition: Function.h:657
objc_copyWeak (derived)
This file defines common analysis utilities used by the ObjC ARC Optimizer.
anything that is inert from an ARC perspective.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool CanDecrementRefCount(ARCInstKind Kind)
Returns false if conservatively we can prove that any instruction mapped to this kind can not decreme...
static bool isInertIntrinsic(unsigned ID)
ARCInstKind
Equivalence classes of instructions in the ARC Model.
bool IsNoThrow(ARCInstKind Class)
Test if the given class represents instructions which are always safe to mark with the nounwind attri...
objc_unsafeClaimAutoreleasedReturnValue
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:70
objc_storeStrong (derived)
could "use" a pointer
bool IsRetain(ARCInstKind Class)
Test if the given class is objc_retain or equivalent.
static bool isUseOnlyIntrinsic(unsigned ID)
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:224
#define I(x, y, z)
Definition: MD5.cpp:58
objc_storeWeak (primitive)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
ARCInstKind GetCallSiteClass(ImmutableCallSite CS)
Helper for GetARCInstKind.
const unsigned Kind
LLVM Value Representation.
Definition: Value.h:73
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
bool CanInterruptRV(ARCInstKind Class)
Test whether the given instruction can autorelease any pointer or cause an autoreleasepool pop...
bool IsNeverTail(ARCInstKind Class)
Test if the given class represents instructions which are never safe to mark with the "tail" keyword...
Type * getElementType() const
Definition: DerivedTypes.h:486
bool IsAutorelease(ARCInstKind Class)
Test if the given class is objc_autorelease or equivalent.