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