10b57cec5SDimitry Andric //===- HexagonBitTracker.cpp ----------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "HexagonBitTracker.h"
100b57cec5SDimitry Andric #include "Hexagon.h"
110b57cec5SDimitry Andric #include "HexagonInstrInfo.h"
120b57cec5SDimitry Andric #include "HexagonRegisterInfo.h"
130b57cec5SDimitry Andric #include "HexagonSubtarget.h"
140b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
200b57cec5SDimitry Andric #include "llvm/IR/Argument.h"
210b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
220b57cec5SDimitry Andric #include "llvm/IR/Function.h"
230b57cec5SDimitry Andric #include "llvm/IR/Type.h"
240b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
250b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
260b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
270b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
280b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
290b57cec5SDimitry Andric #include <cassert>
300b57cec5SDimitry Andric #include <cstddef>
310b57cec5SDimitry Andric #include <cstdint>
320b57cec5SDimitry Andric #include <cstdlib>
330b57cec5SDimitry Andric #include <utility>
340b57cec5SDimitry Andric #include <vector>
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric using namespace llvm;
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric using BT = BitTracker;
390b57cec5SDimitry Andric
HexagonEvaluator(const HexagonRegisterInfo & tri,MachineRegisterInfo & mri,const HexagonInstrInfo & tii,MachineFunction & mf)400b57cec5SDimitry Andric HexagonEvaluator::HexagonEvaluator(const HexagonRegisterInfo &tri,
410b57cec5SDimitry Andric MachineRegisterInfo &mri,
420b57cec5SDimitry Andric const HexagonInstrInfo &tii,
430b57cec5SDimitry Andric MachineFunction &mf)
440b57cec5SDimitry Andric : MachineEvaluator(tri, mri), MF(mf), MFI(mf.getFrameInfo()), TII(tii) {
450b57cec5SDimitry Andric // Populate the VRX map (VR to extension-type).
460b57cec5SDimitry Andric // Go over all the formal parameters of the function. If a given parameter
470b57cec5SDimitry Andric // P is sign- or zero-extended, locate the virtual register holding that
480b57cec5SDimitry Andric // parameter and create an entry in the VRX map indicating the type of ex-
490b57cec5SDimitry Andric // tension (and the source type).
500b57cec5SDimitry Andric // This is a bit complicated to do accurately, since the memory layout in-
510b57cec5SDimitry Andric // formation is necessary to precisely determine whether an aggregate para-
520b57cec5SDimitry Andric // meter will be passed in a register or in memory. What is given in MRI
530b57cec5SDimitry Andric // is the association between the physical register that is live-in (i.e.
540b57cec5SDimitry Andric // holds an argument), and the virtual register that this value will be
550b57cec5SDimitry Andric // copied into. This, by itself, is not sufficient to map back the virtual
560b57cec5SDimitry Andric // register to a formal parameter from Function (since consecutive live-ins
570b57cec5SDimitry Andric // from MRI may not correspond to consecutive formal parameters from Func-
580b57cec5SDimitry Andric // tion). To avoid the complications with in-memory arguments, only consi-
590b57cec5SDimitry Andric // der the initial sequence of formal parameters that are known to be
600b57cec5SDimitry Andric // passed via registers.
610b57cec5SDimitry Andric unsigned InVirtReg, InPhysReg = 0;
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric for (const Argument &Arg : MF.getFunction().args()) {
640b57cec5SDimitry Andric Type *ATy = Arg.getType();
650b57cec5SDimitry Andric unsigned Width = 0;
660b57cec5SDimitry Andric if (ATy->isIntegerTy())
670b57cec5SDimitry Andric Width = ATy->getIntegerBitWidth();
680b57cec5SDimitry Andric else if (ATy->isPointerTy())
690b57cec5SDimitry Andric Width = 32;
700b57cec5SDimitry Andric // If pointer size is not set through target data, it will default to
710b57cec5SDimitry Andric // Module::AnyPointerSize.
720b57cec5SDimitry Andric if (Width == 0 || Width > 64)
730b57cec5SDimitry Andric break;
740b57cec5SDimitry Andric if (Arg.hasAttribute(Attribute::ByVal))
750b57cec5SDimitry Andric continue;
760b57cec5SDimitry Andric InPhysReg = getNextPhysReg(InPhysReg, Width);
770b57cec5SDimitry Andric if (!InPhysReg)
780b57cec5SDimitry Andric break;
790b57cec5SDimitry Andric InVirtReg = getVirtRegFor(InPhysReg);
800b57cec5SDimitry Andric if (!InVirtReg)
810b57cec5SDimitry Andric continue;
820b57cec5SDimitry Andric if (Arg.hasAttribute(Attribute::SExt))
830b57cec5SDimitry Andric VRX.insert(std::make_pair(InVirtReg, ExtType(ExtType::SExt, Width)));
840b57cec5SDimitry Andric else if (Arg.hasAttribute(Attribute::ZExt))
850b57cec5SDimitry Andric VRX.insert(std::make_pair(InVirtReg, ExtType(ExtType::ZExt, Width)));
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric
mask(Register Reg,unsigned Sub) const89e8d8bef9SDimitry Andric BT::BitMask HexagonEvaluator::mask(Register Reg, unsigned Sub) const {
900b57cec5SDimitry Andric if (Sub == 0)
910b57cec5SDimitry Andric return MachineEvaluator::mask(Reg, 0);
920b57cec5SDimitry Andric const TargetRegisterClass &RC = *MRI.getRegClass(Reg);
930b57cec5SDimitry Andric unsigned ID = RC.getID();
940b57cec5SDimitry Andric uint16_t RW = getRegBitWidth(RegisterRef(Reg, Sub));
950b57cec5SDimitry Andric const auto &HRI = static_cast<const HexagonRegisterInfo&>(TRI);
960b57cec5SDimitry Andric bool IsSubLo = (Sub == HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo));
970b57cec5SDimitry Andric switch (ID) {
980b57cec5SDimitry Andric case Hexagon::DoubleRegsRegClassID:
990b57cec5SDimitry Andric case Hexagon::HvxWRRegClassID:
1000b57cec5SDimitry Andric case Hexagon::HvxVQRRegClassID:
1010b57cec5SDimitry Andric return IsSubLo ? BT::BitMask(0, RW-1)
1020b57cec5SDimitry Andric : BT::BitMask(RW, 2*RW-1);
1030b57cec5SDimitry Andric default:
1040b57cec5SDimitry Andric break;
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric #ifndef NDEBUG
1070b57cec5SDimitry Andric dbgs() << printReg(Reg, &TRI, Sub) << " in reg class "
1080b57cec5SDimitry Andric << TRI.getRegClassName(&RC) << '\n';
1090b57cec5SDimitry Andric #endif
1100b57cec5SDimitry Andric llvm_unreachable("Unexpected register/subregister");
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric
getPhysRegBitWidth(MCRegister Reg) const113e8d8bef9SDimitry Andric uint16_t HexagonEvaluator::getPhysRegBitWidth(MCRegister Reg) const {
1140b57cec5SDimitry Andric using namespace Hexagon;
1150b57cec5SDimitry Andric const auto &HST = MF.getSubtarget<HexagonSubtarget>();
1160b57cec5SDimitry Andric if (HST.useHVXOps()) {
1170b57cec5SDimitry Andric for (auto &RC : {HvxVRRegClass, HvxWRRegClass, HvxQRRegClass,
1180b57cec5SDimitry Andric HvxVQRRegClass})
1190b57cec5SDimitry Andric if (RC.contains(Reg))
1200b57cec5SDimitry Andric return TRI.getRegSizeInBits(RC);
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric // Default treatment for other physical registers.
1230b57cec5SDimitry Andric if (const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg))
1240b57cec5SDimitry Andric return TRI.getRegSizeInBits(*RC);
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric llvm_unreachable(
1270b57cec5SDimitry Andric (Twine("Unhandled physical register") + TRI.getName(Reg)).str().c_str());
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric
composeWithSubRegIndex(const TargetRegisterClass & RC,unsigned Idx) const1300b57cec5SDimitry Andric const TargetRegisterClass &HexagonEvaluator::composeWithSubRegIndex(
1310b57cec5SDimitry Andric const TargetRegisterClass &RC, unsigned Idx) const {
1320b57cec5SDimitry Andric if (Idx == 0)
1330b57cec5SDimitry Andric return RC;
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric #ifndef NDEBUG
1360b57cec5SDimitry Andric const auto &HRI = static_cast<const HexagonRegisterInfo&>(TRI);
1370b57cec5SDimitry Andric bool IsSubLo = (Idx == HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo));
1380b57cec5SDimitry Andric bool IsSubHi = (Idx == HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi));
1390b57cec5SDimitry Andric assert(IsSubLo != IsSubHi && "Must refer to either low or high subreg");
1400b57cec5SDimitry Andric #endif
1410b57cec5SDimitry Andric
1420b57cec5SDimitry Andric switch (RC.getID()) {
1430b57cec5SDimitry Andric case Hexagon::DoubleRegsRegClassID:
1440b57cec5SDimitry Andric return Hexagon::IntRegsRegClass;
1450b57cec5SDimitry Andric case Hexagon::HvxWRRegClassID:
1460b57cec5SDimitry Andric return Hexagon::HvxVRRegClass;
1470b57cec5SDimitry Andric case Hexagon::HvxVQRRegClassID:
1480b57cec5SDimitry Andric return Hexagon::HvxWRRegClass;
1490b57cec5SDimitry Andric default:
1500b57cec5SDimitry Andric break;
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric #ifndef NDEBUG
1530b57cec5SDimitry Andric dbgs() << "Reg class id: " << RC.getID() << " idx: " << Idx << '\n';
1540b57cec5SDimitry Andric #endif
1550b57cec5SDimitry Andric llvm_unreachable("Unimplemented combination of reg class/subreg idx");
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andric namespace {
1590b57cec5SDimitry Andric
1600b57cec5SDimitry Andric class RegisterRefs {
1610b57cec5SDimitry Andric std::vector<BT::RegisterRef> Vector;
1620b57cec5SDimitry Andric
1630b57cec5SDimitry Andric public:
RegisterRefs(const MachineInstr & MI)1640b57cec5SDimitry Andric RegisterRefs(const MachineInstr &MI) : Vector(MI.getNumOperands()) {
1650b57cec5SDimitry Andric for (unsigned i = 0, n = Vector.size(); i < n; ++i) {
1660b57cec5SDimitry Andric const MachineOperand &MO = MI.getOperand(i);
1670b57cec5SDimitry Andric if (MO.isReg())
1680b57cec5SDimitry Andric Vector[i] = BT::RegisterRef(MO);
1690b57cec5SDimitry Andric // For indices that don't correspond to registers, the entry will
1700b57cec5SDimitry Andric // remain constructed via the default constructor.
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric
size() const1740b57cec5SDimitry Andric size_t size() const { return Vector.size(); }
1750b57cec5SDimitry Andric
operator [](unsigned n) const1760b57cec5SDimitry Andric const BT::RegisterRef &operator[](unsigned n) const {
1770b57cec5SDimitry Andric // The main purpose of this operator is to assert with bad argument.
1780b57cec5SDimitry Andric assert(n < Vector.size());
1790b57cec5SDimitry Andric return Vector[n];
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric };
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andric } // end anonymous namespace
1840b57cec5SDimitry Andric
evaluate(const MachineInstr & MI,const CellMapType & Inputs,CellMapType & Outputs) const1850b57cec5SDimitry Andric bool HexagonEvaluator::evaluate(const MachineInstr &MI,
1860b57cec5SDimitry Andric const CellMapType &Inputs,
1870b57cec5SDimitry Andric CellMapType &Outputs) const {
1880b57cec5SDimitry Andric using namespace Hexagon;
1890b57cec5SDimitry Andric
1900b57cec5SDimitry Andric unsigned NumDefs = 0;
1910b57cec5SDimitry Andric
1924824e7fdSDimitry Andric // Basic correctness check: there should not be any defs with subregisters.
1930b57cec5SDimitry Andric for (const MachineOperand &MO : MI.operands()) {
1940b57cec5SDimitry Andric if (!MO.isReg() || !MO.isDef())
1950b57cec5SDimitry Andric continue;
1960b57cec5SDimitry Andric NumDefs++;
1970b57cec5SDimitry Andric assert(MO.getSubReg() == 0);
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric
2000b57cec5SDimitry Andric if (NumDefs == 0)
2010b57cec5SDimitry Andric return false;
2020b57cec5SDimitry Andric
2030b57cec5SDimitry Andric unsigned Opc = MI.getOpcode();
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andric if (MI.mayLoad()) {
2060b57cec5SDimitry Andric switch (Opc) {
2070b57cec5SDimitry Andric // These instructions may be marked as mayLoad, but they are generating
2080b57cec5SDimitry Andric // immediate values, so skip them.
2090b57cec5SDimitry Andric case CONST32:
2100b57cec5SDimitry Andric case CONST64:
2110b57cec5SDimitry Andric break;
2120b57cec5SDimitry Andric default:
2130b57cec5SDimitry Andric return evaluateLoad(MI, Inputs, Outputs);
2140b57cec5SDimitry Andric }
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric
2170b57cec5SDimitry Andric // Check COPY instructions that copy formal parameters into virtual
2180b57cec5SDimitry Andric // registers. Such parameters can be sign- or zero-extended at the
2190b57cec5SDimitry Andric // call site, and we should take advantage of this knowledge. The MRI
2200b57cec5SDimitry Andric // keeps a list of pairs of live-in physical and virtual registers,
2210b57cec5SDimitry Andric // which provides information about which virtual registers will hold
2220b57cec5SDimitry Andric // the argument values. The function will still contain instructions
2230b57cec5SDimitry Andric // defining those virtual registers, and in practice those are COPY
2240b57cec5SDimitry Andric // instructions from a physical to a virtual register. In such cases,
2250b57cec5SDimitry Andric // applying the argument extension to the virtual register can be seen
2260b57cec5SDimitry Andric // as simply mirroring the extension that had already been applied to
2270b57cec5SDimitry Andric // the physical register at the call site. If the defining instruction
2280b57cec5SDimitry Andric // was not a COPY, it would not be clear how to mirror that extension
2290b57cec5SDimitry Andric // on the callee's side. For that reason, only check COPY instructions
2300b57cec5SDimitry Andric // for potential extensions.
2310b57cec5SDimitry Andric if (MI.isCopy()) {
2320b57cec5SDimitry Andric if (evaluateFormalCopy(MI, Inputs, Outputs))
2330b57cec5SDimitry Andric return true;
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric
2360b57cec5SDimitry Andric // Beyond this point, if any operand is a global, skip that instruction.
2370b57cec5SDimitry Andric // The reason is that certain instructions that can take an immediate
2380b57cec5SDimitry Andric // operand can also have a global symbol in that operand. To avoid
2390b57cec5SDimitry Andric // checking what kind of operand a given instruction has individually
2400b57cec5SDimitry Andric // for each instruction, do it here. Global symbols as operands gene-
2410b57cec5SDimitry Andric // rally do not provide any useful information.
2420b57cec5SDimitry Andric for (const MachineOperand &MO : MI.operands()) {
2430b57cec5SDimitry Andric if (MO.isGlobal() || MO.isBlockAddress() || MO.isSymbol() || MO.isJTI() ||
2440b57cec5SDimitry Andric MO.isCPI())
2450b57cec5SDimitry Andric return false;
2460b57cec5SDimitry Andric }
2470b57cec5SDimitry Andric
2480b57cec5SDimitry Andric RegisterRefs Reg(MI);
2490b57cec5SDimitry Andric #define op(i) MI.getOperand(i)
2500b57cec5SDimitry Andric #define rc(i) RegisterCell::ref(getCell(Reg[i], Inputs))
2510b57cec5SDimitry Andric #define im(i) MI.getOperand(i).getImm()
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andric // If the instruction has no register operands, skip it.
2540b57cec5SDimitry Andric if (Reg.size() == 0)
2550b57cec5SDimitry Andric return false;
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andric // Record result for register in operand 0.
2580b57cec5SDimitry Andric auto rr0 = [this,Reg] (const BT::RegisterCell &Val, CellMapType &Outputs)
2590b57cec5SDimitry Andric -> bool {
2600b57cec5SDimitry Andric putCell(Reg[0], Val, Outputs);
2610b57cec5SDimitry Andric return true;
2620b57cec5SDimitry Andric };
2630b57cec5SDimitry Andric // Get the cell corresponding to the N-th operand.
2640b57cec5SDimitry Andric auto cop = [this, &Reg, &MI, &Inputs](unsigned N,
2650b57cec5SDimitry Andric uint16_t W) -> BT::RegisterCell {
2660b57cec5SDimitry Andric const MachineOperand &Op = MI.getOperand(N);
2670b57cec5SDimitry Andric if (Op.isImm())
2680b57cec5SDimitry Andric return eIMM(Op.getImm(), W);
2690b57cec5SDimitry Andric if (!Op.isReg())
2700b57cec5SDimitry Andric return RegisterCell::self(0, W);
2710b57cec5SDimitry Andric assert(getRegBitWidth(Reg[N]) == W && "Register width mismatch");
2720b57cec5SDimitry Andric return rc(N);
2730b57cec5SDimitry Andric };
2740b57cec5SDimitry Andric // Extract RW low bits of the cell.
2750b57cec5SDimitry Andric auto lo = [this] (const BT::RegisterCell &RC, uint16_t RW)
2760b57cec5SDimitry Andric -> BT::RegisterCell {
2770b57cec5SDimitry Andric assert(RW <= RC.width());
2780b57cec5SDimitry Andric return eXTR(RC, 0, RW);
2790b57cec5SDimitry Andric };
2800b57cec5SDimitry Andric // Extract RW high bits of the cell.
2810b57cec5SDimitry Andric auto hi = [this] (const BT::RegisterCell &RC, uint16_t RW)
2820b57cec5SDimitry Andric -> BT::RegisterCell {
2830b57cec5SDimitry Andric uint16_t W = RC.width();
2840b57cec5SDimitry Andric assert(RW <= W);
2850b57cec5SDimitry Andric return eXTR(RC, W-RW, W);
2860b57cec5SDimitry Andric };
2870b57cec5SDimitry Andric // Extract N-th halfword (counting from the least significant position).
2880b57cec5SDimitry Andric auto half = [this] (const BT::RegisterCell &RC, unsigned N)
2890b57cec5SDimitry Andric -> BT::RegisterCell {
2900b57cec5SDimitry Andric assert(N*16+16 <= RC.width());
2910b57cec5SDimitry Andric return eXTR(RC, N*16, N*16+16);
2920b57cec5SDimitry Andric };
2930b57cec5SDimitry Andric // Shuffle bits (pick even/odd from cells and merge into result).
2940b57cec5SDimitry Andric auto shuffle = [this] (const BT::RegisterCell &Rs, const BT::RegisterCell &Rt,
2950b57cec5SDimitry Andric uint16_t BW, bool Odd) -> BT::RegisterCell {
2960b57cec5SDimitry Andric uint16_t I = Odd, Ws = Rs.width();
2970b57cec5SDimitry Andric assert(Ws == Rt.width());
2980b57cec5SDimitry Andric RegisterCell RC = eXTR(Rt, I*BW, I*BW+BW).cat(eXTR(Rs, I*BW, I*BW+BW));
2990b57cec5SDimitry Andric I += 2;
3000b57cec5SDimitry Andric while (I*BW < Ws) {
3010b57cec5SDimitry Andric RC.cat(eXTR(Rt, I*BW, I*BW+BW)).cat(eXTR(Rs, I*BW, I*BW+BW));
3020b57cec5SDimitry Andric I += 2;
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric return RC;
3050b57cec5SDimitry Andric };
3060b57cec5SDimitry Andric
3070b57cec5SDimitry Andric // The bitwidth of the 0th operand. In most (if not all) of the
3080b57cec5SDimitry Andric // instructions below, the 0th operand is the defined register.
3090b57cec5SDimitry Andric // Pre-compute the bitwidth here, because it is needed in many cases
3100b57cec5SDimitry Andric // cases below.
3110b57cec5SDimitry Andric uint16_t W0 = (Reg[0].Reg != 0) ? getRegBitWidth(Reg[0]) : 0;
3120b57cec5SDimitry Andric
3130b57cec5SDimitry Andric // Register id of the 0th operand. It can be 0.
3140b57cec5SDimitry Andric unsigned Reg0 = Reg[0].Reg;
3150b57cec5SDimitry Andric
3160b57cec5SDimitry Andric switch (Opc) {
3170b57cec5SDimitry Andric // Transfer immediate:
3180b57cec5SDimitry Andric
3190b57cec5SDimitry Andric case A2_tfrsi:
3200b57cec5SDimitry Andric case A2_tfrpi:
3210b57cec5SDimitry Andric case CONST32:
3220b57cec5SDimitry Andric case CONST64:
3230b57cec5SDimitry Andric return rr0(eIMM(im(1), W0), Outputs);
3240b57cec5SDimitry Andric case PS_false:
3250b57cec5SDimitry Andric return rr0(RegisterCell(W0).fill(0, W0, BT::BitValue::Zero), Outputs);
3260b57cec5SDimitry Andric case PS_true:
3270b57cec5SDimitry Andric return rr0(RegisterCell(W0).fill(0, W0, BT::BitValue::One), Outputs);
3280b57cec5SDimitry Andric case PS_fi: {
3290b57cec5SDimitry Andric int FI = op(1).getIndex();
3300b57cec5SDimitry Andric int Off = op(2).getImm();
3315ffd83dbSDimitry Andric unsigned A = MFI.getObjectAlign(FI).value() + std::abs(Off);
332*06c3fb27SDimitry Andric unsigned L = llvm::countr_zero(A);
3330b57cec5SDimitry Andric RegisterCell RC = RegisterCell::self(Reg[0].Reg, W0);
3340b57cec5SDimitry Andric RC.fill(0, L, BT::BitValue::Zero);
3350b57cec5SDimitry Andric return rr0(RC, Outputs);
3360b57cec5SDimitry Andric }
3370b57cec5SDimitry Andric
3380b57cec5SDimitry Andric // Transfer register:
3390b57cec5SDimitry Andric
3400b57cec5SDimitry Andric case A2_tfr:
3410b57cec5SDimitry Andric case A2_tfrp:
3420b57cec5SDimitry Andric case C2_pxfer_map:
3430b57cec5SDimitry Andric return rr0(rc(1), Outputs);
3440b57cec5SDimitry Andric case C2_tfrpr: {
3450b57cec5SDimitry Andric uint16_t RW = W0;
3460b57cec5SDimitry Andric uint16_t PW = 8; // XXX Pred size: getRegBitWidth(Reg[1]);
3470b57cec5SDimitry Andric assert(PW <= RW);
3480b57cec5SDimitry Andric RegisterCell PC = eXTR(rc(1), 0, PW);
3490b57cec5SDimitry Andric RegisterCell RC = RegisterCell(RW).insert(PC, BT::BitMask(0, PW-1));
3500b57cec5SDimitry Andric RC.fill(PW, RW, BT::BitValue::Zero);
3510b57cec5SDimitry Andric return rr0(RC, Outputs);
3520b57cec5SDimitry Andric }
3530b57cec5SDimitry Andric case C2_tfrrp: {
3540b57cec5SDimitry Andric uint16_t RW = W0;
3550b57cec5SDimitry Andric uint16_t PW = 8; // XXX Pred size: getRegBitWidth(Reg[1]);
3560b57cec5SDimitry Andric RegisterCell RC = RegisterCell::self(Reg[0].Reg, RW);
3570b57cec5SDimitry Andric RC.fill(PW, RW, BT::BitValue::Zero);
3580b57cec5SDimitry Andric return rr0(eINS(RC, eXTR(rc(1), 0, PW), 0), Outputs);
3590b57cec5SDimitry Andric }
3600b57cec5SDimitry Andric
3610b57cec5SDimitry Andric // Arithmetic:
3620b57cec5SDimitry Andric
3630b57cec5SDimitry Andric case A2_abs:
3640b57cec5SDimitry Andric case A2_absp:
3650b57cec5SDimitry Andric // TODO
3660b57cec5SDimitry Andric break;
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andric case A2_addsp: {
3690b57cec5SDimitry Andric uint16_t W1 = getRegBitWidth(Reg[1]);
3700b57cec5SDimitry Andric assert(W0 == 64 && W1 == 32);
3710b57cec5SDimitry Andric RegisterCell CW = RegisterCell(W0).insert(rc(1), BT::BitMask(0, W1-1));
3720b57cec5SDimitry Andric RegisterCell RC = eADD(eSXT(CW, W1), rc(2));
3730b57cec5SDimitry Andric return rr0(RC, Outputs);
3740b57cec5SDimitry Andric }
3750b57cec5SDimitry Andric case A2_add:
3760b57cec5SDimitry Andric case A2_addp:
3770b57cec5SDimitry Andric return rr0(eADD(rc(1), rc(2)), Outputs);
3780b57cec5SDimitry Andric case A2_addi:
3790b57cec5SDimitry Andric return rr0(eADD(rc(1), eIMM(im(2), W0)), Outputs);
3800b57cec5SDimitry Andric case S4_addi_asl_ri: {
3810b57cec5SDimitry Andric RegisterCell RC = eADD(eIMM(im(1), W0), eASL(rc(2), im(3)));
3820b57cec5SDimitry Andric return rr0(RC, Outputs);
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric case S4_addi_lsr_ri: {
3850b57cec5SDimitry Andric RegisterCell RC = eADD(eIMM(im(1), W0), eLSR(rc(2), im(3)));
3860b57cec5SDimitry Andric return rr0(RC, Outputs);
3870b57cec5SDimitry Andric }
3880b57cec5SDimitry Andric case S4_addaddi: {
3890b57cec5SDimitry Andric RegisterCell RC = eADD(rc(1), eADD(rc(2), eIMM(im(3), W0)));
3900b57cec5SDimitry Andric return rr0(RC, Outputs);
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric case M4_mpyri_addi: {
3930b57cec5SDimitry Andric RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
3940b57cec5SDimitry Andric RegisterCell RC = eADD(eIMM(im(1), W0), lo(M, W0));
3950b57cec5SDimitry Andric return rr0(RC, Outputs);
3960b57cec5SDimitry Andric }
3970b57cec5SDimitry Andric case M4_mpyrr_addi: {
3980b57cec5SDimitry Andric RegisterCell M = eMLS(rc(2), rc(3));
3990b57cec5SDimitry Andric RegisterCell RC = eADD(eIMM(im(1), W0), lo(M, W0));
4000b57cec5SDimitry Andric return rr0(RC, Outputs);
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric case M4_mpyri_addr_u2: {
4030b57cec5SDimitry Andric RegisterCell M = eMLS(eIMM(im(2), W0), rc(3));
4040b57cec5SDimitry Andric RegisterCell RC = eADD(rc(1), lo(M, W0));
4050b57cec5SDimitry Andric return rr0(RC, Outputs);
4060b57cec5SDimitry Andric }
4070b57cec5SDimitry Andric case M4_mpyri_addr: {
4080b57cec5SDimitry Andric RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
4090b57cec5SDimitry Andric RegisterCell RC = eADD(rc(1), lo(M, W0));
4100b57cec5SDimitry Andric return rr0(RC, Outputs);
4110b57cec5SDimitry Andric }
4120b57cec5SDimitry Andric case M4_mpyrr_addr: {
4130b57cec5SDimitry Andric RegisterCell M = eMLS(rc(2), rc(3));
4140b57cec5SDimitry Andric RegisterCell RC = eADD(rc(1), lo(M, W0));
4150b57cec5SDimitry Andric return rr0(RC, Outputs);
4160b57cec5SDimitry Andric }
4170b57cec5SDimitry Andric case S4_subaddi: {
4180b57cec5SDimitry Andric RegisterCell RC = eADD(rc(1), eSUB(eIMM(im(2), W0), rc(3)));
4190b57cec5SDimitry Andric return rr0(RC, Outputs);
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric case M2_accii: {
4220b57cec5SDimitry Andric RegisterCell RC = eADD(rc(1), eADD(rc(2), eIMM(im(3), W0)));
4230b57cec5SDimitry Andric return rr0(RC, Outputs);
4240b57cec5SDimitry Andric }
4250b57cec5SDimitry Andric case M2_acci: {
4260b57cec5SDimitry Andric RegisterCell RC = eADD(rc(1), eADD(rc(2), rc(3)));
4270b57cec5SDimitry Andric return rr0(RC, Outputs);
4280b57cec5SDimitry Andric }
4290b57cec5SDimitry Andric case M2_subacc: {
4300b57cec5SDimitry Andric RegisterCell RC = eADD(rc(1), eSUB(rc(2), rc(3)));
4310b57cec5SDimitry Andric return rr0(RC, Outputs);
4320b57cec5SDimitry Andric }
4330b57cec5SDimitry Andric case S2_addasl_rrri: {
4340b57cec5SDimitry Andric RegisterCell RC = eADD(rc(1), eASL(rc(2), im(3)));
4350b57cec5SDimitry Andric return rr0(RC, Outputs);
4360b57cec5SDimitry Andric }
4370b57cec5SDimitry Andric case C4_addipc: {
4380b57cec5SDimitry Andric RegisterCell RPC = RegisterCell::self(Reg[0].Reg, W0);
4390b57cec5SDimitry Andric RPC.fill(0, 2, BT::BitValue::Zero);
4400b57cec5SDimitry Andric return rr0(eADD(RPC, eIMM(im(2), W0)), Outputs);
4410b57cec5SDimitry Andric }
4420b57cec5SDimitry Andric case A2_sub:
4430b57cec5SDimitry Andric case A2_subp:
4440b57cec5SDimitry Andric return rr0(eSUB(rc(1), rc(2)), Outputs);
4450b57cec5SDimitry Andric case A2_subri:
4460b57cec5SDimitry Andric return rr0(eSUB(eIMM(im(1), W0), rc(2)), Outputs);
4470b57cec5SDimitry Andric case S4_subi_asl_ri: {
4480b57cec5SDimitry Andric RegisterCell RC = eSUB(eIMM(im(1), W0), eASL(rc(2), im(3)));
4490b57cec5SDimitry Andric return rr0(RC, Outputs);
4500b57cec5SDimitry Andric }
4510b57cec5SDimitry Andric case S4_subi_lsr_ri: {
4520b57cec5SDimitry Andric RegisterCell RC = eSUB(eIMM(im(1), W0), eLSR(rc(2), im(3)));
4530b57cec5SDimitry Andric return rr0(RC, Outputs);
4540b57cec5SDimitry Andric }
4550b57cec5SDimitry Andric case M2_naccii: {
4560b57cec5SDimitry Andric RegisterCell RC = eSUB(rc(1), eADD(rc(2), eIMM(im(3), W0)));
4570b57cec5SDimitry Andric return rr0(RC, Outputs);
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric case M2_nacci: {
4600b57cec5SDimitry Andric RegisterCell RC = eSUB(rc(1), eADD(rc(2), rc(3)));
4610b57cec5SDimitry Andric return rr0(RC, Outputs);
4620b57cec5SDimitry Andric }
4630b57cec5SDimitry Andric // 32-bit negation is done by "Rd = A2_subri 0, Rs"
4640b57cec5SDimitry Andric case A2_negp:
4650b57cec5SDimitry Andric return rr0(eSUB(eIMM(0, W0), rc(1)), Outputs);
4660b57cec5SDimitry Andric
4670b57cec5SDimitry Andric case M2_mpy_up: {
4680b57cec5SDimitry Andric RegisterCell M = eMLS(rc(1), rc(2));
4690b57cec5SDimitry Andric return rr0(hi(M, W0), Outputs);
4700b57cec5SDimitry Andric }
4710b57cec5SDimitry Andric case M2_dpmpyss_s0:
4720b57cec5SDimitry Andric return rr0(eMLS(rc(1), rc(2)), Outputs);
4730b57cec5SDimitry Andric case M2_dpmpyss_acc_s0:
4740b57cec5SDimitry Andric return rr0(eADD(rc(1), eMLS(rc(2), rc(3))), Outputs);
4750b57cec5SDimitry Andric case M2_dpmpyss_nac_s0:
4760b57cec5SDimitry Andric return rr0(eSUB(rc(1), eMLS(rc(2), rc(3))), Outputs);
4770b57cec5SDimitry Andric case M2_mpyi: {
4780b57cec5SDimitry Andric RegisterCell M = eMLS(rc(1), rc(2));
4790b57cec5SDimitry Andric return rr0(lo(M, W0), Outputs);
4800b57cec5SDimitry Andric }
4810b57cec5SDimitry Andric case M2_macsip: {
4820b57cec5SDimitry Andric RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
4830b57cec5SDimitry Andric RegisterCell RC = eADD(rc(1), lo(M, W0));
4840b57cec5SDimitry Andric return rr0(RC, Outputs);
4850b57cec5SDimitry Andric }
4860b57cec5SDimitry Andric case M2_macsin: {
4870b57cec5SDimitry Andric RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
4880b57cec5SDimitry Andric RegisterCell RC = eSUB(rc(1), lo(M, W0));
4890b57cec5SDimitry Andric return rr0(RC, Outputs);
4900b57cec5SDimitry Andric }
4910b57cec5SDimitry Andric case M2_maci: {
4920b57cec5SDimitry Andric RegisterCell M = eMLS(rc(2), rc(3));
4930b57cec5SDimitry Andric RegisterCell RC = eADD(rc(1), lo(M, W0));
4940b57cec5SDimitry Andric return rr0(RC, Outputs);
4950b57cec5SDimitry Andric }
49604eeddc0SDimitry Andric case M2_mnaci: {
49704eeddc0SDimitry Andric RegisterCell M = eMLS(rc(2), rc(3));
49804eeddc0SDimitry Andric RegisterCell RC = eSUB(rc(1), lo(M, W0));
49904eeddc0SDimitry Andric return rr0(RC, Outputs);
50004eeddc0SDimitry Andric }
5010b57cec5SDimitry Andric case M2_mpysmi: {
5020b57cec5SDimitry Andric RegisterCell M = eMLS(rc(1), eIMM(im(2), W0));
5030b57cec5SDimitry Andric return rr0(lo(M, 32), Outputs);
5040b57cec5SDimitry Andric }
5050b57cec5SDimitry Andric case M2_mpysin: {
5060b57cec5SDimitry Andric RegisterCell M = eMLS(rc(1), eIMM(-im(2), W0));
5070b57cec5SDimitry Andric return rr0(lo(M, 32), Outputs);
5080b57cec5SDimitry Andric }
5090b57cec5SDimitry Andric case M2_mpysip: {
5100b57cec5SDimitry Andric RegisterCell M = eMLS(rc(1), eIMM(im(2), W0));
5110b57cec5SDimitry Andric return rr0(lo(M, 32), Outputs);
5120b57cec5SDimitry Andric }
5130b57cec5SDimitry Andric case M2_mpyu_up: {
5140b57cec5SDimitry Andric RegisterCell M = eMLU(rc(1), rc(2));
5150b57cec5SDimitry Andric return rr0(hi(M, W0), Outputs);
5160b57cec5SDimitry Andric }
5170b57cec5SDimitry Andric case M2_dpmpyuu_s0:
5180b57cec5SDimitry Andric return rr0(eMLU(rc(1), rc(2)), Outputs);
5190b57cec5SDimitry Andric case M2_dpmpyuu_acc_s0:
5200b57cec5SDimitry Andric return rr0(eADD(rc(1), eMLU(rc(2), rc(3))), Outputs);
5210b57cec5SDimitry Andric case M2_dpmpyuu_nac_s0:
5220b57cec5SDimitry Andric return rr0(eSUB(rc(1), eMLU(rc(2), rc(3))), Outputs);
5230b57cec5SDimitry Andric //case M2_mpysu_up:
5240b57cec5SDimitry Andric
5250b57cec5SDimitry Andric // Logical/bitwise:
5260b57cec5SDimitry Andric
5270b57cec5SDimitry Andric case A2_andir:
5280b57cec5SDimitry Andric return rr0(eAND(rc(1), eIMM(im(2), W0)), Outputs);
5290b57cec5SDimitry Andric case A2_and:
5300b57cec5SDimitry Andric case A2_andp:
5310b57cec5SDimitry Andric return rr0(eAND(rc(1), rc(2)), Outputs);
5320b57cec5SDimitry Andric case A4_andn:
5330b57cec5SDimitry Andric case A4_andnp:
5340b57cec5SDimitry Andric return rr0(eAND(rc(1), eNOT(rc(2))), Outputs);
5350b57cec5SDimitry Andric case S4_andi_asl_ri: {
5360b57cec5SDimitry Andric RegisterCell RC = eAND(eIMM(im(1), W0), eASL(rc(2), im(3)));
5370b57cec5SDimitry Andric return rr0(RC, Outputs);
5380b57cec5SDimitry Andric }
5390b57cec5SDimitry Andric case S4_andi_lsr_ri: {
5400b57cec5SDimitry Andric RegisterCell RC = eAND(eIMM(im(1), W0), eLSR(rc(2), im(3)));
5410b57cec5SDimitry Andric return rr0(RC, Outputs);
5420b57cec5SDimitry Andric }
5430b57cec5SDimitry Andric case M4_and_and:
5440b57cec5SDimitry Andric return rr0(eAND(rc(1), eAND(rc(2), rc(3))), Outputs);
5450b57cec5SDimitry Andric case M4_and_andn:
5460b57cec5SDimitry Andric return rr0(eAND(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
5470b57cec5SDimitry Andric case M4_and_or:
5480b57cec5SDimitry Andric return rr0(eAND(rc(1), eORL(rc(2), rc(3))), Outputs);
5490b57cec5SDimitry Andric case M4_and_xor:
5500b57cec5SDimitry Andric return rr0(eAND(rc(1), eXOR(rc(2), rc(3))), Outputs);
5510b57cec5SDimitry Andric case A2_orir:
5520b57cec5SDimitry Andric return rr0(eORL(rc(1), eIMM(im(2), W0)), Outputs);
5530b57cec5SDimitry Andric case A2_or:
5540b57cec5SDimitry Andric case A2_orp:
5550b57cec5SDimitry Andric return rr0(eORL(rc(1), rc(2)), Outputs);
5560b57cec5SDimitry Andric case A4_orn:
5570b57cec5SDimitry Andric case A4_ornp:
5580b57cec5SDimitry Andric return rr0(eORL(rc(1), eNOT(rc(2))), Outputs);
5590b57cec5SDimitry Andric case S4_ori_asl_ri: {
5600b57cec5SDimitry Andric RegisterCell RC = eORL(eIMM(im(1), W0), eASL(rc(2), im(3)));
5610b57cec5SDimitry Andric return rr0(RC, Outputs);
5620b57cec5SDimitry Andric }
5630b57cec5SDimitry Andric case S4_ori_lsr_ri: {
5640b57cec5SDimitry Andric RegisterCell RC = eORL(eIMM(im(1), W0), eLSR(rc(2), im(3)));
5650b57cec5SDimitry Andric return rr0(RC, Outputs);
5660b57cec5SDimitry Andric }
5670b57cec5SDimitry Andric case M4_or_and:
5680b57cec5SDimitry Andric return rr0(eORL(rc(1), eAND(rc(2), rc(3))), Outputs);
5690b57cec5SDimitry Andric case M4_or_andn:
5700b57cec5SDimitry Andric return rr0(eORL(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
5710b57cec5SDimitry Andric case S4_or_andi:
5720b57cec5SDimitry Andric case S4_or_andix: {
5730b57cec5SDimitry Andric RegisterCell RC = eORL(rc(1), eAND(rc(2), eIMM(im(3), W0)));
5740b57cec5SDimitry Andric return rr0(RC, Outputs);
5750b57cec5SDimitry Andric }
5760b57cec5SDimitry Andric case S4_or_ori: {
5770b57cec5SDimitry Andric RegisterCell RC = eORL(rc(1), eORL(rc(2), eIMM(im(3), W0)));
5780b57cec5SDimitry Andric return rr0(RC, Outputs);
5790b57cec5SDimitry Andric }
5800b57cec5SDimitry Andric case M4_or_or:
5810b57cec5SDimitry Andric return rr0(eORL(rc(1), eORL(rc(2), rc(3))), Outputs);
5820b57cec5SDimitry Andric case M4_or_xor:
5830b57cec5SDimitry Andric return rr0(eORL(rc(1), eXOR(rc(2), rc(3))), Outputs);
5840b57cec5SDimitry Andric case A2_xor:
5850b57cec5SDimitry Andric case A2_xorp:
5860b57cec5SDimitry Andric return rr0(eXOR(rc(1), rc(2)), Outputs);
5870b57cec5SDimitry Andric case M4_xor_and:
5880b57cec5SDimitry Andric return rr0(eXOR(rc(1), eAND(rc(2), rc(3))), Outputs);
5890b57cec5SDimitry Andric case M4_xor_andn:
5900b57cec5SDimitry Andric return rr0(eXOR(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
5910b57cec5SDimitry Andric case M4_xor_or:
5920b57cec5SDimitry Andric return rr0(eXOR(rc(1), eORL(rc(2), rc(3))), Outputs);
5930b57cec5SDimitry Andric case M4_xor_xacc:
5940b57cec5SDimitry Andric return rr0(eXOR(rc(1), eXOR(rc(2), rc(3))), Outputs);
5950b57cec5SDimitry Andric case A2_not:
5960b57cec5SDimitry Andric case A2_notp:
5970b57cec5SDimitry Andric return rr0(eNOT(rc(1)), Outputs);
5980b57cec5SDimitry Andric
5990b57cec5SDimitry Andric case S2_asl_i_r:
6000b57cec5SDimitry Andric case S2_asl_i_p:
6010b57cec5SDimitry Andric return rr0(eASL(rc(1), im(2)), Outputs);
6020b57cec5SDimitry Andric case A2_aslh:
6030b57cec5SDimitry Andric return rr0(eASL(rc(1), 16), Outputs);
6040b57cec5SDimitry Andric case S2_asl_i_r_acc:
6050b57cec5SDimitry Andric case S2_asl_i_p_acc:
6060b57cec5SDimitry Andric return rr0(eADD(rc(1), eASL(rc(2), im(3))), Outputs);
6070b57cec5SDimitry Andric case S2_asl_i_r_nac:
6080b57cec5SDimitry Andric case S2_asl_i_p_nac:
6090b57cec5SDimitry Andric return rr0(eSUB(rc(1), eASL(rc(2), im(3))), Outputs);
6100b57cec5SDimitry Andric case S2_asl_i_r_and:
6110b57cec5SDimitry Andric case S2_asl_i_p_and:
6120b57cec5SDimitry Andric return rr0(eAND(rc(1), eASL(rc(2), im(3))), Outputs);
6130b57cec5SDimitry Andric case S2_asl_i_r_or:
6140b57cec5SDimitry Andric case S2_asl_i_p_or:
6150b57cec5SDimitry Andric return rr0(eORL(rc(1), eASL(rc(2), im(3))), Outputs);
6160b57cec5SDimitry Andric case S2_asl_i_r_xacc:
6170b57cec5SDimitry Andric case S2_asl_i_p_xacc:
6180b57cec5SDimitry Andric return rr0(eXOR(rc(1), eASL(rc(2), im(3))), Outputs);
6190b57cec5SDimitry Andric case S2_asl_i_vh:
6200b57cec5SDimitry Andric case S2_asl_i_vw:
6210b57cec5SDimitry Andric // TODO
6220b57cec5SDimitry Andric break;
6230b57cec5SDimitry Andric
6240b57cec5SDimitry Andric case S2_asr_i_r:
6250b57cec5SDimitry Andric case S2_asr_i_p:
6260b57cec5SDimitry Andric return rr0(eASR(rc(1), im(2)), Outputs);
6270b57cec5SDimitry Andric case A2_asrh:
6280b57cec5SDimitry Andric return rr0(eASR(rc(1), 16), Outputs);
6290b57cec5SDimitry Andric case S2_asr_i_r_acc:
6300b57cec5SDimitry Andric case S2_asr_i_p_acc:
6310b57cec5SDimitry Andric return rr0(eADD(rc(1), eASR(rc(2), im(3))), Outputs);
6320b57cec5SDimitry Andric case S2_asr_i_r_nac:
6330b57cec5SDimitry Andric case S2_asr_i_p_nac:
6340b57cec5SDimitry Andric return rr0(eSUB(rc(1), eASR(rc(2), im(3))), Outputs);
6350b57cec5SDimitry Andric case S2_asr_i_r_and:
6360b57cec5SDimitry Andric case S2_asr_i_p_and:
6370b57cec5SDimitry Andric return rr0(eAND(rc(1), eASR(rc(2), im(3))), Outputs);
6380b57cec5SDimitry Andric case S2_asr_i_r_or:
6390b57cec5SDimitry Andric case S2_asr_i_p_or:
6400b57cec5SDimitry Andric return rr0(eORL(rc(1), eASR(rc(2), im(3))), Outputs);
6410b57cec5SDimitry Andric case S2_asr_i_r_rnd: {
6420b57cec5SDimitry Andric // The input is first sign-extended to 64 bits, then the output
6430b57cec5SDimitry Andric // is truncated back to 32 bits.
6440b57cec5SDimitry Andric assert(W0 == 32);
6450b57cec5SDimitry Andric RegisterCell XC = eSXT(rc(1).cat(eIMM(0, W0)), W0);
6460b57cec5SDimitry Andric RegisterCell RC = eASR(eADD(eASR(XC, im(2)), eIMM(1, 2*W0)), 1);
6470b57cec5SDimitry Andric return rr0(eXTR(RC, 0, W0), Outputs);
6480b57cec5SDimitry Andric }
6490b57cec5SDimitry Andric case S2_asr_i_r_rnd_goodsyntax: {
6500b57cec5SDimitry Andric int64_t S = im(2);
6510b57cec5SDimitry Andric if (S == 0)
6520b57cec5SDimitry Andric return rr0(rc(1), Outputs);
6530b57cec5SDimitry Andric // Result: S2_asr_i_r_rnd Rs, u5-1
6540b57cec5SDimitry Andric RegisterCell XC = eSXT(rc(1).cat(eIMM(0, W0)), W0);
6550b57cec5SDimitry Andric RegisterCell RC = eLSR(eADD(eASR(XC, S-1), eIMM(1, 2*W0)), 1);
6560b57cec5SDimitry Andric return rr0(eXTR(RC, 0, W0), Outputs);
6570b57cec5SDimitry Andric }
6580b57cec5SDimitry Andric case S2_asr_r_vh:
6590b57cec5SDimitry Andric case S2_asr_i_vw:
6600b57cec5SDimitry Andric case S2_asr_i_svw_trun:
6610b57cec5SDimitry Andric // TODO
6620b57cec5SDimitry Andric break;
6630b57cec5SDimitry Andric
6640b57cec5SDimitry Andric case S2_lsr_i_r:
6650b57cec5SDimitry Andric case S2_lsr_i_p:
6660b57cec5SDimitry Andric return rr0(eLSR(rc(1), im(2)), Outputs);
6670b57cec5SDimitry Andric case S2_lsr_i_r_acc:
6680b57cec5SDimitry Andric case S2_lsr_i_p_acc:
6690b57cec5SDimitry Andric return rr0(eADD(rc(1), eLSR(rc(2), im(3))), Outputs);
6700b57cec5SDimitry Andric case S2_lsr_i_r_nac:
6710b57cec5SDimitry Andric case S2_lsr_i_p_nac:
6720b57cec5SDimitry Andric return rr0(eSUB(rc(1), eLSR(rc(2), im(3))), Outputs);
6730b57cec5SDimitry Andric case S2_lsr_i_r_and:
6740b57cec5SDimitry Andric case S2_lsr_i_p_and:
6750b57cec5SDimitry Andric return rr0(eAND(rc(1), eLSR(rc(2), im(3))), Outputs);
6760b57cec5SDimitry Andric case S2_lsr_i_r_or:
6770b57cec5SDimitry Andric case S2_lsr_i_p_or:
6780b57cec5SDimitry Andric return rr0(eORL(rc(1), eLSR(rc(2), im(3))), Outputs);
6790b57cec5SDimitry Andric case S2_lsr_i_r_xacc:
6800b57cec5SDimitry Andric case S2_lsr_i_p_xacc:
6810b57cec5SDimitry Andric return rr0(eXOR(rc(1), eLSR(rc(2), im(3))), Outputs);
6820b57cec5SDimitry Andric
6830b57cec5SDimitry Andric case S2_clrbit_i: {
6840b57cec5SDimitry Andric RegisterCell RC = rc(1);
6850b57cec5SDimitry Andric RC[im(2)] = BT::BitValue::Zero;
6860b57cec5SDimitry Andric return rr0(RC, Outputs);
6870b57cec5SDimitry Andric }
6880b57cec5SDimitry Andric case S2_setbit_i: {
6890b57cec5SDimitry Andric RegisterCell RC = rc(1);
6900b57cec5SDimitry Andric RC[im(2)] = BT::BitValue::One;
6910b57cec5SDimitry Andric return rr0(RC, Outputs);
6920b57cec5SDimitry Andric }
6930b57cec5SDimitry Andric case S2_togglebit_i: {
6940b57cec5SDimitry Andric RegisterCell RC = rc(1);
6950b57cec5SDimitry Andric uint16_t BX = im(2);
6960b57cec5SDimitry Andric RC[BX] = RC[BX].is(0) ? BT::BitValue::One
6970b57cec5SDimitry Andric : RC[BX].is(1) ? BT::BitValue::Zero
6980b57cec5SDimitry Andric : BT::BitValue::self();
6990b57cec5SDimitry Andric return rr0(RC, Outputs);
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric
7020b57cec5SDimitry Andric case A4_bitspliti: {
7030b57cec5SDimitry Andric uint16_t W1 = getRegBitWidth(Reg[1]);
7040b57cec5SDimitry Andric uint16_t BX = im(2);
7050b57cec5SDimitry Andric // Res.uw[1] = Rs[bx+1:], Res.uw[0] = Rs[0:bx]
7060b57cec5SDimitry Andric const BT::BitValue Zero = BT::BitValue::Zero;
7070b57cec5SDimitry Andric RegisterCell RZ = RegisterCell(W0).fill(BX, W1, Zero)
7080b57cec5SDimitry Andric .fill(W1+(W1-BX), W0, Zero);
7090b57cec5SDimitry Andric RegisterCell BF1 = eXTR(rc(1), 0, BX), BF2 = eXTR(rc(1), BX, W1);
7100b57cec5SDimitry Andric RegisterCell RC = eINS(eINS(RZ, BF1, 0), BF2, W1);
7110b57cec5SDimitry Andric return rr0(RC, Outputs);
7120b57cec5SDimitry Andric }
7130b57cec5SDimitry Andric case S4_extract:
7140b57cec5SDimitry Andric case S4_extractp:
7150b57cec5SDimitry Andric case S2_extractu:
7160b57cec5SDimitry Andric case S2_extractup: {
7170b57cec5SDimitry Andric uint16_t Wd = im(2), Of = im(3);
7180b57cec5SDimitry Andric assert(Wd <= W0);
7190b57cec5SDimitry Andric if (Wd == 0)
7200b57cec5SDimitry Andric return rr0(eIMM(0, W0), Outputs);
7210b57cec5SDimitry Andric // If the width extends beyond the register size, pad the register
7220b57cec5SDimitry Andric // with 0 bits.
7230b57cec5SDimitry Andric RegisterCell Pad = (Wd+Of > W0) ? rc(1).cat(eIMM(0, Wd+Of-W0)) : rc(1);
7240b57cec5SDimitry Andric RegisterCell Ext = eXTR(Pad, Of, Wd+Of);
7250b57cec5SDimitry Andric // Ext is short, need to extend it with 0s or sign bit.
7260b57cec5SDimitry Andric RegisterCell RC = RegisterCell(W0).insert(Ext, BT::BitMask(0, Wd-1));
7270b57cec5SDimitry Andric if (Opc == S2_extractu || Opc == S2_extractup)
7280b57cec5SDimitry Andric return rr0(eZXT(RC, Wd), Outputs);
7290b57cec5SDimitry Andric return rr0(eSXT(RC, Wd), Outputs);
7300b57cec5SDimitry Andric }
7310b57cec5SDimitry Andric case S2_insert:
7320b57cec5SDimitry Andric case S2_insertp: {
7330b57cec5SDimitry Andric uint16_t Wd = im(3), Of = im(4);
7340b57cec5SDimitry Andric assert(Wd < W0 && Of < W0);
7350b57cec5SDimitry Andric // If Wd+Of exceeds W0, the inserted bits are truncated.
7360b57cec5SDimitry Andric if (Wd+Of > W0)
7370b57cec5SDimitry Andric Wd = W0-Of;
7380b57cec5SDimitry Andric if (Wd == 0)
7390b57cec5SDimitry Andric return rr0(rc(1), Outputs);
7400b57cec5SDimitry Andric return rr0(eINS(rc(1), eXTR(rc(2), 0, Wd), Of), Outputs);
7410b57cec5SDimitry Andric }
7420b57cec5SDimitry Andric
7430b57cec5SDimitry Andric // Bit permutations:
7440b57cec5SDimitry Andric
7450b57cec5SDimitry Andric case A2_combineii:
7460b57cec5SDimitry Andric case A4_combineii:
7470b57cec5SDimitry Andric case A4_combineir:
7480b57cec5SDimitry Andric case A4_combineri:
7490b57cec5SDimitry Andric case A2_combinew:
7500b57cec5SDimitry Andric case V6_vcombine:
7510b57cec5SDimitry Andric assert(W0 % 2 == 0);
7520b57cec5SDimitry Andric return rr0(cop(2, W0/2).cat(cop(1, W0/2)), Outputs);
7530b57cec5SDimitry Andric case A2_combine_ll:
7540b57cec5SDimitry Andric case A2_combine_lh:
7550b57cec5SDimitry Andric case A2_combine_hl:
7560b57cec5SDimitry Andric case A2_combine_hh: {
7570b57cec5SDimitry Andric assert(W0 == 32);
7580b57cec5SDimitry Andric assert(getRegBitWidth(Reg[1]) == 32 && getRegBitWidth(Reg[2]) == 32);
7590b57cec5SDimitry Andric // Low half in the output is 0 for _ll and _hl, 1 otherwise:
7600b57cec5SDimitry Andric unsigned LoH = !(Opc == A2_combine_ll || Opc == A2_combine_hl);
7610b57cec5SDimitry Andric // High half in the output is 0 for _ll and _lh, 1 otherwise:
7620b57cec5SDimitry Andric unsigned HiH = !(Opc == A2_combine_ll || Opc == A2_combine_lh);
7630b57cec5SDimitry Andric RegisterCell R1 = rc(1);
7640b57cec5SDimitry Andric RegisterCell R2 = rc(2);
7650b57cec5SDimitry Andric RegisterCell RC = half(R2, LoH).cat(half(R1, HiH));
7660b57cec5SDimitry Andric return rr0(RC, Outputs);
7670b57cec5SDimitry Andric }
7680b57cec5SDimitry Andric case S2_packhl: {
7690b57cec5SDimitry Andric assert(W0 == 64);
7700b57cec5SDimitry Andric assert(getRegBitWidth(Reg[1]) == 32 && getRegBitWidth(Reg[2]) == 32);
7710b57cec5SDimitry Andric RegisterCell R1 = rc(1);
7720b57cec5SDimitry Andric RegisterCell R2 = rc(2);
7730b57cec5SDimitry Andric RegisterCell RC = half(R2, 0).cat(half(R1, 0)).cat(half(R2, 1))
7740b57cec5SDimitry Andric .cat(half(R1, 1));
7750b57cec5SDimitry Andric return rr0(RC, Outputs);
7760b57cec5SDimitry Andric }
7770b57cec5SDimitry Andric case S2_shuffeb: {
7780b57cec5SDimitry Andric RegisterCell RC = shuffle(rc(1), rc(2), 8, false);
7790b57cec5SDimitry Andric return rr0(RC, Outputs);
7800b57cec5SDimitry Andric }
7810b57cec5SDimitry Andric case S2_shuffeh: {
7820b57cec5SDimitry Andric RegisterCell RC = shuffle(rc(1), rc(2), 16, false);
7830b57cec5SDimitry Andric return rr0(RC, Outputs);
7840b57cec5SDimitry Andric }
7850b57cec5SDimitry Andric case S2_shuffob: {
7860b57cec5SDimitry Andric RegisterCell RC = shuffle(rc(1), rc(2), 8, true);
7870b57cec5SDimitry Andric return rr0(RC, Outputs);
7880b57cec5SDimitry Andric }
7890b57cec5SDimitry Andric case S2_shuffoh: {
7900b57cec5SDimitry Andric RegisterCell RC = shuffle(rc(1), rc(2), 16, true);
7910b57cec5SDimitry Andric return rr0(RC, Outputs);
7920b57cec5SDimitry Andric }
7930b57cec5SDimitry Andric case C2_mask: {
7940b57cec5SDimitry Andric uint16_t WR = W0;
7950b57cec5SDimitry Andric uint16_t WP = 8; // XXX Pred size: getRegBitWidth(Reg[1]);
7960b57cec5SDimitry Andric assert(WR == 64 && WP == 8);
7970b57cec5SDimitry Andric RegisterCell R1 = rc(1);
7980b57cec5SDimitry Andric RegisterCell RC(WR);
7990b57cec5SDimitry Andric for (uint16_t i = 0; i < WP; ++i) {
8000b57cec5SDimitry Andric const BT::BitValue &V = R1[i];
8010b57cec5SDimitry Andric BT::BitValue F = (V.is(0) || V.is(1)) ? V : BT::BitValue::self();
8020b57cec5SDimitry Andric RC.fill(i*8, i*8+8, F);
8030b57cec5SDimitry Andric }
8040b57cec5SDimitry Andric return rr0(RC, Outputs);
8050b57cec5SDimitry Andric }
8060b57cec5SDimitry Andric
8070b57cec5SDimitry Andric // Mux:
8080b57cec5SDimitry Andric
8090b57cec5SDimitry Andric case C2_muxii:
8100b57cec5SDimitry Andric case C2_muxir:
8110b57cec5SDimitry Andric case C2_muxri:
8120b57cec5SDimitry Andric case C2_mux: {
8130b57cec5SDimitry Andric BT::BitValue PC0 = rc(1)[0];
8140b57cec5SDimitry Andric RegisterCell R2 = cop(2, W0);
8150b57cec5SDimitry Andric RegisterCell R3 = cop(3, W0);
8160b57cec5SDimitry Andric if (PC0.is(0) || PC0.is(1))
8170b57cec5SDimitry Andric return rr0(RegisterCell::ref(PC0 ? R2 : R3), Outputs);
8180b57cec5SDimitry Andric R2.meet(R3, Reg[0].Reg);
8190b57cec5SDimitry Andric return rr0(R2, Outputs);
8200b57cec5SDimitry Andric }
8210b57cec5SDimitry Andric case C2_vmux:
8220b57cec5SDimitry Andric // TODO
8230b57cec5SDimitry Andric break;
8240b57cec5SDimitry Andric
8250b57cec5SDimitry Andric // Sign- and zero-extension:
8260b57cec5SDimitry Andric
8270b57cec5SDimitry Andric case A2_sxtb:
8280b57cec5SDimitry Andric return rr0(eSXT(rc(1), 8), Outputs);
8290b57cec5SDimitry Andric case A2_sxth:
8300b57cec5SDimitry Andric return rr0(eSXT(rc(1), 16), Outputs);
8310b57cec5SDimitry Andric case A2_sxtw: {
8320b57cec5SDimitry Andric uint16_t W1 = getRegBitWidth(Reg[1]);
8330b57cec5SDimitry Andric assert(W0 == 64 && W1 == 32);
8340b57cec5SDimitry Andric RegisterCell RC = eSXT(rc(1).cat(eIMM(0, W1)), W1);
8350b57cec5SDimitry Andric return rr0(RC, Outputs);
8360b57cec5SDimitry Andric }
8370b57cec5SDimitry Andric case A2_zxtb:
8380b57cec5SDimitry Andric return rr0(eZXT(rc(1), 8), Outputs);
8390b57cec5SDimitry Andric case A2_zxth:
8400b57cec5SDimitry Andric return rr0(eZXT(rc(1), 16), Outputs);
8410b57cec5SDimitry Andric
8420b57cec5SDimitry Andric // Saturations
8430b57cec5SDimitry Andric
8440b57cec5SDimitry Andric case A2_satb:
8450b57cec5SDimitry Andric return rr0(eSXT(RegisterCell::self(0, W0).regify(Reg0), 8), Outputs);
8460b57cec5SDimitry Andric case A2_sath:
8470b57cec5SDimitry Andric return rr0(eSXT(RegisterCell::self(0, W0).regify(Reg0), 16), Outputs);
8480b57cec5SDimitry Andric case A2_satub:
8490b57cec5SDimitry Andric return rr0(eZXT(RegisterCell::self(0, W0).regify(Reg0), 8), Outputs);
8500b57cec5SDimitry Andric case A2_satuh:
8510b57cec5SDimitry Andric return rr0(eZXT(RegisterCell::self(0, W0).regify(Reg0), 16), Outputs);
8520b57cec5SDimitry Andric
8530b57cec5SDimitry Andric // Bit count:
8540b57cec5SDimitry Andric
8550b57cec5SDimitry Andric case S2_cl0:
8560b57cec5SDimitry Andric case S2_cl0p:
8570b57cec5SDimitry Andric // Always produce a 32-bit result.
8580b57cec5SDimitry Andric return rr0(eCLB(rc(1), false/*bit*/, 32), Outputs);
8590b57cec5SDimitry Andric case S2_cl1:
8600b57cec5SDimitry Andric case S2_cl1p:
8610b57cec5SDimitry Andric return rr0(eCLB(rc(1), true/*bit*/, 32), Outputs);
8620b57cec5SDimitry Andric case S2_clb:
8630b57cec5SDimitry Andric case S2_clbp: {
8640b57cec5SDimitry Andric uint16_t W1 = getRegBitWidth(Reg[1]);
8650b57cec5SDimitry Andric RegisterCell R1 = rc(1);
8660b57cec5SDimitry Andric BT::BitValue TV = R1[W1-1];
8670b57cec5SDimitry Andric if (TV.is(0) || TV.is(1))
8680b57cec5SDimitry Andric return rr0(eCLB(R1, TV, 32), Outputs);
8690b57cec5SDimitry Andric break;
8700b57cec5SDimitry Andric }
8710b57cec5SDimitry Andric case S2_ct0:
8720b57cec5SDimitry Andric case S2_ct0p:
8730b57cec5SDimitry Andric return rr0(eCTB(rc(1), false/*bit*/, 32), Outputs);
8740b57cec5SDimitry Andric case S2_ct1:
8750b57cec5SDimitry Andric case S2_ct1p:
8760b57cec5SDimitry Andric return rr0(eCTB(rc(1), true/*bit*/, 32), Outputs);
8770b57cec5SDimitry Andric case S5_popcountp:
8780b57cec5SDimitry Andric // TODO
8790b57cec5SDimitry Andric break;
8800b57cec5SDimitry Andric
8810b57cec5SDimitry Andric case C2_all8: {
8820b57cec5SDimitry Andric RegisterCell P1 = rc(1);
8830b57cec5SDimitry Andric bool Has0 = false, All1 = true;
8840b57cec5SDimitry Andric for (uint16_t i = 0; i < 8/*XXX*/; ++i) {
8850b57cec5SDimitry Andric if (!P1[i].is(1))
8860b57cec5SDimitry Andric All1 = false;
8870b57cec5SDimitry Andric if (!P1[i].is(0))
8880b57cec5SDimitry Andric continue;
8890b57cec5SDimitry Andric Has0 = true;
8900b57cec5SDimitry Andric break;
8910b57cec5SDimitry Andric }
8920b57cec5SDimitry Andric if (!Has0 && !All1)
8930b57cec5SDimitry Andric break;
8940b57cec5SDimitry Andric RegisterCell RC(W0);
8950b57cec5SDimitry Andric RC.fill(0, W0, (All1 ? BT::BitValue::One : BT::BitValue::Zero));
8960b57cec5SDimitry Andric return rr0(RC, Outputs);
8970b57cec5SDimitry Andric }
8980b57cec5SDimitry Andric case C2_any8: {
8990b57cec5SDimitry Andric RegisterCell P1 = rc(1);
9000b57cec5SDimitry Andric bool Has1 = false, All0 = true;
9010b57cec5SDimitry Andric for (uint16_t i = 0; i < 8/*XXX*/; ++i) {
9020b57cec5SDimitry Andric if (!P1[i].is(0))
9030b57cec5SDimitry Andric All0 = false;
9040b57cec5SDimitry Andric if (!P1[i].is(1))
9050b57cec5SDimitry Andric continue;
9060b57cec5SDimitry Andric Has1 = true;
9070b57cec5SDimitry Andric break;
9080b57cec5SDimitry Andric }
9090b57cec5SDimitry Andric if (!Has1 && !All0)
9100b57cec5SDimitry Andric break;
9110b57cec5SDimitry Andric RegisterCell RC(W0);
9120b57cec5SDimitry Andric RC.fill(0, W0, (Has1 ? BT::BitValue::One : BT::BitValue::Zero));
9130b57cec5SDimitry Andric return rr0(RC, Outputs);
9140b57cec5SDimitry Andric }
9150b57cec5SDimitry Andric case C2_and:
9160b57cec5SDimitry Andric return rr0(eAND(rc(1), rc(2)), Outputs);
9170b57cec5SDimitry Andric case C2_andn:
9180b57cec5SDimitry Andric return rr0(eAND(rc(1), eNOT(rc(2))), Outputs);
9190b57cec5SDimitry Andric case C2_not:
9200b57cec5SDimitry Andric return rr0(eNOT(rc(1)), Outputs);
9210b57cec5SDimitry Andric case C2_or:
9220b57cec5SDimitry Andric return rr0(eORL(rc(1), rc(2)), Outputs);
9230b57cec5SDimitry Andric case C2_orn:
9240b57cec5SDimitry Andric return rr0(eORL(rc(1), eNOT(rc(2))), Outputs);
9250b57cec5SDimitry Andric case C2_xor:
9260b57cec5SDimitry Andric return rr0(eXOR(rc(1), rc(2)), Outputs);
9270b57cec5SDimitry Andric case C4_and_and:
9280b57cec5SDimitry Andric return rr0(eAND(rc(1), eAND(rc(2), rc(3))), Outputs);
9290b57cec5SDimitry Andric case C4_and_andn:
9300b57cec5SDimitry Andric return rr0(eAND(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
9310b57cec5SDimitry Andric case C4_and_or:
9320b57cec5SDimitry Andric return rr0(eAND(rc(1), eORL(rc(2), rc(3))), Outputs);
9330b57cec5SDimitry Andric case C4_and_orn:
9340b57cec5SDimitry Andric return rr0(eAND(rc(1), eORL(rc(2), eNOT(rc(3)))), Outputs);
9350b57cec5SDimitry Andric case C4_or_and:
9360b57cec5SDimitry Andric return rr0(eORL(rc(1), eAND(rc(2), rc(3))), Outputs);
9370b57cec5SDimitry Andric case C4_or_andn:
9380b57cec5SDimitry Andric return rr0(eORL(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
9390b57cec5SDimitry Andric case C4_or_or:
9400b57cec5SDimitry Andric return rr0(eORL(rc(1), eORL(rc(2), rc(3))), Outputs);
9410b57cec5SDimitry Andric case C4_or_orn:
9420b57cec5SDimitry Andric return rr0(eORL(rc(1), eORL(rc(2), eNOT(rc(3)))), Outputs);
9430b57cec5SDimitry Andric case C2_bitsclr:
9440b57cec5SDimitry Andric case C2_bitsclri:
9450b57cec5SDimitry Andric case C2_bitsset:
9460b57cec5SDimitry Andric case C4_nbitsclr:
9470b57cec5SDimitry Andric case C4_nbitsclri:
9480b57cec5SDimitry Andric case C4_nbitsset:
9490b57cec5SDimitry Andric // TODO
9500b57cec5SDimitry Andric break;
9510b57cec5SDimitry Andric case S2_tstbit_i:
9520b57cec5SDimitry Andric case S4_ntstbit_i: {
9530b57cec5SDimitry Andric BT::BitValue V = rc(1)[im(2)];
9540b57cec5SDimitry Andric if (V.is(0) || V.is(1)) {
9550b57cec5SDimitry Andric // If instruction is S2_tstbit_i, test for 1, otherwise test for 0.
9560b57cec5SDimitry Andric bool TV = (Opc == S2_tstbit_i);
9570b57cec5SDimitry Andric BT::BitValue F = V.is(TV) ? BT::BitValue::One : BT::BitValue::Zero;
9580b57cec5SDimitry Andric return rr0(RegisterCell(W0).fill(0, W0, F), Outputs);
9590b57cec5SDimitry Andric }
9600b57cec5SDimitry Andric break;
9610b57cec5SDimitry Andric }
9620b57cec5SDimitry Andric
9630b57cec5SDimitry Andric default:
9640b57cec5SDimitry Andric // For instructions that define a single predicate registers, store
9650b57cec5SDimitry Andric // the low 8 bits of the register only.
9660b57cec5SDimitry Andric if (unsigned DefR = getUniqueDefVReg(MI)) {
9670b57cec5SDimitry Andric if (MRI.getRegClass(DefR) == &Hexagon::PredRegsRegClass) {
9680b57cec5SDimitry Andric BT::RegisterRef PD(DefR, 0);
9690b57cec5SDimitry Andric uint16_t RW = getRegBitWidth(PD);
9700b57cec5SDimitry Andric uint16_t PW = 8; // XXX Pred size: getRegBitWidth(Reg[1]);
9710b57cec5SDimitry Andric RegisterCell RC = RegisterCell::self(DefR, RW);
9720b57cec5SDimitry Andric RC.fill(PW, RW, BT::BitValue::Zero);
9730b57cec5SDimitry Andric putCell(PD, RC, Outputs);
9740b57cec5SDimitry Andric return true;
9750b57cec5SDimitry Andric }
9760b57cec5SDimitry Andric }
9770b57cec5SDimitry Andric return MachineEvaluator::evaluate(MI, Inputs, Outputs);
9780b57cec5SDimitry Andric }
9790b57cec5SDimitry Andric #undef im
9800b57cec5SDimitry Andric #undef rc
9810b57cec5SDimitry Andric #undef op
9820b57cec5SDimitry Andric return false;
9830b57cec5SDimitry Andric }
9840b57cec5SDimitry Andric
evaluate(const MachineInstr & BI,const CellMapType & Inputs,BranchTargetList & Targets,bool & FallsThru) const9850b57cec5SDimitry Andric bool HexagonEvaluator::evaluate(const MachineInstr &BI,
9860b57cec5SDimitry Andric const CellMapType &Inputs,
9870b57cec5SDimitry Andric BranchTargetList &Targets,
9880b57cec5SDimitry Andric bool &FallsThru) const {
9890b57cec5SDimitry Andric // We need to evaluate one branch at a time. TII::analyzeBranch checks
9900b57cec5SDimitry Andric // all the branches in a basic block at once, so we cannot use it.
9910b57cec5SDimitry Andric unsigned Opc = BI.getOpcode();
9920b57cec5SDimitry Andric bool SimpleBranch = false;
9930b57cec5SDimitry Andric bool Negated = false;
9940b57cec5SDimitry Andric switch (Opc) {
9950b57cec5SDimitry Andric case Hexagon::J2_jumpf:
9960b57cec5SDimitry Andric case Hexagon::J2_jumpfpt:
9970b57cec5SDimitry Andric case Hexagon::J2_jumpfnew:
9980b57cec5SDimitry Andric case Hexagon::J2_jumpfnewpt:
9990b57cec5SDimitry Andric Negated = true;
1000bdd1243dSDimitry Andric [[fallthrough]];
10010b57cec5SDimitry Andric case Hexagon::J2_jumpt:
10020b57cec5SDimitry Andric case Hexagon::J2_jumptpt:
10030b57cec5SDimitry Andric case Hexagon::J2_jumptnew:
10040b57cec5SDimitry Andric case Hexagon::J2_jumptnewpt:
10050b57cec5SDimitry Andric // Simple branch: if([!]Pn) jump ...
10060b57cec5SDimitry Andric // i.e. Op0 = predicate, Op1 = branch target.
10070b57cec5SDimitry Andric SimpleBranch = true;
10080b57cec5SDimitry Andric break;
10090b57cec5SDimitry Andric case Hexagon::J2_jump:
10100b57cec5SDimitry Andric Targets.insert(BI.getOperand(0).getMBB());
10110b57cec5SDimitry Andric FallsThru = false;
10120b57cec5SDimitry Andric return true;
10130b57cec5SDimitry Andric default:
10140b57cec5SDimitry Andric // If the branch is of unknown type, assume that all successors are
10150b57cec5SDimitry Andric // executable.
10160b57cec5SDimitry Andric return false;
10170b57cec5SDimitry Andric }
10180b57cec5SDimitry Andric
10190b57cec5SDimitry Andric if (!SimpleBranch)
10200b57cec5SDimitry Andric return false;
10210b57cec5SDimitry Andric
10220b57cec5SDimitry Andric // BI is a conditional branch if we got here.
10230b57cec5SDimitry Andric RegisterRef PR = BI.getOperand(0);
10240b57cec5SDimitry Andric RegisterCell PC = getCell(PR, Inputs);
10250b57cec5SDimitry Andric const BT::BitValue &Test = PC[0];
10260b57cec5SDimitry Andric
10270b57cec5SDimitry Andric // If the condition is neither true nor false, then it's unknown.
10280b57cec5SDimitry Andric if (!Test.is(0) && !Test.is(1))
10290b57cec5SDimitry Andric return false;
10300b57cec5SDimitry Andric
10310b57cec5SDimitry Andric // "Test.is(!Negated)" means "branch condition is true".
10320b57cec5SDimitry Andric if (!Test.is(!Negated)) {
10330b57cec5SDimitry Andric // Condition known to be false.
10340b57cec5SDimitry Andric FallsThru = true;
10350b57cec5SDimitry Andric return true;
10360b57cec5SDimitry Andric }
10370b57cec5SDimitry Andric
10380b57cec5SDimitry Andric Targets.insert(BI.getOperand(1).getMBB());
10390b57cec5SDimitry Andric FallsThru = false;
10400b57cec5SDimitry Andric return true;
10410b57cec5SDimitry Andric }
10420b57cec5SDimitry Andric
getUniqueDefVReg(const MachineInstr & MI) const10430b57cec5SDimitry Andric unsigned HexagonEvaluator::getUniqueDefVReg(const MachineInstr &MI) const {
10440b57cec5SDimitry Andric unsigned DefReg = 0;
10450b57cec5SDimitry Andric for (const MachineOperand &Op : MI.operands()) {
10460b57cec5SDimitry Andric if (!Op.isReg() || !Op.isDef())
10470b57cec5SDimitry Andric continue;
10488bcb0991SDimitry Andric Register R = Op.getReg();
1049e8d8bef9SDimitry Andric if (!R.isVirtual())
10500b57cec5SDimitry Andric continue;
10510b57cec5SDimitry Andric if (DefReg != 0)
10520b57cec5SDimitry Andric return 0;
10530b57cec5SDimitry Andric DefReg = R;
10540b57cec5SDimitry Andric }
10550b57cec5SDimitry Andric return DefReg;
10560b57cec5SDimitry Andric }
10570b57cec5SDimitry Andric
evaluateLoad(const MachineInstr & MI,const CellMapType & Inputs,CellMapType & Outputs) const10580b57cec5SDimitry Andric bool HexagonEvaluator::evaluateLoad(const MachineInstr &MI,
10590b57cec5SDimitry Andric const CellMapType &Inputs,
10600b57cec5SDimitry Andric CellMapType &Outputs) const {
10610b57cec5SDimitry Andric using namespace Hexagon;
10620b57cec5SDimitry Andric
10630b57cec5SDimitry Andric if (TII.isPredicated(MI))
10640b57cec5SDimitry Andric return false;
10650b57cec5SDimitry Andric assert(MI.mayLoad() && "A load that mayn't?");
10660b57cec5SDimitry Andric unsigned Opc = MI.getOpcode();
10670b57cec5SDimitry Andric
10680b57cec5SDimitry Andric uint16_t BitNum;
10690b57cec5SDimitry Andric bool SignEx;
10700b57cec5SDimitry Andric
10710b57cec5SDimitry Andric switch (Opc) {
10720b57cec5SDimitry Andric default:
10730b57cec5SDimitry Andric return false;
10740b57cec5SDimitry Andric
10750b57cec5SDimitry Andric #if 0
10760b57cec5SDimitry Andric // memb_fifo
10770b57cec5SDimitry Andric case L2_loadalignb_pbr:
10780b57cec5SDimitry Andric case L2_loadalignb_pcr:
10790b57cec5SDimitry Andric case L2_loadalignb_pi:
10800b57cec5SDimitry Andric // memh_fifo
10810b57cec5SDimitry Andric case L2_loadalignh_pbr:
10820b57cec5SDimitry Andric case L2_loadalignh_pcr:
10830b57cec5SDimitry Andric case L2_loadalignh_pi:
10840b57cec5SDimitry Andric // membh
10850b57cec5SDimitry Andric case L2_loadbsw2_pbr:
10860b57cec5SDimitry Andric case L2_loadbsw2_pci:
10870b57cec5SDimitry Andric case L2_loadbsw2_pcr:
10880b57cec5SDimitry Andric case L2_loadbsw2_pi:
10890b57cec5SDimitry Andric case L2_loadbsw4_pbr:
10900b57cec5SDimitry Andric case L2_loadbsw4_pci:
10910b57cec5SDimitry Andric case L2_loadbsw4_pcr:
10920b57cec5SDimitry Andric case L2_loadbsw4_pi:
10930b57cec5SDimitry Andric // memubh
10940b57cec5SDimitry Andric case L2_loadbzw2_pbr:
10950b57cec5SDimitry Andric case L2_loadbzw2_pci:
10960b57cec5SDimitry Andric case L2_loadbzw2_pcr:
10970b57cec5SDimitry Andric case L2_loadbzw2_pi:
10980b57cec5SDimitry Andric case L2_loadbzw4_pbr:
10990b57cec5SDimitry Andric case L2_loadbzw4_pci:
11000b57cec5SDimitry Andric case L2_loadbzw4_pcr:
11010b57cec5SDimitry Andric case L2_loadbzw4_pi:
11020b57cec5SDimitry Andric #endif
11030b57cec5SDimitry Andric
11040b57cec5SDimitry Andric case L2_loadrbgp:
11050b57cec5SDimitry Andric case L2_loadrb_io:
11060b57cec5SDimitry Andric case L2_loadrb_pbr:
11070b57cec5SDimitry Andric case L2_loadrb_pci:
11080b57cec5SDimitry Andric case L2_loadrb_pcr:
11090b57cec5SDimitry Andric case L2_loadrb_pi:
11100b57cec5SDimitry Andric case PS_loadrbabs:
11110b57cec5SDimitry Andric case L4_loadrb_ap:
11120b57cec5SDimitry Andric case L4_loadrb_rr:
11130b57cec5SDimitry Andric case L4_loadrb_ur:
11140b57cec5SDimitry Andric BitNum = 8;
11150b57cec5SDimitry Andric SignEx = true;
11160b57cec5SDimitry Andric break;
11170b57cec5SDimitry Andric
11180b57cec5SDimitry Andric case L2_loadrubgp:
11190b57cec5SDimitry Andric case L2_loadrub_io:
11200b57cec5SDimitry Andric case L2_loadrub_pbr:
11210b57cec5SDimitry Andric case L2_loadrub_pci:
11220b57cec5SDimitry Andric case L2_loadrub_pcr:
11230b57cec5SDimitry Andric case L2_loadrub_pi:
11240b57cec5SDimitry Andric case PS_loadrubabs:
11250b57cec5SDimitry Andric case L4_loadrub_ap:
11260b57cec5SDimitry Andric case L4_loadrub_rr:
11270b57cec5SDimitry Andric case L4_loadrub_ur:
11280b57cec5SDimitry Andric BitNum = 8;
11290b57cec5SDimitry Andric SignEx = false;
11300b57cec5SDimitry Andric break;
11310b57cec5SDimitry Andric
11320b57cec5SDimitry Andric case L2_loadrhgp:
11330b57cec5SDimitry Andric case L2_loadrh_io:
11340b57cec5SDimitry Andric case L2_loadrh_pbr:
11350b57cec5SDimitry Andric case L2_loadrh_pci:
11360b57cec5SDimitry Andric case L2_loadrh_pcr:
11370b57cec5SDimitry Andric case L2_loadrh_pi:
11380b57cec5SDimitry Andric case PS_loadrhabs:
11390b57cec5SDimitry Andric case L4_loadrh_ap:
11400b57cec5SDimitry Andric case L4_loadrh_rr:
11410b57cec5SDimitry Andric case L4_loadrh_ur:
11420b57cec5SDimitry Andric BitNum = 16;
11430b57cec5SDimitry Andric SignEx = true;
11440b57cec5SDimitry Andric break;
11450b57cec5SDimitry Andric
11460b57cec5SDimitry Andric case L2_loadruhgp:
11470b57cec5SDimitry Andric case L2_loadruh_io:
11480b57cec5SDimitry Andric case L2_loadruh_pbr:
11490b57cec5SDimitry Andric case L2_loadruh_pci:
11500b57cec5SDimitry Andric case L2_loadruh_pcr:
11510b57cec5SDimitry Andric case L2_loadruh_pi:
11520b57cec5SDimitry Andric case L4_loadruh_rr:
11530b57cec5SDimitry Andric case PS_loadruhabs:
11540b57cec5SDimitry Andric case L4_loadruh_ap:
11550b57cec5SDimitry Andric case L4_loadruh_ur:
11560b57cec5SDimitry Andric BitNum = 16;
11570b57cec5SDimitry Andric SignEx = false;
11580b57cec5SDimitry Andric break;
11590b57cec5SDimitry Andric
11600b57cec5SDimitry Andric case L2_loadrigp:
11610b57cec5SDimitry Andric case L2_loadri_io:
11620b57cec5SDimitry Andric case L2_loadri_pbr:
11630b57cec5SDimitry Andric case L2_loadri_pci:
11640b57cec5SDimitry Andric case L2_loadri_pcr:
11650b57cec5SDimitry Andric case L2_loadri_pi:
11660b57cec5SDimitry Andric case L2_loadw_locked:
11670b57cec5SDimitry Andric case PS_loadriabs:
11680b57cec5SDimitry Andric case L4_loadri_ap:
11690b57cec5SDimitry Andric case L4_loadri_rr:
11700b57cec5SDimitry Andric case L4_loadri_ur:
11710b57cec5SDimitry Andric case LDriw_pred:
11720b57cec5SDimitry Andric BitNum = 32;
11730b57cec5SDimitry Andric SignEx = true;
11740b57cec5SDimitry Andric break;
11750b57cec5SDimitry Andric
11760b57cec5SDimitry Andric case L2_loadrdgp:
11770b57cec5SDimitry Andric case L2_loadrd_io:
11780b57cec5SDimitry Andric case L2_loadrd_pbr:
11790b57cec5SDimitry Andric case L2_loadrd_pci:
11800b57cec5SDimitry Andric case L2_loadrd_pcr:
11810b57cec5SDimitry Andric case L2_loadrd_pi:
11820b57cec5SDimitry Andric case L4_loadd_locked:
11830b57cec5SDimitry Andric case PS_loadrdabs:
11840b57cec5SDimitry Andric case L4_loadrd_ap:
11850b57cec5SDimitry Andric case L4_loadrd_rr:
11860b57cec5SDimitry Andric case L4_loadrd_ur:
11870b57cec5SDimitry Andric BitNum = 64;
11880b57cec5SDimitry Andric SignEx = true;
11890b57cec5SDimitry Andric break;
11900b57cec5SDimitry Andric }
11910b57cec5SDimitry Andric
11920b57cec5SDimitry Andric const MachineOperand &MD = MI.getOperand(0);
11930b57cec5SDimitry Andric assert(MD.isReg() && MD.isDef());
11940b57cec5SDimitry Andric RegisterRef RD = MD;
11950b57cec5SDimitry Andric
11960b57cec5SDimitry Andric uint16_t W = getRegBitWidth(RD);
11970b57cec5SDimitry Andric assert(W >= BitNum && BitNum > 0);
11980b57cec5SDimitry Andric RegisterCell Res(W);
11990b57cec5SDimitry Andric
12000b57cec5SDimitry Andric for (uint16_t i = 0; i < BitNum; ++i)
12010b57cec5SDimitry Andric Res[i] = BT::BitValue::self(BT::BitRef(RD.Reg, i));
12020b57cec5SDimitry Andric
12030b57cec5SDimitry Andric if (SignEx) {
12040b57cec5SDimitry Andric const BT::BitValue &Sign = Res[BitNum-1];
12050b57cec5SDimitry Andric for (uint16_t i = BitNum; i < W; ++i)
12060b57cec5SDimitry Andric Res[i] = BT::BitValue::ref(Sign);
12070b57cec5SDimitry Andric } else {
12080b57cec5SDimitry Andric for (uint16_t i = BitNum; i < W; ++i)
12090b57cec5SDimitry Andric Res[i] = BT::BitValue::Zero;
12100b57cec5SDimitry Andric }
12110b57cec5SDimitry Andric
12120b57cec5SDimitry Andric putCell(RD, Res, Outputs);
12130b57cec5SDimitry Andric return true;
12140b57cec5SDimitry Andric }
12150b57cec5SDimitry Andric
evaluateFormalCopy(const MachineInstr & MI,const CellMapType & Inputs,CellMapType & Outputs) const12160b57cec5SDimitry Andric bool HexagonEvaluator::evaluateFormalCopy(const MachineInstr &MI,
12170b57cec5SDimitry Andric const CellMapType &Inputs,
12180b57cec5SDimitry Andric CellMapType &Outputs) const {
12190b57cec5SDimitry Andric // If MI defines a formal parameter, but is not a copy (loads are handled
12200b57cec5SDimitry Andric // in evaluateLoad), then it's not clear what to do.
12210b57cec5SDimitry Andric assert(MI.isCopy());
12220b57cec5SDimitry Andric
12230b57cec5SDimitry Andric RegisterRef RD = MI.getOperand(0);
12240b57cec5SDimitry Andric RegisterRef RS = MI.getOperand(1);
12250b57cec5SDimitry Andric assert(RD.Sub == 0);
1226bdd1243dSDimitry Andric if (!RS.Reg.isPhysical())
12270b57cec5SDimitry Andric return false;
12280b57cec5SDimitry Andric RegExtMap::const_iterator F = VRX.find(RD.Reg);
12290b57cec5SDimitry Andric if (F == VRX.end())
12300b57cec5SDimitry Andric return false;
12310b57cec5SDimitry Andric
12320b57cec5SDimitry Andric uint16_t EW = F->second.Width;
12330b57cec5SDimitry Andric // Store RD's cell into the map. This will associate the cell with a virtual
12340b57cec5SDimitry Andric // register, and make zero-/sign-extends possible (otherwise we would be ex-
12350b57cec5SDimitry Andric // tending "self" bit values, which will have no effect, since "self" values
12360b57cec5SDimitry Andric // cannot be references to anything).
12370b57cec5SDimitry Andric putCell(RD, getCell(RS, Inputs), Outputs);
12380b57cec5SDimitry Andric
12390b57cec5SDimitry Andric RegisterCell Res;
12400b57cec5SDimitry Andric // Read RD's cell from the outputs instead of RS's cell from the inputs:
12410b57cec5SDimitry Andric if (F->second.Type == ExtType::SExt)
12420b57cec5SDimitry Andric Res = eSXT(getCell(RD, Outputs), EW);
12430b57cec5SDimitry Andric else if (F->second.Type == ExtType::ZExt)
12440b57cec5SDimitry Andric Res = eZXT(getCell(RD, Outputs), EW);
12450b57cec5SDimitry Andric
12460b57cec5SDimitry Andric putCell(RD, Res, Outputs);
12470b57cec5SDimitry Andric return true;
12480b57cec5SDimitry Andric }
12490b57cec5SDimitry Andric
getNextPhysReg(unsigned PReg,unsigned Width) const12500b57cec5SDimitry Andric unsigned HexagonEvaluator::getNextPhysReg(unsigned PReg, unsigned Width) const {
12510b57cec5SDimitry Andric using namespace Hexagon;
12520b57cec5SDimitry Andric
12530b57cec5SDimitry Andric bool Is64 = DoubleRegsRegClass.contains(PReg);
12540b57cec5SDimitry Andric assert(PReg == 0 || Is64 || IntRegsRegClass.contains(PReg));
12550b57cec5SDimitry Andric
12560b57cec5SDimitry Andric static const unsigned Phys32[] = { R0, R1, R2, R3, R4, R5 };
12570b57cec5SDimitry Andric static const unsigned Phys64[] = { D0, D1, D2 };
12580b57cec5SDimitry Andric const unsigned Num32 = sizeof(Phys32)/sizeof(unsigned);
12590b57cec5SDimitry Andric const unsigned Num64 = sizeof(Phys64)/sizeof(unsigned);
12600b57cec5SDimitry Andric
12610b57cec5SDimitry Andric // Return the first parameter register of the required width.
12620b57cec5SDimitry Andric if (PReg == 0)
12630b57cec5SDimitry Andric return (Width <= 32) ? Phys32[0] : Phys64[0];
12640b57cec5SDimitry Andric
12650b57cec5SDimitry Andric // Set Idx32, Idx64 in such a way that Idx+1 would give the index of the
12660b57cec5SDimitry Andric // next register.
12670b57cec5SDimitry Andric unsigned Idx32 = 0, Idx64 = 0;
12680b57cec5SDimitry Andric if (!Is64) {
12690b57cec5SDimitry Andric while (Idx32 < Num32) {
12700b57cec5SDimitry Andric if (Phys32[Idx32] == PReg)
12710b57cec5SDimitry Andric break;
12720b57cec5SDimitry Andric Idx32++;
12730b57cec5SDimitry Andric }
12740b57cec5SDimitry Andric Idx64 = Idx32/2;
12750b57cec5SDimitry Andric } else {
12760b57cec5SDimitry Andric while (Idx64 < Num64) {
12770b57cec5SDimitry Andric if (Phys64[Idx64] == PReg)
12780b57cec5SDimitry Andric break;
12790b57cec5SDimitry Andric Idx64++;
12800b57cec5SDimitry Andric }
12810b57cec5SDimitry Andric Idx32 = Idx64*2+1;
12820b57cec5SDimitry Andric }
12830b57cec5SDimitry Andric
12840b57cec5SDimitry Andric if (Width <= 32)
12850b57cec5SDimitry Andric return (Idx32+1 < Num32) ? Phys32[Idx32+1] : 0;
12860b57cec5SDimitry Andric return (Idx64+1 < Num64) ? Phys64[Idx64+1] : 0;
12870b57cec5SDimitry Andric }
12880b57cec5SDimitry Andric
getVirtRegFor(unsigned PReg) const12890b57cec5SDimitry Andric unsigned HexagonEvaluator::getVirtRegFor(unsigned PReg) const {
12900b57cec5SDimitry Andric for (std::pair<unsigned,unsigned> P : MRI.liveins())
12910b57cec5SDimitry Andric if (P.first == PReg)
12920b57cec5SDimitry Andric return P.second;
12930b57cec5SDimitry Andric return 0;
12940b57cec5SDimitry Andric }
1295