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