13dac3a9bSDimitry Andric //===-- WebAssemblyRegisterInfo.cpp - WebAssembly Register Information ----===//
23dac3a9bSDimitry Andric //
33dac3a9bSDimitry Andric //                     The LLVM Compiler Infrastructure
43dac3a9bSDimitry Andric //
53dac3a9bSDimitry Andric // This file is distributed under the University of Illinois Open Source
63dac3a9bSDimitry Andric // License. See LICENSE.TXT for details.
73dac3a9bSDimitry Andric //
83dac3a9bSDimitry Andric //===----------------------------------------------------------------------===//
93dac3a9bSDimitry Andric ///
103dac3a9bSDimitry Andric /// \file
114ba319b5SDimitry Andric /// This file contains the WebAssembly implementation of the
123dac3a9bSDimitry Andric /// TargetRegisterInfo class.
133dac3a9bSDimitry Andric ///
143dac3a9bSDimitry Andric //===----------------------------------------------------------------------===//
153dac3a9bSDimitry Andric 
163dac3a9bSDimitry Andric #include "WebAssemblyRegisterInfo.h"
173dac3a9bSDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
183dac3a9bSDimitry Andric #include "WebAssemblyFrameLowering.h"
193dac3a9bSDimitry Andric #include "WebAssemblyInstrInfo.h"
203dac3a9bSDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
213dac3a9bSDimitry Andric #include "WebAssemblySubtarget.h"
223dac3a9bSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
233dac3a9bSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
243dac3a9bSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
25*b5893f02SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
263dac3a9bSDimitry Andric #include "llvm/IR/Function.h"
273dac3a9bSDimitry Andric #include "llvm/Support/raw_ostream.h"
283dac3a9bSDimitry Andric #include "llvm/Target/TargetOptions.h"
293dac3a9bSDimitry Andric using namespace llvm;
303dac3a9bSDimitry Andric 
313dac3a9bSDimitry Andric #define DEBUG_TYPE "wasm-reg-info"
323dac3a9bSDimitry Andric 
33875ed548SDimitry Andric #define GET_REGINFO_TARGET_DESC
34875ed548SDimitry Andric #include "WebAssemblyGenRegisterInfo.inc"
35875ed548SDimitry Andric 
WebAssemblyRegisterInfo(const Triple & TT)36875ed548SDimitry Andric WebAssemblyRegisterInfo::WebAssemblyRegisterInfo(const Triple &TT)
37875ed548SDimitry Andric     : WebAssemblyGenRegisterInfo(0), TT(TT) {}
38875ed548SDimitry Andric 
39875ed548SDimitry Andric const MCPhysReg *
getCalleeSavedRegs(const MachineFunction *) const40875ed548SDimitry Andric WebAssemblyRegisterInfo::getCalleeSavedRegs(const MachineFunction *) const {
41875ed548SDimitry Andric   static const MCPhysReg CalleeSavedRegs[] = {0};
42875ed548SDimitry Andric   return CalleeSavedRegs;
43875ed548SDimitry Andric }
44875ed548SDimitry Andric 
45875ed548SDimitry Andric BitVector
getReservedRegs(const MachineFunction &) const467d523365SDimitry Andric WebAssemblyRegisterInfo::getReservedRegs(const MachineFunction & /*MF*/) const {
47875ed548SDimitry Andric   BitVector Reserved(getNumRegs());
48875ed548SDimitry Andric   for (auto Reg : {WebAssembly::SP32, WebAssembly::SP64, WebAssembly::FP32,
49875ed548SDimitry Andric                    WebAssembly::FP64})
50875ed548SDimitry Andric     Reserved.set(Reg);
51875ed548SDimitry Andric   return Reserved;
52875ed548SDimitry Andric }
53875ed548SDimitry Andric 
eliminateFrameIndex(MachineBasicBlock::iterator II,int SPAdj,unsigned FIOperandNum,RegScavenger *) const54875ed548SDimitry Andric void WebAssemblyRegisterInfo::eliminateFrameIndex(
553ca95b02SDimitry Andric     MachineBasicBlock::iterator II, int SPAdj, unsigned FIOperandNum,
563ca95b02SDimitry Andric     RegScavenger * /*RS*/) const {
577d523365SDimitry Andric   assert(SPAdj == 0);
587d523365SDimitry Andric   MachineInstr &MI = *II;
597d523365SDimitry Andric 
607d523365SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
617d523365SDimitry Andric   MachineFunction &MF = *MBB.getParent();
623ca95b02SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
637d523365SDimitry Andric   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
64d88c1a5aSDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
65444ed5c5SDimitry Andric   int64_t FrameOffset = MFI.getStackSize() + MFI.getObjectOffset(FrameIndex);
667d523365SDimitry Andric 
67d88c1a5aSDimitry Andric   assert(MFI.getObjectSize(FrameIndex) != 0 &&
68d88c1a5aSDimitry Andric          "We assume that variable-sized objects have already been lowered, "
69d88c1a5aSDimitry Andric          "and don't use FrameIndex operands.");
70d88c1a5aSDimitry Andric   unsigned FrameRegister = getFrameRegister(MF);
71d88c1a5aSDimitry Andric 
723ca95b02SDimitry Andric   // If this is the address operand of a load or store, make it relative to SP
733ca95b02SDimitry Andric   // and fold the frame offset directly in.
74d88c1a5aSDimitry Andric   if ((MI.mayLoad() && FIOperandNum == WebAssembly::LoadAddressOperandNo) ||
75d88c1a5aSDimitry Andric       (MI.mayStore() && FIOperandNum == WebAssembly::StoreAddressOperandNo)) {
76d88c1a5aSDimitry Andric     assert(FrameOffset >= 0 && MI.getOperand(FIOperandNum - 1).getImm() >= 0);
77d88c1a5aSDimitry Andric     int64_t Offset = MI.getOperand(FIOperandNum - 1).getImm() + FrameOffset;
78444ed5c5SDimitry Andric 
793ca95b02SDimitry Andric     if (static_cast<uint64_t>(Offset) <= std::numeric_limits<uint32_t>::max()) {
803ca95b02SDimitry Andric       MI.getOperand(FIOperandNum - 1).setImm(Offset);
813ca95b02SDimitry Andric       MI.getOperand(FIOperandNum)
82d88c1a5aSDimitry Andric           .ChangeToRegister(FrameRegister, /*IsDef=*/false);
833ca95b02SDimitry Andric       return;
84444ed5c5SDimitry Andric     }
853ca95b02SDimitry Andric   }
867d523365SDimitry Andric 
873ca95b02SDimitry Andric   // If this is an address being added to a constant, fold the frame offset
883ca95b02SDimitry Andric   // into the constant.
893ca95b02SDimitry Andric   if (MI.getOpcode() == WebAssembly::ADD_I32) {
903ca95b02SDimitry Andric     MachineOperand &OtherMO = MI.getOperand(3 - FIOperandNum);
913ca95b02SDimitry Andric     if (OtherMO.isReg()) {
923ca95b02SDimitry Andric       unsigned OtherMOReg = OtherMO.getReg();
933ca95b02SDimitry Andric       if (TargetRegisterInfo::isVirtualRegister(OtherMOReg)) {
943ca95b02SDimitry Andric         MachineInstr *Def = MF.getRegInfo().getUniqueVRegDef(OtherMOReg);
953ca95b02SDimitry Andric         // TODO: For now we just opportunistically do this in the case where
963ca95b02SDimitry Andric         // the CONST_I32 happens to have exactly one def and one use. We
973ca95b02SDimitry Andric         // should generalize this to optimize in more cases.
983ca95b02SDimitry Andric         if (Def && Def->getOpcode() == WebAssembly::CONST_I32 &&
993ca95b02SDimitry Andric             MRI.hasOneNonDBGUse(Def->getOperand(0).getReg())) {
1003ca95b02SDimitry Andric           MachineOperand &ImmMO = Def->getOperand(1);
1013ca95b02SDimitry Andric           ImmMO.setImm(ImmMO.getImm() + uint32_t(FrameOffset));
1023ca95b02SDimitry Andric           MI.getOperand(FIOperandNum)
103d88c1a5aSDimitry Andric               .ChangeToRegister(FrameRegister, /*IsDef=*/false);
1043ca95b02SDimitry Andric           return;
1057d523365SDimitry Andric         }
106875ed548SDimitry Andric       }
1073ca95b02SDimitry Andric     }
1083ca95b02SDimitry Andric   }
1093ca95b02SDimitry Andric 
1103ca95b02SDimitry Andric   // Otherwise create an i32.add SP, offset and make it the operand.
1113ca95b02SDimitry Andric   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
1123ca95b02SDimitry Andric 
113d88c1a5aSDimitry Andric   unsigned FIRegOperand = FrameRegister;
1143ca95b02SDimitry Andric   if (FrameOffset) {
1153ca95b02SDimitry Andric     // Create i32.add SP, offset and make it the operand.
1163ca95b02SDimitry Andric     const TargetRegisterClass *PtrRC =
1173ca95b02SDimitry Andric         MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
1183ca95b02SDimitry Andric     unsigned OffsetOp = MRI.createVirtualRegister(PtrRC);
1193ca95b02SDimitry Andric     BuildMI(MBB, *II, II->getDebugLoc(), TII->get(WebAssembly::CONST_I32),
1203ca95b02SDimitry Andric             OffsetOp)
1213ca95b02SDimitry Andric         .addImm(FrameOffset);
1223ca95b02SDimitry Andric     FIRegOperand = MRI.createVirtualRegister(PtrRC);
1233ca95b02SDimitry Andric     BuildMI(MBB, *II, II->getDebugLoc(), TII->get(WebAssembly::ADD_I32),
1243ca95b02SDimitry Andric             FIRegOperand)
125d88c1a5aSDimitry Andric         .addReg(FrameRegister)
1263ca95b02SDimitry Andric         .addReg(OffsetOp);
1273ca95b02SDimitry Andric   }
1283ca95b02SDimitry Andric   MI.getOperand(FIOperandNum).ChangeToRegister(FIRegOperand, /*IsDef=*/false);
1293ca95b02SDimitry Andric }
130875ed548SDimitry Andric 
131875ed548SDimitry Andric unsigned
getFrameRegister(const MachineFunction & MF) const132875ed548SDimitry Andric WebAssemblyRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
133875ed548SDimitry Andric   static const unsigned Regs[2][2] = {
134875ed548SDimitry Andric       /*            !isArch64Bit       isArch64Bit      */
135875ed548SDimitry Andric       /* !hasFP */ {WebAssembly::SP32, WebAssembly::SP64},
136875ed548SDimitry Andric       /*  hasFP */ {WebAssembly::FP32, WebAssembly::FP64}};
137875ed548SDimitry Andric   const WebAssemblyFrameLowering *TFI = getFrameLowering(MF);
138875ed548SDimitry Andric   return Regs[TFI->hasFP(MF)][TT.isArch64Bit()];
139875ed548SDimitry Andric }
140875ed548SDimitry Andric 
1417d523365SDimitry Andric const TargetRegisterClass *
getPointerRegClass(const MachineFunction & MF,unsigned Kind) const1427d523365SDimitry Andric WebAssemblyRegisterInfo::getPointerRegClass(const MachineFunction &MF,
1437d523365SDimitry Andric                                             unsigned Kind) const {
1447d523365SDimitry Andric   assert(Kind == 0 && "Only one kind of pointer on WebAssembly");
1457d523365SDimitry Andric   if (MF.getSubtarget<WebAssemblySubtarget>().hasAddr64())
1467d523365SDimitry Andric     return &WebAssembly::I64RegClass;
1477d523365SDimitry Andric   return &WebAssembly::I32RegClass;
148875ed548SDimitry Andric }
149