1 //===- LegalizeVectorOps.cpp - Implement SelectionDAG::LegalizeVectors ----===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the SelectionDAG::LegalizeVectors method.
11 // The vector legalizer looks for vector operations which might need to be
12 // scalarized and legalizes them. This is a separate step from Legalize because
13 // scalarizing can introduce illegal types. For example, suppose we have an
14 // ISD::SDIV of type v2i64 on x86-32. The type is legal (for example, addition
15 // on a v2i64 is legal), but ISD::SDIV isn't legal, so we have to unroll the
16 // operation, which introduces nodes with the illegal type i64 which must be
17 // expanded. Similarly, suppose we have an ISD::SRA of type v16i8 on PowerPC;
18 // the operation must be unrolled, which introduces nodes with the illegal
19 // type i8 which must be promoted.
21 // This does not legalize vector manipulations like ISD::BUILD_VECTOR,
22 // or operations that happen to take a vector which are custom-lowered;
23 // the legalization for such operations never produces nodes
24 // with illegal types, so it's okay to put off legalizing them until
25 // SelectionDAG::Legalize runs.
27 //===----------------------------------------------------------------------===//
29 #include "llvm/ADT/APInt.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/CodeGen/ISDOpcodes.h"
33 #include "llvm/CodeGen/MachineMemOperand.h"
34 #include "llvm/CodeGen/SelectionDAG.h"
35 #include "llvm/CodeGen/SelectionDAGNodes.h"
36 #include "llvm/CodeGen/TargetLowering.h"
37 #include "llvm/CodeGen/ValueTypes.h"
38 #include "llvm/IR/DataLayout.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/Compiler.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/MachineValueType.h"
43 #include "llvm/Support/MathExtras.h"
51 #define DEBUG_TYPE "legalizevectorops"
55 class VectorLegalizer
{
57 const TargetLowering
&TLI
;
58 bool Changed
= false; // Keep track of whether anything changed
60 /// For nodes that are of legal width, and that have more than one use, this
61 /// map indicates what regularized operand to use. This allows us to avoid
62 /// legalizing the same thing more than once.
63 SmallDenseMap
<SDValue
, SDValue
, 64> LegalizedNodes
;
65 /// Adds a node to the translation cache.
66 void AddLegalizedOperand(SDValue From
, SDValue To
) {
67 LegalizedNodes
.insert(std::make_pair(From
, To
));
68 // If someone requests legalization of the new node, return itself.
70 LegalizedNodes
.insert(std::make_pair(To
, To
));
73 /// Legalizes the given node.
74 SDValue
LegalizeOp(SDValue Op
);
76 /// Assuming the node is legal, "legalize" the results.
77 SDValue
TranslateLegalizeResults(SDValue Op
, SDValue Result
);
79 /// Implements unrolling a VSETCC.
80 SDValue
UnrollVSETCC(SDValue Op
);
82 /// Implement expand-based legalization of vector operations.
84 /// This is just a high-level routine to dispatch to specific code paths for
85 /// operations to legalize them.
86 SDValue
Expand(SDValue Op
);
88 /// Implements expansion for FP_TO_UINT; falls back to UnrollVectorOp if
89 /// FP_TO_SINT isn't legal.
90 SDValue
ExpandFP_TO_UINT(SDValue Op
);
92 /// Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if
93 /// SINT_TO_FLOAT and SHR on vectors isn't legal.
94 SDValue
ExpandUINT_TO_FLOAT(SDValue Op
);
96 /// Implement expansion for SIGN_EXTEND_INREG using SRL and SRA.
97 SDValue
ExpandSEXTINREG(SDValue Op
);
99 /// Implement expansion for ANY_EXTEND_VECTOR_INREG.
101 /// Shuffles the low lanes of the operand into place and bitcasts to the proper
102 /// type. The contents of the bits in the extended part of each element are
104 SDValue
ExpandANY_EXTEND_VECTOR_INREG(SDValue Op
);
106 /// Implement expansion for SIGN_EXTEND_VECTOR_INREG.
108 /// Shuffles the low lanes of the operand into place, bitcasts to the proper
109 /// type, then shifts left and arithmetic shifts right to introduce a sign
111 SDValue
ExpandSIGN_EXTEND_VECTOR_INREG(SDValue Op
);
113 /// Implement expansion for ZERO_EXTEND_VECTOR_INREG.
115 /// Shuffles the low lanes of the operand into place and blends zeros into
116 /// the remaining lanes, finally bitcasting to the proper type.
117 SDValue
ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op
);
119 /// Implement expand-based legalization of ABS vector operations.
120 /// If following expanding is legal/custom then do it:
121 /// (ABS x) --> (XOR (ADD x, (SRA x, sizeof(x)-1)), (SRA x, sizeof(x)-1))
122 /// else unroll the operation.
123 SDValue
ExpandABS(SDValue Op
);
125 /// Expand bswap of vectors into a shuffle if legal.
126 SDValue
ExpandBSWAP(SDValue Op
);
128 /// Implement vselect in terms of XOR, AND, OR when blend is not
129 /// supported by the target.
130 SDValue
ExpandVSELECT(SDValue Op
);
131 SDValue
ExpandSELECT(SDValue Op
);
132 SDValue
ExpandLoad(SDValue Op
);
133 SDValue
ExpandStore(SDValue Op
);
134 SDValue
ExpandFNEG(SDValue Op
);
135 SDValue
ExpandFSUB(SDValue Op
);
136 SDValue
ExpandBITREVERSE(SDValue Op
);
137 SDValue
ExpandCTPOP(SDValue Op
);
138 SDValue
ExpandCTLZ(SDValue Op
);
139 SDValue
ExpandCTTZ(SDValue Op
);
140 SDValue
ExpandFunnelShift(SDValue Op
);
141 SDValue
ExpandROT(SDValue Op
);
142 SDValue
ExpandFMINNUM_FMAXNUM(SDValue Op
);
143 SDValue
ExpandAddSubSat(SDValue Op
);
144 SDValue
ExpandFixedPointMul(SDValue Op
);
145 SDValue
ExpandStrictFPOp(SDValue Op
);
147 /// Implements vector promotion.
149 /// This is essentially just bitcasting the operands to a different type and
150 /// bitcasting the result back to the original type.
151 SDValue
Promote(SDValue Op
);
153 /// Implements [SU]INT_TO_FP vector promotion.
155 /// This is a [zs]ext of the input operand to a larger integer type.
156 SDValue
PromoteINT_TO_FP(SDValue Op
);
158 /// Implements FP_TO_[SU]INT vector promotion of the result type.
160 /// It is promoted to a larger integer type. The result is then
161 /// truncated back to the original type.
162 SDValue
PromoteFP_TO_INT(SDValue Op
);
165 VectorLegalizer(SelectionDAG
& dag
) :
166 DAG(dag
), TLI(dag
.getTargetLoweringInfo()) {}
168 /// Begin legalizer the vector operations in the DAG.
172 } // end anonymous namespace
174 bool VectorLegalizer::Run() {
175 // Before we start legalizing vector nodes, check if there are any vectors.
176 bool HasVectors
= false;
177 for (SelectionDAG::allnodes_iterator I
= DAG
.allnodes_begin(),
178 E
= std::prev(DAG
.allnodes_end()); I
!= std::next(E
); ++I
) {
179 // Check if the values of the nodes contain vectors. We don't need to check
180 // the operands because we are going to check their values at some point.
181 for (SDNode::value_iterator J
= I
->value_begin(), E
= I
->value_end();
183 HasVectors
|= J
->isVector();
185 // If we found a vector node we can start the legalization.
190 // If this basic block has no vectors then no need to legalize vectors.
194 // The legalize process is inherently a bottom-up recursive process (users
195 // legalize their uses before themselves). Given infinite stack space, we
196 // could just start legalizing on the root and traverse the whole graph. In
197 // practice however, this causes us to run out of stack space on large basic
198 // blocks. To avoid this problem, compute an ordering of the nodes where each
199 // node is only legalized after all of its operands are legalized.
200 DAG
.AssignTopologicalOrder();
201 for (SelectionDAG::allnodes_iterator I
= DAG
.allnodes_begin(),
202 E
= std::prev(DAG
.allnodes_end()); I
!= std::next(E
); ++I
)
203 LegalizeOp(SDValue(&*I
, 0));
205 // Finally, it's possible the root changed. Get the new root.
206 SDValue OldRoot
= DAG
.getRoot();
207 assert(LegalizedNodes
.count(OldRoot
) && "Root didn't get legalized?");
208 DAG
.setRoot(LegalizedNodes
[OldRoot
]);
210 LegalizedNodes
.clear();
212 // Remove dead nodes now.
213 DAG
.RemoveDeadNodes();
218 SDValue
VectorLegalizer::TranslateLegalizeResults(SDValue Op
, SDValue Result
) {
219 // Generic legalization: just pass the operand through.
220 for (unsigned i
= 0, e
= Op
.getNode()->getNumValues(); i
!= e
; ++i
)
221 AddLegalizedOperand(Op
.getValue(i
), Result
.getValue(i
));
222 return Result
.getValue(Op
.getResNo());
225 SDValue
VectorLegalizer::LegalizeOp(SDValue Op
) {
226 // Note that LegalizeOp may be reentered even from single-use nodes, which
227 // means that we always must cache transformed nodes.
228 DenseMap
<SDValue
, SDValue
>::iterator I
= LegalizedNodes
.find(Op
);
229 if (I
!= LegalizedNodes
.end()) return I
->second
;
231 SDNode
* Node
= Op
.getNode();
233 // Legalize the operands
234 SmallVector
<SDValue
, 8> Ops
;
235 for (const SDValue
&Op
: Node
->op_values())
236 Ops
.push_back(LegalizeOp(Op
));
238 SDValue Result
= SDValue(DAG
.UpdateNodeOperands(Op
.getNode(), Ops
),
241 if (Op
.getOpcode() == ISD::LOAD
) {
242 LoadSDNode
*LD
= cast
<LoadSDNode
>(Op
.getNode());
243 ISD::LoadExtType ExtType
= LD
->getExtensionType();
244 if (LD
->getMemoryVT().isVector() && ExtType
!= ISD::NON_EXTLOAD
) {
245 LLVM_DEBUG(dbgs() << "\nLegalizing extending vector load: ";
247 switch (TLI
.getLoadExtAction(LD
->getExtensionType(), LD
->getValueType(0),
248 LD
->getMemoryVT())) {
249 default: llvm_unreachable("This action is not supported yet!");
250 case TargetLowering::Legal
:
251 return TranslateLegalizeResults(Op
, Result
);
252 case TargetLowering::Custom
:
253 if (SDValue Lowered
= TLI
.LowerOperation(Result
, DAG
)) {
254 assert(Lowered
->getNumValues() == Op
->getNumValues() &&
255 "Unexpected number of results");
256 if (Lowered
!= Result
) {
257 // Make sure the new code is also legal.
258 Lowered
= LegalizeOp(Lowered
);
261 return TranslateLegalizeResults(Op
, Lowered
);
264 case TargetLowering::Expand
:
266 return LegalizeOp(ExpandLoad(Op
));
269 } else if (Op
.getOpcode() == ISD::STORE
) {
270 StoreSDNode
*ST
= cast
<StoreSDNode
>(Op
.getNode());
271 EVT StVT
= ST
->getMemoryVT();
272 MVT ValVT
= ST
->getValue().getSimpleValueType();
273 if (StVT
.isVector() && ST
->isTruncatingStore()) {
274 LLVM_DEBUG(dbgs() << "\nLegalizing truncating vector store: ";
276 switch (TLI
.getTruncStoreAction(ValVT
, StVT
)) {
277 default: llvm_unreachable("This action is not supported yet!");
278 case TargetLowering::Legal
:
279 return TranslateLegalizeResults(Op
, Result
);
280 case TargetLowering::Custom
: {
281 SDValue Lowered
= TLI
.LowerOperation(Result
, DAG
);
282 if (Lowered
!= Result
) {
283 // Make sure the new code is also legal.
284 Lowered
= LegalizeOp(Lowered
);
287 return TranslateLegalizeResults(Op
, Lowered
);
289 case TargetLowering::Expand
:
291 return LegalizeOp(ExpandStore(Op
));
296 bool HasVectorValue
= false;
297 for (SDNode::value_iterator J
= Node
->value_begin(), E
= Node
->value_end();
300 HasVectorValue
|= J
->isVector();
302 return TranslateLegalizeResults(Op
, Result
);
304 TargetLowering::LegalizeAction Action
= TargetLowering::Legal
;
305 switch (Op
.getOpcode()) {
307 return TranslateLegalizeResults(Op
, Result
);
308 case ISD::STRICT_FADD
:
309 case ISD::STRICT_FSUB
:
310 case ISD::STRICT_FMUL
:
311 case ISD::STRICT_FDIV
:
312 case ISD::STRICT_FREM
:
313 case ISD::STRICT_FSQRT
:
314 case ISD::STRICT_FMA
:
315 case ISD::STRICT_FPOW
:
316 case ISD::STRICT_FPOWI
:
317 case ISD::STRICT_FSIN
:
318 case ISD::STRICT_FCOS
:
319 case ISD::STRICT_FEXP
:
320 case ISD::STRICT_FEXP2
:
321 case ISD::STRICT_FLOG
:
322 case ISD::STRICT_FLOG10
:
323 case ISD::STRICT_FLOG2
:
324 case ISD::STRICT_FRINT
:
325 case ISD::STRICT_FNEARBYINT
:
326 case ISD::STRICT_FMAXNUM
:
327 case ISD::STRICT_FMINNUM
:
328 case ISD::STRICT_FCEIL
:
329 case ISD::STRICT_FFLOOR
:
330 case ISD::STRICT_FROUND
:
331 case ISD::STRICT_FTRUNC
:
332 // These pseudo-ops get legalized as if they were their non-strict
333 // equivalent. For instance, if ISD::FSQRT is legal then ISD::STRICT_FSQRT
334 // is also legal, but if ISD::FSQRT requires expansion then so does
335 // ISD::STRICT_FSQRT.
336 Action
= TLI
.getStrictFPOperationAction(Node
->getOpcode(),
337 Node
->getValueType(0));
367 case ISD::BITREVERSE
:
370 case ISD::CTLZ_ZERO_UNDEF
:
371 case ISD::CTTZ_ZERO_UNDEF
:
377 case ISD::ZERO_EXTEND
:
378 case ISD::ANY_EXTEND
:
380 case ISD::SIGN_EXTEND
:
381 case ISD::FP_TO_SINT
:
382 case ISD::FP_TO_UINT
:
387 case ISD::FMINNUM_IEEE
:
388 case ISD::FMAXNUM_IEEE
:
405 case ISD::FNEARBYINT
:
411 case ISD::SIGN_EXTEND_INREG
:
412 case ISD::ANY_EXTEND_VECTOR_INREG
:
413 case ISD::SIGN_EXTEND_VECTOR_INREG
:
414 case ISD::ZERO_EXTEND_VECTOR_INREG
:
421 case ISD::FCANONICALIZE
:
426 Action
= TLI
.getOperationAction(Node
->getOpcode(), Node
->getValueType(0));
430 unsigned Scale
= Node
->getConstantOperandVal(2);
431 Action
= TLI
.getFixedPointOperationAction(Node
->getOpcode(),
432 Node
->getValueType(0), Scale
);
435 case ISD::FP_ROUND_INREG
:
436 Action
= TLI
.getOperationAction(Node
->getOpcode(),
437 cast
<VTSDNode
>(Node
->getOperand(1))->getVT());
439 case ISD::SINT_TO_FP
:
440 case ISD::UINT_TO_FP
:
441 Action
= TLI
.getOperationAction(Node
->getOpcode(),
442 Node
->getOperand(0).getValueType());
446 LLVM_DEBUG(dbgs() << "\nLegalizing vector op: "; Node
->dump(&DAG
));
449 default: llvm_unreachable("This action is not supported yet!");
450 case TargetLowering::Promote
:
451 Result
= Promote(Op
);
454 case TargetLowering::Legal
:
455 LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n");
457 case TargetLowering::Custom
: {
458 LLVM_DEBUG(dbgs() << "Trying custom legalization\n");
459 if (SDValue Tmp1
= TLI
.LowerOperation(Op
, DAG
)) {
460 LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
464 LLVM_DEBUG(dbgs() << "Could not custom legalize node\n");
467 case TargetLowering::Expand
:
471 // Make sure that the generated code is itself legal.
473 Result
= LegalizeOp(Result
);
477 // Note that LegalizeOp may be reentered even from single-use nodes, which
478 // means that we always must cache transformed nodes.
479 AddLegalizedOperand(Op
, Result
);
483 SDValue
VectorLegalizer::Promote(SDValue Op
) {
484 // For a few operations there is a specific concept for promotion based on
485 // the operand's type.
486 switch (Op
.getOpcode()) {
487 case ISD::SINT_TO_FP
:
488 case ISD::UINT_TO_FP
:
489 // "Promote" the operation by extending the operand.
490 return PromoteINT_TO_FP(Op
);
491 case ISD::FP_TO_UINT
:
492 case ISD::FP_TO_SINT
:
493 // Promote the operation by extending the operand.
494 return PromoteFP_TO_INT(Op
);
497 // There are currently two cases of vector promotion:
498 // 1) Bitcasting a vector of integers to a different type to a vector of the
499 // same overall length. For example, x86 promotes ISD::AND v2i32 to v1i64.
500 // 2) Extending a vector of floats to a vector of the same number of larger
501 // floats. For example, AArch64 promotes ISD::FADD on v4f16 to v4f32.
502 MVT VT
= Op
.getSimpleValueType();
503 assert(Op
.getNode()->getNumValues() == 1 &&
504 "Can't promote a vector with multiple results!");
505 MVT NVT
= TLI
.getTypeToPromoteTo(Op
.getOpcode(), VT
);
507 SmallVector
<SDValue
, 4> Operands(Op
.getNumOperands());
509 for (unsigned j
= 0; j
!= Op
.getNumOperands(); ++j
) {
510 if (Op
.getOperand(j
).getValueType().isVector())
513 .getVectorElementType()
514 .isFloatingPoint() &&
515 NVT
.isVector() && NVT
.getVectorElementType().isFloatingPoint())
516 Operands
[j
] = DAG
.getNode(ISD::FP_EXTEND
, dl
, NVT
, Op
.getOperand(j
));
518 Operands
[j
] = DAG
.getNode(ISD::BITCAST
, dl
, NVT
, Op
.getOperand(j
));
520 Operands
[j
] = Op
.getOperand(j
);
523 Op
= DAG
.getNode(Op
.getOpcode(), dl
, NVT
, Operands
, Op
.getNode()->getFlags());
524 if ((VT
.isFloatingPoint() && NVT
.isFloatingPoint()) ||
525 (VT
.isVector() && VT
.getVectorElementType().isFloatingPoint() &&
526 NVT
.isVector() && NVT
.getVectorElementType().isFloatingPoint()))
527 return DAG
.getNode(ISD::FP_ROUND
, dl
, VT
, Op
, DAG
.getIntPtrConstant(0, dl
));
529 return DAG
.getNode(ISD::BITCAST
, dl
, VT
, Op
);
532 SDValue
VectorLegalizer::PromoteINT_TO_FP(SDValue Op
) {
533 // INT_TO_FP operations may require the input operand be promoted even
534 // when the type is otherwise legal.
535 MVT VT
= Op
.getOperand(0).getSimpleValueType();
536 MVT NVT
= TLI
.getTypeToPromoteTo(Op
.getOpcode(), VT
);
537 assert(NVT
.getVectorNumElements() == VT
.getVectorNumElements() &&
538 "Vectors have different number of elements!");
541 SmallVector
<SDValue
, 4> Operands(Op
.getNumOperands());
543 unsigned Opc
= Op
.getOpcode() == ISD::UINT_TO_FP
? ISD::ZERO_EXTEND
:
545 for (unsigned j
= 0; j
!= Op
.getNumOperands(); ++j
) {
546 if (Op
.getOperand(j
).getValueType().isVector())
547 Operands
[j
] = DAG
.getNode(Opc
, dl
, NVT
, Op
.getOperand(j
));
549 Operands
[j
] = Op
.getOperand(j
);
552 return DAG
.getNode(Op
.getOpcode(), dl
, Op
.getValueType(), Operands
);
555 // For FP_TO_INT we promote the result type to a vector type with wider
556 // elements and then truncate the result. This is different from the default
557 // PromoteVector which uses bitcast to promote thus assumning that the
558 // promoted vector type has the same overall size.
559 SDValue
VectorLegalizer::PromoteFP_TO_INT(SDValue Op
) {
560 MVT VT
= Op
.getSimpleValueType();
561 MVT NVT
= TLI
.getTypeToPromoteTo(Op
.getOpcode(), VT
);
562 assert(NVT
.getVectorNumElements() == VT
.getVectorNumElements() &&
563 "Vectors have different number of elements!");
565 unsigned NewOpc
= Op
->getOpcode();
566 // Change FP_TO_UINT to FP_TO_SINT if possible.
567 // TODO: Should we only do this if FP_TO_UINT itself isn't legal?
568 if (NewOpc
== ISD::FP_TO_UINT
&&
569 TLI
.isOperationLegalOrCustom(ISD::FP_TO_SINT
, NVT
))
570 NewOpc
= ISD::FP_TO_SINT
;
573 SDValue Promoted
= DAG
.getNode(NewOpc
, dl
, NVT
, Op
.getOperand(0));
575 // Assert that the converted value fits in the original type. If it doesn't
576 // (eg: because the value being converted is too big), then the result of the
577 // original operation was undefined anyway, so the assert is still correct.
578 Promoted
= DAG
.getNode(Op
->getOpcode() == ISD::FP_TO_UINT
? ISD::AssertZext
581 DAG
.getValueType(VT
.getScalarType()));
582 return DAG
.getNode(ISD::TRUNCATE
, dl
, VT
, Promoted
);
585 SDValue
VectorLegalizer::ExpandLoad(SDValue Op
) {
586 LoadSDNode
*LD
= cast
<LoadSDNode
>(Op
.getNode());
588 EVT SrcVT
= LD
->getMemoryVT();
589 EVT SrcEltVT
= SrcVT
.getScalarType();
590 unsigned NumElem
= SrcVT
.getVectorNumElements();
594 if (SrcVT
.getVectorNumElements() > 1 && !SrcEltVT
.isByteSized()) {
597 SmallVector
<SDValue
, 8> Vals
;
598 SmallVector
<SDValue
, 8> LoadChains
;
600 EVT DstEltVT
= LD
->getValueType(0).getScalarType();
601 SDValue Chain
= LD
->getChain();
602 SDValue BasePTR
= LD
->getBasePtr();
603 ISD::LoadExtType ExtType
= LD
->getExtensionType();
605 // When elements in a vector is not byte-addressable, we cannot directly
606 // load each element by advancing pointer, which could only address bytes.
607 // Instead, we load all significant words, mask bits off, and concatenate
608 // them to form each element. Finally, they are extended to destination
609 // scalar type to build the destination vector.
610 EVT WideVT
= TLI
.getPointerTy(DAG
.getDataLayout());
612 assert(WideVT
.isRound() &&
613 "Could not handle the sophisticated case when the widest integer is"
615 assert(WideVT
.bitsGE(SrcEltVT
) &&
616 "Type is not legalized?");
618 unsigned WideBytes
= WideVT
.getStoreSize();
620 unsigned RemainingBytes
= SrcVT
.getStoreSize();
621 SmallVector
<SDValue
, 8> LoadVals
;
622 while (RemainingBytes
> 0) {
624 unsigned LoadBytes
= WideBytes
;
626 if (RemainingBytes
>= LoadBytes
) {
628 DAG
.getLoad(WideVT
, dl
, Chain
, BasePTR
,
629 LD
->getPointerInfo().getWithOffset(Offset
),
630 MinAlign(LD
->getAlignment(), Offset
),
631 LD
->getMemOperand()->getFlags(), LD
->getAAInfo());
634 while (RemainingBytes
< LoadBytes
) {
635 LoadBytes
>>= 1; // Reduce the load size by half.
636 LoadVT
= EVT::getIntegerVT(*DAG
.getContext(), LoadBytes
<< 3);
639 DAG
.getExtLoad(ISD::EXTLOAD
, dl
, WideVT
, Chain
, BasePTR
,
640 LD
->getPointerInfo().getWithOffset(Offset
), LoadVT
,
641 MinAlign(LD
->getAlignment(), Offset
),
642 LD
->getMemOperand()->getFlags(), LD
->getAAInfo());
645 RemainingBytes
-= LoadBytes
;
648 BasePTR
= DAG
.getObjectPtrOffset(dl
, BasePTR
, LoadBytes
);
650 LoadVals
.push_back(ScalarLoad
.getValue(0));
651 LoadChains
.push_back(ScalarLoad
.getValue(1));
654 // Extract bits, pack and extend/trunc them into destination type.
655 unsigned SrcEltBits
= SrcEltVT
.getSizeInBits();
656 SDValue SrcEltBitMask
= DAG
.getConstant((1U << SrcEltBits
) - 1, dl
, WideVT
);
658 unsigned BitOffset
= 0;
659 unsigned WideIdx
= 0;
660 unsigned WideBits
= WideVT
.getSizeInBits();
662 for (unsigned Idx
= 0; Idx
!= NumElem
; ++Idx
) {
663 SDValue Lo
, Hi
, ShAmt
;
665 if (BitOffset
< WideBits
) {
666 ShAmt
= DAG
.getConstant(
667 BitOffset
, dl
, TLI
.getShiftAmountTy(WideVT
, DAG
.getDataLayout()));
668 Lo
= DAG
.getNode(ISD::SRL
, dl
, WideVT
, LoadVals
[WideIdx
], ShAmt
);
669 Lo
= DAG
.getNode(ISD::AND
, dl
, WideVT
, Lo
, SrcEltBitMask
);
672 BitOffset
+= SrcEltBits
;
673 if (BitOffset
>= WideBits
) {
675 BitOffset
-= WideBits
;
677 ShAmt
= DAG
.getConstant(
678 SrcEltBits
- BitOffset
, dl
,
679 TLI
.getShiftAmountTy(WideVT
, DAG
.getDataLayout()));
680 Hi
= DAG
.getNode(ISD::SHL
, dl
, WideVT
, LoadVals
[WideIdx
], ShAmt
);
681 Hi
= DAG
.getNode(ISD::AND
, dl
, WideVT
, Hi
, SrcEltBitMask
);
686 Lo
= DAG
.getNode(ISD::OR
, dl
, WideVT
, Lo
, Hi
);
689 default: llvm_unreachable("Unknown extended-load op!");
691 Lo
= DAG
.getAnyExtOrTrunc(Lo
, dl
, DstEltVT
);
694 Lo
= DAG
.getZExtOrTrunc(Lo
, dl
, DstEltVT
);
698 DAG
.getConstant(WideBits
- SrcEltBits
, dl
,
699 TLI
.getShiftAmountTy(WideVT
, DAG
.getDataLayout()));
700 Lo
= DAG
.getNode(ISD::SHL
, dl
, WideVT
, Lo
, ShAmt
);
701 Lo
= DAG
.getNode(ISD::SRA
, dl
, WideVT
, Lo
, ShAmt
);
702 Lo
= DAG
.getSExtOrTrunc(Lo
, dl
, DstEltVT
);
708 NewChain
= DAG
.getNode(ISD::TokenFactor
, dl
, MVT::Other
, LoadChains
);
709 Value
= DAG
.getBuildVector(Op
.getNode()->getValueType(0), dl
, Vals
);
711 SDValue Scalarized
= TLI
.scalarizeVectorLoad(LD
, DAG
);
712 // Skip past MERGE_VALUE node if known.
713 if (Scalarized
->getOpcode() == ISD::MERGE_VALUES
) {
714 NewChain
= Scalarized
.getOperand(1);
715 Value
= Scalarized
.getOperand(0);
717 NewChain
= Scalarized
.getValue(1);
718 Value
= Scalarized
.getValue(0);
722 AddLegalizedOperand(Op
.getValue(0), Value
);
723 AddLegalizedOperand(Op
.getValue(1), NewChain
);
725 return (Op
.getResNo() ? NewChain
: Value
);
728 SDValue
VectorLegalizer::ExpandStore(SDValue Op
) {
729 StoreSDNode
*ST
= cast
<StoreSDNode
>(Op
.getNode());
730 SDValue TF
= TLI
.scalarizeVectorStore(ST
, DAG
);
731 AddLegalizedOperand(Op
, TF
);
735 SDValue
VectorLegalizer::Expand(SDValue Op
) {
736 switch (Op
->getOpcode()) {
737 case ISD::SIGN_EXTEND_INREG
:
738 return ExpandSEXTINREG(Op
);
739 case ISD::ANY_EXTEND_VECTOR_INREG
:
740 return ExpandANY_EXTEND_VECTOR_INREG(Op
);
741 case ISD::SIGN_EXTEND_VECTOR_INREG
:
742 return ExpandSIGN_EXTEND_VECTOR_INREG(Op
);
743 case ISD::ZERO_EXTEND_VECTOR_INREG
:
744 return ExpandZERO_EXTEND_VECTOR_INREG(Op
);
746 return ExpandBSWAP(Op
);
748 return ExpandVSELECT(Op
);
750 return ExpandSELECT(Op
);
751 case ISD::FP_TO_UINT
:
752 return ExpandFP_TO_UINT(Op
);
753 case ISD::UINT_TO_FP
:
754 return ExpandUINT_TO_FLOAT(Op
);
756 return ExpandFNEG(Op
);
758 return ExpandFSUB(Op
);
760 return UnrollVSETCC(Op
);
762 return ExpandABS(Op
);
763 case ISD::BITREVERSE
:
764 return ExpandBITREVERSE(Op
);
766 return ExpandCTPOP(Op
);
768 case ISD::CTLZ_ZERO_UNDEF
:
769 return ExpandCTLZ(Op
);
771 case ISD::CTTZ_ZERO_UNDEF
:
772 return ExpandCTTZ(Op
);
775 return ExpandFunnelShift(Op
);
778 return ExpandROT(Op
);
781 return ExpandFMINNUM_FMAXNUM(Op
);
786 return ExpandAddSubSat(Op
);
789 return ExpandFixedPointMul(Op
);
790 case ISD::STRICT_FADD
:
791 case ISD::STRICT_FSUB
:
792 case ISD::STRICT_FMUL
:
793 case ISD::STRICT_FDIV
:
794 case ISD::STRICT_FREM
:
795 case ISD::STRICT_FSQRT
:
796 case ISD::STRICT_FMA
:
797 case ISD::STRICT_FPOW
:
798 case ISD::STRICT_FPOWI
:
799 case ISD::STRICT_FSIN
:
800 case ISD::STRICT_FCOS
:
801 case ISD::STRICT_FEXP
:
802 case ISD::STRICT_FEXP2
:
803 case ISD::STRICT_FLOG
:
804 case ISD::STRICT_FLOG10
:
805 case ISD::STRICT_FLOG2
:
806 case ISD::STRICT_FRINT
:
807 case ISD::STRICT_FNEARBYINT
:
808 case ISD::STRICT_FMAXNUM
:
809 case ISD::STRICT_FMINNUM
:
810 case ISD::STRICT_FCEIL
:
811 case ISD::STRICT_FFLOOR
:
812 case ISD::STRICT_FROUND
:
813 case ISD::STRICT_FTRUNC
:
814 return ExpandStrictFPOp(Op
);
816 return DAG
.UnrollVectorOp(Op
.getNode());
820 SDValue
VectorLegalizer::ExpandSELECT(SDValue Op
) {
821 // Lower a select instruction where the condition is a scalar and the
822 // operands are vectors. Lower this select to VSELECT and implement it
823 // using XOR AND OR. The selector bit is broadcasted.
824 EVT VT
= Op
.getValueType();
827 SDValue Mask
= Op
.getOperand(0);
828 SDValue Op1
= Op
.getOperand(1);
829 SDValue Op2
= Op
.getOperand(2);
831 assert(VT
.isVector() && !Mask
.getValueType().isVector()
832 && Op1
.getValueType() == Op2
.getValueType() && "Invalid type");
834 // If we can't even use the basic vector operations of
835 // AND,OR,XOR, we will have to scalarize the op.
836 // Notice that the operation may be 'promoted' which means that it is
837 // 'bitcasted' to another type which is handled.
838 // Also, we need to be able to construct a splat vector using BUILD_VECTOR.
839 if (TLI
.getOperationAction(ISD::AND
, VT
) == TargetLowering::Expand
||
840 TLI
.getOperationAction(ISD::XOR
, VT
) == TargetLowering::Expand
||
841 TLI
.getOperationAction(ISD::OR
, VT
) == TargetLowering::Expand
||
842 TLI
.getOperationAction(ISD::BUILD_VECTOR
, VT
) == TargetLowering::Expand
)
843 return DAG
.UnrollVectorOp(Op
.getNode());
845 // Generate a mask operand.
846 EVT MaskTy
= VT
.changeVectorElementTypeToInteger();
848 // What is the size of each element in the vector mask.
849 EVT BitTy
= MaskTy
.getScalarType();
851 Mask
= DAG
.getSelect(DL
, BitTy
, Mask
,
852 DAG
.getConstant(APInt::getAllOnesValue(BitTy
.getSizeInBits()), DL
,
854 DAG
.getConstant(0, DL
, BitTy
));
856 // Broadcast the mask so that the entire vector is all-one or all zero.
857 Mask
= DAG
.getSplatBuildVector(MaskTy
, DL
, Mask
);
859 // Bitcast the operands to be the same type as the mask.
860 // This is needed when we select between FP types because
861 // the mask is a vector of integers.
862 Op1
= DAG
.getNode(ISD::BITCAST
, DL
, MaskTy
, Op1
);
863 Op2
= DAG
.getNode(ISD::BITCAST
, DL
, MaskTy
, Op2
);
865 SDValue AllOnes
= DAG
.getConstant(
866 APInt::getAllOnesValue(BitTy
.getSizeInBits()), DL
, MaskTy
);
867 SDValue NotMask
= DAG
.getNode(ISD::XOR
, DL
, MaskTy
, Mask
, AllOnes
);
869 Op1
= DAG
.getNode(ISD::AND
, DL
, MaskTy
, Op1
, Mask
);
870 Op2
= DAG
.getNode(ISD::AND
, DL
, MaskTy
, Op2
, NotMask
);
871 SDValue Val
= DAG
.getNode(ISD::OR
, DL
, MaskTy
, Op1
, Op2
);
872 return DAG
.getNode(ISD::BITCAST
, DL
, Op
.getValueType(), Val
);
875 SDValue
VectorLegalizer::ExpandSEXTINREG(SDValue Op
) {
876 EVT VT
= Op
.getValueType();
878 // Make sure that the SRA and SHL instructions are available.
879 if (TLI
.getOperationAction(ISD::SRA
, VT
) == TargetLowering::Expand
||
880 TLI
.getOperationAction(ISD::SHL
, VT
) == TargetLowering::Expand
)
881 return DAG
.UnrollVectorOp(Op
.getNode());
884 EVT OrigTy
= cast
<VTSDNode
>(Op
->getOperand(1))->getVT();
886 unsigned BW
= VT
.getScalarSizeInBits();
887 unsigned OrigBW
= OrigTy
.getScalarSizeInBits();
888 SDValue ShiftSz
= DAG
.getConstant(BW
- OrigBW
, DL
, VT
);
890 Op
= Op
.getOperand(0);
891 Op
= DAG
.getNode(ISD::SHL
, DL
, VT
, Op
, ShiftSz
);
892 return DAG
.getNode(ISD::SRA
, DL
, VT
, Op
, ShiftSz
);
895 // Generically expand a vector anyext in register to a shuffle of the relevant
896 // lanes into the appropriate locations, with other lanes left undef.
897 SDValue
VectorLegalizer::ExpandANY_EXTEND_VECTOR_INREG(SDValue Op
) {
899 EVT VT
= Op
.getValueType();
900 int NumElements
= VT
.getVectorNumElements();
901 SDValue Src
= Op
.getOperand(0);
902 EVT SrcVT
= Src
.getValueType();
903 int NumSrcElements
= SrcVT
.getVectorNumElements();
905 // Build a base mask of undef shuffles.
906 SmallVector
<int, 16> ShuffleMask
;
907 ShuffleMask
.resize(NumSrcElements
, -1);
909 // Place the extended lanes into the correct locations.
910 int ExtLaneScale
= NumSrcElements
/ NumElements
;
911 int EndianOffset
= DAG
.getDataLayout().isBigEndian() ? ExtLaneScale
- 1 : 0;
912 for (int i
= 0; i
< NumElements
; ++i
)
913 ShuffleMask
[i
* ExtLaneScale
+ EndianOffset
] = i
;
916 ISD::BITCAST
, DL
, VT
,
917 DAG
.getVectorShuffle(SrcVT
, DL
, Src
, DAG
.getUNDEF(SrcVT
), ShuffleMask
));
920 SDValue
VectorLegalizer::ExpandSIGN_EXTEND_VECTOR_INREG(SDValue Op
) {
922 EVT VT
= Op
.getValueType();
923 SDValue Src
= Op
.getOperand(0);
924 EVT SrcVT
= Src
.getValueType();
926 // First build an any-extend node which can be legalized above when we
927 // recurse through it.
928 Op
= DAG
.getNode(ISD::ANY_EXTEND_VECTOR_INREG
, DL
, VT
, Src
);
930 // Now we need sign extend. Do this by shifting the elements. Even if these
931 // aren't legal operations, they have a better chance of being legalized
932 // without full scalarization than the sign extension does.
933 unsigned EltWidth
= VT
.getScalarSizeInBits();
934 unsigned SrcEltWidth
= SrcVT
.getScalarSizeInBits();
935 SDValue ShiftAmount
= DAG
.getConstant(EltWidth
- SrcEltWidth
, DL
, VT
);
936 return DAG
.getNode(ISD::SRA
, DL
, VT
,
937 DAG
.getNode(ISD::SHL
, DL
, VT
, Op
, ShiftAmount
),
941 // Generically expand a vector zext in register to a shuffle of the relevant
942 // lanes into the appropriate locations, a blend of zero into the high bits,
943 // and a bitcast to the wider element type.
944 SDValue
VectorLegalizer::ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op
) {
946 EVT VT
= Op
.getValueType();
947 int NumElements
= VT
.getVectorNumElements();
948 SDValue Src
= Op
.getOperand(0);
949 EVT SrcVT
= Src
.getValueType();
950 int NumSrcElements
= SrcVT
.getVectorNumElements();
952 // Build up a zero vector to blend into this one.
953 SDValue Zero
= DAG
.getConstant(0, DL
, SrcVT
);
955 // Shuffle the incoming lanes into the correct position, and pull all other
956 // lanes from the zero vector.
957 SmallVector
<int, 16> ShuffleMask
;
958 ShuffleMask
.reserve(NumSrcElements
);
959 for (int i
= 0; i
< NumSrcElements
; ++i
)
960 ShuffleMask
.push_back(i
);
962 int ExtLaneScale
= NumSrcElements
/ NumElements
;
963 int EndianOffset
= DAG
.getDataLayout().isBigEndian() ? ExtLaneScale
- 1 : 0;
964 for (int i
= 0; i
< NumElements
; ++i
)
965 ShuffleMask
[i
* ExtLaneScale
+ EndianOffset
] = NumSrcElements
+ i
;
967 return DAG
.getNode(ISD::BITCAST
, DL
, VT
,
968 DAG
.getVectorShuffle(SrcVT
, DL
, Zero
, Src
, ShuffleMask
));
971 static void createBSWAPShuffleMask(EVT VT
, SmallVectorImpl
<int> &ShuffleMask
) {
972 int ScalarSizeInBytes
= VT
.getScalarSizeInBits() / 8;
973 for (int I
= 0, E
= VT
.getVectorNumElements(); I
!= E
; ++I
)
974 for (int J
= ScalarSizeInBytes
- 1; J
>= 0; --J
)
975 ShuffleMask
.push_back((I
* ScalarSizeInBytes
) + J
);
978 SDValue
VectorLegalizer::ExpandBSWAP(SDValue Op
) {
979 EVT VT
= Op
.getValueType();
981 // Generate a byte wise shuffle mask for the BSWAP.
982 SmallVector
<int, 16> ShuffleMask
;
983 createBSWAPShuffleMask(VT
, ShuffleMask
);
984 EVT ByteVT
= EVT::getVectorVT(*DAG
.getContext(), MVT::i8
, ShuffleMask
.size());
986 // Only emit a shuffle if the mask is legal.
987 if (!TLI
.isShuffleMaskLegal(ShuffleMask
, ByteVT
))
988 return DAG
.UnrollVectorOp(Op
.getNode());
991 Op
= DAG
.getNode(ISD::BITCAST
, DL
, ByteVT
, Op
.getOperand(0));
992 Op
= DAG
.getVectorShuffle(ByteVT
, DL
, Op
, DAG
.getUNDEF(ByteVT
), ShuffleMask
);
993 return DAG
.getNode(ISD::BITCAST
, DL
, VT
, Op
);
996 SDValue
VectorLegalizer::ExpandBITREVERSE(SDValue Op
) {
997 EVT VT
= Op
.getValueType();
999 // If we have the scalar operation, it's probably cheaper to unroll it.
1000 if (TLI
.isOperationLegalOrCustom(ISD::BITREVERSE
, VT
.getScalarType()))
1001 return DAG
.UnrollVectorOp(Op
.getNode());
1003 // If the vector element width is a whole number of bytes, test if its legal
1004 // to BSWAP shuffle the bytes and then perform the BITREVERSE on the byte
1005 // vector. This greatly reduces the number of bit shifts necessary.
1006 unsigned ScalarSizeInBits
= VT
.getScalarSizeInBits();
1007 if (ScalarSizeInBits
> 8 && (ScalarSizeInBits
% 8) == 0) {
1008 SmallVector
<int, 16> BSWAPMask
;
1009 createBSWAPShuffleMask(VT
, BSWAPMask
);
1011 EVT ByteVT
= EVT::getVectorVT(*DAG
.getContext(), MVT::i8
, BSWAPMask
.size());
1012 if (TLI
.isShuffleMaskLegal(BSWAPMask
, ByteVT
) &&
1013 (TLI
.isOperationLegalOrCustom(ISD::BITREVERSE
, ByteVT
) ||
1014 (TLI
.isOperationLegalOrCustom(ISD::SHL
, ByteVT
) &&
1015 TLI
.isOperationLegalOrCustom(ISD::SRL
, ByteVT
) &&
1016 TLI
.isOperationLegalOrCustomOrPromote(ISD::AND
, ByteVT
) &&
1017 TLI
.isOperationLegalOrCustomOrPromote(ISD::OR
, ByteVT
)))) {
1019 Op
= DAG
.getNode(ISD::BITCAST
, DL
, ByteVT
, Op
.getOperand(0));
1020 Op
= DAG
.getVectorShuffle(ByteVT
, DL
, Op
, DAG
.getUNDEF(ByteVT
),
1022 Op
= DAG
.getNode(ISD::BITREVERSE
, DL
, ByteVT
, Op
);
1023 return DAG
.getNode(ISD::BITCAST
, DL
, VT
, Op
);
1027 // If we have the appropriate vector bit operations, it is better to use them
1028 // than unrolling and expanding each component.
1029 if (!TLI
.isOperationLegalOrCustom(ISD::SHL
, VT
) ||
1030 !TLI
.isOperationLegalOrCustom(ISD::SRL
, VT
) ||
1031 !TLI
.isOperationLegalOrCustomOrPromote(ISD::AND
, VT
) ||
1032 !TLI
.isOperationLegalOrCustomOrPromote(ISD::OR
, VT
))
1033 return DAG
.UnrollVectorOp(Op
.getNode());
1035 // Let LegalizeDAG handle this later.
1039 SDValue
VectorLegalizer::ExpandVSELECT(SDValue Op
) {
1040 // Implement VSELECT in terms of XOR, AND, OR
1041 // on platforms which do not support blend natively.
1044 SDValue Mask
= Op
.getOperand(0);
1045 SDValue Op1
= Op
.getOperand(1);
1046 SDValue Op2
= Op
.getOperand(2);
1048 EVT VT
= Mask
.getValueType();
1050 // If we can't even use the basic vector operations of
1051 // AND,OR,XOR, we will have to scalarize the op.
1052 // Notice that the operation may be 'promoted' which means that it is
1053 // 'bitcasted' to another type which is handled.
1054 // This operation also isn't safe with AND, OR, XOR when the boolean
1055 // type is 0/1 as we need an all ones vector constant to mask with.
1056 // FIXME: Sign extend 1 to all ones if thats legal on the target.
1057 if (TLI
.getOperationAction(ISD::AND
, VT
) == TargetLowering::Expand
||
1058 TLI
.getOperationAction(ISD::XOR
, VT
) == TargetLowering::Expand
||
1059 TLI
.getOperationAction(ISD::OR
, VT
) == TargetLowering::Expand
||
1060 TLI
.getBooleanContents(Op1
.getValueType()) !=
1061 TargetLowering::ZeroOrNegativeOneBooleanContent
)
1062 return DAG
.UnrollVectorOp(Op
.getNode());
1064 // If the mask and the type are different sizes, unroll the vector op. This
1065 // can occur when getSetCCResultType returns something that is different in
1066 // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8.
1067 if (VT
.getSizeInBits() != Op1
.getValueSizeInBits())
1068 return DAG
.UnrollVectorOp(Op
.getNode());
1070 // Bitcast the operands to be the same type as the mask.
1071 // This is needed when we select between FP types because
1072 // the mask is a vector of integers.
1073 Op1
= DAG
.getNode(ISD::BITCAST
, DL
, VT
, Op1
);
1074 Op2
= DAG
.getNode(ISD::BITCAST
, DL
, VT
, Op2
);
1076 SDValue AllOnes
= DAG
.getConstant(
1077 APInt::getAllOnesValue(VT
.getScalarSizeInBits()), DL
, VT
);
1078 SDValue NotMask
= DAG
.getNode(ISD::XOR
, DL
, VT
, Mask
, AllOnes
);
1080 Op1
= DAG
.getNode(ISD::AND
, DL
, VT
, Op1
, Mask
);
1081 Op2
= DAG
.getNode(ISD::AND
, DL
, VT
, Op2
, NotMask
);
1082 SDValue Val
= DAG
.getNode(ISD::OR
, DL
, VT
, Op1
, Op2
);
1083 return DAG
.getNode(ISD::BITCAST
, DL
, Op
.getValueType(), Val
);
1086 SDValue
VectorLegalizer::ExpandABS(SDValue Op
) {
1087 // Attempt to expand using TargetLowering.
1089 if (TLI
.expandABS(Op
.getNode(), Result
, DAG
))
1092 // Otherwise go ahead and unroll.
1093 return DAG
.UnrollVectorOp(Op
.getNode());
1096 SDValue
VectorLegalizer::ExpandFP_TO_UINT(SDValue Op
) {
1097 // Attempt to expand using TargetLowering.
1099 if (TLI
.expandFP_TO_UINT(Op
.getNode(), Result
, DAG
))
1102 // Otherwise go ahead and unroll.
1103 return DAG
.UnrollVectorOp(Op
.getNode());
1106 SDValue
VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op
) {
1107 EVT VT
= Op
.getOperand(0).getValueType();
1110 // Attempt to expand using TargetLowering.
1112 if (TLI
.expandUINT_TO_FP(Op
.getNode(), Result
, DAG
))
1115 // Make sure that the SINT_TO_FP and SRL instructions are available.
1116 if (TLI
.getOperationAction(ISD::SINT_TO_FP
, VT
) == TargetLowering::Expand
||
1117 TLI
.getOperationAction(ISD::SRL
, VT
) == TargetLowering::Expand
)
1118 return DAG
.UnrollVectorOp(Op
.getNode());
1120 unsigned BW
= VT
.getScalarSizeInBits();
1121 assert((BW
== 64 || BW
== 32) &&
1122 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
1124 SDValue HalfWord
= DAG
.getConstant(BW
/ 2, DL
, VT
);
1126 // Constants to clear the upper part of the word.
1127 // Notice that we can also use SHL+SHR, but using a constant is slightly
1129 uint64_t HWMask
= (BW
== 64) ? 0x00000000FFFFFFFF : 0x0000FFFF;
1130 SDValue HalfWordMask
= DAG
.getConstant(HWMask
, DL
, VT
);
1132 // Two to the power of half-word-size.
1133 SDValue TWOHW
= DAG
.getConstantFP(1ULL << (BW
/ 2), DL
, Op
.getValueType());
1135 // Clear upper part of LO, lower HI
1136 SDValue HI
= DAG
.getNode(ISD::SRL
, DL
, VT
, Op
.getOperand(0), HalfWord
);
1137 SDValue LO
= DAG
.getNode(ISD::AND
, DL
, VT
, Op
.getOperand(0), HalfWordMask
);
1139 // Convert hi and lo to floats
1140 // Convert the hi part back to the upper values
1141 // TODO: Can any fast-math-flags be set on these nodes?
1142 SDValue fHI
= DAG
.getNode(ISD::SINT_TO_FP
, DL
, Op
.getValueType(), HI
);
1143 fHI
= DAG
.getNode(ISD::FMUL
, DL
, Op
.getValueType(), fHI
, TWOHW
);
1144 SDValue fLO
= DAG
.getNode(ISD::SINT_TO_FP
, DL
, Op
.getValueType(), LO
);
1146 // Add the two halves
1147 return DAG
.getNode(ISD::FADD
, DL
, Op
.getValueType(), fHI
, fLO
);
1150 SDValue
VectorLegalizer::ExpandFNEG(SDValue Op
) {
1151 if (TLI
.isOperationLegalOrCustom(ISD::FSUB
, Op
.getValueType())) {
1153 SDValue Zero
= DAG
.getConstantFP(-0.0, DL
, Op
.getValueType());
1154 // TODO: If FNEG had fast-math-flags, they'd get propagated to this FSUB.
1155 return DAG
.getNode(ISD::FSUB
, DL
, Op
.getValueType(),
1156 Zero
, Op
.getOperand(0));
1158 return DAG
.UnrollVectorOp(Op
.getNode());
1161 SDValue
VectorLegalizer::ExpandFSUB(SDValue Op
) {
1162 // For floating-point values, (a-b) is the same as a+(-b). If FNEG is legal,
1163 // we can defer this to operation legalization where it will be lowered as
1165 EVT VT
= Op
.getValueType();
1166 if (TLI
.isOperationLegalOrCustom(ISD::FNEG
, VT
) &&
1167 TLI
.isOperationLegalOrCustom(ISD::FADD
, VT
))
1168 return Op
; // Defer to LegalizeDAG
1170 return DAG
.UnrollVectorOp(Op
.getNode());
1173 SDValue
VectorLegalizer::ExpandCTPOP(SDValue Op
) {
1175 if (TLI
.expandCTPOP(Op
.getNode(), Result
, DAG
))
1178 return DAG
.UnrollVectorOp(Op
.getNode());
1181 SDValue
VectorLegalizer::ExpandCTLZ(SDValue Op
) {
1183 if (TLI
.expandCTLZ(Op
.getNode(), Result
, DAG
))
1186 return DAG
.UnrollVectorOp(Op
.getNode());
1189 SDValue
VectorLegalizer::ExpandCTTZ(SDValue Op
) {
1191 if (TLI
.expandCTTZ(Op
.getNode(), Result
, DAG
))
1194 return DAG
.UnrollVectorOp(Op
.getNode());
1197 SDValue
VectorLegalizer::ExpandFunnelShift(SDValue Op
) {
1199 if (TLI
.expandFunnelShift(Op
.getNode(), Result
, DAG
))
1202 return DAG
.UnrollVectorOp(Op
.getNode());
1205 SDValue
VectorLegalizer::ExpandROT(SDValue Op
) {
1207 if (TLI
.expandROT(Op
.getNode(), Result
, DAG
))
1210 return DAG
.UnrollVectorOp(Op
.getNode());
1213 SDValue
VectorLegalizer::ExpandFMINNUM_FMAXNUM(SDValue Op
) {
1214 if (SDValue Expanded
= TLI
.expandFMINNUM_FMAXNUM(Op
.getNode(), DAG
))
1216 return DAG
.UnrollVectorOp(Op
.getNode());
1219 SDValue
VectorLegalizer::ExpandAddSubSat(SDValue Op
) {
1220 if (SDValue Expanded
= TLI
.expandAddSubSat(Op
.getNode(), DAG
))
1222 return DAG
.UnrollVectorOp(Op
.getNode());
1225 SDValue
VectorLegalizer::ExpandFixedPointMul(SDValue Op
) {
1226 if (SDValue Expanded
= TLI
.expandFixedPointMul(Op
.getNode(), DAG
))
1228 return DAG
.UnrollVectorOp(Op
.getNode());
1231 SDValue
VectorLegalizer::ExpandStrictFPOp(SDValue Op
) {
1232 EVT VT
= Op
.getValueType();
1233 EVT EltVT
= VT
.getVectorElementType();
1234 unsigned NumElems
= VT
.getVectorNumElements();
1235 unsigned NumOpers
= Op
.getNumOperands();
1236 const TargetLowering
&TLI
= DAG
.getTargetLoweringInfo();
1237 EVT ValueVTs
[] = {EltVT
, MVT::Other
};
1238 SDValue Chain
= Op
.getOperand(0);
1241 SmallVector
<SDValue
, 32> OpValues
;
1242 SmallVector
<SDValue
, 32> OpChains
;
1243 for (unsigned i
= 0; i
< NumElems
; ++i
) {
1244 SmallVector
<SDValue
, 4> Opers
;
1245 SDValue Idx
= DAG
.getConstant(i
, dl
,
1246 TLI
.getVectorIdxTy(DAG
.getDataLayout()));
1248 // The Chain is the first operand.
1249 Opers
.push_back(Chain
);
1251 // Now process the remaining operands.
1252 for (unsigned j
= 1; j
< NumOpers
; ++j
) {
1253 SDValue Oper
= Op
.getOperand(j
);
1254 EVT OperVT
= Oper
.getValueType();
1256 if (OperVT
.isVector())
1257 Oper
= DAG
.getNode(ISD::EXTRACT_VECTOR_ELT
, dl
,
1260 Opers
.push_back(Oper
);
1263 SDValue ScalarOp
= DAG
.getNode(Op
->getOpcode(), dl
, ValueVTs
, Opers
);
1265 OpValues
.push_back(ScalarOp
.getValue(0));
1266 OpChains
.push_back(ScalarOp
.getValue(1));
1269 SDValue Result
= DAG
.getBuildVector(VT
, dl
, OpValues
);
1270 SDValue NewChain
= DAG
.getNode(ISD::TokenFactor
, dl
, MVT::Other
, OpChains
);
1272 AddLegalizedOperand(Op
.getValue(0), Result
);
1273 AddLegalizedOperand(Op
.getValue(1), NewChain
);
1275 return Op
.getResNo() ? NewChain
: Result
;
1278 SDValue
VectorLegalizer::UnrollVSETCC(SDValue Op
) {
1279 EVT VT
= Op
.getValueType();
1280 unsigned NumElems
= VT
.getVectorNumElements();
1281 EVT EltVT
= VT
.getVectorElementType();
1282 SDValue LHS
= Op
.getOperand(0), RHS
= Op
.getOperand(1), CC
= Op
.getOperand(2);
1283 EVT TmpEltVT
= LHS
.getValueType().getVectorElementType();
1285 SmallVector
<SDValue
, 8> Ops(NumElems
);
1286 for (unsigned i
= 0; i
< NumElems
; ++i
) {
1287 SDValue LHSElem
= DAG
.getNode(
1288 ISD::EXTRACT_VECTOR_ELT
, dl
, TmpEltVT
, LHS
,
1289 DAG
.getConstant(i
, dl
, TLI
.getVectorIdxTy(DAG
.getDataLayout())));
1290 SDValue RHSElem
= DAG
.getNode(
1291 ISD::EXTRACT_VECTOR_ELT
, dl
, TmpEltVT
, RHS
,
1292 DAG
.getConstant(i
, dl
, TLI
.getVectorIdxTy(DAG
.getDataLayout())));
1293 Ops
[i
] = DAG
.getNode(ISD::SETCC
, dl
,
1294 TLI
.getSetCCResultType(DAG
.getDataLayout(),
1295 *DAG
.getContext(), TmpEltVT
),
1296 LHSElem
, RHSElem
, CC
);
1297 Ops
[i
] = DAG
.getSelect(dl
, EltVT
, Ops
[i
],
1298 DAG
.getConstant(APInt::getAllOnesValue
1299 (EltVT
.getSizeInBits()), dl
, EltVT
),
1300 DAG
.getConstant(0, dl
, EltVT
));
1302 return DAG
.getBuildVector(VT
, dl
, Ops
);
1305 bool SelectionDAG::LegalizeVectors() {
1306 return VectorLegalizer(*this).Run();