LLVM  8.0.1
ObjCARC.h
Go to the documentation of this file.
1 //===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===//
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 common definitions/declarations used by the ObjC ARC
11 /// Optimizer. ARC stands for Automatic Reference Counting and is a system for
12 /// managing reference counts for objects in Objective C.
13 ///
14 /// WARNING: This file knows about certain library functions. It recognizes them
15 /// by name, and hardwires knowledge of their semantics.
16 ///
17 /// WARNING: This file knows about how certain Objective-C library functions are
18 /// used. Naive LLVM IR transformations which would otherwise be
19 /// behavior-preserving may break these assumptions.
20 ///
21 //===----------------------------------------------------------------------===//
22 
23 #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
24 #define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
25 
26 #include "llvm/ADT/StringSwitch.h"
30 #include "llvm/Analysis/Passes.h"
33 #include "llvm/IR/CallSite.h"
34 #include "llvm/IR/InstIterator.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/Pass.h"
38 
39 namespace llvm {
40 class raw_ostream;
41 }
42 
43 namespace llvm {
44 namespace objcarc {
45 
46 /// Erase the given instruction.
47 ///
48 /// Many ObjC calls return their argument verbatim,
49 /// so if it's such a call and the return value has users, replace them with the
50 /// argument value.
51 ///
52 static inline void EraseInstruction(Instruction *CI) {
53  Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
54 
55  bool Unused = CI->use_empty();
56 
57  if (!Unused) {
58  // Replace the return value with the argument.
61  IsNullOrUndef(OldArg->stripPointerCasts()))) &&
62  "Can't delete non-forwarding instruction with users!");
63  CI->replaceAllUsesWith(OldArg);
64  }
65 
66  CI->eraseFromParent();
67 
68  if (Unused)
70 }
71 
72 /// If Inst is a ReturnRV and its operand is a call or invoke, return the
73 /// operand. Otherwise return null.
74 static inline const Instruction *getreturnRVOperand(const Instruction &Inst,
75  ARCInstKind Class) {
76  if (Class != ARCInstKind::RetainRV)
77  return nullptr;
78 
79  const auto *Opnd = Inst.getOperand(0)->stripPointerCasts();
80  if (const auto *C = dyn_cast<CallInst>(Opnd))
81  return C;
82  return dyn_cast<InvokeInst>(Opnd);
83 }
84 
85 /// Return the list of PHI nodes that are equivalent to PN.
86 template<class PHINodeTy, class VectorTy>
87 void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList) {
88  auto *BB = PN.getParent();
89  for (auto &P : BB->phis()) {
90  if (&P == &PN) // Do not add PN to the list.
91  continue;
92  unsigned I = 0, E = PN.getNumIncomingValues();
93  for (; I < E; ++I) {
94  auto *BB = PN.getIncomingBlock(I);
95  auto *PNOpnd = PN.getIncomingValue(I)->stripPointerCasts();
96  auto *POpnd = P.getIncomingValueForBlock(BB)->stripPointerCasts();
97  if (PNOpnd != POpnd)
98  break;
99  }
100  if (I == E)
101  PHIList.push_back(&P);
102  }
103 }
104 
105 } // end namespace objcarc
106 } // end namespace llvm
107 
108 #endif
uint64_t CallInst * C
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:68
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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.
bool IsNullOrUndef(const Value *V)
objc_retainAutoreleasedReturnValue
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
Value * getOperand(unsigned i) const
Definition: User.h:170
#define P(N)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:430
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, and aliases.
Definition: Value.cpp:529
This file defines common analysis utilities used by the ObjC ARC Optimizer.
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
static void EraseInstruction(Instruction *CI)
Erase the given instruction.
Definition: ObjCARC.h:52
static const Instruction * getreturnRVOperand(const Instruction &Inst, ARCInstKind Class)
If Inst is a ReturnRV and its operand is a call or invoke, return the operand.
Definition: ObjCARC.h:74
ARCInstKind
Equivalence classes of instructions in the ARC Model.
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition: MD5.cpp:58
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
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
Invoke instruction.
bool use_empty() const
Definition: Value.h:323
void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList)
Return the list of PHI nodes that are equivalent to PN.
Definition: ObjCARC.h:87