Home | History | Annotate | Download | only in llvm-mca
      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_TOOLS_LLVM_MCA_INSTRBUILDER_H
     16 #define LLVM_TOOLS_LLVM_MCA_INSTRBUILDER_H
     17 
     18 #include "Instruction.h"
     19 #include "Support.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"
     25 
     26 namespace mca {
     27 
     28 class DispatchUnit;
     29 
     30 /// A builder class that knows how to construct Instruction objects.
     31 ///
     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.
     35 ///
     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.
     40 class InstrBuilder {
     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;
     47 
     48   llvm::DenseMap<unsigned short, std::unique_ptr<const InstrDesc>> Descriptors;
     49   llvm::DenseMap<const llvm::MCInst *, std::unique_ptr<const InstrDesc>>
     50       VariantDescriptors;
     51 
     52   const InstrDesc &createInstrDescImpl(const llvm::MCInst &MCI);
     53   InstrBuilder(const InstrBuilder &) = delete;
     54   InstrBuilder &operator=(const InstrBuilder &) = delete;
     55 
     56   void populateWrites(InstrDesc &ID, const llvm::MCInst &MCI,
     57                       unsigned SchedClassID);
     58   void populateReads(InstrDesc &ID, const llvm::MCInst &MCI,
     59                      unsigned SchedClassID);
     60 
     61 public:
     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);
     68   }
     69 
     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;
     77   }
     78 
     79   void clear() { VariantDescriptors.shrink_and_clear(); }
     80 
     81   std::unique_ptr<Instruction> createInstruction(const llvm::MCInst &MCI);
     82 };
     83 } // namespace mca
     84 
     85 #endif
     86