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