xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-mca/Views/InstructionInfoView.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===--------------------- InstructionInfoView.cpp --------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric /// \file
90b57cec5SDimitry Andric ///
100b57cec5SDimitry Andric /// This file implements the InstructionInfoView API.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "Views/InstructionInfoView.h"
15*8bcb0991SDimitry Andric #include "llvm/Support/FormattedStream.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric namespace llvm {
180b57cec5SDimitry Andric namespace mca {
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric void InstructionInfoView::printView(raw_ostream &OS) const {
210b57cec5SDimitry Andric   std::string Buffer;
220b57cec5SDimitry Andric   raw_string_ostream TempStream(Buffer);
230b57cec5SDimitry Andric   const MCSchedModel &SM = STI.getSchedModel();
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric   std::string Instruction;
260b57cec5SDimitry Andric   raw_string_ostream InstrStream(Instruction);
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric   TempStream << "\n\nInstruction Info:\n";
290b57cec5SDimitry Andric   TempStream << "[1]: #uOps\n[2]: Latency\n[3]: RThroughput\n"
30*8bcb0991SDimitry Andric              << "[4]: MayLoad\n[5]: MayStore\n[6]: HasSideEffects (U)\n";
31*8bcb0991SDimitry Andric   if (PrintEncodings) {
32*8bcb0991SDimitry Andric     TempStream << "[7]: Encoding Size\n";
33*8bcb0991SDimitry Andric     TempStream << "\n[1]    [2]    [3]    [4]    [5]    [6]    [7]    "
34*8bcb0991SDimitry Andric                << "Encodings:                    Instructions:\n";
35*8bcb0991SDimitry Andric   } else {
36*8bcb0991SDimitry Andric     TempStream << "\n[1]    [2]    [3]    [4]    [5]    [6]    Instructions:\n";
37*8bcb0991SDimitry Andric   }
380b57cec5SDimitry Andric 
39*8bcb0991SDimitry Andric   for (unsigned I = 0, E = Source.size(); I < E; ++I) {
40*8bcb0991SDimitry Andric     const MCInst &Inst = Source[I];
410b57cec5SDimitry Andric     const MCInstrDesc &MCDesc = MCII.get(Inst.getOpcode());
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric     // Obtain the scheduling class information from the instruction.
440b57cec5SDimitry Andric     unsigned SchedClassID = MCDesc.getSchedClass();
450b57cec5SDimitry Andric     unsigned CPUID = SM.getProcessorID();
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric     // Try to solve variant scheduling classes.
480b57cec5SDimitry Andric     while (SchedClassID && SM.getSchedClassDesc(SchedClassID)->isVariant())
490b57cec5SDimitry Andric       SchedClassID = STI.resolveVariantSchedClass(SchedClassID, &Inst, CPUID);
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric     const MCSchedClassDesc &SCDesc = *SM.getSchedClassDesc(SchedClassID);
520b57cec5SDimitry Andric     unsigned NumMicroOpcodes = SCDesc.NumMicroOps;
530b57cec5SDimitry Andric     unsigned Latency = MCSchedModel::computeInstrLatency(STI, SCDesc);
540b57cec5SDimitry Andric     // Add extra latency due to delays in the forwarding data paths.
550b57cec5SDimitry Andric     Latency += MCSchedModel::getForwardingDelayCycles(
560b57cec5SDimitry Andric         STI.getReadAdvanceEntries(SCDesc));
570b57cec5SDimitry Andric     Optional<double> RThroughput =
580b57cec5SDimitry Andric         MCSchedModel::getReciprocalThroughput(STI, SCDesc);
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric     TempStream << ' ' << NumMicroOpcodes << "    ";
610b57cec5SDimitry Andric     if (NumMicroOpcodes < 10)
620b57cec5SDimitry Andric       TempStream << "  ";
630b57cec5SDimitry Andric     else if (NumMicroOpcodes < 100)
640b57cec5SDimitry Andric       TempStream << ' ';
650b57cec5SDimitry Andric     TempStream << Latency << "   ";
660b57cec5SDimitry Andric     if (Latency < 10)
670b57cec5SDimitry Andric       TempStream << "  ";
680b57cec5SDimitry Andric     else if (Latency < 100)
690b57cec5SDimitry Andric       TempStream << ' ';
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric     if (RThroughput.hasValue()) {
720b57cec5SDimitry Andric       double RT = RThroughput.getValue();
730b57cec5SDimitry Andric       TempStream << format("%.2f", RT) << ' ';
740b57cec5SDimitry Andric       if (RT < 10.0)
750b57cec5SDimitry Andric         TempStream << "  ";
760b57cec5SDimitry Andric       else if (RT < 100.0)
770b57cec5SDimitry Andric         TempStream << ' ';
780b57cec5SDimitry Andric     } else {
790b57cec5SDimitry Andric       TempStream << " -     ";
800b57cec5SDimitry Andric     }
810b57cec5SDimitry Andric     TempStream << (MCDesc.mayLoad() ? " *     " : "       ");
820b57cec5SDimitry Andric     TempStream << (MCDesc.mayStore() ? " *     " : "       ");
830b57cec5SDimitry Andric     TempStream << (MCDesc.hasUnmodeledSideEffects() ? " U     " : "       ");
840b57cec5SDimitry Andric 
85*8bcb0991SDimitry Andric     if (PrintEncodings) {
86*8bcb0991SDimitry Andric       StringRef Encoding(CE.getEncoding(I));
87*8bcb0991SDimitry Andric       unsigned EncodingSize = Encoding.size();
88*8bcb0991SDimitry Andric       TempStream << " " << EncodingSize
89*8bcb0991SDimitry Andric                  << (EncodingSize < 10 ? "     " : "    ");
90*8bcb0991SDimitry Andric       TempStream.flush();
91*8bcb0991SDimitry Andric       formatted_raw_ostream FOS(TempStream);
92*8bcb0991SDimitry Andric       for (unsigned i = 0, e = Encoding.size(); i != e; ++i)
93*8bcb0991SDimitry Andric         FOS << format("%02x ", (uint8_t)Encoding[i]);
94*8bcb0991SDimitry Andric       FOS.PadToColumn(30);
95*8bcb0991SDimitry Andric       FOS.flush();
96*8bcb0991SDimitry Andric     }
97*8bcb0991SDimitry Andric 
980b57cec5SDimitry Andric     MCIP.printInst(&Inst, InstrStream, "", STI);
990b57cec5SDimitry Andric     InstrStream.flush();
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric     // Consume any tabs or spaces at the beginning of the string.
1020b57cec5SDimitry Andric     StringRef Str(Instruction);
1030b57cec5SDimitry Andric     Str = Str.ltrim();
104*8bcb0991SDimitry Andric     TempStream << Str << '\n';
1050b57cec5SDimitry Andric     Instruction = "";
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   TempStream.flush();
1090b57cec5SDimitry Andric   OS << Buffer;
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric } // namespace mca.
1120b57cec5SDimitry Andric } // namespace llvm
113