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 RTLIB::LibcallImpl LCImpl = TLI.getLibcallImpl(LC);
2696 if (LCImpl == RTLIB::Unsupported) {
2697 // Scalarize vector FPOWI instead of promoting the type. This allows the
2698 // scalar FPOWIs to be visited and converted to libcalls before promoting
2699 // the type.
2700 // FIXME: This should be done in LegalizeVectorOps/LegalizeDAG, but call
2701 // lowering needs the unpromoted EVT.
2702 if (IsPowI && N->getValueType(0).isVector())
2703 return DAG.UnrollVectorOp(N);
2704 SmallVector<SDValue, 3> NewOps(N->ops());
2705 NewOps[1 + OpOffset] = SExtPromotedInteger(N->getOperand(1 + OpOffset));
2706 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2707 }
2708
2709 // We can't just promote the exponent type in FPOWI, since we want to lower
2710 // the node to a libcall and we if we promote to a type larger than
2711 // sizeof(int) the libcall might not be according to the targets ABI. Instead
2712 // we rewrite to a libcall here directly, letting makeLibCall handle promotion
2713 // if the target accepts it according to shouldSignExtendTypeInLibCall.
2714
2715 // The exponent should fit in a sizeof(int) type for the libcall to be valid.
2716 assert(DAG.getLibInfo().getIntSize() ==
2717 N->getOperand(1 + OpOffset).getValueType().getSizeInBits() &&
2718 "POWI exponent should match with sizeof(int) when doing the libcall.");
2719 TargetLowering::MakeLibCallOptions CallOptions;
2720 CallOptions.setIsSigned(true);
2721 SDValue Ops[2] = {N->getOperand(0 + OpOffset), N->getOperand(1 + OpOffset)};
2722 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
2723 DAG, LCImpl, N->getValueType(0), Ops, CallOptions, SDLoc(N), Chain);
2724 ReplaceValueWith(SDValue(N, 0), Tmp.first);
2725 if (IsStrict)
2726 ReplaceValueWith(SDValue(N, 1), Tmp.second);
2727 return SDValue();
2728}
2729
2731 switch (N->getOpcode()) {
2732 default:
2733 llvm_unreachable("Expected integer vector reduction");
2734 case ISD::VECREDUCE_ADD:
2735 case ISD::VECREDUCE_MUL:
2736 case ISD::VECREDUCE_AND:
2737 case ISD::VECREDUCE_OR:
2738 case ISD::VECREDUCE_XOR:
2739 case ISD::VP_REDUCE_ADD:
2740 case ISD::VP_REDUCE_MUL:
2741 case ISD::VP_REDUCE_AND:
2742 case ISD::VP_REDUCE_OR:
2743 case ISD::VP_REDUCE_XOR:
2744 return ISD::ANY_EXTEND;
2745 case ISD::VECREDUCE_SMAX:
2746 case ISD::VECREDUCE_SMIN:
2747 case ISD::VP_REDUCE_SMAX:
2748 case ISD::VP_REDUCE_SMIN:
2749 return ISD::SIGN_EXTEND;
2750 case ISD::VECREDUCE_UMAX:
2751 case ISD::VECREDUCE_UMIN:
2752 case ISD::VP_REDUCE_UMAX:
2753 case ISD::VP_REDUCE_UMIN:
2754 return ISD::ZERO_EXTEND;
2755 }
2756}
2757
2758SDValue DAGTypeLegalizer::PromoteIntOpVectorReduction(SDNode *N, SDValue V) {
2759 switch (getExtendForIntVecReduction(N)) {
2760 default:
2761 llvm_unreachable("Impossible extension kind for integer reduction");
2762 case ISD::ANY_EXTEND:
2763 return GetPromotedInteger(V);
2764 case ISD::SIGN_EXTEND:
2765 return SExtPromotedInteger(V);
2766 case ISD::ZERO_EXTEND:
2767 return ZExtPromotedInteger(V);
2768 }
2769}
2770
2771SDValue DAGTypeLegalizer::PromoteIntOp_VECREDUCE(SDNode *N) {
2772 SDLoc dl(N);
2773 SDValue Op = PromoteIntOpVectorReduction(N, N->getOperand(0));
2774
2775 EVT OrigEltVT = N->getOperand(0).getValueType().getVectorElementType();
2776 EVT InVT = Op.getValueType();
2777 EVT EltVT = InVT.getVectorElementType();
2778 EVT ResVT = N->getValueType(0);
2779 unsigned Opcode = N->getOpcode();
2780
2781 // An i1 vecreduce_xor is equivalent to vecreduce_add, use that instead if
2782 // vecreduce_xor is not legal
2783 if (Opcode == ISD::VECREDUCE_XOR && OrigEltVT == MVT::i1 &&
2784 !TLI.isOperationLegalOrCustom(ISD::VECREDUCE_XOR, InVT) &&
2785 TLI.isOperationLegalOrCustom(ISD::VECREDUCE_ADD, InVT))
2786 Opcode = ISD::VECREDUCE_ADD;
2787
2788 // An i1 vecreduce_or is equivalent to vecreduce_umax, use that instead if
2789 // vecreduce_or is not legal
2790 else if (Opcode == ISD::VECREDUCE_OR && OrigEltVT == MVT::i1 &&
2791 !TLI.isOperationLegalOrCustom(ISD::VECREDUCE_OR, InVT) &&
2792 TLI.isOperationLegalOrCustom(ISD::VECREDUCE_UMAX, InVT)) {
2793 Opcode = ISD::VECREDUCE_UMAX;
2794 // Can't use promoteTargetBoolean here because we still need
2795 // to either sign_ext or zero_ext in the undefined case.
2796 switch (TLI.getBooleanContents(InVT)) {
2799 Op = ZExtPromotedInteger(N->getOperand(0));
2800 break;
2802 Op = SExtPromotedInteger(N->getOperand(0));
2803 break;
2804 }
2805 }
2806
2807 // An i1 vecreduce_and is equivalent to vecreduce_umin, use that instead if
2808 // vecreduce_and is not legal
2809 else if (Opcode == ISD::VECREDUCE_AND && OrigEltVT == MVT::i1 &&
2810 !TLI.isOperationLegalOrCustom(ISD::VECREDUCE_AND, InVT) &&
2811 TLI.isOperationLegalOrCustom(ISD::VECREDUCE_UMIN, InVT)) {
2812 Opcode = ISD::VECREDUCE_UMIN;
2813 // Can't use promoteTargetBoolean here because we still need
2814 // to either sign_ext or zero_ext in the undefined case.
2815 switch (TLI.getBooleanContents(InVT)) {
2818 Op = ZExtPromotedInteger(N->getOperand(0));
2819 break;
2821 Op = SExtPromotedInteger(N->getOperand(0));
2822 break;
2823 }
2824 }
2825
2826 if (ResVT.bitsGE(EltVT))
2827 return DAG.getNode(Opcode, SDLoc(N), ResVT, Op);
2828
2829 // Result size must be >= element size. If this is not the case after
2830 // promotion, also promote the result type and then truncate.
2831 SDValue Reduce = DAG.getNode(Opcode, dl, EltVT, Op);
2832 return DAG.getNode(ISD::TRUNCATE, dl, ResVT, Reduce);
2833}
2834
2835SDValue DAGTypeLegalizer::PromoteIntOp_VP_REDUCE(SDNode *N, unsigned OpNo) {
2836 SDLoc DL(N);
2837 SDValue Op = N->getOperand(OpNo);
2838 SmallVector<SDValue, 4> NewOps(N->ops());
2839
2840 if (OpNo == 2) { // Mask
2841 // Update in place.
2842 NewOps[2] = PromoteTargetBoolean(Op, N->getOperand(1).getValueType());
2843 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2844 }
2845
2846 assert(OpNo == 1 && "Unexpected operand for promotion");
2847
2848 Op = PromoteIntOpVectorReduction(N, Op);
2849
2850 NewOps[OpNo] = Op;
2851
2852 EVT VT = N->getValueType(0);
2853 EVT EltVT = Op.getValueType().getScalarType();
2854
2855 if (VT.bitsGE(EltVT))
2856 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, NewOps);
2857
2858 // Result size must be >= element/start-value size. If this is not the case
2859 // after promotion, also promote both the start value and result type and
2860 // then truncate.
2861 NewOps[0] =
2862 DAG.getNode(getExtendForIntVecReduction(N), DL, EltVT, N->getOperand(0));
2863 SDValue Reduce = DAG.getNode(N->getOpcode(), DL, EltVT, NewOps);
2864 return DAG.getNode(ISD::TRUNCATE, DL, VT, Reduce);
2865}
2866
2867SDValue DAGTypeLegalizer::PromoteIntOp_SET_ROUNDING(SDNode *N) {
2868 SDValue Op = ZExtPromotedInteger(N->getOperand(1));
2869 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op), 0);
2870}
2871
2872SDValue DAGTypeLegalizer::PromoteIntOp_STACKMAP(SDNode *N, unsigned OpNo) {
2873 assert(OpNo > 1); // Because the first two arguments are guaranteed legal.
2874 SmallVector<SDValue> NewOps(N->ops());
2875 NewOps[OpNo] = GetPromotedInteger(NewOps[OpNo]);
2876 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2877}
2878
2879SDValue DAGTypeLegalizer::PromoteIntOp_PATCHPOINT(SDNode *N, unsigned OpNo) {
2880 assert(OpNo >= 7);
2881 SmallVector<SDValue> NewOps(N->ops());
2882 NewOps[OpNo] = GetPromotedInteger(NewOps[OpNo]);
2883 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2884}
2885
2886SDValue DAGTypeLegalizer::PromoteIntOp_WRITE_REGISTER(SDNode *N,
2887 unsigned OpNo) {
2888 const Function &Fn = DAG.getMachineFunction().getFunction();
2889 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
2890 "cannot use llvm.write_register with illegal type", Fn,
2891 N->getDebugLoc()));
2892 return N->getOperand(0);
2893}
2894
2895SDValue DAGTypeLegalizer::PromoteIntOp_VP_STRIDED(SDNode *N, unsigned OpNo) {
2896 assert((N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD && OpNo == 3) ||
2897 (N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE && OpNo == 4));
2898
2899 SmallVector<SDValue, 8> NewOps(N->ops());
2900 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
2901
2902 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2903}
2904
2905SDValue DAGTypeLegalizer::PromoteIntOp_VP_SPLICE(SDNode *N, unsigned OpNo) {
2906 SmallVector<SDValue, 6> NewOps(N->ops());
2907
2908 if (OpNo == 2) { // Offset operand
2909 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
2910 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2911 }
2912
2913 assert((OpNo == 4 || OpNo == 5) && "Unexpected operand for promotion");
2914
2915 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
2916 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2917}
2918
2919SDValue DAGTypeLegalizer::PromoteIntOp_VECTOR_HISTOGRAM(SDNode *N,
2920 unsigned OpNo) {
2921 assert(OpNo == 1 && "Unexpected operand for promotion");
2922 SmallVector<SDValue, 7> NewOps(N->ops());
2923 NewOps[1] = GetPromotedInteger(N->getOperand(1));
2924 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2925}
2926
2927SDValue DAGTypeLegalizer::PromoteIntOp_VECTOR_FIND_LAST_ACTIVE(SDNode *N,
2928 unsigned OpNo) {
2929 SmallVector<SDValue, 1> NewOps(N->ops());
2930 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
2931 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2932}
2933
2934SDValue DAGTypeLegalizer::PromoteIntOp_GET_ACTIVE_LANE_MASK(SDNode *N) {
2935 SmallVector<SDValue, 1> NewOps(N->ops());
2936 NewOps[0] = ZExtPromotedInteger(N->getOperand(0));
2937 NewOps[1] = ZExtPromotedInteger(N->getOperand(1));
2938 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2939}
2940
2941SDValue DAGTypeLegalizer::PromoteIntOp_PARTIAL_REDUCE_MLA(SDNode *N) {
2942 SmallVector<SDValue, 1> NewOps(N->ops());
2943 switch (N->getOpcode()) {
2944 case ISD::PARTIAL_REDUCE_SMLA:
2945 NewOps[1] = SExtPromotedInteger(N->getOperand(1));
2946 NewOps[2] = SExtPromotedInteger(N->getOperand(2));
2947 break;
2948 case ISD::PARTIAL_REDUCE_UMLA:
2949 NewOps[1] = ZExtPromotedInteger(N->getOperand(1));
2950 NewOps[2] = ZExtPromotedInteger(N->getOperand(2));
2951 break;
2952 case ISD::PARTIAL_REDUCE_SUMLA:
2953 NewOps[1] = SExtPromotedInteger(N->getOperand(1));
2954 NewOps[2] = ZExtPromotedInteger(N->getOperand(2));
2955 break;
2956 default:
2957 llvm_unreachable("unexpected opcode");
2958 }
2959 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2960}
2961
2962//===----------------------------------------------------------------------===//
2963// Integer Result Expansion
2964//===----------------------------------------------------------------------===//
2965
2966/// ExpandIntegerResult - This method is called when the specified result of the
2967/// specified node is found to need expansion. At this point, the node may also
2968/// have invalid operands or may have other results that need promotion, we just
2969/// know that (at least) one result needs expansion.
2970void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
2971 LLVM_DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG));
2972 SDValue Lo, Hi;
2973 Lo = Hi = SDValue();
2974
2975 // See if the target wants to custom expand this node.
2976 if (CustomLowerNode(N, N->getValueType(ResNo), true))
2977 return;
2978
2979 switch (N->getOpcode()) {
2980 default:
2981#ifndef NDEBUG
2982 dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
2983 N->dump(&DAG); dbgs() << "\n";
2984#endif
2985 report_fatal_error("Do not know how to expand the result of this "
2986 "operator!");
2987
2988 case ISD::ARITH_FENCE: SplitRes_ARITH_FENCE(N, Lo, Hi); break;
2989 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
2990 case ISD::SELECT: SplitRes_Select(N, Lo, Hi); break;
2991 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
2992 case ISD::POISON:
2993 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
2994 case ISD::FREEZE: SplitRes_FREEZE(N, Lo, Hi); break;
2995 case ISD::SETCC: ExpandIntRes_SETCC(N, Lo, Hi); break;
2996
2997 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
2998 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
2999 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
3000 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
3001 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
3002
3003 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
3004 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
3005 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
3006 case ISD::BITREVERSE: ExpandIntRes_BITREVERSE(N, Lo, Hi); break;
3007 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
3008 case ISD::PARITY: ExpandIntRes_PARITY(N, Lo, Hi); break;
3009 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
3010 case ISD::ABS: ExpandIntRes_ABS(N, Lo, Hi); break;
3011 case ISD::ABDS:
3012 case ISD::ABDU: ExpandIntRes_ABD(N, Lo, Hi); break;
3014 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
3015 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
3017 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
3018 case ISD::GET_ROUNDING:ExpandIntRes_GET_ROUNDING(N, Lo, Hi); break;
3020 case ISD::FP_TO_SINT:
3022 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_XINT(N, Lo, Hi); break;
3024 case ISD::FP_TO_UINT_SAT: ExpandIntRes_FP_TO_XINT_SAT(N, Lo, Hi); break;
3025 case ISD::STRICT_LROUND:
3026 case ISD::STRICT_LRINT:
3027 case ISD::LROUND:
3028 case ISD::LRINT:
3030 case ISD::STRICT_LLRINT:
3031 case ISD::LLROUND:
3032 case ISD::LLRINT: ExpandIntRes_XROUND_XRINT(N, Lo, Hi); break;
3033 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
3034 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
3035 case ISD::READCYCLECOUNTER:
3036 case ISD::READSTEADYCOUNTER: ExpandIntRes_READCOUNTER(N, Lo, Hi); break;
3037 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
3038 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
3039 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
3040 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
3041 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
3042 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
3043 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
3044 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
3045 case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
3046
3047 case ISD::ATOMIC_LOAD_ADD:
3048 case ISD::ATOMIC_LOAD_SUB:
3049 case ISD::ATOMIC_LOAD_AND:
3050 case ISD::ATOMIC_LOAD_CLR:
3051 case ISD::ATOMIC_LOAD_OR:
3052 case ISD::ATOMIC_LOAD_XOR:
3053 case ISD::ATOMIC_LOAD_NAND:
3054 case ISD::ATOMIC_LOAD_MIN:
3055 case ISD::ATOMIC_LOAD_MAX:
3056 case ISD::ATOMIC_LOAD_UMIN:
3057 case ISD::ATOMIC_LOAD_UMAX:
3058 case ISD::ATOMIC_SWAP:
3059 case ISD::ATOMIC_CMP_SWAP: {
3060 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
3061 SplitInteger(Tmp.first, Lo, Hi);
3062 ReplaceValueWith(SDValue(N, 1), Tmp.second);
3063 break;
3064 }
3065 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
3066 AtomicSDNode *AN = cast<AtomicSDNode>(N);
3067 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other);
3068 SDValue Tmp = DAG.getAtomicCmpSwap(
3069 ISD::ATOMIC_CMP_SWAP, SDLoc(N), AN->getMemoryVT(), VTs,
3070 N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3),
3071 AN->getMemOperand());
3072
3073 // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
3074 // success simply by comparing the loaded value against the ingoing
3075 // comparison.
3076 SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp,
3077 N->getOperand(2), ISD::SETEQ);
3078
3079 SplitInteger(Tmp, Lo, Hi);
3080 ReplaceValueWith(SDValue(N, 1), Success);
3081 ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1));
3082 break;
3083 }
3084
3085 case ISD::AND:
3086 case ISD::OR:
3087 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
3088
3089 case ISD::UMAX:
3090 case ISD::SMAX:
3091 case ISD::UMIN:
3092 case ISD::SMIN: ExpandIntRes_MINMAX(N, Lo, Hi); break;
3093
3094 case ISD::SCMP:
3095 case ISD::UCMP: ExpandIntRes_CMP(N, Lo, Hi); break;
3096
3097 case ISD::ADD:
3098 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
3099
3100 case ISD::ADDC:
3101 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
3102
3103 case ISD::ADDE:
3104 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
3105
3106 case ISD::UADDO_CARRY:
3107 case ISD::USUBO_CARRY: ExpandIntRes_UADDSUBO_CARRY(N, Lo, Hi); break;
3108
3109 case ISD::SADDO_CARRY:
3110 case ISD::SSUBO_CARRY: ExpandIntRes_SADDSUBO_CARRY(N, Lo, Hi); break;
3111
3112 case ISD::SHL:
3113 case ISD::SRA:
3114 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
3115
3116 case ISD::SADDO:
3117 case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
3118 case ISD::UADDO:
3119 case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
3120 case ISD::UMULO:
3121 case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
3122
3123 case ISD::SADDSAT:
3124 case ISD::UADDSAT:
3125 case ISD::SSUBSAT:
3126 case ISD::USUBSAT: ExpandIntRes_ADDSUBSAT(N, Lo, Hi); break;
3127
3128 case ISD::SSHLSAT:
3129 case ISD::USHLSAT: ExpandIntRes_SHLSAT(N, Lo, Hi); break;
3130
3131 case ISD::AVGCEILS:
3132 case ISD::AVGCEILU:
3133 case ISD::AVGFLOORS:
3134 case ISD::AVGFLOORU: ExpandIntRes_AVG(N, Lo, Hi); break;
3135
3136 case ISD::SMULFIX:
3137 case ISD::SMULFIXSAT:
3138 case ISD::UMULFIX:
3139 case ISD::UMULFIXSAT: ExpandIntRes_MULFIX(N, Lo, Hi); break;
3140
3141 case ISD::SDIVFIX:
3142 case ISD::SDIVFIXSAT:
3143 case ISD::UDIVFIX:
3144 case ISD::UDIVFIXSAT: ExpandIntRes_DIVFIX(N, Lo, Hi); break;
3145
3146 case ISD::VECREDUCE_ADD:
3147 case ISD::VECREDUCE_MUL:
3148 case ISD::VECREDUCE_AND:
3149 case ISD::VECREDUCE_OR:
3150 case ISD::VECREDUCE_XOR:
3151 case ISD::VECREDUCE_SMAX:
3152 case ISD::VECREDUCE_SMIN:
3153 case ISD::VECREDUCE_UMAX:
3154 case ISD::VECREDUCE_UMIN: ExpandIntRes_VECREDUCE(N, Lo, Hi); break;
3155
3156 case ISD::ROTL:
3157 case ISD::ROTR:
3158 ExpandIntRes_Rotate(N, Lo, Hi);
3159 break;
3160
3161 case ISD::FSHL:
3162 case ISD::FSHR:
3163 ExpandIntRes_FunnelShift(N, Lo, Hi);
3164 break;
3165
3166 case ISD::VSCALE:
3167 ExpandIntRes_VSCALE(N, Lo, Hi);
3168 break;
3169
3170 case ISD::READ_REGISTER:
3171 ExpandIntRes_READ_REGISTER(N, Lo, Hi);
3172 break;
3173 }
3174
3175 // If Lo/Hi is null, the sub-method took care of registering results etc.
3176 if (Lo.getNode())
3177 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
3178}
3179
3180/// Lower an atomic node to the appropriate builtin call.
3181std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
3182 unsigned Opc = Node->getOpcode();
3183 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
3184 AtomicOrdering order = cast<AtomicSDNode>(Node)->getMergedOrdering();
3185 // Lower to outline atomic libcall if outline atomics enabled,
3186 // or to sync libcall otherwise
3187 RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, order, VT);
3188 EVT RetVT = Node->getValueType(0);
3189 TargetLowering::MakeLibCallOptions CallOptions;
3191
3192 RTLIB::LibcallImpl LCImpl = TLI.getLibcallImpl(LC);
3193 if (LCImpl != RTLIB::Unsupported) {
3194 Ops.append(Node->op_begin() + 2, Node->op_end());
3195 Ops.push_back(Node->getOperand(1));
3196 } else {
3197 LC = RTLIB::getSYNC(Opc, VT);
3198 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
3199 "Unexpected atomic op or value type!");
3200 Ops.append(Node->op_begin() + 1, Node->op_end());
3201 LCImpl = TLI.getLibcallImpl(LC);
3202 }
3203 return TLI.makeLibCall(DAG, LCImpl, RetVT, Ops, CallOptions, SDLoc(Node),
3204 Node->getOperand(0));
3205}
3206
3207/// N is a shift by a value that needs to be expanded,
3208/// and the shift amount is a constant 'Amt'. Expand the operation.
3209void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, const APInt &Amt,
3210 SDValue &Lo, SDValue &Hi) {
3211 SDLoc DL(N);
3212 // Expand the incoming operand to be shifted, so that we have its parts
3213 SDValue InL, InH;
3214 GetExpandedInteger(N->getOperand(0), InL, InH);
3215
3216 // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
3217 // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
3218 if (!Amt) {
3219 Lo = InL;
3220 Hi = InH;
3221 return;
3222 }
3223
3224 EVT NVT = InL.getValueType();
3225 unsigned VTBits = N->getValueType(0).getSizeInBits();
3226 unsigned NVTBits = NVT.getSizeInBits();
3227
3228 if (N->getOpcode() == ISD::SHL) {
3229 if (Amt.uge(VTBits)) {
3230 Lo = Hi = DAG.getConstant(0, DL, NVT);
3231 } else if (Amt.ugt(NVTBits)) {
3232 Lo = DAG.getConstant(0, DL, NVT);
3233 Hi = DAG.getNode(ISD::SHL, DL, NVT, InL,
3234 DAG.getShiftAmountConstant(Amt - NVTBits, NVT, DL));
3235 } else if (Amt == NVTBits) {
3236 Lo = DAG.getConstant(0, DL, NVT);
3237 Hi = InL;
3238 } else {
3239 Lo = DAG.getNode(ISD::SHL, DL, NVT, InL,
3240 DAG.getShiftAmountConstant(Amt, NVT, DL));
3241 Hi = DAG.getNode(
3242 ISD::OR, DL, NVT,
3243 DAG.getNode(ISD::SHL, DL, NVT, InH,
3244 DAG.getShiftAmountConstant(Amt, NVT, DL)),
3245 DAG.getNode(ISD::SRL, DL, NVT, InL,
3246 DAG.getShiftAmountConstant(-Amt + NVTBits, NVT, DL)));
3247 }
3248 return;
3249 }
3250
3251 if (N->getOpcode() == ISD::SRL) {
3252 if (Amt.uge(VTBits)) {
3253 Lo = Hi = DAG.getConstant(0, DL, NVT);
3254 } else if (Amt.ugt(NVTBits)) {
3255 Lo = DAG.getNode(ISD::SRL, DL, NVT, InH,
3256 DAG.getShiftAmountConstant(Amt - NVTBits, NVT, DL));
3257 Hi = DAG.getConstant(0, DL, NVT);
3258 } else if (Amt == NVTBits) {
3259 Lo = InH;
3260 Hi = DAG.getConstant(0, DL, NVT);
3261 } else {
3262 Lo = DAG.getNode(
3263 ISD::OR, DL, NVT,
3264 DAG.getNode(ISD::SRL, DL, NVT, InL,
3265 DAG.getShiftAmountConstant(Amt, NVT, DL)),
3266 DAG.getNode(ISD::SHL, DL, NVT, InH,
3267 DAG.getShiftAmountConstant(-Amt + NVTBits, NVT, DL)));
3268 Hi = DAG.getNode(ISD::SRL, DL, NVT, InH,
3269 DAG.getShiftAmountConstant(Amt, NVT, DL));
3270 }
3271 return;
3272 }
3273
3274 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
3275 if (Amt.uge(VTBits)) {
3276 Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
3277 DAG.getShiftAmountConstant(NVTBits - 1, NVT, DL));
3278 } else if (Amt.ugt(NVTBits)) {
3279 Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
3280 DAG.getShiftAmountConstant(Amt - NVTBits, NVT, DL));
3281 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
3282 DAG.getShiftAmountConstant(NVTBits - 1, NVT, DL));
3283 } else if (Amt == NVTBits) {
3284 Lo = InH;
3285 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
3286 DAG.getShiftAmountConstant(NVTBits - 1, NVT, DL));
3287 } else {
3288 Lo = DAG.getNode(
3289 ISD::OR, DL, NVT,
3290 DAG.getNode(ISD::SRL, DL, NVT, InL,
3291 DAG.getShiftAmountConstant(Amt, NVT, DL)),
3292 DAG.getNode(ISD::SHL, DL, NVT, InH,
3293 DAG.getShiftAmountConstant(-Amt + NVTBits, NVT, DL)));
3294 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
3295 DAG.getShiftAmountConstant(Amt, NVT, DL));
3296 }
3297}
3298
3299/// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
3300/// this shift based on knowledge of the high bit of the shift amount. If we
3301/// can tell this, we know that it is >= 32 or < 32, without knowing the actual
3302/// shift amount.
3303bool DAGTypeLegalizer::
3304ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
3305 unsigned Opc = N->getOpcode();
3306 SDValue In = N->getOperand(0);
3307 SDValue Amt = N->getOperand(1);
3308 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3309 EVT ShTy = Amt.getValueType();
3310 unsigned ShBits = ShTy.getScalarSizeInBits();
3311 unsigned NVTBits = NVT.getScalarSizeInBits();
3312 assert(isPowerOf2_32(NVTBits) &&
3313 "Expanded integer type size not a power of two!");
3314 SDLoc dl(N);
3315
3316 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
3317 KnownBits Known = DAG.computeKnownBits(Amt);
3318
3319 // If we don't know anything about the high bits, exit.
3320 if (((Known.Zero | Known.One) & HighBitMask) == 0)
3321 return false;
3322
3323 // Get the incoming operand to be shifted.
3324 SDValue InL, InH;
3325 GetExpandedInteger(In, InL, InH);
3326
3327 // If we know that any of the high bits of the shift amount are one, then we
3328 // can do this as a couple of simple shifts.
3329 if (Known.One.intersects(HighBitMask)) {
3330 // Mask out the high bit, which we know is set.
3331 Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
3332 DAG.getConstant(~HighBitMask, dl, ShTy));
3333
3334 switch (Opc) {
3335 default: llvm_unreachable("Unknown shift");
3336 case ISD::SHL:
3337 Lo = DAG.getConstant(0, dl, NVT); // Low part is zero.
3338 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
3339 return true;
3340 case ISD::SRL:
3341 Hi = DAG.getConstant(0, dl, NVT); // Hi part is zero.
3342 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
3343 return true;
3344 case ISD::SRA:
3345 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
3346 DAG.getConstant(NVTBits - 1, dl, ShTy));
3347 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
3348 return true;
3349 }
3350 }
3351
3352 // If we know that all of the high bits of the shift amount are zero, then we
3353 // can do this as a couple of simple shifts.
3354 if (HighBitMask.isSubsetOf(Known.Zero)) {
3355 // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
3356 // shift if x is zero. We can use XOR here because x is known to be smaller
3357 // than 32.
3358 SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
3359 DAG.getConstant(NVTBits - 1, dl, ShTy));
3360
3361 unsigned Op1, Op2;
3362 switch (Opc) {
3363 default: llvm_unreachable("Unknown shift");
3364 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
3365 case ISD::SRL:
3366 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
3367 }
3368
3369 // When shifting right the arithmetic for Lo and Hi is swapped.
3370 if (Opc != ISD::SHL)
3371 std::swap(InL, InH);
3372
3373 // Use a little trick to get the bits that move from Lo to Hi. First
3374 // shift by one bit.
3375 SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, dl, ShTy));
3376 // Then compute the remaining shift with amount-1.
3377 SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
3378
3379 Lo = DAG.getNode(Opc, dl, NVT, InL, Amt);
3380 Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
3381
3382 if (Opc != ISD::SHL)
3383 std::swap(Hi, Lo);
3384 return true;
3385 }
3386
3387 return false;
3388}
3389
3390/// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
3391/// of any size.
3392bool DAGTypeLegalizer::
3393ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
3394 SDValue Amt = N->getOperand(1);
3395 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3396 EVT ShTy = Amt.getValueType();
3397 unsigned NVTBits = NVT.getSizeInBits();
3398 assert(isPowerOf2_32(NVTBits) &&
3399 "Expanded integer type size not a power of two!");
3400 SDLoc dl(N);
3401
3402 // Get the incoming operand to be shifted.
3403 SDValue InL, InH;
3404 GetExpandedInteger(N->getOperand(0), InL, InH);
3405
3406 SDValue NVBitsNode = DAG.getConstant(NVTBits, dl, ShTy);
3407 SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
3408 SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
3409 SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
3410 Amt, NVBitsNode, ISD::SETULT);
3411 SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(ShTy),
3412 Amt, DAG.getConstant(0, dl, ShTy),
3413 ISD::SETEQ);
3414
3415 SDValue LoS, HiS, LoL, HiL;
3416 switch (N->getOpcode()) {
3417 default: llvm_unreachable("Unknown shift");
3418 case ISD::SHL:
3419 // Short: ShAmt < NVTBits
3420 LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
3421 HiS = DAG.getNode(ISD::OR, dl, NVT,
3422 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
3423 DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
3424
3425 // Long: ShAmt >= NVTBits
3426 LoL = DAG.getConstant(0, dl, NVT); // Lo part is zero.
3427 HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
3428
3429 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
3430 Hi = DAG.getSelect(dl, NVT, isZero, InH,
3431 DAG.getSelect(dl, NVT, isShort, HiS, HiL));
3432 return true;
3433 case ISD::SRL:
3434 // Short: ShAmt < NVTBits
3435 HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
3436 LoS = DAG.getNode(ISD::OR, dl, NVT,
3437 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
3438 // FIXME: If Amt is zero, the following shift generates an undefined result
3439 // on some architectures.
3440 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
3441
3442 // Long: ShAmt >= NVTBits
3443 HiL = DAG.getConstant(0, dl, NVT); // Hi part is zero.
3444 LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
3445
3446 Lo = DAG.getSelect(dl, NVT, isZero, InL,
3447 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
3448 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
3449 return true;
3450 case ISD::SRA:
3451 // Short: ShAmt < NVTBits
3452 HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
3453 LoS = DAG.getNode(ISD::OR, dl, NVT,
3454 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
3455 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
3456
3457 // Long: ShAmt >= NVTBits
3458 HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
3459 DAG.getConstant(NVTBits - 1, dl, ShTy));
3460 LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
3461
3462 Lo = DAG.getSelect(dl, NVT, isZero, InL,
3463 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
3464 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
3465 return true;
3466 }
3467}
3468
3469static std::pair<ISD::CondCode, ISD::NodeType> getExpandedMinMaxOps(int Op) {
3470
3471 switch (Op) {
3472 default: llvm_unreachable("invalid min/max opcode");
3473 case ISD::SMAX:
3474 return std::make_pair(ISD::SETGT, ISD::UMAX);
3475 case ISD::UMAX:
3476 return std::make_pair(ISD::SETUGT, ISD::UMAX);
3477 case ISD::SMIN:
3478 return std::make_pair(ISD::SETLT, ISD::UMIN);
3479 case ISD::UMIN:
3480 return std::make_pair(ISD::SETULT, ISD::UMIN);
3481 }
3482}
3483
3484void DAGTypeLegalizer::ExpandIntRes_SETCC(SDNode *N, SDValue &Lo, SDValue &Hi) {
3485 SDLoc DL(N);
3486
3487 SDValue LHS = N->getOperand(0);
3488 SDValue RHS = N->getOperand(1);
3489 EVT NewVT = getSetCCResultType(LHS.getValueType());
3490
3491 // Taking the same approach as ScalarizeVecRes_SETCC
3492 SDValue Res = DAG.getNode(ISD::SETCC, DL, NewVT, LHS, RHS, N->getOperand(2));
3493
3494 Res = DAG.getBoolExtOrTrunc(Res, DL, N->getValueType(0), NewVT);
3495 SplitInteger(Res, Lo, Hi);
3496}
3497
3498void DAGTypeLegalizer::ExpandIntRes_MINMAX(SDNode *N,
3499 SDValue &Lo, SDValue &Hi) {
3500 SDLoc DL(N);
3501
3502 SDValue LHS = N->getOperand(0);
3503 SDValue RHS = N->getOperand(1);
3504
3505 // If the upper halves are all sign bits, then we can perform the MINMAX on
3506 // the lower half and sign-extend the result to the upper half.
3507 unsigned NumBits = N->getValueType(0).getScalarSizeInBits();
3508 unsigned NumHalfBits = NumBits / 2;
3509 if (DAG.ComputeNumSignBits(LHS) > NumHalfBits &&
3510 DAG.ComputeNumSignBits(RHS) > NumHalfBits) {
3511 SDValue LHSL, LHSH, RHSL, RHSH;
3512 GetExpandedInteger(LHS, LHSL, LHSH);
3513 GetExpandedInteger(RHS, RHSL, RHSH);
3514 EVT NVT = LHSL.getValueType();
3515
3516 Lo = DAG.getNode(N->getOpcode(), DL, NVT, LHSL, RHSL);
3517 Hi = DAG.getNode(ISD::SRA, DL, NVT, Lo,
3518 DAG.getShiftAmountConstant(NumHalfBits - 1, NVT, DL));
3519 return;
3520 }
3521
3522 // The Lo of smin(X, -1) is LHSL if X is negative. Otherwise it's -1.
3523 // The Lo of smax(X, 0) is 0 if X is negative. Otherwise it's LHSL.
3524 if ((N->getOpcode() == ISD::SMAX && isNullConstant(RHS)) ||
3525 (N->getOpcode() == ISD::SMIN && isAllOnesConstant(RHS))) {
3526 SDValue LHSL, LHSH, RHSL, RHSH;
3527 GetExpandedInteger(LHS, LHSL, LHSH);
3528 GetExpandedInteger(RHS, RHSL, RHSH);
3529 EVT NVT = LHSL.getValueType();
3530 EVT CCT = getSetCCResultType(NVT);
3531
3532 SDValue HiNeg =
3533 DAG.getSetCC(DL, CCT, LHSH, DAG.getConstant(0, DL, NVT), ISD::SETLT);
3534 if (N->getOpcode() == ISD::SMIN) {
3535 Lo = DAG.getSelect(DL, NVT, HiNeg, LHSL, DAG.getAllOnesConstant(DL, NVT));
3536 } else {
3537 Lo = DAG.getSelect(DL, NVT, HiNeg, DAG.getConstant(0, DL, NVT), LHSL);
3538 }
3539 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
3540 return;
3541 }
3542
3543 const APInt *RHSVal = nullptr;
3544 if (auto *RHSConst = dyn_cast<ConstantSDNode>(RHS))
3545 RHSVal = &RHSConst->getAPIntValue();
3546
3547 // The high half of MIN/MAX is always just the the MIN/MAX of the
3548 // high halves of the operands. Expand this way if it appears profitable.
3549 if (RHSVal && (N->getOpcode() == ISD::UMIN || N->getOpcode() == ISD::UMAX) &&
3550 (RHSVal->countLeadingOnes() >= NumHalfBits ||
3551 RHSVal->countLeadingZeros() >= NumHalfBits)) {
3552 SDValue LHSL, LHSH, RHSL, RHSH;
3553 GetExpandedInteger(LHS, LHSL, LHSH);
3554 GetExpandedInteger(RHS, RHSL, RHSH);
3555 EVT NVT = LHSL.getValueType();
3556 EVT CCT = getSetCCResultType(NVT);
3557
3558 ISD::NodeType LoOpc;
3559 ISD::CondCode CondC;
3560 std::tie(CondC, LoOpc) = getExpandedMinMaxOps(N->getOpcode());
3561
3562 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
3563 // We need to know whether to select Lo part that corresponds to 'winning'
3564 // Hi part or if Hi parts are equal.
3565 SDValue IsHiLeft = DAG.getSetCC(DL, CCT, LHSH, RHSH, CondC);
3566 SDValue IsHiEq = DAG.getSetCC(DL, CCT, LHSH, RHSH, ISD::SETEQ);
3567
3568 // Lo part corresponding to the 'winning' Hi part
3569 SDValue LoCmp = DAG.getSelect(DL, NVT, IsHiLeft, LHSL, RHSL);
3570
3571 // Recursed Lo part if Hi parts are equal, this uses unsigned version
3572 SDValue LoMinMax = DAG.getNode(LoOpc, DL, NVT, {LHSL, RHSL});
3573
3574 Lo = DAG.getSelect(DL, NVT, IsHiEq, LoMinMax, LoCmp);
3575 return;
3576 }
3577
3578 // Expand to "a < b ? a : b" etc. Prefer ge/le if that simplifies
3579 // the compare.
3580 ISD::CondCode Pred;
3581 switch (N->getOpcode()) {
3582 default: llvm_unreachable("How did we get here?");
3583 case ISD::SMAX:
3584 if (RHSVal && RHSVal->countTrailingZeros() >= NumHalfBits)
3585 Pred = ISD::SETGE;
3586 else
3587 Pred = ISD::SETGT;
3588 break;
3589 case ISD::SMIN:
3590 if (RHSVal && RHSVal->countTrailingOnes() >= NumHalfBits)
3591 Pred = ISD::SETLE;
3592 else
3593 Pred = ISD::SETLT;
3594 break;
3595 case ISD::UMAX:
3596 if (RHSVal && RHSVal->countTrailingZeros() >= NumHalfBits)
3597 Pred = ISD::SETUGE;
3598 else
3599 Pred = ISD::SETUGT;
3600 break;
3601 case ISD::UMIN:
3602 if (RHSVal && RHSVal->countTrailingOnes() >= NumHalfBits)
3603 Pred = ISD::SETULE;
3604 else
3605 Pred = ISD::SETULT;
3606 break;
3607 }
3608 EVT VT = N->getValueType(0);
3609 EVT CCT = getSetCCResultType(VT);
3610 SDValue Cond = DAG.getSetCC(DL, CCT, LHS, RHS, Pred);
3611 SDValue Result = DAG.getSelect(DL, VT, Cond, LHS, RHS);
3612 SplitInteger(Result, Lo, Hi);
3613}
3614
3615void DAGTypeLegalizer::ExpandIntRes_CMP(SDNode *N, SDValue &Lo, SDValue &Hi) {
3616 SDValue ExpandedCMP = TLI.expandCMP(N, DAG);
3617 SplitInteger(ExpandedCMP, Lo, Hi);
3618}
3619
3620void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
3621 SDValue &Lo, SDValue &Hi) {
3622 SDLoc dl(N);
3623 // Expand the subcomponents.
3624 SDValue LHSL, LHSH, RHSL, RHSH;
3625 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3626 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3627
3628 EVT NVT = LHSL.getValueType();
3629 SDValue LoOps[2] = { LHSL, RHSL };
3630 SDValue HiOps[3] = { LHSH, RHSH };
3631
3632 bool HasOpCarry = TLI.isOperationLegalOrCustom(
3633 N->getOpcode() == ISD::ADD ? ISD::UADDO_CARRY : ISD::USUBO_CARRY,
3634 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3635 if (HasOpCarry) {
3636 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
3637 if (N->getOpcode() == ISD::ADD) {
3638 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
3639 HiOps[2] = Lo.getValue(1);
3640 Hi = DAG.computeKnownBits(HiOps[2]).isZero()
3641 ? DAG.getNode(ISD::ADD, dl, NVT, ArrayRef(HiOps, 2))
3642 : DAG.getNode(ISD::UADDO_CARRY, dl, VTList, HiOps);
3643 } else {
3644 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
3645 HiOps[2] = Lo.getValue(1);
3646 Hi = DAG.computeKnownBits(HiOps[2]).isZero()
3647 ? DAG.getNode(ISD::SUB, dl, NVT, ArrayRef(HiOps, 2))
3648 : DAG.getNode(ISD::USUBO_CARRY, dl, VTList, HiOps);
3649 }
3650 return;
3651 }
3652
3653 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
3654 // them. TODO: Teach operation legalization how to expand unsupported
3655 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
3656 // a carry of type MVT::Glue, but there doesn't seem to be any way to
3657 // generate a value of this type in the expanded code sequence.
3658 bool hasCarry =
3659 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
3661 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3662
3663 if (hasCarry) {
3664 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
3665 if (N->getOpcode() == ISD::ADD) {
3666 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
3667 HiOps[2] = Lo.getValue(1);
3668 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
3669 } else {
3670 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
3671 HiOps[2] = Lo.getValue(1);
3672 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
3673 }
3674 return;
3675 }
3676
3677 bool hasOVF =
3678 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
3680 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3681 TargetLoweringBase::BooleanContent BoolType = TLI.getBooleanContents(NVT);
3682
3683 if (hasOVF) {
3684 EVT OvfVT = getSetCCResultType(NVT);
3685 SDVTList VTList = DAG.getVTList(NVT, OvfVT);
3686 int RevOpc;
3687 if (N->getOpcode() == ISD::ADD) {
3688 RevOpc = ISD::SUB;
3689 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
3690 Hi = DAG.getNode(ISD::ADD, dl, NVT, ArrayRef(HiOps, 2));
3691 } else {
3692 RevOpc = ISD::ADD;
3693 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
3694 Hi = DAG.getNode(ISD::SUB, dl, NVT, ArrayRef(HiOps, 2));
3695 }
3696 SDValue OVF = Lo.getValue(1);
3697
3698 switch (BoolType) {
3700 OVF = DAG.getNode(ISD::AND, dl, OvfVT, DAG.getConstant(1, dl, OvfVT), OVF);
3701 [[fallthrough]];
3703 OVF = DAG.getZExtOrTrunc(OVF, dl, NVT);
3704 Hi = DAG.getNode(N->getOpcode(), dl, NVT, Hi, OVF);
3705 break;
3707 OVF = DAG.getSExtOrTrunc(OVF, dl, NVT);
3708 Hi = DAG.getNode(RevOpc, dl, NVT, Hi, OVF);
3709 }
3710 return;
3711 }
3712
3713 if (N->getOpcode() == ISD::ADD) {
3714 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps);
3715 SDValue Cmp;
3716 // Special case: X+1 has a carry out if X+1==0. This may reduce the live
3717 // range of X. We assume comparing with 0 is cheap.
3718 if (isOneConstant(LoOps[1]))
3719 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
3720 DAG.getConstant(0, dl, NVT), ISD::SETEQ);
3721 else if (isAllOnesConstant(LoOps[1])) {
3722 if (isAllOnesConstant(HiOps[1]))
3723 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), LoOps[0],
3724 DAG.getConstant(0, dl, NVT), ISD::SETEQ);
3725 else
3726 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), LoOps[0],
3727 DAG.getConstant(0, dl, NVT), ISD::SETNE);
3728 } else
3729 Cmp = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
3730 ISD::SETULT);
3731
3732 SDValue Carry;
3734 Carry = DAG.getZExtOrTrunc(Cmp, dl, NVT);
3735 else
3736 Carry = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
3737 DAG.getConstant(0, dl, NVT));
3738
3739 if (isAllOnesConstant(LoOps[1]) && isAllOnesConstant(HiOps[1])) {
3740 Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps[0], Carry);
3741 } else {
3742 Hi = DAG.getNode(ISD::ADD, dl, NVT, ArrayRef(HiOps, 2));
3743 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry);
3744 }
3745 } else {
3746 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps);
3747 Hi = DAG.getNode(ISD::SUB, dl, NVT, ArrayRef(HiOps, 2));
3748 SDValue Cmp =
3749 DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
3750 LoOps[0], LoOps[1], ISD::SETULT);
3751
3752 SDValue Borrow;
3754 Borrow = DAG.getZExtOrTrunc(Cmp, dl, NVT);
3755 else
3756 Borrow = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
3757 DAG.getConstant(0, dl, NVT));
3758
3759 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
3760 }
3761}
3762
3763void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
3764 SDValue &Lo, SDValue &Hi) {
3765 // Expand the subcomponents.
3766 SDValue LHSL, LHSH, RHSL, RHSH;
3767 SDLoc dl(N);
3768 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3769 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3770 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
3771 SDValue LoOps[2] = { LHSL, RHSL };
3772 SDValue HiOps[3] = { LHSH, RHSH };
3773
3774 if (N->getOpcode() == ISD::ADDC) {
3775 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
3776 HiOps[2] = Lo.getValue(1);
3777 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
3778 } else {
3779 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
3780 HiOps[2] = Lo.getValue(1);
3781 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
3782 }
3783
3784 // Legalized the flag result - switch anything that used the old flag to
3785 // use the new one.
3786 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
3787}
3788
3789void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
3790 SDValue &Lo, SDValue &Hi) {
3791 // Expand the subcomponents.
3792 SDValue LHSL, LHSH, RHSL, RHSH;
3793 SDLoc dl(N);
3794 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3795 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3796 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
3797 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
3798 SDValue HiOps[3] = { LHSH, RHSH };
3799
3800 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
3801 HiOps[2] = Lo.getValue(1);
3802 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
3803
3804 // Legalized the flag result - switch anything that used the old flag to
3805 // use the new one.
3806 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
3807}
3808
3809void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
3810 SDValue &Lo, SDValue &Hi) {
3811 SDValue LHS = N->getOperand(0);
3812 SDValue RHS = N->getOperand(1);
3813 SDLoc dl(N);
3814
3815 SDValue Ovf;
3816
3817 unsigned CarryOp, NoCarryOp;
3819 switch(N->getOpcode()) {
3820 case ISD::UADDO:
3821 CarryOp = ISD::UADDO_CARRY;
3822 NoCarryOp = ISD::ADD;
3823 Cond = ISD::SETULT;
3824 break;
3825 case ISD::USUBO:
3826 CarryOp = ISD::USUBO_CARRY;
3827 NoCarryOp = ISD::SUB;
3828 Cond = ISD::SETUGT;
3829 break;
3830 default:
3831 llvm_unreachable("Node has unexpected Opcode");
3832 }
3833
3834 bool HasCarryOp = TLI.isOperationLegalOrCustom(
3835 CarryOp, TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
3836
3837 if (HasCarryOp) {
3838 // Expand the subcomponents.
3839 SDValue LHSL, LHSH, RHSL, RHSH;
3840 GetExpandedInteger(LHS, LHSL, LHSH);
3841 GetExpandedInteger(RHS, RHSL, RHSH);
3842 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
3843 SDValue LoOps[2] = { LHSL, RHSL };
3844 SDValue HiOps[3] = { LHSH, RHSH };
3845
3846 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
3847 HiOps[2] = Lo.getValue(1);
3848 Hi = DAG.getNode(CarryOp, dl, VTList, HiOps);
3849
3850 Ovf = Hi.getValue(1);
3851 } else {
3852 // Expand the result by simply replacing it with the equivalent
3853 // non-overflow-checking operation.
3854 SDValue Sum = DAG.getNode(NoCarryOp, dl, LHS.getValueType(), LHS, RHS);
3855 SplitInteger(Sum, Lo, Hi);
3856
3857 if (N->getOpcode() == ISD::UADDO && isOneConstant(RHS)) {
3858 // Special case: uaddo X, 1 overflowed if X+1 == 0. We can detect this
3859 // with (Lo | Hi) == 0.
3860 SDValue Or = DAG.getNode(ISD::OR, dl, Lo.getValueType(), Lo, Hi);
3861 Ovf = DAG.getSetCC(dl, N->getValueType(1), Or,
3862 DAG.getConstant(0, dl, Lo.getValueType()), ISD::SETEQ);
3863 } else if (N->getOpcode() == ISD::UADDO && isAllOnesConstant(RHS)) {
3864 // Special case: uaddo X, -1 overflows if X == 0.
3865 Ovf =
3866 DAG.getSetCC(dl, N->getValueType(1), LHS,
3867 DAG.getConstant(0, dl, LHS.getValueType()), ISD::SETNE);
3868 } else {
3869 // Calculate the overflow: addition overflows iff a + b < a, and
3870 // subtraction overflows iff a - b > a.
3871 Ovf = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS, Cond);
3872 }
3873 }
3874
3875 // Legalized the flag result - switch anything that used the old flag to
3876 // use the new one.
3877 ReplaceValueWith(SDValue(N, 1), Ovf);
3878}
3879
3880void DAGTypeLegalizer::ExpandIntRes_UADDSUBO_CARRY(SDNode *N, SDValue &Lo,
3881 SDValue &Hi) {
3882 // Expand the subcomponents.
3883 SDValue LHSL, LHSH, RHSL, RHSH;
3884 SDLoc dl(N);
3885 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3886 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3887 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
3888 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
3889 SDValue HiOps[3] = { LHSH, RHSH, SDValue() };
3890
3891 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
3892 HiOps[2] = Lo.getValue(1);
3893 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
3894
3895 // Legalized the flag result - switch anything that used the old flag to
3896 // use the new one.
3897 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
3898}
3899
3900void DAGTypeLegalizer::ExpandIntRes_SADDSUBO_CARRY(SDNode *N,
3901 SDValue &Lo, SDValue &Hi) {
3902 // Expand the subcomponents.
3903 SDValue LHSL, LHSH, RHSL, RHSH;
3904 SDLoc dl(N);
3905 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3906 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3907 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
3908
3909 // We need to use an unsigned carry op for the lo part.
3910 unsigned CarryOp =
3912 Lo = DAG.getNode(CarryOp, dl, VTList, { LHSL, RHSL, N->getOperand(2) });
3913 Hi = DAG.getNode(N->getOpcode(), dl, VTList, { LHSH, RHSH, Lo.getValue(1) });
3914
3915 // Legalized the flag result - switch anything that used the old flag to
3916 // use the new one.
3917 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
3918}
3919
3920void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
3921 SDValue &Lo, SDValue &Hi) {
3922 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3923 SDLoc dl(N);
3924 SDValue Op = N->getOperand(0);
3925 if (Op.getValueType().bitsLE(NVT)) {
3926 // The low part is any extension of the input (which degenerates to a copy).
3927 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
3928 Hi = DAG.getUNDEF(NVT); // The high part is undefined.
3929 } else {
3930 // For example, extension of an i48 to an i64. The operand type necessarily
3931 // promotes to the result type, so will end up being expanded too.
3932 assert(getTypeAction(Op.getValueType()) ==
3934 "Only know how to promote this result!");
3935 SDValue Res = GetPromotedInteger(Op);
3936 assert(Res.getValueType() == N->getValueType(0) &&
3937 "Operand over promoted?");
3938 // Split the promoted operand. This will simplify when it is expanded.
3939 SplitInteger(Res, Lo, Hi);
3940 }
3941}
3942
3943void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
3944 SDValue &Lo, SDValue &Hi) {
3945 SDLoc dl(N);
3946 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3947 EVT NVT = Lo.getValueType();
3948 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3949 unsigned NVTBits = NVT.getSizeInBits();
3950 unsigned EVTBits = EVT.getSizeInBits();
3951
3952 if (NVTBits < EVTBits) {
3953 Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
3954 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
3955 EVTBits - NVTBits)));
3956 } else {
3957 Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
3958 // The high part replicates the sign bit of Lo, make it explicit.
3959 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
3960 DAG.getShiftAmountConstant(NVTBits - 1, NVT, dl));
3961 }
3962}
3963
3964void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
3965 SDValue &Lo, SDValue &Hi) {
3966 SDLoc dl(N);
3967 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3968 EVT NVT = Lo.getValueType();
3969 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3970 unsigned NVTBits = NVT.getSizeInBits();
3971 unsigned EVTBits = EVT.getSizeInBits();
3972
3973 if (NVTBits < EVTBits) {
3974 Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
3975 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
3976 EVTBits - NVTBits)));
3977 } else {
3978 Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
3979 // The high part must be zero, make it explicit.
3980 Hi = DAG.getConstant(0, dl, NVT);
3981 }
3982}
3983
3984void DAGTypeLegalizer::ExpandIntRes_BITREVERSE(SDNode *N,
3985 SDValue &Lo, SDValue &Hi) {
3986 SDLoc dl(N);
3987 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
3988 Lo = DAG.getNode(ISD::BITREVERSE, dl, Lo.getValueType(), Lo);
3989 Hi = DAG.getNode(ISD::BITREVERSE, dl, Hi.getValueType(), Hi);
3990}
3991
3992void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
3993 SDValue &Lo, SDValue &Hi) {
3994 SDLoc dl(N);
3995 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
3996 Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
3997 Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
3998}
3999
4000void DAGTypeLegalizer::ExpandIntRes_PARITY(SDNode *N, SDValue &Lo,
4001 SDValue &Hi) {
4002 SDLoc dl(N);
4003 // parity(HiLo) -> parity(Lo^Hi)
4004 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4005 EVT NVT = Lo.getValueType();
4006 Lo =
4007 DAG.getNode(ISD::PARITY, dl, NVT, DAG.getNode(ISD::XOR, dl, NVT, Lo, Hi));
4008 Hi = DAG.getConstant(0, dl, NVT);
4009}
4010
4011void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
4012 SDValue &Lo, SDValue &Hi) {
4013 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4014 unsigned NBitWidth = NVT.getSizeInBits();
4016 const APInt &Cst = Constant->getAPIntValue();
4017 bool IsTarget = Constant->isTargetOpcode();
4018 bool IsOpaque = Constant->isOpaque();
4019 SDLoc dl(N);
4020 Lo = DAG.getConstant(Cst.trunc(NBitWidth), dl, NVT, IsTarget, IsOpaque);
4021 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), dl, NVT, IsTarget,
4022 IsOpaque);
4023}
4024
4025void DAGTypeLegalizer::ExpandIntRes_ABS(SDNode *N, SDValue &Lo, SDValue &Hi) {
4026 SDLoc dl(N);
4027
4028 SDValue N0 = N->getOperand(0);
4029 GetExpandedInteger(N0, Lo, Hi);
4030 EVT NVT = Lo.getValueType();
4031
4032 // If the upper half is all sign bits, then we can perform the ABS on the
4033 // lower half and zero-extend.
4034 if (DAG.ComputeNumSignBits(N0) > NVT.getScalarSizeInBits()) {
4035 Lo = DAG.getNode(ISD::ABS, dl, NVT, Lo);
4036 Hi = DAG.getConstant(0, dl, NVT);
4037 return;
4038 }
4039
4040 // If we have USUBO_CARRY, use the expanded form of the sra+xor+sub sequence
4041 // we use in LegalizeDAG. The SUB part of the expansion is based on
4042 // ExpandIntRes_ADDSUB which also uses USUBO_CARRY/USUBO after checking that
4043 // USUBO_CARRY is LegalOrCustom. Each of the pieces here can be further
4044 // expanded if needed. Shift expansion has a special case for filling with
4045 // sign bits so that we will only end up with one SRA.
4046 bool HasSubCarry = TLI.isOperationLegalOrCustom(
4047 ISD::USUBO_CARRY, TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
4048 if (HasSubCarry) {
4049 SDValue Sign = DAG.getNode(
4050 ISD::SRA, dl, NVT, Hi,
4051 DAG.getShiftAmountConstant(NVT.getSizeInBits() - 1, NVT, dl));
4052 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
4053 Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Sign);
4054 Hi = DAG.getNode(ISD::XOR, dl, NVT, Hi, Sign);
4055 Lo = DAG.getNode(ISD::USUBO, dl, VTList, Lo, Sign);
4056 Hi = DAG.getNode(ISD::USUBO_CARRY, dl, VTList, Hi, Sign, Lo.getValue(1));
4057 return;
4058 }
4059
4060 // abs(HiLo) -> (Hi < 0 ? -HiLo : HiLo)
4061 EVT VT = N->getValueType(0);
4062 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT,
4063 DAG.getConstant(0, dl, VT), N0);
4064 SDValue NegLo, NegHi;
4065 SplitInteger(Neg, NegLo, NegHi);
4066
4067 SDValue HiIsNeg = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
4068 DAG.getConstant(0, dl, NVT), ISD::SETLT);
4069 Lo = DAG.getSelect(dl, NVT, HiIsNeg, NegLo, Lo);
4070 Hi = DAG.getSelect(dl, NVT, HiIsNeg, NegHi, Hi);
4071}
4072
4073void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
4074 SDValue &Lo, SDValue &Hi) {
4075 SDLoc dl(N);
4076 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
4077 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4078 EVT NVT = Lo.getValueType();
4079
4080 SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
4081 DAG.getConstant(0, dl, NVT), ISD::SETNE);
4082
4083 SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
4084 SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, NVT, Hi);
4085
4086 Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
4087 DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
4088 DAG.getConstant(NVT.getSizeInBits(), dl,
4089 NVT)));
4090 Hi = DAG.getConstant(0, dl, NVT);
4091}
4092
4093void DAGTypeLegalizer::ExpandIntRes_ABD(SDNode *N, SDValue &Lo, SDValue &Hi) {
4094 SDValue Result = TLI.expandABD(N, DAG);
4095 SplitInteger(Result, Lo, Hi);
4096}
4097
4098void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N, SDValue &Lo, SDValue &Hi) {
4099 SDValue Op = N->getOperand(0);
4100 EVT VT = N->getValueType(0);
4101 SDLoc DL(N);
4102
4103 if (TLI.getOperationAction(ISD::CTPOP, VT) == TargetLoweringBase::LibCall) {
4104 RTLIB::Libcall LC = RTLIB::getCTPOP(VT);
4105 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4106 "LibCall explicitly requested, but not available");
4107
4108 if (RTLIB::LibcallImpl LCImpl = TLI.getLibcallImpl(LC)) {
4109 TargetLowering::MakeLibCallOptions CallOptions;
4110 EVT IntVT =
4111 EVT::getIntegerVT(*DAG.getContext(), DAG.getLibInfo().getIntSize());
4112 SDValue Res =
4113 TLI.makeLibCall(DAG, LCImpl, IntVT, Op, CallOptions, DL).first;
4114 SplitInteger(DAG.getSExtOrTrunc(Res, DL, VT), Lo, Hi);
4115 return;
4116 }
4117
4118 // If the function is not available, fall back on the expansion.
4119 }
4120
4121 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
4122 GetExpandedInteger(Op, Lo, Hi);
4123 EVT NVT = Lo.getValueType();
4124 Lo = DAG.getNode(ISD::ADD, DL, NVT, DAG.getNode(ISD::CTPOP, DL, NVT, Lo),
4125 DAG.getNode(ISD::CTPOP, DL, NVT, Hi));
4126 Hi = DAG.getConstant(0, DL, NVT);
4127}
4128
4129void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
4130 SDValue &Lo, SDValue &Hi) {
4131 SDLoc dl(N);
4132 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
4133 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4134 EVT NVT = Lo.getValueType();
4135
4136 SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
4137 DAG.getConstant(0, dl, NVT), ISD::SETNE);
4138
4139 SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, NVT, Lo);
4140 SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
4141
4142 Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
4143 DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
4144 DAG.getConstant(NVT.getSizeInBits(), dl,
4145 NVT)));
4146 Hi = DAG.getConstant(0, dl, NVT);
4147}
4148
4149void DAGTypeLegalizer::ExpandIntRes_GET_ROUNDING(SDNode *N, SDValue &Lo,
4150 SDValue &Hi) {
4151 SDLoc dl(N);
4152 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4153 unsigned NBitWidth = NVT.getSizeInBits();
4154
4155 Lo = DAG.getNode(ISD::GET_ROUNDING, dl, {NVT, MVT::Other}, N->getOperand(0));
4156 SDValue Chain = Lo.getValue(1);
4157 // The high part is the sign of Lo, as -1 is a valid value for GET_ROUNDING
4158 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
4159 DAG.getShiftAmountConstant(NBitWidth - 1, NVT, dl));
4160
4161 // Legalize the chain result - switch anything that used the old chain to
4162 // use the new one.
4163 ReplaceValueWith(SDValue(N, 1), Chain);
4164}
4165
4166// Helper for producing an FP_EXTEND/STRICT_FP_EXTEND of Op.
4167static SDValue fpExtendHelper(SDValue Op, SDValue &Chain, bool IsStrict, EVT VT,
4168 SDLoc DL, SelectionDAG &DAG) {
4169 if (IsStrict) {
4170 Op = DAG.getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op});
4171 Chain = Op.getValue(1);
4172 return Op;
4173 }
4174 return DAG.getNode(ISD::FP_EXTEND, DL, VT, Op);
4175}
4176
4177void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT(SDNode *N, SDValue &Lo,
4178 SDValue &Hi) {
4179 SDLoc dl(N);
4180 EVT VT = N->getValueType(0);
4181
4182 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
4183 N->getOpcode() == ISD::STRICT_FP_TO_SINT;
4184 bool IsStrict = N->isStrictFPOpcode();
4185 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
4186 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
4187 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
4188 Op = GetPromotedFloat(Op);
4189
4190 // If the input is bf16 or needs to be soft promoted, extend to f32.
4191 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftPromoteHalf ||
4192 Op.getValueType() == MVT::bf16) {
4193 Op = fpExtendHelper(Op, Chain, IsStrict, MVT::f32, dl, DAG);
4194 }
4195
4196 // NOTE: We need a variable that lives across makeLibCall so
4197 // CallOptions.setTypeListBeforeSoften can save a reference to it.
4198 EVT OpVT = Op.getValueType();
4199
4200 RTLIB::Libcall LC =
4201 IsSigned ? RTLIB::getFPTOSINT(OpVT, VT) : RTLIB::getFPTOUINT(OpVT, VT);
4202 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-xint conversion!");
4203 TargetLowering::MakeLibCallOptions CallOptions;
4204 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftenFloat)
4205 CallOptions.setTypeListBeforeSoften(OpVT, VT);
4206 else
4207 CallOptions.setIsSigned(true); // FIXME: Is this needed?
4208 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, VT, Op,
4209 CallOptions, dl, Chain);
4210 SplitInteger(Tmp.first, Lo, Hi);
4211
4212 if (IsStrict)
4213 ReplaceValueWith(SDValue(N, 1), Tmp.second);
4214}
4215
4216void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT_SAT(SDNode *N, SDValue &Lo,
4217 SDValue &Hi) {
4218 SDValue Res = TLI.expandFP_TO_INT_SAT(N, DAG);
4219 SplitInteger(Res, Lo, Hi);
4220}
4221
4222void DAGTypeLegalizer::ExpandIntRes_XROUND_XRINT(SDNode *N, SDValue &Lo,
4223 SDValue &Hi) {
4224 SDLoc dl(N);
4225 bool IsStrict = N->isStrictFPOpcode();
4226 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
4227 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
4228
4229 assert(getTypeAction(Op.getValueType()) != TargetLowering::TypePromoteFloat &&
4230 "Input type needs to be promoted!");
4231
4232 EVT VT = Op.getValueType();
4233
4234 if (VT == MVT::f16) {
4235 // Extend to f32.
4236 VT = MVT::f32;
4237 Op = fpExtendHelper(Op, Chain, IsStrict, VT, dl, DAG);
4238 }
4239
4240 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4241 if (N->getOpcode() == ISD::LROUND ||
4242 N->getOpcode() == ISD::STRICT_LROUND) {
4243 LC = RTLIB::getLROUND(VT);
4244 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected lround input type!");
4245 } else if (N->getOpcode() == ISD::LRINT ||
4246 N->getOpcode() == ISD::STRICT_LRINT) {
4247 LC = RTLIB::getLRINT(VT);
4248 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected lrint input type!");
4249 } else if (N->getOpcode() == ISD::LLROUND ||
4250 N->getOpcode() == ISD::STRICT_LLROUND) {
4251 LC = RTLIB::getLLROUND(VT);
4252 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llround input type!");
4253 } else if (N->getOpcode() == ISD::LLRINT ||
4254 N->getOpcode() == ISD::STRICT_LLRINT) {
4255 LC = RTLIB::getLLRINT(VT);
4256 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llrint input type!");
4257 } else
4258 llvm_unreachable("Unexpected opcode!");
4259
4260 EVT RetVT = N->getValueType(0);
4261
4262 TargetLowering::MakeLibCallOptions CallOptions;
4263 CallOptions.setIsSigned(true);
4264 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
4265 Op, CallOptions, dl,
4266 Chain);
4267 SplitInteger(Tmp.first, Lo, Hi);
4268
4269 if (N->isStrictFPOpcode())
4270 ReplaceValueWith(SDValue(N, 1), Tmp.second);
4271}
4272
4273void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
4274 SDValue &Lo, SDValue &Hi) {
4275 assert(!N->isAtomic() && "Should have been a ATOMIC_LOAD?");
4276
4277 if (ISD::isNormalLoad(N)) {
4278 ExpandRes_NormalLoad(N, Lo, Hi);
4279 return;
4280 }
4281
4282 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
4283
4284 EVT VT = N->getValueType(0);
4285 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4286 SDValue Ch = N->getChain();
4287 SDValue Ptr = N->getBasePtr();
4288 ISD::LoadExtType ExtType = N->getExtensionType();
4289 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
4290 AAMDNodes AAInfo = N->getAAInfo();
4291 SDLoc dl(N);
4292
4293 assert(NVT.isByteSized() && "Expanded type not byte sized!");
4294
4295 if (N->getMemoryVT().bitsLE(NVT)) {
4296 EVT MemVT = N->getMemoryVT();
4297
4298 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(), MemVT,
4299 N->getBaseAlign(), MMOFlags, AAInfo);
4300
4301 // Remember the chain.
4302 Ch = Lo.getValue(1);
4303
4304 if (ExtType == ISD::SEXTLOAD) {
4305 // The high part is obtained by SRA'ing all but one of the bits of the
4306 // lo part.
4307 unsigned LoSize = Lo.getValueSizeInBits();
4308 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
4309 DAG.getShiftAmountConstant(LoSize - 1, NVT, dl));
4310 } else if (ExtType == ISD::ZEXTLOAD) {
4311 // The high part is just a zero.
4312 Hi = DAG.getConstant(0, dl, NVT);
4313 } else {
4314 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
4315 // The high part is undefined.
4316 Hi = DAG.getUNDEF(NVT);
4317 }
4318 } else if (DAG.getDataLayout().isLittleEndian()) {
4319 // Little-endian - low bits are at low addresses.
4320 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(), N->getBaseAlign(),
4321 MMOFlags, AAInfo);
4322
4323 unsigned ExcessBits =
4324 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
4325 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
4326
4327 // Increment the pointer to the other half.
4328 unsigned IncrementSize = NVT.getSizeInBits()/8;
4329 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
4330 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
4331 N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
4332 N->getBaseAlign(), MMOFlags, AAInfo);
4333
4334 // Build a factor node to remember that this load is independent of the
4335 // other one.
4336 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
4337 Hi.getValue(1));
4338 } else {
4339 // Big-endian - high bits are at low addresses. Favor aligned loads at
4340 // the cost of some bit-fiddling.
4341 EVT MemVT = N->getMemoryVT();
4342 unsigned EBytes = MemVT.getStoreSize();
4343 unsigned IncrementSize = NVT.getSizeInBits()/8;
4344 unsigned ExcessBits = (EBytes - IncrementSize)*8;
4345
4346 // Load both the high bits and maybe some of the low bits.
4347 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
4348 EVT::getIntegerVT(*DAG.getContext(),
4349 MemVT.getSizeInBits() - ExcessBits),
4350 N->getBaseAlign(), MMOFlags, AAInfo);
4351
4352 // Increment the pointer to the other half.
4353 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
4354 // Load the rest of the low bits.
4355 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
4356 N->getPointerInfo().getWithOffset(IncrementSize),
4357 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
4358 N->getBaseAlign(), MMOFlags, AAInfo);
4359
4360 // Build a factor node to remember that this load is independent of the
4361 // other one.
4362 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
4363 Hi.getValue(1));
4364
4365 if (ExcessBits < NVT.getSizeInBits()) {
4366 // Transfer low bits from the bottom of Hi to the top of Lo.
4367 Lo = DAG.getNode(
4368 ISD::OR, dl, NVT, Lo,
4369 DAG.getNode(ISD::SHL, dl, NVT, Hi,
4370 DAG.getShiftAmountConstant(ExcessBits, NVT, dl)));
4371 // Move high bits to the right position in Hi.
4372 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, NVT,
4373 Hi,
4374 DAG.getShiftAmountConstant(
4375 NVT.getSizeInBits() - ExcessBits, NVT, dl));
4376 }
4377 }
4378
4379 // Legalize the chain result - switch anything that used the old chain to
4380 // use the new one.
4381 ReplaceValueWith(SDValue(N, 1), Ch);
4382}
4383
4384void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
4385 SDValue &Lo, SDValue &Hi) {
4386 SDLoc dl(N);
4387 SDValue LL, LH, RL, RH;
4388 GetExpandedInteger(N->getOperand(0), LL, LH);
4389 GetExpandedInteger(N->getOperand(1), RL, RH);
4390
4391 SDNodeFlags Flags;
4392 if (N->getOpcode() == ISD::OR)
4393 Flags.setDisjoint(N->getFlags().hasDisjoint());
4394
4395 Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL, Flags);
4396 Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH, Flags);
4397}
4398
4399void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
4400 SDValue &Lo, SDValue &Hi) {
4401 EVT VT = N->getValueType(0);
4402 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4403 SDLoc dl(N);
4404
4405 SDValue LL, LH, RL, RH;
4406 GetExpandedInteger(N->getOperand(0), LL, LH);
4407 GetExpandedInteger(N->getOperand(1), RL, RH);
4408
4409 if (TLI.expandMUL(N, Lo, Hi, NVT, DAG,
4411 LL, LH, RL, RH))
4412 return;
4413
4414 // If nothing else, we can make a libcall.
4415 RTLIB::Libcall LC = RTLIB::getMUL(VT);
4416 RTLIB::LibcallImpl LCImpl = TLI.getLibcallImpl(LC);
4417 if (LCImpl == RTLIB::Unsupported) {
4418 // Perform a wide multiplication where the wide type is the original VT and
4419 // the 4 parts are the split arguments.
4420 TLI.forceExpandMultiply(DAG, dl, /*Signed=*/false, Lo, Hi, LL, RL, LH, RH);
4421 return;
4422 }
4423
4424 // Note that we don't need to do a wide MUL here since we don't care about the
4425 // upper half of the result if it exceeds VT.
4426 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
4427 TargetLowering::MakeLibCallOptions CallOptions;
4428 CallOptions.setIsSigned(true);
4429 SplitInteger(TLI.makeLibCall(DAG, LCImpl, VT, Ops, CallOptions, dl).first, Lo,
4430 Hi);
4431}
4432
4433void DAGTypeLegalizer::ExpandIntRes_READCOUNTER(SDNode *N, SDValue &Lo,
4434 SDValue &Hi) {
4435 SDLoc DL(N);
4436 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4437 SDVTList VTs = DAG.getVTList(NVT, NVT, MVT::Other);
4438 SDValue R = DAG.getNode(N->getOpcode(), DL, VTs, N->getOperand(0));
4439 Lo = R.getValue(0);
4440 Hi = R.getValue(1);
4441 ReplaceValueWith(SDValue(N, 1), R.getValue(2));
4442}
4443
4444void DAGTypeLegalizer::ExpandIntRes_AVG(SDNode *N, SDValue &Lo, SDValue &Hi) {
4445 SDValue Result = TLI.expandAVG(N, DAG);
4446 SplitInteger(Result, Lo, Hi);
4447}
4448
4449void DAGTypeLegalizer::ExpandIntRes_ADDSUBSAT(SDNode *N, SDValue &Lo,
4450 SDValue &Hi) {
4451 SDValue Result = TLI.expandAddSubSat(N, DAG);
4452 SplitInteger(Result, Lo, Hi);
4453}
4454
4455void DAGTypeLegalizer::ExpandIntRes_SHLSAT(SDNode *N, SDValue &Lo,
4456 SDValue &Hi) {
4457 SDValue Result = TLI.expandShlSat(N, DAG);
4458 SplitInteger(Result, Lo, Hi);
4459}
4460
4461/// This performs an expansion of the integer result for a fixed point
4462/// multiplication. The default expansion performs rounding down towards
4463/// negative infinity, though targets that do care about rounding should specify
4464/// a target hook for rounding and provide their own expansion or lowering of
4465/// fixed point multiplication to be consistent with rounding.
4466void DAGTypeLegalizer::ExpandIntRes_MULFIX(SDNode *N, SDValue &Lo,
4467 SDValue &Hi) {
4468 SDLoc dl(N);
4469 EVT VT = N->getValueType(0);
4470 unsigned VTSize = VT.getScalarSizeInBits();
4471 SDValue LHS = N->getOperand(0);
4472 SDValue RHS = N->getOperand(1);
4473 uint64_t Scale = N->getConstantOperandVal(2);
4474 bool Saturating = (N->getOpcode() == ISD::SMULFIXSAT ||
4475 N->getOpcode() == ISD::UMULFIXSAT);
4476 bool Signed = (N->getOpcode() == ISD::SMULFIX ||
4477 N->getOpcode() == ISD::SMULFIXSAT);
4478
4479 // Handle special case when scale is equal to zero.
4480 if (!Scale) {
4482 if (!Saturating) {
4483 Result = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
4484 } else {
4485 EVT BoolVT = getSetCCResultType(VT);
4486 unsigned MulOp = Signed ? ISD::SMULO : ISD::UMULO;
4487 Result = DAG.getNode(MulOp, dl, DAG.getVTList(VT, BoolVT), LHS, RHS);
4488 SDValue Product = Result.getValue(0);
4489 SDValue Overflow = Result.getValue(1);
4490 if (Signed) {
4491 APInt MinVal = APInt::getSignedMinValue(VTSize);
4492 APInt MaxVal = APInt::getSignedMaxValue(VTSize);
4493 SDValue SatMin = DAG.getConstant(MinVal, dl, VT);
4494 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
4495 SDValue Zero = DAG.getConstant(0, dl, VT);
4496 // Xor the inputs, if resulting sign bit is 0 the product will be
4497 // positive, else negative.
4498 SDValue Xor = DAG.getNode(ISD::XOR, dl, VT, LHS, RHS);
4499 SDValue ProdNeg = DAG.getSetCC(dl, BoolVT, Xor, Zero, ISD::SETLT);
4500 Result = DAG.getSelect(dl, VT, ProdNeg, SatMin, SatMax);
4501 Result = DAG.getSelect(dl, VT, Overflow, Result, Product);
4502 } else {
4503 // For unsigned multiplication, we only need to check the max since we
4504 // can't really overflow towards zero.
4505 APInt MaxVal = APInt::getMaxValue(VTSize);
4506 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
4507 Result = DAG.getSelect(dl, VT, Overflow, SatMax, Product);
4508 }
4509 }
4510 SplitInteger(Result, Lo, Hi);
4511 return;
4512 }
4513
4514 // For SMULFIX[SAT] we only expect to find Scale<VTSize, but this assert will
4515 // cover for unhandled cases below, while still being valid for UMULFIX[SAT].
4516 assert(Scale <= VTSize && "Scale can't be larger than the value type size.");
4517
4518 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4519 SDValue LL, LH, RL, RH;
4520 GetExpandedInteger(LHS, LL, LH);
4521 GetExpandedInteger(RHS, RL, RH);
4523
4524 unsigned LoHiOp = Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI;
4525 if (!TLI.expandMUL_LOHI(LoHiOp, VT, dl, LHS, RHS, Result, NVT, DAG,
4527 LL, LH, RL, RH)) {
4528 Result.clear();
4529 Result.resize(4);
4530
4531 SDValue LoTmp, HiTmp;
4532 TLI.forceExpandWideMUL(DAG, dl, Signed, LHS, RHS, LoTmp, HiTmp);
4533 SplitInteger(LoTmp, Result[0], Result[1]);
4534 SplitInteger(HiTmp, Result[2], Result[3]);
4535 }
4536 assert(Result.size() == 4 && "Unexpected number of partlets in the result");
4537
4538 unsigned NVTSize = NVT.getScalarSizeInBits();
4539 assert((VTSize == NVTSize * 2) && "Expected the new value type to be half "
4540 "the size of the current value type");
4541
4542 // After getting the multiplication result in 4 parts, we need to perform a
4543 // shift right by the amount of the scale to get the result in that scale.
4544 //
4545 // Let's say we multiply 2 64 bit numbers. The resulting value can be held in
4546 // 128 bits that are cut into 4 32-bit parts:
4547 //
4548 // HH HL LH LL
4549 // |---32---|---32---|---32---|---32---|
4550 // 128 96 64 32 0
4551 //
4552 // |------VTSize-----|
4553 //
4554 // |NVTSize-|
4555 //
4556 // The resulting Lo and Hi would normally be in LL and LH after the shift. But
4557 // to avoid unneccessary shifting of all 4 parts, we can adjust the shift
4558 // amount and get Lo and Hi using two funnel shifts. Or for the special case
4559 // when Scale is a multiple of NVTSize we can just pick the result without
4560 // shifting.
4561 uint64_t Part0 = Scale / NVTSize; // Part holding lowest bit needed.
4562 if (Scale % NVTSize) {
4563 SDValue ShiftAmount = DAG.getShiftAmountConstant(Scale % NVTSize, NVT, dl);
4564 Lo = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 1], Result[Part0],
4565 ShiftAmount);
4566 Hi = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 2], Result[Part0 + 1],
4567 ShiftAmount);
4568 } else {
4569 Lo = Result[Part0];
4570 Hi = Result[Part0 + 1];
4571 }
4572
4573 // Unless saturation is requested we are done. The result is in <Hi,Lo>.
4574 if (!Saturating)
4575 return;
4576
4577 // Can not overflow when there is no integer part.
4578 if (Scale == VTSize)
4579 return;
4580
4581 // To handle saturation we must check for overflow in the multiplication.
4582 //
4583 // Unsigned overflow happened if the upper (VTSize - Scale) bits (of Result)
4584 // aren't all zeroes.
4585 //
4586 // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of Result)
4587 // aren't all ones or all zeroes.
4588 //
4589 // We cannot overflow past HH when multiplying 2 ints of size VTSize, so the
4590 // highest bit of HH determines saturation direction in the event of signed
4591 // saturation.
4592
4593 SDValue ResultHL = Result[2];
4594 SDValue ResultHH = Result[3];
4595
4596 SDValue SatMax, SatMin;
4597 SDValue NVTZero = DAG.getConstant(0, dl, NVT);
4598 SDValue NVTNeg1 = DAG.getAllOnesConstant(dl, NVT);
4599 EVT BoolNVT = getSetCCResultType(NVT);
4600
4601 if (!Signed) {
4602 if (Scale < NVTSize) {
4603 // Overflow happened if ((HH | (HL >> Scale)) != 0).
4604 SDValue HLAdjusted =
4605 DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
4606 DAG.getShiftAmountConstant(Scale, NVT, dl));
4607 SDValue Tmp = DAG.getNode(ISD::OR, dl, NVT, HLAdjusted, ResultHH);
4608 SatMax = DAG.getSetCC(dl, BoolNVT, Tmp, NVTZero, ISD::SETNE);
4609 } else if (Scale == NVTSize) {
4610 // Overflow happened if (HH != 0).
4611 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETNE);
4612 } else if (Scale < VTSize) {
4613 // Overflow happened if ((HH >> (Scale - NVTSize)) != 0).
4614 SDValue HLAdjusted =
4615 DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
4616 DAG.getShiftAmountConstant(Scale - NVTSize, NVT, dl));
4617 SatMax = DAG.getSetCC(dl, BoolNVT, HLAdjusted, NVTZero, ISD::SETNE);
4618 } else
4619 llvm_unreachable("Scale must be less or equal to VTSize for UMULFIXSAT"
4620 "(and saturation can't happen with Scale==VTSize).");
4621
4622 Hi = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Hi);
4623 Lo = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Lo);
4624 return;
4625 }
4626
4627 if (Scale < NVTSize) {
4628 // The number of overflow bits we can check are VTSize - Scale + 1 (we
4629 // include the sign bit). If these top bits are > 0, then we overflowed past
4630 // the max value. If these top bits are < -1, then we overflowed past the
4631 // min value. Otherwise, we did not overflow.
4632 unsigned OverflowBits = VTSize - Scale + 1;
4633 assert(OverflowBits <= VTSize && OverflowBits > NVTSize &&
4634 "Extent of overflow bits must start within HL");
4635 SDValue HLHiMask = DAG.getConstant(
4636 APInt::getHighBitsSet(NVTSize, OverflowBits - NVTSize), dl, NVT);
4637 SDValue HLLoMask = DAG.getConstant(
4638 APInt::getLowBitsSet(NVTSize, VTSize - OverflowBits), dl, NVT);
4639 // We overflow max if HH > 0 or (HH == 0 && HL > HLLoMask).
4640 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
4641 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
4642 SDValue HLUGT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLLoMask, ISD::SETUGT);
4643 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
4644 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLUGT));
4645 // We overflow min if HH < -1 or (HH == -1 && HL < HLHiMask).
4646 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
4647 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
4648 SDValue HLULT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLHiMask, ISD::SETULT);
4649 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
4650 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLULT));
4651 } else if (Scale == NVTSize) {
4652 // We overflow max if HH > 0 or (HH == 0 && HL sign bit is 1).
4653 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
4654 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
4655 SDValue HLNeg = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETLT);
4656 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
4657 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLNeg));
4658 // We overflow min if HH < -1 or (HH == -1 && HL sign bit is 0).
4659 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
4660 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
4661 SDValue HLPos = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETGE);
4662 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
4663 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLPos));
4664 } else if (Scale < VTSize) {
4665 // This is similar to the case when we saturate if Scale < NVTSize, but we
4666 // only need to check HH.
4667 unsigned OverflowBits = VTSize - Scale + 1;
4668 SDValue HHHiMask = DAG.getConstant(
4669 APInt::getHighBitsSet(NVTSize, OverflowBits), dl, NVT);
4670 SDValue HHLoMask = DAG.getConstant(
4671 APInt::getLowBitsSet(NVTSize, NVTSize - OverflowBits), dl, NVT);
4672 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, HHLoMask, ISD::SETGT);
4673 SatMin = DAG.getSetCC(dl, BoolNVT, ResultHH, HHHiMask, ISD::SETLT);
4674 } else
4675 llvm_unreachable("Illegal scale for signed fixed point mul.");
4676
4677 // Saturate to signed maximum.
4678 APInt MaxHi = APInt::getSignedMaxValue(NVTSize);
4679 APInt MaxLo = APInt::getAllOnes(NVTSize);
4680 Hi = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxHi, dl, NVT), Hi);
4681 Lo = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxLo, dl, NVT), Lo);
4682 // Saturate to signed minimum.
4683 APInt MinHi = APInt::getSignedMinValue(NVTSize);
4684 Hi = DAG.getSelect(dl, NVT, SatMin, DAG.getConstant(MinHi, dl, NVT), Hi);
4685 Lo = DAG.getSelect(dl, NVT, SatMin, NVTZero, Lo);
4686}
4687
4688void DAGTypeLegalizer::ExpandIntRes_DIVFIX(SDNode *N, SDValue &Lo,
4689 SDValue &Hi) {
4690 SDLoc dl(N);
4691 // Try expanding in the existing type first.
4692 SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, N->getOperand(0),
4693 N->getOperand(1),
4694 N->getConstantOperandVal(2), DAG);
4695
4696 if (!Res)
4697 Res = earlyExpandDIVFIX(N, N->getOperand(0), N->getOperand(1),
4698 N->getConstantOperandVal(2), TLI, DAG);
4699 SplitInteger(Res, Lo, Hi);
4700}
4701
4702void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
4703 SDValue &Lo, SDValue &Hi) {
4704 assert((Node->getOpcode() == ISD::SADDO || Node->getOpcode() == ISD::SSUBO) &&
4705 "Node has unexpected Opcode");
4706 SDValue LHS = Node->getOperand(0);
4707 SDValue RHS = Node->getOperand(1);
4708 SDLoc dl(Node);
4709
4710 SDValue Ovf;
4711
4712 bool IsAdd = Node->getOpcode() == ISD::SADDO;
4713 unsigned CarryOp = IsAdd ? ISD::SADDO_CARRY : ISD::SSUBO_CARRY;
4714
4715 bool HasCarryOp = TLI.isOperationLegalOrCustom(
4716 CarryOp, TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
4717
4718 if (HasCarryOp) {
4719 // Expand the subcomponents.
4720 SDValue LHSL, LHSH, RHSL, RHSH;
4721 GetExpandedInteger(LHS, LHSL, LHSH);
4722 GetExpandedInteger(RHS, RHSL, RHSH);
4723 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), Node->getValueType(1));
4724
4725 Lo = DAG.getNode(IsAdd ? ISD::UADDO : ISD::USUBO, dl, VTList, {LHSL, RHSL});
4726 Hi = DAG.getNode(CarryOp, dl, VTList, { LHSH, RHSH, Lo.getValue(1) });
4727
4728 Ovf = Hi.getValue(1);
4729 } else {
4730 // Expand the result by simply replacing it with the equivalent
4731 // non-overflow-checking operation.
4732 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4733 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
4734 LHS, RHS);
4735 SplitInteger(Sum, Lo, Hi);
4736
4737 // Compute the overflow.
4738 //
4739 // LHSSign -> LHS < 0
4740 // RHSSign -> RHS < 0
4741 // SumSign -> Sum < 0
4742 //
4743 // Add:
4744 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
4745 // Sub:
4746 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
4747 //
4748 // To get better codegen we can rewrite this by doing bitwise math on
4749 // the integers and extract the final sign bit at the end. So the
4750 // above becomes:
4751 //
4752 // Add:
4753 // Overflow -> (~(LHS ^ RHS) & (LHS ^ Sum)) < 0
4754 // Sub:
4755 // Overflow -> ((LHS ^ RHS) & (LHS ^ Sum)) < 0
4756 //
4757 // NOTE: This is different than the expansion we do in expandSADDSUBO
4758 // because it is more costly to determine the RHS is > 0 for SSUBO with the
4759 // integers split.
4760 EVT VT = LHS.getValueType();
4761 SDValue SignsMatch = DAG.getNode(ISD::XOR, dl, VT, LHS, RHS);
4762 if (IsAdd)
4763 SignsMatch = DAG.getNOT(dl, SignsMatch, VT);
4764
4765 SDValue SumSignNE = DAG.getNode(ISD::XOR, dl, VT, LHS, Sum);
4766 Ovf = DAG.getNode(ISD::AND, dl, VT, SignsMatch, SumSignNE);
4767 EVT OType = Node->getValueType(1);
4768 Ovf = DAG.getSetCC(dl, OType, Ovf, DAG.getConstant(0, dl, VT), ISD::SETLT);
4769 }
4770
4771 // Use the calculated overflow everywhere.
4772 ReplaceValueWith(SDValue(Node, 1), Ovf);
4773}
4774
4775void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
4776 SDValue &Lo, SDValue &Hi) {
4777 EVT VT = N->getValueType(0);
4778 SDLoc dl(N);
4779 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
4780
4781 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
4782 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
4783 SplitInteger(Res.getValue(0), Lo, Hi);
4784 return;
4785 }
4786
4787 RTLIB::Libcall LC = RTLIB::getSDIV(VT);
4788 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
4789
4790 TargetLowering::MakeLibCallOptions CallOptions;
4791 CallOptions.setIsSigned(true);
4792 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
4793}
4794
4795void DAGTypeLegalizer::ExpandIntRes_ShiftThroughStack(SDNode *N, SDValue &Lo,
4796 SDValue &Hi) {
4797 SDLoc dl(N);
4798 SDValue Shiftee = N->getOperand(0);
4799 EVT VT = Shiftee.getValueType();
4800 SDValue ShAmt = N->getOperand(1);
4801 EVT ShAmtVT = ShAmt.getValueType();
4802
4803 EVT LoadVT = VT;
4804 do {
4805 LoadVT = TLI.getTypeToTransformTo(*DAG.getContext(), LoadVT);
4806 } while (!TLI.isTypeLegal(LoadVT));
4807
4808 const unsigned ShiftUnitInBits = LoadVT.getStoreSizeInBits();
4809 assert(ShiftUnitInBits <= VT.getScalarSizeInBits());
4810 assert(isPowerOf2_32(ShiftUnitInBits) &&
4811 "Shifting unit is not a a power of two!");
4812
4813 const bool IsOneStepShift =
4814 DAG.computeKnownBits(ShAmt).countMinTrailingZeros() >=
4815 Log2_32(ShiftUnitInBits);
4816
4817 // If we can't do it as one step, we'll have two uses of shift amount,
4818 // and thus must freeze it.
4819 if (!IsOneStepShift)
4820 ShAmt = DAG.getFreeze(ShAmt);
4821
4822 unsigned VTBitWidth = VT.getScalarSizeInBits();
4823 assert(VTBitWidth % 8 == 0 && "Shifting a not byte multiple value?");
4824 unsigned VTByteWidth = VTBitWidth / 8;
4825 assert(isPowerOf2_32(VTByteWidth) &&
4826 "Shiftee type size is not a power of two!");
4827 unsigned StackSlotByteWidth = 2 * VTByteWidth;
4828 unsigned StackSlotBitWidth = 8 * StackSlotByteWidth;
4829 EVT StackSlotVT = EVT::getIntegerVT(*DAG.getContext(), StackSlotBitWidth);
4830
4831 // Get a temporary stack slot 2x the width of our VT.
4832 // FIXME: reuse stack slots?
4833 Align StackAlign = DAG.getReducedAlign(StackSlotVT, /*UseABI=*/false);
4835 DAG.CreateStackTemporary(StackSlotVT.getStoreSize(), StackAlign);
4836 EVT PtrTy = StackPtr.getValueType();
4837 SDValue Ch = DAG.getEntryNode();
4838
4839 MachinePointerInfo StackPtrInfo = MachinePointerInfo::getFixedStack(
4840 DAG.getMachineFunction(),
4841 cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex());
4842
4843 // Extend the value, that is being shifted, to the entire stack slot's width.
4844 SDValue Init;
4845 if (N->getOpcode() != ISD::SHL) {
4846 unsigned WideningOpc =
4847 N->getOpcode() == ISD::SRA ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4848 Init = DAG.getNode(WideningOpc, dl, StackSlotVT, Shiftee);
4849 } else {
4850 // For left-shifts, pad the Shiftee's LSB with zeros to twice it's width.
4851 SDValue AllZeros = DAG.getConstant(0, dl, VT);
4852 Init = DAG.getNode(ISD::BUILD_PAIR, dl, StackSlotVT, AllZeros, Shiftee);
4853 }
4854 // And spill it into the stack slot.
4855 Ch = DAG.getStore(Ch, dl, Init, StackPtr, StackPtrInfo, StackAlign);
4856
4857 // Now, compute the full-byte offset into stack slot from where we can load.
4858 // We have shift amount, which is in bits. Offset should point to an aligned
4859 // address.
4860 SDNodeFlags Flags;
4861 Flags.setExact(IsOneStepShift);
4862 SDValue SrlTmp = DAG.getNode(
4863 ISD::SRL, dl, ShAmtVT, ShAmt,
4864 DAG.getConstant(Log2_32(ShiftUnitInBits), dl, ShAmtVT), Flags);
4865 SDValue BitOffset =
4866 DAG.getNode(ISD::SHL, dl, ShAmtVT, SrlTmp,
4867 DAG.getConstant(Log2_32(ShiftUnitInBits), dl, ShAmtVT));
4868
4869 SDValue ByteOffset =
4870 DAG.getNode(ISD::SRL, dl, ShAmtVT, BitOffset,
4871 DAG.getConstant(3, dl, ShAmtVT), SDNodeFlags::Exact);
4872 // And clamp it, because OOB load is an immediate UB,
4873 // while shift overflow would have *just* been poison.
4874 ByteOffset = DAG.getNode(ISD::AND, dl, ShAmtVT, ByteOffset,
4875 DAG.getConstant(VTByteWidth - 1, dl, ShAmtVT));
4876 // We have exactly two strategies on indexing into stack slot here:
4877 // 1. upwards starting from the beginning of the slot
4878 // 2. downwards starting from the middle of the slot
4879 // On little-endian machine, we pick 1. for right shifts and 2. for left-shift
4880 // and vice versa on big-endian machine.
4881 bool WillIndexUpwards = N->getOpcode() != ISD::SHL;
4882 if (DAG.getDataLayout().isBigEndian())
4883 WillIndexUpwards = !WillIndexUpwards;
4884
4885 SDValue AdjStackPtr;
4886 if (WillIndexUpwards) {
4887 AdjStackPtr = StackPtr;
4888 } else {
4889 AdjStackPtr = DAG.getMemBasePlusOffset(
4890 StackPtr, DAG.getConstant(VTByteWidth, dl, PtrTy), dl);
4891 ByteOffset = DAG.getNegative(ByteOffset, dl, ShAmtVT);
4892 }
4893
4894 // Get the pointer somewhere into the stack slot from which we need to load.
4895 ByteOffset = DAG.getSExtOrTrunc(ByteOffset, dl, PtrTy);
4896 AdjStackPtr = DAG.getMemBasePlusOffset(AdjStackPtr, ByteOffset, dl);
4897
4898 // And load it! While the load is not legal, legalizing it is obvious.
4899 SDValue Res =
4900 DAG.getLoad(VT, dl, Ch, AdjStackPtr,
4901 MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
4902 commonAlignment(StackAlign, LoadVT.getStoreSize()));
4903
4904 // If we may still have a remaining bits to shift by, do so now.
4905 if (!IsOneStepShift) {
4906 SDValue ShAmtRem =
4907 DAG.getNode(ISD::AND, dl, ShAmtVT, ShAmt,
4908 DAG.getConstant(ShiftUnitInBits - 1, dl, ShAmtVT));
4909 Res = DAG.getNode(N->getOpcode(), dl, VT, Res, ShAmtRem);
4910 }
4911
4912 // Finally, split the computed value.
4913 SplitInteger(Res, Lo, Hi);
4914}
4915
4916void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
4917 SDValue &Lo, SDValue &Hi) {
4918 EVT VT = N->getValueType(0);
4919 unsigned Opc = N->getOpcode();
4920 SDLoc dl(N);
4921
4922 // If we can emit an efficient shift operation, do so now. Check to see if
4923 // the RHS is a constant.
4924 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
4925 return ExpandShiftByConstant(N, CN->getAPIntValue(), Lo, Hi);
4926
4927 // If we can determine that the high bit of the shift is zero or one, even if
4928 // the low bits are variable, emit this shift in an optimized form.
4929 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
4930 return;
4931
4932 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
4933 unsigned PartsOpc;
4934 if (Opc == ISD::SHL) {
4935 PartsOpc = ISD::SHL_PARTS;
4936 } else if (Opc == ISD::SRL) {
4937 PartsOpc = ISD::SRL_PARTS;
4938 } else {
4939 assert(Opc == ISD::SRA && "Unknown shift!");
4940 PartsOpc = ISD::SRA_PARTS;
4941 }
4942
4943 // Next check to see if the target supports this SHL_PARTS operation or if it
4944 // will custom expand it. Don't lower this to SHL_PARTS when we optimise for
4945 // size, but create a libcall instead.
4946 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4947 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
4948 const bool LegalOrCustom =
4949 (Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4950 Action == TargetLowering::Custom;
4951
4952 unsigned ExpansionFactor = 1;
4953 // That VT->NVT expansion is one step. But will we re-expand NVT?
4954 for (EVT TmpVT = NVT;;) {
4955 EVT NewTMPVT = TLI.getTypeToTransformTo(*DAG.getContext(), TmpVT);
4956 if (NewTMPVT == TmpVT)
4957 break;
4958 TmpVT = NewTMPVT;
4959 ++ExpansionFactor;
4960 }
4961
4963 TLI.preferredShiftLegalizationStrategy(DAG, N, ExpansionFactor);
4964
4966 return ExpandIntRes_ShiftThroughStack(N, Lo, Hi);
4967
4968 if (LegalOrCustom &&
4970 // Expand the subcomponents.
4971 SDValue LHSL, LHSH;
4972 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
4973 EVT VT = LHSL.getValueType();
4974
4975 // If the shift amount operand is coming from a vector legalization it may
4976 // have an illegal type. Fix that first by casting the operand, otherwise
4977 // the new SHL_PARTS operation would need further legalization.
4978 SDValue ShiftOp = N->getOperand(1);
4979 EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
4980 if (ShiftOp.getValueType() != ShiftTy)
4981 ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy);
4982
4983 SDValue Ops[] = { LHSL, LHSH, ShiftOp };
4984 Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops);
4985 Hi = Lo.getValue(1);
4986 return;
4987 }
4988
4989 // Otherwise, emit a libcall.
4990 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4991 bool isSigned;
4992 if (Opc == ISD::SHL) {
4993 isSigned = false; /*sign irrelevant*/
4994 LC = RTLIB::getSHL(VT);
4995 } else if (Opc == ISD::SRL) {
4996 isSigned = false;
4997 LC = RTLIB::getSRL(VT);
4998 } else {
4999 assert(Opc == ISD::SRA && "Unknown shift!");
5000 isSigned = true;
5001 LC = RTLIB::getSRA(VT);
5002 }
5003
5004 if (RTLIB::LibcallImpl LibcallImpl = TLI.getLibcallImpl(LC)) {
5005 EVT ShAmtTy =
5006 EVT::getIntegerVT(*DAG.getContext(), DAG.getLibInfo().getIntSize());
5007 SDValue ShAmt = DAG.getZExtOrTrunc(N->getOperand(1), dl, ShAmtTy);
5008 SDValue Ops[2] = {N->getOperand(0), ShAmt};
5009 TargetLowering::MakeLibCallOptions CallOptions;
5010 CallOptions.setIsSigned(isSigned);
5011 SplitInteger(
5012 TLI.makeLibCall(DAG, LibcallImpl, VT, Ops, CallOptions, dl).first, Lo,
5013 Hi);
5014 return;
5015 }
5016
5017 if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
5018 llvm_unreachable("Unsupported shift!");
5019}
5020
5021void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
5022 SDValue &Lo, SDValue &Hi) {
5023 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5024 SDLoc dl(N);
5025 SDValue Op = N->getOperand(0);
5026 if (Op.getValueType().bitsLE(NVT)) {
5027 // The low part is sign extension of the input (degenerates to a copy).
5028 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
5029 // The high part is obtained by SRA'ing all but one of the bits of low part.
5030 unsigned LoSize = NVT.getSizeInBits();
5031 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
5032 DAG.getShiftAmountConstant(LoSize - 1, NVT, dl));
5033 } else {
5034 // For example, extension of an i48 to an i64. The operand type necessarily
5035 // promotes to the result type, so will end up being expanded too.
5036 assert(getTypeAction(Op.getValueType()) ==
5038 "Only know how to promote this result!");
5039 SDValue Res = GetPromotedInteger(Op);
5040 assert(Res.getValueType() == N->getValueType(0) &&
5041 "Operand over promoted?");
5042 // Split the promoted operand. This will simplify when it is expanded.
5043 SplitInteger(Res, Lo, Hi);
5044 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
5045 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
5046 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
5047 ExcessBits)));
5048 }
5049}
5050
5051void DAGTypeLegalizer::
5052ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
5053 SDLoc dl(N);
5054 GetExpandedInteger(N->getOperand(0), Lo, Hi);
5055 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
5056
5057 if (EVT.bitsLE(Lo.getValueType())) {
5058 // sext_inreg the low part if needed.
5059 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
5060 N->getOperand(1));
5061
5062 // The high part gets the sign extension from the lo-part. This handles
5063 // things like sextinreg V:i64 from i8.
5064 Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
5065 DAG.getShiftAmountConstant(Hi.getValueSizeInBits() - 1,
5066 Hi.getValueType(), dl));
5067 } else {
5068 // For example, extension of an i48 to an i64. Leave the low part alone,
5069 // sext_inreg the high part.
5070 unsigned ExcessBits = EVT.getSizeInBits() - Lo.getValueSizeInBits();
5071 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
5072 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
5073 ExcessBits)));
5074 }
5075}
5076
5077void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
5078 SDValue &Lo, SDValue &Hi) {
5079 EVT VT = N->getValueType(0);
5080 SDLoc dl(N);
5081 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
5082
5083 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
5084 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
5085 SplitInteger(Res.getValue(1), Lo, Hi);
5086 return;
5087 }
5088
5089 RTLIB::Libcall LC = RTLIB::getSREM(VT);
5090 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
5091
5092 TargetLowering::MakeLibCallOptions CallOptions;
5093 CallOptions.setIsSigned(true);
5094 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
5095}
5096
5097void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
5098 SDValue &Lo, SDValue &Hi) {
5099 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5100 SDValue InOp = N->getOperand(0);
5101 EVT InVT = InOp.getValueType();
5102 SDLoc dl(N);
5103 Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, InOp);
5104 Hi = DAG.getNode(ISD::SRL, dl, InVT, InOp,
5105 DAG.getShiftAmountConstant(NVT.getSizeInBits(), InVT, dl));
5106 Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
5107}
5108
5109void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N,
5110 SDValue &Lo, SDValue &Hi) {
5111 EVT VT = N->getValueType(0);
5112 SDLoc dl(N);
5113
5114 if (N->getOpcode() == ISD::UMULO) {
5115 // This section expands the operation into the following sequence of
5116 // instructions. `iNh` here refers to a type which has half the bit width of
5117 // the type the original operation operated on.
5118 //
5119 // %0 = %LHS.HI != 0 && %RHS.HI != 0
5120 // %1 = { iNh, i1 } @umul.with.overflow.iNh(iNh %LHS.HI, iNh %RHS.LO)
5121 // %2 = { iNh, i1 } @umul.with.overflow.iNh(iNh %RHS.HI, iNh %LHS.LO)
5122 // %3 = mul nuw iN (%LHS.LOW as iN), (%RHS.LOW as iN)
5123 // %4 = add iNh %1.0, %2.0 as iN
5124 // %5 = { iNh, i1 } @uadd.with.overflow.iNh(iNh %4, iNh %3.HIGH)
5125 //
5126 // %lo = %3.LO
5127 // %hi = %5.0
5128 // %ovf = %0 || %1.1 || %2.1 || %5.1
5129 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
5130 SDValue LHSHigh, LHSLow, RHSHigh, RHSLow;
5131 GetExpandedInteger(LHS, LHSLow, LHSHigh);
5132 GetExpandedInteger(RHS, RHSLow, RHSHigh);
5133 EVT HalfVT = LHSLow.getValueType();
5134 EVT BitVT = N->getValueType(1);
5135 SDVTList VTHalfWithO = DAG.getVTList(HalfVT, BitVT);
5136
5137 SDValue HalfZero = DAG.getConstant(0, dl, HalfVT);
5138 SDValue Overflow = DAG.getNode(ISD::AND, dl, BitVT,
5139 DAG.getSetCC(dl, BitVT, LHSHigh, HalfZero, ISD::SETNE),
5140 DAG.getSetCC(dl, BitVT, RHSHigh, HalfZero, ISD::SETNE));
5141
5142 SDValue One = DAG.getNode(ISD::UMULO, dl, VTHalfWithO, LHSHigh, RHSLow);
5143 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, One.getValue(1));
5144
5145 SDValue Two = DAG.getNode(ISD::UMULO, dl, VTHalfWithO, RHSHigh, LHSLow);
5146 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Two.getValue(1));
5147
5148 SDValue HighSum = DAG.getNode(ISD::ADD, dl, HalfVT, One, Two);
5149
5150 // Cannot use `UMUL_LOHI` directly, because some 32-bit targets (ARM) do not
5151 // know how to expand `i64,i64 = umul_lohi a, b` and abort (why isn’t this
5152 // operation recursively legalized?).
5153 //
5154 // Many backends understand this pattern and will convert into LOHI
5155 // themselves, if applicable.
5156 SDValue Three = DAG.getNode(ISD::MUL, dl, VT,
5157 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LHSLow),
5158 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RHSLow));
5159 SplitInteger(Three, Lo, Hi);
5160
5161 Hi = DAG.getNode(ISD::UADDO, dl, VTHalfWithO, Hi, HighSum);
5162 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Hi.getValue(1));
5163 ReplaceValueWith(SDValue(N, 1), Overflow);
5164 return;
5165 }
5166
5167 Type *RetTy = VT.getTypeForEVT(*DAG.getContext());
5168 EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
5169 Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext());
5170
5171 // Replace this with a libcall that will check overflow.
5172 RTLIB::Libcall LC = RTLIB::getMULO(VT);
5173 RTLIB::LibcallImpl LCImpl = TLI.getLibcallImpl(LC);
5174
5175 // If we don't have the libcall or if the function we are compiling is the
5176 // implementation of the expected libcall (avoid inf-loop), expand inline.
5177 if (LCImpl == RTLIB::Unsupported ||
5178 TLI.getLibcallImplName(LCImpl) == DAG.getMachineFunction().getName()) {
5179 // FIXME: This is not an optimal expansion, but better than crashing.
5180 SDValue MulLo, MulHi;
5181 TLI.forceExpandWideMUL(DAG, dl, /*Signed=*/true, N->getOperand(0),
5182 N->getOperand(1), MulLo, MulHi);
5183 SDValue SRA = DAG.getNode(
5184 ISD::SRA, dl, VT, MulLo,
5185 DAG.getShiftAmountConstant(VT.getScalarSizeInBits() - 1, VT, dl));
5186 SDValue Overflow =
5187 DAG.getSetCC(dl, N->getValueType(1), MulHi, SRA, ISD::SETNE);
5188 SplitInteger(MulLo, Lo, Hi);
5189 ReplaceValueWith(SDValue(N, 1), Overflow);
5190 return;
5191 }
5192
5193 SDValue Temp = DAG.CreateStackTemporary(PtrVT);
5194 // Temporary for the overflow value, default it to zero.
5195 SDValue Chain =
5196 DAG.getStore(DAG.getEntryNode(), dl, DAG.getConstant(0, dl, PtrVT), Temp,
5197 MachinePointerInfo());
5198
5200 for (const SDValue &Op : N->op_values()) {
5201 EVT ArgVT = Op.getValueType();
5202 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
5203 TargetLowering::ArgListEntry Entry(Op, ArgTy);
5204 Entry.IsSExt = true;
5205 Entry.IsZExt = false;
5206 Args.push_back(Entry);
5207 }
5208
5209 // Also pass the address of the overflow check.
5210 TargetLowering::ArgListEntry Entry(
5211 Temp, PointerType::getUnqual(PtrTy->getContext()));
5212 Entry.IsSExt = true;
5213 Entry.IsZExt = false;
5214 Args.push_back(Entry);
5215
5216 SDValue Func = DAG.getExternalSymbol(LCImpl, PtrVT);
5217
5218 TargetLowering::CallLoweringInfo CLI(DAG);
5219 CLI.setDebugLoc(dl)
5220 .setChain(Chain)
5221 .setLibCallee(TLI.getLibcallImplCallingConv(LCImpl), RetTy, Func,
5222 std::move(Args))
5223 .setSExtResult();
5224
5225 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
5226
5227 SplitInteger(CallInfo.first, Lo, Hi);
5228 SDValue Temp2 =
5229 DAG.getLoad(PtrVT, dl, CallInfo.second, Temp, MachinePointerInfo());
5230 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2,
5231 DAG.getConstant(0, dl, PtrVT),
5232 ISD::SETNE);
5233 // Use the overflow from the libcall everywhere.
5234 ReplaceValueWith(SDValue(N, 1), Ofl);
5235}
5236
5237void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
5238 SDValue &Lo, SDValue &Hi) {
5239 EVT VT = N->getValueType(0);
5240 SDLoc dl(N);
5241 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
5242
5243 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
5244 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
5245 SplitInteger(Res.getValue(0), Lo, Hi);
5246 return;
5247 }
5248
5249 // Try to expand UDIV by constant.
5250 if (isa<ConstantSDNode>(N->getOperand(1))) {
5251 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5252 // Only if the new type is legal.
5253 if (isTypeLegal(NVT)) {
5254 SDValue InL, InH;
5255 GetExpandedInteger(N->getOperand(0), InL, InH);
5257 if (TLI.expandDIVREMByConstant(N, Result, NVT, DAG, InL, InH)) {
5258 Lo = Result[0];
5259 Hi = Result[1];
5260 return;
5261 }
5262 }
5263 }
5264
5265 RTLIB::Libcall LC = RTLIB::getUDIV(VT);
5266 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
5267
5268 TargetLowering::MakeLibCallOptions CallOptions;
5269 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
5270}
5271
5272void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
5273 SDValue &Lo, SDValue &Hi) {
5274 EVT VT = N->getValueType(0);
5275 SDLoc dl(N);
5276 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
5277
5278 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
5279 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
5280 SplitInteger(Res.getValue(1), Lo, Hi);
5281 return;
5282 }
5283
5284 // Try to expand UREM by constant.
5285 if (isa<ConstantSDNode>(N->getOperand(1))) {
5286 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5287 // Only if the new type is legal.
5288 if (isTypeLegal(NVT)) {
5289 SDValue InL, InH;
5290 GetExpandedInteger(N->getOperand(0), InL, InH);
5292 if (TLI.expandDIVREMByConstant(N, Result, NVT, DAG, InL, InH)) {
5293 Lo = Result[0];
5294 Hi = Result[1];
5295 return;
5296 }
5297 }
5298 }
5299
5300 RTLIB::Libcall LC = RTLIB::getUREM(VT);
5301 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
5302
5303 TargetLowering::MakeLibCallOptions CallOptions;
5304 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
5305}
5306
5307void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
5308 SDValue &Lo, SDValue &Hi) {
5309 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5310 SDLoc dl(N);
5311 SDValue Op = N->getOperand(0);
5312 if (Op.getValueType().bitsLE(NVT)) {
5313 // The low part is zero extension of the input (degenerates to a copy).
5314 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
5315 Hi = DAG.getConstant(0, dl, NVT); // The high part is just a zero.
5316 } else {
5317 // For example, extension of an i48 to an i64. The operand type necessarily
5318 // promotes to the result type, so will end up being expanded too.
5319 assert(getTypeAction(Op.getValueType()) ==
5321 "Only know how to promote this result!");
5322 SDValue Res = GetPromotedInteger(Op);
5323 assert(Res.getValueType() == N->getValueType(0) &&
5324 "Operand over promoted?");
5325 // Split the promoted operand. This will simplify when it is expanded.
5326 SplitInteger(Res, Lo, Hi);
5327 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
5328 Hi = DAG.getZeroExtendInReg(Hi, dl,
5329 EVT::getIntegerVT(*DAG.getContext(),
5330 ExcessBits));
5331 }
5332}
5333
5334void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N,
5335 SDValue &Lo, SDValue &Hi) {
5336 SDLoc dl(N);
5337 EVT VT = cast<AtomicSDNode>(N)->getMemoryVT();
5338 SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
5339 SDValue Zero = DAG.getConstant(0, dl, VT);
5340 SDValue Swap = DAG.getAtomicCmpSwap(
5341 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, dl,
5342 cast<AtomicSDNode>(N)->getMemoryVT(), VTs, N->getOperand(0),
5343 N->getOperand(1), Zero, Zero, cast<AtomicSDNode>(N)->getMemOperand());
5344
5345 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
5346 ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
5347}
5348
5349void DAGTypeLegalizer::ExpandIntRes_VECREDUCE(SDNode *N,
5350 SDValue &Lo, SDValue &Hi) {
5351 // TODO For VECREDUCE_(AND|OR|XOR) we could split the vector and calculate
5352 // both halves independently.
5353 SDValue Res = TLI.expandVecReduce(N, DAG);
5354 SplitInteger(Res, Lo, Hi);
5355}
5356
5357void DAGTypeLegalizer::ExpandIntRes_Rotate(SDNode *N,
5358 SDValue &Lo, SDValue &Hi) {
5359 // Delegate to funnel-shift expansion.
5360 SDLoc DL(N);
5361 unsigned Opcode = N->getOpcode() == ISD::ROTL ? ISD::FSHL : ISD::FSHR;
5362 SDValue Res = DAG.getNode(Opcode, DL, N->getValueType(0), N->getOperand(0),
5363 N->getOperand(0), N->getOperand(1));
5364 SplitInteger(Res, Lo, Hi);
5365}
5366
5367void DAGTypeLegalizer::ExpandIntRes_FunnelShift(SDNode *N, SDValue &Lo,
5368 SDValue &Hi) {
5369 // Values numbered from least significant to most significant.
5370 SDValue In1, In2, In3, In4;
5371 GetExpandedInteger(N->getOperand(0), In3, In4);
5372 GetExpandedInteger(N->getOperand(1), In1, In2);
5373 EVT HalfVT = In1.getValueType();
5374
5375 SDLoc DL(N);
5376 unsigned Opc = N->getOpcode();
5377 SDValue ShAmt = N->getOperand(2);
5378 EVT ShAmtVT = ShAmt.getValueType();
5379 EVT ShAmtCCVT = getSetCCResultType(ShAmtVT);
5380
5381 // If the shift amount is at least half the bitwidth, swap the inputs.
5382 unsigned HalfVTBits = HalfVT.getScalarSizeInBits();
5383 SDValue AndNode = DAG.getNode(ISD::AND, DL, ShAmtVT, ShAmt,
5384 DAG.getConstant(HalfVTBits, DL, ShAmtVT));
5385 SDValue Cond =
5386 DAG.getSetCC(DL, ShAmtCCVT, AndNode, DAG.getConstant(0, DL, ShAmtVT),
5388
5389 // Expand to a pair of funnel shifts.
5390 EVT NewShAmtVT = TLI.getShiftAmountTy(HalfVT, DAG.getDataLayout());
5391 SDValue NewShAmt = DAG.getAnyExtOrTrunc(ShAmt, DL, NewShAmtVT);
5392
5393 SDValue Select1 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In1, In2);
5394 SDValue Select2 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In2, In3);
5395 SDValue Select3 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In3, In4);
5396 Lo = DAG.getNode(Opc, DL, HalfVT, Select2, Select1, NewShAmt);
5397 Hi = DAG.getNode(Opc, DL, HalfVT, Select3, Select2, NewShAmt);
5398}
5399
5400void DAGTypeLegalizer::ExpandIntRes_VSCALE(SDNode *N, SDValue &Lo,
5401 SDValue &Hi) {
5402 EVT VT = N->getValueType(0);
5403 EVT HalfVT =
5404 EVT::getIntegerVT(*DAG.getContext(), N->getValueSizeInBits(0) / 2);
5405 SDLoc dl(N);
5406
5407 // We assume VSCALE(1) fits into a legal integer.
5408 APInt One(HalfVT.getSizeInBits(), 1);
5409 SDValue VScaleBase = DAG.getVScale(dl, HalfVT, One);
5410 VScaleBase = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, VScaleBase);
5411 SDValue Res = DAG.getNode(ISD::MUL, dl, VT, VScaleBase, N->getOperand(0));
5412 SplitInteger(Res, Lo, Hi);
5413}
5414
5415void DAGTypeLegalizer::ExpandIntRes_READ_REGISTER(SDNode *N, SDValue &Lo,
5416 SDValue &Hi) {
5417 const Function &Fn = DAG.getMachineFunction().getFunction();
5418 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
5419 "cannot use llvm.read_register with illegal type", Fn, N->getDebugLoc()));
5420 ReplaceValueWith(SDValue(N, 1), N->getOperand(0));
5421 EVT LoVT, HiVT;
5422 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
5423 Lo = DAG.getPOISON(LoVT);
5424 Hi = DAG.getPOISON(HiVT);
5425}
5426
5427//===----------------------------------------------------------------------===//
5428// Integer Operand Expansion
5429//===----------------------------------------------------------------------===//
5430
5431/// ExpandIntegerOperand - This method is called when the specified operand of
5432/// the specified node is found to need expansion. At this point, all of the
5433/// result types of the node are known to be legal, but other operands of the
5434/// node may need promotion or expansion as well as the specified one.
5435bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
5436 LLVM_DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG));
5437 SDValue Res = SDValue();
5438
5439 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
5440 return false;
5441
5442 switch (N->getOpcode()) {
5443 default:
5444 #ifndef NDEBUG
5445 dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
5446 N->dump(&DAG); dbgs() << "\n";
5447 #endif
5448 report_fatal_error("Do not know how to expand this operator's operand!");
5449
5450 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
5451 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
5452 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
5453 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
5454 case ISD::FAKE_USE:
5455 Res = ExpandOp_FAKE_USE(N);
5456 break;
5457 case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
5458 case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
5459 case ISD::EXPERIMENTAL_VP_SPLAT:
5460 case ISD::SPLAT_VECTOR: Res = ExpandIntOp_SPLAT_VECTOR(N); break;
5461 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
5462 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
5463 case ISD::SETCCCARRY: Res = ExpandIntOp_SETCCCARRY(N); break;
5465 case ISD::SINT_TO_FP:
5467 case ISD::UINT_TO_FP: Res = ExpandIntOp_XINT_TO_FP(N); break;
5468 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
5469 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
5470
5471 case ISD::SHL:
5472 case ISD::SRA:
5473 case ISD::SRL:
5474 case ISD::ROTL:
5475 case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
5476 case ISD::RETURNADDR:
5477 case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break;
5478
5479 case ISD::SCMP:
5480 case ISD::UCMP: Res = ExpandIntOp_CMP(N); break;
5481
5482 case ISD::ATOMIC_STORE: Res = ExpandIntOp_ATOMIC_STORE(N); break;
5483 case ISD::STACKMAP:
5484 Res = ExpandIntOp_STACKMAP(N, OpNo);
5485 break;
5486 case ISD::PATCHPOINT:
5487 Res = ExpandIntOp_PATCHPOINT(N, OpNo);
5488 break;
5489 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
5490 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
5491 Res = ExpandIntOp_VP_STRIDED(N, OpNo);
5492 break;
5494 Res = ExpandIntOp_WRITE_REGISTER(N, OpNo);
5495 break;
5496 }
5497
5498 // If the result is null, the sub-method took care of registering results etc.
5499 if (!Res.getNode()) return false;
5500
5501 // If the result is N, the sub-method updated N in place. Tell the legalizer
5502 // core about this.
5503 if (Res.getNode() == N)
5504 return true;
5505
5506 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
5507 "Invalid operand expansion");
5508
5509 ReplaceValueWith(SDValue(N, 0), Res);
5510 return false;
5511}
5512
5513/// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
5514/// is shared among BR_CC, SELECT_CC, and SETCC handlers.
5515void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
5516 SDValue &NewRHS,
5517 ISD::CondCode &CCCode,
5518 const SDLoc &dl) {
5519 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
5520 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
5521 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
5522
5523 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
5524 if (RHSLo == RHSHi && isAllOnesConstant(RHSLo)) {
5525 // Equality comparison to -1.
5526 NewLHS = DAG.getNode(ISD::AND, dl, LHSLo.getValueType(), LHSLo, LHSHi);
5527 NewRHS = RHSLo;
5528 return;
5529 }
5530
5531 NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
5532 NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
5533 NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
5534 NewRHS = DAG.getConstant(0, dl, NewLHS.getValueType());
5535 return;
5536 }
5537
5538 // If this is a comparison of the sign bit, just look at the top part.
5539 // X > -1, x < 0
5540 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
5541 if ((CCCode == ISD::SETLT && CST->isZero()) || // X < 0
5542 (CCCode == ISD::SETGT && CST->isAllOnes())) { // X > -1
5543 NewLHS = LHSHi;
5544 NewRHS = RHSHi;
5545 return;
5546 }
5547
5548 // FIXME: This generated code sucks.
5549 ISD::CondCode LowCC;
5550 switch (CCCode) {
5551 default: llvm_unreachable("Unknown integer setcc!");
5552 case ISD::SETLT:
5553 case ISD::SETULT: LowCC = ISD::SETULT; break;
5554 case ISD::SETGT:
5555 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5556 case ISD::SETLE:
5557 case ISD::SETULE: LowCC = ISD::SETULE; break;
5558 case ISD::SETGE:
5559 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5560 }
5561
5562 // LoCmp = lo(op1) < lo(op2) // Always unsigned comparison
5563 // HiCmp = hi(op1) < hi(op2) // Signedness depends on operands
5564 // dest = hi(op1) == hi(op2) ? LoCmp : HiCmp;
5565
5566 // NOTE: on targets without efficient SELECT of bools, we can always use
5567 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5568 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true,
5569 nullptr);
5570 SDValue LoCmp, HiCmp;
5571 if (TLI.isTypeLegal(LHSLo.getValueType()) &&
5572 TLI.isTypeLegal(RHSLo.getValueType()))
5573 LoCmp = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()), LHSLo,
5574 RHSLo, LowCC, false, DagCombineInfo, dl);
5575 if (!LoCmp.getNode())
5576 LoCmp = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()), LHSLo,
5577 RHSLo, LowCC);
5578 if (TLI.isTypeLegal(LHSHi.getValueType()) &&
5579 TLI.isTypeLegal(RHSHi.getValueType()))
5580 HiCmp = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()), LHSHi,
5581 RHSHi, CCCode, false, DagCombineInfo, dl);
5582 if (!HiCmp.getNode())
5583 HiCmp =
5584 DAG.getNode(ISD::SETCC, dl, getSetCCResultType(LHSHi.getValueType()),
5585 LHSHi, RHSHi, DAG.getCondCode(CCCode));
5586
5587 ConstantSDNode *LoCmpC = dyn_cast<ConstantSDNode>(LoCmp.getNode());
5588 ConstantSDNode *HiCmpC = dyn_cast<ConstantSDNode>(HiCmp.getNode());
5589
5590 bool EqAllowed = ISD::isTrueWhenEqual(CCCode);
5591
5592 // FIXME: Is the HiCmpC->isOne() here correct for
5593 // ZeroOrNegativeOneBooleanContent.
5594 if ((EqAllowed && (HiCmpC && HiCmpC->isZero())) ||
5595 (!EqAllowed &&
5596 ((HiCmpC && HiCmpC->isOne()) || (LoCmpC && LoCmpC->isZero())))) {
5597 // For LE / GE, if high part is known false, ignore the low part.
5598 // For LT / GT: if low part is known false, return the high part.
5599 // if high part is known true, ignore the low part.
5600 NewLHS = HiCmp;
5601 NewRHS = SDValue();
5602 return;
5603 }
5604
5605 if (LHSHi == RHSHi) {
5606 // Comparing the low bits is enough.
5607 NewLHS = LoCmp;
5608 NewRHS = SDValue();
5609 return;
5610 }
5611
5612 // Lower with SETCCCARRY if the target supports it.
5613 EVT HiVT = LHSHi.getValueType();
5614 EVT ExpandVT = TLI.getTypeToExpandTo(*DAG.getContext(), HiVT);
5615 bool HasSETCCCARRY = TLI.isOperationLegalOrCustom(ISD::SETCCCARRY, ExpandVT);
5616
5617 // FIXME: Make all targets support this, then remove the other lowering.
5618 if (HasSETCCCARRY) {
5619 // SETCCCARRY can detect < and >= directly. For > and <=, flip
5620 // operands and condition code.
5621 bool FlipOperands = false;
5622 switch (CCCode) {
5623 case ISD::SETGT: CCCode = ISD::SETLT; FlipOperands = true; break;
5624 case ISD::SETUGT: CCCode = ISD::SETULT; FlipOperands = true; break;
5625 case ISD::SETLE: CCCode = ISD::SETGE; FlipOperands = true; break;
5626 case ISD::SETULE: CCCode = ISD::SETUGE; FlipOperands = true; break;
5627 default: break;
5628 }
5629 if (FlipOperands) {
5630 std::swap(LHSLo, RHSLo);
5631 std::swap(LHSHi, RHSHi);
5632 }
5633 // Perform a wide subtraction, feeding the carry from the low part into
5634 // SETCCCARRY. The SETCCCARRY operation is essentially looking at the high
5635 // part of the result of LHS - RHS. It is negative iff LHS < RHS. It is
5636 // zero or positive iff LHS >= RHS.
5637 EVT LoVT = LHSLo.getValueType();
5638 SDVTList VTList = DAG.getVTList(LoVT, getSetCCResultType(LoVT));
5639 SDValue LowCmp = DAG.getNode(ISD::USUBO, dl, VTList, LHSLo, RHSLo);
5640 SDValue Res = DAG.getNode(ISD::SETCCCARRY, dl, getSetCCResultType(HiVT),
5641 LHSHi, RHSHi, LowCmp.getValue(1),
5642 DAG.getCondCode(CCCode));
5643 NewLHS = Res;
5644 NewRHS = SDValue();
5645 return;
5646 }
5647
5648 NewLHS = TLI.SimplifySetCC(getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ,
5649 false, DagCombineInfo, dl);
5650 if (!NewLHS.getNode())
5651 NewLHS =
5652 DAG.getSetCC(dl, getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ);
5653 NewLHS = DAG.getSelect(dl, LoCmp.getValueType(), NewLHS, LoCmp, HiCmp);
5654 NewRHS = SDValue();
5655}
5656
5657SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
5658 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
5659 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
5660 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
5661
5662 // If ExpandSetCCOperands returned a scalar, we need to compare the result
5663 // against zero to select between true and false values.
5664 if (!NewRHS.getNode()) {
5665 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
5666 CCCode = ISD::SETNE;
5667 }
5668
5669 // Update N to have the operands specified.
5670 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
5671 DAG.getCondCode(CCCode), NewLHS, NewRHS,
5672 N->getOperand(4)), 0);
5673}
5674
5675SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
5676 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
5677 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
5678 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
5679
5680 // If ExpandSetCCOperands returned a scalar, we need to compare the result
5681 // against zero to select between true and false values.
5682 if (!NewRHS.getNode()) {
5683 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
5684 CCCode = ISD::SETNE;
5685 }
5686
5687 // Update N to have the operands specified.
5688 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
5689 N->getOperand(2), N->getOperand(3),
5690 DAG.getCondCode(CCCode)), 0);
5691}
5692
5693SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
5694 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
5695 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
5696 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
5697
5698 // If ExpandSetCCOperands returned a scalar, use it.
5699 if (!NewRHS.getNode()) {
5700 assert(NewLHS.getValueType() == N->getValueType(0) &&
5701 "Unexpected setcc expansion!");
5702 return NewLHS;
5703 }
5704
5705 // Otherwise, update N to have the operands specified.
5706 return SDValue(
5707 DAG.UpdateNodeOperands(N, NewLHS, NewRHS, DAG.getCondCode(CCCode)), 0);
5708}
5709
5710SDValue DAGTypeLegalizer::ExpandIntOp_SETCCCARRY(SDNode *N) {
5711 SDValue LHS = N->getOperand(0);
5712 SDValue RHS = N->getOperand(1);
5713 SDValue Carry = N->getOperand(2);
5714 SDValue Cond = N->getOperand(3);
5715 SDLoc dl = SDLoc(N);
5716
5717 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
5718 GetExpandedInteger(LHS, LHSLo, LHSHi);
5719 GetExpandedInteger(RHS, RHSLo, RHSHi);
5720
5721 // Expand to a USUBO_CARRY for the low part and a SETCCCARRY for the high.
5722 SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), Carry.getValueType());
5723 SDValue LowCmp =
5724 DAG.getNode(ISD::USUBO_CARRY, dl, VTList, LHSLo, RHSLo, Carry);
5725 return DAG.getNode(ISD::SETCCCARRY, dl, N->getValueType(0), LHSHi, RHSHi,
5726 LowCmp.getValue(1), Cond);
5727}
5728
5729SDValue DAGTypeLegalizer::ExpandIntOp_SPLAT_VECTOR(SDNode *N) {
5730 // Split the operand and replace with SPLAT_VECTOR_PARTS.
5731 SDValue Lo, Hi;
5732 GetExpandedInteger(N->getOperand(0), Lo, Hi);
5733 return DAG.getNode(ISD::SPLAT_VECTOR_PARTS, SDLoc(N), N->getValueType(0), Lo,
5734 Hi);
5735}
5736
5737SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
5738 // The value being shifted is legal, but the shift amount is too big.
5739 // It follows that either the result of the shift is undefined, or the
5740 // upper half of the shift amount is zero. Just use the lower half.
5741 SDValue Lo, Hi;
5742 GetExpandedInteger(N->getOperand(1), Lo, Hi);
5743 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
5744}
5745
5746SDValue DAGTypeLegalizer::ExpandIntOp_CMP(SDNode *N) {
5747 return TLI.expandCMP(N, DAG);
5748}
5749
5750SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
5751 // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
5752 // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
5753 // constant to valid type.
5754 SDValue Lo, Hi;
5755 GetExpandedInteger(N->getOperand(0), Lo, Hi);
5756 return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
5757}
5758
5759SDValue DAGTypeLegalizer::ExpandIntOp_XINT_TO_FP(SDNode *N) {
5760 bool IsStrict = N->isStrictFPOpcode();
5761 bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP ||
5762 N->getOpcode() == ISD::STRICT_SINT_TO_FP;
5763 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
5764 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
5765 EVT DstVT = N->getValueType(0);
5766 RTLIB::Libcall LC = IsSigned ? RTLIB::getSINTTOFP(Op.getValueType(), DstVT)
5767 : RTLIB::getUINTTOFP(Op.getValueType(), DstVT);
5768 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
5769 "Don't know how to expand this XINT_TO_FP!");
5770 TargetLowering::MakeLibCallOptions CallOptions;
5771 CallOptions.setIsSigned(true);
5772 std::pair<SDValue, SDValue> Tmp =
5773 TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, SDLoc(N), Chain);
5774
5775 if (!IsStrict)
5776 return Tmp.first;
5777
5778 ReplaceValueWith(SDValue(N, 1), Tmp.second);
5779 ReplaceValueWith(SDValue(N, 0), Tmp.first);
5780 return SDValue();
5781}
5782
5783SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
5784 assert(!N->isAtomic() && "Should have been a ATOMIC_STORE?");
5785
5786 if (ISD::isNormalStore(N))
5787 return ExpandOp_NormalStore(N, OpNo);
5788
5789 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
5790 assert(OpNo == 1 && "Can only expand the stored value so far");
5791
5792 EVT VT = N->getOperand(1).getValueType();
5793 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
5794 SDValue Ch = N->getChain();
5795 SDValue Ptr = N->getBasePtr();
5796 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
5797 AAMDNodes AAInfo = N->getAAInfo();
5798 SDLoc dl(N);
5799 SDValue Lo, Hi;
5800
5801 assert(NVT.isByteSized() && "Expanded type not byte sized!");
5802
5803 if (N->getMemoryVT().bitsLE(NVT)) {
5804 GetExpandedInteger(N->getValue(), Lo, Hi);
5805 return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
5806 N->getMemoryVT(), N->getBaseAlign(), MMOFlags,
5807 AAInfo);
5808 }
5809
5810 if (DAG.getDataLayout().isLittleEndian()) {
5811 // Little-endian - low bits are at low addresses.
5812 GetExpandedInteger(N->getValue(), Lo, Hi);
5813
5814 Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(), N->getBaseAlign(),
5815 MMOFlags, AAInfo);
5816
5817 unsigned ExcessBits =
5818 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
5819 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
5820
5821 // Increment the pointer to the other half.
5822 unsigned IncrementSize = NVT.getSizeInBits()/8;
5823 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::getFixed(IncrementSize));
5824 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr,
5825 N->getPointerInfo().getWithOffset(IncrementSize),
5826 NEVT, N->getBaseAlign(), MMOFlags, AAInfo);
5827 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
5828 }
5829
5830 // Big-endian - high bits are at low addresses. Favor aligned stores at
5831 // the cost of some bit-fiddling.
5832 GetExpandedInteger(N->getValue(), Lo, Hi);
5833
5834 EVT ExtVT = N->getMemoryVT();
5835 unsigned EBytes = ExtVT.getStoreSize();
5836 unsigned IncrementSize = NVT.getSizeInBits()/8;
5837 unsigned ExcessBits = (EBytes - IncrementSize)*8;
5838 EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
5839 ExtVT.getSizeInBits() - ExcessBits);
5840
5841 if (ExcessBits < NVT.getSizeInBits()) {
5842 // Transfer high bits from the top of Lo to the bottom of Hi.
5843 Hi = DAG.getNode(
5844 ISD::SHL, dl, NVT, Hi,
5845 DAG.getShiftAmountConstant(NVT.getSizeInBits() - ExcessBits, NVT, dl));
5846 Hi = DAG.getNode(
5847 ISD::OR, dl, NVT, Hi,
5848 DAG.getNode(ISD::SRL, dl, NVT, Lo,
5849 DAG.getShiftAmountConstant(ExcessBits, NVT, dl)));
5850 }
5851
5852 // Store both the high bits and maybe some of the low bits.
5853 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(), HiVT,
5854 N->getBaseAlign(), MMOFlags, AAInfo);
5855
5856 // Increment the pointer to the other half.
5857 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::getFixed(IncrementSize));
5858 // Store the lowest ExcessBits bits in the second half.
5859 Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
5860 N->getPointerInfo().getWithOffset(IncrementSize),
5861 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
5862 N->getBaseAlign(), MMOFlags, AAInfo);
5863 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
5864}
5865
5866SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
5867 SDValue InL, InH;
5868 GetExpandedInteger(N->getOperand(0), InL, InH);
5869 // Just truncate the low part of the source.
5870 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL);
5871}
5872
5873SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) {
5874 SDLoc dl(N);
5875 SDValue Swap =
5876 DAG.getAtomic(ISD::ATOMIC_SWAP, dl, cast<AtomicSDNode>(N)->getMemoryVT(),
5877 N->getOperand(0), N->getOperand(2), N->getOperand(1),
5878 cast<AtomicSDNode>(N)->getMemOperand());
5879 return Swap.getValue(1);
5880}
5881
5882SDValue DAGTypeLegalizer::ExpandIntOp_VP_STRIDED(SDNode *N, unsigned OpNo) {
5883 assert((N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD && OpNo == 3) ||
5884 (N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE && OpNo == 4));
5885
5886 SDValue Hi; // The upper half is dropped out.
5887 SmallVector<SDValue, 8> NewOps(N->ops());
5888 GetExpandedInteger(NewOps[OpNo], NewOps[OpNo], Hi);
5889
5890 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
5891}
5892
5893SDValue DAGTypeLegalizer::ExpandIntOp_WRITE_REGISTER(SDNode *N, unsigned OpNo) {
5894 const Function &Fn = DAG.getMachineFunction().getFunction();
5895 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
5896 "cannot use llvm.write_register with illegal type", Fn,
5897 N->getDebugLoc()));
5898
5899 return N->getOperand(0);
5900}
5901
5902SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SPLICE(SDNode *N) {
5903 SDLoc dl(N);
5904
5905 SDValue V0 = GetPromotedInteger(N->getOperand(0));
5906 SDValue V1 = GetPromotedInteger(N->getOperand(1));
5907 EVT OutVT = V0.getValueType();
5908
5909 return DAG.getNode(ISD::VECTOR_SPLICE, dl, OutVT, V0, V1, N->getOperand(2));
5910}
5911
5912SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_INTERLEAVE_DEINTERLEAVE(SDNode *N) {
5913 SDLoc DL(N);
5914 unsigned Factor = N->getNumOperands();
5915
5917 for (unsigned i = 0; i != Factor; i++)
5918 Ops[i] = GetPromotedInteger(N->getOperand(i));
5919
5920 SmallVector<EVT, 8> ResVTs(Factor, Ops[0].getValueType());
5921 SDValue Res = DAG.getNode(N->getOpcode(), DL, DAG.getVTList(ResVTs), Ops);
5922
5923 for (unsigned i = 0; i != Factor; i++)
5924 SetPromotedInteger(SDValue(N, i), Res.getValue(i));
5925
5926 return SDValue();
5927}
5928
5929SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
5930
5931 EVT OutVT = N->getValueType(0);
5932 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
5933 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
5934 EVT NOutVTElem = NOutVT.getVectorElementType();
5935
5936 SDLoc dl(N);
5937 SDValue BaseIdx = N->getOperand(1);
5938
5939 // TODO: We may be able to use this for types other than scalable
5940 // vectors and fix those tests that expect BUILD_VECTOR to be used
5941 if (OutVT.isScalableVector()) {
5942 SDValue InOp0 = N->getOperand(0);
5943 EVT InVT = InOp0.getValueType();
5944
5945 // Try and extract from a smaller type so that it eventually falls
5946 // into the promotion code below.
5947 if (getTypeAction(InVT) == TargetLowering::TypeSplitVector ||
5948 getTypeAction(InVT) == TargetLowering::TypeLegal) {
5949 EVT NInVT = InVT.getHalfNumVectorElementsVT(*DAG.getContext());
5950 unsigned NElts = NInVT.getVectorMinNumElements();
5951 uint64_t IdxVal = BaseIdx->getAsZExtVal();
5952
5953 SDValue Step1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NInVT, InOp0,
5954 DAG.getConstant(alignDown(IdxVal, NElts), dl,
5955 BaseIdx.getValueType()));
5956 SDValue Step2 = DAG.getNode(
5957 ISD::EXTRACT_SUBVECTOR, dl, OutVT, Step1,
5958 DAG.getConstant(IdxVal % NElts, dl, BaseIdx.getValueType()));
5959 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Step2);
5960 }
5961
5962 // Try and extract from a widened type.
5963 if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
5964 SDValue Ops[] = {GetWidenedVector(InOp0), BaseIdx};
5965 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), OutVT, Ops);
5966 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Ext);
5967 }
5968
5969 // Promote operands and see if this is handled by target lowering,
5970 // Otherwise, use the BUILD_VECTOR approach below
5971 if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
5972 // Collect the (promoted) operands
5973 SDValue Ops[] = { GetPromotedInteger(InOp0), BaseIdx };
5974
5975 EVT PromEltVT = Ops[0].getValueType().getVectorElementType();
5976 assert(PromEltVT.bitsLE(NOutVTElem) &&
5977 "Promoted operand has an element type greater than result");
5978
5979 EVT ExtVT = NOutVT.changeVectorElementType(PromEltVT);
5980 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), ExtVT, Ops);
5981 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Ext);
5982 }
5983 }
5984
5985 if (OutVT.isScalableVector())
5986 report_fatal_error("Unable to promote scalable types using BUILD_VECTOR");
5987
5988 SDValue InOp0 = N->getOperand(0);
5989 if (getTypeAction(InOp0.getValueType()) == TargetLowering::TypePromoteInteger)
5990 InOp0 = GetPromotedInteger(InOp0);
5991
5992 EVT InVT = InOp0.getValueType();
5993 EVT InSVT = InVT.getVectorElementType();
5994
5995 unsigned OutNumElems = OutVT.getVectorNumElements();
5997 Ops.reserve(OutNumElems);
5998 for (unsigned i = 0; i != OutNumElems; ++i) {
5999 // Extract the element from the original vector.
6000 SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(), BaseIdx,
6001 DAG.getConstant(i, dl, BaseIdx.getValueType()));
6002 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, InSVT,
6003 N->getOperand(0), Index);
6004 SDValue Op = DAG.getAnyExtOrTrunc(Ext, dl, NOutVTElem);
6005 // Insert the converted element to the new vector.
6006 Ops.push_back(Op);
6007 }
6008
6009 return DAG.getBuildVector(NOutVT, dl, Ops);
6010}
6011
6012SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_SUBVECTOR(SDNode *N) {
6013 EVT OutVT = N->getValueType(0);
6014 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6015 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6016
6017 SDLoc dl(N);
6018 SDValue Vec = N->getOperand(0);
6019 SDValue SubVec = N->getOperand(1);
6020 SDValue Idx = N->getOperand(2);
6021
6022 EVT SubVecVT = SubVec.getValueType();
6023 EVT NSubVT =
6024 EVT::getVectorVT(*DAG.getContext(), NOutVT.getVectorElementType(),
6025 SubVecVT.getVectorElementCount());
6026
6027 Vec = GetPromotedInteger(Vec);
6028 SubVec = DAG.getNode(ISD::ANY_EXTEND, dl, NSubVT, SubVec);
6029
6030 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, NOutVT, Vec, SubVec, Idx);
6031}
6032
6033SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_REVERSE(SDNode *N) {
6034 SDLoc dl(N);
6035
6036 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6037 EVT OutVT = V0.getValueType();
6038
6039 return DAG.getNode(ISD::VECTOR_REVERSE, dl, OutVT, V0);
6040}
6041
6042SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
6043 ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
6044 EVT VT = N->getValueType(0);
6045 SDLoc dl(N);
6046
6047 ArrayRef<int> NewMask = SV->getMask().slice(0, VT.getVectorNumElements());
6048
6049 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6050 SDValue V1 = GetPromotedInteger(N->getOperand(1));
6051 EVT OutVT = V0.getValueType();
6052
6053 return DAG.getVectorShuffle(OutVT, dl, V0, V1, NewMask);
6054}
6055
6056SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
6057 EVT OutVT = N->getValueType(0);
6058 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6059 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6060 unsigned NumElems = N->getNumOperands();
6061 EVT NOutVTElem = NOutVT.getVectorElementType();
6062 TargetLoweringBase::BooleanContent NOutBoolType = TLI.getBooleanContents(NOutVT);
6063 unsigned NOutExtOpc = TargetLowering::getExtendForContent(NOutBoolType);
6064 SDLoc dl(N);
6065
6067 Ops.reserve(NumElems);
6068 for (unsigned i = 0; i != NumElems; ++i) {
6069 SDValue Op = N->getOperand(i);
6070 EVT OpVT = Op.getValueType();
6071 // BUILD_VECTOR integer operand types are allowed to be larger than the
6072 // result's element type. This may still be true after the promotion. For
6073 // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
6074 // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
6075 if (OpVT.bitsLT(NOutVTElem)) {
6076 unsigned ExtOpc = ISD::ANY_EXTEND;
6077 // Attempt to extend constant bool vectors to match target's BooleanContent.
6078 // While not necessary, this improves chances of the constant correctly
6079 // folding with compare results (e.g. for NOT patterns).
6080 if (OpVT == MVT::i1 && Op.getOpcode() == ISD::Constant)
6081 ExtOpc = NOutExtOpc;
6082 Op = DAG.getNode(ExtOpc, dl, NOutVTElem, Op);
6083 }
6084 Ops.push_back(Op);
6085 }
6086
6087 return DAG.getBuildVector(NOutVT, dl, Ops);
6088}
6089
6090SDValue DAGTypeLegalizer::PromoteIntRes_ScalarOp(SDNode *N) {
6091
6092 SDLoc dl(N);
6093
6094 assert(!N->getOperand(0).getValueType().isVector() &&
6095 "Input must be a scalar");
6096
6097 EVT OutVT = N->getValueType(0);
6098 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6099 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6100 EVT NOutElemVT = NOutVT.getVectorElementType();
6101
6102 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutElemVT, N->getOperand(0));
6103 if (N->isVPOpcode())
6104 return DAG.getNode(N->getOpcode(), dl, NOutVT, Op, N->getOperand(1),
6105 N->getOperand(2));
6106
6107 return DAG.getNode(N->getOpcode(), dl, NOutVT, Op);
6108}
6109
6110SDValue DAGTypeLegalizer::PromoteIntRes_STEP_VECTOR(SDNode *N) {
6111 SDLoc dl(N);
6112 EVT OutVT = N->getValueType(0);
6113 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6114 assert(NOutVT.isScalableVector() &&
6115 "Type must be promoted to a scalable vector type");
6116 const APInt &StepVal = N->getConstantOperandAPInt(0);
6117 return DAG.getStepVector(dl, NOutVT,
6118 StepVal.sext(NOutVT.getScalarSizeInBits()));
6119}
6120
6121SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) {
6122 SDLoc dl(N);
6123
6124 EVT OutVT = N->getValueType(0);
6125 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6126 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6127
6128 unsigned NumOperands = N->getNumOperands();
6129 unsigned NumOutElem = NOutVT.getVectorMinNumElements();
6130 EVT OutElemTy = NOutVT.getVectorElementType();
6131 if (OutVT.isScalableVector()) {
6132 // Find the largest promoted element type for each of the operands.
6133 SDUse *MaxSizedValue = std::max_element(
6134 N->op_begin(), N->op_end(), [](const SDValue &A, const SDValue &B) {
6135 EVT AVT = A.getValueType().getVectorElementType();
6136 EVT BVT = B.getValueType().getVectorElementType();
6137 return AVT.getScalarSizeInBits() < BVT.getScalarSizeInBits();
6138 });
6139 EVT MaxElementVT = MaxSizedValue->getValueType().getVectorElementType();
6140
6141 // Then promote all vectors to the largest element type.
6143 for (unsigned I = 0; I < NumOperands; ++I) {
6144 SDValue Op = N->getOperand(I);
6145 EVT OpVT = Op.getValueType();
6146 if (getTypeAction(OpVT) == TargetLowering::TypePromoteInteger)
6147 Op = GetPromotedInteger(Op);
6148 else
6149 assert(getTypeAction(OpVT) == TargetLowering::TypeLegal &&
6150 "Unhandled legalization type");
6151
6153 MaxElementVT.getScalarSizeInBits())
6154 Op = DAG.getAnyExtOrTrunc(Op, dl,
6155 OpVT.changeVectorElementType(MaxElementVT));
6156 Ops.push_back(Op);
6157 }
6158
6159 // Do the CONCAT on the promoted type and finally truncate to (the promoted)
6160 // NOutVT.
6161 return DAG.getAnyExtOrTrunc(
6162 DAG.getNode(ISD::CONCAT_VECTORS, dl,
6163 OutVT.changeVectorElementType(MaxElementVT), Ops),
6164 dl, NOutVT);
6165 }
6166
6167 unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements();
6168 assert(NumElem * NumOperands == NumOutElem &&
6169 "Unexpected number of elements");
6170
6171 // Take the elements from the first vector.
6172 SmallVector<SDValue, 8> Ops(NumOutElem);
6173 for (unsigned i = 0; i < NumOperands; ++i) {
6174 SDValue Op = N->getOperand(i);
6175 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger)
6176 Op = GetPromotedInteger(Op);
6177 EVT SclrTy = Op.getValueType().getVectorElementType();
6178 assert(NumElem == Op.getValueType().getVectorNumElements() &&
6179 "Unexpected number of elements");
6180
6181 for (unsigned j = 0; j < NumElem; ++j) {
6182 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Op,
6183 DAG.getVectorIdxConstant(j, dl));
6184 Ops[i * NumElem + j] = DAG.getAnyExtOrTrunc(Ext, dl, OutElemTy);
6185 }
6186 }
6187
6188 return DAG.getBuildVector(NOutVT, dl, Ops);
6189}
6190
6191SDValue DAGTypeLegalizer::PromoteIntRes_EXTEND_VECTOR_INREG(SDNode *N) {
6192 EVT VT = N->getValueType(0);
6193 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6194 assert(NVT.isVector() && "This type must be promoted to a vector type");
6195
6196 SDLoc dl(N);
6197
6198 // For operands whose TypeAction is to promote, extend the promoted node
6199 // appropriately (ZERO_EXTEND or SIGN_EXTEND) from the original pre-promotion
6200 // type, and then construct a new *_EXTEND_VECTOR_INREG node to the promote-to
6201 // type..
6202 if (getTypeAction(N->getOperand(0).getValueType())
6204 SDValue Promoted;
6205
6206 switch(N->getOpcode()) {
6208 Promoted = SExtPromotedInteger(N->getOperand(0));
6209 break;
6211 Promoted = ZExtPromotedInteger(N->getOperand(0));
6212 break;
6214 Promoted = GetPromotedInteger(N->getOperand(0));
6215 break;
6216 default:
6217 llvm_unreachable("Node has unexpected Opcode");
6218 }
6219 return DAG.getNode(N->getOpcode(), dl, NVT, Promoted);
6220 }
6221
6222 // Directly extend to the appropriate transform-to type.
6223 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
6224}
6225
6226SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_FIND_LAST_ACTIVE(SDNode *N) {
6227 EVT VT = N->getValueType(0);
6228 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6229 return DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, SDLoc(N), NVT, N->ops());
6230}
6231
6232SDValue DAGTypeLegalizer::PromoteIntRes_GET_ACTIVE_LANE_MASK(SDNode *N) {
6233 EVT VT = N->getValueType(0);
6234 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6235 return DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, SDLoc(N), NVT, N->ops());
6236}
6237
6238SDValue DAGTypeLegalizer::PromoteIntRes_PARTIAL_REDUCE_MLA(SDNode *N) {
6239 SDLoc DL(N);
6240 EVT VT = N->getValueType(0);
6241 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
6242 SDValue ExtAcc = GetPromotedInteger(N->getOperand(0));
6243 return DAG.getNode(N->getOpcode(), DL, NVT, ExtAcc, N->getOperand(1),
6244 N->getOperand(2));
6245}
6246
6247SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
6248 EVT OutVT = N->getValueType(0);
6249 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
6250 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
6251
6252 EVT NOutVTElem = NOutVT.getVectorElementType();
6253
6254 SDLoc dl(N);
6255 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6256
6257 SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
6258 NOutVTElem, N->getOperand(1));
6259 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT,
6260 V0, ConvElem, N->getOperand(2));
6261}
6262
6263SDValue DAGTypeLegalizer::PromoteIntRes_VECREDUCE(SDNode *N) {
6264 // The VECREDUCE result size may be larger than the element size, so
6265 // we can simply change the result type.
6266 SDLoc dl(N);
6267 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6268 return DAG.getNode(N->getOpcode(), dl, NVT, N->ops());
6269}
6270
6271SDValue DAGTypeLegalizer::PromoteIntRes_VP_REDUCE(SDNode *N) {
6272 // The VP_REDUCE result size may be larger than the element size, so we can
6273 // simply change the result type. However the start value and result must be
6274 // the same.
6275 SDLoc DL(N);
6276 SDValue Start = PromoteIntOpVectorReduction(N, N->getOperand(0));
6277 return DAG.getNode(N->getOpcode(), DL, Start.getValueType(), Start,
6278 N->getOperand(1), N->getOperand(2), N->getOperand(3));
6279}
6280
6281SDValue DAGTypeLegalizer::PromoteIntRes_PATCHPOINT(SDNode *N) {
6282 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6283 SDLoc dl(N);
6284
6285 assert(N->getNumValues() == 3 && "Expected 3 values for PATCHPOINT");
6286 SDVTList VTList = DAG.getVTList({NVT, MVT::Other, MVT::Glue});
6287
6288 SmallVector<SDValue> Ops(N->ops());
6289 SDValue Res = DAG.getNode(ISD::PATCHPOINT, dl, VTList, Ops);
6290
6291 // Replace chain and glue uses with the new patchpoint.
6292 SDValue From[] = {SDValue(N, 1), SDValue(N, 2)};
6293 SDValue To[] = {Res.getValue(1), Res.getValue(2)};
6294 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
6295
6296 return Res.getValue(0);
6297}
6298
6299SDValue DAGTypeLegalizer::PromoteIntRes_READ_REGISTER(SDNode *N) {
6300 const Function &Fn = DAG.getMachineFunction().getFunction();
6301 Fn.getContext().diagnose(DiagnosticInfoLegalizationFailure(
6302 "cannot use llvm.read_register with illegal type", Fn, N->getDebugLoc()));
6303
6304 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
6305 ReplaceValueWith(SDValue(N, 1), N->getOperand(0));
6306 return DAG.getPOISON(NVT);
6307}
6308
6309SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
6310 SDLoc dl(N);
6311 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6312 SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl,
6313 TLI.getVectorIdxTy(DAG.getDataLayout()));
6315 V0->getValueType(0).getScalarType(), V0, V1);
6316
6317 // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
6318 // element types. If this is the case then we need to expand the outgoing
6319 // value and not truncate it.
6320 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
6321}
6322
6323SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_SUBVECTOR(SDNode *N) {
6324 SDLoc dl(N);
6325 // The result type is equal to the first input operand's type, so the
6326 // type that needs promoting must be the second source vector.
6327 SDValue V0 = N->getOperand(0);
6328 SDValue V1 = GetPromotedInteger(N->getOperand(1));
6329 SDValue Idx = N->getOperand(2);
6330 EVT PromVT = EVT::getVectorVT(*DAG.getContext(),
6333 V0 = DAG.getAnyExtOrTrunc(V0, dl, PromVT);
6334 SDValue Ext = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, PromVT, V0, V1, Idx);
6335 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
6336}
6337
6338// FIXME: We wouldn't need this if clang could promote short integers
6339// that are arguments to FAKE_USE.
6340SDValue DAGTypeLegalizer::PromoteIntOp_FAKE_USE(SDNode *N) {
6341 SDLoc dl(N);
6342 SDValue V0 = N->getOperand(0);
6343 SDValue V1 = N->getOperand(1);
6344 EVT InVT1 = V1.getValueType();
6345 SDValue VPromoted =
6346 DAG.getNode(ISD::ANY_EXTEND, dl,
6347 TLI.getTypeToTransformTo(*DAG.getContext(), InVT1), V1);
6348 return DAG.getNode(N->getOpcode(), dl, N->getValueType(0), V0, VPromoted);
6349}
6350
6351SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_SUBVECTOR(SDNode *N) {
6352 SDLoc dl(N);
6353 SDValue V0 = GetPromotedInteger(N->getOperand(0));
6354 MVT InVT = V0.getValueType().getSimpleVT();
6355 MVT OutVT = MVT::getVectorVT(InVT.getVectorElementType(),
6356 N->getValueType(0).getVectorNumElements());
6357 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, V0, N->getOperand(1));
6358 return DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), Ext);
6359}
6360
6361SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
6362 SDLoc dl(N);
6363
6364 EVT ResVT = N->getValueType(0);
6365 unsigned NumElems = N->getNumOperands();
6366
6367 if (ResVT.isScalableVector()) {
6368 SDValue ResVec = DAG.getUNDEF(ResVT);
6369
6370 for (unsigned OpIdx = 0; OpIdx < NumElems; ++OpIdx) {
6371 SDValue Op = N->getOperand(OpIdx);
6372 unsigned OpNumElts = Op.getValueType().getVectorMinNumElements();
6373 ResVec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, ResVec, Op,
6374 DAG.getIntPtrConstant(OpIdx * OpNumElts, dl));
6375 }
6376
6377 return ResVec;
6378 }
6379
6380 EVT RetSclrTy = N->getValueType(0).getVectorElementType();
6381
6383 NewOps.reserve(NumElems);
6384
6385 // For each incoming vector
6386 for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) {
6387 SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
6388 EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
6389 unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
6390
6391 for (unsigned i=0; i<NumElem; ++i) {
6392 // Extract element from incoming vector
6393 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Incoming,
6394 DAG.getVectorIdxConstant(i, dl));
6395 SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
6396 NewOps.push_back(Tr);
6397 }
6398 }
6399
6400 return DAG.getBuildVector(N->getValueType(0), dl, NewOps);
6401}
6402
6403SDValue DAGTypeLegalizer::ExpandIntOp_STACKMAP(SDNode *N, unsigned OpNo) {
6404 assert(OpNo > 1);
6405 SDValue Op = N->getOperand(OpNo);
6406
6407 // FIXME: Non-constant operands are not yet handled:
6408 // - https://github.com/llvm/llvm-project/issues/26431
6409 // - https://github.com/llvm/llvm-project/issues/55957
6410 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op);
6411 if (!CN)
6412 return SDValue();
6413
6414 // Copy operands before the one being expanded.
6415 SmallVector<SDValue> NewOps;
6416 for (unsigned I = 0; I < OpNo; I++)
6417 NewOps.push_back(N->getOperand(I));
6418
6419 EVT Ty = Op.getValueType();
6420 SDLoc DL = SDLoc(N);
6421 if (CN->getConstantIntValue()->getValue().getActiveBits() < 64) {
6422 NewOps.push_back(
6423 DAG.getTargetConstant(StackMaps::ConstantOp, DL, MVT::i64));
6424 NewOps.push_back(DAG.getTargetConstant(CN->getZExtValue(), DL, Ty));
6425 } else {
6426 // FIXME: https://github.com/llvm/llvm-project/issues/55609
6427 return SDValue();
6428 }
6429
6430 // Copy remaining operands.
6431 for (unsigned I = OpNo + 1; I < N->getNumOperands(); I++)
6432 NewOps.push_back(N->getOperand(I));
6433
6434 SDValue NewNode = DAG.getNode(N->getOpcode(), DL, N->getVTList(), NewOps);
6435
6436 for (unsigned ResNum = 0; ResNum < N->getNumValues(); ResNum++)
6437 ReplaceValueWith(SDValue(N, ResNum), NewNode.getValue(ResNum));
6438
6439 return SDValue(); // Signal that we have replaced the node already.
6440}
6441
6442SDValue DAGTypeLegalizer::ExpandIntOp_PATCHPOINT(SDNode *N, unsigned OpNo) {
6443 assert(OpNo >= 7);
6444 SDValue Op = N->getOperand(OpNo);
6445
6446 // FIXME: Non-constant operands are not yet handled:
6447 // - https://github.com/llvm/llvm-project/issues/26431
6448 // - https://github.com/llvm/llvm-project/issues/55957
6449 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op);
6450 if (!CN)
6451 return SDValue();
6452
6453 // Copy operands before the one being expanded.
6454 SmallVector<SDValue> NewOps;
6455 for (unsigned I = 0; I < OpNo; I++)
6456 NewOps.push_back(N->getOperand(I));
6457
6458 EVT Ty = Op.getValueType();
6459 SDLoc DL = SDLoc(N);
6460 if (CN->getConstantIntValue()->getValue().getActiveBits() < 64) {
6461 NewOps.push_back(
6462 DAG.getTargetConstant(StackMaps::ConstantOp, DL, MVT::i64));
6463 NewOps.push_back(DAG.getTargetConstant(CN->getZExtValue(), DL, Ty));
6464 } else {
6465 // FIXME: https://github.com/llvm/llvm-project/issues/55609
6466 return SDValue();
6467 }
6468
6469 // Copy remaining operands.
6470 for (unsigned I = OpNo + 1; I < N->getNumOperands(); I++)
6471 NewOps.push_back(N->getOperand(I));
6472
6473 SDValue NewNode = DAG.getNode(N->getOpcode(), DL, N->getVTList(), NewOps);
6474
6475 for (unsigned ResNum = 0; ResNum < N->getNumValues(); ResNum++)
6476 ReplaceValueWith(SDValue(N, ResNum), NewNode.getValue(ResNum));
6477
6478 return SDValue(); // Signal that we have replaced the node already.
6479}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool isSigned(unsigned int Opcode)
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static SDValue SaturateWidenedDIVFIX(SDValue V, SDLoc &dl, unsigned SatW, bool Signed, const TargetLowering &TLI, SelectionDAG &DAG)
static SDValue fpExtendHelper(SDValue Op, SDValue &Chain, bool IsStrict, EVT VT, SDLoc DL, SelectionDAG &DAG)
static SDValue earlyExpandDIVFIX(SDNode *N, SDValue LHS, SDValue RHS, unsigned Scale, const TargetLowering &TLI, SelectionDAG &DAG, unsigned SatW=0)
static unsigned getExtendForIntVecReduction(SDNode *N)
static std::pair< ISD::CondCode, ISD::NodeType > getExpandedMinMaxOps(int Op)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
#define I(x, y, z)
Definition MD5.cpp:57
MachineInstr unsigned OpIdx
const SmallVectorImpl< MachineOperand > & Cond
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
#define LLVM_DEBUG(...)
Definition Debug.h:114
This file describes how to lower LLVM code to machine code.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1513
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:936
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
unsigned countLeadingOnes() const
Definition APInt.h:1625
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1183
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1250
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
unsigned countTrailingZeros() const
Definition APInt.h:1648
unsigned countLeadingZeros() const
Definition APInt.h:1607
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:985
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
unsigned countTrailingOnes() const
Definition APInt.h:1663
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:852
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:186
This is an SDNode representing atomic operations.
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h: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:343
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
This class is used to represent a VP_LOAD node.
This class is used to represent a VP_STORE node.
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns a value X where RHS.multiplyCoefficientBy(X) will result in a value whose quantity matches ou...
Definition TypeSize.h:277
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ Entry
Definition COFF.h:862
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:807
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:256
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:780
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:504
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:231
@ LOOP_DEPENDENCE_RAW_MASK
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:270
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:593
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:771
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition ISDOpcodes.h:387
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:289
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:393
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:841
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:868
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:577
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:744
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:898
@ SDIVREM
SDIVREM/UDIVREM - Divide two integers and produce both a quotient and remainder result.
Definition ISDOpcodes.h:275
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:400
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:832
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:712
@ STRICT_UINT_TO_FP
Definition ISDOpcodes.h:478
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:662
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:779
@ SETCCCARRY
Like SetCC, ops #0 and #1 are the LHS and RHS operands to compare, but op #2 is a boolean indicating ...
Definition ISDOpcodes.h:815
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:347
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition ISDOpcodes.h:628
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:688
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:541
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:369
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:784
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:228
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:242
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:669
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:343
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
Definition ISDOpcodes.h:958
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:762
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:642
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:607
@ READ_REGISTER
READ_REGISTER, WRITE_REGISTER - This node represents llvm.register on the DAG, which implements the n...
Definition ISDOpcodes.h:134
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:569
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:838
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:799
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:887
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:876
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:724
@ VECTOR_REVERSE
VECTOR_REVERSE(VECTOR) - Returns a vector, of the same type as VECTOR, whose elements are shuffled us...
Definition ISDOpcodes.h:633
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:406
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:793
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:323
@ STRICT_SINT_TO_FP
STRICT_[US]INT_TO_FP - Convert a signed or unsigned integer to a floating point value.
Definition ISDOpcodes.h:477
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ STRICT_FP_TO_UINT
Definition ISDOpcodes.h:471
@ STRICT_FP_TO_SINT
STRICT_FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:470
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:914
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:498
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:736
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:732
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:707
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:299
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:678
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:236
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:558
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition ISDOpcodes.h:654
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:696
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:909
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:933
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:844
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:821
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition ISDOpcodes.h:617
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:719
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:333
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:549
@ LOOP_DEPENDENCE_WAR_MASK
Set rounding mode.
bool isNormalStore(const SDNode *N)
Returns true if the specified node is a non-truncating and unindexed store.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
bool isUNINDEXEDLoad(const SDNode *N)
Returns true if the specified node is an unindexed load.
bool isSignedIntSetCC(CondCode Code)
Return true if this is a setcc instruction that performs a signed comparison when used with integer o...
bool isUNINDEXEDStore(const SDNode *N)
Returns true if the specified node is an unindexed store.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
bool isUnsignedIntSetCC(CondCode Code)
Return true if this is a setcc instruction that performs an unsigned comparison when used with intege...
bool isNormalLoad(const SDNode *N)
Returns true if the specified node is a non-extending and unindexed load.
bool isIntEqualitySetCC(CondCode Code)
Return true if this is a setcc instruction that performs an equality comparison when used with intege...
LLVM_ABI Libcall getPOWI(EVT RetVT)
getPOWI - Return the POWI_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getSINTTOFP(EVT OpVT, EVT RetVT)
getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getUREM(EVT VT)
LLVM_ABI Libcall getSHL(EVT VT)
LLVM_ABI Libcall getSYNC(unsigned Opc, MVT VT)
Return the SYNC_FETCH_AND_* value for the given opcode and type, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getLDEXP(EVT RetVT)
getLDEXP - Return the LDEXP_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getUINTTOFP(EVT OpVT, EVT RetVT)
getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getSDIV(EVT VT)
LLVM_ABI Libcall getSRL(EVT VT)
LLVM_ABI Libcall getSRA(EVT VT)
LLVM_ABI Libcall getUDIV(EVT VT)
LLVM_ABI Libcall getFPTOUINT(EVT OpVT, EVT RetVT)
getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getLLROUND(EVT VT)
LLVM_ABI Libcall getLROUND(EVT VT)
LLVM_ABI Libcall getFPTOSINT(EVT OpVT, EVT RetVT)
getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getLRINT(EVT RetVT)
LLVM_ABI Libcall getOUTLINE_ATOMIC(unsigned Opc, AtomicOrdering Order, MVT VT)
Return the outline atomics value for the given opcode, atomic ordering and type, or UNKNOWN_LIBCALL i...
LLVM_ABI Libcall getLLRINT(EVT RetVT)
LLVM_ABI Libcall getSREM(EVT VT)
LLVM_ABI Libcall getMUL(EVT VT)
LLVM_ABI Libcall getCTPOP(EVT VT)
LLVM_ABI Libcall getMULO(EVT VT)
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:546
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Success
The lock was released successfully.
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ AfterLegalizeTypes
Definition DAGCombine.h:17
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ Xor
Bitwise or logical XOR of integers.
@ Add
Sum of integers.
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h: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)