@@ -495,46 +495,30 @@ StackAllocationPromoter::promoteAllocationInBlock(SILBasicBlock *BB) {
495
495
if (SI->getDest () != ASI)
496
496
continue ;
497
497
498
- // Special handling of entry block
499
- // If we have a store [assign] in the first block, OSSA guarantees we can
500
- // find the previous value stored in the stack location in RunningVal.
501
- // Create destroy_value of the RunningVal.
502
- // For all other blocks we may not know the previous value stored in the
503
- // stack location. So we will create destroy_value in
504
- // StackAllocationPromoter::fixBranchesAndUses, by getting the live-in
505
- // value to the block.
506
- if (BB->isEntry ()) {
507
- if (SI->getOwnershipQualifier () == StoreOwnershipQualifier::Assign) {
508
- assert (RunningVal);
498
+ // If we see a store [assign], always convert it to a store [init]. This
499
+ // simplifies further processing.
500
+ if (SI->getOwnershipQualifier () == StoreOwnershipQualifier::Assign) {
501
+ if (RunningVal) {
509
502
SILBuilderWithScope (SI).createDestroyValue (SI->getLoc (), RunningVal);
503
+ } else {
504
+ SILBuilderWithScope localBuilder (SI);
505
+ auto *newLoad = localBuilder.createLoad (SI->getLoc (), ASI,
506
+ LoadOwnershipQualifier::Take);
507
+ localBuilder.createDestroyValue (SI->getLoc (), newLoad);
510
508
}
509
+ SI->setOwnershipQualifier (StoreOwnershipQualifier::Init);
511
510
}
512
511
513
512
// If we met a store before this one, delete it.
514
- // If the LastStore was a store with [assign], delete it only if we know
515
- // the RunningValue to destroy. If not, it will be deleted in
516
- // StackAllocationPromoter::fixBranchesAndUses.
517
513
if (LastStore) {
518
- if (LastStore->getOwnershipQualifier () ==
519
- StoreOwnershipQualifier::Assign) {
520
- if (RunningVal) {
521
- // For entry block, we would have already created the destroy_value,
522
- // skip it.
523
- if (!BB->isEntry ()) {
524
- SILBuilderWithScope (LastStore).createDestroyValue (
525
- LastStore->getLoc (), RunningVal);
526
- }
527
- LLVM_DEBUG (llvm::dbgs ()
528
- << " *** Removing redundant store: " << *LastStore);
529
- ++NumInstRemoved;
530
- LastStore->eraseFromParent ();
531
- }
532
- } else {
533
- LLVM_DEBUG (llvm::dbgs ()
534
- << " *** Removing redundant store: " << *LastStore);
535
- ++NumInstRemoved;
536
- LastStore->eraseFromParent ();
537
- }
514
+ assert (LastStore->getOwnershipQualifier () !=
515
+ StoreOwnershipQualifier::Assign &&
516
+ " store [assign] to the stack location should have been "
517
+ " transformed to a store [init]" );
518
+ LLVM_DEBUG (llvm::dbgs ()
519
+ << " *** Removing redundant store: " << *LastStore);
520
+ ++NumInstRemoved;
521
+ LastStore->eraseFromParent ();
538
522
}
539
523
540
524
// The stored value is the new running value.
@@ -578,7 +562,12 @@ StackAllocationPromoter::promoteAllocationInBlock(SILBasicBlock *BB) {
578
562
break ;
579
563
}
580
564
}
565
+
581
566
if (LastStore) {
567
+ assert (LastStore->getOwnershipQualifier () !=
568
+ StoreOwnershipQualifier::Assign &&
569
+ " store [assign] to the stack location should have been "
570
+ " transformed to a store [init]" );
582
571
LLVM_DEBUG (llvm::dbgs () << " *** Finished promotion. Last store: "
583
572
<< *LastStore);
584
573
} else {
@@ -756,6 +745,7 @@ void StackAllocationPromoter::fixPhiPredBlock(BlockSet &PhiBlocks,
756
745
void StackAllocationPromoter::fixBranchesAndUses (BlockSet &PhiBlocks) {
757
746
// First update uses of the value.
758
747
SmallVector<LoadInst *, 4 > collectedLoads;
748
+
759
749
for (auto UI = ASI->use_begin (), E = ASI->use_end (); UI != E;) {
760
750
auto *Inst = UI->getUser ();
761
751
++UI;
@@ -789,16 +779,6 @@ void StackAllocationPromoter::fixBranchesAndUses(BlockSet &PhiBlocks) {
789
779
// on.
790
780
SILBasicBlock *BB = Inst->getParent ();
791
781
792
- if (!BB->isEntry ()) {
793
- if (auto *SI = dyn_cast<StoreInst>(Inst)) {
794
- if (SI->getOwnershipQualifier () == StoreOwnershipQualifier::Assign) {
795
- SILValue Def = getLiveInValue (PhiBlocks, BB);
796
- SILBuilderWithScope (SI).createDestroyValue (SI->getLoc (), Def);
797
- continue ;
798
- }
799
- }
800
- }
801
-
802
782
if (auto *DVAI = dyn_cast<DebugValueAddrInst>(Inst)) {
803
783
// Replace DebugValueAddr with DebugValue.
804
784
SILValue Def = getLiveInValue (PhiBlocks, BB);
0 commit comments