1 //===----------------------- View.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. Each view contributes a
11 /// portion of the final report generated by the tool.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TOOLS_LLVM_MCA_VIEW_H
16 #define LLVM_TOOLS_LLVM_MCA_VIEW_H
17 
18 #include "llvm/MC/MCInstPrinter.h"
19 #include "llvm/MCA/HWEventListener.h"
20 #include "llvm/Support/raw_ostream.h"
21 
22 namespace llvm {
23 namespace mca {
24 
25 class View : public HWEventListener {
26 public:
27   virtual void printView(llvm::raw_ostream &OS) const = 0;
28   virtual ~View() = default;
29   void anchor() override;
30 };
31 
32 // The base class for views that deal with individual machine instructions.
33 class InstructionView : public View {
34   const llvm::MCSubtargetInfo &STI;
35   llvm::MCInstPrinter &MCIP;
36   llvm::ArrayRef<llvm::MCInst> Source;
37 
38   mutable std::string InstructionString;
39   mutable raw_string_ostream InstrStream;
40 
41 protected:
InstructionView(const llvm::MCSubtargetInfo & STI,llvm::MCInstPrinter & Printer,llvm::ArrayRef<llvm::MCInst> S)42   InstructionView(const llvm::MCSubtargetInfo &STI,
43                   llvm::MCInstPrinter &Printer,
44                   llvm::ArrayRef<llvm::MCInst> S)
45       : STI(STI), MCIP(Printer), Source(S), InstrStream(InstructionString) {}
46 
47   virtual ~InstructionView() = default;
48 
49   // Return a reference to a string representing a given machine instruction.
50   // The result should be used or copied before the next call to
51   // printInstructionString() as it will overwrite the previous result.
52   StringRef printInstructionString(const llvm::MCInst &MCI) const;
53 
getSubTargetInfo()54   const llvm::MCSubtargetInfo &getSubTargetInfo() const { return STI; }
getInstPrinter()55   llvm::MCInstPrinter &getInstPrinter() const { return MCIP; }
getSource()56   llvm::ArrayRef<llvm::MCInst> getSource() const { return Source; }
57 };
58 } // namespace mca
59 } // namespace llvm
60 
61 #endif
62