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/CodeGen/PseudoSourceValue.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::AssertSext: Res = PromoteIntRes_AssertSext(N); break;
51 case ISD::AssertZext: Res = PromoteIntRes_AssertZext(N); break;
52 case ISD::BITCAST: Res = PromoteIntRes_BITCAST(N); break;
53 case ISD::BSWAP: Res = PromoteIntRes_BSWAP(N); break;
54 case ISD::BUILD_PAIR: Res = PromoteIntRes_BUILD_PAIR(N); break;
55 case ISD::Constant: Res = PromoteIntRes_Constant(N); break;
56 case ISD::CONVERT_RNDSAT:
57 Res = PromoteIntRes_CONVERT_RNDSAT(N); break;
58 case ISD::CTLZ: Res = PromoteIntRes_CTLZ(N); break;
59 case ISD::CTPOP: Res = PromoteIntRes_CTPOP(N); break;
60 case ISD::CTTZ: Res = PromoteIntRes_CTTZ(N); break;
61 case ISD::EXTRACT_VECTOR_ELT:
62 Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
63 case ISD::LOAD: Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N));break;
64 case ISD::SELECT: Res = PromoteIntRes_SELECT(N); break;
65 case ISD::SELECT_CC: Res = PromoteIntRes_SELECT_CC(N); break;
66 case ISD::SETCC: Res = PromoteIntRes_SETCC(N); break;
67 case ISD::SHL: Res = PromoteIntRes_SHL(N); break;
68 case ISD::SIGN_EXTEND_INREG:
69 Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
70 case ISD::SRA: Res = PromoteIntRes_SRA(N); break;
71 case ISD::SRL: Res = PromoteIntRes_SRL(N); break;
72 case ISD::TRUNCATE: Res = PromoteIntRes_TRUNCATE(N); break;
73 case ISD::UNDEF: Res = PromoteIntRes_UNDEF(N); break;
74 case ISD::VAARG: Res = PromoteIntRes_VAARG(N); break;
76 case ISD::EXTRACT_SUBVECTOR:
77 Res = PromoteIntRes_EXTRACT_SUBVECTOR(N); break;
78 case ISD::VECTOR_SHUFFLE:
79 Res = PromoteIntRes_VECTOR_SHUFFLE(N); break;
80 case ISD::INSERT_VECTOR_ELT:
81 Res = PromoteIntRes_INSERT_VECTOR_ELT(N); break;
82 case ISD::BUILD_VECTOR:
83 Res = PromoteIntRes_BUILD_VECTOR(N); break;
84 case ISD::SCALAR_TO_VECTOR:
85 Res = PromoteIntRes_SCALAR_TO_VECTOR(N); break;
87 case ISD::SIGN_EXTEND:
88 case ISD::ZERO_EXTEND:
89 case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break;
92 case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break;
94 case ISD::FP32_TO_FP16:Res = PromoteIntRes_FP32_TO_FP16(N); break;
101 case ISD::MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break;
104 case ISD::SREM: Res = PromoteIntRes_SDIV(N); break;
107 case ISD::UREM: Res = PromoteIntRes_UDIV(N); break;
110 case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
112 case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
114 case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;
116 case ISD::ATOMIC_LOAD_ADD:
117 case ISD::ATOMIC_LOAD_SUB:
118 case ISD::ATOMIC_LOAD_AND:
119 case ISD::ATOMIC_LOAD_OR:
120 case ISD::ATOMIC_LOAD_XOR:
121 case ISD::ATOMIC_LOAD_NAND:
122 case ISD::ATOMIC_LOAD_MIN:
123 case ISD::ATOMIC_LOAD_MAX:
124 case ISD::ATOMIC_LOAD_UMIN:
125 case ISD::ATOMIC_LOAD_UMAX:
126 case ISD::ATOMIC_SWAP:
127 Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
129 case ISD::ATOMIC_CMP_SWAP:
130 Res = PromoteIntRes_Atomic2(cast<AtomicSDNode>(N)); break;
133 // If the result is null then the sub-method took care of registering it.
135 SetPromotedInteger(SDValue(N, ResNo), Res);
138 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
139 // Sign-extend the new bits, and continue the assertion.
140 SDValue Op = SExtPromotedInteger(N->getOperand(0));
141 return DAG.getNode(ISD::AssertSext, N->getDebugLoc(),
142 Op.getValueType(), Op, N->getOperand(1));
145 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
146 // Zero the new bits, and continue the assertion.
147 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
148 return DAG.getNode(ISD::AssertZext, N->getDebugLoc(),
149 Op.getValueType(), Op, N->getOperand(1));
152 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
153 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
154 SDValue Res = DAG.getAtomic(N->getOpcode(), N->getDebugLoc(),
156 N->getChain(), N->getBasePtr(),
157 Op2, N->getMemOperand());
158 // Legalized the chain result - switch anything that used the old chain to
160 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
164 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic2(AtomicSDNode *N) {
165 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
166 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
167 SDValue Res = DAG.getAtomic(N->getOpcode(), N->getDebugLoc(),
168 N->getMemoryVT(), N->getChain(), N->getBasePtr(),
169 Op2, Op3, N->getMemOperand());
170 // Legalized the chain result - switch anything that used the old chain to
172 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
176 SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
177 SDValue InOp = N->getOperand(0);
178 EVT InVT = InOp.getValueType();
179 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
180 EVT OutVT = N->getValueType(0);
181 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
182 DebugLoc dl = N->getDebugLoc();
184 switch (getTypeAction(InVT)) {
186 assert(false && "Unknown type action!");
188 case TargetLowering::TypeLegal:
190 case TargetLowering::TypePromoteInteger:
191 if (NOutVT.bitsEq(NInVT))
192 // The input promotes to the same size. Convert the promoted value.
193 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp));
194 if (NInVT.isVector())
195 // Promote vector element via memory load/store.
196 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
197 CreateStackStoreLoad(InOp, OutVT));
199 case TargetLowering::TypeSoftenFloat:
200 // Promote the integer operand by hand.
201 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
202 case TargetLowering::TypeExpandInteger:
203 case TargetLowering::TypeExpandFloat:
205 case TargetLowering::TypeScalarizeVector:
206 // Convert the element to an integer and promote it by hand.
207 if (!NOutVT.isVector())
208 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
209 BitConvertToInteger(GetScalarizedVector(InOp)));
211 case TargetLowering::TypeSplitVector: {
212 // For example, i32 = BITCAST v2i16 on alpha. Convert the split
213 // pieces of the input into integers and reassemble in the final type.
215 GetSplitVector(N->getOperand(0), Lo, Hi);
216 Lo = BitConvertToInteger(Lo);
217 Hi = BitConvertToInteger(Hi);
219 if (TLI.isBigEndian())
222 InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
223 EVT::getIntegerVT(*DAG.getContext(),
224 NOutVT.getSizeInBits()),
225 JoinIntegers(Lo, Hi));
226 return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp);
228 case TargetLowering::TypeWidenVector:
229 if (OutVT.bitsEq(NInVT))
230 // The input is widened to the same size. Convert to the widened value.
231 return DAG.getNode(ISD::BITCAST, dl, OutVT, GetWidenedVector(InOp));
234 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
235 CreateStackStoreLoad(InOp, OutVT));
238 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
239 SDValue Op = GetPromotedInteger(N->getOperand(0));
240 EVT OVT = N->getValueType(0);
241 EVT NVT = Op.getValueType();
242 DebugLoc dl = N->getDebugLoc();
244 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
245 return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
246 DAG.getConstant(DiffBits, TLI.getPointerTy()));
249 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
250 // The pair element type may be legal, or may not promote to the same type as
251 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
252 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(),
253 TLI.getTypeToTransformTo(*DAG.getContext(),
254 N->getValueType(0)), JoinIntegers(N->getOperand(0),
258 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
259 EVT VT = N->getValueType(0);
260 // FIXME there is no actual debug info here
261 DebugLoc dl = N->getDebugLoc();
262 // Zero extend things like i1, sign extend everything else. It shouldn't
263 // matter in theory which one we pick, but this tends to give better code?
264 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
265 SDValue Result = DAG.getNode(Opc, dl,
266 TLI.getTypeToTransformTo(*DAG.getContext(), VT),
268 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
272 SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) {
273 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
274 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
275 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
276 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
277 "can only promote integers");
278 EVT OutVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
279 return DAG.getConvertRndSat(OutVT, N->getDebugLoc(), N->getOperand(0),
280 N->getOperand(1), N->getOperand(2),
281 N->getOperand(3), N->getOperand(4), CvtCode);
284 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
285 // Zero extend to the promoted type and do the count there.
286 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
287 DebugLoc dl = N->getDebugLoc();
288 EVT OVT = N->getValueType(0);
289 EVT NVT = Op.getValueType();
290 Op = DAG.getNode(ISD::CTLZ, dl, NVT, Op);
291 // Subtract off the extra leading bits in the bigger type.
292 return DAG.getNode(ISD::SUB, dl, NVT, Op,
293 DAG.getConstant(NVT.getSizeInBits() -
294 OVT.getSizeInBits(), NVT));
297 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
298 // Zero extend to the promoted type and do the count there.
299 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
300 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), Op.getValueType(), Op);
303 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
304 SDValue Op = GetPromotedInteger(N->getOperand(0));
305 EVT OVT = N->getValueType(0);
306 EVT NVT = Op.getValueType();
307 DebugLoc dl = N->getDebugLoc();
308 // The count is the same in the promoted type except if the original
309 // value was zero. This can be handled by setting the bit just off
310 // the top of the original type.
311 APInt TopBit(NVT.getSizeInBits(), 0);
312 TopBit.setBit(OVT.getSizeInBits());
313 Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, NVT));
314 return DAG.getNode(ISD::CTTZ, dl, NVT, Op);
317 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
318 DebugLoc dl = N->getDebugLoc();
319 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
320 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, N->getOperand(0),
324 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
325 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
326 unsigned NewOpc = N->getOpcode();
327 DebugLoc dl = N->getDebugLoc();
329 // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
330 // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT
331 // and SINT conversions are Custom, there is no way to tell which is
332 // preferable. We choose SINT because that's the right thing on PPC.)
333 if (N->getOpcode() == ISD::FP_TO_UINT &&
334 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
335 TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
336 NewOpc = ISD::FP_TO_SINT;
338 SDValue Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
340 // Assert that the converted value fits in the original type. If it doesn't
341 // (eg: because the value being converted is too big), then the result of the
342 // original operation was undefined anyway, so the assert is still correct.
343 return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ?
344 ISD::AssertZext : ISD::AssertSext, dl,
345 NVT, Res, DAG.getValueType(N->getValueType(0)));
348 SDValue DAGTypeLegalizer::PromoteIntRes_FP32_TO_FP16(SDNode *N) {
349 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
350 DebugLoc dl = N->getDebugLoc();
352 SDValue Res = DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
354 return DAG.getNode(ISD::AssertZext, dl,
355 NVT, Res, DAG.getValueType(N->getValueType(0)));
358 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
359 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
360 DebugLoc dl = N->getDebugLoc();
362 if (getTypeAction(N->getOperand(0).getValueType())
363 == TargetLowering::TypePromoteInteger) {
364 SDValue Res = GetPromotedInteger(N->getOperand(0));
365 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
367 // If the result and operand types are the same after promotion, simplify
368 // to an in-register extension.
369 if (NVT == Res.getValueType()) {
370 // The high bits are not guaranteed to be anything. Insert an extend.
371 if (N->getOpcode() == ISD::SIGN_EXTEND)
372 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
373 DAG.getValueType(N->getOperand(0).getValueType()));
374 if (N->getOpcode() == ISD::ZERO_EXTEND)
375 return DAG.getZeroExtendInReg(Res, dl, N->getOperand(0).getValueType());
376 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
381 // Otherwise, just extend the original operand all the way to the larger type.
382 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
385 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
386 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
387 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
388 ISD::LoadExtType ExtType =
389 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
390 DebugLoc dl = N->getDebugLoc();
391 SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
393 N->getMemoryVT(), N->isVolatile(),
394 N->isNonTemporal(), N->getAlignment());
396 // Legalized the chain result - switch anything that used the old chain to
398 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
402 /// Promote the overflow flag of an overflowing arithmetic node.
403 SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
404 // Simply change the return type of the boolean result.
405 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
406 EVT ValueVTs[] = { N->getValueType(0), NVT };
407 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
408 SDValue Res = DAG.getNode(N->getOpcode(), N->getDebugLoc(),
409 DAG.getVTList(ValueVTs, 2), Ops, 2);
411 // Modified the sum result - switch anything that used the old sum to use
413 ReplaceValueWith(SDValue(N, 0), Res);
415 return SDValue(Res.getNode(), 1);
418 SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
420 return PromoteIntRes_Overflow(N);
422 // The operation overflowed iff the result in the larger type is not the
423 // sign extension of its truncation to the original type.
424 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
425 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
426 EVT OVT = N->getOperand(0).getValueType();
427 EVT NVT = LHS.getValueType();
428 DebugLoc dl = N->getDebugLoc();
430 // Do the arithmetic in the larger type.
431 unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
432 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
434 // Calculate the overflow flag: sign extend the arithmetic result from
435 // the original type.
436 SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
437 DAG.getValueType(OVT));
438 // Overflowed if and only if this is not equal to Res.
439 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
441 // Use the calculated overflow everywhere.
442 ReplaceValueWith(SDValue(N, 1), Ofl);
447 SDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) {
448 // Sign extend the input.
449 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
450 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
451 return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
452 LHS.getValueType(), LHS, RHS);
455 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
456 SDValue LHS = GetPromotedInteger(N->getOperand(1));
457 SDValue RHS = GetPromotedInteger(N->getOperand(2));
458 return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
459 LHS.getValueType(), N->getOperand(0),LHS,RHS);
462 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
463 SDValue LHS = GetPromotedInteger(N->getOperand(2));
464 SDValue RHS = GetPromotedInteger(N->getOperand(3));
465 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
466 LHS.getValueType(), N->getOperand(0),
467 N->getOperand(1), LHS, RHS, N->getOperand(4));
470 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
471 EVT SVT = TLI.getSetCCResultType(N->getOperand(0).getValueType());
472 assert(isTypeLegal(SVT) && "Illegal SetCC type!");
473 DebugLoc dl = N->getDebugLoc();
475 // Get the SETCC result using the canonical SETCC type.
476 SDValue SetCC = DAG.getNode(ISD::SETCC, dl, SVT, N->getOperand(0),
477 N->getOperand(1), N->getOperand(2));
479 // Convert to the expected type.
480 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
481 assert(NVT.bitsLE(SVT) && "Integer type overpromoted?");
482 return DAG.getNode(ISD::TRUNCATE, dl, NVT, SetCC);
485 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
486 return DAG.getNode(ISD::SHL, N->getDebugLoc(),
487 TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)),
488 GetPromotedInteger(N->getOperand(0)), N->getOperand(1));
491 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
492 SDValue Op = GetPromotedInteger(N->getOperand(0));
493 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(),
494 Op.getValueType(), Op, N->getOperand(1));
497 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
498 // The input may have strange things in the top bits of the registers, but
499 // these operations don't care. They may have weird bits going out, but
500 // that too is okay if they are integer operations.
501 SDValue LHS = GetPromotedInteger(N->getOperand(0));
502 SDValue RHS = GetPromotedInteger(N->getOperand(1));
503 return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
504 LHS.getValueType(), LHS, RHS);
507 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
508 // The input value must be properly sign extended.
509 SDValue Res = SExtPromotedInteger(N->getOperand(0));
510 return DAG.getNode(ISD::SRA, N->getDebugLoc(),
511 Res.getValueType(), Res, N->getOperand(1));
514 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
515 // The input value must be properly zero extended.
516 EVT VT = N->getValueType(0);
517 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
518 SDValue Res = ZExtPromotedInteger(N->getOperand(0));
519 return DAG.getNode(ISD::SRL, N->getDebugLoc(), NVT, Res, N->getOperand(1));
522 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
523 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
526 switch (getTypeAction(N->getOperand(0).getValueType())) {
527 default: llvm_unreachable("Unknown type action!");
528 case TargetLowering::TypeLegal:
529 case TargetLowering::TypeExpandInteger:
530 Res = N->getOperand(0);
532 case TargetLowering::TypePromoteInteger:
533 Res = GetPromotedInteger(N->getOperand(0));
537 // Truncate to NVT instead of VT
538 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Res);
541 SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
543 return PromoteIntRes_Overflow(N);
545 // The operation overflowed iff the result in the larger type is not the
546 // zero extension of its truncation to the original type.
547 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
548 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
549 EVT OVT = N->getOperand(0).getValueType();
550 EVT NVT = LHS.getValueType();
551 DebugLoc dl = N->getDebugLoc();
553 // Do the arithmetic in the larger type.
554 unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
555 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
557 // Calculate the overflow flag: zero extend the arithmetic result from
558 // the original type.
559 SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT);
560 // Overflowed if and only if this is not equal to Res.
561 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
563 // Use the calculated overflow everywhere.
564 ReplaceValueWith(SDValue(N, 1), Ofl);
569 SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
570 // Promote the overflow bit trivially.
572 return PromoteIntRes_Overflow(N);
574 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
575 DebugLoc DL = N->getDebugLoc();
576 EVT SmallVT = LHS.getValueType();
578 // To determine if the result overflowed in a larger type, we extend the
579 // input to the larger type, do the multiply, then check the high bits of
580 // the result to see if the overflow happened.
581 if (N->getOpcode() == ISD::SMULO) {
582 LHS = SExtPromotedInteger(LHS);
583 RHS = SExtPromotedInteger(RHS);
585 LHS = ZExtPromotedInteger(LHS);
586 RHS = ZExtPromotedInteger(RHS);
588 SDValue Mul = DAG.getNode(ISD::MUL, DL, LHS.getValueType(), LHS, RHS);
590 // Overflow occurred iff the high part of the result does not
591 // zero/sign-extend the low part.
593 if (N->getOpcode() == ISD::UMULO) {
594 // Unsigned overflow occurred iff the high part is non-zero.
595 SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
596 DAG.getIntPtrConstant(SmallVT.getSizeInBits()));
597 Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
598 DAG.getConstant(0, Hi.getValueType()), ISD::SETNE);
600 // Signed overflow occurred iff the high part does not sign extend the low.
601 SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
602 Mul, DAG.getValueType(SmallVT));
603 Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
606 // Use the calculated overflow everywhere.
607 ReplaceValueWith(SDValue(N, 1), Overflow);
611 SDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) {
612 // Zero extend the input.
613 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
614 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
615 return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
616 LHS.getValueType(), LHS, RHS);
619 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
620 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
621 N->getValueType(0)));
624 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
625 SDValue Chain = N->getOperand(0); // Get the chain.
626 SDValue Ptr = N->getOperand(1); // Get the pointer.
627 EVT VT = N->getValueType(0);
628 DebugLoc dl = N->getDebugLoc();
630 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
631 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
632 // The argument is passed as NumRegs registers of type RegVT.
634 SmallVector<SDValue, 8> Parts(NumRegs);
635 for (unsigned i = 0; i < NumRegs; ++i) {
636 Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
637 N->getConstantOperandVal(3));
638 Chain = Parts[i].getValue(1);
641 // Handle endianness of the load.
642 if (TLI.isBigEndian())
643 std::reverse(Parts.begin(), Parts.end());
645 // Assemble the parts in the promoted type.
646 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
647 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
648 for (unsigned i = 1; i < NumRegs; ++i) {
649 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
650 // Shift it to the right position and "or" it in.
651 Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
652 DAG.getConstant(i * RegVT.getSizeInBits(),
653 TLI.getPointerTy()));
654 Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
657 // Modified the chain result - switch anything that used the old chain to
659 ReplaceValueWith(SDValue(N, 1), Chain);
664 //===----------------------------------------------------------------------===//
665 // Integer Operand Promotion
666 //===----------------------------------------------------------------------===//
668 /// PromoteIntegerOperand - This method is called when the specified operand of
669 /// the specified node is found to need promotion. At this point, all of the
670 /// result types of the node are known to be legal, but other operands of the
671 /// node may need promotion or expansion as well as the specified one.
672 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
673 DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG); dbgs() << "\n");
674 SDValue Res = SDValue();
676 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
679 switch (N->getOpcode()) {
682 dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
683 N->dump(&DAG); dbgs() << "\n";
685 llvm_unreachable("Do not know how to promote this operator's operand!");
687 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
688 case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break;
689 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
690 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
691 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
692 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
693 case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
694 case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
695 case ISD::CONVERT_RNDSAT:
696 Res = PromoteIntOp_CONVERT_RNDSAT(N); break;
697 case ISD::INSERT_VECTOR_ELT:
698 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
699 case ISD::MEMBARRIER: Res = PromoteIntOp_MEMBARRIER(N); break;
700 case ISD::SCALAR_TO_VECTOR:
701 Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break;
702 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
703 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
704 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
705 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
706 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
707 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
709 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
710 case ISD::FP16_TO_FP32:
711 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
712 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
718 case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
721 // If the result is null, the sub-method took care of registering results etc.
722 if (!Res.getNode()) return false;
724 // If the result is N, the sub-method updated N in place. Tell the legalizer
726 if (Res.getNode() == N)
729 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
730 "Invalid operand expansion");
732 ReplaceValueWith(SDValue(N, 0), Res);
736 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
737 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
738 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
739 ISD::CondCode CCCode) {
740 // We have to insert explicit sign or zero extends. Note that we could
741 // insert sign extends for ALL conditions, but zero extend is cheaper on
742 // many machines (an AND instead of two shifts), so prefer it.
744 default: llvm_unreachable("Unknown integer comparison!");
751 // ALL of these operations will work if we either sign or zero extend
752 // the operands (including the unsigned comparisons!). Zero extend is
753 // usually a simpler/cheaper operation, so prefer it.
754 NewLHS = ZExtPromotedInteger(NewLHS);
755 NewRHS = ZExtPromotedInteger(NewRHS);
761 NewLHS = SExtPromotedInteger(NewLHS);
762 NewRHS = SExtPromotedInteger(NewRHS);
767 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
768 SDValue Op = GetPromotedInteger(N->getOperand(0));
769 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), N->getValueType(0), Op);
772 SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
773 // This should only occur in unusual situations like bitcasting to an
774 // x86_fp80, so just turn it into a store+load
775 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
778 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
779 assert(OpNo == 2 && "Don't know how to promote this operand!");
781 SDValue LHS = N->getOperand(2);
782 SDValue RHS = N->getOperand(3);
783 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
785 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
787 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
788 N->getOperand(1), LHS, RHS, N->getOperand(4)),
792 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
793 assert(OpNo == 1 && "only know how to promote condition");
795 // Promote all the way up to the canonical SetCC type.
796 EVT SVT = TLI.getSetCCResultType(MVT::Other);
797 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), SVT);
799 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
800 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
801 N->getOperand(2)), 0);
804 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
805 // Since the result type is legal, the operands must promote to it.
806 EVT OVT = N->getOperand(0).getValueType();
807 SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
808 SDValue Hi = GetPromotedInteger(N->getOperand(1));
809 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
810 DebugLoc dl = N->getDebugLoc();
812 Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
813 DAG.getConstant(OVT.getSizeInBits(), TLI.getPointerTy()));
814 return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
817 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
818 // The vector type is legal but the element type is not. This implies
819 // that the vector is a power-of-two in length and that the element
820 // type does not have a strange size (eg: it is not i1).
821 EVT VecVT = N->getValueType(0);
822 unsigned NumElts = VecVT.getVectorNumElements();
823 assert(!(NumElts & 1) && "Legal vector of one illegal element?");
825 // Promote the inserted value. The type does not need to match the
826 // vector element type. Check that any extra bits introduced will be
828 assert(N->getOperand(0).getValueType().getSizeInBits() >=
829 N->getValueType(0).getVectorElementType().getSizeInBits() &&
830 "Type of inserted value narrower than vector element type!");
832 SmallVector<SDValue, 16> NewOps;
833 for (unsigned i = 0; i < NumElts; ++i)
834 NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
836 return SDValue(DAG.UpdateNodeOperands(N, &NewOps[0], NumElts), 0);
839 SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) {
840 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
841 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
842 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
843 CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) &&
844 "can only promote integer arguments");
845 SDValue InOp = GetPromotedInteger(N->getOperand(0));
846 return DAG.getConvertRndSat(N->getValueType(0), N->getDebugLoc(), InOp,
847 N->getOperand(1), N->getOperand(2),
848 N->getOperand(3), N->getOperand(4), CvtCode);
851 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
854 // Promote the inserted value. This is valid because the type does not
855 // have to match the vector element type.
857 // Check that any extra bits introduced will be truncated away.
858 assert(N->getOperand(1).getValueType().getSizeInBits() >=
859 N->getValueType(0).getVectorElementType().getSizeInBits() &&
860 "Type of inserted value narrower than vector element type!");
861 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
862 GetPromotedInteger(N->getOperand(1)),
867 assert(OpNo == 2 && "Different operand and result vector types?");
869 // Promote the index.
870 SDValue Idx = ZExtPromotedInteger(N->getOperand(2));
871 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
872 N->getOperand(1), Idx), 0);
875 SDValue DAGTypeLegalizer::PromoteIntOp_MEMBARRIER(SDNode *N) {
877 DebugLoc dl = N->getDebugLoc();
878 NewOps[0] = N->getOperand(0);
879 for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
880 SDValue Flag = GetPromotedInteger(N->getOperand(i));
881 NewOps[i] = DAG.getZeroExtendInReg(Flag, dl, MVT::i1);
883 return SDValue(DAG.UpdateNodeOperands(N, NewOps, array_lengthof(NewOps)), 0);
886 SDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) {
887 // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote
888 // the operand in place.
889 return SDValue(DAG.UpdateNodeOperands(N,
890 GetPromotedInteger(N->getOperand(0))), 0);
893 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
894 assert(OpNo == 0 && "Only know how to promote condition");
896 // Promote all the way up to the canonical SetCC type.
897 EVT SVT = TLI.getSetCCResultType(N->getOperand(1).getValueType());
898 SDValue Cond = PromoteTargetBoolean(N->getOperand(0), SVT);
900 return SDValue(DAG.UpdateNodeOperands(N, Cond,
901 N->getOperand(1), N->getOperand(2)), 0);
904 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
905 assert(OpNo == 0 && "Don't know how to promote this operand!");
907 SDValue LHS = N->getOperand(0);
908 SDValue RHS = N->getOperand(1);
909 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
911 // The CC (#4) and the possible return values (#2 and #3) have legal types.
912 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
913 N->getOperand(3), N->getOperand(4)), 0);
916 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
917 assert(OpNo == 0 && "Don't know how to promote this operand!");
919 SDValue LHS = N->getOperand(0);
920 SDValue RHS = N->getOperand(1);
921 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
923 // The CC (#2) is always legal.
924 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
927 SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
928 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
929 ZExtPromotedInteger(N->getOperand(1))), 0);
932 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
933 SDValue Op = GetPromotedInteger(N->getOperand(0));
934 DebugLoc dl = N->getDebugLoc();
935 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
936 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
937 Op, DAG.getValueType(N->getOperand(0).getValueType()));
940 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
941 return SDValue(DAG.UpdateNodeOperands(N,
942 SExtPromotedInteger(N->getOperand(0))), 0);
945 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
946 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
947 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
948 unsigned Alignment = N->getAlignment();
949 bool isVolatile = N->isVolatile();
950 bool isNonTemporal = N->isNonTemporal();
951 DebugLoc dl = N->getDebugLoc();
953 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
955 // Truncate the value and store the result.
956 return DAG.getTruncStore(Ch, dl, Val, Ptr, N->getPointerInfo(),
958 isVolatile, isNonTemporal, Alignment);
961 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
962 SDValue Op = GetPromotedInteger(N->getOperand(0));
963 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), N->getValueType(0), Op);
966 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
967 return SDValue(DAG.UpdateNodeOperands(N,
968 ZExtPromotedInteger(N->getOperand(0))), 0);
971 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
972 DebugLoc dl = N->getDebugLoc();
973 SDValue Op = GetPromotedInteger(N->getOperand(0));
974 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
975 return DAG.getZeroExtendInReg(Op, dl, N->getOperand(0).getValueType());
979 //===----------------------------------------------------------------------===//
980 // Integer Result Expansion
981 //===----------------------------------------------------------------------===//
983 /// ExpandIntegerResult - This method is called when the specified result of the
984 /// specified node is found to need expansion. At this point, the node may also
985 /// have invalid operands or may have other results that need promotion, we just
986 /// know that (at least) one result needs expansion.
987 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
988 DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG); dbgs() << "\n");
992 // See if the target wants to custom expand this node.
993 if (CustomLowerNode(N, N->getValueType(ResNo), true))
996 switch (N->getOpcode()) {
999 dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
1000 N->dump(&DAG); dbgs() << "\n";
1002 llvm_unreachable("Do not know how to expand the result of this operator!");
1004 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
1005 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
1006 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
1007 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
1009 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
1010 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
1011 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
1012 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
1013 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
1015 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
1016 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
1017 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
1018 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
1019 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
1020 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
1021 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
1022 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
1023 case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
1024 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
1025 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
1026 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
1027 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
1028 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
1029 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
1030 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
1031 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
1032 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
1033 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
1034 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
1036 case ISD::ATOMIC_LOAD_ADD:
1037 case ISD::ATOMIC_LOAD_SUB:
1038 case ISD::ATOMIC_LOAD_AND:
1039 case ISD::ATOMIC_LOAD_OR:
1040 case ISD::ATOMIC_LOAD_XOR:
1041 case ISD::ATOMIC_LOAD_NAND:
1042 case ISD::ATOMIC_LOAD_MIN:
1043 case ISD::ATOMIC_LOAD_MAX:
1044 case ISD::ATOMIC_LOAD_UMIN:
1045 case ISD::ATOMIC_LOAD_UMAX:
1046 case ISD::ATOMIC_SWAP: {
1047 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
1048 SplitInteger(Tmp.first, Lo, Hi);
1049 ReplaceValueWith(SDValue(N, 1), Tmp.second);
1055 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
1058 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
1061 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
1064 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
1068 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
1071 case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
1073 case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
1076 // If Lo/Hi is null, the sub-method took care of registering results etc.
1078 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
1081 /// Lower an atomic node to the appropriate builtin call.
1082 std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
1083 unsigned Opc = Node->getOpcode();
1084 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
1089 llvm_unreachable("Unhandled atomic intrinsic Expand!");
1091 case ISD::ATOMIC_SWAP:
1092 switch (VT.SimpleTy) {
1093 default: llvm_unreachable("Unexpected value type for atomic!");
1094 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
1095 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
1096 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
1097 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
1100 case ISD::ATOMIC_CMP_SWAP:
1101 switch (VT.SimpleTy) {
1102 default: llvm_unreachable("Unexpected value type for atomic!");
1103 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
1104 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
1105 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
1106 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
1109 case ISD::ATOMIC_LOAD_ADD:
1110 switch (VT.SimpleTy) {
1111 default: llvm_unreachable("Unexpected value type for atomic!");
1112 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
1113 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
1114 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
1115 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
1118 case ISD::ATOMIC_LOAD_SUB:
1119 switch (VT.SimpleTy) {
1120 default: llvm_unreachable("Unexpected value type for atomic!");
1121 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
1122 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
1123 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
1124 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
1127 case ISD::ATOMIC_LOAD_AND:
1128 switch (VT.SimpleTy) {
1129 default: llvm_unreachable("Unexpected value type for atomic!");
1130 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
1131 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
1132 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
1133 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
1136 case ISD::ATOMIC_LOAD_OR:
1137 switch (VT.SimpleTy) {
1138 default: llvm_unreachable("Unexpected value type for atomic!");
1139 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
1140 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
1141 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
1142 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
1145 case ISD::ATOMIC_LOAD_XOR:
1146 switch (VT.SimpleTy) {
1147 default: llvm_unreachable("Unexpected value type for atomic!");
1148 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
1149 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
1150 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
1151 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
1154 case ISD::ATOMIC_LOAD_NAND:
1155 switch (VT.SimpleTy) {
1156 default: llvm_unreachable("Unexpected value type for atomic!");
1157 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
1158 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
1159 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
1160 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
1165 return ExpandChainLibCall(LC, Node, false);
1168 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
1169 /// and the shift amount is a constant 'Amt'. Expand the operation.
1170 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
1171 SDValue &Lo, SDValue &Hi) {
1172 DebugLoc DL = N->getDebugLoc();
1173 // Expand the incoming operand to be shifted, so that we have its parts
1175 GetExpandedInteger(N->getOperand(0), InL, InH);
1177 EVT NVT = InL.getValueType();
1178 unsigned VTBits = N->getValueType(0).getSizeInBits();
1179 unsigned NVTBits = NVT.getSizeInBits();
1180 EVT ShTy = N->getOperand(1).getValueType();
1182 if (N->getOpcode() == ISD::SHL) {
1184 Lo = Hi = DAG.getConstant(0, NVT);
1185 } else if (Amt > NVTBits) {
1186 Lo = DAG.getConstant(0, NVT);
1187 Hi = DAG.getNode(ISD::SHL, DL,
1188 NVT, InL, DAG.getConstant(Amt-NVTBits, ShTy));
1189 } else if (Amt == NVTBits) {
1190 Lo = DAG.getConstant(0, NVT);
1192 } else if (Amt == 1 &&
1193 TLI.isOperationLegalOrCustom(ISD::ADDC,
1194 TLI.getTypeToExpandTo(*DAG.getContext(), NVT))) {
1195 // Emit this X << 1 as X+X.
1196 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
1197 SDValue LoOps[2] = { InL, InL };
1198 Lo = DAG.getNode(ISD::ADDC, DL, VTList, LoOps, 2);
1199 SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
1200 Hi = DAG.getNode(ISD::ADDE, DL, VTList, HiOps, 3);
1202 Lo = DAG.getNode(ISD::SHL, DL, NVT, InL, DAG.getConstant(Amt, ShTy));
1203 Hi = DAG.getNode(ISD::OR, DL, NVT,
1204 DAG.getNode(ISD::SHL, DL, NVT, InH,
1205 DAG.getConstant(Amt, ShTy)),
1206 DAG.getNode(ISD::SRL, DL, NVT, InL,
1207 DAG.getConstant(NVTBits-Amt, ShTy)));
1212 if (N->getOpcode() == ISD::SRL) {
1214 Lo = DAG.getConstant(0, NVT);
1215 Hi = DAG.getConstant(0, NVT);
1216 } else if (Amt > NVTBits) {
1217 Lo = DAG.getNode(ISD::SRL, DL,
1218 NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
1219 Hi = DAG.getConstant(0, NVT);
1220 } else if (Amt == NVTBits) {
1222 Hi = DAG.getConstant(0, NVT);
1224 Lo = DAG.getNode(ISD::OR, DL, NVT,
1225 DAG.getNode(ISD::SRL, DL, NVT, InL,
1226 DAG.getConstant(Amt, ShTy)),
1227 DAG.getNode(ISD::SHL, DL, NVT, InH,
1228 DAG.getConstant(NVTBits-Amt, ShTy)));
1229 Hi = DAG.getNode(ISD::SRL, DL, NVT, InH, DAG.getConstant(Amt, ShTy));
1234 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1236 Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
1237 DAG.getConstant(NVTBits-1, ShTy));
1238 } else if (Amt > NVTBits) {
1239 Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
1240 DAG.getConstant(Amt-NVTBits, ShTy));
1241 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
1242 DAG.getConstant(NVTBits-1, ShTy));
1243 } else if (Amt == NVTBits) {
1245 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
1246 DAG.getConstant(NVTBits-1, ShTy));
1248 Lo = DAG.getNode(ISD::OR, DL, NVT,
1249 DAG.getNode(ISD::SRL, DL, NVT, InL,
1250 DAG.getConstant(Amt, ShTy)),
1251 DAG.getNode(ISD::SHL, DL, NVT, InH,
1252 DAG.getConstant(NVTBits-Amt, ShTy)));
1253 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, DAG.getConstant(Amt, ShTy));
1257 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1258 /// this shift based on knowledge of the high bit of the shift amount. If we
1259 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1261 bool DAGTypeLegalizer::
1262 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1263 SDValue Amt = N->getOperand(1);
1264 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1265 EVT ShTy = Amt.getValueType();
1266 unsigned ShBits = ShTy.getScalarType().getSizeInBits();
1267 unsigned NVTBits = NVT.getScalarType().getSizeInBits();
1268 assert(isPowerOf2_32(NVTBits) &&
1269 "Expanded integer type size not a power of two!");
1270 DebugLoc dl = N->getDebugLoc();
1272 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
1273 APInt KnownZero, KnownOne;
1274 DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
1276 // If we don't know anything about the high bits, exit.
1277 if (((KnownZero|KnownOne) & HighBitMask) == 0)
1280 // Get the incoming operand to be shifted.
1282 GetExpandedInteger(N->getOperand(0), InL, InH);
1284 // If we know that any of the high bits of the shift amount are one, then we
1285 // can do this as a couple of simple shifts.
1286 if (KnownOne.intersects(HighBitMask)) {
1287 // Mask out the high bit, which we know is set.
1288 Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
1289 DAG.getConstant(~HighBitMask, ShTy));
1291 switch (N->getOpcode()) {
1292 default: llvm_unreachable("Unknown shift");
1294 Lo = DAG.getConstant(0, NVT); // Low part is zero.
1295 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
1298 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
1299 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
1302 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
1303 DAG.getConstant(NVTBits-1, ShTy));
1304 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
1310 // FIXME: This code is broken for shifts with a zero amount!
1311 // If we know that all of the high bits of the shift amount are zero, then we
1312 // can do this as a couple of simple shifts.
1313 if ((KnownZero & HighBitMask) == HighBitMask) {
1315 SDValue Amt2 = DAG.getNode(ISD::SUB, ShTy,
1316 DAG.getConstant(NVTBits, ShTy),
1319 switch (N->getOpcode()) {
1320 default: llvm_unreachable("Unknown shift");
1321 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1323 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1326 Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
1327 Hi = DAG.getNode(ISD::OR, NVT,
1328 DAG.getNode(Op1, NVT, InH, Amt),
1329 DAG.getNode(Op2, NVT, InL, Amt2));
1337 /// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
1339 bool DAGTypeLegalizer::
1340 ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1341 SDValue Amt = N->getOperand(1);
1342 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1343 EVT ShTy = Amt.getValueType();
1344 unsigned NVTBits = NVT.getSizeInBits();
1345 assert(isPowerOf2_32(NVTBits) &&
1346 "Expanded integer type size not a power of two!");
1347 DebugLoc dl = N->getDebugLoc();
1349 // Get the incoming operand to be shifted.
1351 GetExpandedInteger(N->getOperand(0), InL, InH);
1353 SDValue NVBitsNode = DAG.getConstant(NVTBits, ShTy);
1354 SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
1355 SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
1356 SDValue isShort = DAG.getSetCC(dl, TLI.getSetCCResultType(ShTy),
1357 Amt, NVBitsNode, ISD::SETULT);
1359 SDValue LoS, HiS, LoL, HiL;
1360 switch (N->getOpcode()) {
1361 default: llvm_unreachable("Unknown shift");
1363 // Short: ShAmt < NVTBits
1364 LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
1365 HiS = DAG.getNode(ISD::OR, dl, NVT,
1366 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
1367 // FIXME: If Amt is zero, the following shift generates an undefined result
1368 // on some architectures.
1369 DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
1371 // Long: ShAmt >= NVTBits
1372 LoL = DAG.getConstant(0, NVT); // Lo part is zero.
1373 HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
1375 Lo = DAG.getNode(ISD::SELECT, dl, NVT, isShort, LoS, LoL);
1376 Hi = DAG.getNode(ISD::SELECT, dl, NVT, isShort, HiS, HiL);
1379 // Short: ShAmt < NVTBits
1380 HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
1381 LoS = DAG.getNode(ISD::OR, dl, NVT,
1382 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
1383 // FIXME: If Amt is zero, the following shift generates an undefined result
1384 // on some architectures.
1385 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
1387 // Long: ShAmt >= NVTBits
1388 HiL = DAG.getConstant(0, NVT); // Hi part is zero.
1389 LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
1391 Lo = DAG.getNode(ISD::SELECT, dl, NVT, isShort, LoS, LoL);
1392 Hi = DAG.getNode(ISD::SELECT, dl, NVT, isShort, HiS, HiL);
1395 // Short: ShAmt < NVTBits
1396 HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
1397 LoS = DAG.getNode(ISD::OR, dl, NVT,
1398 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
1399 // FIXME: If Amt is zero, the following shift generates an undefined result
1400 // on some architectures.
1401 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
1403 // Long: ShAmt >= NVTBits
1404 HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
1405 DAG.getConstant(NVTBits-1, ShTy));
1406 LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
1408 Lo = DAG.getNode(ISD::SELECT, dl, NVT, isShort, LoS, LoL);
1409 Hi = DAG.getNode(ISD::SELECT, dl, NVT, isShort, HiS, HiL);
1416 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
1417 SDValue &Lo, SDValue &Hi) {
1418 DebugLoc dl = N->getDebugLoc();
1419 // Expand the subcomponents.
1420 SDValue LHSL, LHSH, RHSL, RHSH;
1421 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1422 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1424 EVT NVT = LHSL.getValueType();
1425 SDValue LoOps[2] = { LHSL, RHSL };
1426 SDValue HiOps[3] = { LHSH, RHSH };
1428 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
1429 // them. TODO: Teach operation legalization how to expand unsupported
1430 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
1431 // a carry of type MVT::Glue, but there doesn't seem to be any way to
1432 // generate a value of this type in the expanded code sequence.
1434 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
1435 ISD::ADDC : ISD::SUBC,
1436 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
1439 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
1440 if (N->getOpcode() == ISD::ADD) {
1441 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
1442 HiOps[2] = Lo.getValue(1);
1443 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
1445 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
1446 HiOps[2] = Lo.getValue(1);
1447 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1452 if (N->getOpcode() == ISD::ADD) {
1453 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
1454 Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
1455 SDValue Cmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo, LoOps[0],
1457 SDValue Carry1 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp1,
1458 DAG.getConstant(1, NVT),
1459 DAG.getConstant(0, NVT));
1460 SDValue Cmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo, LoOps[1],
1462 SDValue Carry2 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp2,
1463 DAG.getConstant(1, NVT), Carry1);
1464 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
1466 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
1467 Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
1469 DAG.getSetCC(dl, TLI.getSetCCResultType(LoOps[0].getValueType()),
1470 LoOps[0], LoOps[1], ISD::SETULT);
1471 SDValue Borrow = DAG.getNode(ISD::SELECT, dl, NVT, Cmp,
1472 DAG.getConstant(1, NVT),
1473 DAG.getConstant(0, NVT));
1474 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
1478 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
1479 SDValue &Lo, SDValue &Hi) {
1480 // Expand the subcomponents.
1481 SDValue LHSL, LHSH, RHSL, RHSH;
1482 DebugLoc dl = N->getDebugLoc();
1483 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1484 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1485 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
1486 SDValue LoOps[2] = { LHSL, RHSL };
1487 SDValue HiOps[3] = { LHSH, RHSH };
1489 if (N->getOpcode() == ISD::ADDC) {
1490 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
1491 HiOps[2] = Lo.getValue(1);
1492 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
1494 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
1495 HiOps[2] = Lo.getValue(1);
1496 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1499 // Legalized the flag result - switch anything that used the old flag to
1501 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1504 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
1505 SDValue &Lo, SDValue &Hi) {
1506 // Expand the subcomponents.
1507 SDValue LHSL, LHSH, RHSL, RHSH;
1508 DebugLoc dl = N->getDebugLoc();
1509 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1510 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1511 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
1512 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
1513 SDValue HiOps[3] = { LHSH, RHSH };
1515 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps, 3);
1516 HiOps[2] = Lo.getValue(1);
1517 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps, 3);
1519 // Legalized the flag result - switch anything that used the old flag to
1521 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1524 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
1525 SDValue &Lo, SDValue &Hi) {
1526 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1527 DebugLoc dl = N->getDebugLoc();
1528 SDValue Op = N->getOperand(0);
1529 if (Op.getValueType().bitsLE(NVT)) {
1530 // The low part is any extension of the input (which degenerates to a copy).
1531 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
1532 Hi = DAG.getUNDEF(NVT); // The high part is undefined.
1534 // For example, extension of an i48 to an i64. The operand type necessarily
1535 // promotes to the result type, so will end up being expanded too.
1536 assert(getTypeAction(Op.getValueType()) ==
1537 TargetLowering::TypePromoteInteger &&
1538 "Only know how to promote this result!");
1539 SDValue Res = GetPromotedInteger(Op);
1540 assert(Res.getValueType() == N->getValueType(0) &&
1541 "Operand over promoted?");
1542 // Split the promoted operand. This will simplify when it is expanded.
1543 SplitInteger(Res, Lo, Hi);
1547 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
1548 SDValue &Lo, SDValue &Hi) {
1549 DebugLoc dl = N->getDebugLoc();
1550 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1551 EVT NVT = Lo.getValueType();
1552 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1553 unsigned NVTBits = NVT.getSizeInBits();
1554 unsigned EVTBits = EVT.getSizeInBits();
1556 if (NVTBits < EVTBits) {
1557 Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
1558 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
1559 EVTBits - NVTBits)));
1561 Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
1562 // The high part replicates the sign bit of Lo, make it explicit.
1563 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
1564 DAG.getConstant(NVTBits-1, TLI.getPointerTy()));
1568 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
1569 SDValue &Lo, SDValue &Hi) {
1570 DebugLoc dl = N->getDebugLoc();
1571 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1572 EVT NVT = Lo.getValueType();
1573 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1574 unsigned NVTBits = NVT.getSizeInBits();
1575 unsigned EVTBits = EVT.getSizeInBits();
1577 if (NVTBits < EVTBits) {
1578 Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
1579 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
1580 EVTBits - NVTBits)));
1582 Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
1583 // The high part must be zero, make it explicit.
1584 Hi = DAG.getConstant(0, NVT);
1588 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
1589 SDValue &Lo, SDValue &Hi) {
1590 DebugLoc dl = N->getDebugLoc();
1591 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
1592 Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
1593 Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
1596 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
1597 SDValue &Lo, SDValue &Hi) {
1598 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1599 unsigned NBitWidth = NVT.getSizeInBits();
1600 const APInt &Cst = cast<ConstantSDNode>(N)->getAPIntValue();
1601 Lo = DAG.getConstant(Cst.trunc(NBitWidth), NVT);
1602 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
1605 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
1606 SDValue &Lo, SDValue &Hi) {
1607 DebugLoc dl = N->getDebugLoc();
1608 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
1609 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1610 EVT NVT = Lo.getValueType();
1612 SDValue HiNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Hi,
1613 DAG.getConstant(0, NVT), ISD::SETNE);
1615 SDValue LoLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Lo);
1616 SDValue HiLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Hi);
1618 Lo = DAG.getNode(ISD::SELECT, dl, NVT, HiNotZero, HiLZ,
1619 DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
1620 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1621 Hi = DAG.getConstant(0, NVT);
1624 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
1625 SDValue &Lo, SDValue &Hi) {
1626 DebugLoc dl = N->getDebugLoc();
1627 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
1628 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1629 EVT NVT = Lo.getValueType();
1630 Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
1631 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
1632 Hi = DAG.getConstant(0, NVT);
1635 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
1636 SDValue &Lo, SDValue &Hi) {
1637 DebugLoc dl = N->getDebugLoc();
1638 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
1639 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1640 EVT NVT = Lo.getValueType();
1642 SDValue LoNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo,
1643 DAG.getConstant(0, NVT), ISD::SETNE);
1645 SDValue LoLZ = DAG.getNode(ISD::CTTZ, dl, NVT, Lo);
1646 SDValue HiLZ = DAG.getNode(ISD::CTTZ, dl, NVT, Hi);
1648 Lo = DAG.getNode(ISD::SELECT, dl, NVT, LoNotZero, LoLZ,
1649 DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
1650 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1651 Hi = DAG.getConstant(0, NVT);
1654 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
1656 DebugLoc dl = N->getDebugLoc();
1657 EVT VT = N->getValueType(0);
1658 SDValue Op = N->getOperand(0);
1659 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
1660 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
1661 SplitInteger(MakeLibCall(LC, VT, &Op, 1, true/*irrelevant*/, dl), Lo, Hi);
1664 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
1666 DebugLoc dl = N->getDebugLoc();
1667 EVT VT = N->getValueType(0);
1668 SDValue Op = N->getOperand(0);
1669 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
1670 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
1671 SplitInteger(MakeLibCall(LC, VT, &Op, 1, false/*irrelevant*/, dl), Lo, Hi);
1674 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
1675 SDValue &Lo, SDValue &Hi) {
1676 if (ISD::isNormalLoad(N)) {
1677 ExpandRes_NormalLoad(N, Lo, Hi);
1681 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1683 EVT VT = N->getValueType(0);
1684 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1685 SDValue Ch = N->getChain();
1686 SDValue Ptr = N->getBasePtr();
1687 ISD::LoadExtType ExtType = N->getExtensionType();
1688 unsigned Alignment = N->getAlignment();
1689 bool isVolatile = N->isVolatile();
1690 bool isNonTemporal = N->isNonTemporal();
1691 DebugLoc dl = N->getDebugLoc();
1693 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1695 if (N->getMemoryVT().bitsLE(NVT)) {
1696 EVT MemVT = N->getMemoryVT();
1698 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
1699 MemVT, isVolatile, isNonTemporal, Alignment);
1701 // Remember the chain.
1702 Ch = Lo.getValue(1);
1704 if (ExtType == ISD::SEXTLOAD) {
1705 // The high part is obtained by SRA'ing all but one of the bits of the
1707 unsigned LoSize = Lo.getValueType().getSizeInBits();
1708 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
1709 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
1710 } else if (ExtType == ISD::ZEXTLOAD) {
1711 // The high part is just a zero.
1712 Hi = DAG.getConstant(0, NVT);
1714 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
1715 // The high part is undefined.
1716 Hi = DAG.getUNDEF(NVT);
1718 } else if (TLI.isLittleEndian()) {
1719 // Little-endian - low bits are at low addresses.
1720 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(),
1721 isVolatile, isNonTemporal, Alignment);
1723 unsigned ExcessBits =
1724 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
1725 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
1727 // Increment the pointer to the other half.
1728 unsigned IncrementSize = NVT.getSizeInBits()/8;
1729 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
1730 DAG.getIntPtrConstant(IncrementSize));
1731 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
1732 N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
1733 isVolatile, isNonTemporal,
1734 MinAlign(Alignment, IncrementSize));
1736 // Build a factor node to remember that this load is independent of the
1738 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1741 // Big-endian - high bits are at low addresses. Favor aligned loads at
1742 // the cost of some bit-fiddling.
1743 EVT MemVT = N->getMemoryVT();
1744 unsigned EBytes = MemVT.getStoreSize();
1745 unsigned IncrementSize = NVT.getSizeInBits()/8;
1746 unsigned ExcessBits = (EBytes - IncrementSize)*8;
1748 // Load both the high bits and maybe some of the low bits.
1749 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
1750 EVT::getIntegerVT(*DAG.getContext(),
1751 MemVT.getSizeInBits() - ExcessBits),
1752 isVolatile, isNonTemporal, Alignment);
1754 // Increment the pointer to the other half.
1755 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
1756 DAG.getIntPtrConstant(IncrementSize));
1757 // Load the rest of the low bits.
1758 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
1759 N->getPointerInfo().getWithOffset(IncrementSize),
1760 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
1761 isVolatile, isNonTemporal,
1762 MinAlign(Alignment, IncrementSize));
1764 // Build a factor node to remember that this load is independent of the
1766 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1769 if (ExcessBits < NVT.getSizeInBits()) {
1770 // Transfer low bits from the bottom of Hi to the top of Lo.
1771 Lo = DAG.getNode(ISD::OR, dl, NVT, Lo,
1772 DAG.getNode(ISD::SHL, dl, NVT, Hi,
1773 DAG.getConstant(ExcessBits,
1774 TLI.getPointerTy())));
1775 // Move high bits to the right position in Hi.
1776 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl,
1778 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
1779 TLI.getPointerTy()));
1783 // Legalized the chain result - switch anything that used the old chain to
1785 ReplaceValueWith(SDValue(N, 1), Ch);
1788 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
1789 SDValue &Lo, SDValue &Hi) {
1790 DebugLoc dl = N->getDebugLoc();
1791 SDValue LL, LH, RL, RH;
1792 GetExpandedInteger(N->getOperand(0), LL, LH);
1793 GetExpandedInteger(N->getOperand(1), RL, RH);
1794 Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
1795 Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
1798 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
1799 SDValue &Lo, SDValue &Hi) {
1800 EVT VT = N->getValueType(0);
1801 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1802 DebugLoc dl = N->getDebugLoc();
1804 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
1805 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
1806 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
1807 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
1808 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1809 SDValue LL, LH, RL, RH;
1810 GetExpandedInteger(N->getOperand(0), LL, LH);
1811 GetExpandedInteger(N->getOperand(1), RL, RH);
1812 unsigned OuterBitSize = VT.getSizeInBits();
1813 unsigned InnerBitSize = NVT.getSizeInBits();
1814 unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1815 unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
1817 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
1818 if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
1819 DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
1820 // The inputs are both zero-extended.
1822 // We can emit a umul_lohi.
1823 Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
1824 Hi = SDValue(Lo.getNode(), 1);
1828 // We can emit a mulhu+mul.
1829 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1830 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
1834 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
1835 // The input values are both sign-extended.
1837 // We can emit a smul_lohi.
1838 Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
1839 Hi = SDValue(Lo.getNode(), 1);
1843 // We can emit a mulhs+mul.
1844 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1845 Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
1850 // Lo,Hi = umul LHS, RHS.
1851 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
1852 DAG.getVTList(NVT, NVT), LL, RL);
1854 Hi = UMulLOHI.getValue(1);
1855 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
1856 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
1857 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
1858 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
1862 Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1863 Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
1864 RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
1865 LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
1866 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
1867 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
1872 // If nothing else, we can make a libcall.
1873 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1875 LC = RTLIB::MUL_I16;
1876 else if (VT == MVT::i32)
1877 LC = RTLIB::MUL_I32;
1878 else if (VT == MVT::i64)
1879 LC = RTLIB::MUL_I64;
1880 else if (VT == MVT::i128)
1881 LC = RTLIB::MUL_I128;
1882 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
1884 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1885 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true/*irrelevant*/, dl), Lo, Hi);
1888 void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
1889 SDValue &Lo, SDValue &Hi) {
1890 SDValue LHS = Node->getOperand(0);
1891 SDValue RHS = Node->getOperand(1);
1892 DebugLoc dl = Node->getDebugLoc();
1894 // Expand the result by simply replacing it with the equivalent
1895 // non-overflow-checking operation.
1896 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
1897 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
1899 SplitInteger(Sum, Lo, Hi);
1901 // Compute the overflow.
1903 // LHSSign -> LHS >= 0
1904 // RHSSign -> RHS >= 0
1905 // SumSign -> Sum >= 0
1908 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
1910 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
1912 EVT OType = Node->getValueType(1);
1913 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
1915 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
1916 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
1917 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
1918 Node->getOpcode() == ISD::SADDO ?
1919 ISD::SETEQ : ISD::SETNE);
1921 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
1922 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
1924 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
1926 // Use the calculated overflow everywhere.
1927 ReplaceValueWith(SDValue(Node, 1), Cmp);
1930 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
1931 SDValue &Lo, SDValue &Hi) {
1932 EVT VT = N->getValueType(0);
1933 DebugLoc dl = N->getDebugLoc();
1935 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1937 LC = RTLIB::SDIV_I16;
1938 else if (VT == MVT::i32)
1939 LC = RTLIB::SDIV_I32;
1940 else if (VT == MVT::i64)
1941 LC = RTLIB::SDIV_I64;
1942 else if (VT == MVT::i128)
1943 LC = RTLIB::SDIV_I128;
1944 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1946 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1947 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true, dl), Lo, Hi);
1950 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
1951 SDValue &Lo, SDValue &Hi) {
1952 EVT VT = N->getValueType(0);
1953 DebugLoc dl = N->getDebugLoc();
1955 // If we can emit an efficient shift operation, do so now. Check to see if
1956 // the RHS is a constant.
1957 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1958 return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi);
1960 // If we can determine that the high bit of the shift is zero or one, even if
1961 // the low bits are variable, emit this shift in an optimized form.
1962 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
1965 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
1967 if (N->getOpcode() == ISD::SHL) {
1968 PartsOpc = ISD::SHL_PARTS;
1969 } else if (N->getOpcode() == ISD::SRL) {
1970 PartsOpc = ISD::SRL_PARTS;
1972 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1973 PartsOpc = ISD::SRA_PARTS;
1976 // Next check to see if the target supports this SHL_PARTS operation or if it
1977 // will custom expand it.
1978 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1979 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
1980 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
1981 Action == TargetLowering::Custom) {
1982 // Expand the subcomponents.
1984 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1986 SDValue Ops[] = { LHSL, LHSH, N->getOperand(1) };
1987 EVT VT = LHSL.getValueType();
1988 Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops, 3);
1989 Hi = Lo.getValue(1);
1993 // Otherwise, emit a libcall.
1994 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1996 if (N->getOpcode() == ISD::SHL) {
1997 isSigned = false; /*sign irrelevant*/
1999 LC = RTLIB::SHL_I16;
2000 else if (VT == MVT::i32)
2001 LC = RTLIB::SHL_I32;
2002 else if (VT == MVT::i64)
2003 LC = RTLIB::SHL_I64;
2004 else if (VT == MVT::i128)
2005 LC = RTLIB::SHL_I128;
2006 } else if (N->getOpcode() == ISD::SRL) {
2009 LC = RTLIB::SRL_I16;
2010 else if (VT == MVT::i32)
2011 LC = RTLIB::SRL_I32;
2012 else if (VT == MVT::i64)
2013 LC = RTLIB::SRL_I64;
2014 else if (VT == MVT::i128)
2015 LC = RTLIB::SRL_I128;
2017 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
2020 LC = RTLIB::SRA_I16;
2021 else if (VT == MVT::i32)
2022 LC = RTLIB::SRA_I32;
2023 else if (VT == MVT::i64)
2024 LC = RTLIB::SRA_I64;
2025 else if (VT == MVT::i128)
2026 LC = RTLIB::SRA_I128;
2029 if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) {
2030 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2031 SplitInteger(MakeLibCall(LC, VT, Ops, 2, isSigned, dl), Lo, Hi);
2035 if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
2036 llvm_unreachable("Unsupported shift!");
2039 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
2040 SDValue &Lo, SDValue &Hi) {
2041 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2042 DebugLoc dl = N->getDebugLoc();
2043 SDValue Op = N->getOperand(0);
2044 if (Op.getValueType().bitsLE(NVT)) {
2045 // The low part is sign extension of the input (degenerates to a copy).
2046 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
2047 // The high part is obtained by SRA'ing all but one of the bits of low part.
2048 unsigned LoSize = NVT.getSizeInBits();
2049 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
2050 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
2052 // For example, extension of an i48 to an i64. The operand type necessarily
2053 // promotes to the result type, so will end up being expanded too.
2054 assert(getTypeAction(Op.getValueType()) ==
2055 TargetLowering::TypePromoteInteger &&
2056 "Only know how to promote this result!");
2057 SDValue Res = GetPromotedInteger(Op);
2058 assert(Res.getValueType() == N->getValueType(0) &&
2059 "Operand over promoted?");
2060 // Split the promoted operand. This will simplify when it is expanded.
2061 SplitInteger(Res, Lo, Hi);
2062 unsigned ExcessBits =
2063 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
2064 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
2065 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
2070 void DAGTypeLegalizer::
2071 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
2072 DebugLoc dl = N->getDebugLoc();
2073 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2074 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2076 if (EVT.bitsLE(Lo.getValueType())) {
2077 // sext_inreg the low part if needed.
2078 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
2081 // The high part gets the sign extension from the lo-part. This handles
2082 // things like sextinreg V:i64 from i8.
2083 Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
2084 DAG.getConstant(Hi.getValueType().getSizeInBits()-1,
2085 TLI.getPointerTy()));
2087 // For example, extension of an i48 to an i64. Leave the low part alone,
2088 // sext_inreg the high part.
2089 unsigned ExcessBits =
2090 EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
2091 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
2092 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
2097 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
2098 SDValue &Lo, SDValue &Hi) {
2099 EVT VT = N->getValueType(0);
2100 DebugLoc dl = N->getDebugLoc();
2102 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2104 LC = RTLIB::SREM_I16;
2105 else if (VT == MVT::i32)
2106 LC = RTLIB::SREM_I32;
2107 else if (VT == MVT::i64)
2108 LC = RTLIB::SREM_I64;
2109 else if (VT == MVT::i128)
2110 LC = RTLIB::SREM_I128;
2111 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
2113 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2114 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true, dl), Lo, Hi);
2117 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
2118 SDValue &Lo, SDValue &Hi) {
2119 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2120 DebugLoc dl = N->getDebugLoc();
2121 Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
2122 Hi = DAG.getNode(ISD::SRL, dl,
2123 N->getOperand(0).getValueType(), N->getOperand(0),
2124 DAG.getConstant(NVT.getSizeInBits(), TLI.getPointerTy()));
2125 Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
2128 void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
2129 SDValue &Lo, SDValue &Hi) {
2130 SDValue LHS = N->getOperand(0);
2131 SDValue RHS = N->getOperand(1);
2132 DebugLoc dl = N->getDebugLoc();
2134 // Expand the result by simply replacing it with the equivalent
2135 // non-overflow-checking operation.
2136 SDValue Sum = DAG.getNode(N->getOpcode() == ISD::UADDO ?
2137 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
2139 SplitInteger(Sum, Lo, Hi);
2141 // Calculate the overflow: addition overflows iff a + b < a, and subtraction
2142 // overflows iff a - b > a.
2143 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS,
2144 N->getOpcode () == ISD::UADDO ?
2145 ISD::SETULT : ISD::SETUGT);
2147 // Use the calculated overflow everywhere.
2148 ReplaceValueWith(SDValue(N, 1), Ofl);
2151 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
2152 SDValue &Lo, SDValue &Hi) {
2153 EVT VT = N->getValueType(0);
2154 DebugLoc dl = N->getDebugLoc();
2156 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2158 LC = RTLIB::UDIV_I16;
2159 else if (VT == MVT::i32)
2160 LC = RTLIB::UDIV_I32;
2161 else if (VT == MVT::i64)
2162 LC = RTLIB::UDIV_I64;
2163 else if (VT == MVT::i128)
2164 LC = RTLIB::UDIV_I128;
2165 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
2167 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2168 SplitInteger(MakeLibCall(LC, VT, Ops, 2, false, dl), Lo, Hi);
2171 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
2172 SDValue &Lo, SDValue &Hi) {
2173 EVT VT = N->getValueType(0);
2174 DebugLoc dl = N->getDebugLoc();
2176 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2178 LC = RTLIB::UREM_I16;
2179 else if (VT == MVT::i32)
2180 LC = RTLIB::UREM_I32;
2181 else if (VT == MVT::i64)
2182 LC = RTLIB::UREM_I64;
2183 else if (VT == MVT::i128)
2184 LC = RTLIB::UREM_I128;
2185 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
2187 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2188 SplitInteger(MakeLibCall(LC, VT, Ops, 2, false, dl), Lo, Hi);
2191 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
2192 SDValue &Lo, SDValue &Hi) {
2193 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2194 DebugLoc dl = N->getDebugLoc();
2195 SDValue Op = N->getOperand(0);
2196 if (Op.getValueType().bitsLE(NVT)) {
2197 // The low part is zero extension of the input (degenerates to a copy).
2198 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
2199 Hi = DAG.getConstant(0, NVT); // The high part is just a zero.
2201 // For example, extension of an i48 to an i64. The operand type necessarily
2202 // promotes to the result type, so will end up being expanded too.
2203 assert(getTypeAction(Op.getValueType()) ==
2204 TargetLowering::TypePromoteInteger &&
2205 "Only know how to promote this result!");
2206 SDValue Res = GetPromotedInteger(Op);
2207 assert(Res.getValueType() == N->getValueType(0) &&
2208 "Operand over promoted?");
2209 // Split the promoted operand. This will simplify when it is expanded.
2210 SplitInteger(Res, Lo, Hi);
2211 unsigned ExcessBits =
2212 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
2213 Hi = DAG.getZeroExtendInReg(Hi, dl,
2214 EVT::getIntegerVT(*DAG.getContext(),
2220 //===----------------------------------------------------------------------===//
2221 // Integer Operand Expansion
2222 //===----------------------------------------------------------------------===//
2224 /// ExpandIntegerOperand - This method is called when the specified operand of
2225 /// the specified node is found to need expansion. At this point, all of the
2226 /// result types of the node are known to be legal, but other operands of the
2227 /// node may need promotion or expansion as well as the specified one.
2228 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
2229 DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG); dbgs() << "\n");
2230 SDValue Res = SDValue();
2232 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
2235 switch (N->getOpcode()) {
2238 dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
2239 N->dump(&DAG); dbgs() << "\n";
2241 llvm_unreachable("Do not know how to expand this operator's operand!");
2243 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
2244 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
2245 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
2246 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
2247 case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
2248 case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
2249 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
2250 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
2251 case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
2252 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
2253 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
2254 case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
2260 case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
2261 case ISD::RETURNADDR:
2262 case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break;
2265 // If the result is null, the sub-method took care of registering results etc.
2266 if (!Res.getNode()) return false;
2268 // If the result is N, the sub-method updated N in place. Tell the legalizer
2270 if (Res.getNode() == N)
2273 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
2274 "Invalid operand expansion");
2276 ReplaceValueWith(SDValue(N, 0), Res);
2280 /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
2281 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
2282 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
2284 ISD::CondCode &CCCode,
2286 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
2287 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
2288 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
2290 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
2291 if (RHSLo == RHSHi) {
2292 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
2293 if (RHSCST->isAllOnesValue()) {
2294 // Equality comparison to -1.
2295 NewLHS = DAG.getNode(ISD::AND, dl,
2296 LHSLo.getValueType(), LHSLo, LHSHi);
2303 NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
2304 NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
2305 NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
2306 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2310 // If this is a comparison of the sign bit, just look at the top part.
2312 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
2313 if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
2314 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
2320 // FIXME: This generated code sucks.
2321 ISD::CondCode LowCC;
2323 default: llvm_unreachable("Unknown integer setcc!");
2325 case ISD::SETULT: LowCC = ISD::SETULT; break;
2327 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2329 case ISD::SETULE: LowCC = ISD::SETULE; break;
2331 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2334 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2335 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2336 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2338 // NOTE: on targets without efficient SELECT of bools, we can always use
2339 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2340 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, true, NULL);
2342 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
2343 LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
2344 if (!Tmp1.getNode())
2345 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
2346 LHSLo, RHSLo, LowCC);
2347 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
2348 LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
2349 if (!Tmp2.getNode())
2350 Tmp2 = DAG.getNode(ISD::SETCC, dl,
2351 TLI.getSetCCResultType(LHSHi.getValueType()),
2352 LHSHi, RHSHi, DAG.getCondCode(CCCode));
2354 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
2355 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
2356 if ((Tmp1C && Tmp1C->isNullValue()) ||
2357 (Tmp2C && Tmp2C->isNullValue() &&
2358 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
2359 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
2360 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
2361 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
2362 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
2363 // low part is known false, returns high part.
2364 // For LE / GE, if high part is known false, ignore the low part.
2365 // For LT / GT, if high part is known true, ignore the low part.
2371 NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
2372 LHSHi, RHSHi, ISD::SETEQ, false,
2373 DagCombineInfo, dl);
2374 if (!NewLHS.getNode())
2375 NewLHS = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
2376 LHSHi, RHSHi, ISD::SETEQ);
2377 NewLHS = DAG.getNode(ISD::SELECT, dl, Tmp1.getValueType(),
2378 NewLHS, Tmp1, Tmp2);
2382 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
2383 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
2384 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
2385 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
2387 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2388 // against zero to select between true and false values.
2389 if (NewRHS.getNode() == 0) {
2390 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2391 CCCode = ISD::SETNE;
2394 // Update N to have the operands specified.
2395 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2396 DAG.getCondCode(CCCode), NewLHS, NewRHS,
2397 N->getOperand(4)), 0);
2400 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
2401 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2402 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
2403 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
2405 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2406 // against zero to select between true and false values.
2407 if (NewRHS.getNode() == 0) {
2408 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2409 CCCode = ISD::SETNE;
2412 // Update N to have the operands specified.
2413 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
2414 N->getOperand(2), N->getOperand(3),
2415 DAG.getCondCode(CCCode)), 0);
2418 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
2419 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2420 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
2421 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
2423 // If ExpandSetCCOperands returned a scalar, use it.
2424 if (NewRHS.getNode() == 0) {
2425 assert(NewLHS.getValueType() == N->getValueType(0) &&
2426 "Unexpected setcc expansion!");
2430 // Otherwise, update N to have the operands specified.
2431 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
2432 DAG.getCondCode(CCCode)), 0);
2435 SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
2436 // The value being shifted is legal, but the shift amount is too big.
2437 // It follows that either the result of the shift is undefined, or the
2438 // upper half of the shift amount is zero. Just use the lower half.
2440 GetExpandedInteger(N->getOperand(1), Lo, Hi);
2441 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
2444 SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
2445 // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
2446 // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
2447 // constant to valid type.
2449 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2450 return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
2453 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
2454 SDValue Op = N->getOperand(0);
2455 EVT DstVT = N->getValueType(0);
2456 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
2457 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2458 "Don't know how to expand this SINT_TO_FP!");
2459 return MakeLibCall(LC, DstVT, &Op, 1, true, N->getDebugLoc());
2462 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
2463 if (ISD::isNormalStore(N))
2464 return ExpandOp_NormalStore(N, OpNo);
2466 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2467 assert(OpNo == 1 && "Can only expand the stored value so far");
2469 EVT VT = N->getOperand(1).getValueType();
2470 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2471 SDValue Ch = N->getChain();
2472 SDValue Ptr = N->getBasePtr();
2473 unsigned Alignment = N->getAlignment();
2474 bool isVolatile = N->isVolatile();
2475 bool isNonTemporal = N->isNonTemporal();
2476 DebugLoc dl = N->getDebugLoc();
2479 assert(NVT.isByteSized() && "Expanded type not byte sized!");
2481 if (N->getMemoryVT().bitsLE(NVT)) {
2482 GetExpandedInteger(N->getValue(), Lo, Hi);
2483 return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
2484 N->getMemoryVT(), isVolatile, isNonTemporal,
2488 if (TLI.isLittleEndian()) {
2489 // Little-endian - low bits are at low addresses.
2490 GetExpandedInteger(N->getValue(), Lo, Hi);
2492 Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
2493 isVolatile, isNonTemporal, Alignment);
2495 unsigned ExcessBits =
2496 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
2497 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
2499 // Increment the pointer to the other half.
2500 unsigned IncrementSize = NVT.getSizeInBits()/8;
2501 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
2502 DAG.getIntPtrConstant(IncrementSize));
2503 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr,
2504 N->getPointerInfo().getWithOffset(IncrementSize),
2505 NEVT, isVolatile, isNonTemporal,
2506 MinAlign(Alignment, IncrementSize));
2507 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
2510 // Big-endian - high bits are at low addresses. Favor aligned stores at
2511 // the cost of some bit-fiddling.
2512 GetExpandedInteger(N->getValue(), Lo, Hi);
2514 EVT ExtVT = N->getMemoryVT();
2515 unsigned EBytes = ExtVT.getStoreSize();
2516 unsigned IncrementSize = NVT.getSizeInBits()/8;
2517 unsigned ExcessBits = (EBytes - IncrementSize)*8;
2518 EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
2519 ExtVT.getSizeInBits() - ExcessBits);
2521 if (ExcessBits < NVT.getSizeInBits()) {
2522 // Transfer high bits from the top of Lo to the bottom of Hi.
2523 Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
2524 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
2525 TLI.getPointerTy()));
2526 Hi = DAG.getNode(ISD::OR, dl, NVT, Hi,
2527 DAG.getNode(ISD::SRL, dl, NVT, Lo,
2528 DAG.getConstant(ExcessBits,
2529 TLI.getPointerTy())));
2532 // Store both the high bits and maybe some of the low bits.
2533 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(),
2534 HiVT, isVolatile, isNonTemporal, Alignment);
2536 // Increment the pointer to the other half.
2537 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
2538 DAG.getIntPtrConstant(IncrementSize));
2539 // Store the lowest ExcessBits bits in the second half.
2540 Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
2541 N->getPointerInfo().getWithOffset(IncrementSize),
2542 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
2543 isVolatile, isNonTemporal,
2544 MinAlign(Alignment, IncrementSize));
2545 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
2548 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
2550 GetExpandedInteger(N->getOperand(0), InL, InH);
2551 // Just truncate the low part of the source.
2552 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), N->getValueType(0), InL);
2555 static const fltSemantics *EVTToAPFloatSemantics(EVT VT) {
2556 switch (VT.getSimpleVT().SimpleTy) {
2557 default: llvm_unreachable("Unknown FP format");
2558 case MVT::f32: return &APFloat::IEEEsingle;
2559 case MVT::f64: return &APFloat::IEEEdouble;
2560 case MVT::f80: return &APFloat::x87DoubleExtended;
2561 case MVT::f128: return &APFloat::IEEEquad;
2562 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
2566 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
2567 SDValue Op = N->getOperand(0);
2568 EVT SrcVT = Op.getValueType();
2569 EVT DstVT = N->getValueType(0);
2570 DebugLoc dl = N->getDebugLoc();
2572 // The following optimization is valid only if every value in SrcVT (when
2573 // treated as signed) is representable in DstVT. Check that the mantissa
2574 // size of DstVT is >= than the number of bits in SrcVT -1.
2575 const fltSemantics *sem = EVTToAPFloatSemantics(DstVT);
2576 if (APFloat::semanticsPrecision(*sem) >= SrcVT.getSizeInBits()-1 &&
2577 TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
2578 // Do a signed conversion then adjust the result.
2579 SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op);
2580 SignedConv = TLI.LowerOperation(SignedConv, DAG);
2582 // The result of the signed conversion needs adjusting if the 'sign bit' of
2583 // the incoming integer was set. To handle this, we dynamically test to see
2584 // if it is set, and, if so, add a fudge factor.
2586 const uint64_t F32TwoE32 = 0x4F800000ULL;
2587 const uint64_t F32TwoE64 = 0x5F800000ULL;
2588 const uint64_t F32TwoE128 = 0x7F800000ULL;
2591 if (SrcVT == MVT::i32)
2592 FF = APInt(32, F32TwoE32);
2593 else if (SrcVT == MVT::i64)
2594 FF = APInt(32, F32TwoE64);
2595 else if (SrcVT == MVT::i128)
2596 FF = APInt(32, F32TwoE128);
2598 assert(false && "Unsupported UINT_TO_FP!");
2600 // Check whether the sign bit is set.
2602 GetExpandedInteger(Op, Lo, Hi);
2603 SDValue SignSet = DAG.getSetCC(dl,
2604 TLI.getSetCCResultType(Hi.getValueType()),
2605 Hi, DAG.getConstant(0, Hi.getValueType()),
2608 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
2609 SDValue FudgePtr = DAG.getConstantPool(
2610 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
2611 TLI.getPointerTy());
2613 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
2614 SDValue Zero = DAG.getIntPtrConstant(0);
2615 SDValue Four = DAG.getIntPtrConstant(4);
2616 if (TLI.isBigEndian()) std::swap(Zero, Four);
2617 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
2619 unsigned Alignment = cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
2620 FudgePtr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), FudgePtr, Offset);
2621 Alignment = std::min(Alignment, 4u);
2623 // Load the value out, extending it from f32 to the destination float type.
2624 // FIXME: Avoid the extend by constructing the right constant pool?
2625 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(),
2627 MachinePointerInfo::getConstantPool(),
2629 false, false, Alignment);
2630 return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge);
2633 // Otherwise, use a libcall.
2634 RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
2635 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2636 "Don't know how to expand this UINT_TO_FP!");
2637 return MakeLibCall(LC, DstVT, &Op, 1, true, dl);
2640 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
2641 SDValue InOp0 = N->getOperand(0);
2642 EVT InVT = InOp0.getValueType();
2643 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
2645 EVT OutVT = N->getValueType(0);
2646 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
2647 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
2648 unsigned OutNumElems = N->getValueType(0).getVectorNumElements();
2649 EVT NOutVTElem = NOutVT.getVectorElementType();
2651 DebugLoc dl = N->getDebugLoc();
2652 SDValue BaseIdx = N->getOperand(1);
2654 SmallVector<SDValue, 8> Ops;
2655 for (unsigned i = 0; i != OutNumElems; ++i) {
2657 // Extract the element from the original vector.
2658 SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(),
2659 BaseIdx, DAG.getIntPtrConstant(i));
2660 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
2661 InVT.getVectorElementType(), N->getOperand(0), Index);
2663 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, Ext);
2664 // Insert the converted element to the new vector.
2668 return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, &Ops[0], Ops.size());
2672 SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
2674 ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
2675 EVT VT = N->getValueType(0);
2676 DebugLoc dl = N->getDebugLoc();
2678 unsigned NumElts = VT.getVectorNumElements();
2679 SmallVector<int, 8> NewMask;
2680 for (unsigned i = 0; i != NumElts; ++i) {
2681 NewMask.push_back(SV->getMaskElt(i));
2684 SDValue V0 = GetPromotedInteger(N->getOperand(0));
2685 SDValue V1 = GetPromotedInteger(N->getOperand(1));
2686 EVT OutVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2688 return DAG.getVectorShuffle(OutVT, dl, V0,V1, &NewMask[0]);
2692 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
2694 SDValue InOp0 = N->getOperand(0);
2695 EVT InVT = InOp0.getValueType();
2696 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
2698 EVT OutVT = N->getValueType(0);
2699 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
2700 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
2701 unsigned NumElems = N->getNumOperands();
2702 EVT NOutVTElem = NOutVT.getVectorElementType();
2704 DebugLoc dl = N->getDebugLoc();
2706 SmallVector<SDValue, 8> Ops;
2707 for (unsigned i = 0; i != NumElems; ++i) {
2708 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(i));
2712 return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, &Ops[0], Ops.size());
2715 SDValue DAGTypeLegalizer::PromoteIntRes_SCALAR_TO_VECTOR(SDNode *N) {
2717 DebugLoc dl = N->getDebugLoc();
2719 SDValue InOp0 = N->getOperand(0);
2720 EVT InVT = InOp0.getValueType();
2721 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
2722 assert(!InVT.isVector() && "Input must not be a scalar");
2724 EVT OutVT = N->getValueType(0);
2725 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
2726 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
2727 EVT NOutVTElem = NOutVT.getVectorElementType();
2729 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(0));
2731 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NOutVT, Op);
2734 SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
2736 SDValue InOp0 = N->getOperand(0);
2737 EVT InVT = InOp0.getValueType();
2738 EVT InElVT = InVT.getVectorElementType();
2739 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
2741 EVT OutVT = N->getValueType(0);
2742 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
2743 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
2745 EVT NOutVTElem = NOutVT.getVectorElementType();
2747 DebugLoc dl = N->getDebugLoc();
2749 SDValue ConvertedVector = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, InOp0);
2751 SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
2752 NOutVTElem, N->getOperand(1));
2753 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,NOutVT,
2754 ConvertedVector, ConvElem, N->getOperand(2));
2757 SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
2758 DebugLoc dl = N->getDebugLoc();
2759 SDValue V0 = GetPromotedInteger(N->getOperand(0));
2760 SDValue V1 = N->getOperand(1);
2761 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
2762 V0->getValueType(0).getScalarType(), V0, V1);
2764 return DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), Ext);
2768 SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
2770 DebugLoc dl = N->getDebugLoc();
2772 EVT RetSclrTy = N->getValueType(0).getVectorElementType();
2774 SmallVector<SDValue, 8> NewOps;
2776 // For each incoming vector
2777 for (unsigned VecIdx = 0, E = N->getNumOperands(); VecIdx!= E; ++VecIdx) {
2778 SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
2779 EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
2780 unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
2782 for (unsigned i=0; i<NumElem; ++i) {
2783 // Extract element from incoming vector
2784 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy,
2785 Incoming, DAG.getIntPtrConstant(i));
2786 SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
2787 NewOps.push_back(Tr);
2791 return DAG.getNode(ISD::BUILD_VECTOR, dl, N->getValueType(0),
2792 &NewOps[0], NewOps.size());