xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-mca/Views/InstructionView.cpp (revision 0d8fe2373503aeac48492f28073049a8bfa4feb5)
1 //===----------------------- View.cpp ---------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This file defines the member functions of the class InstructionView.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include <sstream>
15 #include "Views/InstructionView.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCSubtargetInfo.h"
18 
19 namespace llvm {
20 namespace mca {
21 
22 StringRef InstructionView::printInstructionString(const llvm::MCInst &MCI) const {
23   InstructionString = "";
24   MCIP.printInst(&MCI, 0, "", STI, InstrStream);
25   InstrStream.flush();
26   // Remove any tabs or spaces at the beginning of the instruction.
27   return StringRef(InstructionString).ltrim();
28 }
29 
30 json::Value InstructionView::toJSON() const {
31   json::Object JO;
32   json::Array SourceInfo;
33   for (const auto &MCI : getSource()) {
34     StringRef Instruction = printInstructionString(MCI);
35     SourceInfo.push_back(Instruction.str());
36   }
37   JO.try_emplace("Instructions", std::move(SourceInfo));
38 
39   json::Array Resources;
40   const MCSchedModel &SM = STI.getSchedModel();
41   for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {
42     const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
43     unsigned NumUnits = ProcResource.NumUnits;
44     // Skip groups and invalid resources with zero units.
45     if (ProcResource.SubUnitsIdxBegin || !NumUnits)
46       continue;
47     for (unsigned J = 0; J < NumUnits; ++J) {
48       std::stringstream ResNameStream;
49       ResNameStream << ProcResource.Name;
50       if (NumUnits > 1)
51         ResNameStream << "." << J;
52       Resources.push_back(ResNameStream.str());
53     }
54   }
55   JO.try_emplace("Resources", json::Object({{"CPUName", MCPU}, {"Resources", std::move(Resources)}}));
56 
57   return JO;
58 }
59 } // namespace mca
60 } // namespace llvm
61