LLVM  16.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"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/Instructions.h"
20 
21 using namespace llvm;
22 using namespace llvm::objcarc;
23 
25  FunctionCallee Func, ArrayRef<Value *> Args, const Twine &NameStr,
26  Instruction *InsertBefore,
27  const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
28  FunctionType *FTy = Func.getFunctionType();
29  Value *Callee = Func.getCallee();
31 
32  if (!BlockColors.empty()) {
33  const ColorVector &CV = BlockColors.find(InsertBefore->getParent())->second;
34  assert(CV.size() == 1 && "non-unique color for block!");
35  Instruction *EHPad = CV.front()->getFirstNonPHI();
36  if (EHPad->isEHPad())
37  OpBundles.emplace_back("funclet", EHPad);
38  }
39 
40  return CallInst::Create(FTy, Callee, Args, OpBundles, NameStr, InsertBefore);
41 }
42 
43 std::pair<bool, bool>
45  bool Changed = false, CFGChanged = false;
46 
47  for (BasicBlock &BB : F) {
48  auto *I = dyn_cast<InvokeInst>(BB.getTerminator());
49 
50  if (!I)
51  continue;
52 
54  continue;
55 
56  BasicBlock *DestBB = I->getNormalDest();
57 
58  if (!DestBB->getSinglePredecessor()) {
59  assert(I->getSuccessor(0) == DestBB &&
60  "the normal dest is expected to be the first successor");
62  CFGChanged = true;
63  }
64 
65  // We don't have to call insertRVCallWithColors since DestBB is the normal
66  // destination of the invoke.
67  insertRVCall(&*DestBB->getFirstInsertionPt(), I);
68  Changed = true;
69  }
70 
71  return std::make_pair(Changed, CFGChanged);
72 }
73 
75  CallBase *AnnotatedCall) {
77  return insertRVCallWithColors(InsertPt, AnnotatedCall, BlockColors);
78 }
79 
81  Instruction *InsertPt, CallBase *AnnotatedCall,
82  const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
83  IRBuilder<> Builder(InsertPt);
84  Function *Func = *objcarc::getAttachedARCFunction(AnnotatedCall);
85  assert(Func && "operand isn't a Function");
86  Type *ParamTy = Func->getArg(0)->getType();
87  Value *CallArg = Builder.CreateBitCast(AnnotatedCall, ParamTy);
88  auto *Call =
89  createCallInstWithColors(Func, CallArg, "", InsertPt, BlockColors);
90  RVCalls[Call] = AnnotatedCall;
91  return Call;
92 }
93 
95  for (auto P : RVCalls) {
96  if (ContractPass) {
97  CallBase *CB = P.second;
98  // At this point, we know that the annotated calls can't be tail calls
99  // as they are followed by marker instructions and retainRV/claimRV
100  // calls. Mark them as notail so that the backend knows these calls
101  // can't be tail calls.
102  if (auto *CI = dyn_cast<CallInst>(CB))
103  CI->setTailCallKind(CallInst::TCK_NoTail);
104  }
105 
106  EraseInstruction(P.first);
107  }
108 
109  RVCalls.clear();
110 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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::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:1199
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:285
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
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::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::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:80
llvm::objcarc::BundledRetainClaimRVs::~BundledRetainClaimRVs
~BundledRetainClaimRVs()
Definition: ObjCARC.cpp:94
llvm::DenseMap
Definition: DenseMap.h:714
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:44
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:74
llvm::objcarc
Definition: ObjCARCAliasAnalysis.h:29
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::empty
bool empty() const
Definition: DenseMap.h:98
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
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:187
ObjCARCUtil.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::Instruction::isEHPad
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:669
llvm::objcarc::EraseInstruction
static void EraseInstruction(Instruction *CI)
Erase the given instruction.
Definition: ObjCARC.h:39
Instructions.h
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:24
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:1175
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
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:103
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:941