1 //===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 /// WARNING: This file knows about certain library functions. It recognizes them
15 /// by name, and hardwires knowledge of their semantics.
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.
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
24 #define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
29 #include "llvm/Analysis/ObjCARCInstKind.h"
30 #include "llvm/Analysis/Passes.h"
31 #include "llvm/Transforms/Utils/Local.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/IR/CallSite.h"
34 #include "llvm/IR/InstIterator.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/Pass.h"
37 #include "llvm/Transforms/ObjCARC.h"
46 /// Erase the given instruction.
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
52 static inline void EraseInstruction(Instruction *CI) {
53 Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
55 bool Unused = CI->use_empty();
58 // Replace the return value with the argument.
59 assert((IsForwarding(GetBasicARCInstKind(CI)) ||
60 (IsNoopOnNull(GetBasicARCInstKind(CI)) &&
61 IsNullOrUndef(OldArg->stripPointerCasts()))) &&
62 "Can't delete non-forwarding instruction with users!");
63 CI->replaceAllUsesWith(OldArg);
66 CI->eraseFromParent();
69 RecursivelyDeleteTriviallyDeadInstructions(OldArg);
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,
76 if (Class != ARCInstKind::RetainRV)
79 const auto *Opnd = Inst.getOperand(0)->stripPointerCasts();
80 if (const auto *C = dyn_cast<CallInst>(Opnd))
82 return dyn_cast<InvokeInst>(Opnd);
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.
92 unsigned I = 0, E = PN.getNumIncomingValues();
94 auto *BB = PN.getIncomingBlock(I);
95 auto *PNOpnd = PN.getIncomingValue(I)->stripPointerCasts();
96 auto *POpnd = P.getIncomingValueForBlock(BB)->stripPointerCasts();
101 PHIList.push_back(&P);
105 } // end namespace objcarc
106 } // end namespace llvm