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