Skip to content

Commit 1c308d6

Browse files
committed
[LV] Clean up LoopVectorizationCostModel::calculateRegisterUsage. NFC
Minor refactoring in LoopVectorizationCostModel::calculateRegisterUsage. Also adding some FIXME:s related to what appears to be some short comings related to how the register usage is calculated. Differential Revision: https://reviews.llvm.org/D138342
1 parent 294fdd9 commit 1c308d6

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5951,8 +5951,9 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
59515951
IntervalMap EndPoint;
59525952
// Saves the list of instruction indices that are used in the loop.
59535953
SmallPtrSet<Instruction *, 8> Ends;
5954-
// Saves the list of values that are used in the loop but are
5955-
// defined outside the loop, such as arguments and constants.
5954+
// Saves the list of values that are used in the loop but are defined outside
5955+
// the loop (not including non-instruction values such as arguments and
5956+
// constants).
59565957
SmallPtrSet<Value *, 8> LoopInvariants;
59575958

59585959
for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO())) {
@@ -5964,6 +5965,9 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
59645965
auto *Instr = dyn_cast<Instruction>(U);
59655966

59665967
// Ignore non-instruction values such as arguments, constants, etc.
5968+
// FIXME: Might need some motivation why these values are ignored. If
5969+
// for example an argument is used inside the loop it will increase the
5970+
// register pressure (so shouldn't we add it to LoopInvariants).
59675971
if (!Instr)
59685972
continue;
59695973

@@ -6019,14 +6023,19 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
60196023

60206024
// For each VF find the maximum usage of registers.
60216025
for (unsigned j = 0, e = VFs.size(); j < e; ++j) {
6022-
// Count the number of live intervals.
6026+
// Count the number of registers used, per register class, given all open
6027+
// intervals.
6028+
// Note that elements in this SmallMapVector will be default constructed
6029+
// as 0. So we can use "RegUsage[ClassID] += n" in the code below even if
6030+
// there is no previous entry for ClassID.
60236031
SmallMapVector<unsigned, unsigned, 4> RegUsage;
60246032

60256033
if (VFs[j].isScalar()) {
60266034
for (auto *Inst : OpenIntervals) {
6027-
unsigned ClassID = TTI.getRegisterClassForType(false, Inst->getType());
6028-
// If RegUsage[ClassID] doesn't exist, it will be default
6029-
// constructed as 0 before the addition
6035+
unsigned ClassID =
6036+
TTI.getRegisterClassForType(false, Inst->getType());
6037+
// FIXME: The target might use more than one register for the type
6038+
// even in the scalar case.
60306039
RegUsage[ClassID] += 1;
60316040
}
60326041
} else {
@@ -6036,14 +6045,14 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
60366045
if (VecValuesToIgnore.count(Inst))
60376046
continue;
60386047
if (isScalarAfterVectorization(Inst, VFs[j])) {
6039-
unsigned ClassID = TTI.getRegisterClassForType(false, Inst->getType());
6040-
// If RegUsage[ClassID] doesn't exist, it will be default
6041-
// constructed as 0 before the addition
6048+
unsigned ClassID =
6049+
TTI.getRegisterClassForType(false, Inst->getType());
6050+
// FIXME: The target might use more than one register for the type
6051+
// even in the scalar case.
60426052
RegUsage[ClassID] += 1;
60436053
} else {
6044-
unsigned ClassID = TTI.getRegisterClassForType(true, Inst->getType());
6045-
// If RegUsage[ClassID] doesn't exist, it will be default
6046-
// constructed as 0 before the addition
6054+
unsigned ClassID =
6055+
TTI.getRegisterClassForType(true, Inst->getType());
60476056
RegUsage[ClassID] += GetRegUsage(Inst->getType(), VFs[j]);
60486057
}
60496058
}
@@ -6063,17 +6072,19 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
60636072
}
60646073

60656074
for (unsigned i = 0, e = VFs.size(); i < e; ++i) {
6075+
// Note that elements in this SmallMapVector will be default constructed
6076+
// as 0. So we can use "Invariant[ClassID] += n" in the code below even if
6077+
// there is no previous entry for ClassID.
60666078
SmallMapVector<unsigned, unsigned, 4> Invariant;
60676079

60686080
for (auto *Inst : LoopInvariants) {
6081+
// FIXME: The target might use more than one register for the type
6082+
// even in the scalar case.
60696083
unsigned Usage =
60706084
VFs[i].isScalar() ? 1 : GetRegUsage(Inst->getType(), VFs[i]);
60716085
unsigned ClassID =
60726086
TTI.getRegisterClassForType(VFs[i].isVector(), Inst->getType());
6073-
if (Invariant.find(ClassID) == Invariant.end())
6074-
Invariant[ClassID] = Usage;
6075-
else
6076-
Invariant[ClassID] += Usage;
6087+
Invariant[ClassID] += Usage;
60776088
}
60786089

60796090
LLVM_DEBUG({

0 commit comments

Comments
 (0)