aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/tools/llvm-mca/Views/RegisterFileStatistics.cpp
blob: 4ef8053bff4108c4631436170d59a585b9ff8e91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//===--------------------- RegisterFileStatistics.cpp -----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
/// \file
///
/// This file implements the RegisterFileStatistics interface.
///
//===----------------------------------------------------------------------===//

#include "Views/RegisterFileStatistics.h"
#include "llvm/Support/Format.h"

namespace llvm {
namespace mca {

RegisterFileStatistics::RegisterFileStatistics(const MCSubtargetInfo &sti)
    : STI(sti) {
  const MCSchedModel &SM = STI.getSchedModel();
  RegisterFileUsage RFUEmpty = {0, 0, 0};
  MoveEliminationInfo MEIEmpty = {0, 0, 0, 0, 0};
  if (!SM.hasExtraProcessorInfo()) {
    // Assume a single register file.
    PRFUsage.emplace_back(RFUEmpty);
    MoveElimInfo.emplace_back(MEIEmpty);
    return;
  }

  // Initialize a RegisterFileUsage for every user defined register file, plus
  // the default register file which is always at index #0.
  const MCExtraProcessorInfo &PI = SM.getExtraProcessorInfo();
  // There is always an "InvalidRegisterFile" entry in tablegen. That entry can
  // be skipped. If there are no user defined register files, then reserve a
  // single entry for the default register file at index #0.
  unsigned NumRegFiles = std::max(PI.NumRegisterFiles, 1U);

  PRFUsage.resize(NumRegFiles);
  std::fill(PRFUsage.begin(), PRFUsage.end(), RFUEmpty);

  MoveElimInfo.resize(NumRegFiles);
  std::fill(MoveElimInfo.begin(), MoveElimInfo.end(), MEIEmpty);
}

void RegisterFileStatistics::updateRegisterFileUsage(
    ArrayRef<unsigned> UsedPhysRegs) {
  for (unsigned I = 0, E = PRFUsage.size(); I < E; ++I) {
    RegisterFileUsage &RFU = PRFUsage[I];
    unsigned NumUsedPhysRegs = UsedPhysRegs[I];
    RFU.CurrentlyUsedMappings += NumUsedPhysRegs;
    RFU.TotalMappings += NumUsedPhysRegs;
    RFU.MaxUsedMappings =
        std::max(RFU.MaxUsedMappings, RFU.CurrentlyUsedMappings);
  }
}

void RegisterFileStatistics::updateMoveElimInfo(const Instruction &Inst) {
  if (!Inst.isOptimizableMove())
    return;

  if (Inst.getDefs().size() != Inst.getUses().size())
    return;

  for (size_t I = 0, E = Inst.getDefs().size(); I < E; ++I) {
    const WriteState &WS = Inst.getDefs()[I];
    const ReadState &RS = Inst.getUses()[E - (I + 1)];

    MoveEliminationInfo &Info =
        MoveElimInfo[Inst.getDefs()[0].getRegisterFileID()];
    Info.TotalMoveEliminationCandidates++;
    if (WS.isEliminated())
      Info.CurrentMovesEliminated++;
    if (WS.isWriteZero() && RS.isReadZero())
      Info.TotalMovesThatPropagateZero++;
  }
}

void RegisterFileStatistics::onEvent(const HWInstructionEvent &Event) {
  switch (Event.Type) {
  default:
    break;
  case HWInstructionEvent::Retired: {
    const auto &RE = static_cast<const HWInstructionRetiredEvent &>(Event);
    for (unsigned I = 0, E = PRFUsage.size(); I < E; ++I)
      PRFUsage[I].CurrentlyUsedMappings -= RE.FreedPhysRegs[I];
    break;
  }
  case HWInstructionEvent::Dispatched: {
    const auto &DE = static_cast<const HWInstructionDispatchedEvent &>(Event);
    updateRegisterFileUsage(DE.UsedPhysRegs);
    updateMoveElimInfo(*DE.IR.getInstruction());
  }
  }
}

void RegisterFileStatistics::onCycleEnd() {
  for (MoveEliminationInfo &MEI : MoveElimInfo) {
    unsigned &CurrentMax = MEI.MaxMovesEliminatedPerCycle;
    CurrentMax = std::max(CurrentMax, MEI.CurrentMovesEliminated);
    MEI.TotalMovesEliminated += MEI.CurrentMovesEliminated;
    MEI.CurrentMovesEliminated = 0;
  }
}

void RegisterFileStatistics::printView(raw_ostream &OS) const {
  std::string Buffer;
  raw_string_ostream TempStream(Buffer);

  TempStream << "\n\nRegister File statistics:";
  const RegisterFileUsage &GlobalUsage = PRFUsage[0];
  TempStream << "\nTotal number of mappings created:    "
             << GlobalUsage.TotalMappings;
  TempStream << "\nMax number of mappings used:         "
             << GlobalUsage.MaxUsedMappings << '\n';

  for (unsigned I = 1, E = PRFUsage.size(); I < E; ++I) {
    const RegisterFileUsage &RFU = PRFUsage[I];
    // Obtain the register file descriptor from the scheduling model.
    assert(STI.getSchedModel().hasExtraProcessorInfo() &&
           "Unable to find register file info!");
    const MCExtraProcessorInfo &PI =
        STI.getSchedModel().getExtraProcessorInfo();
    assert(I <= PI.NumRegisterFiles && "Unexpected register file index!");
    const MCRegisterFileDesc &RFDesc = PI.RegisterFiles[I];
    // Skip invalid register files.
    if (!RFDesc.NumPhysRegs)
      continue;

    TempStream << "\n*  Register File #" << I;
    TempStream << " -- " << StringRef(RFDesc.Name) << ':';
    TempStream << "\n   Number of physical registers:     ";
    if (!RFDesc.NumPhysRegs)
      TempStream << "unbounded";
    else
      TempStream << RFDesc.NumPhysRegs;
    TempStream << "\n   Total number of mappings created: "
               << RFU.TotalMappings;
    TempStream << "\n   Max number of mappings used:      "
               << RFU.MaxUsedMappings << '\n';
    const MoveEliminationInfo &MEI = MoveElimInfo[I];

    if (MEI.TotalMoveEliminationCandidates) {
      TempStream << "   Number of optimizable moves:      "
                 << MEI.TotalMoveEliminationCandidates;
      double EliminatedMovProportion = (double)MEI.TotalMovesEliminated /
                                       MEI.TotalMoveEliminationCandidates *
                                       100.0;
      double ZeroMovProportion = (double)MEI.TotalMovesThatPropagateZero /
                                 MEI.TotalMoveEliminationCandidates * 100.0;
      TempStream << "\n   Number of moves eliminated:       "
                 << MEI.TotalMovesEliminated << "  "
                 << format("(%.1f%%)",
                           floor((EliminatedMovProportion * 10) + 0.5) / 10);
      TempStream << "\n   Number of zero moves:             "
                 << MEI.TotalMovesThatPropagateZero << "  "
                 << format("(%.1f%%)",
                           floor((ZeroMovProportion * 10) + 0.5) / 10);
      TempStream << "\n   Max moves eliminated per cycle:   "
                 << MEI.MaxMovesEliminatedPerCycle << '\n';
    }
  }

  TempStream.flush();
  OS << Buffer;
}

} // namespace mca
} // namespace llvm