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