1 //===--------------------- InstrBuilder.h -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// A builder class for instructions that are statically analyzed by llvm-mca.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TOOLS_LLVM_MCA_INSTRBUILDER_H
16 #define LLVM_TOOLS_LLVM_MCA_INSTRBUILDER_H
18 #include "Instruction.h"
20 #include "llvm/MC/MCInstPrinter.h"
21 #include "llvm/MC/MCInstrAnalysis.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
30 /// A builder class that knows how to construct Instruction objects.
32 /// Every llvm-mca Instruction is described by an object of class InstrDesc.
33 /// An InstrDesc describes which registers are read/written by the instruction,
34 /// as well as the instruction latency and hardware resources consumed.
36 /// This class is used by the tool to construct Instructions and instruction
37 /// descriptors (i.e. InstrDesc objects).
38 /// Information from the machine scheduling model is used to identify processor
39 /// resources that are consumed by an instruction.
41 const llvm::MCSubtargetInfo &STI;
42 const llvm::MCInstrInfo &MCII;
43 const llvm::MCRegisterInfo &MRI;
44 const llvm::MCInstrAnalysis &MCIA;
45 llvm::MCInstPrinter &MCIP;
46 llvm::SmallVector<uint64_t, 8> ProcResourceMasks;
48 llvm::DenseMap<unsigned short, std::unique_ptr<const InstrDesc>> Descriptors;
49 llvm::DenseMap<const llvm::MCInst *, std::unique_ptr<const InstrDesc>>
52 const InstrDesc &createInstrDescImpl(const llvm::MCInst &MCI);
53 InstrBuilder(const InstrBuilder &) = delete;
54 InstrBuilder &operator=(const InstrBuilder &) = delete;
56 void populateWrites(InstrDesc &ID, const llvm::MCInst &MCI,
57 unsigned SchedClassID);
58 void populateReads(InstrDesc &ID, const llvm::MCInst &MCI,
59 unsigned SchedClassID);
62 InstrBuilder(const llvm::MCSubtargetInfo &sti, const llvm::MCInstrInfo &mcii,
63 const llvm::MCRegisterInfo &mri,
64 const llvm::MCInstrAnalysis &mcia, llvm::MCInstPrinter &mcip)
65 : STI(sti), MCII(mcii), MRI(mri), MCIA(mcia), MCIP(mcip),
66 ProcResourceMasks(STI.getSchedModel().getNumProcResourceKinds()) {
67 computeProcResourceMasks(STI.getSchedModel(), ProcResourceMasks);
70 const InstrDesc &getOrCreateInstrDesc(const llvm::MCInst &MCI);
71 // Returns an array of processor resource masks.
72 // Masks are computed by function mca::computeProcResourceMasks. see
73 // Support.h for a description of how masks are computed and how masks can be
74 // used to solve set membership problems.
75 llvm::ArrayRef<uint64_t> getProcResourceMasks() const {
76 return ProcResourceMasks;
79 void clear() { VariantDescriptors.shrink_and_clear(); }
81 std::unique_ptr<Instruction> createInstruction(const llvm::MCInst &MCI);