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