LLVM  9.0.0svn
ObjCARCContract.cpp
Go to the documentation of this file.
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"
34 #include "llvm/IR/Dominators.h"
35 #include "llvm/IR/InlineAsm.h"
36 #include "llvm/IR/Operator.h"
37 #include "llvm/Support/Debug.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;
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,
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,
95 
96  void tryToContractReleaseIntoStoreStrong(
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) {
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) {
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.
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.
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(
156  SmallPtrSetImpl<Instruction *> &DependingInstructions,
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)
165  Autorelease->getParent(), Autorelease,
166  DependingInstructions, Visited, PA);
167  else
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
198  Retain->setCalledFunction(Decl);
199 
200  LLVM_DEBUG(dbgs() << " New RetainAutorelease: " << *Retain << "\n");
201 
202  EraseInstruction(Autorelease);
203  return true;
204 }
205 
208  ProvenanceAnalysis &PA,
209  AliasAnalysis *AA) {
210  StoreInst *Store = nullptr;
211  bool SawRelease = false;
212 
213  // Get the location associated with 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 *
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;
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 *
321  const Twine &NameStr, Instruction *InsertBefore,
322  const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
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 *
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(
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 =
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();
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);
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) {
456  return false;
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.
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 =
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  }
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;
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 
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 
561  if (F.hasPersonalityFn() &&
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 =
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;
580 
581  // Cache the basic block size.
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(
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)
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 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:80
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.h:176
objc arc contract
uint64_t CallInst * C
Value * getValueOperand()
Definition: Instructions.h:409
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
use_iterator use_end()
Definition: Value.h:346
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:645
This file declares special dependency analysis routines used in Objective C ARC Optimizations.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Value * getPointerOperand(Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
This file contains a class ARCRuntimeEntryPoints for use in creating/managing references to entry poi...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
EltTy front() const
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
bool CanUse(const Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
Test whether the given instruction can "use" the given pointer&#39;s object in a way that requires the re...
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Blocks objc_retainAutorelease.
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:164
This class represents a function call, abstracting a target machine&#39;s calling convention.
static CallInst * createCallInst(FunctionType *FTy, Value *Func, ArrayRef< Value *> Args, const Twine &NameStr, Instruction *InsertBefore, const DenseMap< BasicBlock *, ColorVector > &BlockColors)
Create a call instruction with the correct funclet token.
Value * GetArgRCIdentityRoot(Value *Inst)
Assuming the given instruction is one of the special calls such as objc_retain or objc_release...
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:709
unsigned second
STATISTIC(NumFunctions, "Total number of functions")
F(f)
could call objc_release
An instruction for reading from memory.
Definition: Instructions.h:167
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:137
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
Definition: TinyPtrVector.h:30
Value * get() const
Definition: Use.h:107
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:268
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1218
static unsigned getOperandNumForIncomingValue(unsigned i)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
objc_autoreleaseReturnValue
static cl::opt< unsigned > MaxBBSize("arc-contract-max-bb-size", cl::Hidden, cl::desc("Maximum basic block size to discover the dominance relation of " "two instructions in the same basic block"), cl::init(65535))
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:131
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
void initializeObjCARCContractPass(PassRegistry &)
bool IsNullOrUndef(const Value *V)
This file defines common definitions/declarations used by the ObjC ARC Optimizer. ...
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch, catchpad/ret, and cleanuppad/ret.
objc_retainAutoreleasedReturnValue
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
Definition: Use.cpp:40
Class to represent function types.
Definition: DerivedTypes.h:102
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
Definition: InstrTypes.h:1300
Pass * createObjCARCContractPass()
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
This class represents a no-op cast from one type to another.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
An instruction for storing to memory.
Definition: Instructions.h:320
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:707
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition: Use.cpp:47
INITIALIZE_PASS_BEGIN(ObjCARCContract, "objc-arc-contract", "ObjC ARC contraction", false, false) INITIALIZE_PASS_END(ObjCARCContract
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
bool ModuleHasARC(const Module &M)
Test if the given module looks interesting to run ARC optimization on.
use_iterator_impl< Use > use_iterator
Definition: Value.h:331
static bool runOnFunction(Function &F, bool PostInlining)
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:189
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static StoreInst * findSafeStoreForStoreStrongContraction(LoadInst *Load, Instruction *Release, ProvenanceAnalysis &PA, AliasAnalysis *AA)
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:318
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1410
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:233
void set(Value *Val)
Definition: Value.h:670
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:310
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
objc_initWeak (derived)
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:160
const Instruction & back() const
Definition: BasicBlock.h:282
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:296
self_iterator getIterator()
Definition: ilist_node.h:81
static Instruction * findRetainForStoreStrongContraction(Value *New, StoreInst *Store, Instruction *Release, ProvenanceAnalysis &PA)
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, and aliases.
Definition: Value.cpp:529
static unsigned getIncomingValueNumForOperand(unsigned i)
static wasm::ValType getType(const TargetRegisterClass *RC)
anything that is inert from an ARC perspective.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
bool CanDecrementRefCount(ARCInstKind Kind)
Returns false if conservatively we can prove that any instruction mapped to this kind can not decreme...
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
static void EraseInstruction(Instruction *CI)
Erase the given instruction.
Definition: ObjCARC.h:51
objc arc ObjC ARC contraction
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:417
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the generic address space (address sp...
Definition: DerivedTypes.h:513
iterator end()
Definition: BasicBlock.h:270
This file declares a special form of Alias Analysis called ``Provenance Analysis&#39;&#39;.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841
ARCInstKind
Equivalence classes of instructions in the ARC Model.
objc_unsafeClaimAutoreleasedReturnValue
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void FindDependencies(DependenceKind Flavor, const Value *Arg, BasicBlock *StartBB, Instruction *StartInst, SmallPtrSetImpl< Instruction *> &DependingInstructions, SmallPtrSetImpl< const BasicBlock *> &Visited, ProvenanceAnalysis &PA)
Walk up the CFG from StartPos (which is in StartBB) and find local and non-local dependencies on Arg...
iterator_range< user_iterator > users()
Definition: Value.h:399
objc arc
objc_storeStrong (derived)
could "use" a pointer
bool IsRetain(ARCInstKind Class)
Test if the given class is objc_retain or equivalent.
use_iterator use_begin()
Definition: Value.h:338
LLVM_NODISCARD bool isModSet(const ModRefInfo MRI)
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:321
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
Establish a view to a call site for examination.
Definition: CallSite.h:892
Blocks objc_retainAutoreleaseReturnValue.
#define I(x, y, z)
Definition: MD5.cpp:58
FunctionType * getFunctionType()
Definition: DerivedTypes.h:181
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
Declarations for ObjC runtime functions and constants.
static InlineAsm * get(FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack=false, AsmDialect asmDialect=AD_ATT)
InlineAsm::get - Return the specified uniqued inline asm string.
Definition: InlineAsm.cpp:42
bool callsFunctionThatReturnsTwice() const
callsFunctionThatReturnsTwice - Return true if the function has a call to setjmp or other function th...
Definition: Function.cpp:1297
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.cpp:1306
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:250
static const Function * getParent(const Value *V)
This is similar to BasicAliasAnalysis, and it uses many of the same techniques, except it uses specia...
const Value * GetRCIdentityRoot(const Value *V)
The RCIdentity root of a value V is a dominating value U for which retaining or releasing U is equiva...
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:132
A single uniqued string.
Definition: Metadata.h:603
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:259
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
bool isSimple() const
Definition: Instructions.h:401
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc)
getModRefInfo (for call sites) - Return information about whether a particular call site modifies or ...
DenseMap< BasicBlock *, ColorVector > colorEHFunclets(Function &F)
If an EH funclet personality is in use (see isFuncletEHPersonality), this will recompute which blocks...
#define LLVM_DEBUG(X)
Definition: Debug.h:122
Value * getPointerOperand()
Definition: Instructions.h:412
void setDoesNotThrow()
Definition: InstrTypes.h:1655
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:173
unsigned size() const
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList)
Return the list of PHI nodes that are equivalent to PN.
Definition: ObjCARC.h:86
bool IsNoopInstruction(const Instruction *I)
const BasicBlock * getParent() const
Definition: Instruction.h:66