LLVM 23.0.0git
LegalizeIntegerTypes.cpp
Go to the documentation of this file.
1//===----- LegalizeIntegerTypes.cpp - Legalization of integer types -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file 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
16// targets).
17//
18//===----------------------------------------------------------------------===//
19
20#include "LegalizeTypes.h"
29#include <algorithm>
30using namespace llvm;
31
32#define DEBUG_TYPE "legalize-types"
33
34//===----------------------------------------------------------------------===//
35// Integer Result Promotion
36//===----------------------------------------------------------------------===//
37
38/// PromoteIntegerResult - This method is called when a result of a node is
39/// found to be in need of promotion to a larger type. At this point, the node
40/// may also have invalid operands or may have other results that need
41/// expansion, we just know that (at least) one result needs promotion.
42void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
43 LLVM_DEBUG(dbgs() << "Promote integer result: "; N->dump(&DAG));
44 SDValue Res = SDValue();
45
46 // See if the target wants to custom expand this node.
47 if (CustomLowerNode(N, N->getValueType(ResNo), true)) {
48 LLVM_DEBUG(dbgs() << "Node has been custom expanded, done\n");
49 return;
50 }
51
52 switch (N->getOpcode()) {
53 default:
54#ifndef NDEBUG
55 dbgs() << "PromoteIntegerResult #" << ResNo << ": ";
56 N->dump(&DAG); dbgs() << "\n";
57#endif
58 report_fatal_error("Do not know how to promote this operator!");
59 case ISD::MERGE_VALUES:Res = PromoteIntRes_MERGE_VALUES(N, ResNo); break;
60 case ISD::AssertSext: Res = PromoteIntRes_AssertSext(N); break;
61 case ISD::AssertZext: Res = PromoteIntRes_AssertZext(N); break;
62 case ISD::BITCAST: Res = PromoteIntRes_BITCAST(N); break;
63 case ISD::VP_BITREVERSE:
64 case ISD::BITREVERSE: Res = PromoteIntRes_BITREVERSE(N); break;
65 case ISD::VP_BSWAP:
66 case ISD::BSWAP: Res = PromoteIntRes_BSWAP(N); break;
67 case ISD::BUILD_PAIR: Res = PromoteIntRes_BUILD_PAIR(N); break;
68 case ISD::Constant: Res = PromoteIntRes_Constant(N); break;
69 case ISD::VP_CTLZ_ZERO_POISON:
70 case ISD::VP_CTLZ:
72 case ISD::CTLZ: Res = PromoteIntRes_CTLZ(N); break;
73 case ISD::CTLS: Res = PromoteIntRes_CTLS(N); break;
74 case ISD::PARITY:
75 case ISD::VP_CTPOP:
76 case ISD::CTPOP: Res = PromoteIntRes_CTPOP_PARITY(N); break;
77 case ISD::VP_CTTZ_ZERO_POISON:
78 case ISD::VP_CTTZ:
80 case ISD::CTTZ: Res = PromoteIntRes_CTTZ(N); break;
82 case ISD::CTTZ_ELTS:
83 case ISD::VP_CTTZ_ELTS_ZERO_POISON:
84 case ISD::VP_CTTZ_ELTS:
85 Res = PromoteIntRes_VP_CttzElements(N);
86 break;
88 Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
89 case ISD::LOAD: Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N)); break;
90 case ISD::VP_LOAD:
91 Res = PromoteIntRes_VP_LOAD(cast<VPLoadSDNode>(N));
92 break;
93 case ISD::MLOAD: Res = PromoteIntRes_MLOAD(cast<MaskedLoadSDNode>(N));
94 break;
95 case ISD::MGATHER: Res = PromoteIntRes_MGATHER(cast<MaskedGatherSDNode>(N));
96 break;
98 Res = PromoteIntRes_VECTOR_COMPRESS(N);
99 break;
100 case ISD::SELECT:
101 case ISD::VSELECT:
102 case ISD::VP_SELECT:
103 case ISD::VP_MERGE:
104 Res = PromoteIntRes_Select(N);
105 break;
106 case ISD::SELECT_CC: Res = PromoteIntRes_SELECT_CC(N); break;
109 case ISD::SETCC: Res = PromoteIntRes_SETCC(N); break;
110 case ISD::SMIN:
111 case ISD::SMAX: Res = PromoteIntRes_SExtIntBinOp(N); break;
112 case ISD::UMIN:
113 case ISD::UMAX: Res = PromoteIntRes_UMINUMAX(N); break;
114
115 case ISD::SHL:
116 case ISD::VP_SHL: Res = PromoteIntRes_SHL(N); break;
118 Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
119 case ISD::SRA:
120 case ISD::VP_SRA: Res = PromoteIntRes_SRA(N); break;
121 case ISD::SRL:
122 case ISD::VP_SRL: Res = PromoteIntRes_SRL(N); break;
123 case ISD::VP_TRUNCATE:
124 case ISD::TRUNCATE: Res = PromoteIntRes_TRUNCATE(N); break;
125 case ISD::POISON:
126 case ISD::UNDEF: Res = PromoteIntRes_UNDEF(N); break;
127 case ISD::VAARG: Res = PromoteIntRes_VAARG(N); break;
128 case ISD::VSCALE: Res = PromoteIntRes_VSCALE(N); break;
129
131 Res = PromoteIntRes_EXTRACT_SUBVECTOR(N); break;
133 Res = PromoteIntRes_INSERT_SUBVECTOR(N); break;
135 Res = PromoteIntRes_VECTOR_REVERSE(N); break;
137 Res = PromoteIntRes_VECTOR_SHUFFLE(N); break;
140 Res = PromoteIntRes_VECTOR_SPLICE(N);
141 break;
144 Res = PromoteIntRes_VECTOR_INTERLEAVE_DEINTERLEAVE(N);
145 return;
147 Res = PromoteIntRes_INSERT_VECTOR_ELT(N); break;
149 Res = PromoteIntRes_BUILD_VECTOR(N);
150 break;
153 Res = PromoteIntRes_ScalarOp(N);
154 break;
155 case ISD::STEP_VECTOR: Res = PromoteIntRes_STEP_VECTOR(N); break;
157 Res = PromoteIntRes_CONCAT_VECTORS(N); break;
158
162 Res = PromoteIntRes_EXTEND_VECTOR_INREG(N); break;
163
165 Res = PromoteIntRes_VECTOR_FIND_LAST_ACTIVE(N);
166 break;
167
169 Res = PromoteIntRes_GET_ACTIVE_LANE_MASK(N);
170 break;
171
175 Res = PromoteIntRes_PARTIAL_REDUCE_MLA(N);
176 break;
177
178 case ISD::SIGN_EXTEND:
179 case ISD::VP_SIGN_EXTEND:
180 case ISD::ZERO_EXTEND:
181 case ISD::VP_ZERO_EXTEND:
182 case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break;
183
184 case ISD::VP_FP_TO_SINT:
185 case ISD::VP_FP_TO_UINT:
188 case ISD::FP_TO_SINT:
189 case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break;
190
193 Res = PromoteIntRes_FP_TO_XINT_SAT(N); break;
194
195 case ISD::FP_TO_BF16:
196 case ISD::FP_TO_FP16:
197 Res = PromoteIntRes_FP_TO_FP16_BF16(N);
198 break;
200 Res = PromoteIntRes_CONVERT_TO_ARBITRARY_FP(N);
201 break;
204 Res = PromoteIntRes_STRICT_FP_TO_FP16_BF16(N);
205 break;
206 case ISD::GET_ROUNDING: Res = PromoteIntRes_GET_ROUNDING(N); break;
207
208 case ISD::AND:
209 case ISD::OR:
210 case ISD::XOR:
211 case ISD::ADD:
212 case ISD::SUB:
213 case ISD::MUL:
214 case ISD::VP_AND:
215 case ISD::VP_OR:
216 case ISD::VP_XOR:
217 case ISD::VP_ADD:
218 case ISD::VP_SUB:
219 case ISD::VP_MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break;
220
221 case ISD::ABDS:
222 case ISD::AVGCEILS:
223 case ISD::AVGFLOORS:
224 case ISD::VP_SMIN:
225 case ISD::VP_SMAX:
226 case ISD::SDIV:
227 case ISD::SREM:
228 case ISD::VP_SDIV:
229 case ISD::VP_SREM: Res = PromoteIntRes_SExtIntBinOp(N); break;
230
231 case ISD::ABDU:
232 case ISD::AVGCEILU:
233 case ISD::AVGFLOORU:
234 case ISD::VP_UMIN:
235 case ISD::VP_UMAX:
236 case ISD::UDIV:
237 case ISD::UREM:
238 case ISD::VP_UDIV:
239 case ISD::VP_UREM: Res = PromoteIntRes_ZExtIntBinOp(N); break;
240
241 case ISD::MASKED_UDIV:
242 case ISD::MASKED_UREM:
243 Res = PromoteIntRes_ZExtMaskedIntBinOp(N);
244 break;
245 case ISD::MASKED_SDIV:
246 case ISD::MASKED_SREM:
247 Res = PromoteIntRes_SExtMaskedIntBinOp(N);
248 break;
249
250 case ISD::SADDO:
251 case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
252 case ISD::UADDO:
253 case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
254 case ISD::SMULO:
255 case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;
256
257 case ISD::ADDE:
258 case ISD::SUBE:
259 case ISD::UADDO_CARRY:
260 case ISD::USUBO_CARRY: Res = PromoteIntRes_UADDSUBO_CARRY(N, ResNo); break;
261
262 case ISD::SADDO_CARRY:
263 case ISD::SSUBO_CARRY: Res = PromoteIntRes_SADDSUBO_CARRY(N, ResNo); break;
264
265 case ISD::SADDSAT:
266 case ISD::UADDSAT:
267 case ISD::SSUBSAT:
268 case ISD::USUBSAT:
269 case ISD::SSHLSAT:
270 case ISD::USHLSAT:
271 Res = PromoteIntRes_ADDSUBSHLSAT<EmptyMatchContext>(N);
272 break;
273 case ISD::VP_SADDSAT:
274 case ISD::VP_UADDSAT:
275 case ISD::VP_SSUBSAT:
276 case ISD::VP_USUBSAT:
277 Res = PromoteIntRes_ADDSUBSHLSAT<VPMatchContext>(N);
278 break;
279
280 case ISD::SCMP:
281 case ISD::UCMP:
282 Res = PromoteIntRes_CMP(N);
283 break;
284
285 case ISD::SMULFIX:
286 case ISD::SMULFIXSAT:
287 case ISD::UMULFIX:
288 case ISD::UMULFIXSAT: Res = PromoteIntRes_MULFIX(N); break;
289
290 case ISD::SDIVFIX:
291 case ISD::SDIVFIXSAT:
292 case ISD::UDIVFIX:
293 case ISD::UDIVFIXSAT: Res = PromoteIntRes_DIVFIX(N); break;
294
295 case ISD::ABS:
297 Res = PromoteIntRes_ABS(N);
298 break;
299
300 case ISD::ATOMIC_LOAD:
301 Res = PromoteIntRes_Atomic0(cast<AtomicSDNode>(N)); break;
302
314 case ISD::ATOMIC_SWAP:
315 Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
316
319 Res = PromoteIntRes_AtomicCmpSwap(cast<AtomicSDNode>(N), ResNo);
320 break;
321
331 Res = PromoteIntRes_VECREDUCE(N);
332 break;
333
334 case ISD::VP_REDUCE_ADD:
335 case ISD::VP_REDUCE_MUL:
336 case ISD::VP_REDUCE_AND:
337 case ISD::VP_REDUCE_OR:
338 case ISD::VP_REDUCE_XOR:
339 case ISD::VP_REDUCE_SMAX:
340 case ISD::VP_REDUCE_SMIN:
341 case ISD::VP_REDUCE_UMAX:
342 case ISD::VP_REDUCE_UMIN:
343 Res = PromoteIntRes_VP_REDUCE(N);
344 break;
345
348 Res = PromoteIntRes_LOOP_DEPENDENCE_MASK(N);
349 break;
350
351 case ISD::FREEZE:
352 Res = PromoteIntRes_FREEZE(N);
353 break;
354
355 case ISD::ROTL:
356 case ISD::ROTR:
357 Res = PromoteIntRes_Rotate(N);
358 break;
359
360 case ISD::FSHL:
361 case ISD::FSHR:
362 Res = PromoteIntRes_FunnelShift(N);
363 break;
364
365 case ISD::VP_FSHL:
366 case ISD::VP_FSHR:
367 Res = PromoteIntRes_VPFunnelShift(N);
368 break;
369
370 case ISD::CLMUL:
371 case ISD::CLMULH:
372 case ISD::CLMULR:
373 Res = PromoteIntRes_CLMUL(N);
374 break;
375
376 case ISD::PEXT:
377 Res = PromoteIntRes_PEXT(N);
378 break;
379
380 case ISD::PDEP:
381 Res = PromoteIntRes_PDEP(N);
382 break;
383
384 case ISD::IS_FPCLASS:
385 Res = PromoteIntRes_IS_FPCLASS(N);
386 break;
387 case ISD::FFREXP:
388 Res = PromoteIntRes_FFREXP(N);
389 break;
390
391 case ISD::LRINT:
392 case ISD::LLRINT:
393 Res = PromoteIntRes_XRINT(N);
394 break;
395
396 case ISD::PATCHPOINT:
397 Res = PromoteIntRes_PATCHPOINT(N);
398 break;
400 Res = PromoteIntRes_READ_REGISTER(N);
401 break;
402 }
403
404 // If the result is null then the sub-method took care of registering it.
405 if (Res.getNode())
406 SetPromotedInteger(SDValue(N, ResNo), Res);
407}
408
409SDValue DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode *N,
410 unsigned ResNo) {
411 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
412 return GetPromotedInteger(Op);
413}
414
415SDValue DAGTypeLegalizer::PromoteIntRes_LOOP_DEPENDENCE_MASK(SDNode *N) {
416 EVT VT = N->getValueType(0);
417 EVT NewVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
418 return DAG.getNode(N->getOpcode(), SDLoc(N), NewVT, N->ops());
419}
420
421SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
422 // Sign-extend the new bits, and continue the assertion.
423 SDValue Op = SExtPromotedInteger(N->getOperand(0));
424 return DAG.getNode(ISD::AssertSext, SDLoc(N),
425 Op.getValueType(), Op, N->getOperand(1));
426}
427
428SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
429 // Zero the new bits, and continue the assertion.
430 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
431 return DAG.getNode(ISD::AssertZext, SDLoc(N),
432 Op.getValueType(), Op, N->getOperand(1));
433}
434
435SDValue DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode *N) {
436 EVT ResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
437 ISD::LoadExtType ExtType = N->getExtensionType();
438 if (ExtType == ISD::NON_EXTLOAD) {
439 switch (TLI.getExtendForAtomicOps()) {
440 case ISD::SIGN_EXTEND:
441 ExtType = ISD::SEXTLOAD;
442 break;
443 case ISD::ZERO_EXTEND:
444 ExtType = ISD::ZEXTLOAD;
445 break;
446 case ISD::ANY_EXTEND:
447 ExtType = ISD::EXTLOAD;
448 break;
449 default:
450 llvm_unreachable("Invalid atomic op extension");
451 }
452 }
453
454 SDValue Res =
455 DAG.getAtomicLoad(ExtType, SDLoc(N), N->getMemoryVT(), ResVT,
456 N->getChain(), N->getBasePtr(), N->getMemOperand());
457
458 // Legalize the chain result - switch anything that used the old chain to
459 // use the new one.
460 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
461 return Res;
462}
463
464SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
465 SDValue Op2 = N->getOperand(2);
466 switch (TLI.getExtendForAtomicRMWArg(N->getOpcode())) {
467 case ISD::SIGN_EXTEND:
468 Op2 = SExtPromotedInteger(Op2);
469 break;
470 case ISD::ZERO_EXTEND:
471 Op2 = ZExtPromotedInteger(Op2);
472 break;
473 case ISD::ANY_EXTEND:
474 Op2 = GetPromotedInteger(Op2);
475 break;
476 default:
477 llvm_unreachable("Invalid atomic op extension");
478 }
479 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
480 N->getMemoryVT(),
481 N->getChain(), N->getBasePtr(),
482 Op2, N->getMemOperand());
483 // Legalize the chain result - switch anything that used the old chain to
484 // use the new one.
485 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
486 return Res;
487}
488
489SDValue DAGTypeLegalizer::PromoteIntRes_AtomicCmpSwap(AtomicSDNode *N,
490 unsigned ResNo) {
491 if (ResNo == 1) {
493 EVT SVT = getSetCCResultType(N->getOperand(2).getValueType());
494 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
495
496 // Only use the result of getSetCCResultType if it is legal,
497 // otherwise just use the promoted result type (NVT).
498 if (!TLI.isTypeLegal(SVT))
499 SVT = NVT;
500
501 SDVTList VTs = DAG.getVTList(N->getValueType(0), SVT, MVT::Other);
502 SDValue Res = DAG.getAtomicCmpSwap(
503 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, SDLoc(N), N->getMemoryVT(), VTs,
504 N->getChain(), N->getBasePtr(), N->getOperand(2), N->getOperand(3),
505 N->getMemOperand());
506 ReplaceValueWith(SDValue(N, 0), Res.getValue(0));
507 ReplaceValueWith(SDValue(N, 2), Res.getValue(2));
508 return DAG.getSExtOrTrunc(Res.getValue(1), SDLoc(N), NVT);
509 }
510
511 // Op2 is used for the comparison and thus must be extended according to the
512 // target's atomic operations. Op3 is merely stored and so can be left alone.
513 SDValue Op2 = N->getOperand(2);
514 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
515 switch (TLI.getExtendForAtomicCmpSwapArg()) {
516 case ISD::SIGN_EXTEND:
517 Op2 = SExtPromotedInteger(Op2);
518 break;
519 case ISD::ZERO_EXTEND:
520 Op2 = ZExtPromotedInteger(Op2);
521 break;
522 case ISD::ANY_EXTEND:
523 Op2 = GetPromotedInteger(Op2);
524 break;
525 default:
526 llvm_unreachable("Invalid atomic op extension");
527 }
528
529 SDVTList VTs =
530 DAG.getVTList(Op2.getValueType(), N->getValueType(1), MVT::Other);
531 SDValue Res = DAG.getAtomicCmpSwap(
532 N->getOpcode(), SDLoc(N), N->getMemoryVT(), VTs, N->getChain(),
533 N->getBasePtr(), Op2, Op3, N->getMemOperand());
534 // Update the use to N with the newly created Res.
535 for (unsigned i = 1, NumResults = N->getNumValues(); i < NumResults; ++i)
536 ReplaceValueWith(SDValue(N, i), Res.getValue(i));
537 return Res;
538}
539
540SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
541 SDValue InOp = N->getOperand(0);
542 EVT InVT = InOp.getValueType();
543 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
544 EVT OutVT = N->getValueType(0);
545 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
546 SDLoc dl(N);
547
548 switch (getTypeAction(InVT)) {
550 break;
552 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector() && !NInVT.isVector())
553 // The input promotes to the same size. Convert the promoted value.
554 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp));
555 break;
557 // Promote the integer operand by hand.
558 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
560 // Promote the integer operand by hand.
561 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftPromotedHalf(InOp));
564 break;
566 // Convert the element to an integer and promote it by hand.
567 if (!NOutVT.isVector())
568 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
569 BitConvertToInteger(GetScalarizedVector(InOp)));
570 break;
572 report_fatal_error("Scalarization of scalable vectors is not supported.");
574 if (!NOutVT.isVector()) {
575 // For example, i32 = BITCAST v2i16 on alpha. Convert the split
576 // pieces of the input into integers and reassemble in the final type.
577 SDValue Lo, Hi;
578 GetSplitVector(N->getOperand(0), Lo, Hi);
579 Lo = BitConvertToInteger(Lo);
580 Hi = BitConvertToInteger(Hi);
581
582 if (DAG.getDataLayout().isBigEndian())
583 std::swap(Lo, Hi);
584
585 InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
586 EVT::getIntegerVT(*DAG.getContext(),
587 NOutVT.getSizeInBits()),
588 JoinIntegers(Lo, Hi));
589 return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp);
590 }
591 break;
592 }
594 // The input is widened to the same size. Convert to the widened value.
595 // Make sure that the outgoing value is not a vector, because this would
596 // make us bitcast between two vectors which are legalized in different ways.
597 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector()) {
598 SDValue Res =
599 DAG.getNode(ISD::BITCAST, dl, NOutVT, GetWidenedVector(InOp));
600
601 // For big endian targets we need to shift the casted value or the
602 // interesting bits will end up at the wrong place.
603 if (DAG.getDataLayout().isBigEndian()) {
604 unsigned ShiftAmt = NInVT.getSizeInBits() - InVT.getSizeInBits();
605 assert(ShiftAmt < NOutVT.getSizeInBits() && "Too large shift amount!");
606 Res = DAG.getNode(ISD::SRL, dl, NOutVT, Res,
607 DAG.getShiftAmountConstant(ShiftAmt, NOutVT, dl));
608 }
609 return Res;
610 }
611 // If the output type is also a vector and widening it to the same size
612 // as the widened input type would be a legal type, we can widen the bitcast
613 // and handle the promotion after.
614 if (NOutVT.isVector()) {
615 TypeSize WidenInSize = NInVT.getSizeInBits();
616 TypeSize OutSize = OutVT.getSizeInBits();
617 if (WidenInSize.hasKnownScalarFactor(OutSize)) {
618 unsigned Scale = WidenInSize.getKnownScalarFactor(OutSize);
619 EVT WideOutVT =
620 EVT::getVectorVT(*DAG.getContext(), OutVT.getVectorElementType(),
621 OutVT.getVectorElementCount() * Scale);
622 if (isTypeLegal(WideOutVT)) {
623 InOp = DAG.getBitcast(WideOutVT, GetWidenedVector(InOp));
624 InOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, InOp,
625 DAG.getVectorIdxConstant(0, dl));
626 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, InOp);
627 }
628 }
629 }
630 }
631
632 // TODO: Handle big endian
633 if (!NOutVT.isVector() && InOp.getValueType().isVector() &&
634 DAG.getDataLayout().isLittleEndian()) {
635 // Pad the vector operand with undef and cast to a wider integer.
636 EVT EltVT = InOp.getValueType().getVectorElementType();
637 TypeSize EltSize = EltVT.getSizeInBits();
638 TypeSize OutSize = NOutVT.getSizeInBits();
639
640 if (OutSize.hasKnownScalarFactor(EltSize)) {
641 unsigned NumEltsWithPadding = OutSize.getKnownScalarFactor(EltSize);
642 EVT WideVecVT =
643 EVT::getVectorVT(*DAG.getContext(), EltVT, NumEltsWithPadding);
644
645 if (isTypeLegal(WideVecVT)) {
646 SDValue Inserted = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideVecVT,
647 DAG.getUNDEF(WideVecVT), InOp,
648 DAG.getVectorIdxConstant(0, dl));
649
650 return DAG.getNode(ISD::BITCAST, dl, NOutVT, Inserted);
651 }
652 }
653 }
654
655 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
656 CreateStackStoreLoad(InOp, OutVT));
657}
658
659SDValue DAGTypeLegalizer::PromoteIntRes_FREEZE(SDNode *N) {
660 SDValue V = GetPromotedInteger(N->getOperand(0));
661 return DAG.getNode(ISD::FREEZE, SDLoc(N),
662 V.getValueType(), V);
663}
664
665SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
666 SDValue Op = GetPromotedInteger(N->getOperand(0));
667 EVT OVT = N->getValueType(0);
668 EVT NVT = Op.getValueType();
669 SDLoc dl(N);
670
671 // If the larger BSWAP isn't supported by the target, try to expand now.
672 // If we expand later we'll end up with more operations since we lost the
673 // original type. We only do this for scalars since we have a shuffle
674 // based lowering for vectors in LegalizeVectorOps.
675 if (!OVT.isVector() &&
676 !TLI.isOperationLegalOrCustomOrPromote(ISD::BSWAP, NVT)) {
677 if (SDValue Res = TLI.expandBSWAP(N, DAG))
678 return DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Res);
679 }
680
681 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
682 SDValue ShAmt = DAG.getShiftAmountConstant(DiffBits, NVT, dl);
683 if (N->getOpcode() == ISD::BSWAP)
684 return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
685 ShAmt);
686 SDValue Mask = N->getOperand(1);
687 SDValue EVL = N->getOperand(2);
688 return DAG.getNode(ISD::VP_SRL, dl, NVT,
689 DAG.getNode(ISD::VP_BSWAP, dl, NVT, Op, Mask, EVL), ShAmt,
690 Mask, EVL);
691}
692
693SDValue DAGTypeLegalizer::PromoteIntRes_BITREVERSE(SDNode *N) {
694 SDValue Op = GetPromotedInteger(N->getOperand(0));
695 EVT OVT = N->getValueType(0);
696 EVT NVT = Op.getValueType();
697 SDLoc dl(N);
698
699 // If the larger BITREVERSE isn't supported by the target, try to expand now.
700 // If we expand later we'll end up with more operations since we lost the
701 // original type. We only do this for scalars since we have a shuffle
702 // based lowering for vectors in LegalizeVectorOps.
703 if (!OVT.isVector() && OVT.isSimple() &&
704 !TLI.isOperationLegalOrCustomOrPromote(ISD::BITREVERSE, NVT)) {
705 if (SDValue Res = TLI.expandBITREVERSE(N, DAG))
706 return DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Res);
707 }
708
709 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
710 SDValue ShAmt = DAG.getShiftAmountConstant(DiffBits, NVT, dl);
711 if (N->getOpcode() == ISD::BITREVERSE)
712 return DAG.getNode(ISD::SRL, dl, NVT,
713 DAG.getNode(ISD::BITREVERSE, dl, NVT, Op), ShAmt);
714 SDValue Mask = N->getOperand(1);
715 SDValue EVL = N->getOperand(2);
716 return DAG.getNode(ISD::VP_SRL, dl, NVT,
717 DAG.getNode(ISD::VP_BITREVERSE, dl, NVT, Op, Mask, EVL),
718 ShAmt, Mask, EVL);
719}
720
721SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
722 // The pair element type may be legal, or may not promote to the same type as
723 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
724 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N),
725 TLI.getTypeToTransformTo(*DAG.getContext(),
726 N->getValueType(0)), JoinIntegers(N->getOperand(0),
727 N->getOperand(1)));
728}
729
730SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
731 EVT VT = N->getValueType(0);
732 // FIXME there is no actual debug info here
733 SDLoc dl(N);
734 // Zero extend things like i1, sign extend everything else. It shouldn't
735 // matter in theory which one we pick, but this tends to give better code?
737 SDValue Result = DAG.getNode(Opc, dl,
738 TLI.getTypeToTransformTo(*DAG.getContext(), VT),
739 SDValue(N, 0));
740 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
741 return Result;
742}
743
744SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
745 EVT OVT = N->getValueType(0);
746 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
747 SDLoc dl(N);
748
749 // If the larger CTLZ 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.
752 if (!OVT.isVector() && TLI.isTypeLegal(NVT) &&
753 !TLI.isOperationLegalOrCustomOrPromote(ISD::CTLZ, NVT) &&
754 !TLI.isOperationLegalOrCustomOrPromote(ISD::CTLZ_ZERO_POISON, NVT)) {
755 if (SDValue Result = TLI.expandCTLZ(N, DAG)) {
756 Result = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Result);
757 return Result;
758 }
759 }
760
761 unsigned CtlzOpcode = N->getOpcode();
762 if (CtlzOpcode == ISD::CTLZ || CtlzOpcode == ISD::VP_CTLZ) {
763 // Subtract off the extra leading bits in the bigger type.
764 SDValue ExtractLeadingBits = DAG.getConstant(
765 NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(), dl, NVT);
766 // Zero extend to the promoted type and do the count there.
767 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
768
769 // At this stage SUB is guaranteed to be positive no-wrap,
770 // that to be used in further KnownBits optimizations.
771 if (!N->isVPOpcode())
772 return DAG.getNode(ISD::SUB, dl, NVT,
773 DAG.getNode(N->getOpcode(), dl, NVT, Op),
774 ExtractLeadingBits, SDNodeFlags::NoUnsignedWrap);
775 SDValue Mask = N->getOperand(1);
776 SDValue EVL = N->getOperand(2);
777 return DAG.getNode(ISD::VP_SUB, dl, NVT,
778 DAG.getNode(N->getOpcode(), dl, NVT, Op, Mask, EVL),
779 ExtractLeadingBits, Mask, EVL,
781 }
782 if (CtlzOpcode == ISD::CTLZ_ZERO_POISON ||
783 CtlzOpcode == ISD::VP_CTLZ_ZERO_POISON) {
784 // Any Extend the argument
785 SDValue Op = GetPromotedInteger(N->getOperand(0));
786 // Op = Op << (sizeinbits(NVT) - sizeinbits(Old VT))
787 unsigned SHLAmount = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
788 auto ShiftConst =
789 DAG.getShiftAmountConstant(SHLAmount, Op.getValueType(), dl);
790 if (!N->isVPOpcode()) {
791 Op = DAG.getNode(ISD::SHL, dl, NVT, Op, ShiftConst);
792 return DAG.getNode(CtlzOpcode, dl, NVT, Op);
793 }
794
795 SDValue Mask = N->getOperand(1);
796 SDValue EVL = N->getOperand(2);
797 Op = DAG.getNode(ISD::VP_SHL, dl, NVT, Op, ShiftConst, Mask, EVL);
798 return DAG.getNode(CtlzOpcode, dl, NVT, Op, Mask, EVL);
799 }
800 llvm_unreachable("Invalid CTLZ Opcode");
801}
802
803SDValue DAGTypeLegalizer::PromoteIntRes_CTLS(SDNode *N) {
804 EVT OVT = N->getValueType(0);
805 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
806 SDLoc dl(N);
807
808 SDValue ExtractLeadingBits = DAG.getConstant(
809 NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(), dl, NVT);
810
811 SDValue Op = SExtPromotedInteger(N->getOperand(0));
812 return DAG.getNode(ISD::SUB, dl, NVT, DAG.getNode(ISD::CTLS, dl, NVT, Op),
813 ExtractLeadingBits);
814}
815
816SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP_PARITY(SDNode *N) {
817 EVT OVT = N->getValueType(0);
818 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
819
820 // If the larger CTPOP isn't supported by the target, try to expand now.
821 // If we expand later we'll end up with more operations since we lost the
822 // original type.
823 // TODO: Expand ISD::PARITY. Need to move ExpandPARITY from LegalizeDAG to
824 // TargetLowering.
825 if (N->getOpcode() == ISD::CTPOP && !OVT.isVector() && TLI.isTypeLegal(NVT) &&
826 !TLI.isOperationLegalOrCustomOrPromote(ISD::CTPOP, NVT)) {
827 if (SDValue Result = TLI.expandCTPOP(N, DAG)) {
828 Result = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), NVT, Result);
829 return Result;
830 }
831 }
832
833 // Zero extend to the promoted type and do the count or parity there.
834 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
835 if (!N->isVPOpcode())
836 return DAG.getNode(N->getOpcode(), SDLoc(N), Op.getValueType(), Op);
837
838 SDValue Mask = N->getOperand(1);
839 SDValue EVL = N->getOperand(2);
840 return DAG.getNode(N->getOpcode(), SDLoc(N), Op.getValueType(), Op, Mask,
841 EVL);
842}
843
844SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
845 SDValue Op = GetPromotedInteger(N->getOperand(0));
846 EVT OVT = N->getValueType(0);
847 EVT NVT = Op.getValueType();
848 SDLoc dl(N);
849
850 // If the larger CTTZ isn't supported by the target, try to expand now.
851 // If we expand later we'll end up with more operations since we lost the
852 // original type. Don't expand if we can use CTPOP or CTLZ expansion on the
853 // larger type.
854 if (!OVT.isVector() && TLI.isTypeLegal(NVT) &&
855 !TLI.isOperationLegalOrCustomOrPromote(ISD::CTTZ, NVT) &&
856 !TLI.isOperationLegalOrCustomOrPromote(ISD::CTTZ_ZERO_POISON, NVT) &&
857 !TLI.isOperationLegal(ISD::CTPOP, NVT) &&
858 !TLI.isOperationLegal(ISD::CTLZ, NVT)) {
859 if (SDValue Result = TLI.expandCTTZ(N, DAG)) {
860 Result = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Result);
861 return Result;
862 }
863 }
864
865 unsigned NewOpc = N->getOpcode();
866 if (NewOpc == ISD::CTTZ || NewOpc == ISD::VP_CTTZ) {
867 // The count is the same in the promoted type except if the original
868 // value was zero. This can be handled by setting the bit just off
869 // the top of the original type.
870 auto TopBit = APInt::getOneBitSet(NVT.getScalarSizeInBits(),
871 OVT.getScalarSizeInBits());
872 if (NewOpc == ISD::CTTZ) {
873 Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, dl, NVT));
874 NewOpc = ISD::CTTZ_ZERO_POISON;
875 } else {
876 Op =
877 DAG.getNode(ISD::VP_OR, dl, NVT, Op, DAG.getConstant(TopBit, dl, NVT),
878 N->getOperand(1), N->getOperand(2));
879 NewOpc = ISD::VP_CTTZ_ZERO_POISON;
880 }
881 }
882 if (!N->isVPOpcode())
883 return DAG.getNode(NewOpc, dl, NVT, Op);
884 return DAG.getNode(NewOpc, dl, NVT, Op, N->getOperand(1), N->getOperand(2));
885}
886
887SDValue DAGTypeLegalizer::PromoteIntRes_VP_CttzElements(SDNode *N) {
888 SDLoc DL(N);
889 EVT NewVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
890 return DAG.getNode(N->getOpcode(), DL, NewVT, N->ops());
891}
892
893SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
894 SDLoc dl(N);
895 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
896
897 SDValue Op0 = N->getOperand(0);
898 SDValue Op1 = N->getOperand(1);
899
900 // If the input also needs to be promoted, do that first so we can get a
901 // get a good idea for the output type.
902 if (TLI.getTypeAction(*DAG.getContext(), Op0.getValueType())
904 SDValue In = GetPromotedInteger(Op0);
905
906 // If the new type is larger than NVT, use it. We probably won't need to
907 // promote it again.
908 EVT SVT = In.getValueType().getScalarType();
909 if (SVT.bitsGE(NVT)) {
910 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SVT, In, Op1);
911 return DAG.getAnyExtOrTrunc(Ext, dl, NVT);
912 }
913 }
914
915 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, Op0, Op1);
916}
917
918SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
919 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
920 unsigned NewOpc =
921 TLI.getPreferredFPToIntOpcode(N->getOpcode(), N->getValueType(0), NVT);
922 SDLoc dl(N);
923
924 SDValue Res;
925 if (N->isStrictFPOpcode()) {
926 Res = DAG.getNode(NewOpc, dl, {NVT, MVT::Other},
927 {N->getOperand(0), N->getOperand(1)});
928 // Legalize the chain result - switch anything that used the old chain to
929 // use the new one.
930 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
931 } else if (NewOpc == ISD::VP_FP_TO_SINT || NewOpc == ISD::VP_FP_TO_UINT) {
932 Res = DAG.getNode(NewOpc, dl, NVT, {N->getOperand(0), N->getOperand(1),
933 N->getOperand(2)});
934 } else {
935 Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
936 }
937
938 // Assert that the converted value fits in the original type. If it doesn't
939 // (eg: because the value being converted is too big), then the result of the
940 // original operation was undefined anyway, so the assert is still correct.
941 //
942 // NOTE: fp-to-uint to fp-to-sint promotion guarantees zero extend. For example:
943 // before legalization: fp-to-uint16, 65534. -> 0xfffe
944 // after legalization: fp-to-sint32, 65534. -> 0x0000fffe
945 return DAG.getNode((N->getOpcode() == ISD::FP_TO_UINT ||
946 N->getOpcode() == ISD::STRICT_FP_TO_UINT ||
947 N->getOpcode() == ISD::VP_FP_TO_UINT)
950 dl, NVT, Res,
951 DAG.getValueType(N->getValueType(0).getScalarType()));
952}
953
954SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT_SAT(SDNode *N) {
955 // Promote the result type, while keeping the original width in Op1.
956 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
957 SDLoc dl(N);
958 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0),
959 N->getOperand(1));
960}
961
962SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_FP16_BF16(SDNode *N) {
963 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
964 SDLoc dl(N);
965
966 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
967}
968
969// TODO: CONVERT_TO_ARBITRARY_FP also needs an ExpandIntegerResult handler for
970// wider arbitrary FP formats whose integer result requires expansion.
971SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_TO_ARBITRARY_FP(SDNode *N) {
972 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
973 SDLoc dl(N);
974
975 return DAG.getNode(ISD::CONVERT_TO_ARBITRARY_FP, dl, NVT, N->getOperand(0),
976 N->getOperand(1), N->getOperand(2), N->getOperand(3));
977}
978
979SDValue DAGTypeLegalizer::PromoteIntRes_STRICT_FP_TO_FP16_BF16(SDNode *N) {
980 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
981 SDLoc dl(N);
982
983 SDValue Res = DAG.getNode(N->getOpcode(), dl, DAG.getVTList(NVT, MVT::Other),
984 N->getOperand(0), N->getOperand(1));
985 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
986 return Res;
987}
988
989SDValue DAGTypeLegalizer::PromoteIntRes_XRINT(SDNode *N) {
990 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
991 SDLoc dl(N);
992 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
993}
994
995SDValue DAGTypeLegalizer::PromoteIntRes_GET_ROUNDING(SDNode *N) {
996 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
997 SDLoc dl(N);
998
999 SDValue Res =
1000 DAG.getNode(N->getOpcode(), dl, {NVT, MVT::Other}, N->getOperand(0));
1001
1002 // Legalize the chain result - switch anything that used the old chain to
1003 // use the new one.
1004 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1005 return Res;
1006}
1007
1008SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
1009 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1010 SDLoc dl(N);
1011
1012 if (getTypeAction(N->getOperand(0).getValueType())
1014 SDValue Res = GetPromotedInteger(N->getOperand(0));
1015 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
1016
1017 // If the result and operand types are the same after promotion, simplify
1018 // to an in-register extension. Unless this is a VP_*_EXTEND.
1019 if (NVT == Res.getValueType() && N->getNumOperands() == 1) {
1020 // The high bits are not guaranteed to be anything. Insert an extend.
1021 if (N->getOpcode() == ISD::SIGN_EXTEND)
1022 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
1023 DAG.getValueType(N->getOperand(0).getValueType()));
1024 if (N->getOpcode() == ISD::ZERO_EXTEND)
1025 return DAG.getZeroExtendInReg(Res, dl, N->getOperand(0).getValueType());
1026 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
1027 return Res;
1028 }
1029 }
1030
1031 // Otherwise, just extend the original operand all the way to the larger type.
1032 if (N->getNumOperands() != 1) {
1033 assert(N->getNumOperands() == 3 && "Unexpected number of operands!");
1034 assert(N->isVPOpcode() && "Expected VP opcode");
1035 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0),
1036 N->getOperand(1), N->getOperand(2));
1037 }
1038 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
1039}
1040
1041SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
1042 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1043 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1044 ISD::LoadExtType ExtType =
1045 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
1046 SDLoc dl(N);
1047 SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
1048 N->getMemoryVT(), N->getMemOperand());
1049
1050 // Legalize the chain result - switch anything that used the old chain to
1051 // use the new one.
1052 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1053 return Res;
1054}
1055
1056SDValue DAGTypeLegalizer::PromoteIntRes_VP_LOAD(VPLoadSDNode *N) {
1057 assert(!N->isIndexed() && "Indexed vp_load during type legalization!");
1058 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1059 ISD::LoadExtType ExtType = (N->getExtensionType() == ISD::NON_EXTLOAD)
1060 ? ISD::EXTLOAD
1061 : N->getExtensionType();
1062 SDLoc dl(N);
1063 SDValue Res =
1064 DAG.getExtLoadVP(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
1065 N->getMask(), N->getVectorLength(), N->getMemoryVT(),
1066 N->getMemOperand(), N->isExpandingLoad());
1067 // Legalize the chain result - switch anything that used the old chain to
1068 // use the new one.
1069 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1070 return Res;
1071}
1072
1073SDValue DAGTypeLegalizer::PromoteIntRes_MLOAD(MaskedLoadSDNode *N) {
1074 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1075 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
1076
1077 ISD::LoadExtType ExtType = N->getExtensionType();
1078 if (ExtType == ISD::NON_EXTLOAD)
1079 ExtType = ISD::EXTLOAD;
1080
1081 SDLoc dl(N);
1082 SDValue Res = DAG.getMaskedLoad(NVT, dl, N->getChain(), N->getBasePtr(),
1083 N->getOffset(), N->getMask(), ExtPassThru,
1084 N->getMemoryVT(), N->getMemOperand(),
1085 N->getAddressingMode(), ExtType,
1086 N->isExpandingLoad());
1087 // Legalize the chain result - switch anything that used the old chain to
1088 // use the new one.
1089 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1090 return Res;
1091}
1092
1093SDValue DAGTypeLegalizer::PromoteIntRes_MGATHER(MaskedGatherSDNode *N) {
1094 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1095 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
1096 assert(NVT == ExtPassThru.getValueType() &&
1097 "Gather result type and the passThru argument type should be the same");
1098
1099 ISD::LoadExtType ExtType = N->getExtensionType();
1100 if (ExtType == ISD::NON_EXTLOAD)
1101 ExtType = ISD::EXTLOAD;
1102
1103 SDLoc dl(N);
1104 SDValue Ops[] = {N->getChain(), ExtPassThru, N->getMask(), N->getBasePtr(),
1105 N->getIndex(), N->getScale() };
1106 SDValue Res = DAG.getMaskedGather(DAG.getVTList(NVT, MVT::Other),
1107 N->getMemoryVT(), dl, Ops,
1108 N->getMemOperand(), N->getIndexType(),
1109 ExtType);
1110 // Legalize the chain result - switch anything that used the old chain to
1111 // use the new one.
1112 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1113 return Res;
1114}
1115
1116SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_COMPRESS(SDNode *N) {
1117 SDValue Vec = GetPromotedInteger(N->getOperand(0));
1118 SDValue Passthru = GetPromotedInteger(N->getOperand(2));
1119 return DAG.getNode(ISD::VECTOR_COMPRESS, SDLoc(N), Vec.getValueType(), Vec,
1120 N->getOperand(1), Passthru);
1121}
1122
1123/// Promote the overflow flag of an overflowing arithmetic node.
1124SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
1125 // Change the return type of the boolean result while obeying
1126 // getSetCCResultType.
1127 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
1128 EVT VT = N->getValueType(0);
1129 EVT SVT = getSetCCResultType(VT);
1130 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1) };
1131 unsigned NumOps = N->getNumOperands();
1132 assert(NumOps <= 3 && "Too many operands");
1133 if (NumOps == 3)
1134 Ops[2] = PromoteTargetBoolean(N->getOperand(2), VT);
1135
1136 SDLoc dl(N);
1137 SDValue Res = DAG.getNode(N->getOpcode(), dl, DAG.getVTList(VT, SVT),
1138 ArrayRef(Ops, NumOps));
1139
1140 // Modified the sum result - switch anything that used the old sum to use
1141 // the new one.
1142 ReplaceValueWith(SDValue(N, 0), Res);
1143
1144 // Convert to the expected type.
1145 return DAG.getBoolExtOrTrunc(Res.getValue(1), dl, NVT, VT);
1146}
1147
1148template <class MatchContextClass>
1149SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBSHLSAT(SDNode *N) {
1150 // If the promoted type is legal, we can convert this to:
1151 // 1. ANY_EXTEND iN to iM
1152 // 2. SHL by M-N
1153 // 3. [US][ADD|SUB|SHL]SAT
1154 // 4. L/ASHR by M-N
1155 // Else it is more efficient to convert this to a min and a max
1156 // operation in the higher precision arithmetic.
1157 SDLoc dl(N);
1158 SDValue Op1 = N->getOperand(0);
1159 SDValue Op2 = N->getOperand(1);
1160 MatchContextClass matcher(DAG, TLI, N);
1161
1162 unsigned Opcode = matcher.getRootBaseOpcode();
1163 unsigned OldBits = Op1.getScalarValueSizeInBits();
1164
1165 // USUBSAT can always be promoted as long as we have zero/sign-extended the
1166 // args.
1167 if (Opcode == ISD::USUBSAT) {
1168 SExtOrZExtPromotedOperands(Op1, Op2);
1169 return matcher.getNode(ISD::USUBSAT, dl, Op1.getValueType(), Op1, Op2);
1170 }
1171
1172 if (Opcode == ISD::UADDSAT) {
1173 EVT OVT = Op1.getValueType();
1174 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
1175 // We can promote if we use sign-extend. Do this if the target prefers.
1176 if (TLI.isSExtCheaperThanZExt(OVT, NVT)) {
1177 Op1 = SExtPromotedInteger(Op1);
1178 Op2 = SExtPromotedInteger(Op2);
1179 return matcher.getNode(ISD::UADDSAT, dl, NVT, Op1, Op2);
1180 }
1181
1182 Op1 = ZExtPromotedInteger(Op1);
1183 Op2 = ZExtPromotedInteger(Op2);
1184 unsigned NewBits = NVT.getScalarSizeInBits();
1185 APInt MaxVal = APInt::getLowBitsSet(NewBits, OldBits);
1186 SDValue SatMax = DAG.getConstant(MaxVal, dl, NVT);
1187 SDValue Add = matcher.getNode(ISD::ADD, dl, NVT, Op1, Op2);
1188 return matcher.getNode(ISD::UMIN, dl, NVT, Add, SatMax);
1189 }
1190
1191 bool IsShift = Opcode == ISD::USHLSAT || Opcode == ISD::SSHLSAT;
1192
1193 // FIXME: We need vp-aware PromotedInteger functions.
1194 if (IsShift) {
1195 Op1 = GetPromotedInteger(Op1);
1196 if (getTypeAction(Op2.getValueType()) == TargetLowering::TypePromoteInteger)
1197 Op2 = ZExtPromotedInteger(Op2);
1198 } else {
1199 Op1 = SExtPromotedInteger(Op1);
1200 Op2 = SExtPromotedInteger(Op2);
1201 }
1202 EVT PromotedType = Op1.getValueType();
1203 unsigned NewBits = PromotedType.getScalarSizeInBits();
1204
1205 // Shift cannot use a min/max expansion, we can't detect overflow if all of
1206 // the bits have been shifted out.
1207 if (IsShift || matcher.isOperationLegal(Opcode, PromotedType)) {
1208 unsigned ShiftOp;
1209 switch (Opcode) {
1210 case ISD::SADDSAT:
1211 case ISD::SSUBSAT:
1212 case ISD::SSHLSAT:
1213 ShiftOp = ISD::SRA;
1214 break;
1215 case ISD::USHLSAT:
1216 ShiftOp = ISD::SRL;
1217 break;
1218 default:
1219 llvm_unreachable("Expected opcode to be signed or unsigned saturation "
1220 "addition, subtraction or left shift");
1221 }
1222
1223 unsigned SHLAmount = NewBits - OldBits;
1224 SDValue ShiftAmount =
1225 DAG.getShiftAmountConstant(SHLAmount, PromotedType, dl);
1226 Op1 = DAG.getNode(ISD::SHL, dl, PromotedType, Op1, ShiftAmount);
1227 if (!IsShift)
1228 Op2 = matcher.getNode(ISD::SHL, dl, PromotedType, Op2, ShiftAmount);
1229
1230 SDValue Result = matcher.getNode(Opcode, dl, PromotedType, Op1, Op2);
1231 return matcher.getNode(ShiftOp, dl, PromotedType, Result, ShiftAmount);
1232 }
1233
1234 unsigned AddOp = Opcode == ISD::SADDSAT ? ISD::ADD : ISD::SUB;
1235 APInt MinVal = APInt::getSignedMinValue(OldBits).sext(NewBits);
1236 APInt MaxVal = APInt::getSignedMaxValue(OldBits).sext(NewBits);
1237 SDValue SatMin = DAG.getConstant(MinVal, dl, PromotedType);
1238 SDValue SatMax = DAG.getConstant(MaxVal, dl, PromotedType);
1239 SDValue Result = matcher.getNode(AddOp, dl, PromotedType, Op1, Op2);
1240 Result = matcher.getNode(ISD::SMIN, dl, PromotedType, Result, SatMax);
1241 Result = matcher.getNode(ISD::SMAX, dl, PromotedType, Result, SatMin);
1242 return Result;
1243}
1244
1245SDValue DAGTypeLegalizer::PromoteIntRes_MULFIX(SDNode *N) {
1246 // Can just promote the operands then continue with operation.
1247 SDLoc dl(N);
1248 SDValue Op1Promoted, Op2Promoted;
1249 bool Signed =
1250 N->getOpcode() == ISD::SMULFIX || N->getOpcode() == ISD::SMULFIXSAT;
1251 bool Saturating =
1252 N->getOpcode() == ISD::SMULFIXSAT || N->getOpcode() == ISD::UMULFIXSAT;
1253 if (Signed) {
1254 Op1Promoted = SExtPromotedInteger(N->getOperand(0));
1255 Op2Promoted = SExtPromotedInteger(N->getOperand(1));
1256 } else {
1257 Op1Promoted = ZExtPromotedInteger(N->getOperand(0));
1258 Op2Promoted = ZExtPromotedInteger(N->getOperand(1));
1259 }
1260 EVT OldType = N->getOperand(0).getValueType();
1261 EVT PromotedType = Op1Promoted.getValueType();
1262 unsigned DiffSize =
1263 PromotedType.getScalarSizeInBits() - OldType.getScalarSizeInBits();
1264
1265 if (Saturating) {
1266 // Promoting the operand and result values changes the saturation width,
1267 // which is extends the values that we clamp to on saturation. This could be
1268 // resolved by shifting one of the operands the same amount, which would
1269 // also shift the result we compare against, then shifting back.
1270 Op1Promoted =
1271 DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted,
1272 DAG.getShiftAmountConstant(DiffSize, PromotedType, dl));
1273 SDValue Result = DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted,
1274 Op2Promoted, N->getOperand(2));
1275 unsigned ShiftOp = Signed ? ISD::SRA : ISD::SRL;
1276 return DAG.getNode(ShiftOp, dl, PromotedType, Result,
1277 DAG.getShiftAmountConstant(DiffSize, PromotedType, dl));
1278 }
1279 return DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted, Op2Promoted,
1280 N->getOperand(2));
1281}
1282
1284 unsigned SatW, bool Signed,
1285 const TargetLowering &TLI,
1286 SelectionDAG &DAG) {
1287 EVT VT = V.getValueType();
1288 unsigned VTW = VT.getScalarSizeInBits();
1289
1290 if (!Signed) {
1291 // Saturate to the unsigned maximum by getting the minimum of V and the
1292 // maximum.
1293 return DAG.getNode(ISD::UMIN, dl, VT, V,
1294 DAG.getConstant(APInt::getLowBitsSet(VTW, SatW),
1295 dl, VT));
1296 }
1297
1298 // Saturate to the signed maximum (the low SatW - 1 bits) by taking the
1299 // signed minimum of it and V.
1300 V = DAG.getNode(ISD::SMIN, dl, VT, V,
1301 DAG.getConstant(APInt::getLowBitsSet(VTW, SatW - 1),
1302 dl, VT));
1303 // Saturate to the signed minimum (the high SatW + 1 bits) by taking the
1304 // signed maximum of it and V.
1305 V = DAG.getNode(ISD::SMAX, dl, VT, V,
1306 DAG.getConstant(APInt::getHighBitsSet(VTW, VTW - SatW + 1),
1307 dl, VT));
1308 return V;
1309}
1310
1312 unsigned Scale, const TargetLowering &TLI,
1313 SelectionDAG &DAG, unsigned SatW = 0) {
1314 EVT VT = LHS.getValueType();
1315 unsigned VTSize = VT.getScalarSizeInBits();
1316 bool Signed = N->getOpcode() == ISD::SDIVFIX ||
1317 N->getOpcode() == ISD::SDIVFIXSAT;
1318 bool Saturating = N->getOpcode() == ISD::SDIVFIXSAT ||
1319 N->getOpcode() == ISD::UDIVFIXSAT;
1320
1321 SDLoc dl(N);
1322 // Widen the types by a factor of two. This is guaranteed to expand, since it
1323 // will always have enough high bits in the LHS to shift into.
1324 EVT WideVT = VT.changeElementType(
1325 *DAG.getContext(), EVT::getIntegerVT(*DAG.getContext(), VTSize * 2));
1326 LHS = DAG.getExtOrTrunc(Signed, LHS, dl, WideVT);
1327 RHS = DAG.getExtOrTrunc(Signed, RHS, dl, WideVT);
1328 SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, LHS, RHS, Scale,
1329 DAG);
1330 assert(Res && "Expanding DIVFIX with wide type failed?");
1331 if (Saturating) {
1332 // If the caller has told us to saturate at something less, use that width
1333 // instead of the type before doubling. However, it cannot be more than
1334 // what we just widened!
1335 assert(SatW <= VTSize &&
1336 "Tried to saturate to more than the original type?");
1337 Res = SaturateWidenedDIVFIX(Res, dl, SatW == 0 ? VTSize : SatW, Signed,
1338 TLI, DAG);
1339 }
1340 return DAG.getZExtOrTrunc(Res, dl, VT);
1341}
1342
1343SDValue DAGTypeLegalizer::PromoteIntRes_DIVFIX(SDNode *N) {
1344 SDLoc dl(N);
1345 SDValue Op1Promoted, Op2Promoted;
1346 bool Signed = N->getOpcode() == ISD::SDIVFIX ||
1347 N->getOpcode() == ISD::SDIVFIXSAT;
1348 bool Saturating = N->getOpcode() == ISD::SDIVFIXSAT ||
1349 N->getOpcode() == ISD::UDIVFIXSAT;
1350 if (Signed) {
1351 Op1Promoted = SExtPromotedInteger(N->getOperand(0));
1352 Op2Promoted = SExtPromotedInteger(N->getOperand(1));
1353 } else {
1354 Op1Promoted = ZExtPromotedInteger(N->getOperand(0));
1355 Op2Promoted = ZExtPromotedInteger(N->getOperand(1));
1356 }
1357 EVT PromotedType = Op1Promoted.getValueType();
1358 unsigned Scale = N->getConstantOperandVal(2);
1359
1360 // If the type is already legal and the operation is legal in that type, we
1361 // should not early expand.
1362 if (TLI.isTypeLegal(PromotedType)) {
1364 TLI.getFixedPointOperationAction(N->getOpcode(), PromotedType, Scale);
1365 if (Action == TargetLowering::Legal || Action == TargetLowering::Custom) {
1366 unsigned Diff = PromotedType.getScalarSizeInBits() -
1367 N->getValueType(0).getScalarSizeInBits();
1368 if (Saturating)
1369 Op1Promoted =
1370 DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted,
1371 DAG.getShiftAmountConstant(Diff, PromotedType, dl));
1372 SDValue Res = DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted,
1373 Op2Promoted, N->getOperand(2));
1374 if (Saturating)
1375 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, dl, PromotedType, Res,
1376 DAG.getShiftAmountConstant(Diff, PromotedType, dl));
1377 return Res;
1378 }
1379 }
1380
1381 // See if we can perform the division in this type without expanding.
1382 if (SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, Op1Promoted,
1383 Op2Promoted, Scale, DAG)) {
1384 if (Saturating)
1385 Res = SaturateWidenedDIVFIX(Res, dl,
1386 N->getValueType(0).getScalarSizeInBits(),
1387 Signed, TLI, DAG);
1388 return Res;
1389 }
1390 // If we cannot, expand it to twice the type width. If we are saturating, give
1391 // it the original width as a saturating width so we don't need to emit
1392 // two saturations.
1393 return earlyExpandDIVFIX(N, Op1Promoted, Op2Promoted, Scale, TLI, DAG,
1394 N->getValueType(0).getScalarSizeInBits());
1395}
1396
1397SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
1398 if (ResNo == 1)
1399 return PromoteIntRes_Overflow(N);
1400
1401 // The operation overflowed iff the result in the larger type is not the
1402 // sign extension of its truncation to the original type.
1403 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1404 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1405 EVT OVT = N->getOperand(0).getValueType();
1406 EVT NVT = LHS.getValueType();
1407 SDLoc dl(N);
1408
1409 // Do the arithmetic in the larger type.
1410 unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
1411 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
1412
1413 // Calculate the overflow flag: sign extend the arithmetic result from
1414 // the original type.
1415 SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
1416 DAG.getValueType(OVT));
1417 // Overflowed if and only if this is not equal to Res.
1418 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
1419
1420 // Use the calculated overflow everywhere.
1421 ReplaceValueWith(SDValue(N, 1), Ofl);
1422
1423 return Res;
1424}
1425
1426SDValue DAGTypeLegalizer::PromoteIntRes_CMP(SDNode *N) {
1427 EVT PromotedResultTy =
1428 TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1429 return DAG.getNode(N->getOpcode(), SDLoc(N), PromotedResultTy,
1430 N->getOperand(0), N->getOperand(1));
1431}
1432
1433SDValue DAGTypeLegalizer::PromoteIntRes_Select(SDNode *N) {
1434 SDValue Mask = N->getOperand(0);
1435
1436 SDValue LHS = GetPromotedInteger(N->getOperand(1));
1437 SDValue RHS = GetPromotedInteger(N->getOperand(2));
1438
1439 unsigned Opcode = N->getOpcode();
1440 if (Opcode == ISD::VP_SELECT || Opcode == ISD::VP_MERGE)
1441 return DAG.getNode(Opcode, SDLoc(N), LHS.getValueType(), Mask, LHS, RHS,
1442 N->getOperand(3));
1443 return DAG.getNode(Opcode, SDLoc(N), LHS.getValueType(), Mask, LHS, RHS);
1444}
1445
1446SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
1447 SDValue LHS = GetPromotedInteger(N->getOperand(2));
1448 SDValue RHS = GetPromotedInteger(N->getOperand(3));
1449 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
1450 LHS.getValueType(), N->getOperand(0),
1451 N->getOperand(1), LHS, RHS, N->getOperand(4));
1452}
1453
1454SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
1455 unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0;
1456 EVT InVT = N->getOperand(OpNo).getValueType();
1457 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1458
1459 EVT SVT = getSetCCResultType(InVT);
1460
1461 // If we got back a type that needs to be promoted, this likely means the
1462 // the input type also needs to be promoted. So get the promoted type for
1463 // the input and try the query again.
1464 if (getTypeAction(SVT) == TargetLowering::TypePromoteInteger) {
1465 if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
1466 InVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
1467 SVT = getSetCCResultType(InVT);
1468 } else {
1469 // Input type isn't promoted, just use the default promoted type.
1470 SVT = NVT;
1471 }
1472 }
1473
1474 SDLoc dl(N);
1475 assert(SVT.isVector() == N->getOperand(OpNo).getValueType().isVector() &&
1476 "Vector compare must return a vector result!");
1477
1478 // Get the SETCC result using the canonical SETCC type.
1479 SDValue SetCC;
1480 if (N->isStrictFPOpcode()) {
1481 SDVTList VTs = DAG.getVTList({SVT, MVT::Other});
1482 SDValue Opers[] = {N->getOperand(0), N->getOperand(1),
1483 N->getOperand(2), N->getOperand(3)};
1484 SetCC = DAG.getNode(N->getOpcode(), dl, VTs, Opers, N->getFlags());
1485 // Legalize the chain result - switch anything that used the old chain to
1486 // use the new one.
1487 ReplaceValueWith(SDValue(N, 1), SetCC.getValue(1));
1488 } else
1489 SetCC = DAG.getNode(N->getOpcode(), dl, SVT, N->getOperand(0),
1490 N->getOperand(1), N->getOperand(2), N->getFlags());
1491
1492 // Convert to the expected type.
1493 return DAG.getSExtOrTrunc(SetCC, dl, NVT);
1494}
1495
1496SDValue DAGTypeLegalizer::PromoteIntRes_IS_FPCLASS(SDNode *N) {
1497 SDLoc DL(N);
1498 SDValue Arg = N->getOperand(0);
1499 SDValue Test = N->getOperand(1);
1500 EVT NResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1501 return DAG.getNode(ISD::IS_FPCLASS, DL, NResVT, Arg, Test);
1502}
1503
1504SDValue DAGTypeLegalizer::PromoteIntRes_FFREXP(SDNode *N) {
1505 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
1506 EVT VT = N->getValueType(0);
1507
1508 SDLoc dl(N);
1509 SDValue Res =
1510 DAG.getNode(N->getOpcode(), dl, DAG.getVTList(VT, NVT), N->getOperand(0));
1511
1512 ReplaceValueWith(SDValue(N, 0), Res);
1513 return Res.getValue(1);
1514}
1515
1516SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
1517 SDValue LHS = GetPromotedInteger(N->getOperand(0));
1518 SDValue RHS = N->getOperand(1);
1519 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1520 RHS = ZExtPromotedInteger(RHS);
1521 if (N->getOpcode() != ISD::VP_SHL)
1522 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1523
1524 SDValue Mask = N->getOperand(2);
1525 SDValue EVL = N->getOperand(3);
1526 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1527 Mask, EVL);
1528}
1529
1530SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
1531 SDValue Op = GetPromotedInteger(N->getOperand(0));
1532 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N),
1533 Op.getValueType(), Op, N->getOperand(1));
1534}
1535
1536SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
1537 // The input may have strange things in the top bits of the registers, but
1538 // these operations don't care. They may have weird bits going out, but
1539 // that too is okay if they are integer operations.
1540 SDValue LHS = GetPromotedInteger(N->getOperand(0));
1541 SDValue RHS = GetPromotedInteger(N->getOperand(1));
1542 if (N->getNumOperands() == 2)
1543 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1544 assert(N->getNumOperands() == 4 && "Unexpected number of operands!");
1545 assert(N->isVPOpcode() && "Expected VP opcode");
1546 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1547 N->getOperand(2), N->getOperand(3));
1548}
1549
1550SDValue DAGTypeLegalizer::PromoteIntRes_SExtIntBinOp(SDNode *N) {
1551 // Sign extend the input.
1552 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1553 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1554 if (N->getNumOperands() == 2)
1555 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1556 assert(N->getNumOperands() == 4 && "Unexpected number of operands!");
1557 assert(N->isVPOpcode() && "Expected VP opcode");
1558 SDValue Mask = N->getOperand(2);
1559 SDValue EVL = N->getOperand(3);
1560 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1561 Mask, EVL);
1562}
1563
1564SDValue DAGTypeLegalizer::PromoteIntRes_ZExtIntBinOp(SDNode *N) {
1565 // Zero extend the input.
1566 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1567 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
1568 if (N->getNumOperands() == 2)
1569 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1570 assert(N->getNumOperands() == 4 && "Unexpected number of operands!");
1571 assert(N->isVPOpcode() && "Expected VP opcode");
1572 // Zero extend the input.
1573 SDValue Mask = N->getOperand(2);
1574 SDValue EVL = N->getOperand(3);
1575 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1576 Mask, EVL);
1577}
1578
1579SDValue DAGTypeLegalizer::PromoteIntRes_ZExtMaskedIntBinOp(SDNode *N) {
1580 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1581 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
1582 SDValue Mask = N->getOperand(2);
1583 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1584 Mask);
1585}
1586
1587SDValue DAGTypeLegalizer::PromoteIntRes_SExtMaskedIntBinOp(SDNode *N) {
1588 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1589 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1590 SDValue Mask = N->getOperand(2);
1591 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1592 Mask);
1593}
1594
1595SDValue DAGTypeLegalizer::PromoteIntRes_UMINUMAX(SDNode *N) {
1596 SDValue LHS = N->getOperand(0);
1597 SDValue RHS = N->getOperand(1);
1598
1599 // It doesn't matter if we sign extend or zero extend in the inputs. So do
1600 // whatever is best for the target and the promoted operands.
1601 SExtOrZExtPromotedOperands(LHS, RHS);
1602
1603 return DAG.getNode(N->getOpcode(), SDLoc(N),
1604 LHS.getValueType(), LHS, RHS);
1605}
1606
1607SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
1608 // The input value must be properly sign extended.
1609 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1610 SDValue RHS = N->getOperand(1);
1611 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1612 RHS = ZExtPromotedInteger(RHS);
1613 if (N->getOpcode() != ISD::VP_SRA)
1614 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1615
1616 SDValue Mask = N->getOperand(2);
1617 SDValue EVL = N->getOperand(3);
1618 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1619 Mask, EVL);
1620}
1621
1622SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
1623 SDValue RHS = N->getOperand(1);
1624 // The input value must be properly zero extended.
1625 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1626 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1627 RHS = ZExtPromotedInteger(RHS);
1628 if (N->getOpcode() != ISD::VP_SRL)
1629 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1630
1631 SDValue Mask = N->getOperand(2);
1632 SDValue EVL = N->getOperand(3);
1633 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1634 Mask, EVL);
1635}
1636
1637SDValue DAGTypeLegalizer::PromoteIntRes_Rotate(SDNode *N) {
1638 EVT OldVT = N->getValueType(0);
1639 EVT VT = TLI.getTypeToTransformTo(*DAG.getContext(), OldVT);
1640 SDValue Amt = N->getOperand(1);
1641 unsigned Opcode = N->getOpcode();
1642 unsigned OldBits = OldVT.getScalarSizeInBits();
1643 unsigned NewBits = VT.getScalarSizeInBits();
1644
1645 // If the promoted type is twice the size (or more), then we can concatenate
1646 // the value with itself and treat this similar to a funnel shift. This isn't
1647 // necessary if the rotate amount is constant or if shl/srl of the original
1648 // type are custom lowered.
1649 // rotl(x,amt) -> (((aext(x) << bw) | zext(x)) << (amt % bw)) >> bw.
1650 // rotr(x,amt) -> (((aext(x) << bw) | zext(x)) >> (amt % bw)).
1651 if (NewBits >= (2 * OldBits) && !isa<ConstantSDNode>(Amt) &&
1652 !TLI.isOperationLegalOrCustom(Opcode, VT) &&
1653 TLI.getOperationAction(ISD::SHL, OldVT) != TargetLowering::Custom &&
1654 TLI.getOperationAction(ISD::SRL, OldVT) != TargetLowering::Custom) {
1655 SDValue Op0 = GetPromotedInteger(N->getOperand(0));
1656 if (getTypeAction(Amt.getValueType()) == TargetLowering::TypePromoteInteger)
1657 Amt = ZExtPromotedInteger(Amt);
1658 EVT AmtVT = Amt.getValueType();
1659
1660 SDLoc DL(N);
1661 // Amount has to be interpreted modulo the old bit width.
1662 Amt = DAG.getNode(ISD::UREM, DL, AmtVT, Amt,
1663 DAG.getConstant(OldBits, DL, AmtVT));
1664 SDValue HiShift = DAG.getShiftAmountConstant(OldBits, VT, DL);
1665 SDValue Hi = DAG.getNode(ISD::SHL, DL, VT, Op0, HiShift);
1666 SDValue Lo = DAG.getZeroExtendInReg(Op0, DL, OldVT);
1667 SDValue Res = DAG.getNode(ISD::OR, DL, VT, Hi, Lo);
1668 bool IsROTR = N->getOpcode() == ISD::ROTR;
1669 Res = DAG.getNode(IsROTR ? ISD::SRL : ISD::SHL, DL, VT, Res, Amt);
1670 // FIXME: We can avoid this by using ROTL when the promoted type is exactly
1671 // twice the size.
1672 if (!IsROTR)
1673 Res = DAG.getNode(ISD::SRL, DL, VT, Res, HiShift);
1674 return Res;
1675 }
1676
1677 // Lower the rotate to shifts and ORs which can be promoted.
1678 SDValue Res = TLI.expandROT(N, true /*AllowVectorOps*/, DAG);
1679 ReplaceValueWith(SDValue(N, 0), Res);
1680 return SDValue();
1681}
1682
1683SDValue DAGTypeLegalizer::PromoteIntRes_FunnelShift(SDNode *N) {
1684 SDValue Hi = GetPromotedInteger(N->getOperand(0));
1685 SDValue Lo = GetPromotedInteger(N->getOperand(1));
1686 SDValue Amt = N->getOperand(2);
1687 if (getTypeAction(Amt.getValueType()) == TargetLowering::TypePromoteInteger)
1688 Amt = ZExtPromotedInteger(Amt);
1689 EVT AmtVT = Amt.getValueType();
1690
1691 SDLoc DL(N);
1692 EVT OldVT = N->getOperand(0).getValueType();
1693 EVT VT = Lo.getValueType();
1694 unsigned Opcode = N->getOpcode();
1695 bool IsFSHR = Opcode == ISD::FSHR;
1696 unsigned OldBits = OldVT.getScalarSizeInBits();
1697 unsigned NewBits = VT.getScalarSizeInBits();
1698
1699 // Amount has to be interpreted modulo the old bit width.
1700 Amt = DAG.getNode(ISD::UREM, DL, AmtVT, Amt,
1701 DAG.getConstant(OldBits, DL, AmtVT));
1702
1703 // If the promoted type is twice the size (or more), then we use the
1704 // traditional funnel 'double' shift codegen. This isn't necessary if the
1705 // shift amount is constant.
1706 // fshl(x,y,z) -> (((aext(x) << bw) | zext(y)) << (z % bw)) >> bw.
1707 // fshr(x,y,z) -> (((aext(x) << bw) | zext(y)) >> (z % bw)).
1708 if (NewBits >= (2 * OldBits) && !isa<ConstantSDNode>(Amt) &&
1709 !TLI.isOperationLegalOrCustom(Opcode, VT)) {
1710 SDValue HiShift = DAG.getShiftAmountConstant(OldBits, VT, DL);
1711 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, HiShift);
1712 Lo = DAG.getZeroExtendInReg(Lo, DL, OldVT);
1713 SDValue Res = DAG.getNode(ISD::OR, DL, VT, Hi, Lo);
1714 Res = DAG.getNode(IsFSHR ? ISD::SRL : ISD::SHL, DL, VT, Res, Amt);
1715 if (!IsFSHR)
1716 Res = DAG.getNode(ISD::SRL, DL, VT, Res, HiShift);
1717 return Res;
1718 }
1719
1720 // Shift Lo up to occupy the upper bits of the promoted type.
1721 Lo = DAG.getNode(ISD::SHL, DL, VT, Lo,
1722 DAG.getShiftAmountConstant(NewBits - OldBits, VT, DL));
1723
1724 // Increase Amount to shift the result into the lower bits of the promoted
1725 // type.
1726 if (IsFSHR)
1727 Amt = DAG.getNode(ISD::ADD, DL, AmtVT, Amt,
1728 DAG.getConstant(NewBits - OldBits, DL, AmtVT));
1729
1730 return DAG.getNode(Opcode, DL, VT, Hi, Lo, Amt);
1731}
1732
1733// A vp version of PromoteIntRes_FunnelShift.
1734SDValue DAGTypeLegalizer::PromoteIntRes_VPFunnelShift(SDNode *N) {
1735 SDValue Hi = GetPromotedInteger(N->getOperand(0));
1736 SDValue Lo = GetPromotedInteger(N->getOperand(1));
1737 SDValue Amt = N->getOperand(2);
1738 SDValue Mask = N->getOperand(3);
1739 SDValue EVL = N->getOperand(4);
1740 if (getTypeAction(Amt.getValueType()) == TargetLowering::TypePromoteInteger)
1741 Amt = ZExtPromotedInteger(Amt);
1742 EVT AmtVT = Amt.getValueType();
1743
1744 SDLoc DL(N);
1745 EVT OldVT = N->getOperand(0).getValueType();
1746 EVT VT = Lo.getValueType();
1747 unsigned Opcode = N->getOpcode();
1748 bool IsFSHR = Opcode == ISD::VP_FSHR;
1749 unsigned OldBits = OldVT.getScalarSizeInBits();
1750 unsigned NewBits = VT.getScalarSizeInBits();
1751
1752 // Amount has to be interpreted modulo the old bit width.
1753 Amt = DAG.getNode(ISD::VP_UREM, DL, AmtVT, Amt,
1754 DAG.getConstant(OldBits, DL, AmtVT), Mask, EVL);
1755
1756 // If the promoted type is twice the size (or more), then we use the
1757 // traditional funnel 'double' shift codegen. This isn't necessary if the
1758 // shift amount is constant.
1759 // fshl(x,y,z) -> (((aext(x) << bw) | zext(y)) << (z % bw)) >> bw.
1760 // fshr(x,y,z) -> (((aext(x) << bw) | zext(y)) >> (z % bw)).
1761 if (NewBits >= (2 * OldBits) && !isa<ConstantSDNode>(Amt) &&
1762 !TLI.isOperationLegalOrCustom(Opcode, VT)) {
1763 SDValue HiShift = DAG.getConstant(OldBits, DL, VT);
1764 Hi = DAG.getNode(ISD::VP_SHL, DL, VT, Hi, HiShift, Mask, EVL);
1765 Lo = DAG.getVPZeroExtendInReg(Lo, Mask, EVL, DL, OldVT);
1766 SDValue Res = DAG.getNode(ISD::VP_OR, DL, VT, Hi, Lo, Mask, EVL);
1767 Res = DAG.getNode(IsFSHR ? ISD::VP_SRL : ISD::VP_SHL, DL, VT, Res, Amt,
1768 Mask, EVL);
1769 if (!IsFSHR)
1770 Res = DAG.getNode(ISD::VP_SRL, DL, VT, Res, HiShift, Mask, EVL);
1771 return Res;
1772 }
1773
1774 // Shift Lo up to occupy the upper bits of the promoted type.
1775 SDValue ShiftOffset = DAG.getConstant(NewBits - OldBits, DL, AmtVT);
1776 Lo = DAG.getNode(ISD::VP_SHL, DL, VT, Lo, ShiftOffset, Mask, EVL);
1777
1778 // Increase Amount to shift the result into the lower bits of the promoted
1779 // type.
1780 if (IsFSHR)
1781 Amt = DAG.getNode(ISD::VP_ADD, DL, AmtVT, Amt, ShiftOffset, Mask, EVL);
1782
1783 return DAG.getNode(Opcode, DL, VT, Hi, Lo, Amt, Mask, EVL);
1784}
1785
1786SDValue DAGTypeLegalizer::PromoteIntRes_CLMUL(SDNode *N) {
1787 unsigned Opcode = N->getOpcode();
1788
1789 SDLoc DL(N);
1790 EVT OldVT = N->getOperand(0).getValueType();
1791 EVT VT = TLI.getTypeToTransformTo(*DAG.getContext(), OldVT);
1792
1793 if (Opcode == ISD::CLMUL) {
1794 if (!TLI.isOperationLegalOrCustomOrPromote(ISD::CLMUL, VT)) {
1795 if (SDValue Res = TLI.expandCLMUL(N, DAG))
1796 return DAG.getNode(ISD::ANY_EXTEND, DL, VT, Res);
1797 }
1798 SDValue X = GetPromotedInteger(N->getOperand(0));
1799 SDValue Y = GetPromotedInteger(N->getOperand(1));
1800 return DAG.getNode(ISD::CLMUL, DL, VT, X, Y);
1801 }
1802
1803 SDValue X = ZExtPromotedInteger(N->getOperand(0));
1804 SDValue Y = ZExtPromotedInteger(N->getOperand(1));
1805
1806 unsigned OldBits = OldVT.getScalarSizeInBits();
1807 unsigned NewBits = VT.getScalarSizeInBits();
1808 if (NewBits < 2 * OldBits) {
1809 SDValue Clmul = DAG.getNode(ISD::CLMUL, DL, VT, X, Y);
1810 unsigned ShAmt = Opcode == ISD::CLMULH ? OldBits : OldBits - 1;
1811 SDValue Lo = DAG.getNode(ISD::SRL, DL, VT, Clmul,
1812 DAG.getShiftAmountConstant(ShAmt, VT, DL));
1813 SDValue Clmulh = DAG.getNode(ISD::CLMULH, DL, VT, X, Y);
1814 ShAmt = Opcode == ISD::CLMULH ? NewBits - OldBits : NewBits - OldBits + 1;
1815 SDValue Hi = DAG.getNode(ISD::SHL, DL, VT, Clmulh,
1816 DAG.getShiftAmountConstant(ShAmt, VT, DL));
1817 return DAG.getNode(ISD::OR, DL, VT, Lo, Hi);
1818 }
1819
1820 SDValue Clmul = DAG.getNode(ISD::CLMUL, DL, VT, X, Y);
1821 unsigned ShAmt = Opcode == ISD::CLMULH ? OldBits : OldBits - 1;
1822 return DAG.getNode(ISD::SRL, DL, VT, Clmul,
1823 DAG.getShiftAmountConstant(ShAmt, VT, DL));
1824}
1825
1826SDValue DAGTypeLegalizer::PromoteIntRes_PEXT(SDNode *N) {
1827 SDLoc DL(N);
1828 EVT VT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1829 if (!TLI.isOperationLegalOrCustomOrPromote(ISD::PEXT, VT)) {
1830 if (SDValue Res = TLI.expandPEXT(N, DAG))
1831 return DAG.getNode(ISD::ANY_EXTEND, DL, VT, Res);
1832 }
1833 // Only the mask operand needs zero-extension because the implicit AND from
1834 // masking clears the corresponding bits in X anyway.
1835 SDValue X = GetPromotedInteger(N->getOperand(0));
1836 SDValue Y = ZExtPromotedInteger(N->getOperand(1));
1837 return DAG.getNode(ISD::PEXT, DL, VT, X, Y);
1838}
1839
1840SDValue DAGTypeLegalizer::PromoteIntRes_PDEP(SDNode *N) {
1841 SDLoc DL(N);
1842 EVT VT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1843 if (!TLI.isOperationLegalOrCustomOrPromote(ISD::PDEP, VT)) {
1844 if (SDValue Res = TLI.expandPDEP(N, DAG))
1845 return DAG.getNode(ISD::ANY_EXTEND, DL, VT, Res);
1846 }
1847 // Neither operand needs zero-extension because the upper operand bits could
1848 // only result in depositing result bits that will be discarded.
1849 SDValue X = GetPromotedInteger(N->getOperand(0));
1850 SDValue Y = GetPromotedInteger(N->getOperand(1));
1851 return DAG.getNode(ISD::PDEP, DL, VT, X, Y);
1852}
1853
1854SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
1855 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1856 SDValue Res;
1857 SDValue InOp = N->getOperand(0);
1858 SDLoc dl(N);
1859
1860 switch (getTypeAction(InOp.getValueType())) {
1861 default: llvm_unreachable("Unknown type action!");
1864 Res = InOp;
1865 break;
1867 Res = GetPromotedInteger(InOp);
1868 break;
1870 EVT InVT = InOp.getValueType();
1871 assert(InVT.isVector() && "Cannot split scalar types");
1872 ElementCount NumElts = InVT.getVectorElementCount();
1873 assert(NumElts == NVT.getVectorElementCount() &&
1874 "Dst and Src must have the same number of elements");
1876 "Promoted vector type must be a power of two");
1877
1878 SDValue EOp1, EOp2;
1879 GetSplitVector(InOp, EOp1, EOp2);
1880
1881 EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(),
1882 NumElts.divideCoefficientBy(2));
1883 if (N->getOpcode() == ISD::TRUNCATE) {
1884 EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1);
1885 EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2);
1886 } else {
1887 assert(N->getOpcode() == ISD::VP_TRUNCATE &&
1888 "Expected VP_TRUNCATE opcode");
1889 SDValue MaskLo, MaskHi, EVLLo, EVLHi;
1890 std::tie(MaskLo, MaskHi) = SplitMask(N->getOperand(1));
1891 std::tie(EVLLo, EVLHi) =
1892 DAG.SplitEVL(N->getOperand(2), N->getValueType(0), dl);
1893 EOp1 = DAG.getNode(ISD::VP_TRUNCATE, dl, HalfNVT, EOp1, MaskLo, EVLLo);
1894 EOp2 = DAG.getNode(ISD::VP_TRUNCATE, dl, HalfNVT, EOp2, MaskHi, EVLHi);
1895 }
1896 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2);
1897 }
1898 // TODO: VP_TRUNCATE need to handle when TypeWidenVector access to some
1899 // targets.
1901 SDValue WideInOp = GetWidenedVector(InOp);
1902
1903 // Truncate widened InOp.
1904 unsigned NumElem = WideInOp.getValueType().getVectorNumElements();
1905 EVT TruncVT = EVT::getVectorVT(*DAG.getContext(),
1906 N->getValueType(0).getScalarType(), NumElem);
1907 SDValue WideTrunc = DAG.getNode(ISD::TRUNCATE, dl, TruncVT, WideInOp);
1908
1909 // Zero extend so that the elements are of same type as those of NVT
1910 EVT ExtVT = EVT::getVectorVT(*DAG.getContext(), NVT.getVectorElementType(),
1911 NumElem);
1912 SDValue WideExt = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtVT, WideTrunc);
1913
1914 // Extract the low NVT subvector.
1915 SDValue ZeroIdx = DAG.getVectorIdxConstant(0, dl);
1916 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, WideExt, ZeroIdx);
1917 }
1918 }
1919
1920 // Truncate to NVT instead of VT
1921 if (N->getOpcode() == ISD::VP_TRUNCATE)
1922 return DAG.getNode(ISD::VP_TRUNCATE, dl, NVT, Res, N->getOperand(1),
1923 N->getOperand(2));
1924 return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res);
1925}
1926
1927SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
1928 if (ResNo == 1)
1929 return PromoteIntRes_Overflow(N);
1930
1931 // The operation overflowed iff the result in the larger type is not the
1932 // zero extension of its truncation to the original type.
1933 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1934 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
1935 EVT OVT = N->getOperand(0).getValueType();
1936 EVT NVT = LHS.getValueType();
1937 SDLoc dl(N);
1938
1939 // Do the arithmetic in the larger type.
1940 unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
1941 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
1942
1943 // Calculate the overflow flag: zero extend the arithmetic result from
1944 // the original type.
1945 SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT);
1946 // Overflowed if and only if this is not equal to Res.
1947 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
1948
1949 // Use the calculated overflow everywhere.
1950 ReplaceValueWith(SDValue(N, 1), Ofl);
1951
1952 return Res;
1953}
1954
1955// Handle promotion for the ADDE/SUBE/UADDO_CARRY/USUBO_CARRY nodes. Notice that
1956// the third operand of ADDE/SUBE nodes is carry flag, which differs from
1957// the UADDO_CARRY/USUBO_CARRY nodes in that the third operand is carry Boolean.
1958SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO_CARRY(SDNode *N,
1959 unsigned ResNo) {
1960 if (ResNo == 1)
1961 return PromoteIntRes_Overflow(N);
1962
1963 // We need to sign-extend the operands so the carry value computed by the
1964 // wide operation will be equivalent to the carry value computed by the
1965 // narrow operation.
1966 // An UADDO_CARRY can generate carry only if any of the operands has its
1967 // most significant bit set. Sign extension propagates the most significant
1968 // bit into the higher bits which means the extra bit that the narrow
1969 // addition would need (i.e. the carry) will be propagated through the higher
1970 // bits of the wide addition.
1971 // A USUBO_CARRY can generate borrow only if LHS < RHS and this property will
1972 // be preserved by sign extension.
1973 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1974 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1975
1976 EVT ValueVTs[] = {LHS.getValueType(), N->getValueType(1)};
1977
1978 // Do the arithmetic in the wide type.
1979 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N), DAG.getVTList(ValueVTs),
1980 LHS, RHS, N->getOperand(2));
1981
1982 // Update the users of the original carry/borrow value.
1983 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1984
1985 return SDValue(Res.getNode(), 0);
1986}
1987
1988SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO_CARRY(SDNode *N,
1989 unsigned ResNo) {
1990 assert(ResNo == 1 && "Don't know how to promote other results yet.");
1991 return PromoteIntRes_Overflow(N);
1992}
1993
1994SDValue DAGTypeLegalizer::PromoteIntRes_ABS(SDNode *N) {
1995 EVT OVT = N->getValueType(0);
1996 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
1997
1998 // If a larger ABS or SMAX isn't supported by the target, try to expand now.
1999 // If we expand later we'll end up sign extending more than just the sra input
2000 // in sra+xor+sub expansion.
2001 if (!OVT.isVector() &&
2002 !TLI.isOperationLegalOrCustomOrPromote(ISD::ABS, NVT) &&
2003 !TLI.isOperationLegalOrCustomOrPromote(ISD::ABS_MIN_POISON, NVT) &&
2004 !TLI.isOperationLegal(ISD::SMAX, NVT)) {
2005 if (SDValue Res = TLI.expandABS(N, DAG))
2006 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), NVT, Res);
2007 }
2008
2009 SDValue Op0 = SExtPromotedInteger(N->getOperand(0));
2010 return DAG.getNode(ISD::ABS_MIN_POISON, SDLoc(N), Op0.getValueType(), Op0);
2011}
2012
2013SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
2014 // Promote the overflow bit trivially.
2015 if (ResNo == 1)
2016 return PromoteIntRes_Overflow(N);
2017
2018 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
2019 SDLoc DL(N);
2020 EVT SmallVT = LHS.getValueType();
2021
2022 // To determine if the result overflowed in a larger type, we extend the
2023 // input to the larger type, do the multiply (checking if it overflows),
2024 // then also check the high bits of the result to see if overflow happened
2025 // there.
2026 if (N->getOpcode() == ISD::SMULO) {
2027 LHS = SExtPromotedInteger(LHS);
2028 RHS = SExtPromotedInteger(RHS);
2029 } else {
2030 LHS = ZExtPromotedInteger(LHS);
2031 RHS = ZExtPromotedInteger(RHS);
2032 }
2033 SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1));
2034 SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS);
2035
2036 // Overflow occurred if it occurred in the larger type, or if the high part
2037 // of the result does not zero/sign-extend the low part. Check this second
2038 // possibility first.
2039 SDValue Overflow;
2040 if (N->getOpcode() == ISD::UMULO) {
2041 // Unsigned overflow occurred if the high part is non-zero.
2042 unsigned Shift = SmallVT.getScalarSizeInBits();
2043 SDValue Hi =
2044 DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
2045 DAG.getShiftAmountConstant(Shift, Mul.getValueType(), DL));
2046 Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
2047 DAG.getConstant(0, DL, Hi.getValueType()),
2048 ISD::SETNE);
2049 } else {
2050 // Signed overflow occurred if the high part does not sign extend the low.
2051 SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
2052 Mul, DAG.getValueType(SmallVT));
2053 Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
2054 }
2055
2056 // The only other way for overflow to occur is if the multiplication in the
2057 // larger type itself overflowed.
2058 Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow,
2059 SDValue(Mul.getNode(), 1));
2060
2061 // Use the calculated overflow everywhere.
2062 ReplaceValueWith(SDValue(N, 1), Overflow);
2063 return Mul;
2064}
2065
2066SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
2067 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
2068 N->getValueType(0)));
2069}
2070
2071SDValue DAGTypeLegalizer::PromoteIntRes_VSCALE(SDNode *N) {
2072 EVT VT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2073
2074 const APInt &MulImm = N->getConstantOperandAPInt(0);
2075 return DAG.getVScale(SDLoc(N), VT, MulImm.sext(VT.getSizeInBits()));
2076}
2077
2078SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
2079 SDValue Chain = N->getOperand(0); // Get the chain.
2080 SDValue Ptr = N->getOperand(1); // Get the pointer.
2081 EVT VT = N->getValueType(0);
2082 SDLoc dl(N);
2083
2084 MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
2085 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
2086 // The argument is passed as NumRegs registers of type RegVT.
2087
2088 SmallVector<SDValue, 8> Parts(NumRegs);
2089 for (unsigned i = 0; i < NumRegs; ++i) {
2090 Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
2091 N->getConstantOperandVal(3));
2092 Chain = Parts[i].getValue(1);
2093 }
2094
2095 // Handle endianness of the load.
2096 if (DAG.getDataLayout().isBigEndian())
2097 std::reverse(Parts.begin(), Parts.end());
2098
2099 // Assemble the parts in the promoted type.
2100 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2101 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
2102 for (unsigned i = 1; i < NumRegs; ++i) {
2103 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
2104 // Shift it to the right position and "or" it in.
2105 Part = DAG.getNode(
2106 ISD::SHL, dl, NVT, Part,
2107 DAG.getShiftAmountConstant(i * RegVT.getSizeInBits(), NVT, dl));
2108 Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
2109 }
2110
2111 // Modified the chain result - switch anything that used the old chain to
2112 // use the new one.
2113 ReplaceValueWith(SDValue(N, 1), Chain);
2114
2115 return Res;
2116}
2117
2118//===----------------------------------------------------------------------===//
2119// Integer Operand Promotion
2120//===----------------------------------------------------------------------===//
2121
2122/// PromoteIntegerOperand - This method is called when the specified operand of
2123/// the specified node is found to need promotion. At this point, all of the
2124/// result types of the node are known to be legal, but other operands of the
2125/// node may need promotion or expansion as well as the specified one.
2126bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
2127 LLVM_DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG));
2128 SDValue Res = SDValue();
2129 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) {
2130 LLVM_DEBUG(dbgs() << "Node has been custom lowered, done\n");
2131 return false;
2132 }
2133
2134 switch (N->getOpcode()) {
2135 default:
2136 #ifndef NDEBUG
2137 dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
2138 N->dump(&DAG); dbgs() << "\n";
2139 #endif
2140 report_fatal_error("Do not know how to promote this operator's operand!");
2141
2142 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
2144 Res = PromoteIntOp_ANY_EXTEND_VECTOR_INREG(N);
2145 break;
2146 case ISD::ATOMIC_STORE:
2147 Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
2148 break;
2149 case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break;
2150 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
2151 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
2152 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
2153 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
2154 case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
2155 case ISD::COND_LOOP:
2156 Res = PromoteIntOp_COND_LOOP(N, OpNo);
2157 break;
2158 case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
2159 case ISD::FAKE_USE:
2160 Res = PromoteIntOp_FAKE_USE(N);
2161 break;
2163 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);
2164 break;
2165 case ISD::SPLAT_VECTOR:
2167 Res = PromoteIntOp_ScalarOp(N);
2168 break;
2169 case ISD::VSELECT:
2170 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
2171 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
2172 case ISD::VP_SETCC:
2173 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
2174 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
2175 case ISD::VP_SIGN_EXTEND: Res = PromoteIntOp_VP_SIGN_EXTEND(N); break;
2176 case ISD::VP_SINT_TO_FP:
2177 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
2178 case ISD::STRICT_SINT_TO_FP: Res = PromoteIntOp_STRICT_SINT_TO_FP(N); break;
2179 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
2180 OpNo); break;
2181 case ISD::VP_STORE:
2182 Res = PromoteIntOp_VP_STORE(cast<VPStoreSDNode>(N), OpNo);
2183 break;
2184 case ISD::MSTORE: Res = PromoteIntOp_MSTORE(cast<MaskedStoreSDNode>(N),
2185 OpNo); break;
2186 case ISD::MLOAD: Res = PromoteIntOp_MLOAD(cast<MaskedLoadSDNode>(N),
2187 OpNo); break;
2188 case ISD::MGATHER: Res = PromoteIntOp_MGATHER(cast<MaskedGatherSDNode>(N),
2189 OpNo); break;
2190 case ISD::MSCATTER: Res = PromoteIntOp_MSCATTER(cast<MaskedScatterSDNode>(N),
2191 OpNo); break;
2193 Res = PromoteIntOp_VECTOR_COMPRESS(N, OpNo);
2194 break;
2195 case ISD::VP_TRUNCATE:
2196 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
2197 case ISD::BF16_TO_FP:
2198 case ISD::FP16_TO_FP:
2199 case ISD::VP_UINT_TO_FP:
2200 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
2202 Res = PromoteIntOp_CONVERT_FROM_ARBITRARY_FP(N);
2203 break;
2205 case ISD::STRICT_UINT_TO_FP: Res = PromoteIntOp_STRICT_UINT_TO_FP(N); break;
2206 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
2207 case ISD::VP_ZERO_EXTEND: Res = PromoteIntOp_VP_ZERO_EXTEND(N); break;
2208 case ISD::EXTRACT_SUBVECTOR: Res = PromoteIntOp_EXTRACT_SUBVECTOR(N); break;
2209 case ISD::INSERT_SUBVECTOR: Res = PromoteIntOp_INSERT_SUBVECTOR(N); break;
2210
2211 case ISD::SHL:
2212 case ISD::SRA:
2213 case ISD::SRL:
2214 case ISD::ROTL:
2215 case ISD::ROTR:
2216 case ISD::SSHLSAT:
2217 case ISD::USHLSAT:
2218 Res = PromoteIntOp_Shift(N);
2219 break;
2220
2221 case ISD::SCMP:
2222 case ISD::UCMP: Res = PromoteIntOp_CMP(N); break;
2223
2224 case ISD::FSHL:
2225 case ISD::FSHR: Res = PromoteIntOp_FunnelShift(N); break;
2226
2227 case ISD::FRAMEADDR:
2228 case ISD::RETURNADDR: Res = PromoteIntOp_FRAMERETURNADDR(N); break;
2229
2230 case ISD::SMULFIX:
2231 case ISD::SMULFIXSAT:
2232 case ISD::UMULFIX:
2233 case ISD::UMULFIXSAT:
2234 case ISD::SDIVFIX:
2235 case ISD::SDIVFIXSAT:
2236 case ISD::UDIVFIX:
2237 case ISD::UDIVFIXSAT: Res = PromoteIntOp_FIX(N); break;
2238 case ISD::FPOWI:
2239 case ISD::STRICT_FPOWI:
2240 case ISD::FLDEXP:
2241 case ISD::STRICT_FLDEXP: Res = PromoteIntOp_ExpOp(N); break;
2242 case ISD::VECREDUCE_ADD:
2243 case ISD::VECREDUCE_MUL:
2244 case ISD::VECREDUCE_AND:
2245 case ISD::VECREDUCE_OR:
2246 case ISD::VECREDUCE_XOR:
2250 case ISD::VECREDUCE_UMIN: Res = PromoteIntOp_VECREDUCE(N); break;
2251 case ISD::VP_REDUCE_ADD:
2252 case ISD::VP_REDUCE_MUL:
2253 case ISD::VP_REDUCE_AND:
2254 case ISD::VP_REDUCE_OR:
2255 case ISD::VP_REDUCE_XOR:
2256 case ISD::VP_REDUCE_SMAX:
2257 case ISD::VP_REDUCE_SMIN:
2258 case ISD::VP_REDUCE_UMAX:
2259 case ISD::VP_REDUCE_UMIN:
2260 Res = PromoteIntOp_VP_REDUCE(N, OpNo);
2261 break;
2262
2263 case ISD::SET_ROUNDING: Res = PromoteIntOp_SET_ROUNDING(N); break;
2264 case ISD::STACKMAP:
2265 Res = PromoteIntOp_STACKMAP(N, OpNo);
2266 break;
2267 case ISD::PATCHPOINT:
2268 Res = PromoteIntOp_PATCHPOINT(N, OpNo);
2269 break;
2271 Res = PromoteIntOp_WRITE_REGISTER(N, OpNo);
2272 break;
2273 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
2274 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
2275 Res = PromoteIntOp_VP_STRIDED(N, OpNo);
2276 break;
2277 case ISD::EXPERIMENTAL_VP_SPLICE:
2278 Res = PromoteIntOp_VP_SPLICE(N, OpNo);
2279 break;
2281 Res = PromoteIntOp_VECTOR_HISTOGRAM(N, OpNo);
2282 break;
2284 case ISD::CTTZ_ELTS:
2286 Res = PromoteIntOp_UnaryBooleanVectorOp(N, OpNo);
2287 break;
2289 Res = PromoteIntOp_GET_ACTIVE_LANE_MASK(N);
2290 break;
2291 case ISD::MASKED_UDIV:
2292 case ISD::MASKED_SDIV:
2293 case ISD::MASKED_UREM:
2294 case ISD::MASKED_SREM:
2295 Res = PromoteIntOp_MaskedBinOp(N, OpNo);
2296 break;
2300 Res = PromoteIntOp_PARTIAL_REDUCE_MLA(N);
2301 break;
2304 Res = PromoteIntOp_LOOP_DEPENDENCE_MASK(N);
2305 break;
2306 }
2307
2308 // If the result is null, the sub-method took care of registering results etc.
2309 if (!Res.getNode()) return false;
2310
2311 // If the result is N, the sub-method updated N in place. Tell the legalizer
2312 // core about this.
2313 if (Res.getNode() == N)
2314 return true;
2315
2316 const bool IsStrictFp = N->isStrictFPOpcode();
2317 assert(Res.getValueType() == N->getValueType(0) &&
2318 N->getNumValues() == (IsStrictFp ? 2 : 1) &&
2319 "Invalid operand expansion");
2320 LLVM_DEBUG(dbgs() << "Replacing: "; N->dump(&DAG); dbgs() << " with: ";
2321 Res.dump());
2322
2323 ReplaceValueWith(SDValue(N, 0), Res);
2324 if (IsStrictFp)
2325 ReplaceValueWith(SDValue(N, 1), SDValue(Res.getNode(), 1));
2326
2327 return false;
2328}
2329
2330// These operands can be either sign extended or zero extended as long as we
2331// treat them the same. If an extension is free, choose that. Otherwise, follow
2332// target preference.
2333void DAGTypeLegalizer::SExtOrZExtPromotedOperands(SDValue &LHS, SDValue &RHS) {
2334 SDValue OpL = GetPromotedInteger(LHS);
2335 SDValue OpR = GetPromotedInteger(RHS);
2336
2337 if (TLI.isSExtCheaperThanZExt(LHS.getValueType(), OpL.getValueType())) {
2338 // The target would prefer to promote the comparison operand with sign
2339 // extension. Honor that unless the promoted values are already zero
2340 // extended.
2341 unsigned OpLEffectiveBits =
2342 DAG.computeKnownBits(OpL).countMaxActiveBits();
2343 unsigned OpREffectiveBits =
2344 DAG.computeKnownBits(OpR).countMaxActiveBits();
2345 if (OpLEffectiveBits <= LHS.getScalarValueSizeInBits() &&
2346 OpREffectiveBits <= RHS.getScalarValueSizeInBits()) {
2347 LHS = OpL;
2348 RHS = OpR;
2349 return;
2350 }
2351
2352 // The promoted values aren't zero extended, use a sext_inreg.
2353 LHS = SExtPromotedInteger(LHS);
2354 RHS = SExtPromotedInteger(RHS);
2355 return;
2356 }
2357
2358 // Prefer to promote the comparison operand with zero extension.
2359
2360 // If the width of OpL/OpR excluding the duplicated sign bits is no greater
2361 // than the width of LHS/RHS, we can avoid inserting a zext_inreg operation
2362 // that we might not be able to remove.
2363 unsigned OpLEffectiveBits = DAG.ComputeMaxSignificantBits(OpL);
2364 unsigned OpREffectiveBits = DAG.ComputeMaxSignificantBits(OpR);
2365 if (OpLEffectiveBits <= LHS.getScalarValueSizeInBits() &&
2366 OpREffectiveBits <= RHS.getScalarValueSizeInBits()) {
2367 LHS = OpL;
2368 RHS = OpR;
2369 return;
2370 }
2371
2372 // Otherwise, use zext_inreg.
2373 LHS = ZExtPromotedInteger(LHS);
2374 RHS = ZExtPromotedInteger(RHS);
2375}
2376
2377/// PromoteSetCCOperands - Promote the operands of a comparison. This code is
2378/// shared among BR_CC, SELECT_CC, and SETCC handlers.
2379void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &LHS, SDValue &RHS,
2380 ISD::CondCode CCCode) {
2381 // We have to insert explicit sign or zero extends. Note that we could
2382 // insert sign extends for ALL conditions. For those operations where either
2383 // zero or sign extension would be valid, we ask the target which extension
2384 // it would prefer.
2385
2386 // Signed comparisons always require sign extension.
2387 if (ISD::isSignedIntSetCC(CCCode)) {
2388 LHS = SExtPromotedInteger(LHS);
2389 RHS = SExtPromotedInteger(RHS);
2390 return;
2391 }
2392
2394 "Unknown integer comparison!");
2395
2396 SExtOrZExtPromotedOperands(LHS, RHS);
2397}
2398
2399SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
2400 SDValue Op = GetPromotedInteger(N->getOperand(0));
2401 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op);
2402}
2403
2404SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND_VECTOR_INREG(SDNode *N) {
2405 SDValue Op = GetPromotedInteger(N->getOperand(0));
2406 EVT ResVT = N->getValueType(0);
2407 EVT OpVT = Op.getValueType();
2408 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), OpVT.getScalarType(),
2409 ResVT.getVectorNumElements());
2410 Op = DAG.getExtractSubvector(SDLoc(Op), NewVT, Op, 0);
2411 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), ResVT, Op);
2412}
2413
2414SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
2415 SDValue Op1 = GetPromotedInteger(N->getOperand(1));
2416 return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(),
2417 N->getChain(), Op1, N->getBasePtr(), N->getMemOperand());
2418}
2419
2420SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
2421 EVT OutVT = N->getValueType(0);
2422 SDValue InOp = N->getOperand(0);
2423 EVT InVT = InOp.getValueType();
2424 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
2425 SDLoc dl(N);
2426
2427 switch (getTypeAction(InVT)) {
2429 // TODO: Handle big endian & vector input type.
2430 if (OutVT.isVector() && !InVT.isVector() &&
2431 DAG.getDataLayout().isLittleEndian()) {
2432 EVT EltVT = OutVT.getVectorElementType();
2433 TypeSize EltSize = EltVT.getSizeInBits();
2434 TypeSize NInSize = NInVT.getSizeInBits();
2435
2436 if (NInSize.hasKnownScalarFactor(EltSize)) {
2437 unsigned NumEltsWithPadding = NInSize.getKnownScalarFactor(EltSize);
2438 EVT WideVecVT =
2439 EVT::getVectorVT(*DAG.getContext(), EltVT, NumEltsWithPadding);
2440
2441 if (isTypeLegal(WideVecVT)) {
2442 SDValue Promoted = GetPromotedInteger(InOp);
2443 SDValue Cast = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Promoted);
2444 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, Cast,
2445 DAG.getVectorIdxConstant(0, dl));
2446 }
2447 }
2448 }
2449
2450 break;
2451 }
2452 default:
2453 break;
2454 }
2455
2456 // This should only occur in unusual situations like bitcasting to an
2457 // x86_fp80, so just turn it into a store+load
2458 return CreateStackStoreLoad(InOp, OutVT);
2459}
2460
2461SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
2462 assert(OpNo == 2 && "Don't know how to promote this operand!");
2463
2464 SDValue LHS = N->getOperand(2);
2465 SDValue RHS = N->getOperand(3);
2466 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
2467
2468 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
2469 // legal types.
2470 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2471 N->getOperand(1), LHS, RHS, N->getOperand(4)),
2472 0);
2473}
2474
2475SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
2476 assert(OpNo == 1 && "only know how to promote condition");
2477
2478 // Promote all the way up to the canonical SetCC type.
2479 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
2480
2481 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
2482 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
2483 N->getOperand(2)), 0);
2484}
2485
2486SDValue DAGTypeLegalizer::PromoteIntOp_COND_LOOP(SDNode *N, unsigned OpNo) {
2487 assert(OpNo == 1 && "only know how to promote condition");
2488
2489 // Promote all the way up to the canonical SetCC type.
2490 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
2491
2492 // The chain (Op#0) is always a legal type.
2493 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond), 0);
2494}
2495
2496SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
2497 // Since the result type is legal, the operands must promote to it.
2498 EVT OVT = N->getOperand(0).getValueType();
2499 SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
2500 SDValue Hi = GetPromotedInteger(N->getOperand(1));
2501 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
2502 SDLoc dl(N);
2503
2504 Hi = DAG.getNode(
2505 ISD::SHL, dl, N->getValueType(0), Hi,
2506 DAG.getShiftAmountConstant(OVT.getSizeInBits(), N->getValueType(0), dl));
2507 return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
2508}
2509
2510SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
2511 // The vector type is legal but the element type is not. This implies
2512 // that the vector is a power-of-two in length and that the element
2513 // type does not have a strange size (eg: it is not i1).
2514 EVT VecVT = N->getValueType(0);
2515 unsigned NumElts = VecVT.getVectorNumElements();
2516 assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) &&
2517 "Legal vector of one illegal element?");
2518
2519 // Promote the inserted value. The type does not need to match the
2520 // vector element type. Check that any extra bits introduced will be
2521 // truncated away.
2522 assert(N->getOperand(0).getValueSizeInBits() >=
2523 N->getValueType(0).getScalarSizeInBits() &&
2524 "Type of inserted value narrower than vector element type!");
2525
2527 for (unsigned i = 0; i < NumElts; ++i)
2528 NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
2529
2530 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2531}
2532
2533SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
2534 unsigned OpNo) {
2535 if (OpNo == 1) {
2536 // Promote the inserted value. This is valid because the type does not
2537 // have to match the vector element type.
2538
2539 // Check that any extra bits introduced will be truncated away.
2540 assert(N->getOperand(1).getValueSizeInBits() >=
2541 N->getValueType(0).getScalarSizeInBits() &&
2542 "Type of inserted value narrower than vector element type!");
2543 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2544 GetPromotedInteger(N->getOperand(1)),
2545 N->getOperand(2)),
2546 0);
2547 }
2548
2549 assert(OpNo == 2 && "Different operand and result vector types?");
2550
2551 // Promote the index.
2552 SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N),
2553 TLI.getVectorIdxTy(DAG.getDataLayout()));
2554 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2555 N->getOperand(1), Idx), 0);
2556}
2557
2558SDValue DAGTypeLegalizer::PromoteIntOp_ScalarOp(SDNode *N) {
2559 SDValue Op = GetPromotedInteger(N->getOperand(0));
2560
2561 // Integer SPLAT_VECTOR/SCALAR_TO_VECTOR operands are implicitly truncated,
2562 // so just promote the operand in place.
2563 return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
2564}
2565
2566SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
2567 assert(OpNo == 0 && "Only know how to promote the condition!");
2568 SDValue Cond = N->getOperand(0);
2569 EVT OpTy = N->getOperand(1).getValueType();
2570
2571 if (N->getOpcode() == ISD::VSELECT)
2572 if (SDValue Res = WidenVSELECTMask(N))
2573 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
2574 Res, N->getOperand(1), N->getOperand(2));
2575
2576 // Promote all the way up to the canonical SetCC type.
2577 EVT OpVT = N->getOpcode() == ISD::SELECT ? OpTy.getScalarType() : OpTy;
2578 Cond = PromoteTargetBoolean(Cond, OpVT);
2579
2580 return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1),
2581 N->getOperand(2)), 0);
2582}
2583
2584SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
2585 assert(OpNo == 0 && "Don't know how to promote this operand!");
2586
2587 SDValue LHS = N->getOperand(0);
2588 SDValue RHS = N->getOperand(1);
2589 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
2590
2591 // The CC (#4) and the possible return values (#2 and #3) have legal types.
2592 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
2593 N->getOperand(3), N->getOperand(4)), 0);
2594}
2595
2596SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
2597 assert(OpNo == 0 && "Don't know how to promote this operand!");
2598
2599 SDValue LHS = N->getOperand(0);
2600 SDValue RHS = N->getOperand(1);
2601 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
2602
2603 // The CC (#2) is always legal.
2604 if (N->getOpcode() == ISD::SETCC)
2605 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
2606
2607 assert(N->getOpcode() == ISD::VP_SETCC && "Expected VP_SETCC opcode");
2608
2609 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
2610 N->getOperand(3), N->getOperand(4)),
2611 0);
2612}
2613
2614SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
2615 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2616 ZExtPromotedInteger(N->getOperand(1))), 0);
2617}
2618
2619SDValue DAGTypeLegalizer::PromoteIntOp_CMP(SDNode *N) {
2620 SDValue LHS = N->getOperand(0);
2621 SDValue RHS = N->getOperand(1);
2622
2623 if (N->getOpcode() == ISD::SCMP) {
2624 LHS = SExtPromotedInteger(LHS);
2625 RHS = SExtPromotedInteger(RHS);
2626 } else {
2627 SExtOrZExtPromotedOperands(LHS, RHS);
2628 }
2629
2630 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS), 0);
2631}
2632
2633SDValue DAGTypeLegalizer::PromoteIntOp_FunnelShift(SDNode *N) {
2634 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1),
2635 ZExtPromotedInteger(N->getOperand(2))), 0);
2636}
2637
2638SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
2639 SDValue Op = GetPromotedInteger(N->getOperand(0));
2640 SDLoc dl(N);
2641 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
2642 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
2643 Op, DAG.getValueType(N->getOperand(0).getValueType()));
2644}
2645
2646SDValue DAGTypeLegalizer::PromoteIntOp_VP_SIGN_EXTEND(SDNode *N) {
2647 SDLoc dl(N);
2648 EVT VT = N->getValueType(0);
2649 SDValue Op = GetPromotedInteger(N->getOperand(0));
2650 // FIXME: There is no VP_ANY_EXTEND yet.
2651 Op = DAG.getNode(ISD::VP_ZERO_EXTEND, dl, VT, Op, N->getOperand(1),
2652 N->getOperand(2));
2653 unsigned Diff =
2654 VT.getScalarSizeInBits() - N->getOperand(0).getScalarValueSizeInBits();
2655 SDValue ShAmt = DAG.getShiftAmountConstant(Diff, VT, dl);
2656 // FIXME: There is no VP_SIGN_EXTEND_INREG so use a pair of shifts.
2657 SDValue Shl = DAG.getNode(ISD::VP_SHL, dl, VT, Op, ShAmt, N->getOperand(1),
2658 N->getOperand(2));
2659 return DAG.getNode(ISD::VP_SRA, dl, VT, Shl, ShAmt, N->getOperand(1),
2660 N->getOperand(2));
2661}
2662
2663SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
2664 if (N->getOpcode() == ISD::VP_SINT_TO_FP)
2665 return SDValue(DAG.UpdateNodeOperands(N,
2666 SExtPromotedInteger(N->getOperand(0)),
2667 N->getOperand(1), N->getOperand(2)),
2668 0);
2669 return SDValue(DAG.UpdateNodeOperands(N,
2670 SExtPromotedInteger(N->getOperand(0))), 0);
2671}
2672
2673SDValue DAGTypeLegalizer::PromoteIntOp_STRICT_SINT_TO_FP(SDNode *N) {
2674 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2675 SExtPromotedInteger(N->getOperand(1))), 0);
2676}
2677
2678SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
2679 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2680 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
2681 SDLoc dl(N);
2682
2683 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
2684
2685 // Truncate the value and store the result.
2686 return DAG.getTruncStore(Ch, dl, Val, Ptr,
2687 N->getMemoryVT(), N->getMemOperand());
2688}
2689
2690SDValue DAGTypeLegalizer::PromoteIntOp_VP_STORE(VPStoreSDNode *N,
2691 unsigned OpNo) {
2692
2693 assert(OpNo == 1 && "Unexpected operand for promotion");
2694 assert(!N->isIndexed() && "expecting unindexed vp_store!");
2695
2696 SDValue DataOp = GetPromotedInteger(N->getValue());
2697 return DAG.getTruncStoreVP(N->getChain(), SDLoc(N), DataOp, N->getBasePtr(),
2698 N->getMask(), N->getVectorLength(),
2699 N->getMemoryVT(), N->getMemOperand(),
2700 N->isCompressingStore());
2701}
2702
2703SDValue DAGTypeLegalizer::PromoteIntOp_MSTORE(MaskedStoreSDNode *N,
2704 unsigned OpNo) {
2705 SDValue DataOp = N->getValue();
2706 SDValue Mask = N->getMask();
2707
2708 if (OpNo == 4) {
2709 // The Mask. Update in place.
2710 EVT DataVT = DataOp.getValueType();
2711 Mask = PromoteTargetBoolean(Mask, DataVT);
2712 SmallVector<SDValue, 4> NewOps(N->ops());
2713 NewOps[4] = Mask;
2714 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2715 }
2716
2717 assert(OpNo == 1 && "Unexpected operand for promotion");
2718 DataOp = GetPromotedInteger(DataOp);
2719
2720 return DAG.getMaskedStore(N->getChain(), SDLoc(N), DataOp, N->getBasePtr(),
2721 N->getOffset(), Mask, N->getMemoryVT(),
2722 N->getMemOperand(), N->getAddressingMode(),
2723 /*IsTruncating*/ true, N->isCompressingStore());
2724}
2725
2726SDValue DAGTypeLegalizer::PromoteIntOp_MLOAD(MaskedLoadSDNode *N,
2727 unsigned OpNo) {
2728 assert(OpNo == 3 && "Only know how to promote the mask!");
2729 EVT DataVT = N->getValueType(0);
2730 SDValue Mask = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
2731 SmallVector<SDValue, 4> NewOps(N->ops());
2732 NewOps[OpNo] = Mask;
2733 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
2734 if (Res == N)
2735 return SDValue(Res, 0);
2736
2737 // Update triggered CSE, do our own replacement since caller can't.
2738 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
2739 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
2740 return SDValue();
2741}
2742
2743SDValue DAGTypeLegalizer::PromoteIntOp_MGATHER(MaskedGatherSDNode *N,
2744 unsigned OpNo) {
2745 SmallVector<SDValue, 5> NewOps(N->ops());
2746
2747 if (OpNo == 2) {
2748 // The Mask
2749 EVT DataVT = N->getValueType(0);
2750 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
2751 } else if (OpNo == 4) {
2752 // The Index
2753 if (N->isIndexSigned())
2754 // Need to sign extend the index since the bits will likely be used.
2755 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
2756 else
2757 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
2758 } else
2759 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
2760
2761 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
2762 if (Res == N)
2763 return SDValue(Res, 0);
2764
2765 // Update triggered CSE, do our own replacement since caller can't.
2766 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
2767 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
2768 return SDValue();
2769}
2770
2771SDValue DAGTypeLegalizer::PromoteIntOp_MSCATTER(MaskedScatterSDNode *N,
2772 unsigned OpNo) {
2773 bool TruncateStore = N->isTruncatingStore();
2774 SmallVector<SDValue, 5> NewOps(N->ops());
2775
2776 if (OpNo == 2) {
2777 // The Mask
2778 EVT DataVT = N->getValue().getValueType();
2779 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
2780 } else if (OpNo == 4) {
2781 // The Index
2782 if (N->isIndexSigned())
2783 // Need to sign extend the index since the bits will likely be used.
2784 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
2785 else
2786 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
2787 } else {
2788 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
2789 TruncateStore = true;
2790 }
2791
2792 return DAG.getMaskedScatter(DAG.getVTList(MVT::Other), N->getMemoryVT(),
2793 SDLoc(N), NewOps, N->getMemOperand(),
2794 N->getIndexType(), TruncateStore);
2795}
2796
2797SDValue DAGTypeLegalizer::PromoteIntOp_VECTOR_COMPRESS(SDNode *N,
2798 unsigned OpNo) {
2799 assert(OpNo == 1 && "Can only promote VECTOR_COMPRESS mask.");
2800 SDValue Vec = N->getOperand(0);
2801 EVT VT = Vec.getValueType();
2802 SDValue Passthru = N->getOperand(2);
2803 SDValue Mask = PromoteTargetBoolean(N->getOperand(1), VT);
2804 return DAG.getNode(ISD::VECTOR_COMPRESS, SDLoc(N), VT, Vec, Mask, Passthru);
2805}
2806
2807SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
2808 SDValue Op = GetPromotedInteger(N->getOperand(0));
2809 if (N->getOpcode() == ISD::VP_TRUNCATE)
2810 return DAG.getNode(ISD::VP_TRUNCATE, SDLoc(N), N->getValueType(0), Op,
2811 N->getOperand(1), N->getOperand(2));
2812 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op);
2813}
2814
2815SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
2816 if (N->getOpcode() == ISD::VP_UINT_TO_FP)
2817 return SDValue(DAG.UpdateNodeOperands(N,
2818 ZExtPromotedInteger(N->getOperand(0)),
2819 N->getOperand(1), N->getOperand(2)),
2820 0);
2821 return SDValue(DAG.UpdateNodeOperands(N,
2822 ZExtPromotedInteger(N->getOperand(0))), 0);
2823}
2824
2825SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_FROM_ARBITRARY_FP(SDNode *N) {
2826 return SDValue(DAG.UpdateNodeOperands(N, GetPromotedInteger(N->getOperand(0)),
2827 N->getOperand(1)),
2828 0);
2829}
2830
2831SDValue DAGTypeLegalizer::PromoteIntOp_STRICT_UINT_TO_FP(SDNode *N) {
2832 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2833 ZExtPromotedInteger(N->getOperand(1))), 0);
2834}
2835
2836SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
2837 SDLoc dl(N);
2838 SDValue Src = N->getOperand(0);
2839 SDValue Op = GetPromotedInteger(Src);
2840 EVT VT = N->getValueType(0);
2841
2842 // If this zext has the nneg flag and the target prefers sext, see if the
2843 // promoted input is already sign extended.
2844 // TODO: Should we have some way to set nneg on ISD::AND instead?
2845 if (N->getFlags().hasNonNeg() && Op.getValueType() == VT &&
2846 TLI.isSExtCheaperThanZExt(Src.getValueType(), VT)) {
2847 unsigned OpEffectiveBits = DAG.ComputeMaxSignificantBits(Op);
2848 if (OpEffectiveBits <= Src.getScalarValueSizeInBits())
2849 return Op;
2850 }
2851
2852 Op = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op);
2853 return DAG.getZeroExtendInReg(Op, dl, Src.getValueType());
2854}
2855
2856SDValue DAGTypeLegalizer::PromoteIntOp_VP_ZERO_EXTEND(SDNode *N) {
2857 SDLoc dl(N);
2858 EVT VT = N->getValueType(0);
2859 SDValue Op = GetPromotedInteger(N->getOperand(0));
2860 // FIXME: There is no VP_ANY_EXTEND yet.
2861 Op = DAG.getNode(ISD::VP_ZERO_EXTEND, dl, VT, Op, N->getOperand(1),
2862 N->getOperand(2));
2863 return DAG.getVPZeroExtendInReg(Op, N->getOperand(1), N->getOperand(2), dl,
2864 N->getOperand(0).getValueType());
2865}
2866
2867SDValue DAGTypeLegalizer::PromoteIntOp_FIX(SDNode *N) {
2868 SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
2869 return SDValue(
2870 DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1), Op2), 0);
2871}
2872
2873SDValue DAGTypeLegalizer::PromoteIntOp_FRAMERETURNADDR(SDNode *N) {
2874 // Promote the RETURNADDR/FRAMEADDR argument to a supported integer width.
2875 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
2876 return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
2877}
2878
2879SDValue DAGTypeLegalizer::PromoteIntOp_ExpOp(SDNode *N) {
2880 bool IsStrict = N->isStrictFPOpcode();
2881 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
2882
2883 bool IsPowI =
2884 N->getOpcode() == ISD::FPOWI || N->getOpcode() == ISD::STRICT_FPOWI;
2885 unsigned OpOffset = IsStrict ? 1 : 0;
2886
2887 // The integer operand is the last operand in FPOWI (or FLDEXP) (so the result
2888 // and floating point operand is already type legalized).
2889 RTLIB::Libcall LC = IsPowI ? RTLIB::getPOWI(N->getValueType(0))
2890 : RTLIB::getLDEXP(N->getValueType(0));
2891
2892 RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(LC);
2893 if (LCImpl == RTLIB::Unsupported) {
2894 // Scalarize vector FPOWI instead of promoting the type. This allows the
2895 // scalar FPOWIs to be visited and converted to libcalls before promoting
2896 // the type.
2897 // FIXME: This should be done in LegalizeVectorOps/LegalizeDAG, but call
2898 // lowering needs the unpromoted EVT.
2899 if (IsPowI && N->getValueType(0).isVector())
2900 return DAG.UnrollVectorOp(N);
2901 SmallVector<SDValue, 3> NewOps(N->ops());
2902 NewOps[1 + OpOffset] = SExtPromotedInteger(N->getOperand(1 + OpOffset));
2903 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2904 }
2905
2906 // We can't just promote the exponent type in FPOWI, since we want to lower
2907 // the node to a libcall and we if we promote to a type larger than
2908 // sizeof(int) the libcall might not be according to the targets ABI. Instead
2909 // we rewrite to a libcall here directly, letting makeLibCall handle promotion
2910 // if the target accepts it according to shouldSignExtendTypeInLibCall.
2911
2912 // A wider-than-int exponent can't be passed in an int (there's no wider
2913 // libcall), so bail like the soften/expand paths. A narrower one is
2914 // sign-extended to int by the makeLibCall below.
2915 if (N->getOperand(1 + OpOffset).getScalarValueSizeInBits() >
2916 DAG.getLibInfo().getIntSize()) {
2917 const Function &Fn = DAG.getMachineFunction().getFunction();
2918 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
2919 Twine(IsPowI ? "powi" : "ldexp") +
2920 " exponent does not match sizeof(int)",
2921 Fn, N->getDebugLoc()));
2922 if (IsStrict)
2923 ReplaceValueWith(SDValue(N, 1), Chain);
2924 ReplaceValueWith(SDValue(N, 0), DAG.getPOISON(N->getValueType(0)));
2925 return SDValue();
2926 }
2927
2928 TargetLowering::MakeLibCallOptions CallOptions;
2929 CallOptions.setIsSigned(true);
2930 SDValue Ops[2] = {N->getOperand(0 + OpOffset), N->getOperand(1 + OpOffset)};
2931 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
2932 DAG, LCImpl, N->getValueType(0), Ops, CallOptions, SDLoc(N), Chain);
2933 ReplaceValueWith(SDValue(N, 0), Tmp.first);
2934 if (IsStrict)
2935 ReplaceValueWith(SDValue(N, 1), Tmp.second);
2936 return SDValue();
2937}
2938
2940 switch (N->getOpcode()) {
2941 default:
2942 llvm_unreachable("Expected integer vector reduction");
2943 case ISD::VECREDUCE_ADD:
2944 case ISD::VECREDUCE_MUL:
2945 case ISD::VECREDUCE_AND:
2946 case ISD::VECREDUCE_OR:
2947 case ISD::VECREDUCE_XOR:
2948 case ISD::VP_REDUCE_ADD:
2949 case ISD::VP_REDUCE_MUL:
2950 case ISD::VP_REDUCE_AND:
2951 case ISD::VP_REDUCE_OR:
2952 case ISD::VP_REDUCE_XOR:
2953 return ISD::ANY_EXTEND;
2956 case ISD::VP_REDUCE_SMAX:
2957 case ISD::VP_REDUCE_SMIN:
2958 return ISD::SIGN_EXTEND;
2961 case ISD::VP_REDUCE_UMAX:
2962 case ISD::VP_REDUCE_UMIN:
2963 return ISD::ZERO_EXTEND;
2964 }
2965}
2966
2967SDValue DAGTypeLegalizer::PromoteIntOpVectorReduction(SDNode *N, SDValue V) {
2968 switch (getExtendForIntVecReduction(N)) {
2969 default:
2970 llvm_unreachable("Impossible extension kind for integer reduction");
2971 case ISD::ANY_EXTEND:
2972 return GetPromotedInteger(V);
2973 case ISD::SIGN_EXTEND:
2974 return SExtPromotedInteger(V);
2975 case ISD::ZERO_EXTEND:
2976 return ZExtPromotedInteger(V);
2977 }
2978}
2979
2980SDValue DAGTypeLegalizer::PromoteIntOp_VECREDUCE(SDNode *N) {
2981 SDLoc dl(N);
2982 SDValue Op = PromoteIntOpVectorReduction(N, N->getOperand(0));
2983
2984 EVT OrigEltVT = N->getOperand(0).getValueType().getVectorElementType();
2985 EVT InVT = Op.getValueType();
2986 EVT EltVT = InVT.getVectorElementType();
2987 EVT ResVT = N->getValueType(0);
2988 unsigned Opcode = N->getOpcode();
2989
2990 // An i1 vecreduce_xor is equivalent to vecreduce_add, use that instead if
2991 // vecreduce_xor is not legal
2992 if (Opcode == ISD::VECREDUCE_XOR && OrigEltVT == MVT::i1 &&
2993 !TLI.isOperationLegalOrCustom(ISD::VECREDUCE_XOR, InVT) &&
2994 TLI.isOperationLegalOrCustom(ISD::VECREDUCE_ADD, InVT))
2995 Opcode = ISD::VECREDUCE_ADD;
2996
2997 // An i1 vecreduce_or is equivalent to vecreduce_umax, use that instead if
2998 // vecreduce_or is not legal
2999 else if (Opcode == ISD::VECREDUCE_OR && OrigEltVT == MVT::i1 &&
3000 !TLI.isOperationLegalOrCustom(ISD::VECREDUCE_OR, InVT) &&
3001 TLI.isOperationLegalOrCustom(ISD::VECREDUCE_UMAX, InVT)) {
3002 Opcode = ISD::VECREDUCE_UMAX;
3003 // Can't use promoteTargetBoolean here because we still need
3004 // to either sign_ext or zero_ext in the undefined case.
3005 switch (TLI.getBooleanContents(InVT)) {
3008 Op = ZExtPromotedInteger(N->getOperand(0));
3009 break;
3011 Op = SExtPromotedInteger(N->getOperand(0));
3012 break;
3013 }
3014 }
3015
3016 // An i1 vecreduce_and is equivalent to vecreduce_umin, use that instead if
3017 // vecreduce_and is not legal
3018 else if (Opcode == ISD::VECREDUCE_AND && OrigEltVT == MVT::i1 &&
3019 !TLI.isOperationLegalOrCustom(ISD::VECREDUCE_AND, InVT) &&
3020 TLI.isOperationLegalOrCustom(ISD::VECREDUCE_UMIN, InVT)) {
3021 Opcode = ISD::VECREDUCE_UMIN;
3022 // Can't use promoteTargetBoolean here because we still need
3023 // to either sign_ext or zero_ext in the undefined case.
3024 switch (TLI.getBooleanContents(InVT)) {
3027 Op = ZExtPromotedInteger(N->getOperand(0));
3028 break;
3030 Op = SExtPromotedInteger(N->getOperand(0));
3031 break;
3032 }
3033 }
3034
3035 if (ResVT.bitsGE(EltVT))
3036 return DAG.getNode(Opcode, SDLoc(N), ResVT, Op);
3037
3038 // Result size must be >= element size. If this is not the case after
3039 // promotion, also promote the result type and then truncate.
3040 SDValue Reduce = DAG.getNode(Opcode, dl, EltVT, Op);
3041 return DAG.getNode(ISD::TRUNCATE, dl, ResVT, Reduce);
3042}
3043
3044SDValue DAGTypeLegalizer::PromoteIntOp_VP_REDUCE(SDNode *N, unsigned OpNo) {
3045 SDLoc DL(N);
3046 SDValue Op = N->getOperand(OpNo);
3047 SmallVector<SDValue, 4> NewOps(N->ops());
3048
3049 if (OpNo == 2) { // Mask
3050 // Update in place.
3051 NewOps[2] = PromoteTargetBoolean(Op, N->getOperand(1).getValueType());
3052 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3053 }
3054
3055 assert(OpNo == 1 && "Unexpected operand for promotion");
3056
3057 Op = PromoteIntOpVectorReduction(N, Op);
3058
3059 NewOps[OpNo] = Op;
3060
3061 EVT VT = N->getValueType(0);
3062 EVT EltVT = Op.getValueType().getScalarType();
3063
3064 if (VT.bitsGE(EltVT))
3065 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, NewOps);
3066
3067 // Result size must be >= element/start-value size. If this is not the case
3068 // after promotion, also promote both the start value and result type and
3069 // then truncate.
3070 NewOps[0] =
3071 DAG.getNode(getExtendForIntVecReduction(N), DL, EltVT, N->getOperand(0));
3072 SDValue Reduce = DAG.getNode(N->getOpcode(), DL, EltVT, NewOps);
3073 return DAG.getNode(ISD::TRUNCATE, DL, VT, Reduce);
3074}
3075
3076SDValue DAGTypeLegalizer::PromoteIntOp_SET_ROUNDING(SDNode *N) {
3077 SDValue Op = ZExtPromotedInteger(N->getOperand(1));
3078 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op), 0);
3079}
3080
3081SDValue DAGTypeLegalizer::PromoteIntOp_STACKMAP(SDNode *N, unsigned OpNo) {
3082 assert(OpNo > 1); // Because the first two arguments are guaranteed legal.
3083 SmallVector<SDValue> NewOps(N->ops());
3084 NewOps[OpNo] = GetPromotedInteger(NewOps[OpNo]);
3085 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3086}
3087
3088SDValue DAGTypeLegalizer::PromoteIntOp_PATCHPOINT(SDNode *N, unsigned OpNo) {
3089 assert(OpNo >= 7);
3090 SmallVector<SDValue> NewOps(N->ops());
3091 NewOps[OpNo] = GetPromotedInteger(NewOps[OpNo]);
3092 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3093}
3094
3095SDValue DAGTypeLegalizer::PromoteIntOp_WRITE_REGISTER(SDNode *N,
3096 unsigned OpNo) {
3097 const Function &Fn = DAG.getMachineFunction().getFunction();
3098 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
3099 "cannot use llvm.write_register with illegal type", Fn,
3100 N->getDebugLoc()));
3101 return N->getOperand(0);
3102}
3103
3104SDValue DAGTypeLegalizer::PromoteIntOp_VP_STRIDED(SDNode *N, unsigned OpNo) {
3105 assert((N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD && OpNo == 3) ||
3106 (N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE && OpNo == 4));
3107
3108 SmallVector<SDValue, 8> NewOps(N->ops());
3109 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
3110 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
3111 if (Res == N)
3112 return SDValue(Res, 0);
3113
3114 // Update triggered CSE, do our own replacement since caller can't.
3115 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
3116 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
3117 return SDValue();
3118}
3119
3120SDValue DAGTypeLegalizer::PromoteIntOp_VP_SPLICE(SDNode *N, unsigned OpNo) {
3121 SmallVector<SDValue, 6> NewOps(N->ops());
3122
3123 if (OpNo == 2) { // Offset operand
3124 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
3125 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3126 }
3127
3128 assert((OpNo == 4 || OpNo == 5) && "Unexpected operand for promotion");
3129
3130 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
3131 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3132}
3133
3134SDValue DAGTypeLegalizer::PromoteIntOp_VECTOR_HISTOGRAM(SDNode *N,
3135 unsigned OpNo) {
3136 assert(OpNo == 1 && "Unexpected operand for promotion");
3137 SmallVector<SDValue, 7> NewOps(N->ops());
3138 NewOps[1] = GetPromotedInteger(N->getOperand(1));
3139 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3140}
3141
3142SDValue DAGTypeLegalizer::PromoteIntOp_UnaryBooleanVectorOp(SDNode *N,
3143 unsigned OpNo) {
3144 assert(OpNo == 0 && "Unexpected operand for promotion");
3145 SDValue Op = N->getOperand(0);
3146
3147 SDValue NewOp;
3148 if (TLI.getBooleanContents(Op.getValueType()) ==
3150 NewOp = SExtPromotedInteger(Op);
3151 else
3152 NewOp = ZExtPromotedInteger(Op);
3153
3154 return SDValue(DAG.UpdateNodeOperands(N, NewOp), 0);
3155}
3156
3157SDValue DAGTypeLegalizer::PromoteIntOp_GET_ACTIVE_LANE_MASK(SDNode *N) {
3158 SmallVector<SDValue, 1> NewOps(N->ops());
3159 NewOps[0] = ZExtPromotedInteger(N->getOperand(0));
3160 NewOps[1] = ZExtPromotedInteger(N->getOperand(1));
3161 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3162}
3163
3164SDValue DAGTypeLegalizer::PromoteIntOp_MaskedBinOp(SDNode *N, unsigned OpNo) {
3165 assert(OpNo == 2);
3166 SmallVector<SDValue, 3> NewOps(N->ops());
3167 NewOps[2] = PromoteTargetBoolean(NewOps[2], N->getValueType(0));
3168 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3169}
3170
3171SDValue DAGTypeLegalizer::PromoteIntOp_PARTIAL_REDUCE_MLA(SDNode *N) {
3172 SmallVector<SDValue, 1> NewOps(N->ops());
3173 switch (N->getOpcode()) {
3175 NewOps[1] = SExtPromotedInteger(N->getOperand(1));
3176 NewOps[2] = SExtPromotedInteger(N->getOperand(2));
3177 break;
3179 NewOps[1] = ZExtPromotedInteger(N->getOperand(1));
3180 NewOps[2] = ZExtPromotedInteger(N->getOperand(2));
3181 break;
3183 NewOps[1] = SExtPromotedInteger(N->getOperand(1));
3184 NewOps[2] = ZExtPromotedInteger(N->getOperand(2));
3185 break;
3186 default:
3187 llvm_unreachable("unexpected opcode");
3188 }
3189 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3190}
3191
3192SDValue DAGTypeLegalizer::PromoteIntOp_LOOP_DEPENDENCE_MASK(SDNode *N) {
3193 SDValue NewOps[4];
3194 NewOps[0] = ZExtPromotedInteger(N->getOperand(0));
3195 NewOps[1] = ZExtPromotedInteger(N->getOperand(1));
3196 NewOps[2] = ZExtPromotedInteger(N->getOperand(2));
3197 NewOps[3] = N->getOperand(3);
3198 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3199}
3200
3201//===----------------------------------------------------------------------===//
3202// Integer Result Expansion
3203//===----------------------------------------------------------------------===//
3204
3205/// ExpandIntegerResult - This method is called when the specified result of the
3206/// specified node is found to need expansion. At this point, the node may also
3207/// have invalid operands or may have other results that need promotion, we just
3208/// know that (at least) one result needs expansion.
3209void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
3210 LLVM_DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG));
3211 SDValue Lo, Hi;
3212 Lo = Hi = SDValue();
3213
3214 // See if the target wants to custom expand this node.
3215 if (CustomLowerNode(N, N->getValueType(ResNo), true))
3216 return;
3217
3218 switch (N->getOpcode()) {
3219 default:
3220#ifndef NDEBUG
3221 dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
3222 N->dump(&DAG); dbgs() << "\n";
3223#endif
3224 report_fatal_error("Do not know how to expand the result of this "
3225 "operator!");
3226
3227 case ISD::ARITH_FENCE: SplitRes_ARITH_FENCE(N, Lo, Hi); break;
3228 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
3229 case ISD::SELECT: SplitRes_Select(N, Lo, Hi); break;
3230 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
3231 case ISD::POISON:
3232 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
3233 case ISD::FREEZE: SplitRes_FREEZE(N, Lo, Hi); break;
3234 case ISD::SETCC: ExpandIntRes_SETCC(N, Lo, Hi); break;
3235
3236 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
3237 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
3238 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
3239 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
3240 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
3241
3242 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
3243 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
3244 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
3245 case ISD::BITREVERSE: ExpandIntRes_BITREVERSE(N, Lo, Hi); break;
3246 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
3247 case ISD::PARITY: ExpandIntRes_PARITY(N, Lo, Hi); break;
3248 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
3249 case ISD::ABS:
3251 ExpandIntRes_ABS(N, Lo, Hi);
3252 break;
3253 case ISD::ABDS:
3254 case ISD::ABDU: ExpandIntRes_ABD(N, Lo, Hi); break;
3256 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
3257 case ISD::CTLS: ExpandIntRes_CTLS(N, Lo, Hi); break;
3258 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
3260 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
3261 case ISD::GET_ROUNDING:ExpandIntRes_GET_ROUNDING(N, Lo, Hi); break;
3263 case ISD::FP_TO_SINT:
3265 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_XINT(N, Lo, Hi); break;
3267 case ISD::FP_TO_UINT_SAT: ExpandIntRes_FP_TO_XINT_SAT(N, Lo, Hi); break;
3268 case ISD::STRICT_LROUND:
3269 case ISD::STRICT_LRINT:
3270 case ISD::LROUND:
3271 case ISD::LRINT:
3273 case ISD::STRICT_LLRINT:
3274 case ISD::LLROUND:
3275 case ISD::LLRINT: ExpandIntRes_XROUND_XRINT(N, Lo, Hi); break;
3276 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
3277 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
3279 case ISD::READSTEADYCOUNTER: ExpandIntRes_READCOUNTER(N, Lo, Hi); break;
3280 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
3281 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
3282 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
3283 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
3284 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
3285 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
3286 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
3287 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
3288 case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
3289
3301 case ISD::ATOMIC_SWAP:
3302 case ISD::ATOMIC_CMP_SWAP: {
3303 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
3304 SplitInteger(Tmp.first, Lo, Hi);
3305 ReplaceValueWith(SDValue(N, 1), Tmp.second);
3306 break;
3307 }
3309 AtomicSDNode *AN = cast<AtomicSDNode>(N);
3310 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other);
3311 SDValue Tmp = DAG.getAtomicCmpSwap(
3312 ISD::ATOMIC_CMP_SWAP, SDLoc(N), AN->getMemoryVT(), VTs,
3313 N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3),
3314 AN->getMemOperand());
3315
3316 // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
3317 // success simply by comparing the loaded value against the ingoing
3318 // comparison.
3319 SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp,
3320 N->getOperand(2), ISD::SETEQ);
3321
3322 SplitInteger(Tmp, Lo, Hi);
3323 ReplaceValueWith(SDValue(N, 1), Success);
3324 ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1));
3325 break;
3326 }
3327
3328 case ISD::AND:
3329 case ISD::OR:
3330 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
3331
3332 case ISD::UMAX:
3333 case ISD::SMAX:
3334 case ISD::UMIN:
3335 case ISD::SMIN: ExpandIntRes_MINMAX(N, Lo, Hi); break;
3336
3337 case ISD::SCMP:
3338 case ISD::UCMP: ExpandIntRes_CMP(N, Lo, Hi); break;
3339
3340 case ISD::ADD:
3341 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
3342
3343 case ISD::ADDC:
3344 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
3345
3346 case ISD::ADDE:
3347 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
3348
3349 case ISD::UADDO_CARRY:
3350 case ISD::USUBO_CARRY: ExpandIntRes_UADDSUBO_CARRY(N, Lo, Hi); break;
3351
3352 case ISD::SADDO_CARRY:
3353 case ISD::SSUBO_CARRY: ExpandIntRes_SADDSUBO_CARRY(N, Lo, Hi); break;
3354
3355 case ISD::SHL:
3356 case ISD::SRA:
3357 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
3358
3359 case ISD::SADDO:
3360 case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
3361 case ISD::UADDO:
3362 case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
3363 case ISD::UMULO:
3364 case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
3365
3366 case ISD::SADDSAT:
3367 case ISD::UADDSAT:
3368 case ISD::SSUBSAT:
3369 case ISD::USUBSAT: ExpandIntRes_ADDSUBSAT(N, Lo, Hi); break;
3370
3371 case ISD::SSHLSAT:
3372 case ISD::USHLSAT: ExpandIntRes_SHLSAT(N, Lo, Hi); break;
3373
3374 case ISD::AVGCEILS:
3375 case ISD::AVGCEILU:
3376 case ISD::AVGFLOORS:
3377 case ISD::AVGFLOORU: ExpandIntRes_AVG(N, Lo, Hi); break;
3378
3379 case ISD::SMULFIX:
3380 case ISD::SMULFIXSAT:
3381 case ISD::UMULFIX:
3382 case ISD::UMULFIXSAT: ExpandIntRes_MULFIX(N, Lo, Hi); break;
3383
3384 case ISD::SDIVFIX:
3385 case ISD::SDIVFIXSAT:
3386 case ISD::UDIVFIX:
3387 case ISD::UDIVFIXSAT: ExpandIntRes_DIVFIX(N, Lo, Hi); break;
3388
3389 case ISD::VECREDUCE_ADD:
3390 case ISD::VECREDUCE_MUL:
3391 case ISD::VECREDUCE_AND:
3392 case ISD::VECREDUCE_OR:
3393 case ISD::VECREDUCE_XOR:
3397 case ISD::VECREDUCE_UMIN: ExpandIntRes_VECREDUCE(N, Lo, Hi); break;
3398
3399 case ISD::ROTL:
3400 case ISD::ROTR:
3401 ExpandIntRes_Rotate(N, Lo, Hi);
3402 break;
3403
3404 case ISD::FSHL:
3405 case ISD::FSHR:
3406 ExpandIntRes_FunnelShift(N, Lo, Hi);
3407 break;
3408
3409 case ISD::CLMUL:
3410 case ISD::CLMULR:
3411 case ISD::CLMULH:
3412 ExpandIntRes_CLMUL(N, Lo, Hi);
3413 break;
3414
3415 case ISD::PEXT:
3416 ExpandIntRes_PEXT(N, Lo, Hi);
3417 break;
3418
3419 case ISD::PDEP:
3420 ExpandIntRes_PDEP(N, Lo, Hi);
3421 break;
3422
3423 case ISD::VSCALE:
3424 ExpandIntRes_VSCALE(N, Lo, Hi);
3425 break;
3426
3427 case ISD::READ_REGISTER:
3428 ExpandIntRes_READ_REGISTER(N, Lo, Hi);
3429 break;
3430
3431 case ISD::CTTZ_ELTS:
3433 ExpandIntRes_CTTZ_ELTS(N, Lo, Hi);
3434 break;
3435 }
3436
3437 // If Lo/Hi is null, the sub-method took care of registering results etc.
3438 if (Lo.getNode())
3439 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
3440}
3441
3442/// Lower an atomic node to the appropriate builtin call.
3443std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
3444 unsigned Opc = Node->getOpcode();
3445 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
3446 AtomicOrdering order = cast<AtomicSDNode>(Node)->getMergedOrdering();
3447 // Lower to outline atomic libcall if outline atomics enabled,
3448 // or to sync libcall otherwise
3449 RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, order, VT);
3450 EVT RetVT = Node->getValueType(0);
3451 TargetLowering::MakeLibCallOptions CallOptions;
3453
3454 RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(LC);
3455 if (LCImpl != RTLIB::Unsupported) {
3456 Ops.append(Node->op_begin() + 2, Node->op_end());
3457 Ops.push_back(Node->getOperand(1));
3458 } else {
3459 LC = RTLIB::getSYNC(Opc, VT);
3460 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
3461 "Unexpected atomic op or value type!");
3462 Ops.append(Node->op_begin() + 1, Node->op_end());
3463 LCImpl = DAG.getLibcalls().getLibcallImpl(LC);
3464 }
3465 return TLI.makeLibCall(DAG, LCImpl, RetVT, Ops, CallOptions, SDLoc(Node),
3466 Node->getOperand(0));
3467}
3468
3469/// N is a shift by a value that needs to be expanded,
3470/// and the shift amount is a constant 'Amt'. Expand the operation.
3471void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, const APInt &Amt,
3472 SDValue &Lo, SDValue &Hi) {
3473 SDLoc DL(N);
3474 // Expand the incoming operand to be shifted, so that we have its parts
3475 SDValue InL, InH;
3476 GetExpandedInteger(N->getOperand(0), InL, InH);
3477
3478 // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
3479 // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
3480 if (!Amt) {
3481 Lo = InL;
3482 Hi = InH;
3483 return;
3484 }
3485
3486 EVT NVT = InL.getValueType();
3487 unsigned VTBits = N->getValueType(0).getSizeInBits();
3488 unsigned NVTBits = NVT.getSizeInBits();
3489
3490 if (N->getOpcode() == ISD::SHL) {
3491 if (Amt.uge(VTBits)) {
3492 Lo = Hi = DAG.getConstant(0, DL, NVT);
3493 } else if (Amt.ugt(NVTBits)) {
3494 Lo = DAG.getConstant(0, DL, NVT);
3495 Hi = DAG.getNode(ISD::SHL, DL, NVT, InL,
3496 DAG.getShiftAmountConstant(Amt - NVTBits, NVT, DL));
3497 } else if (Amt == NVTBits) {
3498 Lo = DAG.getConstant(0, DL, NVT);
3499 Hi = InL;
3500 } else {
3501 Lo = DAG.getNode(ISD::SHL, DL, NVT, InL,
3502 DAG.getShiftAmountConstant(Amt, NVT, DL));
3503 // Use FSHL if legal so we don't need to combine it later.
3504 if (TLI.isOperationLegal(ISD::FSHL, NVT)) {
3505 Hi = DAG.getNode(ISD::FSHL, DL, NVT, InH, InL,
3506 DAG.getShiftAmountConstant(Amt, NVT, DL));
3507 } else {
3508 Hi = DAG.getNode(
3509 ISD::OR, DL, NVT,
3510 DAG.getNode(ISD::SHL, DL, NVT, InH,
3511 DAG.getShiftAmountConstant(Amt, NVT, DL)),
3512 DAG.getNode(ISD::SRL, DL, NVT, InL,
3513 DAG.getShiftAmountConstant(-Amt + NVTBits, NVT, DL)));
3514 }
3515 }
3516 return;
3517 }
3518
3519 if (N->getOpcode() == ISD::SRL) {
3520 if (Amt.uge(VTBits)) {
3521 Lo = Hi = DAG.getConstant(0, DL, NVT);
3522 } else if (Amt.ugt(NVTBits)) {
3523 Lo = DAG.getNode(ISD::SRL, DL, NVT, InH,
3524 DAG.getShiftAmountConstant(Amt - NVTBits, NVT, DL));
3525 Hi = DAG.getConstant(0, DL, NVT);
3526 } else if (Amt == NVTBits) {
3527 Lo = InH;
3528 Hi = DAG.getConstant(0, DL, NVT);
3529 } else {
3530 // Use FSHR if legal so we don't need to combine it later.
3531 if (TLI.isOperationLegal(ISD::FSHR, NVT)) {
3532 Lo = DAG.getNode(ISD::FSHR, DL, NVT, InH, InL,
3533 DAG.getShiftAmountConstant(Amt, NVT, DL));
3534 } else {
3535 Lo = DAG.getNode(
3536 ISD::OR, DL, NVT,
3537 DAG.getNode(ISD::SRL, DL, NVT, InL,
3538 DAG.getShiftAmountConstant(Amt, NVT, DL)),
3539 DAG.getNode(ISD::SHL, DL, NVT, InH,
3540 DAG.getShiftAmountConstant(-Amt + NVTBits, NVT, DL)));
3541 }
3542 Hi = DAG.getNode(ISD::SRL, DL, NVT, InH,
3543 DAG.getShiftAmountConstant(Amt, NVT, DL));
3544 }
3545 return;
3546 }
3547
3548 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
3549 if (Amt.uge(VTBits)) {
3550 Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
3551 DAG.getShiftAmountConstant(NVTBits - 1, NVT, DL));
3552 } else if (Amt.ugt(NVTBits)) {
3553 Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
3554 DAG.getShiftAmountConstant(Amt - NVTBits, NVT, DL));
3555 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
3556 DAG.getShiftAmountConstant(NVTBits - 1, NVT, DL));
3557 } else if (Amt == NVTBits) {
3558 Lo = InH;
3559 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
3560 DAG.getShiftAmountConstant(NVTBits - 1, NVT, DL));
3561 } else {
3562 // Use FSHR if legal so we don't need to combine it later.
3563 if (TLI.isOperationLegal(ISD::FSHR, NVT)) {
3564 Lo = DAG.getNode(ISD::FSHR, DL, NVT, InH, InL,
3565 DAG.getShiftAmountConstant(Amt, NVT, DL));
3566 } else {
3567 Lo = DAG.getNode(
3568 ISD::OR, DL, NVT,
3569 DAG.getNode(ISD::SRL, DL, NVT, InL,
3570 DAG.getShiftAmountConstant(Amt, NVT, DL)),
3571 DAG.getNode(ISD::SHL, DL, NVT, InH,
3572 DAG.getShiftAmountConstant(-Amt + NVTBits, NVT, DL)));
3573 }
3574 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
3575 DAG.getShiftAmountConstant(Amt, NVT, DL));
3576 }
3577}
3578
3579/// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
3580/// this shift based on knowledge of the high bit of the shift amount. If we
3581/// can tell this, we know that it is >= 32 or < 32, without knowing the actual
3582/// shift amount.
3583bool DAGTypeLegalizer::
3584ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
3585 unsigned Opc = N->getOpcode();
3586 SDValue In = N->getOperand(0);
3587 SDValue Amt = N->getOperand(1);
3588 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3589 EVT ShTy = Amt.getValueType();
3590 unsigned ShBits = ShTy.getScalarSizeInBits();
3591 unsigned NVTBits = NVT.getScalarSizeInBits();
3592 assert(isPowerOf2_32(NVTBits) &&
3593 "Expanded integer type size not a power of two!");
3594 SDLoc dl(N);
3595
3596 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
3597 KnownBits Known = DAG.computeKnownBits(Amt);
3598
3599 // If we don't know anything about the high bits, exit.
3600 if (((Known.Zero | Known.One) & HighBitMask) == 0)
3601 return false;
3602
3603 // Get the incoming operand to be shifted.
3604 SDValue InL, InH;
3605 GetExpandedInteger(In, InL, InH);
3606
3607 // If we know that any of the high bits of the shift amount are one, then we
3608 // can do this as a couple of simple shifts.
3609 if (Known.One.intersects(HighBitMask)) {
3610 // Mask out the high bit, which we know is set.
3611 Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
3612 DAG.getConstant(~HighBitMask, dl, ShTy));
3613
3614 switch (Opc) {
3615 default: llvm_unreachable("Unknown shift");
3616 case ISD::SHL:
3617 Lo = DAG.getConstant(0, dl, NVT); // Low part is zero.
3618 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
3619 return true;
3620 case ISD::SRL:
3621 Hi = DAG.getConstant(0, dl, NVT); // Hi part is zero.
3622 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
3623 return true;
3624 case ISD::SRA:
3625 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
3626 DAG.getConstant(NVTBits - 1, dl, ShTy));
3627 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
3628 return true;
3629 }
3630 }
3631
3632 // If we know that all of the high bits of the shift amount are zero, then we
3633 // can do this as a couple of simple shifts.
3634 if (HighBitMask.isSubsetOf(Known.Zero)) {
3635 // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
3636 // shift if x is zero. We can use XOR here because x is known to be smaller
3637 // than 32.
3638 SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
3639 DAG.getConstant(NVTBits - 1, dl, ShTy));
3640
3641 unsigned Op1, Op2;
3642 switch (Opc) {
3643 default: llvm_unreachable("Unknown shift");
3644 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
3645 case ISD::SRL:
3646 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
3647 }
3648
3649 // When shifting right the arithmetic for Lo and Hi is swapped.
3650 if (Opc != ISD::SHL)
3651 std::swap(InL, InH);
3652
3653 // Use a little trick to get the bits that move from Lo to Hi. First
3654 // shift by one bit.
3655 SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, dl, ShTy));
3656 // Then compute the remaining shift with amount-1.
3657 SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
3658
3659 Lo = DAG.getNode(Opc, dl, NVT, InL, Amt);
3660 Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
3661
3662 if (Opc != ISD::SHL)
3663 std::swap(Hi, Lo);
3664 return true;
3665 }
3666
3667 return false;
3668}
3669
3670/// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
3671/// of any size.
3672bool DAGTypeLegalizer::
3673ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
3674 SDValue Amt = N->getOperand(1);
3675 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3676 EVT ShTy = Amt.getValueType();
3677 unsigned NVTBits = NVT.getSizeInBits();
3678 assert(isPowerOf2_32(NVTBits) &&
3679 "Expanded integer type size not a power of two!");
3680 SDLoc dl(N);
3681
3682 // Get the incoming operand to be shifted.
3683 SDValue InL, InH;
3684 GetExpandedInteger(N->getOperand(0), InL, InH);
3685
3686 SDValue NVBitsNode = DAG.getConstant(NVTBits, dl, ShTy);
3687 SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
3688 SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
3689 SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
3690 Amt, NVBitsNode, ISD::SETULT);
3691 SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(ShTy),
3692 Amt, DAG.getConstant(0, dl, ShTy),
3693 ISD::SETEQ);
3694
3695 SDValue LoS, HiS, LoL, HiL;
3696 switch (N->getOpcode()) {
3697 default: llvm_unreachable("Unknown shift");
3698 case ISD::SHL:
3699 // Short: ShAmt < NVTBits
3700 LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
3701 HiS = DAG.getNode(ISD::OR, dl, NVT,
3702 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
3703 DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
3704
3705 // Long: ShAmt >= NVTBits
3706 LoL = DAG.getConstant(0, dl, NVT); // Lo part is zero.
3707 HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
3708
3709 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
3710 Hi = DAG.getSelect(dl, NVT, isZero, InH,
3711 DAG.getSelect(dl, NVT, isShort, HiS, HiL));
3712 return true;
3713 case ISD::SRL:
3714 // Short: ShAmt < NVTBits
3715 HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
3716 LoS = DAG.getNode(ISD::OR, dl, NVT,
3717 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
3718 // FIXME: If Amt is zero, the following shift generates an undefined result
3719 // on some architectures.
3720 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
3721
3722 // Long: ShAmt >= NVTBits
3723 HiL = DAG.getConstant(0, dl, NVT); // Hi part is zero.
3724 LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
3725
3726 Lo = DAG.getSelect(dl, NVT, isZero, InL,
3727 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
3728 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
3729 return true;
3730 case ISD::SRA:
3731 // Short: ShAmt < NVTBits
3732 HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
3733 LoS = DAG.getNode(ISD::OR, dl, NVT,
3734 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
3735 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
3736
3737 // Long: ShAmt >= NVTBits
3738 HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
3739 DAG.getConstant(NVTBits - 1, dl, ShTy));
3740 LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
3741
3742 Lo = DAG.getSelect(dl, NVT, isZero, InL,
3743 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
3744 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
3745 return true;
3746 }
3747}
3748
3749static std::pair<ISD::CondCode, ISD::NodeType> getExpandedMinMaxOps(int Op) {
3750
3751 switch (Op) {
3752 default: llvm_unreachable("invalid min/max opcode");
3753 case ISD::SMAX:
3754 return std::make_pair(ISD::SETGT, ISD::UMAX);
3755 case ISD::UMAX:
3756 return std::make_pair(ISD::SETUGT, ISD::UMAX);
3757 case ISD::SMIN:
3758 return std::make_pair(ISD::SETLT, ISD::UMIN);
3759 case ISD::UMIN:
3760 return std::make_pair(ISD::SETULT, ISD::UMIN);
3761 }
3762}
3763
3764void DAGTypeLegalizer::ExpandIntRes_SETCC(SDNode *N, SDValue &Lo, SDValue &Hi) {
3765 SDLoc DL(N);
3766
3767 SDValue LHS = N->getOperand(0);
3768 SDValue RHS = N->getOperand(1);
3769 EVT NewVT = getSetCCResultType(LHS.getValueType());
3770
3771 // Taking the same approach as ScalarizeVecRes_SETCC
3772 SDValue Res = DAG.getNode(ISD::SETCC, DL, NewVT, LHS, RHS, N->getOperand(2));
3773
3774 Res = DAG.getBoolExtOrTrunc(Res, DL, N->getValueType(0), NewVT);
3775 SplitInteger(Res, Lo, Hi);
3776}
3777
3778void DAGTypeLegalizer::ExpandIntRes_MINMAX(SDNode *N,
3779 SDValue &Lo, SDValue &Hi) {
3780 SDLoc DL(N);
3781
3782 SDValue LHS = N->getOperand(0);
3783 SDValue RHS = N->getOperand(1);
3784
3785 // If the upper halves are all sign bits, then we can perform the MINMAX on
3786 // the lower half and sign-extend the result to the upper half.
3787 unsigned NumBits = N->getValueType(0).getScalarSizeInBits();
3788 unsigned NumHalfBits = NumBits / 2;
3789 if (DAG.ComputeNumSignBits(LHS) > NumHalfBits &&
3790 DAG.ComputeNumSignBits(RHS) > NumHalfBits) {
3791 SDValue LHSL, LHSH, RHSL, RHSH;
3792 GetExpandedInteger(LHS, LHSL, LHSH);
3793 GetExpandedInteger(RHS, RHSL, RHSH);
3794 EVT NVT = LHSL.getValueType();
3795
3796 Lo = DAG.getNode(N->getOpcode(), DL, NVT, LHSL, RHSL);
3797 Hi = DAG.getNode(ISD::SRA, DL, NVT, Lo,
3798 DAG.getShiftAmountConstant(NumHalfBits - 1, NVT, DL));
3799 return;
3800 }
3801
3802 // The Lo of smin(X, -1) is LHSL if X is negative. Otherwise it's -1.
3803 // The Lo of smax(X, 0) is 0 if X is negative. Otherwise it's LHSL.
3804 if ((N->getOpcode() == ISD::SMAX && isNullConstant(RHS)) ||
3805 (N->getOpcode() == ISD::SMIN && isAllOnesConstant(RHS))) {
3806 SDValue LHSL, LHSH, RHSL, RHSH;
3807 GetExpandedInteger(LHS, LHSL, LHSH);
3808 GetExpandedInteger(RHS, RHSL, RHSH);
3809 EVT NVT = LHSL.getValueType();
3810 EVT CCT = getSetCCResultType(NVT);
3811
3812 SDValue HiNeg =
3813 DAG.getSetCC(DL, CCT, LHSH, DAG.getConstant(0, DL, NVT), ISD::SETLT);
3814 if (N->getOpcode() == ISD::SMIN) {
3815 Lo = DAG.getSelect(DL, NVT, HiNeg, LHSL, DAG.getAllOnesConstant(DL, NVT));
3816 } else {
3817 Lo = DAG.getSelect(DL, NVT, HiNeg, DAG.getConstant(0, DL, NVT), LHSL);
3818 }
3819 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
3820 return;
3821 }
3822
3823 const APInt *RHSVal = nullptr;
3824 if (auto *RHSConst = dyn_cast<ConstantSDNode>(RHS))
3825 RHSVal = &RHSConst->getAPIntValue();
3826
3827 // The high half of MIN/MAX is always just the the MIN/MAX of the
3828 // high halves of the operands. Expand this way if it appears profitable.
3829 if (RHSVal && (N->getOpcode() == ISD::UMIN || N->getOpcode() == ISD::UMAX) &&
3830 (RHSVal->countLeadingOnes() >= NumHalfBits ||
3831 RHSVal->countLeadingZeros() >= NumHalfBits)) {
3832 SDValue LHSL, LHSH, RHSL, RHSH;
3833 GetExpandedInteger(LHS, LHSL, LHSH);
3834 GetExpandedInteger(RHS, RHSL, RHSH);
3835 EVT NVT = LHSL.getValueType();
3836 EVT CCT = getSetCCResultType(NVT);
3837
3838 ISD::NodeType LoOpc;
3839 ISD::CondCode CondC;
3840 std::tie(CondC, LoOpc) = getExpandedMinMaxOps(N->getOpcode());
3841
3842 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
3843 // We need to know whether to select Lo part that corresponds to 'winning'
3844 // Hi part or if Hi parts are equal.
3845 SDValue IsHiLeft = DAG.getSetCC(DL, CCT, LHSH, RHSH, CondC);
3846 SDValue IsHiEq = DAG.getSetCC(DL, CCT, LHSH, RHSH, ISD::SETEQ);
3847
3848 // Lo part corresponding to the 'winning' Hi part
3849 SDValue LoCmp = DAG.getSelect(DL, NVT, IsHiLeft, LHSL, RHSL);
3850
3851 // Recursed Lo part if Hi parts are equal, this uses unsigned version
3852 SDValue LoMinMax = DAG.getNode(LoOpc, DL, NVT, {LHSL, RHSL});
3853
3854 Lo = DAG.getSelect(DL, NVT, IsHiEq, LoMinMax, LoCmp);
3855 return;
3856 }
3857
3858 // Expand to "a < b ? a : b" etc. Prefer ge/le if that simplifies
3859 // the compare.
3860 ISD::CondCode Pred;
3861 switch (N->getOpcode()) {
3862 default: llvm_unreachable("How did we get here?");
3863 case ISD::SMAX:
3864 if (RHSVal && RHSVal->countTrailingZeros() >= NumHalfBits)
3865 Pred = ISD::SETGE;
3866 else
3867 Pred = ISD::SETGT;
3868 break;
3869 case ISD::SMIN:
3870 if (RHSVal && RHSVal->countTrailingOnes() >= NumHalfBits)
3871 Pred = ISD::SETLE;
3872 else
3873 Pred = ISD::SETLT;
3874 break;
3875 case ISD::UMAX:
3876 if (RHSVal && RHSVal->countTrailingZeros() >= NumHalfBits)
3877 Pred = ISD::SETUGE;
3878 else
3879 Pred = ISD::SETUGT;
3880 break;
3881 case ISD::UMIN:
3882 if (RHSVal && RHSVal->countTrailingOnes() >= NumHalfBits)
3883 Pred = ISD::SETULE;
3884 else
3885 Pred = ISD::SETULT;
3886 break;
3887 }
3888 EVT VT = N->getValueType(0);
3889 EVT CCT = getSetCCResultType(VT);
3890 SDValue Cond = DAG.getSetCC(DL, CCT, LHS, RHS, Pred);
3891 SDValue Result = DAG.getSelect(DL, VT, Cond, LHS, RHS);
3892 SplitInteger(Result, Lo, Hi);
3893}
3894
3895void DAGTypeLegalizer::ExpandIntRes_CMP(SDNode *N, SDValue &Lo, SDValue &Hi) {
3896 SDValue ExpandedCMP = TLI.expandCMP(N, DAG);
3897 SplitInteger(ExpandedCMP, Lo, Hi);
3898}
3899
3900void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
3901 SDValue &Lo, SDValue &Hi) {
3902 SDLoc dl(N);
3903 // Expand the subcomponents.
3904 SDValue LHSL, LHSH, RHSL, RHSH;
3905 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3906 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3907
3908 EVT NVT = LHSL.getValueType();
3909 SDValue LoOps[2] = { LHSL, RHSL };
3910 SDValue HiOps[3] = { LHSH, RHSH };
3911
3912 bool HasOpCarry = TLI.isOperationLegalOrCustom(
3913 N->getOpcode() == ISD::ADD ? ISD::UADDO_CARRY : ISD::USUBO_CARRY,
3914 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3915 if (HasOpCarry) {
3916 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
3917 if (N->getOpcode() == ISD::ADD) {
3918 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
3919 HiOps[2] = Lo.getValue(1);
3920 Hi = DAG.computeKnownBits(HiOps[2]).isZero()
3921 ? DAG.getNode(ISD::ADD, dl, NVT, ArrayRef(HiOps, 2))
3922 : DAG.getNode(ISD::UADDO_CARRY, dl, VTList, HiOps);
3923 } else {
3924 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
3925 HiOps[2] = Lo.getValue(1);
3926 Hi = DAG.computeKnownBits(HiOps[2]).isZero()
3927 ? DAG.getNode(ISD::SUB, dl, NVT, ArrayRef(HiOps, 2))
3928 : DAG.getNode(ISD::USUBO_CARRY, dl, VTList, HiOps);
3929 }
3930 return;
3931 }
3932
3933 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
3934 // them. TODO: Teach operation legalization how to expand unsupported
3935 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
3936 // a carry of type MVT::Glue, but there doesn't seem to be any way to
3937 // generate a value of this type in the expanded code sequence.
3938 bool hasCarry =
3939 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
3941 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3942
3943 if (hasCarry) {
3944 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
3945 if (N->getOpcode() == ISD::ADD) {
3946 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
3947 HiOps[2] = Lo.getValue(1);
3948 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
3949 } else {
3950 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
3951 HiOps[2] = Lo.getValue(1);
3952 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
3953 }
3954 return;
3955 }
3956
3957 bool hasOVF =
3958 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
3960 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3961 TargetLoweringBase::BooleanContent BoolType = TLI.getBooleanContents(NVT);
3962
3963 if (hasOVF) {
3964 EVT OvfVT = getSetCCResultType(NVT);
3965 SDVTList VTList = DAG.getVTList(NVT, OvfVT);
3966 int RevOpc;
3967 if (N->getOpcode() == ISD::ADD) {
3968 RevOpc = ISD::SUB;
3969 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
3970 Hi = DAG.getNode(ISD::ADD, dl, NVT, ArrayRef(HiOps, 2));
3971 } else {
3972 RevOpc = ISD::ADD;
3973 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
3974 Hi = DAG.getNode(ISD::SUB, dl, NVT, ArrayRef(HiOps, 2));
3975 }
3976 SDValue OVF = Lo.getValue(1);
3977
3978 switch (BoolType) {
3980 OVF = DAG.getNode(ISD::AND, dl, OvfVT, DAG.getConstant(1, dl, OvfVT), OVF);
3981 [[fallthrough]];
3983 OVF = DAG.getZExtOrTrunc(OVF, dl, NVT);
3984 Hi = DAG.getNode(N->getOpcode(), dl, NVT, Hi, OVF);
3985 break;
3987 OVF = DAG.getSExtOrTrunc(OVF, dl, NVT);
3988 Hi = DAG.getNode(RevOpc, dl, NVT, Hi, OVF);
3989 }
3990 return;
3991 }
3992
3993 if (N->getOpcode() == ISD::ADD) {
3994 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps);
3995 SDValue Cmp;
3996 // Special case: X+1 has a carry out if X+1==0. This may reduce the live
3997 // range of X. We assume comparing with 0 is cheap.
3998 if (isOneConstant(LoOps[1]))
3999 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
4000 DAG.getConstant(0, dl, NVT), ISD::SETEQ);
4001 else if (isAllOnesConstant(LoOps[1])) {
4002 if (isAllOnesConstant(HiOps[1]))
4003 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), LoOps[0],
4004 DAG.getConstant(0, dl, NVT), ISD::SETEQ);
4005 else
4006 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), LoOps[0],
4007 DAG.getConstant(0, dl, NVT), ISD::SETNE);
4008 } else
4009 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
4010 ISD::SETULT);
4011
4012 SDValue Carry;
4014 Carry = DAG.getZExtOrTrunc(Cmp, dl, NVT);
4015 else
4016 Carry = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
4017 DAG.getConstant(0, dl, NVT));
4018
4019 if (isAllOnesConstant(LoOps[1]) && isAllOnesConstant(HiOps[1])) {
4020 Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps[0], Carry);
4021 } else {
4022 Hi = DAG.getNode(ISD::ADD, dl, NVT, ArrayRef(HiOps, 2));
4023 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry);
4024 }
4025 } else {
4026 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps);
4027 Hi = DAG.getNode(ISD::SUB, dl, NVT, ArrayRef(HiOps, 2));
4028 SDValue Cmp =
4029 DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
4030 LoOps[0], LoOps[1], ISD::SETULT);
4031
4032 SDValue Borrow;
4034 Borrow = DAG.getZExtOrTrunc(Cmp, dl, NVT);
4035 else
4036 Borrow = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
4037 DAG.getConstant(0, dl, NVT));
4038
4039 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
4040 }
4041}
4042
4043void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
4044 SDValue &Lo, SDValue &Hi) {
4045 // Expand the subcomponents.
4046 SDValue LHSL, LHSH, RHSL, RHSH;
4047 SDLoc dl(N);
4048 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
4049 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
4050 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
4051 SDValue LoOps[2] = { LHSL, RHSL };
4052 SDValue HiOps[3] = { LHSH, RHSH };
4053
4054 if (N->getOpcode() == ISD::ADDC) {
4055 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
4056 HiOps[2] = Lo.getValue(1);
4057 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
4058 } else {
4059 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
4060 HiOps[2] = Lo.getValue(1);
4061 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
4062 }
4063
4064 // Legalized the flag result - switch anything that used the old flag to
4065 // use the new one.
4066 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
4067}
4068
4069void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
4070 SDValue &Lo, SDValue &Hi) {
4071 // Expand the subcomponents.
4072 SDValue LHSL, LHSH, RHSL, RHSH;
4073 SDLoc dl(N);
4074 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
4075 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
4076 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
4077 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
4078 SDValue HiOps[3] = { LHSH, RHSH };
4079
4080 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
4081 HiOps[2] = Lo.getValue(1);
4082 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
4083
4084 // Legalized the flag result - switch anything that used the old flag to
4085 // use the new one.
4086 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
4087}
4088
4089void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
4090 SDValue &Lo, SDValue &Hi) {
4091 SDValue LHS = N->getOperand(0);
4092 SDValue RHS = N->getOperand(1);
4093 SDLoc dl(N);
4094
4095 SDValue Ovf;
4096
4097 unsigned CarryOp, NoCarryOp;
4099 switch(N->getOpcode()) {
4100 case ISD::UADDO:
4101 CarryOp = ISD::UADDO_CARRY;
4102 NoCarryOp = ISD::ADD;
4103 Cond = ISD::SETULT;
4104 break;
4105 case ISD::USUBO:
4106 CarryOp = ISD::USUBO_CARRY;
4107 NoCarryOp = ISD::SUB;
4108 Cond = ISD::SETUGT;
4109 break;
4110 default:
4111 llvm_unreachable("Node has unexpected Opcode");
4112 }
4113
4114 bool HasCarryOp = TLI.isOperationLegalOrCustom(
4115 CarryOp, TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
4116
4117 if (HasCarryOp) {
4118 // Expand the subcomponents.
4119 SDValue LHSL, LHSH, RHSL, RHSH;
4120 GetExpandedInteger(LHS, LHSL, LHSH);
4121 GetExpandedInteger(RHS, RHSL, RHSH);
4122 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
4123 SDValue LoOps[2] = { LHSL, RHSL };
4124 SDValue HiOps[3] = { LHSH, RHSH };
4125
4126 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
4127 HiOps[2] = Lo.getValue(1);
4128 Hi = DAG.getNode(CarryOp, dl, VTList, HiOps);
4129
4130 Ovf = Hi.getValue(1);
4131 } else {
4132 // Expand the result by simply replacing it with the equivalent
4133 // non-overflow-checking operation.
4134 SDValue Sum = DAG.getNode(NoCarryOp, dl, LHS.getValueType(), LHS, RHS);
4135 SplitInteger(Sum, Lo, Hi);
4136
4137 if (N->getOpcode() == ISD::UADDO && isOneConstant(RHS)) {
4138 // Special case: uaddo X, 1 overflowed if X+1 == 0. We can detect this
4139 // with (Lo | Hi) == 0.
4140 SDValue Or = DAG.getNode(ISD::OR, dl, Lo.getValueType(), Lo, Hi);
4141 Ovf = DAG.getSetCC(dl, N->getValueType(1), Or,
4142 DAG.getConstant(0, dl, Lo.getValueType()), ISD::SETEQ);
4143 } else if (N->getOpcode() == ISD::UADDO && isAllOnesConstant(RHS)) {
4144 // Special case: uaddo X, -1 overflows if X == 0.
4145 Ovf =
4146 DAG.getSetCC(dl, N->getValueType(1), LHS,
4147 DAG.getConstant(0, dl, LHS.getValueType()), ISD::SETNE);
4148 } else {
4149 // Calculate the overflow: addition overflows iff a + b < a, and
4150 // subtraction overflows iff a - b > a.
4151 Ovf = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS, Cond);
4152 }
4153 }
4154
4155 // Legalized the flag result - switch anything that used the old flag to
4156 // use the new one.
4157 ReplaceValueWith(SDValue(N, 1), Ovf);
4158}
4159
4160void DAGTypeLegalizer::ExpandIntRes_UADDSUBO_CARRY(SDNode *N, SDValue &Lo,
4161 SDValue &Hi) {
4162 // Expand the subcomponents.
4163 SDValue LHSL, LHSH, RHSL, RHSH;
4164 SDLoc dl(N);
4165 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
4166 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
4167 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
4168 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
4169 SDValue HiOps[3] = { LHSH, RHSH, SDValue() };
4170
4171 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
4172 HiOps[2] = Lo.getValue(1);
4173 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
4174
4175 // Legalized the flag result - switch anything that used the old flag to
4176 // use the new one.
4177 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
4178}
4179
4180void DAGTypeLegalizer::ExpandIntRes_SADDSUBO_CARRY(SDNode *N,
4181 SDValue &Lo, SDValue &Hi) {
4182 // Expand the subcomponents.
4183 SDValue LHSL, LHSH, RHSL, RHSH;
4184 SDLoc dl(N);
4185 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
4186 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
4187 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
4188
4189 // We need to use an unsigned carry op for the lo part.
4190 unsigned CarryOp =
4192 Lo = DAG.getNode(CarryOp, dl, VTList, { LHSL, RHSL, N->getOperand(2) });
4193 Hi = DAG.getNode(N->getOpcode(), dl, VTList, { LHSH, RHSH, Lo.getValue(1) });
4194
4195 // Legalized the flag result - switch anything that used the old flag to
4196 // use the new one.
4197 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
4198}
4199
4200void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
4201 SDValue &Lo, SDValue &Hi) {
4202 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4203 SDLoc dl(N);
4204 SDValue Op = N->getOperand(0);
4205 if (Op.getValueType().bitsLE(NVT)) {
4206 // The low part is any extension of the input (which degenerates to a copy).
4207 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
4208 Hi = DAG.getUNDEF(NVT); // The high part is undefined.
4209 } else {
4210 // For example, extension of an i48 to an i64. The operand type necessarily
4211 // promotes to the result type, so will end up being expanded too.
4212 assert(getTypeAction(Op.getValueType()) ==
4214 "Only know how to promote this result!");
4215 SDValue Res = GetPromotedInteger(Op);
4216 assert(Res.getValueType() == N->getValueType(0) &&
4217 "Operand over promoted?");
4218 // Split the promoted operand. This will simplify when it is expanded.
4219 SplitInteger(Res, Lo, Hi);
4220 }
4221}
4222
4223void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
4224 SDValue &Lo, SDValue &Hi) {
4225 SDLoc dl(N);
4226 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4227 EVT NVT = Lo.getValueType();
4228 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
4229 unsigned NVTBits = NVT.getSizeInBits();
4230 unsigned EVTBits = EVT.getSizeInBits();
4231
4232 if (NVTBits < EVTBits) {
4233 Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
4234 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
4235 EVTBits - NVTBits)));
4236 } else {
4237 Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
4238 // The high part replicates the sign bit of Lo, make it explicit.
4239 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
4240 DAG.getShiftAmountConstant(NVTBits - 1, NVT, dl));
4241 }
4242}
4243
4244void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
4245 SDValue &Lo, SDValue &Hi) {
4246 SDLoc dl(N);
4247 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4248 EVT NVT = Lo.getValueType();
4249 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
4250 unsigned NVTBits = NVT.getSizeInBits();
4251 unsigned EVTBits = EVT.getSizeInBits();
4252
4253 if (NVTBits < EVTBits) {
4254 Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
4255 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
4256 EVTBits - NVTBits)));
4257 } else {
4258 Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
4259 // The high part must be zero, make it explicit.
4260 Hi = DAG.getConstant(0, dl, NVT);
4261 }
4262}
4263
4264void DAGTypeLegalizer::ExpandIntRes_BITREVERSE(SDNode *N,
4265 SDValue &Lo, SDValue &Hi) {
4266 SDLoc dl(N);
4267 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
4268 Lo = DAG.getNode(ISD::BITREVERSE, dl, Lo.getValueType(), Lo);
4269 Hi = DAG.getNode(ISD::BITREVERSE, dl, Hi.getValueType(), Hi);
4270}
4271
4272void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
4273 SDValue &Lo, SDValue &Hi) {
4274 SDLoc dl(N);
4275 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
4276 Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
4277 Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
4278}
4279
4280void DAGTypeLegalizer::ExpandIntRes_PARITY(SDNode *N, SDValue &Lo,
4281 SDValue &Hi) {
4282 SDLoc dl(N);
4283 // parity(HiLo) -> parity(Lo^Hi)
4284 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4285 EVT NVT = Lo.getValueType();
4286 Lo =
4287 DAG.getNode(ISD::PARITY, dl, NVT, DAG.getNode(ISD::XOR, dl, NVT, Lo, Hi));
4288 Hi = DAG.getConstant(0, dl, NVT);
4289}
4290
4291void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
4292 SDValue &Lo, SDValue &Hi) {
4293 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4294 unsigned NBitWidth = NVT.getSizeInBits();
4296 const APInt &Cst = Constant->getAPIntValue();
4297 bool IsTarget = Constant->isTargetOpcode();
4298 bool IsOpaque = Constant->isOpaque();
4299 SDLoc dl(N);
4300 Lo = DAG.getConstant(Cst.trunc(NBitWidth), dl, NVT, IsTarget, IsOpaque);
4301 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), dl, NVT, IsTarget,
4302 IsOpaque);
4303}
4304
4305void DAGTypeLegalizer::ExpandIntRes_ABS(SDNode *N, SDValue &Lo, SDValue &Hi) {
4306 SDLoc dl(N);
4307
4308 SDValue N0 = N->getOperand(0);
4309 GetExpandedInteger(N0, Lo, Hi);
4310 EVT NVT = Lo.getValueType();
4311
4312 // If the upper half is all sign bits, then we can perform the ABS on the
4313 // lower half and zero-extend. We could use ISD::ABS_MIN_POISON here if
4314 // DAG.ComputeNumSignBits(N0) is larger than NVT.getScalarSizeInBits() + 1.
4315 unsigned NumSignBits = DAG.ComputeNumSignBits(N0);
4316 if (NumSignBits > NVT.getScalarSizeInBits()) {
4317 unsigned AbsOpc = NumSignBits > NVT.getScalarSizeInBits() + 1
4319 : ISD::ABS;
4320 Lo = DAG.getNode(AbsOpc, dl, NVT, Lo);
4321 Hi = DAG.getConstant(0, dl, NVT);
4322 return;
4323 }
4324
4325 // If we have USUBO_CARRY, use the expanded form of the sra+xor+sub sequence
4326 // we use in LegalizeDAG. The SUB part of the expansion is based on
4327 // ExpandIntRes_ADDSUB which also uses USUBO_CARRY/USUBO after checking that
4328 // USUBO_CARRY is LegalOrCustom. Each of the pieces here can be further
4329 // expanded if needed. Shift expansion has a special case for filling with
4330 // sign bits so that we will only end up with one SRA.
4331 bool HasSubCarry = TLI.isOperationLegalOrCustom(
4332 ISD::USUBO_CARRY, TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
4333 if (HasSubCarry) {
4334 SDValue Sign = DAG.getNode(
4335 ISD::SRA, dl, NVT, Hi,
4336 DAG.getShiftAmountConstant(NVT.getSizeInBits() - 1, NVT, dl));
4337 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
4338 Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Sign);
4339 Hi = DAG.getNode(ISD::XOR, dl, NVT, Hi, Sign);
4340 Lo = DAG.getNode(ISD::USUBO, dl, VTList, Lo, Sign);
4341 Hi = DAG.getNode(ISD::USUBO_CARRY, dl, VTList, Hi, Sign, Lo.getValue(1));
4342 return;
4343 }
4344
4345 // abs(HiLo) -> (Hi < 0 ? -HiLo : HiLo)
4346 EVT VT = N->getValueType(0);
4347 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT,
4348 DAG.getConstant(0, dl, VT), N0);
4349 SDValue NegLo, NegHi;
4350 SplitInteger(Neg, NegLo, NegHi);
4351
4352 SDValue HiIsNeg = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
4353 DAG.getConstant(0, dl, NVT), ISD::SETLT);
4354 Lo = DAG.getSelect(dl, NVT, HiIsNeg, NegLo, Lo);
4355 Hi = DAG.getSelect(dl, NVT, HiIsNeg, NegHi, Hi);
4356}
4357
4358void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
4359 SDValue &Lo, SDValue &Hi) {
4360 SDLoc dl(N);
4361 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
4362 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4363 EVT NVT = Lo.getValueType();
4364
4365 SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
4366 DAG.getConstant(0, dl, NVT), ISD::SETNE);
4367
4368 SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
4369 SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_POISON, dl, NVT, Hi);
4370
4371 Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
4372 DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
4373 DAG.getConstant(NVT.getSizeInBits(), dl,
4374 NVT)));
4375 Hi = DAG.getConstant(0, dl, NVT);
4376}
4377
4378void DAGTypeLegalizer::ExpandIntRes_CTLS(SDNode *N, SDValue &Lo, SDValue &Hi) {
4379 SDLoc dl(N);
4380 // ctls(HiLo) -> if (IsAllSignBits = (ctls(Hi) == BW-1)) then
4381 // BW-1 + clz(IsNegative = (Hi < 0) ? ~Lo : Lo)
4382 // else ctls(Hi)
4383 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4384 EVT NVT = Lo.getValueType();
4385 unsigned NVTBits = NVT.getScalarSizeInBits();
4386
4387 SDValue Constant0 = DAG.getConstant(0, dl, NVT);
4388 SDValue ConstantBWM1 = DAG.getConstant(NVTBits - 1, dl, NVT);
4389
4390 SDValue HiCTLS = DAG.getNode(ISD::CTLS, dl, NVT, Hi);
4391 SDValue IsAllSignBits = DAG.getSetCC(dl, getSetCCResultType(NVT), HiCTLS,
4392 ConstantBWM1, ISD::SETEQ);
4393 SDValue IsNegative =
4394 DAG.getSetCC(dl, getSetCCResultType(NVT), Hi, Constant0, ISD::SETLT);
4395 SDValue AdjustedLo =
4396 DAG.getSelect(dl, NVT, IsNegative, DAG.getNOT(dl, Lo, NVT), Lo);
4397 SDValue LoCLZ = DAG.getNode(ISD::CTLZ, dl, NVT, AdjustedLo);
4398 Lo = DAG.getSelect(dl, NVT, IsAllSignBits,
4399 DAG.getNode(ISD::ADD, dl, NVT, LoCLZ, ConstantBWM1),
4400 HiCTLS);
4401 Hi = DAG.getConstant(0, dl, NVT);
4402}
4403
4404void DAGTypeLegalizer::ExpandIntRes_ABD(SDNode *N, SDValue &Lo, SDValue &Hi) {
4405 SDValue Result = TLI.expandABD(N, DAG);
4406 SplitInteger(Result, Lo, Hi);
4407}
4408
4409void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N, SDValue &Lo, SDValue &Hi) {
4410 SDValue Op = N->getOperand(0);
4411 EVT VT = N->getValueType(0);
4412 SDLoc DL(N);
4413
4414 if (TLI.getOperationAction(ISD::CTPOP, VT) == TargetLoweringBase::LibCall) {
4415 RTLIB::Libcall LC = RTLIB::getCTPOP(VT);
4416 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4417 "LibCall explicitly requested, but not available");
4418
4419 if (RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(LC)) {
4420 TargetLowering::MakeLibCallOptions CallOptions;
4421 EVT IntVT =
4422 EVT::getIntegerVT(*DAG.getContext(), DAG.getLibInfo().getIntSize());
4423 SDValue Res =
4424 TLI.makeLibCall(DAG, LCImpl, IntVT, Op, CallOptions, DL).first;
4425 SplitInteger(DAG.getSExtOrTrunc(Res, DL, VT), Lo, Hi);
4426 return;
4427 }
4428
4429 // If the function is not available, fall back on the expansion.
4430 }
4431
4432 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
4433 GetExpandedInteger(Op, Lo, Hi);
4434 EVT NVT = Lo.getValueType();
4435 Lo = DAG.getNode(ISD::ADD, DL, NVT, DAG.getNode(ISD::CTPOP, DL, NVT, Lo),
4436 DAG.getNode(ISD::CTPOP, DL, NVT, Hi));
4437 Hi = DAG.getConstant(0, DL, NVT);
4438}
4439
4440void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
4441 SDValue &Lo, SDValue &Hi) {
4442 SDLoc dl(N);
4443 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
4444 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4445 EVT NVT = Lo.getValueType();
4446
4447 SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
4448 DAG.getConstant(0, dl, NVT), ISD::SETNE);
4449
4450 SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_POISON, dl, NVT, Lo);
4451 SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
4452
4453 Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
4454 DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
4455 DAG.getConstant(NVT.getSizeInBits(), dl,
4456 NVT)));
4457 Hi = DAG.getConstant(0, dl, NVT);
4458}
4459
4460void DAGTypeLegalizer::ExpandIntRes_GET_ROUNDING(SDNode *N, SDValue &Lo,
4461 SDValue &Hi) {
4462 SDLoc dl(N);
4463 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4464 unsigned NBitWidth = NVT.getSizeInBits();
4465
4466 Lo = DAG.getNode(ISD::GET_ROUNDING, dl, {NVT, MVT::Other}, N->getOperand(0));
4467 SDValue Chain = Lo.getValue(1);
4468 // The high part is the sign of Lo, as -1 is a valid value for GET_ROUNDING
4469 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
4470 DAG.getShiftAmountConstant(NBitWidth - 1, NVT, dl));
4471
4472 // Legalize the chain result - switch anything that used the old chain to
4473 // use the new one.
4474 ReplaceValueWith(SDValue(N, 1), Chain);
4475}
4476
4477// Helper for producing an FP_EXTEND/STRICT_FP_EXTEND of Op.
4478static SDValue fpExtendHelper(SDValue Op, SDValue &Chain, bool IsStrict, EVT VT,
4479 SDLoc DL, SelectionDAG &DAG) {
4480 if (IsStrict) {
4481 Op = DAG.getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op});
4482 Chain = Op.getValue(1);
4483 return Op;
4484 }
4485 return DAG.getNode(ISD::FP_EXTEND, DL, VT, Op);
4486}
4487
4488void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT(SDNode *N, SDValue &Lo,
4489 SDValue &Hi) {
4490 SDLoc dl(N);
4491 EVT VT = N->getValueType(0);
4492
4493 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
4494 N->getOpcode() == ISD::STRICT_FP_TO_SINT;
4495 bool IsStrict = N->isStrictFPOpcode();
4496 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
4497 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
4498
4499 // If the input is bf16 or needs to be soft promoted, extend to f32.
4500 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftPromoteHalf ||
4501 Op.getValueType() == MVT::bf16) {
4502 Op = fpExtendHelper(Op, Chain, IsStrict, MVT::f32, dl, DAG);
4503 }
4504
4505 // NOTE: We need a variable that lives across makeLibCall so
4506 // CallOptions.setTypeListBeforeSoften can save a reference to it.
4507 EVT OpVT = Op.getValueType();
4508
4509 RTLIB::Libcall LC =
4510 IsSigned ? RTLIB::getFPTOSINT(OpVT, VT) : RTLIB::getFPTOUINT(OpVT, VT);
4511 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-xint conversion!");
4512 TargetLowering::MakeLibCallOptions CallOptions;
4513 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftenFloat)
4514 CallOptions.setTypeListBeforeSoften(OpVT, VT);
4515 else
4516 CallOptions.setIsSigned(true); // FIXME: Is this needed?
4517 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, VT, Op,
4518 CallOptions, dl, Chain);
4519 SplitInteger(Tmp.first, Lo, Hi);
4520
4521 if (IsStrict)
4522 ReplaceValueWith(SDValue(N, 1), Tmp.second);
4523}
4524
4525void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT_SAT(SDNode *N, SDValue &Lo,
4526 SDValue &Hi) {
4527 SDValue Res = TLI.expandFP_TO_INT_SAT(N, DAG);
4528 SplitInteger(Res, Lo, Hi);
4529}
4530
4531void DAGTypeLegalizer::ExpandIntRes_XROUND_XRINT(SDNode *N, SDValue &Lo,
4532 SDValue &Hi) {
4533 SDLoc dl(N);
4534 bool IsStrict = N->isStrictFPOpcode();
4535 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
4536 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
4537
4538 EVT VT = Op.getValueType();
4539
4540 if (VT == MVT::f16) {
4541 // Extend to f32.
4542 VT = MVT::f32;
4543 Op = fpExtendHelper(Op, Chain, IsStrict, VT, dl, DAG);
4544 }
4545
4546 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4547 if (N->getOpcode() == ISD::LROUND ||
4548 N->getOpcode() == ISD::STRICT_LROUND) {
4549 LC = RTLIB::getLROUND(VT);
4550 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected lround input type!");
4551 } else if (N->getOpcode() == ISD::LRINT ||
4552 N->getOpcode() == ISD::STRICT_LRINT) {
4553 LC = RTLIB::getLRINT(VT);
4554 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected lrint input type!");
4555 } else if (N->getOpcode() == ISD::LLROUND ||
4556 N->getOpcode() == ISD::STRICT_LLROUND) {
4557 LC = RTLIB::getLLROUND(VT);
4558 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llround input type!");
4559 } else if (N->getOpcode() == ISD::LLRINT ||
4560 N->getOpcode() == ISD::STRICT_LLRINT) {
4561 LC = RTLIB::getLLRINT(VT);
4562 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llrint input type!");
4563 } else
4564 llvm_unreachable("Unexpected opcode!");
4565
4566 EVT RetVT = N->getValueType(0);
4567
4568 TargetLowering::MakeLibCallOptions CallOptions;
4569 CallOptions.setIsSigned(true);
4570 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
4571 Op, CallOptions, dl,
4572 Chain);
4573 SplitInteger(Tmp.first, Lo, Hi);
4574
4575 if (N->isStrictFPOpcode())
4576 ReplaceValueWith(SDValue(N, 1), Tmp.second);
4577}
4578
4579void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
4580 SDValue &Lo, SDValue &Hi) {
4581 assert(!N->isAtomic() && "Should have been a ATOMIC_LOAD?");
4582
4583 if (ISD::isNormalLoad(N)) {
4584 ExpandRes_NormalLoad(N, Lo, Hi);
4585 return;
4586 }
4587
4588 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
4589
4590 EVT VT = N->getValueType(0);
4591 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4592 SDValue Ch = N->getChain();
4593 SDValue Ptr = N->getBasePtr();
4594 ISD::LoadExtType ExtType = N->getExtensionType();
4595 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
4596 AAMDNodes AAInfo = N->getAAInfo();
4597 SDLoc dl(N);
4598
4599 assert(NVT.isByteSized() && "Expanded type not byte sized!");
4600
4601 if (N->getMemoryVT().bitsLE(NVT)) {
4602 EVT MemVT = N->getMemoryVT();
4603
4604 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(), MemVT,
4605 N->getBaseAlign(), MMOFlags, AAInfo);
4606
4607 // Remember the chain.
4608 Ch = Lo.getValue(1);
4609
4610 if (ExtType == ISD::SEXTLOAD) {
4611 // The high part is obtained by SRA'ing all but one of the bits of the
4612 // lo part.
4613 unsigned LoSize = Lo.getValueSizeInBits();
4614 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
4615 DAG.getShiftAmountConstant(LoSize - 1, NVT, dl));
4616 } else if (ExtType == ISD::ZEXTLOAD) {
4617 // The high part is just a zero.
4618 Hi = DAG.getConstant(0, dl, NVT);
4619 } else {
4620 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
4621 // The high part is undefined.
4622 Hi = DAG.getUNDEF(NVT);
4623 }
4624 } else if (DAG.getDataLayout().isLittleEndian()) {
4625 // Little-endian - low bits are at low addresses.
4626 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(), N->getBaseAlign(),
4627 MMOFlags, AAInfo);
4628
4629 unsigned ExcessBits =
4630 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
4631 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
4632
4633 // Increment the pointer to the other half.
4634 unsigned IncrementSize = NVT.getSizeInBits()/8;
4635 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
4636 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
4637 N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
4638 N->getBaseAlign(), MMOFlags, AAInfo);
4639
4640 // Build a factor node to remember that this load is independent of the
4641 // other one.
4642 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
4643 Hi.getValue(1));
4644 } else {
4645 // Big-endian - high bits are at low addresses. Favor aligned loads at
4646 // the cost of some bit-fiddling.
4647 EVT MemVT = N->getMemoryVT();
4648 unsigned EBytes = MemVT.getStoreSize();
4649 unsigned IncrementSize = NVT.getSizeInBits()/8;
4650 unsigned ExcessBits = (EBytes - IncrementSize)*8;
4651
4652 // Load both the high bits and maybe some of the low bits.
4653 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
4654 EVT::getIntegerVT(*DAG.getContext(),
4655 MemVT.getSizeInBits() - ExcessBits),
4656 N->getBaseAlign(), MMOFlags, AAInfo);
4657
4658 // Increment the pointer to the other half.
4659 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
4660 // Load the rest of the low bits.
4661 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
4662 N->getPointerInfo().getWithOffset(IncrementSize),
4663 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
4664 N->getBaseAlign(), MMOFlags, AAInfo);
4665
4666 // Build a factor node to remember that this load is independent of the
4667 // other one.
4668 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
4669 Hi.getValue(1));
4670
4671 if (ExcessBits < NVT.getSizeInBits()) {
4672 // Transfer low bits from the bottom of Hi to the top of Lo.
4673 Lo = DAG.getNode(
4674 ISD::OR, dl, NVT, Lo,
4675 DAG.getNode(ISD::SHL, dl, NVT, Hi,
4676 DAG.getShiftAmountConstant(ExcessBits, NVT, dl)));
4677 // Move high bits to the right position in Hi.
4678 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, NVT,
4679 Hi,
4680 DAG.getShiftAmountConstant(
4681 NVT.getSizeInBits() - ExcessBits, NVT, dl));
4682 }
4683 }
4684
4685 // Legalize the chain result - switch anything that used the old chain to
4686 // use the new one.
4687 ReplaceValueWith(SDValue(N, 1), Ch);
4688}
4689
4690void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
4691 SDValue &Lo, SDValue &Hi) {
4692 SDLoc dl(N);
4693 SDValue LL, LH, RL, RH;
4694 GetExpandedInteger(N->getOperand(0), LL, LH);
4695 GetExpandedInteger(N->getOperand(1), RL, RH);
4696
4697 SDNodeFlags Flags;
4698 if (N->getOpcode() == ISD::OR)
4699 Flags.setDisjoint(N->getFlags().hasDisjoint());
4700
4701 Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL, Flags);
4702 Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH, Flags);
4703}
4704
4705void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
4706 SDValue &Lo, SDValue &Hi) {
4707 EVT VT = N->getValueType(0);
4708 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4709 SDLoc dl(N);
4710
4711 SDValue LL, LH, RL, RH;
4712 GetExpandedInteger(N->getOperand(0), LL, LH);
4713 GetExpandedInteger(N->getOperand(1), RL, RH);
4714
4715 if (TLI.expandMUL(N, Lo, Hi, NVT, DAG,
4717 LL, LH, RL, RH))
4718 return;
4719
4720 // If nothing else, we can make a libcall.
4721 RTLIB::Libcall LC = RTLIB::getMUL(VT);
4722 RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(LC);
4723 if (LCImpl == RTLIB::Unsupported) {
4724 // Perform a wide multiplication where the wide type is the original VT and
4725 // the 4 parts are the split arguments.
4726 TLI.forceExpandMultiply(DAG, dl, /*Signed=*/false, Lo, Hi, LL, RL, LH, RH);
4727 return;
4728 }
4729
4730 // Note that we don't need to do a wide MUL here since we don't care about the
4731 // upper half of the result if it exceeds VT.
4732 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
4733 TargetLowering::MakeLibCallOptions CallOptions;
4734 CallOptions.setIsSigned(true);
4735 SplitInteger(TLI.makeLibCall(DAG, LCImpl, VT, Ops, CallOptions, dl).first, Lo,
4736 Hi);
4737}
4738
4739void DAGTypeLegalizer::ExpandIntRes_READCOUNTER(SDNode *N, SDValue &Lo,
4740 SDValue &Hi) {
4741 SDLoc DL(N);
4742 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4743 SDVTList VTs = DAG.getVTList(NVT, NVT, MVT::Other);
4744 SDValue R = DAG.getNode(N->getOpcode(), DL, VTs, N->getOperand(0));
4745 Lo = R.getValue(0);
4746 Hi = R.getValue(1);
4747 ReplaceValueWith(SDValue(N, 1), R.getValue(2));
4748}
4749
4750void DAGTypeLegalizer::ExpandIntRes_AVG(SDNode *N, SDValue &Lo, SDValue &Hi) {
4751 SDValue Result = TLI.expandAVG(N, DAG);
4752 SplitInteger(Result, Lo, Hi);
4753}
4754
4755void DAGTypeLegalizer::ExpandIntRes_ADDSUBSAT(SDNode *N, SDValue &Lo,
4756 SDValue &Hi) {
4757 SDValue Result = TLI.expandAddSubSat(N, DAG);
4758 SplitInteger(Result, Lo, Hi);
4759}
4760
4761void DAGTypeLegalizer::ExpandIntRes_SHLSAT(SDNode *N, SDValue &Lo,
4762 SDValue &Hi) {
4763 SDValue Result = TLI.expandShlSat(N, DAG);
4764 SplitInteger(Result, Lo, Hi);
4765}
4766
4767/// This performs an expansion of the integer result for a fixed point
4768/// multiplication. The default expansion performs rounding down towards
4769/// negative infinity, though targets that do care about rounding should specify
4770/// a target hook for rounding and provide their own expansion or lowering of
4771/// fixed point multiplication to be consistent with rounding.
4772void DAGTypeLegalizer::ExpandIntRes_MULFIX(SDNode *N, SDValue &Lo,
4773 SDValue &Hi) {
4774 SDLoc dl(N);
4775 EVT VT = N->getValueType(0);
4776 unsigned VTSize = VT.getScalarSizeInBits();
4777 SDValue LHS = N->getOperand(0);
4778 SDValue RHS = N->getOperand(1);
4779 uint64_t Scale = N->getConstantOperandVal(2);
4780 bool Saturating = (N->getOpcode() == ISD::SMULFIXSAT ||
4781 N->getOpcode() == ISD::UMULFIXSAT);
4782 bool Signed = (N->getOpcode() == ISD::SMULFIX ||
4783 N->getOpcode() == ISD::SMULFIXSAT);
4784
4785 // Handle special case when scale is equal to zero.
4786 if (!Scale) {
4788 if (!Saturating) {
4789 Result = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
4790 } else {
4791 EVT BoolVT = getSetCCResultType(VT);
4792 unsigned MulOp = Signed ? ISD::SMULO : ISD::UMULO;
4793 Result = DAG.getNode(MulOp, dl, DAG.getVTList(VT, BoolVT), LHS, RHS);
4794 SDValue Product = Result.getValue(0);
4795 SDValue Overflow = Result.getValue(1);
4796 if (Signed) {
4797 APInt MinVal = APInt::getSignedMinValue(VTSize);
4798 APInt MaxVal = APInt::getSignedMaxValue(VTSize);
4799 SDValue SatMin = DAG.getConstant(MinVal, dl, VT);
4800 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
4801 SDValue Zero = DAG.getConstant(0, dl, VT);
4802 // Xor the inputs, if resulting sign bit is 0 the product will be
4803 // positive, else negative.
4804 SDValue Xor = DAG.getNode(ISD::XOR, dl, VT, LHS, RHS);
4805 SDValue ProdNeg = DAG.getSetCC(dl, BoolVT, Xor, Zero, ISD::SETLT);
4806 Result = DAG.getSelect(dl, VT, ProdNeg, SatMin, SatMax);
4807 Result = DAG.getSelect(dl, VT, Overflow, Result, Product);
4808 } else {
4809 // For unsigned multiplication, we only need to check the max since we
4810 // can't really overflow towards zero.
4811 APInt MaxVal = APInt::getMaxValue(VTSize);
4812 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
4813 Result = DAG.getSelect(dl, VT, Overflow, SatMax, Product);
4814 }
4815 }
4816 SplitInteger(Result, Lo, Hi);
4817 return;
4818 }
4819
4820 // For SMULFIX[SAT] we only expect to find Scale<VTSize, but this assert will
4821 // cover for unhandled cases below, while still being valid for UMULFIX[SAT].
4822 assert(Scale <= VTSize && "Scale can't be larger than the value type size.");
4823
4824 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4825 SDValue LL, LH, RL, RH;
4826 GetExpandedInteger(LHS, LL, LH);
4827 GetExpandedInteger(RHS, RL, RH);
4829
4830 unsigned LoHiOp = Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI;
4831 if (!TLI.expandMUL_LOHI(LoHiOp, VT, dl, LHS, RHS, Result, NVT, DAG,
4833 LL, LH, RL, RH)) {
4834 Result.clear();
4835 Result.resize(4);
4836
4837 SDValue LoTmp, HiTmp;
4838 TLI.forceExpandWideMUL(DAG, dl, Signed, LHS, RHS, LoTmp, HiTmp);
4839 SplitInteger(LoTmp, Result[0], Result[1]);
4840 SplitInteger(HiTmp, Result[2], Result[3]);
4841 }
4842 assert(Result.size() == 4 && "Unexpected number of partlets in the result");
4843
4844 unsigned NVTSize = NVT.getScalarSizeInBits();
4845 assert((VTSize == NVTSize * 2) && "Expected the new value type to be half "
4846 "the size of the current value type");
4847
4848 // After getting the multiplication result in 4 parts, we need to perform a
4849 // shift right by the amount of the scale to get the result in that scale.
4850 //
4851 // Let's say we multiply 2 64 bit numbers. The resulting value can be held in
4852 // 128 bits that are cut into 4 32-bit parts:
4853 //
4854 // HH HL LH LL
4855 // |---32---|---32---|---32---|---32---|
4856 // 128 96 64 32 0
4857 //
4858 // |------VTSize-----|
4859 //
4860 // |NVTSize-|
4861 //
4862 // The resulting Lo and Hi would normally be in LL and LH after the shift. But
4863 // to avoid unneccessary shifting of all 4 parts, we can adjust the shift
4864 // amount and get Lo and Hi using two funnel shifts. Or for the special case
4865 // when Scale is a multiple of NVTSize we can just pick the result without
4866 // shifting.
4867 uint64_t Part0 = Scale / NVTSize; // Part holding lowest bit needed.
4868 if (Scale % NVTSize) {
4869 SDValue ShiftAmount = DAG.getShiftAmountConstant(Scale % NVTSize, NVT, dl);
4870 Lo = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 1], Result[Part0],
4871 ShiftAmount);
4872 Hi = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 2], Result[Part0 + 1],
4873 ShiftAmount);
4874 } else {
4875 Lo = Result[Part0];
4876 Hi = Result[Part0 + 1];
4877 }
4878
4879 // Unless saturation is requested we are done. The result is in <Hi,Lo>.
4880 if (!Saturating)
4881 return;
4882
4883 // Can not overflow when there is no integer part.
4884 if (Scale == VTSize)
4885 return;
4886
4887 // To handle saturation we must check for overflow in the multiplication.
4888 //
4889 // Unsigned overflow happened if the upper (VTSize - Scale) bits (of Result)
4890 // aren't all zeroes.
4891 //
4892 // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of Result)
4893 // aren't all ones or all zeroes.
4894 //
4895 // We cannot overflow past HH when multiplying 2 ints of size VTSize, so the
4896 // highest bit of HH determines saturation direction in the event of signed
4897 // saturation.
4898
4899 SDValue ResultHL = Result[2];
4900 SDValue ResultHH = Result[3];
4901
4902 SDValue SatMax, SatMin;
4903 SDValue NVTZero = DAG.getConstant(0, dl, NVT);
4904 SDValue NVTNeg1 = DAG.getAllOnesConstant(dl, NVT);
4905 EVT BoolNVT = getSetCCResultType(NVT);
4906
4907 if (!Signed) {
4908 if (Scale < NVTSize) {
4909 // Overflow happened if ((HH | (HL >> Scale)) != 0).
4910 SDValue HLAdjusted =
4911 DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
4912 DAG.getShiftAmountConstant(Scale, NVT, dl));
4913 SDValue Tmp = DAG.getNode(ISD::OR, dl, NVT, HLAdjusted, ResultHH);
4914 SatMax = DAG.getSetCC(dl, BoolNVT, Tmp, NVTZero, ISD::SETNE);
4915 } else if (Scale == NVTSize) {
4916 // Overflow happened if (HH != 0).
4917 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETNE);
4918 } else if (Scale < VTSize) {
4919 // Overflow happened if ((HH >> (Scale - NVTSize)) != 0).
4920 SDValue HLAdjusted =
4921 DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
4922 DAG.getShiftAmountConstant(Scale - NVTSize, NVT, dl));
4923 SatMax = DAG.getSetCC(dl, BoolNVT, HLAdjusted, NVTZero, ISD::SETNE);
4924 } else
4925 llvm_unreachable("Scale must be less or equal to VTSize for UMULFIXSAT"
4926 "(and saturation can't happen with Scale==VTSize).");
4927
4928 Hi = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Hi);
4929 Lo = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Lo);
4930 return;
4931 }
4932
4933 if (Scale < NVTSize) {
4934 // The number of overflow bits we can check are VTSize - Scale + 1 (we
4935 // include the sign bit). If these top bits are > 0, then we overflowed past
4936 // the max value. If these top bits are < -1, then we overflowed past the
4937 // min value. Otherwise, we did not overflow.
4938 unsigned OverflowBits = VTSize - Scale + 1;
4939 assert(OverflowBits <= VTSize && OverflowBits > NVTSize &&
4940 "Extent of overflow bits must start within HL");
4941 SDValue HLHiMask = DAG.getConstant(
4942 APInt::getHighBitsSet(NVTSize, OverflowBits - NVTSize), dl, NVT);
4943 SDValue HLLoMask = DAG.getConstant(
4944 APInt::getLowBitsSet(NVTSize, VTSize - OverflowBits), dl, NVT);
4945 // We overflow max if HH > 0 or (HH == 0 && HL > HLLoMask).
4946 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
4947 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
4948 SDValue HLUGT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLLoMask, ISD::SETUGT);
4949 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
4950 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLUGT));
4951 // We overflow min if HH < -1 or (HH == -1 && HL < HLHiMask).
4952 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
4953 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
4954 SDValue HLULT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLHiMask, ISD::SETULT);
4955 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
4956 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLULT));
4957 } else if (Scale == NVTSize) {
4958 // We overflow max if HH > 0 or (HH == 0 && HL sign bit is 1).
4959 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
4960 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
4961 SDValue HLNeg = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETLT);
4962 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
4963 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLNeg));
4964 // We overflow min if HH < -1 or (HH == -1 && HL sign bit is 0).
4965 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
4966 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
4967 SDValue HLPos = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETGE);
4968 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
4969 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLPos));
4970 } else if (Scale < VTSize) {
4971 // This is similar to the case when we saturate if Scale < NVTSize, but we
4972 // only need to check HH.
4973 unsigned OverflowBits = VTSize - Scale + 1;
4974 SDValue HHHiMask = DAG.getConstant(
4975 APInt::getHighBitsSet(NVTSize, OverflowBits), dl, NVT);
4976 SDValue HHLoMask = DAG.getConstant(
4977 APInt::getLowBitsSet(NVTSize, NVTSize - OverflowBits), dl, NVT);
4978 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, HHLoMask, ISD::SETGT);
4979 SatMin = DAG.getSetCC(dl, BoolNVT, ResultHH, HHHiMask, ISD::SETLT);
4980 } else
4981 llvm_unreachable("Illegal scale for signed fixed point mul.");
4982
4983 // Saturate to signed maximum.
4984 APInt MaxHi = APInt::getSignedMaxValue(NVTSize);
4985 APInt MaxLo = APInt::getAllOnes(NVTSize);
4986 Hi = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxHi, dl, NVT), Hi);
4987 Lo = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxLo, dl, NVT), Lo);
4988 // Saturate to signed minimum.
4989 APInt MinHi = APInt::getSignedMinValue(NVTSize);
4990 Hi = DAG.getSelect(dl, NVT, SatMin, DAG.getConstant(MinHi, dl, NVT), Hi);
4991 Lo = DAG.getSelect(dl, NVT, SatMin, NVTZero, Lo);
4992}
4993
4994void DAGTypeLegalizer::ExpandIntRes_DIVFIX(SDNode *N, SDValue &Lo,
4995 SDValue &Hi) {
4996 SDLoc dl(N);
4997 // Try expanding in the existing type first.
4998 SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, N->getOperand(0),
4999 N->getOperand(1),
5000 N->getConstantOperandVal(2), DAG);
5001
5002 if (!Res)
5003 Res = earlyExpandDIVFIX(N, N->getOperand(0), N->getOperand(1),
5004 N->getConstantOperandVal(2), TLI, DAG);
5005 SplitInteger(Res, Lo, Hi);
5006}
5007
5008void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
5009 SDValue &Lo, SDValue &Hi) {
5010 assert((Node->getOpcode() == ISD::SADDO || Node->getOpcode() == ISD::SSUBO) &&
5011 "Node has unexpected Opcode");
5012 SDValue LHS = Node->getOperand(0);
5013 SDValue RHS = Node->getOperand(1);
5014 SDLoc dl(Node);
5015
5016 SDValue Ovf;
5017
5018 bool IsAdd = Node->getOpcode() == ISD::SADDO;
5019 unsigned CarryOp = IsAdd ? ISD::SADDO_CARRY : ISD::SSUBO_CARRY;
5020
5021 bool HasCarryOp = TLI.isOperationLegalOrCustom(
5022 CarryOp, TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
5023
5024 if (HasCarryOp) {
5025 // Expand the subcomponents.
5026 SDValue LHSL, LHSH, RHSL, RHSH;
5027 GetExpandedInteger(LHS, LHSL, LHSH);
5028 GetExpandedInteger(RHS, RHSL, RHSH);
5029 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), Node->getValueType(1));
5030
5031 Lo = DAG.getNode(IsAdd ? ISD::UADDO : ISD::USUBO, dl, VTList, {LHSL, RHSL});
5032 Hi = DAG.getNode(CarryOp, dl, VTList, { LHSH, RHSH, Lo.getValue(1) });
5033
5034 Ovf = Hi.getValue(1);
5035 } else {
5036 // Expand the result by simply replacing it with the equivalent
5037 // non-overflow-checking operation.
5038 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
5039 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
5040 LHS, RHS);
5041 SplitInteger(Sum, Lo, Hi);
5042
5043 // Compute the overflow.
5044 //
5045 // LHSSign -> LHS < 0
5046 // RHSSign -> RHS < 0
5047 // SumSign -> Sum < 0
5048 //
5049 // Add:
5050 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
5051 // Sub:
5052 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
5053 //
5054 // To get better codegen we can rewrite this by doing bitwise math on
5055 // the integers and extract the final sign bit at the end. So the
5056 // above becomes:
5057 //
5058 // Add:
5059 // Overflow -> (~(LHS ^ RHS) & (LHS ^ Sum)) < 0
5060 // Sub:
5061 // Overflow -> ((LHS ^ RHS) & (LHS ^ Sum)) < 0
5062 //
5063 // NOTE: This is different than the expansion we do in expandSADDSUBO
5064 // because it is more costly to implement the same overflow predicate with
5065 // SETCC nodes when the integers are split.
5066 EVT VT = LHS.getValueType();
5067 SDValue SignsMatch = DAG.getNode(ISD::XOR, dl, VT, LHS, RHS);
5068 if (IsAdd)
5069 SignsMatch = DAG.getNOT(dl, SignsMatch, VT);
5070
5071 SDValue SumSignNE = DAG.getNode(ISD::XOR, dl, VT, LHS, Sum);
5072 Ovf = DAG.getNode(ISD::AND, dl, VT, SignsMatch, SumSignNE);
5073 EVT OType = Node->getValueType(1);
5074 Ovf = DAG.getSetCC(dl, OType, Ovf, DAG.getConstant(0, dl, VT), ISD::SETLT);
5075 }
5076
5077 // Use the calculated overflow everywhere.
5078 ReplaceValueWith(SDValue(Node, 1), Ovf);
5079}
5080
5081void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
5082 SDValue &Lo, SDValue &Hi) {
5083 EVT VT = N->getValueType(0);
5084 SDLoc dl(N);
5085 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
5086
5087 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
5088 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
5089 SplitInteger(Res.getValue(0), Lo, Hi);
5090 return;
5091 }
5092
5093 RTLIB::Libcall LC = RTLIB::getSDIV(VT);
5094 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
5095
5096 TargetLowering::MakeLibCallOptions CallOptions;
5097 CallOptions.setIsSigned(true);
5098 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
5099}
5100
5101void DAGTypeLegalizer::ExpandIntRes_ShiftThroughStack(SDNode *N, SDValue &Lo,
5102 SDValue &Hi) {
5103 SDLoc dl(N);
5104 SDValue Shiftee = N->getOperand(0);
5105 EVT VT = Shiftee.getValueType();
5106 SDValue ShAmt = N->getOperand(1);
5107 EVT ShAmtVT = ShAmt.getValueType();
5108
5109 EVT LoadVT = VT;
5110 do {
5111 LoadVT = TLI.getTypeToTransformTo(*DAG.getContext(), LoadVT);
5112 } while (!TLI.isTypeLegal(LoadVT));
5113
5114 const unsigned ShiftUnitInBits = LoadVT.getStoreSizeInBits();
5115 assert(ShiftUnitInBits <= VT.getScalarSizeInBits());
5116 assert(isPowerOf2_32(ShiftUnitInBits) &&
5117 "Shifting unit is not a a power of two!");
5118
5119 const bool IsOneStepShift =
5120 DAG.computeKnownBits(ShAmt).countMinTrailingZeros() >=
5121 Log2_32(ShiftUnitInBits);
5122
5123 // If we can't do it as one step, we'll have two uses of shift amount,
5124 // and thus must freeze it.
5125 if (!IsOneStepShift)
5126 ShAmt = DAG.getFreeze(ShAmt);
5127
5128 unsigned VTBitWidth = VT.getScalarSizeInBits();
5129 assert(VTBitWidth % 8 == 0 && "Shifting a not byte multiple value?");
5130 unsigned VTByteWidth = VTBitWidth / 8;
5131 assert(isPowerOf2_32(VTByteWidth) &&
5132 "Shiftee type size is not a power of two!");
5133 unsigned StackSlotByteWidth = 2 * VTByteWidth;
5134 unsigned StackSlotBitWidth = 8 * StackSlotByteWidth;
5135 EVT StackSlotVT = EVT::getIntegerVT(*DAG.getContext(), StackSlotBitWidth);
5136
5137 // Get a temporary stack slot 2x the width of our VT.
5138 // FIXME: reuse stack slots?
5139 Align StackAlign = DAG.getReducedAlign(StackSlotVT, /*UseABI=*/false);
5141 DAG.CreateStackTemporary(StackSlotVT.getStoreSize(), StackAlign);
5142 EVT PtrTy = StackPtr.getValueType();
5143 SDValue Ch = DAG.getEntryNode();
5144
5145 MachinePointerInfo StackPtrInfo = MachinePointerInfo::getFixedStack(
5146 DAG.getMachineFunction(),
5147 cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex());
5148
5149 // Extend the value, that is being shifted, to the entire stack slot's width.
5150 SDValue Init;
5151 if (N->getOpcode() != ISD::SHL) {
5152 unsigned WideningOpc =
5153 N->getOpcode() == ISD::SRA ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
5154 Init = DAG.getNode(WideningOpc, dl, StackSlotVT, Shiftee);
5155 } else {
5156 // For left-shifts, pad the Shiftee's LSB with zeros to twice it's width.
5157 SDValue AllZeros = DAG.getConstant(0, dl, VT);
5158 Init = DAG.getNode(ISD::BUILD_PAIR, dl, StackSlotVT, AllZeros, Shiftee);
5159 }
5160 // And spill it into the stack slot.
5161 Ch = DAG.getStore(Ch, dl, Init, StackPtr, StackPtrInfo, StackAlign);
5162
5163 // Now, compute the full-byte offset into stack slot from where we can load.
5164 // We have shift amount, which is in bits. Offset should point to an aligned
5165 // address.
5166 SDNodeFlags Flags;
5167 Flags.setExact(IsOneStepShift);
5168 SDValue SrlTmp = DAG.getNode(
5169 ISD::SRL, dl, ShAmtVT, ShAmt,
5170 DAG.getConstant(Log2_32(ShiftUnitInBits), dl, ShAmtVT), Flags);
5171 SDValue BitOffset =
5172 DAG.getNode(ISD::SHL, dl, ShAmtVT, SrlTmp,
5173 DAG.getConstant(Log2_32(ShiftUnitInBits), dl, ShAmtVT));
5174
5175 SDValue ByteOffset =
5176 DAG.getNode(ISD::SRL, dl, ShAmtVT, BitOffset,
5177 DAG.getConstant(3, dl, ShAmtVT), SDNodeFlags::Exact);
5178 // And clamp it, because OOB load is an immediate UB,
5179 // while shift overflow would have *just* been poison.
5180 ByteOffset = DAG.getNode(ISD::AND, dl, ShAmtVT, ByteOffset,
5181 DAG.getConstant(VTByteWidth - 1, dl, ShAmtVT));
5182 // We have exactly two strategies on indexing into stack slot here:
5183 // 1. upwards starting from the beginning of the slot
5184 // 2. downwards starting from the middle of the slot
5185 // On little-endian machine, we pick 1. for right shifts and 2. for left-shift
5186 // and vice versa on big-endian machine.
5187 bool WillIndexUpwards = N->getOpcode() != ISD::SHL;
5188 if (DAG.getDataLayout().isBigEndian())
5189 WillIndexUpwards = !WillIndexUpwards;
5190
5191 SDValue AdjStackPtr;
5192 if (WillIndexUpwards) {
5193 AdjStackPtr = StackPtr;
5194 } else {
5195 AdjStackPtr = DAG.getMemBasePlusOffset(
5196 StackPtr, DAG.getConstant(VTByteWidth, dl, PtrTy), dl);
5197 ByteOffset = DAG.getNegative(ByteOffset, dl, ShAmtVT);
5198 }
5199
5200 // Get the pointer somewhere into the stack slot from which we need to load.
5201 ByteOffset = DAG.getSExtOrTrunc(ByteOffset, dl, PtrTy);
5202 AdjStackPtr = DAG.getMemBasePlusOffset(AdjStackPtr, ByteOffset, dl);
5203
5204 // And load it! While the load is not legal, legalizing it is obvious.
5205 SDValue Res =
5206 DAG.getLoad(VT, dl, Ch, AdjStackPtr,
5207 MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
5208 commonAlignment(StackAlign, LoadVT.getStoreSize()));
5209
5210 // If we may still have a remaining bits to shift by, do so now.
5211 if (!IsOneStepShift) {
5212 SDValue ShAmtRem =
5213 DAG.getNode(ISD::AND, dl, ShAmtVT, ShAmt,
5214 DAG.getConstant(ShiftUnitInBits - 1, dl, ShAmtVT));
5215 Res = DAG.getNode(N->getOpcode(), dl, VT, Res, ShAmtRem);
5216 }
5217
5218 // Finally, split the computed value.
5219 SplitInteger(Res, Lo, Hi);
5220}
5221
5222void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
5223 SDValue &Lo, SDValue &Hi) {
5224 EVT VT = N->getValueType(0);
5225 unsigned Opc = N->getOpcode();
5226 SDLoc dl(N);
5227
5228 // If we can emit an efficient shift operation, do so now. Check to see if
5229 // the RHS is a constant.
5230 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
5231 return ExpandShiftByConstant(N, CN->getAPIntValue(), Lo, Hi);
5232
5233 // If we can determine that the high bit of the shift is zero or one, even if
5234 // the low bits are variable, emit this shift in an optimized form.
5235 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
5236 return;
5237
5238 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
5239 unsigned PartsOpc;
5240 if (Opc == ISD::SHL) {
5241 PartsOpc = ISD::SHL_PARTS;
5242 } else if (Opc == ISD::SRL) {
5243 PartsOpc = ISD::SRL_PARTS;
5244 } else {
5245 assert(Opc == ISD::SRA && "Unknown shift!");
5246 PartsOpc = ISD::SRA_PARTS;
5247 }
5248
5249 // Next check to see if the target supports this SHL_PARTS operation or if it
5250 // will custom expand it. Don't lower this to SHL_PARTS when we optimise for
5251 // size, but create a libcall instead.
5252 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
5253 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
5254 const bool LegalOrCustom =
5255 (Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5256 Action == TargetLowering::Custom;
5257
5258 unsigned ExpansionFactor = 1;
5259 // That VT->NVT expansion is one step. But will we re-expand NVT?
5260 for (EVT TmpVT = NVT;;) {
5261 EVT NewTMPVT = TLI.getTypeToTransformTo(*DAG.getContext(), TmpVT);
5262 if (NewTMPVT == TmpVT)
5263 break;
5264 TmpVT = NewTMPVT;
5265 ++ExpansionFactor;
5266 }
5267
5269 TLI.preferredShiftLegalizationStrategy(DAG, N, ExpansionFactor);
5270
5272 return ExpandIntRes_ShiftThroughStack(N, Lo, Hi);
5273
5274 if (LegalOrCustom &&
5276 // Expand the subcomponents.
5277 SDValue LHSL, LHSH;
5278 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
5279 EVT VT = LHSL.getValueType();
5280
5281 // If the shift amount operand is coming from a vector legalization it may
5282 // have an illegal type. Fix that first by casting the operand, otherwise
5283 // the new SHL_PARTS operation would need further legalization.
5284 SDValue ShiftOp = N->getOperand(1);
5285 EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
5286 if (ShiftOp.getValueType() != ShiftTy)
5287 ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy);
5288
5289 SDValue Ops[] = { LHSL, LHSH, ShiftOp };
5290 Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops);
5291 Hi = Lo.getValue(1);
5292 return;
5293 }
5294
5295 // Otherwise, emit a libcall.
5296 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
5297 bool isSigned;
5298 if (Opc == ISD::SHL) {
5299 isSigned = false; /*sign irrelevant*/
5300 LC = RTLIB::getSHL(VT);
5301 } else if (Opc == ISD::SRL) {
5302 isSigned = false;
5303 LC = RTLIB::getSRL(VT);
5304 } else {
5305 assert(Opc == ISD::SRA && "Unknown shift!");
5306 isSigned = true;
5307 LC = RTLIB::getSRA(VT);
5308 }
5309
5310 if (RTLIB::LibcallImpl LibcallImpl = DAG.getLibcalls().getLibcallImpl(LC)) {
5311 EVT ShAmtTy =
5312 EVT::getIntegerVT(*DAG.getContext(), DAG.getLibInfo().getIntSize());
5313 SDValue ShAmt = DAG.getZExtOrTrunc(N->getOperand(1), dl, ShAmtTy);
5314 SDValue Ops[2] = {N->getOperand(0), ShAmt};
5315 TargetLowering::MakeLibCallOptions CallOptions;
5316 CallOptions.setIsSigned(isSigned);
5317 SplitInteger(
5318 TLI.makeLibCall(DAG, LibcallImpl, VT, Ops, CallOptions, dl).first, Lo,
5319 Hi);
5320 return;
5321 }
5322
5323 if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
5324 llvm_unreachable("Unsupported shift!");
5325}
5326
5327void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
5328 SDValue &Lo, SDValue &Hi) {
5329 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5330 SDLoc dl(N);
5331 SDValue Op = N->getOperand(0);
5332 if (Op.getValueType().bitsLE(NVT)) {
5333 // The low part is sign extension of the input (degenerates to a copy).
5334 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
5335 // The high part is obtained by SRA'ing all but one of the bits of low part.
5336 unsigned LoSize = NVT.getSizeInBits();
5337 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
5338 DAG.getShiftAmountConstant(LoSize - 1, NVT, dl));
5339 } else {
5340 // For example, extension of an i48 to an i64. The operand type necessarily
5341 // promotes to the result type, so will end up being expanded too.
5342 assert(getTypeAction(Op.getValueType()) ==
5344 "Only know how to promote this result!");
5345 SDValue Res = GetPromotedInteger(Op);
5346 assert(Res.getValueType() == N->getValueType(0) &&
5347 "Operand over promoted?");
5348 // Split the promoted operand. This will simplify when it is expanded.
5349 SplitInteger(Res, Lo, Hi);
5350 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
5351 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
5352 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
5353 ExcessBits)));
5354 }
5355}
5356
5357void DAGTypeLegalizer::
5358ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
5359 SDLoc dl(N);
5360 GetExpandedInteger(N->getOperand(0), Lo, Hi);
5361 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
5362
5363 if (EVT.bitsLE(Lo.getValueType())) {
5364 // sext_inreg the low part if needed.
5365 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
5366 N->getOperand(1));
5367
5368 // The high part gets the sign extension from the lo-part. This handles
5369 // things like sextinreg V:i64 from i8.
5370 Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
5371 DAG.getShiftAmountConstant(Hi.getValueSizeInBits() - 1,
5372 Hi.getValueType(), dl));
5373 } else {
5374 // For example, extension of an i48 to an i64. Leave the low part alone,
5375 // sext_inreg the high part.
5376 unsigned ExcessBits = EVT.getSizeInBits() - Lo.getValueSizeInBits();
5377 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
5378 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
5379 ExcessBits)));
5380 }
5381}
5382
5383void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
5384 SDValue &Lo, SDValue &Hi) {
5385 EVT VT = N->getValueType(0);
5386 SDLoc dl(N);
5387 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
5388
5389 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
5390 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
5391 SplitInteger(Res.getValue(1), Lo, Hi);
5392 return;
5393 }
5394
5395 RTLIB::Libcall LC = RTLIB::getSREM(VT);
5396 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
5397
5398 TargetLowering::MakeLibCallOptions CallOptions;
5399 CallOptions.setIsSigned(true);
5400 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
5401}
5402
5403void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
5404 SDValue &Lo, SDValue &Hi) {
5405 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5406 SDValue InOp = N->getOperand(0);
5407 EVT InVT = InOp.getValueType();
5408 SDLoc dl(N);
5409 Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, InOp);
5410 Hi = DAG.getNode(ISD::SRL, dl, InVT, InOp,
5411 DAG.getShiftAmountConstant(NVT.getSizeInBits(), InVT, dl));
5412 Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
5413}
5414
5415void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N,
5416 SDValue &Lo, SDValue &Hi) {
5417 EVT VT = N->getValueType(0);
5418 SDLoc dl(N);
5419
5420 if (N->getOpcode() == ISD::UMULO) {
5421 // This section expands the operation into the following sequence of
5422 // instructions. `iNh` here refers to a type which has half the bit width of
5423 // the type the original operation operated on.
5424 //
5425 // %0 = %LHS.HI != 0 && %RHS.HI != 0
5426 // %1 = { iNh, i1 } @umul.with.overflow.iNh(iNh %LHS.HI, iNh %RHS.LO)
5427 // %2 = { iNh, i1 } @umul.with.overflow.iNh(iNh %RHS.HI, iNh %LHS.LO)
5428 // %3 = mul nuw iN (%LHS.LOW as iN), (%RHS.LOW as iN)
5429 // %4 = add iNh %1.0, %2.0 as iN
5430 // %5 = { iNh, i1 } @uadd.with.overflow.iNh(iNh %4, iNh %3.HIGH)
5431 //
5432 // %lo = %3.LO
5433 // %hi = %5.0
5434 // %ovf = %0 || %1.1 || %2.1 || %5.1
5435 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
5436 SDValue LHSHigh, LHSLow, RHSHigh, RHSLow;
5437 GetExpandedInteger(LHS, LHSLow, LHSHigh);
5438 GetExpandedInteger(RHS, RHSLow, RHSHigh);
5439 EVT HalfVT = LHSLow.getValueType();
5440 EVT BitVT = N->getValueType(1);
5441 SDVTList VTHalfWithO = DAG.getVTList(HalfVT, BitVT);
5442
5443 SDValue HalfZero = DAG.getConstant(0, dl, HalfVT);
5444 SDValue Overflow = DAG.getNode(ISD::AND, dl, BitVT,
5445 DAG.getSetCC(dl, BitVT, LHSHigh, HalfZero, ISD::SETNE),
5446 DAG.getSetCC(dl, BitVT, RHSHigh, HalfZero, ISD::SETNE));
5447
5448 SDValue One = DAG.getNode(ISD::UMULO, dl, VTHalfWithO, LHSHigh, RHSLow);
5449 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, One.getValue(1));
5450
5451 SDValue Two = DAG.getNode(ISD::UMULO, dl, VTHalfWithO, RHSHigh, LHSLow);
5452 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Two.getValue(1));
5453
5454 SDValue HighSum = DAG.getNode(ISD::ADD, dl, HalfVT, One, Two);
5455
5456 // Cannot use `UMUL_LOHI` directly, because some 32-bit targets (ARM) do not
5457 // know how to expand `i64,i64 = umul_lohi a, b` and abort (why isn’t this
5458 // operation recursively legalized?).
5459 //
5460 // Many backends understand this pattern and will convert into LOHI
5461 // themselves, if applicable.
5462 SDValue Three = DAG.getNode(ISD::MUL, dl, VT,
5463 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LHSLow),
5464 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RHSLow));
5465 SplitInteger(Three, Lo, Hi);
5466
5467 Hi = DAG.getNode(ISD::UADDO, dl, VTHalfWithO, Hi, HighSum);
5468 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Hi.getValue(1));
5469 ReplaceValueWith(SDValue(N, 1), Overflow);
5470 return;
5471 }
5472
5473 Type *RetTy = VT.getTypeForEVT(*DAG.getContext());
5474 EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
5475 Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext());
5476
5477 // Replace this with a libcall that will check overflow.
5478 RTLIB::Libcall LC = RTLIB::getMULO(VT);
5479 RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(LC);
5480
5481 // If we don't have the libcall or if the function we are compiling is the
5482 // implementation of the expected libcall (avoid inf-loop), expand inline.
5483 if (LCImpl == RTLIB::Unsupported ||
5485 DAG.getMachineFunction().getName()) {
5486 // FIXME: This is not an optimal expansion, but better than crashing.
5487 SDValue MulLo, MulHi;
5488 TLI.forceExpandWideMUL(DAG, dl, /*Signed=*/true, N->getOperand(0),
5489 N->getOperand(1), MulLo, MulHi);
5490 SDValue SRA = DAG.getNode(
5491 ISD::SRA, dl, VT, MulLo,
5492 DAG.getShiftAmountConstant(VT.getScalarSizeInBits() - 1, VT, dl));
5493 SDValue Overflow =
5494 DAG.getSetCC(dl, N->getValueType(1), MulHi, SRA, ISD::SETNE);
5495 SplitInteger(MulLo, Lo, Hi);
5496 ReplaceValueWith(SDValue(N, 1), Overflow);
5497 return;
5498 }
5499
5500 SDValue Temp = DAG.CreateStackTemporary(PtrVT);
5501 // Temporary for the overflow value, default it to zero.
5502 SDValue Chain =
5503 DAG.getStore(DAG.getEntryNode(), dl, DAG.getConstant(0, dl, PtrVT), Temp,
5504 MachinePointerInfo());
5505
5507 for (const SDValue &Op : N->op_values()) {
5508 EVT ArgVT = Op.getValueType();
5509 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
5510 TargetLowering::ArgListEntry Entry(Op, ArgTy);
5511 Entry.IsSExt = true;
5512 Entry.IsZExt = false;
5513 Args.push_back(Entry);
5514 }
5515
5516 // Also pass the address of the overflow check.
5517 TargetLowering::ArgListEntry Entry(
5518 Temp, PointerType::getUnqual(PtrTy->getContext()));
5519 Entry.IsSExt = true;
5520 Entry.IsZExt = false;
5521 Args.push_back(Entry);
5522
5523 SDValue Func = DAG.getExternalSymbol(LCImpl, PtrVT);
5524
5525 TargetLowering::CallLoweringInfo CLI(DAG);
5526 CLI.setDebugLoc(dl)
5527 .setChain(Chain)
5528 .setLibCallee(DAG.getLibcalls().getLibcallImplCallingConv(LCImpl), RetTy,
5529 Func, std::move(Args))
5530 .setSExtResult();
5531
5532 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
5533
5534 SplitInteger(CallInfo.first, Lo, Hi);
5535 SDValue Temp2 =
5536 DAG.getLoad(PtrVT, dl, CallInfo.second, Temp, MachinePointerInfo());
5537 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2,
5538 DAG.getConstant(0, dl, PtrVT),
5539 ISD::SETNE);
5540 // Use the overflow from the libcall everywhere.
5541 ReplaceValueWith(SDValue(N, 1), Ofl);
5542}
5543
5544void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
5545 SDValue &Lo, SDValue &Hi) {
5546 EVT VT = N->getValueType(0);
5547 SDLoc dl(N);
5548 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
5549
5550 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
5551 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
5552 SplitInteger(Res.getValue(0), Lo, Hi);
5553 return;
5554 }
5555
5556 // Try to expand UDIV by constant.
5557 if (isa<ConstantSDNode>(N->getOperand(1))) {
5558 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5559 // Only if the new type is legal.
5560 if (isTypeLegal(NVT)) {
5561 SDValue InL, InH;
5562 GetExpandedInteger(N->getOperand(0), InL, InH);
5564 if (TLI.expandDIVREMByConstant(N, Result, NVT, DAG, InL, InH)) {
5565 Lo = Result[0];
5566 Hi = Result[1];
5567 return;
5568 }
5569 }
5570 }
5571
5572 RTLIB::Libcall LC = RTLIB::getUDIV(VT);
5573 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
5574
5575 TargetLowering::MakeLibCallOptions CallOptions;
5576 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
5577}
5578
5579void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
5580 SDValue &Lo, SDValue &Hi) {
5581 EVT VT = N->getValueType(0);
5582 SDLoc dl(N);
5583 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
5584
5585 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
5586 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
5587 SplitInteger(Res.getValue(1), Lo, Hi);
5588 return;
5589 }
5590
5591 // Try to expand UREM by constant.
5592 if (isa<ConstantSDNode>(N->getOperand(1))) {
5593 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5594 // Only if the new type is legal.
5595 if (isTypeLegal(NVT)) {
5596 SDValue InL, InH;
5597 GetExpandedInteger(N->getOperand(0), InL, InH);
5599 if (TLI.expandDIVREMByConstant(N, Result, NVT, DAG, InL, InH)) {
5600 Lo = Result[0];
5601 Hi = Result[1];
5602 return;
5603 }
5604 }
5605 }
5606
5607 RTLIB::Libcall LC = RTLIB::getUREM(VT);
5608 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
5609
5610 TargetLowering::MakeLibCallOptions CallOptions;
5611 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
5612}
5613
5614void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
5615 SDValue &Lo, SDValue &Hi) {
5616 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5617 SDLoc dl(N);
5618 SDValue Op = N->getOperand(0);
5619 if (Op.getValueType().bitsLE(NVT)) {
5620 // The low part is zero extension of the input (degenerates to a copy).
5621 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
5622 Hi = DAG.getConstant(0, dl, NVT); // The high part is just a zero.
5623 } else {
5624 // For example, extension of an i48 to an i64. The operand type necessarily
5625 // promotes to the result type, so will end up being expanded too.
5626 assert(getTypeAction(Op.getValueType()) ==
5628 "Only know how to promote this result!");
5629 SDValue Res = GetPromotedInteger(Op);
5630 assert(Res.getValueType() == N->getValueType(0) &&
5631 "Operand over promoted?");
5632 // Split the promoted operand. This will simplify when it is expanded.
5633 SplitInteger(Res, Lo, Hi);
5634 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
5635 Hi = DAG.getZeroExtendInReg(Hi, dl,
5636 EVT::getIntegerVT(*DAG.getContext(),
5637 ExcessBits));
5638 }
5639}
5640
5641void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N,
5642 SDValue &Lo, SDValue &Hi) {
5643 SDLoc dl(N);
5644 EVT VT = cast<AtomicSDNode>(N)->getMemoryVT();
5645 SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
5646 SDValue Zero = DAG.getConstant(0, dl, VT);
5647 SDValue Swap = DAG.getAtomicCmpSwap(
5649 cast<AtomicSDNode>(N)->getMemoryVT(), VTs, N->getOperand(0),
5650 N->getOperand(1), Zero, Zero, cast<AtomicSDNode>(N)->getMemOperand());
5651
5652 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
5653 ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
5654}
5655
5656void DAGTypeLegalizer::ExpandIntRes_VECREDUCE(SDNode *N,
5657 SDValue &Lo, SDValue &Hi) {
5658 // TODO For VECREDUCE_(AND|OR|XOR) we could split the vector and calculate
5659 // both halves independently.
5660 SDValue Res = TLI.expandVecReduce(N, DAG);
5661 SplitInteger(Res, Lo, Hi);
5662}
5663
5664void DAGTypeLegalizer::ExpandIntRes_Rotate(SDNode *N,
5665 SDValue &Lo, SDValue &Hi) {
5666 // Delegate to funnel-shift expansion.
5667 SDLoc DL(N);
5668 unsigned Opcode = N->getOpcode() == ISD::ROTL ? ISD::FSHL : ISD::FSHR;
5669 SDValue Res = DAG.getNode(Opcode, DL, N->getValueType(0), N->getOperand(0),
5670 N->getOperand(0), N->getOperand(1));
5671 SplitInteger(Res, Lo, Hi);
5672}
5673
5674void DAGTypeLegalizer::ExpandIntRes_FunnelShift(SDNode *N, SDValue &Lo,
5675 SDValue &Hi) {
5676 // Values numbered from least significant to most significant.
5677 SDValue In1, In2, In3, In4;
5678 GetExpandedInteger(N->getOperand(0), In3, In4);
5679 GetExpandedInteger(N->getOperand(1), In1, In2);
5680 EVT HalfVT = In1.getValueType();
5681
5682 SDLoc DL(N);
5683 unsigned Opc = N->getOpcode();
5684 SDValue ShAmt = N->getOperand(2);
5685 EVT ShAmtVT = ShAmt.getValueType();
5686 EVT ShAmtCCVT = getSetCCResultType(ShAmtVT);
5687
5688 // If the shift amount is at least half the bitwidth, swap the inputs.
5689 unsigned HalfVTBits = HalfVT.getScalarSizeInBits();
5690 SDValue AndNode = DAG.getNode(ISD::AND, DL, ShAmtVT, ShAmt,
5691 DAG.getConstant(HalfVTBits, DL, ShAmtVT));
5692 SDValue Cond =
5693 DAG.getSetCC(DL, ShAmtCCVT, AndNode, DAG.getConstant(0, DL, ShAmtVT),
5695
5696 // Expand to a pair of funnel shifts.
5697 EVT NewShAmtVT = TLI.getShiftAmountTy(HalfVT, DAG.getDataLayout());
5698 SDValue NewShAmt = DAG.getAnyExtOrTrunc(ShAmt, DL, NewShAmtVT);
5699
5700 SDValue Select1 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In1, In2);
5701 SDValue Select2 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In2, In3);
5702 SDValue Select3 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In3, In4);
5703 Lo = DAG.getNode(Opc, DL, HalfVT, Select2, Select1, NewShAmt);
5704 Hi = DAG.getNode(Opc, DL, HalfVT, Select3, Select2, NewShAmt);
5705}
5706
5707void DAGTypeLegalizer::ExpandIntRes_CLMUL(SDNode *N, SDValue &Lo, SDValue &Hi) {
5708 if (N->getOpcode() != ISD::CLMUL) {
5709 SDValue Res = TLI.expandCLMUL(N, DAG);
5710 return SplitInteger(Res, Lo, Hi);
5711 }
5712
5713 SDValue LL, LH, RL, RH;
5714 GetExpandedInteger(N->getOperand(0), LL, LH);
5715 GetExpandedInteger(N->getOperand(1), RL, RH);
5716 EVT HalfVT = LL.getValueType();
5717 SDLoc DL(N);
5718
5719 // The low bits are a direct CLMUL of the the low bits.
5720 Lo = DAG.getNode(ISD::CLMUL, DL, HalfVT, LL, RL);
5721
5722 // We compute two Hi-Lo cross-products, XOR them, and XOR it with the overflow
5723 // of the CLMUL of the low bits (given by CLMULH of the low bits) to yield the
5724 // final high bits.
5725 SDValue LoH = DAG.getNode(ISD::CLMULH, DL, HalfVT, LL, RL);
5726 SDValue HiLoCross1 = DAG.getNode(ISD::CLMUL, DL, HalfVT, LL, RH);
5727 SDValue HiLoCross2 = DAG.getNode(ISD::CLMUL, DL, HalfVT, LH, RL);
5728 SDValue HiLoCross = DAG.getNode(ISD::XOR, DL, HalfVT, HiLoCross1, HiLoCross2);
5729 Hi = DAG.getNode(ISD::XOR, DL, HalfVT, LoH, HiLoCross);
5730}
5731
5732void DAGTypeLegalizer::ExpandIntRes_PEXT(SDNode *N, SDValue &Lo, SDValue &Hi) {
5733 SDValue Res = TLI.expandPEXT(N, DAG);
5734 SplitInteger(Res, Lo, Hi);
5735}
5736
5737void DAGTypeLegalizer::ExpandIntRes_PDEP(SDNode *N, SDValue &Lo, SDValue &Hi) {
5738 SDValue Res = TLI.expandPDEP(N, DAG);
5739 SplitInteger(Res, Lo, Hi);
5740}
5741
5742void DAGTypeLegalizer::ExpandIntRes_VSCALE(SDNode *N, SDValue &Lo,
5743 SDValue &Hi) {
5744 EVT VT = N->getValueType(0);
5745 EVT HalfVT =
5746 EVT::getIntegerVT(*DAG.getContext(), N->getValueSizeInBits(0) / 2);
5747 SDLoc dl(N);
5748
5749 // We assume VSCALE(1) fits into a legal integer.
5750 APInt One(HalfVT.getSizeInBits(), 1);
5751 SDValue VScaleBase = DAG.getVScale(dl, HalfVT, One);
5752 VScaleBase = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, VScaleBase);
5753 SDValue Res = DAG.getNode(ISD::MUL, dl, VT, VScaleBase, N->getOperand(0));
5754 SplitInteger(Res, Lo, Hi);
5755}
5756
5757void DAGTypeLegalizer::ExpandIntRes_READ_REGISTER(SDNode *N, SDValue &Lo,
5758 SDValue &Hi) {
5759 const Function &Fn = DAG.getMachineFunction().getFunction();
5760 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
5761 "cannot use llvm.read_register with illegal type", Fn, N->getDebugLoc()));
5762 ReplaceValueWith(SDValue(N, 1), N->getOperand(0));
5763 EVT LoVT, HiVT;
5764 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
5765 Lo = DAG.getPOISON(LoVT);
5766 Hi = DAG.getPOISON(HiVT);
5767}
5768
5769void DAGTypeLegalizer::ExpandIntRes_CTTZ_ELTS(SDNode *N, SDValue &Lo,
5770 SDValue &Hi) {
5771 // Assume that the maximum number of vector elements fits in getVectorIdxTy
5772 // and expand to that.
5773 EVT VT = N->getSimpleValueType(0);
5774 EVT IdxVT = TLI.getVectorIdxTy(DAG.getDataLayout());
5775 assert(IdxVT.bitsLT(VT) &&
5776 "VectorIdxTy should be smaller than type to be expanded?");
5777
5778 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N), IdxVT, N->getOperand(0));
5779 Res = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Res);
5780 SplitInteger(Res, Lo, Hi);
5781}
5782
5783//===----------------------------------------------------------------------===//
5784// Integer Operand Expansion
5785//===----------------------------------------------------------------------===//
5786
5787/// ExpandIntegerOperand - This method is called when the specified operand of
5788/// the specified node is found to need expansion. At this point, all of the
5789/// result types of the node are known to be legal, but other operands of the
5790/// node may need promotion or expansion as well as the specified one.
5791bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
5792 LLVM_DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG));
5793 SDValue Res = SDValue();
5794
5795 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
5796 return false;
5797
5798 switch (N->getOpcode()) {
5799 default:
5800 #ifndef NDEBUG
5801 dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
5802 N->dump(&DAG); dbgs() << "\n";
5803 #endif
5804 report_fatal_error("Do not know how to expand this operator's operand!");
5805
5806 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
5807 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
5808 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
5809 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
5810 case ISD::FAKE_USE:
5811 Res = ExpandOp_FAKE_USE(N);
5812 break;
5815 Res = TLI.expandLoopDependenceMask(N, DAG);
5816 break;
5817 case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
5818 case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
5819 case ISD::SPLAT_VECTOR: Res = ExpandIntOp_SPLAT_VECTOR(N); break;
5820 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
5821 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
5822 case ISD::SETCCCARRY: Res = ExpandIntOp_SETCCCARRY(N); break;
5824 case ISD::SINT_TO_FP:
5826 case ISD::UINT_TO_FP: Res = ExpandIntOp_XINT_TO_FP(N); break;
5827 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
5828 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
5829
5830 case ISD::SHL:
5831 case ISD::SRA:
5832 case ISD::SRL:
5833 case ISD::ROTL:
5834 case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
5835 case ISD::RETURNADDR:
5836 case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break;
5837
5838 case ISD::SCMP:
5839 case ISD::UCMP: Res = ExpandIntOp_CMP(N); break;
5840
5841 case ISD::ATOMIC_STORE: Res = ExpandIntOp_ATOMIC_STORE(N); break;
5842 case ISD::STACKMAP:
5843 Res = ExpandIntOp_STACKMAP(N, OpNo);
5844 break;
5845 case ISD::PATCHPOINT:
5846 Res = ExpandIntOp_PATCHPOINT(N, OpNo);
5847 break;
5848 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
5849 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
5850 Res = ExpandIntOp_VP_STRIDED(N, OpNo);
5851 break;
5853 Res = ExpandIntOp_WRITE_REGISTER(N, OpNo);
5854 break;
5855 }
5856
5857 // If the result is null, the sub-method took care of registering results etc.
5858 if (!Res.getNode()) return false;
5859
5860 // If the result is N, the sub-method updated N in place. Tell the legalizer
5861 // core about this.
5862 if (Res.getNode() == N)
5863 return true;
5864
5865 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
5866 "Invalid operand expansion");
5867
5868 ReplaceValueWith(SDValue(N, 0), Res);
5869 return false;
5870}
5871
5872/// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
5873/// is shared among BR_CC, SELECT_CC, and SETCC handlers.
5874void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
5875 SDValue &NewRHS,
5876 ISD::CondCode &CCCode,
5877 const SDLoc &dl) {
5878 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
5879 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
5880 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
5881
5882 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
5883 if (RHSLo == RHSHi && isAllOnesConstant(RHSLo)) {
5884 // Equality comparison to -1.
5885 NewLHS = DAG.getNode(ISD::AND, dl, LHSLo.getValueType(), LHSLo, LHSHi);
5886 NewRHS = RHSLo;
5887 return;
5888 }
5889
5890 NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
5891 NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
5892 NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
5893 NewRHS = DAG.getConstant(0, dl, NewLHS.getValueType());
5894 return;
5895 }
5896
5897 // If this is a comparison of the sign bit, just look at the top part.
5898 // X > -1, x < 0
5899 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
5900 if ((CCCode == ISD::SETLT && CST->isZero()) || // X < 0
5901 (CCCode == ISD::SETGT && CST->isAllOnes())) { // X > -1
5902 NewLHS = LHSHi;
5903 NewRHS = RHSHi;
5904 return;
5905 }
5906
5907 // FIXME: This generated code sucks.
5908 ISD::CondCode LowCC;
5909 switch (CCCode) {
5910 default: llvm_unreachable("Unknown integer setcc!");
5911 case ISD::SETLT:
5912 case ISD::SETULT: LowCC = ISD::SETULT; break;
5913 case ISD::SETGT:
5914 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5915 case ISD::SETLE:
5916 case ISD::SETULE: LowCC = ISD::SETULE; break;
5917 case ISD::SETGE:
5918 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5919 }
5920
5921 // LoCmp = lo(op1) < lo(op2) // Always unsigned comparison
5922 // HiCmp = hi(op1) < hi(op2) // Signedness depends on operands
5923 // dest = hi(op1) == hi(op2) ? LoCmp : HiCmp;
5924
5925 // NOTE: on targets without efficient SELECT of bools, we can always use
5926 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5927 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true,
5928 nullptr);
5929 SDValue LoCmp, HiCmp;
5930 if (TLI.isTypeLegal(LHSLo.getValueType()))
5931 LoCmp = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()), LHSLo,
5932 RHSLo, LowCC, false, DagCombineInfo, dl);
5933 if (!LoCmp.getNode())
5934 LoCmp = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()), LHSLo,
5935 RHSLo, LowCC);
5936 if (TLI.isTypeLegal(LHSHi.getValueType()))
5937 HiCmp = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()), LHSHi,
5938 RHSHi, CCCode, false, DagCombineInfo, dl);
5939 if (!HiCmp.getNode())
5940 HiCmp =
5941 DAG.getNode(ISD::SETCC, dl, getSetCCResultType(LHSHi.getValueType()),
5942 LHSHi, RHSHi, DAG.getCondCode(CCCode));
5943
5944 ConstantSDNode *LoCmpC = dyn_cast<ConstantSDNode>(LoCmp.getNode());
5945 ConstantSDNode *HiCmpC = dyn_cast<ConstantSDNode>(HiCmp.getNode());
5946
5947 bool EqAllowed = ISD::isTrueWhenEqual(CCCode);
5948
5949 // FIXME: Is the HiCmpC->isOne() here correct for
5950 // ZeroOrNegativeOneBooleanContent.
5951 if ((EqAllowed && (HiCmpC && HiCmpC->isZero())) ||
5952 (!EqAllowed &&
5953 ((HiCmpC && HiCmpC->isOne()) || (LoCmpC && LoCmpC->isZero())))) {
5954 // For LE / GE, if high part is known false, ignore the low part.
5955 // For LT / GT: if low part is known false, return the high part.
5956 // if high part is known true, ignore the low part.
5957 NewLHS = HiCmp;
5958 NewRHS = SDValue();
5959 return;
5960 }
5961
5962 if (LHSHi == RHSHi) {
5963 // Comparing the low bits is enough.
5964 NewLHS = LoCmp;
5965 NewRHS = SDValue();
5966 return;
5967 }
5968
5969 // Lower with SETCCCARRY if the target supports it.
5970 EVT HiVT = LHSHi.getValueType();
5971 EVT ExpandVT = TLI.getTypeToExpandTo(*DAG.getContext(), HiVT);
5972 bool HasSETCCCARRY = TLI.isOperationLegalOrCustom(ISD::SETCCCARRY, ExpandVT);
5973
5974 // FIXME: Make all targets support this, then remove the other lowering.
5975 if (HasSETCCCARRY) {
5976 // SETCCCARRY can detect < and >= directly. For > and <=, flip
5977 // operands and condition code.
5978 bool FlipOperands = false;
5979 switch (CCCode) {
5980 case ISD::SETGT: CCCode = ISD::SETLT; FlipOperands = true; break;
5981 case ISD::SETUGT: CCCode = ISD::SETULT; FlipOperands = true; break;
5982 case ISD::SETLE: CCCode = ISD::SETGE; FlipOperands = true; break;
5983 case ISD::SETULE: CCCode = ISD::SETUGE; FlipOperands = true; break;
5984 default: break;
5985 }
5986 if (FlipOperands) {
5987 std::swap(LHSLo, RHSLo);
5988 std::swap(LHSHi, RHSHi);
5989 }
5990 // Perform a wide subtraction, feeding the carry from the low part into
5991 // SETCCCARRY. The SETCCCARRY operation is essentially looking at the high
5992 // part of the result of LHS - RHS. It is negative iff LHS < RHS. It is
5993 // zero or positive iff LHS >= RHS.
5994 EVT LoVT = LHSLo.getValueType();
5995 SDVTList VTList = DAG.getVTList(LoVT, getSetCCResultType(LoVT));
5996 SDValue LowCmp = DAG.getNode(ISD::USUBO, dl, VTList, LHSLo, RHSLo);
5997 SDValue Res = DAG.getNode(ISD::SETCCCARRY, dl, getSetCCResultType(HiVT),
5998 LHSHi, RHSHi, LowCmp.getValue(1),
5999 DAG.getCondCode(CCCode));
6000 NewLHS = Res;
6001 NewRHS = SDValue();
6002 return;
6003 }
6004
6005 NewLHS = TLI.SimplifySetCC(getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ,
6006 false, DagCombineInfo, dl);
6007 if (!NewLHS.getNode())
6008 NewLHS =
6009 DAG.getSetCC(dl, getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ);
6010 NewLHS = DAG.getSelect(dl, LoCmp.getValueType(), NewLHS, LoCmp, HiCmp);
6011 NewRHS = SDValue();
6012}
6013
6014SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
6015 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
6016 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
6017 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
6018
6019 // If ExpandSetCCOperands returned a scalar, we need to compare the result
6020 // against zero to select between true and false values.
6021 if (!NewRHS.getNode()) {
6022 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
6023 CCCode = ISD::SETNE;
6024 }
6025
6026 // Update N to have the operands specified.
6027 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
6028 DAG.getCondCode(CCCode), NewLHS, NewRHS,
6029 N->getOperand(4)), 0);
6030}
6031
6032SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
6033 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
6034 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
6035 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
6036
6037 // If ExpandSetCCOperands returned a scalar, we need to compare the result
6038 // against zero to select between true and false values.
6039 if (!NewRHS.getNode()) {
6040 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
6041 CCCode = ISD::SETNE;
6042 }
6043
6044 // Update N to have the operands specified.
6045 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
6046 N->getOperand(2), N->getOperand(3),
6047 DAG.getCondCode(CCCode)), 0);
6048}
6049
6050SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
6051 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
6052 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
6053 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
6054
6055 // If ExpandSetCCOperands returned a scalar, use it.
6056 if (!NewRHS.getNode()) {
6057 assert(NewLHS.getValueType() == N->getValueType(0) &&
6058 "Unexpected setcc expansion!");
6059 return NewLHS;
6060 }
6061
6062 // Otherwise, update N to have the operands specified.
6063 return SDValue(
6064 DAG.UpdateNodeOperands(N, NewLHS, NewRHS, DAG.getCondCode(CCCode)), 0);
6065}
6066
6067SDValue DAGTypeLegalizer::ExpandIntOp_SETCCCARRY(SDNode *N) {
6068 SDValue LHS = N->getOperand(0);
6069 SDValue RHS = N->getOperand(1);
6070 SDValue Carry = N->getOperand(2);
6071 SDValue Cond = N->getOperand(3);
6072 SDLoc dl = SDLoc(N);
6073
6074 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
6075 GetExpandedInteger(LHS, LHSLo, LHSHi);
6076 GetExpandedInteger(RHS, RHSLo, RHSHi);
6077
6078 // Expand to a USUBO_CARRY for the low part and a SETCCCARRY for the high.
6079 SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), Carry.getValueType());
6080 SDValue LowCmp =
6081 DAG.getNode(ISD::USUBO_CARRY, dl, VTList, LHSLo, RHSLo, Carry);
6082 return DAG.getNode(ISD::SETCCCARRY, dl, N->getValueType(0), LHSHi, RHSHi,
6083 LowCmp.getValue(1), Cond);
6084}
6085
6086SDValue DAGTypeLegalizer::ExpandIntOp_SPLAT_VECTOR(SDNode *N) {
6087 // Split the operand and replace with SPLAT_VECTOR_PARTS.
6088 SDValue Lo, Hi;
6089 GetExpandedInteger(N->getOperand(0), Lo, Hi);
6090 return DAG.getNode(ISD::SPLAT_VECTOR_PARTS, SDLoc(N), N->getValueType(0), Lo,
6091 Hi);
6092}
6093
6094SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
6095 // The value being shifted is legal, but the shift amount is too big.
6096 // It follows that either the result of the shift is undefined, or the
6097 // upper half of the shift amount is zero. Just use the lower half.
6098 SDValue Lo, Hi;
6099 GetExpandedInteger(N->getOperand(1), Lo, Hi);
6100 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
6101}
6102
6103SDValue DAGTypeLegalizer::ExpandIntOp_CMP(SDNode *N) {
6104 return TLI.expandCMP(N, DAG);
6105}
6106
6107SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
6108 // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
6109 // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
6110 // constant to valid type.
6111 SDValue Lo, Hi;
6112 GetExpandedInteger(N->getOperand(0), Lo, Hi);
6113 return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
6114}
6115
6116SDValue DAGTypeLegalizer::ExpandIntOp_XINT_TO_FP(SDNode *N) {
6117 bool IsStrict = N->isStrictFPOpcode();
6118 bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP ||
6119 N->getOpcode() == ISD::STRICT_SINT_TO_FP;
6120 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
6121 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
6122 EVT DstVT = N->getValueType(0);
6123 RTLIB::Libcall LC = IsSigned ? RTLIB::getSINTTOFP(Op.getValueType(), DstVT)
6124 : RTLIB::getUINTTOFP(Op.getValueType(), DstVT);
6125 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
6126 "Don't know how to expand this XINT_TO_FP!");
6127 TargetLowering::MakeLibCallOptions CallOptions;
6128 CallOptions.setIsSigned(true);
6129 std::pair<SDValue, SDValue> Tmp =
6130 TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, SDLoc(N), Chain);
6131
6132 if (!IsStrict)
6133 return Tmp.first;
6134
6135 ReplaceValueWith(SDValue(N, 1), Tmp.second);
6136 ReplaceValueWith(SDValue(N, 0), Tmp.first);
6137 return SDValue();
6138}
6139
6140SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
6141 assert(!N->isAtomic() && "Should have been a ATOMIC_STORE?");
6142
6143 if (ISD::isNormalStore(N))
6144 return ExpandOp_NormalStore(N, OpNo);
6145
6146 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
6147 assert(OpNo == 1 && "Can only expand the stored value so far");
6148
6149 EVT VT = N->getOperand(1).getValueType();
6150 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6151 SDValue Ch = N->getChain();
6152 SDValue Ptr = N->getBasePtr();
6153 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
6154 AAMDNodes AAInfo = N->getAAInfo();
6155 SDLoc dl(N);
6156 SDValue Lo, Hi;
6157
6158 assert(NVT.isByteSized() && "Expanded type not byte sized!");
6159
6160 if (N->getMemoryVT().bitsLE(NVT)) {
6161 GetExpandedInteger(N->getValue(), Lo, Hi);
6162 return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
6163 N->getMemoryVT(), N->getBaseAlign(), MMOFlags,
6164 AAInfo);
6165 }
6166
6167 if (DAG.getDataLayout().isLittleEndian()) {
6168 // Little-endian - low bits are at low addresses.
6169 GetExpandedInteger(N->getValue(), Lo, Hi);
6170
6171 Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(), N->getBaseAlign(),
6172 MMOFlags, AAInfo);
6173
6174 unsigned ExcessBits =
6175 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
6176 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
6177
6178 // Increment the pointer to the other half.
6179 unsigned IncrementSize = NVT.getSizeInBits()/8;
6180 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::getFixed(IncrementSize));
6181 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr,
6182 N->getPointerInfo().getWithOffset(IncrementSize),
6183 NEVT, N->getBaseAlign(), MMOFlags, AAInfo);
6184 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
6185 }
6186
6187 // Big-endian - high bits are at low addresses. Favor aligned stores at
6188 // the cost of some bit-fiddling.
6189 GetExpandedInteger(N->getValue(), Lo, Hi);
6190
6191 EVT ExtVT = N->getMemoryVT();
6192 unsigned EBytes = ExtVT.getStoreSize();
6193 unsigned IncrementSize = NVT.getSizeInBits()/8;
6194 unsigned ExcessBits = (EBytes - IncrementSize)*8;
6195 EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
6196 ExtVT.getSizeInBits() - ExcessBits);
6197
6198 if (ExcessBits < NVT.getSizeInBits()) {
6199 // Transfer high bits from the top of Lo to the bottom of Hi.
6200 Hi = DAG.getNode(
6201 ISD::SHL, dl, NVT, Hi,
6202 DAG.getShiftAmountConstant(NVT.getSizeInBits() - ExcessBits, NVT, dl));
6203 Hi = DAG.getNode(
6204 ISD::OR, dl, NVT, Hi,
6205 DAG.getNode(ISD::SRL, dl, NVT, Lo,
6206 DAG.getShiftAmountConstant(ExcessBits, NVT, dl)));
6207 }
6208
6209 // Store both the high bits and maybe some of the low bits.
6210 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(), HiVT,
6211 N->getBaseAlign(), MMOFlags, AAInfo);
6212
6213 // Increment the pointer to the other half.
6214 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::getFixed(IncrementSize));
6215 // Store the lowest ExcessBits bits in the second half.
6216 Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
6217 N->getPointerInfo().getWithOffset(IncrementSize),
6218 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
6219 N->getBaseAlign(), MMOFlags, AAInfo);
6220 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
6221}
6222
6223SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
6224 SDValue InL, InH;
6225 GetExpandedInteger(N->getOperand(0), InL, InH);
6226 // Just truncate the low part of the source.
6227 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL);
6228}
6229
6230SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) {
6231 SDLoc dl(N);
6232 SDValue Swap =
6233 DAG.getAtomic(ISD::ATOMIC_SWAP, dl, cast<AtomicSDNode>(N)->getMemoryVT(),
6234 N->getOperand(0), N->getOperand(2), N->getOperand(1),
6235 cast<AtomicSDNode>(N)->getMemOperand());
6236 return Swap.getValue(1);
6237}
6238
6239SDValue DAGTypeLegalizer::ExpandIntOp_VP_STRIDED(SDNode *N, unsigned OpNo) {
6240 assert((N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD && OpNo == 3) ||
6241 (N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE && OpNo == 4));
6242
6243 SDValue Hi; // The upper half is dropped out.
6244 SmallVector<SDValue, 8> NewOps(N->ops());
6245 GetExpandedInteger(NewOps[OpNo], NewOps[OpNo], Hi);
6246
6247 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
6248}
6249
6250SDValue DAGTypeLegalizer::ExpandIntOp_WRITE_REGISTER(SDNode *N, unsigned OpNo) {
6251 const Function &Fn = DAG.getMachineFunction().getFunction();
6252 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
6253 "cannot use llvm.write_register with illegal type", Fn,
6254 N->getDebugLoc()));
6255
6256 return N->getOperand(0);
6257}
6258
6259SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SPLICE(SDNode *N) {
6260 SDLoc dl(N);
6261
6262 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6263 SDValue V1 = GetPromotedInteger(N->getOperand(1));
6264 EVT OutVT = V0.getValueType();
6265
6266 return DAG.getNode(N->getOpcode(), dl, OutVT, V0, V1, N->getOperand(2));
6267}
6268
6269SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_INTERLEAVE_DEINTERLEAVE(SDNode *N) {
6270 SDLoc DL(N);
6271 unsigned Factor = N->getNumOperands();
6272
6274 for (unsigned i = 0; i != Factor; i++)
6275 Ops[i] = GetPromotedInteger(N->getOperand(i));
6276
6277 SmallVector<EVT, 8> ResVTs(Factor, Ops[0].getValueType());
6278 SDValue Res = DAG.getNode(N->getOpcode(), DL, DAG.getVTList(ResVTs), Ops);
6279
6280 for (unsigned i = 0; i != Factor; i++)
6281 SetPromotedInteger(SDValue(N, i), Res.getValue(i));
6282
6283 return SDValue();
6284}
6285
6286SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
6287
6288 EVT OutVT = N->getValueType(0);
6289 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6290 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6291 EVT NOutVTElem = NOutVT.getVectorElementType();
6292
6293 SDLoc dl(N);
6294 SDValue BaseIdx = N->getOperand(1);
6295
6296 // TODO: We may be able to use this for types other than scalable
6297 // vectors and fix those tests that expect BUILD_VECTOR to be used
6298 if (OutVT.isScalableVector()) {
6299 SDValue InOp0 = N->getOperand(0);
6300 EVT InVT = InOp0.getValueType();
6301
6302 // Try and extract from a smaller type so that it eventually falls
6303 // into the promotion code below.
6304 if (getTypeAction(InVT) == TargetLowering::TypeSplitVector ||
6305 getTypeAction(InVT) == TargetLowering::TypeLegal) {
6306 EVT NInVT = InVT.getHalfNumVectorElementsVT(*DAG.getContext());
6307 unsigned NElts = NInVT.getVectorMinNumElements();
6308 uint64_t IdxVal = BaseIdx->getAsZExtVal();
6309
6310 SDValue Step1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NInVT, InOp0,
6311 DAG.getConstant(alignDown(IdxVal, NElts), dl,
6312 BaseIdx.getValueType()));
6313 SDValue Step2 = DAG.getNode(
6314 ISD::EXTRACT_SUBVECTOR, dl, OutVT, Step1,
6315 DAG.getConstant(IdxVal % NElts, dl, BaseIdx.getValueType()));
6316 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Step2);
6317 }
6318
6319 // Try and extract from a widened type.
6320 if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
6321 SDValue Ops[] = {GetWidenedVector(InOp0), BaseIdx};
6322 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), OutVT, Ops);
6323 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Ext);
6324 }
6325
6326 // Promote operands and see if this is handled by target lowering,
6327 // Otherwise, use the BUILD_VECTOR approach below
6328 if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
6329 // Collect the (promoted) operands
6330 SDValue Ops[] = { GetPromotedInteger(InOp0), BaseIdx };
6331
6332 EVT PromEltVT = Ops[0].getValueType().getVectorElementType();
6333 assert(PromEltVT.bitsLE(NOutVTElem) &&
6334 "Promoted operand has an element type greater than result");
6335
6336 EVT ExtVT = NOutVT.changeVectorElementType(*DAG.getContext(), PromEltVT);
6337 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), ExtVT, Ops);
6338 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Ext);
6339 }
6340 }
6341
6342 if (OutVT.isScalableVector())
6343 report_fatal_error("Unable to promote scalable types using BUILD_VECTOR");
6344
6345 SDValue InOp0 = N->getOperand(0);
6346 if (getTypeAction(InOp0.getValueType()) == TargetLowering::TypePromoteInteger)
6347 InOp0 = GetPromotedInteger(InOp0);
6348
6349 EVT InVT = InOp0.getValueType();
6350 EVT InSVT = InVT.getVectorElementType();
6351
6352 unsigned OutNumElems = OutVT.getVectorNumElements();
6354 Ops.reserve(OutNumElems);
6355 for (unsigned i = 0; i != OutNumElems; ++i) {
6356 // Extract the element from the original vector.
6357 SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(), BaseIdx,
6358 DAG.getConstant(i, dl, BaseIdx.getValueType()));
6359 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, InSVT,
6360 N->getOperand(0), Index);
6361 SDValue Op = DAG.getAnyExtOrTrunc(Ext, dl, NOutVTElem);
6362 // Insert the converted element to the new vector.
6363 Ops.push_back(Op);
6364 }
6365
6366 return DAG.getBuildVector(NOutVT, dl, Ops);
6367}
6368
6369SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_SUBVECTOR(SDNode *N) {
6370 EVT OutVT = N->getValueType(0);
6371 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6372 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6373
6374 SDLoc dl(N);
6375 SDValue Vec = N->getOperand(0);
6376 SDValue SubVec = N->getOperand(1);
6377 SDValue Idx = N->getOperand(2);
6378
6379 EVT SubVecVT = SubVec.getValueType();
6380 EVT NSubVT =
6381 EVT::getVectorVT(*DAG.getContext(), NOutVT.getVectorElementType(),
6382 SubVecVT.getVectorElementCount());
6383
6384 Vec = GetPromotedInteger(Vec);
6385 SubVec = DAG.getNode(ISD::ANY_EXTEND, dl, NSubVT, SubVec);
6386
6387 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, NOutVT, Vec, SubVec, Idx);
6388}
6389
6390SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_REVERSE(SDNode *N) {
6391 SDLoc dl(N);
6392
6393 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6394 EVT OutVT = V0.getValueType();
6395
6396 return DAG.getNode(ISD::VECTOR_REVERSE, dl, OutVT, V0);
6397}
6398
6399SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
6400 ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
6401 EVT VT = N->getValueType(0);
6402 SDLoc dl(N);
6403
6404 ArrayRef<int> NewMask = SV->getMask().slice(0, VT.getVectorNumElements());
6405
6406 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6407 SDValue V1 = GetPromotedInteger(N->getOperand(1));
6408 EVT OutVT = V0.getValueType();
6409
6410 return DAG.getVectorShuffle(OutVT, dl, V0, V1, NewMask);
6411}
6412
6413SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
6414 EVT OutVT = N->getValueType(0);
6415 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6416 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6417 unsigned NumElems = N->getNumOperands();
6418 EVT NOutVTElem = NOutVT.getVectorElementType();
6419 TargetLoweringBase::BooleanContent NOutBoolType = TLI.getBooleanContents(NOutVT);
6420 unsigned NOutExtOpc = TargetLowering::getExtendForContent(NOutBoolType);
6421 SDLoc dl(N);
6422
6424 Ops.reserve(NumElems);
6425 for (unsigned i = 0; i != NumElems; ++i) {
6426 SDValue Op = N->getOperand(i);
6427 EVT OpVT = Op.getValueType();
6428 // BUILD_VECTOR integer operand types are allowed to be larger than the
6429 // result's element type. This may still be true after the promotion. For
6430 // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
6431 // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
6432 if (OpVT.bitsLT(NOutVTElem)) {
6433 unsigned ExtOpc = ISD::ANY_EXTEND;
6434 // Attempt to extend constant bool vectors to match target's BooleanContent.
6435 // While not necessary, this improves chances of the constant correctly
6436 // folding with compare results (e.g. for NOT patterns).
6437 if (OpVT == MVT::i1 && Op.getOpcode() == ISD::Constant)
6438 ExtOpc = NOutExtOpc;
6439 Op = DAG.getNode(ExtOpc, dl, NOutVTElem, Op);
6440 }
6441 Ops.push_back(Op);
6442 }
6443
6444 return DAG.getBuildVector(NOutVT, dl, Ops);
6445}
6446
6447SDValue DAGTypeLegalizer::PromoteIntRes_ScalarOp(SDNode *N) {
6448
6449 SDLoc dl(N);
6450
6451 assert(!N->getOperand(0).getValueType().isVector() &&
6452 "Input must be a scalar");
6453
6454 EVT OutVT = N->getValueType(0);
6455 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6456 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6457 EVT NOutElemVT = NOutVT.getVectorElementType();
6458
6459 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutElemVT, N->getOperand(0));
6460 return DAG.getNode(N->getOpcode(), dl, NOutVT, Op);
6461}
6462
6463SDValue DAGTypeLegalizer::PromoteIntRes_STEP_VECTOR(SDNode *N) {
6464 SDLoc dl(N);
6465 EVT OutVT = N->getValueType(0);
6466 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6467 assert(NOutVT.isScalableVector() &&
6468 "Type must be promoted to a scalable vector type");
6469 const APInt &StepVal = N->getConstantOperandAPInt(0);
6470 return DAG.getStepVector(dl, NOutVT,
6471 StepVal.sext(NOutVT.getScalarSizeInBits()));
6472}
6473
6474SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) {
6475 SDLoc dl(N);
6476
6477 EVT OutVT = N->getValueType(0);
6478 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6479 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6480
6481 unsigned NumOperands = N->getNumOperands();
6482 unsigned NumOutElem = NOutVT.getVectorMinNumElements();
6483 EVT OutElemTy = NOutVT.getVectorElementType();
6484 if (OutVT.isScalableVector()) {
6485 // Find the largest promoted element type for each of the operands.
6486 SDUse *MaxSizedValue = std::max_element(
6487 N->op_begin(), N->op_end(), [](const SDValue &A, const SDValue &B) {
6488 EVT AVT = A.getValueType().getVectorElementType();
6489 EVT BVT = B.getValueType().getVectorElementType();
6490 return AVT.getScalarSizeInBits() < BVT.getScalarSizeInBits();
6491 });
6492 EVT MaxElementVT = MaxSizedValue->getValueType().getVectorElementType();
6493
6494 // Then promote all vectors to the largest element type.
6496 for (unsigned I = 0; I < NumOperands; ++I) {
6497 SDValue Op = N->getOperand(I);
6498 EVT OpVT = Op.getValueType();
6499 if (getTypeAction(OpVT) == TargetLowering::TypePromoteInteger)
6500 Op = GetPromotedInteger(Op);
6501 else
6502 assert(getTypeAction(OpVT) == TargetLowering::TypeLegal &&
6503 "Unhandled legalization type");
6504
6506 MaxElementVT.getScalarSizeInBits())
6507 Op = DAG.getAnyExtOrTrunc(
6508 Op, dl,
6509 OpVT.changeVectorElementType(*DAG.getContext(), MaxElementVT));
6510 Ops.push_back(Op);
6511 }
6512
6513 // Do the CONCAT on the promoted type and finally truncate to (the promoted)
6514 // NOutVT.
6515 return DAG.getAnyExtOrTrunc(
6516 DAG.getNode(
6518 OutVT.changeVectorElementType(*DAG.getContext(), MaxElementVT),
6519 Ops),
6520 dl, NOutVT);
6521 }
6522
6523 unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements();
6524 assert(NumElem * NumOperands == NumOutElem &&
6525 "Unexpected number of elements");
6526
6527 // Take the elements from the first vector.
6528 SmallVector<SDValue, 8> Ops(NumOutElem);
6529 for (unsigned i = 0; i < NumOperands; ++i) {
6530 SDValue Op = N->getOperand(i);
6531 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger)
6532 Op = GetPromotedInteger(Op);
6533 EVT SclrTy = Op.getValueType().getVectorElementType();
6534 assert(NumElem == Op.getValueType().getVectorNumElements() &&
6535 "Unexpected number of elements");
6536
6537 for (unsigned j = 0; j < NumElem; ++j) {
6538 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Op,
6539 DAG.getVectorIdxConstant(j, dl));
6540 Ops[i * NumElem + j] = DAG.getAnyExtOrTrunc(Ext, dl, OutElemTy);
6541 }
6542 }
6543
6544 return DAG.getBuildVector(NOutVT, dl, Ops);
6545}
6546
6547SDValue DAGTypeLegalizer::PromoteIntRes_EXTEND_VECTOR_INREG(SDNode *N) {
6548 EVT VT = N->getValueType(0);
6549 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6550 assert(NVT.isVector() && "This type must be promoted to a vector type");
6551
6552 SDLoc dl(N);
6553
6554 // For operands whose TypeAction is to promote, extend the promoted node
6555 // appropriately (ZERO_EXTEND or SIGN_EXTEND) from the original pre-promotion
6556 // type, and then construct a new *_EXTEND_VECTOR_INREG node to the promote-to
6557 // type..
6558 if (getTypeAction(N->getOperand(0).getValueType())
6560 SDValue Promoted;
6561
6562 switch(N->getOpcode()) {
6564 Promoted = SExtPromotedInteger(N->getOperand(0));
6565 break;
6567 Promoted = ZExtPromotedInteger(N->getOperand(0));
6568 break;
6570 Promoted = GetPromotedInteger(N->getOperand(0));
6571 break;
6572 default:
6573 llvm_unreachable("Node has unexpected Opcode");
6574 }
6575 unsigned NewSize = NVT.getSizeInBits();
6576 if (Promoted.getValueType().getSizeInBits() > NewSize) {
6577 EVT ExtractVT = EVT::getVectorVT(
6578 *DAG.getContext(), Promoted.getValueType().getVectorElementType(),
6579 NewSize / Promoted.getScalarValueSizeInBits());
6580
6581 Promoted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ExtractVT, Promoted,
6582 DAG.getVectorIdxConstant(0, dl));
6583 }
6584 return DAG.getNode(N->getOpcode(), dl, NVT, Promoted);
6585 }
6586
6587 // Directly extend to the appropriate transform-to type.
6588 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
6589}
6590
6591SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_FIND_LAST_ACTIVE(SDNode *N) {
6592 EVT VT = N->getValueType(0);
6593 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6594 return DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, SDLoc(N), NVT, N->ops());
6595}
6596
6597SDValue DAGTypeLegalizer::PromoteIntRes_GET_ACTIVE_LANE_MASK(SDNode *N) {
6598 EVT VT = N->getValueType(0);
6599 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6600 return DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, SDLoc(N), NVT, N->ops());
6601}
6602
6603SDValue DAGTypeLegalizer::PromoteIntRes_PARTIAL_REDUCE_MLA(SDNode *N) {
6604 SDLoc DL(N);
6605 EVT VT = N->getValueType(0);
6606 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6607 SDValue ExtAcc = GetPromotedInteger(N->getOperand(0));
6608 return DAG.getNode(N->getOpcode(), DL, NVT, ExtAcc, N->getOperand(1),
6609 N->getOperand(2));
6610}
6611
6612SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
6613 EVT OutVT = N->getValueType(0);
6614 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6615 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6616
6617 EVT NOutVTElem = NOutVT.getVectorElementType();
6618
6619 SDLoc dl(N);
6620 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6621
6622 SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
6623 NOutVTElem, N->getOperand(1));
6624 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT,
6625 V0, ConvElem, N->getOperand(2));
6626}
6627
6628SDValue DAGTypeLegalizer::PromoteIntRes_VECREDUCE(SDNode *N) {
6629 // The VECREDUCE result size may be larger than the element size, so
6630 // we can simply change the result type.
6631 SDLoc dl(N);
6632 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6633 return DAG.getNode(N->getOpcode(), dl, NVT, N->ops());
6634}
6635
6636SDValue DAGTypeLegalizer::PromoteIntRes_VP_REDUCE(SDNode *N) {
6637 // The VP_REDUCE result size may be larger than the element size, so we can
6638 // simply change the result type. However the start value and result must be
6639 // the same.
6640 SDLoc DL(N);
6641 SDValue Start = PromoteIntOpVectorReduction(N, N->getOperand(0));
6642 return DAG.getNode(N->getOpcode(), DL, Start.getValueType(), Start,
6643 N->getOperand(1), N->getOperand(2), N->getOperand(3));
6644}
6645
6646SDValue DAGTypeLegalizer::PromoteIntRes_PATCHPOINT(SDNode *N) {
6647 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6648 SDLoc dl(N);
6649
6650 assert(N->getNumValues() == 3 && "Expected 3 values for PATCHPOINT");
6651 SDVTList VTList = DAG.getVTList({NVT, MVT::Other, MVT::Glue});
6652
6653 SmallVector<SDValue> Ops(N->ops());
6654 SDValue Res = DAG.getNode(ISD::PATCHPOINT, dl, VTList, Ops);
6655
6656 // Replace chain and glue uses with the new patchpoint.
6657 SDValue From[] = {SDValue(N, 1), SDValue(N, 2)};
6658 SDValue To[] = {Res.getValue(1), Res.getValue(2)};
6659 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
6660
6661 return Res.getValue(0);
6662}
6663
6664SDValue DAGTypeLegalizer::PromoteIntRes_READ_REGISTER(SDNode *N) {
6665 const Function &Fn = DAG.getMachineFunction().getFunction();
6666 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
6667 "cannot use llvm.read_register with illegal type", Fn, N->getDebugLoc()));
6668
6669 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6670 ReplaceValueWith(SDValue(N, 1), N->getOperand(0));
6671 return DAG.getPOISON(NVT);
6672}
6673
6674SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
6675 SDLoc dl(N);
6676 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6677 SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl,
6678 TLI.getVectorIdxTy(DAG.getDataLayout()));
6680 V0->getValueType(0).getScalarType(), V0, V1);
6681
6682 // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
6683 // element types. If this is the case then we need to expand the outgoing
6684 // value and not truncate it.
6685 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
6686}
6687
6688SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_SUBVECTOR(SDNode *N) {
6689 SDLoc dl(N);
6690 // The result type is equal to the first input operand's type, so the
6691 // type that needs promoting must be the second source vector.
6692 SDValue V0 = N->getOperand(0);
6693 SDValue V1 = GetPromotedInteger(N->getOperand(1));
6694 SDValue Idx = N->getOperand(2);
6695 EVT PromVT = EVT::getVectorVT(*DAG.getContext(),
6696 V1.getValueType().getVectorElementType(),
6698 V0 = DAG.getAnyExtOrTrunc(V0, dl, PromVT);
6699 SDValue Ext = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, PromVT, V0, V1, Idx);
6700 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
6701}
6702
6703// FIXME: We wouldn't need this if clang could promote short integers
6704// that are arguments to FAKE_USE.
6705SDValue DAGTypeLegalizer::PromoteIntOp_FAKE_USE(SDNode *N) {
6706 SDLoc dl(N);
6707 SDValue V0 = N->getOperand(0);
6708 SDValue V1 = N->getOperand(1);
6709 EVT InVT1 = V1.getValueType();
6710 SDValue VPromoted =
6711 DAG.getNode(ISD::ANY_EXTEND, dl,
6712 TLI.getTypeToTransformTo(*DAG.getContext(), InVT1), V1);
6713 return DAG.getNode(N->getOpcode(), dl, N->getValueType(0), V0, VPromoted);
6714}
6715
6716SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_SUBVECTOR(SDNode *N) {
6717 SDLoc dl(N);
6718 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6719 MVT InVT = V0.getValueType().getSimpleVT();
6720 MVT OutVT = MVT::getVectorVT(InVT.getVectorElementType(),
6721 N->getValueType(0).getVectorNumElements());
6722 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, V0, N->getOperand(1));
6723 return DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), Ext);
6724}
6725
6726SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
6727 SDLoc dl(N);
6728
6729 EVT ResVT = N->getValueType(0);
6730 unsigned NumElems = N->getNumOperands();
6731
6732 if (ResVT.isScalableVector()) {
6733 SDValue ResVec = DAG.getUNDEF(ResVT);
6734
6735 for (unsigned OpIdx = 0; OpIdx < NumElems; ++OpIdx) {
6736 SDValue Op = N->getOperand(OpIdx);
6737 unsigned OpNumElts = Op.getValueType().getVectorMinNumElements();
6738 ResVec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, ResVec, Op,
6739 DAG.getIntPtrConstant(OpIdx * OpNumElts, dl));
6740 }
6741
6742 return ResVec;
6743 }
6744
6745 EVT RetSclrTy = N->getValueType(0).getVectorElementType();
6746
6748 NewOps.reserve(NumElems);
6749
6750 // For each incoming vector
6751 for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) {
6752 SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
6753 EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
6754 unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
6755
6756 for (unsigned i=0; i<NumElem; ++i) {
6757 // Extract element from incoming vector
6758 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Incoming,
6759 DAG.getVectorIdxConstant(i, dl));
6760 SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
6761 NewOps.push_back(Tr);
6762 }
6763 }
6764
6765 return DAG.getBuildVector(N->getValueType(0), dl, NewOps);
6766}
6767
6768SDValue DAGTypeLegalizer::ExpandIntOp_STACKMAP(SDNode *N, unsigned OpNo) {
6769 assert(OpNo > 1);
6770 SDValue Op = N->getOperand(OpNo);
6771
6772 // FIXME: Non-constant operands are not yet handled:
6773 // - https://github.com/llvm/llvm-project/issues/26431
6774 // - https://github.com/llvm/llvm-project/issues/55957
6775 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op);
6776 if (!CN)
6777 return SDValue();
6778
6779 // Copy operands before the one being expanded.
6780 SmallVector<SDValue> NewOps;
6781 for (unsigned I = 0; I < OpNo; I++)
6782 NewOps.push_back(N->getOperand(I));
6783
6784 EVT Ty = Op.getValueType();
6785 SDLoc DL = SDLoc(N);
6786 if (CN->getConstantIntValue()->getValue().getActiveBits() < 64) {
6787 NewOps.push_back(
6788 DAG.getTargetConstant(StackMaps::ConstantOp, DL, MVT::i64));
6789 NewOps.push_back(DAG.getTargetConstant(CN->getZExtValue(), DL, Ty));
6790 } else {
6791 // FIXME: https://github.com/llvm/llvm-project/issues/55609
6792 return SDValue();
6793 }
6794
6795 // Copy remaining operands.
6796 for (unsigned I = OpNo + 1; I < N->getNumOperands(); I++)
6797 NewOps.push_back(N->getOperand(I));
6798
6799 SDValue NewNode = DAG.getNode(N->getOpcode(), DL, N->getVTList(), NewOps);
6800
6801 for (unsigned ResNum = 0; ResNum < N->getNumValues(); ResNum++)
6802 ReplaceValueWith(SDValue(N, ResNum), NewNode.getValue(ResNum));
6803
6804 return SDValue(); // Signal that we have replaced the node already.
6805}
6806
6807SDValue DAGTypeLegalizer::ExpandIntOp_PATCHPOINT(SDNode *N, unsigned OpNo) {
6808 assert(OpNo >= 7);
6809 SDValue Op = N->getOperand(OpNo);
6810
6811 // FIXME: Non-constant operands are not yet handled:
6812 // - https://github.com/llvm/llvm-project/issues/26431
6813 // - https://github.com/llvm/llvm-project/issues/55957
6814 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op);
6815 if (!CN)
6816 return SDValue();
6817
6818 // Copy operands before the one being expanded.
6819 SmallVector<SDValue> NewOps;
6820 for (unsigned I = 0; I < OpNo; I++)
6821 NewOps.push_back(N->getOperand(I));
6822
6823 EVT Ty = Op.getValueType();
6824 SDLoc DL = SDLoc(N);
6825 if (CN->getConstantIntValue()->getValue().getActiveBits() < 64) {
6826 NewOps.push_back(
6827 DAG.getTargetConstant(StackMaps::ConstantOp, DL, MVT::i64));
6828 NewOps.push_back(DAG.getTargetConstant(CN->getZExtValue(), DL, Ty));
6829 } else {
6830 // FIXME: https://github.com/llvm/llvm-project/issues/55609
6831 return SDValue();
6832 }
6833
6834 // Copy remaining operands.
6835 for (unsigned I = OpNo + 1; I < N->getNumOperands(); I++)
6836 NewOps.push_back(N->getOperand(I));
6837
6838 SDValue NewNode = DAG.getNode(N->getOpcode(), DL, N->getVTList(), NewOps);
6839
6840 for (unsigned ResNum = 0; ResNum < N->getNumValues(); ResNum++)
6841 ReplaceValueWith(SDValue(N, ResNum), NewNode.getValue(ResNum));
6842
6843 return SDValue(); // Signal that we have replaced the node already.
6844}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool isSigned(unsigned Opcode)
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static SDValue SaturateWidenedDIVFIX(SDValue V, SDLoc &dl, unsigned SatW, bool Signed, const TargetLowering &TLI, SelectionDAG &DAG)
static SDValue fpExtendHelper(SDValue Op, SDValue &Chain, bool IsStrict, EVT VT, SDLoc DL, SelectionDAG &DAG)
static SDValue earlyExpandDIVFIX(SDNode *N, SDValue LHS, SDValue RHS, unsigned Scale, const TargetLowering &TLI, SelectionDAG &DAG, unsigned SatW=0)
static unsigned getExtendForIntVecReduction(SDNode *N)
static std::pair< ISD::CondCode, ISD::NodeType > getExpandedMinMaxOps(int Op)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
#define I(x, y, z)
Definition MD5.cpp:57
MachineInstr unsigned OpIdx
const SmallVectorImpl< MachineOperand > & Cond
static Type * getValueType(Value *V, bool LookThroughCmp=false)
Returns the "element type" of the given value/instruction V.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
This file describes how to lower LLVM code to machine code.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1537
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:968
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
unsigned countLeadingOnes() const
Definition APInt.h:1649
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1191
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
unsigned countTrailingZeros() const
Definition APInt.h:1672
unsigned countLeadingZeros() const
Definition APInt.h:1631
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:1028
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1266
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
unsigned countTrailingOnes() const
Definition APInt.h:1687
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1230
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:185
This is an SDNode representing atomic operations.
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
const ConstantInt * getConstantIntValue() const
uint64_t getZExtValue() const
@ NewNode
This is a new node, not before seen, that was created in the process of legalizing some other node.
const Function & getFunction() const
Definition Function.h:166
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:353
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
This class is used to represent ISD::LOAD nodes.
unsigned getVectorNumElements() const
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
static MVT getVectorVT(MVT VT, unsigned NumElements)
MVT getVectorElementType() const
Flags
Flags values. These may be or'd together.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
MachineMemOperand * getMemOperand() const
Return the unique MachineMemOperand object describing the memory reference performed by operation.
EVT getMemoryVT() const
Return the type of the in-memory value.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
bool isStrictFPOpcode()
Test if this node is a strict floating point pseudo-op.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
SDNodeFlags getFlags() const
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
const SDValue & getOperand(unsigned Num) const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
EVT getValueType() const
Convenience function for get().getValueType().
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
uint64_t getScalarValueSizeInBits() const
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
SDValue getExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT, unsigned Opcode)
Convert Op, which must be of integer type, to the integer type VT, by either any/sign/zero-extending ...
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVMContext * getContext() const
ArrayRef< int > getMask() const
void reserve(size_type N)
void push_back(const T &Elt)
This class is used to represent ISD::STORE nodes.
LegalizeAction
This enum indicates whether operations are valid for a target, and if not, what action should be used...
ShiftLegalizationStrategy
Return the preferred strategy to legalize tihs SHIFT instruction, with ExpansionFactor being the recu...
BooleanContent
Enum that describes how the target represents true/false values.
std::vector< ArgListEntry > ArgListTy
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
SDValue expandFixedPointDiv(unsigned Opcode, const SDLoc &dl, SDValue LHS, SDValue RHS, unsigned Scale, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US]DIVFIX[SAT].
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
This class is used to represent a VP_LOAD node.
This class is used to represent a VP_STORE node.
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns a value X where RHS.multiplyCoefficientBy(X) will result in a value whose quantity matches ou...
Definition TypeSize.h:277
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ Entry
Definition COFF.h:862
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:829
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:513
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:236
@ PARTIAL_REDUCE_SMLA
PARTIAL_REDUCE_[U|S]MLA(Accumulator, Input1, Input2) The partial reduction nodes sign or zero extend ...
@ LOOP_DEPENDENCE_RAW_MASK
@ COND_LOOP
COND_LOOP is a conditional branch to self, used for implementing efficient conditional traps.
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:275
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:602
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:789
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition ISDOpcodes.h:394
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:294
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:400
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:863
@ CTTZ_ELTS
Returns the number of number of trailing (least significant) zero elements in a vector.
@ VECTOR_FIND_LAST_ACTIVE
Finds the index of the last active mask element Operands: Mask.
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:890
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:586
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:749
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:920
@ SDIVREM
SDIVREM/UDIVREM - Divide two integers and produce both a quotient and remainder result.
Definition ISDOpcodes.h:280
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
@ FAKE_USE
FAKE_USE represents a use of the operand but does not do anything.
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:780
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:407
@ CONVERT_FROM_ARBITRARY_FP
CONVERT_FROM_ARBITRARY_FP - This operator converts from an arbitrary floating-point represented as an...
@ CTLZ_ZERO_POISON
Definition ISDOpcodes.h:798
@ SET_ROUNDING
Set rounding mode.
Definition ISDOpcodes.h:985
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:854
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:717
@ STRICT_UINT_TO_FP
Definition ISDOpcodes.h:487
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:667
@ READSTEADYCOUNTER
READSTEADYCOUNTER - This corresponds to the readfixedcounter intrinsic.
@ SETCCCARRY
Like SetCC, ops #0 and #1 are the LHS and RHS operands to compare, but op #2 is a boolean indicating ...
Definition ISDOpcodes.h:837
@ BR_CC
BR_CC - Conditional branch.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:352
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition ISDOpcodes.h:637
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:693
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:550
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:374
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:806
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:233
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:674
@ GET_ACTIVE_LANE_MASK
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ CTLS
Count leading redundant sign bits.
Definition ISDOpcodes.h:802
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
Definition ISDOpcodes.h:980
@ STRICT_FP_TO_FP16
@ STRICT_FP16_TO_FP
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:771
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:651
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:616
@ READ_REGISTER
READ_REGISTER, WRITE_REGISTER - This node represents llvm.register on the DAG, which implements the n...
Definition ISDOpcodes.h:139
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:578
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:860
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:821
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:386
@ PATCHPOINT
The llvm.experimental.patchpoint.
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:356
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by OFFSET elements an...
Definition ISDOpcodes.h:655
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:909
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:898
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:729
@ MASKED_UDIV
Masked vector arithmetic that returns poison on disabled lanes.
@ VECTOR_REVERSE
VECTOR_REVERSE(VECTOR) - Returns a vector, of the same type as VECTOR, whose elements are shuffled us...
Definition ISDOpcodes.h:642
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:413
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:988
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:815
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:328
@ STRICT_SINT_TO_FP
STRICT_[US]INT_TO_FP - Convert a signed or unsigned integer to a floating point value.
Definition ISDOpcodes.h:486
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ STRICT_FP_TO_UINT
Definition ISDOpcodes.h:480
@ PEXT
Parallel bit extract (compress) and parallel bit deposit (expand).
Definition ISDOpcodes.h:785
@ STRICT_FP_TO_SINT
STRICT_FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:479
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:936
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:507
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:741
@ STRICT_FP_TO_BF16
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:737
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:712
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1,VEC2) right by OFFSET elements a...
Definition ISDOpcodes.h:659
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:304
@ STACKMAP
The llvm.experimental.stackmap intrinsic.
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:683
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:241
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:567
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ CTTZ_ZERO_POISON
Bit counting operators with a poisoned result for zero inputs.
Definition ISDOpcodes.h:797
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:701
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:931
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Experimental vector histogram intrinsic Operands: Input Chain, Inc, Mask, Base, Index,...
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:955
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:866
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
@ BRCOND
BRCOND - Conditional branch.
@ CONVERT_TO_ARBITRARY_FP
CONVERT_TO_ARBITRARY_FP - Converts a native FP value to an arbitrary floating-point format,...
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:843
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ PARTIAL_REDUCE_SUMLA
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:365
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition ISDOpcodes.h:626
@ CTTZ_ELTS_ZERO_POISON
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:724
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:338
@ ABS_MIN_POISON
ABS with a poison result for INT_MIN.
Definition ISDOpcodes.h:753
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:558
@ LOOP_DEPENDENCE_WAR_MASK
The llvm.loop.dependence.
bool isNormalStore(const SDNode *N)
Returns true if the specified node is a non-truncating and unindexed store.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
bool isUNINDEXEDLoad(const SDNode *N)
Returns true if the specified node is an unindexed load.
bool isSignedIntSetCC(CondCode Code)
Return true if this is a setcc instruction that performs a signed comparison when used with integer o...
bool isUNINDEXEDStore(const SDNode *N)
Returns true if the specified node is an unindexed store.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
bool isUnsignedIntSetCC(CondCode Code)
Return true if this is a setcc instruction that performs an unsigned comparison when used with intege...
bool isNormalLoad(const SDNode *N)
Returns true if the specified node is a non-extending and unindexed load.
bool isIntEqualitySetCC(CondCode Code)
Return true if this is a setcc instruction that performs an equality comparison when used with intege...
LLVM_ABI Libcall getPOWI(EVT RetVT)
getPOWI - Return the POWI_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getSINTTOFP(EVT OpVT, EVT RetVT)
getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getUREM(EVT VT)
LLVM_ABI Libcall getSHL(EVT VT)
LLVM_ABI Libcall getSYNC(unsigned Opc, MVT VT)
Return the SYNC_FETCH_AND_* value for the given opcode and type, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getLDEXP(EVT RetVT)
getLDEXP - Return the LDEXP_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getUINTTOFP(EVT OpVT, EVT RetVT)
getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getSDIV(EVT VT)
LLVM_ABI Libcall getSRL(EVT VT)
LLVM_ABI Libcall getSRA(EVT VT)
LLVM_ABI Libcall getUDIV(EVT VT)
LLVM_ABI Libcall getFPTOUINT(EVT OpVT, EVT RetVT)
getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getLLROUND(EVT VT)
LLVM_ABI Libcall getLROUND(EVT VT)
LLVM_ABI Libcall getFPTOSINT(EVT OpVT, EVT RetVT)
getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getLRINT(EVT RetVT)
LLVM_ABI Libcall getOUTLINE_ATOMIC(unsigned Opc, AtomicOrdering Order, MVT VT)
Return the outline atomics value for the given opcode, atomic ordering and type, or UNKNOWN_LIBCALL i...
LLVM_ABI Libcall getLLRINT(EVT RetVT)
LLVM_ABI Libcall getSREM(EVT VT)
LLVM_ABI Libcall getMUL(EVT VT)
LLVM_ABI Libcall getCTPOP(EVT VT)
LLVM_ABI Libcall getMULO(EVT VT)
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:383
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:395
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
@ Known
Known to have no common set bits.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:546
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Success
The lock was released successfully.
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ AfterLegalizeTypes
Definition DAGCombine.h:17
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ Xor
Bitwise or logical XOR of integers.
@ Add
Sum of integers.
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:862
#define N
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:418
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:145
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:70
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:323
ElementCount getVectorElementCount() const
Definition ValueTypes.h:373
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:396
bool isByteSized() const
Return true if the bit size is a multiple of 8.
Definition ValueTypes.h:266
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:382
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:408
TypeSize getStoreSizeInBits() const
Return the number of bits overwritten by a store of the specified value type.
Definition ValueTypes.h:435
EVT changeVectorElementType(LLVMContext &Context, EVT EltVT) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element type...
Definition ValueTypes.h:98
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:339
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:346
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:315
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:279
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:187
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:351
EVT changeElementType(LLVMContext &Context, EVT EltVT) const
Return a VT for a type whose attributes match ourselves with the exception of the element type that i...
Definition ValueTypes.h:121
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:359
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:331
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:484
static LLVM_ABI MachinePointerInfo getUnknownStack(MachineFunction &MF)
Stack memory without other information.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
MakeLibCallOptions & setTypeListBeforeSoften(ArrayRef< EVT > OpsVT, EVT RetVT)
MakeLibCallOptions & setIsSigned(bool Value=true)