[llvm] b97911c - [LoongArch] Implement the TargetLowering::getRegisterByName hook
via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 15 00:23:50 PST 2022
Author: gonglingqin
Date: 2022-11-15T16:10:32+08:00
New Revision: b97911c37b84b3591875de813941616c278c429b
URL: https://github.com/llvm/llvm-project/commit/b97911c37b84b3591875de813941616c278c429b
DIFF: https://github.com/llvm/llvm-project/commit/b97911c37b84b3591875de813941616c278c429b.diff
LOG: [LoongArch] Implement the TargetLowering::getRegisterByName hook
Only reserved registers can be read.
Differential Revision: https://reviews.llvm.org/D137532
Added:
llvm/test/CodeGen/LoongArch/get-reg.ll
Modified:
llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
llvm/lib/Target/LoongArch/LoongArchISelLowering.h
Removed:
################################################################################
diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
index 348f5cdf09a27..f0386949f611d 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
@@ -2457,3 +2457,24 @@ void LoongArchTargetLowering::LowerAsmOperandForConstraint(
}
TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
}
+
+#define GET_REGISTER_MATCHER
+#include "LoongArchGenAsmMatcher.inc"
+
+Register
+LoongArchTargetLowering::getRegisterByName(const char *RegName, LLT VT,
+ const MachineFunction &MF) const {
+ std::pair<StringRef, StringRef> Name = StringRef(RegName).split('$');
+ std::string NewRegName = Name.second.str();
+ Register Reg = MatchRegisterAltName(NewRegName);
+ if (Reg == LoongArch::NoRegister)
+ Reg = MatchRegisterName(NewRegName);
+ if (Reg == LoongArch::NoRegister)
+ report_fatal_error(
+ Twine("Invalid register name \"" + StringRef(RegName) + "\"."));
+ BitVector ReservedRegs = Subtarget.getRegisterInfo()->getReservedRegs(MF);
+ if (!ReservedRegs.test(Reg))
+ report_fatal_error(Twine("Trying to obtain non-reserved register \"" +
+ StringRef(RegName) + "\"."));
+ return Reg;
+}
diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.h b/llvm/lib/Target/LoongArch/LoongArchISelLowering.h
index 3cb9999ed982a..4b7bf9d9c6994 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.h
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.h
@@ -140,6 +140,9 @@ class LoongArchTargetLowering : public TargetLowering {
return ISD::SIGN_EXTEND;
}
+ Register getRegisterByName(const char *RegName, LLT VT,
+ const MachineFunction &MF) const override;
+
private:
/// Target-specific function used to lower LoongArch calling conventions.
typedef bool LoongArchCCAssignFn(const DataLayout &DL, LoongArchABI::ABI ABI,
diff --git a/llvm/test/CodeGen/LoongArch/get-reg.ll b/llvm/test/CodeGen/LoongArch/get-reg.ll
new file mode 100644
index 0000000000000..323030da9e7f3
--- /dev/null
+++ b/llvm/test/CodeGen/LoongArch/get-reg.ll
@@ -0,0 +1,27 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s --mtriple=loongarch64 | FileCheck %s
+
+define i64 @get_stack() nounwind {
+; CHECK-LABEL: get_stack:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: move $a0, $sp
+; CHECK-NEXT: ret
+entry:
+ %sp = call i64 @llvm.read_register.i64(metadata !0)
+ ret i64 %sp
+}
+
+define void @set_stack(i64 %val) nounwind {
+; CHECK-LABEL: set_stack:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: move $sp, $a0
+; CHECK-NEXT: ret
+entry:
+ call void @llvm.write_register.i64(metadata !0, i64 %val)
+ ret void
+}
+
+declare i64 @llvm.read_register.i64(metadata) nounwind
+declare void @llvm.write_register.i64(metadata, i64) nounwind
+
+!0 = !{!"$sp\00"}
More information about the llvm-commits
mailing list