1//===- HexagonMCChecker.h - Instruction bundle checking ---------*- 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//
9// This implements the checking of insns inside a bundle according to the
10// packet constraint rules of the Hexagon ISA.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_HEXAGON_MCTARGETDESC_HEXAGONMCCHECKER_H
15#define LLVM_LIB_TARGET_HEXAGON_MCTARGETDESC_HEXAGONMCCHECKER_H
16
17#include "MCTargetDesc/HexagonMCInstrInfo.h"
18#include "MCTargetDesc/HexagonMCTargetDesc.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/Support/SMLoc.h"
21#include <set>
22#include <utility>
23
24namespace llvm {
25
26class MCContext;
27class MCInst;
28class MCInstrInfo;
29class MCRegisterInfo;
30class MCSubtargetInfo;
31
32/// Check for a valid bundle.
33class HexagonMCChecker {
34 MCContext &Context;
35 MCInst &MCB;
36 const MCRegisterInfo &RI;
37 MCInstrInfo const &MCII;
38 MCSubtargetInfo const &STI;
39 bool ReportErrors;
40
41 /// Set of definitions: register #, if predicated, if predicated true.
42 using PredSense = std::pair<MCRegister, bool>;
43 static const PredSense Unconditional;
44 using PredSet = std::multiset<PredSense>;
45 using PredSetIterator = std::multiset<PredSense>::iterator;
46
47 using DefsIterator = DenseMap<MCRegister, PredSet>::iterator;
48 DenseMap<MCRegister, PredSet> Defs;
49
50 /// Set of weak definitions whose clashes should be enforced selectively.
51 using SoftDefsIterator = std::set<MCRegister>::iterator;
52 std::set<MCRegister> SoftDefs;
53
54 /// Set of temporary definitions not committed to the register file.
55 using TmpDefsIterator = std::set<MCRegister>::iterator;
56 std::set<MCRegister> TmpDefs;
57
58 /// Set of new predicates used.
59 using NewPredsIterator = std::set<MCRegister>::iterator;
60 std::set<MCRegister> NewPreds;
61
62 /// Set of predicates defined late.
63 using LatePredsIterator = std::multiset<MCRegister>::iterator;
64 std::multiset<MCRegister> LatePreds;
65
66 /// Set of uses.
67 using UsesIterator = std::set<MCRegister>::iterator;
68 std::set<MCRegister> Uses;
69
70 /// Pre-defined set of read-only registers.
71 using ReadOnlyIterator = std::set<MCRegister>::iterator;
72 std::set<MCRegister> ReadOnly;
73
74 // Contains the vector-pair-registers with the even number
75 // first ("v0:1", e.g.) used/def'd in this packet.
76 std::set<MCRegister> ReversePairs;
77
78 void init();
79 void init(MCInst const &);
80 void initReg(MCInst const &, MCRegister, MCRegister &PredReg, bool &isTrue);
81
82 bool registerUsed(MCRegister Register);
83
84 /// \return a tuple of: pointer to the producer instruction or nullptr if
85 /// none was found, the operand index, and the PredicateInfo for the
86 /// producer.
87 std::tuple<MCInst const *, unsigned, HexagonMCInstrInfo::PredicateInfo>
88 registerProducer(MCRegister Register,
89 HexagonMCInstrInfo::PredicateInfo Predicated);
90
91 // Checks performed.
92 bool checkPredicates();
93 bool checkNewValues();
94 bool checkRegisters();
95 bool checkRegistersReadOnly();
96 void checkRegisterCurDefs();
97 bool checkSolo();
98 bool checkShuffle();
99 bool checkSlots();
100 bool checkAXOK();
101 bool checkHWLoop();
102 bool checkCOFMax1();
103 bool checkLegalVecRegPair();
104 bool checkValidHMX();
105 bool checkValidTmpDst();
106 bool checkHVXAccum();
107 // An HLX instruction that reads a 64-bit scalar register may not be in the
108 // same packet with another HLX instruction that uses a scalar register or
109 // an immediate. HLX loads and stores are excluded.
110 bool checkHLX64BitRegUse();
111
112 static void compoundRegisterMap(unsigned &);
113
114 bool isLoopRegister(MCRegister R) const {
115 return (Hexagon::SA0 == R || Hexagon::LC0 == R || Hexagon::SA1 == R ||
116 Hexagon::LC1 == R);
117 }
118
119public:
120 explicit HexagonMCChecker(MCContext &Context, MCInstrInfo const &MCII,
121 MCSubtargetInfo const &STI, MCInst &mcb,
122 const MCRegisterInfo &ri, bool ReportErrors = true);
123 explicit HexagonMCChecker(HexagonMCChecker const &Check,
124 MCSubtargetInfo const &STI, bool CopyReportErrors);
125
126 bool check(bool FullCheck = true);
127 void reportErrorRegisters(MCRegister Register);
128 void reportErrorNewValue(MCRegister Register);
129 void reportError(SMLoc Loc, Twine const &Msg);
130 void reportNote(SMLoc Loc, Twine const &Msg);
131 void reportError(Twine const &Msg);
132 void reportWarning(Twine const &Msg);
133 void reportBranchErrors();
134};
135
136} // end namespace llvm
137
138#endif // LLVM_LIB_TARGET_HEXAGON_MCTARGETDESC_HEXAGONMCCHECKER_H
139