LLVM  3.7.0
ARCInstKind.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 
22 #include "ObjCARC.h"
23 #include "llvm/IR/Intrinsics.h"
24 
25 using namespace llvm;
26 using namespace llvm::objcarc;
27 
29  const ARCInstKind Class) {
30  switch (Class) {
32  return OS << "ARCInstKind::Retain";
34  return OS << "ARCInstKind::RetainRV";
36  return OS << "ARCInstKind::RetainBlock";
38  return OS << "ARCInstKind::Release";
40  return OS << "ARCInstKind::Autorelease";
42  return OS << "ARCInstKind::AutoreleaseRV";
44  return OS << "ARCInstKind::AutoreleasepoolPush";
46  return OS << "ARCInstKind::AutoreleasepoolPop";
48  return OS << "ARCInstKind::NoopCast";
50  return OS << "ARCInstKind::FusedRetainAutorelease";
52  return OS << "ARCInstKind::FusedRetainAutoreleaseRV";
54  return OS << "ARCInstKind::LoadWeakRetained";
56  return OS << "ARCInstKind::StoreWeak";
58  return OS << "ARCInstKind::InitWeak";
60  return OS << "ARCInstKind::LoadWeak";
62  return OS << "ARCInstKind::MoveWeak";
64  return OS << "ARCInstKind::CopyWeak";
66  return OS << "ARCInstKind::DestroyWeak";
68  return OS << "ARCInstKind::StoreStrong";
70  return OS << "ARCInstKind::CallOrUser";
71  case ARCInstKind::Call:
72  return OS << "ARCInstKind::Call";
73  case ARCInstKind::User:
74  return OS << "ARCInstKind::User";
76  return OS << "ARCInstKind::IntrinsicUser";
77  case ARCInstKind::None:
78  return OS << "ARCInstKind::None";
79  }
80  llvm_unreachable("Unknown instruction class!");
81 }
82 
84  Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
85 
86  // No (mandatory) arguments.
87  if (AI == AE)
89  .Case("objc_autoreleasePoolPush", ARCInstKind::AutoreleasepoolPush)
90  .Case("clang.arc.use", ARCInstKind::IntrinsicUser)
92 
93  // One argument.
94  const Argument *A0 = AI++;
95  if (AI == AE)
96  // Argument is a pointer.
97  if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) {
98  Type *ETy = PTy->getElementType();
99  // Argument is i8*.
100  if (ETy->isIntegerTy(8))
102  .Case("objc_retain", ARCInstKind::Retain)
103  .Case("objc_retainAutoreleasedReturnValue", ARCInstKind::RetainRV)
104  .Case("objc_retainBlock", ARCInstKind::RetainBlock)
105  .Case("objc_release", ARCInstKind::Release)
106  .Case("objc_autorelease", ARCInstKind::Autorelease)
107  .Case("objc_autoreleaseReturnValue", ARCInstKind::AutoreleaseRV)
108  .Case("objc_autoreleasePoolPop", ARCInstKind::AutoreleasepoolPop)
109  .Case("objc_retainedObject", ARCInstKind::NoopCast)
110  .Case("objc_unretainedObject", ARCInstKind::NoopCast)
111  .Case("objc_unretainedPointer", ARCInstKind::NoopCast)
112  .Case("objc_retain_autorelease",
114  .Case("objc_retainAutorelease", ARCInstKind::FusedRetainAutorelease)
115  .Case("objc_retainAutoreleaseReturnValue",
117  .Case("objc_sync_enter", ARCInstKind::User)
118  .Case("objc_sync_exit", ARCInstKind::User)
120 
121  // Argument is i8**
122  if (PointerType *Pte = dyn_cast<PointerType>(ETy))
123  if (Pte->getElementType()->isIntegerTy(8))
125  .Case("objc_loadWeakRetained", ARCInstKind::LoadWeakRetained)
126  .Case("objc_loadWeak", ARCInstKind::LoadWeak)
127  .Case("objc_destroyWeak", ARCInstKind::DestroyWeak)
129  }
130 
131  // Two arguments, first is i8**.
132  const Argument *A1 = AI++;
133  if (AI == AE)
134  if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
135  if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
136  if (Pte->getElementType()->isIntegerTy(8))
137  if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
138  Type *ETy1 = PTy1->getElementType();
139  // Second argument is i8*
140  if (ETy1->isIntegerTy(8))
142  .Case("objc_storeWeak", ARCInstKind::StoreWeak)
143  .Case("objc_initWeak", ARCInstKind::InitWeak)
144  .Case("objc_storeStrong", ARCInstKind::StoreStrong)
146  // Second argument is i8**.
147  if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
148  if (Pte1->getElementType()->isIntegerTy(8))
150  .Case("objc_moveWeak", ARCInstKind::MoveWeak)
151  .Case("objc_copyWeak", ARCInstKind::CopyWeak)
152  // Ignore annotation calls. This is important to stop the
153  // optimizer from treating annotations as uses which would
154  // make the state of the pointers they are attempting to
155  // elucidate to be incorrect.
156  .Case("llvm.arc.annotation.topdown.bbstart",
158  .Case("llvm.arc.annotation.topdown.bbend",
160  .Case("llvm.arc.annotation.bottomup.bbstart",
162  .Case("llvm.arc.annotation.bottomup.bbend",
165  }
166 
167  // Anything else.
169 }
170 
171 // A whitelist of intrinsics that we know do not use objc pointers or decrement
172 // ref counts.
173 static bool isInertIntrinsic(unsigned ID) {
174  // TODO: Make this into a covered switch.
175  switch (ID) {
176  case Intrinsic::returnaddress:
177  case Intrinsic::frameaddress:
178  case Intrinsic::stacksave:
179  case Intrinsic::stackrestore:
180  case Intrinsic::vastart:
181  case Intrinsic::vacopy:
182  case Intrinsic::vaend:
183  case Intrinsic::objectsize:
184  case Intrinsic::prefetch:
185  case Intrinsic::stackprotector:
186  case Intrinsic::eh_return_i32:
187  case Intrinsic::eh_return_i64:
188  case Intrinsic::eh_typeid_for:
189  case Intrinsic::eh_dwarf_cfa:
190  case Intrinsic::eh_sjlj_lsda:
191  case Intrinsic::eh_sjlj_functioncontext:
192  case Intrinsic::init_trampoline:
193  case Intrinsic::adjust_trampoline:
194  case Intrinsic::lifetime_start:
195  case Intrinsic::lifetime_end:
196  case Intrinsic::invariant_start:
197  case Intrinsic::invariant_end:
198  // Don't let dbg info affect our results.
199  case Intrinsic::dbg_declare:
200  case Intrinsic::dbg_value:
201  // Short cut: Some intrinsics obviously don't use ObjC pointers.
202  return true;
203  default:
204  return false;
205  }
206 }
207 
208 // A whitelist of intrinsics that we know do not use objc pointers or decrement
209 // ref counts.
210 static bool isUseOnlyIntrinsic(unsigned ID) {
211  // We are conservative and even though intrinsics are unlikely to touch
212  // reference counts, we white list them for safety.
213  //
214  // TODO: Expand this into a covered switch. There is a lot more here.
215  switch (ID) {
216  case Intrinsic::memcpy:
217  case Intrinsic::memmove:
218  case Intrinsic::memset:
219  return true;
220  default:
221  return false;
222  }
223 }
224 
225 /// \brief Determine what kind of construct V is.
227  if (const Instruction *I = dyn_cast<Instruction>(V)) {
228  // Any instruction other than bitcast and gep with a pointer operand have a
229  // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
230  // to a subsequent use, rather than using it themselves, in this sense.
231  // As a short cut, several other opcodes are known to have no pointer
232  // operands of interest. And ret is never followed by a release, so it's
233  // not interesting to examine.
234  switch (I->getOpcode()) {
235  case Instruction::Call: {
236  const CallInst *CI = cast<CallInst>(I);
237  // See if we have a function that we know something about.
238  if (const Function *F = CI->getCalledFunction()) {
239  ARCInstKind Class = GetFunctionClass(F);
240  if (Class != ARCInstKind::CallOrUser)
241  return Class;
242  Intrinsic::ID ID = F->getIntrinsicID();
243  if (isInertIntrinsic(ID))
244  return ARCInstKind::None;
245  if (isUseOnlyIntrinsic(ID))
246  return ARCInstKind::User;
247  }
248 
249  // Otherwise, be conservative.
250  return GetCallSiteClass(CI);
251  }
252  case Instruction::Invoke:
253  // Otherwise, be conservative.
254  return GetCallSiteClass(cast<InvokeInst>(I));
255  case Instruction::BitCast:
256  case Instruction::GetElementPtr:
257  case Instruction::Select:
258  case Instruction::PHI:
259  case Instruction::Ret:
260  case Instruction::Br:
261  case Instruction::Switch:
262  case Instruction::IndirectBr:
263  case Instruction::Alloca:
264  case Instruction::VAArg:
265  case Instruction::Add:
266  case Instruction::FAdd:
267  case Instruction::Sub:
268  case Instruction::FSub:
269  case Instruction::Mul:
270  case Instruction::FMul:
271  case Instruction::SDiv:
272  case Instruction::UDiv:
273  case Instruction::FDiv:
274  case Instruction::SRem:
275  case Instruction::URem:
276  case Instruction::FRem:
277  case Instruction::Shl:
278  case Instruction::LShr:
279  case Instruction::AShr:
280  case Instruction::And:
281  case Instruction::Or:
282  case Instruction::Xor:
283  case Instruction::SExt:
284  case Instruction::ZExt:
285  case Instruction::Trunc:
286  case Instruction::IntToPtr:
287  case Instruction::FCmp:
288  case Instruction::FPTrunc:
289  case Instruction::FPExt:
290  case Instruction::FPToUI:
291  case Instruction::FPToSI:
292  case Instruction::UIToFP:
293  case Instruction::SIToFP:
294  case Instruction::InsertElement:
296  case Instruction::ShuffleVector:
297  case Instruction::ExtractValue:
298  break;
299  case Instruction::ICmp:
300  // Comparing a pointer with null, or any other constant, isn't an
301  // interesting use, because we don't care what the pointer points to, or
302  // about the values of any other dynamic reference-counted pointers.
303  if (IsPotentialRetainableObjPtr(I->getOperand(1)))
304  return ARCInstKind::User;
305  break;
306  default:
307  // For anything else, check all the operands.
308  // Note that this includes both operands of a Store: while the first
309  // operand isn't actually being dereferenced, it is being stored to
310  // memory where we can no longer track who might read it and dereference
311  // it, so we have to consider it potentially used.
312  for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
313  OI != OE; ++OI)
315  return ARCInstKind::User;
316  }
317  }
318 
319  // Otherwise, it's totally inert for ARC purposes.
320  return ARCInstKind::None;
321 }
322 
323 /// \brief Test if the given class is a kind of user.
325  switch (Class) {
326  case ARCInstKind::User:
329  return true;
330  case ARCInstKind::Retain:
349  case ARCInstKind::Call:
350  case ARCInstKind::None:
351  return false;
352  }
353  llvm_unreachable("covered switch isn't covered?");
354 }
355 
356 /// \brief Test if the given class is objc_retain or equivalent.
358  switch (Class) {
359  case ARCInstKind::Retain:
361  return true;
362  // I believe we treat retain block as not a retain since it can copy its
363  // block.
383  case ARCInstKind::Call:
384  case ARCInstKind::User:
385  case ARCInstKind::None:
386  return false;
387  }
388  llvm_unreachable("covered switch isn't covered?");
389 }
390 
391 /// \brief Test if the given class is objc_autorelease or equivalent.
393  switch (Class) {
396  return true;
397  case ARCInstKind::Retain:
416  case ARCInstKind::Call:
417  case ARCInstKind::User:
418  case ARCInstKind::None:
419  return false;
420  }
421  llvm_unreachable("covered switch isn't covered?");
422 }
423 
424 /// \brief Test if the given class represents instructions which return their
425 /// argument verbatim.
427  switch (Class) {
428  case ARCInstKind::Retain:
433  return true;
450  case ARCInstKind::Call:
451  case ARCInstKind::User:
452  case ARCInstKind::None:
453  return false;
454  }
455  llvm_unreachable("covered switch isn't covered?");
456 }
457 
458 /// \brief Test if the given class represents instructions which do nothing if
459 /// passed a null pointer.
461  switch (Class) {
462  case ARCInstKind::Retain:
468  return true;
483  case ARCInstKind::Call:
484  case ARCInstKind::User:
485  case ARCInstKind::None:
487  return false;
488  }
489  llvm_unreachable("covered switch isn't covered?");
490 }
491 
492 /// \brief Test if the given class represents instructions which are always safe
493 /// to mark with the "tail" keyword.
495  // ARCInstKind::RetainBlock may be given a stack argument.
496  switch (Class) {
497  case ARCInstKind::Retain:
500  return true;
518  case ARCInstKind::Call:
519  case ARCInstKind::User:
520  case ARCInstKind::None:
522  return false;
523  }
524  llvm_unreachable("covered switch isn't covered?");
525 }
526 
527 /// \brief Test if the given class represents instructions which are never safe
528 /// to mark with the "tail" keyword.
530  /// It is never safe to tail call objc_autorelease since by tail calling
531  /// objc_autorelease: fast autoreleasing causing our object to be potentially
532  /// reclaimed from the autorelease pool which violates the semantics of
533  /// __autoreleasing types in ARC.
534  switch (Class) {
536  return true;
537  case ARCInstKind::Retain:
556  case ARCInstKind::Call:
557  case ARCInstKind::User:
558  case ARCInstKind::None:
560  return false;
561  }
562  llvm_unreachable("covered switch isn't covered?");
563 }
564 
565 /// \brief Test if the given class represents instructions which are always safe
566 /// to mark with the nounwind attribute.
568  // objc_retainBlock is not nounwind because it calls user copy constructors
569  // which could theoretically throw.
570  switch (Class) {
571  case ARCInstKind::Retain:
578  return true;
592  case ARCInstKind::Call:
593  case ARCInstKind::User:
594  case ARCInstKind::None:
596  return false;
597  }
598  llvm_unreachable("covered switch isn't covered?");
599 }
600 
601 /// Test whether the given instruction can autorelease any pointer or cause an
602 /// autoreleasepool pop.
603 ///
604 /// This means that it *could* interrupt the RV optimization.
606  switch (Class) {
609  case ARCInstKind::Call:
614  return true;
615  case ARCInstKind::Retain:
629  case ARCInstKind::User:
630  case ARCInstKind::None:
632  return false;
633  }
634  llvm_unreachable("covered switch isn't covered?");
635 }
636 
638  switch (Kind) {
639  case ARCInstKind::Retain:
647  case ARCInstKind::User:
648  case ARCInstKind::None:
649  return false;
650 
651  // The cases below are conservative.
652 
653  // RetainBlock can result in user defined copy constructors being called
654  // implying releases may occur.
668  case ARCInstKind::Call:
669  return true;
670  }
671 
672  llvm_unreachable("covered switch isn't covered?");
673 }
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.
LLVM Argument representation.
Definition: Argument.h:35
objc_loadWeakRetained (primitive)
could call objc_release and/or "use" pointers
objc_loadWeak (derived)
CallInst - This class represents a function call, abstracting a target machine's calling convention...
objc_retainedObject, etc.
arg_iterator arg_end()
Definition: Function.h:480
F(f)
could call objc_release
StringSwitch & Case(const char(&S)[N], const T &Value)
Definition: StringSwitch.h:55
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.
static ARCInstKind GetCallSiteClass(ImmutableCallSite CS)
Helper for GetARCInstKind.
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
static bool isUseOnlyIntrinsic(unsigned ID)
objc_autoreleaseReturnValue
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
static ConstantInt * ExtractElement(Constant *V, Constant *Idx)
This file defines common definitions/declarations used by the ObjC ARC Optimizer. ...
objc_retainAutoreleasedReturnValue
bool IsAlwaysTail(ARCInstKind Class)
Test if the given class represents instructions which are always safe to mark with the "tail" keyword...
PointerType - Class to represent pointers.
Definition: DerivedTypes.h:449
raw_ostream & operator<<(raw_ostream &OS, const ARCInstKind Class)
Definition: ARCInstKind.cpp:28
static bool isInertIntrinsic(unsigned ID)
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
objc_initWeak (derived)
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1895
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1900
ARCInstKind GetFunctionClass(const Function *F)
Determine if F is one of the special known Functions.
Definition: ARCInstKind.cpp:83
arg_iterator arg_begin()
Definition: Function.h:472
objc_copyWeak (derived)
anything that is inert from an ARC perspective.
bool CanDecrementRefCount(ARCInstKind Kind)
Returns false if conservatively we can prove that any instruction mapped to this kind can not decreme...
ARCInstKind
Equivalence classes of instructions in the ARC Model.
Definition: ARCInstKind.h:30
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
bool IsNoThrow(ARCInstKind Class)
Test if the given class represents instructions which are always safe to mark with the nounwind attri...
Function * getCalledFunction() const
getCalledFunction - Return the function called, or null if this is an indirect function invocation...
ppc loop data prefetch
R Default(const T &Value) const
Definition: StringSwitch.h:111
static bool IsPotentialRetainableObjPtr(const Value *Op)
Test whether the given value is possible a retainable object pointer.
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
Definition: Type.h:193
objc_storeStrong (derived)
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1890
could "use" a pointer
bool IsRetain(ARCInstKind Class)
Test if the given class is objc_retain or equivalent.
#define I(x, y, z)
Definition: MD5.cpp:54
objc_storeWeak (primitive)
const ARM::ArchExtKind Kind
LLVM Value Representation.
Definition: Value.h:69
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
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...
bool IsAutorelease(ARCInstKind Class)
Test if the given class is objc_autorelease or equivalent.