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