1 //===--------------------- InstructionInfoView.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 implements the InstructionInfoView API.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "Views/InstructionInfoView.h"
15 #include "llvm/Support/FormattedStream.h"
16 
17 namespace llvm {
18 namespace mca {
19 
printView(raw_ostream & OS) const20 void InstructionInfoView::printView(raw_ostream &OS) const {
21   std::string Buffer;
22   raw_string_ostream TempStream(Buffer);
23 
24   ArrayRef<llvm::MCInst> Source = getSource();
25   if (!Source.size())
26     return;
27 
28   IIVDVec IIVD(Source.size());
29   collectData(IIVD);
30 
31   TempStream << "\n\nInstruction Info:\n";
32   TempStream << "[1]: #uOps\n[2]: Latency\n[3]: RThroughput\n"
33              << "[4]: MayLoad\n[5]: MayStore\n[6]: HasSideEffects (U)\n";
34   if (PrintEncodings) {
35     TempStream << "[7]: Encoding Size\n";
36     TempStream << "\n[1]    [2]    [3]    [4]    [5]    [6]    [7]    "
37                << "Encodings:                    Instructions:\n";
38   } else {
39     TempStream << "\n[1]    [2]    [3]    [4]    [5]    [6]    Instructions:\n";
40   }
41 
42   for (auto I : enumerate(zip(IIVD, Source))) {
43     const InstructionInfoViewData &IIVDEntry = std::get<0>(I.value());
44 
45     TempStream << ' ' << IIVDEntry.NumMicroOpcodes << "    ";
46     if (IIVDEntry.NumMicroOpcodes < 10)
47       TempStream << "  ";
48     else if (IIVDEntry.NumMicroOpcodes < 100)
49       TempStream << ' ';
50     TempStream << IIVDEntry.Latency << "   ";
51     if (IIVDEntry.Latency < 10)
52       TempStream << "  ";
53     else if (IIVDEntry.Latency < 100)
54       TempStream << ' ';
55 
56     if (IIVDEntry.RThroughput.hasValue()) {
57       double RT = IIVDEntry.RThroughput.getValue();
58       TempStream << format("%.2f", RT) << ' ';
59       if (RT < 10.0)
60         TempStream << "  ";
61       else if (RT < 100.0)
62         TempStream << ' ';
63     } else {
64       TempStream << " -     ";
65     }
66     TempStream << (IIVDEntry.mayLoad ? " *     " : "       ");
67     TempStream << (IIVDEntry.mayStore ? " *     " : "       ");
68     TempStream << (IIVDEntry.hasUnmodeledSideEffects ? " U     " : "       ");
69 
70     if (PrintEncodings) {
71       StringRef Encoding(CE.getEncoding(I.index()));
72       unsigned EncodingSize = Encoding.size();
73       TempStream << " " << EncodingSize
74                  << (EncodingSize < 10 ? "     " : "    ");
75       TempStream.flush();
76       formatted_raw_ostream FOS(TempStream);
77       for (unsigned i = 0, e = Encoding.size(); i != e; ++i)
78         FOS << format("%02x ", (uint8_t)Encoding[i]);
79       FOS.PadToColumn(30);
80       FOS.flush();
81     }
82 
83     const MCInst &Inst = std::get<1>(I.value());
84     TempStream << printInstructionString(Inst) << '\n';
85   }
86 
87   TempStream.flush();
88   OS << Buffer;
89 }
90 
collectData(MutableArrayRef<InstructionInfoViewData> IIVD) const91 void InstructionInfoView::collectData(
92     MutableArrayRef<InstructionInfoViewData> IIVD) const {
93   const llvm::MCSubtargetInfo &STI = getSubTargetInfo();
94   const MCSchedModel &SM = STI.getSchedModel();
95   for (auto I : zip(getSource(), IIVD)) {
96     const MCInst &Inst = std::get<0>(I);
97     InstructionInfoViewData &IIVDEntry = std::get<1>(I);
98     const MCInstrDesc &MCDesc = MCII.get(Inst.getOpcode());
99 
100     // Obtain the scheduling class information from the instruction.
101     unsigned SchedClassID = MCDesc.getSchedClass();
102     unsigned CPUID = SM.getProcessorID();
103 
104     // Try to solve variant scheduling classes.
105     while (SchedClassID && SM.getSchedClassDesc(SchedClassID)->isVariant())
106       SchedClassID =
107           STI.resolveVariantSchedClass(SchedClassID, &Inst, &MCII, CPUID);
108 
109     const MCSchedClassDesc &SCDesc = *SM.getSchedClassDesc(SchedClassID);
110     IIVDEntry.NumMicroOpcodes = SCDesc.NumMicroOps;
111     IIVDEntry.Latency = MCSchedModel::computeInstrLatency(STI, SCDesc);
112     // Add extra latency due to delays in the forwarding data paths.
113     IIVDEntry.Latency += MCSchedModel::getForwardingDelayCycles(
114         STI.getReadAdvanceEntries(SCDesc));
115     IIVDEntry.RThroughput = MCSchedModel::getReciprocalThroughput(STI, SCDesc);
116     IIVDEntry.mayLoad = MCDesc.mayLoad();
117     IIVDEntry.mayStore = MCDesc.mayStore();
118     IIVDEntry.hasUnmodeledSideEffects = MCDesc.hasUnmodeledSideEffects();
119   }
120 }
121 } // namespace mca.
122 } // namespace llvm
123