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