LLVM  6.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  // Short cut: Some intrinsics obviously don't use ObjC pointers.
213  return true;
214  default:
215  return false;
216  }
217 }
218 
219 // A whitelist of intrinsics that we know do not use objc pointers or decrement
220 // ref counts.
221 static bool isUseOnlyIntrinsic(unsigned ID) {
222  // We are conservative and even though intrinsics are unlikely to touch
223  // reference counts, we white list them for safety.
224  //
225  // TODO: Expand this into a covered switch. There is a lot more here.
226  switch (ID) {
227  case Intrinsic::memcpy:
228  case Intrinsic::memmove:
229  case Intrinsic::memset:
230  return true;
231  default:
232  return false;
233  }
234 }
235 
236 /// \brief Determine what kind of construct V is.
238  if (const Instruction *I = dyn_cast<Instruction>(V)) {
239  // Any instruction other than bitcast and gep with a pointer operand have a
240  // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
241  // to a subsequent use, rather than using it themselves, in this sense.
242  // As a short cut, several other opcodes are known to have no pointer
243  // operands of interest. And ret is never followed by a release, so it's
244  // not interesting to examine.
245  switch (I->getOpcode()) {
246  case Instruction::Call: {
247  const CallInst *CI = cast<CallInst>(I);
248  // See if we have a function that we know something about.
249  if (const Function *F = CI->getCalledFunction()) {
250  ARCInstKind Class = GetFunctionClass(F);
251  if (Class != ARCInstKind::CallOrUser)
252  return Class;
253  Intrinsic::ID ID = F->getIntrinsicID();
254  if (isInertIntrinsic(ID))
255  return ARCInstKind::None;
256  if (isUseOnlyIntrinsic(ID))
257  return ARCInstKind::User;
258  }
259 
260  // Otherwise, be conservative.
261  return GetCallSiteClass(CI);
262  }
263  case Instruction::Invoke:
264  // Otherwise, be conservative.
265  return GetCallSiteClass(cast<InvokeInst>(I));
266  case Instruction::BitCast:
267  case Instruction::GetElementPtr:
268  case Instruction::Select:
269  case Instruction::PHI:
270  case Instruction::Ret:
271  case Instruction::Br:
272  case Instruction::Switch:
273  case Instruction::IndirectBr:
274  case Instruction::Alloca:
275  case Instruction::VAArg:
276  case Instruction::Add:
277  case Instruction::FAdd:
278  case Instruction::Sub:
279  case Instruction::FSub:
280  case Instruction::Mul:
281  case Instruction::FMul:
282  case Instruction::SDiv:
283  case Instruction::UDiv:
284  case Instruction::FDiv:
285  case Instruction::SRem:
286  case Instruction::URem:
287  case Instruction::FRem:
288  case Instruction::Shl:
289  case Instruction::LShr:
290  case Instruction::AShr:
291  case Instruction::And:
292  case Instruction::Or:
293  case Instruction::Xor:
294  case Instruction::SExt:
295  case Instruction::ZExt:
296  case Instruction::Trunc:
297  case Instruction::IntToPtr:
298  case Instruction::FCmp:
299  case Instruction::FPTrunc:
300  case Instruction::FPExt:
301  case Instruction::FPToUI:
302  case Instruction::FPToSI:
303  case Instruction::UIToFP:
304  case Instruction::SIToFP:
305  case Instruction::InsertElement:
306  case Instruction::ExtractElement:
307  case Instruction::ShuffleVector:
308  case Instruction::ExtractValue:
309  break;
310  case Instruction::ICmp:
311  // Comparing a pointer with null, or any other constant, isn't an
312  // interesting use, because we don't care what the pointer points to, or
313  // about the values of any other dynamic reference-counted pointers.
314  if (IsPotentialRetainableObjPtr(I->getOperand(1)))
315  return ARCInstKind::User;
316  break;
317  default:
318  // For anything else, check all the operands.
319  // Note that this includes both operands of a Store: while the first
320  // operand isn't actually being dereferenced, it is being stored to
321  // memory where we can no longer track who might read it and dereference
322  // it, so we have to consider it potentially used.
323  for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
324  OI != OE; ++OI)
326  return ARCInstKind::User;
327  }
328  }
329 
330  // Otherwise, it's totally inert for ARC purposes.
331  return ARCInstKind::None;
332 }
333 
334 /// \brief Test if the given class is a kind of user.
336  switch (Class) {
337  case ARCInstKind::User:
340  return true;
341  case ARCInstKind::Retain:
360  case ARCInstKind::Call:
361  case ARCInstKind::None:
363  return false;
364  }
365  llvm_unreachable("covered switch isn't covered?");
366 }
367 
368 /// \brief Test if the given class is objc_retain or equivalent.
370  switch (Class) {
371  case ARCInstKind::Retain:
373  return true;
374  // I believe we treat retain block as not a retain since it can copy its
375  // block.
395  case ARCInstKind::Call:
396  case ARCInstKind::User:
397  case ARCInstKind::None:
399  return false;
400  }
401  llvm_unreachable("covered switch isn't covered?");
402 }
403 
404 /// \brief Test if the given class is objc_autorelease or equivalent.
406  switch (Class) {
409  return true;
410  case ARCInstKind::Retain:
430  case ARCInstKind::Call:
431  case ARCInstKind::User:
432  case ARCInstKind::None:
433  return false;
434  }
435  llvm_unreachable("covered switch isn't covered?");
436 }
437 
438 /// \brief Test if the given class represents instructions which return their
439 /// argument verbatim.
441  switch (Class) {
442  case ARCInstKind::Retain:
448  return true;
465  case ARCInstKind::Call:
466  case ARCInstKind::User:
467  case ARCInstKind::None:
468  return false;
469  }
470  llvm_unreachable("covered switch isn't covered?");
471 }
472 
473 /// \brief Test if the given class represents instructions which do nothing if
474 /// passed a null pointer.
476  switch (Class) {
477  case ARCInstKind::Retain:
484  return true;
499  case ARCInstKind::Call:
500  case ARCInstKind::User:
501  case ARCInstKind::None:
503  return false;
504  }
505  llvm_unreachable("covered switch isn't covered?");
506 }
507 
508 /// \brief Test if the given class represents instructions which are always safe
509 /// to mark with the "tail" keyword.
511  // ARCInstKind::RetainBlock may be given a stack argument.
512  switch (Class) {
513  case ARCInstKind::Retain:
517  return true;
535  case ARCInstKind::Call:
536  case ARCInstKind::User:
537  case ARCInstKind::None:
539  return false;
540  }
541  llvm_unreachable("covered switch isn't covered?");
542 }
543 
544 /// \brief Test if the given class represents instructions which are never safe
545 /// to mark with the "tail" keyword.
547  /// It is never safe to tail call objc_autorelease since by tail calling
548  /// objc_autorelease: fast autoreleasing causing our object to be potentially
549  /// reclaimed from the autorelease pool which violates the semantics of
550  /// __autoreleasing types in ARC.
551  switch (Class) {
553  return true;
554  case ARCInstKind::Retain:
574  case ARCInstKind::Call:
575  case ARCInstKind::User:
576  case ARCInstKind::None:
578  return false;
579  }
580  llvm_unreachable("covered switch isn't covered?");
581 }
582 
583 /// \brief Test if the given class represents instructions which are always safe
584 /// to mark with the nounwind attribute.
586  // objc_retainBlock is not nounwind because it calls user copy constructors
587  // which could theoretically throw.
588  switch (Class) {
589  case ARCInstKind::Retain:
597  return true;
611  case ARCInstKind::Call:
612  case ARCInstKind::User:
613  case ARCInstKind::None:
615  return false;
616  }
617  llvm_unreachable("covered switch isn't covered?");
618 }
619 
620 /// Test whether the given instruction can autorelease any pointer or cause an
621 /// autoreleasepool pop.
622 ///
623 /// This means that it *could* interrupt the RV optimization.
625  switch (Class) {
628  case ARCInstKind::Call:
633  return true;
634  case ARCInstKind::Retain:
649  case ARCInstKind::User:
650  case ARCInstKind::None:
652  return false;
653  }
654  llvm_unreachable("covered switch isn't covered?");
655 }
656 
658  switch (Kind) {
659  case ARCInstKind::Retain:
667  case ARCInstKind::User:
668  case ARCInstKind::None:
669  return false;
670 
671  // The cases below are conservative.
672 
673  // RetainBlock can result in user defined copy constructors being called
674  // implying releases may occur.
688  case ARCInstKind::Call:
690  return true;
691  }
692 
693  llvm_unreachable("covered switch isn't covered?");
694 }
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:612
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
objc_retainAutoreleasedReturnValue
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(const char(&S)[N], const T &Value)
Definition: StringSwitch.h:74
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:603
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.
LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(const T &Value) const
Definition: StringSwitch.h:244
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
objc_storeStrong (derived)
could "use" a pointer
bool IsRetain(ARCInstKind Class)
Test if the given class is objc_retain or equivalent.
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
static bool isUseOnlyIntrinsic(unsigned ID)
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:220
#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:44
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.