1 //===----- LegalizeIntegerTypes.cpp - Legalization of integer types -------===//
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 integer type expansion and promotion for LegalizeTypes.
10 // Promotion is the act of changing a computation in an illegal type into a
11 // computation in a larger type. For example, implementing i8 arithmetic in an
12 // i32 register (often needed on powerpc).
13 // Expansion is the act of changing a computation in an illegal type into a
14 // computation in two identical registers of a smaller type. For example,
15 // implementing i64 arithmetic in two i32 registers (often needed on 32-bit
18 //===----------------------------------------------------------------------===//
20 #include "LegalizeTypes.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/CodeGen/StackMaps.h"
23 #include "llvm/CodeGen/TargetLowering.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/KnownBits.h"
27 #include "llvm/Support/raw_ostream.h"
31 #define DEBUG_TYPE "legalize-types"
33 //===----------------------------------------------------------------------===//
34 // Integer Result Promotion
35 //===----------------------------------------------------------------------===//
37 /// PromoteIntegerResult - This method is called when a result of a node is
38 /// found to be in need of promotion to a larger type. At this point, the node
39 /// may also have invalid operands or may have other results that need
40 /// expansion, we just know that (at least) one result needs promotion.
41 void DAGTypeLegalizer::PromoteIntegerResult(SDNode
*N
, unsigned ResNo
) {
42 LLVM_DEBUG(dbgs() << "Promote integer result: "; N
->dump(&DAG
));
43 SDValue Res
= SDValue();
45 // See if the target wants to custom expand this node.
46 if (CustomLowerNode(N
, N
->getValueType(ResNo
), true)) {
47 LLVM_DEBUG(dbgs() << "Node has been custom expanded, done\n");
51 switch (N
->getOpcode()) {
54 dbgs() << "PromoteIntegerResult #" << ResNo
<< ": ";
55 N
->dump(&DAG
); dbgs() << "\n";
57 report_fatal_error("Do not know how to promote this operator!");
58 case ISD::MERGE_VALUES
:Res
= PromoteIntRes_MERGE_VALUES(N
, ResNo
); break;
59 case ISD::AssertSext
: Res
= PromoteIntRes_AssertSext(N
); break;
60 case ISD::AssertZext
: Res
= PromoteIntRes_AssertZext(N
); break;
61 case ISD::BITCAST
: Res
= PromoteIntRes_BITCAST(N
); break;
62 case ISD::VP_BITREVERSE
:
63 case ISD::BITREVERSE
: Res
= PromoteIntRes_BITREVERSE(N
); break;
65 case ISD::BSWAP
: Res
= PromoteIntRes_BSWAP(N
); break;
66 case ISD::BUILD_PAIR
: Res
= PromoteIntRes_BUILD_PAIR(N
); break;
67 case ISD::Constant
: Res
= PromoteIntRes_Constant(N
); break;
68 case ISD::VP_CTLZ_ZERO_UNDEF
:
70 case ISD::CTLZ_ZERO_UNDEF
:
71 case ISD::CTLZ
: Res
= PromoteIntRes_CTLZ(N
); break;
74 case ISD::CTPOP
: Res
= PromoteIntRes_CTPOP_PARITY(N
); break;
75 case ISD::VP_CTTZ_ZERO_UNDEF
:
77 case ISD::CTTZ_ZERO_UNDEF
:
78 case ISD::CTTZ
: Res
= PromoteIntRes_CTTZ(N
); break;
79 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF
:
80 case ISD::VP_CTTZ_ELTS
:
81 Res
= PromoteIntRes_VP_CttzElements(N
);
83 case ISD::EXTRACT_VECTOR_ELT
:
84 Res
= PromoteIntRes_EXTRACT_VECTOR_ELT(N
); break;
85 case ISD::LOAD
: Res
= PromoteIntRes_LOAD(cast
<LoadSDNode
>(N
)); break;
87 Res
= PromoteIntRes_VP_LOAD(cast
<VPLoadSDNode
>(N
));
89 case ISD::MLOAD
: Res
= PromoteIntRes_MLOAD(cast
<MaskedLoadSDNode
>(N
));
91 case ISD::MGATHER
: Res
= PromoteIntRes_MGATHER(cast
<MaskedGatherSDNode
>(N
));
93 case ISD::VECTOR_COMPRESS
:
94 Res
= PromoteIntRes_VECTOR_COMPRESS(N
);
100 Res
= PromoteIntRes_Select(N
);
102 case ISD::SELECT_CC
: Res
= PromoteIntRes_SELECT_CC(N
); break;
103 case ISD::STRICT_FSETCC
:
104 case ISD::STRICT_FSETCCS
:
105 case ISD::SETCC
: Res
= PromoteIntRes_SETCC(N
); break;
107 case ISD::SMAX
: Res
= PromoteIntRes_SExtIntBinOp(N
); break;
109 case ISD::UMAX
: Res
= PromoteIntRes_UMINUMAX(N
); break;
112 case ISD::VP_SHL
: Res
= PromoteIntRes_SHL(N
); break;
113 case ISD::SIGN_EXTEND_INREG
:
114 Res
= PromoteIntRes_SIGN_EXTEND_INREG(N
); break;
116 case ISD::VP_SRA
: Res
= PromoteIntRes_SRA(N
); break;
118 case ISD::VP_SRL
: Res
= PromoteIntRes_SRL(N
); break;
119 case ISD::VP_TRUNCATE
:
120 case ISD::TRUNCATE
: Res
= PromoteIntRes_TRUNCATE(N
); break;
121 case ISD::UNDEF
: Res
= PromoteIntRes_UNDEF(N
); break;
122 case ISD::VAARG
: Res
= PromoteIntRes_VAARG(N
); break;
123 case ISD::VSCALE
: Res
= PromoteIntRes_VSCALE(N
); break;
125 case ISD::EXTRACT_SUBVECTOR
:
126 Res
= PromoteIntRes_EXTRACT_SUBVECTOR(N
); break;
127 case ISD::INSERT_SUBVECTOR
:
128 Res
= PromoteIntRes_INSERT_SUBVECTOR(N
); break;
129 case ISD::VECTOR_REVERSE
:
130 Res
= PromoteIntRes_VECTOR_REVERSE(N
); break;
131 case ISD::VECTOR_SHUFFLE
:
132 Res
= PromoteIntRes_VECTOR_SHUFFLE(N
); break;
133 case ISD::VECTOR_SPLICE
:
134 Res
= PromoteIntRes_VECTOR_SPLICE(N
); break;
135 case ISD::VECTOR_INTERLEAVE
:
136 case ISD::VECTOR_DEINTERLEAVE
:
137 Res
= PromoteIntRes_VECTOR_INTERLEAVE_DEINTERLEAVE(N
);
139 case ISD::INSERT_VECTOR_ELT
:
140 Res
= PromoteIntRes_INSERT_VECTOR_ELT(N
); break;
141 case ISD::BUILD_VECTOR
:
142 Res
= PromoteIntRes_BUILD_VECTOR(N
);
144 case ISD::SPLAT_VECTOR
:
145 case ISD::SCALAR_TO_VECTOR
:
146 case ISD::EXPERIMENTAL_VP_SPLAT
:
147 Res
= PromoteIntRes_ScalarOp(N
);
149 case ISD::STEP_VECTOR
: Res
= PromoteIntRes_STEP_VECTOR(N
); break;
150 case ISD::CONCAT_VECTORS
:
151 Res
= PromoteIntRes_CONCAT_VECTORS(N
); break;
153 case ISD::ANY_EXTEND_VECTOR_INREG
:
154 case ISD::SIGN_EXTEND_VECTOR_INREG
:
155 case ISD::ZERO_EXTEND_VECTOR_INREG
:
156 Res
= PromoteIntRes_EXTEND_VECTOR_INREG(N
); break;
158 case ISD::SIGN_EXTEND
:
159 case ISD::VP_SIGN_EXTEND
:
160 case ISD::ZERO_EXTEND
:
161 case ISD::VP_ZERO_EXTEND
:
162 case ISD::ANY_EXTEND
: Res
= PromoteIntRes_INT_EXTEND(N
); break;
164 case ISD::VP_FP_TO_SINT
:
165 case ISD::VP_FP_TO_UINT
:
166 case ISD::STRICT_FP_TO_SINT
:
167 case ISD::STRICT_FP_TO_UINT
:
168 case ISD::FP_TO_SINT
:
169 case ISD::FP_TO_UINT
: Res
= PromoteIntRes_FP_TO_XINT(N
); break;
171 case ISD::FP_TO_SINT_SAT
:
172 case ISD::FP_TO_UINT_SAT
:
173 Res
= PromoteIntRes_FP_TO_XINT_SAT(N
); break;
175 case ISD::FP_TO_BF16
:
176 case ISD::FP_TO_FP16
:
177 Res
= PromoteIntRes_FP_TO_FP16_BF16(N
);
179 case ISD::STRICT_FP_TO_BF16
:
180 case ISD::STRICT_FP_TO_FP16
:
181 Res
= PromoteIntRes_STRICT_FP_TO_FP16_BF16(N
);
183 case ISD::GET_ROUNDING
: Res
= PromoteIntRes_GET_ROUNDING(N
); break;
196 case ISD::VP_MUL
: Res
= PromoteIntRes_SimpleIntBinOp(N
); break;
206 case ISD::VP_SREM
: Res
= PromoteIntRes_SExtIntBinOp(N
); break;
216 case ISD::VP_UREM
: Res
= PromoteIntRes_ZExtIntBinOp(N
); break;
219 case ISD::SSUBO
: Res
= PromoteIntRes_SADDSUBO(N
, ResNo
); break;
221 case ISD::USUBO
: Res
= PromoteIntRes_UADDSUBO(N
, ResNo
); break;
223 case ISD::UMULO
: Res
= PromoteIntRes_XMULO(N
, ResNo
); break;
227 case ISD::UADDO_CARRY
:
228 case ISD::USUBO_CARRY
: Res
= PromoteIntRes_UADDSUBO_CARRY(N
, ResNo
); break;
230 case ISD::SADDO_CARRY
:
231 case ISD::SSUBO_CARRY
: Res
= PromoteIntRes_SADDSUBO_CARRY(N
, ResNo
); break;
239 Res
= PromoteIntRes_ADDSUBSHLSAT
<EmptyMatchContext
>(N
);
241 case ISD::VP_SADDSAT
:
242 case ISD::VP_UADDSAT
:
243 case ISD::VP_SSUBSAT
:
244 case ISD::VP_USUBSAT
:
245 Res
= PromoteIntRes_ADDSUBSHLSAT
<VPMatchContext
>(N
);
250 Res
= PromoteIntRes_CMP(N
);
254 case ISD::SMULFIXSAT
:
256 case ISD::UMULFIXSAT
: Res
= PromoteIntRes_MULFIX(N
); break;
259 case ISD::SDIVFIXSAT
:
261 case ISD::UDIVFIXSAT
: Res
= PromoteIntRes_DIVFIX(N
); break;
263 case ISD::ABS
: Res
= PromoteIntRes_ABS(N
); break;
265 case ISD::ATOMIC_LOAD
:
266 Res
= PromoteIntRes_Atomic0(cast
<AtomicSDNode
>(N
)); break;
268 case ISD::ATOMIC_LOAD_ADD
:
269 case ISD::ATOMIC_LOAD_SUB
:
270 case ISD::ATOMIC_LOAD_AND
:
271 case ISD::ATOMIC_LOAD_CLR
:
272 case ISD::ATOMIC_LOAD_OR
:
273 case ISD::ATOMIC_LOAD_XOR
:
274 case ISD::ATOMIC_LOAD_NAND
:
275 case ISD::ATOMIC_LOAD_MIN
:
276 case ISD::ATOMIC_LOAD_MAX
:
277 case ISD::ATOMIC_LOAD_UMIN
:
278 case ISD::ATOMIC_LOAD_UMAX
:
279 case ISD::ATOMIC_SWAP
:
280 Res
= PromoteIntRes_Atomic1(cast
<AtomicSDNode
>(N
)); break;
282 case ISD::ATOMIC_CMP_SWAP
:
283 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS
:
284 Res
= PromoteIntRes_AtomicCmpSwap(cast
<AtomicSDNode
>(N
), ResNo
);
287 case ISD::VECREDUCE_ADD
:
288 case ISD::VECREDUCE_MUL
:
289 case ISD::VECREDUCE_AND
:
290 case ISD::VECREDUCE_OR
:
291 case ISD::VECREDUCE_XOR
:
292 case ISD::VECREDUCE_SMAX
:
293 case ISD::VECREDUCE_SMIN
:
294 case ISD::VECREDUCE_UMAX
:
295 case ISD::VECREDUCE_UMIN
:
296 Res
= PromoteIntRes_VECREDUCE(N
);
299 case ISD::VP_REDUCE_ADD
:
300 case ISD::VP_REDUCE_MUL
:
301 case ISD::VP_REDUCE_AND
:
302 case ISD::VP_REDUCE_OR
:
303 case ISD::VP_REDUCE_XOR
:
304 case ISD::VP_REDUCE_SMAX
:
305 case ISD::VP_REDUCE_SMIN
:
306 case ISD::VP_REDUCE_UMAX
:
307 case ISD::VP_REDUCE_UMIN
:
308 Res
= PromoteIntRes_VP_REDUCE(N
);
312 Res
= PromoteIntRes_FREEZE(N
);
317 Res
= PromoteIntRes_Rotate(N
);
322 Res
= PromoteIntRes_FunnelShift(N
);
327 Res
= PromoteIntRes_VPFunnelShift(N
);
330 case ISD::IS_FPCLASS
:
331 Res
= PromoteIntRes_IS_FPCLASS(N
);
334 Res
= PromoteIntRes_FFREXP(N
);
339 Res
= PromoteIntRes_XRINT(N
);
342 case ISD::PATCHPOINT
:
343 Res
= PromoteIntRes_PATCHPOINT(N
);
347 // If the result is null then the sub-method took care of registering it.
349 SetPromotedInteger(SDValue(N
, ResNo
), Res
);
352 SDValue
DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode
*N
,
354 SDValue Op
= DisintegrateMERGE_VALUES(N
, ResNo
);
355 return GetPromotedInteger(Op
);
358 SDValue
DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode
*N
) {
359 // Sign-extend the new bits, and continue the assertion.
360 SDValue Op
= SExtPromotedInteger(N
->getOperand(0));
361 return DAG
.getNode(ISD::AssertSext
, SDLoc(N
),
362 Op
.getValueType(), Op
, N
->getOperand(1));
365 SDValue
DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode
*N
) {
366 // Zero the new bits, and continue the assertion.
367 SDValue Op
= ZExtPromotedInteger(N
->getOperand(0));
368 return DAG
.getNode(ISD::AssertZext
, SDLoc(N
),
369 Op
.getValueType(), Op
, N
->getOperand(1));
372 SDValue
DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode
*N
) {
373 EVT ResVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
374 SDValue Res
= DAG
.getAtomic(N
->getOpcode(), SDLoc(N
),
375 N
->getMemoryVT(), ResVT
,
376 N
->getChain(), N
->getBasePtr(),
378 if (N
->getOpcode() == ISD::ATOMIC_LOAD
) {
379 ISD::LoadExtType ETy
= cast
<AtomicSDNode
>(N
)->getExtensionType();
380 if (ETy
== ISD::NON_EXTLOAD
) {
381 switch (TLI
.getExtendForAtomicOps()) {
382 case ISD::SIGN_EXTEND
:
385 case ISD::ZERO_EXTEND
:
388 case ISD::ANY_EXTEND
:
392 llvm_unreachable("Invalid atomic op extension");
395 cast
<AtomicSDNode
>(Res
)->setExtensionType(ETy
);
398 // Legalize the chain result - switch anything that used the old chain to
400 ReplaceValueWith(SDValue(N
, 1), Res
.getValue(1));
404 SDValue
DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode
*N
) {
405 SDValue Op2
= GetPromotedInteger(N
->getOperand(2));
406 SDValue Res
= DAG
.getAtomic(N
->getOpcode(), SDLoc(N
),
408 N
->getChain(), N
->getBasePtr(),
409 Op2
, N
->getMemOperand());
410 // Legalize the chain result - switch anything that used the old chain to
412 ReplaceValueWith(SDValue(N
, 1), Res
.getValue(1));
416 SDValue
DAGTypeLegalizer::PromoteIntRes_AtomicCmpSwap(AtomicSDNode
*N
,
419 assert(N
->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS
);
420 EVT SVT
= getSetCCResultType(N
->getOperand(2).getValueType());
421 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(1));
423 // Only use the result of getSetCCResultType if it is legal,
424 // otherwise just use the promoted result type (NVT).
425 if (!TLI
.isTypeLegal(SVT
))
428 SDVTList VTs
= DAG
.getVTList(N
->getValueType(0), SVT
, MVT::Other
);
429 SDValue Res
= DAG
.getAtomicCmpSwap(
430 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS
, SDLoc(N
), N
->getMemoryVT(), VTs
,
431 N
->getChain(), N
->getBasePtr(), N
->getOperand(2), N
->getOperand(3),
433 ReplaceValueWith(SDValue(N
, 0), Res
.getValue(0));
434 ReplaceValueWith(SDValue(N
, 2), Res
.getValue(2));
435 return DAG
.getSExtOrTrunc(Res
.getValue(1), SDLoc(N
), NVT
);
438 // Op2 is used for the comparison and thus must be extended according to the
439 // target's atomic operations. Op3 is merely stored and so can be left alone.
440 SDValue Op2
= N
->getOperand(2);
441 SDValue Op3
= GetPromotedInteger(N
->getOperand(3));
442 switch (TLI
.getExtendForAtomicCmpSwapArg()) {
443 case ISD::SIGN_EXTEND
:
444 Op2
= SExtPromotedInteger(Op2
);
446 case ISD::ZERO_EXTEND
:
447 Op2
= ZExtPromotedInteger(Op2
);
449 case ISD::ANY_EXTEND
:
450 Op2
= GetPromotedInteger(Op2
);
453 llvm_unreachable("Invalid atomic op extension");
457 DAG
.getVTList(Op2
.getValueType(), N
->getValueType(1), MVT::Other
);
458 SDValue Res
= DAG
.getAtomicCmpSwap(
459 N
->getOpcode(), SDLoc(N
), N
->getMemoryVT(), VTs
, N
->getChain(),
460 N
->getBasePtr(), Op2
, Op3
, N
->getMemOperand());
461 // Update the use to N with the newly created Res.
462 for (unsigned i
= 1, NumResults
= N
->getNumValues(); i
< NumResults
; ++i
)
463 ReplaceValueWith(SDValue(N
, i
), Res
.getValue(i
));
467 SDValue
DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode
*N
) {
468 SDValue InOp
= N
->getOperand(0);
469 EVT InVT
= InOp
.getValueType();
470 EVT NInVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), InVT
);
471 EVT OutVT
= N
->getValueType(0);
472 EVT NOutVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OutVT
);
475 switch (getTypeAction(InVT
)) {
476 case TargetLowering::TypeLegal
:
478 case TargetLowering::TypePromoteInteger
:
479 if (NOutVT
.bitsEq(NInVT
) && !NOutVT
.isVector() && !NInVT
.isVector())
480 // The input promotes to the same size. Convert the promoted value.
481 return DAG
.getNode(ISD::BITCAST
, dl
, NOutVT
, GetPromotedInteger(InOp
));
483 case TargetLowering::TypeSoftenFloat
:
484 // Promote the integer operand by hand.
485 return DAG
.getNode(ISD::ANY_EXTEND
, dl
, NOutVT
, GetSoftenedFloat(InOp
));
486 case TargetLowering::TypeSoftPromoteHalf
:
487 // Promote the integer operand by hand.
488 return DAG
.getNode(ISD::ANY_EXTEND
, dl
, NOutVT
, GetSoftPromotedHalf(InOp
));
489 case TargetLowering::TypePromoteFloat
: {
490 // Convert the promoted float by hand.
491 if (!NOutVT
.isVector())
492 return DAG
.getNode(ISD::FP_TO_FP16
, dl
, NOutVT
, GetPromotedFloat(InOp
));
495 case TargetLowering::TypeExpandInteger
:
496 case TargetLowering::TypeExpandFloat
:
498 case TargetLowering::TypeScalarizeVector
:
499 // Convert the element to an integer and promote it by hand.
500 if (!NOutVT
.isVector())
501 return DAG
.getNode(ISD::ANY_EXTEND
, dl
, NOutVT
,
502 BitConvertToInteger(GetScalarizedVector(InOp
)));
504 case TargetLowering::TypeScalarizeScalableVector
:
505 report_fatal_error("Scalarization of scalable vectors is not supported.");
506 case TargetLowering::TypeSplitVector
: {
507 if (!NOutVT
.isVector()) {
508 // For example, i32 = BITCAST v2i16 on alpha. Convert the split
509 // pieces of the input into integers and reassemble in the final type.
511 GetSplitVector(N
->getOperand(0), Lo
, Hi
);
512 Lo
= BitConvertToInteger(Lo
);
513 Hi
= BitConvertToInteger(Hi
);
515 if (DAG
.getDataLayout().isBigEndian())
518 InOp
= DAG
.getNode(ISD::ANY_EXTEND
, dl
,
519 EVT::getIntegerVT(*DAG
.getContext(),
520 NOutVT
.getSizeInBits()),
521 JoinIntegers(Lo
, Hi
));
522 return DAG
.getNode(ISD::BITCAST
, dl
, NOutVT
, InOp
);
526 case TargetLowering::TypeWidenVector
:
527 // The input is widened to the same size. Convert to the widened value.
528 // Make sure that the outgoing value is not a vector, because this would
529 // make us bitcast between two vectors which are legalized in different ways.
530 if (NOutVT
.bitsEq(NInVT
) && !NOutVT
.isVector()) {
532 DAG
.getNode(ISD::BITCAST
, dl
, NOutVT
, GetWidenedVector(InOp
));
534 // For big endian targets we need to shift the casted value or the
535 // interesting bits will end up at the wrong place.
536 if (DAG
.getDataLayout().isBigEndian()) {
537 unsigned ShiftAmt
= NInVT
.getSizeInBits() - InVT
.getSizeInBits();
538 assert(ShiftAmt
< NOutVT
.getSizeInBits() && "Too large shift amount!");
539 Res
= DAG
.getNode(ISD::SRL
, dl
, NOutVT
, Res
,
540 DAG
.getShiftAmountConstant(ShiftAmt
, NOutVT
, dl
));
544 // If the output type is also a vector and widening it to the same size
545 // as the widened input type would be a legal type, we can widen the bitcast
546 // and handle the promotion after.
547 if (NOutVT
.isVector()) {
548 TypeSize WidenInSize
= NInVT
.getSizeInBits();
549 TypeSize OutSize
= OutVT
.getSizeInBits();
550 if (WidenInSize
.hasKnownScalarFactor(OutSize
)) {
551 unsigned Scale
= WidenInSize
.getKnownScalarFactor(OutSize
);
553 EVT::getVectorVT(*DAG
.getContext(), OutVT
.getVectorElementType(),
554 OutVT
.getVectorElementCount() * Scale
);
555 if (isTypeLegal(WideOutVT
)) {
556 InOp
= DAG
.getBitcast(WideOutVT
, GetWidenedVector(InOp
));
557 InOp
= DAG
.getNode(ISD::EXTRACT_SUBVECTOR
, dl
, OutVT
, InOp
,
558 DAG
.getVectorIdxConstant(0, dl
));
559 return DAG
.getNode(ISD::ANY_EXTEND
, dl
, NOutVT
, InOp
);
565 return DAG
.getNode(ISD::ANY_EXTEND
, dl
, NOutVT
,
566 CreateStackStoreLoad(InOp
, OutVT
));
569 SDValue
DAGTypeLegalizer::PromoteIntRes_FREEZE(SDNode
*N
) {
570 SDValue V
= GetPromotedInteger(N
->getOperand(0));
571 return DAG
.getNode(ISD::FREEZE
, SDLoc(N
),
572 V
.getValueType(), V
);
575 SDValue
DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode
*N
) {
576 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
577 EVT OVT
= N
->getValueType(0);
578 EVT NVT
= Op
.getValueType();
581 // If the larger BSWAP isn't supported by the target, try to expand now.
582 // If we expand later we'll end up with more operations since we lost the
583 // original type. We only do this for scalars since we have a shuffle
584 // based lowering for vectors in LegalizeVectorOps.
585 if (!OVT
.isVector() &&
586 !TLI
.isOperationLegalOrCustomOrPromote(ISD::BSWAP
, NVT
)) {
587 if (SDValue Res
= TLI
.expandBSWAP(N
, DAG
))
588 return DAG
.getNode(ISD::ANY_EXTEND
, dl
, NVT
, Res
);
591 unsigned DiffBits
= NVT
.getScalarSizeInBits() - OVT
.getScalarSizeInBits();
592 SDValue ShAmt
= DAG
.getShiftAmountConstant(DiffBits
, NVT
, dl
);
593 if (N
->getOpcode() == ISD::BSWAP
)
594 return DAG
.getNode(ISD::SRL
, dl
, NVT
, DAG
.getNode(ISD::BSWAP
, dl
, NVT
, Op
),
596 SDValue Mask
= N
->getOperand(1);
597 SDValue EVL
= N
->getOperand(2);
598 return DAG
.getNode(ISD::VP_SRL
, dl
, NVT
,
599 DAG
.getNode(ISD::VP_BSWAP
, dl
, NVT
, Op
, Mask
, EVL
), ShAmt
,
603 SDValue
DAGTypeLegalizer::PromoteIntRes_BITREVERSE(SDNode
*N
) {
604 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
605 EVT OVT
= N
->getValueType(0);
606 EVT NVT
= Op
.getValueType();
609 // If the larger BITREVERSE isn't supported by the target, try to expand now.
610 // If we expand later we'll end up with more operations since we lost the
611 // original type. We only do this for scalars since we have a shuffle
612 // based lowering for vectors in LegalizeVectorOps.
613 if (!OVT
.isVector() && OVT
.isSimple() &&
614 !TLI
.isOperationLegalOrCustomOrPromote(ISD::BITREVERSE
, NVT
)) {
615 if (SDValue Res
= TLI
.expandBITREVERSE(N
, DAG
))
616 return DAG
.getNode(ISD::ANY_EXTEND
, dl
, NVT
, Res
);
619 unsigned DiffBits
= NVT
.getScalarSizeInBits() - OVT
.getScalarSizeInBits();
620 SDValue ShAmt
= DAG
.getShiftAmountConstant(DiffBits
, NVT
, dl
);
621 if (N
->getOpcode() == ISD::BITREVERSE
)
622 return DAG
.getNode(ISD::SRL
, dl
, NVT
,
623 DAG
.getNode(ISD::BITREVERSE
, dl
, NVT
, Op
), ShAmt
);
624 SDValue Mask
= N
->getOperand(1);
625 SDValue EVL
= N
->getOperand(2);
626 return DAG
.getNode(ISD::VP_SRL
, dl
, NVT
,
627 DAG
.getNode(ISD::VP_BITREVERSE
, dl
, NVT
, Op
, Mask
, EVL
),
631 SDValue
DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode
*N
) {
632 // The pair element type may be legal, or may not promote to the same type as
633 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
634 return DAG
.getNode(ISD::ANY_EXTEND
, SDLoc(N
),
635 TLI
.getTypeToTransformTo(*DAG
.getContext(),
636 N
->getValueType(0)), JoinIntegers(N
->getOperand(0),
640 SDValue
DAGTypeLegalizer::PromoteIntRes_Constant(SDNode
*N
) {
641 EVT VT
= N
->getValueType(0);
642 // FIXME there is no actual debug info here
644 // Zero extend things like i1, sign extend everything else. It shouldn't
645 // matter in theory which one we pick, but this tends to give better code?
646 unsigned Opc
= VT
.isByteSized() ? ISD::SIGN_EXTEND
: ISD::ZERO_EXTEND
;
647 SDValue Result
= DAG
.getNode(Opc
, dl
,
648 TLI
.getTypeToTransformTo(*DAG
.getContext(), VT
),
650 assert(isa
<ConstantSDNode
>(Result
) && "Didn't constant fold ext?");
654 SDValue
DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode
*N
) {
655 EVT OVT
= N
->getValueType(0);
656 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OVT
);
659 // If the larger CTLZ isn't supported by the target, try to expand now.
660 // If we expand later we'll end up with more operations since we lost the
662 if (!OVT
.isVector() && TLI
.isTypeLegal(NVT
) &&
663 !TLI
.isOperationLegalOrCustomOrPromote(ISD::CTLZ
, NVT
) &&
664 !TLI
.isOperationLegalOrCustomOrPromote(ISD::CTLZ_ZERO_UNDEF
, NVT
)) {
665 if (SDValue Result
= TLI
.expandCTLZ(N
, DAG
)) {
666 Result
= DAG
.getNode(ISD::ANY_EXTEND
, dl
, NVT
, Result
);
671 unsigned CtlzOpcode
= N
->getOpcode();
672 if (CtlzOpcode
== ISD::CTLZ
|| CtlzOpcode
== ISD::VP_CTLZ
) {
673 // Subtract off the extra leading bits in the bigger type.
674 SDValue ExtractLeadingBits
= DAG
.getConstant(
675 NVT
.getScalarSizeInBits() - OVT
.getScalarSizeInBits(), dl
, NVT
);
677 if (!N
->isVPOpcode()) {
678 // Zero extend to the promoted type and do the count there.
679 SDValue Op
= ZExtPromotedInteger(N
->getOperand(0));
680 return DAG
.getNode(ISD::SUB
, dl
, NVT
,
681 DAG
.getNode(N
->getOpcode(), dl
, NVT
, Op
),
684 SDValue Mask
= N
->getOperand(1);
685 SDValue EVL
= N
->getOperand(2);
686 // Zero extend to the promoted type and do the count there.
687 SDValue Op
= VPZExtPromotedInteger(N
->getOperand(0), Mask
, EVL
);
688 return DAG
.getNode(ISD::VP_SUB
, dl
, NVT
,
689 DAG
.getNode(N
->getOpcode(), dl
, NVT
, Op
, Mask
, EVL
),
690 ExtractLeadingBits
, Mask
, EVL
);
692 if (CtlzOpcode
== ISD::CTLZ_ZERO_UNDEF
||
693 CtlzOpcode
== ISD::VP_CTLZ_ZERO_UNDEF
) {
694 // Any Extend the argument
695 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
696 // Op = Op << (sizeinbits(NVT) - sizeinbits(Old VT))
697 unsigned SHLAmount
= NVT
.getScalarSizeInBits() - OVT
.getScalarSizeInBits();
699 DAG
.getShiftAmountConstant(SHLAmount
, Op
.getValueType(), dl
);
700 if (!N
->isVPOpcode()) {
701 Op
= DAG
.getNode(ISD::SHL
, dl
, NVT
, Op
, ShiftConst
);
702 return DAG
.getNode(CtlzOpcode
, dl
, NVT
, Op
);
705 SDValue Mask
= N
->getOperand(1);
706 SDValue EVL
= N
->getOperand(2);
707 Op
= DAG
.getNode(ISD::VP_SHL
, dl
, NVT
, Op
, ShiftConst
, Mask
, EVL
);
708 return DAG
.getNode(CtlzOpcode
, dl
, NVT
, Op
, Mask
, EVL
);
710 llvm_unreachable("Invalid CTLZ Opcode");
713 SDValue
DAGTypeLegalizer::PromoteIntRes_CTPOP_PARITY(SDNode
*N
) {
714 EVT OVT
= N
->getValueType(0);
715 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OVT
);
717 // If the larger CTPOP isn't supported by the target, try to expand now.
718 // If we expand later we'll end up with more operations since we lost the
720 // TODO: Expand ISD::PARITY. Need to move ExpandPARITY from LegalizeDAG to
722 if (N
->getOpcode() == ISD::CTPOP
&& !OVT
.isVector() && TLI
.isTypeLegal(NVT
) &&
723 !TLI
.isOperationLegalOrCustomOrPromote(ISD::CTPOP
, NVT
)) {
724 if (SDValue Result
= TLI
.expandCTPOP(N
, DAG
)) {
725 Result
= DAG
.getNode(ISD::ANY_EXTEND
, SDLoc(N
), NVT
, Result
);
730 // Zero extend to the promoted type and do the count or parity there.
731 if (!N
->isVPOpcode()) {
732 SDValue Op
= ZExtPromotedInteger(N
->getOperand(0));
733 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), Op
.getValueType(), Op
);
736 SDValue Mask
= N
->getOperand(1);
737 SDValue EVL
= N
->getOperand(2);
738 SDValue Op
= VPZExtPromotedInteger(N
->getOperand(0), Mask
, EVL
);
739 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), Op
.getValueType(), Op
, Mask
,
743 SDValue
DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode
*N
) {
744 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
745 EVT OVT
= N
->getValueType(0);
746 EVT NVT
= Op
.getValueType();
749 // If the larger CTTZ isn't supported by the target, try to expand now.
750 // If we expand later we'll end up with more operations since we lost the
751 // original type. Don't expand if we can use CTPOP or CTLZ expansion on the
753 if (!OVT
.isVector() && TLI
.isTypeLegal(NVT
) &&
754 !TLI
.isOperationLegalOrCustomOrPromote(ISD::CTTZ
, NVT
) &&
755 !TLI
.isOperationLegalOrCustomOrPromote(ISD::CTTZ_ZERO_UNDEF
, NVT
) &&
756 !TLI
.isOperationLegal(ISD::CTPOP
, NVT
) &&
757 !TLI
.isOperationLegal(ISD::CTLZ
, NVT
)) {
758 if (SDValue Result
= TLI
.expandCTTZ(N
, DAG
)) {
759 Result
= DAG
.getNode(ISD::ANY_EXTEND
, dl
, NVT
, Result
);
764 unsigned NewOpc
= N
->getOpcode();
765 if (NewOpc
== ISD::CTTZ
|| NewOpc
== ISD::VP_CTTZ
) {
766 // The count is the same in the promoted type except if the original
767 // value was zero. This can be handled by setting the bit just off
768 // the top of the original type.
769 auto TopBit
= APInt::getOneBitSet(NVT
.getScalarSizeInBits(),
770 OVT
.getScalarSizeInBits());
771 if (NewOpc
== ISD::CTTZ
) {
772 Op
= DAG
.getNode(ISD::OR
, dl
, NVT
, Op
, DAG
.getConstant(TopBit
, dl
, NVT
));
773 NewOpc
= ISD::CTTZ_ZERO_UNDEF
;
776 DAG
.getNode(ISD::VP_OR
, dl
, NVT
, Op
, DAG
.getConstant(TopBit
, dl
, NVT
),
777 N
->getOperand(1), N
->getOperand(2));
778 NewOpc
= ISD::VP_CTTZ_ZERO_UNDEF
;
781 if (!N
->isVPOpcode())
782 return DAG
.getNode(NewOpc
, dl
, NVT
, Op
);
783 return DAG
.getNode(NewOpc
, dl
, NVT
, Op
, N
->getOperand(1), N
->getOperand(2));
786 SDValue
DAGTypeLegalizer::PromoteIntRes_VP_CttzElements(SDNode
*N
) {
788 EVT NewVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
789 return DAG
.getNode(N
->getOpcode(), DL
, NewVT
, N
->ops());
792 SDValue
DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode
*N
) {
794 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
796 SDValue Op0
= N
->getOperand(0);
797 SDValue Op1
= N
->getOperand(1);
799 // If the input also needs to be promoted, do that first so we can get a
800 // get a good idea for the output type.
801 if (TLI
.getTypeAction(*DAG
.getContext(), Op0
.getValueType())
802 == TargetLowering::TypePromoteInteger
) {
803 SDValue In
= GetPromotedInteger(Op0
);
805 // If the new type is larger than NVT, use it. We probably won't need to
807 EVT SVT
= In
.getValueType().getScalarType();
808 if (SVT
.bitsGE(NVT
)) {
809 SDValue Ext
= DAG
.getNode(ISD::EXTRACT_VECTOR_ELT
, dl
, SVT
, In
, Op1
);
810 return DAG
.getAnyExtOrTrunc(Ext
, dl
, NVT
);
814 return DAG
.getNode(ISD::EXTRACT_VECTOR_ELT
, dl
, NVT
, Op0
, Op1
);
817 SDValue
DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode
*N
) {
818 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
819 unsigned NewOpc
= N
->getOpcode();
822 // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
823 // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT
824 // and SINT conversions are Custom, there is no way to tell which is
825 // preferable. We choose SINT because that's the right thing on PPC.)
826 if (N
->getOpcode() == ISD::FP_TO_UINT
&&
827 !TLI
.isOperationLegal(ISD::FP_TO_UINT
, NVT
) &&
828 TLI
.isOperationLegalOrCustom(ISD::FP_TO_SINT
, NVT
))
829 NewOpc
= ISD::FP_TO_SINT
;
831 if (N
->getOpcode() == ISD::STRICT_FP_TO_UINT
&&
832 !TLI
.isOperationLegal(ISD::STRICT_FP_TO_UINT
, NVT
) &&
833 TLI
.isOperationLegalOrCustom(ISD::STRICT_FP_TO_SINT
, NVT
))
834 NewOpc
= ISD::STRICT_FP_TO_SINT
;
836 if (N
->getOpcode() == ISD::VP_FP_TO_UINT
&&
837 !TLI
.isOperationLegal(ISD::VP_FP_TO_UINT
, NVT
) &&
838 TLI
.isOperationLegalOrCustom(ISD::VP_FP_TO_SINT
, NVT
))
839 NewOpc
= ISD::VP_FP_TO_SINT
;
842 if (N
->isStrictFPOpcode()) {
843 Res
= DAG
.getNode(NewOpc
, dl
, {NVT
, MVT::Other
},
844 {N
->getOperand(0), N
->getOperand(1)});
845 // Legalize the chain result - switch anything that used the old chain to
847 ReplaceValueWith(SDValue(N
, 1), Res
.getValue(1));
848 } else if (NewOpc
== ISD::VP_FP_TO_SINT
|| NewOpc
== ISD::VP_FP_TO_UINT
) {
849 Res
= DAG
.getNode(NewOpc
, dl
, NVT
, {N
->getOperand(0), N
->getOperand(1),
852 Res
= DAG
.getNode(NewOpc
, dl
, NVT
, N
->getOperand(0));
855 // Assert that the converted value fits in the original type. If it doesn't
856 // (eg: because the value being converted is too big), then the result of the
857 // original operation was undefined anyway, so the assert is still correct.
859 // NOTE: fp-to-uint to fp-to-sint promotion guarantees zero extend. For example:
860 // before legalization: fp-to-uint16, 65534. -> 0xfffe
861 // after legalization: fp-to-sint32, 65534. -> 0x0000fffe
862 return DAG
.getNode((N
->getOpcode() == ISD::FP_TO_UINT
||
863 N
->getOpcode() == ISD::STRICT_FP_TO_UINT
||
864 N
->getOpcode() == ISD::VP_FP_TO_UINT
)
868 DAG
.getValueType(N
->getValueType(0).getScalarType()));
871 SDValue
DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT_SAT(SDNode
*N
) {
872 // Promote the result type, while keeping the original width in Op1.
873 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
875 return DAG
.getNode(N
->getOpcode(), dl
, NVT
, N
->getOperand(0),
879 SDValue
DAGTypeLegalizer::PromoteIntRes_FP_TO_FP16_BF16(SDNode
*N
) {
880 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
883 return DAG
.getNode(N
->getOpcode(), dl
, NVT
, N
->getOperand(0));
886 SDValue
DAGTypeLegalizer::PromoteIntRes_STRICT_FP_TO_FP16_BF16(SDNode
*N
) {
887 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
890 SDValue Res
= DAG
.getNode(N
->getOpcode(), dl
, DAG
.getVTList(NVT
, MVT::Other
),
891 N
->getOperand(0), N
->getOperand(1));
892 ReplaceValueWith(SDValue(N
, 1), Res
.getValue(1));
896 SDValue
DAGTypeLegalizer::PromoteIntRes_XRINT(SDNode
*N
) {
897 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
899 return DAG
.getNode(N
->getOpcode(), dl
, NVT
, N
->getOperand(0));
902 SDValue
DAGTypeLegalizer::PromoteIntRes_GET_ROUNDING(SDNode
*N
) {
903 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
907 DAG
.getNode(N
->getOpcode(), dl
, {NVT
, MVT::Other
}, N
->getOperand(0));
909 // Legalize the chain result - switch anything that used the old chain to
911 ReplaceValueWith(SDValue(N
, 1), Res
.getValue(1));
915 SDValue
DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode
*N
) {
916 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
919 if (getTypeAction(N
->getOperand(0).getValueType())
920 == TargetLowering::TypePromoteInteger
) {
921 SDValue Res
= GetPromotedInteger(N
->getOperand(0));
922 assert(Res
.getValueType().bitsLE(NVT
) && "Extension doesn't make sense!");
924 // If the result and operand types are the same after promotion, simplify
925 // to an in-register extension. Unless this is a VP_*_EXTEND.
926 if (NVT
== Res
.getValueType() && N
->getNumOperands() == 1) {
927 // The high bits are not guaranteed to be anything. Insert an extend.
928 if (N
->getOpcode() == ISD::SIGN_EXTEND
)
929 return DAG
.getNode(ISD::SIGN_EXTEND_INREG
, dl
, NVT
, Res
,
930 DAG
.getValueType(N
->getOperand(0).getValueType()));
931 if (N
->getOpcode() == ISD::ZERO_EXTEND
)
932 return DAG
.getZeroExtendInReg(Res
, dl
, N
->getOperand(0).getValueType());
933 assert(N
->getOpcode() == ISD::ANY_EXTEND
&& "Unknown integer extension!");
938 // Otherwise, just extend the original operand all the way to the larger type.
939 if (N
->getNumOperands() != 1) {
940 assert(N
->getNumOperands() == 3 && "Unexpected number of operands!");
941 assert(N
->isVPOpcode() && "Expected VP opcode");
942 return DAG
.getNode(N
->getOpcode(), dl
, NVT
, N
->getOperand(0),
943 N
->getOperand(1), N
->getOperand(2));
945 return DAG
.getNode(N
->getOpcode(), dl
, NVT
, N
->getOperand(0));
948 SDValue
DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode
*N
) {
949 assert(ISD::isUNINDEXEDLoad(N
) && "Indexed load during type legalization!");
950 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
951 ISD::LoadExtType ExtType
=
952 ISD::isNON_EXTLoad(N
) ? ISD::EXTLOAD
: N
->getExtensionType();
954 SDValue Res
= DAG
.getExtLoad(ExtType
, dl
, NVT
, N
->getChain(), N
->getBasePtr(),
955 N
->getMemoryVT(), N
->getMemOperand());
957 // Legalize the chain result - switch anything that used the old chain to
959 ReplaceValueWith(SDValue(N
, 1), Res
.getValue(1));
963 SDValue
DAGTypeLegalizer::PromoteIntRes_VP_LOAD(VPLoadSDNode
*N
) {
964 assert(!N
->isIndexed() && "Indexed vp_load during type legalization!");
965 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
966 ISD::LoadExtType ExtType
= (N
->getExtensionType() == ISD::NON_EXTLOAD
)
968 : N
->getExtensionType();
971 DAG
.getLoadVP(N
->getAddressingMode(), ExtType
, NVT
, dl
, N
->getChain(),
972 N
->getBasePtr(), N
->getOffset(), N
->getMask(),
973 N
->getVectorLength(), N
->getMemoryVT(), N
->getMemOperand());
974 // Legalize the chain result - switch anything that used the old chain to
976 ReplaceValueWith(SDValue(N
, 1), Res
.getValue(1));
980 SDValue
DAGTypeLegalizer::PromoteIntRes_MLOAD(MaskedLoadSDNode
*N
) {
981 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
982 SDValue ExtPassThru
= GetPromotedInteger(N
->getPassThru());
984 ISD::LoadExtType ExtType
= N
->getExtensionType();
985 if (ExtType
== ISD::NON_EXTLOAD
)
986 ExtType
= ISD::EXTLOAD
;
989 SDValue Res
= DAG
.getMaskedLoad(NVT
, dl
, N
->getChain(), N
->getBasePtr(),
990 N
->getOffset(), N
->getMask(), ExtPassThru
,
991 N
->getMemoryVT(), N
->getMemOperand(),
992 N
->getAddressingMode(), ExtType
,
993 N
->isExpandingLoad());
994 // Legalize the chain result - switch anything that used the old chain to
996 ReplaceValueWith(SDValue(N
, 1), Res
.getValue(1));
1000 SDValue
DAGTypeLegalizer::PromoteIntRes_MGATHER(MaskedGatherSDNode
*N
) {
1001 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
1002 SDValue ExtPassThru
= GetPromotedInteger(N
->getPassThru());
1003 assert(NVT
== ExtPassThru
.getValueType() &&
1004 "Gather result type and the passThru argument type should be the same");
1006 ISD::LoadExtType ExtType
= N
->getExtensionType();
1007 if (ExtType
== ISD::NON_EXTLOAD
)
1008 ExtType
= ISD::EXTLOAD
;
1011 SDValue Ops
[] = {N
->getChain(), ExtPassThru
, N
->getMask(), N
->getBasePtr(),
1012 N
->getIndex(), N
->getScale() };
1013 SDValue Res
= DAG
.getMaskedGather(DAG
.getVTList(NVT
, MVT::Other
),
1014 N
->getMemoryVT(), dl
, Ops
,
1015 N
->getMemOperand(), N
->getIndexType(),
1017 // Legalize the chain result - switch anything that used the old chain to
1019 ReplaceValueWith(SDValue(N
, 1), Res
.getValue(1));
1023 SDValue
DAGTypeLegalizer::PromoteIntRes_VECTOR_COMPRESS(SDNode
*N
) {
1024 SDValue Vec
= GetPromotedInteger(N
->getOperand(0));
1025 SDValue Passthru
= GetPromotedInteger(N
->getOperand(2));
1026 return DAG
.getNode(ISD::VECTOR_COMPRESS
, SDLoc(N
), Vec
.getValueType(), Vec
,
1027 N
->getOperand(1), Passthru
);
1030 /// Promote the overflow flag of an overflowing arithmetic node.
1031 SDValue
DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode
*N
) {
1032 // Change the return type of the boolean result while obeying
1033 // getSetCCResultType.
1034 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(1));
1035 EVT VT
= N
->getValueType(0);
1036 EVT SVT
= getSetCCResultType(VT
);
1037 SDValue Ops
[3] = { N
->getOperand(0), N
->getOperand(1) };
1038 unsigned NumOps
= N
->getNumOperands();
1039 assert(NumOps
<= 3 && "Too many operands");
1041 Ops
[2] = PromoteTargetBoolean(N
->getOperand(2), VT
);
1044 SDValue Res
= DAG
.getNode(N
->getOpcode(), dl
, DAG
.getVTList(VT
, SVT
),
1045 ArrayRef(Ops
, NumOps
));
1047 // Modified the sum result - switch anything that used the old sum to use
1049 ReplaceValueWith(SDValue(N
, 0), Res
);
1051 // Convert to the expected type.
1052 return DAG
.getBoolExtOrTrunc(Res
.getValue(1), dl
, NVT
, VT
);
1055 template <class MatchContextClass
>
1056 SDValue
DAGTypeLegalizer::PromoteIntRes_ADDSUBSHLSAT(SDNode
*N
) {
1057 // If the promoted type is legal, we can convert this to:
1058 // 1. ANY_EXTEND iN to iM
1060 // 3. [US][ADD|SUB|SHL]SAT
1062 // Else it is more efficient to convert this to a min and a max
1063 // operation in the higher precision arithmetic.
1065 SDValue Op1
= N
->getOperand(0);
1066 SDValue Op2
= N
->getOperand(1);
1067 MatchContextClass
matcher(DAG
, TLI
, N
);
1069 unsigned Opcode
= matcher
.getRootBaseOpcode();
1070 unsigned OldBits
= Op1
.getScalarValueSizeInBits();
1072 // USUBSAT can always be promoted as long as we have zero/sign-extended the
1074 if (Opcode
== ISD::USUBSAT
) {
1075 SExtOrZExtPromotedOperands(Op1
, Op2
);
1076 return matcher
.getNode(ISD::USUBSAT
, dl
, Op1
.getValueType(), Op1
, Op2
);
1079 if (Opcode
== ISD::UADDSAT
) {
1080 EVT OVT
= Op1
.getValueType();
1081 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OVT
);
1082 // We can promote if we use sign-extend. Do this if the target prefers.
1083 if (TLI
.isSExtCheaperThanZExt(OVT
, NVT
)) {
1084 Op1
= SExtPromotedInteger(Op1
);
1085 Op2
= SExtPromotedInteger(Op2
);
1086 return matcher
.getNode(ISD::UADDSAT
, dl
, NVT
, Op1
, Op2
);
1089 Op1
= ZExtPromotedInteger(Op1
);
1090 Op2
= ZExtPromotedInteger(Op2
);
1091 unsigned NewBits
= NVT
.getScalarSizeInBits();
1092 APInt MaxVal
= APInt::getLowBitsSet(NewBits
, OldBits
);
1093 SDValue SatMax
= DAG
.getConstant(MaxVal
, dl
, NVT
);
1094 SDValue Add
= matcher
.getNode(ISD::ADD
, dl
, NVT
, Op1
, Op2
);
1095 return matcher
.getNode(ISD::UMIN
, dl
, NVT
, Add
, SatMax
);
1098 bool IsShift
= Opcode
== ISD::USHLSAT
|| Opcode
== ISD::SSHLSAT
;
1100 // FIXME: We need vp-aware PromotedInteger functions.
1102 Op1
= GetPromotedInteger(Op1
);
1103 Op2
= ZExtPromotedInteger(Op2
);
1105 Op1
= SExtPromotedInteger(Op1
);
1106 Op2
= SExtPromotedInteger(Op2
);
1108 EVT PromotedType
= Op1
.getValueType();
1109 unsigned NewBits
= PromotedType
.getScalarSizeInBits();
1111 // Shift cannot use a min/max expansion, we can't detect overflow if all of
1112 // the bits have been shifted out.
1113 if (IsShift
|| matcher
.isOperationLegal(Opcode
, PromotedType
)) {
1125 llvm_unreachable("Expected opcode to be signed or unsigned saturation "
1126 "addition, subtraction or left shift");
1129 unsigned SHLAmount
= NewBits
- OldBits
;
1130 SDValue ShiftAmount
=
1131 DAG
.getShiftAmountConstant(SHLAmount
, PromotedType
, dl
);
1132 Op1
= DAG
.getNode(ISD::SHL
, dl
, PromotedType
, Op1
, ShiftAmount
);
1134 Op2
= matcher
.getNode(ISD::SHL
, dl
, PromotedType
, Op2
, ShiftAmount
);
1136 SDValue Result
= matcher
.getNode(Opcode
, dl
, PromotedType
, Op1
, Op2
);
1137 return matcher
.getNode(ShiftOp
, dl
, PromotedType
, Result
, ShiftAmount
);
1140 unsigned AddOp
= Opcode
== ISD::SADDSAT
? ISD::ADD
: ISD::SUB
;
1141 APInt MinVal
= APInt::getSignedMinValue(OldBits
).sext(NewBits
);
1142 APInt MaxVal
= APInt::getSignedMaxValue(OldBits
).sext(NewBits
);
1143 SDValue SatMin
= DAG
.getConstant(MinVal
, dl
, PromotedType
);
1144 SDValue SatMax
= DAG
.getConstant(MaxVal
, dl
, PromotedType
);
1145 SDValue Result
= matcher
.getNode(AddOp
, dl
, PromotedType
, Op1
, Op2
);
1146 Result
= matcher
.getNode(ISD::SMIN
, dl
, PromotedType
, Result
, SatMax
);
1147 Result
= matcher
.getNode(ISD::SMAX
, dl
, PromotedType
, Result
, SatMin
);
1151 SDValue
DAGTypeLegalizer::PromoteIntRes_MULFIX(SDNode
*N
) {
1152 // Can just promote the operands then continue with operation.
1154 SDValue Op1Promoted
, Op2Promoted
;
1156 N
->getOpcode() == ISD::SMULFIX
|| N
->getOpcode() == ISD::SMULFIXSAT
;
1158 N
->getOpcode() == ISD::SMULFIXSAT
|| N
->getOpcode() == ISD::UMULFIXSAT
;
1160 Op1Promoted
= SExtPromotedInteger(N
->getOperand(0));
1161 Op2Promoted
= SExtPromotedInteger(N
->getOperand(1));
1163 Op1Promoted
= ZExtPromotedInteger(N
->getOperand(0));
1164 Op2Promoted
= ZExtPromotedInteger(N
->getOperand(1));
1166 EVT OldType
= N
->getOperand(0).getValueType();
1167 EVT PromotedType
= Op1Promoted
.getValueType();
1169 PromotedType
.getScalarSizeInBits() - OldType
.getScalarSizeInBits();
1172 // Promoting the operand and result values changes the saturation width,
1173 // which is extends the values that we clamp to on saturation. This could be
1174 // resolved by shifting one of the operands the same amount, which would
1175 // also shift the result we compare against, then shifting back.
1177 DAG
.getNode(ISD::SHL
, dl
, PromotedType
, Op1Promoted
,
1178 DAG
.getShiftAmountConstant(DiffSize
, PromotedType
, dl
));
1179 SDValue Result
= DAG
.getNode(N
->getOpcode(), dl
, PromotedType
, Op1Promoted
,
1180 Op2Promoted
, N
->getOperand(2));
1181 unsigned ShiftOp
= Signed
? ISD::SRA
: ISD::SRL
;
1182 return DAG
.getNode(ShiftOp
, dl
, PromotedType
, Result
,
1183 DAG
.getShiftAmountConstant(DiffSize
, PromotedType
, dl
));
1185 return DAG
.getNode(N
->getOpcode(), dl
, PromotedType
, Op1Promoted
, Op2Promoted
,
1189 static SDValue
SaturateWidenedDIVFIX(SDValue V
, SDLoc
&dl
,
1190 unsigned SatW
, bool Signed
,
1191 const TargetLowering
&TLI
,
1192 SelectionDAG
&DAG
) {
1193 EVT VT
= V
.getValueType();
1194 unsigned VTW
= VT
.getScalarSizeInBits();
1197 // Saturate to the unsigned maximum by getting the minimum of V and the
1199 return DAG
.getNode(ISD::UMIN
, dl
, VT
, V
,
1200 DAG
.getConstant(APInt::getLowBitsSet(VTW
, SatW
),
1204 // Saturate to the signed maximum (the low SatW - 1 bits) by taking the
1205 // signed minimum of it and V.
1206 V
= DAG
.getNode(ISD::SMIN
, dl
, VT
, V
,
1207 DAG
.getConstant(APInt::getLowBitsSet(VTW
, SatW
- 1),
1209 // Saturate to the signed minimum (the high SatW + 1 bits) by taking the
1210 // signed maximum of it and V.
1211 V
= DAG
.getNode(ISD::SMAX
, dl
, VT
, V
,
1212 DAG
.getConstant(APInt::getHighBitsSet(VTW
, VTW
- SatW
+ 1),
1217 static SDValue
earlyExpandDIVFIX(SDNode
*N
, SDValue LHS
, SDValue RHS
,
1218 unsigned Scale
, const TargetLowering
&TLI
,
1219 SelectionDAG
&DAG
, unsigned SatW
= 0) {
1220 EVT VT
= LHS
.getValueType();
1221 unsigned VTSize
= VT
.getScalarSizeInBits();
1222 bool Signed
= N
->getOpcode() == ISD::SDIVFIX
||
1223 N
->getOpcode() == ISD::SDIVFIXSAT
;
1224 bool Saturating
= N
->getOpcode() == ISD::SDIVFIXSAT
||
1225 N
->getOpcode() == ISD::UDIVFIXSAT
;
1228 // Widen the types by a factor of two. This is guaranteed to expand, since it
1229 // will always have enough high bits in the LHS to shift into.
1230 EVT WideVT
= EVT::getIntegerVT(*DAG
.getContext(), VTSize
* 2);
1232 WideVT
= EVT::getVectorVT(*DAG
.getContext(), WideVT
,
1233 VT
.getVectorElementCount());
1234 LHS
= DAG
.getExtOrTrunc(Signed
, LHS
, dl
, WideVT
);
1235 RHS
= DAG
.getExtOrTrunc(Signed
, RHS
, dl
, WideVT
);
1236 SDValue Res
= TLI
.expandFixedPointDiv(N
->getOpcode(), dl
, LHS
, RHS
, Scale
,
1238 assert(Res
&& "Expanding DIVFIX with wide type failed?");
1240 // If the caller has told us to saturate at something less, use that width
1241 // instead of the type before doubling. However, it cannot be more than
1242 // what we just widened!
1243 assert(SatW
<= VTSize
&&
1244 "Tried to saturate to more than the original type?");
1245 Res
= SaturateWidenedDIVFIX(Res
, dl
, SatW
== 0 ? VTSize
: SatW
, Signed
,
1248 return DAG
.getZExtOrTrunc(Res
, dl
, VT
);
1251 SDValue
DAGTypeLegalizer::PromoteIntRes_DIVFIX(SDNode
*N
) {
1253 SDValue Op1Promoted
, Op2Promoted
;
1254 bool Signed
= N
->getOpcode() == ISD::SDIVFIX
||
1255 N
->getOpcode() == ISD::SDIVFIXSAT
;
1256 bool Saturating
= N
->getOpcode() == ISD::SDIVFIXSAT
||
1257 N
->getOpcode() == ISD::UDIVFIXSAT
;
1259 Op1Promoted
= SExtPromotedInteger(N
->getOperand(0));
1260 Op2Promoted
= SExtPromotedInteger(N
->getOperand(1));
1262 Op1Promoted
= ZExtPromotedInteger(N
->getOperand(0));
1263 Op2Promoted
= ZExtPromotedInteger(N
->getOperand(1));
1265 EVT PromotedType
= Op1Promoted
.getValueType();
1266 unsigned Scale
= N
->getConstantOperandVal(2);
1268 // If the type is already legal and the operation is legal in that type, we
1269 // should not early expand.
1270 if (TLI
.isTypeLegal(PromotedType
)) {
1271 TargetLowering::LegalizeAction Action
=
1272 TLI
.getFixedPointOperationAction(N
->getOpcode(), PromotedType
, Scale
);
1273 if (Action
== TargetLowering::Legal
|| Action
== TargetLowering::Custom
) {
1274 unsigned Diff
= PromotedType
.getScalarSizeInBits() -
1275 N
->getValueType(0).getScalarSizeInBits();
1278 DAG
.getNode(ISD::SHL
, dl
, PromotedType
, Op1Promoted
,
1279 DAG
.getShiftAmountConstant(Diff
, PromotedType
, dl
));
1280 SDValue Res
= DAG
.getNode(N
->getOpcode(), dl
, PromotedType
, Op1Promoted
,
1281 Op2Promoted
, N
->getOperand(2));
1283 Res
= DAG
.getNode(Signed
? ISD::SRA
: ISD::SRL
, dl
, PromotedType
, Res
,
1284 DAG
.getShiftAmountConstant(Diff
, PromotedType
, dl
));
1289 // See if we can perform the division in this type without expanding.
1290 if (SDValue Res
= TLI
.expandFixedPointDiv(N
->getOpcode(), dl
, Op1Promoted
,
1291 Op2Promoted
, Scale
, DAG
)) {
1293 Res
= SaturateWidenedDIVFIX(Res
, dl
,
1294 N
->getValueType(0).getScalarSizeInBits(),
1298 // If we cannot, expand it to twice the type width. If we are saturating, give
1299 // it the original width as a saturating width so we don't need to emit
1301 return earlyExpandDIVFIX(N
, Op1Promoted
, Op2Promoted
, Scale
, TLI
, DAG
,
1302 N
->getValueType(0).getScalarSizeInBits());
1305 SDValue
DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode
*N
, unsigned ResNo
) {
1307 return PromoteIntRes_Overflow(N
);
1309 // The operation overflowed iff the result in the larger type is not the
1310 // sign extension of its truncation to the original type.
1311 SDValue LHS
= SExtPromotedInteger(N
->getOperand(0));
1312 SDValue RHS
= SExtPromotedInteger(N
->getOperand(1));
1313 EVT OVT
= N
->getOperand(0).getValueType();
1314 EVT NVT
= LHS
.getValueType();
1317 // Do the arithmetic in the larger type.
1318 unsigned Opcode
= N
->getOpcode() == ISD::SADDO
? ISD::ADD
: ISD::SUB
;
1319 SDValue Res
= DAG
.getNode(Opcode
, dl
, NVT
, LHS
, RHS
);
1321 // Calculate the overflow flag: sign extend the arithmetic result from
1322 // the original type.
1323 SDValue Ofl
= DAG
.getNode(ISD::SIGN_EXTEND_INREG
, dl
, NVT
, Res
,
1324 DAG
.getValueType(OVT
));
1325 // Overflowed if and only if this is not equal to Res.
1326 Ofl
= DAG
.getSetCC(dl
, N
->getValueType(1), Ofl
, Res
, ISD::SETNE
);
1328 // Use the calculated overflow everywhere.
1329 ReplaceValueWith(SDValue(N
, 1), Ofl
);
1334 SDValue
DAGTypeLegalizer::PromoteIntRes_CMP(SDNode
*N
) {
1335 EVT PromotedResultTy
=
1336 TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
1337 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), PromotedResultTy
,
1338 N
->getOperand(0), N
->getOperand(1));
1341 SDValue
DAGTypeLegalizer::PromoteIntRes_Select(SDNode
*N
) {
1342 SDValue Mask
= N
->getOperand(0);
1344 SDValue LHS
= GetPromotedInteger(N
->getOperand(1));
1345 SDValue RHS
= GetPromotedInteger(N
->getOperand(2));
1347 unsigned Opcode
= N
->getOpcode();
1348 if (Opcode
== ISD::VP_SELECT
|| Opcode
== ISD::VP_MERGE
)
1349 return DAG
.getNode(Opcode
, SDLoc(N
), LHS
.getValueType(), Mask
, LHS
, RHS
,
1351 return DAG
.getNode(Opcode
, SDLoc(N
), LHS
.getValueType(), Mask
, LHS
, RHS
);
1354 SDValue
DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode
*N
) {
1355 SDValue LHS
= GetPromotedInteger(N
->getOperand(2));
1356 SDValue RHS
= GetPromotedInteger(N
->getOperand(3));
1357 return DAG
.getNode(ISD::SELECT_CC
, SDLoc(N
),
1358 LHS
.getValueType(), N
->getOperand(0),
1359 N
->getOperand(1), LHS
, RHS
, N
->getOperand(4));
1362 SDValue
DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode
*N
) {
1363 unsigned OpNo
= N
->isStrictFPOpcode() ? 1 : 0;
1364 EVT InVT
= N
->getOperand(OpNo
).getValueType();
1365 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
1367 EVT SVT
= getSetCCResultType(InVT
);
1369 // If we got back a type that needs to be promoted, this likely means the
1370 // the input type also needs to be promoted. So get the promoted type for
1371 // the input and try the query again.
1372 if (getTypeAction(SVT
) == TargetLowering::TypePromoteInteger
) {
1373 if (getTypeAction(InVT
) == TargetLowering::TypePromoteInteger
) {
1374 InVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), InVT
);
1375 SVT
= getSetCCResultType(InVT
);
1377 // Input type isn't promoted, just use the default promoted type.
1383 assert(SVT
.isVector() == N
->getOperand(OpNo
).getValueType().isVector() &&
1384 "Vector compare must return a vector result!");
1386 // Get the SETCC result using the canonical SETCC type.
1388 if (N
->isStrictFPOpcode()) {
1389 SDVTList VTs
= DAG
.getVTList({SVT
, MVT::Other
});
1390 SDValue Opers
[] = {N
->getOperand(0), N
->getOperand(1),
1391 N
->getOperand(2), N
->getOperand(3)};
1392 SetCC
= DAG
.getNode(N
->getOpcode(), dl
, VTs
, Opers
, N
->getFlags());
1393 // Legalize the chain result - switch anything that used the old chain to
1395 ReplaceValueWith(SDValue(N
, 1), SetCC
.getValue(1));
1397 SetCC
= DAG
.getNode(N
->getOpcode(), dl
, SVT
, N
->getOperand(0),
1398 N
->getOperand(1), N
->getOperand(2), N
->getFlags());
1400 // Convert to the expected type.
1401 return DAG
.getSExtOrTrunc(SetCC
, dl
, NVT
);
1404 SDValue
DAGTypeLegalizer::PromoteIntRes_IS_FPCLASS(SDNode
*N
) {
1406 SDValue Arg
= N
->getOperand(0);
1407 SDValue Test
= N
->getOperand(1);
1408 EVT NResVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
1409 return DAG
.getNode(ISD::IS_FPCLASS
, DL
, NResVT
, Arg
, Test
);
1412 SDValue
DAGTypeLegalizer::PromoteIntRes_FFREXP(SDNode
*N
) {
1413 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(1));
1414 EVT VT
= N
->getValueType(0);
1418 DAG
.getNode(N
->getOpcode(), dl
, DAG
.getVTList(VT
, NVT
), N
->getOperand(0));
1420 ReplaceValueWith(SDValue(N
, 0), Res
);
1421 return Res
.getValue(1);
1424 SDValue
DAGTypeLegalizer::PromoteIntRes_SHL(SDNode
*N
) {
1425 SDValue LHS
= GetPromotedInteger(N
->getOperand(0));
1426 SDValue RHS
= N
->getOperand(1);
1427 if (N
->getOpcode() != ISD::VP_SHL
) {
1428 if (getTypeAction(RHS
.getValueType()) == TargetLowering::TypePromoteInteger
)
1429 RHS
= ZExtPromotedInteger(RHS
);
1431 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
);
1434 SDValue Mask
= N
->getOperand(2);
1435 SDValue EVL
= N
->getOperand(3);
1436 if (getTypeAction(RHS
.getValueType()) == TargetLowering::TypePromoteInteger
)
1437 RHS
= VPZExtPromotedInteger(RHS
, Mask
, EVL
);
1438 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
,
1442 SDValue
DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode
*N
) {
1443 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
1444 return DAG
.getNode(ISD::SIGN_EXTEND_INREG
, SDLoc(N
),
1445 Op
.getValueType(), Op
, N
->getOperand(1));
1448 SDValue
DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode
*N
) {
1449 // The input may have strange things in the top bits of the registers, but
1450 // these operations don't care. They may have weird bits going out, but
1451 // that too is okay if they are integer operations.
1452 SDValue LHS
= GetPromotedInteger(N
->getOperand(0));
1453 SDValue RHS
= GetPromotedInteger(N
->getOperand(1));
1454 if (N
->getNumOperands() == 2)
1455 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
);
1456 assert(N
->getNumOperands() == 4 && "Unexpected number of operands!");
1457 assert(N
->isVPOpcode() && "Expected VP opcode");
1458 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
,
1459 N
->getOperand(2), N
->getOperand(3));
1462 SDValue
DAGTypeLegalizer::PromoteIntRes_SExtIntBinOp(SDNode
*N
) {
1463 if (N
->getNumOperands() == 2) {
1464 // Sign extend the input.
1465 SDValue LHS
= SExtPromotedInteger(N
->getOperand(0));
1466 SDValue RHS
= SExtPromotedInteger(N
->getOperand(1));
1467 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
);
1469 assert(N
->getNumOperands() == 4 && "Unexpected number of operands!");
1470 assert(N
->isVPOpcode() && "Expected VP opcode");
1471 SDValue Mask
= N
->getOperand(2);
1472 SDValue EVL
= N
->getOperand(3);
1473 // Sign extend the input.
1474 SDValue LHS
= VPSExtPromotedInteger(N
->getOperand(0), Mask
, EVL
);
1475 SDValue RHS
= VPSExtPromotedInteger(N
->getOperand(1), Mask
, EVL
);
1476 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
,
1480 SDValue
DAGTypeLegalizer::PromoteIntRes_ZExtIntBinOp(SDNode
*N
) {
1481 if (N
->getNumOperands() == 2) {
1482 // Zero extend the input.
1483 SDValue LHS
= ZExtPromotedInteger(N
->getOperand(0));
1484 SDValue RHS
= ZExtPromotedInteger(N
->getOperand(1));
1485 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
);
1487 assert(N
->getNumOperands() == 4 && "Unexpected number of operands!");
1488 assert(N
->isVPOpcode() && "Expected VP opcode");
1489 // Zero extend the input.
1490 SDValue Mask
= N
->getOperand(2);
1491 SDValue EVL
= N
->getOperand(3);
1492 SDValue LHS
= VPZExtPromotedInteger(N
->getOperand(0), Mask
, EVL
);
1493 SDValue RHS
= VPZExtPromotedInteger(N
->getOperand(1), Mask
, EVL
);
1494 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
,
1498 SDValue
DAGTypeLegalizer::PromoteIntRes_UMINUMAX(SDNode
*N
) {
1499 SDValue LHS
= N
->getOperand(0);
1500 SDValue RHS
= N
->getOperand(1);
1502 // It doesn't matter if we sign extend or zero extend in the inputs. So do
1503 // whatever is best for the target and the promoted operands.
1504 SExtOrZExtPromotedOperands(LHS
, RHS
);
1506 return DAG
.getNode(N
->getOpcode(), SDLoc(N
),
1507 LHS
.getValueType(), LHS
, RHS
);
1510 SDValue
DAGTypeLegalizer::PromoteIntRes_SRA(SDNode
*N
) {
1511 SDValue RHS
= N
->getOperand(1);
1512 if (N
->getOpcode() != ISD::VP_SRA
) {
1513 // The input value must be properly sign extended.
1514 SDValue LHS
= SExtPromotedInteger(N
->getOperand(0));
1515 if (getTypeAction(RHS
.getValueType()) == TargetLowering::TypePromoteInteger
)
1516 RHS
= ZExtPromotedInteger(RHS
);
1517 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
);
1520 SDValue Mask
= N
->getOperand(2);
1521 SDValue EVL
= N
->getOperand(3);
1522 // The input value must be properly sign extended.
1523 SDValue LHS
= VPSExtPromotedInteger(N
->getOperand(0), Mask
, EVL
);
1524 if (getTypeAction(RHS
.getValueType()) == TargetLowering::TypePromoteInteger
)
1525 RHS
= VPZExtPromotedInteger(RHS
, Mask
, EVL
);
1526 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
,
1530 SDValue
DAGTypeLegalizer::PromoteIntRes_SRL(SDNode
*N
) {
1531 SDValue RHS
= N
->getOperand(1);
1532 if (N
->getOpcode() != ISD::VP_SRL
) {
1533 // The input value must be properly zero extended.
1534 SDValue LHS
= ZExtPromotedInteger(N
->getOperand(0));
1535 if (getTypeAction(RHS
.getValueType()) == TargetLowering::TypePromoteInteger
)
1536 RHS
= ZExtPromotedInteger(RHS
);
1537 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
);
1540 SDValue Mask
= N
->getOperand(2);
1541 SDValue EVL
= N
->getOperand(3);
1542 // The input value must be properly zero extended.
1543 SDValue LHS
= VPZExtPromotedInteger(N
->getOperand(0), Mask
, EVL
);
1544 if (getTypeAction(RHS
.getValueType()) == TargetLowering::TypePromoteInteger
)
1545 RHS
= VPZExtPromotedInteger(RHS
, Mask
, EVL
);
1546 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), LHS
.getValueType(), LHS
, RHS
,
1550 SDValue
DAGTypeLegalizer::PromoteIntRes_Rotate(SDNode
*N
) {
1551 // Lower the rotate to shifts and ORs which can be promoted.
1552 SDValue Res
= TLI
.expandROT(N
, true /*AllowVectorOps*/, DAG
);
1553 ReplaceValueWith(SDValue(N
, 0), Res
);
1557 SDValue
DAGTypeLegalizer::PromoteIntRes_FunnelShift(SDNode
*N
) {
1558 SDValue Hi
= GetPromotedInteger(N
->getOperand(0));
1559 SDValue Lo
= GetPromotedInteger(N
->getOperand(1));
1560 SDValue Amt
= N
->getOperand(2);
1561 if (getTypeAction(Amt
.getValueType()) == TargetLowering::TypePromoteInteger
)
1562 Amt
= ZExtPromotedInteger(Amt
);
1563 EVT AmtVT
= Amt
.getValueType();
1566 EVT OldVT
= N
->getOperand(0).getValueType();
1567 EVT VT
= Lo
.getValueType();
1568 unsigned Opcode
= N
->getOpcode();
1569 bool IsFSHR
= Opcode
== ISD::FSHR
;
1570 unsigned OldBits
= OldVT
.getScalarSizeInBits();
1571 unsigned NewBits
= VT
.getScalarSizeInBits();
1573 // Amount has to be interpreted modulo the old bit width.
1574 Amt
= DAG
.getNode(ISD::UREM
, DL
, AmtVT
, Amt
,
1575 DAG
.getConstant(OldBits
, DL
, AmtVT
));
1577 // If the promoted type is twice the size (or more), then we use the
1578 // traditional funnel 'double' shift codegen. This isn't necessary if the
1579 // shift amount is constant.
1580 // fshl(x,y,z) -> (((aext(x) << bw) | zext(y)) << (z % bw)) >> bw.
1581 // fshr(x,y,z) -> (((aext(x) << bw) | zext(y)) >> (z % bw)).
1582 if (NewBits
>= (2 * OldBits
) && !isa
<ConstantSDNode
>(Amt
) &&
1583 !TLI
.isOperationLegalOrCustom(Opcode
, VT
)) {
1584 SDValue HiShift
= DAG
.getConstant(OldBits
, DL
, VT
);
1585 Hi
= DAG
.getNode(ISD::SHL
, DL
, VT
, Hi
, HiShift
);
1586 Lo
= DAG
.getZeroExtendInReg(Lo
, DL
, OldVT
);
1587 SDValue Res
= DAG
.getNode(ISD::OR
, DL
, VT
, Hi
, Lo
);
1588 Res
= DAG
.getNode(IsFSHR
? ISD::SRL
: ISD::SHL
, DL
, VT
, Res
, Amt
);
1590 Res
= DAG
.getNode(ISD::SRL
, DL
, VT
, Res
, HiShift
);
1594 // Shift Lo up to occupy the upper bits of the promoted type.
1595 SDValue ShiftOffset
= DAG
.getConstant(NewBits
- OldBits
, DL
, AmtVT
);
1596 Lo
= DAG
.getNode(ISD::SHL
, DL
, VT
, Lo
, ShiftOffset
);
1598 // Increase Amount to shift the result into the lower bits of the promoted
1601 Amt
= DAG
.getNode(ISD::ADD
, DL
, AmtVT
, Amt
, ShiftOffset
);
1603 return DAG
.getNode(Opcode
, DL
, VT
, Hi
, Lo
, Amt
);
1606 // A vp version of PromoteIntRes_FunnelShift.
1607 SDValue
DAGTypeLegalizer::PromoteIntRes_VPFunnelShift(SDNode
*N
) {
1608 SDValue Hi
= GetPromotedInteger(N
->getOperand(0));
1609 SDValue Lo
= GetPromotedInteger(N
->getOperand(1));
1610 SDValue Amt
= N
->getOperand(2);
1611 SDValue Mask
= N
->getOperand(3);
1612 SDValue EVL
= N
->getOperand(4);
1613 if (getTypeAction(Amt
.getValueType()) == TargetLowering::TypePromoteInteger
)
1614 Amt
= VPZExtPromotedInteger(Amt
, Mask
, EVL
);
1615 EVT AmtVT
= Amt
.getValueType();
1618 EVT OldVT
= N
->getOperand(0).getValueType();
1619 EVT VT
= Lo
.getValueType();
1620 unsigned Opcode
= N
->getOpcode();
1621 bool IsFSHR
= Opcode
== ISD::VP_FSHR
;
1622 unsigned OldBits
= OldVT
.getScalarSizeInBits();
1623 unsigned NewBits
= VT
.getScalarSizeInBits();
1625 // Amount has to be interpreted modulo the old bit width.
1626 Amt
= DAG
.getNode(ISD::VP_UREM
, DL
, AmtVT
, Amt
,
1627 DAG
.getConstant(OldBits
, DL
, AmtVT
), Mask
, EVL
);
1629 // If the promoted type is twice the size (or more), then we use the
1630 // traditional funnel 'double' shift codegen. This isn't necessary if the
1631 // shift amount is constant.
1632 // fshl(x,y,z) -> (((aext(x) << bw) | zext(y)) << (z % bw)) >> bw.
1633 // fshr(x,y,z) -> (((aext(x) << bw) | zext(y)) >> (z % bw)).
1634 if (NewBits
>= (2 * OldBits
) && !isa
<ConstantSDNode
>(Amt
) &&
1635 !TLI
.isOperationLegalOrCustom(Opcode
, VT
)) {
1636 SDValue HiShift
= DAG
.getConstant(OldBits
, DL
, VT
);
1637 Hi
= DAG
.getNode(ISD::VP_SHL
, DL
, VT
, Hi
, HiShift
, Mask
, EVL
);
1638 Lo
= DAG
.getVPZeroExtendInReg(Lo
, Mask
, EVL
, DL
, OldVT
);
1639 SDValue Res
= DAG
.getNode(ISD::VP_OR
, DL
, VT
, Hi
, Lo
, Mask
, EVL
);
1640 Res
= DAG
.getNode(IsFSHR
? ISD::VP_SRL
: ISD::VP_SHL
, DL
, VT
, Res
, Amt
,
1643 Res
= DAG
.getNode(ISD::VP_SRL
, DL
, VT
, Res
, HiShift
, Mask
, EVL
);
1647 // Shift Lo up to occupy the upper bits of the promoted type.
1648 SDValue ShiftOffset
= DAG
.getConstant(NewBits
- OldBits
, DL
, AmtVT
);
1649 Lo
= DAG
.getNode(ISD::VP_SHL
, DL
, VT
, Lo
, ShiftOffset
, Mask
, EVL
);
1651 // Increase Amount to shift the result into the lower bits of the promoted
1654 Amt
= DAG
.getNode(ISD::VP_ADD
, DL
, AmtVT
, Amt
, ShiftOffset
, Mask
, EVL
);
1656 return DAG
.getNode(Opcode
, DL
, VT
, Hi
, Lo
, Amt
, Mask
, EVL
);
1659 SDValue
DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode
*N
) {
1660 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
1662 SDValue InOp
= N
->getOperand(0);
1665 switch (getTypeAction(InOp
.getValueType())) {
1666 default: llvm_unreachable("Unknown type action!");
1667 case TargetLowering::TypeLegal
:
1668 case TargetLowering::TypeExpandInteger
:
1671 case TargetLowering::TypePromoteInteger
:
1672 Res
= GetPromotedInteger(InOp
);
1674 case TargetLowering::TypeSplitVector
: {
1675 EVT InVT
= InOp
.getValueType();
1676 assert(InVT
.isVector() && "Cannot split scalar types");
1677 ElementCount NumElts
= InVT
.getVectorElementCount();
1678 assert(NumElts
== NVT
.getVectorElementCount() &&
1679 "Dst and Src must have the same number of elements");
1680 assert(isPowerOf2_32(NumElts
.getKnownMinValue()) &&
1681 "Promoted vector type must be a power of two");
1684 GetSplitVector(InOp
, EOp1
, EOp2
);
1686 EVT HalfNVT
= EVT::getVectorVT(*DAG
.getContext(), NVT
.getScalarType(),
1687 NumElts
.divideCoefficientBy(2));
1688 if (N
->getOpcode() == ISD::TRUNCATE
) {
1689 EOp1
= DAG
.getNode(ISD::TRUNCATE
, dl
, HalfNVT
, EOp1
);
1690 EOp2
= DAG
.getNode(ISD::TRUNCATE
, dl
, HalfNVT
, EOp2
);
1692 assert(N
->getOpcode() == ISD::VP_TRUNCATE
&&
1693 "Expected VP_TRUNCATE opcode");
1694 SDValue MaskLo
, MaskHi
, EVLLo
, EVLHi
;
1695 std::tie(MaskLo
, MaskHi
) = SplitMask(N
->getOperand(1));
1696 std::tie(EVLLo
, EVLHi
) =
1697 DAG
.SplitEVL(N
->getOperand(2), N
->getValueType(0), dl
);
1698 EOp1
= DAG
.getNode(ISD::VP_TRUNCATE
, dl
, HalfNVT
, EOp1
, MaskLo
, EVLLo
);
1699 EOp2
= DAG
.getNode(ISD::VP_TRUNCATE
, dl
, HalfNVT
, EOp2
, MaskHi
, EVLHi
);
1701 return DAG
.getNode(ISD::CONCAT_VECTORS
, dl
, NVT
, EOp1
, EOp2
);
1703 // TODO: VP_TRUNCATE need to handle when TypeWidenVector access to some
1705 case TargetLowering::TypeWidenVector
: {
1706 SDValue WideInOp
= GetWidenedVector(InOp
);
1708 // Truncate widened InOp.
1709 unsigned NumElem
= WideInOp
.getValueType().getVectorNumElements();
1710 EVT TruncVT
= EVT::getVectorVT(*DAG
.getContext(),
1711 N
->getValueType(0).getScalarType(), NumElem
);
1712 SDValue WideTrunc
= DAG
.getNode(ISD::TRUNCATE
, dl
, TruncVT
, WideInOp
);
1714 // Zero extend so that the elements are of same type as those of NVT
1715 EVT ExtVT
= EVT::getVectorVT(*DAG
.getContext(), NVT
.getVectorElementType(),
1717 SDValue WideExt
= DAG
.getNode(ISD::ZERO_EXTEND
, dl
, ExtVT
, WideTrunc
);
1719 // Extract the low NVT subvector.
1720 SDValue ZeroIdx
= DAG
.getVectorIdxConstant(0, dl
);
1721 return DAG
.getNode(ISD::EXTRACT_SUBVECTOR
, dl
, NVT
, WideExt
, ZeroIdx
);
1725 // Truncate to NVT instead of VT
1726 if (N
->getOpcode() == ISD::VP_TRUNCATE
)
1727 return DAG
.getNode(ISD::VP_TRUNCATE
, dl
, NVT
, Res
, N
->getOperand(1),
1729 return DAG
.getNode(ISD::TRUNCATE
, dl
, NVT
, Res
);
1732 SDValue
DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode
*N
, unsigned ResNo
) {
1734 return PromoteIntRes_Overflow(N
);
1736 // The operation overflowed iff the result in the larger type is not the
1737 // zero extension of its truncation to the original type.
1738 SDValue LHS
= ZExtPromotedInteger(N
->getOperand(0));
1739 SDValue RHS
= ZExtPromotedInteger(N
->getOperand(1));
1740 EVT OVT
= N
->getOperand(0).getValueType();
1741 EVT NVT
= LHS
.getValueType();
1744 // Do the arithmetic in the larger type.
1745 unsigned Opcode
= N
->getOpcode() == ISD::UADDO
? ISD::ADD
: ISD::SUB
;
1746 SDValue Res
= DAG
.getNode(Opcode
, dl
, NVT
, LHS
, RHS
);
1748 // Calculate the overflow flag: zero extend the arithmetic result from
1749 // the original type.
1750 SDValue Ofl
= DAG
.getZeroExtendInReg(Res
, dl
, OVT
);
1751 // Overflowed if and only if this is not equal to Res.
1752 Ofl
= DAG
.getSetCC(dl
, N
->getValueType(1), Ofl
, Res
, ISD::SETNE
);
1754 // Use the calculated overflow everywhere.
1755 ReplaceValueWith(SDValue(N
, 1), Ofl
);
1760 // Handle promotion for the ADDE/SUBE/UADDO_CARRY/USUBO_CARRY nodes. Notice that
1761 // the third operand of ADDE/SUBE nodes is carry flag, which differs from
1762 // the UADDO_CARRY/USUBO_CARRY nodes in that the third operand is carry Boolean.
1763 SDValue
DAGTypeLegalizer::PromoteIntRes_UADDSUBO_CARRY(SDNode
*N
,
1766 return PromoteIntRes_Overflow(N
);
1768 // We need to sign-extend the operands so the carry value computed by the
1769 // wide operation will be equivalent to the carry value computed by the
1770 // narrow operation.
1771 // An UADDO_CARRY can generate carry only if any of the operands has its
1772 // most significant bit set. Sign extension propagates the most significant
1773 // bit into the higher bits which means the extra bit that the narrow
1774 // addition would need (i.e. the carry) will be propagated through the higher
1775 // bits of the wide addition.
1776 // A USUBO_CARRY can generate borrow only if LHS < RHS and this property will
1777 // be preserved by sign extension.
1778 SDValue LHS
= SExtPromotedInteger(N
->getOperand(0));
1779 SDValue RHS
= SExtPromotedInteger(N
->getOperand(1));
1781 EVT ValueVTs
[] = {LHS
.getValueType(), N
->getValueType(1)};
1783 // Do the arithmetic in the wide type.
1784 SDValue Res
= DAG
.getNode(N
->getOpcode(), SDLoc(N
), DAG
.getVTList(ValueVTs
),
1785 LHS
, RHS
, N
->getOperand(2));
1787 // Update the users of the original carry/borrow value.
1788 ReplaceValueWith(SDValue(N
, 1), Res
.getValue(1));
1790 return SDValue(Res
.getNode(), 0);
1793 SDValue
DAGTypeLegalizer::PromoteIntRes_SADDSUBO_CARRY(SDNode
*N
,
1795 assert(ResNo
== 1 && "Don't know how to promote other results yet.");
1796 return PromoteIntRes_Overflow(N
);
1799 SDValue
DAGTypeLegalizer::PromoteIntRes_ABS(SDNode
*N
) {
1800 EVT OVT
= N
->getValueType(0);
1801 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OVT
);
1803 // If a larger ABS or SMAX isn't supported by the target, try to expand now.
1804 // If we expand later we'll end up sign extending more than just the sra input
1805 // in sra+xor+sub expansion.
1806 if (!OVT
.isVector() &&
1807 !TLI
.isOperationLegalOrCustomOrPromote(ISD::ABS
, NVT
) &&
1808 !TLI
.isOperationLegal(ISD::SMAX
, NVT
)) {
1809 if (SDValue Res
= TLI
.expandABS(N
, DAG
))
1810 return DAG
.getNode(ISD::ANY_EXTEND
, SDLoc(N
), NVT
, Res
);
1813 SDValue Op0
= SExtPromotedInteger(N
->getOperand(0));
1814 return DAG
.getNode(ISD::ABS
, SDLoc(N
), Op0
.getValueType(), Op0
);
1817 SDValue
DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode
*N
, unsigned ResNo
) {
1818 // Promote the overflow bit trivially.
1820 return PromoteIntRes_Overflow(N
);
1822 SDValue LHS
= N
->getOperand(0), RHS
= N
->getOperand(1);
1824 EVT SmallVT
= LHS
.getValueType();
1826 // To determine if the result overflowed in a larger type, we extend the
1827 // input to the larger type, do the multiply (checking if it overflows),
1828 // then also check the high bits of the result to see if overflow happened
1830 if (N
->getOpcode() == ISD::SMULO
) {
1831 LHS
= SExtPromotedInteger(LHS
);
1832 RHS
= SExtPromotedInteger(RHS
);
1834 LHS
= ZExtPromotedInteger(LHS
);
1835 RHS
= ZExtPromotedInteger(RHS
);
1837 SDVTList VTs
= DAG
.getVTList(LHS
.getValueType(), N
->getValueType(1));
1838 SDValue Mul
= DAG
.getNode(N
->getOpcode(), DL
, VTs
, LHS
, RHS
);
1840 // Overflow occurred if it occurred in the larger type, or if the high part
1841 // of the result does not zero/sign-extend the low part. Check this second
1842 // possibility first.
1844 if (N
->getOpcode() == ISD::UMULO
) {
1845 // Unsigned overflow occurred if the high part is non-zero.
1846 unsigned Shift
= SmallVT
.getScalarSizeInBits();
1848 DAG
.getNode(ISD::SRL
, DL
, Mul
.getValueType(), Mul
,
1849 DAG
.getShiftAmountConstant(Shift
, Mul
.getValueType(), DL
));
1850 Overflow
= DAG
.getSetCC(DL
, N
->getValueType(1), Hi
,
1851 DAG
.getConstant(0, DL
, Hi
.getValueType()),
1854 // Signed overflow occurred if the high part does not sign extend the low.
1855 SDValue SExt
= DAG
.getNode(ISD::SIGN_EXTEND_INREG
, DL
, Mul
.getValueType(),
1856 Mul
, DAG
.getValueType(SmallVT
));
1857 Overflow
= DAG
.getSetCC(DL
, N
->getValueType(1), SExt
, Mul
, ISD::SETNE
);
1860 // The only other way for overflow to occur is if the multiplication in the
1861 // larger type itself overflowed.
1862 Overflow
= DAG
.getNode(ISD::OR
, DL
, N
->getValueType(1), Overflow
,
1863 SDValue(Mul
.getNode(), 1));
1865 // Use the calculated overflow everywhere.
1866 ReplaceValueWith(SDValue(N
, 1), Overflow
);
1870 SDValue
DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode
*N
) {
1871 return DAG
.getUNDEF(TLI
.getTypeToTransformTo(*DAG
.getContext(),
1872 N
->getValueType(0)));
1875 SDValue
DAGTypeLegalizer::PromoteIntRes_VSCALE(SDNode
*N
) {
1876 EVT VT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
1878 const APInt
&MulImm
= N
->getConstantOperandAPInt(0);
1879 return DAG
.getVScale(SDLoc(N
), VT
, MulImm
.sext(VT
.getSizeInBits()));
1882 SDValue
DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode
*N
) {
1883 SDValue Chain
= N
->getOperand(0); // Get the chain.
1884 SDValue Ptr
= N
->getOperand(1); // Get the pointer.
1885 EVT VT
= N
->getValueType(0);
1888 MVT RegVT
= TLI
.getRegisterType(*DAG
.getContext(), VT
);
1889 unsigned NumRegs
= TLI
.getNumRegisters(*DAG
.getContext(), VT
);
1890 // The argument is passed as NumRegs registers of type RegVT.
1892 SmallVector
<SDValue
, 8> Parts(NumRegs
);
1893 for (unsigned i
= 0; i
< NumRegs
; ++i
) {
1894 Parts
[i
] = DAG
.getVAArg(RegVT
, dl
, Chain
, Ptr
, N
->getOperand(2),
1895 N
->getConstantOperandVal(3));
1896 Chain
= Parts
[i
].getValue(1);
1899 // Handle endianness of the load.
1900 if (DAG
.getDataLayout().isBigEndian())
1901 std::reverse(Parts
.begin(), Parts
.end());
1903 // Assemble the parts in the promoted type.
1904 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
1905 SDValue Res
= DAG
.getNode(ISD::ZERO_EXTEND
, dl
, NVT
, Parts
[0]);
1906 for (unsigned i
= 1; i
< NumRegs
; ++i
) {
1907 SDValue Part
= DAG
.getNode(ISD::ZERO_EXTEND
, dl
, NVT
, Parts
[i
]);
1908 // Shift it to the right position and "or" it in.
1909 Part
= DAG
.getNode(ISD::SHL
, dl
, NVT
, Part
,
1910 DAG
.getConstant(i
* RegVT
.getSizeInBits(), dl
,
1911 TLI
.getPointerTy(DAG
.getDataLayout())));
1912 Res
= DAG
.getNode(ISD::OR
, dl
, NVT
, Res
, Part
);
1915 // Modified the chain result - switch anything that used the old chain to
1917 ReplaceValueWith(SDValue(N
, 1), Chain
);
1922 //===----------------------------------------------------------------------===//
1923 // Integer Operand Promotion
1924 //===----------------------------------------------------------------------===//
1926 /// PromoteIntegerOperand - This method is called when the specified operand of
1927 /// the specified node is found to need promotion. At this point, all of the
1928 /// result types of the node are known to be legal, but other operands of the
1929 /// node may need promotion or expansion as well as the specified one.
1930 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode
*N
, unsigned OpNo
) {
1931 LLVM_DEBUG(dbgs() << "Promote integer operand: "; N
->dump(&DAG
));
1932 SDValue Res
= SDValue();
1933 if (CustomLowerNode(N
, N
->getOperand(OpNo
).getValueType(), false)) {
1934 LLVM_DEBUG(dbgs() << "Node has been custom lowered, done\n");
1938 switch (N
->getOpcode()) {
1941 dbgs() << "PromoteIntegerOperand Op #" << OpNo
<< ": ";
1942 N
->dump(&DAG
); dbgs() << "\n";
1944 report_fatal_error("Do not know how to promote this operator's operand!");
1946 case ISD::ANY_EXTEND
: Res
= PromoteIntOp_ANY_EXTEND(N
); break;
1947 case ISD::ATOMIC_STORE
:
1948 Res
= PromoteIntOp_ATOMIC_STORE(cast
<AtomicSDNode
>(N
));
1950 case ISD::BITCAST
: Res
= PromoteIntOp_BITCAST(N
); break;
1951 case ISD::BR_CC
: Res
= PromoteIntOp_BR_CC(N
, OpNo
); break;
1952 case ISD::BRCOND
: Res
= PromoteIntOp_BRCOND(N
, OpNo
); break;
1953 case ISD::BUILD_PAIR
: Res
= PromoteIntOp_BUILD_PAIR(N
); break;
1954 case ISD::BUILD_VECTOR
: Res
= PromoteIntOp_BUILD_VECTOR(N
); break;
1955 case ISD::CONCAT_VECTORS
: Res
= PromoteIntOp_CONCAT_VECTORS(N
); break;
1956 case ISD::EXTRACT_VECTOR_ELT
: Res
= PromoteIntOp_EXTRACT_VECTOR_ELT(N
); break;
1958 Res
= PromoteIntOp_FAKE_USE(N
);
1960 case ISD::INSERT_VECTOR_ELT
:
1961 Res
= PromoteIntOp_INSERT_VECTOR_ELT(N
, OpNo
);
1963 case ISD::SPLAT_VECTOR
:
1964 case ISD::SCALAR_TO_VECTOR
:
1965 case ISD::EXPERIMENTAL_VP_SPLAT
:
1966 Res
= PromoteIntOp_ScalarOp(N
);
1969 case ISD::SELECT
: Res
= PromoteIntOp_SELECT(N
, OpNo
); break;
1970 case ISD::SELECT_CC
: Res
= PromoteIntOp_SELECT_CC(N
, OpNo
); break;
1972 case ISD::SETCC
: Res
= PromoteIntOp_SETCC(N
, OpNo
); break;
1973 case ISD::SIGN_EXTEND
: Res
= PromoteIntOp_SIGN_EXTEND(N
); break;
1974 case ISD::VP_SIGN_EXTEND
: Res
= PromoteIntOp_VP_SIGN_EXTEND(N
); break;
1975 case ISD::VP_SINT_TO_FP
:
1976 case ISD::SINT_TO_FP
: Res
= PromoteIntOp_SINT_TO_FP(N
); break;
1977 case ISD::STRICT_SINT_TO_FP
: Res
= PromoteIntOp_STRICT_SINT_TO_FP(N
); break;
1978 case ISD::STORE
: Res
= PromoteIntOp_STORE(cast
<StoreSDNode
>(N
),
1981 Res
= PromoteIntOp_VP_STORE(cast
<VPStoreSDNode
>(N
), OpNo
);
1983 case ISD::MSTORE
: Res
= PromoteIntOp_MSTORE(cast
<MaskedStoreSDNode
>(N
),
1985 case ISD::MLOAD
: Res
= PromoteIntOp_MLOAD(cast
<MaskedLoadSDNode
>(N
),
1987 case ISD::MGATHER
: Res
= PromoteIntOp_MGATHER(cast
<MaskedGatherSDNode
>(N
),
1989 case ISD::MSCATTER
: Res
= PromoteIntOp_MSCATTER(cast
<MaskedScatterSDNode
>(N
),
1991 case ISD::VECTOR_COMPRESS
:
1992 Res
= PromoteIntOp_VECTOR_COMPRESS(N
, OpNo
);
1994 case ISD::VP_TRUNCATE
:
1995 case ISD::TRUNCATE
: Res
= PromoteIntOp_TRUNCATE(N
); break;
1996 case ISD::BF16_TO_FP
:
1997 case ISD::FP16_TO_FP
:
1998 case ISD::VP_UINT_TO_FP
:
1999 case ISD::UINT_TO_FP
: Res
= PromoteIntOp_UINT_TO_FP(N
); break;
2000 case ISD::STRICT_FP16_TO_FP
:
2001 case ISD::STRICT_UINT_TO_FP
: Res
= PromoteIntOp_STRICT_UINT_TO_FP(N
); break;
2002 case ISD::ZERO_EXTEND
: Res
= PromoteIntOp_ZERO_EXTEND(N
); break;
2003 case ISD::VP_ZERO_EXTEND
: Res
= PromoteIntOp_VP_ZERO_EXTEND(N
); break;
2004 case ISD::EXTRACT_SUBVECTOR
: Res
= PromoteIntOp_EXTRACT_SUBVECTOR(N
); break;
2005 case ISD::INSERT_SUBVECTOR
: Res
= PromoteIntOp_INSERT_SUBVECTOR(N
); break;
2011 case ISD::ROTR
: Res
= PromoteIntOp_Shift(N
); break;
2014 case ISD::UCMP
: Res
= PromoteIntOp_CMP(N
); break;
2017 case ISD::FSHR
: Res
= PromoteIntOp_FunnelShift(N
); break;
2019 case ISD::FRAMEADDR
:
2020 case ISD::RETURNADDR
: Res
= PromoteIntOp_FRAMERETURNADDR(N
); break;
2023 case ISD::SMULFIXSAT
:
2025 case ISD::UMULFIXSAT
:
2027 case ISD::SDIVFIXSAT
:
2029 case ISD::UDIVFIXSAT
: Res
= PromoteIntOp_FIX(N
); break;
2031 case ISD::STRICT_FPOWI
:
2033 case ISD::STRICT_FLDEXP
: Res
= PromoteIntOp_ExpOp(N
); break;
2034 case ISD::VECREDUCE_ADD
:
2035 case ISD::VECREDUCE_MUL
:
2036 case ISD::VECREDUCE_AND
:
2037 case ISD::VECREDUCE_OR
:
2038 case ISD::VECREDUCE_XOR
:
2039 case ISD::VECREDUCE_SMAX
:
2040 case ISD::VECREDUCE_SMIN
:
2041 case ISD::VECREDUCE_UMAX
:
2042 case ISD::VECREDUCE_UMIN
: Res
= PromoteIntOp_VECREDUCE(N
); break;
2043 case ISD::VP_REDUCE_ADD
:
2044 case ISD::VP_REDUCE_MUL
:
2045 case ISD::VP_REDUCE_AND
:
2046 case ISD::VP_REDUCE_OR
:
2047 case ISD::VP_REDUCE_XOR
:
2048 case ISD::VP_REDUCE_SMAX
:
2049 case ISD::VP_REDUCE_SMIN
:
2050 case ISD::VP_REDUCE_UMAX
:
2051 case ISD::VP_REDUCE_UMIN
:
2052 Res
= PromoteIntOp_VP_REDUCE(N
, OpNo
);
2055 case ISD::SET_ROUNDING
: Res
= PromoteIntOp_SET_ROUNDING(N
); break;
2057 Res
= PromoteIntOp_STACKMAP(N
, OpNo
);
2059 case ISD::PATCHPOINT
:
2060 Res
= PromoteIntOp_PATCHPOINT(N
, OpNo
);
2062 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD
:
2063 case ISD::EXPERIMENTAL_VP_STRIDED_STORE
:
2064 Res
= PromoteIntOp_VP_STRIDED(N
, OpNo
);
2066 case ISD::EXPERIMENTAL_VP_SPLICE
:
2067 Res
= PromoteIntOp_VP_SPLICE(N
, OpNo
);
2069 case ISD::EXPERIMENTAL_VECTOR_HISTOGRAM
:
2070 Res
= PromoteIntOp_VECTOR_HISTOGRAM(N
, OpNo
);
2074 // If the result is null, the sub-method took care of registering results etc.
2075 if (!Res
.getNode()) return false;
2077 // If the result is N, the sub-method updated N in place. Tell the legalizer
2079 if (Res
.getNode() == N
)
2082 const bool IsStrictFp
= N
->isStrictFPOpcode();
2083 assert(Res
.getValueType() == N
->getValueType(0) &&
2084 N
->getNumValues() == (IsStrictFp
? 2 : 1) &&
2085 "Invalid operand expansion");
2086 LLVM_DEBUG(dbgs() << "Replacing: "; N
->dump(&DAG
); dbgs() << " with: ";
2089 ReplaceValueWith(SDValue(N
, 0), Res
);
2091 ReplaceValueWith(SDValue(N
, 1), SDValue(Res
.getNode(), 1));
2096 // These operands can be either sign extended or zero extended as long as we
2097 // treat them the same. If an extension is free, choose that. Otherwise, follow
2098 // target preference.
2099 void DAGTypeLegalizer::SExtOrZExtPromotedOperands(SDValue
&LHS
, SDValue
&RHS
) {
2100 SDValue OpL
= GetPromotedInteger(LHS
);
2101 SDValue OpR
= GetPromotedInteger(RHS
);
2103 if (TLI
.isSExtCheaperThanZExt(LHS
.getValueType(), OpL
.getValueType())) {
2104 // The target would prefer to promote the comparison operand with sign
2105 // extension. Honor that unless the promoted values are already zero
2107 unsigned OpLEffectiveBits
=
2108 DAG
.computeKnownBits(OpL
).countMaxActiveBits();
2109 unsigned OpREffectiveBits
=
2110 DAG
.computeKnownBits(OpR
).countMaxActiveBits();
2111 if (OpLEffectiveBits
<= LHS
.getScalarValueSizeInBits() &&
2112 OpREffectiveBits
<= RHS
.getScalarValueSizeInBits()) {
2118 // The promoted values aren't zero extended, use a sext_inreg.
2119 LHS
= SExtPromotedInteger(LHS
);
2120 RHS
= SExtPromotedInteger(RHS
);
2124 // Prefer to promote the comparison operand with zero extension.
2126 // If the width of OpL/OpR excluding the duplicated sign bits is no greater
2127 // than the width of LHS/RHS, we can avoid/ inserting a zext_inreg operation
2128 // that we might not be able to remove.
2129 unsigned OpLEffectiveBits
= DAG
.ComputeMaxSignificantBits(OpL
);
2130 unsigned OpREffectiveBits
= DAG
.ComputeMaxSignificantBits(OpR
);
2131 if (OpLEffectiveBits
<= LHS
.getScalarValueSizeInBits() &&
2132 OpREffectiveBits
<= RHS
.getScalarValueSizeInBits()) {
2138 // Otherwise, use zext_inreg.
2139 LHS
= ZExtPromotedInteger(LHS
);
2140 RHS
= ZExtPromotedInteger(RHS
);
2143 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
2144 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
2145 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue
&LHS
, SDValue
&RHS
,
2146 ISD::CondCode CCCode
) {
2147 // We have to insert explicit sign or zero extends. Note that we could
2148 // insert sign extends for ALL conditions. For those operations where either
2149 // zero or sign extension would be valid, we ask the target which extension
2152 // Signed comparisons always require sign extension.
2153 if (ISD::isSignedIntSetCC(CCCode
)) {
2154 LHS
= SExtPromotedInteger(LHS
);
2155 RHS
= SExtPromotedInteger(RHS
);
2159 assert((ISD::isUnsignedIntSetCC(CCCode
) || ISD::isIntEqualitySetCC(CCCode
)) &&
2160 "Unknown integer comparison!");
2162 SExtOrZExtPromotedOperands(LHS
, RHS
);
2165 SDValue
DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode
*N
) {
2166 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
2167 return DAG
.getNode(ISD::ANY_EXTEND
, SDLoc(N
), N
->getValueType(0), Op
);
2170 SDValue
DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode
*N
) {
2171 SDValue Op1
= GetPromotedInteger(N
->getOperand(1));
2172 return DAG
.getAtomic(N
->getOpcode(), SDLoc(N
), N
->getMemoryVT(),
2173 N
->getChain(), Op1
, N
->getBasePtr(), N
->getMemOperand());
2176 SDValue
DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode
*N
) {
2177 // This should only occur in unusual situations like bitcasting to an
2178 // x86_fp80, so just turn it into a store+load
2179 return CreateStackStoreLoad(N
->getOperand(0), N
->getValueType(0));
2182 SDValue
DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode
*N
, unsigned OpNo
) {
2183 assert(OpNo
== 2 && "Don't know how to promote this operand!");
2185 SDValue LHS
= N
->getOperand(2);
2186 SDValue RHS
= N
->getOperand(3);
2187 PromoteSetCCOperands(LHS
, RHS
, cast
<CondCodeSDNode
>(N
->getOperand(1))->get());
2189 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
2191 return SDValue(DAG
.UpdateNodeOperands(N
, N
->getOperand(0),
2192 N
->getOperand(1), LHS
, RHS
, N
->getOperand(4)),
2196 SDValue
DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode
*N
, unsigned OpNo
) {
2197 assert(OpNo
== 1 && "only know how to promote condition");
2199 // Promote all the way up to the canonical SetCC type.
2200 SDValue Cond
= PromoteTargetBoolean(N
->getOperand(1), MVT::Other
);
2202 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
2203 return SDValue(DAG
.UpdateNodeOperands(N
, N
->getOperand(0), Cond
,
2204 N
->getOperand(2)), 0);
2207 SDValue
DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode
*N
) {
2208 // Since the result type is legal, the operands must promote to it.
2209 EVT OVT
= N
->getOperand(0).getValueType();
2210 SDValue Lo
= ZExtPromotedInteger(N
->getOperand(0));
2211 SDValue Hi
= GetPromotedInteger(N
->getOperand(1));
2212 assert(Lo
.getValueType() == N
->getValueType(0) && "Operand over promoted?");
2215 Hi
= DAG
.getNode(ISD::SHL
, dl
, N
->getValueType(0), Hi
,
2216 DAG
.getConstant(OVT
.getSizeInBits(), dl
,
2217 TLI
.getPointerTy(DAG
.getDataLayout())));
2218 return DAG
.getNode(ISD::OR
, dl
, N
->getValueType(0), Lo
, Hi
);
2221 SDValue
DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode
*N
) {
2222 // The vector type is legal but the element type is not. This implies
2223 // that the vector is a power-of-two in length and that the element
2224 // type does not have a strange size (eg: it is not i1).
2225 EVT VecVT
= N
->getValueType(0);
2226 unsigned NumElts
= VecVT
.getVectorNumElements();
2227 assert(!((NumElts
& 1) && (!TLI
.isTypeLegal(VecVT
))) &&
2228 "Legal vector of one illegal element?");
2230 // Promote the inserted value. The type does not need to match the
2231 // vector element type. Check that any extra bits introduced will be
2233 assert(N
->getOperand(0).getValueSizeInBits() >=
2234 N
->getValueType(0).getScalarSizeInBits() &&
2235 "Type of inserted value narrower than vector element type!");
2237 SmallVector
<SDValue
, 16> NewOps
;
2238 for (unsigned i
= 0; i
< NumElts
; ++i
)
2239 NewOps
.push_back(GetPromotedInteger(N
->getOperand(i
)));
2241 return SDValue(DAG
.UpdateNodeOperands(N
, NewOps
), 0);
2244 SDValue
DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode
*N
,
2247 // Promote the inserted value. This is valid because the type does not
2248 // have to match the vector element type.
2250 // Check that any extra bits introduced will be truncated away.
2251 assert(N
->getOperand(1).getValueSizeInBits() >=
2252 N
->getValueType(0).getScalarSizeInBits() &&
2253 "Type of inserted value narrower than vector element type!");
2254 return SDValue(DAG
.UpdateNodeOperands(N
, N
->getOperand(0),
2255 GetPromotedInteger(N
->getOperand(1)),
2260 assert(OpNo
== 2 && "Different operand and result vector types?");
2262 // Promote the index.
2263 SDValue Idx
= DAG
.getZExtOrTrunc(N
->getOperand(2), SDLoc(N
),
2264 TLI
.getVectorIdxTy(DAG
.getDataLayout()));
2265 return SDValue(DAG
.UpdateNodeOperands(N
, N
->getOperand(0),
2266 N
->getOperand(1), Idx
), 0);
2269 SDValue
DAGTypeLegalizer::PromoteIntOp_ScalarOp(SDNode
*N
) {
2270 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
2271 if (N
->getOpcode() == ISD::EXPERIMENTAL_VP_SPLAT
)
2273 DAG
.UpdateNodeOperands(N
, Op
, N
->getOperand(1), N
->getOperand(2)), 0);
2275 // Integer SPLAT_VECTOR/SCALAR_TO_VECTOR operands are implicitly truncated,
2276 // so just promote the operand in place.
2277 return SDValue(DAG
.UpdateNodeOperands(N
, Op
), 0);
2280 SDValue
DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode
*N
, unsigned OpNo
) {
2281 assert(OpNo
== 0 && "Only know how to promote the condition!");
2282 SDValue Cond
= N
->getOperand(0);
2283 EVT OpTy
= N
->getOperand(1).getValueType();
2285 if (N
->getOpcode() == ISD::VSELECT
)
2286 if (SDValue Res
= WidenVSELECTMask(N
))
2287 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), N
->getValueType(0),
2288 Res
, N
->getOperand(1), N
->getOperand(2));
2290 // Promote all the way up to the canonical SetCC type.
2291 EVT OpVT
= N
->getOpcode() == ISD::SELECT
? OpTy
.getScalarType() : OpTy
;
2292 Cond
= PromoteTargetBoolean(Cond
, OpVT
);
2294 return SDValue(DAG
.UpdateNodeOperands(N
, Cond
, N
->getOperand(1),
2295 N
->getOperand(2)), 0);
2298 SDValue
DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode
*N
, unsigned OpNo
) {
2299 assert(OpNo
== 0 && "Don't know how to promote this operand!");
2301 SDValue LHS
= N
->getOperand(0);
2302 SDValue RHS
= N
->getOperand(1);
2303 PromoteSetCCOperands(LHS
, RHS
, cast
<CondCodeSDNode
>(N
->getOperand(4))->get());
2305 // The CC (#4) and the possible return values (#2 and #3) have legal types.
2306 return SDValue(DAG
.UpdateNodeOperands(N
, LHS
, RHS
, N
->getOperand(2),
2307 N
->getOperand(3), N
->getOperand(4)), 0);
2310 SDValue
DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode
*N
, unsigned OpNo
) {
2311 assert(OpNo
== 0 && "Don't know how to promote this operand!");
2313 SDValue LHS
= N
->getOperand(0);
2314 SDValue RHS
= N
->getOperand(1);
2315 PromoteSetCCOperands(LHS
, RHS
, cast
<CondCodeSDNode
>(N
->getOperand(2))->get());
2317 // The CC (#2) is always legal.
2318 if (N
->getOpcode() == ISD::SETCC
)
2319 return SDValue(DAG
.UpdateNodeOperands(N
, LHS
, RHS
, N
->getOperand(2)), 0);
2321 assert(N
->getOpcode() == ISD::VP_SETCC
&& "Expected VP_SETCC opcode");
2323 return SDValue(DAG
.UpdateNodeOperands(N
, LHS
, RHS
, N
->getOperand(2),
2324 N
->getOperand(3), N
->getOperand(4)),
2328 SDValue
DAGTypeLegalizer::PromoteIntOp_Shift(SDNode
*N
) {
2329 return SDValue(DAG
.UpdateNodeOperands(N
, N
->getOperand(0),
2330 ZExtPromotedInteger(N
->getOperand(1))), 0);
2333 SDValue
DAGTypeLegalizer::PromoteIntOp_CMP(SDNode
*N
) {
2334 SDValue LHS
= N
->getOperand(0);
2335 SDValue RHS
= N
->getOperand(1);
2337 if (N
->getOpcode() == ISD::SCMP
) {
2338 LHS
= SExtPromotedInteger(LHS
);
2339 RHS
= SExtPromotedInteger(RHS
);
2341 SExtOrZExtPromotedOperands(LHS
, RHS
);
2344 return SDValue(DAG
.UpdateNodeOperands(N
, LHS
, RHS
), 0);
2347 SDValue
DAGTypeLegalizer::PromoteIntOp_FunnelShift(SDNode
*N
) {
2348 return SDValue(DAG
.UpdateNodeOperands(N
, N
->getOperand(0), N
->getOperand(1),
2349 ZExtPromotedInteger(N
->getOperand(2))), 0);
2352 SDValue
DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode
*N
) {
2353 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
2355 Op
= DAG
.getNode(ISD::ANY_EXTEND
, dl
, N
->getValueType(0), Op
);
2356 return DAG
.getNode(ISD::SIGN_EXTEND_INREG
, dl
, Op
.getValueType(),
2357 Op
, DAG
.getValueType(N
->getOperand(0).getValueType()));
2360 SDValue
DAGTypeLegalizer::PromoteIntOp_VP_SIGN_EXTEND(SDNode
*N
) {
2362 EVT VT
= N
->getValueType(0);
2363 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
2364 // FIXME: There is no VP_ANY_EXTEND yet.
2365 Op
= DAG
.getNode(ISD::VP_ZERO_EXTEND
, dl
, VT
, Op
, N
->getOperand(1),
2368 VT
.getScalarSizeInBits() - N
->getOperand(0).getScalarValueSizeInBits();
2369 SDValue ShAmt
= DAG
.getShiftAmountConstant(Diff
, VT
, dl
);
2370 // FIXME: There is no VP_SIGN_EXTEND_INREG so use a pair of shifts.
2371 SDValue Shl
= DAG
.getNode(ISD::VP_SHL
, dl
, VT
, Op
, ShAmt
, N
->getOperand(1),
2373 return DAG
.getNode(ISD::VP_SRA
, dl
, VT
, Shl
, ShAmt
, N
->getOperand(1),
2377 SDValue
DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode
*N
) {
2378 if (N
->getOpcode() == ISD::VP_SINT_TO_FP
)
2379 return SDValue(DAG
.UpdateNodeOperands(N
,
2380 SExtPromotedInteger(N
->getOperand(0)),
2381 N
->getOperand(1), N
->getOperand(2)),
2383 return SDValue(DAG
.UpdateNodeOperands(N
,
2384 SExtPromotedInteger(N
->getOperand(0))), 0);
2387 SDValue
DAGTypeLegalizer::PromoteIntOp_STRICT_SINT_TO_FP(SDNode
*N
) {
2388 return SDValue(DAG
.UpdateNodeOperands(N
, N
->getOperand(0),
2389 SExtPromotedInteger(N
->getOperand(1))), 0);
2392 SDValue
DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode
*N
, unsigned OpNo
){
2393 assert(ISD::isUNINDEXEDStore(N
) && "Indexed store during type legalization!");
2394 SDValue Ch
= N
->getChain(), Ptr
= N
->getBasePtr();
2397 SDValue Val
= GetPromotedInteger(N
->getValue()); // Get promoted value.
2399 // Truncate the value and store the result.
2400 return DAG
.getTruncStore(Ch
, dl
, Val
, Ptr
,
2401 N
->getMemoryVT(), N
->getMemOperand());
2404 SDValue
DAGTypeLegalizer::PromoteIntOp_VP_STORE(VPStoreSDNode
*N
,
2407 assert(OpNo
== 1 && "Unexpected operand for promotion");
2408 assert(!N
->isIndexed() && "expecting unindexed vp_store!");
2410 SDValue DataOp
= GetPromotedInteger(N
->getValue());
2411 return DAG
.getTruncStoreVP(N
->getChain(), SDLoc(N
), DataOp
, N
->getBasePtr(),
2412 N
->getMask(), N
->getVectorLength(),
2413 N
->getMemoryVT(), N
->getMemOperand(),
2414 N
->isCompressingStore());
2417 SDValue
DAGTypeLegalizer::PromoteIntOp_MSTORE(MaskedStoreSDNode
*N
,
2419 SDValue DataOp
= N
->getValue();
2420 SDValue Mask
= N
->getMask();
2423 // The Mask. Update in place.
2424 EVT DataVT
= DataOp
.getValueType();
2425 Mask
= PromoteTargetBoolean(Mask
, DataVT
);
2426 SmallVector
<SDValue
, 4> NewOps(N
->ops());
2428 return SDValue(DAG
.UpdateNodeOperands(N
, NewOps
), 0);
2431 assert(OpNo
== 1 && "Unexpected operand for promotion");
2432 DataOp
= GetPromotedInteger(DataOp
);
2434 return DAG
.getMaskedStore(N
->getChain(), SDLoc(N
), DataOp
, N
->getBasePtr(),
2435 N
->getOffset(), Mask
, N
->getMemoryVT(),
2436 N
->getMemOperand(), N
->getAddressingMode(),
2437 /*IsTruncating*/ true, N
->isCompressingStore());
2440 SDValue
DAGTypeLegalizer::PromoteIntOp_MLOAD(MaskedLoadSDNode
*N
,
2442 assert(OpNo
== 3 && "Only know how to promote the mask!");
2443 EVT DataVT
= N
->getValueType(0);
2444 SDValue Mask
= PromoteTargetBoolean(N
->getOperand(OpNo
), DataVT
);
2445 SmallVector
<SDValue
, 4> NewOps(N
->ops());
2446 NewOps
[OpNo
] = Mask
;
2447 SDNode
*Res
= DAG
.UpdateNodeOperands(N
, NewOps
);
2449 return SDValue(Res
, 0);
2451 // Update triggered CSE, do our own replacement since caller can't.
2452 ReplaceValueWith(SDValue(N
, 0), SDValue(Res
, 0));
2453 ReplaceValueWith(SDValue(N
, 1), SDValue(Res
, 1));
2457 SDValue
DAGTypeLegalizer::PromoteIntOp_MGATHER(MaskedGatherSDNode
*N
,
2459 SmallVector
<SDValue
, 5> NewOps(N
->ops());
2463 EVT DataVT
= N
->getValueType(0);
2464 NewOps
[OpNo
] = PromoteTargetBoolean(N
->getOperand(OpNo
), DataVT
);
2465 } else if (OpNo
== 4) {
2467 if (N
->isIndexSigned())
2468 // Need to sign extend the index since the bits will likely be used.
2469 NewOps
[OpNo
] = SExtPromotedInteger(N
->getOperand(OpNo
));
2471 NewOps
[OpNo
] = ZExtPromotedInteger(N
->getOperand(OpNo
));
2473 NewOps
[OpNo
] = GetPromotedInteger(N
->getOperand(OpNo
));
2475 SDNode
*Res
= DAG
.UpdateNodeOperands(N
, NewOps
);
2477 return SDValue(Res
, 0);
2479 // Update triggered CSE, do our own replacement since caller can't.
2480 ReplaceValueWith(SDValue(N
, 0), SDValue(Res
, 0));
2481 ReplaceValueWith(SDValue(N
, 1), SDValue(Res
, 1));
2485 SDValue
DAGTypeLegalizer::PromoteIntOp_MSCATTER(MaskedScatterSDNode
*N
,
2487 bool TruncateStore
= N
->isTruncatingStore();
2488 SmallVector
<SDValue
, 5> NewOps(N
->ops());
2492 EVT DataVT
= N
->getValue().getValueType();
2493 NewOps
[OpNo
] = PromoteTargetBoolean(N
->getOperand(OpNo
), DataVT
);
2494 } else if (OpNo
== 4) {
2496 if (N
->isIndexSigned())
2497 // Need to sign extend the index since the bits will likely be used.
2498 NewOps
[OpNo
] = SExtPromotedInteger(N
->getOperand(OpNo
));
2500 NewOps
[OpNo
] = ZExtPromotedInteger(N
->getOperand(OpNo
));
2502 NewOps
[OpNo
] = GetPromotedInteger(N
->getOperand(OpNo
));
2503 TruncateStore
= true;
2506 return DAG
.getMaskedScatter(DAG
.getVTList(MVT::Other
), N
->getMemoryVT(),
2507 SDLoc(N
), NewOps
, N
->getMemOperand(),
2508 N
->getIndexType(), TruncateStore
);
2511 SDValue
DAGTypeLegalizer::PromoteIntOp_VECTOR_COMPRESS(SDNode
*N
,
2513 assert(OpNo
== 1 && "Can only promote VECTOR_COMPRESS mask.");
2514 SDValue Vec
= N
->getOperand(0);
2515 EVT VT
= Vec
.getValueType();
2516 SDValue Passthru
= N
->getOperand(2);
2517 SDValue Mask
= PromoteTargetBoolean(N
->getOperand(1), VT
);
2518 return DAG
.getNode(ISD::VECTOR_COMPRESS
, SDLoc(N
), VT
, Vec
, Mask
, Passthru
);
2521 SDValue
DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode
*N
) {
2522 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
2523 if (N
->getOpcode() == ISD::VP_TRUNCATE
)
2524 return DAG
.getNode(ISD::VP_TRUNCATE
, SDLoc(N
), N
->getValueType(0), Op
,
2525 N
->getOperand(1), N
->getOperand(2));
2526 return DAG
.getNode(ISD::TRUNCATE
, SDLoc(N
), N
->getValueType(0), Op
);
2529 SDValue
DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode
*N
) {
2530 if (N
->getOpcode() == ISD::VP_UINT_TO_FP
)
2531 return SDValue(DAG
.UpdateNodeOperands(N
,
2532 ZExtPromotedInteger(N
->getOperand(0)),
2533 N
->getOperand(1), N
->getOperand(2)),
2535 return SDValue(DAG
.UpdateNodeOperands(N
,
2536 ZExtPromotedInteger(N
->getOperand(0))), 0);
2539 SDValue
DAGTypeLegalizer::PromoteIntOp_STRICT_UINT_TO_FP(SDNode
*N
) {
2540 return SDValue(DAG
.UpdateNodeOperands(N
, N
->getOperand(0),
2541 ZExtPromotedInteger(N
->getOperand(1))), 0);
2544 SDValue
DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode
*N
) {
2546 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
2547 Op
= DAG
.getNode(ISD::ANY_EXTEND
, dl
, N
->getValueType(0), Op
);
2548 return DAG
.getZeroExtendInReg(Op
, dl
, N
->getOperand(0).getValueType());
2551 SDValue
DAGTypeLegalizer::PromoteIntOp_VP_ZERO_EXTEND(SDNode
*N
) {
2553 EVT VT
= N
->getValueType(0);
2554 SDValue Op
= GetPromotedInteger(N
->getOperand(0));
2555 // FIXME: There is no VP_ANY_EXTEND yet.
2556 Op
= DAG
.getNode(ISD::VP_ZERO_EXTEND
, dl
, VT
, Op
, N
->getOperand(1),
2558 return DAG
.getVPZeroExtendInReg(Op
, N
->getOperand(1), N
->getOperand(2), dl
,
2559 N
->getOperand(0).getValueType());
2562 SDValue
DAGTypeLegalizer::PromoteIntOp_FIX(SDNode
*N
) {
2563 SDValue Op2
= ZExtPromotedInteger(N
->getOperand(2));
2565 DAG
.UpdateNodeOperands(N
, N
->getOperand(0), N
->getOperand(1), Op2
), 0);
2568 SDValue
DAGTypeLegalizer::PromoteIntOp_FRAMERETURNADDR(SDNode
*N
) {
2569 // Promote the RETURNADDR/FRAMEADDR argument to a supported integer width.
2570 SDValue Op
= ZExtPromotedInteger(N
->getOperand(0));
2571 return SDValue(DAG
.UpdateNodeOperands(N
, Op
), 0);
2574 SDValue
DAGTypeLegalizer::PromoteIntOp_ExpOp(SDNode
*N
) {
2575 bool IsStrict
= N
->isStrictFPOpcode();
2576 SDValue Chain
= IsStrict
? N
->getOperand(0) : SDValue();
2579 N
->getOpcode() == ISD::FPOWI
|| N
->getOpcode() == ISD::STRICT_FPOWI
;
2580 unsigned OpOffset
= IsStrict
? 1 : 0;
2582 // The integer operand is the last operand in FPOWI (or FLDEXP) (so the result
2583 // and floating point operand is already type legalized).
2584 RTLIB::Libcall LC
= IsPowI
? RTLIB::getPOWI(N
->getValueType(0))
2585 : RTLIB::getLDEXP(N
->getValueType(0));
2587 if (LC
== RTLIB::UNKNOWN_LIBCALL
|| !TLI
.getLibcallName(LC
)) {
2588 // Scalarize vector FPOWI instead of promoting the type. This allows the
2589 // scalar FPOWIs to be visited and converted to libcalls before promoting
2591 // FIXME: This should be done in LegalizeVectorOps/LegalizeDAG, but call
2592 // lowering needs the unpromoted EVT.
2593 if (IsPowI
&& N
->getValueType(0).isVector())
2594 return DAG
.UnrollVectorOp(N
);
2595 SmallVector
<SDValue
, 3> NewOps(N
->ops());
2596 NewOps
[1 + OpOffset
] = SExtPromotedInteger(N
->getOperand(1 + OpOffset
));
2597 return SDValue(DAG
.UpdateNodeOperands(N
, NewOps
), 0);
2600 // We can't just promote the exponent type in FPOWI, since we want to lower
2601 // the node to a libcall and we if we promote to a type larger than
2602 // sizeof(int) the libcall might not be according to the targets ABI. Instead
2603 // we rewrite to a libcall here directly, letting makeLibCall handle promotion
2604 // if the target accepts it according to shouldSignExtendTypeInLibCall.
2606 // The exponent should fit in a sizeof(int) type for the libcall to be valid.
2607 assert(DAG
.getLibInfo().getIntSize() ==
2608 N
->getOperand(1 + OpOffset
).getValueType().getSizeInBits() &&
2609 "POWI exponent should match with sizeof(int) when doing the libcall.");
2610 TargetLowering::MakeLibCallOptions CallOptions
;
2611 CallOptions
.setIsSigned(true);
2612 SDValue Ops
[2] = {N
->getOperand(0 + OpOffset
), N
->getOperand(1 + OpOffset
)};
2613 std::pair
<SDValue
, SDValue
> Tmp
= TLI
.makeLibCall(
2614 DAG
, LC
, N
->getValueType(0), Ops
, CallOptions
, SDLoc(N
), Chain
);
2615 ReplaceValueWith(SDValue(N
, 0), Tmp
.first
);
2617 ReplaceValueWith(SDValue(N
, 1), Tmp
.second
);
2621 static unsigned getExtendForIntVecReduction(SDNode
*N
) {
2622 switch (N
->getOpcode()) {
2624 llvm_unreachable("Expected integer vector reduction");
2625 case ISD::VECREDUCE_ADD
:
2626 case ISD::VECREDUCE_MUL
:
2627 case ISD::VECREDUCE_AND
:
2628 case ISD::VECREDUCE_OR
:
2629 case ISD::VECREDUCE_XOR
:
2630 case ISD::VP_REDUCE_ADD
:
2631 case ISD::VP_REDUCE_MUL
:
2632 case ISD::VP_REDUCE_AND
:
2633 case ISD::VP_REDUCE_OR
:
2634 case ISD::VP_REDUCE_XOR
:
2635 return ISD::ANY_EXTEND
;
2636 case ISD::VECREDUCE_SMAX
:
2637 case ISD::VECREDUCE_SMIN
:
2638 case ISD::VP_REDUCE_SMAX
:
2639 case ISD::VP_REDUCE_SMIN
:
2640 return ISD::SIGN_EXTEND
;
2641 case ISD::VECREDUCE_UMAX
:
2642 case ISD::VECREDUCE_UMIN
:
2643 case ISD::VP_REDUCE_UMAX
:
2644 case ISD::VP_REDUCE_UMIN
:
2645 return ISD::ZERO_EXTEND
;
2649 SDValue
DAGTypeLegalizer::PromoteIntOpVectorReduction(SDNode
*N
, SDValue V
) {
2650 switch (getExtendForIntVecReduction(N
)) {
2652 llvm_unreachable("Impossible extension kind for integer reduction");
2653 case ISD::ANY_EXTEND
:
2654 return GetPromotedInteger(V
);
2655 case ISD::SIGN_EXTEND
:
2656 return SExtPromotedInteger(V
);
2657 case ISD::ZERO_EXTEND
:
2658 return ZExtPromotedInteger(V
);
2662 SDValue
DAGTypeLegalizer::PromoteIntOp_VECREDUCE(SDNode
*N
) {
2664 SDValue Op
= PromoteIntOpVectorReduction(N
, N
->getOperand(0));
2666 EVT OrigEltVT
= N
->getOperand(0).getValueType().getVectorElementType();
2667 EVT InVT
= Op
.getValueType();
2668 EVT EltVT
= InVT
.getVectorElementType();
2669 EVT ResVT
= N
->getValueType(0);
2670 unsigned Opcode
= N
->getOpcode();
2672 // An i1 vecreduce_xor is equivalent to vecreduce_add, use that instead if
2673 // vecreduce_xor is not legal
2674 if (Opcode
== ISD::VECREDUCE_XOR
&& OrigEltVT
== MVT::i1
&&
2675 !TLI
.isOperationLegalOrCustom(ISD::VECREDUCE_XOR
, InVT
) &&
2676 TLI
.isOperationLegalOrCustom(ISD::VECREDUCE_ADD
, InVT
))
2677 Opcode
= ISD::VECREDUCE_ADD
;
2679 // An i1 vecreduce_or is equivalent to vecreduce_umax, use that instead if
2680 // vecreduce_or is not legal
2681 else if (Opcode
== ISD::VECREDUCE_OR
&& OrigEltVT
== MVT::i1
&&
2682 !TLI
.isOperationLegalOrCustom(ISD::VECREDUCE_OR
, InVT
) &&
2683 TLI
.isOperationLegalOrCustom(ISD::VECREDUCE_UMAX
, InVT
)) {
2684 Opcode
= ISD::VECREDUCE_UMAX
;
2685 // Can't use promoteTargetBoolean here because we still need
2686 // to either sign_ext or zero_ext in the undefined case.
2687 switch (TLI
.getBooleanContents(InVT
)) {
2688 case TargetLoweringBase::UndefinedBooleanContent
:
2689 case TargetLoweringBase::ZeroOrOneBooleanContent
:
2690 Op
= ZExtPromotedInteger(N
->getOperand(0));
2692 case TargetLoweringBase::ZeroOrNegativeOneBooleanContent
:
2693 Op
= SExtPromotedInteger(N
->getOperand(0));
2698 // An i1 vecreduce_and is equivalent to vecreduce_umin, use that instead if
2699 // vecreduce_and is not legal
2700 else if (Opcode
== ISD::VECREDUCE_AND
&& OrigEltVT
== MVT::i1
&&
2701 !TLI
.isOperationLegalOrCustom(ISD::VECREDUCE_AND
, InVT
) &&
2702 TLI
.isOperationLegalOrCustom(ISD::VECREDUCE_UMIN
, InVT
)) {
2703 Opcode
= ISD::VECREDUCE_UMIN
;
2704 // Can't use promoteTargetBoolean here because we still need
2705 // to either sign_ext or zero_ext in the undefined case.
2706 switch (TLI
.getBooleanContents(InVT
)) {
2707 case TargetLoweringBase::UndefinedBooleanContent
:
2708 case TargetLoweringBase::ZeroOrOneBooleanContent
:
2709 Op
= ZExtPromotedInteger(N
->getOperand(0));
2711 case TargetLoweringBase::ZeroOrNegativeOneBooleanContent
:
2712 Op
= SExtPromotedInteger(N
->getOperand(0));
2717 if (ResVT
.bitsGE(EltVT
))
2718 return DAG
.getNode(Opcode
, SDLoc(N
), ResVT
, Op
);
2720 // Result size must be >= element size. If this is not the case after
2721 // promotion, also promote the result type and then truncate.
2722 SDValue Reduce
= DAG
.getNode(Opcode
, dl
, EltVT
, Op
);
2723 return DAG
.getNode(ISD::TRUNCATE
, dl
, ResVT
, Reduce
);
2726 SDValue
DAGTypeLegalizer::PromoteIntOp_VP_REDUCE(SDNode
*N
, unsigned OpNo
) {
2728 SDValue Op
= N
->getOperand(OpNo
);
2729 SmallVector
<SDValue
, 4> NewOps(N
->ops());
2731 if (OpNo
== 2) { // Mask
2733 NewOps
[2] = PromoteTargetBoolean(Op
, N
->getOperand(1).getValueType());
2734 return SDValue(DAG
.UpdateNodeOperands(N
, NewOps
), 0);
2737 assert(OpNo
== 1 && "Unexpected operand for promotion");
2739 Op
= PromoteIntOpVectorReduction(N
, Op
);
2743 EVT VT
= N
->getValueType(0);
2744 EVT EltVT
= Op
.getValueType().getScalarType();
2746 if (VT
.bitsGE(EltVT
))
2747 return DAG
.getNode(N
->getOpcode(), SDLoc(N
), VT
, NewOps
);
2749 // Result size must be >= element/start-value size. If this is not the case
2750 // after promotion, also promote both the start value and result type and
2753 DAG
.getNode(getExtendForIntVecReduction(N
), DL
, EltVT
, N
->getOperand(0));
2754 SDValue Reduce
= DAG
.getNode(N
->getOpcode(), DL
, EltVT
, NewOps
);
2755 return DAG
.getNode(ISD::TRUNCATE
, DL
, VT
, Reduce
);
2758 SDValue
DAGTypeLegalizer::PromoteIntOp_SET_ROUNDING(SDNode
*N
) {
2759 SDValue Op
= ZExtPromotedInteger(N
->getOperand(1));
2760 return SDValue(DAG
.UpdateNodeOperands(N
, N
->getOperand(0), Op
), 0);
2763 SDValue
DAGTypeLegalizer::PromoteIntOp_STACKMAP(SDNode
*N
, unsigned OpNo
) {
2764 assert(OpNo
> 1); // Because the first two arguments are guaranteed legal.
2765 SmallVector
<SDValue
> NewOps(N
->ops());
2766 SDValue Operand
= N
->getOperand(OpNo
);
2767 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), Operand
.getValueType());
2768 NewOps
[OpNo
] = DAG
.getNode(ISD::ANY_EXTEND
, SDLoc(N
), NVT
, Operand
);
2769 return SDValue(DAG
.UpdateNodeOperands(N
, NewOps
), 0);
2772 SDValue
DAGTypeLegalizer::PromoteIntOp_PATCHPOINT(SDNode
*N
, unsigned OpNo
) {
2774 SmallVector
<SDValue
> NewOps(N
->ops());
2775 SDValue Operand
= N
->getOperand(OpNo
);
2776 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), Operand
.getValueType());
2777 NewOps
[OpNo
] = DAG
.getNode(ISD::ANY_EXTEND
, SDLoc(N
), NVT
, Operand
);
2778 return SDValue(DAG
.UpdateNodeOperands(N
, NewOps
), 0);
2781 SDValue
DAGTypeLegalizer::PromoteIntOp_VP_STRIDED(SDNode
*N
, unsigned OpNo
) {
2782 assert((N
->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD
&& OpNo
== 3) ||
2783 (N
->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE
&& OpNo
== 4));
2785 SmallVector
<SDValue
, 8> NewOps(N
->ops());
2786 NewOps
[OpNo
] = SExtPromotedInteger(N
->getOperand(OpNo
));
2788 return SDValue(DAG
.UpdateNodeOperands(N
, NewOps
), 0);
2791 SDValue
DAGTypeLegalizer::PromoteIntOp_VP_SPLICE(SDNode
*N
, unsigned OpNo
) {
2792 SmallVector
<SDValue
, 6> NewOps(N
->ops());
2794 if (OpNo
== 2) { // Offset operand
2795 NewOps
[OpNo
] = SExtPromotedInteger(N
->getOperand(OpNo
));
2796 return SDValue(DAG
.UpdateNodeOperands(N
, NewOps
), 0);
2799 assert((OpNo
== 4 || OpNo
== 5) && "Unexpected operand for promotion");
2801 NewOps
[OpNo
] = ZExtPromotedInteger(N
->getOperand(OpNo
));
2802 return SDValue(DAG
.UpdateNodeOperands(N
, NewOps
), 0);
2805 SDValue
DAGTypeLegalizer::PromoteIntOp_VECTOR_HISTOGRAM(SDNode
*N
,
2807 assert(OpNo
== 1 && "Unexpected operand for promotion");
2808 SmallVector
<SDValue
, 7> NewOps(N
->ops());
2809 NewOps
[1] = GetPromotedInteger(N
->getOperand(1));
2810 return SDValue(DAG
.UpdateNodeOperands(N
, NewOps
), 0);
2813 //===----------------------------------------------------------------------===//
2814 // Integer Result Expansion
2815 //===----------------------------------------------------------------------===//
2817 /// ExpandIntegerResult - This method is called when the specified result of the
2818 /// specified node is found to need expansion. At this point, the node may also
2819 /// have invalid operands or may have other results that need promotion, we just
2820 /// know that (at least) one result needs expansion.
2821 void DAGTypeLegalizer::ExpandIntegerResult(SDNode
*N
, unsigned ResNo
) {
2822 LLVM_DEBUG(dbgs() << "Expand integer result: "; N
->dump(&DAG
));
2824 Lo
= Hi
= SDValue();
2826 // See if the target wants to custom expand this node.
2827 if (CustomLowerNode(N
, N
->getValueType(ResNo
), true))
2830 switch (N
->getOpcode()) {
2833 dbgs() << "ExpandIntegerResult #" << ResNo
<< ": ";
2834 N
->dump(&DAG
); dbgs() << "\n";
2836 report_fatal_error("Do not know how to expand the result of this "
2839 case ISD::ARITH_FENCE
: SplitRes_ARITH_FENCE(N
, Lo
, Hi
); break;
2840 case ISD::MERGE_VALUES
: SplitRes_MERGE_VALUES(N
, ResNo
, Lo
, Hi
); break;
2841 case ISD::SELECT
: SplitRes_Select(N
, Lo
, Hi
); break;
2842 case ISD::SELECT_CC
: SplitRes_SELECT_CC(N
, Lo
, Hi
); break;
2843 case ISD::UNDEF
: SplitRes_UNDEF(N
, Lo
, Hi
); break;
2844 case ISD::FREEZE
: SplitRes_FREEZE(N
, Lo
, Hi
); break;
2845 case ISD::SETCC
: ExpandIntRes_SETCC(N
, Lo
, Hi
); break;
2847 case ISD::BITCAST
: ExpandRes_BITCAST(N
, Lo
, Hi
); break;
2848 case ISD::BUILD_PAIR
: ExpandRes_BUILD_PAIR(N
, Lo
, Hi
); break;
2849 case ISD::EXTRACT_ELEMENT
: ExpandRes_EXTRACT_ELEMENT(N
, Lo
, Hi
); break;
2850 case ISD::EXTRACT_VECTOR_ELT
: ExpandRes_EXTRACT_VECTOR_ELT(N
, Lo
, Hi
); break;
2851 case ISD::VAARG
: ExpandRes_VAARG(N
, Lo
, Hi
); break;
2853 case ISD::ANY_EXTEND
: ExpandIntRes_ANY_EXTEND(N
, Lo
, Hi
); break;
2854 case ISD::AssertSext
: ExpandIntRes_AssertSext(N
, Lo
, Hi
); break;
2855 case ISD::AssertZext
: ExpandIntRes_AssertZext(N
, Lo
, Hi
); break;
2856 case ISD::BITREVERSE
: ExpandIntRes_BITREVERSE(N
, Lo
, Hi
); break;
2857 case ISD::BSWAP
: ExpandIntRes_BSWAP(N
, Lo
, Hi
); break;
2858 case ISD::PARITY
: ExpandIntRes_PARITY(N
, Lo
, Hi
); break;
2859 case ISD::Constant
: ExpandIntRes_Constant(N
, Lo
, Hi
); break;
2860 case ISD::ABS
: ExpandIntRes_ABS(N
, Lo
, Hi
); break;
2862 case ISD::ABDU
: ExpandIntRes_ABD(N
, Lo
, Hi
); break;
2863 case ISD::CTLZ_ZERO_UNDEF
:
2864 case ISD::CTLZ
: ExpandIntRes_CTLZ(N
, Lo
, Hi
); break;
2865 case ISD::CTPOP
: ExpandIntRes_CTPOP(N
, Lo
, Hi
); break;
2866 case ISD::CTTZ_ZERO_UNDEF
:
2867 case ISD::CTTZ
: ExpandIntRes_CTTZ(N
, Lo
, Hi
); break;
2868 case ISD::GET_ROUNDING
:ExpandIntRes_GET_ROUNDING(N
, Lo
, Hi
); break;
2869 case ISD::STRICT_FP_TO_SINT
:
2870 case ISD::FP_TO_SINT
:
2871 case ISD::STRICT_FP_TO_UINT
:
2872 case ISD::FP_TO_UINT
: ExpandIntRes_FP_TO_XINT(N
, Lo
, Hi
); break;
2873 case ISD::FP_TO_SINT_SAT
:
2874 case ISD::FP_TO_UINT_SAT
: ExpandIntRes_FP_TO_XINT_SAT(N
, Lo
, Hi
); break;
2875 case ISD::STRICT_LROUND
:
2876 case ISD::STRICT_LRINT
:
2879 case ISD::STRICT_LLROUND
:
2880 case ISD::STRICT_LLRINT
:
2882 case ISD::LLRINT
: ExpandIntRes_XROUND_XRINT(N
, Lo
, Hi
); break;
2883 case ISD::LOAD
: ExpandIntRes_LOAD(cast
<LoadSDNode
>(N
), Lo
, Hi
); break;
2884 case ISD::MUL
: ExpandIntRes_MUL(N
, Lo
, Hi
); break;
2885 case ISD::READCYCLECOUNTER
:
2886 case ISD::READSTEADYCOUNTER
: ExpandIntRes_READCOUNTER(N
, Lo
, Hi
); break;
2887 case ISD::SDIV
: ExpandIntRes_SDIV(N
, Lo
, Hi
); break;
2888 case ISD::SIGN_EXTEND
: ExpandIntRes_SIGN_EXTEND(N
, Lo
, Hi
); break;
2889 case ISD::SIGN_EXTEND_INREG
: ExpandIntRes_SIGN_EXTEND_INREG(N
, Lo
, Hi
); break;
2890 case ISD::SREM
: ExpandIntRes_SREM(N
, Lo
, Hi
); break;
2891 case ISD::TRUNCATE
: ExpandIntRes_TRUNCATE(N
, Lo
, Hi
); break;
2892 case ISD::UDIV
: ExpandIntRes_UDIV(N
, Lo
, Hi
); break;
2893 case ISD::UREM
: ExpandIntRes_UREM(N
, Lo
, Hi
); break;
2894 case ISD::ZERO_EXTEND
: ExpandIntRes_ZERO_EXTEND(N
, Lo
, Hi
); break;
2895 case ISD::ATOMIC_LOAD
: ExpandIntRes_ATOMIC_LOAD(N
, Lo
, Hi
); break;
2897 case ISD::ATOMIC_LOAD_ADD
:
2898 case ISD::ATOMIC_LOAD_SUB
:
2899 case ISD::ATOMIC_LOAD_AND
:
2900 case ISD::ATOMIC_LOAD_CLR
:
2901 case ISD::ATOMIC_LOAD_OR
:
2902 case ISD::ATOMIC_LOAD_XOR
:
2903 case ISD::ATOMIC_LOAD_NAND
:
2904 case ISD::ATOMIC_LOAD_MIN
:
2905 case ISD::ATOMIC_LOAD_MAX
:
2906 case ISD::ATOMIC_LOAD_UMIN
:
2907 case ISD::ATOMIC_LOAD_UMAX
:
2908 case ISD::ATOMIC_SWAP
:
2909 case ISD::ATOMIC_CMP_SWAP
: {
2910 std::pair
<SDValue
, SDValue
> Tmp
= ExpandAtomic(N
);
2911 SplitInteger(Tmp
.first
, Lo
, Hi
);
2912 ReplaceValueWith(SDValue(N
, 1), Tmp
.second
);
2915 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS
: {
2916 AtomicSDNode
*AN
= cast
<AtomicSDNode
>(N
);
2917 SDVTList VTs
= DAG
.getVTList(N
->getValueType(0), MVT::Other
);
2918 SDValue Tmp
= DAG
.getAtomicCmpSwap(
2919 ISD::ATOMIC_CMP_SWAP
, SDLoc(N
), AN
->getMemoryVT(), VTs
,
2920 N
->getOperand(0), N
->getOperand(1), N
->getOperand(2), N
->getOperand(3),
2921 AN
->getMemOperand());
2923 // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
2924 // success simply by comparing the loaded value against the ingoing
2926 SDValue Success
= DAG
.getSetCC(SDLoc(N
), N
->getValueType(1), Tmp
,
2927 N
->getOperand(2), ISD::SETEQ
);
2929 SplitInteger(Tmp
, Lo
, Hi
);
2930 ReplaceValueWith(SDValue(N
, 1), Success
);
2931 ReplaceValueWith(SDValue(N
, 2), Tmp
.getValue(1));
2937 case ISD::XOR
: ExpandIntRes_Logical(N
, Lo
, Hi
); break;
2942 case ISD::SMIN
: ExpandIntRes_MINMAX(N
, Lo
, Hi
); break;
2945 case ISD::UCMP
: ExpandIntRes_CMP(N
, Lo
, Hi
); break;
2948 case ISD::SUB
: ExpandIntRes_ADDSUB(N
, Lo
, Hi
); break;
2951 case ISD::SUBC
: ExpandIntRes_ADDSUBC(N
, Lo
, Hi
); break;
2954 case ISD::SUBE
: ExpandIntRes_ADDSUBE(N
, Lo
, Hi
); break;
2956 case ISD::UADDO_CARRY
:
2957 case ISD::USUBO_CARRY
: ExpandIntRes_UADDSUBO_CARRY(N
, Lo
, Hi
); break;
2959 case ISD::SADDO_CARRY
:
2960 case ISD::SSUBO_CARRY
: ExpandIntRes_SADDSUBO_CARRY(N
, Lo
, Hi
); break;
2964 case ISD::SRL
: ExpandIntRes_Shift(N
, Lo
, Hi
); break;
2967 case ISD::SSUBO
: ExpandIntRes_SADDSUBO(N
, Lo
, Hi
); break;
2969 case ISD::USUBO
: ExpandIntRes_UADDSUBO(N
, Lo
, Hi
); break;
2971 case ISD::SMULO
: ExpandIntRes_XMULO(N
, Lo
, Hi
); break;
2976 case ISD::USUBSAT
: ExpandIntRes_ADDSUBSAT(N
, Lo
, Hi
); break;
2979 case ISD::USHLSAT
: ExpandIntRes_SHLSAT(N
, Lo
, Hi
); break;
2983 case ISD::AVGFLOORS
:
2984 case ISD::AVGFLOORU
: ExpandIntRes_AVG(N
, Lo
, Hi
); break;
2987 case ISD::SMULFIXSAT
:
2989 case ISD::UMULFIXSAT
: ExpandIntRes_MULFIX(N
, Lo
, Hi
); break;
2992 case ISD::SDIVFIXSAT
:
2994 case ISD::UDIVFIXSAT
: ExpandIntRes_DIVFIX(N
, Lo
, Hi
); break;
2996 case ISD::VECREDUCE_ADD
:
2997 case ISD::VECREDUCE_MUL
:
2998 case ISD::VECREDUCE_AND
:
2999 case ISD::VECREDUCE_OR
:
3000 case ISD::VECREDUCE_XOR
:
3001 case ISD::VECREDUCE_SMAX
:
3002 case ISD::VECREDUCE_SMIN
:
3003 case ISD::VECREDUCE_UMAX
:
3004 case ISD::VECREDUCE_UMIN
: ExpandIntRes_VECREDUCE(N
, Lo
, Hi
); break;
3008 ExpandIntRes_Rotate(N
, Lo
, Hi
);
3013 ExpandIntRes_FunnelShift(N
, Lo
, Hi
);
3017 ExpandIntRes_VSCALE(N
, Lo
, Hi
);
3021 // If Lo/Hi is null, the sub-method took care of registering results etc.
3023 SetExpandedInteger(SDValue(N
, ResNo
), Lo
, Hi
);
3026 /// Lower an atomic node to the appropriate builtin call.
3027 std::pair
<SDValue
, SDValue
> DAGTypeLegalizer::ExpandAtomic(SDNode
*Node
) {
3028 unsigned Opc
= Node
->getOpcode();
3029 MVT VT
= cast
<AtomicSDNode
>(Node
)->getMemoryVT().getSimpleVT();
3030 AtomicOrdering order
= cast
<AtomicSDNode
>(Node
)->getMergedOrdering();
3031 // Lower to outline atomic libcall if outline atomics enabled,
3032 // or to sync libcall otherwise
3033 RTLIB::Libcall LC
= RTLIB::getOUTLINE_ATOMIC(Opc
, order
, VT
);
3034 EVT RetVT
= Node
->getValueType(0);
3035 TargetLowering::MakeLibCallOptions CallOptions
;
3036 SmallVector
<SDValue
, 4> Ops
;
3037 if (TLI
.getLibcallName(LC
)) {
3038 Ops
.append(Node
->op_begin() + 2, Node
->op_end());
3039 Ops
.push_back(Node
->getOperand(1));
3041 LC
= RTLIB::getSYNC(Opc
, VT
);
3042 assert(LC
!= RTLIB::UNKNOWN_LIBCALL
&&
3043 "Unexpected atomic op or value type!");
3044 Ops
.append(Node
->op_begin() + 1, Node
->op_end());
3046 return TLI
.makeLibCall(DAG
, LC
, RetVT
, Ops
, CallOptions
, SDLoc(Node
),
3047 Node
->getOperand(0));
3050 /// N is a shift by a value that needs to be expanded,
3051 /// and the shift amount is a constant 'Amt'. Expand the operation.
3052 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode
*N
, const APInt
&Amt
,
3053 SDValue
&Lo
, SDValue
&Hi
) {
3055 // Expand the incoming operand to be shifted, so that we have its parts
3057 GetExpandedInteger(N
->getOperand(0), InL
, InH
);
3059 // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
3060 // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
3067 EVT NVT
= InL
.getValueType();
3068 unsigned VTBits
= N
->getValueType(0).getSizeInBits();
3069 unsigned NVTBits
= NVT
.getSizeInBits();
3071 if (N
->getOpcode() == ISD::SHL
) {
3072 if (Amt
.uge(VTBits
)) {
3073 Lo
= Hi
= DAG
.getConstant(0, DL
, NVT
);
3074 } else if (Amt
.ugt(NVTBits
)) {
3075 Lo
= DAG
.getConstant(0, DL
, NVT
);
3076 Hi
= DAG
.getNode(ISD::SHL
, DL
, NVT
, InL
,
3077 DAG
.getShiftAmountConstant(Amt
- NVTBits
, NVT
, DL
));
3078 } else if (Amt
== NVTBits
) {
3079 Lo
= DAG
.getConstant(0, DL
, NVT
);
3082 Lo
= DAG
.getNode(ISD::SHL
, DL
, NVT
, InL
,
3083 DAG
.getShiftAmountConstant(Amt
, NVT
, DL
));
3086 DAG
.getNode(ISD::SHL
, DL
, NVT
, InH
,
3087 DAG
.getShiftAmountConstant(Amt
, NVT
, DL
)),
3088 DAG
.getNode(ISD::SRL
, DL
, NVT
, InL
,
3089 DAG
.getShiftAmountConstant(-Amt
+ NVTBits
, NVT
, DL
)));
3094 if (N
->getOpcode() == ISD::SRL
) {
3095 if (Amt
.uge(VTBits
)) {
3096 Lo
= Hi
= DAG
.getConstant(0, DL
, NVT
);
3097 } else if (Amt
.ugt(NVTBits
)) {
3098 Lo
= DAG
.getNode(ISD::SRL
, DL
, NVT
, InH
,
3099 DAG
.getShiftAmountConstant(Amt
- NVTBits
, NVT
, DL
));
3100 Hi
= DAG
.getConstant(0, DL
, NVT
);
3101 } else if (Amt
== NVTBits
) {
3103 Hi
= DAG
.getConstant(0, DL
, NVT
);
3107 DAG
.getNode(ISD::SRL
, DL
, NVT
, InL
,
3108 DAG
.getShiftAmountConstant(Amt
, NVT
, DL
)),
3109 DAG
.getNode(ISD::SHL
, DL
, NVT
, InH
,
3110 DAG
.getShiftAmountConstant(-Amt
+ NVTBits
, NVT
, DL
)));
3111 Hi
= DAG
.getNode(ISD::SRL
, DL
, NVT
, InH
,
3112 DAG
.getShiftAmountConstant(Amt
, NVT
, DL
));
3117 assert(N
->getOpcode() == ISD::SRA
&& "Unknown shift!");
3118 if (Amt
.uge(VTBits
)) {
3119 Hi
= Lo
= DAG
.getNode(ISD::SRA
, DL
, NVT
, InH
,
3120 DAG
.getShiftAmountConstant(NVTBits
- 1, NVT
, DL
));
3121 } else if (Amt
.ugt(NVTBits
)) {
3122 Lo
= DAG
.getNode(ISD::SRA
, DL
, NVT
, InH
,
3123 DAG
.getShiftAmountConstant(Amt
- NVTBits
, NVT
, DL
));
3124 Hi
= DAG
.getNode(ISD::SRA
, DL
, NVT
, InH
,
3125 DAG
.getShiftAmountConstant(NVTBits
- 1, NVT
, DL
));
3126 } else if (Amt
== NVTBits
) {
3128 Hi
= DAG
.getNode(ISD::SRA
, DL
, NVT
, InH
,
3129 DAG
.getShiftAmountConstant(NVTBits
- 1, NVT
, DL
));
3133 DAG
.getNode(ISD::SRL
, DL
, NVT
, InL
,
3134 DAG
.getShiftAmountConstant(Amt
, NVT
, DL
)),
3135 DAG
.getNode(ISD::SHL
, DL
, NVT
, InH
,
3136 DAG
.getShiftAmountConstant(-Amt
+ NVTBits
, NVT
, DL
)));
3137 Hi
= DAG
.getNode(ISD::SRA
, DL
, NVT
, InH
,
3138 DAG
.getShiftAmountConstant(Amt
, NVT
, DL
));
3142 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
3143 /// this shift based on knowledge of the high bit of the shift amount. If we
3144 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
3146 bool DAGTypeLegalizer::
3147 ExpandShiftWithKnownAmountBit(SDNode
*N
, SDValue
&Lo
, SDValue
&Hi
) {
3148 unsigned Opc
= N
->getOpcode();
3149 SDValue In
= N
->getOperand(0);
3150 SDValue Amt
= N
->getOperand(1);
3151 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
3152 EVT ShTy
= Amt
.getValueType();
3153 unsigned ShBits
= ShTy
.getScalarSizeInBits();
3154 unsigned NVTBits
= NVT
.getScalarSizeInBits();
3155 assert(isPowerOf2_32(NVTBits
) &&
3156 "Expanded integer type size not a power of two!");
3159 APInt HighBitMask
= APInt::getHighBitsSet(ShBits
, ShBits
- Log2_32(NVTBits
));
3160 KnownBits Known
= DAG
.computeKnownBits(Amt
);
3162 // If we don't know anything about the high bits, exit.
3163 if (((Known
.Zero
| Known
.One
) & HighBitMask
) == 0)
3166 // Get the incoming operand to be shifted.
3168 GetExpandedInteger(In
, InL
, InH
);
3170 // If we know that any of the high bits of the shift amount are one, then we
3171 // can do this as a couple of simple shifts.
3172 if (Known
.One
.intersects(HighBitMask
)) {
3173 // Mask out the high bit, which we know is set.
3174 Amt
= DAG
.getNode(ISD::AND
, dl
, ShTy
, Amt
,
3175 DAG
.getConstant(~HighBitMask
, dl
, ShTy
));
3178 default: llvm_unreachable("Unknown shift");
3180 Lo
= DAG
.getConstant(0, dl
, NVT
); // Low part is zero.
3181 Hi
= DAG
.getNode(ISD::SHL
, dl
, NVT
, InL
, Amt
); // High part from Lo part.
3184 Hi
= DAG
.getConstant(0, dl
, NVT
); // Hi part is zero.
3185 Lo
= DAG
.getNode(ISD::SRL
, dl
, NVT
, InH
, Amt
); // Lo part from Hi part.
3188 Hi
= DAG
.getNode(ISD::SRA
, dl
, NVT
, InH
, // Sign extend high part.
3189 DAG
.getConstant(NVTBits
- 1, dl
, ShTy
));
3190 Lo
= DAG
.getNode(ISD::SRA
, dl
, NVT
, InH
, Amt
); // Lo part from Hi part.
3195 // If we know that all of the high bits of the shift amount are zero, then we
3196 // can do this as a couple of simple shifts.
3197 if (HighBitMask
.isSubsetOf(Known
.Zero
)) {
3198 // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
3199 // shift if x is zero. We can use XOR here because x is known to be smaller
3201 SDValue Amt2
= DAG
.getNode(ISD::XOR
, dl
, ShTy
, Amt
,
3202 DAG
.getConstant(NVTBits
- 1, dl
, ShTy
));
3206 default: llvm_unreachable("Unknown shift");
3207 case ISD::SHL
: Op1
= ISD::SHL
; Op2
= ISD::SRL
; break;
3209 case ISD::SRA
: Op1
= ISD::SRL
; Op2
= ISD::SHL
; break;
3212 // When shifting right the arithmetic for Lo and Hi is swapped.
3213 if (Opc
!= ISD::SHL
)
3214 std::swap(InL
, InH
);
3216 // Use a little trick to get the bits that move from Lo to Hi. First
3217 // shift by one bit.
3218 SDValue Sh1
= DAG
.getNode(Op2
, dl
, NVT
, InL
, DAG
.getConstant(1, dl
, ShTy
));
3219 // Then compute the remaining shift with amount-1.
3220 SDValue Sh2
= DAG
.getNode(Op2
, dl
, NVT
, Sh1
, Amt2
);
3222 Lo
= DAG
.getNode(Opc
, dl
, NVT
, InL
, Amt
);
3223 Hi
= DAG
.getNode(ISD::OR
, dl
, NVT
, DAG
.getNode(Op1
, dl
, NVT
, InH
, Amt
),Sh2
);
3225 if (Opc
!= ISD::SHL
)
3233 /// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
3235 bool DAGTypeLegalizer::
3236 ExpandShiftWithUnknownAmountBit(SDNode
*N
, SDValue
&Lo
, SDValue
&Hi
) {
3237 SDValue Amt
= N
->getOperand(1);
3238 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
3239 EVT ShTy
= Amt
.getValueType();
3240 unsigned NVTBits
= NVT
.getSizeInBits();
3241 assert(isPowerOf2_32(NVTBits
) &&
3242 "Expanded integer type size not a power of two!");
3245 // Get the incoming operand to be shifted.
3247 GetExpandedInteger(N
->getOperand(0), InL
, InH
);
3249 SDValue NVBitsNode
= DAG
.getConstant(NVTBits
, dl
, ShTy
);
3250 SDValue AmtExcess
= DAG
.getNode(ISD::SUB
, dl
, ShTy
, Amt
, NVBitsNode
);
3251 SDValue AmtLack
= DAG
.getNode(ISD::SUB
, dl
, ShTy
, NVBitsNode
, Amt
);
3252 SDValue isShort
= DAG
.getSetCC(dl
, getSetCCResultType(ShTy
),
3253 Amt
, NVBitsNode
, ISD::SETULT
);
3254 SDValue isZero
= DAG
.getSetCC(dl
, getSetCCResultType(ShTy
),
3255 Amt
, DAG
.getConstant(0, dl
, ShTy
),
3258 SDValue LoS
, HiS
, LoL
, HiL
;
3259 switch (N
->getOpcode()) {
3260 default: llvm_unreachable("Unknown shift");
3262 // Short: ShAmt < NVTBits
3263 LoS
= DAG
.getNode(ISD::SHL
, dl
, NVT
, InL
, Amt
);
3264 HiS
= DAG
.getNode(ISD::OR
, dl
, NVT
,
3265 DAG
.getNode(ISD::SHL
, dl
, NVT
, InH
, Amt
),
3266 DAG
.getNode(ISD::SRL
, dl
, NVT
, InL
, AmtLack
));
3268 // Long: ShAmt >= NVTBits
3269 LoL
= DAG
.getConstant(0, dl
, NVT
); // Lo part is zero.
3270 HiL
= DAG
.getNode(ISD::SHL
, dl
, NVT
, InL
, AmtExcess
); // Hi from Lo part.
3272 Lo
= DAG
.getSelect(dl
, NVT
, isShort
, LoS
, LoL
);
3273 Hi
= DAG
.getSelect(dl
, NVT
, isZero
, InH
,
3274 DAG
.getSelect(dl
, NVT
, isShort
, HiS
, HiL
));
3277 // Short: ShAmt < NVTBits
3278 HiS
= DAG
.getNode(ISD::SRL
, dl
, NVT
, InH
, Amt
);
3279 LoS
= DAG
.getNode(ISD::OR
, dl
, NVT
,
3280 DAG
.getNode(ISD::SRL
, dl
, NVT
, InL
, Amt
),
3281 // FIXME: If Amt is zero, the following shift generates an undefined result
3282 // on some architectures.
3283 DAG
.getNode(ISD::SHL
, dl
, NVT
, InH
, AmtLack
));
3285 // Long: ShAmt >= NVTBits
3286 HiL
= DAG
.getConstant(0, dl
, NVT
); // Hi part is zero.
3287 LoL
= DAG
.getNode(ISD::SRL
, dl
, NVT
, InH
, AmtExcess
); // Lo from Hi part.
3289 Lo
= DAG
.getSelect(dl
, NVT
, isZero
, InL
,
3290 DAG
.getSelect(dl
, NVT
, isShort
, LoS
, LoL
));
3291 Hi
= DAG
.getSelect(dl
, NVT
, isShort
, HiS
, HiL
);
3294 // Short: ShAmt < NVTBits
3295 HiS
= DAG
.getNode(ISD::SRA
, dl
, NVT
, InH
, Amt
);
3296 LoS
= DAG
.getNode(ISD::OR
, dl
, NVT
,
3297 DAG
.getNode(ISD::SRL
, dl
, NVT
, InL
, Amt
),
3298 DAG
.getNode(ISD::SHL
, dl
, NVT
, InH
, AmtLack
));
3300 // Long: ShAmt >= NVTBits
3301 HiL
= DAG
.getNode(ISD::SRA
, dl
, NVT
, InH
, // Sign of Hi part.
3302 DAG
.getConstant(NVTBits
- 1, dl
, ShTy
));
3303 LoL
= DAG
.getNode(ISD::SRA
, dl
, NVT
, InH
, AmtExcess
); // Lo from Hi part.
3305 Lo
= DAG
.getSelect(dl
, NVT
, isZero
, InL
,
3306 DAG
.getSelect(dl
, NVT
, isShort
, LoS
, LoL
));
3307 Hi
= DAG
.getSelect(dl
, NVT
, isShort
, HiS
, HiL
);
3312 static std::pair
<ISD::CondCode
, ISD::NodeType
> getExpandedMinMaxOps(int Op
) {
3315 default: llvm_unreachable("invalid min/max opcode");
3317 return std::make_pair(ISD::SETGT
, ISD::UMAX
);
3319 return std::make_pair(ISD::SETUGT
, ISD::UMAX
);
3321 return std::make_pair(ISD::SETLT
, ISD::UMIN
);
3323 return std::make_pair(ISD::SETULT
, ISD::UMIN
);
3327 void DAGTypeLegalizer::ExpandIntRes_SETCC(SDNode
*N
, SDValue
&Lo
, SDValue
&Hi
) {
3330 SDValue LHS
= N
->getOperand(0);
3331 SDValue RHS
= N
->getOperand(1);
3332 EVT NewVT
= getSetCCResultType(LHS
.getValueType());
3334 // Taking the same approach as ScalarizeVecRes_SETCC
3335 SDValue Res
= DAG
.getNode(ISD::SETCC
, DL
, NewVT
, LHS
, RHS
, N
->getOperand(2));
3337 Res
= DAG
.getBoolExtOrTrunc(Res
, DL
, N
->getValueType(0), NewVT
);
3338 SplitInteger(Res
, Lo
, Hi
);
3341 void DAGTypeLegalizer::ExpandIntRes_MINMAX(SDNode
*N
,
3342 SDValue
&Lo
, SDValue
&Hi
) {
3345 SDValue LHS
= N
->getOperand(0);
3346 SDValue RHS
= N
->getOperand(1);
3348 // If the upper halves are all sign bits, then we can perform the MINMAX on
3349 // the lower half and sign-extend the result to the upper half.
3350 unsigned NumBits
= N
->getValueType(0).getScalarSizeInBits();
3351 unsigned NumHalfBits
= NumBits
/ 2;
3352 if (DAG
.ComputeNumSignBits(LHS
) > NumHalfBits
&&
3353 DAG
.ComputeNumSignBits(RHS
) > NumHalfBits
) {
3354 SDValue LHSL
, LHSH
, RHSL
, RHSH
;
3355 GetExpandedInteger(LHS
, LHSL
, LHSH
);
3356 GetExpandedInteger(RHS
, RHSL
, RHSH
);
3357 EVT NVT
= LHSL
.getValueType();
3359 Lo
= DAG
.getNode(N
->getOpcode(), DL
, NVT
, LHSL
, RHSL
);
3360 Hi
= DAG
.getNode(ISD::SRA
, DL
, NVT
, Lo
,
3361 DAG
.getShiftAmountConstant(NumHalfBits
- 1, NVT
, DL
));
3365 // The Lo of smin(X, -1) is LHSL if X is negative. Otherwise it's -1.
3366 // The Lo of smax(X, 0) is 0 if X is negative. Otherwise it's LHSL.
3367 if ((N
->getOpcode() == ISD::SMAX
&& isNullConstant(RHS
)) ||
3368 (N
->getOpcode() == ISD::SMIN
&& isAllOnesConstant(RHS
))) {
3369 SDValue LHSL
, LHSH
, RHSL
, RHSH
;
3370 GetExpandedInteger(LHS
, LHSL
, LHSH
);
3371 GetExpandedInteger(RHS
, RHSL
, RHSH
);
3372 EVT NVT
= LHSL
.getValueType();
3373 EVT CCT
= getSetCCResultType(NVT
);
3376 DAG
.getSetCC(DL
, CCT
, LHSH
, DAG
.getConstant(0, DL
, NVT
), ISD::SETLT
);
3377 if (N
->getOpcode() == ISD::SMIN
) {
3378 Lo
= DAG
.getSelect(DL
, NVT
, HiNeg
, LHSL
, DAG
.getAllOnesConstant(DL
, NVT
));
3380 Lo
= DAG
.getSelect(DL
, NVT
, HiNeg
, DAG
.getConstant(0, DL
, NVT
), LHSL
);
3382 Hi
= DAG
.getNode(N
->getOpcode(), DL
, NVT
, {LHSH
, RHSH
});
3386 const APInt
*RHSVal
= nullptr;
3387 if (auto *RHSConst
= dyn_cast
<ConstantSDNode
>(RHS
))
3388 RHSVal
= &RHSConst
->getAPIntValue();
3390 // The high half of MIN/MAX is always just the the MIN/MAX of the
3391 // high halves of the operands. Expand this way if it appears profitable.
3392 if (RHSVal
&& (N
->getOpcode() == ISD::UMIN
|| N
->getOpcode() == ISD::UMAX
) &&
3393 (RHSVal
->countLeadingOnes() >= NumHalfBits
||
3394 RHSVal
->countLeadingZeros() >= NumHalfBits
)) {
3395 SDValue LHSL
, LHSH
, RHSL
, RHSH
;
3396 GetExpandedInteger(LHS
, LHSL
, LHSH
);
3397 GetExpandedInteger(RHS
, RHSL
, RHSH
);
3398 EVT NVT
= LHSL
.getValueType();
3399 EVT CCT
= getSetCCResultType(NVT
);
3401 ISD::NodeType LoOpc
;
3402 ISD::CondCode CondC
;
3403 std::tie(CondC
, LoOpc
) = getExpandedMinMaxOps(N
->getOpcode());
3405 Hi
= DAG
.getNode(N
->getOpcode(), DL
, NVT
, {LHSH
, RHSH
});
3406 // We need to know whether to select Lo part that corresponds to 'winning'
3407 // Hi part or if Hi parts are equal.
3408 SDValue IsHiLeft
= DAG
.getSetCC(DL
, CCT
, LHSH
, RHSH
, CondC
);
3409 SDValue IsHiEq
= DAG
.getSetCC(DL
, CCT
, LHSH
, RHSH
, ISD::SETEQ
);
3411 // Lo part corresponding to the 'winning' Hi part
3412 SDValue LoCmp
= DAG
.getSelect(DL
, NVT
, IsHiLeft
, LHSL
, RHSL
);
3414 // Recursed Lo part if Hi parts are equal, this uses unsigned version
3415 SDValue LoMinMax
= DAG
.getNode(LoOpc
, DL
, NVT
, {LHSL
, RHSL
});
3417 Lo
= DAG
.getSelect(DL
, NVT
, IsHiEq
, LoMinMax
, LoCmp
);
3421 // Expand to "a < b ? a : b" etc. Prefer ge/le if that simplifies
3424 switch (N
->getOpcode()) {
3425 default: llvm_unreachable("How did we get here?");
3427 if (RHSVal
&& RHSVal
->countTrailingZeros() >= NumHalfBits
)
3433 if (RHSVal
&& RHSVal
->countTrailingOnes() >= NumHalfBits
)
3439 if (RHSVal
&& RHSVal
->countTrailingZeros() >= NumHalfBits
)
3445 if (RHSVal
&& RHSVal
->countTrailingOnes() >= NumHalfBits
)
3451 EVT VT
= N
->getValueType(0);
3452 EVT CCT
= getSetCCResultType(VT
);
3453 SDValue Cond
= DAG
.getSetCC(DL
, CCT
, LHS
, RHS
, Pred
);
3454 SDValue Result
= DAG
.getSelect(DL
, VT
, Cond
, LHS
, RHS
);
3455 SplitInteger(Result
, Lo
, Hi
);
3458 void DAGTypeLegalizer::ExpandIntRes_CMP(SDNode
*N
, SDValue
&Lo
, SDValue
&Hi
) {
3459 SDValue ExpandedCMP
= TLI
.expandCMP(N
, DAG
);
3460 SplitInteger(ExpandedCMP
, Lo
, Hi
);
3463 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode
*N
,
3464 SDValue
&Lo
, SDValue
&Hi
) {
3466 // Expand the subcomponents.
3467 SDValue LHSL
, LHSH
, RHSL
, RHSH
;
3468 GetExpandedInteger(N
->getOperand(0), LHSL
, LHSH
);
3469 GetExpandedInteger(N
->getOperand(1), RHSL
, RHSH
);
3471 EVT NVT
= LHSL
.getValueType();
3472 SDValue LoOps
[2] = { LHSL
, RHSL
};
3473 SDValue HiOps
[3] = { LHSH
, RHSH
};
3475 bool HasOpCarry
= TLI
.isOperationLegalOrCustom(
3476 N
->getOpcode() == ISD::ADD
? ISD::UADDO_CARRY
: ISD::USUBO_CARRY
,
3477 TLI
.getTypeToExpandTo(*DAG
.getContext(), NVT
));
3479 SDVTList VTList
= DAG
.getVTList(NVT
, getSetCCResultType(NVT
));
3480 if (N
->getOpcode() == ISD::ADD
) {
3481 Lo
= DAG
.getNode(ISD::UADDO
, dl
, VTList
, LoOps
);
3482 HiOps
[2] = Lo
.getValue(1);
3483 Hi
= DAG
.computeKnownBits(HiOps
[2]).isZero()
3484 ? DAG
.getNode(ISD::ADD
, dl
, NVT
, ArrayRef(HiOps
, 2))
3485 : DAG
.getNode(ISD::UADDO_CARRY
, dl
, VTList
, HiOps
);
3487 Lo
= DAG
.getNode(ISD::USUBO
, dl
, VTList
, LoOps
);
3488 HiOps
[2] = Lo
.getValue(1);
3489 Hi
= DAG
.computeKnownBits(HiOps
[2]).isZero()
3490 ? DAG
.getNode(ISD::SUB
, dl
, NVT
, ArrayRef(HiOps
, 2))
3491 : DAG
.getNode(ISD::USUBO_CARRY
, dl
, VTList
, HiOps
);
3496 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
3497 // them. TODO: Teach operation legalization how to expand unsupported
3498 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
3499 // a carry of type MVT::Glue, but there doesn't seem to be any way to
3500 // generate a value of this type in the expanded code sequence.
3502 TLI
.isOperationLegalOrCustom(N
->getOpcode() == ISD::ADD
?
3503 ISD::ADDC
: ISD::SUBC
,
3504 TLI
.getTypeToExpandTo(*DAG
.getContext(), NVT
));
3507 SDVTList VTList
= DAG
.getVTList(NVT
, MVT::Glue
);
3508 if (N
->getOpcode() == ISD::ADD
) {
3509 Lo
= DAG
.getNode(ISD::ADDC
, dl
, VTList
, LoOps
);
3510 HiOps
[2] = Lo
.getValue(1);
3511 Hi
= DAG
.getNode(ISD::ADDE
, dl
, VTList
, HiOps
);
3513 Lo
= DAG
.getNode(ISD::SUBC
, dl
, VTList
, LoOps
);
3514 HiOps
[2] = Lo
.getValue(1);
3515 Hi
= DAG
.getNode(ISD::SUBE
, dl
, VTList
, HiOps
);
3521 TLI
.isOperationLegalOrCustom(N
->getOpcode() == ISD::ADD
?
3522 ISD::UADDO
: ISD::USUBO
,
3523 TLI
.getTypeToExpandTo(*DAG
.getContext(), NVT
));
3524 TargetLoweringBase::BooleanContent BoolType
= TLI
.getBooleanContents(NVT
);
3527 EVT OvfVT
= getSetCCResultType(NVT
);
3528 SDVTList VTList
= DAG
.getVTList(NVT
, OvfVT
);
3530 if (N
->getOpcode() == ISD::ADD
) {
3532 Lo
= DAG
.getNode(ISD::UADDO
, dl
, VTList
, LoOps
);
3533 Hi
= DAG
.getNode(ISD::ADD
, dl
, NVT
, ArrayRef(HiOps
, 2));
3536 Lo
= DAG
.getNode(ISD::USUBO
, dl
, VTList
, LoOps
);
3537 Hi
= DAG
.getNode(ISD::SUB
, dl
, NVT
, ArrayRef(HiOps
, 2));
3539 SDValue OVF
= Lo
.getValue(1);
3542 case TargetLoweringBase::UndefinedBooleanContent
:
3543 OVF
= DAG
.getNode(ISD::AND
, dl
, OvfVT
, DAG
.getConstant(1, dl
, OvfVT
), OVF
);
3545 case TargetLoweringBase::ZeroOrOneBooleanContent
:
3546 OVF
= DAG
.getZExtOrTrunc(OVF
, dl
, NVT
);
3547 Hi
= DAG
.getNode(N
->getOpcode(), dl
, NVT
, Hi
, OVF
);
3549 case TargetLoweringBase::ZeroOrNegativeOneBooleanContent
:
3550 OVF
= DAG
.getSExtOrTrunc(OVF
, dl
, NVT
);
3551 Hi
= DAG
.getNode(RevOpc
, dl
, NVT
, Hi
, OVF
);
3556 if (N
->getOpcode() == ISD::ADD
) {
3557 Lo
= DAG
.getNode(ISD::ADD
, dl
, NVT
, LoOps
);
3559 // Special case: X+1 has a carry out if X+1==0. This may reduce the live
3560 // range of X. We assume comparing with 0 is cheap.
3561 if (isOneConstant(LoOps
[1]))
3562 Cmp
= DAG
.getSetCC(dl
, getSetCCResultType(NVT
), Lo
,
3563 DAG
.getConstant(0, dl
, NVT
), ISD::SETEQ
);
3564 else if (isAllOnesConstant(LoOps
[1])) {
3565 if (isAllOnesConstant(HiOps
[1]))
3566 Cmp
= DAG
.getSetCC(dl
, getSetCCResultType(NVT
), LoOps
[0],
3567 DAG
.getConstant(0, dl
, NVT
), ISD::SETEQ
);
3569 Cmp
= DAG
.getSetCC(dl
, getSetCCResultType(NVT
), LoOps
[0],
3570 DAG
.getConstant(0, dl
, NVT
), ISD::SETNE
);
3572 Cmp
= DAG
.getSetCC(dl
, getSetCCResultType(NVT
), Lo
, LoOps
[0],
3576 if (BoolType
== TargetLoweringBase::ZeroOrOneBooleanContent
)
3577 Carry
= DAG
.getZExtOrTrunc(Cmp
, dl
, NVT
);
3579 Carry
= DAG
.getSelect(dl
, NVT
, Cmp
, DAG
.getConstant(1, dl
, NVT
),
3580 DAG
.getConstant(0, dl
, NVT
));
3582 if (isAllOnesConstant(LoOps
[1]) && isAllOnesConstant(HiOps
[1])) {
3583 Hi
= DAG
.getNode(ISD::SUB
, dl
, NVT
, HiOps
[0], Carry
);
3585 Hi
= DAG
.getNode(ISD::ADD
, dl
, NVT
, ArrayRef(HiOps
, 2));
3586 Hi
= DAG
.getNode(ISD::ADD
, dl
, NVT
, Hi
, Carry
);
3589 Lo
= DAG
.getNode(ISD::SUB
, dl
, NVT
, LoOps
);
3590 Hi
= DAG
.getNode(ISD::SUB
, dl
, NVT
, ArrayRef(HiOps
, 2));
3592 DAG
.getSetCC(dl
, getSetCCResultType(LoOps
[0].getValueType()),
3593 LoOps
[0], LoOps
[1], ISD::SETULT
);
3596 if (BoolType
== TargetLoweringBase::ZeroOrOneBooleanContent
)
3597 Borrow
= DAG
.getZExtOrTrunc(Cmp
, dl
, NVT
);
3599 Borrow
= DAG
.getSelect(dl
, NVT
, Cmp
, DAG
.getConstant(1, dl
, NVT
),
3600 DAG
.getConstant(0, dl
, NVT
));
3602 Hi
= DAG
.getNode(ISD::SUB
, dl
, NVT
, Hi
, Borrow
);
3606 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode
*N
,
3607 SDValue
&Lo
, SDValue
&Hi
) {
3608 // Expand the subcomponents.
3609 SDValue LHSL
, LHSH
, RHSL
, RHSH
;
3611 GetExpandedInteger(N
->getOperand(0), LHSL
, LHSH
);
3612 GetExpandedInteger(N
->getOperand(1), RHSL
, RHSH
);
3613 SDVTList VTList
= DAG
.getVTList(LHSL
.getValueType(), MVT::Glue
);
3614 SDValue LoOps
[2] = { LHSL
, RHSL
};
3615 SDValue HiOps
[3] = { LHSH
, RHSH
};
3617 if (N
->getOpcode() == ISD::ADDC
) {
3618 Lo
= DAG
.getNode(ISD::ADDC
, dl
, VTList
, LoOps
);
3619 HiOps
[2] = Lo
.getValue(1);
3620 Hi
= DAG
.getNode(ISD::ADDE
, dl
, VTList
, HiOps
);
3622 Lo
= DAG
.getNode(ISD::SUBC
, dl
, VTList
, LoOps
);
3623 HiOps
[2] = Lo
.getValue(1);
3624 Hi
= DAG
.getNode(ISD::SUBE
, dl
, VTList
, HiOps
);
3627 // Legalized the flag result - switch anything that used the old flag to
3629 ReplaceValueWith(SDValue(N
, 1), Hi
.getValue(1));
3632 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode
*N
,
3633 SDValue
&Lo
, SDValue
&Hi
) {
3634 // Expand the subcomponents.
3635 SDValue LHSL
, LHSH
, RHSL
, RHSH
;
3637 GetExpandedInteger(N
->getOperand(0), LHSL
, LHSH
);
3638 GetExpandedInteger(N
->getOperand(1), RHSL
, RHSH
);
3639 SDVTList VTList
= DAG
.getVTList(LHSL
.getValueType(), MVT::Glue
);
3640 SDValue LoOps
[3] = { LHSL
, RHSL
, N
->getOperand(2) };
3641 SDValue HiOps
[3] = { LHSH
, RHSH
};
3643 Lo
= DAG
.getNode(N
->getOpcode(), dl
, VTList
, LoOps
);
3644 HiOps
[2] = Lo
.getValue(1);
3645 Hi
= DAG
.getNode(N
->getOpcode(), dl
, VTList
, HiOps
);
3647 // Legalized the flag result - switch anything that used the old flag to
3649 ReplaceValueWith(SDValue(N
, 1), Hi
.getValue(1));
3652 void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode
*N
,
3653 SDValue
&Lo
, SDValue
&Hi
) {
3654 SDValue LHS
= N
->getOperand(0);
3655 SDValue RHS
= N
->getOperand(1);
3660 unsigned CarryOp
, NoCarryOp
;
3662 switch(N
->getOpcode()) {
3664 CarryOp
= ISD::UADDO_CARRY
;
3665 NoCarryOp
= ISD::ADD
;
3669 CarryOp
= ISD::USUBO_CARRY
;
3670 NoCarryOp
= ISD::SUB
;
3674 llvm_unreachable("Node has unexpected Opcode");
3677 bool HasCarryOp
= TLI
.isOperationLegalOrCustom(
3678 CarryOp
, TLI
.getTypeToExpandTo(*DAG
.getContext(), LHS
.getValueType()));
3681 // Expand the subcomponents.
3682 SDValue LHSL
, LHSH
, RHSL
, RHSH
;
3683 GetExpandedInteger(LHS
, LHSL
, LHSH
);
3684 GetExpandedInteger(RHS
, RHSL
, RHSH
);
3685 SDVTList VTList
= DAG
.getVTList(LHSL
.getValueType(), N
->getValueType(1));
3686 SDValue LoOps
[2] = { LHSL
, RHSL
};
3687 SDValue HiOps
[3] = { LHSH
, RHSH
};
3689 Lo
= DAG
.getNode(N
->getOpcode(), dl
, VTList
, LoOps
);
3690 HiOps
[2] = Lo
.getValue(1);
3691 Hi
= DAG
.getNode(CarryOp
, dl
, VTList
, HiOps
);
3693 Ovf
= Hi
.getValue(1);
3695 // Expand the result by simply replacing it with the equivalent
3696 // non-overflow-checking operation.
3697 SDValue Sum
= DAG
.getNode(NoCarryOp
, dl
, LHS
.getValueType(), LHS
, RHS
);
3698 SplitInteger(Sum
, Lo
, Hi
);
3700 if (N
->getOpcode() == ISD::UADDO
&& isOneConstant(RHS
)) {
3701 // Special case: uaddo X, 1 overflowed if X+1 == 0. We can detect this
3702 // with (Lo | Hi) == 0.
3703 SDValue Or
= DAG
.getNode(ISD::OR
, dl
, Lo
.getValueType(), Lo
, Hi
);
3704 Ovf
= DAG
.getSetCC(dl
, N
->getValueType(1), Or
,
3705 DAG
.getConstant(0, dl
, Lo
.getValueType()), ISD::SETEQ
);
3706 } else if (N
->getOpcode() == ISD::UADDO
&& isAllOnesConstant(RHS
)) {
3707 // Special case: uaddo X, -1 overflows if X == 0.
3709 DAG
.getSetCC(dl
, N
->getValueType(1), LHS
,
3710 DAG
.getConstant(0, dl
, LHS
.getValueType()), ISD::SETNE
);
3712 // Calculate the overflow: addition overflows iff a + b < a, and
3713 // subtraction overflows iff a - b > a.
3714 Ovf
= DAG
.getSetCC(dl
, N
->getValueType(1), Sum
, LHS
, Cond
);
3718 // Legalized the flag result - switch anything that used the old flag to
3720 ReplaceValueWith(SDValue(N
, 1), Ovf
);
3723 void DAGTypeLegalizer::ExpandIntRes_UADDSUBO_CARRY(SDNode
*N
, SDValue
&Lo
,
3725 // Expand the subcomponents.
3726 SDValue LHSL
, LHSH
, RHSL
, RHSH
;
3728 GetExpandedInteger(N
->getOperand(0), LHSL
, LHSH
);
3729 GetExpandedInteger(N
->getOperand(1), RHSL
, RHSH
);
3730 SDVTList VTList
= DAG
.getVTList(LHSL
.getValueType(), N
->getValueType(1));
3731 SDValue LoOps
[3] = { LHSL
, RHSL
, N
->getOperand(2) };
3732 SDValue HiOps
[3] = { LHSH
, RHSH
, SDValue() };
3734 Lo
= DAG
.getNode(N
->getOpcode(), dl
, VTList
, LoOps
);
3735 HiOps
[2] = Lo
.getValue(1);
3736 Hi
= DAG
.getNode(N
->getOpcode(), dl
, VTList
, HiOps
);
3738 // Legalized the flag result - switch anything that used the old flag to
3740 ReplaceValueWith(SDValue(N
, 1), Hi
.getValue(1));
3743 void DAGTypeLegalizer::ExpandIntRes_SADDSUBO_CARRY(SDNode
*N
,
3744 SDValue
&Lo
, SDValue
&Hi
) {
3745 // Expand the subcomponents.
3746 SDValue LHSL
, LHSH
, RHSL
, RHSH
;
3748 GetExpandedInteger(N
->getOperand(0), LHSL
, LHSH
);
3749 GetExpandedInteger(N
->getOperand(1), RHSL
, RHSH
);
3750 SDVTList VTList
= DAG
.getVTList(LHSL
.getValueType(), N
->getValueType(1));
3752 // We need to use an unsigned carry op for the lo part.
3754 N
->getOpcode() == ISD::SADDO_CARRY
? ISD::UADDO_CARRY
: ISD::USUBO_CARRY
;
3755 Lo
= DAG
.getNode(CarryOp
, dl
, VTList
, { LHSL
, RHSL
, N
->getOperand(2) });
3756 Hi
= DAG
.getNode(N
->getOpcode(), dl
, VTList
, { LHSH
, RHSH
, Lo
.getValue(1) });
3758 // Legalized the flag result - switch anything that used the old flag to
3760 ReplaceValueWith(SDValue(N
, 1), Hi
.getValue(1));
3763 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode
*N
,
3764 SDValue
&Lo
, SDValue
&Hi
) {
3765 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
3767 SDValue Op
= N
->getOperand(0);
3768 if (Op
.getValueType().bitsLE(NVT
)) {
3769 // The low part is any extension of the input (which degenerates to a copy).
3770 Lo
= DAG
.getNode(ISD::ANY_EXTEND
, dl
, NVT
, Op
);
3771 Hi
= DAG
.getUNDEF(NVT
); // The high part is undefined.
3773 // For example, extension of an i48 to an i64. The operand type necessarily
3774 // promotes to the result type, so will end up being expanded too.
3775 assert(getTypeAction(Op
.getValueType()) ==
3776 TargetLowering::TypePromoteInteger
&&
3777 "Only know how to promote this result!");
3778 SDValue Res
= GetPromotedInteger(Op
);
3779 assert(Res
.getValueType() == N
->getValueType(0) &&
3780 "Operand over promoted?");
3781 // Split the promoted operand. This will simplify when it is expanded.
3782 SplitInteger(Res
, Lo
, Hi
);
3786 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode
*N
,
3787 SDValue
&Lo
, SDValue
&Hi
) {
3789 GetExpandedInteger(N
->getOperand(0), Lo
, Hi
);
3790 EVT NVT
= Lo
.getValueType();
3791 EVT EVT
= cast
<VTSDNode
>(N
->getOperand(1))->getVT();
3792 unsigned NVTBits
= NVT
.getSizeInBits();
3793 unsigned EVTBits
= EVT
.getSizeInBits();
3795 if (NVTBits
< EVTBits
) {
3796 Hi
= DAG
.getNode(ISD::AssertSext
, dl
, NVT
, Hi
,
3797 DAG
.getValueType(EVT::getIntegerVT(*DAG
.getContext(),
3798 EVTBits
- NVTBits
)));
3800 Lo
= DAG
.getNode(ISD::AssertSext
, dl
, NVT
, Lo
, DAG
.getValueType(EVT
));
3801 // The high part replicates the sign bit of Lo, make it explicit.
3802 Hi
= DAG
.getNode(ISD::SRA
, dl
, NVT
, Lo
,
3803 DAG
.getConstant(NVTBits
- 1, dl
,
3804 TLI
.getPointerTy(DAG
.getDataLayout())));
3808 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode
*N
,
3809 SDValue
&Lo
, SDValue
&Hi
) {
3811 GetExpandedInteger(N
->getOperand(0), Lo
, Hi
);
3812 EVT NVT
= Lo
.getValueType();
3813 EVT EVT
= cast
<VTSDNode
>(N
->getOperand(1))->getVT();
3814 unsigned NVTBits
= NVT
.getSizeInBits();
3815 unsigned EVTBits
= EVT
.getSizeInBits();
3817 if (NVTBits
< EVTBits
) {
3818 Hi
= DAG
.getNode(ISD::AssertZext
, dl
, NVT
, Hi
,
3819 DAG
.getValueType(EVT::getIntegerVT(*DAG
.getContext(),
3820 EVTBits
- NVTBits
)));
3822 Lo
= DAG
.getNode(ISD::AssertZext
, dl
, NVT
, Lo
, DAG
.getValueType(EVT
));
3823 // The high part must be zero, make it explicit.
3824 Hi
= DAG
.getConstant(0, dl
, NVT
);
3828 void DAGTypeLegalizer::ExpandIntRes_BITREVERSE(SDNode
*N
,
3829 SDValue
&Lo
, SDValue
&Hi
) {
3831 GetExpandedInteger(N
->getOperand(0), Hi
, Lo
); // Note swapped operands.
3832 Lo
= DAG
.getNode(ISD::BITREVERSE
, dl
, Lo
.getValueType(), Lo
);
3833 Hi
= DAG
.getNode(ISD::BITREVERSE
, dl
, Hi
.getValueType(), Hi
);
3836 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode
*N
,
3837 SDValue
&Lo
, SDValue
&Hi
) {
3839 GetExpandedInteger(N
->getOperand(0), Hi
, Lo
); // Note swapped operands.
3840 Lo
= DAG
.getNode(ISD::BSWAP
, dl
, Lo
.getValueType(), Lo
);
3841 Hi
= DAG
.getNode(ISD::BSWAP
, dl
, Hi
.getValueType(), Hi
);
3844 void DAGTypeLegalizer::ExpandIntRes_PARITY(SDNode
*N
, SDValue
&Lo
,
3847 // parity(HiLo) -> parity(Lo^Hi)
3848 GetExpandedInteger(N
->getOperand(0), Lo
, Hi
);
3849 EVT NVT
= Lo
.getValueType();
3851 DAG
.getNode(ISD::PARITY
, dl
, NVT
, DAG
.getNode(ISD::XOR
, dl
, NVT
, Lo
, Hi
));
3852 Hi
= DAG
.getConstant(0, dl
, NVT
);
3855 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode
*N
,
3856 SDValue
&Lo
, SDValue
&Hi
) {
3857 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
3858 unsigned NBitWidth
= NVT
.getSizeInBits();
3859 auto Constant
= cast
<ConstantSDNode
>(N
);
3860 const APInt
&Cst
= Constant
->getAPIntValue();
3861 bool IsTarget
= Constant
->isTargetOpcode();
3862 bool IsOpaque
= Constant
->isOpaque();
3864 Lo
= DAG
.getConstant(Cst
.trunc(NBitWidth
), dl
, NVT
, IsTarget
, IsOpaque
);
3865 Hi
= DAG
.getConstant(Cst
.lshr(NBitWidth
).trunc(NBitWidth
), dl
, NVT
, IsTarget
,
3869 void DAGTypeLegalizer::ExpandIntRes_ABS(SDNode
*N
, SDValue
&Lo
, SDValue
&Hi
) {
3872 SDValue N0
= N
->getOperand(0);
3873 GetExpandedInteger(N0
, Lo
, Hi
);
3874 EVT NVT
= Lo
.getValueType();
3876 // If the upper half is all sign bits, then we can perform the ABS on the
3877 // lower half and zero-extend.
3878 if (DAG
.ComputeNumSignBits(N0
) > NVT
.getScalarSizeInBits()) {
3879 Lo
= DAG
.getNode(ISD::ABS
, dl
, NVT
, Lo
);
3880 Hi
= DAG
.getConstant(0, dl
, NVT
);
3884 // If we have USUBO_CARRY, use the expanded form of the sra+xor+sub sequence
3885 // we use in LegalizeDAG. The SUB part of the expansion is based on
3886 // ExpandIntRes_ADDSUB which also uses USUBO_CARRY/USUBO after checking that
3887 // USUBO_CARRY is LegalOrCustom. Each of the pieces here can be further
3888 // expanded if needed. Shift expansion has a special case for filling with
3889 // sign bits so that we will only end up with one SRA.
3890 bool HasSubCarry
= TLI
.isOperationLegalOrCustom(
3891 ISD::USUBO_CARRY
, TLI
.getTypeToExpandTo(*DAG
.getContext(), NVT
));
3893 SDValue Sign
= DAG
.getNode(
3894 ISD::SRA
, dl
, NVT
, Hi
,
3895 DAG
.getShiftAmountConstant(NVT
.getSizeInBits() - 1, NVT
, dl
));
3896 SDVTList VTList
= DAG
.getVTList(NVT
, getSetCCResultType(NVT
));
3897 Lo
= DAG
.getNode(ISD::XOR
, dl
, NVT
, Lo
, Sign
);
3898 Hi
= DAG
.getNode(ISD::XOR
, dl
, NVT
, Hi
, Sign
);
3899 Lo
= DAG
.getNode(ISD::USUBO
, dl
, VTList
, Lo
, Sign
);
3900 Hi
= DAG
.getNode(ISD::USUBO_CARRY
, dl
, VTList
, Hi
, Sign
, Lo
.getValue(1));
3904 // abs(HiLo) -> (Hi < 0 ? -HiLo : HiLo)
3905 EVT VT
= N
->getValueType(0);
3906 SDValue Neg
= DAG
.getNode(ISD::SUB
, dl
, VT
,
3907 DAG
.getConstant(0, dl
, VT
), N0
);
3908 SDValue NegLo
, NegHi
;
3909 SplitInteger(Neg
, NegLo
, NegHi
);
3911 SDValue HiIsNeg
= DAG
.getSetCC(dl
, getSetCCResultType(NVT
), Hi
,
3912 DAG
.getConstant(0, dl
, NVT
), ISD::SETLT
);
3913 Lo
= DAG
.getSelect(dl
, NVT
, HiIsNeg
, NegLo
, Lo
);
3914 Hi
= DAG
.getSelect(dl
, NVT
, HiIsNeg
, NegHi
, Hi
);
3917 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode
*N
,
3918 SDValue
&Lo
, SDValue
&Hi
) {
3920 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
3921 GetExpandedInteger(N
->getOperand(0), Lo
, Hi
);
3922 EVT NVT
= Lo
.getValueType();
3924 SDValue HiNotZero
= DAG
.getSetCC(dl
, getSetCCResultType(NVT
), Hi
,
3925 DAG
.getConstant(0, dl
, NVT
), ISD::SETNE
);
3927 SDValue LoLZ
= DAG
.getNode(N
->getOpcode(), dl
, NVT
, Lo
);
3928 SDValue HiLZ
= DAG
.getNode(ISD::CTLZ_ZERO_UNDEF
, dl
, NVT
, Hi
);
3930 Lo
= DAG
.getSelect(dl
, NVT
, HiNotZero
, HiLZ
,
3931 DAG
.getNode(ISD::ADD
, dl
, NVT
, LoLZ
,
3932 DAG
.getConstant(NVT
.getSizeInBits(), dl
,
3934 Hi
= DAG
.getConstant(0, dl
, NVT
);
3937 void DAGTypeLegalizer::ExpandIntRes_ABD(SDNode
*N
, SDValue
&Lo
, SDValue
&Hi
) {
3938 SDValue Result
= TLI
.expandABD(N
, DAG
);
3939 SplitInteger(Result
, Lo
, Hi
);
3942 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode
*N
,
3943 SDValue
&Lo
, SDValue
&Hi
) {
3945 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
3946 GetExpandedInteger(N
->getOperand(0), Lo
, Hi
);
3947 EVT NVT
= Lo
.getValueType();
3948 Lo
= DAG
.getNode(ISD::ADD
, dl
, NVT
, DAG
.getNode(ISD::CTPOP
, dl
, NVT
, Lo
),
3949 DAG
.getNode(ISD::CTPOP
, dl
, NVT
, Hi
));
3950 Hi
= DAG
.getConstant(0, dl
, NVT
);
3953 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode
*N
,
3954 SDValue
&Lo
, SDValue
&Hi
) {
3956 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
3957 GetExpandedInteger(N
->getOperand(0), Lo
, Hi
);
3958 EVT NVT
= Lo
.getValueType();
3960 SDValue LoNotZero
= DAG
.getSetCC(dl
, getSetCCResultType(NVT
), Lo
,
3961 DAG
.getConstant(0, dl
, NVT
), ISD::SETNE
);
3963 SDValue LoLZ
= DAG
.getNode(ISD::CTTZ_ZERO_UNDEF
, dl
, NVT
, Lo
);
3964 SDValue HiLZ
= DAG
.getNode(N
->getOpcode(), dl
, NVT
, Hi
);
3966 Lo
= DAG
.getSelect(dl
, NVT
, LoNotZero
, LoLZ
,
3967 DAG
.getNode(ISD::ADD
, dl
, NVT
, HiLZ
,
3968 DAG
.getConstant(NVT
.getSizeInBits(), dl
,
3970 Hi
= DAG
.getConstant(0, dl
, NVT
);
3973 void DAGTypeLegalizer::ExpandIntRes_GET_ROUNDING(SDNode
*N
, SDValue
&Lo
,
3976 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
3977 unsigned NBitWidth
= NVT
.getSizeInBits();
3979 Lo
= DAG
.getNode(ISD::GET_ROUNDING
, dl
, {NVT
, MVT::Other
}, N
->getOperand(0));
3980 SDValue Chain
= Lo
.getValue(1);
3981 // The high part is the sign of Lo, as -1 is a valid value for GET_ROUNDING
3982 Hi
= DAG
.getNode(ISD::SRA
, dl
, NVT
, Lo
,
3983 DAG
.getShiftAmountConstant(NBitWidth
- 1, NVT
, dl
));
3985 // Legalize the chain result - switch anything that used the old chain to
3987 ReplaceValueWith(SDValue(N
, 1), Chain
);
3990 // Helper for producing an FP_EXTEND/STRICT_FP_EXTEND of Op.
3991 static SDValue
fpExtendHelper(SDValue Op
, SDValue
&Chain
, bool IsStrict
, EVT VT
,
3992 SDLoc DL
, SelectionDAG
&DAG
) {
3994 Op
= DAG
.getNode(ISD::STRICT_FP_EXTEND
, DL
, {VT
, MVT::Other
}, {Chain
, Op
});
3995 Chain
= Op
.getValue(1);
3998 return DAG
.getNode(ISD::FP_EXTEND
, DL
, VT
, Op
);
4001 void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT(SDNode
*N
, SDValue
&Lo
,
4004 EVT VT
= N
->getValueType(0);
4006 bool IsSigned
= N
->getOpcode() == ISD::FP_TO_SINT
||
4007 N
->getOpcode() == ISD::STRICT_FP_TO_SINT
;
4008 bool IsStrict
= N
->isStrictFPOpcode();
4009 SDValue Chain
= IsStrict
? N
->getOperand(0) : SDValue();
4010 SDValue Op
= N
->getOperand(IsStrict
? 1 : 0);
4011 if (getTypeAction(Op
.getValueType()) == TargetLowering::TypePromoteFloat
)
4012 Op
= GetPromotedFloat(Op
);
4014 // If the input is bf16 or needs to be soft promoted, extend to f32.
4015 if (getTypeAction(Op
.getValueType()) == TargetLowering::TypeSoftPromoteHalf
||
4016 Op
.getValueType() == MVT::bf16
) {
4017 Op
= fpExtendHelper(Op
, Chain
, IsStrict
, MVT::f32
, dl
, DAG
);
4020 // NOTE: We need a variable that lives across makeLibCall so
4021 // CallOptions.setTypeListBeforeSoften can save a reference to it.
4022 EVT OpVT
= Op
.getValueType();
4025 IsSigned
? RTLIB::getFPTOSINT(OpVT
, VT
) : RTLIB::getFPTOUINT(OpVT
, VT
);
4026 assert(LC
!= RTLIB::UNKNOWN_LIBCALL
&& "Unexpected fp-to-xint conversion!");
4027 TargetLowering::MakeLibCallOptions CallOptions
;
4028 if (getTypeAction(Op
.getValueType()) == TargetLowering::TypeSoftenFloat
)
4029 CallOptions
.setTypeListBeforeSoften(OpVT
, VT
);
4031 CallOptions
.setIsSigned(true); // FIXME: Is this needed?
4032 std::pair
<SDValue
, SDValue
> Tmp
= TLI
.makeLibCall(DAG
, LC
, VT
, Op
,
4033 CallOptions
, dl
, Chain
);
4034 SplitInteger(Tmp
.first
, Lo
, Hi
);
4037 ReplaceValueWith(SDValue(N
, 1), Tmp
.second
);
4040 void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT_SAT(SDNode
*N
, SDValue
&Lo
,
4042 SDValue Res
= TLI
.expandFP_TO_INT_SAT(N
, DAG
);
4043 SplitInteger(Res
, Lo
, Hi
);
4046 void DAGTypeLegalizer::ExpandIntRes_XROUND_XRINT(SDNode
*N
, SDValue
&Lo
,
4049 bool IsStrict
= N
->isStrictFPOpcode();
4050 SDValue Op
= N
->getOperand(IsStrict
? 1 : 0);
4051 SDValue Chain
= IsStrict
? N
->getOperand(0) : SDValue();
4053 assert(getTypeAction(Op
.getValueType()) != TargetLowering::TypePromoteFloat
&&
4054 "Input type needs to be promoted!");
4056 EVT VT
= Op
.getValueType();
4058 if (VT
== MVT::f16
) {
4061 Op
= fpExtendHelper(Op
, Chain
, IsStrict
, VT
, dl
, DAG
);
4064 RTLIB::Libcall LC
= RTLIB::UNKNOWN_LIBCALL
;
4065 if (N
->getOpcode() == ISD::LROUND
||
4066 N
->getOpcode() == ISD::STRICT_LROUND
) {
4068 LC
= RTLIB::LROUND_F32
;
4069 else if (VT
== MVT::f64
)
4070 LC
= RTLIB::LROUND_F64
;
4071 else if (VT
== MVT::f80
)
4072 LC
= RTLIB::LROUND_F80
;
4073 else if (VT
== MVT::f128
)
4074 LC
= RTLIB::LROUND_F128
;
4075 else if (VT
== MVT::ppcf128
)
4076 LC
= RTLIB::LROUND_PPCF128
;
4077 assert(LC
!= RTLIB::UNKNOWN_LIBCALL
&& "Unexpected lround input type!");
4078 } else if (N
->getOpcode() == ISD::LRINT
||
4079 N
->getOpcode() == ISD::STRICT_LRINT
) {
4081 LC
= RTLIB::LRINT_F32
;
4082 else if (VT
== MVT::f64
)
4083 LC
= RTLIB::LRINT_F64
;
4084 else if (VT
== MVT::f80
)
4085 LC
= RTLIB::LRINT_F80
;
4086 else if (VT
== MVT::f128
)
4087 LC
= RTLIB::LRINT_F128
;
4088 else if (VT
== MVT::ppcf128
)
4089 LC
= RTLIB::LRINT_PPCF128
;
4090 assert(LC
!= RTLIB::UNKNOWN_LIBCALL
&& "Unexpected lrint input type!");
4091 } else if (N
->getOpcode() == ISD::LLROUND
||
4092 N
->getOpcode() == ISD::STRICT_LLROUND
) {
4094 LC
= RTLIB::LLROUND_F32
;
4095 else if (VT
== MVT::f64
)
4096 LC
= RTLIB::LLROUND_F64
;
4097 else if (VT
== MVT::f80
)
4098 LC
= RTLIB::LLROUND_F80
;
4099 else if (VT
== MVT::f128
)
4100 LC
= RTLIB::LLROUND_F128
;
4101 else if (VT
== MVT::ppcf128
)
4102 LC
= RTLIB::LLROUND_PPCF128
;
4103 assert(LC
!= RTLIB::UNKNOWN_LIBCALL
&& "Unexpected llround input type!");
4104 } else if (N
->getOpcode() == ISD::LLRINT
||
4105 N
->getOpcode() == ISD::STRICT_LLRINT
) {
4107 LC
= RTLIB::LLRINT_F32
;
4108 else if (VT
== MVT::f64
)
4109 LC
= RTLIB::LLRINT_F64
;
4110 else if (VT
== MVT::f80
)
4111 LC
= RTLIB::LLRINT_F80
;
4112 else if (VT
== MVT::f128
)
4113 LC
= RTLIB::LLRINT_F128
;
4114 else if (VT
== MVT::ppcf128
)
4115 LC
= RTLIB::LLRINT_PPCF128
;
4116 assert(LC
!= RTLIB::UNKNOWN_LIBCALL
&& "Unexpected llrint input type!");
4118 llvm_unreachable("Unexpected opcode!");
4120 EVT RetVT
= N
->getValueType(0);
4122 TargetLowering::MakeLibCallOptions CallOptions
;
4123 CallOptions
.setIsSigned(true);
4124 std::pair
<SDValue
, SDValue
> Tmp
= TLI
.makeLibCall(DAG
, LC
, RetVT
,
4125 Op
, CallOptions
, dl
,
4127 SplitInteger(Tmp
.first
, Lo
, Hi
);
4129 if (N
->isStrictFPOpcode())
4130 ReplaceValueWith(SDValue(N
, 1), Tmp
.second
);
4133 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode
*N
,
4134 SDValue
&Lo
, SDValue
&Hi
) {
4135 assert(!N
->isAtomic() && "Should have been a ATOMIC_LOAD?");
4137 if (ISD::isNormalLoad(N
)) {
4138 ExpandRes_NormalLoad(N
, Lo
, Hi
);
4142 assert(ISD::isUNINDEXEDLoad(N
) && "Indexed load during type legalization!");
4144 EVT VT
= N
->getValueType(0);
4145 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), VT
);
4146 SDValue Ch
= N
->getChain();
4147 SDValue Ptr
= N
->getBasePtr();
4148 ISD::LoadExtType ExtType
= N
->getExtensionType();
4149 MachineMemOperand::Flags MMOFlags
= N
->getMemOperand()->getFlags();
4150 AAMDNodes AAInfo
= N
->getAAInfo();
4153 assert(NVT
.isByteSized() && "Expanded type not byte sized!");
4155 if (N
->getMemoryVT().bitsLE(NVT
)) {
4156 EVT MemVT
= N
->getMemoryVT();
4158 Lo
= DAG
.getExtLoad(ExtType
, dl
, NVT
, Ch
, Ptr
, N
->getPointerInfo(), MemVT
,
4159 N
->getOriginalAlign(), MMOFlags
, AAInfo
);
4161 // Remember the chain.
4162 Ch
= Lo
.getValue(1);
4164 if (ExtType
== ISD::SEXTLOAD
) {
4165 // The high part is obtained by SRA'ing all but one of the bits of the
4167 unsigned LoSize
= Lo
.getValueSizeInBits();
4168 Hi
= DAG
.getNode(ISD::SRA
, dl
, NVT
, Lo
,
4169 DAG
.getConstant(LoSize
- 1, dl
,
4170 TLI
.getPointerTy(DAG
.getDataLayout())));
4171 } else if (ExtType
== ISD::ZEXTLOAD
) {
4172 // The high part is just a zero.
4173 Hi
= DAG
.getConstant(0, dl
, NVT
);
4175 assert(ExtType
== ISD::EXTLOAD
&& "Unknown extload!");
4176 // The high part is undefined.
4177 Hi
= DAG
.getUNDEF(NVT
);
4179 } else if (DAG
.getDataLayout().isLittleEndian()) {
4180 // Little-endian - low bits are at low addresses.
4181 Lo
= DAG
.getLoad(NVT
, dl
, Ch
, Ptr
, N
->getPointerInfo(),
4182 N
->getOriginalAlign(), MMOFlags
, AAInfo
);
4184 unsigned ExcessBits
=
4185 N
->getMemoryVT().getSizeInBits() - NVT
.getSizeInBits();
4186 EVT NEVT
= EVT::getIntegerVT(*DAG
.getContext(), ExcessBits
);
4188 // Increment the pointer to the other half.
4189 unsigned IncrementSize
= NVT
.getSizeInBits()/8;
4190 Ptr
= DAG
.getMemBasePlusOffset(Ptr
, TypeSize::getFixed(IncrementSize
), dl
);
4191 Hi
= DAG
.getExtLoad(ExtType
, dl
, NVT
, Ch
, Ptr
,
4192 N
->getPointerInfo().getWithOffset(IncrementSize
), NEVT
,
4193 N
->getOriginalAlign(), MMOFlags
, AAInfo
);
4195 // Build a factor node to remember that this load is independent of the
4197 Ch
= DAG
.getNode(ISD::TokenFactor
, dl
, MVT::Other
, Lo
.getValue(1),
4200 // Big-endian - high bits are at low addresses. Favor aligned loads at
4201 // the cost of some bit-fiddling.
4202 EVT MemVT
= N
->getMemoryVT();
4203 unsigned EBytes
= MemVT
.getStoreSize();
4204 unsigned IncrementSize
= NVT
.getSizeInBits()/8;
4205 unsigned ExcessBits
= (EBytes
- IncrementSize
)*8;
4207 // Load both the high bits and maybe some of the low bits.
4208 Hi
= DAG
.getExtLoad(ExtType
, dl
, NVT
, Ch
, Ptr
, N
->getPointerInfo(),
4209 EVT::getIntegerVT(*DAG
.getContext(),
4210 MemVT
.getSizeInBits() - ExcessBits
),
4211 N
->getOriginalAlign(), MMOFlags
, AAInfo
);
4213 // Increment the pointer to the other half.
4214 Ptr
= DAG
.getMemBasePlusOffset(Ptr
, TypeSize::getFixed(IncrementSize
), dl
);
4215 // Load the rest of the low bits.
4216 Lo
= DAG
.getExtLoad(ISD::ZEXTLOAD
, dl
, NVT
, Ch
, Ptr
,
4217 N
->getPointerInfo().getWithOffset(IncrementSize
),
4218 EVT::getIntegerVT(*DAG
.getContext(), ExcessBits
),
4219 N
->getOriginalAlign(), MMOFlags
, AAInfo
);
4221 // Build a factor node to remember that this load is independent of the
4223 Ch
= DAG
.getNode(ISD::TokenFactor
, dl
, MVT::Other
, Lo
.getValue(1),
4226 if (ExcessBits
< NVT
.getSizeInBits()) {
4227 // Transfer low bits from the bottom of Hi to the top of Lo.
4229 ISD::OR
, dl
, NVT
, Lo
,
4230 DAG
.getNode(ISD::SHL
, dl
, NVT
, Hi
,
4231 DAG
.getConstant(ExcessBits
, dl
,
4232 TLI
.getPointerTy(DAG
.getDataLayout()))));
4233 // Move high bits to the right position in Hi.
4234 Hi
= DAG
.getNode(ExtType
== ISD::SEXTLOAD
? ISD::SRA
: ISD::SRL
, dl
, NVT
,
4236 DAG
.getConstant(NVT
.getSizeInBits() - ExcessBits
, dl
,
4237 TLI
.getPointerTy(DAG
.getDataLayout())));
4241 // Legalize the chain result - switch anything that used the old chain to
4243 ReplaceValueWith(SDValue(N
, 1), Ch
);
4246 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode
*N
,
4247 SDValue
&Lo
, SDValue
&Hi
) {
4249 SDValue LL
, LH
, RL
, RH
;
4250 GetExpandedInteger(N
->getOperand(0), LL
, LH
);
4251 GetExpandedInteger(N
->getOperand(1), RL
, RH
);
4252 Lo
= DAG
.getNode(N
->getOpcode(), dl
, LL
.getValueType(), LL
, RL
);
4253 Hi
= DAG
.getNode(N
->getOpcode(), dl
, LL
.getValueType(), LH
, RH
);
4256 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode
*N
,
4257 SDValue
&Lo
, SDValue
&Hi
) {
4258 EVT VT
= N
->getValueType(0);
4259 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), VT
);
4262 SDValue LL
, LH
, RL
, RH
;
4263 GetExpandedInteger(N
->getOperand(0), LL
, LH
);
4264 GetExpandedInteger(N
->getOperand(1), RL
, RH
);
4266 if (TLI
.expandMUL(N
, Lo
, Hi
, NVT
, DAG
,
4267 TargetLowering::MulExpansionKind::OnlyLegalOrCustom
,
4271 // If nothing else, we can make a libcall.
4272 RTLIB::Libcall LC
= RTLIB::UNKNOWN_LIBCALL
;
4274 LC
= RTLIB::MUL_I16
;
4275 else if (VT
== MVT::i32
)
4276 LC
= RTLIB::MUL_I32
;
4277 else if (VT
== MVT::i64
)
4278 LC
= RTLIB::MUL_I64
;
4279 else if (VT
== MVT::i128
)
4280 LC
= RTLIB::MUL_I128
;
4282 if (LC
== RTLIB::UNKNOWN_LIBCALL
|| !TLI
.getLibcallName(LC
)) {
4283 // Perform a wide multiplication where the wide type is the original VT and
4284 // the 4 parts are the split arguments.
4285 TLI
.forceExpandWideMUL(DAG
, dl
, /*Signed=*/true, VT
, LL
, LH
, RL
, RH
, Lo
,
4290 // Note that we don't need to do a wide MUL here since we don't care about the
4291 // upper half of the result if it exceeds VT.
4292 SDValue Ops
[2] = { N
->getOperand(0), N
->getOperand(1) };
4293 TargetLowering::MakeLibCallOptions CallOptions
;
4294 CallOptions
.setIsSigned(true);
4295 SplitInteger(TLI
.makeLibCall(DAG
, LC
, VT
, Ops
, CallOptions
, dl
).first
,
4299 void DAGTypeLegalizer::ExpandIntRes_READCOUNTER(SDNode
*N
, SDValue
&Lo
,
4302 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
4303 SDVTList VTs
= DAG
.getVTList(NVT
, NVT
, MVT::Other
);
4304 SDValue R
= DAG
.getNode(N
->getOpcode(), DL
, VTs
, N
->getOperand(0));
4307 ReplaceValueWith(SDValue(N
, 1), R
.getValue(2));
4310 void DAGTypeLegalizer::ExpandIntRes_AVG(SDNode
*N
, SDValue
&Lo
, SDValue
&Hi
) {
4311 SDValue Result
= TLI
.expandAVG(N
, DAG
);
4312 SplitInteger(Result
, Lo
, Hi
);
4315 void DAGTypeLegalizer::ExpandIntRes_ADDSUBSAT(SDNode
*N
, SDValue
&Lo
,
4317 SDValue Result
= TLI
.expandAddSubSat(N
, DAG
);
4318 SplitInteger(Result
, Lo
, Hi
);
4321 void DAGTypeLegalizer::ExpandIntRes_SHLSAT(SDNode
*N
, SDValue
&Lo
,
4323 SDValue Result
= TLI
.expandShlSat(N
, DAG
);
4324 SplitInteger(Result
, Lo
, Hi
);
4327 /// This performs an expansion of the integer result for a fixed point
4328 /// multiplication. The default expansion performs rounding down towards
4329 /// negative infinity, though targets that do care about rounding should specify
4330 /// a target hook for rounding and provide their own expansion or lowering of
4331 /// fixed point multiplication to be consistent with rounding.
4332 void DAGTypeLegalizer::ExpandIntRes_MULFIX(SDNode
*N
, SDValue
&Lo
,
4335 EVT VT
= N
->getValueType(0);
4336 unsigned VTSize
= VT
.getScalarSizeInBits();
4337 SDValue LHS
= N
->getOperand(0);
4338 SDValue RHS
= N
->getOperand(1);
4339 uint64_t Scale
= N
->getConstantOperandVal(2);
4340 bool Saturating
= (N
->getOpcode() == ISD::SMULFIXSAT
||
4341 N
->getOpcode() == ISD::UMULFIXSAT
);
4342 bool Signed
= (N
->getOpcode() == ISD::SMULFIX
||
4343 N
->getOpcode() == ISD::SMULFIXSAT
);
4345 // Handle special case when scale is equal to zero.
4349 Result
= DAG
.getNode(ISD::MUL
, dl
, VT
, LHS
, RHS
);
4351 EVT BoolVT
= getSetCCResultType(VT
);
4352 unsigned MulOp
= Signed
? ISD::SMULO
: ISD::UMULO
;
4353 Result
= DAG
.getNode(MulOp
, dl
, DAG
.getVTList(VT
, BoolVT
), LHS
, RHS
);
4354 SDValue Product
= Result
.getValue(0);
4355 SDValue Overflow
= Result
.getValue(1);
4357 APInt MinVal
= APInt::getSignedMinValue(VTSize
);
4358 APInt MaxVal
= APInt::getSignedMaxValue(VTSize
);
4359 SDValue SatMin
= DAG
.getConstant(MinVal
, dl
, VT
);
4360 SDValue SatMax
= DAG
.getConstant(MaxVal
, dl
, VT
);
4361 SDValue Zero
= DAG
.getConstant(0, dl
, VT
);
4362 // Xor the inputs, if resulting sign bit is 0 the product will be
4363 // positive, else negative.
4364 SDValue Xor
= DAG
.getNode(ISD::XOR
, dl
, VT
, LHS
, RHS
);
4365 SDValue ProdNeg
= DAG
.getSetCC(dl
, BoolVT
, Xor
, Zero
, ISD::SETLT
);
4366 Result
= DAG
.getSelect(dl
, VT
, ProdNeg
, SatMin
, SatMax
);
4367 Result
= DAG
.getSelect(dl
, VT
, Overflow
, Result
, Product
);
4369 // For unsigned multiplication, we only need to check the max since we
4370 // can't really overflow towards zero.
4371 APInt MaxVal
= APInt::getMaxValue(VTSize
);
4372 SDValue SatMax
= DAG
.getConstant(MaxVal
, dl
, VT
);
4373 Result
= DAG
.getSelect(dl
, VT
, Overflow
, SatMax
, Product
);
4376 SplitInteger(Result
, Lo
, Hi
);
4380 // For SMULFIX[SAT] we only expect to find Scale<VTSize, but this assert will
4381 // cover for unhandled cases below, while still being valid for UMULFIX[SAT].
4382 assert(Scale
<= VTSize
&& "Scale can't be larger than the value type size.");
4384 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), VT
);
4385 SDValue LL
, LH
, RL
, RH
;
4386 GetExpandedInteger(LHS
, LL
, LH
);
4387 GetExpandedInteger(RHS
, RL
, RH
);
4388 SmallVector
<SDValue
, 4> Result
;
4390 unsigned LoHiOp
= Signed
? ISD::SMUL_LOHI
: ISD::UMUL_LOHI
;
4391 if (!TLI
.expandMUL_LOHI(LoHiOp
, VT
, dl
, LHS
, RHS
, Result
, NVT
, DAG
,
4392 TargetLowering::MulExpansionKind::OnlyLegalOrCustom
,
4397 SDValue LoTmp
, HiTmp
;
4398 TLI
.forceExpandWideMUL(DAG
, dl
, Signed
, LHS
, RHS
, LoTmp
, HiTmp
);
4399 SplitInteger(LoTmp
, Result
[0], Result
[1]);
4400 SplitInteger(HiTmp
, Result
[2], Result
[3]);
4402 assert(Result
.size() == 4 && "Unexpected number of partlets in the result");
4404 unsigned NVTSize
= NVT
.getScalarSizeInBits();
4405 assert((VTSize
== NVTSize
* 2) && "Expected the new value type to be half "
4406 "the size of the current value type");
4408 // After getting the multiplication result in 4 parts, we need to perform a
4409 // shift right by the amount of the scale to get the result in that scale.
4411 // Let's say we multiply 2 64 bit numbers. The resulting value can be held in
4412 // 128 bits that are cut into 4 32-bit parts:
4415 // |---32---|---32---|---32---|---32---|
4418 // |------VTSize-----|
4422 // The resulting Lo and Hi would normally be in LL and LH after the shift. But
4423 // to avoid unneccessary shifting of all 4 parts, we can adjust the shift
4424 // amount and get Lo and Hi using two funnel shifts. Or for the special case
4425 // when Scale is a multiple of NVTSize we can just pick the result without
4427 uint64_t Part0
= Scale
/ NVTSize
; // Part holding lowest bit needed.
4428 if (Scale
% NVTSize
) {
4429 SDValue ShiftAmount
= DAG
.getShiftAmountConstant(Scale
% NVTSize
, NVT
, dl
);
4430 Lo
= DAG
.getNode(ISD::FSHR
, dl
, NVT
, Result
[Part0
+ 1], Result
[Part0
],
4432 Hi
= DAG
.getNode(ISD::FSHR
, dl
, NVT
, Result
[Part0
+ 2], Result
[Part0
+ 1],
4436 Hi
= Result
[Part0
+ 1];
4439 // Unless saturation is requested we are done. The result is in <Hi,Lo>.
4443 // Can not overflow when there is no integer part.
4444 if (Scale
== VTSize
)
4447 // To handle saturation we must check for overflow in the multiplication.
4449 // Unsigned overflow happened if the upper (VTSize - Scale) bits (of Result)
4450 // aren't all zeroes.
4452 // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of Result)
4453 // aren't all ones or all zeroes.
4455 // We cannot overflow past HH when multiplying 2 ints of size VTSize, so the
4456 // highest bit of HH determines saturation direction in the event of signed
4459 SDValue ResultHL
= Result
[2];
4460 SDValue ResultHH
= Result
[3];
4462 SDValue SatMax
, SatMin
;
4463 SDValue NVTZero
= DAG
.getConstant(0, dl
, NVT
);
4464 SDValue NVTNeg1
= DAG
.getAllOnesConstant(dl
, NVT
);
4465 EVT BoolNVT
= getSetCCResultType(NVT
);
4468 if (Scale
< NVTSize
) {
4469 // Overflow happened if ((HH | (HL >> Scale)) != 0).
4470 SDValue HLAdjusted
=
4471 DAG
.getNode(ISD::SRL
, dl
, NVT
, ResultHL
,
4472 DAG
.getShiftAmountConstant(Scale
, NVT
, dl
));
4473 SDValue Tmp
= DAG
.getNode(ISD::OR
, dl
, NVT
, HLAdjusted
, ResultHH
);
4474 SatMax
= DAG
.getSetCC(dl
, BoolNVT
, Tmp
, NVTZero
, ISD::SETNE
);
4475 } else if (Scale
== NVTSize
) {
4476 // Overflow happened if (HH != 0).
4477 SatMax
= DAG
.getSetCC(dl
, BoolNVT
, ResultHH
, NVTZero
, ISD::SETNE
);
4478 } else if (Scale
< VTSize
) {
4479 // Overflow happened if ((HH >> (Scale - NVTSize)) != 0).
4480 SDValue HLAdjusted
=
4481 DAG
.getNode(ISD::SRL
, dl
, NVT
, ResultHL
,
4482 DAG
.getShiftAmountConstant(Scale
- NVTSize
, NVT
, dl
));
4483 SatMax
= DAG
.getSetCC(dl
, BoolNVT
, HLAdjusted
, NVTZero
, ISD::SETNE
);
4485 llvm_unreachable("Scale must be less or equal to VTSize for UMULFIXSAT"
4486 "(and saturation can't happen with Scale==VTSize).");
4488 Hi
= DAG
.getSelect(dl
, NVT
, SatMax
, NVTNeg1
, Hi
);
4489 Lo
= DAG
.getSelect(dl
, NVT
, SatMax
, NVTNeg1
, Lo
);
4493 if (Scale
< NVTSize
) {
4494 // The number of overflow bits we can check are VTSize - Scale + 1 (we
4495 // include the sign bit). If these top bits are > 0, then we overflowed past
4496 // the max value. If these top bits are < -1, then we overflowed past the
4497 // min value. Otherwise, we did not overflow.
4498 unsigned OverflowBits
= VTSize
- Scale
+ 1;
4499 assert(OverflowBits
<= VTSize
&& OverflowBits
> NVTSize
&&
4500 "Extent of overflow bits must start within HL");
4501 SDValue HLHiMask
= DAG
.getConstant(
4502 APInt::getHighBitsSet(NVTSize
, OverflowBits
- NVTSize
), dl
, NVT
);
4503 SDValue HLLoMask
= DAG
.getConstant(
4504 APInt::getLowBitsSet(NVTSize
, VTSize
- OverflowBits
), dl
, NVT
);
4505 // We overflow max if HH > 0 or (HH == 0 && HL > HLLoMask).
4506 SDValue HHGT0
= DAG
.getSetCC(dl
, BoolNVT
, ResultHH
, NVTZero
, ISD::SETGT
);
4507 SDValue HHEQ0
= DAG
.getSetCC(dl
, BoolNVT
, ResultHH
, NVTZero
, ISD::SETEQ
);
4508 SDValue HLUGT
= DAG
.getSetCC(dl
, BoolNVT
, ResultHL
, HLLoMask
, ISD::SETUGT
);
4509 SatMax
= DAG
.getNode(ISD::OR
, dl
, BoolNVT
, HHGT0
,
4510 DAG
.getNode(ISD::AND
, dl
, BoolNVT
, HHEQ0
, HLUGT
));
4511 // We overflow min if HH < -1 or (HH == -1 && HL < HLHiMask).
4512 SDValue HHLT
= DAG
.getSetCC(dl
, BoolNVT
, ResultHH
, NVTNeg1
, ISD::SETLT
);
4513 SDValue HHEQ
= DAG
.getSetCC(dl
, BoolNVT
, ResultHH
, NVTNeg1
, ISD::SETEQ
);
4514 SDValue HLULT
= DAG
.getSetCC(dl
, BoolNVT
, ResultHL
, HLHiMask
, ISD::SETULT
);
4515 SatMin
= DAG
.getNode(ISD::OR
, dl
, BoolNVT
, HHLT
,
4516 DAG
.getNode(ISD::AND
, dl
, BoolNVT
, HHEQ
, HLULT
));
4517 } else if (Scale
== NVTSize
) {
4518 // We overflow max if HH > 0 or (HH == 0 && HL sign bit is 1).
4519 SDValue HHGT0
= DAG
.getSetCC(dl
, BoolNVT
, ResultHH
, NVTZero
, ISD::SETGT
);
4520 SDValue HHEQ0
= DAG
.getSetCC(dl
, BoolNVT
, ResultHH
, NVTZero
, ISD::SETEQ
);
4521 SDValue HLNeg
= DAG
.getSetCC(dl
, BoolNVT
, ResultHL
, NVTZero
, ISD::SETLT
);
4522 SatMax
= DAG
.getNode(ISD::OR
, dl
, BoolNVT
, HHGT0
,
4523 DAG
.getNode(ISD::AND
, dl
, BoolNVT
, HHEQ0
, HLNeg
));
4524 // We overflow min if HH < -1 or (HH == -1 && HL sign bit is 0).
4525 SDValue HHLT
= DAG
.getSetCC(dl
, BoolNVT
, ResultHH
, NVTNeg1
, ISD::SETLT
);
4526 SDValue HHEQ
= DAG
.getSetCC(dl
, BoolNVT
, ResultHH
, NVTNeg1
, ISD::SETEQ
);
4527 SDValue HLPos
= DAG
.getSetCC(dl
, BoolNVT
, ResultHL
, NVTZero
, ISD::SETGE
);
4528 SatMin
= DAG
.getNode(ISD::OR
, dl
, BoolNVT
, HHLT
,
4529 DAG
.getNode(ISD::AND
, dl
, BoolNVT
, HHEQ
, HLPos
));
4530 } else if (Scale
< VTSize
) {
4531 // This is similar to the case when we saturate if Scale < NVTSize, but we
4532 // only need to check HH.
4533 unsigned OverflowBits
= VTSize
- Scale
+ 1;
4534 SDValue HHHiMask
= DAG
.getConstant(
4535 APInt::getHighBitsSet(NVTSize
, OverflowBits
), dl
, NVT
);
4536 SDValue HHLoMask
= DAG
.getConstant(
4537 APInt::getLowBitsSet(NVTSize
, NVTSize
- OverflowBits
), dl
, NVT
);
4538 SatMax
= DAG
.getSetCC(dl
, BoolNVT
, ResultHH
, HHLoMask
, ISD::SETGT
);
4539 SatMin
= DAG
.getSetCC(dl
, BoolNVT
, ResultHH
, HHHiMask
, ISD::SETLT
);
4541 llvm_unreachable("Illegal scale for signed fixed point mul.");
4543 // Saturate to signed maximum.
4544 APInt MaxHi
= APInt::getSignedMaxValue(NVTSize
);
4545 APInt MaxLo
= APInt::getAllOnes(NVTSize
);
4546 Hi
= DAG
.getSelect(dl
, NVT
, SatMax
, DAG
.getConstant(MaxHi
, dl
, NVT
), Hi
);
4547 Lo
= DAG
.getSelect(dl
, NVT
, SatMax
, DAG
.getConstant(MaxLo
, dl
, NVT
), Lo
);
4548 // Saturate to signed minimum.
4549 APInt MinHi
= APInt::getSignedMinValue(NVTSize
);
4550 Hi
= DAG
.getSelect(dl
, NVT
, SatMin
, DAG
.getConstant(MinHi
, dl
, NVT
), Hi
);
4551 Lo
= DAG
.getSelect(dl
, NVT
, SatMin
, NVTZero
, Lo
);
4554 void DAGTypeLegalizer::ExpandIntRes_DIVFIX(SDNode
*N
, SDValue
&Lo
,
4557 // Try expanding in the existing type first.
4558 SDValue Res
= TLI
.expandFixedPointDiv(N
->getOpcode(), dl
, N
->getOperand(0),
4560 N
->getConstantOperandVal(2), DAG
);
4563 Res
= earlyExpandDIVFIX(N
, N
->getOperand(0), N
->getOperand(1),
4564 N
->getConstantOperandVal(2), TLI
, DAG
);
4565 SplitInteger(Res
, Lo
, Hi
);
4568 void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode
*Node
,
4569 SDValue
&Lo
, SDValue
&Hi
) {
4570 assert((Node
->getOpcode() == ISD::SADDO
|| Node
->getOpcode() == ISD::SSUBO
) &&
4571 "Node has unexpected Opcode");
4572 SDValue LHS
= Node
->getOperand(0);
4573 SDValue RHS
= Node
->getOperand(1);
4578 bool IsAdd
= Node
->getOpcode() == ISD::SADDO
;
4579 unsigned CarryOp
= IsAdd
? ISD::SADDO_CARRY
: ISD::SSUBO_CARRY
;
4581 bool HasCarryOp
= TLI
.isOperationLegalOrCustom(
4582 CarryOp
, TLI
.getTypeToExpandTo(*DAG
.getContext(), LHS
.getValueType()));
4585 // Expand the subcomponents.
4586 SDValue LHSL
, LHSH
, RHSL
, RHSH
;
4587 GetExpandedInteger(LHS
, LHSL
, LHSH
);
4588 GetExpandedInteger(RHS
, RHSL
, RHSH
);
4589 SDVTList VTList
= DAG
.getVTList(LHSL
.getValueType(), Node
->getValueType(1));
4591 Lo
= DAG
.getNode(IsAdd
? ISD::UADDO
: ISD::USUBO
, dl
, VTList
, {LHSL
, RHSL
});
4592 Hi
= DAG
.getNode(CarryOp
, dl
, VTList
, { LHSH
, RHSH
, Lo
.getValue(1) });
4594 Ovf
= Hi
.getValue(1);
4596 // Expand the result by simply replacing it with the equivalent
4597 // non-overflow-checking operation.
4598 SDValue Sum
= DAG
.getNode(Node
->getOpcode() == ISD::SADDO
?
4599 ISD::ADD
: ISD::SUB
, dl
, LHS
.getValueType(),
4601 SplitInteger(Sum
, Lo
, Hi
);
4603 // Compute the overflow.
4605 // LHSSign -> LHS < 0
4606 // RHSSign -> RHS < 0
4607 // SumSign -> Sum < 0
4610 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
4612 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
4614 // To get better codegen we can rewrite this by doing bitwise math on
4615 // the integers and extract the final sign bit at the end. So the
4619 // Overflow -> (~(LHS ^ RHS) & (LHS ^ Sum)) < 0
4621 // Overflow -> ((LHS ^ RHS) & (LHS ^ Sum)) < 0
4623 // NOTE: This is different than the expansion we do in expandSADDSUBO
4624 // because it is more costly to determine the RHS is > 0 for SSUBO with the
4626 EVT VT
= LHS
.getValueType();
4627 SDValue SignsMatch
= DAG
.getNode(ISD::XOR
, dl
, VT
, LHS
, RHS
);
4629 SignsMatch
= DAG
.getNOT(dl
, SignsMatch
, VT
);
4631 SDValue SumSignNE
= DAG
.getNode(ISD::XOR
, dl
, VT
, LHS
, Sum
);
4632 Ovf
= DAG
.getNode(ISD::AND
, dl
, VT
, SignsMatch
, SumSignNE
);
4633 EVT OType
= Node
->getValueType(1);
4634 Ovf
= DAG
.getSetCC(dl
, OType
, Ovf
, DAG
.getConstant(0, dl
, VT
), ISD::SETLT
);
4637 // Use the calculated overflow everywhere.
4638 ReplaceValueWith(SDValue(Node
, 1), Ovf
);
4641 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode
*N
,
4642 SDValue
&Lo
, SDValue
&Hi
) {
4643 EVT VT
= N
->getValueType(0);
4645 SDValue Ops
[2] = { N
->getOperand(0), N
->getOperand(1) };
4647 if (TLI
.getOperationAction(ISD::SDIVREM
, VT
) == TargetLowering::Custom
) {
4648 SDValue Res
= DAG
.getNode(ISD::SDIVREM
, dl
, DAG
.getVTList(VT
, VT
), Ops
);
4649 SplitInteger(Res
.getValue(0), Lo
, Hi
);
4653 RTLIB::Libcall LC
= RTLIB::UNKNOWN_LIBCALL
;
4655 LC
= RTLIB::SDIV_I16
;
4656 else if (VT
== MVT::i32
)
4657 LC
= RTLIB::SDIV_I32
;
4658 else if (VT
== MVT::i64
)
4659 LC
= RTLIB::SDIV_I64
;
4660 else if (VT
== MVT::i128
)
4661 LC
= RTLIB::SDIV_I128
;
4662 assert(LC
!= RTLIB::UNKNOWN_LIBCALL
&& "Unsupported SDIV!");
4664 TargetLowering::MakeLibCallOptions CallOptions
;
4665 CallOptions
.setIsSigned(true);
4666 SplitInteger(TLI
.makeLibCall(DAG
, LC
, VT
, Ops
, CallOptions
, dl
).first
, Lo
, Hi
);
4669 void DAGTypeLegalizer::ExpandIntRes_ShiftThroughStack(SDNode
*N
, SDValue
&Lo
,
4672 SDValue Shiftee
= N
->getOperand(0);
4673 EVT VT
= Shiftee
.getValueType();
4674 SDValue ShAmt
= N
->getOperand(1);
4675 EVT ShAmtVT
= ShAmt
.getValueType();
4679 LoadVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), LoadVT
);
4680 } while (!TLI
.isTypeLegal(LoadVT
));
4682 const unsigned ShiftUnitInBits
= LoadVT
.getStoreSizeInBits();
4683 assert(ShiftUnitInBits
<= VT
.getScalarSizeInBits());
4684 assert(isPowerOf2_32(ShiftUnitInBits
) &&
4685 "Shifting unit is not a a power of two!");
4687 const bool IsOneStepShift
=
4688 DAG
.computeKnownBits(ShAmt
).countMinTrailingZeros() >=
4689 Log2_32(ShiftUnitInBits
);
4691 // If we can't do it as one step, we'll have two uses of shift amount,
4692 // and thus must freeze it.
4693 if (!IsOneStepShift
)
4694 ShAmt
= DAG
.getFreeze(ShAmt
);
4696 unsigned VTBitWidth
= VT
.getScalarSizeInBits();
4697 assert(VTBitWidth
% 8 == 0 && "Shifting a not byte multiple value?");
4698 unsigned VTByteWidth
= VTBitWidth
/ 8;
4699 assert(isPowerOf2_32(VTByteWidth
) &&
4700 "Shiftee type size is not a power of two!");
4701 unsigned StackSlotByteWidth
= 2 * VTByteWidth
;
4702 unsigned StackSlotBitWidth
= 8 * StackSlotByteWidth
;
4703 EVT StackSlotVT
= EVT::getIntegerVT(*DAG
.getContext(), StackSlotBitWidth
);
4705 // Get a temporary stack slot 2x the width of our VT.
4706 // FIXME: reuse stack slots?
4707 Align StackAlign
= DAG
.getReducedAlign(StackSlotVT
, /*UseABI=*/false);
4709 DAG
.CreateStackTemporary(StackSlotVT
.getStoreSize(), StackAlign
);
4710 EVT PtrTy
= StackPtr
.getValueType();
4711 SDValue Ch
= DAG
.getEntryNode();
4713 MachinePointerInfo StackPtrInfo
= MachinePointerInfo::getFixedStack(
4714 DAG
.getMachineFunction(),
4715 cast
<FrameIndexSDNode
>(StackPtr
.getNode())->getIndex());
4717 // Extend the value, that is being shifted, to the entire stack slot's width.
4719 if (N
->getOpcode() != ISD::SHL
) {
4720 unsigned WideningOpc
=
4721 N
->getOpcode() == ISD::SRA
? ISD::SIGN_EXTEND
: ISD::ZERO_EXTEND
;
4722 Init
= DAG
.getNode(WideningOpc
, dl
, StackSlotVT
, Shiftee
);
4724 // For left-shifts, pad the Shiftee's LSB with zeros to twice it's width.
4725 SDValue AllZeros
= DAG
.getConstant(0, dl
, VT
);
4726 Init
= DAG
.getNode(ISD::BUILD_PAIR
, dl
, StackSlotVT
, AllZeros
, Shiftee
);
4728 // And spill it into the stack slot.
4729 Ch
= DAG
.getStore(Ch
, dl
, Init
, StackPtr
, StackPtrInfo
, StackAlign
);
4731 // Now, compute the full-byte offset into stack slot from where we can load.
4732 // We have shift amount, which is in bits. Offset should point to an aligned
4735 Flags
.setExact(IsOneStepShift
);
4736 SDValue SrlTmp
= DAG
.getNode(
4737 ISD::SRL
, dl
, ShAmtVT
, ShAmt
,
4738 DAG
.getConstant(Log2_32(ShiftUnitInBits
), dl
, ShAmtVT
), Flags
);
4740 DAG
.getNode(ISD::SHL
, dl
, ShAmtVT
, SrlTmp
,
4741 DAG
.getConstant(Log2_32(ShiftUnitInBits
), dl
, ShAmtVT
));
4743 SDValue ByteOffset
=
4744 DAG
.getNode(ISD::SRL
, dl
, ShAmtVT
, BitOffset
,
4745 DAG
.getConstant(3, dl
, ShAmtVT
), SDNodeFlags::Exact
);
4746 // And clamp it, because OOB load is an immediate UB,
4747 // while shift overflow would have *just* been poison.
4748 ByteOffset
= DAG
.getNode(ISD::AND
, dl
, ShAmtVT
, ByteOffset
,
4749 DAG
.getConstant(VTByteWidth
- 1, dl
, ShAmtVT
));
4750 // We have exactly two strategies on indexing into stack slot here:
4751 // 1. upwards starting from the beginning of the slot
4752 // 2. downwards starting from the middle of the slot
4753 // On little-endian machine, we pick 1. for right shifts and 2. for left-shift
4754 // and vice versa on big-endian machine.
4755 bool WillIndexUpwards
= N
->getOpcode() != ISD::SHL
;
4756 if (DAG
.getDataLayout().isBigEndian())
4757 WillIndexUpwards
= !WillIndexUpwards
;
4759 SDValue AdjStackPtr
;
4760 if (WillIndexUpwards
) {
4761 AdjStackPtr
= StackPtr
;
4763 AdjStackPtr
= DAG
.getMemBasePlusOffset(
4764 StackPtr
, DAG
.getConstant(VTByteWidth
, dl
, PtrTy
), dl
);
4765 ByteOffset
= DAG
.getNegative(ByteOffset
, dl
, ShAmtVT
);
4768 // Get the pointer somewhere into the stack slot from which we need to load.
4769 ByteOffset
= DAG
.getSExtOrTrunc(ByteOffset
, dl
, PtrTy
);
4770 AdjStackPtr
= DAG
.getMemBasePlusOffset(AdjStackPtr
, ByteOffset
, dl
);
4772 // And load it! While the load is not legal, legalizing it is obvious.
4774 DAG
.getLoad(VT
, dl
, Ch
, AdjStackPtr
,
4775 MachinePointerInfo::getUnknownStack(DAG
.getMachineFunction()),
4776 commonAlignment(StackAlign
, LoadVT
.getStoreSize()));
4778 // If we may still have a remaining bits to shift by, do so now.
4779 if (!IsOneStepShift
) {
4781 DAG
.getNode(ISD::AND
, dl
, ShAmtVT
, ShAmt
,
4782 DAG
.getConstant(ShiftUnitInBits
- 1, dl
, ShAmtVT
));
4783 Res
= DAG
.getNode(N
->getOpcode(), dl
, VT
, Res
, ShAmtRem
);
4786 // Finally, split the computed value.
4787 SplitInteger(Res
, Lo
, Hi
);
4790 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode
*N
,
4791 SDValue
&Lo
, SDValue
&Hi
) {
4792 EVT VT
= N
->getValueType(0);
4793 unsigned Opc
= N
->getOpcode();
4796 // If we can emit an efficient shift operation, do so now. Check to see if
4797 // the RHS is a constant.
4798 if (ConstantSDNode
*CN
= dyn_cast
<ConstantSDNode
>(N
->getOperand(1)))
4799 return ExpandShiftByConstant(N
, CN
->getAPIntValue(), Lo
, Hi
);
4801 // If we can determine that the high bit of the shift is zero or one, even if
4802 // the low bits are variable, emit this shift in an optimized form.
4803 if (ExpandShiftWithKnownAmountBit(N
, Lo
, Hi
))
4806 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
4808 if (Opc
== ISD::SHL
) {
4809 PartsOpc
= ISD::SHL_PARTS
;
4810 } else if (Opc
== ISD::SRL
) {
4811 PartsOpc
= ISD::SRL_PARTS
;
4813 assert(Opc
== ISD::SRA
&& "Unknown shift!");
4814 PartsOpc
= ISD::SRA_PARTS
;
4817 // Next check to see if the target supports this SHL_PARTS operation or if it
4818 // will custom expand it. Don't lower this to SHL_PARTS when we optimise for
4819 // size, but create a libcall instead.
4820 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), VT
);
4821 TargetLowering::LegalizeAction Action
= TLI
.getOperationAction(PartsOpc
, NVT
);
4822 const bool LegalOrCustom
=
4823 (Action
== TargetLowering::Legal
&& TLI
.isTypeLegal(NVT
)) ||
4824 Action
== TargetLowering::Custom
;
4826 unsigned ExpansionFactor
= 1;
4827 // That VT->NVT expansion is one step. But will we re-expand NVT?
4828 for (EVT TmpVT
= NVT
;;) {
4829 EVT NewTMPVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), TmpVT
);
4830 if (NewTMPVT
== TmpVT
)
4836 TargetLowering::ShiftLegalizationStrategy S
=
4837 TLI
.preferredShiftLegalizationStrategy(DAG
, N
, ExpansionFactor
);
4839 if (S
== TargetLowering::ShiftLegalizationStrategy::ExpandThroughStack
)
4840 return ExpandIntRes_ShiftThroughStack(N
, Lo
, Hi
);
4842 if (LegalOrCustom
&&
4843 S
!= TargetLowering::ShiftLegalizationStrategy::LowerToLibcall
) {
4844 // Expand the subcomponents.
4846 GetExpandedInteger(N
->getOperand(0), LHSL
, LHSH
);
4847 EVT VT
= LHSL
.getValueType();
4849 // If the shift amount operand is coming from a vector legalization it may
4850 // have an illegal type. Fix that first by casting the operand, otherwise
4851 // the new SHL_PARTS operation would need further legalization.
4852 SDValue ShiftOp
= N
->getOperand(1);
4853 EVT ShiftTy
= TLI
.getShiftAmountTy(VT
, DAG
.getDataLayout());
4854 if (ShiftOp
.getValueType() != ShiftTy
)
4855 ShiftOp
= DAG
.getZExtOrTrunc(ShiftOp
, dl
, ShiftTy
);
4857 SDValue Ops
[] = { LHSL
, LHSH
, ShiftOp
};
4858 Lo
= DAG
.getNode(PartsOpc
, dl
, DAG
.getVTList(VT
, VT
), Ops
);
4859 Hi
= Lo
.getValue(1);
4863 // Otherwise, emit a libcall.
4864 RTLIB::Libcall LC
= RTLIB::UNKNOWN_LIBCALL
;
4866 if (Opc
== ISD::SHL
) {
4867 isSigned
= false; /*sign irrelevant*/
4869 LC
= RTLIB::SHL_I16
;
4870 else if (VT
== MVT::i32
)
4871 LC
= RTLIB::SHL_I32
;
4872 else if (VT
== MVT::i64
)
4873 LC
= RTLIB::SHL_I64
;
4874 else if (VT
== MVT::i128
)
4875 LC
= RTLIB::SHL_I128
;
4876 } else if (Opc
== ISD::SRL
) {
4879 LC
= RTLIB::SRL_I16
;
4880 else if (VT
== MVT::i32
)
4881 LC
= RTLIB::SRL_I32
;
4882 else if (VT
== MVT::i64
)
4883 LC
= RTLIB::SRL_I64
;
4884 else if (VT
== MVT::i128
)
4885 LC
= RTLIB::SRL_I128
;
4887 assert(Opc
== ISD::SRA
&& "Unknown shift!");
4890 LC
= RTLIB::SRA_I16
;
4891 else if (VT
== MVT::i32
)
4892 LC
= RTLIB::SRA_I32
;
4893 else if (VT
== MVT::i64
)
4894 LC
= RTLIB::SRA_I64
;
4895 else if (VT
== MVT::i128
)
4896 LC
= RTLIB::SRA_I128
;
4899 if (LC
!= RTLIB::UNKNOWN_LIBCALL
&& TLI
.getLibcallName(LC
)) {
4901 EVT::getIntegerVT(*DAG
.getContext(), DAG
.getLibInfo().getIntSize());
4902 SDValue ShAmt
= DAG
.getZExtOrTrunc(N
->getOperand(1), dl
, ShAmtTy
);
4903 SDValue Ops
[2] = {N
->getOperand(0), ShAmt
};
4904 TargetLowering::MakeLibCallOptions CallOptions
;
4905 CallOptions
.setIsSigned(isSigned
);
4906 SplitInteger(TLI
.makeLibCall(DAG
, LC
, VT
, Ops
, CallOptions
, dl
).first
, Lo
, Hi
);
4910 if (!ExpandShiftWithUnknownAmountBit(N
, Lo
, Hi
))
4911 llvm_unreachable("Unsupported shift!");
4914 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode
*N
,
4915 SDValue
&Lo
, SDValue
&Hi
) {
4916 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
4918 SDValue Op
= N
->getOperand(0);
4919 if (Op
.getValueType().bitsLE(NVT
)) {
4920 // The low part is sign extension of the input (degenerates to a copy).
4921 Lo
= DAG
.getNode(ISD::SIGN_EXTEND
, dl
, NVT
, N
->getOperand(0));
4922 // The high part is obtained by SRA'ing all but one of the bits of low part.
4923 unsigned LoSize
= NVT
.getSizeInBits();
4925 ISD::SRA
, dl
, NVT
, Lo
,
4926 DAG
.getConstant(LoSize
- 1, dl
, TLI
.getPointerTy(DAG
.getDataLayout())));
4928 // For example, extension of an i48 to an i64. The operand type necessarily
4929 // promotes to the result type, so will end up being expanded too.
4930 assert(getTypeAction(Op
.getValueType()) ==
4931 TargetLowering::TypePromoteInteger
&&
4932 "Only know how to promote this result!");
4933 SDValue Res
= GetPromotedInteger(Op
);
4934 assert(Res
.getValueType() == N
->getValueType(0) &&
4935 "Operand over promoted?");
4936 // Split the promoted operand. This will simplify when it is expanded.
4937 SplitInteger(Res
, Lo
, Hi
);
4938 unsigned ExcessBits
= Op
.getValueSizeInBits() - NVT
.getSizeInBits();
4939 Hi
= DAG
.getNode(ISD::SIGN_EXTEND_INREG
, dl
, Hi
.getValueType(), Hi
,
4940 DAG
.getValueType(EVT::getIntegerVT(*DAG
.getContext(),
4945 void DAGTypeLegalizer::
4946 ExpandIntRes_SIGN_EXTEND_INREG(SDNode
*N
, SDValue
&Lo
, SDValue
&Hi
) {
4948 GetExpandedInteger(N
->getOperand(0), Lo
, Hi
);
4949 EVT EVT
= cast
<VTSDNode
>(N
->getOperand(1))->getVT();
4951 if (EVT
.bitsLE(Lo
.getValueType())) {
4952 // sext_inreg the low part if needed.
4953 Lo
= DAG
.getNode(ISD::SIGN_EXTEND_INREG
, dl
, Lo
.getValueType(), Lo
,
4956 // The high part gets the sign extension from the lo-part. This handles
4957 // things like sextinreg V:i64 from i8.
4958 Hi
= DAG
.getNode(ISD::SRA
, dl
, Hi
.getValueType(), Lo
,
4959 DAG
.getConstant(Hi
.getValueSizeInBits() - 1, dl
,
4960 TLI
.getPointerTy(DAG
.getDataLayout())));
4962 // For example, extension of an i48 to an i64. Leave the low part alone,
4963 // sext_inreg the high part.
4964 unsigned ExcessBits
= EVT
.getSizeInBits() - Lo
.getValueSizeInBits();
4965 Hi
= DAG
.getNode(ISD::SIGN_EXTEND_INREG
, dl
, Hi
.getValueType(), Hi
,
4966 DAG
.getValueType(EVT::getIntegerVT(*DAG
.getContext(),
4971 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode
*N
,
4972 SDValue
&Lo
, SDValue
&Hi
) {
4973 EVT VT
= N
->getValueType(0);
4975 SDValue Ops
[2] = { N
->getOperand(0), N
->getOperand(1) };
4977 if (TLI
.getOperationAction(ISD::SDIVREM
, VT
) == TargetLowering::Custom
) {
4978 SDValue Res
= DAG
.getNode(ISD::SDIVREM
, dl
, DAG
.getVTList(VT
, VT
), Ops
);
4979 SplitInteger(Res
.getValue(1), Lo
, Hi
);
4983 RTLIB::Libcall LC
= RTLIB::UNKNOWN_LIBCALL
;
4985 LC
= RTLIB::SREM_I16
;
4986 else if (VT
== MVT::i32
)
4987 LC
= RTLIB::SREM_I32
;
4988 else if (VT
== MVT::i64
)
4989 LC
= RTLIB::SREM_I64
;
4990 else if (VT
== MVT::i128
)
4991 LC
= RTLIB::SREM_I128
;
4992 assert(LC
!= RTLIB::UNKNOWN_LIBCALL
&& "Unsupported SREM!");
4994 TargetLowering::MakeLibCallOptions CallOptions
;
4995 CallOptions
.setIsSigned(true);
4996 SplitInteger(TLI
.makeLibCall(DAG
, LC
, VT
, Ops
, CallOptions
, dl
).first
, Lo
, Hi
);
4999 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode
*N
,
5000 SDValue
&Lo
, SDValue
&Hi
) {
5001 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
5003 Lo
= DAG
.getNode(ISD::TRUNCATE
, dl
, NVT
, N
->getOperand(0));
5004 Hi
= DAG
.getNode(ISD::SRL
, dl
, N
->getOperand(0).getValueType(),
5006 DAG
.getConstant(NVT
.getSizeInBits(), dl
,
5007 TLI
.getPointerTy(DAG
.getDataLayout())));
5008 Hi
= DAG
.getNode(ISD::TRUNCATE
, dl
, NVT
, Hi
);
5011 void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode
*N
,
5012 SDValue
&Lo
, SDValue
&Hi
) {
5013 EVT VT
= N
->getValueType(0);
5016 if (N
->getOpcode() == ISD::UMULO
) {
5017 // This section expands the operation into the following sequence of
5018 // instructions. `iNh` here refers to a type which has half the bit width of
5019 // the type the original operation operated on.
5021 // %0 = %LHS.HI != 0 && %RHS.HI != 0
5022 // %1 = { iNh, i1 } @umul.with.overflow.iNh(iNh %LHS.HI, iNh %RHS.LO)
5023 // %2 = { iNh, i1 } @umul.with.overflow.iNh(iNh %RHS.HI, iNh %LHS.LO)
5024 // %3 = mul nuw iN (%LHS.LOW as iN), (%RHS.LOW as iN)
5025 // %4 = add iNh %1.0, %2.0 as iN
5026 // %5 = { iNh, i1 } @uadd.with.overflow.iNh(iNh %4, iNh %3.HIGH)
5030 // %ovf = %0 || %1.1 || %2.1 || %5.1
5031 SDValue LHS
= N
->getOperand(0), RHS
= N
->getOperand(1);
5032 SDValue LHSHigh
, LHSLow
, RHSHigh
, RHSLow
;
5033 GetExpandedInteger(LHS
, LHSLow
, LHSHigh
);
5034 GetExpandedInteger(RHS
, RHSLow
, RHSHigh
);
5035 EVT HalfVT
= LHSLow
.getValueType();
5036 EVT BitVT
= N
->getValueType(1);
5037 SDVTList VTHalfWithO
= DAG
.getVTList(HalfVT
, BitVT
);
5039 SDValue HalfZero
= DAG
.getConstant(0, dl
, HalfVT
);
5040 SDValue Overflow
= DAG
.getNode(ISD::AND
, dl
, BitVT
,
5041 DAG
.getSetCC(dl
, BitVT
, LHSHigh
, HalfZero
, ISD::SETNE
),
5042 DAG
.getSetCC(dl
, BitVT
, RHSHigh
, HalfZero
, ISD::SETNE
));
5044 SDValue One
= DAG
.getNode(ISD::UMULO
, dl
, VTHalfWithO
, LHSHigh
, RHSLow
);
5045 Overflow
= DAG
.getNode(ISD::OR
, dl
, BitVT
, Overflow
, One
.getValue(1));
5047 SDValue Two
= DAG
.getNode(ISD::UMULO
, dl
, VTHalfWithO
, RHSHigh
, LHSLow
);
5048 Overflow
= DAG
.getNode(ISD::OR
, dl
, BitVT
, Overflow
, Two
.getValue(1));
5050 SDValue HighSum
= DAG
.getNode(ISD::ADD
, dl
, HalfVT
, One
, Two
);
5052 // Cannot use `UMUL_LOHI` directly, because some 32-bit targets (ARM) do not
5053 // know how to expand `i64,i64 = umul_lohi a, b` and abort (why isn’t this
5054 // operation recursively legalized?).
5056 // Many backends understand this pattern and will convert into LOHI
5057 // themselves, if applicable.
5058 SDValue Three
= DAG
.getNode(ISD::MUL
, dl
, VT
,
5059 DAG
.getNode(ISD::ZERO_EXTEND
, dl
, VT
, LHSLow
),
5060 DAG
.getNode(ISD::ZERO_EXTEND
, dl
, VT
, RHSLow
));
5061 SplitInteger(Three
, Lo
, Hi
);
5063 Hi
= DAG
.getNode(ISD::UADDO
, dl
, VTHalfWithO
, Hi
, HighSum
);
5064 Overflow
= DAG
.getNode(ISD::OR
, dl
, BitVT
, Overflow
, Hi
.getValue(1));
5065 ReplaceValueWith(SDValue(N
, 1), Overflow
);
5069 Type
*RetTy
= VT
.getTypeForEVT(*DAG
.getContext());
5070 EVT PtrVT
= TLI
.getPointerTy(DAG
.getDataLayout());
5071 Type
*PtrTy
= PtrVT
.getTypeForEVT(*DAG
.getContext());
5073 // Replace this with a libcall that will check overflow.
5074 RTLIB::Libcall LC
= RTLIB::UNKNOWN_LIBCALL
;
5076 LC
= RTLIB::MULO_I32
;
5077 else if (VT
== MVT::i64
)
5078 LC
= RTLIB::MULO_I64
;
5079 else if (VT
== MVT::i128
)
5080 LC
= RTLIB::MULO_I128
;
5082 // If we don't have the libcall or if the function we are compiling is the
5083 // implementation of the expected libcall (avoid inf-loop), expand inline.
5084 if (LC
== RTLIB::UNKNOWN_LIBCALL
|| !TLI
.getLibcallName(LC
) ||
5085 TLI
.getLibcallName(LC
) == DAG
.getMachineFunction().getName()) {
5086 // FIXME: This is not an optimal expansion, but better than crashing.
5088 EVT::getIntegerVT(*DAG
.getContext(), VT
.getScalarSizeInBits() * 2);
5089 SDValue LHS
= DAG
.getNode(ISD::SIGN_EXTEND
, dl
, WideVT
, N
->getOperand(0));
5090 SDValue RHS
= DAG
.getNode(ISD::SIGN_EXTEND
, dl
, WideVT
, N
->getOperand(1));
5091 SDValue Mul
= DAG
.getNode(ISD::MUL
, dl
, WideVT
, LHS
, RHS
);
5092 SDValue MulLo
, MulHi
;
5093 SplitInteger(Mul
, MulLo
, MulHi
);
5095 DAG
.getNode(ISD::SRA
, dl
, VT
, MulLo
,
5096 DAG
.getConstant(VT
.getScalarSizeInBits() - 1, dl
, VT
));
5098 DAG
.getSetCC(dl
, N
->getValueType(1), MulHi
, SRA
, ISD::SETNE
);
5099 SplitInteger(MulLo
, Lo
, Hi
);
5100 ReplaceValueWith(SDValue(N
, 1), Overflow
);
5104 SDValue Temp
= DAG
.CreateStackTemporary(PtrVT
);
5105 // Temporary for the overflow value, default it to zero.
5107 DAG
.getStore(DAG
.getEntryNode(), dl
, DAG
.getConstant(0, dl
, PtrVT
), Temp
,
5108 MachinePointerInfo());
5110 TargetLowering::ArgListTy Args
;
5111 TargetLowering::ArgListEntry Entry
;
5112 for (const SDValue
&Op
: N
->op_values()) {
5113 EVT ArgVT
= Op
.getValueType();
5114 Type
*ArgTy
= ArgVT
.getTypeForEVT(*DAG
.getContext());
5117 Entry
.IsSExt
= true;
5118 Entry
.IsZExt
= false;
5119 Args
.push_back(Entry
);
5122 // Also pass the address of the overflow check.
5124 Entry
.Ty
= PointerType::getUnqual(PtrTy
->getContext());
5125 Entry
.IsSExt
= true;
5126 Entry
.IsZExt
= false;
5127 Args
.push_back(Entry
);
5129 SDValue Func
= DAG
.getExternalSymbol(TLI
.getLibcallName(LC
), PtrVT
);
5131 TargetLowering::CallLoweringInfo
CLI(DAG
);
5134 .setLibCallee(TLI
.getLibcallCallingConv(LC
), RetTy
, Func
, std::move(Args
))
5137 std::pair
<SDValue
, SDValue
> CallInfo
= TLI
.LowerCallTo(CLI
);
5139 SplitInteger(CallInfo
.first
, Lo
, Hi
);
5141 DAG
.getLoad(PtrVT
, dl
, CallInfo
.second
, Temp
, MachinePointerInfo());
5142 SDValue Ofl
= DAG
.getSetCC(dl
, N
->getValueType(1), Temp2
,
5143 DAG
.getConstant(0, dl
, PtrVT
),
5145 // Use the overflow from the libcall everywhere.
5146 ReplaceValueWith(SDValue(N
, 1), Ofl
);
5149 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode
*N
,
5150 SDValue
&Lo
, SDValue
&Hi
) {
5151 EVT VT
= N
->getValueType(0);
5153 SDValue Ops
[2] = { N
->getOperand(0), N
->getOperand(1) };
5155 if (TLI
.getOperationAction(ISD::UDIVREM
, VT
) == TargetLowering::Custom
) {
5156 SDValue Res
= DAG
.getNode(ISD::UDIVREM
, dl
, DAG
.getVTList(VT
, VT
), Ops
);
5157 SplitInteger(Res
.getValue(0), Lo
, Hi
);
5161 // Try to expand UDIV by constant.
5162 if (isa
<ConstantSDNode
>(N
->getOperand(1))) {
5163 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
5164 // Only if the new type is legal.
5165 if (isTypeLegal(NVT
)) {
5167 GetExpandedInteger(N
->getOperand(0), InL
, InH
);
5168 SmallVector
<SDValue
> Result
;
5169 if (TLI
.expandDIVREMByConstant(N
, Result
, NVT
, DAG
, InL
, InH
)) {
5177 RTLIB::Libcall LC
= RTLIB::UNKNOWN_LIBCALL
;
5179 LC
= RTLIB::UDIV_I16
;
5180 else if (VT
== MVT::i32
)
5181 LC
= RTLIB::UDIV_I32
;
5182 else if (VT
== MVT::i64
)
5183 LC
= RTLIB::UDIV_I64
;
5184 else if (VT
== MVT::i128
)
5185 LC
= RTLIB::UDIV_I128
;
5186 assert(LC
!= RTLIB::UNKNOWN_LIBCALL
&& "Unsupported UDIV!");
5188 TargetLowering::MakeLibCallOptions CallOptions
;
5189 SplitInteger(TLI
.makeLibCall(DAG
, LC
, VT
, Ops
, CallOptions
, dl
).first
, Lo
, Hi
);
5192 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode
*N
,
5193 SDValue
&Lo
, SDValue
&Hi
) {
5194 EVT VT
= N
->getValueType(0);
5196 SDValue Ops
[2] = { N
->getOperand(0), N
->getOperand(1) };
5198 if (TLI
.getOperationAction(ISD::UDIVREM
, VT
) == TargetLowering::Custom
) {
5199 SDValue Res
= DAG
.getNode(ISD::UDIVREM
, dl
, DAG
.getVTList(VT
, VT
), Ops
);
5200 SplitInteger(Res
.getValue(1), Lo
, Hi
);
5204 // Try to expand UREM by constant.
5205 if (isa
<ConstantSDNode
>(N
->getOperand(1))) {
5206 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
5207 // Only if the new type is legal.
5208 if (isTypeLegal(NVT
)) {
5210 GetExpandedInteger(N
->getOperand(0), InL
, InH
);
5211 SmallVector
<SDValue
> Result
;
5212 if (TLI
.expandDIVREMByConstant(N
, Result
, NVT
, DAG
, InL
, InH
)) {
5220 RTLIB::Libcall LC
= RTLIB::UNKNOWN_LIBCALL
;
5222 LC
= RTLIB::UREM_I16
;
5223 else if (VT
== MVT::i32
)
5224 LC
= RTLIB::UREM_I32
;
5225 else if (VT
== MVT::i64
)
5226 LC
= RTLIB::UREM_I64
;
5227 else if (VT
== MVT::i128
)
5228 LC
= RTLIB::UREM_I128
;
5229 assert(LC
!= RTLIB::UNKNOWN_LIBCALL
&& "Unsupported UREM!");
5231 TargetLowering::MakeLibCallOptions CallOptions
;
5232 SplitInteger(TLI
.makeLibCall(DAG
, LC
, VT
, Ops
, CallOptions
, dl
).first
, Lo
, Hi
);
5235 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode
*N
,
5236 SDValue
&Lo
, SDValue
&Hi
) {
5237 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
5239 SDValue Op
= N
->getOperand(0);
5240 if (Op
.getValueType().bitsLE(NVT
)) {
5241 // The low part is zero extension of the input (degenerates to a copy).
5242 Lo
= DAG
.getNode(ISD::ZERO_EXTEND
, dl
, NVT
, N
->getOperand(0));
5243 Hi
= DAG
.getConstant(0, dl
, NVT
); // The high part is just a zero.
5245 // For example, extension of an i48 to an i64. The operand type necessarily
5246 // promotes to the result type, so will end up being expanded too.
5247 assert(getTypeAction(Op
.getValueType()) ==
5248 TargetLowering::TypePromoteInteger
&&
5249 "Only know how to promote this result!");
5250 SDValue Res
= GetPromotedInteger(Op
);
5251 assert(Res
.getValueType() == N
->getValueType(0) &&
5252 "Operand over promoted?");
5253 // Split the promoted operand. This will simplify when it is expanded.
5254 SplitInteger(Res
, Lo
, Hi
);
5255 unsigned ExcessBits
= Op
.getValueSizeInBits() - NVT
.getSizeInBits();
5256 Hi
= DAG
.getZeroExtendInReg(Hi
, dl
,
5257 EVT::getIntegerVT(*DAG
.getContext(),
5262 void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode
*N
,
5263 SDValue
&Lo
, SDValue
&Hi
) {
5265 EVT VT
= cast
<AtomicSDNode
>(N
)->getMemoryVT();
5266 SDVTList VTs
= DAG
.getVTList(VT
, MVT::i1
, MVT::Other
);
5267 SDValue Zero
= DAG
.getConstant(0, dl
, VT
);
5268 SDValue Swap
= DAG
.getAtomicCmpSwap(
5269 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS
, dl
,
5270 cast
<AtomicSDNode
>(N
)->getMemoryVT(), VTs
, N
->getOperand(0),
5271 N
->getOperand(1), Zero
, Zero
, cast
<AtomicSDNode
>(N
)->getMemOperand());
5273 ReplaceValueWith(SDValue(N
, 0), Swap
.getValue(0));
5274 ReplaceValueWith(SDValue(N
, 1), Swap
.getValue(2));
5277 void DAGTypeLegalizer::ExpandIntRes_VECREDUCE(SDNode
*N
,
5278 SDValue
&Lo
, SDValue
&Hi
) {
5279 // TODO For VECREDUCE_(AND|OR|XOR) we could split the vector and calculate
5280 // both halves independently.
5281 SDValue Res
= TLI
.expandVecReduce(N
, DAG
);
5282 SplitInteger(Res
, Lo
, Hi
);
5285 void DAGTypeLegalizer::ExpandIntRes_Rotate(SDNode
*N
,
5286 SDValue
&Lo
, SDValue
&Hi
) {
5287 // Delegate to funnel-shift expansion.
5289 unsigned Opcode
= N
->getOpcode() == ISD::ROTL
? ISD::FSHL
: ISD::FSHR
;
5290 SDValue Res
= DAG
.getNode(Opcode
, DL
, N
->getValueType(0), N
->getOperand(0),
5291 N
->getOperand(0), N
->getOperand(1));
5292 SplitInteger(Res
, Lo
, Hi
);
5295 void DAGTypeLegalizer::ExpandIntRes_FunnelShift(SDNode
*N
, SDValue
&Lo
,
5297 // Values numbered from least significant to most significant.
5298 SDValue In1
, In2
, In3
, In4
;
5299 GetExpandedInteger(N
->getOperand(0), In3
, In4
);
5300 GetExpandedInteger(N
->getOperand(1), In1
, In2
);
5301 EVT HalfVT
= In1
.getValueType();
5304 unsigned Opc
= N
->getOpcode();
5305 SDValue ShAmt
= N
->getOperand(2);
5306 EVT ShAmtVT
= ShAmt
.getValueType();
5307 EVT ShAmtCCVT
= getSetCCResultType(ShAmtVT
);
5309 // If the shift amount is at least half the bitwidth, swap the inputs.
5310 unsigned HalfVTBits
= HalfVT
.getScalarSizeInBits();
5311 SDValue AndNode
= DAG
.getNode(ISD::AND
, DL
, ShAmtVT
, ShAmt
,
5312 DAG
.getConstant(HalfVTBits
, DL
, ShAmtVT
));
5314 DAG
.getSetCC(DL
, ShAmtCCVT
, AndNode
, DAG
.getConstant(0, DL
, ShAmtVT
),
5315 Opc
== ISD::FSHL
? ISD::SETNE
: ISD::SETEQ
);
5317 // Expand to a pair of funnel shifts.
5318 EVT NewShAmtVT
= TLI
.getShiftAmountTy(HalfVT
, DAG
.getDataLayout());
5319 SDValue NewShAmt
= DAG
.getAnyExtOrTrunc(ShAmt
, DL
, NewShAmtVT
);
5321 SDValue Select1
= DAG
.getNode(ISD::SELECT
, DL
, HalfVT
, Cond
, In1
, In2
);
5322 SDValue Select2
= DAG
.getNode(ISD::SELECT
, DL
, HalfVT
, Cond
, In2
, In3
);
5323 SDValue Select3
= DAG
.getNode(ISD::SELECT
, DL
, HalfVT
, Cond
, In3
, In4
);
5324 Lo
= DAG
.getNode(Opc
, DL
, HalfVT
, Select2
, Select1
, NewShAmt
);
5325 Hi
= DAG
.getNode(Opc
, DL
, HalfVT
, Select3
, Select2
, NewShAmt
);
5328 void DAGTypeLegalizer::ExpandIntRes_VSCALE(SDNode
*N
, SDValue
&Lo
,
5330 EVT VT
= N
->getValueType(0);
5332 EVT::getIntegerVT(*DAG
.getContext(), N
->getValueSizeInBits(0) / 2);
5335 // We assume VSCALE(1) fits into a legal integer.
5336 APInt
One(HalfVT
.getSizeInBits(), 1);
5337 SDValue VScaleBase
= DAG
.getVScale(dl
, HalfVT
, One
);
5338 VScaleBase
= DAG
.getNode(ISD::ZERO_EXTEND
, dl
, VT
, VScaleBase
);
5339 SDValue Res
= DAG
.getNode(ISD::MUL
, dl
, VT
, VScaleBase
, N
->getOperand(0));
5340 SplitInteger(Res
, Lo
, Hi
);
5343 //===----------------------------------------------------------------------===//
5344 // Integer Operand Expansion
5345 //===----------------------------------------------------------------------===//
5347 /// ExpandIntegerOperand - This method is called when the specified operand of
5348 /// the specified node is found to need expansion. At this point, all of the
5349 /// result types of the node are known to be legal, but other operands of the
5350 /// node may need promotion or expansion as well as the specified one.
5351 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode
*N
, unsigned OpNo
) {
5352 LLVM_DEBUG(dbgs() << "Expand integer operand: "; N
->dump(&DAG
));
5353 SDValue Res
= SDValue();
5355 if (CustomLowerNode(N
, N
->getOperand(OpNo
).getValueType(), false))
5358 switch (N
->getOpcode()) {
5361 dbgs() << "ExpandIntegerOperand Op #" << OpNo
<< ": ";
5362 N
->dump(&DAG
); dbgs() << "\n";
5364 report_fatal_error("Do not know how to expand this operator's operand!");
5366 case ISD::BITCAST
: Res
= ExpandOp_BITCAST(N
); break;
5367 case ISD::BR_CC
: Res
= ExpandIntOp_BR_CC(N
); break;
5368 case ISD::BUILD_VECTOR
: Res
= ExpandOp_BUILD_VECTOR(N
); break;
5369 case ISD::EXTRACT_ELEMENT
: Res
= ExpandOp_EXTRACT_ELEMENT(N
); break;
5371 Res
= ExpandOp_FAKE_USE(N
);
5373 case ISD::INSERT_VECTOR_ELT
: Res
= ExpandOp_INSERT_VECTOR_ELT(N
); break;
5374 case ISD::SCALAR_TO_VECTOR
: Res
= ExpandOp_SCALAR_TO_VECTOR(N
); break;
5375 case ISD::EXPERIMENTAL_VP_SPLAT
:
5376 case ISD::SPLAT_VECTOR
: Res
= ExpandIntOp_SPLAT_VECTOR(N
); break;
5377 case ISD::SELECT_CC
: Res
= ExpandIntOp_SELECT_CC(N
); break;
5378 case ISD::SETCC
: Res
= ExpandIntOp_SETCC(N
); break;
5379 case ISD::SETCCCARRY
: Res
= ExpandIntOp_SETCCCARRY(N
); break;
5380 case ISD::STRICT_SINT_TO_FP
:
5381 case ISD::SINT_TO_FP
:
5382 case ISD::STRICT_UINT_TO_FP
:
5383 case ISD::UINT_TO_FP
: Res
= ExpandIntOp_XINT_TO_FP(N
); break;
5384 case ISD::STORE
: Res
= ExpandIntOp_STORE(cast
<StoreSDNode
>(N
), OpNo
); break;
5385 case ISD::TRUNCATE
: Res
= ExpandIntOp_TRUNCATE(N
); break;
5391 case ISD::ROTR
: Res
= ExpandIntOp_Shift(N
); break;
5392 case ISD::RETURNADDR
:
5393 case ISD::FRAMEADDR
: Res
= ExpandIntOp_RETURNADDR(N
); break;
5396 case ISD::UCMP
: Res
= ExpandIntOp_CMP(N
); break;
5398 case ISD::ATOMIC_STORE
: Res
= ExpandIntOp_ATOMIC_STORE(N
); break;
5400 Res
= ExpandIntOp_STACKMAP(N
, OpNo
);
5402 case ISD::PATCHPOINT
:
5403 Res
= ExpandIntOp_PATCHPOINT(N
, OpNo
);
5405 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD
:
5406 case ISD::EXPERIMENTAL_VP_STRIDED_STORE
:
5407 Res
= ExpandIntOp_VP_STRIDED(N
, OpNo
);
5411 // If the result is null, the sub-method took care of registering results etc.
5412 if (!Res
.getNode()) return false;
5414 // If the result is N, the sub-method updated N in place. Tell the legalizer
5416 if (Res
.getNode() == N
)
5419 assert(Res
.getValueType() == N
->getValueType(0) && N
->getNumValues() == 1 &&
5420 "Invalid operand expansion");
5422 ReplaceValueWith(SDValue(N
, 0), Res
);
5426 /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
5427 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
5428 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue
&NewLHS
,
5430 ISD::CondCode
&CCCode
,
5432 SDValue LHSLo
, LHSHi
, RHSLo
, RHSHi
;
5433 GetExpandedInteger(NewLHS
, LHSLo
, LHSHi
);
5434 GetExpandedInteger(NewRHS
, RHSLo
, RHSHi
);
5436 if (CCCode
== ISD::SETEQ
|| CCCode
== ISD::SETNE
) {
5437 if (RHSLo
== RHSHi
&& isAllOnesConstant(RHSLo
)) {
5438 // Equality comparison to -1.
5439 NewLHS
= DAG
.getNode(ISD::AND
, dl
, LHSLo
.getValueType(), LHSLo
, LHSHi
);
5444 NewLHS
= DAG
.getNode(ISD::XOR
, dl
, LHSLo
.getValueType(), LHSLo
, RHSLo
);
5445 NewRHS
= DAG
.getNode(ISD::XOR
, dl
, LHSLo
.getValueType(), LHSHi
, RHSHi
);
5446 NewLHS
= DAG
.getNode(ISD::OR
, dl
, NewLHS
.getValueType(), NewLHS
, NewRHS
);
5447 NewRHS
= DAG
.getConstant(0, dl
, NewLHS
.getValueType());
5451 // If this is a comparison of the sign bit, just look at the top part.
5453 if (ConstantSDNode
*CST
= dyn_cast
<ConstantSDNode
>(NewRHS
))
5454 if ((CCCode
== ISD::SETLT
&& CST
->isZero()) || // X < 0
5455 (CCCode
== ISD::SETGT
&& CST
->isAllOnes())) { // X > -1
5461 // FIXME: This generated code sucks.
5462 ISD::CondCode LowCC
;
5464 default: llvm_unreachable("Unknown integer setcc!");
5466 case ISD::SETULT
: LowCC
= ISD::SETULT
; break;
5468 case ISD::SETUGT
: LowCC
= ISD::SETUGT
; break;
5470 case ISD::SETULE
: LowCC
= ISD::SETULE
; break;
5472 case ISD::SETUGE
: LowCC
= ISD::SETUGE
; break;
5475 // LoCmp = lo(op1) < lo(op2) // Always unsigned comparison
5476 // HiCmp = hi(op1) < hi(op2) // Signedness depends on operands
5477 // dest = hi(op1) == hi(op2) ? LoCmp : HiCmp;
5479 // NOTE: on targets without efficient SELECT of bools, we can always use
5480 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5481 TargetLowering::DAGCombinerInfo
DagCombineInfo(DAG
, AfterLegalizeTypes
, true,
5483 SDValue LoCmp
, HiCmp
;
5484 if (TLI
.isTypeLegal(LHSLo
.getValueType()) &&
5485 TLI
.isTypeLegal(RHSLo
.getValueType()))
5486 LoCmp
= TLI
.SimplifySetCC(getSetCCResultType(LHSLo
.getValueType()), LHSLo
,
5487 RHSLo
, LowCC
, false, DagCombineInfo
, dl
);
5488 if (!LoCmp
.getNode())
5489 LoCmp
= DAG
.getSetCC(dl
, getSetCCResultType(LHSLo
.getValueType()), LHSLo
,
5491 if (TLI
.isTypeLegal(LHSHi
.getValueType()) &&
5492 TLI
.isTypeLegal(RHSHi
.getValueType()))
5493 HiCmp
= TLI
.SimplifySetCC(getSetCCResultType(LHSHi
.getValueType()), LHSHi
,
5494 RHSHi
, CCCode
, false, DagCombineInfo
, dl
);
5495 if (!HiCmp
.getNode())
5497 DAG
.getNode(ISD::SETCC
, dl
, getSetCCResultType(LHSHi
.getValueType()),
5498 LHSHi
, RHSHi
, DAG
.getCondCode(CCCode
));
5500 ConstantSDNode
*LoCmpC
= dyn_cast
<ConstantSDNode
>(LoCmp
.getNode());
5501 ConstantSDNode
*HiCmpC
= dyn_cast
<ConstantSDNode
>(HiCmp
.getNode());
5503 bool EqAllowed
= ISD::isTrueWhenEqual(CCCode
);
5505 // FIXME: Is the HiCmpC->isOne() here correct for
5506 // ZeroOrNegativeOneBooleanContent.
5507 if ((EqAllowed
&& (HiCmpC
&& HiCmpC
->isZero())) ||
5509 ((HiCmpC
&& HiCmpC
->isOne()) || (LoCmpC
&& LoCmpC
->isZero())))) {
5510 // For LE / GE, if high part is known false, ignore the low part.
5511 // For LT / GT: if low part is known false, return the high part.
5512 // if high part is known true, ignore the low part.
5518 if (LHSHi
== RHSHi
) {
5519 // Comparing the low bits is enough.
5525 // Lower with SETCCCARRY if the target supports it.
5526 EVT HiVT
= LHSHi
.getValueType();
5527 EVT ExpandVT
= TLI
.getTypeToExpandTo(*DAG
.getContext(), HiVT
);
5528 bool HasSETCCCARRY
= TLI
.isOperationLegalOrCustom(ISD::SETCCCARRY
, ExpandVT
);
5530 // FIXME: Make all targets support this, then remove the other lowering.
5531 if (HasSETCCCARRY
) {
5532 // SETCCCARRY can detect < and >= directly. For > and <=, flip
5533 // operands and condition code.
5534 bool FlipOperands
= false;
5536 case ISD::SETGT
: CCCode
= ISD::SETLT
; FlipOperands
= true; break;
5537 case ISD::SETUGT
: CCCode
= ISD::SETULT
; FlipOperands
= true; break;
5538 case ISD::SETLE
: CCCode
= ISD::SETGE
; FlipOperands
= true; break;
5539 case ISD::SETULE
: CCCode
= ISD::SETUGE
; FlipOperands
= true; break;
5543 std::swap(LHSLo
, RHSLo
);
5544 std::swap(LHSHi
, RHSHi
);
5546 // Perform a wide subtraction, feeding the carry from the low part into
5547 // SETCCCARRY. The SETCCCARRY operation is essentially looking at the high
5548 // part of the result of LHS - RHS. It is negative iff LHS < RHS. It is
5549 // zero or positive iff LHS >= RHS.
5550 EVT LoVT
= LHSLo
.getValueType();
5551 SDVTList VTList
= DAG
.getVTList(LoVT
, getSetCCResultType(LoVT
));
5552 SDValue LowCmp
= DAG
.getNode(ISD::USUBO
, dl
, VTList
, LHSLo
, RHSLo
);
5553 SDValue Res
= DAG
.getNode(ISD::SETCCCARRY
, dl
, getSetCCResultType(HiVT
),
5554 LHSHi
, RHSHi
, LowCmp
.getValue(1),
5555 DAG
.getCondCode(CCCode
));
5561 NewLHS
= TLI
.SimplifySetCC(getSetCCResultType(HiVT
), LHSHi
, RHSHi
, ISD::SETEQ
,
5562 false, DagCombineInfo
, dl
);
5563 if (!NewLHS
.getNode())
5565 DAG
.getSetCC(dl
, getSetCCResultType(HiVT
), LHSHi
, RHSHi
, ISD::SETEQ
);
5566 NewLHS
= DAG
.getSelect(dl
, LoCmp
.getValueType(), NewLHS
, LoCmp
, HiCmp
);
5570 SDValue
DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode
*N
) {
5571 SDValue NewLHS
= N
->getOperand(2), NewRHS
= N
->getOperand(3);
5572 ISD::CondCode CCCode
= cast
<CondCodeSDNode
>(N
->getOperand(1))->get();
5573 IntegerExpandSetCCOperands(NewLHS
, NewRHS
, CCCode
, SDLoc(N
));
5575 // If ExpandSetCCOperands returned a scalar, we need to compare the result
5576 // against zero to select between true and false values.
5577 if (!NewRHS
.getNode()) {
5578 NewRHS
= DAG
.getConstant(0, SDLoc(N
), NewLHS
.getValueType());
5579 CCCode
= ISD::SETNE
;
5582 // Update N to have the operands specified.
5583 return SDValue(DAG
.UpdateNodeOperands(N
, N
->getOperand(0),
5584 DAG
.getCondCode(CCCode
), NewLHS
, NewRHS
,
5585 N
->getOperand(4)), 0);
5588 SDValue
DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode
*N
) {
5589 SDValue NewLHS
= N
->getOperand(0), NewRHS
= N
->getOperand(1);
5590 ISD::CondCode CCCode
= cast
<CondCodeSDNode
>(N
->getOperand(4))->get();
5591 IntegerExpandSetCCOperands(NewLHS
, NewRHS
, CCCode
, SDLoc(N
));
5593 // If ExpandSetCCOperands returned a scalar, we need to compare the result
5594 // against zero to select between true and false values.
5595 if (!NewRHS
.getNode()) {
5596 NewRHS
= DAG
.getConstant(0, SDLoc(N
), NewLHS
.getValueType());
5597 CCCode
= ISD::SETNE
;
5600 // Update N to have the operands specified.
5601 return SDValue(DAG
.UpdateNodeOperands(N
, NewLHS
, NewRHS
,
5602 N
->getOperand(2), N
->getOperand(3),
5603 DAG
.getCondCode(CCCode
)), 0);
5606 SDValue
DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode
*N
) {
5607 SDValue NewLHS
= N
->getOperand(0), NewRHS
= N
->getOperand(1);
5608 ISD::CondCode CCCode
= cast
<CondCodeSDNode
>(N
->getOperand(2))->get();
5609 IntegerExpandSetCCOperands(NewLHS
, NewRHS
, CCCode
, SDLoc(N
));
5611 // If ExpandSetCCOperands returned a scalar, use it.
5612 if (!NewRHS
.getNode()) {
5613 assert(NewLHS
.getValueType() == N
->getValueType(0) &&
5614 "Unexpected setcc expansion!");
5618 // Otherwise, update N to have the operands specified.
5620 DAG
.UpdateNodeOperands(N
, NewLHS
, NewRHS
, DAG
.getCondCode(CCCode
)), 0);
5623 SDValue
DAGTypeLegalizer::ExpandIntOp_SETCCCARRY(SDNode
*N
) {
5624 SDValue LHS
= N
->getOperand(0);
5625 SDValue RHS
= N
->getOperand(1);
5626 SDValue Carry
= N
->getOperand(2);
5627 SDValue Cond
= N
->getOperand(3);
5628 SDLoc dl
= SDLoc(N
);
5630 SDValue LHSLo
, LHSHi
, RHSLo
, RHSHi
;
5631 GetExpandedInteger(LHS
, LHSLo
, LHSHi
);
5632 GetExpandedInteger(RHS
, RHSLo
, RHSHi
);
5634 // Expand to a USUBO_CARRY for the low part and a SETCCCARRY for the high.
5635 SDVTList VTList
= DAG
.getVTList(LHSLo
.getValueType(), Carry
.getValueType());
5637 DAG
.getNode(ISD::USUBO_CARRY
, dl
, VTList
, LHSLo
, RHSLo
, Carry
);
5638 return DAG
.getNode(ISD::SETCCCARRY
, dl
, N
->getValueType(0), LHSHi
, RHSHi
,
5639 LowCmp
.getValue(1), Cond
);
5642 SDValue
DAGTypeLegalizer::ExpandIntOp_SPLAT_VECTOR(SDNode
*N
) {
5643 // Split the operand and replace with SPLAT_VECTOR_PARTS.
5645 GetExpandedInteger(N
->getOperand(0), Lo
, Hi
);
5646 return DAG
.getNode(ISD::SPLAT_VECTOR_PARTS
, SDLoc(N
), N
->getValueType(0), Lo
,
5650 SDValue
DAGTypeLegalizer::ExpandIntOp_Shift(SDNode
*N
) {
5651 // The value being shifted is legal, but the shift amount is too big.
5652 // It follows that either the result of the shift is undefined, or the
5653 // upper half of the shift amount is zero. Just use the lower half.
5655 GetExpandedInteger(N
->getOperand(1), Lo
, Hi
);
5656 return SDValue(DAG
.UpdateNodeOperands(N
, N
->getOperand(0), Lo
), 0);
5659 SDValue
DAGTypeLegalizer::ExpandIntOp_CMP(SDNode
*N
) {
5660 return TLI
.expandCMP(N
, DAG
);
5663 SDValue
DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode
*N
) {
5664 // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
5665 // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
5666 // constant to valid type.
5668 GetExpandedInteger(N
->getOperand(0), Lo
, Hi
);
5669 return SDValue(DAG
.UpdateNodeOperands(N
, Lo
), 0);
5672 SDValue
DAGTypeLegalizer::ExpandIntOp_XINT_TO_FP(SDNode
*N
) {
5673 bool IsStrict
= N
->isStrictFPOpcode();
5674 bool IsSigned
= N
->getOpcode() == ISD::SINT_TO_FP
||
5675 N
->getOpcode() == ISD::STRICT_SINT_TO_FP
;
5676 SDValue Chain
= IsStrict
? N
->getOperand(0) : SDValue();
5677 SDValue Op
= N
->getOperand(IsStrict
? 1 : 0);
5678 EVT DstVT
= N
->getValueType(0);
5679 RTLIB::Libcall LC
= IsSigned
? RTLIB::getSINTTOFP(Op
.getValueType(), DstVT
)
5680 : RTLIB::getUINTTOFP(Op
.getValueType(), DstVT
);
5681 assert(LC
!= RTLIB::UNKNOWN_LIBCALL
&&
5682 "Don't know how to expand this XINT_TO_FP!");
5683 TargetLowering::MakeLibCallOptions CallOptions
;
5684 CallOptions
.setIsSigned(true);
5685 std::pair
<SDValue
, SDValue
> Tmp
=
5686 TLI
.makeLibCall(DAG
, LC
, DstVT
, Op
, CallOptions
, SDLoc(N
), Chain
);
5691 ReplaceValueWith(SDValue(N
, 1), Tmp
.second
);
5692 ReplaceValueWith(SDValue(N
, 0), Tmp
.first
);
5696 SDValue
DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode
*N
, unsigned OpNo
) {
5697 assert(!N
->isAtomic() && "Should have been a ATOMIC_STORE?");
5699 if (ISD::isNormalStore(N
))
5700 return ExpandOp_NormalStore(N
, OpNo
);
5702 assert(ISD::isUNINDEXEDStore(N
) && "Indexed store during type legalization!");
5703 assert(OpNo
== 1 && "Can only expand the stored value so far");
5705 EVT VT
= N
->getOperand(1).getValueType();
5706 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), VT
);
5707 SDValue Ch
= N
->getChain();
5708 SDValue Ptr
= N
->getBasePtr();
5709 MachineMemOperand::Flags MMOFlags
= N
->getMemOperand()->getFlags();
5710 AAMDNodes AAInfo
= N
->getAAInfo();
5714 assert(NVT
.isByteSized() && "Expanded type not byte sized!");
5716 if (N
->getMemoryVT().bitsLE(NVT
)) {
5717 GetExpandedInteger(N
->getValue(), Lo
, Hi
);
5718 return DAG
.getTruncStore(Ch
, dl
, Lo
, Ptr
, N
->getPointerInfo(),
5719 N
->getMemoryVT(), N
->getOriginalAlign(), MMOFlags
,
5723 if (DAG
.getDataLayout().isLittleEndian()) {
5724 // Little-endian - low bits are at low addresses.
5725 GetExpandedInteger(N
->getValue(), Lo
, Hi
);
5727 Lo
= DAG
.getStore(Ch
, dl
, Lo
, Ptr
, N
->getPointerInfo(),
5728 N
->getOriginalAlign(), MMOFlags
, AAInfo
);
5730 unsigned ExcessBits
=
5731 N
->getMemoryVT().getSizeInBits() - NVT
.getSizeInBits();
5732 EVT NEVT
= EVT::getIntegerVT(*DAG
.getContext(), ExcessBits
);
5734 // Increment the pointer to the other half.
5735 unsigned IncrementSize
= NVT
.getSizeInBits()/8;
5736 Ptr
= DAG
.getObjectPtrOffset(dl
, Ptr
, TypeSize::getFixed(IncrementSize
));
5737 Hi
= DAG
.getTruncStore(Ch
, dl
, Hi
, Ptr
,
5738 N
->getPointerInfo().getWithOffset(IncrementSize
),
5739 NEVT
, N
->getOriginalAlign(), MMOFlags
, AAInfo
);
5740 return DAG
.getNode(ISD::TokenFactor
, dl
, MVT::Other
, Lo
, Hi
);
5743 // Big-endian - high bits are at low addresses. Favor aligned stores at
5744 // the cost of some bit-fiddling.
5745 GetExpandedInteger(N
->getValue(), Lo
, Hi
);
5747 EVT ExtVT
= N
->getMemoryVT();
5748 unsigned EBytes
= ExtVT
.getStoreSize();
5749 unsigned IncrementSize
= NVT
.getSizeInBits()/8;
5750 unsigned ExcessBits
= (EBytes
- IncrementSize
)*8;
5751 EVT HiVT
= EVT::getIntegerVT(*DAG
.getContext(),
5752 ExtVT
.getSizeInBits() - ExcessBits
);
5754 if (ExcessBits
< NVT
.getSizeInBits()) {
5755 // Transfer high bits from the top of Lo to the bottom of Hi.
5756 Hi
= DAG
.getNode(ISD::SHL
, dl
, NVT
, Hi
,
5757 DAG
.getConstant(NVT
.getSizeInBits() - ExcessBits
, dl
,
5758 TLI
.getPointerTy(DAG
.getDataLayout())));
5760 ISD::OR
, dl
, NVT
, Hi
,
5761 DAG
.getNode(ISD::SRL
, dl
, NVT
, Lo
,
5762 DAG
.getConstant(ExcessBits
, dl
,
5763 TLI
.getPointerTy(DAG
.getDataLayout()))));
5766 // Store both the high bits and maybe some of the low bits.
5767 Hi
= DAG
.getTruncStore(Ch
, dl
, Hi
, Ptr
, N
->getPointerInfo(), HiVT
,
5768 N
->getOriginalAlign(), MMOFlags
, AAInfo
);
5770 // Increment the pointer to the other half.
5771 Ptr
= DAG
.getObjectPtrOffset(dl
, Ptr
, TypeSize::getFixed(IncrementSize
));
5772 // Store the lowest ExcessBits bits in the second half.
5773 Lo
= DAG
.getTruncStore(Ch
, dl
, Lo
, Ptr
,
5774 N
->getPointerInfo().getWithOffset(IncrementSize
),
5775 EVT::getIntegerVT(*DAG
.getContext(), ExcessBits
),
5776 N
->getOriginalAlign(), MMOFlags
, AAInfo
);
5777 return DAG
.getNode(ISD::TokenFactor
, dl
, MVT::Other
, Lo
, Hi
);
5780 SDValue
DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode
*N
) {
5782 GetExpandedInteger(N
->getOperand(0), InL
, InH
);
5783 // Just truncate the low part of the source.
5784 return DAG
.getNode(ISD::TRUNCATE
, SDLoc(N
), N
->getValueType(0), InL
);
5787 SDValue
DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode
*N
) {
5790 DAG
.getAtomic(ISD::ATOMIC_SWAP
, dl
, cast
<AtomicSDNode
>(N
)->getMemoryVT(),
5791 N
->getOperand(0), N
->getOperand(2), N
->getOperand(1),
5792 cast
<AtomicSDNode
>(N
)->getMemOperand());
5793 return Swap
.getValue(1);
5796 SDValue
DAGTypeLegalizer::ExpandIntOp_VP_STRIDED(SDNode
*N
, unsigned OpNo
) {
5797 assert((N
->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD
&& OpNo
== 3) ||
5798 (N
->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE
&& OpNo
== 4));
5800 SDValue Hi
; // The upper half is dropped out.
5801 SmallVector
<SDValue
, 8> NewOps(N
->ops());
5802 GetExpandedInteger(NewOps
[OpNo
], NewOps
[OpNo
], Hi
);
5804 return SDValue(DAG
.UpdateNodeOperands(N
, NewOps
), 0);
5807 SDValue
DAGTypeLegalizer::PromoteIntRes_VECTOR_SPLICE(SDNode
*N
) {
5810 SDValue V0
= GetPromotedInteger(N
->getOperand(0));
5811 SDValue V1
= GetPromotedInteger(N
->getOperand(1));
5812 EVT OutVT
= V0
.getValueType();
5814 return DAG
.getNode(ISD::VECTOR_SPLICE
, dl
, OutVT
, V0
, V1
, N
->getOperand(2));
5817 SDValue
DAGTypeLegalizer::PromoteIntRes_VECTOR_INTERLEAVE_DEINTERLEAVE(SDNode
*N
) {
5820 SDValue V0
= GetPromotedInteger(N
->getOperand(0));
5821 SDValue V1
= GetPromotedInteger(N
->getOperand(1));
5822 EVT ResVT
= V0
.getValueType();
5823 SDValue Res
= DAG
.getNode(N
->getOpcode(), dl
,
5824 DAG
.getVTList(ResVT
, ResVT
), V0
, V1
);
5825 SetPromotedInteger(SDValue(N
, 0), Res
.getValue(0));
5826 SetPromotedInteger(SDValue(N
, 1), Res
.getValue(1));
5830 SDValue
DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode
*N
) {
5832 EVT OutVT
= N
->getValueType(0);
5833 EVT NOutVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OutVT
);
5834 assert(NOutVT
.isVector() && "This type must be promoted to a vector type");
5835 EVT NOutVTElem
= NOutVT
.getVectorElementType();
5838 SDValue BaseIdx
= N
->getOperand(1);
5840 // TODO: We may be able to use this for types other than scalable
5841 // vectors and fix those tests that expect BUILD_VECTOR to be used
5842 if (OutVT
.isScalableVector()) {
5843 SDValue InOp0
= N
->getOperand(0);
5844 EVT InVT
= InOp0
.getValueType();
5846 // Try and extract from a smaller type so that it eventually falls
5847 // into the promotion code below.
5848 if (getTypeAction(InVT
) == TargetLowering::TypeSplitVector
||
5849 getTypeAction(InVT
) == TargetLowering::TypeLegal
) {
5850 EVT NInVT
= InVT
.getHalfNumVectorElementsVT(*DAG
.getContext());
5851 unsigned NElts
= NInVT
.getVectorMinNumElements();
5852 uint64_t IdxVal
= BaseIdx
->getAsZExtVal();
5854 SDValue Step1
= DAG
.getNode(ISD::EXTRACT_SUBVECTOR
, dl
, NInVT
, InOp0
,
5855 DAG
.getConstant(alignDown(IdxVal
, NElts
), dl
,
5856 BaseIdx
.getValueType()));
5857 SDValue Step2
= DAG
.getNode(
5858 ISD::EXTRACT_SUBVECTOR
, dl
, OutVT
, Step1
,
5859 DAG
.getConstant(IdxVal
% NElts
, dl
, BaseIdx
.getValueType()));
5860 return DAG
.getNode(ISD::ANY_EXTEND
, dl
, NOutVT
, Step2
);
5863 // Try and extract from a widened type.
5864 if (getTypeAction(InVT
) == TargetLowering::TypeWidenVector
) {
5865 SDValue Ops
[] = {GetWidenedVector(InOp0
), BaseIdx
};
5866 SDValue Ext
= DAG
.getNode(ISD::EXTRACT_SUBVECTOR
, SDLoc(N
), OutVT
, Ops
);
5867 return DAG
.getNode(ISD::ANY_EXTEND
, dl
, NOutVT
, Ext
);
5870 // Promote operands and see if this is handled by target lowering,
5871 // Otherwise, use the BUILD_VECTOR approach below
5872 if (getTypeAction(InVT
) == TargetLowering::TypePromoteInteger
) {
5873 // Collect the (promoted) operands
5874 SDValue Ops
[] = { GetPromotedInteger(InOp0
), BaseIdx
};
5876 EVT PromEltVT
= Ops
[0].getValueType().getVectorElementType();
5877 assert(PromEltVT
.bitsLE(NOutVTElem
) &&
5878 "Promoted operand has an element type greater than result");
5880 EVT ExtVT
= NOutVT
.changeVectorElementType(PromEltVT
);
5881 SDValue Ext
= DAG
.getNode(ISD::EXTRACT_SUBVECTOR
, SDLoc(N
), ExtVT
, Ops
);
5882 return DAG
.getNode(ISD::ANY_EXTEND
, dl
, NOutVT
, Ext
);
5886 if (OutVT
.isScalableVector())
5887 report_fatal_error("Unable to promote scalable types using BUILD_VECTOR");
5889 SDValue InOp0
= N
->getOperand(0);
5890 if (getTypeAction(InOp0
.getValueType()) == TargetLowering::TypePromoteInteger
)
5891 InOp0
= GetPromotedInteger(InOp0
);
5893 EVT InVT
= InOp0
.getValueType();
5894 EVT InSVT
= InVT
.getVectorElementType();
5896 unsigned OutNumElems
= OutVT
.getVectorNumElements();
5897 SmallVector
<SDValue
, 8> Ops
;
5898 Ops
.reserve(OutNumElems
);
5899 for (unsigned i
= 0; i
!= OutNumElems
; ++i
) {
5900 // Extract the element from the original vector.
5901 SDValue Index
= DAG
.getNode(ISD::ADD
, dl
, BaseIdx
.getValueType(), BaseIdx
,
5902 DAG
.getConstant(i
, dl
, BaseIdx
.getValueType()));
5903 SDValue Ext
= DAG
.getNode(ISD::EXTRACT_VECTOR_ELT
, dl
, InSVT
,
5904 N
->getOperand(0), Index
);
5905 SDValue Op
= DAG
.getAnyExtOrTrunc(Ext
, dl
, NOutVTElem
);
5906 // Insert the converted element to the new vector.
5910 return DAG
.getBuildVector(NOutVT
, dl
, Ops
);
5913 SDValue
DAGTypeLegalizer::PromoteIntRes_INSERT_SUBVECTOR(SDNode
*N
) {
5914 EVT OutVT
= N
->getValueType(0);
5915 EVT NOutVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OutVT
);
5916 assert(NOutVT
.isVector() && "This type must be promoted to a vector type");
5919 SDValue Vec
= N
->getOperand(0);
5920 SDValue SubVec
= N
->getOperand(1);
5921 SDValue Idx
= N
->getOperand(2);
5923 EVT SubVecVT
= SubVec
.getValueType();
5925 EVT::getVectorVT(*DAG
.getContext(), NOutVT
.getVectorElementType(),
5926 SubVecVT
.getVectorElementCount());
5928 Vec
= GetPromotedInteger(Vec
);
5929 SubVec
= DAG
.getNode(ISD::ANY_EXTEND
, dl
, NSubVT
, SubVec
);
5931 return DAG
.getNode(ISD::INSERT_SUBVECTOR
, dl
, NOutVT
, Vec
, SubVec
, Idx
);
5934 SDValue
DAGTypeLegalizer::PromoteIntRes_VECTOR_REVERSE(SDNode
*N
) {
5937 SDValue V0
= GetPromotedInteger(N
->getOperand(0));
5938 EVT OutVT
= V0
.getValueType();
5940 return DAG
.getNode(ISD::VECTOR_REVERSE
, dl
, OutVT
, V0
);
5943 SDValue
DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode
*N
) {
5944 ShuffleVectorSDNode
*SV
= cast
<ShuffleVectorSDNode
>(N
);
5945 EVT VT
= N
->getValueType(0);
5948 ArrayRef
<int> NewMask
= SV
->getMask().slice(0, VT
.getVectorNumElements());
5950 SDValue V0
= GetPromotedInteger(N
->getOperand(0));
5951 SDValue V1
= GetPromotedInteger(N
->getOperand(1));
5952 EVT OutVT
= V0
.getValueType();
5954 return DAG
.getVectorShuffle(OutVT
, dl
, V0
, V1
, NewMask
);
5957 SDValue
DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode
*N
) {
5958 EVT OutVT
= N
->getValueType(0);
5959 EVT NOutVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OutVT
);
5960 assert(NOutVT
.isVector() && "This type must be promoted to a vector type");
5961 unsigned NumElems
= N
->getNumOperands();
5962 EVT NOutVTElem
= NOutVT
.getVectorElementType();
5963 TargetLoweringBase::BooleanContent NOutBoolType
= TLI
.getBooleanContents(NOutVT
);
5964 unsigned NOutExtOpc
= TargetLowering::getExtendForContent(NOutBoolType
);
5967 SmallVector
<SDValue
, 8> Ops
;
5968 Ops
.reserve(NumElems
);
5969 for (unsigned i
= 0; i
!= NumElems
; ++i
) {
5970 SDValue Op
= N
->getOperand(i
);
5971 EVT OpVT
= Op
.getValueType();
5972 // BUILD_VECTOR integer operand types are allowed to be larger than the
5973 // result's element type. This may still be true after the promotion. For
5974 // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
5975 // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
5976 if (OpVT
.bitsLT(NOutVTElem
)) {
5977 unsigned ExtOpc
= ISD::ANY_EXTEND
;
5978 // Attempt to extend constant bool vectors to match target's BooleanContent.
5979 // While not necessary, this improves chances of the constant correctly
5980 // folding with compare results (e.g. for NOT patterns).
5981 if (OpVT
== MVT::i1
&& Op
.getOpcode() == ISD::Constant
)
5982 ExtOpc
= NOutExtOpc
;
5983 Op
= DAG
.getNode(ExtOpc
, dl
, NOutVTElem
, Op
);
5988 return DAG
.getBuildVector(NOutVT
, dl
, Ops
);
5991 SDValue
DAGTypeLegalizer::PromoteIntRes_ScalarOp(SDNode
*N
) {
5995 assert(!N
->getOperand(0).getValueType().isVector() &&
5996 "Input must be a scalar");
5998 EVT OutVT
= N
->getValueType(0);
5999 EVT NOutVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OutVT
);
6000 assert(NOutVT
.isVector() && "This type must be promoted to a vector type");
6001 EVT NOutElemVT
= NOutVT
.getVectorElementType();
6003 SDValue Op
= DAG
.getNode(ISD::ANY_EXTEND
, dl
, NOutElemVT
, N
->getOperand(0));
6004 if (N
->isVPOpcode())
6005 return DAG
.getNode(N
->getOpcode(), dl
, NOutVT
, Op
, N
->getOperand(1),
6008 return DAG
.getNode(N
->getOpcode(), dl
, NOutVT
, Op
);
6011 SDValue
DAGTypeLegalizer::PromoteIntRes_STEP_VECTOR(SDNode
*N
) {
6013 EVT OutVT
= N
->getValueType(0);
6014 EVT NOutVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OutVT
);
6015 assert(NOutVT
.isScalableVector() &&
6016 "Type must be promoted to a scalable vector type");
6017 const APInt
&StepVal
= N
->getConstantOperandAPInt(0);
6018 return DAG
.getStepVector(dl
, NOutVT
,
6019 StepVal
.sext(NOutVT
.getScalarSizeInBits()));
6022 SDValue
DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode
*N
) {
6025 EVT OutVT
= N
->getValueType(0);
6026 EVT NOutVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OutVT
);
6027 assert(NOutVT
.isVector() && "This type must be promoted to a vector type");
6029 unsigned NumOperands
= N
->getNumOperands();
6030 unsigned NumOutElem
= NOutVT
.getVectorMinNumElements();
6031 EVT OutElemTy
= NOutVT
.getVectorElementType();
6032 if (OutVT
.isScalableVector()) {
6033 // Find the largest promoted element type for each of the operands.
6034 SDUse
*MaxSizedValue
= std::max_element(
6035 N
->op_begin(), N
->op_end(), [](const SDValue
&A
, const SDValue
&B
) {
6036 EVT AVT
= A
.getValueType().getVectorElementType();
6037 EVT BVT
= B
.getValueType().getVectorElementType();
6038 return AVT
.getScalarSizeInBits() < BVT
.getScalarSizeInBits();
6040 EVT MaxElementVT
= MaxSizedValue
->getValueType().getVectorElementType();
6042 // Then promote all vectors to the largest element type.
6043 SmallVector
<SDValue
, 8> Ops
;
6044 for (unsigned I
= 0; I
< NumOperands
; ++I
) {
6045 SDValue Op
= N
->getOperand(I
);
6046 EVT OpVT
= Op
.getValueType();
6047 if (getTypeAction(OpVT
) == TargetLowering::TypePromoteInteger
)
6048 Op
= GetPromotedInteger(Op
);
6050 assert(getTypeAction(OpVT
) == TargetLowering::TypeLegal
&&
6051 "Unhandled legalization type");
6053 if (OpVT
.getVectorElementType().getScalarSizeInBits() <
6054 MaxElementVT
.getScalarSizeInBits())
6055 Op
= DAG
.getAnyExtOrTrunc(Op
, dl
,
6056 OpVT
.changeVectorElementType(MaxElementVT
));
6060 // Do the CONCAT on the promoted type and finally truncate to (the promoted)
6062 return DAG
.getAnyExtOrTrunc(
6063 DAG
.getNode(ISD::CONCAT_VECTORS
, dl
,
6064 OutVT
.changeVectorElementType(MaxElementVT
), Ops
),
6068 unsigned NumElem
= N
->getOperand(0).getValueType().getVectorNumElements();
6069 assert(NumElem
* NumOperands
== NumOutElem
&&
6070 "Unexpected number of elements");
6072 // Take the elements from the first vector.
6073 SmallVector
<SDValue
, 8> Ops(NumOutElem
);
6074 for (unsigned i
= 0; i
< NumOperands
; ++i
) {
6075 SDValue Op
= N
->getOperand(i
);
6076 if (getTypeAction(Op
.getValueType()) == TargetLowering::TypePromoteInteger
)
6077 Op
= GetPromotedInteger(Op
);
6078 EVT SclrTy
= Op
.getValueType().getVectorElementType();
6079 assert(NumElem
== Op
.getValueType().getVectorNumElements() &&
6080 "Unexpected number of elements");
6082 for (unsigned j
= 0; j
< NumElem
; ++j
) {
6083 SDValue Ext
= DAG
.getNode(ISD::EXTRACT_VECTOR_ELT
, dl
, SclrTy
, Op
,
6084 DAG
.getVectorIdxConstant(j
, dl
));
6085 Ops
[i
* NumElem
+ j
] = DAG
.getAnyExtOrTrunc(Ext
, dl
, OutElemTy
);
6089 return DAG
.getBuildVector(NOutVT
, dl
, Ops
);
6092 SDValue
DAGTypeLegalizer::PromoteIntRes_EXTEND_VECTOR_INREG(SDNode
*N
) {
6093 EVT VT
= N
->getValueType(0);
6094 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), VT
);
6095 assert(NVT
.isVector() && "This type must be promoted to a vector type");
6099 // For operands whose TypeAction is to promote, extend the promoted node
6100 // appropriately (ZERO_EXTEND or SIGN_EXTEND) from the original pre-promotion
6101 // type, and then construct a new *_EXTEND_VECTOR_INREG node to the promote-to
6103 if (getTypeAction(N
->getOperand(0).getValueType())
6104 == TargetLowering::TypePromoteInteger
) {
6107 switch(N
->getOpcode()) {
6108 case ISD::SIGN_EXTEND_VECTOR_INREG
:
6109 Promoted
= SExtPromotedInteger(N
->getOperand(0));
6111 case ISD::ZERO_EXTEND_VECTOR_INREG
:
6112 Promoted
= ZExtPromotedInteger(N
->getOperand(0));
6114 case ISD::ANY_EXTEND_VECTOR_INREG
:
6115 Promoted
= GetPromotedInteger(N
->getOperand(0));
6118 llvm_unreachable("Node has unexpected Opcode");
6120 return DAG
.getNode(N
->getOpcode(), dl
, NVT
, Promoted
);
6123 // Directly extend to the appropriate transform-to type.
6124 return DAG
.getNode(N
->getOpcode(), dl
, NVT
, N
->getOperand(0));
6127 SDValue
DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode
*N
) {
6128 EVT OutVT
= N
->getValueType(0);
6129 EVT NOutVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), OutVT
);
6130 assert(NOutVT
.isVector() && "This type must be promoted to a vector type");
6132 EVT NOutVTElem
= NOutVT
.getVectorElementType();
6135 SDValue V0
= GetPromotedInteger(N
->getOperand(0));
6137 SDValue ConvElem
= DAG
.getNode(ISD::ANY_EXTEND
, dl
,
6138 NOutVTElem
, N
->getOperand(1));
6139 return DAG
.getNode(ISD::INSERT_VECTOR_ELT
, dl
, NOutVT
,
6140 V0
, ConvElem
, N
->getOperand(2));
6143 SDValue
DAGTypeLegalizer::PromoteIntRes_VECREDUCE(SDNode
*N
) {
6144 // The VECREDUCE result size may be larger than the element size, so
6145 // we can simply change the result type.
6147 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
6148 return DAG
.getNode(N
->getOpcode(), dl
, NVT
, N
->ops());
6151 SDValue
DAGTypeLegalizer::PromoteIntRes_VP_REDUCE(SDNode
*N
) {
6152 // The VP_REDUCE result size may be larger than the element size, so we can
6153 // simply change the result type. However the start value and result must be
6156 SDValue Start
= PromoteIntOpVectorReduction(N
, N
->getOperand(0));
6157 return DAG
.getNode(N
->getOpcode(), DL
, Start
.getValueType(), Start
,
6158 N
->getOperand(1), N
->getOperand(2), N
->getOperand(3));
6161 SDValue
DAGTypeLegalizer::PromoteIntRes_PATCHPOINT(SDNode
*N
) {
6162 EVT NVT
= TLI
.getTypeToTransformTo(*DAG
.getContext(), N
->getValueType(0));
6165 assert(N
->getNumValues() == 3 && "Expected 3 values for PATCHPOINT");
6166 SDVTList VTList
= DAG
.getVTList({NVT
, MVT::Other
, MVT::Glue
});
6168 SmallVector
<SDValue
> Ops(N
->ops());
6169 SDValue Res
= DAG
.getNode(ISD::PATCHPOINT
, dl
, VTList
, Ops
);
6171 // Replace chain and glue uses with the new patchpoint.
6172 SDValue From
[] = {SDValue(N
, 1), SDValue(N
, 2)};
6173 SDValue To
[] = {Res
.getValue(1), Res
.getValue(2)};
6174 DAG
.ReplaceAllUsesOfValuesWith(From
, To
, 2);
6176 return Res
.getValue(0);
6179 SDValue
DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode
*N
) {
6181 SDValue V0
= GetPromotedInteger(N
->getOperand(0));
6182 SDValue V1
= DAG
.getZExtOrTrunc(N
->getOperand(1), dl
,
6183 TLI
.getVectorIdxTy(DAG
.getDataLayout()));
6184 SDValue Ext
= DAG
.getNode(ISD::EXTRACT_VECTOR_ELT
, dl
,
6185 V0
->getValueType(0).getScalarType(), V0
, V1
);
6187 // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
6188 // element types. If this is the case then we need to expand the outgoing
6189 // value and not truncate it.
6190 return DAG
.getAnyExtOrTrunc(Ext
, dl
, N
->getValueType(0));
6193 SDValue
DAGTypeLegalizer::PromoteIntOp_INSERT_SUBVECTOR(SDNode
*N
) {
6195 // The result type is equal to the first input operand's type, so the
6196 // type that needs promoting must be the second source vector.
6197 SDValue V0
= N
->getOperand(0);
6198 SDValue V1
= GetPromotedInteger(N
->getOperand(1));
6199 SDValue Idx
= N
->getOperand(2);
6200 EVT PromVT
= EVT::getVectorVT(*DAG
.getContext(),
6201 V1
.getValueType().getVectorElementType(),
6202 V0
.getValueType().getVectorElementCount());
6203 V0
= DAG
.getAnyExtOrTrunc(V0
, dl
, PromVT
);
6204 SDValue Ext
= DAG
.getNode(ISD::INSERT_SUBVECTOR
, dl
, PromVT
, V0
, V1
, Idx
);
6205 return DAG
.getAnyExtOrTrunc(Ext
, dl
, N
->getValueType(0));
6208 // FIXME: We wouldn't need this if clang could promote short integers
6209 // that are arguments to FAKE_USE.
6210 SDValue
DAGTypeLegalizer::PromoteIntOp_FAKE_USE(SDNode
*N
) {
6212 SDValue V0
= N
->getOperand(0);
6213 SDValue V1
= N
->getOperand(1);
6214 EVT InVT1
= V1
.getValueType();
6216 DAG
.getNode(ISD::ANY_EXTEND
, dl
,
6217 TLI
.getTypeToTransformTo(*DAG
.getContext(), InVT1
), V1
);
6218 return DAG
.getNode(N
->getOpcode(), dl
, N
->getValueType(0), V0
, VPromoted
);
6221 SDValue
DAGTypeLegalizer::PromoteIntOp_EXTRACT_SUBVECTOR(SDNode
*N
) {
6223 SDValue V0
= GetPromotedInteger(N
->getOperand(0));
6224 MVT InVT
= V0
.getValueType().getSimpleVT();
6225 MVT OutVT
= MVT::getVectorVT(InVT
.getVectorElementType(),
6226 N
->getValueType(0).getVectorNumElements());
6227 SDValue Ext
= DAG
.getNode(ISD::EXTRACT_SUBVECTOR
, dl
, OutVT
, V0
, N
->getOperand(1));
6228 return DAG
.getNode(ISD::TRUNCATE
, dl
, N
->getValueType(0), Ext
);
6231 SDValue
DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode
*N
) {
6234 EVT ResVT
= N
->getValueType(0);
6235 unsigned NumElems
= N
->getNumOperands();
6237 if (ResVT
.isScalableVector()) {
6238 SDValue ResVec
= DAG
.getUNDEF(ResVT
);
6240 for (unsigned OpIdx
= 0; OpIdx
< NumElems
; ++OpIdx
) {
6241 SDValue Op
= N
->getOperand(OpIdx
);
6242 unsigned OpNumElts
= Op
.getValueType().getVectorMinNumElements();
6243 ResVec
= DAG
.getNode(ISD::INSERT_SUBVECTOR
, dl
, ResVT
, ResVec
, Op
,
6244 DAG
.getIntPtrConstant(OpIdx
* OpNumElts
, dl
));
6250 EVT RetSclrTy
= N
->getValueType(0).getVectorElementType();
6252 SmallVector
<SDValue
, 8> NewOps
;
6253 NewOps
.reserve(NumElems
);
6255 // For each incoming vector
6256 for (unsigned VecIdx
= 0; VecIdx
!= NumElems
; ++VecIdx
) {
6257 SDValue Incoming
= GetPromotedInteger(N
->getOperand(VecIdx
));
6258 EVT SclrTy
= Incoming
->getValueType(0).getVectorElementType();
6259 unsigned NumElem
= Incoming
->getValueType(0).getVectorNumElements();
6261 for (unsigned i
=0; i
<NumElem
; ++i
) {
6262 // Extract element from incoming vector
6263 SDValue Ex
= DAG
.getNode(ISD::EXTRACT_VECTOR_ELT
, dl
, SclrTy
, Incoming
,
6264 DAG
.getVectorIdxConstant(i
, dl
));
6265 SDValue Tr
= DAG
.getNode(ISD::TRUNCATE
, dl
, RetSclrTy
, Ex
);
6266 NewOps
.push_back(Tr
);
6270 return DAG
.getBuildVector(N
->getValueType(0), dl
, NewOps
);
6273 SDValue
DAGTypeLegalizer::ExpandIntOp_STACKMAP(SDNode
*N
, unsigned OpNo
) {
6275 SDValue Op
= N
->getOperand(OpNo
);
6277 // FIXME: Non-constant operands are not yet handled:
6278 // - https://github.com/llvm/llvm-project/issues/26431
6279 // - https://github.com/llvm/llvm-project/issues/55957
6280 ConstantSDNode
*CN
= dyn_cast
<ConstantSDNode
>(Op
);
6284 // Copy operands before the one being expanded.
6285 SmallVector
<SDValue
> NewOps
;
6286 for (unsigned I
= 0; I
< OpNo
; I
++)
6287 NewOps
.push_back(N
->getOperand(I
));
6289 EVT Ty
= Op
.getValueType();
6290 SDLoc DL
= SDLoc(N
);
6291 if (CN
->getConstantIntValue()->getValue().getActiveBits() < 64) {
6293 DAG
.getTargetConstant(StackMaps::ConstantOp
, DL
, MVT::i64
));
6294 NewOps
.push_back(DAG
.getTargetConstant(CN
->getZExtValue(), DL
, Ty
));
6296 // FIXME: https://github.com/llvm/llvm-project/issues/55609
6300 // Copy remaining operands.
6301 for (unsigned I
= OpNo
+ 1; I
< N
->getNumOperands(); I
++)
6302 NewOps
.push_back(N
->getOperand(I
));
6304 SDValue NewNode
= DAG
.getNode(N
->getOpcode(), DL
, N
->getVTList(), NewOps
);
6306 for (unsigned ResNum
= 0; ResNum
< N
->getNumValues(); ResNum
++)
6307 ReplaceValueWith(SDValue(N
, ResNum
), NewNode
.getValue(ResNum
));
6309 return SDValue(); // Signal that we have replaced the node already.
6312 SDValue
DAGTypeLegalizer::ExpandIntOp_PATCHPOINT(SDNode
*N
, unsigned OpNo
) {
6314 SDValue Op
= N
->getOperand(OpNo
);
6316 // FIXME: Non-constant operands are not yet handled:
6317 // - https://github.com/llvm/llvm-project/issues/26431
6318 // - https://github.com/llvm/llvm-project/issues/55957
6319 ConstantSDNode
*CN
= dyn_cast
<ConstantSDNode
>(Op
);
6323 // Copy operands before the one being expanded.
6324 SmallVector
<SDValue
> NewOps
;
6325 for (unsigned I
= 0; I
< OpNo
; I
++)
6326 NewOps
.push_back(N
->getOperand(I
));
6328 EVT Ty
= Op
.getValueType();
6329 SDLoc DL
= SDLoc(N
);
6330 if (CN
->getConstantIntValue()->getValue().getActiveBits() < 64) {
6332 DAG
.getTargetConstant(StackMaps::ConstantOp
, DL
, MVT::i64
));
6333 NewOps
.push_back(DAG
.getTargetConstant(CN
->getZExtValue(), DL
, Ty
));
6335 // FIXME: https://github.com/llvm/llvm-project/issues/55609
6339 // Copy remaining operands.
6340 for (unsigned I
= OpNo
+ 1; I
< N
->getNumOperands(); I
++)
6341 NewOps
.push_back(N
->getOperand(I
));
6343 SDValue NewNode
= DAG
.getNode(N
->getOpcode(), DL
, N
->getVTList(), NewOps
);
6345 for (unsigned ResNum
= 0; ResNum
< N
->getNumValues(); ResNum
++)
6346 ReplaceValueWith(SDValue(N
, ResNum
), NewNode
.getValue(ResNum
));
6348 return SDValue(); // Signal that we have replaced the node already.