@@ -298,6 +298,74 @@ bool IsAddressWrittenToDefUseAnalysis::isWrittenToHelper(SILValue initialValue)
298298 return false ;
299299}
300300
301+ // ===----------------------------------------------------------------------===//
302+ // Convert Forwarding Insts from Owned -> Guaranteed
303+ // ===----------------------------------------------------------------------===//
304+
305+ static void convertForwardingInstsFromOwnedToGuaranteed (
306+ ArrayRef<SILInstruction *> guaranteedForwardingInsts) {
307+ // Then change all of our guaranteed forwarding insts to have guaranteed
308+ // ownership kind instead of what ever they previously had (ignoring trivial
309+ // results);
310+ while (!guaranteedForwardingInsts.empty ()) {
311+ auto *i = guaranteedForwardingInsts.back ();
312+ guaranteedForwardingInsts = guaranteedForwardingInsts.drop_back ();
313+
314+ // If this is a term inst, just convert all of its incoming values that are
315+ // owned to be guaranteed.
316+ if (auto *ti = dyn_cast<TermInst>(i)) {
317+ for (auto &succ : ti->getSuccessors ()) {
318+ auto *succBlock = succ.getBB ();
319+
320+ // If we do not have any arguments, then continue.
321+ if (succBlock->args_empty ())
322+ continue ;
323+
324+ for (auto *succArg : succBlock->getSILPhiArguments ()) {
325+ // If we have an any value, just continue.
326+ if (succArg->getOwnershipKind () == ValueOwnershipKind::Owned) {
327+ succArg->setOwnershipKind (ValueOwnershipKind::Guaranteed);
328+ }
329+ }
330+ }
331+ continue ;
332+ }
333+
334+ assert (i->hasResults ());
335+ for (SILValue result : i->getResults ()) {
336+ if (auto *svi = dyn_cast<OwnershipForwardingSingleValueInst>(result)) {
337+ if (svi->getOwnershipKind () == ValueOwnershipKind::Owned) {
338+ svi->setOwnershipKind (ValueOwnershipKind::Guaranteed);
339+ }
340+ continue ;
341+ }
342+
343+ if (auto *ofci = dyn_cast<OwnershipForwardingConversionInst>(result)) {
344+ if (ofci->getOwnershipKind () == ValueOwnershipKind::Owned) {
345+ ofci->setOwnershipKind (ValueOwnershipKind::Guaranteed);
346+ }
347+ continue ;
348+ }
349+
350+ if (auto *sei = dyn_cast<OwnershipForwardingSelectEnumInstBase>(result)) {
351+ if (sei->getOwnershipKind () == ValueOwnershipKind::Owned) {
352+ sei->setOwnershipKind (ValueOwnershipKind::Guaranteed);
353+ }
354+ continue ;
355+ }
356+
357+ if (auto *mvir = dyn_cast<MultipleValueInstructionResult>(result)) {
358+ if (mvir->getOwnershipKind () == ValueOwnershipKind::Owned) {
359+ mvir->setOwnershipKind (ValueOwnershipKind::Guaranteed);
360+ }
361+ continue ;
362+ }
363+
364+ llvm_unreachable (" unhandled forwarding instruction?!" );
365+ }
366+ }
367+ }
368+
301369// ===----------------------------------------------------------------------===//
302370// Implementation
303371// ===----------------------------------------------------------------------===//
@@ -519,6 +587,10 @@ bool SemanticARCOptVisitor::processWorklist() {
519587 return madeChange;
520588}
521589
590+ // ===----------------------------------------------------------------------===//
591+ // Redundant Borrow Scope Elimination
592+ // ===----------------------------------------------------------------------===//
593+
522594bool SemanticARCOptVisitor::visitBeginBorrowInst (BeginBorrowInst *bbi) {
523595 auto kind = bbi->getOperand ().getOwnershipKind ();
524596 SmallVector<EndBorrowInst *, 16 > endBorrows;
@@ -555,69 +627,9 @@ bool SemanticARCOptVisitor::visitBeginBorrowInst(BeginBorrowInst *bbi) {
555627 return true ;
556628}
557629
558- static void convertForwardingInstsFromOwnedToGuaranteed (
559- ArrayRef<SILInstruction *> guaranteedForwardingInsts) {
560- // Then change all of our guaranteed forwarding insts to have guaranteed
561- // ownership kind instead of what ever they previously had (ignoring trivial
562- // results);
563- while (!guaranteedForwardingInsts.empty ()) {
564- auto *i = guaranteedForwardingInsts.back ();
565- guaranteedForwardingInsts = guaranteedForwardingInsts.drop_back ();
566-
567- // If this is a term inst, just convert all of its incoming values that are
568- // owned to be guaranteed.
569- if (auto *ti = dyn_cast<TermInst>(i)) {
570- for (auto &succ : ti->getSuccessors ()) {
571- auto *succBlock = succ.getBB ();
572-
573- // If we do not have any arguments, then continue.
574- if (succBlock->args_empty ())
575- continue ;
576-
577- for (auto *succArg : succBlock->getSILPhiArguments ()) {
578- // If we have an any value, just continue.
579- if (succArg->getOwnershipKind () == ValueOwnershipKind::Owned) {
580- succArg->setOwnershipKind (ValueOwnershipKind::Guaranteed);
581- }
582- }
583- }
584- continue ;
585- }
586-
587- assert (i->hasResults ());
588- for (SILValue result : i->getResults ()) {
589- if (auto *svi = dyn_cast<OwnershipForwardingSingleValueInst>(result)) {
590- if (svi->getOwnershipKind () == ValueOwnershipKind::Owned) {
591- svi->setOwnershipKind (ValueOwnershipKind::Guaranteed);
592- }
593- continue ;
594- }
595-
596- if (auto *ofci = dyn_cast<OwnershipForwardingConversionInst>(result)) {
597- if (ofci->getOwnershipKind () == ValueOwnershipKind::Owned) {
598- ofci->setOwnershipKind (ValueOwnershipKind::Guaranteed);
599- }
600- continue ;
601- }
602-
603- if (auto *sei = dyn_cast<OwnershipForwardingSelectEnumInstBase>(result)) {
604- if (sei->getOwnershipKind () == ValueOwnershipKind::Owned) {
605- sei->setOwnershipKind (ValueOwnershipKind::Guaranteed);
606- }
607- continue ;
608- }
609-
610- if (auto *mvir = dyn_cast<MultipleValueInstructionResult>(result)) {
611- if (mvir->getOwnershipKind () == ValueOwnershipKind::Owned) {
612- mvir->setOwnershipKind (ValueOwnershipKind::Guaranteed);
613- }
614- continue ;
615- }
616-
617- llvm_unreachable (" unhandled forwarding instruction?!" );
618- }
619- }
620- }
630+ // ===----------------------------------------------------------------------===//
631+ // CopyValue Optimizations Elimination
632+ // ===----------------------------------------------------------------------===//
621633
622634// Eliminate a copy of a borrowed value, if:
623635//
0 commit comments