LLVM  12.0.0git
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"
35 #include "llvm/IR/Dominators.h"
36 #include "llvm/IR/InlineAsm.h"
37 #include "llvm/IR/InstIterator.h"
38 #include "llvm/IR/Operator.h"
39 #include "llvm/IR/PassManager.h"
40 #include "llvm/InitializePasses.h"
42 #include "llvm/Support/Debug.h"
45 
46 using namespace llvm;
47 using namespace llvm::objcarc;
48 
49 #define DEBUG_TYPE "objc-arc-contract"
50 
51 STATISTIC(NumPeeps, "Number of calls peephole-optimized");
52 STATISTIC(NumStoreStrongs, "Number objc_storeStrong calls formed");
53 
54 //===----------------------------------------------------------------------===//
55 // Declarations
56 //===----------------------------------------------------------------------===//
57 
58 namespace {
59 /// Late ARC optimizations
60 ///
61 /// These change the IR in a way that makes it difficult to be analyzed by
62 /// ObjCARCOpt, so it's run late.
63 
64 class ObjCARCContract {
65  bool Changed;
66  AAResults *AA;
67  DominatorTree *DT;
70 
71  /// A flag indicating whether this optimization pass should run.
72  bool Run;
73 
74  /// The inline asm string to insert between calls and RetainRV calls to make
75  /// the optimization work on targets which need it.
76  const MDString *RVInstMarker;
77 
78  /// The set of inserted objc_storeStrong calls. If at the end of walking the
79  /// function we have found no alloca instructions, these calls can be marked
80  /// "tail".
81  SmallPtrSet<CallInst *, 8> StoreStrongCalls;
82 
83  /// Returns true if we eliminated Inst.
84  bool tryToPeepholeInstruction(
85  Function &F, Instruction *Inst, inst_iterator &Iter,
86  bool &TailOkForStoreStrong,
87  const DenseMap<BasicBlock *, ColorVector> &BlockColors);
88 
89  bool optimizeRetainCall(Function &F, Instruction *Retain);
90 
91  bool contractAutorelease(Function &F, Instruction *Autorelease,
92  ARCInstKind Class);
93 
94  void tryToContractReleaseIntoStoreStrong(
96  const DenseMap<BasicBlock *, ColorVector> &BlockColors);
97 
98 public:
99  bool init(Module &M);
100  bool run(Function &F, AAResults *AA, DominatorTree *DT);
101 };
102 
103 class ObjCARCContractLegacyPass : public FunctionPass {
104  ObjCARCContract OCARCC;
105 
106 public:
107  void getAnalysisUsage(AnalysisUsage &AU) const override;
108  bool doInitialization(Module &M) override;
109  bool runOnFunction(Function &F) override;
110 
111  static char ID;
112  ObjCARCContractLegacyPass() : FunctionPass(ID) {
114  }
115 };
116 }
117 
118 //===----------------------------------------------------------------------===//
119 // Implementation
120 //===----------------------------------------------------------------------===//
121 
122 /// Turn objc_retain into objc_retainAutoreleasedReturnValue if the operand is a
123 /// return value. We do this late so we do not disrupt the dataflow analysis in
124 /// ObjCARCOpt.
125 bool ObjCARCContract::optimizeRetainCall(Function &F, Instruction *Retain) {
126  const auto *Call = dyn_cast<CallBase>(GetArgRCIdentityRoot(Retain));
127  if (!Call)
128  return false;
129  if (Call->getParent() != Retain->getParent())
130  return false;
131 
132  // Check that the call is next to the retain.
133  BasicBlock::const_iterator I = ++Call->getIterator();
134  while (IsNoopInstruction(&*I))
135  ++I;
136  if (&*I != Retain)
137  return false;
138 
139  // Turn it to an objc_retainAutoreleasedReturnValue.
140  Changed = true;
141  ++NumPeeps;
142 
143  LLVM_DEBUG(
144  dbgs() << "Transforming objc_retain => "
145  "objc_retainAutoreleasedReturnValue since the operand is a "
146  "return value.\nOld: "
147  << *Retain << "\n");
148 
149  // We do not have to worry about tail calls/does not throw since
150  // retain/retainRV have the same properties.
152  cast<CallInst>(Retain)->setCalledFunction(Decl);
153 
154  LLVM_DEBUG(dbgs() << "New: " << *Retain << "\n");
155  return true;
156 }
157 
158 /// Merge an autorelease with a retain into a fused call.
159 bool ObjCARCContract::contractAutorelease(Function &F, Instruction *Autorelease,
160  ARCInstKind Class) {
161  const Value *Arg = GetArgRCIdentityRoot(Autorelease);
162 
163  // Check that there are no instructions between the retain and the autorelease
164  // (such as an autorelease_pop) which may change the count.
168  auto *Retain = dyn_cast_or_null<CallInst>(
169  findSingleDependency(DK, Arg, Autorelease->getParent(), Autorelease, PA));
170 
171  if (!Retain || GetBasicARCInstKind(Retain) != ARCInstKind::Retain ||
172  GetArgRCIdentityRoot(Retain) != Arg)
173  return false;
174 
175  Changed = true;
176  ++NumPeeps;
177 
178  LLVM_DEBUG(dbgs() << " Fusing retain/autorelease!\n"
179  " Autorelease:"
180  << *Autorelease
181  << "\n"
182  " Retain: "
183  << *Retain << "\n");
184 
185  Function *Decl = EP.get(Class == ARCInstKind::AutoreleaseRV
188  Retain->setCalledFunction(Decl);
189 
190  LLVM_DEBUG(dbgs() << " New RetainAutorelease: " << *Retain << "\n");
191 
192  EraseInstruction(Autorelease);
193  return true;
194 }
195 
197  Instruction *Release,
198  ProvenanceAnalysis &PA,
199  AAResults *AA) {
200  StoreInst *Store = nullptr;
201  bool SawRelease = false;
202 
203  // Get the location associated with Load.
205  auto *LocPtr = Loc.Ptr->stripPointerCasts();
206 
207  // Walk down to find the store and the release, which may be in either order.
208  for (auto I = std::next(BasicBlock::iterator(Load)),
209  E = Load->getParent()->end();
210  I != E; ++I) {
211  // If we found the store we were looking for and saw the release,
212  // break. There is no more work to be done.
213  if (Store && SawRelease)
214  break;
215 
216  // Now we know that we have not seen either the store or the release. If I
217  // is the release, mark that we saw the release and continue.
218  Instruction *Inst = &*I;
219  if (Inst == Release) {
220  SawRelease = true;
221  continue;
222  }
223 
224  // Otherwise, we check if Inst is a "good" store. Grab the instruction class
225  // of Inst.
226  ARCInstKind Class = GetBasicARCInstKind(Inst);
227 
228  // If Inst is an unrelated retain, we don't care about it.
229  //
230  // TODO: This is one area where the optimization could be made more
231  // aggressive.
232  if (IsRetain(Class))
233  continue;
234 
235  // If we have seen the store, but not the release...
236  if (Store) {
237  // We need to make sure that it is safe to move the release from its
238  // current position to the store. This implies proving that any
239  // instruction in between Store and the Release conservatively can not use
240  // the RCIdentityRoot of Release. If we can prove we can ignore Inst, so
241  // continue...
242  if (!CanUse(Inst, Load, PA, Class)) {
243  continue;
244  }
245 
246  // Otherwise, be conservative and return nullptr.
247  return nullptr;
248  }
249 
250  // Ok, now we know we have not seen a store yet. See if Inst can write to
251  // our load location, if it can not, just ignore the instruction.
252  if (!isModSet(AA->getModRefInfo(Inst, Loc)))
253  continue;
254 
255  Store = dyn_cast<StoreInst>(Inst);
256 
257  // If Inst can, then check if Inst is a simple store. If Inst is not a
258  // store or a store that is not simple, then we have some we do not
259  // understand writing to this memory implying we can not move the load
260  // over the write to any subsequent store that we may find.
261  if (!Store || !Store->isSimple())
262  return nullptr;
263 
264  // Then make sure that the pointer we are storing to is Ptr. If so, we
265  // found our Store!
266  if (Store->getPointerOperand()->stripPointerCasts() == LocPtr)
267  continue;
268 
269  // Otherwise, we have an unknown store to some other ptr that clobbers
270  // Loc.Ptr. Bail!
271  return nullptr;
272  }
273 
274  // If we did not find the store or did not see the release, fail.
275  if (!Store || !SawRelease)
276  return nullptr;
277 
278  // We succeeded!
279  return Store;
280 }
281 
282 static Instruction *
284  Instruction *Release,
285  ProvenanceAnalysis &PA) {
286  // Walk up from the Store to find the retain.
287  BasicBlock::iterator I = Store->getIterator();
288  BasicBlock::iterator Begin = Store->getParent()->begin();
289  while (I != Begin && GetBasicARCInstKind(&*I) != ARCInstKind::Retain) {
290  Instruction *Inst = &*I;
291 
292  // It is only safe to move the retain to the store if we can prove
293  // conservatively that nothing besides the release can decrement reference
294  // counts in between the retain and the store.
295  if (CanDecrementRefCount(Inst, New, PA) && Inst != Release)
296  return nullptr;
297  --I;
298  }
299  Instruction *Retain = &*I;
301  return nullptr;
302  if (GetArgRCIdentityRoot(Retain) != New)
303  return nullptr;
304  return Retain;
305 }
306 
307 /// Create a call instruction with the correct funclet token. Should be used
308 /// instead of calling CallInst::Create directly.
309 static CallInst *
311  const Twine &NameStr, Instruction *InsertBefore,
312  const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
314  if (!BlockColors.empty()) {
315  const ColorVector &CV = BlockColors.find(InsertBefore->getParent())->second;
316  assert(CV.size() == 1 && "non-unique color for block!");
317  Instruction *EHPad = CV.front()->getFirstNonPHI();
318  if (EHPad->isEHPad())
319  OpBundles.emplace_back("funclet", EHPad);
320  }
321 
322  return CallInst::Create(FTy, Func, Args, OpBundles, NameStr, InsertBefore);
323 }
324 
325 static CallInst *
327  Instruction *InsertBefore,
328  const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
329  return createCallInst(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
330  InsertBefore, BlockColors);
331 }
332 
333 /// Attempt to merge an objc_release with a store, load, and objc_retain to form
334 /// an objc_storeStrong. An objc_storeStrong:
335 ///
336 /// objc_storeStrong(i8** %old_ptr, i8* new_value)
337 ///
338 /// is equivalent to the following IR sequence:
339 ///
340 /// ; Load old value.
341 /// %old_value = load i8** %old_ptr (1)
342 ///
343 /// ; Increment the new value and then release the old value. This must occur
344 /// ; in order in case old_value releases new_value in its destructor causing
345 /// ; us to potentially have a dangling ptr.
346 /// tail call i8* @objc_retain(i8* %new_value) (2)
347 /// tail call void @objc_release(i8* %old_value) (3)
348 ///
349 /// ; Store the new_value into old_ptr
350 /// store i8* %new_value, i8** %old_ptr (4)
351 ///
352 /// The safety of this optimization is based around the following
353 /// considerations:
354 ///
355 /// 1. We are forming the store strong at the store. Thus to perform this
356 /// optimization it must be safe to move the retain, load, and release to
357 /// (4).
358 /// 2. We need to make sure that any re-orderings of (1), (2), (3), (4) are
359 /// safe.
360 void ObjCARCContract::tryToContractReleaseIntoStoreStrong(
361  Instruction *Release, inst_iterator &Iter,
362  const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
363  // See if we are releasing something that we just loaded.
364  auto *Load = dyn_cast<LoadInst>(GetArgRCIdentityRoot(Release));
365  if (!Load || !Load->isSimple())
366  return;
367 
368  // For now, require everything to be in one basic block.
369  BasicBlock *BB = Release->getParent();
370  if (Load->getParent() != BB)
371  return;
372 
373  // First scan down the BB from Load, looking for a store of the RCIdentityRoot
374  // of Load's
375  StoreInst *Store =
377  // If we fail, bail.
378  if (!Store)
379  return;
380 
381  // Then find what new_value's RCIdentity Root is.
382  Value *New = GetRCIdentityRoot(Store->getValueOperand());
383 
384  // Then walk up the BB and look for a retain on New without any intervening
385  // instructions which conservatively might decrement ref counts.
387  findRetainForStoreStrongContraction(New, Store, Release, PA);
388 
389  // If we fail, bail.
390  if (!Retain)
391  return;
392 
393  Changed = true;
394  ++NumStoreStrongs;
395 
396  LLVM_DEBUG(
397  llvm::dbgs() << " Contracting retain, release into objc_storeStrong.\n"
398  << " Old:\n"
399  << " Store: " << *Store << "\n"
400  << " Release: " << *Release << "\n"
401  << " Retain: " << *Retain << "\n"
402  << " Load: " << *Load << "\n");
403 
404  LLVMContext &C = Release->getContext();
406  Type *I8XX = PointerType::getUnqual(I8X);
407 
408  Value *Args[] = { Load->getPointerOperand(), New };
409  if (Args[0]->getType() != I8XX)
410  Args[0] = new BitCastInst(Args[0], I8XX, "", Store);
411  if (Args[1]->getType() != I8X)
412  Args[1] = new BitCastInst(Args[1], I8X, "", Store);
414  CallInst *StoreStrong = createCallInst(Decl, Args, "", Store, BlockColors);
415  StoreStrong->setDoesNotThrow();
416  StoreStrong->setDebugLoc(Store->getDebugLoc());
417 
418  // We can't set the tail flag yet, because we haven't yet determined
419  // whether there are any escaping allocas. Remember this call, so that
420  // we can set the tail flag once we know it's safe.
421  StoreStrongCalls.insert(StoreStrong);
422 
423  LLVM_DEBUG(llvm::dbgs() << " New Store Strong: " << *StoreStrong
424  << "\n");
425 
426  if (&*Iter == Retain) ++Iter;
427  if (&*Iter == Store) ++Iter;
428  Store->eraseFromParent();
429  Release->eraseFromParent();
430  EraseInstruction(Retain);
431  if (Load->use_empty())
432  Load->eraseFromParent();
433 }
434 
435 bool ObjCARCContract::tryToPeepholeInstruction(
436  Function &F, Instruction *Inst, inst_iterator &Iter,
437  bool &TailOkForStoreStrongs,
438  const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
439  // Only these library routines return their argument. In particular,
440  // objc_retainBlock does not necessarily return its argument.
442  switch (Class) {
445  return false;
448  return contractAutorelease(F, Inst, Class);
449  case ARCInstKind::Retain:
450  // Attempt to convert retains to retainrvs if they are next to function
451  // calls.
452  if (!optimizeRetainCall(F, Inst))
453  return false;
454  // If we succeed in our optimization, fall through.
457  case ARCInstKind::ClaimRV: {
458  // If we're compiling for a target which needs a special inline-asm
459  // marker to do the return value optimization, insert it now.
460  if (!RVInstMarker)
461  return false;
462  BasicBlock::iterator BBI = Inst->getIterator();
463  BasicBlock *InstParent = Inst->getParent();
464 
465  // Step up to see if the call immediately precedes the RV call.
466  // If it's an invoke, we have to cross a block boundary. And we have
467  // to carefully dodge no-op instructions.
468  do {
469  if (BBI == InstParent->begin()) {
470  BasicBlock *Pred = InstParent->getSinglePredecessor();
471  if (!Pred)
472  goto decline_rv_optimization;
473  BBI = Pred->getTerminator()->getIterator();
474  break;
475  }
476  --BBI;
477  } while (IsNoopInstruction(&*BBI));
478 
479  if (GetRCIdentityRoot(&*BBI) == GetArgRCIdentityRoot(Inst)) {
480  LLVM_DEBUG(dbgs() << "Adding inline asm marker for the return value "
481  "optimization.\n");
482  Changed = true;
483  InlineAsm *IA =
485  /*isVarArg=*/false),
486  RVInstMarker->getString(),
487  /*Constraints=*/"", /*hasSideEffects=*/true);
488 
489  createCallInst(IA, None, "", Inst, BlockColors);
490  }
491  decline_rv_optimization:
492  return false;
493  }
494  case ARCInstKind::InitWeak: {
495  // objc_initWeak(p, null) => *p = null
496  CallInst *CI = cast<CallInst>(Inst);
497  if (IsNullOrUndef(CI->getArgOperand(1))) {
498  Value *Null = ConstantPointerNull::get(cast<PointerType>(CI->getType()));
499  Changed = true;
500  new StoreInst(Null, CI->getArgOperand(0), CI);
501 
502  LLVM_DEBUG(dbgs() << "OBJCARCContract: Old = " << *CI << "\n"
503  << " New = " << *Null << "\n");
504 
505  CI->replaceAllUsesWith(Null);
506  CI->eraseFromParent();
507  }
508  return true;
509  }
511  // Try to form an objc store strong from our release. If we fail, there is
512  // nothing further to do below, so continue.
513  tryToContractReleaseIntoStoreStrong(Inst, Iter, BlockColors);
514  return true;
515  case ARCInstKind::User:
516  // Be conservative if the function has any alloca instructions.
517  // Technically we only care about escaping alloca instructions,
518  // but this is sufficient to handle some interesting cases.
519  if (isa<AllocaInst>(Inst))
520  TailOkForStoreStrongs = false;
521  return true;
523  // Remove calls to @llvm.objc.clang.arc.use(...).
524  Changed = true;
525  Inst->eraseFromParent();
526  return true;
527  default:
528  return true;
529  }
530 }
531 
532 //===----------------------------------------------------------------------===//
533 // Top Level Driver
534 //===----------------------------------------------------------------------===//
535 
536 bool ObjCARCContract::init(Module &M) {
537  // If nothing in the Module uses ARC, don't do anything.
538  Run = ModuleHasARC(M);
539  if (!Run)
540  return false;
541 
542  EP.init(&M);
543 
544  // Initialize RVInstMarker.
545  const char *MarkerKey = "clang.arc.retainAutoreleasedReturnValueMarker";
546  RVInstMarker = dyn_cast_or_null<MDString>(M.getModuleFlag(MarkerKey));
547 
548  return false;
549 }
550 
551 bool ObjCARCContract::run(Function &F, AAResults *A, DominatorTree *D) {
552  if (!EnableARCOpts)
553  return false;
554 
555  // If nothing in the Module uses ARC, don't do anything.
556  if (!Run)
557  return false;
558 
559  Changed = false;
560  AA = A;
561  DT = D;
562  PA.setAA(A);
563 
565  if (F.hasPersonalityFn() &&
566  isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
567  BlockColors = colorEHFunclets(F);
568 
569  LLVM_DEBUG(llvm::dbgs() << "**** ObjCARC Contract ****\n");
570 
571  // Track whether it's ok to mark objc_storeStrong calls with the "tail"
572  // keyword. Be conservative if the function has variadic arguments.
573  // It seems that functions which "return twice" are also unsafe for the
574  // "tail" argument, because they are setjmp, which could need to
575  // return to an earlier stack state.
576  bool TailOkForStoreStrongs =
577  !F.isVarArg() && !F.callsFunctionThatReturnsTwice();
578 
579  // For ObjC library calls which return their argument, replace uses of the
580  // argument with uses of the call return value, if it dominates the use. This
581  // reduces register pressure.
582  for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E;) {
583  Instruction *Inst = &*I++;
584 
585  LLVM_DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
586 
587  // First try to peephole Inst. If there is nothing further we can do in
588  // terms of undoing objc-arc-expand, process the next inst.
589  if (tryToPeepholeInstruction(F, Inst, I, TailOkForStoreStrongs,
590  BlockColors))
591  continue;
592 
593  // Otherwise, try to undo objc-arc-expand.
594 
595  // Don't use GetArgRCIdentityRoot because we don't want to look through bitcasts
596  // and such; to do the replacement, the argument must have type i8*.
597 
598  // Function for replacing uses of Arg dominated by Inst.
599  auto ReplaceArgUses = [Inst, this](Value *Arg) {
600  // If we're compiling bugpointed code, don't get in trouble.
601  if (!isa<Instruction>(Arg) && !isa<Argument>(Arg))
602  return;
603 
604  // Look through the uses of the pointer.
605  for (Value::use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
606  UI != UE; ) {
607  // Increment UI now, because we may unlink its element.
608  Use &U = *UI++;
609  unsigned OperandNo = U.getOperandNo();
610 
611  // If the call's return value dominates a use of the call's argument
612  // value, rewrite the use to use the return value. We check for
613  // reachability here because an unreachable call is considered to
614  // trivially dominate itself, which would lead us to rewriting its
615  // argument in terms of its return value, which would lead to
616  // infinite loops in GetArgRCIdentityRoot.
617  if (!DT->isReachableFromEntry(U) || !DT->dominates(Inst, U))
618  continue;
619 
620  Changed = true;
621  Instruction *Replacement = Inst;
622  Type *UseTy = U.get()->getType();
623  if (PHINode *PHI = dyn_cast<PHINode>(U.getUser())) {
624  // For PHI nodes, insert the bitcast in the predecessor block.
625  unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo);
626  BasicBlock *IncomingBB = PHI->getIncomingBlock(ValNo);
627  if (Replacement->getType() != UseTy) {
628  // A catchswitch is both a pad and a terminator, meaning a basic
629  // block with a catchswitch has no insertion point. Keep going up
630  // the dominator tree until we find a non-catchswitch.
631  BasicBlock *InsertBB = IncomingBB;
632  while (isa<CatchSwitchInst>(InsertBB->getFirstNonPHI())) {
633  InsertBB = DT->getNode(InsertBB)->getIDom()->getBlock();
634  }
635 
636  assert(DT->dominates(Inst, &InsertBB->back()) &&
637  "Invalid insertion point for bitcast");
638  Replacement =
639  new BitCastInst(Replacement, UseTy, "", &InsertBB->back());
640  }
641 
642  // While we're here, rewrite all edges for this PHI, rather
643  // than just one use at a time, to minimize the number of
644  // bitcasts we emit.
645  for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
646  if (PHI->getIncomingBlock(i) == IncomingBB) {
647  // Keep the UI iterator valid.
648  if (UI != UE &&
649  &PHI->getOperandUse(
651  ++UI;
652  PHI->setIncomingValue(i, Replacement);
653  }
654  } else {
655  if (Replacement->getType() != UseTy)
656  Replacement = new BitCastInst(Replacement, UseTy, "",
657  cast<Instruction>(U.getUser()));
658  U.set(Replacement);
659  }
660  }
661  };
662 
663  Value *Arg = cast<CallInst>(Inst)->getArgOperand(0);
664  Value *OrigArg = Arg;
665 
666  // TODO: Change this to a do-while.
667  for (;;) {
668  ReplaceArgUses(Arg);
669 
670  // If Arg is a no-op casted pointer, strip one level of casts and iterate.
671  if (const BitCastInst *BI = dyn_cast<BitCastInst>(Arg))
672  Arg = BI->getOperand(0);
673  else if (isa<GEPOperator>(Arg) &&
674  cast<GEPOperator>(Arg)->hasAllZeroIndices())
675  Arg = cast<GEPOperator>(Arg)->getPointerOperand();
676  else if (isa<GlobalAlias>(Arg) &&
677  !cast<GlobalAlias>(Arg)->isInterposable())
678  Arg = cast<GlobalAlias>(Arg)->getAliasee();
679  else {
680  // If Arg is a PHI node, get PHIs that are equivalent to it and replace
681  // their uses.
682  if (PHINode *PN = dyn_cast<PHINode>(Arg)) {
683  SmallVector<Value *, 1> PHIList;
684  getEquivalentPHIs(*PN, PHIList);
685  for (Value *PHI : PHIList)
686  ReplaceArgUses(PHI);
687  }
688  break;
689  }
690  }
691 
692  // Replace bitcast users of Arg that are dominated by Inst.
693  SmallVector<BitCastInst *, 2> BitCastUsers;
694 
695  // Add all bitcast users of the function argument first.
696  for (User *U : OrigArg->users())
697  if (auto *BC = dyn_cast<BitCastInst>(U))
698  BitCastUsers.push_back(BC);
699 
700  // Replace the bitcasts with the call return. Iterate until list is empty.
701  while (!BitCastUsers.empty()) {
702  auto *BC = BitCastUsers.pop_back_val();
703  for (User *U : BC->users())
704  if (auto *B = dyn_cast<BitCastInst>(U))
705  BitCastUsers.push_back(B);
706 
707  ReplaceArgUses(BC);
708  }
709  }
710 
711  // If this function has no escaping allocas or suspicious vararg usage,
712  // objc_storeStrong calls can be marked with the "tail" keyword.
713  if (TailOkForStoreStrongs)
714  for (CallInst *CI : StoreStrongCalls)
715  CI->setTailCall();
716  StoreStrongCalls.clear();
717 
718  return Changed;
719 }
720 
721 //===----------------------------------------------------------------------===//
722 // Misc Pass Manager
723 //===----------------------------------------------------------------------===//
724 
726 INITIALIZE_PASS_BEGIN(ObjCARCContractLegacyPass, "objc-arc-contract",
727  "ObjC ARC contraction", false, false)
730 INITIALIZE_PASS_END(ObjCARCContractLegacyPass, "objc-arc-contract",
731  "ObjC ARC contraction", false, false)
732 
733 void ObjCARCContractLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
734  AU.addRequired<AAResultsWrapperPass>();
735  AU.addRequired<DominatorTreeWrapperPass>();
736  AU.setPreservesCFG();
737 }
738 
740  return new ObjCARCContractLegacyPass();
741 }
742 
743 bool ObjCARCContractLegacyPass::doInitialization(Module &M) {
744  return OCARCC.init(M);
745 }
746 
748  auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
749  auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
750  return OCARCC.run(F, AA, DT);
751 }
752 
755  ObjCARCContract OCAC;
756  OCAC.init(*F.getParent());
757 
758  bool Changed = OCAC.run(F, &AM.getResult<AAManager>(F),
760  if (Changed) {
762  PA.preserveSet<CFGAnalyses>();
763  return PA;
764  }
765  return PreservedAnalyses::all();
766 }
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
objc arc contract
uint64_t CallInst * C
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:77
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:856
This file declares special dependency analysis routines used in Objective C ARC Optimizations.
llvm::Instruction * findSingleDependency(DependenceKind Flavor, const Value *Arg, BasicBlock *StartBB, Instruction *StartInst, ProvenanceAnalysis &PA)
Find dependent instructions.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This file contains a class ARCRuntimeEntryPoints for use in creating/managing references to entry poi...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:785
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:67
bool CanUse(const Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
Test whether the given instruction can "use" the given pointer'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's calling convention.
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:868
STATISTIC(NumFunctions, "Total number of functions")
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:249
F(f)
An instruction for reading from memory.
Definition: Instructions.h:174
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:148
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:67
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:296
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1323
static unsigned getOperandNumForIncomingValue(unsigned i)
INITIALIZE_PASS_BEGIN(ObjCARCContractLegacyPass, "objc-arc-contract", "ObjC ARC contraction", false, false) INITIALIZE_PASS_END(ObjCARCContractLegacyPass
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:44
bool IsNullOrUndef(const Value *V)
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
objc_retainAutoreleasedReturnValue
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:246
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.
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:321
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:303
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:523
unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition: Use.cpp:33
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
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:356
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:427
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:212
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
void initializeObjCARCContractLegacyPassPass(PassRegistry &)
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1728
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:264
void set(Value *Val)
Definition: Value.h:848
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
A manager for alias analyses.
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:180
const Instruction & back() const
Definition: BasicBlock.h:310
constexpr double e
Definition: MathExtras.h:58
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
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 address space casts.
Definition: Value.cpp:630
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
static unsigned getIncomingValueNumForOperand(unsigned i)
static wasm::ValType getType(const TargetRegisterClass *RC)
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.
DependenceKind
Defines different dependence kinds among various ARC constructs.
Representation for a specific memory location.
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:73
static void EraseInstruction(Instruction *CI)
Erase the given instruction.
Definition: ObjCARC.h:37
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:442
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:670
void setTailCall(bool IsTc=true)
This file declares a special form of Alias Analysis called Provenance Analysis''.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1116
ARCInstKind
Equivalence classes of instructions in the ARC Model.
objc_unsafeClaimAutoreleasedReturnValue
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:595
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
iterator_range< user_iterator > users()
Definition: Value.h:424
objc arc
Represents analyses that only rely on functions' control flow.
Definition: PassManager.h:116
objc_storeStrong (derived)
bool IsRetain(ARCInstKind Class)
Test if the given class is objc_retain or equivalent.
LLVM_NODISCARD bool isModSet(const ModRefInfo MRI)
static StoreInst * findSafeStoreForStoreStrongContraction(LoadInst *Load, Instruction *Release, ProvenanceAnalysis &PA, AAResults *AA)
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:191
Blocks objc_retainAutoreleaseReturnValue.
#define I(x, y, z)
Definition: MD5.cpp:59
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
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:75
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:280
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:602
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:278
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
This header defines various interfaces for pass management in LLVM.
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
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:195
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.
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:72
bool IsNoopInstruction(const Instruction *I)
const BasicBlock * getParent() const
Definition: Instruction.h:94
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)