1 //===----- LegalizeIntegerTypes.cpp - Legalization of integer types -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements integer type expansion and promotion for LegalizeTypes.
11 // Promotion is the act of changing a computation in an illegal type into a
12 // computation in a larger type. For example, implementing i8 arithmetic in an
13 // i32 register (often needed on powerpc).
14 // Expansion is the act of changing a computation in an illegal type into a
15 // computation in two identical registers of a smaller type. For example,
16 // implementing i64 arithmetic in two i32 registers (often needed on 32-bit
19 //===----------------------------------------------------------------------===//
21 #include "LegalizeTypes.h"
24 //===----------------------------------------------------------------------===//
25 // Integer Result Promotion
26 //===----------------------------------------------------------------------===//
28 /// PromoteIntegerResult - This method is called when a result of a node is
29 /// found to be in need of promotion to a larger type. At this point, the node
30 /// may also have invalid operands or may have other results that need
31 /// expansion, we just know that (at least) one result needs promotion.
32 void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
33 DEBUG(cerr << "Promote integer result: "; N->dump(&DAG); cerr << "\n");
34 SDValue Result = SDValue();
36 // See if the target wants to custom expand this node.
37 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
38 TargetLowering::Custom) {
39 // If the target wants to, allow it to lower this itself.
40 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
41 // Everything that once used N now uses P. We are guaranteed that the
42 // result value types of N and the result value types of P match.
43 ReplaceNodeWith(N, P);
48 switch (N->getOpcode()) {
51 cerr << "PromoteIntegerResult #" << ResNo << ": ";
52 N->dump(&DAG); cerr << "\n";
54 assert(0 && "Do not know how to promote this operator!");
56 case ISD::AssertSext: Result = PromoteIntRes_AssertSext(N); break;
57 case ISD::AssertZext: Result = PromoteIntRes_AssertZext(N); break;
58 case ISD::BIT_CONVERT: Result = PromoteIntRes_BIT_CONVERT(N); break;
59 case ISD::BSWAP: Result = PromoteIntRes_BSWAP(N); break;
60 case ISD::BUILD_PAIR: Result = PromoteIntRes_BUILD_PAIR(N); break;
61 case ISD::Constant: Result = PromoteIntRes_Constant(N); break;
62 case ISD::CTLZ: Result = PromoteIntRes_CTLZ(N); break;
63 case ISD::CTPOP: Result = PromoteIntRes_CTPOP(N); break;
64 case ISD::CTTZ: Result = PromoteIntRes_CTTZ(N); break;
65 case ISD::EXTRACT_VECTOR_ELT:
66 Result = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
67 case ISD::LOAD: Result = PromoteIntRes_LOAD(cast<LoadSDNode>(N));break;
68 case ISD::SELECT: Result = PromoteIntRes_SELECT(N); break;
69 case ISD::SELECT_CC: Result = PromoteIntRes_SELECT_CC(N); break;
70 case ISD::SETCC: Result = PromoteIntRes_SETCC(N); break;
71 case ISD::SHL: Result = PromoteIntRes_SHL(N); break;
72 case ISD::SIGN_EXTEND_INREG:
73 Result = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
74 case ISD::SRA: Result = PromoteIntRes_SRA(N); break;
75 case ISD::SRL: Result = PromoteIntRes_SRL(N); break;
76 case ISD::TRUNCATE: Result = PromoteIntRes_TRUNCATE(N); break;
77 case ISD::UNDEF: Result = PromoteIntRes_UNDEF(N); break;
78 case ISD::VAARG: Result = PromoteIntRes_VAARG(N); break;
80 case ISD::SIGN_EXTEND:
81 case ISD::ZERO_EXTEND:
82 case ISD::ANY_EXTEND: Result = PromoteIntRes_INT_EXTEND(N); break;
85 case ISD::FP_TO_UINT: Result = PromoteIntRes_FP_TO_XINT(N); break;
92 case ISD::MUL: Result = PromoteIntRes_SimpleIntBinOp(N); break;
95 case ISD::SREM: Result = PromoteIntRes_SDIV(N); break;
98 case ISD::UREM: Result = PromoteIntRes_UDIV(N); break;
100 case ISD::ATOMIC_LOAD_ADD_8:
101 case ISD::ATOMIC_LOAD_SUB_8:
102 case ISD::ATOMIC_LOAD_AND_8:
103 case ISD::ATOMIC_LOAD_OR_8:
104 case ISD::ATOMIC_LOAD_XOR_8:
105 case ISD::ATOMIC_LOAD_NAND_8:
106 case ISD::ATOMIC_LOAD_MIN_8:
107 case ISD::ATOMIC_LOAD_MAX_8:
108 case ISD::ATOMIC_LOAD_UMIN_8:
109 case ISD::ATOMIC_LOAD_UMAX_8:
110 case ISD::ATOMIC_SWAP_8:
111 case ISD::ATOMIC_LOAD_ADD_16:
112 case ISD::ATOMIC_LOAD_SUB_16:
113 case ISD::ATOMIC_LOAD_AND_16:
114 case ISD::ATOMIC_LOAD_OR_16:
115 case ISD::ATOMIC_LOAD_XOR_16:
116 case ISD::ATOMIC_LOAD_NAND_16:
117 case ISD::ATOMIC_LOAD_MIN_16:
118 case ISD::ATOMIC_LOAD_MAX_16:
119 case ISD::ATOMIC_LOAD_UMIN_16:
120 case ISD::ATOMIC_LOAD_UMAX_16:
121 case ISD::ATOMIC_SWAP_16:
122 case ISD::ATOMIC_LOAD_ADD_32:
123 case ISD::ATOMIC_LOAD_SUB_32:
124 case ISD::ATOMIC_LOAD_AND_32:
125 case ISD::ATOMIC_LOAD_OR_32:
126 case ISD::ATOMIC_LOAD_XOR_32:
127 case ISD::ATOMIC_LOAD_NAND_32:
128 case ISD::ATOMIC_LOAD_MIN_32:
129 case ISD::ATOMIC_LOAD_MAX_32:
130 case ISD::ATOMIC_LOAD_UMIN_32:
131 case ISD::ATOMIC_LOAD_UMAX_32:
132 case ISD::ATOMIC_SWAP_32:
133 case ISD::ATOMIC_LOAD_ADD_64:
134 case ISD::ATOMIC_LOAD_SUB_64:
135 case ISD::ATOMIC_LOAD_AND_64:
136 case ISD::ATOMIC_LOAD_OR_64:
137 case ISD::ATOMIC_LOAD_XOR_64:
138 case ISD::ATOMIC_LOAD_NAND_64:
139 case ISD::ATOMIC_LOAD_MIN_64:
140 case ISD::ATOMIC_LOAD_MAX_64:
141 case ISD::ATOMIC_LOAD_UMIN_64:
142 case ISD::ATOMIC_LOAD_UMAX_64:
143 case ISD::ATOMIC_SWAP_64:
144 Result = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
146 case ISD::ATOMIC_CMP_SWAP_8:
147 case ISD::ATOMIC_CMP_SWAP_16:
148 case ISD::ATOMIC_CMP_SWAP_32:
149 case ISD::ATOMIC_CMP_SWAP_64:
150 Result = PromoteIntRes_Atomic2(cast<AtomicSDNode>(N)); break;
153 // If Result is null, the sub-method took care of registering the result.
154 if (Result.getNode())
155 SetPromotedInteger(SDValue(N, ResNo), Result);
158 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
159 // Sign-extend the new bits, and continue the assertion.
160 MVT OldVT = N->getValueType(0);
161 SDValue Op = GetPromotedInteger(N->getOperand(0));
162 return DAG.getNode(ISD::AssertSext, Op.getValueType(),
163 DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(), Op,
164 DAG.getValueType(OldVT)), N->getOperand(1));
167 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
168 // Zero the new bits, and continue the assertion.
169 MVT OldVT = N->getValueType(0);
170 SDValue Op = GetPromotedInteger(N->getOperand(0));
171 return DAG.getNode(ISD::AssertZext, Op.getValueType(),
172 DAG.getZeroExtendInReg(Op, OldVT), N->getOperand(1));
175 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
176 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
177 SDValue Res = DAG.getAtomic(N->getOpcode(), N->getChain(), N->getBasePtr(),
178 Op2, N->getSrcValue(), N->getAlignment());
179 // Legalized the chain result - switch anything that used the old chain to
181 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
185 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic2(AtomicSDNode *N) {
186 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
187 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
188 SDValue Res = DAG.getAtomic(N->getOpcode(), N->getChain(), N->getBasePtr(),
189 Op2, Op3, N->getSrcValue(), N->getAlignment());
190 // Legalized the chain result - switch anything that used the old chain to
192 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
196 SDValue DAGTypeLegalizer::PromoteIntRes_BIT_CONVERT(SDNode *N) {
197 SDValue InOp = N->getOperand(0);
198 MVT InVT = InOp.getValueType();
199 MVT NInVT = TLI.getTypeToTransformTo(InVT);
200 MVT OutVT = TLI.getTypeToTransformTo(N->getValueType(0));
202 switch (getTypeAction(InVT)) {
204 assert(false && "Unknown type action!");
209 if (OutVT.getSizeInBits() == NInVT.getSizeInBits())
210 // The input promotes to the same size. Convert the promoted value.
211 return DAG.getNode(ISD::BIT_CONVERT, OutVT, GetPromotedInteger(InOp));
214 // Promote the integer operand by hand.
215 return DAG.getNode(ISD::ANY_EXTEND, OutVT, GetSoftenedFloat(InOp));
219 case ScalarizeVector:
220 // Convert the element to an integer and promote it by hand.
221 return DAG.getNode(ISD::ANY_EXTEND, OutVT,
222 BitConvertToInteger(GetScalarizedVector(InOp)));
224 // For example, i32 = BIT_CONVERT v2i16 on alpha. Convert the split
225 // pieces of the input into integers and reassemble in the final type.
227 GetSplitVector(N->getOperand(0), Lo, Hi);
228 Lo = BitConvertToInteger(Lo);
229 Hi = BitConvertToInteger(Hi);
231 if (TLI.isBigEndian())
234 InOp = DAG.getNode(ISD::ANY_EXTEND,
235 MVT::getIntegerVT(OutVT.getSizeInBits()),
236 JoinIntegers(Lo, Hi));
237 return DAG.getNode(ISD::BIT_CONVERT, OutVT, InOp);
240 // Otherwise, lower the bit-convert to a store/load from the stack, then
242 SDValue Op = CreateStackStoreLoad(InOp, N->getValueType(0));
243 return PromoteIntRes_LOAD(cast<LoadSDNode>(Op.getNode()));
246 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
247 SDValue Op = GetPromotedInteger(N->getOperand(0));
248 MVT OVT = N->getValueType(0);
249 MVT NVT = Op.getValueType();
251 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
252 return DAG.getNode(ISD::SRL, NVT, DAG.getNode(ISD::BSWAP, NVT, Op),
253 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
256 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
257 // The pair element type may be legal, or may not promote to the same type as
258 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
259 return DAG.getNode(ISD::ANY_EXTEND,
260 TLI.getTypeToTransformTo(N->getValueType(0)),
261 JoinIntegers(N->getOperand(0), N->getOperand(1)));
264 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
265 MVT VT = N->getValueType(0);
266 // Zero extend things like i1, sign extend everything else. It shouldn't
267 // matter in theory which one we pick, but this tends to give better code?
268 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
269 SDValue Result = DAG.getNode(Opc, TLI.getTypeToTransformTo(VT),
271 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
275 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
276 SDValue Op = GetPromotedInteger(N->getOperand(0));
277 MVT OVT = N->getValueType(0);
278 MVT NVT = Op.getValueType();
279 // Zero extend to the promoted type and do the count there.
280 Op = DAG.getNode(ISD::CTLZ, NVT, DAG.getZeroExtendInReg(Op, OVT));
281 // Subtract off the extra leading bits in the bigger type.
282 return DAG.getNode(ISD::SUB, NVT, Op,
283 DAG.getConstant(NVT.getSizeInBits() -
284 OVT.getSizeInBits(), NVT));
287 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
288 SDValue Op = GetPromotedInteger(N->getOperand(0));
289 MVT OVT = N->getValueType(0);
290 MVT NVT = Op.getValueType();
291 // Zero extend to the promoted type and do the count there.
292 return DAG.getNode(ISD::CTPOP, NVT, DAG.getZeroExtendInReg(Op, OVT));
295 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
296 SDValue Op = GetPromotedInteger(N->getOperand(0));
297 MVT OVT = N->getValueType(0);
298 MVT NVT = Op.getValueType();
299 // The count is the same in the promoted type except if the original
300 // value was zero. This can be handled by setting the bit just off
301 // the top of the original type.
302 APInt TopBit(NVT.getSizeInBits(), 0);
303 TopBit.set(OVT.getSizeInBits());
304 Op = DAG.getNode(ISD::OR, NVT, Op, DAG.getConstant(TopBit, NVT));
305 return DAG.getNode(ISD::CTTZ, NVT, Op);
308 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
309 MVT OldVT = N->getValueType(0);
310 SDValue OldVec = N->getOperand(0);
311 unsigned OldElts = OldVec.getValueType().getVectorNumElements();
314 assert(!isTypeLegal(OldVec.getValueType()) &&
315 "Legal one-element vector of a type needing promotion!");
316 // It is tempting to follow GetScalarizedVector by a call to
317 // GetPromotedInteger, but this would be wrong because the
318 // scalarized value may not yet have been processed.
319 return DAG.getNode(ISD::ANY_EXTEND, TLI.getTypeToTransformTo(OldVT),
320 GetScalarizedVector(OldVec));
323 // Convert to a vector half as long with an element type of twice the width,
324 // for example <4 x i16> -> <2 x i32>.
325 assert(!(OldElts & 1) && "Odd length vectors not supported!");
326 MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
327 assert(OldVT.isSimple() && NewVT.isSimple());
329 SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT,
330 MVT::getVectorVT(NewVT, OldElts / 2),
333 // Extract the element at OldIdx / 2 from the new vector.
334 SDValue OldIdx = N->getOperand(1);
335 SDValue NewIdx = DAG.getNode(ISD::SRL, OldIdx.getValueType(), OldIdx,
336 DAG.getConstant(1, TLI.getShiftAmountTy()));
337 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, NewIdx);
339 // Select the appropriate half of the element: Lo if OldIdx was even,
342 SDValue Hi = DAG.getNode(ISD::SRL, NewVT, Elt,
343 DAG.getConstant(OldVT.getSizeInBits(),
344 TLI.getShiftAmountTy()));
345 if (TLI.isBigEndian())
348 SDValue Odd = DAG.getNode(ISD::TRUNCATE, MVT::i1, OldIdx);
349 return DAG.getNode(ISD::SELECT, NewVT, Odd, Hi, Lo);
352 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
353 unsigned NewOpc = N->getOpcode();
354 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
356 // If we're promoting a UINT to a larger size, check to see if the new node
357 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
358 // we can use that instead. This allows us to generate better code for
359 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
360 // legal, such as PowerPC.
361 if (N->getOpcode() == ISD::FP_TO_UINT) {
362 if (!TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
363 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
364 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom))
365 NewOpc = ISD::FP_TO_SINT;
368 return DAG.getNode(NewOpc, NVT, N->getOperand(0));
371 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
372 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
374 if (getTypeAction(N->getOperand(0).getValueType()) == PromoteInteger) {
375 SDValue Res = GetPromotedInteger(N->getOperand(0));
376 assert(Res.getValueType().getSizeInBits() <= NVT.getSizeInBits() &&
377 "Extension doesn't make sense!");
379 // If the result and operand types are the same after promotion, simplify
380 // to an in-register extension.
381 if (NVT == Res.getValueType()) {
382 // The high bits are not guaranteed to be anything. Insert an extend.
383 if (N->getOpcode() == ISD::SIGN_EXTEND)
384 return DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res,
385 DAG.getValueType(N->getOperand(0).getValueType()));
386 if (N->getOpcode() == ISD::ZERO_EXTEND)
387 return DAG.getZeroExtendInReg(Res, N->getOperand(0).getValueType());
388 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
393 // Otherwise, just extend the original operand all the way to the larger type.
394 return DAG.getNode(N->getOpcode(), NVT, N->getOperand(0));
397 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
398 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
399 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
400 ISD::LoadExtType ExtType =
401 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
402 SDValue Res = DAG.getExtLoad(ExtType, NVT, N->getChain(), N->getBasePtr(),
403 N->getSrcValue(), N->getSrcValueOffset(),
404 N->getMemoryVT(), N->isVolatile(),
407 // Legalized the chain result - switch anything that used the old chain to
409 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
413 SDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) {
414 // Sign extend the input.
415 SDValue LHS = GetPromotedInteger(N->getOperand(0));
416 SDValue RHS = GetPromotedInteger(N->getOperand(1));
417 MVT VT = N->getValueType(0);
418 LHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, LHS.getValueType(), LHS,
419 DAG.getValueType(VT));
420 RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, RHS.getValueType(), RHS,
421 DAG.getValueType(VT));
423 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
426 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
427 SDValue LHS = GetPromotedInteger(N->getOperand(1));
428 SDValue RHS = GetPromotedInteger(N->getOperand(2));
429 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
432 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
433 SDValue LHS = GetPromotedInteger(N->getOperand(2));
434 SDValue RHS = GetPromotedInteger(N->getOperand(3));
435 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
436 N->getOperand(1), LHS, RHS, N->getOperand(4));
439 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
440 MVT SVT = TLI.getSetCCResultType(N->getOperand(0));
441 assert(isTypeLegal(SVT) && "Illegal SetCC type!");
443 // Get the SETCC result using the canonical SETCC type.
444 SDValue SetCC = DAG.getNode(ISD::SETCC, SVT, N->getOperand(0),
445 N->getOperand(1), N->getOperand(2));
447 // Convert to the expected type.
448 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
449 assert(NVT.getSizeInBits() <= SVT.getSizeInBits() &&
450 "Integer type overpromoted?");
451 return DAG.getNode(ISD::TRUNCATE, NVT, SetCC);
454 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
455 return DAG.getNode(ISD::SHL, TLI.getTypeToTransformTo(N->getValueType(0)),
456 GetPromotedInteger(N->getOperand(0)), N->getOperand(1));
459 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
460 SDValue Op = GetPromotedInteger(N->getOperand(0));
461 return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(), Op,
465 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
466 // The input may have strange things in the top bits of the registers, but
467 // these operations don't care. They may have weird bits going out, but
468 // that too is okay if they are integer operations.
469 SDValue LHS = GetPromotedInteger(N->getOperand(0));
470 SDValue RHS = GetPromotedInteger(N->getOperand(1));
471 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
474 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
475 // The input value must be properly sign extended.
476 MVT VT = N->getValueType(0);
477 MVT NVT = TLI.getTypeToTransformTo(VT);
478 SDValue Res = GetPromotedInteger(N->getOperand(0));
479 Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res, DAG.getValueType(VT));
480 return DAG.getNode(ISD::SRA, NVT, Res, N->getOperand(1));
483 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
484 // The input value must be properly zero extended.
485 MVT VT = N->getValueType(0);
486 MVT NVT = TLI.getTypeToTransformTo(VT);
487 SDValue Res = ZExtPromotedInteger(N->getOperand(0));
488 return DAG.getNode(ISD::SRL, NVT, Res, N->getOperand(1));
491 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
494 switch (getTypeAction(N->getOperand(0).getValueType())) {
495 default: assert(0 && "Unknown type action!");
498 Res = N->getOperand(0);
501 Res = GetPromotedInteger(N->getOperand(0));
505 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
506 assert(Res.getValueType().getSizeInBits() >= NVT.getSizeInBits() &&
507 "Truncation doesn't make sense!");
508 if (Res.getValueType() == NVT)
511 // Truncate to NVT instead of VT
512 return DAG.getNode(ISD::TRUNCATE, NVT, Res);
515 SDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) {
516 // Zero extend the input.
517 SDValue LHS = GetPromotedInteger(N->getOperand(0));
518 SDValue RHS = GetPromotedInteger(N->getOperand(1));
519 MVT VT = N->getValueType(0);
520 LHS = DAG.getZeroExtendInReg(LHS, VT);
521 RHS = DAG.getZeroExtendInReg(RHS, VT);
523 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
526 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
527 return DAG.getNode(ISD::UNDEF, TLI.getTypeToTransformTo(N->getValueType(0)));
530 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
531 SDValue Chain = N->getOperand(0); // Get the chain.
532 SDValue Ptr = N->getOperand(1); // Get the pointer.
533 MVT VT = N->getValueType(0);
535 MVT RegVT = TLI.getRegisterType(VT);
536 unsigned NumRegs = TLI.getNumRegisters(VT);
537 // The argument is passed as NumRegs registers of type RegVT.
539 SmallVector<SDValue, 8> Parts(NumRegs);
540 for (unsigned i = 0; i < NumRegs; ++i) {
541 Parts[i] = DAG.getVAArg(RegVT, Chain, Ptr, N->getOperand(2));
542 Chain = Parts[i].getValue(1);
545 // Handle endianness of the load.
546 if (TLI.isBigEndian())
547 std::reverse(Parts.begin(), Parts.end());
549 // Assemble the parts in the promoted type.
550 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
551 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, NVT, Parts[0]);
552 for (unsigned i = 1; i < NumRegs; ++i) {
553 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, NVT, Parts[i]);
554 // Shift it to the right position and "or" it in.
555 Part = DAG.getNode(ISD::SHL, NVT, Part,
556 DAG.getConstant(i * RegVT.getSizeInBits(),
557 TLI.getShiftAmountTy()));
558 Res = DAG.getNode(ISD::OR, NVT, Res, Part);
561 // Modified the chain result - switch anything that used the old chain to
563 ReplaceValueWith(SDValue(N, 1), Chain);
569 //===----------------------------------------------------------------------===//
570 // Integer Operand Promotion
571 //===----------------------------------------------------------------------===//
573 /// PromoteIntegerOperand - This method is called when the specified operand of
574 /// the specified node is found to need promotion. At this point, all of the
575 /// result types of the node are known to be legal, but other operands of the
576 /// node may need promotion or expansion as well as the specified one.
577 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
578 DEBUG(cerr << "Promote integer operand: "; N->dump(&DAG); cerr << "\n");
579 SDValue Res = SDValue();
581 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
582 == TargetLowering::Custom)
583 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
585 if (Res.getNode() == 0) {
586 switch (N->getOpcode()) {
589 cerr << "PromoteIntegerOperand Op #" << OpNo << ": ";
590 N->dump(&DAG); cerr << "\n";
592 assert(0 && "Do not know how to promote this operator's operand!");
595 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
596 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
597 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
598 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
599 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
600 case ISD::FP_EXTEND: Res = PromoteIntOp_FP_EXTEND(N); break;
601 case ISD::FP_ROUND: Res = PromoteIntOp_FP_ROUND(N); break;
602 case ISD::INSERT_VECTOR_ELT:
603 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
604 case ISD::MEMBARRIER: Res = PromoteIntOp_MEMBARRIER(N); break;
605 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
606 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
607 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
608 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
609 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
611 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
612 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
614 case ISD::SINT_TO_FP:
615 case ISD::UINT_TO_FP: Res = PromoteIntOp_INT_TO_FP(N); break;
619 // If the result is null, the sub-method took care of registering results etc.
620 if (!Res.getNode()) return false;
621 // If the result is N, the sub-method updated N in place.
622 if (Res.getNode() == N) {
623 // Mark N as new and remark N and its operands. This allows us to correctly
624 // revisit N if it needs another step of promotion and allows us to visit
625 // any new operands to N.
630 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
631 "Invalid operand expansion");
633 ReplaceValueWith(SDValue(N, 0), Res);
637 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
638 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
639 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
640 ISD::CondCode CCCode) {
641 MVT VT = NewLHS.getValueType();
643 // Get the promoted values.
644 NewLHS = GetPromotedInteger(NewLHS);
645 NewRHS = GetPromotedInteger(NewRHS);
647 // Otherwise, we have to insert explicit sign or zero extends. Note
648 // that we could insert sign extends for ALL conditions, but zero extend
649 // is cheaper on many machines (an AND instead of two shifts), so prefer
652 default: assert(0 && "Unknown integer comparison!");
659 // ALL of these operations will work if we either sign or zero extend
660 // the operands (including the unsigned comparisons!). Zero extend is
661 // usually a simpler/cheaper operation, so prefer it.
662 NewLHS = DAG.getZeroExtendInReg(NewLHS, VT);
663 NewRHS = DAG.getZeroExtendInReg(NewRHS, VT);
669 NewLHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewLHS.getValueType(), NewLHS,
670 DAG.getValueType(VT));
671 NewRHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewRHS.getValueType(), NewRHS,
672 DAG.getValueType(VT));
677 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
678 SDValue Op = GetPromotedInteger(N->getOperand(0));
679 return DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
682 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
683 assert(OpNo == 2 && "Don't know how to promote this operand!");
685 SDValue LHS = N->getOperand(2);
686 SDValue RHS = N->getOperand(3);
687 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
689 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
691 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
692 N->getOperand(1), LHS, RHS, N->getOperand(4));
695 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
696 assert(OpNo == 1 && "only know how to promote condition");
697 SDValue Cond = GetPromotedInteger(N->getOperand(1)); // Promote condition.
699 // Make sure the extra bits coming from type promotion conform to
700 // getSetCCResultContents.
701 unsigned CondBits = Cond.getValueSizeInBits();
702 switch (TLI.getSetCCResultContents()) {
704 assert(false && "Unknown SetCCResultValue!");
705 case TargetLowering::UndefinedSetCCResult:
706 // The promoted value, which may contain rubbish in the upper bits, is fine.
708 case TargetLowering::ZeroOrOneSetCCResult:
709 if (!DAG.MaskedValueIsZero(Cond,APInt::getHighBitsSet(CondBits,CondBits-1)))
710 Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
712 case TargetLowering::ZeroOrNegativeOneSetCCResult:
713 if (DAG.ComputeNumSignBits(Cond) != CondBits)
714 Cond = DAG.getNode(ISD::SIGN_EXTEND_INREG, Cond.getValueType(), Cond,
715 DAG.getValueType(MVT::i1));
719 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
720 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Cond,
724 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
725 // Since the result type is legal, the operands must promote to it.
726 MVT OVT = N->getOperand(0).getValueType();
727 SDValue Lo = GetPromotedInteger(N->getOperand(0));
728 SDValue Hi = GetPromotedInteger(N->getOperand(1));
729 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
731 Lo = DAG.getZeroExtendInReg(Lo, OVT);
732 Hi = DAG.getNode(ISD::SHL, N->getValueType(0), Hi,
733 DAG.getConstant(OVT.getSizeInBits(),
734 TLI.getShiftAmountTy()));
735 return DAG.getNode(ISD::OR, N->getValueType(0), Lo, Hi);
738 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
739 // The vector type is legal but the element type is not. This implies
740 // that the vector is a power-of-two in length and that the element
741 // type does not have a strange size (eg: it is not i1).
742 MVT VecVT = N->getValueType(0);
743 unsigned NumElts = VecVT.getVectorNumElements();
744 assert(!(NumElts & 1) && "Legal vector of one illegal element?");
746 // Build a vector of half the length out of elements of twice the bitwidth.
747 // For example <4 x i16> -> <2 x i32>.
748 MVT OldVT = N->getOperand(0).getValueType();
749 MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
750 assert(OldVT.isSimple() && NewVT.isSimple());
752 std::vector<SDValue> NewElts;
753 NewElts.reserve(NumElts/2);
755 for (unsigned i = 0; i < NumElts; i += 2) {
756 // Combine two successive elements into one promoted element.
757 SDValue Lo = N->getOperand(i);
758 SDValue Hi = N->getOperand(i+1);
759 if (TLI.isBigEndian())
761 NewElts.push_back(JoinIntegers(Lo, Hi));
764 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR,
765 MVT::getVectorVT(NewVT, NewElts.size()),
766 &NewElts[0], NewElts.size());
768 // Convert the new vector to the old vector type.
769 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
772 SDValue DAGTypeLegalizer::PromoteIntOp_FP_EXTEND(SDNode *N) {
773 SDValue Op = GetPromotedInteger(N->getOperand(0));
774 return DAG.getNode(ISD::FP_EXTEND, N->getValueType(0), Op);
777 SDValue DAGTypeLegalizer::PromoteIntOp_FP_ROUND(SDNode *N) {
778 SDValue Op = GetPromotedInteger(N->getOperand(0));
779 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Op,
780 DAG.getIntPtrConstant(0));
783 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
786 // Promote the inserted value. This is valid because the type does not
787 // have to match the vector element type.
789 // Check that any extra bits introduced will be truncated away.
790 assert(N->getOperand(1).getValueType().getSizeInBits() >=
791 N->getValueType(0).getVectorElementType().getSizeInBits() &&
792 "Type of inserted value narrower than vector element type!");
793 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
794 GetPromotedInteger(N->getOperand(1)),
798 assert(OpNo == 2 && "Different operand and result vector types?");
800 // Promote the index.
801 SDValue Idx = N->getOperand(2);
802 Idx = DAG.getZeroExtendInReg(GetPromotedInteger(Idx), Idx.getValueType());
803 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
804 N->getOperand(1), Idx);
807 SDValue DAGTypeLegalizer::PromoteIntOp_INT_TO_FP(SDNode *N) {
808 SDValue In = GetPromotedInteger(N->getOperand(0));
809 MVT OpVT = N->getOperand(0).getValueType();
810 if (N->getOpcode() == ISD::UINT_TO_FP)
811 In = DAG.getZeroExtendInReg(In, OpVT);
813 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(),
814 In, DAG.getValueType(OpVT));
816 return DAG.UpdateNodeOperands(SDValue(N, 0), In);
819 SDValue DAGTypeLegalizer::PromoteIntOp_MEMBARRIER(SDNode *N) {
821 NewOps[0] = N->getOperand(0);
822 for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
823 SDValue Flag = GetPromotedInteger(N->getOperand(i));
824 NewOps[i] = DAG.getZeroExtendInReg(Flag, MVT::i1);
826 return DAG.UpdateNodeOperands(SDValue (N, 0), NewOps,
827 array_lengthof(NewOps));
830 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
831 assert(OpNo == 0 && "Only know how to promote condition");
832 SDValue Cond = GetPromotedInteger(N->getOperand(0));
834 // Promote all the way up to SVT, the canonical SetCC type.
835 // FIXME: Not clear what value to pass to getSetCCResultType.
836 // [This only matters for CellSPU since all other targets
837 // ignore the argument.] We used to pass Cond, resulting in
838 // SVT = MVT::i8, but CellSPU has no select patterns for i8,
839 // causing an abort later. Passing the result type works
840 // around the problem.
841 MVT SVT = TLI.getSetCCResultType(N->getOperand(1));
842 assert(isTypeLegal(SVT) && "Illegal SetCC type!");
843 assert(Cond.getValueSizeInBits() <= SVT.getSizeInBits() &&
844 "Unexpected SetCC type!");
846 // Make sure the extra bits conform to getSetCCResultContents. There are
847 // two sets of extra bits: those in Cond, which come from type promotion,
848 // and those we need to add to have the final type be SVT (for most targets
849 // this last set of bits is empty).
850 unsigned CondBits = Cond.getValueSizeInBits();
851 ISD::NodeType ExtendCode;
852 switch (TLI.getSetCCResultContents()) {
854 assert(false && "Unknown SetCCResultValue!");
855 case TargetLowering::UndefinedSetCCResult:
856 // Extend to SVT by adding rubbish.
857 ExtendCode = ISD::ANY_EXTEND;
859 case TargetLowering::ZeroOrOneSetCCResult:
860 ExtendCode = ISD::ZERO_EXTEND;
861 if (!DAG.MaskedValueIsZero(Cond,APInt::getHighBitsSet(CondBits,CondBits-1)))
862 // All extra bits need to be cleared. Do this by zero extending the
863 // original condition value all the way to SVT.
864 Cond = N->getOperand(0);
866 case TargetLowering::ZeroOrNegativeOneSetCCResult: {
867 ExtendCode = ISD::SIGN_EXTEND;
868 unsigned SignBits = DAG.ComputeNumSignBits(Cond);
869 if (SignBits != CondBits)
870 // All extra bits need to be sign extended. Do this by sign extending the
871 // original condition value all the way to SVT.
872 Cond = N->getOperand(0);
876 Cond = DAG.getNode(ExtendCode, SVT, Cond);
878 return DAG.UpdateNodeOperands(SDValue(N, 0), Cond,
879 N->getOperand(1), N->getOperand(2));
882 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(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(4))->get());
889 // The CC (#4) and the possible return values (#2 and #3) have legal types.
890 return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2),
891 N->getOperand(3), N->getOperand(4));
894 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
895 assert(OpNo == 0 && "Don't know how to promote this operand!");
897 SDValue LHS = N->getOperand(0);
898 SDValue RHS = N->getOperand(1);
899 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
901 // The CC (#2) is always legal.
902 return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2));
905 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
906 SDValue Op = GetPromotedInteger(N->getOperand(0));
907 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
908 return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(),
909 Op, DAG.getValueType(N->getOperand(0).getValueType()));
912 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
913 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
914 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
915 int SVOffset = N->getSrcValueOffset();
916 unsigned Alignment = N->getAlignment();
917 bool isVolatile = N->isVolatile();
919 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
921 assert(!N->isTruncatingStore() && "Cannot promote this store operand!");
923 // Truncate the value and store the result.
924 return DAG.getTruncStore(Ch, Val, Ptr, N->getSrcValue(),
925 SVOffset, N->getMemoryVT(),
926 isVolatile, Alignment);
929 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
930 SDValue Op = GetPromotedInteger(N->getOperand(0));
931 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), Op);
934 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
935 SDValue Op = GetPromotedInteger(N->getOperand(0));
936 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
937 return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
941 //===----------------------------------------------------------------------===//
942 // Integer Result Expansion
943 //===----------------------------------------------------------------------===//
945 /// ExpandIntegerResult - This method is called when the specified result of the
946 /// specified node is found to need expansion. At this point, the node may also
947 /// have invalid operands or may have other results that need promotion, we just
948 /// know that (at least) one result needs expansion.
949 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
950 DEBUG(cerr << "Expand integer result: "; N->dump(&DAG); cerr << "\n");
954 // See if the target wants to custom expand this node.
955 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
956 TargetLowering::Custom) {
957 // If the target wants to, allow it to lower this itself.
958 if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
959 // Everything that once used N now uses P. We are guaranteed that the
960 // result value types of N and the result value types of P match.
961 ReplaceNodeWith(N, P);
966 switch (N->getOpcode()) {
969 cerr << "ExpandIntegerResult #" << ResNo << ": ";
970 N->dump(&DAG); cerr << "\n";
972 assert(0 && "Do not know how to expand the result of this operator!");
975 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
976 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
977 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
978 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
980 case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
981 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
982 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
983 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
984 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
986 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
987 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
988 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
989 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
990 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
991 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
992 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
993 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
994 case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
995 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
996 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
997 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
998 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
999 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
1000 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
1001 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
1002 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
1003 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
1004 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
1005 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
1009 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
1012 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
1015 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
1018 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
1022 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
1025 // If Lo/Hi is null, the sub-method took care of registering results etc.
1027 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
1030 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
1031 /// and the shift amount is a constant 'Amt'. Expand the operation.
1032 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
1033 SDValue &Lo, SDValue &Hi) {
1034 // Expand the incoming operand to be shifted, so that we have its parts
1036 GetExpandedInteger(N->getOperand(0), InL, InH);
1038 MVT NVT = InL.getValueType();
1039 unsigned VTBits = N->getValueType(0).getSizeInBits();
1040 unsigned NVTBits = NVT.getSizeInBits();
1041 MVT ShTy = N->getOperand(1).getValueType();
1043 if (N->getOpcode() == ISD::SHL) {
1045 Lo = Hi = DAG.getConstant(0, NVT);
1046 } else if (Amt > NVTBits) {
1047 Lo = DAG.getConstant(0, NVT);
1048 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
1049 } else if (Amt == NVTBits) {
1050 Lo = DAG.getConstant(0, NVT);
1052 } else if (Amt == 1) {
1053 // Emit this X << 1 as X+X.
1054 SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1055 SDValue LoOps[2] = { InL, InL };
1056 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1057 SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
1058 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1060 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt, ShTy));
1061 Hi = DAG.getNode(ISD::OR, NVT,
1062 DAG.getNode(ISD::SHL, NVT, InH,
1063 DAG.getConstant(Amt, ShTy)),
1064 DAG.getNode(ISD::SRL, NVT, InL,
1065 DAG.getConstant(NVTBits-Amt, ShTy)));
1070 if (N->getOpcode() == ISD::SRL) {
1072 Lo = DAG.getConstant(0, NVT);
1073 Hi = DAG.getConstant(0, NVT);
1074 } else if (Amt > NVTBits) {
1075 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
1076 Hi = DAG.getConstant(0, NVT);
1077 } else if (Amt == NVTBits) {
1079 Hi = DAG.getConstant(0, NVT);
1081 Lo = DAG.getNode(ISD::OR, NVT,
1082 DAG.getNode(ISD::SRL, NVT, InL,
1083 DAG.getConstant(Amt, ShTy)),
1084 DAG.getNode(ISD::SHL, NVT, InH,
1085 DAG.getConstant(NVTBits-Amt, ShTy)));
1086 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt, ShTy));
1091 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1093 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1094 DAG.getConstant(NVTBits-1, ShTy));
1095 } else if (Amt > NVTBits) {
1096 Lo = DAG.getNode(ISD::SRA, NVT, InH,
1097 DAG.getConstant(Amt-NVTBits, ShTy));
1098 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1099 DAG.getConstant(NVTBits-1, ShTy));
1100 } else if (Amt == NVTBits) {
1102 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1103 DAG.getConstant(NVTBits-1, ShTy));
1105 Lo = DAG.getNode(ISD::OR, NVT,
1106 DAG.getNode(ISD::SRL, NVT, InL,
1107 DAG.getConstant(Amt, ShTy)),
1108 DAG.getNode(ISD::SHL, NVT, InH,
1109 DAG.getConstant(NVTBits-Amt, ShTy)));
1110 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Amt, ShTy));
1114 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1115 /// this shift based on knowledge of the high bit of the shift amount. If we
1116 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1118 bool DAGTypeLegalizer::
1119 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1120 SDValue Amt = N->getOperand(1);
1121 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1122 MVT ShTy = Amt.getValueType();
1123 unsigned ShBits = ShTy.getSizeInBits();
1124 unsigned NVTBits = NVT.getSizeInBits();
1125 assert(isPowerOf2_32(NVTBits) &&
1126 "Expanded integer type size not a power of two!");
1128 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
1129 APInt KnownZero, KnownOne;
1130 DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
1132 // If we don't know anything about the high bits, exit.
1133 if (((KnownZero|KnownOne) & HighBitMask) == 0)
1136 // Get the incoming operand to be shifted.
1138 GetExpandedInteger(N->getOperand(0), InL, InH);
1140 // If we know that any of the high bits of the shift amount are one, then we
1141 // can do this as a couple of simple shifts.
1142 if (KnownOne.intersects(HighBitMask)) {
1143 // Mask out the high bit, which we know is set.
1144 Amt = DAG.getNode(ISD::AND, ShTy, Amt,
1145 DAG.getConstant(~HighBitMask, ShTy));
1147 switch (N->getOpcode()) {
1148 default: assert(0 && "Unknown shift");
1150 Lo = DAG.getConstant(0, NVT); // Low part is zero.
1151 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
1154 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
1155 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
1158 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
1159 DAG.getConstant(NVTBits-1, ShTy));
1160 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
1165 // If we know that all of the high bits of the shift amount are zero, then we
1166 // can do this as a couple of simple shifts.
1167 if ((KnownZero & HighBitMask) == HighBitMask) {
1169 SDValue Amt2 = DAG.getNode(ISD::SUB, ShTy,
1170 DAG.getConstant(NVTBits, ShTy),
1173 switch (N->getOpcode()) {
1174 default: assert(0 && "Unknown shift");
1175 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1177 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1180 Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
1181 Hi = DAG.getNode(ISD::OR, NVT,
1182 DAG.getNode(Op1, NVT, InH, Amt),
1183 DAG.getNode(Op2, NVT, InL, Amt2));
1190 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
1191 SDValue &Lo, SDValue &Hi) {
1192 // Expand the subcomponents.
1193 SDValue LHSL, LHSH, RHSL, RHSH;
1194 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1195 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1197 MVT NVT = LHSL.getValueType();
1198 SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1199 SDValue LoOps[2] = { LHSL, RHSL };
1200 SDValue HiOps[3] = { LHSH, RHSH };
1202 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
1203 // them. TODO: Teach operation legalization how to expand unsupported
1204 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
1205 // a carry of type MVT::Flag, but there doesn't seem to be any way to
1206 // generate a value of this type in the expanded code sequence.
1208 TLI.isOperationLegal(N->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC,
1209 TLI.getTypeToExpandTo(NVT));
1212 if (N->getOpcode() == ISD::ADD) {
1213 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1214 HiOps[2] = Lo.getValue(1);
1215 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1217 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1218 HiOps[2] = Lo.getValue(1);
1219 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1222 if (N->getOpcode() == ISD::ADD) {
1223 Lo = DAG.getNode(ISD::ADD, VTList, LoOps, 2);
1224 Hi = DAG.getNode(ISD::ADD, VTList, HiOps, 2);
1225 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo), Lo, LoOps[0],
1227 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
1228 DAG.getConstant(1, NVT),
1229 DAG.getConstant(0, NVT));
1230 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo), Lo, LoOps[1],
1232 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
1233 DAG.getConstant(1, NVT), Carry1);
1234 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
1236 Lo = DAG.getNode(ISD::SUB, VTList, LoOps, 2);
1237 Hi = DAG.getNode(ISD::SUB, VTList, HiOps, 2);
1238 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
1239 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
1240 DAG.getConstant(1, NVT),
1241 DAG.getConstant(0, NVT));
1242 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
1247 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
1248 SDValue &Lo, SDValue &Hi) {
1249 // Expand the subcomponents.
1250 SDValue LHSL, LHSH, RHSL, RHSH;
1251 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1252 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1253 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1254 SDValue LoOps[2] = { LHSL, RHSL };
1255 SDValue HiOps[3] = { LHSH, RHSH };
1257 if (N->getOpcode() == ISD::ADDC) {
1258 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1259 HiOps[2] = Lo.getValue(1);
1260 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1262 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1263 HiOps[2] = Lo.getValue(1);
1264 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1267 // Legalized the flag result - switch anything that used the old flag to
1269 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1272 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
1273 SDValue &Lo, SDValue &Hi) {
1274 // Expand the subcomponents.
1275 SDValue LHSL, LHSH, RHSL, RHSH;
1276 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1277 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1278 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1279 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
1280 SDValue HiOps[3] = { LHSH, RHSH };
1282 Lo = DAG.getNode(N->getOpcode(), VTList, LoOps, 3);
1283 HiOps[2] = Lo.getValue(1);
1284 Hi = DAG.getNode(N->getOpcode(), VTList, HiOps, 3);
1286 // Legalized the flag result - switch anything that used the old flag to
1288 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1291 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
1292 SDValue &Lo, SDValue &Hi) {
1293 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1294 SDValue Op = N->getOperand(0);
1295 if (Op.getValueType().bitsLE(NVT)) {
1296 // The low part is any extension of the input (which degenerates to a copy).
1297 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Op);
1298 Hi = DAG.getNode(ISD::UNDEF, NVT); // The high part is undefined.
1300 // For example, extension of an i48 to an i64. The operand type necessarily
1301 // promotes to the result type, so will end up being expanded too.
1302 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1303 "Only know how to promote this result!");
1304 SDValue Res = GetPromotedInteger(Op);
1305 assert(Res.getValueType() == N->getValueType(0) &&
1306 "Operand over promoted?");
1307 // Split the promoted operand. This will simplify when it is expanded.
1308 SplitInteger(Res, Lo, Hi);
1312 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
1313 SDValue &Lo, SDValue &Hi) {
1314 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1315 MVT NVT = Lo.getValueType();
1316 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1317 unsigned NVTBits = NVT.getSizeInBits();
1318 unsigned EVTBits = EVT.getSizeInBits();
1320 if (NVTBits < EVTBits) {
1321 Hi = DAG.getNode(ISD::AssertSext, NVT, Hi,
1322 DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1324 Lo = DAG.getNode(ISD::AssertSext, NVT, Lo, DAG.getValueType(EVT));
1325 // The high part replicates the sign bit of Lo, make it explicit.
1326 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1327 DAG.getConstant(NVTBits-1, TLI.getShiftAmountTy()));
1331 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
1332 SDValue &Lo, SDValue &Hi) {
1333 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1334 MVT NVT = Lo.getValueType();
1335 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1336 unsigned NVTBits = NVT.getSizeInBits();
1337 unsigned EVTBits = EVT.getSizeInBits();
1339 if (NVTBits < EVTBits) {
1340 Hi = DAG.getNode(ISD::AssertZext, NVT, Hi,
1341 DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1343 Lo = DAG.getNode(ISD::AssertZext, NVT, Lo, DAG.getValueType(EVT));
1344 // The high part must be zero, make it explicit.
1345 Hi = DAG.getConstant(0, NVT);
1349 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
1350 SDValue &Lo, SDValue &Hi) {
1351 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
1352 Lo = DAG.getNode(ISD::BSWAP, Lo.getValueType(), Lo);
1353 Hi = DAG.getNode(ISD::BSWAP, Hi.getValueType(), Hi);
1356 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
1357 SDValue &Lo, SDValue &Hi) {
1358 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1359 unsigned NBitWidth = NVT.getSizeInBits();
1360 const APInt &Cst = cast<ConstantSDNode>(N)->getAPIntValue();
1361 Lo = DAG.getConstant(APInt(Cst).trunc(NBitWidth), NVT);
1362 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
1365 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
1366 SDValue &Lo, SDValue &Hi) {
1367 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
1368 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1369 MVT NVT = Lo.getValueType();
1371 SDValue HiNotZero = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
1372 DAG.getConstant(0, NVT), ISD::SETNE);
1374 SDValue LoLZ = DAG.getNode(ISD::CTLZ, NVT, Lo);
1375 SDValue HiLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
1377 Lo = DAG.getNode(ISD::SELECT, NVT, HiNotZero, HiLZ,
1378 DAG.getNode(ISD::ADD, NVT, LoLZ,
1379 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1380 Hi = DAG.getConstant(0, NVT);
1383 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
1384 SDValue &Lo, SDValue &Hi) {
1385 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
1386 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1387 MVT NVT = Lo.getValueType();
1388 Lo = DAG.getNode(ISD::ADD, NVT, DAG.getNode(ISD::CTPOP, NVT, Lo),
1389 DAG.getNode(ISD::CTPOP, NVT, Hi));
1390 Hi = DAG.getConstant(0, NVT);
1393 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
1394 SDValue &Lo, SDValue &Hi) {
1395 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
1396 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1397 MVT NVT = Lo.getValueType();
1399 SDValue LoNotZero = DAG.getSetCC(TLI.getSetCCResultType(Lo), Lo,
1400 DAG.getConstant(0, NVT), ISD::SETNE);
1402 SDValue LoLZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
1403 SDValue HiLZ = DAG.getNode(ISD::CTTZ, NVT, Hi);
1405 Lo = DAG.getNode(ISD::SELECT, NVT, LoNotZero, LoLZ,
1406 DAG.getNode(ISD::ADD, NVT, HiLZ,
1407 DAG.getConstant(NVT.getSizeInBits(), NVT)));
1408 Hi = DAG.getConstant(0, NVT);
1411 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
1413 MVT VT = N->getValueType(0);
1414 SDValue Op = N->getOperand(0);
1415 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
1416 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
1417 SplitInteger(MakeLibCall(LC, VT, &Op, 1, true/*sign irrelevant*/), Lo, Hi);
1420 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
1422 MVT VT = N->getValueType(0);
1423 SDValue Op = N->getOperand(0);
1424 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
1425 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
1426 SplitInteger(MakeLibCall(LC, VT, &Op, 1, false/*sign irrelevant*/), Lo, Hi);
1429 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
1430 SDValue &Lo, SDValue &Hi) {
1431 if (ISD::isNormalLoad(N)) {
1432 ExpandRes_NormalLoad(N, Lo, Hi);
1436 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1438 MVT VT = N->getValueType(0);
1439 MVT NVT = TLI.getTypeToTransformTo(VT);
1440 SDValue Ch = N->getChain();
1441 SDValue Ptr = N->getBasePtr();
1442 ISD::LoadExtType ExtType = N->getExtensionType();
1443 int SVOffset = N->getSrcValueOffset();
1444 unsigned Alignment = N->getAlignment();
1445 bool isVolatile = N->isVolatile();
1447 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1449 if (N->getMemoryVT().bitsLE(NVT)) {
1450 MVT EVT = N->getMemoryVT();
1452 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset, EVT,
1453 isVolatile, Alignment);
1455 // Remember the chain.
1456 Ch = Lo.getValue(1);
1458 if (ExtType == ISD::SEXTLOAD) {
1459 // The high part is obtained by SRA'ing all but one of the bits of the
1461 unsigned LoSize = Lo.getValueType().getSizeInBits();
1462 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1463 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
1464 } else if (ExtType == ISD::ZEXTLOAD) {
1465 // The high part is just a zero.
1466 Hi = DAG.getConstant(0, NVT);
1468 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
1469 // The high part is undefined.
1470 Hi = DAG.getNode(ISD::UNDEF, NVT);
1472 } else if (TLI.isLittleEndian()) {
1473 // Little-endian - low bits are at low addresses.
1474 Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1475 isVolatile, Alignment);
1477 unsigned ExcessBits =
1478 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
1479 MVT NEVT = MVT::getIntegerVT(ExcessBits);
1481 // Increment the pointer to the other half.
1482 unsigned IncrementSize = NVT.getSizeInBits()/8;
1483 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1484 DAG.getIntPtrConstant(IncrementSize));
1485 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(),
1486 SVOffset+IncrementSize, NEVT,
1487 isVolatile, MinAlign(Alignment, IncrementSize));
1489 // Build a factor node to remember that this load is independent of the
1491 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1494 // Big-endian - high bits are at low addresses. Favor aligned loads at
1495 // the cost of some bit-fiddling.
1496 MVT EVT = N->getMemoryVT();
1497 unsigned EBytes = EVT.getStoreSizeInBits()/8;
1498 unsigned IncrementSize = NVT.getSizeInBits()/8;
1499 unsigned ExcessBits = (EBytes - IncrementSize)*8;
1501 // Load both the high bits and maybe some of the low bits.
1502 Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1503 MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits),
1504 isVolatile, Alignment);
1506 // Increment the pointer to the other half.
1507 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1508 DAG.getIntPtrConstant(IncrementSize));
1509 // Load the rest of the low bits.
1510 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Ch, Ptr, N->getSrcValue(),
1511 SVOffset+IncrementSize,
1512 MVT::getIntegerVT(ExcessBits),
1513 isVolatile, MinAlign(Alignment, IncrementSize));
1515 // Build a factor node to remember that this load is independent of the
1517 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1520 if (ExcessBits < NVT.getSizeInBits()) {
1521 // Transfer low bits from the bottom of Hi to the top of Lo.
1522 Lo = DAG.getNode(ISD::OR, NVT, Lo,
1523 DAG.getNode(ISD::SHL, NVT, Hi,
1524 DAG.getConstant(ExcessBits,
1525 TLI.getShiftAmountTy())));
1526 // Move high bits to the right position in Hi.
1527 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, NVT, Hi,
1528 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
1529 TLI.getShiftAmountTy()));
1533 // Legalized the chain result - switch anything that used the old chain to
1535 ReplaceValueWith(SDValue(N, 1), Ch);
1538 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
1539 SDValue &Lo, SDValue &Hi) {
1540 SDValue LL, LH, RL, RH;
1541 GetExpandedInteger(N->getOperand(0), LL, LH);
1542 GetExpandedInteger(N->getOperand(1), RL, RH);
1543 Lo = DAG.getNode(N->getOpcode(), LL.getValueType(), LL, RL);
1544 Hi = DAG.getNode(N->getOpcode(), LL.getValueType(), LH, RH);
1547 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
1548 SDValue &Lo, SDValue &Hi) {
1549 MVT VT = N->getValueType(0);
1550 MVT NVT = TLI.getTypeToTransformTo(VT);
1552 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
1553 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
1554 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
1555 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
1556 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1557 SDValue LL, LH, RL, RH;
1558 GetExpandedInteger(N->getOperand(0), LL, LH);
1559 GetExpandedInteger(N->getOperand(1), RL, RH);
1560 unsigned OuterBitSize = VT.getSizeInBits();
1561 unsigned InnerBitSize = NVT.getSizeInBits();
1562 unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1563 unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
1565 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
1566 if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
1567 DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
1568 // The inputs are both zero-extended.
1570 // We can emit a umul_lohi.
1571 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1572 Hi = SDValue(Lo.getNode(), 1);
1576 // We can emit a mulhu+mul.
1577 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1578 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1582 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
1583 // The input values are both sign-extended.
1585 // We can emit a smul_lohi.
1586 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1587 Hi = SDValue(Lo.getNode(), 1);
1591 // We can emit a mulhs+mul.
1592 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1593 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
1598 // Lo,Hi = umul LHS, RHS.
1599 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
1600 DAG.getVTList(NVT, NVT), LL, RL);
1602 Hi = UMulLOHI.getValue(1);
1603 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1604 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1605 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1606 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1610 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1611 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1612 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1613 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1614 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1615 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1620 // If nothing else, we can make a libcall.
1621 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1623 LC = RTLIB::MUL_I32;
1624 else if (VT == MVT::i64)
1625 LC = RTLIB::MUL_I64;
1626 else if (VT == MVT::i128)
1627 LC = RTLIB::MUL_I128;
1628 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
1630 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1631 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true/*sign irrelevant*/), Lo, Hi);
1634 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
1635 SDValue &Lo, SDValue &Hi) {
1636 MVT VT = N->getValueType(0);
1638 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1640 LC = RTLIB::SDIV_I32;
1641 else if (VT == MVT::i64)
1642 LC = RTLIB::SDIV_I64;
1643 else if (VT == MVT::i128)
1644 LC = RTLIB::SDIV_I128;
1645 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1647 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1648 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true), Lo, Hi);
1651 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
1652 SDValue &Lo, SDValue &Hi) {
1653 MVT VT = N->getValueType(0);
1655 // If we can emit an efficient shift operation, do so now. Check to see if
1656 // the RHS is a constant.
1657 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1658 return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi);
1660 // If we can determine that the high bit of the shift is zero or one, even if
1661 // the low bits are variable, emit this shift in an optimized form.
1662 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
1665 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
1667 if (N->getOpcode() == ISD::SHL) {
1668 PartsOpc = ISD::SHL_PARTS;
1669 } else if (N->getOpcode() == ISD::SRL) {
1670 PartsOpc = ISD::SRL_PARTS;
1672 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1673 PartsOpc = ISD::SRA_PARTS;
1676 // Next check to see if the target supports this SHL_PARTS operation or if it
1677 // will custom expand it.
1678 MVT NVT = TLI.getTypeToTransformTo(VT);
1679 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
1680 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
1681 Action == TargetLowering::Custom) {
1682 // Expand the subcomponents.
1684 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1686 SDValue Ops[] = { LHSL, LHSH, N->getOperand(1) };
1687 MVT VT = LHSL.getValueType();
1688 Lo = DAG.getNode(PartsOpc, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
1689 Hi = Lo.getValue(1);
1693 // Otherwise, emit a libcall.
1694 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1696 if (N->getOpcode() == ISD::SHL) {
1697 isSigned = false; /*sign irrelevant*/
1699 LC = RTLIB::SHL_I32;
1700 else if (VT == MVT::i64)
1701 LC = RTLIB::SHL_I64;
1702 else if (VT == MVT::i128)
1703 LC = RTLIB::SHL_I128;
1704 } else if (N->getOpcode() == ISD::SRL) {
1707 LC = RTLIB::SRL_I32;
1708 else if (VT == MVT::i64)
1709 LC = RTLIB::SRL_I64;
1710 else if (VT == MVT::i128)
1711 LC = RTLIB::SRL_I128;
1713 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1716 LC = RTLIB::SRA_I32;
1717 else if (VT == MVT::i64)
1718 LC = RTLIB::SRA_I64;
1719 else if (VT == MVT::i128)
1720 LC = RTLIB::SRA_I128;
1722 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported shift!");
1724 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1725 SplitInteger(MakeLibCall(LC, VT, Ops, 2, isSigned), Lo, Hi);
1728 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
1729 SDValue &Lo, SDValue &Hi) {
1730 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1731 SDValue Op = N->getOperand(0);
1732 if (Op.getValueType().bitsLE(NVT)) {
1733 // The low part is sign extension of the input (degenerates to a copy).
1734 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, N->getOperand(0));
1735 // The high part is obtained by SRA'ing all but one of the bits of low part.
1736 unsigned LoSize = NVT.getSizeInBits();
1737 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1738 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
1740 // For example, extension of an i48 to an i64. The operand type necessarily
1741 // promotes to the result type, so will end up being expanded too.
1742 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1743 "Only know how to promote this result!");
1744 SDValue Res = GetPromotedInteger(Op);
1745 assert(Res.getValueType() == N->getValueType(0) &&
1746 "Operand over promoted?");
1747 // Split the promoted operand. This will simplify when it is expanded.
1748 SplitInteger(Res, Lo, Hi);
1749 unsigned ExcessBits =
1750 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1751 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
1752 DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1756 void DAGTypeLegalizer::
1757 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
1758 GetExpandedInteger(N->getOperand(0), Lo, Hi);
1759 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1761 if (EVT.bitsLE(Lo.getValueType())) {
1762 // sext_inreg the low part if needed.
1763 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, Lo.getValueType(), Lo,
1766 // The high part gets the sign extension from the lo-part. This handles
1767 // things like sextinreg V:i64 from i8.
1768 Hi = DAG.getNode(ISD::SRA, Hi.getValueType(), Lo,
1769 DAG.getConstant(Hi.getValueType().getSizeInBits()-1,
1770 TLI.getShiftAmountTy()));
1772 // For example, extension of an i48 to an i64. Leave the low part alone,
1773 // sext_inreg the high part.
1774 unsigned ExcessBits =
1775 EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
1776 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
1777 DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1781 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
1782 SDValue &Lo, SDValue &Hi) {
1783 MVT VT = N->getValueType(0);
1785 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1787 LC = RTLIB::SREM_I32;
1788 else if (VT == MVT::i64)
1789 LC = RTLIB::SREM_I64;
1790 else if (VT == MVT::i128)
1791 LC = RTLIB::SREM_I128;
1792 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1794 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1795 SplitInteger(MakeLibCall(LC, VT, Ops, 2, true), Lo, Hi);
1798 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
1799 SDValue &Lo, SDValue &Hi) {
1800 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1801 Lo = DAG.getNode(ISD::TRUNCATE, NVT, N->getOperand(0));
1802 Hi = DAG.getNode(ISD::SRL, N->getOperand(0).getValueType(), N->getOperand(0),
1803 DAG.getConstant(NVT.getSizeInBits(),
1804 TLI.getShiftAmountTy()));
1805 Hi = DAG.getNode(ISD::TRUNCATE, NVT, Hi);
1808 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
1809 SDValue &Lo, SDValue &Hi) {
1810 MVT VT = N->getValueType(0);
1812 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1814 LC = RTLIB::UDIV_I32;
1815 else if (VT == MVT::i64)
1816 LC = RTLIB::UDIV_I64;
1817 else if (VT == MVT::i128)
1818 LC = RTLIB::UDIV_I128;
1819 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
1821 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1822 SplitInteger(MakeLibCall(LC, VT, Ops, 2, false), Lo, Hi);
1825 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
1826 SDValue &Lo, SDValue &Hi) {
1827 MVT VT = N->getValueType(0);
1829 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1831 LC = RTLIB::UREM_I32;
1832 else if (VT == MVT::i64)
1833 LC = RTLIB::UREM_I64;
1834 else if (VT == MVT::i128)
1835 LC = RTLIB::UREM_I128;
1836 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
1838 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1839 SplitInteger(MakeLibCall(LC, VT, Ops, 2, false), Lo, Hi);
1842 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
1843 SDValue &Lo, SDValue &Hi) {
1844 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1845 SDValue Op = N->getOperand(0);
1846 if (Op.getValueType().bitsLE(NVT)) {
1847 // The low part is zero extension of the input (degenerates to a copy).
1848 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, N->getOperand(0));
1849 Hi = DAG.getConstant(0, NVT); // The high part is just a zero.
1851 // For example, extension of an i48 to an i64. The operand type necessarily
1852 // promotes to the result type, so will end up being expanded too.
1853 assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1854 "Only know how to promote this result!");
1855 SDValue Res = GetPromotedInteger(Op);
1856 assert(Res.getValueType() == N->getValueType(0) &&
1857 "Operand over promoted?");
1858 // Split the promoted operand. This will simplify when it is expanded.
1859 SplitInteger(Res, Lo, Hi);
1860 unsigned ExcessBits =
1861 Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1862 Hi = DAG.getZeroExtendInReg(Hi, MVT::getIntegerVT(ExcessBits));
1867 //===----------------------------------------------------------------------===//
1868 // Integer Operand Expansion
1869 //===----------------------------------------------------------------------===//
1871 /// ExpandIntegerOperand - This method is called when the specified operand of
1872 /// the specified node is found to need expansion. At this point, all of the
1873 /// result types of the node are known to be legal, but other operands of the
1874 /// node may need promotion or expansion as well as the specified one.
1875 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
1876 DEBUG(cerr << "Expand integer operand: "; N->dump(&DAG); cerr << "\n");
1877 SDValue Res = SDValue();
1879 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
1880 == TargetLowering::Custom)
1881 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
1883 if (Res.getNode() == 0) {
1884 switch (N->getOpcode()) {
1887 cerr << "ExpandIntegerOperand Op #" << OpNo << ": ";
1888 N->dump(&DAG); cerr << "\n";
1890 assert(0 && "Do not know how to expand this operator's operand!");
1893 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1894 case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
1895 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1897 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
1898 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
1899 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
1900 case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
1901 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo);
1903 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
1904 case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
1908 // If the result is null, the sub-method took care of registering results etc.
1909 if (!Res.getNode()) return false;
1910 // If the result is N, the sub-method updated N in place. Check to see if any
1911 // operands are new, and if so, mark them.
1912 if (Res.getNode() == N) {
1913 // Mark N as new and remark N and its operands. This allows us to correctly
1914 // revisit N if it needs another step of expansion and allows us to visit
1915 // any new operands to N.
1920 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1921 "Invalid operand expansion");
1923 ReplaceValueWith(SDValue(N, 0), Res);
1927 /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
1928 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1929 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
1931 ISD::CondCode &CCCode) {
1932 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1933 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
1934 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
1936 MVT VT = NewLHS.getValueType();
1938 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1939 if (RHSLo == RHSHi) {
1940 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
1941 if (RHSCST->isAllOnesValue()) {
1942 // Equality comparison to -1.
1943 NewLHS = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
1950 NewLHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1951 NewRHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1952 NewLHS = DAG.getNode(ISD::OR, NewLHS.getValueType(), NewLHS, NewRHS);
1953 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1957 // If this is a comparison of the sign bit, just look at the top part.
1959 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1960 if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
1961 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
1967 // FIXME: This generated code sucks.
1968 ISD::CondCode LowCC;
1970 default: assert(0 && "Unknown integer setcc!");
1972 case ISD::SETULT: LowCC = ISD::SETULT; break;
1974 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1976 case ISD::SETULE: LowCC = ISD::SETULE; break;
1978 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1981 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1982 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1983 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1985 // NOTE: on targets without efficient SELECT of bools, we can always use
1986 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
1987 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
1989 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC,
1990 false, DagCombineInfo);
1991 if (!Tmp1.getNode())
1992 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
1993 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
1994 CCCode, false, DagCombineInfo);
1995 if (!Tmp2.getNode())
1996 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
1997 DAG.getCondCode(CCCode));
1999 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
2000 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
2001 if ((Tmp1C && Tmp1C->isNullValue()) ||
2002 (Tmp2C && Tmp2C->isNullValue() &&
2003 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
2004 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
2005 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
2006 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
2007 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
2008 // low part is known false, returns high part.
2009 // For LE / GE, if high part is known false, ignore the low part.
2010 // For LT / GT, if high part is known true, ignore the low part.
2016 NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
2017 ISD::SETEQ, false, DagCombineInfo);
2018 if (!NewLHS.getNode())
2019 NewLHS = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
2021 NewLHS = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2022 NewLHS, Tmp1, Tmp2);
2026 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
2027 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
2028 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
2029 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2031 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2032 // against zero to select between true and false values.
2033 if (NewRHS.getNode() == 0) {
2034 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2035 CCCode = ISD::SETNE;
2038 // Update N to have the operands specified.
2039 return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
2040 DAG.getCondCode(CCCode), NewLHS, NewRHS,
2044 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
2045 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2046 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
2047 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2049 // If ExpandSetCCOperands returned a scalar, we need to compare the result
2050 // against zero to select between true and false values.
2051 if (NewRHS.getNode() == 0) {
2052 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2053 CCCode = ISD::SETNE;
2056 // Update N to have the operands specified.
2057 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2058 N->getOperand(2), N->getOperand(3),
2059 DAG.getCondCode(CCCode));
2062 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
2063 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2064 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
2065 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2067 // If ExpandSetCCOperands returned a scalar, use it.
2068 if (NewRHS.getNode() == 0) {
2069 assert(NewLHS.getValueType() == N->getValueType(0) &&
2070 "Unexpected setcc expansion!");
2074 // Otherwise, update N to have the operands specified.
2075 return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2076 DAG.getCondCode(CCCode));
2079 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
2080 SDValue Op = N->getOperand(0);
2081 MVT DstVT = N->getValueType(0);
2082 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
2083 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2084 "Don't know how to expand this SINT_TO_FP!");
2085 return MakeLibCall(LC, DstVT, &Op, 1, true);
2088 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
2089 if (ISD::isNormalStore(N))
2090 return ExpandOp_NormalStore(N, OpNo);
2092 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2093 assert(OpNo == 1 && "Can only expand the stored value so far");
2095 MVT VT = N->getOperand(1).getValueType();
2096 MVT NVT = TLI.getTypeToTransformTo(VT);
2097 SDValue Ch = N->getChain();
2098 SDValue Ptr = N->getBasePtr();
2099 int SVOffset = N->getSrcValueOffset();
2100 unsigned Alignment = N->getAlignment();
2101 bool isVolatile = N->isVolatile();
2104 assert(NVT.isByteSized() && "Expanded type not byte sized!");
2106 if (N->getMemoryVT().bitsLE(NVT)) {
2107 GetExpandedInteger(N->getValue(), Lo, Hi);
2108 return DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
2109 N->getMemoryVT(), isVolatile, Alignment);
2110 } else if (TLI.isLittleEndian()) {
2111 // Little-endian - low bits are at low addresses.
2112 GetExpandedInteger(N->getValue(), Lo, Hi);
2114 Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
2115 isVolatile, Alignment);
2117 unsigned ExcessBits =
2118 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
2119 MVT NEVT = MVT::getIntegerVT(ExcessBits);
2121 // Increment the pointer to the other half.
2122 unsigned IncrementSize = NVT.getSizeInBits()/8;
2123 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2124 DAG.getIntPtrConstant(IncrementSize));
2125 Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
2126 SVOffset+IncrementSize, NEVT,
2127 isVolatile, MinAlign(Alignment, IncrementSize));
2128 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2130 // Big-endian - high bits are at low addresses. Favor aligned stores at
2131 // the cost of some bit-fiddling.
2132 GetExpandedInteger(N->getValue(), Lo, Hi);
2134 MVT EVT = N->getMemoryVT();
2135 unsigned EBytes = EVT.getStoreSizeInBits()/8;
2136 unsigned IncrementSize = NVT.getSizeInBits()/8;
2137 unsigned ExcessBits = (EBytes - IncrementSize)*8;
2138 MVT HiVT = MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits);
2140 if (ExcessBits < NVT.getSizeInBits()) {
2141 // Transfer high bits from the top of Lo to the bottom of Hi.
2142 Hi = DAG.getNode(ISD::SHL, NVT, Hi,
2143 DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
2144 TLI.getShiftAmountTy()));
2145 Hi = DAG.getNode(ISD::OR, NVT, Hi,
2146 DAG.getNode(ISD::SRL, NVT, Lo,
2147 DAG.getConstant(ExcessBits,
2148 TLI.getShiftAmountTy())));
2151 // Store both the high bits and maybe some of the low bits.
2152 Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
2153 SVOffset, HiVT, isVolatile, Alignment);
2155 // Increment the pointer to the other half.
2156 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2157 DAG.getIntPtrConstant(IncrementSize));
2158 // Store the lowest ExcessBits bits in the second half.
2159 Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(),
2160 SVOffset+IncrementSize,
2161 MVT::getIntegerVT(ExcessBits),
2162 isVolatile, MinAlign(Alignment, IncrementSize));
2163 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2167 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
2169 GetExpandedInteger(N->getOperand(0), InL, InH);
2170 // Just truncate the low part of the source.
2171 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), InL);
2174 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
2175 SDValue Op = N->getOperand(0);
2176 MVT SrcVT = Op.getValueType();
2177 MVT DstVT = N->getValueType(0);
2179 if (TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
2180 // Do a signed conversion then adjust the result.
2181 SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, DstVT, Op);
2182 SignedConv = TLI.LowerOperation(SignedConv, DAG);
2184 // The result of the signed conversion needs adjusting if the 'sign bit' of
2185 // the incoming integer was set. To handle this, we dynamically test to see
2186 // if it is set, and, if so, add a fudge factor.
2188 const uint64_t F32TwoE32 = 0x4F800000ULL;
2189 const uint64_t F32TwoE64 = 0x5F800000ULL;
2190 const uint64_t F32TwoE128 = 0x7F800000ULL;
2193 if (SrcVT == MVT::i32)
2194 FF = APInt(32, F32TwoE32);
2195 else if (SrcVT == MVT::i64)
2196 FF = APInt(32, F32TwoE64);
2197 else if (SrcVT == MVT::i128)
2198 FF = APInt(32, F32TwoE128);
2200 assert(false && "Unsupported UINT_TO_FP!");
2202 // Check whether the sign bit is set.
2204 GetExpandedInteger(Op, Lo, Hi);
2205 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
2206 DAG.getConstant(0, Hi.getValueType()),
2209 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
2210 SDValue FudgePtr = DAG.getConstantPool(ConstantInt::get(FF.zext(64)),
2211 TLI.getPointerTy());
2213 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
2214 SDValue Zero = DAG.getIntPtrConstant(0);
2215 SDValue Four = DAG.getIntPtrConstant(4);
2216 if (TLI.isBigEndian()) std::swap(Zero, Four);
2217 SDValue Offset = DAG.getNode(ISD::SELECT, Zero.getValueType(), SignSet,
2219 unsigned Alignment =
2220 1 << cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
2221 FudgePtr = DAG.getNode(ISD::ADD, TLI.getPointerTy(), FudgePtr, Offset);
2222 Alignment = std::min(Alignment, 4u);
2224 // Load the value out, extending it from f32 to the destination float type.
2225 // FIXME: Avoid the extend by constructing the right constant pool?
2226 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, DstVT, DAG.getEntryNode(),
2227 FudgePtr, NULL, 0, MVT::f32,
2229 return DAG.getNode(ISD::FADD, DstVT, SignedConv, Fudge);
2232 // Otherwise, use a libcall.
2233 RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
2234 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2235 "Don't know how to expand this UINT_TO_FP!");
2236 return MakeLibCall(LC, DstVT, &Op, 1, true);