blob: a6f9153b4945cf2be64881493f094956d304f1ab [file] [log] [blame]
Matt Davis712db512018-06-18 21:38:38 +00001//===--------------------- InstructionInfoView.cpp --------------*- C++ -*-===//
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
9///
Matt Davis712db512018-06-18 21:38:38 +000010/// This file implements the InstructionInfoView API.
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000011///
12//===----------------------------------------------------------------------===//
13
Matt Davis10aa09f2018-08-24 20:24:53 +000014#include "Views/InstructionInfoView.h"
Andrea Di Biagiocbec9af2019-08-09 11:26:27 +000015#include "llvm/Support/FormattedStream.h"
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000016
Fangrui Song5a8fd652018-10-30 15:56:08 +000017namespace llvm {
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000018namespace mca {
19
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000020void InstructionInfoView::printView(raw_ostream &OS) const {
21 std::string Buffer;
22 raw_string_ostream TempStream(Buffer);
23 const MCSchedModel &SM = STI.getSchedModel();
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000024
Andrea Di Biagioa7c3c452018-05-15 15:18:05 +000025 std::string Instruction;
26 raw_string_ostream InstrStream(Instruction);
27
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000028 TempStream << "\n\nInstruction Info:\n";
29 TempStream << "[1]: #uOps\n[2]: Latency\n[3]: RThroughput\n"
Andrea Di Biagiocbec9af2019-08-09 11:26:27 +000030 << "[4]: MayLoad\n[5]: MayStore\n[6]: HasSideEffects (U)\n";
31 if (PrintEncodings) {
32 TempStream << "[7]: Encoding Size\n";
33 TempStream << "\n[1] [2] [3] [4] [5] [6] [7] "
34 << "Encodings: Instructions:\n";
35 } else {
36 TempStream << "\n[1] [2] [3] [4] [5] [6] Instructions:\n";
37 }
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000038
Andrea Di Biagiocbec9af2019-08-09 11:26:27 +000039 for (unsigned I = 0, E = Source.size(); I < E; ++I) {
40 const MCInst &Inst = Source[I];
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000041 const MCInstrDesc &MCDesc = MCII.get(Inst.getOpcode());
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000042
Andrea Di Biagio39e5a562018-06-04 15:43:09 +000043 // Obtain the scheduling class information from the instruction.
44 unsigned SchedClassID = MCDesc.getSchedClass();
45 unsigned CPUID = SM.getProcessorID();
46
47 // Try to solve variant scheduling classes.
48 while (SchedClassID && SM.getSchedClassDesc(SchedClassID)->isVariant())
49 SchedClassID = STI.resolveVariantSchedClass(SchedClassID, &Inst, CPUID);
50
51 const MCSchedClassDesc &SCDesc = *SM.getSchedClassDesc(SchedClassID);
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000052 unsigned NumMicroOpcodes = SCDesc.NumMicroOps;
53 unsigned Latency = MCSchedModel::computeInstrLatency(STI, SCDesc);
Andrea Di Biagiod768d352019-01-23 16:35:07 +000054 // Add extra latency due to delays in the forwarding data paths.
55 Latency += MCSchedModel::getForwardingDelayCycles(
56 STI.getReadAdvanceEntries(SCDesc));
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +000057 Optional<double> RThroughput =
58 MCSchedModel::getReciprocalThroughput(STI, SCDesc);
59
60 TempStream << ' ' << NumMicroOpcodes << " ";
61 if (NumMicroOpcodes < 10)
62 TempStream << " ";
63 else if (NumMicroOpcodes < 100)
64 TempStream << ' ';
65 TempStream << Latency << " ";
66 if (Latency < 10)
67 TempStream << " ";
68 else if (Latency < 100)
69 TempStream << ' ';
70
71 if (RThroughput.hasValue()) {
72 double RT = RThroughput.getValue();
73 TempStream << format("%.2f", RT) << ' ';
74 if (RT < 10.0)
75 TempStream << " ";
76 else if (RT < 100.0)
77 TempStream << ' ';
78 } else {
79 TempStream << " - ";
80 }
81 TempStream << (MCDesc.mayLoad() ? " * " : " ");
82 TempStream << (MCDesc.mayStore() ? " * " : " ");
Andrea Di Biagiocbec9af2019-08-09 11:26:27 +000083 TempStream << (MCDesc.hasUnmodeledSideEffects() ? " U " : " ");
84
85 if (PrintEncodings) {
86 StringRef Encoding(CE.getEncoding(I));
87 unsigned EncodingSize = Encoding.size();
88 TempStream << " " << EncodingSize
89 << (EncodingSize < 10 ? " " : " ");
90 TempStream.flush();
91 formatted_raw_ostream FOS(TempStream);
92 for (unsigned i = 0, e = Encoding.size(); i != e; ++i)
93 FOS << format("%02x ", (uint8_t)Encoding[i]);
94 FOS.PadToColumn(30);
95 FOS.flush();
96 }
Andrea Di Biagioa7c3c452018-05-15 15:18:05 +000097
98 MCIP.printInst(&Inst, InstrStream, "", STI);
99 InstrStream.flush();
100
101 // Consume any tabs or spaces at the beginning of the string.
102 StringRef Str(Instruction);
103 Str = Str.ltrim();
Andrea Di Biagiocbec9af2019-08-09 11:26:27 +0000104 TempStream << Str << '\n';
Andrea Di Biagioa7c3c452018-05-15 15:18:05 +0000105 Instruction = "";
Andrea Di Biagiodf5d9482018-03-23 19:40:04 +0000106 }
107
108 TempStream.flush();
109 OS << Buffer;
110}
111} // namespace mca.
Fangrui Song5a8fd652018-10-30 15:56:08 +0000112} // namespace llvm