@@ -140,6 +140,8 @@ struct BlockState {
140
140
// / code motion procedure should be.
141
141
class CodeMotionContext {
142
142
protected:
143
+ SILFunctionTransform *parentTransform;
144
+
143
145
// / Dataflow needs multiple iteration to converge. If this is false, then we
144
146
// / do not need to generate the genset or killset, i.e. we can simply do 1
145
147
// / pessimistic data flow iteration.
@@ -202,11 +204,13 @@ class CodeMotionContext {
202
204
203
205
public:
204
206
// / Constructor.
205
- CodeMotionContext (llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
207
+ CodeMotionContext (SILFunctionTransform *parentTransform,
208
+ llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
206
209
SILFunction *F,
207
210
PostOrderFunctionInfo *PO, AliasAnalysis *AA,
208
211
RCIdentityFunctionInfo *RCFI)
209
- : MultiIteration(true ), BPA(BPA), F(F), PO(PO), AA(AA), RCFI(RCFI),
212
+ : parentTransform(parentTransform),
213
+ MultiIteration (true ), BPA(BPA), F(F), PO(PO), AA(AA), RCFI(RCFI),
210
214
InterestBlocks(F) {}
211
215
212
216
// / virtual destructor.
@@ -310,11 +314,16 @@ class RetainBlockState : public BlockState {
310
314
311
315
// / RetainCodeMotionContext - Context to perform retain code motion.
312
316
class RetainCodeMotionContext : public CodeMotionContext {
313
- // / All the retain block state for all the basic blocks in the function.
317
+ // / All the retain block state for all the basic blocks in the function.
314
318
BasicBlockData<RetainBlockState> BlockStates;
315
319
320
+ InstructionSet retainInstructions;
321
+
316
322
ProgramTerminationFunctionInfo PTFI;
317
323
324
+ bool isRetain (SILInstruction *inst) const {
325
+ return retainInstructions.contains (inst);
326
+ }
318
327
// / Return true if the instruction blocks the Ptr to be moved further.
319
328
bool mayBlockCodeMotion (SILInstruction *II, SILValue Ptr) override {
320
329
// NOTE: If more checks are to be added, place the most expensive in the
@@ -326,7 +335,7 @@ class RetainCodeMotionContext : public CodeMotionContext {
326
335
return true ;
327
336
// Identical RC root blocks code motion, we will be able to move this retain
328
337
// further once we move the blocking retain.
329
- if (isRetainInstruction (II) && getRCRoot (II) == Ptr) {
338
+ if (isRetain (II) && getRCRoot (II) == Ptr) {
330
339
LLVM_DEBUG (if (printCtx) llvm::dbgs ()
331
340
<< " Retain " << Ptr << " at matching retain " << *II);
332
341
return true ;
@@ -353,17 +362,19 @@ class RetainCodeMotionContext : public CodeMotionContext {
353
362
if (&*I->getParent ()->begin () == I)
354
363
return nullptr ;
355
364
auto Prev = &*std::prev (SILBasicBlock::iterator (I));
356
- if (isRetainInstruction (Prev) && getRCRoot (Prev) == Root)
365
+ if (isRetain (Prev) && getRCRoot (Prev) == Root)
357
366
return Prev;
358
367
return nullptr ;
359
368
}
360
369
361
370
public:
362
371
// / Constructor.
363
- RetainCodeMotionContext (llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
372
+ RetainCodeMotionContext (SILFunctionTransform *parentTransform,
373
+ llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
364
374
SILFunction *F, PostOrderFunctionInfo *PO,
365
375
AliasAnalysis *AA, RCIdentityFunctionInfo *RCFI)
366
- : CodeMotionContext(BPA, F, PO, AA, RCFI), BlockStates(F), PTFI(F) {}
376
+ : CodeMotionContext(parentTransform, BPA, F, PO, AA, RCFI),
377
+ BlockStates (F), retainInstructions(F), PTFI(F) {}
367
378
368
379
// / virtual destructor.
369
380
~RetainCodeMotionContext () override {}
@@ -419,6 +430,9 @@ void RetainCodeMotionContext::initializeCodeMotionDataFlow() {
419
430
for (auto &II : BB) {
420
431
if (!isRetainInstruction (&II))
421
432
continue ;
433
+ if (!parentTransform->continueWithNextSubpassRun (&II))
434
+ continue ;
435
+ retainInstructions.insert (&II);
422
436
RCInstructions.insert (&II);
423
437
SILValue Root = getRCRoot (&II);
424
438
if (RCRootIndex.find (Root) != RCRootIndex.end ())
@@ -457,7 +471,7 @@ void RetainCodeMotionContext::initializeCodeMotionBBMaxSet() {
457
471
// NOTE: this is a conservative approximation, because some retains may be
458
472
// blocked before it reaches this block.
459
473
for (auto &II : *BB) {
460
- if (!isRetainInstruction (&II))
474
+ if (!isRetain (&II))
461
475
continue ;
462
476
State.BBMaxSet .set (RCRootIndex[getRCRoot (&II)]);
463
477
}
@@ -479,7 +493,7 @@ void RetainCodeMotionContext::computeCodeMotionGenKillSet() {
479
493
State.BBGenSet .reset (i);
480
494
}
481
495
// If this is a retain instruction, it also generates.
482
- if (isRetainInstruction (&I)) {
496
+ if (isRetain (&I)) {
483
497
unsigned idx = RCRootIndex[getRCRoot (&I)];
484
498
State.BBGenSet .set (idx);
485
499
assert (State.BBKillSet .test (idx) && " Killset computed incorrectly" );
@@ -622,7 +636,7 @@ void RetainCodeMotionContext::computeCodeMotionInsertPoints() {
622
636
}
623
637
624
638
// If this is a retain instruction, it also generates.
625
- if (isRetainInstruction (&*I)) {
639
+ if (isRetain (&*I)) {
626
640
S.BBSetIn .set (RCRootIndex[getRCRoot (&*I)]);
627
641
}
628
642
}
@@ -668,8 +682,6 @@ class ReleaseBlockState : public BlockState {
668
682
669
683
// / ReleaseCodeMotionContext - Context to perform release code motion.
670
684
class ReleaseCodeMotionContext : public CodeMotionContext {
671
- SILFunctionTransform *parentTransform;
672
-
673
685
// / All the release block state for all the basic blocks in the function.
674
686
BasicBlockData<ReleaseBlockState> BlockStates;
675
687
@@ -730,8 +742,7 @@ class ReleaseCodeMotionContext : public CodeMotionContext {
730
742
AliasAnalysis *AA, RCIdentityFunctionInfo *RCFI,
731
743
bool FreezeEpilogueReleases,
732
744
ConsumedArgToEpilogueReleaseMatcher &ERM)
733
- : CodeMotionContext(BPA, F, PO, AA, RCFI),
734
- parentTransform (parentTransform),
745
+ : CodeMotionContext(parentTransform, BPA, F, PO, AA, RCFI),
735
746
BlockStates (F),
736
747
releaseInstructions(F),
737
748
FreezeEpilogueReleases(FreezeEpilogueReleases), ERM(ERM) {}
@@ -1232,7 +1243,7 @@ class ARCCodeMotion : public SILFunctionTransform {
1232
1243
// Run release hoisting.
1233
1244
InstChanged |= RelCM.run ();
1234
1245
} else {
1235
- RetainCodeMotionContext RetCM (BPA, F, PO, AA, RCFI);
1246
+ RetainCodeMotionContext RetCM (this , BPA, F, PO, AA, RCFI);
1236
1247
// Run retain sinking.
1237
1248
InstChanged |= RetCM.run ();
1238
1249
// Eliminate any retains that are right before program termination
0 commit comments