Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 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 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 18 | #include "Instruction.h" |
Andrea Di Biagio | 4704f03 | 2018-03-20 12:25:54 +0000 | [diff] [blame] | 19 | #include "Support.h" |
Andrea Di Biagio | 8834779 | 2018-07-09 12:30:55 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCInstPrinter.h" |
Andrea Di Biagio | 2145b13 | 2018-06-20 10:08:11 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCInstrAnalysis.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCInstrInfo.h" |
Andrea Di Biagio | 2145b13 | 2018-06-20 10:08:11 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCRegisterInfo.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSubtargetInfo.h" |
Matt Davis | 4bcf369 | 2018-08-13 18:11:48 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Error.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 26 | |
| 27 | namespace mca { |
| 28 | |
| 29 | class DispatchUnit; |
| 30 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 31 | /// A builder class that knows how to construct Instruction objects. |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 32 | /// |
| 33 | /// Every llvm-mca Instruction is described by an object of class InstrDesc. |
| 34 | /// An InstrDesc describes which registers are read/written by the instruction, |
| 35 | /// as well as the instruction latency and hardware resources consumed. |
| 36 | /// |
| 37 | /// This class is used by the tool to construct Instructions and instruction |
| 38 | /// descriptors (i.e. InstrDesc objects). |
| 39 | /// Information from the machine scheduling model is used to identify processor |
| 40 | /// resources that are consumed by an instruction. |
| 41 | class InstrBuilder { |
Andrea Di Biagio | 4704f03 | 2018-03-20 12:25:54 +0000 | [diff] [blame] | 42 | const llvm::MCSubtargetInfo &STI; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 43 | const llvm::MCInstrInfo &MCII; |
Andrea Di Biagio | 2145b13 | 2018-06-20 10:08:11 +0000 | [diff] [blame] | 44 | const llvm::MCRegisterInfo &MRI; |
| 45 | const llvm::MCInstrAnalysis &MCIA; |
Andrea Di Biagio | 8834779 | 2018-07-09 12:30:55 +0000 | [diff] [blame] | 46 | llvm::MCInstPrinter &MCIP; |
Andrea Di Biagio | 4704f03 | 2018-03-20 12:25:54 +0000 | [diff] [blame] | 47 | llvm::SmallVector<uint64_t, 8> ProcResourceMasks; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 48 | |
| 49 | llvm::DenseMap<unsigned short, std::unique_ptr<const InstrDesc>> Descriptors; |
Andrea Di Biagio | 39e5a56 | 2018-06-04 15:43:09 +0000 | [diff] [blame] | 50 | llvm::DenseMap<const llvm::MCInst *, std::unique_ptr<const InstrDesc>> |
| 51 | VariantDescriptors; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 52 | |
Matt Davis | 4bcf369 | 2018-08-13 18:11:48 +0000 | [diff] [blame] | 53 | llvm::Expected<const InstrDesc &> |
| 54 | createInstrDescImpl(const llvm::MCInst &MCI); |
| 55 | llvm::Expected<const InstrDesc &> |
| 56 | getOrCreateInstrDesc(const llvm::MCInst &MCI); |
Matt Davis | 99a1ce9 | 2018-08-10 20:24:27 +0000 | [diff] [blame] | 57 | |
Andrea Di Biagio | 49c8591 | 2018-05-04 13:10:10 +0000 | [diff] [blame] | 58 | InstrBuilder(const InstrBuilder &) = delete; |
| 59 | InstrBuilder &operator=(const InstrBuilder &) = delete; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 60 | |
Matt Davis | 4bcf369 | 2018-08-13 18:11:48 +0000 | [diff] [blame] | 61 | llvm::Error populateWrites(InstrDesc &ID, const llvm::MCInst &MCI, |
| 62 | unsigned SchedClassID); |
| 63 | llvm::Error populateReads(InstrDesc &ID, const llvm::MCInst &MCI, |
| 64 | unsigned SchedClassID); |
Andrea Di Biagio | 8834779 | 2018-07-09 12:30:55 +0000 | [diff] [blame] | 65 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 66 | public: |
Andrea Di Biagio | 2145b13 | 2018-06-20 10:08:11 +0000 | [diff] [blame] | 67 | InstrBuilder(const llvm::MCSubtargetInfo &sti, const llvm::MCInstrInfo &mcii, |
| 68 | const llvm::MCRegisterInfo &mri, |
Andrea Di Biagio | 8834779 | 2018-07-09 12:30:55 +0000 | [diff] [blame] | 69 | const llvm::MCInstrAnalysis &mcia, llvm::MCInstPrinter &mcip) |
| 70 | : STI(sti), MCII(mcii), MRI(mri), MCIA(mcia), MCIP(mcip), |
Andrea Di Biagio | 4704f03 | 2018-03-20 12:25:54 +0000 | [diff] [blame] | 71 | ProcResourceMasks(STI.getSchedModel().getNumProcResourceKinds()) { |
| 72 | computeProcResourceMasks(STI.getSchedModel(), ProcResourceMasks); |
| 73 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 74 | |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 75 | // Returns an array of processor resource masks. |
| 76 | // Masks are computed by function mca::computeProcResourceMasks. see |
| 77 | // Support.h for a description of how masks are computed and how masks can be |
| 78 | // used to solve set membership problems. |
| 79 | llvm::ArrayRef<uint64_t> getProcResourceMasks() const { |
| 80 | return ProcResourceMasks; |
| 81 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 82 | |
Andrea Di Biagio | 9b3cb08 | 2018-07-02 20:39:57 +0000 | [diff] [blame] | 83 | void clear() { VariantDescriptors.shrink_and_clear(); } |
| 84 | |
Matt Davis | 4bcf369 | 2018-08-13 18:11:48 +0000 | [diff] [blame] | 85 | llvm::Expected<std::unique_ptr<Instruction>> |
| 86 | createInstruction(const llvm::MCInst &MCI); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 87 | }; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 88 | } // namespace mca |
| 89 | |
| 90 | #endif |