1 //===----- HexagonMCChecker.cpp - Instruction bundle checking -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the checking of insns inside a bundle according to the
11 // packet constraint rules of the Hexagon ISA.
13 //===----------------------------------------------------------------------===//
15 #include "HexagonMCChecker.h"
17 #include "HexagonBaseInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCInstrDesc.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/raw_ostream.h"
30 RelaxNVChecks("relax-nv-checks", cl::init(false), cl::ZeroOrMore,
31 cl::Hidden, cl::desc("Relax checks of new-value validity"));
33 const HexagonMCChecker::PredSense
34 HexagonMCChecker::Unconditional(Hexagon::NoRegister, false);
36 void HexagonMCChecker::init() {
37 // Initialize read-only registers set.
38 ReadOnly.insert(Hexagon::PC);
39 ReadOnly.insert(Hexagon::C9_8);
41 // Figure out the loop-registers definitions.
42 if (HexagonMCInstrInfo::isInnerLoop(MCB)) {
43 Defs[Hexagon::SA0].insert(Unconditional); // FIXME: define or change SA0?
44 Defs[Hexagon::LC0].insert(Unconditional);
46 if (HexagonMCInstrInfo::isOuterLoop(MCB)) {
47 Defs[Hexagon::SA1].insert(Unconditional); // FIXME: define or change SA0?
48 Defs[Hexagon::LC1].insert(Unconditional);
51 if (HexagonMCInstrInfo::isBundle(MCB))
53 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
54 MCInst const &Inst = *I.getInst();
55 if (HexagonMCInstrInfo::isDuplex(MCII, Inst)) {
56 init(*Inst.getOperand(0).getInst());
57 init(*Inst.getOperand(1).getInst());
65 void HexagonMCChecker::initReg(MCInst const &MCI, unsigned R, unsigned &PredReg,
67 if (HexagonMCInstrInfo::isPredicated(MCII, MCI) && isPredicateRegister(R)) {
68 // Note an used predicate register.
70 isTrue = HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI);
72 // Note use of new predicate register.
73 if (HexagonMCInstrInfo::isPredicatedNew(MCII, MCI))
74 NewPreds.insert(PredReg);
76 // Note register use. Super-registers are not tracked directly,
77 // but their components.
78 for (MCRegAliasIterator SRI(R, &RI, !MCSubRegIterator(R, &RI).isValid());
80 if (!MCSubRegIterator(*SRI, &RI).isValid())
81 // Skip super-registers used indirectly.
85 void HexagonMCChecker::init(MCInst const &MCI) {
86 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MCI);
87 unsigned PredReg = Hexagon::NoRegister;
90 // Get used registers.
91 for (unsigned i = MCID.getNumDefs(); i < MCID.getNumOperands(); ++i)
92 if (MCI.getOperand(i).isReg())
93 initReg(MCI, MCI.getOperand(i).getReg(), PredReg, isTrue);
94 for (unsigned i = 0; i < MCID.getNumImplicitUses(); ++i)
95 initReg(MCI, MCID.getImplicitUses()[i], PredReg, isTrue);
97 // Get implicit register definitions.
98 if (const MCPhysReg *ImpDef = MCID.getImplicitDefs())
99 for (; *ImpDef; ++ImpDef) {
100 unsigned R = *ImpDef;
102 if (Hexagon::R31 != R && MCID.isCall())
103 // Any register other than the LR and the PC are actually volatile ones
104 // as defined by the ABI, not modified implicitly by the call insn.
106 if (Hexagon::PC == R)
107 // Branches are the only insns that can change the PC,
108 // otherwise a read-only register.
111 if (Hexagon::USR_OVF == R)
112 // Many insns change the USR implicitly, but only one or another flag.
113 // The instruction table models the USR.OVF flag, which can be
114 // implicitly modified more than once, but cannot be modified in the
115 // same packet with an instruction that modifies is explicitly. Deal
116 // with such situations individually.
118 else if (isPredicateRegister(R) &&
119 HexagonMCInstrInfo::isPredicateLate(MCII, MCI))
120 // Include implicit late predicates.
123 Defs[R].insert(PredSense(PredReg, isTrue));
126 // Figure out explicit register definitions.
127 for (unsigned i = 0; i < MCID.getNumDefs(); ++i) {
128 unsigned R = MCI.getOperand(i).getReg(), S = Hexagon::NoRegister;
129 // USR has subregisters (while C8 does not for technical reasons), so
130 // reset R to USR, since we know how to handle multiple defs of USR,
131 // taking into account its subregisters.
132 if (R == Hexagon::C8)
135 // Note register definitions, direct ones as well as indirect side-effects.
136 // Super-registers are not tracked directly, but their components.
137 for (MCRegAliasIterator SRI(R, &RI, !MCSubRegIterator(R, &RI).isValid());
138 SRI.isValid(); ++SRI) {
139 if (MCSubRegIterator(*SRI, &RI).isValid())
140 // Skip super-registers defined indirectly.
145 // Avoid scoring the defined register multiple times.
148 // Note that the defined register has already been scored.
152 if (Hexagon::P3_0 != R && Hexagon::P3_0 == *SRI)
153 // P3:0 is a special case, since multiple predicate register definitions
154 // in a packet is allowed as the equivalent of their logical "and".
155 // Only an explicit definition of P3:0 is noted as such; if a
156 // side-effect, then note as a soft definition.
157 SoftDefs.insert(*SRI);
158 else if (HexagonMCInstrInfo::isPredicateLate(MCII, MCI) &&
159 isPredicateRegister(*SRI))
160 // Some insns produce predicates too late to be used in the same packet.
161 LatePreds.insert(*SRI);
162 else if (i == 0 && llvm::HexagonMCInstrInfo::getType(MCII, MCI) ==
163 HexagonII::TypeCVI_VM_TMP_LD)
164 // Temporary loads should be used in the same packet, but don't commit
165 // results, so it should be disregarded if another insn changes the same
167 // TODO: relies on the impossibility of a current and a temporary loads
168 // in the same packet.
169 TmpDefs.insert(*SRI);
170 else if (i <= 1 && llvm::HexagonMCInstrInfo::hasNewValue2(MCII, MCI))
171 // vshuff(Vx, Vy, Rx) <- Vx(0) and Vy(1) are both source and
172 // destination registers with this instruction. same for vdeal(Vx,Vy,Rx)
175 Defs[*SRI].insert(PredSense(PredReg, isTrue));
179 // Figure out register definitions that produce new values.
180 if (HexagonMCInstrInfo::hasNewValue(MCII, MCI)) {
181 unsigned R = HexagonMCInstrInfo::getNewValueOperand(MCII, MCI).getReg();
183 if (HexagonMCInstrInfo::isCompound(MCII, MCI))
184 compoundRegisterMap(R); // Compound insns have a limited register range.
186 for (MCRegAliasIterator SRI(R, &RI, !MCSubRegIterator(R, &RI).isValid());
187 SRI.isValid(); ++SRI)
188 if (!MCSubRegIterator(*SRI, &RI).isValid())
189 // No super-registers defined indirectly.
190 NewDefs[*SRI].push_back(NewSense::Def(
191 PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI),
192 HexagonMCInstrInfo::isFloat(MCII, MCI)));
194 // For fairly unique 2-dot-new producers, example:
195 // vdeal(V1, V9, R0) V1.new and V9.new can be used by consumers.
196 if (HexagonMCInstrInfo::hasNewValue2(MCII, MCI)) {
197 unsigned R2 = HexagonMCInstrInfo::getNewValueOperand2(MCII, MCI).getReg();
199 bool HasSubRegs = MCSubRegIterator(R2, &RI).isValid();
200 for (MCRegAliasIterator SRI(R2, &RI, !HasSubRegs); SRI.isValid(); ++SRI)
201 if (!MCSubRegIterator(*SRI, &RI).isValid())
202 NewDefs[*SRI].push_back(NewSense::Def(
203 PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI),
204 HexagonMCInstrInfo::isFloat(MCII, MCI)));
208 // Figure out definitions of new predicate registers.
209 if (HexagonMCInstrInfo::isPredicatedNew(MCII, MCI))
210 for (unsigned i = MCID.getNumDefs(); i < MCID.getNumOperands(); ++i)
211 if (MCI.getOperand(i).isReg()) {
212 unsigned P = MCI.getOperand(i).getReg();
214 if (isPredicateRegister(P))
218 // Figure out uses of new values.
219 if (HexagonMCInstrInfo::isNewValue(MCII, MCI)) {
220 unsigned N = HexagonMCInstrInfo::getNewValueOperand(MCII, MCI).getReg();
222 if (!MCSubRegIterator(N, &RI).isValid()) {
223 // Super-registers cannot use new values.
225 NewUses[N] = NewSense::Jmp(
226 llvm::HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeNCJ);
228 NewUses[N] = NewSense::Use(
229 PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI));
234 HexagonMCChecker::HexagonMCChecker(MCContext &Context, MCInstrInfo const &MCII,
235 MCSubtargetInfo const &STI, MCInst &mcb,
236 MCRegisterInfo const &ri, bool ReportErrors)
237 : Context(Context), MCB(mcb), RI(ri), MCII(MCII), STI(STI),
238 ReportErrors(ReportErrors) {
242 bool HexagonMCChecker::check(bool FullCheck) {
243 bool chkB = checkBranches();
244 bool chkP = checkPredicates();
245 bool chkNV = checkNewValues();
246 bool chkR = checkRegisters();
247 bool chkRRO = checkRegistersReadOnly();
248 bool chkELB = checkEndloopBranches();
249 checkRegisterCurDefs();
250 bool chkS = checkSolo();
253 chkSh = checkShuffle();
256 chkSl = checkSlots();
257 bool chkAXOK = checkAXOK();
258 bool chk = chkB && chkP && chkNV && chkR && chkRRO && chkELB && chkS &&
259 chkSh && chkSl && chkAXOK;
264 bool HexagonMCChecker::checkEndloopBranches() {
265 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
266 MCInstrDesc const &Desc = HexagonMCInstrInfo::getDesc(MCII, I);
267 if (Desc.isBranch() || Desc.isCall()) {
268 auto Inner = HexagonMCInstrInfo::isInnerLoop(MCB);
269 if (Inner || HexagonMCInstrInfo::isOuterLoop(MCB)) {
270 reportError(I.getLoc(),
271 llvm::Twine("packet marked with `:endloop") +
272 (Inner ? "0" : "1") + "' " +
273 "cannot contain instructions that modify register " +
274 "`" + llvm::Twine(RI.getName(Hexagon::PC)) + "'");
283 bool isDuplexAGroup(unsigned Opcode) {
285 case Hexagon::SA1_addi:
286 case Hexagon::SA1_addrx:
287 case Hexagon::SA1_addsp:
288 case Hexagon::SA1_and1:
289 case Hexagon::SA1_clrf:
290 case Hexagon::SA1_clrfnew:
291 case Hexagon::SA1_clrt:
292 case Hexagon::SA1_clrtnew:
293 case Hexagon::SA1_cmpeqi:
294 case Hexagon::SA1_combine0i:
295 case Hexagon::SA1_combine1i:
296 case Hexagon::SA1_combine2i:
297 case Hexagon::SA1_combine3i:
298 case Hexagon::SA1_combinerz:
299 case Hexagon::SA1_combinezr:
300 case Hexagon::SA1_dec:
301 case Hexagon::SA1_inc:
302 case Hexagon::SA1_seti:
303 case Hexagon::SA1_setin1:
304 case Hexagon::SA1_sxtb:
305 case Hexagon::SA1_sxth:
306 case Hexagon::SA1_tfr:
307 case Hexagon::SA1_zxtb:
308 case Hexagon::SA1_zxth:
316 bool isNeitherAnorX(MCInstrInfo const &MCII, MCInst const &ID) {
318 unsigned Type = HexagonMCInstrInfo::getType(MCII, ID);
319 if (Type == HexagonII::TypeDUPLEX) {
320 unsigned subInst0Opcode = ID.getOperand(0).getInst()->getOpcode();
321 unsigned subInst1Opcode = ID.getOperand(1).getInst()->getOpcode();
322 Result += !isDuplexAGroup(subInst0Opcode);
323 Result += !isDuplexAGroup(subInst1Opcode);
326 Type != HexagonII::TypeALU32_2op && Type != HexagonII::TypeALU32_3op &&
327 Type != HexagonII::TypeALU32_ADDI && Type != HexagonII::TypeS_2op &&
328 Type != HexagonII::TypeS_3op &&
329 (Type != HexagonII::TypeALU64 || HexagonMCInstrInfo::isFloat(MCII, ID));
334 bool HexagonMCChecker::checkAXOK() {
335 MCInst const *HasSoloAXInst = nullptr;
336 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
337 if (HexagonMCInstrInfo::isSoloAX(MCII, I)) {
343 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
344 if (&I != HasSoloAXInst && isNeitherAnorX(MCII, I)) {
346 HasSoloAXInst->getLoc(),
347 llvm::Twine("Instruction can only be in a packet with ALU or "
348 "non-FPU XTYPE instructions"));
349 reportError(I.getLoc(),
350 llvm::Twine("Not an ALU or non-FPU XTYPE instruction"));
357 bool HexagonMCChecker::checkSlots() {
358 unsigned slotsUsed = 0;
359 for (auto HMI : HexagonMCInstrInfo::bundleInstructions(MCB)) {
360 MCInst const &MCI = *HMI.getInst();
361 if (HexagonMCInstrInfo::isImmext(MCI))
363 if (HexagonMCInstrInfo::isDuplex(MCII, MCI))
369 if (slotsUsed > HEXAGON_PACKET_SIZE) {
370 reportError("invalid instruction packet: out of slots");
376 // Check legal use of branches.
377 bool HexagonMCChecker::checkBranches() {
378 if (HexagonMCInstrInfo::isBundle(MCB)) {
379 bool hasConditional = false;
380 unsigned Branches = 0, Conditional = HEXAGON_PRESHUFFLE_PACKET_SIZE,
381 Unconditional = HEXAGON_PRESHUFFLE_PACKET_SIZE;
383 for (unsigned i = HexagonMCInstrInfo::bundleInstructionsOffset;
384 i < MCB.size(); ++i) {
385 MCInst const &MCI = *MCB.begin()[i].getInst();
387 if (HexagonMCInstrInfo::isImmext(MCI))
389 if (HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch() ||
390 HexagonMCInstrInfo::getDesc(MCII, MCI).isCall()) {
392 if (HexagonMCInstrInfo::isPredicated(MCII, MCI) ||
393 HexagonMCInstrInfo::isPredicatedNew(MCII, MCI)) {
394 hasConditional = true;
395 Conditional = i; // Record the position of the conditional branch.
397 Unconditional = i; // Record the position of the unconditional branch.
403 if (!hasConditional || Conditional > Unconditional) {
404 // Error out if more than one unconditional branch or
405 // the conditional branch appears after the unconditional one.
407 "unconditional branch cannot precede another branch in packet");
415 // Check legal use of predicate registers.
416 bool HexagonMCChecker::checkPredicates() {
417 // Check for proper use of new predicate registers.
418 for (const auto &I : NewPreds) {
421 if (!Defs.count(P) || LatePreds.count(P)) {
422 // Error out if the new predicate register is not defined,
424 // (e.g., "{ if (p3.new)... ; p3 = sp1loop0(#r7:2, Rs) }").
425 reportErrorNewValue(P);
430 // Check for proper use of auto-anded of predicate registers.
431 for (const auto &I : LatePreds) {
434 if (LatePreds.count(P) > 1 || Defs.count(P)) {
435 // Error out if predicate register defined "late" multiple times or
436 // defined late and regularly defined
437 // (e.g., "{ p3 = sp1loop0(...); p3 = cmp.eq(...) }".
438 reportErrorRegisters(P);
446 // Check legal use of new values.
447 bool HexagonMCChecker::checkNewValues() {
448 for (auto &I : NewUses) {
449 unsigned R = I.first;
450 NewSense &US = I.second;
452 if (!hasValidNewValueDef(US, NewDefs[R])) {
453 reportErrorNewValue(R);
461 bool HexagonMCChecker::checkRegistersReadOnly() {
462 for (auto I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
463 MCInst const &Inst = *I.getInst();
464 unsigned Defs = HexagonMCInstrInfo::getDesc(MCII, Inst).getNumDefs();
465 for (unsigned j = 0; j < Defs; ++j) {
466 MCOperand const &Operand = Inst.getOperand(j);
467 assert(Operand.isReg() && "Def is not a register");
468 unsigned Register = Operand.getReg();
469 if (ReadOnly.find(Register) != ReadOnly.end()) {
470 reportError(Inst.getLoc(), "Cannot write to read-only register `" +
471 llvm::Twine(RI.getName(Register)) + "'");
479 bool HexagonMCChecker::registerUsed(unsigned Register) {
480 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB))
481 for (unsigned j = HexagonMCInstrInfo::getDesc(MCII, I).getNumDefs(),
482 n = I.getNumOperands();
484 MCOperand const &Operand = I.getOperand(j);
485 if (Operand.isReg() && Operand.getReg() == Register)
491 void HexagonMCChecker::checkRegisterCurDefs() {
492 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
493 if (HexagonMCInstrInfo::isCVINew(MCII, I) &&
494 HexagonMCInstrInfo::getDesc(MCII, I).mayLoad()) {
495 unsigned Register = I.getOperand(0).getReg();
496 if (!registerUsed(Register))
497 reportWarning("Register `" + llvm::Twine(RI.getName(Register)) +
498 "' used with `.cur' "
499 "but not used in the same packet");
504 // Check for legal register uses and definitions.
505 bool HexagonMCChecker::checkRegisters() {
506 // Check for proper register definitions.
507 for (const auto &I : Defs) {
508 unsigned R = I.first;
510 if (isLoopRegister(R) && Defs.count(R) > 1 &&
511 (HexagonMCInstrInfo::isInnerLoop(MCB) ||
512 HexagonMCInstrInfo::isOuterLoop(MCB))) {
513 // Error out for definitions of loop registers at the end of a loop.
514 reportError("loop-setup and some branch instructions "
515 "cannot be in the same packet");
518 if (SoftDefs.count(R)) {
519 // Error out for explicit changes to registers also weakly defined
520 // (e.g., "{ usr = r0; r0 = sfadd(...) }").
521 unsigned UsrR = Hexagon::USR; // Silence warning about mixed types in ?:.
522 unsigned BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R;
523 reportErrorRegisters(BadR);
526 if (!isPredicateRegister(R) && Defs[R].size() > 1) {
527 // Check for multiple register definitions.
528 PredSet &PM = Defs[R];
530 // Check for multiple unconditional register definitions.
531 if (PM.count(Unconditional)) {
532 // Error out on an unconditional change when there are any other
533 // changes, conditional or not.
534 unsigned UsrR = Hexagon::USR;
535 unsigned BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R;
536 reportErrorRegisters(BadR);
539 // Check for multiple conditional register definitions.
540 for (const auto &J : PM) {
543 // Check for multiple uses of the same condition.
544 if (PM.count(P) > 1) {
545 // Error out on conditional changes based on the same predicate
546 // (e.g., "{ if (!p0) r0 =...; if (!p0) r0 =... }").
547 reportErrorRegisters(R);
550 // Check for the use of the complementary condition.
551 P.second = !P.second;
552 if (PM.count(P) && PM.size() > 2) {
553 // Error out on conditional changes based on the same predicate
555 // (e.g., "if (p0) r0 =...; if (!p0) r0 =... }; if (!p0) r0 =...").
556 reportErrorRegisters(R);
563 // Check for use of temporary definitions.
564 for (const auto &I : TmpDefs) {
567 if (!Uses.count(R)) {
568 // special case for vhist
569 bool vHistFound = false;
570 for (auto const &HMI : HexagonMCInstrInfo::bundleInstructions(MCB)) {
571 if (llvm::HexagonMCInstrInfo::getType(MCII, *HMI.getInst()) ==
572 HexagonII::TypeCVI_HIST) {
573 vHistFound = true; // vhist() implicitly uses ALL REGxx.tmp
577 // Warn on an unused temporary definition.
578 if (vHistFound == false) {
579 reportWarning("register `" + llvm::Twine(RI.getName(R)) +
580 "' used with `.tmp' "
581 "but not used in the same packet");
590 // Check for legal use of solo insns.
591 bool HexagonMCChecker::checkSolo() {
592 if (HexagonMCInstrInfo::bundleSize(MCB) > 1)
593 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
594 if (llvm::HexagonMCInstrInfo::isSolo(MCII, I)) {
595 reportError(I.getLoc(), "Instruction is marked `isSolo' and "
596 "cannot have other instructions in "
605 bool HexagonMCChecker::checkShuffle() {
606 HexagonMCShuffler MCSDX(Context, ReportErrors, MCII, STI, MCB);
607 return MCSDX.check();
610 void HexagonMCChecker::compoundRegisterMap(unsigned &Register) {
615 Register = Hexagon::R23;
618 Register = Hexagon::R22;
621 Register = Hexagon::R21;
624 Register = Hexagon::R20;
627 Register = Hexagon::R19;
630 Register = Hexagon::R18;
633 Register = Hexagon::R17;
636 Register = Hexagon::R16;
641 bool HexagonMCChecker::hasValidNewValueDef(const NewSense &Use,
642 const NewSenseList &Defs) const {
643 bool Strict = !RelaxNVChecks;
645 for (unsigned i = 0, n = Defs.size(); i < n; ++i) {
646 const NewSense &Def = Defs[i];
647 // NVJ cannot use a new FP value [7.6.1]
648 if (Use.IsNVJ && (Def.IsFloat || Def.PredReg != 0))
650 // If the definition was not predicated, then it does not matter if
652 if (Def.PredReg == 0)
654 // With the strict checks, both the definition and the use must be
655 // predicated on the same register and condition.
657 if (Def.PredReg == Use.PredReg && Def.Cond == Use.Cond)
660 // With the relaxed checks, if the definition was predicated, the only
661 // detectable violation is if the use is predicated on the opposing
662 // condition, otherwise, it's ok.
663 if (Def.PredReg != Use.PredReg || Def.Cond == Use.Cond)
670 void HexagonMCChecker::reportErrorRegisters(unsigned Register) {
671 reportError("register `" + llvm::Twine(RI.getName(Register)) +
672 "' modified more than once");
675 void HexagonMCChecker::reportErrorNewValue(unsigned Register) {
676 reportError("register `" + llvm::Twine(RI.getName(Register)) +
677 "' used with `.new' "
678 "but not validly modified in the same packet");
681 void HexagonMCChecker::reportError(llvm::Twine const &Msg) {
682 reportError(MCB.getLoc(), Msg);
685 void HexagonMCChecker::reportError(SMLoc Loc, llvm::Twine const &Msg) {
687 Context.reportError(Loc, Msg);
690 void HexagonMCChecker::reportWarning(llvm::Twine const &Msg) {
692 auto SM = Context.getSourceManager();
694 SM->PrintMessage(MCB.getLoc(), SourceMgr::DK_Warning, Msg);