@@ -753,11 +753,8 @@ SILValue AvailableValueAggregator::handlePrimitiveValue(SILType loadTy,
753
753
SingleValueInstruction *
754
754
AvailableValueAggregator::addMissingDestroysForCopiedValues (
755
755
SingleValueInstruction *svi, SILValue newVal) {
756
- // If ownership is not enabled... bail. We do not need to do this since we do
757
- // not need to insert an extra copy unless we have ownership since without
758
- // ownership stores do not consume.
759
- if (!B.hasOwnership ())
760
- return svi;
756
+ assert (B.hasOwnership () &&
757
+ " We assume this is only called if we have ownership" );
761
758
762
759
assert ((isa<LoadBorrowInst>(svi) || isa<LoadInst>(svi)) &&
763
760
" Expected to have a /real/ load here since we assume that we have a "
@@ -1492,9 +1489,9 @@ class AllocOptimize {
1492
1489
Optional<std::pair<SILType, unsigned >>
1493
1490
computeAvailableValues (SILValue SrcAddr, SILInstruction *Inst,
1494
1491
SmallVectorImpl<AvailableValue> &AvailableValues);
1495
- bool promoteLoadCopy (LoadInst *Inst );
1496
- bool promoteLoadBorrow (LoadBorrowInst *Inst );
1497
- bool promoteCopyAddr (CopyAddrInst *CAI );
1492
+ bool promoteLoadCopy (LoadInst *li );
1493
+ bool promoteLoadBorrow (LoadBorrowInst *lbi );
1494
+ bool promoteCopyAddr (CopyAddrInst *cai );
1498
1495
void promoteLoadTake (LoadInst *Inst, MutableArrayRef<AvailableValue> values);
1499
1496
void promoteDestroyAddr (DestroyAddrInst *dai,
1500
1497
MutableArrayRef<AvailableValue> values);
@@ -1570,7 +1567,7 @@ static SILValue tryFindSrcAddrForLoad(SILInstruction *i) {
1570
1567
// / cross element accesses have been scalarized.
1571
1568
// /
1572
1569
// / This returns true if the load has been removed from the program.
1573
- bool AllocOptimize::promoteLoadCopy (LoadInst *Inst ) {
1570
+ bool AllocOptimize::promoteLoadCopy (LoadInst *li ) {
1574
1571
// Note that we intentionally don't support forwarding of weak pointers,
1575
1572
// because the underlying value may drop be deallocated at any time. We would
1576
1573
// have to prove that something in this function is holding the weak value
@@ -1579,29 +1576,40 @@ bool AllocOptimize::promoteLoadCopy(LoadInst *Inst) {
1579
1576
1580
1577
// First attempt to find a source addr for our "load" instruction. If we fail
1581
1578
// to find a valid value, just return.
1582
- SILValue SrcAddr = tryFindSrcAddrForLoad (Inst );
1583
- if (!SrcAddr )
1579
+ SILValue srcAddr = tryFindSrcAddrForLoad (li );
1580
+ if (!srcAddr )
1584
1581
return false ;
1585
1582
1586
- SmallVector<AvailableValue, 8 > AvailableValues ;
1587
- auto Result = computeAvailableValues (SrcAddr, Inst, AvailableValues );
1588
- if (!Result .hasValue ())
1583
+ SmallVector<AvailableValue, 8 > availableValues ;
1584
+ auto result = computeAvailableValues (srcAddr, li, availableValues );
1585
+ if (!result .hasValue ())
1589
1586
return false ;
1590
1587
1591
- SILType LoadTy = Result ->first ;
1592
- unsigned FirstElt = Result ->second ;
1588
+ SILType loadTy = result ->first ;
1589
+ unsigned firstElt = result ->second ;
1593
1590
1594
1591
// Aggregate together all of the subelements into something that has the same
1595
1592
// type as the load did, and emit smaller loads for any subelements that were
1596
1593
// not available. We are "propagating" a +1 available value from the store
1597
1594
// points.
1598
- auto *load = dyn_cast<SingleValueInstruction>(Inst);
1599
- AvailableValueAggregator agg (load, AvailableValues, Uses, deadEndBlocks,
1595
+ AvailableValueAggregator agg (li, availableValues, Uses, deadEndBlocks,
1600
1596
false /* isTake*/ );
1601
- SILValue newVal = agg.aggregateValues (LoadTy, load ->getOperand (0 ), FirstElt );
1597
+ SILValue newVal = agg.aggregateValues (loadTy, li ->getOperand (), firstElt );
1602
1598
1603
- LLVM_DEBUG (llvm::dbgs () << " *** Promoting load: " << *load << " \n " );
1599
+ LLVM_DEBUG (llvm::dbgs () << " *** Promoting load: " << *li << " \n " );
1604
1600
LLVM_DEBUG (llvm::dbgs () << " To value: " << *newVal << " \n " );
1601
+ ++NumLoadPromoted;
1602
+
1603
+ // If we did not have ownership, we did not insert extra copies at our stores,
1604
+ // so we can just RAUW and return.
1605
+ if (!li->getFunction ()->hasOwnership ()) {
1606
+ li->replaceAllUsesWith (newVal);
1607
+ SILValue addr = li->getOperand ();
1608
+ li->eraseFromParent ();
1609
+ if (auto *addrI = addr->getDefiningInstruction ())
1610
+ recursivelyDeleteTriviallyDeadInstructions (addrI);
1611
+ return true ;
1612
+ }
1605
1613
1606
1614
// If we inserted any copies, we created the copies at our stores. We know
1607
1615
// that in our load block, we will reform the aggregate as appropriate at the
@@ -1610,9 +1618,7 @@ bool AllocOptimize::promoteLoadCopy(LoadInst *Inst) {
1610
1618
// blocks that we may have can be found by performing a linear lifetime check
1611
1619
// over all copies that we found using the load as the "consuming uses" (just
1612
1620
// for the purposes of identifying the consuming block).
1613
- auto *oldLoad = agg.addMissingDestroysForCopiedValues (load, newVal);
1614
-
1615
- ++NumLoadPromoted;
1621
+ auto *oldLoad = agg.addMissingDestroysForCopiedValues (li, newVal);
1616
1622
1617
1623
// If we are returned the load, eliminate it. Otherwise, it was already
1618
1624
// handled for us... so return true.
@@ -1627,7 +1633,7 @@ bool AllocOptimize::promoteLoadCopy(LoadInst *Inst) {
1627
1633
return true ;
1628
1634
}
1629
1635
1630
- bool AllocOptimize::promoteCopyAddr (CopyAddrInst *Inst ) {
1636
+ bool AllocOptimize::promoteCopyAddr (CopyAddrInst *cai ) {
1631
1637
// Note that we intentionally don't support forwarding of weak pointers,
1632
1638
// because the underlying value may drop be deallocated at any time. We would
1633
1639
// have to prove that something in this function is holding the weak value
@@ -1636,19 +1642,19 @@ bool AllocOptimize::promoteCopyAddr(CopyAddrInst *Inst) {
1636
1642
1637
1643
// First attempt to find a source addr for our "load" instruction. If we fail
1638
1644
// to find a valid value, just return.
1639
- SILValue SrcAddr = tryFindSrcAddrForLoad (Inst );
1640
- if (!SrcAddr )
1645
+ SILValue srcAddr = tryFindSrcAddrForLoad (cai );
1646
+ if (!srcAddr )
1641
1647
return false ;
1642
1648
1643
- SmallVector<AvailableValue, 8 > AvailableValues ;
1644
- auto Result = computeAvailableValues (SrcAddr, Inst, AvailableValues );
1645
- if (!Result .hasValue ())
1649
+ SmallVector<AvailableValue, 8 > availableValues ;
1650
+ auto result = computeAvailableValues (srcAddr, cai, availableValues );
1651
+ if (!result .hasValue ())
1646
1652
return false ;
1647
1653
1648
1654
// Ok, we have some available values. If we have a copy_addr, explode it now,
1649
1655
// exposing the load operation within it. Subsequent optimization passes will
1650
1656
// see the load and propagate the available values into it.
1651
- DataflowContext.explodeCopyAddr (Inst );
1657
+ DataflowContext.explodeCopyAddr (cai );
1652
1658
1653
1659
// This is removing the copy_addr, but explodeCopyAddr takes care of
1654
1660
// removing the instruction from Uses for us, so we return false.
@@ -1661,7 +1667,7 @@ bool AllocOptimize::promoteCopyAddr(CopyAddrInst *Inst) {
1661
1667
// / cross element accesses have been scalarized.
1662
1668
// /
1663
1669
// / This returns true if the load has been removed from the program.
1664
- bool AllocOptimize::promoteLoadBorrow (LoadBorrowInst *Inst ) {
1670
+ bool AllocOptimize::promoteLoadBorrow (LoadBorrowInst *lbi ) {
1665
1671
// Note that we intentionally don't support forwarding of weak pointers,
1666
1672
// because the underlying value may drop be deallocated at any time. We would
1667
1673
// have to prove that something in this function is holding the weak value
@@ -1670,28 +1676,27 @@ bool AllocOptimize::promoteLoadBorrow(LoadBorrowInst *Inst) {
1670
1676
1671
1677
// First attempt to find a source addr for our "load" instruction. If we fail
1672
1678
// to find a valid value, just return.
1673
- SILValue SrcAddr = tryFindSrcAddrForLoad (Inst );
1674
- if (!SrcAddr )
1679
+ SILValue srcAddr = tryFindSrcAddrForLoad (lbi );
1680
+ if (!srcAddr )
1675
1681
return false ;
1676
1682
1677
- SmallVector<AvailableValue, 8 > AvailableValues ;
1678
- auto Result = computeAvailableValues (SrcAddr, Inst, AvailableValues );
1679
- if (!Result .hasValue ())
1683
+ SmallVector<AvailableValue, 8 > availableValues ;
1684
+ auto result = computeAvailableValues (srcAddr, lbi, availableValues );
1685
+ if (!result .hasValue ())
1680
1686
return false ;
1681
1687
1682
- SILType LoadTy = Result ->first ;
1683
- unsigned FirstElt = Result ->second ;
1688
+ SILType loadTy = result ->first ;
1689
+ unsigned firstElt = result ->second ;
1684
1690
1685
1691
// Aggregate together all of the subelements into something that has the same
1686
1692
// type as the load did, and emit smaller loads for any subelements that were
1687
1693
// not available. We are "propagating" a +1 available value from the store
1688
1694
// points.
1689
- auto *load = dyn_cast<SingleValueInstruction>(Inst);
1690
- AvailableValueAggregator agg (load, AvailableValues, Uses, deadEndBlocks,
1695
+ AvailableValueAggregator agg (lbi, availableValues, Uses, deadEndBlocks,
1691
1696
false /* isTake*/ );
1692
- SILValue newVal = agg.aggregateValues (LoadTy, load ->getOperand (0 ), FirstElt );
1697
+ SILValue newVal = agg.aggregateValues (loadTy, lbi ->getOperand (), firstElt );
1693
1698
1694
- LLVM_DEBUG (llvm::dbgs () << " *** Promoting load: " << *load << " \n " );
1699
+ LLVM_DEBUG (llvm::dbgs () << " *** Promoting load: " << *lbi << " \n " );
1695
1700
LLVM_DEBUG (llvm::dbgs () << " To value: " << *newVal << " \n " );
1696
1701
1697
1702
// If we inserted any copies, we created the copies at our stores. We know
@@ -1701,7 +1706,7 @@ bool AllocOptimize::promoteLoadBorrow(LoadBorrowInst *Inst) {
1701
1706
// blocks that we may have can be found by performing a linear lifetime check
1702
1707
// over all copies that we found using the load as the "consuming uses" (just
1703
1708
// for the purposes of identifying the consuming block).
1704
- auto *oldLoad = agg.addMissingDestroysForCopiedValues (load , newVal);
1709
+ auto *oldLoad = agg.addMissingDestroysForCopiedValues (lbi , newVal);
1705
1710
1706
1711
++NumLoadPromoted;
1707
1712
0 commit comments