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 (CustomLowerResults(N, ResNo))
40 switch (N->getOpcode()) {
43 cerr << "PromoteIntegerResult #" << ResNo << ": ";
44 N->dump(&DAG); cerr << "\n";
46 assert(0 && "Do not know how to promote this operator!");
48 case ISD::AssertSext: Result = PromoteIntRes_AssertSext(N); break;
49 case ISD::AssertZext: Result = PromoteIntRes_AssertZext(N); break;
50 case ISD::BIT_CONVERT: Result = PromoteIntRes_BIT_CONVERT(N); break;
51 case ISD::BSWAP: Result = PromoteIntRes_BSWAP(N); break;
52 case ISD::BUILD_PAIR: Result = PromoteIntRes_BUILD_PAIR(N); break;
53 case ISD::Constant: Result = PromoteIntRes_Constant(N); break;
54 case ISD::CONVERT_RNDSAT:
55 Result = PromoteIntRes_CONVERT_RNDSAT(N); break;
56 case ISD::CTLZ: Result = PromoteIntRes_CTLZ(N); break;
57 case ISD::CTPOP: Result = PromoteIntRes_CTPOP(N); break;
58 case ISD::CTTZ: Result = PromoteIntRes_CTTZ(N); break;
59 case ISD::EXTRACT_VECTOR_ELT:
60 Result = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
61 case ISD::LOAD: Result = PromoteIntRes_LOAD(cast<LoadSDNode>(N));break;
62 case ISD::SELECT: Result = PromoteIntRes_SELECT(N); break;
63 case ISD::SELECT_CC: Result = PromoteIntRes_SELECT_CC(N); break;
64 case ISD::SETCC: Result = PromoteIntRes_SETCC(N); break;
65 case ISD::SHL: Result = PromoteIntRes_SHL(N); break;
66 case ISD::SIGN_EXTEND_INREG:
67 Result = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
68 case ISD::SRA: Result = PromoteIntRes_SRA(N); break;
69 case ISD::SRL: Result = PromoteIntRes_SRL(N); break;
70 case ISD::TRUNCATE: Result = PromoteIntRes_TRUNCATE(N); break;
71 case ISD::UNDEF: Result = PromoteIntRes_UNDEF(N); break;
72 case ISD::VAARG: Result = PromoteIntRes_VAARG(N); break;
74 case ISD::SIGN_EXTEND:
75 case ISD::ZERO_EXTEND:
76 case ISD::ANY_EXTEND: Result = PromoteIntRes_INT_EXTEND(N); break;
79 case ISD::FP_TO_UINT: Result = PromoteIntRes_FP_TO_XINT(N); break;
86 case ISD::MUL: Result = PromoteIntRes_SimpleIntBinOp(N); break;
89 case ISD::SREM: Result = PromoteIntRes_SDIV(N); break;
92 case ISD::UREM: Result = PromoteIntRes_UDIV(N); break;
99 case ISD::UMULO: Result = PromoteIntRes_XALUO(N, ResNo); break;
101 case ISD::ATOMIC_LOAD_ADD_8:
102 case ISD::ATOMIC_LOAD_SUB_8:
103 case ISD::ATOMIC_LOAD_AND_8:
104 case ISD::ATOMIC_LOAD_OR_8:
105 case ISD::ATOMIC_LOAD_XOR_8:
106 case ISD::ATOMIC_LOAD_NAND_8:
107 case ISD::ATOMIC_LOAD_MIN_8:
108 case ISD::ATOMIC_LOAD_MAX_8:
109 case ISD::ATOMIC_LOAD_UMIN_8:
110 case ISD::ATOMIC_LOAD_UMAX_8:
111 case ISD::ATOMIC_SWAP_8:
112 case ISD::ATOMIC_LOAD_ADD_16:
113 case ISD::ATOMIC_LOAD_SUB_16:
114 case ISD::ATOMIC_LOAD_AND_16:
115 case ISD::ATOMIC_LOAD_OR_16:
116 case ISD::ATOMIC_LOAD_XOR_16:
117 case ISD::ATOMIC_LOAD_NAND_16:
118 case ISD::ATOMIC_LOAD_MIN_16:
119 case ISD::ATOMIC_LOAD_MAX_16:
120 case ISD::ATOMIC_LOAD_UMIN_16:
121 case ISD::ATOMIC_LOAD_UMAX_16:
122 case ISD::ATOMIC_SWAP_16:
123 case ISD::ATOMIC_LOAD_ADD_32:
124 case ISD::ATOMIC_LOAD_SUB_32:
125 case ISD::ATOMIC_LOAD_AND_32:
126 case ISD::ATOMIC_LOAD_OR_32:
127 case ISD::ATOMIC_LOAD_XOR_32:
128 case ISD::ATOMIC_LOAD_NAND_32:
129 case ISD::ATOMIC_LOAD_MIN_32:
130 case ISD::ATOMIC_LOAD_MAX_32:
131 case ISD::ATOMIC_LOAD_UMIN_32:
132 case ISD::ATOMIC_LOAD_UMAX_32:
133 case ISD::ATOMIC_SWAP_32:
134 case ISD::ATOMIC_LOAD_ADD_64:
135 case ISD::ATOMIC_LOAD_SUB_64:
136 case ISD::ATOMIC_LOAD_AND_64:
137 case ISD::ATOMIC_LOAD_OR_64:
138 case ISD::ATOMIC_LOAD_XOR_64:
139 case ISD::ATOMIC_LOAD_NAND_64:
140 case ISD::ATOMIC_LOAD_MIN_64:
141 case ISD::ATOMIC_LOAD_MAX_64:
142 case ISD::ATOMIC_LOAD_UMIN_64:
143 case ISD::ATOMIC_LOAD_UMAX_64:
144 case ISD::ATOMIC_SWAP_64:
145 Result = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
147 case ISD::ATOMIC_CMP_SWAP_8:
148 case ISD::ATOMIC_CMP_SWAP_16:
149 case ISD::ATOMIC_CMP_SWAP_32:
150 case ISD::ATOMIC_CMP_SWAP_64:
151 Result = PromoteIntRes_Atomic2(cast<AtomicSDNode>(N)); break;
154 // If Result is null, the sub-method took care of registering the result.
155 if (Result.getNode())
156 SetPromotedInteger(SDValue(N, ResNo), Result);
159 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
160 // Sign-extend the new bits, and continue the assertion.
161 SDValue Op = SExtPromotedInteger(N->getOperand(0));
162 return DAG.getNode(ISD::AssertSext, Op.getValueType(), Op, N->getOperand(1));
165 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
166 // Zero the new bits, and continue the assertion.
167 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
168 return DAG.getNode(ISD::AssertZext, Op.getValueType(), Op, N->getOperand(1));
171 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
172 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
173 SDValue Res = DAG.getAtomic(N->getOpcode(), N->getChain(), N->getBasePtr(),
174 Op2, N->getSrcValue(), N->getAlignment());
175 // Legalized the chain result - switch anything that used the old chain to
177 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
181 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic2(AtomicSDNode *N) {
182 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
183 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
184 SDValue Res = DAG.getAtomic(N->getOpcode(), N->getChain(), N->getBasePtr(),
185 Op2, Op3, N->getSrcValue(), N->getAlignment());
186 // Legalized the chain result - switch anything that used the old chain to
188 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
192 SDValue DAGTypeLegalizer::PromoteIntRes_BIT_CONVERT(SDNode *N) {
193 SDValue InOp = N->getOperand(0);
194 MVT InVT = InOp.getValueType();
195 MVT NInVT = TLI.getTypeToTransformTo(InVT);
196 MVT OutVT = N->getValueType(0);
197 MVT NOutVT = TLI.getTypeToTransformTo(OutVT);
199 switch (getTypeAction(InVT)) {
201 assert(false && "Unknown type action!");
206 if (NOutVT.bitsEq(NInVT))
207 // The input promotes to the same size. Convert the promoted value.
208 return DAG.getNode(ISD::BIT_CONVERT, NOutVT, GetPromotedInteger(InOp));
211 // Promote the integer operand by hand.
212 return DAG.getNode(ISD::ANY_EXTEND, NOutVT, GetSoftenedFloat(InOp));
216 case ScalarizeVector:
217 // Convert the element to an integer and promote it by hand.
218 return DAG.getNode(ISD::ANY_EXTEND, NOutVT,
219 BitConvertToInteger(GetScalarizedVector(InOp)));
221 // For example, i32 = BIT_CONVERT v2i16 on alpha. Convert the split
222 // pieces of the input into integers and reassemble in the final type.
224 GetSplitVector(N->getOperand(0), Lo, Hi);
225 Lo = BitConvertToInteger(Lo);
226 Hi = BitConvertToInteger(Hi);
228 if (TLI.isBigEndian())
231 InOp = DAG.getNode(ISD::ANY_EXTEND,
232 MVT::getIntegerVT(NOutVT.getSizeInBits()),
233 JoinIntegers(Lo, Hi));
234 return DAG.getNode(ISD::BIT_CONVERT, NOutVT, InOp);
237 // Otherwise, lower the bit-convert to a store/load from the stack.
239 // Create the stack frame object. Make sure it is aligned for both
240 // the source and destination types.
241 SDValue FIPtr = DAG.CreateStackTemporary(InVT, OutVT);
243 // Emit a store to the stack slot.
244 SDValue Store = DAG.getStore(DAG.getEntryNode(), InOp, FIPtr, NULL, 0);
246 // Result is an extending load from the stack slot.
247 return DAG.getExtLoad(ISD::EXTLOAD, NOutVT, Store, FIPtr, NULL, 0, OutVT);
250 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
251 SDValue Op = GetPromotedInteger(N->getOperand(0));
252 MVT OVT = N->getValueType(0);
253 MVT NVT = Op.getValueType();
255 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
256 return DAG.getNode(ISD::SRL, NVT, DAG.getNode(ISD::BSWAP, NVT, Op),
257 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
260 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
261 // The pair element type may be legal, or may not promote to the same type as
262 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
263 return DAG.getNode(ISD::ANY_EXTEND,
264 TLI.getTypeToTransformTo(N->getValueType(0)),
265 JoinIntegers(N->getOperand(0), N->getOperand(1)));
268 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
269 MVT VT = N->getValueType(0);
270 // Zero extend things like i1, sign extend everything else. It shouldn't
271 // matter in theory which one we pick, but this tends to give better code?
272 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
273 SDValue Result = DAG.getNode(Opc, TLI.getTypeToTransformTo(VT),
275 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
279 SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) {
280 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
281 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
282 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
283 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
284 "can only promote integers");
285 MVT OutVT = TLI.getTypeToTransformTo(N->getValueType(0));
286 return DAG.getConvertRndSat(OutVT, N->getOperand(0),
287 N->getOperand(1), N->getOperand(2),
288 N->getOperand(3), N->getOperand(4), CvtCode);
291 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
292 // Zero extend to the promoted type and do the count there.
293 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
294 MVT OVT = N->getValueType(0);
295 MVT NVT = Op.getValueType();
296 Op = DAG.getNode(ISD::CTLZ, NVT, Op);
297 // Subtract off the extra leading bits in the bigger type.
298 return DAG.getNode(ISD::SUB, NVT, Op,
299 DAG.getConstant(NVT.getSizeInBits() -
300 OVT.getSizeInBits(), NVT));
303 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
304 // Zero extend to the promoted type and do the count there.
305 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
306 return DAG.getNode(ISD::CTPOP, Op.getValueType(), Op);
309 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
310 SDValue Op = GetPromotedInteger(N->getOperand(0));
311 MVT OVT = N->getValueType(0);
312 MVT NVT = Op.getValueType();
313 // The count is the same in the promoted type except if the original
314 // value was zero. This can be handled by setting the bit just off
315 // the top of the original type.
316 APInt TopBit(NVT.getSizeInBits(), 0);
317 TopBit.set(OVT.getSizeInBits());
318 Op = DAG.getNode(ISD::OR, NVT, Op, DAG.getConstant(TopBit, NVT));
319 return DAG.getNode(ISD::CTTZ, NVT, Op);
322 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
323 MVT OldVT = N->getValueType(0);
324 SDValue OldVec = N->getOperand(0);
325 unsigned OldElts = OldVec.getValueType().getVectorNumElements();
328 assert(!isTypeLegal(OldVec.getValueType()) &&
329 "Legal one-element vector of a type needing promotion!");
330 // It is tempting to follow GetScalarizedVector by a call to
331 // GetPromotedInteger, but this would be wrong because the
332 // scalarized value may not yet have been processed.
333 return DAG.getNode(ISD::ANY_EXTEND, TLI.getTypeToTransformTo(OldVT),
334 GetScalarizedVector(OldVec));
337 // Convert to a vector half as long with an element type of twice the width,
338 // for example <4 x i16> -> <2 x i32>.
339 assert(!(OldElts & 1) && "Odd length vectors not supported!");
340 MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
341 assert(OldVT.isSimple() && NewVT.isSimple());
343 SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT,
344 MVT::getVectorVT(NewVT, OldElts / 2),
347 // Extract the element at OldIdx / 2 from the new vector.
348 SDValue OldIdx = N->getOperand(1);
349 SDValue NewIdx = DAG.getNode(ISD::SRL, OldIdx.getValueType(), OldIdx,
350 DAG.getConstant(1, TLI.getShiftAmountTy()));
351 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, NewIdx);
353 // Select the appropriate half of the element: Lo if OldIdx was even,
356 SDValue Hi = DAG.getNode(ISD::SRL, NewVT, Elt,
357 DAG.getConstant(OldVT.getSizeInBits(),
358 TLI.getShiftAmountTy()));
359 if (TLI.isBigEndian())
362 SDValue Odd = DAG.getNode(ISD::TRUNCATE, MVT::i1, OldIdx);
363 return DAG.getNode(ISD::SELECT, NewVT, Odd, Hi, Lo);
366 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
367 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
368 unsigned NewOpc = N->getOpcode();
370 // If we're promoting a UINT to a larger size, check to see if the new node
371 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
372 // we can use that instead. This allows us to generate better code for
373 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
374 // legal, such as PowerPC.
375 if (N->getOpcode() == ISD::FP_TO_UINT &&
376 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
377 TLI.isOperationLegal(ISD::FP_TO_SINT, NVT))
378 NewOpc = ISD::FP_TO_SINT;
380 SDValue Res = DAG.getNode(NewOpc, NVT, N->getOperand(0));
382 // Assert that the converted value fits in the original type. If it doesn't
383 // (eg: because the value being converted is too big), then the result of the
384 // original operation was undefined anyway, so the assert is still correct.
385 return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ?
386 ISD::AssertZext : ISD::AssertSext,
387 NVT, Res, DAG.getValueType(N->getValueType(0)));
390 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
391 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
393 if (getTypeAction(N->getOperand(0).getValueType()) == PromoteInteger) {
394 SDValue Res = GetPromotedInteger(N->getOperand(0));
395 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
397 // If the result and operand types are the same after promotion, simplify
398 // to an in-register extension.
399 if (NVT == Res.getValueType()) {
400 // The high bits are not guaranteed to be anything. Insert an extend.
401 if (N->getOpcode() == ISD::SIGN_EXTEND)
402 return DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res,
403 DAG.getValueType(N->getOperand(0).getValueType()));
404 if (N->getOpcode() == ISD::ZERO_EXTEND)
405 return DAG.getZeroExtendInReg(Res, N->getOperand(0).getValueType());
406 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
411 // Otherwise, just extend the original operand all the way to the larger type.
412 return DAG.getNode(N->getOpcode(), NVT, N->getOperand(0));
415 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
416 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
417 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
418 ISD::LoadExtType ExtType =
419 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
420 SDValue Res = DAG.getExtLoad(ExtType, NVT, N->getChain(), N->getBasePtr(),
421 N->getSrcValue(), N->getSrcValueOffset(),
422 N->getMemoryVT(), N->isVolatile(),
425 // Legalized the chain result - switch anything that used the old chain to
427 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
431 SDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) {
432 // Sign extend the input.
433 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
434 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
435 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
438 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
439 SDValue LHS = GetPromotedInteger(N->getOperand(1));
440 SDValue RHS = GetPromotedInteger(N->getOperand(2));
441 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
444 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
445 SDValue LHS = GetPromotedInteger(N->getOperand(2));
446 SDValue RHS = GetPromotedInteger(N->getOperand(3));
447 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
448 N->getOperand(1), LHS, RHS, N->getOperand(4));
451 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
452 MVT SVT = TLI.getSetCCResultType(N->getOperand(0));
453 assert(isTypeLegal(SVT) && "Illegal SetCC type!");
455 // Get the SETCC result using the canonical SETCC type.
456 SDValue SetCC = DAG.getNode(ISD::SETCC, SVT, N->getOperand(0),
457 N->getOperand(1), N->getOperand(2));
459 // Convert to the expected type.
460 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
461 assert(NVT.bitsLE(SVT) && "Integer type overpromoted?");
462 return DAG.getNode(ISD::TRUNCATE, NVT, SetCC);
465 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
466 return DAG.getNode(ISD::SHL, TLI.getTypeToTransformTo(N->getValueType(0)),
467 GetPromotedInteger(N->getOperand(0)), N->getOperand(1));
470 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
471 SDValue Op = GetPromotedInteger(N->getOperand(0));
472 return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(), Op,
476 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
477 // The input may have strange things in the top bits of the registers, but
478 // these operations don't care. They may have weird bits going out, but
479 // that too is okay if they are integer operations.
480 SDValue LHS = GetPromotedInteger(N->getOperand(0));
481 SDValue RHS = GetPromotedInteger(N->getOperand(1));
482 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
485 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
486 // The input value must be properly sign extended.
487 SDValue Res = SExtPromotedInteger(N->getOperand(0));
488 return DAG.getNode(ISD::SRA, Res.getValueType(), Res, N->getOperand(1));
491 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
492 // The input value must be properly zero extended.
493 MVT VT = N->getValueType(0);
494 MVT NVT = TLI.getTypeToTransformTo(VT);
495 SDValue Res = ZExtPromotedInteger(N->getOperand(0));
496 return DAG.getNode(ISD::SRL, NVT, Res, N->getOperand(1));
499 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
500 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
503 switch (getTypeAction(N->getOperand(0).getValueType())) {
504 default: assert(0 && "Unknown type action!");
507 Res = N->getOperand(0);
510 Res = GetPromotedInteger(N->getOperand(0));
514 // Truncate to NVT instead of VT
515 return DAG.getNode(ISD::TRUNCATE, NVT, Res);
518 SDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) {
519 // Zero extend the input.
520 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
521 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
522 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
525 SDValue DAGTypeLegalizer::PromoteIntRes_XALUO(SDNode *N, unsigned ResNo) {
526 assert(ResNo == 1 && "Only boolean result promotion currently supported!");
528 // Simply change the return type of the boolean result.
529 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(1));
530 MVT ValueVTs[] = { N->getValueType(0), NVT };
531 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
532 SDValue Res = DAG.getNode(N->getOpcode(), DAG.getVTList(ValueVTs, 2), Ops, 2);
534 // Modified the sum result - switch anything that used the old sum to use
536 ReplaceValueWith(SDValue(N, 0), Res);
538 return SDValue(Res.getNode(), 1);
541 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
542 return DAG.getNode(ISD::UNDEF, TLI.getTypeToTransformTo(N->getValueType(0)));
545 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
546 SDValue Chain = N->getOperand(0); // Get the chain.
547 SDValue Ptr = N->getOperand(1); // Get the pointer.
548 MVT VT = N->getValueType(0);
550 MVT RegVT = TLI.getRegisterType(VT);
551 unsigned NumRegs = TLI.getNumRegisters(VT);
552 // The argument is passed as NumRegs registers of type RegVT.
554 SmallVector<SDValue, 8> Parts(NumRegs);
555 for (unsigned i = 0; i < NumRegs; ++i) {
556 Parts[i] = DAG.getVAArg(RegVT, Chain, Ptr, N->getOperand(2));
557 Chain = Parts[i].getValue(1);
560 // Handle endianness of the load.
561 if (TLI.isBigEndian())
562 std::reverse(Parts.begin(), Parts.end());
564 // Assemble the parts in the promoted type.
565 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
566 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, NVT, Parts[0]);
567 for (unsigned i = 1; i < NumRegs; ++i) {
568 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, NVT, Parts[i]);
569 // Shift it to the right position and "or" it in.
570 Part = DAG.getNode(ISD::SHL, NVT, Part,
571 DAG.getConstant(i * RegVT.getSizeInBits(),
572 TLI.getShiftAmountTy()));
573 Res = DAG.getNode(ISD::OR, NVT, Res, Part);
576 // Modified the chain result - switch anything that used the old chain to
578 ReplaceValueWith(SDValue(N, 1), Chain);
584 //===----------------------------------------------------------------------===//
585 // Integer Operand Promotion
586 //===----------------------------------------------------------------------===//
588 /// PromoteIntegerOperand - This method is called when the specified operand of
589 /// the specified node is found to need promotion. At this point, all of the
590 /// result types of the node are known to be legal, but other operands of the
591 /// node may need promotion or expansion as well as the specified one.
592 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
593 DEBUG(cerr << "Promote integer operand: "; N->dump(&DAG); cerr << "\n");
594 SDValue Res = SDValue();
596 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
597 == TargetLowering::Custom)
598 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
600 if (Res.getNode() == 0) {
601 switch (N->getOpcode()) {
604 cerr << "PromoteIntegerOperand Op #" << OpNo << ": ";
605 N->dump(&DAG); cerr << "\n";
607 assert(0 && "Do not know how to promote this operator's operand!");
610 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
611 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
612 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
613 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
614 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
615 case ISD::CONVERT_RNDSAT:
616 Res = PromoteIntOp_CONVERT_RNDSAT(N); break;
617 case ISD::INSERT_VECTOR_ELT:
618 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
619 case ISD::MEMBARRIER: Res = PromoteIntOp_MEMBARRIER(N); break;
620 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
621 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
622 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
623 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
624 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
625 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
627 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
628 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
629 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
633 // If the result is null, the sub-method took care of registering results etc.
634 if (!Res.getNode()) return false;
636 // If the result is N, the sub-method updated N in place. Tell the legalizer
638 if (Res.getNode() == N)
641 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
642 "Invalid operand expansion");
644 ReplaceValueWith(SDValue(N, 0), Res);
648 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
649 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
650 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
651 ISD::CondCode CCCode) {
652 // We have to insert explicit sign or zero extends. Note that we could
653 // insert sign extends for ALL conditions, but zero extend is cheaper on
654 // many machines (an AND instead of two shifts), so prefer it.
656 default: assert(0 && "Unknown integer comparison!");
663 // ALL of these operations will work if we either sign or zero extend
664 // the operands (including the unsigned comparisons!). Zero extend is
665 // usually a simpler/cheaper operation, so prefer it.
666 NewLHS = ZExtPromotedInteger(NewLHS);
667 NewRHS = ZExtPromotedInteger(NewRHS);
673 NewLHS = SExtPromotedInteger(NewLHS);
674 NewRHS = SExtPromotedInteger(NewRHS);
679 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
680 SDValue Op = GetPromotedInteger(N->getOperand(0));
681 return DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
684 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
685 assert(OpNo == 2 && "Don't know how to promote this operand!");
687 SDValue LHS = N->getOperand(2);
688 SDValue RHS = N->getOperand(3);
689 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
691 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
693 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
694 N->getOperand(1), LHS, RHS, N->getOperand(4));
697 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
698 assert(OpNo == 1 && "only know how to promote condition");
699 SDValue Cond = GetPromotedInteger(N->getOperand(1)); // Promote condition.
701 // Make sure the extra bits coming from type promotion conform to
702 // getBooleanContents.
703 unsigned CondBits = Cond.getValueSizeInBits();
704 switch (TLI.getBooleanContents()) {
706 assert(false && "Unknown BooleanContent!");
707 case TargetLowering::UndefinedBooleanContent:
708 // The promoted value, which may contain rubbish in the upper bits, is fine.
710 case TargetLowering::ZeroOrOneBooleanContent:
711 if (!DAG.MaskedValueIsZero(Cond,APInt::getHighBitsSet(CondBits,CondBits-1)))
712 Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
714 case TargetLowering::ZeroOrNegativeOneBooleanContent:
715 if (DAG.ComputeNumSignBits(Cond) != CondBits)
716 Cond = DAG.getNode(ISD::SIGN_EXTEND_INREG, Cond.getValueType(), Cond,
717 DAG.getValueType(MVT::i1));
721 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
722 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Cond,
726 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
727 // Since the result type is legal, the operands must promote to it.
728 MVT OVT = N->getOperand(0).getValueType();
729 SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
730 SDValue Hi = GetPromotedInteger(N->getOperand(1));
731 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
733 Hi = DAG.getNode(ISD::SHL, N->getValueType(0), Hi,
734 DAG.getConstant(OVT.getSizeInBits(),
735 TLI.getShiftAmountTy()));
736 return DAG.getNode(ISD::OR, N->getValueType(0), Lo, Hi);
739 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
740 // The vector type is legal but the element type is not. This implies
741 // that the vector is a power-of-two in length and that the element
742 // type does not have a strange size (eg: it is not i1).
743 MVT VecVT = N->getValueType(0);
744 unsigned NumElts = VecVT.getVectorNumElements();
745 assert(!(NumElts & 1) && "Legal vector of one illegal element?");
747 // Build a vector of half the length out of elements of twice the bitwidth.
748 // For example <4 x i16> -> <2 x i32>.
749 MVT OldVT = N->getOperand(0).getValueType();
750 MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
751 assert(OldVT.isSimple() && NewVT.isSimple());
753 std::vector<SDValue> NewElts;
754 NewElts.reserve(NumElts/2);
756 for (unsigned i = 0; i < NumElts; i += 2) {
757 // Combine two successive elements into one promoted element.
758 SDValue Lo = N->getOperand(i);
759 SDValue Hi = N->getOperand(i+1);
760 if (TLI.isBigEndian())
762 NewElts.push_back(JoinIntegers(Lo, Hi));
765 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR,
766 MVT::getVectorVT(NewVT, NewElts.size()),
767 &NewElts[0], NewElts.size());
769 // Convert the new vector to the old vector type.
770 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
773 SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) {
774 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
775 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
776 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
777 CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) &&
778 "can only promote integer arguments");
779 SDValue InOp = GetPromotedInteger(N->getOperand(0));
780 return DAG.getConvertRndSat(N->getValueType(0), InOp,
781 N->getOperand(1), N->getOperand(2),
782 N->getOperand(3), N->getOperand(4), CvtCode);
785 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
788 // Promote the inserted value. This is valid because the type does not
789 // have to match the vector element type.
791 // Check that any extra bits introduced will be truncated away.
792 assert(N->getOperand(1).getValueType().getSizeInBits() >=
793 N->getValueType(0).getVectorElementType().getSizeInBits() &&
794 "Type of inserted value narrower than vector element type!");
795 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
796 GetPromotedInteger(N->getOperand(1)),
800 assert(OpNo == 2 && "Different operand and result vector types?");
802 // Promote the index.
803 SDValue Idx = ZExtPromotedInteger(N->getOperand(2));
804 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
805 N->getOperand(1), Idx);
808 SDValue DAGTypeLegalizer::PromoteIntOp_MEMBARRIER(SDNode *N) {
810 NewOps[0] = N->getOperand(0);
811 for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
812 SDValue Flag = GetPromotedInteger(N->getOperand(i));
813 NewOps[i] = DAG.getZeroExtendInReg(Flag, MVT::i1);
815 return DAG.UpdateNodeOperands(SDValue (N, 0), NewOps,
816 array_lengthof(NewOps));
819 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
820 assert(OpNo == 0 && "Only know how to promote condition");
821 SDValue Cond = GetPromotedInteger(N->getOperand(0));
823 // Promote all the way up to SVT, the canonical SetCC type.
824 // FIXME: Not clear what value to pass to getSetCCResultType.
825 // [This only matters for CellSPU since all other targets
826 // ignore the argument.] We used to pass Cond, resulting in
827 // SVT = MVT::i8, but CellSPU has no select patterns for i8,
828 // causing an abort later. Passing the result type works
829 // around the problem.
830 MVT SVT = TLI.getSetCCResultType(N->getOperand(1));
831 assert(isTypeLegal(SVT) && "Illegal SetCC type!");
832 assert(Cond.getValueType().bitsLE(SVT) && "Unexpected SetCC type!");
834 // Make sure the extra bits conform to getBooleanContents. There are
835 // two sets of extra bits: those in Cond, which come from type promotion,
836 // and those we need to add to have the final type be SVT (for most targets
837 // this last set of bits is empty).
838 unsigned CondBits = Cond.getValueSizeInBits();
839 ISD::NodeType ExtendCode;
840 switch (TLI.getBooleanContents()) {
842 assert(false && "Unknown BooleanContent!");
843 case TargetLowering::UndefinedBooleanContent:
844 // Extend to SVT by adding rubbish.
845 ExtendCode = ISD::ANY_EXTEND;
847 case TargetLowering::ZeroOrOneBooleanContent:
848 ExtendCode = ISD::ZERO_EXTEND;
849 if (!DAG.MaskedValueIsZero(Cond,APInt::getHighBitsSet(CondBits,CondBits-1)))
850 // All extra bits need to be cleared. Do this by zero extending the
851 // original condition value all the way to SVT.
852 Cond = N->getOperand(0);
854 case TargetLowering::ZeroOrNegativeOneBooleanContent: {
855 ExtendCode = ISD::SIGN_EXTEND;
856 unsigned SignBits = DAG.ComputeNumSignBits(Cond);
857 if (SignBits != CondBits)
858 // All extra bits need to be sign extended. Do this by sign extending the
859 // original condition value all the way to SVT.
860 Cond = N->getOperand(0);
864 Cond = DAG.getNode(ExtendCode, SVT, Cond);
866 return DAG.UpdateNodeOperands(SDValue(N, 0), Cond,
867 N->getOperand(1), N->getOperand(2));
870 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
871 assert(OpNo == 0 && "Don't know how to promote this operand!");
873 SDValue LHS = N->getOperand(0);
874 SDValue RHS = N->getOperand(1);
875 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
877 // The CC (#4) and the possible return values (#2 and #3) have legal types.
878 return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2),
879 N->getOperand(3), N->getOperand(4));
882 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
883 assert(OpNo == 0 && "Don't know how to promote this operand!");
885 SDValue LHS = N->getOperand(0);
886 SDValue RHS = N->getOperand(1);
887 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
889 // The CC (#2) is always legal.
890 return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2));
893 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
894 SDValue Op = GetPromotedInteger(N->getOperand(0));
895 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
896 return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(),
897 Op, DAG.getValueType(N->getOperand(0).getValueType()));
900 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
901 return DAG.UpdateNodeOperands(SDValue(N, 0),
902 SExtPromotedInteger(N->getOperand(0)));
905 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
906 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
907 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
908 int SVOffset = N->getSrcValueOffset();
909 unsigned Alignment = N->getAlignment();
910 bool isVolatile = N->isVolatile();
912 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
914 assert(!N->isTruncatingStore() && "Cannot promote this store operand!");
916 // Truncate the value and store the result.
917 return DAG.getTruncStore(Ch, Val, Ptr, N->getSrcValue(),
918 SVOffset, N->getMemoryVT(),
919 isVolatile, Alignment);
922 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
923 SDValue Op = GetPromotedInteger(N->getOperand(0));
924 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), Op);
927 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
928 return DAG.UpdateNodeOperands(SDValue(N, 0),
929 ZExtPromotedInteger(N->getOperand(0)));
932 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
933 SDValue Op = GetPromotedInteger(N->getOperand(0));
934 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
935 return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
939 //===----------------------------------------------------------------------===//
940 // Integer Result Expansion
941 //===----------------------------------------------------------------------===//
943 /// ExpandIntegerResult - This method is called when the specified result of the
944 /// specified node is found to need expansion. At this point, the node may also
945 /// have invalid operands or may have other results that need promotion, we just
946 /// know that (at least) one result needs expansion.
947 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
948 DEBUG(cerr << "Expand integer result: "; N->dump(&DAG); cerr << "\n");
952 // See if the target wants to custom expand this node.
953 if (CustomLowerResults(N, ResNo))
956 switch (N->getOpcode()) {
959 cerr << "ExpandIntegerResult #" << ResNo << ": ";
960 N->dump(&DAG); cerr << "\n";
962 assert(0 && "Do not know how to expand the result of this operator!");
965 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
966 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
967 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
968 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
970 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
971 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
972 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
973 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
974 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
976 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
977 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
978 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
979 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
980 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
981 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
982 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
983 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
984 case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
985 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
986 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
987 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
988 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
989 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
990 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
991 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
992 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
993 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
994 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
995 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
999 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
1002 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
1005 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
1008 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
1012 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
1015 // If Lo/Hi is null, the sub-method took care of registering results etc.
1017 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
1020 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
1021 /// and the shift amount is a constant 'Amt'. Expand the operation.
1022 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
1023 SDValue &Lo, SDValue &Hi) {
1024 // Expand the incoming operand to be shifted, so that we have its parts
1026 GetExpandedInteger(N->getOperand(0), InL, InH);
1028 MVT NVT = InL.getValueType();
1029 unsigned VTBits = N->getValueType(0).getSizeInBits();
1030 unsigned NVTBits = NVT.getSizeInBits();
1031 MVT ShTy = N->getOperand(1).getValueType();
1033 if (N->getOpcode() == ISD::SHL) {
1035 Lo = Hi = DAG.getConstant(0, NVT);
1036 } else if (Amt > NVTBits) {
1037 Lo = DAG.getConstant(0, NVT);
1038 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
1039 } else if (Amt == NVTBits) {
1040 Lo = DAG.getConstant(0, NVT);
1042 } else if (Amt == 1 &&
1043 TLI.isOperationLegal(ISD::ADDC, TLI.getTypeToExpandTo(NVT))) {
1044 // Emit this X << 1 as X+X.
1045 SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1046 SDValue LoOps[2] = { InL, InL };
1047 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1048 SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
1049 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1051 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt, ShTy));
1052 Hi = DAG.getNode(ISD::OR, NVT,
1053 DAG.getNode(ISD::SHL, NVT, InH,
1054 DAG.getConstant(Amt, ShTy)),
1055 DAG.getNode(ISD::SRL, NVT, InL,
1056 DAG.getConstant(NVTBits-Amt, ShTy)));
1061 if (N->getOpcode() == ISD::SRL) {
1063 Lo = DAG.getConstant(0, NVT);
1064 Hi = DAG.getConstant(0, NVT);
1065 } else if (Amt > NVTBits) {
1066 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
1067 Hi = DAG.getConstant(0, NVT);
1068 } else if (Amt == NVTBits) {
1070 Hi = DAG.getConstant(0, NVT);
1072 Lo = DAG.getNode(ISD::OR, NVT,
1073 DAG.getNode(ISD::SRL, NVT, InL,
1074 DAG.getConstant(Amt, ShTy)),
1075 DAG.getNode(ISD::SHL, NVT, InH,
1076 DAG.getConstant(NVTBits-Amt, ShTy)));
1077 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt, ShTy));
1082 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1084 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1085 DAG.getConstant(NVTBits-1, ShTy));
1086 } else if (Amt > NVTBits) {
1087 Lo = DAG.getNode(ISD::SRA, NVT, InH,
1088 DAG.getConstant(Amt-NVTBits, ShTy));
1089 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1090 DAG.getConstant(NVTBits-1, ShTy));
1091 } else if (Amt == NVTBits) {
1093 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1094 DAG.getConstant(NVTBits-1, ShTy));
1096 Lo = DAG.getNode(ISD::OR, NVT,
1097 DAG.getNode(ISD::SRL, NVT, InL,
1098 DAG.getConstant(Amt, ShTy)),
1099 DAG.getNode(ISD::SHL, NVT, InH,
1100 DAG.getConstant(NVTBits-Amt, ShTy)));
1101 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Amt, ShTy));
1105 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1106 /// this shift based on knowledge of the high bit of the shift amount. If we
1107 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1109 bool DAGTypeLegalizer::
1110 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1111 SDValue Amt = N->getOperand(1);
1112 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1113 MVT ShTy = Amt.getValueType();
1114 unsigned ShBits = ShTy.getSizeInBits();
1115 unsigned NVTBits = NVT.getSizeInBits();
1116 assert(isPowerOf2_32(NVTBits) &&
1117 "Expanded integer type size not a power of two!");
1119 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
1120 APInt KnownZero, KnownOne;
1121 DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
1123 // If we don't know anything about the high bits, exit.
1124 if (((KnownZero|KnownOne) & HighBitMask) == 0)
1127 // Get the incoming operand to be shifted.
1129 GetExpandedInteger(N->getOperand(0), InL, InH);
1131 // If we know that any of the high bits of the shift amount are one, then we
1132 // can do this as a couple of simple shifts.
1133 if (KnownOne.intersects(HighBitMask)) {
1134 // Mask out the high bit, which we know is set.
1135 Amt = DAG.getNode(ISD::AND, ShTy, Amt,
1136 DAG.getConstant(~HighBitMask, ShTy));
1138 switch (N->getOpcode()) {
1139 default: assert(0 && "Unknown shift");
1141 Lo = DAG.getConstant(0, NVT); // Low part is zero.
1142 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
1145 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
1146 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
1149 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
1150 DAG.getConstant(NVTBits-1, ShTy));
1151 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
1156 // If we know that all of the high bits of the shift amount are zero, then we
1157 // can do this as a couple of simple shifts.
1158 if ((KnownZero & HighBitMask) == HighBitMask) {
1160 SDValue Amt2 = DAG.getNode(ISD::SUB, ShTy,
1161 DAG.getConstant(NVTBits, ShTy),
1164 switch (N->getOpcode()) {
1165 default: assert(0 && "Unknown shift");
1166 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1168 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1171 Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
1172 Hi = DAG.getNode(ISD::OR, NVT,
1173 DAG.getNode(Op1, NVT, InH, Amt),
1174 DAG.getNode(Op2, NVT, InL, Amt2));
1181 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
1182 SDValue &Lo, SDValue &Hi) {
1183 // Expand the subcomponents.
1184 SDValue LHSL, LHSH, RHSL, RHSH;
1185 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1186 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1188 MVT NVT = LHSL.getValueType();
1189 SDValue LoOps[2] = { LHSL, RHSL };
1190 SDValue HiOps[3] = { LHSH, RHSH };
1192 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
1193 // them. TODO: Teach operation legalization how to expand unsupported
1194 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
1195 // a carry of type MVT::Flag, but there doesn't seem to be any way to
1196 // generate a value of this type in the expanded code sequence.
1198 TLI.isOperationLegal(N->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC,
1199 TLI.getTypeToExpandTo(NVT));
1202 SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1203 if (N->getOpcode() == ISD::ADD) {
1204 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1205 HiOps[2] = Lo.getValue(1);
1206 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1208 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1209 HiOps[2] = Lo.getValue(1);
1210 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1213 if (N->getOpcode() == ISD::ADD) {
1214 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
1215 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
1216 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo), Lo, LoOps[0],
1218 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
1219 DAG.getConstant(1, NVT),
1220 DAG.getConstant(0, NVT));
1221 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo), Lo, LoOps[1],
1223 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
1224 DAG.getConstant(1, NVT), Carry1);
1225 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
1227 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
1228 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
1229 SDValue Cmp = DAG.getSetCC(TLI.getSetCCResultType(LoOps[0]),
1230 LoOps[0], LoOps[1], ISD::SETULT);
1231 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
1232 DAG.getConstant(1, NVT),
1233 DAG.getConstant(0, NVT));
1234 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
1239 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
1240 SDValue &Lo, SDValue &Hi) {
1241 // Expand the subcomponents.
1242 SDValue LHSL, LHSH, RHSL, RHSH;
1243 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1244 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1245 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1246 SDValue LoOps[2] = { LHSL, RHSL };
1247 SDValue HiOps[3] = { LHSH, RHSH };
1249 if (N->getOpcode() == ISD::ADDC) {
1250 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1251 HiOps[2] = Lo.getValue(1);
1252 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1254 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1255 HiOps[2] = Lo.getValue(1);
1256 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1259 // Legalized the flag result - switch anything that used the old flag to
1261 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1264 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
1265 SDValue &Lo, SDValue &Hi) {
1266 // Expand the subcomponents.
1267 SDValue LHSL, LHSH, RHSL, RHSH;
1268 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1269 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1270 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1271 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
1272 SDValue HiOps[3] = { LHSH, RHSH };
1274 Lo = DAG.getNode(N->getOpcode(), VTList, LoOps, 3);
1275 HiOps[2] = Lo.getValue(1);
1276 Hi = DAG.getNode(N->getOpcode(), VTList, HiOps, 3);
1278 // Legalized the flag result - switch anything that used the old flag to
1280 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1283 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
1284 SDValue &Lo, SDValue &Hi) {
1285 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1286 SDValue Op = N->getOperand(0);
1287 if (Op.getValueType().bitsLE(NVT)) {
1288 // The low part is any extension of the input (which degenerates to a copy).
1289 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Op);
1290 Hi = DAG.getNode(ISD::UNDEF, NVT); // The high part is undefined.
1292 // For example, extension of an i48 to an i64. The operand type necessarily
1293 // promotes to the result type, so will end up being expanded too.
1294 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1295 "Only know how to promote this result!");
1296 SDValue Res = GetPromotedInteger(Op);
1297 assert(Res.getValueType() == N->getValueType(0) &&
1298 "Operand over promoted?");
1299 // Split the promoted operand. This will simplify when it is expanded.
1300 SplitInteger(Res, Lo, Hi);
1304 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
1305 SDValue &Lo, SDValue &Hi) {
1306 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1307 MVT NVT = Lo.getValueType();
1308 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1309 unsigned NVTBits = NVT.getSizeInBits();
1310 unsigned EVTBits = EVT.getSizeInBits();
1312 if (NVTBits < EVTBits) {
1313 Hi = DAG.getNode(ISD::AssertSext, NVT, Hi,
1314 DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1316 Lo = DAG.getNode(ISD::AssertSext, NVT, Lo, DAG.getValueType(EVT));
1317 // The high part replicates the sign bit of Lo, make it explicit.
1318 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1319 DAG.getConstant(NVTBits-1, TLI.getShiftAmountTy()));
1323 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
1324 SDValue &Lo, SDValue &Hi) {
1325 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1326 MVT NVT = Lo.getValueType();
1327 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1328 unsigned NVTBits = NVT.getSizeInBits();
1329 unsigned EVTBits = EVT.getSizeInBits();
1331 if (NVTBits < EVTBits) {
1332 Hi = DAG.getNode(ISD::AssertZext, NVT, Hi,
1333 DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1335 Lo = DAG.getNode(ISD::AssertZext, NVT, Lo, DAG.getValueType(EVT));
1336 // The high part must be zero, make it explicit.
1337 Hi = DAG.getConstant(0, NVT);
1341 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
1342 SDValue &Lo, SDValue &Hi) {
1343 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
1344 Lo = DAG.getNode(ISD::BSWAP, Lo.getValueType(), Lo);
1345 Hi = DAG.getNode(ISD::BSWAP, Hi.getValueType(), Hi);
1348 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
1349 SDValue &Lo, SDValue &Hi) {
1350 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1351 unsigned NBitWidth = NVT.getSizeInBits();
1352 const APInt &Cst = cast<ConstantSDNode>(N)->getAPIntValue();
1353 Lo = DAG.getConstant(APInt(Cst).trunc(NBitWidth), NVT);
1354 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
1357 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
1358 SDValue &Lo, SDValue &Hi) {
1359 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
1360 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1361 MVT NVT = Lo.getValueType();
1363 SDValue HiNotZero = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
1364 DAG.getConstant(0, NVT), ISD::SETNE);
1366 SDValue LoLZ = DAG.getNode(ISD::CTLZ, NVT, Lo);
1367 SDValue HiLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
1369 Lo = DAG.getNode(ISD::SELECT, NVT, HiNotZero, HiLZ,
1370 DAG.getNode(ISD::ADD, NVT, LoLZ,
1371 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1372 Hi = DAG.getConstant(0, NVT);
1375 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
1376 SDValue &Lo, SDValue &Hi) {
1377 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
1378 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1379 MVT NVT = Lo.getValueType();
1380 Lo = DAG.getNode(ISD::ADD, NVT, DAG.getNode(ISD::CTPOP, NVT, Lo),
1381 DAG.getNode(ISD::CTPOP, NVT, Hi));
1382 Hi = DAG.getConstant(0, NVT);
1385 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
1386 SDValue &Lo, SDValue &Hi) {
1387 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
1388 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1389 MVT NVT = Lo.getValueType();
1391 SDValue LoNotZero = DAG.getSetCC(TLI.getSetCCResultType(Lo), Lo,
1392 DAG.getConstant(0, NVT), ISD::SETNE);
1394 SDValue LoLZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
1395 SDValue HiLZ = DAG.getNode(ISD::CTTZ, NVT, Hi);
1397 Lo = DAG.getNode(ISD::SELECT, NVT, LoNotZero, LoLZ,
1398 DAG.getNode(ISD::ADD, NVT, HiLZ,
1399 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1400 Hi = DAG.getConstant(0, NVT);
1403 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
1405 MVT VT = N->getValueType(0);
1406 SDValue Op = N->getOperand(0);
1407 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
1408 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
1409 SplitInteger(MakeLibCall(LC, VT, &Op, 1, true/*sign irrelevant*/), Lo, Hi);
1412 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
1414 MVT VT = N->getValueType(0);
1415 SDValue Op = N->getOperand(0);
1416 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
1417 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
1418 SplitInteger(MakeLibCall(LC, VT, &Op, 1, false/*sign irrelevant*/), Lo, Hi);
1421 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
1422 SDValue &Lo, SDValue &Hi) {
1423 if (ISD::isNormalLoad(N)) {
1424 ExpandRes_NormalLoad(N, Lo, Hi);
1428 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1430 MVT VT = N->getValueType(0);
1431 MVT NVT = TLI.getTypeToTransformTo(VT);
1432 SDValue Ch = N->getChain();
1433 SDValue Ptr = N->getBasePtr();
1434 ISD::LoadExtType ExtType = N->getExtensionType();
1435 int SVOffset = N->getSrcValueOffset();
1436 unsigned Alignment = N->getAlignment();
1437 bool isVolatile = N->isVolatile();
1439 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1441 if (N->getMemoryVT().bitsLE(NVT)) {
1442 MVT EVT = N->getMemoryVT();
1444 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset, EVT,
1445 isVolatile, Alignment);
1447 // Remember the chain.
1448 Ch = Lo.getValue(1);
1450 if (ExtType == ISD::SEXTLOAD) {
1451 // The high part is obtained by SRA'ing all but one of the bits of the
1453 unsigned LoSize = Lo.getValueType().getSizeInBits();
1454 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1455 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
1456 } else if (ExtType == ISD::ZEXTLOAD) {
1457 // The high part is just a zero.
1458 Hi = DAG.getConstant(0, NVT);
1460 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
1461 // The high part is undefined.
1462 Hi = DAG.getNode(ISD::UNDEF, NVT);
1464 } else if (TLI.isLittleEndian()) {
1465 // Little-endian - low bits are at low addresses.
1466 Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1467 isVolatile, Alignment);
1469 unsigned ExcessBits =
1470 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
1471 MVT NEVT = MVT::getIntegerVT(ExcessBits);
1473 // Increment the pointer to the other half.
1474 unsigned IncrementSize = NVT.getSizeInBits()/8;
1475 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1476 DAG.getIntPtrConstant(IncrementSize));
1477 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(),
1478 SVOffset+IncrementSize, NEVT,
1479 isVolatile, MinAlign(Alignment, IncrementSize));
1481 // Build a factor node to remember that this load is independent of the
1483 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1486 // Big-endian - high bits are at low addresses. Favor aligned loads at
1487 // the cost of some bit-fiddling.
1488 MVT EVT = N->getMemoryVT();
1489 unsigned EBytes = EVT.getStoreSizeInBits()/8;
1490 unsigned IncrementSize = NVT.getSizeInBits()/8;
1491 unsigned ExcessBits = (EBytes - IncrementSize)*8;
1493 // Load both the high bits and maybe some of the low bits.
1494 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1495 MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits),
1496 isVolatile, Alignment);
1498 // Increment the pointer to the other half.
1499 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1500 DAG.getIntPtrConstant(IncrementSize));
1501 // Load the rest of the low bits.
1502 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Ch, Ptr, N->getSrcValue(),
1503 SVOffset+IncrementSize,
1504 MVT::getIntegerVT(ExcessBits),
1505 isVolatile, MinAlign(Alignment, IncrementSize));
1507 // Build a factor node to remember that this load is independent of the
1509 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1512 if (ExcessBits < NVT.getSizeInBits()) {
1513 // Transfer low bits from the bottom of Hi to the top of Lo.
1514 Lo = DAG.getNode(ISD::OR, NVT, Lo,
1515 DAG.getNode(ISD::SHL, NVT, Hi,
1516 DAG.getConstant(ExcessBits,
1517 TLI.getShiftAmountTy())));
1518 // Move high bits to the right position in Hi.
1519 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, NVT, Hi,
1520 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
1521 TLI.getShiftAmountTy()));
1525 // Legalized the chain result - switch anything that used the old chain to
1527 ReplaceValueWith(SDValue(N, 1), Ch);
1530 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
1531 SDValue &Lo, SDValue &Hi) {
1532 SDValue LL, LH, RL, RH;
1533 GetExpandedInteger(N->getOperand(0), LL, LH);
1534 GetExpandedInteger(N->getOperand(1), RL, RH);
1535 Lo = DAG.getNode(N->getOpcode(), LL.getValueType(), LL, RL);
1536 Hi = DAG.getNode(N->getOpcode(), LL.getValueType(), LH, RH);
1539 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
1540 SDValue &Lo, SDValue &Hi) {
1541 MVT VT = N->getValueType(0);
1542 MVT NVT = TLI.getTypeToTransformTo(VT);
1544 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
1545 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
1546 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
1547 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
1548 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1549 SDValue LL, LH, RL, RH;
1550 GetExpandedInteger(N->getOperand(0), LL, LH);
1551 GetExpandedInteger(N->getOperand(1), RL, RH);
1552 unsigned OuterBitSize = VT.getSizeInBits();
1553 unsigned InnerBitSize = NVT.getSizeInBits();
1554 unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1555 unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
1557 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
1558 if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
1559 DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
1560 // The inputs are both zero-extended.
1562 // We can emit a umul_lohi.
1563 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1564 Hi = SDValue(Lo.getNode(), 1);
1568 // We can emit a mulhu+mul.
1569 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1570 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1574 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
1575 // The input values are both sign-extended.
1577 // We can emit a smul_lohi.
1578 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1579 Hi = SDValue(Lo.getNode(), 1);
1583 // We can emit a mulhs+mul.
1584 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1585 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
1590 // Lo,Hi = umul LHS, RHS.
1591 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
1592 DAG.getVTList(NVT, NVT), LL, RL);
1594 Hi = UMulLOHI.getValue(1);
1595 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1596 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1597 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1598 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1602 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1603 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1604 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1605 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1606 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1607 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1612 // If nothing else, we can make a libcall.
1613 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1615 LC = RTLIB::MUL_I32;
1616 else if (VT == MVT::i64)
1617 LC = RTLIB::MUL_I64;
1618 else if (VT == MVT::i128)
1619 LC = RTLIB::MUL_I128;
1620 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
1622 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1623 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true/*sign irrelevant*/), Lo, Hi);
1626 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
1627 SDValue &Lo, SDValue &Hi) {
1628 MVT VT = N->getValueType(0);
1630 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1632 LC = RTLIB::SDIV_I32;
1633 else if (VT == MVT::i64)
1634 LC = RTLIB::SDIV_I64;
1635 else if (VT == MVT::i128)
1636 LC = RTLIB::SDIV_I128;
1637 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1639 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1640 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true), Lo, Hi);
1643 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
1644 SDValue &Lo, SDValue &Hi) {
1645 MVT VT = N->getValueType(0);
1647 // If we can emit an efficient shift operation, do so now. Check to see if
1648 // the RHS is a constant.
1649 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1650 return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi);
1652 // If we can determine that the high bit of the shift is zero or one, even if
1653 // the low bits are variable, emit this shift in an optimized form.
1654 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
1657 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
1659 if (N->getOpcode() == ISD::SHL) {
1660 PartsOpc = ISD::SHL_PARTS;
1661 } else if (N->getOpcode() == ISD::SRL) {
1662 PartsOpc = ISD::SRL_PARTS;
1664 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1665 PartsOpc = ISD::SRA_PARTS;
1668 // Next check to see if the target supports this SHL_PARTS operation or if it
1669 // will custom expand it.
1670 MVT NVT = TLI.getTypeToTransformTo(VT);
1671 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
1672 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
1673 Action == TargetLowering::Custom) {
1674 // Expand the subcomponents.
1676 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1678 SDValue Ops[] = { LHSL, LHSH, N->getOperand(1) };
1679 MVT VT = LHSL.getValueType();
1680 Lo = DAG.getNode(PartsOpc, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
1681 Hi = Lo.getValue(1);
1685 // Otherwise, emit a libcall.
1686 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1688 if (N->getOpcode() == ISD::SHL) {
1689 isSigned = false; /*sign irrelevant*/
1691 LC = RTLIB::SHL_I32;
1692 else if (VT == MVT::i64)
1693 LC = RTLIB::SHL_I64;
1694 else if (VT == MVT::i128)
1695 LC = RTLIB::SHL_I128;
1696 } else if (N->getOpcode() == ISD::SRL) {
1699 LC = RTLIB::SRL_I32;
1700 else if (VT == MVT::i64)
1701 LC = RTLIB::SRL_I64;
1702 else if (VT == MVT::i128)
1703 LC = RTLIB::SRL_I128;
1705 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1708 LC = RTLIB::SRA_I32;
1709 else if (VT == MVT::i64)
1710 LC = RTLIB::SRA_I64;
1711 else if (VT == MVT::i128)
1712 LC = RTLIB::SRA_I128;
1714 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported shift!");
1716 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1717 SplitInteger(MakeLibCall(LC, VT, Ops, 2, isSigned), Lo, Hi);
1720 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
1721 SDValue &Lo, SDValue &Hi) {
1722 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1723 SDValue Op = N->getOperand(0);
1724 if (Op.getValueType().bitsLE(NVT)) {
1725 // The low part is sign extension of the input (degenerates to a copy).
1726 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, N->getOperand(0));
1727 // The high part is obtained by SRA'ing all but one of the bits of low part.
1728 unsigned LoSize = NVT.getSizeInBits();
1729 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1730 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
1732 // For example, extension of an i48 to an i64. The operand type necessarily
1733 // promotes to the result type, so will end up being expanded too.
1734 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1735 "Only know how to promote this result!");
1736 SDValue Res = GetPromotedInteger(Op);
1737 assert(Res.getValueType() == N->getValueType(0) &&
1738 "Operand over promoted?");
1739 // Split the promoted operand. This will simplify when it is expanded.
1740 SplitInteger(Res, Lo, Hi);
1741 unsigned ExcessBits =
1742 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1743 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
1744 DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1748 void DAGTypeLegalizer::
1749 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
1750 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1751 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1753 if (EVT.bitsLE(Lo.getValueType())) {
1754 // sext_inreg the low part if needed.
1755 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, Lo.getValueType(), Lo,
1758 // The high part gets the sign extension from the lo-part. This handles
1759 // things like sextinreg V:i64 from i8.
1760 Hi = DAG.getNode(ISD::SRA, Hi.getValueType(), Lo,
1761 DAG.getConstant(Hi.getValueType().getSizeInBits()-1,
1762 TLI.getShiftAmountTy()));
1764 // For example, extension of an i48 to an i64. Leave the low part alone,
1765 // sext_inreg the high part.
1766 unsigned ExcessBits =
1767 EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
1768 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
1769 DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1773 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
1774 SDValue &Lo, SDValue &Hi) {
1775 MVT VT = N->getValueType(0);
1777 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1779 LC = RTLIB::SREM_I32;
1780 else if (VT == MVT::i64)
1781 LC = RTLIB::SREM_I64;
1782 else if (VT == MVT::i128)
1783 LC = RTLIB::SREM_I128;
1784 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1786 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1787 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true), Lo, Hi);
1790 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
1791 SDValue &Lo, SDValue &Hi) {
1792 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1793 Lo = DAG.getNode(ISD::TRUNCATE, NVT, N->getOperand(0));
1794 Hi = DAG.getNode(ISD::SRL, N->getOperand(0).getValueType(), N->getOperand(0),
1795 DAG.getConstant(NVT.getSizeInBits(),
1796 TLI.getShiftAmountTy()));
1797 Hi = DAG.getNode(ISD::TRUNCATE, NVT, Hi);
1800 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
1801 SDValue &Lo, SDValue &Hi) {
1802 MVT VT = N->getValueType(0);
1804 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1806 LC = RTLIB::UDIV_I32;
1807 else if (VT == MVT::i64)
1808 LC = RTLIB::UDIV_I64;
1809 else if (VT == MVT::i128)
1810 LC = RTLIB::UDIV_I128;
1811 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
1813 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1814 SplitInteger(MakeLibCall(LC, VT, Ops, 2, false), Lo, Hi);
1817 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
1818 SDValue &Lo, SDValue &Hi) {
1819 MVT VT = N->getValueType(0);
1821 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1823 LC = RTLIB::UREM_I32;
1824 else if (VT == MVT::i64)
1825 LC = RTLIB::UREM_I64;
1826 else if (VT == MVT::i128)
1827 LC = RTLIB::UREM_I128;
1828 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
1830 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1831 SplitInteger(MakeLibCall(LC, VT, Ops, 2, false), Lo, Hi);
1834 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
1835 SDValue &Lo, SDValue &Hi) {
1836 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1837 SDValue Op = N->getOperand(0);
1838 if (Op.getValueType().bitsLE(NVT)) {
1839 // The low part is zero extension of the input (degenerates to a copy).
1840 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, N->getOperand(0));
1841 Hi = DAG.getConstant(0, NVT); // The high part is just a zero.
1843 // For example, extension of an i48 to an i64. The operand type necessarily
1844 // promotes to the result type, so will end up being expanded too.
1845 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1846 "Only know how to promote this result!");
1847 SDValue Res = GetPromotedInteger(Op);
1848 assert(Res.getValueType() == N->getValueType(0) &&
1849 "Operand over promoted?");
1850 // Split the promoted operand. This will simplify when it is expanded.
1851 SplitInteger(Res, Lo, Hi);
1852 unsigned ExcessBits =
1853 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1854 Hi = DAG.getZeroExtendInReg(Hi, MVT::getIntegerVT(ExcessBits));
1859 //===----------------------------------------------------------------------===//
1860 // Integer Operand Expansion
1861 //===----------------------------------------------------------------------===//
1863 /// ExpandIntegerOperand - This method is called when the specified operand of
1864 /// the specified node is found to need expansion. At this point, all of the
1865 /// result types of the node are known to be legal, but other operands of the
1866 /// node may need promotion or expansion as well as the specified one.
1867 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
1868 DEBUG(cerr << "Expand integer operand: "; N->dump(&DAG); cerr << "\n");
1869 SDValue Res = SDValue();
1871 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
1872 == TargetLowering::Custom)
1873 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
1875 if (Res.getNode() == 0) {
1876 switch (N->getOpcode()) {
1879 cerr << "ExpandIntegerOperand Op #" << OpNo << ": ";
1880 N->dump(&DAG); cerr << "\n";
1882 assert(0 && "Do not know how to expand this operator's operand!");
1885 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1886 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
1887 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1889 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
1890 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
1891 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
1892 case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
1893 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo);
1895 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
1896 case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
1900 // If the result is null, the sub-method took care of registering results etc.
1901 if (!Res.getNode()) return false;
1903 // If the result is N, the sub-method updated N in place. Tell the legalizer
1905 if (Res.getNode() == N)
1908 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1909 "Invalid operand expansion");
1911 ReplaceValueWith(SDValue(N, 0), Res);
1915 /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
1916 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1917 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
1919 ISD::CondCode &CCCode) {
1920 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1921 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
1922 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
1924 MVT VT = NewLHS.getValueType();
1926 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1927 if (RHSLo == RHSHi) {
1928 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
1929 if (RHSCST->isAllOnesValue()) {
1930 // Equality comparison to -1.
1931 NewLHS = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
1938 NewLHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1939 NewRHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1940 NewLHS = DAG.getNode(ISD::OR, NewLHS.getValueType(), NewLHS, NewRHS);
1941 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1945 // If this is a comparison of the sign bit, just look at the top part.
1947 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1948 if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
1949 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
1955 // FIXME: This generated code sucks.
1956 ISD::CondCode LowCC;
1958 default: assert(0 && "Unknown integer setcc!");
1960 case ISD::SETULT: LowCC = ISD::SETULT; break;
1962 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1964 case ISD::SETULE: LowCC = ISD::SETULE; break;
1966 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1969 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1970 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1971 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1973 // NOTE: on targets without efficient SELECT of bools, we can always use
1974 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
1975 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
1977 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC,
1978 false, DagCombineInfo);
1979 if (!Tmp1.getNode())
1980 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
1981 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
1982 CCCode, false, DagCombineInfo);
1983 if (!Tmp2.getNode())
1984 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
1985 DAG.getCondCode(CCCode));
1987 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
1988 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
1989 if ((Tmp1C && Tmp1C->isNullValue()) ||
1990 (Tmp2C && Tmp2C->isNullValue() &&
1991 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
1992 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
1993 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
1994 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
1995 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
1996 // low part is known false, returns high part.
1997 // For LE / GE, if high part is known false, ignore the low part.
1998 // For LT / GT, if high part is known true, ignore the low part.
2004 NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
2005 ISD::SETEQ, false, DagCombineInfo);
2006 if (!NewLHS.getNode())
2007 NewLHS = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
2009 NewLHS = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2010 NewLHS, Tmp1, Tmp2);
2014 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
2015 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
2016 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
2017 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2019 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2020 // against zero to select between true and false values.
2021 if (NewRHS.getNode() == 0) {
2022 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2023 CCCode = ISD::SETNE;
2026 // Update N to have the operands specified.
2027 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
2028 DAG.getCondCode(CCCode), NewLHS, NewRHS,
2032 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
2033 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2034 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
2035 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2037 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2038 // against zero to select between true and false values.
2039 if (NewRHS.getNode() == 0) {
2040 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2041 CCCode = ISD::SETNE;
2044 // Update N to have the operands specified.
2045 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2046 N->getOperand(2), N->getOperand(3),
2047 DAG.getCondCode(CCCode));
2050 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
2051 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2052 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
2053 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2055 // If ExpandSetCCOperands returned a scalar, use it.
2056 if (NewRHS.getNode() == 0) {
2057 assert(NewLHS.getValueType() == N->getValueType(0) &&
2058 "Unexpected setcc expansion!");
2062 // Otherwise, update N to have the operands specified.
2063 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2064 DAG.getCondCode(CCCode));
2067 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
2068 SDValue Op = N->getOperand(0);
2069 MVT DstVT = N->getValueType(0);
2070 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
2071 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2072 "Don't know how to expand this SINT_TO_FP!");
2073 return MakeLibCall(LC, DstVT, &Op, 1, true);
2076 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
2077 if (ISD::isNormalStore(N))
2078 return ExpandOp_NormalStore(N, OpNo);
2080 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2081 assert(OpNo == 1 && "Can only expand the stored value so far");
2083 MVT VT = N->getOperand(1).getValueType();
2084 MVT NVT = TLI.getTypeToTransformTo(VT);
2085 SDValue Ch = N->getChain();
2086 SDValue Ptr = N->getBasePtr();
2087 int SVOffset = N->getSrcValueOffset();
2088 unsigned Alignment = N->getAlignment();
2089 bool isVolatile = N->isVolatile();
2092 assert(NVT.isByteSized() && "Expanded type not byte sized!");
2094 if (N->getMemoryVT().bitsLE(NVT)) {
2095 GetExpandedInteger(N->getValue(), Lo, Hi);
2096 return DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
2097 N->getMemoryVT(), isVolatile, Alignment);
2098 } else if (TLI.isLittleEndian()) {
2099 // Little-endian - low bits are at low addresses.
2100 GetExpandedInteger(N->getValue(), Lo, Hi);
2102 Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
2103 isVolatile, Alignment);
2105 unsigned ExcessBits =
2106 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
2107 MVT NEVT = MVT::getIntegerVT(ExcessBits);
2109 // Increment the pointer to the other half.
2110 unsigned IncrementSize = NVT.getSizeInBits()/8;
2111 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2112 DAG.getIntPtrConstant(IncrementSize));
2113 Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
2114 SVOffset+IncrementSize, NEVT,
2115 isVolatile, MinAlign(Alignment, IncrementSize));
2116 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2118 // Big-endian - high bits are at low addresses. Favor aligned stores at
2119 // the cost of some bit-fiddling.
2120 GetExpandedInteger(N->getValue(), Lo, Hi);
2122 MVT EVT = N->getMemoryVT();
2123 unsigned EBytes = EVT.getStoreSizeInBits()/8;
2124 unsigned IncrementSize = NVT.getSizeInBits()/8;
2125 unsigned ExcessBits = (EBytes - IncrementSize)*8;
2126 MVT HiVT = MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits);
2128 if (ExcessBits < NVT.getSizeInBits()) {
2129 // Transfer high bits from the top of Lo to the bottom of Hi.
2130 Hi = DAG.getNode(ISD::SHL, NVT, Hi,
2131 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
2132 TLI.getShiftAmountTy()));
2133 Hi = DAG.getNode(ISD::OR, NVT, Hi,
2134 DAG.getNode(ISD::SRL, NVT, Lo,
2135 DAG.getConstant(ExcessBits,
2136 TLI.getShiftAmountTy())));
2139 // Store both the high bits and maybe some of the low bits.
2140 Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
2141 SVOffset, HiVT, isVolatile, Alignment);
2143 // Increment the pointer to the other half.
2144 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2145 DAG.getIntPtrConstant(IncrementSize));
2146 // Store the lowest ExcessBits bits in the second half.
2147 Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(),
2148 SVOffset+IncrementSize,
2149 MVT::getIntegerVT(ExcessBits),
2150 isVolatile, MinAlign(Alignment, IncrementSize));
2151 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2155 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
2157 GetExpandedInteger(N->getOperand(0), InL, InH);
2158 // Just truncate the low part of the source.
2159 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), InL);
2162 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
2163 SDValue Op = N->getOperand(0);
2164 MVT SrcVT = Op.getValueType();
2165 MVT DstVT = N->getValueType(0);
2167 if (TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
2168 // Do a signed conversion then adjust the result.
2169 SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, DstVT, Op);
2170 SignedConv = TLI.LowerOperation(SignedConv, DAG);
2172 // The result of the signed conversion needs adjusting if the 'sign bit' of
2173 // the incoming integer was set. To handle this, we dynamically test to see
2174 // if it is set, and, if so, add a fudge factor.
2176 const uint64_t F32TwoE32 = 0x4F800000ULL;
2177 const uint64_t F32TwoE64 = 0x5F800000ULL;
2178 const uint64_t F32TwoE128 = 0x7F800000ULL;
2181 if (SrcVT == MVT::i32)
2182 FF = APInt(32, F32TwoE32);
2183 else if (SrcVT == MVT::i64)
2184 FF = APInt(32, F32TwoE64);
2185 else if (SrcVT == MVT::i128)
2186 FF = APInt(32, F32TwoE128);
2188 assert(false && "Unsupported UINT_TO_FP!");
2190 // Check whether the sign bit is set.
2192 GetExpandedInteger(Op, Lo, Hi);
2193 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
2194 DAG.getConstant(0, Hi.getValueType()),
2197 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
2198 SDValue FudgePtr = DAG.getConstantPool(ConstantInt::get(FF.zext(64)),
2199 TLI.getPointerTy());
2201 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
2202 SDValue Zero = DAG.getIntPtrConstant(0);
2203 SDValue Four = DAG.getIntPtrConstant(4);
2204 if (TLI.isBigEndian()) std::swap(Zero, Four);
2205 SDValue Offset = DAG.getNode(ISD::SELECT, Zero.getValueType(), SignSet,
2207 unsigned Alignment =
2208 1 << cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
2209 FudgePtr = DAG.getNode(ISD::ADD, TLI.getPointerTy(), FudgePtr, Offset);
2210 Alignment = std::min(Alignment, 4u);
2212 // Load the value out, extending it from f32 to the destination float type.
2213 // FIXME: Avoid the extend by constructing the right constant pool?
2214 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, DstVT, DAG.getEntryNode(),
2215 FudgePtr, NULL, 0, MVT::f32,
2217 return DAG.getNode(ISD::FADD, DstVT, SignedConv, Fudge);
2220 // Otherwise, use a libcall.
2221 RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
2222 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2223 "Don't know how to expand this UINT_TO_FP!");
2224 return MakeLibCall(LC, DstVT, &Op, 1, true);