LLVM  13.0.0git
ObjCARC.h
Go to the documentation of this file.
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 "ARCRuntimeEntryPoints.h"
30 
31 namespace llvm {
32 namespace objcarc {
33 
34 /// Erase the given instruction.
35 ///
36 /// Many ObjC calls return their argument verbatim,
37 /// so if it's such a call and the return value has users, replace them with the
38 /// argument value.
39 ///
40 static inline void EraseInstruction(Instruction *CI) {
41  Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
42 
43  bool Unused = CI->use_empty();
44 
45  if (!Unused) {
46  // Replace the return value with the argument.
49  IsNullOrUndef(OldArg->stripPointerCasts()))) &&
50  "Can't delete non-forwarding instruction with users!");
51  CI->replaceAllUsesWith(OldArg);
52  }
53 
54  CI->eraseFromParent();
55 
56  if (Unused)
58 }
59 
60 /// If Inst is a ReturnRV and its operand is a call or invoke, return the
61 /// operand. Otherwise return null.
62 static inline const Instruction *getreturnRVOperand(const Instruction &Inst,
63  ARCInstKind Class) {
64  if (Class != ARCInstKind::RetainRV)
65  return nullptr;
66 
67  const auto *Opnd = Inst.getOperand(0)->stripPointerCasts();
68  if (const auto *C = dyn_cast<CallInst>(Opnd))
69  return C;
70  return dyn_cast<InvokeInst>(Opnd);
71 }
72 
73 /// Return the list of PHI nodes that are equivalent to PN.
74 template<class PHINodeTy, class VectorTy>
75 void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList) {
76  auto *BB = PN.getParent();
77  for (auto &P : BB->phis()) {
78  if (&P == &PN) // Do not add PN to the list.
79  continue;
80  unsigned I = 0, E = PN.getNumIncomingValues();
81  for (; I < E; ++I) {
82  auto *BB = PN.getIncomingBlock(I);
83  auto *PNOpnd = PN.getIncomingValue(I)->stripPointerCasts();
84  auto *POpnd = P.getIncomingValueForBlock(BB)->stripPointerCasts();
85  if (PNOpnd != POpnd)
86  break;
87  }
88  if (I == E)
89  PHIList.push_back(&P);
90  }
91 }
92 
93 static inline MDString *getRVInstMarker(Module &M) {
94  const char *MarkerKey = getRVMarkerModuleFlagStr();
95  return dyn_cast_or_null<MDString>(M.getModuleFlag(MarkerKey));
96 }
97 
98 /// Create a call instruction with the correct funclet token. This should be
99 /// called instead of calling CallInst::Create directly unless the call is
100 /// going to be removed from the IR before WinEHPrepare.
102  FunctionCallee Func, ArrayRef<Value *> Args, const Twine &NameStr,
103  Instruction *InsertBefore,
104  const DenseMap<BasicBlock *, ColorVector> &BlockColors);
105 
107 public:
109  : EP(P), ContractPass(ContractPass) {}
111 
112  /// Insert a retainRV/claimRV call to the normal destination blocks of invokes
113  /// with operand bundle "clang.arc.attachedcall". If the edge to the normal
114  /// destination block is a critical edge, split it.
115  std::pair<bool, bool> insertAfterInvokes(Function &F, DominatorTree *DT);
116 
117  /// Insert a retainRV/claimRV call.
118  CallInst *insertRVCall(Instruction *InsertPt, CallBase *AnnotatedCall);
119 
120  /// Insert a retainRV/claimRV call with colors.
122  Instruction *InsertPt, CallBase *AnnotatedCall,
123  const DenseMap<BasicBlock *, ColorVector> &BlockColors);
124 
125  /// See if an instruction is a bundled retainRV/claimRV call.
126  bool contains(const Instruction *I) const {
127  if (auto *CI = dyn_cast<CallInst>(I))
128  return RVCalls.count(CI);
129  return false;
130  }
131 
132  /// Remove a retainRV/claimRV call entirely.
133  void eraseInst(CallInst *CI) {
134  auto It = RVCalls.find(CI);
135  if (It != RVCalls.end()) {
136  // Remove call to @llvm.objc.clang.arc.noop.use.
137  for (auto U = It->second->user_begin(), E = It->second->user_end(); U != E; ++U)
138  if (auto *CI = dyn_cast<CallInst>(*U))
139  if (CI->getIntrinsicID() == Intrinsic::objc_clang_arc_noop_use) {
140  CI->eraseFromParent();
141  break;
142  }
143 
144  auto *NewCall = CallBase::removeOperandBundle(
145  It->second, LLVMContext::OB_clang_arc_attachedcall, It->second);
146  NewCall->copyMetadata(*It->second);
147  It->second->replaceAllUsesWith(NewCall);
148  It->second->eraseFromParent();
149  RVCalls.erase(It);
150  }
151  EraseInstruction(CI);
152  }
153 
154 private:
155  /// A map of inserted retainRV/claimRV calls to annotated calls/invokes.
157 
159  bool ContractPass;
160 };
161 
162 } // end namespace objcarc
163 } // end namespace llvm
164 
165 #endif
llvm::objcarc::BundledRetainClaimRVs
Definition: ObjCARC.h:106
llvm::objcarc::GetBasicARCInstKind
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
Definition: ObjCARCInstKind.h:104
llvm::RecursivelyDeleteTriviallyDeadInstructions
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:496
llvm
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::CallBase::removeOperandBundle
static CallBase * removeOperandBundle(CallBase *CB, uint32_t ID, Instruction *InsertPt=nullptr)
Create a clone of CB with operand bundle ID removed.
Definition: Instructions.cpp:452
llvm::Function
Definition: Function.h:61
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
Local.h
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
EHPersonalities.h
F
#define F(x, y, z)
Definition: MD5.cpp:56
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::objcarc::ARCInstKind::RetainRV
@ RetainRV
objc_retainAutoreleasedReturnValue
llvm::objcarc::getEquivalentPHIs
void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList)
Return the list of PHI nodes that are equivalent to PN.
Definition: ObjCARC.h:75
llvm::Instruction
Definition: Instruction.h:45
llvm::Value::use_empty
bool use_empty() const
Definition: Value.h:345
llvm::objcarc::ARCInstKind
ARCInstKind
Definition: ObjCARCInstKind.h:28
ARCRuntimeEntryPoints.h
llvm::Instruction::eraseFromParent
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:78
llvm::objcarc::BundledRetainClaimRVs::insertRVCallWithColors
CallInst * insertRVCallWithColors(Instruction *InsertPt, CallBase *AnnotatedCall, const DenseMap< BasicBlock *, ColorVector > &BlockColors)
Insert a retainRV/claimRV call with colors.
Definition: ObjCARC.cpp:102
llvm::objcarc::BundledRetainClaimRVs::~BundledRetainClaimRVs
~BundledRetainClaimRVs()
Definition: ObjCARC.cpp:117
llvm::DenseMap
Definition: DenseMap.h:714
llvm::objcarc::BundledRetainClaimRVs::insertAfterInvokes
std::pair< bool, bool > insertAfterInvokes(Function &F, DominatorTree *DT)
Insert a retainRV/claimRV call to the normal destination blocks of invokes with operand bundle "clang...
Definition: ObjCARC.cpp:66
I
#define I(x, y, z)
Definition: MD5.cpp:59
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::objcarc::BundledRetainClaimRVs::insertRVCall
CallInst * insertRVCall(Instruction *InsertPt, CallBase *AnnotatedCall)
Insert a retainRV/claimRV call.
Definition: ObjCARC.cpp:96
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::objcarc::getreturnRVOperand
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:62
llvm::objcarc::getRVMarkerModuleFlagStr
const char * getRVMarkerModuleFlagStr()
Definition: ObjCARCUtil.h:23
llvm::CallBase::getIntrinsicID
Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
Definition: Instructions.cpp:307
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::objcarc::IsNullOrUndef
bool IsNullOrUndef(const Value *V)
Definition: ObjCARCAnalysisUtils.h:132
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:517
llvm::Value::stripPointerCasts
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:662
ObjCARCAnalysisUtils.h
ObjCARCUtil.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
llvm::objcarc::ARCRuntimeEntryPoints
Declarations for ObjC runtime functions and constants.
Definition: ARCRuntimeEntryPoints.h:52
llvm::objcarc::getRVInstMarker
static MDString * getRVInstMarker(Module &M)
Definition: ObjCARC.h:93
llvm::objcarc::BundledRetainClaimRVs::contains
bool contains(const Instruction *I) const
See if an instruction is a bundled retainRV/claimRV call.
Definition: ObjCARC.h:126
llvm::objcarc::EraseInstruction
static void EraseInstruction(Instruction *CI)
Erase the given instruction.
Definition: ObjCARC.h:40
llvm::objcarc::createCallInstWithColors
CallInst * createCallInstWithColors(FunctionCallee Func, ArrayRef< Value * > Args, const Twine &NameStr, Instruction *InsertBefore, const DenseMap< BasicBlock *, ColorVector > &BlockColors)
Create a call instruction with the correct funclet token.
Definition: ObjCARC.cpp:46
llvm::FunctionCallee
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:164
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1164
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1478
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::objcarc::BundledRetainClaimRVs::BundledRetainClaimRVs
BundledRetainClaimRVs(ARCRuntimeEntryPoints &P, bool ContractPass)
Definition: ObjCARC.h:108
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition: User.h:169
llvm::MDString
A single uniqued string.
Definition: Metadata.h:611
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::objcarc::IsForwarding
bool IsForwarding(ARCInstKind Class)
Test if the given class represents instructions which return their argument verbatim.
Definition: ObjCARCInstKind.cpp:416
llvm::objcarc::IsNoopOnNull
bool IsNoopOnNull(ARCInstKind Class)
Test if the given class represents instructions which do nothing if passed a null pointer.
Definition: ObjCARCInstKind.cpp:451
llvm::LLVMContext::OB_clang_arc_attachedcall
@ OB_clang_arc_attachedcall
Definition: LLVMContext.h:96
llvm::objcarc::BundledRetainClaimRVs::eraseInst
void eraseInst(CallInst *CI)
Remove a retainRV/claimRV call entirely.
Definition: ObjCARC.h:133