LLVM 24.0.0git
LegalizeVectorTypes.cpp
Go to the documentation of this file.
1//===------- LegalizeVectorTypes.cpp - Legalization of vector types -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file performs vector type splitting and scalarization for LegalizeTypes.
10// Scalarization is the act of changing a computation in an illegal one-element
11// vector type to be a computation in its scalar element type. For example,
12// implementing <1 x f32> arithmetic in a scalar f32 register. This is needed
13// as a base case when scalarizing vector arithmetic like <4 x f32>, which
14// eventually decomposes to scalars if the target doesn't support v4f32 or v2f32
15// types.
16// Splitting is the act of changing a computation in an invalid vector type to
17// be a computation in two vectors of half the size. For example, implementing
18// <128 x f32> operations in terms of two <64 x f32> operations.
19//
20//===----------------------------------------------------------------------===//
21
22#include "LegalizeTypes.h"
27#include "llvm/IR/DataLayout.h"
31#include <numeric>
32
33using namespace llvm;
34
35#define DEBUG_TYPE "legalize-types"
36
37//===----------------------------------------------------------------------===//
38// Result Vector Scalarization: <1 x ty> -> ty.
39//===----------------------------------------------------------------------===//
40
41void DAGTypeLegalizer::ScalarizeVectorResult(SDNode *N, unsigned ResNo) {
42 LLVM_DEBUG(dbgs() << "Scalarize node result " << ResNo << ": ";
43 N->dump(&DAG));
44 SDValue R = SDValue();
45
46 // See if the target wants to custom expand this node.
47 if (CustomLowerNode(N, N->getValueType(ResNo), true))
48 return;
49
50 switch (N->getOpcode()) {
51 default:
52#ifndef NDEBUG
53 dbgs() << "ScalarizeVectorResult #" << ResNo << ": ";
54 N->dump(&DAG);
55 dbgs() << "\n";
56#endif
57 report_fatal_error("Do not know how to scalarize the result of this "
58 "operator!\n");
59
62 R = ScalarizeVecRes_LOOP_DEPENDENCE_MASK(N);
63 break;
64 case ISD::MERGE_VALUES: R = ScalarizeVecRes_MERGE_VALUES(N, ResNo);break;
65 case ISD::BITCAST: R = ScalarizeVecRes_BITCAST(N); break;
68 R = ScalarizeVecRes_BUILD_VECTOR_OR_SPLAT(N);
69 break;
70 case ISD::EXTRACT_SUBVECTOR: R = ScalarizeVecRes_EXTRACT_SUBVECTOR(N); break;
72 R = ScalarizeVecRes_VECTOR_SHUFFLE_VAR(N);
73 break;
74 case ISD::FP_ROUND: R = ScalarizeVecRes_FP_ROUND(N); break;
76 R = ScalarizeVecRes_CONVERT_FROM_ARBITRARY_FP(N);
77 break;
79 R = ScalarizeVecRes_CONVERT_TO_ARBITRARY_FP(N);
80 break;
81 case ISD::AssertZext:
82 case ISD::AssertSext:
83 case ISD::FPOWI:
85 R = ScalarizeVecRes_UnaryOpWithExtraInput(N);
86 break;
87 case ISD::INSERT_VECTOR_ELT: R = ScalarizeVecRes_INSERT_VECTOR_ELT(N); break;
89 R = ScalarizeVecRes_ATOMIC_LOAD(cast<AtomicSDNode>(N));
90 break;
114 R = ScalarizeVecRes_ATOMIC_RMW(cast<AtomicSDNode>(N));
115 break;
116 case ISD::LOAD: R = ScalarizeVecRes_LOAD(cast<LoadSDNode>(N));break;
117 case ISD::SCALAR_TO_VECTOR: R = ScalarizeVecRes_SCALAR_TO_VECTOR(N); break;
120 R = ScalarizeVecRes_VECTOR_INTERLEAVE_DEINTERLEAVE(N);
121 break;
122 case ISD::SIGN_EXTEND_INREG: R = ScalarizeVecRes_InregOp(N); break;
123 case ISD::VSELECT: R = ScalarizeVecRes_VSELECT(N); break;
124 case ISD::SELECT: R = ScalarizeVecRes_SELECT(N); break;
125 case ISD::SELECT_CC: R = ScalarizeVecRes_SELECT_CC(N); break;
126 case ISD::SETCC: R = ScalarizeVecRes_SETCC(N); break;
128 R = ScalarizeVecRes_VECTOR_MATCH(N);
129 break;
130 case ISD::POISON:
131 case ISD::UNDEF: R = ScalarizeVecRes_UNDEF(N); break;
132 case ISD::VECTOR_SHUFFLE: R = ScalarizeVecRes_VECTOR_SHUFFLE(N); break;
133 case ISD::IS_FPCLASS: R = ScalarizeVecRes_IS_FPCLASS(N); break;
137 R = ScalarizeVecRes_VecInregOp(N);
138 break;
139 case ISD::ABS:
141 case ISD::ANY_EXTEND:
142 case ISD::BITREVERSE:
143 case ISD::BSWAP:
144 case ISD::CTLZ:
146 case ISD::CTPOP:
147 case ISD::CTTZ:
149 case ISD::FABS:
150 case ISD::FACOS:
151 case ISD::FASIN:
152 case ISD::FATAN:
153 case ISD::FCEIL:
154 case ISD::FCOS:
155 case ISD::FCOSH:
156 case ISD::FEXP:
157 case ISD::FEXP2:
158 case ISD::FEXP10:
159 case ISD::FFLOOR:
160 case ISD::FLOG:
161 case ISD::FLOG10:
162 case ISD::FLOG2:
163 case ISD::FNEARBYINT:
164 case ISD::FNEG:
165 case ISD::FREEZE:
166 case ISD::ARITH_FENCE:
167 case ISD::FP_EXTEND:
168 case ISD::FP_TO_SINT:
169 case ISD::FP_TO_UINT:
170 case ISD::FRINT:
171 case ISD::LRINT:
172 case ISD::LLRINT:
173 case ISD::FROUND:
174 case ISD::FROUNDEVEN:
175 case ISD::LROUND:
176 case ISD::LLROUND:
177 case ISD::FSIN:
178 case ISD::FSINH:
179 case ISD::FSQRT:
180 case ISD::FTAN:
181 case ISD::FTANH:
182 case ISD::FTRUNC:
183 case ISD::SIGN_EXTEND:
184 case ISD::SINT_TO_FP:
185 case ISD::TRUNCATE:
186 case ISD::UINT_TO_FP:
187 case ISD::ZERO_EXTEND:
189 R = ScalarizeVecRes_UnaryOp(N);
190 break;
192 R = ScalarizeVecRes_ADDRSPACECAST(N);
193 break;
194 case ISD::FMODF:
195 case ISD::FFREXP:
196 case ISD::FSINCOS:
197 case ISD::FSINCOSPI:
198 R = ScalarizeVecRes_UnaryOpWithTwoResults(N, ResNo);
199 break;
200 case ISD::ADD:
201 case ISD::AND:
202 case ISD::AVGCEILS:
203 case ISD::AVGCEILU:
204 case ISD::AVGFLOORS:
205 case ISD::AVGFLOORU:
206 case ISD::FADD:
207 case ISD::FCOPYSIGN:
208 case ISD::FDIV:
209 case ISD::FMUL:
210 case ISD::FMINNUM:
211 case ISD::FMAXNUM:
214 case ISD::FMINIMUM:
215 case ISD::FMAXIMUM:
216 case ISD::FMINIMUMNUM:
217 case ISD::FMAXIMUMNUM:
218 case ISD::ABDS:
219 case ISD::ABDU:
220 case ISD::SMIN:
221 case ISD::SMAX:
222 case ISD::UMIN:
223 case ISD::UMAX:
224
225 case ISD::SADDSAT:
226 case ISD::UADDSAT:
227 case ISD::SSUBSAT:
228 case ISD::USUBSAT:
229 case ISD::SSHLSAT:
230 case ISD::USHLSAT:
231
232 case ISD::FPOW:
233 case ISD::FATAN2:
234 case ISD::FREM:
235 case ISD::FSUB:
236 case ISD::MUL:
237 case ISD::MULHS:
238 case ISD::MULHU:
239 case ISD::OR:
240 case ISD::SDIV:
241 case ISD::SREM:
242 case ISD::SUB:
243 case ISD::UDIV:
244 case ISD::UREM:
245 case ISD::XOR:
246 case ISD::SHL:
247 case ISD::SRA:
248 case ISD::SRL:
249 case ISD::ROTL:
250 case ISD::ROTR:
251 case ISD::CLMUL:
252 case ISD::CLMULR:
253 case ISD::CLMULH:
254 case ISD::PEXT:
255 case ISD::PDEP:
256 R = ScalarizeVecRes_BinOp(N);
257 break;
258
259 case ISD::MASKED_UDIV:
260 case ISD::MASKED_SDIV:
261 case ISD::MASKED_UREM:
262 case ISD::MASKED_SREM:
263 R = ScalarizeVecRes_MaskedBinOp(N);
264 break;
265
266 case ISD::FLDEXP:
267 R = ScalarizeVecRes_FPOp_MultiType(N);
268 break;
269
270 case ISD::SCMP:
271 case ISD::UCMP:
272 R = ScalarizeVecRes_CMP(N);
273 break;
274
275 case ISD::FMA:
276 case ISD::FSHL:
277 case ISD::FSHR:
278 R = ScalarizeVecRes_TernaryOp(N);
279 break;
280
281#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
282 case ISD::STRICT_##DAGN:
283#include "llvm/IR/ConstrainedOps.def"
284 R = ScalarizeVecRes_StrictFPOp(N);
285 break;
286
289 R = ScalarizeVecRes_FP_TO_XINT_SAT(N);
290 break;
291
292 case ISD::UADDO:
293 case ISD::SADDO:
294 case ISD::USUBO:
295 case ISD::SSUBO:
296 case ISD::UMULO:
297 case ISD::SMULO:
298 R = ScalarizeVecRes_OverflowOp(N, ResNo);
299 break;
300 case ISD::SMULFIX:
301 case ISD::SMULFIXSAT:
302 case ISD::UMULFIX:
303 case ISD::UMULFIXSAT:
304 case ISD::SDIVFIX:
305 case ISD::SDIVFIXSAT:
306 case ISD::UDIVFIX:
307 case ISD::UDIVFIXSAT:
308 R = ScalarizeVecRes_FIX(N);
309 break;
314 R = ScalarizeVecRes_PartialReduction(N);
315 break;
316 }
317
318 // If R is null, the sub-method took care of registering the result.
319 if (R.getNode())
320 SetScalarizedVector(SDValue(N, ResNo), R);
321}
322
323SDValue DAGTypeLegalizer::ScalarizeVecRes_BinOp(SDNode *N) {
324 SDValue LHS = GetScalarizedVector(N->getOperand(0));
325 SDValue RHS = GetScalarizedVector(N->getOperand(1));
326 return DAG.getNode(N->getOpcode(), SDLoc(N),
327 LHS.getValueType(), LHS, RHS, N->getFlags());
328}
329
330SDValue DAGTypeLegalizer::ScalarizeVecRes_MaskedBinOp(SDNode *N) {
331 SDLoc DL(N);
332 SDValue LHS = GetScalarizedVector(N->getOperand(0));
333 SDValue RHS = GetScalarizedVector(N->getOperand(1));
334 SDValue Mask = N->getOperand(2);
335 EVT MaskVT = Mask.getValueType();
336 // The vselect result and input vectors need scalarizing, but it's
337 // not a given that the mask does. For instance, in AVX512 v1i1 is legal.
338 // See the similar logic in ScalarizeVecRes_SETCC.
339 if (getTypeAction(MaskVT) == TargetLowering::TypeScalarizeVector)
340 Mask = GetScalarizedVector(Mask);
341 else
342 Mask = DAG.getExtractVectorElt(DL, MaskVT.getVectorElementType(), Mask, 0);
343 // Vectors may have a different boolean contents to scalars, so truncate to i1
344 // and let type legalization promote appropriately.
345 Mask = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Mask);
346 // Masked binary ops don't have UB on disabled lanes but produce poison, so
347 // use 1 as the divisor to avoid division by zero and overflow.
348 SDValue Divisor = DAG.getSelect(DL, LHS.getValueType(), Mask, RHS,
349 DAG.getConstant(1, DL, LHS.getValueType()));
350 return DAG.getNode(ISD::getUnmaskedBinOpOpcode(N->getOpcode()), DL,
351 LHS.getValueType(), LHS, Divisor);
352}
353
354SDValue DAGTypeLegalizer::ScalarizeVecRes_CMP(SDNode *N) {
355 SDLoc DL(N);
356
357 SDValue LHS = N->getOperand(0);
358 SDValue RHS = N->getOperand(1);
359 if (getTypeAction(LHS.getValueType()) ==
361 LHS = GetScalarizedVector(LHS);
362 RHS = GetScalarizedVector(RHS);
363 } else {
364 EVT VT = LHS.getValueType().getVectorElementType();
365 LHS = DAG.getExtractVectorElt(DL, VT, LHS, 0);
366 RHS = DAG.getExtractVectorElt(DL, VT, RHS, 0);
367 }
368
369 return DAG.getNode(N->getOpcode(), SDLoc(N),
370 N->getValueType(0).getVectorElementType(), LHS, RHS);
371}
372
373SDValue DAGTypeLegalizer::ScalarizeVecRes_TernaryOp(SDNode *N) {
374 SDValue Op0 = GetScalarizedVector(N->getOperand(0));
375 SDValue Op1 = GetScalarizedVector(N->getOperand(1));
376 SDValue Op2 = GetScalarizedVector(N->getOperand(2));
377 return DAG.getNode(N->getOpcode(), SDLoc(N), Op0.getValueType(), Op0, Op1,
378 Op2, N->getFlags());
379}
380
381SDValue DAGTypeLegalizer::ScalarizeVecRes_FIX(SDNode *N) {
382 SDValue Op0 = GetScalarizedVector(N->getOperand(0));
383 SDValue Op1 = GetScalarizedVector(N->getOperand(1));
384 SDValue Op2 = N->getOperand(2);
385 return DAG.getNode(N->getOpcode(), SDLoc(N), Op0.getValueType(), Op0, Op1,
386 Op2, N->getFlags());
387}
388
389SDValue DAGTypeLegalizer::ScalarizeVecRes_PartialReduction(SDNode *N) {
390 SDValue Acc = GetScalarizedVector(N->getOperand(0));
391 SDValue L = N->getOperand(1);
392 SDValue R = N->getOperand(2);
393 EVT ResVT = Acc.getValueType();
394 EVT InVT = L.getValueType();
395 bool IsFP = ResVT.isFloatingPoint();
396 SDLoc DL(N);
397
398 // Make sure we're not trying an extending partial reduction in this path.
399 assert(InVT.getScalarType() == ResVT && "Extending operands unsupported");
400
401 SDValue Mul = DAG.getNode(IsFP ? ISD::FMUL : ISD::MUL, DL, InVT, L, R);
402 SDValue In = DAG.getNode(IsFP ? ISD::VECREDUCE_FADD : ISD::VECREDUCE_ADD, DL,
403 ResVT, Mul);
404
405 return DAG.getNode(IsFP ? ISD::FADD : ISD::ADD, DL, ResVT, Acc, In);
406}
407
409DAGTypeLegalizer::ScalarizeVecRes_UnaryOpWithTwoResults(SDNode *N,
410 unsigned ResNo) {
411 assert(N->getValueType(0).getVectorNumElements() == 1 &&
412 "Unexpected vector type!");
413 SDValue Elt = GetScalarizedVector(N->getOperand(0));
414
415 EVT VT0 = N->getValueType(0);
416 EVT VT1 = N->getValueType(1);
417 SDLoc dl(N);
418
419 SDNode *ScalarNode =
420 DAG.getNode(N->getOpcode(), dl,
421 {VT0.getScalarType(), VT1.getScalarType()}, Elt)
422 .getNode();
423
424 // Replace the other vector result not being explicitly scalarized here.
425 unsigned OtherNo = 1 - ResNo;
426 EVT OtherVT = N->getValueType(OtherNo);
427 if (getTypeAction(OtherVT) == TargetLowering::TypeScalarizeVector) {
428 SetScalarizedVector(SDValue(N, OtherNo), SDValue(ScalarNode, OtherNo));
429 } else {
430 SDValue OtherVal = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, OtherVT,
431 SDValue(ScalarNode, OtherNo));
432 ReplaceValueWith(SDValue(N, OtherNo), OtherVal);
433 }
434
435 return SDValue(ScalarNode, ResNo);
436}
437
438SDValue DAGTypeLegalizer::ScalarizeVecRes_StrictFPOp(SDNode *N) {
439 EVT VT = N->getValueType(0).getVectorElementType();
440 unsigned NumOpers = N->getNumOperands();
441 SDValue Chain = N->getOperand(0);
442 EVT ValueVTs[] = {VT, MVT::Other};
443 SDLoc dl(N);
444
445 SmallVector<SDValue, 4> Opers(NumOpers);
446
447 // The Chain is the first operand.
448 Opers[0] = Chain;
449
450 // Now process the remaining operands.
451 for (unsigned i = 1; i < NumOpers; ++i) {
452 SDValue Oper = N->getOperand(i);
453 EVT OperVT = Oper.getValueType();
454
455 if (OperVT.isVector()) {
456 if (getTypeAction(OperVT) == TargetLowering::TypeScalarizeVector)
457 Oper = GetScalarizedVector(Oper);
458 else
459 Oper =
460 DAG.getExtractVectorElt(dl, OperVT.getVectorElementType(), Oper, 0);
461 }
462
463 Opers[i] = Oper;
464 }
465
466 SDValue Result = DAG.getNode(N->getOpcode(), dl, DAG.getVTList(ValueVTs),
467 Opers, N->getFlags());
468
469 // Legalize the chain result - switch anything that used the old chain to
470 // use the new one.
471 ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
472 return Result;
473}
474
475SDValue DAGTypeLegalizer::ScalarizeVecRes_OverflowOp(SDNode *N,
476 unsigned ResNo) {
477 SDLoc DL(N);
478 EVT ResVT = N->getValueType(0);
479 EVT OvVT = N->getValueType(1);
480
481 SDValue ScalarLHS, ScalarRHS;
482 if (getTypeAction(ResVT) == TargetLowering::TypeScalarizeVector) {
483 ScalarLHS = GetScalarizedVector(N->getOperand(0));
484 ScalarRHS = GetScalarizedVector(N->getOperand(1));
485 } else {
486 SmallVector<SDValue, 1> ElemsLHS, ElemsRHS;
487 DAG.ExtractVectorElements(N->getOperand(0), ElemsLHS);
488 DAG.ExtractVectorElements(N->getOperand(1), ElemsRHS);
489 ScalarLHS = ElemsLHS[0];
490 ScalarRHS = ElemsRHS[0];
491 }
492
493 SDVTList ScalarVTs = DAG.getVTList(
495 SDNode *ScalarNode = DAG.getNode(N->getOpcode(), DL, ScalarVTs,
496 {ScalarLHS, ScalarRHS}, N->getFlags())
497 .getNode();
498
499 // Replace the other vector result not being explicitly scalarized here.
500 unsigned OtherNo = 1 - ResNo;
501 EVT OtherVT = N->getValueType(OtherNo);
502 if (getTypeAction(OtherVT) == TargetLowering::TypeScalarizeVector) {
503 SetScalarizedVector(SDValue(N, OtherNo), SDValue(ScalarNode, OtherNo));
504 } else {
505 SDValue OtherVal = DAG.getNode(
506 ISD::SCALAR_TO_VECTOR, DL, OtherVT, SDValue(ScalarNode, OtherNo));
507 ReplaceValueWith(SDValue(N, OtherNo), OtherVal);
508 }
509
510 return SDValue(ScalarNode, ResNo);
511}
512
513SDValue DAGTypeLegalizer::ScalarizeVecRes_MERGE_VALUES(SDNode *N,
514 unsigned ResNo) {
515 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
516 return GetScalarizedVector(Op);
517}
518
519SDValue DAGTypeLegalizer::ScalarizeVecRes_LOOP_DEPENDENCE_MASK(SDNode *N) {
520 SDLoc DL(N);
521 // Reuse the expansion (which should scalarize).
522 SDValue Mask = TLI.expandLoopDependenceMask(N, DAG);
523 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
524 N->getValueType(0).getScalarType(), Mask,
525 DAG.getVectorIdxConstant(0, DL));
526}
527
528SDValue DAGTypeLegalizer::ScalarizeVecRes_BITCAST(SDNode *N) {
529 SDValue Op = N->getOperand(0);
530 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeScalarizeVector)
531 Op = GetScalarizedVector(Op);
532 EVT NewVT = N->getValueType(0).getVectorElementType();
533 return DAG.getNode(ISD::BITCAST, SDLoc(N),
534 NewVT, Op);
535}
536
537SDValue DAGTypeLegalizer::ScalarizeVecRes_BUILD_VECTOR_OR_SPLAT(SDNode *N) {
538 EVT EltVT = N->getValueType(0).getVectorElementType();
539 SDValue InOp = N->getOperand(0);
540 // The BUILD_VECTOR / SPLAT operands may be of wider element types and
541 // we may need to truncate them back to the requested return type.
542 if (EltVT.isInteger())
543 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, InOp);
544 return InOp;
545}
546
547SDValue DAGTypeLegalizer::ScalarizeVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
548 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
549 N->getValueType(0).getVectorElementType(),
550 N->getOperand(0), N->getOperand(1));
551}
552
553SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_SHUFFLE_VAR(SDNode *N) {
554 // Index 0 is the only in-range index.
555 return GetScalarizedVector(N->getOperand(0));
556}
557
558SDValue DAGTypeLegalizer::ScalarizeVecRes_FP_ROUND(SDNode *N) {
559 SDLoc DL(N);
560 SDValue Op = N->getOperand(0);
561 EVT OpVT = Op.getValueType();
562 // The result needs scalarizing, but it's not a given that the source does.
563 // See similar logic in ScalarizeVecRes_UnaryOp.
564 if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
565 Op = GetScalarizedVector(Op);
566 } else {
567 EVT VT = OpVT.getVectorElementType();
568 Op = DAG.getExtractVectorElt(DL, VT, Op, 0);
569 }
570 return DAG.getNode(ISD::FP_ROUND, DL,
571 N->getValueType(0).getVectorElementType(), Op,
572 N->getOperand(1));
573}
574
575SDValue DAGTypeLegalizer::ScalarizeVecRes_CONVERT_FROM_ARBITRARY_FP(SDNode *N) {
576 SDLoc DL(N);
577 SDValue Op = N->getOperand(0);
578 EVT OpVT = Op.getValueType();
579 // The result needs scalarizing, but it's not a given that the source does.
580 // See similar logic in ScalarizeVecRes_UnaryOp.
581 if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
582 Op = GetScalarizedVector(Op);
583 } else {
584 EVT VT = OpVT.getVectorElementType();
585 Op = DAG.getExtractVectorElt(DL, VT, Op, 0);
586 }
587 return DAG.getNode(ISD::CONVERT_FROM_ARBITRARY_FP, DL,
588 N->getValueType(0).getVectorElementType(), Op,
589 N->getOperand(1));
590}
591
592SDValue DAGTypeLegalizer::ScalarizeVecRes_CONVERT_TO_ARBITRARY_FP(SDNode *N) {
593 SDLoc DL(N);
594 SDValue Op = N->getOperand(0);
595 EVT OpVT = Op.getValueType();
596 // The result needs scalarizing, but it's not a given that the source does.
597 if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
598 Op = GetScalarizedVector(Op);
599 } else {
600 EVT VT = OpVT.getVectorElementType();
601 Op = DAG.getExtractVectorElt(DL, VT, Op, 0);
602 }
603 return DAG.getNode(ISD::CONVERT_TO_ARBITRARY_FP, DL,
604 N->getValueType(0).getVectorElementType(), Op,
605 N->getOperand(1), N->getOperand(2), N->getOperand(3));
606}
607
608SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOpWithExtraInput(SDNode *N) {
609 SDValue Op = GetScalarizedVector(N->getOperand(0));
610 return DAG.getNode(N->getOpcode(), SDLoc(N), Op.getValueType(), Op,
611 N->getOperand(1));
612}
613
614SDValue DAGTypeLegalizer::ScalarizeVecRes_FPOp_MultiType(SDNode *N) {
615 SDLoc DL(N);
616 SDValue LHS = GetScalarizedVector(N->getOperand(0));
617 SDValue RHS = N->getOperand(1);
618 EVT RHSVT = RHS.getValueType();
619 // The exponent has its own type action and may not have been scalarized:
620 // v1i1 is legal on AVX-512, v1i32 is widened on AArch64.
621 if (RHSVT.isVector()) {
622 if (getTypeAction(RHSVT) == TargetLowering::TypeScalarizeVector)
623 RHS = GetScalarizedVector(RHS);
624 else
625 RHS = DAG.getExtractVectorElt(DL, RHSVT.getVectorElementType(), RHS, 0);
626 }
627 return DAG.getNode(N->getOpcode(), DL, LHS.getValueType(), LHS, RHS,
628 N->getFlags());
629}
630
631SDValue DAGTypeLegalizer::ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N) {
632 // The value to insert may have a wider type than the vector element type,
633 // so be sure to truncate it to the element type if necessary.
634 SDValue Op = N->getOperand(1);
635 EVT EltVT = N->getValueType(0).getVectorElementType();
636 if (Op.getValueType() != EltVT)
637 // FIXME: Can this happen for floating point types?
638 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, Op);
639 return Op;
640}
641
642SDValue DAGTypeLegalizer::ScalarizeVecRes_ATOMIC_LOAD(AtomicSDNode *N) {
643 SDValue Result = DAG.getAtomicLoad(
644 N->getExtensionType(), SDLoc(N), N->getMemoryVT().getVectorElementType(),
645 N->getValueType(0).getVectorElementType(), N->getChain(), N->getBasePtr(),
646 N->getMemOperand());
647
648 // Legalize the chain result - switch anything that used the old chain to
649 // use the new one.
650 ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
651 return Result;
652}
653
654SDValue DAGTypeLegalizer::ScalarizeVecRes_ATOMIC_RMW(AtomicSDNode *N) {
655 SDValue Result = DAG.getAtomic(
656 N->getOpcode(), SDLoc(N), N->getMemoryVT().getVectorElementType(),
657 N->getChain(), N->getBasePtr(), GetScalarizedVector(N->getVal()),
658 N->getMemOperand());
659
660 ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
661 return Result;
662}
663
664SDValue DAGTypeLegalizer::ScalarizeVecRes_LOAD(LoadSDNode *N) {
665 assert(N->isUnindexed() && "Indexed vector load?");
666
667 SDValue Result = DAG.getLoad(
668 ISD::UNINDEXED, N->getExtensionType(),
669 N->getValueType(0).getVectorElementType(), SDLoc(N), N->getChain(),
670 N->getBasePtr(), DAG.getPOISON(N->getBasePtr().getValueType()),
671 N->getPointerInfo(), N->getMemoryVT().getVectorElementType(),
672 N->getBaseAlign(), N->getMemOperand()->getFlags(),
673 N->getMMOMetadataForSubAccess());
674
675 // Legalize the chain result - switch anything that used the old chain to
676 // use the new one.
677 ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
678 return Result;
679}
680
681SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOp(SDNode *N) {
682 // Get the dest type - it doesn't always match the input type, e.g. int_to_fp.
683 EVT DestVT = N->getValueType(0).getVectorElementType();
684 SDValue Op = N->getOperand(0);
685 EVT OpVT = Op.getValueType();
686 SDLoc DL(N);
687 // The result needs scalarizing, but it's not a given that the source does.
688 // This is a workaround for targets where it's impossible to scalarize the
689 // result of a conversion, because the source type is legal.
690 // For instance, this happens on AArch64: v1i1 is illegal but v1i{8,16,32}
691 // are widened to v8i8, v4i16, and v2i32, which is legal, because v1i64 is
692 // legal and was not scalarized.
693 // See the similar logic in ScalarizeVecRes_SETCC
694 if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
695 Op = GetScalarizedVector(Op);
696 } else {
697 EVT VT = OpVT.getVectorElementType();
698 Op = DAG.getExtractVectorElt(DL, VT, Op, 0);
699 }
700 return DAG.getNode(N->getOpcode(), SDLoc(N), DestVT, Op, N->getFlags());
701}
702
703SDValue DAGTypeLegalizer::ScalarizeVecRes_InregOp(SDNode *N) {
704 EVT EltVT = N->getValueType(0).getVectorElementType();
705 EVT ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT().getVectorElementType();
706 SDValue LHS = GetScalarizedVector(N->getOperand(0));
707 return DAG.getNode(N->getOpcode(), SDLoc(N), EltVT,
708 LHS, DAG.getValueType(ExtVT));
709}
710
711SDValue DAGTypeLegalizer::ScalarizeVecRes_VecInregOp(SDNode *N) {
712 SDLoc DL(N);
713 SDValue Op = N->getOperand(0);
714
715 EVT OpVT = Op.getValueType();
716 EVT OpEltVT = OpVT.getVectorElementType();
717 EVT EltVT = N->getValueType(0).getVectorElementType();
718
719 if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
720 Op = GetScalarizedVector(Op);
721 } else {
722 Op = DAG.getExtractVectorElt(DL, OpEltVT, Op, 0);
723 }
724
725 switch (N->getOpcode()) {
727 return DAG.getNode(ISD::ANY_EXTEND, DL, EltVT, Op);
729 return DAG.getNode(ISD::SIGN_EXTEND, DL, EltVT, Op);
731 return DAG.getNode(ISD::ZERO_EXTEND, DL, EltVT, Op);
732 }
733
734 llvm_unreachable("Illegal extend_vector_inreg opcode");
735}
736
737SDValue DAGTypeLegalizer::ScalarizeVecRes_ADDRSPACECAST(SDNode *N) {
738 EVT DestVT = N->getValueType(0).getVectorElementType();
739 SDValue Op = N->getOperand(0);
740 EVT OpVT = Op.getValueType();
741 SDLoc DL(N);
742 // The result needs scalarizing, but it's not a given that the source does.
743 // This is a workaround for targets where it's impossible to scalarize the
744 // result of a conversion, because the source type is legal.
745 // For instance, this happens on AArch64: v1i1 is illegal but v1i{8,16,32}
746 // are widened to v8i8, v4i16, and v2i32, which is legal, because v1i64 is
747 // legal and was not scalarized.
748 // See the similar logic in ScalarizeVecRes_SETCC
749 if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
750 Op = GetScalarizedVector(Op);
751 } else {
752 EVT VT = OpVT.getVectorElementType();
753 Op = DAG.getExtractVectorElt(DL, VT, Op, 0);
754 }
755 auto *AddrSpaceCastN = cast<AddrSpaceCastSDNode>(N);
756 unsigned SrcAS = AddrSpaceCastN->getSrcAddressSpace();
757 unsigned DestAS = AddrSpaceCastN->getDestAddressSpace();
758 return DAG.getAddrSpaceCast(DL, DestVT, Op, SrcAS, DestAS,
759 AddrSpaceCastN->getFlags());
760}
761
762SDValue DAGTypeLegalizer::ScalarizeVecRes_SCALAR_TO_VECTOR(SDNode *N) {
763 // If the operand is wider than the vector element type then it is implicitly
764 // truncated. Make that explicit here.
765 EVT EltVT = N->getValueType(0).getVectorElementType();
766 SDValue InOp = N->getOperand(0);
767 if (InOp.getValueType() != EltVT)
768 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, InOp);
769 return InOp;
770}
771
773DAGTypeLegalizer::ScalarizeVecRes_VECTOR_INTERLEAVE_DEINTERLEAVE(SDNode *N) {
774 assert(N->getNumValues() == N->getNumOperands() &&
775 "Expected one result per operand");
776
777 // Interleaving or deinterleaving one-element vectors leaves each result
778 // equal to the corresponding operand.
779 for (unsigned I = 0; I != N->getNumValues(); ++I)
780 SetScalarizedVector(SDValue(N, I), GetScalarizedVector(N->getOperand(I)));
781 return SDValue();
782}
783
784SDValue DAGTypeLegalizer::ScalarizeVecRes_VSELECT(SDNode *N) {
785 SDValue Cond = N->getOperand(0);
786 EVT OpVT = Cond.getValueType();
787 SDLoc DL(N);
788 // The vselect result and true/value operands needs scalarizing, but it's
789 // not a given that the Cond does. For instance, in AVX512 v1i1 is legal.
790 // See the similar logic in ScalarizeVecRes_SETCC
791 if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
792 Cond = GetScalarizedVector(Cond);
793 } else {
794 EVT VT = OpVT.getVectorElementType();
795 Cond = DAG.getExtractVectorElt(DL, VT, Cond, 0);
796 }
797
798 SDValue LHS = GetScalarizedVector(N->getOperand(1));
799 TargetLowering::BooleanContent ScalarBool = TLI.getBooleanContents(false);
800 TargetLowering::BooleanContent VecBool = TLI.getBooleanContents(true);
801
802 EVT CondVT = Cond.getValueType();
803 if (ScalarBool != VecBool) {
804 switch (ScalarBool) {
806 break;
810 // Vector read from all ones, scalar expects a single 1 so mask.
811 Cond = DAG.getNode(ISD::AND, SDLoc(N), CondVT,
812 Cond, DAG.getConstant(1, SDLoc(N), CondVT));
813 break;
817 // Vector reads from a one, scalar from all ones so sign extend.
818 Cond = DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), CondVT,
819 Cond, DAG.getValueType(MVT::i1));
820 break;
821 }
822 }
823
824 // Truncate the condition if needed
825 auto BoolVT = getSetCCResultType(CondVT);
826 if (BoolVT.bitsLT(CondVT))
827 Cond = DAG.getNode(ISD::TRUNCATE, SDLoc(N), BoolVT, Cond);
828
829 return DAG.getSelect(SDLoc(N), LHS.getValueType(), Cond, LHS,
830 GetScalarizedVector(N->getOperand(2)), N->getFlags());
831}
832
833SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT(SDNode *N) {
834 SDValue LHS = GetScalarizedVector(N->getOperand(1));
835 return DAG.getSelect(SDLoc(N),
836 LHS.getValueType(), N->getOperand(0), LHS,
837 GetScalarizedVector(N->getOperand(2)));
838}
839
840SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT_CC(SDNode *N) {
841 SDValue LHS = GetScalarizedVector(N->getOperand(2));
842 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), LHS.getValueType(),
843 N->getOperand(0), N->getOperand(1),
844 LHS, GetScalarizedVector(N->getOperand(3)),
845 N->getOperand(4));
846}
847
848SDValue DAGTypeLegalizer::ScalarizeVecRes_UNDEF(SDNode *N) {
849 return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
850}
851
852SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_SHUFFLE(SDNode *N) {
853 // Figure out if the scalar is the LHS or RHS and return it.
854 SDValue Arg = N->getOperand(2).getOperand(0);
855 if (Arg.isUndef())
856 return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
857 unsigned Op = !cast<ConstantSDNode>(Arg)->isZero();
858 return GetScalarizedVector(N->getOperand(Op));
859}
860
861SDValue DAGTypeLegalizer::ScalarizeVecRes_FP_TO_XINT_SAT(SDNode *N) {
862 SDValue Src = N->getOperand(0);
863 EVT SrcVT = Src.getValueType();
864 SDLoc dl(N);
865
866 // Handle case where result is scalarized but operand is not
867 if (getTypeAction(SrcVT) == TargetLowering::TypeScalarizeVector)
868 Src = GetScalarizedVector(Src);
869 else
870 Src = DAG.getNode(
872 DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
873
874 EVT DstVT = N->getValueType(0).getVectorElementType();
875 return DAG.getNode(N->getOpcode(), dl, DstVT, Src, N->getOperand(1));
876}
877
878SDValue DAGTypeLegalizer::ScalarizeVecRes_SETCC(SDNode *N) {
879 assert(N->getValueType(0).isVector() &&
880 N->getOperand(0).getValueType().isVector() &&
881 "Operand types must be vectors");
882 SDValue LHS = N->getOperand(0);
883 SDValue RHS = N->getOperand(1);
884 EVT OpVT = LHS.getValueType();
885 EVT NVT = N->getValueType(0).getVectorElementType();
886 SDLoc DL(N);
887
888 // The result needs scalarizing, but it's not a given that the source does.
889 if (getTypeAction(OpVT) == TargetLowering::TypeScalarizeVector) {
890 LHS = GetScalarizedVector(LHS);
891 RHS = GetScalarizedVector(RHS);
892 } else {
893 EVT VT = OpVT.getVectorElementType();
894 LHS = DAG.getExtractVectorElt(DL, VT, LHS, 0);
895 RHS = DAG.getExtractVectorElt(DL, VT, RHS, 0);
896 }
897
898 // Turn it into a scalar SETCC.
899 SDValue Res = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS,
900 N->getOperand(2));
901 // Vectors may have a different boolean contents to scalars. Promote the
902 // value appropriately.
903 ISD::NodeType ExtendCode =
904 TargetLowering::getExtendForContent(TLI.getBooleanContents(OpVT));
905 return DAG.getNode(ExtendCode, DL, NVT, Res);
906}
907
908SDValue DAGTypeLegalizer::ScalarizeVecRes_IS_FPCLASS(SDNode *N) {
909 SDLoc DL(N);
910 SDValue Arg = N->getOperand(0);
911 SDValue Test = N->getOperand(1);
912 EVT ArgVT = Arg.getValueType();
913 EVT ResultVT = N->getValueType(0).getVectorElementType();
914
915 if (getTypeAction(ArgVT) == TargetLowering::TypeScalarizeVector) {
916 Arg = GetScalarizedVector(Arg);
917 } else {
918 EVT VT = ArgVT.getVectorElementType();
919 Arg = DAG.getExtractVectorElt(DL, VT, Arg, 0);
920 }
921
922 SDValue Res =
923 DAG.getNode(ISD::IS_FPCLASS, DL, MVT::i1, {Arg, Test}, N->getFlags());
924 // Vectors may have a different boolean contents to scalars. Promote the
925 // value appropriately.
926 ISD::NodeType ExtendCode =
927 TargetLowering::getExtendForContent(TLI.getBooleanContents(ArgVT));
928 return DAG.getNode(ExtendCode, DL, ResultVT, Res);
929}
930
931//===----------------------------------------------------------------------===//
932// Operand Vector Scalarization <1 x ty> -> ty.
933//===----------------------------------------------------------------------===//
934
935bool DAGTypeLegalizer::ScalarizeVectorOperand(SDNode *N, unsigned OpNo) {
936 LLVM_DEBUG(dbgs() << "Scalarize node operand " << OpNo << ": ";
937 N->dump(&DAG));
938 SDValue Res = SDValue();
939
940 // See if the target wants to custom scalarize this node.
941 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
942 return false;
943
944 switch (N->getOpcode()) {
945 default:
946#ifndef NDEBUG
947 dbgs() << "ScalarizeVectorOperand Op #" << OpNo << ": ";
948 N->dump(&DAG);
949 dbgs() << "\n";
950#endif
951 report_fatal_error("Do not know how to scalarize this operator's "
952 "operand!\n");
953 case ISD::BITCAST:
954 Res = ScalarizeVecOp_BITCAST(N);
955 break;
956 case ISD::FAKE_USE:
957 Res = ScalarizeVecOp_FAKE_USE(N);
958 break;
960 // Index 0 is the only in-range index.
961 Res = N->getOperand(0);
962 break;
963 case ISD::ANY_EXTEND:
964 case ISD::ZERO_EXTEND:
965 case ISD::SIGN_EXTEND:
966 case ISD::TRUNCATE:
967 case ISD::FP_TO_SINT:
968 case ISD::FP_TO_UINT:
969 case ISD::SINT_TO_FP:
970 case ISD::UINT_TO_FP:
971 case ISD::LROUND:
972 case ISD::LLROUND:
973 case ISD::LRINT:
974 case ISD::LLRINT:
975 Res = ScalarizeVecOp_UnaryOp(N);
976 break;
980 Res = ScalarizeVecOp_UnaryOpWithExtraInput(N);
981 break;
983 assert(N->getValueType(0).getVectorNumElements() == 1 &&
984 "Unexpected vector type!");
985 SDValue Elt = GetScalarizedVector(N->getOperand(0));
986 SDValue Op = DAG.getNode(
987 N->getOpcode(), SDLoc(N), N->getValueType(0).getScalarType(), Elt,
988 N->getOperand(1), N->getOperand(2), N->getOperand(3));
989 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Op);
990 break;
991 }
996 Res = ScalarizeVecOp_UnaryOp_StrictFP(N);
997 break;
999 Res = ScalarizeVecOp_CONCAT_VECTORS(N);
1000 break;
1002 Res = ScalarizeVecOp_INSERT_SUBVECTOR(N, OpNo);
1003 break;
1005 Res = ScalarizeVecOp_EXTRACT_VECTOR_ELT(N);
1006 break;
1007 case ISD::VSELECT:
1008 Res = ScalarizeVecOp_VSELECT(N);
1009 break;
1010 case ISD::SETCC:
1011 Res = ScalarizeVecOp_VSETCC(N);
1012 break;
1013 case ISD::STRICT_FSETCC:
1015 Res = ScalarizeVecOp_VSTRICT_FSETCC(N, OpNo);
1016 break;
1017 case ISD::STORE:
1018 Res = ScalarizeVecOp_STORE(cast<StoreSDNode>(N), OpNo);
1019 break;
1020 case ISD::ATOMIC_STORE:
1021 Res = ScalarizeVecOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
1022 break;
1023 case ISD::MLOAD:
1024 Res = ScalarizeVecOp_MLOAD(cast<MaskedLoadSDNode>(N), OpNo);
1025 break;
1026 case ISD::MSTORE:
1027 Res = ScalarizeVecOp_MSTORE(cast<MaskedStoreSDNode>(N), OpNo);
1028 break;
1030 Res = ScalarizeVecOp_STRICT_FP_ROUND(N, OpNo);
1031 break;
1032 case ISD::FP_ROUND:
1033 Res = ScalarizeVecOp_FP_ROUND(N, OpNo);
1034 break;
1036 Res = ScalarizeVecOp_STRICT_FP_EXTEND(N);
1037 break;
1038 case ISD::FP_EXTEND:
1039 Res = ScalarizeVecOp_FP_EXTEND(N);
1040 break;
1043 case ISD::VECREDUCE_ADD:
1044 case ISD::VECREDUCE_MUL:
1045 case ISD::VECREDUCE_AND:
1046 case ISD::VECREDUCE_OR:
1047 case ISD::VECREDUCE_XOR:
1058 Res = ScalarizeVecOp_VECREDUCE(N);
1059 break;
1062 Res = ScalarizeVecOp_VECREDUCE_SEQ(N);
1063 break;
1064 case ISD::SCMP:
1065 case ISD::UCMP:
1066 Res = ScalarizeVecOp_CMP(N);
1067 break;
1069 Res = ScalarizeVecOp_VECTOR_FIND_LAST_ACTIVE(N);
1070 break;
1071 case ISD::CTTZ_ELTS:
1073 Res = ScalarizeVecOp_CTTZ_ELTS(N);
1074 break;
1075 case ISD::VECTOR_MATCH:
1076 Res = ScalarizeVecOp_VECTOR_MATCH(N, OpNo);
1077 break;
1078 case ISD::MASKED_UDIV:
1079 case ISD::MASKED_SDIV:
1080 case ISD::MASKED_UREM:
1081 case ISD::MASKED_SREM:
1082 Res = ScalarizeVecOp_MaskedBinOp(N, OpNo);
1083 break;
1084 }
1085
1086 // If the result is null, the sub-method took care of registering results etc.
1087 if (!Res.getNode()) return false;
1088
1089 // If the result is N, the sub-method updated N in place. Tell the legalizer
1090 // core about this.
1091 if (Res.getNode() == N)
1092 return true;
1093
1094 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1095 "Invalid operand expansion");
1096
1097 ReplaceValueWith(SDValue(N, 0), Res);
1098 return false;
1099}
1100
1101/// If the value to convert is a vector that needs to be scalarized, it must be
1102/// <1 x ty>. Convert the element instead.
1103SDValue DAGTypeLegalizer::ScalarizeVecOp_BITCAST(SDNode *N) {
1104 SDValue Elt = GetScalarizedVector(N->getOperand(0));
1105 return DAG.getNode(ISD::BITCAST, SDLoc(N),
1106 N->getValueType(0), Elt);
1107}
1108
1109// Need to legalize vector operands of fake uses. Must be <1 x ty>.
1110SDValue DAGTypeLegalizer::ScalarizeVecOp_FAKE_USE(SDNode *N) {
1111 assert(N->getOperand(1).getValueType().getVectorNumElements() == 1 &&
1112 "Fake Use: Unexpected vector type!");
1113 SDValue Elt = GetScalarizedVector(N->getOperand(1));
1114 return DAG.getNode(ISD::FAKE_USE, SDLoc(), MVT::Other, N->getOperand(0), Elt);
1115}
1116
1117/// If the input is a vector that needs to be scalarized, it must be <1 x ty>.
1118/// Do the operation on the element instead.
1119SDValue DAGTypeLegalizer::ScalarizeVecOp_UnaryOp(SDNode *N) {
1120 assert(N->getValueType(0).getVectorNumElements() == 1 &&
1121 "Unexpected vector type!");
1122 SDValue Elt = GetScalarizedVector(N->getOperand(0));
1123 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N),
1124 N->getValueType(0).getScalarType(), Elt);
1125 // Revectorize the result so the types line up with what the uses of this
1126 // expression expect.
1127 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Op);
1128}
1129
1130/// Same as ScalarizeVecOp_UnaryOp with an extra operand (for example a
1131/// typesize).
1132SDValue DAGTypeLegalizer::ScalarizeVecOp_UnaryOpWithExtraInput(SDNode *N) {
1133 assert(N->getValueType(0).getVectorNumElements() == 1 &&
1134 "Unexpected vector type!");
1135 SDValue Elt = GetScalarizedVector(N->getOperand(0));
1136 SDValue Op =
1137 DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0).getScalarType(),
1138 Elt, N->getOperand(1));
1139 // Revectorize the result so the types line up with what the uses of this
1140 // expression expect.
1141 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Op);
1142}
1143
1144/// If the input is a vector that needs to be scalarized, it must be <1 x ty>.
1145/// Do the strict FP operation on the element instead.
1146SDValue DAGTypeLegalizer::ScalarizeVecOp_UnaryOp_StrictFP(SDNode *N) {
1147 assert(N->getValueType(0).getVectorNumElements() == 1 &&
1148 "Unexpected vector type!");
1149 SDValue Elt = GetScalarizedVector(N->getOperand(1));
1150 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N),
1151 { N->getValueType(0).getScalarType(), MVT::Other },
1152 { N->getOperand(0), Elt });
1153 // Legalize the chain result - switch anything that used the old chain to
1154 // use the new one.
1155 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1156 // Revectorize the result so the types line up with what the uses of this
1157 // expression expect.
1158 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Res);
1159
1160 // Do our own replacement and return SDValue() to tell the caller that we
1161 // handled all replacements since caller can only handle a single result.
1162 ReplaceValueWith(SDValue(N, 0), Res);
1163 return SDValue();
1164}
1165
1166/// The vectors to concatenate have length one - use a BUILD_VECTOR instead.
1167SDValue DAGTypeLegalizer::ScalarizeVecOp_CONCAT_VECTORS(SDNode *N) {
1168 SmallVector<SDValue, 8> Ops(N->getNumOperands());
1169 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
1170 Ops[i] = GetScalarizedVector(N->getOperand(i));
1171 return DAG.getBuildVector(N->getValueType(0), SDLoc(N), Ops);
1172}
1173
1174/// The inserted subvector is to be scalarized - use insert vector element
1175/// instead.
1176SDValue DAGTypeLegalizer::ScalarizeVecOp_INSERT_SUBVECTOR(SDNode *N,
1177 unsigned OpNo) {
1178 // We should not be attempting to scalarize the containing vector
1179 assert(OpNo == 1);
1180 SDValue Elt = GetScalarizedVector(N->getOperand(1));
1181 SDValue ContainingVec = N->getOperand(0);
1182 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N),
1183 ContainingVec.getValueType(), ContainingVec, Elt,
1184 N->getOperand(2));
1185}
1186
1187/// If the input is a vector that needs to be scalarized, it must be <1 x ty>,
1188/// so just return the element, ignoring the index.
1189SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
1190 EVT VT = N->getValueType(0);
1191 SDValue Res = GetScalarizedVector(N->getOperand(0));
1192 if (Res.getValueType() != VT)
1193 Res = VT.isFloatingPoint()
1194 ? DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Res)
1195 : DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Res);
1196 return Res;
1197}
1198
1199/// If the input condition is a vector that needs to be scalarized, it must be
1200/// <1 x i1>, so just convert to a normal ISD::SELECT
1201/// (still with vector output type since that was acceptable if we got here).
1202SDValue DAGTypeLegalizer::ScalarizeVecOp_VSELECT(SDNode *N) {
1203 SDValue ScalarCond = GetScalarizedVector(N->getOperand(0));
1204 EVT VT = N->getValueType(0);
1205
1206 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, ScalarCond, N->getOperand(1),
1207 N->getOperand(2));
1208}
1209
1210/// If the operand is a vector that needs to be scalarized then the
1211/// result must be a single-element vector, so just convert to a scalar
1212/// SETCC and wrap with a scalar_to_vector since the res type is legal
1213/// if we got here
1214SDValue DAGTypeLegalizer::ScalarizeVecOp_VSETCC(SDNode *N) {
1215 assert(N->getValueType(0).isVector() &&
1216 N->getOperand(0).getValueType().isVector() &&
1217 "Operand types must be vectors");
1218 assert(N->getValueType(0).getVectorNumElements() == 1 &&
1219 "Expected single-element vector type");
1220
1221 EVT VT = N->getValueType(0);
1222 SDValue LHS = GetScalarizedVector(N->getOperand(0));
1223 SDValue RHS = GetScalarizedVector(N->getOperand(1));
1224
1225 EVT OpVT = N->getOperand(0).getValueType();
1226 EVT NVT = VT.getVectorElementType();
1227 SDLoc DL(N);
1228 // Turn it into a scalar SETCC.
1229 SDValue Res = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS,
1230 N->getOperand(2));
1231
1232 // Vectors may have a different boolean contents to scalars. Promote the
1233 // value appropriately.
1234 ISD::NodeType ExtendCode =
1235 TargetLowering::getExtendForContent(TLI.getBooleanContents(OpVT));
1236
1237 Res = DAG.getNode(ExtendCode, DL, NVT, Res);
1238
1239 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Res);
1240}
1241
1242// Similiar to ScalarizeVecOp_VSETCC, with added logic to update chains.
1243SDValue DAGTypeLegalizer::ScalarizeVecOp_VSTRICT_FSETCC(SDNode *N,
1244 unsigned OpNo) {
1245 assert(OpNo == 1 && "Wrong operand for scalarization!");
1246 assert(N->getValueType(0).isVector() &&
1247 N->getOperand(1).getValueType().isVector() &&
1248 "Operand types must be vectors");
1249 assert(N->getValueType(0).getVectorNumElements() == 1 &&
1250 "Expected single-element vector type");
1251
1252 EVT VT = N->getValueType(0);
1253 SDValue Ch = N->getOperand(0);
1254 SDValue LHS = GetScalarizedVector(N->getOperand(1));
1255 SDValue RHS = GetScalarizedVector(N->getOperand(2));
1256 SDValue CC = N->getOperand(3);
1257
1258 EVT OpVT = N->getOperand(1).getValueType();
1259 EVT NVT = VT.getVectorElementType();
1260 SDLoc DL(N);
1261 SDValue Res = DAG.getNode(N->getOpcode(), DL, {MVT::i1, MVT::Other},
1262 {Ch, LHS, RHS, CC});
1263
1264 // Legalize the chain result - switch anything that used the old chain to
1265 // use the new one.
1266 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1267
1268 ISD::NodeType ExtendCode =
1269 TargetLowering::getExtendForContent(TLI.getBooleanContents(OpVT));
1270
1271 Res = DAG.getNode(ExtendCode, DL, NVT, Res);
1272 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Res);
1273
1274 // Do our own replacement and return SDValue() to tell the caller that we
1275 // handled all replacements since caller can only handle a single result.
1276 ReplaceValueWith(SDValue(N, 0), Res);
1277 return SDValue();
1278}
1279
1280/// If the value to store is a vector that needs to be scalarized, it must be
1281/// <1 x ty>. Just store the element.
1282SDValue DAGTypeLegalizer::ScalarizeVecOp_STORE(StoreSDNode *N, unsigned OpNo){
1283 assert(N->isUnindexed() && "Indexed store of one-element vector?");
1284 assert(OpNo == 1 && "Do not know how to scalarize this operand!");
1285 SDLoc dl(N);
1286
1287 if (N->isTruncatingStore())
1288 return DAG.getTruncStore(
1289 N->getChain(), dl, GetScalarizedVector(N->getOperand(1)),
1290 N->getBasePtr(), N->getPointerInfo(),
1291 N->getMemoryVT().getVectorElementType(), N->getBaseAlign(),
1292 N->getMemOperand()->getFlags(), N->getMMOMetadataForSubAccess());
1293
1294 return DAG.getStore(N->getChain(), dl, GetScalarizedVector(N->getOperand(1)),
1295 N->getBasePtr(), N->getPointerInfo(), N->getBaseAlign(),
1296 N->getMemOperand()->getFlags(),
1297 N->getMMOMetadataForSubAccess());
1298}
1299
1300/// If the value to store is a vector that needs to be scalarized, it must be
1301/// <1 x ty>. Just store the element.
1302SDValue DAGTypeLegalizer::ScalarizeVecOp_ATOMIC_STORE(AtomicSDNode *N) {
1303 SDValue ScalarVal = GetScalarizedVector(N->getVal());
1304 return DAG.getAtomic(ISD::ATOMIC_STORE, SDLoc(N),
1305 N->getMemoryVT().getVectorElementType(), N->getChain(),
1306 ScalarVal, N->getBasePtr(), N->getMemOperand());
1307}
1308
1309/// If the mask is a vector that needs to be scalarized, it must be <1 x i1>.
1310/// Promote it to a target boolean instead.
1311SDValue DAGTypeLegalizer::ScalarizeVecOp_MLOAD(MaskedLoadSDNode *N,
1312 unsigned OpNo) {
1313 assert(OpNo == 3 && "Can only scalarize the mask operand!");
1314 EVT DataVT = N->getValueType(0);
1315 SDValue Mask = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1316 SmallVector<SDValue, 4> NewOps(N->ops());
1317 NewOps[OpNo] = Mask;
1318 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
1319 if (Res == N)
1320 return SDValue(Res, 0);
1321
1322 // Update triggered CSE, do our own replacement since caller can't.
1323 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
1324 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
1325 return SDValue();
1326}
1327
1328/// If the mask is a vector that needs to be scalarized, it must be <1 x i1>.
1329/// Promote it to a target boolean instead.
1330SDValue DAGTypeLegalizer::ScalarizeVecOp_MSTORE(MaskedStoreSDNode *N,
1331 unsigned OpNo) {
1332 assert(!N->isCompressingStore() && "CompressingStore not expected");
1333 if (OpNo != 4)
1334 reportFatalInternalError("Scalarization of MSTORE data is not supported");
1335 EVT DataVT = N->getValue().getValueType();
1336 SmallVector<SDValue, 4> NewOps(N->ops());
1337 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1338 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1339}
1340
1341/// If the value to round is a vector that needs to be scalarized, it must be
1342/// <1 x ty>. Convert the element instead.
1343SDValue DAGTypeLegalizer::ScalarizeVecOp_FP_ROUND(SDNode *N, unsigned OpNo) {
1344 assert(OpNo == 0 && "Wrong operand for scalarization!");
1345 SDValue Elt = GetScalarizedVector(N->getOperand(0));
1346 SDValue Res = DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1347 N->getValueType(0).getVectorElementType(), Elt,
1348 N->getOperand(1));
1349 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Res);
1350}
1351
1352SDValue DAGTypeLegalizer::ScalarizeVecOp_STRICT_FP_ROUND(SDNode *N,
1353 unsigned OpNo) {
1354 assert(OpNo == 1 && "Wrong operand for scalarization!");
1355 SDValue Elt = GetScalarizedVector(N->getOperand(1));
1356 SDValue Res =
1357 DAG.getNode(ISD::STRICT_FP_ROUND, SDLoc(N),
1358 {N->getValueType(0).getVectorElementType(), MVT::Other},
1359 {N->getOperand(0), Elt, N->getOperand(2)});
1360 // Legalize the chain result - switch anything that used the old chain to
1361 // use the new one.
1362 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1363
1364 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Res);
1365
1366 // Do our own replacement and return SDValue() to tell the caller that we
1367 // handled all replacements since caller can only handle a single result.
1368 ReplaceValueWith(SDValue(N, 0), Res);
1369 return SDValue();
1370}
1371
1372/// If the value to extend is a vector that needs to be scalarized, it must be
1373/// <1 x ty>. Convert the element instead.
1374SDValue DAGTypeLegalizer::ScalarizeVecOp_FP_EXTEND(SDNode *N) {
1375 SDValue Elt = GetScalarizedVector(N->getOperand(0));
1376 SDValue Res = DAG.getNode(ISD::FP_EXTEND, SDLoc(N),
1377 N->getValueType(0).getVectorElementType(), Elt);
1378 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Res);
1379}
1380
1381/// If the value to extend is a vector that needs to be scalarized, it must be
1382/// <1 x ty>. Convert the element instead.
1383SDValue DAGTypeLegalizer::ScalarizeVecOp_STRICT_FP_EXTEND(SDNode *N) {
1384 SDValue Elt = GetScalarizedVector(N->getOperand(1));
1385 SDValue Res =
1386 DAG.getNode(ISD::STRICT_FP_EXTEND, SDLoc(N),
1387 {N->getValueType(0).getVectorElementType(), MVT::Other},
1388 {N->getOperand(0), Elt});
1389 // Legalize the chain result - switch anything that used the old chain to
1390 // use the new one.
1391 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1392
1393 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Res);
1394
1395 // Do our own replacement and return SDValue() to tell the caller that we
1396 // handled all replacements since caller can only handle a single result.
1397 ReplaceValueWith(SDValue(N, 0), Res);
1398 return SDValue();
1399}
1400
1401SDValue DAGTypeLegalizer::ScalarizeVecOp_VECREDUCE(SDNode *N) {
1402 SDValue Res = GetScalarizedVector(N->getOperand(0));
1403 // Result type may be wider than element type.
1404 if (Res.getValueType() != N->getValueType(0))
1405 Res = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Res);
1406 return Res;
1407}
1408
1409SDValue DAGTypeLegalizer::ScalarizeVecOp_VECREDUCE_SEQ(SDNode *N) {
1410 SDValue AccOp = N->getOperand(0);
1411 SDValue VecOp = N->getOperand(1);
1412
1413 unsigned BaseOpc = ISD::getVecReduceBaseOpcode(N->getOpcode());
1414
1415 SDValue Op = GetScalarizedVector(VecOp);
1416 return DAG.getNode(BaseOpc, SDLoc(N), N->getValueType(0),
1417 AccOp, Op, N->getFlags());
1418}
1419
1420SDValue DAGTypeLegalizer::ScalarizeVecOp_CMP(SDNode *N) {
1421 SDValue LHS = GetScalarizedVector(N->getOperand(0));
1422 SDValue RHS = GetScalarizedVector(N->getOperand(1));
1423
1424 EVT ResVT = N->getValueType(0).getVectorElementType();
1425 SDValue Cmp = DAG.getNode(N->getOpcode(), SDLoc(N), ResVT, LHS, RHS);
1426 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), N->getValueType(0), Cmp);
1427}
1428
1429SDValue DAGTypeLegalizer::ScalarizeVecOp_VECTOR_FIND_LAST_ACTIVE(SDNode *N) {
1430 // Since there is no "none-active" result, the only valid return for <1 x ty>
1431 // is 0. Note: Since we check the high mask during splitting this is safe.
1432 // As e.g., a <2 x ty> operation would split to:
1433 // any_active(%hi_mask) ? (1 + last_active(%hi_mask))
1434 // : `last_active(%lo_mask)`
1435 // Which then scalarizes to:
1436 // %mask[1] ? 1 : 0
1437 EVT VT = N->getValueType(0);
1438 return DAG.getConstant(0, SDLoc(N), VT);
1439}
1440
1441SDValue DAGTypeLegalizer::ScalarizeVecOp_CTTZ_ELTS(SDNode *N) {
1442 // The number of trailing zero elements is 1 if the element is 0, and 0
1443 // otherwise.
1444 if (N->getOpcode() == ISD::CTTZ_ELTS_ZERO_POISON)
1445 return DAG.getConstant(0, SDLoc(N), N->getValueType(0));
1446 SDValue Op = GetScalarizedVector(N->getOperand(0));
1447 SDValue SetCC =
1448 DAG.getSetCC(SDLoc(N), MVT::i1, Op,
1449 DAG.getConstant(0, SDLoc(N), Op.getValueType()), ISD::SETEQ);
1450 return DAG.getZExtOrTrunc(SetCC, SDLoc(N), N->getValueType(0));
1451}
1452
1453SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_MATCH(SDNode *N) {
1454 SDLoc DL(N);
1455 // Reuse the expansion (which should scalarize).
1456 SDValue Mask = TLI.expandVectorMatch(N, DAG);
1457 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
1458 N->getValueType(0).getScalarType(), Mask,
1459 DAG.getVectorIdxConstant(0, DL));
1460}
1461
1462SDValue DAGTypeLegalizer::ScalarizeVecOp_VECTOR_MATCH(SDNode *N,
1463 unsigned OpNo) {
1464 return TLI.expandVectorMatch(N, DAG);
1465}
1466
1467SDValue DAGTypeLegalizer::ScalarizeVecOp_MaskedBinOp(SDNode *N, unsigned OpNo) {
1468 assert(OpNo == 2 && "Can only scalarize mask operand");
1469 SDLoc DL(N);
1470 EVT VT = N->getOperand(0).getValueType().getVectorElementType();
1471 SDValue LHS = DAG.getExtractVectorElt(DL, VT, N->getOperand(0), 0);
1472 SDValue RHS = DAG.getExtractVectorElt(DL, VT, N->getOperand(1), 0);
1473 SDValue Mask = GetScalarizedVector(N->getOperand(2));
1474 // Vectors may have a different boolean contents to scalars, so truncate to i1
1475 // and let type legalization promote appropriately.
1476 Mask = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Mask);
1477 // Masked binary ops don't have UB on disabled lanes but produce poison, so
1478 // use 1 as the divisor to avoid division by zero and overflow.
1479 SDValue BinOp =
1480 DAG.getNode(ISD::getUnmaskedBinOpOpcode(N->getOpcode()), DL, VT, LHS,
1481 DAG.getSelect(DL, VT, Mask, RHS, DAG.getConstant(1, DL, VT)));
1482 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, N->getValueType(0), BinOp);
1483}
1484
1485//===----------------------------------------------------------------------===//
1486// Result Vector Splitting
1487//===----------------------------------------------------------------------===//
1488
1489/// This method is called when the specified result of the specified node is
1490/// found to need vector splitting. At this point, the node may also have
1491/// invalid operands or may have other results that need legalization, we just
1492/// know that (at least) one result needs vector splitting.
1493void DAGTypeLegalizer::SplitVectorResult(SDNode *N, unsigned ResNo) {
1494 LLVM_DEBUG(dbgs() << "Split node result: "; N->dump(&DAG));
1495 SDValue Lo, Hi;
1496
1497 // See if the target wants to custom expand this node.
1498 if (CustomLowerNode(N, N->getValueType(ResNo), true))
1499 return;
1500
1501 switch (N->getOpcode()) {
1502 default:
1503#ifndef NDEBUG
1504 dbgs() << "SplitVectorResult #" << ResNo << ": ";
1505 N->dump(&DAG);
1506 dbgs() << "\n";
1507#endif
1508 report_fatal_error("Do not know how to split the result of this "
1509 "operator!\n");
1510
1513 SplitVecRes_LOOP_DEPENDENCE_MASK(N, Lo, Hi);
1514 break;
1516 SplitVecRes_MASK_BEFOREFIRST(N, Lo, Hi);
1517 break;
1518 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
1519 case ISD::AssertZext: SplitVecRes_AssertZext(N, Lo, Hi); break;
1520 case ISD::AssertSext: SplitVecRes_AssertSext(N, Lo, Hi); break;
1521 case ISD::VSELECT:
1522 case ISD::SELECT:
1523 case ISD::VP_MERGE: SplitRes_Select(N, Lo, Hi); break;
1524 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
1525 case ISD::POISON:
1526 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
1527 case ISD::BITCAST: SplitVecRes_BITCAST(N, Lo, Hi); break;
1528 case ISD::BUILD_VECTOR: SplitVecRes_BUILD_VECTOR(N, Lo, Hi); break;
1529 case ISD::CONCAT_VECTORS: SplitVecRes_CONCAT_VECTORS(N, Lo, Hi); break;
1530 case ISD::EXTRACT_SUBVECTOR: SplitVecRes_EXTRACT_SUBVECTOR(N, Lo, Hi); break;
1531 case ISD::INSERT_SUBVECTOR: SplitVecRes_INSERT_SUBVECTOR(N, Lo, Hi); break;
1532 case ISD::FPOWI:
1533 case ISD::FLDEXP:
1534 case ISD::FCOPYSIGN: SplitVecRes_FPOp_MultiType(N, Lo, Hi); break;
1535 case ISD::IS_FPCLASS: SplitVecRes_IS_FPCLASS(N, Lo, Hi); break;
1536 case ISD::INSERT_VECTOR_ELT: SplitVecRes_INSERT_VECTOR_ELT(N, Lo, Hi); break;
1537 case ISD::SPLAT_VECTOR:
1539 SplitVecRes_ScalarOp(N, Lo, Hi);
1540 break;
1541 case ISD::STEP_VECTOR:
1542 SplitVecRes_STEP_VECTOR(N, Lo, Hi);
1543 break;
1544 case ISD::SIGN_EXTEND_INREG: SplitVecRes_InregOp(N, Lo, Hi); break;
1545 case ISD::ATOMIC_LOAD:
1546 SplitVecRes_ATOMIC_LOAD(cast<AtomicSDNode>(N), Lo, Hi);
1547 break;
1548 case ISD::LOAD:
1549 SplitVecRes_LOAD(cast<LoadSDNode>(N), Lo, Hi);
1550 break;
1551 case ISD::VP_LOAD:
1552 SplitVecRes_VP_LOAD(cast<VPLoadSDNode>(N), Lo, Hi);
1553 break;
1554 case ISD::VP_LOAD_FF:
1555 SplitVecRes_VP_LOAD_FF(cast<VPLoadFFSDNode>(N), Lo, Hi);
1556 break;
1557 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
1558 SplitVecRes_VP_STRIDED_LOAD(cast<VPStridedLoadSDNode>(N), Lo, Hi);
1559 break;
1560 case ISD::MLOAD:
1561 SplitVecRes_MLOAD(cast<MaskedLoadSDNode>(N), Lo, Hi);
1562 break;
1563 case ISD::MGATHER:
1564 case ISD::VP_GATHER:
1565 SplitVecRes_Gather(cast<MemSDNode>(N), Lo, Hi, /*SplitSETCC*/ true);
1566 break;
1568 SplitVecRes_VECTOR_COMPRESS(N, Lo, Hi);
1569 break;
1571 SplitVecRes_VECTOR_SHUFFLE_VAR(N, Lo, Hi);
1572 break;
1573 case ISD::SETCC:
1574 SplitVecRes_SETCC(N, Lo, Hi);
1575 break;
1576 case ISD::VECTOR_REPEAT:
1577 SplitVecRes_VECTOR_REPEAT(N, Lo, Hi);
1578 break;
1580 SplitVecRes_VECTOR_REVERSE(N, Lo, Hi);
1581 break;
1583 SplitVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N), Lo, Hi);
1584 break;
1587 SplitVecRes_VECTOR_SPLICE(N, Lo, Hi);
1588 break;
1590 SplitVecRes_VECTOR_DEINTERLEAVE(N);
1591 return;
1593 SplitVecRes_VECTOR_INTERLEAVE(N);
1594 return;
1595 case ISD::VAARG:
1596 SplitVecRes_VAARG(N, Lo, Hi);
1597 break;
1598
1602 SplitVecRes_ExtVecInRegOp(N, Lo, Hi);
1603 break;
1604
1605 case ISD::ABS:
1607 case ISD::BITREVERSE:
1608 case ISD::BSWAP:
1609 case ISD::CTLZ:
1610 case ISD::CTTZ:
1613 case ISD::CTPOP:
1614 case ISD::FABS:
1615 case ISD::FACOS:
1616 case ISD::FASIN:
1617 case ISD::FATAN:
1618 case ISD::FCEIL:
1619 case ISD::FCOS:
1620 case ISD::FCOSH:
1621 case ISD::FEXP:
1622 case ISD::FEXP2:
1623 case ISD::FEXP10:
1624 case ISD::FFLOOR:
1625 case ISD::FLOG:
1626 case ISD::FLOG10:
1627 case ISD::FLOG2:
1628 case ISD::FNEARBYINT:
1629 case ISD::FNEG:
1630 case ISD::FREEZE:
1631 case ISD::ARITH_FENCE:
1632 case ISD::FP_EXTEND:
1633 case ISD::FP_ROUND:
1634 case ISD::FP_TO_SINT:
1635 case ISD::FP_TO_UINT:
1636 case ISD::FRINT:
1637 case ISD::LRINT:
1638 case ISD::LLRINT:
1639 case ISD::FROUND:
1640 case ISD::FROUNDEVEN:
1641 case ISD::LROUND:
1642 case ISD::LLROUND:
1643 case ISD::FSIN:
1644 case ISD::FSINH:
1645 case ISD::FSQRT:
1646 case ISD::FTAN:
1647 case ISD::FTANH:
1648 case ISD::FTRUNC:
1649 case ISD::SINT_TO_FP:
1650 case ISD::TRUNCATE:
1651 case ISD::UINT_TO_FP:
1652 case ISD::FCANONICALIZE:
1656 SplitVecRes_UnaryOp(N, Lo, Hi);
1657 break;
1658 case ISD::ADDRSPACECAST:
1659 SplitVecRes_ADDRSPACECAST(N, Lo, Hi);
1660 break;
1661 case ISD::FMODF:
1662 case ISD::FFREXP:
1663 case ISD::FSINCOS:
1664 case ISD::FSINCOSPI:
1665 SplitVecRes_UnaryOpWithTwoResults(N, ResNo, Lo, Hi);
1666 break;
1667
1668 case ISD::ANY_EXTEND:
1669 case ISD::SIGN_EXTEND:
1670 case ISD::ZERO_EXTEND:
1671 SplitVecRes_ExtendOp(N, Lo, Hi);
1672 break;
1673
1674 case ISD::ADD:
1675 case ISD::SUB:
1676 case ISD::MUL:
1677 case ISD::CLMUL:
1678 case ISD::CLMULR:
1679 case ISD::CLMULH:
1680 case ISD::PEXT:
1681 case ISD::PDEP:
1682 case ISD::MULHS:
1683 case ISD::MULHU:
1684 case ISD::ABDS:
1685 case ISD::ABDU:
1686 case ISD::AVGCEILS:
1687 case ISD::AVGCEILU:
1688 case ISD::AVGFLOORS:
1689 case ISD::AVGFLOORU:
1690 case ISD::FADD:
1691 case ISD::FSUB:
1692 case ISD::FMUL:
1693 case ISD::FMINNUM:
1694 case ISD::FMINNUM_IEEE:
1695 case ISD::FMAXNUM:
1696 case ISD::FMAXNUM_IEEE:
1697 case ISD::FMINIMUM:
1698 case ISD::FMAXIMUM:
1699 case ISD::FMINIMUMNUM:
1700 case ISD::FMAXIMUMNUM:
1701 case ISD::SDIV: case ISD::VP_SDIV:
1702 case ISD::UDIV: case ISD::VP_UDIV:
1703 case ISD::FDIV:
1704 case ISD::FPOW:
1705 case ISD::FATAN2:
1706 case ISD::AND:
1707 case ISD::OR:
1708 case ISD::XOR:
1709 case ISD::SHL:
1710 case ISD::SRA:
1711 case ISD::SRL:
1712 case ISD::UREM: case ISD::VP_UREM:
1713 case ISD::SREM: case ISD::VP_SREM:
1714 case ISD::FREM:
1715 case ISD::SMIN:
1716 case ISD::SMAX:
1717 case ISD::UMIN:
1718 case ISD::UMAX:
1719 case ISD::SADDSAT:
1720 case ISD::UADDSAT:
1721 case ISD::SSUBSAT:
1722 case ISD::USUBSAT:
1723 case ISD::SSHLSAT:
1724 case ISD::USHLSAT:
1725 case ISD::ROTL:
1726 case ISD::ROTR:
1727 SplitVecRes_BinOp(N, Lo, Hi);
1728 break;
1729 case ISD::MASKED_UDIV:
1730 case ISD::MASKED_SDIV:
1731 case ISD::MASKED_UREM:
1732 case ISD::MASKED_SREM:
1733 SplitVecRes_MaskedBinOp(N, Lo, Hi);
1734 break;
1735 case ISD::FMA:
1736 case ISD::FSHL:
1737 case ISD::FSHR:
1738 SplitVecRes_TernaryOp(N, Lo, Hi);
1739 break;
1740
1741 case ISD::SCMP: case ISD::UCMP:
1742 SplitVecRes_CMP(N, Lo, Hi);
1743 break;
1744
1745#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
1746 case ISD::STRICT_##DAGN:
1747#include "llvm/IR/ConstrainedOps.def"
1748 SplitVecRes_StrictFPOp(N, Lo, Hi);
1749 break;
1750
1753 SplitVecRes_FP_TO_XINT_SAT(N, Lo, Hi);
1754 break;
1755
1756 case ISD::UADDO:
1757 case ISD::SADDO:
1758 case ISD::USUBO:
1759 case ISD::SSUBO:
1760 case ISD::UMULO:
1761 case ISD::SMULO:
1762 SplitVecRes_OverflowOp(N, ResNo, Lo, Hi);
1763 break;
1764 case ISD::SMULFIX:
1765 case ISD::SMULFIXSAT:
1766 case ISD::UMULFIX:
1767 case ISD::UMULFIXSAT:
1768 case ISD::SDIVFIX:
1769 case ISD::SDIVFIXSAT:
1770 case ISD::UDIVFIX:
1771 case ISD::UDIVFIXSAT:
1772 SplitVecRes_FIX(N, Lo, Hi);
1773 break;
1774 case ISD::EXPERIMENTAL_VP_SPLICE:
1775 SplitVecRes_VP_SPLICE(N, Lo, Hi);
1776 break;
1777 case ISD::EXPERIMENTAL_VP_REVERSE:
1778 SplitVecRes_VP_REVERSE(N, Lo, Hi);
1779 break;
1784 SplitVecRes_PARTIAL_REDUCE_MLA(N, Lo, Hi);
1785 break;
1787 SplitVecRes_GET_ACTIVE_LANE_MASK(N, Lo, Hi);
1788 break;
1789 case ISD::VECTOR_MATCH:
1790 SplitVecRes_VECTOR_MATCH(N, Lo, Hi);
1791 break;
1792 }
1793
1794 // If Lo/Hi is null, the sub-method took care of registering results etc.
1795 if (Lo.getNode())
1796 SetSplitVector(SDValue(N, ResNo), Lo, Hi);
1797}
1798
1799void DAGTypeLegalizer::IncrementPointer(MemSDNode *N, EVT MemVT,
1800 MachinePointerInfo &MPI, SDValue &Ptr,
1801 uint64_t *ScaledOffset) {
1802 SDLoc DL(N);
1803 unsigned IncrementSize = MemVT.getSizeInBits().getKnownMinValue() / 8;
1804
1805 if (MemVT.isScalableVector()) {
1806 SDValue BytesIncrement = DAG.getVScale(
1807 DL, Ptr.getValueType(),
1808 APInt(Ptr.getValueSizeInBits().getFixedValue(), IncrementSize));
1809 MPI = MachinePointerInfo(N->getPointerInfo().getAddrSpace());
1810 if (ScaledOffset)
1811 *ScaledOffset += IncrementSize;
1812 Ptr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr, BytesIncrement,
1814 } else {
1815 MPI = N->getPointerInfo().getWithOffset(IncrementSize);
1816 // Increment the pointer to the other half.
1817 Ptr = DAG.getObjectPtrOffset(DL, Ptr, TypeSize::getFixed(IncrementSize));
1818 }
1819}
1820
1821std::pair<SDValue, SDValue> DAGTypeLegalizer::SplitMask(SDValue Mask) {
1822 return SplitMask(Mask, SDLoc(Mask));
1823}
1824
1825std::pair<SDValue, SDValue> DAGTypeLegalizer::SplitMask(SDValue Mask,
1826 const SDLoc &DL) {
1827 SDValue MaskLo, MaskHi;
1828 EVT MaskVT = Mask.getValueType();
1829 if (getTypeAction(MaskVT) == TargetLowering::TypeSplitVector)
1830 GetSplitVector(Mask, MaskLo, MaskHi);
1831 else
1832 std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, DL);
1833 return std::make_pair(MaskLo, MaskHi);
1834}
1835
1836void DAGTypeLegalizer::SplitVecRes_BinOp(SDNode *N, SDValue &Lo, SDValue &Hi) {
1837 SDValue LHSLo, LHSHi;
1838 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
1839 SDValue RHSLo, RHSHi;
1840 GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
1841 SDLoc dl(N);
1842
1843 const SDNodeFlags Flags = N->getFlags();
1844 unsigned Opcode = N->getOpcode();
1845 if (N->getNumOperands() == 2) {
1846 Lo = DAG.getNode(Opcode, dl, LHSLo.getValueType(), LHSLo, RHSLo, Flags);
1847 Hi = DAG.getNode(Opcode, dl, LHSHi.getValueType(), LHSHi, RHSHi, Flags);
1848 return;
1849 }
1850
1851 assert(N->getNumOperands() == 4 && "Unexpected number of operands!");
1852 assert((N->getOpcode() == ISD::VP_UDIV || N->getOpcode() == ISD::VP_SDIV ||
1853 N->getOpcode() == ISD::VP_UREM || N->getOpcode() == ISD::VP_SREM) &&
1854 "Expected VP opcode");
1855
1856 SDValue MaskLo, MaskHi;
1857 std::tie(MaskLo, MaskHi) = SplitMask(N->getOperand(2));
1858
1859 SDValue EVLLo, EVLHi;
1860 std::tie(EVLLo, EVLHi) =
1861 DAG.SplitEVL(N->getOperand(3), N->getValueType(0), dl);
1862
1863 Lo = DAG.getNode(Opcode, dl, LHSLo.getValueType(),
1864 {LHSLo, RHSLo, MaskLo, EVLLo}, Flags);
1865 Hi = DAG.getNode(Opcode, dl, LHSHi.getValueType(),
1866 {LHSHi, RHSHi, MaskHi, EVLHi}, Flags);
1867}
1868
1869void DAGTypeLegalizer::SplitVecRes_MaskedBinOp(SDNode *N, SDValue &Lo,
1870 SDValue &Hi) {
1871 SDValue LHSLo, LHSHi;
1872 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
1873 SDValue RHSLo, RHSHi;
1874 GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
1875
1876 SDValue MaskLo, MaskHi, Mask = N->getOperand(2);
1877 if (Mask.getOpcode() == ISD::SETCC)
1878 SplitVecRes_SETCC(Mask.getNode(), MaskLo, MaskHi);
1879 else
1880 std::tie(MaskLo, MaskHi) = SplitMask(Mask);
1881
1882 SDLoc dl(N);
1883
1884 const SDNodeFlags Flags = N->getFlags();
1885 unsigned Opcode = N->getOpcode();
1886 Lo = DAG.getNode(Opcode, dl, LHSLo.getValueType(), LHSLo, RHSLo, MaskLo,
1887 Flags);
1888 Hi = DAG.getNode(Opcode, dl, LHSHi.getValueType(), LHSHi, RHSHi, MaskHi,
1889 Flags);
1890}
1891
1892void DAGTypeLegalizer::SplitVecRes_TernaryOp(SDNode *N, SDValue &Lo,
1893 SDValue &Hi) {
1894 SDValue Op0Lo, Op0Hi;
1895 GetSplitVector(N->getOperand(0), Op0Lo, Op0Hi);
1896 SDValue Op1Lo, Op1Hi;
1897 GetSplitVector(N->getOperand(1), Op1Lo, Op1Hi);
1898 SDValue Op2Lo, Op2Hi;
1899 GetSplitVector(N->getOperand(2), Op2Lo, Op2Hi);
1900 SDLoc dl(N);
1901
1902 const SDNodeFlags Flags = N->getFlags();
1903 unsigned Opcode = N->getOpcode();
1904 Lo =
1905 DAG.getNode(Opcode, dl, Op0Lo.getValueType(), Op0Lo, Op1Lo, Op2Lo, Flags);
1906 Hi =
1907 DAG.getNode(Opcode, dl, Op0Hi.getValueType(), Op0Hi, Op1Hi, Op2Hi, Flags);
1908}
1909
1910void DAGTypeLegalizer::SplitVecRes_CMP(SDNode *N, SDValue &Lo, SDValue &Hi) {
1911 LLVMContext &Ctxt = *DAG.getContext();
1912 SDLoc dl(N);
1913
1914 SDValue LHS = N->getOperand(0);
1915 SDValue RHS = N->getOperand(1);
1916
1917 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1918 if (getTypeAction(LHS.getValueType()) == TargetLowering::TypeSplitVector) {
1919 GetSplitVector(LHS, LHSLo, LHSHi);
1920 GetSplitVector(RHS, RHSLo, RHSHi);
1921 } else {
1922 std::tie(LHSLo, LHSHi) = DAG.SplitVector(LHS, dl);
1923 std::tie(RHSLo, RHSHi) = DAG.SplitVector(RHS, dl);
1924 }
1925
1926 EVT SplitResVT = N->getValueType(0).getHalfNumVectorElementsVT(Ctxt);
1927 Lo = DAG.getNode(N->getOpcode(), dl, SplitResVT, LHSLo, RHSLo);
1928 Hi = DAG.getNode(N->getOpcode(), dl, SplitResVT, LHSHi, RHSHi);
1929}
1930
1931void DAGTypeLegalizer::SplitVecRes_FIX(SDNode *N, SDValue &Lo, SDValue &Hi) {
1932 SDValue LHSLo, LHSHi;
1933 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
1934 SDValue RHSLo, RHSHi;
1935 GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
1936 SDLoc dl(N);
1937 SDValue Op2 = N->getOperand(2);
1938
1939 unsigned Opcode = N->getOpcode();
1940 Lo = DAG.getNode(Opcode, dl, LHSLo.getValueType(), LHSLo, RHSLo, Op2,
1941 N->getFlags());
1942 Hi = DAG.getNode(Opcode, dl, LHSHi.getValueType(), LHSHi, RHSHi, Op2,
1943 N->getFlags());
1944}
1945
1946void DAGTypeLegalizer::SplitVecRes_BITCAST(SDNode *N, SDValue &Lo,
1947 SDValue &Hi) {
1948 // We know the result is a vector. The input may be either a vector or a
1949 // scalar value.
1950 EVT LoVT, HiVT;
1951 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
1952 SDLoc dl(N);
1953
1954 SDValue InOp = N->getOperand(0);
1955 EVT InVT = InOp.getValueType();
1956
1957 // Handle some special cases efficiently.
1958 switch (getTypeAction(InVT)) {
1965 break;
1968 // A scalar to vector conversion, where the scalar needs expansion.
1969 // If the vector is being split in two then we can just convert the
1970 // expanded pieces.
1971 if (LoVT == HiVT) {
1972 GetExpandedOp(InOp, Lo, Hi);
1973 if (DAG.getDataLayout().isBigEndian())
1974 std::swap(Lo, Hi);
1975 Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
1976 Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
1977 return;
1978 }
1979 break;
1981 // If the input is a vector that needs to be split, convert each split
1982 // piece of the input now.
1983 GetSplitVector(InOp, Lo, Hi);
1984 Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
1985 Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
1986 return;
1988 report_fatal_error("scalarization of scalable vectors is not supported");
1989 }
1990
1991 if (LoVT.isScalableVector()) {
1992 auto [InLo, InHi] = DAG.SplitVectorOperand(N, 0);
1993 Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, InLo);
1994 Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, InHi);
1995 return;
1996 }
1997
1998 // In the general case, convert the input to an integer and split it by hand.
1999 EVT LoIntVT = EVT::getIntegerVT(*DAG.getContext(), LoVT.getSizeInBits());
2000 EVT HiIntVT = EVT::getIntegerVT(*DAG.getContext(), HiVT.getSizeInBits());
2001 if (DAG.getDataLayout().isBigEndian())
2002 std::swap(LoIntVT, HiIntVT);
2003
2004 SplitInteger(BitConvertToInteger(InOp), LoIntVT, HiIntVT, Lo, Hi);
2005
2006 if (DAG.getDataLayout().isBigEndian())
2007 std::swap(Lo, Hi);
2008 Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
2009 Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
2010}
2011
2012void DAGTypeLegalizer::SplitVecRes_LOOP_DEPENDENCE_MASK(SDNode *N, SDValue &Lo,
2013 SDValue &Hi) {
2014 SDLoc DL(N);
2015 EVT LoVT, HiVT;
2016 SDValue PtrA = N->getOperand(0);
2017 SDValue PtrB = N->getOperand(1);
2018 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
2019
2020 // The lane offset for the "Lo" half of the mask is unchanged.
2021 Lo = DAG.getNode(N->getOpcode(), DL, LoVT, PtrA, PtrB,
2022 /*ElementSizeInBytes=*/N->getOperand(2),
2023 /*LaneOffset=*/N->getOperand(3));
2024 // The lane offset for the "Hi" half of the mask is incremented by the number
2025 // of elements in the "Lo" half.
2026 unsigned LaneOffset =
2027 N->getConstantOperandVal(3) + LoVT.getVectorMinNumElements();
2028 // Note: The lane offset is implicitly scalable for scalable masks.
2029 Hi = DAG.getNode(N->getOpcode(), DL, HiVT, PtrA, PtrB,
2030 /*ElementSizeInBytes=*/N->getOperand(2),
2031 /*LaneOffset=*/DAG.getConstant(LaneOffset, DL, MVT::i64));
2032}
2033
2034void DAGTypeLegalizer::SplitVecRes_MASK_BEFOREFIRST(SDNode *N, SDValue &Lo,
2035 SDValue &Hi) {
2036 SDLoc DL(N);
2037 SDValue InLo, InHi;
2038 GetSplitVector(N->getOperand(0), InLo, InHi);
2039 EVT VT = InLo.getValueType();
2040 Lo = DAG.getNode(ISD::MASK_BEFOREFIRST, DL, VT, InLo);
2041
2042 // hi = AnyLoActive ? all-zeros : (mask_beforefirst hi)
2043 SDValue AnyLoActive = DAG.getNode(ISD::VECREDUCE_OR, DL, MVT::i1, InLo);
2044 SDValue Cond = DAG.getBoolExtOrTrunc(AnyLoActive, DL,
2045 getSetCCResultType(MVT::i1), MVT::i1);
2046 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, DAG.getConstant(0, DL, VT),
2047 DAG.getNode(ISD::MASK_BEFOREFIRST, DL, VT, InHi));
2048}
2049
2050void DAGTypeLegalizer::SplitVecRes_BUILD_VECTOR(SDNode *N, SDValue &Lo,
2051 SDValue &Hi) {
2052 EVT LoVT, HiVT;
2053 SDLoc dl(N);
2054 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
2055 unsigned LoNumElts = LoVT.getVectorNumElements();
2056 SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+LoNumElts);
2057 Lo = DAG.getBuildVector(LoVT, dl, LoOps);
2058
2059 SmallVector<SDValue, 8> HiOps(N->op_begin()+LoNumElts, N->op_end());
2060 Hi = DAG.getBuildVector(HiVT, dl, HiOps);
2061}
2062
2063void DAGTypeLegalizer::SplitVecRes_CONCAT_VECTORS(SDNode *N, SDValue &Lo,
2064 SDValue &Hi) {
2065 assert(!(N->getNumOperands() & 1) && "Unsupported CONCAT_VECTORS");
2066 SDLoc dl(N);
2067 unsigned NumSubvectors = N->getNumOperands() / 2;
2068 if (NumSubvectors == 1) {
2069 Lo = N->getOperand(0);
2070 Hi = N->getOperand(1);
2071 return;
2072 }
2073
2074 EVT LoVT, HiVT;
2075 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
2076
2077 SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+NumSubvectors);
2078 Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, LoVT, LoOps);
2079
2080 SmallVector<SDValue, 8> HiOps(N->op_begin()+NumSubvectors, N->op_end());
2081 Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, HiVT, HiOps);
2082}
2083
2084void DAGTypeLegalizer::SplitVecRes_EXTRACT_SUBVECTOR(SDNode *N, SDValue &Lo,
2085 SDValue &Hi) {
2086 SDValue Vec = N->getOperand(0);
2087 SDValue Idx = N->getOperand(1);
2088 SDLoc dl(N);
2089
2090 EVT LoVT, HiVT;
2091 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
2092
2093 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, LoVT, Vec, Idx);
2094 uint64_t IdxVal = Idx->getAsZExtVal();
2095 Hi = DAG.getNode(
2096 ISD::EXTRACT_SUBVECTOR, dl, HiVT, Vec,
2097 DAG.getVectorIdxConstant(IdxVal + LoVT.getVectorMinNumElements(), dl));
2098}
2099
2100void DAGTypeLegalizer::SplitVecRes_INSERT_SUBVECTOR(SDNode *N, SDValue &Lo,
2101 SDValue &Hi) {
2102 SDValue Vec = N->getOperand(0);
2103 SDValue SubVec = N->getOperand(1);
2104 SDValue Idx = N->getOperand(2);
2105 SDLoc dl(N);
2106 GetSplitVector(Vec, Lo, Hi);
2107
2108 EVT VecVT = Vec.getValueType();
2109 EVT LoVT = Lo.getValueType();
2110 EVT SubVecVT = SubVec.getValueType();
2111 unsigned VecElems = VecVT.getVectorMinNumElements();
2112 unsigned SubElems = SubVecVT.getVectorMinNumElements();
2113 unsigned LoElems = LoVT.getVectorMinNumElements();
2114
2115 // If we know the index is in the first half, and we know the subvector
2116 // doesn't cross the boundary between the halves, we can avoid spilling the
2117 // vector, and insert into the lower half of the split vector directly.
2118 unsigned IdxVal = Idx->getAsZExtVal();
2119 if (IdxVal + SubElems <= LoElems) {
2120 Lo = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, LoVT, Lo, SubVec, Idx);
2121 return;
2122 }
2123 // Similarly if the subvector is fully in the high half, but mind that we
2124 // can't tell whether a fixed-length subvector is fully within the high half
2125 // of a scalable vector.
2126 if (VecVT.isScalableVector() == SubVecVT.isScalableVector() &&
2127 IdxVal >= LoElems && IdxVal + SubElems <= VecElems) {
2128 Hi = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, Hi.getValueType(), Hi, SubVec,
2129 DAG.getVectorIdxConstant(IdxVal - LoElems, dl));
2130 return;
2131 }
2132
2133 if (getTypeAction(SubVecVT) == TargetLowering::TypeWidenVector &&
2134 Vec.isUndef() && SubVecVT.getVectorElementType() == MVT::i1) {
2135 SDValue WideSubVec = GetWidenedVector(SubVec);
2136 if (WideSubVec.getValueType() == VecVT) {
2137 std::tie(Lo, Hi) = DAG.SplitVector(WideSubVec, SDLoc(WideSubVec));
2138 return;
2139 }
2140 }
2141
2142 // Spill the vector to the stack.
2143 // In cases where the vector is illegal it will be broken down into parts
2144 // and stored in parts - we should use the alignment for the smallest part.
2145 Align SmallestAlign = DAG.getReducedAlign(VecVT, /*UseABI=*/false);
2147 DAG.CreateStackTemporary(VecVT.getStoreSize(), SmallestAlign);
2148 auto &MF = DAG.getMachineFunction();
2149 auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
2150 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
2151
2152 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
2153 SmallestAlign);
2154
2155 // Store the new subvector into the specified index.
2156 SDValue SubVecPtr =
2157 TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, SubVecVT, Idx);
2158 Store = DAG.getStore(Store, dl, SubVec, SubVecPtr,
2160
2161 // Load the Lo part from the stack slot.
2162 Lo = DAG.getLoad(Lo.getValueType(), dl, Store, StackPtr, PtrInfo,
2163 SmallestAlign);
2164
2165 // Increment the pointer to the other part.
2166 auto *Load = cast<LoadSDNode>(Lo);
2167 MachinePointerInfo MPI = Load->getPointerInfo();
2168 IncrementPointer(Load, LoVT, MPI, StackPtr);
2169
2170 // Load the Hi part from the stack slot.
2171 Hi = DAG.getLoad(Hi.getValueType(), dl, Store, StackPtr, MPI, SmallestAlign);
2172}
2173
2174// Handle splitting an FP where the second operand does not match the first
2175// type. The second operand may be a scalar, or a vector that has exactly as
2176// many elements as the first
2177void DAGTypeLegalizer::SplitVecRes_FPOp_MultiType(SDNode *N, SDValue &Lo,
2178 SDValue &Hi) {
2179 SDValue LHSLo, LHSHi;
2180 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
2181 SDLoc DL(N);
2182
2183 SDValue RHSLo, RHSHi;
2184 SDValue RHS = N->getOperand(1);
2185 EVT RHSVT = RHS.getValueType();
2186 if (RHSVT.isVector()) {
2187 if (getTypeAction(RHSVT) == TargetLowering::TypeSplitVector)
2188 GetSplitVector(RHS, RHSLo, RHSHi);
2189 else
2190 std::tie(RHSLo, RHSHi) = DAG.SplitVector(RHS, SDLoc(RHS));
2191
2192 Lo = DAG.getNode(N->getOpcode(), DL, LHSLo.getValueType(), LHSLo, RHSLo);
2193 Hi = DAG.getNode(N->getOpcode(), DL, LHSHi.getValueType(), LHSHi, RHSHi);
2194 } else {
2195 Lo = DAG.getNode(N->getOpcode(), DL, LHSLo.getValueType(), LHSLo, RHS);
2196 Hi = DAG.getNode(N->getOpcode(), DL, LHSHi.getValueType(), LHSHi, RHS);
2197 }
2198}
2199
2200void DAGTypeLegalizer::SplitVecRes_IS_FPCLASS(SDNode *N, SDValue &Lo,
2201 SDValue &Hi) {
2202 SDLoc DL(N);
2203 SDValue ArgLo, ArgHi;
2204 SDValue Test = N->getOperand(1);
2205 SDValue FpValue = N->getOperand(0);
2206 if (getTypeAction(FpValue.getValueType()) == TargetLowering::TypeSplitVector)
2207 GetSplitVector(FpValue, ArgLo, ArgHi);
2208 else
2209 std::tie(ArgLo, ArgHi) = DAG.SplitVector(FpValue, SDLoc(FpValue));
2210 EVT LoVT, HiVT;
2211 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
2212
2213 Lo = DAG.getNode(ISD::IS_FPCLASS, DL, LoVT, ArgLo, Test, N->getFlags());
2214 Hi = DAG.getNode(ISD::IS_FPCLASS, DL, HiVT, ArgHi, Test, N->getFlags());
2215}
2216
2217void DAGTypeLegalizer::SplitVecRes_InregOp(SDNode *N, SDValue &Lo,
2218 SDValue &Hi) {
2219 SDValue LHSLo, LHSHi;
2220 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
2221 SDLoc dl(N);
2222
2223 EVT LoVT, HiVT;
2224 std::tie(LoVT, HiVT) =
2225 DAG.GetSplitDestVTs(cast<VTSDNode>(N->getOperand(1))->getVT());
2226
2227 Lo = DAG.getNode(N->getOpcode(), dl, LHSLo.getValueType(), LHSLo,
2228 DAG.getValueType(LoVT));
2229 Hi = DAG.getNode(N->getOpcode(), dl, LHSHi.getValueType(), LHSHi,
2230 DAG.getValueType(HiVT));
2231}
2232
2233void DAGTypeLegalizer::SplitVecRes_ExtVecInRegOp(SDNode *N, SDValue &Lo,
2234 SDValue &Hi) {
2235 unsigned Opcode = N->getOpcode();
2236 SDValue N0 = N->getOperand(0);
2237
2238 SDLoc dl(N);
2239 SDValue InLo, InHi;
2240
2241 if (getTypeAction(N0.getValueType()) == TargetLowering::TypeSplitVector)
2242 GetSplitVector(N0, InLo, InHi);
2243 else
2244 std::tie(InLo, InHi) = DAG.SplitVectorOperand(N, 0);
2245
2246 EVT InLoVT = InLo.getValueType();
2247 unsigned InNumElements = InLoVT.getVectorNumElements();
2248
2249 EVT OutLoVT, OutHiVT;
2250 std::tie(OutLoVT, OutHiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
2251 unsigned OutNumElements = OutLoVT.getVectorNumElements();
2252 assert((2 * OutNumElements) <= InNumElements &&
2253 "Illegal extend vector in reg split");
2254
2255 // *_EXTEND_VECTOR_INREG instructions extend the lowest elements of the
2256 // input vector (i.e. we only use InLo):
2257 // OutLo will extend the first OutNumElements from InLo.
2258 // OutHi will extend the next OutNumElements from InLo.
2259
2260 // Shuffle the elements from InLo for OutHi into the bottom elements to
2261 // create a 'fake' InHi.
2262 SmallVector<int, 8> SplitHi(InNumElements, -1);
2263 for (unsigned i = 0; i != OutNumElements; ++i)
2264 SplitHi[i] = i + OutNumElements;
2265 InHi = DAG.getVectorShuffle(InLoVT, dl, InLo, DAG.getPOISON(InLoVT), SplitHi);
2266
2267 Lo = DAG.getNode(Opcode, dl, OutLoVT, InLo);
2268 Hi = DAG.getNode(Opcode, dl, OutHiVT, InHi);
2269}
2270
2271void DAGTypeLegalizer::SplitVecRes_StrictFPOp(SDNode *N, SDValue &Lo,
2272 SDValue &Hi) {
2273 unsigned NumOps = N->getNumOperands();
2274 SDValue Chain = N->getOperand(0);
2275 EVT LoVT, HiVT;
2276 SDLoc dl(N);
2277 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
2278
2281
2282 // The Chain is the first operand.
2283 OpsLo[0] = Chain;
2284 OpsHi[0] = Chain;
2285
2286 // Now process the remaining operands.
2287 for (unsigned i = 1; i < NumOps; ++i) {
2288 SDValue Op = N->getOperand(i);
2289 SDValue OpLo = Op;
2290 SDValue OpHi = Op;
2291
2292 EVT InVT = Op.getValueType();
2293 if (InVT.isVector()) {
2294 // If the input also splits, handle it directly for a
2295 // compile time speedup. Otherwise split it by hand.
2296 if (getTypeAction(InVT) == TargetLowering::TypeSplitVector)
2297 GetSplitVector(Op, OpLo, OpHi);
2298 else
2299 std::tie(OpLo, OpHi) = DAG.SplitVectorOperand(N, i);
2300 }
2301
2302 OpsLo[i] = OpLo;
2303 OpsHi[i] = OpHi;
2304 }
2305
2306 EVT LoValueVTs[] = {LoVT, MVT::Other};
2307 EVT HiValueVTs[] = {HiVT, MVT::Other};
2308 Lo = DAG.getNode(N->getOpcode(), dl, DAG.getVTList(LoValueVTs), OpsLo,
2309 N->getFlags());
2310 Hi = DAG.getNode(N->getOpcode(), dl, DAG.getVTList(HiValueVTs), OpsHi,
2311 N->getFlags());
2312
2313 // Build a factor node to remember that this Op is independent of the
2314 // other one.
2315 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2316 Lo.getValue(1), Hi.getValue(1));
2317
2318 // Legalize the chain result - switch anything that used the old chain to
2319 // use the new one.
2320 ReplaceValueWith(SDValue(N, 1), Chain);
2321}
2322
2323SDValue DAGTypeLegalizer::UnrollVectorOp_StrictFP(SDNode *N, unsigned ResNE) {
2324 EVT VT = N->getValueType(0);
2325 if (VT.isScalableVector())
2326 report_fatal_error("Cannot unroll scalable vector strict-fp operation!");
2327
2328 SDValue Chain = N->getOperand(0);
2329 unsigned NE = VT.getVectorNumElements();
2330 EVT EltVT = VT.getVectorElementType();
2331 SDLoc dl(N);
2332
2334 SmallVector<SDValue, 4> Operands(N->getNumOperands());
2335
2336 // If ResNE is 0, fully unroll the vector op.
2337 if (ResNE == 0)
2338 ResNE = NE;
2339 else if (NE > ResNE)
2340 NE = ResNE;
2341
2342 //The results of each unrolled operation, including the chain.
2343 SDVTList ChainVTs = DAG.getVTList(EltVT, MVT::Other);
2345
2346 unsigned i;
2347 for (i = 0; i != NE; ++i) {
2348 Operands[0] = Chain;
2349 for (unsigned j = 1, e = N->getNumOperands(); j != e; ++j) {
2350 SDValue Operand = N->getOperand(j);
2351 EVT OperandVT = Operand.getValueType();
2352 if (OperandVT.isVector()) {
2353 EVT OperandEltVT = OperandVT.getVectorElementType();
2354 Operands[j] = DAG.getExtractVectorElt(dl, OperandEltVT, Operand, i);
2355 } else {
2356 Operands[j] = Operand;
2357 }
2358 }
2359 SDValue Scalar =
2360 DAG.getNode(N->getOpcode(), dl, ChainVTs, Operands, N->getFlags());
2361
2362 //Add in the scalar as well as its chain value to the
2363 //result vectors.
2364 Scalars.push_back(Scalar);
2365 Chains.push_back(Scalar.getValue(1));
2366 }
2367
2368 for (; i < ResNE; ++i)
2369 Scalars.push_back(DAG.getPOISON(EltVT));
2370
2371 // Build a new factor node to connect the chain back together.
2372 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
2373 ReplaceValueWith(SDValue(N, 1), Chain);
2374
2375 // Create a new BUILD_VECTOR node
2376 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, ResNE);
2377 return DAG.getBuildVector(VecVT, dl, Scalars);
2378}
2379
2380void DAGTypeLegalizer::SplitVecRes_OverflowOp(SDNode *N, unsigned ResNo,
2381 SDValue &Lo, SDValue &Hi) {
2382 SDLoc dl(N);
2383 EVT ResVT = N->getValueType(0);
2384 EVT OvVT = N->getValueType(1);
2385 EVT LoResVT, HiResVT, LoOvVT, HiOvVT;
2386 std::tie(LoResVT, HiResVT) = DAG.GetSplitDestVTs(ResVT);
2387 std::tie(LoOvVT, HiOvVT) = DAG.GetSplitDestVTs(OvVT);
2388
2389 SDValue LoLHS, HiLHS, LoRHS, HiRHS;
2390 if (getTypeAction(ResVT) == TargetLowering::TypeSplitVector) {
2391 GetSplitVector(N->getOperand(0), LoLHS, HiLHS);
2392 GetSplitVector(N->getOperand(1), LoRHS, HiRHS);
2393 } else {
2394 std::tie(LoLHS, HiLHS) = DAG.SplitVectorOperand(N, 0);
2395 std::tie(LoRHS, HiRHS) = DAG.SplitVectorOperand(N, 1);
2396 }
2397
2398 unsigned Opcode = N->getOpcode();
2399 SDVTList LoVTs = DAG.getVTList(LoResVT, LoOvVT);
2400 SDVTList HiVTs = DAG.getVTList(HiResVT, HiOvVT);
2401 SDNode *LoNode =
2402 DAG.getNode(Opcode, dl, LoVTs, {LoLHS, LoRHS}, N->getFlags()).getNode();
2403 SDNode *HiNode =
2404 DAG.getNode(Opcode, dl, HiVTs, {HiLHS, HiRHS}, N->getFlags()).getNode();
2405
2406 Lo = SDValue(LoNode, ResNo);
2407 Hi = SDValue(HiNode, ResNo);
2408
2409 // Replace the other vector result not being explicitly split here.
2410 unsigned OtherNo = 1 - ResNo;
2411 EVT OtherVT = N->getValueType(OtherNo);
2412 if (getTypeAction(OtherVT) == TargetLowering::TypeSplitVector) {
2413 SetSplitVector(SDValue(N, OtherNo),
2414 SDValue(LoNode, OtherNo), SDValue(HiNode, OtherNo));
2415 } else {
2416 SDValue OtherVal = DAG.getNode(
2417 ISD::CONCAT_VECTORS, dl, OtherVT,
2418 SDValue(LoNode, OtherNo), SDValue(HiNode, OtherNo));
2419 ReplaceValueWith(SDValue(N, OtherNo), OtherVal);
2420 }
2421}
2422
2423void DAGTypeLegalizer::SplitVecRes_INSERT_VECTOR_ELT(SDNode *N, SDValue &Lo,
2424 SDValue &Hi) {
2425 SDValue Vec = N->getOperand(0);
2426 SDValue Elt = N->getOperand(1);
2427 SDValue Idx = N->getOperand(2);
2428 SDLoc dl(N);
2429 GetSplitVector(Vec, Lo, Hi);
2430
2431 if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
2432 unsigned IdxVal = CIdx->getZExtValue();
2433 unsigned LoNumElts = Lo.getValueType().getVectorMinNumElements();
2434 if (IdxVal < LoNumElts) {
2435 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
2436 Lo.getValueType(), Lo, Elt, Idx);
2437 return;
2438 } else if (!Vec.getValueType().isScalableVector()) {
2439 Hi = DAG.getInsertVectorElt(dl, Hi, Elt, IdxVal - LoNumElts);
2440 return;
2441 }
2442 }
2443
2444 // Make the vector elements byte-addressable if they aren't already.
2445 EVT VecVT = Vec.getValueType();
2446 EVT EltVT = VecVT.getVectorElementType();
2447 if (!EltVT.isByteSized()) {
2448 EltVT = EltVT.changeTypeToInteger().getRoundIntegerType(*DAG.getContext());
2449 VecVT = VecVT.changeElementType(*DAG.getContext(), EltVT);
2450 Vec = DAG.getNode(ISD::ANY_EXTEND, dl, VecVT, Vec);
2451 // Extend the element type to match if needed.
2452 if (EltVT.bitsGT(Elt.getValueType()))
2453 Elt = DAG.getNode(ISD::ANY_EXTEND, dl, EltVT, Elt);
2454 }
2455
2456 // Spill the vector to the stack.
2457 // In cases where the vector is illegal it will be broken down into parts
2458 // and stored in parts - we should use the alignment for the smallest part.
2459 Align SmallestAlign = DAG.getReducedAlign(VecVT, /*UseABI=*/false);
2461 DAG.CreateStackTemporary(VecVT.getStoreSize(), SmallestAlign);
2462 auto &MF = DAG.getMachineFunction();
2463 auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
2464 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
2465
2466 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
2467 SmallestAlign);
2468
2469 // Store the new element. This may be larger than the vector element type,
2470 // so use a truncating store.
2471 SDValue EltPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
2472 Store = DAG.getTruncStore(
2473 Store, dl, Elt, EltPtr, MachinePointerInfo::getUnknownStack(MF), EltVT,
2474 commonAlignment(SmallestAlign,
2475 EltVT.getFixedSizeInBits() / 8));
2476
2477 EVT LoVT, HiVT;
2478 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VecVT);
2479
2480 // Load the Lo part from the stack slot.
2481 Lo = DAG.getLoad(LoVT, dl, Store, StackPtr, PtrInfo, SmallestAlign);
2482
2483 // Increment the pointer to the other part.
2484 auto Load = cast<LoadSDNode>(Lo);
2485 MachinePointerInfo MPI = Load->getPointerInfo();
2486 IncrementPointer(Load, LoVT, MPI, StackPtr);
2487
2488 Hi = DAG.getLoad(HiVT, dl, Store, StackPtr, MPI, SmallestAlign);
2489
2490 // If we adjusted the original type, we need to truncate the results.
2491 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
2492 if (LoVT != Lo.getValueType())
2493 Lo = DAG.getNode(ISD::TRUNCATE, dl, LoVT, Lo);
2494 if (HiVT != Hi.getValueType())
2495 Hi = DAG.getNode(ISD::TRUNCATE, dl, HiVT, Hi);
2496}
2497
2498void DAGTypeLegalizer::SplitVecRes_STEP_VECTOR(SDNode *N, SDValue &Lo,
2499 SDValue &Hi) {
2500 EVT LoVT, HiVT;
2501 SDLoc dl(N);
2502 assert(N->getValueType(0).isScalableVector() &&
2503 "Only scalable vectors are supported for STEP_VECTOR");
2504 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
2505 SDValue Step = N->getOperand(0);
2506
2507 Lo = DAG.getNode(ISD::STEP_VECTOR, dl, LoVT, Step);
2508
2509 // Hi = Lo + (EltCnt * Step)
2510 EVT EltVT = Step.getValueType();
2511 APInt StepVal = Step->getAsAPIntVal();
2512 SDValue StartOfHi =
2513 DAG.getVScale(dl, EltVT, StepVal * LoVT.getVectorMinNumElements());
2514 StartOfHi = DAG.getSExtOrTrunc(StartOfHi, dl, HiVT.getVectorElementType());
2515 StartOfHi = DAG.getNode(ISD::SPLAT_VECTOR, dl, HiVT, StartOfHi);
2516
2517 Hi = DAG.getNode(ISD::STEP_VECTOR, dl, HiVT, Step);
2518 Hi = DAG.getNode(ISD::ADD, dl, HiVT, Hi, StartOfHi);
2519}
2520
2521void DAGTypeLegalizer::SplitVecRes_ScalarOp(SDNode *N, SDValue &Lo,
2522 SDValue &Hi) {
2523 EVT LoVT, HiVT;
2524 SDLoc dl(N);
2525 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
2526 Lo = DAG.getNode(N->getOpcode(), dl, LoVT, N->getOperand(0));
2527 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2528 Hi = DAG.getPOISON(HiVT);
2529 } else {
2530 assert(N->getOpcode() == ISD::SPLAT_VECTOR && "Unexpected opcode");
2531 Hi = Lo;
2532 }
2533}
2534
2535void DAGTypeLegalizer::SplitVecRes_ATOMIC_LOAD(AtomicSDNode *LD, SDValue &Lo,
2536 SDValue &Hi) {
2537 assert(LD->getExtensionType() == ISD::NON_EXTLOAD &&
2538 "Extended load during type legalization!");
2539 SDLoc dl(LD);
2540 EVT VT = LD->getValueType(0);
2541 EVT LoVT, HiVT;
2542 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
2543
2544 SDValue Ch = LD->getChain();
2545 SDValue Ptr = LD->getBasePtr();
2546
2547 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
2548 EVT MemIntVT =
2549 EVT::getIntegerVT(*DAG.getContext(), LD->getMemoryVT().getSizeInBits());
2550 SDValue ALD = DAG.getAtomicLoad(LD->getExtensionType(), dl, MemIntVT, IntVT,
2551 Ch, Ptr, LD->getMemOperand());
2552
2553 EVT LoIntVT = EVT::getIntegerVT(*DAG.getContext(), LoVT.getSizeInBits());
2554 EVT HiIntVT = EVT::getIntegerVT(*DAG.getContext(), HiVT.getSizeInBits());
2555 SDValue ExtractLo, ExtractHi;
2556 SplitInteger(ALD, LoIntVT, HiIntVT, ExtractLo, ExtractHi);
2557
2558 Lo = DAG.getBitcast(LoVT, ExtractLo);
2559 Hi = DAG.getBitcast(HiVT, ExtractHi);
2560
2561 // Legalize the chain result - switch anything that used the old chain to
2562 // use the new one.
2563 ReplaceValueWith(SDValue(LD, 1), ALD.getValue(1));
2564}
2565
2566void DAGTypeLegalizer::SplitVecRes_LOAD(LoadSDNode *LD, SDValue &Lo,
2567 SDValue &Hi) {
2568 assert(ISD::isUNINDEXEDLoad(LD) && "Indexed load during type legalization!");
2569 EVT LoVT, HiVT;
2570 SDLoc dl(LD);
2571 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(LD->getValueType(0));
2572
2573 ISD::LoadExtType ExtType = LD->getExtensionType();
2574 SDValue Ch = LD->getChain();
2575 SDValue Ptr = LD->getBasePtr();
2576 SDValue Offset = DAG.getPOISON(Ptr.getValueType());
2577 EVT MemoryVT = LD->getMemoryVT();
2578 MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags();
2579 MMOMetadata Metadata = LD->getMMOMetadataForSubAccess();
2580
2581 EVT LoMemVT, HiMemVT;
2582 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
2583
2584 if (!LoMemVT.isByteSized() || !HiMemVT.isByteSized()) {
2585 SDValue Value, NewChain;
2586 std::tie(Value, NewChain) = TLI.scalarizeVectorLoad(LD, DAG);
2587 std::tie(Lo, Hi) = DAG.SplitVector(Value, dl);
2588 ReplaceValueWith(SDValue(LD, 1), NewChain);
2589 return;
2590 }
2591
2592 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType, LoVT, dl, Ch, Ptr, Offset,
2593 LD->getPointerInfo(), LoMemVT, LD->getBaseAlign(), MMOFlags,
2594 Metadata);
2595
2596 MachinePointerInfo MPI;
2597 IncrementPointer(LD, LoMemVT, MPI, Ptr);
2598
2599 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType, HiVT, dl, Ch, Ptr, Offset, MPI,
2600 HiMemVT, LD->getBaseAlign(), MMOFlags, Metadata);
2601
2602 // Build a factor node to remember that this load is independent of the
2603 // other one.
2604 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
2605 Hi.getValue(1));
2606
2607 // Legalize the chain result - switch anything that used the old chain to
2608 // use the new one.
2609 ReplaceValueWith(SDValue(LD, 1), Ch);
2610}
2611
2612void DAGTypeLegalizer::SplitVecRes_VP_LOAD(VPLoadSDNode *LD, SDValue &Lo,
2613 SDValue &Hi) {
2614 assert(LD->isUnindexed() && "Indexed VP load during type legalization!");
2615 EVT LoVT, HiVT;
2616 SDLoc dl(LD);
2617 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(LD->getValueType(0));
2618
2619 ISD::LoadExtType ExtType = LD->getExtensionType();
2620 SDValue Ch = LD->getChain();
2621 SDValue Ptr = LD->getBasePtr();
2622 SDValue Offset = LD->getOffset();
2623 assert(Offset.isUndef() && "Unexpected indexed variable-length load offset");
2624 Align Alignment = LD->getBaseAlign();
2625 SDValue Mask = LD->getMask();
2626 SDValue EVL = LD->getVectorLength();
2627 EVT MemoryVT = LD->getMemoryVT();
2628
2629 EVT LoMemVT, HiMemVT;
2630 bool HiIsEmpty = false;
2631 std::tie(LoMemVT, HiMemVT) =
2632 DAG.GetDependentSplitDestVTs(MemoryVT, LoVT, &HiIsEmpty);
2633
2634 // Split Mask operand
2635 SDValue MaskLo, MaskHi;
2636 if (Mask.getOpcode() == ISD::SETCC) {
2637 SplitVecRes_SETCC(Mask.getNode(), MaskLo, MaskHi);
2638 } else {
2639 if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
2640 GetSplitVector(Mask, MaskLo, MaskHi);
2641 else
2642 std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, dl);
2643 }
2644
2645 // Split EVL operand
2646 SDValue EVLLo, EVLHi;
2647 std::tie(EVLLo, EVLHi) = DAG.SplitEVL(EVL, LD->getValueType(0), dl);
2648
2649 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
2650 LD->getPointerInfo(), MachineMemOperand::MOLoad,
2652 MMOMetadata(LD->getAAInfo(), LD->getRanges()));
2653
2654 Lo =
2655 DAG.getLoadVP(LD->getAddressingMode(), ExtType, LoVT, dl, Ch, Ptr, Offset,
2656 MaskLo, EVLLo, LoMemVT, MMO, LD->isExpandingLoad());
2657
2658 if (HiIsEmpty) {
2659 // The hi vp_load has zero storage size. We therefore simply set it to
2660 // the low vp_load and rely on subsequent removal from the chain.
2661 Hi = Lo;
2662 } else {
2663 // Generate hi vp_load.
2664 Ptr = TLI.IncrementMemoryAddress(Ptr, MaskLo, dl, LoMemVT, DAG,
2665 LD->isExpandingLoad());
2666
2667 MachinePointerInfo MPI;
2668 if (LoMemVT.isScalableVector())
2669 MPI = MachinePointerInfo(LD->getPointerInfo().getAddrSpace());
2670 else
2671 MPI = LD->getPointerInfo().getWithOffset(
2672 LoMemVT.getStoreSize().getFixedValue());
2673
2674 MMO = DAG.getMachineFunction().getMachineMemOperand(
2676 Alignment, MMOMetadata(LD->getAAInfo(), LD->getRanges()));
2677
2678 Hi = DAG.getLoadVP(LD->getAddressingMode(), ExtType, HiVT, dl, Ch, Ptr,
2679 Offset, MaskHi, EVLHi, HiMemVT, MMO,
2680 LD->isExpandingLoad());
2681 }
2682
2683 // Build a factor node to remember that this load is independent of the
2684 // other one.
2685 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
2686 Hi.getValue(1));
2687
2688 // Legalize the chain result - switch anything that used the old chain to
2689 // use the new one.
2690 ReplaceValueWith(SDValue(LD, 1), Ch);
2691}
2692
2693void DAGTypeLegalizer::SplitVecRes_VP_LOAD_FF(VPLoadFFSDNode *LD, SDValue &Lo,
2694 SDValue &Hi) {
2695 SDLoc dl(LD);
2696 auto [LoVT, HiVT] = DAG.GetSplitDestVTs(LD->getValueType(0));
2697
2698 SDValue Ch = LD->getChain();
2699 SDValue Ptr = LD->getBasePtr();
2700 Align Alignment = LD->getBaseAlign();
2701 SDValue Mask = LD->getMask();
2702 SDValue EVL = LD->getVectorLength();
2703
2704 // Split Mask operand
2705 SDValue MaskLo, MaskHi;
2706 if (Mask.getOpcode() == ISD::SETCC) {
2707 SplitVecRes_SETCC(Mask.getNode(), MaskLo, MaskHi);
2708 } else {
2709 if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
2710 GetSplitVector(Mask, MaskLo, MaskHi);
2711 else
2712 std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, dl);
2713 }
2714
2715 // Split EVL operand
2716 auto [EVLLo, EVLHi] = DAG.SplitEVL(EVL, LD->getValueType(0), dl);
2717
2718 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
2719 LD->getPointerInfo(), MachineMemOperand::MOLoad,
2721 MMOMetadata(LD->getAAInfo(), LD->getRanges()));
2722
2723 Lo = DAG.getLoadFFVP(LoVT, dl, Ch, Ptr, MaskLo, EVLLo, MMO);
2724
2725 // Fill the upper half with poison.
2726 Hi = DAG.getPOISON(HiVT);
2727
2728 ReplaceValueWith(SDValue(LD, 1), Lo.getValue(1));
2729 ReplaceValueWith(SDValue(LD, 2), Lo.getValue(2));
2730}
2731
2732void DAGTypeLegalizer::SplitVecRes_VP_STRIDED_LOAD(VPStridedLoadSDNode *SLD,
2733 SDValue &Lo, SDValue &Hi) {
2734 assert(SLD->isUnindexed() &&
2735 "Indexed VP strided load during type legalization!");
2736 assert(SLD->getOffset().isUndef() &&
2737 "Unexpected indexed variable-length load offset");
2738
2739 SDLoc DL(SLD);
2740
2741 EVT LoVT, HiVT;
2742 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(SLD->getValueType(0));
2743
2744 EVT LoMemVT, HiMemVT;
2745 bool HiIsEmpty = false;
2746 std::tie(LoMemVT, HiMemVT) =
2747 DAG.GetDependentSplitDestVTs(SLD->getMemoryVT(), LoVT, &HiIsEmpty);
2748
2749 SDValue Mask = SLD->getMask();
2750 SDValue LoMask, HiMask;
2751 if (Mask.getOpcode() == ISD::SETCC) {
2752 SplitVecRes_SETCC(Mask.getNode(), LoMask, HiMask);
2753 } else {
2754 if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
2755 GetSplitVector(Mask, LoMask, HiMask);
2756 else
2757 std::tie(LoMask, HiMask) = DAG.SplitVector(Mask, DL);
2758 }
2759
2760 SDValue LoEVL, HiEVL;
2761 std::tie(LoEVL, HiEVL) =
2762 DAG.SplitEVL(SLD->getVectorLength(), SLD->getValueType(0), DL);
2763
2764 // Generate the low vp_strided_load
2765 Lo = DAG.getStridedLoadVP(
2766 SLD->getAddressingMode(), SLD->getExtensionType(), LoVT, DL,
2767 SLD->getChain(), SLD->getBasePtr(), SLD->getOffset(), SLD->getStride(),
2768 LoMask, LoEVL, LoMemVT, SLD->getMemOperand(), SLD->isExpandingLoad());
2769
2770 if (HiIsEmpty) {
2771 // The high vp_strided_load has zero storage size. We therefore simply set
2772 // it to the low vp_strided_load and rely on subsequent removal from the
2773 // chain.
2774 Hi = Lo;
2775 } else {
2776 // Generate the high vp_strided_load.
2777 // To calculate the high base address, we need to sum to the low base
2778 // address stride number of bytes for each element already loaded by low,
2779 // that is: Ptr = Ptr + (LoEVL * Stride)
2780 EVT PtrVT = SLD->getBasePtr().getValueType();
2782 DAG.getNode(ISD::MUL, DL, PtrVT, LoEVL,
2783 DAG.getSExtOrTrunc(SLD->getStride(), DL, PtrVT));
2784 SDValue Ptr =
2785 DAG.getNode(ISD::ADD, DL, PtrVT, SLD->getBasePtr(), Increment);
2786
2787 Align Alignment = SLD->getBaseAlign();
2788 if (LoMemVT.isScalableVector())
2790 Alignment, LoMemVT.getSizeInBits().getKnownMinValue() / 8);
2791
2792 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
2793 MachinePointerInfo(SLD->getPointerInfo().getAddrSpace()),
2795 Alignment, MMOMetadata(SLD->getAAInfo(), SLD->getRanges()));
2796
2797 Hi = DAG.getStridedLoadVP(SLD->getAddressingMode(), SLD->getExtensionType(),
2798 HiVT, DL, SLD->getChain(), Ptr, SLD->getOffset(),
2799 SLD->getStride(), HiMask, HiEVL, HiMemVT, MMO,
2800 SLD->isExpandingLoad());
2801 }
2802
2803 // Build a factor node to remember that this load is independent of the
2804 // other one.
2805 SDValue Ch = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo.getValue(1),
2806 Hi.getValue(1));
2807
2808 // Legalize the chain result - switch anything that used the old chain to
2809 // use the new one.
2810 ReplaceValueWith(SDValue(SLD, 1), Ch);
2811}
2812
2813void DAGTypeLegalizer::SplitVecRes_MLOAD(MaskedLoadSDNode *MLD,
2814 SDValue &Lo, SDValue &Hi) {
2815 assert(MLD->isUnindexed() && "Indexed masked load during type legalization!");
2816 EVT LoVT, HiVT;
2817 SDLoc dl(MLD);
2818 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(MLD->getValueType(0));
2819
2820 SDValue Ch = MLD->getChain();
2821 SDValue Ptr = MLD->getBasePtr();
2822 SDValue Offset = MLD->getOffset();
2823 assert(Offset.isUndef() && "Unexpected indexed masked load offset");
2824 SDValue Mask = MLD->getMask();
2825 SDValue PassThru = MLD->getPassThru();
2826 Align Alignment = MLD->getBaseAlign();
2827 ISD::LoadExtType ExtType = MLD->getExtensionType();
2828 MachineMemOperand::Flags MMOFlags = MLD->getMemOperand()->getFlags();
2829
2830 // Split Mask operand
2831 SDValue MaskLo, MaskHi;
2832 if (Mask.getOpcode() == ISD::SETCC) {
2833 SplitVecRes_SETCC(Mask.getNode(), MaskLo, MaskHi);
2834 } else {
2835 if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
2836 GetSplitVector(Mask, MaskLo, MaskHi);
2837 else
2838 std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, dl);
2839 }
2840
2841 EVT MemoryVT = MLD->getMemoryVT();
2842 EVT LoMemVT, HiMemVT;
2843 bool HiIsEmpty = false;
2844 std::tie(LoMemVT, HiMemVT) =
2845 DAG.GetDependentSplitDestVTs(MemoryVT, LoVT, &HiIsEmpty);
2846
2847 SDValue PassThruLo, PassThruHi;
2848 if (getTypeAction(PassThru.getValueType()) == TargetLowering::TypeSplitVector)
2849 GetSplitVector(PassThru, PassThruLo, PassThruHi);
2850 else
2851 std::tie(PassThruLo, PassThruHi) = DAG.SplitVector(PassThru, dl);
2852
2853 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
2855 Alignment,
2856 MMOMetadata(MLD->getAAInfo(), MLD->getRanges(), MLD->getMemCacheHint()));
2857
2858 Lo = DAG.getMaskedLoad(LoVT, dl, Ch, Ptr, Offset, MaskLo, PassThruLo, LoMemVT,
2859 MMO, MLD->getAddressingMode(), ExtType,
2860 MLD->isExpandingLoad());
2861
2862 if (HiIsEmpty) {
2863 // The hi masked load has zero storage size. We therefore simply set it to
2864 // the low masked load and rely on subsequent removal from the chain.
2865 Hi = Lo;
2866 } else {
2867 // Generate hi masked load.
2868 Ptr = TLI.IncrementMemoryAddress(Ptr, MaskLo, dl, LoMemVT, DAG,
2869 MLD->isExpandingLoad());
2870
2871 MachinePointerInfo MPI;
2872 if (LoMemVT.isScalableVector())
2873 MPI = MachinePointerInfo(MLD->getPointerInfo().getAddrSpace());
2874 else
2875 MPI = MLD->getPointerInfo().getWithOffset(
2876 LoMemVT.getStoreSize().getFixedValue());
2877
2878 MMO = DAG.getMachineFunction().getMachineMemOperand(
2879 MPI, MMOFlags, LocationSize::beforeOrAfterPointer(), Alignment,
2880 MMOMetadata(MLD->getAAInfo(), MLD->getRanges(),
2881 MLD->getMemCacheHint()));
2882
2883 Hi = DAG.getMaskedLoad(HiVT, dl, Ch, Ptr, Offset, MaskHi, PassThruHi,
2884 HiMemVT, MMO, MLD->getAddressingMode(), ExtType,
2885 MLD->isExpandingLoad());
2886 }
2887
2888 // Build a factor node to remember that this load is independent of the
2889 // other one.
2890 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
2891 Hi.getValue(1));
2892
2893 // Legalize the chain result - switch anything that used the old chain to
2894 // use the new one.
2895 ReplaceValueWith(SDValue(MLD, 1), Ch);
2896
2897}
2898
2899void DAGTypeLegalizer::SplitVecRes_Gather(MemSDNode *N, SDValue &Lo,
2900 SDValue &Hi, bool SplitSETCC) {
2901 EVT LoVT, HiVT;
2902 SDLoc dl(N);
2903 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
2904
2905 SDValue Ch = N->getChain();
2906 SDValue Ptr = N->getBasePtr();
2907 struct Operands {
2908 SDValue Mask;
2909 SDValue Index;
2910 SDValue Scale;
2911 } Ops = [&]() -> Operands {
2912 if (auto *MSC = dyn_cast<MaskedGatherSDNode>(N)) {
2913 return {MSC->getMask(), MSC->getIndex(), MSC->getScale()};
2914 }
2915 auto *VPSC = cast<VPGatherSDNode>(N);
2916 return {VPSC->getMask(), VPSC->getIndex(), VPSC->getScale()};
2917 }();
2918
2919 EVT MemoryVT = N->getMemoryVT();
2920 Align Alignment = N->getBaseAlign();
2921
2922 // Split Mask operand
2923 SDValue MaskLo, MaskHi;
2924 if (SplitSETCC && Ops.Mask.getOpcode() == ISD::SETCC) {
2925 SplitVecRes_SETCC(Ops.Mask.getNode(), MaskLo, MaskHi);
2926 } else {
2927 std::tie(MaskLo, MaskHi) = SplitMask(Ops.Mask, dl);
2928 }
2929
2930 EVT LoMemVT, HiMemVT;
2931 // Split MemoryVT
2932 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
2933
2934 SDValue IndexHi, IndexLo;
2935 if (getTypeAction(Ops.Index.getValueType()) ==
2937 GetSplitVector(Ops.Index, IndexLo, IndexHi);
2938 else
2939 std::tie(IndexLo, IndexHi) = DAG.SplitVector(Ops.Index, dl);
2940
2941 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
2942 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
2943 N->getPointerInfo(), MMOFlags, LocationSize::beforeOrAfterPointer(),
2944 Alignment, MMOMetadata(N->getAAInfo(), N->getRanges()));
2945
2946 if (auto *MGT = dyn_cast<MaskedGatherSDNode>(N)) {
2947 SDValue PassThru = MGT->getPassThru();
2948 SDValue PassThruLo, PassThruHi;
2949 if (getTypeAction(PassThru.getValueType()) ==
2951 GetSplitVector(PassThru, PassThruLo, PassThruHi);
2952 else
2953 std::tie(PassThruLo, PassThruHi) = DAG.SplitVector(PassThru, dl);
2954
2955 ISD::LoadExtType ExtType = MGT->getExtensionType();
2956 ISD::MemIndexType IndexTy = MGT->getIndexType();
2957
2958 SDValue OpsLo[] = {Ch, PassThruLo, MaskLo, Ptr, IndexLo, Ops.Scale};
2959 Lo = DAG.getMaskedGather(DAG.getVTList(LoVT, MVT::Other), LoMemVT, dl,
2960 OpsLo, MMO, IndexTy, ExtType);
2961
2962 SDValue OpsHi[] = {Ch, PassThruHi, MaskHi, Ptr, IndexHi, Ops.Scale};
2963 Hi = DAG.getMaskedGather(DAG.getVTList(HiVT, MVT::Other), HiMemVT, dl,
2964 OpsHi, MMO, IndexTy, ExtType);
2965 } else {
2966 auto *VPGT = cast<VPGatherSDNode>(N);
2967 SDValue EVLLo, EVLHi;
2968 std::tie(EVLLo, EVLHi) =
2969 DAG.SplitEVL(VPGT->getVectorLength(), MemoryVT, dl);
2970
2971 SDValue OpsLo[] = {Ch, Ptr, IndexLo, Ops.Scale, MaskLo, EVLLo};
2972 Lo = DAG.getGatherVP(DAG.getVTList(LoVT, MVT::Other), LoMemVT, dl, OpsLo,
2973 MMO, VPGT->getIndexType());
2974
2975 SDValue OpsHi[] = {Ch, Ptr, IndexHi, Ops.Scale, MaskHi, EVLHi};
2976 Hi = DAG.getGatherVP(DAG.getVTList(HiVT, MVT::Other), HiMemVT, dl, OpsHi,
2977 MMO, VPGT->getIndexType());
2978 }
2979
2980 // Build a factor node to remember that this load is independent of the
2981 // other one.
2982 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
2983 Hi.getValue(1));
2984
2985 // Legalize the chain result - switch anything that used the old chain to
2986 // use the new one.
2987 ReplaceValueWith(SDValue(N, 1), Ch);
2988}
2989
2990void DAGTypeLegalizer::SplitVecRes_VECTOR_COMPRESS(SDNode *N, SDValue &Lo,
2991 SDValue &Hi) {
2992 // This is not "trivial", as there is a dependency between the two subvectors.
2993 // Depending on the number of 1s in the mask, the elements from the Hi vector
2994 // need to be moved to the Lo vector. Passthru values make this even harder.
2995 // We try to use VECTOR_COMPRESS if the target has custom lowering with
2996 // smaller types and passthru is undef, as it is most likely faster than the
2997 // fully expand path. Otherwise, just do the full expansion as one "big"
2998 // operation and then extract the Lo and Hi vectors from that. This gets
2999 // rid of VECTOR_COMPRESS and all other operands can be legalized later.
3000 SDLoc DL(N);
3001 EVT VecVT = N->getValueType(0);
3002
3003 auto [LoVT, HiVT] = DAG.GetSplitDestVTs(VecVT);
3004 bool HasCustomLowering = false;
3005 EVT CheckVT = LoVT;
3006 while (CheckVT.getVectorMinNumElements() > 1) {
3007 // TLI.isOperationLegalOrCustom requires a legal type, but we could have a
3008 // custom lowering for illegal types. So we do the checks separately.
3009 if (TLI.isOperationLegal(ISD::VECTOR_COMPRESS, CheckVT) ||
3010 TLI.isOperationCustom(ISD::VECTOR_COMPRESS, CheckVT)) {
3011 HasCustomLowering = true;
3012 break;
3013 }
3014 CheckVT = CheckVT.getHalfNumVectorElementsVT(*DAG.getContext());
3015 }
3016
3017 SDValue Passthru = N->getOperand(2);
3018 if (!HasCustomLowering) {
3019 SDValue Compressed = TLI.expandVECTOR_COMPRESS(N, DAG);
3020 std::tie(Lo, Hi) = DAG.SplitVector(Compressed, DL, LoVT, HiVT);
3021 return;
3022 }
3023
3024 // Try to VECTOR_COMPRESS smaller vectors and combine via a stack store+load.
3025 SDValue Mask = N->getOperand(1);
3026 SDValue LoMask, HiMask;
3027 std::tie(Lo, Hi) = DAG.SplitVectorOperand(N, 0);
3028 std::tie(LoMask, HiMask) = SplitMask(Mask);
3029
3030 SDValue UndefPassthru = DAG.getPOISON(LoVT);
3031 Lo = DAG.getNode(ISD::VECTOR_COMPRESS, DL, LoVT, Lo, LoMask, UndefPassthru);
3032 Hi = DAG.getNode(ISD::VECTOR_COMPRESS, DL, HiVT, Hi, HiMask, UndefPassthru);
3033
3034 SDValue StackPtr = DAG.CreateStackTemporary(
3035 VecVT.getStoreSize(), DAG.getReducedAlign(VecVT, /*UseABI=*/false));
3036 MachineFunction &MF = DAG.getMachineFunction();
3037 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(
3038 MF, cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex());
3039
3040 EVT LoMaskVT = LoMask.getValueType();
3041 assert(LoMaskVT.getScalarType() == MVT::i1 && "Expected vector of i1s");
3042
3043 // We store LoVec and then insert HiVec starting at offset=|1s| in LoMask.
3044 EVT WideLoMaskVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
3045 LoMaskVT.getVectorElementCount());
3046 SDValue WideLoMask = DAG.getNode(ISD::ZERO_EXTEND, DL, WideLoMaskVT, LoMask);
3047 SDValue Offset = DAG.getNode(ISD::VECREDUCE_ADD, DL, MVT::i32, WideLoMask);
3048 Offset = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Offset);
3049
3050 SDValue Chain = DAG.getEntryNode();
3051 Chain = DAG.getStore(Chain, DL, Lo, StackPtr, PtrInfo);
3052 Chain = DAG.getStore(Chain, DL, Hi, Offset,
3054
3055 SDValue Compressed = DAG.getLoad(VecVT, DL, Chain, StackPtr, PtrInfo);
3056 if (!Passthru.isUndef()) {
3057 // Compress the input mask so only inactive lanes of the result are replaced
3058 // by their passthrough value.
3059 EVT MaskVT = Mask.getValueType();
3060 EVT WideMaskVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
3061 MaskVT.getVectorElementCount());
3062 SDValue WideMask = DAG.getNode(ISD::ZERO_EXTEND, DL, WideMaskVT, Mask);
3063 SDValue NumActiveElts =
3064 DAG.getNode(ISD::VECREDUCE_ADD, DL, MVT::i32, WideMask);
3065
3066 SDValue StepVector = DAG.getStepVector(DL, WideMaskVT);
3067 SDValue SplatNumActiveElts = DAG.getSplat(WideMaskVT, DL, NumActiveElts);
3068 SDValue CompressedMask =
3069 DAG.getSetCC(DL, MaskVT, StepVector, SplatNumActiveElts, ISD::SETULT);
3070
3071 Compressed = DAG.getNode(ISD::VSELECT, DL, VecVT, CompressedMask,
3072 Compressed, Passthru);
3073 }
3074 std::tie(Lo, Hi) = DAG.SplitVector(Compressed, DL);
3075}
3076
3077void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE_VAR(SDNode *N, SDValue &Lo,
3078 SDValue &Hi) {
3079 // TODO: Split into half-width shuffles when the target lowers them natively.
3080 SDValue Res = TLI.expandVECTOR_SHUFFLE_VAR(N, DAG);
3081 std::tie(Lo, Hi) = DAG.SplitVector(Res, SDLoc(N));
3082}
3083
3084void DAGTypeLegalizer::SplitVecRes_SETCC(SDNode *N, SDValue &Lo, SDValue &Hi) {
3085 assert(N->getValueType(0).isVector() &&
3086 N->getOperand(0).getValueType().isVector() &&
3087 "Operand types must be vectors");
3088
3089 EVT LoVT, HiVT;
3090 SDLoc DL(N);
3091 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
3092
3093 // If the input also splits, handle it directly. Otherwise split it by hand.
3094 SDValue LL, LH, RL, RH;
3095 if (getTypeAction(N->getOperand(0).getValueType()) ==
3097 GetSplitVector(N->getOperand(0), LL, LH);
3098 else
3099 std::tie(LL, LH) = DAG.SplitVectorOperand(N, 0);
3100
3101 if (getTypeAction(N->getOperand(1).getValueType()) ==
3103 GetSplitVector(N->getOperand(1), RL, RH);
3104 else
3105 std::tie(RL, RH) = DAG.SplitVectorOperand(N, 1);
3106
3107 Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2));
3108 Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2));
3109}
3110
3111void DAGTypeLegalizer::SplitVecRes_UnaryOp(SDNode *N, SDValue &Lo,
3112 SDValue &Hi) {
3113 // Get the dest types - they may not match the input types, e.g. int_to_fp.
3114 EVT LoVT, HiVT;
3115 SDLoc dl(N);
3116 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
3117
3118 // If the input also splits, handle it directly for a compile time speedup.
3119 // Otherwise split it by hand.
3120 EVT InVT = N->getOperand(0).getValueType();
3121 if (getTypeAction(InVT) == TargetLowering::TypeSplitVector)
3122 GetSplitVector(N->getOperand(0), Lo, Hi);
3123 else
3124 std::tie(Lo, Hi) = DAG.SplitVectorOperand(N, 0);
3125
3126 const SDNodeFlags Flags = N->getFlags();
3127 unsigned Opcode = N->getOpcode();
3128 if (Opcode == ISD::CONVERT_TO_ARBITRARY_FP) {
3129 Lo = DAG.getNode(Opcode, dl, LoVT, Lo, N->getOperand(1), N->getOperand(2),
3130 N->getOperand(3), Flags);
3131 Hi = DAG.getNode(Opcode, dl, HiVT, Hi, N->getOperand(1), N->getOperand(2),
3132 N->getOperand(3), Flags);
3133 return;
3134 }
3135
3136 if (Opcode == ISD::FP_ROUND || Opcode == ISD::AssertNoFPClass ||
3138 Lo = DAG.getNode(Opcode, dl, LoVT, Lo, N->getOperand(1), Flags);
3139 Hi = DAG.getNode(Opcode, dl, HiVT, Hi, N->getOperand(1), Flags);
3140 } else {
3141 Lo = DAG.getNode(Opcode, dl, LoVT, Lo, Flags);
3142 Hi = DAG.getNode(Opcode, dl, HiVT, Hi, Flags);
3143 }
3144}
3145
3146void DAGTypeLegalizer::SplitVecRes_ADDRSPACECAST(SDNode *N, SDValue &Lo,
3147 SDValue &Hi) {
3148 SDLoc dl(N);
3149 auto [LoVT, HiVT] = DAG.GetSplitDestVTs(N->getValueType(0));
3150
3151 // If the input also splits, handle it directly for a compile time speedup.
3152 // Otherwise split it by hand.
3153 EVT InVT = N->getOperand(0).getValueType();
3154 if (getTypeAction(InVT) == TargetLowering::TypeSplitVector)
3155 GetSplitVector(N->getOperand(0), Lo, Hi);
3156 else
3157 std::tie(Lo, Hi) = DAG.SplitVectorOperand(N, 0);
3158
3159 auto *AddrSpaceCastN = cast<AddrSpaceCastSDNode>(N);
3160 unsigned SrcAS = AddrSpaceCastN->getSrcAddressSpace();
3161 unsigned DestAS = AddrSpaceCastN->getDestAddressSpace();
3162 SDNodeFlags Flags = AddrSpaceCastN->getFlags();
3163 Lo = DAG.getAddrSpaceCast(dl, LoVT, Lo, SrcAS, DestAS, Flags);
3164 Hi = DAG.getAddrSpaceCast(dl, HiVT, Hi, SrcAS, DestAS, Flags);
3165}
3166
3167void DAGTypeLegalizer::SplitVecRes_UnaryOpWithTwoResults(SDNode *N,
3168 unsigned ResNo,
3169 SDValue &Lo,
3170 SDValue &Hi) {
3171 SDLoc dl(N);
3172 auto [LoVT, HiVT] = DAG.GetSplitDestVTs(N->getValueType(0));
3173 auto [LoVT1, HiVT1] = DAG.GetSplitDestVTs(N->getValueType(1));
3174
3175 // If the input also splits, handle it directly for a compile time speedup.
3176 // Otherwise split it by hand.
3177 EVT InVT = N->getOperand(0).getValueType();
3178 if (getTypeAction(InVT) == TargetLowering::TypeSplitVector)
3179 GetSplitVector(N->getOperand(0), Lo, Hi);
3180 else
3181 std::tie(Lo, Hi) = DAG.SplitVectorOperand(N, 0);
3182
3183 Lo = DAG.getNode(N->getOpcode(), dl, {LoVT, LoVT1}, Lo, N->getFlags());
3184 Hi = DAG.getNode(N->getOpcode(), dl, {HiVT, HiVT1}, Hi, N->getFlags());
3185
3186 SDNode *HiNode = Hi.getNode();
3187 SDNode *LoNode = Lo.getNode();
3188
3189 // Replace the other vector result not being explicitly split here.
3190 unsigned OtherNo = 1 - ResNo;
3191 EVT OtherVT = N->getValueType(OtherNo);
3192 if (getTypeAction(OtherVT) == TargetLowering::TypeSplitVector) {
3193 SetSplitVector(SDValue(N, OtherNo), SDValue(LoNode, OtherNo),
3194 SDValue(HiNode, OtherNo));
3195 } else {
3196 SDValue OtherVal =
3197 DAG.getNode(ISD::CONCAT_VECTORS, dl, OtherVT, SDValue(LoNode, OtherNo),
3198 SDValue(HiNode, OtherNo));
3199 ReplaceValueWith(SDValue(N, OtherNo), OtherVal);
3200 }
3201}
3202
3203void DAGTypeLegalizer::SplitVecRes_ExtendOp(SDNode *N, SDValue &Lo,
3204 SDValue &Hi) {
3205 SDLoc dl(N);
3206 EVT SrcVT = N->getOperand(0).getValueType();
3207 EVT DestVT = N->getValueType(0);
3208 EVT LoVT, HiVT;
3209 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(DestVT);
3210
3211 // We can do better than a generic split operation if the extend is doing
3212 // more than just doubling the width of the elements and the following are
3213 // true:
3214 // - The number of vector elements is even,
3215 // - the source type is legal,
3216 // - the type of a split source is illegal,
3217 // - the type of an extended (by doubling element size) source is legal, and
3218 // - the type of that extended source when split is legal.
3219 //
3220 // This won't necessarily completely legalize the operation, but it will
3221 // more effectively move in the right direction and prevent falling down
3222 // to scalarization in many cases due to the input vector being split too
3223 // far.
3224 if (SrcVT.getVectorElementCount().isKnownEven() &&
3225 SrcVT.getScalarSizeInBits() * 2 < DestVT.getScalarSizeInBits()) {
3226 LLVMContext &Ctx = *DAG.getContext();
3227 EVT NewSrcVT = SrcVT.widenIntegerVectorElementType(Ctx);
3228 EVT SplitSrcVT = SrcVT.getHalfNumVectorElementsVT(Ctx);
3229
3230 EVT SplitLoVT, SplitHiVT;
3231 std::tie(SplitLoVT, SplitHiVT) = DAG.GetSplitDestVTs(NewSrcVT);
3232 if (TLI.isTypeLegal(SrcVT) && !TLI.isTypeLegal(SplitSrcVT) &&
3233 TLI.isTypeLegal(NewSrcVT) && TLI.isTypeLegal(SplitLoVT)) {
3234 LLVM_DEBUG(dbgs() << "Split vector extend via incremental extend:";
3235 N->dump(&DAG); dbgs() << "\n");
3236 // Extend the source vector by one step.
3237 SDValue NewSrc =
3238 DAG.getNode(N->getOpcode(), dl, NewSrcVT, N->getOperand(0));
3239 // Get the low and high halves of the new, extended one step, vector.
3240 std::tie(Lo, Hi) = DAG.SplitVector(NewSrc, dl);
3241 // Extend those vector halves the rest of the way.
3242 Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo);
3243 Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi);
3244 return;
3245 }
3246 }
3247 // Fall back to the generic unary operator splitting otherwise.
3248 SplitVecRes_UnaryOp(N, Lo, Hi);
3249}
3250
3251void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
3252 SDValue &Lo, SDValue &Hi) {
3253 // The low and high parts of the original input give four input vectors.
3254 SDValue Inputs[4];
3255 SDLoc DL(N);
3256 GetSplitVector(N->getOperand(0), Inputs[0], Inputs[1]);
3257 GetSplitVector(N->getOperand(1), Inputs[2], Inputs[3]);
3258 EVT NewVT = Inputs[0].getValueType();
3259 unsigned NewElts = NewVT.getVectorNumElements();
3260
3261 auto &&IsConstant = [](const SDValue &N) {
3262 APInt SplatValue;
3263 return N.getResNo() == 0 && (ISD::isConstantSplatVector(N, SplatValue) ||
3265 };
3266 auto &&BuildVector = [NewElts, &DAG = DAG, NewVT, &DL](SDValue &Input1,
3267 SDValue &Input2,
3268 ArrayRef<int> Mask) {
3269 assert(Input1->getOpcode() == ISD::BUILD_VECTOR &&
3270 Input2->getOpcode() == ISD::BUILD_VECTOR &&
3271 "Expected build vector node.");
3272 EVT EltVT = NewVT.getVectorElementType();
3273 SmallVector<SDValue> Ops(NewElts, DAG.getPOISON(EltVT));
3274 for (unsigned I = 0; I < NewElts; ++I) {
3275 if (Mask[I] == PoisonMaskElem)
3276 continue;
3277 unsigned Idx = Mask[I];
3278 if (Idx >= NewElts)
3279 Ops[I] = Input2.getOperand(Idx - NewElts);
3280 else
3281 Ops[I] = Input1.getOperand(Idx);
3282 // Make the type of all elements the same as the element type.
3283 if (Ops[I].getValueType().bitsGT(EltVT))
3284 Ops[I] = DAG.getNode(ISD::TRUNCATE, DL, EltVT, Ops[I]);
3285 }
3286 return DAG.getBuildVector(NewVT, DL, Ops);
3287 };
3288
3289 // If Lo or Hi uses elements from at most two of the four input vectors, then
3290 // express it as a vector shuffle of those two inputs. Otherwise extract the
3291 // input elements by hand and construct the Lo/Hi output using a BUILD_VECTOR.
3292 SmallVector<int> OrigMask(N->getMask());
3293 // Try to pack incoming shuffles/inputs.
3294 auto &&TryPeekThroughShufflesInputs = [&Inputs, &NewVT, this, NewElts,
3295 &DL](SmallVectorImpl<int> &Mask) {
3296 // Check if all inputs are shuffles of the same operands or non-shuffles.
3297 MapVector<std::pair<SDValue, SDValue>, SmallVector<unsigned>> ShufflesIdxs;
3298 for (unsigned Idx = 0; Idx < std::size(Inputs); ++Idx) {
3299 SDValue Input = Inputs[Idx];
3300 auto *Shuffle = dyn_cast<ShuffleVectorSDNode>(Input.getNode());
3301 if (!Shuffle ||
3302 Input.getOperand(0).getValueType() != Input.getValueType())
3303 continue;
3304 ShufflesIdxs[std::make_pair(Input.getOperand(0), Input.getOperand(1))]
3305 .push_back(Idx);
3306 ShufflesIdxs[std::make_pair(Input.getOperand(1), Input.getOperand(0))]
3307 .push_back(Idx);
3308 }
3309 for (auto &P : ShufflesIdxs) {
3310 if (P.second.size() < 2)
3311 continue;
3312 // Use shuffles operands instead of shuffles themselves.
3313 // 1. Adjust mask.
3314 for (int &Idx : Mask) {
3315 if (Idx == PoisonMaskElem)
3316 continue;
3317 unsigned SrcRegIdx = Idx / NewElts;
3318 if (Inputs[SrcRegIdx].isUndef()) {
3320 continue;
3321 }
3322 auto *Shuffle =
3323 dyn_cast<ShuffleVectorSDNode>(Inputs[SrcRegIdx].getNode());
3324 if (!Shuffle || !is_contained(P.second, SrcRegIdx))
3325 continue;
3326 int MaskElt = Shuffle->getMaskElt(Idx % NewElts);
3327 if (MaskElt == PoisonMaskElem) {
3329 continue;
3330 }
3331 Idx = MaskElt % NewElts +
3332 P.second[Shuffle->getOperand(MaskElt / NewElts) == P.first.first
3333 ? 0
3334 : 1] *
3335 NewElts;
3336 }
3337 // 2. Update inputs.
3338 Inputs[P.second[0]] = P.first.first;
3339 Inputs[P.second[1]] = P.first.second;
3340 // Clear the pair data.
3341 P.second.clear();
3342 ShufflesIdxs[std::make_pair(P.first.second, P.first.first)].clear();
3343 }
3344 // Check if any concat_vectors can be simplified.
3345 SmallBitVector UsedSubVector(2 * std::size(Inputs));
3346 for (int &Idx : Mask) {
3347 if (Idx == PoisonMaskElem)
3348 continue;
3349 unsigned SrcRegIdx = Idx / NewElts;
3350 if (Inputs[SrcRegIdx].isUndef()) {
3352 continue;
3353 }
3355 getTypeAction(Inputs[SrcRegIdx].getValueType());
3356 if (Inputs[SrcRegIdx].getOpcode() == ISD::CONCAT_VECTORS &&
3357 Inputs[SrcRegIdx].getNumOperands() == 2 &&
3358 !Inputs[SrcRegIdx].getOperand(1).isUndef() &&
3359 (TypeAction == TargetLowering::TypeLegal ||
3360 TypeAction == TargetLowering::TypeWidenVector))
3361 UsedSubVector.set(2 * SrcRegIdx + (Idx % NewElts) / (NewElts / 2));
3362 }
3363 if (UsedSubVector.count() > 1) {
3365 for (unsigned I = 0; I < std::size(Inputs); ++I) {
3366 if (UsedSubVector.test(2 * I) == UsedSubVector.test(2 * I + 1))
3367 continue;
3368 if (Pairs.empty() || Pairs.back().size() == 2)
3369 Pairs.emplace_back();
3370 if (UsedSubVector.test(2 * I)) {
3371 Pairs.back().emplace_back(I, 0);
3372 } else {
3373 assert(UsedSubVector.test(2 * I + 1) &&
3374 "Expected to be used one of the subvectors.");
3375 Pairs.back().emplace_back(I, 1);
3376 }
3377 }
3378 if (!Pairs.empty() && Pairs.front().size() > 1) {
3379 // Adjust mask.
3380 for (int &Idx : Mask) {
3381 if (Idx == PoisonMaskElem)
3382 continue;
3383 unsigned SrcRegIdx = Idx / NewElts;
3384 auto *It = find_if(
3385 Pairs, [SrcRegIdx](ArrayRef<std::pair<unsigned, int>> Idxs) {
3386 return Idxs.front().first == SrcRegIdx ||
3387 Idxs.back().first == SrcRegIdx;
3388 });
3389 if (It == Pairs.end())
3390 continue;
3391 Idx = It->front().first * NewElts + (Idx % NewElts) % (NewElts / 2) +
3392 (SrcRegIdx == It->front().first ? 0 : (NewElts / 2));
3393 }
3394 // Adjust inputs.
3395 for (ArrayRef<std::pair<unsigned, int>> Idxs : Pairs) {
3396 Inputs[Idxs.front().first] = DAG.getNode(
3398 Inputs[Idxs.front().first].getValueType(),
3399 Inputs[Idxs.front().first].getOperand(Idxs.front().second),
3400 Inputs[Idxs.back().first].getOperand(Idxs.back().second));
3401 }
3402 }
3403 }
3404 bool Changed;
3405 do {
3406 // Try to remove extra shuffles (except broadcasts) and shuffles with the
3407 // reused operands.
3408 Changed = false;
3409 for (unsigned I = 0; I < std::size(Inputs); ++I) {
3410 auto *Shuffle = dyn_cast<ShuffleVectorSDNode>(Inputs[I].getNode());
3411 if (!Shuffle)
3412 continue;
3413 if (Shuffle->getOperand(0).getValueType() != NewVT)
3414 continue;
3415 int Op = -1;
3416 if (!Inputs[I].hasOneUse() && Shuffle->getOperand(1).isUndef() &&
3417 !Shuffle->isSplat()) {
3418 Op = 0;
3419 } else if (!Inputs[I].hasOneUse() &&
3420 !Shuffle->getOperand(1).isUndef()) {
3421 // Find the only used operand, if possible.
3422 for (int &Idx : Mask) {
3423 if (Idx == PoisonMaskElem)
3424 continue;
3425 unsigned SrcRegIdx = Idx / NewElts;
3426 if (SrcRegIdx != I)
3427 continue;
3428 int MaskElt = Shuffle->getMaskElt(Idx % NewElts);
3429 if (MaskElt == PoisonMaskElem) {
3431 continue;
3432 }
3433 int OpIdx = MaskElt / NewElts;
3434 if (Op == -1) {
3435 Op = OpIdx;
3436 continue;
3437 }
3438 if (Op != OpIdx) {
3439 Op = -1;
3440 break;
3441 }
3442 }
3443 }
3444 if (Op < 0) {
3445 // Try to check if one of the shuffle operands is used already.
3446 for (int OpIdx = 0; OpIdx < 2; ++OpIdx) {
3447 if (Shuffle->getOperand(OpIdx).isUndef())
3448 continue;
3449 auto *It = find(Inputs, Shuffle->getOperand(OpIdx));
3450 if (It == std::end(Inputs))
3451 continue;
3452 int FoundOp = std::distance(std::begin(Inputs), It);
3453 // Found that operand is used already.
3454 // 1. Fix the mask for the reused operand.
3455 for (int &Idx : Mask) {
3456 if (Idx == PoisonMaskElem)
3457 continue;
3458 unsigned SrcRegIdx = Idx / NewElts;
3459 if (SrcRegIdx != I)
3460 continue;
3461 int MaskElt = Shuffle->getMaskElt(Idx % NewElts);
3462 if (MaskElt == PoisonMaskElem) {
3464 continue;
3465 }
3466 int MaskIdx = MaskElt / NewElts;
3467 if (OpIdx == MaskIdx)
3468 Idx = MaskElt % NewElts + FoundOp * NewElts;
3469 }
3470 // 2. Set Op to the unused OpIdx.
3471 Op = (OpIdx + 1) % 2;
3472 break;
3473 }
3474 }
3475 if (Op >= 0) {
3476 Changed = true;
3477 Inputs[I] = Shuffle->getOperand(Op);
3478 // Adjust mask.
3479 for (int &Idx : Mask) {
3480 if (Idx == PoisonMaskElem)
3481 continue;
3482 unsigned SrcRegIdx = Idx / NewElts;
3483 if (SrcRegIdx != I)
3484 continue;
3485 int MaskElt = Shuffle->getMaskElt(Idx % NewElts);
3486 int OpIdx = MaskElt / NewElts;
3487 if (OpIdx != Op)
3488 continue;
3489 Idx = MaskElt % NewElts + SrcRegIdx * NewElts;
3490 }
3491 }
3492 }
3493 } while (Changed);
3494 };
3495 TryPeekThroughShufflesInputs(OrigMask);
3496 // Proces unique inputs.
3497 auto &&MakeUniqueInputs = [&Inputs, &IsConstant,
3498 NewElts](SmallVectorImpl<int> &Mask) {
3499 SetVector<SDValue> UniqueInputs;
3500 SetVector<SDValue> UniqueConstantInputs;
3501 for (const auto &I : Inputs) {
3502 if (IsConstant(I))
3503 UniqueConstantInputs.insert(I);
3504 else if (!I.isUndef())
3505 UniqueInputs.insert(I);
3506 }
3507 // Adjust mask in case of reused inputs. Also, need to insert constant
3508 // inputs at first, otherwise it affects the final outcome.
3509 if (UniqueInputs.size() != std::size(Inputs)) {
3510 auto &&UniqueVec = UniqueInputs.takeVector();
3511 auto &&UniqueConstantVec = UniqueConstantInputs.takeVector();
3512 unsigned ConstNum = UniqueConstantVec.size();
3513 for (int &Idx : Mask) {
3514 if (Idx == PoisonMaskElem)
3515 continue;
3516 unsigned SrcRegIdx = Idx / NewElts;
3517 if (Inputs[SrcRegIdx].isUndef()) {
3519 continue;
3520 }
3521 const auto It = find(UniqueConstantVec, Inputs[SrcRegIdx]);
3522 if (It != UniqueConstantVec.end()) {
3523 Idx = (Idx % NewElts) +
3524 NewElts * std::distance(UniqueConstantVec.begin(), It);
3525 assert(Idx >= 0 && "Expected defined mask idx.");
3526 continue;
3527 }
3528 const auto RegIt = find(UniqueVec, Inputs[SrcRegIdx]);
3529 assert(RegIt != UniqueVec.end() && "Cannot find non-const value.");
3530 Idx = (Idx % NewElts) +
3531 NewElts * (std::distance(UniqueVec.begin(), RegIt) + ConstNum);
3532 assert(Idx >= 0 && "Expected defined mask idx.");
3533 }
3534 copy(UniqueConstantVec, std::begin(Inputs));
3535 copy(UniqueVec, std::next(std::begin(Inputs), ConstNum));
3536 }
3537 };
3538 MakeUniqueInputs(OrigMask);
3539 SDValue OrigInputs[4];
3540 copy(Inputs, std::begin(OrigInputs));
3541 for (unsigned High = 0; High < 2; ++High) {
3542 SDValue &Output = High ? Hi : Lo;
3543
3544 // Build a shuffle mask for the output, discovering on the fly which
3545 // input vectors to use as shuffle operands.
3546 unsigned FirstMaskIdx = High * NewElts;
3547 SmallVector<int> Mask(NewElts * std::size(Inputs), PoisonMaskElem);
3548 copy(ArrayRef(OrigMask).slice(FirstMaskIdx, NewElts), Mask.begin());
3549 assert(!Output && "Expected default initialized initial value.");
3550 TryPeekThroughShufflesInputs(Mask);
3551 MakeUniqueInputs(Mask);
3552 SDValue TmpInputs[4];
3553 copy(Inputs, std::begin(TmpInputs));
3554 // Track changes in the output registers.
3555 int UsedIdx = -1;
3556 bool SecondIteration = false;
3557 auto &&AccumulateResults = [&UsedIdx, &SecondIteration](unsigned Idx) {
3558 if (UsedIdx < 0) {
3559 UsedIdx = Idx;
3560 return false;
3561 }
3562 if (UsedIdx >= 0 && static_cast<unsigned>(UsedIdx) == Idx)
3563 SecondIteration = true;
3564 return SecondIteration;
3565 };
3567 Mask, std::size(Inputs), std::size(Inputs),
3568 /*NumOfUsedRegs=*/1,
3569 [&Output, &DAG = DAG, NewVT]() { Output = DAG.getPOISON(NewVT); },
3570 [&Output, &DAG = DAG, NewVT, &DL, &Inputs,
3571 &BuildVector](ArrayRef<int> Mask, unsigned Idx, unsigned /*Unused*/) {
3572 if (Inputs[Idx]->getOpcode() == ISD::BUILD_VECTOR)
3573 Output = BuildVector(Inputs[Idx], Inputs[Idx], Mask);
3574 else
3575 Output = DAG.getVectorShuffle(NewVT, DL, Inputs[Idx],
3576 DAG.getPOISON(NewVT), Mask);
3577 Inputs[Idx] = Output;
3578 },
3579 [&AccumulateResults, &Output, &DAG = DAG, NewVT, &DL, &Inputs,
3580 &TmpInputs, &BuildVector](ArrayRef<int> Mask, unsigned Idx1,
3581 unsigned Idx2, bool /*Unused*/) {
3582 if (AccumulateResults(Idx1)) {
3583 if (Inputs[Idx1]->getOpcode() == ISD::BUILD_VECTOR &&
3584 Inputs[Idx2]->getOpcode() == ISD::BUILD_VECTOR)
3585 Output = BuildVector(Inputs[Idx1], Inputs[Idx2], Mask);
3586 else
3587 Output = DAG.getVectorShuffle(NewVT, DL, Inputs[Idx1],
3588 Inputs[Idx2], Mask);
3589 } else {
3590 if (TmpInputs[Idx1]->getOpcode() == ISD::BUILD_VECTOR &&
3591 TmpInputs[Idx2]->getOpcode() == ISD::BUILD_VECTOR)
3592 Output = BuildVector(TmpInputs[Idx1], TmpInputs[Idx2], Mask);
3593 else
3594 Output = DAG.getVectorShuffle(NewVT, DL, TmpInputs[Idx1],
3595 TmpInputs[Idx2], Mask);
3596 }
3597 Inputs[Idx1] = Output;
3598 });
3599 copy(OrigInputs, std::begin(Inputs));
3600 }
3601}
3602
3603void DAGTypeLegalizer::SplitVecRes_VAARG(SDNode *N, SDValue &Lo, SDValue &Hi) {
3604 EVT OVT = N->getValueType(0);
3605 EVT NVT = OVT.getHalfNumVectorElementsVT(*DAG.getContext());
3606 SDValue Chain = N->getOperand(0);
3607 SDValue Ptr = N->getOperand(1);
3608 SDValue SV = N->getOperand(2);
3609 SDLoc dl(N);
3610
3611 const Align Alignment =
3612 DAG.getDataLayout().getABITypeAlign(NVT.getTypeForEVT(*DAG.getContext()));
3613
3614 Lo = DAG.getVAArg(NVT, dl, Chain, Ptr, SV, Alignment.value());
3615 Hi = DAG.getVAArg(NVT, dl, Lo.getValue(1), Ptr, SV, Alignment.value());
3616 Chain = Hi.getValue(1);
3617
3618 // Modified the chain - switch anything that used the old chain to use
3619 // the new one.
3620 ReplaceValueWith(SDValue(N, 1), Chain);
3621}
3622
3623void DAGTypeLegalizer::SplitVecRes_FP_TO_XINT_SAT(SDNode *N, SDValue &Lo,
3624 SDValue &Hi) {
3625 EVT DstVTLo, DstVTHi;
3626 std::tie(DstVTLo, DstVTHi) = DAG.GetSplitDestVTs(N->getValueType(0));
3627 SDLoc dl(N);
3628
3629 SDValue SrcLo, SrcHi;
3630 EVT SrcVT = N->getOperand(0).getValueType();
3631 if (getTypeAction(SrcVT) == TargetLowering::TypeSplitVector)
3632 GetSplitVector(N->getOperand(0), SrcLo, SrcHi);
3633 else
3634 std::tie(SrcLo, SrcHi) = DAG.SplitVectorOperand(N, 0);
3635
3636 Lo = DAG.getNode(N->getOpcode(), dl, DstVTLo, SrcLo, N->getOperand(1));
3637 Hi = DAG.getNode(N->getOpcode(), dl, DstVTHi, SrcHi, N->getOperand(1));
3638}
3639
3640void DAGTypeLegalizer::SplitVecRes_VECTOR_REPEAT(SDNode *N, SDValue &Lo,
3641 SDValue &Hi) {
3642 EVT VT = N->getValueType(0);
3643 SDValue Src = N->getOperand(0);
3644 auto [LoVT, HiVT] = DAG.GetSplitDestVTs(VT);
3645 assert(LoVT == HiVT && "Expected equal split types");
3646
3647 // Use smaller even/odd source vectors so their broadcasts can be
3648 // reinterleaved in the original lane order for every value of vscale.
3649 SDLoc DL(N);
3650 auto [SrcLo, SrcHi] = DAG.SplitVector(Src, DL);
3651 EVT SplitSrcVT = SrcLo.getValueType();
3652 SDValue Deinterleaved =
3653 DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
3654 DAG.getVTList(SplitSrcVT, SplitSrcVT), SrcLo, SrcHi);
3655 SDValue Even =
3656 DAG.getNode(ISD::VECTOR_REPEAT, DL, LoVT, Deinterleaved.getValue(0));
3657 SDValue Odd =
3658 DAG.getNode(ISD::VECTOR_REPEAT, DL, LoVT, Deinterleaved.getValue(1));
3659 SDValue Interleaved = DAG.getNode(ISD::VECTOR_INTERLEAVE, DL,
3660 DAG.getVTList(LoVT, LoVT), Even, Odd);
3661 Lo = Interleaved.getValue(0);
3662 Hi = Interleaved.getValue(1);
3663}
3664
3665void DAGTypeLegalizer::SplitVecRes_VECTOR_REVERSE(SDNode *N, SDValue &Lo,
3666 SDValue &Hi) {
3667 SDValue InLo, InHi;
3668 GetSplitVector(N->getOperand(0), InLo, InHi);
3669 SDLoc DL(N);
3670
3671 Lo = DAG.getNode(ISD::VECTOR_REVERSE, DL, InHi.getValueType(), InHi);
3672 Hi = DAG.getNode(ISD::VECTOR_REVERSE, DL, InLo.getValueType(), InLo);
3673}
3674
3675void DAGTypeLegalizer::SplitVecRes_VECTOR_SPLICE(SDNode *N, SDValue &Lo,
3676 SDValue &Hi) {
3677 SDLoc DL(N);
3678
3679 SDValue Expanded = TLI.expandVectorSplice(N, DAG);
3680 std::tie(Lo, Hi) = DAG.SplitVector(Expanded, DL);
3681}
3682
3683void DAGTypeLegalizer::SplitVecRes_VP_REVERSE(SDNode *N, SDValue &Lo,
3684 SDValue &Hi) {
3685 EVT VT = N->getValueType(0);
3686 SDValue Val = N->getOperand(0);
3687 SDValue Mask = N->getOperand(1);
3688 SDValue EVL = N->getOperand(2);
3689 SDLoc DL(N);
3690
3691 // The stack round-trip uses a byte stride, so a sub-byte element (e.g. i1)
3692 // would get stride 0 and alias every lane. Widen to a byte integer, reverse,
3693 // then truncate back.
3694 EVT OrigVT = VT;
3695 if (!VT.getVectorElementType().isByteSized()) {
3696 EVT WideEltVT = VT.getVectorElementType().changeTypeToInteger();
3697 WideEltVT = WideEltVT.getRoundIntegerType(*DAG.getContext());
3698 VT = VT.changeVectorElementType(*DAG.getContext(), WideEltVT);
3699 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Val);
3700 }
3701
3702 // Fallback to VP_STRIDED_STORE to stack followed by VP_LOAD.
3703 Align Alignment = DAG.getReducedAlign(VT, /*UseABI=*/false);
3704
3705 EVT MemVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
3707 SDValue StackPtr = DAG.CreateStackTemporary(MemVT.getStoreSize(), Alignment);
3708 EVT PtrVT = StackPtr.getValueType();
3709 auto &MF = DAG.getMachineFunction();
3710 auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
3711 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
3712
3713 MachineMemOperand *StoreMMO = DAG.getMachineFunction().getMachineMemOperand(
3715 Alignment);
3716 MachineMemOperand *LoadMMO = DAG.getMachineFunction().getMachineMemOperand(
3718 Alignment);
3719
3720 EVL = DAG.getNode(
3721 ISD::UMIN, DL, EVL.getValueType(), DAG.getFreeze(EVL),
3722 DAG.getElementCount(DL, EVL.getValueType(), VT.getVectorElementCount()));
3723
3724 unsigned EltWidth = VT.getScalarSizeInBits() / 8;
3725 SDValue NumElemMinus1 =
3726 DAG.getNode(ISD::SUB, DL, PtrVT, DAG.getZExtOrTrunc(EVL, DL, PtrVT),
3727 DAG.getConstant(1, DL, PtrVT));
3728 SDValue StartOffset = DAG.getNode(ISD::MUL, DL, PtrVT, NumElemMinus1,
3729 DAG.getConstant(EltWidth, DL, PtrVT));
3730 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, StartOffset);
3731 SDValue Stride = DAG.getConstant(-(int64_t)EltWidth, DL, PtrVT);
3732
3733 SDValue TrueMask = DAG.getBoolConstant(true, DL, Mask.getValueType(), VT);
3734 SDValue Store = DAG.getStridedStoreVP(DAG.getEntryNode(), DL, Val, StorePtr,
3735 DAG.getPOISON(PtrVT), Stride, TrueMask,
3736 EVL, MemVT, StoreMMO, ISD::UNINDEXED);
3737
3738 SDValue Load = DAG.getLoadVP(VT, DL, Store, StackPtr, Mask, EVL, LoadMMO);
3739
3740 // Truncate back if we widened above.
3741 if (OrigVT != VT)
3742 Load = DAG.getNode(ISD::TRUNCATE, DL, OrigVT, Load);
3743
3744 std::tie(Lo, Hi) = DAG.SplitVector(Load, DL);
3745}
3746
3747void DAGTypeLegalizer::SplitVecRes_VP_SPLICE(SDNode *N, SDValue &Lo,
3748 SDValue &Hi) {
3749 EVT VT = N->getValueType(0);
3750 SDValue V1 = N->getOperand(0);
3751 SDValue V2 = N->getOperand(1);
3752 int64_t Imm = cast<ConstantSDNode>(N->getOperand(2))->getSExtValue();
3753 SDValue Mask = N->getOperand(3);
3754 SDValue EVL1 = N->getOperand(4);
3755 SDValue EVL2 = N->getOperand(5);
3756 SDLoc DL(N);
3757
3758 // Since EVL2 is considered the real VL it gets promoted during
3759 // SelectionDAGBuilder. Promote EVL1 here if needed.
3760 if (getTypeAction(EVL1.getValueType()) == TargetLowering::TypePromoteInteger)
3761 EVL1 = ZExtPromotedInteger(EVL1);
3762
3763 // The stack splice addresses elements by byte offset/stride, which breaks for
3764 // a sub-byte element (e.g. i1): getVectorElementPointer asserts and the
3765 // stride is 0. Widen to a byte integer, splice, then truncate back.
3766 EVT OrigVT = VT;
3767 if (!VT.getVectorElementType().isByteSized()) {
3768 EVT WideEltVT = VT.getVectorElementType().changeTypeToInteger();
3769 WideEltVT = WideEltVT.getRoundIntegerType(*DAG.getContext());
3770 VT = VT.changeVectorElementType(*DAG.getContext(), WideEltVT);
3771 V1 = DAG.getNode(ISD::ANY_EXTEND, DL, VT, V1);
3772 V2 = DAG.getNode(ISD::ANY_EXTEND, DL, VT, V2);
3773 }
3774
3775 Align Alignment = DAG.getReducedAlign(VT, /*UseABI=*/false);
3776
3777 EVT MemVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
3778 VT.getVectorElementCount() * 2);
3779 SDValue StackPtr = DAG.CreateStackTemporary(MemVT.getStoreSize(), Alignment);
3780 EVT PtrVT = StackPtr.getValueType();
3781 auto &MF = DAG.getMachineFunction();
3782 auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
3783 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
3784
3785 MachineMemOperand *StoreMMO = DAG.getMachineFunction().getMachineMemOperand(
3787 Alignment);
3788 MachineMemOperand *LoadMMO = DAG.getMachineFunction().getMachineMemOperand(
3790 Alignment);
3791
3792 SDValue EltByteSize =
3793 DAG.getTypeSize(DL, PtrVT, VT.getVectorElementType().getStoreSize());
3794 SDValue EVL1Ptr = DAG.getZExtOrTrunc(DAG.getFreeze(EVL1), DL, PtrVT);
3795 SDValue EVL1Bytes = DAG.getNode(ISD::MUL, DL, PtrVT, EVL1Ptr, EltByteSize);
3796 // Clip EVL1Bytes to make sure we stay within the stack object.
3797 SDValue VTBytes = DAG.getTypeSize(DL, PtrVT, VT.getStoreSize());
3798 EVL1Bytes = DAG.getNode(ISD::UMIN, DL, PtrVT, EVL1Bytes, VTBytes);
3799 SDValue StackPtr2 = DAG.getMemBasePlusOffset(StackPtr, EVL1Bytes, DL);
3800 SDValue PoisonPtr = DAG.getPOISON(PtrVT);
3801
3802 SDValue TrueMask = DAG.getBoolConstant(true, DL, Mask.getValueType(), VT);
3803 SDValue StoreV1 =
3804 DAG.getStoreVP(DAG.getEntryNode(), DL, V1, StackPtr, PoisonPtr, TrueMask,
3805 EVL1, V1.getValueType(), StoreMMO, ISD::UNINDEXED);
3806
3808 DAG.getStoreVP(StoreV1, DL, V2, StackPtr2, PoisonPtr, TrueMask, EVL2,
3809 V2.getValueType(), StoreMMO, ISD::UNINDEXED);
3810
3811 SDValue Load;
3812 if (Imm >= 0) {
3813 StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VT, N->getOperand(2));
3814 Load = DAG.getLoadVP(VT, DL, StoreV2, StackPtr, Mask, EVL2, LoadMMO);
3815 } else {
3816 uint64_t TrailingElts = -Imm;
3817 unsigned EltWidth = VT.getScalarSizeInBits() / 8;
3818 SDValue TrailingBytes = DAG.getConstant(TrailingElts * EltWidth, DL, PtrVT);
3819
3820 // Make sure TrailingBytes doesn't exceed the size of vec1.
3821 SDValue OffsetToV2 = DAG.getNode(ISD::SUB, DL, PtrVT, StackPtr2, StackPtr);
3822 TrailingBytes =
3823 DAG.getNode(ISD::UMIN, DL, PtrVT, TrailingBytes, OffsetToV2);
3824
3825 // Calculate the start address of the spliced result.
3826 StackPtr2 = DAG.getNode(ISD::SUB, DL, PtrVT, StackPtr2, TrailingBytes);
3827 Load = DAG.getLoadVP(VT, DL, StoreV2, StackPtr2, Mask, EVL2, LoadMMO);
3828 }
3829
3830 // Truncate back if we widened above.
3831 if (OrigVT != VT)
3832 Load = DAG.getNode(ISD::TRUNCATE, DL, OrigVT, Load);
3833
3834 EVT LoVT, HiVT;
3835 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(OrigVT);
3836 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, Load,
3837 DAG.getVectorIdxConstant(0, DL));
3838 Hi =
3839 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, Load,
3840 DAG.getVectorIdxConstant(LoVT.getVectorMinNumElements(), DL));
3841}
3842
3843void DAGTypeLegalizer::SplitVecRes_PARTIAL_REDUCE_MLA(SDNode *N, SDValue &Lo,
3844 SDValue &Hi) {
3845 SDLoc DL(N);
3846 SDValue Acc = N->getOperand(0);
3847 SDValue Input1 = N->getOperand(1);
3848 SDValue Input2 = N->getOperand(2);
3849
3850 SDValue AccLo, AccHi;
3851 GetSplitVector(Acc, AccLo, AccHi);
3852 unsigned Opcode = N->getOpcode();
3853
3854 // If the input types don't need splitting, just accumulate into the
3855 // low part of the accumulator.
3856 if (getTypeAction(Input1.getValueType()) != TargetLowering::TypeSplitVector) {
3857 Lo = DAG.getNode(Opcode, DL, AccLo.getValueType(), AccLo, Input1, Input2);
3858 Hi = AccHi;
3859 return;
3860 }
3861
3862 SDValue Input1Lo, Input1Hi;
3863 SDValue Input2Lo, Input2Hi;
3864 GetSplitVector(Input1, Input1Lo, Input1Hi);
3865 GetSplitVector(Input2, Input2Lo, Input2Hi);
3866 EVT ResultVT = AccLo.getValueType();
3867
3868 Lo = DAG.getNode(Opcode, DL, ResultVT, AccLo, Input1Lo, Input2Lo);
3869 Hi = DAG.getNode(Opcode, DL, ResultVT, AccHi, Input1Hi, Input2Hi);
3870}
3871
3872void DAGTypeLegalizer::SplitVecRes_GET_ACTIVE_LANE_MASK(SDNode *N, SDValue &Lo,
3873 SDValue &Hi) {
3874 SDLoc DL(N);
3875 SDValue Op0 = N->getOperand(0);
3876 SDValue Op1 = N->getOperand(1);
3877 EVT OpVT = Op0.getValueType();
3878
3879 EVT LoVT, HiVT;
3880 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
3881
3882 Lo = DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, DL, LoVT, Op0, Op1);
3883 SDValue LoElts = DAG.getElementCount(DL, OpVT, LoVT.getVectorElementCount());
3884 SDValue HiStartVal = DAG.getNode(ISD::UADDSAT, DL, OpVT, Op0, LoElts);
3885 Hi = DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, DL, HiVT, HiStartVal, Op1);
3886}
3887
3888void DAGTypeLegalizer::SplitVecRes_VECTOR_MATCH(SDNode *N, SDValue &Lo,
3889 SDValue &Hi) {
3890 SDValue SourceLo, SourceHi;
3891 GetSplitVector(N->getOperand(0), SourceLo, SourceHi);
3892 SDValue MaskLo, MaskHi;
3893 GetSplitVector(N->getOperand(2), MaskLo, MaskHi);
3894 SDLoc DL(N);
3895
3896 Lo = DAG.getNode(ISD::VECTOR_MATCH, DL, MaskLo.getValueType(), SourceLo,
3897 N->getOperand(1), MaskLo, N->getFlags());
3898 Hi = DAG.getNode(ISD::VECTOR_MATCH, DL, MaskHi.getValueType(), SourceHi,
3899 N->getOperand(1), MaskHi, N->getFlags());
3900}
3901
3902void DAGTypeLegalizer::SplitVecRes_VECTOR_DEINTERLEAVE(SDNode *N) {
3903 unsigned Factor = N->getNumOperands();
3904
3905 SmallVector<SDValue, 8> Ops(Factor * 2);
3906 for (unsigned i = 0; i != Factor; ++i) {
3907 SDValue OpLo, OpHi;
3908 GetSplitVector(N->getOperand(i), OpLo, OpHi);
3909 Ops[i * 2] = OpLo;
3910 Ops[i * 2 + 1] = OpHi;
3911 }
3912
3913 SmallVector<EVT, 8> VTs(Factor, Ops[0].getValueType());
3914
3915 SDLoc DL(N);
3916 SDValue ResLo = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL, VTs,
3917 ArrayRef(Ops).slice(0, Factor));
3918 SDValue ResHi = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL, VTs,
3919 ArrayRef(Ops).slice(Factor, Factor));
3920
3921 for (unsigned i = 0; i != Factor; ++i)
3922 SetSplitVector(SDValue(N, i), ResLo.getValue(i), ResHi.getValue(i));
3923}
3924
3925void DAGTypeLegalizer::SplitVecRes_VECTOR_INTERLEAVE(SDNode *N) {
3926 unsigned Factor = N->getNumOperands();
3927
3928 SmallVector<SDValue, 8> Ops(Factor * 2);
3929 for (unsigned i = 0; i != Factor; ++i) {
3930 SDValue OpLo, OpHi;
3931 GetSplitVector(N->getOperand(i), OpLo, OpHi);
3932 Ops[i] = OpLo;
3933 Ops[i + Factor] = OpHi;
3934 }
3935
3936 SmallVector<EVT, 8> VTs(Factor, Ops[0].getValueType());
3937
3938 SDLoc DL(N);
3939 SDValue Res[] = {DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, VTs,
3940 ArrayRef(Ops).slice(0, Factor)),
3941 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, VTs,
3942 ArrayRef(Ops).slice(Factor, Factor))};
3943
3944 for (unsigned i = 0; i != Factor; ++i) {
3945 unsigned IdxLo = 2 * i;
3946 unsigned IdxHi = 2 * i + 1;
3947 SetSplitVector(SDValue(N, i), Res[IdxLo / Factor].getValue(IdxLo % Factor),
3948 Res[IdxHi / Factor].getValue(IdxHi % Factor));
3949 }
3950}
3951
3952//===----------------------------------------------------------------------===//
3953// Operand Vector Splitting
3954//===----------------------------------------------------------------------===//
3955
3956/// This method is called when the specified operand of the specified node is
3957/// found to need vector splitting. At this point, all of the result types of
3958/// the node are known to be legal, but other operands of the node may need
3959/// legalization as well as the specified one.
3960bool DAGTypeLegalizer::SplitVectorOperand(SDNode *N, unsigned OpNo) {
3961 LLVM_DEBUG(dbgs() << "Split node operand: "; N->dump(&DAG));
3962 SDValue Res = SDValue();
3963
3964 // See if the target wants to custom split this node.
3965 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
3966 return false;
3967
3968 switch (N->getOpcode()) {
3969 default:
3970#ifndef NDEBUG
3971 dbgs() << "SplitVectorOperand Op #" << OpNo << ": ";
3972 N->dump(&DAG);
3973 dbgs() << "\n";
3974#endif
3975 report_fatal_error("Do not know how to split this operator's "
3976 "operand!\n");
3977
3978 case ISD::STRICT_FSETCC:
3980 case ISD::SETCC: Res = SplitVecOp_VSETCC(N); break;
3981 case ISD::BITCAST: Res = SplitVecOp_BITCAST(N); break;
3982 case ISD::EXTRACT_SUBVECTOR: Res = SplitVecOp_EXTRACT_SUBVECTOR(N); break;
3983 case ISD::INSERT_SUBVECTOR: Res = SplitVecOp_INSERT_SUBVECTOR(N, OpNo); break;
3984 case ISD::EXTRACT_VECTOR_ELT:Res = SplitVecOp_EXTRACT_VECTOR_ELT(N); break;
3985 case ISD::CONCAT_VECTORS: Res = SplitVecOp_CONCAT_VECTORS(N); break;
3987 Res = SplitVecOp_VECTOR_FIND_LAST_ACTIVE(N);
3988 break;
3989 case ISD::TRUNCATE:
3990 Res = SplitVecOp_TruncateHelper(N);
3991 break;
3993 case ISD::FP_ROUND:
3996 Res = SplitVecOp_FP_ROUND(N);
3997 break;
3998 case ISD::FCOPYSIGN: Res = SplitVecOp_FPOpDifferentTypes(N); break;
3999 case ISD::STORE:
4000 Res = SplitVecOp_STORE(cast<StoreSDNode>(N), OpNo);
4001 break;
4002 case ISD::ATOMIC_STORE:
4003 Res = SplitVecOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
4004 break;
4005 case ISD::VP_STORE:
4006 Res = SplitVecOp_VP_STORE(cast<VPStoreSDNode>(N), OpNo);
4007 break;
4008 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
4009 Res = SplitVecOp_VP_STRIDED_STORE(cast<VPStridedStoreSDNode>(N), OpNo);
4010 break;
4011 case ISD::MSTORE:
4012 Res = SplitVecOp_MSTORE(cast<MaskedStoreSDNode>(N), OpNo);
4013 break;
4014 case ISD::MSCATTER:
4015 case ISD::VP_SCATTER:
4016 Res = SplitVecOp_Scatter(cast<MemSDNode>(N), OpNo);
4017 break;
4018 case ISD::MGATHER:
4019 case ISD::VP_GATHER:
4020 Res = SplitVecOp_Gather(cast<MemSDNode>(N), OpNo);
4021 break;
4022 case ISD::VSELECT:
4023 Res = SplitVecOp_VSELECT(N, OpNo);
4024 break;
4025 case ISD::MASKED_UDIV:
4026 case ISD::MASKED_SDIV:
4027 case ISD::MASKED_UREM:
4028 case ISD::MASKED_SREM:
4029 Res = SplitVecOp_MaskedBinOp(N, OpNo);
4030 break;
4032 Res = SplitVecOp_VECTOR_COMPRESS(N, OpNo);
4033 break;
4035 Res = SplitVecOp_VECTOR_SHUFFLE_VAR(N, OpNo);
4036 break;
4039 case ISD::SINT_TO_FP:
4040 case ISD::UINT_TO_FP:
4041 if (N->getValueType(0).bitsLT(
4042 N->getOperand(N->isStrictFPOpcode() ? 1 : 0).getValueType()))
4043 Res = SplitVecOp_TruncateHelper(N);
4044 else
4045 Res = SplitVecOp_UnaryOp(N);
4046 break;
4049 Res = SplitVecOp_FP_TO_XINT_SAT(N);
4050 break;
4051 case ISD::FP_TO_SINT:
4052 case ISD::FP_TO_UINT:
4056 case ISD::FP_EXTEND:
4057 case ISD::SIGN_EXTEND:
4058 case ISD::ZERO_EXTEND:
4059 case ISD::ANY_EXTEND:
4060 case ISD::FTRUNC:
4061 case ISD::LROUND:
4062 case ISD::LLROUND:
4063 case ISD::LRINT:
4064 case ISD::LLRINT:
4065 Res = SplitVecOp_UnaryOp(N);
4066 break;
4067 case ISD::FLDEXP:
4068 Res = SplitVecOp_FPOpDifferentTypes(N);
4069 break;
4070
4071 case ISD::SCMP:
4072 case ISD::UCMP:
4073 Res = SplitVecOp_CMP(N);
4074 break;
4075
4076 case ISD::FAKE_USE:
4077 Res = SplitVecOp_FAKE_USE(N);
4078 break;
4082 Res = SplitVecOp_ExtVecInRegOp(N);
4083 break;
4084
4087 case ISD::VECREDUCE_ADD:
4088 case ISD::VECREDUCE_MUL:
4089 case ISD::VECREDUCE_AND:
4090 case ISD::VECREDUCE_OR:
4091 case ISD::VECREDUCE_XOR:
4102 Res = SplitVecOp_VECREDUCE(N, OpNo);
4103 break;
4106 Res = SplitVecOp_VECREDUCE_SEQ(N);
4107 break;
4108 case ISD::VP_REDUCE_FADD:
4109 case ISD::VP_REDUCE_SEQ_FADD:
4110 case ISD::VP_REDUCE_FMUL:
4111 case ISD::VP_REDUCE_SEQ_FMUL:
4112 case ISD::VP_REDUCE_ADD:
4113 case ISD::VP_REDUCE_MUL:
4114 case ISD::VP_REDUCE_AND:
4115 case ISD::VP_REDUCE_OR:
4116 case ISD::VP_REDUCE_XOR:
4117 case ISD::VP_REDUCE_SMAX:
4118 case ISD::VP_REDUCE_SMIN:
4119 case ISD::VP_REDUCE_UMAX:
4120 case ISD::VP_REDUCE_UMIN:
4121 case ISD::VP_REDUCE_FMAX:
4122 case ISD::VP_REDUCE_FMIN:
4123 case ISD::VP_REDUCE_FMAXIMUM:
4124 case ISD::VP_REDUCE_FMINIMUM:
4125 Res = SplitVecOp_VP_REDUCE(N, OpNo);
4126 break;
4127 case ISD::CTTZ_ELTS:
4129 Res = SplitVecOp_CttzElts(N);
4130 break;
4131 case ISD::VP_CTTZ_ELTS:
4132 case ISD::VP_CTTZ_ELTS_ZERO_POISON:
4133 Res = SplitVecOp_VP_CttzElements(N);
4134 break;
4136 Res = SplitVecOp_VECTOR_HISTOGRAM(N);
4137 break;
4142 Res = SplitVecOp_PARTIAL_REDUCE_MLA(N);
4143 break;
4144 case ISD::VECTOR_MATCH:
4145 Res = SplitVecOp_VECTOR_MATCH(N, OpNo);
4146 break;
4147 }
4148
4149 // If the result is null, the sub-method took care of registering results etc.
4150 if (!Res.getNode()) return false;
4151
4152 // If the result is N, the sub-method updated N in place. Tell the legalizer
4153 // core about this.
4154 if (Res.getNode() == N)
4155 return true;
4156
4157 if (N->isStrictFPOpcode())
4158 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 2 &&
4159 "Invalid operand expansion");
4160 else
4161 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
4162 "Invalid operand expansion");
4163
4164 ReplaceValueWith(SDValue(N, 0), Res);
4165 return false;
4166}
4167
4168SDValue DAGTypeLegalizer::SplitVecOp_VECTOR_FIND_LAST_ACTIVE(SDNode *N) {
4169 SDLoc DL(N);
4170
4171 SDValue LoMask, HiMask;
4172 GetSplitVector(N->getOperand(0), LoMask, HiMask);
4173
4174 EVT VT = N->getValueType(0);
4175 EVT SplitVT = LoMask.getValueType();
4176 ElementCount SplitEC = SplitVT.getVectorElementCount();
4177
4178 // Find the last active in both the low and the high masks.
4179 SDValue LoFind = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, DL, VT, LoMask);
4180 SDValue HiFind = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, DL, VT, HiMask);
4181
4182 // Check if any lane is active in the high mask.
4183 // FIXME: This would not be necessary if VECTOR_FIND_LAST_ACTIVE returned a
4184 // sentinel value for "none active".
4185 SDValue AnyHiActive = DAG.getNode(ISD::VECREDUCE_OR, DL, MVT::i1, HiMask);
4186 SDValue Cond = DAG.getBoolExtOrTrunc(AnyHiActive, DL,
4187 getSetCCResultType(MVT::i1), MVT::i1);
4188
4189 // Return: AnyHiActive ? (HiFind + SplitEC) : LoFind;
4190 return DAG.getNode(ISD::SELECT, DL, VT, Cond,
4191 DAG.getNode(ISD::ADD, DL, VT, HiFind,
4192 DAG.getElementCount(DL, VT, SplitEC)),
4193 LoFind);
4194}
4195
4196SDValue DAGTypeLegalizer::SplitVecOp_VSELECT(SDNode *N, unsigned OpNo) {
4197 // The only possibility for an illegal operand is the mask, since result type
4198 // legalization would have handled this node already otherwise.
4199 assert(OpNo == 0 && "Illegal operand must be mask");
4200
4201 SDValue Mask = N->getOperand(0);
4202 SDValue Src0 = N->getOperand(1);
4203 SDValue Src1 = N->getOperand(2);
4204 EVT Src0VT = Src0.getValueType();
4205 SDLoc DL(N);
4206 assert(Mask.getValueType().isVector() && "VSELECT without a vector mask?");
4207
4208 SDValue Lo, Hi;
4209 GetSplitVector(N->getOperand(0), Lo, Hi);
4210 assert(Lo.getValueType() == Hi.getValueType() &&
4211 "Lo and Hi have differing types");
4212
4213 EVT LoOpVT, HiOpVT;
4214 std::tie(LoOpVT, HiOpVT) = DAG.GetSplitDestVTs(Src0VT);
4215 assert(LoOpVT == HiOpVT && "Asymmetric vector split?");
4216
4217 SDValue LoOp0, HiOp0, LoOp1, HiOp1, LoMask, HiMask;
4218 std::tie(LoOp0, HiOp0) = DAG.SplitVector(Src0, DL);
4219 std::tie(LoOp1, HiOp1) = DAG.SplitVector(Src1, DL);
4220 std::tie(LoMask, HiMask) = DAG.SplitVector(Mask, DL);
4221
4222 SDValue LoSelect =
4223 DAG.getNode(ISD::VSELECT, DL, LoOpVT, LoMask, LoOp0, LoOp1);
4224 SDValue HiSelect =
4225 DAG.getNode(ISD::VSELECT, DL, HiOpVT, HiMask, HiOp0, HiOp1);
4226
4227 return DAG.getNode(ISD::CONCAT_VECTORS, DL, Src0VT, LoSelect, HiSelect);
4228}
4229
4230SDValue DAGTypeLegalizer::SplitVecOp_MaskedBinOp(SDNode *N, unsigned OpNo) {
4231 assert(OpNo == 2 && "Illegal operand must be mask");
4232
4233 SDLoc DL(N);
4234 auto [LHSLo, LHSHi] = DAG.SplitVector(N->getOperand(0), DL);
4235 auto [RHSLo, RHSHi] = DAG.SplitVector(N->getOperand(1), DL);
4236 SDValue MaskLo, MaskHi;
4237 GetSplitVector(N->getOperand(2), MaskLo, MaskHi);
4238
4239 SDValue Lo = DAG.getNode(N->getOpcode(), DL, LHSLo.getValueType(), LHSLo,
4240 RHSLo, MaskLo, N->getFlags());
4241 SDValue Hi = DAG.getNode(N->getOpcode(), DL, LHSHi.getValueType(), LHSHi,
4242 RHSHi, MaskHi, N->getFlags());
4243 return DAG.getNode(ISD::CONCAT_VECTORS, DL, N->getValueType(0), Lo, Hi);
4244}
4245
4246SDValue DAGTypeLegalizer::SplitVecOp_VECTOR_COMPRESS(SDNode *N, unsigned OpNo) {
4247 // The only possibility for an illegal operand is the mask, since result type
4248 // legalization would have handled this node already otherwise.
4249 assert(OpNo == 1 && "Illegal operand must be mask");
4250
4251 // To split the mask, we need to split the result type too, so we can just
4252 // reuse that logic here.
4253 SDValue Lo, Hi;
4254 SplitVecRes_VECTOR_COMPRESS(N, Lo, Hi);
4255
4256 EVT VecVT = N->getValueType(0);
4257 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VecVT, Lo, Hi);
4258}
4259
4260SDValue DAGTypeLegalizer::SplitVecOp_VECTOR_SHUFFLE_VAR(SDNode *N,
4261 unsigned OpNo) {
4262 // The only possibility for an illegal operand is the mask, since result type
4263 // legalization would have handled this node already otherwise.
4264 assert(OpNo == 1 && "Illegal operand must be mask");
4265 // TODO: Legalize the mask type instead once a target lowers the node.
4266 return TLI.expandVECTOR_SHUFFLE_VAR(N, DAG);
4267}
4268
4269SDValue DAGTypeLegalizer::SplitVecOp_VECREDUCE(SDNode *N, unsigned OpNo) {
4270 EVT ResVT = N->getValueType(0);
4271 SDValue Lo, Hi;
4272 SDLoc dl(N);
4273
4274 SDValue VecOp = N->getOperand(OpNo);
4275 EVT VecVT = VecOp.getValueType();
4276 assert(VecVT.isVector() && "Can only split reduce vector operand");
4277 GetSplitVector(VecOp, Lo, Hi);
4278 EVT LoOpVT, HiOpVT;
4279 std::tie(LoOpVT, HiOpVT) = DAG.GetSplitDestVTs(VecVT);
4280
4281 // Use the appropriate scalar instruction on the split subvectors before
4282 // reducing the now partially reduced smaller vector.
4283 unsigned CombineOpc = ISD::getVecReduceBaseOpcode(N->getOpcode());
4284 SDValue Partial = DAG.getNode(CombineOpc, dl, LoOpVT, Lo, Hi, N->getFlags());
4285 return DAG.getNode(N->getOpcode(), dl, ResVT, Partial, N->getFlags());
4286}
4287
4288SDValue DAGTypeLegalizer::SplitVecOp_VECREDUCE_SEQ(SDNode *N) {
4289 EVT ResVT = N->getValueType(0);
4290 SDValue Lo, Hi;
4291 SDLoc dl(N);
4292
4293 SDValue AccOp = N->getOperand(0);
4294 SDValue VecOp = N->getOperand(1);
4295 SDNodeFlags Flags = N->getFlags();
4296
4297 EVT VecVT = VecOp.getValueType();
4298 assert(VecVT.isVector() && "Can only split reduce vector operand");
4299 GetSplitVector(VecOp, Lo, Hi);
4300 EVT LoOpVT, HiOpVT;
4301 std::tie(LoOpVT, HiOpVT) = DAG.GetSplitDestVTs(VecVT);
4302
4303 // Reduce low half.
4304 SDValue Partial = DAG.getNode(N->getOpcode(), dl, ResVT, AccOp, Lo, Flags);
4305
4306 // Reduce high half, using low half result as initial value.
4307 return DAG.getNode(N->getOpcode(), dl, ResVT, Partial, Hi, Flags);
4308}
4309
4310SDValue DAGTypeLegalizer::SplitVecOp_VP_REDUCE(SDNode *N, unsigned OpNo) {
4311 assert(N->isVPOpcode() && "Expected VP opcode");
4312 assert(OpNo == 1 && "Can only split reduce vector operand");
4313
4314 unsigned Opc = N->getOpcode();
4315 EVT ResVT = N->getValueType(0);
4316 SDValue Lo, Hi;
4317 SDLoc dl(N);
4318
4319 SDValue VecOp = N->getOperand(OpNo);
4320 EVT VecVT = VecOp.getValueType();
4321 assert(VecVT.isVector() && "Can only split reduce vector operand");
4322 GetSplitVector(VecOp, Lo, Hi);
4323
4324 SDValue MaskLo, MaskHi;
4325 std::tie(MaskLo, MaskHi) = SplitMask(N->getOperand(2));
4326
4327 SDValue EVLLo, EVLHi;
4328 std::tie(EVLLo, EVLHi) = DAG.SplitEVL(N->getOperand(3), VecVT, dl);
4329
4330 const SDNodeFlags Flags = N->getFlags();
4331
4332 SDValue ResLo =
4333 DAG.getNode(Opc, dl, ResVT, {N->getOperand(0), Lo, MaskLo, EVLLo}, Flags);
4334 return DAG.getNode(Opc, dl, ResVT, {ResLo, Hi, MaskHi, EVLHi}, Flags);
4335}
4336
4337SDValue DAGTypeLegalizer::SplitVecOp_UnaryOp(SDNode *N) {
4338 // The result has a legal vector type, but the input needs splitting.
4339 EVT ResVT = N->getValueType(0);
4340 SDValue Lo, Hi;
4341 SDLoc dl(N);
4342 GetSplitVector(N->getOperand(N->isStrictFPOpcode() ? 1 : 0), Lo, Hi);
4343 EVT InVT = Lo.getValueType();
4344
4345 EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
4346 InVT.getVectorElementCount());
4347
4348 if (N->isStrictFPOpcode()) {
4349 Lo = DAG.getNode(N->getOpcode(), dl, {OutVT, MVT::Other},
4350 {N->getOperand(0), Lo});
4351 Hi = DAG.getNode(N->getOpcode(), dl, {OutVT, MVT::Other},
4352 {N->getOperand(0), Hi});
4353
4354 // Build a factor node to remember that this operation is independent
4355 // of the other one.
4356 SDValue Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
4357 Hi.getValue(1));
4358
4359 // Legalize the chain result - switch anything that used the old chain to
4360 // use the new one.
4361 ReplaceValueWith(SDValue(N, 1), Ch);
4362 } else {
4363 Lo = DAG.getNode(N->getOpcode(), dl, OutVT, Lo);
4364 Hi = DAG.getNode(N->getOpcode(), dl, OutVT, Hi);
4365 }
4366
4367 return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi);
4368}
4369
4370// Split a FAKE_USE use of a vector into FAKE_USEs of hi and lo part.
4371SDValue DAGTypeLegalizer::SplitVecOp_FAKE_USE(SDNode *N) {
4372 SDValue Lo, Hi;
4373 GetSplitVector(N->getOperand(1), Lo, Hi);
4374 SDValue Chain =
4375 DAG.getNode(ISD::FAKE_USE, SDLoc(), MVT::Other, N->getOperand(0), Lo);
4376 return DAG.getNode(ISD::FAKE_USE, SDLoc(), MVT::Other, Chain, Hi);
4377}
4378
4379SDValue DAGTypeLegalizer::SplitVecOp_BITCAST(SDNode *N) {
4380 // For example, i64 = BITCAST v4i16 on alpha. Typically the vector will
4381 // end up being split all the way down to individual components. Convert the
4382 // split pieces into integers and reassemble.
4383 EVT ResVT = N->getValueType(0);
4384 SDValue Lo, Hi;
4385 GetSplitVector(N->getOperand(0), Lo, Hi);
4386 SDLoc dl(N);
4387
4388 if (ResVT.isScalableVector()) {
4389 auto [LoVT, HiVT] = DAG.GetSplitDestVTs(ResVT);
4390 Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
4391 Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
4392 return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi);
4393 }
4394
4395 Lo = BitConvertToInteger(Lo);
4396 Hi = BitConvertToInteger(Hi);
4397
4398 if (DAG.getDataLayout().isBigEndian())
4399 std::swap(Lo, Hi);
4400
4401 return DAG.getNode(ISD::BITCAST, dl, ResVT, JoinIntegers(Lo, Hi));
4402}
4403
4404SDValue DAGTypeLegalizer::SplitVecOp_INSERT_SUBVECTOR(SDNode *N,
4405 unsigned OpNo) {
4406 assert(OpNo == 1 && "Invalid OpNo; can only split SubVec.");
4407 // We know that the result type is legal.
4408 EVT ResVT = N->getValueType(0);
4409
4410 SDValue Vec = N->getOperand(0);
4411 SDValue SubVec = N->getOperand(1);
4412 SDValue Idx = N->getOperand(2);
4413 SDLoc dl(N);
4414
4415 SDValue Lo, Hi;
4416 GetSplitVector(SubVec, Lo, Hi);
4417
4418 uint64_t IdxVal = Idx->getAsZExtVal();
4420
4421 SDValue FirstInsertion =
4422 DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, Vec, Lo, Idx);
4423 SDValue SecondInsertion =
4424 DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, FirstInsertion, Hi,
4425 DAG.getVectorIdxConstant(IdxVal + LoElts, dl));
4426
4427 return SecondInsertion;
4428}
4429
4430SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
4431 // We know that the extracted result type is legal.
4432 EVT SubVT = N->getValueType(0);
4433 SDValue Idx = N->getOperand(1);
4434 SDLoc dl(N);
4435 SDValue Lo, Hi;
4436
4437 GetSplitVector(N->getOperand(0), Lo, Hi);
4438
4439 ElementCount LoElts = Lo.getValueType().getVectorElementCount();
4440 // Note: For scalable vectors, the index is scaled by vscale.
4441 ElementCount IdxVal =
4442 ElementCount::get(Idx->getAsZExtVal(), SubVT.isScalableVector());
4443 uint64_t IdxValMin = IdxVal.getKnownMinValue();
4444
4445 EVT SrcVT = N->getOperand(0).getValueType();
4446 ElementCount NumResultElts = SubVT.getVectorElementCount();
4447
4448 // If the extracted elements are all in the low half, do a simple extract.
4449 if (ElementCount::isKnownLE(IdxVal + NumResultElts, LoElts))
4450 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, Lo, Idx);
4451
4452 unsigned LoEltsMin = LoElts.getKnownMinValue();
4453 if (IdxValMin < LoEltsMin && SubVT.isFixedLengthVector() &&
4454 SrcVT.isFixedLengthVector()) {
4455 // Extracted subvector crosses vector split, so we need to blend the two
4456 // halves.
4457 // TODO: May be able to emit partial extract_subvector.
4459 Elts.reserve(NumResultElts.getFixedValue());
4460
4461 // This is not valid for scalable vectors. If SubVT is scalable, this is the
4462 // same as unrolling a scalable dimension (invalid). If ScrVT is scalable,
4463 // `Lo[LoEltsMin]` may not be the last element of `Lo`.
4464 DAG.ExtractVectorElements(Lo, Elts, /*Start=*/IdxValMin,
4465 /*Count=*/LoEltsMin - IdxValMin);
4466 DAG.ExtractVectorElements(Hi, Elts, /*Start=*/0,
4467 /*Count=*/SubVT.getVectorNumElements() -
4468 Elts.size());
4469 return DAG.getBuildVector(SubVT, dl, Elts);
4470 }
4471
4472 if (SubVT.isScalableVector() == SrcVT.isScalableVector()) {
4473 ElementCount ExtractIdx = IdxVal - LoElts;
4474 if (ExtractIdx.isKnownMultipleOf(NumResultElts))
4475 return DAG.getExtractSubvector(dl, SubVT, Hi,
4476 ExtractIdx.getKnownMinValue());
4477
4478 EVT HiVT = Hi.getValueType();
4479 assert(HiVT.isFixedLengthVector() &&
4480 "Only fixed-vector extracts are supported in this case");
4481
4482 // We cannot create an extract_subvector that isn't a multiple of the
4483 // result size, which may go out of bounds for the last elements. Shuffle
4484 // the desired elements down to 0 and do a simple 0 extract.
4485 SmallVector<int, 8> Mask(HiVT.getVectorNumElements(), -1);
4486 for (int I = 0; I != int(NumResultElts.getFixedValue()); ++I)
4487 Mask[I] = int(ExtractIdx.getFixedValue()) + I;
4488
4489 SDValue Shuffle =
4490 DAG.getVectorShuffle(HiVT, dl, Hi, DAG.getPOISON(HiVT), Mask);
4491 return DAG.getExtractSubvector(dl, SubVT, Shuffle, 0);
4492 }
4493
4494 // After this point the DAG node only permits extracting fixed-width
4495 // subvectors from scalable vectors.
4496 assert(SubVT.isFixedLengthVector() &&
4497 "Extracting scalable subvector from fixed-width unsupported");
4498
4499 // If the element type is i1 and we're not promoting the result, then we may
4500 // end up loading the wrong data since the bits are packed tightly into
4501 // bytes. For example, if we extract a v4i1 (legal) from a nxv4i1 (legal)
4502 // type at index 4, then we will load a byte starting at index 0.
4503 if (SubVT.getScalarType() == MVT::i1)
4504 report_fatal_error("Don't know how to extract fixed-width predicate "
4505 "subvector from a scalable predicate vector");
4506
4507 // Spill the vector to the stack. We should use the alignment for
4508 // the smallest part.
4509 SDValue Vec = N->getOperand(0);
4510 EVT VecVT = Vec.getValueType();
4511 Align SmallestAlign = DAG.getReducedAlign(VecVT, /*UseABI=*/false);
4513 DAG.CreateStackTemporary(VecVT.getStoreSize(), SmallestAlign);
4514 auto &MF = DAG.getMachineFunction();
4515 auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
4516 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
4517
4518 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
4519 SmallestAlign);
4520
4521 // Extract the subvector by loading the correct part.
4522 StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, SubVT, Idx);
4523
4524 return DAG.getLoad(
4525 SubVT, dl, Store, StackPtr,
4526 MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()));
4527}
4528
4529SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
4530 SDValue Vec = N->getOperand(0);
4531 SDValue Idx = N->getOperand(1);
4532 EVT VecVT = Vec.getValueType();
4533
4534 if (const ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Idx)) {
4535 uint64_t IdxVal = Index->getZExtValue();
4536
4537 SDValue Lo, Hi;
4538 GetSplitVector(Vec, Lo, Hi);
4539
4540 uint64_t LoElts = Lo.getValueType().getVectorMinNumElements();
4541
4542 if (IdxVal < LoElts)
4543 return SDValue(DAG.UpdateNodeOperands(N, Lo, Idx), 0);
4544 else if (!Vec.getValueType().isScalableVector())
4545 return SDValue(DAG.UpdateNodeOperands(N, Hi,
4546 DAG.getConstant(IdxVal - LoElts, SDLoc(N),
4547 Idx.getValueType())), 0);
4548 }
4549
4550 // See if the target wants to custom expand this node.
4551 if (CustomLowerNode(N, N->getValueType(0), true))
4552 return SDValue();
4553
4554 // Make the vector elements byte-addressable if they aren't already.
4555 SDLoc dl(N);
4556 EVT EltVT = VecVT.getVectorElementType();
4557 if (!EltVT.isByteSized()) {
4558 EltVT = EltVT.changeTypeToInteger().getRoundIntegerType(*DAG.getContext());
4559 VecVT = VecVT.changeElementType(*DAG.getContext(), EltVT);
4560 Vec = DAG.getNode(ISD::ANY_EXTEND, dl, VecVT, Vec);
4561 SDValue NewExtract =
4562 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Vec, Idx);
4563 return DAG.getAnyExtOrTrunc(NewExtract, dl, N->getValueType(0));
4564 }
4565
4566 // Store the vector to the stack.
4567 // In cases where the vector is illegal it will be broken down into parts
4568 // and stored in parts - we should use the alignment for the smallest part.
4569 Align SmallestAlign = DAG.getReducedAlign(VecVT, /*UseABI=*/false);
4571 DAG.CreateStackTemporary(VecVT.getStoreSize(), SmallestAlign);
4572 auto &MF = DAG.getMachineFunction();
4573 auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
4574 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
4575 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
4576 SmallestAlign);
4577
4578 // Load back the required element.
4579 StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
4580
4581 // EXTRACT_VECTOR_ELT can extend the element type to the width of the return
4582 // type, leaving the high bits undefined. But it can't truncate.
4583 assert(N->getValueType(0).bitsGE(EltVT) && "Illegal EXTRACT_VECTOR_ELT.");
4584
4585 return DAG.getExtLoad(
4586 ISD::EXTLOAD, dl, N->getValueType(0), Store, StackPtr,
4587 MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()), EltVT,
4588 commonAlignment(SmallestAlign, EltVT.getFixedSizeInBits() / 8));
4589}
4590
4591SDValue DAGTypeLegalizer::SplitVecOp_ExtVecInRegOp(SDNode *N) {
4592 SDValue Lo, Hi;
4593
4594 // *_EXTEND_VECTOR_INREG only reference the lower half of the input, so
4595 // splitting the result has the same effect as splitting the input operand.
4596 SplitVecRes_ExtVecInRegOp(N, Lo, Hi);
4597
4598 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), N->getValueType(0), Lo, Hi);
4599}
4600
4601SDValue DAGTypeLegalizer::SplitVecOp_Gather(MemSDNode *N, unsigned OpNo) {
4602 (void)OpNo;
4603 SDValue Lo, Hi;
4604 SplitVecRes_Gather(N, Lo, Hi);
4605
4606 SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, N, N->getValueType(0), Lo, Hi);
4607 ReplaceValueWith(SDValue(N, 0), Res);
4608 return SDValue();
4609}
4610
4611SDValue DAGTypeLegalizer::SplitVecOp_VP_STORE(VPStoreSDNode *N, unsigned OpNo) {
4612 assert(N->isUnindexed() && "Indexed vp_store of vector?");
4613 SDValue Ch = N->getChain();
4614 SDValue Ptr = N->getBasePtr();
4615 SDValue Offset = N->getOffset();
4616 assert(Offset.isUndef() && "Unexpected VP store offset");
4617 SDValue Mask = N->getMask();
4618 SDValue EVL = N->getVectorLength();
4619 SDValue Data = N->getValue();
4620 Align Alignment = N->getBaseAlign();
4621 SDLoc DL(N);
4622
4623 SDValue DataLo, DataHi;
4624 if (getTypeAction(Data.getValueType()) == TargetLowering::TypeSplitVector)
4625 // Split Data operand
4626 GetSplitVector(Data, DataLo, DataHi);
4627 else
4628 std::tie(DataLo, DataHi) = DAG.SplitVector(Data, DL);
4629
4630 // Split Mask operand
4631 SDValue MaskLo, MaskHi;
4632 if (OpNo == 1 && Mask.getOpcode() == ISD::SETCC) {
4633 SplitVecRes_SETCC(Mask.getNode(), MaskLo, MaskHi);
4634 } else {
4635 if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
4636 GetSplitVector(Mask, MaskLo, MaskHi);
4637 else
4638 std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, DL);
4639 }
4640
4641 EVT MemoryVT = N->getMemoryVT();
4642 EVT LoMemVT, HiMemVT;
4643 bool HiIsEmpty = false;
4644 std::tie(LoMemVT, HiMemVT) =
4645 DAG.GetDependentSplitDestVTs(MemoryVT, DataLo.getValueType(), &HiIsEmpty);
4646
4647 // Split EVL
4648 SDValue EVLLo, EVLHi;
4649 std::tie(EVLLo, EVLHi) = DAG.SplitEVL(EVL, Data.getValueType(), DL);
4650
4651 SDValue Lo, Hi;
4652 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4653 N->getPointerInfo(), MachineMemOperand::MOStore,
4655 MMOMetadata(N->getAAInfo(), N->getRanges()));
4656
4657 Lo = DAG.getStoreVP(Ch, DL, DataLo, Ptr, Offset, MaskLo, EVLLo, LoMemVT, MMO,
4658 N->getAddressingMode(), N->isTruncatingStore(),
4659 N->isCompressingStore());
4660
4661 // If the hi vp_store has zero storage size, only the lo vp_store is needed.
4662 if (HiIsEmpty)
4663 return Lo;
4664
4665 Ptr = TLI.IncrementMemoryAddress(Ptr, MaskLo, DL, LoMemVT, DAG,
4666 N->isCompressingStore());
4667
4668 MachinePointerInfo MPI;
4669 if (LoMemVT.isScalableVector()) {
4670 Alignment = commonAlignment(Alignment,
4671 LoMemVT.getSizeInBits().getKnownMinValue() / 8);
4672 MPI = MachinePointerInfo(N->getPointerInfo().getAddrSpace());
4673 } else
4674 MPI = N->getPointerInfo().getWithOffset(
4675 LoMemVT.getStoreSize().getFixedValue());
4676
4677 MMO = DAG.getMachineFunction().getMachineMemOperand(
4679 Alignment, MMOMetadata(N->getAAInfo(), N->getRanges()));
4680
4681 Hi = DAG.getStoreVP(Ch, DL, DataHi, Ptr, Offset, MaskHi, EVLHi, HiMemVT, MMO,
4682 N->getAddressingMode(), N->isTruncatingStore(),
4683 N->isCompressingStore());
4684
4685 // Build a factor node to remember that this store is independent of the
4686 // other one.
4687 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
4688}
4689
4690SDValue DAGTypeLegalizer::SplitVecOp_VP_STRIDED_STORE(VPStridedStoreSDNode *N,
4691 unsigned OpNo) {
4692 assert(N->isUnindexed() && "Indexed vp_strided_store of a vector?");
4693 assert(N->getOffset().isUndef() && "Unexpected VP strided store offset");
4694
4695 SDLoc DL(N);
4696
4697 SDValue Data = N->getValue();
4698 SDValue LoData, HiData;
4699 if (getTypeAction(Data.getValueType()) == TargetLowering::TypeSplitVector)
4700 GetSplitVector(Data, LoData, HiData);
4701 else
4702 std::tie(LoData, HiData) = DAG.SplitVector(Data, DL);
4703
4704 EVT LoMemVT, HiMemVT;
4705 bool HiIsEmpty = false;
4706 std::tie(LoMemVT, HiMemVT) = DAG.GetDependentSplitDestVTs(
4707 N->getMemoryVT(), LoData.getValueType(), &HiIsEmpty);
4708
4709 SDValue Mask = N->getMask();
4710 SDValue LoMask, HiMask;
4711 if (OpNo == 1 && Mask.getOpcode() == ISD::SETCC)
4712 SplitVecRes_SETCC(Mask.getNode(), LoMask, HiMask);
4713 else if (getTypeAction(Mask.getValueType()) ==
4715 GetSplitVector(Mask, LoMask, HiMask);
4716 else
4717 std::tie(LoMask, HiMask) = DAG.SplitVector(Mask, DL);
4718
4719 SDValue LoEVL, HiEVL;
4720 std::tie(LoEVL, HiEVL) =
4721 DAG.SplitEVL(N->getVectorLength(), Data.getValueType(), DL);
4722
4723 // Generate the low vp_strided_store
4724 SDValue Lo = DAG.getStridedStoreVP(
4725 N->getChain(), DL, LoData, N->getBasePtr(), N->getOffset(),
4726 N->getStride(), LoMask, LoEVL, LoMemVT, N->getMemOperand(),
4727 N->getAddressingMode(), N->isTruncatingStore(), N->isCompressingStore());
4728
4729 // If the high vp_strided_store has zero storage size, only the low
4730 // vp_strided_store is needed.
4731 if (HiIsEmpty)
4732 return Lo;
4733
4734 // Generate the high vp_strided_store.
4735 // To calculate the high base address, we need to sum to the low base
4736 // address stride number of bytes for each element already stored by low,
4737 // that is: Ptr = Ptr + (LoEVL * Stride)
4738 EVT PtrVT = N->getBasePtr().getValueType();
4740 DAG.getNode(ISD::MUL, DL, PtrVT, LoEVL,
4741 DAG.getSExtOrTrunc(N->getStride(), DL, PtrVT));
4742 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, N->getBasePtr(), Increment);
4743
4744 Align Alignment = N->getBaseAlign();
4745 if (LoMemVT.isScalableVector())
4746 Alignment = commonAlignment(Alignment,
4747 LoMemVT.getSizeInBits().getKnownMinValue() / 8);
4748
4749 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4750 MachinePointerInfo(N->getPointerInfo().getAddrSpace()),
4752 Alignment, MMOMetadata(N->getAAInfo(), N->getRanges()));
4753
4754 SDValue Hi = DAG.getStridedStoreVP(
4755 N->getChain(), DL, HiData, Ptr, N->getOffset(), N->getStride(), HiMask,
4756 HiEVL, HiMemVT, MMO, N->getAddressingMode(), N->isTruncatingStore(),
4757 N->isCompressingStore());
4758
4759 // Build a factor node to remember that this store is independent of the
4760 // other one.
4761 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
4762}
4763
4764SDValue DAGTypeLegalizer::SplitVecOp_MSTORE(MaskedStoreSDNode *N,
4765 unsigned OpNo) {
4766 assert(N->isUnindexed() && "Indexed masked store of vector?");
4767 SDValue Ch = N->getChain();
4768 SDValue Ptr = N->getBasePtr();
4769 SDValue Offset = N->getOffset();
4770 assert(Offset.isUndef() && "Unexpected indexed masked store offset");
4771 SDValue Mask = N->getMask();
4772 SDValue Data = N->getValue();
4773 Align Alignment = N->getBaseAlign();
4774 SDLoc DL(N);
4775
4776 SDValue DataLo, DataHi;
4777 if (getTypeAction(Data.getValueType()) == TargetLowering::TypeSplitVector)
4778 // Split Data operand
4779 GetSplitVector(Data, DataLo, DataHi);
4780 else
4781 std::tie(DataLo, DataHi) = DAG.SplitVector(Data, DL);
4782
4783 // Split Mask operand
4784 SDValue MaskLo, MaskHi;
4785 if (OpNo == 1 && Mask.getOpcode() == ISD::SETCC) {
4786 SplitVecRes_SETCC(Mask.getNode(), MaskLo, MaskHi);
4787 } else {
4788 if (getTypeAction(Mask.getValueType()) == TargetLowering::TypeSplitVector)
4789 GetSplitVector(Mask, MaskLo, MaskHi);
4790 else
4791 std::tie(MaskLo, MaskHi) = DAG.SplitVector(Mask, DL);
4792 }
4793
4794 EVT MemoryVT = N->getMemoryVT();
4795 EVT LoMemVT, HiMemVT;
4796 bool HiIsEmpty = false;
4797 std::tie(LoMemVT, HiMemVT) =
4798 DAG.GetDependentSplitDestVTs(MemoryVT, DataLo.getValueType(), &HiIsEmpty);
4799
4800 SDValue Lo, Hi, Res;
4801 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4802 N->getPointerInfo(), MachineMemOperand::MOStore,
4804 MMOMetadata(N->getAAInfo(), N->getRanges(), N->getMemCacheHint()));
4805
4806 Lo = DAG.getMaskedStore(Ch, DL, DataLo, Ptr, Offset, MaskLo, LoMemVT, MMO,
4807 N->getAddressingMode(), N->isTruncatingStore(),
4808 N->isCompressingStore());
4809
4810 if (HiIsEmpty) {
4811 // The hi masked store has zero storage size.
4812 // Only the lo masked store is needed.
4813 Res = Lo;
4814 } else {
4815
4816 Ptr = TLI.IncrementMemoryAddress(Ptr, MaskLo, DL, LoMemVT, DAG,
4817 N->isCompressingStore());
4818
4819 MachinePointerInfo MPI;
4820 if (LoMemVT.isScalableVector()) {
4822 Alignment, LoMemVT.getSizeInBits().getKnownMinValue() / 8);
4823 MPI = MachinePointerInfo(N->getPointerInfo().getAddrSpace());
4824 } else
4825 MPI = N->getPointerInfo().getWithOffset(
4826 LoMemVT.getStoreSize().getFixedValue());
4827
4828 MMO = DAG.getMachineFunction().getMachineMemOperand(
4830 Alignment,
4831 MMOMetadata(N->getAAInfo(), N->getRanges(), N->getMemCacheHint()));
4832
4833 Hi = DAG.getMaskedStore(Ch, DL, DataHi, Ptr, Offset, MaskHi, HiMemVT, MMO,
4834 N->getAddressingMode(), N->isTruncatingStore(),
4835 N->isCompressingStore());
4836
4837 // Build a factor node to remember that this store is independent of the
4838 // other one.
4839 Res = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
4840 }
4841
4842 return Res;
4843}
4844
4845SDValue DAGTypeLegalizer::SplitVecOp_Scatter(MemSDNode *N, unsigned OpNo) {
4846 SDValue Ch = N->getChain();
4847 SDValue Ptr = N->getBasePtr();
4848 EVT MemoryVT = N->getMemoryVT();
4849 Align Alignment = N->getBaseAlign();
4850 SDLoc DL(N);
4851 struct Operands {
4852 SDValue Mask;
4853 SDValue Index;
4854 SDValue Scale;
4855 SDValue Data;
4856 } Ops = [&]() -> Operands {
4857 if (auto *MSC = dyn_cast<MaskedScatterSDNode>(N)) {
4858 return {MSC->getMask(), MSC->getIndex(), MSC->getScale(),
4859 MSC->getValue()};
4860 }
4861 auto *VPSC = cast<VPScatterSDNode>(N);
4862 return {VPSC->getMask(), VPSC->getIndex(), VPSC->getScale(),
4863 VPSC->getValue()};
4864 }();
4865 // Split all operands
4866
4867 EVT LoMemVT, HiMemVT;
4868 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
4869
4870 SDValue DataLo, DataHi;
4871 if (getTypeAction(Ops.Data.getValueType()) == TargetLowering::TypeSplitVector)
4872 // Split Data operand
4873 GetSplitVector(Ops.Data, DataLo, DataHi);
4874 else
4875 std::tie(DataLo, DataHi) = DAG.SplitVector(Ops.Data, DL);
4876
4877 // Split Mask operand
4878 SDValue MaskLo, MaskHi;
4879 if (OpNo == 1 && Ops.Mask.getOpcode() == ISD::SETCC) {
4880 SplitVecRes_SETCC(Ops.Mask.getNode(), MaskLo, MaskHi);
4881 } else {
4882 std::tie(MaskLo, MaskHi) = SplitMask(Ops.Mask, DL);
4883 }
4884
4885 SDValue IndexHi, IndexLo;
4886 if (getTypeAction(Ops.Index.getValueType()) ==
4888 GetSplitVector(Ops.Index, IndexLo, IndexHi);
4889 else
4890 std::tie(IndexLo, IndexHi) = DAG.SplitVector(Ops.Index, DL);
4891
4892 SDValue Lo;
4893 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
4894 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4895 N->getPointerInfo(), MMOFlags, LocationSize::beforeOrAfterPointer(),
4896 Alignment, MMOMetadata(N->getAAInfo(), N->getRanges()));
4897
4898 if (auto *MSC = dyn_cast<MaskedScatterSDNode>(N)) {
4899 SDValue OpsLo[] = {Ch, DataLo, MaskLo, Ptr, IndexLo, Ops.Scale};
4900 Lo =
4901 DAG.getMaskedScatter(DAG.getVTList(MVT::Other), LoMemVT, DL, OpsLo, MMO,
4902 MSC->getIndexType(), MSC->isTruncatingStore());
4903
4904 // The order of the Scatter operation after split is well defined. The "Hi"
4905 // part comes after the "Lo". So these two operations should be chained one
4906 // after another.
4907 SDValue OpsHi[] = {Lo, DataHi, MaskHi, Ptr, IndexHi, Ops.Scale};
4908 return DAG.getMaskedScatter(DAG.getVTList(MVT::Other), HiMemVT, DL, OpsHi,
4909 MMO, MSC->getIndexType(),
4910 MSC->isTruncatingStore());
4911 }
4912 auto *VPSC = cast<VPScatterSDNode>(N);
4913 SDValue EVLLo, EVLHi;
4914 std::tie(EVLLo, EVLHi) =
4915 DAG.SplitEVL(VPSC->getVectorLength(), Ops.Data.getValueType(), DL);
4916
4917 SDValue OpsLo[] = {Ch, DataLo, Ptr, IndexLo, Ops.Scale, MaskLo, EVLLo};
4918 Lo = DAG.getScatterVP(DAG.getVTList(MVT::Other), LoMemVT, DL, OpsLo, MMO,
4919 VPSC->getIndexType());
4920
4921 // The order of the Scatter operation after split is well defined. The "Hi"
4922 // part comes after the "Lo". So these two operations should be chained one
4923 // after another.
4924 SDValue OpsHi[] = {Lo, DataHi, Ptr, IndexHi, Ops.Scale, MaskHi, EVLHi};
4925 return DAG.getScatterVP(DAG.getVTList(MVT::Other), HiMemVT, DL, OpsHi, MMO,
4926 VPSC->getIndexType());
4927}
4928
4929SDValue DAGTypeLegalizer::SplitVecOp_STORE(StoreSDNode *N, unsigned OpNo) {
4930 assert(N->isUnindexed() && "Indexed store of vector?");
4931 assert(OpNo == 1 && "Can only split the stored value");
4932 SDLoc DL(N);
4933
4934 bool isTruncating = N->isTruncatingStore();
4935 SDValue Ch = N->getChain();
4936 SDValue Ptr = N->getBasePtr();
4937 EVT MemoryVT = N->getMemoryVT();
4938 Align Alignment = N->getBaseAlign();
4939 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
4940 MMOMetadata Metadata = N->getMMOMetadataForSubAccess();
4941 SDValue Lo, Hi;
4942 GetSplitVector(N->getOperand(1), Lo, Hi);
4943
4944 EVT LoMemVT, HiMemVT;
4945 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemoryVT);
4946
4947 // Scalarize if the split halves are not byte-sized.
4948 if (!LoMemVT.isByteSized() || !HiMemVT.isByteSized())
4949 return TLI.scalarizeVectorStore(N, DAG);
4950
4951 if (isTruncating)
4952 Lo = DAG.getTruncStore(Ch, DL, Lo, Ptr, N->getPointerInfo(), LoMemVT,
4953 Alignment, MMOFlags, Metadata);
4954 else
4955 Lo = DAG.getStore(Ch, DL, Lo, Ptr, N->getPointerInfo(), Alignment, MMOFlags,
4956 Metadata);
4957
4958 MachinePointerInfo MPI;
4959 IncrementPointer(N, LoMemVT, MPI, Ptr);
4960
4961 if (isTruncating)
4962 Hi = DAG.getTruncStore(Ch, DL, Hi, Ptr, MPI, HiMemVT, Alignment, MMOFlags,
4963 Metadata);
4964 else
4965 Hi = DAG.getStore(Ch, DL, Hi, Ptr, MPI, Alignment, MMOFlags, Metadata);
4966
4967 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
4968}
4969
4970SDValue DAGTypeLegalizer::SplitVecOp_ATOMIC_STORE(AtomicSDNode *N) {
4971 SDLoc DL(N);
4972 LLVMContext &Ctx = *DAG.getContext();
4973 SDValue StVal = N->getVal();
4974 EVT VT = StVal.getValueType();
4975 EVT MemIntVT = EVT::getIntegerVT(Ctx, N->getMemoryVT().getSizeInBits());
4976
4977 // The store needs a single value spanning the full memory width. If the
4978 // value can be held in a legal vector register, keep it there and extract
4979 // the low integer element of the memory width. This lets the store be issued
4980 // directly from a vector register (e.g. a single MOVQ/MOVD) instead of
4981 // bitcasting the split vector straight to a scalar integer, which would
4982 // reassemble the value element by element in GPRs.
4983 //
4984 // Reinterpret the value as a same-shaped integer vector first: an FP element
4985 // type may not have a legal vector form (e.g. bfloat on SSE2) while the
4986 // integer-of-element-size form does. Ask the target which legal vector type
4987 // it widens to.
4988 EVT IntVecVT = VT.changeVectorElementTypeToInteger();
4989 EVT IntEltVT = IntVecVT.getVectorElementType();
4990 EVT WideVT = TLI.getLegalTypeToTransformTo(Ctx, IntVecVT);
4991 if (DAG.getDataLayout().isLittleEndian() && TLI.isTypeLegal(MemIntVT) &&
4992 WideVT.isVector() && WideVT.getVectorElementType() == IntEltVT &&
4993 IntEltVT.getSizeInBits() <= MemIntVT.getSizeInBits() &&
4994 WideVT.getSizeInBits() % MemIntVT.getSizeInBits() == 0) {
4995 SDValue Wide = ModifyToType(DAG.getBitcast(IntVecVT, StVal), WideVT);
4996 unsigned NumMemElts = WideVT.getSizeInBits() / MemIntVT.getSizeInBits();
4997 EVT MemVecVT = EVT::getVectorVT(Ctx, MemIntVT, NumMemElts);
4998 SDValue Elt = DAG.getExtractVectorElt(DL, MemIntVT,
4999 DAG.getBitcast(MemVecVT, Wide), 0);
5000 return DAG.getAtomic(ISD::ATOMIC_STORE, DL, MemIntVT, N->getChain(), Elt,
5001 N->getBasePtr(), N->getMemOperand());
5002 }
5003
5004 // Otherwise issue a single atomic store of an integer that spans the full
5005 // memory width. Bitcasting the (illegal) vector value to that integer lets
5006 // the type legalizer further legalize the BITCAST input as needed, while the
5007 // ATOMIC_STORE itself uses only the legal integer type.
5008 EVT IntVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits());
5009 SDValue AsInt = DAG.getBitcast(IntVT, StVal);
5010 return DAG.getAtomic(ISD::ATOMIC_STORE, DL, MemIntVT, N->getChain(), AsInt,
5011 N->getBasePtr(), N->getMemOperand());
5012}
5013
5014SDValue DAGTypeLegalizer::SplitVecOp_CONCAT_VECTORS(SDNode *N) {
5015 SDLoc DL(N);
5016
5017 // The input operands all must have the same type, and we know the result
5018 // type is valid. Convert this to a buildvector which extracts all the
5019 // input elements.
5020 // TODO: If the input elements are power-two vectors, we could convert this to
5021 // a new CONCAT_VECTORS node with elements that are half-wide.
5023 EVT EltVT = N->getValueType(0).getVectorElementType();
5024 for (const SDValue &Op : N->op_values()) {
5025 for (unsigned i = 0, e = Op.getValueType().getVectorNumElements();
5026 i != e; ++i) {
5027 Elts.push_back(DAG.getExtractVectorElt(DL, EltVT, Op, i));
5028 }
5029 }
5030
5031 return DAG.getBuildVector(N->getValueType(0), DL, Elts);
5032}
5033
5034SDValue DAGTypeLegalizer::SplitVecOp_TruncateHelper(SDNode *N) {
5035 // The result type is legal, but the input type is illegal. If splitting
5036 // ends up with the result type of each half still being legal, just
5037 // do that. If, however, that would result in an illegal result type,
5038 // we can try to get more clever with power-two vectors. Specifically,
5039 // split the input type, but also widen the result element size, then
5040 // concatenate the halves and truncate again. For example, consider a target
5041 // where v8i8 is legal and v8i32 is not (ARM, which doesn't have 256-bit
5042 // vectors). To perform a "%res = v8i8 trunc v8i32 %in" we do:
5043 // %inlo = v4i32 extract_subvector %in, 0
5044 // %inhi = v4i32 extract_subvector %in, 4
5045 // %lo16 = v4i16 trunc v4i32 %inlo
5046 // %hi16 = v4i16 trunc v4i32 %inhi
5047 // %in16 = v8i16 concat_vectors v4i16 %lo16, v4i16 %hi16
5048 // %res = v8i8 trunc v8i16 %in16
5049 //
5050 // Without this transform, the original truncate would end up being
5051 // scalarized, which is pretty much always a last resort.
5052 unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0;
5053 SDValue InVec = N->getOperand(OpNo);
5054 EVT InVT = InVec->getValueType(0);
5055 EVT OutVT = N->getValueType(0);
5056 ElementCount NumElements = OutVT.getVectorElementCount();
5057 bool IsFloat = OutVT.isFloatingPoint();
5058
5059 unsigned InElementSize = InVT.getScalarSizeInBits();
5060 unsigned OutElementSize = OutVT.getScalarSizeInBits();
5061
5062 // Determine the split output VT. If its legal we can just split dirctly.
5063 EVT LoOutVT, HiOutVT;
5064 std::tie(LoOutVT, HiOutVT) = DAG.GetSplitDestVTs(OutVT);
5065 assert(LoOutVT == HiOutVT && "Unequal split?");
5066
5067 // If the input elements are only 1/2 the width of the result elements,
5068 // just use the normal splitting. Our trick only work if there's room
5069 // to split more than once.
5070 if (isTypeLegal(LoOutVT) || InElementSize <= OutElementSize * 2 ||
5071 (IsFloat && !isPowerOf2_32(InElementSize)))
5072 return SplitVecOp_UnaryOp(N);
5073 SDLoc DL(N);
5074
5075 // Don't touch if this will be scalarized.
5076 EVT FinalVT = InVT;
5077 while (getTypeAction(FinalVT) == TargetLowering::TypeSplitVector)
5078 FinalVT = FinalVT.getHalfNumVectorElementsVT(*DAG.getContext());
5079
5080 if (getTypeAction(FinalVT) == TargetLowering::TypeScalarizeVector)
5081 return SplitVecOp_UnaryOp(N);
5082
5083 // Get the split input vector.
5084 SDValue InLoVec, InHiVec;
5085 GetSplitVector(InVec, InLoVec, InHiVec);
5086
5087 // Truncate them to 1/2 the element size.
5088 //
5089 // This assumes the number of elements is a power of two; any vector that
5090 // isn't should be widened, not split.
5091 EVT HalfElementVT = IsFloat ?
5092 EVT::getFloatingPointVT(InElementSize/2) :
5093 EVT::getIntegerVT(*DAG.getContext(), InElementSize/2);
5094 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), HalfElementVT,
5095 NumElements.divideCoefficientBy(2));
5096
5097 SDValue HalfLo;
5098 SDValue HalfHi;
5099 SDValue Chain;
5100 if (N->isStrictFPOpcode()) {
5101 HalfLo = DAG.getNode(N->getOpcode(), DL, {HalfVT, MVT::Other},
5102 {N->getOperand(0), InLoVec});
5103 HalfHi = DAG.getNode(N->getOpcode(), DL, {HalfVT, MVT::Other},
5104 {N->getOperand(0), InHiVec});
5105 // Legalize the chain result - switch anything that used the old chain to
5106 // use the new one.
5107 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, HalfLo.getValue(1),
5108 HalfHi.getValue(1));
5109 } else {
5110 HalfLo = DAG.getNode(N->getOpcode(), DL, HalfVT, InLoVec);
5111 HalfHi = DAG.getNode(N->getOpcode(), DL, HalfVT, InHiVec);
5112 }
5113
5114 // Concatenate them to get the full intermediate truncation result.
5115 EVT InterVT = EVT::getVectorVT(*DAG.getContext(), HalfElementVT, NumElements);
5116 SDValue InterVec = DAG.getNode(ISD::CONCAT_VECTORS, DL, InterVT, HalfLo,
5117 HalfHi);
5118 // Now finish up by truncating all the way down to the original result
5119 // type. This should normally be something that ends up being legal directly,
5120 // but in theory if a target has very wide vectors and an annoyingly
5121 // restricted set of legal types, this split can chain to build things up.
5122
5123 if (N->isStrictFPOpcode()) {
5124 SDValue Res = DAG.getNode(
5125 ISD::STRICT_FP_ROUND, DL, {OutVT, MVT::Other},
5126 {Chain, InterVec,
5127 DAG.getTargetConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()))});
5128 // Relink the chain
5129 ReplaceValueWith(SDValue(N, 1), SDValue(Res.getNode(), 1));
5130 return Res;
5131 }
5132
5133 return IsFloat
5134 ? DAG.getNode(ISD::FP_ROUND, DL, OutVT, InterVec,
5135 DAG.getTargetConstant(
5136 0, DL, TLI.getPointerTy(DAG.getDataLayout())))
5137 : DAG.getNode(ISD::TRUNCATE, DL, OutVT, InterVec);
5138}
5139
5140SDValue DAGTypeLegalizer::SplitVecOp_VSETCC(SDNode *N) {
5141 unsigned Opc = N->getOpcode();
5142 bool isStrict = Opc == ISD::STRICT_FSETCC || Opc == ISD::STRICT_FSETCCS;
5143 assert(N->getValueType(0).isVector() &&
5144 N->getOperand(isStrict ? 1 : 0).getValueType().isVector() &&
5145 "Operand types must be vectors");
5146 // The result has a legal vector type, but the input needs splitting.
5147 SDValue Lo0, Hi0, Lo1, Hi1, LoRes, HiRes;
5148 SDLoc DL(N);
5149 GetSplitVector(N->getOperand(isStrict ? 1 : 0), Lo0, Hi0);
5150 GetSplitVector(N->getOperand(isStrict ? 2 : 1), Lo1, Hi1);
5151
5152 EVT VT = N->getValueType(0);
5153 EVT PartResVT = getSetCCResultType(Lo0.getValueType());
5154
5155 if (Opc == ISD::SETCC) {
5156 LoRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Lo0, Lo1, N->getOperand(2));
5157 HiRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Hi0, Hi1, N->getOperand(2));
5158 } else {
5159 assert(isStrict && "unexpected node");
5160 LoRes = DAG.getNode(Opc, DL, DAG.getVTList(PartResVT, N->getValueType(1)),
5161 N->getOperand(0), Lo0, Lo1, N->getOperand(3));
5162 HiRes = DAG.getNode(Opc, DL, DAG.getVTList(PartResVT, N->getValueType(1)),
5163 N->getOperand(0), Hi0, Hi1, N->getOperand(3));
5164 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5165 LoRes.getValue(1), HiRes.getValue(1));
5166 ReplaceValueWith(SDValue(N, 1), NewChain);
5167 }
5168
5169 EVT ConcatVT = PartResVT.getDoubleNumVectorElementsVT(*DAG.getContext());
5170 SDValue Con = DAG.getNode(ISD::CONCAT_VECTORS, DL, ConcatVT, LoRes, HiRes);
5171 if (VT == ConcatVT)
5172 return Con;
5173
5174 EVT OpVT = N->getOperand(0).getValueType();
5175 ISD::NodeType ExtendCode =
5176 TargetLowering::getExtendForContent(TLI.getBooleanContents(OpVT));
5177 return DAG.getExtOrTrunc(Con, DL, VT, ExtendCode);
5178}
5179
5180
5181SDValue DAGTypeLegalizer::SplitVecOp_FP_ROUND(SDNode *N) {
5182 // The result has a legal vector type, but the input needs splitting.
5183 EVT ResVT = N->getValueType(0);
5184 SDValue Lo, Hi;
5185 SDLoc DL(N);
5186 GetSplitVector(N->getOperand(N->isStrictFPOpcode() ? 1 : 0), Lo, Hi);
5187 EVT InVT = Lo.getValueType();
5188
5189 EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
5190 InVT.getVectorElementCount());
5191
5192 if (N->isStrictFPOpcode()) {
5193 Lo = DAG.getNode(N->getOpcode(), DL, {OutVT, MVT::Other},
5194 {N->getOperand(0), Lo, N->getOperand(2)});
5195 Hi = DAG.getNode(N->getOpcode(), DL, {OutVT, MVT::Other},
5196 {N->getOperand(0), Hi, N->getOperand(2)});
5197 // Legalize the chain result - switch anything that used the old chain to
5198 // use the new one.
5199 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5200 Lo.getValue(1), Hi.getValue(1));
5201 ReplaceValueWith(SDValue(N, 1), NewChain);
5202 } else if (N->getOpcode() == ISD::CONVERT_TO_ARBITRARY_FP) {
5203 Lo = DAG.getNode(N->getOpcode(), DL, OutVT, Lo, N->getOperand(1),
5204 N->getOperand(2), N->getOperand(3));
5205 Hi = DAG.getNode(N->getOpcode(), DL, OutVT, Hi, N->getOperand(1),
5206 N->getOperand(2), N->getOperand(3));
5207 } else {
5208 Lo = DAG.getNode(N->getOpcode(), DL, OutVT, Lo, N->getOperand(1));
5209 Hi = DAG.getNode(N->getOpcode(), DL, OutVT, Hi, N->getOperand(1));
5210 }
5211
5212 return DAG.getNode(ISD::CONCAT_VECTORS, DL, ResVT, Lo, Hi);
5213}
5214
5215// Split a vector type in an FP binary operation where the second operand has a
5216// different type from the first.
5217//
5218// The result (and the first input) has a legal vector type, but the second
5219// input needs splitting.
5220SDValue DAGTypeLegalizer::SplitVecOp_FPOpDifferentTypes(SDNode *N) {
5221 SDLoc DL(N);
5222
5223 EVT LHSLoVT, LHSHiVT;
5224 std::tie(LHSLoVT, LHSHiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
5225
5226 if (!isTypeLegal(LHSLoVT) || !isTypeLegal(LHSHiVT))
5227 return DAG.UnrollVectorOp(N, N->getValueType(0).getVectorNumElements());
5228
5229 SDValue LHSLo, LHSHi;
5230 std::tie(LHSLo, LHSHi) =
5231 DAG.SplitVector(N->getOperand(0), DL, LHSLoVT, LHSHiVT);
5232
5233 SDValue RHSLo, RHSHi;
5234 std::tie(RHSLo, RHSHi) = DAG.SplitVector(N->getOperand(1), DL);
5235
5236 SDValue Lo = DAG.getNode(N->getOpcode(), DL, LHSLoVT, LHSLo, RHSLo);
5237 SDValue Hi = DAG.getNode(N->getOpcode(), DL, LHSHiVT, LHSHi, RHSHi);
5238
5239 return DAG.getNode(ISD::CONCAT_VECTORS, DL, N->getValueType(0), Lo, Hi);
5240}
5241
5242SDValue DAGTypeLegalizer::SplitVecOp_CMP(SDNode *N) {
5243 LLVMContext &Ctxt = *DAG.getContext();
5244 SDLoc dl(N);
5245
5246 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
5247 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
5248 GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
5249
5250 EVT ResVT = N->getValueType(0);
5251 ElementCount SplitOpEC = LHSLo.getValueType().getVectorElementCount();
5252 EVT NewResVT =
5253 EVT::getVectorVT(Ctxt, ResVT.getVectorElementType(), SplitOpEC);
5254
5255 SDValue Lo = DAG.getNode(N->getOpcode(), dl, NewResVT, LHSLo, RHSLo);
5256 SDValue Hi = DAG.getNode(N->getOpcode(), dl, NewResVT, LHSHi, RHSHi);
5257
5258 return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi);
5259}
5260
5261SDValue DAGTypeLegalizer::SplitVecOp_FP_TO_XINT_SAT(SDNode *N) {
5262 EVT ResVT = N->getValueType(0);
5263 SDValue Lo, Hi;
5264 SDLoc dl(N);
5265 GetSplitVector(N->getOperand(0), Lo, Hi);
5266 EVT InVT = Lo.getValueType();
5267
5268 EVT NewResVT =
5269 EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
5270 InVT.getVectorElementCount());
5271
5272 Lo = DAG.getNode(N->getOpcode(), dl, NewResVT, Lo, N->getOperand(1));
5273 Hi = DAG.getNode(N->getOpcode(), dl, NewResVT, Hi, N->getOperand(1));
5274
5275 return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi);
5276}
5277
5278SDValue DAGTypeLegalizer::SplitVecOp_CttzElts(SDNode *N) {
5279 SDLoc DL(N);
5280 EVT ResVT = N->getValueType(0);
5281
5282 SDValue Lo, Hi;
5283 SDValue VecOp = N->getOperand(0);
5284 GetSplitVector(VecOp, Lo, Hi);
5285
5286 // if CTTZ_ELTS(Lo) != VL => CTTZ_ELTS(Lo).
5287 // else => VL + (CTTZ_ELTS(Hi) or CTTZ_ELTS_ZERO_POISON(Hi)).
5288 SDValue ResLo = DAG.getNode(ISD::CTTZ_ELTS, DL, ResVT, Lo);
5289 SDValue VL =
5290 DAG.getElementCount(DL, ResVT, Lo.getValueType().getVectorElementCount());
5291 SDValue ResLoNotVL =
5292 DAG.getSetCC(DL, getSetCCResultType(ResVT), ResLo, VL, ISD::SETNE);
5293 SDValue ResHi = DAG.getNode(N->getOpcode(), DL, ResVT, Hi);
5294 return DAG.getSelect(DL, ResVT, ResLoNotVL, ResLo,
5295 DAG.getNode(ISD::ADD, DL, ResVT, VL, ResHi));
5296}
5297
5298SDValue DAGTypeLegalizer::SplitVecOp_VP_CttzElements(SDNode *N) {
5299 SDLoc DL(N);
5300 EVT ResVT = N->getValueType(0);
5301
5302 SDValue Lo, Hi;
5303 SDValue VecOp = N->getOperand(0);
5304 GetSplitVector(VecOp, Lo, Hi);
5305
5306 auto [MaskLo, MaskHi] = SplitMask(N->getOperand(1));
5307 auto [EVLLo, EVLHi] =
5308 DAG.SplitEVL(N->getOperand(2), VecOp.getValueType(), DL);
5309 SDValue VLo = DAG.getZExtOrTrunc(EVLLo, DL, ResVT);
5310
5311 // if VP_CTTZ_ELTS(Lo) != EVLLo => VP_CTTZ_ELTS(Lo).
5312 // else => EVLLo + (VP_CTTZ_ELTS(Hi) or VP_CTTZ_ELTS_ZERO_POISON(Hi)).
5313 SDValue ResLo = DAG.getNode(ISD::VP_CTTZ_ELTS, DL, ResVT, Lo, MaskLo, EVLLo);
5314 SDValue ResLoNotEVL =
5315 DAG.getSetCC(DL, getSetCCResultType(ResVT), ResLo, VLo, ISD::SETNE);
5316 SDValue ResHi = DAG.getNode(N->getOpcode(), DL, ResVT, Hi, MaskHi, EVLHi);
5317 return DAG.getSelect(DL, ResVT, ResLoNotEVL, ResLo,
5318 DAG.getNode(ISD::ADD, DL, ResVT, VLo, ResHi));
5319}
5320
5321SDValue DAGTypeLegalizer::SplitVecOp_VECTOR_HISTOGRAM(SDNode *N) {
5322 MaskedHistogramSDNode *HG = cast<MaskedHistogramSDNode>(N);
5323 SDLoc DL(HG);
5324 SDValue Inc = HG->getInc();
5325 SDValue Ptr = HG->getBasePtr();
5326 SDValue Scale = HG->getScale();
5327 SDValue IntID = HG->getIntID();
5328 EVT MemVT = HG->getMemoryVT();
5329 MachineMemOperand *MMO = HG->getMemOperand();
5330 ISD::MemIndexType IndexType = HG->getIndexType();
5331
5332 SDValue IndexLo, IndexHi, MaskLo, MaskHi;
5333 std::tie(IndexLo, IndexHi) = DAG.SplitVector(HG->getIndex(), DL);
5334 std::tie(MaskLo, MaskHi) = DAG.SplitVector(HG->getMask(), DL);
5335 SDValue OpsLo[] = {HG->getChain(), Inc, MaskLo, Ptr, IndexLo, Scale, IntID};
5336 SDValue Lo = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), MemVT, DL,
5337 OpsLo, MMO, IndexType);
5338 SDValue OpsHi[] = {Lo, Inc, MaskHi, Ptr, IndexHi, Scale, IntID};
5339 return DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), MemVT, DL, OpsHi,
5340 MMO, IndexType);
5341}
5342
5343SDValue DAGTypeLegalizer::SplitVecOp_VECTOR_MATCH(SDNode *N, unsigned OpNo) {
5344 SDLoc DL(N);
5345
5346 if (OpNo == 0) {
5347 EVT LoResVT, HiResVT;
5348 std::tie(LoResVT, HiResVT) = DAG.GetSplitDestVTs(N->getValueType(0));
5349 SDValue SourceLo, SourceHi;
5350 std::tie(SourceLo, SourceHi) = DAG.SplitVectorOperand(N, 0);
5351 SDValue MaskLo, MaskHi;
5352 std::tie(MaskLo, MaskHi) = DAG.SplitVectorOperand(N, 2);
5353
5354 SDValue MatchLo = DAG.getNode(ISD::VECTOR_MATCH, DL, LoResVT, SourceLo,
5355 N->getOperand(1), MaskLo, N->getFlags());
5356 SDValue MatchHi = DAG.getNode(ISD::VECTOR_MATCH, DL, HiResVT, SourceHi,
5357 N->getOperand(1), MaskHi, N->getFlags());
5358 return DAG.getNode(ISD::CONCAT_VECTORS, DL, N->getValueType(0), MatchLo,
5359 MatchHi);
5360 }
5361
5362 // Note: The Mask (OpNo == 2) should be widened with the result.
5363 assert(OpNo == 1 && "Unexpected VECTOR_MATCH operand");
5364
5365 SDValue NeedleLo, NeedleHi;
5366 GetSplitVector(N->getOperand(1), NeedleLo, NeedleHi);
5367
5368 SDValue MatchLo =
5369 DAG.getNode(ISD::VECTOR_MATCH, DL, N->getValueType(0), N->getOperand(0),
5370 NeedleLo, N->getOperand(2), N->getFlags());
5371 SDValue MatchHi =
5372 DAG.getNode(ISD::VECTOR_MATCH, DL, N->getValueType(0), N->getOperand(0),
5373 NeedleHi, N->getOperand(2), N->getFlags());
5374 return DAG.getNode(ISD::OR, DL, N->getValueType(0), MatchLo, MatchHi);
5375}
5376
5377SDValue DAGTypeLegalizer::SplitVecOp_PARTIAL_REDUCE_MLA(SDNode *N) {
5378 SDValue Acc = N->getOperand(0);
5379 assert(getTypeAction(Acc.getValueType()) != TargetLowering::TypeSplitVector &&
5380 "Accumulator should already be a legal type, and shouldn't need "
5381 "further splitting");
5382
5383 SDLoc DL(N);
5384 SDValue Input1Lo, Input1Hi, Input2Lo, Input2Hi;
5385 GetSplitVector(N->getOperand(1), Input1Lo, Input1Hi);
5386 GetSplitVector(N->getOperand(2), Input2Lo, Input2Hi);
5387 unsigned Opcode = N->getOpcode();
5388 EVT ResultVT = Acc.getValueType();
5389
5390 SDValue Lo = DAG.getNode(Opcode, DL, ResultVT, Acc, Input1Lo, Input2Lo);
5391 return DAG.getNode(Opcode, DL, ResultVT, Lo, Input1Hi, Input2Hi);
5392}
5393
5394//===----------------------------------------------------------------------===//
5395// Result Vector Widening
5396//===----------------------------------------------------------------------===//
5397
5398void DAGTypeLegalizer::ReplaceOtherWidenResults(SDNode *N, SDNode *WidenNode,
5399 unsigned WidenResNo) {
5400 unsigned NumResults = N->getNumValues();
5401 for (unsigned ResNo = 0; ResNo < NumResults; ResNo++) {
5402 if (ResNo == WidenResNo)
5403 continue;
5404 EVT ResVT = N->getValueType(ResNo);
5405 if (getTypeAction(ResVT) == TargetLowering::TypeWidenVector) {
5406 SetWidenedVector(SDValue(N, ResNo), SDValue(WidenNode, ResNo));
5407 } else {
5408 SDLoc DL(N);
5409 SDValue ResVal =
5410 DAG.getExtractSubvector(DL, ResVT, SDValue(WidenNode, ResNo), 0);
5411 ReplaceValueWith(SDValue(N, ResNo), ResVal);
5412 }
5413 }
5414}
5415
5416void DAGTypeLegalizer::WidenVectorResult(SDNode *N, unsigned ResNo) {
5417 LLVM_DEBUG(dbgs() << "Widen node result " << ResNo << ": "; N->dump(&DAG));
5418
5419 // See if the target wants to custom widen this node.
5420 if (CustomWidenLowerNode(N, N->getValueType(ResNo)))
5421 return;
5422
5423 SDValue Res = SDValue();
5424
5425 auto unrollExpandedOp = [&]() {
5426 // We're going to widen this vector op to a legal type by padding with undef
5427 // elements. If the wide vector op is eventually going to be expanded to
5428 // scalar libcalls, then unroll into scalar ops now to avoid unnecessary
5429 // libcalls on the undef elements.
5430
5431 // The lane count comes from the result being widened, but operation
5432 // legality is queried using the corresponding result 0 type.
5433 EVT ResVT = N->getValueType(ResNo);
5434 EVT WideResVT = TLI.getTypeToTransformTo(*DAG.getContext(), ResVT);
5435 if (WideResVT.isScalableVector())
5436 return false;
5437
5438 EVT VT0 = N->getValueType(0);
5439 EVT WideVecVT = VT0.changeVectorElementCount(
5440 *DAG.getContext(), WideResVT.getVectorElementCount());
5441
5442 if (!TLI.isOperationLegalOrCustomOrPromote(N->getOpcode(), WideVecVT) &&
5443 TLI.isOperationExpandOrLibCall(N->getOpcode(), VT0.getScalarType())) {
5444 SDValue Unrolled =
5445 DAG.UnrollVectorOp(N, WideVecVT.getVectorNumElements());
5446 Res = Unrolled.getValue(ResNo);
5447 if (N->getNumValues() > 1)
5448 ReplaceOtherWidenResults(N, Unrolled.getNode(), ResNo);
5449 return true;
5450 }
5451 return false;
5452 };
5453
5454 switch (N->getOpcode()) {
5455 default:
5456#ifndef NDEBUG
5457 dbgs() << "WidenVectorResult #" << ResNo << ": ";
5458 N->dump(&DAG);
5459 dbgs() << "\n";
5460#endif
5461 report_fatal_error("do not know how to widen the result of this operator!");
5462
5465 Res = WidenVecRes_LOOP_DEPENDENCE_MASK(N);
5466 break;
5467 case ISD::MERGE_VALUES: Res = WidenVecRes_MERGE_VALUES(N, ResNo); break;
5468 case ISD::ADDRSPACECAST:
5469 Res = WidenVecRes_ADDRSPACECAST(N);
5470 break;
5471 case ISD::AssertZext: Res = WidenVecRes_AssertZext(N); break;
5472 case ISD::BITCAST: Res = WidenVecRes_BITCAST(N); break;
5473 case ISD::BUILD_VECTOR: Res = WidenVecRes_BUILD_VECTOR(N); break;
5474 case ISD::CONCAT_VECTORS: Res = WidenVecRes_CONCAT_VECTORS(N); break;
5476 Res = WidenVecRes_INSERT_SUBVECTOR(N);
5477 break;
5478 case ISD::EXTRACT_SUBVECTOR: Res = WidenVecRes_EXTRACT_SUBVECTOR(N); break;
5479 case ISD::INSERT_VECTOR_ELT: Res = WidenVecRes_INSERT_VECTOR_ELT(N); break;
5480 case ISD::ATOMIC_LOAD:
5481 Res = WidenVecRes_ATOMIC_LOAD(cast<AtomicSDNode>(N));
5482 break;
5483 case ISD::LOAD: Res = WidenVecRes_LOAD(N); break;
5484 case ISD::STEP_VECTOR:
5485 case ISD::SPLAT_VECTOR:
5487 Res = WidenVecRes_ScalarOp(N);
5488 break;
5489 case ISD::SIGN_EXTEND_INREG: Res = WidenVecRes_InregOp(N); break;
5490 case ISD::VSELECT:
5491 case ISD::SELECT:
5492 case ISD::VP_MERGE:
5493 Res = WidenVecRes_Select(N);
5494 break;
5495 case ISD::SELECT_CC: Res = WidenVecRes_SELECT_CC(N); break;
5496 case ISD::SETCC: Res = WidenVecRes_SETCC(N); break;
5497 case ISD::POISON:
5498 case ISD::UNDEF: Res = WidenVecRes_UNDEF(N); break;
5500 Res = WidenVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N));
5501 break;
5502 case ISD::VP_LOAD:
5503 Res = WidenVecRes_VP_LOAD(cast<VPLoadSDNode>(N));
5504 break;
5505 case ISD::VP_LOAD_FF:
5506 Res = WidenVecRes_VP_LOAD_FF(cast<VPLoadFFSDNode>(N));
5507 break;
5508 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
5509 Res = WidenVecRes_VP_STRIDED_LOAD(cast<VPStridedLoadSDNode>(N));
5510 break;
5512 Res = WidenVecRes_VECTOR_COMPRESS(N);
5513 break;
5515 Res = WidenVecRes_VECTOR_SHUFFLE_VAR(N);
5516 break;
5517 case ISD::MLOAD:
5518 Res = WidenVecRes_MLOAD(cast<MaskedLoadSDNode>(N));
5519 break;
5520 case ISD::MGATHER:
5521 Res = WidenVecRes_MGATHER(cast<MaskedGatherSDNode>(N));
5522 break;
5523 case ISD::VP_GATHER:
5524 Res = WidenVecRes_VP_GATHER(cast<VPGatherSDNode>(N));
5525 break;
5527 Res = WidenVecRes_VECTOR_REVERSE(N);
5528 break;
5530 Res = WidenVecRes_GET_ACTIVE_LANE_MASK(N);
5531 break;
5533 WidenVecRes_VECTOR_INTERLEAVE(N);
5534 break;
5535 case ISD::VECTOR_MATCH:
5536 Res = WidenVecRes_VECTOR_MATCH(N);
5537 break;
5539 WidenVecRes_VECTOR_DEINTERLEAVE(N);
5540 break;
5541
5542 case ISD::ADD:
5543 case ISD::AND:
5544 case ISD::MUL:
5545 case ISD::MULHS:
5546 case ISD::MULHU:
5547 case ISD::ABDS:
5548 case ISD::ABDU:
5549 case ISD::OR:
5550 case ISD::SUB:
5551 case ISD::XOR:
5552 case ISD::SHL:
5553 case ISD::SRA:
5554 case ISD::SRL:
5555 case ISD::CLMUL:
5556 case ISD::CLMULR:
5557 case ISD::CLMULH:
5558 case ISD::PEXT:
5559 case ISD::PDEP:
5560 case ISD::FMINNUM:
5561 case ISD::FMINNUM_IEEE:
5562 case ISD::FMAXNUM:
5563 case ISD::FMAXNUM_IEEE:
5564 case ISD::FMINIMUM:
5565 case ISD::FMAXIMUM:
5566 case ISD::FMINIMUMNUM:
5567 case ISD::FMAXIMUMNUM:
5568 case ISD::SMIN:
5569 case ISD::SMAX:
5570 case ISD::UMIN:
5571 case ISD::UMAX:
5572 case ISD::UADDSAT:
5573 case ISD::SADDSAT:
5574 case ISD::USUBSAT:
5575 case ISD::SSUBSAT:
5576 case ISD::SSHLSAT:
5577 case ISD::USHLSAT:
5578 case ISD::ROTL:
5579 case ISD::ROTR:
5580 case ISD::AVGFLOORS:
5581 case ISD::AVGFLOORU:
5582 case ISD::AVGCEILS:
5583 case ISD::AVGCEILU:
5584 case ISD::AVGSUBFLOORS:
5585 case ISD::AVGSUBFLOORU:
5586 // Vector-predicated binary op widening. Note that -- unlike the
5587 // unpredicated versions -- we don't have to worry about trapping on
5588 // operations like UDIV, FADD, etc., as we pass on the original vector
5589 // length parameter. This means the widened elements containing garbage
5590 // aren't active.
5591 case ISD::VP_SDIV:
5592 case ISD::VP_UDIV:
5593 case ISD::VP_SREM:
5594 case ISD::VP_UREM:
5595 Res = WidenVecRes_Binary(N);
5596 break;
5597
5598 case ISD::MASKED_UDIV:
5599 case ISD::MASKED_SDIV:
5600 case ISD::MASKED_UREM:
5601 case ISD::MASKED_SREM:
5602 Res = WidenVecRes_MaskedBinary(N);
5603 break;
5604
5605 case ISD::SCMP:
5606 case ISD::UCMP:
5607 Res = WidenVecRes_CMP(N);
5608 break;
5609
5610 case ISD::FPOW:
5611 case ISD::FATAN2:
5612 case ISD::FREM:
5613 if (unrollExpandedOp())
5614 break;
5615 // If the target has custom/legal support for the scalar FP intrinsic ops
5616 // (they are probably not destined to become libcalls), then widen those
5617 // like any other binary ops.
5618 [[fallthrough]];
5619
5620 case ISD::FADD:
5621 case ISD::FMUL:
5622 case ISD::FSUB:
5623 case ISD::FDIV:
5624 case ISD::SDIV:
5625 case ISD::UDIV:
5626 case ISD::SREM:
5627 case ISD::UREM:
5628 Res = WidenVecRes_BinaryCanTrap(N);
5629 break;
5630
5631 case ISD::SMULFIX:
5632 case ISD::SMULFIXSAT:
5633 case ISD::UMULFIX:
5634 case ISD::UMULFIXSAT:
5635 // These are binary operations, but with an extra operand that shouldn't
5636 // be widened (the scale).
5637 Res = WidenVecRes_BinaryWithExtraScalarOp(N);
5638 break;
5639
5640#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
5641 case ISD::STRICT_##DAGN:
5642#include "llvm/IR/ConstrainedOps.def"
5643 Res = WidenVecRes_StrictFP(N);
5644 break;
5645
5646 case ISD::UADDO:
5647 case ISD::SADDO:
5648 case ISD::USUBO:
5649 case ISD::SSUBO:
5650 case ISD::UMULO:
5651 case ISD::SMULO:
5652 Res = WidenVecRes_OverflowOp(N, ResNo);
5653 break;
5654
5655 case ISD::FCOPYSIGN:
5656 Res = WidenVecRes_FCOPYSIGN(N);
5657 break;
5658
5659 case ISD::IS_FPCLASS:
5660 case ISD::FPTRUNC_ROUND:
5661 Res = WidenVecRes_UnarySameEltsWithScalarArg(N);
5662 break;
5663
5664 case ISD::FLDEXP:
5665 case ISD::FPOWI:
5666 if (!unrollExpandedOp())
5667 Res = WidenVecRes_ExpOp(N);
5668 break;
5669
5673 Res = WidenVecRes_EXTEND_VECTOR_INREG(N);
5674 break;
5675
5676 case ISD::ANY_EXTEND:
5677 case ISD::FP_EXTEND:
5678 case ISD::FP_ROUND:
5679 case ISD::FP_TO_SINT:
5680 case ISD::FP_TO_UINT:
5681 case ISD::SIGN_EXTEND:
5682 case ISD::SINT_TO_FP:
5683 case ISD::TRUNCATE:
5684 case ISD::UINT_TO_FP:
5685 case ISD::ZERO_EXTEND:
5688 Res = WidenVecRes_Convert(N);
5689 break;
5690
5693 Res = WidenVecRes_FP_TO_XINT_SAT(N);
5694 break;
5695
5696 case ISD::LRINT:
5697 case ISD::LLRINT:
5698 case ISD::LROUND:
5699 case ISD::LLROUND:
5700 Res = WidenVecRes_XROUND(N);
5701 break;
5702
5703 case ISD::FACOS:
5704 case ISD::FASIN:
5705 case ISD::FATAN:
5706 case ISD::FCEIL:
5707 case ISD::FCOS:
5708 case ISD::FCOSH:
5709 case ISD::FEXP:
5710 case ISD::FEXP2:
5711 case ISD::FEXP10:
5712 case ISD::FFLOOR:
5713 case ISD::FLOG:
5714 case ISD::FLOG10:
5715 case ISD::FLOG2:
5716 case ISD::FNEARBYINT:
5717 case ISD::FRINT:
5718 case ISD::FROUND:
5719 case ISD::FROUNDEVEN:
5720 case ISD::FSIN:
5721 case ISD::FSINH:
5722 case ISD::FSQRT:
5723 case ISD::FTAN:
5724 case ISD::FTANH:
5725 case ISD::FTRUNC:
5726 if (unrollExpandedOp())
5727 break;
5728 // If the target has custom/legal support for the scalar FP intrinsic ops
5729 // (they are probably not destined to become libcalls), then widen those
5730 // like any other unary ops.
5731 [[fallthrough]];
5732
5733 case ISD::ABS:
5735 case ISD::BITREVERSE:
5736 case ISD::BSWAP:
5737 case ISD::CTLZ:
5739 case ISD::CTPOP:
5740 case ISD::CTTZ:
5742 case ISD::FNEG:
5743 case ISD::FABS:
5744 case ISD::FREEZE:
5745 case ISD::ARITH_FENCE:
5746 case ISD::FCANONICALIZE:
5749 Res = WidenVecRes_Unary(N);
5750 break;
5751 case ISD::FMA:
5752 case ISD::FSHL:
5753 case ISD::FSHR:
5754 Res = WidenVecRes_Ternary(N);
5755 break;
5756 case ISD::FMODF:
5757 case ISD::FFREXP:
5758 case ISD::FSINCOS:
5759 case ISD::FSINCOSPI: {
5760 if (!unrollExpandedOp())
5761 Res = WidenVecRes_UnaryOpWithTwoResults(N, ResNo);
5762 break;
5763 }
5768 Res = WidenVecRes_PARTIAL_REDUCE_MLA(N);
5769 break;
5770 }
5771
5772 // If Res is null, the sub-method took care of registering the result.
5773 if (Res.getNode())
5774 SetWidenedVector(SDValue(N, ResNo), Res);
5775}
5776
5777SDValue DAGTypeLegalizer::WidenVecRes_Ternary(SDNode *N) {
5778 // Ternary op widening.
5779 SDLoc dl(N);
5780 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5781 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
5782 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
5783 SDValue InOp3 = GetWidenedVector(N->getOperand(2));
5784 return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, InOp3);
5785}
5786
5787SDValue DAGTypeLegalizer::WidenVecRes_Binary(SDNode *N) {
5788 // Binary op widening.
5789 SDLoc dl(N);
5790 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5791 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
5792 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
5793 if (N->getNumOperands() == 2)
5794 return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2,
5795 N->getFlags());
5796
5797 assert(N->getNumOperands() == 4 && "Unexpected number of operands!");
5798 assert((N->getOpcode() == ISD::VP_UDIV || N->getOpcode() == ISD::VP_SDIV ||
5799 N->getOpcode() == ISD::VP_UREM || N->getOpcode() == ISD::VP_SREM) &&
5800 "Expected VP opcode");
5801
5802 SDValue Mask =
5803 GetWidenedMask(N->getOperand(2), WidenVT.getVectorElementCount());
5804 return DAG.getNode(N->getOpcode(), dl, WidenVT,
5805 {InOp1, InOp2, Mask, N->getOperand(3)}, N->getFlags());
5806}
5807
5808SDValue DAGTypeLegalizer::WidenVecRes_MaskedBinary(SDNode *N) {
5809 SDLoc dl(N);
5810 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5811 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
5812 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
5813 SDValue Mask = N->getOperand(2);
5814 EVT WideMaskVT = WidenVT.changeVectorElementType(
5815 *DAG.getContext(), Mask.getValueType().getVectorElementType());
5816 Mask = ModifyToType(Mask, WideMaskVT, /*FillWithZeros=*/true);
5817 return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, Mask,
5818 N->getFlags());
5819}
5820
5821SDValue DAGTypeLegalizer::WidenVecRes_CMP(SDNode *N) {
5822 LLVMContext &Ctxt = *DAG.getContext();
5823 SDLoc dl(N);
5824
5825 SDValue LHS = N->getOperand(0);
5826 SDValue RHS = N->getOperand(1);
5827 EVT OpVT = LHS.getValueType();
5828 if (getTypeAction(OpVT) == TargetLowering::TypeWidenVector) {
5829 LHS = GetWidenedVector(LHS);
5830 RHS = GetWidenedVector(RHS);
5831 OpVT = LHS.getValueType();
5832 }
5833
5834 EVT WidenResVT = TLI.getTypeToTransformTo(Ctxt, N->getValueType(0));
5835 ElementCount WidenResEC = WidenResVT.getVectorElementCount();
5836 if (WidenResEC == OpVT.getVectorElementCount()) {
5837 return DAG.getNode(N->getOpcode(), dl, WidenResVT, LHS, RHS);
5838 }
5839
5840 return DAG.UnrollVectorOp(N, WidenResVT.getVectorNumElements());
5841}
5842
5843SDValue DAGTypeLegalizer::WidenVecRes_BinaryWithExtraScalarOp(SDNode *N) {
5844 // Binary op widening, but with an extra operand that shouldn't be widened.
5845 SDLoc dl(N);
5846 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5847 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
5848 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
5849 SDValue InOp3 = N->getOperand(2);
5850 return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, InOp3,
5851 N->getFlags());
5852}
5853
5854// Given a vector of operations that have been broken up to widen, see
5855// if we can collect them together into the next widest legal VT. This
5856// implementation is trap-safe.
5858 SmallVectorImpl<SDValue> &ConcatOps,
5859 unsigned ConcatEnd, EVT VT, EVT MaxVT,
5860 EVT WidenVT) {
5861 // Check to see if we have a single operation with the widen type.
5862 if (ConcatEnd == 1) {
5863 VT = ConcatOps[0].getValueType();
5864 if (VT == WidenVT)
5865 return ConcatOps[0];
5866 }
5867
5868 SDLoc dl(ConcatOps[0]);
5869 EVT WidenEltVT = WidenVT.getVectorElementType();
5870
5871 // while (Some element of ConcatOps is not of type MaxVT) {
5872 // From the end of ConcatOps, collect elements of the same type and put
5873 // them into an op of the next larger supported type
5874 // }
5875 while (ConcatOps[ConcatEnd-1].getValueType() != MaxVT) {
5876 int Idx = ConcatEnd - 1;
5877 VT = ConcatOps[Idx--].getValueType();
5878 while (Idx >= 0 && ConcatOps[Idx].getValueType() == VT)
5879 Idx--;
5880
5881 int NextSize = VT.isVector() ? VT.getVectorNumElements() : 1;
5882 EVT NextVT;
5883 do {
5884 NextSize *= 2;
5885 NextVT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NextSize);
5886 } while (!TLI.isTypeLegal(NextVT));
5887
5888 if (!VT.isVector()) {
5889 // Scalar type, create an INSERT_VECTOR_ELEMENT of type NextVT
5890 SDValue VecOp = DAG.getPOISON(NextVT);
5891 unsigned NumToInsert = ConcatEnd - Idx - 1;
5892 for (unsigned i = 0, OpIdx = Idx + 1; i < NumToInsert; i++, OpIdx++)
5893 VecOp = DAG.getInsertVectorElt(dl, VecOp, ConcatOps[OpIdx], i);
5894 ConcatOps[Idx+1] = VecOp;
5895 ConcatEnd = Idx + 2;
5896 } else {
5897 // Vector type, create a CONCAT_VECTORS of type NextVT
5898 SDValue undefVec = DAG.getPOISON(VT);
5899 unsigned OpsToConcat = NextSize/VT.getVectorNumElements();
5900 SmallVector<SDValue, 16> SubConcatOps(OpsToConcat);
5901 unsigned RealVals = ConcatEnd - Idx - 1;
5902 unsigned SubConcatEnd = 0;
5903 unsigned SubConcatIdx = Idx + 1;
5904 while (SubConcatEnd < RealVals)
5905 SubConcatOps[SubConcatEnd++] = ConcatOps[++Idx];
5906 while (SubConcatEnd < OpsToConcat)
5907 SubConcatOps[SubConcatEnd++] = undefVec;
5908 ConcatOps[SubConcatIdx] = DAG.getNode(ISD::CONCAT_VECTORS, dl,
5909 NextVT, SubConcatOps);
5910 ConcatEnd = SubConcatIdx + 1;
5911 }
5912 }
5913
5914 // Check to see if we have a single operation with the widen type.
5915 if (ConcatEnd == 1) {
5916 VT = ConcatOps[0].getValueType();
5917 if (VT == WidenVT)
5918 return ConcatOps[0];
5919 }
5920
5921 // add undefs of size MaxVT until ConcatOps grows to length of WidenVT
5923 MaxVT.getVectorElementCount());
5924 if (NumOps != ConcatEnd ) {
5925 SDValue UndefVal = DAG.getPOISON(MaxVT);
5926 for (unsigned j = ConcatEnd; j < NumOps; ++j)
5927 ConcatOps[j] = UndefVal;
5928 }
5929 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
5930 ArrayRef(ConcatOps.data(), NumOps));
5931}
5932
5933SDValue DAGTypeLegalizer::WidenVecRes_BinaryCanTrap(SDNode *N) {
5934 // Binary op widening for operations that can trap.
5935 unsigned Opcode = N->getOpcode();
5936 SDLoc dl(N);
5937 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5938 EVT VT = WidenVT;
5939 ElementCount NumElts = VT.getVectorElementCount();
5940 const SDNodeFlags Flags = N->getFlags();
5941 while (!TLI.isTypeLegal(VT) && NumElts.getKnownMinValue() != 1) {
5942 NumElts = NumElts.divideCoefficientBy(2);
5943 VT = VT.changeVectorElementCount(*DAG.getContext(), NumElts);
5944 }
5945
5946 bool FoundLegalVectorType = TLI.isTypeLegal(VT) && !NumElts.isScalar();
5947 if (FoundLegalVectorType && DAG.isSafeToSpeculativelyExecute(Opcode)) {
5948 // Operation can be speculatively executed so just widen as normal.
5949 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
5950 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
5951 return DAG.getNode(Opcode, dl, WidenVT, InOp1, InOp2, Flags);
5952 }
5953
5954 // Generate a vp.op if it is custom/legal for the target. This avoids need
5955 // to split and tile the subvectors (below), because the inactive lanes can
5956 // simply be disabled. To avoid possible recursion, only do this if the
5957 // widened mask type is legal.
5958 if (auto VPOpcode = ISD::getVPForBaseOpcode(Opcode);
5959 VPOpcode && TLI.isOperationLegalOrCustom(*VPOpcode, WidenVT)) {
5960 if (EVT WideMaskVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
5961 WidenVT.getVectorElementCount());
5962 TLI.isTypeLegal(WideMaskVT)) {
5963 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
5964 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
5965 SDValue Mask = DAG.getAllOnesConstant(dl, WideMaskVT);
5966 SDValue EVL =
5967 DAG.getElementCount(dl, TLI.getVPExplicitVectorLengthTy(),
5968 N->getValueType(0).getVectorElementCount());
5969 return DAG.getNode(*VPOpcode, dl, WidenVT, InOp1, InOp2, Mask, EVL,
5970 Flags);
5971 }
5972 }
5973
5974 // No legal vector version so unroll the vector operation and then widen.
5975 if (NumElts.isScalar())
5976 return DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements());
5977
5978 if (!TLI.isTypeLegal(VT))
5979 report_fatal_error("Cannot widen scalable vector trapping binop!");
5980
5981 // Since the operation can trap, apply operation on the original vector.
5982 EVT MaxVT = VT;
5983 EVT EltVT = VT.getVectorElementType();
5984 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
5985 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
5986 ElementCount CurNumElts = N->getValueType(0).getVectorElementCount();
5987
5988 SmallVector<SDValue, 16> ConcatOps(CurNumElts.getKnownMinValue());
5989 unsigned ConcatEnd = 0; // Current ConcatOps index.
5990 int Idx = 0; // Current Idx into input vectors.
5991
5992 // NumElts := greatest legal vector size (at most WidenVT)
5993 // while (orig. vector has unhandled elements) {
5994 // take munches of size NumElts from the beginning and add to ConcatOps
5995 // NumElts := next smaller supported vector size or 1
5996 // }
5997 for (;;) {
5998 while (ElementCount::isKnownGE(CurNumElts, NumElts)) {
5999 SDValue EOp1 = DAG.getExtractSubvector(dl, VT, InOp1, Idx);
6000 SDValue EOp2 = DAG.getExtractSubvector(dl, VT, InOp2, Idx);
6001 ConcatOps[ConcatEnd++] = DAG.getNode(Opcode, dl, VT, EOp1, EOp2, Flags);
6002 // Subvector indices are implicitly scaled by vscale.
6003 Idx += NumElts.getKnownMinValue();
6004 CurNumElts -= NumElts;
6005 }
6006
6007 if (CurNumElts.isZero())
6008 break;
6009
6010 // Find the next smaller legal vector type.
6011 do {
6012 NumElts = NumElts.divideCoefficientBy(2);
6013 VT = VT.changeVectorElementCount(*DAG.getContext(), NumElts);
6014 } while (!TLI.isTypeLegal(VT) && NumElts.getKnownMinValue() != 1);
6015
6016 if (NumElts.isScalar()) {
6017 // Scalarize the non-type-legal remainder.
6018 for (unsigned i = 0; i != CurNumElts.getFixedValue(); ++i, ++Idx) {
6019 SDValue EOp1 = DAG.getExtractVectorElt(dl, EltVT, InOp1, Idx);
6020 SDValue EOp2 = DAG.getExtractVectorElt(dl, EltVT, InOp2, Idx);
6021 ConcatOps[ConcatEnd++] =
6022 DAG.getNode(Opcode, dl, EltVT, EOp1, EOp2, Flags);
6023 }
6024
6025 break;
6026 }
6027
6028 if (!TLI.isTypeLegal(VT))
6029 report_fatal_error("scalarization of scalable vectors is not supported");
6030 }
6031
6032 return CollectOpsToWiden(DAG, TLI, ConcatOps, ConcatEnd, VT, MaxVT, WidenVT);
6033}
6034
6035SDValue DAGTypeLegalizer::WidenVecRes_StrictFP(SDNode *N) {
6036 switch (N->getOpcode()) {
6037 case ISD::STRICT_FSETCC:
6039 return WidenVecRes_STRICT_FSETCC(N);
6046 return WidenVecRes_Convert_StrictFP(N);
6047 default:
6048 break;
6049 }
6050
6051 // StrictFP op widening for operations that can trap.
6052 unsigned NumOpers = N->getNumOperands();
6053 unsigned Opcode = N->getOpcode();
6054 SDLoc dl(N);
6055 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6056 EVT WidenEltVT = WidenVT.getVectorElementType();
6057 EVT VT = WidenVT;
6058 unsigned NumElts = VT.getVectorNumElements();
6059 while (!TLI.isTypeLegal(VT) && NumElts != 1) {
6060 NumElts = NumElts / 2;
6061 VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
6062 }
6063
6064 // No legal vector version so unroll the vector operation and then widen.
6065 if (NumElts == 1)
6066 return UnrollVectorOp_StrictFP(N, WidenVT.getVectorNumElements());
6067
6068 // Since the operation can trap, apply operation on the original vector.
6069 EVT MaxVT = VT;
6071 unsigned CurNumElts = N->getValueType(0).getVectorNumElements();
6072
6073 SmallVector<SDValue, 16> ConcatOps(CurNumElts);
6075 unsigned ConcatEnd = 0; // Current ConcatOps index.
6076 int Idx = 0; // Current Idx into input vectors.
6077
6078 // The Chain is the first operand.
6079 InOps.push_back(N->getOperand(0));
6080
6081 // Now process the remaining operands.
6082 for (unsigned i = 1; i < NumOpers; ++i) {
6083 SDValue Oper = N->getOperand(i);
6084
6085 EVT OpVT = Oper.getValueType();
6086 if (OpVT.isVector()) {
6087 if (getTypeAction(OpVT) == TargetLowering::TypeWidenVector)
6088 Oper = GetWidenedVector(Oper);
6089 else {
6090 EVT WideOpVT =
6091 EVT::getVectorVT(*DAG.getContext(), OpVT.getVectorElementType(),
6092 WidenVT.getVectorElementCount());
6093 Oper = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideOpVT,
6094 DAG.getPOISON(WideOpVT), Oper,
6095 DAG.getVectorIdxConstant(0, dl));
6096 }
6097 }
6098
6099 InOps.push_back(Oper);
6100 }
6101
6102 // NumElts := greatest legal vector size (at most WidenVT)
6103 // while (orig. vector has unhandled elements) {
6104 // take munches of size NumElts from the beginning and add to ConcatOps
6105 // NumElts := next smaller supported vector size or 1
6106 // }
6107 while (CurNumElts != 0) {
6108 while (CurNumElts >= NumElts) {
6110
6111 for (unsigned i = 0; i < NumOpers; ++i) {
6112 SDValue Op = InOps[i];
6113
6114 EVT OpVT = Op.getValueType();
6115 if (OpVT.isVector()) {
6116 EVT OpExtractVT =
6117 EVT::getVectorVT(*DAG.getContext(), OpVT.getVectorElementType(),
6119 Op = DAG.getExtractSubvector(dl, OpExtractVT, Op, Idx);
6120 }
6121
6122 EOps.push_back(Op);
6123 }
6124
6125 EVT OperVT[] = {VT, MVT::Other};
6126 SDValue Oper = DAG.getNode(Opcode, dl, OperVT, EOps);
6127 ConcatOps[ConcatEnd++] = Oper;
6128 Chains.push_back(Oper.getValue(1));
6129 Idx += NumElts;
6130 CurNumElts -= NumElts;
6131 }
6132 do {
6133 NumElts = NumElts / 2;
6134 VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
6135 } while (!TLI.isTypeLegal(VT) && NumElts != 1);
6136
6137 if (NumElts == 1) {
6138 for (unsigned i = 0; i != CurNumElts; ++i, ++Idx) {
6140
6141 for (unsigned i = 0; i < NumOpers; ++i) {
6142 SDValue Op = InOps[i];
6143
6144 EVT OpVT = Op.getValueType();
6145 if (OpVT.isVector())
6146 Op = DAG.getExtractVectorElt(dl, OpVT.getVectorElementType(), Op,
6147 Idx);
6148
6149 EOps.push_back(Op);
6150 }
6151
6152 EVT WidenVT[] = {WidenEltVT, MVT::Other};
6153 SDValue Oper = DAG.getNode(Opcode, dl, WidenVT, EOps);
6154 ConcatOps[ConcatEnd++] = Oper;
6155 Chains.push_back(Oper.getValue(1));
6156 }
6157 CurNumElts = 0;
6158 }
6159 }
6160
6161 // Build a factor node to remember all the Ops that have been created.
6162 SDValue NewChain;
6163 if (Chains.size() == 1)
6164 NewChain = Chains[0];
6165 else
6166 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
6167 ReplaceValueWith(SDValue(N, 1), NewChain);
6168
6169 return CollectOpsToWiden(DAG, TLI, ConcatOps, ConcatEnd, VT, MaxVT, WidenVT);
6170}
6171
6172SDValue DAGTypeLegalizer::WidenVecRes_OverflowOp(SDNode *N, unsigned ResNo) {
6173 SDLoc DL(N);
6174 EVT ResVT = N->getValueType(0);
6175 EVT OvVT = N->getValueType(1);
6176 EVT WideResVT, WideOvVT;
6177 SDValue WideLHS, WideRHS;
6178
6179 // TODO: This might result in a widen/split loop.
6180 if (ResNo == 0) {
6181 WideResVT = TLI.getTypeToTransformTo(*DAG.getContext(), ResVT);
6182 WideOvVT = EVT::getVectorVT(
6183 *DAG.getContext(), OvVT.getVectorElementType(),
6184 WideResVT.getVectorNumElements());
6185
6186 WideLHS = GetWidenedVector(N->getOperand(0));
6187 WideRHS = GetWidenedVector(N->getOperand(1));
6188 } else {
6189 WideOvVT = TLI.getTypeToTransformTo(*DAG.getContext(), OvVT);
6190 WideResVT = EVT::getVectorVT(
6191 *DAG.getContext(), ResVT.getVectorElementType(),
6192 WideOvVT.getVectorNumElements());
6193
6194 SDValue Zero = DAG.getVectorIdxConstant(0, DL);
6195 SDValue Poison = DAG.getPOISON(WideResVT);
6196
6197 WideLHS = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, WideResVT, Poison,
6198 N->getOperand(0), Zero);
6199 WideRHS = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, WideResVT, Poison,
6200 N->getOperand(1), Zero);
6201 }
6202
6203 SDVTList WideVTs = DAG.getVTList(WideResVT, WideOvVT);
6204 SDNode *WideNode = DAG.getNode(
6205 N->getOpcode(), DL, WideVTs, WideLHS, WideRHS).getNode();
6206
6207 // Replace the other vector result not being explicitly widened here.
6208 unsigned OtherNo = 1 - ResNo;
6209 EVT OtherVT = N->getValueType(OtherNo);
6210 if (getTypeAction(OtherVT) == TargetLowering::TypeWidenVector) {
6211 SetWidenedVector(SDValue(N, OtherNo), SDValue(WideNode, OtherNo));
6212 } else {
6213 SDValue Zero = DAG.getVectorIdxConstant(0, DL);
6214 SDValue OtherVal = DAG.getNode(
6215 ISD::EXTRACT_SUBVECTOR, DL, OtherVT, SDValue(WideNode, OtherNo), Zero);
6216 ReplaceValueWith(SDValue(N, OtherNo), OtherVal);
6217 }
6218
6219 return SDValue(WideNode, ResNo);
6220}
6221
6222SDValue DAGTypeLegalizer::WidenVecRes_Convert(SDNode *N) {
6223 LLVMContext &Ctx = *DAG.getContext();
6224 SDValue InOp = N->getOperand(0);
6225 SDLoc DL(N);
6226
6227 EVT WidenVT = TLI.getTypeToTransformTo(Ctx, N->getValueType(0));
6228 ElementCount WidenEC = WidenVT.getVectorElementCount();
6229
6230 EVT InVT = InOp.getValueType();
6231
6232 unsigned Opcode = N->getOpcode();
6233 const SDNodeFlags Flags = N->getFlags();
6234
6235 // Handle the case of ZERO_EXTEND where the promoted InVT element size does
6236 // not equal that of WidenVT.
6237 if (N->getOpcode() == ISD::ZERO_EXTEND &&
6238 getTypeAction(InVT) == TargetLowering::TypePromoteInteger &&
6239 TLI.getTypeToTransformTo(Ctx, InVT).getScalarSizeInBits() !=
6240 WidenVT.getScalarSizeInBits()) {
6241 InOp = ZExtPromotedInteger(InOp);
6242 InVT = InOp.getValueType();
6243 if (WidenVT.getScalarSizeInBits() < InVT.getScalarSizeInBits())
6244 Opcode = ISD::TRUNCATE;
6245 }
6246
6247 EVT InEltVT = InVT.getVectorElementType();
6248 EVT InWidenVT = EVT::getVectorVT(Ctx, InEltVT, WidenEC);
6249 ElementCount InVTEC = InVT.getVectorElementCount();
6250
6251 // Helper to build node with all scalar trailing operands.
6252 auto MakeConvertNode = [&](EVT VT, SDValue Op) -> SDValue {
6253 if (N->getNumOperands() == 1)
6254 return DAG.getNode(Opcode, DL, VT, Op, Flags);
6255 if (Opcode == ISD::CONVERT_TO_ARBITRARY_FP)
6256 return DAG.getNode(Opcode, DL, VT, Op, N->getOperand(1), N->getOperand(2),
6257 N->getOperand(3), Flags);
6258 return DAG.getNode(Opcode, DL, VT, Op, N->getOperand(1), Flags);
6259 };
6260
6261 if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
6262 InOp = GetWidenedVector(N->getOperand(0));
6263 InVT = InOp.getValueType();
6264 InVTEC = InVT.getVectorElementCount();
6265 if (InVTEC == WidenEC)
6266 return MakeConvertNode(WidenVT, InOp);
6267 if (WidenVT.getSizeInBits() == InVT.getSizeInBits()) {
6268 // If both input and result vector types are of same width, extend
6269 // operations should be done with SIGN/ZERO_EXTEND_VECTOR_INREG, which
6270 // accepts fewer elements in the result than in the input.
6271 if (Opcode == ISD::ANY_EXTEND)
6272 return DAG.getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, WidenVT, InOp);
6273 if (Opcode == ISD::SIGN_EXTEND)
6274 return DAG.getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, WidenVT, InOp);
6275 if (Opcode == ISD::ZERO_EXTEND)
6276 return DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, WidenVT, InOp);
6277 }
6278
6279 // For TRUNCATE, try to widen using the legal EC of the input type instead
6280 // if the legalisation action for that intermediate type is not widening.
6281 // E.g. for trunc nxv1i64 -> nxv1i8 where
6282 // - nxv1i64 input gets widened to nxv2i64
6283 // - nxv1i8 output gets widened to nxv16i8
6284 // Then one can try widening the result to nxv2i8 (instead of going all the
6285 // way to nxv16i8) if this later allows type promotion.
6286 EVT MidResVT =
6287 EVT::getVectorVT(Ctx, WidenVT.getVectorElementType(), InVTEC);
6288 if (N->getOpcode() == ISD::TRUNCATE &&
6289 getTypeAction(MidResVT) == TargetLowering::TypePromoteInteger) {
6290 SDValue MidRes = DAG.getNode(ISD::TRUNCATE, DL, MidResVT, InOp, Flags);
6291 return DAG.getInsertSubvector(DL, DAG.getPOISON(WidenVT), MidRes, 0);
6292 }
6293 }
6294
6295 if (TLI.isTypeLegal(InWidenVT)) {
6296 // Because the result and the input are different vector types, widening
6297 // the result could create a legal type but widening the input might make
6298 // it an illegal type that might lead to repeatedly splitting the input
6299 // and then widening it. To avoid this, we widen the input only if
6300 // it results in a legal type.
6301 if (WidenEC.isKnownMultipleOf(InVTEC.getKnownMinValue())) {
6302 // Widen the input and call convert on the widened input vector.
6303 unsigned NumConcat =
6304 WidenEC.getKnownMinValue() / InVTEC.getKnownMinValue();
6305 SmallVector<SDValue, 16> Ops(NumConcat, DAG.getPOISON(InVT));
6306 Ops[0] = InOp;
6307 SDValue InVec = DAG.getNode(ISD::CONCAT_VECTORS, DL, InWidenVT, Ops);
6308 return MakeConvertNode(WidenVT, InVec);
6309 }
6310
6311 if (InVTEC.isKnownMultipleOf(WidenEC.getKnownMinValue())) {
6312 SDValue InVal = DAG.getExtractSubvector(DL, InWidenVT, InOp, 0);
6313 // Extract the input and convert the shorten input vector.
6314 return MakeConvertNode(WidenVT, InVal);
6315 }
6316 }
6317
6318 // Otherwise unroll into some nasty scalar code and rebuild the vector.
6319 EVT EltVT = WidenVT.getVectorElementType();
6320 SmallVector<SDValue, 16> Ops(WidenEC.getFixedValue(), DAG.getPOISON(EltVT));
6321 // Use the original element count so we don't do more scalar opts than
6322 // necessary.
6323 unsigned MinElts = N->getValueType(0).getVectorNumElements();
6324 for (unsigned i=0; i < MinElts; ++i) {
6325 SDValue Val = DAG.getExtractVectorElt(DL, InEltVT, InOp, i);
6326 Ops[i] = MakeConvertNode(EltVT, Val);
6327 }
6328
6329 return DAG.getBuildVector(WidenVT, DL, Ops);
6330}
6331
6332SDValue DAGTypeLegalizer::WidenVecRes_FP_TO_XINT_SAT(SDNode *N) {
6333 SDLoc dl(N);
6334 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6335 ElementCount WidenNumElts = WidenVT.getVectorElementCount();
6336
6337 SDValue Src = N->getOperand(0);
6338 EVT SrcVT = Src.getValueType();
6339
6340 // Also widen the input.
6341 if (getTypeAction(SrcVT) == TargetLowering::TypeWidenVector) {
6342 Src = GetWidenedVector(Src);
6343 SrcVT = Src.getValueType();
6344 }
6345
6346 // Input and output not widened to the same size, give up.
6347 if (WidenNumElts != SrcVT.getVectorElementCount())
6348 return DAG.UnrollVectorOp(N, WidenNumElts.getKnownMinValue());
6349
6350 return DAG.getNode(N->getOpcode(), dl, WidenVT, Src, N->getOperand(1));
6351}
6352
6353SDValue DAGTypeLegalizer::WidenVecRes_XROUND(SDNode *N) {
6354 SDLoc dl(N);
6355 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6356 ElementCount WidenNumElts = WidenVT.getVectorElementCount();
6357
6358 SDValue Src = N->getOperand(0);
6359 EVT SrcVT = Src.getValueType();
6360
6361 // Also widen the input.
6362 if (getTypeAction(SrcVT) == TargetLowering::TypeWidenVector) {
6363 Src = GetWidenedVector(Src);
6364 SrcVT = Src.getValueType();
6365 }
6366
6367 // Input and output not widened to the same size, give up.
6368 if (WidenNumElts != SrcVT.getVectorElementCount())
6369 return DAG.UnrollVectorOp(N, WidenNumElts.getKnownMinValue());
6370
6371 return DAG.getNode(N->getOpcode(), dl, WidenVT, Src);
6372}
6373
6374SDValue DAGTypeLegalizer::WidenVecRes_Convert_StrictFP(SDNode *N) {
6375 SDValue InOp = N->getOperand(1);
6376 SDLoc DL(N);
6377 SmallVector<SDValue, 4> NewOps(N->ops());
6378
6379 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6380 unsigned WidenNumElts = WidenVT.getVectorNumElements();
6381
6382 EVT InVT = InOp.getValueType();
6383 EVT InEltVT = InVT.getVectorElementType();
6384
6385 unsigned Opcode = N->getOpcode();
6386
6387 // FIXME: Optimizations need to be implemented here.
6388
6389 // Otherwise unroll into some nasty scalar code and rebuild the vector.
6390 EVT EltVT = WidenVT.getVectorElementType();
6391 std::array<EVT, 2> EltVTs = {{EltVT, MVT::Other}};
6392 SmallVector<SDValue, 16> Ops(WidenNumElts, DAG.getPOISON(EltVT));
6393 SmallVector<SDValue, 32> OpChains;
6394 // Use the original element count so we don't do more scalar opts than
6395 // necessary.
6396 unsigned MinElts = N->getValueType(0).getVectorNumElements();
6397 for (unsigned i=0; i < MinElts; ++i) {
6398 NewOps[1] = DAG.getExtractVectorElt(DL, InEltVT, InOp, i);
6399 Ops[i] = DAG.getNode(Opcode, DL, EltVTs, NewOps);
6400 OpChains.push_back(Ops[i].getValue(1));
6401 }
6402 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OpChains);
6403 ReplaceValueWith(SDValue(N, 1), NewChain);
6404
6405 return DAG.getBuildVector(WidenVT, DL, Ops);
6406}
6407
6408SDValue DAGTypeLegalizer::WidenVecRes_EXTEND_VECTOR_INREG(SDNode *N) {
6409 unsigned Opcode = N->getOpcode();
6410 SDValue InOp = N->getOperand(0);
6411 SDLoc DL(N);
6412
6413 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6414 EVT WidenSVT = WidenVT.getVectorElementType();
6415 unsigned WidenNumElts = WidenVT.getVectorNumElements();
6416
6417 EVT InVT = InOp.getValueType();
6418 EVT InSVT = InVT.getVectorElementType();
6419 unsigned InVTNumElts = InVT.getVectorNumElements();
6420
6421 if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
6422 InOp = GetWidenedVector(InOp);
6423 InVT = InOp.getValueType();
6424 if (InVT.getSizeInBits() == WidenVT.getSizeInBits()) {
6425 switch (Opcode) {
6429 return DAG.getNode(Opcode, DL, WidenVT, InOp);
6430 }
6431 }
6432 }
6433
6434 // Unroll, extend the scalars and rebuild the vector.
6436 for (unsigned i = 0, e = std::min(InVTNumElts, WidenNumElts); i != e; ++i) {
6437 SDValue Val = DAG.getExtractVectorElt(DL, InSVT, InOp, i);
6438 switch (Opcode) {
6440 Val = DAG.getNode(ISD::ANY_EXTEND, DL, WidenSVT, Val);
6441 break;
6443 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, WidenSVT, Val);
6444 break;
6446 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, WidenSVT, Val);
6447 break;
6448 default:
6449 llvm_unreachable("A *_EXTEND_VECTOR_INREG node was expected");
6450 }
6451 Ops.push_back(Val);
6452 }
6453
6454 while (Ops.size() != WidenNumElts)
6455 Ops.push_back(DAG.getPOISON(WidenSVT));
6456
6457 return DAG.getBuildVector(WidenVT, DL, Ops);
6458}
6459
6460SDValue DAGTypeLegalizer::WidenVecRes_FCOPYSIGN(SDNode *N) {
6461 // If this is an FCOPYSIGN with same input types, we can treat it as a
6462 // normal binary op.
6463 if (N->getOperand(0).getValueType() == N->getOperand(1).getValueType())
6464 return WidenVecRes_Binary(N);
6465
6466 // If the types are different, fall back to unrolling.
6467 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6468 return DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements());
6469}
6470
6471/// Result and first source operand are different scalar types, but must have
6472/// the same number of elements. There is an additional control argument which
6473/// should be passed through unchanged.
6474SDValue DAGTypeLegalizer::WidenVecRes_UnarySameEltsWithScalarArg(SDNode *N) {
6475 SDValue FpValue = N->getOperand(0);
6476 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6477 if (getTypeAction(FpValue.getValueType()) != TargetLowering::TypeWidenVector)
6478 return DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements());
6479 SDValue Arg = GetWidenedVector(FpValue);
6480 return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, {Arg, N->getOperand(1)},
6481 N->getFlags());
6482}
6483
6484SDValue DAGTypeLegalizer::WidenVecRes_ExpOp(SDNode *N) {
6485 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6486 SDValue InOp = GetWidenedVector(N->getOperand(0));
6487 SDValue RHS = N->getOperand(1);
6488 EVT ExpVT = RHS.getValueType();
6489 SDValue ExpOp = RHS;
6490 if (ExpVT.isVector()) {
6491 EVT WideExpVT = WidenVT.changeVectorElementType(
6492 *DAG.getContext(), ExpVT.getVectorElementType());
6493 if (WideExpVT == TLI.getTypeToTransformTo(*DAG.getContext(), ExpVT))
6494 ExpOp = GetWidenedVector(RHS);
6495 else
6496 ExpOp = ModifyToType(RHS, WideExpVT);
6497 }
6498
6499 return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp, ExpOp);
6500}
6501
6502SDValue DAGTypeLegalizer::WidenVecRes_Unary(SDNode *N) {
6503 // Unary op widening.
6504 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6505 SDValue InOp = GetWidenedVector(N->getOperand(0));
6506 if (N->getNumOperands() == 1)
6507 return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp, N->getFlags());
6508 assert(N->getOpcode() == ISD::AssertNoFPClass && "unexpected opcode");
6509 return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp, N->getOperand(1),
6510 N->getFlags());
6511}
6512
6513SDValue DAGTypeLegalizer::WidenVecRes_InregOp(SDNode *N) {
6514 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6515 EVT ExtVT = EVT::getVectorVT(
6516 *DAG.getContext(),
6517 cast<VTSDNode>(N->getOperand(1))->getVT().getVectorElementType(),
6518 WidenVT.getVectorElementCount());
6519 SDValue WidenLHS = GetWidenedVector(N->getOperand(0));
6520 return DAG.getNode(N->getOpcode(), SDLoc(N),
6521 WidenVT, WidenLHS, DAG.getValueType(ExtVT));
6522}
6523
6524SDValue DAGTypeLegalizer::WidenVecRes_UnaryOpWithTwoResults(SDNode *N,
6525 unsigned ResNo) {
6526 EVT VT0 = N->getValueType(0);
6527 EVT VT1 = N->getValueType(1);
6528
6529 assert(VT0.isVector() && VT1.isVector() &&
6531 "expected both results to be vectors of matching element count");
6532
6533 LLVMContext &Ctx = *DAG.getContext();
6534 SDValue InOp = GetWidenedVector(N->getOperand(0));
6535
6536 EVT WidenVT = TLI.getTypeToTransformTo(Ctx, N->getValueType(ResNo));
6537 ElementCount WidenEC = WidenVT.getVectorElementCount();
6538
6539 EVT WidenVT0 = EVT::getVectorVT(Ctx, VT0.getVectorElementType(), WidenEC);
6540 EVT WidenVT1 = EVT::getVectorVT(Ctx, VT1.getVectorElementType(), WidenEC);
6541
6542 SDNode *WidenNode =
6543 DAG.getNode(N->getOpcode(), SDLoc(N), {WidenVT0, WidenVT1}, InOp)
6544 .getNode();
6545
6546 ReplaceOtherWidenResults(N, WidenNode, ResNo);
6547 return SDValue(WidenNode, ResNo);
6548}
6549
6550SDValue DAGTypeLegalizer::WidenVecRes_MERGE_VALUES(SDNode *N, unsigned ResNo) {
6551 SDValue WidenVec = DisintegrateMERGE_VALUES(N, ResNo);
6552 return GetWidenedVector(WidenVec);
6553}
6554
6555SDValue DAGTypeLegalizer::WidenVecRes_ADDRSPACECAST(SDNode *N) {
6556 SDLoc DL(N);
6557 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6558 ElementCount WidenEC = WidenVT.getVectorElementCount();
6559 auto *AddrSpaceCastN = cast<AddrSpaceCastSDNode>(N);
6560
6561 // The source has the same number of elements as the result, so widen it to
6562 // match WidenVT. It only lives in the widened-vector map if it is itself
6563 // widened; otherwise pad it up to the widened element count
6564 // when it is illegal.
6565 SDValue InOp = N->getOperand(0);
6566 EVT InVT = InOp.getValueType();
6567 TargetLowering::LegalizeTypeAction InAction = getTypeAction(InVT);
6568 if (InAction == TargetLowering::TypeWidenVector) {
6569 InOp = GetWidenedVector(InOp);
6570 } else if (InAction != TargetLowering::TypeLegal) {
6571 EVT InWidenVT = EVT::getVectorVT(*DAG.getContext(),
6572 InVT.getVectorElementType(), WidenEC);
6573 InOp = DAG.getInsertSubvector(DL, DAG.getPOISON(InWidenVT), InOp, 0);
6574 }
6575
6576 return DAG.getAddrSpaceCast(
6577 DL, WidenVT, InOp, AddrSpaceCastN->getSrcAddressSpace(),
6578 AddrSpaceCastN->getDestAddressSpace(), AddrSpaceCastN->getFlags());
6579}
6580
6581SDValue DAGTypeLegalizer::WidenVecRes_BITCAST(SDNode *N) {
6582 SDValue InOp = N->getOperand(0);
6583 EVT InVT = InOp.getValueType();
6584 EVT VT = N->getValueType(0);
6585 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6586 SDLoc dl(N);
6587
6588 switch (getTypeAction(InVT)) {
6590 break;
6592 report_fatal_error("scalarization of scalable vectors is not supported");
6594 // If the incoming type is a vector that is being promoted, then
6595 // we know that the elements are arranged differently and that we
6596 // must perform the conversion using a stack slot.
6597 if (InVT.isVector())
6598 break;
6599
6600 // If the InOp is promoted to the same size, convert it. Otherwise,
6601 // fall out of the switch and widen the promoted input.
6602 SDValue NInOp = GetPromotedInteger(InOp);
6603 EVT NInVT = NInOp.getValueType();
6604 if (WidenVT.bitsEq(NInVT)) {
6605 // For big endian targets we need to shift the input integer or the
6606 // interesting bits will end up at the wrong place.
6607 if (DAG.getDataLayout().isBigEndian()) {
6608 unsigned ShiftAmt = NInVT.getSizeInBits() - InVT.getSizeInBits();
6609 NInOp = DAG.getNode(ISD::SHL, dl, NInVT, NInOp,
6610 DAG.getShiftAmountConstant(ShiftAmt, NInVT, dl));
6611 }
6612 return DAG.getNode(ISD::BITCAST, dl, WidenVT, NInOp);
6613 }
6614 InOp = NInOp;
6615 InVT = NInVT;
6616 break;
6617 }
6624 break;
6626 // If the InOp is widened to the same size, convert it. Otherwise, fall
6627 // out of the switch and widen the widened input.
6628 InOp = GetWidenedVector(InOp);
6629 InVT = InOp.getValueType();
6630 if (WidenVT.bitsEq(InVT))
6631 // The input widens to the same size. Convert to the widen value.
6632 return DAG.getNode(ISD::BITCAST, dl, WidenVT, InOp);
6633 break;
6634 }
6635
6636 unsigned WidenSize = WidenVT.getSizeInBits();
6637 unsigned InSize = InVT.getSizeInBits();
6638 unsigned InScalarSize = InVT.getScalarSizeInBits();
6639 // x86mmx is not an acceptable vector element type, so don't try.
6640 if (WidenSize % InScalarSize == 0 && InVT != MVT::x86mmx) {
6641 // Determine new input vector type. The new input vector type will use
6642 // the same element type (if its a vector) or use the input type as a
6643 // vector. It is the same size as the type to widen to.
6644 EVT NewInVT;
6645 unsigned NewNumParts = WidenSize / InSize;
6646 if (InVT.isVector()) {
6647 EVT InEltVT = InVT.getVectorElementType();
6648 NewInVT = EVT::getVectorVT(*DAG.getContext(), InEltVT,
6649 WidenSize / InEltVT.getSizeInBits());
6650 } else {
6651 // For big endian systems, using the promoted input scalar type
6652 // to produce the scalar_to_vector would put the desired bits into
6653 // the least significant byte(s) of the wider element zero. This
6654 // will mean that the users of the result vector are using incorrect
6655 // bits. Use the original input type instead. Although either input
6656 // type can be used on little endian systems, for consistency we
6657 // use the original type there as well.
6658 EVT OrigInVT = N->getOperand(0).getValueType();
6659 NewNumParts = WidenSize / OrigInVT.getSizeInBits();
6660 NewInVT = EVT::getVectorVT(*DAG.getContext(), OrigInVT, NewNumParts);
6661 }
6662
6663 if (TLI.isTypeLegal(NewInVT)) {
6664 SDValue NewVec;
6665 if (InVT.isVector()) {
6666 // Because the result and the input are different vector types, widening
6667 // the result could create a legal type but widening the input might
6668 // make it an illegal type that might lead to repeatedly splitting the
6669 // input and then widening it. To avoid this, we widen the input only if
6670 // it results in a legal type.
6671 if (WidenSize % InSize == 0) {
6672 SmallVector<SDValue, 16> Ops(NewNumParts, DAG.getPOISON(InVT));
6673 Ops[0] = InOp;
6674
6675 NewVec = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewInVT, Ops);
6676 } else {
6678 DAG.ExtractVectorElements(InOp, Ops);
6679 Ops.append(WidenSize / InScalarSize - Ops.size(),
6680 DAG.getPOISON(InVT.getVectorElementType()));
6681
6682 NewVec = DAG.getNode(ISD::BUILD_VECTOR, dl, NewInVT, Ops);
6683 }
6684 } else {
6685 NewVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewInVT, InOp);
6686 }
6687 return DAG.getNode(ISD::BITCAST, dl, WidenVT, NewVec);
6688 }
6689 }
6690
6691 return CreateStackStoreLoad(InOp, WidenVT);
6692}
6693
6694SDValue DAGTypeLegalizer::WidenVecRes_LOOP_DEPENDENCE_MASK(SDNode *N) {
6695 return DAG.getNode(
6696 N->getOpcode(), SDLoc(N),
6697 TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)),
6698 N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3));
6699}
6700
6701SDValue DAGTypeLegalizer::WidenVecRes_BUILD_VECTOR(SDNode *N) {
6702 SDLoc dl(N);
6703 // Build a vector with poison for the new nodes.
6704 EVT VT = N->getValueType(0);
6705
6706 // Integer BUILD_VECTOR operands may be larger than the node's vector element
6707 // type. The POISONs need to have the same type as the existing operands.
6708 EVT EltVT = N->getOperand(0).getValueType();
6709 unsigned NumElts = VT.getVectorNumElements();
6710
6711 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6712 unsigned WidenNumElts = WidenVT.getVectorNumElements();
6713
6714 SmallVector<SDValue, 16> NewOps(N->ops());
6715 assert(WidenNumElts >= NumElts && "Shrinking vector instead of widening!");
6716 NewOps.append(WidenNumElts - NumElts, DAG.getPOISON(EltVT));
6717
6718 return DAG.getBuildVector(WidenVT, dl, NewOps);
6719}
6720
6721SDValue DAGTypeLegalizer::WidenVecRes_CONCAT_VECTORS(SDNode *N) {
6722 EVT InVT = N->getOperand(0).getValueType();
6723 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6724 SDLoc dl(N);
6725 unsigned NumOperands = N->getNumOperands();
6726
6727 bool InputWidened = false; // Indicates we need to widen the input.
6728 if (getTypeAction(InVT) != TargetLowering::TypeWidenVector) {
6729 unsigned WidenNumElts = WidenVT.getVectorMinNumElements();
6730 unsigned NumInElts = InVT.getVectorMinNumElements();
6731 if (WidenNumElts % NumInElts == 0) {
6732 // Add undef vectors to widen to correct length.
6733 unsigned NumConcat = WidenNumElts / NumInElts;
6734 SDValue UndefVal = DAG.getPOISON(InVT);
6735 SmallVector<SDValue, 16> Ops(NumConcat);
6736 for (unsigned i=0; i < NumOperands; ++i)
6737 Ops[i] = N->getOperand(i);
6738 for (unsigned i = NumOperands; i != NumConcat; ++i)
6739 Ops[i] = UndefVal;
6740 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, Ops);
6741 }
6742 } else {
6743 InputWidened = true;
6744 if (WidenVT == TLI.getTypeToTransformTo(*DAG.getContext(), InVT)) {
6745 // The inputs and the result are widen to the same value.
6746 unsigned i;
6747 for (i=1; i < NumOperands; ++i)
6748 if (!N->getOperand(i).isUndef())
6749 break;
6750
6751 if (i == NumOperands)
6752 // Everything but the first operand is an UNDEF so just return the
6753 // widened first operand.
6754 return GetWidenedVector(N->getOperand(0));
6755
6756 if (NumOperands == 2) {
6757 assert(!WidenVT.isScalableVector() &&
6758 "Cannot use vector shuffles to widen CONCAT_VECTOR result");
6759 unsigned WidenNumElts = WidenVT.getVectorNumElements();
6760 unsigned NumInElts = InVT.getVectorNumElements();
6761
6762 // Replace concat of two operands with a shuffle.
6763 SmallVector<int, 16> MaskOps(WidenNumElts, -1);
6764 for (unsigned i = 0; i < NumInElts; ++i) {
6765 MaskOps[i] = i;
6766 MaskOps[i + NumInElts] = i + WidenNumElts;
6767 }
6768 return DAG.getVectorShuffle(WidenVT, dl,
6769 GetWidenedVector(N->getOperand(0)),
6770 GetWidenedVector(N->getOperand(1)),
6771 MaskOps);
6772 }
6773 }
6774 }
6775
6776 if (WidenVT.isScalableVector()) {
6777 SDValue WideVec = DAG.getPOISON(WidenVT);
6778 unsigned NumInElts = InVT.getVectorMinNumElements();
6779 for (unsigned I = 0; I < NumOperands; ++I)
6780 WideVec =
6781 DAG.getInsertSubvector(dl, WideVec, N->getOperand(I), I * NumInElts);
6782 return WideVec;
6783 }
6784
6785 unsigned WidenNumElts = WidenVT.getVectorNumElements();
6786 unsigned NumInElts = InVT.getVectorNumElements();
6787
6788 // Fall back to use extracts and build vector.
6789 EVT EltVT = WidenVT.getVectorElementType();
6790 SmallVector<SDValue, 16> Ops(WidenNumElts);
6791 unsigned Idx = 0;
6792 for (unsigned i=0; i < NumOperands; ++i) {
6793 SDValue InOp = N->getOperand(i);
6794 if (InputWidened)
6795 InOp = GetWidenedVector(InOp);
6796 for (unsigned j = 0; j < NumInElts; ++j)
6797 Ops[Idx++] = DAG.getExtractVectorElt(dl, EltVT, InOp, j);
6798 }
6799 SDValue UndefVal = DAG.getPOISON(EltVT);
6800 for (; Idx < WidenNumElts; ++Idx)
6801 Ops[Idx] = UndefVal;
6802 return DAG.getBuildVector(WidenVT, dl, Ops);
6803}
6804
6805SDValue DAGTypeLegalizer::WidenVecRes_INSERT_SUBVECTOR(SDNode *N) {
6806 EVT VT = N->getValueType(0);
6807 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6808 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
6809 SDValue InOp2 = N->getOperand(1);
6810 SDValue Idx = N->getOperand(2);
6811 SDLoc dl(N);
6812 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WidenVT, InOp1, InOp2, Idx);
6813}
6814
6815SDValue DAGTypeLegalizer::WidenVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
6816 EVT VT = N->getValueType(0);
6817 EVT EltVT = VT.getVectorElementType();
6818 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6819 SDValue InOp = N->getOperand(0);
6820 SDValue Idx = N->getOperand(1);
6821 SDLoc dl(N);
6822
6823 auto InOpTypeAction = getTypeAction(InOp.getValueType());
6824 if (InOpTypeAction == TargetLowering::TypeWidenVector)
6825 InOp = GetWidenedVector(InOp);
6826
6827 EVT InVT = InOp.getValueType();
6828
6829 // Check if we can just return the input vector after widening.
6830 uint64_t IdxVal = Idx->getAsZExtVal();
6831 if (IdxVal == 0 && InVT == WidenVT)
6832 return InOp;
6833
6834 // Check if we can extract from the vector.
6835 unsigned WidenNumElts = WidenVT.getVectorMinNumElements();
6836 unsigned InNumElts = InVT.getVectorMinNumElements();
6837 unsigned VTNumElts = VT.getVectorMinNumElements();
6838 assert(IdxVal % VTNumElts == 0 &&
6839 "Expected Idx to be a multiple of subvector minimum vector length");
6840 if (IdxVal % WidenNumElts == 0 && IdxVal + WidenNumElts < InNumElts)
6841 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, InOp, Idx);
6842
6843 if (VT.isScalableVector()) {
6844 // Try to split the operation up into smaller extracts and concat the
6845 // results together, e.g.
6846 // nxv6i64 extract_subvector(nxv12i64, 6)
6847 // <->
6848 // nxv8i64 concat(
6849 // nxv2i64 extract_subvector(nxv16i64, 6)
6850 // nxv2i64 extract_subvector(nxv16i64, 8)
6851 // nxv2i64 extract_subvector(nxv16i64, 10)
6852 // undef)
6853 unsigned GCD = std::gcd(VTNumElts, WidenNumElts);
6854 assert((IdxVal % GCD) == 0 && "Expected Idx to be a multiple of the broken "
6855 "down type's element count");
6856 EVT PartVT = EVT::getVectorVT(*DAG.getContext(), EltVT,
6858 // Avoid recursion around e.g. nxv1i8.
6859 if (getTypeAction(PartVT) != TargetLowering::TypeWidenVector) {
6861 unsigned I = 0;
6862 for (; I < VTNumElts / GCD; ++I)
6863 Parts.push_back(
6864 DAG.getExtractSubvector(dl, PartVT, InOp, IdxVal + I * GCD));
6865 for (; I < WidenNumElts / GCD; ++I)
6866 Parts.push_back(DAG.getPOISON(PartVT));
6867
6868 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, Parts);
6869 }
6870
6871 // Fallback to extracting through memory.
6872
6873 Align Alignment = DAG.getReducedAlign(InVT, /*UseABI=*/false);
6874 SDValue StackPtr = DAG.CreateStackTemporary(InVT.getStoreSize(), Alignment);
6875 MachineFunction &MF = DAG.getMachineFunction();
6876 int FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
6877 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
6878
6879 MachineMemOperand *StoreMMO = MF.getMachineMemOperand(
6882 MachineMemOperand *LoadMMO = MF.getMachineMemOperand(
6885
6886 // Write out the input vector.
6887 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InOp, StackPtr, StoreMMO);
6888
6889 // Build a mask to match the length of the non-widened result.
6890 SDValue Mask =
6891 DAG.getMaskFromElementCount(dl, WidenVT, VT.getVectorElementCount());
6892
6893 // Read back the sub-vector setting the remaining lanes to poison.
6894 StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, InVT, VT, Idx);
6895 return DAG.getMaskedLoad(
6896 WidenVT, dl, Ch, StackPtr, DAG.getPOISON(StackPtr.getValueType()), Mask,
6897 DAG.getPOISON(WidenVT), VT, LoadMMO, ISD::UNINDEXED, ISD::NON_EXTLOAD);
6898 }
6899
6900 // We could try widening the input to the right length but for now, extract
6901 // the original elements, fill the rest with undefs and build a vector.
6902 SmallVector<SDValue, 16> Ops(WidenNumElts);
6903 unsigned i;
6904 for (i = 0; i < VTNumElts; ++i)
6905 Ops[i] = DAG.getExtractVectorElt(dl, EltVT, InOp, IdxVal + i);
6906
6907 SDValue UndefVal = DAG.getPOISON(EltVT);
6908 for (; i < WidenNumElts; ++i)
6909 Ops[i] = UndefVal;
6910 return DAG.getBuildVector(WidenVT, dl, Ops);
6911}
6912
6913SDValue DAGTypeLegalizer::WidenVecRes_AssertZext(SDNode *N) {
6914 SDValue InOp = ModifyToType(
6915 N->getOperand(0),
6916 TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)), true);
6917 return DAG.getNode(ISD::AssertZext, SDLoc(N), InOp.getValueType(), InOp,
6918 N->getOperand(1));
6919}
6920
6921SDValue DAGTypeLegalizer::WidenVecRes_INSERT_VECTOR_ELT(SDNode *N) {
6922 SDValue InOp = GetWidenedVector(N->getOperand(0));
6923 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N),
6924 InOp.getValueType(), InOp,
6925 N->getOperand(1), N->getOperand(2));
6926}
6927
6928/// Either return the same load or provide appropriate casts
6929/// from the load and return that.
6930static SDValue coerceLoadedValue(SDValue LdOp, EVT FirstVT, EVT WidenVT,
6931 TypeSize LdWidth, TypeSize FirstVTWidth,
6932 SDLoc dl, SelectionDAG &DAG) {
6933 assert(TypeSize::isKnownLE(LdWidth, FirstVTWidth) &&
6934 "Load width must be less than or equal to first value type width");
6935 TypeSize WidenWidth = WidenVT.getSizeInBits();
6936 if (!FirstVT.isVector()) {
6937 unsigned NumElts =
6938 WidenWidth.getFixedValue() / FirstVTWidth.getFixedValue();
6939 EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), FirstVT, NumElts);
6940 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT, LdOp);
6941 return DAG.getNode(ISD::BITCAST, dl, WidenVT, VecOp);
6942 }
6943 assert(FirstVT == WidenVT && "First value type must equal widen value type");
6944 return LdOp;
6945}
6946
6947/// Inverse of coerceLoadedValue: pull a FirstVT-sized scalar/vector out of the
6948/// widened value so it can be issued in a single atomic store.
6949static SDValue coerceStoredValue(SDValue StVal, EVT FirstVT, EVT WidenVT,
6950 TypeSize FirstVTWidth, const SDLoc &dl,
6951 SelectionDAG &DAG) {
6952 TypeSize WidenWidth = WidenVT.getSizeInBits();
6953 if (!FirstVT.isVector()) {
6954 unsigned NumElts =
6955 WidenWidth.getFixedValue() / FirstVTWidth.getFixedValue();
6956 EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), FirstVT, NumElts);
6957 SDValue VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, StVal);
6958 return DAG.getExtractVectorElt(dl, FirstVT, VecOp, 0);
6959 }
6960 assert(FirstVT == WidenVT && "First value type must equal widen value type");
6961 return StVal;
6962}
6963
6964static std::optional<EVT> findMemType(SelectionDAG &DAG,
6965 const TargetLowering &TLI, unsigned Width,
6966 EVT WidenVT, unsigned Align,
6967 unsigned WidenEx);
6968
6969SDValue DAGTypeLegalizer::WidenVecRes_ATOMIC_LOAD(AtomicSDNode *LD) {
6970 EVT WidenVT =
6971 TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
6972 EVT LdVT = LD->getMemoryVT();
6973 SDLoc dl(LD);
6974
6975 // Load information
6976 SDValue Chain = LD->getChain();
6977 SDValue BasePtr = LD->getBasePtr();
6978
6979 TypeSize LdWidth = LdVT.getSizeInBits();
6980 TypeSize WidenWidth = WidenVT.getSizeInBits();
6981 TypeSize WidthDiff = WidenWidth - LdWidth;
6982
6983 // Find the vector type that can load from.
6984 std::optional<EVT> FirstVT =
6985 findMemType(DAG, TLI, LdWidth.getKnownMinValue(), WidenVT, /*LdAlign=*/0,
6986 WidthDiff.getKnownMinValue());
6987
6988 if (!FirstVT)
6989 return SDValue();
6990
6991 TypeSize FirstVTWidth = FirstVT->getSizeInBits();
6992
6993 SDValue LdOp = DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, *FirstVT, *FirstVT,
6994 Chain, BasePtr, LD->getMemOperand());
6995
6996 // Load the element with one instruction.
6997 SDValue Result = coerceLoadedValue(LdOp, *FirstVT, WidenVT, LdWidth,
6998 FirstVTWidth, dl, DAG);
6999
7000 // Modified the chain - switch anything that used the old chain to use
7001 // the new one.
7002 ReplaceValueWith(SDValue(LD, 1), LdOp.getValue(1));
7003 return Result;
7004}
7005
7006SDValue DAGTypeLegalizer::WidenVecRes_LOAD(SDNode *N) {
7007 LoadSDNode *LD = cast<LoadSDNode>(N);
7008 ISD::LoadExtType ExtType = LD->getExtensionType();
7009
7010 // A vector must always be stored in memory as-is, i.e. without any padding
7011 // between the elements, since various code depend on it, e.g. in the
7012 // handling of a bitcast of a vector type to int, which may be done with a
7013 // vector store followed by an integer load. A vector that does not have
7014 // elements that are byte-sized must therefore be stored as an integer
7015 // built out of the extracted vector elements.
7016 if (!LD->getMemoryVT().isByteSized()) {
7017 SDValue Value, NewChain;
7018 std::tie(Value, NewChain) = TLI.scalarizeVectorLoad(LD, DAG);
7019 ReplaceValueWith(SDValue(LD, 0), Value);
7020 ReplaceValueWith(SDValue(LD, 1), NewChain);
7021 return SDValue();
7022 }
7023
7024 // Generate a vector-predicated load if it is custom/legal on the target. To
7025 // avoid possible recursion, only do this if the widened mask type is legal.
7026 // FIXME: Not all targets may support EVL in VP_LOAD. These will have been
7027 // removed from the IR by the ExpandVectorPredication pass but we're
7028 // reintroducing them here.
7029 EVT VT = LD->getValueType(0);
7030 EVT WideVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
7031 EVT WideMaskVT = getSetCCResultType(WideVT);
7032
7033 if (ExtType == ISD::NON_EXTLOAD &&
7034 TLI.isOperationLegalOrCustom(ISD::VP_LOAD, WideVT) &&
7035 TLI.isTypeLegal(WideMaskVT)) {
7036 SDLoc DL(N);
7037 SDValue Mask = DAG.getAllOnesConstant(DL, WideMaskVT);
7038 SDValue EVL = DAG.getElementCount(DL, TLI.getVPExplicitVectorLengthTy(),
7040 SDValue NewLoad =
7041 DAG.getLoadVP(LD->getAddressingMode(), ISD::NON_EXTLOAD, WideVT, DL,
7042 LD->getChain(), LD->getBasePtr(), LD->getOffset(), Mask,
7043 EVL, LD->getMemoryVT(), LD->getMemOperand());
7044
7045 // Modified the chain - switch anything that used the old chain to use
7046 // the new one.
7047 ReplaceValueWith(SDValue(N, 1), NewLoad.getValue(1));
7048
7049 return NewLoad;
7050 }
7051
7053 SmallVector<SDValue, 16> LdChain; // Chain for the series of load
7054 if (ExtType != ISD::NON_EXTLOAD)
7055 Result = GenWidenVectorExtLoads(LdChain, LD, ExtType);
7056 else
7057 Result = GenWidenVectorLoads(LdChain, LD);
7058
7059 if (Result) {
7060 // If we generate a single load, we can use that for the chain. Otherwise,
7061 // build a factor node to remember the multiple loads are independent and
7062 // chain to that.
7063 SDValue NewChain;
7064 if (LdChain.size() == 1)
7065 NewChain = LdChain[0];
7066 else
7067 NewChain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other, LdChain);
7068
7069 // Modified the chain - switch anything that used the old chain to use
7070 // the new one.
7071 ReplaceValueWith(SDValue(N, 1), NewChain);
7072
7073 return Result;
7074 }
7075
7076 if (VT.isVector()) {
7077 // If all else fails replace the load with a wide masked load.
7078 SDLoc DL(N);
7079 SDValue Mask =
7080 DAG.getMaskFromElementCount(DL, WideVT, VT.getVectorElementCount());
7081
7082 SDValue NewLoad = DAG.getMaskedLoad(
7083 WideVT, DL, LD->getChain(), LD->getBasePtr(), LD->getOffset(), Mask,
7084 DAG.getPOISON(WideVT), LD->getMemoryVT(), LD->getMemOperand(),
7085 LD->getAddressingMode(), LD->getExtensionType());
7086
7087 ReplaceValueWith(SDValue(N, 1), NewLoad.getValue(1));
7088 return NewLoad;
7089 }
7090
7091 report_fatal_error("Unable to widen vector load");
7092}
7093
7094SDValue DAGTypeLegalizer::WidenVecRes_VP_LOAD(VPLoadSDNode *N) {
7095 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7096 SDValue Mask = N->getMask();
7097 SDValue EVL = N->getVectorLength();
7098 ISD::LoadExtType ExtType = N->getExtensionType();
7099 SDLoc dl(N);
7100
7101 // The mask should be widened as well
7102 assert(getTypeAction(Mask.getValueType()) ==
7104 "Unable to widen binary VP op");
7105 Mask = GetWidenedVector(Mask);
7106 assert(Mask.getValueType().getVectorElementCount() ==
7107 TLI.getTypeToTransformTo(*DAG.getContext(), Mask.getValueType())
7108 .getVectorElementCount() &&
7109 "Unable to widen vector load");
7110
7111 SDValue Res =
7112 DAG.getLoadVP(N->getAddressingMode(), ExtType, WidenVT, dl, N->getChain(),
7113 N->getBasePtr(), N->getOffset(), Mask, EVL,
7114 N->getMemoryVT(), N->getMemOperand(), N->isExpandingLoad());
7115 // Legalize the chain result - switch anything that used the old chain to
7116 // use the new one.
7117 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
7118 return Res;
7119}
7120
7121SDValue DAGTypeLegalizer::WidenVecRes_VP_LOAD_FF(VPLoadFFSDNode *N) {
7122 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7123 SDValue Mask = N->getMask();
7124 SDValue EVL = N->getVectorLength();
7125 SDLoc dl(N);
7126
7127 // The mask should be widened as well
7128 assert(getTypeAction(Mask.getValueType()) ==
7130 "Unable to widen binary VP op");
7131 Mask = GetWidenedVector(Mask);
7132 assert(Mask.getValueType().getVectorElementCount() ==
7133 TLI.getTypeToTransformTo(*DAG.getContext(), Mask.getValueType())
7134 .getVectorElementCount() &&
7135 "Unable to widen vector load");
7136
7137 SDValue Res = DAG.getLoadFFVP(WidenVT, dl, N->getChain(), N->getBasePtr(),
7138 Mask, EVL, N->getMemOperand());
7139 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
7140 ReplaceValueWith(SDValue(N, 2), Res.getValue(2));
7141 return Res;
7142}
7143
7144SDValue DAGTypeLegalizer::WidenVecRes_VP_STRIDED_LOAD(VPStridedLoadSDNode *N) {
7145 SDLoc DL(N);
7146
7147 // The mask should be widened as well
7148 SDValue Mask = N->getMask();
7149 assert(getTypeAction(Mask.getValueType()) ==
7151 "Unable to widen VP strided load");
7152 Mask = GetWidenedVector(Mask);
7153
7154 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7155 assert(Mask.getValueType().getVectorElementCount() ==
7156 WidenVT.getVectorElementCount() &&
7157 "Data and mask vectors should have the same number of elements");
7158
7159 SDValue Res = DAG.getStridedLoadVP(
7160 N->getAddressingMode(), N->getExtensionType(), WidenVT, DL, N->getChain(),
7161 N->getBasePtr(), N->getOffset(), N->getStride(), Mask,
7162 N->getVectorLength(), N->getMemoryVT(), N->getMemOperand(),
7163 N->isExpandingLoad());
7164
7165 // Legalize the chain result - switch anything that used the old chain to
7166 // use the new one.
7167 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
7168 return Res;
7169}
7170
7171SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_COMPRESS(SDNode *N) {
7172 SDValue Vec = N->getOperand(0);
7173 SDValue Mask = N->getOperand(1);
7174 SDValue Passthru = N->getOperand(2);
7175 EVT WideVecVT =
7176 TLI.getTypeToTransformTo(*DAG.getContext(), Vec.getValueType());
7177 EVT WideMaskVT = EVT::getVectorVT(*DAG.getContext(),
7178 Mask.getValueType().getVectorElementType(),
7179 WideVecVT.getVectorElementCount());
7180
7181 SDValue WideVec = ModifyToType(Vec, WideVecVT);
7182 SDValue WideMask = ModifyToType(Mask, WideMaskVT, /*FillWithZeroes=*/true);
7183 SDValue WidePassthru = ModifyToType(Passthru, WideVecVT);
7184 return DAG.getNode(ISD::VECTOR_COMPRESS, SDLoc(N), WideVecVT, WideVec,
7185 WideMask, WidePassthru);
7186}
7187
7188SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_SHUFFLE_VAR(SDNode *N) {
7189 // Only indices that were out of range can select the new source elements.
7190 SDValue Src = GetWidenedVector(N->getOperand(0));
7191 EVT WideVT = Src.getValueType();
7192 EVT WideMaskVT = WideVT.changeVectorElementType(
7193 *DAG.getContext(),
7194 N->getOperand(1).getValueType().getVectorElementType());
7195 SDValue Mask = ModifyToType(N->getOperand(1), WideMaskVT);
7196 return DAG.getNode(ISD::VECTOR_SHUFFLE_VAR, SDLoc(N), WideVT, Src, Mask);
7197}
7198
7199SDValue DAGTypeLegalizer::WidenVecRes_MLOAD(MaskedLoadSDNode *N) {
7200 EVT VT = N->getValueType(0);
7201 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
7202 SDValue Mask = N->getMask();
7203 EVT MaskVT = Mask.getValueType();
7204 SDValue PassThru = GetWidenedVector(N->getPassThru());
7205 ISD::LoadExtType ExtType = N->getExtensionType();
7206 SDLoc dl(N);
7207
7208 EVT WideMaskVT =
7209 EVT::getVectorVT(*DAG.getContext(), MaskVT.getVectorElementType(),
7210 WidenVT.getVectorElementCount());
7211
7212 if (ExtType == ISD::NON_EXTLOAD && !N->isExpandingLoad() &&
7213 TLI.isOperationLegalOrCustom(ISD::VP_LOAD, WidenVT) &&
7214 TLI.isTypeLegal(WideMaskVT) &&
7215 // If there is a passthru, we shouldn't use vp.load. However,
7216 // type legalizer will struggle on masked.load with
7217 // scalable vectors, so for scalable vectors, we still use vp.load
7218 // but manually merge the load result with the passthru using vp.select.
7219 (N->getPassThru()->isUndef() || VT.isScalableVector())) {
7220 Mask = DAG.getInsertSubvector(dl, DAG.getPOISON(WideMaskVT), Mask, 0);
7221 SDValue EVL = DAG.getElementCount(dl, TLI.getVPExplicitVectorLengthTy(),
7223 SDValue NewLoad =
7224 DAG.getLoadVP(N->getAddressingMode(), ISD::NON_EXTLOAD, WidenVT, dl,
7225 N->getChain(), N->getBasePtr(), N->getOffset(), Mask, EVL,
7226 N->getMemoryVT(), N->getMemOperand());
7227 SDValue NewVal = NewLoad;
7228
7229 // Manually merge with vselect
7230 if (!N->getPassThru()->isUndef()) {
7231 assert(WidenVT.isScalableVector());
7232 NewVal = DAG.getNode(ISD::VSELECT, dl, WidenVT, Mask, NewVal, PassThru);
7233 // The lanes past EVL are poison.
7234 NewVal = DAG.getNode(ISD::VP_MERGE, dl, WidenVT,
7235 DAG.getAllOnesConstant(dl, WideMaskVT), NewVal,
7236 DAG.getPOISON(WidenVT), EVL);
7237 }
7238
7239 // Modified the chain - switch anything that used the old chain to use
7240 // the new one.
7241 ReplaceValueWith(SDValue(N, 1), NewLoad.getValue(1));
7242
7243 return NewVal;
7244 }
7245
7246 // The mask should be widened as well
7247 Mask = ModifyToType(Mask, WideMaskVT, true);
7248
7249 SDValue Res = DAG.getMaskedLoad(
7250 WidenVT, dl, N->getChain(), N->getBasePtr(), N->getOffset(), Mask,
7251 PassThru, N->getMemoryVT(), N->getMemOperand(), N->getAddressingMode(),
7252 ExtType, N->isExpandingLoad());
7253 // Legalize the chain result - switch anything that used the old chain to
7254 // use the new one.
7255 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
7256 return Res;
7257}
7258
7259SDValue DAGTypeLegalizer::WidenVecRes_MGATHER(MaskedGatherSDNode *N) {
7260
7261 EVT WideVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7262 SDValue Mask = N->getMask();
7263 EVT MaskVT = Mask.getValueType();
7264 SDValue PassThru = GetWidenedVector(N->getPassThru());
7265 SDValue Scale = N->getScale();
7266 ElementCount WideEC = WideVT.getVectorElementCount();
7267 SDLoc dl(N);
7268
7269 // The mask should be widened as well
7270 EVT WideMaskVT = EVT::getVectorVT(*DAG.getContext(),
7271 MaskVT.getVectorElementType(), WideEC);
7272 Mask = ModifyToType(Mask, WideMaskVT, true);
7273
7274 // Widen the Index operand
7275 SDValue Index = N->getIndex();
7276 EVT WideIndexVT = EVT::getVectorVT(
7277 *DAG.getContext(), Index.getValueType().getScalarType(), WideEC);
7278 Index = ModifyToType(Index, WideIndexVT);
7279 SDValue Ops[] = { N->getChain(), PassThru, Mask, N->getBasePtr(), Index,
7280 Scale };
7281
7282 // Widen the MemoryType
7283 EVT WideMemVT = EVT::getVectorVT(*DAG.getContext(),
7284 N->getMemoryVT().getScalarType(), WideEC);
7285 SDValue Res = DAG.getMaskedGather(DAG.getVTList(WideVT, MVT::Other),
7286 WideMemVT, dl, Ops, N->getMemOperand(),
7287 N->getIndexType(), N->getExtensionType());
7288
7289 // Legalize the chain result - switch anything that used the old chain to
7290 // use the new one.
7291 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
7292 return Res;
7293}
7294
7295SDValue DAGTypeLegalizer::WidenVecRes_VP_GATHER(VPGatherSDNode *N) {
7296 EVT WideVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7297 SDValue Mask = N->getMask();
7298 SDValue Scale = N->getScale();
7299 ElementCount WideEC = WideVT.getVectorElementCount();
7300 SDLoc dl(N);
7301
7302 SDValue Index = GetWidenedVector(N->getIndex());
7303 EVT WideMemVT = EVT::getVectorVT(*DAG.getContext(),
7304 N->getMemoryVT().getScalarType(), WideEC);
7305 Mask = GetWidenedMask(Mask, WideEC);
7306
7307 SDValue Ops[] = {N->getChain(), N->getBasePtr(), Index, Scale,
7308 Mask, N->getVectorLength()};
7309 SDValue Res = DAG.getGatherVP(DAG.getVTList(WideVT, MVT::Other), WideMemVT,
7310 dl, Ops, N->getMemOperand(), N->getIndexType());
7311
7312 // Legalize the chain result - switch anything that used the old chain to
7313 // use the new one.
7314 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
7315 return Res;
7316}
7317
7318SDValue DAGTypeLegalizer::WidenVecRes_ScalarOp(SDNode *N) {
7319 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7320 return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, N->getOperand(0));
7321}
7322
7323// Return true is this is a SETCC node or a strict version of it.
7324static inline bool isSETCCOp(unsigned Opcode) {
7325 switch (Opcode) {
7326 case ISD::SETCC:
7327 case ISD::STRICT_FSETCC:
7329 return true;
7330 }
7331 return false;
7332}
7333
7334// Return true if this is a node that could have two SETCCs as operands.
7335static inline bool isLogicalMaskOp(unsigned Opcode) {
7336 switch (Opcode) {
7337 case ISD::AND:
7338 case ISD::OR:
7339 case ISD::XOR:
7340 return true;
7341 }
7342 return false;
7343}
7344
7345// If N is a SETCC or a strict variant of it, return the type
7346// of the compare operands.
7348 unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0;
7349 return N->getOperand(OpNo).getValueType();
7350}
7351
7352// This is used just for the assert in convertMask(). Check that this either
7353// a SETCC or a previously handled SETCC by convertMask().
7354#ifndef NDEBUG
7355static inline bool isSETCCorConvertedSETCC(SDValue N) {
7356 if (N.getOpcode() == ISD::EXTRACT_SUBVECTOR)
7357 N = N.getOperand(0);
7358 else if (N.getOpcode() == ISD::CONCAT_VECTORS) {
7359 for (unsigned i = 1; i < N->getNumOperands(); ++i)
7360 if (!N->getOperand(i)->isUndef())
7361 return false;
7362 N = N.getOperand(0);
7363 }
7364
7365 if (N.getOpcode() == ISD::TRUNCATE)
7366 N = N.getOperand(0);
7367 else if (N.getOpcode() == ISD::SIGN_EXTEND)
7368 N = N.getOperand(0);
7369
7370 if (isLogicalMaskOp(N.getOpcode()))
7371 return isSETCCorConvertedSETCC(N.getOperand(0)) &&
7372 isSETCCorConvertedSETCC(N.getOperand(1));
7373
7374 return (isSETCCOp(N.getOpcode()) || ISD::isBuildVectorOfConstantSDNodes(N));
7375}
7376#endif
7377
7378// Return a mask of vector type MaskVT to replace InMask. Also adjust MaskVT
7379// to ToMaskVT if needed with vector extension or truncation.
7380SDValue DAGTypeLegalizer::convertMask(SDValue InMask, EVT MaskVT,
7381 EVT ToMaskVT) {
7382 // Called from convertMaskTree for SETCC leaf nodes. Re-creates the SETCC with
7383 // result type MaskVT, then sign-extends/truncates and pads to ToMaskVT.
7384 assert(isSETCCorConvertedSETCC(InMask) && "Unexpected mask argument.");
7385
7386 // Make a new Mask node, with a legal result VT.
7387 SDValue Mask;
7389 for (unsigned i = 0, e = InMask->getNumOperands(); i < e; ++i)
7390 Ops.push_back(InMask->getOperand(i));
7391 if (InMask->isStrictFPOpcode()) {
7392 Mask = DAG.getNode(InMask->getOpcode(), SDLoc(InMask),
7393 { MaskVT, MVT::Other }, Ops);
7394 ReplaceValueWith(InMask.getValue(1), Mask.getValue(1));
7395 }
7396 else
7397 Mask = DAG.getNode(InMask->getOpcode(), SDLoc(InMask), MaskVT, Ops,
7398 InMask->getFlags());
7399
7400 return adjustMaskToType(Mask, ToMaskVT);
7401}
7402
7403// Adjust element width (sign-extend/truncate) and element count
7404// (extract/concat) of Mask to match ToMaskVT.
7405SDValue DAGTypeLegalizer::adjustMaskToType(SDValue Mask, EVT ToMaskVT) {
7406 LLVMContext &Ctx = *DAG.getContext();
7407 EVT MaskVT = Mask.getValueType();
7408 unsigned MaskScalarBits = MaskVT.getScalarSizeInBits();
7409 unsigned ToMaskScalBits = ToMaskVT.getScalarSizeInBits();
7410 if (MaskScalarBits < ToMaskScalBits) {
7411 EVT ExtVT = EVT::getVectorVT(Ctx, ToMaskVT.getVectorElementType(),
7412 MaskVT.getVectorNumElements());
7413 Mask = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(Mask), ExtVT, Mask);
7414 } else if (MaskScalarBits > ToMaskScalBits) {
7415 EVT TruncVT = EVT::getVectorVT(Ctx, ToMaskVT.getVectorElementType(),
7416 MaskVT.getVectorNumElements());
7417 Mask = DAG.getNode(ISD::TRUNCATE, SDLoc(Mask), TruncVT, Mask);
7418 }
7419
7420 assert(Mask->getValueType(0).getScalarSizeInBits() ==
7421 ToMaskVT.getScalarSizeInBits() &&
7422 "Mask should have the right element size by now.");
7423
7424 // Adjust Mask to the right number of elements.
7425 unsigned CurrMaskNumEls = Mask->getValueType(0).getVectorNumElements();
7426 if (CurrMaskNumEls > ToMaskVT.getVectorNumElements()) {
7427 Mask = DAG.getExtractSubvector(SDLoc(Mask), ToMaskVT, Mask, 0);
7428 } else if (CurrMaskNumEls < ToMaskVT.getVectorNumElements()) {
7429 unsigned NumSubVecs = (ToMaskVT.getVectorNumElements() / CurrMaskNumEls);
7430 EVT SubVT = Mask->getValueType(0);
7431 SmallVector<SDValue, 16> SubOps(NumSubVecs, DAG.getPOISON(SubVT));
7432 SubOps[0] = Mask;
7433 Mask = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Mask), ToMaskVT, SubOps);
7434 }
7435
7436 assert((Mask->getValueType(0) == ToMaskVT) &&
7437 "A mask of ToMaskVT should have been produced by now.");
7438
7439 return Mask;
7440}
7441
7442// Adjust both operands to a common intermediate mask type, picking a scalar
7443// width that minimizes extend/truncate overhead given the final target ToVT.
7444EVT DAGTypeLegalizer::unifyMaskTypes(SDValue &Op0, bool IsOpLenient0,
7445 SDValue &Op1, bool IsOpLenient1,
7446 EVT ToVT) {
7449 "unifyMaskTypes only handles scalar width differences");
7450
7451 // If only one of the operands lenient type-wise, we can simply
7452 // adjust its type to the other operand's type assuming that this
7453 // adjustment can be folded away.
7454 //
7455 // NOTE: We essentially rely on the fact that further optimizations
7456 // do spot redundant casts in "lenient" cases. If at some
7457 // point we decide that we want to do better here, we can
7458 // postpone converting lenient sub-trees right away and postpone
7459 // it to the moment when we know the best fitting integer type
7460 // to materialize them, and do it there.
7461 if (IsOpLenient0 != IsOpLenient1) {
7462 SDValue *LenientOp, *NonLenientOp;
7463 if (IsOpLenient0) {
7464 LenientOp = &Op0;
7465 NonLenientOp = &Op1;
7466 } else {
7467 LenientOp = &Op1;
7468 NonLenientOp = &Op0;
7469 }
7470 EVT OpVT = NonLenientOp->getValueType();
7471 *LenientOp = adjustMaskToType(*LenientOp, OpVT);
7472 return OpVT;
7473 }
7474
7475 unsigned Bits0 = Op0.getScalarValueSizeInBits();
7476 unsigned Bits1 = Op1.getScalarValueSizeInBits();
7477 unsigned NarrowBits = std::min(Bits0, Bits1);
7478 unsigned WideBits = std::max(Bits0, Bits1);
7479 unsigned ToBits = ToVT.getScalarSizeInBits();
7480 unsigned IntBits = NarrowBits == WideBits ? NarrowBits
7481 : ToBits >= WideBits ? WideBits
7482 : ToBits <= NarrowBits ? NarrowBits
7483 : ToBits;
7484 EVT OpVT = Op0.getValueType().changeVectorElementType(
7485 *DAG.getContext(), EVT::getIntegerVT(*DAG.getContext(), IntBits));
7486 Op0 = adjustMaskToType(Op0, OpVT);
7487 Op1 = adjustMaskToType(Op1, OpVT);
7488 return OpVT;
7489}
7490
7491std::pair<SDValue, bool>
7492DAGTypeLegalizer::convertMaskTreeImpl(SDValue V, EVT ToVT, unsigned Depth) {
7493 // The main idea is to recursively traverse VSELECT's mask that needs
7494 // widening to see if we can avoid unnecessary casts. The problem usually
7495 // stems from a simple fact that SETCC might naturally produce results
7496 // not in i1 (as we model it in LLVM IR) and we can continue using that
7497 // type until we have to switch it up. Another important aspect is that
7498 // "all ones" and "all zeros" constants can be materialized at any type,
7499 // so we can try to utilize that to keep SETCC results at their natural
7500 // types as much as possible.
7501 //
7502 // The algorithm traverses the mask-producing tree of operations that
7503 // retain "mask-vector"-ness of the input (i.e. it remains a vector of
7504 // -1s and 0s).
7505 //
7506 // SETCC1 SETCC2 CONST1 SETCC3 CONST2 CONST3
7507 // | / | / | /
7508 // | / | / | /
7509 // |_____/ |______/ |______/
7510 // | * choose the | * choose SETCC3 | * keep it as final type
7511 // | most fitting | type | but consider it subject to
7512 // | type | / change
7513 // | | /
7514 // | |______________/
7515 // | | * choose SETCC3 type
7516 // | /
7517 // | /
7518 // |_____________/
7519 // | * choose the most fitting type
7520 // | and then cast to the final desired type
7521 // |
7522 // VSELECT
7523 //
7524 if (Depth >= DAG.MaxRecursionDepth)
7525 return {};
7526
7527 // Bail out when encounter the vector element count mismatch.
7528 // It potentially can be just an assertion, but we deliberately try to
7529 // be overly conservative here.
7530 if (V.getValueType().getVectorNumElements() != ToVT.getVectorNumElements())
7531 return {};
7532
7533 unsigned Opcode = V.getOpcode();
7534
7535 // Base case: SETCC produces the mask at its natural type.
7536 if (isSETCCOp(Opcode)) {
7537 EVT MaskVT = getSetCCResultType(getSETCCOperandType(V));
7538 return {convertMask(V, MaskVT, MaskVT), /*IsTypeLenient=*/false};
7539 }
7540
7541 SDLoc DL(V);
7542
7543 // Base case: all-zeros or all-ones BUILD_VECTOR. Type-lenient since these are
7544 // invariant under sign-extend/truncate. Materialize "true" using the
7545 // target's vector boolean contents (e.g. 1 on ZeroOrOne targets).
7547 return {DAG.getConstant(0, DL, ToVT), /*IsTypeLenient=*/true};
7549 return {DAG.getBoolConstant(true, DL, ToVT, ToVT), /*IsTypeLenient=*/true};
7550
7551 // Logical operations (AND/OR/XOR): try picking the best fitting width out
7552 // of children's element widths.
7553 if (isLogicalMaskOp(Opcode)) {
7554 auto [Op0, IsLenientOp0] =
7555 convertMaskTreeImpl(V.getOperand(0), ToVT, Depth + 1);
7556 if (!Op0)
7557 return {};
7558 auto [Op1, IsLenientOp1] =
7559 convertMaskTreeImpl(V.getOperand(1), ToVT, Depth + 1);
7560 if (!Op1)
7561 return {};
7562 EVT OpVT = unifyMaskTypes(Op0, IsLenientOp0, Op1, IsLenientOp1, ToVT);
7563 return {DAG.getNode(Opcode, DL, OpVT, Op0, Op1),
7564 IsLenientOp0 && IsLenientOp1};
7565 }
7566
7567 // FREEZE: widen the operand and re-wrap.
7568 if (Opcode == ISD::FREEZE) {
7569 auto [Inner, IsTypeLenient] =
7570 convertMaskTreeImpl(V.getOperand(0), ToVT, Depth + 1);
7571 if (!Inner)
7572 return {};
7573 return {DAG.getNode(ISD::FREEZE, DL, Inner.getValueType(), Inner),
7574 IsTypeLenient};
7575 }
7576
7577 // Vector shuffle: try inferring the best fitting width from operands.
7578 if (Opcode == ISD::VECTOR_SHUFFLE) {
7579 auto *Shuf = cast<ShuffleVectorSDNode>(V);
7580 auto [Op0, IsLenientOp0] =
7581 convertMaskTreeImpl(V.getOperand(0), ToVT, Depth + 1);
7582 if (!Op0)
7583 return {};
7584 if (V.getOperand(1).isUndef()) {
7585 EVT OpVT = Op0.getValueType();
7586 return {DAG.getVectorShuffle(OpVT, DL, Op0, DAG.getUNDEF(OpVT),
7587 Shuf->getMask()),
7588 IsLenientOp0};
7589 }
7590 auto [Op1, IsLenientOp1] =
7591 convertMaskTreeImpl(V.getOperand(1), ToVT, Depth + 1);
7592 if (!Op1)
7593 return {};
7594 EVT OpVT = unifyMaskTypes(Op0, IsLenientOp0, Op1, IsLenientOp1, ToVT);
7595 return {DAG.getVectorShuffle(OpVT, DL, Op0, Op1, Shuf->getMask()),
7596 IsLenientOp0 && IsLenientOp1};
7597 }
7598
7599 // SELECT/VSELECT: try inferring the best fitting width from operands.
7600 if (Opcode == ISD::SELECT || Opcode == ISD::VSELECT) {
7601 auto [Op1, IsLenientOp1] =
7602 convertMaskTreeImpl(V.getOperand(1), ToVT, Depth + 1);
7603 if (!Op1)
7604 return {};
7605 auto [Op2, IsLenientOp2] =
7606 convertMaskTreeImpl(V.getOperand(2), ToVT, Depth + 1);
7607 if (!Op2)
7608 return {};
7609 EVT OpVT = unifyMaskTypes(Op1, IsLenientOp1, Op2, IsLenientOp2, ToVT);
7610
7611 // We deliberately skip traversing/modifying VSELECT's mask because
7612 //
7613 // a. We only change bitwidth of the operands and it shouldn't affect
7614 // condition on its own.
7615 //
7616 // b. This VSELECT's mask can be widened in an independent traversal
7617 // if needed.
7618 SDValue Cond = V.getOperand(0);
7619 return {DAG.getNode(Opcode, DL, OpVT, Cond, Op1, Op2),
7620 IsLenientOp1 && IsLenientOp2};
7621 }
7622
7623 return {};
7624}
7625
7626SDValue DAGTypeLegalizer::convertMaskTree(SDValue V, EVT ToVT) {
7627 // In general, we are converting from <N x i1> into <M x iW>.
7628 // This would mean that during the tree traversal we need to pay
7629 // attention to both bitwidth and element count, which can be error-prone.
7630 //
7631 // Instead, we split the task in two, we first widen the type of the tree
7632 // and then change the element count.
7633 EVT MaskTreeVT = ToVT.changeVectorElementCount(
7634 *DAG.getContext(), V.getValueType().getVectorElementCount());
7635 auto [Result, _] = convertMaskTreeImpl(V, MaskTreeVT);
7636 if (!Result)
7637 return Result;
7638 return adjustMaskToType(Result, ToVT);
7639}
7640
7641// This method tries to handle some special cases for the vselect mask
7642// and if needed adjusting the mask vector type to match that of the VSELECT.
7643// Without it, many cases end up with scalarization of the SETCC, with many
7644// unnecessary instructions.
7645SDValue DAGTypeLegalizer::WidenVSELECTMask(SDNode *N) {
7646 LLVMContext &Ctx = *DAG.getContext();
7647 SDValue Cond = N->getOperand(0);
7648
7649 if (N->getOpcode() != ISD::VSELECT)
7650 return SDValue();
7651
7652 // If this is a splitted VSELECT that was previously already handled, do
7653 // nothing.
7654 EVT CondVT = Cond->getValueType(0);
7655 if (CondVT.getScalarSizeInBits() != 1)
7656 return SDValue();
7657
7658 EVT VSelVT = N->getValueType(0);
7659
7660 // This method can't handle scalable vector types.
7661 // FIXME: This support could be added in the future.
7662 if (VSelVT.isScalableVector())
7663 return SDValue();
7664
7665 // Only handle vector types which are a power of 2.
7666 if (!isPowerOf2_64(VSelVT.getSizeInBits()))
7667 return SDValue();
7668
7669 // Don't touch if this will be scalarized.
7670 EVT FinalVT = VSelVT;
7671 while (getTypeAction(FinalVT) == TargetLowering::TypeSplitVector)
7672 FinalVT = FinalVT.getHalfNumVectorElementsVT(Ctx);
7673
7674 if (FinalVT.getVectorNumElements() == 1)
7675 return SDValue();
7676
7677 // If there is support for an i1 vector mask, don't touch.
7678 if (isSETCCOp(Cond.getOpcode())) {
7679 EVT SetCCOpVT = getSETCCOperandType(Cond);
7680 while (TLI.getTypeAction(Ctx, SetCCOpVT) != TargetLowering::TypeLegal)
7681 SetCCOpVT = TLI.getTypeToTransformTo(Ctx, SetCCOpVT);
7682 EVT SetCCResVT = getSetCCResultType(SetCCOpVT);
7683 if (SetCCResVT.getScalarSizeInBits() == 1)
7684 return SDValue();
7685 } else if (CondVT.getScalarType() == MVT::i1) {
7686 // If there is support for an i1 vector mask (or only scalar i1 conditions),
7687 // don't touch.
7688 while (TLI.getTypeAction(Ctx, CondVT) != TargetLowering::TypeLegal)
7689 CondVT = TLI.getTypeToTransformTo(Ctx, CondVT);
7690
7691 if (CondVT.getScalarType() == MVT::i1)
7692 return SDValue();
7693 }
7694
7695 // Widen the vselect result type if needed.
7696 if (getTypeAction(VSelVT) == TargetLowering::TypeWidenVector)
7697 VSelVT = TLI.getTypeToTransformTo(Ctx, VSelVT);
7698
7699 // The mask of the VSELECT should have integer elements.
7700 EVT ToMaskVT = VSelVT;
7701 if (!ToMaskVT.getScalarType().isInteger())
7702 ToMaskVT = ToMaskVT.changeVectorElementTypeToInteger();
7703
7704 // Try to recursively widen the mask expression tree to the target type.
7705 return convertMaskTree(Cond, ToMaskVT);
7706}
7707
7708SDValue DAGTypeLegalizer::WidenVecRes_Select(SDNode *N) {
7709 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7710 ElementCount WidenEC = WidenVT.getVectorElementCount();
7711
7712 SDValue Cond1 = N->getOperand(0);
7713 EVT CondVT = Cond1.getValueType();
7714 unsigned Opcode = N->getOpcode();
7715 if (CondVT.isVector()) {
7716 if (SDValue WideCond = WidenVSELECTMask(N)) {
7717 SDValue InOp1 = GetWidenedVector(N->getOperand(1));
7718 SDValue InOp2 = GetWidenedVector(N->getOperand(2));
7719 assert(InOp1.getValueType() == WidenVT && InOp2.getValueType() == WidenVT);
7720 return DAG.getNode(Opcode, SDLoc(N), WidenVT, WideCond, InOp1, InOp2);
7721 }
7722
7723 EVT CondEltVT = CondVT.getVectorElementType();
7724 EVT CondWidenVT = EVT::getVectorVT(*DAG.getContext(), CondEltVT, WidenEC);
7725 if (getTypeAction(CondVT) == TargetLowering::TypeWidenVector)
7726 Cond1 = GetWidenedVector(Cond1);
7727
7728 // If we have to split the condition there is no point in widening the
7729 // select. This would result in an cycle of widening the select ->
7730 // widening the condition operand -> splitting the condition operand ->
7731 // splitting the select -> widening the select. Instead split this select
7732 // further and widen the resulting type.
7733 if (getTypeAction(CondVT) == TargetLowering::TypeSplitVector) {
7734 SDValue SplitSelect = SplitVecOp_VSELECT(N, 0);
7735 SDValue Res = ModifyToType(SplitSelect, WidenVT);
7736 return Res;
7737 }
7738
7739 if (Cond1.getValueType() != CondWidenVT)
7740 Cond1 = ModifyToType(Cond1, CondWidenVT);
7741 }
7742
7743 SDValue InOp1 = GetWidenedVector(N->getOperand(1));
7744 SDValue InOp2 = GetWidenedVector(N->getOperand(2));
7745 assert(InOp1.getValueType() == WidenVT && InOp2.getValueType() == WidenVT);
7746 if (Opcode == ISD::VP_MERGE)
7747 return DAG.getNode(Opcode, SDLoc(N), WidenVT, Cond1, InOp1, InOp2,
7748 N->getOperand(3));
7749 return DAG.getNode(Opcode, SDLoc(N), WidenVT, Cond1, InOp1, InOp2);
7750}
7751
7752SDValue DAGTypeLegalizer::WidenVecRes_SELECT_CC(SDNode *N) {
7753 SDValue InOp1 = GetWidenedVector(N->getOperand(2));
7754 SDValue InOp2 = GetWidenedVector(N->getOperand(3));
7755 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
7756 InOp1.getValueType(), N->getOperand(0),
7757 N->getOperand(1), InOp1, InOp2, N->getOperand(4));
7758}
7759
7760SDValue DAGTypeLegalizer::WidenVecRes_UNDEF(SDNode *N) {
7761 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7762 return DAG.getUNDEF(WidenVT);
7763}
7764
7765SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N) {
7766 EVT VT = N->getValueType(0);
7767 SDLoc dl(N);
7768
7769 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
7770 unsigned NumElts = VT.getVectorNumElements();
7771 unsigned WidenNumElts = WidenVT.getVectorNumElements();
7772
7773 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
7774 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
7775
7776 // Adjust mask based on new input vector length.
7777 SmallVector<int, 16> NewMask(WidenNumElts, -1);
7778 for (unsigned i = 0; i != NumElts; ++i) {
7779 int Idx = N->getMaskElt(i);
7780 if (Idx < (int)NumElts)
7781 NewMask[i] = Idx;
7782 else
7783 NewMask[i] = Idx - NumElts + WidenNumElts;
7784 }
7785 return DAG.getVectorShuffle(WidenVT, dl, InOp1, InOp2, NewMask);
7786}
7787
7788SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_REVERSE(SDNode *N) {
7789 EVT VT = N->getValueType(0);
7790 EVT EltVT = VT.getVectorElementType();
7791 SDLoc dl(N);
7792
7793 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
7794 SDValue OpValue = GetWidenedVector(N->getOperand(0));
7795 assert(WidenVT == OpValue.getValueType() && "Unexpected widened vector type");
7796
7797 SDValue ReverseVal = DAG.getNode(ISD::VECTOR_REVERSE, dl, WidenVT, OpValue);
7798 unsigned WidenNumElts = WidenVT.getVectorMinNumElements();
7799 unsigned VTNumElts = VT.getVectorMinNumElements();
7800 unsigned IdxVal = WidenNumElts - VTNumElts;
7801
7802 if (VT.isScalableVector()) {
7803 // Try to split the 'Widen ReverseVal' into smaller extracts and concat the
7804 // results together, e.g.(nxv6i64 -> nxv8i64)
7805 // nxv8i64 vector_reverse
7806 // <->
7807 // nxv8i64 concat(
7808 // nxv2i64 extract_subvector(nxv8i64, 2)
7809 // nxv2i64 extract_subvector(nxv8i64, 4)
7810 // nxv2i64 extract_subvector(nxv8i64, 6)
7811 // nxv2i64 undef)
7812
7813 unsigned GCD = std::gcd(VTNumElts, WidenNumElts);
7814 EVT PartVT = EVT::getVectorVT(*DAG.getContext(), EltVT,
7816 assert((IdxVal % GCD) == 0 && "Expected Idx to be a multiple of the broken "
7817 "down type's element count");
7819 unsigned i = 0;
7820 for (; i < VTNumElts / GCD; ++i)
7821 Parts.push_back(
7822 DAG.getExtractSubvector(dl, PartVT, ReverseVal, IdxVal + i * GCD));
7823 for (; i < WidenNumElts / GCD; ++i)
7824 Parts.push_back(DAG.getPOISON(PartVT));
7825
7826 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, Parts);
7827 }
7828
7829 // Use VECTOR_SHUFFLE to combine new vector from 'ReverseVal' for
7830 // fixed-vectors.
7831 SmallVector<int, 16> Mask(WidenNumElts, -1);
7832 std::iota(Mask.begin(), Mask.begin() + VTNumElts, IdxVal);
7833
7834 return DAG.getVectorShuffle(WidenVT, dl, ReverseVal, DAG.getPOISON(WidenVT),
7835 Mask);
7836}
7837
7838SDValue DAGTypeLegalizer::WidenVecRes_GET_ACTIVE_LANE_MASK(SDNode *N) {
7839 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7840 return DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, SDLoc(N), NVT, N->ops());
7841}
7842
7843void DAGTypeLegalizer::WidenVecRes_VECTOR_INTERLEAVE(SDNode *N) {
7844 EVT VT = N->getValueType(0);
7845 EVT EltVT = VT.getVectorElementType();
7846 ElementCount OrigEC = VT.getVectorElementCount();
7847 unsigned Factor = N->getNumOperands();
7848 SDLoc DL(N);
7849
7850 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
7851 ElementCount WidenEC = WidenVT.getVectorElementCount();
7852
7853 SmallVector<SDValue, 8> WidenOps(Factor);
7854 for (unsigned Idx = 0U; Idx < Factor; ++Idx)
7855 WidenOps[Idx] = GetWidenedVector(N->getOperand(Idx));
7856
7857 SmallVector<EVT, 8> WidenVTs(Factor, WidenVT);
7858 SDValue Interleaved =
7859 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, WidenVTs, WidenOps);
7860
7861 EVT PackedWidenVT =
7862 EVT::getVectorVT(*DAG.getContext(), EltVT, WidenEC * Factor);
7863 SmallVector<SDValue, 8> Slices(Factor);
7864 for (unsigned Idx = 0; Idx != Factor; ++Idx)
7865 Slices[Idx] = Interleaved.getValue(Idx);
7866
7867 SDValue Packed = DAG.getNode(ISD::CONCAT_VECTORS, DL, PackedWidenVT, Slices);
7868
7869 for (unsigned Idx = 0U; Idx < Factor; ++Idx) {
7870 SDValue Narrow = DAG.getExtractSubvector(DL, VT, Packed,
7871 OrigEC.getKnownMinValue() * Idx);
7872 SDValue Wide =
7873 DAG.getInsertSubvector(DL, DAG.getPOISON(WidenVT), Narrow, /*Idx=*/0U);
7874 SetWidenedVector(SDValue(N, Idx), Wide);
7875 }
7876}
7877
7878SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_MATCH(SDNode *N) {
7879 SDLoc DL(N);
7880 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7881 EVT SourceVT = N->getOperand(0).getValueType();
7882 EVT WideSourceVT =
7883 EVT::getVectorVT(*DAG.getContext(), SourceVT.getVectorElementType(),
7884 WidenVT.getVectorElementCount());
7885
7886 SDValue WideSource = DAG.getInsertSubvector(DL, DAG.getUNDEF(WideSourceVT),
7887 N->getOperand(0), 0);
7888 SDValue WideMask = DAG.getInsertSubvector(DL, DAG.getConstant(0, DL, WidenVT),
7889 N->getOperand(2), 0);
7890 return DAG.getNode(ISD::VECTOR_MATCH, DL, WidenVT, WideSource,
7891 N->getOperand(1), WideMask, N->getFlags());
7892}
7893
7894void DAGTypeLegalizer::WidenVecRes_VECTOR_DEINTERLEAVE(SDNode *N) {
7895 EVT VT = N->getValueType(0);
7896 EVT EltVT = VT.getVectorElementType();
7897 ElementCount OrigEC = VT.getVectorElementCount();
7898 unsigned Factor = N->getNumOperands();
7899 SDLoc DL(N);
7900
7901 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
7902 ElementCount WidenEC = WidenVT.getVectorElementCount();
7903 // We cannot just use the widened operands directly: since they might be
7904 // individually widened, using them directly will result in de-interleaving
7905 // the "padded" lanes that sit in the middle of the vector. Instead, we should
7906 // not concat the widened operands but the original ones to effectively
7907 // generate a "packed" concated and widened vector, before extracting new
7908 // operand vectors with the widened type.
7909 EVT PackedWidenVT =
7910 EVT::getVectorVT(*DAG.getContext(), EltVT, WidenEC * Factor);
7911 EVT ConcatVT = EVT::getVectorVT(*DAG.getContext(), EltVT, OrigEC * Factor);
7912 SDValue ConcatOp = DAG.getNode(ISD::CONCAT_VECTORS, DL, ConcatVT, N->ops());
7913 SDValue PackedWidenVec = DAG.getInsertSubvector(
7914 DL, DAG.getUNDEF(PackedWidenVT), ConcatOp, /*Idx=*/0U);
7915
7916 // Extract the new widened operand vectors.
7917 SmallVector<SDValue, 8> NewOps(Factor, SDValue());
7918 for (unsigned Idx = 0U; Idx < Factor; ++Idx) {
7919 NewOps[Idx] = DAG.getExtractSubvector(DL, WidenVT, PackedWidenVec,
7920 WidenEC.getKnownMinValue() * Idx);
7921 }
7922
7923 SmallVector<EVT, 8> NewVTs(Factor, WidenVT);
7924 SDValue NewRes = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL, NewVTs, NewOps);
7925 // Set the widened results manually.
7926 for (unsigned Idx = 0U; Idx < Factor; ++Idx)
7927 SetWidenedVector(SDValue(N, Idx), NewRes.getValue(Idx));
7928}
7929
7930SDValue DAGTypeLegalizer::WidenVecRes_SETCC(SDNode *N) {
7931 assert(N->getValueType(0).isVector() &&
7932 N->getOperand(0).getValueType().isVector() &&
7933 "Operands must be vectors");
7934 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7935 ElementCount WidenEC = WidenVT.getVectorElementCount();
7936
7937 SDValue InOp1 = N->getOperand(0);
7938 EVT InVT = InOp1.getValueType();
7939 assert(InVT.isVector() && "can not widen non-vector type");
7940 EVT WidenInVT =
7941 EVT::getVectorVT(*DAG.getContext(), InVT.getVectorElementType(), WidenEC);
7942
7943 // The input and output types often differ here, and it could be that while
7944 // we'd prefer to widen the result type, the input operands have been split.
7945 // In this case, we also need to split the result of this node as well.
7946 if (getTypeAction(InVT) == TargetLowering::TypeSplitVector) {
7947 SDValue SplitVSetCC = SplitVecOp_VSETCC(N);
7948 SDValue Res = ModifyToType(SplitVSetCC, WidenVT);
7949 return Res;
7950 }
7951
7952 // If the inputs also widen, handle them directly. Otherwise widen by hand.
7953 SDValue InOp2 = N->getOperand(1);
7954 if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
7955 InOp1 = GetWidenedVector(InOp1);
7956 InOp2 = GetWidenedVector(InOp2);
7957 } else {
7958 SDValue Poison = DAG.getPOISON(WidenInVT);
7959 SDValue ZeroIdx = DAG.getVectorIdxConstant(0, SDLoc(N));
7960 InOp1 = DAG.getNode(ISD::INSERT_SUBVECTOR, SDLoc(N), WidenInVT, Poison,
7961 InOp1, ZeroIdx);
7962 InOp2 = DAG.getNode(ISD::INSERT_SUBVECTOR, SDLoc(N), WidenInVT, Poison,
7963 InOp2, ZeroIdx);
7964 }
7965
7966 // Assume that the input and output will be widen appropriately. If not,
7967 // we will have to unroll it at some point.
7968 assert(InOp1.getValueType() == WidenInVT &&
7969 InOp2.getValueType() == WidenInVT &&
7970 "Input not widened to expected type!");
7971 (void)WidenInVT;
7972 return DAG.getNode(ISD::SETCC, SDLoc(N), WidenVT, InOp1, InOp2,
7973 N->getOperand(2));
7974}
7975
7976SDValue DAGTypeLegalizer::WidenVecRes_STRICT_FSETCC(SDNode *N) {
7977 assert(N->getValueType(0).isVector() &&
7978 N->getOperand(1).getValueType().isVector() &&
7979 "Operands must be vectors");
7980 EVT VT = N->getValueType(0);
7981 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
7982 unsigned WidenNumElts = WidenVT.getVectorNumElements();
7983 unsigned NumElts = VT.getVectorNumElements();
7984 EVT EltVT = VT.getVectorElementType();
7985
7986 SDLoc dl(N);
7987 SDValue Chain = N->getOperand(0);
7988 SDValue LHS = N->getOperand(1);
7989 SDValue RHS = N->getOperand(2);
7990 SDValue CC = N->getOperand(3);
7991 EVT TmpEltVT = LHS.getValueType().getVectorElementType();
7992
7993 // Fully unroll and reassemble.
7994 SmallVector<SDValue, 8> Scalars(WidenNumElts, DAG.getPOISON(EltVT));
7995 SmallVector<SDValue, 8> Chains(NumElts);
7996 for (unsigned i = 0; i != NumElts; ++i) {
7997 SDValue LHSElem = DAG.getExtractVectorElt(dl, TmpEltVT, LHS, i);
7998 SDValue RHSElem = DAG.getExtractVectorElt(dl, TmpEltVT, RHS, i);
7999
8000 Scalars[i] = DAG.getNode(N->getOpcode(), dl, {MVT::i1, MVT::Other},
8001 {Chain, LHSElem, RHSElem, CC});
8002 Chains[i] = Scalars[i].getValue(1);
8003 Scalars[i] = DAG.getSelect(dl, EltVT, Scalars[i],
8004 DAG.getBoolConstant(true, dl, EltVT, VT),
8005 DAG.getBoolConstant(false, dl, EltVT, VT));
8006 }
8007
8008 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
8009 ReplaceValueWith(SDValue(N, 1), NewChain);
8010
8011 return DAG.getBuildVector(WidenVT, dl, Scalars);
8012}
8013
8014SDValue DAGTypeLegalizer::WidenVecRes_PARTIAL_REDUCE_MLA(SDNode *N) {
8015 SDLoc DL(N);
8016 EVT VT = N->getValueType(0);
8017
8018 // Expand, then widen the result.
8019 SDValue Expanded = TLI.expandPartialReduceMLA(N, DAG);
8020 EVT WideVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
8021 return DAG.getInsertSubvector(DL, DAG.getPOISON(WideVT), Expanded, 0);
8022}
8023
8024//===----------------------------------------------------------------------===//
8025// Widen Vector Operand
8026//===----------------------------------------------------------------------===//
8027bool DAGTypeLegalizer::WidenVectorOperand(SDNode *N, unsigned OpNo) {
8028 LLVM_DEBUG(dbgs() << "Widen node operand " << OpNo << ": "; N->dump(&DAG));
8029 SDValue Res = SDValue();
8030
8031 // See if the target wants to custom widen this node.
8032 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
8033 return false;
8034
8035 switch (N->getOpcode()) {
8036 default:
8037#ifndef NDEBUG
8038 dbgs() << "WidenVectorOperand op #" << OpNo << ": ";
8039 N->dump(&DAG);
8040 dbgs() << "\n";
8041#endif
8042 report_fatal_error("do not know how to widen this operator's operand!");
8043
8044 case ISD::BITCAST: Res = WidenVecOp_BITCAST(N); break;
8045 case ISD::FAKE_USE:
8046 Res = WidenVecOp_FAKE_USE(N);
8047 break;
8048 case ISD::CONCAT_VECTORS: Res = WidenVecOp_CONCAT_VECTORS(N); break;
8049 case ISD::VECTOR_REPEAT:
8050 Res = WidenVecOp_VECTOR_REPEAT(N);
8051 break;
8052 case ISD::INSERT_SUBVECTOR: Res = WidenVecOp_INSERT_SUBVECTOR(N); break;
8053 case ISD::EXTRACT_SUBVECTOR: Res = WidenVecOp_EXTRACT_SUBVECTOR(N); break;
8054 case ISD::EXTRACT_VECTOR_ELT: Res = WidenVecOp_EXTRACT_VECTOR_ELT(N); break;
8055 case ISD::STORE: Res = WidenVecOp_STORE(N); break;
8056 case ISD::ATOMIC_STORE:
8057 Res = WidenVecOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
8058 break;
8059 case ISD::VP_STORE: Res = WidenVecOp_VP_STORE(N, OpNo); break;
8060 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8061 Res = WidenVecOp_VP_STRIDED_STORE(N, OpNo);
8062 break;
8066 Res = WidenVecOp_EXTEND_VECTOR_INREG(N);
8067 break;
8068 case ISD::MSTORE: Res = WidenVecOp_MSTORE(N, OpNo); break;
8069 case ISD::MGATHER: Res = WidenVecOp_MGATHER(N, OpNo); break;
8070 case ISD::MSCATTER: Res = WidenVecOp_MSCATTER(N, OpNo); break;
8071 case ISD::VP_SCATTER: Res = WidenVecOp_VP_SCATTER(N, OpNo); break;
8072 case ISD::SETCC: Res = WidenVecOp_SETCC(N); break;
8073 case ISD::STRICT_FSETCC:
8074 case ISD::STRICT_FSETCCS: Res = WidenVecOp_STRICT_FSETCC(N); break;
8075 case ISD::VSELECT: Res = WidenVecOp_VSELECT(N); break;
8076 case ISD::FLDEXP:
8077 case ISD::FCOPYSIGN:
8078 case ISD::LROUND:
8079 case ISD::LLROUND:
8080 case ISD::LRINT:
8081 case ISD::LLRINT:
8082 Res = WidenVecOp_UnrollVectorOp(N);
8083 break;
8084 case ISD::IS_FPCLASS: Res = WidenVecOp_IS_FPCLASS(N); break;
8085
8086 case ISD::ANY_EXTEND:
8087 case ISD::SIGN_EXTEND:
8088 case ISD::ZERO_EXTEND:
8089 Res = WidenVecOp_EXTEND(N);
8090 break;
8091
8092 case ISD::SCMP:
8093 case ISD::UCMP:
8094 Res = WidenVecOp_CMP(N);
8095 break;
8096
8097 case ISD::FP_EXTEND:
8099 case ISD::FP_ROUND:
8101 case ISD::FP_TO_SINT:
8103 case ISD::FP_TO_UINT:
8105 case ISD::SINT_TO_FP:
8107 case ISD::UINT_TO_FP:
8109 case ISD::TRUNCATE:
8112 Res = WidenVecOp_Convert(N);
8113 break;
8114
8117 Res = WidenVecOp_FP_TO_XINT_SAT(N);
8118 break;
8119
8122 case ISD::VECREDUCE_ADD:
8123 case ISD::VECREDUCE_MUL:
8124 case ISD::VECREDUCE_AND:
8125 case ISD::VECREDUCE_OR:
8126 case ISD::VECREDUCE_XOR:
8137 Res = WidenVecOp_VECREDUCE(N);
8138 break;
8141 Res = WidenVecOp_VECREDUCE_SEQ(N);
8142 break;
8143 case ISD::VP_REDUCE_FADD:
8144 case ISD::VP_REDUCE_SEQ_FADD:
8145 case ISD::VP_REDUCE_FMUL:
8146 case ISD::VP_REDUCE_SEQ_FMUL:
8147 case ISD::VP_REDUCE_ADD:
8148 case ISD::VP_REDUCE_MUL:
8149 case ISD::VP_REDUCE_AND:
8150 case ISD::VP_REDUCE_OR:
8151 case ISD::VP_REDUCE_XOR:
8152 case ISD::VP_REDUCE_SMAX:
8153 case ISD::VP_REDUCE_SMIN:
8154 case ISD::VP_REDUCE_UMAX:
8155 case ISD::VP_REDUCE_UMIN:
8156 case ISD::VP_REDUCE_FMAX:
8157 case ISD::VP_REDUCE_FMIN:
8158 case ISD::VP_REDUCE_FMAXIMUM:
8159 case ISD::VP_REDUCE_FMINIMUM:
8160 Res = WidenVecOp_VP_REDUCE(N);
8161 break;
8162 case ISD::CTTZ_ELTS:
8164 Res = WidenVecOp_CttzElements(N);
8165 break;
8166 case ISD::VP_CTTZ_ELTS:
8167 case ISD::VP_CTTZ_ELTS_ZERO_POISON:
8168 Res = WidenVecOp_VP_CttzElements(N);
8169 break;
8171 Res = WidenVecOp_VECTOR_FIND_LAST_ACTIVE(N);
8172 break;
8174 Res = WidenVecOp_VECTOR_SHUFFLE_VAR(N, OpNo);
8175 break;
8176 case ISD::VECTOR_MATCH:
8177 Res = WidenVecOp_VECTOR_MATCH(N, OpNo);
8178 break;
8183 Res = WidenVecOp_PARTIAL_REDUCE_MLA(N);
8184 break;
8185 }
8186
8187 // If Res is null, the sub-method took care of registering the result.
8188 if (!Res.getNode()) return false;
8189
8190 // If the result is N, the sub-method updated N in place. Tell the legalizer
8191 // core about this.
8192 if (Res.getNode() == N)
8193 return true;
8194
8195
8196 if (N->isStrictFPOpcode())
8197 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 2 &&
8198 "Invalid operand expansion");
8199 else
8200 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
8201 "Invalid operand expansion");
8202
8203 ReplaceValueWith(SDValue(N, 0), Res);
8204 return false;
8205}
8206
8207SDValue DAGTypeLegalizer::WidenVecOp_EXTEND(SDNode *N) {
8208 SDLoc DL(N);
8209 EVT VT = N->getValueType(0);
8210
8211 SDValue InOp = N->getOperand(0);
8212 assert(getTypeAction(InOp.getValueType()) ==
8214 "Unexpected type action");
8215 InOp = GetWidenedVector(InOp);
8218 "Input wasn't widened!");
8219
8220 // We may need to further widen the operand until it has the same total
8221 // vector size as the result.
8222 EVT InVT = InOp.getValueType();
8223 if (InVT.getSizeInBits() != VT.getSizeInBits()) {
8224 EVT InEltVT = InVT.getVectorElementType();
8225 for (EVT FixedVT : MVT::vector_valuetypes()) {
8226 EVT FixedEltVT = FixedVT.getVectorElementType();
8227 if (TLI.isTypeLegal(FixedVT) &&
8228 FixedVT.getSizeInBits() == VT.getSizeInBits() &&
8229 FixedEltVT == InEltVT) {
8230 assert(FixedVT.getVectorNumElements() >= VT.getVectorNumElements() &&
8231 "Not enough elements in the fixed type for the operand!");
8232 assert(FixedVT.getVectorNumElements() != InVT.getVectorNumElements() &&
8233 "We can't have the same type as we started with!");
8234 if (FixedVT.getVectorNumElements() > InVT.getVectorNumElements())
8235 InOp = DAG.getInsertSubvector(DL, DAG.getPOISON(FixedVT), InOp, 0);
8236 else
8237 InOp = DAG.getExtractSubvector(DL, FixedVT, InOp, 0);
8238 break;
8239 }
8240 }
8241 InVT = InOp.getValueType();
8242 if (InVT.getSizeInBits() != VT.getSizeInBits())
8243 // We couldn't find a legal vector type that was a widening of the input
8244 // and could be extended in-register to the result type, so we have to
8245 // scalarize.
8246 return WidenVecOp_Convert(N);
8247 }
8248
8249 // Use special DAG nodes to represent the operation of extending the
8250 // low lanes.
8251 switch (N->getOpcode()) {
8252 default:
8253 llvm_unreachable("Extend legalization on extend operation!");
8254 case ISD::ANY_EXTEND:
8255 return DAG.getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, InOp);
8256 case ISD::SIGN_EXTEND:
8257 return DAG.getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, InOp);
8258 case ISD::ZERO_EXTEND:
8259 return DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, InOp);
8260 }
8261}
8262
8263SDValue DAGTypeLegalizer::WidenVecOp_CMP(SDNode *N) {
8264 SDLoc dl(N);
8265
8266 EVT OpVT = N->getOperand(0).getValueType();
8267 EVT ResVT = N->getValueType(0);
8268 SDValue LHS = GetWidenedVector(N->getOperand(0));
8269 SDValue RHS = GetWidenedVector(N->getOperand(1));
8270
8271 // 1. EXTRACT_SUBVECTOR
8272 // 2. SIGN_EXTEND/ZERO_EXTEND
8273 // 3. CMP
8274 LHS = DAG.getExtractSubvector(dl, OpVT, LHS, 0);
8275 RHS = DAG.getExtractSubvector(dl, OpVT, RHS, 0);
8276
8277 // At this point the result type is guaranteed to be valid, so we can use it
8278 // as the operand type by extending it appropriately
8279 ISD::NodeType ExtendOpcode =
8280 N->getOpcode() == ISD::SCMP ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
8281 LHS = DAG.getNode(ExtendOpcode, dl, ResVT, LHS);
8282 RHS = DAG.getNode(ExtendOpcode, dl, ResVT, RHS);
8283
8284 return DAG.getNode(N->getOpcode(), dl, ResVT, LHS, RHS);
8285}
8286
8287SDValue DAGTypeLegalizer::WidenVecOp_UnrollVectorOp(SDNode *N) {
8288 // The result (and first input) is legal, but the second input is illegal.
8289 // We can't do much to fix that, so just unroll and let the extracts off of
8290 // the second input be widened as needed later.
8291 return DAG.UnrollVectorOp(N);
8292}
8293
8294SDValue DAGTypeLegalizer::WidenVecOp_IS_FPCLASS(SDNode *N) {
8295 SDLoc DL(N);
8296 EVT ResultVT = N->getValueType(0);
8297 SDValue Test = N->getOperand(1);
8298 SDValue WideArg = GetWidenedVector(N->getOperand(0));
8299
8300 // Process this node similarly to SETCC.
8301 EVT WideResultVT = getSetCCResultType(WideArg.getValueType());
8302 if (ResultVT.getScalarType() == MVT::i1)
8303 WideResultVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8304 WideResultVT.getVectorNumElements());
8305
8306 SDValue WideNode = DAG.getNode(ISD::IS_FPCLASS, DL, WideResultVT,
8307 {WideArg, Test}, N->getFlags());
8308
8309 // Extract the needed results from the result vector.
8310 EVT ResVT =
8311 EVT::getVectorVT(*DAG.getContext(), WideResultVT.getVectorElementType(),
8312 ResultVT.getVectorNumElements());
8313 SDValue CC = DAG.getExtractSubvector(DL, ResVT, WideNode, 0);
8314
8315 EVT OpVT = N->getOperand(0).getValueType();
8316 ISD::NodeType ExtendCode =
8317 TargetLowering::getExtendForContent(TLI.getBooleanContents(OpVT));
8318 return DAG.getNode(ExtendCode, DL, ResultVT, CC);
8319}
8320
8321SDValue DAGTypeLegalizer::WidenVecOp_Convert(SDNode *N) {
8322 // Since the result is legal and the input is illegal.
8323 EVT VT = N->getValueType(0);
8324 EVT EltVT = VT.getVectorElementType();
8325 SDLoc dl(N);
8326 SDValue InOp = N->getOperand(N->isStrictFPOpcode() ? 1 : 0);
8327 assert(getTypeAction(InOp.getValueType()) ==
8329 "Unexpected type action");
8330 InOp = GetWidenedVector(InOp);
8331 EVT InVT = InOp.getValueType();
8332 unsigned Opcode = N->getOpcode();
8333
8334 // Helper to build a convert node with all scalar trailing operands.
8335 auto MakeConvertNode = [&](EVT VT, SDValue Op) -> SDValue {
8336 if (Opcode == ISD::CONVERT_TO_ARBITRARY_FP)
8337 return DAG.getNode(Opcode, dl, VT, Op, N->getOperand(1), N->getOperand(2),
8338 N->getOperand(3));
8339 if (Opcode == ISD::FP_ROUND || Opcode == ISD::CONVERT_FROM_ARBITRARY_FP)
8340 return DAG.getNode(Opcode, dl, VT, Op, N->getOperand(1));
8341 return DAG.getNode(Opcode, dl, VT, Op);
8342 };
8343
8344 // See if a widened result type would be legal, if so widen the node.
8345 // FIXME: This isn't safe for StrictFP. Other optimization here is needed.
8346 EVT WideVT = EVT::getVectorVT(*DAG.getContext(), EltVT,
8347 InVT.getVectorElementCount());
8348 if (TLI.isTypeLegal(WideVT) && !N->isStrictFPOpcode()) {
8349 SDValue Res;
8350 if (N->isStrictFPOpcode()) {
8351 if (Opcode == ISD::STRICT_FP_ROUND)
8352 Res = DAG.getNode(Opcode, dl, { WideVT, MVT::Other },
8353 { N->getOperand(0), InOp, N->getOperand(2) });
8354 else
8355 Res = DAG.getNode(Opcode, dl, { WideVT, MVT::Other },
8356 { N->getOperand(0), InOp });
8357 // Legalize the chain result - switch anything that used the old chain to
8358 // use the new one.
8359 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
8360 } else {
8361 Res = MakeConvertNode(WideVT, InOp);
8362 }
8363 return DAG.getExtractSubvector(dl, VT, Res, 0);
8364 }
8365
8366 EVT InEltVT = InVT.getVectorElementType();
8367
8368 // Unroll the convert into some scalar code and create a nasty build vector.
8369 unsigned NumElts = VT.getVectorNumElements();
8371 if (N->isStrictFPOpcode()) {
8372 SmallVector<SDValue, 4> NewOps(N->ops());
8373 SmallVector<SDValue, 32> OpChains;
8374 for (unsigned i=0; i < NumElts; ++i) {
8375 NewOps[1] = DAG.getExtractVectorElt(dl, InEltVT, InOp, i);
8376 Ops[i] = DAG.getNode(Opcode, dl, { EltVT, MVT::Other }, NewOps);
8377 OpChains.push_back(Ops[i].getValue(1));
8378 }
8379 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OpChains);
8380 ReplaceValueWith(SDValue(N, 1), NewChain);
8381 } else {
8382 for (unsigned i = 0; i < NumElts; ++i) {
8383 SDValue Elt = DAG.getExtractVectorElt(dl, InEltVT, InOp, i);
8384 Ops[i] = MakeConvertNode(EltVT, Elt);
8385 }
8386 }
8387
8388 return DAG.getBuildVector(VT, dl, Ops);
8389}
8390
8391SDValue DAGTypeLegalizer::WidenVecOp_FP_TO_XINT_SAT(SDNode *N) {
8392 EVT DstVT = N->getValueType(0);
8393 SDValue Src = GetWidenedVector(N->getOperand(0));
8394 EVT SrcVT = Src.getValueType();
8395 ElementCount WideNumElts = SrcVT.getVectorElementCount();
8396 SDLoc dl(N);
8397
8398 // See if a widened result type would be legal, if so widen the node.
8399 EVT WideDstVT = EVT::getVectorVT(*DAG.getContext(),
8400 DstVT.getVectorElementType(), WideNumElts);
8401 if (TLI.isTypeLegal(WideDstVT)) {
8402 SDValue Res =
8403 DAG.getNode(N->getOpcode(), dl, WideDstVT, Src, N->getOperand(1));
8404 return DAG.getNode(
8405 ISD::EXTRACT_SUBVECTOR, dl, DstVT, Res,
8406 DAG.getConstant(0, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
8407 }
8408
8409 // Give up and unroll.
8410 return DAG.UnrollVectorOp(N);
8411}
8412
8413SDValue DAGTypeLegalizer::WidenVecOp_BITCAST(SDNode *N) {
8414 EVT VT = N->getValueType(0);
8415 SDValue InOp = GetWidenedVector(N->getOperand(0));
8416 EVT InWidenVT = InOp.getValueType();
8417 SDLoc dl(N);
8418
8419 // Check if we can convert between two legal vector types and extract.
8420 TypeSize InWidenSize = InWidenVT.getSizeInBits();
8421 TypeSize Size = VT.getSizeInBits();
8422 // x86mmx is not an acceptable vector element type, so don't try.
8423 if (!VT.isVector() && VT != MVT::x86mmx &&
8424 InWidenSize.hasKnownScalarFactor(Size)) {
8425 unsigned NewNumElts = InWidenSize.getKnownScalarFactor(Size);
8426 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), VT, NewNumElts);
8427 if (TLI.isTypeLegal(NewVT)) {
8428 SDValue BitOp = DAG.getNode(ISD::BITCAST, dl, NewVT, InOp);
8429 return DAG.getExtractVectorElt(dl, VT, BitOp, 0);
8430 }
8431 }
8432
8433 // Handle a case like bitcast v12i8 -> v3i32. Normally that would get widened
8434 // to v16i8 -> v4i32, but for a target where v3i32 is legal but v12i8 is not,
8435 // we end up here. Handling the case here with EXTRACT_SUBVECTOR avoids
8436 // having to copy via memory.
8437 if (VT.isVector()) {
8438 EVT EltVT = VT.getVectorElementType();
8439 unsigned EltSize = EltVT.getFixedSizeInBits();
8440 if (InWidenSize.isKnownMultipleOf(EltSize)) {
8441 ElementCount NewNumElts =
8442 (InWidenVT.getVectorElementCount() * InWidenVT.getScalarSizeInBits())
8443 .divideCoefficientBy(EltSize);
8444 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NewNumElts);
8445 if (TLI.isTypeLegal(NewVT)) {
8446 SDValue BitOp = DAG.getNode(ISD::BITCAST, dl, NewVT, InOp);
8447 return DAG.getExtractSubvector(dl, VT, BitOp, 0);
8448 }
8449 }
8450 }
8451
8452 return CreateStackStoreLoad(InOp, VT);
8453}
8454
8455// Vectors with sizes that are not powers of 2 need to be widened to the
8456// next largest power of 2. For example, we may get a vector of 3 32-bit
8457// integers or of 6 16-bit integers, both of which have to be widened to a
8458// 128-bit vector.
8459SDValue DAGTypeLegalizer::WidenVecOp_FAKE_USE(SDNode *N) {
8460 SDValue WidenedOp = GetWidenedVector(N->getOperand(1));
8461 return DAG.getNode(ISD::FAKE_USE, SDLoc(), MVT::Other, N->getOperand(0),
8462 WidenedOp);
8463}
8464
8465SDValue DAGTypeLegalizer::WidenVecOp_CONCAT_VECTORS(SDNode *N) {
8466 EVT VT = N->getValueType(0);
8467 EVT EltVT = VT.getVectorElementType();
8468 EVT InVT = N->getOperand(0).getValueType();
8469 SDLoc dl(N);
8470
8471 // If the widen width for this operand is the same as the width of the concat
8472 // and all but the first operand is undef, just use the widened operand.
8473 unsigned NumOperands = N->getNumOperands();
8474 if (VT == TLI.getTypeToTransformTo(*DAG.getContext(), InVT)) {
8475 unsigned i;
8476 for (i = 1; i < NumOperands; ++i)
8477 if (!N->getOperand(i).isUndef())
8478 break;
8479
8480 if (i == NumOperands)
8481 return GetWidenedVector(N->getOperand(0));
8482 }
8483
8484 if (VT.isScalableVector()) {
8485 SDValue Result = DAG.getPOISON(VT);
8486 unsigned NumInElts = InVT.getVectorMinNumElements();
8487 for (unsigned i = 0; i < NumOperands; ++i) {
8488 SDValue InOp = GetWidenedVector(N->getOperand(i));
8489 if (InOp.getValueType() != InVT)
8490 InOp = DAG.getExtractSubvector(dl, InVT, InOp, 0);
8491 Result = DAG.getInsertSubvector(dl, Result, InOp, i * NumInElts);
8492 }
8493 return Result;
8494 }
8495
8496 // Otherwise, fall back to a nasty build vector.
8497 unsigned NumElts = VT.getVectorNumElements();
8499
8500 unsigned NumInElts = InVT.getVectorNumElements();
8501
8502 unsigned Idx = 0;
8503 for (unsigned i=0; i < NumOperands; ++i) {
8504 SDValue InOp = N->getOperand(i);
8505 assert(getTypeAction(InOp.getValueType()) ==
8507 "Unexpected type action");
8508 InOp = GetWidenedVector(InOp);
8509 for (unsigned j = 0; j < NumInElts; ++j)
8510 Ops[Idx++] = DAG.getExtractVectorElt(dl, EltVT, InOp, j);
8511 }
8512 return DAG.getBuildVector(VT, dl, Ops);
8513}
8514
8515SDValue DAGTypeLegalizer::WidenVecOp_VECTOR_REPEAT(SDNode *N) {
8516 SDLoc DL(N);
8517 EVT VT = N->getValueType(0);
8518 SDValue Src = N->getOperand(0);
8519 EVT SrcVT = Src.getValueType();
8520 EVT WidenedSrcVT = TLI.getTypeToTransformTo(*DAG.getContext(), SrcVT);
8521
8522 if (!WidenedSrcVT.getVectorElementCount().hasKnownScalarFactor(
8523 SrcVT.getVectorElementCount()))
8525 "Cannot widen VECTOR_REPEAT operand to an ElementCount that's not "
8526 "a known scalar multiple of the input ElementCount.");
8527
8528 // Repeat the original source because the extra lanes of its widened value
8529 // are unspecified.
8530 unsigned NumConcat =
8531 WidenedSrcVT.getVectorNumElements() / SrcVT.getVectorNumElements();
8532 SmallVector<SDValue, 8> Ops(NumConcat, Src);
8533 SDValue WidenedSrc = DAG.getNode(ISD::CONCAT_VECTORS, DL, WidenedSrcVT, Ops);
8534 EVT WidenedVT = VT.changeVectorElementCount(
8535 *DAG.getContext(),
8537 SDValue Widened = DAG.getNode(ISD::VECTOR_REPEAT, DL, WidenedVT, WidenedSrc);
8538 return DAG.getExtractSubvector(DL, VT, Widened, 0);
8539}
8540
8541SDValue DAGTypeLegalizer::WidenVecOp_INSERT_SUBVECTOR(SDNode *N) {
8542 EVT VT = N->getValueType(0);
8543 SDValue SubVec = N->getOperand(1);
8544 SDValue InVec = N->getOperand(0);
8545
8546 EVT OrigVT = SubVec.getValueType();
8547 SubVec = GetWidenedVector(SubVec);
8548 EVT SubVT = SubVec.getValueType();
8549
8550 // Whether or not all the elements of the widened SubVec will be inserted into
8551 // valid indices of VT.
8552 bool IndicesValid = false;
8553 // If we statically know that VT can fit SubVT, the indices are valid.
8554 if (VT.knownBitsGE(SubVT))
8555 IndicesValid = true;
8556 else if (VT.isScalableVector() && SubVT.isFixedLengthVector()) {
8557 // Otherwise, if we're inserting a fixed vector into a scalable vector and
8558 // we know the minimum vscale we can work out if it's valid ourselves.
8559 Attribute Attr = DAG.getMachineFunction().getFunction().getFnAttribute(
8560 Attribute::VScaleRange);
8561 if (Attr.isValid()) {
8562 unsigned VScaleMin = Attr.getVScaleRangeMin();
8563 if (VT.getSizeInBits().getKnownMinValue() * VScaleMin >=
8564 SubVT.getFixedSizeInBits())
8565 IndicesValid = true;
8566 }
8567 }
8568
8569 if (!IndicesValid)
8571 "Don't know how to widen the operands for INSERT_SUBVECTOR");
8572
8573 SDLoc DL(N);
8574
8575 // We need to make sure that the indices are still valid, otherwise we might
8576 // widen what was previously well-defined to something undefined.
8577 if (InVec.isUndef() && N->getConstantOperandVal(2) == 0)
8578 return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, InVec, SubVec,
8579 N->getOperand(2));
8580
8581 if (OrigVT.isScalableVector()) {
8582 // When the widened types match, overwriting the start of a vector is
8583 // effectively a merge operation that can be implement as a vselect.
8584 if (SubVT == VT && N->getConstantOperandVal(2) == 0) {
8585 SDValue Mask =
8586 DAG.getMaskFromElementCount(DL, VT, OrigVT.getVectorElementCount());
8587 return DAG.getNode(ISD::VSELECT, DL, VT, Mask, SubVec, InVec);
8588 }
8589
8590 // Fallback to inserting through memory.
8591 Align Alignment = DAG.getReducedAlign(VT, /*UseABI=*/false);
8592 SDValue StackPtr = DAG.CreateStackTemporary(VT.getStoreSize(), Alignment);
8593 MachineFunction &MF = DAG.getMachineFunction();
8594 int FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
8595 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
8596
8597 MachineMemOperand *StoreMMO = MF.getMachineMemOperand(
8600 MachineMemOperand *LoadMMO = MF.getMachineMemOperand(
8603
8604 // Write out the vector being inserting into.
8605 SDValue Ch =
8606 DAG.getStore(DAG.getEntryNode(), DL, InVec, StackPtr, StoreMMO);
8607
8608 // Build a mask to match the length of the sub-vector.
8609 SDValue Mask =
8610 DAG.getMaskFromElementCount(DL, SubVT, OrigVT.getVectorElementCount());
8611
8612 // Overwrite the sub-vector at the required offset.
8613 SDValue SubVecPtr =
8614 TLI.getVectorSubVecPointer(DAG, StackPtr, VT, OrigVT, N->getOperand(2));
8615 Ch = DAG.getMaskedStore(Ch, DL, SubVec, SubVecPtr,
8616 DAG.getPOISON(SubVecPtr.getValueType()), Mask, VT,
8617 StoreMMO, ISD::UNINDEXED, ISD::NON_EXTLOAD);
8618
8619 // Read back the result.
8620 return DAG.getLoad(VT, DL, Ch, StackPtr, LoadMMO);
8621 }
8622
8623 // If the operands can't be widened legally, just replace the INSERT_SUBVECTOR
8624 // with a series of INSERT_VECTOR_ELT
8625 unsigned Idx = N->getConstantOperandVal(2);
8626
8627 SDValue InsertElt = InVec;
8628 for (unsigned I = 0, E = OrigVT.getVectorNumElements(); I != E; ++I) {
8629 SDValue ExtractElt =
8630 DAG.getExtractVectorElt(DL, VT.getVectorElementType(), SubVec, I);
8631 InsertElt = DAG.getInsertVectorElt(DL, InsertElt, ExtractElt, I + Idx);
8632 }
8633
8634 return InsertElt;
8635}
8636
8637SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
8638 SDValue InOp = GetWidenedVector(N->getOperand(0));
8639 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N),
8640 N->getValueType(0), InOp, N->getOperand(1));
8641}
8642
8643SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
8644 SDValue InOp = GetWidenedVector(N->getOperand(0));
8645 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
8646 N->getValueType(0), InOp, N->getOperand(1));
8647}
8648
8649SDValue DAGTypeLegalizer::WidenVecOp_EXTEND_VECTOR_INREG(SDNode *N) {
8650 SDLoc DL(N);
8651 EVT ResVT = N->getValueType(0);
8652
8653 // Widen the input as requested by the legalizer.
8654 SDValue WideInOp = GetWidenedVector(N->getOperand(0));
8655 EVT WideInVT = WideInOp.getValueType();
8656
8657 // Simple case: if widened input is still smaller than or equal to result,
8658 // just use it directly.
8659 if (WideInVT.getSizeInBits() <= ResVT.getSizeInBits())
8660 return DAG.getNode(N->getOpcode(), DL, ResVT, WideInOp);
8661
8662 // EXTEND_VECTOR_INREG requires input bits <= result bits.
8663 // If widening makes the input larger than the original result, widen the
8664 // result to match, then extract back down.
8665 EVT ResEltVT = ResVT.getVectorElementType();
8666 unsigned EltBits = ResEltVT.getSizeInBits();
8667 assert((WideInVT.getSizeInBits() % EltBits) == 0 &&
8668 "Widened input size must be a multiple of result element size");
8669
8670 unsigned WideNumElts = WideInVT.getSizeInBits() / EltBits;
8671 EVT WideResVT = EVT::getVectorVT(*DAG.getContext(), ResEltVT, WideNumElts);
8672
8673 SDValue WideRes = DAG.getNode(N->getOpcode(), DL, WideResVT, WideInOp);
8674 return DAG.getExtractSubvector(DL, ResVT, WideRes, 0);
8675}
8676
8677SDValue DAGTypeLegalizer::WidenVecOp_STORE(SDNode *N) {
8678 // We have to widen the value, but we want only to store the original
8679 // vector type.
8680 StoreSDNode *ST = cast<StoreSDNode>(N);
8681
8682 if (!ST->getMemoryVT().getScalarType().isByteSized())
8683 return TLI.scalarizeVectorStore(ST, DAG);
8684
8685 if (ST->isTruncatingStore())
8686 return TLI.scalarizeVectorStore(ST, DAG);
8687
8688 // Generate a vector-predicated store if it is custom/legal on the target.
8689 // To avoid possible recursion, only do this if the widened mask type is
8690 // legal.
8691 // FIXME: Not all targets may support EVL in VP_STORE. These will have been
8692 // removed from the IR by the ExpandVectorPredication pass but we're
8693 // reintroducing them here.
8694 SDValue StVal = ST->getValue();
8695 EVT StVT = StVal.getValueType();
8696 EVT WideVT = TLI.getTypeToTransformTo(*DAG.getContext(), StVT);
8697 EVT WideMaskVT = getSetCCResultType(WideVT);
8698
8699 if (TLI.isOperationLegalOrCustom(ISD::VP_STORE, WideVT) &&
8700 TLI.isTypeLegal(WideMaskVT)) {
8701 // Widen the value.
8702 SDLoc DL(N);
8703 StVal = GetWidenedVector(StVal);
8704 SDValue Mask = DAG.getAllOnesConstant(DL, WideMaskVT);
8705 SDValue EVL = DAG.getElementCount(DL, TLI.getVPExplicitVectorLengthTy(),
8706 StVT.getVectorElementCount());
8707 return DAG.getStoreVP(ST->getChain(), DL, StVal, ST->getBasePtr(),
8708 ST->getOffset(), Mask, EVL, StVT, ST->getMemOperand(),
8709 ST->getAddressingMode());
8710 }
8711
8713 if (GenWidenVectorStores(StChain, ST)) {
8714 if (StChain.size() == 1)
8715 return StChain[0];
8716
8717 return DAG.getNode(ISD::TokenFactor, SDLoc(ST), MVT::Other, StChain);
8718 }
8719
8720 if (StVT.isVector()) {
8721 // If all else fails replace the store with a wide masked store.
8722 SDLoc DL(N);
8723 SDValue WideStVal = GetWidenedVector(StVal);
8724 SDValue Mask =
8725 DAG.getMaskFromElementCount(DL, WideVT, StVT.getVectorElementCount());
8726
8727 return DAG.getMaskedStore(ST->getChain(), DL, WideStVal, ST->getBasePtr(),
8728 ST->getOffset(), Mask, ST->getMemoryVT(),
8729 ST->getMemOperand(), ST->getAddressingMode(),
8730 ST->isTruncatingStore());
8731 }
8732
8733 report_fatal_error("Unable to widen vector store");
8734}
8735
8736SDValue DAGTypeLegalizer::WidenVecOp_ATOMIC_STORE(AtomicSDNode *ST) {
8737 EVT StVT = ST->getMemoryVT();
8738 SDLoc dl(ST);
8739
8740 SDValue StVal = GetWidenedVector(ST->getVal());
8741 EVT WidenVT = StVal.getValueType();
8742
8743 TypeSize StWidth = StVT.getSizeInBits();
8744 TypeSize WidenWidth = WidenVT.getSizeInBits();
8745 TypeSize WidthDiff = WidenWidth - StWidth;
8746
8747 // Find the vector type that can store the original memory width in one
8748 // atomic operation. Pass StAlign=0 (like atomic loads); a real align would
8749 // let findMemType widen the access past the value (e.g. <2 x i8> at align 4
8750 // implies a 4-byte movl, writing undef bytes past its object).
8751 std::optional<EVT> FirstVT =
8752 findMemType(DAG, TLI, StWidth.getKnownMinValue(), WidenVT, /*StAlign=*/0,
8753 WidthDiff.getKnownMinValue());
8754 if (!FirstVT)
8755 return SDValue();
8756
8757 TypeSize FirstVTWidth = FirstVT->getSizeInBits();
8758
8759 SDValue StOp =
8760 coerceStoredValue(StVal, *FirstVT, WidenVT, FirstVTWidth, dl, DAG);
8761
8762 return DAG.getAtomic(ISD::ATOMIC_STORE, dl, *FirstVT, ST->getChain(), StOp,
8763 ST->getBasePtr(), ST->getMemOperand());
8764}
8765
8766SDValue DAGTypeLegalizer::WidenVecOp_VP_STORE(SDNode *N, unsigned OpNo) {
8767 assert((OpNo == 1 || OpNo == 3) &&
8768 "Can widen only data or mask operand of vp_store");
8769 VPStoreSDNode *ST = cast<VPStoreSDNode>(N);
8770 SDValue Mask = ST->getMask();
8771 SDValue StVal = ST->getValue();
8772 SDLoc dl(N);
8773
8774 if (OpNo == 1) {
8775 // Widen the value.
8776 StVal = GetWidenedVector(StVal);
8777
8778 // We only handle the case where the mask needs widening to an
8779 // identically-sized type as the vector inputs.
8780 assert(getTypeAction(Mask.getValueType()) ==
8782 "Unable to widen VP store");
8783 Mask = GetWidenedVector(Mask);
8784 } else {
8785 Mask = GetWidenedVector(Mask);
8786
8787 // We only handle the case where the stored value needs widening to an
8788 // identically-sized type as the mask.
8789 assert(getTypeAction(StVal.getValueType()) ==
8791 "Unable to widen VP store");
8792 StVal = GetWidenedVector(StVal);
8793 }
8794
8795 assert(Mask.getValueType().getVectorElementCount() ==
8797 "Mask and data vectors should have the same number of elements");
8798 return DAG.getStoreVP(ST->getChain(), dl, StVal, ST->getBasePtr(),
8799 ST->getOffset(), Mask, ST->getVectorLength(),
8800 ST->getMemoryVT(), ST->getMemOperand(),
8801 ST->getAddressingMode(), ST->isTruncatingStore(),
8802 ST->isCompressingStore());
8803}
8804
8805SDValue DAGTypeLegalizer::WidenVecOp_VP_STRIDED_STORE(SDNode *N,
8806 unsigned OpNo) {
8807 assert((OpNo == 1 || OpNo == 4) &&
8808 "Can widen only data or mask operand of vp_strided_store");
8809 VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
8810 SDValue Mask = SST->getMask();
8811 SDValue StVal = SST->getValue();
8812 SDLoc DL(N);
8813
8814 if (OpNo == 1)
8815 assert(getTypeAction(Mask.getValueType()) ==
8817 "Unable to widen VP strided store");
8818 else
8819 assert(getTypeAction(StVal.getValueType()) ==
8821 "Unable to widen VP strided store");
8822
8823 StVal = GetWidenedVector(StVal);
8824 Mask = GetWidenedVector(Mask);
8825
8827 Mask.getValueType().getVectorElementCount() &&
8828 "Data and mask vectors should have the same number of elements");
8829
8830 return DAG.getStridedStoreVP(
8831 SST->getChain(), DL, StVal, SST->getBasePtr(), SST->getOffset(),
8832 SST->getStride(), Mask, SST->getVectorLength(), SST->getMemoryVT(),
8833 SST->getMemOperand(), SST->getAddressingMode(), SST->isTruncatingStore(),
8834 SST->isCompressingStore());
8835}
8836
8837SDValue DAGTypeLegalizer::WidenVecOp_MSTORE(SDNode *N, unsigned OpNo) {
8838 assert((OpNo == 1 || OpNo == 4) &&
8839 "Can widen only data or mask operand of mstore");
8840 MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
8841 SDValue Mask = MST->getMask();
8842 EVT MaskVT = Mask.getValueType();
8843 SDValue StVal = MST->getValue();
8844 EVT VT = StVal.getValueType();
8845 SDLoc dl(N);
8846
8847 EVT WideVT, WideMaskVT;
8848 if (OpNo == 1) {
8849 // Widen the value.
8850 StVal = GetWidenedVector(StVal);
8851
8852 WideVT = StVal.getValueType();
8853 WideMaskVT =
8854 EVT::getVectorVT(*DAG.getContext(), MaskVT.getVectorElementType(),
8855 WideVT.getVectorElementCount());
8856 } else {
8857 WideMaskVT = TLI.getTypeToTransformTo(*DAG.getContext(), MaskVT);
8858
8859 EVT ValueVT = StVal.getValueType();
8860 WideVT = EVT::getVectorVT(*DAG.getContext(), ValueVT.getVectorElementType(),
8861 WideMaskVT.getVectorElementCount());
8862 }
8863
8864 if (TLI.isOperationLegalOrCustom(ISD::VP_STORE, WideVT) &&
8865 TLI.isTypeLegal(WideMaskVT) && !MST->isCompressingStore()) {
8866 Mask = DAG.getInsertSubvector(dl, DAG.getPOISON(WideMaskVT), Mask, 0);
8867 SDValue EVL = DAG.getElementCount(dl, TLI.getVPExplicitVectorLengthTy(),
8869 return DAG.getStoreVP(MST->getChain(), dl, StVal, MST->getBasePtr(),
8870 MST->getOffset(), Mask, EVL, MST->getMemoryVT(),
8871 MST->getMemOperand(), MST->getAddressingMode());
8872 }
8873
8874 if (OpNo == 1) {
8875 // The mask should be widened as well.
8876 Mask = ModifyToType(Mask, WideMaskVT, true);
8877 } else {
8878 // Widen the mask.
8879 Mask = ModifyToType(Mask, WideMaskVT, true);
8880
8881 StVal = ModifyToType(StVal, WideVT);
8882 }
8883
8884 assert(Mask.getValueType().getVectorElementCount() ==
8886 "Mask and data vectors should have the same number of elements");
8887 return DAG.getMaskedStore(MST->getChain(), dl, StVal, MST->getBasePtr(),
8888 MST->getOffset(), Mask, MST->getMemoryVT(),
8889 MST->getMemOperand(), MST->getAddressingMode(),
8890 false, MST->isCompressingStore());
8891}
8892
8893SDValue DAGTypeLegalizer::WidenVecOp_MGATHER(SDNode *N, unsigned OpNo) {
8894 assert(OpNo == 4 && "Can widen only the index of mgather");
8895 auto *MG = cast<MaskedGatherSDNode>(N);
8896 SDValue DataOp = MG->getPassThru();
8897 SDValue Mask = MG->getMask();
8898 SDValue Scale = MG->getScale();
8899
8900 // Just widen the index. It's allowed to have extra elements.
8901 SDValue Index = GetWidenedVector(MG->getIndex());
8902
8903 SDLoc dl(N);
8904 SDValue Ops[] = {MG->getChain(), DataOp, Mask, MG->getBasePtr(), Index,
8905 Scale};
8906 SDValue Res = DAG.getMaskedGather(MG->getVTList(), MG->getMemoryVT(), dl, Ops,
8907 MG->getMemOperand(), MG->getIndexType(),
8908 MG->getExtensionType());
8909 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
8910 ReplaceValueWith(SDValue(N, 0), Res.getValue(0));
8911 return SDValue();
8912}
8913
8914SDValue DAGTypeLegalizer::WidenVecOp_MSCATTER(SDNode *N, unsigned OpNo) {
8915 MaskedScatterSDNode *MSC = cast<MaskedScatterSDNode>(N);
8916 SDValue DataOp = MSC->getValue();
8917 SDValue Mask = MSC->getMask();
8918 SDValue Index = MSC->getIndex();
8919 SDValue Scale = MSC->getScale();
8920 EVT WideMemVT = MSC->getMemoryVT();
8921
8922 if (OpNo == 1) {
8923 DataOp = GetWidenedVector(DataOp);
8924 ElementCount WideEC = DataOp.getValueType().getVectorElementCount();
8925
8926 // Widen index.
8927 EVT IndexVT = Index.getValueType();
8928 EVT WideIndexVT = EVT::getVectorVT(*DAG.getContext(),
8929 IndexVT.getVectorElementType(), WideEC);
8930 Index = ModifyToType(Index, WideIndexVT);
8931
8932 // The mask should be widened as well.
8933 EVT MaskVT = Mask.getValueType();
8934 EVT WideMaskVT = EVT::getVectorVT(*DAG.getContext(),
8935 MaskVT.getVectorElementType(), WideEC);
8936 Mask = ModifyToType(Mask, WideMaskVT, true);
8937
8938 // Widen the MemoryType
8939 WideMemVT = EVT::getVectorVT(*DAG.getContext(),
8940 MSC->getMemoryVT().getScalarType(), WideEC);
8941 } else if (OpNo == 4) {
8942 // Just widen the index. It's allowed to have extra elements.
8943 Index = GetWidenedVector(Index);
8944 } else
8945 llvm_unreachable("Can't widen this operand of mscatter");
8946
8947 SDValue Ops[] = {MSC->getChain(), DataOp, Mask, MSC->getBasePtr(), Index,
8948 Scale};
8949 return DAG.getMaskedScatter(DAG.getVTList(MVT::Other), WideMemVT, SDLoc(N),
8950 Ops, MSC->getMemOperand(), MSC->getIndexType(),
8951 MSC->isTruncatingStore());
8952}
8953
8954SDValue DAGTypeLegalizer::WidenVecOp_VP_SCATTER(SDNode *N, unsigned OpNo) {
8955 VPScatterSDNode *VPSC = cast<VPScatterSDNode>(N);
8956 SDValue DataOp = VPSC->getValue();
8957 SDValue Mask = VPSC->getMask();
8958 SDValue Index = VPSC->getIndex();
8959 SDValue Scale = VPSC->getScale();
8960 EVT WideMemVT = VPSC->getMemoryVT();
8961
8962 if (OpNo == 1) {
8963 DataOp = GetWidenedVector(DataOp);
8964 Index = GetWidenedVector(Index);
8965 const auto WideEC = DataOp.getValueType().getVectorElementCount();
8966 Mask = GetWidenedMask(Mask, WideEC);
8967 WideMemVT = EVT::getVectorVT(*DAG.getContext(),
8968 VPSC->getMemoryVT().getScalarType(), WideEC);
8969 } else if (OpNo == 3) {
8970 // Just widen the index. It's allowed to have extra elements.
8971 Index = GetWidenedVector(Index);
8972 } else
8973 llvm_unreachable("Can't widen this operand of VP_SCATTER");
8974
8975 SDValue Ops[] = {
8976 VPSC->getChain(), DataOp, VPSC->getBasePtr(), Index, Scale, Mask,
8977 VPSC->getVectorLength()};
8978 return DAG.getScatterVP(DAG.getVTList(MVT::Other), WideMemVT, SDLoc(N), Ops,
8979 VPSC->getMemOperand(), VPSC->getIndexType());
8980}
8981
8982SDValue DAGTypeLegalizer::WidenVecOp_SETCC(SDNode *N) {
8983 SDValue InOp0 = GetWidenedVector(N->getOperand(0));
8984 SDValue InOp1 = GetWidenedVector(N->getOperand(1));
8985 SDLoc dl(N);
8986 EVT VT = N->getValueType(0);
8987
8988 // WARNING: In this code we widen the compare instruction with garbage.
8989 // This garbage may contain denormal floats which may be slow. Is this a real
8990 // concern ? Should we zero the unused lanes if this is a float compare ?
8991
8992 // Get a new SETCC node to compare the newly widened operands.
8993 // Only some of the compared elements are legal.
8994 EVT SVT = getSetCCResultType(InOp0.getValueType());
8995 // The result type is legal, if its vXi1, keep vXi1 for the new SETCC.
8996 if (VT.getScalarType() == MVT::i1)
8997 SVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8998 SVT.getVectorElementCount());
8999
9000 SDValue WideSETCC = DAG.getNode(ISD::SETCC, SDLoc(N),
9001 SVT, InOp0, InOp1, N->getOperand(2));
9002
9003 // Extract the needed results from the result vector.
9004 EVT ResVT = EVT::getVectorVT(*DAG.getContext(),
9007 SDValue CC = DAG.getExtractSubvector(dl, ResVT, WideSETCC, 0);
9008
9009 EVT OpVT = N->getOperand(0).getValueType();
9010 ISD::NodeType ExtendCode =
9011 TargetLowering::getExtendForContent(TLI.getBooleanContents(OpVT));
9012 return DAG.getNode(ExtendCode, dl, VT, CC);
9013}
9014
9015SDValue DAGTypeLegalizer::WidenVecOp_STRICT_FSETCC(SDNode *N) {
9016 SDValue Chain = N->getOperand(0);
9017 SDValue LHS = GetWidenedVector(N->getOperand(1));
9018 SDValue RHS = GetWidenedVector(N->getOperand(2));
9019 SDValue CC = N->getOperand(3);
9020 SDLoc dl(N);
9021
9022 EVT VT = N->getValueType(0);
9023 EVT EltVT = VT.getVectorElementType();
9024 EVT TmpEltVT = LHS.getValueType().getVectorElementType();
9025 unsigned NumElts = VT.getVectorNumElements();
9026
9027 // Unroll into a build vector.
9028 SmallVector<SDValue, 8> Scalars(NumElts);
9029 SmallVector<SDValue, 8> Chains(NumElts);
9030
9031 for (unsigned i = 0; i != NumElts; ++i) {
9032 SDValue LHSElem = DAG.getExtractVectorElt(dl, TmpEltVT, LHS, i);
9033 SDValue RHSElem = DAG.getExtractVectorElt(dl, TmpEltVT, RHS, i);
9034
9035 Scalars[i] = DAG.getNode(N->getOpcode(), dl, {MVT::i1, MVT::Other},
9036 {Chain, LHSElem, RHSElem, CC});
9037 Chains[i] = Scalars[i].getValue(1);
9038 Scalars[i] = DAG.getSelect(dl, EltVT, Scalars[i],
9039 DAG.getBoolConstant(true, dl, EltVT, VT),
9040 DAG.getBoolConstant(false, dl, EltVT, VT));
9041 }
9042
9043 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
9044 ReplaceValueWith(SDValue(N, 1), NewChain);
9045
9046 return DAG.getBuildVector(VT, dl, Scalars);
9047}
9048
9049static unsigned getExtendForIntVecReduction(unsigned Opc) {
9050 switch (Opc) {
9051 default:
9052 llvm_unreachable("Expected integer vector reduction");
9053 case ISD::VECREDUCE_ADD:
9054 case ISD::VECREDUCE_MUL:
9055 case ISD::VECREDUCE_AND:
9056 case ISD::VECREDUCE_OR:
9057 case ISD::VECREDUCE_XOR:
9058 return ISD::ANY_EXTEND;
9061 return ISD::SIGN_EXTEND;
9064 return ISD::ZERO_EXTEND;
9065 }
9066}
9067
9068SDValue DAGTypeLegalizer::WidenVecOp_VECREDUCE(SDNode *N) {
9069 SDLoc dl(N);
9070 SDValue Op = GetWidenedVector(N->getOperand(0));
9071 EVT VT = N->getValueType(0);
9072 EVT OrigVT = N->getOperand(0).getValueType();
9073 EVT WideVT = Op.getValueType();
9074 EVT ElemVT = OrigVT.getVectorElementType();
9075 SDNodeFlags Flags = N->getFlags();
9076
9077 unsigned Opc = N->getOpcode();
9078 unsigned BaseOpc = ISD::getVecReduceBaseOpcode(Opc);
9079 SDValue NeutralElem = DAG.getIdentityElement(BaseOpc, dl, ElemVT, Flags);
9080 assert(NeutralElem && "Neutral element must exist");
9081
9082 // Pad the vector with the neutral element.
9083 unsigned OrigElts = OrigVT.getVectorMinNumElements();
9084 unsigned WideElts = WideVT.getVectorMinNumElements();
9085
9086 // Generate a vp.reduce_op if it is custom/legal for the target. This avoids
9087 // needing to pad the source vector, because the inactive lanes can simply be
9088 // disabled and not contribute to the result.
9089 if (auto VPOpcode = ISD::getVPForBaseOpcode(Opc);
9090 VPOpcode && TLI.isOperationLegalOrCustom(*VPOpcode, WideVT)) {
9091 SDValue Start = NeutralElem;
9092 if (VT.isInteger())
9093 Start = DAG.getNode(getExtendForIntVecReduction(Opc), dl, VT, Start);
9094 assert(Start.getValueType() == VT);
9095 EVT WideMaskVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
9096 WideVT.getVectorElementCount());
9097 SDValue Mask = DAG.getAllOnesConstant(dl, WideMaskVT);
9098 SDValue EVL = DAG.getElementCount(dl, TLI.getVPExplicitVectorLengthTy(),
9099 OrigVT.getVectorElementCount());
9100 return DAG.getNode(*VPOpcode, dl, VT, {Start, Op, Mask, EVL}, Flags);
9101 }
9102
9103 if (WideVT.isScalableVector()) {
9104 unsigned GCD = std::gcd(OrigElts, WideElts);
9105 EVT SplatVT = EVT::getVectorVT(*DAG.getContext(), ElemVT,
9107 SDValue SplatNeutral = DAG.getSplatVector(SplatVT, dl, NeutralElem);
9108 for (unsigned Idx = OrigElts; Idx < WideElts; Idx = Idx + GCD)
9109 Op = DAG.getInsertSubvector(dl, Op, SplatNeutral, Idx);
9110 return DAG.getNode(Opc, dl, VT, Op, Flags);
9111 }
9112
9113 for (unsigned Idx = OrigElts; Idx < WideElts; Idx++)
9114 Op = DAG.getInsertVectorElt(dl, Op, NeutralElem, Idx);
9115
9116 return DAG.getNode(Opc, dl, VT, Op, Flags);
9117}
9118
9119SDValue DAGTypeLegalizer::WidenVecOp_VECREDUCE_SEQ(SDNode *N) {
9120 SDLoc dl(N);
9121 SDValue AccOp = N->getOperand(0);
9122 SDValue VecOp = N->getOperand(1);
9123 SDValue Op = GetWidenedVector(VecOp);
9124
9125 EVT VT = N->getValueType(0);
9126 EVT OrigVT = VecOp.getValueType();
9127 EVT WideVT = Op.getValueType();
9128 EVT ElemVT = OrigVT.getVectorElementType();
9129 SDNodeFlags Flags = N->getFlags();
9130
9131 unsigned Opc = N->getOpcode();
9132 unsigned BaseOpc = ISD::getVecReduceBaseOpcode(Opc);
9133 SDValue NeutralElem = DAG.getIdentityElement(BaseOpc, dl, ElemVT, Flags);
9134
9135 // Pad the vector with the neutral element.
9136 unsigned OrigElts = OrigVT.getVectorMinNumElements();
9137 unsigned WideElts = WideVT.getVectorMinNumElements();
9138
9139 // Generate a vp.reduce_op if it is custom/legal for the target. This avoids
9140 // needing to pad the source vector, because the inactive lanes can simply be
9141 // disabled and not contribute to the result.
9142 if (auto VPOpcode = ISD::getVPForBaseOpcode(Opc);
9143 VPOpcode && TLI.isOperationLegalOrCustom(*VPOpcode, WideVT)) {
9144 EVT WideMaskVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
9145 WideVT.getVectorElementCount());
9146 SDValue Mask = DAG.getAllOnesConstant(dl, WideMaskVT);
9147 SDValue EVL = DAG.getElementCount(dl, TLI.getVPExplicitVectorLengthTy(),
9148 OrigVT.getVectorElementCount());
9149 return DAG.getNode(*VPOpcode, dl, VT, {AccOp, Op, Mask, EVL}, Flags);
9150 }
9151
9152 if (WideVT.isScalableVector()) {
9153 unsigned GCD = std::gcd(OrigElts, WideElts);
9154 EVT SplatVT = EVT::getVectorVT(*DAG.getContext(), ElemVT,
9156 SDValue SplatNeutral = DAG.getSplatVector(SplatVT, dl, NeutralElem);
9157 for (unsigned Idx = OrigElts; Idx < WideElts; Idx = Idx + GCD)
9158 Op = DAG.getInsertSubvector(dl, Op, SplatNeutral, Idx);
9159 return DAG.getNode(Opc, dl, VT, AccOp, Op, Flags);
9160 }
9161
9162 for (unsigned Idx = OrigElts; Idx < WideElts; Idx++)
9163 Op = DAG.getInsertVectorElt(dl, Op, NeutralElem, Idx);
9164
9165 return DAG.getNode(Opc, dl, VT, AccOp, Op, Flags);
9166}
9167
9168SDValue DAGTypeLegalizer::WidenVecOp_VP_REDUCE(SDNode *N) {
9169 assert(N->isVPOpcode() && "Expected VP opcode");
9170
9171 SDLoc dl(N);
9172 SDValue Op = GetWidenedVector(N->getOperand(1));
9173 SDValue Mask = GetWidenedMask(N->getOperand(2),
9174 Op.getValueType().getVectorElementCount());
9175
9176 return DAG.getNode(N->getOpcode(), dl, N->getValueType(0),
9177 {N->getOperand(0), Op, Mask, N->getOperand(3)},
9178 N->getFlags());
9179}
9180
9181SDValue DAGTypeLegalizer::WidenVecOp_VSELECT(SDNode *N) {
9182 // This only gets called in the case that the left and right inputs and
9183 // result are of a legal odd vector type, and the condition is illegal i1 of
9184 // the same odd width that needs widening.
9185 EVT VT = N->getValueType(0);
9186 assert(VT.isVector() && !VT.isPow2VectorType() && isTypeLegal(VT));
9187
9188 SDValue Cond = GetWidenedVector(N->getOperand(0));
9189 SDValue LeftIn = DAG.WidenVector(N->getOperand(1), SDLoc(N));
9190 SDValue RightIn = DAG.WidenVector(N->getOperand(2), SDLoc(N));
9191 SDLoc DL(N);
9192
9193 SDValue Select = DAG.getNode(N->getOpcode(), DL, LeftIn.getValueType(), Cond,
9194 LeftIn, RightIn);
9195 return DAG.getExtractSubvector(DL, VT, Select, 0);
9196}
9197
9198SDValue DAGTypeLegalizer::WidenVecOp_CttzElements(SDNode *N) {
9199 SDLoc DL(N);
9200 SDValue Source = N->getOperand(0);
9201 EVT SourceVT = Source.getValueType();
9202 EVT WideVT = TLI.getTypeToTransformTo(*DAG.getContext(), SourceVT);
9203
9204 SDValue WideSource;
9205 if (N->getOpcode() == ISD::CTTZ_ELTS_ZERO_POISON) {
9206 WideSource = GetWidenedVector(Source);
9207 } else {
9208 // Pad the widened portion with all-ones so the extra lanes appear as
9209 // active (non-zero) elements and do not contribute trailing zeros.
9210 SDValue AllOnes = DAG.getAllOnesConstant(DL, WideVT);
9211 if (WideVT.isFixedLengthVector() &&
9212 getTypeAction(WideVT) == TargetLowering::TypeSplitVector) {
9213 WideSource = GetWidenedVector(Source);
9214 unsigned WideElts = WideVT.getVectorNumElements();
9215 SmallVector<int> Mask(WideElts);
9216 std::iota(Mask.begin(), Mask.end(), 0);
9217 for (unsigned I = SourceVT.getVectorNumElements(); I != WideElts; ++I)
9218 Mask[I] += WideElts;
9219 WideSource = DAG.getVectorShuffle(WideVT, DL, WideSource, AllOnes, Mask);
9220 } else {
9221 WideSource = DAG.getInsertSubvector(DL, AllOnes, Source, 0);
9222 }
9223 }
9224
9225 return DAG.getNode(N->getOpcode(), DL, N->getValueType(0), WideSource,
9226 N->getFlags());
9227}
9228
9229SDValue DAGTypeLegalizer::WidenVecOp_VP_CttzElements(SDNode *N) {
9230 SDLoc DL(N);
9231 SDValue Source = GetWidenedVector(N->getOperand(0));
9232 EVT SrcVT = Source.getValueType();
9233 SDValue Mask =
9234 GetWidenedMask(N->getOperand(1), SrcVT.getVectorElementCount());
9235
9236 return DAG.getNode(N->getOpcode(), DL, N->getValueType(0),
9237 {Source, Mask, N->getOperand(2)}, N->getFlags());
9238}
9239
9240SDValue DAGTypeLegalizer::WidenVecOp_VECTOR_FIND_LAST_ACTIVE(SDNode *N) {
9241 SDLoc DL(N);
9242 SDValue Mask = N->getOperand(0);
9243 EVT OrigMaskVT = Mask.getValueType();
9244 SDValue WideMask = GetWidenedVector(Mask);
9245 EVT WideMaskVT = WideMask.getValueType();
9246
9247 // Pad the mask with zeros to ensure inactive lanes don't affect the result.
9248 unsigned OrigElts = OrigMaskVT.getVectorNumElements();
9249 unsigned WideElts = WideMaskVT.getVectorNumElements();
9250 if (OrigElts != WideElts) {
9251 SDValue ZeroMask = DAG.getConstant(0, DL, WideMaskVT);
9252 WideMask = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, WideMaskVT, ZeroMask,
9253 Mask, DAG.getVectorIdxConstant(0, DL));
9254 }
9255
9256 return DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, DL, N->getValueType(0),
9257 WideMask);
9258}
9259
9260SDValue DAGTypeLegalizer::WidenVecOp_VECTOR_SHUFFLE_VAR(SDNode *N,
9261 unsigned OpNo) {
9262 // The only possibility for an illegal operand is the mask, since result type
9263 // legalization would have handled this node already otherwise.
9264 assert(OpNo == 1 && "Illegal operand must be mask");
9265 // TODO: Legalize the mask type instead once a target lowers the node.
9266 return TLI.expandVECTOR_SHUFFLE_VAR(N, DAG);
9267}
9268
9269SDValue DAGTypeLegalizer::WidenVecOp_VECTOR_MATCH(SDNode *N, unsigned OpNo) {
9270 if (OpNo == 0) {
9271 SDLoc DL(N);
9272 EVT ResVT = N->getValueType(0);
9273 EVT SourceVT = N->getOperand(0).getValueType();
9274 EVT WideSourceVT = TLI.getTypeToTransformTo(*DAG.getContext(), SourceVT);
9275 EVT WidenVT =
9276 EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
9277 WideSourceVT.getVectorElementCount());
9278
9279 SDValue WideSource = DAG.getInsertSubvector(DL, DAG.getUNDEF(WideSourceVT),
9280 N->getOperand(0), 0);
9281 SDValue WideMask = DAG.getInsertSubvector(
9282 DL, DAG.getConstant(0, DL, WidenVT), N->getOperand(2), 0);
9283 SDValue WideMatch = DAG.getNode(ISD::VECTOR_MATCH, DL, WidenVT, WideSource,
9284 N->getOperand(1), WideMask, N->getFlags());
9285 return DAG.getExtractSubvector(DL, ResVT, WideMatch, 0);
9286 }
9287
9288 // Note: The Mask (OpNo == 2) should be widened with the result.
9289 assert(OpNo == 1 && "Unexpected VECTOR_MATCH operand");
9290
9291 SDLoc DL(N);
9292 SDValue Needle = N->getOperand(1);
9293 EVT NeedleVT = Needle.getValueType();
9294 if (NeedleVT.getVectorNumElements() == 1)
9295 return TLI.expandVectorMatch(N, DAG);
9296
9297 EVT WidenNeedleVT = TLI.getTypeToTransformTo(*DAG.getContext(), NeedleVT);
9298
9299 SDValue Fill =
9300 DAG.getExtractVectorElt(DL, NeedleVT.getVectorElementType(), Needle, 0);
9301 SDValue WideNeedle = DAG.getSplatVector(WidenNeedleVT, DL, Fill);
9302 WideNeedle = DAG.getInsertSubvector(DL, WideNeedle, Needle, 0);
9303
9304 return DAG.getNode(ISD::VECTOR_MATCH, DL, N->getValueType(0),
9305 N->getOperand(0), WideNeedle, N->getOperand(2),
9306 N->getFlags());
9307}
9308
9309SDValue DAGTypeLegalizer::WidenVecOp_PARTIAL_REDUCE_MLA(SDNode *N) {
9310 // The accumulator is legal and only the inputs need widening. Expand, as
9311 // when widening the result; the extended inputs are widened from there.
9312 return TLI.expandPartialReduceMLA(N, DAG);
9313}
9314
9315//===----------------------------------------------------------------------===//
9316// Vector Widening Utilities
9317//===----------------------------------------------------------------------===//
9318
9319// Utility function to find the type to chop up a widen vector for load/store
9320// TLI: Target lowering used to determine legal types.
9321// Width: Width left need to load/store.
9322// WidenVT: The widen vector type to load to/store from
9323// Align: If 0, don't allow use of a wider type
9324// WidenEx: If Align is not 0, the amount additional we can load/store from.
9325
9326static std::optional<EVT> findMemType(SelectionDAG &DAG,
9327 const TargetLowering &TLI, unsigned Width,
9328 EVT WidenVT, unsigned Align = 0,
9329 unsigned WidenEx = 0) {
9330 EVT WidenEltVT = WidenVT.getVectorElementType();
9331 const bool Scalable = WidenVT.isScalableVector();
9332 unsigned WidenWidth = WidenVT.getSizeInBits().getKnownMinValue();
9333 unsigned WidenEltWidth = WidenEltVT.getSizeInBits();
9334 unsigned AlignInBits = Align*8;
9335
9336 EVT RetVT = WidenEltVT;
9337 // Don't bother looking for an integer type if the vector is scalable, skip
9338 // to vector types.
9339 if (!Scalable) {
9340 // If we have one element to load/store, return it.
9341 if (Width == WidenEltWidth)
9342 return RetVT;
9343
9344 // See if there is larger legal integer than the element type to load/store.
9345 for (EVT MemVT : reverse(MVT::integer_valuetypes())) {
9346 unsigned MemVTWidth = MemVT.getSizeInBits();
9347 if (MemVT.getSizeInBits() <= WidenEltWidth)
9348 break;
9349 auto Action = TLI.getTypeAction(*DAG.getContext(), MemVT);
9350 if ((Action == TargetLowering::TypeLegal ||
9352 (WidenWidth % MemVTWidth) == 0 &&
9353 isPowerOf2_32(WidenWidth / MemVTWidth) &&
9354 (MemVTWidth <= Width ||
9355 (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
9356 if (MemVTWidth == WidenWidth)
9357 return MemVT;
9358 RetVT = MemVT;
9359 break;
9360 }
9361 }
9362 }
9363
9364 // See if there is a larger vector type to load/store that has the same vector
9365 // element type and is evenly divisible with the WidenVT.
9366 for (EVT MemVT : reverse(MVT::vector_valuetypes())) {
9367 // Skip vector MVTs which don't match the scalable property of WidenVT.
9368 if (Scalable != MemVT.isScalableVector())
9369 continue;
9370 unsigned MemVTWidth = MemVT.getSizeInBits().getKnownMinValue();
9371 auto Action = TLI.getTypeAction(*DAG.getContext(), MemVT);
9372 if ((Action == TargetLowering::TypeLegal ||
9374 WidenEltVT == MemVT.getVectorElementType() &&
9375 (WidenWidth % MemVTWidth) == 0 &&
9376 isPowerOf2_32(WidenWidth / MemVTWidth) &&
9377 (MemVTWidth <= Width ||
9378 (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
9379 if (RetVT.getFixedSizeInBits() < MemVTWidth || MemVT == WidenVT)
9380 return MemVT;
9381 }
9382 }
9383
9384 // Using element-wise loads and stores for widening operations is not
9385 // supported for scalable vectors
9386 if (Scalable)
9387 return std::nullopt;
9388
9389 return RetVT;
9390}
9391
9392// Builds a vector type from scalar loads
9393// VecTy: Resulting Vector type
9394// LDOps: Load operators to build a vector type
9395// [Start,End) the list of loads to use.
9398 unsigned Start, unsigned End) {
9399 SDLoc dl(LdOps[Start]);
9400 EVT LdTy = LdOps[Start].getValueType();
9401 unsigned Width = VecTy.getSizeInBits();
9402 unsigned NumElts = Width / LdTy.getSizeInBits();
9403 EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), LdTy, NumElts);
9404
9405 unsigned Idx = 1;
9406 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT,LdOps[Start]);
9407
9408 for (unsigned i = Start + 1; i != End; ++i) {
9409 EVT NewLdTy = LdOps[i].getValueType();
9410 if (NewLdTy != LdTy) {
9411 NumElts = Width / NewLdTy.getSizeInBits();
9412 NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewLdTy, NumElts);
9413 VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, VecOp);
9414 // Readjust position and vector position based on new load type.
9415 Idx = Idx * LdTy.getSizeInBits() / NewLdTy.getSizeInBits();
9416 LdTy = NewLdTy;
9417 }
9418 VecOp = DAG.getInsertVectorElt(dl, VecOp, LdOps[i], Idx++);
9419 }
9420 return DAG.getNode(ISD::BITCAST, dl, VecTy, VecOp);
9421}
9422
9423SDValue DAGTypeLegalizer::GenWidenVectorLoads(SmallVectorImpl<SDValue> &LdChain,
9424 LoadSDNode *LD) {
9425 // The strategy assumes that we can efficiently load power-of-two widths.
9426 // The routine chops the vector into the largest vector loads with the same
9427 // element type or scalar loads and then recombines it to the widen vector
9428 // type.
9429 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),LD->getValueType(0));
9430 EVT LdVT = LD->getMemoryVT();
9431 SDLoc dl(LD);
9432 assert(LdVT.isVector() && WidenVT.isVector());
9433 assert(LdVT.isScalableVector() == WidenVT.isScalableVector());
9435
9436 // Load information
9437 SDValue Chain = LD->getChain();
9438 SDValue BasePtr = LD->getBasePtr();
9439 MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags();
9440 MMOMetadata Metadata = LD->getMMOMetadataForSubAccess();
9441
9442 TypeSize LdWidth = LdVT.getSizeInBits();
9443 TypeSize WidenWidth = WidenVT.getSizeInBits();
9444 TypeSize WidthDiff = WidenWidth - LdWidth;
9445 // Allow wider loads if they are sufficiently aligned to avoid memory faults
9446 // and if the original load is simple.
9447 unsigned LdAlign =
9448 (!LD->isSimple() || LdVT.isScalableVector()) ? 0 : LD->getAlign().value();
9449
9450 // Find the vector type that can load from.
9451 std::optional<EVT> FirstVT =
9452 findMemType(DAG, TLI, LdWidth.getKnownMinValue(), WidenVT, LdAlign,
9453 WidthDiff.getKnownMinValue());
9454
9455 if (!FirstVT)
9456 return SDValue();
9457
9458 SmallVector<EVT, 8> MemVTs;
9459 TypeSize FirstVTWidth = FirstVT->getSizeInBits();
9460
9461 // Unless we're able to load in one instruction we must work out how to load
9462 // the remainder.
9463 if (!TypeSize::isKnownLE(LdWidth, FirstVTWidth)) {
9464 std::optional<EVT> NewVT = FirstVT;
9465 TypeSize RemainingWidth = LdWidth;
9466 TypeSize NewVTWidth = FirstVTWidth;
9467 do {
9468 RemainingWidth -= NewVTWidth;
9469 if (TypeSize::isKnownLT(RemainingWidth, NewVTWidth)) {
9470 // The current type we are using is too large. Find a better size.
9471 NewVT = findMemType(DAG, TLI, RemainingWidth.getKnownMinValue(),
9472 WidenVT, LdAlign, WidthDiff.getKnownMinValue());
9473 if (!NewVT)
9474 return SDValue();
9475 NewVTWidth = NewVT->getSizeInBits();
9476 }
9477 MemVTs.push_back(*NewVT);
9478 } while (TypeSize::isKnownGT(RemainingWidth, NewVTWidth));
9479 }
9480
9481 SDValue LdOp = DAG.getLoad(*FirstVT, dl, Chain, BasePtr, LD->getPointerInfo(),
9482 LD->getBaseAlign(), MMOFlags, Metadata);
9483 LdChain.push_back(LdOp.getValue(1));
9484
9485 // Check if we can load the element with one instruction.
9486 if (MemVTs.empty())
9487 return coerceLoadedValue(LdOp, *FirstVT, WidenVT, LdWidth, FirstVTWidth, dl,
9488 DAG);
9489
9490 // Load vector by using multiple loads from largest vector to scalar.
9492 LdOps.push_back(LdOp);
9493
9494 uint64_t ScaledOffset = 0;
9495 MachinePointerInfo MPI = LD->getPointerInfo();
9496
9497 // First incremement past the first load.
9498 IncrementPointer(cast<LoadSDNode>(LdOp), *FirstVT, MPI, BasePtr,
9499 &ScaledOffset);
9500
9501 for (EVT MemVT : MemVTs) {
9502 Align NewAlign = ScaledOffset == 0
9503 ? LD->getBaseAlign()
9504 : commonAlignment(LD->getAlign(), ScaledOffset);
9505 SDValue L = DAG.getLoad(MemVT, dl, Chain, BasePtr, MPI, NewAlign, MMOFlags,
9506 Metadata);
9507
9508 LdOps.push_back(L);
9509 LdChain.push_back(L.getValue(1));
9510 IncrementPointer(cast<LoadSDNode>(L), MemVT, MPI, BasePtr, &ScaledOffset);
9511 }
9512
9513 // Build the vector from the load operations.
9514 unsigned End = LdOps.size();
9515 if (!LdOps[0].getValueType().isVector())
9516 // All the loads are scalar loads.
9517 return BuildVectorFromScalar(DAG, WidenVT, LdOps, 0, End);
9518
9519 // If the load contains vectors, build the vector using concat vector.
9520 // All of the vectors used to load are power-of-2, and the scalar loads can be
9521 // combined to make a power-of-2 vector.
9522 SmallVector<SDValue, 16> ConcatOps(End);
9523 int i = End - 1;
9524 int Idx = End;
9525 EVT LdTy = LdOps[i].getValueType();
9526 // First, combine the scalar loads to a vector.
9527 if (!LdTy.isVector()) {
9528 for (--i; i >= 0; --i) {
9529 LdTy = LdOps[i].getValueType();
9530 if (LdTy.isVector())
9531 break;
9532 }
9533 ConcatOps[--Idx] = BuildVectorFromScalar(DAG, LdTy, LdOps, i + 1, End);
9534 }
9535
9536 ConcatOps[--Idx] = LdOps[i];
9537 for (--i; i >= 0; --i) {
9538 EVT NewLdTy = LdOps[i].getValueType();
9539 if (NewLdTy != LdTy) {
9540 // Create a larger vector.
9541 TypeSize LdTySize = LdTy.getSizeInBits();
9542 TypeSize NewLdTySize = NewLdTy.getSizeInBits();
9543 assert(NewLdTySize.isScalable() == LdTySize.isScalable() &&
9544 NewLdTySize.isKnownMultipleOf(LdTySize.getKnownMinValue()));
9545 unsigned NumOps =
9546 NewLdTySize.getKnownMinValue() / LdTySize.getKnownMinValue();
9548 unsigned j = 0;
9549 for (; j != End-Idx; ++j)
9550 WidenOps[j] = ConcatOps[Idx+j];
9551 for (; j != NumOps; ++j)
9552 WidenOps[j] = DAG.getPOISON(LdTy);
9553
9554 ConcatOps[End-1] = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewLdTy,
9555 WidenOps);
9556 Idx = End - 1;
9557 LdTy = NewLdTy;
9558 }
9559 ConcatOps[--Idx] = LdOps[i];
9560 }
9561
9562 if (WidenWidth == LdTy.getSizeInBits() * (End - Idx))
9563 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
9564 ArrayRef(&ConcatOps[Idx], End - Idx));
9565
9566 // We need to fill the rest with undefs to build the vector.
9567 unsigned NumOps =
9568 WidenWidth.getKnownMinValue() / LdTy.getSizeInBits().getKnownMinValue();
9570 SDValue UndefVal = DAG.getPOISON(LdTy);
9571 {
9572 unsigned i = 0;
9573 for (; i != End-Idx; ++i)
9574 WidenOps[i] = ConcatOps[Idx+i];
9575 for (; i != NumOps; ++i)
9576 WidenOps[i] = UndefVal;
9577 }
9578 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, WidenOps);
9579}
9580
9581SDValue
9582DAGTypeLegalizer::GenWidenVectorExtLoads(SmallVectorImpl<SDValue> &LdChain,
9583 LoadSDNode *LD,
9584 ISD::LoadExtType ExtType) {
9585 // For extension loads, it may not be more efficient to chop up the vector
9586 // and then extend it. Instead, we unroll the load and build a new vector.
9587 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),LD->getValueType(0));
9588 EVT LdVT = LD->getMemoryVT();
9589 SDLoc dl(LD);
9590 assert(LdVT.isVector() && WidenVT.isVector());
9591 assert(LdVT.isScalableVector() == WidenVT.isScalableVector());
9592
9593 // Load information
9594 SDValue Chain = LD->getChain();
9595 SDValue BasePtr = LD->getBasePtr();
9596 MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags();
9597 MMOMetadata Metadata = LD->getMMOMetadataForSubAccess();
9598
9599 if (LdVT.isScalableVector())
9600 return SDValue();
9601
9602 EVT EltVT = WidenVT.getVectorElementType();
9603 EVT LdEltVT = LdVT.getVectorElementType();
9604 unsigned NumElts = LdVT.getVectorNumElements();
9605
9606 // Load each element and widen.
9607 unsigned WidenNumElts = WidenVT.getVectorNumElements();
9608 SmallVector<SDValue, 16> Ops(WidenNumElts);
9609 unsigned Increment = LdEltVT.getSizeInBits() / 8;
9610 Ops[0] =
9611 DAG.getExtLoad(ExtType, dl, EltVT, Chain, BasePtr, LD->getPointerInfo(),
9612 LdEltVT, LD->getBaseAlign(), MMOFlags, Metadata);
9613 LdChain.push_back(Ops[0].getValue(1));
9614 unsigned i = 0, Offset = Increment;
9615 for (i=1; i < NumElts; ++i, Offset += Increment) {
9616 SDValue NewBasePtr =
9617 DAG.getObjectPtrOffset(dl, BasePtr, TypeSize::getFixed(Offset));
9618 Ops[i] = DAG.getExtLoad(ExtType, dl, EltVT, Chain, NewBasePtr,
9619 LD->getPointerInfo().getWithOffset(Offset), LdEltVT,
9620 LD->getBaseAlign(), MMOFlags, Metadata);
9621 LdChain.push_back(Ops[i].getValue(1));
9622 }
9623
9624 // Fill the rest with undefs.
9625 SDValue UndefVal = DAG.getPOISON(EltVT);
9626 for (; i != WidenNumElts; ++i)
9627 Ops[i] = UndefVal;
9628
9629 return DAG.getBuildVector(WidenVT, dl, Ops);
9630}
9631
9632bool DAGTypeLegalizer::GenWidenVectorStores(SmallVectorImpl<SDValue> &StChain,
9633 StoreSDNode *ST) {
9634 // The strategy assumes that we can efficiently store power-of-two widths.
9635 // The routine chops the vector into the largest vector stores with the same
9636 // element type or scalar stores.
9637 SDValue Chain = ST->getChain();
9638 SDValue BasePtr = ST->getBasePtr();
9639 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
9640 MMOMetadata Metadata = ST->getMMOMetadataForSubAccess();
9641 SDValue ValOp = GetWidenedVector(ST->getValue());
9642 SDLoc dl(ST);
9643
9644 EVT StVT = ST->getMemoryVT();
9645 TypeSize StWidth = StVT.getSizeInBits();
9646 EVT ValVT = ValOp.getValueType();
9647 TypeSize ValWidth = ValVT.getSizeInBits();
9648 EVT ValEltVT = ValVT.getVectorElementType();
9649 unsigned ValEltWidth = ValEltVT.getFixedSizeInBits();
9650 assert(StVT.getVectorElementType() == ValEltVT);
9651 assert(StVT.isScalableVector() == ValVT.isScalableVector() &&
9652 "Mismatch between store and value types");
9653
9654 int Idx = 0; // current index to store
9655
9656 MachinePointerInfo MPI = ST->getPointerInfo();
9657 uint64_t ScaledOffset = 0;
9658
9659 // A breakdown of how to widen this vector store. Each element of the vector
9660 // is a memory VT combined with the number of times it is to be stored to,
9661 // e,g., v5i32 -> {{v2i32,2},{i32,1}}
9663
9664 while (StWidth.isNonZero()) {
9665 // Find the largest vector type we can store with.
9666 std::optional<EVT> NewVT =
9667 findMemType(DAG, TLI, StWidth.getKnownMinValue(), ValVT);
9668 if (!NewVT)
9669 return false;
9670 MemVTs.push_back({*NewVT, 0});
9671 TypeSize NewVTWidth = NewVT->getSizeInBits();
9672
9673 do {
9674 StWidth -= NewVTWidth;
9675 MemVTs.back().second++;
9676 } while (StWidth.isNonZero() && TypeSize::isKnownGE(StWidth, NewVTWidth));
9677 }
9678
9679 for (const auto &Pair : MemVTs) {
9680 EVT NewVT = Pair.first;
9681 unsigned Count = Pair.second;
9682 TypeSize NewVTWidth = NewVT.getSizeInBits();
9683
9684 if (NewVT.isVector()) {
9685 unsigned NumVTElts = NewVT.getVectorMinNumElements();
9686 do {
9687 Align NewAlign = ScaledOffset == 0
9688 ? ST->getBaseAlign()
9689 : commonAlignment(ST->getAlign(), ScaledOffset);
9690 SDValue EOp = DAG.getExtractSubvector(dl, NewVT, ValOp, Idx);
9691 SDValue PartStore = DAG.getStore(Chain, dl, EOp, BasePtr, MPI, NewAlign,
9692 MMOFlags, Metadata);
9693 StChain.push_back(PartStore);
9694
9695 Idx += NumVTElts;
9696 IncrementPointer(cast<StoreSDNode>(PartStore), NewVT, MPI, BasePtr,
9697 &ScaledOffset);
9698 } while (--Count);
9699 } else {
9700 // Cast the vector to the scalar type we can store.
9701 unsigned NumElts = ValWidth.getFixedValue() / NewVTWidth.getFixedValue();
9702 EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewVT, NumElts);
9703 SDValue VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, ValOp);
9704 // Readjust index position based on new vector type.
9705 Idx = Idx * ValEltWidth / NewVTWidth.getFixedValue();
9706 do {
9707 SDValue EOp = DAG.getExtractVectorElt(dl, NewVT, VecOp, Idx++);
9708 SDValue PartStore =
9709 DAG.getStore(Chain, dl, EOp, BasePtr, MPI, ST->getBaseAlign(),
9710 MMOFlags, Metadata);
9711 StChain.push_back(PartStore);
9712
9713 IncrementPointer(cast<StoreSDNode>(PartStore), NewVT, MPI, BasePtr);
9714 } while (--Count);
9715 // Restore index back to be relative to the original widen element type.
9716 Idx = Idx * NewVTWidth.getFixedValue() / ValEltWidth;
9717 }
9718 }
9719
9720 return true;
9721}
9722
9723/// Modifies a vector input (widen or narrows) to a vector of NVT. The
9724/// input vector must have the same element type as NVT.
9725/// FillWithZeroes specifies that the vector should be widened with zeroes.
9726SDValue DAGTypeLegalizer::ModifyToType(SDValue InOp, EVT NVT,
9727 bool FillWithZeroes) {
9728 // Note that InOp might have been widened so it might already have
9729 // the right width or it might need be narrowed.
9730 EVT InVT = InOp.getValueType();
9732 "input and widen element type must match");
9733 assert(InVT.isScalableVector() == NVT.isScalableVector() &&
9734 "cannot modify scalable vectors in this way");
9735 SDLoc dl(InOp);
9736
9737 // Check if InOp already has the right width.
9738 if (InVT == NVT)
9739 return InOp;
9740
9741 ElementCount InEC = InVT.getVectorElementCount();
9742 ElementCount WidenEC = NVT.getVectorElementCount();
9743 if (WidenEC.hasKnownScalarFactor(InEC)) {
9744 unsigned NumConcat = WidenEC.getKnownScalarFactor(InEC);
9745 SmallVector<SDValue, 16> Ops(NumConcat);
9746 SDValue FillVal =
9747 FillWithZeroes ? DAG.getConstant(0, dl, InVT) : DAG.getPOISON(InVT);
9748 Ops[0] = InOp;
9749 for (unsigned i = 1; i != NumConcat; ++i)
9750 Ops[i] = FillVal;
9751
9752 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, Ops);
9753 }
9754
9755 if (InEC.hasKnownScalarFactor(WidenEC))
9756 return DAG.getExtractSubvector(dl, NVT, InOp, 0);
9757
9758 if (NVT.isScalableVector() && InVT.isScalableVector()) {
9759 // Split the input into the largest equal-sized scalable subvectors.
9760 unsigned InNumElts = InVT.getVectorMinNumElements();
9761 unsigned NewNumElts = NVT.getVectorMinNumElements();
9762 unsigned CommonFactor = std::gcd(InNumElts, NewNumElts);
9763 EVT PartVT = EVT::getVectorVT(*DAG.getContext(), NVT.getVectorElementType(),
9764 ElementCount::getScalable(CommonFactor));
9765
9767 unsigned NumCopiedParts = std::min(InNumElts, NewNumElts) / CommonFactor;
9768 for (unsigned I = 0; I != NumCopiedParts; ++I)
9769 Ops.push_back(
9770 DAG.getExtractSubvector(dl, PartVT, InOp, I * CommonFactor));
9771
9772 unsigned NumResultParts = NewNumElts / CommonFactor;
9773 if (NumResultParts > NumCopiedParts) {
9774 SDValue FillVal = FillWithZeroes ? DAG.getConstant(0, dl, PartVT)
9775 : DAG.getPOISON(PartVT);
9776 Ops.append(NumResultParts - NumCopiedParts, FillVal);
9777 }
9778
9779 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, Ops);
9780 }
9781
9782 assert(!InVT.isScalableVector() && !NVT.isScalableVector() &&
9783 "Scalable vectors should have been handled already.");
9784
9785 unsigned InNumElts = InEC.getFixedValue();
9786 unsigned WidenNumElts = WidenEC.getFixedValue();
9787
9788 // Fall back to extract and build (+ mask, if padding with zeros).
9789 SmallVector<SDValue, 16> Ops(WidenNumElts);
9790 EVT EltVT = NVT.getVectorElementType();
9791 unsigned MinNumElts = std::min(WidenNumElts, InNumElts);
9792 unsigned Idx;
9793 for (Idx = 0; Idx < MinNumElts; ++Idx)
9794 Ops[Idx] = DAG.getExtractVectorElt(dl, EltVT, InOp, Idx);
9795
9796 SDValue UndefVal = DAG.getPOISON(EltVT);
9797 for (; Idx < WidenNumElts; ++Idx)
9798 Ops[Idx] = UndefVal;
9799
9800 SDValue Widened = DAG.getBuildVector(NVT, dl, Ops);
9801 if (!FillWithZeroes)
9802 return Widened;
9803
9804 assert(NVT.isInteger() &&
9805 "We expect to never want to FillWithZeroes for non-integral types.");
9806
9808 MaskOps.append(MinNumElts, DAG.getAllOnesConstant(dl, EltVT));
9809 MaskOps.append(WidenNumElts - MinNumElts, DAG.getConstant(0, dl, EltVT));
9810
9811 return DAG.getNode(ISD::AND, dl, NVT, Widened,
9812 DAG.getBuildVector(NVT, dl, MaskOps));
9813}
return SDValue()
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
uint64_t uint64_t
uint64_t Imm
AMDGPU Register Bank Select
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
dxil translate DXIL Translate Metadata
#define _
static constexpr Value * getValue(Ty &ValueOrUse)
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static unsigned getExtendForIntVecReduction(SDNode *N)
static SDValue BuildVectorFromScalar(SelectionDAG &DAG, EVT VecTy, SmallVectorImpl< SDValue > &LdOps, unsigned Start, unsigned End)
static std::optional< EVT > findMemType(SelectionDAG &DAG, const TargetLowering &TLI, unsigned Width, EVT WidenVT, unsigned Align, unsigned WidenEx)
static EVT getSETCCOperandType(SDValue N)
static bool isSETCCOp(unsigned Opcode)
static bool isLogicalMaskOp(unsigned Opcode)
static bool isSETCCorConvertedSETCC(SDValue N)
static SDValue coerceStoredValue(SDValue StVal, EVT FirstVT, EVT WidenVT, TypeSize FirstVTWidth, const SDLoc &dl, SelectionDAG &DAG)
Inverse of coerceLoadedValue: pull a FirstVT-sized scalar/vector out of the widened value so it can b...
static SDValue CollectOpsToWiden(SelectionDAG &DAG, const TargetLowering &TLI, SmallVectorImpl< SDValue > &ConcatOps, unsigned ConcatEnd, EVT VT, EVT MaxVT, EVT WidenVT)
static SDValue coerceLoadedValue(SDValue LdOp, EVT FirstVT, EVT WidenVT, TypeSize LdWidth, TypeSize FirstVTWidth, SDLoc dl, SelectionDAG &DAG)
Either return the same load or provide appropriate casts from the load and return that.
SmallVector< MachineOperand, 4 > Cond
unsigned Opc
#define I(x, y, z)
Definition MD5.cpp:57
static bool isUndef(const MachineInstr &MI)
This file provides utility analysis objects describing memory locations.
uint64_t High
#define P(N)
RISCVCC::CondCode CC
const MachineOperand & HiOp1
SI Fold Operands
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
This file implements the SmallBitVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
Value * RHS
Value * LHS
This is an SDNode representing atomic operations.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:260
static constexpr ElementCount getScalable(ScalarTy MinVal)
Definition TypeSize.h:309
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:312
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:317
This class is used to represent ISD::LOAD nodes.
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
static auto integer_valuetypes()
static auto vector_valuetypes()
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.
Flags
Flags values. These may be or'd together.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
Flags getFlags() const
Return the raw flags of the source value,.
This class is used to represent an MGATHER node.
const SDValue & getBasePtr() const
ISD::MemIndexType getIndexType() const
How is Index applied to BasePtr when computing addresses.
const SDValue & getInc() const
const SDValue & getScale() const
const SDValue & getMask() const
const SDValue & getIntID() const
const SDValue & getIndex() const
const SDValue & getBasePtr() const
ISD::MemIndexType getIndexType() const
This class is used to represent an MLOAD node.
const SDValue & getBasePtr() const
ISD::LoadExtType getExtensionType() const
const SDValue & getMask() const
const SDValue & getPassThru() const
const SDValue & getOffset() const
bool isUnindexed() const
Return true if this is NOT a pre/post inc/dec load/store.
ISD::MemIndexedMode getAddressingMode() const
Return the addressing mode for this load or store: unindexed, pre-inc, pre-dec, post-inc,...
const SDValue & getValue() const
bool isTruncatingStore() const
Return true if the op does a truncation before store.
This class is used to represent an MSTORE node.
bool isCompressingStore() const
Returns true if the op does a compression to the vector before storing.
const SDValue & getOffset() const
const SDValue & getBasePtr() const
const SDValue & getMask() const
const SDValue & getValue() const
This is an abstract virtual class for memory operations.
Align getBaseAlign() const
Returns alignment and volatility of the memory access.
const MDNode * getRanges() const
Returns the Ranges that describes the dereference.
AAMDNodes getAAInfo() const
Returns the AA info that describes the dereference.
MachineMemOperand * getMemOperand() const
Return the unique MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
const SDValue & getChain() const
EVT getMemoryVT() const
Return the type of the in-memory value.
const MDNode * getMemCacheHint() const
Returns the cache hint metadata for this memory access.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
bool isStrictFPOpcode()
Test if this node is a strict floating point pseudo-op.
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
SDNodeFlags getFlags() const
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
uint64_t getScalarValueSizeInBits() const
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
SDValue getExtractVectorElt(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Extract element at Idx from Vec.
SDValue getInsertVectorElt(const SDLoc &DL, SDValue Vec, SDValue Elt, unsigned Idx)
Insert Elt into Vec at offset Idx.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
LLVMContext * getContext() const
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
Vector takeVector()
Clear the SetVector and return the underlying vector.
Definition SetVector.h:94
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
LegalizeTypeAction
This enum indicates whether a types are legal for a target, and if not, what action should be used to...
BooleanContent
Enum that describes how the target represents true/false values.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:352
ISD::MemIndexedMode getAddressingMode() const
Return the addressing mode for this load or store: unindexed, pre-inc, pre-dec, post-inc,...
bool isUnindexed() const
Return true if this is NOT a pre/post inc/dec load/store.
This class is used to represent an VP_GATHER node.
const SDValue & getScale() const
ISD::MemIndexType getIndexType() const
How is Index applied to BasePtr when computing addresses.
const SDValue & getVectorLength() const
const SDValue & getIndex() const
const SDValue & getBasePtr() const
const SDValue & getMask() const
This class is used to represent a VP_LOAD node.
const SDValue & getValue() const
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
const SDValue & getMask() const
ISD::LoadExtType getExtensionType() const
const SDValue & getStride() const
const SDValue & getOffset() const
const SDValue & getVectorLength() const
const SDValue & getBasePtr() const
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
const SDValue & getBasePtr() const
const SDValue & getMask() const
const SDValue & getValue() const
bool isTruncatingStore() const
Return true if this is a truncating store.
const SDValue & getOffset() const
const SDValue & getVectorLength() const
const SDValue & getStride() const
bool isCompressingStore() const
Returns true if the op does a compression to the vector before storing.
constexpr bool isKnownMultipleOf(ScalarTy RHS) const
This function tells the caller whether the element count is known at compile time to be a multiple of...
Definition TypeSize.h:181
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS*X will result in a value whose quantity matches our ...
Definition TypeSize.h:266
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:201
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:231
constexpr bool isNonZero() const
Definition TypeSize.h:156
constexpr ScalarTy getKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns a value X where RHS*X will result in a value whose quantity matches our own.
Definition TypeSize.h:274
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:217
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr bool isKnownEven() const
A return value of true indicates we know at compile time that the number of elements (vscale * Min) i...
Definition TypeSize.h:177
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
constexpr bool isZero() const
Definition TypeSize.h:154
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
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:238
#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.
LLVM_ABI bool isConstantSplatVector(SDValue V, APInt &SplatValue)
Node predicates.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:43
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:851
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:263
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:516
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:238
@ PARTIAL_REDUCE_SMLA
PARTIAL_REDUCE_[U|S]MLA(Accumulator, Input1, Input2) The partial reduction nodes sign or zero extend ...
@ ATOMIC_LOAD_FMINIMUMNUM
@ LOOP_DEPENDENCE_RAW_MASK
@ MASK_BEFOREFIRST
Has one mask vector operand.
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
@ VECREDUCE_FMINIMUMNUM
@ AVGSUBFLOORS
AVGSUBFLOORS/AVGSUBFLOORU - Averaging subtract - Subtract two integers using an integer of type i[N+1...
Definition ISDOpcodes.h:739
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:605
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:811
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition ISDOpcodes.h:397
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:266
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:403
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:885
@ CTTZ_ELTS
Returns the number of number of trailing (least significant) zero elements in a vector.
@ ATOMIC_LOAD_USUB_COND
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:523
@ VECTOR_FIND_LAST_ACTIVE
Finds the index of the last active mask element Operands: Mask.
@ FMODF
FMODF - Decomposes the operand into integral and fractional parts, each having the same type and sign...
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ FSINCOSPI
FSINCOSPI - Compute both the sine and cosine times pi more accurately than FSINCOS(pi*x),...
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:912
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:589
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:420
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ VECTOR_SHUFFLE_VAR
VECTOR_SHUFFLE_VAR(VEC, MASK) - Returns VEC permuted by MASK, an integer vector with the same number ...
Definition ISDOpcodes.h:665
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:771
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:942
@ FPTRUNC_ROUND
FPTRUNC_ROUND - This corresponds to the fptrunc_round intrinsic.
Definition ISDOpcodes.h:520
@ FAKE_USE
FAKE_USE represents a use of the operand but does not do anything.
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:802
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:410
@ CONVERT_FROM_ARBITRARY_FP
CONVERT_FROM_ARBITRARY_FP - This operator converts from an arbitrary floating-point represented as an...
@ ATOMIC_LOAD_USUB_SAT
@ CTLZ_ZERO_POISON
Definition ISDOpcodes.h:820
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:876
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:731
@ STRICT_UINT_TO_FP
Definition ISDOpcodes.h:490
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:681
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
@ PARTIAL_REDUCE_FMLA
@ VECREDUCE_FMAXIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM nodes do not propagate NaNs and order signed zeroes using the llvm....
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:355
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition ISDOpcodes.h:640
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:707
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:546
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:553
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:377
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:828
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:235
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:688
@ GET_ACTIVE_LANE_MASK
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:351
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:720
@ ATOMIC_LOAD_FMAXIMUM
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:793
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:80
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:659
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:619
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Same as FMINNUM/FMAXNUM, except that a signaling NaN operand deterministi...
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:581
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:882
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:843
@ FMINNUM
FMINNUM/FMAXNUM - Same semantics as the llvm.minnum/maxnum intrinsics.
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:389
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:359
@ ATOMIC_LOAD_FMINIMUM
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by OFFSET elements an...
Definition ISDOpcodes.h:669
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:931
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:920
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:751
@ MASKED_UDIV
Masked vector arithmetic that returns poison on disabled lanes.
@ VECTOR_REVERSE
VECTOR_REVERSE(VECTOR) - Returns a vector, of the same type as VECTOR, whose elements are shuffled us...
Definition ISDOpcodes.h:650
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:416
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:837
@ STRICT_SINT_TO_FP
STRICT_[US]INT_TO_FP - Convert a signed or unsigned integer to a floating point value.
Definition ISDOpcodes.h:489
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
@ ATOMIC_LOAD_FMAXIMUMNUM
@ ATOMIC_LOAD_UDEC_WRAP
@ STRICT_FP_TO_UINT
Definition ISDOpcodes.h:483
@ PEXT
Parallel bit extract (compress) and parallel bit deposit (expand).
Definition ISDOpcodes.h:807
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:505
@ STRICT_FP_TO_SINT
STRICT_FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:482
@ FMINIMUM
FMINIMUM/FMAXIMUM - Same semantics as the llvm.minimum/maximum intrinsics.
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:958
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:510
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:763
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:759
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:726
@ VECTOR_MATCH
VECTOR_MATCH - this corresponds to the llvm.experimental.vector.match intrinsic.
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1,VEC2) right by OFFSET elements a...
Definition ISDOpcodes.h:673
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:243
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:570
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:55
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ CTTZ_ZERO_POISON
Bit counting operators with a poisoned result for zero inputs.
Definition ISDOpcodes.h:819
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:991
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:715
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:953
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Experimental vector histogram intrinsic Operands: Input Chain, Inc, Mask, Base, Index,...
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:977
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:888
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
@ VECREDUCE_SEQ_FMUL
@ CONVERT_TO_ARBITRARY_FP
CONVERT_TO_ARBITRARY_FP - Converts a native FP value to an arbitrary floating-point format,...
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:64
@ ATOMIC_LOAD_UINC_WRAP
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:539
@ PARTIAL_REDUCE_SUMLA
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:368
@ VECTOR_REPEAT
VECTOR_REPEAT(FIXED_LENGTH_VECTOR) Repeatedly copies the elements of the source fixed-length vector t...
Definition ISDOpcodes.h:645
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition ISDOpcodes.h:629
@ CTTZ_ELTS_ZERO_POISON
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - Same semantics as the llvm.minimumnum/maximumnum intrinsics.
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:746
@ ABS_MIN_POISON
ABS with a poison result for INT_MIN.
Definition ISDOpcodes.h:775
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:561
@ LOOP_DEPENDENCE_WAR_MASK
The llvm.loop.dependence.
LLVM_ABI NodeType getUnmaskedBinOpOpcode(unsigned MaskedOpc)
Given a MaskedOpc of ISD::MASKED_(U|S)(DIV|REM), returns the unmasked ISD::(U|S)(DIV|REM).
LLVM_ABI bool isBuildVectorAllOnes(SDValue V)
Return true if the specified value is a BUILD_VECTOR where all of the elements are ~0 or undef.
bool isUNINDEXEDLoad(const SDNode *N)
Returns true if the specified node is an unindexed load.
LLVM_ABI std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
LLVM_ABI bool isBuildVectorAllZeros(SDValue V)
Return true if the specified value is a BUILD_VECTOR where all of the elements are 0 or undef.
LLVM_ABI bool isBuildVectorOfConstantSDNodes(SDValue V)
Return true if the specified value is a BUILD_VECTOR node of all ConstantSDNode or undef.
LLVM_ABI NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
LLVM_ABI LegalityPredicate isVector(unsigned TypeIdx)
True iff the specified type index is a vector.
constexpr double e
Type * getValueType(Value *V, bool ReVec, bool LookThroughCmp)
Returns the "element type" of the given value/instruction V.
unsigned getOpcode(const VPValue *V)
Return the instruction opcode for the recipe defining V or 0 for unsupported recipes and VPValues not...
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1781
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
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 void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
Definition Error.cpp:173
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
constexpr int PoisonMaskElem
@ Mul
Product of integers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1901
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:206
LLVM_ABI void processShuffleMasks(ArrayRef< int > Mask, unsigned NumOfSrcRegs, unsigned NumOfDestRegs, unsigned NumOfUsedRegs, function_ref< void()> NoInputAction, function_ref< void(ArrayRef< int >, unsigned, unsigned)> SingleInputAction, function_ref< void(ArrayRef< int >, unsigned, unsigned, bool)> ManyInputsAction)
Splits and processes shuffle mask depending on the number of input and output registers.
@ Increment
Incrementally increasing token ID.
Definition AllocToken.h:26
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Extended Value Type.
Definition ValueTypes.h:35
EVT changeVectorElementTypeToInteger() const
Return a vector with the same number of elements as this vector, but with the element type converted ...
Definition ValueTypes.h:90
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:433
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:70
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:144
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:322
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:170
ElementCount getVectorElementCount() const
Definition ValueTypes.h:388
EVT getDoubleNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:509
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:411
bool isByteSized() const
Return true if the bit size is a multiple of 8.
Definition ValueTypes.h:281
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:397
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:423
bool isPow2VectorType() const
Returns true if the given vector is a power of 2.
Definition ValueTypes.h:516
EVT changeVectorElementType(LLVMContext &Context, EVT EltVT) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element type...
Definition ValueTypes.h:98
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:419
EVT widenIntegerVectorElementType(LLVMContext &Context) const
Return a VT for an integer vector type with the size of the elements doubled.
Definition ValueTypes.h:490
EVT changeVectorElementCount(LLVMContext &Context, ElementCount EC) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element coun...
Definition ValueTypes.h:109
bool isFixedLengthVector() const
Definition ValueTypes.h:214
static EVT getFloatingPointVT(unsigned BitWidth)
Returns the EVT that represents a floating-point type with the given number of bits.
Definition ValueTypes.h:55
EVT getRoundIntegerType(LLVMContext &Context) const
Rounds the bit-width of the given integer EVT up to the nearest power of two (and at least to eight),...
Definition ValueTypes.h:457
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:191
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:361
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:294
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:202
bool knownBitsGE(EVT VT) const
Return true if we know at compile time this has more than or the same bits as VT.
Definition ValueTypes.h:306
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:366
EVT changeElementType(LLVMContext &Context, EVT EltVT) const
Return a VT for a type whose attributes match ourselves with the exception of the element type that i...
Definition ValueTypes.h:136
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:374
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:499
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:175
This class contains a discriminated union of information about pointers in memory operands,...
LLVM_ABI unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getUnknownStack(MachineFunction &MF)
Stack memory without other information.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.