LLVM 24.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 // Avoid the generic expansion if the cross-product expansion in
1795 // ExpandIntRes_CLMUL would produce a better result.
1796 if (!TLI.isOperationLegalOrCustomOrPromote(ISD::CLMUL, VT) &&
1797 !(getTypeAction(VT) == TargetLowering::TypeExpandInteger &&
1798 TLI.isOperationLegalOrCustom(
1799 ISD::CLMUL, TLI.getRegisterType(*DAG.getContext(), VT)))) {
1800 if (SDValue Res = TLI.expandCLMUL(N, DAG))
1801 return DAG.getNode(ISD::ANY_EXTEND, DL, VT, Res);
1802 }
1803 SDValue X = GetPromotedInteger(N->getOperand(0));
1804 SDValue Y = GetPromotedInteger(N->getOperand(1));
1805 return DAG.getNode(ISD::CLMUL, DL, VT, X, Y);
1806 }
1807
1808 SDValue X = ZExtPromotedInteger(N->getOperand(0));
1809 SDValue Y = ZExtPromotedInteger(N->getOperand(1));
1810
1811 unsigned OldBits = OldVT.getScalarSizeInBits();
1812 unsigned NewBits = VT.getScalarSizeInBits();
1813 if (NewBits < 2 * OldBits) {
1814 SDValue Clmul = DAG.getNode(ISD::CLMUL, DL, VT, X, Y);
1815 unsigned ShAmt = Opcode == ISD::CLMULH ? OldBits : OldBits - 1;
1816 SDValue Lo = DAG.getNode(ISD::SRL, DL, VT, Clmul,
1817 DAG.getShiftAmountConstant(ShAmt, VT, DL));
1818 SDValue Clmulh = DAG.getNode(ISD::CLMULH, DL, VT, X, Y);
1819 ShAmt = Opcode == ISD::CLMULH ? NewBits - OldBits : NewBits - OldBits + 1;
1820 SDValue Hi = DAG.getNode(ISD::SHL, DL, VT, Clmulh,
1821 DAG.getShiftAmountConstant(ShAmt, VT, DL));
1822 return DAG.getNode(ISD::OR, DL, VT, Lo, Hi);
1823 }
1824
1825 SDValue Clmul = DAG.getNode(ISD::CLMUL, DL, VT, X, Y);
1826 unsigned ShAmt = Opcode == ISD::CLMULH ? OldBits : OldBits - 1;
1827 return DAG.getNode(ISD::SRL, DL, VT, Clmul,
1828 DAG.getShiftAmountConstant(ShAmt, VT, DL));
1829}
1830
1831SDValue DAGTypeLegalizer::PromoteIntRes_PEXT(SDNode *N) {
1832 SDLoc DL(N);
1833 EVT VT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1834 if (!TLI.isOperationLegalOrCustomOrPromote(ISD::PEXT, VT)) {
1835 if (SDValue Res = TLI.expandPEXT(N, DAG))
1836 return DAG.getNode(ISD::ANY_EXTEND, DL, VT, Res);
1837 }
1838 // Only the mask operand needs zero-extension because the implicit AND from
1839 // masking clears the corresponding bits in X anyway.
1840 SDValue X = GetPromotedInteger(N->getOperand(0));
1841 SDValue Y = ZExtPromotedInteger(N->getOperand(1));
1842 return DAG.getNode(ISD::PEXT, DL, VT, X, Y);
1843}
1844
1845SDValue DAGTypeLegalizer::PromoteIntRes_PDEP(SDNode *N) {
1846 SDLoc DL(N);
1847 EVT VT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1848 if (!TLI.isOperationLegalOrCustomOrPromote(ISD::PDEP, VT)) {
1849 if (SDValue Res = TLI.expandPDEP(N, DAG))
1850 return DAG.getNode(ISD::ANY_EXTEND, DL, VT, Res);
1851 }
1852 // Neither operand needs zero-extension because the upper operand bits could
1853 // only result in depositing result bits that will be discarded.
1854 SDValue X = GetPromotedInteger(N->getOperand(0));
1855 SDValue Y = GetPromotedInteger(N->getOperand(1));
1856 return DAG.getNode(ISD::PDEP, DL, VT, X, Y);
1857}
1858
1859SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
1860 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1861 SDValue Res;
1862 SDValue InOp = N->getOperand(0);
1863 SDLoc dl(N);
1864
1865 switch (getTypeAction(InOp.getValueType())) {
1866 default: llvm_unreachable("Unknown type action!");
1869 Res = InOp;
1870 break;
1872 Res = GetPromotedInteger(InOp);
1873 break;
1875 EVT InVT = InOp.getValueType();
1876 assert(InVT.isVector() && "Cannot split scalar types");
1877 ElementCount NumElts = InVT.getVectorElementCount();
1878 assert(NumElts == NVT.getVectorElementCount() &&
1879 "Dst and Src must have the same number of elements");
1881 "Promoted vector type must be a power of two");
1882
1883 SDValue EOp1, EOp2;
1884 GetSplitVector(InOp, EOp1, EOp2);
1885
1886 EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(),
1887 NumElts.divideCoefficientBy(2));
1888 if (N->getOpcode() == ISD::TRUNCATE) {
1889 EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1);
1890 EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2);
1891 } else {
1892 assert(N->getOpcode() == ISD::VP_TRUNCATE &&
1893 "Expected VP_TRUNCATE opcode");
1894 SDValue MaskLo, MaskHi, EVLLo, EVLHi;
1895 std::tie(MaskLo, MaskHi) = SplitMask(N->getOperand(1));
1896 std::tie(EVLLo, EVLHi) =
1897 DAG.SplitEVL(N->getOperand(2), N->getValueType(0), dl);
1898 EOp1 = DAG.getNode(ISD::VP_TRUNCATE, dl, HalfNVT, EOp1, MaskLo, EVLLo);
1899 EOp2 = DAG.getNode(ISD::VP_TRUNCATE, dl, HalfNVT, EOp2, MaskHi, EVLHi);
1900 }
1901 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2);
1902 }
1903 // TODO: VP_TRUNCATE need to handle when TypeWidenVector access to some
1904 // targets.
1906 SDValue WideInOp = GetWidenedVector(InOp);
1907
1908 // Truncate widened InOp.
1909 unsigned NumElem = WideInOp.getValueType().getVectorNumElements();
1910 EVT TruncVT = EVT::getVectorVT(*DAG.getContext(),
1911 N->getValueType(0).getScalarType(), NumElem);
1912 SDValue WideTrunc = DAG.getNode(ISD::TRUNCATE, dl, TruncVT, WideInOp);
1913
1914 // Zero extend so that the elements are of same type as those of NVT
1915 EVT ExtVT = EVT::getVectorVT(*DAG.getContext(), NVT.getVectorElementType(),
1916 NumElem);
1917 SDValue WideExt = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtVT, WideTrunc);
1918
1919 // Extract the low NVT subvector.
1920 SDValue ZeroIdx = DAG.getVectorIdxConstant(0, dl);
1921 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, WideExt, ZeroIdx);
1922 }
1923 }
1924
1925 // Truncate to NVT instead of VT
1926 if (N->getOpcode() == ISD::VP_TRUNCATE)
1927 return DAG.getNode(ISD::VP_TRUNCATE, dl, NVT, Res, N->getOperand(1),
1928 N->getOperand(2));
1929 return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res);
1930}
1931
1932SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
1933 if (ResNo == 1)
1934 return PromoteIntRes_Overflow(N);
1935
1936 // The operation overflowed iff the result in the larger type is not the
1937 // zero extension of its truncation to the original type.
1938 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1939 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
1940 EVT OVT = N->getOperand(0).getValueType();
1941 EVT NVT = LHS.getValueType();
1942 SDLoc dl(N);
1943
1944 // Do the arithmetic in the larger type.
1945 unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
1946 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
1947
1948 // Calculate the overflow flag: zero extend the arithmetic result from
1949 // the original type.
1950 SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT);
1951 // Overflowed if and only if this is not equal to Res.
1952 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
1953
1954 // Use the calculated overflow everywhere.
1955 ReplaceValueWith(SDValue(N, 1), Ofl);
1956
1957 return Res;
1958}
1959
1960// Handle promotion for the ADDE/SUBE/UADDO_CARRY/USUBO_CARRY nodes. Notice that
1961// the third operand of ADDE/SUBE nodes is carry flag, which differs from
1962// the UADDO_CARRY/USUBO_CARRY nodes in that the third operand is carry Boolean.
1963SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO_CARRY(SDNode *N,
1964 unsigned ResNo) {
1965 if (ResNo == 1)
1966 return PromoteIntRes_Overflow(N);
1967
1968 // We need to sign-extend the operands so the carry value computed by the
1969 // wide operation will be equivalent to the carry value computed by the
1970 // narrow operation.
1971 // An UADDO_CARRY can generate carry only if any of the operands has its
1972 // most significant bit set. Sign extension propagates the most significant
1973 // bit into the higher bits which means the extra bit that the narrow
1974 // addition would need (i.e. the carry) will be propagated through the higher
1975 // bits of the wide addition.
1976 // A USUBO_CARRY can generate borrow only if LHS < RHS and this property will
1977 // be preserved by sign extension.
1978 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1979 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1980
1981 EVT ValueVTs[] = {LHS.getValueType(), N->getValueType(1)};
1982
1983 // Do the arithmetic in the wide type.
1984 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N), DAG.getVTList(ValueVTs),
1985 LHS, RHS, N->getOperand(2));
1986
1987 // Update the users of the original carry/borrow value.
1988 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1989
1990 return SDValue(Res.getNode(), 0);
1991}
1992
1993SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO_CARRY(SDNode *N,
1994 unsigned ResNo) {
1995 assert(ResNo == 1 && "Don't know how to promote other results yet.");
1996 return PromoteIntRes_Overflow(N);
1997}
1998
1999SDValue DAGTypeLegalizer::PromoteIntRes_ABS(SDNode *N) {
2000 EVT OVT = N->getValueType(0);
2001 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
2002
2003 // If a larger ABS or SMAX isn't supported by the target, try to expand now.
2004 // If we expand later we'll end up sign extending more than just the sra input
2005 // in sra+xor+sub expansion.
2006 if (!OVT.isVector() &&
2007 !TLI.isOperationLegalOrCustomOrPromote(ISD::ABS, NVT) &&
2008 !TLI.isOperationLegalOrCustomOrPromote(ISD::ABS_MIN_POISON, NVT) &&
2009 !TLI.isOperationLegal(ISD::SMAX, NVT)) {
2010 if (SDValue Res = TLI.expandABS(N, DAG))
2011 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), NVT, Res);
2012 }
2013
2014 SDValue Op0 = SExtPromotedInteger(N->getOperand(0));
2015 return DAG.getNode(ISD::ABS_MIN_POISON, SDLoc(N), Op0.getValueType(), Op0);
2016}
2017
2018SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
2019 // Promote the overflow bit trivially.
2020 if (ResNo == 1)
2021 return PromoteIntRes_Overflow(N);
2022
2023 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
2024 SDLoc DL(N);
2025 EVT SmallVT = LHS.getValueType();
2026
2027 // To determine if the result overflowed in a larger type, we extend the
2028 // input to the larger type, do the multiply (checking if it overflows),
2029 // then also check the high bits of the result to see if overflow happened
2030 // there.
2031 if (N->getOpcode() == ISD::SMULO) {
2032 LHS = SExtPromotedInteger(LHS);
2033 RHS = SExtPromotedInteger(RHS);
2034 } else {
2035 LHS = ZExtPromotedInteger(LHS);
2036 RHS = ZExtPromotedInteger(RHS);
2037 }
2038 SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1));
2039 SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS);
2040
2041 // Overflow occurred if it occurred in the larger type, or if the high part
2042 // of the result does not zero/sign-extend the low part. Check this second
2043 // possibility first.
2044 SDValue Overflow;
2045 if (N->getOpcode() == ISD::UMULO) {
2046 // Unsigned overflow occurred if the high part is non-zero.
2047 unsigned Shift = SmallVT.getScalarSizeInBits();
2048 SDValue Hi =
2049 DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
2050 DAG.getShiftAmountConstant(Shift, Mul.getValueType(), DL));
2051 Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
2052 DAG.getConstant(0, DL, Hi.getValueType()),
2053 ISD::SETNE);
2054 } else {
2055 // Signed overflow occurred if the high part does not sign extend the low.
2056 SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
2057 Mul, DAG.getValueType(SmallVT));
2058 Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
2059 }
2060
2061 // The only other way for overflow to occur is if the multiplication in the
2062 // larger type itself overflowed.
2063 Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow,
2064 SDValue(Mul.getNode(), 1));
2065
2066 // Use the calculated overflow everywhere.
2067 ReplaceValueWith(SDValue(N, 1), Overflow);
2068 return Mul;
2069}
2070
2071SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
2072 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
2073 N->getValueType(0)));
2074}
2075
2076SDValue DAGTypeLegalizer::PromoteIntRes_VSCALE(SDNode *N) {
2077 EVT VT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2078
2079 const APInt &MulImm = N->getConstantOperandAPInt(0);
2080 return DAG.getVScale(SDLoc(N), VT, MulImm.sext(VT.getSizeInBits()));
2081}
2082
2083SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
2084 SDValue Chain = N->getOperand(0); // Get the chain.
2085 SDValue Ptr = N->getOperand(1); // Get the pointer.
2086 EVT VT = N->getValueType(0);
2087 SDLoc dl(N);
2088
2089 MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
2090 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
2091 // The argument is passed as NumRegs registers of type RegVT.
2092
2093 SmallVector<SDValue, 8> Parts(NumRegs);
2094 for (unsigned i = 0; i < NumRegs; ++i) {
2095 Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
2096 N->getConstantOperandVal(3));
2097 Chain = Parts[i].getValue(1);
2098 }
2099
2100 // Handle endianness of the load.
2101 if (DAG.getDataLayout().isBigEndian())
2102 std::reverse(Parts.begin(), Parts.end());
2103
2104 // Assemble the parts in the promoted type.
2105 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2106 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
2107 for (unsigned i = 1; i < NumRegs; ++i) {
2108 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
2109 // Shift it to the right position and "or" it in.
2110 Part = DAG.getNode(
2111 ISD::SHL, dl, NVT, Part,
2112 DAG.getShiftAmountConstant(i * RegVT.getSizeInBits(), NVT, dl));
2113 Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
2114 }
2115
2116 // Modified the chain result - switch anything that used the old chain to
2117 // use the new one.
2118 ReplaceValueWith(SDValue(N, 1), Chain);
2119
2120 return Res;
2121}
2122
2123//===----------------------------------------------------------------------===//
2124// Integer Operand Promotion
2125//===----------------------------------------------------------------------===//
2126
2127/// PromoteIntegerOperand - This method is called when the specified operand of
2128/// the specified node is found to need promotion. At this point, all of the
2129/// result types of the node are known to be legal, but other operands of the
2130/// node may need promotion or expansion as well as the specified one.
2131bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
2132 LLVM_DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG));
2133 SDValue Res = SDValue();
2134 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) {
2135 LLVM_DEBUG(dbgs() << "Node has been custom lowered, done\n");
2136 return false;
2137 }
2138
2139 switch (N->getOpcode()) {
2140 default:
2141 #ifndef NDEBUG
2142 dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
2143 N->dump(&DAG); dbgs() << "\n";
2144 #endif
2145 report_fatal_error("Do not know how to promote this operator's operand!");
2146
2147 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
2149 Res = PromoteIntOp_ANY_EXTEND_VECTOR_INREG(N);
2150 break;
2151 case ISD::ATOMIC_STORE:
2152 Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
2153 break;
2154 case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break;
2155 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
2156 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
2157 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
2158 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
2159 case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
2160 case ISD::COND_LOOP:
2161 Res = PromoteIntOp_COND_LOOP(N, OpNo);
2162 break;
2163 case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
2164 case ISD::FAKE_USE:
2165 Res = PromoteIntOp_FAKE_USE(N);
2166 break;
2168 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);
2169 break;
2170 case ISD::SPLAT_VECTOR:
2172 Res = PromoteIntOp_ScalarOp(N);
2173 break;
2174 case ISD::VSELECT:
2175 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
2176 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
2177 case ISD::VP_SETCC:
2178 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
2179 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
2180 case ISD::VP_SIGN_EXTEND: Res = PromoteIntOp_VP_SIGN_EXTEND(N); break;
2181 case ISD::VP_SINT_TO_FP:
2182 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
2183 case ISD::STRICT_SINT_TO_FP: Res = PromoteIntOp_STRICT_SINT_TO_FP(N); break;
2184 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
2185 OpNo); break;
2186 case ISD::VP_STORE:
2187 Res = PromoteIntOp_VP_STORE(cast<VPStoreSDNode>(N), OpNo);
2188 break;
2189 case ISD::MSTORE: Res = PromoteIntOp_MSTORE(cast<MaskedStoreSDNode>(N),
2190 OpNo); break;
2191 case ISD::MLOAD: Res = PromoteIntOp_MLOAD(cast<MaskedLoadSDNode>(N),
2192 OpNo); break;
2193 case ISD::MGATHER: Res = PromoteIntOp_MGATHER(cast<MaskedGatherSDNode>(N),
2194 OpNo); break;
2195 case ISD::MSCATTER: Res = PromoteIntOp_MSCATTER(cast<MaskedScatterSDNode>(N),
2196 OpNo); break;
2198 Res = PromoteIntOp_VECTOR_COMPRESS(N, OpNo);
2199 break;
2200 case ISD::VP_TRUNCATE:
2201 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
2202 case ISD::BF16_TO_FP:
2203 case ISD::FP16_TO_FP:
2204 case ISD::VP_UINT_TO_FP:
2205 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
2207 Res = PromoteIntOp_CONVERT_FROM_ARBITRARY_FP(N);
2208 break;
2210 case ISD::STRICT_UINT_TO_FP: Res = PromoteIntOp_STRICT_UINT_TO_FP(N); break;
2211 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
2212 case ISD::VP_ZERO_EXTEND: Res = PromoteIntOp_VP_ZERO_EXTEND(N); break;
2213 case ISD::EXTRACT_SUBVECTOR: Res = PromoteIntOp_EXTRACT_SUBVECTOR(N); break;
2214 case ISD::INSERT_SUBVECTOR: Res = PromoteIntOp_INSERT_SUBVECTOR(N); break;
2215
2216 case ISD::SHL:
2217 case ISD::SRA:
2218 case ISD::SRL:
2219 case ISD::ROTL:
2220 case ISD::ROTR:
2221 case ISD::SSHLSAT:
2222 case ISD::USHLSAT:
2223 Res = PromoteIntOp_Shift(N);
2224 break;
2225
2226 case ISD::SCMP:
2227 case ISD::UCMP: Res = PromoteIntOp_CMP(N); break;
2228
2229 case ISD::FSHL:
2230 case ISD::FSHR: Res = PromoteIntOp_FunnelShift(N); break;
2231
2232 case ISD::FRAMEADDR:
2233 case ISD::RETURNADDR: Res = PromoteIntOp_FRAMERETURNADDR(N); break;
2234
2235 case ISD::SMULFIX:
2236 case ISD::SMULFIXSAT:
2237 case ISD::UMULFIX:
2238 case ISD::UMULFIXSAT:
2239 case ISD::SDIVFIX:
2240 case ISD::SDIVFIXSAT:
2241 case ISD::UDIVFIX:
2242 case ISD::UDIVFIXSAT: Res = PromoteIntOp_FIX(N); break;
2243 case ISD::FPOWI:
2244 case ISD::STRICT_FPOWI:
2245 case ISD::FLDEXP:
2246 case ISD::STRICT_FLDEXP: Res = PromoteIntOp_ExpOp(N); break;
2247 case ISD::VECREDUCE_ADD:
2248 case ISD::VECREDUCE_MUL:
2249 case ISD::VECREDUCE_AND:
2250 case ISD::VECREDUCE_OR:
2251 case ISD::VECREDUCE_XOR:
2255 case ISD::VECREDUCE_UMIN: Res = PromoteIntOp_VECREDUCE(N); break;
2256 case ISD::VP_REDUCE_ADD:
2257 case ISD::VP_REDUCE_MUL:
2258 case ISD::VP_REDUCE_AND:
2259 case ISD::VP_REDUCE_OR:
2260 case ISD::VP_REDUCE_XOR:
2261 case ISD::VP_REDUCE_SMAX:
2262 case ISD::VP_REDUCE_SMIN:
2263 case ISD::VP_REDUCE_UMAX:
2264 case ISD::VP_REDUCE_UMIN:
2265 Res = PromoteIntOp_VP_REDUCE(N, OpNo);
2266 break;
2267
2268 case ISD::SET_ROUNDING: Res = PromoteIntOp_SET_ROUNDING(N); break;
2269 case ISD::STACKMAP:
2270 Res = PromoteIntOp_STACKMAP(N, OpNo);
2271 break;
2272 case ISD::PATCHPOINT:
2273 Res = PromoteIntOp_PATCHPOINT(N, OpNo);
2274 break;
2276 Res = PromoteIntOp_WRITE_REGISTER(N, OpNo);
2277 break;
2278 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
2279 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
2280 Res = PromoteIntOp_VP_STRIDED(N, OpNo);
2281 break;
2282 case ISD::EXPERIMENTAL_VP_SPLICE:
2283 Res = PromoteIntOp_VP_SPLICE(N, OpNo);
2284 break;
2286 Res = PromoteIntOp_VECTOR_HISTOGRAM(N, OpNo);
2287 break;
2289 case ISD::CTTZ_ELTS:
2291 Res = PromoteIntOp_UnaryBooleanVectorOp(N, OpNo);
2292 break;
2294 Res = PromoteIntOp_GET_ACTIVE_LANE_MASK(N);
2295 break;
2296 case ISD::MASKED_UDIV:
2297 case ISD::MASKED_SDIV:
2298 case ISD::MASKED_UREM:
2299 case ISD::MASKED_SREM:
2300 Res = PromoteIntOp_MaskedBinOp(N, OpNo);
2301 break;
2305 Res = PromoteIntOp_PARTIAL_REDUCE_MLA(N);
2306 break;
2309 Res = PromoteIntOp_LOOP_DEPENDENCE_MASK(N);
2310 break;
2311 }
2312
2313 // If the result is null, the sub-method took care of registering results etc.
2314 if (!Res.getNode()) return false;
2315
2316 // If the result is N, the sub-method updated N in place. Tell the legalizer
2317 // core about this.
2318 if (Res.getNode() == N)
2319 return true;
2320
2321 const bool IsStrictFp = N->isStrictFPOpcode();
2322 assert(Res.getValueType() == N->getValueType(0) &&
2323 N->getNumValues() == (IsStrictFp ? 2 : 1) &&
2324 "Invalid operand expansion");
2325 LLVM_DEBUG(dbgs() << "Replacing: "; N->dump(&DAG); dbgs() << " with: ";
2326 Res.dump());
2327
2328 ReplaceValueWith(SDValue(N, 0), Res);
2329 if (IsStrictFp)
2330 ReplaceValueWith(SDValue(N, 1), SDValue(Res.getNode(), 1));
2331
2332 return false;
2333}
2334
2335// These operands can be either sign extended or zero extended as long as we
2336// treat them the same. If an extension is free, choose that. Otherwise, follow
2337// target preference.
2338void DAGTypeLegalizer::SExtOrZExtPromotedOperands(SDValue &LHS, SDValue &RHS) {
2339 SDValue OpL = GetPromotedInteger(LHS);
2340 SDValue OpR = GetPromotedInteger(RHS);
2341
2342 if (TLI.isSExtCheaperThanZExt(LHS.getValueType(), OpL.getValueType())) {
2343 // The target would prefer to promote the comparison operand with sign
2344 // extension. Honor that unless the promoted values are already zero
2345 // extended.
2346 unsigned OpLEffectiveBits =
2347 DAG.computeKnownBits(OpL).countMaxActiveBits();
2348 unsigned OpREffectiveBits =
2349 DAG.computeKnownBits(OpR).countMaxActiveBits();
2350 if (OpLEffectiveBits <= LHS.getScalarValueSizeInBits() &&
2351 OpREffectiveBits <= RHS.getScalarValueSizeInBits()) {
2352 LHS = OpL;
2353 RHS = OpR;
2354 return;
2355 }
2356
2357 // The promoted values aren't zero extended, use a sext_inreg.
2358 LHS = SExtPromotedInteger(LHS);
2359 RHS = SExtPromotedInteger(RHS);
2360 return;
2361 }
2362
2363 // Prefer to promote the comparison operand with zero extension.
2364
2365 // If the width of OpL/OpR excluding the duplicated sign bits is no greater
2366 // than the width of LHS/RHS, we can avoid inserting a zext_inreg operation
2367 // that we might not be able to remove.
2368 unsigned OpLEffectiveBits = DAG.ComputeMaxSignificantBits(OpL);
2369 unsigned OpREffectiveBits = DAG.ComputeMaxSignificantBits(OpR);
2370 if (OpLEffectiveBits <= LHS.getScalarValueSizeInBits() &&
2371 OpREffectiveBits <= RHS.getScalarValueSizeInBits()) {
2372 LHS = OpL;
2373 RHS = OpR;
2374 return;
2375 }
2376
2377 // Otherwise, use zext_inreg.
2378 LHS = ZExtPromotedInteger(LHS);
2379 RHS = ZExtPromotedInteger(RHS);
2380}
2381
2382/// PromoteSetCCOperands - Promote the operands of a comparison. This code is
2383/// shared among BR_CC, SELECT_CC, and SETCC handlers.
2384void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &LHS, SDValue &RHS,
2385 ISD::CondCode CCCode) {
2386 // We have to insert explicit sign or zero extends. Note that we could
2387 // insert sign extends for ALL conditions. For those operations where either
2388 // zero or sign extension would be valid, we ask the target which extension
2389 // it would prefer.
2390
2391 // Signed comparisons always require sign extension.
2392 if (ISD::isSignedIntSetCC(CCCode)) {
2393 LHS = SExtPromotedInteger(LHS);
2394 RHS = SExtPromotedInteger(RHS);
2395 return;
2396 }
2397
2399 "Unknown integer comparison!");
2400
2401 SExtOrZExtPromotedOperands(LHS, RHS);
2402}
2403
2404SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
2405 SDValue Op = GetPromotedInteger(N->getOperand(0));
2406 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op);
2407}
2408
2409SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND_VECTOR_INREG(SDNode *N) {
2410 SDValue Op = GetPromotedInteger(N->getOperand(0));
2411 EVT ResVT = N->getValueType(0);
2412 EVT OpVT = Op.getValueType();
2413 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), OpVT.getScalarType(),
2414 ResVT.getVectorNumElements());
2415 Op = DAG.getExtractSubvector(SDLoc(Op), NewVT, Op, 0);
2416 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), ResVT, Op);
2417}
2418
2419SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
2420 SDValue Op1 = GetPromotedInteger(N->getOperand(1));
2421 return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(),
2422 N->getChain(), Op1, N->getBasePtr(), N->getMemOperand());
2423}
2424
2425SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
2426 EVT OutVT = N->getValueType(0);
2427 SDValue InOp = N->getOperand(0);
2428 EVT InVT = InOp.getValueType();
2429 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
2430 SDLoc dl(N);
2431
2432 switch (getTypeAction(InVT)) {
2434 // TODO: Handle big endian & vector input type.
2435 if (OutVT.isVector() && !InVT.isVector() &&
2436 DAG.getDataLayout().isLittleEndian()) {
2437 EVT EltVT = OutVT.getVectorElementType();
2438 TypeSize EltSize = EltVT.getSizeInBits();
2439 TypeSize NInSize = NInVT.getSizeInBits();
2440
2441 if (NInSize.hasKnownScalarFactor(EltSize)) {
2442 unsigned NumEltsWithPadding = NInSize.getKnownScalarFactor(EltSize);
2443 EVT WideVecVT =
2444 EVT::getVectorVT(*DAG.getContext(), EltVT, NumEltsWithPadding);
2445
2446 if (isTypeLegal(WideVecVT)) {
2447 SDValue Promoted = GetPromotedInteger(InOp);
2448 SDValue Cast = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Promoted);
2449 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, Cast,
2450 DAG.getVectorIdxConstant(0, dl));
2451 }
2452 }
2453 }
2454
2455 break;
2456 }
2457 default:
2458 break;
2459 }
2460
2461 // This should only occur in unusual situations like bitcasting to an
2462 // x86_fp80, so just turn it into a store+load
2463 return CreateStackStoreLoad(InOp, OutVT);
2464}
2465
2466SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
2467 assert(OpNo == 2 && "Don't know how to promote this operand!");
2468
2469 SDValue LHS = N->getOperand(2);
2470 SDValue RHS = N->getOperand(3);
2471 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
2472
2473 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
2474 // legal types.
2475 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2476 N->getOperand(1), LHS, RHS, N->getOperand(4)),
2477 0);
2478}
2479
2480SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
2481 assert(OpNo == 1 && "only know how to promote condition");
2482
2483 // Promote all the way up to the canonical SetCC type.
2484 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
2485
2486 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
2487 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
2488 N->getOperand(2)), 0);
2489}
2490
2491SDValue DAGTypeLegalizer::PromoteIntOp_COND_LOOP(SDNode *N, unsigned OpNo) {
2492 assert(OpNo == 1 && "only know how to promote condition");
2493
2494 // Promote all the way up to the canonical SetCC type.
2495 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
2496
2497 // The chain (Op#0) is always a legal type.
2498 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond), 0);
2499}
2500
2501SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
2502 // Since the result type is legal, the operands must promote to it.
2503 EVT OVT = N->getOperand(0).getValueType();
2504 SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
2505 SDValue Hi = GetPromotedInteger(N->getOperand(1));
2506 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
2507 SDLoc dl(N);
2508
2509 Hi = DAG.getNode(
2510 ISD::SHL, dl, N->getValueType(0), Hi,
2511 DAG.getShiftAmountConstant(OVT.getSizeInBits(), N->getValueType(0), dl));
2512 return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
2513}
2514
2515SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
2516 // The vector type is legal but the element type is not. This implies
2517 // that the vector is a power-of-two in length and that the element
2518 // type does not have a strange size (eg: it is not i1).
2519 EVT VecVT = N->getValueType(0);
2520 unsigned NumElts = VecVT.getVectorNumElements();
2521 assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) &&
2522 "Legal vector of one illegal element?");
2523
2524 // Promote the inserted value. The type does not need to match the
2525 // vector element type. Check that any extra bits introduced will be
2526 // truncated away.
2527 assert(N->getOperand(0).getValueSizeInBits() >=
2528 N->getValueType(0).getScalarSizeInBits() &&
2529 "Type of inserted value narrower than vector element type!");
2530
2532 for (unsigned i = 0; i < NumElts; ++i)
2533 NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
2534
2535 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2536}
2537
2538SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
2539 unsigned OpNo) {
2540 if (OpNo == 1) {
2541 // Promote the inserted value. This is valid because the type does not
2542 // have to match the vector element type.
2543
2544 // Check that any extra bits introduced will be truncated away.
2545 assert(N->getOperand(1).getValueSizeInBits() >=
2546 N->getValueType(0).getScalarSizeInBits() &&
2547 "Type of inserted value narrower than vector element type!");
2548 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2549 GetPromotedInteger(N->getOperand(1)),
2550 N->getOperand(2)),
2551 0);
2552 }
2553
2554 assert(OpNo == 2 && "Different operand and result vector types?");
2555
2556 // Promote the index.
2557 SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N),
2558 TLI.getVectorIdxTy(DAG.getDataLayout()));
2559 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2560 N->getOperand(1), Idx), 0);
2561}
2562
2563SDValue DAGTypeLegalizer::PromoteIntOp_ScalarOp(SDNode *N) {
2564 SDValue Op = GetPromotedInteger(N->getOperand(0));
2565
2566 // Integer SPLAT_VECTOR/SCALAR_TO_VECTOR operands are implicitly truncated,
2567 // so just promote the operand in place.
2568 return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
2569}
2570
2571SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
2572 assert(OpNo == 0 && "Only know how to promote the condition!");
2573 SDValue Cond = N->getOperand(0);
2574 EVT OpTy = N->getOperand(1).getValueType();
2575
2576 if (N->getOpcode() == ISD::VSELECT)
2577 if (SDValue Res = WidenVSELECTMask(N))
2578 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
2579 Res, N->getOperand(1), N->getOperand(2));
2580
2581 // Promote all the way up to the canonical SetCC type.
2582 EVT OpVT = N->getOpcode() == ISD::SELECT ? OpTy.getScalarType() : OpTy;
2583 Cond = PromoteTargetBoolean(Cond, OpVT);
2584
2585 return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1),
2586 N->getOperand(2)), 0);
2587}
2588
2589SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
2590 assert(OpNo == 0 && "Don't know how to promote this operand!");
2591
2592 SDValue LHS = N->getOperand(0);
2593 SDValue RHS = N->getOperand(1);
2594 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
2595
2596 // The CC (#4) and the possible return values (#2 and #3) have legal types.
2597 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
2598 N->getOperand(3), N->getOperand(4)), 0);
2599}
2600
2601SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
2602 assert(OpNo == 0 && "Don't know how to promote this operand!");
2603
2604 SDValue LHS = N->getOperand(0);
2605 SDValue RHS = N->getOperand(1);
2606 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
2607
2608 // The CC (#2) is always legal.
2609 if (N->getOpcode() == ISD::SETCC)
2610 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
2611
2612 assert(N->getOpcode() == ISD::VP_SETCC && "Expected VP_SETCC opcode");
2613
2614 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
2615 N->getOperand(3), N->getOperand(4)),
2616 0);
2617}
2618
2619SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
2620 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2621 ZExtPromotedInteger(N->getOperand(1))), 0);
2622}
2623
2624SDValue DAGTypeLegalizer::PromoteIntOp_CMP(SDNode *N) {
2625 SDValue LHS = N->getOperand(0);
2626 SDValue RHS = N->getOperand(1);
2627
2628 if (N->getOpcode() == ISD::SCMP) {
2629 LHS = SExtPromotedInteger(LHS);
2630 RHS = SExtPromotedInteger(RHS);
2631 } else {
2632 SExtOrZExtPromotedOperands(LHS, RHS);
2633 }
2634
2635 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS), 0);
2636}
2637
2638SDValue DAGTypeLegalizer::PromoteIntOp_FunnelShift(SDNode *N) {
2639 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1),
2640 ZExtPromotedInteger(N->getOperand(2))), 0);
2641}
2642
2643SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
2644 SDValue Op = GetPromotedInteger(N->getOperand(0));
2645 SDLoc dl(N);
2646 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
2647 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
2648 Op, DAG.getValueType(N->getOperand(0).getValueType()));
2649}
2650
2651SDValue DAGTypeLegalizer::PromoteIntOp_VP_SIGN_EXTEND(SDNode *N) {
2652 SDLoc dl(N);
2653 EVT VT = N->getValueType(0);
2654 SDValue Op = GetPromotedInteger(N->getOperand(0));
2655 // FIXME: There is no VP_ANY_EXTEND yet.
2656 Op = DAG.getNode(ISD::VP_ZERO_EXTEND, dl, VT, Op, N->getOperand(1),
2657 N->getOperand(2));
2658 unsigned Diff =
2659 VT.getScalarSizeInBits() - N->getOperand(0).getScalarValueSizeInBits();
2660 SDValue ShAmt = DAG.getShiftAmountConstant(Diff, VT, dl);
2661 // FIXME: There is no VP_SIGN_EXTEND_INREG so use a pair of shifts.
2662 SDValue Shl = DAG.getNode(ISD::VP_SHL, dl, VT, Op, ShAmt, N->getOperand(1),
2663 N->getOperand(2));
2664 return DAG.getNode(ISD::VP_SRA, dl, VT, Shl, ShAmt, N->getOperand(1),
2665 N->getOperand(2));
2666}
2667
2668SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
2669 if (N->getOpcode() == ISD::VP_SINT_TO_FP)
2670 return SDValue(DAG.UpdateNodeOperands(N,
2671 SExtPromotedInteger(N->getOperand(0)),
2672 N->getOperand(1), N->getOperand(2)),
2673 0);
2674 return SDValue(DAG.UpdateNodeOperands(N,
2675 SExtPromotedInteger(N->getOperand(0))), 0);
2676}
2677
2678SDValue DAGTypeLegalizer::PromoteIntOp_STRICT_SINT_TO_FP(SDNode *N) {
2679 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2680 SExtPromotedInteger(N->getOperand(1))), 0);
2681}
2682
2683SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
2684 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2685 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
2686 SDLoc dl(N);
2687
2688 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
2689
2690 // Truncate the value and store the result.
2691 return DAG.getTruncStore(Ch, dl, Val, Ptr,
2692 N->getMemoryVT(), N->getMemOperand());
2693}
2694
2695SDValue DAGTypeLegalizer::PromoteIntOp_VP_STORE(VPStoreSDNode *N,
2696 unsigned OpNo) {
2697
2698 assert(OpNo == 1 && "Unexpected operand for promotion");
2699 assert(!N->isIndexed() && "expecting unindexed vp_store!");
2700
2701 SDValue DataOp = GetPromotedInteger(N->getValue());
2702 return DAG.getTruncStoreVP(N->getChain(), SDLoc(N), DataOp, N->getBasePtr(),
2703 N->getMask(), N->getVectorLength(),
2704 N->getMemoryVT(), N->getMemOperand(),
2705 N->isCompressingStore());
2706}
2707
2708SDValue DAGTypeLegalizer::PromoteIntOp_MSTORE(MaskedStoreSDNode *N,
2709 unsigned OpNo) {
2710 SDValue DataOp = N->getValue();
2711 SDValue Mask = N->getMask();
2712
2713 if (OpNo == 4) {
2714 // The Mask. Update in place.
2715 EVT DataVT = DataOp.getValueType();
2716 Mask = PromoteTargetBoolean(Mask, DataVT);
2717 SmallVector<SDValue, 4> NewOps(N->ops());
2718 NewOps[4] = Mask;
2719 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2720 }
2721
2722 assert(OpNo == 1 && "Unexpected operand for promotion");
2723 DataOp = GetPromotedInteger(DataOp);
2724
2725 return DAG.getMaskedStore(N->getChain(), SDLoc(N), DataOp, N->getBasePtr(),
2726 N->getOffset(), Mask, N->getMemoryVT(),
2727 N->getMemOperand(), N->getAddressingMode(),
2728 /*IsTruncating*/ true, N->isCompressingStore());
2729}
2730
2731SDValue DAGTypeLegalizer::PromoteIntOp_MLOAD(MaskedLoadSDNode *N,
2732 unsigned OpNo) {
2733 assert(OpNo == 3 && "Only know how to promote the mask!");
2734 EVT DataVT = N->getValueType(0);
2735 SDValue Mask = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
2736 SmallVector<SDValue, 4> NewOps(N->ops());
2737 NewOps[OpNo] = Mask;
2738 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
2739 if (Res == N)
2740 return SDValue(Res, 0);
2741
2742 // Update triggered CSE, do our own replacement since caller can't.
2743 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
2744 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
2745 return SDValue();
2746}
2747
2748SDValue DAGTypeLegalizer::PromoteIntOp_MGATHER(MaskedGatherSDNode *N,
2749 unsigned OpNo) {
2750 SmallVector<SDValue, 5> NewOps(N->ops());
2751
2752 if (OpNo == 2) {
2753 // The Mask
2754 EVT DataVT = N->getValueType(0);
2755 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
2756 } else if (OpNo == 4) {
2757 // The Index
2758 if (N->isIndexSigned())
2759 // Need to sign extend the index since the bits will likely be used.
2760 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
2761 else
2762 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
2763 } else
2764 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
2765
2766 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
2767 if (Res == N)
2768 return SDValue(Res, 0);
2769
2770 // Update triggered CSE, do our own replacement since caller can't.
2771 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
2772 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
2773 return SDValue();
2774}
2775
2776SDValue DAGTypeLegalizer::PromoteIntOp_MSCATTER(MaskedScatterSDNode *N,
2777 unsigned OpNo) {
2778 bool TruncateStore = N->isTruncatingStore();
2779 SmallVector<SDValue, 5> NewOps(N->ops());
2780
2781 if (OpNo == 2) {
2782 // The Mask
2783 EVT DataVT = N->getValue().getValueType();
2784 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
2785 } else if (OpNo == 4) {
2786 // The Index
2787 if (N->isIndexSigned())
2788 // Need to sign extend the index since the bits will likely be used.
2789 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
2790 else
2791 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
2792 } else {
2793 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
2794 TruncateStore = true;
2795 }
2796
2797 return DAG.getMaskedScatter(DAG.getVTList(MVT::Other), N->getMemoryVT(),
2798 SDLoc(N), NewOps, N->getMemOperand(),
2799 N->getIndexType(), TruncateStore);
2800}
2801
2802SDValue DAGTypeLegalizer::PromoteIntOp_VECTOR_COMPRESS(SDNode *N,
2803 unsigned OpNo) {
2804 assert(OpNo == 1 && "Can only promote VECTOR_COMPRESS mask.");
2805 SDValue Vec = N->getOperand(0);
2806 EVT VT = Vec.getValueType();
2807 SDValue Passthru = N->getOperand(2);
2808 SDValue Mask = PromoteTargetBoolean(N->getOperand(1), VT);
2809 return DAG.getNode(ISD::VECTOR_COMPRESS, SDLoc(N), VT, Vec, Mask, Passthru);
2810}
2811
2812SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
2813 SDValue Op = GetPromotedInteger(N->getOperand(0));
2814 if (N->getOpcode() == ISD::VP_TRUNCATE)
2815 return DAG.getNode(ISD::VP_TRUNCATE, SDLoc(N), N->getValueType(0), Op,
2816 N->getOperand(1), N->getOperand(2));
2817 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op);
2818}
2819
2820SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
2821 if (N->getOpcode() == ISD::VP_UINT_TO_FP)
2822 return SDValue(DAG.UpdateNodeOperands(N,
2823 ZExtPromotedInteger(N->getOperand(0)),
2824 N->getOperand(1), N->getOperand(2)),
2825 0);
2826 return SDValue(DAG.UpdateNodeOperands(N,
2827 ZExtPromotedInteger(N->getOperand(0))), 0);
2828}
2829
2830SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_FROM_ARBITRARY_FP(SDNode *N) {
2831 return SDValue(DAG.UpdateNodeOperands(N, GetPromotedInteger(N->getOperand(0)),
2832 N->getOperand(1)),
2833 0);
2834}
2835
2836SDValue DAGTypeLegalizer::PromoteIntOp_STRICT_UINT_TO_FP(SDNode *N) {
2837 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2838 ZExtPromotedInteger(N->getOperand(1))), 0);
2839}
2840
2841SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
2842 SDLoc dl(N);
2843 SDValue Src = N->getOperand(0);
2844 SDValue Op = GetPromotedInteger(Src);
2845 EVT VT = N->getValueType(0);
2846
2847 // If this zext has the nneg flag and the target prefers sext, see if the
2848 // promoted input is already sign extended.
2849 // TODO: Should we have some way to set nneg on ISD::AND instead?
2850 if (N->getFlags().hasNonNeg() && Op.getValueType() == VT &&
2851 TLI.isSExtCheaperThanZExt(Src.getValueType(), VT)) {
2852 unsigned OpEffectiveBits = DAG.ComputeMaxSignificantBits(Op);
2853 if (OpEffectiveBits <= Src.getScalarValueSizeInBits())
2854 return Op;
2855 }
2856
2857 Op = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op);
2858 return DAG.getZeroExtendInReg(Op, dl, Src.getValueType());
2859}
2860
2861SDValue DAGTypeLegalizer::PromoteIntOp_VP_ZERO_EXTEND(SDNode *N) {
2862 SDLoc dl(N);
2863 EVT VT = N->getValueType(0);
2864 SDValue Op = GetPromotedInteger(N->getOperand(0));
2865 // FIXME: There is no VP_ANY_EXTEND yet.
2866 Op = DAG.getNode(ISD::VP_ZERO_EXTEND, dl, VT, Op, N->getOperand(1),
2867 N->getOperand(2));
2868 return DAG.getVPZeroExtendInReg(Op, N->getOperand(1), N->getOperand(2), dl,
2869 N->getOperand(0).getValueType());
2870}
2871
2872SDValue DAGTypeLegalizer::PromoteIntOp_FIX(SDNode *N) {
2873 SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
2874 return SDValue(
2875 DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1), Op2), 0);
2876}
2877
2878SDValue DAGTypeLegalizer::PromoteIntOp_FRAMERETURNADDR(SDNode *N) {
2879 // Promote the RETURNADDR/FRAMEADDR argument to a supported integer width.
2880 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
2881 return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
2882}
2883
2884SDValue DAGTypeLegalizer::PromoteIntOp_ExpOp(SDNode *N) {
2885 bool IsStrict = N->isStrictFPOpcode();
2886 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
2887
2888 bool IsPowI =
2889 N->getOpcode() == ISD::FPOWI || N->getOpcode() == ISD::STRICT_FPOWI;
2890 unsigned OpOffset = IsStrict ? 1 : 0;
2891
2892 // The integer operand is the last operand in FPOWI (or FLDEXP) (so the result
2893 // and floating point operand is already type legalized).
2894 RTLIB::Libcall LC = IsPowI ? RTLIB::getPOWI(N->getValueType(0))
2895 : RTLIB::getLDEXP(N->getValueType(0));
2896
2897 RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(LC);
2898 if (LCImpl == RTLIB::Unsupported) {
2899 // Scalarize vector FPOWI instead of promoting the type. This allows the
2900 // scalar FPOWIs to be visited and converted to libcalls before promoting
2901 // the type.
2902 // FIXME: This should be done in LegalizeVectorOps/LegalizeDAG, but call
2903 // lowering needs the unpromoted EVT.
2904 if (IsPowI && N->getValueType(0).isVector())
2905 return DAG.UnrollVectorOp(N);
2906 SmallVector<SDValue, 3> NewOps(N->ops());
2907 NewOps[1 + OpOffset] = SExtPromotedInteger(N->getOperand(1 + OpOffset));
2908 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2909 }
2910
2911 // We can't just promote the exponent type in FPOWI, since we want to lower
2912 // the node to a libcall and we if we promote to a type larger than
2913 // sizeof(int) the libcall might not be according to the targets ABI. Instead
2914 // we rewrite to a libcall here directly, letting makeLibCall handle promotion
2915 // if the target accepts it according to shouldSignExtendTypeInLibCall.
2916
2917 // A wider-than-int exponent can't be passed in an int (there's no wider
2918 // libcall), so bail like the soften/expand paths. A narrower one is
2919 // sign-extended to int by the makeLibCall below.
2920 if (N->getOperand(1 + OpOffset).getScalarValueSizeInBits() >
2921 DAG.getLibInfo().getIntSize()) {
2922 const Function &Fn = DAG.getMachineFunction().getFunction();
2923 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
2924 Twine(IsPowI ? "powi" : "ldexp") +
2925 " exponent does not match sizeof(int)",
2926 Fn, N->getDebugLoc()));
2927 if (IsStrict)
2928 ReplaceValueWith(SDValue(N, 1), Chain);
2929 ReplaceValueWith(SDValue(N, 0), DAG.getPOISON(N->getValueType(0)));
2930 return SDValue();
2931 }
2932
2933 TargetLowering::MakeLibCallOptions CallOptions;
2934 CallOptions.setIsSigned(true);
2935 SDValue Ops[2] = {N->getOperand(0 + OpOffset), N->getOperand(1 + OpOffset)};
2936 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
2937 DAG, LCImpl, N->getValueType(0), Ops, CallOptions, SDLoc(N), Chain);
2938 ReplaceValueWith(SDValue(N, 0), Tmp.first);
2939 if (IsStrict)
2940 ReplaceValueWith(SDValue(N, 1), Tmp.second);
2941 return SDValue();
2942}
2943
2945 switch (N->getOpcode()) {
2946 default:
2947 llvm_unreachable("Expected integer vector reduction");
2948 case ISD::VECREDUCE_ADD:
2949 case ISD::VECREDUCE_MUL:
2950 case ISD::VECREDUCE_AND:
2951 case ISD::VECREDUCE_OR:
2952 case ISD::VECREDUCE_XOR:
2953 case ISD::VP_REDUCE_ADD:
2954 case ISD::VP_REDUCE_MUL:
2955 case ISD::VP_REDUCE_AND:
2956 case ISD::VP_REDUCE_OR:
2957 case ISD::VP_REDUCE_XOR:
2958 return ISD::ANY_EXTEND;
2961 case ISD::VP_REDUCE_SMAX:
2962 case ISD::VP_REDUCE_SMIN:
2963 return ISD::SIGN_EXTEND;
2966 case ISD::VP_REDUCE_UMAX:
2967 case ISD::VP_REDUCE_UMIN:
2968 return ISD::ZERO_EXTEND;
2969 }
2970}
2971
2972SDValue DAGTypeLegalizer::PromoteIntOpVectorReduction(SDNode *N, SDValue V) {
2973 switch (getExtendForIntVecReduction(N)) {
2974 default:
2975 llvm_unreachable("Impossible extension kind for integer reduction");
2976 case ISD::ANY_EXTEND:
2977 return GetPromotedInteger(V);
2978 case ISD::SIGN_EXTEND:
2979 return SExtPromotedInteger(V);
2980 case ISD::ZERO_EXTEND:
2981 return ZExtPromotedInteger(V);
2982 }
2983}
2984
2985SDValue DAGTypeLegalizer::PromoteIntOp_VECREDUCE(SDNode *N) {
2986 SDLoc dl(N);
2987 SDValue Op = PromoteIntOpVectorReduction(N, N->getOperand(0));
2988
2989 EVT OrigEltVT = N->getOperand(0).getValueType().getVectorElementType();
2990 EVT InVT = Op.getValueType();
2991 EVT EltVT = InVT.getVectorElementType();
2992 EVT ResVT = N->getValueType(0);
2993 unsigned Opcode = N->getOpcode();
2994
2995 // An i1 vecreduce_xor is equivalent to vecreduce_add, use that instead if
2996 // vecreduce_xor is not legal
2997 if (Opcode == ISD::VECREDUCE_XOR && OrigEltVT == MVT::i1 &&
2998 !TLI.isOperationLegalOrCustom(ISD::VECREDUCE_XOR, InVT) &&
2999 TLI.isOperationLegalOrCustom(ISD::VECREDUCE_ADD, InVT))
3000 Opcode = ISD::VECREDUCE_ADD;
3001
3002 // An i1 vecreduce_or is equivalent to vecreduce_umax, use that instead if
3003 // vecreduce_or is not legal
3004 else if (Opcode == ISD::VECREDUCE_OR && OrigEltVT == MVT::i1 &&
3005 !TLI.isOperationLegalOrCustom(ISD::VECREDUCE_OR, InVT) &&
3006 TLI.isOperationLegalOrCustom(ISD::VECREDUCE_UMAX, InVT)) {
3007 Opcode = ISD::VECREDUCE_UMAX;
3008 // Can't use promoteTargetBoolean here because we still need
3009 // to either sign_ext or zero_ext in the undefined case.
3010 switch (TLI.getBooleanContents(InVT)) {
3013 Op = ZExtPromotedInteger(N->getOperand(0));
3014 break;
3016 Op = SExtPromotedInteger(N->getOperand(0));
3017 break;
3018 }
3019 }
3020
3021 // An i1 vecreduce_and is equivalent to vecreduce_umin, use that instead if
3022 // vecreduce_and is not legal
3023 else if (Opcode == ISD::VECREDUCE_AND && OrigEltVT == MVT::i1 &&
3024 !TLI.isOperationLegalOrCustom(ISD::VECREDUCE_AND, InVT) &&
3025 TLI.isOperationLegalOrCustom(ISD::VECREDUCE_UMIN, InVT)) {
3026 Opcode = ISD::VECREDUCE_UMIN;
3027 // Can't use promoteTargetBoolean here because we still need
3028 // to either sign_ext or zero_ext in the undefined case.
3029 switch (TLI.getBooleanContents(InVT)) {
3032 Op = ZExtPromotedInteger(N->getOperand(0));
3033 break;
3035 Op = SExtPromotedInteger(N->getOperand(0));
3036 break;
3037 }
3038 }
3039
3040 if (ResVT.bitsGE(EltVT))
3041 return DAG.getNode(Opcode, SDLoc(N), ResVT, Op);
3042
3043 // Result size must be >= element size. If this is not the case after
3044 // promotion, also promote the result type and then truncate.
3045 SDValue Reduce = DAG.getNode(Opcode, dl, EltVT, Op);
3046 return DAG.getNode(ISD::TRUNCATE, dl, ResVT, Reduce);
3047}
3048
3049SDValue DAGTypeLegalizer::PromoteIntOp_VP_REDUCE(SDNode *N, unsigned OpNo) {
3050 SDLoc DL(N);
3051 SDValue Op = N->getOperand(OpNo);
3052 SmallVector<SDValue, 4> NewOps(N->ops());
3053
3054 if (OpNo == 2) { // Mask
3055 // Update in place.
3056 NewOps[2] = PromoteTargetBoolean(Op, N->getOperand(1).getValueType());
3057 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3058 }
3059
3060 assert(OpNo == 1 && "Unexpected operand for promotion");
3061
3062 Op = PromoteIntOpVectorReduction(N, Op);
3063
3064 NewOps[OpNo] = Op;
3065
3066 EVT VT = N->getValueType(0);
3067 EVT EltVT = Op.getValueType().getScalarType();
3068
3069 if (VT.bitsGE(EltVT))
3070 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, NewOps);
3071
3072 // Result size must be >= element/start-value size. If this is not the case
3073 // after promotion, also promote both the start value and result type and
3074 // then truncate.
3075 NewOps[0] =
3076 DAG.getNode(getExtendForIntVecReduction(N), DL, EltVT, N->getOperand(0));
3077 SDValue Reduce = DAG.getNode(N->getOpcode(), DL, EltVT, NewOps);
3078 return DAG.getNode(ISD::TRUNCATE, DL, VT, Reduce);
3079}
3080
3081SDValue DAGTypeLegalizer::PromoteIntOp_SET_ROUNDING(SDNode *N) {
3082 SDValue Op = ZExtPromotedInteger(N->getOperand(1));
3083 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op), 0);
3084}
3085
3086SDValue DAGTypeLegalizer::PromoteIntOp_STACKMAP(SDNode *N, unsigned OpNo) {
3087 assert(OpNo > 1); // Because the first two arguments are guaranteed legal.
3088 SmallVector<SDValue> NewOps(N->ops());
3089 NewOps[OpNo] = GetPromotedInteger(NewOps[OpNo]);
3090 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3091}
3092
3093SDValue DAGTypeLegalizer::PromoteIntOp_PATCHPOINT(SDNode *N, unsigned OpNo) {
3094 assert(OpNo >= 7);
3095 SmallVector<SDValue> NewOps(N->ops());
3096 NewOps[OpNo] = GetPromotedInteger(NewOps[OpNo]);
3097 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3098}
3099
3100SDValue DAGTypeLegalizer::PromoteIntOp_WRITE_REGISTER(SDNode *N,
3101 unsigned OpNo) {
3102 const Function &Fn = DAG.getMachineFunction().getFunction();
3103 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
3104 "cannot use llvm.write_register with illegal type", Fn,
3105 N->getDebugLoc()));
3106 return N->getOperand(0);
3107}
3108
3109SDValue DAGTypeLegalizer::PromoteIntOp_VP_STRIDED(SDNode *N, unsigned OpNo) {
3110 assert((N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD && OpNo == 3) ||
3111 (N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE && OpNo == 4));
3112
3113 SmallVector<SDValue, 8> NewOps(N->ops());
3114 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
3115 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
3116 if (Res == N)
3117 return SDValue(Res, 0);
3118
3119 // Update triggered CSE, do our own replacement since caller can't.
3120 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
3121 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
3122 return SDValue();
3123}
3124
3125SDValue DAGTypeLegalizer::PromoteIntOp_VP_SPLICE(SDNode *N, unsigned OpNo) {
3126 SmallVector<SDValue, 6> NewOps(N->ops());
3127
3128 if (OpNo == 2) { // Offset operand
3129 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
3130 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3131 }
3132
3133 assert((OpNo == 4 || OpNo == 5) && "Unexpected operand for promotion");
3134
3135 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
3136 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3137}
3138
3139SDValue DAGTypeLegalizer::PromoteIntOp_VECTOR_HISTOGRAM(SDNode *N,
3140 unsigned OpNo) {
3141 assert(OpNo == 1 && "Unexpected operand for promotion");
3142 SmallVector<SDValue, 7> NewOps(N->ops());
3143 NewOps[1] = GetPromotedInteger(N->getOperand(1));
3144 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3145}
3146
3147SDValue DAGTypeLegalizer::PromoteIntOp_UnaryBooleanVectorOp(SDNode *N,
3148 unsigned OpNo) {
3149 assert(OpNo == 0 && "Unexpected operand for promotion");
3150 SDValue Op = N->getOperand(0);
3151
3152 SDValue NewOp;
3153 if (TLI.getBooleanContents(Op.getValueType()) ==
3155 NewOp = SExtPromotedInteger(Op);
3156 else
3157 NewOp = ZExtPromotedInteger(Op);
3158
3159 return SDValue(DAG.UpdateNodeOperands(N, NewOp), 0);
3160}
3161
3162SDValue DAGTypeLegalizer::PromoteIntOp_GET_ACTIVE_LANE_MASK(SDNode *N) {
3163 SmallVector<SDValue, 1> NewOps(N->ops());
3164 NewOps[0] = ZExtPromotedInteger(N->getOperand(0));
3165 NewOps[1] = ZExtPromotedInteger(N->getOperand(1));
3166 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3167}
3168
3169SDValue DAGTypeLegalizer::PromoteIntOp_MaskedBinOp(SDNode *N, unsigned OpNo) {
3170 assert(OpNo == 2);
3171 SmallVector<SDValue, 3> NewOps(N->ops());
3172 NewOps[2] = PromoteTargetBoolean(NewOps[2], N->getValueType(0));
3173 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3174}
3175
3176SDValue DAGTypeLegalizer::PromoteIntOp_PARTIAL_REDUCE_MLA(SDNode *N) {
3177 SmallVector<SDValue, 1> NewOps(N->ops());
3178 switch (N->getOpcode()) {
3180 NewOps[1] = SExtPromotedInteger(N->getOperand(1));
3181 NewOps[2] = SExtPromotedInteger(N->getOperand(2));
3182 break;
3184 NewOps[1] = ZExtPromotedInteger(N->getOperand(1));
3185 NewOps[2] = ZExtPromotedInteger(N->getOperand(2));
3186 break;
3188 NewOps[1] = SExtPromotedInteger(N->getOperand(1));
3189 NewOps[2] = ZExtPromotedInteger(N->getOperand(2));
3190 break;
3191 default:
3192 llvm_unreachable("unexpected opcode");
3193 }
3194 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3195}
3196
3197SDValue DAGTypeLegalizer::PromoteIntOp_LOOP_DEPENDENCE_MASK(SDNode *N) {
3198 SDValue NewOps[4];
3199 NewOps[0] = ZExtPromotedInteger(N->getOperand(0));
3200 NewOps[1] = ZExtPromotedInteger(N->getOperand(1));
3201 NewOps[2] = ZExtPromotedInteger(N->getOperand(2));
3202 NewOps[3] = N->getOperand(3);
3203 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3204}
3205
3206//===----------------------------------------------------------------------===//
3207// Integer Result Expansion
3208//===----------------------------------------------------------------------===//
3209
3210/// ExpandIntegerResult - This method is called when the specified result of the
3211/// specified node is found to need expansion. At this point, the node may also
3212/// have invalid operands or may have other results that need promotion, we just
3213/// know that (at least) one result needs expansion.
3214void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
3215 LLVM_DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG));
3216 SDValue Lo, Hi;
3217 Lo = Hi = SDValue();
3218
3219 // See if the target wants to custom expand this node.
3220 if (CustomLowerNode(N, N->getValueType(ResNo), true))
3221 return;
3222
3223 switch (N->getOpcode()) {
3224 default:
3225#ifndef NDEBUG
3226 dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
3227 N->dump(&DAG); dbgs() << "\n";
3228#endif
3229 report_fatal_error("Do not know how to expand the result of this "
3230 "operator!");
3231
3232 case ISD::ARITH_FENCE: SplitRes_ARITH_FENCE(N, Lo, Hi); break;
3233 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
3234 case ISD::SELECT: SplitRes_Select(N, Lo, Hi); break;
3235 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
3236 case ISD::POISON:
3237 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
3238 case ISD::FREEZE: SplitRes_FREEZE(N, Lo, Hi); break;
3239 case ISD::SETCC: ExpandIntRes_SETCC(N, Lo, Hi); break;
3240
3241 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
3242 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
3243 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
3244 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
3245 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
3246
3247 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
3248 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
3249 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
3250 case ISD::BITREVERSE: ExpandIntRes_BITREVERSE(N, Lo, Hi); break;
3251 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
3252 case ISD::PARITY: ExpandIntRes_PARITY(N, Lo, Hi); break;
3253 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
3254 case ISD::ABS:
3256 ExpandIntRes_ABS(N, Lo, Hi);
3257 break;
3258 case ISD::ABDS:
3259 case ISD::ABDU: ExpandIntRes_ABD(N, Lo, Hi); break;
3261 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
3262 case ISD::CTLS: ExpandIntRes_CTLS(N, Lo, Hi); break;
3263 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
3265 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
3266 case ISD::GET_ROUNDING:ExpandIntRes_GET_ROUNDING(N, Lo, Hi); break;
3268 case ISD::FP_TO_SINT:
3270 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_XINT(N, Lo, Hi); break;
3272 case ISD::FP_TO_UINT_SAT: ExpandIntRes_FP_TO_XINT_SAT(N, Lo, Hi); break;
3273 case ISD::STRICT_LROUND:
3274 case ISD::STRICT_LRINT:
3275 case ISD::LROUND:
3276 case ISD::LRINT:
3278 case ISD::STRICT_LLRINT:
3279 case ISD::LLROUND:
3280 case ISD::LLRINT: ExpandIntRes_XROUND_XRINT(N, Lo, Hi); break;
3281 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
3282 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
3284 case ISD::READSTEADYCOUNTER: ExpandIntRes_READCOUNTER(N, Lo, Hi); break;
3285 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
3286 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
3287 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
3288 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
3289 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
3290 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
3291 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
3292 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
3293 case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
3294
3306 case ISD::ATOMIC_SWAP:
3307 case ISD::ATOMIC_CMP_SWAP: {
3308 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
3309 SplitInteger(Tmp.first, Lo, Hi);
3310 ReplaceValueWith(SDValue(N, 1), Tmp.second);
3311 break;
3312 }
3314 AtomicSDNode *AN = cast<AtomicSDNode>(N);
3315 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other);
3316 SDValue Tmp = DAG.getAtomicCmpSwap(
3317 ISD::ATOMIC_CMP_SWAP, SDLoc(N), AN->getMemoryVT(), VTs,
3318 N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3),
3319 AN->getMemOperand());
3320
3321 // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
3322 // success simply by comparing the loaded value against the ingoing
3323 // comparison.
3324 SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp,
3325 N->getOperand(2), ISD::SETEQ);
3326
3327 SplitInteger(Tmp, Lo, Hi);
3328 ReplaceValueWith(SDValue(N, 1), Success);
3329 ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1));
3330 break;
3331 }
3332
3333 case ISD::AND:
3334 case ISD::OR:
3335 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
3336
3337 case ISD::UMAX:
3338 case ISD::SMAX:
3339 case ISD::UMIN:
3340 case ISD::SMIN: ExpandIntRes_MINMAX(N, Lo, Hi); break;
3341
3342 case ISD::SCMP:
3343 case ISD::UCMP: ExpandIntRes_CMP(N, Lo, Hi); break;
3344
3345 case ISD::ADD:
3346 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
3347
3348 case ISD::ADDC:
3349 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
3350
3351 case ISD::ADDE:
3352 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
3353
3354 case ISD::UADDO_CARRY:
3355 case ISD::USUBO_CARRY: ExpandIntRes_UADDSUBO_CARRY(N, Lo, Hi); break;
3356
3357 case ISD::SADDO_CARRY:
3358 case ISD::SSUBO_CARRY: ExpandIntRes_SADDSUBO_CARRY(N, Lo, Hi); break;
3359
3360 case ISD::SHL:
3361 case ISD::SRA:
3362 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
3363
3364 case ISD::SADDO:
3365 case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
3366 case ISD::UADDO:
3367 case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
3368 case ISD::UMULO:
3369 case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
3370
3371 case ISD::SADDSAT:
3372 case ISD::UADDSAT:
3373 case ISD::SSUBSAT:
3374 case ISD::USUBSAT: ExpandIntRes_ADDSUBSAT(N, Lo, Hi); break;
3375
3376 case ISD::SSHLSAT:
3377 case ISD::USHLSAT: ExpandIntRes_SHLSAT(N, Lo, Hi); break;
3378
3379 case ISD::AVGCEILS:
3380 case ISD::AVGCEILU:
3381 case ISD::AVGFLOORS:
3382 case ISD::AVGFLOORU: ExpandIntRes_AVG(N, Lo, Hi); break;
3383
3384 case ISD::SMULFIX:
3385 case ISD::SMULFIXSAT:
3386 case ISD::UMULFIX:
3387 case ISD::UMULFIXSAT: ExpandIntRes_MULFIX(N, Lo, Hi); break;
3388
3389 case ISD::SDIVFIX:
3390 case ISD::SDIVFIXSAT:
3391 case ISD::UDIVFIX:
3392 case ISD::UDIVFIXSAT: ExpandIntRes_DIVFIX(N, Lo, Hi); break;
3393
3394 case ISD::VECREDUCE_ADD:
3395 case ISD::VECREDUCE_MUL:
3396 case ISD::VECREDUCE_AND:
3397 case ISD::VECREDUCE_OR:
3398 case ISD::VECREDUCE_XOR:
3402 case ISD::VECREDUCE_UMIN: ExpandIntRes_VECREDUCE(N, Lo, Hi); break;
3403
3404 case ISD::ROTL:
3405 case ISD::ROTR:
3406 ExpandIntRes_Rotate(N, Lo, Hi);
3407 break;
3408
3409 case ISD::FSHL:
3410 case ISD::FSHR:
3411 ExpandIntRes_FunnelShift(N, Lo, Hi);
3412 break;
3413
3414 case ISD::CLMUL:
3415 case ISD::CLMULR:
3416 case ISD::CLMULH:
3417 ExpandIntRes_CLMUL(N, Lo, Hi);
3418 break;
3419
3420 case ISD::PEXT:
3421 ExpandIntRes_PEXT(N, Lo, Hi);
3422 break;
3423
3424 case ISD::PDEP:
3425 ExpandIntRes_PDEP(N, Lo, Hi);
3426 break;
3427
3428 case ISD::VSCALE:
3429 ExpandIntRes_VSCALE(N, Lo, Hi);
3430 break;
3431
3432 case ISD::READ_REGISTER:
3433 ExpandIntRes_READ_REGISTER(N, Lo, Hi);
3434 break;
3435
3436 case ISD::CTTZ_ELTS:
3438 ExpandIntRes_CTTZ_ELTS(N, Lo, Hi);
3439 break;
3440 }
3441
3442 // If Lo/Hi is null, the sub-method took care of registering results etc.
3443 if (Lo.getNode())
3444 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
3445}
3446
3447/// Lower an atomic node to the appropriate builtin call.
3448std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
3449 unsigned Opc = Node->getOpcode();
3450 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
3451 AtomicOrdering order = cast<AtomicSDNode>(Node)->getMergedOrdering();
3452 // Lower to outline atomic libcall if outline atomics enabled,
3453 // or to sync libcall otherwise
3454 RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, order, VT);
3455 EVT RetVT = Node->getValueType(0);
3456 TargetLowering::MakeLibCallOptions CallOptions;
3458
3459 RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(LC);
3460 if (LCImpl != RTLIB::Unsupported) {
3461 Ops.append(Node->op_begin() + 2, Node->op_end());
3462 Ops.push_back(Node->getOperand(1));
3463 } else {
3464 LC = RTLIB::getSYNC(Opc, VT);
3465 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
3466 "Unexpected atomic op or value type!");
3467 Ops.append(Node->op_begin() + 1, Node->op_end());
3468 LCImpl = DAG.getLibcalls().getLibcallImpl(LC);
3469 }
3470 return TLI.makeLibCall(DAG, LCImpl, RetVT, Ops, CallOptions, SDLoc(Node),
3471 Node->getOperand(0));
3472}
3473
3474/// N is a shift by a value that needs to be expanded,
3475/// and the shift amount is a constant 'Amt'. Expand the operation.
3476void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, const APInt &Amt,
3477 SDValue &Lo, SDValue &Hi) {
3478 SDLoc DL(N);
3479 // Expand the incoming operand to be shifted, so that we have its parts
3480 SDValue InL, InH;
3481 GetExpandedInteger(N->getOperand(0), InL, InH);
3482
3483 // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
3484 // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
3485 if (!Amt) {
3486 Lo = InL;
3487 Hi = InH;
3488 return;
3489 }
3490
3491 EVT NVT = InL.getValueType();
3492 unsigned VTBits = N->getValueType(0).getSizeInBits();
3493 unsigned NVTBits = NVT.getSizeInBits();
3494
3495 if (N->getOpcode() == ISD::SHL) {
3496 if (Amt.uge(VTBits)) {
3497 Lo = Hi = DAG.getConstant(0, DL, NVT);
3498 } else if (Amt.ugt(NVTBits)) {
3499 Lo = DAG.getConstant(0, DL, NVT);
3500 Hi = DAG.getNode(ISD::SHL, DL, NVT, InL,
3501 DAG.getShiftAmountConstant(Amt - NVTBits, NVT, DL));
3502 } else if (Amt == NVTBits) {
3503 Lo = DAG.getConstant(0, DL, NVT);
3504 Hi = InL;
3505 } else {
3506 Lo = DAG.getNode(ISD::SHL, DL, NVT, InL,
3507 DAG.getShiftAmountConstant(Amt, NVT, DL));
3508 // Use FSHL if legal so we don't need to combine it later.
3509 if (TLI.isOperationLegal(ISD::FSHL, NVT)) {
3510 Hi = DAG.getNode(ISD::FSHL, DL, NVT, InH, InL,
3511 DAG.getShiftAmountConstant(Amt, NVT, DL));
3512 } else {
3513 Hi = DAG.getNode(
3514 ISD::OR, DL, NVT,
3515 DAG.getNode(ISD::SHL, DL, NVT, InH,
3516 DAG.getShiftAmountConstant(Amt, NVT, DL)),
3517 DAG.getNode(ISD::SRL, DL, NVT, InL,
3518 DAG.getShiftAmountConstant(-Amt + NVTBits, NVT, DL)));
3519 }
3520 }
3521 return;
3522 }
3523
3524 if (N->getOpcode() == ISD::SRL) {
3525 if (Amt.uge(VTBits)) {
3526 Lo = Hi = DAG.getConstant(0, DL, NVT);
3527 } else if (Amt.ugt(NVTBits)) {
3528 Lo = DAG.getNode(ISD::SRL, DL, NVT, InH,
3529 DAG.getShiftAmountConstant(Amt - NVTBits, NVT, DL));
3530 Hi = DAG.getConstant(0, DL, NVT);
3531 } else if (Amt == NVTBits) {
3532 Lo = InH;
3533 Hi = DAG.getConstant(0, DL, NVT);
3534 } else {
3535 // Use FSHR if legal so we don't need to combine it later.
3536 if (TLI.isOperationLegal(ISD::FSHR, NVT)) {
3537 Lo = DAG.getNode(ISD::FSHR, DL, NVT, InH, InL,
3538 DAG.getShiftAmountConstant(Amt, NVT, DL));
3539 } else {
3540 Lo = DAG.getNode(
3541 ISD::OR, DL, NVT,
3542 DAG.getNode(ISD::SRL, DL, NVT, InL,
3543 DAG.getShiftAmountConstant(Amt, NVT, DL)),
3544 DAG.getNode(ISD::SHL, DL, NVT, InH,
3545 DAG.getShiftAmountConstant(-Amt + NVTBits, NVT, DL)));
3546 }
3547 Hi = DAG.getNode(ISD::SRL, DL, NVT, InH,
3548 DAG.getShiftAmountConstant(Amt, NVT, DL));
3549 }
3550 return;
3551 }
3552
3553 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
3554 if (Amt.uge(VTBits)) {
3555 Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
3556 DAG.getShiftAmountConstant(NVTBits - 1, NVT, DL));
3557 } else if (Amt.ugt(NVTBits)) {
3558 Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
3559 DAG.getShiftAmountConstant(Amt - NVTBits, NVT, DL));
3560 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
3561 DAG.getShiftAmountConstant(NVTBits - 1, NVT, DL));
3562 } else if (Amt == NVTBits) {
3563 Lo = InH;
3564 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
3565 DAG.getShiftAmountConstant(NVTBits - 1, NVT, DL));
3566 } else {
3567 // Use FSHR if legal so we don't need to combine it later.
3568 if (TLI.isOperationLegal(ISD::FSHR, NVT)) {
3569 Lo = DAG.getNode(ISD::FSHR, DL, NVT, InH, InL,
3570 DAG.getShiftAmountConstant(Amt, NVT, DL));
3571 } else {
3572 Lo = DAG.getNode(
3573 ISD::OR, DL, NVT,
3574 DAG.getNode(ISD::SRL, DL, NVT, InL,
3575 DAG.getShiftAmountConstant(Amt, NVT, DL)),
3576 DAG.getNode(ISD::SHL, DL, NVT, InH,
3577 DAG.getShiftAmountConstant(-Amt + NVTBits, NVT, DL)));
3578 }
3579 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
3580 DAG.getShiftAmountConstant(Amt, NVT, DL));
3581 }
3582}
3583
3584/// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
3585/// this shift based on knowledge of the high bit of the shift amount. If we
3586/// can tell this, we know that it is >= 32 or < 32, without knowing the actual
3587/// shift amount.
3588bool DAGTypeLegalizer::
3589ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
3590 unsigned Opc = N->getOpcode();
3591 SDValue In = N->getOperand(0);
3592 SDValue Amt = N->getOperand(1);
3593 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3594 EVT ShTy = Amt.getValueType();
3595 unsigned ShBits = ShTy.getScalarSizeInBits();
3596 unsigned NVTBits = NVT.getScalarSizeInBits();
3597 assert(isPowerOf2_32(NVTBits) &&
3598 "Expanded integer type size not a power of two!");
3599 SDLoc dl(N);
3600
3601 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
3602 KnownBits Known = DAG.computeKnownBits(Amt);
3603
3604 // If we don't know anything about the high bits, exit.
3605 if (((Known.Zero | Known.One) & HighBitMask) == 0)
3606 return false;
3607
3608 // Get the incoming operand to be shifted.
3609 SDValue InL, InH;
3610 GetExpandedInteger(In, InL, InH);
3611
3612 // If we know that any of the high bits of the shift amount are one, then we
3613 // can do this as a couple of simple shifts.
3614 if (Known.One.intersects(HighBitMask)) {
3615 // Mask out the high bit, which we know is set.
3616 Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
3617 DAG.getConstant(~HighBitMask, dl, ShTy));
3618
3619 switch (Opc) {
3620 default: llvm_unreachable("Unknown shift");
3621 case ISD::SHL:
3622 Lo = DAG.getConstant(0, dl, NVT); // Low part is zero.
3623 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
3624 return true;
3625 case ISD::SRL:
3626 Hi = DAG.getConstant(0, dl, NVT); // Hi part is zero.
3627 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
3628 return true;
3629 case ISD::SRA:
3630 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
3631 DAG.getConstant(NVTBits - 1, dl, ShTy));
3632 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
3633 return true;
3634 }
3635 }
3636
3637 // If we know that all of the high bits of the shift amount are zero, then we
3638 // can do this as a couple of simple shifts.
3639 if (HighBitMask.isSubsetOf(Known.Zero)) {
3640 // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
3641 // shift if x is zero. We can use XOR here because x is known to be smaller
3642 // than 32.
3643 SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
3644 DAG.getConstant(NVTBits - 1, dl, ShTy));
3645
3646 unsigned Op1, Op2;
3647 switch (Opc) {
3648 default: llvm_unreachable("Unknown shift");
3649 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
3650 case ISD::SRL:
3651 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
3652 }
3653
3654 // When shifting right the arithmetic for Lo and Hi is swapped.
3655 if (Opc != ISD::SHL)
3656 std::swap(InL, InH);
3657
3658 // Use a little trick to get the bits that move from Lo to Hi. First
3659 // shift by one bit.
3660 SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, dl, ShTy));
3661 // Then compute the remaining shift with amount-1.
3662 SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
3663
3664 Lo = DAG.getNode(Opc, dl, NVT, InL, Amt);
3665 Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
3666
3667 if (Opc != ISD::SHL)
3668 std::swap(Hi, Lo);
3669 return true;
3670 }
3671
3672 return false;
3673}
3674
3675/// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
3676/// of any size.
3677bool DAGTypeLegalizer::
3678ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
3679 SDValue Amt = N->getOperand(1);
3680 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3681 EVT ShTy = Amt.getValueType();
3682 unsigned NVTBits = NVT.getSizeInBits();
3683 assert(isPowerOf2_32(NVTBits) &&
3684 "Expanded integer type size not a power of two!");
3685 SDLoc dl(N);
3686
3687 // Get the incoming operand to be shifted.
3688 SDValue InL, InH;
3689 GetExpandedInteger(N->getOperand(0), InL, InH);
3690
3691 SDValue NVBitsNode = DAG.getConstant(NVTBits, dl, ShTy);
3692 SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
3693 SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
3694 SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
3695 Amt, NVBitsNode, ISD::SETULT);
3696 SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(ShTy),
3697 Amt, DAG.getConstant(0, dl, ShTy),
3698 ISD::SETEQ);
3699
3700 SDValue LoS, HiS, LoL, HiL;
3701 switch (N->getOpcode()) {
3702 default: llvm_unreachable("Unknown shift");
3703 case ISD::SHL:
3704 // Short: ShAmt < NVTBits
3705 LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
3706 HiS = DAG.getNode(ISD::OR, dl, NVT,
3707 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
3708 DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
3709
3710 // Long: ShAmt >= NVTBits
3711 LoL = DAG.getConstant(0, dl, NVT); // Lo part is zero.
3712 HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
3713
3714 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
3715 Hi = DAG.getSelect(dl, NVT, isZero, InH,
3716 DAG.getSelect(dl, NVT, isShort, HiS, HiL));
3717 return true;
3718 case ISD::SRL:
3719 // Short: ShAmt < NVTBits
3720 HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
3721 LoS = DAG.getNode(ISD::OR, dl, NVT,
3722 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
3723 // FIXME: If Amt is zero, the following shift generates an undefined result
3724 // on some architectures.
3725 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
3726
3727 // Long: ShAmt >= NVTBits
3728 HiL = DAG.getConstant(0, dl, NVT); // Hi part is zero.
3729 LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
3730
3731 Lo = DAG.getSelect(dl, NVT, isZero, InL,
3732 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
3733 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
3734 return true;
3735 case ISD::SRA:
3736 // Short: ShAmt < NVTBits
3737 HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
3738 LoS = DAG.getNode(ISD::OR, dl, NVT,
3739 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
3740 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
3741
3742 // Long: ShAmt >= NVTBits
3743 HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
3744 DAG.getConstant(NVTBits - 1, dl, ShTy));
3745 LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
3746
3747 Lo = DAG.getSelect(dl, NVT, isZero, InL,
3748 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
3749 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
3750 return true;
3751 }
3752}
3753
3754static std::pair<ISD::CondCode, ISD::NodeType> getExpandedMinMaxOps(int Op) {
3755
3756 switch (Op) {
3757 default: llvm_unreachable("invalid min/max opcode");
3758 case ISD::SMAX:
3759 return std::make_pair(ISD::SETGT, ISD::UMAX);
3760 case ISD::UMAX:
3761 return std::make_pair(ISD::SETUGT, ISD::UMAX);
3762 case ISD::SMIN:
3763 return std::make_pair(ISD::SETLT, ISD::UMIN);
3764 case ISD::UMIN:
3765 return std::make_pair(ISD::SETULT, ISD::UMIN);
3766 }
3767}
3768
3769void DAGTypeLegalizer::ExpandIntRes_SETCC(SDNode *N, SDValue &Lo, SDValue &Hi) {
3770 SDLoc DL(N);
3771
3772 SDValue LHS = N->getOperand(0);
3773 SDValue RHS = N->getOperand(1);
3774 EVT NewVT = getSetCCResultType(LHS.getValueType());
3775
3776 // Taking the same approach as ScalarizeVecRes_SETCC
3777 SDValue Res = DAG.getNode(ISD::SETCC, DL, NewVT, LHS, RHS, N->getOperand(2));
3778
3779 Res = DAG.getBoolExtOrTrunc(Res, DL, N->getValueType(0), NewVT);
3780 SplitInteger(Res, Lo, Hi);
3781}
3782
3783void DAGTypeLegalizer::ExpandIntRes_MINMAX(SDNode *N,
3784 SDValue &Lo, SDValue &Hi) {
3785 SDLoc DL(N);
3786
3787 SDValue LHS = N->getOperand(0);
3788 SDValue RHS = N->getOperand(1);
3789
3790 // If the upper halves are all sign bits, then we can perform the MINMAX on
3791 // the lower half and sign-extend the result to the upper half.
3792 unsigned NumBits = N->getValueType(0).getScalarSizeInBits();
3793 unsigned NumHalfBits = NumBits / 2;
3794 if (DAG.ComputeNumSignBits(LHS) > NumHalfBits &&
3795 DAG.ComputeNumSignBits(RHS) > NumHalfBits) {
3796 SDValue LHSL, LHSH, RHSL, RHSH;
3797 GetExpandedInteger(LHS, LHSL, LHSH);
3798 GetExpandedInteger(RHS, RHSL, RHSH);
3799 EVT NVT = LHSL.getValueType();
3800
3801 Lo = DAG.getNode(N->getOpcode(), DL, NVT, LHSL, RHSL);
3802 Hi = DAG.getNode(ISD::SRA, DL, NVT, Lo,
3803 DAG.getShiftAmountConstant(NumHalfBits - 1, NVT, DL));
3804 return;
3805 }
3806
3807 // The Lo of smin(X, -1) is LHSL if X is negative. Otherwise it's -1.
3808 // The Lo of smax(X, 0) is 0 if X is negative. Otherwise it's LHSL.
3809 if ((N->getOpcode() == ISD::SMAX && isNullConstant(RHS)) ||
3810 (N->getOpcode() == ISD::SMIN && isAllOnesConstant(RHS))) {
3811 SDValue LHSL, LHSH, RHSL, RHSH;
3812 GetExpandedInteger(LHS, LHSL, LHSH);
3813 GetExpandedInteger(RHS, RHSL, RHSH);
3814 EVT NVT = LHSL.getValueType();
3815 EVT CCT = getSetCCResultType(NVT);
3816
3817 SDValue HiNeg =
3818 DAG.getSetCC(DL, CCT, LHSH, DAG.getConstant(0, DL, NVT), ISD::SETLT);
3819 if (N->getOpcode() == ISD::SMIN) {
3820 Lo = DAG.getSelect(DL, NVT, HiNeg, LHSL, DAG.getAllOnesConstant(DL, NVT));
3821 } else {
3822 Lo = DAG.getSelect(DL, NVT, HiNeg, DAG.getConstant(0, DL, NVT), LHSL);
3823 }
3824 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
3825 return;
3826 }
3827
3828 const APInt *RHSVal = nullptr;
3829 if (auto *RHSConst = dyn_cast<ConstantSDNode>(RHS))
3830 RHSVal = &RHSConst->getAPIntValue();
3831
3832 // The high half of MIN/MAX is always just the the MIN/MAX of the
3833 // high halves of the operands. Expand this way if it appears profitable.
3834 if (RHSVal && (N->getOpcode() == ISD::UMIN || N->getOpcode() == ISD::UMAX) &&
3835 (RHSVal->countLeadingOnes() >= NumHalfBits ||
3836 RHSVal->countLeadingZeros() >= NumHalfBits)) {
3837 SDValue LHSL, LHSH, RHSL, RHSH;
3838 GetExpandedInteger(LHS, LHSL, LHSH);
3839 GetExpandedInteger(RHS, RHSL, RHSH);
3840 EVT NVT = LHSL.getValueType();
3841 EVT CCT = getSetCCResultType(NVT);
3842
3843 ISD::NodeType LoOpc;
3844 ISD::CondCode CondC;
3845 std::tie(CondC, LoOpc) = getExpandedMinMaxOps(N->getOpcode());
3846
3847 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
3848 // We need to know whether to select Lo part that corresponds to 'winning'
3849 // Hi part or if Hi parts are equal.
3850 SDValue IsHiLeft = DAG.getSetCC(DL, CCT, LHSH, RHSH, CondC);
3851 SDValue IsHiEq = DAG.getSetCC(DL, CCT, LHSH, RHSH, ISD::SETEQ);
3852
3853 // Lo part corresponding to the 'winning' Hi part
3854 SDValue LoCmp = DAG.getSelect(DL, NVT, IsHiLeft, LHSL, RHSL);
3855
3856 // Recursed Lo part if Hi parts are equal, this uses unsigned version
3857 SDValue LoMinMax = DAG.getNode(LoOpc, DL, NVT, {LHSL, RHSL});
3858
3859 Lo = DAG.getSelect(DL, NVT, IsHiEq, LoMinMax, LoCmp);
3860 return;
3861 }
3862
3863 // Expand to "a < b ? a : b" etc. Prefer ge/le if that simplifies
3864 // the compare.
3865 ISD::CondCode Pred;
3866 switch (N->getOpcode()) {
3867 default: llvm_unreachable("How did we get here?");
3868 case ISD::SMAX:
3869 if (RHSVal && RHSVal->countTrailingZeros() >= NumHalfBits)
3870 Pred = ISD::SETGE;
3871 else
3872 Pred = ISD::SETGT;
3873 break;
3874 case ISD::SMIN:
3875 if (RHSVal && RHSVal->countTrailingOnes() >= NumHalfBits)
3876 Pred = ISD::SETLE;
3877 else
3878 Pred = ISD::SETLT;
3879 break;
3880 case ISD::UMAX:
3881 if (RHSVal && RHSVal->countTrailingZeros() >= NumHalfBits)
3882 Pred = ISD::SETUGE;
3883 else
3884 Pred = ISD::SETUGT;
3885 break;
3886 case ISD::UMIN:
3887 if (RHSVal && RHSVal->countTrailingOnes() >= NumHalfBits)
3888 Pred = ISD::SETULE;
3889 else
3890 Pred = ISD::SETULT;
3891 break;
3892 }
3893 EVT VT = N->getValueType(0);
3894 EVT CCT = getSetCCResultType(VT);
3895 SDValue Cond = DAG.getSetCC(DL, CCT, LHS, RHS, Pred);
3896 SDValue Result = DAG.getSelect(DL, VT, Cond, LHS, RHS);
3897 SplitInteger(Result, Lo, Hi);
3898}
3899
3900void DAGTypeLegalizer::ExpandIntRes_CMP(SDNode *N, SDValue &Lo, SDValue &Hi) {
3901 SDValue ExpandedCMP = TLI.expandCMP(N, DAG);
3902 SplitInteger(ExpandedCMP, Lo, Hi);
3903}
3904
3905void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
3906 SDValue &Lo, SDValue &Hi) {
3907 SDLoc dl(N);
3908 // Expand the subcomponents.
3909 SDValue LHSL, LHSH, RHSL, RHSH;
3910 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3911 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3912
3913 EVT NVT = LHSL.getValueType();
3914 SDValue LoOps[2] = { LHSL, RHSL };
3915 SDValue HiOps[3] = { LHSH, RHSH };
3916
3917 bool HasOpCarry = TLI.isOperationLegalOrCustom(
3918 N->getOpcode() == ISD::ADD ? ISD::UADDO_CARRY : ISD::USUBO_CARRY,
3919 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3920 if (HasOpCarry) {
3921 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
3922 if (N->getOpcode() == ISD::ADD) {
3923 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
3924 HiOps[2] = Lo.getValue(1);
3925 Hi = DAG.computeKnownBits(HiOps[2]).isZero()
3926 ? DAG.getNode(ISD::ADD, dl, NVT, ArrayRef(HiOps, 2))
3927 : DAG.getNode(ISD::UADDO_CARRY, dl, VTList, HiOps);
3928 } else {
3929 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
3930 HiOps[2] = Lo.getValue(1);
3931 Hi = DAG.computeKnownBits(HiOps[2]).isZero()
3932 ? DAG.getNode(ISD::SUB, dl, NVT, ArrayRef(HiOps, 2))
3933 : DAG.getNode(ISD::USUBO_CARRY, dl, VTList, HiOps);
3934 }
3935 return;
3936 }
3937
3938 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
3939 // them. TODO: Teach operation legalization how to expand unsupported
3940 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
3941 // a carry of type MVT::Glue, but there doesn't seem to be any way to
3942 // generate a value of this type in the expanded code sequence.
3943 bool hasCarry =
3944 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
3946 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3947
3948 if (hasCarry) {
3949 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
3950 if (N->getOpcode() == ISD::ADD) {
3951 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
3952 HiOps[2] = Lo.getValue(1);
3953 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
3954 } else {
3955 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
3956 HiOps[2] = Lo.getValue(1);
3957 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
3958 }
3959 return;
3960 }
3961
3962 bool hasOVF =
3963 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
3965 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3966 TargetLoweringBase::BooleanContent BoolType = TLI.getBooleanContents(NVT);
3967
3968 if (hasOVF) {
3969 EVT OvfVT = getSetCCResultType(NVT);
3970 SDVTList VTList = DAG.getVTList(NVT, OvfVT);
3971 int RevOpc;
3972 if (N->getOpcode() == ISD::ADD) {
3973 RevOpc = ISD::SUB;
3974 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
3975 Hi = DAG.getNode(ISD::ADD, dl, NVT, ArrayRef(HiOps, 2));
3976 } else {
3977 RevOpc = ISD::ADD;
3978 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
3979 Hi = DAG.getNode(ISD::SUB, dl, NVT, ArrayRef(HiOps, 2));
3980 }
3981 SDValue OVF = Lo.getValue(1);
3982
3983 switch (BoolType) {
3985 OVF = DAG.getNode(ISD::AND, dl, OvfVT, DAG.getConstant(1, dl, OvfVT), OVF);
3986 [[fallthrough]];
3988 OVF = DAG.getZExtOrTrunc(OVF, dl, NVT);
3989 Hi = DAG.getNode(N->getOpcode(), dl, NVT, Hi, OVF);
3990 break;
3992 OVF = DAG.getSExtOrTrunc(OVF, dl, NVT);
3993 Hi = DAG.getNode(RevOpc, dl, NVT, Hi, OVF);
3994 }
3995 return;
3996 }
3997
3998 if (N->getOpcode() == ISD::ADD) {
3999 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps);
4000 SDValue Cmp;
4001 // Special case: X+1 has a carry out if X+1==0. This may reduce the live
4002 // range of X. We assume comparing with 0 is cheap.
4003 if (isOneConstant(LoOps[1]))
4004 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
4005 DAG.getConstant(0, dl, NVT), ISD::SETEQ);
4006 else if (isAllOnesConstant(LoOps[1])) {
4007 if (isAllOnesConstant(HiOps[1]))
4008 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), LoOps[0],
4009 DAG.getConstant(0, dl, NVT), ISD::SETEQ);
4010 else
4011 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), LoOps[0],
4012 DAG.getConstant(0, dl, NVT), ISD::SETNE);
4013 } else
4014 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
4015 ISD::SETULT);
4016
4017 SDValue Carry;
4019 Carry = DAG.getZExtOrTrunc(Cmp, dl, NVT);
4020 else
4021 Carry = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
4022 DAG.getConstant(0, dl, NVT));
4023
4024 if (isAllOnesConstant(LoOps[1]) && isAllOnesConstant(HiOps[1])) {
4025 Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps[0], Carry);
4026 } else {
4027 Hi = DAG.getNode(ISD::ADD, dl, NVT, ArrayRef(HiOps, 2));
4028 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry);
4029 }
4030 } else {
4031 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps);
4032 Hi = DAG.getNode(ISD::SUB, dl, NVT, ArrayRef(HiOps, 2));
4033 SDValue Cmp =
4034 DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
4035 LoOps[0], LoOps[1], ISD::SETULT);
4036
4037 SDValue Borrow;
4039 Borrow = DAG.getZExtOrTrunc(Cmp, dl, NVT);
4040 else
4041 Borrow = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
4042 DAG.getConstant(0, dl, NVT));
4043
4044 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
4045 }
4046}
4047
4048void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
4049 SDValue &Lo, SDValue &Hi) {
4050 // Expand the subcomponents.
4051 SDValue LHSL, LHSH, RHSL, RHSH;
4052 SDLoc dl(N);
4053 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
4054 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
4055 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
4056 SDValue LoOps[2] = { LHSL, RHSL };
4057 SDValue HiOps[3] = { LHSH, RHSH };
4058
4059 if (N->getOpcode() == ISD::ADDC) {
4060 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
4061 HiOps[2] = Lo.getValue(1);
4062 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
4063 } else {
4064 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
4065 HiOps[2] = Lo.getValue(1);
4066 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
4067 }
4068
4069 // Legalized the flag result - switch anything that used the old flag to
4070 // use the new one.
4071 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
4072}
4073
4074void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
4075 SDValue &Lo, SDValue &Hi) {
4076 // Expand the subcomponents.
4077 SDValue LHSL, LHSH, RHSL, RHSH;
4078 SDLoc dl(N);
4079 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
4080 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
4081 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
4082 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
4083 SDValue HiOps[3] = { LHSH, RHSH };
4084
4085 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
4086 HiOps[2] = Lo.getValue(1);
4087 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
4088
4089 // Legalized the flag result - switch anything that used the old flag to
4090 // use the new one.
4091 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
4092}
4093
4094void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
4095 SDValue &Lo, SDValue &Hi) {
4096 SDValue LHS = N->getOperand(0);
4097 SDValue RHS = N->getOperand(1);
4098 SDLoc dl(N);
4099
4100 SDValue Ovf;
4101
4102 unsigned CarryOp, NoCarryOp;
4104 switch(N->getOpcode()) {
4105 case ISD::UADDO:
4106 CarryOp = ISD::UADDO_CARRY;
4107 NoCarryOp = ISD::ADD;
4108 Cond = ISD::SETULT;
4109 break;
4110 case ISD::USUBO:
4111 CarryOp = ISD::USUBO_CARRY;
4112 NoCarryOp = ISD::SUB;
4113 Cond = ISD::SETUGT;
4114 break;
4115 default:
4116 llvm_unreachable("Node has unexpected Opcode");
4117 }
4118
4119 bool HasCarryOp = TLI.isOperationLegalOrCustom(
4120 CarryOp, TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
4121
4122 if (HasCarryOp) {
4123 // Expand the subcomponents.
4124 SDValue LHSL, LHSH, RHSL, RHSH;
4125 GetExpandedInteger(LHS, LHSL, LHSH);
4126 GetExpandedInteger(RHS, RHSL, RHSH);
4127 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
4128 SDValue LoOps[2] = { LHSL, RHSL };
4129 SDValue HiOps[3] = { LHSH, RHSH };
4130
4131 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
4132 HiOps[2] = Lo.getValue(1);
4133 Hi = DAG.getNode(CarryOp, dl, VTList, HiOps);
4134
4135 Ovf = Hi.getValue(1);
4136 } else {
4137 // Expand the result by simply replacing it with the equivalent
4138 // non-overflow-checking operation.
4139 SDValue Sum = DAG.getNode(NoCarryOp, dl, LHS.getValueType(), LHS, RHS);
4140 SplitInteger(Sum, Lo, Hi);
4141
4142 if (N->getOpcode() == ISD::UADDO && isOneConstant(RHS)) {
4143 // Special case: uaddo X, 1 overflowed if X+1 == 0. We can detect this
4144 // with (Lo | Hi) == 0.
4145 SDValue Or = DAG.getNode(ISD::OR, dl, Lo.getValueType(), Lo, Hi);
4146 Ovf = DAG.getSetCC(dl, N->getValueType(1), Or,
4147 DAG.getConstant(0, dl, Lo.getValueType()), ISD::SETEQ);
4148 } else if (N->getOpcode() == ISD::UADDO && isAllOnesConstant(RHS)) {
4149 // Special case: uaddo X, -1 overflows if X == 0.
4150 Ovf =
4151 DAG.getSetCC(dl, N->getValueType(1), LHS,
4152 DAG.getConstant(0, dl, LHS.getValueType()), ISD::SETNE);
4153 } else {
4154 // Calculate the overflow: addition overflows iff a + b < a, and
4155 // subtraction overflows iff a - b > a.
4156 Ovf = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS, Cond);
4157 }
4158 }
4159
4160 // Legalized the flag result - switch anything that used the old flag to
4161 // use the new one.
4162 ReplaceValueWith(SDValue(N, 1), Ovf);
4163}
4164
4165void DAGTypeLegalizer::ExpandIntRes_UADDSUBO_CARRY(SDNode *N, SDValue &Lo,
4166 SDValue &Hi) {
4167 // Expand the subcomponents.
4168 SDValue LHSL, LHSH, RHSL, RHSH;
4169 SDLoc dl(N);
4170 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
4171 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
4172 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
4173 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
4174 SDValue HiOps[3] = { LHSH, RHSH, SDValue() };
4175
4176 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
4177 HiOps[2] = Lo.getValue(1);
4178 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
4179
4180 // Legalized the flag result - switch anything that used the old flag to
4181 // use the new one.
4182 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
4183}
4184
4185void DAGTypeLegalizer::ExpandIntRes_SADDSUBO_CARRY(SDNode *N,
4186 SDValue &Lo, SDValue &Hi) {
4187 // Expand the subcomponents.
4188 SDValue LHSL, LHSH, RHSL, RHSH;
4189 SDLoc dl(N);
4190 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
4191 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
4192 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
4193
4194 // We need to use an unsigned carry op for the lo part.
4195 unsigned CarryOp =
4197 Lo = DAG.getNode(CarryOp, dl, VTList, { LHSL, RHSL, N->getOperand(2) });
4198 Hi = DAG.getNode(N->getOpcode(), dl, VTList, { LHSH, RHSH, Lo.getValue(1) });
4199
4200 // Legalized the flag result - switch anything that used the old flag to
4201 // use the new one.
4202 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
4203}
4204
4205void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
4206 SDValue &Lo, SDValue &Hi) {
4207 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4208 SDLoc dl(N);
4209 SDValue Op = N->getOperand(0);
4210 if (Op.getValueType().bitsLE(NVT)) {
4211 // The low part is any extension of the input (which degenerates to a copy).
4212 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
4213 Hi = DAG.getUNDEF(NVT); // The high part is undefined.
4214 } else {
4215 // For example, extension of an i48 to an i64. The operand type necessarily
4216 // promotes to the result type, so will end up being expanded too.
4217 assert(getTypeAction(Op.getValueType()) ==
4219 "Only know how to promote this result!");
4220 SDValue Res = GetPromotedInteger(Op);
4221 assert(Res.getValueType() == N->getValueType(0) &&
4222 "Operand over promoted?");
4223 // Split the promoted operand. This will simplify when it is expanded.
4224 SplitInteger(Res, Lo, Hi);
4225 }
4226}
4227
4228void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
4229 SDValue &Lo, SDValue &Hi) {
4230 SDLoc dl(N);
4231 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4232 EVT NVT = Lo.getValueType();
4233 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
4234 unsigned NVTBits = NVT.getSizeInBits();
4235 unsigned EVTBits = EVT.getSizeInBits();
4236
4237 if (NVTBits < EVTBits) {
4238 Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
4239 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
4240 EVTBits - NVTBits)));
4241 } else {
4242 Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
4243 // The high part replicates the sign bit of Lo, make it explicit.
4244 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
4245 DAG.getShiftAmountConstant(NVTBits - 1, NVT, dl));
4246 }
4247}
4248
4249void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
4250 SDValue &Lo, SDValue &Hi) {
4251 SDLoc dl(N);
4252 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4253 EVT NVT = Lo.getValueType();
4254 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
4255 unsigned NVTBits = NVT.getSizeInBits();
4256 unsigned EVTBits = EVT.getSizeInBits();
4257
4258 if (NVTBits < EVTBits) {
4259 Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
4260 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
4261 EVTBits - NVTBits)));
4262 } else {
4263 Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
4264 // The high part must be zero, make it explicit.
4265 Hi = DAG.getConstant(0, dl, NVT);
4266 }
4267}
4268
4269void DAGTypeLegalizer::ExpandIntRes_BITREVERSE(SDNode *N,
4270 SDValue &Lo, SDValue &Hi) {
4271 SDLoc dl(N);
4272 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
4273 Lo = DAG.getNode(ISD::BITREVERSE, dl, Lo.getValueType(), Lo);
4274 Hi = DAG.getNode(ISD::BITREVERSE, dl, Hi.getValueType(), Hi);
4275}
4276
4277void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
4278 SDValue &Lo, SDValue &Hi) {
4279 SDLoc dl(N);
4280 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
4281 Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
4282 Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
4283}
4284
4285void DAGTypeLegalizer::ExpandIntRes_PARITY(SDNode *N, SDValue &Lo,
4286 SDValue &Hi) {
4287 SDLoc dl(N);
4288 // parity(HiLo) -> parity(Lo^Hi)
4289 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4290 EVT NVT = Lo.getValueType();
4291 Lo =
4292 DAG.getNode(ISD::PARITY, dl, NVT, DAG.getNode(ISD::XOR, dl, NVT, Lo, Hi));
4293 Hi = DAG.getConstant(0, dl, NVT);
4294}
4295
4296void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
4297 SDValue &Lo, SDValue &Hi) {
4298 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4299 unsigned NBitWidth = NVT.getSizeInBits();
4301 const APInt &Cst = Constant->getAPIntValue();
4302 bool IsTarget = Constant->isTargetOpcode();
4303 bool IsOpaque = Constant->isOpaque();
4304 SDLoc dl(N);
4305 Lo = DAG.getConstant(Cst.trunc(NBitWidth), dl, NVT, IsTarget, IsOpaque);
4306 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), dl, NVT, IsTarget,
4307 IsOpaque);
4308}
4309
4310void DAGTypeLegalizer::ExpandIntRes_ABS(SDNode *N, SDValue &Lo, SDValue &Hi) {
4311 SDLoc dl(N);
4312
4313 SDValue N0 = N->getOperand(0);
4314 GetExpandedInteger(N0, Lo, Hi);
4315 EVT NVT = Lo.getValueType();
4316
4317 // If the upper half is all sign bits, then we can perform the ABS on the
4318 // lower half and zero-extend. We could use ISD::ABS_MIN_POISON here if
4319 // DAG.ComputeNumSignBits(N0) is larger than NVT.getScalarSizeInBits() + 1.
4320 unsigned NumSignBits = DAG.ComputeNumSignBits(N0);
4321 if (NumSignBits > NVT.getScalarSizeInBits()) {
4322 unsigned AbsOpc = NumSignBits > NVT.getScalarSizeInBits() + 1
4324 : ISD::ABS;
4325 Lo = DAG.getNode(AbsOpc, dl, NVT, Lo);
4326 Hi = DAG.getConstant(0, dl, NVT);
4327 return;
4328 }
4329
4330 // If we have USUBO_CARRY, use the expanded form of the sra+xor+sub sequence
4331 // we use in LegalizeDAG. The SUB part of the expansion is based on
4332 // ExpandIntRes_ADDSUB which also uses USUBO_CARRY/USUBO after checking that
4333 // USUBO_CARRY is LegalOrCustom. Each of the pieces here can be further
4334 // expanded if needed. Shift expansion has a special case for filling with
4335 // sign bits so that we will only end up with one SRA.
4336 bool HasSubCarry = TLI.isOperationLegalOrCustom(
4337 ISD::USUBO_CARRY, TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
4338 if (HasSubCarry) {
4339 SDValue Sign = DAG.getNode(
4340 ISD::SRA, dl, NVT, Hi,
4341 DAG.getShiftAmountConstant(NVT.getSizeInBits() - 1, NVT, dl));
4342 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
4343 Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Sign);
4344 Hi = DAG.getNode(ISD::XOR, dl, NVT, Hi, Sign);
4345 Lo = DAG.getNode(ISD::USUBO, dl, VTList, Lo, Sign);
4346 Hi = DAG.getNode(ISD::USUBO_CARRY, dl, VTList, Hi, Sign, Lo.getValue(1));
4347 return;
4348 }
4349
4350 // abs(HiLo) -> (Hi < 0 ? -HiLo : HiLo)
4351 EVT VT = N->getValueType(0);
4352 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT,
4353 DAG.getConstant(0, dl, VT), N0);
4354 SDValue NegLo, NegHi;
4355 SplitInteger(Neg, NegLo, NegHi);
4356
4357 SDValue HiIsNeg = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
4358 DAG.getConstant(0, dl, NVT), ISD::SETLT);
4359 Lo = DAG.getSelect(dl, NVT, HiIsNeg, NegLo, Lo);
4360 Hi = DAG.getSelect(dl, NVT, HiIsNeg, NegHi, Hi);
4361}
4362
4363void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
4364 SDValue &Lo, SDValue &Hi) {
4365 SDLoc dl(N);
4366 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
4367 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4368 EVT NVT = Lo.getValueType();
4369
4370 SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
4371 DAG.getConstant(0, dl, NVT), ISD::SETNE);
4372
4373 SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
4374 SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_POISON, dl, NVT, Hi);
4375
4376 Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
4377 DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
4378 DAG.getConstant(NVT.getSizeInBits(), dl,
4379 NVT)));
4380 Hi = DAG.getConstant(0, dl, NVT);
4381}
4382
4383void DAGTypeLegalizer::ExpandIntRes_CTLS(SDNode *N, SDValue &Lo, SDValue &Hi) {
4384 SDLoc dl(N);
4385 // ctls(HiLo) -> if (IsAllSignBits = (ctls(Hi) == BW-1)) then
4386 // BW-1 + clz(IsNegative = (Hi < 0) ? ~Lo : Lo)
4387 // else ctls(Hi)
4388 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4389 EVT NVT = Lo.getValueType();
4390 unsigned NVTBits = NVT.getScalarSizeInBits();
4391
4392 SDValue Constant0 = DAG.getConstant(0, dl, NVT);
4393 SDValue ConstantBWM1 = DAG.getConstant(NVTBits - 1, dl, NVT);
4394
4395 SDValue HiCTLS = DAG.getNode(ISD::CTLS, dl, NVT, Hi);
4396 SDValue IsAllSignBits = DAG.getSetCC(dl, getSetCCResultType(NVT), HiCTLS,
4397 ConstantBWM1, ISD::SETEQ);
4398 SDValue IsNegative =
4399 DAG.getSetCC(dl, getSetCCResultType(NVT), Hi, Constant0, ISD::SETLT);
4400 SDValue AdjustedLo =
4401 DAG.getSelect(dl, NVT, IsNegative, DAG.getNOT(dl, Lo, NVT), Lo);
4402 SDValue LoCLZ = DAG.getNode(ISD::CTLZ, dl, NVT, AdjustedLo);
4403 Lo = DAG.getSelect(dl, NVT, IsAllSignBits,
4404 DAG.getNode(ISD::ADD, dl, NVT, LoCLZ, ConstantBWM1),
4405 HiCTLS);
4406 Hi = DAG.getConstant(0, dl, NVT);
4407}
4408
4409void DAGTypeLegalizer::ExpandIntRes_ABD(SDNode *N, SDValue &Lo, SDValue &Hi) {
4410 SDValue Result = TLI.expandABD(N, DAG);
4411 SplitInteger(Result, Lo, Hi);
4412}
4413
4414void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N, SDValue &Lo, SDValue &Hi) {
4415 SDValue Op = N->getOperand(0);
4416 EVT VT = N->getValueType(0);
4417 SDLoc DL(N);
4418
4419 if (TLI.getOperationAction(ISD::CTPOP, VT) == TargetLoweringBase::LibCall) {
4420 RTLIB::Libcall LC = RTLIB::getCTPOP(VT);
4421 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4422 "LibCall explicitly requested, but not available");
4423
4424 if (RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(LC)) {
4425 TargetLowering::MakeLibCallOptions CallOptions;
4426 EVT IntVT =
4427 EVT::getIntegerVT(*DAG.getContext(), DAG.getLibInfo().getIntSize());
4428 SDValue Res =
4429 TLI.makeLibCall(DAG, LCImpl, IntVT, Op, CallOptions, DL).first;
4430 SplitInteger(DAG.getSExtOrTrunc(Res, DL, VT), Lo, Hi);
4431 return;
4432 }
4433
4434 // If the function is not available, fall back on the expansion.
4435 }
4436
4437 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
4438 GetExpandedInteger(Op, Lo, Hi);
4439 EVT NVT = Lo.getValueType();
4440 Lo = DAG.getNode(ISD::ADD, DL, NVT, DAG.getNode(ISD::CTPOP, DL, NVT, Lo),
4441 DAG.getNode(ISD::CTPOP, DL, NVT, Hi));
4442 Hi = DAG.getConstant(0, DL, NVT);
4443}
4444
4445void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
4446 SDValue &Lo, SDValue &Hi) {
4447 SDLoc dl(N);
4448 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
4449 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4450 EVT NVT = Lo.getValueType();
4451
4452 SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
4453 DAG.getConstant(0, dl, NVT), ISD::SETNE);
4454
4455 SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_POISON, dl, NVT, Lo);
4456 SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
4457
4458 Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
4459 DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
4460 DAG.getConstant(NVT.getSizeInBits(), dl,
4461 NVT)));
4462 Hi = DAG.getConstant(0, dl, NVT);
4463}
4464
4465void DAGTypeLegalizer::ExpandIntRes_GET_ROUNDING(SDNode *N, SDValue &Lo,
4466 SDValue &Hi) {
4467 SDLoc dl(N);
4468 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4469 unsigned NBitWidth = NVT.getSizeInBits();
4470
4471 Lo = DAG.getNode(ISD::GET_ROUNDING, dl, {NVT, MVT::Other}, N->getOperand(0));
4472 SDValue Chain = Lo.getValue(1);
4473 // The high part is the sign of Lo, as -1 is a valid value for GET_ROUNDING
4474 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
4475 DAG.getShiftAmountConstant(NBitWidth - 1, NVT, dl));
4476
4477 // Legalize the chain result - switch anything that used the old chain to
4478 // use the new one.
4479 ReplaceValueWith(SDValue(N, 1), Chain);
4480}
4481
4482// Helper for producing an FP_EXTEND/STRICT_FP_EXTEND of Op.
4483static SDValue fpExtendHelper(SDValue Op, SDValue &Chain, bool IsStrict, EVT VT,
4484 SDLoc DL, SelectionDAG &DAG) {
4485 if (IsStrict) {
4486 Op = DAG.getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op});
4487 Chain = Op.getValue(1);
4488 return Op;
4489 }
4490 return DAG.getNode(ISD::FP_EXTEND, DL, VT, Op);
4491}
4492
4493void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT(SDNode *N, SDValue &Lo,
4494 SDValue &Hi) {
4495 SDLoc dl(N);
4496 EVT VT = N->getValueType(0);
4497
4498 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
4499 N->getOpcode() == ISD::STRICT_FP_TO_SINT;
4500 bool IsStrict = N->isStrictFPOpcode();
4501 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
4502 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
4503
4504 // If the input is bf16 or needs to be soft promoted, extend to f32.
4505 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftPromoteHalf ||
4506 Op.getValueType() == MVT::bf16) {
4507 Op = fpExtendHelper(Op, Chain, IsStrict, MVT::f32, dl, DAG);
4508 }
4509
4510 // NOTE: We need a variable that lives across makeLibCall so
4511 // CallOptions.setTypeListBeforeSoften can save a reference to it.
4512 EVT OpVT = Op.getValueType();
4513
4514 RTLIB::Libcall LC =
4515 IsSigned ? RTLIB::getFPTOSINT(OpVT, VT) : RTLIB::getFPTOUINT(OpVT, VT);
4516 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-xint conversion!");
4517 TargetLowering::MakeLibCallOptions CallOptions;
4518 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftenFloat)
4519 CallOptions.setTypeListBeforeSoften(OpVT, VT);
4520 else
4521 CallOptions.setIsSigned(true); // FIXME: Is this needed?
4522 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, VT, Op,
4523 CallOptions, dl, Chain);
4524 SplitInteger(Tmp.first, Lo, Hi);
4525
4526 if (IsStrict)
4527 ReplaceValueWith(SDValue(N, 1), Tmp.second);
4528}
4529
4530void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT_SAT(SDNode *N, SDValue &Lo,
4531 SDValue &Hi) {
4532 SDValue Res = TLI.expandFP_TO_INT_SAT(N, DAG);
4533 SplitInteger(Res, Lo, Hi);
4534}
4535
4536void DAGTypeLegalizer::ExpandIntRes_XROUND_XRINT(SDNode *N, SDValue &Lo,
4537 SDValue &Hi) {
4538 SDLoc dl(N);
4539 bool IsStrict = N->isStrictFPOpcode();
4540 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
4541 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
4542
4543 EVT VT = Op.getValueType();
4544
4545 if (VT == MVT::f16) {
4546 // Extend to f32.
4547 VT = MVT::f32;
4548 Op = fpExtendHelper(Op, Chain, IsStrict, VT, dl, DAG);
4549 }
4550
4551 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4552 if (N->getOpcode() == ISD::LROUND ||
4553 N->getOpcode() == ISD::STRICT_LROUND) {
4554 LC = RTLIB::getLROUND(VT);
4555 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected lround input type!");
4556 } else if (N->getOpcode() == ISD::LRINT ||
4557 N->getOpcode() == ISD::STRICT_LRINT) {
4558 LC = RTLIB::getLRINT(VT);
4559 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected lrint input type!");
4560 } else if (N->getOpcode() == ISD::LLROUND ||
4561 N->getOpcode() == ISD::STRICT_LLROUND) {
4562 LC = RTLIB::getLLROUND(VT);
4563 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llround input type!");
4564 } else if (N->getOpcode() == ISD::LLRINT ||
4565 N->getOpcode() == ISD::STRICT_LLRINT) {
4566 LC = RTLIB::getLLRINT(VT);
4567 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llrint input type!");
4568 } else
4569 llvm_unreachable("Unexpected opcode!");
4570
4571 EVT RetVT = N->getValueType(0);
4572
4573 TargetLowering::MakeLibCallOptions CallOptions;
4574 CallOptions.setIsSigned(true);
4575 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
4576 Op, CallOptions, dl,
4577 Chain);
4578 SplitInteger(Tmp.first, Lo, Hi);
4579
4580 if (N->isStrictFPOpcode())
4581 ReplaceValueWith(SDValue(N, 1), Tmp.second);
4582}
4583
4584void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
4585 SDValue &Lo, SDValue &Hi) {
4586 assert(!N->isAtomic() && "Should have been a ATOMIC_LOAD?");
4587
4588 if (ISD::isNormalLoad(N)) {
4589 ExpandRes_NormalLoad(N, Lo, Hi);
4590 return;
4591 }
4592
4593 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
4594
4595 EVT VT = N->getValueType(0);
4596 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4597 SDValue Ch = N->getChain();
4598 SDValue Ptr = N->getBasePtr();
4599 ISD::LoadExtType ExtType = N->getExtensionType();
4600 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
4601 AAMDNodes AAInfo = N->getAAInfo();
4602 SDLoc dl(N);
4603
4604 assert(NVT.isByteSized() && "Expanded type not byte sized!");
4605
4606 if (N->getMemoryVT().bitsLE(NVT)) {
4607 EVT MemVT = N->getMemoryVT();
4608
4609 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(), MemVT,
4610 N->getBaseAlign(), MMOFlags, AAInfo);
4611
4612 // Remember the chain.
4613 Ch = Lo.getValue(1);
4614
4615 if (ExtType == ISD::SEXTLOAD) {
4616 // The high part is obtained by SRA'ing all but one of the bits of the
4617 // lo part.
4618 unsigned LoSize = Lo.getValueSizeInBits();
4619 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
4620 DAG.getShiftAmountConstant(LoSize - 1, NVT, dl));
4621 } else if (ExtType == ISD::ZEXTLOAD) {
4622 // The high part is just a zero.
4623 Hi = DAG.getConstant(0, dl, NVT);
4624 } else {
4625 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
4626 // The high part is undefined.
4627 Hi = DAG.getUNDEF(NVT);
4628 }
4629 } else if (DAG.getDataLayout().isLittleEndian()) {
4630 // Little-endian - low bits are at low addresses.
4631 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(), N->getBaseAlign(),
4632 MMOFlags, AAInfo);
4633
4634 unsigned ExcessBits =
4635 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
4636 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
4637
4638 // Increment the pointer to the other half.
4639 unsigned IncrementSize = NVT.getSizeInBits()/8;
4640 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
4641 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
4642 N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
4643 N->getBaseAlign(), MMOFlags, AAInfo);
4644
4645 // Build a factor node to remember that this load is independent of the
4646 // other one.
4647 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
4648 Hi.getValue(1));
4649 } else {
4650 // Big-endian - high bits are at low addresses. Favor aligned loads at
4651 // the cost of some bit-fiddling.
4652 EVT MemVT = N->getMemoryVT();
4653 unsigned EBytes = MemVT.getStoreSize();
4654 unsigned IncrementSize = NVT.getSizeInBits()/8;
4655 unsigned ExcessBits = (EBytes - IncrementSize)*8;
4656
4657 // Load both the high bits and maybe some of the low bits.
4658 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
4659 EVT::getIntegerVT(*DAG.getContext(),
4660 MemVT.getSizeInBits() - ExcessBits),
4661 N->getBaseAlign(), MMOFlags, AAInfo);
4662
4663 // Increment the pointer to the other half.
4664 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
4665 // Load the rest of the low bits.
4666 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
4667 N->getPointerInfo().getWithOffset(IncrementSize),
4668 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
4669 N->getBaseAlign(), MMOFlags, AAInfo);
4670
4671 // Build a factor node to remember that this load is independent of the
4672 // other one.
4673 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
4674 Hi.getValue(1));
4675
4676 if (ExcessBits < NVT.getSizeInBits()) {
4677 // Transfer low bits from the bottom of Hi to the top of Lo.
4678 Lo = DAG.getNode(
4679 ISD::OR, dl, NVT, Lo,
4680 DAG.getNode(ISD::SHL, dl, NVT, Hi,
4681 DAG.getShiftAmountConstant(ExcessBits, NVT, dl)));
4682 // Move high bits to the right position in Hi.
4683 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, NVT,
4684 Hi,
4685 DAG.getShiftAmountConstant(
4686 NVT.getSizeInBits() - ExcessBits, NVT, dl));
4687 }
4688 }
4689
4690 // Legalize the chain result - switch anything that used the old chain to
4691 // use the new one.
4692 ReplaceValueWith(SDValue(N, 1), Ch);
4693}
4694
4695void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
4696 SDValue &Lo, SDValue &Hi) {
4697 SDLoc dl(N);
4698 SDValue LL, LH, RL, RH;
4699 GetExpandedInteger(N->getOperand(0), LL, LH);
4700 GetExpandedInteger(N->getOperand(1), RL, RH);
4701
4702 SDNodeFlags Flags;
4703 if (N->getOpcode() == ISD::OR)
4704 Flags.setDisjoint(N->getFlags().hasDisjoint());
4705
4706 Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL, Flags);
4707 Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH, Flags);
4708}
4709
4710void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
4711 SDValue &Lo, SDValue &Hi) {
4712 EVT VT = N->getValueType(0);
4713 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4714 SDLoc dl(N);
4715
4716 SDValue LL, LH, RL, RH;
4717 GetExpandedInteger(N->getOperand(0), LL, LH);
4718 GetExpandedInteger(N->getOperand(1), RL, RH);
4719
4720 if (TLI.expandMUL(N, Lo, Hi, NVT, DAG,
4722 LL, LH, RL, RH))
4723 return;
4724
4725 // If nothing else, we can make a libcall.
4726 RTLIB::Libcall LC = RTLIB::getMUL(VT);
4727 RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(LC);
4728 if (LCImpl == RTLIB::Unsupported) {
4729 // Perform a wide multiplication where the wide type is the original VT and
4730 // the 4 parts are the split arguments.
4731 TLI.forceExpandMultiply(DAG, dl, /*Signed=*/false, Lo, Hi, LL, RL, LH, RH);
4732 return;
4733 }
4734
4735 // Note that we don't need to do a wide MUL here since we don't care about the
4736 // upper half of the result if it exceeds VT.
4737 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
4738 TargetLowering::MakeLibCallOptions CallOptions;
4739 CallOptions.setIsSigned(true);
4740 SplitInteger(TLI.makeLibCall(DAG, LCImpl, VT, Ops, CallOptions, dl).first, Lo,
4741 Hi);
4742}
4743
4744void DAGTypeLegalizer::ExpandIntRes_READCOUNTER(SDNode *N, SDValue &Lo,
4745 SDValue &Hi) {
4746 SDLoc DL(N);
4747 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4748 SDVTList VTs = DAG.getVTList(NVT, NVT, MVT::Other);
4749 SDValue R = DAG.getNode(N->getOpcode(), DL, VTs, N->getOperand(0));
4750 Lo = R.getValue(0);
4751 Hi = R.getValue(1);
4752 ReplaceValueWith(SDValue(N, 1), R.getValue(2));
4753}
4754
4755void DAGTypeLegalizer::ExpandIntRes_AVG(SDNode *N, SDValue &Lo, SDValue &Hi) {
4756 SDValue Result = TLI.expandAVG(N, DAG);
4757 SplitInteger(Result, Lo, Hi);
4758}
4759
4760void DAGTypeLegalizer::ExpandIntRes_ADDSUBSAT(SDNode *N, SDValue &Lo,
4761 SDValue &Hi) {
4762 SDValue Result = TLI.expandAddSubSat(N, DAG);
4763 SplitInteger(Result, Lo, Hi);
4764}
4765
4766void DAGTypeLegalizer::ExpandIntRes_SHLSAT(SDNode *N, SDValue &Lo,
4767 SDValue &Hi) {
4768 SDValue Result = TLI.expandShlSat(N, DAG);
4769 SplitInteger(Result, Lo, Hi);
4770}
4771
4772/// This performs an expansion of the integer result for a fixed point
4773/// multiplication. The default expansion performs rounding down towards
4774/// negative infinity, though targets that do care about rounding should specify
4775/// a target hook for rounding and provide their own expansion or lowering of
4776/// fixed point multiplication to be consistent with rounding.
4777void DAGTypeLegalizer::ExpandIntRes_MULFIX(SDNode *N, SDValue &Lo,
4778 SDValue &Hi) {
4779 SDLoc dl(N);
4780 EVT VT = N->getValueType(0);
4781 unsigned VTSize = VT.getScalarSizeInBits();
4782 SDValue LHS = N->getOperand(0);
4783 SDValue RHS = N->getOperand(1);
4784 uint64_t Scale = N->getConstantOperandVal(2);
4785 bool Saturating = (N->getOpcode() == ISD::SMULFIXSAT ||
4786 N->getOpcode() == ISD::UMULFIXSAT);
4787 bool Signed = (N->getOpcode() == ISD::SMULFIX ||
4788 N->getOpcode() == ISD::SMULFIXSAT);
4789
4790 // Handle special case when scale is equal to zero.
4791 if (!Scale) {
4793 if (!Saturating) {
4794 Result = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
4795 } else {
4796 EVT BoolVT = getSetCCResultType(VT);
4797 unsigned MulOp = Signed ? ISD::SMULO : ISD::UMULO;
4798 Result = DAG.getNode(MulOp, dl, DAG.getVTList(VT, BoolVT), LHS, RHS);
4799 SDValue Product = Result.getValue(0);
4800 SDValue Overflow = Result.getValue(1);
4801 if (Signed) {
4802 APInt MinVal = APInt::getSignedMinValue(VTSize);
4803 APInt MaxVal = APInt::getSignedMaxValue(VTSize);
4804 SDValue SatMin = DAG.getConstant(MinVal, dl, VT);
4805 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
4806 SDValue Zero = DAG.getConstant(0, dl, VT);
4807 // Xor the inputs, if resulting sign bit is 0 the product will be
4808 // positive, else negative.
4809 SDValue Xor = DAG.getNode(ISD::XOR, dl, VT, LHS, RHS);
4810 SDValue ProdNeg = DAG.getSetCC(dl, BoolVT, Xor, Zero, ISD::SETLT);
4811 Result = DAG.getSelect(dl, VT, ProdNeg, SatMin, SatMax);
4812 Result = DAG.getSelect(dl, VT, Overflow, Result, Product);
4813 } else {
4814 // For unsigned multiplication, we only need to check the max since we
4815 // can't really overflow towards zero.
4816 APInt MaxVal = APInt::getMaxValue(VTSize);
4817 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
4818 Result = DAG.getSelect(dl, VT, Overflow, SatMax, Product);
4819 }
4820 }
4821 SplitInteger(Result, Lo, Hi);
4822 return;
4823 }
4824
4825 // For SMULFIX[SAT] we only expect to find Scale<VTSize, but this assert will
4826 // cover for unhandled cases below, while still being valid for UMULFIX[SAT].
4827 assert(Scale <= VTSize && "Scale can't be larger than the value type size.");
4828
4829 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4830 SDValue LL, LH, RL, RH;
4831 GetExpandedInteger(LHS, LL, LH);
4832 GetExpandedInteger(RHS, RL, RH);
4834
4835 unsigned LoHiOp = Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI;
4836 if (!TLI.expandMUL_LOHI(LoHiOp, VT, dl, LHS, RHS, Result, NVT, DAG,
4838 LL, LH, RL, RH)) {
4839 Result.clear();
4840 Result.resize(4);
4841
4842 SDValue LoTmp, HiTmp;
4843 TLI.forceExpandWideMUL(DAG, dl, Signed, LHS, RHS, LoTmp, HiTmp);
4844 SplitInteger(LoTmp, Result[0], Result[1]);
4845 SplitInteger(HiTmp, Result[2], Result[3]);
4846 }
4847 assert(Result.size() == 4 && "Unexpected number of partlets in the result");
4848
4849 unsigned NVTSize = NVT.getScalarSizeInBits();
4850 assert((VTSize == NVTSize * 2) && "Expected the new value type to be half "
4851 "the size of the current value type");
4852
4853 // After getting the multiplication result in 4 parts, we need to perform a
4854 // shift right by the amount of the scale to get the result in that scale.
4855 //
4856 // Let's say we multiply 2 64 bit numbers. The resulting value can be held in
4857 // 128 bits that are cut into 4 32-bit parts:
4858 //
4859 // HH HL LH LL
4860 // |---32---|---32---|---32---|---32---|
4861 // 128 96 64 32 0
4862 //
4863 // |------VTSize-----|
4864 //
4865 // |NVTSize-|
4866 //
4867 // The resulting Lo and Hi would normally be in LL and LH after the shift. But
4868 // to avoid unneccessary shifting of all 4 parts, we can adjust the shift
4869 // amount and get Lo and Hi using two funnel shifts. Or for the special case
4870 // when Scale is a multiple of NVTSize we can just pick the result without
4871 // shifting.
4872 uint64_t Part0 = Scale / NVTSize; // Part holding lowest bit needed.
4873 if (Scale % NVTSize) {
4874 SDValue ShiftAmount = DAG.getShiftAmountConstant(Scale % NVTSize, NVT, dl);
4875 Lo = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 1], Result[Part0],
4876 ShiftAmount);
4877 Hi = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 2], Result[Part0 + 1],
4878 ShiftAmount);
4879 } else {
4880 Lo = Result[Part0];
4881 Hi = Result[Part0 + 1];
4882 }
4883
4884 // Unless saturation is requested we are done. The result is in <Hi,Lo>.
4885 if (!Saturating)
4886 return;
4887
4888 // Can not overflow when there is no integer part.
4889 if (Scale == VTSize)
4890 return;
4891
4892 // To handle saturation we must check for overflow in the multiplication.
4893 //
4894 // Unsigned overflow happened if the upper (VTSize - Scale) bits (of Result)
4895 // aren't all zeroes.
4896 //
4897 // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of Result)
4898 // aren't all ones or all zeroes.
4899 //
4900 // We cannot overflow past HH when multiplying 2 ints of size VTSize, so the
4901 // highest bit of HH determines saturation direction in the event of signed
4902 // saturation.
4903
4904 SDValue ResultHL = Result[2];
4905 SDValue ResultHH = Result[3];
4906
4907 SDValue SatMax, SatMin;
4908 SDValue NVTZero = DAG.getConstant(0, dl, NVT);
4909 SDValue NVTNeg1 = DAG.getAllOnesConstant(dl, NVT);
4910 EVT BoolNVT = getSetCCResultType(NVT);
4911
4912 if (!Signed) {
4913 if (Scale < NVTSize) {
4914 // Overflow happened if ((HH | (HL >> Scale)) != 0).
4915 SDValue HLAdjusted =
4916 DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
4917 DAG.getShiftAmountConstant(Scale, NVT, dl));
4918 SDValue Tmp = DAG.getNode(ISD::OR, dl, NVT, HLAdjusted, ResultHH);
4919 SatMax = DAG.getSetCC(dl, BoolNVT, Tmp, NVTZero, ISD::SETNE);
4920 } else if (Scale == NVTSize) {
4921 // Overflow happened if (HH != 0).
4922 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETNE);
4923 } else if (Scale < VTSize) {
4924 // Overflow happened if ((HH >> (Scale - NVTSize)) != 0).
4925 SDValue HLAdjusted =
4926 DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
4927 DAG.getShiftAmountConstant(Scale - NVTSize, NVT, dl));
4928 SatMax = DAG.getSetCC(dl, BoolNVT, HLAdjusted, NVTZero, ISD::SETNE);
4929 } else
4930 llvm_unreachable("Scale must be less or equal to VTSize for UMULFIXSAT"
4931 "(and saturation can't happen with Scale==VTSize).");
4932
4933 Hi = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Hi);
4934 Lo = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Lo);
4935 return;
4936 }
4937
4938 if (Scale < NVTSize) {
4939 // The number of overflow bits we can check are VTSize - Scale + 1 (we
4940 // include the sign bit). If these top bits are > 0, then we overflowed past
4941 // the max value. If these top bits are < -1, then we overflowed past the
4942 // min value. Otherwise, we did not overflow.
4943 unsigned OverflowBits = VTSize - Scale + 1;
4944 assert(OverflowBits <= VTSize && OverflowBits > NVTSize &&
4945 "Extent of overflow bits must start within HL");
4946 SDValue HLHiMask = DAG.getConstant(
4947 APInt::getHighBitsSet(NVTSize, OverflowBits - NVTSize), dl, NVT);
4948 SDValue HLLoMask = DAG.getConstant(
4949 APInt::getLowBitsSet(NVTSize, VTSize - OverflowBits), dl, NVT);
4950 // We overflow max if HH > 0 or (HH == 0 && HL > HLLoMask).
4951 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
4952 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
4953 SDValue HLUGT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLLoMask, ISD::SETUGT);
4954 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
4955 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLUGT));
4956 // We overflow min if HH < -1 or (HH == -1 && HL < HLHiMask).
4957 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
4958 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
4959 SDValue HLULT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLHiMask, ISD::SETULT);
4960 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
4961 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLULT));
4962 } else if (Scale == NVTSize) {
4963 // We overflow max if HH > 0 or (HH == 0 && HL sign bit is 1).
4964 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
4965 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
4966 SDValue HLNeg = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETLT);
4967 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
4968 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLNeg));
4969 // We overflow min if HH < -1 or (HH == -1 && HL sign bit is 0).
4970 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
4971 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
4972 SDValue HLPos = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETGE);
4973 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
4974 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLPos));
4975 } else if (Scale < VTSize) {
4976 // This is similar to the case when we saturate if Scale < NVTSize, but we
4977 // only need to check HH.
4978 unsigned OverflowBits = VTSize - Scale + 1;
4979 SDValue HHHiMask = DAG.getConstant(
4980 APInt::getHighBitsSet(NVTSize, OverflowBits), dl, NVT);
4981 SDValue HHLoMask = DAG.getConstant(
4982 APInt::getLowBitsSet(NVTSize, NVTSize - OverflowBits), dl, NVT);
4983 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, HHLoMask, ISD::SETGT);
4984 SatMin = DAG.getSetCC(dl, BoolNVT, ResultHH, HHHiMask, ISD::SETLT);
4985 } else
4986 llvm_unreachable("Illegal scale for signed fixed point mul.");
4987
4988 // Saturate to signed maximum.
4989 APInt MaxHi = APInt::getSignedMaxValue(NVTSize);
4990 APInt MaxLo = APInt::getAllOnes(NVTSize);
4991 Hi = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxHi, dl, NVT), Hi);
4992 Lo = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxLo, dl, NVT), Lo);
4993 // Saturate to signed minimum.
4994 APInt MinHi = APInt::getSignedMinValue(NVTSize);
4995 Hi = DAG.getSelect(dl, NVT, SatMin, DAG.getConstant(MinHi, dl, NVT), Hi);
4996 Lo = DAG.getSelect(dl, NVT, SatMin, NVTZero, Lo);
4997}
4998
4999void DAGTypeLegalizer::ExpandIntRes_DIVFIX(SDNode *N, SDValue &Lo,
5000 SDValue &Hi) {
5001 SDLoc dl(N);
5002 // Try expanding in the existing type first.
5003 SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, N->getOperand(0),
5004 N->getOperand(1),
5005 N->getConstantOperandVal(2), DAG);
5006
5007 if (!Res)
5008 Res = earlyExpandDIVFIX(N, N->getOperand(0), N->getOperand(1),
5009 N->getConstantOperandVal(2), TLI, DAG);
5010 SplitInteger(Res, Lo, Hi);
5011}
5012
5013void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
5014 SDValue &Lo, SDValue &Hi) {
5015 assert((Node->getOpcode() == ISD::SADDO || Node->getOpcode() == ISD::SSUBO) &&
5016 "Node has unexpected Opcode");
5017 SDValue LHS = Node->getOperand(0);
5018 SDValue RHS = Node->getOperand(1);
5019 SDLoc dl(Node);
5020
5021 SDValue Ovf;
5022
5023 bool IsAdd = Node->getOpcode() == ISD::SADDO;
5024 unsigned CarryOp = IsAdd ? ISD::SADDO_CARRY : ISD::SSUBO_CARRY;
5025
5026 bool HasCarryOp = TLI.isOperationLegalOrCustom(
5027 CarryOp, TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
5028
5029 if (HasCarryOp) {
5030 // Expand the subcomponents.
5031 SDValue LHSL, LHSH, RHSL, RHSH;
5032 GetExpandedInteger(LHS, LHSL, LHSH);
5033 GetExpandedInteger(RHS, RHSL, RHSH);
5034 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), Node->getValueType(1));
5035
5036 Lo = DAG.getNode(IsAdd ? ISD::UADDO : ISD::USUBO, dl, VTList, {LHSL, RHSL});
5037 Hi = DAG.getNode(CarryOp, dl, VTList, { LHSH, RHSH, Lo.getValue(1) });
5038
5039 Ovf = Hi.getValue(1);
5040 } else {
5041 // Expand the result by simply replacing it with the equivalent
5042 // non-overflow-checking operation.
5043 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
5044 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
5045 LHS, RHS);
5046 SplitInteger(Sum, Lo, Hi);
5047
5048 // Compute the overflow.
5049 //
5050 // LHSSign -> LHS < 0
5051 // RHSSign -> RHS < 0
5052 // SumSign -> Sum < 0
5053 //
5054 // Add:
5055 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
5056 // Sub:
5057 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
5058 //
5059 // To get better codegen we can rewrite this by doing bitwise math on
5060 // the integers and extract the final sign bit at the end. So the
5061 // above becomes:
5062 //
5063 // Add:
5064 // Overflow -> (~(LHS ^ RHS) & (LHS ^ Sum)) < 0
5065 // Sub:
5066 // Overflow -> ((LHS ^ RHS) & (LHS ^ Sum)) < 0
5067 //
5068 // NOTE: This is different than the expansion we do in expandSADDSUBO
5069 // because it is more costly to implement the same overflow predicate with
5070 // SETCC nodes when the integers are split.
5071 EVT VT = LHS.getValueType();
5072 SDValue SignsMatch = DAG.getNode(ISD::XOR, dl, VT, LHS, RHS);
5073 if (IsAdd)
5074 SignsMatch = DAG.getNOT(dl, SignsMatch, VT);
5075
5076 SDValue SumSignNE = DAG.getNode(ISD::XOR, dl, VT, LHS, Sum);
5077 Ovf = DAG.getNode(ISD::AND, dl, VT, SignsMatch, SumSignNE);
5078 EVT OType = Node->getValueType(1);
5079 Ovf = DAG.getSetCC(dl, OType, Ovf, DAG.getConstant(0, dl, VT), ISD::SETLT);
5080 }
5081
5082 // Use the calculated overflow everywhere.
5083 ReplaceValueWith(SDValue(Node, 1), Ovf);
5084}
5085
5086void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
5087 SDValue &Lo, SDValue &Hi) {
5088 EVT VT = N->getValueType(0);
5089 SDLoc dl(N);
5090 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
5091
5092 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
5093 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
5094 SplitInteger(Res.getValue(0), Lo, Hi);
5095 return;
5096 }
5097
5098 RTLIB::Libcall LC = RTLIB::getSDIV(VT);
5099 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
5100
5101 TargetLowering::MakeLibCallOptions CallOptions;
5102 CallOptions.setIsSigned(true);
5103 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
5104}
5105
5106void DAGTypeLegalizer::ExpandIntRes_ShiftThroughStack(SDNode *N, SDValue &Lo,
5107 SDValue &Hi) {
5108 SDLoc dl(N);
5109 SDValue Shiftee = N->getOperand(0);
5110 EVT VT = Shiftee.getValueType();
5111 SDValue ShAmt = N->getOperand(1);
5112 EVT ShAmtVT = ShAmt.getValueType();
5113
5114 EVT LoadVT = VT;
5115 do {
5116 LoadVT = TLI.getTypeToTransformTo(*DAG.getContext(), LoadVT);
5117 } while (!TLI.isTypeLegal(LoadVT));
5118
5119 const unsigned ShiftUnitInBits = LoadVT.getStoreSizeInBits();
5120 assert(ShiftUnitInBits <= VT.getScalarSizeInBits());
5121 assert(isPowerOf2_32(ShiftUnitInBits) &&
5122 "Shifting unit is not a a power of two!");
5123
5124 const bool IsOneStepShift =
5125 DAG.computeKnownBits(ShAmt).countMinTrailingZeros() >=
5126 Log2_32(ShiftUnitInBits);
5127
5128 // If we can't do it as one step, we'll have two uses of shift amount,
5129 // and thus must freeze it.
5130 if (!IsOneStepShift)
5131 ShAmt = DAG.getFreeze(ShAmt);
5132
5133 unsigned VTBitWidth = VT.getScalarSizeInBits();
5134 assert(VTBitWidth % 8 == 0 && "Shifting a not byte multiple value?");
5135 unsigned VTByteWidth = VTBitWidth / 8;
5136 assert(isPowerOf2_32(VTByteWidth) &&
5137 "Shiftee type size is not a power of two!");
5138 unsigned StackSlotByteWidth = 2 * VTByteWidth;
5139 unsigned StackSlotBitWidth = 8 * StackSlotByteWidth;
5140 EVT StackSlotVT = EVT::getIntegerVT(*DAG.getContext(), StackSlotBitWidth);
5141
5142 // Get a temporary stack slot 2x the width of our VT.
5143 // FIXME: reuse stack slots?
5144 Align StackAlign = DAG.getReducedAlign(StackSlotVT, /*UseABI=*/false);
5146 DAG.CreateStackTemporary(StackSlotVT.getStoreSize(), StackAlign);
5147 EVT PtrTy = StackPtr.getValueType();
5148 SDValue Ch = DAG.getEntryNode();
5149
5150 MachinePointerInfo StackPtrInfo = MachinePointerInfo::getFixedStack(
5151 DAG.getMachineFunction(),
5152 cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex());
5153
5154 // Extend the value, that is being shifted, to the entire stack slot's width.
5155 SDValue Init;
5156 if (N->getOpcode() != ISD::SHL) {
5157 unsigned WideningOpc =
5158 N->getOpcode() == ISD::SRA ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
5159 Init = DAG.getNode(WideningOpc, dl, StackSlotVT, Shiftee);
5160 } else {
5161 // For left-shifts, pad the Shiftee's LSB with zeros to twice it's width.
5162 SDValue AllZeros = DAG.getConstant(0, dl, VT);
5163 Init = DAG.getNode(ISD::BUILD_PAIR, dl, StackSlotVT, AllZeros, Shiftee);
5164 }
5165 // And spill it into the stack slot.
5166 Ch = DAG.getStore(Ch, dl, Init, StackPtr, StackPtrInfo, StackAlign);
5167
5168 // Now, compute the full-byte offset into stack slot from where we can load.
5169 // We have shift amount, which is in bits. Offset should point to an aligned
5170 // address.
5171 SDNodeFlags Flags;
5172 Flags.setExact(IsOneStepShift);
5173 SDValue SrlTmp = DAG.getNode(
5174 ISD::SRL, dl, ShAmtVT, ShAmt,
5175 DAG.getConstant(Log2_32(ShiftUnitInBits), dl, ShAmtVT), Flags);
5176 SDValue BitOffset =
5177 DAG.getNode(ISD::SHL, dl, ShAmtVT, SrlTmp,
5178 DAG.getConstant(Log2_32(ShiftUnitInBits), dl, ShAmtVT));
5179
5180 SDValue ByteOffset =
5181 DAG.getNode(ISD::SRL, dl, ShAmtVT, BitOffset,
5182 DAG.getConstant(3, dl, ShAmtVT), SDNodeFlags::Exact);
5183 // And clamp it, because OOB load is an immediate UB,
5184 // while shift overflow would have *just* been poison.
5185 ByteOffset = DAG.getNode(ISD::AND, dl, ShAmtVT, ByteOffset,
5186 DAG.getConstant(VTByteWidth - 1, dl, ShAmtVT));
5187 // We have exactly two strategies on indexing into stack slot here:
5188 // 1. upwards starting from the beginning of the slot
5189 // 2. downwards starting from the middle of the slot
5190 // On little-endian machine, we pick 1. for right shifts and 2. for left-shift
5191 // and vice versa on big-endian machine.
5192 bool WillIndexUpwards = N->getOpcode() != ISD::SHL;
5193 if (DAG.getDataLayout().isBigEndian())
5194 WillIndexUpwards = !WillIndexUpwards;
5195
5196 SDValue AdjStackPtr;
5197 if (WillIndexUpwards) {
5198 AdjStackPtr = StackPtr;
5199 } else {
5200 AdjStackPtr = DAG.getMemBasePlusOffset(
5201 StackPtr, DAG.getConstant(VTByteWidth, dl, PtrTy), dl);
5202 ByteOffset = DAG.getNegative(ByteOffset, dl, ShAmtVT);
5203 }
5204
5205 // Get the pointer somewhere into the stack slot from which we need to load.
5206 ByteOffset = DAG.getSExtOrTrunc(ByteOffset, dl, PtrTy);
5207 AdjStackPtr = DAG.getMemBasePlusOffset(AdjStackPtr, ByteOffset, dl);
5208
5209 // And load it! While the load is not legal, legalizing it is obvious.
5210 SDValue Res =
5211 DAG.getLoad(VT, dl, Ch, AdjStackPtr,
5212 MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
5213 commonAlignment(StackAlign, LoadVT.getStoreSize()));
5214
5215 // If we may still have a remaining bits to shift by, do so now.
5216 if (!IsOneStepShift) {
5217 SDValue ShAmtRem =
5218 DAG.getNode(ISD::AND, dl, ShAmtVT, ShAmt,
5219 DAG.getConstant(ShiftUnitInBits - 1, dl, ShAmtVT));
5220 Res = DAG.getNode(N->getOpcode(), dl, VT, Res, ShAmtRem);
5221 }
5222
5223 // Finally, split the computed value.
5224 SplitInteger(Res, Lo, Hi);
5225}
5226
5227void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
5228 SDValue &Lo, SDValue &Hi) {
5229 EVT VT = N->getValueType(0);
5230 unsigned Opc = N->getOpcode();
5231 SDLoc dl(N);
5232
5233 // If we can emit an efficient shift operation, do so now. Check to see if
5234 // the RHS is a constant.
5235 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
5236 return ExpandShiftByConstant(N, CN->getAPIntValue(), Lo, Hi);
5237
5238 // If we can determine that the high bit of the shift is zero or one, even if
5239 // the low bits are variable, emit this shift in an optimized form.
5240 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
5241 return;
5242
5243 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
5244 unsigned PartsOpc;
5245 if (Opc == ISD::SHL) {
5246 PartsOpc = ISD::SHL_PARTS;
5247 } else if (Opc == ISD::SRL) {
5248 PartsOpc = ISD::SRL_PARTS;
5249 } else {
5250 assert(Opc == ISD::SRA && "Unknown shift!");
5251 PartsOpc = ISD::SRA_PARTS;
5252 }
5253
5254 // Next check to see if the target supports this SHL_PARTS operation or if it
5255 // will custom expand it. Don't lower this to SHL_PARTS when we optimise for
5256 // size, but create a libcall instead.
5257 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
5258 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
5259 const bool LegalOrCustom =
5260 (Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5261 Action == TargetLowering::Custom;
5262
5263 unsigned ExpansionFactor = 1;
5264 // That VT->NVT expansion is one step. But will we re-expand NVT?
5265 for (EVT TmpVT = NVT;;) {
5266 EVT NewTMPVT = TLI.getTypeToTransformTo(*DAG.getContext(), TmpVT);
5267 if (NewTMPVT == TmpVT)
5268 break;
5269 TmpVT = NewTMPVT;
5270 ++ExpansionFactor;
5271 }
5272
5274 TLI.preferredShiftLegalizationStrategy(DAG, N, ExpansionFactor);
5275
5277 return ExpandIntRes_ShiftThroughStack(N, Lo, Hi);
5278
5279 if (LegalOrCustom &&
5281 // Expand the subcomponents.
5282 SDValue LHSL, LHSH;
5283 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
5284 EVT VT = LHSL.getValueType();
5285
5286 // If the shift amount operand is coming from a vector legalization it may
5287 // have an illegal type. Fix that first by casting the operand, otherwise
5288 // the new SHL_PARTS operation would need further legalization.
5289 SDValue ShiftOp = N->getOperand(1);
5290 EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
5291 if (ShiftOp.getValueType() != ShiftTy)
5292 ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy);
5293
5294 SDValue Ops[] = { LHSL, LHSH, ShiftOp };
5295 Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops);
5296 Hi = Lo.getValue(1);
5297 return;
5298 }
5299
5300 // Otherwise, emit a libcall.
5301 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
5302 bool isSigned;
5303 if (Opc == ISD::SHL) {
5304 isSigned = false; /*sign irrelevant*/
5305 LC = RTLIB::getSHL(VT);
5306 } else if (Opc == ISD::SRL) {
5307 isSigned = false;
5308 LC = RTLIB::getSRL(VT);
5309 } else {
5310 assert(Opc == ISD::SRA && "Unknown shift!");
5311 isSigned = true;
5312 LC = RTLIB::getSRA(VT);
5313 }
5314
5315 if (RTLIB::LibcallImpl LibcallImpl = DAG.getLibcalls().getLibcallImpl(LC)) {
5316 EVT ShAmtTy =
5317 EVT::getIntegerVT(*DAG.getContext(), DAG.getLibInfo().getIntSize());
5318 SDValue ShAmt = DAG.getZExtOrTrunc(N->getOperand(1), dl, ShAmtTy);
5319 SDValue Ops[2] = {N->getOperand(0), ShAmt};
5320 TargetLowering::MakeLibCallOptions CallOptions;
5321 CallOptions.setIsSigned(isSigned);
5322 SplitInteger(
5323 TLI.makeLibCall(DAG, LibcallImpl, VT, Ops, CallOptions, dl).first, Lo,
5324 Hi);
5325 return;
5326 }
5327
5328 if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
5329 llvm_unreachable("Unsupported shift!");
5330}
5331
5332void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
5333 SDValue &Lo, SDValue &Hi) {
5334 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5335 SDLoc dl(N);
5336 SDValue Op = N->getOperand(0);
5337 if (Op.getValueType().bitsLE(NVT)) {
5338 // The low part is sign extension of the input (degenerates to a copy).
5339 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
5340 // The high part is obtained by SRA'ing all but one of the bits of low part.
5341 unsigned LoSize = NVT.getSizeInBits();
5342 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
5343 DAG.getShiftAmountConstant(LoSize - 1, NVT, dl));
5344 } else {
5345 // For example, extension of an i48 to an i64. The operand type necessarily
5346 // promotes to the result type, so will end up being expanded too.
5347 assert(getTypeAction(Op.getValueType()) ==
5349 "Only know how to promote this result!");
5350 SDValue Res = GetPromotedInteger(Op);
5351 assert(Res.getValueType() == N->getValueType(0) &&
5352 "Operand over promoted?");
5353 // Split the promoted operand. This will simplify when it is expanded.
5354 SplitInteger(Res, Lo, Hi);
5355 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
5356 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
5357 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
5358 ExcessBits)));
5359 }
5360}
5361
5362void DAGTypeLegalizer::
5363ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
5364 SDLoc dl(N);
5365 GetExpandedInteger(N->getOperand(0), Lo, Hi);
5366 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
5367
5368 if (EVT.bitsLE(Lo.getValueType())) {
5369 // sext_inreg the low part if needed.
5370 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
5371 N->getOperand(1));
5372
5373 // The high part gets the sign extension from the lo-part. This handles
5374 // things like sextinreg V:i64 from i8.
5375 Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
5376 DAG.getShiftAmountConstant(Hi.getValueSizeInBits() - 1,
5377 Hi.getValueType(), dl));
5378 } else {
5379 // For example, extension of an i48 to an i64. Leave the low part alone,
5380 // sext_inreg the high part.
5381 unsigned ExcessBits = EVT.getSizeInBits() - Lo.getValueSizeInBits();
5382 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
5383 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
5384 ExcessBits)));
5385 }
5386}
5387
5388void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
5389 SDValue &Lo, SDValue &Hi) {
5390 EVT VT = N->getValueType(0);
5391 SDLoc dl(N);
5392 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
5393
5394 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
5395 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
5396 SplitInteger(Res.getValue(1), Lo, Hi);
5397 return;
5398 }
5399
5400 RTLIB::Libcall LC = RTLIB::getSREM(VT);
5401 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
5402
5403 TargetLowering::MakeLibCallOptions CallOptions;
5404 CallOptions.setIsSigned(true);
5405 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
5406}
5407
5408void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
5409 SDValue &Lo, SDValue &Hi) {
5410 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5411 SDValue InOp = N->getOperand(0);
5412 EVT InVT = InOp.getValueType();
5413 SDLoc dl(N);
5414 Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, InOp);
5415 Hi = DAG.getNode(ISD::SRL, dl, InVT, InOp,
5416 DAG.getShiftAmountConstant(NVT.getSizeInBits(), InVT, dl));
5417 Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
5418}
5419
5420void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N,
5421 SDValue &Lo, SDValue &Hi) {
5422 EVT VT = N->getValueType(0);
5423 SDLoc dl(N);
5424
5425 if (N->getOpcode() == ISD::UMULO) {
5426 // This section expands the operation into the following sequence of
5427 // instructions. `iNh` here refers to a type which has half the bit width of
5428 // the type the original operation operated on.
5429 //
5430 // %0 = %LHS.HI != 0 && %RHS.HI != 0
5431 // %1 = { iNh, i1 } @umul.with.overflow.iNh(iNh %LHS.HI, iNh %RHS.LO)
5432 // %2 = { iNh, i1 } @umul.with.overflow.iNh(iNh %RHS.HI, iNh %LHS.LO)
5433 // %3 = mul nuw iN (%LHS.LOW as iN), (%RHS.LOW as iN)
5434 // %4 = add iNh %1.0, %2.0 as iN
5435 // %5 = { iNh, i1 } @uadd.with.overflow.iNh(iNh %4, iNh %3.HIGH)
5436 //
5437 // %lo = %3.LO
5438 // %hi = %5.0
5439 // %ovf = %0 || %1.1 || %2.1 || %5.1
5440 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
5441 SDValue LHSHigh, LHSLow, RHSHigh, RHSLow;
5442 GetExpandedInteger(LHS, LHSLow, LHSHigh);
5443 GetExpandedInteger(RHS, RHSLow, RHSHigh);
5444 EVT HalfVT = LHSLow.getValueType();
5445 EVT BitVT = N->getValueType(1);
5446 SDVTList VTHalfWithO = DAG.getVTList(HalfVT, BitVT);
5447
5448 SDValue HalfZero = DAG.getConstant(0, dl, HalfVT);
5449 SDValue Overflow = DAG.getNode(ISD::AND, dl, BitVT,
5450 DAG.getSetCC(dl, BitVT, LHSHigh, HalfZero, ISD::SETNE),
5451 DAG.getSetCC(dl, BitVT, RHSHigh, HalfZero, ISD::SETNE));
5452
5453 SDValue One = DAG.getNode(ISD::UMULO, dl, VTHalfWithO, LHSHigh, RHSLow);
5454 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, One.getValue(1));
5455
5456 SDValue Two = DAG.getNode(ISD::UMULO, dl, VTHalfWithO, RHSHigh, LHSLow);
5457 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Two.getValue(1));
5458
5459 SDValue HighSum = DAG.getNode(ISD::ADD, dl, HalfVT, One, Two);
5460
5461 // Cannot use `UMUL_LOHI` directly, because some 32-bit targets (ARM) do not
5462 // know how to expand `i64,i64 = umul_lohi a, b` and abort (why isn’t this
5463 // operation recursively legalized?).
5464 //
5465 // Many backends understand this pattern and will convert into LOHI
5466 // themselves, if applicable.
5467 SDValue Three = DAG.getNode(ISD::MUL, dl, VT,
5468 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LHSLow),
5469 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RHSLow));
5470 SplitInteger(Three, Lo, Hi);
5471
5472 Hi = DAG.getNode(ISD::UADDO, dl, VTHalfWithO, Hi, HighSum);
5473 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Hi.getValue(1));
5474 ReplaceValueWith(SDValue(N, 1), Overflow);
5475 return;
5476 }
5477
5478 Type *RetTy = VT.getTypeForEVT(*DAG.getContext());
5479 EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
5480 Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext());
5481
5482 // Replace this with a libcall that will check overflow.
5483 RTLIB::Libcall LC = RTLIB::getMULO(VT);
5484 RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(LC);
5485
5486 // If we don't have the libcall or if the function we are compiling is the
5487 // implementation of the expected libcall (avoid inf-loop), expand inline.
5488 if (LCImpl == RTLIB::Unsupported ||
5490 DAG.getMachineFunction().getName()) {
5491 // FIXME: This is not an optimal expansion, but better than crashing.
5492 SDValue MulLo, MulHi;
5493 TLI.forceExpandWideMUL(DAG, dl, /*Signed=*/true, N->getOperand(0),
5494 N->getOperand(1), MulLo, MulHi);
5495 SDValue SRA = DAG.getNode(
5496 ISD::SRA, dl, VT, MulLo,
5497 DAG.getShiftAmountConstant(VT.getScalarSizeInBits() - 1, VT, dl));
5498 SDValue Overflow =
5499 DAG.getSetCC(dl, N->getValueType(1), MulHi, SRA, ISD::SETNE);
5500 SplitInteger(MulLo, Lo, Hi);
5501 ReplaceValueWith(SDValue(N, 1), Overflow);
5502 return;
5503 }
5504
5505 SDValue Temp = DAG.CreateStackTemporary(PtrVT);
5506 // Temporary for the overflow value, default it to zero.
5507 SDValue Chain =
5508 DAG.getStore(DAG.getEntryNode(), dl, DAG.getConstant(0, dl, PtrVT), Temp,
5509 MachinePointerInfo());
5510
5512 for (const SDValue &Op : N->op_values()) {
5513 EVT ArgVT = Op.getValueType();
5514 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
5515 TargetLowering::ArgListEntry Entry(Op, ArgTy);
5516 Entry.IsSExt = true;
5517 Entry.IsZExt = false;
5518 Args.push_back(Entry);
5519 }
5520
5521 // Also pass the address of the overflow check.
5522 TargetLowering::ArgListEntry Entry(
5523 Temp, PointerType::getUnqual(PtrTy->getContext()));
5524 Entry.IsSExt = true;
5525 Entry.IsZExt = false;
5526 Args.push_back(Entry);
5527
5528 SDValue Func = DAG.getExternalSymbol(LCImpl, PtrVT);
5529
5530 TargetLowering::CallLoweringInfo CLI(DAG);
5531 CLI.setDebugLoc(dl)
5532 .setChain(Chain)
5533 .setLibCallee(DAG.getLibcalls().getLibcallImplCallingConv(LCImpl), RetTy,
5534 Func, std::move(Args))
5535 .setSExtResult();
5536
5537 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
5538
5539 SplitInteger(CallInfo.first, Lo, Hi);
5540 SDValue Temp2 =
5541 DAG.getLoad(PtrVT, dl, CallInfo.second, Temp, MachinePointerInfo());
5542 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2,
5543 DAG.getConstant(0, dl, PtrVT),
5544 ISD::SETNE);
5545 // Use the overflow from the libcall everywhere.
5546 ReplaceValueWith(SDValue(N, 1), Ofl);
5547}
5548
5549void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
5550 SDValue &Lo, SDValue &Hi) {
5551 EVT VT = N->getValueType(0);
5552 SDLoc dl(N);
5553 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
5554
5555 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
5556 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
5557 SplitInteger(Res.getValue(0), Lo, Hi);
5558 return;
5559 }
5560
5561 // Try to expand UDIV by constant.
5562 if (isa<ConstantSDNode>(N->getOperand(1))) {
5563 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5564 // Only if the new type is legal.
5565 if (isTypeLegal(NVT)) {
5566 SDValue InL, InH;
5567 GetExpandedInteger(N->getOperand(0), InL, InH);
5569 if (TLI.expandDIVREMByConstant(N, Result, NVT, DAG, InL, InH)) {
5570 Lo = Result[0];
5571 Hi = Result[1];
5572 return;
5573 }
5574 }
5575 }
5576
5577 RTLIB::Libcall LC = RTLIB::getUDIV(VT);
5578 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
5579
5580 TargetLowering::MakeLibCallOptions CallOptions;
5581 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
5582}
5583
5584void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
5585 SDValue &Lo, SDValue &Hi) {
5586 EVT VT = N->getValueType(0);
5587 SDLoc dl(N);
5588 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
5589
5590 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
5591 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
5592 SplitInteger(Res.getValue(1), Lo, Hi);
5593 return;
5594 }
5595
5596 // Try to expand UREM by constant.
5597 if (isa<ConstantSDNode>(N->getOperand(1))) {
5598 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5599 // Only if the new type is legal.
5600 if (isTypeLegal(NVT)) {
5601 SDValue InL, InH;
5602 GetExpandedInteger(N->getOperand(0), InL, InH);
5604 if (TLI.expandDIVREMByConstant(N, Result, NVT, DAG, InL, InH)) {
5605 Lo = Result[0];
5606 Hi = Result[1];
5607 return;
5608 }
5609 }
5610 }
5611
5612 RTLIB::Libcall LC = RTLIB::getUREM(VT);
5613 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
5614
5615 TargetLowering::MakeLibCallOptions CallOptions;
5616 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
5617}
5618
5619void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
5620 SDValue &Lo, SDValue &Hi) {
5621 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5622 SDLoc dl(N);
5623 SDValue Op = N->getOperand(0);
5624 if (Op.getValueType().bitsLE(NVT)) {
5625 // The low part is zero extension of the input (degenerates to a copy).
5626 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
5627 Hi = DAG.getConstant(0, dl, NVT); // The high part is just a zero.
5628 } else {
5629 // For example, extension of an i48 to an i64. The operand type necessarily
5630 // promotes to the result type, so will end up being expanded too.
5631 assert(getTypeAction(Op.getValueType()) ==
5633 "Only know how to promote this result!");
5634 SDValue Res = GetPromotedInteger(Op);
5635 assert(Res.getValueType() == N->getValueType(0) &&
5636 "Operand over promoted?");
5637 // Split the promoted operand. This will simplify when it is expanded.
5638 SplitInteger(Res, Lo, Hi);
5639 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
5640 Hi = DAG.getZeroExtendInReg(Hi, dl,
5641 EVT::getIntegerVT(*DAG.getContext(),
5642 ExcessBits));
5643 }
5644}
5645
5646void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N,
5647 SDValue &Lo, SDValue &Hi) {
5648 SDLoc dl(N);
5649 EVT VT = cast<AtomicSDNode>(N)->getMemoryVT();
5650 SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
5651 SDValue Zero = DAG.getConstant(0, dl, VT);
5652 SDValue Swap = DAG.getAtomicCmpSwap(
5654 cast<AtomicSDNode>(N)->getMemoryVT(), VTs, N->getOperand(0),
5655 N->getOperand(1), Zero, Zero, cast<AtomicSDNode>(N)->getMemOperand());
5656
5657 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
5658 ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
5659}
5660
5661void DAGTypeLegalizer::ExpandIntRes_VECREDUCE(SDNode *N,
5662 SDValue &Lo, SDValue &Hi) {
5663 // TODO For VECREDUCE_(AND|OR|XOR) we could split the vector and calculate
5664 // both halves independently.
5665 SDValue Res = TLI.expandVecReduce(N, DAG);
5666 SplitInteger(Res, Lo, Hi);
5667}
5668
5669void DAGTypeLegalizer::ExpandIntRes_Rotate(SDNode *N,
5670 SDValue &Lo, SDValue &Hi) {
5671 // Delegate to funnel-shift expansion.
5672 SDLoc DL(N);
5673 unsigned Opcode = N->getOpcode() == ISD::ROTL ? ISD::FSHL : ISD::FSHR;
5674 SDValue Res = DAG.getNode(Opcode, DL, N->getValueType(0), N->getOperand(0),
5675 N->getOperand(0), N->getOperand(1));
5676 SplitInteger(Res, Lo, Hi);
5677}
5678
5679void DAGTypeLegalizer::ExpandIntRes_FunnelShift(SDNode *N, SDValue &Lo,
5680 SDValue &Hi) {
5681 // Values numbered from least significant to most significant.
5682 SDValue In1, In2, In3, In4;
5683 GetExpandedInteger(N->getOperand(0), In3, In4);
5684 GetExpandedInteger(N->getOperand(1), In1, In2);
5685 EVT HalfVT = In1.getValueType();
5686
5687 SDLoc DL(N);
5688 unsigned Opc = N->getOpcode();
5689 SDValue ShAmt = N->getOperand(2);
5690 EVT ShAmtVT = ShAmt.getValueType();
5691 EVT ShAmtCCVT = getSetCCResultType(ShAmtVT);
5692
5693 // If the shift amount is at least half the bitwidth, swap the inputs.
5694 unsigned HalfVTBits = HalfVT.getScalarSizeInBits();
5695 SDValue AndNode = DAG.getNode(ISD::AND, DL, ShAmtVT, ShAmt,
5696 DAG.getConstant(HalfVTBits, DL, ShAmtVT));
5697 SDValue Cond =
5698 DAG.getSetCC(DL, ShAmtCCVT, AndNode, DAG.getConstant(0, DL, ShAmtVT),
5700
5701 // Expand to a pair of funnel shifts.
5702 EVT NewShAmtVT = TLI.getShiftAmountTy(HalfVT, DAG.getDataLayout());
5703 SDValue NewShAmt = DAG.getAnyExtOrTrunc(ShAmt, DL, NewShAmtVT);
5704
5705 SDValue Select1 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In1, In2);
5706 SDValue Select2 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In2, In3);
5707 SDValue Select3 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In3, In4);
5708 Lo = DAG.getNode(Opc, DL, HalfVT, Select2, Select1, NewShAmt);
5709 Hi = DAG.getNode(Opc, DL, HalfVT, Select3, Select2, NewShAmt);
5710}
5711
5712void DAGTypeLegalizer::ExpandIntRes_CLMUL(SDNode *N, SDValue &Lo, SDValue &Hi) {
5713 if (N->getOpcode() != ISD::CLMUL) {
5714 SDValue Res = TLI.expandCLMUL(N, DAG);
5715 return SplitInteger(Res, Lo, Hi);
5716 }
5717
5718 SDValue LL, LH, RL, RH;
5719 GetExpandedInteger(N->getOperand(0), LL, LH);
5720 GetExpandedInteger(N->getOperand(1), RL, RH);
5721 EVT HalfVT = LL.getValueType();
5722 SDLoc DL(N);
5723
5724 // The low bits are a direct CLMUL of the the low bits.
5725 Lo = DAG.getNode(ISD::CLMUL, DL, HalfVT, LL, RL);
5726
5727 // We compute two Hi-Lo cross-products, XOR them, and XOR it with the overflow
5728 // of the CLMUL of the low bits (given by CLMULH of the low bits) to yield the
5729 // final high bits.
5730 SDValue LoH = DAG.getNode(ISD::CLMULH, DL, HalfVT, LL, RL);
5731 SDValue HiLoCross1 = DAG.getNode(ISD::CLMUL, DL, HalfVT, LL, RH);
5732 SDValue HiLoCross2 = DAG.getNode(ISD::CLMUL, DL, HalfVT, LH, RL);
5733 SDValue HiLoCross = DAG.getNode(ISD::XOR, DL, HalfVT, HiLoCross1, HiLoCross2);
5734 Hi = DAG.getNode(ISD::XOR, DL, HalfVT, LoH, HiLoCross);
5735}
5736
5737void DAGTypeLegalizer::ExpandIntRes_PEXT(SDNode *N, SDValue &Lo, SDValue &Hi) {
5738 SDValue Res = TLI.expandPEXT(N, DAG);
5739 SplitInteger(Res, Lo, Hi);
5740}
5741
5742void DAGTypeLegalizer::ExpandIntRes_PDEP(SDNode *N, SDValue &Lo, SDValue &Hi) {
5743 SDValue Res = TLI.expandPDEP(N, DAG);
5744 SplitInteger(Res, Lo, Hi);
5745}
5746
5747void DAGTypeLegalizer::ExpandIntRes_VSCALE(SDNode *N, SDValue &Lo,
5748 SDValue &Hi) {
5749 EVT VT = N->getValueType(0);
5750 EVT HalfVT =
5751 EVT::getIntegerVT(*DAG.getContext(), N->getValueSizeInBits(0) / 2);
5752 SDLoc dl(N);
5753
5754 // We assume VSCALE(1) fits into a legal integer.
5755 APInt One(HalfVT.getSizeInBits(), 1);
5756 SDValue VScaleBase = DAG.getVScale(dl, HalfVT, One);
5757 VScaleBase = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, VScaleBase);
5758 SDValue Res = DAG.getNode(ISD::MUL, dl, VT, VScaleBase, N->getOperand(0));
5759 SplitInteger(Res, Lo, Hi);
5760}
5761
5762void DAGTypeLegalizer::ExpandIntRes_READ_REGISTER(SDNode *N, SDValue &Lo,
5763 SDValue &Hi) {
5764 const Function &Fn = DAG.getMachineFunction().getFunction();
5765 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
5766 "cannot use llvm.read_register with illegal type", Fn, N->getDebugLoc()));
5767 ReplaceValueWith(SDValue(N, 1), N->getOperand(0));
5768 EVT LoVT, HiVT;
5769 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
5770 Lo = DAG.getPOISON(LoVT);
5771 Hi = DAG.getPOISON(HiVT);
5772}
5773
5774void DAGTypeLegalizer::ExpandIntRes_CTTZ_ELTS(SDNode *N, SDValue &Lo,
5775 SDValue &Hi) {
5776 // Assume that the maximum number of vector elements fits in getVectorIdxTy
5777 // and expand to that.
5778 EVT VT = N->getSimpleValueType(0);
5779 EVT IdxVT = TLI.getVectorIdxTy(DAG.getDataLayout());
5780 assert(IdxVT.bitsLT(VT) &&
5781 "VectorIdxTy should be smaller than type to be expanded?");
5782
5783 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N), IdxVT, N->getOperand(0));
5784 Res = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Res);
5785 SplitInteger(Res, Lo, Hi);
5786}
5787
5788//===----------------------------------------------------------------------===//
5789// Integer Operand Expansion
5790//===----------------------------------------------------------------------===//
5791
5792/// ExpandIntegerOperand - This method is called when the specified operand of
5793/// the specified node is found to need expansion. At this point, all of the
5794/// result types of the node are known to be legal, but other operands of the
5795/// node may need promotion or expansion as well as the specified one.
5796bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
5797 LLVM_DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG));
5798 SDValue Res = SDValue();
5799
5800 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
5801 return false;
5802
5803 switch (N->getOpcode()) {
5804 default:
5805 #ifndef NDEBUG
5806 dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
5807 N->dump(&DAG); dbgs() << "\n";
5808 #endif
5809 report_fatal_error("Do not know how to expand this operator's operand!");
5810
5811 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
5812 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
5813 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
5814 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
5815 case ISD::FAKE_USE:
5816 Res = ExpandOp_FAKE_USE(N);
5817 break;
5820 Res = TLI.expandLoopDependenceMask(N, DAG);
5821 break;
5822 case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
5823 case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
5824 case ISD::SPLAT_VECTOR: Res = ExpandIntOp_SPLAT_VECTOR(N); break;
5825 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
5826 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
5827 case ISD::SETCCCARRY: Res = ExpandIntOp_SETCCCARRY(N); break;
5829 case ISD::SINT_TO_FP:
5831 case ISD::UINT_TO_FP: Res = ExpandIntOp_XINT_TO_FP(N); break;
5832 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
5833 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
5834
5835 case ISD::SHL:
5836 case ISD::SRA:
5837 case ISD::SRL:
5838 case ISD::ROTL:
5839 case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
5840 case ISD::RETURNADDR:
5841 case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break;
5842
5843 case ISD::SCMP:
5844 case ISD::UCMP: Res = ExpandIntOp_CMP(N); break;
5845
5846 case ISD::ATOMIC_STORE: Res = ExpandIntOp_ATOMIC_STORE(N); break;
5847 case ISD::STACKMAP:
5848 Res = ExpandIntOp_STACKMAP(N, OpNo);
5849 break;
5850 case ISD::PATCHPOINT:
5851 Res = ExpandIntOp_PATCHPOINT(N, OpNo);
5852 break;
5853 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
5854 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
5855 Res = ExpandIntOp_VP_STRIDED(N, OpNo);
5856 break;
5858 Res = ExpandIntOp_WRITE_REGISTER(N, OpNo);
5859 break;
5860 }
5861
5862 // If the result is null, the sub-method took care of registering results etc.
5863 if (!Res.getNode()) return false;
5864
5865 // If the result is N, the sub-method updated N in place. Tell the legalizer
5866 // core about this.
5867 if (Res.getNode() == N)
5868 return true;
5869
5870 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
5871 "Invalid operand expansion");
5872
5873 ReplaceValueWith(SDValue(N, 0), Res);
5874 return false;
5875}
5876
5877/// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
5878/// is shared among BR_CC, SELECT_CC, and SETCC handlers.
5879void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
5880 SDValue &NewRHS,
5881 ISD::CondCode &CCCode,
5882 const SDLoc &dl) {
5883 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
5884 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
5885 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
5886
5887 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
5888 if (RHSLo == RHSHi && isAllOnesConstant(RHSLo)) {
5889 // Equality comparison to -1.
5890 NewLHS = DAG.getNode(ISD::AND, dl, LHSLo.getValueType(), LHSLo, LHSHi);
5891 NewRHS = RHSLo;
5892 return;
5893 }
5894
5895 NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
5896 NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
5897 NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
5898 NewRHS = DAG.getConstant(0, dl, NewLHS.getValueType());
5899 return;
5900 }
5901
5902 // If this is a comparison of the sign bit, just look at the top part.
5903 // X > -1, x < 0
5904 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
5905 if ((CCCode == ISD::SETLT && CST->isZero()) || // X < 0
5906 (CCCode == ISD::SETGT && CST->isAllOnes())) { // X > -1
5907 NewLHS = LHSHi;
5908 NewRHS = RHSHi;
5909 return;
5910 }
5911
5912 // FIXME: This generated code sucks.
5913 ISD::CondCode LowCC;
5914 switch (CCCode) {
5915 default: llvm_unreachable("Unknown integer setcc!");
5916 case ISD::SETLT:
5917 case ISD::SETULT: LowCC = ISD::SETULT; break;
5918 case ISD::SETGT:
5919 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5920 case ISD::SETLE:
5921 case ISD::SETULE: LowCC = ISD::SETULE; break;
5922 case ISD::SETGE:
5923 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5924 }
5925
5926 // LoCmp = lo(op1) < lo(op2) // Always unsigned comparison
5927 // HiCmp = hi(op1) < hi(op2) // Signedness depends on operands
5928 // dest = hi(op1) == hi(op2) ? LoCmp : HiCmp;
5929
5930 // NOTE: on targets without efficient SELECT of bools, we can always use
5931 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5932 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true,
5933 nullptr);
5934 SDValue LoCmp, HiCmp;
5935 if (TLI.isTypeLegal(LHSLo.getValueType()))
5936 LoCmp = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()), LHSLo,
5937 RHSLo, LowCC, false, DagCombineInfo, dl);
5938 if (!LoCmp.getNode())
5939 LoCmp = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()), LHSLo,
5940 RHSLo, LowCC);
5941 if (TLI.isTypeLegal(LHSHi.getValueType()))
5942 HiCmp = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()), LHSHi,
5943 RHSHi, CCCode, false, DagCombineInfo, dl);
5944 if (!HiCmp.getNode())
5945 HiCmp =
5946 DAG.getNode(ISD::SETCC, dl, getSetCCResultType(LHSHi.getValueType()),
5947 LHSHi, RHSHi, DAG.getCondCode(CCCode));
5948
5949 ConstantSDNode *LoCmpC = dyn_cast<ConstantSDNode>(LoCmp.getNode());
5950 ConstantSDNode *HiCmpC = dyn_cast<ConstantSDNode>(HiCmp.getNode());
5951
5952 bool EqAllowed = ISD::isTrueWhenEqual(CCCode);
5953
5954 // FIXME: Is the HiCmpC->isOne() here correct for
5955 // ZeroOrNegativeOneBooleanContent.
5956 if ((EqAllowed && (HiCmpC && HiCmpC->isZero())) ||
5957 (!EqAllowed &&
5958 ((HiCmpC && HiCmpC->isOne()) || (LoCmpC && LoCmpC->isZero())))) {
5959 // For LE / GE, if high part is known false, ignore the low part.
5960 // For LT / GT: if low part is known false, return the high part.
5961 // if high part is known true, ignore the low part.
5962 NewLHS = HiCmp;
5963 NewRHS = SDValue();
5964 return;
5965 }
5966
5967 if (LHSHi == RHSHi) {
5968 // Comparing the low bits is enough.
5969 NewLHS = LoCmp;
5970 NewRHS = SDValue();
5971 return;
5972 }
5973
5974 // Lower with SETCCCARRY if the target supports it.
5975 EVT HiVT = LHSHi.getValueType();
5976 EVT ExpandVT = TLI.getTypeToExpandTo(*DAG.getContext(), HiVT);
5977 bool HasSETCCCARRY = TLI.isOperationLegalOrCustom(ISD::SETCCCARRY, ExpandVT);
5978
5979 // FIXME: Make all targets support this, then remove the other lowering.
5980 if (HasSETCCCARRY) {
5981 // SETCCCARRY can detect < and >= directly. For > and <=, flip
5982 // operands and condition code.
5983 bool FlipOperands = false;
5984 switch (CCCode) {
5985 case ISD::SETGT: CCCode = ISD::SETLT; FlipOperands = true; break;
5986 case ISD::SETUGT: CCCode = ISD::SETULT; FlipOperands = true; break;
5987 case ISD::SETLE: CCCode = ISD::SETGE; FlipOperands = true; break;
5988 case ISD::SETULE: CCCode = ISD::SETUGE; FlipOperands = true; break;
5989 default: break;
5990 }
5991 if (FlipOperands) {
5992 std::swap(LHSLo, RHSLo);
5993 std::swap(LHSHi, RHSHi);
5994 }
5995 // Perform a wide subtraction, feeding the carry from the low part into
5996 // SETCCCARRY. The SETCCCARRY operation is essentially looking at the high
5997 // part of the result of LHS - RHS. It is negative iff LHS < RHS. It is
5998 // zero or positive iff LHS >= RHS.
5999 EVT LoVT = LHSLo.getValueType();
6000 SDVTList VTList = DAG.getVTList(LoVT, getSetCCResultType(LoVT));
6001 SDValue LowCmp = DAG.getNode(ISD::USUBO, dl, VTList, LHSLo, RHSLo);
6002 SDValue Res = DAG.getNode(ISD::SETCCCARRY, dl, getSetCCResultType(HiVT),
6003 LHSHi, RHSHi, LowCmp.getValue(1),
6004 DAG.getCondCode(CCCode));
6005 NewLHS = Res;
6006 NewRHS = SDValue();
6007 return;
6008 }
6009
6010 NewLHS = TLI.SimplifySetCC(getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ,
6011 false, DagCombineInfo, dl);
6012 if (!NewLHS.getNode())
6013 NewLHS =
6014 DAG.getSetCC(dl, getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ);
6015 NewLHS = DAG.getSelect(dl, LoCmp.getValueType(), NewLHS, LoCmp, HiCmp);
6016 NewRHS = SDValue();
6017}
6018
6019SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
6020 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
6021 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
6022 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
6023
6024 // If ExpandSetCCOperands returned a scalar, we need to compare the result
6025 // against zero to select between true and false values.
6026 if (!NewRHS.getNode()) {
6027 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
6028 CCCode = ISD::SETNE;
6029 }
6030
6031 // Update N to have the operands specified.
6032 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
6033 DAG.getCondCode(CCCode), NewLHS, NewRHS,
6034 N->getOperand(4)), 0);
6035}
6036
6037SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
6038 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
6039 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
6040 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
6041
6042 // If ExpandSetCCOperands returned a scalar, we need to compare the result
6043 // against zero to select between true and false values.
6044 if (!NewRHS.getNode()) {
6045 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
6046 CCCode = ISD::SETNE;
6047 }
6048
6049 // Update N to have the operands specified.
6050 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
6051 N->getOperand(2), N->getOperand(3),
6052 DAG.getCondCode(CCCode)), 0);
6053}
6054
6055SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
6056 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
6057 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
6058 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
6059
6060 // If ExpandSetCCOperands returned a scalar, use it.
6061 if (!NewRHS.getNode()) {
6062 assert(NewLHS.getValueType() == N->getValueType(0) &&
6063 "Unexpected setcc expansion!");
6064 return NewLHS;
6065 }
6066
6067 // Otherwise, update N to have the operands specified.
6068 return SDValue(
6069 DAG.UpdateNodeOperands(N, NewLHS, NewRHS, DAG.getCondCode(CCCode)), 0);
6070}
6071
6072SDValue DAGTypeLegalizer::ExpandIntOp_SETCCCARRY(SDNode *N) {
6073 SDValue LHS = N->getOperand(0);
6074 SDValue RHS = N->getOperand(1);
6075 SDValue Carry = N->getOperand(2);
6076 SDValue Cond = N->getOperand(3);
6077 SDLoc dl = SDLoc(N);
6078
6079 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
6080 GetExpandedInteger(LHS, LHSLo, LHSHi);
6081 GetExpandedInteger(RHS, RHSLo, RHSHi);
6082
6083 // Expand to a USUBO_CARRY for the low part and a SETCCCARRY for the high.
6084 SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), Carry.getValueType());
6085 SDValue LowCmp =
6086 DAG.getNode(ISD::USUBO_CARRY, dl, VTList, LHSLo, RHSLo, Carry);
6087 return DAG.getNode(ISD::SETCCCARRY, dl, N->getValueType(0), LHSHi, RHSHi,
6088 LowCmp.getValue(1), Cond);
6089}
6090
6091SDValue DAGTypeLegalizer::ExpandIntOp_SPLAT_VECTOR(SDNode *N) {
6092 // Split the operand and replace with SPLAT_VECTOR_PARTS.
6093 SDValue Lo, Hi;
6094 GetExpandedInteger(N->getOperand(0), Lo, Hi);
6095 return DAG.getNode(ISD::SPLAT_VECTOR_PARTS, SDLoc(N), N->getValueType(0), Lo,
6096 Hi);
6097}
6098
6099SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
6100 // The value being shifted is legal, but the shift amount is too big.
6101 // It follows that either the result of the shift is undefined, or the
6102 // upper half of the shift amount is zero. Just use the lower half.
6103 SDValue Lo, Hi;
6104 GetExpandedInteger(N->getOperand(1), Lo, Hi);
6105 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
6106}
6107
6108SDValue DAGTypeLegalizer::ExpandIntOp_CMP(SDNode *N) {
6109 return TLI.expandCMP(N, DAG);
6110}
6111
6112SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
6113 // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
6114 // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
6115 // constant to valid type.
6116 SDValue Lo, Hi;
6117 GetExpandedInteger(N->getOperand(0), Lo, Hi);
6118 return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
6119}
6120
6121SDValue DAGTypeLegalizer::ExpandIntOp_XINT_TO_FP(SDNode *N) {
6122 bool IsStrict = N->isStrictFPOpcode();
6123 bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP ||
6124 N->getOpcode() == ISD::STRICT_SINT_TO_FP;
6125 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
6126 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
6127 EVT DstVT = N->getValueType(0);
6128 RTLIB::Libcall LC = IsSigned ? RTLIB::getSINTTOFP(Op.getValueType(), DstVT)
6129 : RTLIB::getUINTTOFP(Op.getValueType(), DstVT);
6130 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
6131 "Don't know how to expand this XINT_TO_FP!");
6132 TargetLowering::MakeLibCallOptions CallOptions;
6133 CallOptions.setIsSigned(true);
6134 std::pair<SDValue, SDValue> Tmp =
6135 TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, SDLoc(N), Chain);
6136
6137 if (!IsStrict)
6138 return Tmp.first;
6139
6140 ReplaceValueWith(SDValue(N, 1), Tmp.second);
6141 ReplaceValueWith(SDValue(N, 0), Tmp.first);
6142 return SDValue();
6143}
6144
6145SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
6146 assert(!N->isAtomic() && "Should have been a ATOMIC_STORE?");
6147
6148 if (ISD::isNormalStore(N))
6149 return ExpandOp_NormalStore(N, OpNo);
6150
6151 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
6152 assert(OpNo == 1 && "Can only expand the stored value so far");
6153
6154 EVT VT = N->getOperand(1).getValueType();
6155 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6156 SDValue Ch = N->getChain();
6157 SDValue Ptr = N->getBasePtr();
6158 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
6159 AAMDNodes AAInfo = N->getAAInfo();
6160 SDLoc dl(N);
6161 SDValue Lo, Hi;
6162
6163 assert(NVT.isByteSized() && "Expanded type not byte sized!");
6164
6165 if (N->getMemoryVT().bitsLE(NVT)) {
6166 GetExpandedInteger(N->getValue(), Lo, Hi);
6167 return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
6168 N->getMemoryVT(), N->getBaseAlign(), MMOFlags,
6169 AAInfo);
6170 }
6171
6172 if (DAG.getDataLayout().isLittleEndian()) {
6173 // Little-endian - low bits are at low addresses.
6174 GetExpandedInteger(N->getValue(), Lo, Hi);
6175
6176 Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(), N->getBaseAlign(),
6177 MMOFlags, AAInfo);
6178
6179 unsigned ExcessBits =
6180 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
6181 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
6182
6183 // Increment the pointer to the other half.
6184 unsigned IncrementSize = NVT.getSizeInBits()/8;
6185 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::getFixed(IncrementSize));
6186 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr,
6187 N->getPointerInfo().getWithOffset(IncrementSize),
6188 NEVT, N->getBaseAlign(), MMOFlags, AAInfo);
6189 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
6190 }
6191
6192 // Big-endian - high bits are at low addresses. Favor aligned stores at
6193 // the cost of some bit-fiddling.
6194 GetExpandedInteger(N->getValue(), Lo, Hi);
6195
6196 EVT ExtVT = N->getMemoryVT();
6197 unsigned EBytes = ExtVT.getStoreSize();
6198 unsigned IncrementSize = NVT.getSizeInBits()/8;
6199 unsigned ExcessBits = (EBytes - IncrementSize)*8;
6200 EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
6201 ExtVT.getSizeInBits() - ExcessBits);
6202
6203 if (ExcessBits < NVT.getSizeInBits()) {
6204 // Transfer high bits from the top of Lo to the bottom of Hi.
6205 Hi = DAG.getNode(
6206 ISD::SHL, dl, NVT, Hi,
6207 DAG.getShiftAmountConstant(NVT.getSizeInBits() - ExcessBits, NVT, dl));
6208 Hi = DAG.getNode(
6209 ISD::OR, dl, NVT, Hi,
6210 DAG.getNode(ISD::SRL, dl, NVT, Lo,
6211 DAG.getShiftAmountConstant(ExcessBits, NVT, dl)));
6212 }
6213
6214 // Store both the high bits and maybe some of the low bits.
6215 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(), HiVT,
6216 N->getBaseAlign(), MMOFlags, AAInfo);
6217
6218 // Increment the pointer to the other half.
6219 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::getFixed(IncrementSize));
6220 // Store the lowest ExcessBits bits in the second half.
6221 Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
6222 N->getPointerInfo().getWithOffset(IncrementSize),
6223 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
6224 N->getBaseAlign(), MMOFlags, AAInfo);
6225 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
6226}
6227
6228SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
6229 SDValue InL, InH;
6230 GetExpandedInteger(N->getOperand(0), InL, InH);
6231 // Just truncate the low part of the source.
6232 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL);
6233}
6234
6235SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) {
6236 SDLoc dl(N);
6237 SDValue Swap =
6238 DAG.getAtomic(ISD::ATOMIC_SWAP, dl, cast<AtomicSDNode>(N)->getMemoryVT(),
6239 N->getOperand(0), N->getOperand(2), N->getOperand(1),
6240 cast<AtomicSDNode>(N)->getMemOperand());
6241 return Swap.getValue(1);
6242}
6243
6244SDValue DAGTypeLegalizer::ExpandIntOp_VP_STRIDED(SDNode *N, unsigned OpNo) {
6245 assert((N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD && OpNo == 3) ||
6246 (N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE && OpNo == 4));
6247
6248 SDValue Hi; // The upper half is dropped out.
6249 SmallVector<SDValue, 8> NewOps(N->ops());
6250 GetExpandedInteger(NewOps[OpNo], NewOps[OpNo], Hi);
6251
6252 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
6253}
6254
6255SDValue DAGTypeLegalizer::ExpandIntOp_WRITE_REGISTER(SDNode *N, unsigned OpNo) {
6256 const Function &Fn = DAG.getMachineFunction().getFunction();
6257 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
6258 "cannot use llvm.write_register with illegal type", Fn,
6259 N->getDebugLoc()));
6260
6261 return N->getOperand(0);
6262}
6263
6264SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SPLICE(SDNode *N) {
6265 SDLoc dl(N);
6266
6267 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6268 SDValue V1 = GetPromotedInteger(N->getOperand(1));
6269 EVT OutVT = V0.getValueType();
6270
6271 return DAG.getNode(N->getOpcode(), dl, OutVT, V0, V1, N->getOperand(2));
6272}
6273
6274SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_INTERLEAVE_DEINTERLEAVE(SDNode *N) {
6275 SDLoc DL(N);
6276 unsigned Factor = N->getNumOperands();
6277
6279 for (unsigned i = 0; i != Factor; i++)
6280 Ops[i] = GetPromotedInteger(N->getOperand(i));
6281
6282 SmallVector<EVT, 8> ResVTs(Factor, Ops[0].getValueType());
6283 SDValue Res = DAG.getNode(N->getOpcode(), DL, DAG.getVTList(ResVTs), Ops);
6284
6285 for (unsigned i = 0; i != Factor; i++)
6286 SetPromotedInteger(SDValue(N, i), Res.getValue(i));
6287
6288 return SDValue();
6289}
6290
6291SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
6292
6293 EVT OutVT = N->getValueType(0);
6294 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6295 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6296 EVT NOutVTElem = NOutVT.getVectorElementType();
6297
6298 SDLoc dl(N);
6299 SDValue BaseIdx = N->getOperand(1);
6300
6301 // TODO: We may be able to use this for types other than scalable
6302 // vectors and fix those tests that expect BUILD_VECTOR to be used
6303 if (OutVT.isScalableVector()) {
6304 SDValue InOp0 = N->getOperand(0);
6305 EVT InVT = InOp0.getValueType();
6306
6307 // Try and extract from a smaller type so that it eventually falls
6308 // into the promotion code below.
6309 if (getTypeAction(InVT) == TargetLowering::TypeSplitVector ||
6310 getTypeAction(InVT) == TargetLowering::TypeLegal) {
6311 EVT NInVT = InVT.getHalfNumVectorElementsVT(*DAG.getContext());
6312 unsigned NElts = NInVT.getVectorMinNumElements();
6313 uint64_t IdxVal = BaseIdx->getAsZExtVal();
6314
6315 SDValue Step1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NInVT, InOp0,
6316 DAG.getConstant(alignDown(IdxVal, NElts), dl,
6317 BaseIdx.getValueType()));
6318 SDValue Step2 = DAG.getNode(
6319 ISD::EXTRACT_SUBVECTOR, dl, OutVT, Step1,
6320 DAG.getConstant(IdxVal % NElts, dl, BaseIdx.getValueType()));
6321 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Step2);
6322 }
6323
6324 // Try and extract from a widened type.
6325 if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
6326 SDValue Ops[] = {GetWidenedVector(InOp0), BaseIdx};
6327 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), OutVT, Ops);
6328 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Ext);
6329 }
6330
6331 // Promote operands and see if this is handled by target lowering,
6332 // Otherwise, use the BUILD_VECTOR approach below
6333 if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
6334 // Collect the (promoted) operands
6335 SDValue Ops[] = { GetPromotedInteger(InOp0), BaseIdx };
6336
6337 EVT PromEltVT = Ops[0].getValueType().getVectorElementType();
6338 assert(PromEltVT.bitsLE(NOutVTElem) &&
6339 "Promoted operand has an element type greater than result");
6340
6341 EVT ExtVT = NOutVT.changeVectorElementType(*DAG.getContext(), PromEltVT);
6342 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), ExtVT, Ops);
6343 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Ext);
6344 }
6345 }
6346
6347 if (OutVT.isScalableVector())
6348 report_fatal_error("Unable to promote scalable types using BUILD_VECTOR");
6349
6350 SDValue InOp0 = N->getOperand(0);
6351 if (getTypeAction(InOp0.getValueType()) == TargetLowering::TypePromoteInteger)
6352 InOp0 = GetPromotedInteger(InOp0);
6353
6354 EVT InVT = InOp0.getValueType();
6355 EVT InSVT = InVT.getVectorElementType();
6356
6357 unsigned OutNumElems = OutVT.getVectorNumElements();
6359 Ops.reserve(OutNumElems);
6360 for (unsigned i = 0; i != OutNumElems; ++i) {
6361 // Extract the element from the original vector.
6362 SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(), BaseIdx,
6363 DAG.getConstant(i, dl, BaseIdx.getValueType()));
6364 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, InSVT,
6365 N->getOperand(0), Index);
6366 SDValue Op = DAG.getAnyExtOrTrunc(Ext, dl, NOutVTElem);
6367 // Insert the converted element to the new vector.
6368 Ops.push_back(Op);
6369 }
6370
6371 return DAG.getBuildVector(NOutVT, dl, Ops);
6372}
6373
6374SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_SUBVECTOR(SDNode *N) {
6375 EVT OutVT = N->getValueType(0);
6376 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6377 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6378
6379 SDLoc dl(N);
6380 SDValue Vec = N->getOperand(0);
6381 SDValue SubVec = N->getOperand(1);
6382 SDValue Idx = N->getOperand(2);
6383
6384 EVT SubVecVT = SubVec.getValueType();
6385 EVT NSubVT =
6386 EVT::getVectorVT(*DAG.getContext(), NOutVT.getVectorElementType(),
6387 SubVecVT.getVectorElementCount());
6388
6389 Vec = GetPromotedInteger(Vec);
6390 SubVec = DAG.getNode(ISD::ANY_EXTEND, dl, NSubVT, SubVec);
6391
6392 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, NOutVT, Vec, SubVec, Idx);
6393}
6394
6395SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_REVERSE(SDNode *N) {
6396 SDLoc dl(N);
6397
6398 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6399 EVT OutVT = V0.getValueType();
6400
6401 return DAG.getNode(ISD::VECTOR_REVERSE, dl, OutVT, V0);
6402}
6403
6404SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
6405 ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
6406 EVT VT = N->getValueType(0);
6407 SDLoc dl(N);
6408
6409 ArrayRef<int> NewMask = SV->getMask().slice(0, VT.getVectorNumElements());
6410
6411 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6412 SDValue V1 = GetPromotedInteger(N->getOperand(1));
6413 EVT OutVT = V0.getValueType();
6414
6415 return DAG.getVectorShuffle(OutVT, dl, V0, V1, NewMask);
6416}
6417
6418SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
6419 EVT OutVT = N->getValueType(0);
6420 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6421 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6422 unsigned NumElems = N->getNumOperands();
6423 EVT NOutVTElem = NOutVT.getVectorElementType();
6424 TargetLoweringBase::BooleanContent NOutBoolType = TLI.getBooleanContents(NOutVT);
6425 unsigned NOutExtOpc = TargetLowering::getExtendForContent(NOutBoolType);
6426 SDLoc dl(N);
6427
6429 Ops.reserve(NumElems);
6430 for (unsigned i = 0; i != NumElems; ++i) {
6431 SDValue Op = N->getOperand(i);
6432 EVT OpVT = Op.getValueType();
6433 // BUILD_VECTOR integer operand types are allowed to be larger than the
6434 // result's element type. This may still be true after the promotion. For
6435 // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
6436 // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
6437 if (OpVT.bitsLT(NOutVTElem)) {
6438 unsigned ExtOpc = ISD::ANY_EXTEND;
6439 // Attempt to extend constant bool vectors to match target's BooleanContent.
6440 // While not necessary, this improves chances of the constant correctly
6441 // folding with compare results (e.g. for NOT patterns).
6442 if (OpVT == MVT::i1 && Op.getOpcode() == ISD::Constant)
6443 ExtOpc = NOutExtOpc;
6444 Op = DAG.getNode(ExtOpc, dl, NOutVTElem, Op);
6445 }
6446 Ops.push_back(Op);
6447 }
6448
6449 return DAG.getBuildVector(NOutVT, dl, Ops);
6450}
6451
6452SDValue DAGTypeLegalizer::PromoteIntRes_ScalarOp(SDNode *N) {
6453
6454 SDLoc dl(N);
6455
6456 assert(!N->getOperand(0).getValueType().isVector() &&
6457 "Input must be a scalar");
6458
6459 EVT OutVT = N->getValueType(0);
6460 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6461 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6462 EVT NOutElemVT = NOutVT.getVectorElementType();
6463
6464 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutElemVT, N->getOperand(0));
6465 return DAG.getNode(N->getOpcode(), dl, NOutVT, Op);
6466}
6467
6468SDValue DAGTypeLegalizer::PromoteIntRes_STEP_VECTOR(SDNode *N) {
6469 SDLoc dl(N);
6470 EVT OutVT = N->getValueType(0);
6471 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6472 assert(NOutVT.isScalableVector() &&
6473 "Type must be promoted to a scalable vector type");
6474 const APInt &StepVal = N->getConstantOperandAPInt(0);
6475 return DAG.getStepVector(dl, NOutVT,
6476 StepVal.sext(NOutVT.getScalarSizeInBits()));
6477}
6478
6479SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) {
6480 SDLoc dl(N);
6481
6482 EVT OutVT = N->getValueType(0);
6483 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6484 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6485
6486 unsigned NumOperands = N->getNumOperands();
6487 unsigned NumOutElem = NOutVT.getVectorMinNumElements();
6488 EVT OutElemTy = NOutVT.getVectorElementType();
6489 if (OutVT.isScalableVector()) {
6490 // Find the largest promoted element type for each of the operands.
6491 SDUse *MaxSizedValue = std::max_element(
6492 N->op_begin(), N->op_end(), [](const SDValue &A, const SDValue &B) {
6493 EVT AVT = A.getValueType().getVectorElementType();
6494 EVT BVT = B.getValueType().getVectorElementType();
6495 return AVT.getScalarSizeInBits() < BVT.getScalarSizeInBits();
6496 });
6497 EVT MaxElementVT = MaxSizedValue->getValueType().getVectorElementType();
6498
6499 // Then promote all vectors to the largest element type.
6501 for (unsigned I = 0; I < NumOperands; ++I) {
6502 SDValue Op = N->getOperand(I);
6503 EVT OpVT = Op.getValueType();
6504 if (getTypeAction(OpVT) == TargetLowering::TypePromoteInteger)
6505 Op = GetPromotedInteger(Op);
6506 else
6507 assert(getTypeAction(OpVT) == TargetLowering::TypeLegal &&
6508 "Unhandled legalization type");
6509
6511 MaxElementVT.getScalarSizeInBits())
6512 Op = DAG.getAnyExtOrTrunc(
6513 Op, dl,
6514 OpVT.changeVectorElementType(*DAG.getContext(), MaxElementVT));
6515 Ops.push_back(Op);
6516 }
6517
6518 // Do the CONCAT on the promoted type and finally truncate to (the promoted)
6519 // NOutVT.
6520 return DAG.getAnyExtOrTrunc(
6521 DAG.getNode(
6523 OutVT.changeVectorElementType(*DAG.getContext(), MaxElementVT),
6524 Ops),
6525 dl, NOutVT);
6526 }
6527
6528 unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements();
6529 assert(NumElem * NumOperands == NumOutElem &&
6530 "Unexpected number of elements");
6531
6532 // Take the elements from the first vector.
6533 SmallVector<SDValue, 8> Ops(NumOutElem);
6534 for (unsigned i = 0; i < NumOperands; ++i) {
6535 SDValue Op = N->getOperand(i);
6536 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger)
6537 Op = GetPromotedInteger(Op);
6538 EVT SclrTy = Op.getValueType().getVectorElementType();
6539 assert(NumElem == Op.getValueType().getVectorNumElements() &&
6540 "Unexpected number of elements");
6541
6542 for (unsigned j = 0; j < NumElem; ++j) {
6543 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Op,
6544 DAG.getVectorIdxConstant(j, dl));
6545 Ops[i * NumElem + j] = DAG.getAnyExtOrTrunc(Ext, dl, OutElemTy);
6546 }
6547 }
6548
6549 return DAG.getBuildVector(NOutVT, dl, Ops);
6550}
6551
6552SDValue DAGTypeLegalizer::PromoteIntRes_EXTEND_VECTOR_INREG(SDNode *N) {
6553 EVT VT = N->getValueType(0);
6554 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6555 assert(NVT.isVector() && "This type must be promoted to a vector type");
6556
6557 SDLoc dl(N);
6558
6559 // For operands whose TypeAction is to promote, extend the promoted node
6560 // appropriately (ZERO_EXTEND or SIGN_EXTEND) from the original pre-promotion
6561 // type, and then construct a new *_EXTEND_VECTOR_INREG node to the promote-to
6562 // type..
6563 if (getTypeAction(N->getOperand(0).getValueType())
6565 SDValue Promoted;
6566
6567 switch(N->getOpcode()) {
6569 Promoted = SExtPromotedInteger(N->getOperand(0));
6570 break;
6572 Promoted = ZExtPromotedInteger(N->getOperand(0));
6573 break;
6575 Promoted = GetPromotedInteger(N->getOperand(0));
6576 break;
6577 default:
6578 llvm_unreachable("Node has unexpected Opcode");
6579 }
6580 unsigned NewSize = NVT.getSizeInBits();
6581 if (Promoted.getValueType().getSizeInBits() > NewSize) {
6582 EVT ExtractVT = EVT::getVectorVT(
6583 *DAG.getContext(), Promoted.getValueType().getVectorElementType(),
6584 NewSize / Promoted.getScalarValueSizeInBits());
6585
6586 Promoted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ExtractVT, Promoted,
6587 DAG.getVectorIdxConstant(0, dl));
6588 }
6589 return DAG.getNode(N->getOpcode(), dl, NVT, Promoted);
6590 }
6591
6592 // Directly extend to the appropriate transform-to type.
6593 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
6594}
6595
6596SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_FIND_LAST_ACTIVE(SDNode *N) {
6597 EVT VT = N->getValueType(0);
6598 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6599 return DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, SDLoc(N), NVT, N->ops());
6600}
6601
6602SDValue DAGTypeLegalizer::PromoteIntRes_GET_ACTIVE_LANE_MASK(SDNode *N) {
6603 EVT VT = N->getValueType(0);
6604 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6605 return DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, SDLoc(N), NVT, N->ops());
6606}
6607
6608SDValue DAGTypeLegalizer::PromoteIntRes_PARTIAL_REDUCE_MLA(SDNode *N) {
6609 SDLoc DL(N);
6610 EVT VT = N->getValueType(0);
6611 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6612 SDValue ExtAcc = GetPromotedInteger(N->getOperand(0));
6613 return DAG.getNode(N->getOpcode(), DL, NVT, ExtAcc, N->getOperand(1),
6614 N->getOperand(2));
6615}
6616
6617SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
6618 EVT OutVT = N->getValueType(0);
6619 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6620 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6621
6622 EVT NOutVTElem = NOutVT.getVectorElementType();
6623
6624 SDLoc dl(N);
6625 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6626
6627 SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
6628 NOutVTElem, N->getOperand(1));
6629 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT,
6630 V0, ConvElem, N->getOperand(2));
6631}
6632
6633SDValue DAGTypeLegalizer::PromoteIntRes_VECREDUCE(SDNode *N) {
6634 // The VECREDUCE result size may be larger than the element size, so
6635 // we can simply change the result type.
6636 SDLoc dl(N);
6637 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6638 return DAG.getNode(N->getOpcode(), dl, NVT, N->ops());
6639}
6640
6641SDValue DAGTypeLegalizer::PromoteIntRes_VP_REDUCE(SDNode *N) {
6642 // The VP_REDUCE result size may be larger than the element size, so we can
6643 // simply change the result type. However the start value and result must be
6644 // the same.
6645 SDLoc DL(N);
6646 SDValue Start = PromoteIntOpVectorReduction(N, N->getOperand(0));
6647 return DAG.getNode(N->getOpcode(), DL, Start.getValueType(), Start,
6648 N->getOperand(1), N->getOperand(2), N->getOperand(3));
6649}
6650
6651SDValue DAGTypeLegalizer::PromoteIntRes_PATCHPOINT(SDNode *N) {
6652 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6653 SDLoc dl(N);
6654
6655 assert(N->getNumValues() == 3 && "Expected 3 values for PATCHPOINT");
6656 SDVTList VTList = DAG.getVTList({NVT, MVT::Other, MVT::Glue});
6657
6658 SmallVector<SDValue> Ops(N->ops());
6659 SDValue Res = DAG.getNode(ISD::PATCHPOINT, dl, VTList, Ops);
6660
6661 // Replace chain and glue uses with the new patchpoint.
6662 SDValue From[] = {SDValue(N, 1), SDValue(N, 2)};
6663 SDValue To[] = {Res.getValue(1), Res.getValue(2)};
6664 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
6665
6666 return Res.getValue(0);
6667}
6668
6669SDValue DAGTypeLegalizer::PromoteIntRes_READ_REGISTER(SDNode *N) {
6670 const Function &Fn = DAG.getMachineFunction().getFunction();
6671 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
6672 "cannot use llvm.read_register with illegal type", Fn, N->getDebugLoc()));
6673
6674 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6675 ReplaceValueWith(SDValue(N, 1), N->getOperand(0));
6676 return DAG.getPOISON(NVT);
6677}
6678
6679SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
6680 SDLoc dl(N);
6681 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6682 SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl,
6683 TLI.getVectorIdxTy(DAG.getDataLayout()));
6685 V0->getValueType(0).getScalarType(), V0, V1);
6686
6687 // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
6688 // element types. If this is the case then we need to expand the outgoing
6689 // value and not truncate it.
6690 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
6691}
6692
6693SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_SUBVECTOR(SDNode *N) {
6694 SDLoc dl(N);
6695 // The result type is equal to the first input operand's type, so the
6696 // type that needs promoting must be the second source vector.
6697 SDValue V0 = N->getOperand(0);
6698 SDValue V1 = GetPromotedInteger(N->getOperand(1));
6699 SDValue Idx = N->getOperand(2);
6700 EVT PromVT = EVT::getVectorVT(*DAG.getContext(),
6701 V1.getValueType().getVectorElementType(),
6703 V0 = DAG.getAnyExtOrTrunc(V0, dl, PromVT);
6704 SDValue Ext = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, PromVT, V0, V1, Idx);
6705 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
6706}
6707
6708// FIXME: We wouldn't need this if clang could promote short integers
6709// that are arguments to FAKE_USE.
6710SDValue DAGTypeLegalizer::PromoteIntOp_FAKE_USE(SDNode *N) {
6711 SDLoc dl(N);
6712 SDValue V0 = N->getOperand(0);
6713 SDValue V1 = N->getOperand(1);
6714 EVT InVT1 = V1.getValueType();
6715 SDValue VPromoted =
6716 DAG.getNode(ISD::ANY_EXTEND, dl,
6717 TLI.getTypeToTransformTo(*DAG.getContext(), InVT1), V1);
6718 return DAG.getNode(N->getOpcode(), dl, N->getValueType(0), V0, VPromoted);
6719}
6720
6721SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_SUBVECTOR(SDNode *N) {
6722 SDLoc dl(N);
6723 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6724 MVT InVT = V0.getValueType().getSimpleVT();
6725 MVT OutVT = MVT::getVectorVT(InVT.getVectorElementType(),
6726 N->getValueType(0).getVectorNumElements());
6727 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, V0, N->getOperand(1));
6728 return DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), Ext);
6729}
6730
6731SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
6732 SDLoc dl(N);
6733
6734 EVT ResVT = N->getValueType(0);
6735 unsigned NumElems = N->getNumOperands();
6736
6737 if (ResVT.isScalableVector()) {
6738 SDValue ResVec = DAG.getUNDEF(ResVT);
6739
6740 for (unsigned OpIdx = 0; OpIdx < NumElems; ++OpIdx) {
6741 SDValue Op = N->getOperand(OpIdx);
6742 unsigned OpNumElts = Op.getValueType().getVectorMinNumElements();
6743 ResVec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, ResVec, Op,
6744 DAG.getIntPtrConstant(OpIdx * OpNumElts, dl));
6745 }
6746
6747 return ResVec;
6748 }
6749
6750 EVT RetSclrTy = N->getValueType(0).getVectorElementType();
6751
6753 NewOps.reserve(NumElems);
6754
6755 // For each incoming vector
6756 for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) {
6757 SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
6758 EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
6759 unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
6760
6761 for (unsigned i=0; i<NumElem; ++i) {
6762 // Extract element from incoming vector
6763 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Incoming,
6764 DAG.getVectorIdxConstant(i, dl));
6765 SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
6766 NewOps.push_back(Tr);
6767 }
6768 }
6769
6770 return DAG.getBuildVector(N->getValueType(0), dl, NewOps);
6771}
6772
6773SDValue DAGTypeLegalizer::ExpandIntOp_STACKMAP(SDNode *N, unsigned OpNo) {
6774 assert(OpNo > 1);
6775 SDValue Op = N->getOperand(OpNo);
6776
6777 // FIXME: Non-constant operands are not yet handled:
6778 // - https://github.com/llvm/llvm-project/issues/26431
6779 // - https://github.com/llvm/llvm-project/issues/55957
6780 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op);
6781 if (!CN)
6782 return SDValue();
6783
6784 // Copy operands before the one being expanded.
6785 SmallVector<SDValue> NewOps;
6786 for (unsigned I = 0; I < OpNo; I++)
6787 NewOps.push_back(N->getOperand(I));
6788
6789 EVT Ty = Op.getValueType();
6790 SDLoc DL = SDLoc(N);
6791 if (CN->getConstantIntValue()->getValue().getActiveBits() < 64) {
6792 NewOps.push_back(
6793 DAG.getTargetConstant(StackMaps::ConstantOp, DL, MVT::i64));
6794 NewOps.push_back(DAG.getTargetConstant(CN->getZExtValue(), DL, Ty));
6795 } else {
6796 // FIXME: https://github.com/llvm/llvm-project/issues/55609
6797 return SDValue();
6798 }
6799
6800 // Copy remaining operands.
6801 for (unsigned I = OpNo + 1; I < N->getNumOperands(); I++)
6802 NewOps.push_back(N->getOperand(I));
6803
6804 SDValue NewNode = DAG.getNode(N->getOpcode(), DL, N->getVTList(), NewOps);
6805
6806 for (unsigned ResNum = 0; ResNum < N->getNumValues(); ResNum++)
6807 ReplaceValueWith(SDValue(N, ResNum), NewNode.getValue(ResNum));
6808
6809 return SDValue(); // Signal that we have replaced the node already.
6810}
6811
6812SDValue DAGTypeLegalizer::ExpandIntOp_PATCHPOINT(SDNode *N, unsigned OpNo) {
6813 assert(OpNo >= 7);
6814 SDValue Op = N->getOperand(OpNo);
6815
6816 // FIXME: Non-constant operands are not yet handled:
6817 // - https://github.com/llvm/llvm-project/issues/26431
6818 // - https://github.com/llvm/llvm-project/issues/55957
6819 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op);
6820 if (!CN)
6821 return SDValue();
6822
6823 // Copy operands before the one being expanded.
6824 SmallVector<SDValue> NewOps;
6825 for (unsigned I = 0; I < OpNo; I++)
6826 NewOps.push_back(N->getOperand(I));
6827
6828 EVT Ty = Op.getValueType();
6829 SDLoc DL = SDLoc(N);
6830 if (CN->getConstantIntValue()->getValue().getActiveBits() < 64) {
6831 NewOps.push_back(
6832 DAG.getTargetConstant(StackMaps::ConstantOp, DL, MVT::i64));
6833 NewOps.push_back(DAG.getTargetConstant(CN->getZExtValue(), DL, Ty));
6834 } else {
6835 // FIXME: https://github.com/llvm/llvm-project/issues/55609
6836 return SDValue();
6837 }
6838
6839 // Copy remaining operands.
6840 for (unsigned I = OpNo + 1; I < N->getNumOperands(); I++)
6841 NewOps.push_back(N->getOperand(I));
6842
6843 SDValue NewNode = DAG.getNode(N->getOpcode(), DL, N->getVTList(), NewOps);
6844
6845 for (unsigned ResNum = 0; ResNum < N->getNumValues(); ResNum++)
6846 ReplaceValueWith(SDValue(N, ResNum), NewNode.getValue(ResNum));
6847
6848 return SDValue(); // Signal that we have replaced the node already.
6849}
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:547
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:332
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
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:880
#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)