LLVM  15.0.0git
ObjCARC.cpp
Go to the documentation of this file.
1 //===-- ObjCARC.cpp -------------------------------------------------------===//
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 //
9 // This file implements common infrastructure for libLLVMObjCARCOpts.a, which
10 // implements several scalar transformations over the LLVM intermediate
11 // representation, including the C bindings for that library.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "ObjCARC.h"
16 #include "llvm-c/Initialization.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/InitializePasses.h"
22 
23 namespace llvm {
24  class PassRegistry;
25 }
26 
27 using namespace llvm;
28 using namespace llvm::objcarc;
29 
30 /// initializeObjCARCOptsPasses - Initialize all passes linked into the
31 /// ObjCARCOpts library.
39 }
40 
43 }
44 
46  FunctionCallee Func, ArrayRef<Value *> Args, const Twine &NameStr,
47  Instruction *InsertBefore,
48  const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
49  FunctionType *FTy = Func.getFunctionType();
50  Value *Callee = Func.getCallee();
52 
53  if (!BlockColors.empty()) {
54  const ColorVector &CV = BlockColors.find(InsertBefore->getParent())->second;
55  assert(CV.size() == 1 && "non-unique color for block!");
56  Instruction *EHPad = CV.front()->getFirstNonPHI();
57  if (EHPad->isEHPad())
58  OpBundles.emplace_back("funclet", EHPad);
59  }
60 
61  return CallInst::Create(FTy, Callee, Args, OpBundles, NameStr, InsertBefore);
62 }
63 
64 std::pair<bool, bool>
66  bool Changed = false, CFGChanged = false;
67 
68  for (BasicBlock &BB : F) {
69  auto *I = dyn_cast<InvokeInst>(BB.getTerminator());
70 
71  if (!I)
72  continue;
73 
75  continue;
76 
77  BasicBlock *DestBB = I->getNormalDest();
78 
79  if (!DestBB->getSinglePredecessor()) {
80  assert(I->getSuccessor(0) == DestBB &&
81  "the normal dest is expected to be the first successor");
83  CFGChanged = true;
84  }
85 
86  // We don't have to call insertRVCallWithColors since DestBB is the normal
87  // destination of the invoke.
88  insertRVCall(&*DestBB->getFirstInsertionPt(), I);
89  Changed = true;
90  }
91 
92  return std::make_pair(Changed, CFGChanged);
93 }
94 
96  CallBase *AnnotatedCall) {
98  return insertRVCallWithColors(InsertPt, AnnotatedCall, BlockColors);
99 }
100 
102  Instruction *InsertPt, CallBase *AnnotatedCall,
103  const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
104  IRBuilder<> Builder(InsertPt);
105  Function *Func = *objcarc::getAttachedARCFunction(AnnotatedCall);
106  assert(Func && "operand isn't a Function");
107  Type *ParamTy = Func->getArg(0)->getType();
108  Value *CallArg = Builder.CreateBitCast(AnnotatedCall, ParamTy);
109  auto *Call =
110  createCallInstWithColors(Func, CallArg, "", InsertPt, BlockColors);
111  RVCalls[Call] = AnnotatedCall;
112  return Call;
113 }
114 
116  for (auto P : RVCalls) {
117  if (ContractPass) {
118  CallBase *CB = P.second;
119  // At this point, we know that the annotated calls can't be tail calls
120  // as they are followed by marker instructions and retainRV/claimRV
121  // calls. Mark them as notail so that the backend knows these calls
122  // can't be tail calls.
123  if (auto *CI = dyn_cast<CallInst>(CB))
124  CI->setTailCallKind(CallInst::TCK_NoTail);
125  }
126 
127  EraseInstruction(P.first);
128  }
129 
130  RVCalls.clear();
131 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::objcarc::hasAttachedCallOpBundle
bool hasAttachedCallOpBundle(const CallBase *CB)
Definition: ObjCARCUtil.h:29
llvm::objcarc::getAttachedARCFunction
Optional< Function * > getAttachedARCFunction(const CallBase *CB)
This function returns operand bundle clang_arc_attachedcall's argument, which is the address of the A...
Definition: ObjCARCUtil.h:43
llvm::initializePAEvalPass
void initializePAEvalPass(PassRegistry &)
LLVMPassRegistryRef
struct LLVMOpaquePassRegistry * LLVMPassRegistryRef
Definition: Types.h:130
llvm::Function
Definition: Function.h:60
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
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::IRBuilder<>
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::SplitCriticalEdge
BasicBlock * SplitCriticalEdge(Instruction *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions(), const Twine &BBName="")
If this edge is a critical edge, insert a new node to split the critical edge.
Definition: BreakCriticalEdges.cpp:101
llvm::TinyPtrVector::front
EltTy front() const
Definition: TinyPtrVector.h:230
ObjCARC.h
llvm::BasicBlock::getSinglePredecessor
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:261
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:240
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
llvm::initializeObjCARCContractLegacyPassPass
void initializeObjCARCContractLegacyPassPass(PassRegistry &)
llvm::CallInst::Create
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:1517
llvm::CriticalEdgeSplittingOptions
Option class for critical edge splitting.
Definition: BasicBlockUtils.h:142
llvm::initializeObjCARCOptLegacyPassPass
void initializeObjCARCOptLegacyPassPass(PassRegistry &)
llvm::BasicBlock::getFirstInsertionPt
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:246
llvm::Instruction
Definition: Instruction.h:42
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
llvm::initializeObjCARCOpts
void initializeObjCARCOpts(PassRegistry &)
Initialize all passes linked into the ObjCARCOpts library.
Definition: ObjCARC.cpp:32
llvm::unwrap
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:264
llvm::MCID::Call
@ Call
Definition: MCInstrDesc.h:155
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:101
llvm::objcarc::BundledRetainClaimRVs::~BundledRetainClaimRVs
~BundledRetainClaimRVs()
Definition: ObjCARC.cpp:115
llvm::DenseMap
Definition: DenseMap.h:716
llvm::initializeObjCARCAAWrapperPassPass
void initializeObjCARCAAWrapperPassPass(PassRegistry &)
I
#define I(x, y, z)
Definition: MD5.cpp:58
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:65
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:152
IRBuilder.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::NVPTXISD::CallArg
@ CallArg
Definition: NVPTXISelLowering.h:40
llvm::objcarc::BundledRetainClaimRVs::insertRVCall
CallInst * insertRVCall(Instruction *InsertPt, CallBase *AnnotatedCall)
Insert a retainRV/claimRV call.
Definition: ObjCARC.cpp:95
llvm::objcarc
Definition: ObjCARCAliasAnalysis.h:29
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:651
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::TinyPtrVector::size
unsigned size() const
Definition: TinyPtrVector.h:172
llvm::initializeObjCARCExpandPass
void initializeObjCARCExpandPass(PassRegistry &)
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::empty
LLVM_NODISCARD bool empty() const
Definition: DenseMap.h:98
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:186
ObjCARCUtil.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::Instruction::isEHPad
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:662
llvm::objcarc::EraseInstruction
static void EraseInstruction(Instruction *CI)
Erase the given instruction.
Definition: ObjCARC.h:39
llvm::initializeObjCARCAPElimPass
void initializeObjCARCAPElimPass(PassRegistry &)
Instructions.h
LLVMInitializeObjCARCOpts
void LLVMInitializeObjCARCOpts(LLVMPassRegistryRef R)
Definition: ObjCARC.cpp:41
llvm::Registry
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
llvm::Instruction::getParent
const BasicBlock * getParent() const
Definition: Instruction.h:91
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:45
llvm::FunctionCallee
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:165
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1474
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::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:394
llvm::TinyPtrVector
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
Definition: TinyPtrVector.h:30
llvm::CallInst::TCK_NoTail
@ TCK_NoTail
Definition: Instructions.h:1655
BasicBlockUtils.h
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:103
Initialization.h
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:927