1 //===--------------------- InstrBuilder.h -----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 ///
11 /// A builder class for instructions that are statically analyzed by llvm-mca.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_MCA_INSTRBUILDER_H
16 #define LLVM_MCA_INSTRBUILDER_H
17 
18 #include "llvm/MC/MCInstrAnalysis.h"
19 #include "llvm/MC/MCInstrInfo.h"
20 #include "llvm/MC/MCRegisterInfo.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/MCA/Instruction.h"
23 #include "llvm/MCA/Support.h"
24 #include "llvm/Support/Error.h"
25 
26 namespace llvm {
27 namespace mca {
28 
29 /// A builder class that knows how to construct Instruction objects.
30 ///
31 /// Every llvm-mca Instruction is described by an object of class InstrDesc.
32 /// An InstrDesc describes which registers are read/written by the instruction,
33 /// as well as the instruction latency and hardware resources consumed.
34 ///
35 /// This class is used by the tool to construct Instructions and instruction
36 /// descriptors (i.e. InstrDesc objects).
37 /// Information from the machine scheduling model is used to identify processor
38 /// resources that are consumed by an instruction.
39 class InstrBuilder {
40   const MCSubtargetInfo &STI;
41   const MCInstrInfo &MCII;
42   const MCRegisterInfo &MRI;
43   const MCInstrAnalysis *MCIA;
44   SmallVector<uint64_t, 8> ProcResourceMasks;
45 
46   DenseMap<unsigned short, std::unique_ptr<const InstrDesc>> Descriptors;
47   DenseMap<const MCInst *, std::unique_ptr<const InstrDesc>> VariantDescriptors;
48 
49   bool FirstCallInst;
50   bool FirstReturnInst;
51 
52   Expected<const InstrDesc &> createInstrDescImpl(const MCInst &MCI);
53   Expected<const InstrDesc &> getOrCreateInstrDesc(const MCInst &MCI);
54 
55   InstrBuilder(const InstrBuilder &) = delete;
56   InstrBuilder &operator=(const InstrBuilder &) = delete;
57 
58   void populateWrites(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
59   void populateReads(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
60   Error verifyInstrDesc(const InstrDesc &ID, const MCInst &MCI) const;
61 
62 public:
63   InstrBuilder(const MCSubtargetInfo &STI, const MCInstrInfo &MCII,
64                const MCRegisterInfo &RI, const MCInstrAnalysis *IA);
65 
clear()66   void clear() {
67     VariantDescriptors.shrink_and_clear();
68     FirstCallInst = true;
69     FirstReturnInst = true;
70   }
71 
72   Expected<std::unique_ptr<Instruction>> createInstruction(const MCInst &MCI);
73 };
74 } // namespace mca
75 } // namespace llvm
76 
77 #endif // LLVM_MCA_INSTRBUILDER_H
78