blob: f8b2a52cd5004a5a77cba48c568c8d877428c9cf [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 Parzyszek107f82d2017-05-02 17:51:14 +0000249 checkRegisterCurDefs();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000250 bool chkS = checkSolo();
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000251 bool chkSh = true;
252 if (FullCheck)
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000253 chkSh = checkShuffle();
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000254 bool chkSl = true;
255 if (FullCheck)
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000256 chkSl = checkSlots();
Krzysztof Parzyszek55db4832017-05-01 20:10:41 +0000257 bool chk = chkB && chkP && chkNV && chkR && chkRRO && chkS && chkSh && chkSl;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000258
259 return chk;
260}
261
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000262bool HexagonMCChecker::checkSlots() {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000263 unsigned slotsUsed = 0;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000264 for (auto HMI : HexagonMCInstrInfo::bundleInstructions(MCB)) {
265 MCInst const &MCI = *HMI.getInst();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000266 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000275 reportError("invalid instruction packet: out of slots");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000276 return false;
277 }
278 return true;
279}
280
281// Check legal use of branches.
282bool HexagonMCChecker::checkBranches() {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000283 if (HexagonMCInstrInfo::isBundle(MCB)) {
284 bool hasConditional = false;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000285 unsigned Branches = 0, Conditional = HEXAGON_PRESHUFFLE_PACKET_SIZE,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000286 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 LeMahieu7cd08922015-11-09 04:07:48 +0000297 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 LeMahieu7cd08922015-11-09 04:07:48 +0000305 }
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 Parzyszeke12d1e72017-05-01 19:41:43 +0000311 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 LeMahieu7cd08922015-11-09 04:07:48 +0000315 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000321 reportError(
322 "unconditional branch cannot precede another branch in packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000323 return false;
324 }
325 }
326
327 return true;
328}
329
330// Check legal use of predicate registers.
331bool HexagonMCChecker::checkPredicates() {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000332 // Check for proper use of new predicate registers.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000333 for (const auto &I : NewPreds) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000334 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000340 reportErrorNewValue(P);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000341 return false;
342 }
343 }
344
345 // Check for proper use of auto-anded of predicate registers.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000346 for (const auto &I : LatePreds) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000347 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000353 reportErrorRegisters(P);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000354 return false;
355 }
356 }
357
358 return true;
359}
360
361// Check legal use of new values.
362bool HexagonMCChecker::checkNewValues() {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000363 for (auto &I : NewUses) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000364 unsigned R = I.first;
365 NewSense &US = I.second;
366
367 if (!hasValidNewValueDef(US, NewDefs[R])) {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000368 reportErrorNewValue(R);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000369 return false;
370 }
371 }
372
373 return true;
374}
375
Krzysztof Parzyszek55db4832017-05-01 20:10:41 +0000376bool 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 Parzyszek107f82d2017-05-02 17:51:14 +0000394bool HexagonMCChecker::registerUsed(MCInst const &Inst, unsigned Register) {
Krzysztof Parzyszek1cc6bfb2017-05-02 17:53:51 +0000395 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 Parzyszek107f82d2017-05-02 17:51:14 +0000399 return true;
Krzysztof Parzyszek107f82d2017-05-02 17:51:14 +0000400 }
401 return false;
402}
403
404bool HexagonMCChecker::registerUsed(unsigned Register) {
Krzysztof Parzyszek1cc6bfb2017-05-02 17:53:51 +0000405 for (auto const &I: HexagonMCInstrInfo::bundleInstructions(MCII, MCB))
406 if (registerUsed(I, Register))
407 return true;
408 return false;
Krzysztof Parzyszek107f82d2017-05-02 17:51:14 +0000409}
410
411void 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 LeMahieu7cd08922015-11-09 04:07:48 +0000425// Check for legal register uses and definitions.
426bool HexagonMCChecker::checkRegisters() {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000427 // Check for proper register definitions.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000428 for (const auto &I : Defs) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000429 unsigned R = I.first;
430
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000431 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000435 reportError("loop-setup and some branch instructions "
436 "cannot be in the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000437 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000444 reportErrorRegisters(BadR);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000445 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000457 reportErrorRegisters(BadR);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000458 return false;
459 }
460 // Check for multiple conditional register definitions.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000461 for (const auto &J : PM) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000462 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000468 reportErrorRegisters(R);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000469 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000476 // (e.g., "{ if (p0) r0 =...; if (!p0) r0 =... }; if (!p0) r0 =...
477 // }").
478 reportErrorRegisters(R);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000479 return false;
480 }
481 }
482 }
483 }
484
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000485 // Check for use of temporary definitions.
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000486 for (const auto &I : TmpDefs) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000487 unsigned R = I;
488
489 if (!Uses.count(R)) {
490 // special case for vhist
491 bool vHistFound = false;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000492 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 LeMahieu7cd08922015-11-09 04:07:48 +0000496 break;
497 }
498 }
499 // Warn on an unused temporary definition.
500 if (vHistFound == false) {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000501 reportWarning("register `" + llvm::Twine(RI.getName(R)) +
502 "' used with `.tmp' "
503 "but not used in the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000504 return true;
505 }
506 }
507 }
508
509 return true;
510}
511
512// Check for legal use of solo insns.
513bool HexagonMCChecker::checkSolo() {
Krzysztof Parzyszeke96d27a2017-05-01 20:06:01 +0000514 if (HexagonMCInstrInfo::bundleSize(MCB) > 1)
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000515 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000516 if (llvm::HexagonMCInstrInfo::isSolo(MCII, *I.getInst())) {
Krzysztof Parzyszeke96d27a2017-05-01 20:06:01 +0000517 SMLoc Loc = I.getInst()->getLoc();
518 reportError(Loc, "Instruction is marked `isSolo' and "
519 "cannot have other instructions in "
520 "the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000521 return false;
522 }
523 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000524
525 return true;
526}
527
528bool HexagonMCChecker::checkShuffle() {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000529 HexagonMCShuffler MCSDX(Context, ReportErrors, MCII, STI, MCB);
530 return MCSDX.check();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000531}
532
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000533void HexagonMCChecker::compoundRegisterMap(unsigned &Register) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000534 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
564bool HexagonMCChecker::hasValidNewValueDef(const NewSense &Use,
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000565 const NewSenseList &Defs) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000566 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 Parzyszeke12d1e72017-05-01 19:41:43 +0000593void HexagonMCChecker::reportErrorRegisters(unsigned Register) {
594 reportError("register `" + llvm::Twine(RI.getName(Register)) +
595 "' modified more than once");
596}
597
598void 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
604void HexagonMCChecker::reportError(llvm::Twine const &Msg) {
Krzysztof Parzyszeke96d27a2017-05-01 20:06:01 +0000605 reportError(MCB.getLoc(), Msg);
606}
607
608void HexagonMCChecker::reportError(SMLoc Loc, llvm::Twine const &Msg) {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000609 if (ReportErrors)
Krzysztof Parzyszeke96d27a2017-05-01 20:06:01 +0000610 Context.reportError(Loc, Msg);
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000611}
612
613void 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}