1 //===----- LegalizeIntegerTypes.cpp - Legalization of integer types -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements integer type expansion and promotion for LegalizeTypes.
11 // Promotion is the act of changing a computation in an illegal type into a
12 // computation in a larger type. For example, implementing i8 arithmetic in an
13 // i32 register (often needed on powerpc).
14 // Expansion is the act of changing a computation in an illegal type into a
15 // computation in two identical registers of a smaller type. For example,
16 // implementing i64 arithmetic in two i32 registers (often needed on 32-bit
19 //===----------------------------------------------------------------------===//
21 #include "LegalizeTypes.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
27 //===----------------------------------------------------------------------===//
28 // Integer Result Promotion
29 //===----------------------------------------------------------------------===//
31 /// PromoteIntegerResult - This method is called when a result of a node is
32 /// found to be in need of promotion to a larger type. At this point, the node
33 /// may also have invalid operands or may have other results that need
34 /// expansion, we just know that (at least) one result needs promotion.
35 void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
36 DEBUG(dbgs() << "Promote integer result: "; N->dump(&DAG); dbgs() << "\n");
37 SDValue Res = SDValue();
39 // See if the target wants to custom expand this node.
40 if (CustomLowerNode(N, N->getValueType(ResNo), true))
43 switch (N->getOpcode()) {
46 dbgs() << "PromoteIntegerResult #" << ResNo << ": ";
47 N->dump(&DAG); dbgs() << "\n";
49 llvm_unreachable("Do not know how to promote this operator!");
50 case ISD::MERGE_VALUES:Res = PromoteIntRes_MERGE_VALUES(N, ResNo); break;
51 case ISD::AssertSext: Res = PromoteIntRes_AssertSext(N); break;
52 case ISD::AssertZext: Res = PromoteIntRes_AssertZext(N); break;
53 case ISD::BITCAST: Res = PromoteIntRes_BITCAST(N); break;
54 case ISD::BSWAP: Res = PromoteIntRes_BSWAP(N); break;
55 case ISD::BUILD_PAIR: Res = PromoteIntRes_BUILD_PAIR(N); break;
56 case ISD::Constant: Res = PromoteIntRes_Constant(N); break;
57 case ISD::CONVERT_RNDSAT:
58 Res = PromoteIntRes_CONVERT_RNDSAT(N); break;
59 case ISD::CTLZ_ZERO_UNDEF:
60 case ISD::CTLZ: Res = PromoteIntRes_CTLZ(N); break;
61 case ISD::CTPOP: Res = PromoteIntRes_CTPOP(N); break;
62 case ISD::CTTZ_ZERO_UNDEF:
63 case ISD::CTTZ: Res = PromoteIntRes_CTTZ(N); break;
64 case ISD::EXTRACT_VECTOR_ELT:
65 Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
66 case ISD::LOAD: Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N));break;
67 case ISD::SELECT: Res = PromoteIntRes_SELECT(N); break;
68 case ISD::VSELECT: Res = PromoteIntRes_VSELECT(N); break;
69 case ISD::SELECT_CC: Res = PromoteIntRes_SELECT_CC(N); break;
70 case ISD::SETCC: Res = PromoteIntRes_SETCC(N); break;
71 case ISD::SHL: Res = PromoteIntRes_SHL(N); break;
72 case ISD::SIGN_EXTEND_INREG:
73 Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
74 case ISD::SRA: Res = PromoteIntRes_SRA(N); break;
75 case ISD::SRL: Res = PromoteIntRes_SRL(N); break;
76 case ISD::TRUNCATE: Res = PromoteIntRes_TRUNCATE(N); break;
77 case ISD::UNDEF: Res = PromoteIntRes_UNDEF(N); break;
78 case ISD::VAARG: Res = PromoteIntRes_VAARG(N); break;
80 case ISD::EXTRACT_SUBVECTOR:
81 Res = PromoteIntRes_EXTRACT_SUBVECTOR(N); break;
82 case ISD::VECTOR_SHUFFLE:
83 Res = PromoteIntRes_VECTOR_SHUFFLE(N); break;
84 case ISD::INSERT_VECTOR_ELT:
85 Res = PromoteIntRes_INSERT_VECTOR_ELT(N); break;
86 case ISD::BUILD_VECTOR:
87 Res = PromoteIntRes_BUILD_VECTOR(N); break;
88 case ISD::SCALAR_TO_VECTOR:
89 Res = PromoteIntRes_SCALAR_TO_VECTOR(N); break;
90 case ISD::CONCAT_VECTORS:
91 Res = PromoteIntRes_CONCAT_VECTORS(N); break;
93 case ISD::SIGN_EXTEND:
94 case ISD::ZERO_EXTEND:
95 case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break;
98 case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break;
100 case ISD::FP32_TO_FP16:Res = PromoteIntRes_FP32_TO_FP16(N); break;
107 case ISD::MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break;
110 case ISD::SREM: Res = PromoteIntRes_SDIV(N); break;
113 case ISD::UREM: Res = PromoteIntRes_UDIV(N); break;
116 case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
118 case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
120 case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;
122 case ISD::ATOMIC_LOAD:
123 Res = PromoteIntRes_Atomic0(cast<AtomicSDNode>(N)); break;
125 case ISD::ATOMIC_LOAD_ADD:
126 case ISD::ATOMIC_LOAD_SUB:
127 case ISD::ATOMIC_LOAD_AND:
128 case ISD::ATOMIC_LOAD_OR:
129 case ISD::ATOMIC_LOAD_XOR:
130 case ISD::ATOMIC_LOAD_NAND:
131 case ISD::ATOMIC_LOAD_MIN:
132 case ISD::ATOMIC_LOAD_MAX:
133 case ISD::ATOMIC_LOAD_UMIN:
134 case ISD::ATOMIC_LOAD_UMAX:
135 case ISD::ATOMIC_SWAP:
136 Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
138 case ISD::ATOMIC_CMP_SWAP:
139 Res = PromoteIntRes_Atomic2(cast<AtomicSDNode>(N)); break;
142 // If the result is null then the sub-method took care of registering it.
144 SetPromotedInteger(SDValue(N, ResNo), Res);
147 SDValue DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode *N,
149 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
150 return GetPromotedInteger(Op);
153 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
154 // Sign-extend the new bits, and continue the assertion.
155 SDValue Op = SExtPromotedInteger(N->getOperand(0));
156 return DAG.getNode(ISD::AssertSext, SDLoc(N),
157 Op.getValueType(), Op, N->getOperand(1));
160 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
161 // Zero the new bits, and continue the assertion.
162 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
163 return DAG.getNode(ISD::AssertZext, SDLoc(N),
164 Op.getValueType(), Op, N->getOperand(1));
167 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode *N) {
168 EVT ResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
169 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
170 N->getMemoryVT(), ResVT,
171 N->getChain(), N->getBasePtr(),
172 N->getMemOperand(), N->getOrdering(),
174 // Legalized the chain result - switch anything that used the old chain to
176 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
180 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
181 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
182 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
184 N->getChain(), N->getBasePtr(),
185 Op2, N->getMemOperand(), N->getOrdering(),
187 // Legalized the chain result - switch anything that used the old chain to
189 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
193 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic2(AtomicSDNode *N) {
194 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
195 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
196 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
197 N->getMemoryVT(), N->getChain(), N->getBasePtr(),
198 Op2, Op3, N->getMemOperand(), N->getOrdering(),
200 // Legalized the chain result - switch anything that used the old chain to
202 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
206 SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
207 SDValue InOp = N->getOperand(0);
208 EVT InVT = InOp.getValueType();
209 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
210 EVT OutVT = N->getValueType(0);
211 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
214 switch (getTypeAction(InVT)) {
215 case TargetLowering::TypeLegal:
217 case TargetLowering::TypePromoteInteger:
218 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector() && !NInVT.isVector())
219 // The input promotes to the same size. Convert the promoted value.
220 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp));
222 case TargetLowering::TypeSoftenFloat:
223 // Promote the integer operand by hand.
224 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
225 case TargetLowering::TypeExpandInteger:
226 case TargetLowering::TypeExpandFloat:
228 case TargetLowering::TypeScalarizeVector:
229 // Convert the element to an integer and promote it by hand.
230 if (!NOutVT.isVector())
231 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
232 BitConvertToInteger(GetScalarizedVector(InOp)));
234 case TargetLowering::TypeSplitVector: {
235 // For example, i32 = BITCAST v2i16 on alpha. Convert the split
236 // pieces of the input into integers and reassemble in the final type.
238 GetSplitVector(N->getOperand(0), Lo, Hi);
239 Lo = BitConvertToInteger(Lo);
240 Hi = BitConvertToInteger(Hi);
242 if (TLI.isBigEndian())
245 InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
246 EVT::getIntegerVT(*DAG.getContext(),
247 NOutVT.getSizeInBits()),
248 JoinIntegers(Lo, Hi));
249 return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp);
251 case TargetLowering::TypeWidenVector:
252 // The input is widened to the same size. Convert to the widened value.
253 // Make sure that the outgoing value is not a vector, because this would
254 // make us bitcast between two vectors which are legalized in different ways.
255 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector())
256 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetWidenedVector(InOp));
259 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
260 CreateStackStoreLoad(InOp, OutVT));
263 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
264 SDValue Op = GetPromotedInteger(N->getOperand(0));
265 EVT OVT = N->getValueType(0);
266 EVT NVT = Op.getValueType();
269 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
270 return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
271 DAG.getConstant(DiffBits, TLI.getPointerTy()));
274 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
275 // The pair element type may be legal, or may not promote to the same type as
276 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
277 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N),
278 TLI.getTypeToTransformTo(*DAG.getContext(),
279 N->getValueType(0)), JoinIntegers(N->getOperand(0),
283 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
284 EVT VT = N->getValueType(0);
285 // FIXME there is no actual debug info here
287 // Zero extend things like i1, sign extend everything else. It shouldn't
288 // matter in theory which one we pick, but this tends to give better code?
289 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
290 SDValue Result = DAG.getNode(Opc, dl,
291 TLI.getTypeToTransformTo(*DAG.getContext(), VT),
293 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
297 SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) {
298 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
299 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
300 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
301 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
302 "can only promote integers");
303 EVT OutVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
304 return DAG.getConvertRndSat(OutVT, SDLoc(N), N->getOperand(0),
305 N->getOperand(1), N->getOperand(2),
306 N->getOperand(3), N->getOperand(4), CvtCode);
309 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
310 // Zero extend to the promoted type and do the count there.
311 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
313 EVT OVT = N->getValueType(0);
314 EVT NVT = Op.getValueType();
315 Op = DAG.getNode(N->getOpcode(), dl, NVT, Op);
316 // Subtract off the extra leading bits in the bigger type.
317 return DAG.getNode(ISD::SUB, dl, NVT, Op,
318 DAG.getConstant(NVT.getSizeInBits() -
319 OVT.getSizeInBits(), NVT));
322 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
323 // Zero extend to the promoted type and do the count there.
324 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
325 return DAG.getNode(ISD::CTPOP, SDLoc(N), Op.getValueType(), Op);
328 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
329 SDValue Op = GetPromotedInteger(N->getOperand(0));
330 EVT OVT = N->getValueType(0);
331 EVT NVT = Op.getValueType();
333 if (N->getOpcode() == ISD::CTTZ) {
334 // The count is the same in the promoted type except if the original
335 // value was zero. This can be handled by setting the bit just off
336 // the top of the original type.
337 APInt TopBit(NVT.getSizeInBits(), 0);
338 TopBit.setBit(OVT.getSizeInBits());
339 Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, NVT));
341 return DAG.getNode(N->getOpcode(), dl, NVT, Op);
344 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
346 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
347 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, N->getOperand(0),
351 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
352 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
353 unsigned NewOpc = N->getOpcode();
356 // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
357 // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT
358 // and SINT conversions are Custom, there is no way to tell which is
359 // preferable. We choose SINT because that's the right thing on PPC.)
360 if (N->getOpcode() == ISD::FP_TO_UINT &&
361 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
362 TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
363 NewOpc = ISD::FP_TO_SINT;
365 SDValue Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
367 // Assert that the converted value fits in the original type. If it doesn't
368 // (eg: because the value being converted is too big), then the result of the
369 // original operation was undefined anyway, so the assert is still correct.
370 return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ?
371 ISD::AssertZext : ISD::AssertSext, dl, NVT, Res,
372 DAG.getValueType(N->getValueType(0).getScalarType()));
375 SDValue DAGTypeLegalizer::PromoteIntRes_FP32_TO_FP16(SDNode *N) {
376 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
379 SDValue Res = DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
381 return DAG.getNode(ISD::AssertZext, dl,
382 NVT, Res, DAG.getValueType(N->getValueType(0)));
385 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
386 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
389 if (getTypeAction(N->getOperand(0).getValueType())
390 == TargetLowering::TypePromoteInteger) {
391 SDValue Res = GetPromotedInteger(N->getOperand(0));
392 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
394 // If the result and operand types are the same after promotion, simplify
395 // to an in-register extension.
396 if (NVT == Res.getValueType()) {
397 // The high bits are not guaranteed to be anything. Insert an extend.
398 if (N->getOpcode() == ISD::SIGN_EXTEND)
399 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
400 DAG.getValueType(N->getOperand(0).getValueType()));
401 if (N->getOpcode() == ISD::ZERO_EXTEND)
402 return DAG.getZeroExtendInReg(Res, dl,
403 N->getOperand(0).getValueType().getScalarType());
404 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
409 // Otherwise, just extend the original operand all the way to the larger type.
410 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
413 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
414 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
415 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
416 ISD::LoadExtType ExtType =
417 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
419 SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
421 N->getMemoryVT(), N->isVolatile(),
422 N->isNonTemporal(), N->getAlignment());
424 // Legalized the chain result - switch anything that used the old chain to
426 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
430 /// Promote the overflow flag of an overflowing arithmetic node.
431 SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
432 // Simply change the return type of the boolean result.
433 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
434 EVT ValueVTs[] = { N->getValueType(0), NVT };
435 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
436 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N),
437 DAG.getVTList(ValueVTs, 2), Ops, 2);
439 // Modified the sum result - switch anything that used the old sum to use
441 ReplaceValueWith(SDValue(N, 0), Res);
443 return SDValue(Res.getNode(), 1);
446 SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
448 return PromoteIntRes_Overflow(N);
450 // The operation overflowed iff the result in the larger type is not the
451 // sign extension of its truncation to the original type.
452 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
453 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
454 EVT OVT = N->getOperand(0).getValueType();
455 EVT NVT = LHS.getValueType();
458 // Do the arithmetic in the larger type.
459 unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
460 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
462 // Calculate the overflow flag: sign extend the arithmetic result from
463 // the original type.
464 SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
465 DAG.getValueType(OVT));
466 // Overflowed if and only if this is not equal to Res.
467 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
469 // Use the calculated overflow everywhere.
470 ReplaceValueWith(SDValue(N, 1), Ofl);
475 SDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) {
476 // Sign extend the input.
477 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
478 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
479 return DAG.getNode(N->getOpcode(), SDLoc(N),
480 LHS.getValueType(), LHS, RHS);
483 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
484 SDValue LHS = GetPromotedInteger(N->getOperand(1));
485 SDValue RHS = GetPromotedInteger(N->getOperand(2));
486 return DAG.getSelect(SDLoc(N),
487 LHS.getValueType(), N->getOperand(0), LHS, RHS);
490 SDValue DAGTypeLegalizer::PromoteIntRes_VSELECT(SDNode *N) {
491 SDValue Mask = N->getOperand(0);
492 EVT OpTy = N->getOperand(1).getValueType();
494 // Promote all the way up to the canonical SetCC type.
495 Mask = PromoteTargetBoolean(Mask, getSetCCResultType(OpTy));
496 SDValue LHS = GetPromotedInteger(N->getOperand(1));
497 SDValue RHS = GetPromotedInteger(N->getOperand(2));
498 return DAG.getNode(ISD::VSELECT, SDLoc(N),
499 LHS.getValueType(), Mask, LHS, RHS);
502 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
503 SDValue LHS = GetPromotedInteger(N->getOperand(2));
504 SDValue RHS = GetPromotedInteger(N->getOperand(3));
505 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
506 LHS.getValueType(), N->getOperand(0),
507 N->getOperand(1), LHS, RHS, N->getOperand(4));
510 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
511 EVT SVT = getSetCCResultType(N->getOperand(0).getValueType());
513 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
515 // Only use the result of getSetCCResultType if it is legal,
516 // otherwise just use the promoted result type (NVT).
517 if (!TLI.isTypeLegal(SVT))
521 assert(SVT.isVector() == N->getOperand(0).getValueType().isVector() &&
522 "Vector compare must return a vector result!");
524 SDValue LHS = N->getOperand(0);
525 SDValue RHS = N->getOperand(1);
526 if (LHS.getValueType() != RHS.getValueType()) {
527 if (getTypeAction(LHS.getValueType()) == TargetLowering::TypePromoteInteger &&
528 !LHS.getValueType().isVector())
529 LHS = GetPromotedInteger(LHS);
530 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger &&
531 !RHS.getValueType().isVector())
532 RHS = GetPromotedInteger(RHS);
535 // Get the SETCC result using the canonical SETCC type.
536 SDValue SetCC = DAG.getNode(N->getOpcode(), dl, SVT, LHS, RHS,
539 assert(NVT.bitsLE(SVT) && "Integer type overpromoted?");
540 // Convert to the expected type.
541 return DAG.getNode(ISD::TRUNCATE, dl, NVT, SetCC);
544 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
545 SDValue Res = GetPromotedInteger(N->getOperand(0));
546 SDValue Amt = N->getOperand(1);
547 Amt = Amt.getValueType().isVector() ? ZExtPromotedInteger(Amt) : Amt;
548 return DAG.getNode(ISD::SHL, SDLoc(N), Res.getValueType(), Res, Amt);
551 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
552 SDValue Op = GetPromotedInteger(N->getOperand(0));
553 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N),
554 Op.getValueType(), Op, N->getOperand(1));
557 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
558 // The input may have strange things in the top bits of the registers, but
559 // these operations don't care. They may have weird bits going out, but
560 // that too is okay if they are integer operations.
561 SDValue LHS = GetPromotedInteger(N->getOperand(0));
562 SDValue RHS = GetPromotedInteger(N->getOperand(1));
563 return DAG.getNode(N->getOpcode(), SDLoc(N),
564 LHS.getValueType(), LHS, RHS);
567 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
568 // The input value must be properly sign extended.
569 SDValue Res = SExtPromotedInteger(N->getOperand(0));
570 SDValue Amt = N->getOperand(1);
571 Amt = Amt.getValueType().isVector() ? ZExtPromotedInteger(Amt) : Amt;
572 return DAG.getNode(ISD::SRA, SDLoc(N), Res.getValueType(), Res, Amt);
575 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
576 // The input value must be properly zero extended.
577 SDValue Res = ZExtPromotedInteger(N->getOperand(0));
578 SDValue Amt = N->getOperand(1);
579 Amt = Amt.getValueType().isVector() ? ZExtPromotedInteger(Amt) : Amt;
580 return DAG.getNode(ISD::SRL, SDLoc(N), Res.getValueType(), Res, Amt);
583 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
584 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
586 SDValue InOp = N->getOperand(0);
589 switch (getTypeAction(InOp.getValueType())) {
590 default: llvm_unreachable("Unknown type action!");
591 case TargetLowering::TypeLegal:
592 case TargetLowering::TypeExpandInteger:
595 case TargetLowering::TypePromoteInteger:
596 Res = GetPromotedInteger(InOp);
598 case TargetLowering::TypeSplitVector:
599 EVT InVT = InOp.getValueType();
600 assert(InVT.isVector() && "Cannot split scalar types");
601 unsigned NumElts = InVT.getVectorNumElements();
602 assert(NumElts == NVT.getVectorNumElements() &&
603 "Dst and Src must have the same number of elements");
604 assert(isPowerOf2_32(NumElts) &&
605 "Promoted vector type must be a power of two");
608 GetSplitVector(InOp, EOp1, EOp2);
610 EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(),
612 EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1);
613 EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2);
615 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2);
618 // Truncate to NVT instead of VT
619 return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res);
622 SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
624 return PromoteIntRes_Overflow(N);
626 // The operation overflowed iff the result in the larger type is not the
627 // zero extension of its truncation to the original type.
628 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
629 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
630 EVT OVT = N->getOperand(0).getValueType();
631 EVT NVT = LHS.getValueType();
634 // Do the arithmetic in the larger type.
635 unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
636 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
638 // Calculate the overflow flag: zero extend the arithmetic result from
639 // the original type.
640 SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT);
641 // Overflowed if and only if this is not equal to Res.
642 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
644 // Use the calculated overflow everywhere.
645 ReplaceValueWith(SDValue(N, 1), Ofl);
650 SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
651 // Promote the overflow bit trivially.
653 return PromoteIntRes_Overflow(N);
655 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
657 EVT SmallVT = LHS.getValueType();
659 // To determine if the result overflowed in a larger type, we extend the
660 // input to the larger type, do the multiply (checking if it overflows),
661 // then also check the high bits of the result to see if overflow happened
663 if (N->getOpcode() == ISD::SMULO) {
664 LHS = SExtPromotedInteger(LHS);
665 RHS = SExtPromotedInteger(RHS);
667 LHS = ZExtPromotedInteger(LHS);
668 RHS = ZExtPromotedInteger(RHS);
670 SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1));
671 SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS);
673 // Overflow occurred if it occurred in the larger type, or if the high part
674 // of the result does not zero/sign-extend the low part. Check this second
675 // possibility first.
677 if (N->getOpcode() == ISD::UMULO) {
678 // Unsigned overflow occurred if the high part is non-zero.
679 SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
680 DAG.getIntPtrConstant(SmallVT.getSizeInBits()));
681 Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
682 DAG.getConstant(0, Hi.getValueType()), ISD::SETNE);
684 // Signed overflow occurred if the high part does not sign extend the low.
685 SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
686 Mul, DAG.getValueType(SmallVT));
687 Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
690 // The only other way for overflow to occur is if the multiplication in the
691 // larger type itself overflowed.
692 Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow,
693 SDValue(Mul.getNode(), 1));
695 // Use the calculated overflow everywhere.
696 ReplaceValueWith(SDValue(N, 1), Overflow);
700 SDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) {
701 // Zero extend the input.
702 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
703 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
704 return DAG.getNode(N->getOpcode(), SDLoc(N),
705 LHS.getValueType(), LHS, RHS);
708 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
709 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
710 N->getValueType(0)));
713 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
714 SDValue Chain = N->getOperand(0); // Get the chain.
715 SDValue Ptr = N->getOperand(1); // Get the pointer.
716 EVT VT = N->getValueType(0);
719 MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
720 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
721 // The argument is passed as NumRegs registers of type RegVT.
723 SmallVector<SDValue, 8> Parts(NumRegs);
724 for (unsigned i = 0; i < NumRegs; ++i) {
725 Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
726 N->getConstantOperandVal(3));
727 Chain = Parts[i].getValue(1);
730 // Handle endianness of the load.
731 if (TLI.isBigEndian())
732 std::reverse(Parts.begin(), Parts.end());
734 // Assemble the parts in the promoted type.
735 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
736 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
737 for (unsigned i = 1; i < NumRegs; ++i) {
738 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
739 // Shift it to the right position and "or" it in.
740 Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
741 DAG.getConstant(i * RegVT.getSizeInBits(),
742 TLI.getPointerTy()));
743 Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
746 // Modified the chain result - switch anything that used the old chain to
748 ReplaceValueWith(SDValue(N, 1), Chain);
753 //===----------------------------------------------------------------------===//
754 // Integer Operand Promotion
755 //===----------------------------------------------------------------------===//
757 /// PromoteIntegerOperand - This method is called when the specified operand of
758 /// the specified node is found to need promotion. At this point, all of the
759 /// result types of the node are known to be legal, but other operands of the
760 /// node may need promotion or expansion as well as the specified one.
761 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
762 DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG); dbgs() << "\n");
763 SDValue Res = SDValue();
765 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
768 switch (N->getOpcode()) {
771 dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
772 N->dump(&DAG); dbgs() << "\n";
774 llvm_unreachable("Do not know how to promote this operator's operand!");
776 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
777 case ISD::ATOMIC_STORE:
778 Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
780 case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break;
781 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
782 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
783 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
784 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
785 case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
786 case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
787 case ISD::CONVERT_RNDSAT:
788 Res = PromoteIntOp_CONVERT_RNDSAT(N); break;
789 case ISD::INSERT_VECTOR_ELT:
790 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
791 case ISD::SCALAR_TO_VECTOR:
792 Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break;
794 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
795 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
796 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
797 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
798 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
799 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
801 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
802 case ISD::FP16_TO_FP32:
803 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
804 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
810 case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
813 // If the result is null, the sub-method took care of registering results etc.
814 if (!Res.getNode()) return false;
816 // If the result is N, the sub-method updated N in place. Tell the legalizer
818 if (Res.getNode() == N)
821 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
822 "Invalid operand expansion");
824 ReplaceValueWith(SDValue(N, 0), Res);
828 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
829 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
830 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
831 ISD::CondCode CCCode) {
832 // We have to insert explicit sign or zero extends. Note that we could
833 // insert sign extends for ALL conditions, but zero extend is cheaper on
834 // many machines (an AND instead of two shifts), so prefer it.
836 default: llvm_unreachable("Unknown integer comparison!");
843 // ALL of these operations will work if we either sign or zero extend
844 // the operands (including the unsigned comparisons!). Zero extend is
845 // usually a simpler/cheaper operation, so prefer it.
846 NewLHS = ZExtPromotedInteger(NewLHS);
847 NewRHS = ZExtPromotedInteger(NewRHS);
853 NewLHS = SExtPromotedInteger(NewLHS);
854 NewRHS = SExtPromotedInteger(NewRHS);
859 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
860 SDValue Op = GetPromotedInteger(N->getOperand(0));
861 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op);
864 SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
865 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
866 return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(),
867 N->getChain(), N->getBasePtr(), Op2, N->getMemOperand(),
868 N->getOrdering(), N->getSynchScope());
871 SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
872 // This should only occur in unusual situations like bitcasting to an
873 // x86_fp80, so just turn it into a store+load
874 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
877 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
878 assert(OpNo == 2 && "Don't know how to promote this operand!");
880 SDValue LHS = N->getOperand(2);
881 SDValue RHS = N->getOperand(3);
882 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
884 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
886 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
887 N->getOperand(1), LHS, RHS, N->getOperand(4)),
891 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
892 assert(OpNo == 1 && "only know how to promote condition");
894 // Promote all the way up to the canonical SetCC type.
895 EVT SVT = getSetCCResultType(MVT::Other);
896 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), SVT);
898 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
899 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
900 N->getOperand(2)), 0);
903 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
904 // Since the result type is legal, the operands must promote to it.
905 EVT OVT = N->getOperand(0).getValueType();
906 SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
907 SDValue Hi = GetPromotedInteger(N->getOperand(1));
908 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
911 Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
912 DAG.getConstant(OVT.getSizeInBits(), TLI.getPointerTy()));
913 return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
916 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
917 // The vector type is legal but the element type is not. This implies
918 // that the vector is a power-of-two in length and that the element
919 // type does not have a strange size (eg: it is not i1).
920 EVT VecVT = N->getValueType(0);
921 unsigned NumElts = VecVT.getVectorNumElements();
922 assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) &&
923 "Legal vector of one illegal element?");
925 // Promote the inserted value. The type does not need to match the
926 // vector element type. Check that any extra bits introduced will be
928 assert(N->getOperand(0).getValueType().getSizeInBits() >=
929 N->getValueType(0).getVectorElementType().getSizeInBits() &&
930 "Type of inserted value narrower than vector element type!");
932 SmallVector<SDValue, 16> NewOps;
933 for (unsigned i = 0; i < NumElts; ++i)
934 NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
936 return SDValue(DAG.UpdateNodeOperands(N, &NewOps[0], NumElts), 0);
939 SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) {
940 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
941 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
942 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
943 CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) &&
944 "can only promote integer arguments");
945 SDValue InOp = GetPromotedInteger(N->getOperand(0));
946 return DAG.getConvertRndSat(N->getValueType(0), SDLoc(N), InOp,
947 N->getOperand(1), N->getOperand(2),
948 N->getOperand(3), N->getOperand(4), CvtCode);
951 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
954 // Promote the inserted value. This is valid because the type does not
955 // have to match the vector element type.
957 // Check that any extra bits introduced will be truncated away.
958 assert(N->getOperand(1).getValueType().getSizeInBits() >=
959 N->getValueType(0).getVectorElementType().getSizeInBits() &&
960 "Type of inserted value narrower than vector element type!");
961 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
962 GetPromotedInteger(N->getOperand(1)),
967 assert(OpNo == 2 && "Different operand and result vector types?");
969 // Promote the index.
970 SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N),
971 TLI.getVectorIdxTy());
972 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
973 N->getOperand(1), Idx), 0);
976 SDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) {
977 // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote
978 // the operand in place.
979 return SDValue(DAG.UpdateNodeOperands(N,
980 GetPromotedInteger(N->getOperand(0))), 0);
983 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
984 assert(OpNo == 0 && "Only know how to promote the condition!");
985 SDValue Cond = N->getOperand(0);
986 EVT OpTy = N->getOperand(1).getValueType();
988 // Promote all the way up to the canonical SetCC type.
989 EVT SVT = getSetCCResultType(N->getOpcode() == ISD::SELECT ?
990 OpTy.getScalarType() : OpTy);
991 Cond = PromoteTargetBoolean(Cond, SVT);
993 return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1),
994 N->getOperand(2)), 0);
997 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
998 assert(OpNo == 0 && "Don't know how to promote this operand!");
1000 SDValue LHS = N->getOperand(0);
1001 SDValue RHS = N->getOperand(1);
1002 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
1004 // The CC (#4) and the possible return values (#2 and #3) have legal types.
1005 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
1006 N->getOperand(3), N->getOperand(4)), 0);
1009 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
1010 assert(OpNo == 0 && "Don't know how to promote this operand!");
1012 SDValue LHS = N->getOperand(0);
1013 SDValue RHS = N->getOperand(1);
1014 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
1016 // The CC (#2) is always legal.
1017 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
1020 SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
1021 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1022 ZExtPromotedInteger(N->getOperand(1))), 0);
1025 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
1026 SDValue Op = GetPromotedInteger(N->getOperand(0));
1028 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
1029 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
1030 Op, DAG.getValueType(N->getOperand(0).getValueType()));
1033 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
1034 return SDValue(DAG.UpdateNodeOperands(N,
1035 SExtPromotedInteger(N->getOperand(0))), 0);
1038 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
1039 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1040 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
1041 unsigned Alignment = N->getAlignment();
1042 bool isVolatile = N->isVolatile();
1043 bool isNonTemporal = N->isNonTemporal();
1046 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
1048 // Truncate the value and store the result.
1049 return DAG.getTruncStore(Ch, dl, Val, Ptr, N->getPointerInfo(),
1051 isVolatile, isNonTemporal, Alignment);
1054 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
1055 SDValue Op = GetPromotedInteger(N->getOperand(0));
1056 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op);
1059 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
1060 return SDValue(DAG.UpdateNodeOperands(N,
1061 ZExtPromotedInteger(N->getOperand(0))), 0);
1064 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
1066 SDValue Op = GetPromotedInteger(N->getOperand(0));
1067 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
1068 return DAG.getZeroExtendInReg(Op, dl,
1069 N->getOperand(0).getValueType().getScalarType());
1073 //===----------------------------------------------------------------------===//
1074 // Integer Result Expansion
1075 //===----------------------------------------------------------------------===//
1077 /// ExpandIntegerResult - This method is called when the specified result of the
1078 /// specified node is found to need expansion. At this point, the node may also
1079 /// have invalid operands or may have other results that need promotion, we just
1080 /// know that (at least) one result needs expansion.
1081 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
1082 DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG); dbgs() << "\n");
1084 Lo = Hi = SDValue();
1086 // See if the target wants to custom expand this node.
1087 if (CustomLowerNode(N, N->getValueType(ResNo), true))
1090 switch (N->getOpcode()) {
1093 dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
1094 N->dump(&DAG); dbgs() << "\n";
1096 llvm_unreachable("Do not know how to expand the result of this operator!");
1098 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
1099 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
1100 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
1101 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
1103 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
1104 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
1105 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
1106 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
1107 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
1109 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
1110 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
1111 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
1112 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
1113 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
1114 case ISD::CTLZ_ZERO_UNDEF:
1115 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
1116 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
1117 case ISD::CTTZ_ZERO_UNDEF:
1118 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
1119 case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
1120 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
1121 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
1122 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
1123 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
1124 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
1125 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
1126 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
1127 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
1128 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
1129 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
1130 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
1131 case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
1133 case ISD::ATOMIC_LOAD_ADD:
1134 case ISD::ATOMIC_LOAD_SUB:
1135 case ISD::ATOMIC_LOAD_AND:
1136 case ISD::ATOMIC_LOAD_OR:
1137 case ISD::ATOMIC_LOAD_XOR:
1138 case ISD::ATOMIC_LOAD_NAND:
1139 case ISD::ATOMIC_LOAD_MIN:
1140 case ISD::ATOMIC_LOAD_MAX:
1141 case ISD::ATOMIC_LOAD_UMIN:
1142 case ISD::ATOMIC_LOAD_UMAX:
1143 case ISD::ATOMIC_SWAP:
1144 case ISD::ATOMIC_CMP_SWAP: {
1145 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
1146 SplitInteger(Tmp.first, Lo, Hi);
1147 ReplaceValueWith(SDValue(N, 1), Tmp.second);
1153 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
1156 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
1159 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
1162 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
1166 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
1169 case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
1171 case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
1173 case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
1176 // If Lo/Hi is null, the sub-method took care of registering results etc.
1178 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
1181 /// Lower an atomic node to the appropriate builtin call.
1182 std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
1183 unsigned Opc = Node->getOpcode();
1184 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
1189 llvm_unreachable("Unhandled atomic intrinsic Expand!");
1190 case ISD::ATOMIC_SWAP:
1191 switch (VT.SimpleTy) {
1192 default: llvm_unreachable("Unexpected value type for atomic!");
1193 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
1194 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
1195 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
1196 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
1197 case MVT::i128:LC = RTLIB::SYNC_LOCK_TEST_AND_SET_16;break;
1200 case ISD::ATOMIC_CMP_SWAP:
1201 switch (VT.SimpleTy) {
1202 default: llvm_unreachable("Unexpected value type for atomic!");
1203 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
1204 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
1205 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
1206 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
1207 case MVT::i128:LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_16;break;
1210 case ISD::ATOMIC_LOAD_ADD:
1211 switch (VT.SimpleTy) {
1212 default: llvm_unreachable("Unexpected value type for atomic!");
1213 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
1214 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
1215 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
1216 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
1217 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_ADD_16;break;
1220 case ISD::ATOMIC_LOAD_SUB:
1221 switch (VT.SimpleTy) {
1222 default: llvm_unreachable("Unexpected value type for atomic!");
1223 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
1224 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
1225 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
1226 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
1227 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_SUB_16;break;
1230 case ISD::ATOMIC_LOAD_AND:
1231 switch (VT.SimpleTy) {
1232 default: llvm_unreachable("Unexpected value type for atomic!");
1233 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
1234 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
1235 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
1236 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
1237 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_AND_16;break;
1240 case ISD::ATOMIC_LOAD_OR:
1241 switch (VT.SimpleTy) {
1242 default: llvm_unreachable("Unexpected value type for atomic!");
1243 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
1244 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
1245 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
1246 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
1247 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_OR_16;break;
1250 case ISD::ATOMIC_LOAD_XOR:
1251 switch (VT.SimpleTy) {
1252 default: llvm_unreachable("Unexpected value type for atomic!");
1253 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
1254 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
1255 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
1256 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
1257 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_XOR_16;break;
1260 case ISD::ATOMIC_LOAD_NAND:
1261 switch (VT.SimpleTy) {
1262 default: llvm_unreachable("Unexpected value type for atomic!");
1263 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
1264 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
1265 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
1266 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
1267 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_NAND_16;break;
1272 return ExpandChainLibCall(LC, Node, false);
1275 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
1276 /// and the shift amount is a constant 'Amt'. Expand the operation.
1277 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
1278 SDValue &Lo, SDValue &Hi) {
1280 // Expand the incoming operand to be shifted, so that we have its parts
1282 GetExpandedInteger(N->getOperand(0), InL, InH);
1284 EVT NVT = InL.getValueType();
1285 unsigned VTBits = N->getValueType(0).getSizeInBits();
1286 unsigned NVTBits = NVT.getSizeInBits();
1287 EVT ShTy = N->getOperand(1).getValueType();
1289 if (N->getOpcode() == ISD::SHL) {
1291 Lo = Hi = DAG.getConstant(0, NVT);
1292 } else if (Amt > NVTBits) {
1293 Lo = DAG.getConstant(0, NVT);
1294 Hi = DAG.getNode(ISD::SHL, DL,
1295 NVT, InL, DAG.getConstant(Amt-NVTBits, ShTy));
1296 } else if (Amt == NVTBits) {
1297 Lo = DAG.getConstant(0, NVT);
1299 } else if (Amt == 1 &&
1300 TLI.isOperationLegalOrCustom(ISD::ADDC,
1301 TLI.getTypeToExpandTo(*DAG.getContext(), NVT))) {
1302 // Emit this X << 1 as X+X.
1303 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
1304 SDValue LoOps[2] = { InL, InL };
1305 Lo = DAG.getNode(ISD::ADDC, DL, VTList, LoOps, 2);
1306 SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
1307 Hi = DAG.getNode(ISD::ADDE, DL, VTList, HiOps, 3);
1309 Lo = DAG.getNode(ISD::SHL, DL, NVT, InL, DAG.getConstant(Amt, ShTy));
1310 Hi = DAG.getNode(ISD::OR, DL, NVT,
1311 DAG.getNode(ISD::SHL, DL, NVT, InH,
1312 DAG.getConstant(Amt, ShTy)),
1313 DAG.getNode(ISD::SRL, DL, NVT, InL,
1314 DAG.getConstant(NVTBits-Amt, ShTy)));
1319 if (N->getOpcode() == ISD::SRL) {
1321 Lo = DAG.getConstant(0, NVT);
1322 Hi = DAG.getConstant(0, NVT);
1323 } else if (Amt > NVTBits) {
1324 Lo = DAG.getNode(ISD::SRL, DL,
1325 NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
1326 Hi = DAG.getConstant(0, NVT);
1327 } else if (Amt == NVTBits) {
1329 Hi = DAG.getConstant(0, NVT);
1331 Lo = DAG.getNode(ISD::OR, DL, NVT,
1332 DAG.getNode(ISD::SRL, DL, NVT, InL,
1333 DAG.getConstant(Amt, ShTy)),
1334 DAG.getNode(ISD::SHL, DL, NVT, InH,
1335 DAG.getConstant(NVTBits-Amt, ShTy)));
1336 Hi = DAG.getNode(ISD::SRL, DL, NVT, InH, DAG.getConstant(Amt, ShTy));
1341 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1343 Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
1344 DAG.getConstant(NVTBits-1, ShTy));
1345 } else if (Amt > NVTBits) {
1346 Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
1347 DAG.getConstant(Amt-NVTBits, ShTy));
1348 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
1349 DAG.getConstant(NVTBits-1, ShTy));
1350 } else if (Amt == NVTBits) {
1352 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
1353 DAG.getConstant(NVTBits-1, ShTy));
1355 Lo = DAG.getNode(ISD::OR, DL, NVT,
1356 DAG.getNode(ISD::SRL, DL, NVT, InL,
1357 DAG.getConstant(Amt, ShTy)),
1358 DAG.getNode(ISD::SHL, DL, NVT, InH,
1359 DAG.getConstant(NVTBits-Amt, ShTy)));
1360 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, DAG.getConstant(Amt, ShTy));
1364 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1365 /// this shift based on knowledge of the high bit of the shift amount. If we
1366 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1368 bool DAGTypeLegalizer::
1369 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1370 SDValue Amt = N->getOperand(1);
1371 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1372 EVT ShTy = Amt.getValueType();
1373 unsigned ShBits = ShTy.getScalarType().getSizeInBits();
1374 unsigned NVTBits = NVT.getScalarType().getSizeInBits();
1375 assert(isPowerOf2_32(NVTBits) &&
1376 "Expanded integer type size not a power of two!");
1379 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
1380 APInt KnownZero, KnownOne;
1381 DAG.ComputeMaskedBits(N->getOperand(1), KnownZero, KnownOne);
1383 // If we don't know anything about the high bits, exit.
1384 if (((KnownZero|KnownOne) & HighBitMask) == 0)
1387 // Get the incoming operand to be shifted.
1389 GetExpandedInteger(N->getOperand(0), InL, InH);
1391 // If we know that any of the high bits of the shift amount are one, then we
1392 // can do this as a couple of simple shifts.
1393 if (KnownOne.intersects(HighBitMask)) {
1394 // Mask out the high bit, which we know is set.
1395 Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
1396 DAG.getConstant(~HighBitMask, ShTy));
1398 switch (N->getOpcode()) {
1399 default: llvm_unreachable("Unknown shift");
1401 Lo = DAG.getConstant(0, NVT); // Low part is zero.
1402 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
1405 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
1406 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
1409 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
1410 DAG.getConstant(NVTBits-1, ShTy));
1411 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
1416 // If we know that all of the high bits of the shift amount are zero, then we
1417 // can do this as a couple of simple shifts.
1418 if ((KnownZero & HighBitMask) == HighBitMask) {
1419 // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
1420 // shift if x is zero. We can use XOR here because x is known to be smaller
1422 SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
1423 DAG.getConstant(NVTBits-1, ShTy));
1426 switch (N->getOpcode()) {
1427 default: llvm_unreachable("Unknown shift");
1428 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1430 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1433 // When shifting right the arithmetic for Lo and Hi is swapped.
1434 if (N->getOpcode() != ISD::SHL)
1435 std::swap(InL, InH);
1437 // Use a little trick to get the bits that move from Lo to Hi. First
1438 // shift by one bit.
1439 SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, ShTy));
1440 // Then compute the remaining shift with amount-1.
1441 SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
1443 Lo = DAG.getNode(N->getOpcode(), dl, NVT, InL, Amt);
1444 Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
1446 if (N->getOpcode() != ISD::SHL)
1454 /// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
1456 bool DAGTypeLegalizer::
1457 ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1458 SDValue Amt = N->getOperand(1);
1459 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1460 EVT ShTy = Amt.getValueType();
1461 unsigned NVTBits = NVT.getSizeInBits();
1462 assert(isPowerOf2_32(NVTBits) &&
1463 "Expanded integer type size not a power of two!");
1466 // Get the incoming operand to be shifted.
1468 GetExpandedInteger(N->getOperand(0), InL, InH);
1470 SDValue NVBitsNode = DAG.getConstant(NVTBits, ShTy);
1471 SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
1472 SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
1473 SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
1474 Amt, NVBitsNode, ISD::SETULT);
1476 SDValue LoS, HiS, LoL, HiL;
1477 switch (N->getOpcode()) {
1478 default: llvm_unreachable("Unknown shift");
1480 // Short: ShAmt < NVTBits
1481 LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
1482 HiS = DAG.getNode(ISD::OR, dl, NVT,
1483 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
1484 // FIXME: If Amt is zero, the following shift generates an undefined result
1485 // on some architectures.
1486 DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
1488 // Long: ShAmt >= NVTBits
1489 LoL = DAG.getConstant(0, NVT); // Lo part is zero.
1490 HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
1492 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
1493 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
1496 // Short: ShAmt < NVTBits
1497 HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
1498 LoS = DAG.getNode(ISD::OR, dl, NVT,
1499 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
1500 // FIXME: If Amt is zero, the following shift generates an undefined result
1501 // on some architectures.
1502 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
1504 // Long: ShAmt >= NVTBits
1505 HiL = DAG.getConstant(0, NVT); // Hi part is zero.
1506 LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
1508 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
1509 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
1512 // Short: ShAmt < NVTBits
1513 HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
1514 LoS = DAG.getNode(ISD::OR, dl, NVT,
1515 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
1516 // FIXME: If Amt is zero, the following shift generates an undefined result
1517 // on some architectures.
1518 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
1520 // Long: ShAmt >= NVTBits
1521 HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
1522 DAG.getConstant(NVTBits-1, ShTy));
1523 LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
1525 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
1526 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
1531 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
1532 SDValue &Lo, SDValue &Hi) {
1534 // Expand the subcomponents.
1535 SDValue LHSL, LHSH, RHSL, RHSH;
1536 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1537 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1539 EVT NVT = LHSL.getValueType();
1540 SDValue LoOps[2] = { LHSL, RHSL };
1541 SDValue HiOps[3] = { LHSH, RHSH };
1543 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
1544 // them. TODO: Teach operation legalization how to expand unsupported
1545 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
1546 // a carry of type MVT::Glue, but there doesn't seem to be any way to
1547 // generate a value of this type in the expanded code sequence.
1549 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
1550 ISD::ADDC : ISD::SUBC,
1551 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
1554 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
1555 if (N->getOpcode() == ISD::ADD) {
1556 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
1557 HiOps[2] = Lo.getValue(1);
1558 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
1560 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
1561 HiOps[2] = Lo.getValue(1);
1562 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1567 if (N->getOpcode() == ISD::ADD) {
1568 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
1569 Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
1570 SDValue Cmp1 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
1572 SDValue Carry1 = DAG.getSelect(dl, NVT, Cmp1,
1573 DAG.getConstant(1, NVT),
1574 DAG.getConstant(0, NVT));
1575 SDValue Cmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[1],
1577 SDValue Carry2 = DAG.getSelect(dl, NVT, Cmp2,
1578 DAG.getConstant(1, NVT), Carry1);
1579 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
1581 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
1582 Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
1584 DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
1585 LoOps[0], LoOps[1], ISD::SETULT);
1586 SDValue Borrow = DAG.getSelect(dl, NVT, Cmp,
1587 DAG.getConstant(1, NVT),
1588 DAG.getConstant(0, NVT));
1589 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
1593 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
1594 SDValue &Lo, SDValue &Hi) {
1595 // Expand the subcomponents.
1596 SDValue LHSL, LHSH, RHSL, RHSH;
1598 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1599 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1600 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
1601 SDValue LoOps[2] = { LHSL, RHSL };
1602 SDValue HiOps[3] = { LHSH, RHSH };
1604 if (N->getOpcode() == ISD::ADDC) {
1605 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
1606 HiOps[2] = Lo.getValue(1);
1607 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
1609 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
1610 HiOps[2] = Lo.getValue(1);
1611 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1614 // Legalized the flag result - switch anything that used the old flag to
1616 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1619 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
1620 SDValue &Lo, SDValue &Hi) {
1621 // Expand the subcomponents.
1622 SDValue LHSL, LHSH, RHSL, RHSH;
1624 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1625 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1626 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
1627 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
1628 SDValue HiOps[3] = { LHSH, RHSH };
1630 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps, 3);
1631 HiOps[2] = Lo.getValue(1);
1632 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps, 3);
1634 // Legalized the flag result - switch anything that used the old flag to
1636 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1639 void DAGTypeLegalizer::ExpandIntRes_MERGE_VALUES(SDNode *N, unsigned ResNo,
1640 SDValue &Lo, SDValue &Hi) {
1641 SDValue Res = DisintegrateMERGE_VALUES(N, ResNo);
1642 SplitInteger(Res, Lo, Hi);
1645 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
1646 SDValue &Lo, SDValue &Hi) {
1647 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1649 SDValue Op = N->getOperand(0);
1650 if (Op.getValueType().bitsLE(NVT)) {
1651 // The low part is any extension of the input (which degenerates to a copy).
1652 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
1653 Hi = DAG.getUNDEF(NVT); // The high part is undefined.
1655 // For example, extension of an i48 to an i64. The operand type necessarily
1656 // promotes to the result type, so will end up being expanded too.
1657 assert(getTypeAction(Op.getValueType()) ==
1658 TargetLowering::TypePromoteInteger &&
1659 "Only know how to promote this result!");
1660 SDValue Res = GetPromotedInteger(Op);
1661 assert(Res.getValueType() == N->getValueType(0) &&
1662 "Operand over promoted?");
1663 // Split the promoted operand. This will simplify when it is expanded.
1664 SplitInteger(Res, Lo, Hi);
1668 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
1669 SDValue &Lo, SDValue &Hi) {
1671 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1672 EVT NVT = Lo.getValueType();
1673 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1674 unsigned NVTBits = NVT.getSizeInBits();
1675 unsigned EVTBits = EVT.getSizeInBits();
1677 if (NVTBits < EVTBits) {
1678 Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
1679 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
1680 EVTBits - NVTBits)));
1682 Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
1683 // The high part replicates the sign bit of Lo, make it explicit.
1684 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
1685 DAG.getConstant(NVTBits-1, TLI.getPointerTy()));
1689 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
1690 SDValue &Lo, SDValue &Hi) {
1692 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1693 EVT NVT = Lo.getValueType();
1694 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1695 unsigned NVTBits = NVT.getSizeInBits();
1696 unsigned EVTBits = EVT.getSizeInBits();
1698 if (NVTBits < EVTBits) {
1699 Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
1700 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
1701 EVTBits - NVTBits)));
1703 Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
1704 // The high part must be zero, make it explicit.
1705 Hi = DAG.getConstant(0, NVT);
1709 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
1710 SDValue &Lo, SDValue &Hi) {
1712 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
1713 Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
1714 Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
1717 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
1718 SDValue &Lo, SDValue &Hi) {
1719 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1720 unsigned NBitWidth = NVT.getSizeInBits();
1721 const APInt &Cst = cast<ConstantSDNode>(N)->getAPIntValue();
1722 Lo = DAG.getConstant(Cst.trunc(NBitWidth), NVT);
1723 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
1726 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
1727 SDValue &Lo, SDValue &Hi) {
1729 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
1730 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1731 EVT NVT = Lo.getValueType();
1733 SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
1734 DAG.getConstant(0, NVT), ISD::SETNE);
1736 SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
1737 SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, NVT, Hi);
1739 Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
1740 DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
1741 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1742 Hi = DAG.getConstant(0, NVT);
1745 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
1746 SDValue &Lo, SDValue &Hi) {
1748 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
1749 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1750 EVT NVT = Lo.getValueType();
1751 Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
1752 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
1753 Hi = DAG.getConstant(0, NVT);
1756 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
1757 SDValue &Lo, SDValue &Hi) {
1759 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
1760 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1761 EVT NVT = Lo.getValueType();
1763 SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
1764 DAG.getConstant(0, NVT), ISD::SETNE);
1766 SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, NVT, Lo);
1767 SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
1769 Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
1770 DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
1771 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1772 Hi = DAG.getConstant(0, NVT);
1775 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
1778 EVT VT = N->getValueType(0);
1779 SDValue Op = N->getOperand(0);
1780 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
1781 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
1782 SplitInteger(TLI.makeLibCall(DAG, LC, VT, &Op, 1, true/*irrelevant*/,
1787 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
1790 EVT VT = N->getValueType(0);
1791 SDValue Op = N->getOperand(0);
1792 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
1793 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
1794 SplitInteger(TLI.makeLibCall(DAG, LC, VT, &Op, 1, false/*irrelevant*/,
1799 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
1800 SDValue &Lo, SDValue &Hi) {
1801 if (ISD::isNormalLoad(N)) {
1802 ExpandRes_NormalLoad(N, Lo, Hi);
1806 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1808 EVT VT = N->getValueType(0);
1809 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1810 SDValue Ch = N->getChain();
1811 SDValue Ptr = N->getBasePtr();
1812 ISD::LoadExtType ExtType = N->getExtensionType();
1813 unsigned Alignment = N->getAlignment();
1814 bool isVolatile = N->isVolatile();
1815 bool isNonTemporal = N->isNonTemporal();
1816 bool isInvariant = N->isInvariant();
1819 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1821 if (N->getMemoryVT().bitsLE(NVT)) {
1822 EVT MemVT = N->getMemoryVT();
1824 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
1825 MemVT, isVolatile, isNonTemporal, Alignment);
1827 // Remember the chain.
1828 Ch = Lo.getValue(1);
1830 if (ExtType == ISD::SEXTLOAD) {
1831 // The high part is obtained by SRA'ing all but one of the bits of the
1833 unsigned LoSize = Lo.getValueType().getSizeInBits();
1834 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
1835 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
1836 } else if (ExtType == ISD::ZEXTLOAD) {
1837 // The high part is just a zero.
1838 Hi = DAG.getConstant(0, NVT);
1840 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
1841 // The high part is undefined.
1842 Hi = DAG.getUNDEF(NVT);
1844 } else if (TLI.isLittleEndian()) {
1845 // Little-endian - low bits are at low addresses.
1846 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(),
1847 isVolatile, isNonTemporal, isInvariant, Alignment);
1849 unsigned ExcessBits =
1850 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
1851 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
1853 // Increment the pointer to the other half.
1854 unsigned IncrementSize = NVT.getSizeInBits()/8;
1855 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
1856 DAG.getConstant(IncrementSize, Ptr.getValueType()));
1857 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
1858 N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
1859 isVolatile, isNonTemporal,
1860 MinAlign(Alignment, IncrementSize));
1862 // Build a factor node to remember that this load is independent of the
1864 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1867 // Big-endian - high bits are at low addresses. Favor aligned loads at
1868 // the cost of some bit-fiddling.
1869 EVT MemVT = N->getMemoryVT();
1870 unsigned EBytes = MemVT.getStoreSize();
1871 unsigned IncrementSize = NVT.getSizeInBits()/8;
1872 unsigned ExcessBits = (EBytes - IncrementSize)*8;
1874 // Load both the high bits and maybe some of the low bits.
1875 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
1876 EVT::getIntegerVT(*DAG.getContext(),
1877 MemVT.getSizeInBits() - ExcessBits),
1878 isVolatile, isNonTemporal, Alignment);
1880 // Increment the pointer to the other half.
1881 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
1882 DAG.getConstant(IncrementSize, Ptr.getValueType()));
1883 // Load the rest of the low bits.
1884 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
1885 N->getPointerInfo().getWithOffset(IncrementSize),
1886 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
1887 isVolatile, isNonTemporal,
1888 MinAlign(Alignment, IncrementSize));
1890 // Build a factor node to remember that this load is independent of the
1892 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1895 if (ExcessBits < NVT.getSizeInBits()) {
1896 // Transfer low bits from the bottom of Hi to the top of Lo.
1897 Lo = DAG.getNode(ISD::OR, dl, NVT, Lo,
1898 DAG.getNode(ISD::SHL, dl, NVT, Hi,
1899 DAG.getConstant(ExcessBits,
1900 TLI.getPointerTy())));
1901 // Move high bits to the right position in Hi.
1902 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl,
1904 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
1905 TLI.getPointerTy()));
1909 // Legalized the chain result - switch anything that used the old chain to
1911 ReplaceValueWith(SDValue(N, 1), Ch);
1914 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
1915 SDValue &Lo, SDValue &Hi) {
1917 SDValue LL, LH, RL, RH;
1918 GetExpandedInteger(N->getOperand(0), LL, LH);
1919 GetExpandedInteger(N->getOperand(1), RL, RH);
1920 Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
1921 Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
1924 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
1925 SDValue &Lo, SDValue &Hi) {
1926 EVT VT = N->getValueType(0);
1927 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1930 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
1931 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
1932 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
1933 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
1934 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1935 SDValue LL, LH, RL, RH;
1936 GetExpandedInteger(N->getOperand(0), LL, LH);
1937 GetExpandedInteger(N->getOperand(1), RL, RH);
1938 unsigned OuterBitSize = VT.getSizeInBits();
1939 unsigned InnerBitSize = NVT.getSizeInBits();
1940 unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1941 unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
1943 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
1944 if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
1945 DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
1946 // The inputs are both zero-extended.
1948 // We can emit a umul_lohi.
1949 Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
1950 Hi = SDValue(Lo.getNode(), 1);
1954 // We can emit a mulhu+mul.
1955 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1956 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
1960 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
1961 // The input values are both sign-extended.
1963 // We can emit a smul_lohi.
1964 Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
1965 Hi = SDValue(Lo.getNode(), 1);
1969 // We can emit a mulhs+mul.
1970 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1971 Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
1976 // Lo,Hi = umul LHS, RHS.
1977 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
1978 DAG.getVTList(NVT, NVT), LL, RL);
1980 Hi = UMulLOHI.getValue(1);
1981 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
1982 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
1983 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
1984 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
1988 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1989 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
1990 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
1991 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
1992 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
1993 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
1998 // If nothing else, we can make a libcall.
1999 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2001 LC = RTLIB::MUL_I16;
2002 else if (VT == MVT::i32)
2003 LC = RTLIB::MUL_I32;
2004 else if (VT == MVT::i64)
2005 LC = RTLIB::MUL_I64;
2006 else if (VT == MVT::i128)
2007 LC = RTLIB::MUL_I128;
2008 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
2010 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2011 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, true/*irrelevant*/,
2016 void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
2017 SDValue &Lo, SDValue &Hi) {
2018 SDValue LHS = Node->getOperand(0);
2019 SDValue RHS = Node->getOperand(1);
2022 // Expand the result by simply replacing it with the equivalent
2023 // non-overflow-checking operation.
2024 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
2025 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
2027 SplitInteger(Sum, Lo, Hi);
2029 // Compute the overflow.
2031 // LHSSign -> LHS >= 0
2032 // RHSSign -> RHS >= 0
2033 // SumSign -> Sum >= 0
2036 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
2038 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
2040 EVT OType = Node->getValueType(1);
2041 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
2043 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
2044 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
2045 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
2046 Node->getOpcode() == ISD::SADDO ?
2047 ISD::SETEQ : ISD::SETNE);
2049 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
2050 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
2052 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
2054 // Use the calculated overflow everywhere.
2055 ReplaceValueWith(SDValue(Node, 1), Cmp);
2058 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
2059 SDValue &Lo, SDValue &Hi) {
2060 EVT VT = N->getValueType(0);
2063 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2065 LC = RTLIB::SDIV_I16;
2066 else if (VT == MVT::i32)
2067 LC = RTLIB::SDIV_I32;
2068 else if (VT == MVT::i64)
2069 LC = RTLIB::SDIV_I64;
2070 else if (VT == MVT::i128)
2071 LC = RTLIB::SDIV_I128;
2072 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
2074 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2075 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, true, dl).first, Lo, Hi);
2078 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
2079 SDValue &Lo, SDValue &Hi) {
2080 EVT VT = N->getValueType(0);
2083 // If we can emit an efficient shift operation, do so now. Check to see if
2084 // the RHS is a constant.
2085 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2086 return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi);
2088 // If we can determine that the high bit of the shift is zero or one, even if
2089 // the low bits are variable, emit this shift in an optimized form.
2090 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
2093 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
2095 if (N->getOpcode() == ISD::SHL) {
2096 PartsOpc = ISD::SHL_PARTS;
2097 } else if (N->getOpcode() == ISD::SRL) {
2098 PartsOpc = ISD::SRL_PARTS;
2100 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
2101 PartsOpc = ISD::SRA_PARTS;
2104 // Next check to see if the target supports this SHL_PARTS operation or if it
2105 // will custom expand it.
2106 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2107 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
2108 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
2109 Action == TargetLowering::Custom) {
2110 // Expand the subcomponents.
2112 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2113 EVT VT = LHSL.getValueType();
2115 // If the shift amount operand is coming from a vector legalization it may
2116 // have an illegal type. Fix that first by casting the operand, otherwise
2117 // the new SHL_PARTS operation would need further legalization.
2118 SDValue ShiftOp = N->getOperand(1);
2119 EVT ShiftTy = TLI.getShiftAmountTy(VT);
2120 assert(ShiftTy.getScalarType().getSizeInBits() >=
2121 Log2_32_Ceil(VT.getScalarType().getSizeInBits()) &&
2122 "ShiftAmountTy is too small to cover the range of this type!");
2123 if (ShiftOp.getValueType() != ShiftTy)
2124 ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy);
2126 SDValue Ops[] = { LHSL, LHSH, ShiftOp };
2127 Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops, 3);
2128 Hi = Lo.getValue(1);
2132 // Otherwise, emit a libcall.
2133 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2135 if (N->getOpcode() == ISD::SHL) {
2136 isSigned = false; /*sign irrelevant*/
2138 LC = RTLIB::SHL_I16;
2139 else if (VT == MVT::i32)
2140 LC = RTLIB::SHL_I32;
2141 else if (VT == MVT::i64)
2142 LC = RTLIB::SHL_I64;
2143 else if (VT == MVT::i128)
2144 LC = RTLIB::SHL_I128;
2145 } else if (N->getOpcode() == ISD::SRL) {
2148 LC = RTLIB::SRL_I16;
2149 else if (VT == MVT::i32)
2150 LC = RTLIB::SRL_I32;
2151 else if (VT == MVT::i64)
2152 LC = RTLIB::SRL_I64;
2153 else if (VT == MVT::i128)
2154 LC = RTLIB::SRL_I128;
2156 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
2159 LC = RTLIB::SRA_I16;
2160 else if (VT == MVT::i32)
2161 LC = RTLIB::SRA_I32;
2162 else if (VT == MVT::i64)
2163 LC = RTLIB::SRA_I64;
2164 else if (VT == MVT::i128)
2165 LC = RTLIB::SRA_I128;
2168 if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) {
2169 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2170 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, isSigned, dl).first, Lo,
2175 if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
2176 llvm_unreachable("Unsupported shift!");
2179 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
2180 SDValue &Lo, SDValue &Hi) {
2181 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2183 SDValue Op = N->getOperand(0);
2184 if (Op.getValueType().bitsLE(NVT)) {
2185 // The low part is sign extension of the input (degenerates to a copy).
2186 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
2187 // The high part is obtained by SRA'ing all but one of the bits of low part.
2188 unsigned LoSize = NVT.getSizeInBits();
2189 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
2190 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
2192 // For example, extension of an i48 to an i64. The operand type necessarily
2193 // promotes to the result type, so will end up being expanded too.
2194 assert(getTypeAction(Op.getValueType()) ==
2195 TargetLowering::TypePromoteInteger &&
2196 "Only know how to promote this result!");
2197 SDValue Res = GetPromotedInteger(Op);
2198 assert(Res.getValueType() == N->getValueType(0) &&
2199 "Operand over promoted?");
2200 // Split the promoted operand. This will simplify when it is expanded.
2201 SplitInteger(Res, Lo, Hi);
2202 unsigned ExcessBits =
2203 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
2204 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
2205 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
2210 void DAGTypeLegalizer::
2211 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
2213 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2214 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2216 if (EVT.bitsLE(Lo.getValueType())) {
2217 // sext_inreg the low part if needed.
2218 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
2221 // The high part gets the sign extension from the lo-part. This handles
2222 // things like sextinreg V:i64 from i8.
2223 Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
2224 DAG.getConstant(Hi.getValueType().getSizeInBits()-1,
2225 TLI.getPointerTy()));
2227 // For example, extension of an i48 to an i64. Leave the low part alone,
2228 // sext_inreg the high part.
2229 unsigned ExcessBits =
2230 EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
2231 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
2232 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
2237 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
2238 SDValue &Lo, SDValue &Hi) {
2239 EVT VT = N->getValueType(0);
2242 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2244 LC = RTLIB::SREM_I16;
2245 else if (VT == MVT::i32)
2246 LC = RTLIB::SREM_I32;
2247 else if (VT == MVT::i64)
2248 LC = RTLIB::SREM_I64;
2249 else if (VT == MVT::i128)
2250 LC = RTLIB::SREM_I128;
2251 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
2253 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2254 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, true, dl).first, Lo, Hi);
2257 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
2258 SDValue &Lo, SDValue &Hi) {
2259 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2261 Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
2262 Hi = DAG.getNode(ISD::SRL, dl,
2263 N->getOperand(0).getValueType(), N->getOperand(0),
2264 DAG.getConstant(NVT.getSizeInBits(), TLI.getPointerTy()));
2265 Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
2268 void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
2269 SDValue &Lo, SDValue &Hi) {
2270 SDValue LHS = N->getOperand(0);
2271 SDValue RHS = N->getOperand(1);
2274 // Expand the result by simply replacing it with the equivalent
2275 // non-overflow-checking operation.
2276 SDValue Sum = DAG.getNode(N->getOpcode() == ISD::UADDO ?
2277 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
2279 SplitInteger(Sum, Lo, Hi);
2281 // Calculate the overflow: addition overflows iff a + b < a, and subtraction
2282 // overflows iff a - b > a.
2283 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS,
2284 N->getOpcode () == ISD::UADDO ?
2285 ISD::SETULT : ISD::SETUGT);
2287 // Use the calculated overflow everywhere.
2288 ReplaceValueWith(SDValue(N, 1), Ofl);
2291 void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N,
2292 SDValue &Lo, SDValue &Hi) {
2293 EVT VT = N->getValueType(0);
2296 // A divide for UMULO should be faster than a function call.
2297 if (N->getOpcode() == ISD::UMULO) {
2298 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
2300 SDValue MUL = DAG.getNode(ISD::MUL, dl, LHS.getValueType(), LHS, RHS);
2301 SplitInteger(MUL, Lo, Hi);
2303 // A divide for UMULO will be faster than a function call. Select to
2304 // make sure we aren't using 0.
2305 SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(VT),
2306 RHS, DAG.getConstant(0, VT), ISD::SETEQ);
2307 SDValue NotZero = DAG.getSelect(dl, VT, isZero,
2308 DAG.getConstant(1, VT), RHS);
2309 SDValue DIV = DAG.getNode(ISD::UDIV, dl, VT, MUL, NotZero);
2310 SDValue Overflow = DAG.getSetCC(dl, N->getValueType(1), DIV, LHS,
2312 Overflow = DAG.getSelect(dl, N->getValueType(1), isZero,
2313 DAG.getConstant(0, N->getValueType(1)),
2315 ReplaceValueWith(SDValue(N, 1), Overflow);
2319 Type *RetTy = VT.getTypeForEVT(*DAG.getContext());
2320 EVT PtrVT = TLI.getPointerTy();
2321 Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext());
2323 // Replace this with a libcall that will check overflow.
2324 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2326 LC = RTLIB::MULO_I32;
2327 else if (VT == MVT::i64)
2328 LC = RTLIB::MULO_I64;
2329 else if (VT == MVT::i128)
2330 LC = RTLIB::MULO_I128;
2331 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XMULO!");
2333 SDValue Temp = DAG.CreateStackTemporary(PtrVT);
2334 // Temporary for the overflow value, default it to zero.
2335 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl,
2336 DAG.getConstant(0, PtrVT), Temp,
2337 MachinePointerInfo(), false, false, 0);
2339 TargetLowering::ArgListTy Args;
2340 TargetLowering::ArgListEntry Entry;
2341 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2342 EVT ArgVT = N->getOperand(i).getValueType();
2343 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2344 Entry.Node = N->getOperand(i);
2346 Entry.isSExt = true;
2347 Entry.isZExt = false;
2348 Args.push_back(Entry);
2351 // Also pass the address of the overflow check.
2353 Entry.Ty = PtrTy->getPointerTo();
2354 Entry.isSExt = true;
2355 Entry.isZExt = false;
2356 Args.push_back(Entry);
2358 SDValue Func = DAG.getExternalSymbol(TLI.getLibcallName(LC), PtrVT);
2360 CallLoweringInfo CLI(Chain, RetTy, true, false, false, false,
2361 0, TLI.getLibcallCallingConv(LC),
2362 /*isTailCall=*/false,
2363 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
2364 Func, Args, DAG, dl);
2365 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2367 SplitInteger(CallInfo.first, Lo, Hi);
2368 SDValue Temp2 = DAG.getLoad(PtrVT, dl, CallInfo.second, Temp,
2369 MachinePointerInfo(), false, false, false, 0);
2370 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2,
2371 DAG.getConstant(0, PtrVT),
2373 // Use the overflow from the libcall everywhere.
2374 ReplaceValueWith(SDValue(N, 1), Ofl);
2377 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
2378 SDValue &Lo, SDValue &Hi) {
2379 EVT VT = N->getValueType(0);
2382 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2384 LC = RTLIB::UDIV_I16;
2385 else if (VT == MVT::i32)
2386 LC = RTLIB::UDIV_I32;
2387 else if (VT == MVT::i64)
2388 LC = RTLIB::UDIV_I64;
2389 else if (VT == MVT::i128)
2390 LC = RTLIB::UDIV_I128;
2391 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
2393 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2394 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, false, dl).first, Lo, Hi);
2397 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
2398 SDValue &Lo, SDValue &Hi) {
2399 EVT VT = N->getValueType(0);
2402 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2404 LC = RTLIB::UREM_I16;
2405 else if (VT == MVT::i32)
2406 LC = RTLIB::UREM_I32;
2407 else if (VT == MVT::i64)
2408 LC = RTLIB::UREM_I64;
2409 else if (VT == MVT::i128)
2410 LC = RTLIB::UREM_I128;
2411 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
2413 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2414 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, false, dl).first, Lo, Hi);
2417 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
2418 SDValue &Lo, SDValue &Hi) {
2419 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2421 SDValue Op = N->getOperand(0);
2422 if (Op.getValueType().bitsLE(NVT)) {
2423 // The low part is zero extension of the input (degenerates to a copy).
2424 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
2425 Hi = DAG.getConstant(0, NVT); // The high part is just a zero.
2427 // For example, extension of an i48 to an i64. The operand type necessarily
2428 // promotes to the result type, so will end up being expanded too.
2429 assert(getTypeAction(Op.getValueType()) ==
2430 TargetLowering::TypePromoteInteger &&
2431 "Only know how to promote this result!");
2432 SDValue Res = GetPromotedInteger(Op);
2433 assert(Res.getValueType() == N->getValueType(0) &&
2434 "Operand over promoted?");
2435 // Split the promoted operand. This will simplify when it is expanded.
2436 SplitInteger(Res, Lo, Hi);
2437 unsigned ExcessBits =
2438 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
2439 Hi = DAG.getZeroExtendInReg(Hi, dl,
2440 EVT::getIntegerVT(*DAG.getContext(),
2445 void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N,
2446 SDValue &Lo, SDValue &Hi) {
2448 EVT VT = cast<AtomicSDNode>(N)->getMemoryVT();
2449 SDValue Zero = DAG.getConstant(0, VT);
2450 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
2452 N->getOperand(1), Zero, Zero,
2453 cast<AtomicSDNode>(N)->getMemOperand(),
2454 cast<AtomicSDNode>(N)->getOrdering(),
2455 cast<AtomicSDNode>(N)->getSynchScope());
2456 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
2457 ReplaceValueWith(SDValue(N, 1), Swap.getValue(1));
2460 //===----------------------------------------------------------------------===//
2461 // Integer Operand Expansion
2462 //===----------------------------------------------------------------------===//
2464 /// ExpandIntegerOperand - This method is called when the specified operand of
2465 /// the specified node is found to need expansion. At this point, all of the
2466 /// result types of the node are known to be legal, but other operands of the
2467 /// node may need promotion or expansion as well as the specified one.
2468 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
2469 DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG); dbgs() << "\n");
2470 SDValue Res = SDValue();
2472 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
2475 switch (N->getOpcode()) {
2478 dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
2479 N->dump(&DAG); dbgs() << "\n";
2481 llvm_unreachable("Do not know how to expand this operator's operand!");
2483 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
2484 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
2485 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
2486 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
2487 case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
2488 case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
2489 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
2490 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
2491 case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
2492 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
2493 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
2494 case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
2500 case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
2501 case ISD::RETURNADDR:
2502 case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break;
2504 case ISD::ATOMIC_STORE: Res = ExpandIntOp_ATOMIC_STORE(N); break;
2507 // If the result is null, the sub-method took care of registering results etc.
2508 if (!Res.getNode()) return false;
2510 // If the result is N, the sub-method updated N in place. Tell the legalizer
2512 if (Res.getNode() == N)
2515 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
2516 "Invalid operand expansion");
2518 ReplaceValueWith(SDValue(N, 0), Res);
2522 /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
2523 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
2524 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
2526 ISD::CondCode &CCCode,
2528 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
2529 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
2530 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
2532 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
2533 if (RHSLo == RHSHi) {
2534 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
2535 if (RHSCST->isAllOnesValue()) {
2536 // Equality comparison to -1.
2537 NewLHS = DAG.getNode(ISD::AND, dl,
2538 LHSLo.getValueType(), LHSLo, LHSHi);
2545 NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
2546 NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
2547 NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
2548 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2552 // If this is a comparison of the sign bit, just look at the top part.
2554 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
2555 if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
2556 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
2562 // FIXME: This generated code sucks.
2563 ISD::CondCode LowCC;
2565 default: llvm_unreachable("Unknown integer setcc!");
2567 case ISD::SETULT: LowCC = ISD::SETULT; break;
2569 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2571 case ISD::SETULE: LowCC = ISD::SETULE; break;
2573 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2576 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2577 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2578 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2580 // NOTE: on targets without efficient SELECT of bools, we can always use
2581 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2582 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true, NULL);
2584 Tmp1 = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()),
2585 LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
2586 if (!Tmp1.getNode())
2587 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
2588 LHSLo, RHSLo, LowCC);
2589 Tmp2 = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()),
2590 LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
2591 if (!Tmp2.getNode())
2592 Tmp2 = DAG.getNode(ISD::SETCC, dl,
2593 getSetCCResultType(LHSHi.getValueType()),
2594 LHSHi, RHSHi, DAG.getCondCode(CCCode));
2596 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
2597 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
2598 if ((Tmp1C && Tmp1C->isNullValue()) ||
2599 (Tmp2C && Tmp2C->isNullValue() &&
2600 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
2601 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
2602 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
2603 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
2604 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
2605 // low part is known false, returns high part.
2606 // For LE / GE, if high part is known false, ignore the low part.
2607 // For LT / GT, if high part is known true, ignore the low part.
2613 NewLHS = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()),
2614 LHSHi, RHSHi, ISD::SETEQ, false,
2615 DagCombineInfo, dl);
2616 if (!NewLHS.getNode())
2617 NewLHS = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
2618 LHSHi, RHSHi, ISD::SETEQ);
2619 NewLHS = DAG.getSelect(dl, Tmp1.getValueType(),
2620 NewLHS, Tmp1, Tmp2);
2624 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
2625 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
2626 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
2627 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
2629 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2630 // against zero to select between true and false values.
2631 if (NewRHS.getNode() == 0) {
2632 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2633 CCCode = ISD::SETNE;
2636 // Update N to have the operands specified.
2637 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2638 DAG.getCondCode(CCCode), NewLHS, NewRHS,
2639 N->getOperand(4)), 0);
2642 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
2643 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2644 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
2645 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
2647 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2648 // against zero to select between true and false values.
2649 if (NewRHS.getNode() == 0) {
2650 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2651 CCCode = ISD::SETNE;
2654 // Update N to have the operands specified.
2655 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
2656 N->getOperand(2), N->getOperand(3),
2657 DAG.getCondCode(CCCode)), 0);
2660 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
2661 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2662 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
2663 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
2665 // If ExpandSetCCOperands returned a scalar, use it.
2666 if (NewRHS.getNode() == 0) {
2667 assert(NewLHS.getValueType() == N->getValueType(0) &&
2668 "Unexpected setcc expansion!");
2672 // Otherwise, update N to have the operands specified.
2673 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
2674 DAG.getCondCode(CCCode)), 0);
2677 SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
2678 // The value being shifted is legal, but the shift amount is too big.
2679 // It follows that either the result of the shift is undefined, or the
2680 // upper half of the shift amount is zero. Just use the lower half.
2682 GetExpandedInteger(N->getOperand(1), Lo, Hi);
2683 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
2686 SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
2687 // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
2688 // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
2689 // constant to valid type.
2691 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2692 return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
2695 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
2696 SDValue Op = N->getOperand(0);
2697 EVT DstVT = N->getValueType(0);
2698 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
2699 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2700 "Don't know how to expand this SINT_TO_FP!");
2701 return TLI.makeLibCall(DAG, LC, DstVT, &Op, 1, true, SDLoc(N)).first;
2704 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
2705 if (ISD::isNormalStore(N))
2706 return ExpandOp_NormalStore(N, OpNo);
2708 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2709 assert(OpNo == 1 && "Can only expand the stored value so far");
2711 EVT VT = N->getOperand(1).getValueType();
2712 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2713 SDValue Ch = N->getChain();
2714 SDValue Ptr = N->getBasePtr();
2715 unsigned Alignment = N->getAlignment();
2716 bool isVolatile = N->isVolatile();
2717 bool isNonTemporal = N->isNonTemporal();
2721 assert(NVT.isByteSized() && "Expanded type not byte sized!");
2723 if (N->getMemoryVT().bitsLE(NVT)) {
2724 GetExpandedInteger(N->getValue(), Lo, Hi);
2725 return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
2726 N->getMemoryVT(), isVolatile, isNonTemporal,
2730 if (TLI.isLittleEndian()) {
2731 // Little-endian - low bits are at low addresses.
2732 GetExpandedInteger(N->getValue(), Lo, Hi);
2734 Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
2735 isVolatile, isNonTemporal, Alignment);
2737 unsigned ExcessBits =
2738 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
2739 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
2741 // Increment the pointer to the other half.
2742 unsigned IncrementSize = NVT.getSizeInBits()/8;
2743 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
2744 DAG.getConstant(IncrementSize, Ptr.getValueType()));
2745 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr,
2746 N->getPointerInfo().getWithOffset(IncrementSize),
2747 NEVT, isVolatile, isNonTemporal,
2748 MinAlign(Alignment, IncrementSize));
2749 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
2752 // Big-endian - high bits are at low addresses. Favor aligned stores at
2753 // the cost of some bit-fiddling.
2754 GetExpandedInteger(N->getValue(), Lo, Hi);
2756 EVT ExtVT = N->getMemoryVT();
2757 unsigned EBytes = ExtVT.getStoreSize();
2758 unsigned IncrementSize = NVT.getSizeInBits()/8;
2759 unsigned ExcessBits = (EBytes - IncrementSize)*8;
2760 EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
2761 ExtVT.getSizeInBits() - ExcessBits);
2763 if (ExcessBits < NVT.getSizeInBits()) {
2764 // Transfer high bits from the top of Lo to the bottom of Hi.
2765 Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
2766 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
2767 TLI.getPointerTy()));
2768 Hi = DAG.getNode(ISD::OR, dl, NVT, Hi,
2769 DAG.getNode(ISD::SRL, dl, NVT, Lo,
2770 DAG.getConstant(ExcessBits,
2771 TLI.getPointerTy())));
2774 // Store both the high bits and maybe some of the low bits.
2775 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(),
2776 HiVT, isVolatile, isNonTemporal, Alignment);
2778 // Increment the pointer to the other half.
2779 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
2780 DAG.getConstant(IncrementSize, Ptr.getValueType()));
2781 // Store the lowest ExcessBits bits in the second half.
2782 Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
2783 N->getPointerInfo().getWithOffset(IncrementSize),
2784 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
2785 isVolatile, isNonTemporal,
2786 MinAlign(Alignment, IncrementSize));
2787 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
2790 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
2792 GetExpandedInteger(N->getOperand(0), InL, InH);
2793 // Just truncate the low part of the source.
2794 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL);
2797 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
2798 SDValue Op = N->getOperand(0);
2799 EVT SrcVT = Op.getValueType();
2800 EVT DstVT = N->getValueType(0);
2803 // The following optimization is valid only if every value in SrcVT (when
2804 // treated as signed) is representable in DstVT. Check that the mantissa
2805 // size of DstVT is >= than the number of bits in SrcVT -1.
2806 const fltSemantics &sem = DAG.EVTToAPFloatSemantics(DstVT);
2807 if (APFloat::semanticsPrecision(sem) >= SrcVT.getSizeInBits()-1 &&
2808 TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
2809 // Do a signed conversion then adjust the result.
2810 SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op);
2811 SignedConv = TLI.LowerOperation(SignedConv, DAG);
2813 // The result of the signed conversion needs adjusting if the 'sign bit' of
2814 // the incoming integer was set. To handle this, we dynamically test to see
2815 // if it is set, and, if so, add a fudge factor.
2817 const uint64_t F32TwoE32 = 0x4F800000ULL;
2818 const uint64_t F32TwoE64 = 0x5F800000ULL;
2819 const uint64_t F32TwoE128 = 0x7F800000ULL;
2822 if (SrcVT == MVT::i32)
2823 FF = APInt(32, F32TwoE32);
2824 else if (SrcVT == MVT::i64)
2825 FF = APInt(32, F32TwoE64);
2826 else if (SrcVT == MVT::i128)
2827 FF = APInt(32, F32TwoE128);
2829 llvm_unreachable("Unsupported UINT_TO_FP!");
2831 // Check whether the sign bit is set.
2833 GetExpandedInteger(Op, Lo, Hi);
2834 SDValue SignSet = DAG.getSetCC(dl,
2835 getSetCCResultType(Hi.getValueType()),
2836 Hi, DAG.getConstant(0, Hi.getValueType()),
2839 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
2840 SDValue FudgePtr = DAG.getConstantPool(
2841 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
2842 TLI.getPointerTy());
2844 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
2845 SDValue Zero = DAG.getIntPtrConstant(0);
2846 SDValue Four = DAG.getIntPtrConstant(4);
2847 if (TLI.isBigEndian()) std::swap(Zero, Four);
2848 SDValue Offset = DAG.getSelect(dl, Zero.getValueType(), SignSet,
2850 unsigned Alignment = cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
2851 FudgePtr = DAG.getNode(ISD::ADD, dl, FudgePtr.getValueType(),
2853 Alignment = std::min(Alignment, 4u);
2855 // Load the value out, extending it from f32 to the destination float type.
2856 // FIXME: Avoid the extend by constructing the right constant pool?
2857 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(),
2859 MachinePointerInfo::getConstantPool(),
2861 false, false, Alignment);
2862 return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge);
2865 // Otherwise, use a libcall.
2866 RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
2867 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2868 "Don't know how to expand this UINT_TO_FP!");
2869 return TLI.makeLibCall(DAG, LC, DstVT, &Op, 1, true, dl).first;
2872 SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) {
2874 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
2875 cast<AtomicSDNode>(N)->getMemoryVT(),
2877 N->getOperand(1), N->getOperand(2),
2878 cast<AtomicSDNode>(N)->getMemOperand(),
2879 cast<AtomicSDNode>(N)->getOrdering(),
2880 cast<AtomicSDNode>(N)->getSynchScope());
2881 return Swap.getValue(1);
2885 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
2886 SDValue InOp0 = N->getOperand(0);
2887 EVT InVT = InOp0.getValueType();
2889 EVT OutVT = N->getValueType(0);
2890 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
2891 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
2892 unsigned OutNumElems = OutVT.getVectorNumElements();
2893 EVT NOutVTElem = NOutVT.getVectorElementType();
2896 SDValue BaseIdx = N->getOperand(1);
2898 SmallVector<SDValue, 8> Ops;
2899 Ops.reserve(OutNumElems);
2900 for (unsigned i = 0; i != OutNumElems; ++i) {
2902 // Extract the element from the original vector.
2903 SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(),
2904 BaseIdx, DAG.getConstant(i, BaseIdx.getValueType()));
2905 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
2906 InVT.getVectorElementType(), N->getOperand(0), Index);
2908 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, Ext);
2909 // Insert the converted element to the new vector.
2913 return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, &Ops[0], Ops.size());
2917 SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
2918 ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
2919 EVT VT = N->getValueType(0);
2922 unsigned NumElts = VT.getVectorNumElements();
2923 SmallVector<int, 8> NewMask;
2924 for (unsigned i = 0; i != NumElts; ++i) {
2925 NewMask.push_back(SV->getMaskElt(i));
2928 SDValue V0 = GetPromotedInteger(N->getOperand(0));
2929 SDValue V1 = GetPromotedInteger(N->getOperand(1));
2930 EVT OutVT = V0.getValueType();
2932 return DAG.getVectorShuffle(OutVT, dl, V0, V1, &NewMask[0]);
2936 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
2937 EVT OutVT = N->getValueType(0);
2938 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
2939 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
2940 unsigned NumElems = N->getNumOperands();
2941 EVT NOutVTElem = NOutVT.getVectorElementType();
2945 SmallVector<SDValue, 8> Ops;
2946 Ops.reserve(NumElems);
2947 for (unsigned i = 0; i != NumElems; ++i) {
2949 // BUILD_VECTOR integer operand types are allowed to be larger than the
2950 // result's element type. This may still be true after the promotion. For
2951 // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
2952 // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
2953 if (N->getOperand(i).getValueType().bitsLT(NOutVTElem))
2954 Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(i));
2956 Op = N->getOperand(i);
2960 return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, &Ops[0], Ops.size());
2963 SDValue DAGTypeLegalizer::PromoteIntRes_SCALAR_TO_VECTOR(SDNode *N) {
2967 assert(!N->getOperand(0).getValueType().isVector() &&
2968 "Input must be a scalar");
2970 EVT OutVT = N->getValueType(0);
2971 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
2972 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
2973 EVT NOutVTElem = NOutVT.getVectorElementType();
2975 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(0));
2977 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NOutVT, Op);
2980 SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) {
2983 EVT OutVT = N->getValueType(0);
2984 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
2985 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
2987 EVT InElemTy = OutVT.getVectorElementType();
2988 EVT OutElemTy = NOutVT.getVectorElementType();
2990 unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements();
2991 unsigned NumOutElem = NOutVT.getVectorNumElements();
2992 unsigned NumOperands = N->getNumOperands();
2993 assert(NumElem * NumOperands == NumOutElem &&
2994 "Unexpected number of elements");
2996 // Take the elements from the first vector.
2997 SmallVector<SDValue, 8> Ops(NumOutElem);
2998 for (unsigned i = 0; i < NumOperands; ++i) {
2999 SDValue Op = N->getOperand(i);
3000 for (unsigned j = 0; j < NumElem; ++j) {
3001 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3002 InElemTy, Op, DAG.getConstant(j,
3003 TLI.getVectorIdxTy()));
3004 Ops[i * NumElem + j] = DAG.getNode(ISD::ANY_EXTEND, dl, OutElemTy, Ext);
3008 return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, &Ops[0], Ops.size());
3011 SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
3012 EVT OutVT = N->getValueType(0);
3013 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
3014 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
3016 EVT NOutVTElem = NOutVT.getVectorElementType();
3019 SDValue V0 = GetPromotedInteger(N->getOperand(0));
3021 SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
3022 NOutVTElem, N->getOperand(1));
3023 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT,
3024 V0, ConvElem, N->getOperand(2));
3027 SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
3029 SDValue V0 = GetPromotedInteger(N->getOperand(0));
3030 SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl, TLI.getVectorIdxTy());
3031 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3032 V0->getValueType(0).getScalarType(), V0, V1);
3034 // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
3035 // element types. If this is the case then we need to expand the outgoing
3036 // value and not truncate it.
3037 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
3040 SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
3042 unsigned NumElems = N->getNumOperands();
3044 EVT RetSclrTy = N->getValueType(0).getVectorElementType();
3046 SmallVector<SDValue, 8> NewOps;
3047 NewOps.reserve(NumElems);
3049 // For each incoming vector
3050 for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) {
3051 SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
3052 EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
3053 unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
3055 for (unsigned i=0; i<NumElem; ++i) {
3056 // Extract element from incoming vector
3057 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy,
3058 Incoming, DAG.getConstant(i, TLI.getVectorIdxTy()));
3059 SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
3060 NewOps.push_back(Tr);
3064 return DAG.getNode(ISD::BUILD_VECTOR, dl, N->getValueType(0),
3065 &NewOps[0], NewOps.size());