LLVM  14.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/InlineAsm.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/InitializePasses.h"
23 
24 namespace llvm {
25  class PassRegistry;
26 }
27 
28 using namespace llvm;
29 using namespace llvm::objcarc;
30 
31 /// initializeObjCARCOptsPasses - Initialize all passes linked into the
32 /// ObjCARCOpts library.
40 }
41 
44 }
45 
47  FunctionCallee Func, ArrayRef<Value *> Args, const Twine &NameStr,
48  Instruction *InsertBefore,
49  const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
50  FunctionType *FTy = Func.getFunctionType();
51  Value *Callee = Func.getCallee();
53 
54  if (!BlockColors.empty()) {
55  const ColorVector &CV = BlockColors.find(InsertBefore->getParent())->second;
56  assert(CV.size() == 1 && "non-unique color for block!");
57  Instruction *EHPad = CV.front()->getFirstNonPHI();
58  if (EHPad->isEHPad())
59  OpBundles.emplace_back("funclet", EHPad);
60  }
61 
62  return CallInst::Create(FTy, Callee, Args, OpBundles, NameStr, InsertBefore);
63 }
64 
65 std::pair<bool, bool>
67  bool Changed = false, CFGChanged = false;
68 
69  for (BasicBlock &BB : F) {
70  auto *I = dyn_cast<InvokeInst>(BB.getTerminator());
71 
72  if (!I)
73  continue;
74 
76  continue;
77 
78  BasicBlock *DestBB = I->getNormalDest();
79 
80  if (!DestBB->getSinglePredecessor()) {
81  assert(I->getSuccessor(0) == DestBB &&
82  "the normal dest is expected to be the first successor");
84  CFGChanged = true;
85  }
86 
87  // We don't have to call insertRVCallWithColors since DestBB is the normal
88  // destination of the invoke.
89  insertRVCall(&*DestBB->getFirstInsertionPt(), I);
90  Changed = true;
91  }
92 
93  return std::make_pair(Changed, CFGChanged);
94 }
95 
97  CallBase *AnnotatedCall) {
99  return insertRVCallWithColors(InsertPt, AnnotatedCall, BlockColors);
100 }
101 
103  Instruction *InsertPt, CallBase *AnnotatedCall,
104  const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
105  IRBuilder<> Builder(InsertPt);
106  Function *Func = *objcarc::getAttachedARCFunction(AnnotatedCall);
107  assert(Func && "operand isn't a Function");
108  Type *ParamTy = Func->getArg(0)->getType();
109  Value *CallArg = Builder.CreateBitCast(AnnotatedCall, ParamTy);
110  auto *Call =
111  createCallInstWithColors(Func, CallArg, "", InsertPt, BlockColors);
112  RVCalls[Call] = AnnotatedCall;
113  return Call;
114 }
115 
117  for (auto P : RVCalls) {
118  if (ContractPass) {
119  CallBase *CB = P.second;
120  // At this point, we know that the annotated calls can't be tail calls
121  // as they are followed by marker instructions and retainRV/claimRV
122  // calls. Mark them as notail so that the backend knows these calls
123  // can't be tail calls.
124  if (auto *CI = dyn_cast<CallInst>(CB))
125  CI->setTailCallKind(CallInst::TCK_NoTail);
126 
127  if (UseMarker) {
128  // Remove the retainRV/claimRV function operand from the operand bundle
129  // to reflect the fact that the backend is responsible for emitting only
130  // the marker instruction, but not the retainRV/claimRV call.
131  OperandBundleDef OB("clang.arc.attachedcall", None);
132  auto *NewCB = CallBase::Create(CB, OB, CB);
133  CB->replaceAllUsesWith(NewCB);
134  CB->eraseFromParent();
135  }
136  }
137 
138  if (!ContractPass || !UseMarker)
139  EraseInstruction(P.first);
140  }
141 
142  RVCalls.clear();
143 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
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:62
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:1168
InlineAsm.h
llvm::IRBuilder<>
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
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:103
llvm::OperandBundleDefT
A container for an operand bundle being viewed as a set of values rather than a set of uses.
Definition: InstrTypes.h:1129
llvm::TinyPtrVector::front
EltTy front() const
Definition: TinyPtrVector.h:230
ObjCARC.h
llvm::X86II::OB
@ OB
Definition: X86BaseInfo.h:801
llvm::BasicBlock::getSinglePredecessor
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:268
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:207
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::initializeObjCARCContractLegacyPassPass
void initializeObjCARCContractLegacyPassPass(PassRegistry &)
llvm::CallInst::Create
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:1530
llvm::CriticalEdgeSplittingOptions
Option class for critical edge splitting.
Definition: BasicBlockUtils.h:143
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:253
llvm::Instruction
Definition: Instruction.h:45
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:33
llvm::unwrap
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:256
DebugLocVerifyLevel::None
@ None
llvm::MCID::Call
@ Call
Definition: MCInstrDesc.h:153
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:116
llvm::DenseMap
Definition: DenseMap.h:714
llvm::initializeObjCARCAAWrapperPassPass
void initializeObjCARCAAWrapperPassPass(PassRegistry &)
I
#define I(x, y, z)
Definition: MD5.cpp:59
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
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:150
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:96
llvm::objcarc
Definition: ObjCARCAliasAnalysis.h:29
llvm::CallBase::Create
static CallBase * Create(CallBase *CB, ArrayRef< OperandBundleDef > Bundles, Instruction *InsertPt=nullptr)
Create a clone of CB with a different set of operand bundles and insert it before InsertPt.
Definition: Instructions.cpp:255
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:650
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::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:532
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:97
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:206
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:665
llvm::objcarc::EraseInstruction
static void EraseInstruction(Instruction *CI)
Erase the given instruction.
Definition: ObjCARC.h:40
llvm::initializeObjCARCAPElimPass
void initializeObjCARCAPElimPass(PassRegistry &)
Instructions.h
LLVMInitializeObjCARCOpts
void LLVMInitializeObjCARCOpts(LLVMPassRegistryRef R)
Definition: ObjCARC.cpp:42
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:94
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:165
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1176
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1487
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:389
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:1668
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:908