LLVM 24.0.0git
MipsABIInfo.cpp
Go to the documentation of this file.
1//===---- MipsABIInfo.cpp - Information about MIPS ABI's ------------------===//
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#include "MipsABIInfo.h"
10#include "Mips.h"
11#include "llvm/ADT/StringRef.h"
13
14using namespace llvm;
15
16namespace {
17static constexpr MCPhysReg O32IntRegs[] = {Mips::R4, Mips::R5, Mips::R6,
18 Mips::R7};
19static constexpr MCPhysReg NABIIntRegs[] = {Mips::R4, Mips::R5, Mips::R6,
20 Mips::R7, Mips::R8, Mips::R9,
21 Mips::R10, Mips::R11};
22static const MCPhysReg P32IntRegs[] = {Mips::R4, Mips::R5, Mips::R6,
23 Mips::R7, Mips::R8, Mips::R9,
24 Mips::R10, Mips::R11};
25static constexpr MCPhysReg Mips64IntRegs[] = {
26 Mips::R4_64, Mips::R5_64, Mips::R6_64, Mips::R7_64,
27 Mips::R8_64, Mips::R9_64, Mips::R10_64, Mips::R11_64};
28
29struct GPR {
30 MCPhysReg Reg32;
31 MCPhysReg Reg64;
32};
33
34static constexpr GPR OABITempRegs[] = {
35 {Mips::R8, Mips::R8_64}, {Mips::R9, Mips::R9_64},
36 {Mips::R10, Mips::R10_64}, {Mips::R11, Mips::R11_64},
37 {Mips::R12, Mips::R12_64}, {Mips::R13, Mips::R13_64},
38 {Mips::R14, Mips::R14_64}, {Mips::R15, Mips::R15_64},
39 {Mips::R24, Mips::R24_64}, {Mips::R25, Mips::R25_64},
40};
41
42static constexpr GPR NABITempRegs[] = {
43 {Mips::R12, Mips::R12_64},
44 {Mips::R13, Mips::R13_64},
45 {Mips::R14, Mips::R14_64},
46 {Mips::R15, Mips::R15_64},
47 {Mips::NoRegister, Mips::NoRegister},
48 {Mips::NoRegister, Mips::NoRegister},
49 {Mips::NoRegister, Mips::NoRegister},
50 {Mips::NoRegister, Mips::NoRegister},
51 {Mips::R24, Mips::R24_64},
52 {Mips::R25, Mips::R25_64},
53};
54
55static constexpr GPR PABITempRegs[] = {
56 {Mips::R12, Mips::R12_64},
57 {Mips::R13, Mips::R13_64},
58 {Mips::R14, Mips::R14_64},
59 {Mips::R15, Mips::R15_64},
60 {Mips::R2, Mips::R2_64},
61 {Mips::R3, Mips::R3_64},
62 {Mips::NoRegister, Mips::NoRegister},
63 {Mips::NoRegister, Mips::NoRegister},
64 {Mips::R24, Mips::R24_64},
65 {Mips::R25, Mips::R25_64},
66};
67
68static constexpr GPR SavedRegs[] = {
69 {Mips::R16, Mips::R16_64}, {Mips::R17, Mips::R17_64},
70 {Mips::R18, Mips::R18_64}, {Mips::R19, Mips::R19_64},
71 {Mips::R20, Mips::R20_64}, {Mips::R21, Mips::R21_64},
72 {Mips::R22, Mips::R22_64}, {Mips::R23, Mips::R23_64},
73};
74
75static constexpr GPR ReturnRegs[] = {
76 {Mips::R2, Mips::R2_64},
77 {Mips::R3, Mips::R3_64},
78};
79
80static constexpr GPR PABIReturnRegs[] = {
81 {Mips::R4, Mips::R4_64},
82 {Mips::R5, Mips::R5_64},
83};
84
85MCRegister getReg(ArrayRef<GPR> Regs, unsigned I, bool Is64Bit) {
86 assert(I < Regs.size() && "Invalid ABI register index");
87 MCRegister Reg = Is64Bit ? Regs[I].Reg64 : Regs[I].Reg32;
88 assert(Reg && "Register name is not defined by this ABI");
89 return Reg;
90}
91} // namespace
92
94 assert(IsKnown() && "Unknown ABI");
95 if (Is64Bit)
96 return ArrayRef(Mips64IntRegs).take_front(IsO32() ? 4 : 8);
97 return IsO32() ? ArrayRef(O32IntRegs)
98 : IsP32() ? ArrayRef(P32IntRegs)
99 : ArrayRef(NABIIntRegs);
100}
101
105
107 switch (ThisABI) {
108 case ABI::O32:
109 return Mips::OABIRegAltName;
110 case ABI::N32:
111 case ABI::N64:
112 return Mips::NABIRegAltName;
113 case ABI::P32:
114 return Mips::PABIRegAltName;
115 case ABI::Unknown:
116 llvm_unreachable("Unknown ABI");
117 }
118 llvm_unreachable("Unhandled ABI");
119}
120
121MCRegister MipsABIInfo::getArgReg(unsigned I, bool Is64Bit) const {
122 ArrayRef<MCPhysReg> Regs = getArgRegs(Is64Bit);
123 assert(I < Regs.size() && "Invalid argument register");
124 return Regs[I];
125}
126
127MCRegister MipsABIInfo::getTempReg(unsigned I, bool Is64Bit) const {
128 switch (getRegAltNameIndex()) {
129 case Mips::OABIRegAltName:
130 return getReg(OABITempRegs, I, Is64Bit);
131 case Mips::NABIRegAltName:
132 return getReg(NABITempRegs, I, Is64Bit);
133 case Mips::PABIRegAltName:
134 return getReg(PABITempRegs, I, Is64Bit);
135 default:
136 llvm_unreachable("Unknown register naming convention");
137 }
138}
139
140MCRegister MipsABIInfo::getSavedReg(unsigned I, bool Is64Bit) const {
141 assert(IsKnown() && "Unknown ABI");
142 return getReg(SavedRegs, I, Is64Bit);
143}
144
145MCRegister MipsABIInfo::getReturnReg(unsigned I, bool Is64Bit) const {
146 switch (ThisABI) {
147 case ABI::O32:
148 case ABI::N32:
149 case ABI::N64:
150 return getReg(ReturnRegs, I, Is64Bit);
151 case ABI::P32:
152 return getReg(PABIReturnRegs, I, Is64Bit);
153 case ABI::Unknown:
154 llvm_unreachable("Unknown ABI");
155 }
156 llvm_unreachable("Unhandled ABI");
157}
158
160 if (IsO32()) {
161 if (isGP64bit)
162 return ArrayRef(Mips64IntRegs);
163 else
164 return ArrayRef(O32IntRegs);
165 }
166 if (IsN32() || IsN64())
167 return ArrayRef(Mips64IntRegs);
168 if (IsP32())
169 return ArrayRef(P32IntRegs);
170 llvm_unreachable("Unhandled ABI");
171}
172
174 if (IsO32())
175 return CC != CallingConv::Fast ? 16 : 0;
176 if (IsN32() || IsN64() || IsP32())
177 return 0;
178 llvm_unreachable("Unhandled ABI");
179}
180
182 if (ABIName.starts_with("o32"))
183 return MipsABIInfo::O32();
184 if (ABIName.starts_with("n32"))
185 return MipsABIInfo::N32();
186 if (ABIName.starts_with("n64"))
187 return MipsABIInfo::N64();
188 if (ABIName.starts_with("p32"))
189 return MipsABIInfo::P32();
190 if (TT.isABIN32())
191 return MipsABIInfo::N32();
192 assert(ABIName.empty() && "Unknown ABI option for MIPS");
193
194 if (TT.isMIPS64())
195 return MipsABIInfo::N64();
196 if (TT.isNanoMips())
197 return MipsABIInfo::P32();
198 return MipsABIInfo::O32();
199}
200
201unsigned MipsABIInfo::GetStackPtr() const {
202 return ArePtrs64bit() ? Mips::SP_64 : Mips::SP;
203}
204
205unsigned MipsABIInfo::GetFramePtr() const {
206 return ArePtrs64bit() ? Mips::FP_64 : Mips::FP;
207}
208
209unsigned MipsABIInfo::GetBasePtr() const { return getSavedRegPtr(7); }
210
212 return ArePtrs64bit() ? Mips::GP_64 : Mips::GP;
213}
214
215unsigned MipsABIInfo::GetNullPtr() const {
216 return ArePtrs64bit() ? Mips::R0_64 : Mips::R0;
217}
218
219unsigned MipsABIInfo::GetZeroReg() const {
220 return AreGprs64bit() ? Mips::R0_64 : Mips::R0;
221}
222
224 return ArePtrs64bit() ? Mips::DADDu : IsP32() ? Mips::ADDu_NM : Mips::ADDu;
225}
226
228 return ArePtrs64bit() ? Mips::DADDiu : IsP32() ? Mips::ADDIU_NM : Mips::ADDiu;
229}
230
232 return ArePtrs64bit() ? Mips::DSUBu : IsP32() ? Mips::SUBu_NM : Mips::SUBu;
233}
234
235unsigned MipsABIInfo::GetPtrAndOp() const {
236 return ArePtrs64bit() ? Mips::AND64 : IsP32() ? Mips::AND_NM : Mips::AND;
237}
238
240 return ArePtrs64bit() ? Mips::OR64 : IsP32() ? Mips::OR_NM : Mips::OR;
241}
242
243unsigned MipsABIInfo::GetEhDataReg(unsigned I) const {
244 assert(I < 4 && "Invalid EH data register");
245 return getArgRegPtr(I);
246}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
RISCVCC::CondCode CC
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
bool AreGprs64bit() const
bool IsN64() const
Definition MipsABIInfo.h:42
unsigned GetGlobalPtr() const
static MipsABIInfo O32()
Definition MipsABIInfo.h:33
MipsABIInfo(ABI ThisABI)
Definition MipsABIInfo.h:30
unsigned GetEhDataReg(unsigned I) const
bool ArePtrs64bit() const
unsigned GetGPRMoveOp() const
MCRegister getArgReg(unsigned I, bool Is64Bit) const
ABI register accessors default to the ABI's GPR width.
static MipsABIInfo P32()
Definition MipsABIInfo.h:36
unsigned GetStackPtr() const
static MipsABIInfo N32()
Definition MipsABIInfo.h:34
unsigned GetZeroReg() const
unsigned GetCalleeAllocdArgSizeInBytes(CallingConv::ID CC) const
Obtain the size of the area allocated by the callee for arguments.
static MipsABIInfo N64()
Definition MipsABIInfo.h:35
unsigned GetPtrAddiuOp() const
unsigned GetPtrAndOp() const
unsigned GetFramePtr() const
unsigned getRegAltNameIndex() const
Register naming convention for this ABI.
static MipsABIInfo computeTargetABI(const Triple &TT, StringRef ABIName)
ArrayRef< MCPhysReg > GetByValArgRegs() const
The registers to use for byval arguments.
unsigned GetNullPtr() const
ArrayRef< MCPhysReg > getVarArgRegs(bool isGP64bit) const
The registers to use for the variable argument list.
unsigned GetPtrAdduOp() const
bool IsKnown() const
Definition MipsABIInfo.h:39
bool IsN32() const
Definition MipsABIInfo.h:41
MCRegister getReturnReg(unsigned I, bool Is64Bit) const
Integer return-value registers.
MCRegister getSavedReg(unsigned I, bool Is64Bit) const
unsigned GetBasePtr() const
unsigned GetPtrSubuOp() const
MCRegister getSavedRegPtr(unsigned I) const
Definition MipsABIInfo.h:74
bool IsO32() const
Definition MipsABIInfo.h:40
bool IsP32() const
Definition MipsABIInfo.h:43
MCRegister getTempReg(unsigned I, bool Is64Bit) const
I is the suffix in tI (NABI has t0-t3 and t8-t9).
ArrayRef< MCPhysReg > getArgRegs(bool Is64Bit) const
Integer argument registers in calling-convention order.
MCRegister getArgRegPtr(unsigned I) const
Definition MipsABIInfo.h:59
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition CallingConv.h:41
constexpr MCPhysReg FP_64
constexpr MCPhysReg GP_64
constexpr MCPhysReg FP
constexpr MCPhysReg GP
constexpr MCPhysReg SP
constexpr MCPhysReg SP_64
This is an optimization pass for GlobalISel generic memory operations.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
ArrayRef(const T &OneElt) -> ArrayRef< T >