| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 1 | //===----- HexagonMCChecker.cpp - Instruction bundle checking -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements the checking of insns inside a bundle according to the |
| 11 | // packet constraint rules of the Hexagon ISA. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "HexagonMCChecker.h" |
| 16 | |
| 17 | #include "HexagonBaseInfo.h" |
| 18 | |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCContext.h" |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCInstrDesc.h" |
| 21 | #include "llvm/MC/MCInstrInfo.h" |
| 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/Debug.h" |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 24 | #include "llvm/Support/SourceMgr.h" |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 29 | static cl::opt<bool> |
| 30 | RelaxNVChecks("relax-nv-checks", cl::init(false), cl::ZeroOrMore, |
| 31 | cl::Hidden, cl::desc("Relax checks of new-value validity")); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 32 | |
| 33 | const HexagonMCChecker::PredSense |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 34 | HexagonMCChecker::Unconditional(Hexagon::NoRegister, false); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 35 | |
| 36 | void HexagonMCChecker::init() { |
| 37 | // Initialize read-only registers set. |
| 38 | ReadOnly.insert(Hexagon::PC); |
| Krzysztof Parzyszek | 55db483 | 2017-05-01 20:10:41 +0000 | [diff] [blame] | 39 | ReadOnly.insert(Hexagon::C9_8); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 40 | |
| 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); |
| 45 | } |
| 46 | if (HexagonMCInstrInfo::isOuterLoop(MCB)) { |
| 47 | Defs[Hexagon::SA1].insert(Unconditional); // FIXME: define or change SA0? |
| 48 | Defs[Hexagon::LC1].insert(Unconditional); |
| 49 | } |
| 50 | |
| 51 | if (HexagonMCInstrInfo::isBundle(MCB)) |
| 52 | // Unfurl a bundle. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 53 | for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) { |
| Krzysztof Parzyszek | 8cdfe8e | 2017-02-06 19:35:46 +0000 | [diff] [blame] | 54 | MCInst const &Inst = *I.getInst(); |
| 55 | if (HexagonMCInstrInfo::isDuplex(MCII, Inst)) { |
| 56 | init(*Inst.getOperand(0).getInst()); |
| 57 | init(*Inst.getOperand(1).getInst()); |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 58 | } else |
| Krzysztof Parzyszek | 8cdfe8e | 2017-02-06 19:35:46 +0000 | [diff] [blame] | 59 | init(Inst); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 60 | } |
| 61 | else |
| 62 | init(MCB); |
| 63 | } |
| 64 | |
| Krzysztof Parzyszek | 8cdfe8e | 2017-02-06 19:35:46 +0000 | [diff] [blame] | 65 | void HexagonMCChecker::initReg(MCInst const &MCI, unsigned R, unsigned &PredReg, |
| 66 | bool &isTrue) { |
| 67 | if (HexagonMCInstrInfo::isPredicated(MCII, MCI) && isPredicateRegister(R)) { |
| 68 | // Note an used predicate register. |
| 69 | PredReg = R; |
| 70 | isTrue = HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI); |
| 71 | |
| 72 | // Note use of new predicate register. |
| 73 | if (HexagonMCInstrInfo::isPredicatedNew(MCII, MCI)) |
| 74 | NewPreds.insert(PredReg); |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 75 | } else |
| Krzysztof Parzyszek | 8cdfe8e | 2017-02-06 19:35:46 +0000 | [diff] [blame] | 76 | // Note register use. Super-registers are not tracked directly, |
| 77 | // but their components. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 78 | for (MCRegAliasIterator SRI(R, &RI, !MCSubRegIterator(R, &RI).isValid()); |
| 79 | SRI.isValid(); ++SRI) |
| Krzysztof Parzyszek | 8cdfe8e | 2017-02-06 19:35:46 +0000 | [diff] [blame] | 80 | if (!MCSubRegIterator(*SRI, &RI).isValid()) |
| 81 | // Skip super-registers used indirectly. |
| 82 | Uses.insert(*SRI); |
| 83 | } |
| 84 | |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 85 | void HexagonMCChecker::init(MCInst const &MCI) { |
| 86 | const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MCI); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 87 | unsigned PredReg = Hexagon::NoRegister; |
| 88 | bool isTrue = false; |
| 89 | |
| 90 | // Get used registers. |
| 91 | for (unsigned i = MCID.getNumDefs(); i < MCID.getNumOperands(); ++i) |
| Krzysztof Parzyszek | 8cdfe8e | 2017-02-06 19:35:46 +0000 | [diff] [blame] | 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); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 96 | |
| 97 | // Get implicit register definitions. |
| Craig Topper | 5c32279 | 2015-12-05 17:34:07 +0000 | [diff] [blame] | 98 | if (const MCPhysReg *ImpDef = MCID.getImplicitDefs()) |
| 99 | for (; *ImpDef; ++ImpDef) { |
| 100 | unsigned R = *ImpDef; |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 101 | |
| Craig Topper | 5c32279 | 2015-12-05 17:34:07 +0000 | [diff] [blame] | 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. |
| 105 | continue; |
| 106 | if (Hexagon::PC == R) |
| 107 | // Branches are the only insns that can change the PC, |
| 108 | // otherwise a read-only register. |
| 109 | continue; |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 110 | |
| Craig Topper | 5c32279 | 2015-12-05 17:34:07 +0000 | [diff] [blame] | 111 | if (Hexagon::USR_OVF == R) |
| 112 | // Many insns change the USR implicitly, but only one or another flag. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 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 situ- ations individually. |
| Craig Topper | 5c32279 | 2015-12-05 17:34:07 +0000 | [diff] [blame] | 117 | SoftDefs.insert(R); |
| 118 | else if (isPredicateRegister(R) && |
| 119 | HexagonMCInstrInfo::isPredicateLate(MCII, MCI)) |
| 120 | // Include implicit late predicates. |
| 121 | LatePreds.insert(R); |
| 122 | else |
| 123 | Defs[R].insert(PredSense(PredReg, isTrue)); |
| 124 | } |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 125 | |
| 126 | // Figure out explicit register definitions. |
| 127 | for (unsigned i = 0; i < MCID.getNumDefs(); ++i) { |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 128 | unsigned R = MCI.getOperand(i).getReg(), S = Hexagon::NoRegister; |
| Krzysztof Parzyszek | 96bb4fe | 2016-05-28 01:51:16 +0000 | [diff] [blame] | 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) |
| 133 | R = Hexagon::USR; |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 134 | |
| 135 | // Note register definitions, direct ones as well as indirect side-effects. |
| 136 | // Super-registers are not tracked directly, but their components. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 137 | for (MCRegAliasIterator SRI(R, &RI, !MCSubRegIterator(R, &RI).isValid()); |
| 138 | SRI.isValid(); ++SRI) { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 139 | if (MCSubRegIterator(*SRI, &RI).isValid()) |
| 140 | // Skip super-registers defined indirectly. |
| 141 | continue; |
| 142 | |
| 143 | if (R == *SRI) { |
| 144 | if (S == R) |
| 145 | // Avoid scoring the defined register multiple times. |
| 146 | continue; |
| 147 | else |
| 148 | // Note that the defined register has already been scored. |
| 149 | S = R; |
| 150 | } |
| 151 | |
| 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); |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 158 | else if (HexagonMCInstrInfo::isPredicateLate(MCII, MCI) && |
| 159 | isPredicateRegister(*SRI)) |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 160 | // Some insns produce predicates too late to be used in the same packet. |
| 161 | LatePreds.insert(*SRI); |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 162 | else if (i == 0 && llvm::HexagonMCInstrInfo::getType(MCII, MCI) == |
| 163 | HexagonII::TypeCVI_VM_TMP_LD) |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 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 |
| 166 | // register. |
| 167 | // TODO: relies on the impossibility of a current and a temporary loads |
| 168 | // in the same packet. |
| 169 | TmpDefs.insert(*SRI); |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 170 | else if (i <= 1 && llvm::HexagonMCInstrInfo::hasNewValue2(MCII, MCI)) |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 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) |
| 173 | Uses.insert(*SRI); |
| 174 | else |
| 175 | Defs[*SRI].insert(PredSense(PredReg, isTrue)); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Figure out register definitions that produce new values. |
| 180 | if (HexagonMCInstrInfo::hasNewValue(MCII, MCI)) { |
| 181 | unsigned R = HexagonMCInstrInfo::getNewValueOperand(MCII, MCI).getReg(); |
| 182 | |
| 183 | if (HexagonMCInstrInfo::isCompound(MCII, MCI)) |
| 184 | compoundRegisterMap(R); // Compound insns have a limited register range. |
| 185 | |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 186 | for (MCRegAliasIterator SRI(R, &RI, !MCSubRegIterator(R, &RI).isValid()); |
| 187 | SRI.isValid(); ++SRI) |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 188 | if (!MCSubRegIterator(*SRI, &RI).isValid()) |
| 189 | // No super-registers defined indirectly. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 190 | NewDefs[*SRI].push_back(NewSense::Def( |
| 191 | PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI), |
| 192 | HexagonMCInstrInfo::isFloat(MCII, MCI))); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 193 | |
| 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(); |
| 198 | |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 199 | for (MCRegAliasIterator SRI(R2, &RI, |
| 200 | !MCSubRegIterator(R2, &RI).isValid()); |
| 201 | SRI.isValid(); ++SRI) |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 202 | if (!MCSubRegIterator(*SRI, &RI).isValid()) |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 203 | NewDefs[*SRI].push_back(NewSense::Def( |
| 204 | PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI), |
| 205 | HexagonMCInstrInfo::isFloat(MCII, MCI))); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | // Figure out definitions of new predicate registers. |
| 210 | if (HexagonMCInstrInfo::isPredicatedNew(MCII, MCI)) |
| 211 | for (unsigned i = MCID.getNumDefs(); i < MCID.getNumOperands(); ++i) |
| 212 | if (MCI.getOperand(i).isReg()) { |
| 213 | unsigned P = MCI.getOperand(i).getReg(); |
| 214 | |
| 215 | if (isPredicateRegister(P)) |
| 216 | NewPreds.insert(P); |
| 217 | } |
| 218 | |
| 219 | // Figure out uses of new values. |
| 220 | if (HexagonMCInstrInfo::isNewValue(MCII, MCI)) { |
| 221 | unsigned N = HexagonMCInstrInfo::getNewValueOperand(MCII, MCI).getReg(); |
| 222 | |
| 223 | if (!MCSubRegIterator(N, &RI).isValid()) { |
| 224 | // Super-registers cannot use new values. |
| 225 | if (MCID.isBranch()) |
| Krzysztof Parzyszek | 8cdfe8e | 2017-02-06 19:35:46 +0000 | [diff] [blame] | 226 | NewUses[N] = NewSense::Jmp( |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 227 | llvm::HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeNCJ); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 228 | else |
| Krzysztof Parzyszek | 8cdfe8e | 2017-02-06 19:35:46 +0000 | [diff] [blame] | 229 | NewUses[N] = NewSense::Use( |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 230 | PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI)); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 235 | HexagonMCChecker::HexagonMCChecker(MCContext &Context, MCInstrInfo const &MCII, |
| 236 | MCSubtargetInfo const &STI, MCInst &mcb, |
| 237 | MCRegisterInfo const &ri, bool ReportErrors) |
| 238 | : Context(Context), MCB(mcb), RI(ri), MCII(MCII), STI(STI), |
| 239 | ReportErrors(ReportErrors) { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 240 | init(); |
| 241 | } |
| 242 | |
| Krzysztof Parzyszek | 8cdfe8e | 2017-02-06 19:35:46 +0000 | [diff] [blame] | 243 | bool HexagonMCChecker::check(bool FullCheck) { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 244 | bool chkB = checkBranches(); |
| 245 | bool chkP = checkPredicates(); |
| 246 | bool chkNV = checkNewValues(); |
| 247 | bool chkR = checkRegisters(); |
| Krzysztof Parzyszek | 55db483 | 2017-05-01 20:10:41 +0000 | [diff] [blame] | 248 | bool chkRRO = checkRegistersReadOnly(); |
| Krzysztof Parzyszek | 107f82d | 2017-05-02 17:51:14 +0000 | [diff] [blame] | 249 | checkRegisterCurDefs(); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 250 | bool chkS = checkSolo(); |
| Krzysztof Parzyszek | 8cdfe8e | 2017-02-06 19:35:46 +0000 | [diff] [blame] | 251 | bool chkSh = true; |
| 252 | if (FullCheck) |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 253 | chkSh = checkShuffle(); |
| Krzysztof Parzyszek | 8cdfe8e | 2017-02-06 19:35:46 +0000 | [diff] [blame] | 254 | bool chkSl = true; |
| 255 | if (FullCheck) |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 256 | chkSl = checkSlots(); |
| Krzysztof Parzyszek | 55db483 | 2017-05-01 20:10:41 +0000 | [diff] [blame] | 257 | bool chk = chkB && chkP && chkNV && chkR && chkRRO && chkS && chkSh && chkSl; |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 258 | |
| 259 | return chk; |
| 260 | } |
| 261 | |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 262 | bool HexagonMCChecker::checkSlots() { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 263 | unsigned slotsUsed = 0; |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 264 | for (auto HMI : HexagonMCInstrInfo::bundleInstructions(MCB)) { |
| 265 | MCInst const &MCI = *HMI.getInst(); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 266 | if (HexagonMCInstrInfo::isImmext(MCI)) |
| 267 | continue; |
| 268 | if (HexagonMCInstrInfo::isDuplex(MCII, MCI)) |
| 269 | slotsUsed += 2; |
| 270 | else |
| 271 | ++slotsUsed; |
| 272 | } |
| 273 | |
| 274 | if (slotsUsed > HEXAGON_PACKET_SIZE) { |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 275 | reportError("invalid instruction packet: out of slots"); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 276 | return false; |
| 277 | } |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | // Check legal use of branches. |
| 282 | bool HexagonMCChecker::checkBranches() { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 283 | if (HexagonMCInstrInfo::isBundle(MCB)) { |
| 284 | bool hasConditional = false; |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 285 | unsigned Branches = 0, Conditional = HEXAGON_PRESHUFFLE_PACKET_SIZE, |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 286 | Unconditional = HEXAGON_PRESHUFFLE_PACKET_SIZE; |
| 287 | |
| 288 | for (unsigned i = HexagonMCInstrInfo::bundleInstructionsOffset; |
| 289 | i < MCB.size(); ++i) { |
| 290 | MCInst const &MCI = *MCB.begin()[i].getInst(); |
| 291 | |
| 292 | if (HexagonMCInstrInfo::isImmext(MCI)) |
| 293 | continue; |
| 294 | if (HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch() || |
| 295 | HexagonMCInstrInfo::getDesc(MCII, MCI).isCall()) { |
| 296 | ++Branches; |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 297 | if (HexagonMCInstrInfo::isPredicated(MCII, MCI) || |
| 298 | HexagonMCInstrInfo::isPredicatedNew(MCII, MCI)) { |
| 299 | hasConditional = true; |
| 300 | Conditional = i; // Record the position of the conditional branch. |
| 301 | } else { |
| 302 | Unconditional = i; // Record the position of the unconditional branch. |
| 303 | } |
| 304 | } |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | if (Branches) // FIXME: should "Defs.count(Hexagon::PC)" be here too? |
| 308 | if (HexagonMCInstrInfo::isInnerLoop(MCB) || |
| 309 | HexagonMCInstrInfo::isOuterLoop(MCB)) { |
| 310 | // Error out if there's any branch in a loop-end packet. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 311 | Twine N(HexagonMCInstrInfo::isInnerLoop(MCB) ? '0' : '1'); |
| 312 | reportError("packet marked with `:endloop" + N + "' " + |
| 313 | "cannot contain instructions that modify register " + "`" + |
| 314 | llvm::Twine(RI.getName(Hexagon::PC)) + "'"); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 315 | return false; |
| 316 | } |
| 317 | if (Branches > 1) |
| 318 | if (!hasConditional || Conditional > Unconditional) { |
| 319 | // Error out if more than one unconditional branch or |
| 320 | // the conditional branch appears after the unconditional one. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 321 | reportError( |
| 322 | "unconditional branch cannot precede another branch in packet"); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | // Check legal use of predicate registers. |
| 331 | bool HexagonMCChecker::checkPredicates() { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 332 | // Check for proper use of new predicate registers. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 333 | for (const auto &I : NewPreds) { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 334 | unsigned P = I; |
| 335 | |
| 336 | if (!Defs.count(P) || LatePreds.count(P)) { |
| 337 | // Error out if the new predicate register is not defined, |
| 338 | // or defined "late" |
| 339 | // (e.g., "{ if (p3.new)... ; p3 = sp1loop0(#r7:2, Rs) }"). |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 340 | reportErrorNewValue(P); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Check for proper use of auto-anded of predicate registers. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 346 | for (const auto &I : LatePreds) { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 347 | unsigned P = I; |
| 348 | |
| 349 | if (LatePreds.count(P) > 1 || Defs.count(P)) { |
| 350 | // Error out if predicate register defined "late" multiple times or |
| 351 | // defined late and regularly defined |
| 352 | // (e.g., "{ p3 = sp1loop0(...); p3 = cmp.eq(...) }". |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 353 | reportErrorRegisters(P); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 354 | return false; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | // Check legal use of new values. |
| 362 | bool HexagonMCChecker::checkNewValues() { |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 363 | for (auto &I : NewUses) { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 364 | unsigned R = I.first; |
| 365 | NewSense &US = I.second; |
| 366 | |
| 367 | if (!hasValidNewValueDef(US, NewDefs[R])) { |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 368 | reportErrorNewValue(R); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | return true; |
| 374 | } |
| 375 | |
| Krzysztof Parzyszek | 55db483 | 2017-05-01 20:10:41 +0000 | [diff] [blame] | 376 | bool HexagonMCChecker::checkRegistersReadOnly() { |
| 377 | for (auto I : HexagonMCInstrInfo::bundleInstructions(MCB)) { |
| 378 | MCInst const &Inst = *I.getInst(); |
| 379 | unsigned Defs = HexagonMCInstrInfo::getDesc(MCII, Inst).getNumDefs(); |
| 380 | for (unsigned j = 0; j < Defs; ++j) { |
| 381 | MCOperand const &Operand = Inst.getOperand(j); |
| 382 | assert(Operand.isReg() && "Def is not a register"); |
| 383 | unsigned Register = Operand.getReg(); |
| 384 | if (ReadOnly.find(Register) != ReadOnly.end()) { |
| 385 | reportError(Inst.getLoc(), "Cannot write to read-only register `" + |
| 386 | llvm::Twine(RI.getName(Register)) + "'"); |
| 387 | return false; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
| Krzysztof Parzyszek | 107f82d | 2017-05-02 17:51:14 +0000 | [diff] [blame] | 394 | bool HexagonMCChecker::registerUsed(MCInst const &Inst, unsigned Register) { |
| Krzysztof Parzyszek | 1cc6bfb | 2017-05-02 17:53:51 +0000 | [diff] [blame^] | 395 | unsigned Defs = HexagonMCInstrInfo::getDesc(MCII, Inst).getNumDefs(); |
| 396 | for (unsigned j = Defs, n = Inst.getNumOperands(); j < n; ++j) { |
| 397 | MCOperand const &Operand = Inst.getOperand(j); |
| 398 | if (Operand.isReg() && Operand.getReg() == Register) |
| Krzysztof Parzyszek | 107f82d | 2017-05-02 17:51:14 +0000 | [diff] [blame] | 399 | return true; |
| Krzysztof Parzyszek | 107f82d | 2017-05-02 17:51:14 +0000 | [diff] [blame] | 400 | } |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | bool HexagonMCChecker::registerUsed(unsigned Register) { |
| Krzysztof Parzyszek | 1cc6bfb | 2017-05-02 17:53:51 +0000 | [diff] [blame^] | 405 | for (auto const &I: HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) |
| 406 | if (registerUsed(I, Register)) |
| 407 | return true; |
| 408 | return false; |
| Krzysztof Parzyszek | 107f82d | 2017-05-02 17:51:14 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void HexagonMCChecker::checkRegisterCurDefs() { |
| 412 | for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) { |
| 413 | MCInst const &Inst = *I.getInst(); |
| 414 | if (HexagonMCInstrInfo::isCVINew(MCII, Inst) && |
| 415 | HexagonMCInstrInfo::getDesc(MCII, Inst).mayLoad()) { |
| 416 | unsigned Register = Inst.getOperand(0).getReg(); |
| 417 | if (!registerUsed(Register)) |
| 418 | reportWarning("Register `" + llvm::Twine(RI.getName(Register)) + |
| 419 | "' used with `.cur' " |
| 420 | "but not used in the same packet"); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 425 | // Check for legal register uses and definitions. |
| 426 | bool HexagonMCChecker::checkRegisters() { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 427 | // Check for proper register definitions. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 428 | for (const auto &I : Defs) { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 429 | unsigned R = I.first; |
| 430 | |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 431 | if (isLoopRegister(R) && Defs.count(R) > 1 && |
| 432 | (HexagonMCInstrInfo::isInnerLoop(MCB) || |
| 433 | HexagonMCInstrInfo::isOuterLoop(MCB))) { |
| 434 | // Error out for definitions of loop registers at the end of a loop. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 435 | reportError("loop-setup and some branch instructions " |
| 436 | "cannot be in the same packet"); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 437 | return false; |
| 438 | } |
| 439 | if (SoftDefs.count(R)) { |
| 440 | // Error out for explicit changes to registers also weakly defined |
| 441 | // (e.g., "{ usr = r0; r0 = sfadd(...) }"). |
| 442 | unsigned UsrR = Hexagon::USR; // Silence warning about mixed types in ?:. |
| 443 | unsigned BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R; |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 444 | reportErrorRegisters(BadR); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | if (!isPredicateRegister(R) && Defs[R].size() > 1) { |
| 448 | // Check for multiple register definitions. |
| 449 | PredSet &PM = Defs[R]; |
| 450 | |
| 451 | // Check for multiple unconditional register definitions. |
| 452 | if (PM.count(Unconditional)) { |
| 453 | // Error out on an unconditional change when there are any other |
| 454 | // changes, conditional or not. |
| 455 | unsigned UsrR = Hexagon::USR; |
| 456 | unsigned BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R; |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 457 | reportErrorRegisters(BadR); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 458 | return false; |
| 459 | } |
| 460 | // Check for multiple conditional register definitions. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 461 | for (const auto &J : PM) { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 462 | PredSense P = J; |
| 463 | |
| 464 | // Check for multiple uses of the same condition. |
| 465 | if (PM.count(P) > 1) { |
| 466 | // Error out on conditional changes based on the same predicate |
| 467 | // (e.g., "{ if (!p0) r0 =...; if (!p0) r0 =... }"). |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 468 | reportErrorRegisters(R); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 469 | return false; |
| 470 | } |
| 471 | // Check for the use of the complementary condition. |
| 472 | P.second = !P.second; |
| 473 | if (PM.count(P) && PM.size() > 2) { |
| 474 | // Error out on conditional changes based on the same predicate |
| 475 | // multiple times |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 476 | // (e.g., "{ if (p0) r0 =...; if (!p0) r0 =... }; if (!p0) r0 =... |
| 477 | // }"). |
| 478 | reportErrorRegisters(R); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 479 | return false; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 485 | // Check for use of temporary definitions. |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 486 | for (const auto &I : TmpDefs) { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 487 | unsigned R = I; |
| 488 | |
| 489 | if (!Uses.count(R)) { |
| 490 | // special case for vhist |
| 491 | bool vHistFound = false; |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 492 | for (auto const &HMI : HexagonMCInstrInfo::bundleInstructions(MCB)) { |
| 493 | if (llvm::HexagonMCInstrInfo::getType(MCII, *HMI.getInst()) == |
| 494 | HexagonII::TypeCVI_HIST) { |
| 495 | vHistFound = true; // vhist() implicitly uses ALL REGxx.tmp |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 496 | break; |
| 497 | } |
| 498 | } |
| 499 | // Warn on an unused temporary definition. |
| 500 | if (vHistFound == false) { |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 501 | reportWarning("register `" + llvm::Twine(RI.getName(R)) + |
| 502 | "' used with `.tmp' " |
| 503 | "but not used in the same packet"); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 504 | return true; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | // Check for legal use of solo insns. |
| 513 | bool HexagonMCChecker::checkSolo() { |
| Krzysztof Parzyszek | e96d27a | 2017-05-01 20:06:01 +0000 | [diff] [blame] | 514 | if (HexagonMCInstrInfo::bundleSize(MCB) > 1) |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 515 | for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 516 | if (llvm::HexagonMCInstrInfo::isSolo(MCII, *I.getInst())) { |
| Krzysztof Parzyszek | e96d27a | 2017-05-01 20:06:01 +0000 | [diff] [blame] | 517 | SMLoc Loc = I.getInst()->getLoc(); |
| 518 | reportError(Loc, "Instruction is marked `isSolo' and " |
| 519 | "cannot have other instructions in " |
| 520 | "the same packet"); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 521 | return false; |
| 522 | } |
| 523 | } |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 524 | |
| 525 | return true; |
| 526 | } |
| 527 | |
| 528 | bool HexagonMCChecker::checkShuffle() { |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 529 | HexagonMCShuffler MCSDX(Context, ReportErrors, MCII, STI, MCB); |
| 530 | return MCSDX.check(); |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 533 | void HexagonMCChecker::compoundRegisterMap(unsigned &Register) { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 534 | switch (Register) { |
| 535 | default: |
| 536 | break; |
| 537 | case Hexagon::R15: |
| 538 | Register = Hexagon::R23; |
| 539 | break; |
| 540 | case Hexagon::R14: |
| 541 | Register = Hexagon::R22; |
| 542 | break; |
| 543 | case Hexagon::R13: |
| 544 | Register = Hexagon::R21; |
| 545 | break; |
| 546 | case Hexagon::R12: |
| 547 | Register = Hexagon::R20; |
| 548 | break; |
| 549 | case Hexagon::R11: |
| 550 | Register = Hexagon::R19; |
| 551 | break; |
| 552 | case Hexagon::R10: |
| 553 | Register = Hexagon::R18; |
| 554 | break; |
| 555 | case Hexagon::R9: |
| 556 | Register = Hexagon::R17; |
| 557 | break; |
| 558 | case Hexagon::R8: |
| 559 | Register = Hexagon::R16; |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | bool HexagonMCChecker::hasValidNewValueDef(const NewSense &Use, |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 565 | const NewSenseList &Defs) const { |
| Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 566 | bool Strict = !RelaxNVChecks; |
| 567 | |
| 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)) |
| 572 | continue; |
| 573 | // If the definition was not predicated, then it does not matter if |
| 574 | // the use is. |
| 575 | if (Def.PredReg == 0) |
| 576 | return true; |
| 577 | // With the strict checks, both the definition and the use must be |
| 578 | // predicated on the same register and condition. |
| 579 | if (Strict) { |
| 580 | if (Def.PredReg == Use.PredReg && Def.Cond == Use.Cond) |
| 581 | return true; |
| 582 | } else { |
| 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) |
| 587 | return true; |
| 588 | } |
| 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 593 | void HexagonMCChecker::reportErrorRegisters(unsigned Register) { |
| 594 | reportError("register `" + llvm::Twine(RI.getName(Register)) + |
| 595 | "' modified more than once"); |
| 596 | } |
| 597 | |
| 598 | void HexagonMCChecker::reportErrorNewValue(unsigned Register) { |
| 599 | reportError("register `" + llvm::Twine(RI.getName(Register)) + |
| 600 | "' used with `.new' " |
| 601 | "but not validly modified in the same packet"); |
| 602 | } |
| 603 | |
| 604 | void HexagonMCChecker::reportError(llvm::Twine const &Msg) { |
| Krzysztof Parzyszek | e96d27a | 2017-05-01 20:06:01 +0000 | [diff] [blame] | 605 | reportError(MCB.getLoc(), Msg); |
| 606 | } |
| 607 | |
| 608 | void HexagonMCChecker::reportError(SMLoc Loc, llvm::Twine const &Msg) { |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 609 | if (ReportErrors) |
| Krzysztof Parzyszek | e96d27a | 2017-05-01 20:06:01 +0000 | [diff] [blame] | 610 | Context.reportError(Loc, Msg); |
| Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void HexagonMCChecker::reportWarning(llvm::Twine const &Msg) { |
| 614 | if (ReportErrors) { |
| 615 | auto SM = Context.getSourceManager(); |
| 616 | if (SM) |
| 617 | SM->PrintMessage(MCB.getLoc(), SourceMgr::DK_Warning, Msg); |
| 618 | } |
| 619 | } |