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