@@ -140,6 +140,8 @@ struct BlockState {
140140// / code motion procedure should be.
141141class CodeMotionContext {
142142protected:
143+ SILFunctionTransform *parentTransform;
144+
143145 // / Dataflow needs multiple iteration to converge. If this is false, then we
144146 // / do not need to generate the genset or killset, i.e. we can simply do 1
145147 // / pessimistic data flow iteration.
@@ -202,11 +204,13 @@ class CodeMotionContext {
202204
203205public:
204206 // / Constructor.
205- CodeMotionContext (llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
207+ CodeMotionContext (SILFunctionTransform *parentTransform,
208+ llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
206209 SILFunction *F,
207210 PostOrderFunctionInfo *PO, AliasAnalysis *AA,
208211 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),
210214 InterestBlocks(F) {}
211215
212216 // / virtual destructor.
@@ -310,11 +314,16 @@ class RetainBlockState : public BlockState {
310314
311315// / RetainCodeMotionContext - Context to perform retain code motion.
312316class 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.
314318 BasicBlockData<RetainBlockState> BlockStates;
315319
320+ InstructionSet retainInstructions;
321+
316322 ProgramTerminationFunctionInfo PTFI;
317323
324+ bool isRetain (SILInstruction *inst) const {
325+ return retainInstructions.contains (inst);
326+ }
318327 // / Return true if the instruction blocks the Ptr to be moved further.
319328 bool mayBlockCodeMotion (SILInstruction *II, SILValue Ptr) override {
320329 // NOTE: If more checks are to be added, place the most expensive in the
@@ -326,7 +335,7 @@ class RetainCodeMotionContext : public CodeMotionContext {
326335 return true ;
327336 // Identical RC root blocks code motion, we will be able to move this retain
328337 // further once we move the blocking retain.
329- if (isRetainInstruction (II) && getRCRoot (II) == Ptr) {
338+ if (isRetain (II) && getRCRoot (II) == Ptr) {
330339 LLVM_DEBUG (if (printCtx) llvm::dbgs ()
331340 << " Retain " << Ptr << " at matching retain " << *II);
332341 return true ;
@@ -353,17 +362,19 @@ class RetainCodeMotionContext : public CodeMotionContext {
353362 if (&*I->getParent ()->begin () == I)
354363 return nullptr ;
355364 auto Prev = &*std::prev (SILBasicBlock::iterator (I));
356- if (isRetainInstruction (Prev) && getRCRoot (Prev) == Root)
365+ if (isRetain (Prev) && getRCRoot (Prev) == Root)
357366 return Prev;
358367 return nullptr ;
359368 }
360369
361370public:
362371 // / Constructor.
363- RetainCodeMotionContext (llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
372+ RetainCodeMotionContext (SILFunctionTransform *parentTransform,
373+ llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
364374 SILFunction *F, PostOrderFunctionInfo *PO,
365375 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) {}
367378
368379 // / virtual destructor.
369380 ~RetainCodeMotionContext () override {}
@@ -419,6 +430,9 @@ void RetainCodeMotionContext::initializeCodeMotionDataFlow() {
419430 for (auto &II : BB) {
420431 if (!isRetainInstruction (&II))
421432 continue ;
433+ if (!parentTransform->continueWithNextSubpassRun (&II))
434+ continue ;
435+ retainInstructions.insert (&II);
422436 RCInstructions.insert (&II);
423437 SILValue Root = getRCRoot (&II);
424438 if (RCRootIndex.find (Root) != RCRootIndex.end ())
@@ -457,7 +471,7 @@ void RetainCodeMotionContext::initializeCodeMotionBBMaxSet() {
457471 // NOTE: this is a conservative approximation, because some retains may be
458472 // blocked before it reaches this block.
459473 for (auto &II : *BB) {
460- if (!isRetainInstruction (&II))
474+ if (!isRetain (&II))
461475 continue ;
462476 State.BBMaxSet .set (RCRootIndex[getRCRoot (&II)]);
463477 }
@@ -479,7 +493,7 @@ void RetainCodeMotionContext::computeCodeMotionGenKillSet() {
479493 State.BBGenSet .reset (i);
480494 }
481495 // If this is a retain instruction, it also generates.
482- if (isRetainInstruction (&I)) {
496+ if (isRetain (&I)) {
483497 unsigned idx = RCRootIndex[getRCRoot (&I)];
484498 State.BBGenSet .set (idx);
485499 assert (State.BBKillSet .test (idx) && " Killset computed incorrectly" );
@@ -622,7 +636,7 @@ void RetainCodeMotionContext::computeCodeMotionInsertPoints() {
622636 }
623637
624638 // If this is a retain instruction, it also generates.
625- if (isRetainInstruction (&*I)) {
639+ if (isRetain (&*I)) {
626640 S.BBSetIn .set (RCRootIndex[getRCRoot (&*I)]);
627641 }
628642 }
@@ -668,8 +682,6 @@ class ReleaseBlockState : public BlockState {
668682
669683// / ReleaseCodeMotionContext - Context to perform release code motion.
670684class ReleaseCodeMotionContext : public CodeMotionContext {
671- SILFunctionTransform *parentTransform;
672-
673685 // / All the release block state for all the basic blocks in the function.
674686 BasicBlockData<ReleaseBlockState> BlockStates;
675687
@@ -730,8 +742,7 @@ class ReleaseCodeMotionContext : public CodeMotionContext {
730742 AliasAnalysis *AA, RCIdentityFunctionInfo *RCFI,
731743 bool FreezeEpilogueReleases,
732744 ConsumedArgToEpilogueReleaseMatcher &ERM)
733- : CodeMotionContext(BPA, F, PO, AA, RCFI),
734- parentTransform (parentTransform),
745+ : CodeMotionContext(parentTransform, BPA, F, PO, AA, RCFI),
735746 BlockStates (F),
736747 releaseInstructions(F),
737748 FreezeEpilogueReleases(FreezeEpilogueReleases), ERM(ERM) {}
@@ -1232,7 +1243,7 @@ class ARCCodeMotion : public SILFunctionTransform {
12321243 // Run release hoisting.
12331244 InstChanged |= RelCM.run ();
12341245 } else {
1235- RetainCodeMotionContext RetCM (BPA, F, PO, AA, RCFI);
1246+ RetainCodeMotionContext RetCM (this , BPA, F, PO, AA, RCFI);
12361247 // Run retain sinking.
12371248 InstChanged |= RetCM.run ();
12381249 // Eliminate any retains that are right before program termination
0 commit comments