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