blob: 6ce0277a073d8a87bd257b832a65892727419258 [file] [log] [blame]
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001//===----- 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 Parzyszeke12d1e72017-05-01 19:41:43 +000019#include "llvm/MC/MCContext.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000020#include "llvm/MC/MCInstrDesc.h"
21#include "llvm/MC/MCInstrInfo.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Debug.h"
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +000024#include "llvm/Support/SourceMgr.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000025#include "llvm/Support/raw_ostream.h"
26
27using namespace llvm;
28
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +000029static 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 LeMahieu7cd08922015-11-09 04:07:48 +000032
33const HexagonMCChecker::PredSense
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +000034 HexagonMCChecker::Unconditional(Hexagon::NoRegister, false);
Colin LeMahieu7cd08922015-11-09 04:07:48 +000035
36void HexagonMCChecker::init() {
37 // Initialize read-only registers set.
38 ReadOnly.insert(Hexagon::PC);
Krzysztof Parzyszek55db4832017-05-01 20:10:41 +000039 ReadOnly.insert(Hexagon::C9_8);
Colin LeMahieu7cd08922015-11-09 04:07:48 +000040
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 Parzyszeke12d1e72017-05-01 19:41:43 +000053 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000054 MCInst const &Inst = *I.getInst();
55 if (HexagonMCInstrInfo::isDuplex(MCII, Inst)) {
56 init(*Inst.getOperand(0).getInst());
57 init(*Inst.getOperand(1).getInst());
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +000058 } else
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000059 init(Inst);
Colin LeMahieu7cd08922015-11-09 04:07:48 +000060 }
61 else
62 init(MCB);
63}
64
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000065void 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 Parzyszeke12d1e72017-05-01 19:41:43 +000075 } else
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000076 // Note register use. Super-registers are not tracked directly,
77 // but their components.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +000078 for (MCRegAliasIterator SRI(R, &RI, !MCSubRegIterator(R, &RI).isValid());
79 SRI.isValid(); ++SRI)
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000080 if (!MCSubRegIterator(*SRI, &RI).isValid())
81 // Skip super-registers used indirectly.
82 Uses.insert(*SRI);
83}
84
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +000085void HexagonMCChecker::init(MCInst const &MCI) {
86 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MCI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +000087 unsigned PredReg = Hexagon::NoRegister;
88 bool isTrue = false;
89
90 // Get used registers.
91 for (unsigned i = MCID.getNumDefs(); i < MCID.getNumOperands(); ++i)
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000092 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 LeMahieu7cd08922015-11-09 04:07:48 +000096
97 // Get implicit register definitions.
Craig Topper5c322792015-12-05 17:34:07 +000098 if (const MCPhysReg *ImpDef = MCID.getImplicitDefs())
99 for (; *ImpDef; ++ImpDef) {
100 unsigned R = *ImpDef;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000101
Craig Topper5c322792015-12-05 17:34:07 +0000102 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 LeMahieu7cd08922015-11-09 04:07:48 +0000110
Craig Topper5c322792015-12-05 17:34:07 +0000111 if (Hexagon::USR_OVF == R)
112 // Many insns change the USR implicitly, but only one or another flag.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000113 // 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 Topper5c322792015-12-05 17:34:07 +0000117 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 LeMahieu7cd08922015-11-09 04:07:48 +0000125
126 // Figure out explicit register definitions.
127 for (unsigned i = 0; i < MCID.getNumDefs(); ++i) {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000128 unsigned R = MCI.getOperand(i).getReg(), S = Hexagon::NoRegister;
Krzysztof Parzyszek96bb4fe2016-05-28 01:51:16 +0000129 // 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 LeMahieu7cd08922015-11-09 04:07:48 +0000134
135 // Note register definitions, direct ones as well as indirect side-effects.
136 // Super-registers are not tracked directly, but their components.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000137 for (MCRegAliasIterator SRI(R, &RI, !MCSubRegIterator(R, &RI).isValid());
138 SRI.isValid(); ++SRI) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000139 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000158 else if (HexagonMCInstrInfo::isPredicateLate(MCII, MCI) &&
159 isPredicateRegister(*SRI))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000160 // Some insns produce predicates too late to be used in the same packet.
161 LatePreds.insert(*SRI);
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000162 else if (i == 0 && llvm::HexagonMCInstrInfo::getType(MCII, MCI) ==
163 HexagonII::TypeCVI_VM_TMP_LD)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000164 // 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000170 else if (i <= 1 && llvm::HexagonMCInstrInfo::hasNewValue2(MCII, MCI))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000171 // 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000186 for (MCRegAliasIterator SRI(R, &RI, !MCSubRegIterator(R, &RI).isValid());
187 SRI.isValid(); ++SRI)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000188 if (!MCSubRegIterator(*SRI, &RI).isValid())
189 // No super-registers defined indirectly.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000190 NewDefs[*SRI].push_back(NewSense::Def(
191 PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI),
192 HexagonMCInstrInfo::isFloat(MCII, MCI)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000193
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 Parzyszeke12d1e72017-05-01 19:41:43 +0000199 for (MCRegAliasIterator SRI(R2, &RI,
200 !MCSubRegIterator(R2, &RI).isValid());
201 SRI.isValid(); ++SRI)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000202 if (!MCSubRegIterator(*SRI, &RI).isValid())
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000203 NewDefs[*SRI].push_back(NewSense::Def(
204 PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI),
205 HexagonMCInstrInfo::isFloat(MCII, MCI)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000206 }
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 Parzyszek8cdfe8e2017-02-06 19:35:46 +0000226 NewUses[N] = NewSense::Jmp(
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000227 llvm::HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeNCJ);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000228 else
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000229 NewUses[N] = NewSense::Use(
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000230 PredReg, HexagonMCInstrInfo::isPredicatedTrue(MCII, MCI));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000231 }
232 }
233}
234
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000235HexagonMCChecker::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 LeMahieu7cd08922015-11-09 04:07:48 +0000240 init();
241}
242
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000243bool HexagonMCChecker::check(bool FullCheck) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000244 bool chkB = checkBranches();
245 bool chkP = checkPredicates();
246 bool chkNV = checkNewValues();
247 bool chkR = checkRegisters();
Krzysztof Parzyszek55db4832017-05-01 20:10:41 +0000248 bool chkRRO = checkRegistersReadOnly();
Krzysztof Parzyszekc15f8d22017-05-02 17:56:11 +0000249 bool chkELB = checkEndloopBranches();
Krzysztof Parzyszek107f82d2017-05-02 17:51:14 +0000250 checkRegisterCurDefs();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000251 bool chkS = checkSolo();
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000252 bool chkSh = true;
253 if (FullCheck)
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000254 chkSh = checkShuffle();
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000255 bool chkSl = true;
256 if (FullCheck)
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000257 chkSl = checkSlots();
Krzysztof Parzyszekc15f8d22017-05-02 17:56:11 +0000258 bool chk = chkB && chkP && chkNV && chkR && chkRRO && chkELB && chkS &&
259 chkSh && chkSl;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000260
261 return chk;
262}
263
Krzysztof Parzyszekc15f8d22017-05-02 17:56:11 +0000264bool HexagonMCChecker::checkEndloopBranches() {
265 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
266 MCInstrDesc const &Desc = HexagonMCInstrInfo::getDesc(MCII, I);
267 if (Desc.isBranch() || Desc.isCall()) {
268 auto Inner = HexagonMCInstrInfo::isInnerLoop(MCB);
269 if (Inner || HexagonMCInstrInfo::isOuterLoop(MCB)) {
270 reportError(I.getLoc(),
271 llvm::Twine("packet marked with `:endloop") +
272 (Inner ? "0" : "1") + "' " +
273 "cannot contain instructions that modify register " +
274 "`" + llvm::Twine(RI.getName(Hexagon::PC)) + "'");
275 return false;
276 }
277 }
278 }
279 return true;
280}
281
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000282bool HexagonMCChecker::checkSlots() {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000283 unsigned slotsUsed = 0;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000284 for (auto HMI : HexagonMCInstrInfo::bundleInstructions(MCB)) {
285 MCInst const &MCI = *HMI.getInst();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000286 if (HexagonMCInstrInfo::isImmext(MCI))
287 continue;
288 if (HexagonMCInstrInfo::isDuplex(MCII, MCI))
289 slotsUsed += 2;
290 else
291 ++slotsUsed;
292 }
293
294 if (slotsUsed > HEXAGON_PACKET_SIZE) {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000295 reportError("invalid instruction packet: out of slots");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000296 return false;
297 }
298 return true;
299}
300
301// Check legal use of branches.
302bool HexagonMCChecker::checkBranches() {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000303 if (HexagonMCInstrInfo::isBundle(MCB)) {
304 bool hasConditional = false;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000305 unsigned Branches = 0, Conditional = HEXAGON_PRESHUFFLE_PACKET_SIZE,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000306 Unconditional = HEXAGON_PRESHUFFLE_PACKET_SIZE;
307
308 for (unsigned i = HexagonMCInstrInfo::bundleInstructionsOffset;
309 i < MCB.size(); ++i) {
310 MCInst const &MCI = *MCB.begin()[i].getInst();
311
312 if (HexagonMCInstrInfo::isImmext(MCI))
313 continue;
314 if (HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch() ||
315 HexagonMCInstrInfo::getDesc(MCII, MCI).isCall()) {
316 ++Branches;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000317 if (HexagonMCInstrInfo::isPredicated(MCII, MCI) ||
318 HexagonMCInstrInfo::isPredicatedNew(MCII, MCI)) {
319 hasConditional = true;
320 Conditional = i; // Record the position of the conditional branch.
321 } else {
322 Unconditional = i; // Record the position of the unconditional branch.
323 }
324 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000325 }
326
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000327 if (Branches > 1)
328 if (!hasConditional || Conditional > Unconditional) {
329 // Error out if more than one unconditional branch or
330 // the conditional branch appears after the unconditional one.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000331 reportError(
332 "unconditional branch cannot precede another branch in packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000333 return false;
334 }
335 }
336
337 return true;
338}
339
340// Check legal use of predicate registers.
341bool HexagonMCChecker::checkPredicates() {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000342 // Check for proper use of new predicate registers.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000343 for (const auto &I : NewPreds) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000344 unsigned P = I;
345
346 if (!Defs.count(P) || LatePreds.count(P)) {
347 // Error out if the new predicate register is not defined,
348 // or defined "late"
349 // (e.g., "{ if (p3.new)... ; p3 = sp1loop0(#r7:2, Rs) }").
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000350 reportErrorNewValue(P);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000351 return false;
352 }
353 }
354
355 // Check for proper use of auto-anded of predicate registers.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000356 for (const auto &I : LatePreds) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000357 unsigned P = I;
358
359 if (LatePreds.count(P) > 1 || Defs.count(P)) {
360 // Error out if predicate register defined "late" multiple times or
361 // defined late and regularly defined
362 // (e.g., "{ p3 = sp1loop0(...); p3 = cmp.eq(...) }".
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000363 reportErrorRegisters(P);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000364 return false;
365 }
366 }
367
368 return true;
369}
370
371// Check legal use of new values.
372bool HexagonMCChecker::checkNewValues() {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000373 for (auto &I : NewUses) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000374 unsigned R = I.first;
375 NewSense &US = I.second;
376
377 if (!hasValidNewValueDef(US, NewDefs[R])) {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000378 reportErrorNewValue(R);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000379 return false;
380 }
381 }
382
383 return true;
384}
385
Krzysztof Parzyszek55db4832017-05-01 20:10:41 +0000386bool HexagonMCChecker::checkRegistersReadOnly() {
387 for (auto I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
388 MCInst const &Inst = *I.getInst();
389 unsigned Defs = HexagonMCInstrInfo::getDesc(MCII, Inst).getNumDefs();
390 for (unsigned j = 0; j < Defs; ++j) {
391 MCOperand const &Operand = Inst.getOperand(j);
392 assert(Operand.isReg() && "Def is not a register");
393 unsigned Register = Operand.getReg();
394 if (ReadOnly.find(Register) != ReadOnly.end()) {
395 reportError(Inst.getLoc(), "Cannot write to read-only register `" +
396 llvm::Twine(RI.getName(Register)) + "'");
397 return false;
398 }
399 }
400 }
401 return true;
402}
403
Krzysztof Parzyszek107f82d2017-05-02 17:51:14 +0000404bool HexagonMCChecker::registerUsed(unsigned Register) {
Krzysztof Parzyszekc15f8d22017-05-02 17:56:11 +0000405 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB))
406 for (unsigned j = HexagonMCInstrInfo::getDesc(MCII, I).getNumDefs(),
407 n = I.getNumOperands();
408 j < n; ++j) {
409 MCOperand const &Operand = I.getOperand(j);
410 if (Operand.isReg() && Operand.getReg() == Register)
411 return true;
412 }
Krzysztof Parzyszek1cc6bfb2017-05-02 17:53:51 +0000413 return false;
Krzysztof Parzyszek107f82d2017-05-02 17:51:14 +0000414}
415
416void HexagonMCChecker::checkRegisterCurDefs() {
Krzysztof Parzyszekc15f8d22017-05-02 17:56:11 +0000417 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
418 if (HexagonMCInstrInfo::isCVINew(MCII, I) &&
419 HexagonMCInstrInfo::getDesc(MCII, I).mayLoad()) {
420 unsigned Register = I.getOperand(0).getReg();
Krzysztof Parzyszek107f82d2017-05-02 17:51:14 +0000421 if (!registerUsed(Register))
422 reportWarning("Register `" + llvm::Twine(RI.getName(Register)) +
423 "' used with `.cur' "
424 "but not used in the same packet");
425 }
426 }
427}
428
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000429// Check for legal register uses and definitions.
430bool HexagonMCChecker::checkRegisters() {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000431 // Check for proper register definitions.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000432 for (const auto &I : Defs) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000433 unsigned R = I.first;
434
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000435 if (isLoopRegister(R) && Defs.count(R) > 1 &&
436 (HexagonMCInstrInfo::isInnerLoop(MCB) ||
437 HexagonMCInstrInfo::isOuterLoop(MCB))) {
438 // Error out for definitions of loop registers at the end of a loop.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000439 reportError("loop-setup and some branch instructions "
440 "cannot be in the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000441 return false;
442 }
443 if (SoftDefs.count(R)) {
444 // Error out for explicit changes to registers also weakly defined
445 // (e.g., "{ usr = r0; r0 = sfadd(...) }").
446 unsigned UsrR = Hexagon::USR; // Silence warning about mixed types in ?:.
447 unsigned BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000448 reportErrorRegisters(BadR);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000449 return false;
450 }
451 if (!isPredicateRegister(R) && Defs[R].size() > 1) {
452 // Check for multiple register definitions.
453 PredSet &PM = Defs[R];
454
455 // Check for multiple unconditional register definitions.
456 if (PM.count(Unconditional)) {
457 // Error out on an unconditional change when there are any other
458 // changes, conditional or not.
459 unsigned UsrR = Hexagon::USR;
460 unsigned BadR = RI.isSubRegister(Hexagon::USR, R) ? UsrR : R;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000461 reportErrorRegisters(BadR);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000462 return false;
463 }
464 // Check for multiple conditional register definitions.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000465 for (const auto &J : PM) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000466 PredSense P = J;
467
468 // Check for multiple uses of the same condition.
469 if (PM.count(P) > 1) {
470 // Error out on conditional changes based on the same predicate
471 // (e.g., "{ if (!p0) r0 =...; if (!p0) r0 =... }").
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000472 reportErrorRegisters(R);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000473 return false;
474 }
475 // Check for the use of the complementary condition.
476 P.second = !P.second;
477 if (PM.count(P) && PM.size() > 2) {
478 // Error out on conditional changes based on the same predicate
479 // multiple times
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000480 // (e.g., "{ if (p0) r0 =...; if (!p0) r0 =... }; if (!p0) r0 =...
481 // }").
482 reportErrorRegisters(R);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000483 return false;
484 }
485 }
486 }
487 }
488
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000489 // Check for use of temporary definitions.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000490 for (const auto &I : TmpDefs) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000491 unsigned R = I;
492
493 if (!Uses.count(R)) {
494 // special case for vhist
495 bool vHistFound = false;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000496 for (auto const &HMI : HexagonMCInstrInfo::bundleInstructions(MCB)) {
497 if (llvm::HexagonMCInstrInfo::getType(MCII, *HMI.getInst()) ==
498 HexagonII::TypeCVI_HIST) {
499 vHistFound = true; // vhist() implicitly uses ALL REGxx.tmp
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000500 break;
501 }
502 }
503 // Warn on an unused temporary definition.
504 if (vHistFound == false) {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000505 reportWarning("register `" + llvm::Twine(RI.getName(R)) +
506 "' used with `.tmp' "
507 "but not used in the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000508 return true;
509 }
510 }
511 }
512
513 return true;
514}
515
516// Check for legal use of solo insns.
517bool HexagonMCChecker::checkSolo() {
Krzysztof Parzyszeke96d27a2017-05-01 20:06:01 +0000518 if (HexagonMCInstrInfo::bundleSize(MCB) > 1)
Krzysztof Parzyszekc15f8d22017-05-02 17:56:11 +0000519 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCII, MCB)) {
520 if (llvm::HexagonMCInstrInfo::isSolo(MCII, I)) {
521 reportError(I.getLoc(), "Instruction is marked `isSolo' and "
522 "cannot have other instructions in "
523 "the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000524 return false;
525 }
526 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000527
528 return true;
529}
530
531bool HexagonMCChecker::checkShuffle() {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000532 HexagonMCShuffler MCSDX(Context, ReportErrors, MCII, STI, MCB);
533 return MCSDX.check();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000534}
535
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000536void HexagonMCChecker::compoundRegisterMap(unsigned &Register) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000537 switch (Register) {
538 default:
539 break;
540 case Hexagon::R15:
541 Register = Hexagon::R23;
542 break;
543 case Hexagon::R14:
544 Register = Hexagon::R22;
545 break;
546 case Hexagon::R13:
547 Register = Hexagon::R21;
548 break;
549 case Hexagon::R12:
550 Register = Hexagon::R20;
551 break;
552 case Hexagon::R11:
553 Register = Hexagon::R19;
554 break;
555 case Hexagon::R10:
556 Register = Hexagon::R18;
557 break;
558 case Hexagon::R9:
559 Register = Hexagon::R17;
560 break;
561 case Hexagon::R8:
562 Register = Hexagon::R16;
563 break;
564 }
565}
566
567bool HexagonMCChecker::hasValidNewValueDef(const NewSense &Use,
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000568 const NewSenseList &Defs) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000569 bool Strict = !RelaxNVChecks;
570
571 for (unsigned i = 0, n = Defs.size(); i < n; ++i) {
572 const NewSense &Def = Defs[i];
573 // NVJ cannot use a new FP value [7.6.1]
574 if (Use.IsNVJ && (Def.IsFloat || Def.PredReg != 0))
575 continue;
576 // If the definition was not predicated, then it does not matter if
577 // the use is.
578 if (Def.PredReg == 0)
579 return true;
580 // With the strict checks, both the definition and the use must be
581 // predicated on the same register and condition.
582 if (Strict) {
583 if (Def.PredReg == Use.PredReg && Def.Cond == Use.Cond)
584 return true;
585 } else {
586 // With the relaxed checks, if the definition was predicated, the only
587 // detectable violation is if the use is predicated on the opposing
588 // condition, otherwise, it's ok.
589 if (Def.PredReg != Use.PredReg || Def.Cond == Use.Cond)
590 return true;
591 }
592 }
593 return false;
594}
595
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000596void HexagonMCChecker::reportErrorRegisters(unsigned Register) {
597 reportError("register `" + llvm::Twine(RI.getName(Register)) +
598 "' modified more than once");
599}
600
601void HexagonMCChecker::reportErrorNewValue(unsigned Register) {
602 reportError("register `" + llvm::Twine(RI.getName(Register)) +
603 "' used with `.new' "
604 "but not validly modified in the same packet");
605}
606
607void HexagonMCChecker::reportError(llvm::Twine const &Msg) {
Krzysztof Parzyszeke96d27a2017-05-01 20:06:01 +0000608 reportError(MCB.getLoc(), Msg);
609}
610
611void HexagonMCChecker::reportError(SMLoc Loc, llvm::Twine const &Msg) {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000612 if (ReportErrors)
Krzysztof Parzyszeke96d27a2017-05-01 20:06:01 +0000613 Context.reportError(Loc, Msg);
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000614}
615
616void HexagonMCChecker::reportWarning(llvm::Twine const &Msg) {
617 if (ReportErrors) {
618 auto SM = Context.getSourceManager();
619 if (SM)
620 SM->PrintMessage(MCB.getLoc(), SourceMgr::DK_Warning, Msg);
621 }
622}