LLVM 18.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 dbgs() << "\n");
44 SDValue Res = SDValue();
45
46 // See if the target wants to custom expand this node.
47 if (CustomLowerNode(N, N->getValueType(ResNo), true)) {
48 LLVM_DEBUG(dbgs() << "Node has been custom expanded, done\n");
49 return;
50 }
51
52 switch (N->getOpcode()) {
53 default:
54#ifndef NDEBUG
55 dbgs() << "PromoteIntegerResult #" << ResNo << ": ";
56 N->dump(&DAG); dbgs() << "\n";
57#endif
58 report_fatal_error("Do not know how to promote this operator!");
59 case ISD::MERGE_VALUES:Res = PromoteIntRes_MERGE_VALUES(N, ResNo); break;
60 case ISD::AssertSext: Res = PromoteIntRes_AssertSext(N); break;
61 case ISD::AssertZext: Res = PromoteIntRes_AssertZext(N); break;
62 case ISD::BITCAST: Res = PromoteIntRes_BITCAST(N); break;
63 case ISD::VP_BITREVERSE:
64 case ISD::BITREVERSE: Res = PromoteIntRes_BITREVERSE(N); break;
65 case ISD::VP_BSWAP:
66 case ISD::BSWAP: Res = PromoteIntRes_BSWAP(N); break;
67 case ISD::BUILD_PAIR: Res = PromoteIntRes_BUILD_PAIR(N); break;
68 case ISD::Constant: Res = PromoteIntRes_Constant(N); break;
69 case ISD::VP_CTLZ_ZERO_UNDEF:
70 case ISD::VP_CTLZ:
72 case ISD::CTLZ: Res = PromoteIntRes_CTLZ(N); break;
73 case ISD::PARITY:
74 case ISD::VP_CTPOP:
75 case ISD::CTPOP: Res = PromoteIntRes_CTPOP_PARITY(N); break;
76 case ISD::VP_CTTZ_ZERO_UNDEF:
77 case ISD::VP_CTTZ:
79 case ISD::CTTZ: Res = PromoteIntRes_CTTZ(N); break;
81 Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
82 case ISD::LOAD: Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N)); break;
83 case ISD::MLOAD: Res = PromoteIntRes_MLOAD(cast<MaskedLoadSDNode>(N));
84 break;
85 case ISD::MGATHER: Res = PromoteIntRes_MGATHER(cast<MaskedGatherSDNode>(N));
86 break;
87 case ISD::SELECT:
88 case ISD::VSELECT:
89 case ISD::VP_SELECT:
90 case ISD::VP_MERGE:
91 Res = PromoteIntRes_Select(N);
92 break;
93 case ISD::SELECT_CC: Res = PromoteIntRes_SELECT_CC(N); break;
96 case ISD::SETCC: Res = PromoteIntRes_SETCC(N); break;
97 case ISD::SMIN:
98 case ISD::SMAX: Res = PromoteIntRes_SExtIntBinOp(N); break;
99 case ISD::UMIN:
100 case ISD::UMAX: Res = PromoteIntRes_UMINUMAX(N); break;
101
102 case ISD::SHL:
103 case ISD::VP_SHL: Res = PromoteIntRes_SHL(N); break;
105 Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
106 case ISD::SRA:
107 case ISD::VP_ASHR: Res = PromoteIntRes_SRA(N); break;
108 case ISD::SRL:
109 case ISD::VP_LSHR: Res = PromoteIntRes_SRL(N); break;
110 case ISD::VP_TRUNCATE:
111 case ISD::TRUNCATE: Res = PromoteIntRes_TRUNCATE(N); break;
112 case ISD::UNDEF: Res = PromoteIntRes_UNDEF(N); break;
113 case ISD::VAARG: Res = PromoteIntRes_VAARG(N); break;
114 case ISD::VSCALE: Res = PromoteIntRes_VSCALE(N); break;
115
117 Res = PromoteIntRes_EXTRACT_SUBVECTOR(N); break;
119 Res = PromoteIntRes_INSERT_SUBVECTOR(N); break;
121 Res = PromoteIntRes_VECTOR_REVERSE(N); break;
123 Res = PromoteIntRes_VECTOR_SHUFFLE(N); break;
125 Res = PromoteIntRes_VECTOR_SPLICE(N); break;
128 Res = PromoteIntRes_VECTOR_INTERLEAVE_DEINTERLEAVE(N);
129 return;
131 Res = PromoteIntRes_INSERT_VECTOR_ELT(N); break;
133 Res = PromoteIntRes_BUILD_VECTOR(N);
134 break;
137 Res = PromoteIntRes_ScalarOp(N);
138 break;
139 case ISD::STEP_VECTOR: Res = PromoteIntRes_STEP_VECTOR(N); break;
141 Res = PromoteIntRes_CONCAT_VECTORS(N); break;
142
146 Res = PromoteIntRes_EXTEND_VECTOR_INREG(N); break;
147
148 case ISD::SIGN_EXTEND:
149 case ISD::VP_SIGN_EXTEND:
150 case ISD::ZERO_EXTEND:
151 case ISD::VP_ZERO_EXTEND:
152 case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break;
153
154 case ISD::VP_FP_TO_SINT:
155 case ISD::VP_FP_TO_UINT:
158 case ISD::FP_TO_SINT:
159 case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break;
160
163 Res = PromoteIntRes_FP_TO_XINT_SAT(N); break;
164
165 case ISD::FP_TO_BF16:
166 case ISD::FP_TO_FP16:
167 Res = PromoteIntRes_FP_TO_FP16_BF16(N);
168 break;
169
170 case ISD::GET_ROUNDING: Res = PromoteIntRes_GET_ROUNDING(N); break;
171
172 case ISD::AND:
173 case ISD::OR:
174 case ISD::XOR:
175 case ISD::ADD:
176 case ISD::SUB:
177 case ISD::MUL:
178 case ISD::VP_AND:
179 case ISD::VP_OR:
180 case ISD::VP_XOR:
181 case ISD::VP_ADD:
182 case ISD::VP_SUB:
183 case ISD::VP_MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break;
184
185 case ISD::VP_SMIN:
186 case ISD::VP_SMAX:
187 case ISD::SDIV:
188 case ISD::SREM:
189 case ISD::VP_SDIV:
190 case ISD::VP_SREM: Res = PromoteIntRes_SExtIntBinOp(N); break;
191
192 case ISD::VP_UMIN:
193 case ISD::VP_UMAX:
194 case ISD::UDIV:
195 case ISD::UREM:
196 case ISD::VP_UDIV:
197 case ISD::VP_UREM: Res = PromoteIntRes_ZExtIntBinOp(N); break;
198
199 case ISD::SADDO:
200 case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
201 case ISD::UADDO:
202 case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
203 case ISD::SMULO:
204 case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;
205
206 case ISD::ADDE:
207 case ISD::SUBE:
208 case ISD::UADDO_CARRY:
209 case ISD::USUBO_CARRY: Res = PromoteIntRes_UADDSUBO_CARRY(N, ResNo); break;
210
211 case ISD::SADDO_CARRY:
212 case ISD::SSUBO_CARRY: Res = PromoteIntRes_SADDSUBO_CARRY(N, ResNo); break;
213
214 case ISD::SADDSAT:
215 case ISD::UADDSAT:
216 case ISD::SSUBSAT:
217 case ISD::USUBSAT:
218 case ISD::SSHLSAT:
219 case ISD::USHLSAT: Res = PromoteIntRes_ADDSUBSHLSAT(N); break;
220
221 case ISD::SMULFIX:
222 case ISD::SMULFIXSAT:
223 case ISD::UMULFIX:
224 case ISD::UMULFIXSAT: Res = PromoteIntRes_MULFIX(N); break;
225
226 case ISD::SDIVFIX:
227 case ISD::SDIVFIXSAT:
228 case ISD::UDIVFIX:
229 case ISD::UDIVFIXSAT: Res = PromoteIntRes_DIVFIX(N); break;
230
231 case ISD::ABS: Res = PromoteIntRes_ABS(N); break;
232
233 case ISD::ATOMIC_LOAD:
234 Res = PromoteIntRes_Atomic0(cast<AtomicSDNode>(N)); break;
235
247 case ISD::ATOMIC_SWAP:
248 Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
249
252 Res = PromoteIntRes_AtomicCmpSwap(cast<AtomicSDNode>(N), ResNo);
253 break;
254
264 Res = PromoteIntRes_VECREDUCE(N);
265 break;
266
267 case ISD::VP_REDUCE_ADD:
268 case ISD::VP_REDUCE_MUL:
269 case ISD::VP_REDUCE_AND:
270 case ISD::VP_REDUCE_OR:
271 case ISD::VP_REDUCE_XOR:
272 case ISD::VP_REDUCE_SMAX:
273 case ISD::VP_REDUCE_SMIN:
274 case ISD::VP_REDUCE_UMAX:
275 case ISD::VP_REDUCE_UMIN:
276 Res = PromoteIntRes_VP_REDUCE(N);
277 break;
278
279 case ISD::FREEZE:
280 Res = PromoteIntRes_FREEZE(N);
281 break;
282
283 case ISD::ROTL:
284 case ISD::ROTR:
285 Res = PromoteIntRes_Rotate(N);
286 break;
287
288 case ISD::FSHL:
289 case ISD::FSHR:
290 Res = PromoteIntRes_FunnelShift(N);
291 break;
292
293 case ISD::VP_FSHL:
294 case ISD::VP_FSHR:
295 Res = PromoteIntRes_VPFunnelShift(N);
296 break;
297
298 case ISD::IS_FPCLASS:
299 Res = PromoteIntRes_IS_FPCLASS(N);
300 break;
301 case ISD::FFREXP:
302 Res = PromoteIntRes_FFREXP(N);
303 break;
304 }
305
306 // If the result is null then the sub-method took care of registering it.
307 if (Res.getNode())
308 SetPromotedInteger(SDValue(N, ResNo), Res);
309}
310
311SDValue DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode *N,
312 unsigned ResNo) {
313 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
314 return GetPromotedInteger(Op);
315}
316
317SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
318 // Sign-extend the new bits, and continue the assertion.
319 SDValue Op = SExtPromotedInteger(N->getOperand(0));
320 return DAG.getNode(ISD::AssertSext, SDLoc(N),
321 Op.getValueType(), Op, N->getOperand(1));
322}
323
324SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
325 // Zero the new bits, and continue the assertion.
326 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
327 return DAG.getNode(ISD::AssertZext, SDLoc(N),
328 Op.getValueType(), Op, N->getOperand(1));
329}
330
331SDValue DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode *N) {
332 EVT ResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
333 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
334 N->getMemoryVT(), ResVT,
335 N->getChain(), N->getBasePtr(),
336 N->getMemOperand());
337 // Legalize the chain result - switch anything that used the old chain to
338 // use the new one.
339 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
340 return Res;
341}
342
343SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
344 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
345 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
346 N->getMemoryVT(),
347 N->getChain(), N->getBasePtr(),
348 Op2, N->getMemOperand());
349 // Legalize the chain result - switch anything that used the old chain to
350 // use the new one.
351 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
352 return Res;
353}
354
355SDValue DAGTypeLegalizer::PromoteIntRes_AtomicCmpSwap(AtomicSDNode *N,
356 unsigned ResNo) {
357 if (ResNo == 1) {
359 EVT SVT = getSetCCResultType(N->getOperand(2).getValueType());
360 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
361
362 // Only use the result of getSetCCResultType if it is legal,
363 // otherwise just use the promoted result type (NVT).
364 if (!TLI.isTypeLegal(SVT))
365 SVT = NVT;
366
367 SDVTList VTs = DAG.getVTList(N->getValueType(0), SVT, MVT::Other);
368 SDValue Res = DAG.getAtomicCmpSwap(
369 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, SDLoc(N), N->getMemoryVT(), VTs,
370 N->getChain(), N->getBasePtr(), N->getOperand(2), N->getOperand(3),
371 N->getMemOperand());
372 ReplaceValueWith(SDValue(N, 0), Res.getValue(0));
373 ReplaceValueWith(SDValue(N, 2), Res.getValue(2));
374 return Res.getValue(1);
375 }
376
377 // Op2 is used for the comparison and thus must be extended according to the
378 // target's atomic operations. Op3 is merely stored and so can be left alone.
379 SDValue Op2 = N->getOperand(2);
380 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
381 switch (TLI.getExtendForAtomicCmpSwapArg()) {
382 case ISD::SIGN_EXTEND:
383 Op2 = SExtPromotedInteger(Op2);
384 break;
385 case ISD::ZERO_EXTEND:
386 Op2 = ZExtPromotedInteger(Op2);
387 break;
388 case ISD::ANY_EXTEND:
389 Op2 = GetPromotedInteger(Op2);
390 break;
391 default:
392 llvm_unreachable("Invalid atomic op extension");
393 }
394
395 SDVTList VTs =
396 DAG.getVTList(Op2.getValueType(), N->getValueType(1), MVT::Other);
397 SDValue Res = DAG.getAtomicCmpSwap(
398 N->getOpcode(), SDLoc(N), N->getMemoryVT(), VTs, N->getChain(),
399 N->getBasePtr(), Op2, Op3, N->getMemOperand());
400 // Update the use to N with the newly created Res.
401 for (unsigned i = 1, NumResults = N->getNumValues(); i < NumResults; ++i)
402 ReplaceValueWith(SDValue(N, i), Res.getValue(i));
403 return Res;
404}
405
406SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
407 SDValue InOp = N->getOperand(0);
408 EVT InVT = InOp.getValueType();
409 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
410 EVT OutVT = N->getValueType(0);
411 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
412 SDLoc dl(N);
413
414 switch (getTypeAction(InVT)) {
416 break;
418 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector() && !NInVT.isVector())
419 // The input promotes to the same size. Convert the promoted value.
420 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp));
421 break;
423 // Promote the integer operand by hand.
424 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
426 // Promote the integer operand by hand.
427 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftPromotedHalf(InOp));
429 // Convert the promoted float by hand.
430 if (!NOutVT.isVector())
431 return DAG.getNode(ISD::FP_TO_FP16, dl, NOutVT, GetPromotedFloat(InOp));
432 break;
433 }
436 break;
438 // Convert the element to an integer and promote it by hand.
439 if (!NOutVT.isVector())
440 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
441 BitConvertToInteger(GetScalarizedVector(InOp)));
442 break;
444 report_fatal_error("Scalarization of scalable vectors is not supported.");
446 if (!NOutVT.isVector()) {
447 // For example, i32 = BITCAST v2i16 on alpha. Convert the split
448 // pieces of the input into integers and reassemble in the final type.
449 SDValue Lo, Hi;
450 GetSplitVector(N->getOperand(0), Lo, Hi);
451 Lo = BitConvertToInteger(Lo);
452 Hi = BitConvertToInteger(Hi);
453
454 if (DAG.getDataLayout().isBigEndian())
455 std::swap(Lo, Hi);
456
457 InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
459 NOutVT.getSizeInBits()),
460 JoinIntegers(Lo, Hi));
461 return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp);
462 }
463 break;
464 }
466 // The input is widened to the same size. Convert to the widened value.
467 // Make sure that the outgoing value is not a vector, because this would
468 // make us bitcast between two vectors which are legalized in different ways.
469 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector()) {
470 SDValue Res =
471 DAG.getNode(ISD::BITCAST, dl, NOutVT, GetWidenedVector(InOp));
472
473 // For big endian targets we need to shift the casted value or the
474 // interesting bits will end up at the wrong place.
475 if (DAG.getDataLayout().isBigEndian()) {
476 unsigned ShiftAmt = NInVT.getSizeInBits() - InVT.getSizeInBits();
477 assert(ShiftAmt < NOutVT.getSizeInBits() && "Too large shift amount!");
478 Res = DAG.getNode(ISD::SRL, dl, NOutVT, Res,
479 DAG.getShiftAmountConstant(ShiftAmt, NOutVT, dl));
480 }
481 return Res;
482 }
483 // If the output type is also a vector and widening it to the same size
484 // as the widened input type would be a legal type, we can widen the bitcast
485 // and handle the promotion after.
486 if (NOutVT.isVector()) {
487 TypeSize WidenInSize = NInVT.getSizeInBits();
488 TypeSize OutSize = OutVT.getSizeInBits();
489 if (WidenInSize.hasKnownScalarFactor(OutSize)) {
490 unsigned Scale = WidenInSize.getKnownScalarFactor(OutSize);
491 EVT WideOutVT =
493 OutVT.getVectorElementCount() * Scale);
494 if (isTypeLegal(WideOutVT)) {
495 InOp = DAG.getBitcast(WideOutVT, GetWidenedVector(InOp));
496 InOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, InOp,
497 DAG.getVectorIdxConstant(0, dl));
498 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, InOp);
499 }
500 }
501 }
502 }
503
504 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
505 CreateStackStoreLoad(InOp, OutVT));
506}
507
508SDValue DAGTypeLegalizer::PromoteIntRes_FREEZE(SDNode *N) {
509 SDValue V = GetPromotedInteger(N->getOperand(0));
510 return DAG.getNode(ISD::FREEZE, SDLoc(N),
511 V.getValueType(), V);
512}
513
514SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
515 SDValue Op = GetPromotedInteger(N->getOperand(0));
516 EVT OVT = N->getValueType(0);
517 EVT NVT = Op.getValueType();
518 SDLoc dl(N);
519
520 // If the larger BSWAP isn't supported by the target, try to expand now.
521 // If we expand later we'll end up with more operations since we lost the
522 // original type. We only do this for scalars since we have a shuffle
523 // based lowering for vectors in LegalizeVectorOps.
524 if (!OVT.isVector() &&
526 if (SDValue Res = TLI.expandBSWAP(N, DAG))
527 return DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Res);
528 }
529
530 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
531 SDValue ShAmt = DAG.getShiftAmountConstant(DiffBits, NVT, dl);
532 if (N->getOpcode() == ISD::BSWAP)
533 return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
534 ShAmt);
535 SDValue Mask = N->getOperand(1);
536 SDValue EVL = N->getOperand(2);
537 return DAG.getNode(ISD::VP_LSHR, dl, NVT,
538 DAG.getNode(ISD::VP_BSWAP, dl, NVT, Op, Mask, EVL), ShAmt,
539 Mask, EVL);
540}
541
542SDValue DAGTypeLegalizer::PromoteIntRes_BITREVERSE(SDNode *N) {
543 SDValue Op = GetPromotedInteger(N->getOperand(0));
544 EVT OVT = N->getValueType(0);
545 EVT NVT = Op.getValueType();
546 SDLoc dl(N);
547
548 // If the larger BITREVERSE isn't supported by the target, try to expand now.
549 // If we expand later we'll end up with more operations since we lost the
550 // original type. We only do this for scalars since we have a shuffle
551 // based lowering for vectors in LegalizeVectorOps.
552 if (!OVT.isVector() && OVT.isSimple() &&
554 if (SDValue Res = TLI.expandBITREVERSE(N, DAG))
555 return DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Res);
556 }
557
558 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
559 SDValue ShAmt = DAG.getShiftAmountConstant(DiffBits, NVT, dl);
560 if (N->getOpcode() == ISD::BITREVERSE)
561 return DAG.getNode(ISD::SRL, dl, NVT,
562 DAG.getNode(ISD::BITREVERSE, dl, NVT, Op), ShAmt);
563 SDValue Mask = N->getOperand(1);
564 SDValue EVL = N->getOperand(2);
565 return DAG.getNode(ISD::VP_LSHR, dl, NVT,
566 DAG.getNode(ISD::VP_BITREVERSE, dl, NVT, Op, Mask, EVL),
567 ShAmt, Mask, EVL);
568}
569
570SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
571 // The pair element type may be legal, or may not promote to the same type as
572 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
573 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N),
575 N->getValueType(0)), JoinIntegers(N->getOperand(0),
576 N->getOperand(1)));
577}
578
579SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
580 EVT VT = N->getValueType(0);
581 // FIXME there is no actual debug info here
582 SDLoc dl(N);
583 // Zero extend things like i1, sign extend everything else. It shouldn't
584 // matter in theory which one we pick, but this tends to give better code?
585 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
586 SDValue Result = DAG.getNode(Opc, dl,
587 TLI.getTypeToTransformTo(*DAG.getContext(), VT),
588 SDValue(N, 0));
589 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
590 return Result;
591}
592
593SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
594 EVT OVT = N->getValueType(0);
595 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
596 SDLoc dl(N);
597
598 // If the larger CTLZ isn't supported by the target, try to expand now.
599 // If we expand later we'll end up with more operations since we lost the
600 // original type.
601 if (!OVT.isVector() && TLI.isTypeLegal(NVT) &&
604 if (SDValue Result = TLI.expandCTLZ(N, DAG)) {
605 Result = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Result);
606 return Result;
607 }
608 }
609
610 // Zero extend to the promoted type and do the count there.
611 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
612
613 // Subtract off the extra leading bits in the bigger type.
614 SDValue ExtractLeadingBits = DAG.getConstant(
615 NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(), dl, NVT);
616 if (!N->isVPOpcode())
617 return DAG.getNode(ISD::SUB, dl, NVT,
618 DAG.getNode(N->getOpcode(), dl, NVT, Op),
619 ExtractLeadingBits);
620 SDValue Mask = N->getOperand(1);
621 SDValue EVL = N->getOperand(2);
622 return DAG.getNode(ISD::VP_SUB, dl, NVT,
623 DAG.getNode(N->getOpcode(), dl, NVT, Op, Mask, EVL),
624 ExtractLeadingBits, Mask, EVL);
625}
626
627SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP_PARITY(SDNode *N) {
628 EVT OVT = N->getValueType(0);
629 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
630
631 // If the larger CTPOP isn't supported by the target, try to expand now.
632 // If we expand later we'll end up with more operations since we lost the
633 // original type.
634 // TODO: Expand ISD::PARITY. Need to move ExpandPARITY from LegalizeDAG to
635 // TargetLowering.
636 if (N->getOpcode() == ISD::CTPOP && !OVT.isVector() && TLI.isTypeLegal(NVT) &&
638 if (SDValue Result = TLI.expandCTPOP(N, DAG)) {
639 Result = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), NVT, Result);
640 return Result;
641 }
642 }
643
644 // Zero extend to the promoted type and do the count or parity there.
645 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
646 if (!N->isVPOpcode())
647 return DAG.getNode(N->getOpcode(), SDLoc(N), Op.getValueType(), Op);
648 return DAG.getNode(N->getOpcode(), SDLoc(N), Op.getValueType(), Op,
649 N->getOperand(1), N->getOperand(2));
650}
651
652SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
653 SDValue Op = GetPromotedInteger(N->getOperand(0));
654 EVT OVT = N->getValueType(0);
655 EVT NVT = Op.getValueType();
656 SDLoc dl(N);
657
658 // If the larger CTTZ isn't supported by the target, try to expand now.
659 // If we expand later we'll end up with more operations since we lost the
660 // original type. Don't expand if we can use CTPOP or CTLZ expansion on the
661 // larger type.
662 if (!OVT.isVector() && TLI.isTypeLegal(NVT) &&
665 !TLI.isOperationLegal(ISD::CTPOP, NVT) &&
666 !TLI.isOperationLegal(ISD::CTLZ, NVT)) {
667 if (SDValue Result = TLI.expandCTTZ(N, DAG)) {
668 Result = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Result);
669 return Result;
670 }
671 }
672
673 if (N->getOpcode() == ISD::CTTZ || N->getOpcode() == ISD::VP_CTTZ) {
674 // The count is the same in the promoted type except if the original
675 // value was zero. This can be handled by setting the bit just off
676 // the top of the original type.
677 auto TopBit = APInt::getOneBitSet(NVT.getScalarSizeInBits(),
678 OVT.getScalarSizeInBits());
679 if (N->getOpcode() == ISD::CTTZ)
680 Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, dl, NVT));
681 else
682 Op =
683 DAG.getNode(ISD::VP_OR, dl, NVT, Op, DAG.getConstant(TopBit, dl, NVT),
684 N->getOperand(1), N->getOperand(2));
685 }
686 if (!N->isVPOpcode())
687 return DAG.getNode(N->getOpcode(), dl, NVT, Op);
688 return DAG.getNode(N->getOpcode(), dl, NVT, Op, N->getOperand(1),
689 N->getOperand(2));
690}
691
692SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
693 SDLoc dl(N);
694 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
695
696 SDValue Op0 = N->getOperand(0);
697 SDValue Op1 = N->getOperand(1);
698
699 // If the input also needs to be promoted, do that first so we can get a
700 // get a good idea for the output type.
701 if (TLI.getTypeAction(*DAG.getContext(), Op0.getValueType())
703 SDValue In = GetPromotedInteger(Op0);
704
705 // If the new type is larger than NVT, use it. We probably won't need to
706 // promote it again.
707 EVT SVT = In.getValueType().getScalarType();
708 if (SVT.bitsGE(NVT)) {
709 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SVT, In, Op1);
710 return DAG.getAnyExtOrTrunc(Ext, dl, NVT);
711 }
712 }
713
714 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, Op0, Op1);
715}
716
717SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
718 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
719 unsigned NewOpc = N->getOpcode();
720 SDLoc dl(N);
721
722 // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
723 // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT
724 // and SINT conversions are Custom, there is no way to tell which is
725 // preferable. We choose SINT because that's the right thing on PPC.)
726 if (N->getOpcode() == ISD::FP_TO_UINT &&
729 NewOpc = ISD::FP_TO_SINT;
730
731 if (N->getOpcode() == ISD::STRICT_FP_TO_UINT &&
734 NewOpc = ISD::STRICT_FP_TO_SINT;
735
736 if (N->getOpcode() == ISD::VP_FP_TO_UINT &&
737 !TLI.isOperationLegal(ISD::VP_FP_TO_UINT, NVT) &&
738 TLI.isOperationLegalOrCustom(ISD::VP_FP_TO_SINT, NVT))
739 NewOpc = ISD::VP_FP_TO_SINT;
740
741 SDValue Res;
742 if (N->isStrictFPOpcode()) {
743 Res = DAG.getNode(NewOpc, dl, {NVT, MVT::Other},
744 {N->getOperand(0), N->getOperand(1)});
745 // Legalize the chain result - switch anything that used the old chain to
746 // use the new one.
747 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
748 } else if (NewOpc == ISD::VP_FP_TO_SINT || NewOpc == ISD::VP_FP_TO_UINT) {
749 Res = DAG.getNode(NewOpc, dl, NVT, {N->getOperand(0), N->getOperand(1),
750 N->getOperand(2)});
751 } else {
752 Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
753 }
754
755 // Assert that the converted value fits in the original type. If it doesn't
756 // (eg: because the value being converted is too big), then the result of the
757 // original operation was undefined anyway, so the assert is still correct.
758 //
759 // NOTE: fp-to-uint to fp-to-sint promotion guarantees zero extend. For example:
760 // before legalization: fp-to-uint16, 65534. -> 0xfffe
761 // after legalization: fp-to-sint32, 65534. -> 0x0000fffe
762 return DAG.getNode((N->getOpcode() == ISD::FP_TO_UINT ||
763 N->getOpcode() == ISD::STRICT_FP_TO_UINT ||
764 N->getOpcode() == ISD::VP_FP_TO_UINT)
767 dl, NVT, Res,
768 DAG.getValueType(N->getValueType(0).getScalarType()));
769}
770
771SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT_SAT(SDNode *N) {
772 // Promote the result type, while keeping the original width in Op1.
773 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
774 SDLoc dl(N);
775 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0),
776 N->getOperand(1));
777}
778
779SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_FP16_BF16(SDNode *N) {
780 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
781 SDLoc dl(N);
782
783 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
784}
785
786SDValue DAGTypeLegalizer::PromoteIntRes_GET_ROUNDING(SDNode *N) {
787 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
788 SDLoc dl(N);
789
790 SDValue Res =
791 DAG.getNode(N->getOpcode(), dl, {NVT, MVT::Other}, N->getOperand(0));
792
793 // Legalize the chain result - switch anything that used the old chain to
794 // use the new one.
795 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
796 return Res;
797}
798
799SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
800 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
801 SDLoc dl(N);
802
803 if (getTypeAction(N->getOperand(0).getValueType())
805 SDValue Res = GetPromotedInteger(N->getOperand(0));
806 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
807
808 // If the result and operand types are the same after promotion, simplify
809 // to an in-register extension. Unless this is a VP_*_EXTEND.
810 if (NVT == Res.getValueType() && N->getNumOperands() == 1) {
811 // The high bits are not guaranteed to be anything. Insert an extend.
812 if (N->getOpcode() == ISD::SIGN_EXTEND)
813 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
814 DAG.getValueType(N->getOperand(0).getValueType()));
815 if (N->getOpcode() == ISD::ZERO_EXTEND)
816 return DAG.getZeroExtendInReg(Res, dl, N->getOperand(0).getValueType());
817 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
818 return Res;
819 }
820 }
821
822 // Otherwise, just extend the original operand all the way to the larger type.
823 if (N->getNumOperands() != 1) {
824 assert(N->getNumOperands() == 3 && "Unexpected number of operands!");
825 assert(N->isVPOpcode() && "Expected VP opcode");
826 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0),
827 N->getOperand(1), N->getOperand(2));
828 }
829 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
830}
831
832SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
833 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
834 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
835 ISD::LoadExtType ExtType =
836 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
837 SDLoc dl(N);
838 SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
839 N->getMemoryVT(), N->getMemOperand());
840
841 // Legalize the chain result - switch anything that used the old chain to
842 // use the new one.
843 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
844 return Res;
845}
846
847SDValue DAGTypeLegalizer::PromoteIntRes_MLOAD(MaskedLoadSDNode *N) {
848 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
849 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
850
851 ISD::LoadExtType ExtType = N->getExtensionType();
852 if (ExtType == ISD::NON_EXTLOAD)
853 ExtType = ISD::EXTLOAD;
854
855 SDLoc dl(N);
856 SDValue Res = DAG.getMaskedLoad(NVT, dl, N->getChain(), N->getBasePtr(),
857 N->getOffset(), N->getMask(), ExtPassThru,
858 N->getMemoryVT(), N->getMemOperand(),
859 N->getAddressingMode(), ExtType,
860 N->isExpandingLoad());
861 // Legalize the chain result - switch anything that used the old chain to
862 // use the new one.
863 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
864 return Res;
865}
866
867SDValue DAGTypeLegalizer::PromoteIntRes_MGATHER(MaskedGatherSDNode *N) {
868 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
869 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
870 assert(NVT == ExtPassThru.getValueType() &&
871 "Gather result type and the passThru argument type should be the same");
872
873 ISD::LoadExtType ExtType = N->getExtensionType();
874 if (ExtType == ISD::NON_EXTLOAD)
875 ExtType = ISD::EXTLOAD;
876
877 SDLoc dl(N);
878 SDValue Ops[] = {N->getChain(), ExtPassThru, N->getMask(), N->getBasePtr(),
879 N->getIndex(), N->getScale() };
880 SDValue Res = DAG.getMaskedGather(DAG.getVTList(NVT, MVT::Other),
881 N->getMemoryVT(), dl, Ops,
882 N->getMemOperand(), N->getIndexType(),
883 ExtType);
884 // Legalize the chain result - switch anything that used the old chain to
885 // use the new one.
886 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
887 return Res;
888}
889
890/// Promote the overflow flag of an overflowing arithmetic node.
891SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
892 // Change the return type of the boolean result while obeying
893 // getSetCCResultType.
894 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
895 EVT VT = N->getValueType(0);
896 EVT SVT = getSetCCResultType(VT);
897 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1) };
898 unsigned NumOps = N->getNumOperands();
899 assert(NumOps <= 3 && "Too many operands");
900 if (NumOps == 3)
901 Ops[2] = N->getOperand(2);
902
903 SDLoc dl(N);
904 SDValue Res = DAG.getNode(N->getOpcode(), dl, DAG.getVTList(VT, SVT),
905 ArrayRef(Ops, NumOps));
906
907 // Modified the sum result - switch anything that used the old sum to use
908 // the new one.
909 ReplaceValueWith(SDValue(N, 0), Res);
910
911 // Convert to the expected type.
912 return DAG.getBoolExtOrTrunc(Res.getValue(1), dl, NVT, VT);
913}
914
915SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBSHLSAT(SDNode *N) {
916 // If the promoted type is legal, we can convert this to:
917 // 1. ANY_EXTEND iN to iM
918 // 2. SHL by M-N
919 // 3. [US][ADD|SUB|SHL]SAT
920 // 4. L/ASHR by M-N
921 // Else it is more efficient to convert this to a min and a max
922 // operation in the higher precision arithmetic.
923 SDLoc dl(N);
924 SDValue Op1 = N->getOperand(0);
925 SDValue Op2 = N->getOperand(1);
926 unsigned OldBits = Op1.getScalarValueSizeInBits();
927
928 unsigned Opcode = N->getOpcode();
929 bool IsShift = Opcode == ISD::USHLSAT || Opcode == ISD::SSHLSAT;
930
931 SDValue Op1Promoted, Op2Promoted;
932 if (IsShift) {
933 Op1Promoted = GetPromotedInteger(Op1);
934 Op2Promoted = ZExtPromotedInteger(Op2);
935 } else if (Opcode == ISD::UADDSAT || Opcode == ISD::USUBSAT) {
936 Op1Promoted = ZExtPromotedInteger(Op1);
937 Op2Promoted = ZExtPromotedInteger(Op2);
938 } else {
939 Op1Promoted = SExtPromotedInteger(Op1);
940 Op2Promoted = SExtPromotedInteger(Op2);
941 }
942 EVT PromotedType = Op1Promoted.getValueType();
943 unsigned NewBits = PromotedType.getScalarSizeInBits();
944
945 if (Opcode == ISD::UADDSAT) {
946 APInt MaxVal = APInt::getAllOnes(OldBits).zext(NewBits);
947 SDValue SatMax = DAG.getConstant(MaxVal, dl, PromotedType);
948 SDValue Add =
949 DAG.getNode(ISD::ADD, dl, PromotedType, Op1Promoted, Op2Promoted);
950 return DAG.getNode(ISD::UMIN, dl, PromotedType, Add, SatMax);
951 }
952
953 // USUBSAT can always be promoted as long as we have zero-extended the args.
954 if (Opcode == ISD::USUBSAT)
955 return DAG.getNode(ISD::USUBSAT, dl, PromotedType, Op1Promoted,
956 Op2Promoted);
957
958 // Shift cannot use a min/max expansion, we can't detect overflow if all of
959 // the bits have been shifted out.
960 if (IsShift || TLI.isOperationLegal(Opcode, PromotedType)) {
961 unsigned ShiftOp;
962 switch (Opcode) {
963 case ISD::SADDSAT:
964 case ISD::SSUBSAT:
965 case ISD::SSHLSAT:
966 ShiftOp = ISD::SRA;
967 break;
968 case ISD::USHLSAT:
969 ShiftOp = ISD::SRL;
970 break;
971 default:
972 llvm_unreachable("Expected opcode to be signed or unsigned saturation "
973 "addition, subtraction or left shift");
974 }
975
976 unsigned SHLAmount = NewBits - OldBits;
977 SDValue ShiftAmount =
978 DAG.getShiftAmountConstant(SHLAmount, PromotedType, dl);
979 Op1Promoted =
980 DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted, ShiftAmount);
981 if (!IsShift)
982 Op2Promoted =
983 DAG.getNode(ISD::SHL, dl, PromotedType, Op2Promoted, ShiftAmount);
984
986 DAG.getNode(Opcode, dl, PromotedType, Op1Promoted, Op2Promoted);
987 return DAG.getNode(ShiftOp, dl, PromotedType, Result, ShiftAmount);
988 }
989
990 unsigned AddOp = Opcode == ISD::SADDSAT ? ISD::ADD : ISD::SUB;
991 APInt MinVal = APInt::getSignedMinValue(OldBits).sext(NewBits);
992 APInt MaxVal = APInt::getSignedMaxValue(OldBits).sext(NewBits);
993 SDValue SatMin = DAG.getConstant(MinVal, dl, PromotedType);
994 SDValue SatMax = DAG.getConstant(MaxVal, dl, PromotedType);
996 DAG.getNode(AddOp, dl, PromotedType, Op1Promoted, Op2Promoted);
997 Result = DAG.getNode(ISD::SMIN, dl, PromotedType, Result, SatMax);
998 Result = DAG.getNode(ISD::SMAX, dl, PromotedType, Result, SatMin);
999 return Result;
1000}
1001
1002SDValue DAGTypeLegalizer::PromoteIntRes_MULFIX(SDNode *N) {
1003 // Can just promote the operands then continue with operation.
1004 SDLoc dl(N);
1005 SDValue Op1Promoted, Op2Promoted;
1006 bool Signed =
1007 N->getOpcode() == ISD::SMULFIX || N->getOpcode() == ISD::SMULFIXSAT;
1008 bool Saturating =
1009 N->getOpcode() == ISD::SMULFIXSAT || N->getOpcode() == ISD::UMULFIXSAT;
1010 if (Signed) {
1011 Op1Promoted = SExtPromotedInteger(N->getOperand(0));
1012 Op2Promoted = SExtPromotedInteger(N->getOperand(1));
1013 } else {
1014 Op1Promoted = ZExtPromotedInteger(N->getOperand(0));
1015 Op2Promoted = ZExtPromotedInteger(N->getOperand(1));
1016 }
1017 EVT OldType = N->getOperand(0).getValueType();
1018 EVT PromotedType = Op1Promoted.getValueType();
1019 unsigned DiffSize =
1020 PromotedType.getScalarSizeInBits() - OldType.getScalarSizeInBits();
1021
1022 if (Saturating) {
1023 // Promoting the operand and result values changes the saturation width,
1024 // which is extends the values that we clamp to on saturation. This could be
1025 // resolved by shifting one of the operands the same amount, which would
1026 // also shift the result we compare against, then shifting back.
1027 Op1Promoted =
1028 DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted,
1029 DAG.getShiftAmountConstant(DiffSize, PromotedType, dl));
1030 SDValue Result = DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted,
1031 Op2Promoted, N->getOperand(2));
1032 unsigned ShiftOp = Signed ? ISD::SRA : ISD::SRL;
1033 return DAG.getNode(ShiftOp, dl, PromotedType, Result,
1034 DAG.getShiftAmountConstant(DiffSize, PromotedType, dl));
1035 }
1036 return DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted, Op2Promoted,
1037 N->getOperand(2));
1038}
1039
1041 unsigned SatW, bool Signed,
1042 const TargetLowering &TLI,
1043 SelectionDAG &DAG) {
1044 EVT VT = V.getValueType();
1045 unsigned VTW = VT.getScalarSizeInBits();
1046
1047 if (!Signed) {
1048 // Saturate to the unsigned maximum by getting the minimum of V and the
1049 // maximum.
1050 return DAG.getNode(ISD::UMIN, dl, VT, V,
1051 DAG.getConstant(APInt::getLowBitsSet(VTW, SatW),
1052 dl, VT));
1053 }
1054
1055 // Saturate to the signed maximum (the low SatW - 1 bits) by taking the
1056 // signed minimum of it and V.
1057 V = DAG.getNode(ISD::SMIN, dl, VT, V,
1058 DAG.getConstant(APInt::getLowBitsSet(VTW, SatW - 1),
1059 dl, VT));
1060 // Saturate to the signed minimum (the high SatW + 1 bits) by taking the
1061 // signed maximum of it and V.
1062 V = DAG.getNode(ISD::SMAX, dl, VT, V,
1063 DAG.getConstant(APInt::getHighBitsSet(VTW, VTW - SatW + 1),
1064 dl, VT));
1065 return V;
1066}
1067
1069 unsigned Scale, const TargetLowering &TLI,
1070 SelectionDAG &DAG, unsigned SatW = 0) {
1071 EVT VT = LHS.getValueType();
1072 unsigned VTSize = VT.getScalarSizeInBits();
1073 bool Signed = N->getOpcode() == ISD::SDIVFIX ||
1074 N->getOpcode() == ISD::SDIVFIXSAT;
1075 bool Saturating = N->getOpcode() == ISD::SDIVFIXSAT ||
1076 N->getOpcode() == ISD::UDIVFIXSAT;
1077
1078 SDLoc dl(N);
1079 // Widen the types by a factor of two. This is guaranteed to expand, since it
1080 // will always have enough high bits in the LHS to shift into.
1081 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VTSize * 2);
1082 if (VT.isVector())
1083 WideVT = EVT::getVectorVT(*DAG.getContext(), WideVT,
1085 LHS = DAG.getExtOrTrunc(Signed, LHS, dl, WideVT);
1086 RHS = DAG.getExtOrTrunc(Signed, RHS, dl, WideVT);
1087 SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, LHS, RHS, Scale,
1088 DAG);
1089 assert(Res && "Expanding DIVFIX with wide type failed?");
1090 if (Saturating) {
1091 // If the caller has told us to saturate at something less, use that width
1092 // instead of the type before doubling. However, it cannot be more than
1093 // what we just widened!
1094 assert(SatW <= VTSize &&
1095 "Tried to saturate to more than the original type?");
1096 Res = SaturateWidenedDIVFIX(Res, dl, SatW == 0 ? VTSize : SatW, Signed,
1097 TLI, DAG);
1098 }
1099 return DAG.getZExtOrTrunc(Res, dl, VT);
1100}
1101
1102SDValue DAGTypeLegalizer::PromoteIntRes_DIVFIX(SDNode *N) {
1103 SDLoc dl(N);
1104 SDValue Op1Promoted, Op2Promoted;
1105 bool Signed = N->getOpcode() == ISD::SDIVFIX ||
1106 N->getOpcode() == ISD::SDIVFIXSAT;
1107 bool Saturating = N->getOpcode() == ISD::SDIVFIXSAT ||
1108 N->getOpcode() == ISD::UDIVFIXSAT;
1109 if (Signed) {
1110 Op1Promoted = SExtPromotedInteger(N->getOperand(0));
1111 Op2Promoted = SExtPromotedInteger(N->getOperand(1));
1112 } else {
1113 Op1Promoted = ZExtPromotedInteger(N->getOperand(0));
1114 Op2Promoted = ZExtPromotedInteger(N->getOperand(1));
1115 }
1116 EVT PromotedType = Op1Promoted.getValueType();
1117 unsigned Scale = N->getConstantOperandVal(2);
1118
1119 // If the type is already legal and the operation is legal in that type, we
1120 // should not early expand.
1121 if (TLI.isTypeLegal(PromotedType)) {
1123 TLI.getFixedPointOperationAction(N->getOpcode(), PromotedType, Scale);
1124 if (Action == TargetLowering::Legal || Action == TargetLowering::Custom) {
1125 unsigned Diff = PromotedType.getScalarSizeInBits() -
1126 N->getValueType(0).getScalarSizeInBits();
1127 if (Saturating)
1128 Op1Promoted =
1129 DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted,
1130 DAG.getShiftAmountConstant(Diff, PromotedType, dl));
1131 SDValue Res = DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted,
1132 Op2Promoted, N->getOperand(2));
1133 if (Saturating)
1134 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, dl, PromotedType, Res,
1135 DAG.getShiftAmountConstant(Diff, PromotedType, dl));
1136 return Res;
1137 }
1138 }
1139
1140 // See if we can perform the division in this type without expanding.
1141 if (SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, Op1Promoted,
1142 Op2Promoted, Scale, DAG)) {
1143 if (Saturating)
1144 Res = SaturateWidenedDIVFIX(Res, dl,
1145 N->getValueType(0).getScalarSizeInBits(),
1146 Signed, TLI, DAG);
1147 return Res;
1148 }
1149 // If we cannot, expand it to twice the type width. If we are saturating, give
1150 // it the original width as a saturating width so we don't need to emit
1151 // two saturations.
1152 return earlyExpandDIVFIX(N, Op1Promoted, Op2Promoted, Scale, TLI, DAG,
1153 N->getValueType(0).getScalarSizeInBits());
1154}
1155
1156SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
1157 if (ResNo == 1)
1158 return PromoteIntRes_Overflow(N);
1159
1160 // The operation overflowed iff the result in the larger type is not the
1161 // sign extension of its truncation to the original type.
1162 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1163 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1164 EVT OVT = N->getOperand(0).getValueType();
1165 EVT NVT = LHS.getValueType();
1166 SDLoc dl(N);
1167
1168 // Do the arithmetic in the larger type.
1169 unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
1170 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
1171
1172 // Calculate the overflow flag: sign extend the arithmetic result from
1173 // the original type.
1174 SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
1175 DAG.getValueType(OVT));
1176 // Overflowed if and only if this is not equal to Res.
1177 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
1178
1179 // Use the calculated overflow everywhere.
1180 ReplaceValueWith(SDValue(N, 1), Ofl);
1181
1182 return Res;
1183}
1184
1185SDValue DAGTypeLegalizer::PromoteIntRes_Select(SDNode *N) {
1186 SDValue Mask = N->getOperand(0);
1187
1188 SDValue LHS = GetPromotedInteger(N->getOperand(1));
1189 SDValue RHS = GetPromotedInteger(N->getOperand(2));
1190
1191 unsigned Opcode = N->getOpcode();
1192 if (Opcode == ISD::VP_SELECT || Opcode == ISD::VP_MERGE)
1193 return DAG.getNode(Opcode, SDLoc(N), LHS.getValueType(), Mask, LHS, RHS,
1194 N->getOperand(3));
1195 return DAG.getNode(Opcode, SDLoc(N), LHS.getValueType(), Mask, LHS, RHS);
1196}
1197
1198SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
1199 SDValue LHS = GetPromotedInteger(N->getOperand(2));
1200 SDValue RHS = GetPromotedInteger(N->getOperand(3));
1201 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
1202 LHS.getValueType(), N->getOperand(0),
1203 N->getOperand(1), LHS, RHS, N->getOperand(4));
1204}
1205
1206SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
1207 unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0;
1208 EVT InVT = N->getOperand(OpNo).getValueType();
1209 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1210
1211 EVT SVT = getSetCCResultType(InVT);
1212
1213 // If we got back a type that needs to be promoted, this likely means the
1214 // the input type also needs to be promoted. So get the promoted type for
1215 // the input and try the query again.
1216 if (getTypeAction(SVT) == TargetLowering::TypePromoteInteger) {
1217 if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
1218 InVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
1219 SVT = getSetCCResultType(InVT);
1220 } else {
1221 // Input type isn't promoted, just use the default promoted type.
1222 SVT = NVT;
1223 }
1224 }
1225
1226 SDLoc dl(N);
1227 assert(SVT.isVector() == N->getOperand(OpNo).getValueType().isVector() &&
1228 "Vector compare must return a vector result!");
1229
1230 // Get the SETCC result using the canonical SETCC type.
1231 SDValue SetCC;
1232 if (N->isStrictFPOpcode()) {
1233 SDVTList VTs = DAG.getVTList({SVT, MVT::Other});
1234 SDValue Opers[] = {N->getOperand(0), N->getOperand(1),
1235 N->getOperand(2), N->getOperand(3)};
1236 SetCC = DAG.getNode(N->getOpcode(), dl, VTs, Opers, N->getFlags());
1237 // Legalize the chain result - switch anything that used the old chain to
1238 // use the new one.
1239 ReplaceValueWith(SDValue(N, 1), SetCC.getValue(1));
1240 } else
1241 SetCC = DAG.getNode(N->getOpcode(), dl, SVT, N->getOperand(0),
1242 N->getOperand(1), N->getOperand(2), N->getFlags());
1243
1244 // Convert to the expected type.
1245 return DAG.getSExtOrTrunc(SetCC, dl, NVT);
1246}
1247
1248SDValue DAGTypeLegalizer::PromoteIntRes_IS_FPCLASS(SDNode *N) {
1249 SDLoc DL(N);
1250 SDValue Arg = N->getOperand(0);
1251 SDValue Test = N->getOperand(1);
1252 EVT NResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1253 return DAG.getNode(ISD::IS_FPCLASS, DL, NResVT, Arg, Test);
1254}
1255
1256SDValue DAGTypeLegalizer::PromoteIntRes_FFREXP(SDNode *N) {
1257 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
1258 EVT VT = N->getValueType(0);
1259
1260 SDLoc dl(N);
1261 SDValue Res =
1262 DAG.getNode(N->getOpcode(), dl, DAG.getVTList(VT, NVT), N->getOperand(0));
1263
1264 ReplaceValueWith(SDValue(N, 0), Res);
1265 return Res.getValue(1);
1266}
1267
1268SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
1269 SDValue LHS = GetPromotedInteger(N->getOperand(0));
1270 SDValue RHS = N->getOperand(1);
1271 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1272 RHS = ZExtPromotedInteger(RHS);
1273 if (N->getOpcode() != ISD::VP_SHL)
1274 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1275 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1276 N->getOperand(2), N->getOperand(3));
1277}
1278
1279SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
1280 SDValue Op = GetPromotedInteger(N->getOperand(0));
1282 Op.getValueType(), Op, N->getOperand(1));
1283}
1284
1285SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
1286 // The input may have strange things in the top bits of the registers, but
1287 // these operations don't care. They may have weird bits going out, but
1288 // that too is okay if they are integer operations.
1289 SDValue LHS = GetPromotedInteger(N->getOperand(0));
1290 SDValue RHS = GetPromotedInteger(N->getOperand(1));
1291 if (N->getNumOperands() == 2)
1292 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1293 assert(N->getNumOperands() == 4 && "Unexpected number of operands!");
1294 assert(N->isVPOpcode() && "Expected VP opcode");
1295 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1296 N->getOperand(2), N->getOperand(3));
1297}
1298
1299SDValue DAGTypeLegalizer::PromoteIntRes_SExtIntBinOp(SDNode *N) {
1300 // Sign extend the input.
1301 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1302 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1303 if (N->getNumOperands() == 2)
1304 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1305 assert(N->getNumOperands() == 4 && "Unexpected number of operands!");
1306 assert(N->isVPOpcode() && "Expected VP opcode");
1307 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1308 N->getOperand(2), N->getOperand(3));
1309}
1310
1311SDValue DAGTypeLegalizer::PromoteIntRes_ZExtIntBinOp(SDNode *N) {
1312 // Zero extend the input.
1313 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1314 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
1315 if (N->getNumOperands() == 2)
1316 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1317 assert(N->getNumOperands() == 4 && "Unexpected number of operands!");
1318 assert(N->isVPOpcode() && "Expected VP opcode");
1319 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1320 N->getOperand(2), N->getOperand(3));
1321}
1322
1323SDValue DAGTypeLegalizer::PromoteIntRes_UMINUMAX(SDNode *N) {
1324 // It doesn't matter if we sign extend or zero extend in the inputs. So do
1325 // whatever is best for the target.
1326 SDValue LHS = SExtOrZExtPromotedInteger(N->getOperand(0));
1327 SDValue RHS = SExtOrZExtPromotedInteger(N->getOperand(1));
1328 return DAG.getNode(N->getOpcode(), SDLoc(N),
1329 LHS.getValueType(), LHS, RHS);
1330}
1331
1332SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
1333 // The input value must be properly sign extended.
1334 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1335 SDValue RHS = N->getOperand(1);
1336 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1337 RHS = ZExtPromotedInteger(RHS);
1338 if (N->getOpcode() != ISD::VP_ASHR)
1339 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1340 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1341 N->getOperand(2), N->getOperand(3));
1342}
1343
1344SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
1345 // The input value must be properly zero extended.
1346 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1347 SDValue RHS = N->getOperand(1);
1348 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1349 RHS = ZExtPromotedInteger(RHS);
1350 if (N->getOpcode() != ISD::VP_LSHR)
1351 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1352 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1353 N->getOperand(2), N->getOperand(3));
1354}
1355
1356SDValue DAGTypeLegalizer::PromoteIntRes_Rotate(SDNode *N) {
1357 // Lower the rotate to shifts and ORs which can be promoted.
1358 SDValue Res = TLI.expandROT(N, true /*AllowVectorOps*/, DAG);
1359 ReplaceValueWith(SDValue(N, 0), Res);
1360 return SDValue();
1361}
1362
1363SDValue DAGTypeLegalizer::PromoteIntRes_FunnelShift(SDNode *N) {
1364 SDValue Hi = GetPromotedInteger(N->getOperand(0));
1365 SDValue Lo = GetPromotedInteger(N->getOperand(1));
1366 SDValue Amt = N->getOperand(2);
1367 if (getTypeAction(Amt.getValueType()) == TargetLowering::TypePromoteInteger)
1368 Amt = ZExtPromotedInteger(Amt);
1369 EVT AmtVT = Amt.getValueType();
1370
1371 SDLoc DL(N);
1372 EVT OldVT = N->getOperand(0).getValueType();
1373 EVT VT = Lo.getValueType();
1374 unsigned Opcode = N->getOpcode();
1375 bool IsFSHR = Opcode == ISD::FSHR;
1376 unsigned OldBits = OldVT.getScalarSizeInBits();
1377 unsigned NewBits = VT.getScalarSizeInBits();
1378
1379 // Amount has to be interpreted modulo the old bit width.
1380 Amt = DAG.getNode(ISD::UREM, DL, AmtVT, Amt,
1381 DAG.getConstant(OldBits, DL, AmtVT));
1382
1383 // If the promoted type is twice the size (or more), then we use the
1384 // traditional funnel 'double' shift codegen. This isn't necessary if the
1385 // shift amount is constant.
1386 // fshl(x,y,z) -> (((aext(x) << bw) | zext(y)) << (z % bw)) >> bw.
1387 // fshr(x,y,z) -> (((aext(x) << bw) | zext(y)) >> (z % bw)).
1388 if (NewBits >= (2 * OldBits) && !isa<ConstantSDNode>(Amt) &&
1389 !TLI.isOperationLegalOrCustom(Opcode, VT)) {
1390 SDValue HiShift = DAG.getConstant(OldBits, DL, VT);
1391 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, HiShift);
1392 Lo = DAG.getZeroExtendInReg(Lo, DL, OldVT);
1393 SDValue Res = DAG.getNode(ISD::OR, DL, VT, Hi, Lo);
1394 Res = DAG.getNode(IsFSHR ? ISD::SRL : ISD::SHL, DL, VT, Res, Amt);
1395 if (!IsFSHR)
1396 Res = DAG.getNode(ISD::SRL, DL, VT, Res, HiShift);
1397 return Res;
1398 }
1399
1400 // Shift Lo up to occupy the upper bits of the promoted type.
1401 SDValue ShiftOffset = DAG.getConstant(NewBits - OldBits, DL, AmtVT);
1402 Lo = DAG.getNode(ISD::SHL, DL, VT, Lo, ShiftOffset);
1403
1404 // Increase Amount to shift the result into the lower bits of the promoted
1405 // type.
1406 if (IsFSHR)
1407 Amt = DAG.getNode(ISD::ADD, DL, AmtVT, Amt, ShiftOffset);
1408
1409 return DAG.getNode(Opcode, DL, VT, Hi, Lo, Amt);
1410}
1411
1412// A vp version of PromoteIntRes_FunnelShift.
1413SDValue DAGTypeLegalizer::PromoteIntRes_VPFunnelShift(SDNode *N) {
1414 SDValue Hi = GetPromotedInteger(N->getOperand(0));
1415 SDValue Lo = GetPromotedInteger(N->getOperand(1));
1416 SDValue Amt = N->getOperand(2);
1417 SDValue Mask = N->getOperand(3);
1418 SDValue EVL = N->getOperand(4);
1419 if (getTypeAction(Amt.getValueType()) == TargetLowering::TypePromoteInteger)
1420 Amt = ZExtPromotedInteger(Amt);
1421 EVT AmtVT = Amt.getValueType();
1422
1423 SDLoc DL(N);
1424 EVT OldVT = N->getOperand(0).getValueType();
1425 EVT VT = Lo.getValueType();
1426 unsigned Opcode = N->getOpcode();
1427 bool IsFSHR = Opcode == ISD::VP_FSHR;
1428 unsigned OldBits = OldVT.getScalarSizeInBits();
1429 unsigned NewBits = VT.getScalarSizeInBits();
1430
1431 // Amount has to be interpreted modulo the old bit width.
1432 Amt = DAG.getNode(ISD::VP_UREM, DL, AmtVT, Amt,
1433 DAG.getConstant(OldBits, DL, AmtVT), Mask, EVL);
1434
1435 // If the promoted type is twice the size (or more), then we use the
1436 // traditional funnel 'double' shift codegen. This isn't necessary if the
1437 // shift amount is constant.
1438 // fshl(x,y,z) -> (((aext(x) << bw) | zext(y)) << (z % bw)) >> bw.
1439 // fshr(x,y,z) -> (((aext(x) << bw) | zext(y)) >> (z % bw)).
1440 if (NewBits >= (2 * OldBits) && !isa<ConstantSDNode>(Amt) &&
1441 !TLI.isOperationLegalOrCustom(Opcode, VT)) {
1442 SDValue HiShift = DAG.getConstant(OldBits, DL, VT);
1443 Hi = DAG.getNode(ISD::VP_SHL, DL, VT, Hi, HiShift, Mask, EVL);
1444 // FIXME: Replace it by vp operations.
1445 Lo = DAG.getZeroExtendInReg(Lo, DL, OldVT);
1446 SDValue Res = DAG.getNode(ISD::VP_OR, DL, VT, Hi, Lo, Mask, EVL);
1447 Res = DAG.getNode(IsFSHR ? ISD::VP_LSHR : ISD::VP_SHL, DL, VT, Res, Amt,
1448 Mask, EVL);
1449 if (!IsFSHR)
1450 Res = DAG.getNode(ISD::VP_LSHR, DL, VT, Res, HiShift, Mask, EVL);
1451 return Res;
1452 }
1453
1454 // Shift Lo up to occupy the upper bits of the promoted type.
1455 SDValue ShiftOffset = DAG.getConstant(NewBits - OldBits, DL, AmtVT);
1456 Lo = DAG.getNode(ISD::VP_SHL, DL, VT, Lo, ShiftOffset, Mask, EVL);
1457
1458 // Increase Amount to shift the result into the lower bits of the promoted
1459 // type.
1460 if (IsFSHR)
1461 Amt = DAG.getNode(ISD::VP_ADD, DL, AmtVT, Amt, ShiftOffset, Mask, EVL);
1462
1463 return DAG.getNode(Opcode, DL, VT, Hi, Lo, Amt, Mask, EVL);
1464}
1465
1466SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
1467 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1468 SDValue Res;
1469 SDValue InOp = N->getOperand(0);
1470 SDLoc dl(N);
1471
1472 switch (getTypeAction(InOp.getValueType())) {
1473 default: llvm_unreachable("Unknown type action!");
1476 Res = InOp;
1477 break;
1479 Res = GetPromotedInteger(InOp);
1480 break;
1482 EVT InVT = InOp.getValueType();
1483 assert(InVT.isVector() && "Cannot split scalar types");
1484 ElementCount NumElts = InVT.getVectorElementCount();
1485 assert(NumElts == NVT.getVectorElementCount() &&
1486 "Dst and Src must have the same number of elements");
1488 "Promoted vector type must be a power of two");
1489
1490 SDValue EOp1, EOp2;
1491 GetSplitVector(InOp, EOp1, EOp2);
1492
1493 EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(),
1494 NumElts.divideCoefficientBy(2));
1495 if (N->getOpcode() == ISD::TRUNCATE) {
1496 EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1);
1497 EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2);
1498 } else {
1499 assert(N->getOpcode() == ISD::VP_TRUNCATE &&
1500 "Expected VP_TRUNCATE opcode");
1501 SDValue MaskLo, MaskHi, EVLLo, EVLHi;
1502 std::tie(MaskLo, MaskHi) = SplitMask(N->getOperand(1));
1503 std::tie(EVLLo, EVLHi) =
1504 DAG.SplitEVL(N->getOperand(2), N->getValueType(0), dl);
1505 EOp1 = DAG.getNode(ISD::VP_TRUNCATE, dl, HalfNVT, EOp1, MaskLo, EVLLo);
1506 EOp2 = DAG.getNode(ISD::VP_TRUNCATE, dl, HalfNVT, EOp2, MaskHi, EVLHi);
1507 }
1508 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2);
1509 }
1510 // TODO: VP_TRUNCATE need to handle when TypeWidenVector access to some
1511 // targets.
1513 SDValue WideInOp = GetWidenedVector(InOp);
1514
1515 // Truncate widened InOp.
1516 unsigned NumElem = WideInOp.getValueType().getVectorNumElements();
1517 EVT TruncVT = EVT::getVectorVT(*DAG.getContext(),
1518 N->getValueType(0).getScalarType(), NumElem);
1519 SDValue WideTrunc = DAG.getNode(ISD::TRUNCATE, dl, TruncVT, WideInOp);
1520
1521 // Zero extend so that the elements are of same type as those of NVT
1523 NumElem);
1524 SDValue WideExt = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtVT, WideTrunc);
1525
1526 // Extract the low NVT subvector.
1527 SDValue ZeroIdx = DAG.getVectorIdxConstant(0, dl);
1528 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, WideExt, ZeroIdx);
1529 }
1530 }
1531
1532 // Truncate to NVT instead of VT
1533 if (N->getOpcode() == ISD::VP_TRUNCATE)
1534 return DAG.getNode(ISD::VP_TRUNCATE, dl, NVT, Res, N->getOperand(1),
1535 N->getOperand(2));
1536 return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res);
1537}
1538
1539SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
1540 if (ResNo == 1)
1541 return PromoteIntRes_Overflow(N);
1542
1543 // The operation overflowed iff the result in the larger type is not the
1544 // zero extension of its truncation to the original type.
1545 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1546 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
1547 EVT OVT = N->getOperand(0).getValueType();
1548 EVT NVT = LHS.getValueType();
1549 SDLoc dl(N);
1550
1551 // Do the arithmetic in the larger type.
1552 unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
1553 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
1554
1555 // Calculate the overflow flag: zero extend the arithmetic result from
1556 // the original type.
1557 SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT);
1558 // Overflowed if and only if this is not equal to Res.
1559 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
1560
1561 // Use the calculated overflow everywhere.
1562 ReplaceValueWith(SDValue(N, 1), Ofl);
1563
1564 return Res;
1565}
1566
1567// Handle promotion for the ADDE/SUBE/UADDO_CARRY/USUBO_CARRY nodes. Notice that
1568// the third operand of ADDE/SUBE nodes is carry flag, which differs from
1569// the UADDO_CARRY/USUBO_CARRY nodes in that the third operand is carry Boolean.
1570SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO_CARRY(SDNode *N,
1571 unsigned ResNo) {
1572 if (ResNo == 1)
1573 return PromoteIntRes_Overflow(N);
1574
1575 // We need to sign-extend the operands so the carry value computed by the
1576 // wide operation will be equivalent to the carry value computed by the
1577 // narrow operation.
1578 // An UADDO_CARRY can generate carry only if any of the operands has its
1579 // most significant bit set. Sign extension propagates the most significant
1580 // bit into the higher bits which means the extra bit that the narrow
1581 // addition would need (i.e. the carry) will be propagated through the higher
1582 // bits of the wide addition.
1583 // A USUBO_CARRY can generate borrow only if LHS < RHS and this property will
1584 // be preserved by sign extension.
1585 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1586 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1587
1588 EVT ValueVTs[] = {LHS.getValueType(), N->getValueType(1)};
1589
1590 // Do the arithmetic in the wide type.
1591 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N), DAG.getVTList(ValueVTs),
1592 LHS, RHS, N->getOperand(2));
1593
1594 // Update the users of the original carry/borrow value.
1595 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1596
1597 return SDValue(Res.getNode(), 0);
1598}
1599
1600SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO_CARRY(SDNode *N,
1601 unsigned ResNo) {
1602 assert(ResNo == 1 && "Don't know how to promote other results yet.");
1603 return PromoteIntRes_Overflow(N);
1604}
1605
1606SDValue DAGTypeLegalizer::PromoteIntRes_ABS(SDNode *N) {
1607 EVT OVT = N->getValueType(0);
1608 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
1609
1610 // If a larger ABS or SMAX isn't supported by the target, try to expand now.
1611 // If we expand later we'll end up sign extending more than just the sra input
1612 // in sra+xor+sub expansion.
1613 if (!OVT.isVector() &&
1615 !TLI.isOperationLegal(ISD::SMAX, NVT)) {
1616 if (SDValue Res = TLI.expandABS(N, DAG))
1617 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), NVT, Res);
1618 }
1619
1620 SDValue Op0 = SExtPromotedInteger(N->getOperand(0));
1621 return DAG.getNode(ISD::ABS, SDLoc(N), Op0.getValueType(), Op0);
1622}
1623
1624SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
1625 // Promote the overflow bit trivially.
1626 if (ResNo == 1)
1627 return PromoteIntRes_Overflow(N);
1628
1629 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
1630 SDLoc DL(N);
1631 EVT SmallVT = LHS.getValueType();
1632
1633 // To determine if the result overflowed in a larger type, we extend the
1634 // input to the larger type, do the multiply (checking if it overflows),
1635 // then also check the high bits of the result to see if overflow happened
1636 // there.
1637 if (N->getOpcode() == ISD::SMULO) {
1638 LHS = SExtPromotedInteger(LHS);
1639 RHS = SExtPromotedInteger(RHS);
1640 } else {
1641 LHS = ZExtPromotedInteger(LHS);
1642 RHS = ZExtPromotedInteger(RHS);
1643 }
1644 SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1));
1645 SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS);
1646
1647 // Overflow occurred if it occurred in the larger type, or if the high part
1648 // of the result does not zero/sign-extend the low part. Check this second
1649 // possibility first.
1650 SDValue Overflow;
1651 if (N->getOpcode() == ISD::UMULO) {
1652 // Unsigned overflow occurred if the high part is non-zero.
1653 unsigned Shift = SmallVT.getScalarSizeInBits();
1654 SDValue Hi =
1655 DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
1656 DAG.getShiftAmountConstant(Shift, Mul.getValueType(), DL));
1657 Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
1658 DAG.getConstant(0, DL, Hi.getValueType()),
1659 ISD::SETNE);
1660 } else {
1661 // Signed overflow occurred if the high part does not sign extend the low.
1662 SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
1663 Mul, DAG.getValueType(SmallVT));
1664 Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
1665 }
1666
1667 // The only other way for overflow to occur is if the multiplication in the
1668 // larger type itself overflowed.
1669 Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow,
1670 SDValue(Mul.getNode(), 1));
1671
1672 // Use the calculated overflow everywhere.
1673 ReplaceValueWith(SDValue(N, 1), Overflow);
1674 return Mul;
1675}
1676
1677SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
1678 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
1679 N->getValueType(0)));
1680}
1681
1682SDValue DAGTypeLegalizer::PromoteIntRes_VSCALE(SDNode *N) {
1683 EVT VT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1684
1685 const APInt &MulImm = N->getConstantOperandAPInt(0);
1686 return DAG.getVScale(SDLoc(N), VT, MulImm.sext(VT.getSizeInBits()));
1687}
1688
1689SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
1690 SDValue Chain = N->getOperand(0); // Get the chain.
1691 SDValue Ptr = N->getOperand(1); // Get the pointer.
1692 EVT VT = N->getValueType(0);
1693 SDLoc dl(N);
1694
1695 MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
1696 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
1697 // The argument is passed as NumRegs registers of type RegVT.
1698
1699 SmallVector<SDValue, 8> Parts(NumRegs);
1700 for (unsigned i = 0; i < NumRegs; ++i) {
1701 Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
1702 N->getConstantOperandVal(3));
1703 Chain = Parts[i].getValue(1);
1704 }
1705
1706 // Handle endianness of the load.
1707 if (DAG.getDataLayout().isBigEndian())
1708 std::reverse(Parts.begin(), Parts.end());
1709
1710 // Assemble the parts in the promoted type.
1711 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1712 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
1713 for (unsigned i = 1; i < NumRegs; ++i) {
1714 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
1715 // Shift it to the right position and "or" it in.
1716 Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
1717 DAG.getConstant(i * RegVT.getSizeInBits(), dl,
1718 TLI.getPointerTy(DAG.getDataLayout())));
1719 Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
1720 }
1721
1722 // Modified the chain result - switch anything that used the old chain to
1723 // use the new one.
1724 ReplaceValueWith(SDValue(N, 1), Chain);
1725
1726 return Res;
1727}
1728
1729//===----------------------------------------------------------------------===//
1730// Integer Operand Promotion
1731//===----------------------------------------------------------------------===//
1732
1733/// PromoteIntegerOperand - This method is called when the specified operand of
1734/// the specified node is found to need promotion. At this point, all of the
1735/// result types of the node are known to be legal, but other operands of the
1736/// node may need promotion or expansion as well as the specified one.
1737bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
1738 LLVM_DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG);
1739 dbgs() << "\n");
1740 SDValue Res = SDValue();
1741 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) {
1742 LLVM_DEBUG(dbgs() << "Node has been custom lowered, done\n");
1743 return false;
1744 }
1745
1746 switch (N->getOpcode()) {
1747 default:
1748 #ifndef NDEBUG
1749 dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
1750 N->dump(&DAG); dbgs() << "\n";
1751 #endif
1752 report_fatal_error("Do not know how to promote this operator's operand!");
1753
1754 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
1755 case ISD::ATOMIC_STORE:
1756 Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
1757 break;
1758 case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break;
1759 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
1760 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
1761 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
1762 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
1763 case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
1764 case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
1766 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);
1767 break;
1768 case ISD::SPLAT_VECTOR:
1770 Res = PromoteIntOp_ScalarOp(N);
1771 break;
1772 case ISD::VSELECT:
1773 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
1774 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
1775 case ISD::VP_SETCC:
1776 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
1777 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
1778 case ISD::VP_SIGN_EXTEND: Res = PromoteIntOp_VP_SIGN_EXTEND(N); break;
1779 case ISD::VP_SINT_TO_FP:
1780 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
1781 case ISD::STRICT_SINT_TO_FP: Res = PromoteIntOp_STRICT_SINT_TO_FP(N); break;
1782 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
1783 OpNo); break;
1784 case ISD::MSTORE: Res = PromoteIntOp_MSTORE(cast<MaskedStoreSDNode>(N),
1785 OpNo); break;
1786 case ISD::MLOAD: Res = PromoteIntOp_MLOAD(cast<MaskedLoadSDNode>(N),
1787 OpNo); break;
1788 case ISD::MGATHER: Res = PromoteIntOp_MGATHER(cast<MaskedGatherSDNode>(N),
1789 OpNo); break;
1790 case ISD::MSCATTER: Res = PromoteIntOp_MSCATTER(cast<MaskedScatterSDNode>(N),
1791 OpNo); break;
1792 case ISD::VP_TRUNCATE:
1793 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
1794 case ISD::BF16_TO_FP:
1795 case ISD::FP16_TO_FP:
1796 case ISD::VP_UINT_TO_FP:
1797 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
1798 case ISD::STRICT_UINT_TO_FP: Res = PromoteIntOp_STRICT_UINT_TO_FP(N); break;
1799 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
1800 case ISD::VP_ZERO_EXTEND: Res = PromoteIntOp_VP_ZERO_EXTEND(N); break;
1801 case ISD::EXTRACT_SUBVECTOR: Res = PromoteIntOp_EXTRACT_SUBVECTOR(N); break;
1802 case ISD::INSERT_SUBVECTOR: Res = PromoteIntOp_INSERT_SUBVECTOR(N); break;
1803
1804 case ISD::SHL:
1805 case ISD::SRA:
1806 case ISD::SRL:
1807 case ISD::ROTL:
1808 case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
1809
1810 case ISD::FSHL:
1811 case ISD::FSHR: Res = PromoteIntOp_FunnelShift(N); break;
1812
1813 case ISD::SADDO_CARRY:
1814 case ISD::SSUBO_CARRY:
1815 case ISD::UADDO_CARRY:
1816 case ISD::USUBO_CARRY: Res = PromoteIntOp_ADDSUBO_CARRY(N, OpNo); break;
1817
1818 case ISD::FRAMEADDR:
1819 case ISD::RETURNADDR: Res = PromoteIntOp_FRAMERETURNADDR(N); break;
1820
1821 case ISD::SMULFIX:
1822 case ISD::SMULFIXSAT:
1823 case ISD::UMULFIX:
1824 case ISD::UMULFIXSAT:
1825 case ISD::SDIVFIX:
1826 case ISD::SDIVFIXSAT:
1827 case ISD::UDIVFIX:
1828 case ISD::UDIVFIXSAT: Res = PromoteIntOp_FIX(N); break;
1829 case ISD::FPOWI:
1830 case ISD::STRICT_FPOWI:
1831 case ISD::FLDEXP:
1832 case ISD::STRICT_FLDEXP: Res = PromoteIntOp_ExpOp(N); break;
1833 case ISD::VECREDUCE_ADD:
1834 case ISD::VECREDUCE_MUL:
1835 case ISD::VECREDUCE_AND:
1836 case ISD::VECREDUCE_OR:
1837 case ISD::VECREDUCE_XOR:
1841 case ISD::VECREDUCE_UMIN: Res = PromoteIntOp_VECREDUCE(N); break;
1842 case ISD::VP_REDUCE_ADD:
1843 case ISD::VP_REDUCE_MUL:
1844 case ISD::VP_REDUCE_AND:
1845 case ISD::VP_REDUCE_OR:
1846 case ISD::VP_REDUCE_XOR:
1847 case ISD::VP_REDUCE_SMAX:
1848 case ISD::VP_REDUCE_SMIN:
1849 case ISD::VP_REDUCE_UMAX:
1850 case ISD::VP_REDUCE_UMIN:
1851 Res = PromoteIntOp_VP_REDUCE(N, OpNo);
1852 break;
1853
1854 case ISD::SET_ROUNDING: Res = PromoteIntOp_SET_ROUNDING(N); break;
1855 case ISD::STACKMAP:
1856 Res = PromoteIntOp_STACKMAP(N, OpNo);
1857 break;
1858 case ISD::PATCHPOINT:
1859 Res = PromoteIntOp_PATCHPOINT(N, OpNo);
1860 break;
1861 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
1862 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
1863 Res = PromoteIntOp_VP_STRIDED(N, OpNo);
1864 break;
1865 }
1866
1867 // If the result is null, the sub-method took care of registering results etc.
1868 if (!Res.getNode()) return false;
1869
1870 // If the result is N, the sub-method updated N in place. Tell the legalizer
1871 // core about this.
1872 if (Res.getNode() == N)
1873 return true;
1874
1875 const bool IsStrictFp = N->isStrictFPOpcode();
1876 assert(Res.getValueType() == N->getValueType(0) &&
1877 N->getNumValues() == (IsStrictFp ? 2 : 1) &&
1878 "Invalid operand expansion");
1879 LLVM_DEBUG(dbgs() << "Replacing: "; N->dump(&DAG); dbgs() << " with: ";
1880 Res.dump());
1881
1882 ReplaceValueWith(SDValue(N, 0), Res);
1883 if (IsStrictFp)
1884 ReplaceValueWith(SDValue(N, 1), SDValue(Res.getNode(), 1));
1885
1886 return false;
1887}
1888
1889/// PromoteSetCCOperands - Promote the operands of a comparison. This code is
1890/// shared among BR_CC, SELECT_CC, and SETCC handlers.
1891void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &LHS, SDValue &RHS,
1892 ISD::CondCode CCCode) {
1893 // We have to insert explicit sign or zero extends. Note that we could
1894 // insert sign extends for ALL conditions. For those operations where either
1895 // zero or sign extension would be valid, we ask the target which extension
1896 // it would prefer.
1897
1898 // Signed comparisons always require sign extension.
1899 if (ISD::isSignedIntSetCC(CCCode)) {
1900 LHS = SExtPromotedInteger(LHS);
1901 RHS = SExtPromotedInteger(RHS);
1902 return;
1903 }
1904
1906 "Unknown integer comparison!");
1907
1908 SDValue OpL = GetPromotedInteger(LHS);
1909 SDValue OpR = GetPromotedInteger(RHS);
1910
1911 if (TLI.isSExtCheaperThanZExt(LHS.getValueType(), OpL.getValueType())) {
1912 // The target would prefer to promote the comparison operand with sign
1913 // extension. Honor that unless the promoted values are already zero
1914 // extended.
1915 unsigned OpLEffectiveBits =
1917 unsigned OpREffectiveBits =
1919 if (OpLEffectiveBits <= LHS.getScalarValueSizeInBits() &&
1920 OpREffectiveBits <= RHS.getScalarValueSizeInBits()) {
1921 LHS = OpL;
1922 RHS = OpR;
1923 return;
1924 }
1925
1926 // The promoted values aren't zero extended, use a sext_inreg.
1927 LHS = SExtPromotedInteger(LHS);
1928 RHS = SExtPromotedInteger(RHS);
1929 return;
1930 }
1931
1932 // Prefer to promote the comparison operand with zero extension.
1933
1934 // If the width of OpL/OpR excluding the duplicated sign bits is no greater
1935 // than the width of LHS/RHS, we can avoid/ inserting a zext_inreg operation
1936 // that we might not be able to remove.
1937 unsigned OpLEffectiveBits = DAG.ComputeMaxSignificantBits(OpL);
1938 unsigned OpREffectiveBits = DAG.ComputeMaxSignificantBits(OpR);
1939 if (OpLEffectiveBits <= LHS.getScalarValueSizeInBits() &&
1940 OpREffectiveBits <= RHS.getScalarValueSizeInBits()) {
1941 LHS = OpL;
1942 RHS = OpR;
1943 return;
1944 }
1945
1946 // Otherwise, use zext_inreg.
1947 LHS = ZExtPromotedInteger(LHS);
1948 RHS = ZExtPromotedInteger(RHS);
1949}
1950
1951SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
1952 SDValue Op = GetPromotedInteger(N->getOperand(0));
1953 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op);
1954}
1955
1956SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
1957 SDValue Op1 = GetPromotedInteger(N->getOperand(1));
1958 return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(),
1959 N->getChain(), Op1, N->getBasePtr(), N->getMemOperand());
1960}
1961
1962SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
1963 // This should only occur in unusual situations like bitcasting to an
1964 // x86_fp80, so just turn it into a store+load
1965 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
1966}
1967
1968SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
1969 assert(OpNo == 2 && "Don't know how to promote this operand!");
1970
1971 SDValue LHS = N->getOperand(2);
1972 SDValue RHS = N->getOperand(3);
1973 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
1974
1975 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
1976 // legal types.
1977 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1978 N->getOperand(1), LHS, RHS, N->getOperand(4)),
1979 0);
1980}
1981
1982SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
1983 assert(OpNo == 1 && "only know how to promote condition");
1984
1985 // Promote all the way up to the canonical SetCC type.
1986 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
1987
1988 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
1989 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
1990 N->getOperand(2)), 0);
1991}
1992
1993SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
1994 // Since the result type is legal, the operands must promote to it.
1995 EVT OVT = N->getOperand(0).getValueType();
1996 SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
1997 SDValue Hi = GetPromotedInteger(N->getOperand(1));
1998 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
1999 SDLoc dl(N);
2000
2001 Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
2002 DAG.getConstant(OVT.getSizeInBits(), dl,
2003 TLI.getPointerTy(DAG.getDataLayout())));
2004 return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
2005}
2006
2007SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
2008 // The vector type is legal but the element type is not. This implies
2009 // that the vector is a power-of-two in length and that the element
2010 // type does not have a strange size (eg: it is not i1).
2011 EVT VecVT = N->getValueType(0);
2012 unsigned NumElts = VecVT.getVectorNumElements();
2013 assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) &&
2014 "Legal vector of one illegal element?");
2015
2016 // Promote the inserted value. The type does not need to match the
2017 // vector element type. Check that any extra bits introduced will be
2018 // truncated away.
2019 assert(N->getOperand(0).getValueSizeInBits() >=
2020 N->getValueType(0).getScalarSizeInBits() &&
2021 "Type of inserted value narrower than vector element type!");
2022
2024 for (unsigned i = 0; i < NumElts; ++i)
2025 NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
2026
2027 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2028}
2029
2030SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
2031 unsigned OpNo) {
2032 if (OpNo == 1) {
2033 // Promote the inserted value. This is valid because the type does not
2034 // have to match the vector element type.
2035
2036 // Check that any extra bits introduced will be truncated away.
2037 assert(N->getOperand(1).getValueSizeInBits() >=
2038 N->getValueType(0).getScalarSizeInBits() &&
2039 "Type of inserted value narrower than vector element type!");
2040 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2041 GetPromotedInteger(N->getOperand(1)),
2042 N->getOperand(2)),
2043 0);
2044 }
2045
2046 assert(OpNo == 2 && "Different operand and result vector types?");
2047
2048 // Promote the index.
2049 SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N),
2050 TLI.getVectorIdxTy(DAG.getDataLayout()));
2051 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2052 N->getOperand(1), Idx), 0);
2053}
2054
2055SDValue DAGTypeLegalizer::PromoteIntOp_ScalarOp(SDNode *N) {
2056 // Integer SPLAT_VECTOR/SCALAR_TO_VECTOR operands are implicitly truncated,
2057 // so just promote the operand in place.
2058 return SDValue(DAG.UpdateNodeOperands(N,
2059 GetPromotedInteger(N->getOperand(0))), 0);
2060}
2061
2062SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
2063 assert(OpNo == 0 && "Only know how to promote the condition!");
2064 SDValue Cond = N->getOperand(0);
2065 EVT OpTy = N->getOperand(1).getValueType();
2066
2067 if (N->getOpcode() == ISD::VSELECT)
2068 if (SDValue Res = WidenVSELECTMask(N))
2069 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
2070 Res, N->getOperand(1), N->getOperand(2));
2071
2072 // Promote all the way up to the canonical SetCC type.
2073 EVT OpVT = N->getOpcode() == ISD::SELECT ? OpTy.getScalarType() : OpTy;
2074 Cond = PromoteTargetBoolean(Cond, OpVT);
2075
2076 return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1),
2077 N->getOperand(2)), 0);
2078}
2079
2080SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
2081 assert(OpNo == 0 && "Don't know how to promote this operand!");
2082
2083 SDValue LHS = N->getOperand(0);
2084 SDValue RHS = N->getOperand(1);
2085 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
2086
2087 // The CC (#4) and the possible return values (#2 and #3) have legal types.
2088 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
2089 N->getOperand(3), N->getOperand(4)), 0);
2090}
2091
2092SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
2093 assert(OpNo == 0 && "Don't know how to promote this operand!");
2094
2095 SDValue LHS = N->getOperand(0);
2096 SDValue RHS = N->getOperand(1);
2097 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
2098
2099 // The CC (#2) is always legal.
2100 if (N->getOpcode() == ISD::SETCC)
2101 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
2102
2103 assert(N->getOpcode() == ISD::VP_SETCC && "Expected VP_SETCC opcode");
2104
2105 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
2106 N->getOperand(3), N->getOperand(4)),
2107 0);
2108}
2109
2110SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
2111 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2112 ZExtPromotedInteger(N->getOperand(1))), 0);
2113}
2114
2115SDValue DAGTypeLegalizer::PromoteIntOp_FunnelShift(SDNode *N) {
2116 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1),
2117 ZExtPromotedInteger(N->getOperand(2))), 0);
2118}
2119
2120SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
2121 SDValue Op = GetPromotedInteger(N->getOperand(0));
2122 SDLoc dl(N);
2123 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
2124 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
2125 Op, DAG.getValueType(N->getOperand(0).getValueType()));
2126}
2127
2128SDValue DAGTypeLegalizer::PromoteIntOp_VP_SIGN_EXTEND(SDNode *N) {
2129 SDLoc dl(N);
2130 EVT VT = N->getValueType(0);
2131 SDValue Op = GetPromotedInteger(N->getOperand(0));
2132 // FIXME: There is no VP_ANY_EXTEND yet.
2133 Op = DAG.getNode(ISD::VP_ZERO_EXTEND, dl, VT, Op, N->getOperand(1),
2134 N->getOperand(2));
2135 unsigned Diff =
2136 VT.getScalarSizeInBits() - N->getOperand(0).getScalarValueSizeInBits();
2137 SDValue ShAmt = DAG.getShiftAmountConstant(Diff, VT, dl);
2138 // FIXME: There is no VP_SIGN_EXTEND_INREG so use a pair of shifts.
2139 SDValue Shl = DAG.getNode(ISD::VP_SHL, dl, VT, Op, ShAmt, N->getOperand(1),
2140 N->getOperand(2));
2141 return DAG.getNode(ISD::VP_ASHR, dl, VT, Shl, ShAmt, N->getOperand(1),
2142 N->getOperand(2));
2143}
2144
2145SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
2146 if (N->getOpcode() == ISD::VP_SINT_TO_FP)
2147 return SDValue(DAG.UpdateNodeOperands(N,
2148 SExtPromotedInteger(N->getOperand(0)),
2149 N->getOperand(1), N->getOperand(2)),
2150 0);
2151 return SDValue(DAG.UpdateNodeOperands(N,
2152 SExtPromotedInteger(N->getOperand(0))), 0);
2153}
2154
2155SDValue DAGTypeLegalizer::PromoteIntOp_STRICT_SINT_TO_FP(SDNode *N) {
2156 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2157 SExtPromotedInteger(N->getOperand(1))), 0);
2158}
2159
2160SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
2161 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2162 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
2163 SDLoc dl(N);
2164
2165 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
2166
2167 // Truncate the value and store the result.
2168 return DAG.getTruncStore(Ch, dl, Val, Ptr,
2169 N->getMemoryVT(), N->getMemOperand());
2170}
2171
2172SDValue DAGTypeLegalizer::PromoteIntOp_MSTORE(MaskedStoreSDNode *N,
2173 unsigned OpNo) {
2174 SDValue DataOp = N->getValue();
2175 SDValue Mask = N->getMask();
2176
2177 if (OpNo == 4) {
2178 // The Mask. Update in place.
2179 EVT DataVT = DataOp.getValueType();
2180 Mask = PromoteTargetBoolean(Mask, DataVT);
2181 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
2182 NewOps[4] = Mask;
2183 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2184 }
2185
2186 assert(OpNo == 1 && "Unexpected operand for promotion");
2187 DataOp = GetPromotedInteger(DataOp);
2188
2189 return DAG.getMaskedStore(N->getChain(), SDLoc(N), DataOp, N->getBasePtr(),
2190 N->getOffset(), Mask, N->getMemoryVT(),
2191 N->getMemOperand(), N->getAddressingMode(),
2192 /*IsTruncating*/ true, N->isCompressingStore());
2193}
2194
2195SDValue DAGTypeLegalizer::PromoteIntOp_MLOAD(MaskedLoadSDNode *N,
2196 unsigned OpNo) {
2197 assert(OpNo == 3 && "Only know how to promote the mask!");
2198 EVT DataVT = N->getValueType(0);
2199 SDValue Mask = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
2200 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
2201 NewOps[OpNo] = Mask;
2202 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
2203 if (Res == N)
2204 return SDValue(Res, 0);
2205
2206 // Update triggered CSE, do our own replacement since caller can't.
2207 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
2208 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
2209 return SDValue();
2210}
2211
2212SDValue DAGTypeLegalizer::PromoteIntOp_MGATHER(MaskedGatherSDNode *N,
2213 unsigned OpNo) {
2214 SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
2215
2216 if (OpNo == 2) {
2217 // The Mask
2218 EVT DataVT = N->getValueType(0);
2219 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
2220 } else if (OpNo == 4) {
2221 // The Index
2222 if (N->isIndexSigned())
2223 // Need to sign extend the index since the bits will likely be used.
2224 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
2225 else
2226 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
2227 } else
2228 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
2229
2230 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
2231 if (Res == N)
2232 return SDValue(Res, 0);
2233
2234 // Update triggered CSE, do our own replacement since caller can't.
2235 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
2236 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
2237 return SDValue();
2238}
2239
2240SDValue DAGTypeLegalizer::PromoteIntOp_MSCATTER(MaskedScatterSDNode *N,
2241 unsigned OpNo) {
2242 bool TruncateStore = N->isTruncatingStore();
2243 SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
2244
2245 if (OpNo == 2) {
2246 // The Mask
2247 EVT DataVT = N->getValue().getValueType();
2248 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
2249 } else if (OpNo == 4) {
2250 // The Index
2251 if (N->isIndexSigned())
2252 // Need to sign extend the index since the bits will likely be used.
2253 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
2254 else
2255 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
2256 } else {
2257 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
2258 TruncateStore = true;
2259 }
2260
2261 return DAG.getMaskedScatter(DAG.getVTList(MVT::Other), N->getMemoryVT(),
2262 SDLoc(N), NewOps, N->getMemOperand(),
2263 N->getIndexType(), TruncateStore);
2264}
2265
2266SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
2267 SDValue Op = GetPromotedInteger(N->getOperand(0));
2268 if (N->getOpcode() == ISD::VP_TRUNCATE)
2269 return DAG.getNode(ISD::VP_TRUNCATE, SDLoc(N), N->getValueType(0), Op,
2270 N->getOperand(1), N->getOperand(2));
2271 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op);
2272}
2273
2274SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
2275 if (N->getOpcode() == ISD::VP_UINT_TO_FP)
2276 return SDValue(DAG.UpdateNodeOperands(N,
2277 ZExtPromotedInteger(N->getOperand(0)),
2278 N->getOperand(1), N->getOperand(2)),
2279 0);
2280 return SDValue(DAG.UpdateNodeOperands(N,
2281 ZExtPromotedInteger(N->getOperand(0))), 0);
2282}
2283
2284SDValue DAGTypeLegalizer::PromoteIntOp_STRICT_UINT_TO_FP(SDNode *N) {
2285 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2286 ZExtPromotedInteger(N->getOperand(1))), 0);
2287}
2288
2289SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
2290 SDLoc dl(N);
2291 SDValue Op = GetPromotedInteger(N->getOperand(0));
2292 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
2293 return DAG.getZeroExtendInReg(Op, dl, N->getOperand(0).getValueType());
2294}
2295
2296SDValue DAGTypeLegalizer::PromoteIntOp_VP_ZERO_EXTEND(SDNode *N) {
2297 SDLoc dl(N);
2298 EVT VT = N->getValueType(0);
2299 SDValue Op = GetPromotedInteger(N->getOperand(0));
2300 // FIXME: There is no VP_ANY_EXTEND yet.
2301 Op = DAG.getNode(ISD::VP_ZERO_EXTEND, dl, VT, Op, N->getOperand(1),
2302 N->getOperand(2));
2304 N->getOperand(0).getScalarValueSizeInBits());
2305 return DAG.getNode(ISD::VP_AND, dl, VT, Op, DAG.getConstant(Imm, dl, VT),
2306 N->getOperand(1), N->getOperand(2));
2307}
2308
2309SDValue DAGTypeLegalizer::PromoteIntOp_ADDSUBO_CARRY(SDNode *N, unsigned OpNo) {
2310 assert(OpNo == 2 && "Don't know how to promote this operand!");
2311
2312 SDValue LHS = N->getOperand(0);
2313 SDValue RHS = N->getOperand(1);
2314 SDValue Carry = N->getOperand(2);
2315 SDLoc DL(N);
2316
2317 Carry = PromoteTargetBoolean(Carry, LHS.getValueType());
2318
2319 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, Carry), 0);
2320}
2321
2322SDValue DAGTypeLegalizer::PromoteIntOp_FIX(SDNode *N) {
2323 SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
2324 return SDValue(
2325 DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1), Op2), 0);
2326}
2327
2328SDValue DAGTypeLegalizer::PromoteIntOp_FRAMERETURNADDR(SDNode *N) {
2329 // Promote the RETURNADDR/FRAMEADDR argument to a supported integer width.
2330 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
2331 return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
2332}
2333
2334SDValue DAGTypeLegalizer::PromoteIntOp_ExpOp(SDNode *N) {
2335 bool IsStrict = N->isStrictFPOpcode();
2336 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
2337
2338 bool IsPowI =
2339 N->getOpcode() == ISD::FPOWI || N->getOpcode() == ISD::STRICT_FPOWI;
2340
2341 // The integer operand is the last operand in FPOWI (or FLDEXP) (so the result
2342 // and floating point operand is already type legalized).
2343 RTLIB::Libcall LC = IsPowI ? RTLIB::getPOWI(N->getValueType(0))
2344 : RTLIB::getLDEXP(N->getValueType(0));
2345
2346 if (LC == RTLIB::UNKNOWN_LIBCALL || !TLI.getLibcallName(LC)) {
2347 SDValue Op = SExtPromotedInteger(N->getOperand(1));
2348 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op), 0);
2349 }
2350
2351 // We can't just promote the exponent type in FPOWI, since we want to lower
2352 // the node to a libcall and we if we promote to a type larger than
2353 // sizeof(int) the libcall might not be according to the targets ABI. Instead
2354 // we rewrite to a libcall here directly, letting makeLibCall handle promotion
2355 // if the target accepts it according to shouldSignExtendTypeInLibCall.
2356
2357 unsigned OpOffset = IsStrict ? 1 : 0;
2358 // The exponent should fit in a sizeof(int) type for the libcall to be valid.
2359 assert(DAG.getLibInfo().getIntSize() ==
2360 N->getOperand(1 + OpOffset).getValueType().getSizeInBits() &&
2361 "POWI exponent should match with sizeof(int) when doing the libcall.");
2363 CallOptions.setSExt(true);
2364 SDValue Ops[2] = {N->getOperand(0 + OpOffset), N->getOperand(1 + OpOffset)};
2365 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
2366 DAG, LC, N->getValueType(0), Ops, CallOptions, SDLoc(N), Chain);
2367 ReplaceValueWith(SDValue(N, 0), Tmp.first);
2368 if (IsStrict)
2369 ReplaceValueWith(SDValue(N, 1), Tmp.second);
2370 return SDValue();
2371}
2372
2374 switch (N->getOpcode()) {
2375 default:
2376 llvm_unreachable("Expected integer vector reduction");
2377 case ISD::VECREDUCE_ADD:
2378 case ISD::VECREDUCE_MUL:
2379 case ISD::VECREDUCE_AND:
2380 case ISD::VECREDUCE_OR:
2381 case ISD::VECREDUCE_XOR:
2382 case ISD::VP_REDUCE_ADD:
2383 case ISD::VP_REDUCE_MUL:
2384 case ISD::VP_REDUCE_AND:
2385 case ISD::VP_REDUCE_OR:
2386 case ISD::VP_REDUCE_XOR:
2387 return ISD::ANY_EXTEND;
2390 case ISD::VP_REDUCE_SMAX:
2391 case ISD::VP_REDUCE_SMIN:
2392 return ISD::SIGN_EXTEND;
2395 case ISD::VP_REDUCE_UMAX:
2396 case ISD::VP_REDUCE_UMIN:
2397 return ISD::ZERO_EXTEND;
2398 }
2399}
2400
2401SDValue DAGTypeLegalizer::PromoteIntOpVectorReduction(SDNode *N, SDValue V) {
2402 switch (getExtendForIntVecReduction(N)) {
2403 default:
2404 llvm_unreachable("Impossible extension kind for integer reduction");
2405 case ISD::ANY_EXTEND:
2406 return GetPromotedInteger(V);
2407 case ISD::SIGN_EXTEND:
2408 return SExtPromotedInteger(V);
2409 case ISD::ZERO_EXTEND:
2410 return ZExtPromotedInteger(V);
2411 }
2412}
2413
2414SDValue DAGTypeLegalizer::PromoteIntOp_VECREDUCE(SDNode *N) {
2415 SDLoc dl(N);
2416 SDValue Op = PromoteIntOpVectorReduction(N, N->getOperand(0));
2417
2418 EVT OrigEltVT = N->getOperand(0).getValueType().getVectorElementType();
2419 EVT InVT = Op.getValueType();
2420 EVT EltVT = InVT.getVectorElementType();
2421 EVT ResVT = N->getValueType(0);
2422 unsigned Opcode = N->getOpcode();
2423
2424 // An i1 vecreduce_xor is equivalent to vecreduce_add, use that instead if
2425 // vecreduce_xor is not legal
2426 if (Opcode == ISD::VECREDUCE_XOR && OrigEltVT == MVT::i1 &&
2429 Opcode = ISD::VECREDUCE_ADD;
2430
2431 // An i1 vecreduce_or is equivalent to vecreduce_umax, use that instead if
2432 // vecreduce_or is not legal
2433 else if (Opcode == ISD::VECREDUCE_OR && OrigEltVT == MVT::i1 &&
2436 Opcode = ISD::VECREDUCE_UMAX;
2437 // Can't use promoteTargetBoolean here because we still need
2438 // to either sign_ext or zero_ext in the undefined case.
2439 switch (TLI.getBooleanContents(InVT)) {
2442 Op = ZExtPromotedInteger(N->getOperand(0));
2443 break;
2445 Op = SExtPromotedInteger(N->getOperand(0));
2446 break;
2447 }
2448 }
2449
2450 // An i1 vecreduce_and is equivalent to vecreduce_umin, use that instead if
2451 // vecreduce_and is not legal
2452 else if (Opcode == ISD::VECREDUCE_AND && OrigEltVT == MVT::i1 &&
2455 Opcode = ISD::VECREDUCE_UMIN;
2456 // Can't use promoteTargetBoolean here because we still need
2457 // to either sign_ext or zero_ext in the undefined case.
2458 switch (TLI.getBooleanContents(InVT)) {
2461 Op = ZExtPromotedInteger(N->getOperand(0));
2462 break;
2464 Op = SExtPromotedInteger(N->getOperand(0));
2465 break;
2466 }
2467 }
2468
2469 if (ResVT.bitsGE(EltVT))
2470 return DAG.getNode(Opcode, SDLoc(N), ResVT, Op);
2471
2472 // Result size must be >= element size. If this is not the case after
2473 // promotion, also promote the result type and then truncate.
2474 SDValue Reduce = DAG.getNode(Opcode, dl, EltVT, Op);
2475 return DAG.getNode(ISD::TRUNCATE, dl, ResVT, Reduce);
2476}
2477
2478SDValue DAGTypeLegalizer::PromoteIntOp_VP_REDUCE(SDNode *N, unsigned OpNo) {
2479 SDLoc DL(N);
2480 SDValue Op = N->getOperand(OpNo);
2481 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
2482
2483 if (OpNo == 2) { // Mask
2484 // Update in place.
2485 NewOps[2] = PromoteTargetBoolean(Op, N->getOperand(1).getValueType());
2486 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2487 }
2488
2489 assert(OpNo == 1 && "Unexpected operand for promotion");
2490
2491 Op = PromoteIntOpVectorReduction(N, Op);
2492
2493 NewOps[OpNo] = Op;
2494
2495 EVT VT = N->getValueType(0);
2496 EVT EltVT = Op.getValueType().getScalarType();
2497
2498 if (VT.bitsGE(EltVT))
2499 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, NewOps);
2500
2501 // Result size must be >= element/start-value size. If this is not the case
2502 // after promotion, also promote both the start value and result type and
2503 // then truncate.
2504 NewOps[0] =
2505 DAG.getNode(getExtendForIntVecReduction(N), DL, EltVT, N->getOperand(0));
2506 SDValue Reduce = DAG.getNode(N->getOpcode(), DL, EltVT, NewOps);
2507 return DAG.getNode(ISD::TRUNCATE, DL, VT, Reduce);
2508}
2509
2510SDValue DAGTypeLegalizer::PromoteIntOp_SET_ROUNDING(SDNode *N) {
2511 SDValue Op = ZExtPromotedInteger(N->getOperand(1));
2512 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op), 0);
2513}
2514
2515SDValue DAGTypeLegalizer::PromoteIntOp_STACKMAP(SDNode *N, unsigned OpNo) {
2516 assert(OpNo > 1); // Because the first two arguments are guaranteed legal.
2517 SmallVector<SDValue> NewOps(N->ops().begin(), N->ops().end());
2518 SDValue Operand = N->getOperand(OpNo);
2519 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), Operand.getValueType());
2520 NewOps[OpNo] = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), NVT, Operand);
2521 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2522}
2523
2524SDValue DAGTypeLegalizer::PromoteIntOp_PATCHPOINT(SDNode *N, unsigned OpNo) {
2525 assert(OpNo >= 7);
2526 SmallVector<SDValue> NewOps(N->ops().begin(), N->ops().end());
2527 SDValue Operand = N->getOperand(OpNo);
2528 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), Operand.getValueType());
2529 NewOps[OpNo] = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), NVT, Operand);
2530 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2531}
2532
2533SDValue DAGTypeLegalizer::PromoteIntOp_VP_STRIDED(SDNode *N, unsigned OpNo) {
2534 assert((N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD && OpNo == 3) ||
2535 (N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE && OpNo == 4));
2536
2537 SmallVector<SDValue, 8> NewOps(N->op_begin(), N->op_end());
2538 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
2539
2540 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2541}
2542
2543//===----------------------------------------------------------------------===//
2544// Integer Result Expansion
2545//===----------------------------------------------------------------------===//
2546
2547/// ExpandIntegerResult - This method is called when the specified result of the
2548/// specified node is found to need expansion. At this point, the node may also
2549/// have invalid operands or may have other results that need promotion, we just
2550/// know that (at least) one result needs expansion.
2551void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
2552 LLVM_DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG);
2553 dbgs() << "\n");
2554 SDValue Lo, Hi;
2555 Lo = Hi = SDValue();
2556
2557 // See if the target wants to custom expand this node.
2558 if (CustomLowerNode(N, N->getValueType(ResNo), true))
2559 return;
2560
2561 switch (N->getOpcode()) {
2562 default:
2563#ifndef NDEBUG
2564 dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
2565 N->dump(&DAG); dbgs() << "\n";
2566#endif
2567 report_fatal_error("Do not know how to expand the result of this "
2568 "operator!");
2569
2570 case ISD::ARITH_FENCE: SplitRes_ARITH_FENCE(N, Lo, Hi); break;
2571 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
2572 case ISD::SELECT: SplitRes_Select(N, Lo, Hi); break;
2573 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
2574 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
2575 case ISD::FREEZE: SplitRes_FREEZE(N, Lo, Hi); break;
2576
2577 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
2578 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
2579 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
2580 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
2581 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
2582
2583 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
2584 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
2585 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
2586 case ISD::BITREVERSE: ExpandIntRes_BITREVERSE(N, Lo, Hi); break;
2587 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
2588 case ISD::PARITY: ExpandIntRes_PARITY(N, Lo, Hi); break;
2589 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
2590 case ISD::ABS: ExpandIntRes_ABS(N, Lo, Hi); break;
2592 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
2593 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
2595 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
2596 case ISD::GET_ROUNDING:ExpandIntRes_GET_ROUNDING(N, Lo, Hi); break;
2598 case ISD::FP_TO_SINT:
2600 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_XINT(N, Lo, Hi); break;
2602 case ISD::FP_TO_UINT_SAT: ExpandIntRes_FP_TO_XINT_SAT(N, Lo, Hi); break;
2603 case ISD::STRICT_LROUND:
2604 case ISD::STRICT_LRINT:
2605 case ISD::LROUND:
2606 case ISD::LRINT:
2608 case ISD::STRICT_LLRINT:
2609 case ISD::LLROUND:
2610 case ISD::LLRINT: ExpandIntRes_XROUND_XRINT(N, Lo, Hi); break;
2611 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
2612 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
2613 case ISD::READCYCLECOUNTER: ExpandIntRes_READCYCLECOUNTER(N, Lo, Hi); break;
2614 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
2615 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
2616 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
2617 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
2618 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
2619 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
2620 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
2621 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
2622 case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
2623
2635 case ISD::ATOMIC_SWAP:
2636 case ISD::ATOMIC_CMP_SWAP: {
2637 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
2638 SplitInteger(Tmp.first, Lo, Hi);
2639 ReplaceValueWith(SDValue(N, 1), Tmp.second);
2640 break;
2641 }
2643 AtomicSDNode *AN = cast<AtomicSDNode>(N);
2644 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other);
2645 SDValue Tmp = DAG.getAtomicCmpSwap(
2647 N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3),
2648 AN->getMemOperand());
2649
2650 // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
2651 // success simply by comparing the loaded value against the ingoing
2652 // comparison.
2653 SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp,
2654 N->getOperand(2), ISD::SETEQ);
2655
2656 SplitInteger(Tmp, Lo, Hi);
2657 ReplaceValueWith(SDValue(N, 1), Success);
2658 ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1));
2659 break;
2660 }
2661
2662 case ISD::AND:
2663 case ISD::OR:
2664 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
2665
2666 case ISD::UMAX:
2667 case ISD::SMAX:
2668 case ISD::UMIN:
2669 case ISD::SMIN: ExpandIntRes_MINMAX(N, Lo, Hi); break;
2670
2671 case ISD::ADD:
2672 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
2673
2674 case ISD::ADDC:
2675 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
2676
2677 case ISD::ADDE:
2678 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
2679
2680 case ISD::UADDO_CARRY:
2681 case ISD::USUBO_CARRY: ExpandIntRes_UADDSUBO_CARRY(N, Lo, Hi); break;
2682
2683 case ISD::SADDO_CARRY:
2684 case ISD::SSUBO_CARRY: ExpandIntRes_SADDSUBO_CARRY(N, Lo, Hi); break;
2685
2686 case ISD::SHL:
2687 case ISD::SRA:
2688 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
2689
2690 case ISD::SADDO:
2691 case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
2692 case ISD::UADDO:
2693 case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
2694 case ISD::UMULO:
2695 case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
2696
2697 case ISD::SADDSAT:
2698 case ISD::UADDSAT:
2699 case ISD::SSUBSAT:
2700 case ISD::USUBSAT: ExpandIntRes_ADDSUBSAT(N, Lo, Hi); break;
2701
2702 case ISD::SSHLSAT:
2703 case ISD::USHLSAT: ExpandIntRes_SHLSAT(N, Lo, Hi); break;
2704
2705 case ISD::SMULFIX:
2706 case ISD::SMULFIXSAT:
2707 case ISD::UMULFIX:
2708 case ISD::UMULFIXSAT: ExpandIntRes_MULFIX(N, Lo, Hi); break;
2709
2710 case ISD::SDIVFIX:
2711 case ISD::SDIVFIXSAT:
2712 case ISD::UDIVFIX:
2713 case ISD::UDIVFIXSAT: ExpandIntRes_DIVFIX(N, Lo, Hi); break;
2714
2715 case ISD::VECREDUCE_ADD:
2716 case ISD::VECREDUCE_MUL:
2717 case ISD::VECREDUCE_AND:
2718 case ISD::VECREDUCE_OR:
2719 case ISD::VECREDUCE_XOR:
2723 case ISD::VECREDUCE_UMIN: ExpandIntRes_VECREDUCE(N, Lo, Hi); break;
2724
2725 case ISD::ROTL:
2726 case ISD::ROTR:
2727 ExpandIntRes_Rotate(N, Lo, Hi);
2728 break;
2729
2730 case ISD::FSHL:
2731 case ISD::FSHR:
2732 ExpandIntRes_FunnelShift(N, Lo, Hi);
2733 break;
2734
2735 case ISD::VSCALE:
2736 ExpandIntRes_VSCALE(N, Lo, Hi);
2737 break;
2738 }
2739
2740 // If Lo/Hi is null, the sub-method took care of registering results etc.
2741 if (Lo.getNode())
2742 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
2743}
2744
2745/// Lower an atomic node to the appropriate builtin call.
2746std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
2747 unsigned Opc = Node->getOpcode();
2748 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2749 AtomicOrdering order = cast<AtomicSDNode>(Node)->getMergedOrdering();
2750 // Lower to outline atomic libcall if outline atomics enabled,
2751 // or to sync libcall otherwise
2752 RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, order, VT);
2753 EVT RetVT = Node->getValueType(0);
2756 if (TLI.getLibcallName(LC)) {
2757 Ops.append(Node->op_begin() + 2, Node->op_end());
2758 Ops.push_back(Node->getOperand(1));
2759 } else {
2760 LC = RTLIB::getSYNC(Opc, VT);
2761 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2762 "Unexpected atomic op or value type!");
2763 Ops.append(Node->op_begin() + 1, Node->op_end());
2764 }
2765 return TLI.makeLibCall(DAG, LC, RetVT, Ops, CallOptions, SDLoc(Node),
2766 Node->getOperand(0));
2767}
2768
2769/// N is a shift by a value that needs to be expanded,
2770/// and the shift amount is a constant 'Amt'. Expand the operation.
2771void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, const APInt &Amt,
2772 SDValue &Lo, SDValue &Hi) {
2773 SDLoc DL(N);
2774 // Expand the incoming operand to be shifted, so that we have its parts
2775 SDValue InL, InH;
2776 GetExpandedInteger(N->getOperand(0), InL, InH);
2777
2778 // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
2779 // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
2780 if (!Amt) {
2781 Lo = InL;
2782 Hi = InH;
2783 return;
2784 }
2785
2786 EVT NVT = InL.getValueType();
2787 unsigned VTBits = N->getValueType(0).getSizeInBits();
2788 unsigned NVTBits = NVT.getSizeInBits();
2789 EVT ShTy = N->getOperand(1).getValueType();
2790
2791 if (N->getOpcode() == ISD::SHL) {
2792 if (Amt.uge(VTBits)) {
2793 Lo = Hi = DAG.getConstant(0, DL, NVT);
2794 } else if (Amt.ugt(NVTBits)) {
2795 Lo = DAG.getConstant(0, DL, NVT);
2796 Hi = DAG.getNode(ISD::SHL, DL,
2797 NVT, InL, DAG.getConstant(Amt - NVTBits, DL, ShTy));
2798 } else if (Amt == NVTBits) {
2799 Lo = DAG.getConstant(0, DL, NVT);
2800 Hi = InL;
2801 } else {
2802 Lo = DAG.getNode(ISD::SHL, DL, NVT, InL, DAG.getConstant(Amt, DL, ShTy));
2803 Hi = DAG.getNode(ISD::OR, DL, NVT,
2804 DAG.getNode(ISD::SHL, DL, NVT, InH,
2805 DAG.getConstant(Amt, DL, ShTy)),
2806 DAG.getNode(ISD::SRL, DL, NVT, InL,
2807 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
2808 }
2809 return;
2810 }
2811
2812 if (N->getOpcode() == ISD::SRL) {
2813 if (Amt.uge(VTBits)) {
2814 Lo = Hi = DAG.getConstant(0, DL, NVT);
2815 } else if (Amt.ugt(NVTBits)) {
2816 Lo = DAG.getNode(ISD::SRL, DL,
2817 NVT, InH, DAG.getConstant(Amt - NVTBits, DL, ShTy));
2818 Hi = DAG.getConstant(0, DL, NVT);
2819 } else if (Amt == NVTBits) {
2820 Lo = InH;
2821 Hi = DAG.getConstant(0, DL, NVT);
2822 } else {
2823 Lo = DAG.getNode(ISD::OR, DL, NVT,
2824 DAG.getNode(ISD::SRL, DL, NVT, InL,
2825 DAG.getConstant(Amt, DL, ShTy)),
2826 DAG.getNode(ISD::SHL, DL, NVT, InH,
2827 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
2828 Hi = DAG.getNode(ISD::SRL, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
2829 }
2830 return;
2831 }
2832
2833 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
2834 if (Amt.uge(VTBits)) {
2835 Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
2836 DAG.getConstant(NVTBits - 1, DL, ShTy));
2837 } else if (Amt.ugt(NVTBits)) {
2838 Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
2839 DAG.getConstant(Amt - NVTBits, DL, ShTy));
2840 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
2841 DAG.getConstant(NVTBits - 1, DL, ShTy));
2842 } else if (Amt == NVTBits) {
2843 Lo = InH;
2844 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
2845 DAG.getConstant(NVTBits - 1, DL, ShTy));
2846 } else {
2847 Lo = DAG.getNode(ISD::OR, DL, NVT,
2848 DAG.getNode(ISD::SRL, DL, NVT, InL,
2849 DAG.getConstant(Amt, DL, ShTy)),
2850 DAG.getNode(ISD::SHL, DL, NVT, InH,
2851 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
2852 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
2853 }
2854}
2855
2856/// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
2857/// this shift based on knowledge of the high bit of the shift amount. If we
2858/// can tell this, we know that it is >= 32 or < 32, without knowing the actual
2859/// shift amount.
2860bool DAGTypeLegalizer::
2861ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
2862 SDValue Amt = N->getOperand(1);
2863 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2864 EVT ShTy = Amt.getValueType();
2865 unsigned ShBits = ShTy.getScalarSizeInBits();
2866 unsigned NVTBits = NVT.getScalarSizeInBits();
2867 assert(isPowerOf2_32(NVTBits) &&
2868 "Expanded integer type size not a power of two!");
2869 SDLoc dl(N);
2870
2871 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
2872 KnownBits Known = DAG.computeKnownBits(N->getOperand(1));
2873
2874 // If we don't know anything about the high bits, exit.
2875 if (((Known.Zero|Known.One) & HighBitMask) == 0)
2876 return false;
2877
2878 // Get the incoming operand to be shifted.
2879 SDValue InL, InH;
2880 GetExpandedInteger(N->getOperand(0), InL, InH);
2881
2882 // If we know that any of the high bits of the shift amount are one, then we
2883 // can do this as a couple of simple shifts.
2884 if (Known.One.intersects(HighBitMask)) {
2885 // Mask out the high bit, which we know is set.
2886 Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
2887 DAG.getConstant(~HighBitMask, dl, ShTy));
2888
2889 switch (N->getOpcode()) {
2890 default: llvm_unreachable("Unknown shift");
2891 case ISD::SHL:
2892 Lo = DAG.getConstant(0, dl, NVT); // Low part is zero.
2893 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
2894 return true;
2895 case ISD::SRL:
2896 Hi = DAG.getConstant(0, dl, NVT); // Hi part is zero.
2897 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
2898 return true;
2899 case ISD::SRA:
2900 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
2901 DAG.getConstant(NVTBits - 1, dl, ShTy));
2902 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
2903 return true;
2904 }
2905 }
2906
2907 // If we know that all of the high bits of the shift amount are zero, then we
2908 // can do this as a couple of simple shifts.
2909 if (HighBitMask.isSubsetOf(Known.Zero)) {
2910 // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
2911 // shift if x is zero. We can use XOR here because x is known to be smaller
2912 // than 32.
2913 SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
2914 DAG.getConstant(NVTBits - 1, dl, ShTy));
2915
2916 unsigned Op1, Op2;
2917 switch (N->getOpcode()) {
2918 default: llvm_unreachable("Unknown shift");
2919 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
2920 case ISD::SRL:
2921 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
2922 }
2923
2924 // When shifting right the arithmetic for Lo and Hi is swapped.
2925 if (N->getOpcode() != ISD::SHL)
2926 std::swap(InL, InH);
2927
2928 // Use a little trick to get the bits that move from Lo to Hi. First
2929 // shift by one bit.
2930 SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, dl, ShTy));
2931 // Then compute the remaining shift with amount-1.
2932 SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
2933
2934 Lo = DAG.getNode(N->getOpcode(), dl, NVT, InL, Amt);
2935 Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
2936
2937 if (N->getOpcode() != ISD::SHL)
2938 std::swap(Hi, Lo);
2939 return true;
2940 }
2941
2942 return false;
2943}
2944
2945/// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
2946/// of any size.
2947bool DAGTypeLegalizer::
2948ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
2949 SDValue Amt = N->getOperand(1);
2950 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2951 EVT ShTy = Amt.getValueType();
2952 unsigned NVTBits = NVT.getSizeInBits();
2953 assert(isPowerOf2_32(NVTBits) &&
2954 "Expanded integer type size not a power of two!");
2955 SDLoc dl(N);
2956
2957 // Get the incoming operand to be shifted.
2958 SDValue InL, InH;
2959 GetExpandedInteger(N->getOperand(0), InL, InH);
2960
2961 SDValue NVBitsNode = DAG.getConstant(NVTBits, dl, ShTy);
2962 SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
2963 SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
2964 SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
2965 Amt, NVBitsNode, ISD::SETULT);
2966 SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(ShTy),
2967 Amt, DAG.getConstant(0, dl, ShTy),
2968 ISD::SETEQ);
2969
2970 SDValue LoS, HiS, LoL, HiL;
2971 switch (N->getOpcode()) {
2972 default: llvm_unreachable("Unknown shift");
2973 case ISD::SHL:
2974 // Short: ShAmt < NVTBits
2975 LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
2976 HiS = DAG.getNode(ISD::OR, dl, NVT,
2977 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
2978 DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
2979
2980 // Long: ShAmt >= NVTBits
2981 LoL = DAG.getConstant(0, dl, NVT); // Lo part is zero.
2982 HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
2983
2984 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
2985 Hi = DAG.getSelect(dl, NVT, isZero, InH,
2986 DAG.getSelect(dl, NVT, isShort, HiS, HiL));
2987 return true;
2988 case ISD::SRL:
2989 // Short: ShAmt < NVTBits
2990 HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
2991 LoS = DAG.getNode(ISD::OR, dl, NVT,
2992 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
2993 // FIXME: If Amt is zero, the following shift generates an undefined result
2994 // on some architectures.
2995 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
2996
2997 // Long: ShAmt >= NVTBits
2998 HiL = DAG.getConstant(0, dl, NVT); // Hi part is zero.
2999 LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
3000
3001 Lo = DAG.getSelect(dl, NVT, isZero, InL,
3002 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
3003 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
3004 return true;
3005 case ISD::SRA:
3006 // Short: ShAmt < NVTBits
3007 HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
3008 LoS = DAG.getNode(ISD::OR, dl, NVT,
3009 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
3010 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
3011
3012 // Long: ShAmt >= NVTBits
3013 HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
3014 DAG.getConstant(NVTBits - 1, dl, ShTy));
3015 LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
3016
3017 Lo = DAG.getSelect(dl, NVT, isZero, InL,
3018 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
3019 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
3020 return true;
3021 }
3022}
3023
3024static std::pair<ISD::CondCode, ISD::NodeType> getExpandedMinMaxOps(int Op) {
3025
3026 switch (Op) {
3027 default: llvm_unreachable("invalid min/max opcode");
3028 case ISD::SMAX:
3029 return std::make_pair(ISD::SETGT, ISD::UMAX);
3030 case ISD::UMAX:
3031 return std::make_pair(ISD::SETUGT, ISD::UMAX);
3032 case ISD::SMIN:
3033 return std::make_pair(ISD::SETLT, ISD::UMIN);
3034 case ISD::UMIN:
3035 return std::make_pair(ISD::SETULT, ISD::UMIN);
3036 }
3037}
3038
3039void DAGTypeLegalizer::ExpandIntRes_MINMAX(SDNode *N,
3040 SDValue &Lo, SDValue &Hi) {
3041 SDLoc DL(N);
3042
3043 SDValue LHS = N->getOperand(0);
3044 SDValue RHS = N->getOperand(1);
3045
3046 // If the upper halves are all sign bits, then we can perform the MINMAX on
3047 // the lower half and sign-extend the result to the upper half.
3048 unsigned NumBits = N->getValueType(0).getScalarSizeInBits();
3049 unsigned NumHalfBits = NumBits / 2;
3050 if (DAG.ComputeNumSignBits(LHS) > NumHalfBits &&
3051 DAG.ComputeNumSignBits(RHS) > NumHalfBits) {
3052 SDValue LHSL, LHSH, RHSL, RHSH;
3053 GetExpandedInteger(LHS, LHSL, LHSH);
3054 GetExpandedInteger(RHS, RHSL, RHSH);
3055 EVT NVT = LHSL.getValueType();
3056
3057 Lo = DAG.getNode(N->getOpcode(), DL, NVT, LHSL, RHSL);
3058 Hi = DAG.getNode(ISD::SRA, DL, NVT, Lo,
3059 DAG.getShiftAmountConstant(NumHalfBits - 1, NVT, DL));
3060 return;
3061 }
3062
3063 // The Lo of smin(X, -1) is LHSL if X is negative. Otherwise it's -1.
3064 // The Lo of smax(X, 0) is 0 if X is negative. Otherwise it's LHSL.
3065 if ((N->getOpcode() == ISD::SMAX && isNullConstant(RHS)) ||
3066 (N->getOpcode() == ISD::SMIN && isAllOnesConstant(RHS))) {
3067 SDValue LHSL, LHSH, RHSL, RHSH;
3068 GetExpandedInteger(LHS, LHSL, LHSH);
3069 GetExpandedInteger(RHS, RHSL, RHSH);
3070 EVT NVT = LHSL.getValueType();
3071 EVT CCT = getSetCCResultType(NVT);
3072
3073 SDValue HiNeg =
3074 DAG.getSetCC(DL, CCT, LHSH, DAG.getConstant(0, DL, NVT), ISD::SETLT);
3075 if (N->getOpcode() == ISD::SMIN) {
3076 Lo = DAG.getSelect(DL, NVT, HiNeg, LHSL, DAG.getConstant(-1, DL, NVT));
3077 } else {
3078 Lo = DAG.getSelect(DL, NVT, HiNeg, DAG.getConstant(0, DL, NVT), LHSL);
3079 }
3080 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
3081 return;
3082 }
3083
3084 const APInt *RHSVal = nullptr;
3085 if (auto *RHSConst = dyn_cast<ConstantSDNode>(RHS))
3086 RHSVal = &RHSConst->getAPIntValue();
3087
3088 // The high half of MIN/MAX is always just the the MIN/MAX of the
3089 // high halves of the operands. Expand this way if it appears profitable.
3090 if (RHSVal && (N->getOpcode() == ISD::UMIN || N->getOpcode() == ISD::UMAX) &&
3091 (RHSVal->countLeadingOnes() >= NumHalfBits ||
3092 RHSVal->countLeadingZeros() >= NumHalfBits)) {
3093 SDValue LHSL, LHSH, RHSL, RHSH;
3094 GetExpandedInteger(LHS, LHSL, LHSH);
3095 GetExpandedInteger(RHS, RHSL, RHSH);
3096 EVT NVT = LHSL.getValueType();
3097 EVT CCT = getSetCCResultType(NVT);
3098
3099 ISD::NodeType LoOpc;
3100 ISD::CondCode CondC;
3101 std::tie(CondC, LoOpc) = getExpandedMinMaxOps(N->getOpcode());
3102
3103 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
3104 // We need to know whether to select Lo part that corresponds to 'winning'
3105 // Hi part or if Hi parts are equal.
3106 SDValue IsHiLeft = DAG.getSetCC(DL, CCT, LHSH, RHSH, CondC);
3107 SDValue IsHiEq = DAG.getSetCC(DL, CCT, LHSH, RHSH, ISD::SETEQ);
3108
3109 // Lo part corresponding to the 'winning' Hi part
3110 SDValue LoCmp = DAG.getSelect(DL, NVT, IsHiLeft, LHSL, RHSL);
3111
3112 // Recursed Lo part if Hi parts are equal, this uses unsigned version
3113 SDValue LoMinMax = DAG.getNode(LoOpc, DL, NVT, {LHSL, RHSL});
3114
3115 Lo = DAG.getSelect(DL, NVT, IsHiEq, LoMinMax, LoCmp);
3116 return;
3117 }
3118
3119 // Expand to "a < b ? a : b" etc. Prefer ge/le if that simplifies
3120 // the compare.
3121 ISD::CondCode Pred;
3122 switch (N->getOpcode()) {
3123 default: llvm_unreachable("How did we get here?");
3124 case ISD::SMAX:
3125 if (RHSVal && RHSVal->countTrailingZeros() >= NumHalfBits)
3126 Pred = ISD::SETGE;
3127 else
3128 Pred = ISD::SETGT;
3129 break;
3130 case ISD::SMIN:
3131 if (RHSVal && RHSVal->countTrailingOnes() >= NumHalfBits)
3132 Pred = ISD::SETLE;
3133 else
3134 Pred = ISD::SETLT;
3135 break;
3136 case ISD::UMAX:
3137 if (RHSVal && RHSVal->countTrailingZeros() >= NumHalfBits)
3138 Pred = ISD::SETUGE;
3139 else
3140 Pred = ISD::SETUGT;
3141 break;
3142 case ISD::UMIN:
3143 if (RHSVal && RHSVal->countTrailingOnes() >= NumHalfBits)
3144 Pred = ISD::SETULE;
3145 else
3146 Pred = ISD::SETULT;
3147 break;
3148 }
3149 EVT VT = N->getValueType(0);
3150 EVT CCT = getSetCCResultType(VT);
3151 SDValue Cond = DAG.getSetCC(DL, CCT, LHS, RHS, Pred);
3152 SDValue Result = DAG.getSelect(DL, VT, Cond, LHS, RHS);
3153 SplitInteger(Result, Lo, Hi);
3154}
3155
3156void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
3157 SDValue &Lo, SDValue &Hi) {
3158 SDLoc dl(N);
3159 // Expand the subcomponents.
3160 SDValue LHSL, LHSH, RHSL, RHSH;
3161 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3162 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3163
3164 EVT NVT = LHSL.getValueType();
3165 SDValue LoOps[2] = { LHSL, RHSL };
3166 SDValue HiOps[3] = { LHSH, RHSH };
3167
3168 bool HasOpCarry = TLI.isOperationLegalOrCustom(
3169 N->getOpcode() == ISD::ADD ? ISD::UADDO_CARRY : ISD::USUBO_CARRY,
3170 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3171 if (HasOpCarry) {
3172 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
3173 if (N->getOpcode() == ISD::ADD) {
3174 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
3175 HiOps[2] = Lo.getValue(1);
3176 Hi = DAG.computeKnownBits(HiOps[2]).isZero()
3177 ? DAG.getNode(ISD::UADDO, dl, VTList, ArrayRef(HiOps, 2))
3178 : DAG.getNode(ISD::UADDO_CARRY, dl, VTList, HiOps);
3179 } else {
3180 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
3181 HiOps[2] = Lo.getValue(1);
3182 Hi = DAG.computeKnownBits(HiOps[2]).isZero()
3183 ? DAG.getNode(ISD::USUBO, dl, VTList, ArrayRef(HiOps, 2))
3184 : DAG.getNode(ISD::USUBO_CARRY, dl, VTList, HiOps);
3185 }
3186 return;
3187 }
3188
3189 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
3190 // them. TODO: Teach operation legalization how to expand unsupported
3191 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
3192 // a carry of type MVT::Glue, but there doesn't seem to be any way to
3193 // generate a value of this type in the expanded code sequence.
3194 bool hasCarry =
3195 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
3197 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3198
3199 if (hasCarry) {
3200 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
3201 if (N->getOpcode() == ISD::ADD) {
3202 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
3203 HiOps[2] = Lo.getValue(1);
3204 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
3205 } else {
3206 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
3207 HiOps[2] = Lo.getValue(1);
3208 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
3209 }
3210 return;
3211 }
3212
3213 bool hasOVF =
3214 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
3216 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3218
3219 if (hasOVF) {
3220 EVT OvfVT = getSetCCResultType(NVT);
3221 SDVTList VTList = DAG.getVTList(NVT, OvfVT);
3222 int RevOpc;
3223 if (N->getOpcode() == ISD::ADD) {
3224 RevOpc = ISD::SUB;
3225 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
3226 Hi = DAG.getNode(ISD::ADD, dl, NVT, ArrayRef(HiOps, 2));
3227 } else {
3228 RevOpc = ISD::ADD;
3229 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
3230 Hi = DAG.getNode(ISD::SUB, dl, NVT, ArrayRef(HiOps, 2));
3231 }
3232 SDValue OVF = Lo.getValue(1);
3233
3234 switch (BoolType) {
3236 OVF = DAG.getNode(ISD::AND, dl, OvfVT, DAG.getConstant(1, dl, OvfVT), OVF);
3237 [[fallthrough]];
3239 OVF = DAG.getZExtOrTrunc(OVF, dl, NVT);
3240 Hi = DAG.getNode(N->getOpcode(), dl, NVT, Hi, OVF);
3241 break;
3243 OVF = DAG.getSExtOrTrunc(OVF, dl, NVT);
3244 Hi = DAG.getNode(RevOpc, dl, NVT, Hi, OVF);
3245 }
3246 return;
3247 }
3248
3249 if (N->getOpcode() == ISD::ADD) {
3250 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps);
3251 Hi = DAG.getNode(ISD::ADD, dl, NVT, ArrayRef(HiOps, 2));
3252 SDValue Cmp;
3253 // Special case: X+1 has a carry out if X+1==0. This may reduce the live
3254 // range of X. We assume comparing with 0 is cheap.
3255 if (isOneConstant(LoOps[1]))
3256 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
3257 DAG.getConstant(0, dl, NVT), ISD::SETEQ);
3258 else if (isAllOnesConstant(LoOps[1])) {
3259 if (isAllOnesConstant(HiOps[1]))
3260 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), LoOps[0],
3261 DAG.getConstant(0, dl, NVT), ISD::SETEQ);
3262 else
3263 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), LoOps[0],
3264 DAG.getConstant(0, dl, NVT), ISD::SETNE);
3265 } else
3266 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
3267 ISD::SETULT);
3268
3269 SDValue Carry;
3271 Carry = DAG.getZExtOrTrunc(Cmp, dl, NVT);
3272 else
3273 Carry = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
3274 DAG.getConstant(0, dl, NVT));
3275
3276 if (isAllOnesConstant(LoOps[1]) && isAllOnesConstant(HiOps[1]))
3277 Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps[0], Carry);
3278 else
3279 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry);
3280 } else {
3281 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps);
3282 Hi = DAG.getNode(ISD::SUB, dl, NVT, ArrayRef(HiOps, 2));
3283 SDValue Cmp =
3284 DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
3285 LoOps[0], LoOps[1], ISD::SETULT);
3286
3287 SDValue Borrow;
3289 Borrow = DAG.getZExtOrTrunc(Cmp, dl, NVT);
3290 else
3291 Borrow = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
3292 DAG.getConstant(0, dl, NVT));
3293
3294 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
3295 }
3296}
3297
3298void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
3299 SDValue &Lo, SDValue &Hi) {
3300 // Expand the subcomponents.
3301 SDValue LHSL, LHSH, RHSL, RHSH;
3302 SDLoc dl(N);
3303 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3304 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3305 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
3306 SDValue LoOps[2] = { LHSL, RHSL };
3307 SDValue HiOps[3] = { LHSH, RHSH };
3308
3309 if (N->getOpcode() == ISD::ADDC) {
3310 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
3311 HiOps[2] = Lo.getValue(1);
3312 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
3313 } else {
3314 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
3315 HiOps[2] = Lo.getValue(1);
3316 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
3317 }
3318
3319 // Legalized the flag result - switch anything that used the old flag to
3320 // use the new one.
3321 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
3322}
3323
3324void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
3325 SDValue &Lo, SDValue &Hi) {
3326 // Expand the subcomponents.
3327 SDValue LHSL, LHSH, RHSL, RHSH;
3328 SDLoc dl(N);
3329 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3330 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3331 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
3332 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
3333 SDValue HiOps[3] = { LHSH, RHSH };
3334
3335 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
3336 HiOps[2] = Lo.getValue(1);
3337 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
3338
3339 // Legalized the flag result - switch anything that used the old flag to
3340 // use the new one.
3341 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
3342}
3343
3344void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
3345 SDValue &Lo, SDValue &Hi) {
3346 SDValue LHS = N->getOperand(0);
3347 SDValue RHS = N->getOperand(1);
3348 SDLoc dl(N);
3349
3350 SDValue Ovf;
3351
3352 unsigned CarryOp, NoCarryOp;
3354 switch(N->getOpcode()) {
3355 case ISD::UADDO:
3356 CarryOp = ISD::UADDO_CARRY;
3357 NoCarryOp = ISD::ADD;
3358 Cond = ISD::SETULT;
3359 break;
3360 case ISD::USUBO:
3361 CarryOp = ISD::USUBO_CARRY;
3362 NoCarryOp = ISD::SUB;
3363 Cond = ISD::SETUGT;
3364 break;
3365 default:
3366 llvm_unreachable("Node has unexpected Opcode");
3367 }
3368
3369 bool HasCarryOp = TLI.isOperationLegalOrCustom(
3370 CarryOp, TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
3371
3372 if (HasCarryOp) {
3373 // Expand the subcomponents.
3374 SDValue LHSL, LHSH, RHSL, RHSH;
3375 GetExpandedInteger(LHS, LHSL, LHSH);
3376 GetExpandedInteger(RHS, RHSL, RHSH);
3377 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
3378 SDValue LoOps[2] = { LHSL, RHSL };
3379 SDValue HiOps[3] = { LHSH, RHSH };
3380
3381 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
3382 HiOps[2] = Lo.getValue(1);
3383 Hi = DAG.getNode(CarryOp, dl, VTList, HiOps);
3384
3385 Ovf = Hi.getValue(1);
3386 } else {
3387 // Expand the result by simply replacing it with the equivalent
3388 // non-overflow-checking operation.
3389 SDValue Sum = DAG.getNode(NoCarryOp, dl, LHS.getValueType(), LHS, RHS);
3390 SplitInteger(Sum, Lo, Hi);
3391
3392 if (N->getOpcode() == ISD::UADDO && isOneConstant(RHS)) {
3393 // Special case: uaddo X, 1 overflowed if X+1 == 0. We can detect this
3394 // with (Lo | Hi) == 0.
3395 SDValue Or = DAG.getNode(ISD::OR, dl, Lo.getValueType(), Lo, Hi);
3396 Ovf = DAG.getSetCC(dl, N->getValueType(1), Or,
3397 DAG.getConstant(0, dl, Lo.getValueType()), ISD::SETEQ);
3398 } else if (N->getOpcode() == ISD::UADDO && isAllOnesConstant(RHS)) {
3399 // Special case: uaddo X, -1 overflows if X == 0.
3400 Ovf =
3401 DAG.getSetCC(dl, N->getValueType(1), LHS,
3402 DAG.getConstant(0, dl, LHS.getValueType()), ISD::SETNE);
3403 } else {
3404 // Calculate the overflow: addition overflows iff a + b < a, and
3405 // subtraction overflows iff a - b > a.
3406 Ovf = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS, Cond);
3407 }
3408 }
3409
3410 // Legalized the flag result - switch anything that used the old flag to
3411 // use the new one.
3412 ReplaceValueWith(SDValue(N, 1), Ovf);
3413}
3414
3415void DAGTypeLegalizer::ExpandIntRes_UADDSUBO_CARRY(SDNode *N, SDValue &Lo,
3416 SDValue &Hi) {
3417 // Expand the subcomponents.
3418 SDValue LHSL, LHSH, RHSL, RHSH;
3419 SDLoc dl(N);
3420 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3421 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3422 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
3423 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
3424 SDValue HiOps[3] = { LHSH, RHSH, SDValue() };
3425
3426 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
3427 HiOps[2] = Lo.getValue(1);
3428 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
3429
3430 // Legalized the flag result - switch anything that used the old flag to
3431 // use the new one.
3432 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
3433}
3434
3435void DAGTypeLegalizer::ExpandIntRes_SADDSUBO_CARRY(SDNode *N,
3436 SDValue &Lo, SDValue &Hi) {
3437 // Expand the subcomponents.
3438 SDValue LHSL, LHSH, RHSL, RHSH;
3439 SDLoc dl(N);
3440 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3441 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3442 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
3443
3444 // We need to use an unsigned carry op for the lo part.
3445 unsigned CarryOp =
3447 Lo = DAG.getNode(CarryOp, dl, VTList, { LHSL, RHSL, N->getOperand(2) });
3448 Hi = DAG.getNode(N->getOpcode(), dl, VTList, { LHSH, RHSH, Lo.getValue(1) });
3449
3450 // Legalized the flag result - switch anything that used the old flag to
3451 // use the new one.
3452 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
3453}
3454
3455void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
3456 SDValue &Lo, SDValue &Hi) {
3457 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3458 SDLoc dl(N);
3459 SDValue Op = N->getOperand(0);
3460 if (Op.getValueType().bitsLE(NVT)) {
3461 // The low part is any extension of the input (which degenerates to a copy).
3462 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
3463 Hi = DAG.getUNDEF(NVT); // The high part is undefined.
3464 } else {
3465 // For example, extension of an i48 to an i64. The operand type necessarily
3466 // promotes to the result type, so will end up being expanded too.
3467 assert(getTypeAction(Op.getValueType()) ==
3469 "Only know how to promote this result!");
3470 SDValue Res = GetPromotedInteger(Op);
3471 assert(Res.getValueType() == N->getValueType(0) &&
3472 "Operand over promoted?");
3473 // Split the promoted operand. This will simplify when it is expanded.
3474 SplitInteger(Res, Lo, Hi);
3475 }
3476}
3477
3478void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
3479 SDValue &Lo, SDValue &Hi) {
3480 SDLoc dl(N);
3481 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3482 EVT NVT = Lo.getValueType();
3483 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3484 unsigned NVTBits = NVT.getSizeInBits();
3485 unsigned EVTBits = EVT.getSizeInBits();
3486
3487 if (NVTBits < EVTBits) {
3488 Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
3490 EVTBits - NVTBits)));
3491 } else {
3492 Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
3493 // The high part replicates the sign bit of Lo, make it explicit.
3494 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
3495 DAG.getConstant(NVTBits - 1, dl,
3496 TLI.getPointerTy(DAG.getDataLayout())));
3497 }
3498}
3499
3500void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
3501 SDValue &Lo, SDValue &Hi) {
3502 SDLoc dl(N);
3503 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3504 EVT NVT = Lo.getValueType();
3505 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3506 unsigned NVTBits = NVT.getSizeInBits();
3507 unsigned EVTBits = EVT.getSizeInBits();
3508
3509 if (NVTBits < EVTBits) {
3510 Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
3512 EVTBits - NVTBits)));
3513 } else {
3514 Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
3515 // The high part must be zero, make it explicit.
3516 Hi = DAG.getConstant(0, dl, NVT);
3517 }
3518}
3519
3520void DAGTypeLegalizer::ExpandIntRes_BITREVERSE(SDNode *N,
3521 SDValue &Lo, SDValue &Hi) {
3522 SDLoc dl(N);
3523 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
3524 Lo = DAG.getNode(ISD::BITREVERSE, dl, Lo.getValueType(), Lo);
3525 Hi = DAG.getNode(ISD::BITREVERSE, dl, Hi.getValueType(), Hi);
3526}
3527
3528void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
3529 SDValue &Lo, SDValue &Hi) {
3530 SDLoc dl(N);
3531 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
3532 Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
3533 Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
3534}
3535
3536void DAGTypeLegalizer::ExpandIntRes_PARITY(SDNode *N, SDValue &Lo,
3537 SDValue &Hi) {
3538 SDLoc dl(N);
3539 // parity(HiLo) -> parity(Lo^Hi)
3540 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3541 EVT NVT = Lo.getValueType();
3542 Lo =
3543 DAG.getNode(ISD::PARITY, dl, NVT, DAG.getNode(ISD::XOR, dl, NVT, Lo, Hi));
3544 Hi = DAG.getConstant(0, dl, NVT);
3545}
3546
3547void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
3548 SDValue &Lo, SDValue &Hi) {
3549 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3550 unsigned NBitWidth = NVT.getSizeInBits();
3551 auto Constant = cast<ConstantSDNode>(N);
3552 const APInt &Cst = Constant->getAPIntValue();
3553 bool IsTarget = Constant->isTargetOpcode();
3554 bool IsOpaque = Constant->isOpaque();
3555 SDLoc dl(N);
3556 Lo = DAG.getConstant(Cst.trunc(NBitWidth), dl, NVT, IsTarget, IsOpaque);
3557 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), dl, NVT, IsTarget,
3558 IsOpaque);
3559}
3560
3561void DAGTypeLegalizer::ExpandIntRes_ABS(SDNode *N, SDValue &Lo, SDValue &Hi) {
3562 SDLoc dl(N);
3563
3564 SDValue N0 = N->getOperand(0);
3565 GetExpandedInteger(N0, Lo, Hi);
3566 EVT NVT = Lo.getValueType();
3567
3568 // If the upper half is all sign bits, then we can perform the ABS on the
3569 // lower half and zero-extend.
3570 if (DAG.ComputeNumSignBits(N0) > NVT.getScalarSizeInBits()) {
3571 Lo = DAG.getNode(ISD::ABS, dl, NVT, Lo);
3572 Hi = DAG.getConstant(0, dl, NVT);
3573 return;
3574 }
3575
3576 // If we have USUBO_CARRY, use the expanded form of the sra+xor+sub sequence
3577 // we use in LegalizeDAG. The SUB part of the expansion is based on
3578 // ExpandIntRes_ADDSUB which also uses USUBO_CARRY/USUBO after checking that
3579 // USUBO_CARRY is LegalOrCustom. Each of the pieces here can be further
3580 // expanded if needed. Shift expansion has a special case for filling with
3581 // sign bits so that we will only end up with one SRA.
3582 bool HasSubCarry = TLI.isOperationLegalOrCustom(
3584 if (HasSubCarry) {
3585 SDValue Sign = DAG.getNode(
3586 ISD::SRA, dl, NVT, Hi,
3587 DAG.getShiftAmountConstant(NVT.getSizeInBits() - 1, NVT, dl));
3588 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
3589 Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Sign);
3590 Hi = DAG.getNode(ISD::XOR, dl, NVT, Hi, Sign);
3591 Lo = DAG.getNode(ISD::USUBO, dl, VTList, Lo, Sign);
3592 Hi = DAG.getNode(ISD::USUBO_CARRY, dl, VTList, Hi, Sign, Lo.getValue(1));
3593 return;
3594 }
3595
3596 // abs(HiLo) -> (Hi < 0 ? -HiLo : HiLo)
3597 EVT VT = N->getValueType(0);
3598 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT,
3599 DAG.getConstant(0, dl, VT), N0);
3600 SDValue NegLo, NegHi;
3601 SplitInteger(Neg, NegLo, NegHi);
3602
3603 SDValue HiIsNeg = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
3604 DAG.getConstant(0, dl, NVT), ISD::SETLT);
3605 Lo = DAG.getSelect(dl, NVT, HiIsNeg, NegLo, Lo);
3606 Hi = DAG.getSelect(dl, NVT, HiIsNeg, NegHi, Hi);
3607}
3608
3609void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
3610 SDValue &Lo, SDValue &Hi) {
3611 SDLoc dl(N);
3612 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
3613 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3614 EVT NVT = Lo.getValueType();
3615
3616 SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
3617 DAG.getConstant(0, dl, NVT), ISD::SETNE);
3618
3619 SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
3620 SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, NVT, Hi);
3621
3622 Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
3623 DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
3624 DAG.getConstant(NVT.getSizeInBits(), dl,
3625 NVT)));
3626 Hi = DAG.getConstant(0, dl, NVT);
3627}
3628
3629void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
3630 SDValue &Lo, SDValue &Hi) {
3631 SDLoc dl(N);
3632 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
3633 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3634 EVT NVT = Lo.getValueType();
3635 Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
3636 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
3637 Hi = DAG.getConstant(0, dl, NVT);
3638}
3639
3640void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
3641 SDValue &Lo, SDValue &Hi) {
3642 SDLoc dl(N);
3643 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
3644 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3645 EVT NVT = Lo.getValueType();
3646
3647 SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
3648 DAG.getConstant(0, dl, NVT), ISD::SETNE);
3649
3650 SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, NVT, Lo);
3651 SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
3652
3653 Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
3654 DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
3655 DAG.getConstant(NVT.getSizeInBits(), dl,
3656 NVT)));
3657 Hi = DAG.getConstant(0, dl, NVT);
3658}
3659
3660void DAGTypeLegalizer::ExpandIntRes_GET_ROUNDING(SDNode *N, SDValue &Lo,
3661 SDValue &Hi) {
3662 SDLoc dl(N);
3663 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3664 unsigned NBitWidth = NVT.getSizeInBits();
3665
3666 Lo = DAG.getNode(ISD::GET_ROUNDING, dl, {NVT, MVT::Other}, N->getOperand(0));
3667 SDValue Chain = Lo.getValue(1);
3668 // The high part is the sign of Lo, as -1 is a valid value for GET_ROUNDING
3669 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
3670 DAG.getShiftAmountConstant(NBitWidth - 1, NVT, dl));
3671
3672 // Legalize the chain result - switch anything that used the old chain to
3673 // use the new one.
3674 ReplaceValueWith(SDValue(N, 1), Chain);
3675}
3676
3677// Helper for producing an FP_EXTEND/STRICT_FP_EXTEND of Op.
3678static SDValue fpExtendHelper(SDValue Op, SDValue &Chain, bool IsStrict, EVT VT,
3679 SDLoc DL, SelectionDAG &DAG) {
3680 if (IsStrict) {
3681 Op = DAG.getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op});
3682 Chain = Op.getValue(1);
3683 return Op;
3684 }
3685 return DAG.getNode(ISD::FP_EXTEND, DL, VT, Op);
3686}
3687
3688void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT(SDNode *N, SDValue &Lo,
3689 SDValue &Hi) {
3690 SDLoc dl(N);
3691 EVT VT = N->getValueType(0);
3692
3693 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
3694 N->getOpcode() == ISD::STRICT_FP_TO_SINT;
3695 bool IsStrict = N->isStrictFPOpcode();
3696 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
3697 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
3698 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
3699 Op = GetPromotedFloat(Op);
3700
3701 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftPromoteHalf) {
3702 EVT OFPVT = Op.getValueType();
3703 EVT NFPVT = TLI.getTypeToTransformTo(*DAG.getContext(), OFPVT);
3704 Op = GetSoftPromotedHalf(Op);
3705 Op = DAG.getNode(OFPVT == MVT::f16 ? ISD::FP16_TO_FP : ISD::BF16_TO_FP, dl,
3706 NFPVT, Op);
3707 Op = DAG.getNode(IsSigned ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, dl, VT, Op);
3708 SplitInteger(Op, Lo, Hi);
3709 return;
3710 }
3711
3712 if (Op.getValueType() == MVT::bf16) {
3713 // Extend to f32 as there is no bf16 libcall.
3714 Op = fpExtendHelper(Op, Chain, IsStrict, MVT::f32, dl, DAG);
3715 }
3716
3717 RTLIB::Libcall LC = IsSigned ? RTLIB::getFPTOSINT(Op.getValueType(), VT)
3718 : RTLIB::getFPTOUINT(Op.getValueType(), VT);
3719 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-xint conversion!");
3721 CallOptions.setSExt(true);
3722 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, VT, Op,
3723 CallOptions, dl, Chain);
3724 SplitInteger(Tmp.first, Lo, Hi);
3725
3726 if (IsStrict)
3727 ReplaceValueWith(SDValue(N, 1), Tmp.second);
3728}
3729
3730void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT_SAT(SDNode *N, SDValue &Lo,
3731 SDValue &Hi) {
3732 SDValue Res = TLI.expandFP_TO_INT_SAT(N, DAG);
3733 SplitInteger(Res, Lo, Hi);
3734}
3735
3736void DAGTypeLegalizer::ExpandIntRes_XROUND_XRINT(SDNode *N, SDValue &Lo,
3737 SDValue &Hi) {
3738 SDLoc dl(N);
3739 bool IsStrict = N->isStrictFPOpcode();
3740 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
3741 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
3742
3743 assert(getTypeAction(Op.getValueType()) != TargetLowering::TypePromoteFloat &&
3744 "Input type needs to be promoted!");
3745
3746 EVT VT = Op.getValueType();
3747
3748 if (VT == MVT::f16) {
3749 // Extend to f32.
3750 VT = MVT::f32;
3751 Op = fpExtendHelper(Op, Chain, IsStrict, VT, dl, DAG);
3752 }
3753
3754 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3755 if (N->getOpcode() == ISD::LROUND ||
3756 N->getOpcode() == ISD::STRICT_LROUND) {
3757 if (VT == MVT::f32)
3758 LC = RTLIB::LROUND_F32;
3759 else if (VT == MVT::f64)
3760 LC = RTLIB::LROUND_F64;
3761 else if (VT == MVT::f80)
3762 LC = RTLIB::LROUND_F80;
3763 else if (VT == MVT::f128)
3764 LC = RTLIB::LROUND_F128;
3765 else if (VT == MVT::ppcf128)
3766 LC = RTLIB::LROUND_PPCF128;
3767 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected lround input type!");
3768 } else if (N->getOpcode() == ISD::LRINT ||
3769 N->getOpcode() == ISD::STRICT_LRINT) {
3770 if (VT == MVT::f32)
3771 LC = RTLIB::LRINT_F32;
3772 else if (VT == MVT::f64)
3773 LC = RTLIB::LRINT_F64;
3774 else if (VT == MVT::f80)
3775 LC = RTLIB::LRINT_F80;
3776 else if (VT == MVT::f128)
3777 LC = RTLIB::LRINT_F128;
3778 else if (VT == MVT::ppcf128)
3779 LC = RTLIB::LRINT_PPCF128;
3780 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected lrint input type!");
3781 } else if (N->getOpcode() == ISD::LLROUND ||
3782 N->getOpcode() == ISD::STRICT_LLROUND) {
3783 if (VT == MVT::f32)
3784 LC = RTLIB::LLROUND_F32;
3785 else if (VT == MVT::f64)
3786 LC = RTLIB::LLROUND_F64;
3787 else if (VT == MVT::f80)
3788 LC = RTLIB::LLROUND_F80;
3789 else if (VT == MVT::f128)
3790 LC = RTLIB::LLROUND_F128;
3791 else if (VT == MVT::ppcf128)
3792 LC = RTLIB::LLROUND_PPCF128;
3793 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llround input type!");
3794 } else if (N->getOpcode() == ISD::LLRINT ||
3795 N->getOpcode() == ISD::STRICT_LLRINT) {
3796 if (VT == MVT::f32)
3797 LC = RTLIB::LLRINT_F32;
3798 else if (VT == MVT::f64)
3799 LC = RTLIB::LLRINT_F64;
3800 else if (VT == MVT::f80)
3801 LC = RTLIB::LLRINT_F80;
3802 else if (VT == MVT::f128)
3803 LC = RTLIB::LLRINT_F128;
3804 else if (VT == MVT::ppcf128)
3805 LC = RTLIB::LLRINT_PPCF128;
3806 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llrint input type!");
3807 } else
3808 llvm_unreachable("Unexpected opcode!");
3809
3810 EVT RetVT = N->getValueType(0);
3811
3813 CallOptions.setSExt(true);
3814 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
3815 Op, CallOptions, dl,
3816 Chain);
3817 SplitInteger(Tmp.first, Lo, Hi);
3818
3819 if (N->isStrictFPOpcode())
3820 ReplaceValueWith(SDValue(N, 1), Tmp.second);
3821}
3822
3823void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
3824 SDValue &Lo, SDValue &Hi) {
3825 if (N->isAtomic()) {
3826 // It's typical to have larger CAS than atomic load instructions.
3827 SDLoc dl(N);
3828 EVT VT = N->getMemoryVT();
3829 SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
3830 SDValue Zero = DAG.getConstant(0, dl, VT);
3831 SDValue Swap = DAG.getAtomicCmpSwap(
3833 VT, VTs, N->getOperand(0),
3834 N->getOperand(1), Zero, Zero, N->getMemOperand());
3835 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
3836 ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
3837 return;
3838 }
3839
3840 if (ISD::isNormalLoad(N)) {
3841 ExpandRes_NormalLoad(N, Lo, Hi);
3842 return;
3843 }
3844
3845 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
3846
3847 EVT VT = N->getValueType(0);
3848 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3849 SDValue Ch = N->getChain();
3850 SDValue Ptr = N->getBasePtr();
3851 ISD::LoadExtType ExtType = N->getExtensionType();
3852 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
3853 AAMDNodes AAInfo = N->getAAInfo();
3854 SDLoc dl(N);
3855
3856 assert(NVT.isByteSized() && "Expanded type not byte sized!");
3857
3858 if (N->getMemoryVT().bitsLE(NVT)) {
3859 EVT MemVT = N->getMemoryVT();
3860
3861 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(), MemVT,
3862 N->getOriginalAlign(), MMOFlags, AAInfo);
3863
3864 // Remember the chain.
3865 Ch = Lo.getValue(1);
3866
3867 if (ExtType == ISD::SEXTLOAD) {
3868 // The high part is obtained by SRA'ing all but one of the bits of the
3869 // lo part.
3870 unsigned LoSize = Lo.getValueSizeInBits();
3871 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
3872 DAG.getConstant(LoSize - 1, dl,
3873 TLI.getPointerTy(DAG.getDataLayout())));
3874 } else if (ExtType == ISD::ZEXTLOAD) {
3875 // The high part is just a zero.
3876 Hi = DAG.getConstant(0, dl, NVT);
3877 } else {
3878