LLVM 20.0.0git
WebAssemblyRegisterInfo.cpp
Go to the documentation of this file.
1//===-- WebAssemblyRegisterInfo.cpp - WebAssembly Register Information ----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains the WebAssembly implementation of the
11/// TargetRegisterInfo class.
12///
13//===----------------------------------------------------------------------===//
14
26using namespace llvm;
27
28#define DEBUG_TYPE "wasm-reg-info"
29
30#define GET_REGINFO_TARGET_DESC
31#include "WebAssemblyGenRegisterInfo.inc"
32
34 : WebAssemblyGenRegisterInfo(0), TT(TT) {}
35
36const MCPhysReg *
38 static const MCPhysReg CalleeSavedRegs[] = {0};
39 return CalleeSavedRegs;
40}
41
44 BitVector Reserved(getNumRegs());
45 for (auto Reg : {WebAssembly::SP32, WebAssembly::SP64, WebAssembly::FP32,
46 WebAssembly::FP64})
47 Reserved.set(Reg);
48 return Reserved;
49}
50
52 MachineBasicBlock::iterator II, int SPAdj, unsigned FIOperandNum,
53 RegScavenger * /*RS*/) const {
54 assert(SPAdj == 0);
55 MachineInstr &MI = *II;
56
57 MachineBasicBlock &MBB = *MI.getParent();
60 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
61 const MachineFrameInfo &MFI = MF.getFrameInfo();
62 int64_t FrameOffset = MFI.getStackSize() + MFI.getObjectOffset(FrameIndex);
63
64 assert(MFI.getObjectSize(FrameIndex) != 0 &&
65 "We assume that variable-sized objects have already been lowered, "
66 "and don't use FrameIndex operands.");
67 Register FrameRegister = getFrameRegister(MF);
68
69 // If this is the address operand of a load or store, make it relative to SP
70 // and fold the frame offset directly in.
71 unsigned AddrOperandNum = WebAssembly::getNamedOperandIdx(
72 MI.getOpcode(), WebAssembly::OpName::addr);
73 if (AddrOperandNum == FIOperandNum) {
74 unsigned OffsetOperandNum = WebAssembly::getNamedOperandIdx(
75 MI.getOpcode(), WebAssembly::OpName::off);
76 assert(FrameOffset >= 0 && MI.getOperand(OffsetOperandNum).getImm() >= 0);
77 int64_t Offset = MI.getOperand(OffsetOperandNum).getImm() + FrameOffset;
78
79 if (static_cast<uint64_t>(Offset) <= std::numeric_limits<uint32_t>::max()) {
80 MI.getOperand(OffsetOperandNum).setImm(Offset);
81 MI.getOperand(FIOperandNum)
82 .ChangeToRegister(FrameRegister, /*isDef=*/false);
83 return false;
84 }
85 }
86
87 // If this is an address being added to a constant, fold the frame offset
88 // into the constant.
89 if (MI.getOpcode() == WebAssemblyFrameLowering::getOpcAdd(MF)) {
90 MachineOperand &OtherMO = MI.getOperand(3 - FIOperandNum);
91 if (OtherMO.isReg()) {
92 Register OtherMOReg = OtherMO.getReg();
93 if (OtherMOReg.isVirtual()) {
94 MachineInstr *Def = MF.getRegInfo().getUniqueVRegDef(OtherMOReg);
95 // TODO: For now we just opportunistically do this in the case where
96 // the CONST_I32/64 happens to have exactly one def and one use. We
97 // should generalize this to optimize in more cases.
98 if (Def && Def->getOpcode() ==
100 MRI.hasOneNonDBGUse(Def->getOperand(0).getReg())) {
101 MachineOperand &ImmMO = Def->getOperand(1);
102 if (ImmMO.isImm()) {
103 ImmMO.setImm(ImmMO.getImm() + uint32_t(FrameOffset));
104 MI.getOperand(FIOperandNum)
105 .ChangeToRegister(FrameRegister, /*isDef=*/false);
106 return false;
107 }
108 }
109 }
110 }
111 }
112
113 // Otherwise create an i32/64.add SP, offset and make it the operand.
114 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
115
116 unsigned FIRegOperand = FrameRegister;
117 if (FrameOffset) {
118 // Create i32/64.add SP, offset and make it the operand.
119 const TargetRegisterClass *PtrRC =
120 MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
121 Register OffsetOp = MRI.createVirtualRegister(PtrRC);
122 BuildMI(MBB, *II, II->getDebugLoc(),
124 OffsetOp)
125 .addImm(FrameOffset);
126 FIRegOperand = MRI.createVirtualRegister(PtrRC);
127 BuildMI(MBB, *II, II->getDebugLoc(),
129 FIRegOperand)
130 .addReg(FrameRegister)
131 .addReg(OffsetOp);
132 }
133 MI.getOperand(FIOperandNum).ChangeToRegister(FIRegOperand, /*isDef=*/false);
134 return false;
135}
136
139 // If the PReg has been replaced by a VReg, return that.
140 const auto &MFI = MF.getInfo<WebAssemblyFunctionInfo>();
141 if (MFI->isFrameBaseVirtual())
142 return MFI->getFrameBaseVreg();
143 static const unsigned Regs[2][2] = {
144 /* !isArch64Bit isArch64Bit */
145 /* !hasFP */ {WebAssembly::SP32, WebAssembly::SP64},
146 /* hasFP */ {WebAssembly::FP32, WebAssembly::FP64}};
147 const WebAssemblyFrameLowering *TFI = getFrameLowering(MF);
148 return Regs[TFI->hasFP(MF)][TT.isArch64Bit()];
149}
150
153 unsigned Kind) const {
154 assert(Kind == 0 && "Only one kind of pointer on WebAssembly");
156 return &WebAssembly::I64RegClass;
157 return &WebAssembly::I32RegClass;
158}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements WebAssembly-specific bits of TargetFrameLowering class.
This file contains the WebAssembly implementation of the TargetInstrInfo class.
This file provides WebAssembly-specific target descriptions.
This file declares WebAssembly-specific per-machine-function information.
This file contains the WebAssembly implementation of the WebAssemblyRegisterInfo class.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
void setImm(int64_t immVal)
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:91
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isArch64Bit() const
Test whether the architecture is 64-bit.
Definition: Triple.cpp:1681
static unsigned getOpcAdd(const MachineFunction &MF)
static unsigned getOpcConst(const MachineFunction &MF)
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
bool eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
Register getFrameRegister(const MachineFunction &MF) const override
BitVector getReservedRegs(const MachineFunction &MF) const override
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
const TargetRegisterClass * getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const override
int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIndex)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.