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