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::EXTRACT_SUBVECTOR: R = ScalarizeVecRes_EXTRACT_SUBVECTOR(N); break;
47 case ISD::FPOWI: R = ScalarizeVecRes_FPOWI(N); break;
48 case ISD::INSERT_VECTOR_ELT: R = ScalarizeVecRes_INSERT_VECTOR_ELT(N); break;
49 case ISD::LOAD: R = ScalarizeVecRes_LOAD(cast<LoadSDNode>(N));break;
50 case ISD::SELECT: R = ScalarizeVecRes_SELECT(N); break;
51 case ISD::SELECT_CC: R = ScalarizeVecRes_SELECT_CC(N); break;
52 case ISD::UNDEF: R = ScalarizeVecRes_UNDEF(N); break;
53 case ISD::VECTOR_SHUFFLE: R = ScalarizeVecRes_VECTOR_SHUFFLE(N); break;
54 case ISD::VSETCC: R = ScalarizeVecRes_VSETCC(N); break;
73 case ISD::UINT_TO_FP: R = ScalarizeVecRes_UnaryOp(N); break;
90 case ISD::XOR: R = ScalarizeVecRes_BinOp(N); break;
93 // If R is null, the sub-method took care of registering the result.
95 SetScalarizedVector(SDValue(N, ResNo), R);
98 SDValue DAGTypeLegalizer::ScalarizeVecRes_BinOp(SDNode *N) {
99 SDValue LHS = GetScalarizedVector(N->getOperand(0));
100 SDValue RHS = GetScalarizedVector(N->getOperand(1));
101 return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
104 SDValue DAGTypeLegalizer::ScalarizeVecRes_BIT_CONVERT(SDNode *N) {
105 MVT NewVT = N->getValueType(0).getVectorElementType();
106 return DAG.getNode(ISD::BIT_CONVERT, NewVT, N->getOperand(0));
109 SDValue DAGTypeLegalizer::ScalarizeVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
110 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
111 N->getValueType(0).getVectorElementType(),
112 N->getOperand(0), N->getOperand(1));
115 SDValue DAGTypeLegalizer::ScalarizeVecRes_FPOWI(SDNode *N) {
116 SDValue Op = GetScalarizedVector(N->getOperand(0));
117 return DAG.getNode(ISD::FPOWI, Op.getValueType(), Op, N->getOperand(1));
120 SDValue DAGTypeLegalizer::ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N) {
121 // The value to insert may have a wider type than the vector element type,
122 // so be sure to truncate it to the element type if necessary.
123 SDValue Op = N->getOperand(1);
124 MVT EltVT = N->getValueType(0).getVectorElementType();
125 if (Op.getValueType() != EltVT)
126 // FIXME: Can this happen for floating point types?
127 Op = DAG.getNode(ISD::TRUNCATE, EltVT, Op);
131 SDValue DAGTypeLegalizer::ScalarizeVecRes_LOAD(LoadSDNode *N) {
132 assert(N->isUnindexed() && "Indexed vector load?");
134 SDValue Result = DAG.getLoad(ISD::UNINDEXED, N->getExtensionType(),
135 N->getValueType(0).getVectorElementType(),
136 N->getChain(), N->getBasePtr(),
137 DAG.getNode(ISD::UNDEF,
138 N->getBasePtr().getValueType()),
139 N->getSrcValue(), N->getSrcValueOffset(),
140 N->getMemoryVT().getVectorElementType(),
141 N->isVolatile(), N->getAlignment());
143 // Legalized the chain result - switch anything that used the old chain to
145 ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
149 SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOp(SDNode *N) {
150 // Get the dest type - it doesn't always match the input type, e.g. int_to_fp.
151 MVT DestVT = N->getValueType(0).getVectorElementType();
152 SDValue Op = GetScalarizedVector(N->getOperand(0));
153 return DAG.getNode(N->getOpcode(), DestVT, Op);
156 SDValue DAGTypeLegalizer::ScalarizeVecRes_UNDEF(SDNode *N) {
157 return DAG.getNode(ISD::UNDEF, N->getValueType(0).getVectorElementType());
160 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT(SDNode *N) {
161 SDValue LHS = GetScalarizedVector(N->getOperand(1));
162 return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0), LHS,
163 GetScalarizedVector(N->getOperand(2)));
166 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT_CC(SDNode *N) {
167 SDValue LHS = GetScalarizedVector(N->getOperand(2));
168 return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(),
169 N->getOperand(0), N->getOperand(1),
170 LHS, GetScalarizedVector(N->getOperand(3)),
174 SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_SHUFFLE(SDNode *N) {
175 // Figure out if the scalar is the LHS or RHS and return it.
176 SDValue Arg = N->getOperand(2).getOperand(0);
177 if (Arg.getOpcode() == ISD::UNDEF)
178 return DAG.getNode(ISD::UNDEF, N->getValueType(0).getVectorElementType());
179 unsigned Op = !cast<ConstantSDNode>(Arg)->isNullValue();
180 return GetScalarizedVector(N->getOperand(Op));
183 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSETCC(SDNode *N) {
184 SDValue LHS = GetScalarizedVector(N->getOperand(0));
185 SDValue RHS = GetScalarizedVector(N->getOperand(1));
186 MVT NVT = N->getValueType(0).getVectorElementType();
187 MVT SVT = TLI.getSetCCResultType(LHS);
189 // Turn it into a scalar SETCC.
190 SDValue Res = DAG.getNode(ISD::SETCC, SVT, LHS, RHS, N->getOperand(2));
192 // VSETCC always returns a sign-extended value, while SETCC may not. The
193 // SETCC result type may not match the vector element type. Correct these.
194 if (NVT.getSizeInBits() <= SVT.getSizeInBits()) {
195 // The SETCC result type is bigger than the vector element type.
196 // Ensure the SETCC result is sign-extended.
197 if (TLI.getSetCCResultContents() !=
198 TargetLowering::ZeroOrNegativeOneSetCCResult)
199 Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, SVT, Res,
200 DAG.getValueType(MVT::i1));
201 // Truncate to the final type.
202 return DAG.getNode(ISD::TRUNCATE, NVT, Res);
204 // The SETCC result type is smaller than the vector element type.
205 // If the SetCC result is not sign-extended, chop it down to MVT::i1.
206 if (TLI.getSetCCResultContents() !=
207 TargetLowering::ZeroOrNegativeOneSetCCResult)
208 Res = DAG.getNode(ISD::TRUNCATE, MVT::i1, Res);
209 // Sign extend to the final type.
210 return DAG.getNode(ISD::SIGN_EXTEND, NVT, Res);
215 //===----------------------------------------------------------------------===//
216 // Operand Vector Scalarization <1 x ty> -> ty.
217 //===----------------------------------------------------------------------===//
219 bool DAGTypeLegalizer::ScalarizeVectorOperand(SDNode *N, unsigned OpNo) {
220 DEBUG(cerr << "Scalarize node operand " << OpNo << ": "; N->dump(&DAG);
222 SDValue Res = SDValue();
224 if (Res.getNode() == 0) {
225 switch (N->getOpcode()) {
228 cerr << "ScalarizeVectorOperand Op #" << OpNo << ": ";
229 N->dump(&DAG); cerr << "\n";
231 assert(0 && "Do not know how to scalarize this operator's operand!");
234 case ISD::BIT_CONVERT:
235 Res = ScalarizeVecOp_BIT_CONVERT(N); break;
237 case ISD::CONCAT_VECTORS:
238 Res = ScalarizeVecOp_CONCAT_VECTORS(N); break;
240 case ISD::EXTRACT_VECTOR_ELT:
241 Res = ScalarizeVecOp_EXTRACT_VECTOR_ELT(N); break;
244 Res = ScalarizeVecOp_STORE(cast<StoreSDNode>(N), OpNo); break;
248 // If the result is null, the sub-method took care of registering results etc.
249 if (!Res.getNode()) return false;
251 // If the result is N, the sub-method updated N in place. Check to see if any
252 // operands are new, and if so, mark them.
253 if (Res.getNode() == N) {
254 // Mark N as new and remark N and its operands. This allows us to correctly
255 // revisit N if it needs another step of promotion and allows us to visit
256 // any new operands to N.
261 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
262 "Invalid operand expansion");
264 ReplaceValueWith(SDValue(N, 0), Res);
268 /// ScalarizeVecOp_BIT_CONVERT - If the value to convert is a vector that needs
269 /// to be scalarized, it must be <1 x ty>. Convert the element instead.
270 SDValue DAGTypeLegalizer::ScalarizeVecOp_BIT_CONVERT(SDNode *N) {
271 SDValue Elt = GetScalarizedVector(N->getOperand(0));
272 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Elt);
275 /// ScalarizeVecOp_CONCAT_VECTORS - The vectors to concatenate have length one -
276 /// use a BUILD_VECTOR instead.
277 SDValue DAGTypeLegalizer::ScalarizeVecOp_CONCAT_VECTORS(SDNode *N) {
278 SmallVector<SDValue, 8> Ops(N->getNumOperands());
279 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
280 Ops[i] = GetScalarizedVector(N->getOperand(i));
281 return DAG.getNode(ISD::BUILD_VECTOR, N->getValueType(0),
282 &Ops[0], Ops.size());
285 /// ScalarizeVecOp_EXTRACT_VECTOR_ELT - If the input is a vector that needs to
286 /// be scalarized, it must be <1 x ty>, so just return the element, ignoring the
288 SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
289 return GetScalarizedVector(N->getOperand(0));
292 /// ScalarizeVecOp_STORE - If the value to store is a vector that needs to be
293 /// scalarized, it must be <1 x ty>. Just store the element.
294 SDValue DAGTypeLegalizer::ScalarizeVecOp_STORE(StoreSDNode *N, unsigned OpNo){
295 assert(N->isUnindexed() && "Indexed store of one-element vector?");
296 assert(OpNo == 1 && "Do not know how to scalarize this operand!");
298 if (N->isTruncatingStore())
299 return DAG.getTruncStore(N->getChain(),
300 GetScalarizedVector(N->getOperand(1)),
302 N->getSrcValue(), N->getSrcValueOffset(),
303 N->getMemoryVT().getVectorElementType(),
304 N->isVolatile(), N->getAlignment());
306 return DAG.getStore(N->getChain(), GetScalarizedVector(N->getOperand(1)),
307 N->getBasePtr(), N->getSrcValue(), N->getSrcValueOffset(),
308 N->isVolatile(), N->getAlignment());
312 //===----------------------------------------------------------------------===//
313 // Result Vector Splitting
314 //===----------------------------------------------------------------------===//
316 /// SplitVectorResult - This method is called when the specified result of the
317 /// specified node is found to need vector splitting. At this point, the node
318 /// may also have invalid operands or may have other results that need
319 /// legalization, we just know that (at least) one result needs vector
321 void DAGTypeLegalizer::SplitVectorResult(SDNode *N, unsigned ResNo) {
322 DEBUG(cerr << "Split node result: "; N->dump(&DAG); cerr << "\n");
325 switch (N->getOpcode()) {
328 cerr << "SplitVectorResult #" << ResNo << ": ";
329 N->dump(&DAG); cerr << "\n";
331 assert(0 && "Do not know how to split the result of this operator!");
334 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
335 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
336 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
337 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
339 case ISD::BIT_CONVERT: SplitVecRes_BIT_CONVERT(N, Lo, Hi); break;
340 case ISD::BUILD_VECTOR: SplitVecRes_BUILD_VECTOR(N, Lo, Hi); break;
341 case ISD::CONCAT_VECTORS: SplitVecRes_CONCAT_VECTORS(N, Lo, Hi); break;
342 case ISD::EXTRACT_SUBVECTOR: SplitVecRes_EXTRACT_SUBVECTOR(N, Lo, Hi); break;
343 case ISD::FPOWI: SplitVecRes_FPOWI(N, Lo, Hi); break;
344 case ISD::INSERT_VECTOR_ELT: SplitVecRes_INSERT_VECTOR_ELT(N, Lo, Hi); break;
345 case ISD::LOAD: SplitVecRes_LOAD(cast<LoadSDNode>(N), Lo, Hi);break;
346 case ISD::VECTOR_SHUFFLE: SplitVecRes_VECTOR_SHUFFLE(N, Lo, Hi); break;
347 case ISD::VSETCC: SplitVecRes_VSETCC(N, Lo, Hi); break;
361 case ISD::FNEARBYINT:
362 case ISD::FP_TO_SINT:
363 case ISD::FP_TO_UINT:
364 case ISD::SINT_TO_FP:
366 case ISD::UINT_TO_FP: SplitVecRes_UnaryOp(N, Lo, Hi); break;
383 case ISD::FREM: SplitVecRes_BinOp(N, Lo, Hi); break;
386 // If Lo/Hi is null, the sub-method took care of registering results etc.
388 SetSplitVector(SDValue(N, ResNo), Lo, Hi);
391 void DAGTypeLegalizer::SplitVecRes_BinOp(SDNode *N, SDValue &Lo,
393 SDValue LHSLo, LHSHi;
394 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
395 SDValue RHSLo, RHSHi;
396 GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
398 Lo = DAG.getNode(N->getOpcode(), LHSLo.getValueType(), LHSLo, RHSLo);
399 Hi = DAG.getNode(N->getOpcode(), LHSHi.getValueType(), LHSHi, RHSHi);
402 void DAGTypeLegalizer::SplitVecRes_BIT_CONVERT(SDNode *N, SDValue &Lo,
404 // We know the result is a vector. The input may be either a vector or a
407 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
409 SDValue InOp = N->getOperand(0);
410 MVT InVT = InOp.getValueType();
412 // Handle some special cases efficiently.
413 switch (getTypeAction(InVT)) {
415 assert(false && "Unknown type action!");
419 case ScalarizeVector:
423 // A scalar to vector conversion, where the scalar needs expansion.
424 // If the vector is being split in two then we can just convert the
427 GetExpandedOp(InOp, Lo, Hi);
428 if (TLI.isBigEndian())
430 Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
431 Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
436 // If the input is a vector that needs to be split, convert each split
437 // piece of the input now.
438 GetSplitVector(InOp, Lo, Hi);
439 Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
440 Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
444 // In the general case, convert the input to an integer and split it by hand.
445 MVT LoIntVT = MVT::getIntegerVT(LoVT.getSizeInBits());
446 MVT HiIntVT = MVT::getIntegerVT(HiVT.getSizeInBits());
447 if (TLI.isBigEndian())
448 std::swap(LoIntVT, HiIntVT);
450 SplitInteger(BitConvertToInteger(InOp), LoIntVT, HiIntVT, Lo, Hi);
452 if (TLI.isBigEndian())
454 Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
455 Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
458 void DAGTypeLegalizer::SplitVecRes_BUILD_VECTOR(SDNode *N, SDValue &Lo,
461 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
462 unsigned LoNumElts = LoVT.getVectorNumElements();
463 SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+LoNumElts);
464 Lo = DAG.getNode(ISD::BUILD_VECTOR, LoVT, &LoOps[0], LoOps.size());
466 SmallVector<SDValue, 8> HiOps(N->op_begin()+LoNumElts, N->op_end());
467 Hi = DAG.getNode(ISD::BUILD_VECTOR, HiVT, &HiOps[0], HiOps.size());
470 void DAGTypeLegalizer::SplitVecRes_CONCAT_VECTORS(SDNode *N, SDValue &Lo,
472 assert(!(N->getNumOperands() & 1) && "Unsupported CONCAT_VECTORS");
473 unsigned NumSubvectors = N->getNumOperands() / 2;
474 if (NumSubvectors == 1) {
475 Lo = N->getOperand(0);
476 Hi = N->getOperand(1);
481 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
483 SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+NumSubvectors);
484 Lo = DAG.getNode(ISD::CONCAT_VECTORS, LoVT, &LoOps[0], LoOps.size());
486 SmallVector<SDValue, 8> HiOps(N->op_begin()+NumSubvectors, N->op_end());
487 Hi = DAG.getNode(ISD::CONCAT_VECTORS, HiVT, &HiOps[0], HiOps.size());
490 void DAGTypeLegalizer::SplitVecRes_EXTRACT_SUBVECTOR(SDNode *N, SDValue &Lo,
493 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
494 unsigned LoNumElts = LoVT.getVectorNumElements();
496 SDValue Vec = N->getOperand(0);
497 SDValue Idx = N->getOperand(1);
498 MVT IdxVT = Idx.getValueType();
499 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, LoVT, Vec, Idx);
501 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
503 unsigned IdxVal = CIdx->getZExtValue();
504 assert (IdxVal % LoVT.getVectorNumElements() == 0 &&
505 (IdxVal+LoNumElts) % HiVT.getVectorNumElements()==0 &&
506 "Index must be a multiple of the result type");
507 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, HiVT, Vec,
508 DAG.getConstant(IdxVal + LoNumElts, IdxVT));
510 assert(LoVT == HiVT && "Low and High value type should be the same");
511 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx, DAG.getConstant(LoNumElts, IdxVT));
512 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, HiVT, Vec, Idx);
516 void DAGTypeLegalizer::SplitVecRes_FPOWI(SDNode *N, SDValue &Lo,
518 GetSplitVector(N->getOperand(0), Lo, Hi);
519 Lo = DAG.getNode(ISD::FPOWI, Lo.getValueType(), Lo, N->getOperand(1));
520 Hi = DAG.getNode(ISD::FPOWI, Hi.getValueType(), Hi, N->getOperand(1));
523 void DAGTypeLegalizer::SplitVecRes_INSERT_VECTOR_ELT(SDNode *N, SDValue &Lo,
525 SDValue Vec = N->getOperand(0);
526 SDValue Elt = N->getOperand(1);
527 SDValue Idx = N->getOperand(2);
528 GetSplitVector(Vec, Lo, Hi);
530 if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
531 unsigned IdxVal = CIdx->getZExtValue();
532 unsigned LoNumElts = Lo.getValueType().getVectorNumElements();
533 if (IdxVal < LoNumElts)
534 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, Lo.getValueType(), Lo, Elt, Idx);
536 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, Hi.getValueType(), Hi, Elt,
537 DAG.getIntPtrConstant(IdxVal - LoNumElts));
541 // Spill the vector to the stack.
542 MVT VecVT = Vec.getValueType();
543 MVT EltVT = VecVT.getVectorElementType();
544 SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
545 SDValue Store = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
547 // Store the new element. This may be larger than the vector element type,
548 // so use a truncating store.
549 SDValue EltPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
550 Store = DAG.getTruncStore(Store, Elt, EltPtr, NULL, 0, EltVT);
552 // Reload the vector from the stack.
553 SDValue Load = DAG.getLoad(VecVT, Store, StackPtr, NULL, 0);
556 SplitVecRes_LOAD(cast<LoadSDNode>(Load.getNode()), Lo, Hi);
559 void DAGTypeLegalizer::SplitVecRes_LOAD(LoadSDNode *LD, SDValue &Lo,
561 assert(ISD::isUNINDEXEDLoad(LD) && "Indexed load during type legalization!");
563 GetSplitDestVTs(LD->getValueType(0), LoVT, HiVT);
565 ISD::LoadExtType ExtType = LD->getExtensionType();
566 SDValue Ch = LD->getChain();
567 SDValue Ptr = LD->getBasePtr();
568 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
569 const Value *SV = LD->getSrcValue();
570 int SVOffset = LD->getSrcValueOffset();
571 MVT MemoryVT = LD->getMemoryVT();
572 unsigned Alignment = LD->getAlignment();
573 bool isVolatile = LD->isVolatile();
575 MVT LoMemVT, HiMemVT;
576 GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
578 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType, LoVT, Ch, Ptr, Offset,
579 SV, SVOffset, LoMemVT, isVolatile, Alignment);
581 unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
582 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
583 DAG.getIntPtrConstant(IncrementSize));
584 SVOffset += IncrementSize;
585 Alignment = MinAlign(Alignment, IncrementSize);
586 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType, HiVT, Ch, Ptr, Offset,
587 SV, SVOffset, HiMemVT, isVolatile, Alignment);
589 // Build a factor node to remember that this load is independent of the
591 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
594 // Legalized the chain result - switch anything that used the old chain to
596 ReplaceValueWith(SDValue(LD, 1), Ch);
599 void DAGTypeLegalizer::SplitVecRes_UnaryOp(SDNode *N, SDValue &Lo,
601 // Get the dest types - they may not match the input types, e.g. int_to_fp.
603 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
606 MVT InVT = N->getOperand(0).getValueType();
607 switch (getTypeAction(InVT)) {
608 default: assert(0 && "Unexpected type action!");
610 assert(LoVT == HiVT && "Legal non-power-of-two vector type?");
611 MVT InNVT = MVT::getVectorVT(InVT.getVectorElementType(),
612 LoVT.getVectorNumElements());
613 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, InNVT, N->getOperand(0),
614 DAG.getIntPtrConstant(0));
615 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, InNVT, N->getOperand(0),
616 DAG.getIntPtrConstant(InNVT.getVectorNumElements()));
620 GetSplitVector(N->getOperand(0), Lo, Hi);
624 Lo = DAG.getNode(N->getOpcode(), LoVT, Lo);
625 Hi = DAG.getNode(N->getOpcode(), HiVT, Hi);
628 void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(SDNode *N, SDValue &Lo,
630 // Build the low part.
631 SDValue Mask = N->getOperand(2);
632 SmallVector<SDValue, 16> Ops;
634 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
635 MVT EltVT = LoVT.getVectorElementType();
636 unsigned LoNumElts = LoVT.getVectorNumElements();
637 unsigned NumElements = Mask.getNumOperands();
639 // Insert all of the elements from the input that are needed. We use
640 // buildvector of extractelement here because the input vectors will have
641 // to be legalized, so this makes the code simpler.
642 for (unsigned i = 0; i != LoNumElts; ++i) {
643 SDValue Arg = Mask.getOperand(i);
644 if (Arg.getOpcode() == ISD::UNDEF) {
645 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
647 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getZExtValue();
648 SDValue InVec = N->getOperand(0);
649 if (Idx >= NumElements) {
650 InVec = N->getOperand(1);
653 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, InVec,
654 DAG.getIntPtrConstant(Idx)));
657 Lo = DAG.getNode(ISD::BUILD_VECTOR, LoVT, &Ops[0], Ops.size());
660 for (unsigned i = LoNumElts; i != NumElements; ++i) {
661 SDValue Arg = Mask.getOperand(i);
662 if (Arg.getOpcode() == ISD::UNDEF) {
663 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
665 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getZExtValue();
666 SDValue InVec = N->getOperand(0);
667 if (Idx >= NumElements) {
668 InVec = N->getOperand(1);
671 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, InVec,
672 DAG.getIntPtrConstant(Idx)));
675 Hi = DAG.getNode(ISD::BUILD_VECTOR, HiVT, &Ops[0], Ops.size());
678 void DAGTypeLegalizer::SplitVecRes_VSETCC(SDNode *N, SDValue &Lo,
681 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
683 SDValue LL, LH, RL, RH;
684 GetSplitVector(N->getOperand(0), LL, LH);
685 GetSplitVector(N->getOperand(1), RL, RH);
687 Lo = DAG.getNode(ISD::VSETCC, LoVT, LL, RL, N->getOperand(2));
688 Hi = DAG.getNode(ISD::VSETCC, HiVT, LH, RH, N->getOperand(2));
692 //===----------------------------------------------------------------------===//
693 // Operand Vector Splitting
694 //===----------------------------------------------------------------------===//
696 /// SplitVectorOperand - This method is called when the specified operand of the
697 /// specified node is found to need vector splitting. At this point, all of the
698 /// result types of the node are known to be legal, but other operands of the
699 /// node may need legalization as well as the specified one.
700 bool DAGTypeLegalizer::SplitVectorOperand(SDNode *N, unsigned OpNo) {
701 DEBUG(cerr << "Split node operand: "; N->dump(&DAG); cerr << "\n");
702 SDValue Res = SDValue();
704 if (Res.getNode() == 0) {
705 switch (N->getOpcode()) {
708 cerr << "SplitVectorOperand Op #" << OpNo << ": ";
709 N->dump(&DAG); cerr << "\n";
711 assert(0 && "Do not know how to split this operator's operand!");
714 case ISD::BIT_CONVERT: Res = SplitVecOp_BIT_CONVERT(N); break;
715 case ISD::EXTRACT_SUBVECTOR: Res = SplitVecOp_EXTRACT_SUBVECTOR(N); break;
716 case ISD::EXTRACT_VECTOR_ELT:Res = SplitVecOp_EXTRACT_VECTOR_ELT(N); break;
717 case ISD::STORE: Res = SplitVecOp_STORE(cast<StoreSDNode>(N),
719 case ISD::VECTOR_SHUFFLE: Res = SplitVecOp_VECTOR_SHUFFLE(N, OpNo);break;
724 case ISD::FP_TO_SINT:
725 case ISD::FP_TO_UINT:
726 case ISD::SINT_TO_FP:
728 case ISD::UINT_TO_FP: Res = SplitVecOp_UnaryOp(N); break;
732 // If the result is null, the sub-method took care of registering results etc.
733 if (!Res.getNode()) return false;
735 // If the result is N, the sub-method updated N in place. Check to see if any
736 // operands are new, and if so, mark them.
737 if (Res.getNode() == N) {
738 // Mark N as new and remark N and its operands. This allows us to correctly
739 // revisit N if it needs another step of promotion and allows us to visit
740 // any new operands to N.
745 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
746 "Invalid operand expansion");
748 ReplaceValueWith(SDValue(N, 0), Res);
752 SDValue DAGTypeLegalizer::SplitVecOp_UnaryOp(SDNode *N) {
753 // The result has a legal vector type, but the input needs splitting.
754 MVT ResVT = N->getValueType(0);
756 GetSplitVector(N->getOperand(0), Lo, Hi);
757 assert(Lo.getValueType() == Hi.getValueType() &&
758 "Returns legal non-power-of-two vector type?");
759 MVT InVT = Lo.getValueType();
761 MVT OutVT = MVT::getVectorVT(ResVT.getVectorElementType(),
762 InVT.getVectorNumElements());
764 Lo = DAG.getNode(N->getOpcode(), OutVT, Lo);
765 Hi = DAG.getNode(N->getOpcode(), OutVT, Hi);
767 return DAG.getNode(ISD::CONCAT_VECTORS, ResVT, Lo, Hi);
770 SDValue DAGTypeLegalizer::SplitVecOp_BIT_CONVERT(SDNode *N) {
771 // For example, i64 = BIT_CONVERT v4i16 on alpha. Typically the vector will
772 // end up being split all the way down to individual components. Convert the
773 // split pieces into integers and reassemble.
775 GetSplitVector(N->getOperand(0), Lo, Hi);
776 Lo = BitConvertToInteger(Lo);
777 Hi = BitConvertToInteger(Hi);
779 if (TLI.isBigEndian())
782 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
783 JoinIntegers(Lo, Hi));
786 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
787 // We know that the extracted result type is legal. For now, assume the index
789 MVT SubVT = N->getValueType(0);
790 SDValue Idx = N->getOperand(1);
792 GetSplitVector(N->getOperand(0), Lo, Hi);
794 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
795 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
797 if (IdxVal < LoElts) {
798 assert(IdxVal + SubVT.getVectorNumElements() <= LoElts &&
799 "Extracted subvector crosses vector split!");
800 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SubVT, Lo, Idx);
802 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SubVT, Hi,
803 DAG.getConstant(IdxVal - LoElts, Idx.getValueType()));
807 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
808 SDValue Vec = N->getOperand(0);
809 SDValue Idx = N->getOperand(1);
810 MVT VecVT = Vec.getValueType();
812 if (isa<ConstantSDNode>(Idx)) {
813 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
814 assert(IdxVal < VecVT.getVectorNumElements() && "Invalid vector index!");
817 GetSplitVector(Vec, Lo, Hi);
819 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
822 return DAG.UpdateNodeOperands(SDValue(N, 0), Lo, Idx);
824 return DAG.UpdateNodeOperands(SDValue(N, 0), Hi,
825 DAG.getConstant(IdxVal - LoElts,
826 Idx.getValueType()));
829 // Store the vector to the stack.
830 MVT EltVT = VecVT.getVectorElementType();
831 SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
832 SDValue Store = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
834 // Load back the required element.
835 StackPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
836 return DAG.getLoad(EltVT, Store, StackPtr, NULL, 0);
839 SDValue DAGTypeLegalizer::SplitVecOp_STORE(StoreSDNode *N, unsigned OpNo) {
840 assert(N->isUnindexed() && "Indexed store of vector?");
841 assert(OpNo == 1 && "Can only split the stored value");
843 bool isTruncating = N->isTruncatingStore();
844 SDValue Ch = N->getChain();
845 SDValue Ptr = N->getBasePtr();
846 int SVOffset = N->getSrcValueOffset();
847 MVT MemoryVT = N->getMemoryVT();
848 unsigned Alignment = N->getAlignment();
849 bool isVol = N->isVolatile();
851 GetSplitVector(N->getOperand(1), Lo, Hi);
853 MVT LoMemVT, HiMemVT;
854 GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
856 unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
859 Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
860 LoMemVT, isVol, Alignment);
862 Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
865 // Increment the pointer to the other half.
866 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
867 DAG.getIntPtrConstant(IncrementSize));
870 Hi = DAG.getTruncStore(Ch, Hi, Ptr,
871 N->getSrcValue(), SVOffset+IncrementSize,
873 isVol, MinAlign(Alignment, IncrementSize));
875 Hi = DAG.getStore(Ch, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
876 isVol, MinAlign(Alignment, IncrementSize));
878 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
881 SDValue DAGTypeLegalizer::SplitVecOp_VECTOR_SHUFFLE(SDNode *N, unsigned OpNo) {
882 assert(OpNo == 2 && "Shuffle source type differs from result type?");
883 SDValue Mask = N->getOperand(2);
884 unsigned MaskLength = Mask.getValueType().getVectorNumElements();
885 unsigned LargestMaskEntryPlusOne = 2 * MaskLength;
886 unsigned MinimumBitWidth = Log2_32_Ceil(LargestMaskEntryPlusOne);
888 // Look for a legal vector type to place the mask values in.
889 // Note that there may not be *any* legal vector-of-integer
890 // type for which the element type is legal!
891 for (MVT::SimpleValueType EltVT = MVT::FIRST_INTEGER_VALUETYPE;
892 EltVT <= MVT::LAST_INTEGER_VALUETYPE;
893 // Integer values types are consecutively numbered. Exploit this.
894 EltVT = MVT::SimpleValueType(EltVT + 1)) {
896 // Is the element type big enough to hold the values?
897 if (MVT(EltVT).getSizeInBits() < MinimumBitWidth)
901 // Is the vector type legal?
902 MVT VecVT = MVT::getVectorVT(EltVT, MaskLength);
903 if (!isTypeLegal(VecVT))
907 // If the element type is not legal, find a larger legal type to use for
908 // the BUILD_VECTOR operands. This is an ugly hack, but seems to work!
909 // FIXME: The real solution is to change VECTOR_SHUFFLE into a variadic
910 // node where the shuffle mask is a list of integer operands, #2 .. #2+n.
911 for (MVT::SimpleValueType OpVT = EltVT; OpVT <= MVT::LAST_INTEGER_VALUETYPE;
912 // Integer values types are consecutively numbered. Exploit this.
913 OpVT = MVT::SimpleValueType(OpVT + 1)) {
914 if (!isTypeLegal(OpVT))
917 // Success! Rebuild the vector using the legal types.
918 SmallVector<SDValue, 16> Ops(MaskLength);
919 for (unsigned i = 0; i < MaskLength; ++i) {
920 SDValue Arg = Mask.getOperand(i);
921 if (Arg.getOpcode() == ISD::UNDEF) {
922 Ops[i] = DAG.getNode(ISD::UNDEF, OpVT);
924 uint64_t Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
925 Ops[i] = DAG.getConstant(Idx, OpVT);
928 return DAG.UpdateNodeOperands(SDValue(N,0),
929 N->getOperand(0), N->getOperand(1),
930 DAG.getNode(ISD::BUILD_VECTOR,
931 VecVT, &Ops[0], Ops.size()));
934 // Continuing is pointless - failure is certain.
937 assert(false && "Failed to find an appropriate mask type!");
938 return SDValue(N, 0);