Skip to content

Commit fad176e

Browse files
committed
[pred-memopt] Extract out the computation of available values into a helper.
NFC.
1 parent 933af09 commit fad176e

File tree

1 file changed

+52
-90
lines changed

1 file changed

+52
-90
lines changed

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 52 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,9 @@ class AllocOptimize {
14891489
bool tryToRemoveDeadAllocation();
14901490

14911491
private:
1492+
Optional<std::pair<SILType, unsigned>>
1493+
computeAvailableValues(SILValue SrcAddr, SILInstruction *Inst,
1494+
SmallVectorImpl<AvailableValue> &AvailableValues);
14921495
bool promoteLoadCopy(LoadInst *Inst);
14931496
bool promoteLoadBorrow(LoadBorrowInst *Inst);
14941497
bool promoteCopyAddr(CopyAddrInst *CAI);
@@ -1501,6 +1504,43 @@ class AllocOptimize {
15011504

15021505
} // end anonymous namespace
15031506

1507+
Optional<std::pair<SILType, unsigned>> AllocOptimize::computeAvailableValues(
1508+
SILValue SrcAddr, SILInstruction *Inst,
1509+
SmallVectorImpl<AvailableValue> &AvailableValues) {
1510+
// If the box has escaped at this instruction, we can't safely promote the
1511+
// load.
1512+
if (DataflowContext.hasEscapedAt(Inst))
1513+
return None;
1514+
1515+
SILType LoadTy = SrcAddr->getType().getObjectType();
1516+
1517+
// If this is a load/copy_addr from a struct field that we want to promote,
1518+
// compute the access path down to the field so we can determine precise
1519+
// def/use behavior.
1520+
unsigned FirstElt = computeSubelement(SrcAddr, TheMemory);
1521+
1522+
// If this is a load from within an enum projection, we can't promote it since
1523+
// we don't track subelements in a type that could be changing.
1524+
if (FirstElt == ~0U)
1525+
return None;
1526+
1527+
unsigned NumLoadSubElements = getNumSubElements(LoadTy, Module);
1528+
1529+
// Set up the bitvector of elements being demanded by the load.
1530+
SmallBitVector RequiredElts(NumMemorySubElements);
1531+
RequiredElts.set(FirstElt, FirstElt + NumLoadSubElements);
1532+
1533+
AvailableValues.resize(NumMemorySubElements);
1534+
1535+
// Find out if we have any available values. If no bits are demanded, we
1536+
// trivially succeed. This can happen when there is a load of an empty struct.
1537+
if (NumLoadSubElements != 0 &&
1538+
!DataflowContext.computeAvailableValues(
1539+
Inst, FirstElt, NumLoadSubElements, RequiredElts, AvailableValues))
1540+
return None;
1541+
1542+
return std::make_pair(LoadTy, FirstElt);
1543+
}
15041544

15051545
/// If we are able to optimize \p Inst, return the source address that
15061546
/// instruction is loading from. If we can not optimize \p Inst, then just
@@ -1543,39 +1583,14 @@ bool AllocOptimize::promoteLoadCopy(LoadInst *Inst) {
15431583
if (!SrcAddr)
15441584
return false;
15451585

1546-
// If the box has escaped at this instruction, we can't safely promote the
1547-
// load.
1548-
if (DataflowContext.hasEscapedAt(Inst))
1549-
return false;
1550-
1551-
SILType LoadTy = SrcAddr->getType().getObjectType();
1552-
1553-
// If this is a load/copy_addr from a struct field that we want to promote,
1554-
// compute the access path down to the field so we can determine precise
1555-
// def/use behavior.
1556-
unsigned FirstElt = computeSubelement(SrcAddr, TheMemory);
1557-
1558-
// If this is a load from within an enum projection, we can't promote it since
1559-
// we don't track subelements in a type that could be changing.
1560-
if (FirstElt == ~0U)
1561-
return false;
1562-
1563-
unsigned NumLoadSubElements = getNumSubElements(LoadTy, Module);
1564-
1565-
// Set up the bitvector of elements being demanded by the load.
1566-
SmallBitVector RequiredElts(NumMemorySubElements);
1567-
RequiredElts.set(FirstElt, FirstElt + NumLoadSubElements);
1568-
15691586
SmallVector<AvailableValue, 8> AvailableValues;
1570-
AvailableValues.resize(NumMemorySubElements);
1571-
1572-
// Find out if we have any available values. If no bits are demanded, we
1573-
// trivially succeed. This can happen when there is a load of an empty struct.
1574-
if (NumLoadSubElements != 0 &&
1575-
!DataflowContext.computeAvailableValues(
1576-
Inst, FirstElt, NumLoadSubElements, RequiredElts, AvailableValues))
1587+
auto Result = computeAvailableValues(SrcAddr, Inst, AvailableValues);
1588+
if (!Result.hasValue())
15771589
return false;
15781590

1591+
SILType LoadTy = Result->first;
1592+
unsigned FirstElt = Result->second;
1593+
15791594
// Aggregate together all of the subelements into something that has the same
15801595
// type as the load did, and emit smaller loads for any subelements that were
15811596
// not available. We are "propagating" a +1 available value from the store
@@ -1625,37 +1640,9 @@ bool AllocOptimize::promoteCopyAddr(CopyAddrInst *Inst) {
16251640
if (!SrcAddr)
16261641
return false;
16271642

1628-
// If the box has escaped at this instruction, we can't safely promote the
1629-
// load.
1630-
if (DataflowContext.hasEscapedAt(Inst))
1631-
return false;
1632-
1633-
SILType LoadTy = SrcAddr->getType().getObjectType();
1634-
1635-
// If this is a load/copy_addr from a struct field that we want to promote,
1636-
// compute the access path down to the field so we can determine precise
1637-
// def/use behavior.
1638-
unsigned FirstElt = computeSubelement(SrcAddr, TheMemory);
1639-
1640-
// If this is a load from within an enum projection, we can't promote it since
1641-
// we don't track subelements in a type that could be changing.
1642-
if (FirstElt == ~0U)
1643-
return false;
1644-
1645-
unsigned NumLoadSubElements = getNumSubElements(LoadTy, Module);
1646-
1647-
// Set up the bitvector of elements being demanded by the load.
1648-
SmallBitVector RequiredElts(NumMemorySubElements);
1649-
RequiredElts.set(FirstElt, FirstElt+NumLoadSubElements);
1650-
16511643
SmallVector<AvailableValue, 8> AvailableValues;
1652-
AvailableValues.resize(NumMemorySubElements);
1653-
1654-
// Find out if we have any available values. If no bits are demanded, we
1655-
// trivially succeed. This can happen when there is a load of an empty struct.
1656-
if (NumLoadSubElements != 0 &&
1657-
!DataflowContext.computeAvailableValues(
1658-
Inst, FirstElt, NumLoadSubElements, RequiredElts, AvailableValues))
1644+
auto Result = computeAvailableValues(SrcAddr, Inst, AvailableValues);
1645+
if (!Result.hasValue())
16591646
return false;
16601647

16611648
// Ok, we have some available values. If we have a copy_addr, explode it now,
@@ -1687,39 +1674,14 @@ bool AllocOptimize::promoteLoadBorrow(LoadBorrowInst *Inst) {
16871674
if (!SrcAddr)
16881675
return false;
16891676

1690-
// If the box has escaped at this instruction, we can't safely promote the
1691-
// load.
1692-
if (DataflowContext.hasEscapedAt(Inst))
1693-
return false;
1694-
1695-
SILType LoadTy = SrcAddr->getType().getObjectType();
1696-
1697-
// If this is a load/copy_addr from a struct field that we want to promote,
1698-
// compute the access path down to the field so we can determine precise
1699-
// def/use behavior.
1700-
unsigned FirstElt = computeSubelement(SrcAddr, TheMemory);
1701-
1702-
// If this is a load from within an enum projection, we can't promote it since
1703-
// we don't track subelements in a type that could be changing.
1704-
if (FirstElt == ~0U)
1705-
return false;
1706-
1707-
unsigned NumLoadSubElements = getNumSubElements(LoadTy, Module);
1708-
1709-
// Set up the bitvector of elements being demanded by the load.
1710-
SmallBitVector RequiredElts(NumMemorySubElements);
1711-
RequiredElts.set(FirstElt, FirstElt + NumLoadSubElements);
1712-
17131677
SmallVector<AvailableValue, 8> AvailableValues;
1714-
AvailableValues.resize(NumMemorySubElements);
1715-
1716-
// Find out if we have any available values. If no bits are demanded, we
1717-
// trivially succeed. This can happen when there is a load of an empty struct.
1718-
if (NumLoadSubElements != 0 &&
1719-
!DataflowContext.computeAvailableValues(
1720-
Inst, FirstElt, NumLoadSubElements, RequiredElts, AvailableValues))
1678+
auto Result = computeAvailableValues(SrcAddr, Inst, AvailableValues);
1679+
if (!Result.hasValue())
17211680
return false;
17221681

1682+
SILType LoadTy = Result->first;
1683+
unsigned FirstElt = Result->second;
1684+
17231685
// Aggregate together all of the subelements into something that has the same
17241686
// type as the load did, and emit smaller loads for any subelements that were
17251687
// not available. We are "propagating" a +1 available value from the store

0 commit comments

Comments
 (0)