xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-mca/Views/InstructionView.h (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1*e8d8bef9SDimitry Andric //===----------------------- InstrucionView.h -----------------------------*- 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 main interface for Views that examine and reference
11*e8d8bef9SDimitry Andric /// a sequence of machine instructions.
12*e8d8bef9SDimitry Andric ///
13*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
14*e8d8bef9SDimitry Andric 
15*e8d8bef9SDimitry Andric #ifndef LLVM_TOOLS_LLVM_MCA_INSTRUCTIONVIEW_H
16*e8d8bef9SDimitry Andric #define LLVM_TOOLS_LLVM_MCA_INSTRUCTIONVIEW_H
17*e8d8bef9SDimitry Andric 
18*e8d8bef9SDimitry Andric #include "Views/View.h"
19*e8d8bef9SDimitry Andric #include "llvm/MC/MCInstPrinter.h"
20*e8d8bef9SDimitry Andric #include "llvm/Support/raw_ostream.h"
21*e8d8bef9SDimitry Andric #include "llvm/Support/JSON.h"
22*e8d8bef9SDimitry Andric 
23*e8d8bef9SDimitry Andric namespace llvm {
24*e8d8bef9SDimitry Andric namespace mca {
25*e8d8bef9SDimitry Andric 
26*e8d8bef9SDimitry Andric // The base class for views that deal with individual machine instructions.
27*e8d8bef9SDimitry Andric class InstructionView : public View {
28*e8d8bef9SDimitry Andric   const llvm::MCSubtargetInfo &STI;
29*e8d8bef9SDimitry Andric   llvm::MCInstPrinter &MCIP;
30*e8d8bef9SDimitry Andric   llvm::ArrayRef<llvm::MCInst> Source;
31*e8d8bef9SDimitry Andric   StringRef MCPU;
32*e8d8bef9SDimitry Andric 
33*e8d8bef9SDimitry Andric   mutable std::string InstructionString;
34*e8d8bef9SDimitry Andric   mutable raw_string_ostream InstrStream;
35*e8d8bef9SDimitry Andric 
36*e8d8bef9SDimitry Andric public:
37*e8d8bef9SDimitry Andric   void printView(llvm::raw_ostream &) const override {}
38*e8d8bef9SDimitry Andric   InstructionView(const llvm::MCSubtargetInfo &STI,
39*e8d8bef9SDimitry Andric                   llvm::MCInstPrinter &Printer,
40*e8d8bef9SDimitry Andric                   llvm::ArrayRef<llvm::MCInst> S,
41*e8d8bef9SDimitry Andric                   StringRef MCPU = StringRef())
42*e8d8bef9SDimitry Andric       : STI(STI), MCIP(Printer), Source(S), MCPU(MCPU),
43*e8d8bef9SDimitry Andric         InstrStream(InstructionString) {}
44*e8d8bef9SDimitry Andric 
45*e8d8bef9SDimitry Andric   virtual ~InstructionView() = default;
46*e8d8bef9SDimitry Andric 
47*e8d8bef9SDimitry Andric   StringRef getNameAsString() const override {
48*e8d8bef9SDimitry Andric     return "Instructions and CPU resources";
49*e8d8bef9SDimitry Andric   }
50*e8d8bef9SDimitry Andric   // Return a reference to a string representing a given machine instruction.
51*e8d8bef9SDimitry Andric   // The result should be used or copied before the next call to
52*e8d8bef9SDimitry Andric   // printInstructionString() as it will overwrite the previous result.
53*e8d8bef9SDimitry Andric   StringRef printInstructionString(const llvm::MCInst &MCI) const;
54*e8d8bef9SDimitry Andric   const llvm::MCSubtargetInfo &getSubTargetInfo() const { return STI; }
55*e8d8bef9SDimitry Andric 
56*e8d8bef9SDimitry Andric   llvm::MCInstPrinter &getInstPrinter() const { return MCIP; }
57*e8d8bef9SDimitry Andric   llvm::ArrayRef<llvm::MCInst> getSource() const { return Source; }
58*e8d8bef9SDimitry Andric   json::Value toJSON() const override;
59*e8d8bef9SDimitry Andric   virtual void printViewJSON(llvm::raw_ostream &OS) override {
60*e8d8bef9SDimitry Andric     json::Value JV = toJSON();
61*e8d8bef9SDimitry Andric     OS << formatv("{0:2}", JV) << "\n";
62*e8d8bef9SDimitry Andric   }
63*e8d8bef9SDimitry Andric };
64*e8d8bef9SDimitry Andric } // namespace mca
65*e8d8bef9SDimitry Andric } // namespace llvm
66*e8d8bef9SDimitry Andric 
67*e8d8bef9SDimitry Andric #endif
68