xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp (revision 162ae9c834f6d9f9cb443bd62cceb23e0b5fef48)
1 //===- ObjCARCContract.cpp - ObjC ARC Optimization ------------------------===//
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 /// \file
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
11 /// in Objective C.
12 ///
13 /// This specific file mainly deals with ``contracting'' multiple lower level
14 /// operations into singular higher level operations through pattern matching.
15 ///
16 /// WARNING: This file knows about certain library functions. It recognizes them
17 /// by name, and hardwires knowledge of their semantics.
18 ///
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.
22 ///
23 //===----------------------------------------------------------------------===//
24 
25 // TODO: ObjCARCContract could insert PHI nodes when uses aren't
26 // dominated by single calls.
27 
28 #include "ARCRuntimeEntryPoints.h"
29 #include "DependencyAnalysis.h"
30 #include "ObjCARC.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"
39 
40 using namespace llvm;
41 using namespace llvm::objcarc;
42 
43 #define DEBUG_TYPE "objc-arc-contract"
44 
45 STATISTIC(NumPeeps,       "Number of calls peephole-optimized");
46 STATISTIC(NumStoreStrongs, "Number objc_storeStrong calls formed");
47 
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));
51 
52 //===----------------------------------------------------------------------===//
53 //                                Declarations
54 //===----------------------------------------------------------------------===//
55 
56 namespace {
57   /// Late ARC optimizations
58   ///
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 {
62     bool Changed;
63     AliasAnalysis *AA;
64     DominatorTree *DT;
65     ProvenanceAnalysis PA;
66     ARCRuntimeEntryPoints EP;
67 
68     /// A flag indicating whether this optimization pass should run.
69     bool Run;
70 
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;
74 
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
77     /// "tail".
78     SmallPtrSet<CallInst *, 8> StoreStrongCalls;
79 
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);
87 
88     bool optimizeRetainCall(Function &F, Instruction *Retain);
89 
90     bool
91     contractAutorelease(Function &F, Instruction *Autorelease,
92                         ARCInstKind Class,
93                         SmallPtrSetImpl<Instruction *> &DependingInstructions,
94                         SmallPtrSetImpl<const BasicBlock *> &Visited);
95 
96     void tryToContractReleaseIntoStoreStrong(
97         Instruction *Release, inst_iterator &Iter,
98         const DenseMap<BasicBlock *, ColorVector> &BlockColors);
99 
100     void getAnalysisUsage(AnalysisUsage &AU) const override;
101     bool doInitialization(Module &M) override;
102     bool runOnFunction(Function &F) override;
103 
104   public:
105     static char ID;
106     ObjCARCContract() : FunctionPass(ID) {
107       initializeObjCARCContractPass(*PassRegistry::getPassRegistry());
108     }
109   };
110 }
111 
112 //===----------------------------------------------------------------------===//
113 //                               Implementation
114 //===----------------------------------------------------------------------===//
115 
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
118 /// ObjCARCOpt.
119 bool ObjCARCContract::optimizeRetainCall(Function &F, Instruction *Retain) {
120   ImmutableCallSite CS(GetArgRCIdentityRoot(Retain));
121   const Instruction *Call = CS.getInstruction();
122   if (!Call)
123     return false;
124   if (Call->getParent() != Retain->getParent())
125     return false;
126 
127   // Check that the call is next to the retain.
128   BasicBlock::const_iterator I = ++Call->getIterator();
129   while (IsNoopInstruction(&*I))
130     ++I;
131   if (&*I != Retain)
132     return false;
133 
134   // Turn it to an objc_retainAutoreleasedReturnValue.
135   Changed = true;
136   ++NumPeeps;
137 
138   LLVM_DEBUG(
139       dbgs() << "Transforming objc_retain => "
140                 "objc_retainAutoreleasedReturnValue since the operand is a "
141                 "return value.\nOld: "
142              << *Retain << "\n");
143 
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);
148 
149   LLVM_DEBUG(dbgs() << "New: " << *Retain << "\n");
150   return true;
151 }
152 
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);
159 
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);
167   else
168     FindDependencies(RetainAutoreleaseDep, Arg,
169                      Autorelease->getParent(), Autorelease,
170                      DependingInstructions, Visited, PA);
171 
172   Visited.clear();
173   if (DependingInstructions.size() != 1) {
174     DependingInstructions.clear();
175     return false;
176   }
177 
178   Retain = dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
179   DependingInstructions.clear();
180 
181   if (!Retain || GetBasicARCInstKind(Retain) != ARCInstKind::Retain ||
182       GetArgRCIdentityRoot(Retain) != Arg)
183     return false;
184 
185   Changed = true;
186   ++NumPeeps;
187 
188   LLVM_DEBUG(dbgs() << "    Fusing retain/autorelease!\n"
189                        "        Autorelease:"
190                     << *Autorelease
191                     << "\n"
192                        "        Retain: "
193                     << *Retain << "\n");
194 
195   Function *Decl = EP.get(Class == ARCInstKind::AutoreleaseRV
196                               ? ARCRuntimeEntryPointKind::RetainAutoreleaseRV
197                               : ARCRuntimeEntryPointKind::RetainAutorelease);
198   Retain->setCalledFunction(Decl);
199 
200   LLVM_DEBUG(dbgs() << "        New RetainAutorelease: " << *Retain << "\n");
201 
202   EraseInstruction(Autorelease);
203   return true;
204 }
205 
206 static StoreInst *findSafeStoreForStoreStrongContraction(LoadInst *Load,
207                                                          Instruction *Release,
208                                                          ProvenanceAnalysis &PA,
209                                                          AliasAnalysis *AA) {
210   StoreInst *Store = nullptr;
211   bool SawRelease = false;
212 
213   // Get the location associated with Load.
214   MemoryLocation Loc = MemoryLocation::get(Load);
215   auto *LocPtr = Loc.Ptr->stripPointerCasts();
216 
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();
220        I != E; ++I) {
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)
224       break;
225 
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) {
230       SawRelease = true;
231       continue;
232     }
233 
234     // Otherwise, we check if Inst is a "good" store. Grab the instruction class
235     // of Inst.
236     ARCInstKind Class = GetBasicARCInstKind(Inst);
237 
238     // If Inst is an unrelated retain, we don't care about it.
239     //
240     // TODO: This is one area where the optimization could be made more
241     // aggressive.
242     if (IsRetain(Class))
243       continue;
244 
245     // If we have seen the store, but not the release...
246     if (Store) {
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
251       // continue...
252       if (!CanUse(Inst, Load, PA, Class)) {
253         continue;
254       }
255 
256       // Otherwise, be conservative and return nullptr.
257       return nullptr;
258     }
259 
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)))
263       continue;
264 
265     Store = dyn_cast<StoreInst>(Inst);
266 
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())
272       return nullptr;
273 
274     // Then make sure that the pointer we are storing to is Ptr. If so, we
275     // found our Store!
276     if (Store->getPointerOperand()->stripPointerCasts() == LocPtr)
277       continue;
278 
279     // Otherwise, we have an unknown store to some other ptr that clobbers
280     // Loc.Ptr. Bail!
281     return nullptr;
282   }
283 
284   // If we did not find the store or did not see the release, fail.
285   if (!Store || !SawRelease)
286     return nullptr;
287 
288   // We succeeded!
289   return Store;
290 }
291 
292 static Instruction *
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;
301 
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)
306       return nullptr;
307     --I;
308   }
309   Instruction *Retain = &*I;
310   if (GetBasicARCInstKind(Retain) != ARCInstKind::Retain)
311     return nullptr;
312   if (GetArgRCIdentityRoot(Retain) != New)
313     return nullptr;
314   return Retain;
315 }
316 
317 /// Create a call instruction with the correct funclet token. Should be used
318 /// instead of calling CallInst::Create directly.
319 static CallInst *
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);
330   }
331 
332   return CallInst::Create(FTy, Func, Args, OpBundles, NameStr, InsertBefore);
333 }
334 
335 static CallInst *
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);
341 }
342 
343 /// Attempt to merge an objc_release with a store, load, and objc_retain to form
344 /// an objc_storeStrong. An objc_storeStrong:
345 ///
346 ///   objc_storeStrong(i8** %old_ptr, i8* new_value)
347 ///
348 /// is equivalent to the following IR sequence:
349 ///
350 ///   ; Load old value.
351 ///   %old_value = load i8** %old_ptr               (1)
352 ///
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)
358 ///
359 ///   ; Store the new_value into old_ptr
360 ///   store i8* %new_value, i8** %old_ptr           (4)
361 ///
362 /// The safety of this optimization is based around the following
363 /// considerations:
364 ///
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
367 ///     (4).
368 ///  2. We need to make sure that any re-orderings of (1), (2), (3), (4) are
369 ///     safe.
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())
376     return;
377 
378   // For now, require everything to be in one basic block.
379   BasicBlock *BB = Release->getParent();
380   if (Load->getParent() != BB)
381     return;
382 
383   // First scan down the BB from Load, looking for a store of the RCIdentityRoot
384   // of Load's
385   StoreInst *Store =
386       findSafeStoreForStoreStrongContraction(Load, Release, PA, AA);
387   // If we fail, bail.
388   if (!Store)
389     return;
390 
391   // Then find what new_value's RCIdentity Root is.
392   Value *New = GetRCIdentityRoot(Store->getValueOperand());
393 
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);
398 
399   // If we fail, bail.
400   if (!Retain)
401     return;
402 
403   Changed = true;
404   ++NumStoreStrongs;
405 
406   LLVM_DEBUG(
407       llvm::dbgs() << "    Contracting retain, release into objc_storeStrong.\n"
408                    << "        Old:\n"
409                    << "            Store:   " << *Store << "\n"
410                    << "            Release: " << *Release << "\n"
411                    << "            Retain:  " << *Retain << "\n"
412                    << "            Load:    " << *Load << "\n");
413 
414   LLVMContext &C = Release->getContext();
415   Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
416   Type *I8XX = PointerType::getUnqual(I8X);
417 
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());
427 
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);
432 
433   LLVM_DEBUG(llvm::dbgs() << "        New Store Strong: " << *StoreStrong
434                           << "\n");
435 
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();
443 }
444 
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);
453   switch (Class) {
454   case ARCInstKind::FusedRetainAutorelease:
455   case ARCInstKind::FusedRetainAutoreleaseRV:
456     return false;
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
462     // calls.
463     if (!optimizeRetainCall(F, Inst))
464       return false;
465     // If we succeed in our optimization, fall through.
466     LLVM_FALLTHROUGH;
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.
471     if (!RVInstMarker)
472       return false;
473     BasicBlock::iterator BBI = Inst->getIterator();
474     BasicBlock *InstParent = Inst->getParent();
475 
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.
479     do {
480       if (BBI == InstParent->begin()) {
481         BasicBlock *Pred = InstParent->getSinglePredecessor();
482         if (!Pred)
483           goto decline_rv_optimization;
484         BBI = Pred->getTerminator()->getIterator();
485         break;
486       }
487       --BBI;
488     } while (IsNoopInstruction(&*BBI));
489 
490     if (&*BBI == GetArgRCIdentityRoot(Inst)) {
491       LLVM_DEBUG(dbgs() << "Adding inline asm marker for the return value "
492                            "optimization.\n");
493       Changed = true;
494       InlineAsm *IA =
495           InlineAsm::get(FunctionType::get(Type::getVoidTy(Inst->getContext()),
496                                            /*isVarArg=*/false),
497                          RVInstMarker->getString(),
498                          /*Constraints=*/"", /*hasSideEffects=*/true);
499 
500       createCallInst(IA, None, "", Inst, BlockColors);
501     }
502   decline_rv_optimization:
503     return false;
504   }
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()));
510       Changed = true;
511       new StoreInst(Null, CI->getArgOperand(0), CI);
512 
513       LLVM_DEBUG(dbgs() << "OBJCARCContract: Old = " << *CI << "\n"
514                         << "                 New = " << *Null << "\n");
515 
516       CI->replaceAllUsesWith(Null);
517       CI->eraseFromParent();
518     }
519     return true;
520   }
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);
525     return true;
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;
532     return true;
533   case ARCInstKind::IntrinsicUser:
534     // Remove calls to @llvm.objc.clang.arc.use(...).
535     Inst->eraseFromParent();
536     return true;
537   default:
538     return true;
539   }
540 }
541 
542 //===----------------------------------------------------------------------===//
543 //                              Top Level Driver
544 //===----------------------------------------------------------------------===//
545 
546 bool ObjCARCContract::runOnFunction(Function &F) {
547   if (!EnableARCOpts)
548     return false;
549 
550   // If nothing in the Module uses ARC, don't do anything.
551   if (!Run)
552     return false;
553 
554   Changed = false;
555   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
556   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
557 
558   PA.setAA(&getAnalysis<AAResultsWrapperPass>().getAAResults());
559 
560   DenseMap<BasicBlock *, ColorVector> BlockColors;
561   if (F.hasPersonalityFn() &&
562       isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
563     BlockColors = colorEHFunclets(F);
564 
565   LLVM_DEBUG(llvm::dbgs() << "**** ObjCARC Contract ****\n");
566 
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();
574 
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;
580 
581   // Cache the basic block size.
582   DenseMap<const BasicBlock *, unsigned> BBSizeMap;
583 
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) {
587     unsigned BBSize;
588     auto I = BBSizeMap.find(BB);
589 
590     if (I != BBSizeMap.end())
591       BBSize = I->second;
592     else
593       BBSize = BBSizeMap[BB] = BB->size();
594 
595     return BBSize > MaxBBSize;
596   };
597 
598   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E;) {
599     Instruction *Inst = &*I++;
600 
601     LLVM_DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
602 
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))
607       continue;
608 
609     // Otherwise, try to undo objc-arc-expand.
610 
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*.
613 
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))
618         return;
619 
620       // Look through the uses of the pointer.
621       for (Value::use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
622            UI != UE; ) {
623         // Increment UI now, because we may unlink its element.
624         Use &U = *UI++;
625         unsigned OperandNo = U.getOperandNo();
626 
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()))
636           continue;
637 
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))
645           continue;
646 
647         Changed = true;
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();
661             }
662 
663             assert(DT->dominates(Inst, &InsertBB->back()) &&
664                    "Invalid insertion point for bitcast");
665             Replacement =
666                 new BitCastInst(Replacement, UseTy, "", &InsertBB->back());
667           }
668 
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
671           // bitcasts we emit.
672           for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
673             if (PHI->getIncomingBlock(i) == IncomingBB) {
674               // Keep the UI iterator valid.
675               if (UI != UE &&
676                   &PHI->getOperandUse(
677                       PHINode::getOperandNumForIncomingValue(i)) == &*UI)
678                 ++UI;
679               PHI->setIncomingValue(i, Replacement);
680             }
681         } else {
682           if (Replacement->getType() != UseTy)
683             Replacement = new BitCastInst(Replacement, UseTy, "",
684                                           cast<Instruction>(U.getUser()));
685           U.set(Replacement);
686         }
687       }
688     };
689 
690 
691     Value *Arg = cast<CallInst>(Inst)->getArgOperand(0);
692     Value *OrigArg = Arg;
693 
694     // TODO: Change this to a do-while.
695     for (;;) {
696       ReplaceArgUses(Arg);
697 
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();
707       else {
708         // If Arg is a PHI node, get PHIs that are equivalent to it and replace
709         // their uses.
710         if (PHINode *PN = dyn_cast<PHINode>(Arg)) {
711           SmallVector<Value *, 1> PHIList;
712           getEquivalentPHIs(*PN, PHIList);
713           for (Value *PHI : PHIList)
714             ReplaceArgUses(PHI);
715         }
716         break;
717       }
718     }
719 
720     // Replace bitcast users of Arg that are dominated by Inst.
721     SmallVector<BitCastInst *, 2> BitCastUsers;
722 
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);
727 
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);
734 
735       ReplaceArgUses(BC);
736     }
737   }
738 
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)
743       CI->setTailCall();
744   StoreStrongCalls.clear();
745 
746   return Changed;
747 }
748 
749 //===----------------------------------------------------------------------===//
750 //                             Misc Pass Manager
751 //===----------------------------------------------------------------------===//
752 
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)
760 
761 void ObjCARCContract::getAnalysisUsage(AnalysisUsage &AU) const {
762   AU.addRequired<AAResultsWrapperPass>();
763   AU.addRequired<DominatorTreeWrapperPass>();
764   AU.setPreservesCFG();
765 }
766 
767 Pass *llvm::createObjCARCContractPass() { return new ObjCARCContract(); }
768 
769 bool ObjCARCContract::doInitialization(Module &M) {
770   // If nothing in the Module uses ARC, don't do anything.
771   Run = ModuleHasARC(M);
772   if (!Run)
773     return false;
774 
775   EP.init(&M);
776 
777   // Initialize RVInstMarker.
778   const char *MarkerKey = "clang.arc.retainAutoreleasedReturnValueMarker";
779   RVInstMarker = dyn_cast_or_null<MDString>(M.getModuleFlag(MarkerKey));
780 
781   return false;
782 }
783