Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:16

0001 //===--------------------- InstrBuilder.h -----------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 ///
0010 /// A builder class for instructions that are statically analyzed by llvm-mca.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_MCA_INSTRBUILDER_H
0015 #define LLVM_MCA_INSTRBUILDER_H
0016 
0017 #include "llvm/ADT/Hashing.h"
0018 #include "llvm/ADT/STLExtras.h"
0019 #include "llvm/MC/MCInstrAnalysis.h"
0020 #include "llvm/MC/MCInstrInfo.h"
0021 #include "llvm/MC/MCRegisterInfo.h"
0022 #include "llvm/MC/MCSubtargetInfo.h"
0023 #include "llvm/MCA/CustomBehaviour.h"
0024 #include "llvm/MCA/Instruction.h"
0025 #include "llvm/MCA/Support.h"
0026 #include "llvm/Support/Error.h"
0027 
0028 namespace llvm {
0029 namespace mca {
0030 
0031 class RecycledInstErr : public ErrorInfo<RecycledInstErr> {
0032   Instruction *RecycledInst;
0033 
0034 public:
0035   static char ID;
0036 
0037   explicit RecycledInstErr(Instruction *Inst) : RecycledInst(Inst) {}
0038   // Always need to carry an Instruction
0039   RecycledInstErr() = delete;
0040 
0041   Instruction *getInst() const { return RecycledInst; }
0042 
0043   void log(raw_ostream &OS) const override {
0044     OS << "Instruction is recycled\n";
0045   }
0046 
0047   std::error_code convertToErrorCode() const override {
0048     return llvm::inconvertibleErrorCode();
0049   }
0050 };
0051 
0052 /// A builder class that knows how to construct Instruction objects.
0053 ///
0054 /// Every llvm-mca Instruction is described by an object of class InstrDesc.
0055 /// An InstrDesc describes which registers are read/written by the instruction,
0056 /// as well as the instruction latency and hardware resources consumed.
0057 ///
0058 /// This class is used by the tool to construct Instructions and instruction
0059 /// descriptors (i.e. InstrDesc objects).
0060 /// Information from the machine scheduling model is used to identify processor
0061 /// resources that are consumed by an instruction.
0062 class InstrBuilder {
0063   const MCSubtargetInfo &STI;
0064   const MCInstrInfo &MCII;
0065   const MCRegisterInfo &MRI;
0066   const MCInstrAnalysis *MCIA;
0067   const InstrumentManager &IM;
0068   SmallVector<uint64_t, 8> ProcResourceMasks;
0069 
0070   // Key is the MCI.Opcode and SchedClassID the describe the value InstrDesc
0071   DenseMap<std::pair<unsigned short, unsigned>,
0072            std::unique_ptr<const InstrDesc>>
0073       Descriptors;
0074 
0075   // Key is a hash of the MCInstruction and a SchedClassID that describe the
0076   // value InstrDesc
0077   DenseMap<std::pair<hash_code, unsigned>, std::unique_ptr<const InstrDesc>>
0078       VariantDescriptors;
0079 
0080   bool FirstCallInst;
0081   bool FirstReturnInst;
0082   unsigned CallLatency;
0083 
0084   using InstRecycleCallback = std::function<Instruction *(const InstrDesc &)>;
0085   InstRecycleCallback InstRecycleCB;
0086 
0087   Expected<unsigned> getVariantSchedClassID(const MCInst &MCI, unsigned SchedClassID);
0088   Expected<const InstrDesc &>
0089   createInstrDescImpl(const MCInst &MCI, const SmallVector<Instrument *> &IVec);
0090   Expected<const InstrDesc &>
0091   getOrCreateInstrDesc(const MCInst &MCI,
0092                        const SmallVector<Instrument *> &IVec);
0093 
0094   InstrBuilder(const InstrBuilder &) = delete;
0095   InstrBuilder &operator=(const InstrBuilder &) = delete;
0096 
0097   void populateWrites(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
0098   void populateReads(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
0099   Error verifyInstrDesc(const InstrDesc &ID, const MCInst &MCI) const;
0100 
0101 public:
0102   InstrBuilder(const MCSubtargetInfo &STI, const MCInstrInfo &MCII,
0103                const MCRegisterInfo &RI, const MCInstrAnalysis *IA,
0104                const InstrumentManager &IM, unsigned CallLatency);
0105 
0106   void clear() {
0107     Descriptors.clear();
0108     VariantDescriptors.clear();
0109     FirstCallInst = true;
0110     FirstReturnInst = true;
0111   }
0112 
0113   /// Set a callback which is invoked to retrieve a recycled mca::Instruction
0114   /// or null if there isn't any.
0115   void setInstRecycleCallback(InstRecycleCallback CB) { InstRecycleCB = CB; }
0116 
0117   Expected<std::unique_ptr<Instruction>>
0118   createInstruction(const MCInst &MCI, const SmallVector<Instrument *> &IVec);
0119 };
0120 } // namespace mca
0121 } // namespace llvm
0122 
0123 #endif // LLVM_MCA_INSTRBUILDER_H