10b57cec5SDimitry Andric //===- Mips16InstrInfo.cpp - Mips16 Instruction Information ---------------===//
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 the Mips16 implementation of the TargetInstrInfo class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "Mips16InstrInfo.h"
140b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
240b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
250b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
260b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
270b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
280b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
290b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
300b57cec5SDimitry Andric #include <cassert>
310b57cec5SDimitry Andric #include <cctype>
320b57cec5SDimitry Andric #include <cstdint>
330b57cec5SDimitry Andric #include <cstdlib>
340b57cec5SDimitry Andric #include <cstring>
350b57cec5SDimitry Andric #include <iterator>
360b57cec5SDimitry Andric #include <vector>
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric using namespace llvm;
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric #define DEBUG_TYPE "mips16-instrinfo"
410b57cec5SDimitry Andric
Mips16InstrInfo(const MipsSubtarget & STI)420b57cec5SDimitry Andric Mips16InstrInfo::Mips16InstrInfo(const MipsSubtarget &STI)
430b57cec5SDimitry Andric : MipsInstrInfo(STI, Mips::Bimm16) {}
440b57cec5SDimitry Andric
getRegisterInfo() const450b57cec5SDimitry Andric const MipsRegisterInfo &Mips16InstrInfo::getRegisterInfo() const {
460b57cec5SDimitry Andric return RI;
470b57cec5SDimitry Andric }
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric /// isLoadFromStackSlot - If the specified machine instruction is a direct
500b57cec5SDimitry Andric /// load from a stack slot, return the virtual or physical register number of
510b57cec5SDimitry Andric /// the destination along with the FrameIndex of the loaded stack slot. If
520b57cec5SDimitry Andric /// not, return 0. This predicate must return 0 if the instruction has
530b57cec5SDimitry Andric /// any side effects other than loading from the stack slot.
isLoadFromStackSlot(const MachineInstr & MI,int & FrameIndex) const54*0fca6ea1SDimitry Andric Register Mips16InstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
550b57cec5SDimitry Andric int &FrameIndex) const {
560b57cec5SDimitry Andric return 0;
570b57cec5SDimitry Andric }
580b57cec5SDimitry Andric
590b57cec5SDimitry Andric /// isStoreToStackSlot - If the specified machine instruction is a direct
600b57cec5SDimitry Andric /// store to a stack slot, return the virtual or physical register number of
610b57cec5SDimitry Andric /// the source reg along with the FrameIndex of the loaded stack slot. If
620b57cec5SDimitry Andric /// not, return 0. This predicate must return 0 if the instruction has
630b57cec5SDimitry Andric /// any side effects other than storing to the stack slot.
isStoreToStackSlot(const MachineInstr & MI,int & FrameIndex) const64*0fca6ea1SDimitry Andric Register Mips16InstrInfo::isStoreToStackSlot(const MachineInstr &MI,
650b57cec5SDimitry Andric int &FrameIndex) const {
660b57cec5SDimitry Andric return 0;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric
copyPhysReg(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,const DebugLoc & DL,MCRegister DestReg,MCRegister SrcReg,bool KillSrc) const690b57cec5SDimitry Andric void Mips16InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
700b57cec5SDimitry Andric MachineBasicBlock::iterator I,
71480093f4SDimitry Andric const DebugLoc &DL, MCRegister DestReg,
72480093f4SDimitry Andric MCRegister SrcReg, bool KillSrc) const {
730b57cec5SDimitry Andric unsigned Opc = 0;
740b57cec5SDimitry Andric
750b57cec5SDimitry Andric if (Mips::CPU16RegsRegClass.contains(DestReg) &&
760b57cec5SDimitry Andric Mips::GPR32RegClass.contains(SrcReg))
770b57cec5SDimitry Andric Opc = Mips::MoveR3216;
780b57cec5SDimitry Andric else if (Mips::GPR32RegClass.contains(DestReg) &&
790b57cec5SDimitry Andric Mips::CPU16RegsRegClass.contains(SrcReg))
800b57cec5SDimitry Andric Opc = Mips::Move32R16;
810b57cec5SDimitry Andric else if ((SrcReg == Mips::HI0) &&
820b57cec5SDimitry Andric (Mips::CPU16RegsRegClass.contains(DestReg)))
830b57cec5SDimitry Andric Opc = Mips::Mfhi16, SrcReg = 0;
840b57cec5SDimitry Andric else if ((SrcReg == Mips::LO0) &&
850b57cec5SDimitry Andric (Mips::CPU16RegsRegClass.contains(DestReg)))
860b57cec5SDimitry Andric Opc = Mips::Mflo16, SrcReg = 0;
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric assert(Opc && "Cannot copy registers");
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric if (DestReg)
930b57cec5SDimitry Andric MIB.addReg(DestReg, RegState::Define);
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric if (SrcReg)
960b57cec5SDimitry Andric MIB.addReg(SrcReg, getKillRegState(KillSrc));
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric
99bdd1243dSDimitry Andric std::optional<DestSourcePair>
isCopyInstrImpl(const MachineInstr & MI) const100480093f4SDimitry Andric Mips16InstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
101480093f4SDimitry Andric if (MI.isMoveReg())
102480093f4SDimitry Andric return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
103bdd1243dSDimitry Andric return std::nullopt;
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric
storeRegToStack(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,Register SrcReg,bool isKill,int FI,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI,int64_t Offset) const1060b57cec5SDimitry Andric void Mips16InstrInfo::storeRegToStack(MachineBasicBlock &MBB,
1070b57cec5SDimitry Andric MachineBasicBlock::iterator I,
1085ffd83dbSDimitry Andric Register SrcReg, bool isKill, int FI,
1090b57cec5SDimitry Andric const TargetRegisterClass *RC,
1100b57cec5SDimitry Andric const TargetRegisterInfo *TRI,
1110b57cec5SDimitry Andric int64_t Offset) const {
1120b57cec5SDimitry Andric DebugLoc DL;
1130b57cec5SDimitry Andric if (I != MBB.end()) DL = I->getDebugLoc();
1140b57cec5SDimitry Andric MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
1150b57cec5SDimitry Andric unsigned Opc = 0;
1160b57cec5SDimitry Andric if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
1170b57cec5SDimitry Andric Opc = Mips::SwRxSpImmX16;
1180b57cec5SDimitry Andric assert(Opc && "Register class not handled!");
1190b57cec5SDimitry Andric BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill)).
1200b57cec5SDimitry Andric addFrameIndex(FI).addImm(Offset)
1210b57cec5SDimitry Andric .addMemOperand(MMO);
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric
loadRegFromStack(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,Register DestReg,int FI,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI,int64_t Offset) const1240b57cec5SDimitry Andric void Mips16InstrInfo::loadRegFromStack(MachineBasicBlock &MBB,
1250b57cec5SDimitry Andric MachineBasicBlock::iterator I,
1265ffd83dbSDimitry Andric Register DestReg, int FI,
1270b57cec5SDimitry Andric const TargetRegisterClass *RC,
1280b57cec5SDimitry Andric const TargetRegisterInfo *TRI,
1290b57cec5SDimitry Andric int64_t Offset) const {
1300b57cec5SDimitry Andric DebugLoc DL;
1310b57cec5SDimitry Andric if (I != MBB.end()) DL = I->getDebugLoc();
1320b57cec5SDimitry Andric MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
1330b57cec5SDimitry Andric unsigned Opc = 0;
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
1360b57cec5SDimitry Andric Opc = Mips::LwRxSpImmX16;
1370b57cec5SDimitry Andric assert(Opc && "Register class not handled!");
1380b57cec5SDimitry Andric BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(Offset)
1390b57cec5SDimitry Andric .addMemOperand(MMO);
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric
expandPostRAPseudo(MachineInstr & MI) const1420b57cec5SDimitry Andric bool Mips16InstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1430b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent();
1440b57cec5SDimitry Andric switch (MI.getDesc().getOpcode()) {
1450b57cec5SDimitry Andric default:
1460b57cec5SDimitry Andric return false;
1470b57cec5SDimitry Andric case Mips::RetRA16:
1480b57cec5SDimitry Andric ExpandRetRA16(MBB, MI, Mips::JrcRa16);
1490b57cec5SDimitry Andric break;
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric
1520b57cec5SDimitry Andric MBB.erase(MI.getIterator());
1530b57cec5SDimitry Andric return true;
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric
1560b57cec5SDimitry Andric /// GetOppositeBranchOpc - Return the inverse of the specified
1570b57cec5SDimitry Andric /// opcode, e.g. turning BEQ to BNE.
getOppositeBranchOpc(unsigned Opc) const1580b57cec5SDimitry Andric unsigned Mips16InstrInfo::getOppositeBranchOpc(unsigned Opc) const {
1590b57cec5SDimitry Andric switch (Opc) {
1600b57cec5SDimitry Andric case Mips::BeqzRxImmX16: return Mips::BnezRxImmX16;
1610b57cec5SDimitry Andric case Mips::BnezRxImmX16: return Mips::BeqzRxImmX16;
1620b57cec5SDimitry Andric case Mips::BeqzRxImm16: return Mips::BnezRxImm16;
1630b57cec5SDimitry Andric case Mips::BnezRxImm16: return Mips::BeqzRxImm16;
1640b57cec5SDimitry Andric case Mips::BteqzT8CmpX16: return Mips::BtnezT8CmpX16;
1650b57cec5SDimitry Andric case Mips::BteqzT8SltX16: return Mips::BtnezT8SltX16;
1660b57cec5SDimitry Andric case Mips::BteqzT8SltiX16: return Mips::BtnezT8SltiX16;
1670b57cec5SDimitry Andric case Mips::Btnez16: return Mips::Bteqz16;
1680b57cec5SDimitry Andric case Mips::BtnezX16: return Mips::BteqzX16;
1690b57cec5SDimitry Andric case Mips::BtnezT8CmpiX16: return Mips::BteqzT8CmpiX16;
1700b57cec5SDimitry Andric case Mips::BtnezT8SltuX16: return Mips::BteqzT8SltuX16;
1710b57cec5SDimitry Andric case Mips::BtnezT8SltiuX16: return Mips::BteqzT8SltiuX16;
1720b57cec5SDimitry Andric case Mips::Bteqz16: return Mips::Btnez16;
1730b57cec5SDimitry Andric case Mips::BteqzX16: return Mips::BtnezX16;
1740b57cec5SDimitry Andric case Mips::BteqzT8CmpiX16: return Mips::BtnezT8CmpiX16;
1750b57cec5SDimitry Andric case Mips::BteqzT8SltuX16: return Mips::BtnezT8SltuX16;
1760b57cec5SDimitry Andric case Mips::BteqzT8SltiuX16: return Mips::BtnezT8SltiuX16;
1770b57cec5SDimitry Andric case Mips::BtnezT8CmpX16: return Mips::BteqzT8CmpX16;
1780b57cec5SDimitry Andric case Mips::BtnezT8SltX16: return Mips::BteqzT8SltX16;
1790b57cec5SDimitry Andric case Mips::BtnezT8SltiX16: return Mips::BteqzT8SltiX16;
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric llvm_unreachable("Illegal opcode!");
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric
addSaveRestoreRegs(MachineInstrBuilder & MIB,ArrayRef<CalleeSavedInfo> CSI,unsigned Flags=0)1840b57cec5SDimitry Andric static void addSaveRestoreRegs(MachineInstrBuilder &MIB,
1855ffd83dbSDimitry Andric ArrayRef<CalleeSavedInfo> CSI,
1860b57cec5SDimitry Andric unsigned Flags = 0) {
1870b57cec5SDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1880b57cec5SDimitry Andric // Add the callee-saved register as live-in. Do not add if the register is
1890b57cec5SDimitry Andric // RA and return address is taken, because it has already been added in
1900b57cec5SDimitry Andric // method MipsTargetLowering::lowerRETURNADDR.
1910b57cec5SDimitry Andric // It's killed at the spill, unless the register is RA and return address
1920b57cec5SDimitry Andric // is taken.
19304eeddc0SDimitry Andric Register Reg = CSI[e-i-1].getReg();
1940b57cec5SDimitry Andric switch (Reg) {
1950b57cec5SDimitry Andric case Mips::RA:
1960b57cec5SDimitry Andric case Mips::S0:
1970b57cec5SDimitry Andric case Mips::S1:
1980b57cec5SDimitry Andric MIB.addReg(Reg, Flags);
1990b57cec5SDimitry Andric break;
2000b57cec5SDimitry Andric case Mips::S2:
2010b57cec5SDimitry Andric break;
2020b57cec5SDimitry Andric default:
2030b57cec5SDimitry Andric llvm_unreachable("unexpected mips16 callee saved register");
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric }
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric
2090b57cec5SDimitry Andric // Adjust SP by FrameSize bytes. Save RA, S0, S1
makeFrame(unsigned SP,int64_t FrameSize,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const2100b57cec5SDimitry Andric void Mips16InstrInfo::makeFrame(unsigned SP, int64_t FrameSize,
2110b57cec5SDimitry Andric MachineBasicBlock &MBB,
2120b57cec5SDimitry Andric MachineBasicBlock::iterator I) const {
2130b57cec5SDimitry Andric DebugLoc DL;
2140b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent();
2150b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo();
2160b57cec5SDimitry Andric const BitVector Reserved = RI.getReservedRegs(MF);
2170b57cec5SDimitry Andric bool SaveS2 = Reserved[Mips::S2];
2180b57cec5SDimitry Andric MachineInstrBuilder MIB;
2190b57cec5SDimitry Andric unsigned Opc = ((FrameSize <= 128) && !SaveS2)? Mips::Save16:Mips::SaveX16;
2200b57cec5SDimitry Andric MIB = BuildMI(MBB, I, DL, get(Opc));
2210b57cec5SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
2220b57cec5SDimitry Andric addSaveRestoreRegs(MIB, CSI);
2230b57cec5SDimitry Andric if (SaveS2)
2240b57cec5SDimitry Andric MIB.addReg(Mips::S2);
2250b57cec5SDimitry Andric if (isUInt<11>(FrameSize))
2260b57cec5SDimitry Andric MIB.addImm(FrameSize);
2270b57cec5SDimitry Andric else {
2280b57cec5SDimitry Andric int Base = 2040; // should create template function like isUInt that
2290b57cec5SDimitry Andric // returns largest possible n bit unsigned integer
2300b57cec5SDimitry Andric int64_t Remainder = FrameSize - Base;
2310b57cec5SDimitry Andric MIB.addImm(Base);
2320b57cec5SDimitry Andric if (isInt<16>(-Remainder))
2330b57cec5SDimitry Andric BuildAddiuSpImm(MBB, I, -Remainder);
2340b57cec5SDimitry Andric else
2350b57cec5SDimitry Andric adjustStackPtrBig(SP, -Remainder, MBB, I, Mips::V0, Mips::V1);
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric }
2380b57cec5SDimitry Andric
2390b57cec5SDimitry Andric // Adjust SP by FrameSize bytes. Restore RA, S0, S1
restoreFrame(unsigned SP,int64_t FrameSize,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const2400b57cec5SDimitry Andric void Mips16InstrInfo::restoreFrame(unsigned SP, int64_t FrameSize,
2410b57cec5SDimitry Andric MachineBasicBlock &MBB,
2420b57cec5SDimitry Andric MachineBasicBlock::iterator I) const {
2430b57cec5SDimitry Andric DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
2440b57cec5SDimitry Andric MachineFunction *MF = MBB.getParent();
2450b57cec5SDimitry Andric MachineFrameInfo &MFI = MF->getFrameInfo();
2460b57cec5SDimitry Andric const BitVector Reserved = RI.getReservedRegs(*MF);
2470b57cec5SDimitry Andric bool SaveS2 = Reserved[Mips::S2];
2480b57cec5SDimitry Andric MachineInstrBuilder MIB;
2490b57cec5SDimitry Andric unsigned Opc = ((FrameSize <= 128) && !SaveS2)?
2500b57cec5SDimitry Andric Mips::Restore16:Mips::RestoreX16;
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andric if (!isUInt<11>(FrameSize)) {
2530b57cec5SDimitry Andric unsigned Base = 2040;
2540b57cec5SDimitry Andric int64_t Remainder = FrameSize - Base;
2550b57cec5SDimitry Andric FrameSize = Base; // should create template function like isUInt that
2560b57cec5SDimitry Andric // returns largest possible n bit unsigned integer
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andric if (isInt<16>(Remainder))
2590b57cec5SDimitry Andric BuildAddiuSpImm(MBB, I, Remainder);
2600b57cec5SDimitry Andric else
2610b57cec5SDimitry Andric adjustStackPtrBig(SP, Remainder, MBB, I, Mips::A0, Mips::A1);
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric MIB = BuildMI(MBB, I, DL, get(Opc));
2640b57cec5SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
2650b57cec5SDimitry Andric addSaveRestoreRegs(MIB, CSI, RegState::Define);
2660b57cec5SDimitry Andric if (SaveS2)
2670b57cec5SDimitry Andric MIB.addReg(Mips::S2, RegState::Define);
2680b57cec5SDimitry Andric MIB.addImm(FrameSize);
2690b57cec5SDimitry Andric }
2700b57cec5SDimitry Andric
2710b57cec5SDimitry Andric // Adjust SP by Amount bytes where bytes can be up to 32bit number.
2720b57cec5SDimitry Andric // This can only be called at times that we know that there is at least one free
2730b57cec5SDimitry Andric // register.
2740b57cec5SDimitry Andric // This is clearly safe at prologue and epilogue.
adjustStackPtrBig(unsigned SP,int64_t Amount,MachineBasicBlock & MBB,MachineBasicBlock::iterator I,unsigned Reg1,unsigned Reg2) const2750b57cec5SDimitry Andric void Mips16InstrInfo::adjustStackPtrBig(unsigned SP, int64_t Amount,
2760b57cec5SDimitry Andric MachineBasicBlock &MBB,
2770b57cec5SDimitry Andric MachineBasicBlock::iterator I,
2780b57cec5SDimitry Andric unsigned Reg1, unsigned Reg2) const {
2790b57cec5SDimitry Andric DebugLoc DL;
2800b57cec5SDimitry Andric //
2810b57cec5SDimitry Andric // li reg1, constant
2820b57cec5SDimitry Andric // move reg2, sp
2830b57cec5SDimitry Andric // add reg1, reg1, reg2
2840b57cec5SDimitry Andric // move sp, reg1
2850b57cec5SDimitry Andric //
2860b57cec5SDimitry Andric //
2870b57cec5SDimitry Andric MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwConstant32), Reg1);
2880b57cec5SDimitry Andric MIB1.addImm(Amount).addImm(-1);
2890b57cec5SDimitry Andric MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::MoveR3216), Reg2);
2900b57cec5SDimitry Andric MIB2.addReg(Mips::SP, RegState::Kill);
2910b57cec5SDimitry Andric MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::AdduRxRyRz16), Reg1);
2920b57cec5SDimitry Andric MIB3.addReg(Reg1);
2930b57cec5SDimitry Andric MIB3.addReg(Reg2, RegState::Kill);
2940b57cec5SDimitry Andric MachineInstrBuilder MIB4 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
2950b57cec5SDimitry Andric Mips::SP);
2960b57cec5SDimitry Andric MIB4.addReg(Reg1, RegState::Kill);
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric
adjustStackPtrBigUnrestricted(unsigned SP,int64_t Amount,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const2990b57cec5SDimitry Andric void Mips16InstrInfo::adjustStackPtrBigUnrestricted(
3000b57cec5SDimitry Andric unsigned SP, int64_t Amount, MachineBasicBlock &MBB,
3010b57cec5SDimitry Andric MachineBasicBlock::iterator I) const {
3020b57cec5SDimitry Andric llvm_unreachable("adjust stack pointer amount exceeded");
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andric /// Adjust SP by Amount bytes.
adjustStackPtr(unsigned SP,int64_t Amount,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const3060b57cec5SDimitry Andric void Mips16InstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
3070b57cec5SDimitry Andric MachineBasicBlock &MBB,
3080b57cec5SDimitry Andric MachineBasicBlock::iterator I) const {
3090b57cec5SDimitry Andric if (Amount == 0)
3100b57cec5SDimitry Andric return;
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andric if (isInt<16>(Amount)) // need to change to addiu sp, ....and isInt<16>
3130b57cec5SDimitry Andric BuildAddiuSpImm(MBB, I, Amount);
3140b57cec5SDimitry Andric else
3150b57cec5SDimitry Andric adjustStackPtrBigUnrestricted(SP, Amount, MBB, I);
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andric /// This function generates the sequence of instructions needed to get the
3190b57cec5SDimitry Andric /// result of adding register REG and immediate IMM.
loadImmediate(unsigned FrameReg,int64_t Imm,MachineBasicBlock & MBB,MachineBasicBlock::iterator II,const DebugLoc & DL,unsigned & NewImm) const3200b57cec5SDimitry Andric unsigned Mips16InstrInfo::loadImmediate(unsigned FrameReg, int64_t Imm,
3210b57cec5SDimitry Andric MachineBasicBlock &MBB,
3220b57cec5SDimitry Andric MachineBasicBlock::iterator II,
3230b57cec5SDimitry Andric const DebugLoc &DL,
3240b57cec5SDimitry Andric unsigned &NewImm) const {
3250b57cec5SDimitry Andric //
3260b57cec5SDimitry Andric // given original instruction is:
3270b57cec5SDimitry Andric // Instr rx, T[offset] where offset is too big.
3280b57cec5SDimitry Andric //
3290b57cec5SDimitry Andric // lo = offset & 0xFFFF
3300b57cec5SDimitry Andric // hi = ((offset >> 16) + (lo >> 15)) & 0xFFFF;
3310b57cec5SDimitry Andric //
3320b57cec5SDimitry Andric // let T = temporary register
3330b57cec5SDimitry Andric // li T, hi
3340b57cec5SDimitry Andric // shl T, 16
3350b57cec5SDimitry Andric // add T, Rx, T
3360b57cec5SDimitry Andric //
3370b57cec5SDimitry Andric RegScavenger rs;
3380b57cec5SDimitry Andric int32_t lo = Imm & 0xFFFF;
3390b57cec5SDimitry Andric NewImm = lo;
3400b57cec5SDimitry Andric int Reg =0;
3410b57cec5SDimitry Andric int SpReg = 0;
3420b57cec5SDimitry Andric
34306c3fb27SDimitry Andric rs.enterBasicBlockEnd(MBB);
3445f757f3fSDimitry Andric rs.backward(std::next(II));
3450b57cec5SDimitry Andric //
3460b57cec5SDimitry Andric // We need to know which registers can be used, in the case where there
3470b57cec5SDimitry Andric // are not enough free registers. We exclude all registers that
3480b57cec5SDimitry Andric // are used in the instruction that we are helping.
3490b57cec5SDimitry Andric // // Consider all allocatable registers in the register class initially
3500b57cec5SDimitry Andric BitVector Candidates =
3510b57cec5SDimitry Andric RI.getAllocatableSet
3520b57cec5SDimitry Andric (*II->getParent()->getParent(), &Mips::CPU16RegsRegClass);
3530b57cec5SDimitry Andric // Exclude all the registers being used by the instruction.
35406c3fb27SDimitry Andric for (MachineOperand &MO : II->operands()) {
3550b57cec5SDimitry Andric if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
356bdd1243dSDimitry Andric !MO.getReg().isVirtual())
3570b57cec5SDimitry Andric Candidates.reset(MO.getReg());
3580b57cec5SDimitry Andric }
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andric // If the same register was used and defined in an instruction, then
3610b57cec5SDimitry Andric // it will not be in the list of candidates.
3620b57cec5SDimitry Andric //
3630b57cec5SDimitry Andric // we need to analyze the instruction that we are helping.
3640b57cec5SDimitry Andric // we need to know if it defines register x but register x is not
3650b57cec5SDimitry Andric // present as an operand of the instruction. this tells
3660b57cec5SDimitry Andric // whether the register is live before the instruction. if it's not
3670b57cec5SDimitry Andric // then we don't need to save it in case there are no free registers.
3680b57cec5SDimitry Andric int DefReg = 0;
36906c3fb27SDimitry Andric for (MachineOperand &MO : II->operands()) {
3700b57cec5SDimitry Andric if (MO.isReg() && MO.isDef()) {
3710b57cec5SDimitry Andric DefReg = MO.getReg();
3720b57cec5SDimitry Andric break;
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric }
3750b57cec5SDimitry Andric
3760b57cec5SDimitry Andric BitVector Available = rs.getRegsAvailable(&Mips::CPU16RegsRegClass);
3770b57cec5SDimitry Andric Available &= Candidates;
3780b57cec5SDimitry Andric //
3790b57cec5SDimitry Andric // we use T0 for the first register, if we need to save something away.
3800b57cec5SDimitry Andric // we use T1 for the second register, if we need to save something away.
3810b57cec5SDimitry Andric //
3820b57cec5SDimitry Andric unsigned FirstRegSaved =0, SecondRegSaved=0;
3830b57cec5SDimitry Andric unsigned FirstRegSavedTo = 0, SecondRegSavedTo = 0;
3840b57cec5SDimitry Andric
3850b57cec5SDimitry Andric Reg = Available.find_first();
3860b57cec5SDimitry Andric
3870b57cec5SDimitry Andric if (Reg == -1) {
3880b57cec5SDimitry Andric Reg = Candidates.find_first();
3890b57cec5SDimitry Andric Candidates.reset(Reg);
3900b57cec5SDimitry Andric if (DefReg != Reg) {
3910b57cec5SDimitry Andric FirstRegSaved = Reg;
3920b57cec5SDimitry Andric FirstRegSavedTo = Mips::T0;
3930b57cec5SDimitry Andric copyPhysReg(MBB, II, DL, FirstRegSavedTo, FirstRegSaved, true);
3940b57cec5SDimitry Andric }
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric else
3970b57cec5SDimitry Andric Available.reset(Reg);
3980b57cec5SDimitry Andric BuildMI(MBB, II, DL, get(Mips::LwConstant32), Reg).addImm(Imm).addImm(-1);
3990b57cec5SDimitry Andric NewImm = 0;
4000b57cec5SDimitry Andric if (FrameReg == Mips::SP) {
4010b57cec5SDimitry Andric SpReg = Available.find_first();
4020b57cec5SDimitry Andric if (SpReg == -1) {
4030b57cec5SDimitry Andric SpReg = Candidates.find_first();
4040b57cec5SDimitry Andric // Candidates.reset(SpReg); // not really needed
4050b57cec5SDimitry Andric if (DefReg!= SpReg) {
4060b57cec5SDimitry Andric SecondRegSaved = SpReg;
4070b57cec5SDimitry Andric SecondRegSavedTo = Mips::T1;
4080b57cec5SDimitry Andric }
4090b57cec5SDimitry Andric if (SecondRegSaved)
4100b57cec5SDimitry Andric copyPhysReg(MBB, II, DL, SecondRegSavedTo, SecondRegSaved, true);
4110b57cec5SDimitry Andric }
4120b57cec5SDimitry Andric else
4130b57cec5SDimitry Andric Available.reset(SpReg);
4140b57cec5SDimitry Andric copyPhysReg(MBB, II, DL, SpReg, Mips::SP, false);
415480093f4SDimitry Andric BuildMI(MBB, II, DL, get(Mips::AdduRxRyRz16), Reg)
416480093f4SDimitry Andric .addReg(SpReg, RegState::Kill)
4170b57cec5SDimitry Andric .addReg(Reg);
4180b57cec5SDimitry Andric }
4190b57cec5SDimitry Andric else
4200b57cec5SDimitry Andric BuildMI(MBB, II, DL, get(Mips:: AdduRxRyRz16), Reg).addReg(FrameReg)
4210b57cec5SDimitry Andric .addReg(Reg, RegState::Kill);
4220b57cec5SDimitry Andric if (FirstRegSaved || SecondRegSaved) {
4230b57cec5SDimitry Andric II = std::next(II);
4240b57cec5SDimitry Andric if (FirstRegSaved)
4250b57cec5SDimitry Andric copyPhysReg(MBB, II, DL, FirstRegSaved, FirstRegSavedTo, true);
4260b57cec5SDimitry Andric if (SecondRegSaved)
4270b57cec5SDimitry Andric copyPhysReg(MBB, II, DL, SecondRegSaved, SecondRegSavedTo, true);
4280b57cec5SDimitry Andric }
4290b57cec5SDimitry Andric return Reg;
4300b57cec5SDimitry Andric }
4310b57cec5SDimitry Andric
getAnalyzableBrOpc(unsigned Opc) const4320b57cec5SDimitry Andric unsigned Mips16InstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
4330b57cec5SDimitry Andric return (Opc == Mips::BeqzRxImmX16 || Opc == Mips::BimmX16 ||
4340b57cec5SDimitry Andric Opc == Mips::Bimm16 ||
4350b57cec5SDimitry Andric Opc == Mips::Bteqz16 || Opc == Mips::Btnez16 ||
4360b57cec5SDimitry Andric Opc == Mips::BeqzRxImm16 || Opc == Mips::BnezRxImm16 ||
4370b57cec5SDimitry Andric Opc == Mips::BnezRxImmX16 || Opc == Mips::BteqzX16 ||
4380b57cec5SDimitry Andric Opc == Mips::BteqzT8CmpX16 || Opc == Mips::BteqzT8CmpiX16 ||
4390b57cec5SDimitry Andric Opc == Mips::BteqzT8SltX16 || Opc == Mips::BteqzT8SltuX16 ||
4400b57cec5SDimitry Andric Opc == Mips::BteqzT8SltiX16 || Opc == Mips::BteqzT8SltiuX16 ||
4410b57cec5SDimitry Andric Opc == Mips::BtnezX16 || Opc == Mips::BtnezT8CmpX16 ||
4420b57cec5SDimitry Andric Opc == Mips::BtnezT8CmpiX16 || Opc == Mips::BtnezT8SltX16 ||
4430b57cec5SDimitry Andric Opc == Mips::BtnezT8SltuX16 || Opc == Mips::BtnezT8SltiX16 ||
4440b57cec5SDimitry Andric Opc == Mips::BtnezT8SltiuX16 ) ? Opc : 0;
4450b57cec5SDimitry Andric }
4460b57cec5SDimitry Andric
ExpandRetRA16(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,unsigned Opc) const4470b57cec5SDimitry Andric void Mips16InstrInfo::ExpandRetRA16(MachineBasicBlock &MBB,
4480b57cec5SDimitry Andric MachineBasicBlock::iterator I,
4490b57cec5SDimitry Andric unsigned Opc) const {
4500b57cec5SDimitry Andric BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
4510b57cec5SDimitry Andric }
4520b57cec5SDimitry Andric
AddiuSpImm(int64_t Imm) const4530b57cec5SDimitry Andric const MCInstrDesc &Mips16InstrInfo::AddiuSpImm(int64_t Imm) const {
4540b57cec5SDimitry Andric if (validSpImm8(Imm))
4550b57cec5SDimitry Andric return get(Mips::AddiuSpImm16);
4560b57cec5SDimitry Andric else
4570b57cec5SDimitry Andric return get(Mips::AddiuSpImmX16);
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric
BuildAddiuSpImm(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,int64_t Imm) const4600b57cec5SDimitry Andric void Mips16InstrInfo::BuildAddiuSpImm
4610b57cec5SDimitry Andric (MachineBasicBlock &MBB, MachineBasicBlock::iterator I, int64_t Imm) const {
4620b57cec5SDimitry Andric DebugLoc DL;
4630b57cec5SDimitry Andric BuildMI(MBB, I, DL, AddiuSpImm(Imm)).addImm(Imm);
4640b57cec5SDimitry Andric }
4650b57cec5SDimitry Andric
createMips16InstrInfo(const MipsSubtarget & STI)4660b57cec5SDimitry Andric const MipsInstrInfo *llvm::createMips16InstrInfo(const MipsSubtarget &STI) {
4670b57cec5SDimitry Andric return new Mips16InstrInfo(STI);
4680b57cec5SDimitry Andric }
4690b57cec5SDimitry Andric
validImmediate(unsigned Opcode,unsigned Reg,int64_t Amount)4700b57cec5SDimitry Andric bool Mips16InstrInfo::validImmediate(unsigned Opcode, unsigned Reg,
4710b57cec5SDimitry Andric int64_t Amount) {
4720b57cec5SDimitry Andric switch (Opcode) {
4730b57cec5SDimitry Andric case Mips::LbRxRyOffMemX16:
4740b57cec5SDimitry Andric case Mips::LbuRxRyOffMemX16:
4750b57cec5SDimitry Andric case Mips::LhRxRyOffMemX16:
4760b57cec5SDimitry Andric case Mips::LhuRxRyOffMemX16:
4770b57cec5SDimitry Andric case Mips::SbRxRyOffMemX16:
4780b57cec5SDimitry Andric case Mips::ShRxRyOffMemX16:
4790b57cec5SDimitry Andric case Mips::LwRxRyOffMemX16:
4800b57cec5SDimitry Andric case Mips::SwRxRyOffMemX16:
4810b57cec5SDimitry Andric case Mips::SwRxSpImmX16:
4820b57cec5SDimitry Andric case Mips::LwRxSpImmX16:
4830b57cec5SDimitry Andric return isInt<16>(Amount);
4840b57cec5SDimitry Andric case Mips::AddiuRxRyOffMemX16:
4850b57cec5SDimitry Andric if ((Reg == Mips::PC) || (Reg == Mips::SP))
4860b57cec5SDimitry Andric return isInt<16>(Amount);
4870b57cec5SDimitry Andric return isInt<15>(Amount);
4880b57cec5SDimitry Andric }
4890b57cec5SDimitry Andric llvm_unreachable("unexpected Opcode in validImmediate");
4900b57cec5SDimitry Andric }
491