75 #define DEBUG_TYPE "objc-arc-opts" 85 if (isa<ConstantData>(Arg))
89 if (
const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
92 if (
GEP->hasAllZeroIndices())
96 cast<CallInst>(Arg)->getArgOperand(0));
158 STATISTIC(NumNoops,
"Number of no-op objc calls eliminated");
159 STATISTIC(NumPartialNoops,
"Number of partially no-op objc calls eliminated");
160 STATISTIC(NumAutoreleases,
"Number of autoreleases converted to releases");
161 STATISTIC(NumRets,
"Number of return value forwarding " 162 "retain+autoreleases eliminated");
163 STATISTIC(NumRRs,
"Number of retain+release paths eliminated");
164 STATISTIC(NumPeeps,
"Number of calls peephole-optimized");
167 "Number of retains before optimization");
169 "Number of releases before optimization");
171 "Number of retains after optimization");
173 "Number of releases after optimization");
182 unsigned TopDownPathCount = 0;
185 unsigned BottomUpPathCount = 0;
208 using top_down_ptr_iterator = decltype(PerPtrTopDown)::iterator;
209 using const_top_down_ptr_iterator = decltype(PerPtrTopDown)::const_iterator;
211 top_down_ptr_iterator top_down_ptr_begin() {
return PerPtrTopDown.
begin(); }
212 top_down_ptr_iterator top_down_ptr_end() {
return PerPtrTopDown.
end(); }
213 const_top_down_ptr_iterator top_down_ptr_begin()
const {
214 return PerPtrTopDown.
begin();
216 const_top_down_ptr_iterator top_down_ptr_end()
const {
217 return PerPtrTopDown.
end();
219 bool hasTopDownPtrs()
const {
220 return !PerPtrTopDown.
empty();
223 using bottom_up_ptr_iterator = decltype(PerPtrBottomUp)::iterator;
224 using const_bottom_up_ptr_iterator =
225 decltype(PerPtrBottomUp)::const_iterator;
227 bottom_up_ptr_iterator bottom_up_ptr_begin() {
228 return PerPtrBottomUp.
begin();
230 bottom_up_ptr_iterator bottom_up_ptr_end() {
return PerPtrBottomUp.
end(); }
231 const_bottom_up_ptr_iterator bottom_up_ptr_begin()
const {
232 return PerPtrBottomUp.
begin();
234 const_bottom_up_ptr_iterator bottom_up_ptr_end()
const {
235 return PerPtrBottomUp.
end();
237 bool hasBottomUpPtrs()
const {
238 return !PerPtrBottomUp.
empty();
243 void SetAsEntry() { TopDownPathCount = 1; }
247 void SetAsExit() { BottomUpPathCount = 1; }
253 return PerPtrTopDown[
Arg];
260 return PerPtrBottomUp[
Arg];
265 bottom_up_ptr_iterator findPtrBottomUpState(
const Value *Arg) {
266 return PerPtrBottomUp.
find(Arg);
269 void clearBottomUpPointers() {
270 PerPtrBottomUp.
clear();
273 void clearTopDownPointers() {
274 PerPtrTopDown.
clear();
277 void InitFromPred(
const BBState &
Other);
278 void InitFromSucc(
const BBState &Other);
279 void MergePred(
const BBState &Other);
280 void MergeSucc(
const BBState &Other);
288 bool GetAllPathCountWithOverflow(
unsigned &PathCount)
const {
289 if (TopDownPathCount == OverflowOccurredValue ||
290 BottomUpPathCount == OverflowOccurredValue)
292 unsigned long long Product =
293 (
unsigned long long)TopDownPathCount*BottomUpPathCount;
296 return (Product >> 32) ||
297 ((PathCount = Product) == OverflowOccurredValue);
304 edge_iterator
pred_end()
const {
return Preds.
end(); }
306 edge_iterator
succ_end()
const {
return Succs.
end(); }
311 bool isExit()
const {
return Succs.
empty(); }
316 const unsigned BBState::OverflowOccurredValue = 0xffffffff;
325 void BBState::InitFromPred(
const BBState &
Other) {
326 PerPtrTopDown = Other.PerPtrTopDown;
327 TopDownPathCount = Other.TopDownPathCount;
330 void BBState::InitFromSucc(
const BBState &Other) {
331 PerPtrBottomUp = Other.PerPtrBottomUp;
332 BottomUpPathCount = Other.BottomUpPathCount;
337 void BBState::MergePred(
const BBState &Other) {
338 if (TopDownPathCount == OverflowOccurredValue)
343 TopDownPathCount += Other.TopDownPathCount;
348 if (TopDownPathCount == OverflowOccurredValue) {
349 clearTopDownPointers();
355 if (TopDownPathCount < Other.TopDownPathCount) {
356 TopDownPathCount = OverflowOccurredValue;
357 clearTopDownPointers();
364 for (
auto MI = Other.top_down_ptr_begin(), ME = Other.top_down_ptr_end();
366 auto Pair = PerPtrTopDown.
insert(*
MI);
373 for (
auto MI = top_down_ptr_begin(), ME = top_down_ptr_end();
MI != ME; ++
MI)
374 if (Other.PerPtrTopDown.find(
MI->first) == Other.PerPtrTopDown.end())
380 void BBState::MergeSucc(
const BBState &Other) {
381 if (BottomUpPathCount == OverflowOccurredValue)
386 BottomUpPathCount += Other.BottomUpPathCount;
391 if (BottomUpPathCount == OverflowOccurredValue) {
392 clearBottomUpPointers();
398 if (BottomUpPathCount < Other.BottomUpPathCount) {
399 BottomUpPathCount = OverflowOccurredValue;
400 clearBottomUpPointers();
407 for (
auto MI = Other.bottom_up_ptr_begin(), ME = Other.bottom_up_ptr_end();
409 auto Pair = PerPtrBottomUp.
insert(*
MI);
416 for (
auto MI = bottom_up_ptr_begin(), ME = bottom_up_ptr_end();
MI != ME;
418 if (Other.PerPtrBottomUp.find(
MI->first) == Other.PerPtrBottomUp.end())
424 OS <<
" TopDown State:\n";
425 if (!BBInfo.hasTopDownPtrs()) {
428 for (
auto I = BBInfo.top_down_ptr_begin(),
E = BBInfo.top_down_ptr_end();
431 OS <<
" Ptr: " << *
I->first
432 <<
"\n KnownSafe: " << (P.
IsKnownSafe()?
"true":
"false")
433 <<
"\n ImpreciseRelease: " 435 <<
" HasCFGHazards: " 437 <<
" KnownPositive: " 444 OS <<
" BottomUp State:\n";
445 if (!BBInfo.hasBottomUpPtrs()) {
448 for (
auto I = BBInfo.bottom_up_ptr_begin(),
E = BBInfo.bottom_up_ptr_end();
451 OS <<
" Ptr: " << *
I->first
452 <<
"\n KnownSafe: " << (P.
IsKnownSafe()?
"true":
"false")
453 <<
"\n ImpreciseRelease: " 455 <<
" HasCFGHazards: " 457 <<
" KnownPositive: " 486 unsigned UsedInThisFunction;
491 void OptimizeIndividualCalls(
Function &F);
495 BBState &MyStates)
const;
524 Value *Arg,
bool KnownSafe,
525 bool &AnyPairsCompletelyEliminated);
531 void OptimizeWeakCalls(
Function &F);
533 bool OptimizeSequences(
Function &F);
538 void GatherStatistics(
Function &F,
bool AfterOptimization =
false);
542 bool doInitialization(
Module &M)
override;
544 void releaseMemory()
override;
559 "objc-arc",
"ObjC ARC optimization",
false,
false)
562 "objc-arc",
"ObjC ARC optimization",
false,
false)
565 return new ObjCARCOpt();
593 if (II->getNormalDest() == RetainRVParent) {
616 LLVM_DEBUG(
dbgs() <<
"Erasing autoreleaseRV,retainRV pair: " << *I <<
"\n" 617 <<
"Erasing " << *RetainRV <<
"\n");
629 LLVM_DEBUG(
dbgs() <<
"Transforming objc_retainAutoreleasedReturnValue => " 630 "objc_retain since the operand is not a return value.\n" 632 << *RetainRV <<
"\n");
635 cast<CallInst>(
RetainRV)->setCalledFunction(NewDecl);
644 void ObjCARCOpt::OptimizeAutoreleaseRVCall(
Function &F,
652 if (isa<ConstantData>(Ptr))
659 if (
const PHINode *PN = dyn_cast<PHINode>(Ptr))
667 if (isa<BitCastInst>(U))
670 }
while (!Users.
empty());
676 dbgs() <<
"Transforming objc_autoreleaseReturnValue => " 677 "objc_autorelease since its operand is not used as a return " 680 << *AutoreleaseRV <<
"\n");
704 if (!BlockColors.
empty()) {
706 assert(CV.
size() == 1 &&
"non-unique color for block!");
708 if (EHPad->isEHPad())
718 void ObjCARCOpt::OptimizeIndividualCalls(
Function &F) {
719 LLVM_DEBUG(
dbgs() <<
"\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
721 UsedInThisFunction = 0;
734 LLVM_DEBUG(
dbgs() <<
"Visiting: Class: " << Class <<
"; " << *Inst <<
"\n");
760 CallInst *CI = cast<CallInst>(Inst);
769 dbgs() <<
"A null pointer-to-weak-pointer is undefined behavior." 771 << *CI <<
"\nNew = " << *NewValue <<
"\n");
780 CallInst *CI = cast<CallInst>(Inst);
791 dbgs() <<
"A null pointer-to-weak-pointer is undefined behavior." 793 << *CI <<
"\nNew = " << *NewValue <<
"\n");
802 if (OptimizeRetainRVCall(F, Inst))
806 OptimizeAutoreleaseRVCall(F, Inst, Class);
829 dbgs() <<
"Replacing autorelease{,RV}(x) with objc_release(x) " 830 "since x is otherwise unused.\nOld: " 831 << *Call <<
"\nNew: " << *NewCall <<
"\n");
844 dbgs() <<
"Adding tail keyword to function since it can never be " 845 "passed stack args: " 847 cast<CallInst>(Inst)->setTailCall();
854 LLVM_DEBUG(
dbgs() <<
"Removing tail keyword from function: " << *Inst
856 cast<CallInst>(Inst)->setTailCall(
false);
862 LLVM_DEBUG(
dbgs() <<
"Found no throw class. Setting nounwind on: " 868 UsedInThisFunction |= 1 <<
unsigned(Class);
878 LLVM_DEBUG(
dbgs() <<
"ARC calls with null are no-ops. Erasing: " << *Inst
886 UsedInThisFunction |= 1 <<
unsigned(Class);
899 Worklist.
push_back(std::make_pair(Inst, Arg));
901 std::pair<Instruction *, const Value *> Pair = Worklist.
pop_back_val();
910 bool HasNull =
false;
911 bool HasCriticalEdges =
false;
918 .getNumSuccessors() != 1) {
919 HasCriticalEdges =
true;
924 if (!HasCriticalEdges && HasNull) {
940 DependingInstructions, Visited, PA);
946 DependingInstructions, Visited, PA);
960 if (DependingInstructions.
size() == 1 &&
961 *DependingInstructions.
begin() == PN) {
965 CallInst *CInst = cast<CallInst>(Inst);
973 CallInst *Clone = cast<CallInst>(CloneCallInstForBB(
974 *CInst, *InsertPos->
getParent(), BlockColors));
982 "And inserting clone at " 983 << *InsertPos <<
"\n");
984 Worklist.
push_back(std::make_pair(Clone, Incoming));
993 }
while (!Worklist.
empty());
1000 const bool SuccSRRIKnownSafe,
1002 bool &SomeSuccHasSame,
1003 bool &AllSuccsHaveSame,
1004 bool &NotAllSeqEqualButKnownSafe,
1005 bool &ShouldContinue) {
1013 ShouldContinue =
true;
1017 SomeSuccHasSame =
true;
1023 AllSuccsHaveSame =
false;
1025 NotAllSeqEqualButKnownSafe =
true;
1038 const bool SuccSRRIKnownSafe,
1040 bool &SomeSuccHasSame,
1041 bool &AllSuccsHaveSame,
1042 bool &NotAllSeqEqualButKnownSafe) {
1045 SomeSuccHasSame =
true;
1052 AllSuccsHaveSame =
false;
1054 NotAllSeqEqualButKnownSafe =
true;
1067 ObjCARCOpt::CheckForCFGHazards(
const BasicBlock *BB,
1069 BBState &MyStates)
const {
1072 for (
auto I = MyStates.top_down_ptr_begin(),
E = MyStates.top_down_ptr_end();
1075 const Sequence Seq =
I->second.GetSeq();
1084 "Unknown top down sequence state.");
1088 bool SomeSuccHasSame =
false;
1089 bool AllSuccsHaveSame =
true;
1090 bool NotAllSeqEqualButKnownSafe =
false;
1094 for (;
SI != SE; ++
SI) {
1101 const Sequence SuccSSeq = SuccS.GetSeq();
1108 if (SuccSSeq ==
S_None) {
1115 const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe();
1121 bool ShouldContinue =
false;
1123 AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
1131 SomeSuccHasSame, AllSuccsHaveSame,
1132 NotAllSeqEqualButKnownSafe);
1146 if (SomeSuccHasSame && !AllSuccsHaveSame) {
1148 }
else if (NotAllSeqEqualButKnownSafe) {
1158 bool ObjCARCOpt::VisitInstructionBottomUp(
1160 BBState &MyStates) {
1161 bool NestingDetected =
false;
1198 MyStates.clearBottomUpPointers();
1199 return NestingDetected;
1203 return NestingDetected;
1210 for (
auto MI = MyStates.bottom_up_ptr_begin(),
1211 ME = MyStates.bottom_up_ptr_end();
1224 return NestingDetected;
1227 bool ObjCARCOpt::VisitBottomUp(
BasicBlock *BB,
1232 bool NestingDetected =
false;
1233 BBState &MyStates = BBStates[BB];
1237 BBState::edge_iterator
SI(MyStates.succ_begin()),
1238 SE(MyStates.succ_end());
1243 MyStates.InitFromSucc(I->second);
1245 for (;
SI != SE; ++
SI) {
1247 I = BBStates.
find(Succ);
1249 MyStates.MergeSucc(I->second);
1254 << BBStates[BB] <<
"\n" 1255 <<
"Performing Dataflow:\n");
1262 if (isa<InvokeInst>(Inst))
1267 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
1273 for (BBState::edge_iterator PI(MyStates.pred_begin()),
1274 PE(MyStates.pred_end()); PI != PE; ++PI) {
1277 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
1280 LLVM_DEBUG(
dbgs() <<
"\nFinal State:\n" << BBStates[BB] <<
"\n");
1282 return NestingDetected;
1286 ObjCARCOpt::VisitInstructionTopDown(
Instruction *Inst,
1288 BBState &MyStates) {
1289 bool NestingDetected =
false;
1327 MyStates.clearTopDownPointers();
1339 for (
auto MI = MyStates.top_down_ptr_begin(),
1340 ME = MyStates.top_down_ptr_end();
1352 return NestingDetected;
1360 bool NestingDetected =
false;
1361 BBState &MyStates = BBStates[BB];
1365 BBState::edge_iterator PI(MyStates.pred_begin()),
1366 PE(MyStates.pred_end());
1371 MyStates.InitFromPred(I->second);
1373 for (; PI != PE; ++PI) {
1375 I = BBStates.
find(Pred);
1377 MyStates.MergePred(I->second);
1382 << BBStates[BB] <<
"\n" 1383 <<
"Performing Dataflow:\n");
1389 NestingDetected |= VisitInstructionTopDown(&Inst, Releases, MyStates);
1392 LLVM_DEBUG(
dbgs() <<
"\nState Before Checking for CFG Hazards:\n" 1393 << BBStates[BB] <<
"\n\n");
1394 CheckForCFGHazards(BB, BBStates, MyStates);
1396 return NestingDetected;
1403 unsigned NoObjCARCExceptionsMDKind,
1415 BBState &MyStates = BBStates[EntryBB];
1416 MyStates.SetAsEntry();
1427 while (SuccStack.
back().second != SE) {
1429 if (Visited.
insert(SuccBB).second) {
1432 BBStates[CurrBB].addSucc(SuccBB);
1433 BBState &SuccStates = BBStates[SuccBB];
1434 SuccStates.addPred(CurrBB);
1439 if (!OnStack.
count(SuccBB)) {
1440 BBStates[CurrBB].addSucc(SuccBB);
1441 BBStates[SuccBB].addPred(CurrBB);
1444 OnStack.
erase(CurrBB);
1447 }
while (!SuccStack.
empty());
1456 BBState &MyStates = BBStates[&ExitBB];
1457 if (!MyStates.isExit())
1460 MyStates.SetAsExit();
1462 PredStack.
push_back(std::make_pair(&ExitBB, MyStates.pred_begin()));
1464 while (!PredStack.
empty()) {
1465 reverse_dfs_next_succ:
1466 BBState::edge_iterator PE = BBStates[PredStack.
back().first].pred_end();
1467 while (PredStack.
back().second != PE) {
1469 if (Visited.
insert(BB).second) {
1471 goto reverse_dfs_next_succ;
1480 bool ObjCARCOpt::Visit(
Function &F,
1496 bool BottomUpNestingDetected =
false;
1498 BottomUpNestingDetected |= VisitBottomUp(BB, BBStates, Retains);
1501 bool TopDownNestingDetected =
false;
1503 TopDownNestingDetected |= VisitTopDown(BB, BBStates, Releases);
1505 return TopDownNestingDetected && BottomUpNestingDetected;
1522 Value *MyArg = ArgTy == ParamTy ?
Arg :
1531 "At insertion point: " 1532 << *InsertPt <<
"\n");
1535 Value *MyArg = ArgTy == ParamTy ?
Arg :
1548 "At insertion point: " 1549 << *InsertPt <<
"\n");
1554 Retains.
blot(OrigRetain);
1556 LLVM_DEBUG(
dbgs() <<
"Deleting retain: " << *OrigRetain <<
"\n");
1559 Releases.
erase(OrigRelease);
1561 LLVM_DEBUG(
dbgs() <<
"Deleting release: " << *OrigRelease <<
"\n");
1565 bool ObjCARCOpt::PairUpRetainsAndReleases(
1571 RRInfo &ReleasesToMove,
Value *Arg,
bool KnownSafe,
1572 bool &AnyPairsCompletelyEliminated) {
1576 bool KnownSafeTD =
true, KnownSafeBU =
true;
1577 bool CFGHazardAfflicted =
false;
1583 unsigned OldDelta = 0;
1584 unsigned NewDelta = 0;
1585 unsigned OldCount = 0;
1586 unsigned NewCount = 0;
1587 bool FirstRelease =
true;
1591 auto It = Retains.
find(NewRetain);
1593 const RRInfo &NewRetainRRI = It->second;
1594 KnownSafeTD &= NewRetainRRI.KnownSafe;
1595 CFGHazardAfflicted |= NewRetainRRI.CFGHazardAfflicted;
1596 for (
Instruction *NewRetainRelease : NewRetainRRI.Calls) {
1597 auto Jt = Releases.
find(NewRetainRelease);
1598 if (Jt == Releases.
end())
1600 const RRInfo &NewRetainReleaseRRI = Jt->second;
1607 if (!NewRetainReleaseRRI.
Calls.count(NewRetain))
1610 if (ReleasesToMove.
Calls.insert(NewRetainRelease).second) {
1613 const BBState &NRRBBState = BBStates[NewRetainRelease->
getParent()];
1614 unsigned PathCount = BBState::OverflowOccurredValue;
1615 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
1617 assert(PathCount != BBState::OverflowOccurredValue &&
1618 "PathCount at this point can not be " 1619 "OverflowOccurredValue.");
1620 OldDelta -= PathCount;
1628 FirstRelease =
false;
1644 const BBState &RIPBBState = BBStates[RIP->
getParent()];
1645 PathCount = BBState::OverflowOccurredValue;
1646 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
1648 assert(PathCount != BBState::OverflowOccurredValue &&
1649 "PathCount at this point can not be " 1650 "OverflowOccurredValue.");
1651 NewDelta -= PathCount;
1654 NewReleases.
push_back(NewRetainRelease);
1659 if (NewReleases.
empty())
break;
1663 auto It = Releases.
find(NewRelease);
1665 const RRInfo &NewReleaseRRI = It->second;
1667 CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
1668 for (
Instruction *NewReleaseRetain : NewReleaseRRI.Calls) {
1669 auto Jt = Retains.
find(NewReleaseRetain);
1670 if (Jt == Retains.
end())
1672 const RRInfo &NewReleaseRetainRRI = Jt->second;
1679 if (!NewReleaseRetainRRI.
Calls.count(NewRelease))
1682 if (RetainsToMove.
Calls.insert(NewReleaseRetain).second) {
1685 const BBState &NRRBBState = BBStates[NewReleaseRetain->
getParent()];
1686 unsigned PathCount = BBState::OverflowOccurredValue;
1687 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
1689 assert(PathCount != BBState::OverflowOccurredValue &&
1690 "PathCount at this point can not be " 1691 "OverflowOccurredValue.");
1692 OldDelta += PathCount;
1693 OldCount += PathCount;
1701 const BBState &RIPBBState = BBStates[RIP->
getParent()];
1703 PathCount = BBState::OverflowOccurredValue;
1704 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
1706 assert(PathCount != BBState::OverflowOccurredValue &&
1707 "PathCount at this point can not be " 1708 "OverflowOccurredValue.");
1709 NewDelta += PathCount;
1710 NewCount += PathCount;
1713 NewRetains.push_back(NewReleaseRetain);
1717 if (NewRetains.empty())
break;
1721 bool UnconditionallySafe = KnownSafeTD && KnownSafeBU;
1722 if (UnconditionallySafe) {
1737 const bool WillPerformCodeMotion =
1740 if (CFGHazardAfflicted && WillPerformCodeMotion)
1753 assert(OldCount != 0 &&
"Unreachable code?");
1754 NumRRs += OldCount - NewCount;
1756 AnyPairsCompletelyEliminated = NewCount == 0;
1764 bool ObjCARCOpt::PerformCodePlacement(
1768 LLVM_DEBUG(
dbgs() <<
"\n== ObjCARCOpt::PerformCodePlacement ==\n");
1770 bool AnyPairsCompletelyEliminated =
false;
1789 bool KnownSafe = isa<Constant>(
Arg) || isa<AllocaInst>(Arg);
1793 if (
const LoadInst *LI = dyn_cast<LoadInst>(Arg))
1795 dyn_cast<GlobalVariable>(
1797 if (GV->isConstant())
1802 RRInfo RetainsToMove, ReleasesToMove;
1804 bool PerformMoveCalls = PairUpRetainsAndReleases(
1805 BBStates, Retains, Releases, M, Retain, DeadInsts,
1806 RetainsToMove, ReleasesToMove, Arg, KnownSafe,
1807 AnyPairsCompletelyEliminated);
1809 if (PerformMoveCalls) {
1812 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
1813 Retains, Releases, DeadInsts, M);
1819 while (!DeadInsts.
empty())
1822 return AnyPairsCompletelyEliminated;
1826 void ObjCARCOpt::OptimizeWeakCalls(
Function &F) {
1859 switch (EarlierClass) {
1865 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
1867 Value *EarlierArg = EarlierCall->getArgOperand(0);
1868 switch (PA.getAA()->alias(Arg, EarlierArg)) {
1894 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
1896 Value *EarlierArg = EarlierCall->getArgOperand(0);
1897 switch (PA.getAA()->alias(Arg, EarlierArg)) {
1947 if (
AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
1949 const Instruction *UserInst = cast<Instruction>(U);
1960 for (
auto UI = Alloca->user_begin(), UE = Alloca->user_end(); UI != UE;) {
1961 CallInst *UserInst = cast<CallInst>(*UI++);
1976 Alloca->eraseFromParent();
1984 bool ObjCARCOpt::OptimizeSequences(
Function &F) {
1997 bool NestingDetected = Visit(F, BBStates, Retains, Releases);
2000 bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
2004 return AnyPairsCompletelyEliminated && NestingDetected;
2016 DepInsts, Visited, PA);
2017 if (DepInsts.size() != 1)
2020 auto *
Call = dyn_cast_or_null<CallInst>(*DepInsts.begin());
2041 BB, Autorelease, DepInsts, Visited, PA);
2042 if (DepInsts.
size() != 1)
2045 auto *Retain = dyn_cast_or_null<CallInst>(*DepInsts.
begin());
2066 BB, Ret, DepInsts, V, PA);
2067 if (DepInsts.
size() != 1)
2090 void ObjCARCOpt::OptimizeReturns(
Function &F) {
2111 Arg, &BB, Ret, DependingInstructions, Visited, PA);
2112 DependingInstructions.
clear();
2119 Arg, Autorelease->getParent(),
Autorelease, DependingInstructions,
2121 DependingInstructions.clear();
2130 DependingInstructions,
2132 DependingInstructions.clear();
2135 if (!HasSafePathToCall)
2141 LLVM_DEBUG(
dbgs() <<
"Erasing: " << *Retain <<
"\nErasing: " << *Autorelease
2150 ObjCARCOpt::GatherStatistics(
Function &F,
bool AfterOptimization) {
2152 AfterOptimization ? NumRetainsAfterOpt : NumRetainsBeforeOpt;
2154 AfterOptimization ? NumReleasesAfterOpt : NumReleasesBeforeOpt;
2172 bool ObjCARCOpt::doInitialization(
Module &M) {
2184 MDKindCache.init(&M);
2206 PA.setAA(&getAnalysis<AAResultsWrapperPass>().getAAResults());
2210 GatherStatistics(F,
false);
2219 OptimizeIndividualCalls(F);
2229 OptimizeWeakCalls(F);
2238 while (OptimizeSequences(F)) {}
2248 GatherStatistics(F,
true);
2257 void ObjCARCOpt::releaseMemory() {
Pass interface - Implemented by all 'passes'.
Return a value (possibly void), from a function.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void push_back(const T &Elt)
This file declares special dependency analysis routines used in Objective C ARC Optimizations.
ARCInstKind GetARCInstKind(const Value *V)
Map V to its ARCInstKind equivalence class.
objc_destroyWeak (derived)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
SmallPtrSet< Instruction *, 2 > Calls
For a top-down sequence, the set of objc_retains or objc_retainBlocks.
This file contains a class ARCRuntimeEntryPoints for use in creating/managing references to entry poi...
Compute iterated dominance frontiers using a linear time algorithm.
objc_loadWeakRetained (primitive)
typename VectorTy::const_iterator const_iterator
could call objc_release and/or "use" pointers
A Module instance is used to store all the information related to an LLVM module. ...
bool IsTrackingImpreciseReleases() const
bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
unsigned getNumOperandBundles() const
Return the number of operand bundles associated with this User.
This class represents a function call, abstracting a target machine's calling convention.
objc_retainedObject, etc.
Value * GetArgRCIdentityRoot(Value *Inst)
Assuming the given instruction is one of the special calls such as objc_retain or objc_release...
The two locations do not alias at all.
OperandBundleUse getOperandBundleAt(unsigned Index) const
Return the operand bundle at a specific index.
LLVMContext & getContext() const
All values hold a context through their type.
bool MatchWithRelease(ARCMDKindCache &Cache, Instruction *Release)
Return true if this set of retains can be paired with the given release.
STATISTIC(NumFunctions, "Total number of functions")
static CallInst * Create(Value *Func, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > Bundles=None, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
void initializeObjCARCOptPass(PassRegistry &)
An instruction for reading from memory.
TerminatorInst::SuccIterator< TerminatorInst *, BasicBlock > succ_iterator
iv Induction Variable Users
SmallPtrSet< Instruction *, 2 > ReverseInsertPts
The set of optimal insert positions for moving calls in the opposite sequence.
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
bool IsNoopOnNull(ARCInstKind Class)
Test if the given class represents instructions which do nothing if passed a null pointer...
bool IsForwarding(ARCInstKind Class)
Test if the given class represents instructions which return their argument verbatim.
bool IsObjCIdentifiedObject(const Value *V)
Return true if this value refers to a distinct and identifiable object.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
iterator begin()
Instruction iterator methods.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
bool HasKnownPositiveRefCount() const
objc_autoreleaseReturnValue
inst_iterator inst_begin(Function *F)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
InstrTy * getInstruction() const
This class summarizes several per-pointer runtime properties which are propagated through the flow gr...
bool IsNullOrUndef(const Value *V)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch, catchpad/ret, and cleanuppad/ret.
bool IsTailCallRelease
True of the objc_release calls are all marked with the "tail" keyword.
bool InitBottomUp(ARCMDKindCache &Cache, Instruction *I)
(Re-)Initialize this bottom up pointer returning true if we detected a pointer with nested releases...
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
objc_retainAutoreleasedReturnValue
Type * getType() const
All values are typed, get the type of this value.
static bool setDoesNotThrow(Function &F)
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
This class represents a no-op cast from one type to another.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
void HandlePotentialUse(BasicBlock *BB, Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
An instruction for storing to memory.
bool hasPersonalityFn() const
Check whether this function has a personality function.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
bool IsAlwaysTail(ARCInstKind Class)
Test if the given class represents instructions which are always safe to mark with the "tail" keyword...
BBIty & getBasicBlockIterator()
bool ModuleHasARC(const Module &M)
Test if the given module looks interesting to run ARC optimization on.
Value * getOperand(unsigned i) const
Unidirectional information about either a retain-decrement-use-release sequence or release-use-decrem...
Interval::succ_iterator succ_end(Interval *I)
raw_ostream & operator<<(raw_ostream &OS, const ARCInstKind Class)
iterator find(const_arg_type_t< KeyT > Val)
const BasicBlock & getEntryBlock() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
iterator find(const KeyT &Key)
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
static bool runOnFunction(Function &F, bool PostInlining)
bool erase(const KeyT &Val)
Type * getReturnType() const
Returns the type of the ret val.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Subclasses of this class are all able to terminate a basic block.
void HandlePotentialUse(Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
BIty & getInstructionIterator()
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &InsertPair)
like S_Release, but code motion is stopped.
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.
const Instruction & back() const
void setCalledFunction(Value *Fn)
Set the function called.
FunctionPass class - This class is used to implement most global optimizations.
void setArgOperand(unsigned i, Value *v)
Interval::pred_iterator pred_end(Interval *I)
static const Value * FindSingleUseIdentifiedObject(const Value *Arg)
This is similar to GetRCIdentityRoot but it stops as soon as it finds a value with multiple uses...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
self_iterator getIterator()
A cache of MDKinds used by various ARC optimizations.
static CallInst * FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB, ReturnInst *Ret, SmallPtrSetImpl< Instruction *> &DepInsts, SmallPtrSetImpl< const BasicBlock *> &V, ProvenanceAnalysis &PA)
Look for an ``autorelease'' instruction dependent on Arg such that there are no instructions dependen...
bool MatchWithRetain()
Return true if this set of releases can be paired with a release.
static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq, const bool SuccSRRIKnownSafe, TopDownPtrState &S, bool &SomeSuccHasSame, bool &AllSuccsHaveSame, bool &NotAllSeqEqualButKnownSafe)
If we have a Top Down pointer in the S_CanRelease state, make sure that there are no CFG hazards by c...
void setTailCall(bool isTC=true)
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
This file defines common analysis utilities used by the ObjC ARC Optimizer.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
anything that is inert from an ARC perspective.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This file declares a simple ARC-aware AliasAnalysis using special knowledge of Objective C to enhance...
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
objc_release(x), !clang.imprecise_release.
bool IsCFGHazardAfflicted() const
The two locations may or may not alias. This is the least precise result.
The two locations precisely alias each other.
static void EraseInstruction(Instruction *CI)
Erase the given instruction.
Iterator for intrusive lists based on ilist_node.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the generic address space (address sp...
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
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...
ARCInstKind
Equivalence classes of instructions in the ARC Model.
An associative container with fast insertion-order (deterministic) iteration over its elements...
bool IsNoThrow(ARCInstKind Class)
Test if the given class represents instructions which are always safe to mark with the nounwind attri...
objc_unsafeClaimAutoreleasedReturnValue
bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
LLVM_NODISCARD T pop_back_val()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
unsigned getNumIncomingValues() const
Return the number of incoming edges.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
MDNode * ReleaseMetadata
If the Calls are objc_release calls and they all have a clang.imprecise_release tag, this is the metadata tag.
Pass * createObjCARCOptPass()
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()
#define LLVM_ATTRIBUTE_UNUSED
amdgpu Simplify well known AMD library false Value Value * Arg
void ClearSequenceProgress()
bool IsRetain(ARCInstKind Class)
Test if the given class is objc_retain or equivalent.
static bool HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain, SmallPtrSetImpl< Instruction *> &DepInsts, SmallPtrSetImpl< const BasicBlock *> &Visited, ProvenanceAnalysis &PA)
Check if there is a dependent call earlier that does not have anything in between the Retain and the ...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
const RRInfo & GetRRInfo() const
void emplace_back(ArgTypes &&... Args)
LLVM_NODISCARD bool empty() const
StringRef getName() const
Return a constant reference to the value's name.
Establish a view to a call site for examination.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
objc_storeWeak (primitive)
Sequence
A sequence of states that a pointer may go through in which an objc_retain and objc_release are actua...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static void CheckForUseCFGHazard(const Sequence SuccSSeq, const bool SuccSRRIKnownSafe, TopDownPtrState &S, bool &SomeSuccHasSame, bool &AllSuccsHaveSame, bool &NotAllSeqEqualButKnownSafe, bool &ShouldContinue)
If we have a top down pointer in the S_Use state, make sure that there are no CFG hazards by checking...
Declarations for ObjC runtime functions and constants.
void SetCFGHazardAfflicted(const bool NewValue)
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
LLVM_NODISCARD bool empty() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
INITIALIZE_PASS_BEGIN(ObjCARCOpt, "objc-arc", "ObjC ARC optimization", false, false) INITIALIZE_PASS_END(ObjCARCOpt
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
Constant * getPersonalityFn() const
Get the personality function associated with this function.
foo(x) – x could possibly see a ref count decrement.
Legacy wrapper pass to provide the ObjCARCAAResult object.
static void ComputePostOrders(Function &F, SmallVectorImpl< BasicBlock *> &PostOrder, SmallVectorImpl< BasicBlock *> &ReverseCFGPostOrder, unsigned NoObjCARCExceptionsMDKind, DenseMap< const BasicBlock *, BBState > &BBStates)
static const unsigned OverflowOccurredValue
bool KnownSafe
After an objc_retain, the reference count of the referenced object is known to be positive...
This class implements an extremely fast bulk output stream that can only output to a stream...
This is similar to BasicAliasAnalysis, and it uses many of the same techniques, except it uses specia...
bool hasOneUse() const
Return true if there is exactly one user of this value.
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)
static CallInst * FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB, Instruction *Autorelease, SmallPtrSetImpl< Instruction *> &DepInsts, SmallPtrSetImpl< const BasicBlock *> &Visited, ProvenanceAnalysis &PA)
Find a dependent retain that precedes the given autorelease for which there is nothing in between the...
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
The two locations alias, but only due to a partial overlap.
bool InitTopDown(ARCInstKind Kind, Instruction *I)
(Re-)Initialize this bottom up pointer returning true if we detected a pointer with nested releases...
DenseMap< BasicBlock *, ColorVector > colorEHFunclets(Function &F)
If an EH funclet personality is in use (see isFuncletEHPersonality), this will recompute which blocks...
static IntegerType * getInt8Ty(LLVMContext &C)
bool AreStatisticsEnabled()
Check if statistics are enabled.
bool IsNeverTail(ARCInstKind Class)
Test if the given class represents instructions which are never safe to mark with the "tail" keyword...
void blot(const KeyT &Key)
This is similar to erase, but instead of removing the element from the vector, it just zeros out the ...
void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList)
Return the list of PHI nodes that are equivalent to PN.
bool IsNoopInstruction(const Instruction *I)
const BasicBlock * getParent() const
an instruction to allocate memory on the stack
bool IsAutorelease(ARCInstKind Class)
Test if the given class is objc_autorelease or equivalent.