1 //===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file defines common definitions/declarations used by the ObjC ARC 10 /// Optimizer. ARC stands for Automatic Reference Counting and is a system for 11 /// managing reference counts for objects in Objective C. 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 #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H 23 #define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H 24 25 #include "llvm/ADT/StringSwitch.h" 26 #include "llvm/Analysis/AliasAnalysis.h" 27 #include "llvm/Analysis/ObjCARCAnalysisUtils.h" 28 #include "llvm/Analysis/ObjCARCInstKind.h" 29 #include "llvm/Analysis/Passes.h" 30 #include "llvm/Transforms/Utils/Local.h" 31 #include "llvm/Analysis/ValueTracking.h" 32 #include "llvm/IR/InstIterator.h" 33 #include "llvm/IR/Module.h" 34 #include "llvm/Pass.h" 35 #include "llvm/Transforms/ObjCARC.h" 36 37 namespace llvm { 38 namespace objcarc { 39 40 /// Erase the given instruction. 41 /// 42 /// Many ObjC calls return their argument verbatim, 43 /// so if it's such a call and the return value has users, replace them with the 44 /// argument value. 45 /// 46 static inline void EraseInstruction(Instruction *CI) { 47 Value *OldArg = cast<CallInst>(CI)->getArgOperand(0); 48 49 bool Unused = CI->use_empty(); 50 51 if (!Unused) { 52 // Replace the return value with the argument. 53 assert((IsForwarding(GetBasicARCInstKind(CI)) || 54 (IsNoopOnNull(GetBasicARCInstKind(CI)) && 55 IsNullOrUndef(OldArg->stripPointerCasts()))) && 56 "Can't delete non-forwarding instruction with users!"); 57 CI->replaceAllUsesWith(OldArg); 58 } 59 60 CI->eraseFromParent(); 61 62 if (Unused) 63 RecursivelyDeleteTriviallyDeadInstructions(OldArg); 64 } 65 66 /// If Inst is a ReturnRV and its operand is a call or invoke, return the 67 /// operand. Otherwise return null. 68 static inline const Instruction *getreturnRVOperand(const Instruction &Inst, 69 ARCInstKind Class) { 70 if (Class != ARCInstKind::RetainRV) 71 return nullptr; 72 73 const auto *Opnd = Inst.getOperand(0)->stripPointerCasts(); 74 if (const auto *C = dyn_cast<CallInst>(Opnd)) 75 return C; 76 return dyn_cast<InvokeInst>(Opnd); 77 } 78 79 /// Return the list of PHI nodes that are equivalent to PN. 80 template<class PHINodeTy, class VectorTy> 81 void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList) { 82 auto *BB = PN.getParent(); 83 for (auto &P : BB->phis()) { 84 if (&P == &PN) // Do not add PN to the list. 85 continue; 86 unsigned I = 0, E = PN.getNumIncomingValues(); 87 for (; I < E; ++I) { 88 auto *BB = PN.getIncomingBlock(I); 89 auto *PNOpnd = PN.getIncomingValue(I)->stripPointerCasts(); 90 auto *POpnd = P.getIncomingValueForBlock(BB)->stripPointerCasts(); 91 if (PNOpnd != POpnd) 92 break; 93 } 94 if (I == E) 95 PHIList.push_back(&P); 96 } 97 } 98 99 } // end namespace objcarc 100 } // end namespace llvm 101 102 #endif 103