LLVM 24.0.0git
HexagonMCChecker.cpp
Go to the documentation of this file.
1//===----- HexagonMCChecker.cpp - Instruction bundle checking -------------===//
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
19
20#include "llvm/ADT/Twine.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCInstrDesc.h"
28#include <cassert>
29
30using namespace llvm;
31
32static cl::opt<bool>
33 RelaxNVChecks("relax-nv-checks", cl::Hidden,
34 cl::desc("Relax checks of new-value validity"));
35
36const HexagonMCChecker::PredSense
37 HexagonMCChecker::Unconditional(Hexagon::NoRegister, false);
38
39void HexagonMCChecker::init() {
40 // Initialize read-only registers set.
41 ReadOnly.insert(Hexagon::PC);
42 ReadOnly.insert(Hexagon::C9_8);
43 ReadOnly.insert(Hexagon::UPMUCNT0);
44 ReadOnly.insert(Hexagon::UPMUCNT1);
45 ReadOnly.insert(Hexagon::UPMUCNT2);
46 ReadOnly.insert(Hexagon::UPMUCNT3);
47 ReadOnly.insert(Hexagon::UPMUCNT4);
48 ReadOnly.insert(Hexagon::UPMUCNT5);
49 ReadOnly.insert(Hexagon::UPMUCNT6);
50 ReadOnly.insert(Hexagon::UPMUCNT7);
51 ReadOnly.insert(Hexagon::C21_20);
52 ReadOnly.insert(Hexagon::C23_22);
53 ReadOnly.insert(Hexagon::C25_24);
54 ReadOnly.insert(Hexagon::C27_26);
55
56 // Figure out the loop-registers definitions.
58 Defs[Hexagon::SA0].insert(Unconditional); // FIXME: define or change SA0?
59 Defs[Hexagon::LC0].insert(Unconditional);
60 }
62 Defs[Hexagon::SA1].insert(Unconditional); // FIXME: define or change SA0?
63 Defs[Hexagon::LC1].insert(Unconditional);
64 }
65
67 // Unfurl a bundle.
68 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
69 MCInst const &Inst = *I.getInst();
70 if (HexagonMCInstrInfo::isDuplex(MCII, Inst)) {
71 init(*Inst.getOperand(0).getInst());
72 init(*Inst.getOperand(1).getInst());
73 } else
74 init(Inst);
75 }
76 else
77 init(MCB);
78}
79
80void HexagonMCChecker::initReg(MCInst const &MCI, MCRegister R,
81 MCRegister &PredReg, bool &isTrue) {
82 if (HexagonMCInstrInfo::isPredicated(MCII, MCI) &&
84 // Note an used predicate register.
85 PredReg = R;
86 isTrue = HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI);
87
88 // Note use of new predicate register.
90 NewPreds.insert(PredReg);
91 } else
92 // Note register use. Super-registers are not tracked directly,
93 // but their components.
94 for (MCRegAliasIterator SRI(R, &RI, RI.subregs(R).empty()); SRI.isValid();
95 ++SRI)
96 if (RI.subregs(*SRI).empty())
97 // Skip super-registers used indirectly.
98 Uses.insert(*SRI);
99
101 ReversePairs.insert(R);
102}
103
104void HexagonMCChecker::init(MCInst const &MCI) {
105 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MCI);
106 MCRegister PredReg;
107 bool isTrue = false;
108
109 // Get used registers.
110 for (unsigned i = MCID.getNumDefs(); i < MCID.getNumOperands(); ++i)
111 if (MCI.getOperand(i).isReg())
112 initReg(MCI, MCI.getOperand(i).getReg(), PredReg, isTrue);
113 for (MCPhysReg ImpUse : MCID.implicit_uses())
114 initReg(MCI, ImpUse, PredReg, isTrue);
115
116 const bool IgnoreTmpDst = (HexagonMCInstrInfo::hasTmpDst(MCII, MCI) ||
117 HexagonMCInstrInfo::isTmpWrite(MCII, MCI)) &&
118 STI.hasFeature(Hexagon::ArchV69);
119
120 // Get implicit register definitions.
121 for (MCPhysReg R : MCID.implicit_defs()) {
122 if (Hexagon::R31 != R && MCID.isCall())
123 // Any register other than the LR and the PC are actually volatile ones
124 // as defined by the ABI, not modified implicitly by the call insn.
125 continue;
126 if (Hexagon::PC == R)
127 // Branches are the only insns that can change the PC,
128 // otherwise a read-only register.
129 continue;
130
131 if (Hexagon::USR_OVF == R)
132 // Many insns change the USR implicitly, but only one or another flag.
133 // The instruction table models the USR.OVF flag, which can be
134 // implicitly modified more than once, but cannot be modified in the
135 // same packet with an instruction that modifies is explicitly. Deal
136 // with such situations individually.
137 SoftDefs.insert(R);
138 else if (HexagonMCInstrInfo::isPredReg(RI, R) &&
140 // Include implicit late predicates.
141 LatePreds.insert(R);
142 else if (!IgnoreTmpDst)
143 Defs[R].insert(PredSense(PredReg, isTrue));
144 }
145
146 // Figure out explicit register definitions.
147 for (unsigned i = 0; i < MCID.getNumDefs(); ++i) {
148 MCRegister R = MCI.getOperand(i).getReg(), S = MCRegister();
149 // USR has subregisters (while C8 does not for technical reasons), so
150 // reset R to USR, since we know how to handle multiple defs of USR,
151 // taking into account its subregisters.
152 if (R == Hexagon::C8)
153 R = Hexagon::USR;
154
156 ReversePairs.insert(R);
157
158 // Note register definitions, direct ones as well as indirect side-effects.
159 // Super-registers are not tracked directly, but their components.
160 for (MCRegAliasIterator SRI(R, &RI, RI.subregs(R).empty()); SRI.isValid();
161 ++SRI) {
162 if (!RI.subregs(*SRI).empty())
163 // Skip super-registers defined indirectly.
164 continue;
165
166 if (R == *SRI) {
167 if (S == R)
168 // Avoid scoring the defined register multiple times.
169 continue;
170 else
171 // Note that the defined register has already been scored.
172 S = R;
173 }
174
175 if (Hexagon::P3_0 != R && Hexagon::P3_0 == *SRI)
176 // P3:0 is a special case, since multiple predicate register definitions
177 // in a packet is allowed as the equivalent of their logical "and".
178 // Only an explicit definition of P3:0 is noted as such; if a
179 // side-effect, then note as a soft definition.
180 SoftDefs.insert(*SRI);
181 else if (HexagonMCInstrInfo::isPredicateLate(MCII, MCI) &&
183 // Some insns produce predicates too late to be used in the same packet.
184 LatePreds.insert(*SRI);
185 else if (i == 0 && HexagonMCInstrInfo::getType(MCII, MCI) ==
187 // Temporary loads should be used in the same packet, but don't commit
188 // results, so it should be disregarded if another insn changes the same
189 // register.
190 // TODO: relies on the impossibility of a current and a temporary loads
191 // in the same packet.
192 TmpDefs.insert(*SRI);
193 else if (!IgnoreTmpDst)
194 Defs[*SRI].insert(PredSense(PredReg, isTrue));
195 }
196 }
197
198 // Figure out definitions of new predicate registers.
200 for (unsigned i = MCID.getNumDefs(); i < MCID.getNumOperands(); ++i)
201 if (MCI.getOperand(i).isReg()) {
202 MCRegister P = MCI.getOperand(i).getReg();
203
205 NewPreds.insert(P);
206 }
207}
208
210 MCSubtargetInfo const &STI, MCInst &mcb,
211 MCRegisterInfo const &ri, bool ReportErrors)
212 : Context(Context), MCB(mcb), RI(ri), MCII(MCII), STI(STI),
213 ReportErrors(ReportErrors) {
214 init();
215}
216
218 MCSubtargetInfo const &STI,
219 bool CopyReportErrors)
220 : Context(Other.Context), MCB(Other.MCB), RI(Other.RI), MCII(Other.MCII),
221 STI(STI), ReportErrors(CopyReportErrors ? Other.ReportErrors : false) {
222 init();
223}
224
225bool HexagonMCChecker::check(bool FullCheck) {
226 bool chkP = checkPredicates();
227 bool chkNV = checkNewValues();
228 bool chkR = checkRegisters();
229 bool chkRRO = checkRegistersReadOnly();
230 checkRegisterCurDefs();
231 bool chkS = checkSolo();
232 bool chkSh = true;
233 if (FullCheck)
234 chkSh = checkShuffle();
235 bool chkSl = true;
236 if (FullCheck)
237 chkSl = checkSlots();
238 bool chkAXOK = checkAXOK();
239 bool chkCofMax1 = checkCOFMax1();
240 bool chkHWLoop = checkHWLoop();
241 bool chkHMX = checkValidHMX();
242 bool chkValidTmpDst = FullCheck ? checkValidTmpDst() : true;
243 bool chkLegalVecRegPair = checkLegalVecRegPair();
244 bool ChkHVXAccum = checkHVXAccum();
245 bool ChkHVXRestrictions = checkHVXRestrictions();
246 bool ChkHLX64BitRegUse = checkHLX64BitRegUse();
247 bool chk = chkP && chkNV && chkR && chkRRO && chkS && chkSh && chkSl &&
248 chkAXOK && chkCofMax1 && chkHWLoop && chkHMX && chkValidTmpDst &&
249 chkLegalVecRegPair && ChkHVXAccum && ChkHVXRestrictions &&
250 ChkHLX64BitRegUse;
251
252 return chk;
253}
254
255static bool isDuplexAGroup(unsigned Opcode) {
256 switch (Opcode) {
257 case Hexagon::SA1_addi:
258 case Hexagon::SA1_addrx:
259 case Hexagon::SA1_addsp:
260 case Hexagon::SA1_and1:
261 case Hexagon::SA1_clrf:
262 case Hexagon::SA1_clrfnew:
263 case Hexagon::SA1_clrt:
264 case Hexagon::SA1_clrtnew:
265 case Hexagon::SA1_cmpeqi:
266 case Hexagon::SA1_combine0i:
267 case Hexagon::SA1_combine1i:
268 case Hexagon::SA1_combine2i:
269 case Hexagon::SA1_combine3i:
270 case Hexagon::SA1_combinerz:
271 case Hexagon::SA1_combinezr:
272 case Hexagon::SA1_dec:
273 case Hexagon::SA1_inc:
274 case Hexagon::SA1_seti:
275 case Hexagon::SA1_setin1:
276 case Hexagon::SA1_sxtb:
277 case Hexagon::SA1_sxth:
278 case Hexagon::SA1_tfr:
279 case Hexagon::SA1_zxtb:
280 case Hexagon::SA1_zxth:
281 return true;
282 break;
283 default:
284 return false;
285 }
286}
287
288static bool isNeitherAnorX(MCInstrInfo const &MCII, MCInst const &ID) {
289 if (HexagonMCInstrInfo::isFloat(MCII, ID))
290 return true;
291 unsigned Type = HexagonMCInstrInfo::getType(MCII, ID);
292 switch (Type) {
299 case HexagonII::TypeM:
301 return false;
303 return !isDuplexAGroup(ID.getOpcode());
304 }
306 llvm_unreachable("unexpected duplex instruction");
307 default:
308 return true;
309 }
310}
311
312bool HexagonMCChecker::checkAXOK() {
313 MCInst const *HasSoloAXInst = nullptr;
314 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
315 if (HexagonMCInstrInfo::isSoloAX(MCII, I)) {
316 HasSoloAXInst = &I;
317 }
318 }
319 if (!HasSoloAXInst)
320 return true;
321 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
322 if (&I != HasSoloAXInst && isNeitherAnorX(MCII, I)) {
324 HasSoloAXInst->getLoc(),
325 Twine("Instruction can only be in a packet with ALU or non-FPU XTYPE "
326 "instructions"));
327 reportError(I.getLoc(),
328 Twine("Not an ALU or non-FPU XTYPE instruction"));
329 return false;
330 }
331 }
332 return true;
333}
334
336 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
338 reportNote(I.getLoc(), "Branching instruction");
339 }
340}
341
342bool HexagonMCChecker::checkHWLoop() {
345 return true;
346 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
347 if (HexagonMCInstrInfo::IsABranchingInst(MCII, STI, I)) {
348 reportError(MCB.getLoc(),
349 "Branches cannot be in a packet with hardware loops");
351 return false;
352 }
353 }
354 return true;
355}
356
357bool HexagonMCChecker::checkCOFMax1() {
358 SmallVector<MCInst const *, 2> BranchLocations;
359 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
361 BranchLocations.push_back(&I);
362 }
363 for (unsigned J = 0, N = BranchLocations.size(); J < N; ++J) {
364 MCInst const &I = *BranchLocations[J];
365 if (HexagonMCInstrInfo::isCofMax1(MCII, I)) {
366 bool Relax1 = HexagonMCInstrInfo::isCofRelax1(MCII, I);
367 bool Relax2 = HexagonMCInstrInfo::isCofRelax2(MCII, I);
368 if (N > 1 && !Relax1 && !Relax2) {
369 reportError(I.getLoc(),
370 "Instruction may not be in a packet with other branches");
372 return false;
373 }
374 if (N > 1 && J == 0 && !Relax1) {
375 reportError(I.getLoc(),
376 "Instruction may not be the first branch in packet");
378 return false;
379 }
380 if (N > 1 && J == 1 && !Relax2) {
381 reportError(I.getLoc(),
382 "Instruction may not be the second branch in packet");
384 return false;
385 }
386 }
387 }
388 return true;
389}
390
391bool HexagonMCChecker::checkSlots() {
392 if (HexagonMCInstrInfo::slotsConsumed(MCII, STI, MCB) >
394 reportError("invalid instruction packet: out of slots");
395 return false;
396 }
397 return true;
398}
399
400// Check legal use of predicate registers.
401bool HexagonMCChecker::checkPredicates() {
402 // Check for proper use of new predicate registers.
403 for (const auto &I : NewPreds) {
404 MCRegister P = I;
405
406 if (!Defs.count(P) || LatePreds.count(P) || Defs.count(Hexagon::P3_0)) {
407 // Error out if the new predicate register is not defined,
408 // or defined "late"
409 // (e.g., "{ if (p3.new)... ; p3 = sp1loop0(#r7:2, Rs) }").
411 return false;
412 }
413 }
414
415 // Check for proper use of auto-anded of predicate registers.
416 for (const auto &I : LatePreds) {
417 MCRegister P = I;
418
419 if (LatePreds.count(P) > 1 || Defs.count(P)) {
420 // Error out if predicate register defined "late" multiple times or
421 // defined late and regularly defined
422 // (e.g., "{ p3 = sp1loop0(...); p3 = cmp.eq(...) }".
424 return false;
425 }
426 }
427
428 return true;
429}
430
431// Check legal use of new values.
432bool HexagonMCChecker::checkNewValues() {
433 for (auto const &ConsumerInst :
435 if (!HexagonMCInstrInfo::isNewValue(MCII, ConsumerInst))
436 continue;
437
438 const HexagonMCInstrInfo::PredicateInfo ConsumerPredInfo =
439 HexagonMCInstrInfo::predicateInfo(MCII, ConsumerInst);
440
441 bool Branch = HexagonMCInstrInfo::getDesc(MCII, ConsumerInst).isBranch();
442 MCOperand const &Op =
443 HexagonMCInstrInfo::getNewValueOperand(MCII, ConsumerInst);
444 assert(Op.isReg());
445
446 auto Producer = registerProducer(Op.getReg(), ConsumerPredInfo);
447 const MCInst *const ProducerInst = std::get<0>(Producer);
448 const HexagonMCInstrInfo::PredicateInfo ProducerPredInfo =
449 std::get<2>(Producer);
450
451 if (ProducerInst == nullptr) {
452 reportError(ConsumerInst.getLoc(),
453 "New value register consumer has no producer");
454 return false;
455 }
456 if (!RelaxNVChecks) {
457 // Checks that statically prove correct new value consumption
458 if (ProducerPredInfo.isPredicated() &&
459 (!ConsumerPredInfo.isPredicated() ||
460 llvm::HexagonMCInstrInfo::getType(MCII, ConsumerInst) ==
463 ProducerInst->getLoc(),
464 "Register producer is predicated and consumer is unconditional");
465 reportError(ConsumerInst.getLoc(),
466 "Instruction does not have a valid new register producer");
467 return false;
468 }
469 if (ProducerPredInfo.Register != Hexagon::NoRegister &&
470 ProducerPredInfo.Register != ConsumerPredInfo.Register) {
471 reportNote(ProducerInst->getLoc(),
472 "Register producer does not use the same predicate "
473 "register as the consumer");
474 reportError(ConsumerInst.getLoc(),
475 "Instruction does not have a valid new register producer");
476 return false;
477 }
478 }
479 if (ProducerPredInfo.Register == ConsumerPredInfo.Register &&
480 ConsumerPredInfo.PredicatedTrue != ProducerPredInfo.PredicatedTrue) {
482 ProducerInst->getLoc(),
483 "Register producer has the opposite predicate sense as consumer");
484 reportError(ConsumerInst.getLoc(),
485 "Instruction does not have a valid new register producer");
486 return false;
487 }
488
489 MCInstrDesc const &Desc = HexagonMCInstrInfo::getDesc(MCII, *ProducerInst);
490 const unsigned ProducerOpIndex = std::get<1>(Producer);
491
492 if (Desc.operands()[ProducerOpIndex].RegClass ==
493 Hexagon::DoubleRegsRegClassID) {
494 reportNote(ProducerInst->getLoc(),
495 "Double registers cannot be new-value producers");
496 reportError(ConsumerInst.getLoc(),
497 "Instruction does not have a valid new register producer");
498 return false;
499 }
500
501 // The ProducerOpIsMemIndex logic checks for the index of the producer
502 // register operand. Z-reg load instructions have an implicit operand
503 // that's not encoded, so the producer won't appear as the 1-th def, it
504 // will be at the 0-th.
505 const unsigned ProducerOpSearchIndex =
506 (HexagonMCInstrInfo::getType(MCII, *ProducerInst) ==
508 ? 0
509 : 1;
510
511 const bool ProducerOpIsMemIndex =
512 ((Desc.mayLoad() && ProducerOpIndex == ProducerOpSearchIndex) ||
513 (Desc.mayStore() && ProducerOpIndex == 0));
514
515 if (ProducerOpIsMemIndex) {
516 unsigned Mode = HexagonMCInstrInfo::getAddrMode(MCII, *ProducerInst);
517
518 StringRef ModeError;
520 ModeError = "Absolute-set";
522 ModeError = "Auto-increment";
523 if (!ModeError.empty()) {
524 reportNote(ProducerInst->getLoc(),
525 ModeError + " registers cannot be a new-value "
526 "producer");
527 reportError(ConsumerInst.getLoc(),
528 "Instruction does not have a valid new register producer");
529 return false;
530 }
531 }
532 if (Branch && HexagonMCInstrInfo::isFloat(MCII, *ProducerInst)) {
533 reportNote(ProducerInst->getLoc(),
534 "FPU instructions cannot be new-value producers for jumps");
535 reportError(ConsumerInst.getLoc(),
536 "Instruction does not have a valid new register producer");
537 return false;
538 }
539 }
540 return true;
541}
542
543bool HexagonMCChecker::checkRegistersReadOnly() {
544 for (auto I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
545 MCInst const &Inst = *I.getInst();
546 unsigned Defs = HexagonMCInstrInfo::getDesc(MCII, Inst).getNumDefs();
547 for (unsigned j = 0; j < Defs; ++j) {
548 MCOperand const &Operand = Inst.getOperand(j);
549 assert(Operand.isReg() && "Def is not a register");
550 MCRegister Register = Operand.getReg();
551 if (ReadOnly.find(Register) != ReadOnly.end()) {
552 reportError(Inst.getLoc(), "Cannot write to read-only register `" +
553 Twine(RI.getName(Register)) + "'");
554 return false;
555 }
556 }
557 }
558 return true;
559}
560
561bool HexagonMCChecker::registerUsed(MCRegister Register) {
562 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB))
563 for (unsigned j = HexagonMCInstrInfo::getDesc(MCII, I).getNumDefs(),
564 n = I.getNumOperands();
565 j < n; ++j) {
566 MCOperand const &Operand = I.getOperand(j);
567 if (Operand.isReg() && Operand.getReg() == Register)
568 return true;
569 }
570 return false;
571}
572
573std::tuple<MCInst const *, unsigned, HexagonMCInstrInfo::PredicateInfo>
574HexagonMCChecker::registerProducer(
576 std::tuple<MCInst const *, unsigned, HexagonMCInstrInfo::PredicateInfo>
577 WrongSense;
578
579 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
580 MCInstrDesc const &Desc = HexagonMCInstrInfo::getDesc(MCII, I);
581 auto ProducerPredicate = HexagonMCInstrInfo::predicateInfo(MCII, I);
582
583 for (unsigned J = 0, N = Desc.getNumDefs(); J < N; ++J)
584 for (auto K = MCRegAliasIterator(I.getOperand(J).getReg(), &RI, true);
585 K.isValid(); ++K)
586 if (*K == Register) {
587 if (RelaxNVChecks ||
588 (ProducerPredicate.Register == ConsumerPredicate.Register &&
589 (ProducerPredicate.Register == Hexagon::NoRegister ||
590 ProducerPredicate.PredicatedTrue ==
591 ConsumerPredicate.PredicatedTrue)))
592 return std::make_tuple(&I, J, ProducerPredicate);
593 std::get<0>(WrongSense) = &I;
594 std::get<1>(WrongSense) = J;
595 std::get<2>(WrongSense) = ProducerPredicate;
596 }
597 if (Register == Hexagon::VTMP && HexagonMCInstrInfo::hasTmpDst(MCII, I))
598 return std::make_tuple(&I, 0, HexagonMCInstrInfo::PredicateInfo());
599 }
600 return WrongSense;
601}
602
603void HexagonMCChecker::checkRegisterCurDefs() {
604 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
605 if (HexagonMCInstrInfo::isCVINew(MCII, I) &&
606 HexagonMCInstrInfo::getDesc(MCII, I).mayLoad()) {
607 const MCRegister RegDef = I.getOperand(0).getReg();
608
609 bool HasRegDefUse = false;
610 for (MCRegAliasIterator Alias(RegDef, &RI, true); Alias.isValid();
611 ++Alias)
612 HasRegDefUse = HasRegDefUse || registerUsed(*Alias);
613
614 if (!HasRegDefUse)
615 reportWarning("Register `" + Twine(RI.getName(RegDef)) +
616 "' used with `.cur' "
617 "but not used in the same packet");
618 }
619 }
620}
621
622// Check for legal register uses and definitions.
623bool HexagonMCChecker::checkRegisters() {
624 // Check for proper register definitions.
625 for (const auto &I : Defs) {
626 MCRegister R = I.first;
627
628 if (isLoopRegister(R) && Defs.count(R) > 1 &&
631 // Error out for definitions of loop registers at the end of a loop.
632 reportError("loop-setup and some branch instructions "
633 "cannot be in the same packet");
634 return false;
635 }
636 if (SoftDefs.count(R)) {
637 // Error out for explicit changes to registers also weakly defined
638 // (e.g., "{ usr = r0; r0 = sfadd(...) }").
639 MCRegister UsrR = Hexagon::USR;
640 MCRegister BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R;
642 return false;
643 }
644 if (!HexagonMCInstrInfo::isPredReg(RI, R) && Defs[R].size() > 1) {
645 // Check for multiple register definitions.
646 PredSet &PM = Defs[R];
647
648 // Check for multiple unconditional register definitions.
649 if (PM.count(Unconditional)) {
650 // Error out on an unconditional change when there are any other
651 // changes, conditional or not.
652 MCRegister UsrR = Hexagon::USR;
653 MCRegister BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R;
655 return false;
656 }
657 // Check for multiple conditional register definitions.
658 for (const auto &J : PM) {
659 PredSense P = J;
660
661 // Check for multiple uses of the same condition.
662 if (PM.count(P) > 1) {
663 // Error out on conditional changes based on the same predicate
664 // (e.g., "{ if (!p0) r0 =...; if (!p0) r0 =... }").
666 return false;
667 }
668 // Check for the use of the complementary condition.
669 P.second = !P.second;
670 if (PM.count(P) && PM.size() > 2) {
671 // Error out on conditional changes based on the same predicate
672 // multiple times
673 // (e.g., "if (p0) r0 =...; if (!p0) r0 =... }; if (!p0) r0 =...").
675 return false;
676 }
677 }
678 }
679 }
680
681 // Check for use of temporary definitions.
682 for (const auto &I : TmpDefs) {
683 MCRegister R = I;
684
685 if (!Uses.count(R)) {
686 // special case for vhist
687 bool vHistFound = false;
688 for (auto const &HMI : HexagonMCInstrInfo::bundleInstructions(MCB)) {
689 if (HexagonMCInstrInfo::getType(MCII, *HMI.getInst()) ==
691 vHistFound = true; // vhist() implicitly uses ALL REGxx.tmp
692 break;
693 }
694 }
695 // Warn on an unused temporary definition.
696 if (!vHistFound) {
697 reportWarning("register `" + Twine(RI.getName(R)) +
698 "' used with `.tmp' but not used in the same packet");
699 return true;
700 }
701 }
702 }
703
704 return true;
705}
706
707bool HexagonMCChecker::checkValidHMX() {
708 bool hasWeight = false;
709 bool hasActivation = false;
710 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
711 hasWeight = hasWeight || (HexagonMCInstrInfo::isHMXWeight(MCII, I));
712 hasActivation =
713 hasActivation || (HexagonMCInstrInfo::isHMXActivation(MCII, I));
714 }
715
716 // To be valid they must both be present or neither:
717 const bool ValidWeightActPairing = hasWeight == hasActivation;
718 if (!ValidWeightActPairing) {
719 const std::string ErrMsg = hasWeight ? "weight without an activation"
720 : "activation without a weight";
721 reportError(MCB.getLoc(), Twine("packet has HMX ") + ErrMsg);
722
724 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
726 reportNote(I.getLoc(), "this is an HMX weight instruction");
728 reportNote(I.getLoc(), "this is an HMX activation instruction");
729 else
730 reportNote(I.getLoc(),
731 "this is neither an HMX weight nor activation");
732 }
733 }
734 return ValidWeightActPairing;
735}
736
737// Check for legal use of solo insns.
738bool HexagonMCChecker::checkSolo() {
740 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
741 if (HexagonMCInstrInfo::isSolo(MCII, I)) {
742 reportError(I.getLoc(), "Instruction is marked `isSolo' and "
743 "cannot have other instructions in "
744 "the same packet");
745 return false;
746 }
747 }
748
749 return true;
750}
751
752bool HexagonMCChecker::checkShuffle() {
753 HexagonMCShuffler MCSDX(Context, ReportErrors, MCII, STI, MCB);
754 return MCSDX.check();
755}
756
757bool HexagonMCChecker::checkValidTmpDst() {
758 if (!STI.hasFeature(Hexagon::ArchV69)) {
759 return true;
760 }
761 auto HasTmp = [&](MCInst const &I) {
762 return HexagonMCInstrInfo::hasTmpDst(MCII, I) ||
765 };
766 unsigned HasTmpCount =
768
769 if (HasTmpCount > 1) {
771 MCB.getLoc(),
772 "this packet has more than one HVX vtmp/.tmp destination instruction");
773
774 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB))
775 if (HasTmp(I))
776 reportNote(I.getLoc(),
777 "this is an HVX vtmp/.tmp destination instruction");
778
779 return false;
780 }
781 return true;
782}
783
784void HexagonMCChecker::compoundRegisterMap(unsigned &Register) {
785 switch (Register) {
786 default:
787 break;
788 case Hexagon::R15:
789 Register = Hexagon::R23;
790 break;
791 case Hexagon::R14:
792 Register = Hexagon::R22;
793 break;
794 case Hexagon::R13:
795 Register = Hexagon::R21;
796 break;
797 case Hexagon::R12:
798 Register = Hexagon::R20;
799 break;
800 case Hexagon::R11:
801 Register = Hexagon::R19;
802 break;
803 case Hexagon::R10:
804 Register = Hexagon::R18;
805 break;
806 case Hexagon::R9:
807 Register = Hexagon::R17;
808 break;
809 case Hexagon::R8:
810 Register = Hexagon::R16;
811 break;
812 }
813}
814
816 reportError("register `" + Twine(RI.getName(Register)) +
817 "' modified more than once");
818}
819
821 reportError("register `" + Twine(RI.getName(Register)) +
822 "' used with `.new' "
823 "but not validly modified in the same packet");
824}
825
827 reportError(MCB.getLoc(), Msg);
828}
829
831 if (ReportErrors)
832 Context.reportError(Loc, Msg);
833}
834
836 if (ReportErrors) {
837 auto SM = Context.getSourceManager();
838 if (SM)
839 SM->PrintMessage(Loc, SourceMgr::DK_Note, Msg);
840 }
841}
842
844 if (ReportErrors)
845 Context.reportWarning(MCB.getLoc(), Msg);
846}
847
848bool HexagonMCChecker::checkLegalVecRegPair() {
849 const bool IsPermitted = STI.hasFeature(Hexagon::ArchV67);
850 const bool HasReversePairs = ReversePairs.size() != 0;
851
852 if (!IsPermitted && HasReversePairs) {
853 for (auto R : ReversePairs)
854 reportError("register pair `" + Twine(RI.getName(R)) +
855 "' is not permitted for this architecture");
856 return false;
857 }
858 return true;
859}
860
861bool HexagonMCChecker::checkHVXRestrictions() {
862 const MCInst *VS3SRC = nullptr;
863 bool PacketHasNonUnaryALU = false;
864 bool PacketHasVP = false;
865 bool PacketHasAnotherVS = false;
866 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
867 if (!VS3SRC && HexagonMCInstrInfo::isHVXVS3SRC(MCII, I)) {
868 VS3SRC = &I;
869 continue; // Do not count itself as the other VS.
870 }
872 PacketHasNonUnaryALU = true;
874 PacketHasVP = true;
875 // V79 allows a shift to execute on the permute resource. If this happens
876 // the same 3SRC restriction must be checked against that shift.
878 PacketHasAnotherVS = true;
879 }
880 if (!VS3SRC)
881 return true;
882 bool ret = !PacketHasNonUnaryALU && !PacketHasVP && !PacketHasAnotherVS;
883 if (!ret) {
884 reportError("invalid instruction packet: HVX resource use violation");
885 if (PacketHasNonUnaryALU)
886 reportNote(VS3SRC->getLoc(),
887 "This instruction cannot be paired with an HVX "
888 "non-unary instruction");
889 if (PacketHasVP)
890 reportNote(VS3SRC->getLoc(),
891 "This instruction cannot be paired with an HVX "
892 "permute instruction");
893 if (PacketHasAnotherVS)
894 reportNote(VS3SRC->getLoc(),
895 "This instruction cannot be paired with another HVX "
896 "shift instruction");
897 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
899 reportNote(I.getLoc(), "HVX non-unary instruction");
901 reportNote(I.getLoc(), "HVX permute instruction");
903 &I != VS3SRC)
904 reportNote(I.getLoc(), "another HVX shift instruction");
905 }
906 }
907 return ret;
908}
909
910// Vd.tmp can't be accumulated
911bool HexagonMCChecker::checkHVXAccum()
912{
913 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
914 bool IsTarget =
915 HexagonMCInstrInfo::isAccumulator(MCII, I) && I.getOperand(0).isReg();
916 if (!IsTarget)
917 continue;
918 MCRegister R = I.getOperand(0).getReg();
919 TmpDefsIterator It = TmpDefs.find(R);
920 if (It != TmpDefs.end()) {
921 reportError("register `" + Twine(RI.getName(R)) + ".tmp" +
922 "' is accumulated in this packet");
923 return false;
924 }
925 }
926 return true;
927}
928
929bool HexagonMCChecker::checkHLX64BitRegUse() {
930 const MCRegisterClass &DoubleRegs =
931 RI.getRegClass(Hexagon::DoubleRegsRegClassID);
932 const MCRegisterClass &IntRegs = RI.getRegClass(Hexagon::IntRegsRegClassID);
933 const MCInst *DoubleInst = nullptr;
934 unsigned NumScalarArg = 0;
935 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
936 if (!HexagonMCInstrInfo::isHLX(MCII, I))
937 continue;
938 const MCInstrDesc &Desc = HexagonMCInstrInfo::getDesc(MCII, I);
939 if (Desc.mayLoad() || Desc.mayStore())
940 continue;
941 bool UsesScalarArg = false;
942 for (const auto &Op : I) {
943 if (!DoubleInst && Op.isReg() && DoubleRegs.contains(Op.getReg()))
944 DoubleInst = &I;
945 if (Op.isImm() || Op.isExpr() ||
946 (Op.isReg() &&
947 (IntRegs.contains(Op.getReg()) || DoubleRegs.contains(Op.getReg()))))
948 UsesScalarArg = true;
949 }
950 if (UsesScalarArg)
951 ++NumScalarArg;
952 }
953
954 if (DoubleInst && NumScalarArg > 1) {
955 reportError(DoubleInst->getLoc(),
956 "HLX instruction using a 64-bit scalar register cannot be "
957 "in the same packet with another HLX instruction using a "
958 "scalar register or immediate");
959 return false;
960 }
961 return true;
962}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< bool > RelaxNVChecks("relax-nv-checks", cl::Hidden, cl::desc("Relax checks of new-value validity"))
static bool isDuplexAGroup(unsigned Opcode)
static bool isNeitherAnorX(MCInstrInfo const &MCII, MCInst const &ID)
#define I(x, y, z)
Definition MD5.cpp:57
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define P(N)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
const char * Msg
static const MCPhysReg IntRegs[32]
void reportErrorNewValue(MCRegister Register)
HexagonMCChecker(MCContext &Context, MCInstrInfo const &MCII, MCSubtargetInfo const &STI, MCInst &mcb, const MCRegisterInfo &ri, bool ReportErrors=true)
bool check(bool FullCheck=true)
void reportWarning(Twine const &Msg)
void reportNote(SMLoc Loc, Twine const &Msg)
void reportErrorRegisters(MCRegister Register)
void reportError(SMLoc Loc, Twine const &Msg)
Context object for machine code objects.
Definition MCContext.h:84
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
SMLoc getLoc() const
Definition MCInst.h:208
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:210
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
bool isBranch() const
Returns true if this is a conditional, unconditional, or indirect branch.
ArrayRef< MCPhysReg > implicit_defs() const
Return a list of registers that are potentially written by any instance of this machine instruction.
bool isCall() const
Return true if the instruction is a call.
ArrayRef< MCPhysReg > implicit_uses() const
Return a list of registers that are potentially read by any instance of this machine instruction.
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
bool isReg() const
Definition MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
const MCInst * getInst() const
Definition MCInst.h:128
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
const char * getName(MCRegister RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Represents a location in source code.
Definition SMLoc.h:22
void push_back(const T &Elt)
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isTmpWrite(MCInstrInfo const &MCII, MCInst const &MCI)
bool isHMXWeight(MCInstrInfo const &MCII, MCInst const &MCI)
bool isHVXVS3SRC(MCInstrInfo const &MCII, MCInst const &MCI)
bool isSolo(MCInstrInfo const &MCII, MCInst const &MCI)
Return whether the insn is solo, i.e., cannot be in a packet.
bool isPredicatedNew(MCInstrInfo const &MCII, MCInst const &MCI)
Return whether the insn is newly predicated.
bool IsReverseVecRegPair(MCRegister VecReg)
bool isOuterLoop(MCInst const &MCI)
bool isHVXALU2SRC(MCInstrInfo const &MCII, MCInst const &MCI)
unsigned getAddrMode(MCInstrInfo const &MCII, MCInst const &MCI)
size_t bundleSize(MCInst const &MCI)
bool IsABranchingInst(MCInstrInfo const &MCII, MCSubtargetInfo const &STI, MCInst const &I)
bool isDuplex(MCInstrInfo const &MCII, MCInst const &MCI)
bool isPredicateLate(MCInstrInfo const &MCII, MCInst const &MCI)
bool isSoloAX(MCInstrInfo const &MCII, MCInst const &MCI)
Return whether the insn can be packaged only with A and X-type insns.
bool isNewValue(MCInstrInfo const &MCII, MCInst const &MCI)
Return whether the insn expects newly produced value.
bool isHVX(MCInstrInfo const &MCII, MCInst const &MCI)
bool isHLX(MCInstrInfo const &MCII, MCInst const &MCI)
MCInstrDesc const & getDesc(MCInstrInfo const &MCII, MCInst const &MCI)
iterator_range< Hexagon::PacketIterator > bundleInstructions(MCInstrInfo const &MCII, MCInst const &MCI)
bool isBundle(MCInst const &MCI)
bool isCofRelax1(MCInstrInfo const &MCII, MCInst const &MCI)
unsigned getType(MCInstrInfo const &MCII, MCInst const &MCI)
Return the Hexagon ISA class for the insn.
MCOperand const & getNewValueOperand(MCInstrInfo const &MCII, MCInst const &MCI)
bool isPredicated(MCInstrInfo const &MCII, MCInst const &MCI)
bool isCofMax1(MCInstrInfo const &MCII, MCInst const &MCI)
bool isInnerLoop(MCInst const &MCI)
PredicateInfo predicateInfo(MCInstrInfo const &MCII, MCInst const &MCI)
unsigned slotsConsumed(MCInstrInfo const &MCII, MCSubtargetInfo const &STI, MCInst const &MCI)
bool hasTmpDst(MCInstrInfo const &MCII, MCInst const &MCI)
unsigned packetSizeSlots(MCSubtargetInfo const &STI)
bool isPredReg(MCRegisterInfo const &MRI, MCRegister Reg)
bool isPredicatedTrue(MCInstrInfo const &MCII, MCInst const &MCI)
bool isAccumulator(MCInstrInfo const &MCII, MCInst const &MCI)
Return where the instruction is an accumulator.
bool isHMXActivation(MCInstrInfo const &MCII, MCInst const &MCI)
bool isCVINew(MCInstrInfo const &MCII, MCInst const &MCI)
bool isFloat(MCInstrInfo const &MCII, MCInst const &MCI)
Return whether it is a floating-point insn.
bool isCofRelax2(MCInstrInfo const &MCII, MCInst const &MCI)
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
Op::Description Desc
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Other
Any other memory.
Definition ModRef.h:68
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:2035
#define N