Thanks to visit codestin.com
Credit goes to llvm.org

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