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