LLVM 24.0.0git
RISCVLegalizerInfo.cpp
Go to the documentation of this file.
1//===-- RISCVLegalizerInfo.cpp ----------------------------------*- 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/// \file
9/// This file implements the targeting of the Machinelegalizer class for RISC-V.
10/// \todo This should be generated by TableGen.
11//===----------------------------------------------------------------------===//
12
13#include "RISCVLegalizerInfo.h"
16#include "RISCVSubtarget.h"
30#include "llvm/IR/Intrinsics.h"
31#include "llvm/IR/IntrinsicsRISCV.h"
32#include "llvm/IR/Type.h"
33
34using namespace llvm;
35using namespace LegalityPredicates;
36using namespace LegalizeMutations;
37using namespace MIPatternMatch;
38
40typeIsLegalIntOrFPVec(unsigned TypeIdx,
41 std::initializer_list<LLT> IntOrFPVecTys,
42 const RISCVSubtarget &ST) {
43 LegalityPredicate P = [=, &ST](const LegalityQuery &Query) {
44 return ST.hasVInstructions() &&
45 (Query.Types[TypeIdx].getScalarSizeInBits() != 64 ||
46 ST.hasVInstructionsI64()) &&
47 (Query.Types[TypeIdx].getElementCount().getKnownMinValue() != 1 ||
48 ST.getELen() == 64);
49 };
50
51 return all(typeInSet(TypeIdx, IntOrFPVecTys), P);
52}
53
55typeIsLegalBoolVec(unsigned TypeIdx, std::initializer_list<LLT> BoolVecTys,
56 const RISCVSubtarget &ST) {
57 LegalityPredicate P = [=, &ST](const LegalityQuery &Query) {
58 return ST.hasVInstructions() &&
59 (Query.Types[TypeIdx].getElementCount().getKnownMinValue() != 1 ||
60 ST.getELen() == 64);
61 };
62 return all(typeInSet(TypeIdx, BoolVecTys), P);
63}
64
65static LegalityPredicate typeIsLegalPtrVec(unsigned TypeIdx,
66 std::initializer_list<LLT> PtrVecTys,
67 const RISCVSubtarget &ST) {
68 LegalityPredicate P = [=, &ST](const LegalityQuery &Query) {
69 return ST.hasVInstructions() &&
70 (Query.Types[TypeIdx].getElementCount().getKnownMinValue() != 1 ||
71 ST.getELen() == 64) &&
72 (Query.Types[TypeIdx].getElementCount().getKnownMinValue() != 16 ||
73 Query.Types[TypeIdx].getScalarSizeInBits() == 32);
74 };
75 return all(typeInSet(TypeIdx, PtrVecTys), P);
76}
77
79 : STI(ST), XLen(STI.getXLen()), sXLen(LLT::scalar(XLen)),
80 iXLen(LLT::integer(XLen)) {
81 const LLT sDoubleXLen = LLT::scalar(2 * XLen);
82 const LLT p0 = LLT::pointer(0, XLen);
83 const LLT s1 = LLT::scalar(1);
84 const LLT s8 = LLT::scalar(8);
85 const LLT s16 = LLT::scalar(16);
86 const LLT f16 = LLT::float16();
87 const LLT f32 = LLT::float32();
88 const LLT f64 = LLT::float64();
89 const LLT f128 = LLT::float128();
90 const LLT s32 = LLT::scalar(32);
91 const LLT s64 = LLT::scalar(64);
92 const LLT s128 = LLT::scalar(128);
93
94 const LLT nxv1s1 = LLT::scalable_vector(1, s1);
95 const LLT nxv2s1 = LLT::scalable_vector(2, s1);
96 const LLT nxv4s1 = LLT::scalable_vector(4, s1);
97 const LLT nxv8s1 = LLT::scalable_vector(8, s1);
98 const LLT nxv16s1 = LLT::scalable_vector(16, s1);
99 const LLT nxv32s1 = LLT::scalable_vector(32, s1);
100 const LLT nxv64s1 = LLT::scalable_vector(64, s1);
101
102 const LLT nxv1s8 = LLT::scalable_vector(1, s8);
103 const LLT nxv2s8 = LLT::scalable_vector(2, s8);
104 const LLT nxv4s8 = LLT::scalable_vector(4, s8);
105 const LLT nxv8s8 = LLT::scalable_vector(8, s8);
106 const LLT nxv16s8 = LLT::scalable_vector(16, s8);
107 const LLT nxv32s8 = LLT::scalable_vector(32, s8);
108 const LLT nxv64s8 = LLT::scalable_vector(64, s8);
109
110 const LLT nxv1s16 = LLT::scalable_vector(1, s16);
111 const LLT nxv2s16 = LLT::scalable_vector(2, s16);
112 const LLT nxv4s16 = LLT::scalable_vector(4, s16);
113 const LLT nxv8s16 = LLT::scalable_vector(8, s16);
114 const LLT nxv16s16 = LLT::scalable_vector(16, s16);
115 const LLT nxv32s16 = LLT::scalable_vector(32, s16);
116
117 const LLT nxv1s32 = LLT::scalable_vector(1, s32);
118 const LLT nxv2s32 = LLT::scalable_vector(2, s32);
119 const LLT nxv4s32 = LLT::scalable_vector(4, s32);
120 const LLT nxv8s32 = LLT::scalable_vector(8, s32);
121 const LLT nxv16s32 = LLT::scalable_vector(16, s32);
122
123 const LLT nxv1s64 = LLT::scalable_vector(1, s64);
124 const LLT nxv2s64 = LLT::scalable_vector(2, s64);
125 const LLT nxv4s64 = LLT::scalable_vector(4, s64);
126 const LLT nxv8s64 = LLT::scalable_vector(8, s64);
127
128 const LLT nxv1p0 = LLT::scalable_vector(1, p0);
129 const LLT nxv2p0 = LLT::scalable_vector(2, p0);
130 const LLT nxv4p0 = LLT::scalable_vector(4, p0);
131 const LLT nxv8p0 = LLT::scalable_vector(8, p0);
132 const LLT nxv16p0 = LLT::scalable_vector(16, p0);
133
134 using namespace TargetOpcode;
135
136 auto BoolVecTys = {nxv1s1, nxv2s1, nxv4s1, nxv8s1, nxv16s1, nxv32s1, nxv64s1};
137
138 auto IntOrFPVecTys = {nxv1s8, nxv2s8, nxv4s8, nxv8s8, nxv16s8, nxv32s8,
139 nxv64s8, nxv1s16, nxv2s16, nxv4s16, nxv8s16, nxv16s16,
140 nxv32s16, nxv1s32, nxv2s32, nxv4s32, nxv8s32, nxv16s32,
141 nxv1s64, nxv2s64, nxv4s64, nxv8s64};
142
143 auto PtrVecTys = {nxv1p0, nxv2p0, nxv4p0, nxv8p0, nxv16p0};
144
145 getActionDefinitionsBuilder({G_ADD, G_SUB})
146 .legalFor({sXLen})
147 .legalIf(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST))
148 .customFor(ST.is64Bit(), {s32})
150 .clampScalar(0, sXLen, sXLen);
151
152 getActionDefinitionsBuilder({G_AND, G_OR, G_XOR})
153 .legalFor({sXLen})
154 .legalIf(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST))
156 .clampScalar(0, sXLen, sXLen);
157
159 {G_UADDE, G_UADDO, G_USUBE, G_USUBO, G_READ_REGISTER, G_WRITE_REGISTER})
160 .lower();
161
162 getActionDefinitionsBuilder({G_SADDE, G_SADDO, G_SSUBE, G_SSUBO})
163 .minScalar(0, sXLen)
164 .lower();
165
166 // TODO: Use Vector Single-Width Saturating Instructions for vector types.
168 {G_UADDSAT, G_SADDSAT, G_USUBSAT, G_SSUBSAT, G_SSHLSAT, G_USHLSAT})
169 .lower();
170
171 getActionDefinitionsBuilder({G_SHL, G_ASHR, G_LSHR})
172 .legalFor({{sXLen, sXLen}})
173 .customFor(ST.is64Bit(), {{s32, s32}})
174 .widenScalarToNextPow2(0)
175 .clampScalar(1, sXLen, sXLen)
176 .clampScalar(0, sXLen, sXLen);
177
178 getActionDefinitionsBuilder({G_ZEXT, G_SEXT, G_ANYEXT})
179 .legalFor({{s32, s16}})
180 .legalFor(ST.is64Bit(), {{s64, s16}, {s64, s32}})
181 .legalIf(all(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST),
182 typeIsLegalIntOrFPVec(1, IntOrFPVecTys, ST)))
183 .customIf(typeIsLegalBoolVec(1, BoolVecTys, ST))
184 .maxScalar(0, sXLen);
185
186 getActionDefinitionsBuilder(G_TRUNC).alwaysLegal();
187
188 {
189 LegalityPredicate ValidSextInRegWidth = all(sizeIs(0, 64), immIs(0, 32));
190
191 if (STI.hasStdExtZbb())
192 ValidSextInRegWidth =
193 LegalityPredicates::any(ValidSextInRegWidth, immInSet(0, {8, 16}));
194
195 getActionDefinitionsBuilder(G_SEXT_INREG)
196 .legalIf(all(typeIs(0, sXLen), ValidSextInRegWidth))
197 .clampScalar(0, sXLen, sXLen)
198 .lower();
199 }
200
201 // Merge/Unmerge
202 for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) {
203 auto &MergeUnmergeActions = getActionDefinitionsBuilder(Op);
204 unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1;
205 unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0;
206 if (XLen == 32 && (ST.hasStdExtD() || ST.hasStdExtZilsd())) {
207 MergeUnmergeActions.legalIf(
208 all(typeIs(BigTyIdx, s64), typeIs(LitTyIdx, s32)));
209 }
210 MergeUnmergeActions.widenScalarToNextPow2(LitTyIdx, XLen)
211 .widenScalarToNextPow2(BigTyIdx, XLen)
212 .clampScalar(LitTyIdx, sXLen, sXLen)
213 .clampScalar(BigTyIdx, sXLen, sXLen);
214 }
215
216 getActionDefinitionsBuilder({G_FSHL, G_FSHR}).lower();
217
218 getActionDefinitionsBuilder({G_ROTR, G_ROTL})
219 .legalFor(ST.hasStdExtZbb() || ST.hasStdExtZbkb(), {{sXLen, sXLen}})
220 .customFor(ST.is64Bit() && (ST.hasStdExtZbb() || ST.hasStdExtZbkb()),
221 {{s32, s32}})
222 .lower();
223
224 getActionDefinitionsBuilder(G_BITREVERSE)
225 .customFor(ST.hasStdExtZbkb(), {s8})
226 .lowerIf(scalarNarrowerThan(0, 8))
227 .widenScalarToNextPow2(0)
228 .maxScalar(0, sXLen)
229 .lower();
230
231 getActionDefinitionsBuilder(G_BITCAST)
232 .legalIf(all(
234 typeIsLegalBoolVec(0, BoolVecTys, ST)),
236 typeIsLegalBoolVec(1, BoolVecTys, ST))))
237 // Scalar int <-> fp bitcasts that fit in a GPR are register moves.
238 .legalIf(all(isScalar(0), isScalar(1), scalarNarrowerThan(0, XLen + 1)))
239 // Wider ones (i128/f128, or f64/i64 on RV32) are split into XLen pieces.
240 .narrowScalarIf(all(isScalar(0), isScalar(1), scalarWiderThan(0, XLen)),
242
243 auto &BSWAPActions = getActionDefinitionsBuilder(G_BSWAP);
244 if (ST.hasStdExtZbb() || ST.hasStdExtZbkb())
245 BSWAPActions.legalFor({sXLen}).clampScalar(0, sXLen, sXLen);
246 else
247 BSWAPActions.maxScalar(0, sXLen).lower();
248
249 getActionDefinitionsBuilder(G_CLMUL)
250 .legalFor(ST.hasStdExtZbkc(), {sXLen})
251 .unsupported();
252
253 getActionDefinitionsBuilder(G_CLMULH)
254 .legalFor(ST.hasStdExtZbkc(), {sXLen})
255 .customFor(ST.is64Bit() && ST.hasStdExtZbkc(), {s32})
256 .unsupported();
257
258 // CLMULR is Zbc-only; Zbkc is a subset that has CLMUL/CLMULH but not CLMULR.
259 getActionDefinitionsBuilder(G_CLMULR)
260 .legalFor(ST.hasStdExtZbc(), {sXLen})
261 .customFor(ST.is64Bit() && ST.hasStdExtZbc(), {s32})
262 .unsupported();
263
264 auto &CTLZPoisonActions = getActionDefinitionsBuilder(G_CTLZ_ZERO_POISON);
265 auto &CTTZPoisonActions = getActionDefinitionsBuilder(G_CTTZ_ZERO_POISON);
266 if (ST.hasStdExtZbb()) {
267 getActionDefinitionsBuilder({G_CTLZ, G_CTTZ})
268 .legalFor({{sXLen, sXLen}})
269 .customFor({{s32, s32}})
270 .clampScalar(0, s32, sXLen)
271 .widenScalarToNextPow2(0)
272 .scalarSameSizeAs(1, 0);
273 } else {
274 auto &CTLZActions = getActionDefinitionsBuilder(G_CTLZ);
275 if (ST.hasVendorXTHeadBb())
276 CTLZActions.legalFor({{sXLen, sXLen}})
277 .clampScalar(0, sXLen, sXLen)
278 .scalarSameSizeAs(1, 0);
279 else
280 CTLZActions.maxScalar(0, sXLen).scalarSameSizeAs(1, 0).lower();
281
282 auto &CTTZActions = getActionDefinitionsBuilder(G_CTTZ);
283 if (ST.hasVendorXTHeadBb()) {
284 CTTZActions.clampScalar(0, sXLen, sXLen).scalarSameSizeAs(1, 0).lower();
285 CTLZPoisonActions.clampScalar(0, sXLen, sXLen).scalarSameSizeAs(1, 0);
286 CTTZPoisonActions.clampScalar(0, sXLen, sXLen).scalarSameSizeAs(1, 0);
287 } else {
288 CTTZActions.maxScalar(0, sXLen).scalarSameSizeAs(1, 0).lower();
289 CTLZPoisonActions.maxScalar(0, sXLen).scalarSameSizeAs(1, 0);
290 CTTZPoisonActions.maxScalar(0, sXLen).scalarSameSizeAs(1, 0);
291 }
292 }
293 CTLZPoisonActions.lower();
294 CTTZPoisonActions.lower();
295
296 auto &CountSignActions = getActionDefinitionsBuilder(G_CTLS);
297 if (ST.hasStdExtP()) {
298 CountSignActions.legalFor({{sXLen, sXLen}})
299 .customFor({{s32, s32}})
300 .clampScalar(0, s32, sXLen)
301 .widenScalarToNextPow2(0)
302 .scalarSameSizeAs(1, 0);
303 } else {
304 CountSignActions.maxScalar(0, sXLen).scalarSameSizeAs(1, 0).lower();
305 }
306
307 auto &CTPOPActions = getActionDefinitionsBuilder(G_CTPOP);
308 if (ST.hasStdExtZbb()) {
309 CTPOPActions.legalFor({{sXLen, sXLen}})
310 .clampScalar(0, sXLen, sXLen)
311 .scalarSameSizeAs(1, 0);
312 } else {
313 CTPOPActions.widenScalarToNextPow2(0, /*Min*/ 8)
314 .clampScalar(0, s8, sXLen)
315 .scalarSameSizeAs(1, 0)
316 .lower();
317 }
318
319 getActionDefinitionsBuilder(G_CONSTANT)
320 .legalFor({p0})
321 .legalFor(!ST.is64Bit(), {s32})
322 .customFor(ST.is64Bit(), {s64})
323 .widenScalarToNextPow2(0)
324 .clampScalar(0, sXLen, sXLen);
325
326 // TODO: transform illegal vector types into legal vector type
327 getActionDefinitionsBuilder(G_FREEZE)
328 .legalFor({s16, s32, p0})
329 .legalFor(ST.is64Bit(), {s64})
330 .legalIf(typeIsLegalBoolVec(0, BoolVecTys, ST))
331 .legalIf(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST))
332 .widenScalarToNextPow2(0)
333 .clampScalar(0, s16, sXLen);
334
335 // TODO: transform illegal vector types into legal vector type
336 // TODO: Merge with G_FREEZE?
337 getActionDefinitionsBuilder(
338 {G_IMPLICIT_DEF, G_CONSTANT_FOLD_BARRIER})
339 .legalFor({s32, sXLen, p0})
340 .legalIf(typeIsLegalBoolVec(0, BoolVecTys, ST))
341 .legalIf(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST))
342 .widenScalarToNextPow2(0)
343 .clampScalar(0, s32, sXLen);
344
345 getActionDefinitionsBuilder(G_ICMP)
346 .legalFor({{sXLen, sXLen}, {sXLen, p0}})
347 .legalIf(all(typeIsLegalBoolVec(0, BoolVecTys, ST),
348 typeIsLegalIntOrFPVec(1, IntOrFPVecTys, ST)))
349 .widenScalarOrEltToNextPow2OrMinSize(1, 8)
350 .clampScalar(1, sXLen, sXLen)
351 .clampScalar(0, sXLen, sXLen);
352
353 getActionDefinitionsBuilder(G_SELECT)
354 .legalFor({{s32, sXLen}, {p0, sXLen}})
355 .legalIf(all(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST),
356 typeIsLegalBoolVec(1, BoolVecTys, ST)))
357 .legalFor(XLen == 64 || ST.hasStdExtD(), {{s64, sXLen}})
358 .widenScalarToNextPow2(0)
359 .clampScalar(0, s32, (XLen == 64 || ST.hasStdExtD()) ? s64 : s32)
360 .clampScalar(1, sXLen, sXLen);
361
362 auto &LoadActions = getActionDefinitionsBuilder(G_LOAD);
363 auto &StoreActions = getActionDefinitionsBuilder(G_STORE);
364 auto &ExtLoadActions = getActionDefinitionsBuilder({G_SEXTLOAD, G_ZEXTLOAD});
365
366 // Return the alignment needed for scalar memory ops. If unaligned scalar mem
367 // is supported, we only require byte alignment. Otherwise, we need the memory
368 // op to be natively aligned.
369 auto getScalarMemAlign = [&ST](unsigned Size) {
370 return ST.enableUnalignedScalarMem() ? 8 : Size;
371 };
372
373 LoadActions.legalForTypesWithMemDesc(
374 {{s16, p0, s8, getScalarMemAlign(8)},
375 {s32, p0, s8, getScalarMemAlign(8)},
376 {s16, p0, s16, getScalarMemAlign(16)},
377 {s32, p0, s16, getScalarMemAlign(16)},
378 {s32, p0, s32, getScalarMemAlign(32)},
379 {p0, p0, sXLen, getScalarMemAlign(XLen)}});
380 StoreActions.legalForTypesWithMemDesc(
381 {{s16, p0, s8, getScalarMemAlign(8)},
382 {s32, p0, s8, getScalarMemAlign(8)},
383 {s16, p0, s16, getScalarMemAlign(16)},
384 {s32, p0, s16, getScalarMemAlign(16)},
385 {s32, p0, s32, getScalarMemAlign(32)},
386 {p0, p0, sXLen, getScalarMemAlign(XLen)}});
387 ExtLoadActions.legalForTypesWithMemDesc(
388 {{sXLen, p0, s8, getScalarMemAlign(8)},
389 {sXLen, p0, s16, getScalarMemAlign(16)}});
390 if (XLen == 64) {
391 LoadActions.legalForTypesWithMemDesc(
392 {{s64, p0, s8, getScalarMemAlign(8)},
393 {s64, p0, s16, getScalarMemAlign(16)},
394 {s64, p0, s32, getScalarMemAlign(32)},
395 {s64, p0, s64, getScalarMemAlign(64)}});
396 StoreActions.legalForTypesWithMemDesc(
397 {{s64, p0, s8, getScalarMemAlign(8)},
398 {s64, p0, s16, getScalarMemAlign(16)},
399 {s64, p0, s32, getScalarMemAlign(32)},
400 {s64, p0, s64, getScalarMemAlign(64)}});
401 ExtLoadActions.legalForTypesWithMemDesc(
402 {{s64, p0, s32, getScalarMemAlign(32)}});
403 } else if (ST.hasStdExtD() || ST.hasStdExtZilsd()) {
404 // RV32: s64 is legal for f64 (D/Zdinx) or i64 (Zilsd); LLT cannot tell the
405 // two apart, so one rule covers both.
406 // FIXME: We require natural alignment, so under -mzilsd-word-align an
407 // align-4 i64 access is split into two s32 ones. riscv-prera-zilsd-opt
408 // merges them back into LD_RV32/SD_RV32, but it is skipped at -O0.
409 LoadActions.legalForTypesWithMemDesc(
410 {{s64, p0, s64, getScalarMemAlign(64)}});
411 StoreActions.legalForTypesWithMemDesc(
412 {{s64, p0, s64, getScalarMemAlign(64)}});
413 }
414
415 // Vector loads/stores.
416 if (ST.hasVInstructions()) {
417 LoadActions.legalForTypesWithMemDesc({{nxv2s8, p0, nxv2s8, 8},
418 {nxv4s8, p0, nxv4s8, 8},
419 {nxv8s8, p0, nxv8s8, 8},
420 {nxv16s8, p0, nxv16s8, 8},
421 {nxv32s8, p0, nxv32s8, 8},
422 {nxv64s8, p0, nxv64s8, 8},
423 {nxv2s16, p0, nxv2s16, 16},
424 {nxv4s16, p0, nxv4s16, 16},
425 {nxv8s16, p0, nxv8s16, 16},
426 {nxv16s16, p0, nxv16s16, 16},
427 {nxv32s16, p0, nxv32s16, 16},
428 {nxv2s32, p0, nxv2s32, 32},
429 {nxv4s32, p0, nxv4s32, 32},
430 {nxv8s32, p0, nxv8s32, 32},
431 {nxv16s32, p0, nxv16s32, 32}});
432 StoreActions.legalForTypesWithMemDesc({{nxv2s8, p0, nxv2s8, 8},
433 {nxv4s8, p0, nxv4s8, 8},
434 {nxv8s8, p0, nxv8s8, 8},
435 {nxv16s8, p0, nxv16s8, 8},
436 {nxv32s8, p0, nxv32s8, 8},
437 {nxv64s8, p0, nxv64s8, 8},
438 {nxv2s16, p0, nxv2s16, 16},
439 {nxv4s16, p0, nxv4s16, 16},
440 {nxv8s16, p0, nxv8s16, 16},
441 {nxv16s16, p0, nxv16s16, 16},
442 {nxv32s16, p0, nxv32s16, 16},
443 {nxv2s32, p0, nxv2s32, 32},
444 {nxv4s32, p0, nxv4s32, 32},
445 {nxv8s32, p0, nxv8s32, 32},
446 {nxv16s32, p0, nxv16s32, 32}});
447
448 if (ST.getELen() == 64) {
449 LoadActions.legalForTypesWithMemDesc({{nxv1s8, p0, nxv1s8, 8},
450 {nxv1s16, p0, nxv1s16, 16},
451 {nxv1s32, p0, nxv1s32, 32}});
452 StoreActions.legalForTypesWithMemDesc({{nxv1s8, p0, nxv1s8, 8},
453 {nxv1s16, p0, nxv1s16, 16},
454 {nxv1s32, p0, nxv1s32, 32}});
455 }
456
457 if (ST.hasVInstructionsI64()) {
458 LoadActions.legalForTypesWithMemDesc({{nxv1s64, p0, nxv1s64, 64},
459 {nxv2s64, p0, nxv2s64, 64},
460 {nxv4s64, p0, nxv4s64, 64},
461 {nxv8s64, p0, nxv8s64, 64}});
462 StoreActions.legalForTypesWithMemDesc({{nxv1s64, p0, nxv1s64, 64},
463 {nxv2s64, p0, nxv2s64, 64},
464 {nxv4s64, p0, nxv4s64, 64},
465 {nxv8s64, p0, nxv8s64, 64}});
466 }
467
468 // we will take the custom lowering logic if we have scalable vector types
469 // with non-standard alignments
470 LoadActions.customIf(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST));
471 StoreActions.customIf(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST));
472
473 // Pointers require that XLen sized elements are legal.
474 if (XLen <= ST.getELen()) {
475 LoadActions.customIf(typeIsLegalPtrVec(0, PtrVecTys, ST));
476 StoreActions.customIf(typeIsLegalPtrVec(0, PtrVecTys, ST));
477 }
478 }
479
480 LoadActions.widenScalarToNextPow2(0, /* MinSize = */ 8)
481 .lowerIfMemSizeNotByteSizePow2()
482 .clampScalar(0, s16, sXLen)
483 .lower();
484 StoreActions
485 .clampScalar(0, s16, sXLen)
486 .lowerIfMemSizeNotByteSizePow2()
487 .lower();
488
489 ExtLoadActions.widenScalarToNextPow2(0).clampScalar(0, sXLen, sXLen).lower();
490
491 getActionDefinitionsBuilder({G_PTR_ADD, G_PTRMASK}).legalFor({{p0, sXLen}});
492
493 getActionDefinitionsBuilder(G_PTRTOINT)
494 .legalFor({{sXLen, p0}})
495 .clampScalar(0, sXLen, sXLen);
496
497 getActionDefinitionsBuilder(G_INTTOPTR)
498 .legalFor({{p0, sXLen}})
499 .clampScalar(1, sXLen, sXLen);
500
501 getActionDefinitionsBuilder(G_BR).alwaysLegal();
502
503 getActionDefinitionsBuilder(G_BRCOND).legalFor({sXLen}).minScalar(0, sXLen);
504
505 getActionDefinitionsBuilder(G_BRJT).customFor({{p0, sXLen}});
506
507 getActionDefinitionsBuilder(G_BRINDIRECT).legalFor({p0});
508
509 getActionDefinitionsBuilder(G_PHI)
510 .legalFor({p0, s32, sXLen})
511 .widenScalarToNextPow2(0)
512 .clampScalar(0, s32, sXLen);
513
514 getActionDefinitionsBuilder({G_GLOBAL_VALUE, G_JUMP_TABLE, G_CONSTANT_POOL})
515 .legalFor({p0});
516
517 if (ST.hasStdExtZmmul()) {
518 getActionDefinitionsBuilder(G_MUL)
519 .legalFor({sXLen})
520 .widenScalarToNextPow2(0)
521 .clampScalar(0, sXLen, sXLen);
522
523 // clang-format off
524 getActionDefinitionsBuilder({G_SMULH, G_UMULH})
525 .legalFor({sXLen})
526 .lower();
527 // clang-format on
528
529 getActionDefinitionsBuilder({G_SMULO, G_UMULO}).minScalar(0, sXLen).lower();
530 } else {
531 getActionDefinitionsBuilder(G_MUL)
532 .libcallFor({sXLen, sDoubleXLen})
533 .widenScalarToNextPow2(0)
534 .clampScalar(0, sXLen, sDoubleXLen);
535
536 getActionDefinitionsBuilder({G_SMULH, G_UMULH}).lowerFor({sXLen});
537
538 getActionDefinitionsBuilder({G_SMULO, G_UMULO})
539 .minScalar(0, sXLen)
540 // Widen sXLen to sDoubleXLen so we can use a single libcall to get
541 // the low bits for the mul result and high bits to do the overflow
542 // check.
543 .widenScalarIf(typeIs(0, sXLen),
544 LegalizeMutations::changeTo(0, sDoubleXLen))
545 .lower();
546 }
547
548 if (ST.hasStdExtM()) {
549 getActionDefinitionsBuilder({G_SDIV, G_UDIV, G_UREM})
550 .legalFor({sXLen})
551 .customFor({s32})
552 .libcallFor({sDoubleXLen})
553 .clampScalar(0, s32, sDoubleXLen)
554 .widenScalarToNextPow2(0);
555 getActionDefinitionsBuilder(G_SREM)
556 .legalFor({sXLen})
557 .libcallFor({sDoubleXLen})
558 .clampScalar(0, sXLen, sDoubleXLen)
559 .widenScalarToNextPow2(0);
560 } else {
561 getActionDefinitionsBuilder({G_UDIV, G_SDIV, G_UREM, G_SREM})
562 .libcallFor({sXLen, sDoubleXLen})
563 .clampScalar(0, sXLen, sDoubleXLen)
564 .widenScalarToNextPow2(0);
565 }
566
567 // TODO: Use libcall for sDoubleXLen.
568 getActionDefinitionsBuilder({G_SDIVREM, G_UDIVREM}).lower();
569
570 getActionDefinitionsBuilder(G_ABS)
571 .customFor(ST.hasStdExtZbb(), {sXLen})
572 .minScalar(ST.hasStdExtZbb(), 0, sXLen)
573 .lower();
574
575 getActionDefinitionsBuilder({G_ABDS, G_ABDU})
576 .minScalar(ST.hasStdExtZbb(), 0, sXLen)
577 .lower();
578
579 getActionDefinitionsBuilder({G_UMAX, G_UMIN, G_SMAX, G_SMIN})
580 .legalFor(ST.hasStdExtZbb(), {sXLen})
581 .minScalar(ST.hasStdExtZbb(), 0, sXLen)
582 .lower();
583
584 getActionDefinitionsBuilder({G_SCMP, G_UCMP}).lower();
585
586 getActionDefinitionsBuilder(G_FRAME_INDEX).legalFor({p0});
587
588 getActionDefinitionsBuilder({G_MEMCPY, G_MEMMOVE, G_MEMSET}).libcall();
589
590 getActionDefinitionsBuilder({G_MEMCPY_INLINE, G_MEMSET_INLINE}).lower();
591
592 getActionDefinitionsBuilder({G_DYN_STACKALLOC, G_STACKSAVE, G_STACKRESTORE})
593 .lower();
594
595 // On RV64 the 64-bit counter CSRs (cycle/time) are read directly. On RV32
596 // they are custom-legally lowered to a re-read-the-high-half loop (see
597 // legalizeReadCounter).
598 getActionDefinitionsBuilder({G_READCYCLECOUNTER, G_READSTEADYCOUNTER})
599 .legalFor(ST.is64Bit(), {s64})
600 .customFor(!ST.is64Bit(), {s64});
601
602 // FP Operations
603
604 // FIXME: Support f128 for rv32 when libcall handling is able to use sret.
605 getActionDefinitionsBuilder({G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FMA, G_FSQRT,
606 G_FMAXNUM, G_FMINNUM, G_FMAXIMUMNUM,
607 G_FMINIMUMNUM})
608 .legalFor(ST.hasStdExtF(), {f32})
609 .legalFor(ST.hasStdExtD(), {f64})
610 .legalFor(ST.hasStdExtZfh(), {f16})
611 .libcallFor({f32, f64})
612 .libcallFor(ST.is64Bit(), {f128});
613
614 getActionDefinitionsBuilder({G_FNEG, G_FABS})
615 .legalFor(ST.hasStdExtF(), {f32})
616 .legalFor(ST.hasStdExtD(), {f64})
617 .legalFor(ST.hasStdExtZfh(), {f16})
618 .lowerFor({f32, f64, f128});
619
620 getActionDefinitionsBuilder(G_FREM)
621 .libcallFor({f32, f64})
622 .libcallFor(ST.is64Bit(), {f128})
623 .minScalar(0, f32)
624 .scalarize(0);
625
626 getActionDefinitionsBuilder(G_FCOPYSIGN)
627 .legalFor(ST.hasStdExtF(), {{f32, f32}})
628 .legalFor(ST.hasStdExtD(), {{f64, f64}, {f32, f64}, {f64, f32}})
629 .legalFor(ST.hasStdExtZfh(), {{f16, f16}, {f16, f32}, {f32, f16}})
630 .legalFor(ST.hasStdExtZfh() && ST.hasStdExtD(), {{f16, f64}, {f64, f16}})
631 .lower();
632
633 getActionDefinitionsBuilder(G_FPTRUNC)
634 .legalFor(ST.hasStdExtD(), {{f32, f64}})
635 .legalFor(ST.hasStdExtZfhmin(), {{f16, f32}})
636 .legalFor(ST.hasStdExtZfhmin() && ST.hasStdExtD(), {{f16, f64}})
637 .libcallFor({{f16, f32}, {f16, f64}, {f32, f64}})
638 .libcallFor(ST.is64Bit(), {{f32, f128}, {f64, f128}});
639 getActionDefinitionsBuilder(G_FPEXT)
640 .legalFor(ST.hasStdExtD(), {{f64, f32}})
641 .legalFor(ST.hasStdExtZfhmin(), {{f32, f16}})
642 .legalFor(ST.hasStdExtZfhmin() && ST.hasStdExtD(), {{f64, f16}})
643 .libcallFor(!ST.hasStdExtZfhmin(), {{f32, f16}})
644 .libcallFor(!(ST.hasStdExtZfhmin() && ST.hasStdExtD()), {{f64, f16}})
645 .libcallFor({{f64, f32}})
646 .libcallFor(ST.is64Bit(), {{f128, f32}, {f128, f64}});
647
648 getActionDefinitionsBuilder(G_FCMP)
649 .legalFor(ST.hasStdExtF(), {{sXLen, f32}})
650 .legalFor(ST.hasStdExtD(), {{sXLen, f64}})
651 .legalFor(ST.hasStdExtZfh(), {{sXLen, f16}})
652 .clampScalar(0, sXLen, sXLen)
653 .minScalar(1, f32)
654 .libcallFor({{sXLen, f32}, {sXLen, f64}})
655 .libcallFor(ST.is64Bit(), {{sXLen, f128}});
656
657 // TODO: Support vector version of G_IS_FPCLASS.
658 getActionDefinitionsBuilder(G_IS_FPCLASS)
659 .customFor(ST.hasStdExtF(), {{s1, f32}})
660 .customFor(ST.hasStdExtD(), {{s1, f64}})
661 .customFor(ST.hasStdExtZfh(), {{s1, f16}})
662 .lower();
663
664 getActionDefinitionsBuilder(G_FCONSTANT)
665 .legalFor(ST.hasStdExtF(), {f32})
666 .legalFor(ST.hasStdExtD(), {f64})
667 .legalFor(ST.hasStdExtZfhmin(), {f16})
668 .lowerFor({f32, f64, f128, LLT::bfloat16()});
669
670 getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
671 .legalFor(ST.hasStdExtF(), {{sXLen, f32}})
672 .legalFor(ST.hasStdExtD(), {{sXLen, f64}})
673 .legalFor(ST.hasStdExtZfh(), {{sXLen, f16}})
674 .customFor(ST.is64Bit() && ST.hasStdExtF(), {{s32, f32}})
675 .customFor(ST.is64Bit() && ST.hasStdExtD(), {{s32, f64}})
676 .customFor(ST.is64Bit() && ST.hasStdExtZfh(), {{s32, f16}})
677 .widenScalarToNextPow2(0)
678 .minScalar(0, s32)
679 // The magnitude of a half is at most 65504, so with Zfh use fcvt.w[u].h
680 // and extend the i32 result. Otherwise promote the half source to float
681 // (via fcvt.s.h with Zfhmin, __extendhfsf2 without) and use the float
682 // conversion. On RV32, exclude i64 results here so that they get
683 // narrowed to i32 first.
684 .widenScalarIf(
685 [=, &ST](const LegalityQuery &Query) {
686 return Query.Types[1] == f16 && !ST.hasStdExtZfh() &&
687 (ST.is64Bit() || Query.Types[0].isScalar(32));
688 },
689 changeTo(1, f32))
690 .libcallFor(!ST.hasStdExtZfhmin(), {{s64, f16}})
691 .narrowScalarFor({{s64, f16}}, changeTo(0, s32))
692 .libcallFor({{s32, f32}, {s64, f32}, {s32, f64}, {s64, f64}})
693 .libcallFor(ST.is64Bit(), {{s32, f128}, {s64, f128}}) // FIXME RV32.
694 .libcallFor(ST.is64Bit(), {{s128, f32}, {s128, f64}, {s128, f128}});
695
696 // Unlike G_FPTOSI/G_FPTOUI these need no libcalls for out-of-range values:
697 // the RISC-V conversions already saturate. There is no legalFor() because
698 // they do not produce 0 for NaN, which legalizeCustom fixes up with a
699 // compare and a select. The remaining cases fall back to the generic
700 // lowering, which clamps before converting and only handles scalars.
701 getActionDefinitionsBuilder({G_FPTOSI_SAT, G_FPTOUI_SAT})
702 .customFor(ST.hasStdExtF(), {{sXLen, f32}})
703 .customFor(ST.hasStdExtD(), {{sXLen, f64}})
704 .customFor(ST.hasStdExtZfh(), {{sXLen, f16}})
705 .customFor(ST.is64Bit() && ST.hasStdExtF(), {{s32, f32}})
706 .customFor(ST.is64Bit() && ST.hasStdExtD(), {{s32, f64}})
707 .customFor(ST.is64Bit() && ST.hasStdExtZfh(), {{s32, f16}})
708 // f16 extends exactly to f32; converting via f32 avoids needing rules
709 // for every {sXX, f16} combination.
710 .widenScalarIf(typeIs(1, f16), changeTo(1, f32))
711 .lowerIf(all(isScalar(0), isScalar(1)));
712
713 getActionDefinitionsBuilder({G_LROUND, G_LLROUND})
714 .legalFor(ST.hasStdExtF(), {{sXLen, f32}})
715 .legalFor(ST.hasStdExtD(), {{sXLen, f64}})
716 .legalFor(ST.hasStdExtZfh(), {{sXLen, f16}})
717 .customFor(ST.is64Bit() && ST.hasStdExtF(), {{s32, f32}})
718 .customFor(ST.is64Bit() && ST.hasStdExtD(), {{s32, f64}})
719 .customFor(ST.is64Bit() && ST.hasStdExtZfh(), {{s32, f16}})
720 .minScalar(1, f32)
721 .libcallFor({{s32, f32},
722 {s64, f32},
723 {s32, f64},
724 {s64, f64},
725 {s32, f128},
726 {s64, f128}});
727
728 getActionDefinitionsBuilder({G_INTRINSIC_LRINT, G_INTRINSIC_LLRINT})
729 .legalFor(ST.hasStdExtF(), {{sXLen, f32}})
730 .legalFor(ST.hasStdExtD(), {{sXLen, f64}})
731 .legalFor(ST.hasStdExtZfh(), {{sXLen, f16}})
732 .minScalar(0, sXLen)
733 .minScalar(1, f32)
734 .libcallFor({{s32, f32},
735 {s64, f32},
736 {s32, f64},
737 {s64, f64},
738 {s32, f128},
739 {s64, f128}});
740
741 getActionDefinitionsBuilder({G_SITOFP, G_UITOFP})
742 .legalFor(ST.hasStdExtF(), {{f32, sXLen}})
743 .legalFor(ST.hasStdExtD(), {{f64, sXLen}})
744 .legalFor(ST.hasStdExtZfh(), {{f16, sXLen}})
745 .widenScalarToNextPow2(1)
746 // Promote to XLen if the operation is legal.
747 .widenScalarIf(
748 [=, &ST](const LegalityQuery &Query) {
749 return Query.Types[0].isScalar() && Query.Types[1].isScalar() &&
750 (Query.Types[1].getSizeInBits() < ST.getXLen()) &&
751 ((ST.hasStdExtF() && Query.Types[0].getSizeInBits() == 32) ||
752 (ST.hasStdExtD() && Query.Types[0].getSizeInBits() == 64) ||
753 (ST.hasStdExtZfh() &&
754 Query.Types[0].getSizeInBits() == 16));
755 },
757 // Otherwise only promote to s32 since we have si libcalls.
758 .minScalar(1, s32)
759 .libcallFor({{f32, s32}, {f64, s32}, {f32, s64}, {f64, s64}})
760 .libcallFor(ST.is64Bit(), {{f128, s32}, {f128, s64}}) // FIXME RV32.
761 .libcallFor(ST.is64Bit(), {{f32, s128}, {f64, s128}, {f128, s128}});
762
763 // FIXME: We can do custom inline expansion like SelectionDAG.
764 getActionDefinitionsBuilder({G_FCEIL, G_FFLOOR, G_FRINT, G_FNEARBYINT,
765 G_INTRINSIC_TRUNC, G_INTRINSIC_ROUND,
766 G_INTRINSIC_ROUNDEVEN})
767 .legalFor(ST.hasStdExtZfa(), {f32})
768 .legalFor(ST.hasStdExtZfa() && ST.hasStdExtD(), {f64})
769 .legalFor(ST.hasStdExtZfa() && ST.hasStdExtZfh(), {f16})
770 .libcallFor({f32, f64})
771 .libcallFor(ST.is64Bit(), {f128});
772
773 getActionDefinitionsBuilder({G_FMAXIMUM, G_FMINIMUM})
774 .legalFor(ST.hasStdExtZfa(), {f32})
775 .legalFor(ST.hasStdExtZfa() && ST.hasStdExtD(), {f64})
776 .legalFor(ST.hasStdExtZfa() && ST.hasStdExtZfh(), {f16});
777
778 getActionDefinitionsBuilder({G_FCOS, G_FSIN, G_FTAN, G_FPOW, G_FLOG, G_FLOG2,
779 G_FLOG10, G_FEXP, G_FEXP2, G_FEXP10, G_FACOS,
780 G_FASIN, G_FATAN, G_FATAN2, G_FCOSH, G_FSINH,
781 G_FTANH, G_FMODF, G_FSINCOS})
782 .libcallFor({f32, f64})
783 .libcallFor(ST.is64Bit(), {f128});
784 getActionDefinitionsBuilder({G_FPOWI, G_FLDEXP})
785 .libcallFor({{f32, s32}, {f64, s32}})
786 .libcallFor(ST.is64Bit(), {f128, s32});
787
788 getActionDefinitionsBuilder(G_FCANONICALIZE)
789 .legalFor(ST.hasStdExtF(), {f32})
790 .legalFor(ST.hasStdExtD(), {f64})
791 .legalFor(ST.hasStdExtZfh(), {f16});
792
793 getActionDefinitionsBuilder(G_VASTART).customFor({p0});
794
795 // va_list must be a pointer, but most sized types are pretty easy to handle
796 // as the destination.
797 getActionDefinitionsBuilder(G_VAARG)
798 // TODO: Implement narrowScalar and widenScalar for G_VAARG for types
799 // other than sXLen.
800 .clampScalar(0, sXLen, sXLen)
801 .lowerForCartesianProduct({sXLen, p0}, {p0});
802
803 getActionDefinitionsBuilder(G_VSCALE)
804 .clampScalar(0, sXLen, sXLen)
805 .customFor({sXLen});
806
807 auto &SplatActions =
808 getActionDefinitionsBuilder(G_SPLAT_VECTOR)
809 .legalIf(all(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST),
810 typeIs(1, sXLen)))
811 .customIf(all(typeIsLegalBoolVec(0, BoolVecTys, ST), typeIs(1, s1)));
812 // Handle case of s64 element vectors on RV32. If the subtarget does not have
813 // f64, then try to lower it to G_SPLAT_VECTOR_SPLIT_64_VL. If the subtarget
814 // does have f64, then we don't know whether the type is an f64 or an i64,
815 // so mark the G_SPLAT_VECTOR as legal and decide later what to do with it,
816 // depending on how the instructions it consumes are legalized. They are not
817 // legalized yet since legalization is in reverse postorder, so we cannot
818 // make the decision at this moment.
819 if (XLen == 32) {
820 if (ST.hasVInstructionsF64() && ST.hasStdExtD())
821 SplatActions.legalIf(all(
822 typeInSet(0, {nxv1s64, nxv2s64, nxv4s64, nxv8s64}), typeIs(1, s64)));
823 else if (ST.hasVInstructionsI64())
824 SplatActions.customIf(all(
825 typeInSet(0, {nxv1s64, nxv2s64, nxv4s64, nxv8s64}), typeIs(1, s64)));
826 }
827
828 SplatActions.clampScalar(1, sXLen, sXLen);
829
830 LegalityPredicate ExtractSubvecBitcastPred = [=](const LegalityQuery &Query) {
831 LLT DstTy = Query.Types[0];
832 LLT SrcTy = Query.Types[1];
833 return DstTy.getElementType() == LLT::scalar(1) &&
834 DstTy.getElementCount().getKnownMinValue() >= 8 &&
835 SrcTy.getElementCount().getKnownMinValue() >= 8;
836 };
837 getActionDefinitionsBuilder(G_EXTRACT_SUBVECTOR)
838 // We don't have the ability to slide mask vectors down indexed by their
839 // i1 elements; the smallest we can do is i8. Often we are able to bitcast
840 // to equivalent i8 vectors.
841 .bitcastIf(
842 all(typeIsLegalBoolVec(0, BoolVecTys, ST),
843 typeIsLegalBoolVec(1, BoolVecTys, ST), ExtractSubvecBitcastPred),
844 [=](const LegalityQuery &Query) {
845 LLT CastTy = LLT::vector(
846 Query.Types[0].getElementCount().divideCoefficientBy(8), 8);
847 return std::pair(0, CastTy);
848 })
849 .customIf(LegalityPredicates::any(
850 all(typeIsLegalBoolVec(0, BoolVecTys, ST),
851 typeIsLegalBoolVec(1, BoolVecTys, ST)),
852 all(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST),
853 typeIsLegalIntOrFPVec(1, IntOrFPVecTys, ST))));
854
855 getActionDefinitionsBuilder(G_INSERT_SUBVECTOR)
856 .customIf(all(typeIsLegalBoolVec(0, BoolVecTys, ST),
857 typeIsLegalBoolVec(1, BoolVecTys, ST)))
858 .customIf(all(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST),
859 typeIsLegalIntOrFPVec(1, IntOrFPVecTys, ST)));
860
861 getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG_WITH_SUCCESS)
862 .lowerIf(all(typeInSet(0, {s8, s16, s32, s64}), typeIs(2, p0)));
863
864 getActionDefinitionsBuilder({G_ATOMIC_CMPXCHG, G_ATOMICRMW_ADD,
865 G_ATOMICRMW_XCHG, G_ATOMICRMW_AND,
866 G_ATOMICRMW_OR, G_ATOMICRMW_XOR})
867 .legalFor(ST.hasStdExtA(), {{sXLen, p0}})
868 .libcallFor(!ST.hasStdExtA(), {{s8, p0}, {s16, p0}, {s32, p0}, {s64, p0}})
869 .clampScalar(0, sXLen, sXLen);
870
871 getActionDefinitionsBuilder(G_ATOMICRMW_SUB)
872 .libcallFor(!ST.hasStdExtA(), {{s8, p0}, {s16, p0}, {s32, p0}, {s64, p0}})
873 .clampScalar(0, sXLen, sXLen)
874 .lower();
875
876 getActionDefinitionsBuilder(
877 {G_ATOMICRMW_MAX, G_ATOMICRMW_MIN, G_ATOMICRMW_UMAX, G_ATOMICRMW_UMIN})
878 .legalFor(ST.hasStdExtA(), {{sXLen, p0}})
879 .clampScalar(0, sXLen, sXLen)
880 .unsupported();
881
882 getActionDefinitionsBuilder(G_PREFETCH).legalIf(typeIs(0, p0));
883
884 LegalityPredicate InsertVectorEltPred = [=](const LegalityQuery &Query) {
885 LLT VecTy = Query.Types[0];
886 LLT EltTy = Query.Types[1];
887 return VecTy.getElementType() == EltTy;
888 };
889
890 getActionDefinitionsBuilder(G_INSERT_VECTOR_ELT)
891 .legalIf(all(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST),
892 InsertVectorEltPred, typeIs(2, sXLen)))
893 .legalIf(all(typeIsLegalBoolVec(0, BoolVecTys, ST), InsertVectorEltPred,
894 typeIs(2, sXLen)));
895
896 getActionDefinitionsBuilder({G_INTRINSIC, G_INTRINSIC_W_SIDE_EFFECTS})
897 .alwaysLegal();
898
899 getActionDefinitionsBuilder(G_FENCE).alwaysLegal();
900
901 getActionDefinitionsBuilder({G_TRAP, G_DEBUGTRAP, G_UBSANTRAP}).alwaysLegal();
902
903 verify(*ST.getInstrInfo());
904}
905
907 MachineInstr &MI) const {
908 Intrinsic::ID IntrinsicID = cast<GIntrinsic>(MI).getIntrinsicID();
909
911 RISCVVIntrinsicsTable::getRISCVVIntrinsicInfo(IntrinsicID)) {
912 if (II->hasScalarOperand() && !II->IsFPIntrinsic) {
913 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
914 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
915
916 auto OldScalar = MI.getOperand(II->ScalarOperand + 2).getReg();
917 // Legalize integer vx form intrinsic.
918 if (MRI.getType(OldScalar).isScalar()) {
919 if (MRI.getType(OldScalar).getSizeInBits() < iXLen.getSizeInBits()) {
920 Helper.Observer.changingInstr(MI);
921 Helper.widenScalarSrc(MI, iXLen, II->ScalarOperand + 2,
922 TargetOpcode::G_ANYEXT);
923 Helper.Observer.changedInstr(MI);
924 } else if (MRI.getType(OldScalar).getSizeInBits() >
925 iXLen.getSizeInBits()) {
926 // TODO: i64 in riscv32.
927 return false;
928 }
929 }
930 }
931 return true;
932 }
933
934 switch (IntrinsicID) {
935 default:
936 return false;
937 case Intrinsic::riscv_orc_b: {
938 if (!STI.hasStdExtZbb())
939 return false;
940
941 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
942 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
943 LLT Ty = MRI.getType(MI.getOperand(0).getReg());
944 if (Ty != iXLen && (!STI.is64Bit() || Ty != LLT::scalar(32)))
945 return false;
946
947 // Each output byte depends only on the corresponding input byte, so any
948 // extension is fine for narrow types.
949 Register Src = MI.getOperand(2).getReg();
950 if (Ty != iXLen)
951 Src = MIRBuilder.buildAnyExt(iXLen, Src).getReg(0);
952 auto Result = MIRBuilder.buildInstr(RISCV::G_ORC_B, {iXLen}, {Src});
953 if (Ty != iXLen)
954 MIRBuilder.buildTrunc(MI.getOperand(0), Result);
955 else
956 MIRBuilder.buildCopy(MI.getOperand(0), Result);
957 MI.eraseFromParent();
958 return true;
959 }
960 case Intrinsic::riscv_clmulh:
961 Helper.MIRBuilder.buildInstr(TargetOpcode::G_CLMULH, {MI.getOperand(0)},
962 {MI.getOperand(2), MI.getOperand(3)});
963 MI.eraseFromParent();
964 return true;
965 case Intrinsic::riscv_clmulr:
966 Helper.MIRBuilder.buildInstr(TargetOpcode::G_CLMULR, {MI.getOperand(0)},
967 {MI.getOperand(2), MI.getOperand(3)});
968 MI.eraseFromParent();
969 return true;
970 case Intrinsic::vacopy: {
971 // vacopy arguments must be legal because of the intrinsic signature.
972 // No need to check here.
973
974 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
975 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
976 MachineFunction &MF = *MI.getMF();
977 const DataLayout &DL = MIRBuilder.getDataLayout();
978 LLVMContext &Ctx = MF.getFunction().getContext();
979
980 Register DstLst = MI.getOperand(1).getReg();
981 LLT PtrTy = MRI.getType(DstLst);
982
983 // Load the source va_list
984 Align Alignment = DL.getABITypeAlign(getTypeForLLT(PtrTy, Ctx));
986 MachinePointerInfo(), MachineMemOperand::MOLoad, PtrTy, Alignment);
987 auto Tmp = MIRBuilder.buildLoad(PtrTy, MI.getOperand(2), *LoadMMO);
988
989 // Store the result in the destination va_list
992 MIRBuilder.buildStore(Tmp, DstLst, *StoreMMO);
993
994 MI.eraseFromParent();
995 return true;
996 }
997 case Intrinsic::riscv_vsetvli:
998 case Intrinsic::riscv_vsetvlimax:
999 case Intrinsic::riscv_masked_atomicrmw_add:
1000 case Intrinsic::riscv_masked_atomicrmw_sub:
1001 case Intrinsic::riscv_masked_atomicrmw_xchg:
1002 case Intrinsic::riscv_masked_atomicrmw_max:
1003 case Intrinsic::riscv_masked_atomicrmw_min:
1004 case Intrinsic::riscv_masked_atomicrmw_umax:
1005 case Intrinsic::riscv_masked_atomicrmw_umin:
1006 case Intrinsic::riscv_masked_cmpxchg:
1007 return true;
1008 }
1009}
1010
1011bool RISCVLegalizerInfo::legalizeVAStart(MachineInstr &MI,
1012 MachineIRBuilder &MIRBuilder) const {
1013 // Stores the address of the VarArgsFrameIndex slot into the memory location
1014 assert(MI.getOpcode() == TargetOpcode::G_VASTART);
1015 MachineFunction *MF = MI.getParent()->getParent();
1017 int FI = FuncInfo->getVarArgsFrameIndex();
1018 LLT AddrTy = MIRBuilder.getMRI()->getType(MI.getOperand(0).getReg());
1019 auto FINAddr = MIRBuilder.buildFrameIndex(AddrTy, FI);
1020 assert(MI.hasOneMemOperand());
1021 MIRBuilder.buildStore(FINAddr, MI.getOperand(0).getReg(),
1022 *MI.memoperands()[0]);
1023 MI.eraseFromParent();
1024 return true;
1025}
1026
1027bool RISCVLegalizerInfo::legalizeReadCounter(
1028 MachineInstr &MI, MachineIRBuilder &MIRBuilder,
1029 GISelChangeObserver &Observer) const {
1030 assert((MI.getOpcode() == TargetOpcode::G_READCYCLECOUNTER ||
1031 MI.getOpcode() == TargetOpcode::G_READSTEADYCOUNTER) &&
1032 "Unexpected opcode");
1033 assert(!STI.is64Bit() && "READCYCLECOUNTER/READSTEADYCOUNTER only "
1034 "has custom type legalization on riscv32");
1035
1036 // On RV32 a 64-bit counter CSR must be read as two 32-bit halves. Because
1037 // the count may wrap between the two reads, re-read the high half and loop
1038 // until the two high reads agree.
1039 int64_t LoCounter, HiCounter;
1040 if (MI.getOpcode() == TargetOpcode::G_READCYCLECOUNTER) {
1041 LoCounter = RISCVSysReg::cycle;
1042 HiCounter = RISCVSysReg::cycleh;
1043 } else {
1044 LoCounter = RISCVSysReg::time;
1045 HiCounter = RISCVSysReg::timeh;
1046 }
1047
1048 MachineBasicBlock *BB = MI.getParent();
1049 MachineFunction &MF = *BB->getParent();
1050 const BasicBlock *LLVMBB = BB->getBasicBlock();
1051 DebugLoc DL = MI.getDebugLoc();
1052 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
1053
1054 // Split BB into an entry that falls through into a loop block, and a done
1055 // block that receives the remainder of BB and its original successors.
1056 MachineFunction::iterator It = std::next(BB->getIterator());
1057 MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(LLVMBB);
1058 MachineBasicBlock *DoneMBB = MF.CreateMachineBasicBlock(LLVMBB);
1059 MF.insert(It, LoopMBB);
1060 MF.insert(It, DoneMBB);
1061
1062 // Splice the instructions after the readcyclecounter into DoneMBB, notifying
1063 // the observer about each moved instruction so CSEInfo stays consistent.
1064 for (MachineBasicBlock::iterator I = std::next(MI.getIterator()),
1065 E = BB->end();
1066 I != E; ++I)
1067 Observer.changingInstr(*I);
1068 DoneMBB->splice(DoneMBB->begin(), BB,
1069 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1070 for (MachineInstr &MovedMI : DoneMBB->instrs())
1071 Observer.changedInstr(MovedMI);
1073 BB->addSuccessor(LoopMBB);
1074
1075 LLT i32 = LLT::integer(32);
1076 // Generic vregs carry the s32 type for G_MERGE_VALUES below, but are also
1077 // constrained to GPR so the target CSRRS/BNE instructions satisfy the
1078 // verifier's register-class constraints.
1079 auto CreateGPR = [&]() {
1081 MRI.setRegClass(R, &RISCV::GPRRegClass);
1082 return R;
1083 };
1084 Register LoReg = CreateGPR();
1085 Register HiReg = CreateGPR();
1086 Register ReadAgainReg = CreateGPR();
1087
1088 // read:
1089 // csrrs HiReg, counterh # high word
1090 // csrrs LoReg, counter # low word
1091 // csrrs ReadAgainReg, counterh
1092 // bne HiReg, ReadAgainReg, read
1093 // Emit the target instructions directly with BuildMI.
1094 const RISCVInstrInfo *TII = STI.getInstrInfo();
1095 BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), HiReg)
1096 .addImm(HiCounter)
1097 .addReg(RISCV::X0);
1098 BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), LoReg)
1099 .addImm(LoCounter)
1100 .addReg(RISCV::X0);
1101 BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), ReadAgainReg)
1102 .addImm(HiCounter)
1103 .addReg(RISCV::X0);
1104
1105 BuildMI(LoopMBB, DL, TII->get(RISCV::BNE))
1106 .addReg(HiReg)
1107 .addReg(ReadAgainReg)
1108 .addMBB(LoopMBB);
1109
1110 LoopMBB->addSuccessor(LoopMBB);
1111 LoopMBB->addSuccessor(DoneMBB);
1112
1113 // Re-pair the two halves into the 64-bit result.
1114 Register DstReg = MI.getOperand(0).getReg();
1115 Observer.erasingInstr(MI);
1116 MI.eraseFromParent();
1117
1118 MIRBuilder.setInsertPt(*DoneMBB, DoneMBB->begin());
1119 MIRBuilder.setDebugLoc(DL);
1120 MIRBuilder.buildMergeValues(DstReg, {LoReg, HiReg});
1121 return true;
1122}
1123
1124bool RISCVLegalizerInfo::legalizeBRJT(MachineInstr &MI,
1125 MachineIRBuilder &MIRBuilder) const {
1126 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
1127 auto &MF = *MI.getParent()->getParent();
1128 const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
1129 unsigned EntrySize = MJTI->getEntrySize(MF.getDataLayout());
1130
1131 Register PtrReg = MI.getOperand(0).getReg();
1132 LLT PtrTy = MRI.getType(PtrReg);
1133 Register IndexReg = MI.getOperand(2).getReg();
1134 LLT IndexTy = MRI.getType(IndexReg);
1135
1136 if (!isPowerOf2_32(EntrySize))
1137 return false;
1138
1139 auto ShiftAmt = MIRBuilder.buildConstant(IndexTy, Log2_32(EntrySize));
1140 IndexReg = MIRBuilder.buildShl(IndexTy, IndexReg, ShiftAmt).getReg(0);
1141
1142 auto Addr = MIRBuilder.buildPtrAdd(PtrTy, PtrReg, IndexReg);
1143
1144 MachineMemOperand *MMO = MF.getMachineMemOperand(
1146 EntrySize, Align(MJTI->getEntryAlignment(MF.getDataLayout())));
1147
1148 Register TargetReg;
1149 switch (MJTI->getEntryKind()) {
1150 default:
1151 return false;
1153 // For PIC, the sequence is:
1154 // BRIND(load(Jumptable + index) + RelocBase)
1155 // RelocBase can be JumpTable, GOT or some sort of global base.
1156 unsigned LoadOpc =
1157 STI.is64Bit() ? TargetOpcode::G_SEXTLOAD : TargetOpcode::G_LOAD;
1158 auto Load = MIRBuilder.buildLoadInstr(LoadOpc, IndexTy, Addr, *MMO);
1159 TargetReg = MIRBuilder.buildPtrAdd(PtrTy, PtrReg, Load).getReg(0);
1160 break;
1161 }
1163 auto Load = MIRBuilder.buildLoadInstr(TargetOpcode::G_SEXTLOAD, IndexTy,
1164 Addr, *MMO);
1165 TargetReg = MIRBuilder.buildIntToPtr(PtrTy, Load).getReg(0);
1166 break;
1167 }
1169 TargetReg = MIRBuilder.buildLoad(PtrTy, Addr, *MMO).getReg(0);
1170 break;
1171 }
1172
1173 if (MF.getInfo<RISCVMachineFunctionInfo>()->hasCFProtectionBranch()) {
1174 // When cf-protection-branch is enabled, we need to use a software
1175 // guarded branch for jump table branch.
1176 MIRBuilder.buildInstr(RISCV::G_SW_GUARDED_BRIND, {}, {TargetReg});
1177 } else {
1178 MIRBuilder.buildBrIndirect(TargetReg);
1179 }
1180
1181 MI.eraseFromParent();
1182 return true;
1183}
1184
1185bool RISCVLegalizerInfo::shouldBeInConstantPool(const APInt &APImm,
1186 bool ShouldOptForSize) const {
1187 assert(APImm.getBitWidth() == 32 || APImm.getBitWidth() == 64);
1188 int64_t Imm = APImm.getSExtValue();
1189 // All simm32 constants should be handled by isel.
1190 // NOTE: The getMaxBuildIntsCost call below should return a value >= 2 making
1191 // this check redundant, but small immediates are common so this check
1192 // should have better compile time.
1193 if (isInt<32>(Imm))
1194 return false;
1195
1196 // We only need to cost the immediate, if constant pool lowering is enabled.
1197 if (!STI.useConstantPoolForLargeInts())
1198 return false;
1199
1201 if (Seq.size() <= STI.getMaxBuildIntsCost())
1202 return false;
1203
1204 // Optimizations below are disabled for opt size. If we're optimizing for
1205 // size, use a constant pool.
1206 if (ShouldOptForSize)
1207 return true;
1208 //
1209 // Special case. See if we can build the constant as (ADD (SLLI X, C), X) do
1210 // that if it will avoid a constant pool.
1211 // It will require an extra temporary register though.
1212 // If we have Zba we can use (ADD_UW X, (SLLI X, 32)) to handle cases where
1213 // low and high 32 bits are the same and bit 31 and 63 are set.
1214 unsigned ShiftAmt, AddOpc;
1215 RISCVMatInt::InstSeq SeqLo =
1216 RISCVMatInt::generateTwoRegInstSeq(Imm, STI, ShiftAmt, AddOpc);
1217 return !(!SeqLo.empty() && (SeqLo.size() + 2) <= STI.getMaxBuildIntsCost());
1218}
1219
1220bool RISCVLegalizerInfo::legalizeVScale(MachineInstr &MI,
1221 MachineIRBuilder &MIB) const {
1222 Register Dst = MI.getOperand(0).getReg();
1223
1224 // We define our scalable vector types for lmul=1 to use a 64 bit known
1225 // minimum size. e.g. <vscale x 2 x i32>. VLENB is in bytes so we calculate
1226 // vscale as VLENB / 8.
1227 static_assert(RISCV::RVVBitsPerBlock == 64, "Unexpected bits per block!");
1228 if (STI.getRealMinVLen() < RISCV::RVVBitsPerBlock)
1229 // Support for VLEN==32 is incomplete.
1230 return false;
1231
1232 // We assume VLENB is a multiple of 8. We manually choose the best shift
1233 // here because SimplifyDemandedBits isn't always able to simplify it.
1234 uint64_t Val = MI.getOperand(1).getCImm()->getZExtValue();
1235 if (isPowerOf2_64(Val)) {
1236 uint64_t Log2 = Log2_64(Val);
1237 if (Log2 < 3) {
1238 auto VLENB = MIB.buildInstr(RISCV::G_READ_VLENB, {iXLen}, {});
1239 MIB.buildLShr(Dst, VLENB, MIB.buildConstant(iXLen, 3 - Log2),
1241 } else if (Log2 > 3) {
1242 auto VLENB = MIB.buildInstr(RISCV::G_READ_VLENB, {iXLen}, {});
1243 MIB.buildShl(Dst, VLENB, MIB.buildConstant(iXLen, Log2 - 3));
1244 } else {
1245 MIB.buildInstr(RISCV::G_READ_VLENB, {Dst}, {});
1246 }
1247 } else if ((Val % 8) == 0) {
1248 // If the multiplier is a multiple of 8, scale it down to avoid needing
1249 // to shift the VLENB value.
1250 auto VLENB = MIB.buildInstr(RISCV::G_READ_VLENB, {iXLen}, {});
1251 MIB.buildMul(Dst, VLENB, MIB.buildConstant(iXLen, Val / 8));
1252 } else {
1253 auto VLENB = MIB.buildInstr(RISCV::G_READ_VLENB, {iXLen}, {});
1254 auto VScale = MIB.buildLShr(iXLen, VLENB, MIB.buildConstant(iXLen, 3),
1256 MIB.buildMul(Dst, VScale, MIB.buildConstant(iXLen, Val));
1257 }
1258 MI.eraseFromParent();
1259 return true;
1260}
1261
1262// Custom-lower extensions from mask vectors by using a vselect either with 1
1263// for zero/any-extension or -1 for sign-extension:
1264// (vXiN = (s|z)ext vXi1:vmask) -> (vXiN = vselect vmask, (-1 or 1), 0)
1265// Note that any-extension is lowered identically to zero-extension.
1266bool RISCVLegalizerInfo::legalizeExt(MachineInstr &MI,
1267 MachineIRBuilder &MIB) const {
1268
1269 unsigned Opc = MI.getOpcode();
1270 assert(Opc == TargetOpcode::G_ZEXT || Opc == TargetOpcode::G_SEXT ||
1271 Opc == TargetOpcode::G_ANYEXT);
1272
1273 MachineRegisterInfo &MRI = *MIB.getMRI();
1274 Register Dst = MI.getOperand(0).getReg();
1275 Register Src = MI.getOperand(1).getReg();
1276
1277 LLT DstTy = MRI.getType(Dst);
1278 int64_t ExtTrueVal = Opc == TargetOpcode::G_SEXT ? -1 : 1;
1279 LLT DstEltTy = DstTy.getElementType();
1280 auto SplatZero = MIB.buildSplatVector(DstTy, MIB.buildConstant(DstEltTy, 0));
1281 auto SplatTrue =
1282 MIB.buildSplatVector(DstTy, MIB.buildConstant(DstEltTy, ExtTrueVal));
1283 MIB.buildSelect(Dst, Src, SplatTrue, SplatZero);
1284
1285 MI.eraseFromParent();
1286 return true;
1287}
1288
1289bool RISCVLegalizerInfo::legalizeLoadStore(MachineInstr &MI,
1290 LegalizerHelper &Helper,
1291 MachineIRBuilder &MIB) const {
1293 "Machine instructions must be Load/Store.");
1294 MachineRegisterInfo &MRI = *MIB.getMRI();
1295 MachineFunction *MF = MI.getMF();
1296 const DataLayout &DL = MIB.getDataLayout();
1297 LLVMContext &Ctx = MF->getFunction().getContext();
1298
1299 Register DstReg = MI.getOperand(0).getReg();
1300 LLT DataTy = MRI.getType(DstReg);
1301 if (!DataTy.isVector())
1302 return false;
1303
1304 if (!MI.hasOneMemOperand())
1305 return false;
1306
1307 MachineMemOperand *MMO = *MI.memoperands_begin();
1308
1309 const auto *TLI = STI.getTargetLowering();
1310 EVT VT = EVT::getEVT(getTypeForLLT(DataTy, Ctx));
1311
1312 if (TLI->allowsMemoryAccessForAlignment(Ctx, DL, VT, *MMO))
1313 return true;
1314
1315 unsigned EltSizeBits = DataTy.getScalarSizeInBits();
1316 assert((EltSizeBits == 16 || EltSizeBits == 32 || EltSizeBits == 64) &&
1317 "Unexpected unaligned RVV load type");
1318
1319 // Calculate the new vector type with i8 elements
1320 unsigned NumElements =
1321 DataTy.getElementCount().getKnownMinValue() * (EltSizeBits / 8);
1322 LLT NewDataTy = LLT::scalable_vector(NumElements, 8);
1323
1324 Helper.bitcast(MI, 0, NewDataTy);
1325
1326 return true;
1327}
1328
1329/// Return the type of the mask type suitable for masking the provided
1330/// vector type. This is simply an i1 element type vector of the same
1331/// (possibly scalable) length.
1332static LLT getMaskTypeFor(LLT VecTy) {
1333 assert(VecTy.isVector());
1334 ElementCount EC = VecTy.getElementCount();
1335 return LLT::vector(EC, LLT::integer(1));
1336}
1337
1338/// Creates an all ones mask suitable for masking a vector of type VecTy with
1339/// vector length VL.
1341 MachineIRBuilder &MIB,
1343 LLT MaskTy = getMaskTypeFor(VecTy);
1344 return MIB.buildInstr(RISCV::G_VMSET_VL, {MaskTy}, {VL});
1345}
1346
1347/// Gets the two common "VL" operands: an all-ones mask and the vector length.
1348/// VecTy is a scalable vector type.
1349static std::pair<MachineInstrBuilder, MachineInstrBuilder>
1351 assert(VecTy.isScalableVector() && "Expecting scalable container type");
1352 const RISCVSubtarget &STI = MIB.getMF().getSubtarget<RISCVSubtarget>();
1353 LLT XLenTy(STI.getXLenVT());
1354 auto VL = MIB.buildConstant(XLenTy, -1);
1355 auto Mask = buildAllOnesMask(VecTy, VL, MIB, MRI);
1356 return {Mask, VL};
1357}
1358
1360buildSplatPartsS64WithVL(const DstOp &Dst, const SrcOp &Passthru, Register Lo,
1361 Register Hi, const SrcOp &VL, MachineIRBuilder &MIB,
1363 // TODO: If the Hi bits of the splat are undefined, then it's fine to just
1364 // splat Lo even if it might be sign extended. I don't think we have
1365 // introduced a case where we're build a s64 where the upper bits are undef
1366 // yet.
1367
1368 // Fall back to a stack store and stride x0 vector load.
1369 // TODO: need to lower G_SPLAT_VECTOR_SPLIT_I64. This is done in
1370 // preprocessDAG in SDAG.
1371 return MIB.buildInstr(RISCV::G_SPLAT_VECTOR_SPLIT_I64_VL, {Dst},
1372 {Passthru, Lo, Hi, VL});
1373}
1374
1376buildSplatSplitS64WithVL(const DstOp &Dst, const SrcOp &Passthru,
1377 const SrcOp &Scalar, const SrcOp &VL,
1379 assert(Scalar.getLLTTy(MRI) == LLT::scalar(64) && "Unexpected VecTy!");
1380 auto Unmerge = MIB.buildUnmerge(LLT::integer(32), Scalar);
1381 return buildSplatPartsS64WithVL(Dst, Passthru, Unmerge.getReg(0),
1382 Unmerge.getReg(1), VL, MIB, MRI);
1383}
1384
1385// Lower splats of s1 types to G_ICMP. For each mask vector type, we have a
1386// legal equivalently-sized i8 type, so we can use that as a go-between.
1387// Splats of s1 types that have constant value can be legalized as VMSET_VL or
1388// VMCLR_VL.
1389bool RISCVLegalizerInfo::legalizeSplatVector(MachineInstr &MI,
1390 MachineIRBuilder &MIB) const {
1391 assert(MI.getOpcode() == TargetOpcode::G_SPLAT_VECTOR);
1392
1393 MachineRegisterInfo &MRI = *MIB.getMRI();
1394
1395 Register Dst = MI.getOperand(0).getReg();
1396 Register SplatVal = MI.getOperand(1).getReg();
1397
1398 LLT VecTy = MRI.getType(Dst);
1399 LLT XLenTy(STI.getXLenVT());
1400
1401 // Handle case of s64 element vectors on rv32
1402 if (XLenTy.getSizeInBits() == 32 &&
1403 VecTy.getElementType().getSizeInBits() == 64) {
1404 auto [_, VL] = buildDefaultVLOps(MRI.getType(Dst), MIB, MRI);
1405 buildSplatSplitS64WithVL(Dst, MIB.buildUndef(VecTy), SplatVal, VL, MIB,
1406 MRI);
1407 MI.eraseFromParent();
1408 return true;
1409 }
1410
1411 // All-zeros or all-ones splats are handled specially.
1412 MachineInstr &SplatValMI = *MRI.getVRegDef(SplatVal);
1413 if (isAllOnesOrAllOnesSplat(SplatValMI, MRI)) {
1414 auto VL = buildDefaultVLOps(VecTy, MIB, MRI).second;
1415 MIB.buildInstr(RISCV::G_VMSET_VL, {Dst}, {VL});
1416 MI.eraseFromParent();
1417 return true;
1418 }
1419 if (isNullOrNullSplat(SplatValMI, MRI)) {
1420 auto VL = buildDefaultVLOps(VecTy, MIB, MRI).second;
1421 MIB.buildInstr(RISCV::G_VMCLR_VL, {Dst}, {VL});
1422 MI.eraseFromParent();
1423 return true;
1424 }
1425
1426 // Handle non-constant mask splat (i.e. not sure if it's all zeros or all
1427 // ones) by promoting it to an s8 splat.
1428 LLT InterEltTy = LLT::integer(8);
1429 LLT InterTy = VecTy.changeElementType(InterEltTy);
1430 auto ZExtSplatVal = MIB.buildZExt(InterEltTy, SplatVal);
1431 auto And =
1432 MIB.buildAnd(InterEltTy, ZExtSplatVal, MIB.buildConstant(InterEltTy, 1));
1433 auto LHS = MIB.buildSplatVector(InterTy, And);
1434 auto ZeroSplat =
1435 MIB.buildSplatVector(InterTy, MIB.buildConstant(InterEltTy, 0));
1436 MIB.buildICmp(CmpInst::Predicate::ICMP_NE, Dst, LHS, ZeroSplat);
1437 MI.eraseFromParent();
1438 return true;
1439}
1440
1441static LLT getLMUL1Ty(LLT VecTy) {
1442 assert(VecTy.getElementType().getSizeInBits() <= 64 &&
1443 "Unexpected vector LLT");
1445 VecTy.getElementType().getSizeInBits(),
1446 VecTy.getElementType());
1447}
1448
1449bool RISCVLegalizerInfo::legalizeExtractSubvector(MachineInstr &MI,
1450 MachineIRBuilder &MIB) const {
1451 GExtractSubvector &ES = cast<GExtractSubvector>(MI);
1452
1453 MachineRegisterInfo &MRI = *MIB.getMRI();
1454
1455 Register Dst = ES.getReg(0);
1456 Register Src = ES.getSrcVec();
1457 uint64_t Idx = ES.getIndexImm();
1458
1459 // With an index of 0 this is a cast-like subvector, which can be performed
1460 // with subregister operations.
1461 if (Idx == 0)
1462 return true;
1463
1464 LLT LitTy = MRI.getType(Dst);
1465 LLT BigTy = MRI.getType(Src);
1466
1467 if (LitTy.getElementType() == LLT::scalar(1)) {
1468 // We can't slide this mask vector up indexed by its i1 elements.
1469 // This poses a problem when we wish to insert a scalable vector which
1470 // can't be re-expressed as a larger type. Just choose the slow path and
1471 // extend to a larger type, then truncate back down.
1472 LLT ExtBigTy = BigTy.changeElementType(LLT::integer(8));
1473 LLT ExtLitTy = LitTy.changeElementType(LLT::integer(8));
1474 auto BigZExt = MIB.buildZExt(ExtBigTy, Src);
1475 auto ExtractZExt = MIB.buildExtractSubvector(ExtLitTy, BigZExt, Idx);
1476 auto SplatZero = MIB.buildSplatVector(
1477 ExtLitTy, MIB.buildConstant(ExtLitTy.getElementType(), 0));
1478 MIB.buildICmp(CmpInst::Predicate::ICMP_NE, Dst, ExtractZExt, SplatZero);
1479 MI.eraseFromParent();
1480 return true;
1481 }
1482
1483 // extract_subvector scales the index by vscale if the subvector is scalable,
1484 // and decomposeSubvectorInsertExtractToSubRegs takes this into account.
1485 const RISCVRegisterInfo *TRI = STI.getRegisterInfo();
1486 MVT LitTyMVT = getMVTForLLT(LitTy);
1487 auto Decompose =
1489 getMVTForLLT(BigTy), LitTyMVT, Idx, TRI);
1490 unsigned RemIdx = Decompose.second;
1491
1492 // If the Idx has been completely eliminated then this is a subvector extract
1493 // which naturally aligns to a vector register. These can easily be handled
1494 // using subregister manipulation.
1495 if (RemIdx == 0)
1496 return true;
1497
1498 // Else LitTy is M1 or smaller and may need to be slid down: if LitTy
1499 // was > M1 then the index would need to be a multiple of VLMAX, and so would
1500 // divide exactly.
1501 assert(
1504
1505 // If the vector type is an LMUL-group type, extract a subvector equal to the
1506 // nearest full vector register type.
1507 LLT InterLitTy = BigTy;
1508 Register Vec = Src;
1510 getLMUL1Ty(BigTy).getSizeInBits())) {
1511 // If BigTy has an LMUL > 1, then LitTy should have a smaller LMUL, and
1512 // we should have successfully decomposed the extract into a subregister.
1513 assert(Decompose.first != RISCV::NoSubRegister);
1514 InterLitTy = getLMUL1Ty(BigTy);
1515 // SDAG builds a TargetExtractSubreg. We cannot create a a Copy with SubReg
1516 // specified on the source Register (the equivalent) since generic virtual
1517 // register does not allow subregister index.
1518 Vec = MIB.buildExtractSubvector(InterLitTy, Src, Idx - RemIdx).getReg(0);
1519 }
1520
1521 // Slide this vector register down by the desired number of elements in order
1522 // to place the desired subvector starting at element 0.
1523 const LLT XLenTy(STI.getXLenVT());
1524 auto SlidedownAmt = MIB.buildVScale(XLenTy, RemIdx);
1525 auto [Mask, VL] = buildDefaultVLOps(InterLitTy, MIB, MRI);
1527 auto Slidedown = MIB.buildInstr(
1528 RISCV::G_VSLIDEDOWN_VL, {InterLitTy},
1529 {MIB.buildUndef(InterLitTy), Vec, SlidedownAmt, Mask, VL, Policy});
1530
1531 // Now the vector is in the right position, extract our final subvector. This
1532 // should resolve to a COPY.
1533 MIB.buildExtractSubvector(Dst, Slidedown, 0);
1534
1535 MI.eraseFromParent();
1536 return true;
1537}
1538
1539bool RISCVLegalizerInfo::legalizeInsertSubvector(MachineInstr &MI,
1540 LegalizerHelper &Helper,
1541 MachineIRBuilder &MIB) const {
1542 GInsertSubvector &IS = cast<GInsertSubvector>(MI);
1543
1544 MachineRegisterInfo &MRI = *MIB.getMRI();
1545
1546 Register Dst = IS.getReg(0);
1547 Register BigVec = IS.getBigVec();
1548 Register LitVec = IS.getSubVec();
1549 uint64_t Idx = IS.getIndexImm();
1550
1551 LLT BigTy = MRI.getType(BigVec);
1552 LLT LitTy = MRI.getType(LitVec);
1553
1554 if (Idx == 0 && mi_match(BigVec, MRI, m_GImplicitDef()))
1555 return true;
1556
1557 // We don't have the ability to slide mask vectors up indexed by their i1
1558 // elements; the smallest we can do is i8. Often we are able to bitcast to
1559 // equivalent i8 vectors. Otherwise, we can must zeroextend to equivalent i8
1560 // vectors and truncate down after the insert.
1561 if (LitTy.getElementType() == LLT::scalar(1)) {
1562 auto BigTyMinElts = BigTy.getElementCount().getKnownMinValue();
1563 auto LitTyMinElts = LitTy.getElementCount().getKnownMinValue();
1564 if (BigTyMinElts >= 8 && LitTyMinElts >= 8)
1565 return Helper.bitcast(
1566 IS, 0,
1568
1569 // We can't slide this mask vector up indexed by its i1 elements.
1570 // This poses a problem when we wish to insert a scalable vector which
1571 // can't be re-expressed as a larger type. Just choose the slow path and
1572 // extend to a larger type, then truncate back down.
1573 LLT ExtBigTy = BigTy.changeElementType(LLT::integer(8));
1574 return Helper.widenScalar(IS, 0, ExtBigTy);
1575 }
1576
1577 const RISCVRegisterInfo *TRI = STI.getRegisterInfo();
1578 unsigned SubRegIdx, RemIdx;
1579 std::tie(SubRegIdx, RemIdx) =
1581 getMVTForLLT(BigTy), getMVTForLLT(LitTy), Idx, TRI);
1582
1583 TypeSize VecRegSize = TypeSize::getScalable(RISCV::RVVBitsPerBlock);
1585 STI.expandVScale(LitTy.getSizeInBits()).getKnownMinValue()));
1586 bool ExactlyVecRegSized =
1587 STI.expandVScale(LitTy.getSizeInBits())
1588 .isKnownMultipleOf(STI.expandVScale(VecRegSize));
1589
1590 // If the Idx has been completely eliminated and this subvector's size is a
1591 // vector register or a multiple thereof, or the surrounding elements are
1592 // undef, then this is a subvector insert which naturally aligns to a vector
1593 // register. These can easily be handled using subregister manipulation.
1594 if (RemIdx == 0 && ExactlyVecRegSized)
1595 return true;
1596
1597 // If the subvector is smaller than a vector register, then the insertion
1598 // must preserve the undisturbed elements of the register. We do this by
1599 // lowering to an EXTRACT_SUBVECTOR grabbing the nearest LMUL=1 vector type
1600 // (which resolves to a subregister copy), performing a VSLIDEUP to place the
1601 // subvector within the vector register, and an INSERT_SUBVECTOR of that
1602 // LMUL=1 type back into the larger vector (resolving to another subregister
1603 // operation). See below for how our VSLIDEUP works. We go via a LMUL=1 type
1604 // to avoid allocating a large register group to hold our subvector.
1605
1606 // VSLIDEUP works by leaving elements 0<i<OFFSET undisturbed, elements
1607 // OFFSET<=i<VL set to the "subvector" and vl<=i<VLMAX set to the tail policy
1608 // (in our case undisturbed). This means we can set up a subvector insertion
1609 // where OFFSET is the insertion offset, and the VL is the OFFSET plus the
1610 // size of the subvector.
1611 const LLT XLenTy(STI.getXLenVT());
1612 LLT InterLitTy = BigTy;
1613 Register AlignedExtract = BigVec;
1614 unsigned AlignedIdx = Idx - RemIdx;
1616 getLMUL1Ty(BigTy).getSizeInBits())) {
1617 InterLitTy = getLMUL1Ty(BigTy);
1618 // Extract a subvector equal to the nearest full vector register type. This
1619 // should resolve to a G_EXTRACT on a subreg.
1620 AlignedExtract =
1621 MIB.buildExtractSubvector(InterLitTy, BigVec, AlignedIdx).getReg(0);
1622 }
1623
1624 auto Insert = MIB.buildInsertSubvector(InterLitTy, MIB.buildUndef(InterLitTy),
1625 LitVec, 0);
1626
1627 auto [Mask, _] = buildDefaultVLOps(InterLitTy, MIB, MRI);
1628 auto VL = MIB.buildVScale(XLenTy, LitTy.getElementCount().getKnownMinValue());
1629
1630 // If we're inserting into the lowest elements, use a tail undisturbed
1631 // vmv.v.v.
1632 MachineInstrBuilder Inserted;
1633 bool NeedInsertSubvec =
1634 TypeSize::isKnownGT(BigTy.getSizeInBits(), InterLitTy.getSizeInBits());
1635 Register InsertedDst =
1636 NeedInsertSubvec ? MRI.createGenericVirtualRegister(InterLitTy) : Dst;
1637 if (RemIdx == 0) {
1638 Inserted = MIB.buildInstr(RISCV::G_VMV_V_V_VL, {InsertedDst},
1639 {AlignedExtract, Insert, VL});
1640 } else {
1641 auto SlideupAmt = MIB.buildVScale(XLenTy, RemIdx);
1642 // Construct the vector length corresponding to RemIdx + length(LitTy).
1643 VL = MIB.buildAdd(XLenTy, SlideupAmt, VL);
1644 // Use tail agnostic policy if we're inserting over InterLitTy's tail.
1645 ElementCount EndIndex =
1648 if (STI.expandVScale(EndIndex) ==
1649 STI.expandVScale(InterLitTy.getElementCount()))
1651
1652 Inserted =
1653 MIB.buildInstr(RISCV::G_VSLIDEUP_VL, {InsertedDst},
1654 {AlignedExtract, Insert, SlideupAmt, Mask, VL, Policy});
1655 }
1656
1657 // If required, insert this subvector back into the correct vector register.
1658 // This should resolve to an INSERT_SUBREG instruction.
1659 if (NeedInsertSubvec)
1660 MIB.buildInsertSubvector(Dst, BigVec, Inserted, AlignedIdx);
1661
1662 MI.eraseFromParent();
1663 return true;
1664}
1665
1666bool RISCVLegalizerInfo::legalizeBitreverse(MachineInstr &MI,
1667 MachineIRBuilder &MIB) const {
1668 assert(MI.getOpcode() == TargetOpcode::G_BITREVERSE && "Unexpected opcode");
1669
1670 if (!STI.hasStdExtZbkb())
1671 return false;
1672
1673 MachineRegisterInfo &MRI = *MIB.getMRI();
1674
1675 Register Dst = MI.getOperand(0).getReg();
1676 Register Src = MI.getOperand(1).getReg();
1677
1678 if (!MRI.getType(Dst).isScalar(8))
1679 return false;
1680
1681 auto WideSrc = MIB.buildAnyExt(iXLen, Src);
1682 auto Brev = MIB.buildInstr(RISCV::G_BREV8, {iXLen}, {WideSrc.getReg(0)});
1683 MIB.buildTrunc(Dst, Brev.getReg(0));
1684
1685 MI.eraseFromParent();
1686 return true;
1687}
1688
1689static unsigned getRISCVWOpcode(unsigned Opcode) {
1690 switch (Opcode) {
1691 default:
1692 llvm_unreachable("Unexpected opcode");
1693 case TargetOpcode::G_ASHR:
1694 return RISCV::G_SRAW;
1695 case TargetOpcode::G_LSHR:
1696 return RISCV::G_SRLW;
1697 case TargetOpcode::G_SHL:
1698 return RISCV::G_SLLW;
1699 case TargetOpcode::G_SDIV:
1700 return RISCV::G_DIVW;
1701 case TargetOpcode::G_UDIV:
1702 return RISCV::G_DIVUW;
1703 case TargetOpcode::G_UREM:
1704 return RISCV::G_REMUW;
1705 case TargetOpcode::G_ROTL:
1706 return RISCV::G_ROLW;
1707 case TargetOpcode::G_ROTR:
1708 return RISCV::G_RORW;
1709 case TargetOpcode::G_CTLZ:
1710 return RISCV::G_CLZW;
1711 case TargetOpcode::G_CTTZ:
1712 return RISCV::G_CTZW;
1713 case TargetOpcode::G_CTLS:
1714 return RISCV::G_CLSW;
1715 case TargetOpcode::G_FPTOSI:
1716 return RISCV::G_FCVT_W_RV64;
1717 case TargetOpcode::G_FPTOUI:
1718 return RISCV::G_FCVT_WU_RV64;
1719 }
1720}
1721
1724 LostDebugLocObserver &LocObserver) const {
1725 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
1726 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
1727 MachineFunction &MF = *MI.getParent()->getParent();
1728 switch (MI.getOpcode()) {
1729 default:
1730 // No idea what to do.
1731 return false;
1732 case TargetOpcode::G_ABS:
1733 return Helper.lowerAbsToMaxNeg(MI);
1734 case TargetOpcode::G_CLMULH:
1735 case TargetOpcode::G_CLMULR: {
1736 assert(STI.is64Bit() &&
1737 MRI.getType(MI.getOperand(0).getReg()) == LLT::scalar(32) &&
1738 "Unexpected custom legalization");
1739 // Shift both inputs by 32 so the full product has 64 trailing zeros.
1740 // Perform CLMULH or CLMULR on the shifted inputs, then extract the upper
1741 // 32 bits of the result.
1742 auto Shift = MIRBuilder.buildConstant(iXLen, 32);
1743 auto LHS = MIRBuilder.buildAnyExt(iXLen, MI.getOperand(1));
1744 auto RHS = MIRBuilder.buildAnyExt(iXLen, MI.getOperand(2));
1745 auto ShiftedLHS = MIRBuilder.buildShl(iXLen, LHS, Shift);
1746 auto ShiftedRHS = MIRBuilder.buildShl(iXLen, RHS, Shift);
1747 auto Product = MIRBuilder.buildInstr(MI.getOpcode(), {iXLen},
1748 {ShiftedLHS, ShiftedRHS});
1749 auto High = MIRBuilder.buildLShr(iXLen, Product, Shift);
1750 MIRBuilder.buildTrunc(MI.getOperand(0), High);
1751 MI.eraseFromParent();
1752 return true;
1753 }
1754 case TargetOpcode::G_CONSTANT: {
1755 const Function &F = MF.getFunction();
1756 // TODO: if PSI and BFI are present, add " ||
1757 // llvm::shouldOptForSize(*CurMBB, PSI, BFI)".
1758 bool ShouldOptForSize = F.hasOptSize();
1759 const ConstantInt *ConstVal = MI.getOperand(1).getCImm();
1760 if (!shouldBeInConstantPool(ConstVal->getValue(), ShouldOptForSize))
1761 return true;
1762 return Helper.lowerConstant(MI);
1763 }
1764 case TargetOpcode::G_SUB:
1765 case TargetOpcode::G_ADD: {
1766 Helper.Observer.changingInstr(MI);
1767 // The G_ANYEXTs below leave the new high bits unconstrained, so no-wrap
1768 // claims proved at the narrow width no longer hold.
1770 Helper.widenScalarSrc(MI, iXLen, 1, TargetOpcode::G_ANYEXT);
1771 Helper.widenScalarSrc(MI, iXLen, 2, TargetOpcode::G_ANYEXT);
1772
1773 Register DstALU = MRI.createGenericVirtualRegister(iXLen);
1774
1775 MachineOperand &MO = MI.getOperand(0);
1776 MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
1777 auto DstSext = MIRBuilder.buildSExtInReg(iXLen, DstALU, 32);
1778
1779 MIRBuilder.buildInstr(TargetOpcode::G_TRUNC, {MO}, {DstSext});
1780 MO.setReg(DstALU);
1781
1782 Helper.Observer.changedInstr(MI);
1783 return true;
1784 }
1785 case TargetOpcode::G_ASHR:
1786 case TargetOpcode::G_LSHR:
1787 case TargetOpcode::G_SHL: {
1788 if (getIConstantVRegValWithLookThrough(MI.getOperand(2).getReg(), MRI)) {
1789 // We don't need a custom node for shift by constant. Just widen the
1790 // source and the shift amount.
1791 unsigned ExtOpc = TargetOpcode::G_ANYEXT;
1792 if (MI.getOpcode() == TargetOpcode::G_ASHR)
1793 ExtOpc = TargetOpcode::G_SEXT;
1794 else if (MI.getOpcode() == TargetOpcode::G_LSHR)
1795 ExtOpc = TargetOpcode::G_ZEXT;
1796
1797 Helper.Observer.changingInstr(MI);
1798 // The G_ANYEXT leave the new high bits unconstrained, so no-wrap
1799 // claims proved at the narrow width no longer hold.
1800 if (ExtOpc == TargetOpcode::G_ANYEXT)
1802 Helper.widenScalarSrc(MI, iXLen, 1, ExtOpc);
1803 Helper.widenScalarSrc(MI, iXLen, 2, TargetOpcode::G_ZEXT);
1804 Helper.widenScalarDst(MI, iXLen);
1805 Helper.Observer.changedInstr(MI);
1806 return true;
1807 }
1808
1809 Helper.Observer.changingInstr(MI);
1810 Helper.widenScalarSrc(MI, iXLen, 1, TargetOpcode::G_ANYEXT);
1811 Helper.widenScalarSrc(MI, iXLen, 2, TargetOpcode::G_ANYEXT);
1812 Helper.widenScalarDst(MI, iXLen);
1813 MI.setDesc(MIRBuilder.getTII().get(getRISCVWOpcode(MI.getOpcode())));
1814 Helper.Observer.changedInstr(MI);
1815 return true;
1816 }
1817 case TargetOpcode::G_SDIV:
1818 case TargetOpcode::G_UDIV:
1819 case TargetOpcode::G_UREM:
1820 case TargetOpcode::G_ROTL:
1821 case TargetOpcode::G_ROTR: {
1822 Helper.Observer.changingInstr(MI);
1823 Helper.widenScalarSrc(MI, iXLen, 1, TargetOpcode::G_ANYEXT);
1824 Helper.widenScalarSrc(MI, iXLen, 2, TargetOpcode::G_ANYEXT);
1825 Helper.widenScalarDst(MI, iXLen);
1826 MI.setDesc(MIRBuilder.getTII().get(getRISCVWOpcode(MI.getOpcode())));
1827 Helper.Observer.changedInstr(MI);
1828 return true;
1829 }
1830 case TargetOpcode::G_CTLZ:
1831 case TargetOpcode::G_CTTZ:
1832 case TargetOpcode::G_CTLS: {
1833 Helper.Observer.changingInstr(MI);
1834 Helper.widenScalarSrc(MI, iXLen, 1, TargetOpcode::G_ANYEXT);
1835 Helper.widenScalarDst(MI, iXLen);
1836 MI.setDesc(MIRBuilder.getTII().get(getRISCVWOpcode(MI.getOpcode())));
1837 Helper.Observer.changedInstr(MI);
1838 return true;
1839 }
1840 case TargetOpcode::G_FPTOSI:
1841 case TargetOpcode::G_FPTOUI: {
1842 Helper.Observer.changingInstr(MI);
1843 Helper.widenScalarDst(MI, iXLen);
1844 MI.setDesc(MIRBuilder.getTII().get(getRISCVWOpcode(MI.getOpcode())));
1846 Helper.Observer.changedInstr(MI);
1847 return true;
1848 }
1849 case TargetOpcode::G_FPTOSI_SAT:
1850 case TargetOpcode::G_FPTOUI_SAT: {
1851 // Mirror lowerFP_TO_INT_SAT (RISCVISelLowering.cpp): the conversion
1852 // saturates out-of-range values, but we need to select 0 for NaN as
1853 // required by the intrinsics.
1854 Register Dst = MI.getOperand(0).getReg();
1855 Register Src = MI.getOperand(1).getReg();
1856 LLT DstTy = MRI.getType(Dst);
1857 unsigned DstBits = DstTy.getScalarSizeInBits();
1858 bool IsSigned = MI.getOpcode() == TargetOpcode::G_FPTOSI_SAT;
1859
1860 unsigned FCvtOp;
1861 if (DstBits == XLen) {
1862 FCvtOp = IsSigned ? RISCV::G_FCVT_X : RISCV::G_FCVT_XU;
1863 } else {
1864 assert(STI.is64Bit() && "Non-XLen results are only custom on RV64");
1865 FCvtOp = IsSigned ? RISCV::G_FCVT_W_RV64 : RISCV::G_FCVT_WU_RV64;
1866 }
1867
1868 auto FCvt = MIRBuilder.buildInstr(FCvtOp, {iXLen}, {Src})
1869 .addImm(RISCVFPRndMode::RTZ);
1870
1871 // RISC-V G_FCMP results are XLen wide, and so are G_SELECT conditions.
1872 auto IsNaN = MIRBuilder.buildFCmp(CmpInst::FCMP_UNO, iXLen, Src, Src);
1873
1874 Register FCvtReg = FCvt.getReg(0);
1875 if (DstBits != XLen)
1876 FCvtReg = MIRBuilder.buildTrunc(DstTy, FCvtReg).getReg(0);
1877
1878 MIRBuilder.buildSelect(Dst, IsNaN, MIRBuilder.buildConstant(DstTy, 0),
1879 FCvtReg);
1880
1881 MI.eraseFromParent();
1882 return true;
1883 }
1884 case TargetOpcode::G_LROUND: {
1885 // The (i32 any_lround) Pat is IsRV32-only; on RV64 lower to
1886 // riscv_fcvt_w_rv64 with FRM_RMM.
1887 Helper.Observer.changingInstr(MI);
1888 Helper.widenScalarDst(MI, iXLen);
1889 MI.setDesc(MIRBuilder.getTII().get(RISCV::G_FCVT_W_RV64));
1891 Helper.Observer.changedInstr(MI);
1892 return true;
1893 }
1894 case TargetOpcode::G_READCYCLECOUNTER:
1895 case TargetOpcode::G_READSTEADYCOUNTER:
1896 return legalizeReadCounter(MI, MIRBuilder, Helper.Observer);
1897 case TargetOpcode::G_IS_FPCLASS: {
1898 Register GISFPCLASS = MI.getOperand(0).getReg();
1899 Register Src = MI.getOperand(1).getReg();
1900 const MachineOperand &ImmOp = MI.getOperand(2);
1901 MachineIRBuilder MIB(MI);
1902
1903 // Turn LLVM IR's floating point classes to that in RISC-V,
1904 // by simply rotating the 10-bit immediate right by two bits.
1905 APInt GFpClassImm(10, static_cast<uint64_t>(ImmOp.getImm()));
1906 auto FClassMask = MIB.buildConstant(iXLen, GFpClassImm.rotr(2).zext(XLen));
1907 auto ConstZero = MIB.buildConstant(iXLen, 0);
1908
1909 auto GFClass = MIB.buildInstr(RISCV::G_FCLASS, {iXLen}, {Src});
1910 auto And = MIB.buildAnd(iXLen, GFClass, FClassMask);
1911 MIB.buildICmp(CmpInst::ICMP_NE, GISFPCLASS, And, ConstZero);
1912
1913 MI.eraseFromParent();
1914 return true;
1915 }
1916 case TargetOpcode::G_BRJT:
1917 return legalizeBRJT(MI, MIRBuilder);
1918 case TargetOpcode::G_VASTART:
1919 return legalizeVAStart(MI, MIRBuilder);
1920 case TargetOpcode::G_VSCALE:
1921 return legalizeVScale(MI, MIRBuilder);
1922 case TargetOpcode::G_ZEXT:
1923 case TargetOpcode::G_SEXT:
1924 case TargetOpcode::G_ANYEXT:
1925 return legalizeExt(MI, MIRBuilder);
1926 case TargetOpcode::G_SPLAT_VECTOR:
1927 return legalizeSplatVector(MI, MIRBuilder);
1928 case TargetOpcode::G_EXTRACT_SUBVECTOR:
1929 return legalizeExtractSubvector(MI, MIRBuilder);
1930 case TargetOpcode::G_INSERT_SUBVECTOR:
1931 return legalizeInsertSubvector(MI, Helper, MIRBuilder);
1932 case TargetOpcode::G_BITREVERSE:
1933 return legalizeBitreverse(MI, MIRBuilder);
1934 case TargetOpcode::G_LOAD:
1935 case TargetOpcode::G_STORE:
1936 return legalizeLoadStore(MI, Helper, MIRBuilder);
1937 }
1938
1939 llvm_unreachable("expected switch to return");
1940}
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
bpf unwind lower
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
const HexagonInstrInfo * TII
#define _
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Contains matchers for matching SSA Machine Instructions.
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
This file declares the MachineIRBuilder class.
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
uint64_t High
uint64_t IntrinsicInst * II
#define P(N)
ppc ctr loops verify
MachineRegisterInfo & MRI
static LLT getLMUL1Ty(LLT VecTy)
static MachineInstrBuilder buildAllOnesMask(LLT VecTy, const SrcOp &VL, MachineIRBuilder &MIB, MachineRegisterInfo &MRI)
Creates an all ones mask suitable for masking a vector of type VecTy with vector length VL.
static std::pair< MachineInstrBuilder, MachineInstrBuilder > buildDefaultVLOps(LLT VecTy, MachineIRBuilder &MIB, MachineRegisterInfo &MRI)
Gets the two common "VL" operands: an all-ones mask and the vector length.
static LegalityPredicate typeIsLegalBoolVec(unsigned TypeIdx, std::initializer_list< LLT > BoolVecTys, const RISCVSubtarget &ST)
static MachineInstrBuilder buildSplatSplitS64WithVL(const DstOp &Dst, const SrcOp &Passthru, const SrcOp &Scalar, const SrcOp &VL, MachineIRBuilder &MIB, MachineRegisterInfo &MRI)
static LegalityPredicate typeIsLegalIntOrFPVec(unsigned TypeIdx, std::initializer_list< LLT > IntOrFPVecTys, const RISCVSubtarget &ST)
static MachineInstrBuilder buildSplatPartsS64WithVL(const DstOp &Dst, const SrcOp &Passthru, Register Lo, Register Hi, const SrcOp &VL, MachineIRBuilder &MIB, MachineRegisterInfo &MRI)
static LLT getMaskTypeFor(LLT VecTy)
Return the type of the mask type suitable for masking the provided vector type.
static LegalityPredicate typeIsLegalPtrVec(unsigned TypeIdx, std::initializer_list< LLT > PtrVecTys, const RISCVSubtarget &ST)
static unsigned getRISCVWOpcode(unsigned Opcode)
This file declares the targeting of the Machinelegalizer class for RISC-V.
MachineInstr *& Lo
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1057
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1199
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1582
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
This is the shared class of boolean and integer constants.
Definition Constants.h:87
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
static constexpr ElementCount getScalable(ScalarTy MinVal)
Definition TypeSize.h:309
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:352
Abstract class that contains various methods for clients to notify about changes.
virtual void changingInstr(MachineInstr &MI)=0
This instruction is about to be mutated in some way.
virtual void changedInstr(MachineInstr &MI)=0
This instruction was mutated in some way.
virtual void erasingInstr(MachineInstr &MI)=0
An instruction is about to be erased.
Register getReg(unsigned Idx) const
Access the Idx'th operand as a register and return it.
static constexpr LLT float64()
Get a 64-bit IEEE double value.
constexpr bool isScalableVector() const
Returns true if the LLT is a scalable vector.
constexpr unsigned getScalarSizeInBits() const
constexpr bool isScalar() const
static constexpr LLT scalable_vector(unsigned MinNumElements, unsigned ScalarSizeInBits)
Get a low-level scalable vector of some number of elements and element width.
constexpr LLT changeElementType(LLT NewEltTy) const
If this type is a vector, return a vector with the same number of elements but the new element type.
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
static constexpr LLT float128()
Get a 128-bit IEEE quad value.
constexpr bool isVector() const
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr ElementCount getElementCount() const
static constexpr LLT float16()
Get a 16-bit IEEE half value.
static LLT integer(unsigned SizeInBits)
static constexpr LLT bfloat16()
LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
static constexpr LLT float32()
Get a 32-bit IEEE float value.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
LegalizeRuleSet & maxScalar(unsigned TypeIdx, const LLT Ty)
Ensure the scalar is at most as wide as Ty.
LegalizeRuleSet & lower()
The instruction is lowered.
LegalizeRuleSet & clampScalar(unsigned TypeIdx, const LLT MinTy, const LLT MaxTy)
Limit the range of scalar sizes to MinTy and MaxTy.
LegalizeRuleSet & alwaysLegal()
LegalizeRuleSet & customIf(LegalityPredicate Predicate)
LegalizeRuleSet & widenScalarToNextPow2(unsigned TypeIdx, unsigned MinSize=0)
Widen the scalar to the next power of two that is at least MinSize.
LegalizeRuleSet & customFor(std::initializer_list< LLT > Types)
LLVM_ABI void widenScalarSrc(MachineInstr &MI, LLT WideTy, unsigned OpIdx, unsigned ExtOpcode)
Legalize a single operand OpIdx of the machine instruction MI as a Use by extending the operand's typ...
LLVM_ABI LegalizeResult lowerAbsToMaxNeg(MachineInstr &MI)
LLVM_ABI LegalizeResult bitcast(MachineInstr &MI, unsigned TypeIdx, LLT Ty)
Legalize an instruction by replacing the value type.
GISelChangeObserver & Observer
To keep track of changes made by the LegalizerHelper.
LLVM_ABI LegalizeResult widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy)
Legalize an instruction by performing the operation on a wider scalar type (for example a 16-bit addi...
MachineIRBuilder & MIRBuilder
Expose MIRBuilder so clients can set their own RecordInsertInstruction functions.
LLVM_ABI LegalizeResult lowerConstant(MachineInstr &MI)
LLVM_ABI void widenScalarDst(MachineInstr &MI, LLT WideTy, unsigned OpIdx=0, unsigned TruncOpcode=TargetOpcode::G_TRUNC)
Legalize a single operand OpIdx of the machine instruction MI as a Def by extending the operand's typ...
LegalizeRuleSet & getActionDefinitionsBuilder(unsigned Opcode)
Get the action definition builder for the given opcode.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition MCInstrInfo.h:89
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
Helper class to build MachineInstr.
void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II)
Set the insertion point before the specified position.
MachineInstrBuilder buildAdd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_ADD Op0, Op1.
MachineInstrBuilder buildUndef(const DstOp &Res)
Build and insert Res = IMPLICIT_DEF.
MachineInstrBuilder buildUnmerge(ArrayRef< LLT > Res, const SrcOp &Op)
Build and insert Res0, ... = G_UNMERGE_VALUES Op.
MachineInstrBuilder buildSelect(const DstOp &Res, const SrcOp &Tst, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_SELECT Tst, Op0, Op1.
MachineInstrBuilder buildMul(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_MUL Op0, Op1.
MachineInstrBuilder buildInsertSubvector(const DstOp &Res, const SrcOp &Src0, const SrcOp &Src1, unsigned Index)
Build and insert Res = G_INSERT_SUBVECTOR Src0, Src1, Idx.
MachineInstrBuilder buildAnd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1)
Build and insert Res = G_AND Op0, Op1.
const TargetInstrInfo & getTII()
MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_ICMP Pred, Op0, Op1.
MachineInstrBuilder buildLShr(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
MachineBasicBlock::iterator getInsertPt()
Current insertion point for new instructions.
MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_ZEXT Op.
MachineInstrBuilder buildVScale(const DstOp &Res, unsigned MinElts)
Build and insert Res = G_VSCALE MinElts.
MachineInstrBuilder buildIntToPtr(const DstOp &Dst, const SrcOp &Src)
Build and insert a G_INTTOPTR instruction.
MachineInstrBuilder buildLoad(const DstOp &Res, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert Res = G_LOAD Addr, MMO.
MachineInstrBuilder buildPtrAdd(const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_PTR_ADD Op0, Op1.
MachineInstrBuilder buildShl(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
MachineInstrBuilder buildStore(const SrcOp &Val, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert G_STORE Val, Addr, MMO.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineInstrBuilder buildFrameIndex(const DstOp &Res, int Idx)
Build and insert Res = G_FRAME_INDEX Idx.
MachineFunction & getMF()
Getter for the function we currently build.
MachineInstrBuilder buildMergeValues(const DstOp &Res, ArrayRef< Register > Ops)
Build and insert Res = G_MERGE_VALUES Op0, ...
MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_TRUNC Op.
void setDebugLoc(const DebugLoc &DL)
Set the debug location to DL for all the next build instructions.
const MachineBasicBlock & getMBB() const
Getter for the basic block we currently build.
MachineInstrBuilder buildAnyExt(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_ANYEXT Op0.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
MachineInstrBuilder buildExtractSubvector(const DstOp &Res, const SrcOp &Src, unsigned Index)
Build and insert Res = G_EXTRACT_SUBVECTOR Src, Idx0.
const DataLayout & getDataLayout() const
MachineInstrBuilder buildBrIndirect(Register Tgt)
Build and insert G_BRINDIRECT Tgt.
MachineInstrBuilder buildSplatVector(const DstOp &Res, const SrcOp &Val)
Build and insert Res = G_SPLAT_VECTOR Val.
MachineInstrBuilder buildLoadInstr(unsigned Opcode, const DstOp &Res, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert Res = <opcode> Addr, MMO.
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
MachineInstrBuilder buildFCmp(CmpInst::Predicate Pred, const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_FCMP PredOp0, Op1.
MachineInstrBuilder buildSExtInReg(const DstOp &Res, const SrcOp &Op, int64_t ImmOp)
Build and insert Res = G_SEXT_INREG Op, ImmOp.
Register getReg(unsigned Idx) const
Get the register for the operand index.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
LLVM_ABI unsigned getEntrySize(const DataLayout &TD) const
getEntrySize - Return the size of each entry in the jump table.
@ EK_LabelDifference32
EK_LabelDifference32 - Each entry is the address of the block minus the address of the jump table.
@ EK_Custom32
EK_Custom32 - Each entry is a 32-bit value that is custom lowered by the TargetLowering::LowerCustomJ...
@ EK_BlockAddress
EK_BlockAddress - Each entry is a plain address of block, e.g.: .word LBB123.
LLVM_ABI unsigned getEntryAlignment(const DataLayout &TD) const
getEntryAlignment - Return the alignment of each entry in the jump table.
A description of a memory reference used in the backend.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
static MachineOperand CreateImm(int64_t Val)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
LLVM_ABI void setRegClass(Register Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
LLVM_ABI Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
bool legalizeCustom(LegalizerHelper &Helper, MachineInstr &MI, LostDebugLocObserver &LocObserver) const override
Called for instructions with the Custom LegalizationAction.
bool legalizeIntrinsic(LegalizerHelper &Helper, MachineInstr &MI) const override
RISCVLegalizerInfo(const RISCVSubtarget &ST)
RISCVMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private RISCV-...
static std::pair< unsigned, unsigned > decomposeSubvectorInsertExtractToSubRegs(MVT VecVT, MVT SubVecVT, unsigned InsertExtractIdx, const RISCVRegisterInfo *TRI)
static RISCVVType::VLMUL getLMUL(MVT VT)
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Register getReg() const
bool allowsMemoryAccessForAlignment(LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace=0, Align Alignment=Align(1), MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *Fast=nullptr) const
This function returns true if the memory access is aligned or if the target allows this specific unal...
static constexpr TypeSize getScalable(ScalarTy MinimumSize)
Definition TypeSize.h:355
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
static constexpr bool isKnownGT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:224
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:253
self_iterator getIterator()
Definition ilist_node.h:123
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
LLVM_ABI LegalityPredicate isScalar(unsigned TypeIdx)
True iff the specified type index is a scalar.
LLVM_ABI LegalityPredicate typeInSet(unsigned TypeIdx, std::initializer_list< LLT > TypesInit)
True iff the given type index is one of the specified types.
LLVM_ABI LegalityPredicate immIs(unsigned ImmIdx, int64_t Imm)
True iff the immediate at the given index has the specified value.
Predicate any(Predicate P0, Predicate P1)
True iff P0 or P1 are true.
LLVM_ABI LegalityPredicate sizeIs(unsigned TypeIdx, unsigned Size)
True if the total bitwidth of the specified type index is Size bits.
Predicate all(Predicate P0, Predicate P1)
True iff P0 and P1 are true.
LLVM_ABI LegalityPredicate typeIs(unsigned TypeIdx, LLT TypesInit)
True iff the given type index is the specified type.
LLVM_ABI LegalityPredicate scalarWiderThan(unsigned TypeIdx, unsigned Size)
True iff the specified type index is a scalar that's wider than the given size.
LLVM_ABI LegalityPredicate immInSet(unsigned ImmIdx, std::initializer_list< int64_t > ImmsInit)
True iff the immediate at the given index has one of the specified values.
LLVM_ABI LegalityPredicate scalarNarrowerThan(unsigned TypeIdx, unsigned Size)
True iff the specified type index is a scalar that's narrower than the given size.
LLVM_ABI LegalizeMutation changeTo(unsigned TypeIdx, LLT Ty)
Select this specific type for the given type index.
ImplicitDefMatch m_GImplicitDef()
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
InstSeq generateInstSeq(int64_t Val, const MCSubtargetInfo &STI)
InstSeq generateTwoRegInstSeq(int64_t Val, const MCSubtargetInfo &STI, unsigned &ShiftAmt, unsigned &AddOpc)
SmallVector< Inst, 8 > InstSeq
Definition RISCVMatInt.h:43
LLVM_ABI std::pair< unsigned, bool > decodeVLMUL(VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
Invariant opcodes: All instruction sets have these as their low opcodes.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI Type * getTypeForLLT(LLT Ty, LLVMContext &C)
Get the type back from LLT.
Definition Utils.cpp:2140
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition Utils.cpp:1717
@ Load
The value being inserted comes from a load (InsertElement only).
LLVM_ABI MVT getMVTForLLT(LLT Ty)
Get a rough equivalent of an MVT for a given LLT.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:285
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
Definition Utils.cpp:1699
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:332
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:326
std::function< bool(const LegalityQuery &)> LegalityPredicate
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ And
Bitwise or logical AND of integers.
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI std::optional< ValueAndVReg > getIConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT returns its...
Definition Utils.cpp:440
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:202
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
The LegalityQuery object bundles together all the information that's needed to decide whether a given...
ArrayRef< LLT > Types
Matching combinators.
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getJumpTable(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a jump table entry.