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