LLVM 24.0.0git
HexagonBitTracker.cpp
Go to the documentation of this file.
1//===- HexagonBitTracker.cpp ----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "HexagonBitTracker.h"
10#include "HexagonInstrInfo.h"
11#include "HexagonRegisterInfo.h"
12#include "HexagonSubtarget.h"
18#include "llvm/IR/Argument.h"
19#include "llvm/IR/Attributes.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/Type.h"
23#include "llvm/Support/Debug.h"
25
26using namespace llvm;
27
28using BT = BitTracker;
29
32 const HexagonInstrInfo &tii,
34 : MachineEvaluator(tri, mri), MF(mf), MFI(mf.getFrameInfo()), TII(tii) {
35 // Populate the VRX map (VR to extension-type).
36 // Go over all the formal parameters of the function. If a given parameter
37 // P is sign- or zero-extended, locate the virtual register holding that
38 // parameter and create an entry in the VRX map indicating the type of ex-
39 // tension (and the source type).
40 // This is a bit complicated to do accurately, since the memory layout in-
41 // formation is necessary to precisely determine whether an aggregate para-
42 // meter will be passed in a register or in memory. What is given in MRI
43 // is the association between the physical register that is live-in (i.e.
44 // holds an argument), and the virtual register that this value will be
45 // copied into. This, by itself, is not sufficient to map back the virtual
46 // register to a formal parameter from Function (since consecutive live-ins
47 // from MRI may not correspond to consecutive formal parameters from Func-
48 // tion). To avoid the complications with in-memory arguments, only consi-
49 // der the initial sequence of formal parameters that are known to be
50 // passed via registers.
51 unsigned InVirtReg, InPhysReg = 0;
52
53 for (const Argument &Arg : MF.getFunction().args()) {
54 Type *ATy = Arg.getType();
55 unsigned Width = 0;
56 if (ATy->isIntegerTy())
57 Width = ATy->getIntegerBitWidth();
58 else if (ATy->isPointerTy())
59 Width = 32;
60 // If pointer size is not set through target data, it will default to
61 // Module::AnyPointerSize.
62 if (Width == 0 || Width > 64)
63 break;
64 if (Arg.hasAttribute(Attribute::ByVal))
65 continue;
66 InPhysReg = getNextPhysReg(InPhysReg, Width);
67 if (!InPhysReg)
68 break;
69 InVirtReg = getVirtRegFor(InPhysReg);
70 if (!InVirtReg)
71 continue;
72 if (Arg.hasAttribute(Attribute::SExt))
73 VRX.insert(std::make_pair(InVirtReg, ExtType(ExtType::SExt, Width)));
74 else if (Arg.hasAttribute(Attribute::ZExt))
75 VRX.insert(std::make_pair(InVirtReg, ExtType(ExtType::ZExt, Width)));
76 }
77}
78
80 if (Sub == 0)
81 return MachineEvaluator::mask(Reg, 0);
82 const TargetRegisterClass &RC = *MRI.getRegClass(Reg);
83 uint16_t RW = getRegBitWidth(RegisterRef(Reg, Sub));
84 const auto &HRI = static_cast<const HexagonRegisterInfo&>(TRI);
85 bool IsSubLo = (Sub == HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo));
86 if (Hexagon::DoubleRegsRegClass.hasSubClassEq(&RC) ||
87 Hexagon::HvxWRRegClass.hasSubClassEq(&RC) ||
88 Hexagon::HvxVQRRegClass.hasSubClassEq(&RC) ||
89 Hexagon::CircRegsRegClass.hasSubClassEq(&RC))
90 return IsSubLo ? BT::BitMask(0, RW - 1) : BT::BitMask(RW, 2 * RW - 1);
91#ifndef NDEBUG
92 dbgs() << printReg(Reg, &TRI, Sub) << " in reg class "
93 << TRI.getRegClassName(&RC) << '\n';
94#endif
95 llvm_unreachable("Unexpected register/subregister");
96}
97
99 using namespace Hexagon;
100 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
101 if (HST.useHVXOps()) {
102 for (auto *RC :
103 {&HvxVRRegClass, &HvxWRRegClass, &HvxQRRegClass, &HvxVQRRegClass})
104 if (RC->contains(Reg))
105 return TRI.getRegSizeInBits(*RC);
106 }
107 // Default treatment for other physical registers.
108 if (const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg))
109 return TRI.getRegSizeInBits(*RC);
110
112 (Twine("Unhandled physical register") + TRI.getName(Reg)).str().c_str());
113}
114
116 const TargetRegisterClass &RC, unsigned Idx) const {
117 if (Idx == 0)
118 return RC;
119
120#ifndef NDEBUG
121 const auto &HRI = static_cast<const HexagonRegisterInfo&>(TRI);
122 bool IsSubLo = (Idx == HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo));
123 bool IsSubHi = (Idx == HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi));
124 assert(IsSubLo != IsSubHi && "Must refer to either low or high subreg");
125#endif
126
127 if (Hexagon::DoubleRegsRegClass.hasSubClassEq(&RC) ||
128 Hexagon::CircRegsRegClass.hasSubClassEq(&RC))
129 return Hexagon::IntRegsRegClass;
130 if (Hexagon::HvxWRRegClass.hasSubClassEq(&RC))
131 return Hexagon::HvxVRRegClass;
132 if (Hexagon::HvxVQRRegClass.hasSubClassEq(&RC))
133 return Hexagon::HvxWRRegClass;
134#ifndef NDEBUG
135 dbgs() << "Reg class id: " << RC.getID() << " idx: " << Idx << '\n';
136#endif
137 llvm_unreachable("Unimplemented combination of reg class/subreg idx");
138}
139
140namespace {
141
142class RegisterRefs {
143 std::vector<BT::RegisterRef> Vector;
144
145public:
146 RegisterRefs(const MachineInstr &MI) : Vector(MI.getNumOperands()) {
147 for (unsigned i = 0, n = Vector.size(); i < n; ++i) {
148 const MachineOperand &MO = MI.getOperand(i);
149 if (MO.isReg())
150 Vector[i] = BT::RegisterRef(MO);
151 // For indices that don't correspond to registers, the entry will
152 // remain constructed via the default constructor.
153 }
154 }
155
156 size_t size() const { return Vector.size(); }
157
158 const BT::RegisterRef &operator[](unsigned n) const {
159 // The main purpose of this operator is to assert with bad argument.
160 assert(n < Vector.size());
161 return Vector[n];
162 }
163};
164
165} // end anonymous namespace
166
168 const CellMapType &Inputs,
169 CellMapType &Outputs) const {
170 using namespace Hexagon;
171
172 unsigned NumDefs = 0;
173
174 // Basic correctness check: there should not be any defs with subregisters.
175 for (const MachineOperand &MO : MI.operands()) {
176 if (!MO.isReg() || !MO.isDef())
177 continue;
178 NumDefs++;
179 assert(MO.getSubReg() == 0);
180 }
181
182 if (NumDefs == 0)
183 return false;
184
185 unsigned Opc = MI.getOpcode();
186
187 if (MI.mayLoad()) {
188 switch (Opc) {
189 // These instructions may be marked as mayLoad, but they are generating
190 // immediate values, so skip them.
191 case CONST32:
192 case CONST64:
193 break;
194 default:
195 return evaluateLoad(MI, Inputs, Outputs);
196 }
197 }
198
199 // Check COPY instructions that copy formal parameters into virtual
200 // registers. Such parameters can be sign- or zero-extended at the
201 // call site, and we should take advantage of this knowledge. The MRI
202 // keeps a list of pairs of live-in physical and virtual registers,
203 // which provides information about which virtual registers will hold
204 // the argument values. The function will still contain instructions
205 // defining those virtual registers, and in practice those are COPY
206 // instructions from a physical to a virtual register. In such cases,
207 // applying the argument extension to the virtual register can be seen
208 // as simply mirroring the extension that had already been applied to
209 // the physical register at the call site. If the defining instruction
210 // was not a COPY, it would not be clear how to mirror that extension
211 // on the callee's side. For that reason, only check COPY instructions
212 // for potential extensions.
213 if (MI.isCopy()) {
214 if (evaluateFormalCopy(MI, Inputs, Outputs))
215 return true;
216 }
217
218 // Beyond this point, if any operand is a global, skip that instruction.
219 // The reason is that certain instructions that can take an immediate
220 // operand can also have a global symbol in that operand. To avoid
221 // checking what kind of operand a given instruction has individually
222 // for each instruction, do it here. Global symbols as operands gene-
223 // rally do not provide any useful information.
224 for (const MachineOperand &MO : MI.operands()) {
225 if (MO.isGlobal() || MO.isBlockAddress() || MO.isSymbol() || MO.isJTI() ||
226 MO.isCPI())
227 return false;
228 }
229
230 RegisterRefs Reg(MI);
231#define op(i) MI.getOperand(i)
232#define rc(i) RegisterCell::ref(getCell(Reg[i], Inputs))
233#define im(i) MI.getOperand(i).getImm()
234
235 // If the instruction has no register operands, skip it.
236 if (Reg.size() == 0)
237 return false;
238
239 // Record result for register in operand 0.
240 auto rr0 = [this,Reg] (const BT::RegisterCell &Val, CellMapType &Outputs)
241 -> bool {
242 putCell(Reg[0], Val, Outputs);
243 return true;
244 };
245 // Get the cell corresponding to the N-th operand.
246 auto cop = [this, &Reg, &MI, &Inputs](unsigned N,
247 uint16_t W) -> BT::RegisterCell {
248 const MachineOperand &Op = MI.getOperand(N);
249 if (Op.isImm())
250 return eIMM(Op.getImm(), W);
251 if (!Op.isReg())
252 return RegisterCell::self(0, W);
253 assert(getRegBitWidth(Reg[N]) == W && "Register width mismatch");
254 return rc(N);
255 };
256 // Extract RW low bits of the cell.
257 auto lo = [this] (const BT::RegisterCell &RC, uint16_t RW)
259 assert(RW <= RC.width());
260 return eXTR(RC, 0, RW);
261 };
262 // Extract RW high bits of the cell.
263 auto hi = [this] (const BT::RegisterCell &RC, uint16_t RW)
265 uint16_t W = RC.width();
266 assert(RW <= W);
267 return eXTR(RC, W-RW, W);
268 };
269 // Extract N-th halfword (counting from the least significant position).
270 auto half = [this] (const BT::RegisterCell &RC, unsigned N)
272 assert(N*16+16 <= RC.width());
273 return eXTR(RC, N*16, N*16+16);
274 };
275 // Shuffle bits (pick even/odd from cells and merge into result).
276 auto shuffle = [this] (const BT::RegisterCell &Rs, const BT::RegisterCell &Rt,
277 uint16_t BW, bool Odd) -> BT::RegisterCell {
278 uint16_t I = Odd, Ws = Rs.width();
279 assert(Ws == Rt.width());
280 RegisterCell RC = eXTR(Rt, I*BW, I*BW+BW).cat(eXTR(Rs, I*BW, I*BW+BW));
281 I += 2;
282 while (I*BW < Ws) {
283 RC.cat(eXTR(Rt, I*BW, I*BW+BW)).cat(eXTR(Rs, I*BW, I*BW+BW));
284 I += 2;
285 }
286 return RC;
287 };
288
289 // The bitwidth of the 0th operand. In most (if not all) of the
290 // instructions below, the 0th operand is the defined register.
291 // Pre-compute the bitwidth here, because it is needed in many cases
292 // cases below.
293 uint16_t W0 = (Reg[0].Reg != 0) ? getRegBitWidth(Reg[0]) : 0;
294
295 // Register id of the 0th operand. It can be 0.
296 unsigned Reg0 = Reg[0].Reg;
297
298 switch (Opc) {
299 // Transfer immediate:
300
301 case A2_tfrsi:
302 case A2_tfrpi:
303 case CONST32:
304 case CONST64:
305 return rr0(eIMM(im(1), W0), Outputs);
306 case PS_false:
307 return rr0(RegisterCell(W0).fill(0, W0, BT::BitValue::Zero), Outputs);
308 case PS_true:
309 return rr0(RegisterCell(W0).fill(0, W0, BT::BitValue::One), Outputs);
310 case PS_fi: {
311 int FI = op(1).getIndex();
312 int Off = op(2).getImm();
313 unsigned A = MFI.getObjectAlign(FI).value() + std::abs(Off);
314 unsigned L = llvm::countr_zero(A);
315 RegisterCell RC = RegisterCell::self(Reg[0].Reg, W0);
316 RC.fill(0, L, BT::BitValue::Zero);
317 return rr0(RC, Outputs);
318 }
319
320 // Transfer register:
321
322 case A2_tfr:
323 case A2_tfrp:
324 case C2_pxfer_map:
325 return rr0(rc(1), Outputs);
326 case C2_tfrpr: {
327 uint16_t RW = W0;
328 uint16_t PW = 8; // XXX Pred size: getRegBitWidth(Reg[1]);
329 assert(PW <= RW);
330 RegisterCell PC = eXTR(rc(1), 0, PW);
331 RegisterCell RC = RegisterCell(RW).insert(PC, BT::BitMask(0, PW-1));
332 RC.fill(PW, RW, BT::BitValue::Zero);
333 return rr0(RC, Outputs);
334 }
335 case C2_tfrrp: {
336 uint16_t RW = W0;
337 uint16_t PW = 8; // XXX Pred size: getRegBitWidth(Reg[1]);
338 RegisterCell RC = RegisterCell::self(Reg[0].Reg, RW);
339 RC.fill(PW, RW, BT::BitValue::Zero);
340 return rr0(eINS(RC, eXTR(rc(1), 0, PW), 0), Outputs);
341 }
342
343 // Arithmetic:
344
345 case A2_abs:
346 case A2_absp:
347 // TODO
348 break;
349
350 case A2_addsp: {
351 uint16_t W1 = getRegBitWidth(Reg[1]);
352 assert(W0 == 64 && W1 == 32);
353 RegisterCell CW = RegisterCell(W0).insert(rc(1), BT::BitMask(0, W1-1));
354 RegisterCell RC = eADD(eSXT(CW, W1), rc(2));
355 return rr0(RC, Outputs);
356 }
357 case A2_add:
358 case A2_addp:
359 return rr0(eADD(rc(1), rc(2)), Outputs);
360 case A2_addi:
361 return rr0(eADD(rc(1), eIMM(im(2), W0)), Outputs);
362 case S4_addi_asl_ri: {
363 RegisterCell RC = eADD(eIMM(im(1), W0), eASL(rc(2), im(3)));
364 return rr0(RC, Outputs);
365 }
366 case S4_addi_lsr_ri: {
367 RegisterCell RC = eADD(eIMM(im(1), W0), eLSR(rc(2), im(3)));
368 return rr0(RC, Outputs);
369 }
370 case S4_addaddi: {
371 RegisterCell RC = eADD(rc(1), eADD(rc(2), eIMM(im(3), W0)));
372 return rr0(RC, Outputs);
373 }
374 case M4_mpyri_addi: {
375 RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
376 RegisterCell RC = eADD(eIMM(im(1), W0), lo(M, W0));
377 return rr0(RC, Outputs);
378 }
379 case M4_mpyrr_addi: {
380 RegisterCell M = eMLS(rc(2), rc(3));
381 RegisterCell RC = eADD(eIMM(im(1), W0), lo(M, W0));
382 return rr0(RC, Outputs);
383 }
384 case M4_mpyri_addr_u2: {
385 RegisterCell M = eMLS(eIMM(im(2), W0), rc(3));
386 RegisterCell RC = eADD(rc(1), lo(M, W0));
387 return rr0(RC, Outputs);
388 }
389 case M4_mpyri_addr: {
390 RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
391 RegisterCell RC = eADD(rc(1), lo(M, W0));
392 return rr0(RC, Outputs);
393 }
394 case M4_mpyrr_addr: {
395 RegisterCell M = eMLS(rc(2), rc(3));
396 RegisterCell RC = eADD(rc(1), lo(M, W0));
397 return rr0(RC, Outputs);
398 }
399 case S4_subaddi: {
400 RegisterCell RC = eADD(rc(1), eSUB(eIMM(im(2), W0), rc(3)));
401 return rr0(RC, Outputs);
402 }
403 case M2_accii: {
404 RegisterCell RC = eADD(rc(1), eADD(rc(2), eIMM(im(3), W0)));
405 return rr0(RC, Outputs);
406 }
407 case M2_acci: {
408 RegisterCell RC = eADD(rc(1), eADD(rc(2), rc(3)));
409 return rr0(RC, Outputs);
410 }
411 case M2_subacc: {
412 RegisterCell RC = eADD(rc(1), eSUB(rc(2), rc(3)));
413 return rr0(RC, Outputs);
414 }
415 case S2_addasl_rrri: {
416 RegisterCell RC = eADD(rc(1), eASL(rc(2), im(3)));
417 return rr0(RC, Outputs);
418 }
419 case C4_addipc: {
420 RegisterCell RPC = RegisterCell::self(Reg[0].Reg, W0);
421 RPC.fill(0, 2, BT::BitValue::Zero);
422 return rr0(eADD(RPC, eIMM(im(2), W0)), Outputs);
423 }
424 case A2_sub:
425 case A2_subp:
426 return rr0(eSUB(rc(1), rc(2)), Outputs);
427 case A2_subri:
428 return rr0(eSUB(eIMM(im(1), W0), rc(2)), Outputs);
429 case S4_subi_asl_ri: {
430 RegisterCell RC = eSUB(eIMM(im(1), W0), eASL(rc(2), im(3)));
431 return rr0(RC, Outputs);
432 }
433 case S4_subi_lsr_ri: {
434 RegisterCell RC = eSUB(eIMM(im(1), W0), eLSR(rc(2), im(3)));
435 return rr0(RC, Outputs);
436 }
437 case M2_naccii: {
438 RegisterCell RC = eSUB(rc(1), eADD(rc(2), eIMM(im(3), W0)));
439 return rr0(RC, Outputs);
440 }
441 case M2_nacci: {
442 RegisterCell RC = eSUB(rc(1), eADD(rc(2), rc(3)));
443 return rr0(RC, Outputs);
444 }
445 // 32-bit negation is done by "Rd = A2_subri 0, Rs"
446 case A2_negp:
447 return rr0(eSUB(eIMM(0, W0), rc(1)), Outputs);
448
449 case M2_mpy_up: {
450 RegisterCell M = eMLS(rc(1), rc(2));
451 return rr0(hi(M, W0), Outputs);
452 }
453 case M2_dpmpyss_s0:
454 return rr0(eMLS(rc(1), rc(2)), Outputs);
455 case M2_dpmpyss_acc_s0:
456 return rr0(eADD(rc(1), eMLS(rc(2), rc(3))), Outputs);
457 case M2_dpmpyss_nac_s0:
458 return rr0(eSUB(rc(1), eMLS(rc(2), rc(3))), Outputs);
459 case M2_mpyi: {
460 RegisterCell M = eMLS(rc(1), rc(2));
461 return rr0(lo(M, W0), Outputs);
462 }
463 case M2_macsip: {
464 RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
465 RegisterCell RC = eADD(rc(1), lo(M, W0));
466 return rr0(RC, Outputs);
467 }
468 case M2_macsin: {
469 RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
470 RegisterCell RC = eSUB(rc(1), lo(M, W0));
471 return rr0(RC, Outputs);
472 }
473 case M2_maci: {
474 RegisterCell M = eMLS(rc(2), rc(3));
475 RegisterCell RC = eADD(rc(1), lo(M, W0));
476 return rr0(RC, Outputs);
477 }
478 case M2_mnaci: {
479 RegisterCell M = eMLS(rc(2), rc(3));
480 RegisterCell RC = eSUB(rc(1), lo(M, W0));
481 return rr0(RC, Outputs);
482 }
483 case M2_mpysmi: {
484 RegisterCell M = eMLS(rc(1), eIMM(im(2), W0));
485 return rr0(lo(M, 32), Outputs);
486 }
487 case M2_mpysin: {
488 RegisterCell M = eMLS(rc(1), eIMM(-im(2), W0));
489 return rr0(lo(M, 32), Outputs);
490 }
491 case M2_mpysip: {
492 RegisterCell M = eMLS(rc(1), eIMM(im(2), W0));
493 return rr0(lo(M, 32), Outputs);
494 }
495 case M2_mpyu_up: {
496 RegisterCell M = eMLU(rc(1), rc(2));
497 return rr0(hi(M, W0), Outputs);
498 }
499 case M2_dpmpyuu_s0:
500 return rr0(eMLU(rc(1), rc(2)), Outputs);
501 case M2_dpmpyuu_acc_s0:
502 return rr0(eADD(rc(1), eMLU(rc(2), rc(3))), Outputs);
503 case M2_dpmpyuu_nac_s0:
504 return rr0(eSUB(rc(1), eMLU(rc(2), rc(3))), Outputs);
505 //case M2_mpysu_up:
506
507 // Logical/bitwise:
508
509 case A2_andir:
510 return rr0(eAND(rc(1), eIMM(im(2), W0)), Outputs);
511 case A2_and:
512 case A2_andp:
513 return rr0(eAND(rc(1), rc(2)), Outputs);
514 case A4_andn:
515 case A4_andnp:
516 return rr0(eAND(rc(1), eNOT(rc(2))), Outputs);
517 case S4_andi_asl_ri: {
518 RegisterCell RC = eAND(eIMM(im(1), W0), eASL(rc(2), im(3)));
519 return rr0(RC, Outputs);
520 }
521 case S4_andi_lsr_ri: {
522 RegisterCell RC = eAND(eIMM(im(1), W0), eLSR(rc(2), im(3)));
523 return rr0(RC, Outputs);
524 }
525 case M4_and_and:
526 return rr0(eAND(rc(1), eAND(rc(2), rc(3))), Outputs);
527 case M4_and_andn:
528 return rr0(eAND(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
529 case M4_and_or:
530 return rr0(eAND(rc(1), eORL(rc(2), rc(3))), Outputs);
531 case M4_and_xor:
532 return rr0(eAND(rc(1), eXOR(rc(2), rc(3))), Outputs);
533 case A2_orir:
534 return rr0(eORL(rc(1), eIMM(im(2), W0)), Outputs);
535 case A2_or:
536 case A2_orp:
537 return rr0(eORL(rc(1), rc(2)), Outputs);
538 case A4_orn:
539 case A4_ornp:
540 return rr0(eORL(rc(1), eNOT(rc(2))), Outputs);
541 case S4_ori_asl_ri: {
542 RegisterCell RC = eORL(eIMM(im(1), W0), eASL(rc(2), im(3)));
543 return rr0(RC, Outputs);
544 }
545 case S4_ori_lsr_ri: {
546 RegisterCell RC = eORL(eIMM(im(1), W0), eLSR(rc(2), im(3)));
547 return rr0(RC, Outputs);
548 }
549 case M4_or_and:
550 return rr0(eORL(rc(1), eAND(rc(2), rc(3))), Outputs);
551 case M4_or_andn:
552 return rr0(eORL(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
553 case S4_or_andi:
554 case S4_or_andix: {
555 RegisterCell RC = eORL(rc(1), eAND(rc(2), eIMM(im(3), W0)));
556 return rr0(RC, Outputs);
557 }
558 case S4_or_ori: {
559 RegisterCell RC = eORL(rc(1), eORL(rc(2), eIMM(im(3), W0)));
560 return rr0(RC, Outputs);
561 }
562 case M4_or_or:
563 return rr0(eORL(rc(1), eORL(rc(2), rc(3))), Outputs);
564 case M4_or_xor:
565 return rr0(eORL(rc(1), eXOR(rc(2), rc(3))), Outputs);
566 case A2_xor:
567 case A2_xorp:
568 return rr0(eXOR(rc(1), rc(2)), Outputs);
569 case M4_xor_and:
570 return rr0(eXOR(rc(1), eAND(rc(2), rc(3))), Outputs);
571 case M4_xor_andn:
572 return rr0(eXOR(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
573 case M4_xor_or:
574 return rr0(eXOR(rc(1), eORL(rc(2), rc(3))), Outputs);
575 case M4_xor_xacc:
576 return rr0(eXOR(rc(1), eXOR(rc(2), rc(3))), Outputs);
577 case A2_not:
578 case A2_notp:
579 return rr0(eNOT(rc(1)), Outputs);
580
581 case S2_asl_i_r:
582 case S2_asl_i_p:
583 return rr0(eASL(rc(1), im(2)), Outputs);
584 case A2_aslh:
585 return rr0(eASL(rc(1), 16), Outputs);
586 case S2_asl_i_r_acc:
587 case S2_asl_i_p_acc:
588 return rr0(eADD(rc(1), eASL(rc(2), im(3))), Outputs);
589 case S2_asl_i_r_nac:
590 case S2_asl_i_p_nac:
591 return rr0(eSUB(rc(1), eASL(rc(2), im(3))), Outputs);
592 case S2_asl_i_r_and:
593 case S2_asl_i_p_and:
594 return rr0(eAND(rc(1), eASL(rc(2), im(3))), Outputs);
595 case S2_asl_i_r_or:
596 case S2_asl_i_p_or:
597 return rr0(eORL(rc(1), eASL(rc(2), im(3))), Outputs);
598 case S2_asl_i_r_xacc:
599 case S2_asl_i_p_xacc:
600 return rr0(eXOR(rc(1), eASL(rc(2), im(3))), Outputs);
601 case S2_asl_i_vh:
602 case S2_asl_i_vw:
603 // TODO
604 break;
605
606 case S2_asr_i_r:
607 case S2_asr_i_p:
608 return rr0(eASR(rc(1), im(2)), Outputs);
609 case A2_asrh:
610 return rr0(eASR(rc(1), 16), Outputs);
611 case S2_asr_i_r_acc:
612 case S2_asr_i_p_acc:
613 return rr0(eADD(rc(1), eASR(rc(2), im(3))), Outputs);
614 case S2_asr_i_r_nac:
615 case S2_asr_i_p_nac:
616 return rr0(eSUB(rc(1), eASR(rc(2), im(3))), Outputs);
617 case S2_asr_i_r_and:
618 case S2_asr_i_p_and:
619 return rr0(eAND(rc(1), eASR(rc(2), im(3))), Outputs);
620 case S2_asr_i_r_or:
621 case S2_asr_i_p_or:
622 return rr0(eORL(rc(1), eASR(rc(2), im(3))), Outputs);
623 case S2_asr_i_r_rnd: {
624 // The input is first sign-extended to 64 bits, then the output
625 // is truncated back to 32 bits.
626 assert(W0 == 32);
627 RegisterCell XC = eSXT(rc(1).cat(eIMM(0, W0)), W0);
628 RegisterCell RC = eASR(eADD(eASR(XC, im(2)), eIMM(1, 2*W0)), 1);
629 return rr0(eXTR(RC, 0, W0), Outputs);
630 }
631 case S2_asr_i_r_rnd_goodsyntax: {
632 int64_t S = im(2);
633 if (S == 0)
634 return rr0(rc(1), Outputs);
635 // Result: S2_asr_i_r_rnd Rs, u5-1
636 RegisterCell XC = eSXT(rc(1).cat(eIMM(0, W0)), W0);
637 RegisterCell RC = eLSR(eADD(eASR(XC, S-1), eIMM(1, 2*W0)), 1);
638 return rr0(eXTR(RC, 0, W0), Outputs);
639 }
640 case S2_asr_r_vh:
641 case S2_asr_i_vw:
642 case S2_asr_i_svw_trun:
643 // TODO
644 break;
645
646 case S2_lsr_i_r:
647 case S2_lsr_i_p:
648 return rr0(eLSR(rc(1), im(2)), Outputs);
649 case S2_lsr_i_r_acc:
650 case S2_lsr_i_p_acc:
651 return rr0(eADD(rc(1), eLSR(rc(2), im(3))), Outputs);
652 case S2_lsr_i_r_nac:
653 case S2_lsr_i_p_nac:
654 return rr0(eSUB(rc(1), eLSR(rc(2), im(3))), Outputs);
655 case S2_lsr_i_r_and:
656 case S2_lsr_i_p_and:
657 return rr0(eAND(rc(1), eLSR(rc(2), im(3))), Outputs);
658 case S2_lsr_i_r_or:
659 case S2_lsr_i_p_or:
660 return rr0(eORL(rc(1), eLSR(rc(2), im(3))), Outputs);
661 case S2_lsr_i_r_xacc:
662 case S2_lsr_i_p_xacc:
663 return rr0(eXOR(rc(1), eLSR(rc(2), im(3))), Outputs);
664
665 case S2_clrbit_i: {
666 RegisterCell RC = rc(1);
667 RC[im(2)] = BT::BitValue::Zero;
668 return rr0(RC, Outputs);
669 }
670 case S2_setbit_i: {
671 RegisterCell RC = rc(1);
672 RC[im(2)] = BT::BitValue::One;
673 return rr0(RC, Outputs);
674 }
675 case S2_togglebit_i: {
676 RegisterCell RC = rc(1);
677 uint16_t BX = im(2);
678 RC[BX] = RC[BX].is(0) ? BT::BitValue::One
679 : RC[BX].is(1) ? BT::BitValue::Zero
680 : BT::BitValue::self();
681 return rr0(RC, Outputs);
682 }
683
684 case A4_bitspliti: {
685 uint16_t W1 = getRegBitWidth(Reg[1]);
686 uint16_t BX = im(2);
687 // Res.uw[1] = Rs[bx+1:], Res.uw[0] = Rs[0:bx]
688 const BT::BitValue Zero = BT::BitValue::Zero;
689 RegisterCell RZ = RegisterCell(W0).fill(BX, W1, Zero)
690 .fill(W1+(W1-BX), W0, Zero);
691 RegisterCell BF1 = eXTR(rc(1), 0, BX), BF2 = eXTR(rc(1), BX, W1);
692 RegisterCell RC = eINS(eINS(RZ, BF1, 0), BF2, W1);
693 return rr0(RC, Outputs);
694 }
695 case S4_extract:
696 case S4_extractp:
697 case S2_extractu:
698 case S2_extractup: {
699 uint16_t Wd = im(2), Of = im(3);
700 assert(Wd <= W0);
701 if (Wd == 0)
702 return rr0(eIMM(0, W0), Outputs);
703 // If the width extends beyond the register size, pad the register
704 // with 0 bits.
705 RegisterCell Pad = (Wd+Of > W0) ? rc(1).cat(eIMM(0, Wd+Of-W0)) : rc(1);
706 RegisterCell Ext = eXTR(Pad, Of, Wd+Of);
707 // Ext is short, need to extend it with 0s or sign bit.
708 RegisterCell RC = RegisterCell(W0).insert(Ext, BT::BitMask(0, Wd-1));
709 if (Opc == S2_extractu || Opc == S2_extractup)
710 return rr0(eZXT(RC, Wd), Outputs);
711 return rr0(eSXT(RC, Wd), Outputs);
712 }
713 case S2_insert:
714 case S2_insertp: {
715 uint16_t Wd = im(3), Of = im(4);
716 assert(Wd < W0 && Of < W0);
717 // If Wd+Of exceeds W0, the inserted bits are truncated.
718 if (Wd+Of > W0)
719 Wd = W0-Of;
720 if (Wd == 0)
721 return rr0(rc(1), Outputs);
722 return rr0(eINS(rc(1), eXTR(rc(2), 0, Wd), Of), Outputs);
723 }
724
725 // Bit permutations:
726
727 case A2_combineii:
728 case A4_combineii:
729 case A4_combineir:
730 case A4_combineri:
731 case A2_combinew:
732 case V6_vcombine:
733 assert(W0 % 2 == 0);
734 return rr0(cop(2, W0/2).cat(cop(1, W0/2)), Outputs);
735 case A2_combine_ll:
736 case A2_combine_lh:
737 case A2_combine_hl:
738 case A2_combine_hh: {
739 assert(W0 == 32);
740 assert(getRegBitWidth(Reg[1]) == 32 && getRegBitWidth(Reg[2]) == 32);
741 // Low half in the output is 0 for _ll and _hl, 1 otherwise:
742 unsigned LoH = !(Opc == A2_combine_ll || Opc == A2_combine_hl);
743 // High half in the output is 0 for _ll and _lh, 1 otherwise:
744 unsigned HiH = !(Opc == A2_combine_ll || Opc == A2_combine_lh);
745 RegisterCell R1 = rc(1);
746 RegisterCell R2 = rc(2);
747 RegisterCell RC = half(R2, LoH).cat(half(R1, HiH));
748 return rr0(RC, Outputs);
749 }
750 case S2_packhl: {
751 assert(W0 == 64);
752 assert(getRegBitWidth(Reg[1]) == 32 && getRegBitWidth(Reg[2]) == 32);
753 RegisterCell R1 = rc(1);
754 RegisterCell R2 = rc(2);
755 RegisterCell RC = half(R2, 0).cat(half(R1, 0)).cat(half(R2, 1))
756 .cat(half(R1, 1));
757 return rr0(RC, Outputs);
758 }
759 case S2_shuffeb: {
760 RegisterCell RC = shuffle(rc(1), rc(2), 8, false);
761 return rr0(RC, Outputs);
762 }
763 case S2_shuffeh: {
764 RegisterCell RC = shuffle(rc(1), rc(2), 16, false);
765 return rr0(RC, Outputs);
766 }
767 case S2_shuffob: {
768 RegisterCell RC = shuffle(rc(1), rc(2), 8, true);
769 return rr0(RC, Outputs);
770 }
771 case S2_shuffoh: {
772 RegisterCell RC = shuffle(rc(1), rc(2), 16, true);
773 return rr0(RC, Outputs);
774 }
775 case C2_mask: {
776 uint16_t WR = W0;
777 uint16_t WP = 8; // XXX Pred size: getRegBitWidth(Reg[1]);
778 assert(WR == 64 && WP == 8);
779 RegisterCell R1 = rc(1);
780 RegisterCell RC(WR);
781 for (uint16_t i = 0; i < WP; ++i) {
782 const BT::BitValue &V = R1[i];
783 BT::BitValue F = (V.is(0) || V.is(1)) ? V : BT::BitValue::self();
784 RC.fill(i*8, i*8+8, F);
785 }
786 return rr0(RC, Outputs);
787 }
788
789 // Mux:
790
791 case C2_muxii:
792 case C2_muxir:
793 case C2_muxri:
794 case C2_mux: {
795 BT::BitValue PC0 = rc(1)[0];
796 RegisterCell R2 = cop(2, W0);
797 RegisterCell R3 = cop(3, W0);
798 if (PC0.is(0) || PC0.is(1))
799 return rr0(RegisterCell::ref(PC0 ? R2 : R3), Outputs);
800 R2.meet(R3, Reg[0].Reg);
801 return rr0(R2, Outputs);
802 }
803 case C2_vmux:
804 // TODO
805 break;
806
807 // Sign- and zero-extension:
808
809 case A2_sxtb:
810 return rr0(eSXT(rc(1), 8), Outputs);
811 case A2_sxth:
812 return rr0(eSXT(rc(1), 16), Outputs);
813 case A2_sxtw: {
814 uint16_t W1 = getRegBitWidth(Reg[1]);
815 assert(W0 == 64 && W1 == 32);
816 RegisterCell RC = eSXT(rc(1).cat(eIMM(0, W1)), W1);
817 return rr0(RC, Outputs);
818 }
819 case A2_zxtb:
820 return rr0(eZXT(rc(1), 8), Outputs);
821 case A2_zxth:
822 return rr0(eZXT(rc(1), 16), Outputs);
823
824 // Saturations
825
826 case A2_satb:
827 return rr0(eSXT(RegisterCell::self(0, W0).regify(Reg0), 8), Outputs);
828 case A2_sath:
829 return rr0(eSXT(RegisterCell::self(0, W0).regify(Reg0), 16), Outputs);
830 case A2_satub:
831 return rr0(eZXT(RegisterCell::self(0, W0).regify(Reg0), 8), Outputs);
832 case A2_satuh:
833 return rr0(eZXT(RegisterCell::self(0, W0).regify(Reg0), 16), Outputs);
834
835 // Bit count:
836
837 case S2_cl0:
838 case S2_cl0p:
839 // Always produce a 32-bit result.
840 return rr0(eCLB(rc(1), false/*bit*/, 32), Outputs);
841 case S2_cl1:
842 case S2_cl1p:
843 return rr0(eCLB(rc(1), true/*bit*/, 32), Outputs);
844 case S2_clb:
845 case S2_clbp: {
846 uint16_t W1 = getRegBitWidth(Reg[1]);
847 RegisterCell R1 = rc(1);
848 BT::BitValue TV = R1[W1-1];
849 if (TV.is(0) || TV.is(1))
850 return rr0(eCLB(R1, TV, 32), Outputs);
851 break;
852 }
853 case S2_ct0:
854 case S2_ct0p:
855 return rr0(eCTB(rc(1), false/*bit*/, 32), Outputs);
856 case S2_ct1:
857 case S2_ct1p:
858 return rr0(eCTB(rc(1), true/*bit*/, 32), Outputs);
859 case S5_popcountp:
860 // TODO
861 break;
862
863 case C2_all8: {
864 RegisterCell P1 = rc(1);
865 bool Has0 = false, All1 = true;
866 for (uint16_t i = 0; i < 8/*XXX*/; ++i) {
867 if (!P1[i].is(1))
868 All1 = false;
869 if (!P1[i].is(0))
870 continue;
871 Has0 = true;
872 break;
873 }
874 if (!Has0 && !All1)
875 break;
876 RegisterCell RC(W0);
877 RC.fill(0, W0, (All1 ? BT::BitValue::One : BT::BitValue::Zero));
878 return rr0(RC, Outputs);
879 }
880 case C2_any8: {
881 RegisterCell P1 = rc(1);
882 bool Has1 = false, All0 = true;
883 for (uint16_t i = 0; i < 8/*XXX*/; ++i) {
884 if (!P1[i].is(0))
885 All0 = false;
886 if (!P1[i].is(1))
887 continue;
888 Has1 = true;
889 break;
890 }
891 if (!Has1 && !All0)
892 break;
893 RegisterCell RC(W0);
894 RC.fill(0, W0, (Has1 ? BT::BitValue::One : BT::BitValue::Zero));
895 return rr0(RC, Outputs);
896 }
897 case C2_and:
898 return rr0(eAND(rc(1), rc(2)), Outputs);
899 case C2_andn:
900 return rr0(eAND(rc(1), eNOT(rc(2))), Outputs);
901 case C2_not:
902 return rr0(eNOT(rc(1)), Outputs);
903 case C2_or:
904 return rr0(eORL(rc(1), rc(2)), Outputs);
905 case C2_orn:
906 return rr0(eORL(rc(1), eNOT(rc(2))), Outputs);
907 case C2_xor:
908 return rr0(eXOR(rc(1), rc(2)), Outputs);
909 case C4_and_and:
910 return rr0(eAND(rc(1), eAND(rc(2), rc(3))), Outputs);
911 case C4_and_andn:
912 return rr0(eAND(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
913 case C4_and_or:
914 return rr0(eAND(rc(1), eORL(rc(2), rc(3))), Outputs);
915 case C4_and_orn:
916 return rr0(eAND(rc(1), eORL(rc(2), eNOT(rc(3)))), Outputs);
917 case C4_or_and:
918 return rr0(eORL(rc(1), eAND(rc(2), rc(3))), Outputs);
919 case C4_or_andn:
920 return rr0(eORL(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
921 case C4_or_or:
922 return rr0(eORL(rc(1), eORL(rc(2), rc(3))), Outputs);
923 case C4_or_orn:
924 return rr0(eORL(rc(1), eORL(rc(2), eNOT(rc(3)))), Outputs);
925 case C2_bitsclr:
926 case C2_bitsclri:
927 case C2_bitsset:
928 case C4_nbitsclr:
929 case C4_nbitsclri:
930 case C4_nbitsset:
931 // TODO
932 break;
933 case S2_tstbit_i:
934 case S4_ntstbit_i: {
935 BT::BitValue V = rc(1)[im(2)];
936 if (V.is(0) || V.is(1)) {
937 // If instruction is S2_tstbit_i, test for 1, otherwise test for 0.
938 bool TV = (Opc == S2_tstbit_i);
939 BT::BitValue F = V.is(TV) ? BT::BitValue::One : BT::BitValue::Zero;
940 return rr0(RegisterCell(W0).fill(0, W0, F), Outputs);
941 }
942 break;
943 }
944
945 default:
946 // For instructions that define a single predicate registers, store
947 // the low 8 bits of the register only.
948 if (unsigned DefR = getUniqueDefVReg(MI)) {
949 if (MRI.getRegClass(DefR) == &Hexagon::PredRegsRegClass) {
950 BT::RegisterRef PD(DefR, 0);
951 uint16_t RW = getRegBitWidth(PD);
952 uint16_t PW = 8; // XXX Pred size: getRegBitWidth(Reg[1]);
953 RegisterCell RC = RegisterCell::self(DefR, RW);
954 RC.fill(PW, RW, BT::BitValue::Zero);
955 putCell(PD, RC, Outputs);
956 return true;
957 }
958 }
959 return MachineEvaluator::evaluate(MI, Inputs, Outputs);
960 }
961 #undef im
962 #undef rc
963 #undef op
964 return false;
965}
966
968 const CellMapType &Inputs,
969 BranchTargetList &Targets,
970 bool &FallsThru) const {
971 // We need to evaluate one branch at a time. TII::analyzeBranch checks
972 // all the branches in a basic block at once, so we cannot use it.
973 unsigned Opc = BI.getOpcode();
974 bool SimpleBranch = false;
975 bool Negated = false;
976 switch (Opc) {
977 case Hexagon::J2_jumpf:
978 case Hexagon::J2_jumpfpt:
979 case Hexagon::J2_jumpfnew:
980 case Hexagon::J2_jumpfnewpt:
981 Negated = true;
982 [[fallthrough]];
983 case Hexagon::J2_jumpt:
984 case Hexagon::J2_jumptpt:
985 case Hexagon::J2_jumptnew:
986 case Hexagon::J2_jumptnewpt:
987 // Simple branch: if([!]Pn) jump ...
988 // i.e. Op0 = predicate, Op1 = branch target.
989 SimpleBranch = true;
990 break;
991 case Hexagon::J2_jump:
992 Targets.insert(BI.getOperand(0).getMBB());
993 FallsThru = false;
994 return true;
995 default:
996 // If the branch is of unknown type, assume that all successors are
997 // executable.
998 return false;
999 }
1000
1001 if (!SimpleBranch)
1002 return false;
1003
1004 // BI is a conditional branch if we got here.
1005 RegisterRef PR = BI.getOperand(0);
1006 RegisterCell PC = getCell(PR, Inputs);
1007 const BT::BitValue &Test = PC[0];
1008
1009 // If the condition is neither true nor false, then it's unknown.
1010 if (!Test.is(0) && !Test.is(1))
1011 return false;
1012
1013 // "Test.is(!Negated)" means "branch condition is true".
1014 if (!Test.is(!Negated)) {
1015 // Condition known to be false.
1016 FallsThru = true;
1017 return true;
1018 }
1019
1020 Targets.insert(BI.getOperand(1).getMBB());
1021 FallsThru = false;
1022 return true;
1023}
1024
1025unsigned HexagonEvaluator::getUniqueDefVReg(const MachineInstr &MI) const {
1026 unsigned DefReg = 0;
1027 for (const MachineOperand &Op : MI.operands()) {
1028 if (!Op.isReg() || !Op.isDef())
1029 continue;
1030 Register R = Op.getReg();
1031 if (!R.isVirtual())
1032 continue;
1033 if (DefReg != 0)
1034 return 0;
1035 DefReg = R;
1036 }
1037 return DefReg;
1038}
1039
1040bool HexagonEvaluator::evaluateLoad(const MachineInstr &MI,
1041 const CellMapType &Inputs,
1042 CellMapType &Outputs) const {
1043 using namespace Hexagon;
1044
1045 if (TII.isPredicated(MI))
1046 return false;
1047 assert(MI.mayLoad() && "A load that mayn't?");
1048 unsigned Opc = MI.getOpcode();
1049
1050 uint16_t BitNum;
1051 bool SignEx;
1052
1053 switch (Opc) {
1054 default:
1055 return false;
1056
1057#if 0
1058 // memb_fifo
1059 case L2_loadalignb_pbr:
1060 case L2_loadalignb_pcr:
1061 case L2_loadalignb_pi:
1062 // memh_fifo
1063 case L2_loadalignh_pbr:
1064 case L2_loadalignh_pcr:
1065 case L2_loadalignh_pi:
1066 // membh
1067 case L2_loadbsw2_pbr:
1068 case L2_loadbsw2_pci:
1069 case L2_loadbsw2_pcr:
1070 case L2_loadbsw2_pi:
1071 case L2_loadbsw4_pbr:
1072 case L2_loadbsw4_pci:
1073 case L2_loadbsw4_pcr:
1074 case L2_loadbsw4_pi:
1075 // memubh
1076 case L2_loadbzw2_pbr:
1077 case L2_loadbzw2_pci:
1078 case L2_loadbzw2_pcr:
1079 case L2_loadbzw2_pi:
1080 case L2_loadbzw4_pbr:
1081 case L2_loadbzw4_pci:
1082 case L2_loadbzw4_pcr:
1083 case L2_loadbzw4_pi:
1084#endif
1085
1086 case L2_loadrbgp:
1087 case L2_loadrb_io:
1088 case L2_loadrb_pbr:
1089 case L2_loadrb_pci:
1090 case L2_loadrb_pcr:
1091 case L2_loadrb_pi:
1092 case PS_loadrbabs:
1093 case L4_loadrb_ap:
1094 case L4_loadrb_rr:
1095 case L4_loadrb_ur:
1096 BitNum = 8;
1097 SignEx = true;
1098 break;
1099
1100 case L2_loadrubgp:
1101 case L2_loadrub_io:
1102 case L2_loadrub_pbr:
1103 case L2_loadrub_pci:
1104 case L2_loadrub_pcr:
1105 case L2_loadrub_pi:
1106 case PS_loadrubabs:
1107 case L4_loadrub_ap:
1108 case L4_loadrub_rr:
1109 case L4_loadrub_ur:
1110 BitNum = 8;
1111 SignEx = false;
1112 break;
1113
1114 case L2_loadrhgp:
1115 case L2_loadrh_io:
1116 case L2_loadrh_pbr:
1117 case L2_loadrh_pci:
1118 case L2_loadrh_pcr:
1119 case L2_loadrh_pi:
1120 case PS_loadrhabs:
1121 case L4_loadrh_ap:
1122 case L4_loadrh_rr:
1123 case L4_loadrh_ur:
1124 BitNum = 16;
1125 SignEx = true;
1126 break;
1127
1128 case L2_loadruhgp:
1129 case L2_loadruh_io:
1130 case L2_loadruh_pbr:
1131 case L2_loadruh_pci:
1132 case L2_loadruh_pcr:
1133 case L2_loadruh_pi:
1134 case L4_loadruh_rr:
1135 case PS_loadruhabs:
1136 case L4_loadruh_ap:
1137 case L4_loadruh_ur:
1138 BitNum = 16;
1139 SignEx = false;
1140 break;
1141
1142 case L2_loadrigp:
1143 case L2_loadri_io:
1144 case L2_loadri_pbr:
1145 case L2_loadri_pci:
1146 case L2_loadri_pcr:
1147 case L2_loadri_pi:
1148 case L2_loadw_locked:
1149 case PS_loadriabs:
1150 case L4_loadri_ap:
1151 case L4_loadri_rr:
1152 case L4_loadri_ur:
1153 case LDriw_pred:
1154 BitNum = 32;
1155 SignEx = true;
1156 break;
1157
1158 case L2_loadrdgp:
1159 case L2_loadrd_io:
1160 case L2_loadrd_pbr:
1161 case L2_loadrd_pci:
1162 case L2_loadrd_pcr:
1163 case L2_loadrd_pi:
1164 case L4_loadd_locked:
1165 case PS_loadrdabs:
1166 case L4_loadrd_ap:
1167 case L4_loadrd_rr:
1168 case L4_loadrd_ur:
1169 BitNum = 64;
1170 SignEx = true;
1171 break;
1172 }
1173
1174 const MachineOperand &MD = MI.getOperand(0);
1175 assert(MD.isReg() && MD.isDef());
1176 RegisterRef RD = MD;
1177
1178 uint16_t W = getRegBitWidth(RD);
1179 assert(W >= BitNum && BitNum > 0);
1180 RegisterCell Res(W);
1181
1182 for (uint16_t i = 0; i < BitNum; ++i)
1183 Res[i] = BT::BitValue::self(BT::BitRef(RD.Reg, i));
1184
1185 if (SignEx) {
1186 const BT::BitValue &Sign = Res[BitNum-1];
1187 for (uint16_t i = BitNum; i < W; ++i)
1188 Res[i] = BT::BitValue::ref(Sign);
1189 } else {
1190 for (uint16_t i = BitNum; i < W; ++i)
1191 Res[i] = BT::BitValue::Zero;
1192 }
1193
1194 putCell(RD, Res, Outputs);
1195 return true;
1196}
1197
1198bool HexagonEvaluator::evaluateFormalCopy(const MachineInstr &MI,
1199 const CellMapType &Inputs,
1200 CellMapType &Outputs) const {
1201 // If MI defines a formal parameter, but is not a copy (loads are handled
1202 // in evaluateLoad), then it's not clear what to do.
1203 assert(MI.isCopy());
1204
1205 RegisterRef RD = MI.getOperand(0);
1206 RegisterRef RS = MI.getOperand(1);
1207 assert(RD.Sub == 0);
1208 if (!RS.Reg.isPhysical())
1209 return false;
1210 RegExtMap::const_iterator F = VRX.find(RD.Reg);
1211 if (F == VRX.end())
1212 return false;
1213
1214 uint16_t EW = F->second.Width;
1215 // Store RD's cell into the map. This will associate the cell with a virtual
1216 // register, and make zero-/sign-extends possible (otherwise we would be ex-
1217 // tending "self" bit values, which will have no effect, since "self" values
1218 // cannot be references to anything).
1219 putCell(RD, getCell(RS, Inputs), Outputs);
1220
1221 RegisterCell Res;
1222 // Read RD's cell from the outputs instead of RS's cell from the inputs:
1223 if (F->second.Type == ExtType::SExt)
1224 Res = eSXT(getCell(RD, Outputs), EW);
1225 else if (F->second.Type == ExtType::ZExt)
1226 Res = eZXT(getCell(RD, Outputs), EW);
1227
1228 putCell(RD, Res, Outputs);
1229 return true;
1230}
1231
1232unsigned HexagonEvaluator::getNextPhysReg(unsigned PReg, unsigned Width) const {
1233 using namespace Hexagon;
1234
1235 bool Is64 = DoubleRegsRegClass.contains(PReg);
1236 assert(PReg == 0 || Is64 || IntRegsRegClass.contains(PReg));
1237
1238 static const unsigned Phys32[] = { R0, R1, R2, R3, R4, R5 };
1239 static const unsigned Phys64[] = { D0, D1, D2 };
1240 const unsigned Num32 = sizeof(Phys32)/sizeof(unsigned);
1241 const unsigned Num64 = sizeof(Phys64)/sizeof(unsigned);
1242
1243 // Return the first parameter register of the required width.
1244 if (PReg == 0)
1245 return (Width <= 32) ? Phys32[0] : Phys64[0];
1246
1247 // Set Idx32, Idx64 in such a way that Idx+1 would give the index of the
1248 // next register.
1249 unsigned Idx32 = 0, Idx64 = 0;
1250 if (!Is64) {
1251 while (Idx32 < Num32) {
1252 if (Phys32[Idx32] == PReg)
1253 break;
1254 Idx32++;
1255 }
1256 Idx64 = Idx32/2;
1257 } else {
1258 while (Idx64 < Num64) {
1259 if (Phys64[Idx64] == PReg)
1260 break;
1261 Idx64++;
1262 }
1263 Idx32 = Idx64*2+1;
1264 }
1265
1266 if (Width <= 32)
1267 return (Idx32+1 < Num32) ? Phys32[Idx32+1] : 0;
1268 return (Idx64+1 < Num64) ? Phys64[Idx64+1] : 0;
1269}
1270
1271unsigned HexagonEvaluator::getVirtRegFor(unsigned PReg) const {
1272 for (std::pair<MCRegister, Register> P : MRI.liveins())
1273 if (P.first == PReg)
1274 return P.second;
1275 return 0;
1276}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the simple types necessary to represent the attributes associated with functions a...
BitTracker BT
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define op(i)
#define im(i)
#define rc(i)
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
#define R4(n)
#define R2(n)
#define P(N)
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:679
unsigned getID() const
getID() - Return the register class ID number.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1775
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
void shuffle(Iterator first, Iterator last, RNG &&g)
Definition STLExtras.h:1546
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
#define N
bool is(unsigned T) const
Definition BitTracker.h:207
RegisterCell eXOR(const RegisterCell &A1, const RegisterCell &A2) const
MachineEvaluator(const TargetRegisterInfo &T, MachineRegisterInfo &M)
Definition BitTracker.h:391
const TargetRegisterInfo & TRI
Definition BitTracker.h:488
RegisterCell eNOT(const RegisterCell &A1) const
RegisterCell eIMM(int64_t V, uint16_t W) const
RegisterCell eLSR(const RegisterCell &A1, uint16_t Sh) const
RegisterCell eMLU(const RegisterCell &A1, const RegisterCell &A2) const
RegisterCell eZXT(const RegisterCell &A1, uint16_t FromN) const
RegisterCell eMLS(const RegisterCell &A1, const RegisterCell &A2) const
uint16_t getRegBitWidth(const RegisterRef &RR) const
RegisterCell eCLB(const RegisterCell &A1, bool B, uint16_t W) const
RegisterCell eASR(const RegisterCell &A1, uint16_t Sh) const
RegisterCell eASL(const RegisterCell &A1, uint16_t Sh) const
void putCell(const RegisterRef &RR, RegisterCell RC, CellMapType &M) const
RegisterCell eCTB(const RegisterCell &A1, bool B, uint16_t W) const
RegisterCell getCell(const RegisterRef &RR, const CellMapType &M) const
RegisterCell eAND(const RegisterCell &A1, const RegisterCell &A2) const
RegisterCell eORL(const RegisterCell &A1, const RegisterCell &A2) const
RegisterCell eSXT(const RegisterCell &A1, uint16_t FromN) const
RegisterCell eINS(const RegisterCell &A1, const RegisterCell &A2, uint16_t AtN) const
RegisterCell eADD(const RegisterCell &A1, const RegisterCell &A2) const
RegisterCell eSUB(const RegisterCell &A1, const RegisterCell &A2) const
RegisterCell eXTR(const RegisterCell &A1, uint16_t B, uint16_t E) const
RegisterCell & cat(const RegisterCell &RC)
static RegisterCell self(unsigned Reg, uint16_t Width)
Definition BitTracker.h:362
static RegisterCell ref(const RegisterCell &C)
Definition BitTracker.h:378
RegisterCell & fill(uint16_t B, uint16_t E, const BitValue &V)
RegisterCell & insert(const RegisterCell &RC, const BitMask &M)
BitTracker::BitMask mask(Register Reg, unsigned Sub) const override
uint16_t getPhysRegBitWidth(MCRegister Reg) const override
BitTracker::BranchTargetList BranchTargetList
bool evaluate(const MachineInstr &MI, const CellMapType &Inputs, CellMapType &Outputs) const override
HexagonEvaluator(const HexagonRegisterInfo &tri, MachineRegisterInfo &mri, const HexagonInstrInfo &tii, MachineFunction &mf)
MachineFrameInfo & MFI
BitTracker::RegisterCell RegisterCell
const HexagonInstrInfo & TII
const TargetRegisterClass & composeWithSubRegIndex(const TargetRegisterClass &RC, unsigned Idx) const override
BitTracker::CellMapType CellMapType
BitTracker::RegisterRef RegisterRef