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;
95 case ISD::SSUBO: Result = PromoteIntRes_SADDSUBO(N, ResNo); break;
97 case ISD::USUBO: Result = PromoteIntRes_UADDSUBO(N, ResNo); break;
99 case ISD::UMULO: Result = PromoteIntRes_XMULO(N, ResNo); break;
101 case ISD::ATOMIC_LOAD_ADD:
102 case ISD::ATOMIC_LOAD_SUB:
103 case ISD::ATOMIC_LOAD_AND:
104 case ISD::ATOMIC_LOAD_OR:
105 case ISD::ATOMIC_LOAD_XOR:
106 case ISD::ATOMIC_LOAD_NAND:
107 case ISD::ATOMIC_LOAD_MIN:
108 case ISD::ATOMIC_LOAD_MAX:
109 case ISD::ATOMIC_LOAD_UMIN:
110 case ISD::ATOMIC_LOAD_UMAX:
111 case ISD::ATOMIC_SWAP:
112 Result = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
114 case ISD::ATOMIC_CMP_SWAP:
115 Result = PromoteIntRes_Atomic2(cast<AtomicSDNode>(N)); break;
118 // If Result is null, the sub-method took care of registering the result.
119 if (Result.getNode())
120 SetPromotedInteger(SDValue(N, ResNo), Result);
123 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
124 // Sign-extend the new bits, and continue the assertion.
125 SDValue Op = SExtPromotedInteger(N->getOperand(0));
126 return DAG.getNode(ISD::AssertSext, Op.getValueType(), Op, N->getOperand(1));
129 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
130 // Zero the new bits, and continue the assertion.
131 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
132 return DAG.getNode(ISD::AssertZext, Op.getValueType(), Op, N->getOperand(1));
135 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
136 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
137 SDValue Res = DAG.getAtomic(N->getOpcode(), N->getMemoryVT(),
138 N->getChain(), N->getBasePtr(),
139 Op2, N->getSrcValue(), N->getAlignment());
140 // Legalized the chain result - switch anything that used the old chain to
142 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
146 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic2(AtomicSDNode *N) {
147 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
148 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
149 SDValue Res = DAG.getAtomic(N->getOpcode(), N->getMemoryVT(),
150 N->getChain(), N->getBasePtr(),
151 Op2, Op3, N->getSrcValue(), N->getAlignment());
152 // Legalized the chain result - switch anything that used the old chain to
154 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
158 SDValue DAGTypeLegalizer::PromoteIntRes_BIT_CONVERT(SDNode *N) {
159 SDValue InOp = N->getOperand(0);
160 MVT InVT = InOp.getValueType();
161 MVT NInVT = TLI.getTypeToTransformTo(InVT);
162 MVT OutVT = N->getValueType(0);
163 MVT NOutVT = TLI.getTypeToTransformTo(OutVT);
165 switch (getTypeAction(InVT)) {
167 assert(false && "Unknown type action!");
172 if (NOutVT.bitsEq(NInVT))
173 // The input promotes to the same size. Convert the promoted value.
174 return DAG.getNode(ISD::BIT_CONVERT, NOutVT, GetPromotedInteger(InOp));
177 // Promote the integer operand by hand.
178 return DAG.getNode(ISD::ANY_EXTEND, NOutVT, GetSoftenedFloat(InOp));
182 case ScalarizeVector:
183 // Convert the element to an integer and promote it by hand.
184 return DAG.getNode(ISD::ANY_EXTEND, NOutVT,
185 BitConvertToInteger(GetScalarizedVector(InOp)));
187 // For example, i32 = BIT_CONVERT v2i16 on alpha. Convert the split
188 // pieces of the input into integers and reassemble in the final type.
190 GetSplitVector(N->getOperand(0), Lo, Hi);
191 Lo = BitConvertToInteger(Lo);
192 Hi = BitConvertToInteger(Hi);
194 if (TLI.isBigEndian())
197 InOp = DAG.getNode(ISD::ANY_EXTEND,
198 MVT::getIntegerVT(NOutVT.getSizeInBits()),
199 JoinIntegers(Lo, Hi));
200 return DAG.getNode(ISD::BIT_CONVERT, NOutVT, InOp);
203 if (OutVT.bitsEq(NInVT))
204 // The input is widened to the same size. Convert to the widened value.
205 return DAG.getNode(ISD::BIT_CONVERT, OutVT, GetWidenedVector(InOp));
208 // Otherwise, lower the bit-convert to a store/load from the stack.
209 // Create the stack frame object. Make sure it is aligned for both
210 // the source and destination types.
211 SDValue FIPtr = DAG.CreateStackTemporary(InVT, OutVT);
213 // Emit a store to the stack slot.
214 SDValue Store = DAG.getStore(DAG.getEntryNode(), InOp, FIPtr, NULL, 0);
216 // Result is an extending load from the stack slot.
217 return DAG.getExtLoad(ISD::EXTLOAD, NOutVT, Store, FIPtr, NULL, 0, OutVT);
220 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
221 SDValue Op = GetPromotedInteger(N->getOperand(0));
222 MVT OVT = N->getValueType(0);
223 MVT NVT = Op.getValueType();
225 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
226 return DAG.getNode(ISD::SRL, NVT, DAG.getNode(ISD::BSWAP, NVT, Op),
227 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
230 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
231 // The pair element type may be legal, or may not promote to the same type as
232 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
233 return DAG.getNode(ISD::ANY_EXTEND,
234 TLI.getTypeToTransformTo(N->getValueType(0)),
235 JoinIntegers(N->getOperand(0), N->getOperand(1)));
238 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
239 MVT VT = N->getValueType(0);
240 // Zero extend things like i1, sign extend everything else. It shouldn't
241 // matter in theory which one we pick, but this tends to give better code?
242 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
243 SDValue Result = DAG.getNode(Opc, TLI.getTypeToTransformTo(VT),
245 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
249 SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) {
250 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
251 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
252 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
253 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
254 "can only promote integers");
255 MVT OutVT = TLI.getTypeToTransformTo(N->getValueType(0));
256 return DAG.getConvertRndSat(OutVT, N->getOperand(0),
257 N->getOperand(1), N->getOperand(2),
258 N->getOperand(3), N->getOperand(4), CvtCode);
261 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
262 // Zero extend to the promoted type and do the count there.
263 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
264 MVT OVT = N->getValueType(0);
265 MVT NVT = Op.getValueType();
266 Op = DAG.getNode(ISD::CTLZ, NVT, Op);
267 // Subtract off the extra leading bits in the bigger type.
268 return DAG.getNode(ISD::SUB, NVT, Op,
269 DAG.getConstant(NVT.getSizeInBits() -
270 OVT.getSizeInBits(), NVT));
273 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
274 // Zero extend to the promoted type and do the count there.
275 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
276 return DAG.getNode(ISD::CTPOP, Op.getValueType(), Op);
279 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
280 SDValue Op = GetPromotedInteger(N->getOperand(0));
281 MVT OVT = N->getValueType(0);
282 MVT NVT = Op.getValueType();
283 // The count is the same in the promoted type except if the original
284 // value was zero. This can be handled by setting the bit just off
285 // the top of the original type.
286 APInt TopBit(NVT.getSizeInBits(), 0);
287 TopBit.set(OVT.getSizeInBits());
288 Op = DAG.getNode(ISD::OR, NVT, Op, DAG.getConstant(TopBit, NVT));
289 return DAG.getNode(ISD::CTTZ, NVT, Op);
292 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
293 MVT OldVT = N->getValueType(0);
294 SDValue OldVec = N->getOperand(0);
295 unsigned OldElts = OldVec.getValueType().getVectorNumElements();
298 assert(!isTypeLegal(OldVec.getValueType()) &&
299 "Legal one-element vector of a type needing promotion!");
300 // It is tempting to follow GetScalarizedVector by a call to
301 // GetPromotedInteger, but this would be wrong because the
302 // scalarized value may not yet have been processed.
303 return DAG.getNode(ISD::ANY_EXTEND, TLI.getTypeToTransformTo(OldVT),
304 GetScalarizedVector(OldVec));
307 // Convert to a vector half as long with an element type of twice the width,
308 // for example <4 x i16> -> <2 x i32>.
309 assert(!(OldElts & 1) && "Odd length vectors not supported!");
310 MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
311 assert(OldVT.isSimple() && NewVT.isSimple());
313 SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT,
314 MVT::getVectorVT(NewVT, OldElts / 2),
317 // Extract the element at OldIdx / 2 from the new vector.
318 SDValue OldIdx = N->getOperand(1);
319 SDValue NewIdx = DAG.getNode(ISD::SRL, OldIdx.getValueType(), OldIdx,
320 DAG.getConstant(1, TLI.getShiftAmountTy()));
321 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, NewIdx);
323 // Select the appropriate half of the element: Lo if OldIdx was even,
326 SDValue Hi = DAG.getNode(ISD::SRL, NewVT, Elt,
327 DAG.getConstant(OldVT.getSizeInBits(),
328 TLI.getShiftAmountTy()));
329 if (TLI.isBigEndian())
332 // Extend to the promoted type.
333 SDValue Odd = DAG.getNode(ISD::TRUNCATE, MVT::i1, OldIdx);
334 SDValue Res = DAG.getNode(ISD::SELECT, NewVT, Odd, Hi, Lo);
335 return DAG.getNode(ISD::ANY_EXTEND, TLI.getTypeToTransformTo(OldVT), Res);
338 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
339 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
340 unsigned NewOpc = N->getOpcode();
342 // If we're promoting a UINT to a larger size, check to see if the new node
343 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
344 // we can use that instead. This allows us to generate better code for
345 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
346 // legal, such as PowerPC.
347 if (N->getOpcode() == ISD::FP_TO_UINT &&
348 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
349 TLI.isOperationLegal(ISD::FP_TO_SINT, NVT))
350 NewOpc = ISD::FP_TO_SINT;
352 SDValue Res = DAG.getNode(NewOpc, NVT, N->getOperand(0));
354 // Assert that the converted value fits in the original type. If it doesn't
355 // (eg: because the value being converted is too big), then the result of the
356 // original operation was undefined anyway, so the assert is still correct.
357 return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ?
358 ISD::AssertZext : ISD::AssertSext,
359 NVT, Res, DAG.getValueType(N->getValueType(0)));
362 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
363 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
365 if (getTypeAction(N->getOperand(0).getValueType()) == PromoteInteger) {
366 SDValue Res = GetPromotedInteger(N->getOperand(0));
367 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
369 // If the result and operand types are the same after promotion, simplify
370 // to an in-register extension.
371 if (NVT == Res.getValueType()) {
372 // The high bits are not guaranteed to be anything. Insert an extend.
373 if (N->getOpcode() == ISD::SIGN_EXTEND)
374 return DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res,
375 DAG.getValueType(N->getOperand(0).getValueType()));
376 if (N->getOpcode() == ISD::ZERO_EXTEND)
377 return DAG.getZeroExtendInReg(Res, N->getOperand(0).getValueType());
378 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
383 // Otherwise, just extend the original operand all the way to the larger type.
384 return DAG.getNode(N->getOpcode(), NVT, N->getOperand(0));
387 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
388 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
389 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
390 ISD::LoadExtType ExtType =
391 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
392 SDValue Res = DAG.getExtLoad(ExtType, NVT, N->getChain(), N->getBasePtr(),
393 N->getSrcValue(), N->getSrcValueOffset(),
394 N->getMemoryVT(), N->isVolatile(),
397 // Legalized the chain result - switch anything that used the old chain to
399 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
403 /// Promote the overflow flag of an overflowing arithmetic node.
404 SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
405 // Simply change the return type of the boolean result.
406 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(1));
407 MVT ValueVTs[] = { N->getValueType(0), NVT };
408 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
409 SDValue Res = DAG.getNode(N->getOpcode(), DAG.getVTList(ValueVTs, 2), Ops, 2);
411 // Modified the sum result - switch anything that used the old sum to use
413 ReplaceValueWith(SDValue(N, 0), Res);
415 return SDValue(Res.getNode(), 1);
418 SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
420 return PromoteIntRes_Overflow(N);
422 // The operation overflowed iff the result in the larger type is not the
423 // sign extension of its truncation to the original type.
424 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
425 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
426 MVT OVT = N->getOperand(0).getValueType();
427 MVT NVT = LHS.getValueType();
429 // Do the arithmetic in the larger type.
430 unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
431 SDValue Res = DAG.getNode(Opcode, NVT, LHS, RHS);
433 // Calculate the overflow flag: sign extend the arithmetic result from
434 // the original type.
435 SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res,
436 DAG.getValueType(OVT));
437 // Overflowed if and only if this is not equal to Res.
438 Ofl = DAG.getSetCC(N->getValueType(1), Ofl, Res, ISD::SETNE);
440 // Use the calculated overflow everywhere.
441 ReplaceValueWith(SDValue(N, 1), Ofl);
446 SDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) {
447 // Sign extend the input.
448 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
449 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
450 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
453 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
454 SDValue LHS = GetPromotedInteger(N->getOperand(1));
455 SDValue RHS = GetPromotedInteger(N->getOperand(2));
456 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
459 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
460 SDValue LHS = GetPromotedInteger(N->getOperand(2));
461 SDValue RHS = GetPromotedInteger(N->getOperand(3));
462 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
463 N->getOperand(1), LHS, RHS, N->getOperand(4));
466 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
467 MVT SVT = TLI.getSetCCResultType(N->getOperand(0).getValueType());
468 assert(isTypeLegal(SVT) && "Illegal SetCC type!");
470 // Get the SETCC result using the canonical SETCC type.
471 SDValue SetCC = DAG.getNode(ISD::SETCC, SVT, N->getOperand(0),
472 N->getOperand(1), N->getOperand(2));
474 // Convert to the expected type.
475 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
476 assert(NVT.bitsLE(SVT) && "Integer type overpromoted?");
477 return DAG.getNode(ISD::TRUNCATE, NVT, SetCC);
480 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
481 return DAG.getNode(ISD::SHL, TLI.getTypeToTransformTo(N->getValueType(0)),
482 GetPromotedInteger(N->getOperand(0)), N->getOperand(1));
485 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
486 SDValue Op = GetPromotedInteger(N->getOperand(0));
487 return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(), Op,
491 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
492 // The input may have strange things in the top bits of the registers, but
493 // these operations don't care. They may have weird bits going out, but
494 // that too is okay if they are integer operations.
495 SDValue LHS = GetPromotedInteger(N->getOperand(0));
496 SDValue RHS = GetPromotedInteger(N->getOperand(1));
497 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
500 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
501 // The input value must be properly sign extended.
502 SDValue Res = SExtPromotedInteger(N->getOperand(0));
503 return DAG.getNode(ISD::SRA, Res.getValueType(), Res, N->getOperand(1));
506 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
507 // The input value must be properly zero extended.
508 MVT VT = N->getValueType(0);
509 MVT NVT = TLI.getTypeToTransformTo(VT);
510 SDValue Res = ZExtPromotedInteger(N->getOperand(0));
511 return DAG.getNode(ISD::SRL, NVT, Res, N->getOperand(1));
514 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
515 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
518 switch (getTypeAction(N->getOperand(0).getValueType())) {
519 default: assert(0 && "Unknown type action!");
522 Res = N->getOperand(0);
525 Res = GetPromotedInteger(N->getOperand(0));
529 // Truncate to NVT instead of VT
530 return DAG.getNode(ISD::TRUNCATE, NVT, Res);
533 SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
535 return PromoteIntRes_Overflow(N);
537 // The operation overflowed iff the result in the larger type is not the
538 // zero extension of its truncation to the original type.
539 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
540 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
541 MVT OVT = N->getOperand(0).getValueType();
542 MVT NVT = LHS.getValueType();
544 // Do the arithmetic in the larger type.
545 unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
546 SDValue Res = DAG.getNode(Opcode, NVT, LHS, RHS);
548 // Calculate the overflow flag: zero extend the arithmetic result from
549 // the original type.
550 SDValue Ofl = DAG.getZeroExtendInReg(Res, OVT);
551 // Overflowed if and only if this is not equal to Res.
552 Ofl = DAG.getSetCC(N->getValueType(1), Ofl, Res, ISD::SETNE);
554 // Use the calculated overflow everywhere.
555 ReplaceValueWith(SDValue(N, 1), Ofl);
560 SDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) {
561 // Zero extend the input.
562 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
563 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
564 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
567 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
568 return DAG.getNode(ISD::UNDEF, TLI.getTypeToTransformTo(N->getValueType(0)));
571 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
572 SDValue Chain = N->getOperand(0); // Get the chain.
573 SDValue Ptr = N->getOperand(1); // Get the pointer.
574 MVT VT = N->getValueType(0);
576 MVT RegVT = TLI.getRegisterType(VT);
577 unsigned NumRegs = TLI.getNumRegisters(VT);
578 // The argument is passed as NumRegs registers of type RegVT.
580 SmallVector<SDValue, 8> Parts(NumRegs);
581 for (unsigned i = 0; i < NumRegs; ++i) {
582 Parts[i] = DAG.getVAArg(RegVT, Chain, Ptr, N->getOperand(2));
583 Chain = Parts[i].getValue(1);
586 // Handle endianness of the load.
587 if (TLI.isBigEndian())
588 std::reverse(Parts.begin(), Parts.end());
590 // Assemble the parts in the promoted type.
591 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
592 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, NVT, Parts[0]);
593 for (unsigned i = 1; i < NumRegs; ++i) {
594 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, NVT, Parts[i]);
595 // Shift it to the right position and "or" it in.
596 Part = DAG.getNode(ISD::SHL, NVT, Part,
597 DAG.getConstant(i * RegVT.getSizeInBits(),
598 TLI.getShiftAmountTy()));
599 Res = DAG.getNode(ISD::OR, NVT, Res, Part);
602 // Modified the chain result - switch anything that used the old chain to
604 ReplaceValueWith(SDValue(N, 1), Chain);
609 SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
610 assert(ResNo == 1 && "Only boolean result promotion currently supported!");
611 return PromoteIntRes_Overflow(N);
614 //===----------------------------------------------------------------------===//
615 // Integer Operand Promotion
616 //===----------------------------------------------------------------------===//
618 /// PromoteIntegerOperand - This method is called when the specified operand of
619 /// the specified node is found to need promotion. At this point, all of the
620 /// result types of the node are known to be legal, but other operands of the
621 /// node may need promotion or expansion as well as the specified one.
622 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
623 DEBUG(cerr << "Promote integer operand: "; N->dump(&DAG); cerr << "\n");
624 SDValue Res = SDValue();
626 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
627 == TargetLowering::Custom)
628 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
630 if (Res.getNode() == 0) {
631 switch (N->getOpcode()) {
634 cerr << "PromoteIntegerOperand Op #" << OpNo << ": ";
635 N->dump(&DAG); cerr << "\n";
637 assert(0 && "Do not know how to promote this operator's operand!");
640 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
641 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
642 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
643 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
644 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
645 case ISD::CONVERT_RNDSAT:
646 Res = PromoteIntOp_CONVERT_RNDSAT(N); break;
647 case ISD::INSERT_VECTOR_ELT:
648 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
649 case ISD::MEMBARRIER: Res = PromoteIntOp_MEMBARRIER(N); break;
650 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
651 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
652 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
653 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
654 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
655 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
657 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
658 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
659 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
663 // If the result is null, the sub-method took care of registering results etc.
664 if (!Res.getNode()) return false;
666 // If the result is N, the sub-method updated N in place. Tell the legalizer
668 if (Res.getNode() == N)
671 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
672 "Invalid operand expansion");
674 ReplaceValueWith(SDValue(N, 0), Res);
678 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
679 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
680 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
681 ISD::CondCode CCCode) {
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 = ZExtPromotedInteger(NewLHS);
697 NewRHS = ZExtPromotedInteger(NewRHS);
703 NewLHS = SExtPromotedInteger(NewLHS);
704 NewRHS = SExtPromotedInteger(NewRHS);
709 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
710 SDValue Op = GetPromotedInteger(N->getOperand(0));
711 return DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
714 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
715 assert(OpNo == 2 && "Don't know how to promote this operand!");
717 SDValue LHS = N->getOperand(2);
718 SDValue RHS = N->getOperand(3);
719 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
721 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
723 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
724 N->getOperand(1), LHS, RHS, N->getOperand(4));
727 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
728 assert(OpNo == 1 && "only know how to promote condition");
730 // Promote all the way up to the canonical SetCC type.
731 MVT SVT = TLI.getSetCCResultType(MVT::Other);
732 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), SVT);
734 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
735 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Cond,
739 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
740 // Since the result type is legal, the operands must promote to it.
741 MVT OVT = N->getOperand(0).getValueType();
742 SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
743 SDValue Hi = GetPromotedInteger(N->getOperand(1));
744 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
746 Hi = DAG.getNode(ISD::SHL, N->getValueType(0), Hi,
747 DAG.getConstant(OVT.getSizeInBits(),
748 TLI.getShiftAmountTy()));
749 return DAG.getNode(ISD::OR, N->getValueType(0), Lo, Hi);
752 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
753 // The vector type is legal but the element type is not. This implies
754 // that the vector is a power-of-two in length and that the element
755 // type does not have a strange size (eg: it is not i1).
756 MVT VecVT = N->getValueType(0);
757 unsigned NumElts = VecVT.getVectorNumElements();
758 assert(!(NumElts & 1) && "Legal vector of one illegal element?");
760 // Build a vector of half the length out of elements of twice the bitwidth.
761 // For example <4 x i16> -> <2 x i32>.
762 MVT OldVT = N->getOperand(0).getValueType();
763 MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
764 assert(OldVT.isSimple() && NewVT.isSimple());
766 std::vector<SDValue> NewElts;
767 NewElts.reserve(NumElts/2);
769 for (unsigned i = 0; i < NumElts; i += 2) {
770 // Combine two successive elements into one promoted element.
771 SDValue Lo = N->getOperand(i);
772 SDValue Hi = N->getOperand(i+1);
773 if (TLI.isBigEndian())
775 NewElts.push_back(JoinIntegers(Lo, Hi));
778 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR,
779 MVT::getVectorVT(NewVT, NewElts.size()),
780 &NewElts[0], NewElts.size());
782 // Convert the new vector to the old vector type.
783 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
786 SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) {
787 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
788 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
789 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
790 CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) &&
791 "can only promote integer arguments");
792 SDValue InOp = GetPromotedInteger(N->getOperand(0));
793 return DAG.getConvertRndSat(N->getValueType(0), InOp,
794 N->getOperand(1), N->getOperand(2),
795 N->getOperand(3), N->getOperand(4), CvtCode);
798 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
801 // Promote the inserted value. This is valid because the type does not
802 // have to match the vector element type.
804 // Check that any extra bits introduced will be truncated away.
805 assert(N->getOperand(1).getValueType().getSizeInBits() >=
806 N->getValueType(0).getVectorElementType().getSizeInBits() &&
807 "Type of inserted value narrower than vector element type!");
808 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
809 GetPromotedInteger(N->getOperand(1)),
813 assert(OpNo == 2 && "Different operand and result vector types?");
815 // Promote the index.
816 SDValue Idx = ZExtPromotedInteger(N->getOperand(2));
817 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
818 N->getOperand(1), Idx);
821 SDValue DAGTypeLegalizer::PromoteIntOp_MEMBARRIER(SDNode *N) {
823 NewOps[0] = N->getOperand(0);
824 for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
825 SDValue Flag = GetPromotedInteger(N->getOperand(i));
826 NewOps[i] = DAG.getZeroExtendInReg(Flag, MVT::i1);
828 return DAG.UpdateNodeOperands(SDValue (N, 0), NewOps,
829 array_lengthof(NewOps));
832 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
833 assert(OpNo == 0 && "Only know how to promote condition");
835 // Promote all the way up to the canonical SetCC type.
836 MVT SVT = TLI.getSetCCResultType(N->getOperand(1).getValueType());
837 SDValue Cond = PromoteTargetBoolean(N->getOperand(0), SVT);
839 return DAG.UpdateNodeOperands(SDValue(N, 0), Cond,
840 N->getOperand(1), N->getOperand(2));
843 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
844 assert(OpNo == 0 && "Don't know how to promote this operand!");
846 SDValue LHS = N->getOperand(0);
847 SDValue RHS = N->getOperand(1);
848 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
850 // The CC (#4) and the possible return values (#2 and #3) have legal types.
851 return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2),
852 N->getOperand(3), N->getOperand(4));
855 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
856 assert(OpNo == 0 && "Don't know how to promote this operand!");
858 SDValue LHS = N->getOperand(0);
859 SDValue RHS = N->getOperand(1);
860 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
862 // The CC (#2) is always legal.
863 return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2));
866 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
867 SDValue Op = GetPromotedInteger(N->getOperand(0));
868 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
869 return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(),
870 Op, DAG.getValueType(N->getOperand(0).getValueType()));
873 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
874 return DAG.UpdateNodeOperands(SDValue(N, 0),
875 SExtPromotedInteger(N->getOperand(0)));
878 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
879 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
880 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
881 int SVOffset = N->getSrcValueOffset();
882 unsigned Alignment = N->getAlignment();
883 bool isVolatile = N->isVolatile();
885 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
887 // Truncate the value and store the result.
888 return DAG.getTruncStore(Ch, Val, Ptr, N->getSrcValue(),
889 SVOffset, N->getMemoryVT(),
890 isVolatile, Alignment);
893 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
894 SDValue Op = GetPromotedInteger(N->getOperand(0));
895 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), Op);
898 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
899 return DAG.UpdateNodeOperands(SDValue(N, 0),
900 ZExtPromotedInteger(N->getOperand(0)));
903 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
904 SDValue Op = GetPromotedInteger(N->getOperand(0));
905 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
906 return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
910 //===----------------------------------------------------------------------===//
911 // Integer Result Expansion
912 //===----------------------------------------------------------------------===//
914 /// ExpandIntegerResult - This method is called when the specified result of the
915 /// specified node is found to need expansion. At this point, the node may also
916 /// have invalid operands or may have other results that need promotion, we just
917 /// know that (at least) one result needs expansion.
918 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
919 DEBUG(cerr << "Expand integer result: "; N->dump(&DAG); cerr << "\n");
923 // See if the target wants to custom expand this node.
924 if (CustomLowerResults(N, ResNo))
927 switch (N->getOpcode()) {
930 cerr << "ExpandIntegerResult #" << ResNo << ": ";
931 N->dump(&DAG); cerr << "\n";
933 assert(0 && "Do not know how to expand the result of this operator!");
936 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
937 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
938 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
939 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
941 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
942 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
943 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
944 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
945 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
947 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
948 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
949 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
950 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
951 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
952 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
953 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
954 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
955 case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
956 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
957 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
958 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
959 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
960 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
961 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
962 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
963 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
964 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
965 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
966 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
970 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
973 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
976 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
979 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
983 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
986 // If Lo/Hi is null, the sub-method took care of registering results etc.
988 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
991 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
992 /// and the shift amount is a constant 'Amt'. Expand the operation.
993 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
994 SDValue &Lo, SDValue &Hi) {
995 // Expand the incoming operand to be shifted, so that we have its parts
997 GetExpandedInteger(N->getOperand(0), InL, InH);
999 MVT NVT = InL.getValueType();
1000 unsigned VTBits = N->getValueType(0).getSizeInBits();
1001 unsigned NVTBits = NVT.getSizeInBits();
1002 MVT ShTy = N->getOperand(1).getValueType();
1004 if (N->getOpcode() == ISD::SHL) {
1006 Lo = Hi = DAG.getConstant(0, NVT);
1007 } else if (Amt > NVTBits) {
1008 Lo = DAG.getConstant(0, NVT);
1009 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
1010 } else if (Amt == NVTBits) {
1011 Lo = DAG.getConstant(0, NVT);
1013 } else if (Amt == 1 &&
1014 TLI.isOperationLegal(ISD::ADDC, TLI.getTypeToExpandTo(NVT))) {
1015 // Emit this X << 1 as X+X.
1016 SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1017 SDValue LoOps[2] = { InL, InL };
1018 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1019 SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
1020 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1022 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt, ShTy));
1023 Hi = DAG.getNode(ISD::OR, NVT,
1024 DAG.getNode(ISD::SHL, NVT, InH,
1025 DAG.getConstant(Amt, ShTy)),
1026 DAG.getNode(ISD::SRL, NVT, InL,
1027 DAG.getConstant(NVTBits-Amt, ShTy)));
1032 if (N->getOpcode() == ISD::SRL) {
1034 Lo = DAG.getConstant(0, NVT);
1035 Hi = DAG.getConstant(0, NVT);
1036 } else if (Amt > NVTBits) {
1037 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
1038 Hi = DAG.getConstant(0, NVT);
1039 } else if (Amt == NVTBits) {
1041 Hi = DAG.getConstant(0, NVT);
1043 Lo = DAG.getNode(ISD::OR, NVT,
1044 DAG.getNode(ISD::SRL, NVT, InL,
1045 DAG.getConstant(Amt, ShTy)),
1046 DAG.getNode(ISD::SHL, NVT, InH,
1047 DAG.getConstant(NVTBits-Amt, ShTy)));
1048 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt, ShTy));
1053 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1055 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1056 DAG.getConstant(NVTBits-1, ShTy));
1057 } else if (Amt > NVTBits) {
1058 Lo = DAG.getNode(ISD::SRA, NVT, InH,
1059 DAG.getConstant(Amt-NVTBits, ShTy));
1060 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1061 DAG.getConstant(NVTBits-1, ShTy));
1062 } else if (Amt == NVTBits) {
1064 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1065 DAG.getConstant(NVTBits-1, ShTy));
1067 Lo = DAG.getNode(ISD::OR, NVT,
1068 DAG.getNode(ISD::SRL, NVT, InL,
1069 DAG.getConstant(Amt, ShTy)),
1070 DAG.getNode(ISD::SHL, NVT, InH,
1071 DAG.getConstant(NVTBits-Amt, ShTy)));
1072 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Amt, ShTy));
1076 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1077 /// this shift based on knowledge of the high bit of the shift amount. If we
1078 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1080 bool DAGTypeLegalizer::
1081 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1082 SDValue Amt = N->getOperand(1);
1083 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1084 MVT ShTy = Amt.getValueType();
1085 unsigned ShBits = ShTy.getSizeInBits();
1086 unsigned NVTBits = NVT.getSizeInBits();
1087 assert(isPowerOf2_32(NVTBits) &&
1088 "Expanded integer type size not a power of two!");
1090 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
1091 APInt KnownZero, KnownOne;
1092 DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
1094 // If we don't know anything about the high bits, exit.
1095 if (((KnownZero|KnownOne) & HighBitMask) == 0)
1098 // Get the incoming operand to be shifted.
1100 GetExpandedInteger(N->getOperand(0), InL, InH);
1102 // If we know that any of the high bits of the shift amount are one, then we
1103 // can do this as a couple of simple shifts.
1104 if (KnownOne.intersects(HighBitMask)) {
1105 // Mask out the high bit, which we know is set.
1106 Amt = DAG.getNode(ISD::AND, ShTy, Amt,
1107 DAG.getConstant(~HighBitMask, ShTy));
1109 switch (N->getOpcode()) {
1110 default: assert(0 && "Unknown shift");
1112 Lo = DAG.getConstant(0, NVT); // Low part is zero.
1113 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
1116 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
1117 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
1120 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
1121 DAG.getConstant(NVTBits-1, ShTy));
1122 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
1128 // FIXME: This code is broken for shifts with a zero amount!
1129 // If we know that all of the high bits of the shift amount are zero, then we
1130 // can do this as a couple of simple shifts.
1131 if ((KnownZero & HighBitMask) == HighBitMask) {
1133 SDValue Amt2 = DAG.getNode(ISD::SUB, ShTy,
1134 DAG.getConstant(NVTBits, ShTy),
1137 switch (N->getOpcode()) {
1138 default: assert(0 && "Unknown shift");
1139 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1141 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1144 Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
1145 Hi = DAG.getNode(ISD::OR, NVT,
1146 DAG.getNode(Op1, NVT, InH, Amt),
1147 DAG.getNode(Op2, NVT, InL, Amt2));
1155 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
1156 SDValue &Lo, SDValue &Hi) {
1157 // Expand the subcomponents.
1158 SDValue LHSL, LHSH, RHSL, RHSH;
1159 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1160 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1162 MVT NVT = LHSL.getValueType();
1163 SDValue LoOps[2] = { LHSL, RHSL };
1164 SDValue HiOps[3] = { LHSH, RHSH };
1166 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
1167 // them. TODO: Teach operation legalization how to expand unsupported
1168 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
1169 // a carry of type MVT::Flag, but there doesn't seem to be any way to
1170 // generate a value of this type in the expanded code sequence.
1172 TLI.isOperationLegal(N->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC,
1173 TLI.getTypeToExpandTo(NVT));
1176 SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1177 if (N->getOpcode() == ISD::ADD) {
1178 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1179 HiOps[2] = Lo.getValue(1);
1180 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1182 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1183 HiOps[2] = Lo.getValue(1);
1184 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1187 if (N->getOpcode() == ISD::ADD) {
1188 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
1189 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
1190 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(NVT), Lo, LoOps[0],
1192 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
1193 DAG.getConstant(1, NVT),
1194 DAG.getConstant(0, NVT));
1195 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(NVT), Lo, LoOps[1],
1197 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
1198 DAG.getConstant(1, NVT), Carry1);
1199 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
1201 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
1202 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
1204 DAG.getSetCC(TLI.getSetCCResultType(LoOps[0].getValueType()),
1205 LoOps[0], LoOps[1], ISD::SETULT);
1206 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
1207 DAG.getConstant(1, NVT),
1208 DAG.getConstant(0, NVT));
1209 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
1214 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
1215 SDValue &Lo, SDValue &Hi) {
1216 // Expand the subcomponents.
1217 SDValue LHSL, LHSH, RHSL, RHSH;
1218 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1219 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1220 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1221 SDValue LoOps[2] = { LHSL, RHSL };
1222 SDValue HiOps[3] = { LHSH, RHSH };
1224 if (N->getOpcode() == ISD::ADDC) {
1225 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1226 HiOps[2] = Lo.getValue(1);
1227 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1229 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1230 HiOps[2] = Lo.getValue(1);
1231 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1234 // Legalized the flag result - switch anything that used the old flag to
1236 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1239 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(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[3] = { LHSL, RHSL, N->getOperand(2) };
1247 SDValue HiOps[3] = { LHSH, RHSH };
1249 Lo = DAG.getNode(N->getOpcode(), VTList, LoOps, 3);
1250 HiOps[2] = Lo.getValue(1);
1251 Hi = DAG.getNode(N->getOpcode(), VTList, HiOps, 3);
1253 // Legalized the flag result - switch anything that used the old flag to
1255 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1258 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
1259 SDValue &Lo, SDValue &Hi) {
1260 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1261 SDValue Op = N->getOperand(0);
1262 if (Op.getValueType().bitsLE(NVT)) {
1263 // The low part is any extension of the input (which degenerates to a copy).
1264 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Op);
1265 Hi = DAG.getNode(ISD::UNDEF, NVT); // The high part is undefined.
1267 // For example, extension of an i48 to an i64. The operand type necessarily
1268 // promotes to the result type, so will end up being expanded too.
1269 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1270 "Only know how to promote this result!");
1271 SDValue Res = GetPromotedInteger(Op);
1272 assert(Res.getValueType() == N->getValueType(0) &&
1273 "Operand over promoted?");
1274 // Split the promoted operand. This will simplify when it is expanded.
1275 SplitInteger(Res, Lo, Hi);
1279 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
1280 SDValue &Lo, SDValue &Hi) {
1281 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1282 MVT NVT = Lo.getValueType();
1283 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1284 unsigned NVTBits = NVT.getSizeInBits();
1285 unsigned EVTBits = EVT.getSizeInBits();
1287 if (NVTBits < EVTBits) {
1288 Hi = DAG.getNode(ISD::AssertSext, NVT, Hi,
1289 DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1291 Lo = DAG.getNode(ISD::AssertSext, NVT, Lo, DAG.getValueType(EVT));
1292 // The high part replicates the sign bit of Lo, make it explicit.
1293 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1294 DAG.getConstant(NVTBits-1, TLI.getShiftAmountTy()));
1298 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
1299 SDValue &Lo, SDValue &Hi) {
1300 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1301 MVT NVT = Lo.getValueType();
1302 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1303 unsigned NVTBits = NVT.getSizeInBits();
1304 unsigned EVTBits = EVT.getSizeInBits();
1306 if (NVTBits < EVTBits) {
1307 Hi = DAG.getNode(ISD::AssertZext, NVT, Hi,
1308 DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1310 Lo = DAG.getNode(ISD::AssertZext, NVT, Lo, DAG.getValueType(EVT));
1311 // The high part must be zero, make it explicit.
1312 Hi = DAG.getConstant(0, NVT);
1316 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
1317 SDValue &Lo, SDValue &Hi) {
1318 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
1319 Lo = DAG.getNode(ISD::BSWAP, Lo.getValueType(), Lo);
1320 Hi = DAG.getNode(ISD::BSWAP, Hi.getValueType(), Hi);
1323 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
1324 SDValue &Lo, SDValue &Hi) {
1325 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1326 unsigned NBitWidth = NVT.getSizeInBits();
1327 const APInt &Cst = cast<ConstantSDNode>(N)->getAPIntValue();
1328 Lo = DAG.getConstant(APInt(Cst).trunc(NBitWidth), NVT);
1329 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
1332 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
1333 SDValue &Lo, SDValue &Hi) {
1334 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
1335 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1336 MVT NVT = Lo.getValueType();
1338 SDValue HiNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), Hi,
1339 DAG.getConstant(0, NVT), ISD::SETNE);
1341 SDValue LoLZ = DAG.getNode(ISD::CTLZ, NVT, Lo);
1342 SDValue HiLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
1344 Lo = DAG.getNode(ISD::SELECT, NVT, HiNotZero, HiLZ,
1345 DAG.getNode(ISD::ADD, NVT, LoLZ,
1346 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1347 Hi = DAG.getConstant(0, NVT);
1350 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
1351 SDValue &Lo, SDValue &Hi) {
1352 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
1353 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1354 MVT NVT = Lo.getValueType();
1355 Lo = DAG.getNode(ISD::ADD, NVT, DAG.getNode(ISD::CTPOP, NVT, Lo),
1356 DAG.getNode(ISD::CTPOP, NVT, Hi));
1357 Hi = DAG.getConstant(0, NVT);
1360 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
1361 SDValue &Lo, SDValue &Hi) {
1362 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
1363 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1364 MVT NVT = Lo.getValueType();
1366 SDValue LoNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), Lo,
1367 DAG.getConstant(0, NVT), ISD::SETNE);
1369 SDValue LoLZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
1370 SDValue HiLZ = DAG.getNode(ISD::CTTZ, NVT, Hi);
1372 Lo = DAG.getNode(ISD::SELECT, NVT, LoNotZero, LoLZ,
1373 DAG.getNode(ISD::ADD, NVT, HiLZ,
1374 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1375 Hi = DAG.getConstant(0, NVT);
1378 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
1380 MVT VT = N->getValueType(0);
1381 SDValue Op = N->getOperand(0);
1382 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
1383 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
1384 SplitInteger(MakeLibCall(LC, VT, &Op, 1, true/*sign irrelevant*/), Lo, Hi);
1387 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
1389 MVT VT = N->getValueType(0);
1390 SDValue Op = N->getOperand(0);
1391 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
1392 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
1393 SplitInteger(MakeLibCall(LC, VT, &Op, 1, false/*sign irrelevant*/), Lo, Hi);
1396 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
1397 SDValue &Lo, SDValue &Hi) {
1398 if (ISD::isNormalLoad(N)) {
1399 ExpandRes_NormalLoad(N, Lo, Hi);
1403 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1405 MVT VT = N->getValueType(0);
1406 MVT NVT = TLI.getTypeToTransformTo(VT);
1407 SDValue Ch = N->getChain();
1408 SDValue Ptr = N->getBasePtr();
1409 ISD::LoadExtType ExtType = N->getExtensionType();
1410 int SVOffset = N->getSrcValueOffset();
1411 unsigned Alignment = N->getAlignment();
1412 bool isVolatile = N->isVolatile();
1414 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1416 if (N->getMemoryVT().bitsLE(NVT)) {
1417 MVT EVT = N->getMemoryVT();
1419 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset, EVT,
1420 isVolatile, Alignment);
1422 // Remember the chain.
1423 Ch = Lo.getValue(1);
1425 if (ExtType == ISD::SEXTLOAD) {
1426 // The high part is obtained by SRA'ing all but one of the bits of the
1428 unsigned LoSize = Lo.getValueType().getSizeInBits();
1429 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1430 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
1431 } else if (ExtType == ISD::ZEXTLOAD) {
1432 // The high part is just a zero.
1433 Hi = DAG.getConstant(0, NVT);
1435 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
1436 // The high part is undefined.
1437 Hi = DAG.getNode(ISD::UNDEF, NVT);
1439 } else if (TLI.isLittleEndian()) {
1440 // Little-endian - low bits are at low addresses.
1441 Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1442 isVolatile, Alignment);
1444 unsigned ExcessBits =
1445 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
1446 MVT NEVT = MVT::getIntegerVT(ExcessBits);
1448 // Increment the pointer to the other half.
1449 unsigned IncrementSize = NVT.getSizeInBits()/8;
1450 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1451 DAG.getIntPtrConstant(IncrementSize));
1452 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(),
1453 SVOffset+IncrementSize, NEVT,
1454 isVolatile, MinAlign(Alignment, IncrementSize));
1456 // Build a factor node to remember that this load is independent of the
1458 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1461 // Big-endian - high bits are at low addresses. Favor aligned loads at
1462 // the cost of some bit-fiddling.
1463 MVT EVT = N->getMemoryVT();
1464 unsigned EBytes = EVT.getStoreSizeInBits()/8;
1465 unsigned IncrementSize = NVT.getSizeInBits()/8;
1466 unsigned ExcessBits = (EBytes - IncrementSize)*8;
1468 // Load both the high bits and maybe some of the low bits.
1469 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1470 MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits),
1471 isVolatile, Alignment);
1473 // Increment the pointer to the other half.
1474 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1475 DAG.getIntPtrConstant(IncrementSize));
1476 // Load the rest of the low bits.
1477 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Ch, Ptr, N->getSrcValue(),
1478 SVOffset+IncrementSize,
1479 MVT::getIntegerVT(ExcessBits),
1480 isVolatile, MinAlign(Alignment, IncrementSize));
1482 // Build a factor node to remember that this load is independent of the
1484 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1487 if (ExcessBits < NVT.getSizeInBits()) {
1488 // Transfer low bits from the bottom of Hi to the top of Lo.
1489 Lo = DAG.getNode(ISD::OR, NVT, Lo,
1490 DAG.getNode(ISD::SHL, NVT, Hi,
1491 DAG.getConstant(ExcessBits,
1492 TLI.getShiftAmountTy())));
1493 // Move high bits to the right position in Hi.
1494 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, NVT, Hi,
1495 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
1496 TLI.getShiftAmountTy()));
1500 // Legalized the chain result - switch anything that used the old chain to
1502 ReplaceValueWith(SDValue(N, 1), Ch);
1505 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
1506 SDValue &Lo, SDValue &Hi) {
1507 SDValue LL, LH, RL, RH;
1508 GetExpandedInteger(N->getOperand(0), LL, LH);
1509 GetExpandedInteger(N->getOperand(1), RL, RH);
1510 Lo = DAG.getNode(N->getOpcode(), LL.getValueType(), LL, RL);
1511 Hi = DAG.getNode(N->getOpcode(), LL.getValueType(), LH, RH);
1514 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
1515 SDValue &Lo, SDValue &Hi) {
1516 MVT VT = N->getValueType(0);
1517 MVT NVT = TLI.getTypeToTransformTo(VT);
1519 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
1520 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
1521 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
1522 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
1523 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1524 SDValue LL, LH, RL, RH;
1525 GetExpandedInteger(N->getOperand(0), LL, LH);
1526 GetExpandedInteger(N->getOperand(1), RL, RH);
1527 unsigned OuterBitSize = VT.getSizeInBits();
1528 unsigned InnerBitSize = NVT.getSizeInBits();
1529 unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1530 unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
1532 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
1533 if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
1534 DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
1535 // The inputs are both zero-extended.
1537 // We can emit a umul_lohi.
1538 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1539 Hi = SDValue(Lo.getNode(), 1);
1543 // We can emit a mulhu+mul.
1544 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1545 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1549 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
1550 // The input values are both sign-extended.
1552 // We can emit a smul_lohi.
1553 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1554 Hi = SDValue(Lo.getNode(), 1);
1558 // We can emit a mulhs+mul.
1559 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1560 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
1565 // Lo,Hi = umul LHS, RHS.
1566 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
1567 DAG.getVTList(NVT, NVT), LL, RL);
1569 Hi = UMulLOHI.getValue(1);
1570 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1571 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1572 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1573 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1577 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1578 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1579 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1580 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1581 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1582 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1587 // If nothing else, we can make a libcall.
1588 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1590 LC = RTLIB::MUL_I16;
1591 else if (VT == MVT::i32)
1592 LC = RTLIB::MUL_I32;
1593 else if (VT == MVT::i64)
1594 LC = RTLIB::MUL_I64;
1595 else if (VT == MVT::i128)
1596 LC = RTLIB::MUL_I128;
1597 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
1599 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1600 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true/*sign irrelevant*/), Lo, Hi);
1603 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
1604 SDValue &Lo, SDValue &Hi) {
1605 MVT VT = N->getValueType(0);
1607 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1609 LC = RTLIB::SDIV_I32;
1610 else if (VT == MVT::i64)
1611 LC = RTLIB::SDIV_I64;
1612 else if (VT == MVT::i128)
1613 LC = RTLIB::SDIV_I128;
1614 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1616 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1617 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true), Lo, Hi);
1620 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
1621 SDValue &Lo, SDValue &Hi) {
1622 MVT VT = N->getValueType(0);
1624 // If we can emit an efficient shift operation, do so now. Check to see if
1625 // the RHS is a constant.
1626 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1627 return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi);
1629 // If we can determine that the high bit of the shift is zero or one, even if
1630 // the low bits are variable, emit this shift in an optimized form.
1631 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
1634 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
1636 if (N->getOpcode() == ISD::SHL) {
1637 PartsOpc = ISD::SHL_PARTS;
1638 } else if (N->getOpcode() == ISD::SRL) {
1639 PartsOpc = ISD::SRL_PARTS;
1641 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1642 PartsOpc = ISD::SRA_PARTS;
1645 // Next check to see if the target supports this SHL_PARTS operation or if it
1646 // will custom expand it.
1647 MVT NVT = TLI.getTypeToTransformTo(VT);
1648 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
1649 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
1650 Action == TargetLowering::Custom) {
1651 // Expand the subcomponents.
1653 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1655 SDValue Ops[] = { LHSL, LHSH, N->getOperand(1) };
1656 MVT VT = LHSL.getValueType();
1657 Lo = DAG.getNode(PartsOpc, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
1658 Hi = Lo.getValue(1);
1662 // Otherwise, emit a libcall.
1663 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1665 if (N->getOpcode() == ISD::SHL) {
1666 isSigned = false; /*sign irrelevant*/
1668 LC = RTLIB::SHL_I16;
1669 else if (VT == MVT::i32)
1670 LC = RTLIB::SHL_I32;
1671 else if (VT == MVT::i64)
1672 LC = RTLIB::SHL_I64;
1673 else if (VT == MVT::i128)
1674 LC = RTLIB::SHL_I128;
1675 } else if (N->getOpcode() == ISD::SRL) {
1678 LC = RTLIB::SRL_I16;
1679 else if (VT == MVT::i32)
1680 LC = RTLIB::SRL_I32;
1681 else if (VT == MVT::i64)
1682 LC = RTLIB::SRL_I64;
1683 else if (VT == MVT::i128)
1684 LC = RTLIB::SRL_I128;
1686 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1689 LC = RTLIB::SRA_I16;
1690 else if (VT == MVT::i32)
1691 LC = RTLIB::SRA_I32;
1692 else if (VT == MVT::i64)
1693 LC = RTLIB::SRA_I64;
1694 else if (VT == MVT::i128)
1695 LC = RTLIB::SRA_I128;
1697 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported shift!");
1699 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1700 SplitInteger(MakeLibCall(LC, VT, Ops, 2, isSigned), Lo, Hi);
1703 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
1704 SDValue &Lo, SDValue &Hi) {
1705 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1706 SDValue Op = N->getOperand(0);
1707 if (Op.getValueType().bitsLE(NVT)) {
1708 // The low part is sign extension of the input (degenerates to a copy).
1709 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, N->getOperand(0));
1710 // The high part is obtained by SRA'ing all but one of the bits of low part.
1711 unsigned LoSize = NVT.getSizeInBits();
1712 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1713 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
1715 // For example, extension of an i48 to an i64. The operand type necessarily
1716 // promotes to the result type, so will end up being expanded too.
1717 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1718 "Only know how to promote this result!");
1719 SDValue Res = GetPromotedInteger(Op);
1720 assert(Res.getValueType() == N->getValueType(0) &&
1721 "Operand over promoted?");
1722 // Split the promoted operand. This will simplify when it is expanded.
1723 SplitInteger(Res, Lo, Hi);
1724 unsigned ExcessBits =
1725 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1726 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
1727 DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1731 void DAGTypeLegalizer::
1732 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
1733 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1734 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1736 if (EVT.bitsLE(Lo.getValueType())) {
1737 // sext_inreg the low part if needed.
1738 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, Lo.getValueType(), Lo,
1741 // The high part gets the sign extension from the lo-part. This handles
1742 // things like sextinreg V:i64 from i8.
1743 Hi = DAG.getNode(ISD::SRA, Hi.getValueType(), Lo,
1744 DAG.getConstant(Hi.getValueType().getSizeInBits()-1,
1745 TLI.getShiftAmountTy()));
1747 // For example, extension of an i48 to an i64. Leave the low part alone,
1748 // sext_inreg the high part.
1749 unsigned ExcessBits =
1750 EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
1751 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
1752 DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1756 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
1757 SDValue &Lo, SDValue &Hi) {
1758 MVT VT = N->getValueType(0);
1760 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1762 LC = RTLIB::SREM_I32;
1763 else if (VT == MVT::i64)
1764 LC = RTLIB::SREM_I64;
1765 else if (VT == MVT::i128)
1766 LC = RTLIB::SREM_I128;
1767 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1769 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1770 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true), Lo, Hi);
1773 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
1774 SDValue &Lo, SDValue &Hi) {
1775 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1776 Lo = DAG.getNode(ISD::TRUNCATE, NVT, N->getOperand(0));
1777 Hi = DAG.getNode(ISD::SRL, N->getOperand(0).getValueType(), N->getOperand(0),
1778 DAG.getConstant(NVT.getSizeInBits(),
1779 TLI.getShiftAmountTy()));
1780 Hi = DAG.getNode(ISD::TRUNCATE, NVT, Hi);
1783 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
1784 SDValue &Lo, SDValue &Hi) {
1785 MVT VT = N->getValueType(0);
1787 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1789 LC = RTLIB::UDIV_I32;
1790 else if (VT == MVT::i64)
1791 LC = RTLIB::UDIV_I64;
1792 else if (VT == MVT::i128)
1793 LC = RTLIB::UDIV_I128;
1794 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
1796 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1797 SplitInteger(MakeLibCall(LC, VT, Ops, 2, false), Lo, Hi);
1800 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
1801 SDValue &Lo, SDValue &Hi) {
1802 MVT VT = N->getValueType(0);
1804 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1806 LC = RTLIB::UREM_I32;
1807 else if (VT == MVT::i64)
1808 LC = RTLIB::UREM_I64;
1809 else if (VT == MVT::i128)
1810 LC = RTLIB::UREM_I128;
1811 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
1813 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1814 SplitInteger(MakeLibCall(LC, VT, Ops, 2, false), Lo, Hi);
1817 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
1818 SDValue &Lo, SDValue &Hi) {
1819 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1820 SDValue Op = N->getOperand(0);
1821 if (Op.getValueType().bitsLE(NVT)) {
1822 // The low part is zero extension of the input (degenerates to a copy).
1823 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, N->getOperand(0));
1824 Hi = DAG.getConstant(0, NVT); // The high part is just a zero.
1826 // For example, extension of an i48 to an i64. The operand type necessarily
1827 // promotes to the result type, so will end up being expanded too.
1828 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1829 "Only know how to promote this result!");
1830 SDValue Res = GetPromotedInteger(Op);
1831 assert(Res.getValueType() == N->getValueType(0) &&
1832 "Operand over promoted?");
1833 // Split the promoted operand. This will simplify when it is expanded.
1834 SplitInteger(Res, Lo, Hi);
1835 unsigned ExcessBits =
1836 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1837 Hi = DAG.getZeroExtendInReg(Hi, MVT::getIntegerVT(ExcessBits));
1842 //===----------------------------------------------------------------------===//
1843 // Integer Operand Expansion
1844 //===----------------------------------------------------------------------===//
1846 /// ExpandIntegerOperand - This method is called when the specified operand of
1847 /// the specified node is found to need expansion. At this point, all of the
1848 /// result types of the node are known to be legal, but other operands of the
1849 /// node may need promotion or expansion as well as the specified one.
1850 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
1851 DEBUG(cerr << "Expand integer operand: "; N->dump(&DAG); cerr << "\n");
1852 SDValue Res = SDValue();
1854 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
1855 == TargetLowering::Custom)
1856 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
1858 if (Res.getNode() == 0) {
1859 switch (N->getOpcode()) {
1862 cerr << "ExpandIntegerOperand Op #" << OpNo << ": ";
1863 N->dump(&DAG); cerr << "\n";
1865 assert(0 && "Do not know how to expand this operator's operand!");
1868 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1869 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
1870 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1871 case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
1872 case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
1874 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
1875 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
1876 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
1877 case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
1878 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo);
1880 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
1881 case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
1885 // If the result is null, the sub-method took care of registering results etc.
1886 if (!Res.getNode()) return false;
1888 // If the result is N, the sub-method updated N in place. Tell the legalizer
1890 if (Res.getNode() == N)
1893 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1894 "Invalid operand expansion");
1896 ReplaceValueWith(SDValue(N, 0), Res);
1900 /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
1901 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1902 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
1904 ISD::CondCode &CCCode) {
1905 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1906 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
1907 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
1909 MVT VT = NewLHS.getValueType();
1911 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1912 if (RHSLo == RHSHi) {
1913 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
1914 if (RHSCST->isAllOnesValue()) {
1915 // Equality comparison to -1.
1916 NewLHS = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
1923 NewLHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1924 NewRHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1925 NewLHS = DAG.getNode(ISD::OR, NewLHS.getValueType(), NewLHS, NewRHS);
1926 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1930 // If this is a comparison of the sign bit, just look at the top part.
1932 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1933 if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
1934 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
1940 // FIXME: This generated code sucks.
1941 ISD::CondCode LowCC;
1943 default: assert(0 && "Unknown integer setcc!");
1945 case ISD::SETULT: LowCC = ISD::SETULT; break;
1947 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1949 case ISD::SETULE: LowCC = ISD::SETULE; break;
1951 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1954 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1955 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1956 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1958 // NOTE: on targets without efficient SELECT of bools, we can always use
1959 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
1960 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
1962 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
1963 LHSLo, RHSLo, LowCC, false, DagCombineInfo);
1964 if (!Tmp1.getNode())
1965 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
1966 LHSLo, RHSLo, LowCC);
1967 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
1968 LHSHi, RHSHi, CCCode, false, DagCombineInfo);
1969 if (!Tmp2.getNode())
1970 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi.getValueType()),
1971 LHSHi, RHSHi, DAG.getCondCode(CCCode));
1973 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
1974 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
1975 if ((Tmp1C && Tmp1C->isNullValue()) ||
1976 (Tmp2C && Tmp2C->isNullValue() &&
1977 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
1978 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
1979 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
1980 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
1981 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
1982 // low part is known false, returns high part.
1983 // For LE / GE, if high part is known false, ignore the low part.
1984 // For LT / GT, if high part is known true, ignore the low part.
1990 NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
1991 LHSHi, RHSHi, ISD::SETEQ, false, DagCombineInfo);
1992 if (!NewLHS.getNode())
1993 NewLHS = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
1994 LHSHi, RHSHi, ISD::SETEQ);
1995 NewLHS = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1996 NewLHS, Tmp1, Tmp2);
2000 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
2001 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
2002 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
2003 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2005 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2006 // against zero to select between true and false values.
2007 if (NewRHS.getNode() == 0) {
2008 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2009 CCCode = ISD::SETNE;
2012 // Update N to have the operands specified.
2013 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
2014 DAG.getCondCode(CCCode), NewLHS, NewRHS,
2018 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
2019 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2020 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
2021 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2023 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2024 // against zero to select between true and false values.
2025 if (NewRHS.getNode() == 0) {
2026 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2027 CCCode = ISD::SETNE;
2030 // Update N to have the operands specified.
2031 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2032 N->getOperand(2), N->getOperand(3),
2033 DAG.getCondCode(CCCode));
2036 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
2037 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2038 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
2039 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2041 // If ExpandSetCCOperands returned a scalar, use it.
2042 if (NewRHS.getNode() == 0) {
2043 assert(NewLHS.getValueType() == N->getValueType(0) &&
2044 "Unexpected setcc expansion!");
2048 // Otherwise, update N to have the operands specified.
2049 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2050 DAG.getCondCode(CCCode));
2053 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
2054 SDValue Op = N->getOperand(0);
2055 MVT DstVT = N->getValueType(0);
2056 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
2057 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2058 "Don't know how to expand this SINT_TO_FP!");
2059 return MakeLibCall(LC, DstVT, &Op, 1, true);
2062 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
2063 if (ISD::isNormalStore(N))
2064 return ExpandOp_NormalStore(N, OpNo);
2066 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2067 assert(OpNo == 1 && "Can only expand the stored value so far");
2069 MVT VT = N->getOperand(1).getValueType();
2070 MVT NVT = TLI.getTypeToTransformTo(VT);
2071 SDValue Ch = N->getChain();
2072 SDValue Ptr = N->getBasePtr();
2073 int SVOffset = N->getSrcValueOffset();
2074 unsigned Alignment = N->getAlignment();
2075 bool isVolatile = N->isVolatile();
2078 assert(NVT.isByteSized() && "Expanded type not byte sized!");
2080 if (N->getMemoryVT().bitsLE(NVT)) {
2081 GetExpandedInteger(N->getValue(), Lo, Hi);
2082 return DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
2083 N->getMemoryVT(), isVolatile, Alignment);
2084 } else if (TLI.isLittleEndian()) {
2085 // Little-endian - low bits are at low addresses.
2086 GetExpandedInteger(N->getValue(), Lo, Hi);
2088 Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
2089 isVolatile, Alignment);
2091 unsigned ExcessBits =
2092 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
2093 MVT NEVT = MVT::getIntegerVT(ExcessBits);
2095 // Increment the pointer to the other half.
2096 unsigned IncrementSize = NVT.getSizeInBits()/8;
2097 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2098 DAG.getIntPtrConstant(IncrementSize));
2099 Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
2100 SVOffset+IncrementSize, NEVT,
2101 isVolatile, MinAlign(Alignment, IncrementSize));
2102 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2104 // Big-endian - high bits are at low addresses. Favor aligned stores at
2105 // the cost of some bit-fiddling.
2106 GetExpandedInteger(N->getValue(), Lo, Hi);
2108 MVT EVT = N->getMemoryVT();
2109 unsigned EBytes = EVT.getStoreSizeInBits()/8;
2110 unsigned IncrementSize = NVT.getSizeInBits()/8;
2111 unsigned ExcessBits = (EBytes - IncrementSize)*8;
2112 MVT HiVT = MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits);
2114 if (ExcessBits < NVT.getSizeInBits()) {
2115 // Transfer high bits from the top of Lo to the bottom of Hi.
2116 Hi = DAG.getNode(ISD::SHL, NVT, Hi,
2117 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
2118 TLI.getShiftAmountTy()));
2119 Hi = DAG.getNode(ISD::OR, NVT, Hi,
2120 DAG.getNode(ISD::SRL, NVT, Lo,
2121 DAG.getConstant(ExcessBits,
2122 TLI.getShiftAmountTy())));
2125 // Store both the high bits and maybe some of the low bits.
2126 Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
2127 SVOffset, HiVT, isVolatile, Alignment);
2129 // Increment the pointer to the other half.
2130 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2131 DAG.getIntPtrConstant(IncrementSize));
2132 // Store the lowest ExcessBits bits in the second half.
2133 Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(),
2134 SVOffset+IncrementSize,
2135 MVT::getIntegerVT(ExcessBits),
2136 isVolatile, MinAlign(Alignment, IncrementSize));
2137 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2141 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
2143 GetExpandedInteger(N->getOperand(0), InL, InH);
2144 // Just truncate the low part of the source.
2145 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), InL);
2148 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
2149 SDValue Op = N->getOperand(0);
2150 MVT SrcVT = Op.getValueType();
2151 MVT DstVT = N->getValueType(0);
2153 if (TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
2154 // Do a signed conversion then adjust the result.
2155 SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, DstVT, Op);
2156 SignedConv = TLI.LowerOperation(SignedConv, DAG);
2158 // The result of the signed conversion needs adjusting if the 'sign bit' of
2159 // the incoming integer was set. To handle this, we dynamically test to see
2160 // if it is set, and, if so, add a fudge factor.
2162 const uint64_t F32TwoE32 = 0x4F800000ULL;
2163 const uint64_t F32TwoE64 = 0x5F800000ULL;
2164 const uint64_t F32TwoE128 = 0x7F800000ULL;
2167 if (SrcVT == MVT::i32)
2168 FF = APInt(32, F32TwoE32);
2169 else if (SrcVT == MVT::i64)
2170 FF = APInt(32, F32TwoE64);
2171 else if (SrcVT == MVT::i128)
2172 FF = APInt(32, F32TwoE128);
2174 assert(false && "Unsupported UINT_TO_FP!");
2176 // Check whether the sign bit is set.
2178 GetExpandedInteger(Op, Lo, Hi);
2179 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi.getValueType()),
2180 Hi, DAG.getConstant(0, Hi.getValueType()),
2183 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
2184 SDValue FudgePtr = DAG.getConstantPool(ConstantInt::get(FF.zext(64)),
2185 TLI.getPointerTy());
2187 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
2188 SDValue Zero = DAG.getIntPtrConstant(0);
2189 SDValue Four = DAG.getIntPtrConstant(4);
2190 if (TLI.isBigEndian()) std::swap(Zero, Four);
2191 SDValue Offset = DAG.getNode(ISD::SELECT, Zero.getValueType(), SignSet,
2193 unsigned Alignment =
2194 1 << cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
2195 FudgePtr = DAG.getNode(ISD::ADD, TLI.getPointerTy(), FudgePtr, Offset);
2196 Alignment = std::min(Alignment, 4u);
2198 // Load the value out, extending it from f32 to the destination float type.
2199 // FIXME: Avoid the extend by constructing the right constant pool?
2200 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, DstVT, DAG.getEntryNode(),
2201 FudgePtr, NULL, 0, MVT::f32,
2203 return DAG.getNode(ISD::FADD, DstVT, SignedConv, Fudge);
2206 // Otherwise, use a libcall.
2207 RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
2208 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2209 "Don't know how to expand this UINT_TO_FP!");
2210 return MakeLibCall(LC, DstVT, &Op, 1, true);