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"
24 //===----------------------------------------------------------------------===//
25 // Integer Result Promotion
26 //===----------------------------------------------------------------------===//
28 /// PromoteIntegerResult - This method is called when a result of a node is
29 /// found to be in need of promotion to a larger type. At this point, the node
30 /// may also have invalid operands or may have other results that need
31 /// expansion, we just know that (at least) one result needs promotion.
32 void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
33 DEBUG(cerr << "Promote integer result: "; N->dump(&DAG); cerr << "\n");
34 SDValue Result = SDValue();
36 // See if the target wants to custom expand this node.
37 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
38 TargetLowering::Custom) {
39 // If the target wants to, allow it to lower this itself.
40 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
41 // Everything that once used N now uses P. We are guaranteed that the
42 // result value types of N and the result value types of P match.
43 ReplaceNodeWith(N, P);
48 switch (N->getOpcode()) {
51 cerr << "PromoteIntegerResult #" << ResNo << ": ";
52 N->dump(&DAG); cerr << "\n";
54 assert(0 && "Do not know how to promote this operator!");
56 case ISD::AssertSext: Result = PromoteIntRes_AssertSext(N); break;
57 case ISD::AssertZext: Result = PromoteIntRes_AssertZext(N); break;
58 case ISD::BIT_CONVERT: Result = PromoteIntRes_BIT_CONVERT(N); break;
59 case ISD::BSWAP: Result = PromoteIntRes_BSWAP(N); break;
60 case ISD::BUILD_PAIR: Result = PromoteIntRes_BUILD_PAIR(N); break;
61 case ISD::Constant: Result = PromoteIntRes_Constant(N); break;
62 case ISD::CONVERT_RNDSAT:
63 Result = PromoteIntRes_CONVERT_RNDSAT(N); break;
64 case ISD::CTLZ: Result = PromoteIntRes_CTLZ(N); break;
65 case ISD::CTPOP: Result = PromoteIntRes_CTPOP(N); break;
66 case ISD::CTTZ: Result = PromoteIntRes_CTTZ(N); break;
67 case ISD::EXTRACT_VECTOR_ELT:
68 Result = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
69 case ISD::LOAD: Result = PromoteIntRes_LOAD(cast<LoadSDNode>(N));break;
70 case ISD::SELECT: Result = PromoteIntRes_SELECT(N); break;
71 case ISD::SELECT_CC: Result = PromoteIntRes_SELECT_CC(N); break;
72 case ISD::SETCC: Result = PromoteIntRes_SETCC(N); break;
73 case ISD::SHL: Result = PromoteIntRes_SHL(N); break;
74 case ISD::SIGN_EXTEND_INREG:
75 Result = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
76 case ISD::SRA: Result = PromoteIntRes_SRA(N); break;
77 case ISD::SRL: Result = PromoteIntRes_SRL(N); break;
78 case ISD::TRUNCATE: Result = PromoteIntRes_TRUNCATE(N); break;
79 case ISD::UNDEF: Result = PromoteIntRes_UNDEF(N); break;
80 case ISD::VAARG: Result = PromoteIntRes_VAARG(N); break;
82 case ISD::SIGN_EXTEND:
83 case ISD::ZERO_EXTEND:
84 case ISD::ANY_EXTEND: Result = PromoteIntRes_INT_EXTEND(N); break;
87 case ISD::FP_TO_UINT: Result = PromoteIntRes_FP_TO_XINT(N); break;
94 case ISD::MUL: Result = PromoteIntRes_SimpleIntBinOp(N); break;
97 case ISD::SREM: Result = PromoteIntRes_SDIV(N); break;
100 case ISD::UREM: Result = PromoteIntRes_UDIV(N); break;
102 case ISD::ATOMIC_LOAD_ADD_8:
103 case ISD::ATOMIC_LOAD_SUB_8:
104 case ISD::ATOMIC_LOAD_AND_8:
105 case ISD::ATOMIC_LOAD_OR_8:
106 case ISD::ATOMIC_LOAD_XOR_8:
107 case ISD::ATOMIC_LOAD_NAND_8:
108 case ISD::ATOMIC_LOAD_MIN_8:
109 case ISD::ATOMIC_LOAD_MAX_8:
110 case ISD::ATOMIC_LOAD_UMIN_8:
111 case ISD::ATOMIC_LOAD_UMAX_8:
112 case ISD::ATOMIC_SWAP_8:
113 case ISD::ATOMIC_LOAD_ADD_16:
114 case ISD::ATOMIC_LOAD_SUB_16:
115 case ISD::ATOMIC_LOAD_AND_16:
116 case ISD::ATOMIC_LOAD_OR_16:
117 case ISD::ATOMIC_LOAD_XOR_16:
118 case ISD::ATOMIC_LOAD_NAND_16:
119 case ISD::ATOMIC_LOAD_MIN_16:
120 case ISD::ATOMIC_LOAD_MAX_16:
121 case ISD::ATOMIC_LOAD_UMIN_16:
122 case ISD::ATOMIC_LOAD_UMAX_16:
123 case ISD::ATOMIC_SWAP_16:
124 case ISD::ATOMIC_LOAD_ADD_32:
125 case ISD::ATOMIC_LOAD_SUB_32:
126 case ISD::ATOMIC_LOAD_AND_32:
127 case ISD::ATOMIC_LOAD_OR_32:
128 case ISD::ATOMIC_LOAD_XOR_32:
129 case ISD::ATOMIC_LOAD_NAND_32:
130 case ISD::ATOMIC_LOAD_MIN_32:
131 case ISD::ATOMIC_LOAD_MAX_32:
132 case ISD::ATOMIC_LOAD_UMIN_32:
133 case ISD::ATOMIC_LOAD_UMAX_32:
134 case ISD::ATOMIC_SWAP_32:
135 case ISD::ATOMIC_LOAD_ADD_64:
136 case ISD::ATOMIC_LOAD_SUB_64:
137 case ISD::ATOMIC_LOAD_AND_64:
138 case ISD::ATOMIC_LOAD_OR_64:
139 case ISD::ATOMIC_LOAD_XOR_64:
140 case ISD::ATOMIC_LOAD_NAND_64:
141 case ISD::ATOMIC_LOAD_MIN_64:
142 case ISD::ATOMIC_LOAD_MAX_64:
143 case ISD::ATOMIC_LOAD_UMIN_64:
144 case ISD::ATOMIC_LOAD_UMAX_64:
145 case ISD::ATOMIC_SWAP_64:
146 Result = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
148 case ISD::ATOMIC_CMP_SWAP_8:
149 case ISD::ATOMIC_CMP_SWAP_16:
150 case ISD::ATOMIC_CMP_SWAP_32:
151 case ISD::ATOMIC_CMP_SWAP_64:
152 Result = PromoteIntRes_Atomic2(cast<AtomicSDNode>(N)); break;
155 // If Result is null, the sub-method took care of registering the result.
156 if (Result.getNode())
157 SetPromotedInteger(SDValue(N, ResNo), Result);
160 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
161 // Sign-extend the new bits, and continue the assertion.
162 MVT OldVT = N->getValueType(0);
163 SDValue Op = GetPromotedInteger(N->getOperand(0));
164 return DAG.getNode(ISD::AssertSext, Op.getValueType(),
165 DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(), Op,
166 DAG.getValueType(OldVT)), N->getOperand(1));
169 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
170 // Zero the new bits, and continue the assertion.
171 MVT OldVT = N->getValueType(0);
172 SDValue Op = GetPromotedInteger(N->getOperand(0));
173 return DAG.getNode(ISD::AssertZext, Op.getValueType(),
174 DAG.getZeroExtendInReg(Op, OldVT), N->getOperand(1));
177 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
178 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
179 SDValue Res = DAG.getAtomic(N->getOpcode(), N->getChain(), N->getBasePtr(),
180 Op2, N->getSrcValue(), N->getAlignment());
181 // Legalized the chain result - switch anything that used the old chain to
183 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
187 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic2(AtomicSDNode *N) {
188 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
189 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
190 SDValue Res = DAG.getAtomic(N->getOpcode(), N->getChain(), N->getBasePtr(),
191 Op2, Op3, N->getSrcValue(), N->getAlignment());
192 // Legalized the chain result - switch anything that used the old chain to
194 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
198 SDValue DAGTypeLegalizer::PromoteIntRes_BIT_CONVERT(SDNode *N) {
199 SDValue InOp = N->getOperand(0);
200 MVT InVT = InOp.getValueType();
201 MVT NInVT = TLI.getTypeToTransformTo(InVT);
202 MVT OutVT = TLI.getTypeToTransformTo(N->getValueType(0));
204 switch (getTypeAction(InVT)) {
206 assert(false && "Unknown type action!");
211 if (OutVT.bitsEq(NInVT))
212 // The input promotes to the same size. Convert the promoted value.
213 return DAG.getNode(ISD::BIT_CONVERT, OutVT, GetPromotedInteger(InOp));
216 // Promote the integer operand by hand.
217 return DAG.getNode(ISD::ANY_EXTEND, OutVT, GetSoftenedFloat(InOp));
221 case ScalarizeVector:
222 // Convert the element to an integer and promote it by hand.
223 return DAG.getNode(ISD::ANY_EXTEND, OutVT,
224 BitConvertToInteger(GetScalarizedVector(InOp)));
226 // For example, i32 = BIT_CONVERT v2i16 on alpha. Convert the split
227 // pieces of the input into integers and reassemble in the final type.
229 GetSplitVector(N->getOperand(0), Lo, Hi);
230 Lo = BitConvertToInteger(Lo);
231 Hi = BitConvertToInteger(Hi);
233 if (TLI.isBigEndian())
236 InOp = DAG.getNode(ISD::ANY_EXTEND,
237 MVT::getIntegerVT(OutVT.getSizeInBits()),
238 JoinIntegers(Lo, Hi));
239 return DAG.getNode(ISD::BIT_CONVERT, OutVT, InOp);
242 // Otherwise, lower the bit-convert to a store/load from the stack, then
244 SDValue Op = CreateStackStoreLoad(InOp, N->getValueType(0));
245 return PromoteIntRes_LOAD(cast<LoadSDNode>(Op.getNode()));
248 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
249 SDValue Op = GetPromotedInteger(N->getOperand(0));
250 MVT OVT = N->getValueType(0);
251 MVT NVT = Op.getValueType();
253 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
254 return DAG.getNode(ISD::SRL, NVT, DAG.getNode(ISD::BSWAP, NVT, Op),
255 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
258 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
259 // The pair element type may be legal, or may not promote to the same type as
260 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
261 return DAG.getNode(ISD::ANY_EXTEND,
262 TLI.getTypeToTransformTo(N->getValueType(0)),
263 JoinIntegers(N->getOperand(0), N->getOperand(1)));
266 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
267 MVT VT = N->getValueType(0);
268 // Zero extend things like i1, sign extend everything else. It shouldn't
269 // matter in theory which one we pick, but this tends to give better code?
270 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
271 SDValue Result = DAG.getNode(Opc, TLI.getTypeToTransformTo(VT),
273 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
277 SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) {
278 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
279 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
280 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
281 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
282 "can only promote integers");
283 SDValue InOp = N->getOperand(0);
285 MVT InVT = InOp.getValueType();
286 MVT OutVT = TLI.getTypeToTransformTo(N->getValueType(0));
287 switch (getTypeAction(InVT)) {
289 assert(false && "Unknown type action!");
294 return DAG.getConvertRndSat(OutVT, GetPromotedInteger(InOp),
295 N->getOperand(1), N->getOperand(2),
296 N->getOperand(3), N->getOperand(4), CvtCode);
302 case ScalarizeVector:
304 assert(false && "can not convert a vector to a scalar!");
306 return DAG.getConvertRndSat(OutVT, InOp,
307 N->getOperand(1), N->getOperand(2),
308 N->getOperand(3), N->getOperand(4), CvtCode);
312 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
313 SDValue Op = GetPromotedInteger(N->getOperand(0));
314 MVT OVT = N->getValueType(0);
315 MVT NVT = Op.getValueType();
316 // Zero extend to the promoted type and do the count there.
317 Op = DAG.getNode(ISD::CTLZ, NVT, DAG.getZeroExtendInReg(Op, OVT));
318 // Subtract off the extra leading bits in the bigger type.
319 return DAG.getNode(ISD::SUB, NVT, Op,
320 DAG.getConstant(NVT.getSizeInBits() -
321 OVT.getSizeInBits(), NVT));
324 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
325 SDValue Op = GetPromotedInteger(N->getOperand(0));
326 MVT OVT = N->getValueType(0);
327 MVT NVT = Op.getValueType();
328 // Zero extend to the promoted type and do the count there.
329 return DAG.getNode(ISD::CTPOP, NVT, DAG.getZeroExtendInReg(Op, OVT));
332 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
333 SDValue Op = GetPromotedInteger(N->getOperand(0));
334 MVT OVT = N->getValueType(0);
335 MVT NVT = Op.getValueType();
336 // The count is the same in the promoted type except if the original
337 // value was zero. This can be handled by setting the bit just off
338 // the top of the original type.
339 APInt TopBit(NVT.getSizeInBits(), 0);
340 TopBit.set(OVT.getSizeInBits());
341 Op = DAG.getNode(ISD::OR, NVT, Op, DAG.getConstant(TopBit, NVT));
342 return DAG.getNode(ISD::CTTZ, NVT, Op);
345 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
346 MVT OldVT = N->getValueType(0);
347 SDValue OldVec = N->getOperand(0);
348 unsigned OldElts = OldVec.getValueType().getVectorNumElements();
351 assert(!isTypeLegal(OldVec.getValueType()) &&
352 "Legal one-element vector of a type needing promotion!");
353 // It is tempting to follow GetScalarizedVector by a call to
354 // GetPromotedInteger, but this would be wrong because the
355 // scalarized value may not yet have been processed.
356 return DAG.getNode(ISD::ANY_EXTEND, TLI.getTypeToTransformTo(OldVT),
357 GetScalarizedVector(OldVec));
360 // Convert to a vector half as long with an element type of twice the width,
361 // for example <4 x i16> -> <2 x i32>.
362 assert(!(OldElts & 1) && "Odd length vectors not supported!");
363 MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
364 assert(OldVT.isSimple() && NewVT.isSimple());
366 SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT,
367 MVT::getVectorVT(NewVT, OldElts / 2),
370 // Extract the element at OldIdx / 2 from the new vector.
371 SDValue OldIdx = N->getOperand(1);
372 SDValue NewIdx = DAG.getNode(ISD::SRL, OldIdx.getValueType(), OldIdx,
373 DAG.getConstant(1, TLI.getShiftAmountTy()));
374 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, NewIdx);
376 // Select the appropriate half of the element: Lo if OldIdx was even,
379 SDValue Hi = DAG.getNode(ISD::SRL, NewVT, Elt,
380 DAG.getConstant(OldVT.getSizeInBits(),
381 TLI.getShiftAmountTy()));
382 if (TLI.isBigEndian())
385 SDValue Odd = DAG.getNode(ISD::TRUNCATE, MVT::i1, OldIdx);
386 return DAG.getNode(ISD::SELECT, NewVT, Odd, Hi, Lo);
389 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
390 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
391 unsigned NewOpc = N->getOpcode();
393 // If we're promoting a UINT to a larger size, check to see if the new node
394 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
395 // we can use that instead. This allows us to generate better code for
396 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
397 // legal, such as PowerPC.
398 if (N->getOpcode() == ISD::FP_TO_UINT &&
399 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
400 TLI.isOperationLegal(ISD::FP_TO_SINT, NVT))
401 NewOpc = ISD::FP_TO_SINT;
403 SDValue Res = DAG.getNode(NewOpc, NVT, N->getOperand(0));
405 // Assert that the converted value fits in the original type. If it doesn't
406 // (eg: because the value being converted is too big), then the result of the
407 // original operation was undefined anyway, so the assert is still correct.
408 return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ?
409 ISD::AssertZext : ISD::AssertSext,
410 NVT, Res, DAG.getValueType(N->getValueType(0)));
413 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
414 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
416 if (getTypeAction(N->getOperand(0).getValueType()) == PromoteInteger) {
417 SDValue Res = GetPromotedInteger(N->getOperand(0));
418 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
420 // If the result and operand types are the same after promotion, simplify
421 // to an in-register extension.
422 if (NVT == Res.getValueType()) {
423 // The high bits are not guaranteed to be anything. Insert an extend.
424 if (N->getOpcode() == ISD::SIGN_EXTEND)
425 return DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res,
426 DAG.getValueType(N->getOperand(0).getValueType()));
427 if (N->getOpcode() == ISD::ZERO_EXTEND)
428 return DAG.getZeroExtendInReg(Res, N->getOperand(0).getValueType());
429 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
434 // Otherwise, just extend the original operand all the way to the larger type.
435 return DAG.getNode(N->getOpcode(), NVT, N->getOperand(0));
438 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
439 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
440 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
441 ISD::LoadExtType ExtType =
442 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
443 SDValue Res = DAG.getExtLoad(ExtType, NVT, N->getChain(), N->getBasePtr(),
444 N->getSrcValue(), N->getSrcValueOffset(),
445 N->getMemoryVT(), N->isVolatile(),
448 // Legalized the chain result - switch anything that used the old chain to
450 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
454 SDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) {
455 // Sign extend the input.
456 SDValue LHS = GetPromotedInteger(N->getOperand(0));
457 SDValue RHS = GetPromotedInteger(N->getOperand(1));
458 MVT VT = N->getValueType(0);
459 LHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, LHS.getValueType(), LHS,
460 DAG.getValueType(VT));
461 RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, RHS.getValueType(), RHS,
462 DAG.getValueType(VT));
464 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
467 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
468 SDValue LHS = GetPromotedInteger(N->getOperand(1));
469 SDValue RHS = GetPromotedInteger(N->getOperand(2));
470 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
473 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
474 SDValue LHS = GetPromotedInteger(N->getOperand(2));
475 SDValue RHS = GetPromotedInteger(N->getOperand(3));
476 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
477 N->getOperand(1), LHS, RHS, N->getOperand(4));
480 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
481 MVT SVT = TLI.getSetCCResultType(N->getOperand(0));
482 assert(isTypeLegal(SVT) && "Illegal SetCC type!");
484 // Get the SETCC result using the canonical SETCC type.
485 SDValue SetCC = DAG.getNode(ISD::SETCC, SVT, N->getOperand(0),
486 N->getOperand(1), N->getOperand(2));
488 // Convert to the expected type.
489 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
490 assert(NVT.bitsLE(SVT) && "Integer type overpromoted?");
491 return DAG.getNode(ISD::TRUNCATE, NVT, SetCC);
494 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
495 return DAG.getNode(ISD::SHL, TLI.getTypeToTransformTo(N->getValueType(0)),
496 GetPromotedInteger(N->getOperand(0)), N->getOperand(1));
499 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
500 SDValue Op = GetPromotedInteger(N->getOperand(0));
501 return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(), Op,
505 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
506 // The input may have strange things in the top bits of the registers, but
507 // these operations don't care. They may have weird bits going out, but
508 // that too is okay if they are integer operations.
509 SDValue LHS = GetPromotedInteger(N->getOperand(0));
510 SDValue RHS = GetPromotedInteger(N->getOperand(1));
511 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
514 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
515 // The input value must be properly sign extended.
516 MVT VT = N->getValueType(0);
517 MVT NVT = TLI.getTypeToTransformTo(VT);
518 SDValue Res = GetPromotedInteger(N->getOperand(0));
519 Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res, DAG.getValueType(VT));
520 return DAG.getNode(ISD::SRA, NVT, Res, N->getOperand(1));
523 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
524 // The input value must be properly zero extended.
525 MVT VT = N->getValueType(0);
526 MVT NVT = TLI.getTypeToTransformTo(VT);
527 SDValue Res = ZExtPromotedInteger(N->getOperand(0));
528 return DAG.getNode(ISD::SRL, NVT, Res, N->getOperand(1));
531 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
532 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
535 switch (getTypeAction(N->getOperand(0).getValueType())) {
536 default: assert(0 && "Unknown type action!");
539 Res = N->getOperand(0);
542 Res = GetPromotedInteger(N->getOperand(0));
546 // Truncate to NVT instead of VT
547 return DAG.getNode(ISD::TRUNCATE, NVT, Res);
550 SDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) {
551 // Zero extend the input.
552 SDValue LHS = GetPromotedInteger(N->getOperand(0));
553 SDValue RHS = GetPromotedInteger(N->getOperand(1));
554 MVT VT = N->getValueType(0);
555 LHS = DAG.getZeroExtendInReg(LHS, VT);
556 RHS = DAG.getZeroExtendInReg(RHS, VT);
558 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
561 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
562 return DAG.getNode(ISD::UNDEF, TLI.getTypeToTransformTo(N->getValueType(0)));
565 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
566 SDValue Chain = N->getOperand(0); // Get the chain.
567 SDValue Ptr = N->getOperand(1); // Get the pointer.
568 MVT VT = N->getValueType(0);
570 MVT RegVT = TLI.getRegisterType(VT);
571 unsigned NumRegs = TLI.getNumRegisters(VT);
572 // The argument is passed as NumRegs registers of type RegVT.
574 SmallVector<SDValue, 8> Parts(NumRegs);
575 for (unsigned i = 0; i < NumRegs; ++i) {
576 Parts[i] = DAG.getVAArg(RegVT, Chain, Ptr, N->getOperand(2));
577 Chain = Parts[i].getValue(1);
580 // Handle endianness of the load.
581 if (TLI.isBigEndian())
582 std::reverse(Parts.begin(), Parts.end());
584 // Assemble the parts in the promoted type.
585 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
586 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, NVT, Parts[0]);
587 for (unsigned i = 1; i < NumRegs; ++i) {
588 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, NVT, Parts[i]);
589 // Shift it to the right position and "or" it in.
590 Part = DAG.getNode(ISD::SHL, NVT, Part,
591 DAG.getConstant(i * RegVT.getSizeInBits(),
592 TLI.getShiftAmountTy()));
593 Res = DAG.getNode(ISD::OR, NVT, Res, Part);
596 // Modified the chain result - switch anything that used the old chain to
598 ReplaceValueWith(SDValue(N, 1), Chain);
604 //===----------------------------------------------------------------------===//
605 // Integer Operand Promotion
606 //===----------------------------------------------------------------------===//
608 /// PromoteIntegerOperand - This method is called when the specified operand of
609 /// the specified node is found to need promotion. At this point, all of the
610 /// result types of the node are known to be legal, but other operands of the
611 /// node may need promotion or expansion as well as the specified one.
612 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
613 DEBUG(cerr << "Promote integer operand: "; N->dump(&DAG); cerr << "\n");
614 SDValue Res = SDValue();
616 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
617 == TargetLowering::Custom)
618 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
620 if (Res.getNode() == 0) {
621 switch (N->getOpcode()) {
624 cerr << "PromoteIntegerOperand Op #" << OpNo << ": ";
625 N->dump(&DAG); cerr << "\n";
627 assert(0 && "Do not know how to promote this operator's operand!");
630 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
631 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
632 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
633 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
634 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
635 case ISD::FP_EXTEND: Res = PromoteIntOp_FP_EXTEND(N); break;
636 case ISD::FP_ROUND: Res = PromoteIntOp_FP_ROUND(N); break;
637 case ISD::INSERT_VECTOR_ELT:
638 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
639 case ISD::MEMBARRIER: Res = PromoteIntOp_MEMBARRIER(N); break;
640 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
641 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
642 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
643 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
644 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
646 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
647 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
649 case ISD::SINT_TO_FP:
650 case ISD::UINT_TO_FP: Res = PromoteIntOp_INT_TO_FP(N); break;
654 // If the result is null, the sub-method took care of registering results etc.
655 if (!Res.getNode()) return false;
656 // If the result is N, the sub-method updated N in place.
657 if (Res.getNode() == N) {
658 // Mark N as new and remark N and its operands. This allows us to correctly
659 // revisit N if it needs another step of promotion and allows us to visit
660 // any new operands to N.
665 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
666 "Invalid operand expansion");
668 ReplaceValueWith(SDValue(N, 0), Res);
672 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
673 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
674 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
675 ISD::CondCode CCCode) {
676 MVT VT = NewLHS.getValueType();
678 // Get the promoted values.
679 NewLHS = GetPromotedInteger(NewLHS);
680 NewRHS = GetPromotedInteger(NewRHS);
682 // We have to insert explicit sign or zero extends. Note that we could
683 // insert sign extends for ALL conditions, but zero extend is cheaper on
684 // many machines (an AND instead of two shifts), so prefer it.
686 default: assert(0 && "Unknown integer comparison!");
693 // ALL of these operations will work if we either sign or zero extend
694 // the operands (including the unsigned comparisons!). Zero extend is
695 // usually a simpler/cheaper operation, so prefer it.
696 NewLHS = DAG.getZeroExtendInReg(NewLHS, VT);
697 NewRHS = DAG.getZeroExtendInReg(NewRHS, VT);
703 NewLHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewLHS.getValueType(), NewLHS,
704 DAG.getValueType(VT));
705 NewRHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewRHS.getValueType(), NewRHS,
706 DAG.getValueType(VT));
711 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
712 SDValue Op = GetPromotedInteger(N->getOperand(0));
713 return DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
716 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
717 assert(OpNo == 2 && "Don't know how to promote this operand!");
719 SDValue LHS = N->getOperand(2);
720 SDValue RHS = N->getOperand(3);
721 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
723 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
725 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
726 N->getOperand(1), LHS, RHS, N->getOperand(4));
729 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
730 assert(OpNo == 1 && "only know how to promote condition");
731 SDValue Cond = GetPromotedInteger(N->getOperand(1)); // Promote condition.
733 // Make sure the extra bits coming from type promotion conform to
734 // getSetCCResultContents.
735 unsigned CondBits = Cond.getValueSizeInBits();
736 switch (TLI.getSetCCResultContents()) {
738 assert(false && "Unknown SetCCResultValue!");
739 case TargetLowering::UndefinedSetCCResult:
740 // The promoted value, which may contain rubbish in the upper bits, is fine.
742 case TargetLowering::ZeroOrOneSetCCResult:
743 if (!DAG.MaskedValueIsZero(Cond,APInt::getHighBitsSet(CondBits,CondBits-1)))
744 Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
746 case TargetLowering::ZeroOrNegativeOneSetCCResult:
747 if (DAG.ComputeNumSignBits(Cond) != CondBits)
748 Cond = DAG.getNode(ISD::SIGN_EXTEND_INREG, Cond.getValueType(), Cond,
749 DAG.getValueType(MVT::i1));
753 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
754 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Cond,
758 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
759 // Since the result type is legal, the operands must promote to it.
760 MVT OVT = N->getOperand(0).getValueType();
761 SDValue Lo = GetPromotedInteger(N->getOperand(0));
762 SDValue Hi = GetPromotedInteger(N->getOperand(1));
763 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
765 Lo = DAG.getZeroExtendInReg(Lo, OVT);
766 Hi = DAG.getNode(ISD::SHL, N->getValueType(0), Hi,
767 DAG.getConstant(OVT.getSizeInBits(),
768 TLI.getShiftAmountTy()));
769 return DAG.getNode(ISD::OR, N->getValueType(0), Lo, Hi);
772 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
773 // The vector type is legal but the element type is not. This implies
774 // that the vector is a power-of-two in length and that the element
775 // type does not have a strange size (eg: it is not i1).
776 MVT VecVT = N->getValueType(0);
777 unsigned NumElts = VecVT.getVectorNumElements();
778 assert(!(NumElts & 1) && "Legal vector of one illegal element?");
780 // Build a vector of half the length out of elements of twice the bitwidth.
781 // For example <4 x i16> -> <2 x i32>.
782 MVT OldVT = N->getOperand(0).getValueType();
783 MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
784 assert(OldVT.isSimple() && NewVT.isSimple());
786 std::vector<SDValue> NewElts;
787 NewElts.reserve(NumElts/2);
789 for (unsigned i = 0; i < NumElts; i += 2) {
790 // Combine two successive elements into one promoted element.
791 SDValue Lo = N->getOperand(i);
792 SDValue Hi = N->getOperand(i+1);
793 if (TLI.isBigEndian())
795 NewElts.push_back(JoinIntegers(Lo, Hi));
798 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR,
799 MVT::getVectorVT(NewVT, NewElts.size()),
800 &NewElts[0], NewElts.size());
802 // Convert the new vector to the old vector type.
803 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
806 SDValue DAGTypeLegalizer::PromoteIntOp_FP_EXTEND(SDNode *N) {
807 SDValue Op = GetPromotedInteger(N->getOperand(0));
808 return DAG.getNode(ISD::FP_EXTEND, N->getValueType(0), Op);
811 SDValue DAGTypeLegalizer::PromoteIntOp_FP_ROUND(SDNode *N) {
812 SDValue Op = GetPromotedInteger(N->getOperand(0));
813 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Op,
814 DAG.getIntPtrConstant(0));
817 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
820 // Promote the inserted value. This is valid because the type does not
821 // have to match the vector element type.
823 // Check that any extra bits introduced will be truncated away.
824 assert(N->getOperand(1).getValueType().getSizeInBits() >=
825 N->getValueType(0).getVectorElementType().getSizeInBits() &&
826 "Type of inserted value narrower than vector element type!");
827 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
828 GetPromotedInteger(N->getOperand(1)),
832 assert(OpNo == 2 && "Different operand and result vector types?");
834 // Promote the index.
835 SDValue Idx = N->getOperand(2);
836 Idx = DAG.getZeroExtendInReg(GetPromotedInteger(Idx), Idx.getValueType());
837 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
838 N->getOperand(1), Idx);
841 SDValue DAGTypeLegalizer::PromoteIntOp_INT_TO_FP(SDNode *N) {
842 SDValue In = GetPromotedInteger(N->getOperand(0));
843 MVT OpVT = N->getOperand(0).getValueType();
844 if (N->getOpcode() == ISD::UINT_TO_FP)
845 In = DAG.getZeroExtendInReg(In, OpVT);
847 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(),
848 In, DAG.getValueType(OpVT));
850 return DAG.UpdateNodeOperands(SDValue(N, 0), In);
853 SDValue DAGTypeLegalizer::PromoteIntOp_MEMBARRIER(SDNode *N) {
855 NewOps[0] = N->getOperand(0);
856 for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
857 SDValue Flag = GetPromotedInteger(N->getOperand(i));
858 NewOps[i] = DAG.getZeroExtendInReg(Flag, MVT::i1);
860 return DAG.UpdateNodeOperands(SDValue (N, 0), NewOps,
861 array_lengthof(NewOps));
864 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
865 assert(OpNo == 0 && "Only know how to promote condition");
866 SDValue Cond = GetPromotedInteger(N->getOperand(0));
868 // Promote all the way up to SVT, the canonical SetCC type.
869 // FIXME: Not clear what value to pass to getSetCCResultType.
870 // [This only matters for CellSPU since all other targets
871 // ignore the argument.] We used to pass Cond, resulting in
872 // SVT = MVT::i8, but CellSPU has no select patterns for i8,
873 // causing an abort later. Passing the result type works
874 // around the problem.
875 MVT SVT = TLI.getSetCCResultType(N->getOperand(1));
876 assert(isTypeLegal(SVT) && "Illegal SetCC type!");
877 assert(Cond.getValueType().bitsLE(SVT) && "Unexpected SetCC type!");
879 // Make sure the extra bits conform to getSetCCResultContents. There are
880 // two sets of extra bits: those in Cond, which come from type promotion,
881 // and those we need to add to have the final type be SVT (for most targets
882 // this last set of bits is empty).
883 unsigned CondBits = Cond.getValueSizeInBits();
884 ISD::NodeType ExtendCode;
885 switch (TLI.getSetCCResultContents()) {
887 assert(false && "Unknown SetCCResultValue!");
888 case TargetLowering::UndefinedSetCCResult:
889 // Extend to SVT by adding rubbish.
890 ExtendCode = ISD::ANY_EXTEND;
892 case TargetLowering::ZeroOrOneSetCCResult:
893 ExtendCode = ISD::ZERO_EXTEND;
894 if (!DAG.MaskedValueIsZero(Cond,APInt::getHighBitsSet(CondBits,CondBits-1)))
895 // All extra bits need to be cleared. Do this by zero extending the
896 // original condition value all the way to SVT.
897 Cond = N->getOperand(0);
899 case TargetLowering::ZeroOrNegativeOneSetCCResult: {
900 ExtendCode = ISD::SIGN_EXTEND;
901 unsigned SignBits = DAG.ComputeNumSignBits(Cond);
902 if (SignBits != CondBits)
903 // All extra bits need to be sign extended. Do this by sign extending the
904 // original condition value all the way to SVT.
905 Cond = N->getOperand(0);
909 Cond = DAG.getNode(ExtendCode, SVT, Cond);
911 return DAG.UpdateNodeOperands(SDValue(N, 0), Cond,
912 N->getOperand(1), N->getOperand(2));
915 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
916 assert(OpNo == 0 && "Don't know how to promote this operand!");
918 SDValue LHS = N->getOperand(0);
919 SDValue RHS = N->getOperand(1);
920 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
922 // The CC (#4) and the possible return values (#2 and #3) have legal types.
923 return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2),
924 N->getOperand(3), N->getOperand(4));
927 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
928 assert(OpNo == 0 && "Don't know how to promote this operand!");
930 SDValue LHS = N->getOperand(0);
931 SDValue RHS = N->getOperand(1);
932 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
934 // The CC (#2) is always legal.
935 return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2));
938 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
939 SDValue Op = GetPromotedInteger(N->getOperand(0));
940 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
941 return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(),
942 Op, DAG.getValueType(N->getOperand(0).getValueType()));
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 int SVOffset = N->getSrcValueOffset();
949 unsigned Alignment = N->getAlignment();
950 bool isVolatile = N->isVolatile();
952 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
954 assert(!N->isTruncatingStore() && "Cannot promote this store operand!");
956 // Truncate the value and store the result.
957 return DAG.getTruncStore(Ch, Val, Ptr, N->getSrcValue(),
958 SVOffset, N->getMemoryVT(),
959 isVolatile, Alignment);
962 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
963 SDValue Op = GetPromotedInteger(N->getOperand(0));
964 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), Op);
967 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
968 SDValue Op = GetPromotedInteger(N->getOperand(0));
969 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
970 return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
974 //===----------------------------------------------------------------------===//
975 // Integer Result Expansion
976 //===----------------------------------------------------------------------===//
978 /// ExpandIntegerResult - This method is called when the specified result of the
979 /// specified node is found to need expansion. At this point, the node may also
980 /// have invalid operands or may have other results that need promotion, we just
981 /// know that (at least) one result needs expansion.
982 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
983 DEBUG(cerr << "Expand integer result: "; N->dump(&DAG); cerr << "\n");
987 // See if the target wants to custom expand this node.
988 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
989 TargetLowering::Custom) {
990 // If the target wants to, allow it to lower this itself.
991 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
992 // Everything that once used N now uses P. We are guaranteed that the
993 // result value types of N and the result value types of P match.
994 ReplaceNodeWith(N, P);
999 switch (N->getOpcode()) {
1002 cerr << "ExpandIntegerResult #" << ResNo << ": ";
1003 N->dump(&DAG); cerr << "\n";
1005 assert(0 && "Do not know how to expand the result of this operator!");
1008 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
1009 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
1010 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
1011 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
1013 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
1014 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
1015 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
1016 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
1017 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
1019 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
1020 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
1021 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
1022 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
1023 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
1024 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
1025 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
1026 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
1027 case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
1028 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
1029 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
1030 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
1031 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
1032 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
1033 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
1034 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
1035 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
1036 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
1037 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
1038 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
1042 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
1045 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
1048 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
1051 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
1055 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
1058 // If Lo/Hi is null, the sub-method took care of registering results etc.
1060 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
1063 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
1064 /// and the shift amount is a constant 'Amt'. Expand the operation.
1065 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
1066 SDValue &Lo, SDValue &Hi) {
1067 // Expand the incoming operand to be shifted, so that we have its parts
1069 GetExpandedInteger(N->getOperand(0), InL, InH);
1071 MVT NVT = InL.getValueType();
1072 unsigned VTBits = N->getValueType(0).getSizeInBits();
1073 unsigned NVTBits = NVT.getSizeInBits();
1074 MVT ShTy = N->getOperand(1).getValueType();
1076 if (N->getOpcode() == ISD::SHL) {
1078 Lo = Hi = DAG.getConstant(0, NVT);
1079 } else if (Amt > NVTBits) {
1080 Lo = DAG.getConstant(0, NVT);
1081 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
1082 } else if (Amt == NVTBits) {
1083 Lo = DAG.getConstant(0, NVT);
1085 } else if (Amt == 1) {
1086 // Emit this X << 1 as X+X.
1087 SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1088 SDValue LoOps[2] = { InL, InL };
1089 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1090 SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
1091 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1093 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt, ShTy));
1094 Hi = DAG.getNode(ISD::OR, NVT,
1095 DAG.getNode(ISD::SHL, NVT, InH,
1096 DAG.getConstant(Amt, ShTy)),
1097 DAG.getNode(ISD::SRL, NVT, InL,
1098 DAG.getConstant(NVTBits-Amt, ShTy)));
1103 if (N->getOpcode() == ISD::SRL) {
1105 Lo = DAG.getConstant(0, NVT);
1106 Hi = DAG.getConstant(0, NVT);
1107 } else if (Amt > NVTBits) {
1108 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
1109 Hi = DAG.getConstant(0, NVT);
1110 } else if (Amt == NVTBits) {
1112 Hi = DAG.getConstant(0, NVT);
1114 Lo = DAG.getNode(ISD::OR, NVT,
1115 DAG.getNode(ISD::SRL, NVT, InL,
1116 DAG.getConstant(Amt, ShTy)),
1117 DAG.getNode(ISD::SHL, NVT, InH,
1118 DAG.getConstant(NVTBits-Amt, ShTy)));
1119 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt, ShTy));
1124 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1126 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1127 DAG.getConstant(NVTBits-1, ShTy));
1128 } else if (Amt > NVTBits) {
1129 Lo = DAG.getNode(ISD::SRA, NVT, InH,
1130 DAG.getConstant(Amt-NVTBits, ShTy));
1131 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1132 DAG.getConstant(NVTBits-1, ShTy));
1133 } else if (Amt == NVTBits) {
1135 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1136 DAG.getConstant(NVTBits-1, ShTy));
1138 Lo = DAG.getNode(ISD::OR, NVT,
1139 DAG.getNode(ISD::SRL, NVT, InL,
1140 DAG.getConstant(Amt, ShTy)),
1141 DAG.getNode(ISD::SHL, NVT, InH,
1142 DAG.getConstant(NVTBits-Amt, ShTy)));
1143 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Amt, ShTy));
1147 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1148 /// this shift based on knowledge of the high bit of the shift amount. If we
1149 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1151 bool DAGTypeLegalizer::
1152 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1153 SDValue Amt = N->getOperand(1);
1154 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1155 MVT ShTy = Amt.getValueType();
1156 unsigned ShBits = ShTy.getSizeInBits();
1157 unsigned NVTBits = NVT.getSizeInBits();
1158 assert(isPowerOf2_32(NVTBits) &&
1159 "Expanded integer type size not a power of two!");
1161 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
1162 APInt KnownZero, KnownOne;
1163 DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
1165 // If we don't know anything about the high bits, exit.
1166 if (((KnownZero|KnownOne) & HighBitMask) == 0)
1169 // Get the incoming operand to be shifted.
1171 GetExpandedInteger(N->getOperand(0), InL, InH);
1173 // If we know that any of the high bits of the shift amount are one, then we
1174 // can do this as a couple of simple shifts.
1175 if (KnownOne.intersects(HighBitMask)) {
1176 // Mask out the high bit, which we know is set.
1177 Amt = DAG.getNode(ISD::AND, ShTy, Amt,
1178 DAG.getConstant(~HighBitMask, ShTy));
1180 switch (N->getOpcode()) {
1181 default: assert(0 && "Unknown shift");
1183 Lo = DAG.getConstant(0, NVT); // Low part is zero.
1184 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
1187 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
1188 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
1191 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
1192 DAG.getConstant(NVTBits-1, ShTy));
1193 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
1198 // If we know that all of the high bits of the shift amount are zero, then we
1199 // can do this as a couple of simple shifts.
1200 if ((KnownZero & HighBitMask) == HighBitMask) {
1202 SDValue Amt2 = DAG.getNode(ISD::SUB, ShTy,
1203 DAG.getConstant(NVTBits, ShTy),
1206 switch (N->getOpcode()) {
1207 default: assert(0 && "Unknown shift");
1208 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1210 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1213 Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
1214 Hi = DAG.getNode(ISD::OR, NVT,
1215 DAG.getNode(Op1, NVT, InH, Amt),
1216 DAG.getNode(Op2, NVT, InL, Amt2));
1223 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
1224 SDValue &Lo, SDValue &Hi) {
1225 // Expand the subcomponents.
1226 SDValue LHSL, LHSH, RHSL, RHSH;
1227 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1228 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1230 MVT NVT = LHSL.getValueType();
1231 SDValue LoOps[2] = { LHSL, RHSL };
1232 SDValue HiOps[3] = { LHSH, RHSH };
1234 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
1235 // them. TODO: Teach operation legalization how to expand unsupported
1236 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
1237 // a carry of type MVT::Flag, but there doesn't seem to be any way to
1238 // generate a value of this type in the expanded code sequence.
1240 TLI.isOperationLegal(N->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC,
1241 TLI.getTypeToExpandTo(NVT));
1244 SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1245 if (N->getOpcode() == ISD::ADD) {
1246 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1247 HiOps[2] = Lo.getValue(1);
1248 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1250 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1251 HiOps[2] = Lo.getValue(1);
1252 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1255 if (N->getOpcode() == ISD::ADD) {
1256 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
1257 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
1258 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo), Lo, LoOps[0],
1260 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
1261 DAG.getConstant(1, NVT),
1262 DAG.getConstant(0, NVT));
1263 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo), Lo, LoOps[1],
1265 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
1266 DAG.getConstant(1, NVT), Carry1);
1267 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
1269 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
1270 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
1271 SDValue Cmp = DAG.getSetCC(TLI.getSetCCResultType(LoOps[0]),
1272 LoOps[0], LoOps[1], ISD::SETULT);
1273 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
1274 DAG.getConstant(1, NVT),
1275 DAG.getConstant(0, NVT));
1276 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
1281 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
1282 SDValue &Lo, SDValue &Hi) {
1283 // Expand the subcomponents.
1284 SDValue LHSL, LHSH, RHSL, RHSH;
1285 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1286 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1287 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1288 SDValue LoOps[2] = { LHSL, RHSL };
1289 SDValue HiOps[3] = { LHSH, RHSH };
1291 if (N->getOpcode() == ISD::ADDC) {
1292 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1293 HiOps[2] = Lo.getValue(1);
1294 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1296 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1297 HiOps[2] = Lo.getValue(1);
1298 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1301 // Legalized the flag result - switch anything that used the old flag to
1303 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1306 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
1307 SDValue &Lo, SDValue &Hi) {
1308 // Expand the subcomponents.
1309 SDValue LHSL, LHSH, RHSL, RHSH;
1310 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1311 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1312 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1313 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
1314 SDValue HiOps[3] = { LHSH, RHSH };
1316 Lo = DAG.getNode(N->getOpcode(), VTList, LoOps, 3);
1317 HiOps[2] = Lo.getValue(1);
1318 Hi = DAG.getNode(N->getOpcode(), VTList, HiOps, 3);
1320 // Legalized the flag result - switch anything that used the old flag to
1322 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1325 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
1326 SDValue &Lo, SDValue &Hi) {
1327 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1328 SDValue Op = N->getOperand(0);
1329 if (Op.getValueType().bitsLE(NVT)) {
1330 // The low part is any extension of the input (which degenerates to a copy).
1331 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Op);
1332 Hi = DAG.getNode(ISD::UNDEF, NVT); // The high part is undefined.
1334 // For example, extension of an i48 to an i64. The operand type necessarily
1335 // promotes to the result type, so will end up being expanded too.
1336 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1337 "Only know how to promote this result!");
1338 SDValue Res = GetPromotedInteger(Op);
1339 assert(Res.getValueType() == N->getValueType(0) &&
1340 "Operand over promoted?");
1341 // Split the promoted operand. This will simplify when it is expanded.
1342 SplitInteger(Res, Lo, Hi);
1346 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
1347 SDValue &Lo, SDValue &Hi) {
1348 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1349 MVT NVT = Lo.getValueType();
1350 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1351 unsigned NVTBits = NVT.getSizeInBits();
1352 unsigned EVTBits = EVT.getSizeInBits();
1354 if (NVTBits < EVTBits) {
1355 Hi = DAG.getNode(ISD::AssertSext, NVT, Hi,
1356 DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1358 Lo = DAG.getNode(ISD::AssertSext, NVT, Lo, DAG.getValueType(EVT));
1359 // The high part replicates the sign bit of Lo, make it explicit.
1360 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1361 DAG.getConstant(NVTBits-1, TLI.getShiftAmountTy()));
1365 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
1366 SDValue &Lo, SDValue &Hi) {
1367 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1368 MVT NVT = Lo.getValueType();
1369 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1370 unsigned NVTBits = NVT.getSizeInBits();
1371 unsigned EVTBits = EVT.getSizeInBits();
1373 if (NVTBits < EVTBits) {
1374 Hi = DAG.getNode(ISD::AssertZext, NVT, Hi,
1375 DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1377 Lo = DAG.getNode(ISD::AssertZext, NVT, Lo, DAG.getValueType(EVT));
1378 // The high part must be zero, make it explicit.
1379 Hi = DAG.getConstant(0, NVT);
1383 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
1384 SDValue &Lo, SDValue &Hi) {
1385 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
1386 Lo = DAG.getNode(ISD::BSWAP, Lo.getValueType(), Lo);
1387 Hi = DAG.getNode(ISD::BSWAP, Hi.getValueType(), Hi);
1390 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
1391 SDValue &Lo, SDValue &Hi) {
1392 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1393 unsigned NBitWidth = NVT.getSizeInBits();
1394 const APInt &Cst = cast<ConstantSDNode>(N)->getAPIntValue();
1395 Lo = DAG.getConstant(APInt(Cst).trunc(NBitWidth), NVT);
1396 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
1399 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
1400 SDValue &Lo, SDValue &Hi) {
1401 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
1402 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1403 MVT NVT = Lo.getValueType();
1405 SDValue HiNotZero = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
1406 DAG.getConstant(0, NVT), ISD::SETNE);
1408 SDValue LoLZ = DAG.getNode(ISD::CTLZ, NVT, Lo);
1409 SDValue HiLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
1411 Lo = DAG.getNode(ISD::SELECT, NVT, HiNotZero, HiLZ,
1412 DAG.getNode(ISD::ADD, NVT, LoLZ,
1413 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1414 Hi = DAG.getConstant(0, NVT);
1417 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
1418 SDValue &Lo, SDValue &Hi) {
1419 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
1420 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1421 MVT NVT = Lo.getValueType();
1422 Lo = DAG.getNode(ISD::ADD, NVT, DAG.getNode(ISD::CTPOP, NVT, Lo),
1423 DAG.getNode(ISD::CTPOP, NVT, Hi));
1424 Hi = DAG.getConstant(0, NVT);
1427 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
1428 SDValue &Lo, SDValue &Hi) {
1429 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
1430 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1431 MVT NVT = Lo.getValueType();
1433 SDValue LoNotZero = DAG.getSetCC(TLI.getSetCCResultType(Lo), Lo,
1434 DAG.getConstant(0, NVT), ISD::SETNE);
1436 SDValue LoLZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
1437 SDValue HiLZ = DAG.getNode(ISD::CTTZ, NVT, Hi);
1439 Lo = DAG.getNode(ISD::SELECT, NVT, LoNotZero, LoLZ,
1440 DAG.getNode(ISD::ADD, NVT, HiLZ,
1441 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1442 Hi = DAG.getConstant(0, NVT);
1445 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
1447 MVT VT = N->getValueType(0);
1448 SDValue Op = N->getOperand(0);
1449 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
1450 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
1451 SplitInteger(MakeLibCall(LC, VT, &Op, 1, true/*sign irrelevant*/), Lo, Hi);
1454 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
1456 MVT VT = N->getValueType(0);
1457 SDValue Op = N->getOperand(0);
1458 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
1459 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
1460 SplitInteger(MakeLibCall(LC, VT, &Op, 1, false/*sign irrelevant*/), Lo, Hi);
1463 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
1464 SDValue &Lo, SDValue &Hi) {
1465 if (ISD::isNormalLoad(N)) {
1466 ExpandRes_NormalLoad(N, Lo, Hi);
1470 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1472 MVT VT = N->getValueType(0);
1473 MVT NVT = TLI.getTypeToTransformTo(VT);
1474 SDValue Ch = N->getChain();
1475 SDValue Ptr = N->getBasePtr();
1476 ISD::LoadExtType ExtType = N->getExtensionType();
1477 int SVOffset = N->getSrcValueOffset();
1478 unsigned Alignment = N->getAlignment();
1479 bool isVolatile = N->isVolatile();
1481 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1483 if (N->getMemoryVT().bitsLE(NVT)) {
1484 MVT EVT = N->getMemoryVT();
1486 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset, EVT,
1487 isVolatile, Alignment);
1489 // Remember the chain.
1490 Ch = Lo.getValue(1);
1492 if (ExtType == ISD::SEXTLOAD) {
1493 // The high part is obtained by SRA'ing all but one of the bits of the
1495 unsigned LoSize = Lo.getValueType().getSizeInBits();
1496 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1497 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
1498 } else if (ExtType == ISD::ZEXTLOAD) {
1499 // The high part is just a zero.
1500 Hi = DAG.getConstant(0, NVT);
1502 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
1503 // The high part is undefined.
1504 Hi = DAG.getNode(ISD::UNDEF, NVT);
1506 } else if (TLI.isLittleEndian()) {
1507 // Little-endian - low bits are at low addresses.
1508 Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1509 isVolatile, Alignment);
1511 unsigned ExcessBits =
1512 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
1513 MVT NEVT = MVT::getIntegerVT(ExcessBits);
1515 // Increment the pointer to the other half.
1516 unsigned IncrementSize = NVT.getSizeInBits()/8;
1517 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1518 DAG.getIntPtrConstant(IncrementSize));
1519 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(),
1520 SVOffset+IncrementSize, NEVT,
1521 isVolatile, MinAlign(Alignment, IncrementSize));
1523 // Build a factor node to remember that this load is independent of the
1525 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1528 // Big-endian - high bits are at low addresses. Favor aligned loads at
1529 // the cost of some bit-fiddling.
1530 MVT EVT = N->getMemoryVT();
1531 unsigned EBytes = EVT.getStoreSizeInBits()/8;
1532 unsigned IncrementSize = NVT.getSizeInBits()/8;
1533 unsigned ExcessBits = (EBytes - IncrementSize)*8;
1535 // Load both the high bits and maybe some of the low bits.
1536 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1537 MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits),
1538 isVolatile, Alignment);
1540 // Increment the pointer to the other half.
1541 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1542 DAG.getIntPtrConstant(IncrementSize));
1543 // Load the rest of the low bits.
1544 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Ch, Ptr, N->getSrcValue(),
1545 SVOffset+IncrementSize,
1546 MVT::getIntegerVT(ExcessBits),
1547 isVolatile, MinAlign(Alignment, IncrementSize));
1549 // Build a factor node to remember that this load is independent of the
1551 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1554 if (ExcessBits < NVT.getSizeInBits()) {
1555 // Transfer low bits from the bottom of Hi to the top of Lo.
1556 Lo = DAG.getNode(ISD::OR, NVT, Lo,
1557 DAG.getNode(ISD::SHL, NVT, Hi,
1558 DAG.getConstant(ExcessBits,
1559 TLI.getShiftAmountTy())));
1560 // Move high bits to the right position in Hi.
1561 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, NVT, Hi,
1562 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
1563 TLI.getShiftAmountTy()));
1567 // Legalized the chain result - switch anything that used the old chain to
1569 ReplaceValueWith(SDValue(N, 1), Ch);
1572 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
1573 SDValue &Lo, SDValue &Hi) {
1574 SDValue LL, LH, RL, RH;
1575 GetExpandedInteger(N->getOperand(0), LL, LH);
1576 GetExpandedInteger(N->getOperand(1), RL, RH);
1577 Lo = DAG.getNode(N->getOpcode(), LL.getValueType(), LL, RL);
1578 Hi = DAG.getNode(N->getOpcode(), LL.getValueType(), LH, RH);
1581 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
1582 SDValue &Lo, SDValue &Hi) {
1583 MVT VT = N->getValueType(0);
1584 MVT NVT = TLI.getTypeToTransformTo(VT);
1586 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
1587 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
1588 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
1589 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
1590 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1591 SDValue LL, LH, RL, RH;
1592 GetExpandedInteger(N->getOperand(0), LL, LH);
1593 GetExpandedInteger(N->getOperand(1), RL, RH);
1594 unsigned OuterBitSize = VT.getSizeInBits();
1595 unsigned InnerBitSize = NVT.getSizeInBits();
1596 unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1597 unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
1599 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
1600 if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
1601 DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
1602 // The inputs are both zero-extended.
1604 // We can emit a umul_lohi.
1605 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1606 Hi = SDValue(Lo.getNode(), 1);
1610 // We can emit a mulhu+mul.
1611 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1612 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1616 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
1617 // The input values are both sign-extended.
1619 // We can emit a smul_lohi.
1620 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1621 Hi = SDValue(Lo.getNode(), 1);
1625 // We can emit a mulhs+mul.
1626 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1627 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
1632 // Lo,Hi = umul LHS, RHS.
1633 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
1634 DAG.getVTList(NVT, NVT), LL, RL);
1636 Hi = UMulLOHI.getValue(1);
1637 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1638 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1639 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1640 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1644 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1645 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1646 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1647 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1648 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1649 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1654 // If nothing else, we can make a libcall.
1655 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1657 LC = RTLIB::MUL_I32;
1658 else if (VT == MVT::i64)
1659 LC = RTLIB::MUL_I64;
1660 else if (VT == MVT::i128)
1661 LC = RTLIB::MUL_I128;
1662 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
1664 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1665 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true/*sign irrelevant*/), Lo, Hi);
1668 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
1669 SDValue &Lo, SDValue &Hi) {
1670 MVT VT = N->getValueType(0);
1672 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1674 LC = RTLIB::SDIV_I32;
1675 else if (VT == MVT::i64)
1676 LC = RTLIB::SDIV_I64;
1677 else if (VT == MVT::i128)
1678 LC = RTLIB::SDIV_I128;
1679 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1681 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1682 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true), Lo, Hi);
1685 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
1686 SDValue &Lo, SDValue &Hi) {
1687 MVT VT = N->getValueType(0);
1689 // If we can emit an efficient shift operation, do so now. Check to see if
1690 // the RHS is a constant.
1691 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1692 return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi);
1694 // If we can determine that the high bit of the shift is zero or one, even if
1695 // the low bits are variable, emit this shift in an optimized form.
1696 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
1699 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
1701 if (N->getOpcode() == ISD::SHL) {
1702 PartsOpc = ISD::SHL_PARTS;
1703 } else if (N->getOpcode() == ISD::SRL) {
1704 PartsOpc = ISD::SRL_PARTS;
1706 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1707 PartsOpc = ISD::SRA_PARTS;
1710 // Next check to see if the target supports this SHL_PARTS operation or if it
1711 // will custom expand it.
1712 MVT NVT = TLI.getTypeToTransformTo(VT);
1713 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
1714 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
1715 Action == TargetLowering::Custom) {
1716 // Expand the subcomponents.
1718 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1720 SDValue Ops[] = { LHSL, LHSH, N->getOperand(1) };
1721 MVT VT = LHSL.getValueType();
1722 Lo = DAG.getNode(PartsOpc, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
1723 Hi = Lo.getValue(1);
1727 // Otherwise, emit a libcall.
1728 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1730 if (N->getOpcode() == ISD::SHL) {
1731 isSigned = false; /*sign irrelevant*/
1733 LC = RTLIB::SHL_I32;
1734 else if (VT == MVT::i64)
1735 LC = RTLIB::SHL_I64;
1736 else if (VT == MVT::i128)
1737 LC = RTLIB::SHL_I128;
1738 } else if (N->getOpcode() == ISD::SRL) {
1741 LC = RTLIB::SRL_I32;
1742 else if (VT == MVT::i64)
1743 LC = RTLIB::SRL_I64;
1744 else if (VT == MVT::i128)
1745 LC = RTLIB::SRL_I128;
1747 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1750 LC = RTLIB::SRA_I32;
1751 else if (VT == MVT::i64)
1752 LC = RTLIB::SRA_I64;
1753 else if (VT == MVT::i128)
1754 LC = RTLIB::SRA_I128;
1756 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported shift!");
1758 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1759 SplitInteger(MakeLibCall(LC, VT, Ops, 2, isSigned), Lo, Hi);
1762 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
1763 SDValue &Lo, SDValue &Hi) {
1764 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1765 SDValue Op = N->getOperand(0);
1766 if (Op.getValueType().bitsLE(NVT)) {
1767 // The low part is sign extension of the input (degenerates to a copy).
1768 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, N->getOperand(0));
1769 // The high part is obtained by SRA'ing all but one of the bits of low part.
1770 unsigned LoSize = NVT.getSizeInBits();
1771 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1772 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
1774 // For example, extension of an i48 to an i64. The operand type necessarily
1775 // promotes to the result type, so will end up being expanded too.
1776 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1777 "Only know how to promote this result!");
1778 SDValue Res = GetPromotedInteger(Op);
1779 assert(Res.getValueType() == N->getValueType(0) &&
1780 "Operand over promoted?");
1781 // Split the promoted operand. This will simplify when it is expanded.
1782 SplitInteger(Res, Lo, Hi);
1783 unsigned ExcessBits =
1784 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1785 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
1786 DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1790 void DAGTypeLegalizer::
1791 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
1792 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1793 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1795 if (EVT.bitsLE(Lo.getValueType())) {
1796 // sext_inreg the low part if needed.
1797 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, Lo.getValueType(), Lo,
1800 // The high part gets the sign extension from the lo-part. This handles
1801 // things like sextinreg V:i64 from i8.
1802 Hi = DAG.getNode(ISD::SRA, Hi.getValueType(), Lo,
1803 DAG.getConstant(Hi.getValueType().getSizeInBits()-1,
1804 TLI.getShiftAmountTy()));
1806 // For example, extension of an i48 to an i64. Leave the low part alone,
1807 // sext_inreg the high part.
1808 unsigned ExcessBits =
1809 EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
1810 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
1811 DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1815 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
1816 SDValue &Lo, SDValue &Hi) {
1817 MVT VT = N->getValueType(0);
1819 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1821 LC = RTLIB::SREM_I32;
1822 else if (VT == MVT::i64)
1823 LC = RTLIB::SREM_I64;
1824 else if (VT == MVT::i128)
1825 LC = RTLIB::SREM_I128;
1826 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1828 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1829 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true), Lo, Hi);
1832 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
1833 SDValue &Lo, SDValue &Hi) {
1834 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1835 Lo = DAG.getNode(ISD::TRUNCATE, NVT, N->getOperand(0));
1836 Hi = DAG.getNode(ISD::SRL, N->getOperand(0).getValueType(), N->getOperand(0),
1837 DAG.getConstant(NVT.getSizeInBits(),
1838 TLI.getShiftAmountTy()));
1839 Hi = DAG.getNode(ISD::TRUNCATE, NVT, Hi);
1842 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
1843 SDValue &Lo, SDValue &Hi) {
1844 MVT VT = N->getValueType(0);
1846 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1848 LC = RTLIB::UDIV_I32;
1849 else if (VT == MVT::i64)
1850 LC = RTLIB::UDIV_I64;
1851 else if (VT == MVT::i128)
1852 LC = RTLIB::UDIV_I128;
1853 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
1855 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1856 SplitInteger(MakeLibCall(LC, VT, Ops, 2, false), Lo, Hi);
1859 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
1860 SDValue &Lo, SDValue &Hi) {
1861 MVT VT = N->getValueType(0);
1863 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1865 LC = RTLIB::UREM_I32;
1866 else if (VT == MVT::i64)
1867 LC = RTLIB::UREM_I64;
1868 else if (VT == MVT::i128)
1869 LC = RTLIB::UREM_I128;
1870 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
1872 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1873 SplitInteger(MakeLibCall(LC, VT, Ops, 2, false), Lo, Hi);
1876 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
1877 SDValue &Lo, SDValue &Hi) {
1878 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1879 SDValue Op = N->getOperand(0);
1880 if (Op.getValueType().bitsLE(NVT)) {
1881 // The low part is zero extension of the input (degenerates to a copy).
1882 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, N->getOperand(0));
1883 Hi = DAG.getConstant(0, NVT); // The high part is just a zero.
1885 // For example, extension of an i48 to an i64. The operand type necessarily
1886 // promotes to the result type, so will end up being expanded too.
1887 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1888 "Only know how to promote this result!");
1889 SDValue Res = GetPromotedInteger(Op);
1890 assert(Res.getValueType() == N->getValueType(0) &&
1891 "Operand over promoted?");
1892 // Split the promoted operand. This will simplify when it is expanded.
1893 SplitInteger(Res, Lo, Hi);
1894 unsigned ExcessBits =
1895 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1896 Hi = DAG.getZeroExtendInReg(Hi, MVT::getIntegerVT(ExcessBits));
1901 //===----------------------------------------------------------------------===//
1902 // Integer Operand Expansion
1903 //===----------------------------------------------------------------------===//
1905 /// ExpandIntegerOperand - This method is called when the specified operand of
1906 /// the specified node is found to need expansion. At this point, all of the
1907 /// result types of the node are known to be legal, but other operands of the
1908 /// node may need promotion or expansion as well as the specified one.
1909 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
1910 DEBUG(cerr << "Expand integer operand: "; N->dump(&DAG); cerr << "\n");
1911 SDValue Res = SDValue();
1913 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
1914 == TargetLowering::Custom)
1915 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
1917 if (Res.getNode() == 0) {
1918 switch (N->getOpcode()) {
1921 cerr << "ExpandIntegerOperand Op #" << OpNo << ": ";
1922 N->dump(&DAG); cerr << "\n";
1924 assert(0 && "Do not know how to expand this operator's operand!");
1927 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1928 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
1929 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1931 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
1932 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
1933 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
1934 case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
1935 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo);
1937 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
1938 case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
1942 // If the result is null, the sub-method took care of registering results etc.
1943 if (!Res.getNode()) return false;
1944 // If the result is N, the sub-method updated N in place. Check to see if any
1945 // operands are new, and if so, mark them.
1946 if (Res.getNode() == N) {
1947 // Mark N as new and remark N and its operands. This allows us to correctly
1948 // revisit N if it needs another step of expansion and allows us to visit
1949 // any new operands to N.
1954 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1955 "Invalid operand expansion");
1957 ReplaceValueWith(SDValue(N, 0), Res);
1961 /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
1962 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1963 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
1965 ISD::CondCode &CCCode) {
1966 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1967 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
1968 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
1970 MVT VT = NewLHS.getValueType();
1972 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1973 if (RHSLo == RHSHi) {
1974 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
1975 if (RHSCST->isAllOnesValue()) {
1976 // Equality comparison to -1.
1977 NewLHS = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
1984 NewLHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1985 NewRHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1986 NewLHS = DAG.getNode(ISD::OR, NewLHS.getValueType(), NewLHS, NewRHS);
1987 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1991 // If this is a comparison of the sign bit, just look at the top part.
1993 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1994 if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
1995 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
2001 // FIXME: This generated code sucks.
2002 ISD::CondCode LowCC;
2004 default: assert(0 && "Unknown integer setcc!");
2006 case ISD::SETULT: LowCC = ISD::SETULT; break;
2008 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2010 case ISD::SETULE: LowCC = ISD::SETULE; break;
2012 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2015 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2016 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2017 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2019 // NOTE: on targets without efficient SELECT of bools, we can always use
2020 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2021 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
2023 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC,
2024 false, DagCombineInfo);
2025 if (!Tmp1.getNode())
2026 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
2027 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
2028 CCCode, false, DagCombineInfo);
2029 if (!Tmp2.getNode())
2030 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
2031 DAG.getCondCode(CCCode));
2033 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
2034 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
2035 if ((Tmp1C && Tmp1C->isNullValue()) ||
2036 (Tmp2C && Tmp2C->isNullValue() &&
2037 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
2038 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
2039 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
2040 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
2041 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
2042 // low part is known false, returns high part.
2043 // For LE / GE, if high part is known false, ignore the low part.
2044 // For LT / GT, if high part is known true, ignore the low part.
2050 NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
2051 ISD::SETEQ, false, DagCombineInfo);
2052 if (!NewLHS.getNode())
2053 NewLHS = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
2055 NewLHS = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2056 NewLHS, Tmp1, Tmp2);
2060 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
2061 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
2062 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
2063 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2065 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2066 // against zero to select between true and false values.
2067 if (NewRHS.getNode() == 0) {
2068 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2069 CCCode = ISD::SETNE;
2072 // Update N to have the operands specified.
2073 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
2074 DAG.getCondCode(CCCode), NewLHS, NewRHS,
2078 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
2079 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2080 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
2081 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2083 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2084 // against zero to select between true and false values.
2085 if (NewRHS.getNode() == 0) {
2086 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2087 CCCode = ISD::SETNE;
2090 // Update N to have the operands specified.
2091 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2092 N->getOperand(2), N->getOperand(3),
2093 DAG.getCondCode(CCCode));
2096 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
2097 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2098 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
2099 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2101 // If ExpandSetCCOperands returned a scalar, use it.
2102 if (NewRHS.getNode() == 0) {
2103 assert(NewLHS.getValueType() == N->getValueType(0) &&
2104 "Unexpected setcc expansion!");
2108 // Otherwise, update N to have the operands specified.
2109 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2110 DAG.getCondCode(CCCode));
2113 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
2114 SDValue Op = N->getOperand(0);
2115 MVT DstVT = N->getValueType(0);
2116 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
2117 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2118 "Don't know how to expand this SINT_TO_FP!");
2119 return MakeLibCall(LC, DstVT, &Op, 1, true);
2122 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
2123 if (ISD::isNormalStore(N))
2124 return ExpandOp_NormalStore(N, OpNo);
2126 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2127 assert(OpNo == 1 && "Can only expand the stored value so far");
2129 MVT VT = N->getOperand(1).getValueType();
2130 MVT NVT = TLI.getTypeToTransformTo(VT);
2131 SDValue Ch = N->getChain();
2132 SDValue Ptr = N->getBasePtr();
2133 int SVOffset = N->getSrcValueOffset();
2134 unsigned Alignment = N->getAlignment();
2135 bool isVolatile = N->isVolatile();
2138 assert(NVT.isByteSized() && "Expanded type not byte sized!");
2140 if (N->getMemoryVT().bitsLE(NVT)) {
2141 GetExpandedInteger(N->getValue(), Lo, Hi);
2142 return DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
2143 N->getMemoryVT(), isVolatile, Alignment);
2144 } else if (TLI.isLittleEndian()) {
2145 // Little-endian - low bits are at low addresses.
2146 GetExpandedInteger(N->getValue(), Lo, Hi);
2148 Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
2149 isVolatile, Alignment);
2151 unsigned ExcessBits =
2152 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
2153 MVT NEVT = MVT::getIntegerVT(ExcessBits);
2155 // Increment the pointer to the other half.
2156 unsigned IncrementSize = NVT.getSizeInBits()/8;
2157 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2158 DAG.getIntPtrConstant(IncrementSize));
2159 Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
2160 SVOffset+IncrementSize, NEVT,
2161 isVolatile, MinAlign(Alignment, IncrementSize));
2162 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2164 // Big-endian - high bits are at low addresses. Favor aligned stores at
2165 // the cost of some bit-fiddling.
2166 GetExpandedInteger(N->getValue(), Lo, Hi);
2168 MVT EVT = N->getMemoryVT();
2169 unsigned EBytes = EVT.getStoreSizeInBits()/8;
2170 unsigned IncrementSize = NVT.getSizeInBits()/8;
2171 unsigned ExcessBits = (EBytes - IncrementSize)*8;
2172 MVT HiVT = MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits);
2174 if (ExcessBits < NVT.getSizeInBits()) {
2175 // Transfer high bits from the top of Lo to the bottom of Hi.
2176 Hi = DAG.getNode(ISD::SHL, NVT, Hi,
2177 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
2178 TLI.getShiftAmountTy()));
2179 Hi = DAG.getNode(ISD::OR, NVT, Hi,
2180 DAG.getNode(ISD::SRL, NVT, Lo,
2181 DAG.getConstant(ExcessBits,
2182 TLI.getShiftAmountTy())));
2185 // Store both the high bits and maybe some of the low bits.
2186 Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
2187 SVOffset, HiVT, isVolatile, Alignment);
2189 // Increment the pointer to the other half.
2190 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2191 DAG.getIntPtrConstant(IncrementSize));
2192 // Store the lowest ExcessBits bits in the second half.
2193 Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(),
2194 SVOffset+IncrementSize,
2195 MVT::getIntegerVT(ExcessBits),
2196 isVolatile, MinAlign(Alignment, IncrementSize));
2197 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2201 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
2203 GetExpandedInteger(N->getOperand(0), InL, InH);
2204 // Just truncate the low part of the source.
2205 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), InL);
2208 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
2209 SDValue Op = N->getOperand(0);
2210 MVT SrcVT = Op.getValueType();
2211 MVT DstVT = N->getValueType(0);
2213 if (TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
2214 // Do a signed conversion then adjust the result.
2215 SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, DstVT, Op);
2216 SignedConv = TLI.LowerOperation(SignedConv, DAG);
2218 // The result of the signed conversion needs adjusting if the 'sign bit' of
2219 // the incoming integer was set. To handle this, we dynamically test to see
2220 // if it is set, and, if so, add a fudge factor.
2222 const uint64_t F32TwoE32 = 0x4F800000ULL;
2223 const uint64_t F32TwoE64 = 0x5F800000ULL;
2224 const uint64_t F32TwoE128 = 0x7F800000ULL;
2227 if (SrcVT == MVT::i32)
2228 FF = APInt(32, F32TwoE32);
2229 else if (SrcVT == MVT::i64)
2230 FF = APInt(32, F32TwoE64);
2231 else if (SrcVT == MVT::i128)
2232 FF = APInt(32, F32TwoE128);
2234 assert(false && "Unsupported UINT_TO_FP!");
2236 // Check whether the sign bit is set.
2238 GetExpandedInteger(Op, Lo, Hi);
2239 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
2240 DAG.getConstant(0, Hi.getValueType()),
2243 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
2244 SDValue FudgePtr = DAG.getConstantPool(ConstantInt::get(FF.zext(64)),
2245 TLI.getPointerTy());
2247 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
2248 SDValue Zero = DAG.getIntPtrConstant(0);
2249 SDValue Four = DAG.getIntPtrConstant(4);
2250 if (TLI.isBigEndian()) std::swap(Zero, Four);
2251 SDValue Offset = DAG.getNode(ISD::SELECT, Zero.getValueType(), SignSet,
2253 unsigned Alignment =
2254 1 << cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
2255 FudgePtr = DAG.getNode(ISD::ADD, TLI.getPointerTy(), FudgePtr, Offset);
2256 Alignment = std::min(Alignment, 4u);
2258 // Load the value out, extending it from f32 to the destination float type.
2259 // FIXME: Avoid the extend by constructing the right constant pool?
2260 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, DstVT, DAG.getEntryNode(),
2261 FudgePtr, NULL, 0, MVT::f32,
2263 return DAG.getNode(ISD::FADD, DstVT, SignedConv, Fudge);
2266 // Otherwise, use a libcall.
2267 RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
2268 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2269 "Don't know how to expand this UINT_TO_FP!");
2270 return MakeLibCall(LC, DstVT, &Op, 1, true);