Skip to content

Commit b5e3b3b

Browse files
committed
[NFC] PredictableMemOpts: refactor isFullyAvailable.
For use as a general utility.
1 parent 89708dd commit b5e3b3b

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,29 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const AvailableValue &V) {
320320
// Subelement Extraction
321321
//===----------------------------------------------------------------------===//
322322

323+
static bool isFullyAvailable(SILType loadTy, unsigned firstElt,
324+
ArrayRef<AvailableValue> AvailableValues) {
325+
if (firstElt >= AvailableValues.size()) { // #Elements may be zero.
326+
return false;
327+
}
328+
329+
auto &firstVal = AvailableValues[firstElt];
330+
331+
// Make sure that the first element is available and is the correct type.
332+
if (!firstVal || firstVal.getType() != loadTy)
333+
return false;
334+
335+
auto *function = firstVal.getValue()->getFunction();
336+
return llvm::all_of(
337+
range(getNumSubElements(loadTy, function->getModule(),
338+
TypeExpansionContext(*function))),
339+
[&](unsigned index) -> bool {
340+
auto &val = AvailableValues[firstElt + index];
341+
return val.getValue() == firstVal.getValue() &&
342+
val.getSubElementNumber() == index;
343+
});
344+
}
345+
323346
/// Given an aggregate value and an access path, non-destructively extract the
324347
/// value indicated by the path.
325348
static SILValue nonDestructivelyExtractSubElement(const AvailableValue &Val,
@@ -493,7 +516,6 @@ class AvailableValueAggregator {
493516
SILValue address, unsigned firstElt);
494517
SILValue handlePrimitiveValue(SILType loadTy, SILValue address,
495518
unsigned firstElt);
496-
bool isFullyAvailable(SILType loadTy, unsigned firstElt) const;
497519

498520

499521
/// If as a result of us copying values, we may have unconsumed destroys, find
@@ -520,27 +542,6 @@ void AvailableValueAggregator::print(llvm::raw_ostream &os) const {
520542
}
521543
}
522544

523-
bool AvailableValueAggregator::isFullyAvailable(SILType loadTy,
524-
unsigned firstElt) const {
525-
if (firstElt >= AvailableValueList.size()) { // #Elements may be zero.
526-
return false;
527-
}
528-
529-
auto &firstVal = AvailableValueList[firstElt];
530-
531-
// Make sure that the first element is available and is the correct type.
532-
if (!firstVal || firstVal.getType() != loadTy)
533-
return false;
534-
535-
return llvm::all_of(range(getNumSubElements(
536-
loadTy, M, TypeExpansionContext(B.getFunction()))),
537-
[&](unsigned index) -> bool {
538-
auto &val = AvailableValueList[firstElt + index];
539-
return val.getValue() == firstVal.getValue() &&
540-
val.getSubElementNumber() == index;
541-
});
542-
}
543-
544545
// We can only take if we never have to split a larger value to promote this
545546
// address.
546547
bool AvailableValueAggregator::canTake(SILType loadTy,
@@ -552,7 +553,7 @@ bool AvailableValueAggregator::canTake(SILType loadTy,
552553
return true;
553554

554555
// If we are trivially fully available, just return true.
555-
if (isFullyAvailable(loadTy, firstElt))
556+
if (isFullyAvailable(loadTy, firstElt, AvailableValueList))
556557
return true;
557558

558559
// Otherwise see if we are an aggregate with fully available leaf types.
@@ -659,7 +660,7 @@ SILValue
659660
AvailableValueAggregator::aggregateFullyAvailableValue(SILType loadTy,
660661
unsigned firstElt) {
661662
// Check if our underlying type is fully available. If it isn't, bail.
662-
if (!isFullyAvailable(loadTy, firstElt))
663+
if (!isFullyAvailable(loadTy, firstElt, AvailableValueList))
663664
return SILValue();
664665

665666
// Ok, grab out first value. (note: any actually will do).

0 commit comments

Comments
 (0)