106c3fb27SDimitry Andric //===-- RISCVRegisterInfo.cpp - RISC-V Register Information -----*- C++ -*-===// 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 // 906c3fb27SDimitry Andric // This file contains the RISC-V implementation of the TargetRegisterInfo class. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "RISCVRegisterInfo.h" 140b57cec5SDimitry Andric #include "RISCV.h" 155ffd83dbSDimitry Andric #include "RISCVMachineFunctionInfo.h" 160b57cec5SDimitry Andric #include "RISCVSubtarget.h" 17*5f757f3fSDimitry Andric #include "llvm/ADT/SmallSet.h" 1881ad6265SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 254824e7fdSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 260b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric #define GET_REGINFO_TARGET_DESC 290b57cec5SDimitry Andric #include "RISCVGenRegisterInfo.inc" 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric using namespace llvm; 320b57cec5SDimitry Andric 33bdd1243dSDimitry Andric static cl::opt<bool> 34bdd1243dSDimitry Andric DisableRegAllocHints("riscv-disable-regalloc-hints", cl::Hidden, 35bdd1243dSDimitry Andric cl::init(false), 36bdd1243dSDimitry Andric cl::desc("Disable two address hints for register " 37bdd1243dSDimitry Andric "allocation")); 38bdd1243dSDimitry Andric 398bcb0991SDimitry Andric static_assert(RISCV::X1 == RISCV::X0 + 1, "Register list not consecutive"); 408bcb0991SDimitry Andric static_assert(RISCV::X31 == RISCV::X0 + 31, "Register list not consecutive"); 41e8d8bef9SDimitry Andric static_assert(RISCV::F1_H == RISCV::F0_H + 1, "Register list not consecutive"); 42e8d8bef9SDimitry Andric static_assert(RISCV::F31_H == RISCV::F0_H + 31, 43e8d8bef9SDimitry Andric "Register list not consecutive"); 448bcb0991SDimitry Andric static_assert(RISCV::F1_F == RISCV::F0_F + 1, "Register list not consecutive"); 458bcb0991SDimitry Andric static_assert(RISCV::F31_F == RISCV::F0_F + 31, 468bcb0991SDimitry Andric "Register list not consecutive"); 478bcb0991SDimitry Andric static_assert(RISCV::F1_D == RISCV::F0_D + 1, "Register list not consecutive"); 488bcb0991SDimitry Andric static_assert(RISCV::F31_D == RISCV::F0_D + 31, 498bcb0991SDimitry Andric "Register list not consecutive"); 505ffd83dbSDimitry Andric static_assert(RISCV::V1 == RISCV::V0 + 1, "Register list not consecutive"); 515ffd83dbSDimitry Andric static_assert(RISCV::V31 == RISCV::V0 + 31, "Register list not consecutive"); 528bcb0991SDimitry Andric 530b57cec5SDimitry Andric RISCVRegisterInfo::RISCVRegisterInfo(unsigned HwMode) 540b57cec5SDimitry Andric : RISCVGenRegisterInfo(RISCV::X1, /*DwarfFlavour*/0, /*EHFlavor*/0, 550b57cec5SDimitry Andric /*PC*/0, HwMode) {} 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric const MCPhysReg * 580b57cec5SDimitry Andric RISCVRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 590b57cec5SDimitry Andric auto &Subtarget = MF->getSubtarget<RISCVSubtarget>(); 60e8d8bef9SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::GHC) 61e8d8bef9SDimitry Andric return CSR_NoRegs_SaveList; 620b57cec5SDimitry Andric if (MF->getFunction().hasFnAttribute("interrupt")) { 630b57cec5SDimitry Andric if (Subtarget.hasStdExtD()) 640b57cec5SDimitry Andric return CSR_XLEN_F64_Interrupt_SaveList; 650b57cec5SDimitry Andric if (Subtarget.hasStdExtF()) 660b57cec5SDimitry Andric return CSR_XLEN_F32_Interrupt_SaveList; 670b57cec5SDimitry Andric return CSR_Interrupt_SaveList; 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric switch (Subtarget.getTargetABI()) { 710b57cec5SDimitry Andric default: 720b57cec5SDimitry Andric llvm_unreachable("Unrecognized ABI"); 730b57cec5SDimitry Andric case RISCVABI::ABI_ILP32: 740b57cec5SDimitry Andric case RISCVABI::ABI_LP64: 750b57cec5SDimitry Andric return CSR_ILP32_LP64_SaveList; 760b57cec5SDimitry Andric case RISCVABI::ABI_ILP32F: 770b57cec5SDimitry Andric case RISCVABI::ABI_LP64F: 780b57cec5SDimitry Andric return CSR_ILP32F_LP64F_SaveList; 790b57cec5SDimitry Andric case RISCVABI::ABI_ILP32D: 800b57cec5SDimitry Andric case RISCVABI::ABI_LP64D: 810b57cec5SDimitry Andric return CSR_ILP32D_LP64D_SaveList; 820b57cec5SDimitry Andric } 830b57cec5SDimitry Andric } 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric BitVector RISCVRegisterInfo::getReservedRegs(const MachineFunction &MF) const { 86480093f4SDimitry Andric const RISCVFrameLowering *TFI = getFrameLowering(MF); 870b57cec5SDimitry Andric BitVector Reserved(getNumRegs()); 88*5f757f3fSDimitry Andric auto &Subtarget = MF.getSubtarget<RISCVSubtarget>(); 890b57cec5SDimitry Andric 90480093f4SDimitry Andric // Mark any registers requested to be reserved as such 91480093f4SDimitry Andric for (size_t Reg = 0; Reg < getNumRegs(); Reg++) { 92*5f757f3fSDimitry Andric if (Subtarget.isRegisterReservedByUser(Reg)) 93480093f4SDimitry Andric markSuperRegs(Reserved, Reg); 94480093f4SDimitry Andric } 95480093f4SDimitry Andric 960b57cec5SDimitry Andric // Use markSuperRegs to ensure any register aliases are also reserved 970b57cec5SDimitry Andric markSuperRegs(Reserved, RISCV::X0); // zero 980b57cec5SDimitry Andric markSuperRegs(Reserved, RISCV::X2); // sp 990b57cec5SDimitry Andric markSuperRegs(Reserved, RISCV::X3); // gp 1000b57cec5SDimitry Andric markSuperRegs(Reserved, RISCV::X4); // tp 1010b57cec5SDimitry Andric if (TFI->hasFP(MF)) 1020b57cec5SDimitry Andric markSuperRegs(Reserved, RISCV::X8); // fp 103480093f4SDimitry Andric // Reserve the base register if we need to realign the stack and allocate 104480093f4SDimitry Andric // variable-sized objects at runtime. 105480093f4SDimitry Andric if (TFI->hasBP(MF)) 106480093f4SDimitry Andric markSuperRegs(Reserved, RISCVABI::getBPReg()); // bp 107e8d8bef9SDimitry Andric 10806c3fb27SDimitry Andric // Additionally reserve dummy register used to form the register pair 10906c3fb27SDimitry Andric // beginning with 'x0' for instructions that take register pairs. 11006c3fb27SDimitry Andric markSuperRegs(Reserved, RISCV::DUMMY_REG_PAIR_WITH_X0); 11106c3fb27SDimitry Andric 112e8d8bef9SDimitry Andric // V registers for code generation. We handle them manually. 113e8d8bef9SDimitry Andric markSuperRegs(Reserved, RISCV::VL); 114e8d8bef9SDimitry Andric markSuperRegs(Reserved, RISCV::VTYPE); 115e8d8bef9SDimitry Andric markSuperRegs(Reserved, RISCV::VXSAT); 116e8d8bef9SDimitry Andric markSuperRegs(Reserved, RISCV::VXRM); 11781ad6265SDimitry Andric markSuperRegs(Reserved, RISCV::VLENB); // vlenb (constant) 118e8d8bef9SDimitry Andric 119fe6060f1SDimitry Andric // Floating point environment registers. 120fe6060f1SDimitry Andric markSuperRegs(Reserved, RISCV::FRM); 121fe6060f1SDimitry Andric markSuperRegs(Reserved, RISCV::FFLAGS); 122fe6060f1SDimitry Andric 123*5f757f3fSDimitry Andric if (MF.getFunction().getCallingConv() == CallingConv::GRAAL) { 124*5f757f3fSDimitry Andric if (Subtarget.isRVE()) 125*5f757f3fSDimitry Andric report_fatal_error("Graal reserved registers do not exist in RVE"); 126*5f757f3fSDimitry Andric markSuperRegs(Reserved, RISCV::X23); 127*5f757f3fSDimitry Andric markSuperRegs(Reserved, RISCV::X27); 128*5f757f3fSDimitry Andric } 129*5f757f3fSDimitry Andric 1300b57cec5SDimitry Andric assert(checkAllSuperRegsMarked(Reserved)); 1310b57cec5SDimitry Andric return Reserved; 1320b57cec5SDimitry Andric } 1330b57cec5SDimitry Andric 134480093f4SDimitry Andric bool RISCVRegisterInfo::isAsmClobberable(const MachineFunction &MF, 1355ffd83dbSDimitry Andric MCRegister PhysReg) const { 136480093f4SDimitry Andric return !MF.getSubtarget<RISCVSubtarget>().isRegisterReservedByUser(PhysReg); 137480093f4SDimitry Andric } 138480093f4SDimitry Andric 1390b57cec5SDimitry Andric const uint32_t *RISCVRegisterInfo::getNoPreservedMask() const { 1400b57cec5SDimitry Andric return CSR_NoRegs_RegMask; 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric 1435ffd83dbSDimitry Andric // Frame indexes representing locations of CSRs which are given a fixed location 144*5f757f3fSDimitry Andric // by save/restore libcalls or Zcmp Push/Pop. 14581ad6265SDimitry Andric static const std::pair<unsigned, int> FixedCSRFIMap[] = { 1465ffd83dbSDimitry Andric {/*ra*/ RISCV::X1, -1}, 1475ffd83dbSDimitry Andric {/*s0*/ RISCV::X8, -2}, 1485ffd83dbSDimitry Andric {/*s1*/ RISCV::X9, -3}, 1495ffd83dbSDimitry Andric {/*s2*/ RISCV::X18, -4}, 1505ffd83dbSDimitry Andric {/*s3*/ RISCV::X19, -5}, 1515ffd83dbSDimitry Andric {/*s4*/ RISCV::X20, -6}, 1525ffd83dbSDimitry Andric {/*s5*/ RISCV::X21, -7}, 1535ffd83dbSDimitry Andric {/*s6*/ RISCV::X22, -8}, 1545ffd83dbSDimitry Andric {/*s7*/ RISCV::X23, -9}, 1555ffd83dbSDimitry Andric {/*s8*/ RISCV::X24, -10}, 1565ffd83dbSDimitry Andric {/*s9*/ RISCV::X25, -11}, 1575ffd83dbSDimitry Andric {/*s10*/ RISCV::X26, -12}, 1585ffd83dbSDimitry Andric {/*s11*/ RISCV::X27, -13} 1595ffd83dbSDimitry Andric }; 1605ffd83dbSDimitry Andric 1615ffd83dbSDimitry Andric bool RISCVRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 1625ffd83dbSDimitry Andric Register Reg, 1635ffd83dbSDimitry Andric int &FrameIdx) const { 1645ffd83dbSDimitry Andric const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>(); 16506c3fb27SDimitry Andric if (!RVFI->useSaveRestoreLibCalls(MF) && !RVFI->isPushable(MF)) 1665ffd83dbSDimitry Andric return false; 1675ffd83dbSDimitry Andric 16881ad6265SDimitry Andric const auto *FII = 16981ad6265SDimitry Andric llvm::find_if(FixedCSRFIMap, [&](auto P) { return P.first == Reg; }); 17081ad6265SDimitry Andric if (FII == std::end(FixedCSRFIMap)) 1715ffd83dbSDimitry Andric return false; 1725ffd83dbSDimitry Andric 1735ffd83dbSDimitry Andric FrameIdx = FII->second; 1745ffd83dbSDimitry Andric return true; 1755ffd83dbSDimitry Andric } 1765ffd83dbSDimitry Andric 177bdd1243dSDimitry Andric void RISCVRegisterInfo::adjustReg(MachineBasicBlock &MBB, 178bdd1243dSDimitry Andric MachineBasicBlock::iterator II, 179bdd1243dSDimitry Andric const DebugLoc &DL, Register DestReg, 180bdd1243dSDimitry Andric Register SrcReg, StackOffset Offset, 181bdd1243dSDimitry Andric MachineInstr::MIFlag Flag, 182bdd1243dSDimitry Andric MaybeAlign RequiredAlign) const { 183bdd1243dSDimitry Andric 184bdd1243dSDimitry Andric if (DestReg == SrcReg && !Offset.getFixed() && !Offset.getScalable()) 185bdd1243dSDimitry Andric return; 186bdd1243dSDimitry Andric 187bdd1243dSDimitry Andric MachineFunction &MF = *MBB.getParent(); 188bdd1243dSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 189bdd1243dSDimitry Andric const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>(); 190bdd1243dSDimitry Andric const RISCVInstrInfo *TII = ST.getInstrInfo(); 191bdd1243dSDimitry Andric 192bdd1243dSDimitry Andric bool KillSrcReg = false; 193bdd1243dSDimitry Andric 194bdd1243dSDimitry Andric if (Offset.getScalable()) { 195bdd1243dSDimitry Andric unsigned ScalableAdjOpc = RISCV::ADD; 196bdd1243dSDimitry Andric int64_t ScalableValue = Offset.getScalable(); 197bdd1243dSDimitry Andric if (ScalableValue < 0) { 198bdd1243dSDimitry Andric ScalableValue = -ScalableValue; 199bdd1243dSDimitry Andric ScalableAdjOpc = RISCV::SUB; 200bdd1243dSDimitry Andric } 201bdd1243dSDimitry Andric // Get vlenb and multiply vlen with the number of vector registers. 202bdd1243dSDimitry Andric Register ScratchReg = DestReg; 203bdd1243dSDimitry Andric if (DestReg == SrcReg) 204bdd1243dSDimitry Andric ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass); 205bdd1243dSDimitry Andric TII->getVLENFactoredAmount(MF, MBB, II, DL, ScratchReg, ScalableValue, Flag); 206bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(ScalableAdjOpc), DestReg) 207bdd1243dSDimitry Andric .addReg(SrcReg).addReg(ScratchReg, RegState::Kill) 208bdd1243dSDimitry Andric .setMIFlag(Flag); 209bdd1243dSDimitry Andric SrcReg = DestReg; 210bdd1243dSDimitry Andric KillSrcReg = true; 211bdd1243dSDimitry Andric } 212bdd1243dSDimitry Andric 213bdd1243dSDimitry Andric int64_t Val = Offset.getFixed(); 214bdd1243dSDimitry Andric if (DestReg == SrcReg && Val == 0) 215bdd1243dSDimitry Andric return; 216bdd1243dSDimitry Andric 217bdd1243dSDimitry Andric const uint64_t Align = RequiredAlign.valueOrOne().value(); 218bdd1243dSDimitry Andric 219bdd1243dSDimitry Andric if (isInt<12>(Val)) { 220bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), DestReg) 221bdd1243dSDimitry Andric .addReg(SrcReg, getKillRegState(KillSrcReg)) 222bdd1243dSDimitry Andric .addImm(Val) 223bdd1243dSDimitry Andric .setMIFlag(Flag); 224bdd1243dSDimitry Andric return; 225bdd1243dSDimitry Andric } 226bdd1243dSDimitry Andric 227bdd1243dSDimitry Andric // Try to split the offset across two ADDIs. We need to keep the intermediate 228bdd1243dSDimitry Andric // result aligned after each ADDI. We need to determine the maximum value we 229bdd1243dSDimitry Andric // can put in each ADDI. In the negative direction, we can use -2048 which is 230bdd1243dSDimitry Andric // always sufficiently aligned. In the positive direction, we need to find the 231bdd1243dSDimitry Andric // largest 12-bit immediate that is aligned. Exclude -4096 since it can be 232bdd1243dSDimitry Andric // created with LUI. 233bdd1243dSDimitry Andric assert(Align < 2048 && "Required alignment too large"); 234bdd1243dSDimitry Andric int64_t MaxPosAdjStep = 2048 - Align; 235bdd1243dSDimitry Andric if (Val > -4096 && Val <= (2 * MaxPosAdjStep)) { 236bdd1243dSDimitry Andric int64_t FirstAdj = Val < 0 ? -2048 : MaxPosAdjStep; 237bdd1243dSDimitry Andric Val -= FirstAdj; 238bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), DestReg) 239bdd1243dSDimitry Andric .addReg(SrcReg, getKillRegState(KillSrcReg)) 240bdd1243dSDimitry Andric .addImm(FirstAdj) 241bdd1243dSDimitry Andric .setMIFlag(Flag); 242bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), DestReg) 243bdd1243dSDimitry Andric .addReg(DestReg, RegState::Kill) 244bdd1243dSDimitry Andric .addImm(Val) 245bdd1243dSDimitry Andric .setMIFlag(Flag); 246bdd1243dSDimitry Andric return; 247bdd1243dSDimitry Andric } 248bdd1243dSDimitry Andric 249bdd1243dSDimitry Andric unsigned Opc = RISCV::ADD; 250bdd1243dSDimitry Andric if (Val < 0) { 251bdd1243dSDimitry Andric Val = -Val; 252bdd1243dSDimitry Andric Opc = RISCV::SUB; 253bdd1243dSDimitry Andric } 254bdd1243dSDimitry Andric 255bdd1243dSDimitry Andric Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass); 256bdd1243dSDimitry Andric TII->movImm(MBB, II, DL, ScratchReg, Val, Flag); 257bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(Opc), DestReg) 258bdd1243dSDimitry Andric .addReg(SrcReg, getKillRegState(KillSrcReg)) 259bdd1243dSDimitry Andric .addReg(ScratchReg, RegState::Kill) 260bdd1243dSDimitry Andric .setMIFlag(Flag); 261bdd1243dSDimitry Andric } 262bdd1243dSDimitry Andric 263bdd1243dSDimitry Andric // Split a VSPILLx_Mx pseudo into multiple whole register stores separated by 264bdd1243dSDimitry Andric // LMUL*VLENB bytes. 265bdd1243dSDimitry Andric void RISCVRegisterInfo::lowerVSPILL(MachineBasicBlock::iterator II) const { 266bdd1243dSDimitry Andric DebugLoc DL = II->getDebugLoc(); 267bdd1243dSDimitry Andric MachineBasicBlock &MBB = *II->getParent(); 268bdd1243dSDimitry Andric MachineFunction &MF = *MBB.getParent(); 269bdd1243dSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 270bdd1243dSDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 271bdd1243dSDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 272bdd1243dSDimitry Andric 273bdd1243dSDimitry Andric auto ZvlssegInfo = RISCV::isRVVSpillForZvlsseg(II->getOpcode()); 274bdd1243dSDimitry Andric unsigned NF = ZvlssegInfo->first; 275bdd1243dSDimitry Andric unsigned LMUL = ZvlssegInfo->second; 276bdd1243dSDimitry Andric assert(NF * LMUL <= 8 && "Invalid NF/LMUL combinations."); 277bdd1243dSDimitry Andric unsigned Opcode, SubRegIdx; 278bdd1243dSDimitry Andric switch (LMUL) { 279bdd1243dSDimitry Andric default: 280bdd1243dSDimitry Andric llvm_unreachable("LMUL must be 1, 2, or 4."); 281bdd1243dSDimitry Andric case 1: 282bdd1243dSDimitry Andric Opcode = RISCV::VS1R_V; 283bdd1243dSDimitry Andric SubRegIdx = RISCV::sub_vrm1_0; 284bdd1243dSDimitry Andric break; 285bdd1243dSDimitry Andric case 2: 286bdd1243dSDimitry Andric Opcode = RISCV::VS2R_V; 287bdd1243dSDimitry Andric SubRegIdx = RISCV::sub_vrm2_0; 288bdd1243dSDimitry Andric break; 289bdd1243dSDimitry Andric case 4: 290bdd1243dSDimitry Andric Opcode = RISCV::VS4R_V; 291bdd1243dSDimitry Andric SubRegIdx = RISCV::sub_vrm4_0; 292bdd1243dSDimitry Andric break; 293bdd1243dSDimitry Andric } 294bdd1243dSDimitry Andric static_assert(RISCV::sub_vrm1_7 == RISCV::sub_vrm1_0 + 7, 295bdd1243dSDimitry Andric "Unexpected subreg numbering"); 296bdd1243dSDimitry Andric static_assert(RISCV::sub_vrm2_3 == RISCV::sub_vrm2_0 + 3, 297bdd1243dSDimitry Andric "Unexpected subreg numbering"); 298bdd1243dSDimitry Andric static_assert(RISCV::sub_vrm4_1 == RISCV::sub_vrm4_0 + 1, 299bdd1243dSDimitry Andric "Unexpected subreg numbering"); 300bdd1243dSDimitry Andric 301bdd1243dSDimitry Andric Register VL = MRI.createVirtualRegister(&RISCV::GPRRegClass); 302*5f757f3fSDimitry Andric // Optimize for constant VLEN. 303*5f757f3fSDimitry Andric const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>(); 304*5f757f3fSDimitry Andric if (STI.getRealMinVLen() == STI.getRealMaxVLen()) { 305*5f757f3fSDimitry Andric const int64_t VLENB = STI.getRealMinVLen() / 8; 306*5f757f3fSDimitry Andric int64_t Offset = VLENB * LMUL; 307*5f757f3fSDimitry Andric STI.getInstrInfo()->movImm(MBB, II, DL, VL, Offset); 308*5f757f3fSDimitry Andric } else { 309bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENB), VL); 310bdd1243dSDimitry Andric uint32_t ShiftAmount = Log2_32(LMUL); 311bdd1243dSDimitry Andric if (ShiftAmount != 0) 312bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), VL) 313bdd1243dSDimitry Andric .addReg(VL) 314bdd1243dSDimitry Andric .addImm(ShiftAmount); 315*5f757f3fSDimitry Andric } 316bdd1243dSDimitry Andric 317bdd1243dSDimitry Andric Register SrcReg = II->getOperand(0).getReg(); 318bdd1243dSDimitry Andric Register Base = II->getOperand(1).getReg(); 319bdd1243dSDimitry Andric bool IsBaseKill = II->getOperand(1).isKill(); 320bdd1243dSDimitry Andric Register NewBase = MRI.createVirtualRegister(&RISCV::GPRRegClass); 321bdd1243dSDimitry Andric for (unsigned I = 0; I < NF; ++I) { 322bdd1243dSDimitry Andric // Adding implicit-use of super register to describe we are using part of 323bdd1243dSDimitry Andric // super register, that prevents machine verifier complaining when part of 324bdd1243dSDimitry Andric // subreg is undef, see comment in MachineVerifier::checkLiveness for more 325bdd1243dSDimitry Andric // detail. 326bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(Opcode)) 327bdd1243dSDimitry Andric .addReg(TRI->getSubReg(SrcReg, SubRegIdx + I)) 328bdd1243dSDimitry Andric .addReg(Base, getKillRegState(I == NF - 1)) 329bdd1243dSDimitry Andric .addMemOperand(*(II->memoperands_begin())) 330bdd1243dSDimitry Andric .addReg(SrcReg, RegState::Implicit); 331bdd1243dSDimitry Andric if (I != NF - 1) 332bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(RISCV::ADD), NewBase) 333bdd1243dSDimitry Andric .addReg(Base, getKillRegState(I != 0 || IsBaseKill)) 334bdd1243dSDimitry Andric .addReg(VL, getKillRegState(I == NF - 2)); 335bdd1243dSDimitry Andric Base = NewBase; 336bdd1243dSDimitry Andric } 337bdd1243dSDimitry Andric II->eraseFromParent(); 338bdd1243dSDimitry Andric } 339bdd1243dSDimitry Andric 340bdd1243dSDimitry Andric // Split a VSPILLx_Mx pseudo into multiple whole register loads separated by 341bdd1243dSDimitry Andric // LMUL*VLENB bytes. 342bdd1243dSDimitry Andric void RISCVRegisterInfo::lowerVRELOAD(MachineBasicBlock::iterator II) const { 343bdd1243dSDimitry Andric DebugLoc DL = II->getDebugLoc(); 344bdd1243dSDimitry Andric MachineBasicBlock &MBB = *II->getParent(); 345bdd1243dSDimitry Andric MachineFunction &MF = *MBB.getParent(); 346bdd1243dSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 347bdd1243dSDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 348bdd1243dSDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 349bdd1243dSDimitry Andric 350bdd1243dSDimitry Andric auto ZvlssegInfo = RISCV::isRVVSpillForZvlsseg(II->getOpcode()); 351bdd1243dSDimitry Andric unsigned NF = ZvlssegInfo->first; 352bdd1243dSDimitry Andric unsigned LMUL = ZvlssegInfo->second; 353bdd1243dSDimitry Andric assert(NF * LMUL <= 8 && "Invalid NF/LMUL combinations."); 354bdd1243dSDimitry Andric unsigned Opcode, SubRegIdx; 355bdd1243dSDimitry Andric switch (LMUL) { 356bdd1243dSDimitry Andric default: 357bdd1243dSDimitry Andric llvm_unreachable("LMUL must be 1, 2, or 4."); 358bdd1243dSDimitry Andric case 1: 359bdd1243dSDimitry Andric Opcode = RISCV::VL1RE8_V; 360bdd1243dSDimitry Andric SubRegIdx = RISCV::sub_vrm1_0; 361bdd1243dSDimitry Andric break; 362bdd1243dSDimitry Andric case 2: 363bdd1243dSDimitry Andric Opcode = RISCV::VL2RE8_V; 364bdd1243dSDimitry Andric SubRegIdx = RISCV::sub_vrm2_0; 365bdd1243dSDimitry Andric break; 366bdd1243dSDimitry Andric case 4: 367bdd1243dSDimitry Andric Opcode = RISCV::VL4RE8_V; 368bdd1243dSDimitry Andric SubRegIdx = RISCV::sub_vrm4_0; 369bdd1243dSDimitry Andric break; 370bdd1243dSDimitry Andric } 371bdd1243dSDimitry Andric static_assert(RISCV::sub_vrm1_7 == RISCV::sub_vrm1_0 + 7, 372bdd1243dSDimitry Andric "Unexpected subreg numbering"); 373bdd1243dSDimitry Andric static_assert(RISCV::sub_vrm2_3 == RISCV::sub_vrm2_0 + 3, 374bdd1243dSDimitry Andric "Unexpected subreg numbering"); 375bdd1243dSDimitry Andric static_assert(RISCV::sub_vrm4_1 == RISCV::sub_vrm4_0 + 1, 376bdd1243dSDimitry Andric "Unexpected subreg numbering"); 377bdd1243dSDimitry Andric 378bdd1243dSDimitry Andric Register VL = MRI.createVirtualRegister(&RISCV::GPRRegClass); 379*5f757f3fSDimitry Andric // Optimize for constant VLEN. 380*5f757f3fSDimitry Andric const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>(); 381*5f757f3fSDimitry Andric if (STI.getRealMinVLen() == STI.getRealMaxVLen()) { 382*5f757f3fSDimitry Andric const int64_t VLENB = STI.getRealMinVLen() / 8; 383*5f757f3fSDimitry Andric int64_t Offset = VLENB * LMUL; 384*5f757f3fSDimitry Andric STI.getInstrInfo()->movImm(MBB, II, DL, VL, Offset); 385*5f757f3fSDimitry Andric } else { 386bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENB), VL); 387bdd1243dSDimitry Andric uint32_t ShiftAmount = Log2_32(LMUL); 388bdd1243dSDimitry Andric if (ShiftAmount != 0) 389bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), VL) 390bdd1243dSDimitry Andric .addReg(VL) 391bdd1243dSDimitry Andric .addImm(ShiftAmount); 392*5f757f3fSDimitry Andric } 393bdd1243dSDimitry Andric 394bdd1243dSDimitry Andric Register DestReg = II->getOperand(0).getReg(); 395bdd1243dSDimitry Andric Register Base = II->getOperand(1).getReg(); 396bdd1243dSDimitry Andric bool IsBaseKill = II->getOperand(1).isKill(); 397bdd1243dSDimitry Andric Register NewBase = MRI.createVirtualRegister(&RISCV::GPRRegClass); 398bdd1243dSDimitry Andric for (unsigned I = 0; I < NF; ++I) { 399bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(Opcode), 400bdd1243dSDimitry Andric TRI->getSubReg(DestReg, SubRegIdx + I)) 401bdd1243dSDimitry Andric .addReg(Base, getKillRegState(I == NF - 1)) 402bdd1243dSDimitry Andric .addMemOperand(*(II->memoperands_begin())); 403bdd1243dSDimitry Andric if (I != NF - 1) 404bdd1243dSDimitry Andric BuildMI(MBB, II, DL, TII->get(RISCV::ADD), NewBase) 405bdd1243dSDimitry Andric .addReg(Base, getKillRegState(I != 0 || IsBaseKill)) 406bdd1243dSDimitry Andric .addReg(VL, getKillRegState(I == NF - 2)); 407bdd1243dSDimitry Andric Base = NewBase; 408bdd1243dSDimitry Andric } 409bdd1243dSDimitry Andric II->eraseFromParent(); 410bdd1243dSDimitry Andric } 411bdd1243dSDimitry Andric 412bdd1243dSDimitry Andric bool RISCVRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 4130b57cec5SDimitry Andric int SPAdj, unsigned FIOperandNum, 4140b57cec5SDimitry Andric RegScavenger *RS) const { 4150b57cec5SDimitry Andric assert(SPAdj == 0 && "Unexpected non-zero SPAdj value"); 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric MachineInstr &MI = *II; 4180b57cec5SDimitry Andric MachineFunction &MF = *MI.getParent()->getParent(); 4190b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 420bdd1243dSDimitry Andric const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>(); 4210b57cec5SDimitry Andric DebugLoc DL = MI.getDebugLoc(); 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 4245ffd83dbSDimitry Andric Register FrameReg; 425fe6060f1SDimitry Andric StackOffset Offset = 426fe6060f1SDimitry Andric getFrameLowering(MF)->getFrameIndexReference(MF, FrameIndex, FrameReg); 42781ad6265SDimitry Andric bool IsRVVSpill = RISCV::isRVVSpill(MI); 428fe6060f1SDimitry Andric if (!IsRVVSpill) 429fe6060f1SDimitry Andric Offset += StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm()); 4300b57cec5SDimitry Andric 431bdd1243dSDimitry Andric if (Offset.getScalable() && 432bdd1243dSDimitry Andric ST.getRealMinVLen() == ST.getRealMaxVLen()) { 433bdd1243dSDimitry Andric // For an exact VLEN value, scalable offsets become constant and thus 434bdd1243dSDimitry Andric // can be converted entirely into fixed offsets. 435bdd1243dSDimitry Andric int64_t FixedValue = Offset.getFixed(); 436bdd1243dSDimitry Andric int64_t ScalableValue = Offset.getScalable(); 437bdd1243dSDimitry Andric assert(ScalableValue % 8 == 0 && 438bdd1243dSDimitry Andric "Scalable offset is not a multiple of a single vector size."); 439bdd1243dSDimitry Andric int64_t NumOfVReg = ScalableValue / 8; 440bdd1243dSDimitry Andric int64_t VLENB = ST.getRealMinVLen() / 8; 441bdd1243dSDimitry Andric Offset = StackOffset::getFixed(FixedValue + NumOfVReg * VLENB); 442bdd1243dSDimitry Andric } 443bdd1243dSDimitry Andric 444fe6060f1SDimitry Andric if (!isInt<32>(Offset.getFixed())) { 4450b57cec5SDimitry Andric report_fatal_error( 4460b57cec5SDimitry Andric "Frame offsets outside of the signed 32-bit range not supported"); 4470b57cec5SDimitry Andric } 4480b57cec5SDimitry Andric 449bdd1243dSDimitry Andric if (!IsRVVSpill) { 450bdd1243dSDimitry Andric if (MI.getOpcode() == RISCV::ADDI && !isInt<12>(Offset.getFixed())) { 451bdd1243dSDimitry Andric // We chose to emit the canonical immediate sequence rather than folding 452bdd1243dSDimitry Andric // the offset into the using add under the theory that doing so doesn't 453bdd1243dSDimitry Andric // save dynamic instruction count and some target may fuse the canonical 454bdd1243dSDimitry Andric // 32 bit immediate sequence. We still need to clear the portion of the 455bdd1243dSDimitry Andric // offset encoded in the immediate. 456bdd1243dSDimitry Andric MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0); 457fe6060f1SDimitry Andric } else { 458bdd1243dSDimitry Andric // We can encode an add with 12 bit signed immediate in the immediate 459bdd1243dSDimitry Andric // operand of our user instruction. As a result, the remaining 460bdd1243dSDimitry Andric // offset can by construction, at worst, a LUI and a ADD. 461bdd1243dSDimitry Andric int64_t Val = Offset.getFixed(); 462bdd1243dSDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 463*5f757f3fSDimitry Andric if ((MI.getOpcode() == RISCV::PREFETCH_I || 464*5f757f3fSDimitry Andric MI.getOpcode() == RISCV::PREFETCH_R || 465*5f757f3fSDimitry Andric MI.getOpcode() == RISCV::PREFETCH_W) && 466*5f757f3fSDimitry Andric (Lo12 & 0b11111) != 0) 467*5f757f3fSDimitry Andric MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0); 468*5f757f3fSDimitry Andric else { 469bdd1243dSDimitry Andric MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Lo12); 470bdd1243dSDimitry Andric Offset = StackOffset::get((uint64_t)Val - (uint64_t)Lo12, 471bdd1243dSDimitry Andric Offset.getScalable()); 472bdd1243dSDimitry Andric } 473bdd1243dSDimitry Andric } 474*5f757f3fSDimitry Andric } 475fe6060f1SDimitry Andric 476bdd1243dSDimitry Andric if (Offset.getScalable() || Offset.getFixed()) { 477bdd1243dSDimitry Andric Register DestReg; 478bdd1243dSDimitry Andric if (MI.getOpcode() == RISCV::ADDI) 479bdd1243dSDimitry Andric DestReg = MI.getOperand(0).getReg(); 480bdd1243dSDimitry Andric else 481bdd1243dSDimitry Andric DestReg = MRI.createVirtualRegister(&RISCV::GPRRegClass); 482bdd1243dSDimitry Andric adjustReg(*II->getParent(), II, DL, DestReg, FrameReg, Offset, 483bdd1243dSDimitry Andric MachineInstr::NoFlags, std::nullopt); 484bdd1243dSDimitry Andric MI.getOperand(FIOperandNum).ChangeToRegister(DestReg, /*IsDef*/false, 485bdd1243dSDimitry Andric /*IsImp*/false, 486bdd1243dSDimitry Andric /*IsKill*/true); 487bdd1243dSDimitry Andric } else { 488bdd1243dSDimitry Andric MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, /*IsDef*/false, 489bdd1243dSDimitry Andric /*IsImp*/false, 490bdd1243dSDimitry Andric /*IsKill*/false); 491bdd1243dSDimitry Andric } 492bdd1243dSDimitry Andric 493bdd1243dSDimitry Andric // If after materializing the adjustment, we have a pointless ADDI, remove it 494bdd1243dSDimitry Andric if (MI.getOpcode() == RISCV::ADDI && 495bdd1243dSDimitry Andric MI.getOperand(0).getReg() == MI.getOperand(1).getReg() && 496bdd1243dSDimitry Andric MI.getOperand(2).getImm() == 0) { 497fe6060f1SDimitry Andric MI.eraseFromParent(); 498bdd1243dSDimitry Andric return true; 499fe6060f1SDimitry Andric } 500fe6060f1SDimitry Andric 501bdd1243dSDimitry Andric // Handle spill/fill of synthetic register classes for segment operations to 502bdd1243dSDimitry Andric // ensure correctness in the edge case one gets spilled. There are many 503bdd1243dSDimitry Andric // possible optimizations here, but given the extreme rarity of such spills, 504bdd1243dSDimitry Andric // we prefer simplicity of implementation for now. 505bdd1243dSDimitry Andric switch (MI.getOpcode()) { 506bdd1243dSDimitry Andric case RISCV::PseudoVSPILL2_M1: 507bdd1243dSDimitry Andric case RISCV::PseudoVSPILL2_M2: 508bdd1243dSDimitry Andric case RISCV::PseudoVSPILL2_M4: 509bdd1243dSDimitry Andric case RISCV::PseudoVSPILL3_M1: 510bdd1243dSDimitry Andric case RISCV::PseudoVSPILL3_M2: 511bdd1243dSDimitry Andric case RISCV::PseudoVSPILL4_M1: 512bdd1243dSDimitry Andric case RISCV::PseudoVSPILL4_M2: 513bdd1243dSDimitry Andric case RISCV::PseudoVSPILL5_M1: 514bdd1243dSDimitry Andric case RISCV::PseudoVSPILL6_M1: 515bdd1243dSDimitry Andric case RISCV::PseudoVSPILL7_M1: 516bdd1243dSDimitry Andric case RISCV::PseudoVSPILL8_M1: 517bdd1243dSDimitry Andric lowerVSPILL(II); 518bdd1243dSDimitry Andric return true; 519bdd1243dSDimitry Andric case RISCV::PseudoVRELOAD2_M1: 520bdd1243dSDimitry Andric case RISCV::PseudoVRELOAD2_M2: 521bdd1243dSDimitry Andric case RISCV::PseudoVRELOAD2_M4: 522bdd1243dSDimitry Andric case RISCV::PseudoVRELOAD3_M1: 523bdd1243dSDimitry Andric case RISCV::PseudoVRELOAD3_M2: 524bdd1243dSDimitry Andric case RISCV::PseudoVRELOAD4_M1: 525bdd1243dSDimitry Andric case RISCV::PseudoVRELOAD4_M2: 526bdd1243dSDimitry Andric case RISCV::PseudoVRELOAD5_M1: 527bdd1243dSDimitry Andric case RISCV::PseudoVRELOAD6_M1: 528bdd1243dSDimitry Andric case RISCV::PseudoVRELOAD7_M1: 529bdd1243dSDimitry Andric case RISCV::PseudoVRELOAD8_M1: 530bdd1243dSDimitry Andric lowerVRELOAD(II); 531bdd1243dSDimitry Andric return true; 532fe6060f1SDimitry Andric } 533fe6060f1SDimitry Andric 534bdd1243dSDimitry Andric return false; 535fe6060f1SDimitry Andric } 536bdd1243dSDimitry Andric 537bdd1243dSDimitry Andric bool RISCVRegisterInfo::requiresVirtualBaseRegisters( 538bdd1243dSDimitry Andric const MachineFunction &MF) const { 539bdd1243dSDimitry Andric return true; 540bdd1243dSDimitry Andric } 541bdd1243dSDimitry Andric 542bdd1243dSDimitry Andric // Returns true if the instruction's frame index reference would be better 543bdd1243dSDimitry Andric // served by a base register other than FP or SP. 544bdd1243dSDimitry Andric // Used by LocalStackSlotAllocation pass to determine which frame index 545bdd1243dSDimitry Andric // references it should create new base registers for. 546bdd1243dSDimitry Andric bool RISCVRegisterInfo::needsFrameBaseReg(MachineInstr *MI, 547bdd1243dSDimitry Andric int64_t Offset) const { 548bdd1243dSDimitry Andric unsigned FIOperandNum = 0; 549bdd1243dSDimitry Andric for (; !MI->getOperand(FIOperandNum).isFI(); FIOperandNum++) 550bdd1243dSDimitry Andric assert(FIOperandNum < MI->getNumOperands() && 551bdd1243dSDimitry Andric "Instr doesn't have FrameIndex operand"); 552bdd1243dSDimitry Andric 553bdd1243dSDimitry Andric // For RISC-V, The machine instructions that include a FrameIndex operand 554bdd1243dSDimitry Andric // are load/store, ADDI instructions. 555bdd1243dSDimitry Andric unsigned MIFrm = RISCVII::getFormat(MI->getDesc().TSFlags); 556bdd1243dSDimitry Andric if (MIFrm != RISCVII::InstFormatI && MIFrm != RISCVII::InstFormatS) 557bdd1243dSDimitry Andric return false; 558bdd1243dSDimitry Andric // We only generate virtual base registers for loads and stores, so 559bdd1243dSDimitry Andric // return false for everything else. 560bdd1243dSDimitry Andric if (!MI->mayLoad() && !MI->mayStore()) 561bdd1243dSDimitry Andric return false; 562bdd1243dSDimitry Andric 563bdd1243dSDimitry Andric const MachineFunction &MF = *MI->getMF(); 564bdd1243dSDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 565bdd1243dSDimitry Andric const RISCVFrameLowering *TFI = getFrameLowering(MF); 566bdd1243dSDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 567bdd1243dSDimitry Andric unsigned CalleeSavedSize = 0; 568bdd1243dSDimitry Andric Offset += getFrameIndexInstrOffset(MI, FIOperandNum); 569bdd1243dSDimitry Andric 570bdd1243dSDimitry Andric // Estimate the stack size used to store callee saved registers( 571bdd1243dSDimitry Andric // excludes reserved registers). 572bdd1243dSDimitry Andric BitVector ReservedRegs = getReservedRegs(MF); 573bdd1243dSDimitry Andric for (const MCPhysReg *R = MRI.getCalleeSavedRegs(); MCPhysReg Reg = *R; ++R) { 574bdd1243dSDimitry Andric if (!ReservedRegs.test(Reg)) 575bdd1243dSDimitry Andric CalleeSavedSize += getSpillSize(*getMinimalPhysRegClass(Reg)); 576bdd1243dSDimitry Andric } 577bdd1243dSDimitry Andric 578bdd1243dSDimitry Andric int64_t MaxFPOffset = Offset - CalleeSavedSize; 579bdd1243dSDimitry Andric if (TFI->hasFP(MF) && !shouldRealignStack(MF)) 580bdd1243dSDimitry Andric return !isFrameOffsetLegal(MI, RISCV::X8, MaxFPOffset); 581bdd1243dSDimitry Andric 582bdd1243dSDimitry Andric // Assume 128 bytes spill slots size to estimate the maximum possible 583bdd1243dSDimitry Andric // offset relative to the stack pointer. 584bdd1243dSDimitry Andric // FIXME: The 128 is copied from ARM. We should run some statistics and pick a 585bdd1243dSDimitry Andric // real one for RISC-V. 586bdd1243dSDimitry Andric int64_t MaxSPOffset = Offset + 128; 587bdd1243dSDimitry Andric MaxSPOffset += MFI.getLocalFrameSize(); 588bdd1243dSDimitry Andric return !isFrameOffsetLegal(MI, RISCV::X2, MaxSPOffset); 589bdd1243dSDimitry Andric } 590bdd1243dSDimitry Andric 591bdd1243dSDimitry Andric // Determine whether a given base register plus offset immediate is 592bdd1243dSDimitry Andric // encodable to resolve a frame index. 593bdd1243dSDimitry Andric bool RISCVRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 594bdd1243dSDimitry Andric Register BaseReg, 595bdd1243dSDimitry Andric int64_t Offset) const { 596bdd1243dSDimitry Andric unsigned FIOperandNum = 0; 597bdd1243dSDimitry Andric while (!MI->getOperand(FIOperandNum).isFI()) { 598bdd1243dSDimitry Andric FIOperandNum++; 599bdd1243dSDimitry Andric assert(FIOperandNum < MI->getNumOperands() && 600bdd1243dSDimitry Andric "Instr does not have a FrameIndex operand!"); 601bdd1243dSDimitry Andric } 602bdd1243dSDimitry Andric 603bdd1243dSDimitry Andric Offset += getFrameIndexInstrOffset(MI, FIOperandNum); 604bdd1243dSDimitry Andric return isInt<12>(Offset); 605bdd1243dSDimitry Andric } 606bdd1243dSDimitry Andric 607bdd1243dSDimitry Andric // Insert defining instruction(s) for a pointer to FrameIdx before 608bdd1243dSDimitry Andric // insertion point I. 609bdd1243dSDimitry Andric // Return materialized frame pointer. 610bdd1243dSDimitry Andric Register RISCVRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB, 611bdd1243dSDimitry Andric int FrameIdx, 612bdd1243dSDimitry Andric int64_t Offset) const { 613bdd1243dSDimitry Andric MachineBasicBlock::iterator MBBI = MBB->begin(); 614bdd1243dSDimitry Andric DebugLoc DL; 615bdd1243dSDimitry Andric if (MBBI != MBB->end()) 616bdd1243dSDimitry Andric DL = MBBI->getDebugLoc(); 617bdd1243dSDimitry Andric MachineFunction *MF = MBB->getParent(); 618bdd1243dSDimitry Andric MachineRegisterInfo &MFI = MF->getRegInfo(); 619bdd1243dSDimitry Andric const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 620bdd1243dSDimitry Andric 621bdd1243dSDimitry Andric Register BaseReg = MFI.createVirtualRegister(&RISCV::GPRRegClass); 622bdd1243dSDimitry Andric BuildMI(*MBB, MBBI, DL, TII->get(RISCV::ADDI), BaseReg) 623bdd1243dSDimitry Andric .addFrameIndex(FrameIdx) 624bdd1243dSDimitry Andric .addImm(Offset); 625bdd1243dSDimitry Andric return BaseReg; 626bdd1243dSDimitry Andric } 627bdd1243dSDimitry Andric 628bdd1243dSDimitry Andric // Resolve a frame index operand of an instruction to reference the 629bdd1243dSDimitry Andric // indicated base register plus offset instead. 630bdd1243dSDimitry Andric void RISCVRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg, 631bdd1243dSDimitry Andric int64_t Offset) const { 632bdd1243dSDimitry Andric unsigned FIOperandNum = 0; 633bdd1243dSDimitry Andric while (!MI.getOperand(FIOperandNum).isFI()) { 634bdd1243dSDimitry Andric FIOperandNum++; 635bdd1243dSDimitry Andric assert(FIOperandNum < MI.getNumOperands() && 636bdd1243dSDimitry Andric "Instr does not have a FrameIndex operand!"); 637bdd1243dSDimitry Andric } 638bdd1243dSDimitry Andric 639bdd1243dSDimitry Andric Offset += getFrameIndexInstrOffset(&MI, FIOperandNum); 640bdd1243dSDimitry Andric // FrameIndex Operands are always represented as a 641bdd1243dSDimitry Andric // register followed by an immediate. 642bdd1243dSDimitry Andric MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false); 643bdd1243dSDimitry Andric MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset); 644bdd1243dSDimitry Andric } 645bdd1243dSDimitry Andric 646bdd1243dSDimitry Andric // Get the offset from the referenced frame index in the instruction, 647bdd1243dSDimitry Andric // if there is one. 648bdd1243dSDimitry Andric int64_t RISCVRegisterInfo::getFrameIndexInstrOffset(const MachineInstr *MI, 649bdd1243dSDimitry Andric int Idx) const { 650bdd1243dSDimitry Andric assert((RISCVII::getFormat(MI->getDesc().TSFlags) == RISCVII::InstFormatI || 651bdd1243dSDimitry Andric RISCVII::getFormat(MI->getDesc().TSFlags) == RISCVII::InstFormatS) && 652bdd1243dSDimitry Andric "The MI must be I or S format."); 653bdd1243dSDimitry Andric assert(MI->getOperand(Idx).isFI() && "The Idx'th operand of MI is not a " 654bdd1243dSDimitry Andric "FrameIndex operand"); 655bdd1243dSDimitry Andric return MI->getOperand(Idx + 1).getImm(); 6560b57cec5SDimitry Andric } 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric Register RISCVRegisterInfo::getFrameRegister(const MachineFunction &MF) const { 6590b57cec5SDimitry Andric const TargetFrameLowering *TFI = getFrameLowering(MF); 6600b57cec5SDimitry Andric return TFI->hasFP(MF) ? RISCV::X8 : RISCV::X2; 6610b57cec5SDimitry Andric } 6620b57cec5SDimitry Andric 6630b57cec5SDimitry Andric const uint32_t * 6640b57cec5SDimitry Andric RISCVRegisterInfo::getCallPreservedMask(const MachineFunction & MF, 665e8d8bef9SDimitry Andric CallingConv::ID CC) const { 6660b57cec5SDimitry Andric auto &Subtarget = MF.getSubtarget<RISCVSubtarget>(); 6670b57cec5SDimitry Andric 668e8d8bef9SDimitry Andric if (CC == CallingConv::GHC) 669e8d8bef9SDimitry Andric return CSR_NoRegs_RegMask; 6700b57cec5SDimitry Andric switch (Subtarget.getTargetABI()) { 6710b57cec5SDimitry Andric default: 6720b57cec5SDimitry Andric llvm_unreachable("Unrecognized ABI"); 6730b57cec5SDimitry Andric case RISCVABI::ABI_ILP32: 6740b57cec5SDimitry Andric case RISCVABI::ABI_LP64: 6750b57cec5SDimitry Andric return CSR_ILP32_LP64_RegMask; 6760b57cec5SDimitry Andric case RISCVABI::ABI_ILP32F: 6770b57cec5SDimitry Andric case RISCVABI::ABI_LP64F: 6780b57cec5SDimitry Andric return CSR_ILP32F_LP64F_RegMask; 6790b57cec5SDimitry Andric case RISCVABI::ABI_ILP32D: 6800b57cec5SDimitry Andric case RISCVABI::ABI_LP64D: 6810b57cec5SDimitry Andric return CSR_ILP32D_LP64D_RegMask; 6820b57cec5SDimitry Andric } 6830b57cec5SDimitry Andric } 684fe6060f1SDimitry Andric 685fe6060f1SDimitry Andric const TargetRegisterClass * 686fe6060f1SDimitry Andric RISCVRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC, 687fe6060f1SDimitry Andric const MachineFunction &) const { 688fe6060f1SDimitry Andric if (RC == &RISCV::VMV0RegClass) 689fe6060f1SDimitry Andric return &RISCV::VRRegClass; 690*5f757f3fSDimitry Andric if (RC == &RISCV::VRNoV0RegClass) 691*5f757f3fSDimitry Andric return &RISCV::VRRegClass; 692*5f757f3fSDimitry Andric if (RC == &RISCV::VRM2NoV0RegClass) 693*5f757f3fSDimitry Andric return &RISCV::VRM2RegClass; 694*5f757f3fSDimitry Andric if (RC == &RISCV::VRM4NoV0RegClass) 695*5f757f3fSDimitry Andric return &RISCV::VRM4RegClass; 696*5f757f3fSDimitry Andric if (RC == &RISCV::VRM8NoV0RegClass) 697*5f757f3fSDimitry Andric return &RISCV::VRM8RegClass; 698fe6060f1SDimitry Andric return RC; 699fe6060f1SDimitry Andric } 7004824e7fdSDimitry Andric 7014824e7fdSDimitry Andric void RISCVRegisterInfo::getOffsetOpcodes(const StackOffset &Offset, 7024824e7fdSDimitry Andric SmallVectorImpl<uint64_t> &Ops) const { 7034824e7fdSDimitry Andric // VLENB is the length of a vector register in bytes. We use <vscale x 8 x i8> 7044824e7fdSDimitry Andric // to represent one vector register. The dwarf offset is 7054824e7fdSDimitry Andric // VLENB * scalable_offset / 8. 7064824e7fdSDimitry Andric assert(Offset.getScalable() % 8 == 0 && "Invalid frame offset"); 7074824e7fdSDimitry Andric 7084824e7fdSDimitry Andric // Add fixed-sized offset using existing DIExpression interface. 7094824e7fdSDimitry Andric DIExpression::appendOffset(Ops, Offset.getFixed()); 7104824e7fdSDimitry Andric 7114824e7fdSDimitry Andric unsigned VLENB = getDwarfRegNum(RISCV::VLENB, true); 7124824e7fdSDimitry Andric int64_t VLENBSized = Offset.getScalable() / 8; 7134824e7fdSDimitry Andric if (VLENBSized > 0) { 7144824e7fdSDimitry Andric Ops.push_back(dwarf::DW_OP_constu); 7154824e7fdSDimitry Andric Ops.push_back(VLENBSized); 7164824e7fdSDimitry Andric Ops.append({dwarf::DW_OP_bregx, VLENB, 0ULL}); 7174824e7fdSDimitry Andric Ops.push_back(dwarf::DW_OP_mul); 7184824e7fdSDimitry Andric Ops.push_back(dwarf::DW_OP_plus); 7194824e7fdSDimitry Andric } else if (VLENBSized < 0) { 7204824e7fdSDimitry Andric Ops.push_back(dwarf::DW_OP_constu); 7214824e7fdSDimitry Andric Ops.push_back(-VLENBSized); 7224824e7fdSDimitry Andric Ops.append({dwarf::DW_OP_bregx, VLENB, 0ULL}); 7234824e7fdSDimitry Andric Ops.push_back(dwarf::DW_OP_mul); 7244824e7fdSDimitry Andric Ops.push_back(dwarf::DW_OP_minus); 7254824e7fdSDimitry Andric } 7264824e7fdSDimitry Andric } 72704eeddc0SDimitry Andric 72804eeddc0SDimitry Andric unsigned 72904eeddc0SDimitry Andric RISCVRegisterInfo::getRegisterCostTableIndex(const MachineFunction &MF) const { 730bdd1243dSDimitry Andric return MF.getSubtarget<RISCVSubtarget>().hasStdExtCOrZca() ? 1 : 0; 731bdd1243dSDimitry Andric } 732bdd1243dSDimitry Andric 733bdd1243dSDimitry Andric // Add two address hints to improve chances of being able to use a compressed 734bdd1243dSDimitry Andric // instruction. 735bdd1243dSDimitry Andric bool RISCVRegisterInfo::getRegAllocationHints( 736bdd1243dSDimitry Andric Register VirtReg, ArrayRef<MCPhysReg> Order, 737bdd1243dSDimitry Andric SmallVectorImpl<MCPhysReg> &Hints, const MachineFunction &MF, 738bdd1243dSDimitry Andric const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const { 739bdd1243dSDimitry Andric const MachineRegisterInfo *MRI = &MF.getRegInfo(); 740bdd1243dSDimitry Andric 741bdd1243dSDimitry Andric bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints( 742bdd1243dSDimitry Andric VirtReg, Order, Hints, MF, VRM, Matrix); 743bdd1243dSDimitry Andric 744bdd1243dSDimitry Andric if (!VRM || DisableRegAllocHints) 745bdd1243dSDimitry Andric return BaseImplRetVal; 746bdd1243dSDimitry Andric 747bdd1243dSDimitry Andric // Add any two address hints after any copy hints. 748bdd1243dSDimitry Andric SmallSet<Register, 4> TwoAddrHints; 749bdd1243dSDimitry Andric 750bdd1243dSDimitry Andric auto tryAddHint = [&](const MachineOperand &VRRegMO, const MachineOperand &MO, 751bdd1243dSDimitry Andric bool NeedGPRC) -> void { 752bdd1243dSDimitry Andric Register Reg = MO.getReg(); 753bdd1243dSDimitry Andric Register PhysReg = Reg.isPhysical() ? Reg : Register(VRM->getPhys(Reg)); 754bdd1243dSDimitry Andric if (PhysReg && (!NeedGPRC || RISCV::GPRCRegClass.contains(PhysReg))) { 755bdd1243dSDimitry Andric assert(!MO.getSubReg() && !VRRegMO.getSubReg() && "Unexpected subreg!"); 756bdd1243dSDimitry Andric if (!MRI->isReserved(PhysReg) && !is_contained(Hints, PhysReg)) 757bdd1243dSDimitry Andric TwoAddrHints.insert(PhysReg); 758bdd1243dSDimitry Andric } 759bdd1243dSDimitry Andric }; 760bdd1243dSDimitry Andric 761bdd1243dSDimitry Andric // This is all of the compressible binary instructions. If an instruction 762bdd1243dSDimitry Andric // needs GPRC register class operands \p NeedGPRC will be set to true. 763bdd1243dSDimitry Andric auto isCompressible = [](const MachineInstr &MI, bool &NeedGPRC) { 764bdd1243dSDimitry Andric NeedGPRC = false; 765bdd1243dSDimitry Andric switch (MI.getOpcode()) { 766bdd1243dSDimitry Andric default: 767bdd1243dSDimitry Andric return false; 768bdd1243dSDimitry Andric case RISCV::AND: 769bdd1243dSDimitry Andric case RISCV::OR: 770bdd1243dSDimitry Andric case RISCV::XOR: 771bdd1243dSDimitry Andric case RISCV::SUB: 772bdd1243dSDimitry Andric case RISCV::ADDW: 773bdd1243dSDimitry Andric case RISCV::SUBW: 774bdd1243dSDimitry Andric NeedGPRC = true; 775bdd1243dSDimitry Andric return true; 776bdd1243dSDimitry Andric case RISCV::ANDI: 777bdd1243dSDimitry Andric NeedGPRC = true; 778bdd1243dSDimitry Andric return MI.getOperand(2).isImm() && isInt<6>(MI.getOperand(2).getImm()); 779bdd1243dSDimitry Andric case RISCV::SRAI: 780bdd1243dSDimitry Andric case RISCV::SRLI: 781bdd1243dSDimitry Andric NeedGPRC = true; 782bdd1243dSDimitry Andric return true; 783bdd1243dSDimitry Andric case RISCV::ADD: 784bdd1243dSDimitry Andric case RISCV::SLLI: 785bdd1243dSDimitry Andric return true; 786bdd1243dSDimitry Andric case RISCV::ADDI: 787bdd1243dSDimitry Andric case RISCV::ADDIW: 788bdd1243dSDimitry Andric return MI.getOperand(2).isImm() && isInt<6>(MI.getOperand(2).getImm()); 789bdd1243dSDimitry Andric } 790bdd1243dSDimitry Andric }; 791bdd1243dSDimitry Andric 792bdd1243dSDimitry Andric // Returns true if this operand is compressible. For non-registers it always 793bdd1243dSDimitry Andric // returns true. Immediate range was already checked in isCompressible. 794bdd1243dSDimitry Andric // For registers, it checks if the register is a GPRC register. reg-reg 795bdd1243dSDimitry Andric // instructions that require GPRC need all register operands to be GPRC. 796bdd1243dSDimitry Andric auto isCompressibleOpnd = [&](const MachineOperand &MO) { 797bdd1243dSDimitry Andric if (!MO.isReg()) 798bdd1243dSDimitry Andric return true; 799bdd1243dSDimitry Andric Register Reg = MO.getReg(); 800bdd1243dSDimitry Andric Register PhysReg = Reg.isPhysical() ? Reg : Register(VRM->getPhys(Reg)); 801bdd1243dSDimitry Andric return PhysReg && RISCV::GPRCRegClass.contains(PhysReg); 802bdd1243dSDimitry Andric }; 803bdd1243dSDimitry Andric 804bdd1243dSDimitry Andric for (auto &MO : MRI->reg_nodbg_operands(VirtReg)) { 805bdd1243dSDimitry Andric const MachineInstr &MI = *MO.getParent(); 80606c3fb27SDimitry Andric unsigned OpIdx = MO.getOperandNo(); 807bdd1243dSDimitry Andric bool NeedGPRC; 808bdd1243dSDimitry Andric if (isCompressible(MI, NeedGPRC)) { 809bdd1243dSDimitry Andric if (OpIdx == 0 && MI.getOperand(1).isReg()) { 810bdd1243dSDimitry Andric if (!NeedGPRC || isCompressibleOpnd(MI.getOperand(2))) 811bdd1243dSDimitry Andric tryAddHint(MO, MI.getOperand(1), NeedGPRC); 812bdd1243dSDimitry Andric if (MI.isCommutable() && MI.getOperand(2).isReg() && 813bdd1243dSDimitry Andric (!NeedGPRC || isCompressibleOpnd(MI.getOperand(1)))) 814bdd1243dSDimitry Andric tryAddHint(MO, MI.getOperand(2), NeedGPRC); 815bdd1243dSDimitry Andric } else if (OpIdx == 1 && 816bdd1243dSDimitry Andric (!NeedGPRC || isCompressibleOpnd(MI.getOperand(2)))) { 817bdd1243dSDimitry Andric tryAddHint(MO, MI.getOperand(0), NeedGPRC); 818bdd1243dSDimitry Andric } else if (MI.isCommutable() && OpIdx == 2 && 819bdd1243dSDimitry Andric (!NeedGPRC || isCompressibleOpnd(MI.getOperand(1)))) { 820bdd1243dSDimitry Andric tryAddHint(MO, MI.getOperand(0), NeedGPRC); 821bdd1243dSDimitry Andric } 822bdd1243dSDimitry Andric } 823bdd1243dSDimitry Andric } 824bdd1243dSDimitry Andric 825bdd1243dSDimitry Andric for (MCPhysReg OrderReg : Order) 826bdd1243dSDimitry Andric if (TwoAddrHints.count(OrderReg)) 827bdd1243dSDimitry Andric Hints.push_back(OrderReg); 828bdd1243dSDimitry Andric 829bdd1243dSDimitry Andric return BaseImplRetVal; 83004eeddc0SDimitry Andric } 831