xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonAsmPrinter.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly ---===//
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 // This file contains a printer that converts from our internal representation
100b57cec5SDimitry Andric // of machine-dependent LLVM code to Hexagon assembly language. This printer is
110b57cec5SDimitry Andric // the output mechanism used by `llc'.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "HexagonAsmPrinter.h"
160b57cec5SDimitry Andric #include "Hexagon.h"
170b57cec5SDimitry Andric #include "HexagonInstrInfo.h"
180b57cec5SDimitry Andric #include "HexagonRegisterInfo.h"
190b57cec5SDimitry Andric #include "HexagonSubtarget.h"
20*0fca6ea1SDimitry Andric #include "HexagonTargetStreamer.h"
210b57cec5SDimitry Andric #include "MCTargetDesc/HexagonInstPrinter.h"
220b57cec5SDimitry Andric #include "MCTargetDesc/HexagonMCExpr.h"
230b57cec5SDimitry Andric #include "MCTargetDesc/HexagonMCInstrInfo.h"
240b57cec5SDimitry Andric #include "MCTargetDesc/HexagonMCTargetDesc.h"
250b57cec5SDimitry Andric #include "TargetInfo/HexagonTargetInfo.h"
260b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
270b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
280b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
290b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
370b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
380b57cec5SDimitry Andric #include "llvm/MC/MCDirectives.h"
390b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
400b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
410b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
420b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h"
430b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
440b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
45349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
460b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
470b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
480b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
490b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
50*0fca6ea1SDimitry Andric #include "llvm/Target/TargetMachine.h"
510b57cec5SDimitry Andric #include <algorithm>
520b57cec5SDimitry Andric #include <cassert>
530b57cec5SDimitry Andric #include <cstdint>
540b57cec5SDimitry Andric #include <string>
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric using namespace llvm;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric namespace llvm {
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
610b57cec5SDimitry Andric                       MCInst &MCB, HexagonAsmPrinter &AP);
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric } // end namespace llvm
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric #define DEBUG_TYPE "asm-printer"
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric // Given a scalar register return its pair.
getHexagonRegisterPair(unsigned Reg,const MCRegisterInfo * RI)680b57cec5SDimitry Andric inline static unsigned getHexagonRegisterPair(unsigned Reg,
690b57cec5SDimitry Andric       const MCRegisterInfo *RI) {
700b57cec5SDimitry Andric   assert(Hexagon::IntRegsRegClass.contains(Reg));
7106c3fb27SDimitry Andric   unsigned Pair = *RI->superregs(Reg).begin();
720b57cec5SDimitry Andric   assert(Hexagon::DoubleRegsRegClass.contains(Pair));
730b57cec5SDimitry Andric   return Pair;
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric 
printOperand(const MachineInstr * MI,unsigned OpNo,raw_ostream & O)760b57cec5SDimitry Andric void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
770b57cec5SDimitry Andric                                      raw_ostream &O) {
780b57cec5SDimitry Andric   const MachineOperand &MO = MI->getOperand(OpNo);
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   switch (MO.getType()) {
810b57cec5SDimitry Andric   default:
820b57cec5SDimitry Andric     llvm_unreachable ("<unknown operand type>");
830b57cec5SDimitry Andric   case MachineOperand::MO_Register:
840b57cec5SDimitry Andric     O << HexagonInstPrinter::getRegisterName(MO.getReg());
850b57cec5SDimitry Andric     return;
860b57cec5SDimitry Andric   case MachineOperand::MO_Immediate:
870b57cec5SDimitry Andric     O << MO.getImm();
880b57cec5SDimitry Andric     return;
890b57cec5SDimitry Andric   case MachineOperand::MO_MachineBasicBlock:
900b57cec5SDimitry Andric     MO.getMBB()->getSymbol()->print(O, MAI);
910b57cec5SDimitry Andric     return;
920b57cec5SDimitry Andric   case MachineOperand::MO_ConstantPoolIndex:
930b57cec5SDimitry Andric     GetCPISymbol(MO.getIndex())->print(O, MAI);
940b57cec5SDimitry Andric     return;
950b57cec5SDimitry Andric   case MachineOperand::MO_GlobalAddress:
960b57cec5SDimitry Andric     PrintSymbolOperand(MO, O);
970b57cec5SDimitry Andric     return;
980b57cec5SDimitry Andric   }
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric // isBlockOnlyReachableByFallthrough - We need to override this since the
1020b57cec5SDimitry Andric // default AsmPrinter does not print labels for any basic block that
1030b57cec5SDimitry Andric // is only reachable by a fall through. That works for all cases except
1040b57cec5SDimitry Andric // for the case in which the basic block is reachable by a fall through but
1050b57cec5SDimitry Andric // through an indirect from a jump table. In this case, the jump table
1060b57cec5SDimitry Andric // will contain a label not defined by AsmPrinter.
isBlockOnlyReachableByFallthrough(const MachineBasicBlock * MBB) const1070b57cec5SDimitry Andric bool HexagonAsmPrinter::isBlockOnlyReachableByFallthrough(
1080b57cec5SDimitry Andric       const MachineBasicBlock *MBB) const {
1090b57cec5SDimitry Andric   if (MBB->hasAddressTaken())
1100b57cec5SDimitry Andric     return false;
1110b57cec5SDimitry Andric   return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric /// PrintAsmOperand - Print out an operand for an inline asm expression.
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & OS)1150b57cec5SDimitry Andric bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1160b57cec5SDimitry Andric                                         const char *ExtraCode,
1170b57cec5SDimitry Andric                                         raw_ostream &OS) {
1180b57cec5SDimitry Andric   // Does this asm operand have a single letter operand modifier?
1190b57cec5SDimitry Andric   if (ExtraCode && ExtraCode[0]) {
1200b57cec5SDimitry Andric     if (ExtraCode[1] != 0)
1210b57cec5SDimitry Andric       return true; // Unknown modifier.
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric     switch (ExtraCode[0]) {
1240b57cec5SDimitry Andric     default:
1250b57cec5SDimitry Andric       // See if this is a generic print operand
1260b57cec5SDimitry Andric       return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS);
1270b57cec5SDimitry Andric     case 'L':
1280b57cec5SDimitry Andric     case 'H': { // The highest-numbered register of a pair.
1290b57cec5SDimitry Andric       const MachineOperand &MO = MI->getOperand(OpNo);
1300b57cec5SDimitry Andric       const MachineFunction &MF = *MI->getParent()->getParent();
1310b57cec5SDimitry Andric       const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1320b57cec5SDimitry Andric       if (!MO.isReg())
1330b57cec5SDimitry Andric         return true;
1348bcb0991SDimitry Andric       Register RegNumber = MO.getReg();
1350b57cec5SDimitry Andric       // This should be an assert in the frontend.
1360b57cec5SDimitry Andric       if (Hexagon::DoubleRegsRegClass.contains(RegNumber))
1370b57cec5SDimitry Andric         RegNumber = TRI->getSubReg(RegNumber, ExtraCode[0] == 'L' ?
1380b57cec5SDimitry Andric                                               Hexagon::isub_lo :
1390b57cec5SDimitry Andric                                               Hexagon::isub_hi);
1400b57cec5SDimitry Andric       OS << HexagonInstPrinter::getRegisterName(RegNumber);
1410b57cec5SDimitry Andric       return false;
1420b57cec5SDimitry Andric     }
1430b57cec5SDimitry Andric     case 'I':
1440b57cec5SDimitry Andric       // Write 'i' if an integer constant, otherwise nothing.  Used to print
1450b57cec5SDimitry Andric       // addi vs add, etc.
1460b57cec5SDimitry Andric       if (MI->getOperand(OpNo).isImm())
1470b57cec5SDimitry Andric         OS << "i";
1480b57cec5SDimitry Andric       return false;
1490b57cec5SDimitry Andric     }
1500b57cec5SDimitry Andric   }
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   printOperand(MI, OpNo, OS);
1530b57cec5SDimitry Andric   return false;
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
PrintAsmMemoryOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & O)1560b57cec5SDimitry Andric bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
1570b57cec5SDimitry Andric                                               unsigned OpNo,
1580b57cec5SDimitry Andric                                               const char *ExtraCode,
1590b57cec5SDimitry Andric                                               raw_ostream &O) {
1600b57cec5SDimitry Andric   if (ExtraCode && ExtraCode[0])
1610b57cec5SDimitry Andric     return true; // Unknown modifier.
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   const MachineOperand &Base  = MI->getOperand(OpNo);
1640b57cec5SDimitry Andric   const MachineOperand &Offset = MI->getOperand(OpNo+1);
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   if (Base.isReg())
1670b57cec5SDimitry Andric     printOperand(MI, OpNo, O);
1680b57cec5SDimitry Andric   else
1690b57cec5SDimitry Andric     llvm_unreachable("Unimplemented");
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   if (Offset.isImm()) {
1720b57cec5SDimitry Andric     if (Offset.getImm())
1730b57cec5SDimitry Andric       O << "+#" << Offset.getImm();
1740b57cec5SDimitry Andric   } else {
1750b57cec5SDimitry Andric     llvm_unreachable("Unimplemented");
1760b57cec5SDimitry Andric   }
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric   return false;
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric 
smallData(AsmPrinter & AP,const MachineInstr & MI,MCStreamer & OutStreamer,const MCOperand & Imm,int AlignSize,const MCSubtargetInfo & STI)1810b57cec5SDimitry Andric static MCSymbol *smallData(AsmPrinter &AP, const MachineInstr &MI,
1820b57cec5SDimitry Andric                            MCStreamer &OutStreamer, const MCOperand &Imm,
183349cc55cSDimitry Andric                            int AlignSize, const MCSubtargetInfo& STI) {
1840b57cec5SDimitry Andric   MCSymbol *Sym;
1850b57cec5SDimitry Andric   int64_t Value;
1860b57cec5SDimitry Andric   if (Imm.getExpr()->evaluateAsAbsolute(Value)) {
1870b57cec5SDimitry Andric     StringRef sectionPrefix;
1880b57cec5SDimitry Andric     std::string ImmString;
1890b57cec5SDimitry Andric     StringRef Name;
1900b57cec5SDimitry Andric     if (AlignSize == 8) {
1910b57cec5SDimitry Andric        Name = ".CONST_0000000000000000";
1920b57cec5SDimitry Andric        sectionPrefix = ".gnu.linkonce.l8";
1930b57cec5SDimitry Andric        ImmString = utohexstr(Value);
1940b57cec5SDimitry Andric     } else {
1950b57cec5SDimitry Andric        Name = ".CONST_00000000";
1960b57cec5SDimitry Andric        sectionPrefix = ".gnu.linkonce.l4";
1970b57cec5SDimitry Andric        ImmString = utohexstr(static_cast<uint32_t>(Value));
1980b57cec5SDimitry Andric     }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric     std::string symbolName =   // Yes, leading zeros are kept.
2010b57cec5SDimitry Andric       Name.drop_back(ImmString.size()).str() + ImmString;
2020b57cec5SDimitry Andric     std::string sectionName = sectionPrefix.str() + symbolName;
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric     MCSectionELF *Section = OutStreamer.getContext().getELFSection(
2050b57cec5SDimitry Andric         sectionName, ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
20681ad6265SDimitry Andric     OutStreamer.switchSection(Section);
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric     Sym = AP.OutContext.getOrCreateSymbol(Twine(symbolName));
2090b57cec5SDimitry Andric     if (Sym->isUndefined()) {
2105ffd83dbSDimitry Andric       OutStreamer.emitLabel(Sym);
2115ffd83dbSDimitry Andric       OutStreamer.emitSymbolAttribute(Sym, MCSA_Global);
2125ffd83dbSDimitry Andric       OutStreamer.emitIntValue(Value, AlignSize);
213bdd1243dSDimitry Andric       OutStreamer.emitCodeAlignment(Align(AlignSize), &STI);
2140b57cec5SDimitry Andric     }
2150b57cec5SDimitry Andric   } else {
2160b57cec5SDimitry Andric     assert(Imm.isExpr() && "Expected expression and found none");
2170b57cec5SDimitry Andric     const MachineOperand &MO = MI.getOperand(1);
2180b57cec5SDimitry Andric     assert(MO.isGlobal() || MO.isCPI() || MO.isJTI());
2190b57cec5SDimitry Andric     MCSymbol *MOSymbol = nullptr;
2200b57cec5SDimitry Andric     if (MO.isGlobal())
2210b57cec5SDimitry Andric       MOSymbol = AP.getSymbol(MO.getGlobal());
2220b57cec5SDimitry Andric     else if (MO.isCPI())
2230b57cec5SDimitry Andric       MOSymbol = AP.GetCPISymbol(MO.getIndex());
2240b57cec5SDimitry Andric     else if (MO.isJTI())
2250b57cec5SDimitry Andric       MOSymbol = AP.GetJTISymbol(MO.getIndex());
2260b57cec5SDimitry Andric     else
2270b57cec5SDimitry Andric       llvm_unreachable("Unknown operand type!");
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric     StringRef SymbolName = MOSymbol->getName();
2300b57cec5SDimitry Andric     std::string LitaName = ".CONST_" + SymbolName.str();
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric     MCSectionELF *Section = OutStreamer.getContext().getELFSection(
2330b57cec5SDimitry Andric         ".lita", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
2340b57cec5SDimitry Andric 
23581ad6265SDimitry Andric     OutStreamer.switchSection(Section);
2360b57cec5SDimitry Andric     Sym = AP.OutContext.getOrCreateSymbol(Twine(LitaName));
2370b57cec5SDimitry Andric     if (Sym->isUndefined()) {
2385ffd83dbSDimitry Andric       OutStreamer.emitLabel(Sym);
2395ffd83dbSDimitry Andric       OutStreamer.emitSymbolAttribute(Sym, MCSA_Local);
2405ffd83dbSDimitry Andric       OutStreamer.emitValue(Imm.getExpr(), AlignSize);
241bdd1243dSDimitry Andric       OutStreamer.emitCodeAlignment(Align(AlignSize), &STI);
2420b57cec5SDimitry Andric     }
2430b57cec5SDimitry Andric   }
2440b57cec5SDimitry Andric   return Sym;
2450b57cec5SDimitry Andric }
2460b57cec5SDimitry Andric 
ScaleVectorOffset(MCInst & Inst,unsigned OpNo,unsigned VectorSize,MCContext & Ctx)2470b57cec5SDimitry Andric static MCInst ScaleVectorOffset(MCInst &Inst, unsigned OpNo,
2480b57cec5SDimitry Andric                                 unsigned VectorSize, MCContext &Ctx) {
2490b57cec5SDimitry Andric   MCInst T;
2500b57cec5SDimitry Andric   T.setOpcode(Inst.getOpcode());
2510b57cec5SDimitry Andric   for (unsigned i = 0, n = Inst.getNumOperands(); i != n; ++i) {
2520b57cec5SDimitry Andric     if (i != OpNo) {
2530b57cec5SDimitry Andric       T.addOperand(Inst.getOperand(i));
2540b57cec5SDimitry Andric       continue;
2550b57cec5SDimitry Andric     }
2560b57cec5SDimitry Andric     MCOperand &ImmOp = Inst.getOperand(i);
2570b57cec5SDimitry Andric     const auto *HE = static_cast<const HexagonMCExpr*>(ImmOp.getExpr());
2580b57cec5SDimitry Andric     int32_t V = cast<MCConstantExpr>(HE->getExpr())->getValue();
2590b57cec5SDimitry Andric     auto *NewCE = MCConstantExpr::create(V / int32_t(VectorSize), Ctx);
2600b57cec5SDimitry Andric     auto *NewHE = HexagonMCExpr::create(NewCE, Ctx);
2610b57cec5SDimitry Andric     T.addOperand(MCOperand::createExpr(NewHE));
2620b57cec5SDimitry Andric   }
2630b57cec5SDimitry Andric   return T;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric 
HexagonProcessInstruction(MCInst & Inst,const MachineInstr & MI)2660b57cec5SDimitry Andric void HexagonAsmPrinter::HexagonProcessInstruction(MCInst &Inst,
2670b57cec5SDimitry Andric                                                   const MachineInstr &MI) {
2680b57cec5SDimitry Andric   MCInst &MappedInst = static_cast <MCInst &>(Inst);
2690b57cec5SDimitry Andric   const MCRegisterInfo *RI = OutStreamer->getContext().getRegisterInfo();
2700b57cec5SDimitry Andric   const MachineFunction &MF = *MI.getParent()->getParent();
2710b57cec5SDimitry Andric   auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
2720b57cec5SDimitry Andric   unsigned VectorSize = HRI.getRegSizeInBits(Hexagon::HvxVRRegClass) / 8;
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric   switch (Inst.getOpcode()) {
2750b57cec5SDimitry Andric   default:
2760b57cec5SDimitry Andric     return;
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric   case Hexagon::A2_iconst: {
2790b57cec5SDimitry Andric     Inst.setOpcode(Hexagon::A2_addi);
2800b57cec5SDimitry Andric     MCOperand Reg = Inst.getOperand(0);
2810b57cec5SDimitry Andric     MCOperand S16 = Inst.getOperand(1);
2820b57cec5SDimitry Andric     HexagonMCInstrInfo::setMustNotExtend(*S16.getExpr());
2830b57cec5SDimitry Andric     HexagonMCInstrInfo::setS27_2_reloc(*S16.getExpr());
2840b57cec5SDimitry Andric     Inst.clear();
2850b57cec5SDimitry Andric     Inst.addOperand(Reg);
2860b57cec5SDimitry Andric     Inst.addOperand(MCOperand::createReg(Hexagon::R0));
2870b57cec5SDimitry Andric     Inst.addOperand(S16);
2880b57cec5SDimitry Andric     break;
2890b57cec5SDimitry Andric   }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric   case Hexagon::A2_tfrf: {
2920b57cec5SDimitry Andric     const MCConstantExpr *Zero = MCConstantExpr::create(0, OutContext);
2930b57cec5SDimitry Andric     Inst.setOpcode(Hexagon::A2_paddif);
2940b57cec5SDimitry Andric     Inst.addOperand(MCOperand::createExpr(Zero));
2950b57cec5SDimitry Andric     break;
2960b57cec5SDimitry Andric   }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   case Hexagon::A2_tfrt: {
2990b57cec5SDimitry Andric     const MCConstantExpr *Zero = MCConstantExpr::create(0, OutContext);
3000b57cec5SDimitry Andric     Inst.setOpcode(Hexagon::A2_paddit);
3010b57cec5SDimitry Andric     Inst.addOperand(MCOperand::createExpr(Zero));
3020b57cec5SDimitry Andric     break;
3030b57cec5SDimitry Andric   }
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric   case Hexagon::A2_tfrfnew: {
3060b57cec5SDimitry Andric     const MCConstantExpr *Zero = MCConstantExpr::create(0, OutContext);
3070b57cec5SDimitry Andric     Inst.setOpcode(Hexagon::A2_paddifnew);
3080b57cec5SDimitry Andric     Inst.addOperand(MCOperand::createExpr(Zero));
3090b57cec5SDimitry Andric     break;
3100b57cec5SDimitry Andric   }
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric   case Hexagon::A2_tfrtnew: {
3130b57cec5SDimitry Andric     const MCConstantExpr *Zero = MCConstantExpr::create(0, OutContext);
3140b57cec5SDimitry Andric     Inst.setOpcode(Hexagon::A2_padditnew);
3150b57cec5SDimitry Andric     Inst.addOperand(MCOperand::createExpr(Zero));
3160b57cec5SDimitry Andric     break;
3170b57cec5SDimitry Andric   }
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric   case Hexagon::A2_zxtb: {
3200b57cec5SDimitry Andric     const MCConstantExpr *C255 = MCConstantExpr::create(255, OutContext);
3210b57cec5SDimitry Andric     Inst.setOpcode(Hexagon::A2_andir);
3220b57cec5SDimitry Andric     Inst.addOperand(MCOperand::createExpr(C255));
3230b57cec5SDimitry Andric     break;
3240b57cec5SDimitry Andric   }
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   // "$dst = CONST64(#$src1)",
3270b57cec5SDimitry Andric   case Hexagon::CONST64:
3280b57cec5SDimitry Andric     if (!OutStreamer->hasRawTextSupport()) {
3290b57cec5SDimitry Andric       const MCOperand &Imm = MappedInst.getOperand(1);
3300b57cec5SDimitry Andric       MCSectionSubPair Current = OutStreamer->getCurrentSection();
3310b57cec5SDimitry Andric 
332349cc55cSDimitry Andric       MCSymbol *Sym =
333349cc55cSDimitry Andric           smallData(*this, MI, *OutStreamer, Imm, 8, getSubtargetInfo());
3340b57cec5SDimitry Andric 
33581ad6265SDimitry Andric       OutStreamer->switchSection(Current.first, Current.second);
3360b57cec5SDimitry Andric       MCInst TmpInst;
3370b57cec5SDimitry Andric       MCOperand &Reg = MappedInst.getOperand(0);
3380b57cec5SDimitry Andric       TmpInst.setOpcode(Hexagon::L2_loadrdgp);
3390b57cec5SDimitry Andric       TmpInst.addOperand(Reg);
3400b57cec5SDimitry Andric       TmpInst.addOperand(MCOperand::createExpr(
3410b57cec5SDimitry Andric                          MCSymbolRefExpr::create(Sym, OutContext)));
3420b57cec5SDimitry Andric       MappedInst = TmpInst;
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric     }
3450b57cec5SDimitry Andric     break;
3460b57cec5SDimitry Andric   case Hexagon::CONST32:
3470b57cec5SDimitry Andric     if (!OutStreamer->hasRawTextSupport()) {
3480b57cec5SDimitry Andric       MCOperand &Imm = MappedInst.getOperand(1);
3490b57cec5SDimitry Andric       MCSectionSubPair Current = OutStreamer->getCurrentSection();
350349cc55cSDimitry Andric       MCSymbol *Sym =
351349cc55cSDimitry Andric           smallData(*this, MI, *OutStreamer, Imm, 4, getSubtargetInfo());
35281ad6265SDimitry Andric       OutStreamer->switchSection(Current.first, Current.second);
3530b57cec5SDimitry Andric       MCInst TmpInst;
3540b57cec5SDimitry Andric       MCOperand &Reg = MappedInst.getOperand(0);
3550b57cec5SDimitry Andric       TmpInst.setOpcode(Hexagon::L2_loadrigp);
3560b57cec5SDimitry Andric       TmpInst.addOperand(Reg);
3570b57cec5SDimitry Andric       TmpInst.addOperand(MCOperand::createExpr(HexagonMCExpr::create(
3580b57cec5SDimitry Andric           MCSymbolRefExpr::create(Sym, OutContext), OutContext)));
3590b57cec5SDimitry Andric       MappedInst = TmpInst;
3600b57cec5SDimitry Andric     }
3610b57cec5SDimitry Andric     break;
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   // C2_pxfer_map maps to C2_or instruction. Though, it's possible to use
3640b57cec5SDimitry Andric   // C2_or during instruction selection itself but it results
3650b57cec5SDimitry Andric   // into suboptimal code.
3660b57cec5SDimitry Andric   case Hexagon::C2_pxfer_map: {
3670b57cec5SDimitry Andric     MCOperand &Ps = Inst.getOperand(1);
3680b57cec5SDimitry Andric     MappedInst.setOpcode(Hexagon::C2_or);
3690b57cec5SDimitry Andric     MappedInst.addOperand(Ps);
3700b57cec5SDimitry Andric     return;
3710b57cec5SDimitry Andric   }
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric   // Vector reduce complex multiply by scalar, Rt & 1 map to :hi else :lo
3740b57cec5SDimitry Andric   // The insn is mapped from the 4 operand to the 3 operand raw form taking
3750b57cec5SDimitry Andric   // 3 register pairs.
3760b57cec5SDimitry Andric   case Hexagon::M2_vrcmpys_acc_s1: {
3770b57cec5SDimitry Andric     MCOperand &Rt = Inst.getOperand(3);
3780b57cec5SDimitry Andric     assert(Rt.isReg() && "Expected register and none was found");
3790b57cec5SDimitry Andric     unsigned Reg = RI->getEncodingValue(Rt.getReg());
3800b57cec5SDimitry Andric     if (Reg & 1)
3810b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h);
3820b57cec5SDimitry Andric     else
3830b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l);
3840b57cec5SDimitry Andric     Rt.setReg(getHexagonRegisterPair(Rt.getReg(), RI));
3850b57cec5SDimitry Andric     return;
3860b57cec5SDimitry Andric   }
3870b57cec5SDimitry Andric   case Hexagon::M2_vrcmpys_s1: {
3880b57cec5SDimitry Andric     MCOperand &Rt = Inst.getOperand(2);
3890b57cec5SDimitry Andric     assert(Rt.isReg() && "Expected register and none was found");
3900b57cec5SDimitry Andric     unsigned Reg = RI->getEncodingValue(Rt.getReg());
3910b57cec5SDimitry Andric     if (Reg & 1)
3920b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::M2_vrcmpys_s1_h);
3930b57cec5SDimitry Andric     else
3940b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::M2_vrcmpys_s1_l);
3950b57cec5SDimitry Andric     Rt.setReg(getHexagonRegisterPair(Rt.getReg(), RI));
3960b57cec5SDimitry Andric     return;
3970b57cec5SDimitry Andric   }
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric   case Hexagon::M2_vrcmpys_s1rp: {
4000b57cec5SDimitry Andric     MCOperand &Rt = Inst.getOperand(2);
4010b57cec5SDimitry Andric     assert(Rt.isReg() && "Expected register and none was found");
4020b57cec5SDimitry Andric     unsigned Reg = RI->getEncodingValue(Rt.getReg());
4030b57cec5SDimitry Andric     if (Reg & 1)
4040b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h);
4050b57cec5SDimitry Andric     else
4060b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l);
4070b57cec5SDimitry Andric     Rt.setReg(getHexagonRegisterPair(Rt.getReg(), RI));
4080b57cec5SDimitry Andric     return;
4090b57cec5SDimitry Andric   }
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric   case Hexagon::A4_boundscheck: {
4120b57cec5SDimitry Andric     MCOperand &Rs = Inst.getOperand(1);
4130b57cec5SDimitry Andric     assert(Rs.isReg() && "Expected register and none was found");
4140b57cec5SDimitry Andric     unsigned Reg = RI->getEncodingValue(Rs.getReg());
4150b57cec5SDimitry Andric     if (Reg & 1) // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2
4160b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::A4_boundscheck_hi);
4170b57cec5SDimitry Andric     else         // raw:lo
4180b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::A4_boundscheck_lo);
4190b57cec5SDimitry Andric     Rs.setReg(getHexagonRegisterPair(Rs.getReg(), RI));
4200b57cec5SDimitry Andric     return;
4210b57cec5SDimitry Andric   }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric   case Hexagon::PS_call_nr:
4240b57cec5SDimitry Andric     Inst.setOpcode(Hexagon::J2_call);
4250b57cec5SDimitry Andric     break;
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric   case Hexagon::S5_asrhub_rnd_sat_goodsyntax: {
4280b57cec5SDimitry Andric     MCOperand &MO = MappedInst.getOperand(2);
4290b57cec5SDimitry Andric     int64_t Imm;
4300b57cec5SDimitry Andric     MCExpr const *Expr = MO.getExpr();
4310b57cec5SDimitry Andric     bool Success = Expr->evaluateAsAbsolute(Imm);
4320b57cec5SDimitry Andric     assert(Success && "Expected immediate and none was found");
4330b57cec5SDimitry Andric     (void)Success;
4340b57cec5SDimitry Andric     MCInst TmpInst;
4350b57cec5SDimitry Andric     if (Imm == 0) {
4360b57cec5SDimitry Andric       TmpInst.setOpcode(Hexagon::S2_vsathub);
4370b57cec5SDimitry Andric       TmpInst.addOperand(MappedInst.getOperand(0));
4380b57cec5SDimitry Andric       TmpInst.addOperand(MappedInst.getOperand(1));
4390b57cec5SDimitry Andric       MappedInst = TmpInst;
4400b57cec5SDimitry Andric       return;
4410b57cec5SDimitry Andric     }
4420b57cec5SDimitry Andric     TmpInst.setOpcode(Hexagon::S5_asrhub_rnd_sat);
4430b57cec5SDimitry Andric     TmpInst.addOperand(MappedInst.getOperand(0));
4440b57cec5SDimitry Andric     TmpInst.addOperand(MappedInst.getOperand(1));
4450b57cec5SDimitry Andric     const MCExpr *One = MCConstantExpr::create(1, OutContext);
4460b57cec5SDimitry Andric     const MCExpr *Sub = MCBinaryExpr::createSub(Expr, One, OutContext);
4470b57cec5SDimitry Andric     TmpInst.addOperand(
4480b57cec5SDimitry Andric         MCOperand::createExpr(HexagonMCExpr::create(Sub, OutContext)));
4490b57cec5SDimitry Andric     MappedInst = TmpInst;
4500b57cec5SDimitry Andric     return;
4510b57cec5SDimitry Andric   }
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric   case Hexagon::S5_vasrhrnd_goodsyntax:
4540b57cec5SDimitry Andric   case Hexagon::S2_asr_i_p_rnd_goodsyntax: {
4550b57cec5SDimitry Andric     MCOperand &MO2 = MappedInst.getOperand(2);
4560b57cec5SDimitry Andric     MCExpr const *Expr = MO2.getExpr();
4570b57cec5SDimitry Andric     int64_t Imm;
4580b57cec5SDimitry Andric     bool Success = Expr->evaluateAsAbsolute(Imm);
4590b57cec5SDimitry Andric     assert(Success && "Expected immediate and none was found");
4600b57cec5SDimitry Andric     (void)Success;
4610b57cec5SDimitry Andric     MCInst TmpInst;
4620b57cec5SDimitry Andric     if (Imm == 0) {
4630b57cec5SDimitry Andric       TmpInst.setOpcode(Hexagon::A2_combinew);
4640b57cec5SDimitry Andric       TmpInst.addOperand(MappedInst.getOperand(0));
4650b57cec5SDimitry Andric       MCOperand &MO1 = MappedInst.getOperand(1);
4660b57cec5SDimitry Andric       unsigned High = RI->getSubReg(MO1.getReg(), Hexagon::isub_hi);
4670b57cec5SDimitry Andric       unsigned Low = RI->getSubReg(MO1.getReg(), Hexagon::isub_lo);
4680b57cec5SDimitry Andric       // Add a new operand for the second register in the pair.
4690b57cec5SDimitry Andric       TmpInst.addOperand(MCOperand::createReg(High));
4700b57cec5SDimitry Andric       TmpInst.addOperand(MCOperand::createReg(Low));
4710b57cec5SDimitry Andric       MappedInst = TmpInst;
4720b57cec5SDimitry Andric       return;
4730b57cec5SDimitry Andric     }
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric     if (Inst.getOpcode() == Hexagon::S2_asr_i_p_rnd_goodsyntax)
4760b57cec5SDimitry Andric       TmpInst.setOpcode(Hexagon::S2_asr_i_p_rnd);
4770b57cec5SDimitry Andric     else
4780b57cec5SDimitry Andric       TmpInst.setOpcode(Hexagon::S5_vasrhrnd);
4790b57cec5SDimitry Andric     TmpInst.addOperand(MappedInst.getOperand(0));
4800b57cec5SDimitry Andric     TmpInst.addOperand(MappedInst.getOperand(1));
4810b57cec5SDimitry Andric     const MCExpr *One = MCConstantExpr::create(1, OutContext);
4820b57cec5SDimitry Andric     const MCExpr *Sub = MCBinaryExpr::createSub(Expr, One, OutContext);
4830b57cec5SDimitry Andric     TmpInst.addOperand(
4840b57cec5SDimitry Andric         MCOperand::createExpr(HexagonMCExpr::create(Sub, OutContext)));
4850b57cec5SDimitry Andric     MappedInst = TmpInst;
4860b57cec5SDimitry Andric     return;
4870b57cec5SDimitry Andric   }
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric   // if ("#u5==0") Assembler mapped to: "Rd=Rs"; else Rd=asr(Rs,#u5-1):rnd
4900b57cec5SDimitry Andric   case Hexagon::S2_asr_i_r_rnd_goodsyntax: {
4910b57cec5SDimitry Andric     MCOperand &MO = Inst.getOperand(2);
4920b57cec5SDimitry Andric     MCExpr const *Expr = MO.getExpr();
4930b57cec5SDimitry Andric     int64_t Imm;
4940b57cec5SDimitry Andric     bool Success = Expr->evaluateAsAbsolute(Imm);
4950b57cec5SDimitry Andric     assert(Success && "Expected immediate and none was found");
4960b57cec5SDimitry Andric     (void)Success;
4970b57cec5SDimitry Andric     MCInst TmpInst;
4980b57cec5SDimitry Andric     if (Imm == 0) {
4990b57cec5SDimitry Andric       TmpInst.setOpcode(Hexagon::A2_tfr);
5000b57cec5SDimitry Andric       TmpInst.addOperand(MappedInst.getOperand(0));
5010b57cec5SDimitry Andric       TmpInst.addOperand(MappedInst.getOperand(1));
5020b57cec5SDimitry Andric       MappedInst = TmpInst;
5030b57cec5SDimitry Andric       return;
5040b57cec5SDimitry Andric     }
5050b57cec5SDimitry Andric     TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd);
5060b57cec5SDimitry Andric     TmpInst.addOperand(MappedInst.getOperand(0));
5070b57cec5SDimitry Andric     TmpInst.addOperand(MappedInst.getOperand(1));
5080b57cec5SDimitry Andric     const MCExpr *One = MCConstantExpr::create(1, OutContext);
5090b57cec5SDimitry Andric     const MCExpr *Sub = MCBinaryExpr::createSub(Expr, One, OutContext);
5100b57cec5SDimitry Andric     TmpInst.addOperand(
5110b57cec5SDimitry Andric         MCOperand::createExpr(HexagonMCExpr::create(Sub, OutContext)));
5120b57cec5SDimitry Andric     MappedInst = TmpInst;
5130b57cec5SDimitry Andric     return;
5140b57cec5SDimitry Andric   }
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric   // Translate a "$Rdd = #imm" to "$Rdd = combine(#[-1,0], #imm)"
5170b57cec5SDimitry Andric   case Hexagon::A2_tfrpi: {
5180b57cec5SDimitry Andric     MCInst TmpInst;
5190b57cec5SDimitry Andric     MCOperand &Rdd = MappedInst.getOperand(0);
5200b57cec5SDimitry Andric     MCOperand &MO = MappedInst.getOperand(1);
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric     TmpInst.setOpcode(Hexagon::A2_combineii);
5230b57cec5SDimitry Andric     TmpInst.addOperand(Rdd);
5240b57cec5SDimitry Andric     int64_t Imm;
5250b57cec5SDimitry Andric     bool Success = MO.getExpr()->evaluateAsAbsolute(Imm);
5260b57cec5SDimitry Andric     if (Success && Imm < 0) {
5270b57cec5SDimitry Andric       const MCExpr *MOne = MCConstantExpr::create(-1, OutContext);
5280b57cec5SDimitry Andric       const HexagonMCExpr *E = HexagonMCExpr::create(MOne, OutContext);
5290b57cec5SDimitry Andric       TmpInst.addOperand(MCOperand::createExpr(E));
5300b57cec5SDimitry Andric     } else {
5310b57cec5SDimitry Andric       const MCExpr *Zero = MCConstantExpr::create(0, OutContext);
5320b57cec5SDimitry Andric       const HexagonMCExpr *E = HexagonMCExpr::create(Zero, OutContext);
5330b57cec5SDimitry Andric       TmpInst.addOperand(MCOperand::createExpr(E));
5340b57cec5SDimitry Andric     }
5350b57cec5SDimitry Andric     TmpInst.addOperand(MO);
5360b57cec5SDimitry Andric     MappedInst = TmpInst;
5370b57cec5SDimitry Andric     return;
5380b57cec5SDimitry Andric   }
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric   // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)"
5410b57cec5SDimitry Andric   case Hexagon::A2_tfrp: {
5420b57cec5SDimitry Andric     MCOperand &MO = MappedInst.getOperand(1);
5430b57cec5SDimitry Andric     unsigned High = RI->getSubReg(MO.getReg(), Hexagon::isub_hi);
5440b57cec5SDimitry Andric     unsigned Low = RI->getSubReg(MO.getReg(), Hexagon::isub_lo);
5450b57cec5SDimitry Andric     MO.setReg(High);
5460b57cec5SDimitry Andric     // Add a new operand for the second register in the pair.
5470b57cec5SDimitry Andric     MappedInst.addOperand(MCOperand::createReg(Low));
5480b57cec5SDimitry Andric     MappedInst.setOpcode(Hexagon::A2_combinew);
5490b57cec5SDimitry Andric     return;
5500b57cec5SDimitry Andric   }
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric   case Hexagon::A2_tfrpt:
5530b57cec5SDimitry Andric   case Hexagon::A2_tfrpf: {
5540b57cec5SDimitry Andric     MCOperand &MO = MappedInst.getOperand(2);
5550b57cec5SDimitry Andric     unsigned High = RI->getSubReg(MO.getReg(), Hexagon::isub_hi);
5560b57cec5SDimitry Andric     unsigned Low = RI->getSubReg(MO.getReg(), Hexagon::isub_lo);
5570b57cec5SDimitry Andric     MO.setReg(High);
5580b57cec5SDimitry Andric     // Add a new operand for the second register in the pair.
5590b57cec5SDimitry Andric     MappedInst.addOperand(MCOperand::createReg(Low));
5600b57cec5SDimitry Andric     MappedInst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt)
5610b57cec5SDimitry Andric                           ? Hexagon::C2_ccombinewt
5620b57cec5SDimitry Andric                           : Hexagon::C2_ccombinewf);
5630b57cec5SDimitry Andric     return;
5640b57cec5SDimitry Andric   }
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric   case Hexagon::A2_tfrptnew:
5670b57cec5SDimitry Andric   case Hexagon::A2_tfrpfnew: {
5680b57cec5SDimitry Andric     MCOperand &MO = MappedInst.getOperand(2);
5690b57cec5SDimitry Andric     unsigned High = RI->getSubReg(MO.getReg(), Hexagon::isub_hi);
5700b57cec5SDimitry Andric     unsigned Low = RI->getSubReg(MO.getReg(), Hexagon::isub_lo);
5710b57cec5SDimitry Andric     MO.setReg(High);
5720b57cec5SDimitry Andric     // Add a new operand for the second register in the pair.
5730b57cec5SDimitry Andric     MappedInst.addOperand(MCOperand::createReg(Low));
5740b57cec5SDimitry Andric     MappedInst.setOpcode(Inst.getOpcode() == Hexagon::A2_tfrptnew
5750b57cec5SDimitry Andric                             ? Hexagon::C2_ccombinewnewt
5760b57cec5SDimitry Andric                             : Hexagon::C2_ccombinewnewf);
5770b57cec5SDimitry Andric     return;
5780b57cec5SDimitry Andric   }
5790b57cec5SDimitry Andric 
5800b57cec5SDimitry Andric   case Hexagon::M2_mpysmi: {
5810b57cec5SDimitry Andric     MCOperand &Imm = MappedInst.getOperand(2);
5820b57cec5SDimitry Andric     MCExpr const *Expr = Imm.getExpr();
5830b57cec5SDimitry Andric     int64_t Value;
5840b57cec5SDimitry Andric     bool Success = Expr->evaluateAsAbsolute(Value);
5850b57cec5SDimitry Andric     assert(Success);
5860b57cec5SDimitry Andric     (void)Success;
5870b57cec5SDimitry Andric     if (Value < 0 && Value > -256) {
5880b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::M2_mpysin);
5890b57cec5SDimitry Andric       Imm.setExpr(HexagonMCExpr::create(
5900b57cec5SDimitry Andric           MCUnaryExpr::createMinus(Expr, OutContext), OutContext));
5910b57cec5SDimitry Andric     } else
5920b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::M2_mpysip);
5930b57cec5SDimitry Andric     return;
5940b57cec5SDimitry Andric   }
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric   case Hexagon::A2_addsp: {
5970b57cec5SDimitry Andric     MCOperand &Rt = Inst.getOperand(1);
5980b57cec5SDimitry Andric     assert(Rt.isReg() && "Expected register and none was found");
5990b57cec5SDimitry Andric     unsigned Reg = RI->getEncodingValue(Rt.getReg());
6000b57cec5SDimitry Andric     if (Reg & 1)
6010b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::A2_addsph);
6020b57cec5SDimitry Andric     else
6030b57cec5SDimitry Andric       MappedInst.setOpcode(Hexagon::A2_addspl);
6040b57cec5SDimitry Andric     Rt.setReg(getHexagonRegisterPair(Rt.getReg(), RI));
6050b57cec5SDimitry Andric     return;
6060b57cec5SDimitry Andric   }
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric   case Hexagon::V6_vd0: {
6090b57cec5SDimitry Andric     MCInst TmpInst;
6100b57cec5SDimitry Andric     assert(Inst.getOperand(0).isReg() &&
6110b57cec5SDimitry Andric            "Expected register and none was found");
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric     TmpInst.setOpcode(Hexagon::V6_vxor);
6140b57cec5SDimitry Andric     TmpInst.addOperand(Inst.getOperand(0));
6150b57cec5SDimitry Andric     TmpInst.addOperand(Inst.getOperand(0));
6160b57cec5SDimitry Andric     TmpInst.addOperand(Inst.getOperand(0));
6170b57cec5SDimitry Andric     MappedInst = TmpInst;
6180b57cec5SDimitry Andric     return;
6190b57cec5SDimitry Andric   }
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric   case Hexagon::V6_vdd0: {
6220b57cec5SDimitry Andric     MCInst TmpInst;
6230b57cec5SDimitry Andric     assert (Inst.getOperand(0).isReg() &&
6240b57cec5SDimitry Andric             "Expected register and none was found");
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric     TmpInst.setOpcode(Hexagon::V6_vsubw_dv);
6270b57cec5SDimitry Andric     TmpInst.addOperand(Inst.getOperand(0));
6280b57cec5SDimitry Andric     TmpInst.addOperand(Inst.getOperand(0));
6290b57cec5SDimitry Andric     TmpInst.addOperand(Inst.getOperand(0));
6300b57cec5SDimitry Andric     MappedInst = TmpInst;
6310b57cec5SDimitry Andric     return;
6320b57cec5SDimitry Andric   }
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric   case Hexagon::V6_vL32Ub_pi:
6350b57cec5SDimitry Andric   case Hexagon::V6_vL32b_cur_pi:
6360b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_cur_pi:
6370b57cec5SDimitry Andric   case Hexagon::V6_vL32b_pi:
6380b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_pi:
6390b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_tmp_pi:
6400b57cec5SDimitry Andric   case Hexagon::V6_vL32b_tmp_pi:
6410b57cec5SDimitry Andric     MappedInst = ScaleVectorOffset(Inst, 3, VectorSize, OutContext);
6420b57cec5SDimitry Andric     return;
6430b57cec5SDimitry Andric 
6440b57cec5SDimitry Andric   case Hexagon::V6_vL32Ub_ai:
6450b57cec5SDimitry Andric   case Hexagon::V6_vL32b_ai:
6460b57cec5SDimitry Andric   case Hexagon::V6_vL32b_cur_ai:
6470b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_ai:
6480b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_cur_ai:
6490b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_tmp_ai:
6500b57cec5SDimitry Andric   case Hexagon::V6_vL32b_tmp_ai:
6510b57cec5SDimitry Andric     MappedInst = ScaleVectorOffset(Inst, 2, VectorSize, OutContext);
6520b57cec5SDimitry Andric     return;
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric   case Hexagon::V6_vS32Ub_pi:
6550b57cec5SDimitry Andric   case Hexagon::V6_vS32b_new_pi:
6560b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_new_pi:
6570b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_pi:
6580b57cec5SDimitry Andric   case Hexagon::V6_vS32b_pi:
6590b57cec5SDimitry Andric     MappedInst = ScaleVectorOffset(Inst, 2, VectorSize, OutContext);
6600b57cec5SDimitry Andric     return;
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric   case Hexagon::V6_vS32Ub_ai:
6630b57cec5SDimitry Andric   case Hexagon::V6_vS32b_ai:
6640b57cec5SDimitry Andric   case Hexagon::V6_vS32b_new_ai:
6650b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_ai:
6660b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_new_ai:
6670b57cec5SDimitry Andric     MappedInst = ScaleVectorOffset(Inst, 1, VectorSize, OutContext);
6680b57cec5SDimitry Andric     return;
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric   case Hexagon::V6_vL32b_cur_npred_pi:
6710b57cec5SDimitry Andric   case Hexagon::V6_vL32b_cur_pred_pi:
6720b57cec5SDimitry Andric   case Hexagon::V6_vL32b_npred_pi:
6730b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_cur_npred_pi:
6740b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_cur_pred_pi:
6750b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_npred_pi:
6760b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_pred_pi:
6770b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_tmp_npred_pi:
6780b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_tmp_pred_pi:
6790b57cec5SDimitry Andric   case Hexagon::V6_vL32b_pred_pi:
6800b57cec5SDimitry Andric   case Hexagon::V6_vL32b_tmp_npred_pi:
6810b57cec5SDimitry Andric   case Hexagon::V6_vL32b_tmp_pred_pi:
6820b57cec5SDimitry Andric     MappedInst = ScaleVectorOffset(Inst, 4, VectorSize, OutContext);
6830b57cec5SDimitry Andric     return;
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric   case Hexagon::V6_vL32b_cur_npred_ai:
6860b57cec5SDimitry Andric   case Hexagon::V6_vL32b_cur_pred_ai:
6870b57cec5SDimitry Andric   case Hexagon::V6_vL32b_npred_ai:
6880b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_cur_npred_ai:
6890b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_cur_pred_ai:
6900b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_npred_ai:
6910b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_pred_ai:
6920b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_tmp_npred_ai:
6930b57cec5SDimitry Andric   case Hexagon::V6_vL32b_nt_tmp_pred_ai:
6940b57cec5SDimitry Andric   case Hexagon::V6_vL32b_pred_ai:
6950b57cec5SDimitry Andric   case Hexagon::V6_vL32b_tmp_npred_ai:
6960b57cec5SDimitry Andric   case Hexagon::V6_vL32b_tmp_pred_ai:
6970b57cec5SDimitry Andric     MappedInst = ScaleVectorOffset(Inst, 3, VectorSize, OutContext);
6980b57cec5SDimitry Andric     return;
6990b57cec5SDimitry Andric 
7000b57cec5SDimitry Andric   case Hexagon::V6_vS32Ub_npred_pi:
7010b57cec5SDimitry Andric   case Hexagon::V6_vS32Ub_pred_pi:
7020b57cec5SDimitry Andric   case Hexagon::V6_vS32b_new_npred_pi:
7030b57cec5SDimitry Andric   case Hexagon::V6_vS32b_new_pred_pi:
7040b57cec5SDimitry Andric   case Hexagon::V6_vS32b_npred_pi:
7050b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nqpred_pi:
7060b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_new_npred_pi:
7070b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_new_pred_pi:
7080b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_npred_pi:
7090b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_nqpred_pi:
7100b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_pred_pi:
7110b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_qpred_pi:
7120b57cec5SDimitry Andric   case Hexagon::V6_vS32b_pred_pi:
7130b57cec5SDimitry Andric   case Hexagon::V6_vS32b_qpred_pi:
7140b57cec5SDimitry Andric     MappedInst = ScaleVectorOffset(Inst, 3, VectorSize, OutContext);
7150b57cec5SDimitry Andric     return;
7160b57cec5SDimitry Andric 
7170b57cec5SDimitry Andric   case Hexagon::V6_vS32Ub_npred_ai:
7180b57cec5SDimitry Andric   case Hexagon::V6_vS32Ub_pred_ai:
7190b57cec5SDimitry Andric   case Hexagon::V6_vS32b_new_npred_ai:
7200b57cec5SDimitry Andric   case Hexagon::V6_vS32b_new_pred_ai:
7210b57cec5SDimitry Andric   case Hexagon::V6_vS32b_npred_ai:
7220b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nqpred_ai:
7230b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_new_npred_ai:
7240b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_new_pred_ai:
7250b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_npred_ai:
7260b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_nqpred_ai:
7270b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_pred_ai:
7280b57cec5SDimitry Andric   case Hexagon::V6_vS32b_nt_qpred_ai:
7290b57cec5SDimitry Andric   case Hexagon::V6_vS32b_pred_ai:
7300b57cec5SDimitry Andric   case Hexagon::V6_vS32b_qpred_ai:
7310b57cec5SDimitry Andric     MappedInst = ScaleVectorOffset(Inst, 2, VectorSize, OutContext);
7320b57cec5SDimitry Andric     return;
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric   // V65+
7350b57cec5SDimitry Andric   case Hexagon::V6_vS32b_srls_ai:
7360b57cec5SDimitry Andric     MappedInst = ScaleVectorOffset(Inst, 1, VectorSize, OutContext);
7370b57cec5SDimitry Andric     return;
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric   case Hexagon::V6_vS32b_srls_pi:
7400b57cec5SDimitry Andric     MappedInst = ScaleVectorOffset(Inst, 2, VectorSize, OutContext);
7410b57cec5SDimitry Andric     return;
7420b57cec5SDimitry Andric   }
7430b57cec5SDimitry Andric }
7440b57cec5SDimitry Andric 
7450b57cec5SDimitry Andric /// Print out a single Hexagon MI to the current output stream.
emitInstruction(const MachineInstr * MI)7465ffd83dbSDimitry Andric void HexagonAsmPrinter::emitInstruction(const MachineInstr *MI) {
747753f127fSDimitry Andric   Hexagon_MC::verifyInstructionPredicates(MI->getOpcode(),
748753f127fSDimitry Andric                                           getSubtargetInfo().getFeatureBits());
749753f127fSDimitry Andric 
7500b57cec5SDimitry Andric   MCInst MCB;
7510b57cec5SDimitry Andric   MCB.setOpcode(Hexagon::BUNDLE);
7520b57cec5SDimitry Andric   MCB.addOperand(MCOperand::createImm(0));
7530b57cec5SDimitry Andric   const MCInstrInfo &MCII = *Subtarget->getInstrInfo();
7540b57cec5SDimitry Andric 
7550b57cec5SDimitry Andric   if (MI->isBundle()) {
7560b57cec5SDimitry Andric     const MachineBasicBlock* MBB = MI->getParent();
7570b57cec5SDimitry Andric     MachineBasicBlock::const_instr_iterator MII = MI->getIterator();
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric     for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
7600b57cec5SDimitry Andric       if (!MII->isDebugInstr() && !MII->isImplicitDef())
7610b57cec5SDimitry Andric         HexagonLowerToMC(MCII, &*MII, MCB, *this);
7620b57cec5SDimitry Andric   } else {
7630b57cec5SDimitry Andric     HexagonLowerToMC(MCII, MI, MCB, *this);
7640b57cec5SDimitry Andric   }
7650b57cec5SDimitry Andric 
7660b57cec5SDimitry Andric   const MachineFunction &MF = *MI->getParent()->getParent();
7670b57cec5SDimitry Andric   const auto &HII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
7680b57cec5SDimitry Andric   if (MI->isBundle() && HII.getBundleNoShuf(*MI))
7690b57cec5SDimitry Andric     HexagonMCInstrInfo::setMemReorderDisabled(MCB);
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric   MCContext &Ctx = OutStreamer->getContext();
7720b57cec5SDimitry Andric   bool Ok = HexagonMCInstrInfo::canonicalizePacket(MCII, *Subtarget, Ctx,
7730b57cec5SDimitry Andric                                                    MCB, nullptr);
7740b57cec5SDimitry Andric   assert(Ok); (void)Ok;
7750b57cec5SDimitry Andric   if (HexagonMCInstrInfo::bundleSize(MCB) == 0)
7760b57cec5SDimitry Andric     return;
7775ffd83dbSDimitry Andric   OutStreamer->emitInstruction(MCB, getSubtargetInfo());
7780b57cec5SDimitry Andric }
7790b57cec5SDimitry Andric 
emitStartOfAsmFile(Module & M)780*0fca6ea1SDimitry Andric void HexagonAsmPrinter::emitStartOfAsmFile(Module &M) {
781*0fca6ea1SDimitry Andric   if (TM.getTargetTriple().isOSBinFormatELF())
782*0fca6ea1SDimitry Andric     emitAttributes();
783*0fca6ea1SDimitry Andric }
784*0fca6ea1SDimitry Andric 
emitEndOfAsmFile(Module & M)785*0fca6ea1SDimitry Andric void HexagonAsmPrinter::emitEndOfAsmFile(Module &M) {
786*0fca6ea1SDimitry Andric   HexagonTargetStreamer &HTS =
787*0fca6ea1SDimitry Andric       static_cast<HexagonTargetStreamer &>(*OutStreamer->getTargetStreamer());
788*0fca6ea1SDimitry Andric   if (TM.getTargetTriple().isOSBinFormatELF())
789*0fca6ea1SDimitry Andric     HTS.finishAttributeSection();
790*0fca6ea1SDimitry Andric }
791*0fca6ea1SDimitry Andric 
emitAttributes()792*0fca6ea1SDimitry Andric void HexagonAsmPrinter::emitAttributes() {
793*0fca6ea1SDimitry Andric   HexagonTargetStreamer &HTS =
794*0fca6ea1SDimitry Andric       static_cast<HexagonTargetStreamer &>(*OutStreamer->getTargetStreamer());
795*0fca6ea1SDimitry Andric   HTS.emitTargetAttributes(*TM.getMCSubtargetInfo());
796*0fca6ea1SDimitry Andric }
797*0fca6ea1SDimitry Andric 
EmitSled(const MachineInstr & MI,SledKind Kind)7980eae32dcSDimitry Andric void HexagonAsmPrinter::EmitSled(const MachineInstr &MI, SledKind Kind) {
7990eae32dcSDimitry Andric   static const int8_t NoopsInSledCount = 4;
8000eae32dcSDimitry Andric   // We want to emit the following pattern:
8010eae32dcSDimitry Andric   //
8020eae32dcSDimitry Andric   // .L_xray_sled_N:
8030eae32dcSDimitry Andric   // <xray_sled_base>:
8040eae32dcSDimitry Andric   // { 	jump .Ltmp0 }
8050eae32dcSDimitry Andric   // {  nop
8060eae32dcSDimitry Andric   //    nop
8070eae32dcSDimitry Andric   //    nop
8080eae32dcSDimitry Andric   //    nop }
8090eae32dcSDimitry Andric   // .Ltmp0:
8100eae32dcSDimitry Andric   //
8110eae32dcSDimitry Andric   // We need the 4 nop words because at runtime, we'd be patching over the
8120eae32dcSDimitry Andric   // full 5 words with the following pattern:
8130eae32dcSDimitry Andric   //
8140eae32dcSDimitry Andric   // <xray_sled_n>:
8150eae32dcSDimitry Andric   // { 	immext(#...) // upper 26-bits of trampoline
8160eae32dcSDimitry Andric   //    r6 = ##...   // lower  6-bits of trampoline
8170eae32dcSDimitry Andric   //    immext(#...) // upper 26-bits of func id
8180eae32dcSDimitry Andric   //    r7 = ##... }  // lower 6 bits of func id
8190eae32dcSDimitry Andric   // { 	callr r6 }
8200eae32dcSDimitry Andric   //
8210eae32dcSDimitry Andric   //
8220eae32dcSDimitry Andric   auto CurSled = OutContext.createTempSymbol("xray_sled_", true);
8230eae32dcSDimitry Andric   OutStreamer->emitLabel(CurSled);
8240eae32dcSDimitry Andric 
8250eae32dcSDimitry Andric   MCInst *SledJump = new (OutContext) MCInst();
8260eae32dcSDimitry Andric   SledJump->setOpcode(Hexagon::J2_jump);
8270eae32dcSDimitry Andric   auto PostSled = OutContext.createTempSymbol();
8280eae32dcSDimitry Andric   SledJump->addOperand(MCOperand::createExpr(HexagonMCExpr::create(
8290eae32dcSDimitry Andric       MCSymbolRefExpr::create(PostSled, OutContext), OutContext)));
8300eae32dcSDimitry Andric 
8310eae32dcSDimitry Andric   // Emit "jump PostSled" instruction, which jumps over the nop series.
8320eae32dcSDimitry Andric   MCInst SledJumpPacket;
8330eae32dcSDimitry Andric   SledJumpPacket.setOpcode(Hexagon::BUNDLE);
8340eae32dcSDimitry Andric   SledJumpPacket.addOperand(MCOperand::createImm(0));
8350eae32dcSDimitry Andric   SledJumpPacket.addOperand(MCOperand::createInst(SledJump));
8360eae32dcSDimitry Andric 
8370eae32dcSDimitry Andric   EmitToStreamer(*OutStreamer, SledJumpPacket);
8380eae32dcSDimitry Andric 
8390eae32dcSDimitry Andric   // FIXME: this will emit individual packets, we should
8400eae32dcSDimitry Andric   // special-case this and combine them into a single packet.
8410eae32dcSDimitry Andric   emitNops(NoopsInSledCount);
8420eae32dcSDimitry Andric 
8430eae32dcSDimitry Andric   OutStreamer->emitLabel(PostSled);
844bdd1243dSDimitry Andric   recordSled(CurSled, MI, Kind, 2);
8450eae32dcSDimitry Andric }
8460eae32dcSDimitry Andric 
LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr & MI)8470eae32dcSDimitry Andric void HexagonAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI) {
8480eae32dcSDimitry Andric   EmitSled(MI, SledKind::FUNCTION_ENTER);
8490eae32dcSDimitry Andric }
8500eae32dcSDimitry Andric 
LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr & MI)8510eae32dcSDimitry Andric void HexagonAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI) {
8520eae32dcSDimitry Andric   EmitSled(MI, SledKind::FUNCTION_EXIT);
8530eae32dcSDimitry Andric }
8540eae32dcSDimitry Andric 
LowerPATCHABLE_TAIL_CALL(const MachineInstr & MI)8550eae32dcSDimitry Andric void HexagonAsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI) {
8560eae32dcSDimitry Andric   EmitSled(MI, SledKind::TAIL_CALL);
8570eae32dcSDimitry Andric }
8580eae32dcSDimitry Andric 
LLVMInitializeHexagonAsmPrinter()859480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeHexagonAsmPrinter() {
8600b57cec5SDimitry Andric   RegisterAsmPrinter<HexagonAsmPrinter> X(getTheHexagonTarget());
8610b57cec5SDimitry Andric }
862