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