Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/ObjCARC/ObjCARC.h
35267 views
//===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7/// \file8/// This file defines common definitions/declarations used by the ObjC ARC9/// Optimizer. ARC stands for Automatic Reference Counting and is a system for10/// managing reference counts for objects in Objective C.11///12/// WARNING: This file knows about certain library functions. It recognizes them13/// by name, and hardwires knowledge of their semantics.14///15/// WARNING: This file knows about how certain Objective-C library functions are16/// used. Naive LLVM IR transformations which would otherwise be17/// behavior-preserving may break these assumptions.18///19//===----------------------------------------------------------------------===//2021#ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H22#define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H2324#include "llvm/Analysis/ObjCARCAnalysisUtils.h"25#include "llvm/Analysis/ObjCARCUtil.h"26#include "llvm/IR/EHPersonalities.h"27#include "llvm/Transforms/Utils/Local.h"2829namespace llvm {30namespace objcarc {3132/// Erase the given instruction.33///34/// Many ObjC calls return their argument verbatim,35/// so if it's such a call and the return value has users, replace them with the36/// argument value.37///38static inline void EraseInstruction(Instruction *CI) {39Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);4041bool Unused = CI->use_empty();4243if (!Unused) {44// Replace the return value with the argument.45assert((IsForwarding(GetBasicARCInstKind(CI)) ||46(IsNoopOnNull(GetBasicARCInstKind(CI)) &&47IsNullOrUndef(OldArg->stripPointerCasts()))) &&48"Can't delete non-forwarding instruction with users!");49CI->replaceAllUsesWith(OldArg);50}5152CI->eraseFromParent();5354if (Unused)55RecursivelyDeleteTriviallyDeadInstructions(OldArg);56}5758/// If Inst is a ReturnRV and its operand is a call or invoke, return the59/// operand. Otherwise return null.60static inline const Instruction *getreturnRVOperand(const Instruction &Inst,61ARCInstKind Class) {62if (Class != ARCInstKind::RetainRV)63return nullptr;6465const auto *Opnd = Inst.getOperand(0)->stripPointerCasts();66if (const auto *C = dyn_cast<CallInst>(Opnd))67return C;68return dyn_cast<InvokeInst>(Opnd);69}7071/// Return the list of PHI nodes that are equivalent to PN.72template<class PHINodeTy, class VectorTy>73void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList) {74auto *BB = PN.getParent();75for (auto &P : BB->phis()) {76if (&P == &PN) // Do not add PN to the list.77continue;78unsigned I = 0, E = PN.getNumIncomingValues();79for (; I < E; ++I) {80auto *BB = PN.getIncomingBlock(I);81auto *PNOpnd = PN.getIncomingValue(I)->stripPointerCasts();82auto *POpnd = P.getIncomingValueForBlock(BB)->stripPointerCasts();83if (PNOpnd != POpnd)84break;85}86if (I == E)87PHIList.push_back(&P);88}89}9091static inline MDString *getRVInstMarker(Module &M) {92const char *MarkerKey = getRVMarkerModuleFlagStr();93return dyn_cast_or_null<MDString>(M.getModuleFlag(MarkerKey));94}9596/// Create a call instruction with the correct funclet token. This should be97/// called instead of calling CallInst::Create directly unless the call is98/// going to be removed from the IR before WinEHPrepare.99CallInst *createCallInstWithColors(100FunctionCallee Func, ArrayRef<Value *> Args, const Twine &NameStr,101BasicBlock::iterator InsertBefore,102const DenseMap<BasicBlock *, ColorVector> &BlockColors);103104class BundledRetainClaimRVs {105public:106BundledRetainClaimRVs(bool ContractPass) : ContractPass(ContractPass) {}107~BundledRetainClaimRVs();108109/// Insert a retainRV/claimRV call to the normal destination blocks of invokes110/// with operand bundle "clang.arc.attachedcall". If the edge to the normal111/// destination block is a critical edge, split it.112std::pair<bool, bool> insertAfterInvokes(Function &F, DominatorTree *DT);113114/// Insert a retainRV/claimRV call.115CallInst *insertRVCall(BasicBlock::iterator InsertPt,116CallBase *AnnotatedCall);117118/// Insert a retainRV/claimRV call with colors.119CallInst *insertRVCallWithColors(120BasicBlock::iterator InsertPt, CallBase *AnnotatedCall,121const DenseMap<BasicBlock *, ColorVector> &BlockColors);122123/// See if an instruction is a bundled retainRV/claimRV call.124bool contains(const Instruction *I) const {125if (auto *CI = dyn_cast<CallInst>(I))126return RVCalls.count(CI);127return false;128}129130/// Remove a retainRV/claimRV call entirely.131void eraseInst(CallInst *CI) {132auto It = RVCalls.find(CI);133if (It != RVCalls.end()) {134// Remove call to @llvm.objc.clang.arc.noop.use.135for (User *U : It->second->users())136if (auto *CI = dyn_cast<CallInst>(U))137if (CI->getIntrinsicID() == Intrinsic::objc_clang_arc_noop_use) {138CI->eraseFromParent();139break;140}141142auto *NewCall = CallBase::removeOperandBundle(143It->second, LLVMContext::OB_clang_arc_attachedcall,144It->second->getIterator());145NewCall->copyMetadata(*It->second);146It->second->replaceAllUsesWith(NewCall);147It->second->eraseFromParent();148RVCalls.erase(It);149}150EraseInstruction(CI);151}152153private:154/// A map of inserted retainRV/claimRV calls to annotated calls/invokes.155DenseMap<CallInst *, CallBase *> RVCalls;156157bool ContractPass;158};159160} // end namespace objcarc161} // end namespace llvm162163#endif164165166