1 //===----------------------- CodeRegionGenerator.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 declares classes responsible for generating llvm-mca
11 /// CodeRegions from various types of input. llvm-mca only analyzes CodeRegions,
12 /// so the classes here provide the input-to-CodeRegions translation.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_TOOLS_LLVM_MCA_CODEREGION_GENERATOR_H
17 #define LLVM_TOOLS_LLVM_MCA_CODEREGION_GENERATOR_H
18 
19 #include "CodeRegion.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCSubtargetInfo.h"
23 #include "llvm/MC/TargetRegistry.h"
24 #include "llvm/Support/Error.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include <memory>
27 
28 namespace llvm {
29 namespace mca {
30 
31 /// This class is responsible for parsing the input given to the llvm-mca
32 /// driver, and converting that into a CodeRegions instance.
33 class CodeRegionGenerator {
34 protected:
35   CodeRegions Regions;
36   CodeRegionGenerator(const CodeRegionGenerator &) = delete;
37   CodeRegionGenerator &operator=(const CodeRegionGenerator &) = delete;
38 
39 public:
40   CodeRegionGenerator(llvm::SourceMgr &SM) : Regions(SM) {}
41   virtual ~CodeRegionGenerator();
42   virtual Expected<const CodeRegions &>
43   parseCodeRegions(const std::unique_ptr<MCInstPrinter> &IP) = 0;
44 };
45 
46 /// This class is responsible for parsing input ASM and generating
47 /// a CodeRegions instance.
48 class AsmCodeRegionGenerator final : public CodeRegionGenerator {
49   const Target &TheTarget;
50   MCContext &Ctx;
51   const MCAsmInfo &MAI;
52   const MCSubtargetInfo &STI;
53   const MCInstrInfo &MCII;
54   unsigned AssemblerDialect; // This is set during parsing.
55 
56 public:
57   AsmCodeRegionGenerator(const Target &T, llvm::SourceMgr &SM, MCContext &C,
58                          const MCAsmInfo &A, const MCSubtargetInfo &S,
59                          const MCInstrInfo &I)
60       : CodeRegionGenerator(SM), TheTarget(T), Ctx(C), MAI(A), STI(S), MCII(I),
61         AssemblerDialect(0) {}
62 
63   unsigned getAssemblerDialect() const { return AssemblerDialect; }
64   Expected<const CodeRegions &>
65   parseCodeRegions(const std::unique_ptr<MCInstPrinter> &IP) override;
66 };
67 
68 } // namespace mca
69 } // namespace llvm
70 
71 #endif // LLVM_TOOLS_LLVM_MCA_CODEREGION_GENERATOR_H
72