LLVM  8.0.1
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 
90  switch (ID) {
91  default:
102  return ARCInstKind::CopyWeak;
106  return ARCInstKind::InitWeak;
108  return ARCInstKind::LoadWeak;
112  return ARCInstKind::MoveWeak;
114  return ARCInstKind::Release;
116  return ARCInstKind::Retain;
122  return ARCInstKind::RetainRV;
128  return ARCInstKind::StoreWeak;
132  return ARCInstKind::ClaimRV;
134  return ARCInstKind::NoopCast;
136  return ARCInstKind::NoopCast;
138  return ARCInstKind::NoopCast;
142  return ARCInstKind::User;
144  return ARCInstKind::User;
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;
154  }
155 }
156 
157 // A whitelist of intrinsics that we know do not use objc pointers or decrement
158 // ref counts.
159 static bool isInertIntrinsic(unsigned ID) {
160  // TODO: Make this into a covered switch.
161  switch (ID) {
167  case Intrinsic::vastart:
168  case Intrinsic::vacopy:
169  case Intrinsic::vaend:
171  case Intrinsic::prefetch:
185  // Don't let dbg info affect our results.
189  // 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.
198 static 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 
213 /// Determine what kind of construct V is.
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);
225  // See if we have a function that we know something about.
226  if (const Function *F = CI->getCalledFunction()) {
227  ARCInstKind Class = GetFunctionClass(F);
228  if (Class != ARCInstKind::CallOrUser)
229  return Class;
230  Intrinsic::ID ID = F->getIntrinsicID();
231  if (isInertIntrinsic(ID))
232  return ARCInstKind::None;
233  if (isUseOnlyIntrinsic(ID))
234  return ARCInstKind::User;
235  }
236 
237  // Otherwise, be conservative.
238  return GetCallSiteClass(CI);
239  }
240  case Instruction::Invoke:
241  // Otherwise, be conservative.
242  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)
303  return ARCInstKind::User;
304  }
305  }
306 
307  // Otherwise, it's totally inert for ARC purposes.
308  return ARCInstKind::None;
309 }
310 
311 /// Test if the given class is a kind of user.
313  switch (Class) {
314  case ARCInstKind::User:
317  return true;
318  case ARCInstKind::Retain:
337  case ARCInstKind::Call:
338  case ARCInstKind::None:
340  return false;
341  }
342  llvm_unreachable("covered switch isn't covered?");
343 }
344 
345 /// Test if the given class is objc_retain or equivalent.
347  switch (Class) {
348  case ARCInstKind::Retain:
350  return true;
351  // I believe we treat retain block as not a retain since it can copy its
352  // block.
372  case ARCInstKind::Call:
373  case ARCInstKind::User:
374  case ARCInstKind::None:
376  return false;
377  }
378  llvm_unreachable("covered switch isn't covered?");
379 }
380 
381 /// Test if the given class is objc_autorelease or equivalent.
383  switch (Class) {
386  return true;
387  case ARCInstKind::Retain:
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 
415 /// Test if the given class represents instructions which return their
416 /// argument verbatim.
418  switch (Class) {
419  case ARCInstKind::Retain:
425  return true;
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 
450 /// Test if the given class represents instructions which do nothing if
451 /// passed a null pointer.
453  switch (Class) {
454  case ARCInstKind::Retain:
461  return true;
476  case ARCInstKind::Call:
477  case ARCInstKind::User:
478  case ARCInstKind::None:
480  return false;
481  }
482  llvm_unreachable("covered switch isn't covered?");
483 }
484 
485 /// Test if the given class represents instructions which are always safe
486 /// to mark with the "tail" keyword.
488  // ARCInstKind::RetainBlock may be given a stack argument.
489  switch (Class) {
490  case ARCInstKind::Retain:
494  return true;
512  case ARCInstKind::Call:
513  case ARCInstKind::User:
514  case ARCInstKind::None:
516  return false;
517  }
518  llvm_unreachable("covered switch isn't covered?");
519 }
520 
521 /// Test if the given class represents instructions which are never safe
522 /// to mark with the "tail" keyword.
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) {
530  return true;
531  case ARCInstKind::Retain:
551  case ARCInstKind::Call:
552  case ARCInstKind::User:
553  case ARCInstKind::None:
555  return false;
556  }
557  llvm_unreachable("covered switch isn't covered?");
558 }
559 
560 /// Test if the given class represents instructions which are always safe
561 /// to mark with the nounwind attribute.
563  // objc_retainBlock is not nounwind because it calls user copy constructors
564  // which could theoretically throw.
565  switch (Class) {
566  case ARCInstKind::Retain:
574  return true;
588  case ARCInstKind::Call:
589  case ARCInstKind::User:
590  case ARCInstKind::None:
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.
602  switch (Class) {
605  case ARCInstKind::Call:
610  return true;
611  case ARCInstKind::Retain:
626  case ARCInstKind::User:
627  case ARCInstKind::None:
629  return false;
630  }
631  llvm_unreachable("covered switch isn't covered?");
632 }
633 
635  switch (Kind) {
636  case ARCInstKind::Retain:
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.
665  case ARCInstKind::Call:
667  return true;
668  }
669 
670  llvm_unreachable("covered switch isn't covered?");
671 }
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 lattice values for constants.
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.
F(f)
could call objc_release
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
objc_retainAutoreleasedReturnValue
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...
raw_ostream & operator<<(raw_ostream &OS, const ARCInstKind Class)
objc_initWeak (derived)
ARCInstKind GetFunctionClass(const Function *F)
Determine if F is one of the special known Functions.
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
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:194
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)
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1181
#define I(x, y, z)
Definition: MD5.cpp:58
objc_storeWeak (primitive)
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...
bool IsAutorelease(ARCInstKind Class)
Test if the given class is objc_autorelease or equivalent.