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