1 //===- HexagonBitTracker.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "HexagonBitTracker.h"
10 #include "HexagonInstrInfo.h"
11 #include "HexagonRegisterInfo.h"
12 #include "HexagonSubtarget.h"
13 #include "llvm/CodeGen/MachineFrameInfo.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/CodeGen/MachineOperand.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/CodeGen/TargetRegisterInfo.h"
19 #include "llvm/IR/Argument.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
36 using BT
= BitTracker
;
38 HexagonEvaluator::HexagonEvaluator(const HexagonRegisterInfo
&tri
,
39 MachineRegisterInfo
&mri
,
40 const HexagonInstrInfo
&tii
,
42 : MachineEvaluator(tri
, mri
), MF(mf
), MFI(mf
.getFrameInfo()), TII(tii
) {
43 // Populate the VRX map (VR to extension-type).
44 // Go over all the formal parameters of the function. If a given parameter
45 // P is sign- or zero-extended, locate the virtual register holding that
46 // parameter and create an entry in the VRX map indicating the type of ex-
47 // tension (and the source type).
48 // This is a bit complicated to do accurately, since the memory layout in-
49 // formation is necessary to precisely determine whether an aggregate para-
50 // meter will be passed in a register or in memory. What is given in MRI
51 // is the association between the physical register that is live-in (i.e.
52 // holds an argument), and the virtual register that this value will be
53 // copied into. This, by itself, is not sufficient to map back the virtual
54 // register to a formal parameter from Function (since consecutive live-ins
55 // from MRI may not correspond to consecutive formal parameters from Func-
56 // tion). To avoid the complications with in-memory arguments, only consi-
57 // der the initial sequence of formal parameters that are known to be
58 // passed via registers.
59 unsigned InVirtReg
, InPhysReg
= 0;
61 for (const Argument
&Arg
: MF
.getFunction().args()) {
62 Type
*ATy
= Arg
.getType();
64 if (ATy
->isIntegerTy())
65 Width
= ATy
->getIntegerBitWidth();
66 else if (ATy
->isPointerTy())
68 // If pointer size is not set through target data, it will default to
69 // Module::AnyPointerSize.
70 if (Width
== 0 || Width
> 64)
72 if (Arg
.hasAttribute(Attribute::ByVal
))
74 InPhysReg
= getNextPhysReg(InPhysReg
, Width
);
77 InVirtReg
= getVirtRegFor(InPhysReg
);
80 if (Arg
.hasAttribute(Attribute::SExt
))
81 VRX
.insert(std::make_pair(InVirtReg
, ExtType(ExtType::SExt
, Width
)));
82 else if (Arg
.hasAttribute(Attribute::ZExt
))
83 VRX
.insert(std::make_pair(InVirtReg
, ExtType(ExtType::ZExt
, Width
)));
87 BT::BitMask
HexagonEvaluator::mask(Register Reg
, unsigned Sub
) const {
89 return MachineEvaluator::mask(Reg
, 0);
90 const TargetRegisterClass
&RC
= *MRI
.getRegClass(Reg
);
91 unsigned ID
= RC
.getID();
92 uint16_t RW
= getRegBitWidth(RegisterRef(Reg
, Sub
));
93 const auto &HRI
= static_cast<const HexagonRegisterInfo
&>(TRI
);
94 bool IsSubLo
= (Sub
== HRI
.getHexagonSubRegIndex(RC
, Hexagon::ps_sub_lo
));
96 case Hexagon::DoubleRegsRegClassID
:
97 case Hexagon::HvxWRRegClassID
:
98 case Hexagon::HvxVQRRegClassID
:
99 return IsSubLo
? BT::BitMask(0, RW
-1)
100 : BT::BitMask(RW
, 2*RW
-1);
105 dbgs() << printReg(Reg
, &TRI
, Sub
) << " in reg class "
106 << TRI
.getRegClassName(&RC
) << '\n';
108 llvm_unreachable("Unexpected register/subregister");
111 uint16_t HexagonEvaluator::getPhysRegBitWidth(MCRegister Reg
) const {
112 using namespace Hexagon
;
113 const auto &HST
= MF
.getSubtarget
<HexagonSubtarget
>();
114 if (HST
.useHVXOps()) {
115 for (auto &RC
: {HvxVRRegClass
, HvxWRRegClass
, HvxQRRegClass
,
117 if (RC
.contains(Reg
))
118 return TRI
.getRegSizeInBits(RC
);
120 // Default treatment for other physical registers.
121 if (const TargetRegisterClass
*RC
= TRI
.getMinimalPhysRegClass(Reg
))
122 return TRI
.getRegSizeInBits(*RC
);
125 (Twine("Unhandled physical register") + TRI
.getName(Reg
)).str().c_str());
128 const TargetRegisterClass
&HexagonEvaluator::composeWithSubRegIndex(
129 const TargetRegisterClass
&RC
, unsigned Idx
) const {
134 const auto &HRI
= static_cast<const HexagonRegisterInfo
&>(TRI
);
135 bool IsSubLo
= (Idx
== HRI
.getHexagonSubRegIndex(RC
, Hexagon::ps_sub_lo
));
136 bool IsSubHi
= (Idx
== HRI
.getHexagonSubRegIndex(RC
, Hexagon::ps_sub_hi
));
137 assert(IsSubLo
!= IsSubHi
&& "Must refer to either low or high subreg");
140 switch (RC
.getID()) {
141 case Hexagon::DoubleRegsRegClassID
:
142 return Hexagon::IntRegsRegClass
;
143 case Hexagon::HvxWRRegClassID
:
144 return Hexagon::HvxVRRegClass
;
145 case Hexagon::HvxVQRRegClassID
:
146 return Hexagon::HvxWRRegClass
;
151 dbgs() << "Reg class id: " << RC
.getID() << " idx: " << Idx
<< '\n';
153 llvm_unreachable("Unimplemented combination of reg class/subreg idx");
159 std::vector
<BT::RegisterRef
> Vector
;
162 RegisterRefs(const MachineInstr
&MI
) : Vector(MI
.getNumOperands()) {
163 for (unsigned i
= 0, n
= Vector
.size(); i
< n
; ++i
) {
164 const MachineOperand
&MO
= MI
.getOperand(i
);
166 Vector
[i
] = BT::RegisterRef(MO
);
167 // For indices that don't correspond to registers, the entry will
168 // remain constructed via the default constructor.
172 size_t size() const { return Vector
.size(); }
174 const BT::RegisterRef
&operator[](unsigned n
) const {
175 // The main purpose of this operator is to assert with bad argument.
176 assert(n
< Vector
.size());
181 } // end anonymous namespace
183 bool HexagonEvaluator::evaluate(const MachineInstr
&MI
,
184 const CellMapType
&Inputs
,
185 CellMapType
&Outputs
) const {
186 using namespace Hexagon
;
188 unsigned NumDefs
= 0;
190 // Basic correctness check: there should not be any defs with subregisters.
191 for (const MachineOperand
&MO
: MI
.operands()) {
192 if (!MO
.isReg() || !MO
.isDef())
195 assert(MO
.getSubReg() == 0);
201 unsigned Opc
= MI
.getOpcode();
205 // These instructions may be marked as mayLoad, but they are generating
206 // immediate values, so skip them.
211 return evaluateLoad(MI
, Inputs
, Outputs
);
215 // Check COPY instructions that copy formal parameters into virtual
216 // registers. Such parameters can be sign- or zero-extended at the
217 // call site, and we should take advantage of this knowledge. The MRI
218 // keeps a list of pairs of live-in physical and virtual registers,
219 // which provides information about which virtual registers will hold
220 // the argument values. The function will still contain instructions
221 // defining those virtual registers, and in practice those are COPY
222 // instructions from a physical to a virtual register. In such cases,
223 // applying the argument extension to the virtual register can be seen
224 // as simply mirroring the extension that had already been applied to
225 // the physical register at the call site. If the defining instruction
226 // was not a COPY, it would not be clear how to mirror that extension
227 // on the callee's side. For that reason, only check COPY instructions
228 // for potential extensions.
230 if (evaluateFormalCopy(MI
, Inputs
, Outputs
))
234 // Beyond this point, if any operand is a global, skip that instruction.
235 // The reason is that certain instructions that can take an immediate
236 // operand can also have a global symbol in that operand. To avoid
237 // checking what kind of operand a given instruction has individually
238 // for each instruction, do it here. Global symbols as operands gene-
239 // rally do not provide any useful information.
240 for (const MachineOperand
&MO
: MI
.operands()) {
241 if (MO
.isGlobal() || MO
.isBlockAddress() || MO
.isSymbol() || MO
.isJTI() ||
246 RegisterRefs
Reg(MI
);
247 #define op(i) MI.getOperand(i)
248 #define rc(i) RegisterCell::ref(getCell(Reg[i], Inputs))
249 #define im(i) MI.getOperand(i).getImm()
251 // If the instruction has no register operands, skip it.
255 // Record result for register in operand 0.
256 auto rr0
= [this,Reg
] (const BT::RegisterCell
&Val
, CellMapType
&Outputs
)
258 putCell(Reg
[0], Val
, Outputs
);
261 // Get the cell corresponding to the N-th operand.
262 auto cop
= [this, &Reg
, &MI
, &Inputs
](unsigned N
,
263 uint16_t W
) -> BT::RegisterCell
{
264 const MachineOperand
&Op
= MI
.getOperand(N
);
266 return eIMM(Op
.getImm(), W
);
268 return RegisterCell::self(0, W
);
269 assert(getRegBitWidth(Reg
[N
]) == W
&& "Register width mismatch");
272 // Extract RW low bits of the cell.
273 auto lo
= [this] (const BT::RegisterCell
&RC
, uint16_t RW
)
274 -> BT::RegisterCell
{
275 assert(RW
<= RC
.width());
276 return eXTR(RC
, 0, RW
);
278 // Extract RW high bits of the cell.
279 auto hi
= [this] (const BT::RegisterCell
&RC
, uint16_t RW
)
280 -> BT::RegisterCell
{
281 uint16_t W
= RC
.width();
283 return eXTR(RC
, W
-RW
, W
);
285 // Extract N-th halfword (counting from the least significant position).
286 auto half
= [this] (const BT::RegisterCell
&RC
, unsigned N
)
287 -> BT::RegisterCell
{
288 assert(N
*16+16 <= RC
.width());
289 return eXTR(RC
, N
*16, N
*16+16);
291 // Shuffle bits (pick even/odd from cells and merge into result).
292 auto shuffle
= [this] (const BT::RegisterCell
&Rs
, const BT::RegisterCell
&Rt
,
293 uint16_t BW
, bool Odd
) -> BT::RegisterCell
{
294 uint16_t I
= Odd
, Ws
= Rs
.width();
295 assert(Ws
== Rt
.width());
296 RegisterCell RC
= eXTR(Rt
, I
*BW
, I
*BW
+BW
).cat(eXTR(Rs
, I
*BW
, I
*BW
+BW
));
299 RC
.cat(eXTR(Rt
, I
*BW
, I
*BW
+BW
)).cat(eXTR(Rs
, I
*BW
, I
*BW
+BW
));
305 // The bitwidth of the 0th operand. In most (if not all) of the
306 // instructions below, the 0th operand is the defined register.
307 // Pre-compute the bitwidth here, because it is needed in many cases
309 uint16_t W0
= (Reg
[0].Reg
!= 0) ? getRegBitWidth(Reg
[0]) : 0;
311 // Register id of the 0th operand. It can be 0.
312 unsigned Reg0
= Reg
[0].Reg
;
315 // Transfer immediate:
321 return rr0(eIMM(im(1), W0
), Outputs
);
323 return rr0(RegisterCell(W0
).fill(0, W0
, BT::BitValue::Zero
), Outputs
);
325 return rr0(RegisterCell(W0
).fill(0, W0
, BT::BitValue::One
), Outputs
);
327 int FI
= op(1).getIndex();
328 int Off
= op(2).getImm();
329 unsigned A
= MFI
.getObjectAlign(FI
).value() + std::abs(Off
);
330 unsigned L
= llvm::countr_zero(A
);
331 RegisterCell RC
= RegisterCell::self(Reg
[0].Reg
, W0
);
332 RC
.fill(0, L
, BT::BitValue::Zero
);
333 return rr0(RC
, Outputs
);
336 // Transfer register:
341 return rr0(rc(1), Outputs
);
344 uint16_t PW
= 8; // XXX Pred size: getRegBitWidth(Reg[1]);
346 RegisterCell PC
= eXTR(rc(1), 0, PW
);
347 RegisterCell RC
= RegisterCell(RW
).insert(PC
, BT::BitMask(0, PW
-1));
348 RC
.fill(PW
, RW
, BT::BitValue::Zero
);
349 return rr0(RC
, Outputs
);
353 uint16_t PW
= 8; // XXX Pred size: getRegBitWidth(Reg[1]);
354 RegisterCell RC
= RegisterCell::self(Reg
[0].Reg
, RW
);
355 RC
.fill(PW
, RW
, BT::BitValue::Zero
);
356 return rr0(eINS(RC
, eXTR(rc(1), 0, PW
), 0), Outputs
);
367 uint16_t W1
= getRegBitWidth(Reg
[1]);
368 assert(W0
== 64 && W1
== 32);
369 RegisterCell CW
= RegisterCell(W0
).insert(rc(1), BT::BitMask(0, W1
-1));
370 RegisterCell RC
= eADD(eSXT(CW
, W1
), rc(2));
371 return rr0(RC
, Outputs
);
375 return rr0(eADD(rc(1), rc(2)), Outputs
);
377 return rr0(eADD(rc(1), eIMM(im(2), W0
)), Outputs
);
378 case S4_addi_asl_ri
: {
379 RegisterCell RC
= eADD(eIMM(im(1), W0
), eASL(rc(2), im(3)));
380 return rr0(RC
, Outputs
);
382 case S4_addi_lsr_ri
: {
383 RegisterCell RC
= eADD(eIMM(im(1), W0
), eLSR(rc(2), im(3)));
384 return rr0(RC
, Outputs
);
387 RegisterCell RC
= eADD(rc(1), eADD(rc(2), eIMM(im(3), W0
)));
388 return rr0(RC
, Outputs
);
390 case M4_mpyri_addi
: {
391 RegisterCell M
= eMLS(rc(2), eIMM(im(3), W0
));
392 RegisterCell RC
= eADD(eIMM(im(1), W0
), lo(M
, W0
));
393 return rr0(RC
, Outputs
);
395 case M4_mpyrr_addi
: {
396 RegisterCell M
= eMLS(rc(2), rc(3));
397 RegisterCell RC
= eADD(eIMM(im(1), W0
), lo(M
, W0
));
398 return rr0(RC
, Outputs
);
400 case M4_mpyri_addr_u2
: {
401 RegisterCell M
= eMLS(eIMM(im(2), W0
), rc(3));
402 RegisterCell RC
= eADD(rc(1), lo(M
, W0
));
403 return rr0(RC
, Outputs
);
405 case M4_mpyri_addr
: {
406 RegisterCell M
= eMLS(rc(2), eIMM(im(3), W0
));
407 RegisterCell RC
= eADD(rc(1), lo(M
, W0
));
408 return rr0(RC
, Outputs
);
410 case M4_mpyrr_addr
: {
411 RegisterCell M
= eMLS(rc(2), rc(3));
412 RegisterCell RC
= eADD(rc(1), lo(M
, W0
));
413 return rr0(RC
, Outputs
);
416 RegisterCell RC
= eADD(rc(1), eSUB(eIMM(im(2), W0
), rc(3)));
417 return rr0(RC
, Outputs
);
420 RegisterCell RC
= eADD(rc(1), eADD(rc(2), eIMM(im(3), W0
)));
421 return rr0(RC
, Outputs
);
424 RegisterCell RC
= eADD(rc(1), eADD(rc(2), rc(3)));
425 return rr0(RC
, Outputs
);
428 RegisterCell RC
= eADD(rc(1), eSUB(rc(2), rc(3)));
429 return rr0(RC
, Outputs
);
431 case S2_addasl_rrri
: {
432 RegisterCell RC
= eADD(rc(1), eASL(rc(2), im(3)));
433 return rr0(RC
, Outputs
);
436 RegisterCell RPC
= RegisterCell::self(Reg
[0].Reg
, W0
);
437 RPC
.fill(0, 2, BT::BitValue::Zero
);
438 return rr0(eADD(RPC
, eIMM(im(2), W0
)), Outputs
);
442 return rr0(eSUB(rc(1), rc(2)), Outputs
);
444 return rr0(eSUB(eIMM(im(1), W0
), rc(2)), Outputs
);
445 case S4_subi_asl_ri
: {
446 RegisterCell RC
= eSUB(eIMM(im(1), W0
), eASL(rc(2), im(3)));
447 return rr0(RC
, Outputs
);
449 case S4_subi_lsr_ri
: {
450 RegisterCell RC
= eSUB(eIMM(im(1), W0
), eLSR(rc(2), im(3)));
451 return rr0(RC
, Outputs
);
454 RegisterCell RC
= eSUB(rc(1), eADD(rc(2), eIMM(im(3), W0
)));
455 return rr0(RC
, Outputs
);
458 RegisterCell RC
= eSUB(rc(1), eADD(rc(2), rc(3)));
459 return rr0(RC
, Outputs
);
461 // 32-bit negation is done by "Rd = A2_subri 0, Rs"
463 return rr0(eSUB(eIMM(0, W0
), rc(1)), Outputs
);
466 RegisterCell M
= eMLS(rc(1), rc(2));
467 return rr0(hi(M
, W0
), Outputs
);
470 return rr0(eMLS(rc(1), rc(2)), Outputs
);
471 case M2_dpmpyss_acc_s0
:
472 return rr0(eADD(rc(1), eMLS(rc(2), rc(3))), Outputs
);
473 case M2_dpmpyss_nac_s0
:
474 return rr0(eSUB(rc(1), eMLS(rc(2), rc(3))), Outputs
);
476 RegisterCell M
= eMLS(rc(1), rc(2));
477 return rr0(lo(M
, W0
), Outputs
);
480 RegisterCell M
= eMLS(rc(2), eIMM(im(3), W0
));
481 RegisterCell RC
= eADD(rc(1), lo(M
, W0
));
482 return rr0(RC
, Outputs
);
485 RegisterCell M
= eMLS(rc(2), eIMM(im(3), W0
));
486 RegisterCell RC
= eSUB(rc(1), lo(M
, W0
));
487 return rr0(RC
, Outputs
);
490 RegisterCell M
= eMLS(rc(2), rc(3));
491 RegisterCell RC
= eADD(rc(1), lo(M
, W0
));
492 return rr0(RC
, Outputs
);
495 RegisterCell M
= eMLS(rc(2), rc(3));
496 RegisterCell RC
= eSUB(rc(1), lo(M
, W0
));
497 return rr0(RC
, Outputs
);
500 RegisterCell M
= eMLS(rc(1), eIMM(im(2), W0
));
501 return rr0(lo(M
, 32), Outputs
);
504 RegisterCell M
= eMLS(rc(1), eIMM(-im(2), W0
));
505 return rr0(lo(M
, 32), Outputs
);
508 RegisterCell M
= eMLS(rc(1), eIMM(im(2), W0
));
509 return rr0(lo(M
, 32), Outputs
);
512 RegisterCell M
= eMLU(rc(1), rc(2));
513 return rr0(hi(M
, W0
), Outputs
);
516 return rr0(eMLU(rc(1), rc(2)), Outputs
);
517 case M2_dpmpyuu_acc_s0
:
518 return rr0(eADD(rc(1), eMLU(rc(2), rc(3))), Outputs
);
519 case M2_dpmpyuu_nac_s0
:
520 return rr0(eSUB(rc(1), eMLU(rc(2), rc(3))), Outputs
);
526 return rr0(eAND(rc(1), eIMM(im(2), W0
)), Outputs
);
529 return rr0(eAND(rc(1), rc(2)), Outputs
);
532 return rr0(eAND(rc(1), eNOT(rc(2))), Outputs
);
533 case S4_andi_asl_ri
: {
534 RegisterCell RC
= eAND(eIMM(im(1), W0
), eASL(rc(2), im(3)));
535 return rr0(RC
, Outputs
);
537 case S4_andi_lsr_ri
: {
538 RegisterCell RC
= eAND(eIMM(im(1), W0
), eLSR(rc(2), im(3)));
539 return rr0(RC
, Outputs
);
542 return rr0(eAND(rc(1), eAND(rc(2), rc(3))), Outputs
);
544 return rr0(eAND(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs
);
546 return rr0(eAND(rc(1), eORL(rc(2), rc(3))), Outputs
);
548 return rr0(eAND(rc(1), eXOR(rc(2), rc(3))), Outputs
);
550 return rr0(eORL(rc(1), eIMM(im(2), W0
)), Outputs
);
553 return rr0(eORL(rc(1), rc(2)), Outputs
);
556 return rr0(eORL(rc(1), eNOT(rc(2))), Outputs
);
557 case S4_ori_asl_ri
: {
558 RegisterCell RC
= eORL(eIMM(im(1), W0
), eASL(rc(2), im(3)));
559 return rr0(RC
, Outputs
);
561 case S4_ori_lsr_ri
: {
562 RegisterCell RC
= eORL(eIMM(im(1), W0
), eLSR(rc(2), im(3)));
563 return rr0(RC
, Outputs
);
566 return rr0(eORL(rc(1), eAND(rc(2), rc(3))), Outputs
);
568 return rr0(eORL(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs
);
571 RegisterCell RC
= eORL(rc(1), eAND(rc(2), eIMM(im(3), W0
)));
572 return rr0(RC
, Outputs
);
575 RegisterCell RC
= eORL(rc(1), eORL(rc(2), eIMM(im(3), W0
)));
576 return rr0(RC
, Outputs
);
579 return rr0(eORL(rc(1), eORL(rc(2), rc(3))), Outputs
);
581 return rr0(eORL(rc(1), eXOR(rc(2), rc(3))), Outputs
);
584 return rr0(eXOR(rc(1), rc(2)), Outputs
);
586 return rr0(eXOR(rc(1), eAND(rc(2), rc(3))), Outputs
);
588 return rr0(eXOR(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs
);
590 return rr0(eXOR(rc(1), eORL(rc(2), rc(3))), Outputs
);
592 return rr0(eXOR(rc(1), eXOR(rc(2), rc(3))), Outputs
);
595 return rr0(eNOT(rc(1)), Outputs
);
599 return rr0(eASL(rc(1), im(2)), Outputs
);
601 return rr0(eASL(rc(1), 16), Outputs
);
604 return rr0(eADD(rc(1), eASL(rc(2), im(3))), Outputs
);
607 return rr0(eSUB(rc(1), eASL(rc(2), im(3))), Outputs
);
610 return rr0(eAND(rc(1), eASL(rc(2), im(3))), Outputs
);
613 return rr0(eORL(rc(1), eASL(rc(2), im(3))), Outputs
);
614 case S2_asl_i_r_xacc
:
615 case S2_asl_i_p_xacc
:
616 return rr0(eXOR(rc(1), eASL(rc(2), im(3))), Outputs
);
624 return rr0(eASR(rc(1), im(2)), Outputs
);
626 return rr0(eASR(rc(1), 16), Outputs
);
629 return rr0(eADD(rc(1), eASR(rc(2), im(3))), Outputs
);
632 return rr0(eSUB(rc(1), eASR(rc(2), im(3))), Outputs
);
635 return rr0(eAND(rc(1), eASR(rc(2), im(3))), Outputs
);
638 return rr0(eORL(rc(1), eASR(rc(2), im(3))), Outputs
);
639 case S2_asr_i_r_rnd
: {
640 // The input is first sign-extended to 64 bits, then the output
641 // is truncated back to 32 bits.
643 RegisterCell XC
= eSXT(rc(1).cat(eIMM(0, W0
)), W0
);
644 RegisterCell RC
= eASR(eADD(eASR(XC
, im(2)), eIMM(1, 2*W0
)), 1);
645 return rr0(eXTR(RC
, 0, W0
), Outputs
);
647 case S2_asr_i_r_rnd_goodsyntax
: {
650 return rr0(rc(1), Outputs
);
651 // Result: S2_asr_i_r_rnd Rs, u5-1
652 RegisterCell XC
= eSXT(rc(1).cat(eIMM(0, W0
)), W0
);
653 RegisterCell RC
= eLSR(eADD(eASR(XC
, S
-1), eIMM(1, 2*W0
)), 1);
654 return rr0(eXTR(RC
, 0, W0
), Outputs
);
658 case S2_asr_i_svw_trun
:
664 return rr0(eLSR(rc(1), im(2)), Outputs
);
667 return rr0(eADD(rc(1), eLSR(rc(2), im(3))), Outputs
);
670 return rr0(eSUB(rc(1), eLSR(rc(2), im(3))), Outputs
);
673 return rr0(eAND(rc(1), eLSR(rc(2), im(3))), Outputs
);
676 return rr0(eORL(rc(1), eLSR(rc(2), im(3))), Outputs
);
677 case S2_lsr_i_r_xacc
:
678 case S2_lsr_i_p_xacc
:
679 return rr0(eXOR(rc(1), eLSR(rc(2), im(3))), Outputs
);
682 RegisterCell RC
= rc(1);
683 RC
[im(2)] = BT::BitValue::Zero
;
684 return rr0(RC
, Outputs
);
687 RegisterCell RC
= rc(1);
688 RC
[im(2)] = BT::BitValue::One
;
689 return rr0(RC
, Outputs
);
691 case S2_togglebit_i
: {
692 RegisterCell RC
= rc(1);
694 RC
[BX
] = RC
[BX
].is(0) ? BT::BitValue::One
695 : RC
[BX
].is(1) ? BT::BitValue::Zero
696 : BT::BitValue::self();
697 return rr0(RC
, Outputs
);
701 uint16_t W1
= getRegBitWidth(Reg
[1]);
703 // Res.uw[1] = Rs[bx+1:], Res.uw[0] = Rs[0:bx]
704 const BT::BitValue Zero
= BT::BitValue::Zero
;
705 RegisterCell RZ
= RegisterCell(W0
).fill(BX
, W1
, Zero
)
706 .fill(W1
+(W1
-BX
), W0
, Zero
);
707 RegisterCell BF1
= eXTR(rc(1), 0, BX
), BF2
= eXTR(rc(1), BX
, W1
);
708 RegisterCell RC
= eINS(eINS(RZ
, BF1
, 0), BF2
, W1
);
709 return rr0(RC
, Outputs
);
715 uint16_t Wd
= im(2), Of
= im(3);
718 return rr0(eIMM(0, W0
), Outputs
);
719 // If the width extends beyond the register size, pad the register
721 RegisterCell Pad
= (Wd
+Of
> W0
) ? rc(1).cat(eIMM(0, Wd
+Of
-W0
)) : rc(1);
722 RegisterCell Ext
= eXTR(Pad
, Of
, Wd
+Of
);
723 // Ext is short, need to extend it with 0s or sign bit.
724 RegisterCell RC
= RegisterCell(W0
).insert(Ext
, BT::BitMask(0, Wd
-1));
725 if (Opc
== S2_extractu
|| Opc
== S2_extractup
)
726 return rr0(eZXT(RC
, Wd
), Outputs
);
727 return rr0(eSXT(RC
, Wd
), Outputs
);
731 uint16_t Wd
= im(3), Of
= im(4);
732 assert(Wd
< W0
&& Of
< W0
);
733 // If Wd+Of exceeds W0, the inserted bits are truncated.
737 return rr0(rc(1), Outputs
);
738 return rr0(eINS(rc(1), eXTR(rc(2), 0, Wd
), Of
), Outputs
);
750 return rr0(cop(2, W0
/2).cat(cop(1, W0
/2)), Outputs
);
754 case A2_combine_hh
: {
756 assert(getRegBitWidth(Reg
[1]) == 32 && getRegBitWidth(Reg
[2]) == 32);
757 // Low half in the output is 0 for _ll and _hl, 1 otherwise:
758 unsigned LoH
= !(Opc
== A2_combine_ll
|| Opc
== A2_combine_hl
);
759 // High half in the output is 0 for _ll and _lh, 1 otherwise:
760 unsigned HiH
= !(Opc
== A2_combine_ll
|| Opc
== A2_combine_lh
);
761 RegisterCell R1
= rc(1);
762 RegisterCell R2
= rc(2);
763 RegisterCell RC
= half(R2
, LoH
).cat(half(R1
, HiH
));
764 return rr0(RC
, Outputs
);
768 assert(getRegBitWidth(Reg
[1]) == 32 && getRegBitWidth(Reg
[2]) == 32);
769 RegisterCell R1
= rc(1);
770 RegisterCell R2
= rc(2);
771 RegisterCell RC
= half(R2
, 0).cat(half(R1
, 0)).cat(half(R2
, 1))
773 return rr0(RC
, Outputs
);
776 RegisterCell RC
= shuffle(rc(1), rc(2), 8, false);
777 return rr0(RC
, Outputs
);
780 RegisterCell RC
= shuffle(rc(1), rc(2), 16, false);
781 return rr0(RC
, Outputs
);
784 RegisterCell RC
= shuffle(rc(1), rc(2), 8, true);
785 return rr0(RC
, Outputs
);
788 RegisterCell RC
= shuffle(rc(1), rc(2), 16, true);
789 return rr0(RC
, Outputs
);
793 uint16_t WP
= 8; // XXX Pred size: getRegBitWidth(Reg[1]);
794 assert(WR
== 64 && WP
== 8);
795 RegisterCell R1
= rc(1);
797 for (uint16_t i
= 0; i
< WP
; ++i
) {
798 const BT::BitValue
&V
= R1
[i
];
799 BT::BitValue F
= (V
.is(0) || V
.is(1)) ? V
: BT::BitValue::self();
800 RC
.fill(i
*8, i
*8+8, F
);
802 return rr0(RC
, Outputs
);
811 BT::BitValue PC0
= rc(1)[0];
812 RegisterCell R2
= cop(2, W0
);
813 RegisterCell R3
= cop(3, W0
);
814 if (PC0
.is(0) || PC0
.is(1))
815 return rr0(RegisterCell::ref(PC0
? R2
: R3
), Outputs
);
816 R2
.meet(R3
, Reg
[0].Reg
);
817 return rr0(R2
, Outputs
);
823 // Sign- and zero-extension:
826 return rr0(eSXT(rc(1), 8), Outputs
);
828 return rr0(eSXT(rc(1), 16), Outputs
);
830 uint16_t W1
= getRegBitWidth(Reg
[1]);
831 assert(W0
== 64 && W1
== 32);
832 RegisterCell RC
= eSXT(rc(1).cat(eIMM(0, W1
)), W1
);
833 return rr0(RC
, Outputs
);
836 return rr0(eZXT(rc(1), 8), Outputs
);
838 return rr0(eZXT(rc(1), 16), Outputs
);
843 return rr0(eSXT(RegisterCell::self(0, W0
).regify(Reg0
), 8), Outputs
);
845 return rr0(eSXT(RegisterCell::self(0, W0
).regify(Reg0
), 16), Outputs
);
847 return rr0(eZXT(RegisterCell::self(0, W0
).regify(Reg0
), 8), Outputs
);
849 return rr0(eZXT(RegisterCell::self(0, W0
).regify(Reg0
), 16), Outputs
);
855 // Always produce a 32-bit result.
856 return rr0(eCLB(rc(1), false/*bit*/, 32), Outputs
);
859 return rr0(eCLB(rc(1), true/*bit*/, 32), Outputs
);
862 uint16_t W1
= getRegBitWidth(Reg
[1]);
863 RegisterCell R1
= rc(1);
864 BT::BitValue TV
= R1
[W1
-1];
865 if (TV
.is(0) || TV
.is(1))
866 return rr0(eCLB(R1
, TV
, 32), Outputs
);
871 return rr0(eCTB(rc(1), false/*bit*/, 32), Outputs
);
874 return rr0(eCTB(rc(1), true/*bit*/, 32), Outputs
);
880 RegisterCell P1
= rc(1);
881 bool Has0
= false, All1
= true;
882 for (uint16_t i
= 0; i
< 8/*XXX*/; ++i
) {
893 RC
.fill(0, W0
, (All1
? BT::BitValue::One
: BT::BitValue::Zero
));
894 return rr0(RC
, Outputs
);
897 RegisterCell P1
= rc(1);
898 bool Has1
= false, All0
= true;
899 for (uint16_t i
= 0; i
< 8/*XXX*/; ++i
) {
910 RC
.fill(0, W0
, (Has1
? BT::BitValue::One
: BT::BitValue::Zero
));
911 return rr0(RC
, Outputs
);
914 return rr0(eAND(rc(1), rc(2)), Outputs
);
916 return rr0(eAND(rc(1), eNOT(rc(2))), Outputs
);
918 return rr0(eNOT(rc(1)), Outputs
);
920 return rr0(eORL(rc(1), rc(2)), Outputs
);
922 return rr0(eORL(rc(1), eNOT(rc(2))), Outputs
);
924 return rr0(eXOR(rc(1), rc(2)), Outputs
);
926 return rr0(eAND(rc(1), eAND(rc(2), rc(3))), Outputs
);
928 return rr0(eAND(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs
);
930 return rr0(eAND(rc(1), eORL(rc(2), rc(3))), Outputs
);
932 return rr0(eAND(rc(1), eORL(rc(2), eNOT(rc(3)))), Outputs
);
934 return rr0(eORL(rc(1), eAND(rc(2), rc(3))), Outputs
);
936 return rr0(eORL(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs
);
938 return rr0(eORL(rc(1), eORL(rc(2), rc(3))), Outputs
);
940 return rr0(eORL(rc(1), eORL(rc(2), eNOT(rc(3)))), Outputs
);
951 BT::BitValue V
= rc(1)[im(2)];
952 if (V
.is(0) || V
.is(1)) {
953 // If instruction is S2_tstbit_i, test for 1, otherwise test for 0.
954 bool TV
= (Opc
== S2_tstbit_i
);
955 BT::BitValue F
= V
.is(TV
) ? BT::BitValue::One
: BT::BitValue::Zero
;
956 return rr0(RegisterCell(W0
).fill(0, W0
, F
), Outputs
);
962 // For instructions that define a single predicate registers, store
963 // the low 8 bits of the register only.
964 if (unsigned DefR
= getUniqueDefVReg(MI
)) {
965 if (MRI
.getRegClass(DefR
) == &Hexagon::PredRegsRegClass
) {
966 BT::RegisterRef
PD(DefR
, 0);
967 uint16_t RW
= getRegBitWidth(PD
);
968 uint16_t PW
= 8; // XXX Pred size: getRegBitWidth(Reg[1]);
969 RegisterCell RC
= RegisterCell::self(DefR
, RW
);
970 RC
.fill(PW
, RW
, BT::BitValue::Zero
);
971 putCell(PD
, RC
, Outputs
);
975 return MachineEvaluator::evaluate(MI
, Inputs
, Outputs
);
983 bool HexagonEvaluator::evaluate(const MachineInstr
&BI
,
984 const CellMapType
&Inputs
,
985 BranchTargetList
&Targets
,
986 bool &FallsThru
) const {
987 // We need to evaluate one branch at a time. TII::analyzeBranch checks
988 // all the branches in a basic block at once, so we cannot use it.
989 unsigned Opc
= BI
.getOpcode();
990 bool SimpleBranch
= false;
991 bool Negated
= false;
993 case Hexagon::J2_jumpf
:
994 case Hexagon::J2_jumpfpt
:
995 case Hexagon::J2_jumpfnew
:
996 case Hexagon::J2_jumpfnewpt
:
999 case Hexagon::J2_jumpt
:
1000 case Hexagon::J2_jumptpt
:
1001 case Hexagon::J2_jumptnew
:
1002 case Hexagon::J2_jumptnewpt
:
1003 // Simple branch: if([!]Pn) jump ...
1004 // i.e. Op0 = predicate, Op1 = branch target.
1005 SimpleBranch
= true;
1007 case Hexagon::J2_jump
:
1008 Targets
.insert(BI
.getOperand(0).getMBB());
1012 // If the branch is of unknown type, assume that all successors are
1020 // BI is a conditional branch if we got here.
1021 RegisterRef PR
= BI
.getOperand(0);
1022 RegisterCell PC
= getCell(PR
, Inputs
);
1023 const BT::BitValue
&Test
= PC
[0];
1025 // If the condition is neither true nor false, then it's unknown.
1026 if (!Test
.is(0) && !Test
.is(1))
1029 // "Test.is(!Negated)" means "branch condition is true".
1030 if (!Test
.is(!Negated
)) {
1031 // Condition known to be false.
1036 Targets
.insert(BI
.getOperand(1).getMBB());
1041 unsigned HexagonEvaluator::getUniqueDefVReg(const MachineInstr
&MI
) const {
1042 unsigned DefReg
= 0;
1043 for (const MachineOperand
&Op
: MI
.operands()) {
1044 if (!Op
.isReg() || !Op
.isDef())
1046 Register R
= Op
.getReg();
1056 bool HexagonEvaluator::evaluateLoad(const MachineInstr
&MI
,
1057 const CellMapType
&Inputs
,
1058 CellMapType
&Outputs
) const {
1059 using namespace Hexagon
;
1061 if (TII
.isPredicated(MI
))
1063 assert(MI
.mayLoad() && "A load that mayn't?");
1064 unsigned Opc
= MI
.getOpcode();
1075 case L2_loadalignb_pbr
:
1076 case L2_loadalignb_pcr
:
1077 case L2_loadalignb_pi
:
1079 case L2_loadalignh_pbr
:
1080 case L2_loadalignh_pcr
:
1081 case L2_loadalignh_pi
:
1083 case L2_loadbsw2_pbr
:
1084 case L2_loadbsw2_pci
:
1085 case L2_loadbsw2_pcr
:
1086 case L2_loadbsw2_pi
:
1087 case L2_loadbsw4_pbr
:
1088 case L2_loadbsw4_pci
:
1089 case L2_loadbsw4_pcr
:
1090 case L2_loadbsw4_pi
:
1092 case L2_loadbzw2_pbr
:
1093 case L2_loadbzw2_pci
:
1094 case L2_loadbzw2_pcr
:
1095 case L2_loadbzw2_pi
:
1096 case L2_loadbzw4_pbr
:
1097 case L2_loadbzw4_pci
:
1098 case L2_loadbzw4_pcr
:
1099 case L2_loadbzw4_pi
:
1118 case L2_loadrub_pbr
:
1119 case L2_loadrub_pci
:
1120 case L2_loadrub_pcr
:
1146 case L2_loadruh_pbr
:
1147 case L2_loadruh_pci
:
1148 case L2_loadruh_pcr
:
1164 case L2_loadw_locked
:
1180 case L4_loadd_locked
:
1190 const MachineOperand
&MD
= MI
.getOperand(0);
1191 assert(MD
.isReg() && MD
.isDef());
1192 RegisterRef RD
= MD
;
1194 uint16_t W
= getRegBitWidth(RD
);
1195 assert(W
>= BitNum
&& BitNum
> 0);
1196 RegisterCell
Res(W
);
1198 for (uint16_t i
= 0; i
< BitNum
; ++i
)
1199 Res
[i
] = BT::BitValue::self(BT::BitRef(RD
.Reg
, i
));
1202 const BT::BitValue
&Sign
= Res
[BitNum
-1];
1203 for (uint16_t i
= BitNum
; i
< W
; ++i
)
1204 Res
[i
] = BT::BitValue::ref(Sign
);
1206 for (uint16_t i
= BitNum
; i
< W
; ++i
)
1207 Res
[i
] = BT::BitValue::Zero
;
1210 putCell(RD
, Res
, Outputs
);
1214 bool HexagonEvaluator::evaluateFormalCopy(const MachineInstr
&MI
,
1215 const CellMapType
&Inputs
,
1216 CellMapType
&Outputs
) const {
1217 // If MI defines a formal parameter, but is not a copy (loads are handled
1218 // in evaluateLoad), then it's not clear what to do.
1219 assert(MI
.isCopy());
1221 RegisterRef RD
= MI
.getOperand(0);
1222 RegisterRef RS
= MI
.getOperand(1);
1223 assert(RD
.Sub
== 0);
1224 if (!RS
.Reg
.isPhysical())
1226 RegExtMap::const_iterator F
= VRX
.find(RD
.Reg
);
1230 uint16_t EW
= F
->second
.Width
;
1231 // Store RD's cell into the map. This will associate the cell with a virtual
1232 // register, and make zero-/sign-extends possible (otherwise we would be ex-
1233 // tending "self" bit values, which will have no effect, since "self" values
1234 // cannot be references to anything).
1235 putCell(RD
, getCell(RS
, Inputs
), Outputs
);
1238 // Read RD's cell from the outputs instead of RS's cell from the inputs:
1239 if (F
->second
.Type
== ExtType::SExt
)
1240 Res
= eSXT(getCell(RD
, Outputs
), EW
);
1241 else if (F
->second
.Type
== ExtType::ZExt
)
1242 Res
= eZXT(getCell(RD
, Outputs
), EW
);
1244 putCell(RD
, Res
, Outputs
);
1248 unsigned HexagonEvaluator::getNextPhysReg(unsigned PReg
, unsigned Width
) const {
1249 using namespace Hexagon
;
1251 bool Is64
= DoubleRegsRegClass
.contains(PReg
);
1252 assert(PReg
== 0 || Is64
|| IntRegsRegClass
.contains(PReg
));
1254 static const unsigned Phys32
[] = { R0
, R1
, R2
, R3
, R4
, R5
};
1255 static const unsigned Phys64
[] = { D0
, D1
, D2
};
1256 const unsigned Num32
= sizeof(Phys32
)/sizeof(unsigned);
1257 const unsigned Num64
= sizeof(Phys64
)/sizeof(unsigned);
1259 // Return the first parameter register of the required width.
1261 return (Width
<= 32) ? Phys32
[0] : Phys64
[0];
1263 // Set Idx32, Idx64 in such a way that Idx+1 would give the index of the
1265 unsigned Idx32
= 0, Idx64
= 0;
1267 while (Idx32
< Num32
) {
1268 if (Phys32
[Idx32
] == PReg
)
1274 while (Idx64
< Num64
) {
1275 if (Phys64
[Idx64
] == PReg
)
1283 return (Idx32
+1 < Num32
) ? Phys32
[Idx32
+1] : 0;
1284 return (Idx64
+1 < Num64
) ? Phys64
[Idx64
+1] : 0;
1287 unsigned HexagonEvaluator::getVirtRegFor(unsigned PReg
) const {
1288 for (std::pair
<MCRegister
, Register
> P
: MRI
.liveins())
1289 if (P
.first
== PReg
)