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/MCInstrDesc.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
27 static cl::opt<bool> RelaxNVChecks("relax-nv-checks", cl::init(false),
28 cl::ZeroOrMore, cl::Hidden, cl::desc("Relax checks of new-value validity"));
30 const HexagonMCChecker::PredSense
31 HexagonMCChecker::Unconditional(Hexagon::NoRegister, false);
33 void HexagonMCChecker::init() {
34 // Initialize read-only registers set.
35 ReadOnly.insert(Hexagon::PC);
37 // Figure out the loop-registers definitions.
38 if (HexagonMCInstrInfo::isInnerLoop(MCB)) {
39 Defs[Hexagon::SA0].insert(Unconditional); // FIXME: define or change SA0?
40 Defs[Hexagon::LC0].insert(Unconditional);
42 if (HexagonMCInstrInfo::isOuterLoop(MCB)) {
43 Defs[Hexagon::SA1].insert(Unconditional); // FIXME: define or change SA0?
44 Defs[Hexagon::LC1].insert(Unconditional);
47 if (HexagonMCInstrInfo::isBundle(MCB))
49 for (auto const&I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
50 MCInst const &Inst = *I.getInst();
51 if (HexagonMCInstrInfo::isDuplex(MCII, Inst)) {
52 init(*Inst.getOperand(0).getInst());
53 init(*Inst.getOperand(1).getInst());
62 void HexagonMCChecker::initReg(MCInst const &MCI, unsigned R, unsigned &PredReg,
64 if (HexagonMCInstrInfo::isPredicated(MCII, MCI) && isPredicateRegister(R)) {
65 // Note an used predicate register.
67 isTrue = HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI);
69 // Note use of new predicate register.
70 if (HexagonMCInstrInfo::isPredicatedNew(MCII, MCI))
71 NewPreds.insert(PredReg);
74 // Note register use. Super-registers are not tracked directly,
75 // but their components.
76 for(MCRegAliasIterator SRI(R, &RI, !MCSubRegIterator(R, &RI).isValid());
79 if (!MCSubRegIterator(*SRI, &RI).isValid())
80 // Skip super-registers used indirectly.
84 void HexagonMCChecker::init(MCInst const& MCI) {
85 const MCInstrDesc& MCID = HexagonMCInstrInfo::getDesc(MCII, MCI);
86 unsigned PredReg = Hexagon::NoRegister;
89 // Get used registers.
90 for (unsigned i = MCID.getNumDefs(); i < MCID.getNumOperands(); ++i)
91 if (MCI.getOperand(i).isReg())
92 initReg(MCI, MCI.getOperand(i).getReg(), PredReg, isTrue);
93 for (unsigned i = 0; i < MCID.getNumImplicitUses(); ++i)
94 initReg(MCI, MCID.getImplicitUses()[i], PredReg, isTrue);
96 // Get implicit register definitions.
97 if (const MCPhysReg *ImpDef = MCID.getImplicitDefs())
98 for (; *ImpDef; ++ImpDef) {
101 if (Hexagon::R31 != R && MCID.isCall())
102 // Any register other than the LR and the PC are actually volatile ones
103 // as defined by the ABI, not modified implicitly by the call insn.
105 if (Hexagon::PC == R)
106 // Branches are the only insns that can change the PC,
107 // otherwise a read-only register.
110 if (Hexagon::USR_OVF == R)
111 // Many insns change the USR implicitly, but only one or another flag.
112 // The instruction table models the USR.OVF flag, which can be implicitly
113 // modified more than once, but cannot be modified in the same packet
114 // with an instruction that modifies is explicitly. Deal with such situ-
115 // ations individually.
117 else if (isPredicateRegister(R) &&
118 HexagonMCInstrInfo::isPredicateLate(MCII, MCI))
119 // Include implicit late predicates.
122 Defs[R].insert(PredSense(PredReg, isTrue));
125 // Figure out explicit register definitions.
126 for (unsigned i = 0; i < MCID.getNumDefs(); ++i) {
127 unsigned R = MCI.getOperand(i).getReg(),
128 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());
140 if (MCSubRegIterator(*SRI, &RI).isValid())
141 // Skip super-registers defined indirectly.
146 // Avoid scoring the defined register multiple times.
149 // Note that the defined register has already been scored.
153 if (Hexagon::P3_0 != R && Hexagon::P3_0 == *SRI)
154 // P3:0 is a special case, since multiple predicate register definitions
155 // in a packet is allowed as the equivalent of their logical "and".
156 // Only an explicit definition of P3:0 is noted as such; if a
157 // side-effect, then note as a soft definition.
158 SoftDefs.insert(*SRI);
159 else if (HexagonMCInstrInfo::isPredicateLate(MCII, MCI) && 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) == HexagonII::TypeCVI_VM_CUR_LD)
163 // Current loads should be used in the same packet.
164 // TODO: relies on the impossibility of a current and a temporary loads
165 // in the same packet.
166 CurDefs.insert(*SRI), Defs[*SRI].insert(PredSense(PredReg, isTrue));
167 else if (i == 0 && llvm::HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCVI_VM_TMP_LD)
168 // Temporary loads should be used in the same packet, but don't commit
169 // results, so it should be disregarded if another insn changes the same
171 // TODO: relies on the impossibility of a current and a temporary loads
172 // in the same packet.
173 TmpDefs.insert(*SRI);
174 else if (i <= 1 && llvm::HexagonMCInstrInfo::hasNewValue2(MCII, MCI) )
175 // vshuff(Vx, Vy, Rx) <- Vx(0) and Vy(1) are both source and
176 // destination registers with this instruction. same for vdeal(Vx,Vy,Rx)
179 Defs[*SRI].insert(PredSense(PredReg, isTrue));
183 // Figure out register definitions that produce new values.
184 if (HexagonMCInstrInfo::hasNewValue(MCII, MCI)) {
185 unsigned R = HexagonMCInstrInfo::getNewValueOperand(MCII, MCI).getReg();
187 if (HexagonMCInstrInfo::isCompound(MCII, MCI))
188 compoundRegisterMap(R); // Compound insns have a limited register range.
190 for(MCRegAliasIterator SRI(R, &RI, !MCSubRegIterator(R, &RI).isValid());
193 if (!MCSubRegIterator(*SRI, &RI).isValid())
194 // No super-registers defined indirectly.
195 NewDefs[*SRI].push_back(NewSense::Def(PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI),
196 HexagonMCInstrInfo::isFloat(MCII, MCI)));
198 // For fairly unique 2-dot-new producers, example:
199 // vdeal(V1, V9, R0) V1.new and V9.new can be used by consumers.
200 if (HexagonMCInstrInfo::hasNewValue2(MCII, MCI)) {
201 unsigned R2 = HexagonMCInstrInfo::getNewValueOperand2(MCII, MCI).getReg();
203 for(MCRegAliasIterator SRI(R2, &RI, !MCSubRegIterator(R2, &RI).isValid());
206 if (!MCSubRegIterator(*SRI, &RI).isValid())
207 NewDefs[*SRI].push_back(NewSense::Def(PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI),
208 HexagonMCInstrInfo::isFloat(MCII, MCI)));
212 // Figure out definitions of new predicate registers.
213 if (HexagonMCInstrInfo::isPredicatedNew(MCII, MCI))
214 for (unsigned i = MCID.getNumDefs(); i < MCID.getNumOperands(); ++i)
215 if (MCI.getOperand(i).isReg()) {
216 unsigned P = MCI.getOperand(i).getReg();
218 if (isPredicateRegister(P))
222 // Figure out uses of new values.
223 if (HexagonMCInstrInfo::isNewValue(MCII, MCI)) {
224 unsigned N = HexagonMCInstrInfo::getNewValueOperand(MCII, MCI).getReg();
226 if (!MCSubRegIterator(N, &RI).isValid()) {
227 // Super-registers cannot use new values.
229 NewUses[N] = NewSense::Jmp(
230 llvm::HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeNCJ);
232 NewUses[N] = NewSense::Use(
233 PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI));
238 HexagonMCChecker::HexagonMCChecker(MCInstrInfo const &MCII, MCSubtargetInfo const &STI, MCInst &mcb, MCInst &mcbdx,
239 MCRegisterInfo const &ri)
240 : MCB(mcb), MCBDX(mcbdx), RI(ri), MCII(MCII), STI(STI),
241 bLoadErrInfo(false) {
245 bool HexagonMCChecker::check(bool FullCheck) {
246 bool chkB = checkBranches();
247 bool chkP = checkPredicates();
248 bool chkNV = checkNewValues();
249 bool chkR = checkRegisters();
250 bool chkS = checkSolo();
253 chkSh = checkShuffle();
256 chkSl = checkSlots();
257 bool chk = chkB && chkP && chkNV && chkR && chkS && chkSh && chkSl;
262 bool HexagonMCChecker::checkSlots()
265 unsigned slotsUsed = 0;
266 for (auto HMI: HexagonMCInstrInfo::bundleInstructions(MCBDX)) {
267 MCInst const& MCI = *HMI.getInst();
268 if (HexagonMCInstrInfo::isImmext(MCI))
270 if (HexagonMCInstrInfo::isDuplex(MCII, MCI))
276 if (slotsUsed > HEXAGON_PACKET_SIZE) {
277 HexagonMCErrInfo errInfo;
278 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_NOSLOTS);
285 // Check legal use of branches.
286 bool HexagonMCChecker::checkBranches() {
287 HexagonMCErrInfo errInfo;
288 if (HexagonMCInstrInfo::isBundle(MCB)) {
289 bool hasConditional = false;
290 unsigned Branches = 0,
291 Conditional = HEXAGON_PRESHUFFLE_PACKET_SIZE,
292 Unconditional = HEXAGON_PRESHUFFLE_PACKET_SIZE;
294 for (unsigned i = HexagonMCInstrInfo::bundleInstructionsOffset;
295 i < MCB.size(); ++i) {
296 MCInst const &MCI = *MCB.begin()[i].getInst();
298 if (HexagonMCInstrInfo::isImmext(MCI))
300 if (HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch() ||
301 HexagonMCInstrInfo::getDesc(MCII, MCI).isCall()) {
303 if (HexagonMCInstrInfo::isPredicated(MCII, MCI) ||
304 HexagonMCInstrInfo::isPredicatedNew(MCII, MCI)) {
305 hasConditional = true;
306 Conditional = i; // Record the position of the conditional branch.
308 Unconditional = i; // Record the position of the unconditional branch.
313 if (Branches) // FIXME: should "Defs.count(Hexagon::PC)" be here too?
314 if (HexagonMCInstrInfo::isInnerLoop(MCB) ||
315 HexagonMCInstrInfo::isOuterLoop(MCB)) {
316 // Error out if there's any branch in a loop-end packet.
317 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_ENDLOOP, Hexagon::PC);
322 if (!hasConditional || Conditional > Unconditional) {
323 // Error out if more than one unconditional branch or
324 // the conditional branch appears after the unconditional one.
325 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_BRANCHES);
334 // Check legal use of predicate registers.
335 bool HexagonMCChecker::checkPredicates() {
336 HexagonMCErrInfo errInfo;
337 // Check for proper use of new predicate registers.
338 for (const auto& I : NewPreds) {
341 if (!Defs.count(P) || LatePreds.count(P)) {
342 // Error out if the new predicate register is not defined,
344 // (e.g., "{ if (p3.new)... ; p3 = sp1loop0(#r7:2, Rs) }").
345 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_NEWP, P);
351 // Check for proper use of auto-anded of predicate registers.
352 for (const auto& I : LatePreds) {
355 if (LatePreds.count(P) > 1 || Defs.count(P)) {
356 // Error out if predicate register defined "late" multiple times or
357 // defined late and regularly defined
358 // (e.g., "{ p3 = sp1loop0(...); p3 = cmp.eq(...) }".
359 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_REGISTERS, P);
368 // Check legal use of new values.
369 bool HexagonMCChecker::checkNewValues() {
370 HexagonMCErrInfo errInfo;
371 memset(&errInfo, 0, sizeof(errInfo));
372 for (auto& I : NewUses) {
373 unsigned R = I.first;
374 NewSense &US = I.second;
376 if (!hasValidNewValueDef(US, NewDefs[R])) {
377 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_NEWV, R);
386 // Check for legal register uses and definitions.
387 bool HexagonMCChecker::checkRegisters() {
388 HexagonMCErrInfo errInfo;
389 // Check for proper register definitions.
390 for (const auto& I : Defs) {
391 unsigned R = I.first;
393 if (ReadOnly.count(R)) {
394 // Error out for definitions of read-only registers.
395 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_READONLY, R);
399 if (isLoopRegister(R) && Defs.count(R) > 1 &&
400 (HexagonMCInstrInfo::isInnerLoop(MCB) ||
401 HexagonMCInstrInfo::isOuterLoop(MCB))) {
402 // Error out for definitions of loop registers at the end of a loop.
403 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_LOOP, R);
407 if (SoftDefs.count(R)) {
408 // Error out for explicit changes to registers also weakly defined
409 // (e.g., "{ usr = r0; r0 = sfadd(...) }").
410 unsigned UsrR = Hexagon::USR; // Silence warning about mixed types in ?:.
411 unsigned BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R;
412 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_REGISTERS, BadR);
416 if (!isPredicateRegister(R) && Defs[R].size() > 1) {
417 // Check for multiple register definitions.
418 PredSet &PM = Defs[R];
420 // Check for multiple unconditional register definitions.
421 if (PM.count(Unconditional)) {
422 // Error out on an unconditional change when there are any other
423 // changes, conditional or not.
424 unsigned UsrR = Hexagon::USR;
425 unsigned BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R;
426 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_REGISTERS, BadR);
430 // Check for multiple conditional register definitions.
431 for (const auto& J : PM) {
434 // Check for multiple uses of the same condition.
435 if (PM.count(P) > 1) {
436 // Error out on conditional changes based on the same predicate
437 // (e.g., "{ if (!p0) r0 =...; if (!p0) r0 =... }").
438 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_REGISTERS, R);
442 // Check for the use of the complementary condition.
443 P.second = !P.second;
444 if (PM.count(P) && PM.size() > 2) {
445 // Error out on conditional changes based on the same predicate
447 // (e.g., "{ if (p0) r0 =...; if (!p0) r0 =... }; if (!p0) r0 =... }").
448 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_REGISTERS, R);
456 // Check for use of current definitions.
457 for (const auto& I : CurDefs) {
460 if (!Uses.count(R)) {
461 // Warn on an unused current definition.
462 errInfo.setWarning(HexagonMCErrInfo::CHECK_WARN_CURRENT, R);
468 // Check for use of temporary definitions.
469 for (const auto& I : TmpDefs) {
472 if (!Uses.count(R)) {
473 // special case for vhist
474 bool vHistFound = false;
475 for (auto const&HMI : HexagonMCInstrInfo::bundleInstructions(MCB)) {
476 if(llvm::HexagonMCInstrInfo::getType(MCII, *HMI.getInst()) == HexagonII::TypeCVI_HIST) {
477 vHistFound = true; // vhist() implicitly uses ALL REGxx.tmp
481 // Warn on an unused temporary definition.
482 if (vHistFound == false) {
483 errInfo.setWarning(HexagonMCErrInfo::CHECK_WARN_TEMPORARY, R);
493 // Check for legal use of solo insns.
494 bool HexagonMCChecker::checkSolo() {
495 HexagonMCErrInfo errInfo;
496 if (HexagonMCInstrInfo::isBundle(MCB) &&
497 HexagonMCInstrInfo::bundleSize(MCB) > 1) {
498 for (auto const&I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
499 if (llvm::HexagonMCInstrInfo::isSolo(MCII, *I.getInst())) {
500 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_SOLO);
510 bool HexagonMCChecker::checkShuffle() {
511 HexagonMCErrInfo errInfo;
512 // Branch info is lost when duplexing. The unduplexed insns must be
513 // checked and only branch errors matter for this case.
514 HexagonMCShuffler MCS(true, MCII, STI, MCB);
516 if (MCS.getError() == HexagonShuffler::SHUFFLE_ERROR_BRANCHES) {
517 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_SHUFFLE);
518 errInfo.setShuffleError(MCS.getError());
523 HexagonMCShuffler MCSDX(true, MCII, STI, MCBDX);
524 if (!MCSDX.check()) {
525 errInfo.setError(HexagonMCErrInfo::CHECK_ERROR_SHUFFLE);
526 errInfo.setShuffleError(MCSDX.getError());
533 void HexagonMCChecker::compoundRegisterMap(unsigned& Register) {
538 Register = Hexagon::R23;
541 Register = Hexagon::R22;
544 Register = Hexagon::R21;
547 Register = Hexagon::R20;
550 Register = Hexagon::R19;
553 Register = Hexagon::R18;
556 Register = Hexagon::R17;
559 Register = Hexagon::R16;
564 bool HexagonMCChecker::hasValidNewValueDef(const NewSense &Use,
565 const NewSenseList &Defs) const {
566 bool Strict = !RelaxNVChecks;
568 for (unsigned i = 0, n = Defs.size(); i < n; ++i) {
569 const NewSense &Def = Defs[i];
570 // NVJ cannot use a new FP value [7.6.1]
571 if (Use.IsNVJ && (Def.IsFloat || Def.PredReg != 0))
573 // If the definition was not predicated, then it does not matter if
575 if (Def.PredReg == 0)
577 // With the strict checks, both the definition and the use must be
578 // predicated on the same register and condition.
580 if (Def.PredReg == Use.PredReg && Def.Cond == Use.Cond)
583 // With the relaxed checks, if the definition was predicated, the only
584 // detectable violation is if the use is predicated on the opposing
585 // condition, otherwise, it's ok.
586 if (Def.PredReg != Use.PredReg || Def.Cond == Use.Cond)