1 //===----- HexagonMCChecker.cpp - Instruction bundle checking -------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This implements the checking of insns inside a bundle according to the
10 // packet constraint rules of the Hexagon ISA.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/HexagonMCChecker.h"
15 #include "MCTargetDesc/HexagonBaseInfo.h"
16 #include "MCTargetDesc/HexagonMCInstrInfo.h"
17 #include "MCTargetDesc/HexagonMCShuffler.h"
18 #include "MCTargetDesc/HexagonMCTargetDesc.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstrDesc.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/SourceMgr.h"
31 RelaxNVChecks("relax-nv-checks", cl::init(false), cl::ZeroOrMore
,
32 cl::Hidden
, cl::desc("Relax checks of new-value validity"));
34 const HexagonMCChecker::PredSense
35 HexagonMCChecker::Unconditional(Hexagon::NoRegister
, false);
37 void HexagonMCChecker::init() {
38 // Initialize read-only registers set.
39 ReadOnly
.insert(Hexagon::PC
);
40 ReadOnly
.insert(Hexagon::C9_8
);
42 // Figure out the loop-registers definitions.
43 if (HexagonMCInstrInfo::isInnerLoop(MCB
)) {
44 Defs
[Hexagon::SA0
].insert(Unconditional
); // FIXME: define or change SA0?
45 Defs
[Hexagon::LC0
].insert(Unconditional
);
47 if (HexagonMCInstrInfo::isOuterLoop(MCB
)) {
48 Defs
[Hexagon::SA1
].insert(Unconditional
); // FIXME: define or change SA0?
49 Defs
[Hexagon::LC1
].insert(Unconditional
);
52 if (HexagonMCInstrInfo::isBundle(MCB
))
54 for (auto const &I
: HexagonMCInstrInfo::bundleInstructions(MCB
)) {
55 MCInst
const &Inst
= *I
.getInst();
56 if (HexagonMCInstrInfo::isDuplex(MCII
, Inst
)) {
57 init(*Inst
.getOperand(0).getInst());
58 init(*Inst
.getOperand(1).getInst());
66 void HexagonMCChecker::initReg(MCInst
const &MCI
, unsigned R
, unsigned &PredReg
,
68 if (HexagonMCInstrInfo::isPredicated(MCII
, MCI
) && isPredicateRegister(R
)) {
69 // Note an used predicate register.
71 isTrue
= HexagonMCInstrInfo::isPredicatedTrue(MCII
, MCI
);
73 // Note use of new predicate register.
74 if (HexagonMCInstrInfo::isPredicatedNew(MCII
, MCI
))
75 NewPreds
.insert(PredReg
);
77 // Note register use. Super-registers are not tracked directly,
78 // but their components.
79 for (MCRegAliasIterator
SRI(R
, &RI
, !MCSubRegIterator(R
, &RI
).isValid());
81 if (!MCSubRegIterator(*SRI
, &RI
).isValid())
82 // Skip super-registers used indirectly.
85 if (HexagonMCInstrInfo::IsReverseVecRegPair(R
))
86 ReversePairs
.insert(R
);
89 void HexagonMCChecker::init(MCInst
const &MCI
) {
90 const MCInstrDesc
&MCID
= HexagonMCInstrInfo::getDesc(MCII
, MCI
);
91 unsigned PredReg
= Hexagon::NoRegister
;
94 // Get used registers.
95 for (unsigned i
= MCID
.getNumDefs(); i
< MCID
.getNumOperands(); ++i
)
96 if (MCI
.getOperand(i
).isReg())
97 initReg(MCI
, MCI
.getOperand(i
).getReg(), PredReg
, isTrue
);
98 for (unsigned i
= 0; i
< MCID
.getNumImplicitUses(); ++i
)
99 initReg(MCI
, MCID
.getImplicitUses()[i
], PredReg
, isTrue
);
101 // Get implicit register definitions.
102 if (const MCPhysReg
*ImpDef
= MCID
.getImplicitDefs())
103 for (; *ImpDef
; ++ImpDef
) {
104 unsigned R
= *ImpDef
;
106 if (Hexagon::R31
!= R
&& MCID
.isCall())
107 // Any register other than the LR and the PC are actually volatile ones
108 // as defined by the ABI, not modified implicitly by the call insn.
110 if (Hexagon::PC
== R
)
111 // Branches are the only insns that can change the PC,
112 // otherwise a read-only register.
115 if (Hexagon::USR_OVF
== R
)
116 // Many insns change the USR implicitly, but only one or another flag.
117 // The instruction table models the USR.OVF flag, which can be
118 // implicitly modified more than once, but cannot be modified in the
119 // same packet with an instruction that modifies is explicitly. Deal
120 // with such situations individually.
122 else if (isPredicateRegister(R
) &&
123 HexagonMCInstrInfo::isPredicateLate(MCII
, MCI
))
124 // Include implicit late predicates.
127 Defs
[R
].insert(PredSense(PredReg
, isTrue
));
130 // Figure out explicit register definitions.
131 for (unsigned i
= 0; i
< MCID
.getNumDefs(); ++i
) {
132 unsigned R
= MCI
.getOperand(i
).getReg(), S
= Hexagon::NoRegister
;
133 // USR has subregisters (while C8 does not for technical reasons), so
134 // reset R to USR, since we know how to handle multiple defs of USR,
135 // taking into account its subregisters.
136 if (R
== Hexagon::C8
)
139 if (HexagonMCInstrInfo::IsReverseVecRegPair(R
))
140 ReversePairs
.insert(R
);
142 // Note register definitions, direct ones as well as indirect side-effects.
143 // Super-registers are not tracked directly, but their components.
144 for (MCRegAliasIterator
SRI(R
, &RI
, !MCSubRegIterator(R
, &RI
).isValid());
145 SRI
.isValid(); ++SRI
) {
146 if (MCSubRegIterator(*SRI
, &RI
).isValid())
147 // Skip super-registers defined indirectly.
152 // Avoid scoring the defined register multiple times.
155 // Note that the defined register has already been scored.
159 if (Hexagon::P3_0
!= R
&& Hexagon::P3_0
== *SRI
)
160 // P3:0 is a special case, since multiple predicate register definitions
161 // in a packet is allowed as the equivalent of their logical "and".
162 // Only an explicit definition of P3:0 is noted as such; if a
163 // side-effect, then note as a soft definition.
164 SoftDefs
.insert(*SRI
);
165 else if (HexagonMCInstrInfo::isPredicateLate(MCII
, MCI
) &&
166 isPredicateRegister(*SRI
))
167 // Some insns produce predicates too late to be used in the same packet.
168 LatePreds
.insert(*SRI
);
169 else if (i
== 0 && HexagonMCInstrInfo::getType(MCII
, MCI
) ==
170 HexagonII::TypeCVI_VM_TMP_LD
)
171 // Temporary loads should be used in the same packet, but don't commit
172 // results, so it should be disregarded if another insn changes the same
174 // TODO: relies on the impossibility of a current and a temporary loads
175 // in the same packet.
176 TmpDefs
.insert(*SRI
);
177 else if (i
<= 1 && HexagonMCInstrInfo::hasNewValue2(MCII
, MCI
))
178 // vshuff(Vx, Vy, Rx) <- Vx(0) and Vy(1) are both source and
179 // destination registers with this instruction. same for vdeal(Vx,Vy,Rx)
182 Defs
[*SRI
].insert(PredSense(PredReg
, isTrue
));
186 // Figure out definitions of new predicate registers.
187 if (HexagonMCInstrInfo::isPredicatedNew(MCII
, MCI
))
188 for (unsigned i
= MCID
.getNumDefs(); i
< MCID
.getNumOperands(); ++i
)
189 if (MCI
.getOperand(i
).isReg()) {
190 unsigned P
= MCI
.getOperand(i
).getReg();
192 if (isPredicateRegister(P
))
197 HexagonMCChecker::HexagonMCChecker(MCContext
&Context
, MCInstrInfo
const &MCII
,
198 MCSubtargetInfo
const &STI
, MCInst
&mcb
,
199 MCRegisterInfo
const &ri
, bool ReportErrors
)
200 : Context(Context
), MCB(mcb
), RI(ri
), MCII(MCII
), STI(STI
),
201 ReportErrors(ReportErrors
), ReversePairs() {
205 HexagonMCChecker::HexagonMCChecker(HexagonMCChecker
const &Other
,
206 MCSubtargetInfo
const &STI
,
207 bool CopyReportErrors
)
208 : Context(Other
.Context
), MCB(Other
.MCB
), RI(Other
.RI
), MCII(Other
.MCII
),
209 STI(STI
), ReportErrors(CopyReportErrors
? Other
.ReportErrors
: false),
214 bool HexagonMCChecker::check(bool FullCheck
) {
215 bool chkP
= checkPredicates();
216 bool chkNV
= checkNewValues();
217 bool chkR
= checkRegisters();
218 bool chkRRO
= checkRegistersReadOnly();
219 checkRegisterCurDefs();
220 bool chkS
= checkSolo();
223 chkSh
= checkShuffle();
226 chkSl
= checkSlots();
227 bool chkAXOK
= checkAXOK();
228 bool chkCofMax1
= checkCOFMax1();
229 bool chkHWLoop
= checkHWLoop();
230 bool chkLegalVecRegPair
= checkLegalVecRegPair();
231 bool chk
= chkP
&& chkNV
&& chkR
&& chkRRO
&& chkS
&& chkSh
&& chkSl
&&
232 chkAXOK
&& chkCofMax1
&& chkHWLoop
&& chkLegalVecRegPair
;
237 static bool isDuplexAGroup(unsigned Opcode
) {
239 case Hexagon::SA1_addi
:
240 case Hexagon::SA1_addrx
:
241 case Hexagon::SA1_addsp
:
242 case Hexagon::SA1_and1
:
243 case Hexagon::SA1_clrf
:
244 case Hexagon::SA1_clrfnew
:
245 case Hexagon::SA1_clrt
:
246 case Hexagon::SA1_clrtnew
:
247 case Hexagon::SA1_cmpeqi
:
248 case Hexagon::SA1_combine0i
:
249 case Hexagon::SA1_combine1i
:
250 case Hexagon::SA1_combine2i
:
251 case Hexagon::SA1_combine3i
:
252 case Hexagon::SA1_combinerz
:
253 case Hexagon::SA1_combinezr
:
254 case Hexagon::SA1_dec
:
255 case Hexagon::SA1_inc
:
256 case Hexagon::SA1_seti
:
257 case Hexagon::SA1_setin1
:
258 case Hexagon::SA1_sxtb
:
259 case Hexagon::SA1_sxth
:
260 case Hexagon::SA1_tfr
:
261 case Hexagon::SA1_zxtb
:
262 case Hexagon::SA1_zxth
:
270 static bool isNeitherAnorX(MCInstrInfo
const &MCII
, MCInst
const &ID
) {
272 unsigned Type
= HexagonMCInstrInfo::getType(MCII
, ID
);
273 if (Type
== HexagonII::TypeDUPLEX
) {
274 unsigned subInst0Opcode
= ID
.getOperand(0).getInst()->getOpcode();
275 unsigned subInst1Opcode
= ID
.getOperand(1).getInst()->getOpcode();
276 Result
+= !isDuplexAGroup(subInst0Opcode
);
277 Result
+= !isDuplexAGroup(subInst1Opcode
);
280 Type
!= HexagonII::TypeALU32_2op
&& Type
!= HexagonII::TypeALU32_3op
&&
281 Type
!= HexagonII::TypeALU32_ADDI
&& Type
!= HexagonII::TypeS_2op
&&
282 Type
!= HexagonII::TypeS_3op
&&
283 (Type
!= HexagonII::TypeALU64
|| HexagonMCInstrInfo::isFloat(MCII
, ID
));
287 bool HexagonMCChecker::checkAXOK() {
288 MCInst
const *HasSoloAXInst
= nullptr;
289 for (auto const &I
: HexagonMCInstrInfo::bundleInstructions(MCII
, MCB
)) {
290 if (HexagonMCInstrInfo::isSoloAX(MCII
, I
)) {
296 for (auto const &I
: HexagonMCInstrInfo::bundleInstructions(MCII
, MCB
)) {
297 if (&I
!= HasSoloAXInst
&& isNeitherAnorX(MCII
, I
)) {
299 HasSoloAXInst
->getLoc(),
300 Twine("Instruction can only be in a packet with ALU or non-FPU XTYPE "
302 reportError(I
.getLoc(),
303 Twine("Not an ALU or non-FPU XTYPE instruction"));
310 void HexagonMCChecker::reportBranchErrors() {
311 for (auto const &I
: HexagonMCInstrInfo::bundleInstructions(MCII
, MCB
)) {
312 MCInstrDesc
const &Desc
= HexagonMCInstrInfo::getDesc(MCII
, I
);
313 if (Desc
.isBranch() || Desc
.isCall() || Desc
.isReturn())
314 reportNote(I
.getLoc(), "Branching instruction");
318 bool HexagonMCChecker::checkHWLoop() {
319 if (!HexagonMCInstrInfo::isInnerLoop(MCB
) &&
320 !HexagonMCInstrInfo::isOuterLoop(MCB
))
322 for (auto const &I
: HexagonMCInstrInfo::bundleInstructions(MCII
, MCB
)) {
323 MCInstrDesc
const &Desc
= HexagonMCInstrInfo::getDesc(MCII
, I
);
324 if (Desc
.isBranch() || Desc
.isCall() || Desc
.isReturn()) {
325 reportError(MCB
.getLoc(),
326 "Branches cannot be in a packet with hardware loops");
327 reportBranchErrors();
334 bool HexagonMCChecker::checkCOFMax1() {
335 SmallVector
<MCInst
const *, 2> BranchLocations
;
336 for (auto const &I
: HexagonMCInstrInfo::bundleInstructions(MCII
, MCB
)) {
337 MCInstrDesc
const &Desc
= HexagonMCInstrInfo::getDesc(MCII
, I
);
338 if (Desc
.isBranch() || Desc
.isCall() || Desc
.isReturn())
339 BranchLocations
.push_back(&I
);
341 for (unsigned J
= 0, N
= BranchLocations
.size(); J
< N
; ++J
) {
342 MCInst
const &I
= *BranchLocations
[J
];
343 if (HexagonMCInstrInfo::isCofMax1(MCII
, I
)) {
344 bool Relax1
= HexagonMCInstrInfo::isCofRelax1(MCII
, I
);
345 bool Relax2
= HexagonMCInstrInfo::isCofRelax2(MCII
, I
);
346 if (N
> 1 && !Relax1
&& !Relax2
) {
347 reportError(I
.getLoc(),
348 "Instruction may not be in a packet with other branches");
349 reportBranchErrors();
352 if (N
> 1 && J
== 0 && !Relax1
) {
353 reportError(I
.getLoc(),
354 "Instruction may not be the first branch in packet");
355 reportBranchErrors();
358 if (N
> 1 && J
== 1 && !Relax2
) {
359 reportError(I
.getLoc(),
360 "Instruction may not be the second branch in packet");
361 reportBranchErrors();
369 bool HexagonMCChecker::checkSlots() {
370 unsigned slotsUsed
= 0;
371 for (auto HMI
: HexagonMCInstrInfo::bundleInstructions(MCB
)) {
372 MCInst
const &MCI
= *HMI
.getInst();
373 if (HexagonMCInstrInfo::isImmext(MCI
))
375 if (HexagonMCInstrInfo::isDuplex(MCII
, MCI
))
381 if (slotsUsed
> HEXAGON_PACKET_SIZE
) {
382 reportError("invalid instruction packet: out of slots");
388 // Check legal use of predicate registers.
389 bool HexagonMCChecker::checkPredicates() {
390 // Check for proper use of new predicate registers.
391 for (const auto &I
: NewPreds
) {
394 if (!Defs
.count(P
) || LatePreds
.count(P
) || Defs
.count(Hexagon::P3_0
)) {
395 // Error out if the new predicate register is not defined,
397 // (e.g., "{ if (p3.new)... ; p3 = sp1loop0(#r7:2, Rs) }").
398 reportErrorNewValue(P
);
403 // Check for proper use of auto-anded of predicate registers.
404 for (const auto &I
: LatePreds
) {
407 if (LatePreds
.count(P
) > 1 || Defs
.count(P
)) {
408 // Error out if predicate register defined "late" multiple times or
409 // defined late and regularly defined
410 // (e.g., "{ p3 = sp1loop0(...); p3 = cmp.eq(...) }".
411 reportErrorRegisters(P
);
419 // Check legal use of new values.
420 bool HexagonMCChecker::checkNewValues() {
421 for (auto const &I
: HexagonMCInstrInfo::bundleInstructions(MCII
, MCB
)) {
422 if (!HexagonMCInstrInfo::isNewValue(MCII
, I
))
424 auto Consumer
= HexagonMCInstrInfo::predicateInfo(MCII
, I
);
425 bool Branch
= HexagonMCInstrInfo::getDesc(MCII
, I
).isBranch();
426 MCOperand
const &Op
= HexagonMCInstrInfo::getNewValueOperand(MCII
, I
);
428 auto Producer
= registerProducer(Op
.getReg(), Consumer
);
429 if (std::get
<0>(Producer
) == nullptr) {
430 reportError(I
.getLoc(), "New value register consumer has no producer");
433 if (!RelaxNVChecks
) {
434 // Checks that statically prove correct new value consumption
435 if (std::get
<2>(Producer
).isPredicated() &&
436 (!Consumer
.isPredicated() ||
437 llvm::HexagonMCInstrInfo::getType(MCII
, I
) == HexagonII::TypeNCJ
)) {
439 std::get
<0>(Producer
)->getLoc(),
440 "Register producer is predicated and consumer is unconditional");
441 reportError(I
.getLoc(),
442 "Instruction does not have a valid new register producer");
445 if (std::get
<2>(Producer
).Register
!= Hexagon::NoRegister
&&
446 std::get
<2>(Producer
).Register
!= Consumer
.Register
) {
447 reportNote(std::get
<0>(Producer
)->getLoc(),
448 "Register producer does not use the same predicate "
449 "register as the consumer");
450 reportError(I
.getLoc(),
451 "Instruction does not have a valid new register producer");
455 if (std::get
<2>(Producer
).Register
== Consumer
.Register
&&
456 Consumer
.PredicatedTrue
!= std::get
<2>(Producer
).PredicatedTrue
) {
458 std::get
<0>(Producer
)->getLoc(),
459 "Register producer has the opposite predicate sense as consumer");
460 reportError(I
.getLoc(),
461 "Instruction does not have a valid new register producer");
464 MCInstrDesc
const &Desc
=
465 HexagonMCInstrInfo::getDesc(MCII
, *std::get
<0>(Producer
));
466 if (Desc
.OpInfo
[std::get
<1>(Producer
)].RegClass
==
467 Hexagon::DoubleRegsRegClassID
) {
468 reportNote(std::get
<0>(Producer
)->getLoc(),
469 "Double registers cannot be new-value producers");
470 reportError(I
.getLoc(),
471 "Instruction does not have a valid new register producer");
474 if ((Desc
.mayLoad() && std::get
<1>(Producer
) == 1) ||
475 (Desc
.mayStore() && std::get
<1>(Producer
) == 0)) {
477 HexagonMCInstrInfo::getAddrMode(MCII
, *std::get
<0>(Producer
));
479 if (Mode
== HexagonII::AbsoluteSet
)
480 ModeError
= "Absolute-set";
481 if (Mode
== HexagonII::PostInc
)
482 ModeError
= "Auto-increment";
483 if (!ModeError
.empty()) {
484 reportNote(std::get
<0>(Producer
)->getLoc(),
485 ModeError
+ " registers cannot be a new-value "
487 reportError(I
.getLoc(),
488 "Instruction does not have a valid new register producer");
492 if (Branch
&& HexagonMCInstrInfo::isFloat(MCII
, *std::get
<0>(Producer
))) {
493 reportNote(std::get
<0>(Producer
)->getLoc(),
494 "FPU instructions cannot be new-value producers for jumps");
495 reportError(I
.getLoc(),
496 "Instruction does not have a valid new register producer");
503 bool HexagonMCChecker::checkRegistersReadOnly() {
504 for (auto I
: HexagonMCInstrInfo::bundleInstructions(MCB
)) {
505 MCInst
const &Inst
= *I
.getInst();
506 unsigned Defs
= HexagonMCInstrInfo::getDesc(MCII
, Inst
).getNumDefs();
507 for (unsigned j
= 0; j
< Defs
; ++j
) {
508 MCOperand
const &Operand
= Inst
.getOperand(j
);
509 assert(Operand
.isReg() && "Def is not a register");
510 unsigned Register
= Operand
.getReg();
511 if (ReadOnly
.find(Register
) != ReadOnly
.end()) {
512 reportError(Inst
.getLoc(), "Cannot write to read-only register `" +
513 Twine(RI
.getName(Register
)) + "'");
521 bool HexagonMCChecker::registerUsed(unsigned Register
) {
522 for (auto const &I
: HexagonMCInstrInfo::bundleInstructions(MCII
, MCB
))
523 for (unsigned j
= HexagonMCInstrInfo::getDesc(MCII
, I
).getNumDefs(),
524 n
= I
.getNumOperands();
526 MCOperand
const &Operand
= I
.getOperand(j
);
527 if (Operand
.isReg() && Operand
.getReg() == Register
)
533 std::tuple
<MCInst
const *, unsigned, HexagonMCInstrInfo::PredicateInfo
>
534 HexagonMCChecker::registerProducer(
535 unsigned Register
, HexagonMCInstrInfo::PredicateInfo ConsumerPredicate
) {
536 std::tuple
<MCInst
const *, unsigned, HexagonMCInstrInfo::PredicateInfo
>
538 for (auto const &I
: HexagonMCInstrInfo::bundleInstructions(MCII
, MCB
)) {
539 MCInstrDesc
const &Desc
= HexagonMCInstrInfo::getDesc(MCII
, I
);
540 auto ProducerPredicate
= HexagonMCInstrInfo::predicateInfo(MCII
, I
);
541 for (unsigned J
= 0, N
= Desc
.getNumDefs(); J
< N
; ++J
)
542 for (auto K
= MCRegAliasIterator(I
.getOperand(J
).getReg(), &RI
, true);
544 if (*K
== Register
) {
546 (ProducerPredicate
.Register
== ConsumerPredicate
.Register
&&
547 (ProducerPredicate
.Register
== Hexagon::NoRegister
||
548 ProducerPredicate
.PredicatedTrue
==
549 ConsumerPredicate
.PredicatedTrue
)))
550 return std::make_tuple(&I
, J
, ProducerPredicate
);
551 std::get
<0>(WrongSense
) = &I
;
552 std::get
<1>(WrongSense
) = J
;
553 std::get
<2>(WrongSense
) = ProducerPredicate
;
555 if (Register
== Hexagon::VTMP
&& HexagonMCInstrInfo::hasTmpDst(MCII
, I
))
556 return std::make_tuple(&I
, 0, HexagonMCInstrInfo::PredicateInfo());
561 void HexagonMCChecker::checkRegisterCurDefs() {
562 for (auto const &I
: HexagonMCInstrInfo::bundleInstructions(MCII
, MCB
)) {
563 if (HexagonMCInstrInfo::isCVINew(MCII
, I
) &&
564 HexagonMCInstrInfo::getDesc(MCII
, I
).mayLoad()) {
565 unsigned Register
= I
.getOperand(0).getReg();
566 if (!registerUsed(Register
))
567 reportWarning("Register `" + Twine(RI
.getName(Register
)) +
568 "' used with `.cur' "
569 "but not used in the same packet");
574 // Check for legal register uses and definitions.
575 bool HexagonMCChecker::checkRegisters() {
576 // Check for proper register definitions.
577 for (const auto &I
: Defs
) {
578 unsigned R
= I
.first
;
580 if (isLoopRegister(R
) && Defs
.count(R
) > 1 &&
581 (HexagonMCInstrInfo::isInnerLoop(MCB
) ||
582 HexagonMCInstrInfo::isOuterLoop(MCB
))) {
583 // Error out for definitions of loop registers at the end of a loop.
584 reportError("loop-setup and some branch instructions "
585 "cannot be in the same packet");
588 if (SoftDefs
.count(R
)) {
589 // Error out for explicit changes to registers also weakly defined
590 // (e.g., "{ usr = r0; r0 = sfadd(...) }").
591 unsigned UsrR
= Hexagon::USR
; // Silence warning about mixed types in ?:.
592 unsigned BadR
= RI
.isSubRegister(Hexagon::USR
, R
) ? UsrR
: R
;
593 reportErrorRegisters(BadR
);
596 if (!isPredicateRegister(R
) && Defs
[R
].size() > 1) {
597 // Check for multiple register definitions.
598 PredSet
&PM
= Defs
[R
];
600 // Check for multiple unconditional register definitions.
601 if (PM
.count(Unconditional
)) {
602 // Error out on an unconditional change when there are any other
603 // changes, conditional or not.
604 unsigned UsrR
= Hexagon::USR
;
605 unsigned BadR
= RI
.isSubRegister(Hexagon::USR
, R
) ? UsrR
: R
;
606 reportErrorRegisters(BadR
);
609 // Check for multiple conditional register definitions.
610 for (const auto &J
: PM
) {
613 // Check for multiple uses of the same condition.
614 if (PM
.count(P
) > 1) {
615 // Error out on conditional changes based on the same predicate
616 // (e.g., "{ if (!p0) r0 =...; if (!p0) r0 =... }").
617 reportErrorRegisters(R
);
620 // Check for the use of the complementary condition.
621 P
.second
= !P
.second
;
622 if (PM
.count(P
) && PM
.size() > 2) {
623 // Error out on conditional changes based on the same predicate
625 // (e.g., "if (p0) r0 =...; if (!p0) r0 =... }; if (!p0) r0 =...").
626 reportErrorRegisters(R
);
633 // Check for use of temporary definitions.
634 for (const auto &I
: TmpDefs
) {
637 if (!Uses
.count(R
)) {
638 // special case for vhist
639 bool vHistFound
= false;
640 for (auto const &HMI
: HexagonMCInstrInfo::bundleInstructions(MCB
)) {
641 if (HexagonMCInstrInfo::getType(MCII
, *HMI
.getInst()) ==
642 HexagonII::TypeCVI_HIST
) {
643 vHistFound
= true; // vhist() implicitly uses ALL REGxx.tmp
647 // Warn on an unused temporary definition.
649 reportWarning("register `" + Twine(RI
.getName(R
)) +
650 "' used with `.tmp' but not used in the same packet");
659 // Check for legal use of solo insns.
660 bool HexagonMCChecker::checkSolo() {
661 if (HexagonMCInstrInfo::bundleSize(MCB
) > 1)
662 for (auto const &I
: HexagonMCInstrInfo::bundleInstructions(MCII
, MCB
)) {
663 if (HexagonMCInstrInfo::isSolo(MCII
, I
)) {
664 reportError(I
.getLoc(), "Instruction is marked `isSolo' and "
665 "cannot have other instructions in "
674 bool HexagonMCChecker::checkShuffle() {
675 HexagonMCShuffler
MCSDX(Context
, ReportErrors
, MCII
, STI
, MCB
);
676 return MCSDX
.check();
679 void HexagonMCChecker::compoundRegisterMap(unsigned &Register
) {
684 Register
= Hexagon::R23
;
687 Register
= Hexagon::R22
;
690 Register
= Hexagon::R21
;
693 Register
= Hexagon::R20
;
696 Register
= Hexagon::R19
;
699 Register
= Hexagon::R18
;
702 Register
= Hexagon::R17
;
705 Register
= Hexagon::R16
;
710 void HexagonMCChecker::reportErrorRegisters(unsigned Register
) {
711 reportError("register `" + Twine(RI
.getName(Register
)) +
712 "' modified more than once");
715 void HexagonMCChecker::reportErrorNewValue(unsigned Register
) {
716 reportError("register `" + Twine(RI
.getName(Register
)) +
717 "' used with `.new' "
718 "but not validly modified in the same packet");
721 void HexagonMCChecker::reportError(Twine
const &Msg
) {
722 reportError(MCB
.getLoc(), Msg
);
725 void HexagonMCChecker::reportError(SMLoc Loc
, Twine
const &Msg
) {
727 Context
.reportError(Loc
, Msg
);
730 void HexagonMCChecker::reportNote(SMLoc Loc
, llvm::Twine
const &Msg
) {
732 auto SM
= Context
.getSourceManager();
734 SM
->PrintMessage(Loc
, SourceMgr::DK_Note
, Msg
);
738 void HexagonMCChecker::reportWarning(Twine
const &Msg
) {
740 Context
.reportWarning(MCB
.getLoc(), Msg
);
743 bool HexagonMCChecker::checkLegalVecRegPair() {
744 const bool IsPermitted
= STI
.getFeatureBits()[Hexagon::ArchV67
];
745 const bool HasReversePairs
= ReversePairs
.size() != 0;
747 if (!IsPermitted
&& HasReversePairs
) {
748 for (auto R
: ReversePairs
)
749 reportError("register pair `" + Twine(RI
.getName(R
)) +
750 "' is not permitted for this architecture");