1//===-- RISCVRegisterInfo.h - RISC-V Register Information Impl --*- C++ -*-===//
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// This file contains the RISC-V implementation of the TargetRegisterInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_RISCV_RISCVREGISTERINFO_H
14#define LLVM_LIB_TARGET_RISCV_RISCVREGISTERINFO_H
15
16#include "llvm/CodeGen/TargetRegisterInfo.h"
17#include "llvm/TargetParser/RISCVTargetParser.h"
18#include <bitset>
19
20#define GET_REGINFO_HEADER
21#include "RISCVGenRegisterInfo.inc"
22
23namespace llvm {
24
25namespace RISCVRI {
26enum : uint8_t {
27 // The IsVRegClass value of this RegisterClass.
28 IsVRegClassShift = 0,
29 IsVRegClassShiftMask = 0b1 << IsVRegClassShift,
30 // The VLMul value of this RegisterClass. This value is valid iff IsVRegClass
31 // is true.
32 VLMulShift = IsVRegClassShift + 1,
33 VLMulShiftMask = 0b11 << VLMulShift,
34
35 // The NF value of this RegisterClass. This value is valid iff IsVRegClass is
36 // true.
37 NFShift = VLMulShift + 2,
38 NFShiftMask = 0b111 << NFShift,
39};
40
41/// Register allocation hints for Zilsd register pairs
42enum {
43 // Used for Zilsd LD/SD register pairs
44 RegPairOdd = 1,
45 RegPairEven = 2,
46};
47
48/// \returns the IsVRegClass for the register class.
49static inline bool isVRegClass(uint8_t TSFlags) {
50 return (TSFlags & IsVRegClassShiftMask) >> IsVRegClassShift;
51}
52
53/// \returns the LMUL for the register class.
54static inline RISCVVType::VLMUL getLMul(uint8_t TSFlags) {
55 return static_cast<RISCVVType::VLMUL>((TSFlags & VLMulShiftMask) >>
56 VLMulShift);
57}
58
59/// \returns the NF for the register class.
60static inline unsigned getNF(uint8_t TSFlags) {
61 return static_cast<unsigned>((TSFlags & NFShiftMask) >> NFShift) + 1;
62}
63} // namespace RISCVRI
64
65struct RISCVRegisterInfo : public RISCVGenRegisterInfo {
66
67 /// \p UserReservedXRegs is the set of X registers reserved by the reserve-xN
68 /// features, indexed by N.
69 RISCVRegisterInfo(unsigned HwMode, const std::bitset<32> &UserReservedXRegs);
70
71 const uint32_t *getCallPreservedMask(const MachineFunction &MF,
72 CallingConv::ID) const override;
73
74 unsigned getCSRCost() const override {
75 // The cost will be compared against BlockFrequency where entry has the
76 // value of 1 << 14. A value of 5 will choose to spill or split cold
77 // path instead of using a callee-saved register.
78 return 5;
79 }
80
81 const MCPhysReg *getCalleeSavedRegs(const MachineFunction *MF) const override;
82
83 const TargetRegisterClass *
84 getConstrainedRegClassForReg(Register Reg,
85 const MachineRegisterInfo &MRI) const override;
86
87 const TargetRegisterClass *
88 getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB, bool Is64Bit) const;
89
90 const MCPhysReg *getIPRACSRegs(const MachineFunction *MF) const override;
91
92 BitVector getReservedRegs(const MachineFunction &MF) const override;
93
94 /// Returns true if \p Reg is, or overlaps, a register reserved by the
95 /// reserve-xN features, e.g. X10_W, X10_H or a GPRPair containing X10.
96 bool isRegisterReservedByUser(MCRegister Reg) const {
97 return UserReservedRegs.test(Idx: Reg.id());
98 }
99 bool isAsmClobberable(const MachineFunction &MF,
100 MCRegister PhysReg) const override;
101
102 const uint32_t *getNoPreservedMask() const override;
103
104 // Update DestReg to have the value SrcReg plus an offset. This is
105 // used during frame layout, and we may need to ensure that if we
106 // split the offset internally that the DestReg is always aligned,
107 // assuming that source reg was.
108 void adjustReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator II,
109 const DebugLoc &DL, Register DestReg, Register SrcReg,
110 StackOffset Offset, MachineInstr::MIFlag Flag,
111 MaybeAlign RequiredAlign) const;
112
113 bool eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj,
114 unsigned FIOperandNum,
115 RegScavenger *RS = nullptr) const override;
116
117 bool requiresVirtualBaseRegisters(const MachineFunction &MF) const override;
118
119 bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const override;
120
121 bool isFrameOffsetLegal(const MachineInstr *MI, Register BaseReg,
122 int64_t Offset) const override;
123
124 Register materializeFrameBaseRegister(MachineBasicBlock *MBB, int FrameIdx,
125 int64_t Offset) const override;
126
127 void resolveFrameIndex(MachineInstr &MI, Register BaseReg,
128 int64_t Offset) const override;
129
130 int64_t getFrameIndexInstrOffset(const MachineInstr *MI,
131 int Idx) const override;
132
133 void lowerSegmentSpillReload(MachineBasicBlock::iterator II,
134 bool IsSpill) const;
135
136 Register getFrameRegister(const MachineFunction &MF) const override;
137
138 bool isArgumentRegister(const MachineFunction &MF,
139 MCRegister Reg) const override;
140
141 StringRef getRegAsmName(MCRegister Reg) const override;
142
143 bool requiresRegisterScavenging(const MachineFunction &MF) const override {
144 return true;
145 }
146
147 bool requiresFrameIndexScavenging(const MachineFunction &MF) const override {
148 return true;
149 }
150
151 const TargetRegisterClass *
152 getLargestLegalSuperClass(const TargetRegisterClass *RC,
153 const MachineFunction &) const override;
154
155 void getOffsetOpcodes(const StackOffset &Offset,
156 SmallVectorImpl<uint64_t> &Ops) const override;
157
158 // Returns the even and odd GPRs that make up a GPRPair register. X0_Pair has
159 // no real odd subregister, so both halves are X0.
160 std::pair<Register, Register> getGPRPairSubRegs(MCRegister Pair) const;
161
162 unsigned getRegisterCostTableIndex(const MachineFunction &MF) const override;
163
164 float getSpillWeightScaleFactor(const TargetRegisterClass *RC) const override;
165
166 bool getRegAllocationHints(Register VirtReg, ArrayRef<MCPhysReg> Order,
167 SmallSetVector<MCPhysReg, 16> &Hints,
168 const MachineFunction &MF, const VirtRegMap *VRM,
169 const LiveRegMatrix *Matrix) const override;
170
171 void updateRegAllocHint(Register Reg, Register NewReg,
172 MachineFunction &MF) const override;
173
174 MCRegister findVRegWithEncoding(const TargetRegisterClass &RegClass,
175 uint16_t Encoding) const;
176
177 static bool isVRRegClass(const TargetRegisterClass *RC) {
178 return RISCVRI::isVRegClass(TSFlags: RC->TSFlags) &&
179 RISCVRI::getNF(TSFlags: RC->TSFlags) == 1;
180 }
181
182 static bool isVRNRegClass(const TargetRegisterClass *RC) {
183 return RISCVRI::isVRegClass(TSFlags: RC->TSFlags) && RISCVRI::getNF(TSFlags: RC->TSFlags) > 1;
184 }
185
186 static bool isRVVRegClass(const TargetRegisterClass *RC) {
187 return RISCVRI::isVRegClass(TSFlags: RC->TSFlags);
188 }
189
190 static bool isFPRegister(MCRegister Reg) {
191 return RISCV::FPR16RegClass.contains(Reg) ||
192 RISCV::FPR32RegClass.contains(Reg) ||
193 RISCV::FPR64RegClass.contains(Reg) ||
194 RISCV::FPR128RegClass.contains(Reg);
195 }
196
197private:
198 // The registers reserved by the reserve-xN features and all their aliases.
199 BitVector UserReservedRegs;
200};
201} // namespace llvm
202
203#endif
204