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