LLVM  13.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  bool IsRetainRV = objcarc::hasAttachedCallOpBundle(AnnotatedCall, true);
107  Function *Func = EP.get(IsRetainRV ? ARCRuntimeEntryPointKind::RetainRV
109  Type *ParamTy = Func->getArg(0)->getType();
110  Value *CallArg = Builder.CreateBitCast(AnnotatedCall, ParamTy);
111  auto *Call =
112  createCallInstWithColors(Func, CallArg, "", InsertPt, BlockColors);
113  RVCalls[Call] = AnnotatedCall;
114  return Call;
115 }
116 
118  if (ContractPass) {
119  // At this point, we know that the annotated calls can't be tail calls as
120  // they are followed by marker instructions and retainRV/claimRV calls. Mark
121  // them as notail, so that the backend knows these calls can't be tail
122  // calls.
123  for (auto P : RVCalls)
124  if (auto *CI = dyn_cast<CallInst>(P.second))
125  CI->setTailCallKind(CallInst::TCK_NoTail);
126  } else {
127  for (auto P : RVCalls)
128  EraseInstruction(P.first);
129  }
130 
131  RVCalls.clear();
132 }
llvm
Definition: AllocatorList.h:23
llvm::initializePAEvalPass
void initializePAEvalPass(PassRegistry &)
LLVMPassRegistryRef
struct LLVMOpaquePassRegistry * LLVMPassRegistryRef
Definition: Types.h:130
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
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1167
InlineAsm.h
llvm::IRBuilder<>
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
llvm::objcarc::ARCRuntimeEntryPointKind::RetainRV
@ RetainRV
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
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::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:264
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:180
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::initializeObjCARCContractLegacyPassPass
void initializeObjCARCContractLegacyPassPass(PassRegistry &)
llvm::objcarc::hasAttachedCallOpBundle
bool hasAttachedCallOpBundle(const CallBase *CB, bool IsRetain)
Definition: ObjCARCUtil.h:34
llvm::CallInst::Create
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:1521
llvm::CriticalEdgeSplittingOptions
Option class for critical edge splitting.
Definition: BasicBlockUtils.h:136
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:249
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:244
llvm::MCID::Call
@ Call
Definition: MCInstrDesc.h:153
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::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
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::objcarc::ARCRuntimeEntryPointKind::ClaimRV
@ ClaimRV
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:80
llvm::objcarc::ARCRuntimeEntryPoints::get
Function * get(ARCRuntimeEntryPointKind kind)
Definition: ARCRuntimeEntryPoints.h:70
llvm::Instruction::isEHPad
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:649
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: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::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:1659
BasicBlockUtils.h
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:102
Initialization.h
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:907