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