@@ -1125,7 +1125,7 @@ void ObjCARCOpt::OptimizeIndividualCallImpl(
1125
1125
if (!HasNull)
1126
1126
continue ;
1127
1127
1128
- SmallPtrSet< Instruction *, 4 > DependingInstructions ;
1128
+ Instruction *DepInst = nullptr ;
1129
1129
1130
1130
// Check that there is nothing that cares about the reference
1131
1131
// count between the call and the phi.
@@ -1137,13 +1137,13 @@ void ObjCARCOpt::OptimizeIndividualCallImpl(
1137
1137
case ARCInstKind::Release:
1138
1138
// These can't be moved across things that care about the retain
1139
1139
// count.
1140
- FindDependencies (NeedsPositiveRetainCount, Arg, Inst-> getParent (), Inst ,
1141
- DependingInstructions , PA);
1140
+ DepInst = findSingleDependency (NeedsPositiveRetainCount, Arg ,
1141
+ Inst-> getParent (), Inst , PA);
1142
1142
break ;
1143
1143
case ARCInstKind::Autorelease:
1144
1144
// These can't be moved across autorelease pool scope boundaries.
1145
- FindDependencies (AutoreleasePoolBoundary, Arg, Inst-> getParent (), Inst ,
1146
- DependingInstructions , PA);
1145
+ DepInst = findSingleDependency (AutoreleasePoolBoundary, Arg ,
1146
+ Inst-> getParent (), Inst , PA);
1147
1147
break ;
1148
1148
case ARCInstKind::ClaimRV:
1149
1149
case ARCInstKind::RetainRV:
@@ -1157,9 +1157,7 @@ void ObjCARCOpt::OptimizeIndividualCallImpl(
1157
1157
llvm_unreachable (" Invalid dependence flavor" );
1158
1158
}
1159
1159
1160
- if (DependingInstructions.size () != 1 )
1161
- continue ;
1162
- if (*DependingInstructions.begin () != PN)
1160
+ if (DepInst != PN)
1163
1161
continue ;
1164
1162
1165
1163
Changed = true ;
@@ -2233,24 +2231,21 @@ bool ObjCARCOpt::OptimizeSequences(Function &F) {
2233
2231
// / Check if there is a dependent call earlier that does not have anything in
2234
2232
// / between the Retain and the call that can affect the reference count of their
2235
2233
// / shared pointer argument. Note that Retain need not be in BB.
2236
- static bool
2237
- HasSafePathToPredecessorCall (const Value *Arg, Instruction *Retain,
2238
- SmallPtrSetImpl<Instruction *> &DepInsts,
2239
- ProvenanceAnalysis &PA) {
2240
- FindDependencies (CanChangeRetainCount, Arg, Retain->getParent (), Retain,
2241
- DepInsts, PA);
2242
- if (DepInsts.size () != 1 )
2243
- return false ;
2244
-
2245
- auto *Call = dyn_cast_or_null<CallInst>(*DepInsts.begin ());
2234
+ static CallInst *HasSafePathToPredecessorCall (const Value *Arg,
2235
+ Instruction *Retain,
2236
+ ProvenanceAnalysis &PA) {
2237
+ auto *Call = dyn_cast_or_null<CallInst>(findSingleDependency (
2238
+ CanChangeRetainCount, Arg, Retain->getParent (), Retain, PA));
2246
2239
2247
2240
// Check that the pointer is the return value of the call.
2248
2241
if (!Call || Arg != Call)
2249
- return false ;
2242
+ return nullptr ;
2250
2243
2251
2244
// Check that the call is a regular call.
2252
2245
ARCInstKind Class = GetBasicARCInstKind (Call);
2253
- return Class == ARCInstKind::CallOrUser || Class == ARCInstKind::Call;
2246
+ return Class == ARCInstKind::CallOrUser || Class == ARCInstKind::Call
2247
+ ? Call
2248
+ : nullptr ;
2254
2249
}
2255
2250
2256
2251
// / Find a dependent retain that precedes the given autorelease for which there
@@ -2260,12 +2255,8 @@ static CallInst *
2260
2255
FindPredecessorRetainWithSafePath (const Value *Arg, BasicBlock *BB,
2261
2256
Instruction *Autorelease,
2262
2257
ProvenanceAnalysis &PA) {
2263
- SmallPtrSet<Instruction *, 4 > DepInsts;
2264
- FindDependencies (CanChangeRetainCount, Arg, BB, Autorelease, DepInsts, PA);
2265
- if (DepInsts.size () != 1 )
2266
- return nullptr ;
2267
-
2268
- auto *Retain = dyn_cast_or_null<CallInst>(*DepInsts.begin ());
2258
+ auto *Retain = dyn_cast_or_null<CallInst>(
2259
+ findSingleDependency (CanChangeRetainCount, Arg, BB, Autorelease, PA));
2269
2260
2270
2261
// Check that we found a retain with the same argument.
2271
2262
if (!Retain || !IsRetain (GetBasicARCInstKind (Retain)) ||
@@ -2284,11 +2275,9 @@ FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB,
2284
2275
ReturnInst *Ret,
2285
2276
ProvenanceAnalysis &PA) {
2286
2277
SmallPtrSet<Instruction *, 4 > DepInsts;
2287
- FindDependencies (NeedsPositiveRetainCount, Arg, BB, Ret, DepInsts, PA);
2288
- if (DepInsts.size () != 1 )
2289
- return nullptr ;
2278
+ auto *Autorelease = dyn_cast_or_null<CallInst>(
2279
+ findSingleDependency (NeedsPositiveRetainCount, Arg, BB, Ret, PA));
2290
2280
2291
- auto *Autorelease = dyn_cast_or_null<CallInst>(*DepInsts.begin ());
2292
2281
if (!Autorelease)
2293
2282
return nullptr ;
2294
2283
ARCInstKind AutoreleaseClass = GetBasicARCInstKind (Autorelease);
@@ -2314,7 +2303,6 @@ void ObjCARCOpt::OptimizeReturns(Function &F) {
2314
2303
2315
2304
LLVM_DEBUG (dbgs () << " \n == ObjCARCOpt::OptimizeReturns ==\n " );
2316
2305
2317
- SmallPtrSet<Instruction *, 4 > DependingInstructions;
2318
2306
for (BasicBlock &BB: F) {
2319
2307
ReturnInst *Ret = dyn_cast<ReturnInst>(&BB.back ());
2320
2308
if (!Ret)
@@ -2341,21 +2329,13 @@ void ObjCARCOpt::OptimizeReturns(Function &F) {
2341
2329
2342
2330
// Check that there is nothing that can affect the reference count
2343
2331
// between the retain and the call. Note that Retain need not be in BB.
2344
- bool HasSafePathToCall =
2345
- HasSafePathToPredecessorCall (Arg, Retain, DependingInstructions, PA);
2332
+ CallInst *Call = HasSafePathToPredecessorCall (Arg, Retain, PA);
2346
2333
2347
2334
// Don't remove retainRV/autoreleaseRV pairs if the call isn't a tail call.
2348
- if (HasSafePathToCall &&
2349
- GetBasicARCInstKind (Retain) == ARCInstKind::RetainRV &&
2350
- GetBasicARCInstKind (Autorelease) == ARCInstKind::AutoreleaseRV &&
2351
- !cast<CallInst>(*DependingInstructions.begin ())->isTailCall ()) {
2352
- DependingInstructions.clear ();
2353
- continue ;
2354
- }
2355
-
2356
- DependingInstructions.clear ();
2357
-
2358
- if (!HasSafePathToCall)
2335
+ if (!Call ||
2336
+ (!Call->isTailCall () &&
2337
+ GetBasicARCInstKind (Retain) == ARCInstKind::RetainRV &&
2338
+ GetBasicARCInstKind (Autorelease) == ARCInstKind::AutoreleaseRV))
2359
2339
continue ;
2360
2340
2361
2341
// If so, we can zap the retain and autorelease.
0 commit comments