LLVM 24.0.0git
RISCVSubtarget.cpp
Go to the documentation of this file.
1//===-- RISCVSubtarget.cpp - RISC-V Subtarget Information -----------------===//
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 implements the RISC-V specific subclass of TargetSubtargetInfo.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RISCVSubtarget.h"
17#include "RISCV.h"
18#include "RISCVFrameLowering.h"
20#include "RISCVTargetMachine.h"
21#include "llvm/ADT/Statistic.h"
24#include "llvm/MC/MCSchedule.h"
29
30using namespace llvm;
31
32#define DEBUG_TYPE "riscv-macro-fusion"
33
34#define GET_RISCV_MACRO_FUSION_PRED_IMPL
35#include "RISCVGenMacroFusion.inc"
36
37#undef DEBUG_TYPE
38#define DEBUG_TYPE "riscv-subtarget"
39
40#define GET_SUBTARGETINFO_TARGET_DESC
41#define GET_SUBTARGETINFO_CTOR
42#include "RISCVGenSubtargetInfo.inc"
43
44#define OPTIONS_STRUCT_DEFS
45#include "RISCVOptions.inc"
46
48
49#define GET_RISCVTuneInfoTable_IMPL
50#include "RISCVGenSearchableTables.inc"
51} // namespace llvm::RISCVTuneInfoTable
52
53void RISCVSubtarget::anchor() {}
54
56RISCVSubtarget::initializeSubtargetDependencies(const Triple &TT, StringRef CPU,
57 StringRef TuneCPU, StringRef FS,
58 StringRef ABIName) {
59 // Determine default and user-specified characteristics
60 bool Is64Bit = TT.isArch64Bit();
61 if (CPU.empty() || CPU == "generic")
62 CPU = Is64Bit ? "generic-rv64" : "generic-rv32";
63
64 if (TuneCPU.empty())
65 TuneCPU = CPU;
66 if (TuneCPU == "generic")
67 TuneCPU = Is64Bit ? "generic-rv64" : "generic-rv32";
68
69 TuneInfo = RISCVTuneInfoTable::getRISCVTuneInfo(TuneCPU);
70 // If there is no TuneInfo for this CPU, we fail back to generic.
71 if (!TuneInfo)
72 TuneInfo = RISCVTuneInfoTable::getRISCVTuneInfo("generic");
73 assert(TuneInfo && "TuneInfo shouldn't be nullptr!");
74
75 ParseSubtargetFeatures(CPU, TuneCPU, FS);
76
78
79 // Re-sync the flags.
80 HasStdExtZcd = hasFeature(RISCV::FeatureStdExtZcd);
81 HasStdExtZcf = hasFeature(RISCV::FeatureStdExtZcf);
82 HasStdExtZcy = hasFeature(RISCV::FeatureStdExtZcy);
83 HasStdExtC = hasFeature(RISCV::FeatureStdExtC);
84 HasStdExtZce = hasFeature(RISCV::FeatureStdExtZce);
85 HasStdExtZcmp = hasFeature(RISCV::FeatureStdExtZcmp);
86 HasStdExtZcmt = hasFeature(RISCV::FeatureStdExtZcmt);
87
88 // Can't be fatal: per-function subtargets mean this one may just be the
89 // module-level default with no matching function, e.g. -target-abi ilp32f
90 // with no global -mattr=+f but all functions have their own "+f" attribute.
91 if (auto ABIOrErr = RISCVABI::computeTargetABI(*this, ABIName)) {
92 TargetABI = *ABIOrErr;
93 } else {
94 errs() << "note: " << toString(ABIOrErr.takeError())
95 << " (ignoring target-abi)\n";
96 TargetABI = cantFail(RISCVABI::computeTargetABI(*this, ""));
97 }
98 RISCVFeatures::validate(TT, getFeatureBits());
99 return *this;
100}
101
103 StringRef TuneCPU, StringRef FS,
104 StringRef ABIName, unsigned RVVVectorBitsMin,
105 unsigned RVVVectorBitsMax,
106 const TargetMachine &TM)
107 : RISCVGenSubtargetInfo(TT, CPU, TuneCPU, FS),
108 CLOpts(static_cast<const RISCVTargetMachine &>(TM).getCLOpts()),
109 IsLittleEndian(TT.isLittleEndian()), RVVVectorBitsMin(RVVVectorBitsMin),
110 RVVVectorBitsMax(RVVVectorBitsMax),
111 FrameLowering(
112 initializeSubtargetDependencies(TT, CPU, TuneCPU, FS, ABIName)),
113 InstrInfo(*this), TLInfo(TM, *this) {
114 TSInfo = std::make_unique<RISCVSelectionDAGInfo>();
115}
116
118
122
129
135
137 if (!InstSelector) {
139 *static_cast<const RISCVTargetMachine *>(&TLInfo.getTargetMachine()),
140 *this, *getRegBankInfo()));
141 }
142 return InstSelector.get();
143}
144
146 if (!Legalizer)
147 Legalizer.reset(new RISCVLegalizerInfo(*this));
148 return Legalizer.get();
149}
150
152 if (!RegBankInfo)
153 RegBankInfo.reset(new RISCVRegisterBankInfo(getHwMode()));
154 return RegBankInfo.get();
155}
156
158 return CLOpts.constant_pool_for_large_ints;
159}
160
161// Returns true if VT is a P extension packed SIMD type.
163 if (!HasStdExtP)
164 return false;
165
166 // RV32 supports 32-bit and 64-bit vectors. RV64 only support 64-bit vectors.
167 if (!is64Bit() && (VT == MVT::v4i8 || VT == MVT::v2i16))
168 return true;
169
170 return VT == MVT::v8i8 || VT == MVT::v4i16 || VT == MVT::v2i32;
171}
172
173// Returns true if VT is a P extension packed double-wide SIMD type.
175 if (!HasStdExtP || is64Bit())
176 return false;
177
178 return VT == MVT::v8i8 || VT == MVT::v4i16 || VT == MVT::v2i32;
179}
180
182 // Loading integer from constant pool needs two instructions (the reason why
183 // the minimum cost is 2): an address calculation instruction and a load
184 // instruction. Usually, address calculation and instructions used for
185 // building integers (addi, slli, etc.) can be done in one cycle, so here we
186 // set the default cost to (LoadLatency + 1) if no threshold is provided.
187 return CLOpts.max_build_ints_cost
188 ? std::max<unsigned>(2, *CLOpts.max_build_ints_cost)
189 : getLoadLatency() + 1;
190}
191
193 return CLOpts.sched_mispredict_penalty.value_or(
194 getSchedModel().MispredictPenalty);
195}
196
198 return CLOpts.sched_load_latency.value_or(getSchedModel().LoadLatency);
199}
200
203 "Tried to get vector length without Zve or V extension support!");
204
205 // ZvlLen specifies the minimum required vlen. The upper bound provided by
206 // riscv-v-vector-bits-max should be no less than it.
207 if (RVVVectorBitsMax != 0 && RVVVectorBitsMax < ZvlLen)
208 report_fatal_error("riscv-v-vector-bits-max specified is lower "
209 "than the Zvl*b limitation");
210
211 return RVVVectorBitsMax;
212}
213
216 "Tried to get vector length without Zve or V extension support!");
217
218 if (RVVVectorBitsMin == -1U)
219 return ZvlLen;
220
221 // ZvlLen specifies the minimum required vlen. The lower bound provided by
222 // riscv-v-vector-bits-min should be no less than it.
223 if (RVVVectorBitsMin != 0 && RVVVectorBitsMin < ZvlLen)
224 report_fatal_error("riscv-v-vector-bits-min specified is lower "
225 "than the Zvl*b limitation");
226
227 return RVVVectorBitsMin;
228}
229
231 // Disable fixed length vectors when P is enabled to prevent some
232 // crashes.
233 // FIXME: We need better prioritization.
234 return hasVInstructions() && !hasStdExtP() &&
236}
237
238bool RISCVSubtarget::enableSubRegLiveness() const { return true; }
239
241 return getSchedModel().hasInstrSchedModel();
242}
243
245 // We usually compute max call frame size after ISel. Do the computation now
246 // if the .mir file didn't specify it. Note that this will probably give you
247 // bogus values after PEI has eliminated the callframe setup/destroy pseudo
248 // instructions, specify explicitly if you need it to be correct.
249 MachineFrameInfo &MFI = MF.getFrameInfo();
252}
253
254 /// Enable use of alias analysis during code generation (during MI
255 /// scheduling, DAGCombine, etc.).
256bool RISCVSubtarget::useAA() const { return CLOpts.use_aa; }
257
259 return CLOpts.min_jump_table_entries.value_or(
260 TuneInfo->MinimumJumpTableEntries);
261}
262
264 const SchedRegion &Region) const {
265 // Do bidirectional scheduling since it provides a more balanced scheduling
266 // leading to better performance. This will increase compile time.
267 Policy.OnlyTopDown = false;
268 Policy.OnlyBottomUp = false;
269
270 // Disabling the latency heuristic can reduce the number of spills/reloads but
271 // will cause some regressions on some cores.
272 Policy.DisableLatencyHeuristic = DisableLatencySchedHeuristic;
273
274 // Spilling is generally expensive on all RISC-V cores, so always enable
275 // register-pressure tracking. This will increase compile time.
276 Policy.ShouldTrackPressure = true;
277}
278
280 MachineSchedPolicy &Policy, const SchedRegion &Region) const {
281 MISched::Direction PostRASchedDirection = getPostRASchedDirection();
282 if (PostRASchedDirection == MISched::TopDown) {
283 Policy.OnlyTopDown = true;
284 Policy.OnlyBottomUp = false;
285 } else if (PostRASchedDirection == MISched::BottomUp) {
286 Policy.OnlyTopDown = false;
287 Policy.OnlyBottomUp = true;
288 } else if (PostRASchedDirection == MISched::Bidirectional) {
289 Policy.OnlyTopDown = false;
290 Policy.OnlyBottomUp = false;
291 }
292}
293
295 return CLOpts.mips_load_store_pairs && HasVendorXMIPSLSP;
296}
297
299 return CLOpts.mips_ccmov && HasVendorXMIPSCMov;
300}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static bool hasFeature(StringRef Feature, const FeatureBitset &FeatureBits, ArrayRef< SubtargetFeatureKV > ProcFeatures)
This file describes how to lower LLVM calls to machine code calls.
This file describes how to lower LLVM inline asm to machine code INLINEASM.
This file declares the targeting of the Machinelegalizer class for RISC-V.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
Machine Value Type.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI void computeMaxCallFrameSize(MachineFunction &MF, std::vector< MachineBasicBlock::iterator > *FrameSDOps=nullptr)
Computes the maximum size of a callframe.
bool isMaxCallFrameSizeComputed() const
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
This class provides the information for the target register banks.
unsigned getMinimumJumpTableEntries() const
const LegalizerInfo * getLegalizerInfo() const override
void overrideSchedPolicy(MachineSchedPolicy &Policy, const SchedRegion &Region) const override
std::unique_ptr< LegalizerInfo > Legalizer
bool isPExtPackedDoubleType(MVT VT) const
bool useMIPSLoadStorePairs() const
const InlineAsmLowering * getInlineAsmLowering() const override
bool useRVVForFixedLengthVectors() const
MISched::Direction getPostRASchedDirection() const
std::unique_ptr< InlineAsmLowering > InlineAsmLoweringInfo
const RISCVOptions & getCLOpts() const
bool isPExtPackedType(MVT VT) const
unsigned getLoadLatency() const override
unsigned getMinRVVVectorSizeInBits() const
std::unique_ptr< InstructionSelector > InstSelector
RISCVSubtarget(const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS, StringRef ABIName, unsigned RVVVectorBitsMin, unsigned RVVVectorLMULMax, const TargetMachine &TM)
bool useMIPSCCMovInsn() const
unsigned getMispredictionPenalty() const override
const RISCVRegisterBankInfo * getRegBankInfo() const override
const CallLowering * getCallLowering() const override
InstructionSelector * getInstructionSelector() const override
unsigned getMaxBuildIntsCost() const
std::unique_ptr< const SelectionDAGTargetInfo > TSInfo
bool hasVInstructions() const
bool useAA() const override
Enable use of alias analysis during code generation (during MI scheduling, DAGCombine,...
bool enableMachinePipeliner() const override
bool useConstantPoolForLargeInts() const
bool isLittleEndian() const
~RISCVSubtarget() override
unsigned getMaxRVVVectorSizeInBits() const
void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS)
std::unique_ptr< RISCVRegisterBankInfo > RegBankInfo
void mirFileLoaded(MachineFunction &MF) const override
std::unique_ptr< CallLowering > CallLoweringInfo
const RISCVTargetLowering * getTargetLowering() const override
void overridePostRASchedPolicy(MachineSchedPolicy &Policy, const SchedRegion &Region) const override
bool enableSubRegLiveness() const override
const SelectionDAGTargetInfo * getSelectionDAGInfo() const override
Targets can subclass this to parameterize the SelectionDAG lowering and instruction selection process...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
Primary interface to the complete machine description for the target machine.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Expected< ABI > computeTargetABI(const MCSubtargetInfo &STI, StringRef ABIName)
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
void updateCZceFeatureImplications(MCSubtargetInfo &STI)
static constexpr unsigned RVVBitsPerBlock
This is an optimization pass for GlobalISel generic memory operations.
InstructionSelector * createRISCVInstructionSelector(const RISCVTargetMachine &TM, const RISCVSubtarget &Subtarget, const RISCVRegisterBankInfo &RBI)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
Define a generic scheduling policy for targets that don't provide their own MachineSchedStrategy.
A region of an MBB for scheduling.