[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / CodeGen / SelectionDAG / LegalizeIntegerTypes.cpp
blob8dfdb00f64bf917ad6f1626f68bb5dd93e1a1f91
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).
18 //===----------------------------------------------------------------------===//
20 #include "LegalizeTypes.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/KnownBits.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace llvm;
28 #define DEBUG_TYPE "legalize-types"
30 //===----------------------------------------------------------------------===//
31 // Integer Result Promotion
32 //===----------------------------------------------------------------------===//
34 /// PromoteIntegerResult - This method is called when a result of a node is
35 /// found to be in need of promotion to a larger type. At this point, the node
36 /// may also have invalid operands or may have other results that need
37 /// expansion, we just know that (at least) one result needs promotion.
38 void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
39 LLVM_DEBUG(dbgs() << "Promote integer result: "; N->dump(&DAG);
40 dbgs() << "\n");
41 SDValue Res = SDValue();
43 // See if the target wants to custom expand this node.
44 if (CustomLowerNode(N, N->getValueType(ResNo), true)) {
45 LLVM_DEBUG(dbgs() << "Node has been custom expanded, done\n");
46 return;
49 switch (N->getOpcode()) {
50 default:
51 #ifndef NDEBUG
52 dbgs() << "PromoteIntegerResult #" << ResNo << ": ";
53 N->dump(&DAG); dbgs() << "\n";
54 #endif
55 llvm_unreachable("Do not know how to promote this operator!");
56 case ISD::MERGE_VALUES:Res = PromoteIntRes_MERGE_VALUES(N, ResNo); break;
57 case ISD::AssertSext: Res = PromoteIntRes_AssertSext(N); break;
58 case ISD::AssertZext: Res = PromoteIntRes_AssertZext(N); break;
59 case ISD::BITCAST: Res = PromoteIntRes_BITCAST(N); break;
60 case ISD::BITREVERSE: Res = PromoteIntRes_BITREVERSE(N); break;
61 case ISD::BSWAP: Res = PromoteIntRes_BSWAP(N); break;
62 case ISD::BUILD_PAIR: Res = PromoteIntRes_BUILD_PAIR(N); break;
63 case ISD::Constant: Res = PromoteIntRes_Constant(N); break;
64 case ISD::CTLZ_ZERO_UNDEF:
65 case ISD::CTLZ: Res = PromoteIntRes_CTLZ(N); break;
66 case ISD::PARITY:
67 case ISD::CTPOP: Res = PromoteIntRes_CTPOP_PARITY(N); break;
68 case ISD::CTTZ_ZERO_UNDEF:
69 case ISD::CTTZ: Res = PromoteIntRes_CTTZ(N); break;
70 case ISD::EXTRACT_VECTOR_ELT:
71 Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
72 case ISD::LOAD: Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N)); break;
73 case ISD::MLOAD: Res = PromoteIntRes_MLOAD(cast<MaskedLoadSDNode>(N));
74 break;
75 case ISD::MGATHER: Res = PromoteIntRes_MGATHER(cast<MaskedGatherSDNode>(N));
76 break;
77 case ISD::SELECT: Res = PromoteIntRes_SELECT(N); break;
78 case ISD::VSELECT: Res = PromoteIntRes_VSELECT(N); break;
79 case ISD::SELECT_CC: Res = PromoteIntRes_SELECT_CC(N); break;
80 case ISD::STRICT_FSETCC:
81 case ISD::STRICT_FSETCCS:
82 case ISD::SETCC: Res = PromoteIntRes_SETCC(N); break;
83 case ISD::SMIN:
84 case ISD::SMAX: Res = PromoteIntRes_SExtIntBinOp(N); break;
85 case ISD::UMIN:
86 case ISD::UMAX: Res = PromoteIntRes_UMINUMAX(N); break;
88 case ISD::SHL: Res = PromoteIntRes_SHL(N); break;
89 case ISD::SIGN_EXTEND_INREG:
90 Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
91 case ISD::SRA: Res = PromoteIntRes_SRA(N); break;
92 case ISD::SRL: Res = PromoteIntRes_SRL(N); break;
93 case ISD::TRUNCATE: Res = PromoteIntRes_TRUNCATE(N); break;
94 case ISD::UNDEF: Res = PromoteIntRes_UNDEF(N); break;
95 case ISD::VAARG: Res = PromoteIntRes_VAARG(N); break;
96 case ISD::VSCALE: Res = PromoteIntRes_VSCALE(N); break;
98 case ISD::EXTRACT_SUBVECTOR:
99 Res = PromoteIntRes_EXTRACT_SUBVECTOR(N); break;
100 case ISD::INSERT_SUBVECTOR:
101 Res = PromoteIntRes_INSERT_SUBVECTOR(N); break;
102 case ISD::VECTOR_REVERSE:
103 Res = PromoteIntRes_VECTOR_REVERSE(N); break;
104 case ISD::VECTOR_SHUFFLE:
105 Res = PromoteIntRes_VECTOR_SHUFFLE(N); break;
106 case ISD::VECTOR_SPLICE:
107 Res = PromoteIntRes_VECTOR_SPLICE(N); break;
108 case ISD::INSERT_VECTOR_ELT:
109 Res = PromoteIntRes_INSERT_VECTOR_ELT(N); break;
110 case ISD::BUILD_VECTOR:
111 Res = PromoteIntRes_BUILD_VECTOR(N); break;
112 case ISD::SCALAR_TO_VECTOR:
113 Res = PromoteIntRes_SCALAR_TO_VECTOR(N); break;
114 case ISD::SPLAT_VECTOR:
115 Res = PromoteIntRes_SPLAT_VECTOR(N); break;
116 case ISD::STEP_VECTOR: Res = PromoteIntRes_STEP_VECTOR(N); break;
117 case ISD::CONCAT_VECTORS:
118 Res = PromoteIntRes_CONCAT_VECTORS(N); break;
120 case ISD::ANY_EXTEND_VECTOR_INREG:
121 case ISD::SIGN_EXTEND_VECTOR_INREG:
122 case ISD::ZERO_EXTEND_VECTOR_INREG:
123 Res = PromoteIntRes_EXTEND_VECTOR_INREG(N); break;
125 case ISD::SIGN_EXTEND:
126 case ISD::ZERO_EXTEND:
127 case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break;
129 case ISD::STRICT_FP_TO_SINT:
130 case ISD::STRICT_FP_TO_UINT:
131 case ISD::FP_TO_SINT:
132 case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break;
134 case ISD::FP_TO_SINT_SAT:
135 case ISD::FP_TO_UINT_SAT:
136 Res = PromoteIntRes_FP_TO_XINT_SAT(N); break;
138 case ISD::FP_TO_FP16: Res = PromoteIntRes_FP_TO_FP16(N); break;
140 case ISD::FLT_ROUNDS_: Res = PromoteIntRes_FLT_ROUNDS(N); break;
142 case ISD::ISNAN: Res = PromoteIntRes_ISNAN(N); break;
144 case ISD::AND:
145 case ISD::OR:
146 case ISD::XOR:
147 case ISD::ADD:
148 case ISD::SUB:
149 case ISD::MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break;
151 case ISD::SDIV:
152 case ISD::SREM: Res = PromoteIntRes_SExtIntBinOp(N); break;
154 case ISD::UDIV:
155 case ISD::UREM: Res = PromoteIntRes_ZExtIntBinOp(N); break;
157 case ISD::SADDO:
158 case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
159 case ISD::UADDO:
160 case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
161 case ISD::SMULO:
162 case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;
164 case ISD::ADDE:
165 case ISD::SUBE:
166 case ISD::ADDCARRY:
167 case ISD::SUBCARRY: Res = PromoteIntRes_ADDSUBCARRY(N, ResNo); break;
169 case ISD::SADDO_CARRY:
170 case ISD::SSUBO_CARRY: Res = PromoteIntRes_SADDSUBO_CARRY(N, ResNo); break;
172 case ISD::SADDSAT:
173 case ISD::UADDSAT:
174 case ISD::SSUBSAT:
175 case ISD::USUBSAT:
176 case ISD::SSHLSAT:
177 case ISD::USHLSAT: Res = PromoteIntRes_ADDSUBSHLSAT(N); break;
179 case ISD::SMULFIX:
180 case ISD::SMULFIXSAT:
181 case ISD::UMULFIX:
182 case ISD::UMULFIXSAT: Res = PromoteIntRes_MULFIX(N); break;
184 case ISD::SDIVFIX:
185 case ISD::SDIVFIXSAT:
186 case ISD::UDIVFIX:
187 case ISD::UDIVFIXSAT: Res = PromoteIntRes_DIVFIX(N); break;
189 case ISD::ABS: Res = PromoteIntRes_ABS(N); break;
191 case ISD::ATOMIC_LOAD:
192 Res = PromoteIntRes_Atomic0(cast<AtomicSDNode>(N)); break;
194 case ISD::ATOMIC_LOAD_ADD:
195 case ISD::ATOMIC_LOAD_SUB:
196 case ISD::ATOMIC_LOAD_AND:
197 case ISD::ATOMIC_LOAD_CLR:
198 case ISD::ATOMIC_LOAD_OR:
199 case ISD::ATOMIC_LOAD_XOR:
200 case ISD::ATOMIC_LOAD_NAND:
201 case ISD::ATOMIC_LOAD_MIN:
202 case ISD::ATOMIC_LOAD_MAX:
203 case ISD::ATOMIC_LOAD_UMIN:
204 case ISD::ATOMIC_LOAD_UMAX:
205 case ISD::ATOMIC_SWAP:
206 Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
208 case ISD::ATOMIC_CMP_SWAP:
209 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
210 Res = PromoteIntRes_AtomicCmpSwap(cast<AtomicSDNode>(N), ResNo);
211 break;
213 case ISD::VECREDUCE_ADD:
214 case ISD::VECREDUCE_MUL:
215 case ISD::VECREDUCE_AND:
216 case ISD::VECREDUCE_OR:
217 case ISD::VECREDUCE_XOR:
218 case ISD::VECREDUCE_SMAX:
219 case ISD::VECREDUCE_SMIN:
220 case ISD::VECREDUCE_UMAX:
221 case ISD::VECREDUCE_UMIN:
222 Res = PromoteIntRes_VECREDUCE(N);
223 break;
225 case ISD::FREEZE:
226 Res = PromoteIntRes_FREEZE(N);
227 break;
229 case ISD::ROTL:
230 case ISD::ROTR:
231 Res = PromoteIntRes_Rotate(N);
232 break;
234 case ISD::FSHL:
235 case ISD::FSHR:
236 Res = PromoteIntRes_FunnelShift(N);
237 break;
240 // If the result is null then the sub-method took care of registering it.
241 if (Res.getNode())
242 SetPromotedInteger(SDValue(N, ResNo), Res);
245 SDValue DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode *N,
246 unsigned ResNo) {
247 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
248 return GetPromotedInteger(Op);
251 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
252 // Sign-extend the new bits, and continue the assertion.
253 SDValue Op = SExtPromotedInteger(N->getOperand(0));
254 return DAG.getNode(ISD::AssertSext, SDLoc(N),
255 Op.getValueType(), Op, N->getOperand(1));
258 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
259 // Zero the new bits, and continue the assertion.
260 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
261 return DAG.getNode(ISD::AssertZext, SDLoc(N),
262 Op.getValueType(), Op, N->getOperand(1));
265 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode *N) {
266 EVT ResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
267 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
268 N->getMemoryVT(), ResVT,
269 N->getChain(), N->getBasePtr(),
270 N->getMemOperand());
271 // Legalize the chain result - switch anything that used the old chain to
272 // use the new one.
273 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
274 return Res;
277 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
278 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
279 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
280 N->getMemoryVT(),
281 N->getChain(), N->getBasePtr(),
282 Op2, N->getMemOperand());
283 // Legalize the chain result - switch anything that used the old chain to
284 // use the new one.
285 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
286 return Res;
289 SDValue DAGTypeLegalizer::PromoteIntRes_AtomicCmpSwap(AtomicSDNode *N,
290 unsigned ResNo) {
291 if (ResNo == 1) {
292 assert(N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
293 EVT SVT = getSetCCResultType(N->getOperand(2).getValueType());
294 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
296 // Only use the result of getSetCCResultType if it is legal,
297 // otherwise just use the promoted result type (NVT).
298 if (!TLI.isTypeLegal(SVT))
299 SVT = NVT;
301 SDVTList VTs = DAG.getVTList(N->getValueType(0), SVT, MVT::Other);
302 SDValue Res = DAG.getAtomicCmpSwap(
303 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, SDLoc(N), N->getMemoryVT(), VTs,
304 N->getChain(), N->getBasePtr(), N->getOperand(2), N->getOperand(3),
305 N->getMemOperand());
306 ReplaceValueWith(SDValue(N, 0), Res.getValue(0));
307 ReplaceValueWith(SDValue(N, 2), Res.getValue(2));
308 return Res.getValue(1);
311 // Op2 is used for the comparison and thus must be extended according to the
312 // target's atomic operations. Op3 is merely stored and so can be left alone.
313 SDValue Op2 = N->getOperand(2);
314 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
315 switch (TLI.getExtendForAtomicCmpSwapArg()) {
316 case ISD::SIGN_EXTEND:
317 Op2 = SExtPromotedInteger(Op2);
318 break;
319 case ISD::ZERO_EXTEND:
320 Op2 = ZExtPromotedInteger(Op2);
321 break;
322 case ISD::ANY_EXTEND:
323 Op2 = GetPromotedInteger(Op2);
324 break;
325 default:
326 llvm_unreachable("Invalid atomic op extension");
329 SDVTList VTs =
330 DAG.getVTList(Op2.getValueType(), N->getValueType(1), MVT::Other);
331 SDValue Res = DAG.getAtomicCmpSwap(
332 N->getOpcode(), SDLoc(N), N->getMemoryVT(), VTs, N->getChain(),
333 N->getBasePtr(), Op2, Op3, N->getMemOperand());
334 // Update the use to N with the newly created Res.
335 for (unsigned i = 1, NumResults = N->getNumValues(); i < NumResults; ++i)
336 ReplaceValueWith(SDValue(N, i), Res.getValue(i));
337 return Res;
340 SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
341 SDValue InOp = N->getOperand(0);
342 EVT InVT = InOp.getValueType();
343 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
344 EVT OutVT = N->getValueType(0);
345 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
346 SDLoc dl(N);
348 switch (getTypeAction(InVT)) {
349 case TargetLowering::TypeLegal:
350 break;
351 case TargetLowering::TypePromoteInteger:
352 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector() && !NInVT.isVector())
353 // The input promotes to the same size. Convert the promoted value.
354 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp));
355 break;
356 case TargetLowering::TypeSoftenFloat:
357 // Promote the integer operand by hand.
358 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
359 case TargetLowering::TypeSoftPromoteHalf:
360 // Promote the integer operand by hand.
361 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftPromotedHalf(InOp));
362 case TargetLowering::TypePromoteFloat: {
363 // Convert the promoted float by hand.
364 if (!NOutVT.isVector())
365 return DAG.getNode(ISD::FP_TO_FP16, dl, NOutVT, GetPromotedFloat(InOp));
366 break;
368 case TargetLowering::TypeExpandInteger:
369 case TargetLowering::TypeExpandFloat:
370 break;
371 case TargetLowering::TypeScalarizeVector:
372 // Convert the element to an integer and promote it by hand.
373 if (!NOutVT.isVector())
374 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
375 BitConvertToInteger(GetScalarizedVector(InOp)));
376 break;
377 case TargetLowering::TypeScalarizeScalableVector:
378 report_fatal_error("Scalarization of scalable vectors is not supported.");
379 case TargetLowering::TypeSplitVector: {
380 if (!NOutVT.isVector()) {
381 // For example, i32 = BITCAST v2i16 on alpha. Convert the split
382 // pieces of the input into integers and reassemble in the final type.
383 SDValue Lo, Hi;
384 GetSplitVector(N->getOperand(0), Lo, Hi);
385 Lo = BitConvertToInteger(Lo);
386 Hi = BitConvertToInteger(Hi);
388 if (DAG.getDataLayout().isBigEndian())
389 std::swap(Lo, Hi);
391 InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
392 EVT::getIntegerVT(*DAG.getContext(),
393 NOutVT.getSizeInBits()),
394 JoinIntegers(Lo, Hi));
395 return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp);
397 break;
399 case TargetLowering::TypeWidenVector:
400 // The input is widened to the same size. Convert to the widened value.
401 // Make sure that the outgoing value is not a vector, because this would
402 // make us bitcast between two vectors which are legalized in different ways.
403 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector()) {
404 SDValue Res =
405 DAG.getNode(ISD::BITCAST, dl, NOutVT, GetWidenedVector(InOp));
407 // For big endian targets we need to shift the casted value or the
408 // interesting bits will end up at the wrong place.
409 if (DAG.getDataLayout().isBigEndian()) {
410 unsigned ShiftAmt = NInVT.getSizeInBits() - InVT.getSizeInBits();
411 EVT ShiftAmtTy = TLI.getShiftAmountTy(NOutVT, DAG.getDataLayout());
412 assert(ShiftAmt < NOutVT.getSizeInBits() && "Too large shift amount!");
413 Res = DAG.getNode(ISD::SRL, dl, NOutVT, Res,
414 DAG.getConstant(ShiftAmt, dl, ShiftAmtTy));
416 return Res;
418 // If the output type is also a vector and widening it to the same size
419 // as the widened input type would be a legal type, we can widen the bitcast
420 // and handle the promotion after.
421 if (NOutVT.isVector()) {
422 unsigned WidenInSize = NInVT.getSizeInBits();
423 unsigned OutSize = OutVT.getSizeInBits();
424 if (WidenInSize % OutSize == 0) {
425 unsigned Scale = WidenInSize / OutSize;
426 EVT WideOutVT = EVT::getVectorVT(*DAG.getContext(),
427 OutVT.getVectorElementType(),
428 OutVT.getVectorNumElements() * Scale);
429 if (isTypeLegal(WideOutVT)) {
430 InOp = DAG.getBitcast(WideOutVT, GetWidenedVector(InOp));
431 InOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, InOp,
432 DAG.getVectorIdxConstant(0, dl));
433 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, InOp);
439 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
440 CreateStackStoreLoad(InOp, OutVT));
443 // Helper for BSWAP/BITREVERSE promotion to ensure we can fit any shift amount
444 // in the VT returned by getShiftAmountTy and to return a safe VT if we can't.
445 static EVT getShiftAmountTyForConstant(EVT VT, const TargetLowering &TLI,
446 SelectionDAG &DAG) {
447 EVT ShiftVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
448 // If any possible shift value won't fit in the prefered type, just use
449 // something safe. It will be legalized when the shift is expanded.
450 if (!ShiftVT.isVector() &&
451 ShiftVT.getSizeInBits() < Log2_32_Ceil(VT.getSizeInBits()))
452 ShiftVT = MVT::i32;
453 return ShiftVT;
456 SDValue DAGTypeLegalizer::PromoteIntRes_FREEZE(SDNode *N) {
457 SDValue V = GetPromotedInteger(N->getOperand(0));
458 return DAG.getNode(ISD::FREEZE, SDLoc(N),
459 V.getValueType(), V);
462 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
463 SDValue Op = GetPromotedInteger(N->getOperand(0));
464 EVT OVT = N->getValueType(0);
465 EVT NVT = Op.getValueType();
466 SDLoc dl(N);
468 // If the larger BSWAP isn't supported by the target, try to expand now.
469 // If we expand later we'll end up with more operations since we lost the
470 // original type. We only do this for scalars since we have a shuffle
471 // based lowering for vectors in LegalizeVectorOps.
472 if (!OVT.isVector() &&
473 !TLI.isOperationLegalOrCustomOrPromote(ISD::BSWAP, NVT)) {
474 if (SDValue Res = TLI.expandBSWAP(N, DAG))
475 return DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Res);
478 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
479 EVT ShiftVT = getShiftAmountTyForConstant(NVT, TLI, DAG);
480 return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
481 DAG.getConstant(DiffBits, dl, ShiftVT));
484 SDValue DAGTypeLegalizer::PromoteIntRes_BITREVERSE(SDNode *N) {
485 SDValue Op = GetPromotedInteger(N->getOperand(0));
486 EVT OVT = N->getValueType(0);
487 EVT NVT = Op.getValueType();
488 SDLoc dl(N);
490 // If the larger BITREVERSE isn't supported by the target, try to expand now.
491 // If we expand later we'll end up with more operations since we lost the
492 // original type. We only do this for scalars since we have a shuffle
493 // based lowering for vectors in LegalizeVectorOps.
494 if (!OVT.isVector() && OVT.isSimple() &&
495 !TLI.isOperationLegalOrCustomOrPromote(ISD::BITREVERSE, NVT)) {
496 if (SDValue Res = TLI.expandBITREVERSE(N, DAG))
497 return DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Res);
500 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
501 EVT ShiftVT = getShiftAmountTyForConstant(NVT, TLI, DAG);
502 return DAG.getNode(ISD::SRL, dl, NVT,
503 DAG.getNode(ISD::BITREVERSE, dl, NVT, Op),
504 DAG.getConstant(DiffBits, dl, ShiftVT));
507 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
508 // The pair element type may be legal, or may not promote to the same type as
509 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
510 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N),
511 TLI.getTypeToTransformTo(*DAG.getContext(),
512 N->getValueType(0)), JoinIntegers(N->getOperand(0),
513 N->getOperand(1)));
516 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
517 EVT VT = N->getValueType(0);
518 // FIXME there is no actual debug info here
519 SDLoc dl(N);
520 // Zero extend things like i1, sign extend everything else. It shouldn't
521 // matter in theory which one we pick, but this tends to give better code?
522 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
523 SDValue Result = DAG.getNode(Opc, dl,
524 TLI.getTypeToTransformTo(*DAG.getContext(), VT),
525 SDValue(N, 0));
526 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
527 return Result;
530 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
531 // Zero extend to the promoted type and do the count there.
532 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
533 SDLoc dl(N);
534 EVT OVT = N->getValueType(0);
535 EVT NVT = Op.getValueType();
536 Op = DAG.getNode(N->getOpcode(), dl, NVT, Op);
537 // Subtract off the extra leading bits in the bigger type.
538 return DAG.getNode(
539 ISD::SUB, dl, NVT, Op,
540 DAG.getConstant(NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(), dl,
541 NVT));
544 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP_PARITY(SDNode *N) {
545 // Zero extend to the promoted type and do the count or parity there.
546 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
547 return DAG.getNode(N->getOpcode(), SDLoc(N), Op.getValueType(), Op);
550 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
551 SDValue Op = GetPromotedInteger(N->getOperand(0));
552 EVT OVT = N->getValueType(0);
553 EVT NVT = Op.getValueType();
554 SDLoc dl(N);
555 if (N->getOpcode() == ISD::CTTZ) {
556 // The count is the same in the promoted type except if the original
557 // value was zero. This can be handled by setting the bit just off
558 // the top of the original type.
559 auto TopBit = APInt::getOneBitSet(NVT.getScalarSizeInBits(),
560 OVT.getScalarSizeInBits());
561 Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, dl, NVT));
563 return DAG.getNode(N->getOpcode(), dl, NVT, Op);
566 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
567 SDLoc dl(N);
568 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
570 SDValue Op0 = N->getOperand(0);
571 SDValue Op1 = N->getOperand(1);
573 // If the input also needs to be promoted, do that first so we can get a
574 // get a good idea for the output type.
575 if (TLI.getTypeAction(*DAG.getContext(), Op0.getValueType())
576 == TargetLowering::TypePromoteInteger) {
577 SDValue In = GetPromotedInteger(Op0);
579 // If the new type is larger than NVT, use it. We probably won't need to
580 // promote it again.
581 EVT SVT = In.getValueType().getScalarType();
582 if (SVT.bitsGE(NVT)) {
583 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SVT, In, Op1);
584 return DAG.getAnyExtOrTrunc(Ext, dl, NVT);
588 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, Op0, Op1);
591 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
592 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
593 unsigned NewOpc = N->getOpcode();
594 SDLoc dl(N);
596 // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
597 // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT
598 // and SINT conversions are Custom, there is no way to tell which is
599 // preferable. We choose SINT because that's the right thing on PPC.)
600 if (N->getOpcode() == ISD::FP_TO_UINT &&
601 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
602 TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
603 NewOpc = ISD::FP_TO_SINT;
605 if (N->getOpcode() == ISD::STRICT_FP_TO_UINT &&
606 !TLI.isOperationLegal(ISD::STRICT_FP_TO_UINT, NVT) &&
607 TLI.isOperationLegalOrCustom(ISD::STRICT_FP_TO_SINT, NVT))
608 NewOpc = ISD::STRICT_FP_TO_SINT;
610 SDValue Res;
611 if (N->isStrictFPOpcode()) {
612 Res = DAG.getNode(NewOpc, dl, {NVT, MVT::Other},
613 {N->getOperand(0), N->getOperand(1)});
614 // Legalize the chain result - switch anything that used the old chain to
615 // use the new one.
616 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
617 } else
618 Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
620 // Assert that the converted value fits in the original type. If it doesn't
621 // (eg: because the value being converted is too big), then the result of the
622 // original operation was undefined anyway, so the assert is still correct.
624 // NOTE: fp-to-uint to fp-to-sint promotion guarantees zero extend. For example:
625 // before legalization: fp-to-uint16, 65534. -> 0xfffe
626 // after legalization: fp-to-sint32, 65534. -> 0x0000fffe
627 return DAG.getNode((N->getOpcode() == ISD::FP_TO_UINT ||
628 N->getOpcode() == ISD::STRICT_FP_TO_UINT) ?
629 ISD::AssertZext : ISD::AssertSext, dl, NVT, Res,
630 DAG.getValueType(N->getValueType(0).getScalarType()));
633 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT_SAT(SDNode *N) {
634 // Promote the result type, while keeping the original width in Op1.
635 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
636 SDLoc dl(N);
637 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0),
638 N->getOperand(1));
641 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_FP16(SDNode *N) {
642 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
643 SDLoc dl(N);
645 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
648 SDValue DAGTypeLegalizer::PromoteIntRes_FLT_ROUNDS(SDNode *N) {
649 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
650 SDLoc dl(N);
652 SDValue Res =
653 DAG.getNode(N->getOpcode(), dl, {NVT, MVT::Other}, N->getOperand(0));
655 // Legalize the chain result - switch anything that used the old chain to
656 // use the new one.
657 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
658 return Res;
661 SDValue DAGTypeLegalizer::PromoteIntRes_ISNAN(SDNode *N) {
662 SDLoc DL(N);
663 EVT ResultVT = N->getValueType(0);
664 EVT NewResultVT = TLI.getTypeToTransformTo(*DAG.getContext(), ResultVT);
665 return DAG.getNode(N->getOpcode(), DL, NewResultVT, N->getOperand(0),
666 N->getFlags());
669 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
670 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
671 SDLoc dl(N);
673 if (getTypeAction(N->getOperand(0).getValueType())
674 == TargetLowering::TypePromoteInteger) {
675 SDValue Res = GetPromotedInteger(N->getOperand(0));
676 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
678 // If the result and operand types are the same after promotion, simplify
679 // to an in-register extension.
680 if (NVT == Res.getValueType()) {
681 // The high bits are not guaranteed to be anything. Insert an extend.
682 if (N->getOpcode() == ISD::SIGN_EXTEND)
683 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
684 DAG.getValueType(N->getOperand(0).getValueType()));
685 if (N->getOpcode() == ISD::ZERO_EXTEND)
686 return DAG.getZeroExtendInReg(Res, dl, N->getOperand(0).getValueType());
687 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
688 return Res;
692 // Otherwise, just extend the original operand all the way to the larger type.
693 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
696 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
697 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
698 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
699 ISD::LoadExtType ExtType =
700 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
701 SDLoc dl(N);
702 SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
703 N->getMemoryVT(), N->getMemOperand());
705 // Legalize the chain result - switch anything that used the old chain to
706 // use the new one.
707 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
708 return Res;
711 SDValue DAGTypeLegalizer::PromoteIntRes_MLOAD(MaskedLoadSDNode *N) {
712 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
713 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
715 SDLoc dl(N);
716 SDValue Res = DAG.getMaskedLoad(NVT, dl, N->getChain(), N->getBasePtr(),
717 N->getOffset(), N->getMask(), ExtPassThru,
718 N->getMemoryVT(), N->getMemOperand(),
719 N->getAddressingMode(), ISD::EXTLOAD);
720 // Legalize the chain result - switch anything that used the old chain to
721 // use the new one.
722 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
723 return Res;
726 SDValue DAGTypeLegalizer::PromoteIntRes_MGATHER(MaskedGatherSDNode *N) {
727 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
728 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
729 assert(NVT == ExtPassThru.getValueType() &&
730 "Gather result type and the passThru argument type should be the same");
732 ISD::LoadExtType ExtType = N->getExtensionType();
733 if (ExtType == ISD::NON_EXTLOAD)
734 ExtType = ISD::EXTLOAD;
736 SDLoc dl(N);
737 SDValue Ops[] = {N->getChain(), ExtPassThru, N->getMask(), N->getBasePtr(),
738 N->getIndex(), N->getScale() };
739 SDValue Res = DAG.getMaskedGather(DAG.getVTList(NVT, MVT::Other),
740 N->getMemoryVT(), dl, Ops,
741 N->getMemOperand(), N->getIndexType(),
742 ExtType);
743 // Legalize the chain result - switch anything that used the old chain to
744 // use the new one.
745 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
746 return Res;
749 /// Promote the overflow flag of an overflowing arithmetic node.
750 SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
751 // Change the return type of the boolean result while obeying
752 // getSetCCResultType.
753 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
754 EVT VT = N->getValueType(0);
755 EVT SVT = getSetCCResultType(VT);
756 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1) };
757 unsigned NumOps = N->getNumOperands();
758 assert(NumOps <= 3 && "Too many operands");
759 if (NumOps == 3)
760 Ops[2] = N->getOperand(2);
762 SDLoc dl(N);
763 SDValue Res = DAG.getNode(N->getOpcode(), dl, DAG.getVTList(VT, SVT),
764 makeArrayRef(Ops, NumOps));
766 // Modified the sum result - switch anything that used the old sum to use
767 // the new one.
768 ReplaceValueWith(SDValue(N, 0), Res);
770 // Convert to the expected type.
771 return DAG.getBoolExtOrTrunc(Res.getValue(1), dl, NVT, VT);
774 SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBSHLSAT(SDNode *N) {
775 // If the promoted type is legal, we can convert this to:
776 // 1. ANY_EXTEND iN to iM
777 // 2. SHL by M-N
778 // 3. [US][ADD|SUB|SHL]SAT
779 // 4. L/ASHR by M-N
780 // Else it is more efficient to convert this to a min and a max
781 // operation in the higher precision arithmetic.
782 SDLoc dl(N);
783 SDValue Op1 = N->getOperand(0);
784 SDValue Op2 = N->getOperand(1);
785 unsigned OldBits = Op1.getScalarValueSizeInBits();
787 unsigned Opcode = N->getOpcode();
788 bool IsShift = Opcode == ISD::USHLSAT || Opcode == ISD::SSHLSAT;
790 SDValue Op1Promoted, Op2Promoted;
791 if (IsShift) {
792 Op1Promoted = GetPromotedInteger(Op1);
793 Op2Promoted = ZExtPromotedInteger(Op2);
794 } else if (Opcode == ISD::UADDSAT || Opcode == ISD::USUBSAT) {
795 Op1Promoted = ZExtPromotedInteger(Op1);
796 Op2Promoted = ZExtPromotedInteger(Op2);
797 } else {
798 Op1Promoted = SExtPromotedInteger(Op1);
799 Op2Promoted = SExtPromotedInteger(Op2);
801 EVT PromotedType = Op1Promoted.getValueType();
802 unsigned NewBits = PromotedType.getScalarSizeInBits();
804 if (Opcode == ISD::UADDSAT) {
805 APInt MaxVal = APInt::getAllOnesValue(OldBits).zext(NewBits);
806 SDValue SatMax = DAG.getConstant(MaxVal, dl, PromotedType);
807 SDValue Add =
808 DAG.getNode(ISD::ADD, dl, PromotedType, Op1Promoted, Op2Promoted);
809 return DAG.getNode(ISD::UMIN, dl, PromotedType, Add, SatMax);
812 // USUBSAT can always be promoted as long as we have zero-extended the args.
813 if (Opcode == ISD::USUBSAT)
814 return DAG.getNode(ISD::USUBSAT, dl, PromotedType, Op1Promoted,
815 Op2Promoted);
817 // Shift cannot use a min/max expansion, we can't detect overflow if all of
818 // the bits have been shifted out.
819 if (IsShift || TLI.isOperationLegal(Opcode, PromotedType)) {
820 unsigned ShiftOp;
821 switch (Opcode) {
822 case ISD::SADDSAT:
823 case ISD::SSUBSAT:
824 case ISD::SSHLSAT:
825 ShiftOp = ISD::SRA;
826 break;
827 case ISD::USHLSAT:
828 ShiftOp = ISD::SRL;
829 break;
830 default:
831 llvm_unreachable("Expected opcode to be signed or unsigned saturation "
832 "addition, subtraction or left shift");
835 unsigned SHLAmount = NewBits - OldBits;
836 EVT SHVT = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
837 SDValue ShiftAmount = DAG.getConstant(SHLAmount, dl, SHVT);
838 Op1Promoted =
839 DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted, ShiftAmount);
840 if (!IsShift)
841 Op2Promoted =
842 DAG.getNode(ISD::SHL, dl, PromotedType, Op2Promoted, ShiftAmount);
844 SDValue Result =
845 DAG.getNode(Opcode, dl, PromotedType, Op1Promoted, Op2Promoted);
846 return DAG.getNode(ShiftOp, dl, PromotedType, Result, ShiftAmount);
849 unsigned AddOp = Opcode == ISD::SADDSAT ? ISD::ADD : ISD::SUB;
850 APInt MinVal = APInt::getSignedMinValue(OldBits).sext(NewBits);
851 APInt MaxVal = APInt::getSignedMaxValue(OldBits).sext(NewBits);
852 SDValue SatMin = DAG.getConstant(MinVal, dl, PromotedType);
853 SDValue SatMax = DAG.getConstant(MaxVal, dl, PromotedType);
854 SDValue Result =
855 DAG.getNode(AddOp, dl, PromotedType, Op1Promoted, Op2Promoted);
856 Result = DAG.getNode(ISD::SMIN, dl, PromotedType, Result, SatMax);
857 Result = DAG.getNode(ISD::SMAX, dl, PromotedType, Result, SatMin);
858 return Result;
861 SDValue DAGTypeLegalizer::PromoteIntRes_MULFIX(SDNode *N) {
862 // Can just promote the operands then continue with operation.
863 SDLoc dl(N);
864 SDValue Op1Promoted, Op2Promoted;
865 bool Signed =
866 N->getOpcode() == ISD::SMULFIX || N->getOpcode() == ISD::SMULFIXSAT;
867 bool Saturating =
868 N->getOpcode() == ISD::SMULFIXSAT || N->getOpcode() == ISD::UMULFIXSAT;
869 if (Signed) {
870 Op1Promoted = SExtPromotedInteger(N->getOperand(0));
871 Op2Promoted = SExtPromotedInteger(N->getOperand(1));
872 } else {
873 Op1Promoted = ZExtPromotedInteger(N->getOperand(0));
874 Op2Promoted = ZExtPromotedInteger(N->getOperand(1));
876 EVT OldType = N->getOperand(0).getValueType();
877 EVT PromotedType = Op1Promoted.getValueType();
878 unsigned DiffSize =
879 PromotedType.getScalarSizeInBits() - OldType.getScalarSizeInBits();
881 if (Saturating) {
882 // Promoting the operand and result values changes the saturation width,
883 // which is extends the values that we clamp to on saturation. This could be
884 // resolved by shifting one of the operands the same amount, which would
885 // also shift the result we compare against, then shifting back.
886 EVT ShiftTy = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
887 Op1Promoted = DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted,
888 DAG.getConstant(DiffSize, dl, ShiftTy));
889 SDValue Result = DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted,
890 Op2Promoted, N->getOperand(2));
891 unsigned ShiftOp = Signed ? ISD::SRA : ISD::SRL;
892 return DAG.getNode(ShiftOp, dl, PromotedType, Result,
893 DAG.getConstant(DiffSize, dl, ShiftTy));
895 return DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted, Op2Promoted,
896 N->getOperand(2));
899 static SDValue SaturateWidenedDIVFIX(SDValue V, SDLoc &dl,
900 unsigned SatW, bool Signed,
901 const TargetLowering &TLI,
902 SelectionDAG &DAG) {
903 EVT VT = V.getValueType();
904 unsigned VTW = VT.getScalarSizeInBits();
906 if (!Signed) {
907 // Saturate to the unsigned maximum by getting the minimum of V and the
908 // maximum.
909 return DAG.getNode(ISD::UMIN, dl, VT, V,
910 DAG.getConstant(APInt::getLowBitsSet(VTW, SatW),
911 dl, VT));
914 // Saturate to the signed maximum (the low SatW - 1 bits) by taking the
915 // signed minimum of it and V.
916 V = DAG.getNode(ISD::SMIN, dl, VT, V,
917 DAG.getConstant(APInt::getLowBitsSet(VTW, SatW - 1),
918 dl, VT));
919 // Saturate to the signed minimum (the high SatW + 1 bits) by taking the
920 // signed maximum of it and V.
921 V = DAG.getNode(ISD::SMAX, dl, VT, V,
922 DAG.getConstant(APInt::getHighBitsSet(VTW, VTW - SatW + 1),
923 dl, VT));
924 return V;
927 static SDValue earlyExpandDIVFIX(SDNode *N, SDValue LHS, SDValue RHS,
928 unsigned Scale, const TargetLowering &TLI,
929 SelectionDAG &DAG, unsigned SatW = 0) {
930 EVT VT = LHS.getValueType();
931 unsigned VTSize = VT.getScalarSizeInBits();
932 bool Signed = N->getOpcode() == ISD::SDIVFIX ||
933 N->getOpcode() == ISD::SDIVFIXSAT;
934 bool Saturating = N->getOpcode() == ISD::SDIVFIXSAT ||
935 N->getOpcode() == ISD::UDIVFIXSAT;
937 SDLoc dl(N);
938 // Widen the types by a factor of two. This is guaranteed to expand, since it
939 // will always have enough high bits in the LHS to shift into.
940 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VTSize * 2);
941 if (VT.isVector())
942 WideVT = EVT::getVectorVT(*DAG.getContext(), WideVT,
943 VT.getVectorElementCount());
944 if (Signed) {
945 LHS = DAG.getSExtOrTrunc(LHS, dl, WideVT);
946 RHS = DAG.getSExtOrTrunc(RHS, dl, WideVT);
947 } else {
948 LHS = DAG.getZExtOrTrunc(LHS, dl, WideVT);
949 RHS = DAG.getZExtOrTrunc(RHS, dl, WideVT);
952 SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, LHS, RHS, Scale,
953 DAG);
954 assert(Res && "Expanding DIVFIX with wide type failed?");
955 if (Saturating) {
956 // If the caller has told us to saturate at something less, use that width
957 // instead of the type before doubling. However, it cannot be more than
958 // what we just widened!
959 assert(SatW <= VTSize &&
960 "Tried to saturate to more than the original type?");
961 Res = SaturateWidenedDIVFIX(Res, dl, SatW == 0 ? VTSize : SatW, Signed,
962 TLI, DAG);
964 return DAG.getZExtOrTrunc(Res, dl, VT);
967 SDValue DAGTypeLegalizer::PromoteIntRes_DIVFIX(SDNode *N) {
968 SDLoc dl(N);
969 SDValue Op1Promoted, Op2Promoted;
970 bool Signed = N->getOpcode() == ISD::SDIVFIX ||
971 N->getOpcode() == ISD::SDIVFIXSAT;
972 bool Saturating = N->getOpcode() == ISD::SDIVFIXSAT ||
973 N->getOpcode() == ISD::UDIVFIXSAT;
974 if (Signed) {
975 Op1Promoted = SExtPromotedInteger(N->getOperand(0));
976 Op2Promoted = SExtPromotedInteger(N->getOperand(1));
977 } else {
978 Op1Promoted = ZExtPromotedInteger(N->getOperand(0));
979 Op2Promoted = ZExtPromotedInteger(N->getOperand(1));
981 EVT PromotedType = Op1Promoted.getValueType();
982 unsigned Scale = N->getConstantOperandVal(2);
984 // If the type is already legal and the operation is legal in that type, we
985 // should not early expand.
986 if (TLI.isTypeLegal(PromotedType)) {
987 TargetLowering::LegalizeAction Action =
988 TLI.getFixedPointOperationAction(N->getOpcode(), PromotedType, Scale);
989 if (Action == TargetLowering::Legal || Action == TargetLowering::Custom) {
990 EVT ShiftTy = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
991 unsigned Diff = PromotedType.getScalarSizeInBits() -
992 N->getValueType(0).getScalarSizeInBits();
993 if (Saturating)
994 Op1Promoted = DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted,
995 DAG.getConstant(Diff, dl, ShiftTy));
996 SDValue Res = DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted,
997 Op2Promoted, N->getOperand(2));
998 if (Saturating)
999 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, dl, PromotedType, Res,
1000 DAG.getConstant(Diff, dl, ShiftTy));
1001 return Res;
1005 // See if we can perform the division in this type without expanding.
1006 if (SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, Op1Promoted,
1007 Op2Promoted, Scale, DAG)) {
1008 if (Saturating)
1009 Res = SaturateWidenedDIVFIX(Res, dl,
1010 N->getValueType(0).getScalarSizeInBits(),
1011 Signed, TLI, DAG);
1012 return Res;
1014 // If we cannot, expand it to twice the type width. If we are saturating, give
1015 // it the original width as a saturating width so we don't need to emit
1016 // two saturations.
1017 return earlyExpandDIVFIX(N, Op1Promoted, Op2Promoted, Scale, TLI, DAG,
1018 N->getValueType(0).getScalarSizeInBits());
1021 SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
1022 if (ResNo == 1)
1023 return PromoteIntRes_Overflow(N);
1025 // The operation overflowed iff the result in the larger type is not the
1026 // sign extension of its truncation to the original type.
1027 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1028 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1029 EVT OVT = N->getOperand(0).getValueType();
1030 EVT NVT = LHS.getValueType();
1031 SDLoc dl(N);
1033 // Do the arithmetic in the larger type.
1034 unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
1035 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
1037 // Calculate the overflow flag: sign extend the arithmetic result from
1038 // the original type.
1039 SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
1040 DAG.getValueType(OVT));
1041 // Overflowed if and only if this is not equal to Res.
1042 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
1044 // Use the calculated overflow everywhere.
1045 ReplaceValueWith(SDValue(N, 1), Ofl);
1047 return Res;
1050 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
1051 SDValue LHS = GetPromotedInteger(N->getOperand(1));
1052 SDValue RHS = GetPromotedInteger(N->getOperand(2));
1053 return DAG.getSelect(SDLoc(N),
1054 LHS.getValueType(), N->getOperand(0), LHS, RHS);
1057 SDValue DAGTypeLegalizer::PromoteIntRes_VSELECT(SDNode *N) {
1058 SDValue Mask = N->getOperand(0);
1060 SDValue LHS = GetPromotedInteger(N->getOperand(1));
1061 SDValue RHS = GetPromotedInteger(N->getOperand(2));
1062 return DAG.getNode(ISD::VSELECT, SDLoc(N),
1063 LHS.getValueType(), Mask, LHS, RHS);
1066 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
1067 SDValue LHS = GetPromotedInteger(N->getOperand(2));
1068 SDValue RHS = GetPromotedInteger(N->getOperand(3));
1069 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
1070 LHS.getValueType(), N->getOperand(0),
1071 N->getOperand(1), LHS, RHS, N->getOperand(4));
1074 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
1075 unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0;
1076 EVT InVT = N->getOperand(OpNo).getValueType();
1077 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1079 EVT SVT = getSetCCResultType(InVT);
1081 // If we got back a type that needs to be promoted, this likely means the
1082 // the input type also needs to be promoted. So get the promoted type for
1083 // the input and try the query again.
1084 if (getTypeAction(SVT) == TargetLowering::TypePromoteInteger) {
1085 if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
1086 InVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
1087 SVT = getSetCCResultType(InVT);
1088 } else {
1089 // Input type isn't promoted, just use the default promoted type.
1090 SVT = NVT;
1094 SDLoc dl(N);
1095 assert(SVT.isVector() == N->getOperand(OpNo).getValueType().isVector() &&
1096 "Vector compare must return a vector result!");
1098 // Get the SETCC result using the canonical SETCC type.
1099 SDValue SetCC;
1100 if (N->isStrictFPOpcode()) {
1101 EVT VTs[] = {SVT, MVT::Other};
1102 SDValue Opers[] = {N->getOperand(0), N->getOperand(1),
1103 N->getOperand(2), N->getOperand(3)};
1104 SetCC = DAG.getNode(N->getOpcode(), dl, VTs, Opers);
1105 // Legalize the chain result - switch anything that used the old chain to
1106 // use the new one.
1107 ReplaceValueWith(SDValue(N, 1), SetCC.getValue(1));
1108 } else
1109 SetCC = DAG.getNode(N->getOpcode(), dl, SVT, N->getOperand(0),
1110 N->getOperand(1), N->getOperand(2));
1112 // Convert to the expected type.
1113 return DAG.getSExtOrTrunc(SetCC, dl, NVT);
1116 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
1117 SDValue LHS = GetPromotedInteger(N->getOperand(0));
1118 SDValue RHS = N->getOperand(1);
1119 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1120 RHS = ZExtPromotedInteger(RHS);
1121 return DAG.getNode(ISD::SHL, SDLoc(N), LHS.getValueType(), LHS, RHS);
1124 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
1125 SDValue Op = GetPromotedInteger(N->getOperand(0));
1126 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N),
1127 Op.getValueType(), Op, N->getOperand(1));
1130 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
1131 // The input may have strange things in the top bits of the registers, but
1132 // these operations don't care. They may have weird bits going out, but
1133 // that too is okay if they are integer operations.
1134 SDValue LHS = GetPromotedInteger(N->getOperand(0));
1135 SDValue RHS = GetPromotedInteger(N->getOperand(1));
1136 return DAG.getNode(N->getOpcode(), SDLoc(N),
1137 LHS.getValueType(), LHS, RHS);
1140 SDValue DAGTypeLegalizer::PromoteIntRes_SExtIntBinOp(SDNode *N) {
1141 // Sign extend the input.
1142 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1143 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1144 return DAG.getNode(N->getOpcode(), SDLoc(N),
1145 LHS.getValueType(), LHS, RHS);
1148 SDValue DAGTypeLegalizer::PromoteIntRes_ZExtIntBinOp(SDNode *N) {
1149 // Zero extend the input.
1150 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1151 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
1152 return DAG.getNode(N->getOpcode(), SDLoc(N),
1153 LHS.getValueType(), LHS, RHS);
1156 SDValue DAGTypeLegalizer::PromoteIntRes_UMINUMAX(SDNode *N) {
1157 // It doesn't matter if we sign extend or zero extend in the inputs. So do
1158 // whatever is best for the target.
1159 SDValue LHS = SExtOrZExtPromotedInteger(N->getOperand(0));
1160 SDValue RHS = SExtOrZExtPromotedInteger(N->getOperand(1));
1161 return DAG.getNode(N->getOpcode(), SDLoc(N),
1162 LHS.getValueType(), LHS, RHS);
1165 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
1166 // The input value must be properly sign extended.
1167 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1168 SDValue RHS = N->getOperand(1);
1169 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1170 RHS = ZExtPromotedInteger(RHS);
1171 return DAG.getNode(ISD::SRA, SDLoc(N), LHS.getValueType(), LHS, RHS);
1174 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
1175 // The input value must be properly zero extended.
1176 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1177 SDValue RHS = N->getOperand(1);
1178 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1179 RHS = ZExtPromotedInteger(RHS);
1180 return DAG.getNode(ISD::SRL, SDLoc(N), LHS.getValueType(), LHS, RHS);
1183 SDValue DAGTypeLegalizer::PromoteIntRes_Rotate(SDNode *N) {
1184 // Lower the rotate to shifts and ORs which can be promoted.
1185 SDValue Res;
1186 TLI.expandROT(N, true /*AllowVectorOps*/, Res, DAG);
1187 ReplaceValueWith(SDValue(N, 0), Res);
1188 return SDValue();
1191 SDValue DAGTypeLegalizer::PromoteIntRes_FunnelShift(SDNode *N) {
1192 SDValue Hi = GetPromotedInteger(N->getOperand(0));
1193 SDValue Lo = GetPromotedInteger(N->getOperand(1));
1194 SDValue Amount = GetPromotedInteger(N->getOperand(2));
1196 SDLoc DL(N);
1197 EVT OldVT = N->getOperand(0).getValueType();
1198 EVT VT = Lo.getValueType();
1199 unsigned Opcode = N->getOpcode();
1200 bool IsFSHR = Opcode == ISD::FSHR;
1201 unsigned OldBits = OldVT.getScalarSizeInBits();
1202 unsigned NewBits = VT.getScalarSizeInBits();
1204 // Amount has to be interpreted modulo the old bit width.
1205 Amount =
1206 DAG.getNode(ISD::UREM, DL, VT, Amount, DAG.getConstant(OldBits, DL, VT));
1208 // If the promoted type is twice the size (or more), then we use the
1209 // traditional funnel 'double' shift codegen. This isn't necessary if the
1210 // shift amount is constant.
1211 // fshl(x,y,z) -> (((aext(x) << bw) | zext(y)) << (z % bw)) >> bw.
1212 // fshr(x,y,z) -> (((aext(x) << bw) | zext(y)) >> (z % bw)).
1213 if (NewBits >= (2 * OldBits) && !isa<ConstantSDNode>(Amount) &&
1214 !TLI.isOperationLegalOrCustom(Opcode, VT)) {
1215 SDValue HiShift = DAG.getConstant(OldBits, DL, VT);
1216 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, HiShift);
1217 Lo = DAG.getZeroExtendInReg(Lo, DL, OldVT);
1218 SDValue Res = DAG.getNode(ISD::OR, DL, VT, Hi, Lo);
1219 Res = DAG.getNode(IsFSHR ? ISD::SRL : ISD::SHL, DL, VT, Res, Amount);
1220 if (!IsFSHR)
1221 Res = DAG.getNode(ISD::SRL, DL, VT, Res, HiShift);
1222 return Res;
1225 // Shift Lo up to occupy the upper bits of the promoted type.
1226 SDValue ShiftOffset = DAG.getConstant(NewBits - OldBits, DL, VT);
1227 Lo = DAG.getNode(ISD::SHL, DL, VT, Lo, ShiftOffset);
1229 // Increase Amount to shift the result into the lower bits of the promoted
1230 // type.
1231 if (IsFSHR)
1232 Amount = DAG.getNode(ISD::ADD, DL, VT, Amount, ShiftOffset);
1234 return DAG.getNode(Opcode, DL, VT, Hi, Lo, Amount);
1237 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
1238 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1239 SDValue Res;
1240 SDValue InOp = N->getOperand(0);
1241 SDLoc dl(N);
1243 switch (getTypeAction(InOp.getValueType())) {
1244 default: llvm_unreachable("Unknown type action!");
1245 case TargetLowering::TypeLegal:
1246 case TargetLowering::TypeExpandInteger:
1247 Res = InOp;
1248 break;
1249 case TargetLowering::TypePromoteInteger:
1250 Res = GetPromotedInteger(InOp);
1251 break;
1252 case TargetLowering::TypeSplitVector: {
1253 EVT InVT = InOp.getValueType();
1254 assert(InVT.isVector() && "Cannot split scalar types");
1255 ElementCount NumElts = InVT.getVectorElementCount();
1256 assert(NumElts == NVT.getVectorElementCount() &&
1257 "Dst and Src must have the same number of elements");
1258 assert(isPowerOf2_32(NumElts.getKnownMinValue()) &&
1259 "Promoted vector type must be a power of two");
1261 SDValue EOp1, EOp2;
1262 GetSplitVector(InOp, EOp1, EOp2);
1264 EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(),
1265 NumElts.divideCoefficientBy(2));
1266 EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1);
1267 EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2);
1269 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2);
1271 case TargetLowering::TypeWidenVector: {
1272 SDValue WideInOp = GetWidenedVector(InOp);
1274 // Truncate widened InOp.
1275 unsigned NumElem = WideInOp.getValueType().getVectorNumElements();
1276 EVT TruncVT = EVT::getVectorVT(*DAG.getContext(),
1277 N->getValueType(0).getScalarType(), NumElem);
1278 SDValue WideTrunc = DAG.getNode(ISD::TRUNCATE, dl, TruncVT, WideInOp);
1280 // Zero extend so that the elements are of same type as those of NVT
1281 EVT ExtVT = EVT::getVectorVT(*DAG.getContext(), NVT.getVectorElementType(),
1282 NumElem);
1283 SDValue WideExt = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtVT, WideTrunc);
1285 // Extract the low NVT subvector.
1286 SDValue ZeroIdx = DAG.getVectorIdxConstant(0, dl);
1287 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, WideExt, ZeroIdx);
1291 // Truncate to NVT instead of VT
1292 return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res);
1295 SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
1296 if (ResNo == 1)
1297 return PromoteIntRes_Overflow(N);
1299 // The operation overflowed iff the result in the larger type is not the
1300 // zero extension of its truncation to the original type.
1301 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1302 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
1303 EVT OVT = N->getOperand(0).getValueType();
1304 EVT NVT = LHS.getValueType();
1305 SDLoc dl(N);
1307 // Do the arithmetic in the larger type.
1308 unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
1309 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
1311 // Calculate the overflow flag: zero extend the arithmetic result from
1312 // the original type.
1313 SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT);
1314 // Overflowed if and only if this is not equal to Res.
1315 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
1317 // Use the calculated overflow everywhere.
1318 ReplaceValueWith(SDValue(N, 1), Ofl);
1320 return Res;
1323 // Handle promotion for the ADDE/SUBE/ADDCARRY/SUBCARRY nodes. Notice that
1324 // the third operand of ADDE/SUBE nodes is carry flag, which differs from
1325 // the ADDCARRY/SUBCARRY nodes in that the third operand is carry Boolean.
1326 SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBCARRY(SDNode *N, unsigned ResNo) {
1327 if (ResNo == 1)
1328 return PromoteIntRes_Overflow(N);
1330 // We need to sign-extend the operands so the carry value computed by the
1331 // wide operation will be equivalent to the carry value computed by the
1332 // narrow operation.
1333 // An ADDCARRY can generate carry only if any of the operands has its
1334 // most significant bit set. Sign extension propagates the most significant
1335 // bit into the higher bits which means the extra bit that the narrow
1336 // addition would need (i.e. the carry) will be propagated through the higher
1337 // bits of the wide addition.
1338 // A SUBCARRY can generate borrow only if LHS < RHS and this property will be
1339 // preserved by sign extension.
1340 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1341 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1343 EVT ValueVTs[] = {LHS.getValueType(), N->getValueType(1)};
1345 // Do the arithmetic in the wide type.
1346 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N), DAG.getVTList(ValueVTs),
1347 LHS, RHS, N->getOperand(2));
1349 // Update the users of the original carry/borrow value.
1350 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1352 return SDValue(Res.getNode(), 0);
1355 SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO_CARRY(SDNode *N,
1356 unsigned ResNo) {
1357 assert(ResNo == 1 && "Don't know how to promote other results yet.");
1358 return PromoteIntRes_Overflow(N);
1361 SDValue DAGTypeLegalizer::PromoteIntRes_ABS(SDNode *N) {
1362 SDValue Op0 = SExtPromotedInteger(N->getOperand(0));
1363 return DAG.getNode(ISD::ABS, SDLoc(N), Op0.getValueType(), Op0);
1366 SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
1367 // Promote the overflow bit trivially.
1368 if (ResNo == 1)
1369 return PromoteIntRes_Overflow(N);
1371 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
1372 SDLoc DL(N);
1373 EVT SmallVT = LHS.getValueType();
1375 // To determine if the result overflowed in a larger type, we extend the
1376 // input to the larger type, do the multiply (checking if it overflows),
1377 // then also check the high bits of the result to see if overflow happened
1378 // there.
1379 if (N->getOpcode() == ISD::SMULO) {
1380 LHS = SExtPromotedInteger(LHS);
1381 RHS = SExtPromotedInteger(RHS);
1382 } else {
1383 LHS = ZExtPromotedInteger(LHS);
1384 RHS = ZExtPromotedInteger(RHS);
1386 SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1));
1387 SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS);
1389 // Overflow occurred if it occurred in the larger type, or if the high part
1390 // of the result does not zero/sign-extend the low part. Check this second
1391 // possibility first.
1392 SDValue Overflow;
1393 if (N->getOpcode() == ISD::UMULO) {
1394 // Unsigned overflow occurred if the high part is non-zero.
1395 unsigned Shift = SmallVT.getScalarSizeInBits();
1396 EVT ShiftTy = getShiftAmountTyForConstant(Mul.getValueType(), TLI, DAG);
1397 SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
1398 DAG.getConstant(Shift, DL, ShiftTy));
1399 Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
1400 DAG.getConstant(0, DL, Hi.getValueType()),
1401 ISD::SETNE);
1402 } else {
1403 // Signed overflow occurred if the high part does not sign extend the low.
1404 SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
1405 Mul, DAG.getValueType(SmallVT));
1406 Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
1409 // The only other way for overflow to occur is if the multiplication in the
1410 // larger type itself overflowed.
1411 Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow,
1412 SDValue(Mul.getNode(), 1));
1414 // Use the calculated overflow everywhere.
1415 ReplaceValueWith(SDValue(N, 1), Overflow);
1416 return Mul;
1419 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
1420 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
1421 N->getValueType(0)));
1424 SDValue DAGTypeLegalizer::PromoteIntRes_VSCALE(SDNode *N) {
1425 EVT VT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1427 APInt MulImm = cast<ConstantSDNode>(N->getOperand(0))->getAPIntValue();
1428 return DAG.getVScale(SDLoc(N), VT, MulImm.sextOrSelf(VT.getSizeInBits()));
1431 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
1432 SDValue Chain = N->getOperand(0); // Get the chain.
1433 SDValue Ptr = N->getOperand(1); // Get the pointer.
1434 EVT VT = N->getValueType(0);
1435 SDLoc dl(N);
1437 MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
1438 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
1439 // The argument is passed as NumRegs registers of type RegVT.
1441 SmallVector<SDValue, 8> Parts(NumRegs);
1442 for (unsigned i = 0; i < NumRegs; ++i) {
1443 Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
1444 N->getConstantOperandVal(3));
1445 Chain = Parts[i].getValue(1);
1448 // Handle endianness of the load.
1449 if (DAG.getDataLayout().isBigEndian())
1450 std::reverse(Parts.begin(), Parts.end());
1452 // Assemble the parts in the promoted type.
1453 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1454 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
1455 for (unsigned i = 1; i < NumRegs; ++i) {
1456 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
1457 // Shift it to the right position and "or" it in.
1458 Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
1459 DAG.getConstant(i * RegVT.getSizeInBits(), dl,
1460 TLI.getPointerTy(DAG.getDataLayout())));
1461 Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
1464 // Modified the chain result - switch anything that used the old chain to
1465 // use the new one.
1466 ReplaceValueWith(SDValue(N, 1), Chain);
1468 return Res;
1471 //===----------------------------------------------------------------------===//
1472 // Integer Operand Promotion
1473 //===----------------------------------------------------------------------===//
1475 /// PromoteIntegerOperand - This method is called when the specified operand of
1476 /// the specified node is found to need promotion. At this point, all of the
1477 /// result types of the node are known to be legal, but other operands of the
1478 /// node may need promotion or expansion as well as the specified one.
1479 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
1480 LLVM_DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG);
1481 dbgs() << "\n");
1482 SDValue Res = SDValue();
1483 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) {
1484 LLVM_DEBUG(dbgs() << "Node has been custom lowered, done\n");
1485 return false;
1488 switch (N->getOpcode()) {
1489 default:
1490 #ifndef NDEBUG
1491 dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
1492 N->dump(&DAG); dbgs() << "\n";
1493 #endif
1494 llvm_unreachable("Do not know how to promote this operator's operand!");
1496 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
1497 case ISD::ATOMIC_STORE:
1498 Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
1499 break;
1500 case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break;
1501 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
1502 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
1503 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
1504 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
1505 case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
1506 case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
1507 case ISD::INSERT_VECTOR_ELT:
1508 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
1509 case ISD::SCALAR_TO_VECTOR:
1510 Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break;
1511 case ISD::SPLAT_VECTOR:
1512 Res = PromoteIntOp_SPLAT_VECTOR(N); break;
1513 case ISD::VSELECT:
1514 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
1515 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
1516 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
1517 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
1518 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
1519 case ISD::STRICT_SINT_TO_FP: Res = PromoteIntOp_STRICT_SINT_TO_FP(N); break;
1520 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
1521 OpNo); break;
1522 case ISD::MSTORE: Res = PromoteIntOp_MSTORE(cast<MaskedStoreSDNode>(N),
1523 OpNo); break;
1524 case ISD::MLOAD: Res = PromoteIntOp_MLOAD(cast<MaskedLoadSDNode>(N),
1525 OpNo); break;
1526 case ISD::MGATHER: Res = PromoteIntOp_MGATHER(cast<MaskedGatherSDNode>(N),
1527 OpNo); break;
1528 case ISD::MSCATTER: Res = PromoteIntOp_MSCATTER(cast<MaskedScatterSDNode>(N),
1529 OpNo); break;
1530 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
1531 case ISD::FP16_TO_FP:
1532 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
1533 case ISD::STRICT_UINT_TO_FP: Res = PromoteIntOp_STRICT_UINT_TO_FP(N); break;
1534 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
1535 case ISD::EXTRACT_SUBVECTOR: Res = PromoteIntOp_EXTRACT_SUBVECTOR(N); break;
1537 case ISD::SHL:
1538 case ISD::SRA:
1539 case ISD::SRL:
1540 case ISD::ROTL:
1541 case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
1543 case ISD::SADDO_CARRY:
1544 case ISD::SSUBO_CARRY:
1545 case ISD::ADDCARRY:
1546 case ISD::SUBCARRY: Res = PromoteIntOp_ADDSUBCARRY(N, OpNo); break;
1548 case ISD::FRAMEADDR:
1549 case ISD::RETURNADDR: Res = PromoteIntOp_FRAMERETURNADDR(N); break;
1551 case ISD::PREFETCH: Res = PromoteIntOp_PREFETCH(N, OpNo); break;
1553 case ISD::SMULFIX:
1554 case ISD::SMULFIXSAT:
1555 case ISD::UMULFIX:
1556 case ISD::UMULFIXSAT:
1557 case ISD::SDIVFIX:
1558 case ISD::SDIVFIXSAT:
1559 case ISD::UDIVFIX:
1560 case ISD::UDIVFIXSAT: Res = PromoteIntOp_FIX(N); break;
1562 case ISD::FPOWI: Res = PromoteIntOp_FPOWI(N); break;
1564 case ISD::VECREDUCE_ADD:
1565 case ISD::VECREDUCE_MUL:
1566 case ISD::VECREDUCE_AND:
1567 case ISD::VECREDUCE_OR:
1568 case ISD::VECREDUCE_XOR:
1569 case ISD::VECREDUCE_SMAX:
1570 case ISD::VECREDUCE_SMIN:
1571 case ISD::VECREDUCE_UMAX:
1572 case ISD::VECREDUCE_UMIN: Res = PromoteIntOp_VECREDUCE(N); break;
1574 case ISD::SET_ROUNDING: Res = PromoteIntOp_SET_ROUNDING(N); break;
1577 // If the result is null, the sub-method took care of registering results etc.
1578 if (!Res.getNode()) return false;
1580 // If the result is N, the sub-method updated N in place. Tell the legalizer
1581 // core about this.
1582 if (Res.getNode() == N)
1583 return true;
1585 const bool IsStrictFp = N->isStrictFPOpcode();
1586 assert(Res.getValueType() == N->getValueType(0) &&
1587 N->getNumValues() == (IsStrictFp ? 2 : 1) &&
1588 "Invalid operand expansion");
1589 LLVM_DEBUG(dbgs() << "Replacing: "; N->dump(&DAG); dbgs() << " with: ";
1590 Res.dump());
1592 ReplaceValueWith(SDValue(N, 0), Res);
1593 if (IsStrictFp)
1594 ReplaceValueWith(SDValue(N, 1), SDValue(Res.getNode(), 1));
1596 return false;
1599 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
1600 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
1601 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
1602 ISD::CondCode CCCode) {
1603 // We have to insert explicit sign or zero extends. Note that we could
1604 // insert sign extends for ALL conditions. For those operations where either
1605 // zero or sign extension would be valid, use SExtOrZExtPromotedInteger
1606 // which will choose the cheapest for the target.
1607 switch (CCCode) {
1608 default: llvm_unreachable("Unknown integer comparison!");
1609 case ISD::SETEQ:
1610 case ISD::SETNE: {
1611 SDValue OpL = GetPromotedInteger(NewLHS);
1612 SDValue OpR = GetPromotedInteger(NewRHS);
1614 // We would prefer to promote the comparison operand with sign extension.
1615 // If the width of OpL/OpR excluding the duplicated sign bits is no greater
1616 // than the width of NewLHS/NewRH, we can avoid inserting real truncate
1617 // instruction, which is redundant eventually.
1618 unsigned OpLEffectiveBits =
1619 OpL.getScalarValueSizeInBits() - DAG.ComputeNumSignBits(OpL) + 1;
1620 unsigned OpREffectiveBits =
1621 OpR.getScalarValueSizeInBits() - DAG.ComputeNumSignBits(OpR) + 1;
1622 if (OpLEffectiveBits <= NewLHS.getScalarValueSizeInBits() &&
1623 OpREffectiveBits <= NewRHS.getScalarValueSizeInBits()) {
1624 NewLHS = OpL;
1625 NewRHS = OpR;
1626 } else {
1627 NewLHS = SExtOrZExtPromotedInteger(NewLHS);
1628 NewRHS = SExtOrZExtPromotedInteger(NewRHS);
1630 break;
1632 case ISD::SETUGE:
1633 case ISD::SETUGT:
1634 case ISD::SETULE:
1635 case ISD::SETULT:
1636 NewLHS = SExtOrZExtPromotedInteger(NewLHS);
1637 NewRHS = SExtOrZExtPromotedInteger(NewRHS);
1638 break;
1639 case ISD::SETGE:
1640 case ISD::SETGT:
1641 case ISD::SETLT:
1642 case ISD::SETLE:
1643 NewLHS = SExtPromotedInteger(NewLHS);
1644 NewRHS = SExtPromotedInteger(NewRHS);
1645 break;
1649 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
1650 SDValue Op = GetPromotedInteger(N->getOperand(0));
1651 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op);
1654 SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
1655 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
1656 return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(),
1657 N->getChain(), N->getBasePtr(), Op2, N->getMemOperand());
1660 SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
1661 // This should only occur in unusual situations like bitcasting to an
1662 // x86_fp80, so just turn it into a store+load
1663 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
1666 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
1667 assert(OpNo == 2 && "Don't know how to promote this operand!");
1669 SDValue LHS = N->getOperand(2);
1670 SDValue RHS = N->getOperand(3);
1671 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
1673 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
1674 // legal types.
1675 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1676 N->getOperand(1), LHS, RHS, N->getOperand(4)),
1680 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
1681 assert(OpNo == 1 && "only know how to promote condition");
1683 // Promote all the way up to the canonical SetCC type.
1684 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
1686 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
1687 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
1688 N->getOperand(2)), 0);
1691 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
1692 // Since the result type is legal, the operands must promote to it.
1693 EVT OVT = N->getOperand(0).getValueType();
1694 SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
1695 SDValue Hi = GetPromotedInteger(N->getOperand(1));
1696 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
1697 SDLoc dl(N);
1699 Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
1700 DAG.getConstant(OVT.getSizeInBits(), dl,
1701 TLI.getPointerTy(DAG.getDataLayout())));
1702 return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
1705 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
1706 // The vector type is legal but the element type is not. This implies
1707 // that the vector is a power-of-two in length and that the element
1708 // type does not have a strange size (eg: it is not i1).
1709 EVT VecVT = N->getValueType(0);
1710 unsigned NumElts = VecVT.getVectorNumElements();
1711 assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) &&
1712 "Legal vector of one illegal element?");
1714 // Promote the inserted value. The type does not need to match the
1715 // vector element type. Check that any extra bits introduced will be
1716 // truncated away.
1717 assert(N->getOperand(0).getValueSizeInBits() >=
1718 N->getValueType(0).getScalarSizeInBits() &&
1719 "Type of inserted value narrower than vector element type!");
1721 SmallVector<SDValue, 16> NewOps;
1722 for (unsigned i = 0; i < NumElts; ++i)
1723 NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
1725 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1728 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
1729 unsigned OpNo) {
1730 if (OpNo == 1) {
1731 // Promote the inserted value. This is valid because the type does not
1732 // have to match the vector element type.
1734 // Check that any extra bits introduced will be truncated away.
1735 assert(N->getOperand(1).getValueSizeInBits() >=
1736 N->getValueType(0).getScalarSizeInBits() &&
1737 "Type of inserted value narrower than vector element type!");
1738 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1739 GetPromotedInteger(N->getOperand(1)),
1740 N->getOperand(2)),
1744 assert(OpNo == 2 && "Different operand and result vector types?");
1746 // Promote the index.
1747 SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N),
1748 TLI.getVectorIdxTy(DAG.getDataLayout()));
1749 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1750 N->getOperand(1), Idx), 0);
1753 SDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) {
1754 // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote
1755 // the operand in place.
1756 return SDValue(DAG.UpdateNodeOperands(N,
1757 GetPromotedInteger(N->getOperand(0))), 0);
1760 SDValue DAGTypeLegalizer::PromoteIntOp_SPLAT_VECTOR(SDNode *N) {
1761 // Integer SPLAT_VECTOR operands are implicitly truncated, so just promote the
1762 // operand in place.
1763 return SDValue(
1764 DAG.UpdateNodeOperands(N, GetPromotedInteger(N->getOperand(0))), 0);
1767 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
1768 assert(OpNo == 0 && "Only know how to promote the condition!");
1769 SDValue Cond = N->getOperand(0);
1770 EVT OpTy = N->getOperand(1).getValueType();
1772 if (N->getOpcode() == ISD::VSELECT)
1773 if (SDValue Res = WidenVSELECTMask(N))
1774 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1775 Res, N->getOperand(1), N->getOperand(2));
1777 // Promote all the way up to the canonical SetCC type.
1778 EVT OpVT = N->getOpcode() == ISD::SELECT ? OpTy.getScalarType() : OpTy;
1779 Cond = PromoteTargetBoolean(Cond, OpVT);
1781 return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1),
1782 N->getOperand(2)), 0);
1785 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
1786 assert(OpNo == 0 && "Don't know how to promote this operand!");
1788 SDValue LHS = N->getOperand(0);
1789 SDValue RHS = N->getOperand(1);
1790 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
1792 // The CC (#4) and the possible return values (#2 and #3) have legal types.
1793 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
1794 N->getOperand(3), N->getOperand(4)), 0);
1797 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
1798 assert(OpNo == 0 && "Don't know how to promote this operand!");
1800 SDValue LHS = N->getOperand(0);
1801 SDValue RHS = N->getOperand(1);
1802 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
1804 // The CC (#2) is always legal.
1805 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
1808 SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
1809 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1810 ZExtPromotedInteger(N->getOperand(1))), 0);
1813 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
1814 SDValue Op = GetPromotedInteger(N->getOperand(0));
1815 SDLoc dl(N);
1816 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
1817 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
1818 Op, DAG.getValueType(N->getOperand(0).getValueType()));
1821 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
1822 return SDValue(DAG.UpdateNodeOperands(N,
1823 SExtPromotedInteger(N->getOperand(0))), 0);
1826 SDValue DAGTypeLegalizer::PromoteIntOp_STRICT_SINT_TO_FP(SDNode *N) {
1827 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1828 SExtPromotedInteger(N->getOperand(1))), 0);
1831 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
1832 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1833 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
1834 SDLoc dl(N);
1836 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
1838 // Truncate the value and store the result.
1839 return DAG.getTruncStore(Ch, dl, Val, Ptr,
1840 N->getMemoryVT(), N->getMemOperand());
1843 SDValue DAGTypeLegalizer::PromoteIntOp_MSTORE(MaskedStoreSDNode *N,
1844 unsigned OpNo) {
1845 SDValue DataOp = N->getValue();
1846 SDValue Mask = N->getMask();
1848 if (OpNo == 4) {
1849 // The Mask. Update in place.
1850 EVT DataVT = DataOp.getValueType();
1851 Mask = PromoteTargetBoolean(Mask, DataVT);
1852 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
1853 NewOps[4] = Mask;
1854 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1857 assert(OpNo == 1 && "Unexpected operand for promotion");
1858 DataOp = GetPromotedInteger(DataOp);
1860 return DAG.getMaskedStore(N->getChain(), SDLoc(N), DataOp, N->getBasePtr(),
1861 N->getOffset(), Mask, N->getMemoryVT(),
1862 N->getMemOperand(), N->getAddressingMode(),
1863 /*IsTruncating*/ true, N->isCompressingStore());
1866 SDValue DAGTypeLegalizer::PromoteIntOp_MLOAD(MaskedLoadSDNode *N,
1867 unsigned OpNo) {
1868 assert(OpNo == 3 && "Only know how to promote the mask!");
1869 EVT DataVT = N->getValueType(0);
1870 SDValue Mask = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1871 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
1872 NewOps[OpNo] = Mask;
1873 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
1874 if (Res == N)
1875 return SDValue(Res, 0);
1877 // Update triggered CSE, do our own replacement since caller can't.
1878 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
1879 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
1880 return SDValue();
1883 SDValue DAGTypeLegalizer::PromoteIntOp_MGATHER(MaskedGatherSDNode *N,
1884 unsigned OpNo) {
1886 SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
1887 if (OpNo == 2) {
1888 // The Mask
1889 EVT DataVT = N->getValueType(0);
1890 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1891 } else if (OpNo == 4) {
1892 // The Index
1893 if (N->isIndexSigned())
1894 // Need to sign extend the index since the bits will likely be used.
1895 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
1896 else
1897 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
1898 } else
1899 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
1901 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
1902 if (Res == N)
1903 return SDValue(Res, 0);
1905 // Update triggered CSE, do our own replacement since caller can't.
1906 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
1907 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
1908 return SDValue();
1911 SDValue DAGTypeLegalizer::PromoteIntOp_MSCATTER(MaskedScatterSDNode *N,
1912 unsigned OpNo) {
1913 bool TruncateStore = N->isTruncatingStore();
1914 SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
1915 if (OpNo == 2) {
1916 // The Mask
1917 EVT DataVT = N->getValue().getValueType();
1918 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1919 } else if (OpNo == 4) {
1920 // The Index
1921 if (N->isIndexSigned())
1922 // Need to sign extend the index since the bits will likely be used.
1923 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
1924 else
1925 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
1927 N->setIndexType(TLI.getCanonicalIndexType(N->getIndexType(),
1928 N->getMemoryVT(), NewOps[OpNo]));
1929 } else {
1930 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
1931 TruncateStore = true;
1934 return DAG.getMaskedScatter(DAG.getVTList(MVT::Other), N->getMemoryVT(),
1935 SDLoc(N), NewOps, N->getMemOperand(),
1936 N->getIndexType(), TruncateStore);
1939 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
1940 SDValue Op = GetPromotedInteger(N->getOperand(0));
1941 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op);
1944 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
1945 return SDValue(DAG.UpdateNodeOperands(N,
1946 ZExtPromotedInteger(N->getOperand(0))), 0);
1949 SDValue DAGTypeLegalizer::PromoteIntOp_STRICT_UINT_TO_FP(SDNode *N) {
1950 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1951 ZExtPromotedInteger(N->getOperand(1))), 0);
1954 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
1955 SDLoc dl(N);
1956 SDValue Op = GetPromotedInteger(N->getOperand(0));
1957 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
1958 return DAG.getZeroExtendInReg(Op, dl, N->getOperand(0).getValueType());
1961 SDValue DAGTypeLegalizer::PromoteIntOp_ADDSUBCARRY(SDNode *N, unsigned OpNo) {
1962 assert(OpNo == 2 && "Don't know how to promote this operand!");
1964 SDValue LHS = N->getOperand(0);
1965 SDValue RHS = N->getOperand(1);
1966 SDValue Carry = N->getOperand(2);
1967 SDLoc DL(N);
1969 Carry = PromoteTargetBoolean(Carry, LHS.getValueType());
1971 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, Carry), 0);
1974 SDValue DAGTypeLegalizer::PromoteIntOp_FIX(SDNode *N) {
1975 SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
1976 return SDValue(
1977 DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1), Op2), 0);
1980 SDValue DAGTypeLegalizer::PromoteIntOp_FRAMERETURNADDR(SDNode *N) {
1981 // Promote the RETURNADDR/FRAMEADDR argument to a supported integer width.
1982 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
1983 return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
1986 SDValue DAGTypeLegalizer::PromoteIntOp_PREFETCH(SDNode *N, unsigned OpNo) {
1987 assert(OpNo > 1 && "Don't know how to promote this operand!");
1988 // Promote the rw, locality, and cache type arguments to a supported integer
1989 // width.
1990 SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
1991 SDValue Op3 = ZExtPromotedInteger(N->getOperand(3));
1992 SDValue Op4 = ZExtPromotedInteger(N->getOperand(4));
1993 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1),
1994 Op2, Op3, Op4),
1998 SDValue DAGTypeLegalizer::PromoteIntOp_FPOWI(SDNode *N) {
1999 // FIXME: Support for promotion of STRICT_FPOWI is not implemented yet.
2000 assert(N->getOpcode() == ISD::FPOWI && "No STRICT_FPOWI support here yet.");
2002 // The integer operand is the last operand in FPOWI (so the result and
2003 // floating point operand is already type legalized).
2005 // We can't just promote the exponent type in FPOWI, since we want to lower
2006 // the node to a libcall and we if we promote to a type larger than
2007 // sizeof(int) the libcall might not be according to the targets ABI. Instead
2008 // we rewrite to a libcall here directly, letting makeLibCall handle promotion
2009 // if the target accepts it according to shouldSignExtendTypeInLibCall.
2010 RTLIB::Libcall LC = RTLIB::getPOWI(N->getValueType(0));
2011 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi.");
2012 if (!TLI.getLibcallName(LC)) {
2013 // Some targets don't have a powi libcall; use pow instead.
2014 // FIXME: Implement this if some target needs it.
2015 DAG.getContext()->emitError("Don't know how to promote fpowi to fpow");
2016 return DAG.getUNDEF(N->getValueType(0));
2018 // The exponent should fit in a sizeof(int) type for the libcall to be valid.
2019 assert(DAG.getLibInfo().getIntSize() ==
2020 N->getOperand(1).getValueType().getSizeInBits() &&
2021 "POWI exponent should match with sizeof(int) when doing the libcall.");
2022 TargetLowering::MakeLibCallOptions CallOptions;
2023 CallOptions.setSExt(true);
2024 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2025 std::pair<SDValue, SDValue> Tmp =
2026 TLI.makeLibCall(DAG, LC, N->getValueType(0), Ops,
2027 CallOptions, SDLoc(N), SDValue());
2028 ReplaceValueWith(SDValue(N, 0), Tmp.first);
2029 return SDValue();
2032 SDValue DAGTypeLegalizer::PromoteIntOp_VECREDUCE(SDNode *N) {
2033 SDLoc dl(N);
2034 SDValue Op;
2035 switch (N->getOpcode()) {
2036 default: llvm_unreachable("Expected integer vector reduction");
2037 case ISD::VECREDUCE_ADD:
2038 case ISD::VECREDUCE_MUL:
2039 case ISD::VECREDUCE_AND:
2040 case ISD::VECREDUCE_OR:
2041 case ISD::VECREDUCE_XOR:
2042 Op = GetPromotedInteger(N->getOperand(0));
2043 break;
2044 case ISD::VECREDUCE_SMAX:
2045 case ISD::VECREDUCE_SMIN:
2046 Op = SExtPromotedInteger(N->getOperand(0));
2047 break;
2048 case ISD::VECREDUCE_UMAX:
2049 case ISD::VECREDUCE_UMIN:
2050 Op = ZExtPromotedInteger(N->getOperand(0));
2051 break;
2054 EVT EltVT = Op.getValueType().getVectorElementType();
2055 EVT VT = N->getValueType(0);
2056 if (VT.bitsGE(EltVT))
2057 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, Op);
2059 // Result size must be >= element size. If this is not the case after
2060 // promotion, also promote the result type and then truncate.
2061 SDValue Reduce = DAG.getNode(N->getOpcode(), dl, EltVT, Op);
2062 return DAG.getNode(ISD::TRUNCATE, dl, VT, Reduce);
2065 SDValue DAGTypeLegalizer::PromoteIntOp_SET_ROUNDING(SDNode *N) {
2066 SDValue Op = ZExtPromotedInteger(N->getOperand(1));
2067 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op), 0);
2070 //===----------------------------------------------------------------------===//
2071 // Integer Result Expansion
2072 //===----------------------------------------------------------------------===//
2074 /// ExpandIntegerResult - This method is called when the specified result of the
2075 /// specified node is found to need expansion. At this point, the node may also
2076 /// have invalid operands or may have other results that need promotion, we just
2077 /// know that (at least) one result needs expansion.
2078 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
2079 LLVM_DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG);
2080 dbgs() << "\n");
2081 SDValue Lo, Hi;
2082 Lo = Hi = SDValue();
2084 // See if the target wants to custom expand this node.
2085 if (CustomLowerNode(N, N->getValueType(ResNo), true))
2086 return;
2088 switch (N->getOpcode()) {
2089 default:
2090 #ifndef NDEBUG
2091 dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
2092 N->dump(&DAG); dbgs() << "\n";
2093 #endif
2094 report_fatal_error("Do not know how to expand the result of this "
2095 "operator!");
2097 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
2098 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
2099 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
2100 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
2101 case ISD::FREEZE: SplitRes_FREEZE(N, Lo, Hi); break;
2103 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
2104 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
2105 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
2106 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
2107 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
2109 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
2110 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
2111 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
2112 case ISD::BITREVERSE: ExpandIntRes_BITREVERSE(N, Lo, Hi); break;
2113 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
2114 case ISD::PARITY: ExpandIntRes_PARITY(N, Lo, Hi); break;
2115 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
2116 case ISD::ABS: ExpandIntRes_ABS(N, Lo, Hi); break;
2117 case ISD::CTLZ_ZERO_UNDEF:
2118 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
2119 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
2120 case ISD::CTTZ_ZERO_UNDEF:
2121 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
2122 case ISD::FLT_ROUNDS_: ExpandIntRes_FLT_ROUNDS(N, Lo, Hi); break;
2123 case ISD::STRICT_FP_TO_SINT:
2124 case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
2125 case ISD::STRICT_FP_TO_UINT:
2126 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
2127 case ISD::FP_TO_SINT_SAT:
2128 case ISD::FP_TO_UINT_SAT: ExpandIntRes_FP_TO_XINT_SAT(N, Lo, Hi); break;
2129 case ISD::STRICT_LLROUND:
2130 case ISD::STRICT_LLRINT:
2131 case ISD::LLROUND:
2132 case ISD::LLRINT: ExpandIntRes_LLROUND_LLRINT(N, Lo, Hi); break;
2133 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
2134 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
2135 case ISD::READCYCLECOUNTER: ExpandIntRes_READCYCLECOUNTER(N, Lo, Hi); break;
2136 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
2137 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
2138 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
2139 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
2140 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
2141 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
2142 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
2143 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
2144 case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
2146 case ISD::ATOMIC_LOAD_ADD:
2147 case ISD::ATOMIC_LOAD_SUB:
2148 case ISD::ATOMIC_LOAD_AND:
2149 case ISD::ATOMIC_LOAD_CLR:
2150 case ISD::ATOMIC_LOAD_OR:
2151 case ISD::ATOMIC_LOAD_XOR:
2152 case ISD::ATOMIC_LOAD_NAND:
2153 case ISD::ATOMIC_LOAD_MIN:
2154 case ISD::ATOMIC_LOAD_MAX:
2155 case ISD::ATOMIC_LOAD_UMIN:
2156 case ISD::ATOMIC_LOAD_UMAX:
2157 case ISD::ATOMIC_SWAP:
2158 case ISD::ATOMIC_CMP_SWAP: {
2159 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
2160 SplitInteger(Tmp.first, Lo, Hi);
2161 ReplaceValueWith(SDValue(N, 1), Tmp.second);
2162 break;
2164 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
2165 AtomicSDNode *AN = cast<AtomicSDNode>(N);
2166 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other);
2167 SDValue Tmp = DAG.getAtomicCmpSwap(
2168 ISD::ATOMIC_CMP_SWAP, SDLoc(N), AN->getMemoryVT(), VTs,
2169 N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3),
2170 AN->getMemOperand());
2172 // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
2173 // success simply by comparing the loaded value against the ingoing
2174 // comparison.
2175 SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp,
2176 N->getOperand(2), ISD::SETEQ);
2178 SplitInteger(Tmp, Lo, Hi);
2179 ReplaceValueWith(SDValue(N, 1), Success);
2180 ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1));
2181 break;
2184 case ISD::AND:
2185 case ISD::OR:
2186 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
2188 case ISD::UMAX:
2189 case ISD::SMAX:
2190 case ISD::UMIN:
2191 case ISD::SMIN: ExpandIntRes_MINMAX(N, Lo, Hi); break;
2193 case ISD::ADD:
2194 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
2196 case ISD::ADDC:
2197 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
2199 case ISD::ADDE:
2200 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
2202 case ISD::ADDCARRY:
2203 case ISD::SUBCARRY: ExpandIntRes_ADDSUBCARRY(N, Lo, Hi); break;
2205 case ISD::SADDO_CARRY:
2206 case ISD::SSUBO_CARRY: ExpandIntRes_SADDSUBO_CARRY(N, Lo, Hi); break;
2208 case ISD::SHL:
2209 case ISD::SRA:
2210 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
2212 case ISD::SADDO:
2213 case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
2214 case ISD::UADDO:
2215 case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
2216 case ISD::UMULO:
2217 case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
2219 case ISD::SADDSAT:
2220 case ISD::UADDSAT:
2221 case ISD::SSUBSAT:
2222 case ISD::USUBSAT: ExpandIntRes_ADDSUBSAT(N, Lo, Hi); break;
2224 case ISD::SSHLSAT:
2225 case ISD::USHLSAT: ExpandIntRes_SHLSAT(N, Lo, Hi); break;
2227 case ISD::SMULFIX:
2228 case ISD::SMULFIXSAT:
2229 case ISD::UMULFIX:
2230 case ISD::UMULFIXSAT: ExpandIntRes_MULFIX(N, Lo, Hi); break;
2232 case ISD::SDIVFIX:
2233 case ISD::SDIVFIXSAT:
2234 case ISD::UDIVFIX:
2235 case ISD::UDIVFIXSAT: ExpandIntRes_DIVFIX(N, Lo, Hi); break;
2237 case ISD::VECREDUCE_ADD:
2238 case ISD::VECREDUCE_MUL:
2239 case ISD::VECREDUCE_AND:
2240 case ISD::VECREDUCE_OR:
2241 case ISD::VECREDUCE_XOR:
2242 case ISD::VECREDUCE_SMAX:
2243 case ISD::VECREDUCE_SMIN:
2244 case ISD::VECREDUCE_UMAX:
2245 case ISD::VECREDUCE_UMIN: ExpandIntRes_VECREDUCE(N, Lo, Hi); break;
2247 case ISD::ROTL:
2248 case ISD::ROTR:
2249 ExpandIntRes_Rotate(N, Lo, Hi);
2250 break;
2252 case ISD::FSHL:
2253 case ISD::FSHR:
2254 ExpandIntRes_FunnelShift(N, Lo, Hi);
2255 break;
2257 case ISD::VSCALE:
2258 ExpandIntRes_VSCALE(N, Lo, Hi);
2259 break;
2262 // If Lo/Hi is null, the sub-method took care of registering results etc.
2263 if (Lo.getNode())
2264 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
2267 /// Lower an atomic node to the appropriate builtin call.
2268 std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
2269 unsigned Opc = Node->getOpcode();
2270 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2271 AtomicOrdering order = cast<AtomicSDNode>(Node)->getMergedOrdering();
2272 // Lower to outline atomic libcall if outline atomics enabled,
2273 // or to sync libcall otherwise
2274 RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, order, VT);
2275 EVT RetVT = Node->getValueType(0);
2276 TargetLowering::MakeLibCallOptions CallOptions;
2277 SmallVector<SDValue, 4> Ops;
2278 if (TLI.getLibcallName(LC)) {
2279 Ops.append(Node->op_begin() + 2, Node->op_end());
2280 Ops.push_back(Node->getOperand(1));
2281 } else {
2282 LC = RTLIB::getSYNC(Opc, VT);
2283 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2284 "Unexpected atomic op or value type!");
2285 Ops.append(Node->op_begin() + 1, Node->op_end());
2287 return TLI.makeLibCall(DAG, LC, RetVT, Ops, CallOptions, SDLoc(Node),
2288 Node->getOperand(0));
2291 /// N is a shift by a value that needs to be expanded,
2292 /// and the shift amount is a constant 'Amt'. Expand the operation.
2293 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, const APInt &Amt,
2294 SDValue &Lo, SDValue &Hi) {
2295 SDLoc DL(N);
2296 // Expand the incoming operand to be shifted, so that we have its parts
2297 SDValue InL, InH;
2298 GetExpandedInteger(N->getOperand(0), InL, InH);
2300 // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
2301 // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
2302 if (!Amt) {
2303 Lo = InL;
2304 Hi = InH;
2305 return;
2308 EVT NVT = InL.getValueType();
2309 unsigned VTBits = N->getValueType(0).getSizeInBits();
2310 unsigned NVTBits = NVT.getSizeInBits();
2311 EVT ShTy = N->getOperand(1).getValueType();
2313 if (N->getOpcode() == ISD::SHL) {
2314 if (Amt.ugt(VTBits)) {
2315 Lo = Hi = DAG.getConstant(0, DL, NVT);
2316 } else if (Amt.ugt(NVTBits)) {
2317 Lo = DAG.getConstant(0, DL, NVT);
2318 Hi = DAG.getNode(ISD::SHL, DL,
2319 NVT, InL, DAG.getConstant(Amt - NVTBits, DL, ShTy));
2320 } else if (Amt == NVTBits) {
2321 Lo = DAG.getConstant(0, DL, NVT);
2322 Hi = InL;
2323 } else {
2324 Lo = DAG.getNode(ISD::SHL, DL, NVT, InL, DAG.getConstant(Amt, DL, ShTy));
2325 Hi = DAG.getNode(ISD::OR, DL, NVT,
2326 DAG.getNode(ISD::SHL, DL, NVT, InH,
2327 DAG.getConstant(Amt, DL, ShTy)),
2328 DAG.getNode(ISD::SRL, DL, NVT, InL,
2329 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
2331 return;
2334 if (N->getOpcode() == ISD::SRL) {
2335 if (Amt.ugt(VTBits)) {
2336 Lo = Hi = DAG.getConstant(0, DL, NVT);
2337 } else if (Amt.ugt(NVTBits)) {
2338 Lo = DAG.getNode(ISD::SRL, DL,
2339 NVT, InH, DAG.getConstant(Amt - NVTBits, DL, ShTy));
2340 Hi = DAG.getConstant(0, DL, NVT);
2341 } else if (Amt == NVTBits) {
2342 Lo = InH;
2343 Hi = DAG.getConstant(0, DL, NVT);
2344 } else {
2345 Lo = DAG.getNode(ISD::OR, DL, NVT,
2346 DAG.getNode(ISD::SRL, DL, NVT, InL,
2347 DAG.getConstant(Amt, DL, ShTy)),
2348 DAG.getNode(ISD::SHL, DL, NVT, InH,
2349 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
2350 Hi = DAG.getNode(ISD::SRL, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
2352 return;
2355 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
2356 if (Amt.ugt(VTBits)) {
2357 Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
2358 DAG.getConstant(NVTBits - 1, DL, ShTy));
2359 } else if (Amt.ugt(NVTBits)) {
2360 Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
2361 DAG.getConstant(Amt - NVTBits, DL, ShTy));
2362 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
2363 DAG.getConstant(NVTBits - 1, DL, ShTy));
2364 } else if (Amt == NVTBits) {
2365 Lo = InH;
2366 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
2367 DAG.getConstant(NVTBits - 1, DL, ShTy));
2368 } else {
2369 Lo = DAG.getNode(ISD::OR, DL, NVT,
2370 DAG.getNode(ISD::SRL, DL, NVT, InL,
2371 DAG.getConstant(Amt, DL, ShTy)),
2372 DAG.getNode(ISD::SHL, DL, NVT, InH,
2373 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
2374 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
2378 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
2379 /// this shift based on knowledge of the high bit of the shift amount. If we
2380 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
2381 /// shift amount.
2382 bool DAGTypeLegalizer::
2383 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
2384 SDValue Amt = N->getOperand(1);
2385 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2386 EVT ShTy = Amt.getValueType();
2387 unsigned ShBits = ShTy.getScalarSizeInBits();
2388 unsigned NVTBits = NVT.getScalarSizeInBits();
2389 assert(isPowerOf2_32(NVTBits) &&
2390 "Expanded integer type size not a power of two!");
2391 SDLoc dl(N);
2393 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
2394 KnownBits Known = DAG.computeKnownBits(N->getOperand(1));
2396 // If we don't know anything about the high bits, exit.
2397 if (((Known.Zero|Known.One) & HighBitMask) == 0)
2398 return false;
2400 // Get the incoming operand to be shifted.
2401 SDValue InL, InH;
2402 GetExpandedInteger(N->getOperand(0), InL, InH);
2404 // If we know that any of the high bits of the shift amount are one, then we
2405 // can do this as a couple of simple shifts.
2406 if (Known.One.intersects(HighBitMask)) {
2407 // Mask out the high bit, which we know is set.
2408 Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
2409 DAG.getConstant(~HighBitMask, dl, ShTy));
2411 switch (N->getOpcode()) {
2412 default: llvm_unreachable("Unknown shift");
2413 case ISD::SHL:
2414 Lo = DAG.getConstant(0, dl, NVT); // Low part is zero.
2415 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
2416 return true;
2417 case ISD::SRL:
2418 Hi = DAG.getConstant(0, dl, NVT); // Hi part is zero.
2419 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
2420 return true;
2421 case ISD::SRA:
2422 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
2423 DAG.getConstant(NVTBits - 1, dl, ShTy));
2424 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
2425 return true;
2429 // If we know that all of the high bits of the shift amount are zero, then we
2430 // can do this as a couple of simple shifts.
2431 if (HighBitMask.isSubsetOf(Known.Zero)) {
2432 // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
2433 // shift if x is zero. We can use XOR here because x is known to be smaller
2434 // than 32.
2435 SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
2436 DAG.getConstant(NVTBits - 1, dl, ShTy));
2438 unsigned Op1, Op2;
2439 switch (N->getOpcode()) {
2440 default: llvm_unreachable("Unknown shift");
2441 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
2442 case ISD::SRL:
2443 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
2446 // When shifting right the arithmetic for Lo and Hi is swapped.
2447 if (N->getOpcode() != ISD::SHL)
2448 std::swap(InL, InH);
2450 // Use a little trick to get the bits that move from Lo to Hi. First
2451 // shift by one bit.
2452 SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, dl, ShTy));
2453 // Then compute the remaining shift with amount-1.
2454 SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
2456 Lo = DAG.getNode(N->getOpcode(), dl, NVT, InL, Amt);
2457 Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
2459 if (N->getOpcode() != ISD::SHL)
2460 std::swap(Hi, Lo);
2461 return true;
2464 return false;
2467 /// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
2468 /// of any size.
2469 bool DAGTypeLegalizer::
2470 ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
2471 SDValue Amt = N->getOperand(1);
2472 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2473 EVT ShTy = Amt.getValueType();
2474 unsigned NVTBits = NVT.getSizeInBits();
2475 assert(isPowerOf2_32(NVTBits) &&
2476 "Expanded integer type size not a power of two!");
2477 SDLoc dl(N);
2479 // Get the incoming operand to be shifted.
2480 SDValue InL, InH;
2481 GetExpandedInteger(N->getOperand(0), InL, InH);
2483 SDValue NVBitsNode = DAG.getConstant(NVTBits, dl, ShTy);
2484 SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
2485 SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
2486 SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
2487 Amt, NVBitsNode, ISD::SETULT);
2488 SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(ShTy),
2489 Amt, DAG.getConstant(0, dl, ShTy),
2490 ISD::SETEQ);
2492 SDValue LoS, HiS, LoL, HiL;
2493 switch (N->getOpcode()) {
2494 default: llvm_unreachable("Unknown shift");
2495 case ISD::SHL:
2496 // Short: ShAmt < NVTBits
2497 LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
2498 HiS = DAG.getNode(ISD::OR, dl, NVT,
2499 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
2500 DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
2502 // Long: ShAmt >= NVTBits
2503 LoL = DAG.getConstant(0, dl, NVT); // Lo part is zero.
2504 HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
2506 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
2507 Hi = DAG.getSelect(dl, NVT, isZero, InH,
2508 DAG.getSelect(dl, NVT, isShort, HiS, HiL));
2509 return true;
2510 case ISD::SRL:
2511 // Short: ShAmt < NVTBits
2512 HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
2513 LoS = DAG.getNode(ISD::OR, dl, NVT,
2514 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
2515 // FIXME: If Amt is zero, the following shift generates an undefined result
2516 // on some architectures.
2517 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
2519 // Long: ShAmt >= NVTBits
2520 HiL = DAG.getConstant(0, dl, NVT); // Hi part is zero.
2521 LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
2523 Lo = DAG.getSelect(dl, NVT, isZero, InL,
2524 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
2525 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
2526 return true;
2527 case ISD::SRA:
2528 // Short: ShAmt < NVTBits
2529 HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
2530 LoS = DAG.getNode(ISD::OR, dl, NVT,
2531 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
2532 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
2534 // Long: ShAmt >= NVTBits
2535 HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
2536 DAG.getConstant(NVTBits - 1, dl, ShTy));
2537 LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
2539 Lo = DAG.getSelect(dl, NVT, isZero, InL,
2540 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
2541 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
2542 return true;
2546 static std::pair<ISD::CondCode, ISD::NodeType> getExpandedMinMaxOps(int Op) {
2548 switch (Op) {
2549 default: llvm_unreachable("invalid min/max opcode");
2550 case ISD::SMAX:
2551 return std::make_pair(ISD::SETGT, ISD::UMAX);
2552 case ISD::UMAX:
2553 return std::make_pair(ISD::SETUGT, ISD::UMAX);
2554 case ISD::SMIN:
2555 return std::make_pair(ISD::SETLT, ISD::UMIN);
2556 case ISD::UMIN:
2557 return std::make_pair(ISD::SETULT, ISD::UMIN);
2561 void DAGTypeLegalizer::ExpandIntRes_MINMAX(SDNode *N,
2562 SDValue &Lo, SDValue &Hi) {
2563 SDLoc DL(N);
2564 ISD::NodeType LoOpc;
2565 ISD::CondCode CondC;
2566 std::tie(CondC, LoOpc) = getExpandedMinMaxOps(N->getOpcode());
2568 // Expand the subcomponents.
2569 SDValue LHSL, LHSH, RHSL, RHSH;
2570 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2571 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2573 // Value types
2574 EVT NVT = LHSL.getValueType();
2575 EVT CCT = getSetCCResultType(NVT);
2577 // Hi part is always the same op
2578 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
2580 // We need to know whether to select Lo part that corresponds to 'winning'
2581 // Hi part or if Hi parts are equal.
2582 SDValue IsHiLeft = DAG.getSetCC(DL, CCT, LHSH, RHSH, CondC);
2583 SDValue IsHiEq = DAG.getSetCC(DL, CCT, LHSH, RHSH, ISD::SETEQ);
2585 // Lo part corresponding to the 'winning' Hi part
2586 SDValue LoCmp = DAG.getSelect(DL, NVT, IsHiLeft, LHSL, RHSL);
2588 // Recursed Lo part if Hi parts are equal, this uses unsigned version
2589 SDValue LoMinMax = DAG.getNode(LoOpc, DL, NVT, {LHSL, RHSL});
2591 Lo = DAG.getSelect(DL, NVT, IsHiEq, LoMinMax, LoCmp);
2594 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
2595 SDValue &Lo, SDValue &Hi) {
2596 SDLoc dl(N);
2597 // Expand the subcomponents.
2598 SDValue LHSL, LHSH, RHSL, RHSH;
2599 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2600 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2602 EVT NVT = LHSL.getValueType();
2603 SDValue LoOps[2] = { LHSL, RHSL };
2604 SDValue HiOps[3] = { LHSH, RHSH };
2606 bool HasOpCarry = TLI.isOperationLegalOrCustom(
2607 N->getOpcode() == ISD::ADD ? ISD::ADDCARRY : ISD::SUBCARRY,
2608 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2609 if (HasOpCarry) {
2610 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
2611 if (N->getOpcode() == ISD::ADD) {
2612 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
2613 HiOps[2] = Lo.getValue(1);
2614 Hi = DAG.getNode(ISD::ADDCARRY, dl, VTList, HiOps);
2615 } else {
2616 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
2617 HiOps[2] = Lo.getValue(1);
2618 Hi = DAG.getNode(ISD::SUBCARRY, dl, VTList, HiOps);
2620 return;
2623 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
2624 // them. TODO: Teach operation legalization how to expand unsupported
2625 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
2626 // a carry of type MVT::Glue, but there doesn't seem to be any way to
2627 // generate a value of this type in the expanded code sequence.
2628 bool hasCarry =
2629 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
2630 ISD::ADDC : ISD::SUBC,
2631 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2633 if (hasCarry) {
2634 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
2635 if (N->getOpcode() == ISD::ADD) {
2636 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
2637 HiOps[2] = Lo.getValue(1);
2638 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
2639 } else {
2640 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
2641 HiOps[2] = Lo.getValue(1);
2642 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
2644 return;
2647 bool hasOVF =
2648 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
2649 ISD::UADDO : ISD::USUBO,
2650 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2651 TargetLoweringBase::BooleanContent BoolType = TLI.getBooleanContents(NVT);
2653 if (hasOVF) {
2654 EVT OvfVT = getSetCCResultType(NVT);
2655 SDVTList VTList = DAG.getVTList(NVT, OvfVT);
2656 int RevOpc;
2657 if (N->getOpcode() == ISD::ADD) {
2658 RevOpc = ISD::SUB;
2659 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
2660 Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
2661 } else {
2662 RevOpc = ISD::ADD;
2663 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
2664 Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
2666 SDValue OVF = Lo.getValue(1);
2668 switch (BoolType) {
2669 case TargetLoweringBase::UndefinedBooleanContent:
2670 OVF = DAG.getNode(ISD::AND, dl, OvfVT, DAG.getConstant(1, dl, OvfVT), OVF);
2671 LLVM_FALLTHROUGH;
2672 case TargetLoweringBase::ZeroOrOneBooleanContent:
2673 OVF = DAG.getZExtOrTrunc(OVF, dl, NVT);
2674 Hi = DAG.getNode(N->getOpcode(), dl, NVT, Hi, OVF);
2675 break;
2676 case TargetLoweringBase::ZeroOrNegativeOneBooleanContent:
2677 OVF = DAG.getSExtOrTrunc(OVF, dl, NVT);
2678 Hi = DAG.getNode(RevOpc, dl, NVT, Hi, OVF);
2680 return;
2683 if (N->getOpcode() == ISD::ADD) {
2684 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps);
2685 Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
2686 SDValue Cmp1 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
2687 ISD::SETULT);
2689 if (BoolType == TargetLoweringBase::ZeroOrOneBooleanContent) {
2690 SDValue Carry = DAG.getZExtOrTrunc(Cmp1, dl, NVT);
2691 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry);
2692 return;
2695 SDValue Carry1 = DAG.getSelect(dl, NVT, Cmp1,
2696 DAG.getConstant(1, dl, NVT),
2697 DAG.getConstant(0, dl, NVT));
2698 SDValue Cmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[1],
2699 ISD::SETULT);
2700 SDValue Carry2 = DAG.getSelect(dl, NVT, Cmp2,
2701 DAG.getConstant(1, dl, NVT), Carry1);
2702 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
2703 } else {
2704 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps);
2705 Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
2706 SDValue Cmp =
2707 DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
2708 LoOps[0], LoOps[1], ISD::SETULT);
2710 SDValue Borrow;
2711 if (BoolType == TargetLoweringBase::ZeroOrOneBooleanContent)
2712 Borrow = DAG.getZExtOrTrunc(Cmp, dl, NVT);
2713 else
2714 Borrow = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
2715 DAG.getConstant(0, dl, NVT));
2717 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
2721 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
2722 SDValue &Lo, SDValue &Hi) {
2723 // Expand the subcomponents.
2724 SDValue LHSL, LHSH, RHSL, RHSH;
2725 SDLoc dl(N);
2726 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2727 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2728 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
2729 SDValue LoOps[2] = { LHSL, RHSL };
2730 SDValue HiOps[3] = { LHSH, RHSH };
2732 if (N->getOpcode() == ISD::ADDC) {
2733 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
2734 HiOps[2] = Lo.getValue(1);
2735 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
2736 } else {
2737 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
2738 HiOps[2] = Lo.getValue(1);
2739 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
2742 // Legalized the flag result - switch anything that used the old flag to
2743 // use the new one.
2744 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2747 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
2748 SDValue &Lo, SDValue &Hi) {
2749 // Expand the subcomponents.
2750 SDValue LHSL, LHSH, RHSL, RHSH;
2751 SDLoc dl(N);
2752 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2753 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2754 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
2755 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
2756 SDValue HiOps[3] = { LHSH, RHSH };
2758 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
2759 HiOps[2] = Lo.getValue(1);
2760 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
2762 // Legalized the flag result - switch anything that used the old flag to
2763 // use the new one.
2764 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2767 void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
2768 SDValue &Lo, SDValue &Hi) {
2769 SDValue LHS = N->getOperand(0);
2770 SDValue RHS = N->getOperand(1);
2771 SDLoc dl(N);
2773 SDValue Ovf;
2775 unsigned CarryOp, NoCarryOp;
2776 ISD::CondCode Cond;
2777 switch(N->getOpcode()) {
2778 case ISD::UADDO:
2779 CarryOp = ISD::ADDCARRY;
2780 NoCarryOp = ISD::ADD;
2781 Cond = ISD::SETULT;
2782 break;
2783 case ISD::USUBO:
2784 CarryOp = ISD::SUBCARRY;
2785 NoCarryOp = ISD::SUB;
2786 Cond = ISD::SETUGT;
2787 break;
2788 default:
2789 llvm_unreachable("Node has unexpected Opcode");
2792 bool HasCarryOp = TLI.isOperationLegalOrCustom(
2793 CarryOp, TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
2795 if (HasCarryOp) {
2796 // Expand the subcomponents.
2797 SDValue LHSL, LHSH, RHSL, RHSH;
2798 GetExpandedInteger(LHS, LHSL, LHSH);
2799 GetExpandedInteger(RHS, RHSL, RHSH);
2800 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
2801 SDValue LoOps[2] = { LHSL, RHSL };
2802 SDValue HiOps[3] = { LHSH, RHSH };
2804 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
2805 HiOps[2] = Lo.getValue(1);
2806 Hi = DAG.getNode(CarryOp, dl, VTList, HiOps);
2808 Ovf = Hi.getValue(1);
2809 } else {
2810 // Expand the result by simply replacing it with the equivalent
2811 // non-overflow-checking operation.
2812 SDValue Sum = DAG.getNode(NoCarryOp, dl, LHS.getValueType(), LHS, RHS);
2813 SplitInteger(Sum, Lo, Hi);
2815 // Calculate the overflow: addition overflows iff a + b < a, and subtraction
2816 // overflows iff a - b > a.
2817 Ovf = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS, Cond);
2820 // Legalized the flag result - switch anything that used the old flag to
2821 // use the new one.
2822 ReplaceValueWith(SDValue(N, 1), Ovf);
2825 void DAGTypeLegalizer::ExpandIntRes_ADDSUBCARRY(SDNode *N,
2826 SDValue &Lo, SDValue &Hi) {
2827 // Expand the subcomponents.
2828 SDValue LHSL, LHSH, RHSL, RHSH;
2829 SDLoc dl(N);
2830 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2831 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2832 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
2833 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
2834 SDValue HiOps[3] = { LHSH, RHSH, SDValue() };
2836 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
2837 HiOps[2] = Lo.getValue(1);
2838 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
2840 // Legalized the flag result - switch anything that used the old flag to
2841 // use the new one.
2842 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2845 void DAGTypeLegalizer::ExpandIntRes_SADDSUBO_CARRY(SDNode *N,
2846 SDValue &Lo, SDValue &Hi) {
2847 // Expand the subcomponents.
2848 SDValue LHSL, LHSH, RHSL, RHSH;
2849 SDLoc dl(N);
2850 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2851 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2852 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
2854 // We need to use an unsigned carry op for the lo part.
2855 unsigned CarryOp = N->getOpcode() == ISD::SADDO_CARRY ? ISD::ADDCARRY
2856 : ISD::SUBCARRY;
2857 Lo = DAG.getNode(CarryOp, dl, VTList, { LHSL, RHSL, N->getOperand(2) });
2858 Hi = DAG.getNode(N->getOpcode(), dl, VTList, { LHSH, RHSH, Lo.getValue(1) });
2860 // Legalized the flag result - switch anything that used the old flag to
2861 // use the new one.
2862 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2865 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
2866 SDValue &Lo, SDValue &Hi) {
2867 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2868 SDLoc dl(N);
2869 SDValue Op = N->getOperand(0);
2870 if (Op.getValueType().bitsLE(NVT)) {
2871 // The low part is any extension of the input (which degenerates to a copy).
2872 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
2873 Hi = DAG.getUNDEF(NVT); // The high part is undefined.
2874 } else {
2875 // For example, extension of an i48 to an i64. The operand type necessarily
2876 // promotes to the result type, so will end up being expanded too.
2877 assert(getTypeAction(Op.getValueType()) ==
2878 TargetLowering::TypePromoteInteger &&
2879 "Only know how to promote this result!");
2880 SDValue Res = GetPromotedInteger(Op);
2881 assert(Res.getValueType() == N->getValueType(0) &&
2882 "Operand over promoted?");
2883 // Split the promoted operand. This will simplify when it is expanded.
2884 SplitInteger(Res, Lo, Hi);
2888 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
2889 SDValue &Lo, SDValue &Hi) {
2890 SDLoc dl(N);
2891 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2892 EVT NVT = Lo.getValueType();
2893 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2894 unsigned NVTBits = NVT.getSizeInBits();
2895 unsigned EVTBits = EVT.getSizeInBits();
2897 if (NVTBits < EVTBits) {
2898 Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
2899 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
2900 EVTBits - NVTBits)));
2901 } else {
2902 Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
2903 // The high part replicates the sign bit of Lo, make it explicit.
2904 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
2905 DAG.getConstant(NVTBits - 1, dl,
2906 TLI.getPointerTy(DAG.getDataLayout())));
2910 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
2911 SDValue &Lo, SDValue &Hi) {
2912 SDLoc dl(N);
2913 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2914 EVT NVT = Lo.getValueType();
2915 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2916 unsigned NVTBits = NVT.getSizeInBits();
2917 unsigned EVTBits = EVT.getSizeInBits();
2919 if (NVTBits < EVTBits) {
2920 Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
2921 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
2922 EVTBits - NVTBits)));
2923 } else {
2924 Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
2925 // The high part must be zero, make it explicit.
2926 Hi = DAG.getConstant(0, dl, NVT);
2930 void DAGTypeLegalizer::ExpandIntRes_BITREVERSE(SDNode *N,
2931 SDValue &Lo, SDValue &Hi) {
2932 SDLoc dl(N);
2933 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
2934 Lo = DAG.getNode(ISD::BITREVERSE, dl, Lo.getValueType(), Lo);
2935 Hi = DAG.getNode(ISD::BITREVERSE, dl, Hi.getValueType(), Hi);
2938 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
2939 SDValue &Lo, SDValue &Hi) {
2940 SDLoc dl(N);
2941 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
2942 Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
2943 Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
2946 void DAGTypeLegalizer::ExpandIntRes_PARITY(SDNode *N, SDValue &Lo,
2947 SDValue &Hi) {
2948 SDLoc dl(N);
2949 // parity(HiLo) -> parity(Lo^Hi)
2950 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2951 EVT NVT = Lo.getValueType();
2952 Lo =
2953 DAG.getNode(ISD::PARITY, dl, NVT, DAG.getNode(ISD::XOR, dl, NVT, Lo, Hi));
2954 Hi = DAG.getConstant(0, dl, NVT);
2957 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
2958 SDValue &Lo, SDValue &Hi) {
2959 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2960 unsigned NBitWidth = NVT.getSizeInBits();
2961 auto Constant = cast<ConstantSDNode>(N);
2962 const APInt &Cst = Constant->getAPIntValue();
2963 bool IsTarget = Constant->isTargetOpcode();
2964 bool IsOpaque = Constant->isOpaque();
2965 SDLoc dl(N);
2966 Lo = DAG.getConstant(Cst.trunc(NBitWidth), dl, NVT, IsTarget, IsOpaque);
2967 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), dl, NVT, IsTarget,
2968 IsOpaque);
2971 void DAGTypeLegalizer::ExpandIntRes_ABS(SDNode *N, SDValue &Lo, SDValue &Hi) {
2972 SDLoc dl(N);
2974 SDValue N0 = N->getOperand(0);
2975 GetExpandedInteger(N0, Lo, Hi);
2976 EVT NVT = Lo.getValueType();
2978 // If we have ADDCARRY, use the expanded form of the sra+add+xor sequence we
2979 // use in LegalizeDAG. The ADD part of the expansion is based on
2980 // ExpandIntRes_ADDSUB which also uses ADDCARRY/UADDO after checking that
2981 // ADDCARRY is LegalOrCustom. Each of the pieces here can be further expanded
2982 // if needed. Shift expansion has a special case for filling with sign bits
2983 // so that we will only end up with one SRA.
2984 bool HasAddCarry = TLI.isOperationLegalOrCustom(
2985 ISD::ADDCARRY, TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2986 if (HasAddCarry) {
2987 EVT ShiftAmtTy = getShiftAmountTyForConstant(NVT, TLI, DAG);
2988 SDValue Sign =
2989 DAG.getNode(ISD::SRA, dl, NVT, Hi,
2990 DAG.getConstant(NVT.getSizeInBits() - 1, dl, ShiftAmtTy));
2991 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
2992 Lo = DAG.getNode(ISD::UADDO, dl, VTList, Lo, Sign);
2993 Hi = DAG.getNode(ISD::ADDCARRY, dl, VTList, Hi, Sign, Lo.getValue(1));
2994 Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Sign);
2995 Hi = DAG.getNode(ISD::XOR, dl, NVT, Hi, Sign);
2996 return;
2999 // abs(HiLo) -> (Hi < 0 ? -HiLo : HiLo)
3000 EVT VT = N->getValueType(0);
3001 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT,
3002 DAG.getConstant(0, dl, VT), N0);
3003 SDValue NegLo, NegHi;
3004 SplitInteger(Neg, NegLo, NegHi);
3006 SDValue HiIsNeg = DAG.getSetCC(dl, getSetCCResultType(NVT),
3007 DAG.getConstant(0, dl, NVT), Hi, ISD::SETGT);
3008 Lo = DAG.getSelect(dl, NVT, HiIsNeg, NegLo, Lo);
3009 Hi = DAG.getSelect(dl, NVT, HiIsNeg, NegHi, Hi);
3012 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
3013 SDValue &Lo, SDValue &Hi) {
3014 SDLoc dl(N);
3015 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
3016 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3017 EVT NVT = Lo.getValueType();
3019 SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
3020 DAG.getConstant(0, dl, NVT), ISD::SETNE);
3022 SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
3023 SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, NVT, Hi);
3025 Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
3026 DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
3027 DAG.getConstant(NVT.getSizeInBits(), dl,
3028 NVT)));
3029 Hi = DAG.getConstant(0, dl, NVT);
3032 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
3033 SDValue &Lo, SDValue &Hi) {
3034 SDLoc dl(N);
3035 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
3036 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3037 EVT NVT = Lo.getValueType();
3038 Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
3039 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
3040 Hi = DAG.getConstant(0, dl, NVT);
3043 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
3044 SDValue &Lo, SDValue &Hi) {
3045 SDLoc dl(N);
3046 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
3047 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3048 EVT NVT = Lo.getValueType();
3050 SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
3051 DAG.getConstant(0, dl, NVT), ISD::SETNE);
3053 SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, NVT, Lo);
3054 SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
3056 Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
3057 DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
3058 DAG.getConstant(NVT.getSizeInBits(), dl,
3059 NVT)));
3060 Hi = DAG.getConstant(0, dl, NVT);
3063 void DAGTypeLegalizer::ExpandIntRes_FLT_ROUNDS(SDNode *N, SDValue &Lo,
3064 SDValue &Hi) {
3065 SDLoc dl(N);
3066 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3067 unsigned NBitWidth = NVT.getSizeInBits();
3069 EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
3070 Lo = DAG.getNode(ISD::FLT_ROUNDS_, dl, {NVT, MVT::Other}, N->getOperand(0));
3071 SDValue Chain = Lo.getValue(1);
3072 // The high part is the sign of Lo, as -1 is a valid value for FLT_ROUNDS
3073 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
3074 DAG.getConstant(NBitWidth - 1, dl, ShiftAmtTy));
3076 // Legalize the chain result - switch anything that used the old chain to
3077 // use the new one.
3078 ReplaceValueWith(SDValue(N, 1), Chain);
3081 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
3082 SDValue &Hi) {
3083 SDLoc dl(N);
3084 EVT VT = N->getValueType(0);
3086 bool IsStrict = N->isStrictFPOpcode();
3087 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
3088 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
3089 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
3090 Op = GetPromotedFloat(Op);
3092 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftPromoteHalf) {
3093 EVT NFPVT = TLI.getTypeToTransformTo(*DAG.getContext(), Op.getValueType());
3094 Op = GetSoftPromotedHalf(Op);
3095 Op = DAG.getNode(ISD::FP16_TO_FP, dl, NFPVT, Op);
3098 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
3099 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
3100 TargetLowering::MakeLibCallOptions CallOptions;
3101 CallOptions.setSExt(true);
3102 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, VT, Op,
3103 CallOptions, dl, Chain);
3104 SplitInteger(Tmp.first, Lo, Hi);
3106 if (IsStrict)
3107 ReplaceValueWith(SDValue(N, 1), Tmp.second);
3110 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
3111 SDValue &Hi) {
3112 SDLoc dl(N);
3113 EVT VT = N->getValueType(0);
3115 bool IsStrict = N->isStrictFPOpcode();
3116 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
3117 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
3118 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
3119 Op = GetPromotedFloat(Op);
3121 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftPromoteHalf) {
3122 EVT NFPVT = TLI.getTypeToTransformTo(*DAG.getContext(), Op.getValueType());
3123 Op = GetSoftPromotedHalf(Op);
3124 Op = DAG.getNode(ISD::FP16_TO_FP, dl, NFPVT, Op);
3127 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
3128 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
3129 TargetLowering::MakeLibCallOptions CallOptions;
3130 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, VT, Op,
3131 CallOptions, dl, Chain);
3132 SplitInteger(Tmp.first, Lo, Hi);
3134 if (IsStrict)
3135 ReplaceValueWith(SDValue(N, 1), Tmp.second);
3138 void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT_SAT(SDNode *N, SDValue &Lo,
3139 SDValue &Hi) {
3140 SDValue Res = TLI.expandFP_TO_INT_SAT(N, DAG);
3141 SplitInteger(Res, Lo, Hi);
3144 void DAGTypeLegalizer::ExpandIntRes_LLROUND_LLRINT(SDNode *N, SDValue &Lo,
3145 SDValue &Hi) {
3146 SDValue Op = N->getOperand(N->isStrictFPOpcode() ? 1 : 0);
3148 assert(getTypeAction(Op.getValueType()) != TargetLowering::TypePromoteFloat &&
3149 "Input type needs to be promoted!");
3151 EVT VT = Op.getValueType();
3153 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3154 if (N->getOpcode() == ISD::LLROUND ||
3155 N->getOpcode() == ISD::STRICT_LLROUND) {
3156 if (VT == MVT::f32)
3157 LC = RTLIB::LLROUND_F32;
3158 else if (VT == MVT::f64)
3159 LC = RTLIB::LLROUND_F64;
3160 else if (VT == MVT::f80)
3161 LC = RTLIB::LLROUND_F80;
3162 else if (VT == MVT::f128)
3163 LC = RTLIB::LLROUND_F128;
3164 else if (VT == MVT::ppcf128)
3165 LC = RTLIB::LLROUND_PPCF128;
3166 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llround input type!");
3167 } else if (N->getOpcode() == ISD::LLRINT ||
3168 N->getOpcode() == ISD::STRICT_LLRINT) {
3169 if (VT == MVT::f32)
3170 LC = RTLIB::LLRINT_F32;
3171 else if (VT == MVT::f64)
3172 LC = RTLIB::LLRINT_F64;
3173 else if (VT == MVT::f80)
3174 LC = RTLIB::LLRINT_F80;
3175 else if (VT == MVT::f128)
3176 LC = RTLIB::LLRINT_F128;
3177 else if (VT == MVT::ppcf128)
3178 LC = RTLIB::LLRINT_PPCF128;
3179 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llrint input type!");
3180 } else
3181 llvm_unreachable("Unexpected opcode!");
3183 SDLoc dl(N);
3184 EVT RetVT = N->getValueType(0);
3185 SDValue Chain = N->isStrictFPOpcode() ? N->getOperand(0) : SDValue();
3187 TargetLowering::MakeLibCallOptions CallOptions;
3188 CallOptions.setSExt(true);
3189 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
3190 Op, CallOptions, dl,
3191 Chain);
3192 SplitInteger(Tmp.first, Lo, Hi);
3194 if (N->isStrictFPOpcode())
3195 ReplaceValueWith(SDValue(N, 1), Tmp.second);
3198 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
3199 SDValue &Lo, SDValue &Hi) {
3200 if (N->isAtomic()) {
3201 // It's typical to have larger CAS than atomic load instructions.
3202 SDLoc dl(N);
3203 EVT VT = N->getMemoryVT();
3204 SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
3205 SDValue Zero = DAG.getConstant(0, dl, VT);
3206 SDValue Swap = DAG.getAtomicCmpSwap(
3207 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, dl,
3208 VT, VTs, N->getOperand(0),
3209 N->getOperand(1), Zero, Zero, N->getMemOperand());
3210 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
3211 ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
3212 return;
3215 if (ISD::isNormalLoad(N)) {
3216 ExpandRes_NormalLoad(N, Lo, Hi);
3217 return;
3220 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
3222 EVT VT = N->getValueType(0);
3223 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3224 SDValue Ch = N->getChain();
3225 SDValue Ptr = N->getBasePtr();
3226 ISD::LoadExtType ExtType = N->getExtensionType();
3227 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
3228 AAMDNodes AAInfo = N->getAAInfo();
3229 SDLoc dl(N);
3231 assert(NVT.isByteSized() && "Expanded type not byte sized!");
3233 if (N->getMemoryVT().bitsLE(NVT)) {
3234 EVT MemVT = N->getMemoryVT();
3236 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(), MemVT,
3237 N->getOriginalAlign(), MMOFlags, AAInfo);
3239 // Remember the chain.
3240 Ch = Lo.getValue(1);
3242 if (ExtType == ISD::SEXTLOAD) {
3243 // The high part is obtained by SRA'ing all but one of the bits of the
3244 // lo part.
3245 unsigned LoSize = Lo.getValueSizeInBits();
3246 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
3247 DAG.getConstant(LoSize - 1, dl,
3248 TLI.getPointerTy(DAG.getDataLayout())));
3249 } else if (ExtType == ISD::ZEXTLOAD) {
3250 // The high part is just a zero.
3251 Hi = DAG.getConstant(0, dl, NVT);
3252 } else {
3253 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
3254 // The high part is undefined.
3255 Hi = DAG.getUNDEF(NVT);
3257 } else if (DAG.getDataLayout().isLittleEndian()) {
3258 // Little-endian - low bits are at low addresses.
3259 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(),
3260 N->getOriginalAlign(), MMOFlags, AAInfo);
3262 unsigned ExcessBits =
3263 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
3264 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
3266 // Increment the pointer to the other half.
3267 unsigned IncrementSize = NVT.getSizeInBits()/8;
3268 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(IncrementSize), dl);
3269 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
3270 N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
3271 N->getOriginalAlign(), MMOFlags, AAInfo);
3273 // Build a factor node to remember that this load is independent of the
3274 // other one.
3275 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
3276 Hi.getValue(1));
3277 } else {
3278 // Big-endian - high bits are at low addresses. Favor aligned loads at
3279 // the cost of some bit-fiddling.
3280 EVT MemVT = N->getMemoryVT();
3281 unsigned EBytes = MemVT.getStoreSize();
3282 unsigned IncrementSize = NVT.getSizeInBits()/8;
3283 unsigned ExcessBits = (EBytes - IncrementSize)*8;
3285 // Load both the high bits and maybe some of the low bits.
3286 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
3287 EVT::getIntegerVT(*DAG.getContext(),
3288 MemVT.getSizeInBits() - ExcessBits),
3289 N->getOriginalAlign(), MMOFlags, AAInfo);
3291 // Increment the pointer to the other half.
3292 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(IncrementSize), dl);
3293 // Load the rest of the low bits.
3294 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
3295 N->getPointerInfo().getWithOffset(IncrementSize),
3296 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
3297 N->getOriginalAlign(), MMOFlags, AAInfo);
3299 // Build a factor node to remember that this load is independent of the
3300 // other one.
3301 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
3302 Hi.getValue(1));
3304 if (ExcessBits < NVT.getSizeInBits()) {
3305 // Transfer low bits from the bottom of Hi to the top of Lo.
3306 Lo = DAG.getNode(
3307 ISD::OR, dl, NVT, Lo,
3308 DAG.getNode(ISD::SHL, dl, NVT, Hi,
3309 DAG.getConstant(ExcessBits, dl,
3310 TLI.getPointerTy(DAG.getDataLayout()))));
3311 // Move high bits to the right position in Hi.
3312 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, NVT,
3314 DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
3315 TLI.getPointerTy(DAG.getDataLayout())));
3319 // Legalize the chain result - switch anything that used the old chain to
3320 // use the new one.
3321 ReplaceValueWith(SDValue(N, 1), Ch);
3324 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
3325 SDValue &Lo, SDValue &Hi) {
3326 SDLoc dl(N);
3327 SDValue LL, LH, RL, RH;
3328 GetExpandedInteger(N->getOperand(0), LL, LH);
3329 GetExpandedInteger(N->getOperand(1), RL, RH);
3330 Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
3331 Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
3334 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
3335 SDValue &Lo, SDValue &Hi) {
3336 EVT VT = N->getValueType(0);
3337 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3338 SDLoc dl(N);
3340 SDValue LL, LH, RL, RH;
3341 GetExpandedInteger(N->getOperand(0), LL, LH);
3342 GetExpandedInteger(N->getOperand(1), RL, RH);
3344 if (TLI.expandMUL(N, Lo, Hi, NVT, DAG,
3345 TargetLowering::MulExpansionKind::OnlyLegalOrCustom,
3346 LL, LH, RL, RH))
3347 return;
3349 // If nothing else, we can make a libcall.
3350 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3351 if (VT == MVT::i16)
3352 LC = RTLIB::MUL_I16;
3353 else if (VT == MVT::i32)
3354 LC = RTLIB::MUL_I32;
3355 else if (VT == MVT::i64)
3356 LC = RTLIB::MUL_I64;
3357 else if (VT == MVT::i128)
3358 LC = RTLIB::MUL_I128;
3360 if (LC == RTLIB::UNKNOWN_LIBCALL || !TLI.getLibcallName(LC)) {
3361 // We'll expand the multiplication by brute force because we have no other
3362 // options. This is a trivially-generalized version of the code from
3363 // Hacker's Delight (itself derived from Knuth's Algorithm M from section
3364 // 4.3.1).
3365 unsigned Bits = NVT.getSizeInBits();
3366 unsigned HalfBits = Bits >> 1;
3367 SDValue Mask = DAG.getConstant(APInt::getLowBitsSet(Bits, HalfBits), dl,
3368 NVT);
3369 SDValue LLL = DAG.getNode(ISD::AND, dl, NVT, LL, Mask);
3370 SDValue RLL = DAG.getNode(ISD::AND, dl, NVT, RL, Mask);
3372 SDValue T = DAG.getNode(ISD::MUL, dl, NVT, LLL, RLL);
3373 SDValue TL = DAG.getNode(ISD::AND, dl, NVT, T, Mask);
3375 EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
3376 if (APInt::getMaxValue(ShiftAmtTy.getSizeInBits()).ult(HalfBits)) {
3377 // The type from TLI is too small to fit the shift amount we want.
3378 // Override it with i32. The shift will have to be legalized.
3379 ShiftAmtTy = MVT::i32;
3381 SDValue Shift = DAG.getConstant(HalfBits, dl, ShiftAmtTy);
3382 SDValue TH = DAG.getNode(ISD::SRL, dl, NVT, T, Shift);
3383 SDValue LLH = DAG.getNode(ISD::SRL, dl, NVT, LL, Shift);
3384 SDValue RLH = DAG.getNode(ISD::SRL, dl, NVT, RL, Shift);
3386 SDValue U = DAG.getNode(ISD::ADD, dl, NVT,
3387 DAG.getNode(ISD::MUL, dl, NVT, LLH, RLL), TH);
3388 SDValue UL = DAG.getNode(ISD::AND, dl, NVT, U, Mask);
3389 SDValue UH = DAG.getNode(ISD::SRL, dl, NVT, U, Shift);
3391 SDValue V = DAG.getNode(ISD::ADD, dl, NVT,
3392 DAG.getNode(ISD::MUL, dl, NVT, LLL, RLH), UL);
3393 SDValue VH = DAG.getNode(ISD::SRL, dl, NVT, V, Shift);
3395 SDValue W = DAG.getNode(ISD::ADD, dl, NVT,
3396 DAG.getNode(ISD::MUL, dl, NVT, LLH, RLH),
3397 DAG.getNode(ISD::ADD, dl, NVT, UH, VH));
3398 Lo = DAG.getNode(ISD::ADD, dl, NVT, TL,
3399 DAG.getNode(ISD::SHL, dl, NVT, V, Shift));
3401 Hi = DAG.getNode(ISD::ADD, dl, NVT, W,
3402 DAG.getNode(ISD::ADD, dl, NVT,
3403 DAG.getNode(ISD::MUL, dl, NVT, RH, LL),
3404 DAG.getNode(ISD::MUL, dl, NVT, RL, LH)));
3405 return;
3408 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3409 TargetLowering::MakeLibCallOptions CallOptions;
3410 CallOptions.setSExt(true);
3411 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first,
3412 Lo, Hi);
3415 void DAGTypeLegalizer::ExpandIntRes_READCYCLECOUNTER(SDNode *N, SDValue &Lo,
3416 SDValue &Hi) {
3417 SDLoc DL(N);
3418 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3419 SDVTList VTs = DAG.getVTList(NVT, NVT, MVT::Other);
3420 SDValue R = DAG.getNode(N->getOpcode(), DL, VTs, N->getOperand(0));
3421 Lo = R.getValue(0);
3422 Hi = R.getValue(1);
3423 ReplaceValueWith(SDValue(N, 1), R.getValue(2));
3426 void DAGTypeLegalizer::ExpandIntRes_ADDSUBSAT(SDNode *N, SDValue &Lo,
3427 SDValue &Hi) {
3428 SDValue Result = TLI.expandAddSubSat(N, DAG);
3429 SplitInteger(Result, Lo, Hi);
3432 void DAGTypeLegalizer::ExpandIntRes_SHLSAT(SDNode *N, SDValue &Lo,
3433 SDValue &Hi) {
3434 SDValue Result = TLI.expandShlSat(N, DAG);
3435 SplitInteger(Result, Lo, Hi);
3438 /// This performs an expansion of the integer result for a fixed point
3439 /// multiplication. The default expansion performs rounding down towards
3440 /// negative infinity, though targets that do care about rounding should specify
3441 /// a target hook for rounding and provide their own expansion or lowering of
3442 /// fixed point multiplication to be consistent with rounding.
3443 void DAGTypeLegalizer::ExpandIntRes_MULFIX(SDNode *N, SDValue &Lo,
3444 SDValue &Hi) {
3445 SDLoc dl(N);
3446 EVT VT = N->getValueType(0);
3447 unsigned VTSize = VT.getScalarSizeInBits();
3448 SDValue LHS = N->getOperand(0);
3449 SDValue RHS = N->getOperand(1);
3450 uint64_t Scale = N->getConstantOperandVal(2);
3451 bool Saturating = (N->getOpcode() == ISD::SMULFIXSAT ||
3452 N->getOpcode() == ISD::UMULFIXSAT);
3453 bool Signed = (N->getOpcode() == ISD::SMULFIX ||
3454 N->getOpcode() == ISD::SMULFIXSAT);
3456 // Handle special case when scale is equal to zero.
3457 if (!Scale) {
3458 SDValue Result;
3459 if (!Saturating) {
3460 Result = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3461 } else {
3462 EVT BoolVT = getSetCCResultType(VT);
3463 unsigned MulOp = Signed ? ISD::SMULO : ISD::UMULO;
3464 Result = DAG.getNode(MulOp, dl, DAG.getVTList(VT, BoolVT), LHS, RHS);
3465 SDValue Product = Result.getValue(0);
3466 SDValue Overflow = Result.getValue(1);
3467 if (Signed) {
3468 APInt MinVal = APInt::getSignedMinValue(VTSize);
3469 APInt MaxVal = APInt::getSignedMaxValue(VTSize);
3470 SDValue SatMin = DAG.getConstant(MinVal, dl, VT);
3471 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
3472 SDValue Zero = DAG.getConstant(0, dl, VT);
3473 SDValue ProdNeg = DAG.getSetCC(dl, BoolVT, Product, Zero, ISD::SETLT);
3474 Result = DAG.getSelect(dl, VT, ProdNeg, SatMax, SatMin);
3475 Result = DAG.getSelect(dl, VT, Overflow, Result, Product);
3476 } else {
3477 // For unsigned multiplication, we only need to check the max since we
3478 // can't really overflow towards zero.
3479 APInt MaxVal = APInt::getMaxValue(VTSize);
3480 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
3481 Result = DAG.getSelect(dl, VT, Overflow, SatMax, Product);
3484 SplitInteger(Result, Lo, Hi);
3485 return;
3488 // For SMULFIX[SAT] we only expect to find Scale<VTSize, but this assert will
3489 // cover for unhandled cases below, while still being valid for UMULFIX[SAT].
3490 assert(Scale <= VTSize && "Scale can't be larger than the value type size.");
3492 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3493 SDValue LL, LH, RL, RH;
3494 GetExpandedInteger(LHS, LL, LH);
3495 GetExpandedInteger(RHS, RL, RH);
3496 SmallVector<SDValue, 4> Result;
3498 unsigned LoHiOp = Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI;
3499 if (!TLI.expandMUL_LOHI(LoHiOp, VT, dl, LHS, RHS, Result, NVT, DAG,
3500 TargetLowering::MulExpansionKind::OnlyLegalOrCustom,
3501 LL, LH, RL, RH)) {
3502 report_fatal_error("Unable to expand MUL_FIX using MUL_LOHI.");
3503 return;
3506 unsigned NVTSize = NVT.getScalarSizeInBits();
3507 assert((VTSize == NVTSize * 2) && "Expected the new value type to be half "
3508 "the size of the current value type");
3509 EVT ShiftTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
3511 // After getting the multiplication result in 4 parts, we need to perform a
3512 // shift right by the amount of the scale to get the result in that scale.
3514 // Let's say we multiply 2 64 bit numbers. The resulting value can be held in
3515 // 128 bits that are cut into 4 32-bit parts:
3517 // HH HL LH LL
3518 // |---32---|---32---|---32---|---32---|
3519 // 128 96 64 32 0
3521 // |------VTSize-----|
3523 // |NVTSize-|
3525 // The resulting Lo and Hi would normally be in LL and LH after the shift. But
3526 // to avoid unneccessary shifting of all 4 parts, we can adjust the shift
3527 // amount and get Lo and Hi using two funnel shifts. Or for the special case
3528 // when Scale is a multiple of NVTSize we can just pick the result without
3529 // shifting.
3530 uint64_t Part0 = Scale / NVTSize; // Part holding lowest bit needed.
3531 if (Scale % NVTSize) {
3532 SDValue ShiftAmount = DAG.getConstant(Scale % NVTSize, dl, ShiftTy);
3533 Lo = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 1], Result[Part0],
3534 ShiftAmount);
3535 Hi = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 2], Result[Part0 + 1],
3536 ShiftAmount);
3537 } else {
3538 Lo = Result[Part0];
3539 Hi = Result[Part0 + 1];
3542 // Unless saturation is requested we are done. The result is in <Hi,Lo>.
3543 if (!Saturating)
3544 return;
3546 // Can not overflow when there is no integer part.
3547 if (Scale == VTSize)
3548 return;
3550 // To handle saturation we must check for overflow in the multiplication.
3552 // Unsigned overflow happened if the upper (VTSize - Scale) bits (of Result)
3553 // aren't all zeroes.
3555 // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of Result)
3556 // aren't all ones or all zeroes.
3558 // We cannot overflow past HH when multiplying 2 ints of size VTSize, so the
3559 // highest bit of HH determines saturation direction in the event of signed
3560 // saturation.
3562 SDValue ResultHL = Result[2];
3563 SDValue ResultHH = Result[3];
3565 SDValue SatMax, SatMin;
3566 SDValue NVTZero = DAG.getConstant(0, dl, NVT);
3567 SDValue NVTNeg1 = DAG.getConstant(-1, dl, NVT);
3568 EVT BoolNVT = getSetCCResultType(NVT);
3570 if (!Signed) {
3571 if (Scale < NVTSize) {
3572 // Overflow happened if ((HH | (HL >> Scale)) != 0).
3573 SDValue HLAdjusted = DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
3574 DAG.getConstant(Scale, dl, ShiftTy));
3575 SDValue Tmp = DAG.getNode(ISD::OR, dl, NVT, HLAdjusted, ResultHH);
3576 SatMax = DAG.getSetCC(dl, BoolNVT, Tmp, NVTZero, ISD::SETNE);
3577 } else if (Scale == NVTSize) {
3578 // Overflow happened if (HH != 0).
3579 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETNE);
3580 } else if (Scale < VTSize) {
3581 // Overflow happened if ((HH >> (Scale - NVTSize)) != 0).
3582 SDValue HLAdjusted = DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
3583 DAG.getConstant(Scale - NVTSize, dl,
3584 ShiftTy));
3585 SatMax = DAG.getSetCC(dl, BoolNVT, HLAdjusted, NVTZero, ISD::SETNE);
3586 } else
3587 llvm_unreachable("Scale must be less or equal to VTSize for UMULFIXSAT"
3588 "(and saturation can't happen with Scale==VTSize).");
3590 Hi = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Hi);
3591 Lo = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Lo);
3592 return;
3595 if (Scale < NVTSize) {
3596 // The number of overflow bits we can check are VTSize - Scale + 1 (we
3597 // include the sign bit). If these top bits are > 0, then we overflowed past
3598 // the max value. If these top bits are < -1, then we overflowed past the
3599 // min value. Otherwise, we did not overflow.
3600 unsigned OverflowBits = VTSize - Scale + 1;
3601 assert(OverflowBits <= VTSize && OverflowBits > NVTSize &&
3602 "Extent of overflow bits must start within HL");
3603 SDValue HLHiMask = DAG.getConstant(
3604 APInt::getHighBitsSet(NVTSize, OverflowBits - NVTSize), dl, NVT);
3605 SDValue HLLoMask = DAG.getConstant(
3606 APInt::getLowBitsSet(NVTSize, VTSize - OverflowBits), dl, NVT);
3607 // We overflow max if HH > 0 or (HH == 0 && HL > HLLoMask).
3608 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
3609 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
3610 SDValue HLUGT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLLoMask, ISD::SETUGT);
3611 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
3612 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLUGT));
3613 // We overflow min if HH < -1 or (HH == -1 && HL < HLHiMask).
3614 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
3615 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
3616 SDValue HLULT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLHiMask, ISD::SETULT);
3617 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
3618 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLULT));
3619 } else if (Scale == NVTSize) {
3620 // We overflow max if HH > 0 or (HH == 0 && HL sign bit is 1).
3621 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
3622 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
3623 SDValue HLNeg = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETLT);
3624 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
3625 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLNeg));
3626 // We overflow min if HH < -1 or (HH == -1 && HL sign bit is 0).
3627 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
3628 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
3629 SDValue HLPos = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETGE);
3630 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
3631 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLPos));
3632 } else if (Scale < VTSize) {
3633 // This is similar to the case when we saturate if Scale < NVTSize, but we
3634 // only need to check HH.
3635 unsigned OverflowBits = VTSize - Scale + 1;
3636 SDValue HHHiMask = DAG.getConstant(
3637 APInt::getHighBitsSet(NVTSize, OverflowBits), dl, NVT);
3638 SDValue HHLoMask = DAG.getConstant(
3639 APInt::getLowBitsSet(NVTSize, NVTSize - OverflowBits), dl, NVT);
3640 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, HHLoMask, ISD::SETGT);
3641 SatMin = DAG.getSetCC(dl, BoolNVT, ResultHH, HHHiMask, ISD::SETLT);
3642 } else
3643 llvm_unreachable("Illegal scale for signed fixed point mul.");
3645 // Saturate to signed maximum.
3646 APInt MaxHi = APInt::getSignedMaxValue(NVTSize);
3647 APInt MaxLo = APInt::getAllOnesValue(NVTSize);
3648 Hi = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxHi, dl, NVT), Hi);
3649 Lo = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxLo, dl, NVT), Lo);
3650 // Saturate to signed minimum.
3651 APInt MinHi = APInt::getSignedMinValue(NVTSize);
3652 Hi = DAG.getSelect(dl, NVT, SatMin, DAG.getConstant(MinHi, dl, NVT), Hi);
3653 Lo = DAG.getSelect(dl, NVT, SatMin, NVTZero, Lo);
3656 void DAGTypeLegalizer::ExpandIntRes_DIVFIX(SDNode *N, SDValue &Lo,
3657 SDValue &Hi) {
3658 SDLoc dl(N);
3659 // Try expanding in the existing type first.
3660 SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, N->getOperand(0),
3661 N->getOperand(1),
3662 N->getConstantOperandVal(2), DAG);
3664 if (!Res)
3665 Res = earlyExpandDIVFIX(N, N->getOperand(0), N->getOperand(1),
3666 N->getConstantOperandVal(2), TLI, DAG);
3667 SplitInteger(Res, Lo, Hi);
3670 void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
3671 SDValue &Lo, SDValue &Hi) {
3672 assert((Node->getOpcode() == ISD::SADDO || Node->getOpcode() == ISD::SSUBO) &&
3673 "Node has unexpected Opcode");
3674 SDValue LHS = Node->getOperand(0);
3675 SDValue RHS = Node->getOperand(1);
3676 SDLoc dl(Node);
3678 SDValue Ovf;
3680 bool IsAdd = Node->getOpcode() == ISD::SADDO;
3681 unsigned CarryOp = IsAdd ? ISD::SADDO_CARRY : ISD::SSUBO_CARRY;
3683 bool HasCarryOp = TLI.isOperationLegalOrCustom(
3684 CarryOp, TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
3686 if (HasCarryOp) {
3687 // Expand the subcomponents.
3688 SDValue LHSL, LHSH, RHSL, RHSH;
3689 GetExpandedInteger(LHS, LHSL, LHSH);
3690 GetExpandedInteger(RHS, RHSL, RHSH);
3691 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), Node->getValueType(1));
3693 Lo = DAG.getNode(IsAdd ? ISD::UADDO : ISD::USUBO, dl, VTList, {LHSL, RHSL});
3694 Hi = DAG.getNode(CarryOp, dl, VTList, { LHSH, RHSH, Lo.getValue(1) });
3696 Ovf = Hi.getValue(1);
3697 } else {
3698 // Expand the result by simply replacing it with the equivalent
3699 // non-overflow-checking operation.
3700 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3701 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3702 LHS, RHS);
3703 SplitInteger(Sum, Lo, Hi);
3705 // Compute the overflow.
3707 // LHSSign -> LHS < 0
3708 // RHSSign -> RHS < 0
3709 // SumSign -> Sum < 0
3711 // Add:
3712 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3713 // Sub:
3714 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3716 // To get better codegen we can rewrite this by doing bitwise math on
3717 // the integers and extract the final sign bit at the end. So the
3718 // above becomes:
3720 // Add:
3721 // Overflow -> (~(LHS ^ RHS) & (LHS ^ Sum)) < 0
3722 // Sub:
3723 // Overflow -> ((LHS ^ RHS) & (LHS ^ Sum)) < 0
3725 // NOTE: This is different than the expansion we do in expandSADDSUBO
3726 // because it is more costly to determine the RHS is > 0 for SSUBO with the
3727 // integers split.
3728 EVT VT = LHS.getValueType();
3729 SDValue SignsMatch = DAG.getNode(ISD::XOR, dl, VT, LHS, RHS);
3730 if (IsAdd)
3731 SignsMatch = DAG.getNOT(dl, SignsMatch, VT);
3733 SDValue SumSignNE = DAG.getNode(ISD::XOR, dl, VT, LHS, Sum);
3734 Ovf = DAG.getNode(ISD::AND, dl, VT, SignsMatch, SumSignNE);
3735 EVT OType = Node->getValueType(1);
3736 Ovf = DAG.getSetCC(dl, OType, Ovf, DAG.getConstant(0, dl, VT), ISD::SETLT);
3739 // Use the calculated overflow everywhere.
3740 ReplaceValueWith(SDValue(Node, 1), Ovf);
3743 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
3744 SDValue &Lo, SDValue &Hi) {
3745 EVT VT = N->getValueType(0);
3746 SDLoc dl(N);
3747 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3749 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
3750 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
3751 SplitInteger(Res.getValue(0), Lo, Hi);
3752 return;
3755 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3756 if (VT == MVT::i16)
3757 LC = RTLIB::SDIV_I16;
3758 else if (VT == MVT::i32)
3759 LC = RTLIB::SDIV_I32;
3760 else if (VT == MVT::i64)
3761 LC = RTLIB::SDIV_I64;
3762 else if (VT == MVT::i128)
3763 LC = RTLIB::SDIV_I128;
3764 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
3766 TargetLowering::MakeLibCallOptions CallOptions;
3767 CallOptions.setSExt(true);
3768 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3771 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
3772 SDValue &Lo, SDValue &Hi) {
3773 EVT VT = N->getValueType(0);
3774 SDLoc dl(N);
3776 // If we can emit an efficient shift operation, do so now. Check to see if
3777 // the RHS is a constant.
3778 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
3779 return ExpandShiftByConstant(N, CN->getAPIntValue(), Lo, Hi);
3781 // If we can determine that the high bit of the shift is zero or one, even if
3782 // the low bits are variable, emit this shift in an optimized form.
3783 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
3784 return;
3786 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
3787 unsigned PartsOpc;
3788 if (N->getOpcode() == ISD::SHL) {
3789 PartsOpc = ISD::SHL_PARTS;
3790 } else if (N->getOpcode() == ISD::SRL) {
3791 PartsOpc = ISD::SRL_PARTS;
3792 } else {
3793 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
3794 PartsOpc = ISD::SRA_PARTS;
3797 // Next check to see if the target supports this SHL_PARTS operation or if it
3798 // will custom expand it. Don't lower this to SHL_PARTS when we optimise for
3799 // size, but create a libcall instead.
3800 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3801 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
3802 const bool LegalOrCustom =
3803 (Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3804 Action == TargetLowering::Custom;
3806 if (LegalOrCustom && TLI.shouldExpandShift(DAG, N)) {
3807 // Expand the subcomponents.
3808 SDValue LHSL, LHSH;
3809 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3810 EVT VT = LHSL.getValueType();
3812 // If the shift amount operand is coming from a vector legalization it may
3813 // have an illegal type. Fix that first by casting the operand, otherwise
3814 // the new SHL_PARTS operation would need further legalization.
3815 SDValue ShiftOp = N->getOperand(1);
3816 EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
3817 assert(ShiftTy.getScalarSizeInBits() >=
3818 Log2_32_Ceil(VT.getScalarSizeInBits()) &&
3819 "ShiftAmountTy is too small to cover the range of this type!");
3820 if (ShiftOp.getValueType() != ShiftTy)
3821 ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy);
3823 SDValue Ops[] = { LHSL, LHSH, ShiftOp };
3824 Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops);
3825 Hi = Lo.getValue(1);
3826 return;
3829 // Otherwise, emit a libcall.
3830 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3831 bool isSigned;
3832 if (N->getOpcode() == ISD::SHL) {
3833 isSigned = false; /*sign irrelevant*/
3834 if (VT == MVT::i16)
3835 LC = RTLIB::SHL_I16;
3836 else if (VT == MVT::i32)
3837 LC = RTLIB::SHL_I32;
3838 else if (VT == MVT::i64)
3839 LC = RTLIB::SHL_I64;
3840 else if (VT == MVT::i128)
3841 LC = RTLIB::SHL_I128;
3842 } else if (N->getOpcode() == ISD::SRL) {
3843 isSigned = false;
3844 if (VT == MVT::i16)
3845 LC = RTLIB::SRL_I16;
3846 else if (VT == MVT::i32)
3847 LC = RTLIB::SRL_I32;
3848 else if (VT == MVT::i64)
3849 LC = RTLIB::SRL_I64;
3850 else if (VT == MVT::i128)
3851 LC = RTLIB::SRL_I128;
3852 } else {
3853 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
3854 isSigned = true;
3855 if (VT == MVT::i16)
3856 LC = RTLIB::SRA_I16;
3857 else if (VT == MVT::i32)
3858 LC = RTLIB::SRA_I32;
3859 else if (VT == MVT::i64)
3860 LC = RTLIB::SRA_I64;
3861 else if (VT == MVT::i128)
3862 LC = RTLIB::SRA_I128;
3865 if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) {
3866 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3867 TargetLowering::MakeLibCallOptions CallOptions;
3868 CallOptions.setSExt(isSigned);
3869 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3870 return;
3873 if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
3874 llvm_unreachable("Unsupported shift!");
3877 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
3878 SDValue &Lo, SDValue &Hi) {
3879 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3880 SDLoc dl(N);
3881 SDValue Op = N->getOperand(0);
3882 if (Op.getValueType().bitsLE(NVT)) {
3883 // The low part is sign extension of the input (degenerates to a copy).
3884 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
3885 // The high part is obtained by SRA'ing all but one of the bits of low part.
3886 unsigned LoSize = NVT.getSizeInBits();
3887 Hi = DAG.getNode(
3888 ISD::SRA, dl, NVT, Lo,
3889 DAG.getConstant(LoSize - 1, dl, TLI.getPointerTy(DAG.getDataLayout())));
3890 } else {
3891 // For example, extension of an i48 to an i64. The operand type necessarily
3892 // promotes to the result type, so will end up being expanded too.
3893 assert(getTypeAction(Op.getValueType()) ==
3894 TargetLowering::TypePromoteInteger &&
3895 "Only know how to promote this result!");
3896 SDValue Res = GetPromotedInteger(Op);
3897 assert(Res.getValueType() == N->getValueType(0) &&
3898 "Operand over promoted?");
3899 // Split the promoted operand. This will simplify when it is expanded.
3900 SplitInteger(Res, Lo, Hi);
3901 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
3902 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
3903 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
3904 ExcessBits)));
3908 void DAGTypeLegalizer::
3909 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
3910 SDLoc dl(N);
3911 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3912 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3914 if (EVT.bitsLE(Lo.getValueType())) {
3915 // sext_inreg the low part if needed.
3916 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
3917 N->getOperand(1));
3919 // The high part gets the sign extension from the lo-part. This handles
3920 // things like sextinreg V:i64 from i8.
3921 Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
3922 DAG.getConstant(Hi.getValueSizeInBits() - 1, dl,
3923 TLI.getPointerTy(DAG.getDataLayout())));
3924 } else {
3925 // For example, extension of an i48 to an i64. Leave the low part alone,
3926 // sext_inreg the high part.
3927 unsigned ExcessBits = EVT.getSizeInBits() - Lo.getValueSizeInBits();
3928 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
3929 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
3930 ExcessBits)));
3934 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
3935 SDValue &Lo, SDValue &Hi) {
3936 EVT VT = N->getValueType(0);
3937 SDLoc dl(N);
3938 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3940 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
3941 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
3942 SplitInteger(Res.getValue(1), Lo, Hi);
3943 return;
3946 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3947 if (VT == MVT::i16)
3948 LC = RTLIB::SREM_I16;
3949 else if (VT == MVT::i32)
3950 LC = RTLIB::SREM_I32;
3951 else if (VT == MVT::i64)
3952 LC = RTLIB::SREM_I64;
3953 else if (VT == MVT::i128)
3954 LC = RTLIB::SREM_I128;
3955 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
3957 TargetLowering::MakeLibCallOptions CallOptions;
3958 CallOptions.setSExt(true);
3959 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3962 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
3963 SDValue &Lo, SDValue &Hi) {
3964 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3965 SDLoc dl(N);
3966 Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
3967 Hi = DAG.getNode(ISD::SRL, dl, N->getOperand(0).getValueType(),
3968 N->getOperand(0),
3969 DAG.getConstant(NVT.getSizeInBits(), dl,
3970 TLI.getPointerTy(DAG.getDataLayout())));
3971 Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
3974 void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N,
3975 SDValue &Lo, SDValue &Hi) {
3976 EVT VT = N->getValueType(0);
3977 SDLoc dl(N);
3979 if (N->getOpcode() == ISD::UMULO) {
3980 // This section expands the operation into the following sequence of
3981 // instructions. `iNh` here refers to a type which has half the bit width of
3982 // the type the original operation operated on.
3984 // %0 = %LHS.HI != 0 && %RHS.HI != 0
3985 // %1 = { iNh, i1 } @umul.with.overflow.iNh(iNh %LHS.HI, iNh %RHS.LO)
3986 // %2 = { iNh, i1 } @umul.with.overflow.iNh(iNh %RHS.HI, iNh %LHS.LO)
3987 // %3 = mul nuw iN (%LHS.LOW as iN), (%RHS.LOW as iN)
3988 // %4 = add iNh %1.0, %2.0 as iN
3989 // %5 = { iNh, i1 } @uadd.with.overflow.iNh(iNh %4, iNh %3.HIGH)
3991 // %lo = %3.LO
3992 // %hi = %5.0
3993 // %ovf = %0 || %1.1 || %2.1 || %5.1
3994 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
3995 SDValue LHSHigh, LHSLow, RHSHigh, RHSLow;
3996 GetExpandedInteger(LHS, LHSLow, LHSHigh);
3997 GetExpandedInteger(RHS, RHSLow, RHSHigh);
3998 EVT HalfVT = LHSLow.getValueType();
3999 EVT BitVT = N->getValueType(1);
4000 SDVTList VTHalfWithO = DAG.getVTList(HalfVT, BitVT);
4002 SDValue HalfZero = DAG.getConstant(0, dl, HalfVT);
4003 SDValue Overflow = DAG.getNode(ISD::AND, dl, BitVT,
4004 DAG.getSetCC(dl, BitVT, LHSHigh, HalfZero, ISD::SETNE),
4005 DAG.getSetCC(dl, BitVT, RHSHigh, HalfZero, ISD::SETNE));
4007 SDValue One = DAG.getNode(ISD::UMULO, dl, VTHalfWithO, LHSHigh, RHSLow);
4008 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, One.getValue(1));
4010 SDValue Two = DAG.getNode(ISD::UMULO, dl, VTHalfWithO, RHSHigh, LHSLow);
4011 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Two.getValue(1));
4013 SDValue HighSum = DAG.getNode(ISD::ADD, dl, HalfVT, One, Two);
4015 // Cannot use `UMUL_LOHI` directly, because some 32-bit targets (ARM) do not
4016 // know how to expand `i64,i64 = umul_lohi a, b` and abort (why isn’t this
4017 // operation recursively legalized?).
4019 // Many backends understand this pattern and will convert into LOHI
4020 // themselves, if applicable.
4021 SDValue Three = DAG.getNode(ISD::MUL, dl, VT,
4022 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LHSLow),
4023 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RHSLow));
4024 SplitInteger(Three, Lo, Hi);
4026 Hi = DAG.getNode(ISD::UADDO, dl, VTHalfWithO, Hi, HighSum);
4027 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Hi.getValue(1));
4028 ReplaceValueWith(SDValue(N, 1), Overflow);
4029 return;
4032 Type *RetTy = VT.getTypeForEVT(*DAG.getContext());
4033 EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
4034 Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext());
4036 // Replace this with a libcall that will check overflow.
4037 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4038 if (VT == MVT::i32)
4039 LC = RTLIB::MULO_I32;
4040 else if (VT == MVT::i64)
4041 LC = RTLIB::MULO_I64;
4042 else if (VT == MVT::i128)
4043 LC = RTLIB::MULO_I128;
4045 if (LC == RTLIB::UNKNOWN_LIBCALL || !TLI.getLibcallName(LC)) {
4046 // FIXME: This is not an optimal expansion, but better than crashing.
4047 EVT WideVT =
4048 EVT::getIntegerVT(*DAG.getContext(), VT.getScalarSizeInBits() * 2);
4049 SDValue LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, WideVT, N->getOperand(0));
4050 SDValue RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, WideVT, N->getOperand(1));
4051 SDValue Mul = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
4052 SDValue MulLo, MulHi;
4053 SplitInteger(Mul, MulLo, MulHi);
4054 SDValue SRA =
4055 DAG.getNode(ISD::SRA, dl, VT, MulLo,
4056 DAG.getConstant(VT.getScalarSizeInBits() - 1, dl, VT));
4057 SDValue Overflow =
4058 DAG.getSetCC(dl, N->getValueType(1), MulHi, SRA, ISD::SETNE);
4059 SplitInteger(MulLo, Lo, Hi);
4060 ReplaceValueWith(SDValue(N, 1), Overflow);
4061 return;
4064 SDValue Temp = DAG.CreateStackTemporary(PtrVT);
4065 // Temporary for the overflow value, default it to zero.
4066 SDValue Chain =
4067 DAG.getStore(DAG.getEntryNode(), dl, DAG.getConstant(0, dl, PtrVT), Temp,
4068 MachinePointerInfo());
4070 TargetLowering::ArgListTy Args;
4071 TargetLowering::ArgListEntry Entry;
4072 for (const SDValue &Op : N->op_values()) {
4073 EVT ArgVT = Op.getValueType();
4074 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
4075 Entry.Node = Op;
4076 Entry.Ty = ArgTy;
4077 Entry.IsSExt = true;
4078 Entry.IsZExt = false;
4079 Args.push_back(Entry);
4082 // Also pass the address of the overflow check.
4083 Entry.Node = Temp;
4084 Entry.Ty = PtrTy->getPointerTo();
4085 Entry.IsSExt = true;
4086 Entry.IsZExt = false;
4087 Args.push_back(Entry);
4089 SDValue Func = DAG.getExternalSymbol(TLI.getLibcallName(LC), PtrVT);
4091 TargetLowering::CallLoweringInfo CLI(DAG);
4092 CLI.setDebugLoc(dl)
4093 .setChain(Chain)
4094 .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Func, std::move(Args))
4095 .setSExtResult();
4097 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
4099 SplitInteger(CallInfo.first, Lo, Hi);
4100 SDValue Temp2 =
4101 DAG.getLoad(PtrVT, dl, CallInfo.second, Temp, MachinePointerInfo());
4102 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2,
4103 DAG.getConstant(0, dl, PtrVT),
4104 ISD::SETNE);
4105 // Use the overflow from the libcall everywhere.
4106 ReplaceValueWith(SDValue(N, 1), Ofl);
4109 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
4110 SDValue &Lo, SDValue &Hi) {
4111 EVT VT = N->getValueType(0);
4112 SDLoc dl(N);
4113 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
4115 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
4116 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
4117 SplitInteger(Res.getValue(0), Lo, Hi);
4118 return;
4121 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4122 if (VT == MVT::i16)
4123 LC = RTLIB::UDIV_I16;
4124 else if (VT == MVT::i32)
4125 LC = RTLIB::UDIV_I32;
4126 else if (VT == MVT::i64)
4127 LC = RTLIB::UDIV_I64;
4128 else if (VT == MVT::i128)
4129 LC = RTLIB::UDIV_I128;
4130 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
4132 TargetLowering::MakeLibCallOptions CallOptions;
4133 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
4136 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
4137 SDValue &Lo, SDValue &Hi) {
4138 EVT VT = N->getValueType(0);
4139 SDLoc dl(N);
4140 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
4142 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
4143 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
4144 SplitInteger(Res.getValue(1), Lo, Hi);
4145 return;
4148 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4149 if (VT == MVT::i16)
4150 LC = RTLIB::UREM_I16;
4151 else if (VT == MVT::i32)
4152 LC = RTLIB::UREM_I32;
4153 else if (VT == MVT::i64)
4154 LC = RTLIB::UREM_I64;
4155 else if (VT == MVT::i128)
4156 LC = RTLIB::UREM_I128;
4157 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
4159 TargetLowering::MakeLibCallOptions CallOptions;
4160 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
4163 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
4164 SDValue &Lo, SDValue &Hi) {
4165 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4166 SDLoc dl(N);
4167 SDValue Op = N->getOperand(0);
4168 if (Op.getValueType().bitsLE(NVT)) {
4169 // The low part is zero extension of the input (degenerates to a copy).
4170 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
4171 Hi = DAG.getConstant(0, dl, NVT); // The high part is just a zero.
4172 } else {
4173 // For example, extension of an i48 to an i64. The operand type necessarily
4174 // promotes to the result type, so will end up being expanded too.
4175 assert(getTypeAction(Op.getValueType()) ==
4176 TargetLowering::TypePromoteInteger &&
4177 "Only know how to promote this result!");
4178 SDValue Res = GetPromotedInteger(Op);
4179 assert(Res.getValueType() == N->getValueType(0) &&
4180 "Operand over promoted?");
4181 // Split the promoted operand. This will simplify when it is expanded.
4182 SplitInteger(Res, Lo, Hi);
4183 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
4184 Hi = DAG.getZeroExtendInReg(Hi, dl,
4185 EVT::getIntegerVT(*DAG.getContext(),
4186 ExcessBits));
4190 void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N,
4191 SDValue &Lo, SDValue &Hi) {
4192 SDLoc dl(N);
4193 EVT VT = cast<AtomicSDNode>(N)->getMemoryVT();
4194 SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
4195 SDValue Zero = DAG.getConstant(0, dl, VT);
4196 SDValue Swap = DAG.getAtomicCmpSwap(
4197 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, dl,
4198 cast<AtomicSDNode>(N)->getMemoryVT(), VTs, N->getOperand(0),
4199 N->getOperand(1), Zero, Zero, cast<AtomicSDNode>(N)->getMemOperand());
4201 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
4202 ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
4205 void DAGTypeLegalizer::ExpandIntRes_VECREDUCE(SDNode *N,
4206 SDValue &Lo, SDValue &Hi) {
4207 // TODO For VECREDUCE_(AND|OR|XOR) we could split the vector and calculate
4208 // both halves independently.
4209 SDValue Res = TLI.expandVecReduce(N, DAG);
4210 SplitInteger(Res, Lo, Hi);
4213 void DAGTypeLegalizer::ExpandIntRes_Rotate(SDNode *N,
4214 SDValue &Lo, SDValue &Hi) {
4215 // Lower the rotate to shifts and ORs which can be expanded.
4216 SDValue Res;
4217 TLI.expandROT(N, true /*AllowVectorOps*/, Res, DAG);
4218 SplitInteger(Res, Lo, Hi);
4221 void DAGTypeLegalizer::ExpandIntRes_FunnelShift(SDNode *N,
4222 SDValue &Lo, SDValue &Hi) {
4223 // Lower the funnel shift to shifts and ORs which can be expanded.
4224 SDValue Res;
4225 TLI.expandFunnelShift(N, Res, DAG);
4226 SplitInteger(Res, Lo, Hi);
4229 void DAGTypeLegalizer::ExpandIntRes_VSCALE(SDNode *N, SDValue &Lo,
4230 SDValue &Hi) {
4231 EVT VT = N->getValueType(0);
4232 EVT HalfVT =
4233 EVT::getIntegerVT(*DAG.getContext(), N->getValueSizeInBits(0) / 2);
4234 SDLoc dl(N);
4236 // We assume VSCALE(1) fits into a legal integer.
4237 APInt One(HalfVT.getSizeInBits(), 1);
4238 SDValue VScaleBase = DAG.getVScale(dl, HalfVT, One);
4239 VScaleBase = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, VScaleBase);
4240 SDValue Res = DAG.getNode(ISD::MUL, dl, VT, VScaleBase, N->getOperand(0));
4241 SplitInteger(Res, Lo, Hi);
4244 //===----------------------------------------------------------------------===//
4245 // Integer Operand Expansion
4246 //===----------------------------------------------------------------------===//
4248 /// ExpandIntegerOperand - This method is called when the specified operand of
4249 /// the specified node is found to need expansion. At this point, all of the
4250 /// result types of the node are known to be legal, but other operands of the
4251 /// node may need promotion or expansion as well as the specified one.
4252 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
4253 LLVM_DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG);
4254 dbgs() << "\n");
4255 SDValue Res = SDValue();
4257 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
4258 return false;
4260 switch (N->getOpcode()) {
4261 default:
4262 #ifndef NDEBUG
4263 dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
4264 N->dump(&DAG); dbgs() << "\n";
4265 #endif
4266 report_fatal_error("Do not know how to expand this operator's operand!");
4268 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
4269 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
4270 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
4271 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
4272 case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
4273 case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
4274 case ISD::SPLAT_VECTOR: Res = ExpandIntOp_SPLAT_VECTOR(N); break;
4275 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
4276 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
4277 case ISD::SETCCCARRY: Res = ExpandIntOp_SETCCCARRY(N); break;
4278 case ISD::STRICT_SINT_TO_FP:
4279 case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
4280 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
4281 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
4282 case ISD::STRICT_UINT_TO_FP:
4283 case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
4285 case ISD::SHL:
4286 case ISD::SRA:
4287 case ISD::SRL:
4288 case ISD::ROTL:
4289 case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
4290 case ISD::RETURNADDR:
4291 case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break;
4293 case ISD::ATOMIC_STORE: Res = ExpandIntOp_ATOMIC_STORE(N); break;
4296 // If the result is null, the sub-method took care of registering results etc.
4297 if (!Res.getNode()) return false;
4299 // If the result is N, the sub-method updated N in place. Tell the legalizer
4300 // core about this.
4301 if (Res.getNode() == N)
4302 return true;
4304 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
4305 "Invalid operand expansion");
4307 ReplaceValueWith(SDValue(N, 0), Res);
4308 return false;
4311 /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
4312 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
4313 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
4314 SDValue &NewRHS,
4315 ISD::CondCode &CCCode,
4316 const SDLoc &dl) {
4317 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
4318 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
4319 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
4321 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
4322 if (RHSLo == RHSHi) {
4323 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
4324 if (RHSCST->isAllOnesValue()) {
4325 // Equality comparison to -1.
4326 NewLHS = DAG.getNode(ISD::AND, dl,
4327 LHSLo.getValueType(), LHSLo, LHSHi);
4328 NewRHS = RHSLo;
4329 return;
4334 NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
4335 NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
4336 NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
4337 NewRHS = DAG.getConstant(0, dl, NewLHS.getValueType());
4338 return;
4341 // If this is a comparison of the sign bit, just look at the top part.
4342 // X > -1, x < 0
4343 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
4344 if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
4345 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
4346 NewLHS = LHSHi;
4347 NewRHS = RHSHi;
4348 return;
4351 // FIXME: This generated code sucks.
4352 ISD::CondCode LowCC;
4353 switch (CCCode) {
4354 default: llvm_unreachable("Unknown integer setcc!");
4355 case ISD::SETLT:
4356 case ISD::SETULT: LowCC = ISD::SETULT; break;
4357 case ISD::SETGT:
4358 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4359 case ISD::SETLE:
4360 case ISD::SETULE: LowCC = ISD::SETULE; break;
4361 case ISD::SETGE:
4362 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4365 // LoCmp = lo(op1) < lo(op2) // Always unsigned comparison
4366 // HiCmp = hi(op1) < hi(op2) // Signedness depends on operands
4367 // dest = hi(op1) == hi(op2) ? LoCmp : HiCmp;
4369 // NOTE: on targets without efficient SELECT of bools, we can always use
4370 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4371 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true,
4372 nullptr);
4373 SDValue LoCmp, HiCmp;
4374 if (TLI.isTypeLegal(LHSLo.getValueType()) &&
4375 TLI.isTypeLegal(RHSLo.getValueType()))
4376 LoCmp = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()), LHSLo,
4377 RHSLo, LowCC, false, DagCombineInfo, dl);
4378 if (!LoCmp.getNode())
4379 LoCmp = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()), LHSLo,
4380 RHSLo, LowCC);
4381 if (TLI.isTypeLegal(LHSHi.getValueType()) &&
4382 TLI.isTypeLegal(RHSHi.getValueType()))
4383 HiCmp = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()), LHSHi,
4384 RHSHi, CCCode, false, DagCombineInfo, dl);
4385 if (!HiCmp.getNode())
4386 HiCmp =
4387 DAG.getNode(ISD::SETCC, dl, getSetCCResultType(LHSHi.getValueType()),
4388 LHSHi, RHSHi, DAG.getCondCode(CCCode));
4390 ConstantSDNode *LoCmpC = dyn_cast<ConstantSDNode>(LoCmp.getNode());
4391 ConstantSDNode *HiCmpC = dyn_cast<ConstantSDNode>(HiCmp.getNode());
4393 bool EqAllowed = (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4394 CCCode == ISD::SETUGE || CCCode == ISD::SETULE);
4396 if ((EqAllowed && (HiCmpC && HiCmpC->isNullValue())) ||
4397 (!EqAllowed && ((HiCmpC && (HiCmpC->getAPIntValue() == 1)) ||
4398 (LoCmpC && LoCmpC->isNullValue())))) {
4399 // For LE / GE, if high part is known false, ignore the low part.
4400 // For LT / GT: if low part is known false, return the high part.
4401 // if high part is known true, ignore the low part.
4402 NewLHS = HiCmp;
4403 NewRHS = SDValue();
4404 return;
4407 if (LHSHi == RHSHi) {
4408 // Comparing the low bits is enough.
4409 NewLHS = LoCmp;
4410 NewRHS = SDValue();
4411 return;
4414 // Lower with SETCCCARRY if the target supports it.
4415 EVT HiVT = LHSHi.getValueType();
4416 EVT ExpandVT = TLI.getTypeToExpandTo(*DAG.getContext(), HiVT);
4417 bool HasSETCCCARRY = TLI.isOperationLegalOrCustom(ISD::SETCCCARRY, ExpandVT);
4419 // FIXME: Make all targets support this, then remove the other lowering.
4420 if (HasSETCCCARRY) {
4421 // SETCCCARRY can detect < and >= directly. For > and <=, flip
4422 // operands and condition code.
4423 bool FlipOperands = false;
4424 switch (CCCode) {
4425 case ISD::SETGT: CCCode = ISD::SETLT; FlipOperands = true; break;
4426 case ISD::SETUGT: CCCode = ISD::SETULT; FlipOperands = true; break;
4427 case ISD::SETLE: CCCode = ISD::SETGE; FlipOperands = true; break;
4428 case ISD::SETULE: CCCode = ISD::SETUGE; FlipOperands = true; break;
4429 default: break;
4431 if (FlipOperands) {
4432 std::swap(LHSLo, RHSLo);
4433 std::swap(LHSHi, RHSHi);
4435 // Perform a wide subtraction, feeding the carry from the low part into
4436 // SETCCCARRY. The SETCCCARRY operation is essentially looking at the high
4437 // part of the result of LHS - RHS. It is negative iff LHS < RHS. It is
4438 // zero or positive iff LHS >= RHS.
4439 EVT LoVT = LHSLo.getValueType();
4440 SDVTList VTList = DAG.getVTList(LoVT, getSetCCResultType(LoVT));
4441 SDValue LowCmp = DAG.getNode(ISD::USUBO, dl, VTList, LHSLo, RHSLo);
4442 SDValue Res = DAG.getNode(ISD::SETCCCARRY, dl, getSetCCResultType(HiVT),
4443 LHSHi, RHSHi, LowCmp.getValue(1),
4444 DAG.getCondCode(CCCode));
4445 NewLHS = Res;
4446 NewRHS = SDValue();
4447 return;
4450 NewLHS = TLI.SimplifySetCC(getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ,
4451 false, DagCombineInfo, dl);
4452 if (!NewLHS.getNode())
4453 NewLHS =
4454 DAG.getSetCC(dl, getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ);
4455 NewLHS = DAG.getSelect(dl, LoCmp.getValueType(), NewLHS, LoCmp, HiCmp);
4456 NewRHS = SDValue();
4459 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
4460 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
4461 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
4462 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
4464 // If ExpandSetCCOperands returned a scalar, we need to compare the result
4465 // against zero to select between true and false values.
4466 if (!NewRHS.getNode()) {
4467 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
4468 CCCode = ISD::SETNE;
4471 // Update N to have the operands specified.
4472 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
4473 DAG.getCondCode(CCCode), NewLHS, NewRHS,
4474 N->getOperand(4)), 0);
4477 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
4478 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
4479 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
4480 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
4482 // If ExpandSetCCOperands returned a scalar, we need to compare the result
4483 // against zero to select between true and false values.
4484 if (!NewRHS.getNode()) {
4485 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
4486 CCCode = ISD::SETNE;
4489 // Update N to have the operands specified.
4490 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
4491 N->getOperand(2), N->getOperand(3),
4492 DAG.getCondCode(CCCode)), 0);
4495 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
4496 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
4497 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
4498 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
4500 // If ExpandSetCCOperands returned a scalar, use it.
4501 if (!NewRHS.getNode()) {
4502 assert(NewLHS.getValueType() == N->getValueType(0) &&
4503 "Unexpected setcc expansion!");
4504 return NewLHS;
4507 // Otherwise, update N to have the operands specified.
4508 return SDValue(
4509 DAG.UpdateNodeOperands(N, NewLHS, NewRHS, DAG.getCondCode(CCCode)), 0);
4512 SDValue DAGTypeLegalizer::ExpandIntOp_SETCCCARRY(SDNode *N) {
4513 SDValue LHS = N->getOperand(0);
4514 SDValue RHS = N->getOperand(1);
4515 SDValue Carry = N->getOperand(2);
4516 SDValue Cond = N->getOperand(3);
4517 SDLoc dl = SDLoc(N);
4519 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
4520 GetExpandedInteger(LHS, LHSLo, LHSHi);
4521 GetExpandedInteger(RHS, RHSLo, RHSHi);
4523 // Expand to a SUBE for the low part and a smaller SETCCCARRY for the high.
4524 SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), Carry.getValueType());
4525 SDValue LowCmp = DAG.getNode(ISD::SUBCARRY, dl, VTList, LHSLo, RHSLo, Carry);
4526 return DAG.getNode(ISD::SETCCCARRY, dl, N->getValueType(0), LHSHi, RHSHi,
4527 LowCmp.getValue(1), Cond);
4530 SDValue DAGTypeLegalizer::ExpandIntOp_SPLAT_VECTOR(SDNode *N) {
4531 // Split the operand and replace with SPLAT_VECTOR_PARTS.
4532 SDValue Lo, Hi;
4533 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4534 return DAG.getNode(ISD::SPLAT_VECTOR_PARTS, SDLoc(N), N->getValueType(0), Lo,
4535 Hi);
4538 SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
4539 // The value being shifted is legal, but the shift amount is too big.
4540 // It follows that either the result of the shift is undefined, or the
4541 // upper half of the shift amount is zero. Just use the lower half.
4542 SDValue Lo, Hi;
4543 GetExpandedInteger(N->getOperand(1), Lo, Hi);
4544 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
4547 SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
4548 // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
4549 // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
4550 // constant to valid type.
4551 SDValue Lo, Hi;
4552 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4553 return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
4556 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
4557 bool IsStrict = N->isStrictFPOpcode();
4558 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
4559 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
4560 EVT DstVT = N->getValueType(0);
4561 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
4562 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4563 "Don't know how to expand this SINT_TO_FP!");
4564 TargetLowering::MakeLibCallOptions CallOptions;
4565 CallOptions.setSExt(true);
4566 std::pair<SDValue, SDValue> Tmp =
4567 TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, SDLoc(N), Chain);
4569 if (!IsStrict)
4570 return Tmp.first;
4572 ReplaceValueWith(SDValue(N, 1), Tmp.second);
4573 ReplaceValueWith(SDValue(N, 0), Tmp.first);
4574 return SDValue();
4577 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
4578 if (N->isAtomic()) {
4579 // It's typical to have larger CAS than atomic store instructions.
4580 SDLoc dl(N);
4581 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
4582 N->getMemoryVT(),
4583 N->getOperand(0), N->getOperand(2),
4584 N->getOperand(1),
4585 N->getMemOperand());
4586 return Swap.getValue(1);
4588 if (ISD::isNormalStore(N))
4589 return ExpandOp_NormalStore(N, OpNo);
4591 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
4592 assert(OpNo == 1 && "Can only expand the stored value so far");
4594 EVT VT = N->getOperand(1).getValueType();
4595 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4596 SDValue Ch = N->getChain();
4597 SDValue Ptr = N->getBasePtr();
4598 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
4599 AAMDNodes AAInfo = N->getAAInfo();
4600 SDLoc dl(N);
4601 SDValue Lo, Hi;
4603 assert(NVT.isByteSized() && "Expanded type not byte sized!");
4605 if (N->getMemoryVT().bitsLE(NVT)) {
4606 GetExpandedInteger(N->getValue(), Lo, Hi);
4607 return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
4608 N->getMemoryVT(), N->getOriginalAlign(), MMOFlags,
4609 AAInfo);
4612 if (DAG.getDataLayout().isLittleEndian()) {
4613 // Little-endian - low bits are at low addresses.
4614 GetExpandedInteger(N->getValue(), Lo, Hi);
4616 Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
4617 N->getOriginalAlign(), MMOFlags, AAInfo);
4619 unsigned ExcessBits =
4620 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
4621 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
4623 // Increment the pointer to the other half.
4624 unsigned IncrementSize = NVT.getSizeInBits()/8;
4625 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::Fixed(IncrementSize));
4626 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr,
4627 N->getPointerInfo().getWithOffset(IncrementSize),
4628 NEVT, N->getOriginalAlign(), MMOFlags, AAInfo);
4629 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
4632 // Big-endian - high bits are at low addresses. Favor aligned stores at
4633 // the cost of some bit-fiddling.
4634 GetExpandedInteger(N->getValue(), Lo, Hi);
4636 EVT ExtVT = N->getMemoryVT();
4637 unsigned EBytes = ExtVT.getStoreSize();
4638 unsigned IncrementSize = NVT.getSizeInBits()/8;
4639 unsigned ExcessBits = (EBytes - IncrementSize)*8;
4640 EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
4641 ExtVT.getSizeInBits() - ExcessBits);
4643 if (ExcessBits < NVT.getSizeInBits()) {
4644 // Transfer high bits from the top of Lo to the bottom of Hi.
4645 Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
4646 DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
4647 TLI.getPointerTy(DAG.getDataLayout())));
4648 Hi = DAG.getNode(
4649 ISD::OR, dl, NVT, Hi,
4650 DAG.getNode(ISD::SRL, dl, NVT, Lo,
4651 DAG.getConstant(ExcessBits, dl,
4652 TLI.getPointerTy(DAG.getDataLayout()))));
4655 // Store both the high bits and maybe some of the low bits.
4656 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(), HiVT,
4657 N->getOriginalAlign(), MMOFlags, AAInfo);
4659 // Increment the pointer to the other half.
4660 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::Fixed(IncrementSize));
4661 // Store the lowest ExcessBits bits in the second half.
4662 Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
4663 N->getPointerInfo().getWithOffset(IncrementSize),
4664 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
4665 N->getOriginalAlign(), MMOFlags, AAInfo);
4666 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
4669 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
4670 SDValue InL, InH;
4671 GetExpandedInteger(N->getOperand(0), InL, InH);
4672 // Just truncate the low part of the source.
4673 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL);
4676 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
4677 bool IsStrict = N->isStrictFPOpcode();
4678 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
4679 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
4680 EVT DstVT = N->getValueType(0);
4681 RTLIB::Libcall LC = RTLIB::getUINTTOFP(Op.getValueType(), DstVT);
4682 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4683 "Don't know how to expand this UINT_TO_FP!");
4684 TargetLowering::MakeLibCallOptions CallOptions;
4685 CallOptions.setSExt(true);
4686 std::pair<SDValue, SDValue> Tmp =
4687 TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, SDLoc(N), Chain);
4689 if (!IsStrict)
4690 return Tmp.first;
4692 ReplaceValueWith(SDValue(N, 1), Tmp.second);
4693 ReplaceValueWith(SDValue(N, 0), Tmp.first);
4694 return SDValue();
4697 SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) {
4698 SDLoc dl(N);
4699 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
4700 cast<AtomicSDNode>(N)->getMemoryVT(),
4701 N->getOperand(0),
4702 N->getOperand(1), N->getOperand(2),
4703 cast<AtomicSDNode>(N)->getMemOperand());
4704 return Swap.getValue(1);
4707 SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SPLICE(SDNode *N) {
4708 SDLoc dl(N);
4710 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4711 SDValue V1 = GetPromotedInteger(N->getOperand(1));
4712 EVT OutVT = V0.getValueType();
4714 return DAG.getNode(ISD::VECTOR_SPLICE, dl, OutVT, V0, V1, N->getOperand(2));
4717 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
4719 EVT OutVT = N->getValueType(0);
4720 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4721 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4722 EVT NOutVTElem = NOutVT.getVectorElementType();
4724 SDLoc dl(N);
4725 SDValue BaseIdx = N->getOperand(1);
4727 // TODO: We may be able to use this for types other than scalable
4728 // vectors and fix those tests that expect BUILD_VECTOR to be used
4729 if (OutVT.isScalableVector()) {
4730 SDValue InOp0 = N->getOperand(0);
4731 EVT InVT = InOp0.getValueType();
4733 // Promote operands and see if this is handled by target lowering,
4734 // Otherwise, use the BUILD_VECTOR approach below
4735 if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
4736 // Collect the (promoted) operands
4737 SDValue Ops[] = { GetPromotedInteger(InOp0), BaseIdx };
4739 EVT PromEltVT = Ops[0].getValueType().getVectorElementType();
4740 assert(PromEltVT.bitsLE(NOutVTElem) &&
4741 "Promoted operand has an element type greater than result");
4743 EVT ExtVT = NOutVT.changeVectorElementType(PromEltVT);
4744 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), ExtVT, Ops);
4745 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Ext);
4749 if (OutVT.isScalableVector())
4750 report_fatal_error("Unable to promote scalable types using BUILD_VECTOR");
4752 SDValue InOp0 = N->getOperand(0);
4753 if (getTypeAction(InOp0.getValueType()) == TargetLowering::TypePromoteInteger)
4754 InOp0 = GetPromotedInteger(N->getOperand(0));
4756 EVT InVT = InOp0.getValueType();
4758 unsigned OutNumElems = OutVT.getVectorNumElements();
4759 SmallVector<SDValue, 8> Ops;
4760 Ops.reserve(OutNumElems);
4761 for (unsigned i = 0; i != OutNumElems; ++i) {
4763 // Extract the element from the original vector.
4764 SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(),
4765 BaseIdx, DAG.getConstant(i, dl, BaseIdx.getValueType()));
4766 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
4767 InVT.getVectorElementType(), N->getOperand(0), Index);
4769 SDValue Op = DAG.getAnyExtOrTrunc(Ext, dl, NOutVTElem);
4770 // Insert the converted element to the new vector.
4771 Ops.push_back(Op);
4774 return DAG.getBuildVector(NOutVT, dl, Ops);
4777 SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_SUBVECTOR(SDNode *N) {
4778 EVT OutVT = N->getValueType(0);
4779 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4780 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4782 SDLoc dl(N);
4783 SDValue Vec = N->getOperand(0);
4784 SDValue SubVec = N->getOperand(1);
4785 SDValue Idx = N->getOperand(2);
4787 EVT SubVecVT = SubVec.getValueType();
4788 EVT NSubVT =
4789 EVT::getVectorVT(*DAG.getContext(), NOutVT.getVectorElementType(),
4790 SubVecVT.getVectorElementCount());
4792 Vec = GetPromotedInteger(Vec);
4793 SubVec = DAG.getNode(ISD::ANY_EXTEND, dl, NSubVT, SubVec);
4795 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, NOutVT, Vec, SubVec, Idx);
4798 SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_REVERSE(SDNode *N) {
4799 SDLoc dl(N);
4801 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4802 EVT OutVT = V0.getValueType();
4804 return DAG.getNode(ISD::VECTOR_REVERSE, dl, OutVT, V0);
4807 SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
4808 ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
4809 EVT VT = N->getValueType(0);
4810 SDLoc dl(N);
4812 ArrayRef<int> NewMask = SV->getMask().slice(0, VT.getVectorNumElements());
4814 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4815 SDValue V1 = GetPromotedInteger(N->getOperand(1));
4816 EVT OutVT = V0.getValueType();
4818 return DAG.getVectorShuffle(OutVT, dl, V0, V1, NewMask);
4822 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
4823 EVT OutVT = N->getValueType(0);
4824 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4825 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4826 unsigned NumElems = N->getNumOperands();
4827 EVT NOutVTElem = NOutVT.getVectorElementType();
4829 SDLoc dl(N);
4831 SmallVector<SDValue, 8> Ops;
4832 Ops.reserve(NumElems);
4833 for (unsigned i = 0; i != NumElems; ++i) {
4834 SDValue Op;
4835 // BUILD_VECTOR integer operand types are allowed to be larger than the
4836 // result's element type. This may still be true after the promotion. For
4837 // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
4838 // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
4839 if (N->getOperand(i).getValueType().bitsLT(NOutVTElem))
4840 Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(i));
4841 else
4842 Op = N->getOperand(i);
4843 Ops.push_back(Op);
4846 return DAG.getBuildVector(NOutVT, dl, Ops);
4849 SDValue DAGTypeLegalizer::PromoteIntRes_SCALAR_TO_VECTOR(SDNode *N) {
4851 SDLoc dl(N);
4853 assert(!N->getOperand(0).getValueType().isVector() &&
4854 "Input must be a scalar");
4856 EVT OutVT = N->getValueType(0);
4857 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4858 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4859 EVT NOutVTElem = NOutVT.getVectorElementType();
4861 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(0));
4863 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NOutVT, Op);
4866 SDValue DAGTypeLegalizer::PromoteIntRes_SPLAT_VECTOR(SDNode *N) {
4867 SDLoc dl(N);
4869 SDValue SplatVal = N->getOperand(0);
4871 assert(!SplatVal.getValueType().isVector() && "Input must be a scalar");
4873 EVT OutVT = N->getValueType(0);
4874 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4875 assert(NOutVT.isVector() && "Type must be promoted to a vector type");
4876 EVT NOutElemVT = NOutVT.getVectorElementType();
4878 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutElemVT, SplatVal);
4880 return DAG.getNode(ISD::SPLAT_VECTOR, dl, NOutVT, Op);
4883 SDValue DAGTypeLegalizer::PromoteIntRes_STEP_VECTOR(SDNode *N) {
4884 SDLoc dl(N);
4885 EVT OutVT = N->getValueType(0);
4886 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4887 assert(NOutVT.isVector() && "Type must be promoted to a vector type");
4888 APInt StepVal = cast<ConstantSDNode>(N->getOperand(0))->getAPIntValue();
4889 return DAG.getStepVector(dl, NOutVT,
4890 StepVal.sext(NOutVT.getScalarSizeInBits()));
4893 SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) {
4894 SDLoc dl(N);
4896 EVT OutVT = N->getValueType(0);
4897 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4898 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4900 EVT OutElemTy = NOutVT.getVectorElementType();
4902 unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements();
4903 unsigned NumOutElem = NOutVT.getVectorNumElements();
4904 unsigned NumOperands = N->getNumOperands();
4905 assert(NumElem * NumOperands == NumOutElem &&
4906 "Unexpected number of elements");
4908 // Take the elements from the first vector.
4909 SmallVector<SDValue, 8> Ops(NumOutElem);
4910 for (unsigned i = 0; i < NumOperands; ++i) {
4911 SDValue Op = N->getOperand(i);
4912 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger)
4913 Op = GetPromotedInteger(Op);
4914 EVT SclrTy = Op.getValueType().getVectorElementType();
4915 assert(NumElem == Op.getValueType().getVectorNumElements() &&
4916 "Unexpected number of elements");
4918 for (unsigned j = 0; j < NumElem; ++j) {
4919 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Op,
4920 DAG.getVectorIdxConstant(j, dl));
4921 Ops[i * NumElem + j] = DAG.getAnyExtOrTrunc(Ext, dl, OutElemTy);
4925 return DAG.getBuildVector(NOutVT, dl, Ops);
4928 SDValue DAGTypeLegalizer::PromoteIntRes_EXTEND_VECTOR_INREG(SDNode *N) {
4929 EVT VT = N->getValueType(0);
4930 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4931 assert(NVT.isVector() && "This type must be promoted to a vector type");
4933 SDLoc dl(N);
4935 // For operands whose TypeAction is to promote, extend the promoted node
4936 // appropriately (ZERO_EXTEND or SIGN_EXTEND) from the original pre-promotion
4937 // type, and then construct a new *_EXTEND_VECTOR_INREG node to the promote-to
4938 // type..
4939 if (getTypeAction(N->getOperand(0).getValueType())
4940 == TargetLowering::TypePromoteInteger) {
4941 SDValue Promoted;
4943 switch(N->getOpcode()) {
4944 case ISD::SIGN_EXTEND_VECTOR_INREG:
4945 Promoted = SExtPromotedInteger(N->getOperand(0));
4946 break;
4947 case ISD::ZERO_EXTEND_VECTOR_INREG:
4948 Promoted = ZExtPromotedInteger(N->getOperand(0));
4949 break;
4950 case ISD::ANY_EXTEND_VECTOR_INREG:
4951 Promoted = GetPromotedInteger(N->getOperand(0));
4952 break;
4953 default:
4954 llvm_unreachable("Node has unexpected Opcode");
4956 return DAG.getNode(N->getOpcode(), dl, NVT, Promoted);
4959 // Directly extend to the appropriate transform-to type.
4960 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
4963 SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
4964 EVT OutVT = N->getValueType(0);
4965 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4966 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4968 EVT NOutVTElem = NOutVT.getVectorElementType();
4970 SDLoc dl(N);
4971 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4973 SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
4974 NOutVTElem, N->getOperand(1));
4975 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT,
4976 V0, ConvElem, N->getOperand(2));
4979 SDValue DAGTypeLegalizer::PromoteIntRes_VECREDUCE(SDNode *N) {
4980 // The VECREDUCE result size may be larger than the element size, so
4981 // we can simply change the result type.
4982 SDLoc dl(N);
4983 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4984 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
4987 SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
4988 SDLoc dl(N);
4989 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4990 SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl,
4991 TLI.getVectorIdxTy(DAG.getDataLayout()));
4992 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
4993 V0->getValueType(0).getScalarType(), V0, V1);
4995 // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
4996 // element types. If this is the case then we need to expand the outgoing
4997 // value and not truncate it.
4998 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
5001 SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_SUBVECTOR(SDNode *N) {
5002 SDLoc dl(N);
5003 SDValue V0 = GetPromotedInteger(N->getOperand(0));
5004 MVT InVT = V0.getValueType().getSimpleVT();
5005 MVT OutVT = MVT::getVectorVT(InVT.getVectorElementType(),
5006 N->getValueType(0).getVectorNumElements());
5007 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, V0, N->getOperand(1));
5008 return DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), Ext);
5011 SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
5012 SDLoc dl(N);
5014 EVT ResVT = N->getValueType(0);
5015 unsigned NumElems = N->getNumOperands();
5017 if (ResVT.isScalableVector()) {
5018 SDValue ResVec = DAG.getUNDEF(ResVT);
5020 for (unsigned OpIdx = 0; OpIdx < NumElems; ++OpIdx) {
5021 SDValue Op = N->getOperand(OpIdx);
5022 unsigned OpNumElts = Op.getValueType().getVectorMinNumElements();
5023 ResVec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, ResVec, Op,
5024 DAG.getIntPtrConstant(OpIdx * OpNumElts, dl));
5027 return ResVec;
5030 EVT RetSclrTy = N->getValueType(0).getVectorElementType();
5032 SmallVector<SDValue, 8> NewOps;
5033 NewOps.reserve(NumElems);
5035 // For each incoming vector
5036 for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) {
5037 SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
5038 EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
5039 unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
5041 for (unsigned i=0; i<NumElem; ++i) {
5042 // Extract element from incoming vector
5043 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Incoming,
5044 DAG.getVectorIdxConstant(i, dl));
5045 SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
5046 NewOps.push_back(Tr);
5050 return DAG.getBuildVector(N->getValueType(0), dl, NewOps);