LLVM 20.0.0git
LoongArchISelLowering.cpp
Go to the documentation of this file.
1//=- LoongArchISelLowering.cpp - LoongArch DAG Lowering Implementation ---===//
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 defines the interfaces that LoongArch uses to lower LLVM code into
10// a selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
15#include "LoongArch.h"
18#include "LoongArchSubtarget.h"
22#include "llvm/ADT/Statistic.h"
27#include "llvm/IR/IRBuilder.h"
29#include "llvm/IR/IntrinsicsLoongArch.h"
31#include "llvm/Support/Debug.h"
35
36using namespace llvm;
37
38#define DEBUG_TYPE "loongarch-isel-lowering"
39
40STATISTIC(NumTailCalls, "Number of tail calls");
41
42static cl::opt<bool> ZeroDivCheck("loongarch-check-zero-division", cl::Hidden,
43 cl::desc("Trap on integer division by zero."),
44 cl::init(false));
45
47 const LoongArchSubtarget &STI)
48 : TargetLowering(TM), Subtarget(STI) {
49
50 MVT GRLenVT = Subtarget.getGRLenVT();
51
52 // Set up the register classes.
53
54 addRegisterClass(GRLenVT, &LoongArch::GPRRegClass);
55 if (Subtarget.hasBasicF())
56 addRegisterClass(MVT::f32, &LoongArch::FPR32RegClass);
57 if (Subtarget.hasBasicD())
58 addRegisterClass(MVT::f64, &LoongArch::FPR64RegClass);
59
60 static const MVT::SimpleValueType LSXVTs[] = {
61 MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64};
62 static const MVT::SimpleValueType LASXVTs[] = {
63 MVT::v32i8, MVT::v16i16, MVT::v8i32, MVT::v4i64, MVT::v8f32, MVT::v4f64};
64
65 if (Subtarget.hasExtLSX())
66 for (MVT VT : LSXVTs)
67 addRegisterClass(VT, &LoongArch::LSX128RegClass);
68
69 if (Subtarget.hasExtLASX())
70 for (MVT VT : LASXVTs)
71 addRegisterClass(VT, &LoongArch::LASX256RegClass);
72
73 // Set operations for LA32 and LA64.
74
76 MVT::i1, Promote);
77
84
87 GRLenVT, Custom);
88
90
95
98
102
103 // Expand bitreverse.i16 with native-width bitrev and shift for now, before
104 // we get to know which of sll and revb.2h is faster.
107
108 // LA32 does not have REVB.2W and REVB.D due to the 64-bit operands, and
109 // the narrower REVB.W does not exist. But LA32 does have REVB.2H, so i16
110 // and i32 could still be byte-swapped relatively cheaply.
112
118
121
122 // Set operations for LA64 only.
123
124 if (Subtarget.is64Bit()) {
142
146 }
147
148 // Set operations for LA32 only.
149
150 if (!Subtarget.is64Bit()) {
156 }
157
159
160 static const ISD::CondCode FPCCToExpand[] = {
163
164 // Set operations for 'F' feature.
165
166 if (Subtarget.hasBasicF()) {
167 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
168 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
169 setCondCodeAction(FPCCToExpand, MVT::f32, Expand);
170
186
187 if (Subtarget.is64Bit())
189
190 if (!Subtarget.hasBasicD()) {
192 if (Subtarget.is64Bit()) {
195 }
196 }
197 }
198
199 // Set operations for 'D' feature.
200
201 if (Subtarget.hasBasicD()) {
202 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
203 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
204 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
205 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
206 setCondCodeAction(FPCCToExpand, MVT::f64, Expand);
207
223
224 if (Subtarget.is64Bit())
226 }
227
228 // Set operations for 'LSX' feature.
229
230 if (Subtarget.hasExtLSX()) {
232 // Expand all truncating stores and extending loads.
233 for (MVT InnerVT : MVT::fixedlen_vector_valuetypes()) {
234 setTruncStoreAction(VT, InnerVT, Expand);
237 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
238 }
239 // By default everything must be expanded. Then we will selectively turn
240 // on ones that can be effectively codegen'd.
241 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op)
243 }
244
245 for (MVT VT : LSXVTs) {
249
253
257 }
258 for (MVT VT : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64}) {
261 Legal);
263 VT, Legal);
270 Expand);
271 }
272 for (MVT VT : {MVT::v4i32, MVT::v2i64}) {
275 }
276 for (MVT VT : {MVT::v4f32, MVT::v2f64}) {
284 VT, Expand);
285 }
286 }
287
288 // Set operations for 'LASX' feature.
289
290 if (Subtarget.hasExtLASX()) {
291 for (MVT VT : LASXVTs) {
295
299
303 }
304 for (MVT VT : {MVT::v4i64, MVT::v8i32, MVT::v16i16, MVT::v32i8}) {
307 Legal);
309 VT, Legal);
316 Expand);
317 }
318 for (MVT VT : {MVT::v8i32, MVT::v4i32, MVT::v4i64}) {
321 }
322 for (MVT VT : {MVT::v8f32, MVT::v4f64}) {
330 VT, Expand);
331 }
332 }
333
334 // Set DAG combine for LA32 and LA64.
335
340
341 // Set DAG combine for 'LSX' feature.
342
343 if (Subtarget.hasExtLSX())
345
346 // Compute derived properties from the register classes.
348
350
353
355
357
358 // Function alignments.
360 // Set preferred alignments.
364}
365
367 const GlobalAddressSDNode *GA) const {
368 // In order to maximise the opportunity for common subexpression elimination,
369 // keep a separate ADD node for the global address offset instead of folding
370 // it in the global address node. Later peephole optimisations may choose to
371 // fold it back in when profitable.
372 return false;
373}
374
376 SelectionDAG &DAG) const {
377 switch (Op.getOpcode()) {
379 return lowerATOMIC_FENCE(Op, DAG);
381 return lowerEH_DWARF_CFA(Op, DAG);
383 return lowerGlobalAddress(Op, DAG);
385 return lowerGlobalTLSAddress(Op, DAG);
387 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
389 return lowerINTRINSIC_W_CHAIN(Op, DAG);
391 return lowerINTRINSIC_VOID(Op, DAG);
393 return lowerBlockAddress(Op, DAG);
394 case ISD::JumpTable:
395 return lowerJumpTable(Op, DAG);
396 case ISD::SHL_PARTS:
397 return lowerShiftLeftParts(Op, DAG);
398 case ISD::SRA_PARTS:
399 return lowerShiftRightParts(Op, DAG, true);
400 case ISD::SRL_PARTS:
401 return lowerShiftRightParts(Op, DAG, false);
403 return lowerConstantPool(Op, DAG);
404 case ISD::FP_TO_SINT:
405 return lowerFP_TO_SINT(Op, DAG);
406 case ISD::BITCAST:
407 return lowerBITCAST(Op, DAG);
408 case ISD::UINT_TO_FP:
409 return lowerUINT_TO_FP(Op, DAG);
410 case ISD::SINT_TO_FP:
411 return lowerSINT_TO_FP(Op, DAG);
412 case ISD::VASTART:
413 return lowerVASTART(Op, DAG);
414 case ISD::FRAMEADDR:
415 return lowerFRAMEADDR(Op, DAG);
416 case ISD::RETURNADDR:
417 return lowerRETURNADDR(Op, DAG);
419 return lowerWRITE_REGISTER(Op, DAG);
421 return lowerINSERT_VECTOR_ELT(Op, DAG);
423 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
425 return lowerBUILD_VECTOR(Op, DAG);
427 return lowerVECTOR_SHUFFLE(Op, DAG);
428 }
429 return SDValue();
430}
431
432/// Determine whether a range fits a regular pattern of values.
433/// This function accounts for the possibility of jumping over the End iterator.
434template <typename ValType>
435static bool
437 unsigned CheckStride,
439 ValType ExpectedIndex, unsigned ExpectedIndexStride) {
440 auto &I = Begin;
441
442 while (I != End) {
443 if (*I != -1 && *I != ExpectedIndex)
444 return false;
445 ExpectedIndex += ExpectedIndexStride;
446
447 // Incrementing past End is undefined behaviour so we must increment one
448 // step at a time and check for End at each step.
449 for (unsigned n = 0; n < CheckStride && I != End; ++n, ++I)
450 ; // Empty loop body.
451 }
452 return true;
453}
454
455/// Lower VECTOR_SHUFFLE into VREPLVEI (if possible).
456///
457/// VREPLVEI performs vector broadcast based on an element specified by an
458/// integer immediate, with its mask being similar to:
459/// <x, x, x, ...>
460/// where x is any valid index.
461///
462/// When undef's appear in the mask they are treated as if they were whatever
463/// value is necessary in order to fit the above form.
465 MVT VT, SDValue V1, SDValue V2,
466 SelectionDAG &DAG) {
467 int SplatIndex = -1;
468 for (const auto &M : Mask) {
469 if (M != -1) {
470 SplatIndex = M;
471 break;
472 }
473 }
474
475 if (SplatIndex == -1)
476 return DAG.getUNDEF(VT);
477
478 assert(SplatIndex < (int)Mask.size() && "Out of bounds mask index");
479 if (fitsRegularPattern<int>(Mask.begin(), 1, Mask.end(), SplatIndex, 0)) {
480 APInt Imm(64, SplatIndex);
481 return DAG.getNode(LoongArchISD::VREPLVEI, DL, VT, V1,
482 DAG.getConstant(Imm, DL, MVT::i64));
483 }
484
485 return SDValue();
486}
487
488/// Lower VECTOR_SHUFFLE into VSHUF4I (if possible).
489///
490/// VSHUF4I splits the vector into blocks of four elements, then shuffles these
491/// elements according to a <4 x i2> constant (encoded as an integer immediate).
492///
493/// It is therefore possible to lower into VSHUF4I when the mask takes the form:
494/// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
495/// When undef's appear they are treated as if they were whatever value is
496/// necessary in order to fit the above forms.
497///
498/// For example:
499/// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
500/// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
501/// i32 7, i32 6, i32 5, i32 4>
502/// is lowered to:
503/// (VSHUF4I_H $v0, $v1, 27)
504/// where the 27 comes from:
505/// 3 + (2 << 2) + (1 << 4) + (0 << 6)
507 MVT VT, SDValue V1, SDValue V2,
508 SelectionDAG &DAG) {
509
510 // When the size is less than 4, lower cost instructions may be used.
511 if (Mask.size() < 4)
512 return SDValue();
513
514 int SubMask[4] = {-1, -1, -1, -1};
515 for (unsigned i = 0; i < 4; ++i) {
516 for (unsigned j = i; j < Mask.size(); j += 4) {
517 int Idx = Mask[j];
518
519 // Convert from vector index to 4-element subvector index
520 // If an index refers to an element outside of the subvector then give up
521 if (Idx != -1) {
522 Idx -= 4 * (j / 4);
523 if (Idx < 0 || Idx >= 4)
524 return SDValue();
525 }
526
527 // If the mask has an undef, replace it with the current index.
528 // Note that it might still be undef if the current index is also undef
529 if (SubMask[i] == -1)
530 SubMask[i] = Idx;
531 // Check that non-undef values are the same as in the mask. If they
532 // aren't then give up
533 else if (Idx != -1 && Idx != SubMask[i])
534 return SDValue();
535 }
536 }
537
538 // Calculate the immediate. Replace any remaining undefs with zero
539 APInt Imm(64, 0);
540 for (int i = 3; i >= 0; --i) {
541 int Idx = SubMask[i];
542
543 if (Idx == -1)
544 Idx = 0;
545
546 Imm <<= 2;
547 Imm |= Idx & 0x3;
548 }
549
550 return DAG.getNode(LoongArchISD::VSHUF4I, DL, VT, V1,
551 DAG.getConstant(Imm, DL, MVT::i64));
552}
553
554/// Lower VECTOR_SHUFFLE into VPACKEV (if possible).
555///
556/// VPACKEV interleaves the even elements from each vector.
557///
558/// It is possible to lower into VPACKEV when the mask consists of two of the
559/// following forms interleaved:
560/// <0, 2, 4, ...>
561/// <n, n+2, n+4, ...>
562/// where n is the number of elements in the vector.
563/// For example:
564/// <0, 0, 2, 2, 4, 4, ...>
565/// <0, n, 2, n+2, 4, n+4, ...>
566///
567/// When undef's appear in the mask they are treated as if they were whatever
568/// value is necessary in order to fit the above forms.
570 MVT VT, SDValue V1, SDValue V2,
571 SelectionDAG &DAG) {
572
573 const auto &Begin = Mask.begin();
574 const auto &End = Mask.end();
575 SDValue OriV1 = V1, OriV2 = V2;
576
577 if (fitsRegularPattern<int>(Begin, 2, End, 0, 2))
578 V1 = OriV1;
579 else if (fitsRegularPattern<int>(Begin, 2, End, Mask.size(), 2))
580 V1 = OriV2;
581 else
582 return SDValue();
583
584 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 2))
585 V2 = OriV1;
586 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Mask.size(), 2))
587 V2 = OriV2;
588 else
589 return SDValue();
590
591 return DAG.getNode(LoongArchISD::VPACKEV, DL, VT, V2, V1);
592}
593
594/// Lower VECTOR_SHUFFLE into VPACKOD (if possible).
595///
596/// VPACKOD interleaves the odd elements from each vector.
597///
598/// It is possible to lower into VPACKOD when the mask consists of two of the
599/// following forms interleaved:
600/// <1, 3, 5, ...>
601/// <n+1, n+3, n+5, ...>
602/// where n is the number of elements in the vector.
603/// For example:
604/// <1, 1, 3, 3, 5, 5, ...>
605/// <1, n+1, 3, n+3, 5, n+5, ...>
606///
607/// When undef's appear in the mask they are treated as if they were whatever
608/// value is necessary in order to fit the above forms.
610 MVT VT, SDValue V1, SDValue V2,
611 SelectionDAG &DAG) {
612
613 const auto &Begin = Mask.begin();
614 const auto &End = Mask.end();
615 SDValue OriV1 = V1, OriV2 = V2;
616
617 if (fitsRegularPattern<int>(Begin, 2, End, 1, 2))
618 V1 = OriV1;
619 else if (fitsRegularPattern<int>(Begin, 2, End, Mask.size() + 1, 2))
620 V1 = OriV2;
621 else
622 return SDValue();
623
624 if (fitsRegularPattern<int>(Begin + 1, 2, End, 1, 2))
625 V2 = OriV1;
626 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Mask.size() + 1, 2))
627 V2 = OriV2;
628 else
629 return SDValue();
630
631 return DAG.getNode(LoongArchISD::VPACKOD, DL, VT, V2, V1);
632}
633
634/// Lower VECTOR_SHUFFLE into VILVH (if possible).
635///
636/// VILVH interleaves consecutive elements from the left (highest-indexed) half
637/// of each vector.
638///
639/// It is possible to lower into VILVH when the mask consists of two of the
640/// following forms interleaved:
641/// <x, x+1, x+2, ...>
642/// <n+x, n+x+1, n+x+2, ...>
643/// where n is the number of elements in the vector and x is half n.
644/// For example:
645/// <x, x, x+1, x+1, x+2, x+2, ...>
646/// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
647///
648/// When undef's appear in the mask they are treated as if they were whatever
649/// value is necessary in order to fit the above forms.
651 MVT VT, SDValue V1, SDValue V2,
652 SelectionDAG &DAG) {
653
654 const auto &Begin = Mask.begin();
655 const auto &End = Mask.end();
656 unsigned HalfSize = Mask.size() / 2;
657 SDValue OriV1 = V1, OriV2 = V2;
658
659 if (fitsRegularPattern<int>(Begin, 2, End, HalfSize, 1))
660 V1 = OriV1;
661 else if (fitsRegularPattern<int>(Begin, 2, End, Mask.size() + HalfSize, 1))
662 V1 = OriV2;
663 else
664 return SDValue();
665
666 if (fitsRegularPattern<int>(Begin + 1, 2, End, HalfSize, 1))
667 V2 = OriV1;
668 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Mask.size() + HalfSize,
669 1))
670 V2 = OriV2;
671 else
672 return SDValue();
673
674 return DAG.getNode(LoongArchISD::VILVH, DL, VT, V2, V1);
675}
676
677/// Lower VECTOR_SHUFFLE into VILVL (if possible).
678///
679/// VILVL interleaves consecutive elements from the right (lowest-indexed) half
680/// of each vector.
681///
682/// It is possible to lower into VILVL when the mask consists of two of the
683/// following forms interleaved:
684/// <0, 1, 2, ...>
685/// <n, n+1, n+2, ...>
686/// where n is the number of elements in the vector.
687/// For example:
688/// <0, 0, 1, 1, 2, 2, ...>
689/// <0, n, 1, n+1, 2, n+2, ...>
690///
691/// When undef's appear in the mask they are treated as if they were whatever
692/// value is necessary in order to fit the above forms.
694 MVT VT, SDValue V1, SDValue V2,
695 SelectionDAG &DAG) {
696
697 const auto &Begin = Mask.begin();
698 const auto &End = Mask.end();
699 SDValue OriV1 = V1, OriV2 = V2;
700
701 if (fitsRegularPattern<int>(Begin, 2, End, 0, 1))
702 V1 = OriV1;
703 else if (fitsRegularPattern<int>(Begin, 2, End, Mask.size(), 1))
704 V1 = OriV2;
705 else
706 return SDValue();
707
708 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 1))
709 V2 = OriV1;
710 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Mask.size(), 1))
711 V2 = OriV2;
712 else
713 return SDValue();
714
715 return DAG.getNode(LoongArchISD::VILVL, DL, VT, V2, V1);
716}
717
718/// Lower VECTOR_SHUFFLE into VPICKEV (if possible).
719///
720/// VPICKEV copies the even elements of each vector into the result vector.
721///
722/// It is possible to lower into VPICKEV when the mask consists of two of the
723/// following forms concatenated:
724/// <0, 2, 4, ...>
725/// <n, n+2, n+4, ...>
726/// where n is the number of elements in the vector.
727/// For example:
728/// <0, 2, 4, ..., 0, 2, 4, ...>
729/// <0, 2, 4, ..., n, n+2, n+4, ...>
730///
731/// When undef's appear in the mask they are treated as if they were whatever
732/// value is necessary in order to fit the above forms.
734 MVT VT, SDValue V1, SDValue V2,
735 SelectionDAG &DAG) {
736
737 const auto &Begin = Mask.begin();
738 const auto &Mid = Mask.begin() + Mask.size() / 2;
739 const auto &End = Mask.end();
740 SDValue OriV1 = V1, OriV2 = V2;
741
742 if (fitsRegularPattern<int>(Begin, 1, Mid, 0, 2))
743 V1 = OriV1;
744 else if (fitsRegularPattern<int>(Begin, 1, Mid, Mask.size(), 2))
745 V1 = OriV2;
746 else
747 return SDValue();
748
749 if (fitsRegularPattern<int>(Mid, 1, End, 0, 2))
750 V2 = OriV1;
751 else if (fitsRegularPattern<int>(Mid, 1, End, Mask.size(), 2))
752 V2 = OriV2;
753
754 else
755 return SDValue();
756
757 return DAG.getNode(LoongArchISD::VPICKEV, DL, VT, V2, V1);
758}
759
760/// Lower VECTOR_SHUFFLE into VPICKOD (if possible).
761///
762/// VPICKOD copies the odd elements of each vector into the result vector.
763///
764/// It is possible to lower into VPICKOD when the mask consists of two of the
765/// following forms concatenated:
766/// <1, 3, 5, ...>
767/// <n+1, n+3, n+5, ...>
768/// where n is the number of elements in the vector.
769/// For example:
770/// <1, 3, 5, ..., 1, 3, 5, ...>
771/// <1, 3, 5, ..., n+1, n+3, n+5, ...>
772///
773/// When undef's appear in the mask they are treated as if they were whatever
774/// value is necessary in order to fit the above forms.
776 MVT VT, SDValue V1, SDValue V2,
777 SelectionDAG &DAG) {
778
779 const auto &Begin = Mask.begin();
780 const auto &Mid = Mask.begin() + Mask.size() / 2;
781 const auto &End = Mask.end();
782 SDValue OriV1 = V1, OriV2 = V2;
783
784 if (fitsRegularPattern<int>(Begin, 1, Mid, 1, 2))
785 V1 = OriV1;
786 else if (fitsRegularPattern<int>(Begin, 1, Mid, Mask.size() + 1, 2))
787 V1 = OriV2;
788 else
789 return SDValue();
790
791 if (fitsRegularPattern<int>(Mid, 1, End, 1, 2))
792 V2 = OriV1;
793 else if (fitsRegularPattern<int>(Mid, 1, End, Mask.size() + 1, 2))
794 V2 = OriV2;
795 else
796 return SDValue();
797
798 return DAG.getNode(LoongArchISD::VPICKOD, DL, VT, V2, V1);
799}
800
801/// Lower VECTOR_SHUFFLE into VSHUF.
802///
803/// This mostly consists of converting the shuffle mask into a BUILD_VECTOR and
804/// adding it as an operand to the resulting VSHUF.
806 MVT VT, SDValue V1, SDValue V2,
807 SelectionDAG &DAG) {
808
810 for (auto M : Mask)
811 Ops.push_back(DAG.getConstant(M, DL, MVT::i64));
812
813 EVT MaskVecTy = VT.changeVectorElementTypeToInteger();
814 SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, Ops);
815
816 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
817 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
818 // VSHF concatenates the vectors in a bitwise fashion:
819 // <0b00, 0b01> + <0b10, 0b11> ->
820 // 0b0100 + 0b1110 -> 0b01001110
821 // <0b10, 0b11, 0b00, 0b01>
822 // We must therefore swap the operands to get the correct result.
823 return DAG.getNode(LoongArchISD::VSHUF, DL, VT, MaskVec, V2, V1);
824}
825
826/// Dispatching routine to lower various 128-bit LoongArch vector shuffles.
827///
828/// This routine breaks down the specific type of 128-bit shuffle and
829/// dispatches to the lowering routines accordingly.
831 SDValue V1, SDValue V2, SelectionDAG &DAG) {
832 assert((VT.SimpleTy == MVT::v16i8 || VT.SimpleTy == MVT::v8i16 ||
833 VT.SimpleTy == MVT::v4i32 || VT.SimpleTy == MVT::v2i64 ||
834 VT.SimpleTy == MVT::v4f32 || VT.SimpleTy == MVT::v2f64) &&
835 "Vector type is unsupported for lsx!");
836 assert(V1.getSimpleValueType() == V2.getSimpleValueType() &&
837 "Two operands have different types!");
838 assert(VT.getVectorNumElements() == Mask.size() &&
839 "Unexpected mask size for shuffle!");
840 assert(Mask.size() % 2 == 0 && "Expected even mask size.");
841
842 SDValue Result;
843 // TODO: Add more comparison patterns.
844 if (V2.isUndef()) {
845 if ((Result = lowerVECTOR_SHUFFLE_VREPLVEI(DL, Mask, VT, V1, V2, DAG)))
846 return Result;
847 if ((Result = lowerVECTOR_SHUFFLE_VSHUF4I(DL, Mask, VT, V1, V2, DAG)))
848 return Result;
849
850 // TODO: This comment may be enabled in the future to better match the
851 // pattern for instruction selection.
852 /* V2 = V1; */
853 }
854
855 // It is recommended not to change the pattern comparison order for better
856 // performance.
857 if ((Result = lowerVECTOR_SHUFFLE_VPACKEV(DL, Mask, VT, V1, V2, DAG)))
858 return Result;
859 if ((Result = lowerVECTOR_SHUFFLE_VPACKOD(DL, Mask, VT, V1, V2, DAG)))
860 return Result;
861 if ((Result = lowerVECTOR_SHUFFLE_VILVH(DL, Mask, VT, V1, V2, DAG)))
862 return Result;
863 if ((Result = lowerVECTOR_SHUFFLE_VILVL(DL, Mask, VT, V1, V2, DAG)))
864 return Result;
865 if ((Result = lowerVECTOR_SHUFFLE_VPICKEV(DL, Mask, VT, V1, V2, DAG)))
866 return Result;
867 if ((Result = lowerVECTOR_SHUFFLE_VPICKOD(DL, Mask, VT, V1, V2, DAG)))
868 return Result;
869 if ((Result = lowerVECTOR_SHUFFLE_VSHUF(DL, Mask, VT, V1, V2, DAG)))
870 return Result;
871
872 return SDValue();
873}
874
875/// Lower VECTOR_SHUFFLE into XVREPLVEI (if possible).
876///
877/// It is a XVREPLVEI when the mask is:
878/// <x, x, x, ..., x+n, x+n, x+n, ...>
879/// where the number of x is equal to n and n is half the length of vector.
880///
881/// When undef's appear in the mask they are treated as if they were whatever
882/// value is necessary in order to fit the above form.
884 ArrayRef<int> Mask, MVT VT,
885 SDValue V1, SDValue V2,
886 SelectionDAG &DAG) {
887 int SplatIndex = -1;
888 for (const auto &M : Mask) {
889 if (M != -1) {
890 SplatIndex = M;
891 break;
892 }
893 }
894
895 if (SplatIndex == -1)
896 return DAG.getUNDEF(VT);
897
898 const auto &Begin = Mask.begin();
899 const auto &End = Mask.end();
900 unsigned HalfSize = Mask.size() / 2;
901
902 assert(SplatIndex < (int)Mask.size() && "Out of bounds mask index");
903 if (fitsRegularPattern<int>(Begin, 1, End - HalfSize, SplatIndex, 0) &&
904 fitsRegularPattern<int>(Begin + HalfSize, 1, End, SplatIndex + HalfSize,
905 0)) {
906 APInt Imm(64, SplatIndex);
907 return DAG.getNode(LoongArchISD::VREPLVEI, DL, VT, V1,
908 DAG.getConstant(Imm, DL, MVT::i64));
909 }
910
911 return SDValue();
912}
913
914/// Lower VECTOR_SHUFFLE into XVSHUF4I (if possible).
916 MVT VT, SDValue V1, SDValue V2,
917 SelectionDAG &DAG) {
918 // When the size is less than or equal to 4, lower cost instructions may be
919 // used.
920 if (Mask.size() <= 4)
921 return SDValue();
922 return lowerVECTOR_SHUFFLE_VSHUF4I(DL, Mask, VT, V1, V2, DAG);
923}
924
925/// Lower VECTOR_SHUFFLE into XVPACKEV (if possible).
927 MVT VT, SDValue V1, SDValue V2,
928 SelectionDAG &DAG) {
929 return lowerVECTOR_SHUFFLE_VPACKEV(DL, Mask, VT, V1, V2, DAG);
930}
931
932/// Lower VECTOR_SHUFFLE into XVPACKOD (if possible).
934 MVT VT, SDValue V1, SDValue V2,
935 SelectionDAG &DAG) {
936 return lowerVECTOR_SHUFFLE_VPACKOD(DL, Mask, VT, V1, V2, DAG);
937}
938
939/// Lower VECTOR_SHUFFLE into XVILVH (if possible).
941 MVT VT, SDValue V1, SDValue V2,
942 SelectionDAG &DAG) {
943
944 const auto &Begin = Mask.begin();
945 const auto &End = Mask.end();
946 unsigned HalfSize = Mask.size() / 2;
947 unsigned LeftSize = HalfSize / 2;
948 SDValue OriV1 = V1, OriV2 = V2;
949
950 if (fitsRegularPattern<int>(Begin, 2, End - HalfSize, HalfSize - LeftSize,
951 1) &&
952 fitsRegularPattern<int>(Begin + HalfSize, 2, End, HalfSize + LeftSize, 1))
953 V1 = OriV1;
954 else if (fitsRegularPattern<int>(Begin, 2, End - HalfSize,
955 Mask.size() + HalfSize - LeftSize, 1) &&
956 fitsRegularPattern<int>(Begin + HalfSize, 2, End,
957 Mask.size() + HalfSize + LeftSize, 1))
958 V1 = OriV2;
959 else
960 return SDValue();
961
962 if (fitsRegularPattern<int>(Begin + 1, 2, End - HalfSize, HalfSize - LeftSize,
963 1) &&
964 fitsRegularPattern<int>(Begin + 1 + HalfSize, 2, End, HalfSize + LeftSize,
965 1))
966 V2 = OriV1;
967 else if (fitsRegularPattern<int>(Begin + 1, 2, End - HalfSize,
968 Mask.size() + HalfSize - LeftSize, 1) &&
969 fitsRegularPattern<int>(Begin + 1 + HalfSize, 2, End,
970 Mask.size() + HalfSize + LeftSize, 1))
971 V2 = OriV2;
972 else
973 return SDValue();
974
975 return DAG.getNode(LoongArchISD::VILVH, DL, VT, V2, V1);
976}
977
978/// Lower VECTOR_SHUFFLE into XVILVL (if possible).
980 MVT VT, SDValue V1, SDValue V2,
981 SelectionDAG &DAG) {
982
983 const auto &Begin = Mask.begin();
984 const auto &End = Mask.end();
985 unsigned HalfSize = Mask.size() / 2;
986 SDValue OriV1 = V1, OriV2 = V2;
987
988 if (fitsRegularPattern<int>(Begin, 2, End - HalfSize, 0, 1) &&
989 fitsRegularPattern<int>(Begin + HalfSize, 2, End, HalfSize, 1))
990 V1 = OriV1;
991 else if (fitsRegularPattern<int>(Begin, 2, End - HalfSize, Mask.size(), 1) &&
992 fitsRegularPattern<int>(Begin + HalfSize, 2, End,
993 Mask.size() + HalfSize, 1))
994 V1 = OriV2;
995 else
996 return SDValue();
997
998 if (fitsRegularPattern<int>(Begin + 1, 2, End - HalfSize, 0, 1) &&
999 fitsRegularPattern<int>(Begin + 1 + HalfSize, 2, End, HalfSize, 1))
1000 V2 = OriV1;
1001 else if (fitsRegularPattern<int>(Begin + 1, 2, End - HalfSize, Mask.size(),
1002 1) &&
1003 fitsRegularPattern<int>(Begin + 1 + HalfSize, 2, End,
1004 Mask.size() + HalfSize, 1))
1005 V2 = OriV2;
1006 else
1007 return SDValue();
1008
1009 return DAG.getNode(LoongArchISD::VILVL, DL, VT, V2, V1);
1010}
1011
1012/// Lower VECTOR_SHUFFLE into XVPICKEV (if possible).
1014 MVT VT, SDValue V1, SDValue V2,
1015 SelectionDAG &DAG) {
1016
1017 const auto &Begin = Mask.begin();
1018 const auto &LeftMid = Mask.begin() + Mask.size() / 4;
1019 const auto &Mid = Mask.begin() + Mask.size() / 2;
1020 const auto &RightMid = Mask.end() - Mask.size() / 4;
1021 const auto &End = Mask.end();
1022 unsigned HalfSize = Mask.size() / 2;
1023 SDValue OriV1 = V1, OriV2 = V2;
1024
1025 if (fitsRegularPattern<int>(Begin, 1, LeftMid, 0, 2) &&
1026 fitsRegularPattern<int>(Mid, 1, RightMid, HalfSize, 2))
1027 V1 = OriV1;
1028 else if (fitsRegularPattern<int>(Begin, 1, LeftMid, Mask.size(), 2) &&
1029 fitsRegularPattern<int>(Mid, 1, RightMid, Mask.size() + HalfSize, 2))
1030 V1 = OriV2;
1031 else
1032 return SDValue();
1033
1034 if (fitsRegularPattern<int>(LeftMid, 1, Mid, 0, 2) &&
1035 fitsRegularPattern<int>(RightMid, 1, End, HalfSize, 2))
1036 V2 = OriV1;
1037 else if (fitsRegularPattern<int>(LeftMid, 1, Mid, Mask.size(), 2) &&
1038 fitsRegularPattern<int>(RightMid, 1, End, Mask.size() + HalfSize, 2))
1039 V2 = OriV2;
1040
1041 else
1042 return SDValue();
1043
1044 return DAG.getNode(LoongArchISD::VPICKEV, DL, VT, V2, V1);
1045}
1046
1047/// Lower VECTOR_SHUFFLE into XVPICKOD (if possible).
1049 MVT VT, SDValue V1, SDValue V2,
1050 SelectionDAG &DAG) {
1051
1052 const auto &Begin = Mask.begin();
1053 const auto &LeftMid = Mask.begin() + Mask.size() / 4;
1054 const auto &Mid = Mask.begin() + Mask.size() / 2;
1055 const auto &RightMid = Mask.end() - Mask.size() / 4;
1056 const auto &End = Mask.end();
1057 unsigned HalfSize = Mask.size() / 2;
1058 SDValue OriV1 = V1, OriV2 = V2;
1059
1060 if (fitsRegularPattern<int>(Begin, 1, LeftMid, 1, 2) &&
1061 fitsRegularPattern<int>(Mid, 1, RightMid, HalfSize + 1, 2))
1062 V1 = OriV1;
1063 else if (fitsRegularPattern<int>(Begin, 1, LeftMid, Mask.size() + 1, 2) &&
1064 fitsRegularPattern<int>(Mid, 1, RightMid, Mask.size() + HalfSize + 1,
1065 2))
1066 V1 = OriV2;
1067 else
1068 return SDValue();
1069
1070 if (fitsRegularPattern<int>(LeftMid, 1, Mid, 1, 2) &&
1071 fitsRegularPattern<int>(RightMid, 1, End, HalfSize + 1, 2))
1072 V2 = OriV1;
1073 else if (fitsRegularPattern<int>(LeftMid, 1, Mid, Mask.size() + 1, 2) &&
1074 fitsRegularPattern<int>(RightMid, 1, End, Mask.size() + HalfSize + 1,
1075 2))
1076 V2 = OriV2;
1077 else
1078 return SDValue();
1079
1080 return DAG.getNode(LoongArchISD::VPICKOD, DL, VT, V2, V1);
1081}
1082
1083/// Lower VECTOR_SHUFFLE into XVSHUF (if possible).
1085 MVT VT, SDValue V1, SDValue V2,
1086 SelectionDAG &DAG) {
1087
1088 int MaskSize = Mask.size();
1089 int HalfSize = Mask.size() / 2;
1090 const auto &Begin = Mask.begin();
1091 const auto &Mid = Mask.begin() + HalfSize;
1092 const auto &End = Mask.end();
1093
1094 // VECTOR_SHUFFLE concatenates the vectors:
1095 // <0, 1, 2, 3, 4, 5, 6, 7> + <8, 9, 10, 11, 12, 13, 14, 15>
1096 // shuffling ->
1097 // <0, 1, 2, 3, 8, 9, 10, 11> <4, 5, 6, 7, 12, 13, 14, 15>
1098 //
1099 // XVSHUF concatenates the vectors:
1100 // <a0, a1, a2, a3, b0, b1, b2, b3> + <a4, a5, a6, a7, b4, b5, b6, b7>
1101 // shuffling ->
1102 // <a0, a1, a2, a3, a4, a5, a6, a7> + <b0, b1, b2, b3, b4, b5, b6, b7>
1103 SmallVector<SDValue, 8> MaskAlloc;
1104 for (auto it = Begin; it < Mid; it++) {
1105 if (*it < 0) // UNDEF
1106 MaskAlloc.push_back(DAG.getTargetConstant(0, DL, MVT::i64));
1107 else if ((*it >= 0 && *it < HalfSize) ||
1108 (*it >= MaskSize && *it <= MaskSize + HalfSize)) {
1109 int M = *it < HalfSize ? *it : *it - HalfSize;
1110 MaskAlloc.push_back(DAG.getTargetConstant(M, DL, MVT::i64));
1111 } else
1112 return SDValue();
1113 }
1114 assert((int)MaskAlloc.size() == HalfSize && "xvshuf convert failed!");
1115
1116 for (auto it = Mid; it < End; it++) {
1117 if (*it < 0) // UNDEF
1118 MaskAlloc.push_back(DAG.getTargetConstant(0, DL, MVT::i64));
1119 else if ((*it >= HalfSize && *it < MaskSize) ||
1120 (*it >= MaskSize + HalfSize && *it < MaskSize * 2)) {
1121 int M = *it < MaskSize ? *it - HalfSize : *it - MaskSize;
1122 MaskAlloc.push_back(DAG.getTargetConstant(M, DL, MVT::i64));
1123 } else
1124 return SDValue();
1125 }
1126 assert((int)MaskAlloc.size() == MaskSize && "xvshuf convert failed!");
1127
1128 EVT MaskVecTy = VT.changeVectorElementTypeToInteger();
1129 SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, MaskAlloc);
1130 return DAG.getNode(LoongArchISD::VSHUF, DL, VT, MaskVec, V2, V1);
1131}
1132
1133/// Shuffle vectors by lane to generate more optimized instructions.
1134/// 256-bit shuffles are always considered as 2-lane 128-bit shuffles.
1135///
1136/// Therefore, except for the following four cases, other cases are regarded
1137/// as cross-lane shuffles, where optimization is relatively limited.
1138///
1139/// - Shuffle high, low lanes of two inputs vector
1140/// <0, 1, 2, 3> + <4, 5, 6, 7> --- <0, 5, 3, 6>
1141/// - Shuffle low, high lanes of two inputs vector
1142/// <0, 1, 2, 3> + <4, 5, 6, 7> --- <3, 6, 0, 5>
1143/// - Shuffle low, low lanes of two inputs vector
1144/// <0, 1, 2, 3> + <4, 5, 6, 7> --- <3, 6, 3, 6>
1145/// - Shuffle high, high lanes of two inputs vector
1146/// <0, 1, 2, 3> + <4, 5, 6, 7> --- <0, 5, 0, 5>
1147///
1148/// The first case is the closest to LoongArch instructions and the other
1149/// cases need to be converted to it for processing.
1150///
1151/// This function may modify V1, V2 and Mask
1153 MutableArrayRef<int> Mask, MVT VT,
1154 SDValue &V1, SDValue &V2,
1155 SelectionDAG &DAG) {
1156
1157 enum HalfMaskType { HighLaneTy, LowLaneTy, None };
1158
1159 int MaskSize = Mask.size();
1160 int HalfSize = Mask.size() / 2;
1161
1162 HalfMaskType preMask = None, postMask = None;
1163
1164 if (std::all_of(Mask.begin(), Mask.begin() + HalfSize, [&](int M) {
1165 return M < 0 || (M >= 0 && M < HalfSize) ||
1166 (M >= MaskSize && M < MaskSize + HalfSize);
1167 }))
1168 preMask = HighLaneTy;
1169 else if (std::all_of(Mask.begin(), Mask.begin() + HalfSize, [&](int M) {
1170 return M < 0 || (M >= HalfSize && M < MaskSize) ||
1171 (M >= MaskSize + HalfSize && M < MaskSize * 2);
1172 }))
1173 preMask = LowLaneTy;
1174
1175 if (std::all_of(Mask.begin() + HalfSize, Mask.end(), [&](int M) {
1176 return M < 0 || (M >= 0 && M < HalfSize) ||
1177 (M >= MaskSize && M < MaskSize + HalfSize);
1178 }))
1179 postMask = HighLaneTy;
1180 else if (std::all_of(Mask.begin() + HalfSize, Mask.end(), [&](int M) {
1181 return M < 0 || (M >= HalfSize && M < MaskSize) ||
1182 (M >= MaskSize + HalfSize && M < MaskSize * 2);
1183 }))
1184 postMask = LowLaneTy;
1185
1186 // The pre-half of mask is high lane type, and the post-half of mask
1187 // is low lane type, which is closest to the LoongArch instructions.
1188 //
1189 // Note: In the LoongArch architecture, the high lane of mask corresponds
1190 // to the lower 128-bit of vector register, and the low lane of mask
1191 // corresponds the higher 128-bit of vector register.
1192 if (preMask == HighLaneTy && postMask == LowLaneTy) {
1193 return;
1194 }
1195 if (preMask == LowLaneTy && postMask == HighLaneTy) {
1196 V1 = DAG.getBitcast(MVT::v4i64, V1);
1197 V1 = DAG.getNode(LoongArchISD::XVPERMI, DL, MVT::v4i64, V1,
1198 DAG.getConstant(0b01001110, DL, MVT::i64));
1199 V1 = DAG.getBitcast(VT, V1);
1200
1201 if (!V2.isUndef()) {
1202 V2 = DAG.getBitcast(MVT::v4i64, V2);
1203 V2 = DAG.getNode(LoongArchISD::XVPERMI, DL, MVT::v4i64, V2,
1204 DAG.getConstant(0b01001110, DL, MVT::i64));
1205 V2 = DAG.getBitcast(VT, V2);
1206 }
1207
1208 for (auto it = Mask.begin(); it < Mask.begin() + HalfSize; it++) {
1209 *it = *it < 0 ? *it : *it - HalfSize;
1210 }
1211 for (auto it = Mask.begin() + HalfSize; it < Mask.end(); it++) {
1212 *it = *it < 0 ? *it : *it + HalfSize;
1213 }
1214 } else if (preMask == LowLaneTy && postMask == LowLaneTy) {
1215 V1 = DAG.getBitcast(MVT::v4i64, V1);
1216 V1 = DAG.getNode(LoongArchISD::XVPERMI, DL, MVT::v4i64, V1,
1217 DAG.getConstant(0b11101110, DL, MVT::i64));
1218 V1 = DAG.getBitcast(VT, V1);
1219
1220 if (!V2.isUndef()) {
1221 V2 = DAG.getBitcast(MVT::v4i64, V2);
1222 V2 = DAG.getNode(LoongArchISD::XVPERMI, DL, MVT::v4i64, V2,
1223 DAG.getConstant(0b11101110, DL, MVT::i64));
1224 V2 = DAG.getBitcast(VT, V2);
1225 }
1226
1227 for (auto it = Mask.begin(); it < Mask.begin() + HalfSize; it++) {
1228 *it = *it < 0 ? *it : *it - HalfSize;
1229 }
1230 } else if (preMask == HighLaneTy && postMask == HighLaneTy) {
1231 V1 = DAG.getBitcast(MVT::v4i64, V1);
1232 V1 = DAG.getNode(LoongArchISD::XVPERMI, DL, MVT::v4i64, V1,
1233 DAG.getConstant(0b01000100, DL, MVT::i64));
1234 V1 = DAG.getBitcast(VT, V1);
1235
1236 if (!V2.isUndef()) {
1237 V2 = DAG.getBitcast(MVT::v4i64, V2);
1238 V2 = DAG.getNode(LoongArchISD::XVPERMI, DL, MVT::v4i64, V2,
1239 DAG.getConstant(0b01000100, DL, MVT::i64));
1240 V2 = DAG.getBitcast(VT, V2);
1241 }
1242
1243 for (auto it = Mask.begin() + HalfSize; it < Mask.end(); it++) {
1244 *it = *it < 0 ? *it : *it + HalfSize;
1245 }
1246 } else { // cross-lane
1247 return;
1248 }
1249}
1250
1251/// Dispatching routine to lower various 256-bit LoongArch vector shuffles.
1252///
1253/// This routine breaks down the specific type of 256-bit shuffle and
1254/// dispatches to the lowering routines accordingly.
1256 SDValue V1, SDValue V2, SelectionDAG &DAG) {
1257 assert((VT.SimpleTy == MVT::v32i8 || VT.SimpleTy == MVT::v16i16 ||
1258 VT.SimpleTy == MVT::v8i32 || VT.SimpleTy == MVT::v4i64 ||
1259 VT.SimpleTy == MVT::v8f32 || VT.SimpleTy == MVT::v4f64) &&
1260 "Vector type is unsupported for lasx!");
1261 assert(V1.getSimpleValueType() == V2.getSimpleValueType() &&
1262 "Two operands have different types!");
1263 assert(VT.getVectorNumElements() == Mask.size() &&
1264 "Unexpected mask size for shuffle!");
1265 assert(Mask.size() % 2 == 0 && "Expected even mask size.");
1266 assert(Mask.size() >= 4 && "Mask size is less than 4.");
1267
1268 // canonicalize non cross-lane shuffle vector
1269 SmallVector<int> NewMask(Mask);
1270 canonicalizeShuffleVectorByLane(DL, NewMask, VT, V1, V2, DAG);
1271
1272 SDValue Result;
1273 // TODO: Add more comparison patterns.
1274 if (V2.isUndef()) {
1275 if ((Result = lowerVECTOR_SHUFFLE_XVREPLVEI(DL, NewMask, VT, V1, V2, DAG)))
1276 return Result;
1277 if ((Result = lowerVECTOR_SHUFFLE_XVSHUF4I(DL, NewMask, VT, V1, V2, DAG)))
1278 return Result;
1279
1280 // TODO: This comment may be enabled in the future to better match the
1281 // pattern for instruction selection.
1282 /* V2 = V1; */
1283 }
1284
1285 // It is recommended not to change the pattern comparison order for better
1286 // performance.
1287 if ((Result = lowerVECTOR_SHUFFLE_XVPACKEV(DL, NewMask, VT, V1, V2, DAG)))
1288 return Result;
1289 if ((Result = lowerVECTOR_SHUFFLE_XVPACKOD(DL, NewMask, VT, V1, V2, DAG)))
1290 return Result;
1291 if ((Result = lowerVECTOR_SHUFFLE_XVILVH(DL, NewMask, VT, V1, V2, DAG)))
1292 return Result;
1293 if ((Result = lowerVECTOR_SHUFFLE_XVILVL(DL, NewMask, VT, V1, V2, DAG)))
1294 return Result;
1295 if ((Result = lowerVECTOR_SHUFFLE_XVPICKEV(DL, NewMask, VT, V1, V2, DAG)))
1296 return Result;
1297 if ((Result = lowerVECTOR_SHUFFLE_XVPICKOD(DL, NewMask, VT, V1, V2, DAG)))
1298 return Result;
1299 if ((Result = lowerVECTOR_SHUFFLE_XVSHUF(DL, NewMask, VT, V1, V2, DAG)))
1300 return Result;
1301
1302 return SDValue();
1303}
1304
1305SDValue LoongArchTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
1306 SelectionDAG &DAG) const {
1307 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
1308 ArrayRef<int> OrigMask = SVOp->getMask();
1309 SDValue V1 = Op.getOperand(0);
1310 SDValue V2 = Op.getOperand(1);
1311 MVT VT = Op.getSimpleValueType();
1312 int NumElements = VT.getVectorNumElements();
1313 SDLoc DL(Op);
1314
1315 bool V1IsUndef = V1.isUndef();
1316 bool V2IsUndef = V2.isUndef();
1317 if (V1IsUndef && V2IsUndef)
1318 return DAG.getUNDEF(VT);
1319
1320 // When we create a shuffle node we put the UNDEF node to second operand,
1321 // but in some cases the first operand may be transformed to UNDEF.
1322 // In this case we should just commute the node.
1323 if (V1IsUndef)
1324 return DAG.getCommutedVectorShuffle(*SVOp);
1325
1326 // Check for non-undef masks pointing at an undef vector and make the masks
1327 // undef as well. This makes it easier to match the shuffle based solely on
1328 // the mask.
1329 if (V2IsUndef &&
1330 any_of(OrigMask, [NumElements](int M) { return M >= NumElements; })) {
1331 SmallVector<int, 8> NewMask(OrigMask);
1332 for (int &M : NewMask)
1333 if (M >= NumElements)
1334 M = -1;
1335 return DAG.getVectorShuffle(VT, DL, V1, V2, NewMask);
1336 }
1337
1338 // Check for illegal shuffle mask element index values.
1339 int MaskUpperLimit = OrigMask.size() * (V2IsUndef ? 1 : 2);
1340 (void)MaskUpperLimit;
1341 assert(llvm::all_of(OrigMask,
1342 [&](int M) { return -1 <= M && M < MaskUpperLimit; }) &&
1343 "Out of bounds shuffle index");
1344
1345 // For each vector width, delegate to a specialized lowering routine.
1346 if (VT.is128BitVector())
1347 return lower128BitShuffle(DL, OrigMask, VT, V1, V2, DAG);
1348
1349 if (VT.is256BitVector())
1350 return lower256BitShuffle(DL, OrigMask, VT, V1, V2, DAG);
1351
1352 return SDValue();
1353}
1354
1355static bool isConstantOrUndef(const SDValue Op) {
1356 if (Op->isUndef())
1357 return true;
1358 if (isa<ConstantSDNode>(Op))
1359 return true;
1360 if (isa<ConstantFPSDNode>(Op))
1361 return true;
1362 return false;
1363}
1364
1366 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
1367 if (isConstantOrUndef(Op->getOperand(i)))
1368 return true;
1369 return false;
1370}
1371
1372SDValue LoongArchTargetLowering::lowerBUILD_VECTOR(SDValue Op,
1373 SelectionDAG &DAG) const {
1374 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
1375 EVT ResTy = Op->getValueType(0);
1376 SDLoc DL(Op);
1377 APInt SplatValue, SplatUndef;
1378 unsigned SplatBitSize;
1379 bool HasAnyUndefs;
1380 bool Is128Vec = ResTy.is128BitVector();
1381 bool Is256Vec = ResTy.is256BitVector();
1382
1383 if ((!Subtarget.hasExtLSX() || !Is128Vec) &&
1384 (!Subtarget.hasExtLASX() || !Is256Vec))
1385 return SDValue();
1386
1387 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
1388 /*MinSplatBits=*/8) &&
1389 SplatBitSize <= 64) {
1390 // We can only cope with 8, 16, 32, or 64-bit elements.
1391 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
1392 SplatBitSize != 64)
1393 return SDValue();
1394
1395 EVT ViaVecTy;
1396
1397 switch (SplatBitSize) {
1398 default:
1399 return SDValue();
1400 case 8:
1401 ViaVecTy = Is128Vec ? MVT::v16i8 : MVT::v32i8;
1402 break;
1403 case 16:
1404 ViaVecTy = Is128Vec ? MVT::v8i16 : MVT::v16i16;
1405 break;
1406 case 32:
1407 ViaVecTy = Is128Vec ? MVT::v4i32 : MVT::v8i32;
1408 break;
1409 case 64:
1410 ViaVecTy = Is128Vec ? MVT::v2i64 : MVT::v4i64;
1411 break;
1412 }
1413
1414 // SelectionDAG::getConstant will promote SplatValue appropriately.
1415 SDValue Result = DAG.getConstant(SplatValue, DL, ViaVecTy);
1416
1417 // Bitcast to the type we originally wanted.
1418 if (ViaVecTy != ResTy)
1419 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
1420
1421 return Result;
1422 }
1423
1424 if (DAG.isSplatValue(Op, /*AllowUndefs=*/false))
1425 return Op;
1426
1428 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
1429 // The resulting code is the same length as the expansion, but it doesn't
1430 // use memory operations.
1431 EVT ResTy = Node->getValueType(0);
1432
1433 assert(ResTy.isVector());
1434
1435 unsigned NumElts = ResTy.getVectorNumElements();
1436 SDValue Vector = DAG.getUNDEF(ResTy);
1437 for (unsigned i = 0; i < NumElts; ++i) {
1439 Node->getOperand(i),
1440 DAG.getConstant(i, DL, Subtarget.getGRLenVT()));
1441 }
1442 return Vector;
1443 }
1444
1445 return SDValue();
1446}
1447
1448SDValue
1449LoongArchTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
1450 SelectionDAG &DAG) const {
1451 EVT VecTy = Op->getOperand(0)->getValueType(0);
1452 SDValue Idx = Op->getOperand(1);
1453 EVT EltTy = VecTy.getVectorElementType();
1454 unsigned NumElts = VecTy.getVectorNumElements();
1455
1456 if (isa<ConstantSDNode>(Idx) &&
1457 (EltTy == MVT::i32 || EltTy == MVT::i64 || EltTy == MVT::f32 ||
1458 EltTy == MVT::f64 || Idx->getAsZExtVal() < NumElts / 2))
1459 return Op;
1460
1461 return SDValue();
1462}
1463
1464SDValue
1465LoongArchTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
1466 SelectionDAG &DAG) const {
1467 if (isa<ConstantSDNode>(Op->getOperand(2)))
1468 return Op;
1469 return SDValue();
1470}
1471
1472SDValue LoongArchTargetLowering::lowerATOMIC_FENCE(SDValue Op,
1473 SelectionDAG &DAG) const {
1474 SDLoc DL(Op);
1475 SyncScope::ID FenceSSID =
1476 static_cast<SyncScope::ID>(Op.getConstantOperandVal(2));
1477
1478 // singlethread fences only synchronize with signal handlers on the same
1479 // thread and thus only need to preserve instruction order, not actually
1480 // enforce memory ordering.
1481 if (FenceSSID == SyncScope::SingleThread)
1482 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
1483 return DAG.getNode(ISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
1484
1485 return Op;
1486}
1487
1488SDValue LoongArchTargetLowering::lowerWRITE_REGISTER(SDValue Op,
1489 SelectionDAG &DAG) const {
1490
1491 if (Subtarget.is64Bit() && Op.getOperand(2).getValueType() == MVT::i32) {
1492 DAG.getContext()->emitError(
1493 "On LA64, only 64-bit registers can be written.");
1494 return Op.getOperand(0);
1495 }
1496
1497 if (!Subtarget.is64Bit() && Op.getOperand(2).getValueType() == MVT::i64) {
1498 DAG.getContext()->emitError(
1499 "On LA32, only 32-bit registers can be written.");
1500 return Op.getOperand(0);
1501 }
1502
1503 return Op;
1504}
1505
1506SDValue LoongArchTargetLowering::lowerFRAMEADDR(SDValue Op,
1507 SelectionDAG &DAG) const {
1508 if (!isa<ConstantSDNode>(Op.getOperand(0))) {
1509 DAG.getContext()->emitError("argument to '__builtin_frame_address' must "
1510 "be a constant integer");
1511 return SDValue();
1512 }
1513
1516 Register FrameReg = Subtarget.getRegisterInfo()->getFrameRegister(MF);
1517 EVT VT = Op.getValueType();
1518 SDLoc DL(Op);
1519 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT);
1520 unsigned Depth = Op.getConstantOperandVal(0);
1521 int GRLenInBytes = Subtarget.getGRLen() / 8;
1522
1523 while (Depth--) {
1524 int Offset = -(GRLenInBytes * 2);
1525 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
1527 FrameAddr =
1528 DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
1529 }
1530 return FrameAddr;
1531}
1532
1533SDValue LoongArchTargetLowering::lowerRETURNADDR(SDValue Op,
1534 SelectionDAG &DAG) const {
1536 return SDValue();
1537
1538 // Currently only support lowering return address for current frame.
1539 if (Op.getConstantOperandVal(0) != 0) {
1540 DAG.getContext()->emitError(
1541 "return address can only be determined for the current frame");
1542 return SDValue();
1543 }
1544
1547 MVT GRLenVT = Subtarget.getGRLenVT();
1548
1549 // Return the value of the return address register, marking it an implicit
1550 // live-in.
1551 Register Reg = MF.addLiveIn(Subtarget.getRegisterInfo()->getRARegister(),
1552 getRegClassFor(GRLenVT));
1553 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, GRLenVT);
1554}
1555
1556SDValue LoongArchTargetLowering::lowerEH_DWARF_CFA(SDValue Op,
1557 SelectionDAG &DAG) const {
1559 auto Size = Subtarget.getGRLen() / 8;
1560 auto FI = MF.getFrameInfo().CreateFixedObject(Size, 0, false);
1561 return DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
1562}
1563
1564SDValue LoongArchTargetLowering::lowerVASTART(SDValue Op,
1565 SelectionDAG &DAG) const {
1567 auto *FuncInfo = MF.getInfo<LoongArchMachineFunctionInfo>();
1568
1569 SDLoc DL(Op);
1570 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1572
1573 // vastart just stores the address of the VarArgsFrameIndex slot into the
1574 // memory location argument.
1575 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1576 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
1577 MachinePointerInfo(SV));
1578}
1579
1580SDValue LoongArchTargetLowering::lowerUINT_TO_FP(SDValue Op,
1581 SelectionDAG &DAG) const {
1582 assert(Subtarget.is64Bit() && Subtarget.hasBasicF() &&
1583 !Subtarget.hasBasicD() && "unexpected target features");
1584
1585 SDLoc DL(Op);
1586 SDValue Op0 = Op.getOperand(0);
1587 if (Op0->getOpcode() == ISD::AND) {
1588 auto *C = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
1589 if (C && C->getZExtValue() < UINT64_C(0xFFFFFFFF))
1590 return Op;
1591 }
1592
1593 if (Op0->getOpcode() == LoongArchISD::BSTRPICK &&
1594 Op0.getConstantOperandVal(1) < UINT64_C(0X1F) &&
1595 Op0.getConstantOperandVal(2) == UINT64_C(0))
1596 return Op;
1597
1598 if (Op0.getOpcode() == ISD::AssertZext &&
1599 dyn_cast<VTSDNode>(Op0.getOperand(1))->getVT().bitsLT(MVT::i32))
1600 return Op;
1601
1602 EVT OpVT = Op0.getValueType();
1603 EVT RetVT = Op.getValueType();
1604 RTLIB::Libcall LC = RTLIB::getUINTTOFP(OpVT, RetVT);
1605 MakeLibCallOptions CallOptions;
1606 CallOptions.setTypeListBeforeSoften(OpVT, RetVT, true);
1607 SDValue Chain = SDValue();
1609 std::tie(Result, Chain) =
1610 makeLibCall(DAG, LC, Op.getValueType(), Op0, CallOptions, DL, Chain);
1611 return Result;
1612}
1613
1614SDValue LoongArchTargetLowering::lowerSINT_TO_FP(SDValue Op,
1615 SelectionDAG &DAG) const {
1616 assert(Subtarget.is64Bit() && Subtarget.hasBasicF() &&
1617 !Subtarget.hasBasicD() && "unexpected target features");
1618
1619 SDLoc DL(Op);
1620 SDValue Op0 = Op.getOperand(0);
1621
1622 if ((Op0.getOpcode() == ISD::AssertSext ||
1624 dyn_cast<VTSDNode>(Op0.getOperand(1))->getVT().bitsLE(MVT::i32))
1625 return Op;
1626
1627 EVT OpVT = Op0.getValueType();
1628 EVT RetVT = Op.getValueType();
1629 RTLIB::Libcall LC = RTLIB::getSINTTOFP(OpVT, RetVT);
1630 MakeLibCallOptions CallOptions;
1631 CallOptions.setTypeListBeforeSoften(OpVT, RetVT, true);
1632 SDValue Chain = SDValue();
1634 std::tie(Result, Chain) =
1635 makeLibCall(DAG, LC, Op.getValueType(), Op0, CallOptions, DL, Chain);
1636 return Result;
1637}
1638
1639SDValue LoongArchTargetLowering::lowerBITCAST(SDValue Op,
1640 SelectionDAG &DAG) const {
1641
1642 SDLoc DL(Op);
1643 SDValue Op0 = Op.getOperand(0);
1644
1645 if (Op.getValueType() == MVT::f32 && Op0.getValueType() == MVT::i32 &&
1646 Subtarget.is64Bit() && Subtarget.hasBasicF()) {
1647 SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
1648 return DAG.getNode(LoongArchISD::MOVGR2FR_W_LA64, DL, MVT::f32, NewOp0);
1649 }
1650 return Op;
1651}
1652
1653SDValue LoongArchTargetLowering::lowerFP_TO_SINT(SDValue Op,
1654 SelectionDAG &DAG) const {
1655
1656 SDLoc DL(Op);
1657
1658 if (Op.getValueSizeInBits() > 32 && Subtarget.hasBasicF() &&
1659 !Subtarget.hasBasicD()) {
1660 SDValue Dst =
1661 DAG.getNode(LoongArchISD::FTINT, DL, MVT::f32, Op.getOperand(0));
1662 return DAG.getNode(LoongArchISD::MOVFR2GR_S_LA64, DL, MVT::i64, Dst);
1663 }
1664
1665 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
1666 SDValue Trunc = DAG.getNode(LoongArchISD::FTINT, DL, FPTy, Op.getOperand(0));
1667 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Trunc);
1668}
1669
1671 SelectionDAG &DAG, unsigned Flags) {
1672 return DAG.getTargetGlobalAddress(N->getGlobal(), DL, Ty, 0, Flags);
1673}
1674
1676 SelectionDAG &DAG, unsigned Flags) {
1677 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, N->getOffset(),
1678 Flags);
1679}
1680
1682 SelectionDAG &DAG, unsigned Flags) {
1683 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlign(),
1684 N->getOffset(), Flags);
1685}
1686
1688 SelectionDAG &DAG, unsigned Flags) {
1689 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flags);
1690}
1691
1692template <class NodeTy>
1693SDValue LoongArchTargetLowering::getAddr(NodeTy *N, SelectionDAG &DAG,
1695 bool IsLocal) const {
1696 SDLoc DL(N);
1697 EVT Ty = getPointerTy(DAG.getDataLayout());
1698 SDValue Addr = getTargetNode(N, DL, Ty, DAG, 0);
1699 SDValue Load;
1700
1701 switch (M) {
1702 default:
1703 report_fatal_error("Unsupported code model");
1704
1705 case CodeModel::Large: {
1706 assert(Subtarget.is64Bit() && "Large code model requires LA64");
1707
1708 // This is not actually used, but is necessary for successfully matching
1709 // the PseudoLA_*_LARGE nodes.
1710 SDValue Tmp = DAG.getConstant(0, DL, Ty);
1711 if (IsLocal) {
1712 // This generates the pattern (PseudoLA_PCREL_LARGE tmp sym), that
1713 // eventually becomes the desired 5-insn code sequence.
1714 Load = SDValue(DAG.getMachineNode(LoongArch::PseudoLA_PCREL_LARGE, DL, Ty,
1715 Tmp, Addr),
1716 0);
1717 } else {
1718 // This generates the pattern (PseudoLA_GOT_LARGE tmp sym), that
1719 // eventually becomes the desired 5-insn code sequence.
1720 Load = SDValue(
1721 DAG.getMachineNode(LoongArch::PseudoLA_GOT_LARGE, DL, Ty, Tmp, Addr),
1722 0);
1723 }
1724 break;
1725 }
1726
1727 case CodeModel::Small:
1728 case CodeModel::Medium:
1729 if (IsLocal) {
1730 // This generates the pattern (PseudoLA_PCREL sym), which expands to
1731 // (addi.w/d (pcalau12i %pc_hi20(sym)) %pc_lo12(sym)).
1732 Load = SDValue(
1733 DAG.getMachineNode(LoongArch::PseudoLA_PCREL, DL, Ty, Addr), 0);
1734 } else {
1735 // This generates the pattern (PseudoLA_GOT sym), which expands to (ld.w/d
1736 // (pcalau12i %got_pc_hi20(sym)) %got_pc_lo12(sym)).
1737 Load =
1738 SDValue(DAG.getMachineNode(LoongArch::PseudoLA_GOT, DL, Ty, Addr), 0);
1739 }
1740 }
1741
1742 if (!IsLocal) {
1743 // Mark the load instruction as invariant to enable hoisting in MachineLICM.
1749 LLT(Ty.getSimpleVT()), Align(Ty.getFixedSizeInBits() / 8));
1750 DAG.setNodeMemRefs(cast<MachineSDNode>(Load.getNode()), {MemOp});
1751 }
1752
1753 return Load;
1754}
1755
1756SDValue LoongArchTargetLowering::lowerBlockAddress(SDValue Op,
1757 SelectionDAG &DAG) const {
1758 return getAddr(cast<BlockAddressSDNode>(Op), DAG,
1759 DAG.getTarget().getCodeModel());
1760}
1761
1762SDValue LoongArchTargetLowering::lowerJumpTable(SDValue Op,
1763 SelectionDAG &DAG) const {
1764 return getAddr(cast<JumpTableSDNode>(Op), DAG,
1765 DAG.getTarget().getCodeModel());
1766}
1767
1768SDValue LoongArchTargetLowering::lowerConstantPool(SDValue Op,
1769 SelectionDAG &DAG) const {
1770 return getAddr(cast<ConstantPoolSDNode>(Op), DAG,
1771 DAG.getTarget().getCodeModel());
1772}
1773
1774SDValue LoongArchTargetLowering::lowerGlobalAddress(SDValue Op,
1775 SelectionDAG &DAG) const {
1776 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1777 assert(N->getOffset() == 0 && "unexpected offset in global node");
1778 auto CM = DAG.getTarget().getCodeModel();
1779 const GlobalValue *GV = N->getGlobal();
1780
1781 if (GV->isDSOLocal() && isa<GlobalVariable>(GV)) {
1782 if (auto GCM = dyn_cast<GlobalVariable>(GV)->getCodeModel())
1783 CM = *GCM;
1784 }
1785
1786 return getAddr(N, DAG, CM, GV->isDSOLocal());
1787}
1788
1789SDValue LoongArchTargetLowering::getStaticTLSAddr(GlobalAddressSDNode *N,
1790 SelectionDAG &DAG,
1791 unsigned Opc, bool UseGOT,
1792 bool Large) const {
1793 SDLoc DL(N);
1794 EVT Ty = getPointerTy(DAG.getDataLayout());
1795 MVT GRLenVT = Subtarget.getGRLenVT();
1796
1797 // This is not actually used, but is necessary for successfully matching the
1798 // PseudoLA_*_LARGE nodes.
1799 SDValue Tmp = DAG.getConstant(0, DL, Ty);
1800 SDValue Addr = DAG.getTargetGlobalAddress(N->getGlobal(), DL, Ty, 0, 0);
1802 ? SDValue(DAG.getMachineNode(Opc, DL, Ty, Tmp, Addr), 0)
1803 : SDValue(DAG.getMachineNode(Opc, DL, Ty, Addr), 0);
1804 if (UseGOT) {
1805 // Mark the load instruction as invariant to enable hoisting in MachineLICM.
1811 LLT(Ty.getSimpleVT()), Align(Ty.getFixedSizeInBits() / 8));
1812 DAG.setNodeMemRefs(cast<MachineSDNode>(Offset.getNode()), {MemOp});
1813 }
1814
1815 // Add the thread pointer.
1816 return DAG.getNode(ISD::ADD, DL, Ty, Offset,
1817 DAG.getRegister(LoongArch::R2, GRLenVT));
1818}
1819
1820SDValue LoongArchTargetLowering::getDynamicTLSAddr(GlobalAddressSDNode *N,
1821 SelectionDAG &DAG,
1822 unsigned Opc,
1823 bool Large) const {
1824 SDLoc DL(N);
1825 EVT Ty = getPointerTy(DAG.getDataLayout());
1826 IntegerType *CallTy = Type::getIntNTy(*DAG.getContext(), Ty.getSizeInBits());
1827
1828 // This is not actually used, but is necessary for successfully matching the
1829 // PseudoLA_*_LARGE nodes.
1830 SDValue Tmp = DAG.getConstant(0, DL, Ty);
1831
1832 // Use a PC-relative addressing mode to access the dynamic GOT address.
1833 SDValue Addr = DAG.getTargetGlobalAddress(N->getGlobal(), DL, Ty, 0, 0);
1834 SDValue Load = Large ? SDValue(DAG.getMachineNode(Opc, DL, Ty, Tmp, Addr), 0)
1835 : SDValue(DAG.getMachineNode(Opc, DL, Ty, Addr), 0);
1836
1837 // Prepare argument list to generate call.
1839 ArgListEntry Entry;
1840 Entry.Node = Load;
1841 Entry.Ty = CallTy;
1842 Args.push_back(Entry);
1843
1844 // Setup call to __tls_get_addr.
1846 CLI.setDebugLoc(DL)
1847 .setChain(DAG.getEntryNode())
1848 .setLibCallee(CallingConv::C, CallTy,
1849 DAG.getExternalSymbol("__tls_get_addr", Ty),
1850 std::move(Args));
1851
1852 return LowerCallTo(CLI).first;
1853}
1854
1855SDValue LoongArchTargetLowering::getTLSDescAddr(GlobalAddressSDNode *N,
1856 SelectionDAG &DAG, unsigned Opc,
1857 bool Large) const {
1858 SDLoc DL(N);
1859 EVT Ty = getPointerTy(DAG.getDataLayout());
1860 const GlobalValue *GV = N->getGlobal();
1861
1862 // This is not actually used, but is necessary for successfully matching the
1863 // PseudoLA_*_LARGE nodes.
1864 SDValue Tmp = DAG.getConstant(0, DL, Ty);
1865
1866 // Use a PC-relative addressing mode to access the global dynamic GOT address.
1867 // This generates the pattern (PseudoLA_TLS_DESC_PC{,LARGE} sym).
1868 SDValue Addr = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, 0);
1869 return Large ? SDValue(DAG.getMachineNode(Opc, DL, Ty, Tmp, Addr), 0)
1870 : SDValue(DAG.getMachineNode(Opc, DL, Ty, Addr), 0);
1871}
1872
1873SDValue
1874LoongArchTargetLowering::lowerGlobalTLSAddress(SDValue Op,
1875 SelectionDAG &DAG) const {
1878 report_fatal_error("In GHC calling convention TLS is not supported");
1879
1880 bool Large = DAG.getTarget().getCodeModel() == CodeModel::Large;
1881 assert((!Large || Subtarget.is64Bit()) && "Large code model requires LA64");
1882
1883 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1884 assert(N->getOffset() == 0 && "unexpected offset in global node");
1885
1886 if (DAG.getTarget().useEmulatedTLS())
1887 report_fatal_error("the emulated TLS is prohibited",
1888 /*GenCrashDiag=*/false);
1889
1890 bool IsDesc = DAG.getTarget().useTLSDESC();
1891
1892 switch (getTargetMachine().getTLSModel(N->getGlobal())) {
1894 // In this model, application code calls the dynamic linker function
1895 // __tls_get_addr to locate TLS offsets into the dynamic thread vector at
1896 // runtime.
1897 if (!IsDesc)
1898 return getDynamicTLSAddr(N, DAG,
1899 Large ? LoongArch::PseudoLA_TLS_GD_LARGE
1900 : LoongArch::PseudoLA_TLS_GD,
1901 Large);
1902 break;
1904 // Same as GeneralDynamic, except for assembly modifiers and relocation
1905 // records.
1906 if (!IsDesc)
1907 return getDynamicTLSAddr(N, DAG,
1908 Large ? LoongArch::PseudoLA_TLS_LD_LARGE
1909 : LoongArch::PseudoLA_TLS_LD,
1910 Large);
1911 break;
1913 // This model uses the GOT to resolve TLS offsets.
1914 return getStaticTLSAddr(N, DAG,
1915 Large ? LoongArch::PseudoLA_TLS_IE_LARGE
1916 : LoongArch::PseudoLA_TLS_IE,
1917 /*UseGOT=*/true, Large);
1919 // This model is used when static linking as the TLS offsets are resolved
1920 // during program linking.
1921 //
1922 // This node doesn't need an extra argument for the large code model.
1923 return getStaticTLSAddr(N, DAG, LoongArch::PseudoLA_TLS_LE,
1924 /*UseGOT=*/false);
1925 }
1926
1927 return getTLSDescAddr(N, DAG,
1928 Large ? LoongArch::PseudoLA_TLS_DESC_PC_LARGE
1929 : LoongArch::PseudoLA_TLS_DESC_PC,
1930 Large);
1931}
1932
1933template <unsigned N>
1935 SelectionDAG &DAG, bool IsSigned = false) {
1936 auto *CImm = cast<ConstantSDNode>(Op->getOperand(ImmOp));
1937 // Check the ImmArg.
1938 if ((IsSigned && !isInt<N>(CImm->getSExtValue())) ||
1939 (!IsSigned && !isUInt<N>(CImm->getZExtValue()))) {
1940 DAG.getContext()->emitError(Op->getOperationName(0) +
1941 ": argument out of range.");
1942 return DAG.getNode(ISD::UNDEF, SDLoc(Op), Op.getValueType());
1943 }
1944 return SDValue();
1945}
1946
1947SDValue
1948LoongArchTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1949 SelectionDAG &DAG) const {
1950 SDLoc DL(Op);
1951 switch (Op.getConstantOperandVal(0)) {
1952 default:
1953 return SDValue(); // Don't custom lower most intrinsics.
1954 case Intrinsic::thread_pointer: {
1955 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1956 return DAG.getRegister(LoongArch::R2, PtrVT);
1957 }
1958 case Intrinsic::loongarch_lsx_vpickve2gr_d:
1959 case Intrinsic::loongarch_lsx_vpickve2gr_du:
1960 case Intrinsic::loongarch_lsx_vreplvei_d:
1961 case Intrinsic::loongarch_lasx_xvrepl128vei_d:
1962 return checkIntrinsicImmArg<1>(Op, 2, DAG);
1963 case Intrinsic::loongarch_lsx_vreplvei_w:
1964 case Intrinsic::loongarch_lasx_xvrepl128vei_w:
1965 case Intrinsic::loongarch_lasx_xvpickve2gr_d:
1966 case Intrinsic::loongarch_lasx_xvpickve2gr_du:
1967 case Intrinsic::loongarch_lasx_xvpickve_d:
1968 case Intrinsic::loongarch_lasx_xvpickve_d_f:
1969 return checkIntrinsicImmArg<2>(Op, 2, DAG);
1970 case Intrinsic::loongarch_lasx_xvinsve0_d:
1971 return checkIntrinsicImmArg<2>(Op, 3, DAG);
1972 case Intrinsic::loongarch_lsx_vsat_b:
1973 case Intrinsic::loongarch_lsx_vsat_bu:
1974 case Intrinsic::loongarch_lsx_vrotri_b:
1975 case Intrinsic::loongarch_lsx_vsllwil_h_b:
1976 case Intrinsic::loongarch_lsx_vsllwil_hu_bu:
1977 case Intrinsic::loongarch_lsx_vsrlri_b:
1978 case Intrinsic::loongarch_lsx_vsrari_b:
1979 case Intrinsic::loongarch_lsx_vreplvei_h:
1980 case Intrinsic::loongarch_lasx_xvsat_b:
1981 case Intrinsic::loongarch_lasx_xvsat_bu:
1982 case Intrinsic::loongarch_lasx_xvrotri_b:
1983 case Intrinsic::loongarch_lasx_xvsllwil_h_b:
1984 case Intrinsic::loongarch_lasx_xvsllwil_hu_bu:
1985 case Intrinsic::loongarch_lasx_xvsrlri_b:
1986 case Intrinsic::loongarch_lasx_xvsrari_b:
1987 case Intrinsic::loongarch_lasx_xvrepl128vei_h:
1988 case Intrinsic::loongarch_lasx_xvpickve_w:
1989 case Intrinsic::loongarch_lasx_xvpickve_w_f:
1990 return checkIntrinsicImmArg<3>(Op, 2, DAG);
1991 case Intrinsic::loongarch_lasx_xvinsve0_w:
1992 return checkIntrinsicImmArg<3>(Op, 3, DAG);
1993 case Intrinsic::loongarch_lsx_vsat_h:
1994 case Intrinsic::loongarch_lsx_vsat_hu:
1995 case Intrinsic::loongarch_lsx_vrotri_h:
1996 case Intrinsic::loongarch_lsx_vsllwil_w_h:
1997 case Intrinsic::loongarch_lsx_vsllwil_wu_hu:
1998 case Intrinsic::loongarch_lsx_vsrlri_h:
1999 case Intrinsic::loongarch_lsx_vsrari_h:
2000 case Intrinsic::loongarch_lsx_vreplvei_b:
2001 case Intrinsic::loongarch_lasx_xvsat_h:
2002 case Intrinsic::loongarch_lasx_xvsat_hu:
2003 case Intrinsic::loongarch_lasx_xvrotri_h:
2004 case Intrinsic::loongarch_lasx_xvsllwil_w_h:
2005 case Intrinsic::loongarch_lasx_xvsllwil_wu_hu:
2006 case Intrinsic::loongarch_lasx_xvsrlri_h:
2007 case Intrinsic::loongarch_lasx_xvsrari_h:
2008 case Intrinsic::loongarch_lasx_xvrepl128vei_b:
2009 return checkIntrinsicImmArg<4>(Op, 2, DAG);
2010 case Intrinsic::loongarch_lsx_vsrlni_b_h:
2011 case Intrinsic::loongarch_lsx_vsrani_b_h:
2012 case Intrinsic::loongarch_lsx_vsrlrni_b_h:
2013 case Intrinsic::loongarch_lsx_vsrarni_b_h:
2014 case Intrinsic::loongarch_lsx_vssrlni_b_h:
2015 case Intrinsic::loongarch_lsx_vssrani_b_h:
2016 case Intrinsic::loongarch_lsx_vssrlni_bu_h:
2017 case Intrinsic::loongarch_lsx_vssrani_bu_h:
2018 case Intrinsic::loongarch_lsx_vssrlrni_b_h:
2019 case Intrinsic::loongarch_lsx_vssrarni_b_h:
2020 case Intrinsic::loongarch_lsx_vssrlrni_bu_h:
2021 case Intrinsic::loongarch_lsx_vssrarni_bu_h:
2022 case Intrinsic::loongarch_lasx_xvsrlni_b_h:
2023 case Intrinsic::loongarch_lasx_xvsrani_b_h:
2024 case Intrinsic::loongarch_lasx_xvsrlrni_b_h:
2025 case Intrinsic::loongarch_lasx_xvsrarni_b_h:
2026 case Intrinsic::loongarch_lasx_xvssrlni_b_h:
2027 case Intrinsic::loongarch_lasx_xvssrani_b_h:
2028 case Intrinsic::loongarch_lasx_xvssrlni_bu_h:
2029 case Intrinsic::loongarch_lasx_xvssrani_bu_h:
2030 case Intrinsic::loongarch_lasx_xvssrlrni_b_h:
2031 case Intrinsic::loongarch_lasx_xvssrarni_b_h:
2032 case Intrinsic::loongarch_lasx_xvssrlrni_bu_h:
2033 case Intrinsic::loongarch_lasx_xvssrarni_bu_h:
2034 return checkIntrinsicImmArg<4>(Op, 3, DAG);
2035 case Intrinsic::loongarch_lsx_vsat_w:
2036 case Intrinsic::loongarch_lsx_vsat_wu:
2037 case Intrinsic::loongarch_lsx_vrotri_w:
2038 case Intrinsic::loongarch_lsx_vsllwil_d_w:
2039 case Intrinsic::loongarch_lsx_vsllwil_du_wu:
2040 case Intrinsic::loongarch_lsx_vsrlri_w:
2041 case Intrinsic::loongarch_lsx_vsrari_w:
2042 case Intrinsic::loongarch_lsx_vslei_bu:
2043 case Intrinsic::loongarch_lsx_vslei_hu:
2044 case Intrinsic::loongarch_lsx_vslei_wu:
2045 case Intrinsic::loongarch_lsx_vslei_du:
2046 case Intrinsic::loongarch_lsx_vslti_bu:
2047 case Intrinsic::loongarch_lsx_vslti_hu:
2048 case Intrinsic::loongarch_lsx_vslti_wu:
2049 case Intrinsic::loongarch_lsx_vslti_du:
2050 case Intrinsic::loongarch_lsx_vbsll_v:
2051 case Intrinsic::loongarch_lsx_vbsrl_v:
2052 case Intrinsic::loongarch_lasx_xvsat_w:
2053 case Intrinsic::loongarch_lasx_xvsat_wu:
2054 case Intrinsic::loongarch_lasx_xvrotri_w:
2055 case Intrinsic::loongarch_lasx_xvsllwil_d_w:
2056 case Intrinsic::loongarch_lasx_xvsllwil_du_wu:
2057 case Intrinsic::loongarch_lasx_xvsrlri_w:
2058 case Intrinsic::loongarch_lasx_xvsrari_w:
2059 case Intrinsic::loongarch_lasx_xvslei_bu:
2060 case Intrinsic::loongarch_lasx_xvslei_hu:
2061 case Intrinsic::loongarch_lasx_xvslei_wu:
2062 case Intrinsic::loongarch_lasx_xvslei_du:
2063 case Intrinsic::loongarch_lasx_xvslti_bu:
2064 case Intrinsic::loongarch_lasx_xvslti_hu:
2065 case Intrinsic::loongarch_lasx_xvslti_wu:
2066 case Intrinsic::loongarch_lasx_xvslti_du:
2067 case Intrinsic::loongarch_lasx_xvbsll_v:
2068 case Intrinsic::loongarch_lasx_xvbsrl_v:
2069 return checkIntrinsicImmArg<5>(Op, 2, DAG);
2070 case Intrinsic::loongarch_lsx_vseqi_b:
2071 case Intrinsic::loongarch_lsx_vseqi_h:
2072 case Intrinsic::loongarch_lsx_vseqi_w:
2073 case Intrinsic::loongarch_lsx_vseqi_d:
2074 case Intrinsic::loongarch_lsx_vslei_b:
2075 case Intrinsic::loongarch_lsx_vslei_h:
2076 case Intrinsic::loongarch_lsx_vslei_w:
2077 case Intrinsic::loongarch_lsx_vslei_d:
2078 case Intrinsic::loongarch_lsx_vslti_b:
2079 case Intrinsic::loongarch_lsx_vslti_h:
2080 case Intrinsic::loongarch_lsx_vslti_w:
2081 case Intrinsic::loongarch_lsx_vslti_d:
2082 case Intrinsic::loongarch_lasx_xvseqi_b:
2083 case Intrinsic::loongarch_lasx_xvseqi_h:
2084 case Intrinsic::loongarch_lasx_xvseqi_w:
2085 case Intrinsic::loongarch_lasx_xvseqi_d:
2086 case Intrinsic::loongarch_lasx_xvslei_b:
2087 case Intrinsic::loongarch_lasx_xvslei_h:
2088 case Intrinsic::loongarch_lasx_xvslei_w:
2089 case Intrinsic::loongarch_lasx_xvslei_d:
2090 case Intrinsic::loongarch_lasx_xvslti_b:
2091 case Intrinsic::loongarch_lasx_xvslti_h:
2092 case Intrinsic::loongarch_lasx_xvslti_w:
2093 case Intrinsic::loongarch_lasx_xvslti_d:
2094 return checkIntrinsicImmArg<5>(Op, 2, DAG, /*IsSigned=*/true);
2095 case Intrinsic::loongarch_lsx_vsrlni_h_w:
2096 case Intrinsic::loongarch_lsx_vsrani_h_w:
2097 case Intrinsic::loongarch_lsx_vsrlrni_h_w:
2098 case Intrinsic::loongarch_lsx_vsrarni_h_w:
2099 case Intrinsic::loongarch_lsx_vssrlni_h_w:
2100 case Intrinsic::loongarch_lsx_vssrani_h_w:
2101 case Intrinsic::loongarch_lsx_vssrlni_hu_w:
2102 case Intrinsic::loongarch_lsx_vssrani_hu_w:
2103 case Intrinsic::loongarch_lsx_vssrlrni_h_w:
2104 case Intrinsic::loongarch_lsx_vssrarni_h_w:
2105 case Intrinsic::loongarch_lsx_vssrlrni_hu_w:
2106 case Intrinsic::loongarch_lsx_vssrarni_hu_w:
2107 case Intrinsic::loongarch_lsx_vfrstpi_b:
2108 case Intrinsic::loongarch_lsx_vfrstpi_h:
2109 case Intrinsic::loongarch_lasx_xvsrlni_h_w:
2110 case Intrinsic::loongarch_lasx_xvsrani_h_w:
2111 case Intrinsic::loongarch_lasx_xvsrlrni_h_w:
2112 case Intrinsic::loongarch_lasx_xvsrarni_h_w:
2113 case Intrinsic::loongarch_lasx_xvssrlni_h_w:
2114 case Intrinsic::loongarch_lasx_xvssrani_h_w:
2115 case Intrinsic::loongarch_lasx_xvssrlni_hu_w:
2116 case Intrinsic::loongarch_lasx_xvssrani_hu_w:
2117 case Intrinsic::loongarch_lasx_xvssrlrni_h_w:
2118 case Intrinsic::loongarch_lasx_xvssrarni_h_w:
2119 case Intrinsic::loongarch_lasx_xvssrlrni_hu_w:
2120 case Intrinsic::loongarch_lasx_xvssrarni_hu_w:
2121 case Intrinsic::loongarch_lasx_xvfrstpi_b:
2122 case Intrinsic::loongarch_lasx_xvfrstpi_h:
2123 return checkIntrinsicImmArg<5>(Op, 3, DAG);
2124 case Intrinsic::loongarch_lsx_vsat_d:
2125 case Intrinsic::loongarch_lsx_vsat_du:
2126 case Intrinsic::loongarch_lsx_vrotri_d:
2127 case Intrinsic::loongarch_lsx_vsrlri_d:
2128 case Intrinsic::loongarch_lsx_vsrari_d:
2129 case Intrinsic::loongarch_lasx_xvsat_d:
2130 case Intrinsic::loongarch_lasx_xvsat_du:
2131 case Intrinsic::loongarch_lasx_xvrotri_d:
2132 case Intrinsic::loongarch_lasx_xvsrlri_d:
2133 case Intrinsic::loongarch_lasx_xvsrari_d:
2134 return checkIntrinsicImmArg<6>(Op, 2, DAG);
2135 case Intrinsic::loongarch_lsx_vsrlni_w_d:
2136 case Intrinsic::loongarch_lsx_vsrani_w_d:
2137 case Intrinsic::loongarch_lsx_vsrlrni_w_d:
2138 case Intrinsic::loongarch_lsx_vsrarni_w_d:
2139 case Intrinsic::loongarch_lsx_vssrlni_w_d:
2140 case Intrinsic::loongarch_lsx_vssrani_w_d:
2141 case Intrinsic::loongarch_lsx_vssrlni_wu_d:
2142 case Intrinsic::loongarch_lsx_vssrani_wu_d:
2143 case Intrinsic::loongarch_lsx_vssrlrni_w_d:
2144 case Intrinsic::loongarch_lsx_vssrarni_w_d:
2145 case Intrinsic::loongarch_lsx_vssrlrni_wu_d:
2146 case Intrinsic::loongarch_lsx_vssrarni_wu_d:
2147 case Intrinsic::loongarch_lasx_xvsrlni_w_d:
2148 case Intrinsic::loongarch_lasx_xvsrani_w_d:
2149 case Intrinsic::loongarch_lasx_xvsrlrni_w_d:
2150 case Intrinsic::loongarch_lasx_xvsrarni_w_d:
2151 case Intrinsic::loongarch_lasx_xvssrlni_w_d:
2152 case Intrinsic::loongarch_lasx_xvssrani_w_d:
2153 case Intrinsic::loongarch_lasx_xvssrlni_wu_d:
2154 case Intrinsic::loongarch_lasx_xvssrani_wu_d:
2155 case Intrinsic::loongarch_lasx_xvssrlrni_w_d:
2156 case Intrinsic::loongarch_lasx_xvssrarni_w_d:
2157 case Intrinsic::loongarch_lasx_xvssrlrni_wu_d:
2158 case Intrinsic::loongarch_lasx_xvssrarni_wu_d:
2159 return checkIntrinsicImmArg<6>(Op, 3, DAG);
2160 case Intrinsic::loongarch_lsx_vsrlni_d_q:
2161 case Intrinsic::loongarch_lsx_vsrani_d_q:
2162 case Intrinsic::loongarch_lsx_vsrlrni_d_q:
2163 case Intrinsic::loongarch_lsx_vsrarni_d_q:
2164 case Intrinsic::loongarch_lsx_vssrlni_d_q:
2165 case Intrinsic::loongarch_lsx_vssrani_d_q:
2166 case Intrinsic::loongarch_lsx_vssrlni_du_q:
2167 case Intrinsic::loongarch_lsx_vssrani_du_q:
2168 case Intrinsic::loongarch_lsx_vssrlrni_d_q:
2169 case Intrinsic::loongarch_lsx_vssrarni_d_q:
2170 case Intrinsic::loongarch_lsx_vssrlrni_du_q:
2171 case Intrinsic::loongarch_lsx_vssrarni_du_q:
2172 case Intrinsic::loongarch_lasx_xvsrlni_d_q:
2173 case Intrinsic::loongarch_lasx_xvsrani_d_q:
2174 case Intrinsic::loongarch_lasx_xvsrlrni_d_q:
2175 case Intrinsic::loongarch_lasx_xvsrarni_d_q:
2176 case Intrinsic::loongarch_lasx_xvssrlni_d_q:
2177 case Intrinsic::loongarch_lasx_xvssrani_d_q:
2178 case Intrinsic::loongarch_lasx_xvssrlni_du_q:
2179 case Intrinsic::loongarch_lasx_xvssrani_du_q:
2180 case Intrinsic::loongarch_lasx_xvssrlrni_d_q:
2181 case Intrinsic::loongarch_lasx_xvssrarni_d_q:
2182 case Intrinsic::loongarch_lasx_xvssrlrni_du_q:
2183 case Intrinsic::loongarch_lasx_xvssrarni_du_q:
2184 return checkIntrinsicImmArg<7>(Op, 3, DAG);
2185 case Intrinsic::loongarch_lsx_vnori_b:
2186 case Intrinsic::loongarch_lsx_vshuf4i_b:
2187 case Intrinsic::loongarch_lsx_vshuf4i_h:
2188 case Intrinsic::loongarch_lsx_vshuf4i_w:
2189 case Intrinsic::loongarch_lasx_xvnori_b:
2190 case Intrinsic::loongarch_lasx_xvshuf4i_b:
2191 case Intrinsic::loongarch_lasx_xvshuf4i_h:
2192 case Intrinsic::loongarch_lasx_xvshuf4i_w:
2193 case Intrinsic::loongarch_lasx_xvpermi_d:
2194 return checkIntrinsicImmArg<8>(Op, 2, DAG);
2195 case Intrinsic::loongarch_lsx_vshuf4i_d:
2196 case Intrinsic::loongarch_lsx_vpermi_w:
2197 case Intrinsic::loongarch_lsx_vbitseli_b:
2198 case Intrinsic::loongarch_lsx_vextrins_b:
2199 case Intrinsic::loongarch_lsx_vextrins_h:
2200 case Intrinsic::loongarch_lsx_vextrins_w:
2201 case Intrinsic::loongarch_lsx_vextrins_d:
2202 case Intrinsic::loongarch_lasx_xvshuf4i_d:
2203 case Intrinsic::loongarch_lasx_xvpermi_w:
2204 case Intrinsic::loongarch_lasx_xvpermi_q:
2205 case Intrinsic::loongarch_lasx_xvbitseli_b:
2206 case Intrinsic::loongarch_lasx_xvextrins_b:
2207 case Intrinsic::loongarch_lasx_xvextrins_h:
2208 case Intrinsic::loongarch_lasx_xvextrins_w:
2209 case Intrinsic::loongarch_lasx_xvextrins_d:
2210 return checkIntrinsicImmArg<8>(Op, 3, DAG);
2211 case Intrinsic::loongarch_lsx_vrepli_b:
2212 case Intrinsic::loongarch_lsx_vrepli_h:
2213 case Intrinsic::loongarch_lsx_vrepli_w:
2214 case Intrinsic::loongarch_lsx_vrepli_d:
2215 case Intrinsic::loongarch_lasx_xvrepli_b:
2216 case Intrinsic::loongarch_lasx_xvrepli_h:
2217 case Intrinsic::loongarch_lasx_xvrepli_w:
2218 case Intrinsic::loongarch_lasx_xvrepli_d:
2219 return checkIntrinsicImmArg<10>(Op, 1, DAG, /*IsSigned=*/true);
2220 case Intrinsic::loongarch_lsx_vldi:
2221 case Intrinsic::loongarch_lasx_xvldi:
2222 return checkIntrinsicImmArg<13>(Op, 1, DAG, /*IsSigned=*/true);
2223 }
2224}
2225
2226// Helper function that emits error message for intrinsics with chain and return
2227// merge values of a UNDEF and the chain.
2229 StringRef ErrorMsg,
2230 SelectionDAG &DAG) {
2231 DAG.getContext()->emitError(Op->getOperationName(0) + ": " + ErrorMsg + ".");
2232 return DAG.getMergeValues({DAG.getUNDEF(Op.getValueType()), Op.getOperand(0)},
2233 SDLoc(Op));
2234}
2235
2236SDValue
2237LoongArchTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2238 SelectionDAG &DAG) const {
2239 SDLoc DL(Op);
2240 MVT GRLenVT = Subtarget.getGRLenVT();
2241 EVT VT = Op.getValueType();
2242 SDValue Chain = Op.getOperand(0);
2243 const StringRef ErrorMsgOOR = "argument out of range";
2244 const StringRef ErrorMsgReqLA64 = "requires loongarch64";
2245 const StringRef ErrorMsgReqF = "requires basic 'f' target feature";
2246
2247 switch (Op.getConstantOperandVal(1)) {
2248 default:
2249 return Op;
2250 case Intrinsic::loongarch_crc_w_b_w:
2251 case Intrinsic::loongarch_crc_w_h_w:
2252 case Intrinsic::loongarch_crc_w_w_w:
2253 case Intrinsic::loongarch_crc_w_d_w:
2254 case Intrinsic::loongarch_crcc_w_b_w:
2255 case Intrinsic::loongarch_crcc_w_h_w:
2256 case Intrinsic::loongarch_crcc_w_w_w:
2257 case Intrinsic::loongarch_crcc_w_d_w:
2258 return emitIntrinsicWithChainErrorMessage(Op, ErrorMsgReqLA64, DAG);
2259 case Intrinsic::loongarch_csrrd_w:
2260 case Intrinsic::loongarch_csrrd_d: {
2261 unsigned Imm = Op.getConstantOperandVal(2);
2262 return !isUInt<14>(Imm)
2263 ? emitIntrinsicWithChainErrorMessage(Op, ErrorMsgOOR, DAG)
2264 : DAG.getNode(LoongArchISD::CSRRD, DL, {GRLenVT, MVT::Other},
2265 {Chain, DAG.getConstant(Imm, DL, GRLenVT)});
2266 }
2267 case Intrinsic::loongarch_csrwr_w:
2268 case Intrinsic::loongarch_csrwr_d: {
2269 unsigned Imm = Op.getConstantOperandVal(3);
2270 return !isUInt<14>(Imm)
2271 ? emitIntrinsicWithChainErrorMessage(Op, ErrorMsgOOR, DAG)
2272 : DAG.getNode(LoongArchISD::CSRWR, DL, {GRLenVT, MVT::Other},
2273 {Chain, Op.getOperand(2),
2274 DAG.getConstant(Imm, DL, GRLenVT)});
2275 }
2276 case Intrinsic::loongarch_csrxchg_w:
2277 case Intrinsic::loongarch_csrxchg_d: {
2278 unsigned Imm = Op.getConstantOperandVal(4);
2279 return !isUInt<14>(Imm)
2280 ? emitIntrinsicWithChainErrorMessage(Op, ErrorMsgOOR, DAG)
2281 : DAG.getNode(LoongArchISD::CSRXCHG, DL, {GRLenVT, MVT::Other},
2282 {Chain, Op.getOperand(2), Op.getOperand(3),
2283 DAG.getConstant(Imm, DL, GRLenVT)});
2284 }
2285 case Intrinsic::loongarch_iocsrrd_d: {
2286 return DAG.getNode(
2287 LoongArchISD::IOCSRRD_D, DL, {GRLenVT, MVT::Other},
2288 {Chain, DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op.getOperand(2))});
2289 }
2290#define IOCSRRD_CASE(NAME, NODE) \
2291 case Intrinsic::loongarch_##NAME: { \
2292 return DAG.getNode(LoongArchISD::NODE, DL, {GRLenVT, MVT::Other}, \
2293 {Chain, Op.getOperand(2)}); \
2294 }
2295 IOCSRRD_CASE(iocsrrd_b, IOCSRRD_B);
2296 IOCSRRD_CASE(iocsrrd_h, IOCSRRD_H);
2297 IOCSRRD_CASE(iocsrrd_w, IOCSRRD_W);
2298#undef IOCSRRD_CASE
2299 case Intrinsic::loongarch_cpucfg: {
2300 return DAG.getNode(LoongArchISD::CPUCFG, DL, {GRLenVT, MVT::Other},
2301 {Chain, Op.getOperand(2)});
2302 }
2303 case Intrinsic::loongarch_lddir_d: {
2304 unsigned Imm = Op.getConstantOperandVal(3);
2305 return !isUInt<8>(Imm)
2306 ? emitIntrinsicWithChainErrorMessage(Op, ErrorMsgOOR, DAG)
2307 : Op;
2308 }
2309 case Intrinsic::loongarch_movfcsr2gr: {
2310 if (!Subtarget.hasBasicF())
2311 return emitIntrinsicWithChainErrorMessage(Op, ErrorMsgReqF, DAG);
2312 unsigned Imm = Op.getConstantOperandVal(2);
2313 return !isUInt<2>(Imm)
2314 ? emitIntrinsicWithChainErrorMessage(Op, ErrorMsgOOR, DAG)
2315 : DAG.getNode(LoongArchISD::MOVFCSR2GR, DL, {VT, MVT::Other},
2316 {Chain, DAG.getConstant(Imm, DL, GRLenVT)});
2317 }
2318 case Intrinsic::loongarch_lsx_vld:
2319 case Intrinsic::loongarch_lsx_vldrepl_b:
2320 case Intrinsic::loongarch_lasx_xvld:
2321 case Intrinsic::loongarch_lasx_xvldrepl_b:
2322 return !isInt<12>(cast<ConstantSDNode>(Op.getOperand(3))->getSExtValue())
2323 ? emitIntrinsicWithChainErrorMessage(Op, ErrorMsgOOR, DAG)
2324 : SDValue();
2325 case Intrinsic::loongarch_lsx_vldrepl_h:
2326 case Intrinsic::loongarch_lasx_xvldrepl_h:
2327 return !isShiftedInt<11, 1>(
2328 cast<ConstantSDNode>(Op.getOperand(3))->getSExtValue())
2330 Op, "argument out of range or not a multiple of 2", DAG)
2331 : SDValue();
2332 case Intrinsic::loongarch_lsx_vldrepl_w:
2333 case Intrinsic::loongarch_lasx_xvldrepl_w:
2334 return !isShiftedInt<10, 2>(
2335 cast<ConstantSDNode>(Op.getOperand(3))->getSExtValue())
2337 Op, "argument out of range or not a multiple of 4", DAG)
2338 : SDValue();
2339 case Intrinsic::loongarch_lsx_vldrepl_d:
2340 case Intrinsic::loongarch_lasx_xvldrepl_d:
2341 return !isShiftedInt<9, 3>(
2342 cast<ConstantSDNode>(Op.getOperand(3))->getSExtValue())
2344 Op, "argument out of range or not a multiple of 8", DAG)
2345 : SDValue();
2346 }
2347}
2348
2349// Helper function that emits error message for intrinsics with void return
2350// value and return the chain.
2352 SelectionDAG &DAG) {
2353
2354 DAG.getContext()->emitError(Op->getOperationName(0) + ": " + ErrorMsg + ".");
2355 return Op.getOperand(0);
2356}
2357
2358SDValue LoongArchTargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2359 SelectionDAG &DAG) const {
2360 SDLoc DL(Op);
2361 MVT GRLenVT = Subtarget.getGRLenVT();
2362 SDValue Chain = Op.getOperand(0);
2363 uint64_t IntrinsicEnum = Op.getConstantOperandVal(1);
2364 SDValue Op2 = Op.getOperand(2);
2365 const StringRef ErrorMsgOOR = "argument out of range";
2366 const StringRef ErrorMsgReqLA64 = "requires loongarch64";
2367 const StringRef ErrorMsgReqLA32 = "requires loongarch32";
2368 const StringRef ErrorMsgReqF = "requires basic 'f' target feature";
2369
2370 switch (IntrinsicEnum) {
2371 default:
2372 // TODO: Add more Intrinsics.
2373 return SDValue();
2374 case Intrinsic::loongarch_cacop_d:
2375 case Intrinsic::loongarch_cacop_w: {
2376 if (IntrinsicEnum == Intrinsic::loongarch_cacop_d && !Subtarget.is64Bit())
2377 return emitIntrinsicErrorMessage(Op, ErrorMsgReqLA64, DAG);
2378 if (IntrinsicEnum == Intrinsic::loongarch_cacop_w && Subtarget.is64Bit())
2379 return emitIntrinsicErrorMessage(Op, ErrorMsgReqLA32, DAG);
2380 // call void @llvm.loongarch.cacop.[d/w](uimm5, rj, simm12)
2381 unsigned Imm1 = Op2->getAsZExtVal();
2382 int Imm2 = cast<ConstantSDNode>(Op.getOperand(4))->getSExtValue();
2383 if (!isUInt<5>(Imm1) || !isInt<12>(Imm2))
2384 return emitIntrinsicErrorMessage(Op, ErrorMsgOOR, DAG);
2385 return Op;
2386 }
2387 case Intrinsic::loongarch_dbar: {
2388 unsigned Imm = Op2->getAsZExtVal();
2389 return !isUInt<15>(Imm)
2390 ? emitIntrinsicErrorMessage(Op, ErrorMsgOOR, DAG)
2391 : DAG.getNode(LoongArchISD::DBAR, DL, MVT::Other, Chain,
2392 DAG.getConstant(Imm, DL, GRLenVT));
2393 }
2394 case Intrinsic::loongarch_ibar: {
2395 unsigned Imm = Op2->getAsZExtVal();
2396 return !isUInt<15>(Imm)
2397 ? emitIntrinsicErrorMessage(Op, ErrorMsgOOR, DAG)
2398 : DAG.getNode(LoongArchISD::IBAR, DL, MVT::Other, Chain,
2399 DAG.getConstant(Imm, DL, GRLenVT));
2400 }
2401 case Intrinsic::loongarch_break: {
2402 unsigned Imm = Op2->getAsZExtVal();
2403 return !isUInt<15>(Imm)
2404 ? emitIntrinsicErrorMessage(Op, ErrorMsgOOR, DAG)
2405 : DAG.getNode(LoongArchISD::BREAK, DL, MVT::Other, Chain,
2406 DAG.getConstant(Imm, DL, GRLenVT));
2407 }
2408 case Intrinsic::loongarch_movgr2fcsr: {
2409 if (!Subtarget.hasBasicF())
2410 return emitIntrinsicErrorMessage(Op, ErrorMsgReqF, DAG);
2411 unsigned Imm = Op2->getAsZExtVal();
2412 return !isUInt<2>(Imm)
2413 ? emitIntrinsicErrorMessage(Op, ErrorMsgOOR, DAG)
2414 : DAG.getNode(LoongArchISD::MOVGR2FCSR, DL, MVT::Other, Chain,
2415 DAG.getConstant(Imm, DL, GRLenVT),
2416 DAG.getNode(ISD::ANY_EXTEND, DL, GRLenVT,
2417 Op.getOperand(3)));
2418 }
2419 case Intrinsic::loongarch_syscall: {
2420 unsigned Imm = Op2->getAsZExtVal();
2421 return !isUInt<15>(Imm)
2422 ? emitIntrinsicErrorMessage(Op, ErrorMsgOOR, DAG)
2423 : DAG.getNode(LoongArchISD::SYSCALL, DL, MVT::Other, Chain,
2424 DAG.getConstant(Imm, DL, GRLenVT));
2425 }
2426#define IOCSRWR_CASE(NAME, NODE) \
2427 case Intrinsic::loongarch_##NAME: { \
2428 SDValue Op3 = Op.getOperand(3); \
2429 return Subtarget.is64Bit() \
2430 ? DAG.getNode(LoongArchISD::NODE, DL, MVT::Other, Chain, \
2431 DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op2), \
2432 DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op3)) \
2433 : DAG.getNode(LoongArchISD::NODE, DL, MVT::Other, Chain, Op2, \
2434 Op3); \
2435 }
2436 IOCSRWR_CASE(iocsrwr_b, IOCSRWR_B);
2437 IOCSRWR_CASE(iocsrwr_h, IOCSRWR_H);
2438 IOCSRWR_CASE(iocsrwr_w, IOCSRWR_W);
2439#undef IOCSRWR_CASE
2440 case Intrinsic::loongarch_iocsrwr_d: {
2441 return !Subtarget.is64Bit()
2442 ? emitIntrinsicErrorMessage(Op, ErrorMsgReqLA64, DAG)
2443 : DAG.getNode(LoongArchISD::IOCSRWR_D, DL, MVT::Other, Chain,
2444 Op2,
2445 DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64,
2446 Op.getOperand(3)));
2447 }
2448#define ASRT_LE_GT_CASE(NAME) \
2449 case Intrinsic::loongarch_##NAME: { \
2450 return !Subtarget.is64Bit() \
2451 ? emitIntrinsicErrorMessage(Op, ErrorMsgReqLA64, DAG) \
2452 : Op; \
2453 }
2454 ASRT_LE_GT_CASE(asrtle_d)
2455 ASRT_LE_GT_CASE(asrtgt_d)
2456#undef ASRT_LE_GT_CASE
2457 case Intrinsic::loongarch_ldpte_d: {
2458 unsigned Imm = Op.getConstantOperandVal(3);
2459 return !Subtarget.is64Bit()
2460 ? emitIntrinsicErrorMessage(Op, ErrorMsgReqLA64, DAG)
2461 : !isUInt<8>(Imm) ? emitIntrinsicErrorMessage(Op, ErrorMsgOOR, DAG)
2462 : Op;
2463 }
2464 case Intrinsic::loongarch_lsx_vst:
2465 case Intrinsic::loongarch_lasx_xvst:
2466 return !isInt<12>(cast<ConstantSDNode>(Op.getOperand(4))->getSExtValue())
2467 ? emitIntrinsicErrorMessage(Op, ErrorMsgOOR, DAG)
2468 : SDValue();
2469 case Intrinsic::loongarch_lasx_xvstelm_b:
2470 return (!isInt<8>(cast<ConstantSDNode>(Op.getOperand(4))->getSExtValue()) ||
2471 !isUInt<5>(Op.getConstantOperandVal(5)))
2472 ? emitIntrinsicErrorMessage(Op, ErrorMsgOOR, DAG)
2473 : SDValue();
2474 case Intrinsic::loongarch_lsx_vstelm_b:
2475 return (!isInt<8>(cast<ConstantSDNode>(Op.getOperand(4))->getSExtValue()) ||
2476 !isUInt<4>(Op.getConstantOperandVal(5)))
2477 ? emitIntrinsicErrorMessage(Op, ErrorMsgOOR, DAG)
2478 : SDValue();
2479 case Intrinsic::loongarch_lasx_xvstelm_h:
2480 return (!isShiftedInt<8, 1>(
2481 cast<ConstantSDNode>(Op.getOperand(4))->getSExtValue()) ||
2482 !isUInt<4>(Op.getConstantOperandVal(5)))
2484 Op, "argument out of range or not a multiple of 2", DAG)
2485 : SDValue();
2486 case Intrinsic::loongarch_lsx_vstelm_h:
2487 return (!isShiftedInt<8, 1>(
2488 cast<ConstantSDNode>(Op.getOperand(4))->getSExtValue()) ||
2489 !isUInt<3>(Op.getConstantOperandVal(5)))
2491 Op, "argument out of range or not a multiple of 2", DAG)
2492 : SDValue();
2493 case Intrinsic::loongarch_lasx_xvstelm_w:
2494 return (!isShiftedInt<8, 2>(
2495 cast<ConstantSDNode>(Op.getOperand(4))->getSExtValue()) ||
2496 !isUInt<3>(Op.getConstantOperandVal(5)))
2498 Op, "argument out of range or not a multiple of 4", DAG)
2499 : SDValue();
2500 case Intrinsic::loongarch_lsx_vstelm_w:
2501 return (!isShiftedInt<8, 2>(
2502 cast<ConstantSDNode>(Op.getOperand(4))->getSExtValue()) ||
2503 !isUInt<2>(Op.getConstantOperandVal(5)))
2505 Op, "argument out of range or not a multiple of 4", DAG)
2506 : SDValue();
2507 case Intrinsic::loongarch_lasx_xvstelm_d:
2508 return (!isShiftedInt<8, 3>(
2509 cast<ConstantSDNode>(Op.getOperand(4))->getSExtValue()) ||
2510 !isUInt<2>(Op.getConstantOperandVal(5)))
2512 Op, "argument out of range or not a multiple of 8", DAG)
2513 : SDValue();
2514 case Intrinsic::loongarch_lsx_vstelm_d:
2515 return (!isShiftedInt<8, 3>(
2516 cast<ConstantSDNode>(Op.getOperand(4))->getSExtValue()) ||
2517 !isUInt<1>(Op.getConstantOperandVal(5)))
2519 Op, "argument out of range or not a multiple of 8", DAG)
2520 : SDValue();
2521 }
2522}
2523
2524SDValue LoongArchTargetLowering::lowerShiftLeftParts(SDValue Op,
2525 SelectionDAG &DAG) const {
2526 SDLoc DL(Op);
2527 SDValue Lo = Op.getOperand(0);
2528 SDValue Hi = Op.getOperand(1);
2529 SDValue Shamt = Op.getOperand(2);
2530 EVT VT = Lo.getValueType();
2531
2532 // if Shamt-GRLen < 0: // Shamt < GRLen
2533 // Lo = Lo << Shamt
2534 // Hi = (Hi << Shamt) | ((Lo >>u 1) >>u (GRLen-1 ^ Shamt))
2535 // else:
2536 // Lo = 0
2537 // Hi = Lo << (Shamt-GRLen)
2538
2539 SDValue Zero = DAG.getConstant(0, DL, VT);
2540 SDValue One = DAG.getConstant(1, DL, VT);
2541 SDValue MinusGRLen = DAG.getConstant(-(int)Subtarget.getGRLen(), DL, VT);
2542 SDValue GRLenMinus1 = DAG.getConstant(Subtarget.getGRLen() - 1, DL, VT);
2543 SDValue ShamtMinusGRLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusGRLen);
2544 SDValue GRLenMinus1Shamt = DAG.getNode(ISD::XOR, DL, VT, Shamt, GRLenMinus1);
2545
2546 SDValue LoTrue = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
2547 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo, One);
2548 SDValue ShiftRightLo =
2549 DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, GRLenMinus1Shamt);
2550 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2551 SDValue HiTrue = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2552 SDValue HiFalse = DAG.getNode(ISD::SHL, DL, VT, Lo, ShamtMinusGRLen);
2553
2554 SDValue CC = DAG.getSetCC(DL, VT, ShamtMinusGRLen, Zero, ISD::SETLT);
2555
2556 Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, Zero);
2557 Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
2558
2559 SDValue Parts[2] = {Lo, Hi};
2560 return DAG.getMergeValues(Parts, DL);
2561}
2562
2563SDValue LoongArchTargetLowering::lowerShiftRightParts(SDValue Op,
2564 SelectionDAG &DAG,
2565 bool IsSRA) const {
2566 SDLoc DL(Op);
2567 SDValue Lo = Op.getOperand(0);
2568 SDValue Hi = Op.getOperand(1);
2569 SDValue Shamt = Op.getOperand(2);
2570 EVT VT = Lo.getValueType();
2571
2572 // SRA expansion:
2573 // if Shamt-GRLen < 0: // Shamt < GRLen
2574 // Lo = (Lo >>u Shamt) | ((Hi << 1) << (ShAmt ^ GRLen-1))
2575 // Hi = Hi >>s Shamt
2576 // else:
2577 // Lo = Hi >>s (Shamt-GRLen);
2578 // Hi = Hi >>s (GRLen-1)
2579 //
2580 // SRL expansion:
2581 // if Shamt-GRLen < 0: // Shamt < GRLen
2582 // Lo = (Lo >>u Shamt) | ((Hi << 1) << (ShAmt ^ GRLen-1))
2583 // Hi = Hi >>u Shamt
2584 // else:
2585 // Lo = Hi >>u (Shamt-GRLen);
2586 // Hi = 0;
2587
2588 unsigned ShiftRightOp = IsSRA ? ISD::SRA : ISD::SRL;
2589
2590 SDValue Zero = DAG.getConstant(0, DL, VT);
2591 SDValue One = DAG.getConstant(1, DL, VT);
2592 SDValue MinusGRLen = DAG.getConstant(-(int)Subtarget.getGRLen(), DL, VT);
2593 SDValue GRLenMinus1 = DAG.getConstant(Subtarget.getGRLen() - 1, DL, VT);
2594 SDValue ShamtMinusGRLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusGRLen);
2595 SDValue GRLenMinus1Shamt = DAG.getNode(ISD::XOR, DL, VT, Shamt, GRLenMinus1);
2596
2597 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2598 SDValue ShiftLeftHi1 = DAG.getNode(ISD::SHL, DL, VT, Hi, One);
2599 SDValue ShiftLeftHi =
2600 DAG.getNode(ISD::SHL, DL, VT, ShiftLeftHi1, GRLenMinus1Shamt);
2601 SDValue LoTrue = DAG.getNode(ISD::OR, DL, VT, ShiftRightLo, ShiftLeftHi);
2602 SDValue HiTrue = DAG.getNode(ShiftRightOp, DL, VT, Hi, Shamt);
2603 SDValue LoFalse = DAG.getNode(ShiftRightOp, DL, VT, Hi, ShamtMinusGRLen);
2604 SDValue HiFalse =
2605 IsSRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, GRLenMinus1) : Zero;
2606
2607 SDValue CC = DAG.getSetCC(DL, VT, ShamtMinusGRLen, Zero, ISD::SETLT);
2608
2609 Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, LoFalse);
2610 Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
2611
2612 SDValue Parts[2] = {Lo, Hi};
2613 return DAG.getMergeValues(Parts, DL);
2614}
2615
2616// Returns the opcode of the target-specific SDNode that implements the 32-bit
2617// form of the given Opcode.
2619 switch (Opcode) {
2620 default:
2621 llvm_unreachable("Unexpected opcode");
2622 case ISD::UDIV:
2623 return LoongArchISD::DIV_WU;
2624 case ISD::UREM:
2625 return LoongArchISD::MOD_WU;
2626 case ISD::SHL:
2627 return LoongArchISD::SLL_W;
2628 case ISD::SRA:
2629 return LoongArchISD::SRA_W;
2630 case ISD::SRL:
2631 return LoongArchISD::SRL_W;
2632 case ISD::ROTL:
2633 case ISD::ROTR:
2634 return LoongArchISD::ROTR_W;
2635 case ISD::CTTZ:
2636 return LoongArchISD::CTZ_W;
2637 case ISD::CTLZ:
2638 return LoongArchISD::CLZ_W;
2639 }
2640}
2641
2642// Converts the given i8/i16/i32 operation to a target-specific SelectionDAG
2643// node. Because i8/i16/i32 isn't a legal type for LA64, these operations would
2644// otherwise be promoted to i64, making it difficult to select the
2645// SLL_W/.../*W later one because the fact the operation was originally of
2646// type i8/i16/i32 is lost.
2648 unsigned ExtOpc = ISD::ANY_EXTEND) {
2649 SDLoc DL(N);
2650 LoongArchISD::NodeType WOpcode = getLoongArchWOpcode(N->getOpcode());
2651 SDValue NewOp0, NewRes;
2652
2653 switch (NumOp) {
2654 default:
2655 llvm_unreachable("Unexpected NumOp");
2656 case 1: {
2657 NewOp0 = DAG.getNode(ExtOpc, DL, MVT::i64, N->getOperand(0));
2658 NewRes = DAG.getNode(WOpcode, DL, MVT::i64, NewOp0);
2659 break;
2660 }
2661 case 2: {
2662 NewOp0 = DAG.getNode(ExtOpc, DL, MVT::i64, N->getOperand(0));
2663 SDValue NewOp1 = DAG.getNode(ExtOpc, DL, MVT::i64, N->getOperand(1));
2664 if (N->getOpcode() == ISD::ROTL) {
2665 SDValue TmpOp = DAG.getConstant(32, DL, MVT::i64);
2666 NewOp1 = DAG.getNode(ISD::SUB, DL, MVT::i64, TmpOp, NewOp1);
2667 }
2668 NewRes = DAG.getNode(WOpcode, DL, MVT::i64, NewOp0, NewOp1);
2669 break;
2670 }
2671 // TODO:Handle more NumOp.
2672 }
2673
2674 // ReplaceNodeResults requires we maintain the same type for the return
2675 // value.
2676 return DAG.getNode(ISD::TRUNCATE, DL, N->getValueType(0), NewRes);
2677}
2678
2679// Converts the given 32-bit operation to a i64 operation with signed extension
2680// semantic to reduce the signed extension instructions.
2682 SDLoc DL(N);
2683 SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
2684 SDValue NewOp1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
2685 SDValue NewWOp = DAG.getNode(N->getOpcode(), DL, MVT::i64, NewOp0, NewOp1);
2686 SDValue NewRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i64, NewWOp,
2687 DAG.getValueType(MVT::i32));
2688 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes);
2689}
2690
2691// Helper function that emits error message for intrinsics with/without chain
2692// and return a UNDEF or and the chain as the results.
2695 StringRef ErrorMsg, bool WithChain = true) {
2696 DAG.getContext()->emitError(N->getOperationName(0) + ": " + ErrorMsg + ".");
2697 Results.push_back(DAG.getUNDEF(N->getValueType(0)));
2698 if (!WithChain)
2699 return;
2700 Results.push_back(N->getOperand(0));
2701}
2702
2703template <unsigned N>
2704static void
2706 SelectionDAG &DAG, const LoongArchSubtarget &Subtarget,
2707 unsigned ResOp) {
2708 const StringRef ErrorMsgOOR = "argument out of range";
2709 unsigned Imm = Node->getConstantOperandVal(2);
2710 if (!isUInt<N>(Imm)) {
2712 /*WithChain=*/false);
2713 return;
2714 }
2715 SDLoc DL(Node);
2716 SDValue Vec = Node->getOperand(1);
2717
2718 SDValue PickElt =
2719 DAG.getNode(ResOp, DL, Subtarget.getGRLenVT(), Vec,
2720 DAG.getConstant(Imm, DL, Subtarget.getGRLenVT()),
2722 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, Node->getValueType(0),
2723 PickElt.getValue(0)));
2724}
2725
2728 SelectionDAG &DAG,
2729 const LoongArchSubtarget &Subtarget,
2730 unsigned ResOp) {
2731 SDLoc DL(N);
2732 SDValue Vec = N->getOperand(1);
2733
2734 SDValue CB = DAG.getNode(ResOp, DL, Subtarget.getGRLenVT(), Vec);
2735 Results.push_back(
2736 DAG.getNode(ISD::TRUNCATE, DL, N->getValueType(0), CB.getValue(0)));
2737}
2738
2739static void
2741 SelectionDAG &DAG,
2742 const LoongArchSubtarget &Subtarget) {
2743 switch (N->getConstantOperandVal(0)) {
2744 default:
2745 llvm_unreachable("Unexpected Intrinsic.");
2746 case Intrinsic::loongarch_lsx_vpickve2gr_b:
2747 replaceVPICKVE2GRResults<4>(N, Results, DAG, Subtarget,
2749 break;
2750 case Intrinsic::loongarch_lsx_vpickve2gr_h:
2751 case Intrinsic::loongarch_lasx_xvpickve2gr_w:
2752 replaceVPICKVE2GRResults<3>(N, Results, DAG, Subtarget,
2754 break;
2755 case Intrinsic::loongarch_lsx_vpickve2gr_w:
2756 replaceVPICKVE2GRResults<2>(N, Results, DAG, Subtarget,
2758 break;
2759 case Intrinsic::loongarch_lsx_vpickve2gr_bu:
2760 replaceVPICKVE2GRResults<4>(N, Results, DAG, Subtarget,
2762 break;
2763 case Intrinsic::loongarch_lsx_vpickve2gr_hu:
2764 case Intrinsic::loongarch_lasx_xvpickve2gr_wu:
2765 replaceVPICKVE2GRResults<3>(N, Results, DAG, Subtarget,
2767 break;
2768 case Intrinsic::loongarch_lsx_vpickve2gr_wu:
2769 replaceVPICKVE2GRResults<2>(N, Results, DAG, Subtarget,
2771 break;
2772 case Intrinsic::loongarch_lsx_bz_b:
2773 case Intrinsic::loongarch_lsx_bz_h:
2774 case Intrinsic::loongarch_lsx_bz_w:
2775 case Intrinsic::loongarch_lsx_bz_d:
2776 case Intrinsic::loongarch_lasx_xbz_b:
2777 case Intrinsic::loongarch_lasx_xbz_h:
2778 case Intrinsic::loongarch_lasx_xbz_w:
2779 case Intrinsic::loongarch_lasx_xbz_d:
2780 replaceVecCondBranchResults(N, Results, DAG, Subtarget,
2782 break;
2783 case Intrinsic::loongarch_lsx_bz_v:
2784 case Intrinsic::loongarch_lasx_xbz_v:
2785 replaceVecCondBranchResults(N, Results, DAG, Subtarget,
2787 break;
2788 case Intrinsic::loongarch_lsx_bnz_b:
2789 case Intrinsic::loongarch_lsx_bnz_h:
2790 case Intrinsic::loongarch_lsx_bnz_w:
2791 case Intrinsic::loongarch_lsx_bnz_d:
2792 case Intrinsic::loongarch_lasx_xbnz_b:
2793 case Intrinsic::loongarch_lasx_xbnz_h:
2794 case Intrinsic::loongarch_lasx_xbnz_w:
2795 case Intrinsic::loongarch_lasx_xbnz_d:
2796 replaceVecCondBranchResults(N, Results, DAG, Subtarget,
2798 break;
2799 case Intrinsic::loongarch_lsx_bnz_v:
2800 case Intrinsic::loongarch_lasx_xbnz_v:
2801 replaceVecCondBranchResults(N, Results, DAG, Subtarget,
2803 break;
2804 }
2805}
2806
2809 SDLoc DL(N);
2810 EVT VT = N->getValueType(0);
2811 switch (N->getOpcode()) {
2812 default:
2813 llvm_unreachable("Don't know how to legalize this operation");
2814 case ISD::ADD:
2815 case ISD::SUB:
2816 assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
2817 "Unexpected custom legalisation");
2818 Results.push_back(customLegalizeToWOpWithSExt(N, DAG));
2819 break;
2820 case ISD::UDIV:
2821 case ISD::UREM:
2822 assert(VT == MVT::i32 && Subtarget.is64Bit() &&
2823 "Unexpected custom legalisation");
2824 Results.push_back(customLegalizeToWOp(N, DAG, 2, ISD::SIGN_EXTEND));
2825 break;
2826 case ISD::SHL:
2827 case ISD::SRA:
2828 case ISD::SRL:
2829 assert(VT == MVT::i32 && Subtarget.is64Bit() &&
2830 "Unexpected custom legalisation");
2831 if (N->getOperand(1).getOpcode() != ISD::Constant) {
2832 Results.push_back(customLegalizeToWOp(N, DAG, 2));
2833 break;
2834 }
2835 break;
2836 case ISD::ROTL:
2837 case ISD::ROTR:
2838 assert(VT == MVT::i32 && Subtarget.is64Bit() &&
2839 "Unexpected custom legalisation");
2840 Results.push_back(customLegalizeToWOp(N, DAG, 2));
2841 break;
2842 case ISD::FP_TO_SINT: {
2843 assert(VT == MVT::i32 && Subtarget.is64Bit() &&
2844 "Unexpected custom legalisation");
2845 SDValue Src = N->getOperand(0);
2846 EVT FVT = EVT::getFloatingPointVT(N->getValueSizeInBits(0));
2847 if (getTypeAction(*DAG.getContext(), Src.getValueType()) !=
2849 SDValue Dst = DAG.getNode(LoongArchISD::FTINT, DL, FVT, Src);
2850 Results.push_back(DAG.getNode(ISD::BITCAST, DL, VT, Dst));
2851 return;
2852 }
2853 // If the FP type needs to be softened, emit a library call using the 'si'
2854 // version. If we left it to default legalization we'd end up with 'di'.
2855 RTLIB::Libcall LC;
2856 LC = RTLIB::getFPTOSINT(Src.getValueType(), VT);
2857 MakeLibCallOptions CallOptions;
2858 EVT OpVT = Src.getValueType();
2859 CallOptions.setTypeListBeforeSoften(OpVT, VT, true);
2860 SDValue Chain = SDValue();
2861 SDValue Result;
2862 std::tie(Result, Chain) =
2863 makeLibCall(DAG, LC, VT, Src, CallOptions, DL, Chain);
2864 Results.push_back(Result);
2865 break;
2866 }
2867 case ISD::BITCAST: {
2868 SDValue Src = N->getOperand(0);
2869 EVT SrcVT = Src.getValueType();
2870 if (VT == MVT::i32 && SrcVT == MVT::f32 && Subtarget.is64Bit() &&
2871 Subtarget.hasBasicF()) {
2872 SDValue Dst =
2873 DAG.getNode(LoongArchISD::MOVFR2GR_S_LA64, DL, MVT::i64, Src);
2874 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Dst));
2875 }
2876 break;
2877 }
2878 case ISD::FP_TO_UINT: {
2879 assert(VT == MVT::i32 && Subtarget.is64Bit() &&
2880 "Unexpected custom legalisation");
2881 auto &TLI = DAG.getTargetLoweringInfo();
2882 SDValue Tmp1, Tmp2;
2883 TLI.expandFP_TO_UINT(N, Tmp1, Tmp2, DAG);
2884 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Tmp1));
2885 break;
2886 }
2887 case ISD::BSWAP: {
2888 SDValue Src = N->getOperand(0);
2889 assert((VT == MVT::i16 || VT == MVT::i32) &&
2890 "Unexpected custom legalization");
2891 MVT GRLenVT = Subtarget.getGRLenVT();
2892 SDValue NewSrc = DAG.getNode(ISD::ANY_EXTEND, DL, GRLenVT, Src);
2893 SDValue Tmp;
2894 switch (VT.getSizeInBits()) {
2895 default:
2896 llvm_unreachable("Unexpected operand width");
2897 case 16:
2898 Tmp = DAG.getNode(LoongArchISD::REVB_2H, DL, GRLenVT, NewSrc);
2899 break;
2900 case 32:
2901 // Only LA64 will get to here due to the size mismatch between VT and
2902 // GRLenVT, LA32 lowering is directly defined in LoongArchInstrInfo.
2903 Tmp = DAG.getNode(LoongArchISD::REVB_2W, DL, GRLenVT, NewSrc);
2904 break;
2905 }
2906 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, VT, Tmp));
2907 break;
2908 }
2909 case ISD::BITREVERSE: {
2910 SDValue Src = N->getOperand(0);
2911 assert((VT == MVT::i8 || (VT == MVT::i32 && Subtarget.is64Bit())) &&
2912 "Unexpected custom legalization");
2913 MVT GRLenVT = Subtarget.getGRLenVT();
2914 SDValue NewSrc = DAG.getNode(ISD::ANY_EXTEND, DL, GRLenVT, Src);
2915 SDValue Tmp;
2916 switch (VT.getSizeInBits()) {
2917 default:
2918 llvm_unreachable("Unexpected operand width");
2919 case 8:
2920 Tmp = DAG.getNode(LoongArchISD::BITREV_4B, DL, GRLenVT, NewSrc);
2921 break;
2922 case 32:
2923 Tmp = DAG.getNode(LoongArchISD::BITREV_W, DL, GRLenVT, NewSrc);
2924 break;
2925 }
2926 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, VT, Tmp));
2927 break;
2928 }
2929 case ISD::CTLZ:
2930 case ISD::CTTZ: {
2931 assert(VT == MVT::i32 && Subtarget.is64Bit() &&
2932 "Unexpected custom legalisation");
2933 Results.push_back(customLegalizeToWOp(N, DAG, 1));
2934 break;
2935 }
2937 SDValue Chain = N->getOperand(0);
2938 SDValue Op2 = N->getOperand(2);
2939 MVT GRLenVT = Subtarget.getGRLenVT();
2940 const StringRef ErrorMsgOOR = "argument out of range";
2941 const StringRef ErrorMsgReqLA64 = "requires loongarch64";
2942 const StringRef ErrorMsgReqF = "requires basic 'f' target feature";
2943
2944 switch (N->getConstantOperandVal(1)) {
2945 default:
2946 llvm_unreachable("Unexpected Intrinsic.");
2947 case Intrinsic::loongarch_movfcsr2gr: {
2948 if (!Subtarget.hasBasicF()) {
2949 emitErrorAndReplaceIntrinsicResults(N, Results, DAG, ErrorMsgReqF);
2950 return;
2951 }
2952 unsigned Imm = Op2->getAsZExtVal();
2953 if (!isUInt<2>(Imm)) {
2954 emitErrorAndReplaceIntrinsicResults(N, Results, DAG, ErrorMsgOOR);
2955 return;
2956 }
2957 SDValue MOVFCSR2GRResults = DAG.getNode(
2958 LoongArchISD::MOVFCSR2GR, SDLoc(N), {MVT::i64, MVT::Other},
2959 {Chain, DAG.getConstant(Imm, DL, GRLenVT)});
2960 Results.push_back(
2961 DAG.getNode(ISD::TRUNCATE, DL, VT, MOVFCSR2GRResults.getValue(0)));
2962 Results.push_back(MOVFCSR2GRResults.getValue(1));
2963 break;
2964 }
2965#define CRC_CASE_EXT_BINARYOP(NAME, NODE) \
2966 case Intrinsic::loongarch_##NAME: { \
2967 SDValue NODE = DAG.getNode( \
2968 LoongArchISD::NODE, DL, {MVT::i64, MVT::Other}, \
2969 {Chain, DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op2), \
2970 DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(3))}); \
2971 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, VT, NODE.getValue(0))); \
2972 Results.push_back(NODE.getValue(1)); \
2973 break; \
2974 }
2975 CRC_CASE_EXT_BINARYOP(crc_w_b_w, CRC_W_B_W)
2976 CRC_CASE_EXT_BINARYOP(crc_w_h_w, CRC_W_H_W)
2977 CRC_CASE_EXT_BINARYOP(crc_w_w_w, CRC_W_W_W)
2978 CRC_CASE_EXT_BINARYOP(crcc_w_b_w, CRCC_W_B_W)
2979 CRC_CASE_EXT_BINARYOP(crcc_w_h_w, CRCC_W_H_W)
2980 CRC_CASE_EXT_BINARYOP(crcc_w_w_w, CRCC_W_W_W)
2981#undef CRC_CASE_EXT_BINARYOP
2982
2983#define CRC_CASE_EXT_UNARYOP(NAME, NODE) \
2984 case Intrinsic::loongarch_##NAME: { \
2985 SDValue NODE = DAG.getNode( \
2986 LoongArchISD::NODE, DL, {MVT::i64, MVT::Other}, \
2987 {Chain, Op2, \
2988 DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(3))}); \
2989 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, VT, NODE.getValue(0))); \
2990 Results.push_back(NODE.getValue(1)); \
2991 break; \
2992 }
2993 CRC_CASE_EXT_UNARYOP(crc_w_d_w, CRC_W_D_W)
2994 CRC_CASE_EXT_UNARYOP(crcc_w_d_w, CRCC_W_D_W)
2995#undef CRC_CASE_EXT_UNARYOP
2996#define CSR_CASE(ID) \
2997 case Intrinsic::loongarch_##ID: { \
2998 if (!Subtarget.is64Bit()) \
2999 emitErrorAndReplaceIntrinsicResults(N, Results, DAG, ErrorMsgReqLA64); \
3000 break; \
3001 }
3002 CSR_CASE(csrrd_d);
3003 CSR_CASE(csrwr_d);
3004 CSR_CASE(csrxchg_d);
3005 CSR_CASE(iocsrrd_d);
3006#undef CSR_CASE
3007 case Intrinsic::loongarch_csrrd_w: {
3008 unsigned Imm = Op2->getAsZExtVal();
3009 if (!isUInt<14>(Imm)) {
3010 emitErrorAndReplaceIntrinsicResults(N, Results, DAG, ErrorMsgOOR);
3011 return;
3012 }
3013 SDValue CSRRDResults =
3014 DAG.getNode(LoongArchISD::CSRRD, DL, {GRLenVT, MVT::Other},
3015 {Chain, DAG.getConstant(Imm, DL, GRLenVT)});
3016 Results.push_back(
3017 DAG.getNode(ISD::TRUNCATE, DL, VT, CSRRDResults.getValue(0)));
3018 Results.push_back(CSRRDResults.getValue(1));
3019 break;
3020 }
3021 case Intrinsic::loongarch_csrwr_w: {
3022 unsigned Imm = N->getConstantOperandVal(3);
3023 if (!isUInt<14>(Imm)) {
3024 emitErrorAndReplaceIntrinsicResults(N, Results, DAG, ErrorMsgOOR);
3025 return;
3026 }
3027 SDValue CSRWRResults =
3028 DAG.getNode(LoongArchISD::CSRWR, DL, {GRLenVT, MVT::Other},
3029 {Chain, DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op2),
3030 DAG.getConstant(Imm, DL, GRLenVT)});
3031 Results.push_back(
3032 DAG.getNode(ISD::TRUNCATE, DL, VT, CSRWRResults.getValue(0)));
3033 Results.push_back(CSRWRResults.getValue(1));
3034 break;
3035 }
3036 case Intrinsic::loongarch_csrxchg_w: {
3037 unsigned Imm = N->getConstantOperandVal(4);
3038 if (!isUInt<14>(Imm)) {
3039 emitErrorAndReplaceIntrinsicResults(N, Results, DAG, ErrorMsgOOR);
3040 return;
3041 }
3042 SDValue CSRXCHGResults = DAG.getNode(
3043 LoongArchISD::CSRXCHG, DL, {GRLenVT, MVT::Other},
3044 {Chain, DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op2),
3045 DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(3)),
3046 DAG.getConstant(Imm, DL, GRLenVT)});
3047 Results.push_back(
3048 DAG.getNode(ISD::TRUNCATE, DL, VT, CSRXCHGResults.getValue(0)));
3049 Results.push_back(CSRXCHGResults.getValue(1));
3050 break;
3051 }
3052#define IOCSRRD_CASE(NAME, NODE) \
3053 case Intrinsic::loongarch_##NAME: { \
3054 SDValue IOCSRRDResults = \
3055 DAG.getNode(LoongArchISD::NODE, DL, {MVT::i64, MVT::Other}, \
3056 {Chain, DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op2)}); \
3057 Results.push_back( \
3058 DAG.getNode(ISD::TRUNCATE, DL, VT, IOCSRRDResults.getValue(0))); \
3059 Results.push_back(IOCSRRDResults.getValue(1)); \
3060 break; \
3061 }
3062 IOCSRRD_CASE(iocsrrd_b, IOCSRRD_B);
3063 IOCSRRD_CASE(iocsrrd_h, IOCSRRD_H);
3064 IOCSRRD_CASE(iocsrrd_w, IOCSRRD_W);
3065#undef IOCSRRD_CASE
3066 case Intrinsic::loongarch_cpucfg: {
3067 SDValue CPUCFGResults =
3068 DAG.getNode(LoongArchISD::CPUCFG, DL, {GRLenVT, MVT::Other},
3069 {Chain, DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op2)});
3070 Results.push_back(
3071 DAG.getNode(ISD::TRUNCATE, DL, VT, CPUCFGResults.getValue(0)));
3072 Results.push_back(CPUCFGResults.getValue(1));
3073 break;
3074 }
3075 case Intrinsic::loongarch_lddir_d: {
3076 if (!Subtarget.is64Bit()) {
3077 emitErrorAndReplaceIntrinsicResults(N, Results, DAG, ErrorMsgReqLA64);
3078 return;
3079 }
3080 break;
3081 }
3082 }
3083 break;
3084 }
3085 case ISD::READ_REGISTER: {
3086 if (Subtarget.is64Bit())
3087 DAG.getContext()->emitError(
3088 "On LA64, only 64-bit registers can be read.");
3089 else
3090 DAG.getContext()->emitError(
3091 "On LA32, only 32-bit registers can be read.");
3092 Results.push_back(DAG.getUNDEF(VT));
3093 Results.push_back(N->getOperand(0));
3094 break;
3095 }
3097 replaceINTRINSIC_WO_CHAINResults(N, Results, DAG, Subtarget);
3098 break;
3099 }
3100 }
3101}
3102
3105 const LoongArchSubtarget &Subtarget) {
3106 if (DCI.isBeforeLegalizeOps())
3107 return SDValue();
3108
3109 SDValue FirstOperand = N->getOperand(0);
3110 SDValue SecondOperand = N->getOperand(1);
3111 unsigned FirstOperandOpc = FirstOperand.getOpcode();
3112 EVT ValTy = N->getValueType(0);
3113 SDLoc DL(N);
3114 uint64_t lsb, msb;
3115 unsigned SMIdx, SMLen;
3116 ConstantSDNode *CN;
3117 SDValue NewOperand;
3118 MVT GRLenVT = Subtarget.getGRLenVT();
3119
3120 // Op's second operand must be a shifted mask.
3121 if (!(CN = dyn_cast<ConstantSDNode>(SecondOperand)) ||
3122 !isShiftedMask_64(CN->getZExtValue(), SMIdx, SMLen))
3123 return SDValue();
3124
3125 if (FirstOperandOpc == ISD::SRA || FirstOperandOpc == ISD::SRL) {
3126 // Pattern match BSTRPICK.
3127 // $dst = and ((sra or srl) $src , lsb), (2**len - 1)
3128 // => BSTRPICK $dst, $src, msb, lsb
3129 // where msb = lsb + len - 1
3130
3131 // The second operand of the shift must be an immediate.
3132 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
3133 return SDValue();
3134
3135 lsb = CN->getZExtValue();
3136
3137 // Return if the shifted mask does not start at bit 0 or the sum of its
3138 // length and lsb exceeds the word's size.
3139 if (SMIdx != 0 || lsb + SMLen > ValTy.getSizeInBits())
3140 return SDValue();
3141
3142 NewOperand = FirstOperand.getOperand(0);
3143 } else {
3144 // Pattern match BSTRPICK.
3145 // $dst = and $src, (2**len- 1) , if len > 12
3146 // => BSTRPICK $dst, $src, msb, lsb
3147 // where lsb = 0 and msb = len - 1
3148
3149 // If the mask is <= 0xfff, andi can be used instead.
3150 if (CN->getZExtValue() <= 0xfff)
3151 return SDValue();
3152
3153 // Return if the MSB exceeds.
3154 if (SMIdx + SMLen > ValTy.getSizeInBits())
3155 return SDValue();
3156
3157 if (SMIdx > 0) {
3158 // Omit if the constant has more than 2 uses. This a conservative
3159 // decision. Whether it is a win depends on the HW microarchitecture.
3160 // However it should always be better for 1 and 2 uses.
3161 if (CN->use_size() > 2)
3162 return SDValue();
3163 // Return if the constant can be composed by a single LU12I.W.
3164 if ((CN->getZExtValue() & 0xfff) == 0)
3165 return SDValue();
3166 // Return if the constand can be composed by a single ADDI with
3167 // the zero register.
3168 if (CN->getSExtValue() >= -2048 && CN->getSExtValue() < 0)
3169 return SDValue();
3170 }
3171
3172 lsb = SMIdx;
3173 NewOperand = FirstOperand;
3174 }
3175
3176 msb = lsb + SMLen - 1;
3177 SDValue NR0 = DAG.getNode(LoongArchISD::BSTRPICK, DL, ValTy, NewOperand,
3178 DAG.getConstant(msb, DL, GRLenVT),
3179 DAG.getConstant(lsb, DL, GRLenVT));
3180 if (FirstOperandOpc == ISD::SRA || FirstOperandOpc == ISD::SRL || lsb == 0)
3181 return NR0;
3182 // Try to optimize to
3183 // bstrpick $Rd, $Rs, msb, lsb
3184 // slli $Rd, $Rd, lsb
3185 return DAG.getNode(ISD::SHL, DL, ValTy, NR0,
3186 DAG.getConstant(lsb, DL, GRLenVT));
3187}
3188
3191 const LoongArchSubtarget &Subtarget) {
3192 if (DCI.isBeforeLegalizeOps())
3193 return SDValue();
3194
3195 // $dst = srl (and $src, Mask), Shamt
3196 // =>
3197 // BSTRPICK $dst, $src, MaskIdx+MaskLen-1, Shamt
3198 // when Mask is a shifted mask, and MaskIdx <= Shamt <= MaskIdx+MaskLen-1
3199 //
3200
3201 SDValue FirstOperand = N->getOperand(0);
3202 ConstantSDNode *CN;
3203 EVT ValTy = N->getValueType(0);
3204 SDLoc DL(N);
3205 MVT GRLenVT = Subtarget.getGRLenVT();
3206 unsigned MaskIdx, MaskLen;
3207 uint64_t Shamt;
3208
3209 // The first operand must be an AND and the second operand of the AND must be
3210 // a shifted mask.
3211 if (FirstOperand.getOpcode() != ISD::AND ||
3212 !(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))) ||
3213 !isShiftedMask_64(CN->getZExtValue(), MaskIdx, MaskLen))
3214 return SDValue();
3215
3216 // The second operand (shift amount) must be an immediate.
3217 if (!(CN = dyn_cast<ConstantSDNode>(N->getOperand(1))))
3218 return SDValue();
3219
3220 Shamt = CN->getZExtValue();
3221 if (MaskIdx <= Shamt && Shamt <= MaskIdx + MaskLen - 1)
3222 return DAG.getNode(LoongArchISD::BSTRPICK, DL, ValTy,
3223 FirstOperand->getOperand(0),
3224 DAG.getConstant(MaskIdx + MaskLen - 1, DL, GRLenVT),
3225 DAG.getConstant(Shamt, DL, GRLenVT));
3226
3227 return SDValue();
3228}
3229
3232 const LoongArchSubtarget &Subtarget) {
3233 MVT GRLenVT = Subtarget.getGRLenVT();
3234 EVT ValTy = N->getValueType(0);
3235 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
3236 ConstantSDNode *CN0, *CN1;
3237 SDLoc DL(N);
3238 unsigned ValBits = ValTy.getSizeInBits();
3239 unsigned MaskIdx0, MaskLen0, MaskIdx1, MaskLen1;
3240 unsigned Shamt;
3241 bool SwapAndRetried = false;
3242
3243 if (DCI.isBeforeLegalizeOps())
3244 return SDValue();
3245
3246 if (ValBits != 32 && ValBits != 64)
3247 return SDValue();
3248
3249Retry:
3250 // 1st pattern to match BSTRINS:
3251 // R = or (and X, mask0), (and (shl Y, lsb), mask1)
3252 // where mask1 = (2**size - 1) << lsb, mask0 = ~mask1
3253 // =>
3254 // R = BSTRINS X, Y, msb, lsb (where msb = lsb + size - 1)
3255 if (N0.getOpcode() == ISD::AND &&
3256 (CN0 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) &&
3257 isShiftedMask_64(~CN0->getSExtValue(), MaskIdx0, MaskLen0) &&
3258 N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL &&
3259 (CN1 = dyn_cast<ConstantSDNode>(N1.getOperand(1))) &&
3260 isShiftedMask_64(CN1->getZExtValue(), MaskIdx1, MaskLen1) &&
3261 MaskIdx0 == MaskIdx1 && MaskLen0 == MaskLen1 &&
3262 (CN1 = dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(1))) &&
3263 (Shamt = CN1->getZExtValue()) == MaskIdx0 &&
3264 (MaskIdx0 + MaskLen0 <= ValBits)) {
3265 LLVM_DEBUG(dbgs() << "Perform OR combine: match pattern 1\n");
3266 return DAG.getNode(LoongArchISD::BSTRINS, DL, ValTy, N0.getOperand(0),
3267 N1.getOperand(0).getOperand(0),
3268 DAG.getConstant((MaskIdx0 + MaskLen0 - 1), DL, GRLenVT),
3269 DAG.getConstant(MaskIdx0, DL, GRLenVT));
3270 }
3271
3272 // 2nd pattern to match BSTRINS:
3273 // R = or (and X, mask0), (shl (and Y, mask1), lsb)
3274 // where mask1 = (2**size - 1), mask0 = ~(mask1 << lsb)
3275 // =>
3276 // R = BSTRINS X, Y, msb, lsb (where msb = lsb + size - 1)
3277 if (N0.getOpcode() == ISD::AND &&
3278 (CN0 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) &&
3279 isShiftedMask_64(~CN0->getSExtValue(), MaskIdx0, MaskLen0) &&
3280 N1.getOpcode() == ISD::SHL && N1.getOperand(0).getOpcode() == ISD::AND &&
3281 (CN1 = dyn_cast<ConstantSDNode>(N1.getOperand(1))) &&
3282 (Shamt = CN1->getZExtValue()) == MaskIdx0 &&
3283 (CN1 = dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(1))) &&
3284 isShiftedMask_64(CN1->getZExtValue(), MaskIdx1, MaskLen1) &&
3285 MaskLen0 == MaskLen1 && MaskIdx1 == 0 &&
3286 (MaskIdx0 + MaskLen0 <= ValBits)) {
3287 LLVM_DEBUG(dbgs() << "Perform OR combine: match pattern 2\n");
3288 return DAG.getNode(LoongArchISD::BSTRINS, DL, ValTy, N0.getOperand(0),
3289 N1.getOperand(0).getOperand(0),
3290 DAG.getConstant((MaskIdx0 + MaskLen0 - 1), DL, GRLenVT),
3291 DAG.getConstant(MaskIdx0, DL, GRLenVT));
3292 }
3293
3294 // 3rd pattern to match BSTRINS:
3295 // R = or (and X, mask0), (and Y, mask1)
3296 // where ~mask0 = (2**size - 1) << lsb, mask0 & mask1 = 0
3297 // =>
3298 // R = BSTRINS X, (shr (and Y, mask1), lsb), msb, lsb
3299 // where msb = lsb + size - 1
3300 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == ISD::AND &&
3301 (CN0 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) &&
3302 isShiftedMask_64(~CN0->getSExtValue(), MaskIdx0, MaskLen0) &&
3303 (MaskIdx0 + MaskLen0 <= 64) &&
3304 (CN1 = dyn_cast<ConstantSDNode>(N1->getOperand(1))) &&
3305 (CN1->getSExtValue() & CN0->getSExtValue()) == 0) {
3306 LLVM_DEBUG(dbgs() << "Perform OR combine: match pattern 3\n");
3307 return DAG.getNode(LoongArchISD::BSTRINS, DL, ValTy, N0.getOperand(0),
3308 DAG.getNode(ISD::SRL, DL, N1->getValueType(0), N1,
3309 DAG.getConstant(MaskIdx0, DL, GRLenVT)),
3310 DAG.getConstant(ValBits == 32
3311 ? (MaskIdx0 + (MaskLen0 & 31) - 1)
3312 : (MaskIdx0 + MaskLen0 - 1),
3313 DL, GRLenVT),
3314 DAG.getConstant(MaskIdx0, DL, GRLenVT));
3315 }
3316
3317 // 4th pattern to match BSTRINS:
3318 // R = or (and X, mask), (shl Y, shamt)
3319 // where mask = (2**shamt - 1)
3320 // =>
3321 // R = BSTRINS X, Y, ValBits - 1, shamt
3322 // where ValBits = 32 or 64
3323 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == ISD::SHL &&
3324 (CN0 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) &&
3325 isShiftedMask_64(CN0->getZExtValue(), MaskIdx0, MaskLen0) &&
3326 MaskIdx0 == 0 && (CN1 = dyn_cast<ConstantSDNode>(N1.getOperand(1))) &&
3327 (Shamt = CN1->getZExtValue()) == MaskLen0 &&
3328 (MaskIdx0 + MaskLen0 <= ValBits)) {
3329 LLVM_DEBUG(dbgs() << "Perform OR combine: match pattern 4\n");
3330 return DAG.getNode(LoongArchISD::BSTRINS, DL, ValTy, N0.getOperand(0),
3331 N1.getOperand(0),
3332 DAG.getConstant((ValBits - 1), DL, GRLenVT),
3333 DAG.getConstant(Shamt, DL, GRLenVT));
3334 }
3335
3336 // 5th pattern to match BSTRINS:
3337 // R = or (and X, mask), const
3338 // where ~mask = (2**size - 1) << lsb, mask & const = 0
3339 // =>
3340 // R = BSTRINS X, (const >> lsb), msb, lsb
3341 // where msb = lsb + size - 1
3342 if (N0.getOpcode() == ISD::AND &&
3343 (CN0 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) &&
3344 isShiftedMask_64(~CN0->getSExtValue(), MaskIdx0, MaskLen0) &&
3345 (CN1 = dyn_cast<ConstantSDNode>(N1)) &&
3346 (CN1->getSExtValue() & CN0->getSExtValue()) == 0) {
3347 LLVM_DEBUG(dbgs() << "Perform OR combine: match pattern 5\n");
3348 return DAG.getNode(
3349 LoongArchISD::BSTRINS, DL, ValTy, N0.getOperand(0),
3350 DAG.getConstant(CN1->getSExtValue() >> MaskIdx0, DL, ValTy),
3351 DAG.getConstant(ValBits == 32 ? (MaskIdx0 + (MaskLen0 & 31) - 1)
3352 : (MaskIdx0 + MaskLen0 - 1),
3353 DL, GRLenVT),
3354 DAG.getConstant(MaskIdx0, DL, GRLenVT));
3355 }
3356
3357 // 6th pattern.
3358 // a = b | ((c & mask) << shamt), where all positions in b to be overwritten
3359 // by the incoming bits are known to be zero.
3360 // =>
3361 // a = BSTRINS b, c, shamt + MaskLen - 1, shamt
3362 //
3363 // Note that the 1st pattern is a special situation of the 6th, i.e. the 6th
3364 // pattern is more common than the 1st. So we put the 1st before the 6th in
3365 // order to match as many nodes as possible.
3366 ConstantSDNode *CNMask, *CNShamt;
3367 unsigned MaskIdx, MaskLen;
3368 if (N1.getOpcode() == ISD::SHL && N1.getOperand(0).getOpcode() == ISD::AND &&
3369 (CNMask = dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(1))) &&
3370 isShiftedMask_64(CNMask->getZExtValue(), MaskIdx, MaskLen) &&
3371 MaskIdx == 0 && (CNShamt = dyn_cast<ConstantSDNode>(N1.getOperand(1))) &&
3372 CNShamt->getZExtValue() + MaskLen <= ValBits) {
3373 Shamt = CNShamt->getZExtValue();
3374 APInt ShMask(ValBits, CNMask->getZExtValue() << Shamt);
3375 if (ShMask.isSubsetOf(DAG.computeKnownBits(N0).Zero)) {
3376 LLVM_DEBUG(dbgs() << "Perform OR combine: match pattern 6\n");
3377 return DAG.getNode(LoongArchISD::BSTRINS, DL, ValTy, N0,
3378 N1.getOperand(0).getOperand(0),
3379 DAG.getConstant(Shamt + MaskLen - 1, DL, GRLenVT),
3380 DAG.getConstant(Shamt, DL, GRLenVT));
3381 }
3382 }
3383
3384 // 7th pattern.
3385 // a = b | ((c << shamt) & shifted_mask), where all positions in b to be
3386 // overwritten by the incoming bits are known to be zero.
3387 // =>
3388 // a = BSTRINS b, c, MaskIdx + MaskLen - 1, MaskIdx
3389 //
3390 // Similarly, the 7th pattern is more common than the 2nd. So we put the 2nd
3391 // before the 7th in order to match as many nodes as possible.
3392 if (N1.getOpcode() == ISD::AND &&
3393 (CNMask = dyn_cast<ConstantSDNode>(N1.getOperand(1))) &&
3394 isShiftedMask_64(CNMask->getZExtValue(), MaskIdx, MaskLen) &&
3395 N1.getOperand(0).getOpcode() == ISD::SHL &&
3396 (CNShamt = dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(1))) &&
3397 CNShamt->getZExtValue() == MaskIdx) {
3398 APInt ShMask(ValBits, CNMask->getZExtValue());
3399 if (ShMask.isSubsetOf(DAG.computeKnownBits(N0).Zero)) {
3400 LLVM_DEBUG(dbgs() << "Perform OR combine: match pattern 7\n");
3401 return DAG.getNode(LoongArchISD::BSTRINS, DL, ValTy, N0,
3402 N1.getOperand(0).getOperand(0),
3403 DAG.getConstant(MaskIdx + MaskLen - 1, DL, GRLenVT),
3404 DAG.getConstant(MaskIdx, DL, GRLenVT));
3405 }
3406 }
3407
3408 // (or a, b) and (or b, a) are equivalent, so swap the operands and retry.
3409 if (!SwapAndRetried) {
3410 std::swap(N0, N1);
3411 SwapAndRetried = true;
3412 goto Retry;
3413 }
3414
3415 SwapAndRetried = false;
3416Retry2:
3417 // 8th pattern.
3418 // a = b | (c & shifted_mask), where all positions in b to be overwritten by
3419 // the incoming bits are known to be zero.
3420 // =>
3421 // a = BSTRINS b, c >> MaskIdx, MaskIdx + MaskLen - 1, MaskIdx
3422 //
3423 // Similarly, the 8th pattern is more common than the 4th and 5th patterns. So
3424 // we put it here in order to match as many nodes as possible or generate less
3425 // instructions.
3426 if (N1.getOpcode() == ISD::AND &&
3427 (CNMask = dyn_cast<ConstantSDNode>(N1.getOperand(1))) &&
3428 isShiftedMask_64(CNMask->getZExtValue(), MaskIdx, MaskLen)) {
3429 APInt ShMask(ValBits, CNMask->getZExtValue());
3430 if (ShMask.isSubsetOf(DAG.computeKnownBits(N0).Zero)) {
3431 LLVM_DEBUG(dbgs() << "Perform OR combine: match pattern 8\n");
3432 return DAG.getNode(LoongArchISD::BSTRINS, DL, ValTy, N0,
3433 DAG.getNode(ISD::SRL, DL, N1->getValueType(0),
3434 N1->getOperand(0),
3435 DAG.getConstant(MaskIdx, DL, GRLenVT)),
3436 DAG.getConstant(MaskIdx + MaskLen - 1, DL, GRLenVT),
3437 DAG.getConstant(MaskIdx, DL, GRLenVT));
3438 }
3439 }
3440 // Swap N0/N1 and retry.
3441 if (!SwapAndRetried) {
3442 std::swap(N0, N1);
3443 SwapAndRetried = true;
3444 goto Retry2;
3445 }
3446
3447 return SDValue();
3448}
3449
3450static bool checkValueWidth(SDValue V, ISD::LoadExtType &ExtType) {
3451 ExtType = ISD::NON_EXTLOAD;
3452
3453 switch (V.getNode()->getOpcode()) {
3454 case ISD::LOAD: {
3455 LoadSDNode *LoadNode = cast<LoadSDNode>(V.getNode());
3456 if ((LoadNode->getMemoryVT() == MVT::i8) ||
3457 (LoadNode->getMemoryVT() == MVT::i16)) {
3458 ExtType = LoadNode->getExtensionType();
3459 return true;
3460 }
3461 return false;
3462 }
3463 case ISD::AssertSext: {
3464 VTSDNode *TypeNode = cast<VTSDNode>(V.getNode()->getOperand(1));
3465 if ((TypeNode->getVT() == MVT::i8) || (TypeNode->getVT() == MVT::i16)) {
3466 ExtType = ISD::SEXTLOAD;
3467 return true;
3468 }
3469 return false;
3470 }
3471 case ISD::AssertZext: {
3472 VTSDNode *TypeNode = cast<VTSDNode>(V.getNode()->getOperand(1));
3473 if ((TypeNode->getVT() == MVT::i8) || (TypeNode->getVT() == MVT::i16)) {
3474 ExtType = ISD::ZEXTLOAD;
3475 return true;
3476 }
3477 return false;
3478 }
3479 default:
3480 return false;
3481 }
3482
3483 return false;
3484}
3485
3486// Eliminate redundant truncation and zero-extension nodes.
3487// * Case 1:
3488// +------------+ +------------+ +------------+
3489// | Input1 | | Input2 | | CC |
3490// +------------+ +------------+ +------------+
3491// | | |
3492// V V +----+
3493// +------------+ +------------+ |
3494// | TRUNCATE | | TRUNCATE | |
3495// +------------+ +------------+ |
3496// | | |
3497// V V |
3498// +------------+ +------------+ |
3499// | ZERO_EXT | | ZERO_EXT | |
3500// +------------+ +------------+ |
3501// | | |
3502// | +-------------+ |
3503// V V | |
3504// +----------------+ | |
3505// | AND | | |
3506// +----------------+ | |
3507// | | |
3508// +---------------+ | |
3509// | | |
3510// V V V
3511// +-------------+
3512// | CMP |
3513// +-------------+
3514// * Case 2:
3515// +------------+ +------------+ +-------------+ +------------+ +------------+
3516// | Input1 | | Input2 | | Constant -1 | | Constant 0 | | CC |
3517// +------------+ +------------+ +-------------+ +------------+ +------------+
3518// | | | | |
3519// V | | | |
3520// +------------+ | | | |
3521// | XOR |<---------------------+ | |
3522// +------------+ | | |
3523// | | | |
3524// V V +---------------+ |
3525// +------------+ +------------+ | |
3526// | TRUNCATE | | TRUNCATE | | +-------------------------+
3527// +------------+ +------------+ | |
3528// | | | |
3529// V V | |
3530// +------------+ +------------+ | |
3531// | ZERO_EXT | | ZERO_EXT | | |
3532// +------------+ +------------+ | |
3533// | | | |
3534// V V | |
3535// +----------------+ | |
3536// | AND | | |
3537// +----------------+ | |
3538// | | |
3539// +---------------+ | |
3540// | | |
3541// V V V
3542// +-------------+
3543// | CMP |
3544// +-------------+
3547 const LoongArchSubtarget &Subtarget) {
3548 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
3549
3550 SDNode *AndNode = N->getOperand(0).getNode();
3551 if (AndNode->getOpcode() != ISD::AND)
3552 return SDValue();
3553
3554 SDValue AndInputValue2 = AndNode->getOperand(1);
3555 if (AndInputValue2.getOpcode() != ISD::ZERO_EXTEND)
3556 return SDValue();
3557
3558 SDValue CmpInputValue = N->getOperand(1);
3559 SDValue AndInputValue1 = AndNode->getOperand(0);
3560 if (AndInputValue1.getOpcode() == ISD::XOR) {
3561 if (CC != ISD::SETEQ && CC != ISD::SETNE)
3562 return SDValue();
3563 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(AndInputValue1.getOperand(1));
3564 if (!CN || CN->getSExtValue() != -1)
3565 return SDValue();
3566 CN = dyn_cast<ConstantSDNode>(CmpInputValue);
3567 if (!CN || CN->getSExtValue() != 0)
3568 return SDValue();
3569 AndInputValue1 = AndInputValue1.getOperand(0);
3570 if (AndInputValue1.getOpcode() != ISD::ZERO_EXTEND)
3571 return SDValue();
3572 } else if (AndInputValue1.getOpcode() == ISD::ZERO_EXTEND) {
3573 if (AndInputValue2 != CmpInputValue)
3574 return SDValue();
3575 } else {
3576 return SDValue();
3577 }
3578
3579 SDValue TruncValue1 = AndInputValue1.getNode()->getOperand(0);
3580 if (TruncValue1.getOpcode() != ISD::TRUNCATE)
3581 return SDValue();
3582
3583 SDValue TruncValue2 = AndInputValue2.getNode()->getOperand(0);
3584 if (TruncValue2.getOpcode() != ISD::TRUNCATE)
3585 return SDValue();
3586
3587 SDValue TruncInputValue1 = TruncValue1.getNode()->getOperand(0);
3588 SDValue TruncInputValue2 = TruncValue2.getNode()->getOperand(0);
3589 ISD::LoadExtType ExtType1;
3590 ISD::LoadExtType ExtType2;
3591
3592 if (!checkValueWidth(TruncInputValue1, ExtType1) ||
3593 !checkValueWidth(TruncInputValue2, ExtType2))
3594 return SDValue();
3595
3596 if (TruncInputValue1->getValueType(0) != TruncInputValue2->getValueType(0) ||
3597 AndNode->getValueType(0) != TruncInputValue1->getValueType(0))
3598 return SDValue();
3599
3600 if ((ExtType2 != ISD::ZEXTLOAD) &&
3601 ((ExtType2 != ISD::SEXTLOAD) && (ExtType1 != ISD::SEXTLOAD)))
3602 return SDValue();
3603
3604 // These truncation and zero-extension nodes are not necessary, remove them.
3605 SDValue NewAnd = DAG.getNode(ISD::AND, SDLoc(N), AndNode->getValueType(0),
3606 TruncInputValue1, TruncInputValue2);
3607 SDValue NewSetCC =
3608 DAG.getSetCC(SDLoc(N), N->getValueType(0), NewAnd, TruncInputValue2, CC);
3609 DAG.ReplaceAllUsesWith(N, NewSetCC.getNode());
3610 return SDValue(N, 0);
3611}
3612
3613// Combine (loongarch_bitrev_w (loongarch_revb_2w X)) to loongarch_bitrev_4b.
3616 const LoongArchSubtarget &Subtarget) {
3617 if (DCI.isBeforeLegalizeOps())
3618 return SDValue();
3619
3620 SDValue Src = N->getOperand(0);
3621 if (Src.getOpcode() != LoongArchISD::REVB_2W)
3622 return SDValue();
3623
3624 return DAG.getNode(LoongArchISD::BITREV_4B, SDLoc(N), N->getValueType(0),
3625 Src.getOperand(0));
3626}
3627
3628template <unsigned N>
3630 SelectionDAG &DAG,
3631 const LoongArchSubtarget &Subtarget,
3632 bool IsSigned = false) {
3633 SDLoc DL(Node);
3634 auto *CImm = cast<ConstantSDNode>(Node->getOperand(ImmOp));
3635 // Check the ImmArg.
3636 if ((IsSigned && !isInt<N>(CImm->getSExtValue())) ||
3637 (!IsSigned && !isUInt<N>(CImm->getZExtValue()))) {
3638 DAG.getContext()->emitError(Node->getOperationName(0) +
3639 ": argument out of range.");
3640 return DAG.getNode(ISD::UNDEF, DL, Subtarget.getGRLenVT());
3641 }
3642 return DAG.getConstant(CImm->getZExtValue(), DL, Subtarget.getGRLenVT());
3643}
3644
3645template <unsigned N>
3646static SDValue lowerVectorSplatImm(SDNode *Node, unsigned ImmOp,
3647 SelectionDAG &DAG, bool IsSigned = false) {
3648 SDLoc DL(Node);
3649 EVT ResTy = Node->getValueType(0);
3650 auto *CImm = cast<ConstantSDNode>(Node->getOperand(ImmOp));
3651
3652 // Check the ImmArg.
3653 if ((IsSigned && !isInt<N>(CImm->getSExtValue())) ||
3654 (!IsSigned && !isUInt<N>(CImm->getZExtValue()))) {
3655 DAG.getContext()->emitError(Node->getOperationName(0) +
3656 ": argument out of range.");
3657 return DAG.getNode(ISD::UNDEF, DL, ResTy);
3658 }
3659 return DAG.getConstant(
3661 IsSigned ? CImm->getSExtValue() : CImm->getZExtValue(), IsSigned),
3662 DL, ResTy);
3663}
3664
3666 SDLoc DL(Node);
3667 EVT ResTy = Node->getValueType(0);
3668 SDValue Vec = Node->getOperand(2);
3669 SDValue Mask = DAG.getConstant(Vec.getScalarValueSizeInBits() - 1, DL, ResTy);
3670 return DAG.getNode(ISD::AND, DL, ResTy, Vec, Mask);
3671}
3672
3674 SDLoc DL(Node);
3675 EVT ResTy = Node->getValueType(0);
3676 SDValue One = DAG.getConstant(1, DL, ResTy);
3677 SDValue Bit =
3678 DAG.getNode(ISD::SHL, DL, ResTy, One, truncateVecElts(Node, DAG));
3679
3680 return DAG.getNode(ISD::AND, DL, ResTy, Node->getOperand(1),
3681 DAG.getNOT(DL, Bit, ResTy));
3682}
3683
3684template <unsigned N>
3686 SDLoc DL(Node);
3687 EVT ResTy = Node->getValueType(0);
3688 auto *CImm = cast<ConstantSDNode>(Node->getOperand(2));
3689 // Check the unsigned ImmArg.
3690 if (!isUInt<N>(CImm->getZExtValue())) {
3691 DAG.getContext()->emitError(Node->getOperationName(0) +
3692 ": argument out of range.");
3693 return DAG.getNode(ISD::UNDEF, DL, ResTy);
3694 }
3695
3696 APInt BitImm = APInt(ResTy.getScalarSizeInBits(), 1) << CImm->getAPIntValue();
3697 SDValue Mask = DAG.getConstant(~BitImm, DL, ResTy);
3698
3699 return DAG.getNode(ISD::AND, DL, ResTy, Node->getOperand(1), Mask);
3700}
3701
3702template <unsigned N>
3704 SDLoc DL(Node);
3705 EVT ResTy = Node->getValueType(0);
3706 auto *CImm = cast<ConstantSDNode>(Node->getOperand(2));
3707 // Check the unsigned ImmArg.
3708 if (!isUInt<N>(CImm->getZExtValue())) {
3709 DAG.getContext()->emitError(Node->getOperationName(0) +
3710 ": argument out of range.");
3711 return DAG.getNode(ISD::UNDEF, DL, ResTy);
3712 }
3713
3714 APInt Imm = APInt(ResTy.getScalarSizeInBits(), 1) << CImm->getAPIntValue();
3715 SDValue BitImm = DAG.getConstant(Imm, DL, ResTy);
3716 return DAG.getNode(ISD::OR, DL, ResTy, Node->getOperand(1), BitImm);
3717}
3718
3719template <unsigned N>
3721 SDLoc DL(Node);
3722 EVT ResTy = Node->getValueType(0);
3723 auto *CImm = cast<ConstantSDNode>(Node->getOperand(2));
3724 // Check the unsigned ImmArg.
3725 if (!isUInt<N>(CImm->getZExtValue())) {
3726 DAG.getContext()->emitError(Node->getOperationName(0) +
3727 ": argument out of range.");
3728 return DAG.getNode(ISD::UNDEF, DL, ResTy);
3729 }
3730
3731 APInt Imm = APInt(ResTy.getScalarSizeInBits(), 1) << CImm->getAPIntValue();
3732 SDValue BitImm = DAG.getConstant(Imm, DL, ResTy);
3733 return DAG.getNode(ISD::XOR, DL, ResTy, Node->getOperand(1), BitImm);
3734}
3735
3736static SDValue
3739 const LoongArchSubtarget &Subtarget) {
3740 SDLoc DL(N);
3741 switch (N->getConstantOperandVal(0)) {
3742 default:
3743 break;
3744 case Intrinsic::loongarch_lsx_vadd_b:
3745 case Intrinsic::loongarch_lsx_vadd_h:
3746 case Intrinsic::loongarch_lsx_vadd_w:
3747 case Intrinsic::loongarch_lsx_vadd_d:
3748 case Intrinsic::loongarch_lasx_xvadd_b:
3749 case Intrinsic::loongarch_lasx_xvadd_h:
3750 case Intrinsic::loongarch_lasx_xvadd_w:
3751 case Intrinsic::loongarch_lasx_xvadd_d:
3752 return DAG.getNode(ISD::ADD, DL, N->getValueType(0), N->getOperand(1),
3753 N->getOperand(2));
3754 case Intrinsic::loongarch_lsx_vaddi_bu:
3755 case Intrinsic::loongarch_lsx_vaddi_hu:
3756 case Intrinsic::loongarch_lsx_vaddi_wu:
3757 case Intrinsic::loongarch_lsx_vaddi_du:
3758 case Intrinsic::loongarch_lasx_xvaddi_bu:
3759 case Intrinsic::loongarch_lasx_xvaddi_hu:
3760 case Intrinsic::loongarch_lasx_xvaddi_wu:
3761 case Intrinsic::loongarch_lasx_xvaddi_du:
3762 return DAG.getNode(ISD::ADD, DL, N->getValueType(0), N->getOperand(1),
3763 lowerVectorSplatImm<5>(N, 2, DAG));
3764 case Intrinsic::loongarch_lsx_vsub_b:
3765 case Intrinsic::loongarch_lsx_vsub_h:
3766 case Intrinsic::loongarch_lsx_vsub_w:
3767 case Intrinsic::loongarch_lsx_vsub_d:
3768 case Intrinsic::loongarch_lasx_xvsub_b:
3769 case Intrinsic::loongarch_lasx_xvsub_h:
3770 case Intrinsic::loongarch_lasx_xvsub_w:
3771 case Intrinsic::loongarch_lasx_xvsub_d:
3772 return DAG.getNode(ISD::SUB, DL, N->getValueType(0), N->getOperand(1),
3773 N->getOperand(2));
3774 case Intrinsic::loongarch_lsx_vsubi_bu:
3775 case Intrinsic::loongarch_lsx_vsubi_hu:
3776 case Intrinsic::loongarch_lsx_vsubi_wu:
3777 case Intrinsic::loongarch_lsx_vsubi_du:
3778 case Intrinsic::loongarch_lasx_xvsubi_bu:
3779 case Intrinsic::loongarch_lasx_xvsubi_hu:
3780 case Intrinsic::loongarch_lasx_xvsubi_wu:
3781 case Intrinsic::loongarch_lasx_xvsubi_du:
3782 return DAG.getNode(ISD::SUB, DL, N->getValueType(0), N->getOperand(1),
3783 lowerVectorSplatImm<5>(N, 2, DAG));
3784 case Intrinsic::loongarch_lsx_vneg_b:
3785 case Intrinsic::loongarch_lsx_vneg_h:
3786 case Intrinsic::loongarch_lsx_vneg_w:
3787 case Intrinsic::loongarch_lsx_vneg_d:
3788 case Intrinsic::loongarch_lasx_xvneg_b:
3789 case Intrinsic::loongarch_lasx_xvneg_h:
3790 case Intrinsic::loongarch_lasx_xvneg_w:
3791 case Intrinsic::loongarch_lasx_xvneg_d:
3792 return DAG.getNode(
3793 ISD::SUB, DL, N->getValueType(0),
3794 DAG.getConstant(
3795 APInt(N->getValueType(0).getScalarType().getSizeInBits(), 0,
3796 /*isSigned=*/true),
3797 SDLoc(N), N->getValueType(0)),
3798 N->getOperand(1));
3799 case Intrinsic::loongarch_lsx_vmax_b:
3800 case Intrinsic::loongarch_lsx_vmax_h:
3801 case Intrinsic::loongarch_lsx_vmax_w:
3802 case Intrinsic::loongarch_lsx_vmax_d:
3803 case Intrinsic::loongarch_lasx_xvmax_b:
3804 case Intrinsic::loongarch_lasx_xvmax_h:
3805 case Intrinsic::loongarch_lasx_xvmax_w:
3806 case Intrinsic::loongarch_lasx_xvmax_d:
3807 return DAG.getNode(ISD::SMAX, DL, N->getValueType(0), N->getOperand(1),
3808 N->getOperand(2));
3809 case Intrinsic::loongarch_lsx_vmax_bu:
3810 case Intrinsic::loongarch_lsx_vmax_hu:
3811 case Intrinsic::loongarch_lsx_vmax_wu:
3812 case Intrinsic::loongarch_lsx_vmax_du:
3813 case Intrinsic::loongarch_lasx_xvmax_bu:
3814 case Intrinsic::loongarch_lasx_xvmax_hu:
3815 case Intrinsic::loongarch_lasx_xvmax_wu:
3816 case Intrinsic::loongarch_lasx_xvmax_du:
3817 return DAG.getNode(ISD::UMAX, DL, N->getValueType(0), N->getOperand(1),
3818 N->getOperand(2));
3819 case Intrinsic::loongarch_lsx_vmaxi_b:
3820 case Intrinsic::loongarch_lsx_vmaxi_h:
3821 case Intrinsic::loongarch_lsx_vmaxi_w:
3822 case Intrinsic::loongarch_lsx_vmaxi_d:
3823 case Intrinsic::loongarch_lasx_xvmaxi_b:
3824 case Intrinsic::loongarch_lasx_xvmaxi_h:
3825 case Intrinsic::loongarch_lasx_xvmaxi_w:
3826 case Intrinsic::loongarch_lasx_xvmaxi_d:
3827 return DAG.getNode(ISD::SMAX, DL, N->getValueType(0), N->getOperand(1),
3828 lowerVectorSplatImm<5>(N, 2, DAG, /*IsSigned=*/true));
3829 case Intrinsic::loongarch_lsx_vmaxi_bu:
3830 case Intrinsic::loongarch_lsx_vmaxi_hu:
3831 case Intrinsic::loongarch_lsx_vmaxi_wu:
3832 case Intrinsic::loongarch_lsx_vmaxi_du:
3833 case Intrinsic::loongarch_lasx_xvmaxi_bu:
3834 case Intrinsic::loongarch_lasx_xvmaxi_hu:
3835 case Intrinsic::loongarch_lasx_xvmaxi_wu:
3836 case Intrinsic::loongarch_lasx_xvmaxi_du:
3837 return DAG.getNode(ISD::UMAX, DL, N->getValueType(0), N->getOperand(1),
3838 lowerVectorSplatImm<5>(N, 2, DAG));
3839 case Intrinsic::loongarch_lsx_vmin_b:
3840 case Intrinsic::loongarch_lsx_vmin_h:
3841 case Intrinsic::loongarch_lsx_vmin_w:
3842 case Intrinsic::loongarch_lsx_vmin_d:
3843 case Intrinsic::loongarch_lasx_xvmin_b:
3844 case Intrinsic::loongarch_lasx_xvmin_h:
3845 case Intrinsic::loongarch_lasx_xvmin_w:
3846 case Intrinsic::loongarch_lasx_xvmin_d:
3847 return DAG.getNode(ISD::SMIN, DL, N->getValueType(0), N->getOperand(1),
3848 N->getOperand(2));
3849 case Intrinsic::loongarch_lsx_vmin_bu:
3850 case Intrinsic::loongarch_lsx_vmin_hu:
3851 case Intrinsic::loongarch_lsx_vmin_wu:
3852 case Intrinsic::loongarch_lsx_vmin_du:
3853 case Intrinsic::loongarch_lasx_xvmin_bu:
3854 case Intrinsic::loongarch_lasx_xvmin_hu:
3855 case Intrinsic::loongarch_lasx_xvmin_wu:
3856 case Intrinsic::loongarch_lasx_xvmin_du:
3857 return DAG.getNode(ISD::UMIN, DL, N->getValueType(0), N->getOperand(1),
3858 N->getOperand(2));
3859 case Intrinsic::loongarch_lsx_vmini_b:
3860 case Intrinsic::loongarch_lsx_vmini_h:
3861 case Intrinsic::loongarch_lsx_vmini_w:
3862 case Intrinsic::loongarch_lsx_vmini_d:
3863 case Intrinsic::loongarch_lasx_xvmini_b:
3864 case Intrinsic::loongarch_lasx_xvmini_h:
3865 case Intrinsic::loongarch_lasx_xvmini_w:
3866 case Intrinsic::loongarch_lasx_xvmini_d:
3867 return DAG.getNode(ISD::SMIN, DL, N->getValueType(0), N->getOperand(1),
3868 lowerVectorSplatImm<5>(N, 2, DAG, /*IsSigned=*/true));
3869 case Intrinsic::loongarch_lsx_vmini_bu:
3870 case Intrinsic::loongarch_lsx_vmini_hu:
3871 case Intrinsic::loongarch_lsx_vmini_wu:
3872 case Intrinsic::loongarch_lsx_vmini_du:
3873 case Intrinsic::loongarch_lasx_xvmini_bu:
3874 case Intrinsic::loongarch_lasx_xvmini_hu:
3875 case Intrinsic::loongarch_lasx_xvmini_wu:
3876 case Intrinsic::loongarch_lasx_xvmini_du:
3877 return DAG.getNode(ISD::UMIN, DL, N->getValueType(0), N->getOperand(1),
3878 lowerVectorSplatImm<5>(N, 2, DAG));
3879 case Intrinsic::loongarch_lsx_vmul_b:
3880 case Intrinsic::loongarch_lsx_vmul_h:
3881 case Intrinsic::loongarch_lsx_vmul_w:
3882 case Intrinsic::loongarch_lsx_vmul_d:
3883 case Intrinsic::loongarch_lasx_xvmul_b:
3884 case Intrinsic::loongarch_lasx_xvmul_h:
3885 case Intrinsic::loongarch_lasx_xvmul_w:
3886 case Intrinsic::loongarch_lasx_xvmul_d:
3887 return DAG.getNode(ISD::MUL, DL, N->getValueType(0), N->getOperand(1),
3888 N->getOperand(2));
3889 case Intrinsic::loongarch_lsx_vmadd_b:
3890 case Intrinsic::loongarch_lsx_vmadd_h:
3891 case Intrinsic::loongarch_lsx_vmadd_w:
3892 case Intrinsic::loongarch_lsx_vmadd_d:
3893 case Intrinsic::loongarch_lasx_xvmadd_b:
3894 case Intrinsic::loongarch_lasx_xvmadd_h:
3895 case Intrinsic::loongarch_lasx_xvmadd_w:
3896 case Intrinsic::loongarch_lasx_xvmadd_d: {
3897 EVT ResTy = N->getValueType(0);
3898 return DAG.getNode(ISD::ADD, SDLoc(N), ResTy, N->getOperand(1),
3899 DAG.getNode(ISD::MUL, SDLoc(N), ResTy, N->getOperand(2),
3900 N->getOperand(3)));
3901 }
3902 case Intrinsic::loongarch_lsx_vmsub_b:
3903 case Intrinsic::loongarch_lsx_vmsub_h:
3904 case Intrinsic::loongarch_lsx_vmsub_w:
3905 case Intrinsic::loongarch_lsx_vmsub_d:
3906 case Intrinsic::loongarch_lasx_xvmsub_b:
3907 case Intrinsic::loongarch_lasx_xvmsub_h:
3908 case Intrinsic::loongarch_lasx_xvmsub_w:
3909 case Intrinsic::loongarch_lasx_xvmsub_d: {
3910 EVT ResTy = N->getValueType(0);
3911 return DAG.getNode(ISD::SUB, SDLoc(N), ResTy, N->getOperand(1),
3912 DAG.getNode(ISD::MUL, SDLoc(N), ResTy, N->getOperand(2),
3913 N->getOperand(3)));
3914 }
3915 case Intrinsic::loongarch_lsx_vdiv_b:
3916 case Intrinsic::loongarch_lsx_vdiv_h:
3917 case Intrinsic::loongarch_lsx_vdiv_w:
3918 case Intrinsic::loongarch_lsx_vdiv_d:
3919 case Intrinsic::loongarch_lasx_xvdiv_b:
3920 case Intrinsic::loongarch_lasx_xvdiv_h:
3921 case Intrinsic::loongarch_lasx_xvdiv_w:
3922 case Intrinsic::loongarch_lasx_xvdiv_d:
3923 return DAG.getNode(ISD::SDIV, DL, N->getValueType(0), N->getOperand(1),
3924 N->getOperand(2));
3925 case Intrinsic::loongarch_lsx_vdiv_bu:
3926 case Intrinsic::loongarch_lsx_vdiv_hu:
3927 case Intrinsic::loongarch_lsx_vdiv_wu:
3928 case Intrinsic::loongarch_lsx_vdiv_du:
3929 case Intrinsic::loongarch_lasx_xvdiv_bu:
3930 case Intrinsic::loongarch_lasx_xvdiv_hu:
3931 case Intrinsic::loongarch_lasx_xvdiv_wu:
3932 case Intrinsic::loongarch_lasx_xvdiv_du:
3933 return DAG.getNode(ISD::UDIV, DL, N->getValueType(0), N->getOperand(1),
3934 N->getOperand(2));
3935 case Intrinsic::loongarch_lsx_vmod_b:
3936 case Intrinsic::loongarch_lsx_vmod_h:
3937 case Intrinsic::loongarch_lsx_vmod_w:
3938 case Intrinsic::loongarch_lsx_vmod_d:
3939 case Intrinsic::loongarch_lasx_xvmod_b:
3940 case Intrinsic::loongarch_lasx_xvmod_h:
3941 case Intrinsic::loongarch_lasx_xvmod_w:
3942 case Intrinsic::loongarch_lasx_xvmod_d:
3943 return DAG.getNode(ISD::SREM, DL, N->getValueType(0), N->getOperand(1),
3944 N->getOperand(2));
3945 case Intrinsic::loongarch_lsx_vmod_bu:
3946 case Intrinsic::loongarch_lsx_vmod_hu:
3947 case Intrinsic::loongarch_lsx_vmod_wu:
3948 case Intrinsic::loongarch_lsx_vmod_du:
3949 case Intrinsic::loongarch_lasx_xvmod_bu:
3950 case Intrinsic::loongarch_lasx_xvmod_hu:
3951 case Intrinsic::loongarch_lasx_xvmod_wu:
3952 case Intrinsic::loongarch_lasx_xvmod_du:
3953 return DAG.getNode(ISD::UREM, DL, N->getValueType(0), N->getOperand(1),
3954 N->getOperand(2));
3955 case Intrinsic::loongarch_lsx_vand_v:
3956 case Intrinsic::loongarch_lasx_xvand_v:
3957 return DAG.getNode(ISD::AND, DL, N->getValueType(0), N->getOperand(1),
3958 N->getOperand(2));
3959 case Intrinsic::loongarch_lsx_vor_v:
3960 case Intrinsic::loongarch_lasx_xvor_v:
3961 return DAG.getNode(ISD::OR, DL, N->getValueType(0), N->getOperand(1),
3962 N->getOperand(2));
3963 case Intrinsic::loongarch_lsx_vxor_v:
3964 case Intrinsic::loongarch_lasx_xvxor_v:
3965 return DAG.getNode(ISD::XOR, DL, N->getValueType(0), N->getOperand(1),
3966 N->getOperand(2));
3967 case Intrinsic::loongarch_lsx_vnor_v:
3968 case Intrinsic::loongarch_lasx_xvnor_v: {
3969 SDValue Res = DAG.getNode(ISD::OR, DL, N->getValueType(0), N->getOperand(1),
3970 N->getOperand(2));
3971 return DAG.getNOT(DL, Res, Res->getValueType(0));
3972 }
3973 case Intrinsic::loongarch_lsx_vandi_b:
3974 case Intrinsic::loongarch_lasx_xvandi_b:
3975 return DAG.getNode(ISD::AND, DL, N->getValueType(0), N->getOperand(1),
3976 lowerVectorSplatImm<8>(N, 2, DAG));
3977 case Intrinsic::loongarch_lsx_vori_b:
3978 case Intrinsic::loongarch_lasx_xvori_b:
3979 return DAG.getNode(ISD::OR, DL, N->getValueType(0), N->getOperand(1),
3980 lowerVectorSplatImm<8>(N, 2, DAG));
3981 case Intrinsic::loongarch_lsx_vxori_b:
3982 case Intrinsic::loongarch_lasx_xvxori_b:
3983 return DAG.getNode(ISD::XOR, DL, N->getValueType(0), N->getOperand(1),
3984 lowerVectorSplatImm<8>(N, 2, DAG));
3985 case Intrinsic::loongarch_lsx_vsll_b:
3986 case Intrinsic::loongarch_lsx_vsll_h:
3987 case Intrinsic::loongarch_lsx_vsll_w:
3988 case Intrinsic::loongarch_lsx_vsll_d:
3989 case Intrinsic::loongarch_lasx_xvsll_b:
3990 case Intrinsic::loongarch_lasx_xvsll_h:
3991 case Intrinsic::loongarch_lasx_xvsll_w:
3992 case Intrinsic::loongarch_lasx_xvsll_d:
3993 return DAG.getNode(ISD::SHL, DL, N->getValueType(0), N->getOperand(1),
3994 truncateVecElts(N, DAG));
3995 case Intrinsic::loongarch_lsx_vslli_b:
3996 case Intrinsic::loongarch_lasx_xvslli_b:
3997 return DAG.getNode(ISD::SHL, DL, N->getValueType(0), N->getOperand(1),
3998 lowerVectorSplatImm<3>(N, 2, DAG));
3999 case Intrinsic::loongarch_lsx_vslli_h:
4000 case Intrinsic::loongarch_lasx_xvslli_h:
4001 return DAG.getNode(ISD::SHL, DL, N->getValueType(0), N->getOperand(1),
4002 lowerVectorSplatImm<4>(N, 2, DAG));
4003 case Intrinsic::loongarch_lsx_vslli_w:
4004 case Intrinsic::loongarch_lasx_xvslli_w:
4005 return DAG.getNode(ISD::SHL, DL, N->getValueType(0), N->getOperand(1),
4006 lowerVectorSplatImm<5>(N, 2, DAG));
4007 case Intrinsic::loongarch_lsx_vslli_d:
4008 case Intrinsic::loongarch_lasx_xvslli_d:
4009 return DAG.getNode(ISD::SHL, DL, N->getValueType(0), N->getOperand(1),
4010 lowerVectorSplatImm<6>(N, 2, DAG));
4011 case Intrinsic::loongarch_lsx_vsrl_b:
4012 case Intrinsic::loongarch_lsx_vsrl_h:
4013 case Intrinsic::loongarch_lsx_vsrl_w:
4014 case Intrinsic::loongarch_lsx_vsrl_d:
4015 case Intrinsic::loongarch_lasx_xvsrl_b:
4016 case Intrinsic::loongarch_lasx_xvsrl_h:
4017 case Intrinsic::loongarch_lasx_xvsrl_w:
4018 case Intrinsic::loongarch_lasx_xvsrl_d:
4019 return DAG.getNode(ISD::SRL, DL, N->getValueType(0), N->getOperand(1),
4020 truncateVecElts(N, DAG));
4021 case Intrinsic::loongarch_lsx_vsrli_b:
4022 case Intrinsic::loongarch_lasx_xvsrli_b:
4023 return DAG.getNode(ISD::SRL, DL, N->getValueType(0), N->getOperand(1),
4024 lowerVectorSplatImm<3>(N, 2, DAG));
4025 case Intrinsic::loongarch_lsx_vsrli_h:
4026 case Intrinsic::loongarch_lasx_xvsrli_h:
4027 return DAG.getNode(ISD::SRL, DL, N->getValueType(0), N->getOperand(1),
4028 lowerVectorSplatImm<4>(N, 2, DAG));
4029 case Intrinsic::loongarch_lsx_vsrli_w:
4030 case Intrinsic::loongarch_lasx_xvsrli_w:
4031 return DAG.getNode(ISD::SRL, DL, N->getValueType(0), N->getOperand(1),
4032 lowerVectorSplatImm<5>(N, 2, DAG));
4033 case Intrinsic::loongarch_lsx_vsrli_d:
4034 case Intrinsic::loongarch_lasx_xvsrli_d:
4035 return DAG.getNode(ISD::SRL, DL, N->getValueType(0), N->getOperand(1),
4036 lowerVectorSplatImm<6>(N, 2, DAG));
4037 case Intrinsic::loongarch_lsx_vsra_b:
4038 case Intrinsic::loongarch_lsx_vsra_h:
4039 case Intrinsic::loongarch_lsx_vsra_w:
4040 case Intrinsic::loongarch_lsx_vsra_d:
4041 case Intrinsic::loongarch_lasx_xvsra_b:
4042 case Intrinsic::loongarch_lasx_xvsra_h:
4043 case Intrinsic::loongarch_lasx_xvsra_w:
4044 case Intrinsic::loongarch_lasx_xvsra_d:
4045 return DAG.getNode(ISD::SRA, DL, N->getValueType(0), N->getOperand(1),
4046 truncateVecElts(N, DAG));
4047 case Intrinsic::loongarch_lsx_vsrai_b:
4048 case Intrinsic::loongarch_lasx_xvsrai_b:
4049 return DAG.getNode(ISD::SRA, DL, N->getValueType(0), N->getOperand(1),
4050 lowerVectorSplatImm<3>(N, 2, DAG));
4051 case Intrinsic::loongarch_lsx_vsrai_h:
4052 case Intrinsic::loongarch_lasx_xvsrai_h:
4053 return DAG.getNode(ISD::SRA, DL, N->getValueType(0), N->getOperand(1),
4054 lowerVectorSplatImm<4>(N, 2, DAG));
4055 case Intrinsic::loongarch_lsx_vsrai_w:
4056 case Intrinsic::loongarch_lasx_xvsrai_w:
4057 return DAG.getNode(ISD::SRA, DL, N->getValueType(0), N->getOperand(1),
4058 lowerVectorSplatImm<5>(N, 2, DAG));
4059 case Intrinsic::loongarch_lsx_vsrai_d:
4060 case Intrinsic::loongarch_lasx_xvsrai_d:
4061 return DAG.getNode(ISD::SRA, DL, N->getValueType(0), N->getOperand(1),
4062 lowerVectorSplatImm<6>(N, 2, DAG));
4063 case Intrinsic::loongarch_lsx_vclz_b:
4064 case Intrinsic::loongarch_lsx_vclz_h:
4065 case Intrinsic::loongarch_lsx_vclz_w:
4066 case Intrinsic::loongarch_lsx_vclz_d:
4067 case Intrinsic::loongarch_lasx_xvclz_b:
4068 case Intrinsic::loongarch_lasx_xvclz_h:
4069 case Intrinsic::loongarch_lasx_xvclz_w:
4070 case Intrinsic::loongarch_lasx_xvclz_d:
4071 return DAG.getNode(ISD::CTLZ, DL, N->getValueType(0), N->getOperand(1));
4072 case Intrinsic::loongarch_lsx_vpcnt_b:
4073 case Intrinsic::loongarch_lsx_vpcnt_h:
4074 case Intrinsic::loongarch_lsx_vpcnt_w:
4075 case Intrinsic::loongarch_lsx_vpcnt_d:
4076 case Intrinsic::loongarch_lasx_xvpcnt_b:
4077 case Intrinsic::loongarch_lasx_xvpcnt_h:
4078 case Intrinsic::loongarch_lasx_xvpcnt_w:
4079 case Intrinsic::loongarch_lasx_xvpcnt_d:
4080 return DAG.getNode(ISD::CTPOP, DL, N->getValueType(0), N->getOperand(1));
4081 case Intrinsic::loongarch_lsx_vbitclr_b:
4082 case Intrinsic::loongarch_lsx_vbitclr_h:
4083 case Intrinsic::loongarch_lsx_vbitclr_w:
4084 case Intrinsic::loongarch_lsx_vbitclr_d:
4085 case Intrinsic::loongarch_lasx_xvbitclr_b:
4086 case Intrinsic::loongarch_lasx_xvbitclr_h:
4087 case Intrinsic::loongarch_lasx_xvbitclr_w:
4088 case Intrinsic::loongarch_lasx_xvbitclr_d:
4089 return lowerVectorBitClear(N, DAG);
4090 case Intrinsic::loongarch_lsx_vbitclri_b:
4091 case Intrinsic::loongarch_lasx_xvbitclri_b:
4092 return lowerVectorBitClearImm<3>(N, DAG);
4093 case Intrinsic::loongarch_lsx_vbitclri_h:
4094 case Intrinsic::loongarch_lasx_xvbitclri_h:
4095 return lowerVectorBitClearImm<4>(N, DAG);
4096 case Intrinsic::loongarch_lsx_vbitclri_w:
4097 case Intrinsic::loongarch_lasx_xvbitclri_w:
4098 return lowerVectorBitClearImm<5>(N, DAG);
4099 case Intrinsic::loongarch_lsx_vbitclri_d:
4100 case Intrinsic::loongarch_lasx_xvbitclri_d:
4101 return lowerVectorBitClearImm<6>(N, DAG);
4102 case Intrinsic::loongarch_lsx_vbitset_b:
4103 case Intrinsic::loongarch_lsx_vbitset_h:
4104 case Intrinsic::loongarch_lsx_vbitset_w:
4105 case Intrinsic::loongarch_lsx_vbitset_d:
4106 case Intrinsic::loongarch_lasx_xvbitset_b:
4107 case Intrinsic::loongarch_lasx_xvbitset_h:
4108 case Intrinsic::loongarch_lasx_xvbitset_w:
4109 case Intrinsic::loongarch_lasx_xvbitset_d: {
4110 EVT VecTy = N->getValueType(0);
4111 SDValue One = DAG.getConstant(1, DL, VecTy);
4112 return DAG.getNode(
4113 ISD::OR, DL, VecTy, N->getOperand(1),
4114 DAG.getNode(ISD::SHL, DL, VecTy, One, truncateVecElts(N, DAG)));
4115 }
4116 case Intrinsic::loongarch_lsx_vbitseti_b:
4117 case Intrinsic::loongarch_lasx_xvbitseti_b:
4118 return lowerVectorBitSetImm<3>(N, DAG);
4119 case Intrinsic::loongarch_lsx_vbitseti_h:
4120 case Intrinsic::loongarch_lasx_xvbitseti_h:
4121 return lowerVectorBitSetImm<4>(N, DAG);
4122 case Intrinsic::loongarch_lsx_vbitseti_w:
4123 case Intrinsic::loongarch_lasx_xvbitseti_w:
4124 return lowerVectorBitSetImm<5>(N, DAG);
4125 case Intrinsic::loongarch_lsx_vbitseti_d:
4126 case Intrinsic::loongarch_lasx_xvbitseti_d:
4127 return lowerVectorBitSetImm<6>(N, DAG);
4128 case Intrinsic::loongarch_lsx_vbitrev_b:
4129 case Intrinsic::loongarch_lsx_vbitrev_h:
4130 case Intrinsic::loongarch_lsx_vbitrev_w:
4131 case Intrinsic::loongarch_lsx_vbitrev_d:
4132 case Intrinsic::loongarch_lasx_xvbitrev_b:
4133 case Intrinsic::loongarch_lasx_xvbitrev_h:
4134 case Intrinsic::loongarch_lasx_xvbitrev_w:
4135 case Intrinsic::loongarch_lasx_xvbitrev_d: {
4136 EVT VecTy = N->getValueType(0);
4137 SDValue One = DAG.getConstant(1, DL, VecTy);
4138 return DAG.getNode(
4139 ISD::XOR, DL, VecTy, N->getOperand(1),
4140 DAG.getNode(ISD::SHL, DL, VecTy, One, truncateVecElts(N, DAG)));
4141 }
4142 case Intrinsic::loongarch_lsx_vbitrevi_b:
4143 case Intrinsic::loongarch_lasx_xvbitrevi_b:
4144 return lowerVectorBitRevImm<3>(N, DAG);
4145 case Intrinsic::loongarch_lsx_vbitrevi_h:
4146 case Intrinsic::loongarch_lasx_xvbitrevi_h:
4147 return lowerVectorBitRevImm<4>(N, DAG);
4148 case Intrinsic::loongarch_lsx_vbitrevi_w:
4149 case Intrinsic::loongarch_lasx_xvbitrevi_w:
4150 return lowerVectorBitRevImm<5>(N, DAG);
4151 case Intrinsic::loongarch_lsx_vbitrevi_d:
4152 case Intrinsic::loongarch_lasx_xvbitrevi_d:
4153 return lowerVectorBitRevImm<6>(N, DAG);
4154 case Intrinsic::loongarch_lsx_vfadd_s:
4155 case Intrinsic::loongarch_lsx_vfadd_d:
4156 case Intrinsic::loongarch_lasx_xvfadd_s:
4157 case Intrinsic::loongarch_lasx_xvfadd_d:
4158 return DAG.getNode(ISD::FADD, DL, N->getValueType(0), N->getOperand(1),
4159 N->getOperand(2));
4160 case Intrinsic::loongarch_lsx_vfsub_s:
4161 case Intrinsic::loongarch_lsx_vfsub_d:
4162 case Intrinsic::loongarch_lasx_xvfsub_s:
4163 case Intrinsic::loongarch_lasx_xvfsub_d:
4164 return DAG.getNode(ISD::FSUB, DL, N->getValueType(0), N->getOperand(1),
4165 N->getOperand(2));
4166 case Intrinsic::loongarch_lsx_vfmul_s:
4167 case Intrinsic::loongarch_lsx_vfmul_d:
4168 case Intrinsic::loongarch_lasx_xvfmul_s:
4169 case Intrinsic::loongarch_lasx_xvfmul_d:
4170 return DAG.getNode(ISD::FMUL, DL, N->getValueType(0), N->getOperand(1),
4171 N->getOperand(2));
4172 case Intrinsic::loongarch_lsx_vfdiv_s:
4173 case Intrinsic::loongarch_lsx_vfdiv_d:
4174 case Intrinsic::loongarch_lasx_xvfdiv_s:
4175 case Intrinsic::loongarch_lasx_xvfdiv_d:
4176 return DAG.getNode(ISD::FDIV, DL, N->getValueType(0), N->getOperand(1),
4177 N->getOperand(2));
4178 case Intrinsic::loongarch_lsx_vfmadd_s:
4179 case Intrinsic::loongarch_lsx_vfmadd_d:
4180 case Intrinsic::loongarch_lasx_xvfmadd_s:
4181 case Intrinsic::loongarch_lasx_xvfmadd_d:
4182 return DAG.getNode(ISD::FMA, DL, N->getValueType(0), N->getOperand(1),
4183 N->getOperand(2), N->getOperand(3));
4184 case Intrinsic::loongarch_lsx_vinsgr2vr_b:
4185 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N), N->getValueType(0),
4186 N->getOperand(1), N->getOperand(2),
4187 legalizeIntrinsicImmArg<4>(N, 3, DAG, Subtarget));
4188 case Intrinsic::loongarch_lsx_vinsgr2vr_h:
4189 case Intrinsic::loongarch_lasx_xvinsgr2vr_w:
4190 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N), N->getValueType(0),
4191 N->getOperand(1), N->getOperand(2),
4192 legalizeIntrinsicImmArg<3>(N, 3, DAG, Subtarget));
4193 case Intrinsic::loongarch_lsx_vinsgr2vr_w:
4194 case Intrinsic::loongarch_lasx_xvinsgr2vr_d:
4195 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N), N->getValueType(0),
4196 N->getOperand(1), N->getOperand(2),
4197 legalizeIntrinsicImmArg<2>(N, 3, DAG, Subtarget));
4198 case Intrinsic::loongarch_lsx_vinsgr2vr_d:
4199 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N), N->getValueType(0),
4200 N->getOperand(1), N->getOperand(2),
4201 legalizeIntrinsicImmArg<1>(N, 3, DAG, Subtarget));
4202 case Intrinsic::loongarch_lsx_vreplgr2vr_b:
4203 case Intrinsic::loongarch_lsx_vreplgr2vr_h:
4204 case Intrinsic::loongarch_lsx_vreplgr2vr_w:
4205 case Intrinsic::loongarch_lsx_vreplgr2vr_d:
4206 case Intrinsic::loongarch_lasx_xvreplgr2vr_b:
4207 case Intrinsic::loongarch_lasx_xvreplgr2vr_h:
4208 case Intrinsic::loongarch_lasx_xvreplgr2vr_w:
4209 case Intrinsic::loongarch_lasx_xvreplgr2vr_d: {
4210 EVT ResTy = N->getValueType(0);
4211 SmallVector<SDValue> Ops(ResTy.getVectorNumElements(), N->getOperand(1));
4212 return DAG.getBuildVector(ResTy, DL, Ops);
4213 }
4214 case Intrinsic::loongarch_lsx_vreplve_b:
4215 case Intrinsic::loongarch_lsx_vreplve_h:
4216 case Intrinsic::loongarch_lsx_vreplve_w:
4217 case Intrinsic::loongarch_lsx_vreplve_d:
4218 case Intrinsic::loongarch_lasx_xvreplve_b:
4219 case Intrinsic::loongarch_lasx_xvreplve_h:
4220 case Intrinsic::loongarch_lasx_xvreplve_w:
4221 case Intrinsic::loongarch_lasx_xvreplve_d:
4222 return DAG.getNode(LoongArchISD::VREPLVE, DL, N->getValueType(0),
4223 N->getOperand(1),
4224 DAG.getNode(ISD::ANY_EXTEND, DL, Subtarget.getGRLenVT(),
4225 N->getOperand(2)));
4226 }
4227 return SDValue();
4228}
4229
4231 DAGCombinerInfo &DCI) const {
4232 SelectionDAG &DAG = DCI.DAG;
4233 switch (N->getOpcode()) {
4234 default:
4235 break;
4236 case ISD::AND:
4237 return performANDCombine(N, DAG, DCI, Subtarget);
4238 case ISD::OR:
4239 return performORCombine(N, DAG, DCI, Subtarget);
4240 case ISD::SETCC:
4241 return performSETCCCombine(N, DAG, DCI, Subtarget);
4242 case ISD::SRL:
4243 return performSRLCombine(N, DAG, DCI, Subtarget);
4245 return performBITREV_WCombine(N, DAG, DCI, Subtarget);
4247 return performINTRINSIC_WO_CHAINCombine(N, DAG, DCI, Subtarget);
4248 }
4249 return SDValue();
4250}
4251
4254 if (!ZeroDivCheck)
4255 return MBB;
4256
4257 // Build instructions:
4258 // MBB:
4259 // div(or mod) $dst, $dividend, $divisor
4260 // bnez $divisor, SinkMBB
4261 // BreakMBB:
4262 // break 7 // BRK_DIVZERO
4263 // SinkMBB:
4264 // fallthrough
4265 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
4267 MachineFunction *MF = MBB->getParent();
4268 auto BreakMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4269 auto SinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4270 MF->insert(It, BreakMBB);
4271 MF->insert(It, SinkMBB);
4272
4273 // Transfer the remainder of MBB and its successor edges to SinkMBB.
4274 SinkMBB->splice(SinkMBB->end(), MBB, std::next(MI.getIterator()), MBB->end());
4275 SinkMBB->transferSuccessorsAndUpdatePHIs(