1 //===-- LegalizeTypesPromote.cpp - Promotion for LegalizeTypes ------------===//
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 promotion support for LegalizeTypes. Promotion is the
11 // act of changing a computation in an invalid type to be a computation in a
12 // larger type. For example, implementing i8 arithmetic in an i32 register (as
13 // is often needed on powerpc for example).
15 //===----------------------------------------------------------------------===//
17 #include "LegalizeTypes.h"
20 //===----------------------------------------------------------------------===//
22 //===----------------------------------------------------------------------===//
24 /// PromoteResult - This method is called when a result of a node is found to be
25 /// in need of promotion to a larger type. At this point, the node may also
26 /// have invalid operands or may have other results that need expansion, we just
27 /// know that (at least) one result needs promotion.
28 void DAGTypeLegalizer::PromoteResult(SDNode *N, unsigned ResNo) {
29 DEBUG(cerr << "Promote node result: "; N->dump(&DAG); cerr << "\n");
30 SDOperand Result = SDOperand();
32 switch (N->getOpcode()) {
35 cerr << "PromoteResult #" << ResNo << ": ";
36 N->dump(&DAG); cerr << "\n";
38 assert(0 && "Do not know how to promote this operator!");
40 case ISD::UNDEF: Result = PromoteResult_UNDEF(N); break;
41 case ISD::Constant: Result = PromoteResult_Constant(N); break;
43 case ISD::TRUNCATE: Result = PromoteResult_TRUNCATE(N); break;
44 case ISD::SIGN_EXTEND:
45 case ISD::ZERO_EXTEND:
46 case ISD::ANY_EXTEND: Result = PromoteResult_INT_EXTEND(N); break;
47 case ISD::FP_ROUND: Result = PromoteResult_FP_ROUND(N); break;
49 case ISD::FP_TO_UINT: Result = PromoteResult_FP_TO_XINT(N); break;
50 case ISD::SETCC: Result = PromoteResult_SETCC(N); break;
51 case ISD::LOAD: Result = PromoteResult_LOAD(cast<LoadSDNode>(N)); break;
52 case ISD::BUILD_PAIR: Result = PromoteResult_BUILD_PAIR(N); break;
53 case ISD::BIT_CONVERT: Result = PromoteResult_BIT_CONVERT(N); break;
60 case ISD::MUL: Result = PromoteResult_SimpleIntBinOp(N); break;
63 case ISD::SREM: Result = PromoteResult_SDIV(N); break;
66 case ISD::UREM: Result = PromoteResult_UDIV(N); break;
68 case ISD::SHL: Result = PromoteResult_SHL(N); break;
69 case ISD::SRA: Result = PromoteResult_SRA(N); break;
70 case ISD::SRL: Result = PromoteResult_SRL(N); break;
72 case ISD::SELECT: Result = PromoteResult_SELECT(N); break;
73 case ISD::SELECT_CC: Result = PromoteResult_SELECT_CC(N); break;
75 case ISD::CTLZ: Result = PromoteResult_CTLZ(N); break;
76 case ISD::CTPOP: Result = PromoteResult_CTPOP(N); break;
77 case ISD::CTTZ: Result = PromoteResult_CTTZ(N); break;
79 case ISD::EXTRACT_VECTOR_ELT:
80 Result = PromoteResult_EXTRACT_VECTOR_ELT(N);
84 // If Result is null, the sub-method took care of registering the result.
86 SetPromotedOp(SDOperand(N, ResNo), Result);
89 SDOperand DAGTypeLegalizer::PromoteResult_UNDEF(SDNode *N) {
90 return DAG.getNode(ISD::UNDEF, TLI.getTypeToTransformTo(N->getValueType(0)));
93 SDOperand DAGTypeLegalizer::PromoteResult_Constant(SDNode *N) {
94 MVT::ValueType VT = N->getValueType(0);
95 // Zero extend things like i1, sign extend everything else. It shouldn't
96 // matter in theory which one we pick, but this tends to give better code?
97 unsigned Opc = VT != MVT::i1 ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
98 SDOperand Result = DAG.getNode(Opc, TLI.getTypeToTransformTo(VT),
100 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
104 SDOperand DAGTypeLegalizer::PromoteResult_TRUNCATE(SDNode *N) {
107 switch (getTypeAction(N->getOperand(0).getValueType())) {
108 default: assert(0 && "Unknown type action!");
111 Res = N->getOperand(0);
114 Res = GetPromotedOp(N->getOperand(0));
118 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
119 assert(MVT::getSizeInBits(Res.getValueType()) >= MVT::getSizeInBits(NVT) &&
120 "Truncation doesn't make sense!");
121 if (Res.getValueType() == NVT)
124 // Truncate to NVT instead of VT
125 return DAG.getNode(ISD::TRUNCATE, NVT, Res);
128 SDOperand DAGTypeLegalizer::PromoteResult_INT_EXTEND(SDNode *N) {
129 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
131 if (getTypeAction(N->getOperand(0).getValueType()) == Promote) {
132 SDOperand Res = GetPromotedOp(N->getOperand(0));
133 assert(MVT::getSizeInBits(Res.getValueType()) <= MVT::getSizeInBits(NVT) &&
134 "Extension doesn't make sense!");
136 // If the result and operand types are the same after promotion, simplify
137 // to an in-register extension.
138 if (NVT == Res.getValueType()) {
139 // The high bits are not guaranteed to be anything. Insert an extend.
140 if (N->getOpcode() == ISD::SIGN_EXTEND)
141 return DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res,
142 DAG.getValueType(N->getOperand(0).getValueType()));
143 if (N->getOpcode() == ISD::ZERO_EXTEND)
144 return DAG.getZeroExtendInReg(Res, N->getOperand(0).getValueType());
145 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
150 // Otherwise, just extend the original operand all the way to the larger type.
151 return DAG.getNode(N->getOpcode(), NVT, N->getOperand(0));
154 SDOperand DAGTypeLegalizer::PromoteResult_FP_ROUND(SDNode *N) {
155 // NOTE: Assumes input is legal.
156 if (N->getConstantOperandVal(1) == 0)
157 return DAG.getNode(ISD::FP_ROUND_INREG, N->getOperand(0).getValueType(),
158 N->getOperand(0), DAG.getValueType(N->getValueType(0)));
159 // If the precision discard isn't needed, just return the operand unrounded.
160 return N->getOperand(0);
163 SDOperand DAGTypeLegalizer::PromoteResult_FP_TO_XINT(SDNode *N) {
164 SDOperand Op = N->getOperand(0);
165 // If the operand needed to be promoted, do so now.
166 if (getTypeAction(Op.getValueType()) == Promote)
167 // The input result is prerounded, so we don't have to do anything special.
168 Op = GetPromotedOp(Op);
170 unsigned NewOpc = N->getOpcode();
171 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
173 // If we're promoting a UINT to a larger size, check to see if the new node
174 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
175 // we can use that instead. This allows us to generate better code for
176 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
177 // legal, such as PowerPC.
178 if (N->getOpcode() == ISD::FP_TO_UINT) {
179 if (!TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
180 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
181 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom))
182 NewOpc = ISD::FP_TO_SINT;
185 return DAG.getNode(NewOpc, NVT, Op);
188 SDOperand DAGTypeLegalizer::PromoteResult_SETCC(SDNode *N) {
189 assert(isTypeLegal(TLI.getSetCCResultType(N->getOperand(0)))
190 && "SetCC type is not legal??");
191 return DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(N->getOperand(0)),
192 N->getOperand(0), N->getOperand(1), N->getOperand(2));
195 SDOperand DAGTypeLegalizer::PromoteResult_LOAD(LoadSDNode *N) {
196 MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
197 ISD::LoadExtType ExtType =
198 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
199 SDOperand Res = DAG.getExtLoad(ExtType, NVT, N->getChain(), N->getBasePtr(),
200 N->getSrcValue(), N->getSrcValueOffset(),
201 N->getMemoryVT(), N->isVolatile(),
204 // Legalized the chain result - switch anything that used the old chain to
206 ReplaceValueWith(SDOperand(N, 1), Res.getValue(1));
210 SDOperand DAGTypeLegalizer::PromoteResult_BUILD_PAIR(SDNode *N) {
211 // The pair element type may be legal, or may not promote to the same type as
212 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
213 return DAG.getNode(ISD::ANY_EXTEND,
214 TLI.getTypeToTransformTo(N->getValueType(0)),
215 JoinIntegers(N->getOperand(0), N->getOperand(1)));
218 SDOperand DAGTypeLegalizer::PromoteResult_BIT_CONVERT(SDNode *N) {
219 SDOperand InOp = N->getOperand(0);
220 MVT::ValueType InVT = InOp.getValueType();
221 MVT::ValueType NInVT = TLI.getTypeToTransformTo(InVT);
222 MVT::ValueType OutVT = TLI.getTypeToTransformTo(N->getValueType(0));
224 switch (getTypeAction(InVT)) {
226 assert(false && "Unknown type action!");
231 if (MVT::getSizeInBits(OutVT) == MVT::getSizeInBits(NInVT))
232 // The input promotes to the same size. Convert the promoted value.
233 return DAG.getNode(ISD::BIT_CONVERT, OutVT, GetPromotedOp(InOp));
238 // Convert the element to an integer and promote it by hand.
239 InOp = DAG.getNode(ISD::BIT_CONVERT,
240 MVT::getIntegerType(MVT::getSizeInBits(InVT)),
241 GetScalarizedOp(InOp));
242 InOp = DAG.getNode(ISD::ANY_EXTEND,
243 MVT::getIntegerType(MVT::getSizeInBits(OutVT)), InOp);
244 return DAG.getNode(ISD::BIT_CONVERT, OutVT, InOp);
246 // For example, i32 = BIT_CONVERT v2i16 on alpha. Convert the split
247 // pieces of the input into integers and reassemble in the final type.
249 GetSplitOp(N->getOperand(0), Lo, Hi);
251 unsigned LoBits = MVT::getSizeInBits(Lo.getValueType());
252 Lo = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(LoBits), Lo);
254 unsigned HiBits = MVT::getSizeInBits(Hi.getValueType());
255 Hi = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(HiBits), Hi);
257 if (TLI.isBigEndian())
260 InOp = DAG.getNode(ISD::ANY_EXTEND,
261 MVT::getIntegerType(MVT::getSizeInBits(OutVT)),
262 JoinIntegers(Lo, Hi));
263 return DAG.getNode(ISD::BIT_CONVERT, OutVT, InOp);
266 // Otherwise, lower the bit-convert to a store/load from the stack, then
268 SDOperand Op = CreateStackStoreLoad(InOp, N->getValueType(0));
269 return PromoteResult_LOAD(cast<LoadSDNode>(Op.Val));
272 SDOperand DAGTypeLegalizer::PromoteResult_SimpleIntBinOp(SDNode *N) {
273 // The input may have strange things in the top bits of the registers, but
274 // these operations don't care. They may have weird bits going out, but
275 // that too is okay if they are integer operations.
276 SDOperand LHS = GetPromotedOp(N->getOperand(0));
277 SDOperand RHS = GetPromotedOp(N->getOperand(1));
278 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
281 SDOperand DAGTypeLegalizer::PromoteResult_SDIV(SDNode *N) {
282 // Sign extend the input.
283 SDOperand LHS = GetPromotedOp(N->getOperand(0));
284 SDOperand RHS = GetPromotedOp(N->getOperand(1));
285 MVT::ValueType VT = N->getValueType(0);
286 LHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, LHS.getValueType(), LHS,
287 DAG.getValueType(VT));
288 RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, RHS.getValueType(), RHS,
289 DAG.getValueType(VT));
291 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
294 SDOperand DAGTypeLegalizer::PromoteResult_UDIV(SDNode *N) {
295 // Zero extend the input.
296 SDOperand LHS = GetPromotedOp(N->getOperand(0));
297 SDOperand RHS = GetPromotedOp(N->getOperand(1));
298 MVT::ValueType VT = N->getValueType(0);
299 LHS = DAG.getZeroExtendInReg(LHS, VT);
300 RHS = DAG.getZeroExtendInReg(RHS, VT);
302 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
305 SDOperand DAGTypeLegalizer::PromoteResult_SHL(SDNode *N) {
306 return DAG.getNode(ISD::SHL, TLI.getTypeToTransformTo(N->getValueType(0)),
307 GetPromotedOp(N->getOperand(0)), N->getOperand(1));
310 SDOperand DAGTypeLegalizer::PromoteResult_SRA(SDNode *N) {
311 // The input value must be properly sign extended.
312 MVT::ValueType VT = N->getValueType(0);
313 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
314 SDOperand Res = GetPromotedOp(N->getOperand(0));
315 Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Res, DAG.getValueType(VT));
316 return DAG.getNode(ISD::SRA, NVT, Res, N->getOperand(1));
319 SDOperand DAGTypeLegalizer::PromoteResult_SRL(SDNode *N) {
320 // The input value must be properly zero extended.
321 MVT::ValueType VT = N->getValueType(0);
322 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
323 SDOperand Res = GetPromotedZExtOp(N->getOperand(0));
324 return DAG.getNode(ISD::SRL, NVT, Res, N->getOperand(1));
327 SDOperand DAGTypeLegalizer::PromoteResult_SELECT(SDNode *N) {
328 SDOperand LHS = GetPromotedOp(N->getOperand(1));
329 SDOperand RHS = GetPromotedOp(N->getOperand(2));
330 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
333 SDOperand DAGTypeLegalizer::PromoteResult_SELECT_CC(SDNode *N) {
334 SDOperand LHS = GetPromotedOp(N->getOperand(2));
335 SDOperand RHS = GetPromotedOp(N->getOperand(3));
336 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
337 N->getOperand(1), LHS, RHS, N->getOperand(4));
340 SDOperand DAGTypeLegalizer::PromoteResult_CTLZ(SDNode *N) {
341 SDOperand Op = GetPromotedOp(N->getOperand(0));
342 MVT::ValueType OVT = N->getValueType(0);
343 MVT::ValueType NVT = Op.getValueType();
344 // Zero extend to the promoted type and do the count there.
345 Op = DAG.getNode(ISD::CTLZ, NVT, DAG.getZeroExtendInReg(Op, OVT));
346 // Subtract off the extra leading bits in the bigger type.
347 return DAG.getNode(ISD::SUB, NVT, Op,
348 DAG.getConstant(MVT::getSizeInBits(NVT) -
349 MVT::getSizeInBits(OVT), NVT));
352 SDOperand DAGTypeLegalizer::PromoteResult_CTPOP(SDNode *N) {
353 SDOperand Op = GetPromotedOp(N->getOperand(0));
354 MVT::ValueType OVT = N->getValueType(0);
355 MVT::ValueType NVT = Op.getValueType();
356 // Zero extend to the promoted type and do the count there.
357 return DAG.getNode(ISD::CTPOP, NVT, DAG.getZeroExtendInReg(Op, OVT));
360 SDOperand DAGTypeLegalizer::PromoteResult_CTTZ(SDNode *N) {
361 SDOperand Op = GetPromotedOp(N->getOperand(0));
362 MVT::ValueType OVT = N->getValueType(0);
363 MVT::ValueType NVT = Op.getValueType();
364 // The count is the same in the promoted type except if the original
365 // value was zero. This can be handled by setting the bit just off
366 // the top of the original type.
367 Op = DAG.getNode(ISD::OR, NVT, Op,
368 // FIXME: Do this using an APINT constant.
369 DAG.getConstant(1UL << MVT::getSizeInBits(OVT), NVT));
370 return DAG.getNode(ISD::CTTZ, NVT, Op);
373 SDOperand DAGTypeLegalizer::PromoteResult_EXTRACT_VECTOR_ELT(SDNode *N) {
374 MVT::ValueType OldVT = N->getValueType(0);
375 SDOperand OldVec = N->getOperand(0);
376 unsigned OldElts = MVT::getVectorNumElements(OldVec.getValueType());
379 assert(!isTypeLegal(OldVec.getValueType()) &&
380 "Legal one-element vector of a type needing promotion!");
381 // It is tempting to follow GetScalarizedOp by a call to GetPromotedOp,
382 // but this would be wrong because the scalarized value may not yet have
384 return DAG.getNode(ISD::ANY_EXTEND, TLI.getTypeToTransformTo(OldVT),
385 GetScalarizedOp(OldVec));
388 // Convert to a vector half as long with an element type of twice the width,
389 // for example <4 x i16> -> <2 x i32>.
390 assert(!(OldElts & 1) && "Odd length vectors not supported!");
391 MVT::ValueType NewVT = MVT::getIntegerType(2 * MVT::getSizeInBits(OldVT));
392 assert(!MVT::isExtendedVT(OldVT) && !MVT::isExtendedVT(NewVT));
394 SDOperand NewVec = DAG.getNode(ISD::BIT_CONVERT,
395 MVT::getVectorType(NewVT, OldElts / 2),
398 // Extract the element at OldIdx / 2 from the new vector.
399 SDOperand OldIdx = N->getOperand(1);
400 SDOperand NewIdx = DAG.getNode(ISD::SRL, OldIdx.getValueType(), OldIdx,
401 DAG.getConstant(1, TLI.getShiftAmountTy()));
402 SDOperand Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, NewIdx);
404 // Select the appropriate half of the element: Lo if OldIdx was even,
407 SDOperand Hi = DAG.getNode(ISD::SRL, NewVT, Elt,
408 DAG.getConstant(MVT::getSizeInBits(OldVT),
409 TLI.getShiftAmountTy()));
410 if (TLI.isBigEndian())
413 SDOperand Odd = DAG.getNode(ISD::AND, OldIdx.getValueType(), OldIdx,
414 DAG.getConstant(1, TLI.getShiftAmountTy()));
415 return DAG.getNode(ISD::SELECT, NewVT, Odd, Hi, Lo);
418 //===----------------------------------------------------------------------===//
420 //===----------------------------------------------------------------------===//
422 /// PromoteOperand - This method is called when the specified operand of the
423 /// specified node is found to need promotion. At this point, all of the result
424 /// types of the node are known to be legal, but other operands of the node may
425 /// need promotion or expansion as well as the specified one.
426 bool DAGTypeLegalizer::PromoteOperand(SDNode *N, unsigned OpNo) {
427 DEBUG(cerr << "Promote node operand: "; N->dump(&DAG); cerr << "\n");
429 switch (N->getOpcode()) {
432 cerr << "PromoteOperand Op #" << OpNo << ": ";
433 N->dump(&DAG); cerr << "\n";
435 assert(0 && "Do not know how to promote this operator's operand!");
438 case ISD::ANY_EXTEND: Res = PromoteOperand_ANY_EXTEND(N); break;
439 case ISD::ZERO_EXTEND: Res = PromoteOperand_ZERO_EXTEND(N); break;
440 case ISD::SIGN_EXTEND: Res = PromoteOperand_SIGN_EXTEND(N); break;
441 case ISD::TRUNCATE: Res = PromoteOperand_TRUNCATE(N); break;
442 case ISD::FP_EXTEND: Res = PromoteOperand_FP_EXTEND(N); break;
443 case ISD::FP_ROUND: Res = PromoteOperand_FP_ROUND(N); break;
444 case ISD::SINT_TO_FP:
445 case ISD::UINT_TO_FP: Res = PromoteOperand_INT_TO_FP(N); break;
446 case ISD::BUILD_PAIR: Res = PromoteOperand_BUILD_PAIR(N); break;
448 case ISD::SELECT: Res = PromoteOperand_SELECT(N, OpNo); break;
449 case ISD::BRCOND: Res = PromoteOperand_BRCOND(N, OpNo); break;
450 case ISD::BR_CC: Res = PromoteOperand_BR_CC(N, OpNo); break;
451 case ISD::SETCC: Res = PromoteOperand_SETCC(N, OpNo); break;
453 case ISD::STORE: Res = PromoteOperand_STORE(cast<StoreSDNode>(N),
457 case ISD::MEMMOVE: Res = HandleMemIntrinsic(N); break;
459 case ISD::BUILD_VECTOR: Res = PromoteOperand_BUILD_VECTOR(N); break;
460 case ISD::INSERT_VECTOR_ELT:
461 Res = PromoteOperand_INSERT_VECTOR_ELT(N, OpNo);
464 case ISD::RET: Res = PromoteOperand_RET(N, OpNo); break;
466 case ISD::MEMBARRIER: Res = PromoteOperand_MEMBARRIER(N); break;
469 // If the result is null, the sub-method took care of registering results etc.
470 if (!Res.Val) return false;
471 // If the result is N, the sub-method updated N in place.
473 // Mark N as new and remark N and its operands. This allows us to correctly
474 // revisit N if it needs another step of promotion and allows us to visit
475 // any new operands to N.
480 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
481 "Invalid operand expansion");
483 ReplaceValueWith(SDOperand(N, 0), Res);
487 SDOperand DAGTypeLegalizer::PromoteOperand_ANY_EXTEND(SDNode *N) {
488 SDOperand Op = GetPromotedOp(N->getOperand(0));
489 return DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
492 SDOperand DAGTypeLegalizer::PromoteOperand_ZERO_EXTEND(SDNode *N) {
493 SDOperand Op = GetPromotedOp(N->getOperand(0));
494 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
495 return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
498 SDOperand DAGTypeLegalizer::PromoteOperand_SIGN_EXTEND(SDNode *N) {
499 SDOperand Op = GetPromotedOp(N->getOperand(0));
500 Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
501 return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(),
502 Op, DAG.getValueType(N->getOperand(0).getValueType()));
505 SDOperand DAGTypeLegalizer::PromoteOperand_TRUNCATE(SDNode *N) {
506 SDOperand Op = GetPromotedOp(N->getOperand(0));
507 return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), Op);
510 SDOperand DAGTypeLegalizer::PromoteOperand_FP_EXTEND(SDNode *N) {
511 SDOperand Op = GetPromotedOp(N->getOperand(0));
512 return DAG.getNode(ISD::FP_EXTEND, N->getValueType(0), Op);
515 SDOperand DAGTypeLegalizer::PromoteOperand_FP_ROUND(SDNode *N) {
516 SDOperand Op = GetPromotedOp(N->getOperand(0));
517 return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Op,
518 DAG.getIntPtrConstant(0));
521 SDOperand DAGTypeLegalizer::PromoteOperand_INT_TO_FP(SDNode *N) {
522 SDOperand In = GetPromotedOp(N->getOperand(0));
523 MVT::ValueType OpVT = N->getOperand(0).getValueType();
524 if (N->getOpcode() == ISD::UINT_TO_FP)
525 In = DAG.getZeroExtendInReg(In, OpVT);
527 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(),
528 In, DAG.getValueType(OpVT));
530 return DAG.UpdateNodeOperands(SDOperand(N, 0), In);
533 SDOperand DAGTypeLegalizer::PromoteOperand_BUILD_PAIR(SDNode *N) {
534 // Since the result type is legal, the operands must promote to it.
535 MVT::ValueType OVT = N->getOperand(0).getValueType();
536 SDOperand Lo = GetPromotedOp(N->getOperand(0));
537 SDOperand Hi = GetPromotedOp(N->getOperand(1));
538 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
540 Lo = DAG.getZeroExtendInReg(Lo, OVT);
541 Hi = DAG.getNode(ISD::SHL, N->getValueType(0), Hi,
542 DAG.getConstant(MVT::getSizeInBits(OVT),
543 TLI.getShiftAmountTy()));
544 return DAG.getNode(ISD::OR, N->getValueType(0), Lo, Hi);
547 SDOperand DAGTypeLegalizer::PromoteOperand_SELECT(SDNode *N, unsigned OpNo) {
548 assert(OpNo == 0 && "Only know how to promote condition");
549 SDOperand Cond = GetPromotedOp(N->getOperand(0)); // Promote the condition.
551 // The top bits of the promoted condition are not necessarily zero, ensure
552 // that the value is properly zero extended.
553 unsigned BitWidth = Cond.getValueSizeInBits();
554 if (!DAG.MaskedValueIsZero(Cond,
555 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
556 Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
558 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
559 return DAG.UpdateNodeOperands(SDOperand(N, 0), Cond, N->getOperand(1),
563 SDOperand DAGTypeLegalizer::PromoteOperand_BRCOND(SDNode *N, unsigned OpNo) {
564 assert(OpNo == 1 && "only know how to promote condition");
565 SDOperand Cond = GetPromotedOp(N->getOperand(1)); // Promote the condition.
567 // The top bits of the promoted condition are not necessarily zero, ensure
568 // that the value is properly zero extended.
569 unsigned BitWidth = Cond.getValueSizeInBits();
570 if (!DAG.MaskedValueIsZero(Cond,
571 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
572 Cond = DAG.getZeroExtendInReg(Cond, MVT::i1);
574 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
575 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0), Cond,
579 SDOperand DAGTypeLegalizer::PromoteOperand_BR_CC(SDNode *N, unsigned OpNo) {
580 assert(OpNo == 2 && "Don't know how to promote this operand");
582 SDOperand LHS = N->getOperand(2);
583 SDOperand RHS = N->getOperand(3);
584 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
586 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
588 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
589 N->getOperand(1), LHS, RHS, N->getOperand(4));
592 SDOperand DAGTypeLegalizer::PromoteOperand_SETCC(SDNode *N, unsigned OpNo) {
593 assert(OpNo == 0 && "Don't know how to promote this operand");
595 SDOperand LHS = N->getOperand(0);
596 SDOperand RHS = N->getOperand(1);
597 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
599 // The CC (#2) is always legal.
600 return DAG.UpdateNodeOperands(SDOperand(N, 0), LHS, RHS, N->getOperand(2));
603 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
604 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
605 void DAGTypeLegalizer::PromoteSetCCOperands(SDOperand &NewLHS,SDOperand &NewRHS,
606 ISD::CondCode CCCode) {
607 MVT::ValueType VT = NewLHS.getValueType();
609 // Get the promoted values.
610 NewLHS = GetPromotedOp(NewLHS);
611 NewRHS = GetPromotedOp(NewRHS);
613 // If this is an FP compare, the operands have already been extended.
614 if (!MVT::isInteger(NewLHS.getValueType()))
617 // Otherwise, we have to insert explicit sign or zero extends. Note
618 // that we could insert sign extends for ALL conditions, but zero extend
619 // is cheaper on many machines (an AND instead of two shifts), so prefer
622 default: assert(0 && "Unknown integer comparison!");
629 // ALL of these operations will work if we either sign or zero extend
630 // the operands (including the unsigned comparisons!). Zero extend is
631 // usually a simpler/cheaper operation, so prefer it.
632 NewLHS = DAG.getZeroExtendInReg(NewLHS, VT);
633 NewRHS = DAG.getZeroExtendInReg(NewRHS, VT);
639 NewLHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewLHS.getValueType(), NewLHS,
640 DAG.getValueType(VT));
641 NewRHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, NewRHS.getValueType(), NewRHS,
642 DAG.getValueType(VT));
647 SDOperand DAGTypeLegalizer::PromoteOperand_STORE(StoreSDNode *N, unsigned OpNo){
648 SDOperand Ch = N->getChain(), Ptr = N->getBasePtr();
649 int SVOffset = N->getSrcValueOffset();
650 unsigned Alignment = N->getAlignment();
651 bool isVolatile = N->isVolatile();
653 SDOperand Val = GetPromotedOp(N->getValue()); // Get promoted value.
655 assert(!N->isTruncatingStore() && "Cannot promote this store operand!");
657 // Truncate the value and store the result.
658 return DAG.getTruncStore(Ch, Val, Ptr, N->getSrcValue(),
659 SVOffset, N->getMemoryVT(),
660 isVolatile, Alignment);
663 SDOperand DAGTypeLegalizer::PromoteOperand_BUILD_VECTOR(SDNode *N) {
664 // The vector type is legal but the element type is not. This implies
665 // that the vector is a power-of-two in length and that the element
666 // type does not have a strange size (eg: it is not i1).
667 MVT::ValueType VecVT = N->getValueType(0);
668 unsigned NumElts = MVT::getVectorNumElements(VecVT);
669 assert(!(NumElts & 1) && "Legal vector of one illegal element?");
671 // Build a vector of half the length out of elements of twice the bitwidth.
672 // For example <4 x i16> -> <2 x i32>.
673 MVT::ValueType OldVT = N->getOperand(0).getValueType();
674 MVT::ValueType NewVT = MVT::getIntegerType(2 * MVT::getSizeInBits(OldVT));
675 assert(!MVT::isExtendedVT(OldVT) && !MVT::isExtendedVT(NewVT));
677 std::vector<SDOperand> NewElts;
678 NewElts.reserve(NumElts/2);
680 for (unsigned i = 0; i < NumElts; i += 2) {
681 // Combine two successive elements into one promoted element.
682 SDOperand Lo = N->getOperand(i);
683 SDOperand Hi = N->getOperand(i+1);
684 if (TLI.isBigEndian())
686 NewElts.push_back(JoinIntegers(Lo, Hi));
689 SDOperand NewVec = DAG.getNode(ISD::BUILD_VECTOR,
690 MVT::getVectorType(NewVT, NewElts.size()),
691 &NewElts[0], NewElts.size());
693 // Convert the new vector to the old vector type.
694 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
697 SDOperand DAGTypeLegalizer::PromoteOperand_INSERT_VECTOR_ELT(SDNode *N,
700 // Promote the inserted value. This is valid because the type does not
701 // have to match the vector element type.
703 // Check that any extra bits introduced will be truncated away.
704 assert(MVT::getSizeInBits(N->getOperand(1).getValueType()) >=
705 MVT::getSizeInBits(MVT::getVectorElementType(N->getValueType(0))) &&
706 "Type of inserted value narrower than vector element type!");
707 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
708 GetPromotedOp(N->getOperand(1)),
712 assert(OpNo == 2 && "Different operand and result vector types?");
714 // Promote the index.
715 SDOperand Idx = N->getOperand(2);
716 Idx = DAG.getZeroExtendInReg(GetPromotedOp(Idx), Idx.getValueType());
717 return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
718 N->getOperand(1), Idx);
721 SDOperand DAGTypeLegalizer::PromoteOperand_RET(SDNode *N, unsigned OpNo) {
722 assert(!(OpNo & 1) && "Return values should be legally typed!");
723 assert((N->getNumOperands() & 1) && "Wrong number of operands!");
725 // It's a flag. Promote all the flags in one hit, as an optimization.
726 SmallVector<SDOperand, 8> NewValues(N->getNumOperands());
727 NewValues[0] = N->getOperand(0); // The chain
728 for (unsigned i = 1, e = N->getNumOperands(); i < e; i += 2) {
730 NewValues[i] = N->getOperand(i);
733 SDOperand Flag = N->getOperand(i + 1);
734 if (getTypeAction(Flag.getValueType()) == Promote)
735 // The promoted value may have rubbish in the new bits, but that
736 // doesn't matter because those bits aren't queried anyway.
737 Flag = GetPromotedOp(Flag);
738 NewValues[i + 1] = Flag;
741 return DAG.UpdateNodeOperands(SDOperand (N, 0),
742 &NewValues[0], NewValues.size());
745 SDOperand DAGTypeLegalizer::PromoteOperand_MEMBARRIER(SDNode *N) {
747 NewOps[0] = N->getOperand(0);
748 for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
749 SDOperand Flag = GetPromotedOp(N->getOperand(i));
750 NewOps[i] = DAG.getZeroExtendInReg(Flag, MVT::i1);
752 return DAG.UpdateNodeOperands(SDOperand (N, 0), NewOps,
753 array_lengthof(NewOps));