@@ -5951,8 +5951,9 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
5951
5951
IntervalMap EndPoint;
5952
5952
// Saves the list of instruction indices that are used in the loop.
5953
5953
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).
5956
5957
SmallPtrSet<Value *, 8 > LoopInvariants;
5957
5958
5958
5959
for (BasicBlock *BB : make_range (DFS.beginRPO (), DFS.endRPO ())) {
@@ -5964,6 +5965,9 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
5964
5965
auto *Instr = dyn_cast<Instruction>(U);
5965
5966
5966
5967
// 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).
5967
5971
if (!Instr)
5968
5972
continue ;
5969
5973
@@ -6019,14 +6023,19 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
6019
6023
6020
6024
// For each VF find the maximum usage of registers.
6021
6025
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.
6023
6031
SmallMapVector<unsigned , unsigned , 4 > RegUsage;
6024
6032
6025
6033
if (VFs[j].isScalar ()) {
6026
6034
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.
6030
6039
RegUsage[ClassID] += 1 ;
6031
6040
}
6032
6041
} else {
@@ -6036,14 +6045,14 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
6036
6045
if (VecValuesToIgnore.count (Inst))
6037
6046
continue ;
6038
6047
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.
6042
6052
RegUsage[ClassID] += 1 ;
6043
6053
} 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 ());
6047
6056
RegUsage[ClassID] += GetRegUsage (Inst->getType (), VFs[j]);
6048
6057
}
6049
6058
}
@@ -6063,17 +6072,19 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
6063
6072
}
6064
6073
6065
6074
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.
6066
6078
SmallMapVector<unsigned , unsigned , 4 > Invariant;
6067
6079
6068
6080
for (auto *Inst : LoopInvariants) {
6081
+ // FIXME: The target might use more than one register for the type
6082
+ // even in the scalar case.
6069
6083
unsigned Usage =
6070
6084
VFs[i].isScalar () ? 1 : GetRegUsage (Inst->getType (), VFs[i]);
6071
6085
unsigned ClassID =
6072
6086
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;
6077
6088
}
6078
6089
6079
6090
LLVM_DEBUG ({
0 commit comments