Skip to content

Commit 23dfa8c

Browse files
committed
[Mips] Support "$sp" named register
Fix #47656.
1 parent 36c1273 commit 23dfa8c

File tree

5 files changed

+923
-43
lines changed

5 files changed

+923
-43
lines changed

llvm/lib/Target/Mips/MipsISelLowering.cpp

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
#include <cstdint>
7373
#include <deque>
7474
#include <iterator>
75+
#include <regex>
76+
#include <string>
7577
#include <utility>
7678
#include <vector>
7779

@@ -4938,25 +4940,100 @@ MipsTargetLowering::emitPseudoD_SELECT(MachineInstr &MI,
49384940
return BB;
49394941
}
49404942

4943+
// Copies the function MipsAsmParser::matchCPURegisterName.
4944+
int MipsTargetLowering::getCPURegisterIndex(StringRef Name) const {
4945+
int CC;
4946+
4947+
CC = StringSwitch<unsigned>(Name)
4948+
.Case("zero", 0)
4949+
.Cases("at", "AT", 1)
4950+
.Case("a0", 4)
4951+
.Case("a1", 5)
4952+
.Case("a2", 6)
4953+
.Case("a3", 7)
4954+
.Case("v0", 2)
4955+
.Case("v1", 3)
4956+
.Case("s0", 16)
4957+
.Case("s1", 17)
4958+
.Case("s2", 18)
4959+
.Case("s3", 19)
4960+
.Case("s4", 20)
4961+
.Case("s5", 21)
4962+
.Case("s6", 22)
4963+
.Case("s7", 23)
4964+
.Case("k0", 26)
4965+
.Case("k1", 27)
4966+
.Case("gp", 28)
4967+
.Case("sp", 29)
4968+
.Case("fp", 30)
4969+
.Case("s8", 30)
4970+
.Case("ra", 31)
4971+
.Case("t0", 8)
4972+
.Case("t1", 9)
4973+
.Case("t2", 10)
4974+
.Case("t3", 11)
4975+
.Case("t4", 12)
4976+
.Case("t5", 13)
4977+
.Case("t6", 14)
4978+
.Case("t7", 15)
4979+
.Case("t8", 24)
4980+
.Case("t9", 25)
4981+
.Default(-1);
4982+
4983+
if (!(ABI.IsN32() || ABI.IsN64()))
4984+
return CC;
4985+
4986+
// Although SGI documentation just cuts out t0-t3 for n32/n64,
4987+
// GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
4988+
// We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
4989+
if (8 <= CC && CC <= 11)
4990+
CC += 4;
4991+
4992+
if (CC == -1)
4993+
CC = StringSwitch<unsigned>(Name)
4994+
.Case("a4", 8)
4995+
.Case("a5", 9)
4996+
.Case("a6", 10)
4997+
.Case("a7", 11)
4998+
.Case("kt0", 26)
4999+
.Case("kt1", 27)
5000+
.Default(-1);
5001+
5002+
return CC;
5003+
}
5004+
49415005
// FIXME? Maybe this could be a TableGen attribute on some registers and
49425006
// this table could be generated automatically from RegInfo.
49435007
Register
49445008
MipsTargetLowering::getRegisterByName(const char *RegName, LLT VT,
49455009
const MachineFunction &MF) const {
4946-
// The Linux kernel uses $28 and sp.
4947-
if (Subtarget.isGP64bit()) {
4948-
Register Reg = StringSwitch<Register>(RegName)
4949-
.Case("$28", Mips::GP_64)
4950-
.Case("sp", Mips::SP_64)
4951-
.Default(Register());
4952-
return Reg;
5010+
// 1. Delete symbol '$'.
5011+
std::string newRegName = RegName;
5012+
if (StringRef(RegName).starts_with("$"))
5013+
newRegName = StringRef(RegName).substr(1);
5014+
5015+
// 2. Get register index value.
5016+
std::smatch matchResult;
5017+
int regIdx;
5018+
static const std::regex matchStr("^[0-9]*$");
5019+
if (std::regex_match(newRegName, matchResult, matchStr))
5020+
regIdx = std::stoi(newRegName);
5021+
else {
5022+
newRegName = StringRef(newRegName).lower();
5023+
regIdx = getCPURegisterIndex(StringRef(newRegName));
5024+
}
5025+
5026+
// 3. Get register.
5027+
if (regIdx >= 0 && regIdx < 32) {
5028+
const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();
5029+
const MCRegisterClass &RC = Subtarget.isGP64bit()
5030+
? MRI->getRegClass(Mips::GPR64RegClassID)
5031+
: MRI->getRegClass(Mips::GPR32RegClassID);
5032+
return RC.getRegister(regIdx);
49535033
}
49545034

4955-
Register Reg = StringSwitch<Register>(RegName)
4956-
.Case("$28", Mips::GP)
4957-
.Case("sp", Mips::SP)
4958-
.Default(Register());
4959-
return Reg;
5035+
report_fatal_error(
5036+
Twine("Invalid register name \"" + StringRef(RegName) + "\"."));
49605037
}
49615038

49625039
MachineBasicBlock *MipsTargetLowering::emitLDR_W(MachineInstr &MI,

llvm/lib/Target/Mips/MipsISelLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,8 @@ class TargetRegisterClass;
717717
return true;
718718
}
719719

720+
int getCPURegisterIndex(StringRef Name) const;
721+
720722
/// Emit a sign-extension using sll/sra, seb, or seh appropriately.
721723
MachineBasicBlock *emitSignExtendToI32InReg(MachineInstr &MI,
722724
MachineBasicBlock *BB,

0 commit comments

Comments
 (0)