1 //===-- RISCVSubtarget.h - Define Subtarget for the RISCV -------*- 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 declares the RISCV specific subclass of TargetSubtargetInfo. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_LIB_TARGET_RISCV_RISCVSUBTARGET_H 14 #define LLVM_LIB_TARGET_RISCV_RISCVSUBTARGET_H 15 16 #include "MCTargetDesc/RISCVBaseInfo.h" 17 #include "RISCVFrameLowering.h" 18 #include "RISCVISelLowering.h" 19 #include "RISCVInstrInfo.h" 20 #include "llvm/CodeGen/GlobalISel/CallLowering.h" 21 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" 22 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h" 23 #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h" 24 #include "llvm/CodeGen/SelectionDAGTargetInfo.h" 25 #include "llvm/CodeGen/TargetSubtargetInfo.h" 26 #include "llvm/IR/DataLayout.h" 27 #include "llvm/Target/TargetMachine.h" 28 29 #define GET_SUBTARGETINFO_HEADER 30 #include "RISCVGenSubtargetInfo.inc" 31 32 namespace llvm { 33 class StringRef; 34 35 class RISCVSubtarget : public RISCVGenSubtargetInfo { 36 public: 37 enum ExtZvl : unsigned { 38 NotSet = 0, 39 Zvl32b = 32, 40 Zvl64b = 64, 41 Zvl128b = 128, 42 Zvl256b = 256, 43 Zvl512b = 512, 44 Zvl1024b = 1024, 45 Zvl2048b = 2048, 46 Zvl4096b = 4096, 47 Zvl8192b = 8192, 48 Zvl16384b = 16384, 49 Zvl32768b = 32768, 50 Zvl65536b = 65536 51 }; 52 53 enum RISCVProcFamilyEnum : uint8_t { 54 Others, 55 SiFive7, 56 }; 57 58 private: 59 virtual void anchor(); 60 61 RISCVProcFamilyEnum RISCVProcFamily = Others; 62 63 bool HasStdExtM = false; 64 bool HasStdExtA = false; 65 bool HasStdExtF = false; 66 bool HasStdExtD = false; 67 bool HasStdExtC = false; 68 bool HasStdExtZba = false; 69 bool HasStdExtZbb = false; 70 bool HasStdExtZbc = false; 71 bool HasStdExtZbe = false; 72 bool HasStdExtZbf = false; 73 bool HasStdExtZbm = false; 74 bool HasStdExtZbp = false; 75 bool HasStdExtZbr = false; 76 bool HasStdExtZbs = false; 77 bool HasStdExtZbt = false; 78 bool HasStdExtV = false; 79 bool HasStdExtZve32x = false; 80 bool HasStdExtZve32f = false; 81 bool HasStdExtZve64x = false; 82 bool HasStdExtZve64f = false; 83 bool HasStdExtZve64d = false; 84 bool HasStdExtZfhmin = false; 85 bool HasStdExtZfh = false; 86 bool HasStdExtZbkb = false; 87 bool HasStdExtZbkc = false; 88 bool HasStdExtZbkx = false; 89 bool HasStdExtZknd = false; 90 bool HasStdExtZkne = false; 91 bool HasStdExtZknh = false; 92 bool HasStdExtZksed = false; 93 bool HasStdExtZksh = false; 94 bool HasStdExtZkr = false; 95 bool HasStdExtZkn = false; 96 bool HasStdExtZks = false; 97 bool HasStdExtZkt = false; 98 bool HasStdExtZk = false; 99 bool HasRV64 = false; 100 bool IsRV32E = false; 101 bool EnableLinkerRelax = false; 102 bool EnableRVCHintInstrs = true; 103 bool EnableSaveRestore = false; 104 unsigned XLen = 32; 105 ExtZvl ZvlLen = ExtZvl::NotSet; 106 MVT XLenVT = MVT::i32; 107 uint8_t MaxInterleaveFactor = 2; 108 RISCVABI::ABI TargetABI = RISCVABI::ABI_Unknown; 109 BitVector UserReservedRegister; 110 RISCVFrameLowering FrameLowering; 111 RISCVInstrInfo InstrInfo; 112 RISCVRegisterInfo RegInfo; 113 RISCVTargetLowering TLInfo; 114 SelectionDAGTargetInfo TSInfo; 115 116 /// Initializes using the passed in CPU and feature strings so that we can 117 /// use initializer lists for subtarget initialization. 118 RISCVSubtarget &initializeSubtargetDependencies(const Triple &TT, 119 StringRef CPU, 120 StringRef TuneCPU, 121 StringRef FS, 122 StringRef ABIName); 123 124 public: 125 // Initializes the data members to match that of the specified triple. 126 RISCVSubtarget(const Triple &TT, StringRef CPU, StringRef TuneCPU, 127 StringRef FS, StringRef ABIName, const TargetMachine &TM); 128 129 // Parses features string setting specified subtarget options. The 130 // definition of this function is auto-generated by tblgen. 131 void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS); 132 133 const RISCVFrameLowering *getFrameLowering() const override { 134 return &FrameLowering; 135 } 136 const RISCVInstrInfo *getInstrInfo() const override { return &InstrInfo; } 137 const RISCVRegisterInfo *getRegisterInfo() const override { 138 return &RegInfo; 139 } 140 const RISCVTargetLowering *getTargetLowering() const override { 141 return &TLInfo; 142 } 143 const SelectionDAGTargetInfo *getSelectionDAGInfo() const override { 144 return &TSInfo; 145 } 146 bool enableMachineScheduler() const override { return true; } 147 148 /// Returns RISCV processor family. 149 /// Avoid this function! CPU specifics should be kept local to this class 150 /// and preferably modeled with SubtargetFeatures or properties in 151 /// initializeProperties(). 152 RISCVProcFamilyEnum getProcFamily() const { return RISCVProcFamily; } 153 154 bool hasStdExtM() const { return HasStdExtM; } 155 bool hasStdExtA() const { return HasStdExtA; } 156 bool hasStdExtF() const { return HasStdExtF; } 157 bool hasStdExtD() const { return HasStdExtD; } 158 bool hasStdExtC() const { return HasStdExtC; } 159 bool hasStdExtV() const { return HasStdExtV; } 160 bool hasStdExtZba() const { return HasStdExtZba; } 161 bool hasStdExtZbb() const { return HasStdExtZbb; } 162 bool hasStdExtZbc() const { return HasStdExtZbc; } 163 bool hasStdExtZbe() const { return HasStdExtZbe; } 164 bool hasStdExtZbf() const { return HasStdExtZbf; } 165 bool hasStdExtZbm() const { return HasStdExtZbm; } 166 bool hasStdExtZbp() const { return HasStdExtZbp; } 167 bool hasStdExtZbr() const { return HasStdExtZbr; } 168 bool hasStdExtZbs() const { return HasStdExtZbs; } 169 bool hasStdExtZbt() const { return HasStdExtZbt; } 170 bool hasStdExtZvl() const { return ZvlLen != ExtZvl::NotSet; } 171 bool hasStdExtZfhmin() const { return HasStdExtZfhmin; } 172 bool hasStdExtZfh() const { return HasStdExtZfh; } 173 bool hasStdExtZbkb() const { return HasStdExtZbkb; } 174 bool hasStdExtZbkc() const { return HasStdExtZbkc; } 175 bool hasStdExtZbkx() const { return HasStdExtZbkx; } 176 bool hasStdExtZknd() const { return HasStdExtZknd; } 177 bool hasStdExtZkne() const { return HasStdExtZkne; } 178 bool hasStdExtZknh() const { return HasStdExtZknh; } 179 bool hasStdExtZksed() const { return HasStdExtZksed; } 180 bool hasStdExtZksh() const { return HasStdExtZksh; } 181 bool hasStdExtZkr() const { return HasStdExtZkr; } 182 bool is64Bit() const { return HasRV64; } 183 bool isRV32E() const { return IsRV32E; } 184 bool enableLinkerRelax() const { return EnableLinkerRelax; } 185 bool enableRVCHintInstrs() const { return EnableRVCHintInstrs; } 186 bool enableSaveRestore() const { return EnableSaveRestore; } 187 MVT getXLenVT() const { return XLenVT; } 188 unsigned getXLen() const { return XLen; } 189 unsigned getFLen() const { 190 if (HasStdExtD) 191 return 64; 192 193 if (HasStdExtF) 194 return 32; 195 196 return 0; 197 } 198 unsigned getMinVLen() const { return ZvlLen; } 199 RISCVABI::ABI getTargetABI() const { return TargetABI; } 200 bool isRegisterReservedByUser(Register i) const { 201 assert(i < RISCV::NUM_TARGET_REGS && "Register out of range"); 202 return UserReservedRegister[i]; 203 } 204 205 // Vector codegen related methods. 206 bool hasVInstructions() const { return HasStdExtV || HasStdExtZve32x; } 207 bool hasVInstructionsI64() const { return HasStdExtV || HasStdExtZve64x; } 208 bool hasVInstructionsF16() const { 209 return (HasStdExtV || HasStdExtZve32f) && HasStdExtZfh; 210 } 211 // FIXME: Consider Zfinx in the future 212 bool hasVInstructionsF32() const { 213 return HasStdExtV || (HasStdExtZve32f && HasStdExtF); 214 } 215 // FIXME: Consider Zdinx in the future 216 bool hasVInstructionsF64() const { 217 return HasStdExtV || (HasStdExtZve64d && HasStdExtD); 218 } 219 // F16 and F64 both require F32. 220 bool hasVInstructionsAnyF() const { return hasVInstructionsF32(); } 221 unsigned getMaxInterleaveFactor() const { 222 return hasVInstructions() ? MaxInterleaveFactor : 1; 223 } 224 225 protected: 226 // GlobalISel related APIs. 227 std::unique_ptr<CallLowering> CallLoweringInfo; 228 std::unique_ptr<InstructionSelector> InstSelector; 229 std::unique_ptr<LegalizerInfo> Legalizer; 230 std::unique_ptr<RegisterBankInfo> RegBankInfo; 231 232 public: 233 const CallLowering *getCallLowering() const override; 234 InstructionSelector *getInstructionSelector() const override; 235 const LegalizerInfo *getLegalizerInfo() const override; 236 const RegisterBankInfo *getRegBankInfo() const override; 237 238 bool useConstantPoolForLargeInts() const; 239 240 // Maximum cost used for building integers, integers will be put into constant 241 // pool if exceeded. 242 unsigned getMaxBuildIntsCost() const; 243 244 // Return the known range for the bit length of RVV data registers. A value 245 // of 0 means nothing is known about that particular limit beyond what's 246 // implied by the architecture. 247 unsigned getMaxRVVVectorSizeInBits() const; 248 unsigned getMinRVVVectorSizeInBits() const; 249 unsigned getMaxLMULForFixedLengthVectors() const; 250 unsigned getMaxELENForFixedLengthVectors() const; 251 bool useRVVForFixedLengthVectors() const; 252 }; 253 } // End llvm namespace 254 255 #endif 256