LLVM 19.0.0git
Go to the documentation of this file.
1//===-- ObjCARC.cpp -------------------------------------------------------===//
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
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.
15#include "ObjCARC.h"
17#include "llvm/IR/IRBuilder.h"
21using namespace llvm;
22using namespace llvm::objcarc;
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();
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 }
40 return CallInst::Create(FTy, Callee, Args, OpBundles, NameStr, InsertBefore);
43std::pair<bool, bool>
45 bool Changed = false, CFGChanged = false;
47 for (BasicBlock &BB : F) {
48 auto *I = dyn_cast<InvokeInst>(BB.getTerminator());
50 if (!I)
51 continue;
54 continue;
56 BasicBlock *DestBB = I->getNormalDest();
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 }
65 // We don't have to call insertRVCallWithColors since DestBB is the normal
66 // destination of the invoke.
68 Changed = true;
69 }
71 return std::make_pair(Changed, CFGChanged);
75 CallBase *AnnotatedCall) {
77 return insertRVCallWithColors(InsertPt, AnnotatedCall, BlockColors);
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;
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 }
106 EraseInstruction(P.first);
107 }
109 RVCalls.clear();
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file defines ARC utility functions which are used by various parts of the compiler.
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
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:454
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:497
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1457
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr, BasicBlock::iterator InsertBefore)
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
bool empty() const
Definition: DenseMap.h:98
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:168
Class to represent function types.
Definition: DerivedTypes.h:103
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2110
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2649
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:800
const BasicBlock * getParent() const
Definition: Instruction.h:150
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
Definition: TinyPtrVector.h:29
EltTy front() const
unsigned size() const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
CallInst * insertRVCall(Instruction *InsertPt, CallBase *AnnotatedCall)
Insert a retainRV/claimRV call.
Definition: ObjCARC.cpp:74
CallInst * insertRVCallWithColors(Instruction *InsertPt, CallBase *AnnotatedCall, const DenseMap< BasicBlock *, ColorVector > &BlockColors)
Insert a retainRV/claimRV call with colors.
Definition: ObjCARC.cpp:80
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
This file defines common definitions/declarations used by the ObjC ARC Optimizer.
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
@ Call
could call objc_release
std::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
bool hasAttachedCallOpBundle(const CallBase *CB)
Definition: ObjCARCUtil.h:29
static void EraseInstruction(Instruction *CI)
Erase the given instruction.
Definition: ObjCARC.h:39
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
Option class for critical edge splitting.