Skip to content

Commit ea9d043

Browse files
authored
Merge pull request swiftlang#28190 from gottesmm/pr-0272dcb4ea03c202c874d80b063851eefb55e704
2 parents 72350d8 + 76e243c commit ea9d043

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@ static bool anyMissing(unsigned StartSubElt, unsigned NumSubElts,
387387

388388
namespace {
389389

390+
enum class AvailableValueExpectedOwnership {
391+
Take,
392+
Borrow,
393+
Copy,
394+
};
395+
390396
/// A class that aggregates available values, loading them if they are not
391397
/// available.
392398
class AvailableValueAggregator {
@@ -396,7 +402,7 @@ class AvailableValueAggregator {
396402
MutableArrayRef<AvailableValue> AvailableValueList;
397403
SmallVectorImpl<PMOMemoryUse> &Uses;
398404
DeadEndBlocks &deadEndBlocks;
399-
bool isTake;
405+
AvailableValueExpectedOwnership expectedOwnership;
400406

401407
/// Keep track of all instructions that we have added. Once we are done
402408
/// promoting a value, we need to make sure that if we need to balance any
@@ -408,10 +414,11 @@ class AvailableValueAggregator {
408414
AvailableValueAggregator(SILInstruction *Inst,
409415
MutableArrayRef<AvailableValue> AvailableValueList,
410416
SmallVectorImpl<PMOMemoryUse> &Uses,
411-
DeadEndBlocks &deadEndBlocks, bool isTake)
417+
DeadEndBlocks &deadEndBlocks,
418+
AvailableValueExpectedOwnership expectedOwnership)
412419
: M(Inst->getModule()), B(Inst), Loc(Inst->getLoc()),
413420
AvailableValueList(AvailableValueList), Uses(Uses),
414-
deadEndBlocks(deadEndBlocks), isTake(isTake) {}
421+
deadEndBlocks(deadEndBlocks), expectedOwnership(expectedOwnership) {}
415422

416423
// This is intended to be passed by reference only once constructed.
417424
AvailableValueAggregator(const AvailableValueAggregator &) = delete;
@@ -435,6 +442,19 @@ class AvailableValueAggregator {
435442

436443
void print(llvm::raw_ostream &os) const;
437444
void dump() const LLVM_ATTRIBUTE_USED;
445+
446+
bool isTake() const {
447+
return expectedOwnership == AvailableValueExpectedOwnership::Take;
448+
}
449+
450+
bool isBorrow() const {
451+
return expectedOwnership == AvailableValueExpectedOwnership::Borrow;
452+
}
453+
454+
bool isCopy() const {
455+
return expectedOwnership == AvailableValueExpectedOwnership::Copy;
456+
}
457+
438458
private:
439459
SILValue aggregateFullyAvailableValue(SILType loadTy, unsigned firstElt);
440460
SILValue aggregateTupleSubElts(TupleType *tt, SILType loadTy,
@@ -526,7 +546,7 @@ SILValue AvailableValueAggregator::aggregateValues(SILType LoadTy,
526546
bool isTopLevel) {
527547
// If we are performing a take, make sure that we have available values for
528548
// /all/ of our values. Otherwise, bail.
529-
if (isTopLevel && isTake && !canTake(LoadTy, FirstElt)) {
549+
if (isTopLevel && isTake() && !canTake(LoadTy, FirstElt)) {
530550
return SILValue();
531551
}
532552

@@ -576,7 +596,7 @@ AvailableValueAggregator::aggregateFullyAvailableValue(SILType loadTy,
576596
SILBuilderWithScope builder(insertPts[0], &insertedInsts);
577597
SILLocation loc = insertPts[0]->getLoc();
578598
// If we have a take, just return the value.
579-
if (isTake)
599+
if (isTake())
580600
return firstVal.getValue();
581601
// Otherwise, return a copy of the value.
582602
return builder.emitCopyValueOperation(loc, firstVal.getValue());
@@ -597,7 +617,7 @@ AvailableValueAggregator::aggregateFullyAvailableValue(SILType loadTy,
597617
SILValue eltVal = firstVal.getValue();
598618

599619
// If we are not taking, copy the element value.
600-
if (!isTake) {
620+
if (!isTake()) {
601621
eltVal = builder.emitCopyValueOperation(loc, eltVal);
602622
}
603623

@@ -636,7 +656,7 @@ SILValue AvailableValueAggregator::aggregateTupleSubElts(TupleType *TT,
636656
// compute an address to load from.
637657
SILValue EltAddr;
638658
if (anyMissing(FirstElt, NumSubElt, AvailableValueList)) {
639-
assert(!isTake && "When taking, values should never be missing?!");
659+
assert(!isTake() && "When taking, values should never be missing?!");
640660
EltAddr =
641661
B.createTupleElementAddr(Loc, Address, EltNo, EltTy.getAddressType());
642662
}
@@ -663,7 +683,7 @@ SILValue AvailableValueAggregator::aggregateStructSubElts(StructDecl *sd,
663683
// compute an address to load from.
664684
SILValue eltAddr;
665685
if (anyMissing(firstElt, numSubElt, AvailableValueList)) {
666-
assert(!isTake && "When taking, values should never be missing?!");
686+
assert(!isTake() && "When taking, values should never be missing?!");
667687
eltAddr =
668688
B.createStructElementAddr(Loc, address, decl, eltTy.getAddressType());
669689
}
@@ -686,7 +706,7 @@ SILValue AvailableValueAggregator::handlePrimitiveValue(SILType loadTy,
686706

687707
// If the value is not available, load the value and update our use list.
688708
if (!val) {
689-
assert(!isTake && "Should only take fully available values?!");
709+
assert(!isTake() && "Should only take fully available values?!");
690710
LoadInst *load = ([&]() {
691711
if (B.hasOwnership()) {
692712
return B.createTrivialLoadOr(Loc, address,
@@ -1650,7 +1670,7 @@ bool AllocOptimize::promoteLoadCopy(LoadInst *li) {
16501670
// not available. We are "propagating" a +1 available value from the store
16511671
// points.
16521672
AvailableValueAggregator agg(li, availableValues, Uses, deadEndBlocks,
1653-
false /*isTake*/);
1673+
AvailableValueExpectedOwnership::Copy);
16541674
SILValue newVal = agg.aggregateValues(loadTy, li->getOperand(), firstElt);
16551675

16561676
LLVM_DEBUG(llvm::dbgs() << " *** Promoting load: " << *li << "\n");
@@ -1750,7 +1770,7 @@ bool AllocOptimize::promoteLoadBorrow(LoadBorrowInst *lbi) {
17501770
// not available. We are "propagating" a +1 available value from the store
17511771
// points.
17521772
AvailableValueAggregator agg(lbi, availableValues, Uses, deadEndBlocks,
1753-
false /*isTake*/);
1773+
AvailableValueExpectedOwnership::Borrow);
17541774
SILValue newVal = agg.aggregateValues(loadTy, lbi->getOperand(), firstElt);
17551775

17561776
LLVM_DEBUG(llvm::dbgs() << " *** Promoting load: " << *lbi << "\n");
@@ -1829,7 +1849,7 @@ bool AllocOptimize::canPromoteTake(
18291849
// available, we would need to split stores to promote this destroy_addr. We
18301850
// do not support that yet.
18311851
AvailableValueAggregator agg(inst, tmpList, Uses, deadEndBlocks,
1832-
true /*isTake*/);
1852+
AvailableValueExpectedOwnership::Take);
18331853
if (!agg.canTake(loadTy, firstElt))
18341854
return false;
18351855

@@ -1861,7 +1881,7 @@ void AllocOptimize::promoteDestroyAddr(
18611881
// type as the load did, and emit smaller) loads for any subelements that were
18621882
// not available.
18631883
AvailableValueAggregator agg(dai, availableValues, Uses, deadEndBlocks,
1864-
true /*isTake*/);
1884+
AvailableValueExpectedOwnership::Take);
18651885
SILValue newVal = agg.aggregateValues(loadTy, address, firstElt);
18661886

18671887
++NumDestroyAddrPromoted;
@@ -1889,7 +1909,7 @@ void AllocOptimize::promoteLoadTake(
18891909
// type as the load did, and emit smaller) loads for any subelements that were
18901910
// not available.
18911911
AvailableValueAggregator agg(li, availableValues, Uses, deadEndBlocks,
1892-
true /*isTake*/);
1912+
AvailableValueExpectedOwnership::Take);
18931913
SILValue newVal = agg.aggregateValues(loadTy, address, firstElt);
18941914

18951915
++NumLoadTakePromoted;

0 commit comments

Comments
 (0)