1 //===- ObjCARCContract.cpp - ObjC ARC Optimization ------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 /// This file defines late ObjC ARC optimizations. ARC stands for Automatic
10 /// Reference Counting and is a system for managing reference counts for objects
13 /// This specific file mainly deals with ``contracting'' multiple lower level
14 /// operations into singular higher level operations through pattern matching.
16 /// WARNING: This file knows about certain library functions. It recognizes them
17 /// by name, and hardwires knowledge of their semantics.
19 /// WARNING: This file knows about how certain Objective-C library functions are
20 /// used. Naive LLVM IR transformations which would otherwise be
21 /// behavior-preserving may break these assumptions.
23 //===----------------------------------------------------------------------===//
25 // TODO: ObjCARCContract could insert PHI nodes when uses aren't
26 // dominated by single calls.
28 #include "ARCRuntimeEntryPoints.h"
29 #include "DependencyAnalysis.h"
31 #include "ProvenanceAnalysis.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Analysis/EHPersonalities.h"
34 #include "llvm/IR/Dominators.h"
35 #include "llvm/IR/InlineAsm.h"
36 #include "llvm/IR/Operator.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/raw_ostream.h"
41 using namespace llvm::objcarc
;
43 #define DEBUG_TYPE "objc-arc-contract"
45 STATISTIC(NumPeeps
, "Number of calls peephole-optimized");
46 STATISTIC(NumStoreStrongs
, "Number objc_storeStrong calls formed");
48 static cl::opt
<unsigned> MaxBBSize("arc-contract-max-bb-size", cl::Hidden
,
49 cl::desc("Maximum basic block size to discover the dominance relation of "
50 "two instructions in the same basic block"), cl::init(65535));
52 //===----------------------------------------------------------------------===//
54 //===----------------------------------------------------------------------===//
57 /// Late ARC optimizations
59 /// These change the IR in a way that makes it difficult to be analyzed by
60 /// ObjCARCOpt, so it's run late.
61 class ObjCARCContract
: public FunctionPass
{
65 ProvenanceAnalysis PA
;
66 ARCRuntimeEntryPoints EP
;
68 /// A flag indicating whether this optimization pass should run.
71 /// The inline asm string to insert between calls and RetainRV calls to make
72 /// the optimization work on targets which need it.
73 const MDString
*RVInstMarker
;
75 /// The set of inserted objc_storeStrong calls. If at the end of walking the
76 /// function we have found no alloca instructions, these calls can be marked
78 SmallPtrSet
<CallInst
*, 8> StoreStrongCalls
;
80 /// Returns true if we eliminated Inst.
81 bool tryToPeepholeInstruction(
82 Function
&F
, Instruction
*Inst
, inst_iterator
&Iter
,
83 SmallPtrSetImpl
<Instruction
*> &DepInsts
,
84 SmallPtrSetImpl
<const BasicBlock
*> &Visited
,
85 bool &TailOkForStoreStrong
,
86 const DenseMap
<BasicBlock
*, ColorVector
> &BlockColors
);
88 bool optimizeRetainCall(Function
&F
, Instruction
*Retain
);
91 contractAutorelease(Function
&F
, Instruction
*Autorelease
,
93 SmallPtrSetImpl
<Instruction
*> &DependingInstructions
,
94 SmallPtrSetImpl
<const BasicBlock
*> &Visited
);
96 void tryToContractReleaseIntoStoreStrong(
97 Instruction
*Release
, inst_iterator
&Iter
,
98 const DenseMap
<BasicBlock
*, ColorVector
> &BlockColors
);
100 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
101 bool doInitialization(Module
&M
) override
;
102 bool runOnFunction(Function
&F
) override
;
106 ObjCARCContract() : FunctionPass(ID
) {
107 initializeObjCARCContractPass(*PassRegistry::getPassRegistry());
112 //===----------------------------------------------------------------------===//
114 //===----------------------------------------------------------------------===//
116 /// Turn objc_retain into objc_retainAutoreleasedReturnValue if the operand is a
117 /// return value. We do this late so we do not disrupt the dataflow analysis in
119 bool ObjCARCContract::optimizeRetainCall(Function
&F
, Instruction
*Retain
) {
120 ImmutableCallSite
CS(GetArgRCIdentityRoot(Retain
));
121 const Instruction
*Call
= CS
.getInstruction();
124 if (Call
->getParent() != Retain
->getParent())
127 // Check that the call is next to the retain.
128 BasicBlock::const_iterator I
= ++Call
->getIterator();
129 while (IsNoopInstruction(&*I
))
134 // Turn it to an objc_retainAutoreleasedReturnValue.
139 dbgs() << "Transforming objc_retain => "
140 "objc_retainAutoreleasedReturnValue since the operand is a "
141 "return value.\nOld: "
144 // We do not have to worry about tail calls/does not throw since
145 // retain/retainRV have the same properties.
146 Function
*Decl
= EP
.get(ARCRuntimeEntryPointKind::RetainRV
);
147 cast
<CallInst
>(Retain
)->setCalledFunction(Decl
);
149 LLVM_DEBUG(dbgs() << "New: " << *Retain
<< "\n");
153 /// Merge an autorelease with a retain into a fused call.
154 bool ObjCARCContract::contractAutorelease(
155 Function
&F
, Instruction
*Autorelease
, ARCInstKind Class
,
156 SmallPtrSetImpl
<Instruction
*> &DependingInstructions
,
157 SmallPtrSetImpl
<const BasicBlock
*> &Visited
) {
158 const Value
*Arg
= GetArgRCIdentityRoot(Autorelease
);
160 // Check that there are no instructions between the retain and the autorelease
161 // (such as an autorelease_pop) which may change the count.
162 CallInst
*Retain
= nullptr;
163 if (Class
== ARCInstKind::AutoreleaseRV
)
164 FindDependencies(RetainAutoreleaseRVDep
, Arg
,
165 Autorelease
->getParent(), Autorelease
,
166 DependingInstructions
, Visited
, PA
);
168 FindDependencies(RetainAutoreleaseDep
, Arg
,
169 Autorelease
->getParent(), Autorelease
,
170 DependingInstructions
, Visited
, PA
);
173 if (DependingInstructions
.size() != 1) {
174 DependingInstructions
.clear();
178 Retain
= dyn_cast_or_null
<CallInst
>(*DependingInstructions
.begin());
179 DependingInstructions
.clear();
181 if (!Retain
|| GetBasicARCInstKind(Retain
) != ARCInstKind::Retain
||
182 GetArgRCIdentityRoot(Retain
) != Arg
)
188 LLVM_DEBUG(dbgs() << " Fusing retain/autorelease!\n"
195 Function
*Decl
= EP
.get(Class
== ARCInstKind::AutoreleaseRV
196 ? ARCRuntimeEntryPointKind::RetainAutoreleaseRV
197 : ARCRuntimeEntryPointKind::RetainAutorelease
);
198 Retain
->setCalledFunction(Decl
);
200 LLVM_DEBUG(dbgs() << " New RetainAutorelease: " << *Retain
<< "\n");
202 EraseInstruction(Autorelease
);
206 static StoreInst
*findSafeStoreForStoreStrongContraction(LoadInst
*Load
,
207 Instruction
*Release
,
208 ProvenanceAnalysis
&PA
,
210 StoreInst
*Store
= nullptr;
211 bool SawRelease
= false;
213 // Get the location associated with Load.
214 MemoryLocation Loc
= MemoryLocation::get(Load
);
215 auto *LocPtr
= Loc
.Ptr
->stripPointerCasts();
217 // Walk down to find the store and the release, which may be in either order.
218 for (auto I
= std::next(BasicBlock::iterator(Load
)),
219 E
= Load
->getParent()->end();
221 // If we found the store we were looking for and saw the release,
222 // break. There is no more work to be done.
223 if (Store
&& SawRelease
)
226 // Now we know that we have not seen either the store or the release. If I
227 // is the release, mark that we saw the release and continue.
228 Instruction
*Inst
= &*I
;
229 if (Inst
== Release
) {
234 // Otherwise, we check if Inst is a "good" store. Grab the instruction class
236 ARCInstKind Class
= GetBasicARCInstKind(Inst
);
238 // If Inst is an unrelated retain, we don't care about it.
240 // TODO: This is one area where the optimization could be made more
245 // If we have seen the store, but not the release...
247 // We need to make sure that it is safe to move the release from its
248 // current position to the store. This implies proving that any
249 // instruction in between Store and the Release conservatively can not use
250 // the RCIdentityRoot of Release. If we can prove we can ignore Inst, so
252 if (!CanUse(Inst
, Load
, PA
, Class
)) {
256 // Otherwise, be conservative and return nullptr.
260 // Ok, now we know we have not seen a store yet. See if Inst can write to
261 // our load location, if it can not, just ignore the instruction.
262 if (!isModSet(AA
->getModRefInfo(Inst
, Loc
)))
265 Store
= dyn_cast
<StoreInst
>(Inst
);
267 // If Inst can, then check if Inst is a simple store. If Inst is not a
268 // store or a store that is not simple, then we have some we do not
269 // understand writing to this memory implying we can not move the load
270 // over the write to any subsequent store that we may find.
271 if (!Store
|| !Store
->isSimple())
274 // Then make sure that the pointer we are storing to is Ptr. If so, we
276 if (Store
->getPointerOperand()->stripPointerCasts() == LocPtr
)
279 // Otherwise, we have an unknown store to some other ptr that clobbers
284 // If we did not find the store or did not see the release, fail.
285 if (!Store
|| !SawRelease
)
293 findRetainForStoreStrongContraction(Value
*New
, StoreInst
*Store
,
294 Instruction
*Release
,
295 ProvenanceAnalysis
&PA
) {
296 // Walk up from the Store to find the retain.
297 BasicBlock::iterator I
= Store
->getIterator();
298 BasicBlock::iterator Begin
= Store
->getParent()->begin();
299 while (I
!= Begin
&& GetBasicARCInstKind(&*I
) != ARCInstKind::Retain
) {
300 Instruction
*Inst
= &*I
;
302 // It is only safe to move the retain to the store if we can prove
303 // conservatively that nothing besides the release can decrement reference
304 // counts in between the retain and the store.
305 if (CanDecrementRefCount(Inst
, New
, PA
) && Inst
!= Release
)
309 Instruction
*Retain
= &*I
;
310 if (GetBasicARCInstKind(Retain
) != ARCInstKind::Retain
)
312 if (GetArgRCIdentityRoot(Retain
) != New
)
317 /// Create a call instruction with the correct funclet token. Should be used
318 /// instead of calling CallInst::Create directly.
320 createCallInst(FunctionType
*FTy
, Value
*Func
, ArrayRef
<Value
*> Args
,
321 const Twine
&NameStr
, Instruction
*InsertBefore
,
322 const DenseMap
<BasicBlock
*, ColorVector
> &BlockColors
) {
323 SmallVector
<OperandBundleDef
, 1> OpBundles
;
324 if (!BlockColors
.empty()) {
325 const ColorVector
&CV
= BlockColors
.find(InsertBefore
->getParent())->second
;
326 assert(CV
.size() == 1 && "non-unique color for block!");
327 Instruction
*EHPad
= CV
.front()->getFirstNonPHI();
328 if (EHPad
->isEHPad())
329 OpBundles
.emplace_back("funclet", EHPad
);
332 return CallInst::Create(FTy
, Func
, Args
, OpBundles
, NameStr
, InsertBefore
);
336 createCallInst(FunctionCallee Func
, ArrayRef
<Value
*> Args
, const Twine
&NameStr
,
337 Instruction
*InsertBefore
,
338 const DenseMap
<BasicBlock
*, ColorVector
> &BlockColors
) {
339 return createCallInst(Func
.getFunctionType(), Func
.getCallee(), Args
, NameStr
,
340 InsertBefore
, BlockColors
);
343 /// Attempt to merge an objc_release with a store, load, and objc_retain to form
344 /// an objc_storeStrong. An objc_storeStrong:
346 /// objc_storeStrong(i8** %old_ptr, i8* new_value)
348 /// is equivalent to the following IR sequence:
350 /// ; Load old value.
351 /// %old_value = load i8** %old_ptr (1)
353 /// ; Increment the new value and then release the old value. This must occur
354 /// ; in order in case old_value releases new_value in its destructor causing
355 /// ; us to potentially have a dangling ptr.
356 /// tail call i8* @objc_retain(i8* %new_value) (2)
357 /// tail call void @objc_release(i8* %old_value) (3)
359 /// ; Store the new_value into old_ptr
360 /// store i8* %new_value, i8** %old_ptr (4)
362 /// The safety of this optimization is based around the following
365 /// 1. We are forming the store strong at the store. Thus to perform this
366 /// optimization it must be safe to move the retain, load, and release to
368 /// 2. We need to make sure that any re-orderings of (1), (2), (3), (4) are
370 void ObjCARCContract::tryToContractReleaseIntoStoreStrong(
371 Instruction
*Release
, inst_iterator
&Iter
,
372 const DenseMap
<BasicBlock
*, ColorVector
> &BlockColors
) {
373 // See if we are releasing something that we just loaded.
374 auto *Load
= dyn_cast
<LoadInst
>(GetArgRCIdentityRoot(Release
));
375 if (!Load
|| !Load
->isSimple())
378 // For now, require everything to be in one basic block.
379 BasicBlock
*BB
= Release
->getParent();
380 if (Load
->getParent() != BB
)
383 // First scan down the BB from Load, looking for a store of the RCIdentityRoot
386 findSafeStoreForStoreStrongContraction(Load
, Release
, PA
, AA
);
391 // Then find what new_value's RCIdentity Root is.
392 Value
*New
= GetRCIdentityRoot(Store
->getValueOperand());
394 // Then walk up the BB and look for a retain on New without any intervening
395 // instructions which conservatively might decrement ref counts.
396 Instruction
*Retain
=
397 findRetainForStoreStrongContraction(New
, Store
, Release
, PA
);
407 llvm::dbgs() << " Contracting retain, release into objc_storeStrong.\n"
409 << " Store: " << *Store
<< "\n"
410 << " Release: " << *Release
<< "\n"
411 << " Retain: " << *Retain
<< "\n"
412 << " Load: " << *Load
<< "\n");
414 LLVMContext
&C
= Release
->getContext();
415 Type
*I8X
= PointerType::getUnqual(Type::getInt8Ty(C
));
416 Type
*I8XX
= PointerType::getUnqual(I8X
);
418 Value
*Args
[] = { Load
->getPointerOperand(), New
};
419 if (Args
[0]->getType() != I8XX
)
420 Args
[0] = new BitCastInst(Args
[0], I8XX
, "", Store
);
421 if (Args
[1]->getType() != I8X
)
422 Args
[1] = new BitCastInst(Args
[1], I8X
, "", Store
);
423 Function
*Decl
= EP
.get(ARCRuntimeEntryPointKind::StoreStrong
);
424 CallInst
*StoreStrong
= createCallInst(Decl
, Args
, "", Store
, BlockColors
);
425 StoreStrong
->setDoesNotThrow();
426 StoreStrong
->setDebugLoc(Store
->getDebugLoc());
428 // We can't set the tail flag yet, because we haven't yet determined
429 // whether there are any escaping allocas. Remember this call, so that
430 // we can set the tail flag once we know it's safe.
431 StoreStrongCalls
.insert(StoreStrong
);
433 LLVM_DEBUG(llvm::dbgs() << " New Store Strong: " << *StoreStrong
436 if (&*Iter
== Retain
) ++Iter
;
437 if (&*Iter
== Store
) ++Iter
;
438 Store
->eraseFromParent();
439 Release
->eraseFromParent();
440 EraseInstruction(Retain
);
441 if (Load
->use_empty())
442 Load
->eraseFromParent();
445 bool ObjCARCContract::tryToPeepholeInstruction(
446 Function
&F
, Instruction
*Inst
, inst_iterator
&Iter
,
447 SmallPtrSetImpl
<Instruction
*> &DependingInsts
,
448 SmallPtrSetImpl
<const BasicBlock
*> &Visited
, bool &TailOkForStoreStrongs
,
449 const DenseMap
<BasicBlock
*, ColorVector
> &BlockColors
) {
450 // Only these library routines return their argument. In particular,
451 // objc_retainBlock does not necessarily return its argument.
452 ARCInstKind Class
= GetBasicARCInstKind(Inst
);
454 case ARCInstKind::FusedRetainAutorelease
:
455 case ARCInstKind::FusedRetainAutoreleaseRV
:
457 case ARCInstKind::Autorelease
:
458 case ARCInstKind::AutoreleaseRV
:
459 return contractAutorelease(F
, Inst
, Class
, DependingInsts
, Visited
);
460 case ARCInstKind::Retain
:
461 // Attempt to convert retains to retainrvs if they are next to function
463 if (!optimizeRetainCall(F
, Inst
))
465 // If we succeed in our optimization, fall through.
467 case ARCInstKind::RetainRV
:
468 case ARCInstKind::ClaimRV
: {
469 // If we're compiling for a target which needs a special inline-asm
470 // marker to do the return value optimization, insert it now.
473 BasicBlock::iterator BBI
= Inst
->getIterator();
474 BasicBlock
*InstParent
= Inst
->getParent();
476 // Step up to see if the call immediately precedes the RV call.
477 // If it's an invoke, we have to cross a block boundary. And we have
478 // to carefully dodge no-op instructions.
480 if (BBI
== InstParent
->begin()) {
481 BasicBlock
*Pred
= InstParent
->getSinglePredecessor();
483 goto decline_rv_optimization
;
484 BBI
= Pred
->getTerminator()->getIterator();
488 } while (IsNoopInstruction(&*BBI
));
490 if (&*BBI
== GetArgRCIdentityRoot(Inst
)) {
491 LLVM_DEBUG(dbgs() << "Adding inline asm marker for the return value "
495 InlineAsm::get(FunctionType::get(Type::getVoidTy(Inst
->getContext()),
497 RVInstMarker
->getString(),
498 /*Constraints=*/"", /*hasSideEffects=*/true);
500 createCallInst(IA
, None
, "", Inst
, BlockColors
);
502 decline_rv_optimization
:
505 case ARCInstKind::InitWeak
: {
506 // objc_initWeak(p, null) => *p = null
507 CallInst
*CI
= cast
<CallInst
>(Inst
);
508 if (IsNullOrUndef(CI
->getArgOperand(1))) {
509 Value
*Null
= ConstantPointerNull::get(cast
<PointerType
>(CI
->getType()));
511 new StoreInst(Null
, CI
->getArgOperand(0), CI
);
513 LLVM_DEBUG(dbgs() << "OBJCARCContract: Old = " << *CI
<< "\n"
514 << " New = " << *Null
<< "\n");
516 CI
->replaceAllUsesWith(Null
);
517 CI
->eraseFromParent();
521 case ARCInstKind::Release
:
522 // Try to form an objc store strong from our release. If we fail, there is
523 // nothing further to do below, so continue.
524 tryToContractReleaseIntoStoreStrong(Inst
, Iter
, BlockColors
);
526 case ARCInstKind::User
:
527 // Be conservative if the function has any alloca instructions.
528 // Technically we only care about escaping alloca instructions,
529 // but this is sufficient to handle some interesting cases.
530 if (isa
<AllocaInst
>(Inst
))
531 TailOkForStoreStrongs
= false;
533 case ARCInstKind::IntrinsicUser
:
534 // Remove calls to @llvm.objc.clang.arc.use(...).
535 Inst
->eraseFromParent();
542 //===----------------------------------------------------------------------===//
544 //===----------------------------------------------------------------------===//
546 bool ObjCARCContract::runOnFunction(Function
&F
) {
550 // If nothing in the Module uses ARC, don't do anything.
555 AA
= &getAnalysis
<AAResultsWrapperPass
>().getAAResults();
556 DT
= &getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
558 PA
.setAA(&getAnalysis
<AAResultsWrapperPass
>().getAAResults());
560 DenseMap
<BasicBlock
*, ColorVector
> BlockColors
;
561 if (F
.hasPersonalityFn() &&
562 isScopedEHPersonality(classifyEHPersonality(F
.getPersonalityFn())))
563 BlockColors
= colorEHFunclets(F
);
565 LLVM_DEBUG(llvm::dbgs() << "**** ObjCARC Contract ****\n");
567 // Track whether it's ok to mark objc_storeStrong calls with the "tail"
568 // keyword. Be conservative if the function has variadic arguments.
569 // It seems that functions which "return twice" are also unsafe for the
570 // "tail" argument, because they are setjmp, which could need to
571 // return to an earlier stack state.
572 bool TailOkForStoreStrongs
=
573 !F
.isVarArg() && !F
.callsFunctionThatReturnsTwice();
575 // For ObjC library calls which return their argument, replace uses of the
576 // argument with uses of the call return value, if it dominates the use. This
577 // reduces register pressure.
578 SmallPtrSet
<Instruction
*, 4> DependingInstructions
;
579 SmallPtrSet
<const BasicBlock
*, 4> Visited
;
581 // Cache the basic block size.
582 DenseMap
<const BasicBlock
*, unsigned> BBSizeMap
;
584 // A lambda that lazily computes the size of a basic block and determines
585 // whether the size exceeds MaxBBSize.
586 auto IsLargeBB
= [&](const BasicBlock
*BB
) {
588 auto I
= BBSizeMap
.find(BB
);
590 if (I
!= BBSizeMap
.end())
593 BBSize
= BBSizeMap
[BB
] = BB
->size();
595 return BBSize
> MaxBBSize
;
598 for (inst_iterator I
= inst_begin(&F
), E
= inst_end(&F
); I
!= E
;) {
599 Instruction
*Inst
= &*I
++;
601 LLVM_DEBUG(dbgs() << "Visiting: " << *Inst
<< "\n");
603 // First try to peephole Inst. If there is nothing further we can do in
604 // terms of undoing objc-arc-expand, process the next inst.
605 if (tryToPeepholeInstruction(F
, Inst
, I
, DependingInstructions
, Visited
,
606 TailOkForStoreStrongs
, BlockColors
))
609 // Otherwise, try to undo objc-arc-expand.
611 // Don't use GetArgRCIdentityRoot because we don't want to look through bitcasts
612 // and such; to do the replacement, the argument must have type i8*.
614 // Function for replacing uses of Arg dominated by Inst.
615 auto ReplaceArgUses
= [Inst
, IsLargeBB
, this](Value
*Arg
) {
616 // If we're compiling bugpointed code, don't get in trouble.
617 if (!isa
<Instruction
>(Arg
) && !isa
<Argument
>(Arg
))
620 // Look through the uses of the pointer.
621 for (Value::use_iterator UI
= Arg
->use_begin(), UE
= Arg
->use_end();
623 // Increment UI now, because we may unlink its element.
625 unsigned OperandNo
= U
.getOperandNo();
627 // Don't replace the uses if Inst and the user belong to the same basic
628 // block and the size of the basic block is large. We don't want to call
629 // DominatorTree::dominate in that case. We can remove this check if we
630 // can use OrderedBasicBlock to compute the dominance relation between
631 // two instructions, but that's not currently possible since it doesn't
632 // recompute the instruction ordering when new instructions are inserted
633 // to the basic block.
634 if (Inst
->getParent() == cast
<Instruction
>(U
.getUser())->getParent() &&
635 IsLargeBB(Inst
->getParent()))
638 // If the call's return value dominates a use of the call's argument
639 // value, rewrite the use to use the return value. We check for
640 // reachability here because an unreachable call is considered to
641 // trivially dominate itself, which would lead us to rewriting its
642 // argument in terms of its return value, which would lead to
643 // infinite loops in GetArgRCIdentityRoot.
644 if (!DT
->isReachableFromEntry(U
) || !DT
->dominates(Inst
, U
))
648 Instruction
*Replacement
= Inst
;
649 Type
*UseTy
= U
.get()->getType();
650 if (PHINode
*PHI
= dyn_cast
<PHINode
>(U
.getUser())) {
651 // For PHI nodes, insert the bitcast in the predecessor block.
652 unsigned ValNo
= PHINode::getIncomingValueNumForOperand(OperandNo
);
653 BasicBlock
*IncomingBB
= PHI
->getIncomingBlock(ValNo
);
654 if (Replacement
->getType() != UseTy
) {
655 // A catchswitch is both a pad and a terminator, meaning a basic
656 // block with a catchswitch has no insertion point. Keep going up
657 // the dominator tree until we find a non-catchswitch.
658 BasicBlock
*InsertBB
= IncomingBB
;
659 while (isa
<CatchSwitchInst
>(InsertBB
->getFirstNonPHI())) {
660 InsertBB
= DT
->getNode(InsertBB
)->getIDom()->getBlock();
663 assert(DT
->dominates(Inst
, &InsertBB
->back()) &&
664 "Invalid insertion point for bitcast");
666 new BitCastInst(Replacement
, UseTy
, "", &InsertBB
->back());
669 // While we're here, rewrite all edges for this PHI, rather
670 // than just one use at a time, to minimize the number of
672 for (unsigned i
= 0, e
= PHI
->getNumIncomingValues(); i
!= e
; ++i
)
673 if (PHI
->getIncomingBlock(i
) == IncomingBB
) {
674 // Keep the UI iterator valid.
677 PHINode::getOperandNumForIncomingValue(i
)) == &*UI
)
679 PHI
->setIncomingValue(i
, Replacement
);
682 if (Replacement
->getType() != UseTy
)
683 Replacement
= new BitCastInst(Replacement
, UseTy
, "",
684 cast
<Instruction
>(U
.getUser()));
691 Value
*Arg
= cast
<CallInst
>(Inst
)->getArgOperand(0);
692 Value
*OrigArg
= Arg
;
694 // TODO: Change this to a do-while.
698 // If Arg is a no-op casted pointer, strip one level of casts and iterate.
699 if (const BitCastInst
*BI
= dyn_cast
<BitCastInst
>(Arg
))
700 Arg
= BI
->getOperand(0);
701 else if (isa
<GEPOperator
>(Arg
) &&
702 cast
<GEPOperator
>(Arg
)->hasAllZeroIndices())
703 Arg
= cast
<GEPOperator
>(Arg
)->getPointerOperand();
704 else if (isa
<GlobalAlias
>(Arg
) &&
705 !cast
<GlobalAlias
>(Arg
)->isInterposable())
706 Arg
= cast
<GlobalAlias
>(Arg
)->getAliasee();
708 // If Arg is a PHI node, get PHIs that are equivalent to it and replace
710 if (PHINode
*PN
= dyn_cast
<PHINode
>(Arg
)) {
711 SmallVector
<Value
*, 1> PHIList
;
712 getEquivalentPHIs(*PN
, PHIList
);
713 for (Value
*PHI
: PHIList
)
720 // Replace bitcast users of Arg that are dominated by Inst.
721 SmallVector
<BitCastInst
*, 2> BitCastUsers
;
723 // Add all bitcast users of the function argument first.
724 for (User
*U
: OrigArg
->users())
725 if (auto *BC
= dyn_cast
<BitCastInst
>(U
))
726 BitCastUsers
.push_back(BC
);
728 // Replace the bitcasts with the call return. Iterate until list is empty.
729 while (!BitCastUsers
.empty()) {
730 auto *BC
= BitCastUsers
.pop_back_val();
731 for (User
*U
: BC
->users())
732 if (auto *B
= dyn_cast
<BitCastInst
>(U
))
733 BitCastUsers
.push_back(B
);
739 // If this function has no escaping allocas or suspicious vararg usage,
740 // objc_storeStrong calls can be marked with the "tail" keyword.
741 if (TailOkForStoreStrongs
)
742 for (CallInst
*CI
: StoreStrongCalls
)
744 StoreStrongCalls
.clear();
749 //===----------------------------------------------------------------------===//
751 //===----------------------------------------------------------------------===//
753 char ObjCARCContract::ID
= 0;
754 INITIALIZE_PASS_BEGIN(ObjCARCContract
, "objc-arc-contract",
755 "ObjC ARC contraction", false, false)
756 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass
)
757 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
758 INITIALIZE_PASS_END(ObjCARCContract
, "objc-arc-contract",
759 "ObjC ARC contraction", false, false)
761 void ObjCARCContract::getAnalysisUsage(AnalysisUsage
&AU
) const {
762 AU
.addRequired
<AAResultsWrapperPass
>();
763 AU
.addRequired
<DominatorTreeWrapperPass
>();
764 AU
.setPreservesCFG();
767 Pass
*llvm::createObjCARCContractPass() { return new ObjCARCContract(); }
769 bool ObjCARCContract::doInitialization(Module
&M
) {
770 // If nothing in the Module uses ARC, don't do anything.
771 Run
= ModuleHasARC(M
);
777 // Initialize RVInstMarker.
778 const char *MarkerKey
= "clang.arc.retainAutoreleasedReturnValueMarker";
779 RVInstMarker
= dyn_cast_or_null
<MDString
>(M
.getModuleFlag(MarkerKey
));