xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-mca/Views/InstructionView.h (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1 //===----------------------- InstructionView.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/Support/JSON.h"
20 #include "llvm/Support/raw_ostream.h"
21 
22 namespace llvm {
23 namespace mca {
24 
25 // The base class for views that deal with individual machine instructions.
26 class InstructionView : public View {
27   const llvm::MCSubtargetInfo &STI;
28   llvm::MCInstPrinter &MCIP;
29   llvm::ArrayRef<llvm::MCInst> Source;
30 
31   mutable std::string InstructionString;
32   mutable raw_string_ostream InstrStream;
33 
34 public:
35   void printView(llvm::raw_ostream &) const override {}
36   InstructionView(const llvm::MCSubtargetInfo &STI,
37                   llvm::MCInstPrinter &Printer, llvm::ArrayRef<llvm::MCInst> S)
38       : STI(STI), MCIP(Printer), Source(S), InstrStream(InstructionString) {}
39 
40   virtual ~InstructionView();
41 
42   StringRef getNameAsString() const override { return "Instructions"; }
43 
44   // Return a reference to a string representing a given machine instruction.
45   // The result should be used or copied before the next call to
46   // printInstructionString() as it will overwrite the previous result.
47   StringRef printInstructionString(const llvm::MCInst &MCI) const;
48   const llvm::MCSubtargetInfo &getSubTargetInfo() const { return STI; }
49 
50   llvm::MCInstPrinter &getInstPrinter() const { return MCIP; }
51   llvm::ArrayRef<llvm::MCInst> getSource() const { return Source; }
52 
53   json::Value toJSON() const override;
54 };
55 
56 } // namespace mca
57 } // namespace llvm
58 
59 #endif
60