LLVM 24.0.0git
HexagonISelLoweringHVX.cpp
Go to the documentation of this file.
1//===-- HexagonISelLoweringHVX.cpp --- Lowering HVX operations ------------===//
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
10#include "HexagonRegisterInfo.h"
11#include "HexagonSubtarget.h"
12#include "llvm/ADT/SetVector.h"
21#include "llvm/IR/IntrinsicsHexagon.h"
23
24#include <algorithm>
25#include <string>
26#include <utility>
27
28using namespace llvm;
29
30static const MVT LegalV64[] = { MVT::v64i8, MVT::v32i16, MVT::v16i32 };
31static const MVT LegalW64[] = { MVT::v128i8, MVT::v64i16, MVT::v32i32 };
32static const MVT LegalV128[] = { MVT::v128i8, MVT::v64i16, MVT::v32i32 };
33static const MVT LegalW128[] = { MVT::v256i8, MVT::v128i16, MVT::v64i32 };
34
35static const unsigned MaxExpandMLA = 8;
36
37static std::tuple<unsigned, unsigned, unsigned> getIEEEProperties(MVT Ty) {
38 // For a float scalar type, return (exp-bits, exp-bias, fraction-bits)
39 MVT ElemTy = Ty.getScalarType();
40 switch (ElemTy.SimpleTy) {
41 case MVT::f16:
42 return std::make_tuple(5, 15, 10);
43 case MVT::f32:
44 return std::make_tuple(8, 127, 23);
45 case MVT::f64:
46 return std::make_tuple(11, 1023, 52);
47 default:
48 break;
49 }
50 llvm_unreachable(("Unexpected type: " + EVT(ElemTy).getEVTString()).c_str());
51}
52
53void
54HexagonTargetLowering::initializeHVXLowering() {
55 if (Subtarget.useHVX64BOps()) {
56 addRegisterClass(MVT::v64i8, &Hexagon::HvxVRRegClass);
57 addRegisterClass(MVT::v32i16, &Hexagon::HvxVRRegClass);
58 addRegisterClass(MVT::v16i32, &Hexagon::HvxVRRegClass);
59 addRegisterClass(MVT::v128i8, &Hexagon::HvxWRRegClass);
60 addRegisterClass(MVT::v64i16, &Hexagon::HvxWRRegClass);
61 addRegisterClass(MVT::v32i32, &Hexagon::HvxWRRegClass);
62 // These "short" boolean vector types should be legal because
63 // they will appear as results of vector compares. If they were
64 // not legal, type legalization would try to make them legal
65 // and that would require using operations that do not use or
66 // produce such types. That, in turn, would imply using custom
67 // nodes, which would be unoptimizable by the DAG combiner.
68 // The idea is to rely on target-independent operations as much
69 // as possible.
70 addRegisterClass(MVT::v16i1, &Hexagon::HvxQRRegClass);
71 addRegisterClass(MVT::v32i1, &Hexagon::HvxQRRegClass);
72 addRegisterClass(MVT::v64i1, &Hexagon::HvxQRRegClass);
73 } else if (Subtarget.useHVX128BOps()) {
74 addRegisterClass(MVT::v128i8, &Hexagon::HvxVRRegClass);
75 addRegisterClass(MVT::v64i16, &Hexagon::HvxVRRegClass);
76 addRegisterClass(MVT::v32i32, &Hexagon::HvxVRRegClass);
77 addRegisterClass(MVT::v256i8, &Hexagon::HvxWRRegClass);
78 addRegisterClass(MVT::v128i16, &Hexagon::HvxWRRegClass);
79 addRegisterClass(MVT::v64i32, &Hexagon::HvxWRRegClass);
80 addRegisterClass(MVT::v32i1, &Hexagon::HvxQRRegClass);
81 addRegisterClass(MVT::v64i1, &Hexagon::HvxQRRegClass);
82 addRegisterClass(MVT::v128i1, &Hexagon::HvxQRRegClass);
83 if (Subtarget.useHVXV68Ops() && Subtarget.useHVXFloatingPoint()) {
84 addRegisterClass(MVT::v32f32, &Hexagon::HvxVRRegClass);
85 addRegisterClass(MVT::v64f16, &Hexagon::HvxVRRegClass);
86 addRegisterClass(MVT::v64f32, &Hexagon::HvxWRRegClass);
87 addRegisterClass(MVT::v128f16, &Hexagon::HvxWRRegClass);
88 }
89 if (Subtarget.useHVXV81Ops()) {
90 addRegisterClass(MVT::v64bf16, &Hexagon::HvxVRRegClass);
91 addRegisterClass(MVT::v128bf16, &Hexagon::HvxWRRegClass);
92 }
93 }
94
95 // Set up operation actions.
96
97 bool Use64b = Subtarget.useHVX64BOps();
98 ArrayRef<MVT> LegalV = Use64b ? LegalV64 : LegalV128;
99 ArrayRef<MVT> LegalW = Use64b ? LegalW64 : LegalW128;
100 MVT ByteV = Use64b ? MVT::v64i8 : MVT::v128i8;
101 MVT WordV = Use64b ? MVT::v16i32 : MVT::v32i32;
102 MVT ByteW = Use64b ? MVT::v128i8 : MVT::v256i8;
103
104 auto setPromoteTo = [this] (unsigned Opc, MVT FromTy, MVT ToTy) {
106 AddPromotedToType(Opc, FromTy, ToTy);
107 };
108
109 // Handle bitcasts of vector predicates to scalars (e.g. v32i1 to i32).
110 // Note: v16i1 -> i16 is handled in type legalization instead of op
111 // legalization.
121
122 if (Subtarget.useHVX128BOps()) {
126 setOperationAction(ISD::LOAD, MVT::v32i1, Custom);
128 setOperationAction(ISD::LOAD, MVT::v64i1, Custom);
129 setOperationAction(ISD::STORE, MVT::v128i1, Custom);
130 setOperationAction(ISD::LOAD, MVT::v128i1, Custom);
131 }
132 if (Subtarget.useHVX128BOps() && Subtarget.useHVXV68Ops() &&
133 Subtarget.useHVXFloatingPoint()) {
134
135 static const MVT FloatV[] = { MVT::v64f16, MVT::v32f32 };
136 static const MVT FloatW[] = { MVT::v128f16, MVT::v64f32 };
137
138 for (MVT T : FloatV) {
152
155
158
161 // Custom-lower BUILD_VECTOR. The standard (target-independent)
162 // handling of it would convert it to a load, which is not always
163 // the optimal choice.
165 }
166
167
168 // BUILD_VECTOR with f16 operands cannot be promoted without
169 // promoting the result, so lower the node to vsplat or constant pool
172
173 // Vector shuffle is always promoted to ByteV and a bitcast to f16 is
174 // generated.
175 setPromoteTo(ISD::VECTOR_SHUFFLE, MVT::v128f16, ByteW);
176 setPromoteTo(ISD::VECTOR_SHUFFLE, MVT::v64f16, ByteV);
177 setPromoteTo(ISD::VECTOR_SHUFFLE, MVT::v64f32, ByteW);
178 setPromoteTo(ISD::VECTOR_SHUFFLE, MVT::v32f32, ByteV);
179
180 // For HVX <v81 there is no hardware float-equality instruction; only
181 // float-GT (V6_vgtsf/V6_vgthf) is available. The integer-equality
182 // fallback (V6_veqw/V6_veqh) silently treats NaN as equal to itself
183 // because the bit patterns match. Mark SETCC as Custom for the
184 // single-vector float types so we can synthesise the correct
185 // ordered-equal predicate in LowerHvxFpSetoeq.
186 if (!Subtarget.useHVXV81Ops())
187 for (MVT T : FloatV)
189
190 if (Subtarget.useHVXV81Ops()) {
191 setPromoteTo(ISD::VECTOR_SHUFFLE, MVT::v128bf16, ByteW);
192 setPromoteTo(ISD::VECTOR_SHUFFLE, MVT::v64bf16, ByteV);
193 setPromoteTo(ISD::SETCC, MVT::v64bf16, MVT::v64f32);
194 setPromoteTo(ISD::FADD, MVT::v64bf16, MVT::v64f32);
195 setPromoteTo(ISD::FSUB, MVT::v64bf16, MVT::v64f32);
196 setPromoteTo(ISD::FMUL, MVT::v64bf16, MVT::v64f32);
197 setPromoteTo(ISD::FMINNUM, MVT::v64bf16, MVT::v64f32);
198 setPromoteTo(ISD::FMAXNUM, MVT::v64bf16, MVT::v64f32);
199
203
204 setOperationAction(ISD::LOAD, MVT::v128bf16, Custom);
205 setOperationAction(ISD::STORE, MVT::v128bf16, Custom);
206
207 setOperationAction(ISD::MLOAD, MVT::v64bf16, Custom);
208 setOperationAction(ISD::MSTORE, MVT::v64bf16, Custom);
211
212 setOperationAction(ISD::MLOAD, MVT::v128bf16, Custom);
213 setOperationAction(ISD::MSTORE, MVT::v128bf16, Custom);
216
219 }
220
221 for (MVT P : FloatW) {
239
240 // Custom-lower BUILD_VECTOR. The standard (target-independent)
241 // handling of it would convert it to a load, which is not always
242 // the optimal choice.
244 // Make concat-vectors custom to handle concats of more than 2 vectors.
246
249 }
250
251 if (Subtarget.useHVXQFloatOps()) {
254 } else if (Subtarget.useHVXIEEEFPOps()) {
257 }
258 }
259
260 for (MVT T : LegalV) {
263
279 if (T != ByteV) {
283 }
284
287 if (T.getScalarType() != MVT::i32) {
290 }
291
296 if (T.getScalarType() != MVT::i32) {
299 }
300
302 // Make concat-vectors custom to handle concats of more than 2 vectors.
313 if (T != ByteV) {
315 // HVX only has shifts of words and halfwords.
319
320 // Promote all shuffles to operate on vectors of bytes.
321 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteV);
322 }
323
324 if (Subtarget.useHVXFloatingPoint()) {
325 // Same action for both QFloat and IEEE.
330 }
331
339 }
340
341 for (MVT T : LegalW) {
342 // Custom-lower BUILD_VECTOR for vector pairs. The standard (target-
343 // independent) handling of it would convert it to a load, which is
344 // not always the optimal choice.
346 // Make concat-vectors custom to handle concats of more than 2 vectors.
348
349 // Custom-lower these operations for pairs. Expand them into a concat
350 // of the corresponding operations on individual vectors.
359
368
383 if (T != ByteW) {
387
388 // Promote all shuffles to operate on vectors of bytes.
389 setPromoteTo(ISD::VECTOR_SHUFFLE, T, ByteW);
390 }
393
396 if (T.getScalarType() != MVT::i32) {
399 }
400
401 if (Subtarget.useHVXFloatingPoint()) {
402 // Same action for both QFloat and IEEE.
407 }
408 }
409
410 // Legalize all of these to HexagonISD::[SU]MUL_LOHI.
411 setOperationAction(ISD::MULHS, WordV, Custom); // -> _LOHI
412 setOperationAction(ISD::MULHU, WordV, Custom); // -> _LOHI
415
416 setCondCodeAction(ISD::SETNE, MVT::v64f16, Expand);
417 setCondCodeAction(ISD::SETLE, MVT::v64f16, Expand);
418 setCondCodeAction(ISD::SETGE, MVT::v64f16, Expand);
419 setCondCodeAction(ISD::SETLT, MVT::v64f16, Expand);
420 setCondCodeAction(ISD::SETONE, MVT::v64f16, Expand);
421 setCondCodeAction(ISD::SETOLE, MVT::v64f16, Expand);
422 setCondCodeAction(ISD::SETOGE, MVT::v64f16, Expand);
423 setCondCodeAction(ISD::SETOLT, MVT::v64f16, Expand);
424 setCondCodeAction(ISD::SETUNE, MVT::v64f16, Expand);
425 setCondCodeAction(ISD::SETULE, MVT::v64f16, Expand);
426 setCondCodeAction(ISD::SETUGE, MVT::v64f16, Expand);
427 setCondCodeAction(ISD::SETULT, MVT::v64f16, Expand);
428 setCondCodeAction(ISD::SETUO, MVT::v64f16, Expand);
429 setCondCodeAction(ISD::SETO, MVT::v64f16, Expand);
430
431 setCondCodeAction(ISD::SETNE, MVT::v32f32, Expand);
432 setCondCodeAction(ISD::SETLE, MVT::v32f32, Expand);
433 setCondCodeAction(ISD::SETGE, MVT::v32f32, Expand);
434 setCondCodeAction(ISD::SETLT, MVT::v32f32, Expand);
435 setCondCodeAction(ISD::SETONE, MVT::v32f32, Expand);
436 setCondCodeAction(ISD::SETOLE, MVT::v32f32, Expand);
437 setCondCodeAction(ISD::SETOGE, MVT::v32f32, Expand);
438 setCondCodeAction(ISD::SETOLT, MVT::v32f32, Expand);
439 setCondCodeAction(ISD::SETUNE, MVT::v32f32, Expand);
440 setCondCodeAction(ISD::SETULE, MVT::v32f32, Expand);
441 setCondCodeAction(ISD::SETUGE, MVT::v32f32, Expand);
442 setCondCodeAction(ISD::SETULT, MVT::v32f32, Expand);
443 setCondCodeAction(ISD::SETUO, MVT::v32f32, Expand);
444 setCondCodeAction(ISD::SETO, MVT::v32f32, Expand);
445
446 // Boolean vectors.
447
448 for (MVT T : LegalW) {
449 // Boolean types for vector pairs will overlap with the boolean
450 // types for single vectors, e.g.
451 // v64i8 -> v64i1 (single)
452 // v64i16 -> v64i1 (pair)
453 // Set these actions first, and allow the single actions to overwrite
454 // any duplicates.
455 MVT BoolW = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
460 // Masked load/store takes a mask that may need splitting.
463 }
464
465 for (MVT T : LegalV) {
466 MVT BoolV = MVT::getVectorVT(MVT::i1, T.getVectorNumElements());
477 }
478
479 if (Use64b) {
480 for (MVT T: {MVT::v32i8, MVT::v32i16, MVT::v16i8, MVT::v16i16, MVT::v16i32})
482 } else {
483 for (MVT T: {MVT::v64i8, MVT::v64i16, MVT::v32i8, MVT::v32i16, MVT::v32i32})
485 }
486
487 // Handle store widening for short vectors.
488 unsigned HwLen = Subtarget.getVectorLength();
489 for (MVT ElemTy : Subtarget.getHVXElementTypes()) {
490 if (ElemTy == MVT::i1)
491 continue;
492 int ElemWidth = ElemTy.getFixedSizeInBits();
493 int MaxElems = (8*HwLen) / ElemWidth;
494 for (int N = 2; N < MaxElems; N *= 2) {
495 MVT VecTy = MVT::getVectorVT(ElemTy, N);
496 auto Action = getPreferredVectorAction(VecTy);
505 if (Subtarget.useHVXFloatingPoint()) {
510 }
511
512 MVT BoolTy = MVT::getVectorVT(MVT::i1, N);
513 if (!isTypeLegal(BoolTy))
515 }
516 }
517 }
518
519 // Include cases which are not hander earlier
523
525
528
529 // Partial MLA reductions.
530 {
531 static const unsigned MLAOps[] = {ISD::PARTIAL_REDUCE_SMLA,
534
535 auto HvxType = [=](MVT ScalarT, unsigned Factor = 1) {
536 return MVT::getVectorVT(ScalarT, Subtarget.getVectorLength() * Factor *
537 8 / ScalarT.getSizeInBits());
538 };
539
540 // Tuple of (Acc element type, input element type, vector pair).
541 // The assumption is both the input and reduction result are of the same
542 // size so the reduction ratio is the same as the ratio of element type
543 // sizes. This may not hold for all available instructions.
544 typedef std::tuple<MVT, MVT, bool> ReductionSignature;
545
546 static const std::vector<ReductionSignature> NativeReductions = {
547 {MVT::i32, MVT::i8, false},
548 };
549
550 for (const auto &R : NativeReductions) {
551
552 MVT AccType = std::get<0>(R);
553 MVT InputType = std::get<1>(R);
554 unsigned Factor = std::get<2>(R) ? 2 : 1;
555
556 // The native size is legal.
557 setPartialReduceMLAAction(MLAOps, HvxType(AccType), HvxType(InputType),
558 Legal);
559
560 // Allow custom partial MLA reductions on larger vectors than legally
561 // supported. These reduction must be declared as Custom (or Legal)
562 // for foldPartialReduceMLAMulOp() to fold the multiply by one pattern
563 // inserted when the partial reduction intrinsic is converted to
564 // PARTIAL_REDUCE_U/S/SUMLA. Otherwise, the Split action will apply
565 // on the original pattern, including the extensions and multiplies,
566 // which will make it impossible to match.
567 // There are two independent ways to extend the
568 // input size: 1. to concatenate the result - output vector is
569 // proportionally extended, 2) to reduce the result - the output vector
570 // size stays the same. We limit allowed combinations so that the total
571 // number of generated reduction instructions is limited by a constant
572 // number. This limit is arbitrary and can be revised. On one hand, it is
573 // convenient to have more choices; on the other hand, there is a
574 // diminishing benefit of very long sequences, which should probably be
575 // written as loops instead.
576 for (unsigned ConcatFactor = 1; ConcatFactor <= MaxExpandMLA;
577 ConcatFactor <<= 1)
578 for (unsigned ReductionFactor = 1; ReductionFactor <= MaxExpandMLA;
579 ReductionFactor <<= 1)
580 if (ConcatFactor * ReductionFactor != 1 &&
581 ConcatFactor * ReductionFactor <= MaxExpandMLA)
583 MLAOps, HvxType(AccType, Factor * ConcatFactor),
584 HvxType(InputType, Factor * ConcatFactor * ReductionFactor),
585 Custom);
586 }
587 }
588}
589
590unsigned
591HexagonTargetLowering::getPreferredHvxVectorAction(MVT VecTy) const {
592 // Early exit for invalid input types
593 if (!VecTy.isVector())
594 return ~0u;
595
596 MVT ElemTy = VecTy.getVectorElementType();
597 unsigned VecLen = VecTy.getVectorNumElements();
598 unsigned HwLen = Subtarget.getVectorLength();
599
600 // Split vectors of i1 that exceed byte vector length.
601 if (ElemTy == MVT::i1 && VecLen > HwLen)
603
604 ArrayRef<MVT> Tys = Subtarget.getHVXElementTypes();
605 // For shorter vectors of i1, widen them if any of the corresponding
606 // vectors of integers needs to be widened.
607 if (ElemTy == MVT::i1) {
608 for (MVT T : Tys) {
609 assert(T != MVT::i1);
610 auto A = getPreferredHvxVectorAction(MVT::getVectorVT(T, VecLen));
611 if (A != ~0u)
612 return A;
613 }
614 return ~0u;
615 }
616
617 // If the size of VecTy is at least half of the vector length,
618 // widen the vector. Note: the threshold was not selected in
619 // any scientific way.
620 if (llvm::is_contained(Tys, ElemTy)) {
621 unsigned VecWidth = VecTy.getSizeInBits();
622 unsigned HwWidth = 8*HwLen;
623 if (VecWidth > 2*HwWidth)
625
626 bool HaveThreshold = Subtarget.getCLOpts().hvx_widen.has_value();
627 if (HaveThreshold && 8 * *Subtarget.getCLOpts().hvx_widen <= VecWidth)
629 if (VecWidth >= HwWidth/2 && VecWidth < HwWidth)
631 }
632
633 // Defer to default.
634 return ~0u;
635}
636
637unsigned
638HexagonTargetLowering::getCustomHvxOperationAction(SDNode &Op) const {
639 unsigned Opc = Op.getOpcode();
640 switch (Opc) {
641 case HexagonISD::SMUL_LOHI:
642 case HexagonISD::UMUL_LOHI:
643 case HexagonISD::USMUL_LOHI:
645 }
647}
648
650HexagonTargetLowering::getInt(unsigned IntId, MVT ResTy, ArrayRef<SDValue> Ops,
651 const SDLoc &dl, SelectionDAG &DAG) const {
653 IntOps.push_back(DAG.getConstant(IntId, dl, MVT::i32));
654 append_range(IntOps, Ops);
655 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, ResTy, IntOps);
656}
657
658MVT
659HexagonTargetLowering::typeJoin(const TypePair &Tys) const {
660 assert(Tys.first.getVectorElementType() == Tys.second.getVectorElementType());
661
662 MVT ElemTy = Tys.first.getVectorElementType();
663 return MVT::getVectorVT(ElemTy, Tys.first.getVectorNumElements() +
664 Tys.second.getVectorNumElements());
665}
666
667HexagonTargetLowering::TypePair
668HexagonTargetLowering::typeSplit(MVT VecTy) const {
669 assert(VecTy.isVector());
670 unsigned NumElem = VecTy.getVectorNumElements();
671 assert((NumElem % 2) == 0 && "Expecting even-sized vector type");
672 MVT HalfTy = MVT::getVectorVT(VecTy.getVectorElementType(), NumElem/2);
673 return { HalfTy, HalfTy };
674}
675
676MVT
677HexagonTargetLowering::typeExtElem(MVT VecTy, unsigned Factor) const {
678 MVT ElemTy = VecTy.getVectorElementType();
679 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() * Factor);
680 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
681}
682
683MVT
684HexagonTargetLowering::typeTruncElem(MVT VecTy, unsigned Factor) const {
685 MVT ElemTy = VecTy.getVectorElementType();
686 MVT NewElemTy = MVT::getIntegerVT(ElemTy.getSizeInBits() / Factor);
687 return MVT::getVectorVT(NewElemTy, VecTy.getVectorNumElements());
688}
689
691HexagonTargetLowering::opCastElem(SDValue Vec, MVT ElemTy,
692 SelectionDAG &DAG) const {
693 if (ty(Vec).getVectorElementType() == ElemTy)
694 return Vec;
695 MVT CastTy = tyVector(Vec.getValueType().getSimpleVT(), ElemTy);
696 return DAG.getBitcast(CastTy, Vec);
697}
698
700HexagonTargetLowering::opJoin(const VectorPair &Ops, const SDLoc &dl,
701 SelectionDAG &DAG) const {
702 return DAG.getNode(ISD::CONCAT_VECTORS, dl, typeJoin(ty(Ops)),
703 Ops.first, Ops.second);
704}
705
706HexagonTargetLowering::VectorPair
707HexagonTargetLowering::opSplit(SDValue Vec, const SDLoc &dl,
708 SelectionDAG &DAG) const {
709 TypePair Tys = typeSplit(ty(Vec));
710 if (Vec.getOpcode() == HexagonISD::QCAT)
711 return VectorPair(Vec.getOperand(0), Vec.getOperand(1));
712 return DAG.SplitVector(Vec, dl, Tys.first, Tys.second);
713}
714
715bool
716HexagonTargetLowering::isHvxSingleTy(MVT Ty) const {
717 return Subtarget.isHVXVectorType(Ty) &&
718 Ty.getSizeInBits() == 8 * Subtarget.getVectorLength();
719}
720
721bool
722HexagonTargetLowering::isHvxPairTy(MVT Ty) const {
723 return Subtarget.isHVXVectorType(Ty) &&
724 Ty.getSizeInBits() == 16 * Subtarget.getVectorLength();
725}
726
727bool
728HexagonTargetLowering::isHvxBoolTy(MVT Ty) const {
729 return Subtarget.isHVXVectorType(Ty, true) &&
730 Ty.getVectorElementType() == MVT::i1;
731}
732
733bool HexagonTargetLowering::allowsHvxMemoryAccess(
734 MVT VecTy, MachineMemOperand::Flags Flags, unsigned *Fast) const {
735 // Bool vectors are excluded by default, but make it explicit to
736 // emphasize that bool vectors cannot be loaded or stored.
737 // Also, disallow double vector stores (to prevent unnecessary
738 // store widening in DAG combiner).
739 if (VecTy.getSizeInBits() > 8*Subtarget.getVectorLength())
740 return false;
741 if (!Subtarget.isHVXVectorType(VecTy, /*IncludeBool=*/false))
742 return false;
743 if (Fast)
744 *Fast = 1;
745 return true;
746}
747
748bool HexagonTargetLowering::allowsHvxMisalignedMemoryAccesses(
749 MVT VecTy, MachineMemOperand::Flags Flags, unsigned *Fast) const {
750 if (!Subtarget.isHVXVectorType(VecTy))
751 return false;
752 // XXX Should this be false? vmemu are a bit slower than vmem.
753 if (Fast)
754 *Fast = 1;
755 return true;
756}
757
758void HexagonTargetLowering::AdjustHvxInstrPostInstrSelection(
759 MachineInstr &MI, SDNode *Node) const {
760 unsigned Opc = MI.getOpcode();
761 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
762 MachineBasicBlock &MB = *MI.getParent();
763 MachineFunction &MF = *MB.getParent();
764 MachineRegisterInfo &MRI = MF.getRegInfo();
765 DebugLoc DL = MI.getDebugLoc();
766 auto At = MI.getIterator();
767
768 switch (Opc) {
769 case Hexagon::PS_vsplatib:
770 if (Subtarget.useHVXV62Ops()) {
771 // SplatV = A2_tfrsi #imm
772 // OutV = V6_lvsplatb SplatV
773 Register SplatV = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
774 BuildMI(MB, At, DL, TII.get(Hexagon::A2_tfrsi), SplatV)
775 .add(MI.getOperand(1));
776 Register OutV = MI.getOperand(0).getReg();
777 BuildMI(MB, At, DL, TII.get(Hexagon::V6_lvsplatb), OutV)
778 .addReg(SplatV);
779 } else {
780 // SplatV = A2_tfrsi #imm:#imm:#imm:#imm
781 // OutV = V6_lvsplatw SplatV
782 Register SplatV = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
783 const MachineOperand &InpOp = MI.getOperand(1);
784 assert(InpOp.isImm());
785 uint32_t V = InpOp.getImm() & 0xFF;
786 BuildMI(MB, At, DL, TII.get(Hexagon::A2_tfrsi), SplatV)
787 .addImm(V << 24 | V << 16 | V << 8 | V);
788 Register OutV = MI.getOperand(0).getReg();
789 BuildMI(MB, At, DL, TII.get(Hexagon::V6_lvsplatw), OutV).addReg(SplatV);
790 }
791 MB.erase(At);
792 break;
793 case Hexagon::PS_vsplatrb:
794 if (Subtarget.useHVXV62Ops()) {
795 // OutV = V6_lvsplatb Inp
796 Register OutV = MI.getOperand(0).getReg();
797 BuildMI(MB, At, DL, TII.get(Hexagon::V6_lvsplatb), OutV)
798 .add(MI.getOperand(1));
799 } else {
800 Register SplatV = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
801 const MachineOperand &InpOp = MI.getOperand(1);
802 BuildMI(MB, At, DL, TII.get(Hexagon::S2_vsplatrb), SplatV)
803 .addReg(InpOp.getReg(), {}, InpOp.getSubReg());
804 Register OutV = MI.getOperand(0).getReg();
805 BuildMI(MB, At, DL, TII.get(Hexagon::V6_lvsplatw), OutV)
806 .addReg(SplatV);
807 }
808 MB.erase(At);
809 break;
810 case Hexagon::PS_vsplatih:
811 if (Subtarget.useHVXV62Ops()) {
812 // SplatV = A2_tfrsi #imm
813 // OutV = V6_lvsplath SplatV
814 Register SplatV = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
815 BuildMI(MB, At, DL, TII.get(Hexagon::A2_tfrsi), SplatV)
816 .add(MI.getOperand(1));
817 Register OutV = MI.getOperand(0).getReg();
818 BuildMI(MB, At, DL, TII.get(Hexagon::V6_lvsplath), OutV)
819 .addReg(SplatV);
820 } else {
821 // SplatV = A2_tfrsi #imm:#imm
822 // OutV = V6_lvsplatw SplatV
823 Register SplatV = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
824 const MachineOperand &InpOp = MI.getOperand(1);
825 assert(InpOp.isImm());
826 uint32_t V = InpOp.getImm() & 0xFFFF;
827 BuildMI(MB, At, DL, TII.get(Hexagon::A2_tfrsi), SplatV)
828 .addImm(V << 16 | V);
829 Register OutV = MI.getOperand(0).getReg();
830 BuildMI(MB, At, DL, TII.get(Hexagon::V6_lvsplatw), OutV).addReg(SplatV);
831 }
832 MB.erase(At);
833 break;
834 case Hexagon::PS_vsplatrh:
835 if (Subtarget.useHVXV62Ops()) {
836 // OutV = V6_lvsplath Inp
837 Register OutV = MI.getOperand(0).getReg();
838 BuildMI(MB, At, DL, TII.get(Hexagon::V6_lvsplath), OutV)
839 .add(MI.getOperand(1));
840 } else {
841 // SplatV = A2_combine_ll Inp, Inp
842 // OutV = V6_lvsplatw SplatV
843 Register SplatV = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
844 const MachineOperand &InpOp = MI.getOperand(1);
845 BuildMI(MB, At, DL, TII.get(Hexagon::A2_combine_ll), SplatV)
846 .addReg(InpOp.getReg(), {}, InpOp.getSubReg())
847 .addReg(InpOp.getReg(), {}, InpOp.getSubReg());
848 Register OutV = MI.getOperand(0).getReg();
849 BuildMI(MB, At, DL, TII.get(Hexagon::V6_lvsplatw), OutV).addReg(SplatV);
850 }
851 MB.erase(At);
852 break;
853 case Hexagon::PS_vsplatiw:
854 case Hexagon::PS_vsplatrw:
855 if (Opc == Hexagon::PS_vsplatiw) {
856 // SplatV = A2_tfrsi #imm
857 Register SplatV = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
858 BuildMI(MB, At, DL, TII.get(Hexagon::A2_tfrsi), SplatV)
859 .add(MI.getOperand(1));
860 MI.getOperand(1).ChangeToRegister(SplatV, false);
861 }
862 // OutV = V6_lvsplatw SplatV/Inp
863 MI.setDesc(TII.get(Hexagon::V6_lvsplatw));
864 break;
865 }
866}
867
869HexagonTargetLowering::convertToByteIndex(SDValue ElemIdx, MVT ElemTy,
870 SelectionDAG &DAG) const {
871 if (ElemIdx.getValueType().getSimpleVT() != MVT::i32)
872 ElemIdx = DAG.getBitcast(MVT::i32, ElemIdx);
873
874 unsigned ElemWidth = ElemTy.getSizeInBits();
875 if (ElemWidth == 8)
876 return ElemIdx;
877
878 unsigned L = Log2_32(ElemWidth/8);
879 const SDLoc &dl(ElemIdx);
880 return DAG.getNode(ISD::SHL, dl, MVT::i32,
881 {ElemIdx, DAG.getConstant(L, dl, MVT::i32)});
882}
883
885HexagonTargetLowering::getIndexInWord32(SDValue Idx, MVT ElemTy,
886 SelectionDAG &DAG) const {
887 unsigned ElemWidth = ElemTy.getSizeInBits();
888 assert(ElemWidth >= 8 && ElemWidth <= 32);
889 if (ElemWidth == 32)
890 return Idx;
891
892 if (ty(Idx) != MVT::i32)
893 Idx = DAG.getBitcast(MVT::i32, Idx);
894 const SDLoc &dl(Idx);
895 SDValue Mask = DAG.getConstant(32/ElemWidth - 1, dl, MVT::i32);
896 SDValue SubIdx = DAG.getNode(ISD::AND, dl, MVT::i32, {Idx, Mask});
897 return SubIdx;
898}
899
901HexagonTargetLowering::getByteShuffle(const SDLoc &dl, SDValue Op0,
902 SDValue Op1, ArrayRef<int> Mask,
903 SelectionDAG &DAG) const {
904 MVT OpTy = ty(Op0);
905 assert(OpTy == ty(Op1));
906
907 MVT ElemTy = OpTy.getVectorElementType();
908 if (ElemTy == MVT::i8)
909 return DAG.getVectorShuffle(OpTy, dl, Op0, Op1, Mask);
910 assert(ElemTy.getSizeInBits() >= 8);
911
912 MVT ResTy = tyVector(OpTy, MVT::i8);
913 unsigned ElemSize = ElemTy.getSizeInBits() / 8;
914
915 SmallVector<int,128> ByteMask;
916 for (int M : Mask) {
917 if (M < 0) {
918 for (unsigned I = 0; I != ElemSize; ++I)
919 ByteMask.push_back(-1);
920 } else {
921 int NewM = M*ElemSize;
922 for (unsigned I = 0; I != ElemSize; ++I)
923 ByteMask.push_back(NewM+I);
924 }
925 }
926 assert(ResTy.getVectorNumElements() == ByteMask.size());
927 return DAG.getVectorShuffle(ResTy, dl, opCastElem(Op0, MVT::i8, DAG),
928 opCastElem(Op1, MVT::i8, DAG), ByteMask);
929}
930
932HexagonTargetLowering::buildHvxVectorReg(ArrayRef<SDValue> Values,
933 const SDLoc &dl, MVT VecTy,
934 SelectionDAG &DAG) const {
935 unsigned VecLen = Values.size();
937 MVT ElemTy = VecTy.getVectorElementType();
938 unsigned ElemWidth = ElemTy.getSizeInBits();
939 unsigned HwLen = Subtarget.getVectorLength();
940
941 unsigned ElemSize = ElemWidth / 8;
942 assert(ElemSize*VecLen == HwLen);
944
945 if (VecTy.getVectorElementType() != MVT::i32 &&
946 !(Subtarget.useHVXFloatingPoint() &&
947 VecTy.getVectorElementType() == MVT::f32)) {
948 assert((ElemSize == 1 || ElemSize == 2) && "Invalid element size");
949 unsigned OpsPerWord = (ElemSize == 1) ? 4 : 2;
950 MVT PartVT = MVT::getVectorVT(VecTy.getVectorElementType(), OpsPerWord);
951 for (unsigned i = 0; i != VecLen; i += OpsPerWord) {
952 SDValue W = buildVector32(Values.slice(i, OpsPerWord), dl, PartVT, DAG);
953 Words.push_back(DAG.getBitcast(MVT::i32, W));
954 }
955 } else {
956 for (SDValue V : Values)
957 Words.push_back(DAG.getBitcast(MVT::i32, V));
958 }
959 auto isSplat = [] (ArrayRef<SDValue> Values, SDValue &SplatV) {
960 unsigned NumValues = Values.size();
961 assert(NumValues > 0);
962 bool IsUndef = true;
963 for (unsigned i = 0; i != NumValues; ++i) {
964 if (Values[i].isUndef())
965 continue;
966 IsUndef = false;
967 if (!SplatV.getNode())
968 SplatV = Values[i];
969 else if (SplatV != Values[i])
970 return false;
971 }
972 if (IsUndef)
973 SplatV = Values[0];
974 return true;
975 };
976
977 unsigned NumWords = Words.size();
978 SDValue SplatV;
979 bool IsSplat = isSplat(Words, SplatV);
980 if (IsSplat && isUndef(SplatV))
981 return DAG.getUNDEF(VecTy);
982 if (IsSplat) {
983 assert(SplatV.getNode());
984 if (isNullConstant(SplatV))
985 return getZero(dl, VecTy, DAG);
986 MVT WordTy = MVT::getVectorVT(MVT::i32, HwLen/4);
987 SDValue S = DAG.getNode(ISD::SPLAT_VECTOR, dl, WordTy, SplatV);
988 return DAG.getBitcast(VecTy, S);
989 }
990
991 // Delay recognizing constant vectors until here, so that we can generate
992 // a vsplat.
993 SmallVector<ConstantInt*, 128> Consts(VecLen);
994 bool AllConst = getBuildVectorConstInts(Values, VecTy, DAG, Consts);
995 if (AllConst) {
996 ArrayRef<Constant*> Tmp((Constant**)Consts.begin(),
997 (Constant**)Consts.end());
998 Constant *CV = ConstantVector::get(Tmp);
999 Align Alignment(HwLen);
1001 DAG.getConstantPool(CV, getPointerTy(DAG.getDataLayout()), Alignment),
1002 DAG);
1003 return DAG.getLoad(VecTy, dl, DAG.getEntryNode(), CP,
1005 }
1006
1007 // A special case is a situation where the vector is built entirely from
1008 // elements extracted from another vector. This could be done via a shuffle
1009 // more efficiently, but typically, the size of the source vector will not
1010 // match the size of the vector being built (which precludes the use of a
1011 // shuffle directly).
1012 // This only handles a single source vector, and the vector being built
1013 // should be of a sub-vector type of the source vector type.
1014 auto IsBuildFromExtracts = [this,&Values] (SDValue &SrcVec,
1015 SmallVectorImpl<int> &SrcIdx) {
1016 SDValue Vec;
1017 for (SDValue V : Values) {
1018 if (isUndef(V)) {
1019 SrcIdx.push_back(-1);
1020 continue;
1021 }
1022 if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
1023 return false;
1024 // All extracts should come from the same vector.
1025 SDValue T = V.getOperand(0);
1026 if (Vec.getNode() != nullptr && T.getNode() != Vec.getNode())
1027 return false;
1028 Vec = T;
1029 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V.getOperand(1));
1030 if (C == nullptr)
1031 return false;
1032 int I = C->getSExtValue();
1033 assert(I >= 0 && "Negative element index");
1034 SrcIdx.push_back(I);
1035 }
1036 SrcVec = Vec;
1037 return true;
1038 };
1039
1040 SmallVector<int,128> ExtIdx;
1041 SDValue ExtVec;
1042 if (IsBuildFromExtracts(ExtVec, ExtIdx)) {
1043 MVT ExtTy = ty(ExtVec);
1044 unsigned ExtLen = ExtTy.getVectorNumElements();
1045 if (ExtLen == VecLen || ExtLen == 2*VecLen) {
1046 // Construct a new shuffle mask that will produce a vector with the same
1047 // number of elements as the input vector, and such that the vector we
1048 // want will be the initial subvector of it.
1049 SmallVector<int,128> Mask;
1050 BitVector Used(ExtLen);
1051
1052 for (int M : ExtIdx) {
1053 Mask.push_back(M);
1054 if (M >= 0)
1055 Used.set(M);
1056 }
1057 // Fill the rest of the mask with the unused elements of ExtVec in hopes
1058 // that it will result in a permutation of ExtVec's elements. It's still
1059 // fine if it doesn't (e.g. if undefs are present, or elements are
1060 // repeated), but permutations can always be done efficiently via vdelta
1061 // and vrdelta.
1062 for (unsigned I = 0; I != ExtLen; ++I) {
1063 if (Mask.size() == ExtLen)
1064 break;
1065 if (!Used.test(I))
1066 Mask.push_back(I);
1067 }
1068
1069 SDValue S = DAG.getVectorShuffle(ExtTy, dl, ExtVec,
1070 DAG.getUNDEF(ExtTy), Mask);
1071 return ExtLen == VecLen ? S : LoHalf(S, DAG);
1072 }
1073 }
1074
1075 // Find most common element to initialize vector with. This is to avoid
1076 // unnecessary vinsert/valign for cases where the same value is present
1077 // many times. Creates a histogram of the vector's elements to find the
1078 // most common element n.
1079 assert(4*Words.size() == Subtarget.getVectorLength());
1080 int VecHist[32];
1081 int n = 0;
1082 for (unsigned i = 0; i != NumWords; ++i) {
1083 VecHist[i] = 0;
1084 if (Words[i].isUndef())
1085 continue;
1086 for (unsigned j = i; j != NumWords; ++j)
1087 if (Words[i] == Words[j])
1088 VecHist[i]++;
1089
1090 if (VecHist[i] > VecHist[n])
1091 n = i;
1092 }
1093
1094 SDValue HalfV = getZero(dl, VecTy, DAG);
1095 if (VecHist[n] > 1) {
1096 // Always splat at word (i32) granularity so that the SPLAT_VECTOR node
1097 // is selected as PS_vsplatrw (word broadcast) rather than PS_vsplatrb
1098 // (byte broadcast of the low byte only), which would corrupt multi-byte
1099 // element types.
1100 MVT WordVecTy = MVT::getVectorVT(MVT::i32, HwLen / 4);
1101 SDValue WordSplat = DAG.getNode(ISD::SPLAT_VECTOR, dl, WordVecTy, Words[n]);
1102 SDValue SplatV = DAG.getBitcast(VecTy, WordSplat);
1103 HalfV = DAG.getNode(HexagonISD::VALIGN, dl, VecTy,
1104 {HalfV, SplatV, DAG.getConstant(HwLen/2, dl, MVT::i32)});
1105 }
1106 SDValue HalfV0 = HalfV;
1107 SDValue HalfV1 = HalfV;
1108
1109 // Construct two halves in parallel, then or them together. Rn and Rm count
1110 // number of rotations needed before the next element. One last rotation is
1111 // performed post-loop to position the last element.
1112 int Rn = 0, Rm = 0;
1113 SDValue Sn, Sm;
1114 SDValue N = HalfV0;
1115 SDValue M = HalfV1;
1116 for (unsigned i = 0; i != NumWords/2; ++i) {
1117 // Rotate by element count since last insertion.
1118 if (Words[i] != Words[n] || VecHist[n] <= 1) {
1119 Sn = DAG.getConstant(Rn, dl, MVT::i32);
1120 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {N, Sn});
1121 N = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
1122 {HalfV0, Words[i]});
1123 Rn = 0;
1124 }
1125 if (Words[i+NumWords/2] != Words[n] || VecHist[n] <= 1) {
1126 Sm = DAG.getConstant(Rm, dl, MVT::i32);
1127 HalfV1 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {M, Sm});
1128 M = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy,
1129 {HalfV1, Words[i+NumWords/2]});
1130 Rm = 0;
1131 }
1132 Rn += 4;
1133 Rm += 4;
1134 }
1135 // Perform last rotation.
1136 Sn = DAG.getConstant(Rn+HwLen/2, dl, MVT::i32);
1137 Sm = DAG.getConstant(Rm, dl, MVT::i32);
1138 HalfV0 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {N, Sn});
1139 HalfV1 = DAG.getNode(HexagonISD::VROR, dl, VecTy, {M, Sm});
1140
1141 SDValue T0 = DAG.getBitcast(tyVector(VecTy, MVT::i32), HalfV0);
1142 SDValue T1 = DAG.getBitcast(tyVector(VecTy, MVT::i32), HalfV1);
1143
1144 SDValue DstV = DAG.getNode(ISD::OR, dl, ty(T0), {T0, T1});
1145
1146 SDValue OutV =
1147 DAG.getBitcast(tyVector(ty(DstV), VecTy.getVectorElementType()), DstV);
1148 return OutV;
1149}
1150
1151SDValue
1152HexagonTargetLowering::createHvxPrefixPred(SDValue PredV, const SDLoc &dl,
1153 unsigned BitBytes, bool ZeroFill, SelectionDAG &DAG) const {
1154 MVT PredTy = ty(PredV);
1155 unsigned HwLen = Subtarget.getVectorLength();
1156 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1157
1158 if (Subtarget.isHVXVectorType(PredTy, true)) {
1159 // Move the vector predicate SubV to a vector register, and scale it
1160 // down to match the representation (bytes per type element) that VecV
1161 // uses. The scaling down will pick every 2nd or 4th (every Scale-th
1162 // in general) element and put them at the front of the resulting
1163 // vector. This subvector will then be inserted into the Q2V of VecV.
1164 // To avoid having an operation that generates an illegal type (short
1165 // vector), generate a full size vector.
1166 //
1167 SDValue T = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, PredV);
1168 SmallVector<int,128> Mask(HwLen);
1169 // Scale = BitBytes(PredV) / Given BitBytes.
1170 unsigned Scale = HwLen / (PredTy.getVectorNumElements() * BitBytes);
1171 unsigned BlockLen = PredTy.getVectorNumElements() * BitBytes;
1172
1173 for (unsigned i = 0; i != HwLen; ++i) {
1174 unsigned Num = i % Scale;
1175 unsigned Off = i / Scale;
1176 Mask[BlockLen*Num + Off] = i;
1177 }
1178 SDValue S = DAG.getVectorShuffle(ByteTy, dl, T, DAG.getUNDEF(ByteTy), Mask);
1179 if (!ZeroFill)
1180 return S;
1181 // Fill the bytes beyond BlockLen with 0s.
1182 // V6_pred_scalar2 cannot fill the entire predicate, so it only works
1183 // when BlockLen < HwLen.
1184 assert(BlockLen < HwLen && "vsetq(v1) prerequisite");
1185 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
1186 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
1187 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
1188 SDValue M = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, Q);
1189 return DAG.getNode(ISD::AND, dl, ByteTy, S, M);
1190 }
1191
1192 // Make sure that this is a valid scalar predicate.
1193 assert(PredTy == MVT::v2i1 || PredTy == MVT::v4i1 || PredTy == MVT::v8i1);
1194
1195 unsigned Bytes = 8 / PredTy.getVectorNumElements();
1196 SmallVector<SDValue,4> Words[2];
1197 unsigned IdxW = 0;
1198
1199 SDValue W0 = isUndef(PredV)
1200 ? DAG.getUNDEF(MVT::i64)
1201 : DAG.getNode(HexagonISD::P2D, dl, MVT::i64, PredV);
1202 Words[IdxW].push_back(HiHalf(W0, DAG));
1203 Words[IdxW].push_back(LoHalf(W0, DAG));
1204
1205 while (Bytes < BitBytes) {
1206 IdxW ^= 1;
1207 Words[IdxW].clear();
1208
1209 if (Bytes < 4) {
1210 for (const SDValue &W : Words[IdxW ^ 1]) {
1211 SDValue T = expandPredicate(W, dl, DAG);
1212 Words[IdxW].push_back(HiHalf(T, DAG));
1213 Words[IdxW].push_back(LoHalf(T, DAG));
1214 }
1215 } else {
1216 for (const SDValue &W : Words[IdxW ^ 1]) {
1217 Words[IdxW].push_back(W);
1218 Words[IdxW].push_back(W);
1219 }
1220 }
1221 Bytes *= 2;
1222 }
1223
1224 assert(Bytes == BitBytes);
1225 SDValue Vec = ZeroFill ? getZero(dl, ByteTy, DAG) : DAG.getUNDEF(ByteTy);
1226 SDValue S4 = DAG.getConstant(HwLen-4, dl, MVT::i32);
1227 for (const SDValue &W : Words[IdxW]) {
1228 Vec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Vec, S4);
1229 Vec = DAG.getNode(HexagonISD::VINSERTW0, dl, ByteTy, Vec, W);
1230 }
1231
1232 return Vec;
1233}
1234
1235SDValue
1236HexagonTargetLowering::buildHvxVectorPred(ArrayRef<SDValue> Values,
1237 const SDLoc &dl, MVT VecTy,
1238 SelectionDAG &DAG) const {
1239 // Construct a vector V of bytes, such that a comparison V >u 0 would
1240 // produce the required vector predicate.
1241 unsigned VecLen = Values.size();
1242 unsigned HwLen = Subtarget.getVectorLength();
1243 assert(VecLen <= HwLen || VecLen == 8*HwLen);
1245 bool AllT = true, AllF = true;
1246
1247 auto IsTrue = [] (SDValue V) {
1248 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
1249 return !N->isZero();
1250 return false;
1251 };
1252 auto IsFalse = [] (SDValue V) {
1253 if (const auto *N = dyn_cast<ConstantSDNode>(V.getNode()))
1254 return N->isZero();
1255 return false;
1256 };
1257
1258 if (VecLen <= HwLen) {
1259 // In the hardware, each bit of a vector predicate corresponds to a byte
1260 // of a vector register. Calculate how many bytes does a bit of VecTy
1261 // correspond to.
1262 assert(HwLen % VecLen == 0);
1263 unsigned BitBytes = HwLen / VecLen;
1264 for (SDValue V : Values) {
1265 AllT &= IsTrue(V);
1266 AllF &= IsFalse(V);
1267
1268 SDValue Ext = !V.isUndef() ? DAG.getZExtOrTrunc(V, dl, MVT::i8)
1269 : DAG.getUNDEF(MVT::i8);
1270 for (unsigned B = 0; B != BitBytes; ++B)
1271 Bytes.push_back(Ext);
1272 }
1273 } else {
1274 // There are as many i1 values, as there are bits in a vector register.
1275 // Divide the values into groups of 8 and check that each group consists
1276 // of the same value (ignoring undefs).
1277 for (unsigned I = 0; I != VecLen; I += 8) {
1278 unsigned B = 0;
1279 // Find the first non-undef value in this group.
1280 for (; B != 8; ++B) {
1281 if (!Values[I+B].isUndef())
1282 break;
1283 }
1284 SDValue F = Values[I+B];
1285 AllT &= IsTrue(F);
1286 AllF &= IsFalse(F);
1287
1288 SDValue Ext = (B < 8) ? DAG.getZExtOrTrunc(F, dl, MVT::i8)
1289 : DAG.getUNDEF(MVT::i8);
1290 Bytes.push_back(Ext);
1291 // Verify that the rest of values in the group are the same as the
1292 // first.
1293 for (; B != 8; ++B)
1294 assert(Values[I+B].isUndef() || Values[I+B] == F);
1295 }
1296 }
1297
1298 if (AllT)
1299 return DAG.getNode(HexagonISD::QTRUE, dl, VecTy);
1300 if (AllF)
1301 return DAG.getNode(HexagonISD::QFALSE, dl, VecTy);
1302
1303 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1304 SDValue ByteVec = buildHvxVectorReg(Bytes, dl, ByteTy, DAG);
1305 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
1306}
1307
1308SDValue
1309HexagonTargetLowering::extractHvxElementReg(SDValue VecV, SDValue IdxV,
1310 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
1311 MVT ElemTy = ty(VecV).getVectorElementType();
1312
1313 unsigned ElemWidth = ElemTy.getSizeInBits();
1314 assert(ElemWidth >= 8 && ElemWidth <= 32);
1315 (void)ElemWidth;
1316
1317 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
1318 SDValue ExWord = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
1319 {VecV, ByteIdx});
1320 if (ElemTy == MVT::i32)
1321 return ExWord;
1322
1323 // Have an extracted word, need to extract the smaller element out of it.
1324 // 1. Extract the bits of (the original) IdxV that correspond to the index
1325 // of the desired element in the 32-bit word.
1326 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
1327 // 2. Extract the element from the word.
1328 SDValue ExVec = DAG.getBitcast(tyVector(ty(ExWord), ElemTy), ExWord);
1329 return extractVector(ExVec, SubIdx, dl, ElemTy, MVT::i32, DAG);
1330}
1331
1332SDValue
1333HexagonTargetLowering::extractHvxElementPred(SDValue VecV, SDValue IdxV,
1334 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
1335 // Implement other return types if necessary.
1336 assert(ResTy == MVT::i1);
1337
1338 unsigned HwLen = Subtarget.getVectorLength();
1339 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1340 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
1341
1342 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
1343 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
1344 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
1345
1346 SDValue ExtB = extractHvxElementReg(ByteVec, IdxV, dl, MVT::i32, DAG);
1347 SDValue Zero = DAG.getTargetConstant(0, dl, MVT::i32);
1348 return getInstr(Hexagon::C2_cmpgtui, dl, MVT::i1, {ExtB, Zero}, DAG);
1349}
1350
1351SDValue
1352HexagonTargetLowering::insertHvxElementReg(SDValue VecV, SDValue IdxV,
1353 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
1354 MVT ElemTy = ty(VecV).getVectorElementType();
1355
1356 unsigned ElemWidth = ElemTy.getSizeInBits();
1357 assert(ElemWidth >= 8 && ElemWidth <= 32);
1358 (void)ElemWidth;
1359
1360 auto InsertWord = [&DAG,&dl,this] (SDValue VecV, SDValue ValV,
1361 SDValue ByteIdxV) {
1362 MVT VecTy = ty(VecV);
1363 unsigned HwLen = Subtarget.getVectorLength();
1364 SDValue MaskV =
1365 DAG.getNode(ISD::AND, dl, MVT::i32,
1366 {ByteIdxV, DAG.getSignedConstant(-4, dl, MVT::i32)});
1367 SDValue RotV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {VecV, MaskV});
1368 SDValue InsV = DAG.getNode(HexagonISD::VINSERTW0, dl, VecTy, {RotV, ValV});
1369 SDValue SubV = DAG.getNode(ISD::SUB, dl, MVT::i32,
1370 {DAG.getConstant(HwLen, dl, MVT::i32), MaskV});
1371 SDValue TorV = DAG.getNode(HexagonISD::VROR, dl, VecTy, {InsV, SubV});
1372 return TorV;
1373 };
1374
1375 SDValue ByteIdx = convertToByteIndex(IdxV, ElemTy, DAG);
1376 if (ElemTy == MVT::i32)
1377 return InsertWord(VecV, ValV, ByteIdx);
1378
1379 // If this is not inserting a 32-bit word, convert it into such a thing.
1380 // 1. Extract the existing word from the target vector.
1381 SDValue WordIdx = DAG.getNode(ISD::SRL, dl, MVT::i32,
1382 {ByteIdx, DAG.getConstant(2, dl, MVT::i32)});
1383 SDValue Ext = extractHvxElementReg(opCastElem(VecV, MVT::i32, DAG), WordIdx,
1384 dl, MVT::i32, DAG);
1385
1386 // 2. Treating the extracted word as a 32-bit vector, insert the given
1387 // value into it.
1388 SDValue SubIdx = getIndexInWord32(IdxV, ElemTy, DAG);
1389 MVT SubVecTy = tyVector(ty(Ext), ElemTy);
1390 SDValue Ins = insertVector(DAG.getBitcast(SubVecTy, Ext),
1391 ValV, SubIdx, dl, ElemTy, DAG);
1392
1393 // 3. Insert the 32-bit word back into the original vector.
1394 return InsertWord(VecV, Ins, ByteIdx);
1395}
1396
1397SDValue
1398HexagonTargetLowering::insertHvxElementPred(SDValue VecV, SDValue IdxV,
1399 SDValue ValV, const SDLoc &dl, SelectionDAG &DAG) const {
1400 unsigned HwLen = Subtarget.getVectorLength();
1401 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1402 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
1403
1404 unsigned Scale = HwLen / ty(VecV).getVectorNumElements();
1405 SDValue ScV = DAG.getConstant(Scale, dl, MVT::i32);
1406 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, ScV);
1407 ValV = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, ValV);
1408
1409 SDValue InsV = insertHvxElementReg(ByteVec, IdxV, ValV, dl, DAG);
1410 return DAG.getNode(HexagonISD::V2Q, dl, ty(VecV), InsV);
1411}
1412
1413SDValue
1414HexagonTargetLowering::extractHvxSubvectorReg(SDValue OrigOp, SDValue VecV,
1415 SDValue IdxV, const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
1416 MVT VecTy = ty(VecV);
1417 unsigned HwLen = Subtarget.getVectorLength();
1418 unsigned Idx = IdxV.getNode()->getAsZExtVal();
1419 MVT ElemTy = VecTy.getVectorElementType();
1420 unsigned ElemWidth = ElemTy.getSizeInBits();
1421
1422 // If the source vector is a vector pair, get the single vector containing
1423 // the subvector of interest. The subvector will never overlap two single
1424 // vectors.
1425 if (isHvxPairTy(VecTy)) {
1426 unsigned SubIdx = Hexagon::vsub_lo;
1427 if (Idx * ElemWidth >= 8 * HwLen) {
1428 SubIdx = Hexagon::vsub_hi;
1429 Idx -= VecTy.getVectorNumElements() / 2;
1430 }
1431
1432 VecTy = typeSplit(VecTy).first;
1433 VecV = DAG.getTargetExtractSubreg(SubIdx, dl, VecTy, VecV);
1434 if (VecTy == ResTy)
1435 return VecV;
1436 }
1437
1438 // The only meaningful subvectors of a single HVX vector are those that
1439 // fit in a scalar register.
1440 assert(ResTy.getSizeInBits() == 32 || ResTy.getSizeInBits() == 64);
1441
1442 MVT WordTy = tyVector(VecTy, MVT::i32);
1443 SDValue WordVec = DAG.getBitcast(WordTy, VecV);
1444 unsigned WordIdx = (Idx*ElemWidth) / 32;
1445
1446 SDValue W0Idx = DAG.getConstant(WordIdx, dl, MVT::i32);
1447 SDValue W0 = extractHvxElementReg(WordVec, W0Idx, dl, MVT::i32, DAG);
1448 if (ResTy.getSizeInBits() == 32)
1449 return DAG.getBitcast(ResTy, W0);
1450
1451 SDValue W1Idx = DAG.getConstant(WordIdx+1, dl, MVT::i32);
1452 SDValue W1 = extractHvxElementReg(WordVec, W1Idx, dl, MVT::i32, DAG);
1453 SDValue WW = getCombine(W1, W0, dl, MVT::i64, DAG);
1454 return DAG.getBitcast(ResTy, WW);
1455}
1456
1457SDValue
1458HexagonTargetLowering::extractHvxSubvectorPred(SDValue VecV, SDValue IdxV,
1459 const SDLoc &dl, MVT ResTy, SelectionDAG &DAG) const {
1460 MVT VecTy = ty(VecV);
1461 unsigned HwLen = Subtarget.getVectorLength();
1462 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1463 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
1464 // IdxV is required to be a constant.
1465 unsigned Idx = IdxV.getNode()->getAsZExtVal();
1466
1467 unsigned ResLen = ResTy.getVectorNumElements();
1468 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
1469 unsigned Offset = Idx * BitBytes;
1470 SDValue Undef = DAG.getUNDEF(ByteTy);
1471 SmallVector<int,128> Mask;
1472
1473 if (Subtarget.isHVXVectorType(ResTy, true)) {
1474 // Converting between two vector predicates. Since the result is shorter
1475 // than the source, it will correspond to a vector predicate with the
1476 // relevant bits replicated. The replication count is the ratio of the
1477 // source and target vector lengths.
1478 unsigned Rep = VecTy.getVectorNumElements() / ResLen;
1479 assert(isPowerOf2_32(Rep) && HwLen % Rep == 0);
1480 for (unsigned i = 0; i != HwLen/Rep; ++i) {
1481 for (unsigned j = 0; j != Rep; ++j)
1482 Mask.push_back(i + Offset);
1483 }
1484 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
1485 return DAG.getNode(HexagonISD::V2Q, dl, ResTy, ShuffV);
1486 }
1487
1488 // Converting between a vector predicate and a scalar predicate. In the
1489 // vector predicate, a group of BitBytes bits will correspond to a single
1490 // i1 element of the source vector type. Those bits will all have the same
1491 // value. The same will be true for ByteVec, where each byte corresponds
1492 // to a bit in the vector predicate.
1493 // The algorithm is to traverse the ByteVec, going over the i1 values from
1494 // the source vector, and generate the corresponding representation in an
1495 // 8-byte vector. To avoid repeated extracts from ByteVec, shuffle the
1496 // elements so that the interesting 8 bytes will be in the low end of the
1497 // vector.
1498 unsigned Rep = 8 / ResLen;
1499 // Make sure the output fill the entire vector register, so repeat the
1500 // 8-byte groups as many times as necessary.
1501 for (unsigned r = 0; r != HwLen / 8; ++r) {
1502 // This will generate the indexes of the 8 interesting bytes.
1503 for (unsigned i = 0; i != ResLen; ++i) {
1504 for (unsigned j = 0; j != Rep; ++j)
1505 Mask.push_back(Offset + i*BitBytes);
1506 }
1507 }
1508
1509 SDValue Zero = getZero(dl, MVT::i32, DAG);
1510 SDValue ShuffV = DAG.getVectorShuffle(ByteTy, dl, ByteVec, Undef, Mask);
1511 // Combine the two low words from ShuffV into a v8i8, and byte-compare
1512 // them against 0.
1513 SDValue W0 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32, {ShuffV, Zero});
1514 SDValue W1 = DAG.getNode(HexagonISD::VEXTRACTW, dl, MVT::i32,
1515 {ShuffV, DAG.getConstant(4, dl, MVT::i32)});
1516 SDValue Vec64 = getCombine(W1, W0, dl, MVT::v8i8, DAG);
1517 return getInstr(Hexagon::A4_vcmpbgtui, dl, ResTy,
1518 {Vec64, DAG.getTargetConstant(0, dl, MVT::i32)}, DAG);
1519}
1520
1521SDValue
1522HexagonTargetLowering::insertHvxSubvectorReg(SDValue VecV, SDValue SubV,
1523 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
1524 MVT VecTy = ty(VecV);
1525 MVT SubTy = ty(SubV);
1526 unsigned HwLen = Subtarget.getVectorLength();
1527 MVT ElemTy = VecTy.getVectorElementType();
1528 unsigned ElemWidth = ElemTy.getSizeInBits();
1529
1530 bool IsPair = isHvxPairTy(VecTy);
1531 MVT SingleTy = MVT::getVectorVT(ElemTy, (8*HwLen)/ElemWidth);
1532 // The two single vectors that VecV consists of, if it's a pair.
1533 SDValue V0, V1;
1534 SDValue SingleV = VecV;
1535 SDValue PickHi;
1536
1537 if (IsPair) {
1538 V0 = LoHalf(VecV, DAG);
1539 V1 = HiHalf(VecV, DAG);
1540
1541 SDValue HalfV = DAG.getConstant(SingleTy.getVectorNumElements(),
1542 dl, MVT::i32);
1543 PickHi = DAG.getSetCC(dl, MVT::i1, IdxV, HalfV, ISD::SETUGT);
1544 if (isHvxSingleTy(SubTy)) {
1545 if (const auto *CN = dyn_cast<const ConstantSDNode>(IdxV.getNode())) {
1546 unsigned Idx = CN->getZExtValue();
1547 assert(Idx == 0 || Idx == VecTy.getVectorNumElements()/2);
1548 unsigned SubIdx = (Idx == 0) ? Hexagon::vsub_lo : Hexagon::vsub_hi;
1549 return DAG.getTargetInsertSubreg(SubIdx, dl, VecTy, VecV, SubV);
1550 }
1551 // If IdxV is not a constant, generate the two variants: with the
1552 // SubV as the high and as the low subregister, and select the right
1553 // pair based on the IdxV.
1554 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SubV, V1});
1555 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SubV});
1556 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
1557 }
1558 // The subvector being inserted must be entirely contained in one of
1559 // the vectors V0 or V1. Set SingleV to the correct one, and update
1560 // IdxV to be the index relative to the beginning of that vector.
1561 SDValue S = DAG.getNode(ISD::SUB, dl, MVT::i32, IdxV, HalfV);
1562 IdxV = DAG.getNode(ISD::SELECT, dl, MVT::i32, PickHi, S, IdxV);
1563 SingleV = DAG.getNode(ISD::SELECT, dl, SingleTy, PickHi, V1, V0);
1564 }
1565
1566 // The only meaningful subvectors of a single HVX vector are those that
1567 // fit in a scalar register.
1568 assert(SubTy.getSizeInBits() == 32 || SubTy.getSizeInBits() == 64);
1569 // Convert IdxV to be index in bytes.
1570 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
1571 if (!IdxN || !IdxN->isZero()) {
1572 IdxV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
1573 DAG.getConstant(ElemWidth/8, dl, MVT::i32));
1574 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, IdxV);
1575 }
1576 // When inserting a single word, the rotation back to the original position
1577 // would be by HwLen-Idx, but if two words are inserted, it will need to be
1578 // by (HwLen-4)-Idx.
1579 unsigned RolBase = HwLen;
1580 if (SubTy.getSizeInBits() == 32) {
1581 SDValue V = DAG.getBitcast(MVT::i32, SubV);
1582 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, V);
1583 } else {
1584 SDValue V = DAG.getBitcast(MVT::i64, SubV);
1585 SDValue R0 = LoHalf(V, DAG);
1586 SDValue R1 = HiHalf(V, DAG);
1587 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R0);
1588 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV,
1589 DAG.getConstant(4, dl, MVT::i32));
1590 SingleV = DAG.getNode(HexagonISD::VINSERTW0, dl, SingleTy, SingleV, R1);
1591 RolBase = HwLen-4;
1592 }
1593 // If the vector wasn't ror'ed, don't ror it back.
1594 if (RolBase != 4 || !IdxN || !IdxN->isZero()) {
1595 SDValue RolV = DAG.getNode(ISD::SUB, dl, MVT::i32,
1596 DAG.getConstant(RolBase, dl, MVT::i32), IdxV);
1597 SingleV = DAG.getNode(HexagonISD::VROR, dl, SingleTy, SingleV, RolV);
1598 }
1599
1600 if (IsPair) {
1601 SDValue InLo = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {SingleV, V1});
1602 SDValue InHi = DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, {V0, SingleV});
1603 return DAG.getNode(ISD::SELECT, dl, VecTy, PickHi, InHi, InLo);
1604 }
1605 return SingleV;
1606}
1607
1608SDValue
1609HexagonTargetLowering::insertHvxSubvectorPred(SDValue VecV, SDValue SubV,
1610 SDValue IdxV, const SDLoc &dl, SelectionDAG &DAG) const {
1611 MVT VecTy = ty(VecV);
1612 MVT SubTy = ty(SubV);
1613 assert(Subtarget.isHVXVectorType(VecTy, true));
1614 // VecV is an HVX vector predicate. SubV may be either an HVX vector
1615 // predicate as well, or it can be a scalar predicate.
1616
1617 unsigned VecLen = VecTy.getVectorNumElements();
1618 unsigned HwLen = Subtarget.getVectorLength();
1619 assert(HwLen % VecLen == 0 && "Unexpected vector type");
1620
1621 unsigned Scale = VecLen / SubTy.getVectorNumElements();
1622 unsigned BitBytes = HwLen / VecLen;
1623 unsigned BlockLen = HwLen / Scale;
1624
1625 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1626 SDValue ByteVec = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, VecV);
1627 if (!Subtarget.isHVXVectorType(SubTy, true)) {
1628 unsigned SubLen = SubTy.getVectorNumElements();
1629 unsigned SubBitBytes = 8 / SubLen;
1630 if (SubBitBytes > BitBytes) {
1631 unsigned WideLen = 8 / BitBytes;
1632 SmallVector<SDValue, 4> Parts(WideLen / SubLen, DAG.getUNDEF(SubTy));
1633 Parts[0] = SubV;
1634 SubV = DAG.getNode(ISD::CONCAT_VECTORS, dl,
1635 MVT::getVectorVT(MVT::i1, WideLen), Parts);
1636 }
1637 }
1638 SDValue ByteSub = createHvxPrefixPred(SubV, dl, BitBytes, false, DAG);
1639 SDValue ByteIdx;
1640
1641 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV.getNode());
1642 if (!IdxN || !IdxN->isZero()) {
1643 ByteIdx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
1644 DAG.getConstant(BitBytes, dl, MVT::i32));
1645 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteIdx);
1646 }
1647
1648 // ByteVec is the target vector VecV rotated in such a way that the
1649 // subvector should be inserted at index 0. Generate a predicate mask
1650 // and use vmux to do the insertion.
1651 assert(BlockLen < HwLen && "vsetq(v1) prerequisite");
1652 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
1653 SDValue Q = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
1654 {DAG.getConstant(BlockLen, dl, MVT::i32)}, DAG);
1655 ByteVec = getInstr(Hexagon::V6_vmux, dl, ByteTy, {Q, ByteSub, ByteVec}, DAG);
1656 // Rotate ByteVec back, and convert to a vector predicate.
1657 if (!IdxN || !IdxN->isZero()) {
1658 SDValue HwLenV = DAG.getConstant(HwLen, dl, MVT::i32);
1659 SDValue ByteXdi = DAG.getNode(ISD::SUB, dl, MVT::i32, HwLenV, ByteIdx);
1660 ByteVec = DAG.getNode(HexagonISD::VROR, dl, ByteTy, ByteVec, ByteXdi);
1661 }
1662 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, ByteVec);
1663}
1664
1665SDValue
1666HexagonTargetLowering::extendHvxVectorPred(SDValue VecV, const SDLoc &dl,
1667 MVT ResTy, bool ZeroExt, SelectionDAG &DAG) const {
1668 // Sign- and any-extending of a vector predicate to a vector register is
1669 // equivalent to Q2V. For zero-extensions, generate a vmux between 0 and
1670 // a vector of 1s (where the 1s are of type matching the vector type).
1671 assert(Subtarget.isHVXVectorType(ResTy));
1672 if (!ZeroExt)
1673 return DAG.getNode(HexagonISD::Q2V, dl, ResTy, VecV);
1674
1675 assert(ty(VecV).getVectorNumElements() == ResTy.getVectorNumElements());
1676 SDValue True = DAG.getNode(ISD::SPLAT_VECTOR, dl, ResTy,
1677 DAG.getConstant(1, dl, MVT::i32));
1678 SDValue False = getZero(dl, ResTy, DAG);
1679 return DAG.getSelect(dl, ResTy, VecV, True, False);
1680}
1681
1682SDValue
1683HexagonTargetLowering::compressHvxPred(SDValue VecQ, const SDLoc &dl,
1684 MVT ResTy, SelectionDAG &DAG) const {
1685 // Given a predicate register VecQ, transfer bits VecQ[0..HwLen-1]
1686 // (i.e. the entire predicate register) to bits [0..HwLen-1] of a
1687 // vector register. The remaining bits of the vector register are
1688 // unspecified.
1689
1691 unsigned HwLen = Subtarget.getVectorLength();
1692 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1693 MVT PredTy = ty(VecQ);
1694 unsigned PredLen = PredTy.getVectorNumElements();
1695 assert(HwLen % PredLen == 0);
1696 MVT VecTy = MVT::getVectorVT(MVT::getIntegerVT(8*HwLen/PredLen), PredLen);
1697
1698 Type *Int8Ty = Type::getInt8Ty(*DAG.getContext());
1700 // Create an array of bytes (hex): 01,02,04,08,10,20,40,80, 01,02,04,08,...
1701 // These are bytes with the LSB rotated left with respect to their index.
1702 for (unsigned i = 0; i != HwLen/8; ++i) {
1703 for (unsigned j = 0; j != 8; ++j)
1704 Tmp.push_back(ConstantInt::get(Int8Ty, 1ull << j));
1705 }
1706 Constant *CV = ConstantVector::get(Tmp);
1707 Align Alignment(HwLen);
1709 DAG.getConstantPool(CV, getPointerTy(DAG.getDataLayout()), Alignment),
1710 DAG);
1711 SDValue Bytes =
1712 DAG.getLoad(ByteTy, dl, DAG.getEntryNode(), CP,
1714
1715 // Select the bytes that correspond to true bits in the vector predicate.
1716 SDValue Sel = DAG.getSelect(dl, VecTy, VecQ, DAG.getBitcast(VecTy, Bytes),
1717 getZero(dl, VecTy, DAG));
1718 // Calculate the OR of all bytes in each group of 8. That will compress
1719 // all the individual bits into a single byte.
1720 // First, OR groups of 4, via vrmpy with 0x01010101.
1721 SDValue All1 =
1722 DAG.getSplatBuildVector(MVT::v4i8, dl, DAG.getConstant(1, dl, MVT::i32));
1723 SDValue Vrmpy = getInstr(Hexagon::V6_vrmpyub, dl, ByteTy, {Sel, All1}, DAG);
1724 // Then rotate the accumulated vector by 4 bytes, and do the final OR.
1725 SDValue Rot = getInstr(Hexagon::V6_valignbi, dl, ByteTy,
1726 {Vrmpy, Vrmpy, DAG.getTargetConstant(4, dl, MVT::i32)}, DAG);
1727 SDValue Vor = DAG.getNode(ISD::OR, dl, ByteTy, {Vrmpy, Rot});
1728
1729 // Pick every 8th byte and coalesce them at the beginning of the output.
1730 // For symmetry, coalesce every 1+8th byte after that, then every 2+8th
1731 // byte and so on.
1732 SmallVector<int,128> Mask;
1733 for (unsigned i = 0; i != HwLen; ++i)
1734 Mask.push_back((8*i) % HwLen + i/(HwLen/8));
1735 SDValue Collect =
1736 DAG.getVectorShuffle(ByteTy, dl, Vor, DAG.getUNDEF(ByteTy), Mask);
1737 return DAG.getBitcast(ResTy, Collect);
1738}
1739
1740SDValue
1741HexagonTargetLowering::resizeToWidth(SDValue VecV, MVT ResTy, bool Signed,
1742 const SDLoc &dl, SelectionDAG &DAG) const {
1743 // Take a vector and resize the element type to match the given type.
1744 MVT InpTy = ty(VecV);
1745 if (InpTy == ResTy)
1746 return VecV;
1747
1748 unsigned InpWidth = InpTy.getSizeInBits();
1749 unsigned ResWidth = ResTy.getSizeInBits();
1750
1751 if (InpTy.isFloatingPoint()) {
1752 return InpWidth < ResWidth
1753 ? DAG.getNode(ISD::FP_EXTEND, dl, ResTy, VecV)
1754 : DAG.getNode(ISD::FP_ROUND, dl, ResTy, VecV,
1755 DAG.getTargetConstant(0, dl, MVT::i32));
1756 }
1757
1758 assert(InpTy.isInteger());
1759
1760 if (InpWidth < ResWidth) {
1761 unsigned ExtOpc = Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
1762 return DAG.getNode(ExtOpc, dl, ResTy, VecV);
1763 } else {
1764 unsigned NarOpc = Signed ? HexagonISD::SSAT : HexagonISD::USAT;
1765 return DAG.getNode(NarOpc, dl, ResTy, VecV, DAG.getValueType(ResTy));
1766 }
1767}
1768
1769SDValue
1770HexagonTargetLowering::extractSubvector(SDValue Vec, MVT SubTy, unsigned SubIdx,
1771 SelectionDAG &DAG) const {
1772 assert(ty(Vec).getSizeInBits() % SubTy.getSizeInBits() == 0);
1773
1774 const SDLoc &dl(Vec);
1775 unsigned ElemIdx = SubIdx * SubTy.getVectorNumElements();
1776 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubTy,
1777 {Vec, DAG.getConstant(ElemIdx, dl, MVT::i32)});
1778}
1779
1780SDValue
1781HexagonTargetLowering::LowerHvxBuildVector(SDValue Op, SelectionDAG &DAG)
1782 const {
1783 const SDLoc &dl(Op);
1784 MVT VecTy = ty(Op);
1785
1786 unsigned Size = Op.getNumOperands();
1788 for (unsigned i = 0; i != Size; ++i)
1789 Ops.push_back(Op.getOperand(i));
1790
1791 if (VecTy.getVectorElementType() == MVT::i1)
1792 return buildHvxVectorPred(Ops, dl, VecTy, DAG);
1793
1794 // In case of MVT::f16 BUILD_VECTOR, since MVT::f16 is
1795 // not a legal type, just bitcast the node to use i16
1796 // types and bitcast the result back to f16
1797 if (VecTy.getVectorElementType() == MVT::f16 ||
1798 VecTy.getVectorElementType() == MVT::bf16) {
1800 for (unsigned i = 0; i != Size; i++)
1801 NewOps.push_back(DAG.getBitcast(MVT::i16, Ops[i]));
1802
1803 SDValue T0 =
1804 DAG.getNode(ISD::BUILD_VECTOR, dl, tyVector(VecTy, MVT::i16), NewOps);
1805 return DAG.getBitcast(tyVector(VecTy, VecTy.getVectorElementType()), T0);
1806 }
1807
1808 // First, split the BUILD_VECTOR for vector pairs. We could generate
1809 // some pairs directly (via splat), but splats should be generated
1810 // by the combiner prior to getting here.
1811 if (VecTy.getSizeInBits() == 16 * Subtarget.getVectorLength()) {
1813 MVT SingleTy = typeSplit(VecTy).first;
1814 SDValue V0 = buildHvxVectorReg(A.take_front(Size / 2), dl, SingleTy, DAG);
1815 SDValue V1 = buildHvxVectorReg(A.drop_front(Size / 2), dl, SingleTy, DAG);
1816 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VecTy, V0, V1);
1817 }
1818
1819 return buildHvxVectorReg(Ops, dl, VecTy, DAG);
1820}
1821
1822SDValue
1823HexagonTargetLowering::LowerHvxSplatVector(SDValue Op, SelectionDAG &DAG)
1824 const {
1825 const SDLoc &dl(Op);
1826 MVT VecTy = ty(Op);
1827 MVT ArgTy = ty(Op.getOperand(0));
1828
1829 if (ArgTy == MVT::f16 || ArgTy == MVT::bf16) {
1830 MVT SplatTy = MVT::getVectorVT(MVT::i16, VecTy.getVectorNumElements());
1831 SDValue ToInt16 = DAG.getBitcast(MVT::i16, Op.getOperand(0));
1832 SDValue ToInt32 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, ToInt16);
1833 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, dl, SplatTy, ToInt32);
1834 return DAG.getBitcast(VecTy, Splat);
1835 }
1836
1837 return SDValue();
1838}
1839
1840SDValue
1841HexagonTargetLowering::LowerHvxConcatVectors(SDValue Op, SelectionDAG &DAG)
1842 const {
1843 // Vector concatenation of two integer (non-bool) vectors does not need
1844 // special lowering. Custom-lower concats of bool vectors and expand
1845 // concats of more than 2 vectors.
1846 MVT VecTy = ty(Op);
1847 const SDLoc &dl(Op);
1848 unsigned NumOp = Op.getNumOperands();
1849 if (VecTy.getVectorElementType() != MVT::i1) {
1850 if (NumOp == 2)
1851 return Op;
1852 // Expand the other cases into a build-vector.
1854 for (SDValue V : Op.getNode()->ops())
1855 DAG.ExtractVectorElements(V, Elems);
1856 // A vector of i16 will be broken up into a build_vector of i16's.
1857 // This is a problem, since at the time of operation legalization,
1858 // all operations are expected to be type-legalized, and i16 is not
1859 // a legal type. If any of the extracted elements is not of a valid
1860 // type, sign-extend it to a valid one.
1861 for (SDValue &V : Elems) {
1862 MVT Ty = ty(V);
1863 if (!isTypeLegal(Ty)) {
1864 MVT NTy = typeLegalize(Ty, DAG);
1865 if (V.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1866 V = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NTy,
1867 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NTy,
1868 V.getOperand(0), V.getOperand(1)),
1869 DAG.getValueType(Ty));
1870 continue;
1871 }
1872 // A few less complicated cases.
1873 switch (V.getOpcode()) {
1874 case ISD::Constant:
1875 V = DAG.getSExtOrTrunc(V, dl, NTy);
1876 break;
1877 case ISD::UNDEF:
1878 V = DAG.getUNDEF(NTy);
1879 break;
1880 case ISD::TRUNCATE:
1881 V = V.getOperand(0);
1882 break;
1883 default:
1884 llvm_unreachable("Unexpected vector element");
1885 }
1886 }
1887 }
1888 return DAG.getBuildVector(VecTy, dl, Elems);
1889 }
1890
1891 assert(VecTy.getVectorElementType() == MVT::i1);
1892 unsigned HwLen = Subtarget.getVectorLength();
1893 assert(isPowerOf2_32(NumOp) && HwLen % NumOp == 0);
1894
1895 SDValue Op0 = Op.getOperand(0);
1896
1897 // If the operands are HVX types (i.e. not scalar predicates), then
1898 // defer the concatenation, and create QCAT instead.
1899 if (Subtarget.isHVXVectorType(ty(Op0), true)) {
1900 if (NumOp == 2)
1901 return DAG.getNode(HexagonISD::QCAT, dl, VecTy, Op0, Op.getOperand(1));
1902
1903 ArrayRef<SDUse> U(Op.getNode()->ops());
1906
1907 MVT HalfTy = typeSplit(VecTy).first;
1908 SDValue V0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
1909 Ops.take_front(NumOp/2));
1910 SDValue V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfTy,
1911 Ops.take_back(NumOp/2));
1912 return DAG.getNode(HexagonISD::QCAT, dl, VecTy, V0, V1);
1913 }
1914
1915 // Count how many bytes (in a vector register) each bit in VecTy
1916 // corresponds to.
1917 unsigned BitBytes = HwLen / VecTy.getVectorNumElements();
1918
1919 // Make sure that createHvxPrefixPred will only ever need to expand
1920 // the predicate, i.e. bytes-per-bit in the input is not greater than
1921 // the target bytes-per-bit in the result.
1922 SDValue Combined = combineConcatOfScalarPreds(Op, BitBytes, DAG);
1923 SmallVector<SDValue,8> Prefixes;
1924 for (SDValue V : Combined.getNode()->op_values()) {
1925 SDValue P = createHvxPrefixPred(V, dl, BitBytes, true, DAG);
1926 Prefixes.push_back(P);
1927 }
1928
1929 unsigned InpLen = ty(Combined.getOperand(0)).getVectorNumElements();
1930 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
1931 SDValue S = DAG.getConstant(HwLen - InpLen*BitBytes, dl, MVT::i32);
1932 SDValue Res = getZero(dl, ByteTy, DAG);
1933 for (unsigned i = 0, e = Prefixes.size(); i != e; ++i) {
1934 Res = DAG.getNode(HexagonISD::VROR, dl, ByteTy, Res, S);
1935 Res = DAG.getNode(ISD::OR, dl, ByteTy, Res, Prefixes[e-i-1]);
1936 }
1937 return DAG.getNode(HexagonISD::V2Q, dl, VecTy, Res);
1938}
1939
1940SDValue
1941HexagonTargetLowering::LowerHvxExtractElement(SDValue Op, SelectionDAG &DAG)
1942 const {
1943 // Change the type of the extracted element to i32.
1944 SDValue VecV = Op.getOperand(0);
1945 MVT ElemTy = ty(VecV).getVectorElementType();
1946 const SDLoc &dl(Op);
1947 SDValue IdxV = Op.getOperand(1);
1948 if (ElemTy == MVT::i1)
1949 return extractHvxElementPred(VecV, IdxV, dl, ty(Op), DAG);
1950
1951 return extractHvxElementReg(VecV, IdxV, dl, ty(Op), DAG);
1952}
1953
1954SDValue
1955HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
1956 const {
1957 const SDLoc &dl(Op);
1958 SDValue VecV = Op.getOperand(0);
1959 SDValue ValV = Op.getOperand(1);
1960 SDValue IdxV = Op.getOperand(2);
1961 MVT ElemTy = ty(VecV).getVectorElementType();
1962 if (ElemTy == MVT::i1)
1963 return insertHvxElementPred(VecV, IdxV, ValV, dl, DAG);
1964
1965 return insertHvxElementReg(VecV, IdxV, ValV, dl, DAG);
1966}
1967
1968SDValue
1969HexagonTargetLowering::LowerHvxExtractSubvector(SDValue Op, SelectionDAG &DAG)
1970 const {
1971 SDValue SrcV = Op.getOperand(0);
1972 MVT SrcTy = ty(SrcV);
1973 MVT DstTy = ty(Op);
1974 SDValue IdxV = Op.getOperand(1);
1975 unsigned Idx = IdxV.getNode()->getAsZExtVal();
1976 assert(Idx % DstTy.getVectorNumElements() == 0);
1977 (void)Idx;
1978 const SDLoc &dl(Op);
1979
1980 MVT ElemTy = SrcTy.getVectorElementType();
1981 if (ElemTy == MVT::i1)
1982 return extractHvxSubvectorPred(SrcV, IdxV, dl, DstTy, DAG);
1983
1984 return extractHvxSubvectorReg(Op, SrcV, IdxV, dl, DstTy, DAG);
1985}
1986
1987SDValue
1988HexagonTargetLowering::LowerHvxInsertSubvector(SDValue Op, SelectionDAG &DAG)
1989 const {
1990 // Idx does not need to be a constant.
1991 SDValue VecV = Op.getOperand(0);
1992 SDValue ValV = Op.getOperand(1);
1993 SDValue IdxV = Op.getOperand(2);
1994
1995 const SDLoc &dl(Op);
1996 MVT VecTy = ty(VecV);
1997 MVT ElemTy = VecTy.getVectorElementType();
1998 if (ElemTy == MVT::i1)
1999 return insertHvxSubvectorPred(VecV, ValV, IdxV, dl, DAG);
2000
2001 return insertHvxSubvectorReg(VecV, ValV, IdxV, dl, DAG);
2002}
2003
2004SDValue
2005HexagonTargetLowering::LowerHvxAnyExt(SDValue Op, SelectionDAG &DAG) const {
2006 // Lower any-extends of boolean vectors to sign-extends, since they
2007 // translate directly to Q2V. Zero-extending could also be done equally
2008 // fast, but Q2V is used/recognized in more places.
2009 // For all other vectors, use zero-extend.
2010 MVT ResTy = ty(Op);
2011 SDValue InpV = Op.getOperand(0);
2012 MVT ElemTy = ty(InpV).getVectorElementType();
2013 if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
2014 return LowerHvxSignExt(Op, DAG);
2015 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(Op), ResTy, InpV);
2016}
2017
2018SDValue
2019HexagonTargetLowering::LowerHvxSignExt(SDValue Op, SelectionDAG &DAG) const {
2020 MVT ResTy = ty(Op);
2021 SDValue InpV = Op.getOperand(0);
2022 MVT ElemTy = ty(InpV).getVectorElementType();
2023 if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
2024 return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), false, DAG);
2025 return Op;
2026}
2027
2028SDValue
2029HexagonTargetLowering::LowerHvxZeroExt(SDValue Op, SelectionDAG &DAG) const {
2030 MVT ResTy = ty(Op);
2031 SDValue InpV = Op.getOperand(0);
2032 MVT ElemTy = ty(InpV).getVectorElementType();
2033 if (ElemTy == MVT::i1 && Subtarget.isHVXVectorType(ResTy))
2034 return extendHvxVectorPred(InpV, SDLoc(Op), ty(Op), true, DAG);
2035 return Op;
2036}
2037
2038SDValue
2039HexagonTargetLowering::LowerHvxCttz(SDValue Op, SelectionDAG &DAG) const {
2040 // Lower vector CTTZ into a computation using CTLZ (Hacker's Delight):
2041 // cttz(x) = bitwidth(x) - ctlz(~x & (x-1))
2042 const SDLoc &dl(Op);
2043 MVT ResTy = ty(Op);
2044 SDValue InpV = Op.getOperand(0);
2045 assert(ResTy == ty(InpV));
2046
2047 // Calculate the vectors of 1 and bitwidth(x).
2048 MVT ElemTy = ty(InpV).getVectorElementType();
2049 unsigned ElemWidth = ElemTy.getSizeInBits();
2050
2051 SDValue Vec1 = DAG.getNode(ISD::SPLAT_VECTOR, dl, ResTy,
2052 DAG.getConstant(1, dl, MVT::i32));
2053 SDValue VecW = DAG.getNode(ISD::SPLAT_VECTOR, dl, ResTy,
2054 DAG.getConstant(ElemWidth, dl, MVT::i32));
2055 SDValue VecN1 = DAG.getNode(ISD::SPLAT_VECTOR, dl, ResTy,
2056 DAG.getAllOnesConstant(dl, MVT::i32));
2057
2058 // Do not use DAG.getNOT, because that would create BUILD_VECTOR with
2059 // a BITCAST. Here we can skip the BITCAST (so we don't have to handle
2060 // it separately in custom combine or selection).
2061 SDValue A = DAG.getNode(ISD::AND, dl, ResTy,
2062 {DAG.getNode(ISD::XOR, dl, ResTy, {InpV, VecN1}),
2063 DAG.getNode(ISD::SUB, dl, ResTy, {InpV, Vec1})});
2064 return DAG.getNode(ISD::SUB, dl, ResTy,
2065 {VecW, DAG.getNode(ISD::CTLZ, dl, ResTy, A)});
2066}
2067
2068SDValue
2069HexagonTargetLowering::LowerHvxMulh(SDValue Op, SelectionDAG &DAG) const {
2070 const SDLoc &dl(Op);
2071 MVT ResTy = ty(Op);
2072 assert(ResTy.getVectorElementType() == MVT::i32);
2073
2074 SDValue Vs = Op.getOperand(0);
2075 SDValue Vt = Op.getOperand(1);
2076
2077 SDVTList ResTys = DAG.getVTList(ResTy, ResTy);
2078 unsigned Opc = Op.getOpcode();
2079
2080 // On HVX v62+ producing the full product is cheap, so legalize MULH to LOHI.
2081 if (Opc == ISD::MULHU)
2082 return DAG.getNode(HexagonISD::UMUL_LOHI, dl, ResTys, {Vs, Vt}).getValue(1);
2083 if (Opc == ISD::MULHS)
2084 return DAG.getNode(HexagonISD::SMUL_LOHI, dl, ResTys, {Vs, Vt}).getValue(1);
2085
2086#ifndef NDEBUG
2087 Op.dump(&DAG);
2088#endif
2089 llvm_unreachable("Unexpected mulh operation");
2090}
2091
2092SDValue
2093HexagonTargetLowering::LowerHvxMulLoHi(SDValue Op, SelectionDAG &DAG) const {
2094 const SDLoc &dl(Op);
2095 unsigned Opc = Op.getOpcode();
2096 SDValue Vu = Op.getOperand(0);
2097 SDValue Vv = Op.getOperand(1);
2098
2099 // If the HI part is not used, convert it to a regular MUL.
2100 if (auto HiVal = Op.getValue(1); HiVal.use_empty()) {
2101 // Need to preserve the types and the number of values.
2102 SDValue Hi = DAG.getUNDEF(ty(HiVal));
2103 SDValue Lo = DAG.getNode(ISD::MUL, dl, ty(Op), {Vu, Vv});
2104 return DAG.getMergeValues({Lo, Hi}, dl);
2105 }
2106
2107 bool SignedVu = Opc == HexagonISD::SMUL_LOHI;
2108 bool SignedVv = Opc == HexagonISD::SMUL_LOHI || Opc == HexagonISD::USMUL_LOHI;
2109
2110 // Legal on HVX v62+, but lower it here because patterns can't handle multi-
2111 // valued nodes.
2112 if (Subtarget.useHVXV62Ops())
2113 return emitHvxMulLoHiV62(Vu, SignedVu, Vv, SignedVv, dl, DAG);
2114
2115 if (Opc == HexagonISD::SMUL_LOHI) {
2116 // Direct MULHS expansion is cheaper than doing the whole SMUL_LOHI,
2117 // for other signedness LOHI is cheaper.
2118 if (auto LoVal = Op.getValue(0); LoVal.use_empty()) {
2119 SDValue Hi = emitHvxMulHsV60(Vu, Vv, dl, DAG);
2120 SDValue Lo = DAG.getUNDEF(ty(LoVal));
2121 return DAG.getMergeValues({Lo, Hi}, dl);
2122 }
2123 }
2124
2125 return emitHvxMulLoHiV60(Vu, SignedVu, Vv, SignedVv, dl, DAG);
2126}
2127
2128SDValue
2129HexagonTargetLowering::LowerHvxBitcast(SDValue Op, SelectionDAG &DAG) const {
2130 SDValue Val = Op.getOperand(0);
2131 MVT ResTy = ty(Op);
2132 MVT ValTy = ty(Val);
2133 const SDLoc &dl(Op);
2134
2135 if (isHvxBoolTy(ValTy) && ResTy.isScalarInteger()) {
2136 unsigned HwLen = Subtarget.getVectorLength();
2137 MVT WordTy = MVT::getVectorVT(MVT::i32, HwLen/4);
2138
2139 // When the predicate is shorter than the predicate register, each boolean
2140 // is represented by multiple consecutive bits in the input register.
2141 // Condense the bits so each boolean is represented by one bit. This only
2142 // handles 2x and 4x compaction ratios.
2143 unsigned PredLen = ValTy.getVectorNumElements();
2144 if (PredLen < HwLen) {
2145 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
2146 Val = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, Val);
2147 if (HwLen > PredLen * 2) {
2148 assert(HwLen == PredLen * 4);
2149 PredLen *= 2;
2150 Val = getInstr(Hexagon::V6_vdealh, dl, ByteTy, Val, DAG);
2151 }
2152 if (HwLen > PredLen) {
2153 assert(HwLen == PredLen * 2);
2154 Val = getInstr(Hexagon::V6_vdealb, dl, ByteTy, Val, DAG);
2155 }
2156 Val = DAG.getNode(HexagonISD::V2Q, dl, ValTy, Val);
2157 }
2158
2159 SDValue VQ = compressHvxPred(Val, dl, WordTy, DAG);
2160 unsigned BitWidth = ResTy.getSizeInBits();
2161
2162 if (BitWidth < 64) {
2163 SDValue W0 = extractHvxElementReg(VQ, DAG.getConstant(0, dl, MVT::i32),
2164 dl, MVT::i32, DAG);
2165 if (BitWidth == 32)
2166 return W0;
2167 assert(BitWidth < 32u);
2168 return DAG.getZExtOrTrunc(W0, dl, ResTy);
2169 }
2170
2171 // The result is >= 64 bits. The only options are 64 or 128.
2172 assert(BitWidth == 64 || BitWidth == 128);
2174 for (unsigned i = 0; i != BitWidth/32; ++i) {
2175 SDValue W = extractHvxElementReg(
2176 VQ, DAG.getConstant(i, dl, MVT::i32), dl, MVT::i32, DAG);
2177 Words.push_back(W);
2178 }
2179 SmallVector<SDValue,2> Combines;
2180 assert(Words.size() % 2 == 0);
2181 for (unsigned i = 0, e = Words.size(); i < e; i += 2) {
2182 SDValue C = getCombine(Words[i+1], Words[i], dl, MVT::i64, DAG);
2183 Combines.push_back(C);
2184 }
2185
2186 if (BitWidth == 64)
2187 return Combines[0];
2188
2189 return DAG.getNode(ISD::BUILD_PAIR, dl, ResTy, Combines);
2190 }
2191
2192 // Handle bitcast from i32, v2i16, and v4i8 to v32i1.
2193 // Splat the input into a 32-element i32 vector, then AND each element
2194 // with a unique bitmask to isolate individual bits.
2195 auto bitcastI32ToV32I1 = [&](SDValue Val32) {
2196 assert(Val32.getValueType().getSizeInBits() == 32 &&
2197 "Input must be 32 bits");
2198 MVT VecTy = MVT::getVectorVT(MVT::i32, 32);
2199 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, dl, VecTy, Val32);
2201 for (unsigned i = 0; i < 32; ++i)
2202 Mask.push_back(DAG.getConstant(1ull << i, dl, MVT::i32));
2203
2204 SDValue MaskVec = DAG.getBuildVector(VecTy, dl, Mask);
2205 SDValue Anded = DAG.getNode(ISD::AND, dl, VecTy, Splat, MaskVec);
2206 return DAG.getNode(HexagonISD::V2Q, dl, MVT::v32i1, Anded);
2207 };
2208 // === Case: v32i1 ===
2209 if (ResTy == MVT::v32i1 &&
2210 (ValTy == MVT::i32 || ValTy == MVT::v2i16 || ValTy == MVT::v4i8) &&
2211 Subtarget.useHVX128BOps()) {
2212 SDValue Val32 = Val;
2213 if (ValTy == MVT::v2i16 || ValTy == MVT::v4i8)
2214 Val32 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Val);
2215 return bitcastI32ToV32I1(Val32);
2216 }
2217 // === Case: v64i1 ===
2218 if (ResTy == MVT::v64i1 && ValTy == MVT::i64 && Subtarget.useHVX128BOps()) {
2219 // Split i64 into lo/hi 32-bit halves.
2220 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Val);
2221 SDValue HiShifted = DAG.getNode(ISD::SRL, dl, MVT::i64, Val,
2222 DAG.getConstant(32, dl, MVT::i64));
2223 SDValue Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, HiShifted);
2224
2225 // Reuse the same 32-bit logic twice.
2226 SDValue LoRes = bitcastI32ToV32I1(Lo);
2227 SDValue HiRes = bitcastI32ToV32I1(Hi);
2228
2229 // Concatenate into a v64i1 predicate.
2230 return DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v64i1, LoRes, HiRes);
2231 }
2232
2233 if (isHvxBoolTy(ResTy) && ValTy.isScalarInteger()) {
2234 // Handle bitcast from i128 -> v128i1 and i64 -> v64i1.
2235 unsigned BitWidth = ValTy.getSizeInBits();
2236 unsigned HwLen = Subtarget.getVectorLength();
2237 assert(BitWidth == HwLen);
2238
2239 MVT ValAsVecTy = MVT::getVectorVT(MVT::i8, BitWidth / 8);
2240 SDValue ValAsVec = DAG.getBitcast(ValAsVecTy, Val);
2241 // Splat each byte of Val 8 times.
2242 // Bytes = [(b0)x8, (b1)x8, ...., (b15)x8]
2243 // where b0, b1,..., b15 are least to most significant bytes of I.
2245 // Tmp: 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80, 0x01,0x02,0x04,0x08,...
2246 // These are bytes with the LSB rotated left with respect to their index.
2248 for (unsigned I = 0; I != HwLen / 8; ++I) {
2249 SDValue Idx = DAG.getConstant(I, dl, MVT::i32);
2250 SDValue Byte =
2251 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i8, ValAsVec, Idx);
2252 for (unsigned J = 0; J != 8; ++J) {
2253 Bytes.push_back(Byte);
2254 Tmp.push_back(DAG.getConstant(1ull << J, dl, MVT::i8));
2255 }
2256 }
2257
2258 MVT ConstantVecTy = MVT::getVectorVT(MVT::i8, HwLen);
2259 SDValue ConstantVec = DAG.getBuildVector(ConstantVecTy, dl, Tmp);
2260 SDValue I2V = buildHvxVectorReg(Bytes, dl, ConstantVecTy, DAG);
2261
2262 // Each Byte in the I2V will be set iff corresponding bit is set in Val.
2263 I2V = DAG.getNode(ISD::AND, dl, ConstantVecTy, {I2V, ConstantVec});
2264 return DAG.getNode(HexagonISD::V2Q, dl, ResTy, I2V);
2265 }
2266
2267 return Op;
2268}
2269
2270SDValue HexagonTargetLowering::LowerHvxStore(SDValue Op,
2271 SelectionDAG &DAG) const {
2272 const SDLoc &dl(Op);
2273 StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
2274 SDValue Val = SN->getValue();
2275 MVT ValTy = ty(Val);
2276
2277 // Check if this is a store of an HVX bool vector (predicate)
2278 if (!isHvxBoolTy(ValTy))
2279 return SDValue();
2280
2281 unsigned NumBits = ValTy.getVectorNumElements();
2282 MachineMemOperand *MMO = SN->getMemOperand();
2283
2284 // Check alignment requirements based on predicate size
2285 unsigned RequiredAlign = (NumBits == 32) ? 4 : 8;
2286 if (MMO->getBaseAlign().value() % RequiredAlign != 0)
2287 return SDValue();
2288
2289 unsigned HwLen = Subtarget.getVectorLength();
2290 MVT WordTy = MVT::getVectorVT(MVT::i32, HwLen / 4);
2291
2292 // Compress the predicate into a vector register
2293 SDValue VQ = compressHvxPred(Val, dl, WordTy, DAG);
2294
2295 // Extract words from the compressed vector
2297 for (unsigned i = 0; i != NumBits / 32; ++i) {
2298 SDValue W = extractHvxElementReg(VQ, DAG.getConstant(i, dl, MVT::i32), dl,
2299 MVT::i32, DAG);
2300 Words.push_back(W);
2301 }
2302
2303 SDValue Chain = SN->getChain();
2304 SDValue BasePtr = SN->getBasePtr();
2305 MachinePointerInfo PtrInfo = MMO->getPointerInfo();
2306
2307 if (NumBits == 32)
2308 return DAG.getStore(Chain, dl, Words[0], BasePtr, PtrInfo,
2309 MMO->getBaseAlign());
2310
2311 if (NumBits == 64) {
2312 SDValue W64 = getCombine(Words[1], Words[0], dl, MVT::i64, DAG);
2313 return DAG.getStore(Chain, dl, W64, BasePtr, PtrInfo, MMO->getBaseAlign());
2314 }
2315
2316 if (NumBits == 128) {
2317 SDValue Lo64 = getCombine(Words[1], Words[0], dl, MVT::i64, DAG);
2318 SDValue Hi64 = getCombine(Words[3], Words[2], dl, MVT::i64, DAG);
2319
2320 Chain =
2321 DAG.getStore(Chain, dl, Lo64, BasePtr, PtrInfo, MMO->getBaseAlign());
2322
2323 SDValue Offset8 = DAG.getConstant(8, dl, MVT::i32);
2324 SDValue Ptr8 = DAG.getNode(ISD::ADD, dl, MVT::i32, BasePtr, Offset8);
2325 return DAG.getStore(Chain, dl, Hi64, Ptr8, PtrInfo.getWithOffset(8),
2326 Align(8));
2327 }
2328
2329 return SDValue();
2330}
2331
2332SDValue HexagonTargetLowering::LowerHvxLoad(SDValue Op,
2333 SelectionDAG &DAG) const {
2334 const SDLoc &dl(Op);
2335 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
2336 MVT ResTy = ty(Op);
2337
2338 // Check if this is a load of an HVX bool vector (predicate)
2339 if (!isHvxBoolTy(ResTy))
2340 return SDValue();
2341
2342 unsigned NumBits = ResTy.getVectorNumElements();
2343 MachineMemOperand *MMO = LN->getMemOperand();
2344
2345 unsigned RequiredAlign = (NumBits == 32) ? 4 : 8;
2346 if (MMO->getBaseAlign().value() % RequiredAlign != 0)
2347 return SDValue();
2348
2349 SDValue Chain = LN->getChain();
2350 SDValue BasePtr = LN->getBasePtr();
2351 MachinePointerInfo PtrInfo = MMO->getPointerInfo();
2352
2353 if (NumBits == 32) {
2354 SDValue W32 =
2355 DAG.getLoad(MVT::i32, dl, Chain, BasePtr, PtrInfo, MMO->getBaseAlign());
2356 SDValue Pred = DAG.getNode(ISD::BITCAST, dl, MVT::v32i1, W32);
2357 SDValue Ops[] = {Pred, W32.getValue(1)};
2358 return DAG.getMergeValues(Ops, dl);
2359 }
2360
2361 if (NumBits == 64) {
2362 SDValue W64 =
2363 DAG.getLoad(MVT::i64, dl, Chain, BasePtr, PtrInfo, MMO->getBaseAlign());
2364 SDValue Pred = DAG.getNode(ISD::BITCAST, dl, MVT::v64i1, W64);
2365 SDValue Ops[] = {Pred, W64.getValue(1)};
2366 return DAG.getMergeValues(Ops, dl);
2367 }
2368
2369 if (NumBits == 128) {
2370 SDValue Lo64 =
2371 DAG.getLoad(MVT::i64, dl, Chain, BasePtr, PtrInfo, MMO->getBaseAlign());
2372 Chain = Lo64.getValue(1);
2373
2374 SDValue Offset8 = DAG.getConstant(8, dl, MVT::i32);
2375 SDValue Ptr8 = DAG.getNode(ISD::ADD, dl, MVT::i32, BasePtr, Offset8);
2376 SDValue Hi64 = DAG.getLoad(MVT::i64, dl, Chain, Ptr8,
2377 PtrInfo.getWithOffset(8), Align(8));
2378
2379 SDValue LoPred = DAG.getNode(ISD::BITCAST, dl, MVT::v64i1, Lo64);
2380 SDValue HiPred = DAG.getNode(ISD::BITCAST, dl, MVT::v64i1, Hi64);
2381 SDValue Pred =
2382 DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v128i1, LoPred, HiPred);
2383
2384 SDValue Ops[] = {Pred, Hi64.getValue(1)};
2385 return DAG.getMergeValues(Ops, dl);
2386 }
2387
2388 return SDValue();
2389}
2390
2391SDValue
2392HexagonTargetLowering::LowerHvxExtend(SDValue Op, SelectionDAG &DAG) const {
2393 // Sign- and zero-extends are legal.
2394 assert(Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG);
2395 return DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, SDLoc(Op), ty(Op),
2396 Op.getOperand(0));
2397}
2398
2399SDValue
2400HexagonTargetLowering::LowerHvxSelect(SDValue Op, SelectionDAG &DAG) const {
2401 MVT ResTy = ty(Op);
2402 if (ResTy.getVectorElementType() != MVT::i1)
2403 return Op;
2404
2405 const SDLoc &dl(Op);
2406 unsigned HwLen = Subtarget.getVectorLength();
2407 unsigned VecLen = ResTy.getVectorNumElements();
2408 assert(HwLen % VecLen == 0);
2409 unsigned ElemSize = HwLen / VecLen;
2410
2411 MVT VecTy = MVT::getVectorVT(MVT::getIntegerVT(ElemSize * 8), VecLen);
2412 SDValue S =
2413 DAG.getNode(ISD::SELECT, dl, VecTy, Op.getOperand(0),
2414 DAG.getNode(HexagonISD::Q2V, dl, VecTy, Op.getOperand(1)),
2415 DAG.getNode(HexagonISD::Q2V, dl, VecTy, Op.getOperand(2)));
2416 return DAG.getNode(HexagonISD::V2Q, dl, ResTy, S);
2417}
2418
2419SDValue
2420HexagonTargetLowering::LowerHvxShift(SDValue Op, SelectionDAG &DAG) const {
2421 if (SDValue S = getVectorShiftByInt(Op, DAG))
2422 return S;
2423 return Op;
2424}
2425
2426SDValue
2427HexagonTargetLowering::LowerHvxFunnelShift(SDValue Op,
2428 SelectionDAG &DAG) const {
2429 unsigned Opc = Op.getOpcode();
2430 assert(Opc == ISD::FSHL || Opc == ISD::FSHR);
2431
2432 // Make sure the shift amount is within the range of the bitwidth
2433 // of the element type.
2434 SDValue A = Op.getOperand(0);
2435 SDValue B = Op.getOperand(1);
2436 SDValue S = Op.getOperand(2);
2437
2438 MVT InpTy = ty(A);
2439 MVT ElemTy = InpTy.getVectorElementType();
2440
2441 const SDLoc &dl(Op);
2442 unsigned ElemWidth = ElemTy.getSizeInBits();
2443 bool IsLeft = Opc == ISD::FSHL;
2444
2445 // The expansion into regular shifts produces worse code for i8 and for
2446 // right shift of i32 on v65+.
2447 bool UseShifts = ElemTy != MVT::i8;
2448 if (Subtarget.useHVXV65Ops() && ElemTy == MVT::i32)
2449 UseShifts = false;
2450
2451 if (SDValue SplatV = getSplatValue(S, DAG); SplatV && UseShifts) {
2452 // If this is a funnel shift by a scalar, lower it into regular shifts.
2453 SDValue Mask = DAG.getConstant(ElemWidth - 1, dl, MVT::i32);
2454 SDValue ModS =
2455 DAG.getNode(ISD::AND, dl, MVT::i32,
2456 {DAG.getZExtOrTrunc(SplatV, dl, MVT::i32), Mask});
2457 SDValue NegS =
2458 DAG.getNode(ISD::SUB, dl, MVT::i32,
2459 {DAG.getConstant(ElemWidth, dl, MVT::i32), ModS});
2460 SDValue IsZero =
2461 DAG.getSetCC(dl, MVT::i1, ModS, getZero(dl, MVT::i32, DAG), ISD::SETEQ);
2462 // FSHL A, B => A << | B >>n
2463 // FSHR A, B => A <<n | B >>
2464 SDValue Part1 =
2465 DAG.getNode(HexagonISD::VASL, dl, InpTy, {A, IsLeft ? ModS : NegS});
2466 SDValue Part2 =
2467 DAG.getNode(HexagonISD::VLSR, dl, InpTy, {B, IsLeft ? NegS : ModS});
2468 SDValue Or = DAG.getNode(ISD::OR, dl, InpTy, {Part1, Part2});
2469 // If the shift amount was 0, pick A or B, depending on the direction.
2470 // The opposite shift will also be by 0, so the "Or" will be incorrect.
2471 return DAG.getNode(ISD::SELECT, dl, InpTy, {IsZero, (IsLeft ? A : B), Or});
2472 }
2473
2475 InpTy, dl, DAG.getConstant(ElemWidth - 1, dl, ElemTy));
2476
2477 unsigned MOpc = Opc == ISD::FSHL ? HexagonISD::MFSHL : HexagonISD::MFSHR;
2478 return DAG.getNode(MOpc, dl, ty(Op),
2479 {A, B, DAG.getNode(ISD::AND, dl, InpTy, {S, Mask})});
2480}
2481
2482SDValue
2483HexagonTargetLowering::LowerHvxIntrinsic(SDValue Op, SelectionDAG &DAG) const {
2484 const SDLoc &dl(Op);
2485 unsigned IntNo = Op.getConstantOperandVal(0);
2486 SmallVector<SDValue> Ops(Op->ops());
2487
2488 auto Swap = [&](SDValue P) {
2489 return DAG.getMergeValues({P.getValue(1), P.getValue(0)}, dl);
2490 };
2491
2492 const SDLoc &DL(Op);
2493 switch (IntNo) {
2494 // Translate IEEE intrinsic to XQF.
2495 // In v79, the instruction v0.h = vcvt(v0.hf) is not an exact conversion.
2496 // The bit exactness is mimicked using a library call.
2497 case Intrinsic::hexagon_V6_vcvt_b_hf_128B:
2498 case Intrinsic::hexagon_V6_vcvt_h_hf_128B:
2499 case Intrinsic::hexagon_V6_vcvt_ub_hf_128B:
2500 case Intrinsic::hexagon_V6_vcvt_uh_hf_128B: {
2502 auto &HST = MF.getSubtarget<HexagonSubtarget>();
2503 // Generate the call only on v79 only.
2504 if (!HST.useHVXV79OpsOnly())
2505 return Op;
2506
2508 EVT ArgVT;
2509 for (unsigned i = 1; i < Op.getNumOperands(); ++i) {
2510 SDValue Arg = Op.getOperand(i);
2511 ArgVT = Arg.getValueType();
2512 assert(ArgVT.isVector() &&
2513 "Expecting a vector argument type for V6_vcvt_h_hf instrinsic.");
2514 ArgListEntry Entry(Arg, ArgVT.getTypeForEVT(*DAG.getContext()));
2515 Args.push_back(Entry);
2516 }
2517 Type *RetTy = ArgVT.getTypeForEVT(*DAG.getContext());
2518
2519 bool LongCalls = HST.useLongCalls();
2520 unsigned Flag = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
2521
2522 const char *Name = nullptr;
2523 switch (IntNo) {
2524 case Intrinsic::hexagon_V6_vcvt_b_hf_128B: {
2525 Name = Subtarget.getCLOpts().qf_fast_convert
2526 ? "__qf_fast_convert_hf_to_b_rne"
2527 : "__qf_convert_hf_to_b_rne";
2528 break;
2529 }
2530 case Intrinsic::hexagon_V6_vcvt_h_hf_128B: {
2531 Name = Subtarget.getCLOpts().qf_fast_convert
2532 ? "__qf_fast_convert_hf_to_h_rne"
2533 : "__qf_convert_hf_to_h_rne";
2534 break;
2535 }
2536 case Intrinsic::hexagon_V6_vcvt_ub_hf_128B: {
2537 Name = Subtarget.getCLOpts().qf_fast_convert
2538 ? "__qf_fast_convert_hf_to_ub_rne"
2539 : "__qf_convert_hf_to_ub_rne";
2540 break;
2541 }
2542 case Intrinsic::hexagon_V6_vcvt_uh_hf_128B: {
2543 Name = Subtarget.getCLOpts().qf_fast_convert
2544 ? "__qf_fast_convert_hf_to_uh_rne"
2545 : "__qf_convert_hf_to_uh_rne";
2546 break;
2547 }
2548 default:
2549 report_fatal_error("Unhandled intrinsic '" +
2550 Intrinsic::getName(static_cast<Intrinsic::ID>(IntNo)) +
2551 "' in HexagonTargetLowering::LowerHvxIntrinsic");
2552 }
2553
2555 Name, getPointerTy(DAG.getDataLayout()), Flag);
2556
2557 TargetLowering::CallLoweringInfo CLI(DAG);
2558 CLI.setDebugLoc(DL)
2559 .setChain(DAG.getEntryNode())
2560 .setLibCallee(CallingConv::Fast, RetTy, Callee, std::move(Args));
2561 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
2562 return CallResult.first;
2563 }
2564 case Intrinsic::hexagon_V6_pred_typecast:
2565 case Intrinsic::hexagon_V6_pred_typecast_128B: {
2566 MVT ResTy = ty(Op), InpTy = ty(Ops[1]);
2567 if (isHvxBoolTy(ResTy) && isHvxBoolTy(InpTy)) {
2568 if (ResTy == InpTy)
2569 return Ops[1];
2570 return DAG.getNode(HexagonISD::TYPECAST, dl, ResTy, Ops[1]);
2571 }
2572 break;
2573 }
2574 case Intrinsic::hexagon_V6_vmpyss_parts:
2575 case Intrinsic::hexagon_V6_vmpyss_parts_128B:
2576 return Swap(DAG.getNode(HexagonISD::SMUL_LOHI, dl, Op->getVTList(),
2577 {Ops[1], Ops[2]}));
2578 case Intrinsic::hexagon_V6_vmpyuu_parts:
2579 case Intrinsic::hexagon_V6_vmpyuu_parts_128B:
2580 return Swap(DAG.getNode(HexagonISD::UMUL_LOHI, dl, Op->getVTList(),
2581 {Ops[1], Ops[2]}));
2582 case Intrinsic::hexagon_V6_vmpyus_parts:
2583 case Intrinsic::hexagon_V6_vmpyus_parts_128B: {
2584 return Swap(DAG.getNode(HexagonISD::USMUL_LOHI, dl, Op->getVTList(),
2585 {Ops[1], Ops[2]}));
2586 }
2587 } // switch
2588
2589 return Op;
2590}
2591
2592SDValue
2593HexagonTargetLowering::LowerHvxMaskedOp(SDValue Op, SelectionDAG &DAG) const {
2594 const SDLoc &dl(Op);
2595 unsigned HwLen = Subtarget.getVectorLength();
2597 auto *MaskN = cast<MaskedLoadStoreSDNode>(Op.getNode());
2598 SDValue Mask = MaskN->getMask();
2599 SDValue Chain = MaskN->getChain();
2600 SDValue Base = MaskN->getBasePtr();
2601 auto *MemOp = MF.getMachineMemOperand(MaskN->getMemOperand(), 0, HwLen);
2602
2603 unsigned Opc = Op->getOpcode();
2605
2606 if (Opc == ISD::MLOAD) {
2607 MVT ValTy = ty(Op);
2608 SDValue Load = DAG.getLoad(ValTy, dl, Chain, Base, MemOp);
2609 SDValue Thru = cast<MaskedLoadSDNode>(MaskN)->getPassThru();
2610 if (isUndef(Thru))
2611 return Load;
2612 SDValue VSel = DAG.getNode(ISD::VSELECT, dl, ValTy, Mask, Load, Thru);
2613 return DAG.getMergeValues({VSel, Load.getValue(1)}, dl);
2614 }
2615
2616 // MSTORE
2617 // HVX only has aligned masked stores.
2618
2619 // TODO: Fold negations of the mask into the store.
2620 unsigned StoreOpc = Hexagon::V6_vS32b_qpred_ai;
2621 SDValue Value = cast<MaskedStoreSDNode>(MaskN)->getValue();
2622 SDValue Offset0 = DAG.getTargetConstant(0, dl, ty(Base));
2623
2624 if (MaskN->getAlign().value() % HwLen == 0) {
2625 SDValue Store = getInstr(StoreOpc, dl, MVT::Other,
2626 {Mask, Base, Offset0, Value, Chain}, DAG);
2627 DAG.setNodeMemRefs(cast<MachineSDNode>(Store.getNode()), {MemOp});
2628 return Store;
2629 }
2630
2631 // Unaligned case.
2632 auto StoreAlign = [&](SDValue V, SDValue A) {
2633 SDValue Z = getZero(dl, ty(V), DAG);
2634 // TODO: use funnel shifts?
2635 // vlalign(Vu,Vv,Rt) rotates the pair Vu:Vv left by Rt and takes the
2636 // upper half.
2637 SDValue LoV = getInstr(Hexagon::V6_vlalignb, dl, ty(V), {V, Z, A}, DAG);
2638 SDValue HiV = getInstr(Hexagon::V6_vlalignb, dl, ty(V), {Z, V, A}, DAG);
2639 return std::make_pair(LoV, HiV);
2640 };
2641
2642 MVT ByteTy = MVT::getVectorVT(MVT::i8, HwLen);
2643 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
2644 SDValue MaskV = DAG.getNode(HexagonISD::Q2V, dl, ByteTy, Mask);
2645 VectorPair Tmp = StoreAlign(MaskV, Base);
2646 VectorPair MaskU = {DAG.getNode(HexagonISD::V2Q, dl, BoolTy, Tmp.first),
2647 DAG.getNode(HexagonISD::V2Q, dl, BoolTy, Tmp.second)};
2648 VectorPair ValueU = StoreAlign(Value, Base);
2649
2650 SDValue Offset1 = DAG.getTargetConstant(HwLen, dl, MVT::i32);
2651 SDValue StoreLo =
2652 getInstr(StoreOpc, dl, MVT::Other,
2653 {MaskU.first, Base, Offset0, ValueU.first, Chain}, DAG);
2654 DAG.setNodeMemRefs(cast<MachineSDNode>(StoreLo.getNode()), {MemOp});
2655
2656 // If the store fits within one HwLen-aligned block, the high half's predicate
2657 // is always all-zeros and the vmem(Base+HwLen) can be elided entirely.
2658 // Proof: addr % StoreAlign == 0 and StoreMemSize <= StoreAlign implies
2659 // addr % HwLen <= HwLen - StoreAlign, so addr % HwLen + StoreMemSize
2660 // <= HwLen.
2661 // Without this guard, Hexagon v73+ probes the TLB for vmem(Base+HwLen) even
2662 // when the predicate is all-zeros, causing a TLBMISS if that page is
2663 // unmapped.
2664 uint64_t StoreMemSize = MaskN->getMemoryVT().getStoreSize().getFixedValue();
2665 if (StoreMemSize <= MaskN->getAlign().value())
2666 return StoreLo;
2667
2668 SDValue StoreHi =
2669 getInstr(StoreOpc, dl, MVT::Other,
2670 {MaskU.second, Base, Offset1, ValueU.second, Chain}, DAG);
2671 DAG.setNodeMemRefs(cast<MachineSDNode>(StoreHi.getNode()), {MemOp});
2672 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, {StoreLo, StoreHi});
2673}
2674
2675SDValue HexagonTargetLowering::LowerHvxFpExtend(SDValue Op,
2676 SelectionDAG &DAG) const {
2677 // This conversion only applies to QFloat. IEEE extension from f16 to f32
2678 // is legal (done via a pattern).
2679 assert(Subtarget.useHVXQFloatOps());
2680
2681 assert(Op->getOpcode() == ISD::FP_EXTEND);
2682
2683 MVT VecTy = ty(Op);
2684 MVT ArgTy = ty(Op.getOperand(0));
2685 const SDLoc &dl(Op);
2686
2687 if (ArgTy == MVT::v64bf16) {
2688 MVT HalfTy = typeSplit(VecTy).first;
2689 SDValue BF16Vec = Op.getOperand(0);
2690 SDValue Zeroes =
2691 getInstr(Hexagon::V6_vxor, dl, HalfTy, {BF16Vec, BF16Vec}, DAG);
2692 // Interleave zero vector with the bf16 vector, with zeroes in the lower
2693 // half of each 32 bit lane, effectively extending the bf16 values to fp32
2694 // values.
2695 SDValue ShuffVec =
2696 getInstr(Hexagon::V6_vshufoeh, dl, VecTy, {BF16Vec, Zeroes}, DAG);
2697 VectorPair VecPair = opSplit(ShuffVec, dl, DAG);
2698 SDValue Result = getInstr(Hexagon::V6_vshuffvdd, dl, VecTy,
2699 {VecPair.second, VecPair.first,
2700 DAG.getSignedConstant(-4, dl, MVT::i32)},
2701 DAG);
2702 return Result;
2703 }
2704
2705 assert(VecTy == MVT::v64f32 && ArgTy == MVT::v64f16);
2706
2707 SDValue F16Vec = Op.getOperand(0);
2708
2709 APFloat FloatVal = APFloat(1.0f);
2710 bool Ignored;
2712 SDValue Fp16Ones = DAG.getConstantFP(FloatVal, dl, ArgTy);
2713 SDValue VmpyVec =
2714 getInstr(Hexagon::V6_vmpy_qf32_hf, dl, VecTy, {F16Vec, Fp16Ones}, DAG);
2715
2716 MVT HalfTy = typeSplit(VecTy).first;
2717 VectorPair Pair = opSplit(VmpyVec, dl, DAG);
2718 SDValue LoVec =
2719 getInstr(Hexagon::V6_vconv_sf_qf32, dl, HalfTy, {Pair.first}, DAG);
2720 SDValue HiVec =
2721 getInstr(Hexagon::V6_vconv_sf_qf32, dl, HalfTy, {Pair.second}, DAG);
2722
2723 SDValue ShuffVec =
2724 getInstr(Hexagon::V6_vshuffvdd, dl, VecTy,
2725 {HiVec, LoVec, DAG.getSignedConstant(-4, dl, MVT::i32)}, DAG);
2726
2727 return ShuffVec;
2728}
2729
2730SDValue
2731HexagonTargetLowering::LowerHvxFpToInt(SDValue Op, SelectionDAG &DAG) const {
2732 // Catch invalid conversion ops (just in case).
2733 assert(Op.getOpcode() == ISD::FP_TO_SINT ||
2734 Op.getOpcode() == ISD::FP_TO_UINT);
2735
2736 MVT ResTy = ty(Op);
2737 MVT FpTy = ty(Op.getOperand(0)).getVectorElementType();
2738 MVT IntTy = ResTy.getVectorElementType();
2739
2740 if (Subtarget.useHVXIEEEFPOps()) {
2741 // There are only conversions from f16.
2742 if (FpTy == MVT::f16) {
2743 // Other int types aren't legal in HVX, so we shouldn't see them here.
2744 assert(IntTy == MVT::i8 || IntTy == MVT::i16 || IntTy == MVT::i32);
2745 // Conversions to i8 and i16 are legal.
2746 if (IntTy == MVT::i8 || IntTy == MVT::i16)
2747 return Op;
2748 }
2749 }
2750
2751 if (IntTy.getSizeInBits() != FpTy.getSizeInBits())
2752 return EqualizeFpIntConversion(Op, DAG);
2753
2754 return ExpandHvxFpToInt(Op, DAG);
2755}
2756
2757// For vector type v32i1 uint_to_fp/sint_to_fp to v32f32:
2758// R1 = #1, R2 holds the v32i1 param
2759// V1 = vsplat(R1)
2760// V2 = vsplat(R2)
2761// Q0 = vand(V1,R1)
2762// V0.w=prefixsum(Q0)
2763// V0.w=vsub(V0.w,V1.w)
2764// V2.w = vlsr(V2.w,V0.w)
2765// V2 = vand(V2,V1)
2766// V2.sf = V2.w
2767SDValue HexagonTargetLowering::LowerHvxPred32ToFp(SDValue PredOp,
2768 SelectionDAG &DAG) const {
2769
2770 MVT ResTy = ty(PredOp);
2771 const SDLoc &dl(PredOp);
2772
2773 SDValue Const = DAG.getTargetConstant(0x1, dl, MVT::i32);
2774 SDNode *RegConst = DAG.getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32, Const);
2775 SDNode *SplatConst = DAG.getMachineNode(Hexagon::V6_lvsplatw, dl, MVT::v32i32,
2776 SDValue(RegConst, 0));
2777 SDNode *PredTransfer =
2778 DAG.getMachineNode(Hexagon::V6_vandvrt, dl, MVT::v32i1,
2779 SDValue(SplatConst, 0), SDValue(RegConst, 0));
2780 SDNode *PrefixSum = DAG.getMachineNode(Hexagon::V6_vprefixqw, dl, MVT::v32i32,
2781 SDValue(PredTransfer, 0));
2782 SDNode *SplatParam = DAG.getMachineNode(
2783 Hexagon::V6_lvsplatw, dl, MVT::v32i32,
2784 DAG.getNode(ISD::BITCAST, dl, MVT::i32, PredOp.getOperand(0)));
2785 SDNode *Vsub =
2786 DAG.getMachineNode(Hexagon::V6_vsubw, dl, MVT::v32i32,
2787 SDValue(PrefixSum, 0), SDValue(SplatConst, 0));
2788 SDNode *IndexShift =
2789 DAG.getMachineNode(Hexagon::V6_vlsrwv, dl, MVT::v32i32,
2790 SDValue(SplatParam, 0), SDValue(Vsub, 0));
2791 SDNode *MaskOff =
2792 DAG.getMachineNode(Hexagon::V6_vand, dl, MVT::v32i32,
2793 SDValue(IndexShift, 0), SDValue(SplatConst, 0));
2794 SDNode *Convert = DAG.getMachineNode(Hexagon::V6_vconv_sf_w, dl, ResTy,
2795 SDValue(MaskOff, 0));
2796 return SDValue(Convert, 0);
2797}
2798
2799// For vector type v64i1 uint_to_fo to v64f16:
2800// i64 R32 = bitcast v64i1 R3:2 (R3:2 holds v64i1)
2801// R3 = subreg_high (R32)
2802// R2 = subreg_low (R32)
2803// R1 = #1
2804// V1 = vsplat(R1)
2805// V2 = vsplat(R2)
2806// V3 = vsplat(R3)
2807// Q0 = vand(V1,R1)
2808// V0.w=prefixsum(Q0)
2809// V0.w=vsub(V0.w,V1.w)
2810// V2.w = vlsr(V2.w,V0.w)
2811// V3.w = vlsr(V3.w,V0.w)
2812// V2 = vand(V2,V1)
2813// V3 = vand(V3,V1)
2814// V2.h = vpacke(V3.w,V2.w)
2815// V2.hf = V2.h
2816SDValue HexagonTargetLowering::LowerHvxPred64ToFp(SDValue PredOp,
2817 SelectionDAG &DAG) const {
2818
2819 MVT ResTy = ty(PredOp);
2820 const SDLoc &dl(PredOp);
2821
2822 SDValue Inp = DAG.getNode(ISD::BITCAST, dl, MVT::i64, PredOp.getOperand(0));
2823 // Get the hi and lo regs
2824 SDValue HiReg =
2825 DAG.getTargetExtractSubreg(Hexagon::isub_hi, dl, MVT::i32, Inp);
2826 SDValue LoReg =
2827 DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, Inp);
2828 // Get constant #1 and splat into vector V1
2829 SDValue Const = DAG.getTargetConstant(0x1, dl, MVT::i32);
2830 SDNode *RegConst = DAG.getMachineNode(Hexagon::A2_tfrsi, dl, MVT::i32, Const);
2831 SDNode *SplatConst = DAG.getMachineNode(Hexagon::V6_lvsplatw, dl, MVT::v32i32,
2832 SDValue(RegConst, 0));
2833 // Splat the hi and lo args
2834 SDNode *SplatHi =
2835 DAG.getMachineNode(Hexagon::V6_lvsplatw, dl, MVT::v32i32,
2836 DAG.getNode(ISD::BITCAST, dl, MVT::i32, HiReg));
2837 SDNode *SplatLo =
2838 DAG.getMachineNode(Hexagon::V6_lvsplatw, dl, MVT::v32i32,
2839 DAG.getNode(ISD::BITCAST, dl, MVT::i32, LoReg));
2840 // vand between splatted const and const
2841 SDNode *PredTransfer =
2842 DAG.getMachineNode(Hexagon::V6_vandvrt, dl, MVT::v32i1,
2843 SDValue(SplatConst, 0), SDValue(RegConst, 0));
2844 // Get the prefixsum
2845 SDNode *PrefixSum = DAG.getMachineNode(Hexagon::V6_vprefixqw, dl, MVT::v32i32,
2846 SDValue(PredTransfer, 0));
2847 // Get the vsub
2848 SDNode *Vsub =
2849 DAG.getMachineNode(Hexagon::V6_vsubw, dl, MVT::v32i32,
2850 SDValue(PrefixSum, 0), SDValue(SplatConst, 0));
2851 // Get vlsr for hi and lo
2852 SDNode *IndexShift_hi =
2853 DAG.getMachineNode(Hexagon::V6_vlsrwv, dl, MVT::v32i32,
2854 SDValue(SplatHi, 0), SDValue(Vsub, 0));
2855 SDNode *IndexShift_lo =
2856 DAG.getMachineNode(Hexagon::V6_vlsrwv, dl, MVT::v32i32,
2857 SDValue(SplatLo, 0), SDValue(Vsub, 0));
2858 // Get vand of hi and lo
2859 SDNode *MaskOff_hi =
2860 DAG.getMachineNode(Hexagon::V6_vand, dl, MVT::v32i32,
2861 SDValue(IndexShift_hi, 0), SDValue(SplatConst, 0));
2862 SDNode *MaskOff_lo =
2863 DAG.getMachineNode(Hexagon::V6_vand, dl, MVT::v32i32,
2864 SDValue(IndexShift_lo, 0), SDValue(SplatConst, 0));
2865 // Pack them
2866 SDNode *Pack =
2867 DAG.getMachineNode(Hexagon::V6_vpackeh, dl, MVT::v64i16,
2868 SDValue(MaskOff_hi, 0), SDValue(MaskOff_lo, 0));
2869 SDNode *Convert =
2870 DAG.getMachineNode(Hexagon::V6_vconv_hf_h, dl, ResTy, SDValue(Pack, 0));
2871 return SDValue(Convert, 0);
2872}
2873
2874SDValue
2875HexagonTargetLowering::LowerHvxIntToFp(SDValue Op, SelectionDAG &DAG) const {
2876 // Catch invalid conversion ops (just in case).
2877 assert(Op.getOpcode() == ISD::SINT_TO_FP ||
2878 Op.getOpcode() == ISD::UINT_TO_FP);
2879
2880 MVT ResTy = ty(Op);
2881 MVT IntTy = ty(Op.getOperand(0)).getVectorElementType();
2882 MVT FpTy = ResTy.getVectorElementType();
2883
2884 if (Op.getOpcode() == ISD::UINT_TO_FP || Op.getOpcode() == ISD::SINT_TO_FP) {
2885 if (ResTy == MVT::v32f32 && ty(Op.getOperand(0)) == MVT::v32i1)
2886 return LowerHvxPred32ToFp(Op, DAG);
2887 if (ResTy == MVT::v64f16 && ty(Op.getOperand(0)) == MVT::v64i1)
2888 return LowerHvxPred64ToFp(Op, DAG);
2889 }
2890
2891 if (Subtarget.useHVXIEEEFPOps()) {
2892 // There are only conversions to f16.
2893 if (FpTy == MVT::f16) {
2894 // Other int types aren't legal in HVX, so we shouldn't see them here.
2895 assert(IntTy == MVT::i8 || IntTy == MVT::i16 || IntTy == MVT::i32);
2896 // i8, i16 -> f16 is legal.
2897 if (IntTy == MVT::i8 || IntTy == MVT::i16)
2898 return Op;
2899 }
2900 }
2901
2902 if (IntTy.getSizeInBits() != FpTy.getSizeInBits())
2903 return EqualizeFpIntConversion(Op, DAG);
2904
2905 return ExpandHvxIntToFp(Op, DAG);
2906}
2907
2908HexagonTargetLowering::TypePair
2909HexagonTargetLowering::typeExtendToWider(MVT Ty0, MVT Ty1) const {
2910 // Compare the widths of elements of the two types, and extend the narrower
2911 // type to match the with of the wider type. For vector types, apply this
2912 // to the element type.
2913 assert(Ty0.isVector() == Ty1.isVector());
2914
2915 MVT ElemTy0 = Ty0.getScalarType();
2916 MVT ElemTy1 = Ty1.getScalarType();
2917
2918 unsigned Width0 = ElemTy0.getSizeInBits();
2919 unsigned Width1 = ElemTy1.getSizeInBits();
2920 unsigned MaxWidth = std::max(Width0, Width1);
2921
2922 auto getScalarWithWidth = [](MVT ScalarTy, unsigned Width) {
2923 if (ScalarTy.isInteger())
2924 return MVT::getIntegerVT(Width);
2925 assert(ScalarTy.isFloatingPoint());
2926 return MVT::getFloatingPointVT(Width);
2927 };
2928
2929 MVT WideETy0 = getScalarWithWidth(ElemTy0, MaxWidth);
2930 MVT WideETy1 = getScalarWithWidth(ElemTy1, MaxWidth);
2931
2932 if (!Ty0.isVector()) {
2933 // Both types are scalars.
2934 return {WideETy0, WideETy1};
2935 }
2936
2937 // Vector types.
2938 unsigned NumElem = Ty0.getVectorNumElements();
2939 assert(NumElem == Ty1.getVectorNumElements());
2940
2941 return {MVT::getVectorVT(WideETy0, NumElem),
2942 MVT::getVectorVT(WideETy1, NumElem)};
2943}
2944
2945HexagonTargetLowering::TypePair
2946HexagonTargetLowering::typeWidenToWider(MVT Ty0, MVT Ty1) const {
2947 // Compare the numbers of elements of two vector types, and widen the
2948 // narrower one to match the number of elements in the wider one.
2949 assert(Ty0.isVector() && Ty1.isVector());
2950
2951 unsigned Len0 = Ty0.getVectorNumElements();
2952 unsigned Len1 = Ty1.getVectorNumElements();
2953 if (Len0 == Len1)
2954 return {Ty0, Ty1};
2955
2956 unsigned MaxLen = std::max(Len0, Len1);
2957 return {MVT::getVectorVT(Ty0.getVectorElementType(), MaxLen),
2958 MVT::getVectorVT(Ty1.getVectorElementType(), MaxLen)};
2959}
2960
2961MVT
2962HexagonTargetLowering::typeLegalize(MVT Ty, SelectionDAG &DAG) const {
2963 EVT LegalTy = getTypeToTransformTo(*DAG.getContext(), Ty);
2964 assert(LegalTy.isSimple());
2965 return LegalTy.getSimpleVT();
2966}
2967
2968MVT
2969HexagonTargetLowering::typeWidenToHvx(MVT Ty) const {
2970 unsigned HwWidth = 8 * Subtarget.getVectorLength();
2971 assert(Ty.getSizeInBits() <= HwWidth);
2972 if (Ty.getSizeInBits() == HwWidth)
2973 return Ty;
2974
2975 MVT ElemTy = Ty.getScalarType();
2976 return MVT::getVectorVT(ElemTy, HwWidth / ElemTy.getSizeInBits());
2977}
2978
2979HexagonTargetLowering::VectorPair
2980HexagonTargetLowering::emitHvxAddWithOverflow(SDValue A, SDValue B,
2981 const SDLoc &dl, bool Signed, SelectionDAG &DAG) const {
2982 // Compute A+B, return {A+B, O}, where O = vector predicate indicating
2983 // whether an overflow has occurred.
2984 MVT ResTy = ty(A);
2985 assert(ResTy == ty(B));
2986 MVT PredTy = MVT::getVectorVT(MVT::i1, ResTy.getVectorNumElements());
2987
2988 if (!Signed) {
2989 // V62+ has V6_vaddcarry, but it requires input predicate, so it doesn't
2990 // save any instructions.
2991 SDValue Add = DAG.getNode(ISD::ADD, dl, ResTy, {A, B});
2992 SDValue Ovf = DAG.getSetCC(dl, PredTy, Add, A, ISD::SETULT);
2993 return {Add, Ovf};
2994 }
2995
2996 // Signed overflow has happened, if:
2997 // (A, B have the same sign) and (A+B has a different sign from either)
2998 // i.e. (~A xor B) & ((A+B) xor B), then check the sign bit
2999 SDValue Add = DAG.getNode(ISD::ADD, dl, ResTy, {A, B});
3000 SDValue NotA =
3001 DAG.getNode(ISD::XOR, dl, ResTy, {A, DAG.getAllOnesConstant(dl, ResTy)});
3002 SDValue Xor0 = DAG.getNode(ISD::XOR, dl, ResTy, {NotA, B});
3003 SDValue Xor1 = DAG.getNode(ISD::XOR, dl, ResTy, {Add, B});
3004 SDValue And = DAG.getNode(ISD::AND, dl, ResTy, {Xor0, Xor1});
3005 SDValue MSB =
3006 DAG.getSetCC(dl, PredTy, And, getZero(dl, ResTy, DAG), ISD::SETLT);
3007 return {Add, MSB};
3008}
3009
3010HexagonTargetLowering::VectorPair
3011HexagonTargetLowering::emitHvxShiftRightRnd(SDValue Val, unsigned Amt,
3012 bool Signed, SelectionDAG &DAG) const {
3013 // Shift Val right by Amt bits, round the result to the nearest integer,
3014 // tie-break by rounding halves to even integer.
3015
3016 const SDLoc &dl(Val);
3017 MVT ValTy = ty(Val);
3018
3019 // This should also work for signed integers.
3020 //
3021 // uint tmp0 = inp + ((1 << (Amt-1)) - 1);
3022 // bool ovf = (inp > tmp0);
3023 // uint rup = inp & (1 << (Amt+1));
3024 //
3025 // uint tmp1 = inp >> (Amt-1); // tmp1 == tmp2 iff
3026 // uint tmp2 = tmp0 >> (Amt-1); // the Amt-1 lower bits were all 0
3027 // uint tmp3 = tmp2 + rup;
3028 // uint frac = (tmp1 != tmp2) ? tmp2 >> 1 : tmp3 >> 1;
3029 unsigned ElemWidth = ValTy.getVectorElementType().getSizeInBits();
3030 MVT ElemTy = MVT::getIntegerVT(ElemWidth);
3031 MVT IntTy = tyVector(ValTy, ElemTy);
3032 MVT PredTy = MVT::getVectorVT(MVT::i1, IntTy.getVectorNumElements());
3033 unsigned ShRight = Signed ? ISD::SRA : ISD::SRL;
3034
3035 SDValue Inp = DAG.getBitcast(IntTy, Val);
3036 SDValue LowBits = DAG.getConstant((1ull << (Amt - 1)) - 1, dl, IntTy);
3037
3038 SDValue AmtP1 = DAG.getConstant(1ull << Amt, dl, IntTy);
3039 SDValue And = DAG.getNode(ISD::AND, dl, IntTy, {Inp, AmtP1});
3040 SDValue Zero = getZero(dl, IntTy, DAG);
3041 SDValue Bit = DAG.getSetCC(dl, PredTy, And, Zero, ISD::SETNE);
3042 SDValue Rup = DAG.getZExtOrTrunc(Bit, dl, IntTy);
3043 auto [Tmp0, Ovf] = emitHvxAddWithOverflow(Inp, LowBits, dl, Signed, DAG);
3044
3045 SDValue AmtM1 = DAG.getConstant(Amt - 1, dl, IntTy);
3046 SDValue Tmp1 = DAG.getNode(ShRight, dl, IntTy, Inp, AmtM1);
3047 SDValue Tmp2 = DAG.getNode(ShRight, dl, IntTy, Tmp0, AmtM1);
3048 SDValue Tmp3 = DAG.getNode(ISD::ADD, dl, IntTy, Tmp2, Rup);
3049
3050 SDValue Eq = DAG.getSetCC(dl, PredTy, Tmp1, Tmp2, ISD::SETEQ);
3051 SDValue One = DAG.getConstant(1, dl, IntTy);
3052 SDValue Tmp4 = DAG.getNode(ShRight, dl, IntTy, {Tmp2, One});
3053 SDValue Tmp5 = DAG.getNode(ShRight, dl, IntTy, {Tmp3, One});
3054 SDValue Mux = DAG.getNode(ISD::VSELECT, dl, IntTy, {Eq, Tmp5, Tmp4});
3055 return {Mux, Ovf};
3056}
3057
3058SDValue
3059HexagonTargetLowering::emitHvxMulHsV60(SDValue A, SDValue B, const SDLoc &dl,
3060 SelectionDAG &DAG) const {
3061 MVT VecTy = ty(A);
3062 MVT PairTy = typeJoin({VecTy, VecTy});
3063 assert(VecTy.getVectorElementType() == MVT::i32);
3064
3065 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
3066
3067 // mulhs(A,B) =
3068 // = [(Hi(A)*2^16 + Lo(A)) *s (Hi(B)*2^16 + Lo(B))] >> 32
3069 // = [Hi(A)*2^16 *s Hi(B)*2^16 + Hi(A) *su Lo(B)*2^16
3070 // + Lo(A) *us (Hi(B)*2^16 + Lo(B))] >> 32
3071 // = [Hi(A) *s Hi(B)*2^32 + Hi(A) *su Lo(B)*2^16 + Lo(A) *us B] >> 32
3072 // The low half of Lo(A)*Lo(B) will be discarded (it's not added to
3073 // anything, so it cannot produce any carry over to higher bits),
3074 // so everything in [] can be shifted by 16 without loss of precision.
3075 // = [Hi(A) *s Hi(B)*2^16 + Hi(A)*su Lo(B) + Lo(A)*B >> 16] >> 16
3076 // = [Hi(A) *s Hi(B)*2^16 + Hi(A)*su Lo(B) + V6_vmpyewuh(A,B)] >> 16
3077 // The final additions need to make sure to properly maintain any carry-
3078 // out bits.
3079 //
3080 // Hi(B) Lo(B)
3081 // Hi(A) Lo(A)
3082 // --------------
3083 // Lo(B)*Lo(A) | T0 = V6_vmpyewuh(B,A) does this,
3084 // Hi(B)*Lo(A) | + dropping the low 16 bits
3085 // Hi(A)*Lo(B) | T2
3086 // Hi(B)*Hi(A)
3087
3088 SDValue T0 = getInstr(Hexagon::V6_vmpyewuh, dl, VecTy, {B, A}, DAG);
3089 // T1 = get Hi(A) into low halves.
3090 SDValue T1 = getInstr(Hexagon::V6_vasrw, dl, VecTy, {A, S16}, DAG);
3091 // P0 = interleaved T1.h*B.uh (full precision product)
3092 SDValue P0 = getInstr(Hexagon::V6_vmpyhus, dl, PairTy, {T1, B}, DAG);
3093 // T2 = T1.even(h) * B.even(uh), i.e. Hi(A)*Lo(B)
3094 SDValue T2 = LoHalf(P0, DAG);
3095 // We need to add T0+T2, recording the carry-out, which will be 1<<16
3096 // added to the final sum.
3097 // P1 = interleaved even/odd 32-bit (unsigned) sums of 16-bit halves
3098 SDValue P1 = getInstr(Hexagon::V6_vadduhw, dl, PairTy, {T0, T2}, DAG);
3099 // P2 = interleaved even/odd 32-bit (signed) sums of 16-bit halves
3100 SDValue P2 = getInstr(Hexagon::V6_vaddhw, dl, PairTy, {T0, T2}, DAG);
3101 // T3 = full-precision(T0+T2) >> 16
3102 // The low halves are added-unsigned, the high ones are added-signed.
3103 SDValue T3 = getInstr(Hexagon::V6_vasrw_acc, dl, VecTy,
3104 {HiHalf(P2, DAG), LoHalf(P1, DAG), S16}, DAG);
3105 SDValue T4 = getInstr(Hexagon::V6_vasrw, dl, VecTy, {B, S16}, DAG);
3106 // P3 = interleaved Hi(B)*Hi(A) (full precision),
3107 // which is now Lo(T1)*Lo(T4), so we want to keep the even product.
3108 SDValue P3 = getInstr(Hexagon::V6_vmpyhv, dl, PairTy, {T1, T4}, DAG);
3109 SDValue T5 = LoHalf(P3, DAG);
3110 // Add:
3111 SDValue T6 = DAG.getNode(ISD::ADD, dl, VecTy, {T3, T5});
3112 return T6;
3113}
3114
3115SDValue
3116HexagonTargetLowering::emitHvxMulLoHiV60(SDValue A, bool SignedA, SDValue B,
3117 bool SignedB, const SDLoc &dl,
3118 SelectionDAG &DAG) const {
3119 MVT VecTy = ty(A);
3120 MVT PairTy = typeJoin({VecTy, VecTy});
3121 assert(VecTy.getVectorElementType() == MVT::i32);
3122
3123 SDValue S16 = DAG.getConstant(16, dl, MVT::i32);
3124
3125 if (SignedA && !SignedB) {
3126 // Make A:unsigned, B:signed.
3127 std::swap(A, B);
3128 std::swap(SignedA, SignedB);
3129 }
3130
3131 // Do halfword-wise multiplications for unsigned*unsigned product, then
3132 // add corrections for signed and unsigned*signed.
3133
3134 SDValue Lo, Hi;
3135
3136 // P0:lo = (uu) products of low halves of A and B,
3137 // P0:hi = (uu) products of high halves.
3138 SDValue P0 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {A, B}, DAG);
3139
3140 // Swap low/high halves in B
3141 SDValue T0 = getInstr(Hexagon::V6_lvsplatw, dl, VecTy,
3142 {DAG.getConstant(0x02020202, dl, MVT::i32)}, DAG);
3143 SDValue T1 = getInstr(Hexagon::V6_vdelta, dl, VecTy, {B, T0}, DAG);
3144 // P1 = products of even/odd halfwords.
3145 // P1:lo = (uu) products of even(A.uh) * odd(B.uh)
3146 // P1:hi = (uu) products of odd(A.uh) * even(B.uh)
3147 SDValue P1 = getInstr(Hexagon::V6_vmpyuhv, dl, PairTy, {A, T1}, DAG);
3148
3149 // P2:lo = low halves of P1:lo + P1:hi,
3150 // P2:hi = high halves of P1:lo + P1:hi.
3151 SDValue P2 = getInstr(Hexagon::V6_vadduhw, dl, PairTy,
3152 {HiHalf(P1, DAG), LoHalf(P1, DAG)}, DAG);
3153 // Still need to add the high halves of P0:lo to P2:lo
3154 SDValue T2 =
3155 getInstr(Hexagon::V6_vlsrw, dl, VecTy, {LoHalf(P0, DAG), S16}, DAG);
3156 SDValue T3 = DAG.getNode(ISD::ADD, dl, VecTy, {LoHalf(P2, DAG), T2});
3157
3158 // The high halves of T3 will contribute to the HI part of LOHI.
3159 SDValue T4 = getInstr(Hexagon::V6_vasrw_acc, dl, VecTy,
3160 {HiHalf(P2, DAG), T3, S16}, DAG);
3161
3162 // The low halves of P2 need to be added to high halves of the LO part.
3163 Lo = getInstr(Hexagon::V6_vaslw_acc, dl, VecTy,
3164 {LoHalf(P0, DAG), LoHalf(P2, DAG), S16}, DAG);
3165 Hi = DAG.getNode(ISD::ADD, dl, VecTy, {HiHalf(P0, DAG), T4});
3166
3167 if (SignedA) {
3168 assert(SignedB && "Signed A and unsigned B should have been inverted");
3169
3170 MVT PredTy = MVT::getVectorVT(MVT::i1, VecTy.getVectorNumElements());
3171 SDValue Zero = getZero(dl, VecTy, DAG);
3172 SDValue Q0 = DAG.getSetCC(dl, PredTy, A, Zero, ISD::SETLT);
3173 SDValue Q1 = DAG.getSetCC(dl, PredTy, B, Zero, ISD::SETLT);
3174 SDValue X0 = DAG.getNode(ISD::VSELECT, dl, VecTy, {Q0, B, Zero});
3175 SDValue X1 = getInstr(Hexagon::V6_vaddwq, dl, VecTy, {Q1, X0, A}, DAG);
3176 Hi = getInstr(Hexagon::V6_vsubw, dl, VecTy, {Hi, X1}, DAG);
3177 } else if (SignedB) {
3178 // Same correction as for mulhus:
3179 // mulhus(A.uw,B.w) = mulhu(A.uw,B.uw) - (A.w if B < 0)
3180 MVT PredTy = MVT::getVectorVT(MVT::i1, VecTy.getVectorNumElements());
3181 SDValue Zero = getZero(dl, VecTy, DAG);
3182 SDValue Q1 = DAG.getSetCC(dl, PredTy, B, Zero, ISD::SETLT);
3183 Hi = getInstr(Hexagon::V6_vsubwq, dl, VecTy, {Q1, Hi, A}, DAG);
3184 } else {
3185 assert(!SignedA && !SignedB);
3186 }
3187
3188 return DAG.getMergeValues({Lo, Hi}, dl);
3189}
3190
3191SDValue
3192HexagonTargetLowering::emitHvxMulLoHiV62(SDValue A, bool SignedA,
3193 SDValue B, bool SignedB,
3194 const SDLoc &dl,
3195 SelectionDAG &DAG) const {
3196 MVT VecTy = ty(A);
3197 MVT PairTy = typeJoin({VecTy, VecTy});
3198 assert(VecTy.getVectorElementType() == MVT::i32);
3199
3200 if (SignedA && !SignedB) {
3201 // Make A:unsigned, B:signed.
3202 std::swap(A, B);
3203 std::swap(SignedA, SignedB);
3204 }
3205
3206 // Do S*S first, then make corrections for U*S or U*U if needed.
3207 SDValue P0 = getInstr(Hexagon::V6_vmpyewuh_64, dl, PairTy, {A, B}, DAG);
3208 SDValue P1 =
3209 getInstr(Hexagon::V6_vmpyowh_64_acc, dl, PairTy, {P0, A, B}, DAG);
3210 SDValue Lo = LoHalf(P1, DAG);
3211 SDValue Hi = HiHalf(P1, DAG);
3212
3213 if (!SignedB) {
3214 assert(!SignedA && "Signed A and unsigned B should have been inverted");
3215 SDValue Zero = getZero(dl, VecTy, DAG);
3216 MVT PredTy = MVT::getVectorVT(MVT::i1, VecTy.getVectorNumElements());
3217
3218 // Mulhu(X, Y) = Mulhs(X, Y) + (X, if Y < 0) + (Y, if X < 0).
3219 // def: Pat<(VecI32 (mulhu HVI32:$A, HVI32:$B)),
3220 // (V6_vaddw (HiHalf (Muls64O $A, $B)),
3221 // (V6_vaddwq (V6_vgtw (V6_vd0), $B),
3222 // (V6_vandvqv (V6_vgtw (V6_vd0), $A), $B),
3223 // $A))>;
3224 SDValue Q0 = DAG.getSetCC(dl, PredTy, A, Zero, ISD::SETLT);
3225 SDValue Q1 = DAG.getSetCC(dl, PredTy, B, Zero, ISD::SETLT);
3226 SDValue T0 = getInstr(Hexagon::V6_vandvqv, dl, VecTy, {Q0, B}, DAG);
3227 SDValue T1 = getInstr(Hexagon::V6_vaddwq, dl, VecTy, {Q1, T0, A}, DAG);
3228 Hi = getInstr(Hexagon::V6_vaddw, dl, VecTy, {Hi, T1}, DAG);
3229 } else if (!SignedA) {
3230 SDValue Zero = getZero(dl, VecTy, DAG);
3231 MVT PredTy = MVT::getVectorVT(MVT::i1, VecTy.getVectorNumElements());
3232
3233 // Mulhus(unsigned X, signed Y) = Mulhs(X, Y) + (Y, if X < 0).
3234 // def: Pat<(VecI32 (HexagonMULHUS HVI32:$A, HVI32:$B)),
3235 // (V6_vaddwq (V6_vgtw (V6_vd0), $A),
3236 // (HiHalf (Muls64O $A, $B)),
3237 // $B)>;
3238 SDValue Q0 = DAG.getSetCC(dl, PredTy, A, Zero, ISD::SETLT);
3239 Hi = getInstr(Hexagon::V6_vaddwq, dl, VecTy, {Q0, Hi, B}, DAG);
3240 }
3241
3242 return DAG.getMergeValues({Lo, Hi}, dl);
3243}
3244
3245SDValue
3246HexagonTargetLowering::EqualizeFpIntConversion(SDValue Op, SelectionDAG &DAG)
3247 const {
3248 // Rewrite conversion between integer and floating-point in such a way that
3249 // the integer type is extended/narrowed to match the bitwidth of the
3250 // floating-point type, combined with additional integer-integer extensions
3251 // or narrowings to match the original input/result types.
3252 // E.g. f32 -> i8 ==> f32 -> i32 -> i8
3253 //
3254 // The input/result types are not required to be legal, but if they are
3255 // legal, this function should not introduce illegal types.
3256
3257 unsigned Opc = Op.getOpcode();
3260
3261 SDValue Inp = Op.getOperand(0);
3262 MVT InpTy = ty(Inp);
3263 MVT ResTy = ty(Op);
3264
3265 if (InpTy == ResTy)
3266 return Op;
3267
3268 const SDLoc &dl(Op);
3270
3271 auto [WInpTy, WResTy] = typeExtendToWider(InpTy, ResTy);
3272 SDValue WInp = resizeToWidth(Inp, WInpTy, Signed, dl, DAG);
3273 SDValue Conv = DAG.getNode(Opc, dl, WResTy, WInp);
3274 SDValue Res = resizeToWidth(Conv, ResTy, Signed, dl, DAG);
3275 return Res;
3276}
3277
3278SDValue
3279HexagonTargetLowering::ExpandHvxFpToInt(SDValue Op, SelectionDAG &DAG) const {
3280 unsigned Opc = Op.getOpcode();
3282
3283 const SDLoc &dl(Op);
3284 SDValue Op0 = Op.getOperand(0);
3285 MVT InpTy = ty(Op0);
3286 MVT ResTy = ty(Op);
3287 assert(InpTy.changeTypeToInteger() == ResTy);
3288
3289 // At this point this is an experiment under a flag.
3290 // In arch before V81 the rounding mode is towards nearest value.
3291 // The C/C++ standard requires rounding towards zero:
3292 // C (C99 and later): ISO/IEC 9899:2018 (C18), section 6.3.1.4 — "When a
3293 // finite value of real floating type is converted to an integer type, the
3294 // fractional part is discarded (i.e., the value is truncated toward zero)."
3295 // C++: ISO/IEC 14882:2020 (C++20), section 7.3.7 — "A prvalue of a
3296 // floating-point type can be converted to a prvalue of an integer type. The
3297 // conversion truncates; that is, the fractional part is discarded."
3298 // The fast-convert instructions require V73+; hexagon-fp-fast-convert has no
3299 // effect below that.
3300 if (Subtarget.useHVXV73Ops()) {
3301 if (InpTy == MVT::v64f16) {
3302 if (Subtarget.useHVXV81Ops()) {
3303 // This is c/c++ compliant
3304 SDValue ConvVec =
3305 getInstr(Hexagon::V6_vconv_h_hf_rnd, dl, ResTy, {Op0}, DAG);
3306 return ConvVec;
3307 } else if (Subtarget.getCLOpts().fp_fast_convert) {
3308 // Vd32.h=Vu32.hf same as Q6_Vh_equals_Vhf
3309 SDValue ConvVec =
3310 getInstr(Hexagon::V6_vconv_h_hf, dl, ResTy, {Op0}, DAG);
3311 return ConvVec;
3312 }
3313 } else if (Subtarget.getCLOpts().fp_fast_convert && InpTy == MVT::v32f32) {
3314 // Vd32.w=Vu32.sf same as Q6_Vw_equals_Vsf
3315 SDValue ConvVec = getInstr(Hexagon::V6_vconv_w_sf, dl, ResTy, {Op0}, DAG);
3316 return ConvVec;
3317 }
3318 }
3319
3320 // int32_t conv_f32_to_i32(uint32_t inp) {
3321 // // s | exp8 | frac23
3322 //
3323 // int neg = (int32_t)inp < 0;
3324 //
3325 // // "expm1" is the actual exponent minus 1: instead of "bias", subtract
3326 // // "bias+1". When the encoded exp is "all-1" (i.e. inf/nan), this will
3327 // // produce a large positive "expm1", which will result in max u/int.
3328 // // In all IEEE formats, bias is the largest positive number that can be
3329 // // represented in bias-width bits (i.e. 011..1).
3330 // int32_t expm1 = (inp << 1) - 0x80000000;
3331 // expm1 >>= 24;
3332 //
3333 // // Always insert the "implicit 1". Subnormal numbers will become 0
3334 // // regardless.
3335 // uint32_t frac = (inp << 8) | 0x80000000;
3336 //
3337 // // "frac" is the fraction part represented as Q1.31. If it was
3338 // // interpreted as uint32_t, it would be the fraction part multiplied
3339 // // by 2^31.
3340 //
3341 // // Calculate the amount of right shift, since shifting further to the
3342 // // left would lose significant bits. Limit it to 32, because we want
3343 // // shifts by 32+ to produce 0, whereas V6_vlsrwv treats the shift
3344 // // amount as a 6-bit signed value (so 33 is same as -31, i.e. shift
3345 // // left by 31). "rsh" can be negative.
3346 // int32_t rsh = min(31 - (expm1 + 1), 32);
3347 //
3348 // frac >>= rsh; // rsh == 32 will produce 0
3349 //
3350 // // Everything up to this point is the same for conversion to signed
3351 // // unsigned integer.
3352 //
3353 // if (neg) // Only for signed int
3354 // frac = -frac; //
3355 // if (rsh <= 0 && neg) // bound = neg ? 0x80000000 : 0x7fffffff
3356 // frac = 0x80000000; // frac = rsh <= 0 ? bound : frac
3357 // if (rsh <= 0 && !neg) //
3358 // frac = 0x7fffffff; //
3359 //
3360 // if (neg) // Only for unsigned int
3361 // frac = 0; //
3362 // if (rsh < 0 && !neg) // frac = rsh < 0 ? 0x7fffffff : frac;
3363 // frac = 0x7fffffff; // frac = neg ? 0 : frac;
3364 //
3365 // return frac;
3366 // }
3367
3368 MVT PredTy = MVT::getVectorVT(MVT::i1, ResTy.getVectorElementCount());
3369
3370 // Zero = V6_vd0();
3371 // Neg = V6_vgtw(Zero, Inp);
3372 // One = V6_lvsplatw(1);
3373 // M80 = V6_lvsplatw(0x80000000);
3374 // Exp00 = V6_vaslwv(Inp, One);
3375 // Exp01 = V6_vsubw(Exp00, M80);
3376 // ExpM1 = V6_vasrw(Exp01, 24);
3377 // Frc00 = V6_vaslw(Inp, 8);
3378 // Frc01 = V6_vor(Frc00, M80);
3379 // Rsh00 = V6_vsubw(V6_lvsplatw(30), ExpM1);
3380 // Rsh01 = V6_vminw(Rsh00, V6_lvsplatw(32));
3381 // Frc02 = V6_vlsrwv(Frc01, Rsh01);
3382
3383 // if signed int:
3384 // Bnd = V6_vmux(Neg, M80, V6_lvsplatw(0x7fffffff))
3385 // Pos = V6_vgtw(Rsh01, Zero);
3386 // Frc13 = V6_vsubw(Zero, Frc02);
3387 // Frc14 = V6_vmux(Neg, Frc13, Frc02);
3388 // Int = V6_vmux(Pos, Frc14, Bnd);
3389 //
3390 // if unsigned int:
3391 // Rsn = V6_vgtw(Zero, Rsh01)
3392 // Frc23 = V6_vmux(Rsn, V6_lvsplatw(0x7fffffff), Frc02)
3393 // Int = V6_vmux(Neg, Zero, Frc23)
3394
3395 auto [ExpWidth, ExpBias, FracWidth] = getIEEEProperties(InpTy);
3396 unsigned ElemWidth = 1 + ExpWidth + FracWidth;
3397 assert((1ull << (ExpWidth - 1)) == (1 + ExpBias));
3398
3399 SDValue Inp = DAG.getBitcast(ResTy, Op0);
3400 SDValue Zero = getZero(dl, ResTy, DAG);
3401 SDValue Neg = DAG.getSetCC(dl, PredTy, Inp, Zero, ISD::SETLT);
3402 SDValue M80 = DAG.getConstant(1ull << (ElemWidth - 1), dl, ResTy);
3403 SDValue M7F = DAG.getConstant((1ull << (ElemWidth - 1)) - 1, dl, ResTy);
3404 SDValue One = DAG.getConstant(1, dl, ResTy);
3405 SDValue Exp00 = DAG.getNode(ISD::SHL, dl, ResTy, {Inp, One});
3406 SDValue Exp01 = DAG.getNode(ISD::SUB, dl, ResTy, {Exp00, M80});
3407 SDValue MNE = DAG.getConstant(ElemWidth - ExpWidth, dl, ResTy);
3408 SDValue ExpM1 = DAG.getNode(ISD::SRA, dl, ResTy, {Exp01, MNE});
3409
3410 SDValue ExpW = DAG.getConstant(ExpWidth, dl, ResTy);
3411 SDValue Frc00 = DAG.getNode(ISD::SHL, dl, ResTy, {Inp, ExpW});
3412 SDValue Frc01 = DAG.getNode(ISD::OR, dl, ResTy, {Frc00, M80});
3413
3414 SDValue MN2 = DAG.getConstant(ElemWidth - 2, dl, ResTy);
3415 SDValue Rsh00 = DAG.getNode(ISD::SUB, dl, ResTy, {MN2, ExpM1});
3416 SDValue MW = DAG.getConstant(ElemWidth, dl, ResTy);
3417 SDValue Rsh01 = DAG.getNode(ISD::SMIN, dl, ResTy, {Rsh00, MW});
3418 SDValue Frc02 = DAG.getNode(ISD::SRL, dl, ResTy, {Frc01, Rsh01});
3419
3420 SDValue Int;
3421
3422 if (Opc == ISD::FP_TO_SINT) {
3423 SDValue Bnd = DAG.getNode(ISD::VSELECT, dl, ResTy, {Neg, M80, M7F});
3424 SDValue Pos = DAG.getSetCC(dl, PredTy, Rsh01, Zero, ISD::SETGT);
3425 SDValue Frc13 = DAG.getNode(ISD::SUB, dl, ResTy, {Zero, Frc02});
3426 SDValue Frc14 = DAG.getNode(ISD::VSELECT, dl, ResTy, {Neg, Frc13, Frc02});
3427 Int = DAG.getNode(ISD::VSELECT, dl, ResTy, {Pos, Frc14, Bnd});
3428 } else {
3430 SDValue Rsn = DAG.getSetCC(dl, PredTy, Rsh01, Zero, ISD::SETLT);
3431 SDValue Frc23 = DAG.getNode(ISD::VSELECT, dl, ResTy, Rsn, M7F, Frc02);
3432 Int = DAG.getNode(ISD::VSELECT, dl, ResTy, Neg, Zero, Frc23);
3433 }
3434
3435 return Int;
3436}
3437
3438SDValue
3439HexagonTargetLowering::ExpandHvxIntToFp(SDValue Op, SelectionDAG &DAG) const {
3440 unsigned Opc = Op.getOpcode();
3442
3443 const SDLoc &dl(Op);
3444 SDValue Op0 = Op.getOperand(0);
3445 MVT InpTy = ty(Op0);
3446 MVT ResTy = ty(Op);
3447 assert(ResTy.changeTypeToInteger() == InpTy);
3448
3449 // uint32_t vnoc1_rnd(int32_t w) {
3450 // int32_t iszero = w == 0;
3451 // int32_t isneg = w < 0;
3452 // uint32_t u = __builtin_HEXAGON_A2_abs(w);
3453 //
3454 // uint32_t norm_left = __builtin_HEXAGON_S2_cl0(u) + 1;
3455 // uint32_t frac0 = (uint64_t)u << norm_left;
3456 //
3457 // // Rounding:
3458 // uint32_t frac1 = frac0 + ((1 << 8) - 1);
3459 // uint32_t renorm = (frac0 > frac1);
3460 // uint32_t rup = (int)(frac0 << 22) < 0;
3461 //
3462 // uint32_t frac2 = frac0 >> 8;
3463 // uint32_t frac3 = frac1 >> 8;
3464 // uint32_t frac = (frac2 != frac3) ? frac3 >> 1 : (frac3 + rup) >> 1;
3465 //
3466 // int32_t exp = 32 - norm_left + renorm + 127;
3467 // exp <<= 23;
3468 //
3469 // uint32_t sign = 0x80000000 * isneg;
3470 // uint32_t f = sign | exp | frac;
3471 // return iszero ? 0 : f;
3472 // }
3473
3474 MVT PredTy = MVT::getVectorVT(MVT::i1, InpTy.getVectorElementCount());
3475 bool Signed = Opc == ISD::SINT_TO_FP;
3476
3477 auto [ExpWidth, ExpBias, FracWidth] = getIEEEProperties(ResTy);
3478 unsigned ElemWidth = 1 + ExpWidth + FracWidth;
3479
3480 SDValue Zero = getZero(dl, InpTy, DAG);
3481 SDValue One = DAG.getConstant(1, dl, InpTy);
3482 SDValue IsZero = DAG.getSetCC(dl, PredTy, Op0, Zero, ISD::SETEQ);
3483 SDValue Abs = Signed ? DAG.getNode(ISD::ABS, dl, InpTy, Op0) : Op0;
3484 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, InpTy, Abs);
3485 SDValue NLeft = DAG.getNode(ISD::ADD, dl, InpTy, {Clz, One});
3486 SDValue Frac0 = DAG.getNode(ISD::SHL, dl, InpTy, {Abs, NLeft});
3487
3488 auto [Frac, Ovf] = emitHvxShiftRightRnd(Frac0, ExpWidth + 1, false, DAG);
3489 if (Signed) {
3490 SDValue IsNeg = DAG.getSetCC(dl, PredTy, Op0, Zero, ISD::SETLT);
3491 SDValue M80 = DAG.getConstant(1ull << (ElemWidth - 1), dl, InpTy);
3492 SDValue Sign = DAG.getNode(ISD::VSELECT, dl, InpTy, {IsNeg, M80, Zero});
3493 Frac = DAG.getNode(ISD::OR, dl, InpTy, {Sign, Frac});
3494 }
3495
3496 SDValue Rnrm = DAG.getZExtOrTrunc(Ovf, dl, InpTy);
3497 SDValue Exp0 = DAG.getConstant(ElemWidth + ExpBias, dl, InpTy);
3498 SDValue Exp1 = DAG.getNode(ISD::ADD, dl, InpTy, {Rnrm, Exp0});
3499 SDValue Exp2 = DAG.getNode(ISD::SUB, dl, InpTy, {Exp1, NLeft});
3500 SDValue Exp3 = DAG.getNode(ISD::SHL, dl, InpTy,
3501 {Exp2, DAG.getConstant(FracWidth, dl, InpTy)});
3502 SDValue Flt0 = DAG.getNode(ISD::OR, dl, InpTy, {Frac, Exp3});
3503 SDValue Flt1 = DAG.getNode(ISD::VSELECT, dl, InpTy, {IsZero, Zero, Flt0});
3504 SDValue Flt = DAG.getBitcast(ResTy, Flt1);
3505
3506 return Flt;
3507}
3508
3509SDValue
3510HexagonTargetLowering::CreateTLWrapper(SDValue Op, SelectionDAG &DAG) const {
3511 unsigned Opc = Op.getOpcode();
3512 unsigned TLOpc;
3513 switch (Opc) {
3514 case ISD::ANY_EXTEND:
3515 case ISD::SIGN_EXTEND:
3516 case ISD::ZERO_EXTEND:
3517 TLOpc = HexagonISD::TL_EXTEND;
3518 break;
3519 case ISD::TRUNCATE:
3521 break;
3522#ifndef NDEBUG
3523 Op.dump(&DAG);
3524#endif
3525 llvm_unreachable("Unexpected operator");
3526 }
3527
3528 const SDLoc &dl(Op);
3529 return DAG.getNode(TLOpc, dl, ty(Op), Op.getOperand(0),
3530 DAG.getUNDEF(MVT::i128), // illegal type
3531 DAG.getConstant(Opc, dl, MVT::i32));
3532}
3533
3534SDValue
3535HexagonTargetLowering::RemoveTLWrapper(SDValue Op, SelectionDAG &DAG) const {
3536 assert(Op.getOpcode() == HexagonISD::TL_EXTEND ||
3537 Op.getOpcode() == HexagonISD::TL_TRUNCATE);
3538 unsigned Opc = Op.getConstantOperandVal(2);
3539 return DAG.getNode(Opc, SDLoc(Op), ty(Op), Op.getOperand(0));
3540}
3541
3542HexagonTargetLowering::VectorPair
3543HexagonTargetLowering::SplitVectorOp(SDValue Op, SelectionDAG &DAG) const {
3544 assert(!Op.isMachineOpcode());
3545 SmallVector<SDValue, 2> OpsL, OpsH;
3546 const SDLoc &dl(Op);
3547
3548 auto SplitVTNode = [&DAG, this](const VTSDNode *N) {
3549 MVT Ty = typeSplit(N->getVT().getSimpleVT()).first;
3550 SDValue TV = DAG.getValueType(Ty);
3551 return std::make_pair(TV, TV);
3552 };
3553
3554 for (SDValue A : Op.getNode()->ops()) {
3555 auto [Lo, Hi] =
3556 ty(A).isVector() ? opSplit(A, dl, DAG) : std::make_pair(A, A);
3557 // Special case for type operand.
3558 switch (Op.getOpcode()) {
3559 case ISD::SIGN_EXTEND_INREG:
3560 case HexagonISD::SSAT:
3561 case HexagonISD::USAT:
3562 if (const auto *N = dyn_cast<const VTSDNode>(A.getNode()))
3563 std::tie(Lo, Hi) = SplitVTNode(N);
3564 break;
3565 }
3566 OpsL.push_back(Lo);
3567 OpsH.push_back(Hi);
3568 }
3569
3570 MVT ResTy = ty(Op);
3571 MVT HalfTy = typeSplit(ResTy).first;
3572 SDValue L = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsL);
3573 SDValue H = DAG.getNode(Op.getOpcode(), dl, HalfTy, OpsH);
3574 return {L, H};
3575}
3576
3577SDValue
3578HexagonTargetLowering::SplitHvxMemOp(SDValue Op, SelectionDAG &DAG) const {
3579 auto *MemN = cast<MemSDNode>(Op.getNode());
3580 unsigned MemOpc = MemN->getOpcode();
3581 EVT MemTy = MemN->getMemoryVT();
3582
3583 if ((MemOpc == ISD::STORE || MemOpc == ISD::LOAD) &&
3584 (!MemTy.isSimple() || !isHvxPairTy(MemTy.getSimpleVT())))
3585 return Op;
3586
3587 EVT ValueType;
3588 if (MemOpc == ISD::STORE)
3590 else if (MemOpc == ISD::MSTORE)
3592 else // ISD::LOAD, ISD::MLOAD.
3593 ValueType = MemN->getValueType(0);
3594
3595 EVT LoVT, HiVT;
3596 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(ValueType);
3597
3598 EVT LoMemVT, HiMemVT;
3599 bool HiIsEmpty = false;
3600 std::tie(LoMemVT, HiMemVT) =
3601 DAG.GetDependentSplitDestVTs(MemTy, LoVT, &HiIsEmpty);
3602
3603 uint64_t LoSize = LoMemVT.getSizeInBits().getFixedValue() / 8;
3604 uint64_t HiSize = HiMemVT.getSizeInBits().getFixedValue() / 8;
3605
3606 const SDLoc &dl(Op);
3607 SDValue Chain = MemN->getChain();
3608 SDValue Base0 = MemN->getBasePtr();
3609 SDValue Base1 =
3610 DAG.getMemBasePlusOffset(Base0, TypeSize::getFixed(LoSize), dl);
3611
3612 MachineMemOperand *MOp0 = nullptr, *MOp1 = nullptr;
3613 if (MachineMemOperand *MMO = MemN->getMemOperand()) {
3615 auto MemSize = [=](uint64_t Size) {
3616 return (MemOpc == ISD::MLOAD || MemOpc == ISD::MSTORE)
3618 : Size;
3619 };
3620 // MOp1 will not be used if HiIsEmpty for masked loads and stores (MLOAD and
3621 // MSTORE). Non-masked loads and store are always of double-vector size (see
3622 // isHvxPairTy() check above).
3623 MOp0 = MF.getMachineMemOperand(MMO, 0, MemSize(LoSize));
3624 MOp1 = MF.getMachineMemOperand(MMO, LoSize, MemSize(HiSize));
3625 }
3626
3627 if (MemOpc == ISD::LOAD) {
3628 assert(cast<LoadSDNode>(Op)->isUnindexed());
3629 SDValue Load0 = DAG.getLoad(LoVT, dl, Chain, Base0, MOp0);
3630 SDValue Load1 = DAG.getLoad(HiVT, dl, Chain, Base1, MOp1);
3631 return DAG.getMergeValues(
3632 {DAG.getNode(ISD::CONCAT_VECTORS, dl, MemN->getValueType(0), Load0,
3633 Load1),
3634 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Load0.getValue(1),
3635 Load1.getValue(1))},
3636 dl);
3637 }
3638 if (MemOpc == ISD::STORE) {
3639 assert(cast<StoreSDNode>(Op)->isUnindexed());
3640 VectorPair Vals = opSplit(cast<StoreSDNode>(Op)->getValue(), dl, DAG);
3641 SDValue Store0 = DAG.getStore(Chain, dl, Vals.first, Base0, MOp0);
3642 SDValue Store1 = DAG.getStore(Chain, dl, Vals.second, Base1, MOp1);
3643 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store0, Store1);
3644 }
3645
3646 assert(MemOpc == ISD::MLOAD || MemOpc == ISD::MSTORE);
3647
3648 auto MaskN = cast<MaskedLoadStoreSDNode>(Op);
3649 assert(MaskN->isUnindexed());
3650 VectorPair Masks = opSplit(MaskN->getMask(), dl, DAG);
3651 SDValue Offset = DAG.getPOISON(MVT::i32);
3652
3653 if (MemOpc == ISD::MLOAD) {
3654 VectorPair Thru =
3655 opSplit(cast<MaskedLoadSDNode>(Op)->getPassThru(), dl, DAG);
3656 SDValue MLoad0 = DAG.getMaskedLoad(LoVT, dl, Chain, Base0, Offset,
3657 Masks.first, Thru.first, LoMemVT, MOp0,
3659
3660 // The hi masked load has zero storage size. We therefore simply set it to
3661 // the low masked load and rely on subsequent removal from the chain as it
3662 // is unused. See DAGTypeLegalizer::SplitVecRes_MLOAD() for the same logic.
3663 SDValue MLoad1 =
3664 HiIsEmpty ? MLoad0
3665 : DAG.getMaskedLoad(HiVT, dl, Chain, Base1, Offset,
3666 Masks.second, Thru.second, HiMemVT, MOp1,
3668 return DAG.getMergeValues(
3669 {DAG.getNode(ISD::CONCAT_VECTORS, dl, MemN->getValueType(0), MLoad0,
3670 MLoad1),
3671 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MLoad0.getValue(1),
3672 MLoad1.getValue(1))},
3673 dl);
3674 }
3675 if (MemOpc == ISD::MSTORE) {
3676 VectorPair Vals = opSplit(cast<MaskedStoreSDNode>(Op)->getValue(), dl, DAG);
3677 SDValue MStore0 =
3678 DAG.getMaskedStore(Chain, dl, Vals.first, Base0, Offset, Masks.first,
3679 LoMemVT, MOp0, ISD::UNINDEXED, false, false);
3680 if (HiIsEmpty)
3681 return MStore0;
3682 SDValue MStore1 =
3683 DAG.getMaskedStore(Chain, dl, Vals.second, Base1, Offset, Masks.second,
3684 HiMemVT, MOp1, ISD::UNINDEXED, false, false);
3685 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MStore0, MStore1);
3686 }
3687
3688 std::string Name = "Unexpected operation: " + Op->getOperationName(&DAG);
3689 llvm_unreachable(Name.c_str());
3690}
3691
3692SDValue
3693HexagonTargetLowering::WidenHvxLoad(SDValue Op, SelectionDAG &DAG) const {
3694 const SDLoc &dl(Op);
3695 auto *LoadN = cast<LoadSDNode>(Op.getNode());
3696 assert(LoadN->isUnindexed() && "Not widening indexed loads yet");
3697 assert(LoadN->getMemoryVT().getVectorElementType() != MVT::i1 &&
3698 "Not widening loads of i1 yet");
3699
3700 SDValue Chain = LoadN->getChain();
3701 SDValue Base = LoadN->getBasePtr();
3702 SDValue Offset = DAG.getPOISON(MVT::i32);
3703
3704 MVT ResTy = ty(Op);
3705 unsigned HwLen = Subtarget.getVectorLength();
3706 unsigned ResLen = ResTy.getStoreSize();
3707 assert(ResLen < HwLen && "vsetq(v1) prerequisite");
3708
3709 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
3710 SDValue Mask = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
3711 {DAG.getConstant(ResLen, dl, MVT::i32)}, DAG);
3712
3713 MVT LoadTy = MVT::getVectorVT(MVT::i8, HwLen);
3715 auto *MemOp = MF.getMachineMemOperand(LoadN->getMemOperand(), 0, HwLen);
3716
3717 SDValue Load = DAG.getMaskedLoad(LoadTy, dl, Chain, Base, Offset, Mask,
3718 DAG.getUNDEF(LoadTy), LoadTy, MemOp,
3720 SDValue Value = opCastElem(Load, ResTy.getVectorElementType(), DAG);
3721 return DAG.getMergeValues({Value, Load.getValue(1)}, dl);
3722}
3723
3724SDValue
3725HexagonTargetLowering::WidenHvxStore(SDValue Op, SelectionDAG &DAG) const {
3726 const SDLoc &dl(Op);
3727 auto *StoreN = cast<StoreSDNode>(Op.getNode());
3728 assert(StoreN->isUnindexed() && "Not widening indexed stores yet");
3729 assert(StoreN->getMemoryVT().getVectorElementType() != MVT::i1 &&
3730 "Not widening stores of i1 yet");
3731
3732 SDValue Chain = StoreN->getChain();
3733 SDValue Base = StoreN->getBasePtr();
3734 SDValue Offset = DAG.getPOISON(MVT::i32);
3735
3736 SDValue Value = opCastElem(StoreN->getValue(), MVT::i8, DAG);
3737 MVT ValueTy = ty(Value);
3738 unsigned ValueLen = ValueTy.getVectorNumElements();
3739 unsigned HwLen = Subtarget.getVectorLength();
3740 assert(isPowerOf2_32(ValueLen));
3741
3742 for (unsigned Len = ValueLen; Len < HwLen; ) {
3743 Value = opJoin({Value, DAG.getUNDEF(ty(Value))}, dl, DAG);
3744 Len = ty(Value).getVectorNumElements(); // This is Len *= 2
3745 }
3746 assert(ty(Value).getVectorNumElements() == HwLen); // Paranoia
3747
3748 assert(ValueLen < HwLen && "vsetq(v1) prerequisite");
3749 MVT BoolTy = MVT::getVectorVT(MVT::i1, HwLen);
3750 SDValue Mask = getInstr(Hexagon::V6_pred_scalar2, dl, BoolTy,
3751 {DAG.getConstant(ValueLen, dl, MVT::i32)}, DAG);
3753 auto *MemOp = MF.getMachineMemOperand(StoreN->getMemOperand(), 0, HwLen);
3754 return DAG.getMaskedStore(Chain, dl, Value, Base, Offset, Mask,
3755 StoreN->getMemoryVT(), MemOp, ISD::UNINDEXED, false,
3756 false);
3757}
3758
3759SDValue
3760HexagonTargetLowering::WidenHvxSetCC(SDValue Op, SelectionDAG &DAG) const {
3761 const SDLoc &dl(Op);
3762 SDValue Op0 = Op.getOperand(0), Op1 = Op.getOperand(1);
3763 MVT ElemTy = ty(Op0).getVectorElementType();
3764 unsigned HwLen = Subtarget.getVectorLength();
3765
3766 unsigned WideOpLen = (8 * HwLen) / ElemTy.getSizeInBits();
3767 assert(WideOpLen * ElemTy.getSizeInBits() == 8 * HwLen);
3768 MVT WideOpTy = MVT::getVectorVT(ElemTy, WideOpLen);
3769 if (!Subtarget.isHVXVectorType(WideOpTy, true))
3770 return SDValue();
3771
3772 SDValue WideOp0 = appendUndef(Op0, WideOpTy, DAG);
3773 SDValue WideOp1 = appendUndef(Op1, WideOpTy, DAG);
3774 EVT ResTy =
3775 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), WideOpTy);
3776 SDValue SetCC = DAG.getNode(ISD::SETCC, dl, ResTy,
3777 {WideOp0, WideOp1, Op.getOperand(2)});
3778
3779 EVT RetTy = typeLegalize(ty(Op), DAG);
3780 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, RetTy,
3781 {SetCC, getZero(dl, MVT::i32, DAG)});
3782}
3783
3784SDValue HexagonTargetLowering::WidenHvxTruncateToBool(SDValue Op,
3785 SelectionDAG &DAG) const {
3786 // Handle truncation to boolean vector where the result boolean type
3787 // needs widening (e.g., v16i32 -> v16i1 where v16i1 is not a standard
3788 // HVX predicate type, or v16i8 -> v16i1 in 128-byte mode).
3789 // Widen the input to HVX width, perform the truncate to the widened
3790 // boolean type, then extract the result.
3791 const SDLoc &dl(Op);
3792 SDValue Inp = Op.getOperand(0);
3793 MVT InpTy = ty(Inp);
3794 MVT ResTy = ty(Op);
3795
3796 assert(ResTy.getVectorElementType() == MVT::i1 &&
3797 "Expected boolean result type");
3798
3799 MVT ElemTy = InpTy.getVectorElementType();
3800 unsigned HwLen = Subtarget.getVectorLength();
3801
3802 // Calculate the widened input type that fills the HVX register.
3803 unsigned WideLen = (8 * HwLen) / ElemTy.getSizeInBits();
3804 MVT WideInpTy = MVT::getVectorVT(ElemTy, WideLen);
3805 if (!Subtarget.isHVXVectorType(WideInpTy, false))
3806 return SDValue();
3807
3808 // Widen the input to HVX width.
3809 SDValue WideInp = appendUndef(Inp, WideInpTy, DAG);
3810
3811 // Perform the truncate to widened boolean type.
3812 MVT WideBoolTy = MVT::getVectorVT(MVT::i1, WideLen);
3813 SDValue WideTrunc = DAG.getNode(ISD::TRUNCATE, dl, WideBoolTy, WideInp);
3814
3815 // Extract the result.
3816 EVT RetTy = typeLegalize(ResTy, DAG);
3817 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, RetTy,
3818 {WideTrunc, getZero(dl, MVT::i32, DAG)});
3819}
3820
3821SDValue
3822HexagonTargetLowering::LowerHvxOperation(SDValue Op, SelectionDAG &DAG) const {
3823 unsigned Opc = Op.getOpcode();
3824 bool IsPairOp = isHvxPairTy(ty(Op)) ||
3825 llvm::any_of(Op.getNode()->ops(), [this] (SDValue V) {
3826 return isHvxPairTy(ty(V));
3827 });
3828
3829 if (IsPairOp) {
3830 switch (Opc) {
3831 default:
3832 break;
3833 case ISD::LOAD:
3834 case ISD::STORE:
3835 case ISD::MLOAD:
3836 case ISD::MSTORE:
3837 return SplitHvxMemOp(Op, DAG);
3838 case ISD::SINT_TO_FP:
3839 case ISD::UINT_TO_FP:
3840 case ISD::FP_TO_SINT:
3841 case ISD::FP_TO_UINT:
3842 if (ty(Op).getSizeInBits() == ty(Op.getOperand(0)).getSizeInBits())
3843 return opJoin(SplitVectorOp(Op, DAG), SDLoc(Op), DAG);
3844 break;
3845 case ISD::ABS:
3846 case ISD::CTPOP:
3847 case ISD::CTLZ:
3848 case ISD::CTTZ:
3849 case ISD::MUL:
3850 case ISD::FADD:
3851 case ISD::FSUB:
3852 case ISD::FMUL:
3853 case ISD::FMINIMUMNUM:
3854 case ISD::FMAXIMUMNUM:
3855 case ISD::FMINIMUM:
3856 case ISD::FMAXIMUM:
3857 case ISD::FMINNUM:
3858 case ISD::FMAXNUM:
3859 case ISD::MULHS:
3860 case ISD::MULHU:
3861 case ISD::AND:
3862 case ISD::OR:
3863 case ISD::XOR:
3864 case ISD::SRA:
3865 case ISD::SHL:
3866 case ISD::SRL:
3867 case ISD::FSHL:
3868 case ISD::FSHR:
3869 case ISD::SMIN:
3870 case ISD::SMAX:
3871 case ISD::UMIN:
3872 case ISD::UMAX:
3873 case ISD::SETCC:
3874 case ISD::VSELECT:
3876 case ISD::SPLAT_VECTOR:
3877 return opJoin(SplitVectorOp(Op, DAG), SDLoc(Op), DAG);
3878 case ISD::SIGN_EXTEND:
3879 case ISD::ZERO_EXTEND:
3880 // In general, sign- and zero-extends can't be split and still
3881 // be legal. The only exception is extending bool vectors.
3882 if (ty(Op.getOperand(0)).getVectorElementType() == MVT::i1)
3883 return opJoin(SplitVectorOp(Op, DAG), SDLoc(Op), DAG);
3884 break;
3885 }
3886 }
3887
3888 switch (Opc) {
3889 default:
3890 break;
3891 // clang-format off
3892 case ISD::BUILD_VECTOR: return LowerHvxBuildVector(Op, DAG);
3893 case ISD::SPLAT_VECTOR: return LowerHvxSplatVector(Op, DAG);
3894 case ISD::CONCAT_VECTORS: return LowerHvxConcatVectors(Op, DAG);
3895 case ISD::INSERT_SUBVECTOR: return LowerHvxInsertSubvector(Op, DAG);
3896 case ISD::INSERT_VECTOR_ELT: return LowerHvxInsertElement(Op, DAG);
3897 case ISD::EXTRACT_SUBVECTOR: return LowerHvxExtractSubvector(Op, DAG);
3898 case ISD::EXTRACT_VECTOR_ELT: return LowerHvxExtractElement(Op, DAG);
3899 case ISD::BITCAST: return LowerHvxBitcast(Op, DAG);
3900 case ISD::ANY_EXTEND: return LowerHvxAnyExt(Op, DAG);
3901 case ISD::SIGN_EXTEND: return LowerHvxSignExt(Op, DAG);
3902 case ISD::ZERO_EXTEND: return LowerHvxZeroExt(Op, DAG);
3903 case ISD::CTTZ: return LowerHvxCttz(Op, DAG);
3904 case ISD::SELECT: return LowerHvxSelect(Op, DAG);
3905 case ISD::SRA:
3906 case ISD::SHL:
3907 case ISD::SRL: return LowerHvxShift(Op, DAG);
3908 case ISD::FSHL:
3909 case ISD::FSHR: return LowerHvxFunnelShift(Op, DAG);
3910 case ISD::MULHS:
3911 case ISD::MULHU: return LowerHvxMulh(Op, DAG);
3912 case ISD::SMUL_LOHI:
3913 case ISD::UMUL_LOHI: return LowerHvxMulLoHi(Op, DAG);
3914 case ISD::ANY_EXTEND_VECTOR_INREG: return LowerHvxExtend(Op, DAG);
3915 case ISD::SETCC: {
3916 ISD::CondCode CC = Op.getOperandCondCode(2);
3917 if (CC == ISD::SETOEQ &&
3918 ty(Op.getOperand(0)).getScalarType().isFloatingPoint())
3919 return LowerHvxFpSetoeq(Op, DAG);
3920 return Op;
3921 }
3922 case ISD::INTRINSIC_VOID: return Op;
3923 case ISD::INTRINSIC_W_CHAIN: return Op;
3924 case ISD::INTRINSIC_WO_CHAIN: return LowerHvxIntrinsic(Op, DAG);
3925 case ISD::MLOAD:
3926 case ISD::MSTORE: return LowerHvxMaskedOp(Op, DAG);
3927 // Unaligned loads will be handled by the default lowering.
3928 case ISD::LOAD: return LowerHvxLoad(Op, DAG);
3929 case ISD::STORE: return LowerHvxStore(Op, DAG);
3930 case ISD::FP_EXTEND: return LowerHvxFpExtend(Op, DAG);
3931 case ISD::FP_TO_SINT:
3932 case ISD::FP_TO_UINT: return LowerHvxFpToInt(Op, DAG);
3933 case ISD::SINT_TO_FP:
3934 case ISD::UINT_TO_FP: return LowerHvxIntToFp(Op, DAG);
3935
3936 // Special nodes:
3937 case HexagonISD::SMUL_LOHI:
3938 case HexagonISD::UMUL_LOHI:
3939 case HexagonISD::USMUL_LOHI: return LowerHvxMulLoHi(Op, DAG);
3940
3944 return LowerHvxPartialReduceMLA(Op, DAG);
3946 return LowerHvxVecReduceFMin(Op, DAG);
3948 return LowerHvxVecReduceFMax(Op, DAG);
3950 return LowerHvxVecReduceFMinimum(Op, DAG);
3952 return LowerHvxVecReduceFMaximum(Op, DAG);
3953 case ISD::FMINNUM:
3954 return LowerHvxFMinNum(Op, DAG);
3955 case ISD::FMAXNUM:
3956 return LowerHvxFMaxNum(Op, DAG);
3957 // clang-format on
3958 }
3959#ifndef NDEBUG
3960 Op.dumpr(&DAG);
3961#endif
3962 llvm_unreachable("Unhandled HVX operation");
3963}
3964
3965SDValue
3966HexagonTargetLowering::ExpandHvxResizeIntoSteps(SDValue Op, SelectionDAG &DAG)
3967 const {
3968 // Rewrite the extension/truncation/saturation op into steps where each
3969 // step changes the type widths by a factor of 2.
3970 // E.g. i8 -> i16 remains unchanged, but i8 -> i32 ==> i8 -> i16 -> i32.
3971 //
3972 // Some of the vector types in Op may not be legal.
3973
3974 unsigned Opc = Op.getOpcode();
3975 switch (Opc) {
3976 case HexagonISD::SSAT:
3977 case HexagonISD::USAT:
3980 break;
3981 case ISD::ANY_EXTEND:
3982 case ISD::ZERO_EXTEND:
3983 case ISD::SIGN_EXTEND:
3984 case ISD::TRUNCATE:
3985 llvm_unreachable("ISD:: ops will be auto-folded");
3986 break;
3987#ifndef NDEBUG
3988 Op.dump(&DAG);
3989#endif
3990 llvm_unreachable("Unexpected operation");
3991 }
3992
3993 SDValue Inp = Op.getOperand(0);
3994 MVT InpTy = ty(Inp);
3995 MVT ResTy = ty(Op);
3996
3997 unsigned InpWidth = InpTy.getVectorElementType().getSizeInBits();
3998 unsigned ResWidth = ResTy.getVectorElementType().getSizeInBits();
3999 assert(InpWidth != ResWidth);
4000
4001 if (InpWidth == 2 * ResWidth || ResWidth == 2 * InpWidth)
4002 return Op;
4003
4004 const SDLoc &dl(Op);
4005 unsigned NumElems = InpTy.getVectorNumElements();
4006 assert(NumElems == ResTy.getVectorNumElements());
4007
4008 auto repeatOp = [&](unsigned NewWidth, SDValue Arg) {
4009 MVT Ty = MVT::getVectorVT(MVT::getIntegerVT(NewWidth), NumElems);
4010 switch (Opc) {
4011 case HexagonISD::SSAT:
4012 case HexagonISD::USAT:
4013 return DAG.getNode(Opc, dl, Ty, {Arg, DAG.getValueType(Ty)});
4016 return DAG.getNode(Opc, dl, Ty, {Arg, Op.getOperand(1), Op.getOperand(2)});
4017 default:
4018 llvm_unreachable("Unexpected opcode");
4019 }
4020 };
4021
4022 SDValue S = Inp;
4023 if (InpWidth < ResWidth) {
4024 assert(ResWidth % InpWidth == 0 && isPowerOf2_32(ResWidth / InpWidth));
4025 while (InpWidth * 2 <= ResWidth)
4026 S = repeatOp(InpWidth *= 2, S);
4027 } else {
4028 // InpWidth > ResWidth
4029 assert(InpWidth % ResWidth == 0 && isPowerOf2_32(InpWidth / ResWidth));
4030 while (InpWidth / 2 >= ResWidth)
4031 S = repeatOp(InpWidth /= 2, S);
4032 }
4033 return S;
4034}
4035
4036SDValue
4037HexagonTargetLowering::LegalizeHvxResize(SDValue Op, SelectionDAG &DAG) const {
4038 SDValue Inp0 = Op.getOperand(0);
4039 MVT InpTy = ty(Inp0);
4040 MVT ResTy = ty(Op);
4041 unsigned InpWidth = InpTy.getSizeInBits();
4042 unsigned ResWidth = ResTy.getSizeInBits();
4043 unsigned Opc = Op.getOpcode();
4044
4045 if (shouldWidenToHvx(InpTy, DAG) || shouldWidenToHvx(ResTy, DAG)) {
4046 // First, make sure that the narrower type is widened to HVX.
4047 // This may cause the result to be wider than what the legalizer
4048 // expects, so insert EXTRACT_SUBVECTOR to bring it back to the
4049 // desired type.
4050 auto [WInpTy, WResTy] =
4051 InpWidth < ResWidth ? typeWidenToWider(typeWidenToHvx(InpTy), ResTy)
4052 : typeWidenToWider(InpTy, typeWidenToHvx(ResTy));
4053 SDValue W = appendUndef(Inp0, WInpTy, DAG);
4054 SDValue S;
4056 S = DAG.getNode(Opc, SDLoc(Op), WResTy, W, Op.getOperand(1),
4057 Op.getOperand(2));
4058 } else {
4059 S = DAG.getNode(Opc, SDLoc(Op), WResTy, W, DAG.getValueType(WResTy));
4060 }
4061 SDValue T = ExpandHvxResizeIntoSteps(S, DAG);
4062 return extractSubvector(T, typeLegalize(ResTy, DAG), 0, DAG);
4063 } else if (shouldSplitToHvx(InpWidth < ResWidth ? ResTy : InpTy, DAG)) {
4064 // For multi-step extends/truncates (e.g., i8->i32), expand into
4065 // single-step operations first. Splitting a multi-step TL_EXTEND
4066 // would halve the operand type to a sub-HVX size (e.g., v128i8 ->
4067 // v64i8), creating illegal types that cause issues in the type
4068 // legalizer's map tracking. Single-step operations (e.g., i16->i32)
4069 // are safe to split because their halved operand types remain legal.
4070 SDValue T = ExpandHvxResizeIntoSteps(Op, DAG);
4071 if (T != Op)
4072 return T;
4073 return opJoin(SplitVectorOp(Op, DAG), SDLoc(Op), DAG);
4074 } else {
4075 assert(isTypeLegal(InpTy) && isTypeLegal(ResTy));
4076 return RemoveTLWrapper(Op, DAG);
4077 }
4078 llvm_unreachable("Unexpected situation");
4079}
4080
4081void
4082HexagonTargetLowering::LowerHvxOperationWrapper(SDNode *N,
4084 unsigned Opc = N->getOpcode();
4085 SDValue Op(N, 0);
4086 SDValue Inp0; // Optional first argument.
4087 if (N->getNumOperands() > 0)
4088 Inp0 = Op.getOperand(0);
4089
4090 switch (Opc) {
4091 case ISD::ANY_EXTEND:
4092 case ISD::SIGN_EXTEND:
4093 case ISD::ZERO_EXTEND:
4094 if (Subtarget.isHVXElementType(ty(Op)) &&
4095 Subtarget.isHVXElementType(ty(Inp0))) {
4096 Results.push_back(CreateTLWrapper(Op, DAG));
4097 }
4098 break;
4099 case ISD::TRUNCATE:
4100 // Handle truncate to boolean vector when the input is not a
4101 // standard HVX vector type (single or pair). This covers cases
4102 // where the input needs widening (e.g., v64i8 -> v64i1 in
4103 // 128-byte mode) and cases where the result boolean type itself
4104 // needs widening (e.g., v16i32 -> v16i1). When the input is
4105 // already an HVX type, tablegen patterns handle the truncation
4106 // directly (e.g., v64i16 -> v64i1 via V6_vandvrt).
4107 if (ty(Op).getVectorElementType() == MVT::i1 &&
4108 !Subtarget.isHVXVectorType(ty(Inp0), false)) {
4109 if (SDValue T = WidenHvxTruncateToBool(Op, DAG))
4110 Results.push_back(T);
4111 } else if (Subtarget.isHVXElementType(ty(Op)) &&
4112 Subtarget.isHVXElementType(ty(Inp0))) {
4113 Results.push_back(CreateTLWrapper(Op, DAG));
4114 }
4115 break;
4116 case ISD::SETCC:
4117 if (shouldWidenToHvx(ty(Inp0), DAG)) {
4118 if (SDValue T = WidenHvxSetCC(Op, DAG))
4119 Results.push_back(T);
4120 }
4121 break;
4122 case ISD::STORE: {
4123 if (shouldWidenToHvx(ty(cast<StoreSDNode>(N)->getValue()), DAG)) {
4124 SDValue Store = WidenHvxStore(Op, DAG);
4125 Results.push_back(Store);
4126 }
4127 break;
4128 }
4129 case ISD::MLOAD:
4130 if (isHvxPairTy(ty(Op))) {
4131 SDValue S = SplitHvxMemOp(Op, DAG);
4133 Results.push_back(S.getOperand(0));
4134 Results.push_back(S.getOperand(1));
4135 }
4136 break;
4137 case ISD::MSTORE:
4138 if (isHvxPairTy(ty(Op->getOperand(1)))) { // Stored value
4139 SDValue S = SplitHvxMemOp(Op, DAG);
4140 Results.push_back(S);
4141 }
4142 break;
4143 case ISD::SINT_TO_FP:
4144 case ISD::UINT_TO_FP:
4145 case ISD::FP_TO_SINT:
4146 case ISD::FP_TO_UINT:
4147 if (ty(Op).getSizeInBits() != ty(Inp0).getSizeInBits()) {
4148 SDValue T = EqualizeFpIntConversion(Op, DAG);
4149 Results.push_back(T);
4150 }
4151 break;
4152 case HexagonISD::SSAT:
4153 case HexagonISD::USAT:
4156 Results.push_back(LegalizeHvxResize(Op, DAG));
4157 break;
4158 default:
4159 break;
4160 }
4161}
4162
4163void
4164HexagonTargetLowering::ReplaceHvxNodeResults(SDNode *N,
4166 unsigned Opc = N->getOpcode();
4167 SDValue Op(N, 0);
4168 SDValue Inp0; // Optional first argument.
4169 if (N->getNumOperands() > 0)
4170 Inp0 = Op.getOperand(0);
4171
4172 switch (Opc) {
4173 case ISD::ANY_EXTEND:
4174 case ISD::SIGN_EXTEND:
4175 case ISD::ZERO_EXTEND:
4176 if (Subtarget.isHVXElementType(ty(Op)) &&
4177 Subtarget.isHVXElementType(ty(Inp0))) {
4178 Results.push_back(CreateTLWrapper(Op, DAG));
4179 }
4180 break;
4181 case ISD::TRUNCATE:
4182 // Handle truncate to boolean vector when the input is not a
4183 // standard HVX vector type. See comment in LowerHvxOperationWrapper.
4184 if (ty(Op).getVectorElementType() == MVT::i1 &&
4185 !Subtarget.isHVXVectorType(ty(Inp0), false)) {
4186 if (SDValue T = WidenHvxTruncateToBool(Op, DAG))
4187 Results.push_back(T);
4188 } else if (Subtarget.isHVXElementType(ty(Op)) &&
4189 Subtarget.isHVXElementType(ty(Inp0))) {
4190 Results.push_back(CreateTLWrapper(Op, DAG));
4191 }
4192 break;
4193 case ISD::SETCC:
4194 if (shouldWidenToHvx(ty(Op), DAG)) {
4195 if (SDValue T = WidenHvxSetCC(Op, DAG))
4196 Results.push_back(T);
4197 }
4198 break;
4199 case ISD::LOAD: {
4200 if (shouldWidenToHvx(ty(Op), DAG)) {
4201 SDValue Load = WidenHvxLoad(Op, DAG);
4202 assert(Load->getOpcode() == ISD::MERGE_VALUES);
4203 Results.push_back(Load.getOperand(0));
4204 Results.push_back(Load.getOperand(1));
4205 }
4206 break;
4207 }
4208 case ISD::BITCAST:
4209 if (isHvxBoolTy(ty(Inp0))) {
4210 SDValue C = LowerHvxBitcast(Op, DAG);
4211 Results.push_back(C);
4212 }
4213 break;
4214 case ISD::FP_TO_SINT:
4215 case ISD::FP_TO_UINT:
4216 if (ty(Op).getSizeInBits() != ty(Inp0).getSizeInBits()) {
4217 SDValue T = EqualizeFpIntConversion(Op, DAG);
4218 Results.push_back(T);
4219 }
4220 break;
4221 case HexagonISD::SSAT:
4222 case HexagonISD::USAT:
4225 Results.push_back(LegalizeHvxResize(Op, DAG));
4226 break;
4227 default:
4228 break;
4229 }
4230}
4231
4232SDValue
4233HexagonTargetLowering::combineTruncateBeforeLegal(SDValue Op,
4234 DAGCombinerInfo &DCI) const {
4235 // Simplify V:v2NiB --(bitcast)--> vNi2B --(truncate)--> vNiB
4236 // to extract-subvector (shuffle V, pick even, pick odd)
4237
4238 assert(Op.getOpcode() == ISD::TRUNCATE);
4239 SelectionDAG &DAG = DCI.DAG;
4240 const SDLoc &dl(Op);
4241
4242 if (Op.getOperand(0).getOpcode() == ISD::BITCAST)
4243 return SDValue();
4244 SDValue Cast = Op.getOperand(0);
4245 SDValue Src = Cast.getOperand(0);
4246
4247 EVT TruncTy = Op.getValueType();
4248 EVT CastTy = Cast.getValueType();
4249 EVT SrcTy = Src.getValueType();
4250 if (SrcTy.isSimple())
4251 return SDValue();
4252 if (SrcTy.getVectorElementType() != TruncTy.getVectorElementType())
4253 return SDValue();
4254 unsigned SrcLen = SrcTy.getVectorNumElements();
4255 unsigned CastLen = CastTy.getVectorNumElements();
4256 if (2 * CastLen != SrcLen)
4257 return SDValue();
4258
4259 SmallVector<int, 128> Mask(SrcLen);
4260 for (int i = 0; i != static_cast<int>(CastLen); ++i) {
4261 Mask[i] = 2 * i;
4262 Mask[i + CastLen] = 2 * i + 1;
4263 }
4264 SDValue Deal =
4265 DAG.getVectorShuffle(SrcTy, dl, Src, DAG.getUNDEF(SrcTy), Mask);
4266 return opSplit(Deal, dl, DAG).first;
4267}
4268
4269SDValue
4270HexagonTargetLowering::combineConcatOfShuffles(SDValue Op,
4271 SelectionDAG &DAG) const {
4272 // Fold
4273 // concat (shuffle x, y, m1), (shuffle x, y, m2)
4274 // into
4275 // shuffle (concat x, y), undef, m3
4276 if (Op.getNumOperands() != 2)
4277 return SDValue();
4278
4279 const SDLoc &dl(Op);
4280 SDValue V0 = Op.getOperand(0);
4281 SDValue V1 = Op.getOperand(1);
4282
4283 if (V0.getOpcode() != ISD::VECTOR_SHUFFLE)
4284 return SDValue();
4285 if (V1.getOpcode() != ISD::VECTOR_SHUFFLE)
4286 return SDValue();
4287
4288 SetVector<SDValue> Order;
4289 Order.insert(V0.getOperand(0));
4290 Order.insert(V0.getOperand(1));
4291 Order.insert(V1.getOperand(0));
4292 Order.insert(V1.getOperand(1));
4293
4294 if (Order.size() > 2)
4295 return SDValue();
4296
4297 // In ISD::VECTOR_SHUFFLE, the types of each input and the type of the
4298 // result must be the same.
4299 EVT InpTy = V0.getValueType();
4300 assert(InpTy.isVector());
4301 unsigned InpLen = InpTy.getVectorNumElements();
4302
4303 SmallVector<int, 128> LongMask;
4304 auto AppendToMask = [&](SDValue Shuffle) {
4305 auto *SV = cast<ShuffleVectorSDNode>(Shuffle.getNode());
4306 ArrayRef<int> Mask = SV->getMask();
4307 SDValue X = Shuffle.getOperand(0);
4308 SDValue Y = Shuffle.getOperand(1);
4309 for (int M : Mask) {
4310 if (M == -1) {
4311 LongMask.push_back(M);
4312 continue;
4313 }
4314 SDValue Src = static_cast<unsigned>(M) < InpLen ? X : Y;
4315 if (static_cast<unsigned>(M) >= InpLen)
4316 M -= InpLen;
4317
4318 int OutOffset = Order[0] == Src ? 0 : InpLen;
4319 LongMask.push_back(M + OutOffset);
4320 }
4321 };
4322
4323 AppendToMask(V0);
4324 AppendToMask(V1);
4325
4326 SDValue C0 = Order.front();
4327 SDValue C1 = Order.back(); // Can be same as front
4328 EVT LongTy = InpTy.getDoubleNumVectorElementsVT(*DAG.getContext());
4329
4330 SDValue Cat = DAG.getNode(ISD::CONCAT_VECTORS, dl, LongTy, {C0, C1});
4331 return DAG.getVectorShuffle(LongTy, dl, Cat, DAG.getUNDEF(LongTy), LongMask);
4332}
4333
4334// Reassociate concat(p1, p2, ...) into
4335// concat(concat(p1, ...), concat(pi, ...), ...)
4336// where each inner concat produces a predicate where each bit corresponds
4337// to at most BitBytes bytes.
4338// Concatenating predicates decreases the number of bytes per each predicate
4339// bit.
4340SDValue
4341HexagonTargetLowering::combineConcatOfScalarPreds(SDValue Op, unsigned BitBytes,
4342 SelectionDAG &DAG) const {
4343 const SDLoc &dl(Op);
4344 SmallVector<SDValue> Ops(Op->ops());
4345 MVT ResTy = ty(Op);
4346 MVT InpTy = ty(Ops[0]);
4347 unsigned InpLen = InpTy.getVectorNumElements(); // Scalar predicate
4348 unsigned ResLen = ResTy.getVectorNumElements(); // HVX vector predicate
4349 assert(InpLen <= 8 && "Too long for scalar predicate");
4350 assert(ResLen > 8 && "Too short for HVX vector predicate");
4351
4352 unsigned Bytes = 8 / InpLen; // Bytes-per-bit in input
4353
4354 // Already in the right form?
4355 if (Bytes <= BitBytes)
4356 return Op;
4357
4358 ArrayRef<SDValue> Inputs(Ops);
4359 unsigned SliceLen = Bytes / BitBytes;
4360
4362 // (8 / BitBytes) is the desired length of the result of the inner concat.
4363 MVT InnerTy = MVT::getVectorVT(MVT::i1, 8 / BitBytes);
4364 for (unsigned i = 0; i != ResLen / (8 / BitBytes); ++i) {
4365 SDValue Cat = DAG.getNode(ISD::CONCAT_VECTORS, dl, InnerTy,
4366 Inputs.slice(SliceLen * i, SliceLen));
4367 Cats.push_back(Cat);
4368 }
4369
4370 return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResTy, Cats);
4371}
4372
4373SDValue HexagonTargetLowering::combineConcatVectorsBeforeLegal(
4374 SDValue Op, DAGCombinerInfo &DCI) const {
4375 MVT ResTy = ty(Op);
4376 MVT ElemTy = ResTy.getVectorElementType();
4377
4378 if (ElemTy != MVT::i1) {
4379 return combineConcatOfShuffles(Op, DCI.DAG);
4380 }
4381 return SDValue();
4382}
4383
4384// Create the inner partial reduction MLA that can be efficiently lowered. This
4385// function is used by partial and full reductions.
4386SDValue HexagonTargetLowering::createExtendingPartialReduceMLA(
4387 unsigned Opcode, EVT AccEltType, unsigned AccNumElements, EVT InputType,
4388 const SDValue &A, const SDValue &B, unsigned &RemainingReductionRatio,
4389 const SDLoc &DL, SelectionDAG &DAG) const {
4390 const auto &Subtarget = DAG.getSubtarget<HexagonSubtarget>();
4391 if (!Subtarget.useHVXOps())
4392 return SDValue();
4393
4394 EVT InputEltType = InputType.getVectorElementType();
4395
4396 // Find if an optimized instruction for the sub-reduction is available.
4397 unsigned NativeRatio;
4398 if (AccEltType == MVT::i32 && InputEltType == MVT::i8)
4399 NativeRatio = 4;
4400 else
4401 return SDValue();
4402
4403 // We only handle the case when additional reduction will be needed, i.e.
4404 // input is longer by a larger factor than the result.
4405 ElementCount InputEC = InputType.getVectorElementCount();
4406 if (!InputEC.isKnownMultipleOf(AccNumElements * NativeRatio))
4407 return SDValue();
4408
4409 unsigned InputNumElements = InputEC.getFixedValue();
4410 RemainingReductionRatio = InputNumElements / (AccNumElements * NativeRatio);
4411 if (RemainingReductionRatio == 1)
4412 return SDValue();
4413
4414 // Create a reduction by the natively supported factor.
4415 EVT IntermediateType = EVT::getVectorVT(*DAG.getContext(), AccEltType,
4416 InputNumElements / NativeRatio);
4417
4418 SDValue Zero = DAG.getConstant(0, DL, IntermediateType);
4419 return DAG.getNode(Opcode, DL, IntermediateType, Zero, A, B);
4420}
4421
4422static bool DetectExtendingMultiply(const SDValue &N, EVT ScalarType,
4423 unsigned &Opcode, SDValue &A, SDValue &B) {
4424 SDValue Mul = N;
4425 EVT AccType = Mul.getValueType(); // Vector input type after extension.
4426 if (ScalarType != AccType.getVectorElementType())
4427 return false;
4428 bool swap = false;
4429 if (Mul->getOpcode() != ISD::MUL)
4430 return false;
4431 A = Mul->getOperand(0);
4432 B = Mul->getOperand(1);
4433 if (A.getOpcode() == ISD::ZERO_EXTEND) {
4434 if (B.getOpcode() == ISD::ZERO_EXTEND)
4435 Opcode = ISD::PARTIAL_REDUCE_UMLA;
4436 else if (B.getOpcode() == ISD::SIGN_EXTEND) {
4437 swap = true;
4439 } else
4440 return false;
4441 } else if (A.getOpcode() == ISD::SIGN_EXTEND) {
4442 if (B.getOpcode() == ISD::ZERO_EXTEND)
4444 else if (B.getOpcode() == ISD::SIGN_EXTEND)
4445 Opcode = ISD::PARTIAL_REDUCE_SMLA;
4446 else
4447 return false;
4448 } else
4449 return false;
4450
4451 // Get multiplication arguments before extension.
4452 A = A->getOperand(0);
4453 B = B->getOperand(0);
4454 if (A.getValueType() != B.getValueType())
4455 return false;
4456
4457 if (swap)
4458 std::swap(A, B);
4459
4460 return true;
4461}
4462
4463SDValue HexagonTargetLowering::splitVecReduceAdd(SDNode *N,
4464 SelectionDAG &DAG) const {
4465 if (!Subtarget.useHVXOps())
4466 return SDValue();
4467
4468 EVT ScalarType = N->getValueType(0);
4469 unsigned Opcode;
4470 SDValue A, B;
4471 if (!DetectExtendingMultiply(N->getOperand(0), ScalarType, Opcode, A, B))
4472 return SDValue();
4473
4474 SDLoc DL(N);
4475 unsigned RemainingReductionRatio;
4476 SDValue Partial =
4477 createExtendingPartialReduceMLA(Opcode, ScalarType, 1, A.getValueType(),
4478 A, B, RemainingReductionRatio, DL, DAG);
4479 if (!Partial)
4480 return SDValue();
4481
4482 // We could have inserted a trivial MLA and rely on the folding action,
4483 // similar to how vector_partial_reduce_add is lowered to an MLA in
4484 // SelectionDAGBuilder. However, we just replace the final result since we
4485 // have analyzed the input completely.
4486 return DAG.getNode(ISD::VECREDUCE_ADD, DL, ScalarType, Partial);
4487}
4488
4489// Shared helper for VECREDUCE_FMIN/FMAX/FMINIMUM/FMAXIMUM on HVX float
4490// vector types. IgnoreNaN=true (FMIN/FMAX): NaN elements are replaced with
4491// the neutral value before the reduction so they don't corrupt the result
4492// even when the hardware pairwise instruction propagates NaN.
4493// IgnoreNaN=false (FMINIMUM/FMAXIMUM): NaN propagates naturally.
4494SDValue HexagonTargetLowering::LowerHvxVecReduceFMinMax(
4495 SDValue Op, unsigned PairwiseOpc, bool IgnoreNaN, SelectionDAG &DAG) const {
4496 SDLoc DL(Op);
4497 SDValue Vec = Op.getOperand(0);
4498 MVT VecTy = ty(Vec);
4499 SDNodeFlags Flags = Op->getFlags();
4500 bool ShouldStripNaN = IgnoreNaN && !Flags.hasNoNaNs();
4501
4502 // Save original input before NaN stripping; needed for the all-NaN fixup.
4503 SDValue OrigVec = Vec;
4504 MVT OrigVecTy = VecTy;
4505
4506 // Replace NaN elements in V with +/-Inf so the tree reduction ignores them.
4507 bool IsMax = (Op.getOpcode() == ISD::VECREDUCE_FMAX);
4508 auto ReplaceNaN = [&](SDValue V, MVT Ty) -> SDValue {
4510 Ty.getVectorElementType().getFltSemantics(), /*Negative=*/IsMax);
4511 SDValue NeutralVec = DAG.getConstantFP(Neutral, DL, Ty);
4512 EVT BoolTy = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), Ty);
4513 SDValue IsNaN = DAG.getSetCC(DL, BoolTy, V, V, ISD::SETUO);
4514 return DAG.getNode(ISD::VSELECT, DL, Ty, IsNaN, NeutralVec, V);
4515 };
4516
4517 // For a pair vector, strip NaN from each half before the cross-half
4518 // pairwise reduction so a NaN in one half can't corrupt the other.
4519 if (isHvxPairTy(VecTy)) {
4520 auto [Lo, Hi] = opSplit(Vec, DL, DAG);
4521 MVT SingleTy = ty(Lo);
4522 if (ShouldStripNaN) {
4523 Lo = ReplaceNaN(Lo, SingleTy);
4524 Hi = ReplaceNaN(Hi, SingleTy);
4525 }
4526 Vec = DAG.getNode(PairwiseOpc, DL, SingleTy, Lo, Hi, Flags);
4527 VecTy = SingleTy;
4528 } else if (ShouldStripNaN) {
4529 Vec = ReplaceNaN(Vec, VecTy);
4530 }
4531
4532 // Tree reduction using VROR + pairwise op. Each iteration rotates the vector
4533 // by half the remaining element count (in bytes) and takes element-wise
4534 // min/max, halving the active width until element 0 holds the result.
4535 unsigned ElemBytes = VecTy.getScalarSizeInBits() / 8;
4536 unsigned HwLen = Subtarget.getVectorLength();
4537 unsigned NumElems = HwLen / ElemBytes;
4538
4539 SDValue Curr = Vec;
4540 for (unsigned Width = NumElems / 2; Width >= 1; Width /= 2) {
4541 SDValue RotAmt = DAG.getConstant(Width * ElemBytes, DL, MVT::i32);
4542 SDValue Rotated = DAG.getNode(HexagonISD::VROR, DL, VecTy, Curr, RotAmt);
4543 Curr = DAG.getNode(PairwiseOpc, DL, VecTy, Curr, Rotated, Flags);
4544 }
4545
4546 // Extract element 0 as the scalar result.
4547 MVT ScalarTy = Op.getSimpleValueType();
4548 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ScalarTy, Curr,
4549 DAG.getConstant(0, DL, MVT::i32));
4550
4551 // Per llvm.maxnum/minnum semantics, all-NaN input must return NaN. The
4552 // NaN-stripping above replaced every NaN with the neutral value, so an
4553 // all-NaN vector produces the neutral value instead. Fix: if no element
4554 // in the original vector was non-NaN, return NaN.
4555 if (ShouldStripNaN) {
4556 EVT BoolVecTy =
4557 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), OrigVecTy);
4558 // IsOrd[i] == 1 iff OrigVec[i] is not NaN.
4559 SDValue IsOrd = DAG.getSetCC(DL, BoolVecTy, OrigVec, OrigVec, ISD::SETO);
4560 // BoolVecTy is vNi1 with exactly N bits; bitcast to an integer and
4561 // check != 0 to detect whether any element was non-NaN.
4562 unsigned NumBits = BoolVecTy.getSizeInBits();
4563 MVT IntTy = MVT::getIntegerVT(NumBits);
4564 SDValue IsOrdInt = DAG.getBitcast(IntTy, IsOrd);
4565 SDValue AnyNonNaN = DAG.getSetCC(DL, MVT::i1, IsOrdInt,
4566 DAG.getConstant(0, DL, IntTy), ISD::SETNE);
4568 DL, ScalarTy);
4569 Result = DAG.getSelect(DL, ScalarTy, AnyNonNaN, Result, NaN);
4570 }
4571
4572 return Result;
4573}
4574
4575SDValue HexagonTargetLowering::LowerHvxVecReduceFMin(SDValue Op,
4576 SelectionDAG &DAG) const {
4577 return LowerHvxVecReduceFMinMax(Op, ISD::FMINIMUM, /*IgnoreNaN=*/true, DAG);
4578}
4579
4580SDValue HexagonTargetLowering::LowerHvxVecReduceFMax(SDValue Op,
4581 SelectionDAG &DAG) const {
4582 return LowerHvxVecReduceFMinMax(Op, ISD::FMAXIMUM, /*IgnoreNaN=*/true, DAG);
4583}
4584
4585SDValue
4586HexagonTargetLowering::LowerHvxVecReduceFMinimum(SDValue Op,
4587 SelectionDAG &DAG) const {
4588 return LowerHvxVecReduceFMinMax(Op, ISD::FMINIMUM, /*IgnoreNaN=*/false, DAG);
4589}
4590
4591SDValue
4592HexagonTargetLowering::LowerHvxVecReduceFMaximum(SDValue Op,
4593 SelectionDAG &DAG) const {
4594 return LowerHvxVecReduceFMinMax(Op, ISD::FMAXIMUM, /*IgnoreNaN=*/false, DAG);
4595}
4596
4597// Lower FMINNUM/FMAXNUM on a single HVX float vector. These ops must ignore
4598// NaN (return the non-NaN operand). Because the hardware vmin/vmax may
4599// propagate NaN, replace NaN in each operand with the neutral value (+/-Inf)
4600// before delegating to FMINIMUM/FMAXIMUM.
4601SDValue HexagonTargetLowering::LowerHvxFMinNum(SDValue Op,
4602 SelectionDAG &DAG) const {
4603 SDLoc DL(Op);
4604 auto A = Op.getOperand(0), B = Op.getOperand(1);
4605 auto Ty = ty(Op);
4606 auto Flags = Op->getFlags();
4607
4608 if (!Flags.hasNoNaNs()) {
4609 auto &Sem = Ty.getVectorElementType().getFltSemantics();
4610 auto PosInf = DAG.getConstantFP(APFloat::getInf(Sem, false), DL, Ty);
4611 auto BoolTy =
4612 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), Ty);
4613 auto IsNaN_A = DAG.getSetCC(DL, BoolTy, A, A, ISD::SETUO);
4614 auto IsNaN_B = DAG.getSetCC(DL, BoolTy, B, B, ISD::SETUO);
4615 // Per llvm.minnum: if both operands are NaN, return NaN. Replace NaN
4616 // with +Inf so the hardware min ignores single-operand NaN, then restore
4617 // NaN for lanes where both inputs were NaN.
4618 auto BothNaN = DAG.getNode(ISD::AND, DL, BoolTy, IsNaN_A, IsNaN_B);
4619 A = DAG.getNode(ISD::VSELECT, DL, Ty, IsNaN_A, PosInf, A);
4620 B = DAG.getNode(ISD::VSELECT, DL, Ty, IsNaN_B, PosInf, B);
4621 auto Res = DAG.getNode(ISD::FMINIMUM, DL, Ty, A, B, Flags);
4622 auto NaN = DAG.getConstantFP(APFloat::getNaN(Sem), DL, Ty);
4623 return DAG.getNode(ISD::VSELECT, DL, Ty, BothNaN, NaN, Res);
4624 }
4625 return DAG.getNode(ISD::FMINIMUM, DL, Ty, A, B, Flags);
4626}
4627
4628SDValue HexagonTargetLowering::LowerHvxFMaxNum(SDValue Op,
4629 SelectionDAG &DAG) const {
4630 SDLoc DL(Op);
4631 auto A = Op.getOperand(0), B = Op.getOperand(1);
4632 auto Ty = ty(Op);
4633 auto Flags = Op->getFlags();
4634
4635 if (!Flags.hasNoNaNs()) {
4636 auto &Sem = Ty.getVectorElementType().getFltSemantics();
4637 auto NegInf = DAG.getConstantFP(APFloat::getInf(Sem, true), DL, Ty);
4638 auto BoolTy =
4639 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), Ty);
4640 auto IsNaN_A = DAG.getSetCC(DL, BoolTy, A, A, ISD::SETUO);
4641 auto IsNaN_B = DAG.getSetCC(DL, BoolTy, B, B, ISD::SETUO);
4642 // Per llvm.maxnum: if both operands are NaN, return NaN. Replace NaN
4643 // with -Inf so the hardware max ignores single-operand NaN, then restore
4644 // NaN for lanes where both inputs were NaN.
4645 auto BothNaN = DAG.getNode(ISD::AND, DL, BoolTy, IsNaN_A, IsNaN_B);
4646 A = DAG.getNode(ISD::VSELECT, DL, Ty, IsNaN_A, NegInf, A);
4647 B = DAG.getNode(ISD::VSELECT, DL, Ty, IsNaN_B, NegInf, B);
4648 auto Res = DAG.getNode(ISD::FMAXIMUM, DL, Ty, A, B, Flags);
4649 auto NaN = DAG.getConstantFP(APFloat::getNaN(Sem), DL, Ty);
4650 return DAG.getNode(ISD::VSELECT, DL, Ty, BothNaN, NaN, Res);
4651 }
4652 return DAG.getNode(ISD::FMAXIMUM, DL, Ty, A, B, Flags);
4653}
4654
4655// When possible, separate an MLA reduction with extended operands but
4656// unsupported reduction factor into an extending partial reduction that
4657// can be efficiently lowered, and a follow-up partial reduction.
4658// partial_reduce_mla(a, x, y) ->
4659// partial_reduce_mla(a, partial_reduce_mla(0, x, y), 1)
4660SDValue
4661HexagonTargetLowering::splitExtendingPartialReduceMLA(SDNode *N,
4662 SelectionDAG &DAG) const {
4663 if (!Subtarget.useHVXOps())
4664 return SDValue();
4665
4666 SDValue Acc = N->getOperand(0);
4667 SDValue A = N->getOperand(1);
4668 SDValue B = N->getOperand(2);
4669 if (A.getValueType() != B.getValueType())
4670 return SDValue();
4671
4672 // The types should be declared as custom, but do not split already legal
4673 // operation.
4674 EVT AccType = Acc.getValueType();
4675 EVT InputType = A.getValueType();
4676 if (getPartialReduceMLAAction(N->getOpcode(), AccType, InputType) != Custom)
4677 return SDValue();
4678
4679 SDLoc DL(N);
4680 unsigned RemainingReductionRatio;
4681 SDValue Partial = createExtendingPartialReduceMLA(
4682 N->getOpcode(), AccType.getVectorElementType(),
4683 AccType.getVectorNumElements(), InputType, A, B, RemainingReductionRatio,
4684 DL, DAG);
4685 if (!Partial)
4686 return SDValue();
4687 assert(RemainingReductionRatio <= MaxExpandMLA);
4688
4689 // Create the reduction for the remaining ratio.
4690 EVT IntermediateType = Partial->getOperand(0).getValueType();
4691 SDValue One = DAG.getConstant(1, DL, IntermediateType);
4692 return DAG.getNode(N->getOpcode() == ISD::PARTIAL_REDUCE_UMLA
4695 DL, AccType, Acc, Partial, One);
4696}
4697
4698SDValue
4699HexagonTargetLowering::LowerHvxPartialReduceMLA(SDValue Op,
4700 SelectionDAG &DAG) const {
4701 const SDLoc &DL(Op);
4702 SDValue Acc = Op.getOperand(0);
4703 SDValue A = Op.getOperand(1);
4704 SDValue B = Op.getOperand(2);
4705
4706 // Split the input vectors into units of one HVX vector length.
4707 unsigned HwVectorSizeInBits = Subtarget.getVectorLength() * 8;
4708
4709 EVT AccType = Acc.getValueType();
4710 EVT AccEltType = AccType.getVectorElementType();
4711 unsigned AccSubvectorNumElements =
4712 HwVectorSizeInBits / AccEltType.getSizeInBits();
4713 EVT AccSubvectorType =
4714 EVT::getVectorVT(*DAG.getContext(), AccEltType, AccSubvectorNumElements);
4715
4716 EVT InputType = A.getValueType();
4717 assert(InputType.getSizeInBits() % HwVectorSizeInBits == 0);
4718 EVT InputEltType = InputType.getVectorElementType();
4719 unsigned InputSubvectorNumElements =
4720 HwVectorSizeInBits / InputEltType.getSizeInBits();
4721 EVT InputSubvectorType = EVT::getVectorVT(*DAG.getContext(), InputEltType,
4722 InputSubvectorNumElements);
4723
4724 unsigned SubvectorNum = InputType.getFixedSizeInBits() / HwVectorSizeInBits;
4726
4727 for (unsigned I = 0; I != SubvectorNum; ++I) {
4728 SDValue SubvectorAcc = DAG.getExtractSubvector(DL, AccSubvectorType, Acc,
4729 I * AccSubvectorNumElements);
4730 SDValue SubvectorA = DAG.getExtractSubvector(DL, InputSubvectorType, A,
4731 I * InputSubvectorNumElements);
4732 SDValue SubvectorB = DAG.getExtractSubvector(DL, InputSubvectorType, B,
4733 I * InputSubvectorNumElements);
4734 SDValue SubvectorMLA = DAG.getNode(Op.getOpcode(), DL, AccSubvectorType,
4735 SubvectorAcc, SubvectorA, SubvectorB);
4736 Subvectors.push_back(SubvectorMLA);
4737 }
4738
4739 return DAG.getNode(ISD::CONCAT_VECTORS, DL, AccType, Subvectors);
4740}
4741
4742// Lower fcmp oeq on HVX float vectors for architectures before v81, which
4743// lack a dedicated floating-point equality instruction.
4744//
4745// Correct IEEE-754 semantics: oeq(a,b) is true iff a==b and neither is NaN.
4746// We use the available float-GT instruction (V6_vgtsf/V6_vgthf) for the
4747// inequality check and bit manipulation for NaN detection:
4748//
4749// oeq(a, b) = NOT(ogt(a,b) OR ogt(b,a) OR isNaN(a) OR isNaN(b))
4750//
4751// where isNaN(x) = ((int_bits(x) & AbsMask) > NaNThreshold)
4752// f32: AbsMask=0x7FFFFFFF, NaNThreshold=0x7F800000
4753// f16: AbsMask=0x7FFF, NaNThreshold=0x7C00
4754//
4755// This handles +0/-0 correctly because float-GT treats them as equal, so
4756// neither ogt(+0,-0) nor ogt(-0,+0) is ever true.
4757//
4758// Example f32 assembly (no NaNs case):
4759// q0 = vcmp.eq(v0.w, v1.w) // bitwise comparison should just work
4760//
4761// Example f32 assembly (NaN-present case):
4762// q0 = vcmp.gt(v0.sf, v1.sf) // ogt(a,b)
4763// q0 |= vcmp.gt(v1.sf, v0.sf) // |= ogt(b,a)
4764// r0 = ##0x7FFFFFFF
4765// v2 = vsplat(r0) // AbsMask broadcast
4766// r1 = ##0x7F800000
4767// v3 = vsplat(r1) // NaNThresh broadcast
4768// v4 = vand(v0, v2) // int_bits(a) & AbsMask
4769// v5 = vand(v1, v2) // int_bits(b) & AbsMask
4770// q0 |= vcmp.gt(v4.w, v3.w) // |= isNaN(a)
4771// q0 |= vcmp.gt(v5.w, v3.w) // |= isNaN(b)
4772// // q0 now holds AnyFalse; result = XOR(q0, allones) = oeq
4773SDValue HexagonTargetLowering::LowerHvxFpSetoeq(SDValue Op,
4774 SelectionDAG &DAG) const {
4775 auto ResTy = ty(Op);
4776 auto A = Op.getOperand(0), B = Op->getOperand(1);
4777 MVT FloatTy = ty(A);
4778 MVT ElemTy = FloatTy.getVectorElementType();
4779 bool IsF32 = (ElemTy == MVT::f32);
4780 if (!IsF32) {
4781 assert((ElemTy == MVT::f16));
4782 }
4783 const SDLoc &DL(Op);
4784 MVT IntElemTy = IsF32 ? MVT::i32 : MVT::i16;
4785 MVT IntVecTy = tyVector(FloatTy, IntElemTy);
4786
4787 // Under nnan semantics NaN cannot appear, so integer equality is both
4788 // correct and cheaper (one instruction vs the float-GT sequence).
4789 bool NoNaN = Op->getFlags().hasNoNaNs();
4790 if (NoNaN) {
4791 SDValue IA = DAG.getNode(ISD::BITCAST, DL, IntVecTy, A);
4792 SDValue IB = DAG.getNode(ISD::BITCAST, DL, IntVecTy, B);
4793 return DAG.getSetCC(DL, ResTy, IA, IB, ISD::SETEQ);
4794 }
4795
4796 // Float GT comparisons (IEEE-754: false whenever either operand is NaN).
4797 SDValue QAgtB = DAG.getSetCC(DL, ResTy, A, B, ISD::SETOGT);
4798 SDValue QBgtA = DAG.getSetCC(DL, ResTy, B, A, ISD::SETOGT);
4799
4800 // OR all "false" conditions together, then invert.
4801 SDValue AnyFalse = DAG.getNode(ISD::OR, DL, ResTy, QAgtB, QBgtA);
4802
4803 // Detect NaN by checking whether the unbiased exponent/mantissa field
4804 // exceeds the largest finite value.
4805 // f32: (bits & 0x7FFFFFFF) > 0x7F800000
4806 // f16: (bits & 0x7FFF) > 0x7C00
4807 uint64_t AbsMask = IsF32 ? 0x7FFFFFFFull : 0x7FFFull;
4808 uint64_t NaNThresh = IsF32 ? 0x7F800000ull : 0x7C00ull;
4809
4810 SDValue IA = DAG.getNode(ISD::BITCAST, DL, IntVecTy, A);
4811 SDValue IB = DAG.getNode(ISD::BITCAST, DL, IntVecTy, B);
4812 SDValue MaskVec = DAG.getConstant(AbsMask, DL, IntVecTy);
4813 SDValue ThreshVec = DAG.getConstant(NaNThresh, DL, IntVecTy);
4814 SDValue QNanA =
4815 DAG.getSetCC(DL, ResTy, DAG.getNode(ISD::AND, DL, IntVecTy, IA, MaskVec),
4816 ThreshVec, ISD::SETGT);
4817 SDValue QNanB =
4818 DAG.getSetCC(DL, ResTy, DAG.getNode(ISD::AND, DL, IntVecTy, IB, MaskVec),
4819 ThreshVec, ISD::SETGT);
4820 AnyFalse = DAG.getNode(ISD::OR, DL, ResTy, AnyFalse, QNanA);
4821 AnyFalse = DAG.getNode(ISD::OR, DL, ResTy, AnyFalse, QNanB);
4822
4823 // Result = NOT(<Is A gt B>, <IS B gt A>, <IS A NaN>, <IS B NaN>)
4824 // Use XOR with ones to simulate logical not.
4825 return DAG.getNode(ISD::XOR, DL, ResTy, AnyFalse,
4826 DAG.getConstant(1, DL, ResTy));
4827}
4828
4829SDValue
4830HexagonTargetLowering::PerformHvxDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
4831 const {
4832 const SDLoc &dl(N);
4833 SelectionDAG &DAG = DCI.DAG;
4834 SDValue Op(N, 0);
4835 unsigned Opc = Op.getOpcode();
4836
4838
4839 if (Opc == ISD::TRUNCATE)
4840 return combineTruncateBeforeLegal(Op, DCI);
4841 if (Opc == ISD::CONCAT_VECTORS)
4842 return combineConcatVectorsBeforeLegal(Op, DCI);
4843
4844 if (DCI.isBeforeLegalizeOps())
4845 return SDValue();
4846
4847 switch (Opc) {
4848 case HexagonISD::V2Q:
4849 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR) {
4850 if (const auto *C = dyn_cast<ConstantSDNode>(Ops[0].getOperand(0)))
4851 return C->isZero() ? DAG.getNode(HexagonISD::QFALSE, dl, ty(Op))
4852 : DAG.getNode(HexagonISD::QTRUE, dl, ty(Op));
4853 }
4854 break;
4855 case HexagonISD::Q2V:
4856 if (Ops[0].getOpcode() == HexagonISD::QTRUE)
4857 return DAG.getNode(ISD::SPLAT_VECTOR, dl, ty(Op),
4858 DAG.getAllOnesConstant(dl, MVT::i32));
4859 if (Ops[0].getOpcode() == HexagonISD::QFALSE)
4860 return getZero(dl, ty(Op), DAG);
4861 break;
4862 case HexagonISD::VINSERTW0:
4863 if (isUndef(Ops[1]))
4864 return Ops[0];
4865 break;
4866 case HexagonISD::VROR: {
4867 if (Ops[0].getOpcode() == HexagonISD::VROR) {
4868 SDValue Vec = Ops[0].getOperand(0);
4869 SDValue Rot0 = Ops[1], Rot1 = Ops[0].getOperand(1);
4870 SDValue Rot = DAG.getNode(ISD::ADD, dl, ty(Rot0), {Rot0, Rot1});
4871 return DAG.getNode(HexagonISD::VROR, dl, ty(Op), {Vec, Rot});
4872 }
4873 break;
4874 }
4875 }
4876
4877 return SDValue();
4878}
4879
4880bool
4881HexagonTargetLowering::shouldSplitToHvx(MVT Ty, SelectionDAG &DAG) const {
4882 if (Subtarget.isHVXVectorType(Ty, true))
4883 return false;
4884 auto Action = getPreferredHvxVectorAction(Ty);
4886 return Subtarget.isHVXVectorType(typeLegalize(Ty, DAG), true);
4887 return false;
4888}
4889
4890bool
4891HexagonTargetLowering::shouldWidenToHvx(MVT Ty, SelectionDAG &DAG) const {
4892 if (Subtarget.isHVXVectorType(Ty, true))
4893 return false;
4894 auto Action = getPreferredHvxVectorAction(Ty);
4896 return Subtarget.isHVXVectorType(typeLegalize(Ty, DAG), true);
4897 return false;
4898}
4899
4900bool
4901HexagonTargetLowering::isHvxOperation(SDNode *N, SelectionDAG &DAG) const {
4902 if (!Subtarget.useHVXOps())
4903 return false;
4904 // If the type of any result, or any operand type are HVX vector types,
4905 // this is an HVX operation.
4906 auto IsHvxTy = [this](EVT Ty) {
4907 return Ty.isSimple() && Subtarget.isHVXVectorType(Ty.getSimpleVT(), true);
4908 };
4909 auto IsHvxOp = [this](SDValue Op) {
4910 return Op.getValueType().isSimple() &&
4911 Subtarget.isHVXVectorType(ty(Op), true);
4912 };
4913 if (llvm::any_of(N->values(), IsHvxTy) || llvm::any_of(N->ops(), IsHvxOp))
4914 return true;
4915
4916 // Check if this could be an HVX operation after type widening.
4917 auto IsWidenedToHvx = [this, &DAG](SDValue Op) {
4918 if (!Op.getValueType().isSimple())
4919 return false;
4920 MVT ValTy = ty(Op);
4921 return ValTy.isVector() && shouldWidenToHvx(ValTy, DAG);
4922 };
4923
4924 for (int i = 0, e = N->getNumValues(); i != e; ++i) {
4925 if (IsWidenedToHvx(SDValue(N, i)))
4926 return true;
4927 }
4928 return llvm::any_of(N->ops(), IsWidenedToHvx);
4929}
auto * MemN
return SDValue()
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
uint64_t uint64_t
constexpr LLT S16
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static std::tuple< unsigned, unsigned, unsigned > getIEEEProperties(MVT Ty)
static const unsigned MaxExpandMLA
static const MVT LegalV128[]
static const MVT LegalW128[]
static const MVT LegalW64[]
static const MVT LegalV64[]
static bool DetectExtendingMultiply(const SDValue &N, EVT ScalarType, unsigned &Opcode, SDValue &A, SDValue &B)
static MaybeAlign getAlign(Value *Ptr)
IRTranslator LLVM IR MI
static constexpr Value * getValue(Ty &ValueOrUse)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
unsigned Opc
static bool isSplat(Value *V)
Return true if V is a splat of a value (which is used when multiplying a matrix with a scalar).
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define H(x, y, z)
Definition MD5.cpp:56
std::pair< MCSymbol *, MachineModuleInfoImpl::StubValueTy > PairTy
Promote Memory to Register
Definition Mem2Reg.cpp:110
This file provides utility analysis objects describing memory locations.
#define T
#define T1
#define P(N)
int64_t C1
RISCVCC::CondCode CC
MachineRegisterInfo & MRI
int64_t C0
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static llvm::Type * getVectorElementType(llvm::Type *Ty)
BinaryOperator * Mul
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:379
static constexpr const fltSemantics & IEEEhalf()
Definition APFloat.h:302
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6103
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1235
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1246
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
EVT getSetCCResultType(const DataLayout &, LLVMContext &C, EVT VT) const override
Return the ValueType of the result of SETCC operations.
LegalizeTypeAction getPreferredVectorAction(MVT VT) const override
Return the preferred vector type legalization action.
const SDValue & getBasePtr() const
Machine Value Type.
static MVT getFloatingPointVT(unsigned BitWidth)
uint64_t getScalarSizeInBits() const
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
MVT changeTypeToInteger()
Return the type converted to an equivalently sized integer or vector with integer element type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
ElementCount getVectorElementCount() const
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
bool isScalarInteger() const
Return true if this is an integer, not including vectors.
static MVT getVectorVT(MVT VT, unsigned NumElements)
MVT getVectorElementType() const
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
static MVT getIntegerVT(unsigned BitWidth)
MVT getScalarType() const
If this is a vector, return the element type, otherwise return this.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Representation of each machine instruction.
Flags
Flags values. These may be or'd together.
const MachinePointerInfo & getPointerInfo() const
Align getBaseAlign() const
Return the minimum known alignment in bytes of the base address, without the offset.
unsigned getSubReg() const
int64_t getImm() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Register getReg() const
getReg - Returns the register number.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
MachineMemOperand * getMemOperand() const
Return the unique MachineMemOperand object describing the memory reference performed by operation.
const SDValue & getChain() const
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
iterator_range< value_op_iterator > op_values() const
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
const SDValue & getOperand(unsigned Num) const
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
const SDValue & getOperand(unsigned i) const
unsigned getOpcode() const
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
const TargetSubtargetInfo & getSubtarget() const
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false, SDNodeFlags Flags={})
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const MMOMetadata &Metadata=MMOMetadata())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const MMOMetadata &Metadata=MMOMetadata())
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
MachineFunction & getMachineFunction() const
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVMContext * getContext() const
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
LLVM_ABI SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
const value_type & front() const
Return the first element of the SetVector.
Definition SetVector.h:138
const value_type & back() const
Return the last element of the SetVector.
Definition SetVector.h:144
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
const SDValue & getBasePtr() const
const SDValue & getValue() const
void setOperationAction(unsigned Op, MVT VT, LegalizeAction Action)
Indicate that the specified operation does not work with the specified type and indicate what to do a...
void setIndexedLoadAction(ArrayRef< unsigned > IdxModes, MVT VT, LegalizeAction Action)
Indicate that the specified indexed load does or does not work with the specified type and indicate w...
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
void addRegisterClass(MVT VT, const TargetRegisterClass *RC)
Add the specified register class as an available regclass for the specified value type.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
void setIndexedStoreAction(ArrayRef< unsigned > IdxModes, MVT VT, LegalizeAction Action)
Indicate that the specified indexed store does or does not work with the specified type and indicate ...
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
void setPartialReduceMLAAction(unsigned Opc, MVT AccVT, MVT InputVT, LegalizeAction Action)
Indicate how a PARTIAL_REDUCE_U/SMLA node with Acc type AccVT and Input type InputVT should be treate...
LegalizeAction getPartialReduceMLAAction(unsigned Opc, EVT AccVT, EVT InputVT) const
Return how a PARTIAL_REDUCE_U/SMLA node with Acc type AccVT and Input type InputVT should be treated.
void AddPromotedToType(unsigned Opc, MVT OrigVT, MVT DestVT)
If Opc/OrigVT is specified as being promoted, the promotion code defaults to trying a larger integer/...
void setCondCodeAction(ArrayRef< ISD::CondCode > CCs, MVT VT, LegalizeAction Action)
Indicate that the specified condition code is or isn't supported on the target and indicate what to d...
void setTargetDAGCombine(ArrayRef< ISD::NodeType > NTs)
Targets should invoke this method for each target independent node that they want to provide a custom...
std::vector< ArgListEntry > ArgListTy
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:352
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:292
constexpr bool isKnownMultipleOf(ScalarTy RHS) const
This function tells the caller whether the element count is known at compile time to be a multiple of...
Definition TypeSize.h:181
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:201
self_iterator getIterator()
Definition ilist_node.h:123
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ Entry
Definition COFF.h:862
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition CallingConv.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:851
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:263
@ PARTIAL_REDUCE_SMLA
PARTIAL_REDUCE_[U|S]MLA(Accumulator, Input1, Input2) The partial reduction nodes sign or zero extend ...
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:277
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:605
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:811
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:266
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:885
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:222
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:912
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:589
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:420
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:771
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:942
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:256
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:876
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:377
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:828
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:235
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:688
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:720
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:793
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:659
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:619
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:581
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:882
@ FMINNUM
FMINNUM/FMAXNUM - Same semantics as the llvm.minnum/maxnum intrinsics.
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:931
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:920
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:751
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:837
@ FMINIMUM
FMINIMUM/FMAXIMUM - Same semantics as the llvm.minimum/maximum intrinsics.
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:958
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:763
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:207
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:570
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:55
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:991
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:953
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:888
@ PARTIAL_REDUCE_SUMLA
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:368
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - Same semantics as the llvm.minimumnum/maximumnum intrinsics.
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:215
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:561
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LLVM_ABI StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Flag
These should be considered private to the implementation of the MCInstrDesc class.
unsigned getOpcode(const VPValue *V)
Return the instruction opcode for the recipe defining V or 0 for unsupported recipes and VPValues not...
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
@ Undef
Value of the register doesn't matter.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2229
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:326
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
@ Off
Use window algorithm after SMS algorithm fails.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Or
Bitwise or logical OR of integers.
@ And
Bitwise or logical AND of integers.
@ Add
Sum of integers.
@ Fast
Assign the register banks as fast as possible (default).
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
#define N
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:82
Extended Value Type.
Definition ValueTypes.h:35
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:160
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:70
ElementCount getVectorElementCount() const
Definition ValueTypes.h:388
EVT getDoubleNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:509
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:411
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:354
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:419
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:191
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:366
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:374
static LLVM_ABI MachinePointerInfo getConstantPool(MachineFunction &MF)
Return a MachinePointerInfo record that refers to the constant pool.
MachinePointerInfo getWithOffset(int64_t O) const