xref: /aosp_15_r20/external/swiftshader/third_party/llvm-10.0/llvm/lib/Target/RISCV/Utils/RISCVBaseInfo.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 #include "RISCVBaseInfo.h"
2 #include "llvm/ADT/ArrayRef.h"
3 #include "llvm/ADT/Triple.h"
4 #include "llvm/Support/raw_ostream.h"
5 
6 namespace llvm {
7 namespace RISCVSysReg {
8 #define GET_SysRegsList_IMPL
9 #include "RISCVGenSystemOperands.inc"
10 } // namespace RISCVSysReg
11 
12 namespace RISCVABI {
computeTargetABI(const Triple & TT,FeatureBitset FeatureBits,StringRef ABIName)13 ABI computeTargetABI(const Triple &TT, FeatureBitset FeatureBits,
14                      StringRef ABIName) {
15   auto TargetABI = getTargetABI(ABIName);
16   bool IsRV64 = TT.isArch64Bit();
17   bool IsRV32E = FeatureBits[RISCV::FeatureRV32E];
18 
19   if (!ABIName.empty() && TargetABI == ABI_Unknown) {
20     errs()
21         << "'" << ABIName
22         << "' is not a recognized ABI for this target (ignoring target-abi)\n";
23   } else if (ABIName.startswith("ilp32") && IsRV64) {
24     errs() << "32-bit ABIs are not supported for 64-bit targets (ignoring "
25               "target-abi)\n";
26     TargetABI = ABI_Unknown;
27   } else if (ABIName.startswith("lp64") && !IsRV64) {
28     errs() << "64-bit ABIs are not supported for 32-bit targets (ignoring "
29               "target-abi)\n";
30     TargetABI = ABI_Unknown;
31   } else if (IsRV32E && TargetABI != ABI_ILP32E && TargetABI != ABI_Unknown) {
32     // TODO: move this checking to RISCVTargetLowering and RISCVAsmParser
33     errs()
34         << "Only the ilp32e ABI is supported for RV32E (ignoring target-abi)\n";
35     TargetABI = ABI_Unknown;
36   }
37 
38   if (TargetABI != ABI_Unknown)
39     return TargetABI;
40 
41   // For now, default to the ilp32/ilp32e/lp64 ABI if no explicit ABI is given
42   // or an invalid/unrecognised string is given. In the future, it might be
43   // worth changing this to default to ilp32f/lp64f and ilp32d/lp64d when
44   // hardware support for floating point is present.
45   if (IsRV32E)
46     return ABI_ILP32E;
47   if (IsRV64)
48     return ABI_LP64;
49   return ABI_ILP32;
50 }
51 
getTargetABI(StringRef ABIName)52 ABI getTargetABI(StringRef ABIName) {
53   auto TargetABI = StringSwitch<ABI>(ABIName)
54                        .Case("ilp32", ABI_ILP32)
55                        .Case("ilp32f", ABI_ILP32F)
56                        .Case("ilp32d", ABI_ILP32D)
57                        .Case("ilp32e", ABI_ILP32E)
58                        .Case("lp64", ABI_LP64)
59                        .Case("lp64f", ABI_LP64F)
60                        .Case("lp64d", ABI_LP64D)
61                        .Default(ABI_Unknown);
62   return TargetABI;
63 }
64 
65 // To avoid the BP value clobbered by a function call, we need to choose a
66 // callee saved register to save the value. RV32E only has X8 and X9 as callee
67 // saved registers and X8 will be used as fp. So we choose X9 as bp.
getBPReg()68 Register getBPReg() { return RISCV::X9; }
69 
70 } // namespace RISCVABI
71 
72 namespace RISCVFeatures {
73 
validate(const Triple & TT,const FeatureBitset & FeatureBits)74 void validate(const Triple &TT, const FeatureBitset &FeatureBits) {
75   if (TT.isArch64Bit() && FeatureBits[RISCV::FeatureRV32E])
76     report_fatal_error("RV32E can't be enabled for an RV64 target");
77 }
78 
79 } // namespace RISCVFeatures
80 
81 } // namespace llvm
82