1 //===------- LegalizeVectorTypes.cpp - Legalization of vector 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 performs vector type splitting and scalarization for LegalizeTypes.
11 // Scalarization is the act of changing a computation in an illegal one-element
12 // vector type to be a computation in its scalar element type. For example,
13 // implementing <1 x f32> arithmetic in a scalar f32 register. This is needed
14 // as a base case when scalarizing vector arithmetic like <4 x f32>, which
15 // eventually decomposes to scalars if the target doesn't support v4f32 or v2f32
17 // Splitting is the act of changing a computation in an invalid vector type to
18 // be a computation in multiple vectors of a smaller type. For example,
19 // implementing <128 x f32> operations in terms of two <64 x f32> operations.
21 //===----------------------------------------------------------------------===//
23 #include "LegalizeTypes.h"
26 //===----------------------------------------------------------------------===//
27 // Result Vector Scalarization: <1 x ty> -> ty.
28 //===----------------------------------------------------------------------===//
30 void DAGTypeLegalizer::ScalarizeVectorResult(SDNode *N, unsigned ResNo) {
31 DEBUG(cerr << "Scalarize node result " << ResNo << ": "; N->dump(&DAG);
33 SDValue R = SDValue();
35 switch (N->getOpcode()) {
38 cerr << "ScalarizeVectorResult #" << ResNo << ": ";
39 N->dump(&DAG); cerr << "\n";
41 assert(0 && "Do not know how to scalarize the result of this operator!");
44 case ISD::BIT_CONVERT: R = ScalarizeVecRes_BIT_CONVERT(N); break;
45 case ISD::BUILD_VECTOR: R = N->getOperand(0); break;
46 case ISD::CONVERT_RNDSAT: R = ScalarizeVecRes_CONVERT_RNDSAT(N); break;
47 case ISD::EXTRACT_SUBVECTOR: R = ScalarizeVecRes_EXTRACT_SUBVECTOR(N); break;
48 case ISD::FPOWI: R = ScalarizeVecRes_FPOWI(N); break;
49 case ISD::INSERT_VECTOR_ELT: R = ScalarizeVecRes_INSERT_VECTOR_ELT(N); break;
50 case ISD::LOAD: R = ScalarizeVecRes_LOAD(cast<LoadSDNode>(N));break;
51 case ISD::SELECT: R = ScalarizeVecRes_SELECT(N); break;
52 case ISD::SELECT_CC: R = ScalarizeVecRes_SELECT_CC(N); break;
53 case ISD::UNDEF: R = ScalarizeVecRes_UNDEF(N); break;
54 case ISD::VECTOR_SHUFFLE: R = ScalarizeVecRes_VECTOR_SHUFFLE(N); break;
55 case ISD::VSETCC: R = ScalarizeVecRes_VSETCC(N); break;
74 case ISD::UINT_TO_FP: R = ScalarizeVecRes_UnaryOp(N); break;
91 case ISD::XOR: R = ScalarizeVecRes_BinOp(N); break;
94 // If R is null, the sub-method took care of registering the result.
96 SetScalarizedVector(SDValue(N, ResNo), R);
99 SDValue DAGTypeLegalizer::ScalarizeVecRes_BinOp(SDNode *N) {
100 SDValue LHS = GetScalarizedVector(N->getOperand(0));
101 SDValue RHS = GetScalarizedVector(N->getOperand(1));
102 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
105 SDValue DAGTypeLegalizer::ScalarizeVecRes_BIT_CONVERT(SDNode *N) {
106 MVT NewVT = N->getValueType(0).getVectorElementType();
107 return DAG.getNode(ISD::BIT_CONVERT, NewVT, N->getOperand(0));
110 SDValue DAGTypeLegalizer::ScalarizeVecRes_CONVERT_RNDSAT(SDNode *N) {
111 MVT NewVT = N->getValueType(0).getVectorElementType();
112 SDValue Op0 = GetScalarizedVector(N->getOperand(0));
113 return DAG.getConvertRndSat(NewVT, Op0, DAG.getValueType(NewVT),
114 DAG.getValueType(Op0.getValueType()),
117 cast<CvtRndSatSDNode>(N)->getCvtCode());
120 SDValue DAGTypeLegalizer::ScalarizeVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
121 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
122 N->getValueType(0).getVectorElementType(),
123 N->getOperand(0), N->getOperand(1));
126 SDValue DAGTypeLegalizer::ScalarizeVecRes_FPOWI(SDNode *N) {
127 SDValue Op = GetScalarizedVector(N->getOperand(0));
128 return DAG.getNode(ISD::FPOWI, Op.getValueType(), Op, N->getOperand(1));
131 SDValue DAGTypeLegalizer::ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N) {
132 // The value to insert may have a wider type than the vector element type,
133 // so be sure to truncate it to the element type if necessary.
134 SDValue Op = N->getOperand(1);
135 MVT EltVT = N->getValueType(0).getVectorElementType();
136 if (Op.getValueType() != EltVT)
137 // FIXME: Can this happen for floating point types?
138 Op = DAG.getNode(ISD::TRUNCATE, EltVT, Op);
142 SDValue DAGTypeLegalizer::ScalarizeVecRes_LOAD(LoadSDNode *N) {
143 assert(N->isUnindexed() && "Indexed vector load?");
145 SDValue Result = DAG.getLoad(ISD::UNINDEXED, N->getExtensionType(),
146 N->getValueType(0).getVectorElementType(),
147 N->getChain(), N->getBasePtr(),
148 DAG.getNode(ISD::UNDEF,
149 N->getBasePtr().getValueType()),
150 N->getSrcValue(), N->getSrcValueOffset(),
151 N->getMemoryVT().getVectorElementType(),
152 N->isVolatile(), N->getAlignment());
154 // Legalized the chain result - switch anything that used the old chain to
156 ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
160 SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOp(SDNode *N) {
161 // Get the dest type - it doesn't always match the input type, e.g. int_to_fp.
162 MVT DestVT = N->getValueType(0).getVectorElementType();
163 SDValue Op = GetScalarizedVector(N->getOperand(0));
164 return DAG.getNode(N->getOpcode(), DestVT, Op);
167 SDValue DAGTypeLegalizer::ScalarizeVecRes_UNDEF(SDNode *N) {
168 return DAG.getNode(ISD::UNDEF, N->getValueType(0).getVectorElementType());
171 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT(SDNode *N) {
172 SDValue LHS = GetScalarizedVector(N->getOperand(1));
173 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0), LHS,
174 GetScalarizedVector(N->getOperand(2)));
177 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT_CC(SDNode *N) {
178 SDValue LHS = GetScalarizedVector(N->getOperand(2));
179 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(),
180 N->getOperand(0), N->getOperand(1),
181 LHS, GetScalarizedVector(N->getOperand(3)),
185 SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_SHUFFLE(SDNode *N) {
186 // Figure out if the scalar is the LHS or RHS and return it.
187 SDValue Arg = N->getOperand(2).getOperand(0);
188 if (Arg.getOpcode() == ISD::UNDEF)
189 return DAG.getNode(ISD::UNDEF, N->getValueType(0).getVectorElementType());
190 unsigned Op = !cast<ConstantSDNode>(Arg)->isNullValue();
191 return GetScalarizedVector(N->getOperand(Op));
194 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSETCC(SDNode *N) {
195 SDValue LHS = GetScalarizedVector(N->getOperand(0));
196 SDValue RHS = GetScalarizedVector(N->getOperand(1));
197 MVT NVT = N->getValueType(0).getVectorElementType();
198 MVT SVT = TLI.getSetCCResultType(LHS);
200 // Turn it into a scalar SETCC.
201 SDValue Res = DAG.getNode(ISD::SETCC, SVT, LHS, RHS, N->getOperand(2));
203 // VSETCC always returns a sign-extended value, while SETCC may not. The
204 // SETCC result type may not match the vector element type. Correct these.
205 if (NVT.bitsLE(SVT)) {
206 // The SETCC result type is bigger than the vector element type.
207 // Ensure the SETCC result is sign-extended.
208 if (TLI.getSetCCResultContents() !=
209 TargetLowering::ZeroOrNegativeOneSetCCResult)
210 Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, SVT, Res,
211 DAG.getValueType(MVT::i1));
212 // Truncate to the final type.
213 return DAG.getNode(ISD::TRUNCATE, NVT, Res);
215 // The SETCC result type is smaller than the vector element type.
216 // If the SetCC result is not sign-extended, chop it down to MVT::i1.
217 if (TLI.getSetCCResultContents() !=
218 TargetLowering::ZeroOrNegativeOneSetCCResult)
219 Res = DAG.getNode(ISD::TRUNCATE, MVT::i1, Res);
220 // Sign extend to the final type.
221 return DAG.getNode(ISD::SIGN_EXTEND, NVT, Res);
226 //===----------------------------------------------------------------------===//
227 // Operand Vector Scalarization <1 x ty> -> ty.
228 //===----------------------------------------------------------------------===//
230 bool DAGTypeLegalizer::ScalarizeVectorOperand(SDNode *N, unsigned OpNo) {
231 DEBUG(cerr << "Scalarize node operand " << OpNo << ": "; N->dump(&DAG);
233 SDValue Res = SDValue();
235 if (Res.getNode() == 0) {
236 switch (N->getOpcode()) {
239 cerr << "ScalarizeVectorOperand Op #" << OpNo << ": ";
240 N->dump(&DAG); cerr << "\n";
242 assert(0 && "Do not know how to scalarize this operator's operand!");
245 case ISD::BIT_CONVERT:
246 Res = ScalarizeVecOp_BIT_CONVERT(N); break;
248 case ISD::CONCAT_VECTORS:
249 Res = ScalarizeVecOp_CONCAT_VECTORS(N); break;
251 case ISD::EXTRACT_VECTOR_ELT:
252 Res = ScalarizeVecOp_EXTRACT_VECTOR_ELT(N); break;
255 Res = ScalarizeVecOp_STORE(cast<StoreSDNode>(N), OpNo); break;
259 // If the result is null, the sub-method took care of registering results etc.
260 if (!Res.getNode()) return false;
262 // If the result is N, the sub-method updated N in place. Check to see if any
263 // operands are new, and if so, mark them.
264 if (Res.getNode() == N) {
265 // Mark N as new and remark N and its operands. This allows us to correctly
266 // revisit N if it needs another step of promotion and allows us to visit
267 // any new operands to N.
272 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
273 "Invalid operand expansion");
275 ReplaceValueWith(SDValue(N, 0), Res);
279 /// ScalarizeVecOp_BIT_CONVERT - If the value to convert is a vector that needs
280 /// to be scalarized, it must be <1 x ty>. Convert the element instead.
281 SDValue DAGTypeLegalizer::ScalarizeVecOp_BIT_CONVERT(SDNode *N) {
282 SDValue Elt = GetScalarizedVector(N->getOperand(0));
283 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Elt);
286 /// ScalarizeVecOp_CONCAT_VECTORS - The vectors to concatenate have length one -
287 /// use a BUILD_VECTOR instead.
288 SDValue DAGTypeLegalizer::ScalarizeVecOp_CONCAT_VECTORS(SDNode *N) {
289 SmallVector<SDValue, 8> Ops(N->getNumOperands());
290 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
291 Ops[i] = GetScalarizedVector(N->getOperand(i));
292 return DAG.getNode(ISD::BUILD_VECTOR, N->getValueType(0),
293 &Ops[0], Ops.size());
296 /// ScalarizeVecOp_EXTRACT_VECTOR_ELT - If the input is a vector that needs to
297 /// be scalarized, it must be <1 x ty>, so just return the element, ignoring the
299 SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
300 return GetScalarizedVector(N->getOperand(0));
303 /// ScalarizeVecOp_STORE - If the value to store is a vector that needs to be
304 /// scalarized, it must be <1 x ty>. Just store the element.
305 SDValue DAGTypeLegalizer::ScalarizeVecOp_STORE(StoreSDNode *N, unsigned OpNo){
306 assert(N->isUnindexed() && "Indexed store of one-element vector?");
307 assert(OpNo == 1 && "Do not know how to scalarize this operand!");
309 if (N->isTruncatingStore())
310 return DAG.getTruncStore(N->getChain(),
311 GetScalarizedVector(N->getOperand(1)),
313 N->getSrcValue(), N->getSrcValueOffset(),
314 N->getMemoryVT().getVectorElementType(),
315 N->isVolatile(), N->getAlignment());
317 return DAG.getStore(N->getChain(), GetScalarizedVector(N->getOperand(1)),
318 N->getBasePtr(), N->getSrcValue(), N->getSrcValueOffset(),
319 N->isVolatile(), N->getAlignment());
323 //===----------------------------------------------------------------------===//
324 // Result Vector Splitting
325 //===----------------------------------------------------------------------===//
327 /// SplitVectorResult - This method is called when the specified result of the
328 /// specified node is found to need vector splitting. At this point, the node
329 /// may also have invalid operands or may have other results that need
330 /// legalization, we just know that (at least) one result needs vector
332 void DAGTypeLegalizer::SplitVectorResult(SDNode *N, unsigned ResNo) {
333 DEBUG(cerr << "Split node result: "; N->dump(&DAG); cerr << "\n");
336 switch (N->getOpcode()) {
339 cerr << "SplitVectorResult #" << ResNo << ": ";
340 N->dump(&DAG); cerr << "\n";
342 assert(0 && "Do not know how to split the result of this operator!");
345 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
346 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
347 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
348 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
350 case ISD::BIT_CONVERT: SplitVecRes_BIT_CONVERT(N, Lo, Hi); break;
351 case ISD::BUILD_VECTOR: SplitVecRes_BUILD_VECTOR(N, Lo, Hi); break;
352 case ISD::CONCAT_VECTORS: SplitVecRes_CONCAT_VECTORS(N, Lo, Hi); break;
353 case ISD::CONVERT_RNDSAT: SplitVecRes_CONVERT_RNDSAT(N, Lo, Hi); break;
354 case ISD::EXTRACT_SUBVECTOR: SplitVecRes_EXTRACT_SUBVECTOR(N, Lo, Hi); break;
355 case ISD::FPOWI: SplitVecRes_FPOWI(N, Lo, Hi); break;
356 case ISD::INSERT_VECTOR_ELT: SplitVecRes_INSERT_VECTOR_ELT(N, Lo, Hi); break;
357 case ISD::LOAD: SplitVecRes_LOAD(cast<LoadSDNode>(N), Lo, Hi);break;
358 case ISD::VECTOR_SHUFFLE: SplitVecRes_VECTOR_SHUFFLE(N, Lo, Hi); break;
359 case ISD::VSETCC: SplitVecRes_VSETCC(N, Lo, Hi); break;
373 case ISD::FNEARBYINT:
374 case ISD::FP_TO_SINT:
375 case ISD::FP_TO_UINT:
376 case ISD::SINT_TO_FP:
378 case ISD::UINT_TO_FP: SplitVecRes_UnaryOp(N, Lo, Hi); break;
395 case ISD::FREM: SplitVecRes_BinOp(N, Lo, Hi); break;
398 // If Lo/Hi is null, the sub-method took care of registering results etc.
400 SetSplitVector(SDValue(N, ResNo), Lo, Hi);
403 void DAGTypeLegalizer::SplitVecRes_BinOp(SDNode *N, SDValue &Lo,
405 SDValue LHSLo, LHSHi;
406 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
407 SDValue RHSLo, RHSHi;
408 GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
410 Lo = DAG.getNode(N->getOpcode(), LHSLo.getValueType(), LHSLo, RHSLo);
411 Hi = DAG.getNode(N->getOpcode(), LHSHi.getValueType(), LHSHi, RHSHi);
414 void DAGTypeLegalizer::SplitVecRes_BIT_CONVERT(SDNode *N, SDValue &Lo,
416 // We know the result is a vector. The input may be either a vector or a
419 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
421 SDValue InOp = N->getOperand(0);
422 MVT InVT = InOp.getValueType();
424 // Handle some special cases efficiently.
425 switch (getTypeAction(InVT)) {
427 assert(false && "Unknown type action!");
431 case ScalarizeVector:
435 // A scalar to vector conversion, where the scalar needs expansion.
436 // If the vector is being split in two then we can just convert the
439 GetExpandedOp(InOp, Lo, Hi);
440 if (TLI.isBigEndian())
442 Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
443 Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
448 // If the input is a vector that needs to be split, convert each split
449 // piece of the input now.
450 GetSplitVector(InOp, Lo, Hi);
451 Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
452 Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
456 // In the general case, convert the input to an integer and split it by hand.
457 MVT LoIntVT = MVT::getIntegerVT(LoVT.getSizeInBits());
458 MVT HiIntVT = MVT::getIntegerVT(HiVT.getSizeInBits());
459 if (TLI.isBigEndian())
460 std::swap(LoIntVT, HiIntVT);
462 SplitInteger(BitConvertToInteger(InOp), LoIntVT, HiIntVT, Lo, Hi);
464 if (TLI.isBigEndian())
466 Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
467 Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
470 void DAGTypeLegalizer::SplitVecRes_BUILD_VECTOR(SDNode *N, SDValue &Lo,
473 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
474 unsigned LoNumElts = LoVT.getVectorNumElements();
475 SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+LoNumElts);
476 Lo = DAG.getNode(ISD::BUILD_VECTOR, LoVT, &LoOps[0], LoOps.size());
478 SmallVector<SDValue, 8> HiOps(N->op_begin()+LoNumElts, N->op_end());
479 Hi = DAG.getNode(ISD::BUILD_VECTOR, HiVT, &HiOps[0], HiOps.size());
482 void DAGTypeLegalizer::SplitVecRes_CONCAT_VECTORS(SDNode *N, SDValue &Lo,
484 assert(!(N->getNumOperands() & 1) && "Unsupported CONCAT_VECTORS");
485 unsigned NumSubvectors = N->getNumOperands() / 2;
486 if (NumSubvectors == 1) {
487 Lo = N->getOperand(0);
488 Hi = N->getOperand(1);
493 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
495 SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+NumSubvectors);
496 Lo = DAG.getNode(ISD::CONCAT_VECTORS, LoVT, &LoOps[0], LoOps.size());
498 SmallVector<SDValue, 8> HiOps(N->op_begin()+NumSubvectors, N->op_end());
499 Hi = DAG.getNode(ISD::CONCAT_VECTORS, HiVT, &HiOps[0], HiOps.size());
502 void DAGTypeLegalizer::SplitVecRes_CONVERT_RNDSAT(SDNode *N, SDValue &Lo,
505 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
507 GetSplitVector(N->getOperand(0), VLo, VHi);
508 SDValue DTyOpLo = DAG.getValueType(LoVT);
509 SDValue DTyOpHi = DAG.getValueType(HiVT);
510 SDValue STyOpLo = DAG.getValueType(VLo.getValueType());
511 SDValue STyOpHi = DAG.getValueType(VHi.getValueType());
513 SDValue RndOp = N->getOperand(3);
514 SDValue SatOp = N->getOperand(4);
515 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
517 Lo = DAG.getConvertRndSat(LoVT, VLo, DTyOpLo, STyOpLo, RndOp, SatOp, CvtCode);
518 Hi = DAG.getConvertRndSat(HiVT, VHi, DTyOpHi, STyOpHi, RndOp, SatOp, CvtCode);
521 void DAGTypeLegalizer::SplitVecRes_EXTRACT_SUBVECTOR(SDNode *N, SDValue &Lo,
523 SDValue Vec = N->getOperand(0);
524 SDValue Idx = N->getOperand(1);
525 MVT IdxVT = Idx.getValueType();
528 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
529 // The indices are not guaranteed to be a multiple of the new vector
530 // size unless the original vector type was split in two.
531 assert(LoVT == HiVT && "Non power-of-two vectors not supported!");
533 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, LoVT, Vec, Idx);
534 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
535 DAG.getConstant(LoVT.getVectorNumElements(), IdxVT));
536 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, HiVT, Vec, Idx);
539 void DAGTypeLegalizer::SplitVecRes_FPOWI(SDNode *N, SDValue &Lo,
541 GetSplitVector(N->getOperand(0), Lo, Hi);
542 Lo = DAG.getNode(ISD::FPOWI, Lo.getValueType(), Lo, N->getOperand(1));
543 Hi = DAG.getNode(ISD::FPOWI, Hi.getValueType(), Hi, N->getOperand(1));
546 void DAGTypeLegalizer::SplitVecRes_INSERT_VECTOR_ELT(SDNode *N, SDValue &Lo,
548 SDValue Vec = N->getOperand(0);
549 SDValue Elt = N->getOperand(1);
550 SDValue Idx = N->getOperand(2);
551 GetSplitVector(Vec, Lo, Hi);
553 if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
554 unsigned IdxVal = CIdx->getZExtValue();
555 unsigned LoNumElts = Lo.getValueType().getVectorNumElements();
556 if (IdxVal < LoNumElts)
557 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, Lo.getValueType(), Lo, Elt, Idx);
559 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, Hi.getValueType(), Hi, Elt,
560 DAG.getIntPtrConstant(IdxVal - LoNumElts));
564 // Spill the vector to the stack.
565 MVT VecVT = Vec.getValueType();
566 MVT EltVT = VecVT.getVectorElementType();
567 SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
568 SDValue Store = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
570 // Store the new element. This may be larger than the vector element type,
571 // so use a truncating store.
572 SDValue EltPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
573 Store = DAG.getTruncStore(Store, Elt, EltPtr, NULL, 0, EltVT);
575 // Reload the vector from the stack.
576 SDValue Load = DAG.getLoad(VecVT, Store, StackPtr, NULL, 0);
579 SplitVecRes_LOAD(cast<LoadSDNode>(Load.getNode()), Lo, Hi);
582 void DAGTypeLegalizer::SplitVecRes_LOAD(LoadSDNode *LD, SDValue &Lo,
584 assert(ISD::isUNINDEXEDLoad(LD) && "Indexed load during type legalization!");
586 GetSplitDestVTs(LD->getValueType(0), LoVT, HiVT);
588 ISD::LoadExtType ExtType = LD->getExtensionType();
589 SDValue Ch = LD->getChain();
590 SDValue Ptr = LD->getBasePtr();
591 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
592 const Value *SV = LD->getSrcValue();
593 int SVOffset = LD->getSrcValueOffset();
594 MVT MemoryVT = LD->getMemoryVT();
595 unsigned Alignment = LD->getAlignment();
596 bool isVolatile = LD->isVolatile();
598 MVT LoMemVT, HiMemVT;
599 GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
601 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType, LoVT, Ch, Ptr, Offset,
602 SV, SVOffset, LoMemVT, isVolatile, Alignment);
604 unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
605 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
606 DAG.getIntPtrConstant(IncrementSize));
607 SVOffset += IncrementSize;
608 Alignment = MinAlign(Alignment, IncrementSize);
609 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType, HiVT, Ch, Ptr, Offset,
610 SV, SVOffset, HiMemVT, isVolatile, Alignment);
612 // Build a factor node to remember that this load is independent of the
614 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
617 // Legalized the chain result - switch anything that used the old chain to
619 ReplaceValueWith(SDValue(LD, 1), Ch);
622 void DAGTypeLegalizer::SplitVecRes_UnaryOp(SDNode *N, SDValue &Lo,
624 // Get the dest types - they may not match the input types, e.g. int_to_fp.
626 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
629 MVT InVT = N->getOperand(0).getValueType();
630 switch (getTypeAction(InVT)) {
631 default: assert(0 && "Unexpected type action!");
633 assert(LoVT == HiVT && "Legal non-power-of-two vector type?");
634 MVT InNVT = MVT::getVectorVT(InVT.getVectorElementType(),
635 LoVT.getVectorNumElements());
636 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, InNVT, N->getOperand(0),
637 DAG.getIntPtrConstant(0));
638 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, InNVT, N->getOperand(0),
639 DAG.getIntPtrConstant(InNVT.getVectorNumElements()));
643 GetSplitVector(N->getOperand(0), Lo, Hi);
647 Lo = DAG.getNode(N->getOpcode(), LoVT, Lo);
648 Hi = DAG.getNode(N->getOpcode(), HiVT, Hi);
651 void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(SDNode *N, SDValue &Lo,
653 // Build the low part.
654 SDValue Mask = N->getOperand(2);
655 SmallVector<SDValue, 16> Ops;
657 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
658 MVT EltVT = LoVT.getVectorElementType();
659 unsigned LoNumElts = LoVT.getVectorNumElements();
660 unsigned NumElements = Mask.getNumOperands();
662 // Insert all of the elements from the input that are needed. We use
663 // buildvector of extractelement here because the input vectors will have
664 // to be legalized, so this makes the code simpler.
665 for (unsigned i = 0; i != LoNumElts; ++i) {
666 SDValue Arg = Mask.getOperand(i);
667 if (Arg.getOpcode() == ISD::UNDEF) {
668 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
670 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getZExtValue();
671 SDValue InVec = N->getOperand(0);
672 if (Idx >= NumElements) {
673 InVec = N->getOperand(1);
676 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, InVec,
677 DAG.getIntPtrConstant(Idx)));
680 Lo = DAG.getNode(ISD::BUILD_VECTOR, LoVT, &Ops[0], Ops.size());
683 for (unsigned i = LoNumElts; i != NumElements; ++i) {
684 SDValue Arg = Mask.getOperand(i);
685 if (Arg.getOpcode() == ISD::UNDEF) {
686 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
688 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getZExtValue();
689 SDValue InVec = N->getOperand(0);
690 if (Idx >= NumElements) {
691 InVec = N->getOperand(1);
694 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, InVec,
695 DAG.getIntPtrConstant(Idx)));
698 Hi = DAG.getNode(ISD::BUILD_VECTOR, HiVT, &Ops[0], Ops.size());
701 void DAGTypeLegalizer::SplitVecRes_VSETCC(SDNode *N, SDValue &Lo,
704 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
706 SDValue LL, LH, RL, RH;
707 GetSplitVector(N->getOperand(0), LL, LH);
708 GetSplitVector(N->getOperand(1), RL, RH);
710 Lo = DAG.getNode(ISD::VSETCC, LoVT, LL, RL, N->getOperand(2));
711 Hi = DAG.getNode(ISD::VSETCC, HiVT, LH, RH, N->getOperand(2));
715 //===----------------------------------------------------------------------===//
716 // Operand Vector Splitting
717 //===----------------------------------------------------------------------===//
719 /// SplitVectorOperand - This method is called when the specified operand of the
720 /// specified node is found to need vector splitting. At this point, all of the
721 /// result types of the node are known to be legal, but other operands of the
722 /// node may need legalization as well as the specified one.
723 bool DAGTypeLegalizer::SplitVectorOperand(SDNode *N, unsigned OpNo) {
724 DEBUG(cerr << "Split node operand: "; N->dump(&DAG); cerr << "\n");
725 SDValue Res = SDValue();
727 if (Res.getNode() == 0) {
728 switch (N->getOpcode()) {
731 cerr << "SplitVectorOperand Op #" << OpNo << ": ";
732 N->dump(&DAG); cerr << "\n";
734 assert(0 && "Do not know how to split this operator's operand!");
737 case ISD::BIT_CONVERT: Res = SplitVecOp_BIT_CONVERT(N); break;
738 case ISD::EXTRACT_SUBVECTOR: Res = SplitVecOp_EXTRACT_SUBVECTOR(N); break;
739 case ISD::EXTRACT_VECTOR_ELT:Res = SplitVecOp_EXTRACT_VECTOR_ELT(N); break;
740 case ISD::STORE: Res = SplitVecOp_STORE(cast<StoreSDNode>(N),
742 case ISD::VECTOR_SHUFFLE: Res = SplitVecOp_VECTOR_SHUFFLE(N, OpNo);break;
747 case ISD::FP_TO_SINT:
748 case ISD::FP_TO_UINT:
749 case ISD::SINT_TO_FP:
751 case ISD::UINT_TO_FP: Res = SplitVecOp_UnaryOp(N); break;
755 // If the result is null, the sub-method took care of registering results etc.
756 if (!Res.getNode()) return false;
758 // If the result is N, the sub-method updated N in place. Check to see if any
759 // operands are new, and if so, mark them.
760 if (Res.getNode() == N) {
761 // Mark N as new and remark N and its operands. This allows us to correctly
762 // revisit N if it needs another step of promotion and allows us to visit
763 // any new operands to N.
768 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
769 "Invalid operand expansion");
771 ReplaceValueWith(SDValue(N, 0), Res);
775 SDValue DAGTypeLegalizer::SplitVecOp_UnaryOp(SDNode *N) {
776 // The result has a legal vector type, but the input needs splitting.
777 MVT ResVT = N->getValueType(0);
779 GetSplitVector(N->getOperand(0), Lo, Hi);
780 assert(Lo.getValueType() == Hi.getValueType() &&
781 "Returns legal non-power-of-two vector type?");
782 MVT InVT = Lo.getValueType();
784 MVT OutVT = MVT::getVectorVT(ResVT.getVectorElementType(),
785 InVT.getVectorNumElements());
787 Lo = DAG.getNode(N->getOpcode(), OutVT, Lo);
788 Hi = DAG.getNode(N->getOpcode(), OutVT, Hi);
790 return DAG.getNode(ISD::CONCAT_VECTORS, ResVT, Lo, Hi);
793 SDValue DAGTypeLegalizer::SplitVecOp_BIT_CONVERT(SDNode *N) {
794 // For example, i64 = BIT_CONVERT v4i16 on alpha. Typically the vector will
795 // end up being split all the way down to individual components. Convert the
796 // split pieces into integers and reassemble.
798 GetSplitVector(N->getOperand(0), Lo, Hi);
799 Lo = BitConvertToInteger(Lo);
800 Hi = BitConvertToInteger(Hi);
802 if (TLI.isBigEndian())
805 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
806 JoinIntegers(Lo, Hi));
809 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
810 // We know that the extracted result type is legal. For now, assume the index
812 MVT SubVT = N->getValueType(0);
813 SDValue Idx = N->getOperand(1);
815 GetSplitVector(N->getOperand(0), Lo, Hi);
817 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
818 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
820 if (IdxVal < LoElts) {
821 assert(IdxVal + SubVT.getVectorNumElements() <= LoElts &&
822 "Extracted subvector crosses vector split!");
823 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SubVT, Lo, Idx);
825 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SubVT, Hi,
826 DAG.getConstant(IdxVal - LoElts, Idx.getValueType()));
830 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
831 SDValue Vec = N->getOperand(0);
832 SDValue Idx = N->getOperand(1);
833 MVT VecVT = Vec.getValueType();
835 if (isa<ConstantSDNode>(Idx)) {
836 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
837 assert(IdxVal < VecVT.getVectorNumElements() && "Invalid vector index!");
840 GetSplitVector(Vec, Lo, Hi);
842 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
845 return DAG.UpdateNodeOperands(SDValue(N, 0), Lo, Idx);
847 return DAG.UpdateNodeOperands(SDValue(N, 0), Hi,
848 DAG.getConstant(IdxVal - LoElts,
849 Idx.getValueType()));
852 // Store the vector to the stack.
853 MVT EltVT = VecVT.getVectorElementType();
854 SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
855 SDValue Store = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
857 // Load back the required element.
858 StackPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
859 return DAG.getLoad(EltVT, Store, StackPtr, NULL, 0);
862 SDValue DAGTypeLegalizer::SplitVecOp_STORE(StoreSDNode *N, unsigned OpNo) {
863 assert(N->isUnindexed() && "Indexed store of vector?");
864 assert(OpNo == 1 && "Can only split the stored value");
866 bool isTruncating = N->isTruncatingStore();
867 SDValue Ch = N->getChain();
868 SDValue Ptr = N->getBasePtr();
869 int SVOffset = N->getSrcValueOffset();
870 MVT MemoryVT = N->getMemoryVT();
871 unsigned Alignment = N->getAlignment();
872 bool isVol = N->isVolatile();
874 GetSplitVector(N->getOperand(1), Lo, Hi);
876 MVT LoMemVT, HiMemVT;
877 GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
879 unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
882 Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
883 LoMemVT, isVol, Alignment);
885 Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
888 // Increment the pointer to the other half.
889 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
890 DAG.getIntPtrConstant(IncrementSize));
893 Hi = DAG.getTruncStore(Ch, Hi, Ptr,
894 N->getSrcValue(), SVOffset+IncrementSize,
896 isVol, MinAlign(Alignment, IncrementSize));
898 Hi = DAG.getStore(Ch, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
899 isVol, MinAlign(Alignment, IncrementSize));
901 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
904 SDValue DAGTypeLegalizer::SplitVecOp_VECTOR_SHUFFLE(SDNode *N, unsigned OpNo) {
905 assert(OpNo == 2 && "Shuffle source type differs from result type?");
906 SDValue Mask = N->getOperand(2);
907 unsigned MaskLength = Mask.getValueType().getVectorNumElements();
908 unsigned LargestMaskEntryPlusOne = 2 * MaskLength;
909 unsigned MinimumBitWidth = Log2_32_Ceil(LargestMaskEntryPlusOne);
911 // Look for a legal vector type to place the mask values in.
912 // Note that there may not be *any* legal vector-of-integer
913 // type for which the element type is legal!
914 for (MVT::SimpleValueType EltVT = MVT::FIRST_INTEGER_VALUETYPE;
915 EltVT <= MVT::LAST_INTEGER_VALUETYPE;
916 // Integer values types are consecutively numbered. Exploit this.
917 EltVT = MVT::SimpleValueType(EltVT + 1)) {
919 // Is the element type big enough to hold the values?
920 if (MVT(EltVT).getSizeInBits() < MinimumBitWidth)
924 // Is the vector type legal?
925 MVT VecVT = MVT::getVectorVT(EltVT, MaskLength);
926 if (!isTypeLegal(VecVT))
930 // If the element type is not legal, find a larger legal type to use for
931 // the BUILD_VECTOR operands. This is an ugly hack, but seems to work!
932 // FIXME: The real solution is to change VECTOR_SHUFFLE into a variadic
933 // node where the shuffle mask is a list of integer operands, #2 .. #2+n.
934 for (MVT::SimpleValueType OpVT = EltVT; OpVT <= MVT::LAST_INTEGER_VALUETYPE;
935 // Integer values types are consecutively numbered. Exploit this.
936 OpVT = MVT::SimpleValueType(OpVT + 1)) {
937 if (!isTypeLegal(OpVT))
940 // Success! Rebuild the vector using the legal types.
941 SmallVector<SDValue, 16> Ops(MaskLength);
942 for (unsigned i = 0; i < MaskLength; ++i) {
943 SDValue Arg = Mask.getOperand(i);
944 if (Arg.getOpcode() == ISD::UNDEF) {
945 Ops[i] = DAG.getNode(ISD::UNDEF, OpVT);
947 uint64_t Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
948 Ops[i] = DAG.getConstant(Idx, OpVT);
951 return DAG.UpdateNodeOperands(SDValue(N,0),
952 N->getOperand(0), N->getOperand(1),
953 DAG.getNode(ISD::BUILD_VECTOR,
954 VecVT, &Ops[0], Ops.size()));
957 // Continuing is pointless - failure is certain.
960 assert(false && "Failed to find an appropriate mask type!");
961 return SDValue(N, 0);