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