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
44 // Figure out the loop-registers definitions.
46 Defs[Hexagon::SA0].insert(Unconditional); // FIXME: define or change SA0?
47 Defs[Hexagon::LC0].insert(Unconditional);
48 }
50 Defs[Hexagon::SA1].insert(Unconditional); // FIXME: define or change SA0?
51 Defs[Hexagon::LC1].insert(Unconditional);
52 }
53
55 // Unfurl a bundle.
56 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
57 MCInst const &Inst = *I.getInst();
58 if (HexagonMCInstrInfo::isDuplex(MCII, Inst)) {
59 init(*Inst.getOperand(0).getInst());
60 init(*Inst.getOperand(1).getInst());
61 } else
62 init(Inst);
63 }
64 else
65 init(MCB);
66}
67
68void HexagonMCChecker::initReg(MCInst const &MCI, MCRegister R,
69 MCRegister &PredReg, bool &isTrue) {
70 if (HexagonMCInstrInfo::isPredicated(MCII, MCI) &&
72 // Note an used predicate register.
73 PredReg = R;
74 isTrue = HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI);
75
76 // Note use of new predicate register.
78 NewPreds.insert(PredReg);
79 } else
80 // Note register use. Super-registers are not tracked directly,
81 // but their components.
82 for (MCRegAliasIterator SRI(R, &RI, RI.subregs(R).empty()); SRI.isValid();
83 ++SRI)
84 if (RI.subregs(*SRI).empty())
85 // Skip super-registers used indirectly.
86 Uses.insert(*SRI);
87
89 ReversePairs.insert(R);
90}
91
92void HexagonMCChecker::init(MCInst const &MCI) {
93 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MCI);
94 MCRegister PredReg;
95 bool isTrue = false;
96
97 // Get used registers.
98 for (unsigned i = MCID.getNumDefs(); i < MCID.getNumOperands(); ++i)
99 if (MCI.getOperand(i).isReg())
100 initReg(MCI, MCI.getOperand(i).getReg(), PredReg, isTrue);
101 for (MCPhysReg ImpUse : MCID.implicit_uses())
102 initReg(MCI, ImpUse, PredReg, isTrue);
103
104 const bool IgnoreTmpDst = (HexagonMCInstrInfo::hasTmpDst(MCII, MCI) ||
105 HexagonMCInstrInfo::isTmpWrite(MCII, MCI)) &&
106 STI.hasFeature(Hexagon::ArchV69);
107
108 // Get implicit register definitions.
109 for (MCPhysReg R : MCID.implicit_defs()) {
110 if (Hexagon::R31 != R && MCID.isCall())
111 // Any register other than the LR and the PC are actually volatile ones
112 // as defined by the ABI, not modified implicitly by the call insn.
113 continue;
114 if (Hexagon::PC == R)
115 // Branches are the only insns that can change the PC,
116 // otherwise a read-only register.
117 continue;
118
119 if (Hexagon::USR_OVF == R)
120 // Many insns change the USR implicitly, but only one or another flag.
121 // The instruction table models the USR.OVF flag, which can be
122 // implicitly modified more than once, but cannot be modified in the
123 // same packet with an instruction that modifies is explicitly. Deal
124 // with such situations individually.
125 SoftDefs.insert(R);
126 else if (HexagonMCInstrInfo::isPredReg(RI, R) &&
128 // Include implicit late predicates.
129 LatePreds.insert(R);
130 else if (!IgnoreTmpDst)
131 Defs[R].insert(PredSense(PredReg, isTrue));
132 }
133
134 // Figure out explicit register definitions.
135 for (unsigned i = 0; i < MCID.getNumDefs(); ++i) {
136 MCRegister R = MCI.getOperand(i).getReg(), S = MCRegister();
137 // USR has subregisters (while C8 does not for technical reasons), so
138 // reset R to USR, since we know how to handle multiple defs of USR,
139 // taking into account its subregisters.
140 if (R == Hexagon::C8)
141 R = Hexagon::USR;
142
144 ReversePairs.insert(R);
145
146 // Note register definitions, direct ones as well as indirect side-effects.
147 // Super-registers are not tracked directly, but their components.
148 for (MCRegAliasIterator SRI(R, &RI, RI.subregs(R).empty()); SRI.isValid();
149 ++SRI) {
150 if (!RI.subregs(*SRI).empty())
151 // Skip super-registers defined indirectly.
152 continue;
153
154 if (R == *SRI) {
155 if (S == R)
156 // Avoid scoring the defined register multiple times.
157 continue;
158 else
159 // Note that the defined register has already been scored.
160 S = R;
161 }
162
163 if (Hexagon::P3_0 != R && Hexagon::P3_0 == *SRI)
164 // P3:0 is a special case, since multiple predicate register definitions
165 // in a packet is allowed as the equivalent of their logical "and".
166 // Only an explicit definition of P3:0 is noted as such; if a
167 // side-effect, then note as a soft definition.
168 SoftDefs.insert(*SRI);
169 else if (HexagonMCInstrInfo::isPredicateLate(MCII, MCI) &&
171 // Some insns produce predicates too late to be used in the same packet.
172 LatePreds.insert(*SRI);
173 else if (i == 0 && HexagonMCInstrInfo::getType(MCII, MCI) ==
175 // Temporary loads should be used in the same packet, but don't commit
176 // results, so it should be disregarded if another insn changes the same
177 // register.
178 // TODO: relies on the impossibility of a current and a temporary loads
179 // in the same packet.
180 TmpDefs.insert(*SRI);
181 else if (!IgnoreTmpDst)
182 Defs[*SRI].insert(PredSense(PredReg, isTrue));
183 }
184 }
185
186 // Figure out definitions of new predicate registers.
188 for (unsigned i = MCID.getNumDefs(); i < MCID.getNumOperands(); ++i)
189 if (MCI.getOperand(i).isReg()) {
190 MCRegister P = MCI.getOperand(i).getReg();
191
193 NewPreds.insert(P);
194 }
195}
196
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) {
202 init();
203}
204
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) {
210 init();
211}
212
213bool HexagonMCChecker::check(bool FullCheck) {
214 bool chkP = checkPredicates();
215 bool chkNV = checkNewValues();
216 bool chkR = checkRegisters();
217 bool chkRRO = checkRegistersReadOnly();
218 checkRegisterCurDefs();
219 bool chkS = checkSolo();
220 bool chkSh = true;
221 if (FullCheck)
222 chkSh = checkShuffle();
223 bool chkSl = true;
224 if (FullCheck)
225 chkSl = checkSlots();
226 bool chkAXOK = checkAXOK();
227 bool chkCofMax1 = checkCOFMax1();
228 bool chkHWLoop = checkHWLoop();
229 bool chkHMX = checkValidHMX();
230 bool chkValidTmpDst = FullCheck ? checkValidTmpDst() : true;
231 bool chkLegalVecRegPair = checkLegalVecRegPair();
232 bool ChkHVXAccum = checkHVXAccum();
233 bool ChkHVXRestrictions = checkHVXRestrictions();
234 bool ChkHLX64BitRegUse = checkHLX64BitRegUse();
235 bool chk = chkP && chkNV && chkR && chkRRO && chkS && chkSh && chkSl &&
236 chkAXOK && chkCofMax1 && chkHWLoop && chkHMX && chkValidTmpDst &&
237 chkLegalVecRegPair && ChkHVXAccum && ChkHVXRestrictions &&
238 ChkHLX64BitRegUse;
239
240 return chk;
241}
242
243static bool isDuplexAGroup(unsigned Opcode) {
244 switch (Opcode) {
245 case Hexagon::SA1_addi:
246 case Hexagon::SA1_addrx:
247 case Hexagon::SA1_addsp:
248 case Hexagon::SA1_and1:
249 case Hexagon::SA1_clrf:
250 case Hexagon::SA1_clrfnew:
251 case Hexagon::SA1_clrt:
252 case Hexagon::SA1_clrtnew:
253 case Hexagon::SA1_cmpeqi:
254 case Hexagon::SA1_combine0i:
255 case Hexagon::SA1_combine1i:
256 case Hexagon::SA1_combine2i:
257 case Hexagon::SA1_combine3i:
258 case Hexagon::SA1_combinerz:
259 case Hexagon::SA1_combinezr:
260 case Hexagon::SA1_dec:
261 case Hexagon::SA1_inc:
262 case Hexagon::SA1_seti:
263 case Hexagon::SA1_setin1:
264 case Hexagon::SA1_sxtb:
265 case Hexagon::SA1_sxth:
266 case Hexagon::SA1_tfr:
267 case Hexagon::SA1_zxtb:
268 case Hexagon::SA1_zxth:
269 return true;
270 break;
271 default:
272 return false;
273 }
274}
275
276static bool isNeitherAnorX(MCInstrInfo const &MCII, MCInst const &ID) {
277 if (HexagonMCInstrInfo::isFloat(MCII, ID))
278 return true;
279 unsigned Type = HexagonMCInstrInfo::getType(MCII, ID);
280 switch (Type) {
287 case HexagonII::TypeM:
289 return false;
291 return !isDuplexAGroup(ID.getOpcode());
292 }
294 llvm_unreachable("unexpected duplex instruction");
295 default:
296 return true;
297 }
298}
299
300bool HexagonMCChecker::checkAXOK() {
301 MCInst const *HasSoloAXInst = nullptr;
302 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
303 if (HexagonMCInstrInfo::isSoloAX(MCII, I)) {
304 HasSoloAXInst = &I;
305 }
306 }
307 if (!HasSoloAXInst)
308 return true;
309 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
310 if (&I != HasSoloAXInst && isNeitherAnorX(MCII, I)) {
312 HasSoloAXInst->getLoc(),
313 Twine("Instruction can only be in a packet with ALU or non-FPU XTYPE "
314 "instructions"));
315 reportError(I.getLoc(),
316 Twine("Not an ALU or non-FPU XTYPE instruction"));
317 return false;
318 }
319 }
320 return true;
321}
322
324 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
326 reportNote(I.getLoc(), "Branching instruction");
327 }
328}
329
330bool HexagonMCChecker::checkHWLoop() {
333 return true;
334 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
335 if (HexagonMCInstrInfo::IsABranchingInst(MCII, STI, I)) {
336 reportError(MCB.getLoc(),
337 "Branches cannot be in a packet with hardware loops");
339 return false;
340 }
341 }
342 return true;
343}
344
345bool HexagonMCChecker::checkCOFMax1() {
346 SmallVector<MCInst const *, 2> BranchLocations;
347 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
349 BranchLocations.push_back(&I);
350 }
351 for (unsigned J = 0, N = BranchLocations.size(); J < N; ++J) {
352 MCInst const &I = *BranchLocations[J];
353 if (HexagonMCInstrInfo::isCofMax1(MCII, I)) {
354 bool Relax1 = HexagonMCInstrInfo::isCofRelax1(MCII, I);
355 bool Relax2 = HexagonMCInstrInfo::isCofRelax2(MCII, I);
356 if (N > 1 && !Relax1 && !Relax2) {
357 reportError(I.getLoc(),
358 "Instruction may not be in a packet with other branches");
360 return false;
361 }
362 if (N > 1 && J == 0 && !Relax1) {
363 reportError(I.getLoc(),
364 "Instruction may not be the first branch in packet");
366 return false;
367 }
368 if (N > 1 && J == 1 && !Relax2) {
369 reportError(I.getLoc(),
370 "Instruction may not be the second branch in packet");
372 return false;
373 }
374 }
375 }
376 return true;
377}
378
379bool HexagonMCChecker::checkSlots() {
380 if (HexagonMCInstrInfo::slotsConsumed(MCII, STI, MCB) >
382 reportError("invalid instruction packet: out of slots");
383 return false;
384 }
385 return true;
386}
387
388// Check legal use of predicate registers.
389bool HexagonMCChecker::checkPredicates() {
390 // Check for proper use of new predicate registers.
391 for (const auto &I : NewPreds) {
392 MCRegister P = I;
393
394 if (!Defs.count(P) || LatePreds.count(P) || Defs.count(Hexagon::P3_0)) {
395 // Error out if the new predicate register is not defined,
396 // or defined "late"
397 // (e.g., "{ if (p3.new)... ; p3 = sp1loop0(#r7:2, Rs) }").
399 return false;
400 }
401 }
402
403 // Check for proper use of auto-anded of predicate registers.
404 for (const auto &I : LatePreds) {
405 MCRegister P = I;
406
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(...) }".
412 return false;
413 }
414 }
415
416 return true;
417}
418
419// Check legal use of new values.
420bool HexagonMCChecker::checkNewValues() {
421 for (auto const &ConsumerInst :
423 if (!HexagonMCInstrInfo::isNewValue(MCII, ConsumerInst))
424 continue;
425
426 const HexagonMCInstrInfo::PredicateInfo ConsumerPredInfo =
427 HexagonMCInstrInfo::predicateInfo(MCII, ConsumerInst);
428
429 bool Branch = HexagonMCInstrInfo::getDesc(MCII, ConsumerInst).isBranch();
430 MCOperand const &Op =
431 HexagonMCInstrInfo::getNewValueOperand(MCII, ConsumerInst);
432 assert(Op.isReg());
433
434 auto Producer = registerProducer(Op.getReg(), ConsumerPredInfo);
435 const MCInst *const ProducerInst = std::get<0>(Producer);
436 const HexagonMCInstrInfo::PredicateInfo ProducerPredInfo =
437 std::get<2>(Producer);
438
439 if (ProducerInst == nullptr) {
440 reportError(ConsumerInst.getLoc(),
441 "New value register consumer has no producer");
442 return false;
443 }
444 if (!RelaxNVChecks) {
445 // Checks that statically prove correct new value consumption
446 if (ProducerPredInfo.isPredicated() &&
447 (!ConsumerPredInfo.isPredicated() ||
448 llvm::HexagonMCInstrInfo::getType(MCII, ConsumerInst) ==
451 ProducerInst->getLoc(),
452 "Register producer is predicated and consumer is unconditional");
453 reportError(ConsumerInst.getLoc(),
454 "Instruction does not have a valid new register producer");
455 return false;
456 }
457 if (ProducerPredInfo.Register != Hexagon::NoRegister &&
458 ProducerPredInfo.Register != ConsumerPredInfo.Register) {
459 reportNote(ProducerInst->getLoc(),
460 "Register producer does not use the same predicate "
461 "register as the consumer");
462 reportError(ConsumerInst.getLoc(),
463 "Instruction does not have a valid new register producer");
464 return false;
465 }
466 }
467 if (ProducerPredInfo.Register == ConsumerPredInfo.Register &&
468 ConsumerPredInfo.PredicatedTrue != ProducerPredInfo.PredicatedTrue) {
470 ProducerInst->getLoc(),
471 "Register producer has the opposite predicate sense as consumer");
472 reportError(ConsumerInst.getLoc(),
473 "Instruction does not have a valid new register producer");
474 return false;
475 }
476
477 MCInstrDesc const &Desc = HexagonMCInstrInfo::getDesc(MCII, *ProducerInst);
478 const unsigned ProducerOpIndex = std::get<1>(Producer);
479
480 if (Desc.operands()[ProducerOpIndex].RegClass ==
481 Hexagon::DoubleRegsRegClassID) {
482 reportNote(ProducerInst->getLoc(),
483 "Double registers cannot be new-value producers");
484 reportError(ConsumerInst.getLoc(),
485 "Instruction does not have a valid new register producer");
486 return false;
487 }
488
489 // The ProducerOpIsMemIndex logic checks for the index of the producer
490 // register operand. Z-reg load instructions have an implicit operand
491 // that's not encoded, so the producer won't appear as the 1-th def, it
492 // will be at the 0-th.
493 const unsigned ProducerOpSearchIndex =
494 (HexagonMCInstrInfo::getType(MCII, *ProducerInst) ==
496 ? 0
497 : 1;
498
499 const bool ProducerOpIsMemIndex =
500 ((Desc.mayLoad() && ProducerOpIndex == ProducerOpSearchIndex) ||
501 (Desc.mayStore() && ProducerOpIndex == 0));
502
503 if (ProducerOpIsMemIndex) {
504 unsigned Mode = HexagonMCInstrInfo::getAddrMode(MCII, *ProducerInst);
505
506 StringRef ModeError;
508 ModeError = "Absolute-set";
510 ModeError = "Auto-increment";
511 if (!ModeError.empty()) {
512 reportNote(ProducerInst->getLoc(),
513 ModeError + " registers cannot be a new-value "
514 "producer");
515 reportError(ConsumerInst.getLoc(),
516 "Instruction does not have a valid new register producer");
517 return false;
518 }
519 }
520 if (Branch && HexagonMCInstrInfo::isFloat(MCII, *ProducerInst)) {
521 reportNote(ProducerInst->getLoc(),
522 "FPU instructions cannot be new-value producers for jumps");
523 reportError(ConsumerInst.getLoc(),
524 "Instruction does not have a valid new register producer");
525 return false;
526 }
527 }
528 return true;
529}
530
531bool HexagonMCChecker::checkRegistersReadOnly() {
532 for (auto I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
533 MCInst const &Inst = *I.getInst();
534 unsigned Defs = HexagonMCInstrInfo::getDesc(MCII, Inst).getNumDefs();
535 for (unsigned j = 0; j < Defs; ++j) {
536 MCOperand const &Operand = Inst.getOperand(j);
537 assert(Operand.isReg() && "Def is not a register");
538 MCRegister Register = Operand.getReg();
539 if (ReadOnly.find(Register) != ReadOnly.end()) {
540 reportError(Inst.getLoc(), "Cannot write to read-only register `" +
541 Twine(RI.getName(Register)) + "'");
542 return false;
543 }
544 }
545 }
546 return true;
547}
548
549bool HexagonMCChecker::registerUsed(MCRegister Register) {
550 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB))
551 for (unsigned j = HexagonMCInstrInfo::getDesc(MCII, I).getNumDefs(),
552 n = I.getNumOperands();
553 j < n; ++j) {
554 MCOperand const &Operand = I.getOperand(j);
555 if (Operand.isReg() && Operand.getReg() == Register)
556 return true;
557 }
558 return false;
559}
560
561std::tuple<MCInst const *, unsigned, HexagonMCInstrInfo::PredicateInfo>
562HexagonMCChecker::registerProducer(
564 std::tuple<MCInst const *, unsigned, HexagonMCInstrInfo::PredicateInfo>
565 WrongSense;
566
567 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
568 MCInstrDesc const &Desc = HexagonMCInstrInfo::getDesc(MCII, I);
569 auto ProducerPredicate = HexagonMCInstrInfo::predicateInfo(MCII, I);
570
571 for (unsigned J = 0, N = Desc.getNumDefs(); J < N; ++J)
572 for (auto K = MCRegAliasIterator(I.getOperand(J).getReg(), &RI, true);
573 K.isValid(); ++K)
574 if (*K == Register) {
575 if (RelaxNVChecks ||
576 (ProducerPredicate.Register == ConsumerPredicate.Register &&
577 (ProducerPredicate.Register == Hexagon::NoRegister ||
578 ProducerPredicate.PredicatedTrue ==
579 ConsumerPredicate.PredicatedTrue)))
580 return std::make_tuple(&I, J, ProducerPredicate);
581 std::get<0>(WrongSense) = &I;
582 std::get<1>(WrongSense) = J;
583 std::get<2>(WrongSense) = ProducerPredicate;
584 }
585 if (Register == Hexagon::VTMP && HexagonMCInstrInfo::hasTmpDst(MCII, I))
586 return std::make_tuple(&I, 0, HexagonMCInstrInfo::PredicateInfo());
587 }
588 return WrongSense;
589}
590
591void HexagonMCChecker::checkRegisterCurDefs() {
592 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
593 if (HexagonMCInstrInfo::isCVINew(MCII, I) &&
594 HexagonMCInstrInfo::getDesc(MCII, I).mayLoad()) {
595 const MCRegister RegDef = I.getOperand(0).getReg();
596
597 bool HasRegDefUse = false;
598 for (MCRegAliasIterator Alias(RegDef, &RI, true); Alias.isValid();
599 ++Alias)
600 HasRegDefUse = HasRegDefUse || registerUsed(*Alias);
601
602 if (!HasRegDefUse)
603 reportWarning("Register `" + Twine(RI.getName(RegDef)) +
604 "' used with `.cur' "
605 "but not used in the same packet");
606 }
607 }
608}
609
610// Check for legal register uses and definitions.
611bool HexagonMCChecker::checkRegisters() {
612 // Check for proper register definitions.
613 for (const auto &I : Defs) {
614 MCRegister R = I.first;
615
616 if (isLoopRegister(R) && Defs.count(R) > 1 &&
619 // Error out for definitions of loop registers at the end of a loop.
620 reportError("loop-setup and some branch instructions "
621 "cannot be in the same packet");
622 return false;
623 }
624 if (SoftDefs.count(R)) {
625 // Error out for explicit changes to registers also weakly defined
626 // (e.g., "{ usr = r0; r0 = sfadd(...) }").
627 MCRegister UsrR = Hexagon::USR;
628 MCRegister BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R;
630 return false;
631 }
632 if (!HexagonMCInstrInfo::isPredReg(RI, R) && Defs[R].size() > 1) {
633 // Check for multiple register definitions.
634 PredSet &PM = Defs[R];
635
636 // Check for multiple unconditional register definitions.
637 if (PM.count(Unconditional)) {
638 // Error out on an unconditional change when there are any other
639 // changes, conditional or not.
640 MCRegister UsrR = Hexagon::USR;
641 MCRegister BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R;
643 return false;
644 }
645 // Check for multiple conditional register definitions.
646 for (const auto &J : PM) {
647 PredSense P = J;
648
649 // Check for multiple uses of the same condition.
650 if (PM.count(P) > 1) {
651 // Error out on conditional changes based on the same predicate
652 // (e.g., "{ if (!p0) r0 =...; if (!p0) r0 =... }").
654 return false;
655 }
656 // Check for the use of the complementary condition.
657 P.second = !P.second;
658 if (PM.count(P) && PM.size() > 2) {
659 // Error out on conditional changes based on the same predicate
660 // multiple times
661 // (e.g., "if (p0) r0 =...; if (!p0) r0 =... }; if (!p0) r0 =...").
663 return false;
664 }
665 }
666 }
667 }
668
669 // Check for use of temporary definitions.
670 for (const auto &I : TmpDefs) {
671 MCRegister R = I;
672
673 if (!Uses.count(R)) {
674 // special case for vhist
675 bool vHistFound = false;
676 for (auto const &HMI : HexagonMCInstrInfo::bundleInstructions(MCB)) {
677 if (HexagonMCInstrInfo::getType(MCII, *HMI.getInst()) ==
679 vHistFound = true; // vhist() implicitly uses ALL REGxx.tmp
680 break;
681 }
682 }
683 // Warn on an unused temporary definition.
684 if (!vHistFound) {
685 reportWarning("register `" + Twine(RI.getName(R)) +
686 "' used with `.tmp' but not used in the same packet");
687 return true;
688 }
689 }
690 }
691
692 return true;
693}
694
695bool HexagonMCChecker::checkValidHMX() {
696 bool hasWeight = false;
697 bool hasActivation = false;
698 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
699 hasWeight = hasWeight || (HexagonMCInstrInfo::isHMXWeight(MCII, I));
700 hasActivation =
701 hasActivation || (HexagonMCInstrInfo::isHMXActivation(MCII, I));
702 }
703
704 // To be valid they must both be present or neither:
705 const bool ValidWeightActPairing = hasWeight == hasActivation;
706 if (!ValidWeightActPairing) {
707 const std::string ErrMsg = hasWeight ? "weight without an activation"
708 : "activation without a weight";
709 reportError(MCB.getLoc(), Twine("packet has HMX ") + ErrMsg);
710
712 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
714 reportNote(I.getLoc(), "this is an HMX weight instruction");
716 reportNote(I.getLoc(), "this is an HMX activation instruction");
717 else
718 reportNote(I.getLoc(),
719 "this is neither an HMX weight nor activation");
720 }
721 }
722 return ValidWeightActPairing;
723}
724
725// Check for legal use of solo insns.
726bool HexagonMCChecker::checkSolo() {
728 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
729 if (HexagonMCInstrInfo::isSolo(MCII, I)) {
730 reportError(I.getLoc(), "Instruction is marked `isSolo' and "
731 "cannot have other instructions in "
732 "the same packet");
733 return false;
734 }
735 }
736
737 return true;
738}
739
740bool HexagonMCChecker::checkShuffle() {
741 HexagonMCShuffler MCSDX(Context, ReportErrors, MCII, STI, MCB);
742 return MCSDX.check();
743}
744
745bool HexagonMCChecker::checkValidTmpDst() {
746 if (!STI.hasFeature(Hexagon::ArchV69)) {
747 return true;
748 }
749 auto HasTmp = [&](MCInst const &I) {
750 return HexagonMCInstrInfo::hasTmpDst(MCII, I) ||
753 };
754 unsigned HasTmpCount =
756
757 if (HasTmpCount > 1) {
759 MCB.getLoc(),
760 "this packet has more than one HVX vtmp/.tmp destination instruction");
761
762 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB))
763 if (HasTmp(I))
764 reportNote(I.getLoc(),
765 "this is an HVX vtmp/.tmp destination instruction");
766
767 return false;
768 }
769 return true;
770}
771
772void HexagonMCChecker::compoundRegisterMap(unsigned &Register) {
773 switch (Register) {
774 default:
775 break;
776 case Hexagon::R15:
777 Register = Hexagon::R23;
778 break;
779 case Hexagon::R14:
780 Register = Hexagon::R22;
781 break;
782 case Hexagon::R13:
783 Register = Hexagon::R21;
784 break;
785 case Hexagon::R12:
786 Register = Hexagon::R20;
787 break;
788 case Hexagon::R11:
789 Register = Hexagon::R19;
790 break;
791 case Hexagon::R10:
792 Register = Hexagon::R18;
793 break;
794 case Hexagon::R9:
795 Register = Hexagon::R17;
796 break;
797 case Hexagon::R8:
798 Register = Hexagon::R16;
799 break;
800 }
801}
802
804 reportError("register `" + Twine(RI.getName(Register)) +
805 "' modified more than once");
806}
807
809 reportError("register `" + Twine(RI.getName(Register)) +
810 "' used with `.new' "
811 "but not validly modified in the same packet");
812}
813
815 reportError(MCB.getLoc(), Msg);
816}
817
819 if (ReportErrors)
820 Context.reportError(Loc, Msg);
821}
822
824 if (ReportErrors) {
825 auto SM = Context.getSourceManager();
826 if (SM)
827 SM->PrintMessage(Loc, SourceMgr::DK_Note, Msg);
828 }
829}
830
832 if (ReportErrors)
833 Context.reportWarning(MCB.getLoc(), Msg);
834}
835
836bool HexagonMCChecker::checkLegalVecRegPair() {
837 const bool IsPermitted = STI.hasFeature(Hexagon::ArchV67);
838 const bool HasReversePairs = ReversePairs.size() != 0;
839
840 if (!IsPermitted && HasReversePairs) {
841 for (auto R : ReversePairs)
842 reportError("register pair `" + Twine(RI.getName(R)) +
843 "' is not permitted for this architecture");
844 return false;
845 }
846 return true;
847}
848
849bool HexagonMCChecker::checkHVXRestrictions() {
850 bool PacketHasVS3SRC = false;
851 MCInst U;
852 bool PacketHasNonUnaryALU = false;
853 bool PacketHasVP = false;
854 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
856 U = I;
857 PacketHasVS3SRC = true;
858 }
860 PacketHasNonUnaryALU = true;
862 PacketHasVP = true;
863 }
864 if (!PacketHasVS3SRC)
865 return true;
866 bool ret = !PacketHasNonUnaryALU && !PacketHasVP;
867 if (!ret) {
868 reportError("invalid instruction packet: HVX resource use violation");
869 if (PacketHasNonUnaryALU)
870 reportNote(U.getLoc(), "This instruction cannot be paired with an HVX "
871 "non-unary instruction");
872 if (PacketHasVP)
873 reportNote(U.getLoc(), "This instruction cannot be paired with an HVX "
874 "permute instruction");
875 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
877 reportNote(I.getLoc(), "HVX non-unary instruction");
879 reportNote(I.getLoc(), "HVX permute instruction");
880 }
881 }
882 return ret;
883}
884
885// Vd.tmp can't be accumulated
886bool HexagonMCChecker::checkHVXAccum()
887{
888 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
889 bool IsTarget =
890 HexagonMCInstrInfo::isAccumulator(MCII, I) && I.getOperand(0).isReg();
891 if (!IsTarget)
892 continue;
893 MCRegister R = I.getOperand(0).getReg();
894 TmpDefsIterator It = TmpDefs.find(R);
895 if (It != TmpDefs.end()) {
896 reportError("register `" + Twine(RI.getName(R)) + ".tmp" +
897 "' is accumulated in this packet");
898 return false;
899 }
900 }
901 return true;
902}
903
904bool HexagonMCChecker::checkHLX64BitRegUse() {
905 const MCRegisterClass &DoubleRegs =
906 RI.getRegClass(Hexagon::DoubleRegsRegClassID);
907 const MCRegisterClass &IntRegs = RI.getRegClass(Hexagon::IntRegsRegClassID);
908 const MCInst *DoubleInst = nullptr;
909 unsigned NumScalarArg = 0;
910 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
911 if (!HexagonMCInstrInfo::isHLX(MCII, I))
912 continue;
913 const MCInstrDesc &Desc = HexagonMCInstrInfo::getDesc(MCII, I);
914 if (Desc.mayLoad() || Desc.mayStore())
915 continue;
916 bool UsesScalarArg = false;
917 for (const auto &Op : I) {
918 if (!DoubleInst && Op.isReg() && DoubleRegs.contains(Op.getReg()))
919 DoubleInst = &I;
920 if (Op.isImm() || Op.isExpr() ||
921 (Op.isReg() &&
922 (IntRegs.contains(Op.getReg()) || DoubleRegs.contains(Op.getReg()))))
923 UsesScalarArg = true;
924 }
925 if (UsesScalarArg)
926 ++NumScalarArg;
927 }
928
929 if (DoubleInst && NumScalarArg > 1) {
930 reportError(DoubleInst->getLoc(),
931 "HLX instruction using a 64-bit scalar register cannot be "
932 "in the same packet with another HLX instruction using a "
933 "scalar register or immediate");
934 return false;
935 }
936 return true;
937}
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 DoubleRegs[32]
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
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