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 two vectors of half the size. For example, implementing
19 // <128 x f32> operations in terms of two <64 x f32> operations.
21 //===----------------------------------------------------------------------===//
23 #include "LegalizeTypes.h"
24 #include "llvm/DataLayout.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
29 //===----------------------------------------------------------------------===//
30 // Result Vector Scalarization: <1 x ty> -> ty.
31 //===----------------------------------------------------------------------===//
33 void DAGTypeLegalizer::ScalarizeVectorResult(SDNode *N, unsigned ResNo) {
34 DEBUG(dbgs() << "Scalarize node result " << ResNo << ": ";
37 SDValue R = SDValue();
39 switch (N->getOpcode()) {
42 dbgs() << "ScalarizeVectorResult #" << ResNo << ": ";
46 report_fatal_error("Do not know how to scalarize the result of this "
49 case ISD::MERGE_VALUES: R = ScalarizeVecRes_MERGE_VALUES(N, ResNo);break;
50 case ISD::BITCAST: R = ScalarizeVecRes_BITCAST(N); break;
51 case ISD::BUILD_VECTOR: R = ScalarizeVecRes_BUILD_VECTOR(N); break;
52 case ISD::CONVERT_RNDSAT: R = ScalarizeVecRes_CONVERT_RNDSAT(N); break;
53 case ISD::EXTRACT_SUBVECTOR: R = ScalarizeVecRes_EXTRACT_SUBVECTOR(N); break;
54 case ISD::FP_ROUND: R = ScalarizeVecRes_FP_ROUND(N); break;
55 case ISD::FP_ROUND_INREG: R = ScalarizeVecRes_InregOp(N); break;
56 case ISD::FPOWI: R = ScalarizeVecRes_FPOWI(N); break;
57 case ISD::INSERT_VECTOR_ELT: R = ScalarizeVecRes_INSERT_VECTOR_ELT(N); break;
58 case ISD::LOAD: R = ScalarizeVecRes_LOAD(cast<LoadSDNode>(N));break;
59 case ISD::SCALAR_TO_VECTOR: R = ScalarizeVecRes_SCALAR_TO_VECTOR(N); break;
60 case ISD::SIGN_EXTEND_INREG: R = ScalarizeVecRes_InregOp(N); break;
61 case ISD::VSELECT: R = ScalarizeVecRes_VSELECT(N); break;
62 case ISD::SELECT: R = ScalarizeVecRes_SELECT(N); break;
63 case ISD::SELECT_CC: R = ScalarizeVecRes_SELECT_CC(N); break;
64 case ISD::SETCC: R = ScalarizeVecRes_SETCC(N); break;
65 case ISD::UNDEF: R = ScalarizeVecRes_UNDEF(N); break;
66 case ISD::VECTOR_SHUFFLE: R = ScalarizeVecRes_VECTOR_SHUFFLE(N); break;
89 case ISD::SIGN_EXTEND:
93 case ISD::ZERO_EXTEND:
94 R = ScalarizeVecRes_UnaryOp(N);
116 R = ScalarizeVecRes_BinOp(N);
119 R = ScalarizeVecRes_TernaryOp(N);
123 // If R is null, the sub-method took care of registering the result.
125 SetScalarizedVector(SDValue(N, ResNo), R);
128 SDValue DAGTypeLegalizer::ScalarizeVecRes_BinOp(SDNode *N) {
129 SDValue LHS = GetScalarizedVector(N->getOperand(0));
130 SDValue RHS = GetScalarizedVector(N->getOperand(1));
131 return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
132 LHS.getValueType(), LHS, RHS);
135 SDValue DAGTypeLegalizer::ScalarizeVecRes_TernaryOp(SDNode *N) {
136 SDValue Op0 = GetScalarizedVector(N->getOperand(0));
137 SDValue Op1 = GetScalarizedVector(N->getOperand(1));
138 SDValue Op2 = GetScalarizedVector(N->getOperand(2));
139 return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
140 Op0.getValueType(), Op0, Op1, Op2);
143 SDValue DAGTypeLegalizer::ScalarizeVecRes_MERGE_VALUES(SDNode *N,
145 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
146 return GetScalarizedVector(Op);
149 SDValue DAGTypeLegalizer::ScalarizeVecRes_BITCAST(SDNode *N) {
150 EVT NewVT = N->getValueType(0).getVectorElementType();
151 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
152 NewVT, N->getOperand(0));
155 SDValue DAGTypeLegalizer::ScalarizeVecRes_BUILD_VECTOR(SDNode *N) {
156 EVT EltVT = N->getValueType(0).getVectorElementType();
157 SDValue InOp = N->getOperand(0);
158 // The BUILD_VECTOR operands may be of wider element types and
159 // we may need to truncate them back to the requested return type.
160 if (EltVT.isInteger())
161 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), EltVT, InOp);
165 SDValue DAGTypeLegalizer::ScalarizeVecRes_CONVERT_RNDSAT(SDNode *N) {
166 EVT NewVT = N->getValueType(0).getVectorElementType();
167 SDValue Op0 = GetScalarizedVector(N->getOperand(0));
168 return DAG.getConvertRndSat(NewVT, N->getDebugLoc(),
169 Op0, DAG.getValueType(NewVT),
170 DAG.getValueType(Op0.getValueType()),
173 cast<CvtRndSatSDNode>(N)->getCvtCode());
176 SDValue DAGTypeLegalizer::ScalarizeVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
177 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(),
178 N->getValueType(0).getVectorElementType(),
179 N->getOperand(0), N->getOperand(1));
182 SDValue DAGTypeLegalizer::ScalarizeVecRes_FP_ROUND(SDNode *N) {
183 EVT NewVT = N->getValueType(0).getVectorElementType();
184 SDValue Op = GetScalarizedVector(N->getOperand(0));
185 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(),
186 NewVT, Op, N->getOperand(1));
189 SDValue DAGTypeLegalizer::ScalarizeVecRes_FPOWI(SDNode *N) {
190 SDValue Op = GetScalarizedVector(N->getOperand(0));
191 return DAG.getNode(ISD::FPOWI, N->getDebugLoc(),
192 Op.getValueType(), Op, N->getOperand(1));
195 SDValue DAGTypeLegalizer::ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N) {
196 // The value to insert may have a wider type than the vector element type,
197 // so be sure to truncate it to the element type if necessary.
198 SDValue Op = N->getOperand(1);
199 EVT EltVT = N->getValueType(0).getVectorElementType();
200 if (Op.getValueType() != EltVT)
201 // FIXME: Can this happen for floating point types?
202 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), EltVT, Op);
206 SDValue DAGTypeLegalizer::ScalarizeVecRes_LOAD(LoadSDNode *N) {
207 assert(N->isUnindexed() && "Indexed vector load?");
209 SDValue Result = DAG.getLoad(ISD::UNINDEXED,
210 N->getExtensionType(),
211 N->getValueType(0).getVectorElementType(),
213 N->getChain(), N->getBasePtr(),
214 DAG.getUNDEF(N->getBasePtr().getValueType()),
216 N->getMemoryVT().getVectorElementType(),
217 N->isVolatile(), N->isNonTemporal(),
218 N->isInvariant(), N->getOriginalAlignment());
220 // Legalized the chain result - switch anything that used the old chain to
222 ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
226 SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOp(SDNode *N) {
227 // Get the dest type - it doesn't always match the input type, e.g. int_to_fp.
228 EVT DestVT = N->getValueType(0).getVectorElementType();
229 SDValue Op = GetScalarizedVector(N->getOperand(0));
230 return DAG.getNode(N->getOpcode(), N->getDebugLoc(), DestVT, Op);
233 SDValue DAGTypeLegalizer::ScalarizeVecRes_InregOp(SDNode *N) {
234 EVT EltVT = N->getValueType(0).getVectorElementType();
235 EVT ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT().getVectorElementType();
236 SDValue LHS = GetScalarizedVector(N->getOperand(0));
237 return DAG.getNode(N->getOpcode(), N->getDebugLoc(), EltVT,
238 LHS, DAG.getValueType(ExtVT));
241 SDValue DAGTypeLegalizer::ScalarizeVecRes_SCALAR_TO_VECTOR(SDNode *N) {
242 // If the operand is wider than the vector element type then it is implicitly
243 // truncated. Make that explicit here.
244 EVT EltVT = N->getValueType(0).getVectorElementType();
245 SDValue InOp = N->getOperand(0);
246 if (InOp.getValueType() != EltVT)
247 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), EltVT, InOp);
251 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSELECT(SDNode *N) {
252 SDValue Cond = GetScalarizedVector(N->getOperand(0));
253 SDValue LHS = GetScalarizedVector(N->getOperand(1));
254 TargetLowering::BooleanContent ScalarBool = TLI.getBooleanContents(false);
255 TargetLowering::BooleanContent VecBool = TLI.getBooleanContents(true);
256 if (ScalarBool != VecBool) {
257 EVT CondVT = Cond.getValueType();
258 switch (ScalarBool) {
259 case TargetLowering::UndefinedBooleanContent:
261 case TargetLowering::ZeroOrOneBooleanContent:
262 assert(VecBool == TargetLowering::UndefinedBooleanContent ||
263 VecBool == TargetLowering::ZeroOrNegativeOneBooleanContent);
264 // Vector read from all ones, scalar expects a single 1 so mask.
265 Cond = DAG.getNode(ISD::AND, N->getDebugLoc(), CondVT,
266 Cond, DAG.getConstant(1, CondVT));
268 case TargetLowering::ZeroOrNegativeOneBooleanContent:
269 assert(VecBool == TargetLowering::UndefinedBooleanContent ||
270 VecBool == TargetLowering::ZeroOrOneBooleanContent);
271 // Vector reads from a one, scalar from all ones so sign extend.
272 Cond = DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), CondVT,
273 Cond, DAG.getValueType(MVT::i1));
277 return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
278 LHS.getValueType(), Cond, LHS,
279 GetScalarizedVector(N->getOperand(2)));
282 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT(SDNode *N) {
283 SDValue LHS = GetScalarizedVector(N->getOperand(1));
284 return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
285 LHS.getValueType(), N->getOperand(0), LHS,
286 GetScalarizedVector(N->getOperand(2)));
289 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT_CC(SDNode *N) {
290 SDValue LHS = GetScalarizedVector(N->getOperand(2));
291 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), LHS.getValueType(),
292 N->getOperand(0), N->getOperand(1),
293 LHS, GetScalarizedVector(N->getOperand(3)),
297 SDValue DAGTypeLegalizer::ScalarizeVecRes_SETCC(SDNode *N) {
298 assert(N->getValueType(0).isVector() ==
299 N->getOperand(0).getValueType().isVector() &&
300 "Scalar/Vector type mismatch");
302 if (N->getValueType(0).isVector()) return ScalarizeVecRes_VSETCC(N);
304 SDValue LHS = GetScalarizedVector(N->getOperand(0));
305 SDValue RHS = GetScalarizedVector(N->getOperand(1));
306 DebugLoc DL = N->getDebugLoc();
308 // Turn it into a scalar SETCC.
309 return DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, N->getOperand(2));
312 SDValue DAGTypeLegalizer::ScalarizeVecRes_UNDEF(SDNode *N) {
313 return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
316 SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_SHUFFLE(SDNode *N) {
317 // Figure out if the scalar is the LHS or RHS and return it.
318 SDValue Arg = N->getOperand(2).getOperand(0);
319 if (Arg.getOpcode() == ISD::UNDEF)
320 return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
321 unsigned Op = !cast<ConstantSDNode>(Arg)->isNullValue();
322 return GetScalarizedVector(N->getOperand(Op));
325 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSETCC(SDNode *N) {
326 assert(N->getValueType(0).isVector() &&
327 N->getOperand(0).getValueType().isVector() &&
328 "Operand types must be vectors");
330 SDValue LHS = GetScalarizedVector(N->getOperand(0));
331 SDValue RHS = GetScalarizedVector(N->getOperand(1));
332 EVT NVT = N->getValueType(0).getVectorElementType();
333 DebugLoc DL = N->getDebugLoc();
335 // Turn it into a scalar SETCC.
336 SDValue Res = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS,
338 // Vectors may have a different boolean contents to scalars. Promote the
339 // value appropriately.
340 ISD::NodeType ExtendCode =
341 TargetLowering::getExtendForContent(TLI.getBooleanContents(true));
342 return DAG.getNode(ExtendCode, DL, NVT, Res);
346 //===----------------------------------------------------------------------===//
347 // Operand Vector Scalarization <1 x ty> -> ty.
348 //===----------------------------------------------------------------------===//
350 bool DAGTypeLegalizer::ScalarizeVectorOperand(SDNode *N, unsigned OpNo) {
351 DEBUG(dbgs() << "Scalarize node operand " << OpNo << ": ";
354 SDValue Res = SDValue();
356 if (Res.getNode() == 0) {
357 switch (N->getOpcode()) {
360 dbgs() << "ScalarizeVectorOperand Op #" << OpNo << ": ";
364 llvm_unreachable("Do not know how to scalarize this operator's operand!");
366 Res = ScalarizeVecOp_BITCAST(N);
368 case ISD::CONCAT_VECTORS:
369 Res = ScalarizeVecOp_CONCAT_VECTORS(N);
371 case ISD::EXTRACT_VECTOR_ELT:
372 Res = ScalarizeVecOp_EXTRACT_VECTOR_ELT(N);
375 Res = ScalarizeVecOp_STORE(cast<StoreSDNode>(N), OpNo);
380 // If the result is null, the sub-method took care of registering results etc.
381 if (!Res.getNode()) return false;
383 // If the result is N, the sub-method updated N in place. Tell the legalizer
385 if (Res.getNode() == N)
388 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
389 "Invalid operand expansion");
391 ReplaceValueWith(SDValue(N, 0), Res);
395 /// ScalarizeVecOp_BITCAST - If the value to convert is a vector that needs
396 /// to be scalarized, it must be <1 x ty>. Convert the element instead.
397 SDValue DAGTypeLegalizer::ScalarizeVecOp_BITCAST(SDNode *N) {
398 SDValue Elt = GetScalarizedVector(N->getOperand(0));
399 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
400 N->getValueType(0), Elt);
403 /// ScalarizeVecOp_CONCAT_VECTORS - The vectors to concatenate have length one -
404 /// use a BUILD_VECTOR instead.
405 SDValue DAGTypeLegalizer::ScalarizeVecOp_CONCAT_VECTORS(SDNode *N) {
406 SmallVector<SDValue, 8> Ops(N->getNumOperands());
407 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
408 Ops[i] = GetScalarizedVector(N->getOperand(i));
409 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), N->getValueType(0),
410 &Ops[0], Ops.size());
413 /// ScalarizeVecOp_EXTRACT_VECTOR_ELT - If the input is a vector that needs to
414 /// be scalarized, it must be <1 x ty>, so just return the element, ignoring the
416 SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
417 SDValue Res = GetScalarizedVector(N->getOperand(0));
418 if (Res.getValueType() != N->getValueType(0))
419 Res = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), N->getValueType(0),
424 /// ScalarizeVecOp_STORE - If the value to store is a vector that needs to be
425 /// scalarized, it must be <1 x ty>. Just store the element.
426 SDValue DAGTypeLegalizer::ScalarizeVecOp_STORE(StoreSDNode *N, unsigned OpNo){
427 assert(N->isUnindexed() && "Indexed store of one-element vector?");
428 assert(OpNo == 1 && "Do not know how to scalarize this operand!");
429 DebugLoc dl = N->getDebugLoc();
431 if (N->isTruncatingStore())
432 return DAG.getTruncStore(N->getChain(), dl,
433 GetScalarizedVector(N->getOperand(1)),
434 N->getBasePtr(), N->getPointerInfo(),
435 N->getMemoryVT().getVectorElementType(),
436 N->isVolatile(), N->isNonTemporal(),
439 return DAG.getStore(N->getChain(), dl, GetScalarizedVector(N->getOperand(1)),
440 N->getBasePtr(), N->getPointerInfo(),
441 N->isVolatile(), N->isNonTemporal(),
442 N->getOriginalAlignment());
446 //===----------------------------------------------------------------------===//
447 // Result Vector Splitting
448 //===----------------------------------------------------------------------===//
450 /// SplitVectorResult - This method is called when the specified result of the
451 /// specified node is found to need vector splitting. At this point, the node
452 /// may also have invalid operands or may have other results that need
453 /// legalization, we just know that (at least) one result needs vector
455 void DAGTypeLegalizer::SplitVectorResult(SDNode *N, unsigned ResNo) {
456 DEBUG(dbgs() << "Split node result: ";
461 // See if the target wants to custom expand this node.
462 if (CustomLowerNode(N, N->getValueType(ResNo), true))
465 switch (N->getOpcode()) {
468 dbgs() << "SplitVectorResult #" << ResNo << ": ";
472 report_fatal_error("Do not know how to split the result of this "
475 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
477 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
478 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
479 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
480 case ISD::BITCAST: SplitVecRes_BITCAST(N, Lo, Hi); break;
481 case ISD::BUILD_VECTOR: SplitVecRes_BUILD_VECTOR(N, Lo, Hi); break;
482 case ISD::CONCAT_VECTORS: SplitVecRes_CONCAT_VECTORS(N, Lo, Hi); break;
483 case ISD::EXTRACT_SUBVECTOR: SplitVecRes_EXTRACT_SUBVECTOR(N, Lo, Hi); break;
484 case ISD::FP_ROUND_INREG: SplitVecRes_InregOp(N, Lo, Hi); break;
485 case ISD::FPOWI: SplitVecRes_FPOWI(N, Lo, Hi); break;
486 case ISD::INSERT_VECTOR_ELT: SplitVecRes_INSERT_VECTOR_ELT(N, Lo, Hi); break;
487 case ISD::SCALAR_TO_VECTOR: SplitVecRes_SCALAR_TO_VECTOR(N, Lo, Hi); break;
488 case ISD::SIGN_EXTEND_INREG: SplitVecRes_InregOp(N, Lo, Hi); break;
490 SplitVecRes_LOAD(cast<LoadSDNode>(N), Lo, Hi);
493 SplitVecRes_SETCC(N, Lo, Hi);
495 case ISD::VECTOR_SHUFFLE:
496 SplitVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N), Lo, Hi);
499 case ISD::ANY_EXTEND:
500 case ISD::CONVERT_RNDSAT:
503 case ISD::CTLZ_ZERO_UNDEF:
504 case ISD::CTTZ_ZERO_UNDEF:
515 case ISD::FNEARBYINT:
519 case ISD::FP_TO_SINT:
520 case ISD::FP_TO_UINT:
525 case ISD::SIGN_EXTEND:
526 case ISD::SINT_TO_FP:
528 case ISD::UINT_TO_FP:
529 case ISD::ZERO_EXTEND:
530 SplitVecRes_UnaryOp(N, Lo, Hi);
552 SplitVecRes_BinOp(N, Lo, Hi);
555 SplitVecRes_TernaryOp(N, Lo, Hi);
559 // If Lo/Hi is null, the sub-method took care of registering results etc.
561 SetSplitVector(SDValue(N, ResNo), Lo, Hi);
564 void DAGTypeLegalizer::SplitVecRes_BinOp(SDNode *N, SDValue &Lo,
566 SDValue LHSLo, LHSHi;
567 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
568 SDValue RHSLo, RHSHi;
569 GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
570 DebugLoc dl = N->getDebugLoc();
572 Lo = DAG.getNode(N->getOpcode(), dl, LHSLo.getValueType(), LHSLo, RHSLo);
573 Hi = DAG.getNode(N->getOpcode(), dl, LHSHi.getValueType(), LHSHi, RHSHi);
576 void DAGTypeLegalizer::SplitVecRes_TernaryOp(SDNode *N, SDValue &Lo,
578 SDValue Op0Lo, Op0Hi;
579 GetSplitVector(N->getOperand(0), Op0Lo, Op0Hi);
580 SDValue Op1Lo, Op1Hi;
581 GetSplitVector(N->getOperand(1), Op1Lo, Op1Hi);
582 SDValue Op2Lo, Op2Hi;
583 GetSplitVector(N->getOperand(2), Op2Lo, Op2Hi);
584 DebugLoc dl = N->getDebugLoc();
586 Lo = DAG.getNode(N->getOpcode(), dl, Op0Lo.getValueType(),
587 Op0Lo, Op1Lo, Op2Lo);
588 Hi = DAG.getNode(N->getOpcode(), dl, Op0Hi.getValueType(),
589 Op0Hi, Op1Hi, Op2Hi);
592 void DAGTypeLegalizer::SplitVecRes_BITCAST(SDNode *N, SDValue &Lo,
594 // We know the result is a vector. The input may be either a vector or a
597 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
598 DebugLoc dl = N->getDebugLoc();
600 SDValue InOp = N->getOperand(0);
601 EVT InVT = InOp.getValueType();
603 // Handle some special cases efficiently.
604 switch (getTypeAction(InVT)) {
605 case TargetLowering::TypeLegal:
606 case TargetLowering::TypePromoteInteger:
607 case TargetLowering::TypeSoftenFloat:
608 case TargetLowering::TypeScalarizeVector:
609 case TargetLowering::TypeWidenVector:
611 case TargetLowering::TypeExpandInteger:
612 case TargetLowering::TypeExpandFloat:
613 // A scalar to vector conversion, where the scalar needs expansion.
614 // If the vector is being split in two then we can just convert the
617 GetExpandedOp(InOp, Lo, Hi);
618 if (TLI.isBigEndian())
620 Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
621 Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
625 case TargetLowering::TypeSplitVector:
626 // If the input is a vector that needs to be split, convert each split
627 // piece of the input now.
628 GetSplitVector(InOp, Lo, Hi);
629 Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
630 Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
634 // In the general case, convert the input to an integer and split it by hand.
635 EVT LoIntVT = EVT::getIntegerVT(*DAG.getContext(), LoVT.getSizeInBits());
636 EVT HiIntVT = EVT::getIntegerVT(*DAG.getContext(), HiVT.getSizeInBits());
637 if (TLI.isBigEndian())
638 std::swap(LoIntVT, HiIntVT);
640 SplitInteger(BitConvertToInteger(InOp), LoIntVT, HiIntVT, Lo, Hi);
642 if (TLI.isBigEndian())
644 Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
645 Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
648 void DAGTypeLegalizer::SplitVecRes_BUILD_VECTOR(SDNode *N, SDValue &Lo,
651 DebugLoc dl = N->getDebugLoc();
652 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
653 unsigned LoNumElts = LoVT.getVectorNumElements();
654 SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+LoNumElts);
655 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, LoVT, &LoOps[0], LoOps.size());
657 SmallVector<SDValue, 8> HiOps(N->op_begin()+LoNumElts, N->op_end());
658 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, HiVT, &HiOps[0], HiOps.size());
661 void DAGTypeLegalizer::SplitVecRes_CONCAT_VECTORS(SDNode *N, SDValue &Lo,
663 assert(!(N->getNumOperands() & 1) && "Unsupported CONCAT_VECTORS");
664 DebugLoc dl = N->getDebugLoc();
665 unsigned NumSubvectors = N->getNumOperands() / 2;
666 if (NumSubvectors == 1) {
667 Lo = N->getOperand(0);
668 Hi = N->getOperand(1);
673 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
675 SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+NumSubvectors);
676 Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, LoVT, &LoOps[0], LoOps.size());
678 SmallVector<SDValue, 8> HiOps(N->op_begin()+NumSubvectors, N->op_end());
679 Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, HiVT, &HiOps[0], HiOps.size());
682 void DAGTypeLegalizer::SplitVecRes_EXTRACT_SUBVECTOR(SDNode *N, SDValue &Lo,
684 SDValue Vec = N->getOperand(0);
685 SDValue Idx = N->getOperand(1);
686 DebugLoc dl = N->getDebugLoc();
689 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
691 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, LoVT, Vec, Idx);
692 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
693 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, HiVT, Vec,
694 DAG.getIntPtrConstant(IdxVal + LoVT.getVectorNumElements()));
697 void DAGTypeLegalizer::SplitVecRes_FPOWI(SDNode *N, SDValue &Lo,
699 DebugLoc dl = N->getDebugLoc();
700 GetSplitVector(N->getOperand(0), Lo, Hi);
701 Lo = DAG.getNode(ISD::FPOWI, dl, Lo.getValueType(), Lo, N->getOperand(1));
702 Hi = DAG.getNode(ISD::FPOWI, dl, Hi.getValueType(), Hi, N->getOperand(1));
705 void DAGTypeLegalizer::SplitVecRes_InregOp(SDNode *N, SDValue &Lo,
707 SDValue LHSLo, LHSHi;
708 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
709 DebugLoc dl = N->getDebugLoc();
712 GetSplitDestVTs(cast<VTSDNode>(N->getOperand(1))->getVT(), LoVT, HiVT);
714 Lo = DAG.getNode(N->getOpcode(), dl, LHSLo.getValueType(), LHSLo,
715 DAG.getValueType(LoVT));
716 Hi = DAG.getNode(N->getOpcode(), dl, LHSHi.getValueType(), LHSHi,
717 DAG.getValueType(HiVT));
720 void DAGTypeLegalizer::SplitVecRes_INSERT_VECTOR_ELT(SDNode *N, SDValue &Lo,
722 SDValue Vec = N->getOperand(0);
723 SDValue Elt = N->getOperand(1);
724 SDValue Idx = N->getOperand(2);
725 DebugLoc dl = N->getDebugLoc();
726 GetSplitVector(Vec, Lo, Hi);
728 if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
729 unsigned IdxVal = CIdx->getZExtValue();
730 unsigned LoNumElts = Lo.getValueType().getVectorNumElements();
731 if (IdxVal < LoNumElts)
732 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
733 Lo.getValueType(), Lo, Elt, Idx);
735 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, Hi.getValueType(), Hi, Elt,
736 DAG.getIntPtrConstant(IdxVal - LoNumElts));
740 // Spill the vector to the stack.
741 EVT VecVT = Vec.getValueType();
742 EVT EltVT = VecVT.getVectorElementType();
743 SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
744 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
745 MachinePointerInfo(), false, false, 0);
747 // Store the new element. This may be larger than the vector element type,
748 // so use a truncating store.
749 SDValue EltPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
750 Type *VecType = VecVT.getTypeForEVT(*DAG.getContext());
752 TLI.getDataLayout()->getPrefTypeAlignment(VecType);
753 Store = DAG.getTruncStore(Store, dl, Elt, EltPtr, MachinePointerInfo(), EltVT,
756 // Load the Lo part from the stack slot.
757 Lo = DAG.getLoad(Lo.getValueType(), dl, Store, StackPtr, MachinePointerInfo(),
758 false, false, false, 0);
760 // Increment the pointer to the other part.
761 unsigned IncrementSize = Lo.getValueType().getSizeInBits() / 8;
762 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
763 DAG.getIntPtrConstant(IncrementSize));
765 // Load the Hi part from the stack slot.
766 Hi = DAG.getLoad(Hi.getValueType(), dl, Store, StackPtr, MachinePointerInfo(),
767 false, false, false, MinAlign(Alignment, IncrementSize));
770 void DAGTypeLegalizer::SplitVecRes_SCALAR_TO_VECTOR(SDNode *N, SDValue &Lo,
773 DebugLoc dl = N->getDebugLoc();
774 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
775 Lo = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoVT, N->getOperand(0));
776 Hi = DAG.getUNDEF(HiVT);
779 void DAGTypeLegalizer::SplitVecRes_LOAD(LoadSDNode *LD, SDValue &Lo,
781 assert(ISD::isUNINDEXEDLoad(LD) && "Indexed load during type legalization!");
783 DebugLoc dl = LD->getDebugLoc();
784 GetSplitDestVTs(LD->getValueType(0), LoVT, HiVT);
786 ISD::LoadExtType ExtType = LD->getExtensionType();
787 SDValue Ch = LD->getChain();
788 SDValue Ptr = LD->getBasePtr();
789 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
790 EVT MemoryVT = LD->getMemoryVT();
791 unsigned Alignment = LD->getOriginalAlignment();
792 bool isVolatile = LD->isVolatile();
793 bool isNonTemporal = LD->isNonTemporal();
794 bool isInvariant = LD->isInvariant();
796 EVT LoMemVT, HiMemVT;
797 GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
799 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType, LoVT, dl, Ch, Ptr, Offset,
800 LD->getPointerInfo(), LoMemVT, isVolatile, isNonTemporal,
801 isInvariant, Alignment);
803 unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
804 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
805 DAG.getIntPtrConstant(IncrementSize));
806 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType, HiVT, dl, Ch, Ptr, Offset,
807 LD->getPointerInfo().getWithOffset(IncrementSize),
808 HiMemVT, isVolatile, isNonTemporal, isInvariant, Alignment);
810 // Build a factor node to remember that this load is independent of the
812 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
815 // Legalized the chain result - switch anything that used the old chain to
817 ReplaceValueWith(SDValue(LD, 1), Ch);
820 void DAGTypeLegalizer::SplitVecRes_SETCC(SDNode *N, SDValue &Lo, SDValue &Hi) {
821 assert(N->getValueType(0).isVector() &&
822 N->getOperand(0).getValueType().isVector() &&
823 "Operand types must be vectors");
826 DebugLoc DL = N->getDebugLoc();
827 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
830 EVT InVT = N->getOperand(0).getValueType();
831 SDValue LL, LH, RL, RH;
832 EVT InNVT = EVT::getVectorVT(*DAG.getContext(), InVT.getVectorElementType(),
833 LoVT.getVectorNumElements());
834 LL = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(0),
835 DAG.getIntPtrConstant(0));
836 LH = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(0),
837 DAG.getIntPtrConstant(InNVT.getVectorNumElements()));
839 RL = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(1),
840 DAG.getIntPtrConstant(0));
841 RH = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(1),
842 DAG.getIntPtrConstant(InNVT.getVectorNumElements()));
844 Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2));
845 Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2));
848 void DAGTypeLegalizer::SplitVecRes_UnaryOp(SDNode *N, SDValue &Lo,
850 // Get the dest types - they may not match the input types, e.g. int_to_fp.
852 DebugLoc dl = N->getDebugLoc();
853 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
855 // If the input also splits, handle it directly for a compile time speedup.
856 // Otherwise split it by hand.
857 EVT InVT = N->getOperand(0).getValueType();
858 if (getTypeAction(InVT) == TargetLowering::TypeSplitVector) {
859 GetSplitVector(N->getOperand(0), Lo, Hi);
861 EVT InNVT = EVT::getVectorVT(*DAG.getContext(), InVT.getVectorElementType(),
862 LoVT.getVectorNumElements());
863 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InNVT, N->getOperand(0),
864 DAG.getIntPtrConstant(0));
865 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InNVT, N->getOperand(0),
866 DAG.getIntPtrConstant(InNVT.getVectorNumElements()));
869 if (N->getOpcode() == ISD::FP_ROUND) {
870 Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo, N->getOperand(1));
871 Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi, N->getOperand(1));
872 } else if (N->getOpcode() == ISD::CONVERT_RNDSAT) {
873 SDValue DTyOpLo = DAG.getValueType(LoVT);
874 SDValue DTyOpHi = DAG.getValueType(HiVT);
875 SDValue STyOpLo = DAG.getValueType(Lo.getValueType());
876 SDValue STyOpHi = DAG.getValueType(Hi.getValueType());
877 SDValue RndOp = N->getOperand(3);
878 SDValue SatOp = N->getOperand(4);
879 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
880 Lo = DAG.getConvertRndSat(LoVT, dl, Lo, DTyOpLo, STyOpLo, RndOp, SatOp,
882 Hi = DAG.getConvertRndSat(HiVT, dl, Hi, DTyOpHi, STyOpHi, RndOp, SatOp,
885 Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo);
886 Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi);
890 void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
891 SDValue &Lo, SDValue &Hi) {
892 // The low and high parts of the original input give four input vectors.
894 DebugLoc dl = N->getDebugLoc();
895 GetSplitVector(N->getOperand(0), Inputs[0], Inputs[1]);
896 GetSplitVector(N->getOperand(1), Inputs[2], Inputs[3]);
897 EVT NewVT = Inputs[0].getValueType();
898 unsigned NewElts = NewVT.getVectorNumElements();
900 // If Lo or Hi uses elements from at most two of the four input vectors, then
901 // express it as a vector shuffle of those two inputs. Otherwise extract the
902 // input elements by hand and construct the Lo/Hi output using a BUILD_VECTOR.
903 SmallVector<int, 16> Ops;
904 for (unsigned High = 0; High < 2; ++High) {
905 SDValue &Output = High ? Hi : Lo;
907 // Build a shuffle mask for the output, discovering on the fly which
908 // input vectors to use as shuffle operands (recorded in InputUsed).
909 // If building a suitable shuffle vector proves too hard, then bail
910 // out with useBuildVector set.
911 unsigned InputUsed[2] = { -1U, -1U }; // Not yet discovered.
912 unsigned FirstMaskIdx = High * NewElts;
913 bool useBuildVector = false;
914 for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
915 // The mask element. This indexes into the input.
916 int Idx = N->getMaskElt(FirstMaskIdx + MaskOffset);
918 // The input vector this mask element indexes into.
919 unsigned Input = (unsigned)Idx / NewElts;
921 if (Input >= array_lengthof(Inputs)) {
922 // The mask element does not index into any input vector.
927 // Turn the index into an offset from the start of the input vector.
928 Idx -= Input * NewElts;
930 // Find or create a shuffle vector operand to hold this input.
932 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
933 if (InputUsed[OpNo] == Input) {
934 // This input vector is already an operand.
936 } else if (InputUsed[OpNo] == -1U) {
937 // Create a new operand for this input vector.
938 InputUsed[OpNo] = Input;
943 if (OpNo >= array_lengthof(InputUsed)) {
944 // More than two input vectors used! Give up on trying to create a
945 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
946 useBuildVector = true;
950 // Add the mask index for the new shuffle vector.
951 Ops.push_back(Idx + OpNo * NewElts);
954 if (useBuildVector) {
955 EVT EltVT = NewVT.getVectorElementType();
956 SmallVector<SDValue, 16> SVOps;
958 // Extract the input elements by hand.
959 for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
960 // The mask element. This indexes into the input.
961 int Idx = N->getMaskElt(FirstMaskIdx + MaskOffset);
963 // The input vector this mask element indexes into.
964 unsigned Input = (unsigned)Idx / NewElts;
966 if (Input >= array_lengthof(Inputs)) {
967 // The mask element is "undef" or indexes off the end of the input.
968 SVOps.push_back(DAG.getUNDEF(EltVT));
972 // Turn the index into an offset from the start of the input vector.
973 Idx -= Input * NewElts;
975 // Extract the vector element by hand.
976 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
977 Inputs[Input], DAG.getIntPtrConstant(Idx)));
980 // Construct the Lo/Hi output using a BUILD_VECTOR.
981 Output = DAG.getNode(ISD::BUILD_VECTOR,dl,NewVT, &SVOps[0], SVOps.size());
982 } else if (InputUsed[0] == -1U) {
983 // No input vectors were used! The result is undefined.
984 Output = DAG.getUNDEF(NewVT);
986 SDValue Op0 = Inputs[InputUsed[0]];
987 // If only one input was used, use an undefined vector for the other.
988 SDValue Op1 = InputUsed[1] == -1U ?
989 DAG.getUNDEF(NewVT) : Inputs[InputUsed[1]];
990 // At least one input vector was used. Create a new shuffle vector.
991 Output = DAG.getVectorShuffle(NewVT, dl, Op0, Op1, &Ops[0]);
999 //===----------------------------------------------------------------------===//
1000 // Operand Vector Splitting
1001 //===----------------------------------------------------------------------===//
1003 /// SplitVectorOperand - This method is called when the specified operand of the
1004 /// specified node is found to need vector splitting. At this point, all of the
1005 /// result types of the node are known to be legal, but other operands of the
1006 /// node may need legalization as well as the specified one.
1007 bool DAGTypeLegalizer::SplitVectorOperand(SDNode *N, unsigned OpNo) {
1008 DEBUG(dbgs() << "Split node operand: ";
1011 SDValue Res = SDValue();
1013 if (Res.getNode() == 0) {
1014 switch (N->getOpcode()) {
1017 dbgs() << "SplitVectorOperand Op #" << OpNo << ": ";
1021 report_fatal_error("Do not know how to split this operator's "
1024 case ISD::SETCC: Res = SplitVecOp_VSETCC(N); break;
1025 case ISD::BITCAST: Res = SplitVecOp_BITCAST(N); break;
1026 case ISD::EXTRACT_SUBVECTOR: Res = SplitVecOp_EXTRACT_SUBVECTOR(N); break;
1027 case ISD::EXTRACT_VECTOR_ELT:Res = SplitVecOp_EXTRACT_VECTOR_ELT(N); break;
1028 case ISD::CONCAT_VECTORS: Res = SplitVecOp_CONCAT_VECTORS(N); break;
1029 case ISD::FP_ROUND: Res = SplitVecOp_FP_ROUND(N); break;
1031 Res = SplitVecOp_STORE(cast<StoreSDNode>(N), OpNo);
1037 case ISD::FP_EXTEND:
1038 case ISD::FP_TO_SINT:
1039 case ISD::FP_TO_UINT:
1040 case ISD::SINT_TO_FP:
1041 case ISD::UINT_TO_FP:
1044 case ISD::SIGN_EXTEND:
1045 case ISD::ZERO_EXTEND:
1046 case ISD::ANY_EXTEND:
1047 Res = SplitVecOp_UnaryOp(N);
1052 // If the result is null, the sub-method took care of registering results etc.
1053 if (!Res.getNode()) return false;
1055 // If the result is N, the sub-method updated N in place. Tell the legalizer
1057 if (Res.getNode() == N)
1060 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1061 "Invalid operand expansion");
1063 ReplaceValueWith(SDValue(N, 0), Res);
1067 SDValue DAGTypeLegalizer::SplitVecOp_UnaryOp(SDNode *N) {
1068 // The result has a legal vector type, but the input needs splitting.
1069 EVT ResVT = N->getValueType(0);
1071 DebugLoc dl = N->getDebugLoc();
1072 GetSplitVector(N->getOperand(0), Lo, Hi);
1073 EVT InVT = Lo.getValueType();
1075 EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
1076 InVT.getVectorNumElements());
1078 Lo = DAG.getNode(N->getOpcode(), dl, OutVT, Lo);
1079 Hi = DAG.getNode(N->getOpcode(), dl, OutVT, Hi);
1081 return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi);
1084 SDValue DAGTypeLegalizer::SplitVecOp_BITCAST(SDNode *N) {
1085 // For example, i64 = BITCAST v4i16 on alpha. Typically the vector will
1086 // end up being split all the way down to individual components. Convert the
1087 // split pieces into integers and reassemble.
1089 GetSplitVector(N->getOperand(0), Lo, Hi);
1090 Lo = BitConvertToInteger(Lo);
1091 Hi = BitConvertToInteger(Hi);
1093 if (TLI.isBigEndian())
1096 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getValueType(0),
1097 JoinIntegers(Lo, Hi));
1100 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
1101 // We know that the extracted result type is legal.
1102 EVT SubVT = N->getValueType(0);
1103 SDValue Idx = N->getOperand(1);
1104 DebugLoc dl = N->getDebugLoc();
1106 GetSplitVector(N->getOperand(0), Lo, Hi);
1108 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1109 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1111 if (IdxVal < LoElts) {
1112 assert(IdxVal + SubVT.getVectorNumElements() <= LoElts &&
1113 "Extracted subvector crosses vector split!");
1114 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, Lo, Idx);
1116 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, Hi,
1117 DAG.getConstant(IdxVal - LoElts, Idx.getValueType()));
1121 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
1122 SDValue Vec = N->getOperand(0);
1123 SDValue Idx = N->getOperand(1);
1124 EVT VecVT = Vec.getValueType();
1126 if (isa<ConstantSDNode>(Idx)) {
1127 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1128 assert(IdxVal < VecVT.getVectorNumElements() && "Invalid vector index!");
1131 GetSplitVector(Vec, Lo, Hi);
1133 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1135 if (IdxVal < LoElts)
1136 return SDValue(DAG.UpdateNodeOperands(N, Lo, Idx), 0);
1137 return SDValue(DAG.UpdateNodeOperands(N, Hi,
1138 DAG.getConstant(IdxVal - LoElts,
1139 Idx.getValueType())), 0);
1142 // Store the vector to the stack.
1143 EVT EltVT = VecVT.getVectorElementType();
1144 DebugLoc dl = N->getDebugLoc();
1145 SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
1146 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1147 MachinePointerInfo(), false, false, 0);
1149 // Load back the required element.
1150 StackPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
1151 return DAG.getExtLoad(ISD::EXTLOAD, dl, N->getValueType(0), Store, StackPtr,
1152 MachinePointerInfo(), EltVT, false, false, 0);
1155 SDValue DAGTypeLegalizer::SplitVecOp_STORE(StoreSDNode *N, unsigned OpNo) {
1156 assert(N->isUnindexed() && "Indexed store of vector?");
1157 assert(OpNo == 1 && "Can only split the stored value");
1158 DebugLoc DL = N->getDebugLoc();
1160 bool isTruncating = N->isTruncatingStore();
1161 SDValue Ch = N->getChain();
1162 SDValue Ptr = N->getBasePtr();
1163 EVT MemoryVT = N->getMemoryVT();
1164 unsigned Alignment = N->getOriginalAlignment();
1165 bool isVol = N->isVolatile();
1166 bool isNT = N->isNonTemporal();
1168 GetSplitVector(N->getOperand(1), Lo, Hi);
1170 EVT LoMemVT, HiMemVT;
1171 GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
1173 unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
1176 Lo = DAG.getTruncStore(Ch, DL, Lo, Ptr, N->getPointerInfo(),
1177 LoMemVT, isVol, isNT, Alignment);
1179 Lo = DAG.getStore(Ch, DL, Lo, Ptr, N->getPointerInfo(),
1180 isVol, isNT, Alignment);
1182 // Increment the pointer to the other half.
1183 Ptr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1184 DAG.getIntPtrConstant(IncrementSize));
1187 Hi = DAG.getTruncStore(Ch, DL, Hi, Ptr,
1188 N->getPointerInfo().getWithOffset(IncrementSize),
1189 HiMemVT, isVol, isNT, Alignment);
1191 Hi = DAG.getStore(Ch, DL, Hi, Ptr,
1192 N->getPointerInfo().getWithOffset(IncrementSize),
1193 isVol, isNT, Alignment);
1195 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
1198 SDValue DAGTypeLegalizer::SplitVecOp_CONCAT_VECTORS(SDNode *N) {
1199 DebugLoc DL = N->getDebugLoc();
1201 // The input operands all must have the same type, and we know the result the
1202 // result type is valid. Convert this to a buildvector which extracts all the
1204 // TODO: If the input elements are power-two vectors, we could convert this to
1205 // a new CONCAT_VECTORS node with elements that are half-wide.
1206 SmallVector<SDValue, 32> Elts;
1207 EVT EltVT = N->getValueType(0).getVectorElementType();
1208 for (unsigned op = 0, e = N->getNumOperands(); op != e; ++op) {
1209 SDValue Op = N->getOperand(op);
1210 for (unsigned i = 0, e = Op.getValueType().getVectorNumElements();
1212 Elts.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT,
1213 Op, DAG.getIntPtrConstant(i)));
1218 return DAG.getNode(ISD::BUILD_VECTOR, DL, N->getValueType(0),
1219 &Elts[0], Elts.size());
1222 SDValue DAGTypeLegalizer::SplitVecOp_VSETCC(SDNode *N) {
1223 assert(N->getValueType(0).isVector() &&
1224 N->getOperand(0).getValueType().isVector() &&
1225 "Operand types must be vectors");
1226 // The result has a legal vector type, but the input needs splitting.
1227 SDValue Lo0, Hi0, Lo1, Hi1, LoRes, HiRes;
1228 DebugLoc DL = N->getDebugLoc();
1229 GetSplitVector(N->getOperand(0), Lo0, Hi0);
1230 GetSplitVector(N->getOperand(1), Lo1, Hi1);
1231 unsigned PartElements = Lo0.getValueType().getVectorNumElements();
1232 EVT PartResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, PartElements);
1233 EVT WideResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, 2*PartElements);
1235 LoRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Lo0, Lo1, N->getOperand(2));
1236 HiRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Hi0, Hi1, N->getOperand(2));
1237 SDValue Con = DAG.getNode(ISD::CONCAT_VECTORS, DL, WideResVT, LoRes, HiRes);
1238 return PromoteTargetBoolean(Con, N->getValueType(0));
1242 SDValue DAGTypeLegalizer::SplitVecOp_FP_ROUND(SDNode *N) {
1243 // The result has a legal vector type, but the input needs splitting.
1244 EVT ResVT = N->getValueType(0);
1246 DebugLoc DL = N->getDebugLoc();
1247 GetSplitVector(N->getOperand(0), Lo, Hi);
1248 EVT InVT = Lo.getValueType();
1250 EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
1251 InVT.getVectorNumElements());
1253 Lo = DAG.getNode(ISD::FP_ROUND, DL, OutVT, Lo, N->getOperand(1));
1254 Hi = DAG.getNode(ISD::FP_ROUND, DL, OutVT, Hi, N->getOperand(1));
1256 return DAG.getNode(ISD::CONCAT_VECTORS, DL, ResVT, Lo, Hi);
1261 //===----------------------------------------------------------------------===//
1262 // Result Vector Widening
1263 //===----------------------------------------------------------------------===//
1265 void DAGTypeLegalizer::WidenVectorResult(SDNode *N, unsigned ResNo) {
1266 DEBUG(dbgs() << "Widen node result " << ResNo << ": ";
1270 // See if the target wants to custom widen this node.
1271 if (CustomWidenLowerNode(N, N->getValueType(ResNo)))
1274 SDValue Res = SDValue();
1275 switch (N->getOpcode()) {
1278 dbgs() << "WidenVectorResult #" << ResNo << ": ";
1282 llvm_unreachable("Do not know how to widen the result of this operator!");
1284 case ISD::MERGE_VALUES: Res = WidenVecRes_MERGE_VALUES(N, ResNo); break;
1285 case ISD::BITCAST: Res = WidenVecRes_BITCAST(N); break;
1286 case ISD::BUILD_VECTOR: Res = WidenVecRes_BUILD_VECTOR(N); break;
1287 case ISD::CONCAT_VECTORS: Res = WidenVecRes_CONCAT_VECTORS(N); break;
1288 case ISD::CONVERT_RNDSAT: Res = WidenVecRes_CONVERT_RNDSAT(N); break;
1289 case ISD::EXTRACT_SUBVECTOR: Res = WidenVecRes_EXTRACT_SUBVECTOR(N); break;
1290 case ISD::FP_ROUND_INREG: Res = WidenVecRes_InregOp(N); break;
1291 case ISD::INSERT_VECTOR_ELT: Res = WidenVecRes_INSERT_VECTOR_ELT(N); break;
1292 case ISD::LOAD: Res = WidenVecRes_LOAD(N); break;
1293 case ISD::SCALAR_TO_VECTOR: Res = WidenVecRes_SCALAR_TO_VECTOR(N); break;
1294 case ISD::SIGN_EXTEND_INREG: Res = WidenVecRes_InregOp(N); break;
1296 case ISD::SELECT: Res = WidenVecRes_SELECT(N); break;
1297 case ISD::SELECT_CC: Res = WidenVecRes_SELECT_CC(N); break;
1298 case ISD::SETCC: Res = WidenVecRes_SETCC(N); break;
1299 case ISD::UNDEF: Res = WidenVecRes_UNDEF(N); break;
1300 case ISD::VECTOR_SHUFFLE:
1301 Res = WidenVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N));
1307 case ISD::FCOPYSIGN:
1323 Res = WidenVecRes_Binary(N);
1327 Res = WidenVecRes_POWI(N);
1333 Res = WidenVecRes_Shift(N);
1336 case ISD::ANY_EXTEND:
1337 case ISD::FP_EXTEND:
1339 case ISD::FP_TO_SINT:
1340 case ISD::FP_TO_UINT:
1341 case ISD::SIGN_EXTEND:
1342 case ISD::SINT_TO_FP:
1344 case ISD::UINT_TO_FP:
1345 case ISD::ZERO_EXTEND:
1346 Res = WidenVecRes_Convert(N);
1361 case ISD::FNEARBYINT:
1367 Res = WidenVecRes_Unary(N);
1370 Res = WidenVecRes_Ternary(N);
1374 // If Res is null, the sub-method took care of registering the result.
1376 SetWidenedVector(SDValue(N, ResNo), Res);
1379 SDValue DAGTypeLegalizer::WidenVecRes_Ternary(SDNode *N) {
1380 // Ternary op widening.
1381 DebugLoc dl = N->getDebugLoc();
1382 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1383 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
1384 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
1385 SDValue InOp3 = GetWidenedVector(N->getOperand(2));
1386 return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, InOp3);
1389 SDValue DAGTypeLegalizer::WidenVecRes_Binary(SDNode *N) {
1390 // Binary op widening.
1391 unsigned Opcode = N->getOpcode();
1392 DebugLoc dl = N->getDebugLoc();
1393 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1394 EVT WidenEltVT = WidenVT.getVectorElementType();
1396 unsigned NumElts = VT.getVectorNumElements();
1397 while (!TLI.isTypeLegal(VT) && NumElts != 1) {
1398 NumElts = NumElts / 2;
1399 VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
1402 if (NumElts != 1 && !TLI.canOpTrap(N->getOpcode(), VT)) {
1403 // Operation doesn't trap so just widen as normal.
1404 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
1405 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
1406 return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2);
1409 // No legal vector version so unroll the vector operation and then widen.
1411 return DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements());
1413 // Since the operation can trap, apply operation on the original vector.
1415 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
1416 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
1417 unsigned CurNumElts = N->getValueType(0).getVectorNumElements();
1419 SmallVector<SDValue, 16> ConcatOps(CurNumElts);
1420 unsigned ConcatEnd = 0; // Current ConcatOps index.
1421 int Idx = 0; // Current Idx into input vectors.
1423 // NumElts := greatest legal vector size (at most WidenVT)
1424 // while (orig. vector has unhandled elements) {
1425 // take munches of size NumElts from the beginning and add to ConcatOps
1426 // NumElts := next smaller supported vector size or 1
1428 while (CurNumElts != 0) {
1429 while (CurNumElts >= NumElts) {
1430 SDValue EOp1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, InOp1,
1431 DAG.getIntPtrConstant(Idx));
1432 SDValue EOp2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, InOp2,
1433 DAG.getIntPtrConstant(Idx));
1434 ConcatOps[ConcatEnd++] = DAG.getNode(Opcode, dl, VT, EOp1, EOp2);
1436 CurNumElts -= NumElts;
1439 NumElts = NumElts / 2;
1440 VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
1441 } while (!TLI.isTypeLegal(VT) && NumElts != 1);
1444 for (unsigned i = 0; i != CurNumElts; ++i, ++Idx) {
1445 SDValue EOp1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, WidenEltVT,
1446 InOp1, DAG.getIntPtrConstant(Idx));
1447 SDValue EOp2 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, WidenEltVT,
1448 InOp2, DAG.getIntPtrConstant(Idx));
1449 ConcatOps[ConcatEnd++] = DAG.getNode(Opcode, dl, WidenEltVT,
1456 // Check to see if we have a single operation with the widen type.
1457 if (ConcatEnd == 1) {
1458 VT = ConcatOps[0].getValueType();
1460 return ConcatOps[0];
1463 // while (Some element of ConcatOps is not of type MaxVT) {
1464 // From the end of ConcatOps, collect elements of the same type and put
1465 // them into an op of the next larger supported type
1467 while (ConcatOps[ConcatEnd-1].getValueType() != MaxVT) {
1468 Idx = ConcatEnd - 1;
1469 VT = ConcatOps[Idx--].getValueType();
1470 while (Idx >= 0 && ConcatOps[Idx].getValueType() == VT)
1473 int NextSize = VT.isVector() ? VT.getVectorNumElements() : 1;
1477 NextVT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NextSize);
1478 } while (!TLI.isTypeLegal(NextVT));
1480 if (!VT.isVector()) {
1481 // Scalar type, create an INSERT_VECTOR_ELEMENT of type NextVT
1482 SDValue VecOp = DAG.getUNDEF(NextVT);
1483 unsigned NumToInsert = ConcatEnd - Idx - 1;
1484 for (unsigned i = 0, OpIdx = Idx+1; i < NumToInsert; i++, OpIdx++) {
1485 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NextVT, VecOp,
1486 ConcatOps[OpIdx], DAG.getIntPtrConstant(i));
1488 ConcatOps[Idx+1] = VecOp;
1489 ConcatEnd = Idx + 2;
1491 // Vector type, create a CONCAT_VECTORS of type NextVT
1492 SDValue undefVec = DAG.getUNDEF(VT);
1493 unsigned OpsToConcat = NextSize/VT.getVectorNumElements();
1494 SmallVector<SDValue, 16> SubConcatOps(OpsToConcat);
1495 unsigned RealVals = ConcatEnd - Idx - 1;
1496 unsigned SubConcatEnd = 0;
1497 unsigned SubConcatIdx = Idx + 1;
1498 while (SubConcatEnd < RealVals)
1499 SubConcatOps[SubConcatEnd++] = ConcatOps[++Idx];
1500 while (SubConcatEnd < OpsToConcat)
1501 SubConcatOps[SubConcatEnd++] = undefVec;
1502 ConcatOps[SubConcatIdx] = DAG.getNode(ISD::CONCAT_VECTORS, dl,
1503 NextVT, &SubConcatOps[0],
1505 ConcatEnd = SubConcatIdx + 1;
1509 // Check to see if we have a single operation with the widen type.
1510 if (ConcatEnd == 1) {
1511 VT = ConcatOps[0].getValueType();
1513 return ConcatOps[0];
1516 // add undefs of size MaxVT until ConcatOps grows to length of WidenVT
1517 unsigned NumOps = WidenVT.getVectorNumElements()/MaxVT.getVectorNumElements();
1518 if (NumOps != ConcatEnd ) {
1519 SDValue UndefVal = DAG.getUNDEF(MaxVT);
1520 for (unsigned j = ConcatEnd; j < NumOps; ++j)
1521 ConcatOps[j] = UndefVal;
1523 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &ConcatOps[0], NumOps);
1526 SDValue DAGTypeLegalizer::WidenVecRes_Convert(SDNode *N) {
1527 SDValue InOp = N->getOperand(0);
1528 DebugLoc DL = N->getDebugLoc();
1530 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1531 unsigned WidenNumElts = WidenVT.getVectorNumElements();
1533 EVT InVT = InOp.getValueType();
1534 EVT InEltVT = InVT.getVectorElementType();
1535 EVT InWidenVT = EVT::getVectorVT(*DAG.getContext(), InEltVT, WidenNumElts);
1537 unsigned Opcode = N->getOpcode();
1538 unsigned InVTNumElts = InVT.getVectorNumElements();
1540 if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
1541 InOp = GetWidenedVector(N->getOperand(0));
1542 InVT = InOp.getValueType();
1543 InVTNumElts = InVT.getVectorNumElements();
1544 if (InVTNumElts == WidenNumElts) {
1545 if (N->getNumOperands() == 1)
1546 return DAG.getNode(Opcode, DL, WidenVT, InOp);
1547 return DAG.getNode(Opcode, DL, WidenVT, InOp, N->getOperand(1));
1551 if (TLI.isTypeLegal(InWidenVT)) {
1552 // Because the result and the input are different vector types, widening
1553 // the result could create a legal type but widening the input might make
1554 // it an illegal type that might lead to repeatedly splitting the input
1555 // and then widening it. To avoid this, we widen the input only if
1556 // it results in a legal type.
1557 if (WidenNumElts % InVTNumElts == 0) {
1558 // Widen the input and call convert on the widened input vector.
1559 unsigned NumConcat = WidenNumElts/InVTNumElts;
1560 SmallVector<SDValue, 16> Ops(NumConcat);
1562 SDValue UndefVal = DAG.getUNDEF(InVT);
1563 for (unsigned i = 1; i != NumConcat; ++i)
1565 SDValue InVec = DAG.getNode(ISD::CONCAT_VECTORS, DL, InWidenVT,
1566 &Ops[0], NumConcat);
1567 if (N->getNumOperands() == 1)
1568 return DAG.getNode(Opcode, DL, WidenVT, InVec);
1569 return DAG.getNode(Opcode, DL, WidenVT, InVec, N->getOperand(1));
1572 if (InVTNumElts % WidenNumElts == 0) {
1573 SDValue InVal = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InWidenVT,
1574 InOp, DAG.getIntPtrConstant(0));
1575 // Extract the input and convert the shorten input vector.
1576 if (N->getNumOperands() == 1)
1577 return DAG.getNode(Opcode, DL, WidenVT, InVal);
1578 return DAG.getNode(Opcode, DL, WidenVT, InVal, N->getOperand(1));
1582 // Otherwise unroll into some nasty scalar code and rebuild the vector.
1583 SmallVector<SDValue, 16> Ops(WidenNumElts);
1584 EVT EltVT = WidenVT.getVectorElementType();
1585 unsigned MinElts = std::min(InVTNumElts, WidenNumElts);
1587 for (i=0; i < MinElts; ++i) {
1588 SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, InEltVT, InOp,
1589 DAG.getIntPtrConstant(i));
1590 if (N->getNumOperands() == 1)
1591 Ops[i] = DAG.getNode(Opcode, DL, EltVT, Val);
1593 Ops[i] = DAG.getNode(Opcode, DL, EltVT, Val, N->getOperand(1));
1596 SDValue UndefVal = DAG.getUNDEF(EltVT);
1597 for (; i < WidenNumElts; ++i)
1600 return DAG.getNode(ISD::BUILD_VECTOR, DL, WidenVT, &Ops[0], WidenNumElts);
1603 SDValue DAGTypeLegalizer::WidenVecRes_POWI(SDNode *N) {
1604 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1605 SDValue InOp = GetWidenedVector(N->getOperand(0));
1606 SDValue ShOp = N->getOperand(1);
1607 return DAG.getNode(N->getOpcode(), N->getDebugLoc(), WidenVT, InOp, ShOp);
1610 SDValue DAGTypeLegalizer::WidenVecRes_Shift(SDNode *N) {
1611 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1612 SDValue InOp = GetWidenedVector(N->getOperand(0));
1613 SDValue ShOp = N->getOperand(1);
1615 EVT ShVT = ShOp.getValueType();
1616 if (getTypeAction(ShVT) == TargetLowering::TypeWidenVector) {
1617 ShOp = GetWidenedVector(ShOp);
1618 ShVT = ShOp.getValueType();
1620 EVT ShWidenVT = EVT::getVectorVT(*DAG.getContext(),
1621 ShVT.getVectorElementType(),
1622 WidenVT.getVectorNumElements());
1623 if (ShVT != ShWidenVT)
1624 ShOp = ModifyToType(ShOp, ShWidenVT);
1626 return DAG.getNode(N->getOpcode(), N->getDebugLoc(), WidenVT, InOp, ShOp);
1629 SDValue DAGTypeLegalizer::WidenVecRes_Unary(SDNode *N) {
1630 // Unary op widening.
1631 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1632 SDValue InOp = GetWidenedVector(N->getOperand(0));
1633 return DAG.getNode(N->getOpcode(), N->getDebugLoc(), WidenVT, InOp);
1636 SDValue DAGTypeLegalizer::WidenVecRes_InregOp(SDNode *N) {
1637 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1638 EVT ExtVT = EVT::getVectorVT(*DAG.getContext(),
1639 cast<VTSDNode>(N->getOperand(1))->getVT()
1640 .getVectorElementType(),
1641 WidenVT.getVectorNumElements());
1642 SDValue WidenLHS = GetWidenedVector(N->getOperand(0));
1643 return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
1644 WidenVT, WidenLHS, DAG.getValueType(ExtVT));
1647 SDValue DAGTypeLegalizer::WidenVecRes_MERGE_VALUES(SDNode *N, unsigned ResNo) {
1648 SDValue WidenVec = DisintegrateMERGE_VALUES(N, ResNo);
1649 return GetWidenedVector(WidenVec);
1652 SDValue DAGTypeLegalizer::WidenVecRes_BITCAST(SDNode *N) {
1653 SDValue InOp = N->getOperand(0);
1654 EVT InVT = InOp.getValueType();
1655 EVT VT = N->getValueType(0);
1656 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1657 DebugLoc dl = N->getDebugLoc();
1659 switch (getTypeAction(InVT)) {
1660 case TargetLowering::TypeLegal:
1662 case TargetLowering::TypePromoteInteger:
1663 // If the incoming type is a vector that is being promoted, then
1664 // we know that the elements are arranged differently and that we
1665 // must perform the conversion using a stack slot.
1666 if (InVT.isVector())
1669 // If the InOp is promoted to the same size, convert it. Otherwise,
1670 // fall out of the switch and widen the promoted input.
1671 InOp = GetPromotedInteger(InOp);
1672 InVT = InOp.getValueType();
1673 if (WidenVT.bitsEq(InVT))
1674 return DAG.getNode(ISD::BITCAST, dl, WidenVT, InOp);
1676 case TargetLowering::TypeSoftenFloat:
1677 case TargetLowering::TypeExpandInteger:
1678 case TargetLowering::TypeExpandFloat:
1679 case TargetLowering::TypeScalarizeVector:
1680 case TargetLowering::TypeSplitVector:
1682 case TargetLowering::TypeWidenVector:
1683 // If the InOp is widened to the same size, convert it. Otherwise, fall
1684 // out of the switch and widen the widened input.
1685 InOp = GetWidenedVector(InOp);
1686 InVT = InOp.getValueType();
1687 if (WidenVT.bitsEq(InVT))
1688 // The input widens to the same size. Convert to the widen value.
1689 return DAG.getNode(ISD::BITCAST, dl, WidenVT, InOp);
1693 unsigned WidenSize = WidenVT.getSizeInBits();
1694 unsigned InSize = InVT.getSizeInBits();
1695 // x86mmx is not an acceptable vector element type, so don't try.
1696 if (WidenSize % InSize == 0 && InVT != MVT::x86mmx) {
1697 // Determine new input vector type. The new input vector type will use
1698 // the same element type (if its a vector) or use the input type as a
1699 // vector. It is the same size as the type to widen to.
1701 unsigned NewNumElts = WidenSize / InSize;
1702 if (InVT.isVector()) {
1703 EVT InEltVT = InVT.getVectorElementType();
1704 NewInVT = EVT::getVectorVT(*DAG.getContext(), InEltVT,
1705 WidenSize / InEltVT.getSizeInBits());
1707 NewInVT = EVT::getVectorVT(*DAG.getContext(), InVT, NewNumElts);
1710 if (TLI.isTypeLegal(NewInVT)) {
1711 // Because the result and the input are different vector types, widening
1712 // the result could create a legal type but widening the input might make
1713 // it an illegal type that might lead to repeatedly splitting the input
1714 // and then widening it. To avoid this, we widen the input only if
1715 // it results in a legal type.
1716 SmallVector<SDValue, 16> Ops(NewNumElts);
1717 SDValue UndefVal = DAG.getUNDEF(InVT);
1719 for (unsigned i = 1; i < NewNumElts; ++i)
1723 if (InVT.isVector())
1724 NewVec = DAG.getNode(ISD::CONCAT_VECTORS, dl,
1725 NewInVT, &Ops[0], NewNumElts);
1727 NewVec = DAG.getNode(ISD::BUILD_VECTOR, dl,
1728 NewInVT, &Ops[0], NewNumElts);
1729 return DAG.getNode(ISD::BITCAST, dl, WidenVT, NewVec);
1733 return CreateStackStoreLoad(InOp, WidenVT);
1736 SDValue DAGTypeLegalizer::WidenVecRes_BUILD_VECTOR(SDNode *N) {
1737 DebugLoc dl = N->getDebugLoc();
1738 // Build a vector with undefined for the new nodes.
1739 EVT VT = N->getValueType(0);
1740 EVT EltVT = VT.getVectorElementType();
1741 unsigned NumElts = VT.getVectorNumElements();
1743 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1744 unsigned WidenNumElts = WidenVT.getVectorNumElements();
1746 SmallVector<SDValue, 16> NewOps(N->op_begin(), N->op_end());
1747 NewOps.reserve(WidenNumElts);
1748 for (unsigned i = NumElts; i < WidenNumElts; ++i)
1749 NewOps.push_back(DAG.getUNDEF(EltVT));
1751 return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &NewOps[0], NewOps.size());
1754 SDValue DAGTypeLegalizer::WidenVecRes_CONCAT_VECTORS(SDNode *N) {
1755 EVT InVT = N->getOperand(0).getValueType();
1756 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1757 DebugLoc dl = N->getDebugLoc();
1758 unsigned WidenNumElts = WidenVT.getVectorNumElements();
1759 unsigned NumInElts = InVT.getVectorNumElements();
1760 unsigned NumOperands = N->getNumOperands();
1762 bool InputWidened = false; // Indicates we need to widen the input.
1763 if (getTypeAction(InVT) != TargetLowering::TypeWidenVector) {
1764 if (WidenVT.getVectorNumElements() % InVT.getVectorNumElements() == 0) {
1765 // Add undef vectors to widen to correct length.
1766 unsigned NumConcat = WidenVT.getVectorNumElements() /
1767 InVT.getVectorNumElements();
1768 SDValue UndefVal = DAG.getUNDEF(InVT);
1769 SmallVector<SDValue, 16> Ops(NumConcat);
1770 for (unsigned i=0; i < NumOperands; ++i)
1771 Ops[i] = N->getOperand(i);
1772 for (unsigned i = NumOperands; i != NumConcat; ++i)
1774 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &Ops[0], NumConcat);
1777 InputWidened = true;
1778 if (WidenVT == TLI.getTypeToTransformTo(*DAG.getContext(), InVT)) {
1779 // The inputs and the result are widen to the same value.
1781 for (i=1; i < NumOperands; ++i)
1782 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
1785 if (i == NumOperands)
1786 // Everything but the first operand is an UNDEF so just return the
1787 // widened first operand.
1788 return GetWidenedVector(N->getOperand(0));
1790 if (NumOperands == 2) {
1791 // Replace concat of two operands with a shuffle.
1792 SmallVector<int, 16> MaskOps(WidenNumElts, -1);
1793 for (unsigned i = 0; i < NumInElts; ++i) {
1795 MaskOps[i + NumInElts] = i + WidenNumElts;
1797 return DAG.getVectorShuffle(WidenVT, dl,
1798 GetWidenedVector(N->getOperand(0)),
1799 GetWidenedVector(N->getOperand(1)),
1805 // Fall back to use extracts and build vector.
1806 EVT EltVT = WidenVT.getVectorElementType();
1807 SmallVector<SDValue, 16> Ops(WidenNumElts);
1809 for (unsigned i=0; i < NumOperands; ++i) {
1810 SDValue InOp = N->getOperand(i);
1812 InOp = GetWidenedVector(InOp);
1813 for (unsigned j=0; j < NumInElts; ++j)
1814 Ops[Idx++] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
1815 DAG.getIntPtrConstant(j));
1817 SDValue UndefVal = DAG.getUNDEF(EltVT);
1818 for (; Idx < WidenNumElts; ++Idx)
1819 Ops[Idx] = UndefVal;
1820 return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], WidenNumElts);
1823 SDValue DAGTypeLegalizer::WidenVecRes_CONVERT_RNDSAT(SDNode *N) {
1824 DebugLoc dl = N->getDebugLoc();
1825 SDValue InOp = N->getOperand(0);
1826 SDValue RndOp = N->getOperand(3);
1827 SDValue SatOp = N->getOperand(4);
1829 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1830 unsigned WidenNumElts = WidenVT.getVectorNumElements();
1832 EVT InVT = InOp.getValueType();
1833 EVT InEltVT = InVT.getVectorElementType();
1834 EVT InWidenVT = EVT::getVectorVT(*DAG.getContext(), InEltVT, WidenNumElts);
1836 SDValue DTyOp = DAG.getValueType(WidenVT);
1837 SDValue STyOp = DAG.getValueType(InWidenVT);
1838 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
1840 unsigned InVTNumElts = InVT.getVectorNumElements();
1841 if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
1842 InOp = GetWidenedVector(InOp);
1843 InVT = InOp.getValueType();
1844 InVTNumElts = InVT.getVectorNumElements();
1845 if (InVTNumElts == WidenNumElts)
1846 return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
1850 if (TLI.isTypeLegal(InWidenVT)) {
1851 // Because the result and the input are different vector types, widening
1852 // the result could create a legal type but widening the input might make
1853 // it an illegal type that might lead to repeatedly splitting the input
1854 // and then widening it. To avoid this, we widen the input only if
1855 // it results in a legal type.
1856 if (WidenNumElts % InVTNumElts == 0) {
1857 // Widen the input and call convert on the widened input vector.
1858 unsigned NumConcat = WidenNumElts/InVTNumElts;
1859 SmallVector<SDValue, 16> Ops(NumConcat);
1861 SDValue UndefVal = DAG.getUNDEF(InVT);
1862 for (unsigned i = 1; i != NumConcat; ++i)
1865 InOp = DAG.getNode(ISD::CONCAT_VECTORS, dl, InWidenVT, &Ops[0],NumConcat);
1866 return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
1870 if (InVTNumElts % WidenNumElts == 0) {
1871 // Extract the input and convert the shorten input vector.
1872 InOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InWidenVT, InOp,
1873 DAG.getIntPtrConstant(0));
1874 return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
1879 // Otherwise unroll into some nasty scalar code and rebuild the vector.
1880 SmallVector<SDValue, 16> Ops(WidenNumElts);
1881 EVT EltVT = WidenVT.getVectorElementType();
1882 DTyOp = DAG.getValueType(EltVT);
1883 STyOp = DAG.getValueType(InEltVT);
1885 unsigned MinElts = std::min(InVTNumElts, WidenNumElts);
1887 for (i=0; i < MinElts; ++i) {
1888 SDValue ExtVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, InEltVT, InOp,
1889 DAG.getIntPtrConstant(i));
1890 Ops[i] = DAG.getConvertRndSat(WidenVT, dl, ExtVal, DTyOp, STyOp, RndOp,
1894 SDValue UndefVal = DAG.getUNDEF(EltVT);
1895 for (; i < WidenNumElts; ++i)
1898 return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], WidenNumElts);
1901 SDValue DAGTypeLegalizer::WidenVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
1902 EVT VT = N->getValueType(0);
1903 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1904 unsigned WidenNumElts = WidenVT.getVectorNumElements();
1905 SDValue InOp = N->getOperand(0);
1906 SDValue Idx = N->getOperand(1);
1907 DebugLoc dl = N->getDebugLoc();
1909 if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
1910 InOp = GetWidenedVector(InOp);
1912 EVT InVT = InOp.getValueType();
1914 // Check if we can just return the input vector after widening.
1915 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1916 if (IdxVal == 0 && InVT == WidenVT)
1919 // Check if we can extract from the vector.
1920 unsigned InNumElts = InVT.getVectorNumElements();
1921 if (IdxVal % WidenNumElts == 0 && IdxVal + WidenNumElts < InNumElts)
1922 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, InOp, Idx);
1924 // We could try widening the input to the right length but for now, extract
1925 // the original elements, fill the rest with undefs and build a vector.
1926 SmallVector<SDValue, 16> Ops(WidenNumElts);
1927 EVT EltVT = VT.getVectorElementType();
1928 unsigned NumElts = VT.getVectorNumElements();
1930 for (i=0; i < NumElts; ++i)
1931 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
1932 DAG.getIntPtrConstant(IdxVal+i));
1934 SDValue UndefVal = DAG.getUNDEF(EltVT);
1935 for (; i < WidenNumElts; ++i)
1937 return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], WidenNumElts);
1940 SDValue DAGTypeLegalizer::WidenVecRes_INSERT_VECTOR_ELT(SDNode *N) {
1941 SDValue InOp = GetWidenedVector(N->getOperand(0));
1942 return DAG.getNode(ISD::INSERT_VECTOR_ELT, N->getDebugLoc(),
1943 InOp.getValueType(), InOp,
1944 N->getOperand(1), N->getOperand(2));
1947 SDValue DAGTypeLegalizer::WidenVecRes_LOAD(SDNode *N) {
1948 LoadSDNode *LD = cast<LoadSDNode>(N);
1949 ISD::LoadExtType ExtType = LD->getExtensionType();
1952 SmallVector<SDValue, 16> LdChain; // Chain for the series of load
1953 if (ExtType != ISD::NON_EXTLOAD)
1954 Result = GenWidenVectorExtLoads(LdChain, LD, ExtType);
1956 Result = GenWidenVectorLoads(LdChain, LD);
1958 // If we generate a single load, we can use that for the chain. Otherwise,
1959 // build a factor node to remember the multiple loads are independent and
1962 if (LdChain.size() == 1)
1963 NewChain = LdChain[0];
1965 NewChain = DAG.getNode(ISD::TokenFactor, LD->getDebugLoc(), MVT::Other,
1966 &LdChain[0], LdChain.size());
1968 // Modified the chain - switch anything that used the old chain to use
1970 ReplaceValueWith(SDValue(N, 1), NewChain);
1975 SDValue DAGTypeLegalizer::WidenVecRes_SCALAR_TO_VECTOR(SDNode *N) {
1976 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1977 return DAG.getNode(ISD::SCALAR_TO_VECTOR, N->getDebugLoc(),
1978 WidenVT, N->getOperand(0));
1981 SDValue DAGTypeLegalizer::WidenVecRes_SELECT(SDNode *N) {
1982 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1983 unsigned WidenNumElts = WidenVT.getVectorNumElements();
1985 SDValue Cond1 = N->getOperand(0);
1986 EVT CondVT = Cond1.getValueType();
1987 if (CondVT.isVector()) {
1988 EVT CondEltVT = CondVT.getVectorElementType();
1989 EVT CondWidenVT = EVT::getVectorVT(*DAG.getContext(),
1990 CondEltVT, WidenNumElts);
1991 if (getTypeAction(CondVT) == TargetLowering::TypeWidenVector)
1992 Cond1 = GetWidenedVector(Cond1);
1994 if (Cond1.getValueType() != CondWidenVT)
1995 Cond1 = ModifyToType(Cond1, CondWidenVT);
1998 SDValue InOp1 = GetWidenedVector(N->getOperand(1));
1999 SDValue InOp2 = GetWidenedVector(N->getOperand(2));
2000 assert(InOp1.getValueType() == WidenVT && InOp2.getValueType() == WidenVT);
2001 return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
2002 WidenVT, Cond1, InOp1, InOp2);
2005 SDValue DAGTypeLegalizer::WidenVecRes_SELECT_CC(SDNode *N) {
2006 SDValue InOp1 = GetWidenedVector(N->getOperand(2));
2007 SDValue InOp2 = GetWidenedVector(N->getOperand(3));
2008 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
2009 InOp1.getValueType(), N->getOperand(0),
2010 N->getOperand(1), InOp1, InOp2, N->getOperand(4));
2013 SDValue DAGTypeLegalizer::WidenVecRes_SETCC(SDNode *N) {
2014 assert(N->getValueType(0).isVector() ==
2015 N->getOperand(0).getValueType().isVector() &&
2016 "Scalar/Vector type mismatch");
2017 if (N->getValueType(0).isVector()) return WidenVecRes_VSETCC(N);
2019 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2020 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2021 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2022 return DAG.getNode(ISD::SETCC, N->getDebugLoc(), WidenVT,
2023 InOp1, InOp2, N->getOperand(2));
2026 SDValue DAGTypeLegalizer::WidenVecRes_UNDEF(SDNode *N) {
2027 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2028 return DAG.getUNDEF(WidenVT);
2031 SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N) {
2032 EVT VT = N->getValueType(0);
2033 DebugLoc dl = N->getDebugLoc();
2035 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2036 unsigned NumElts = VT.getVectorNumElements();
2037 unsigned WidenNumElts = WidenVT.getVectorNumElements();
2039 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2040 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2042 // Adjust mask based on new input vector length.
2043 SmallVector<int, 16> NewMask;
2044 for (unsigned i = 0; i != NumElts; ++i) {
2045 int Idx = N->getMaskElt(i);
2046 if (Idx < (int)NumElts)
2047 NewMask.push_back(Idx);
2049 NewMask.push_back(Idx - NumElts + WidenNumElts);
2051 for (unsigned i = NumElts; i != WidenNumElts; ++i)
2052 NewMask.push_back(-1);
2053 return DAG.getVectorShuffle(WidenVT, dl, InOp1, InOp2, &NewMask[0]);
2056 SDValue DAGTypeLegalizer::WidenVecRes_VSETCC(SDNode *N) {
2057 assert(N->getValueType(0).isVector() &&
2058 N->getOperand(0).getValueType().isVector() &&
2059 "Operands must be vectors");
2060 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2061 unsigned WidenNumElts = WidenVT.getVectorNumElements();
2063 SDValue InOp1 = N->getOperand(0);
2064 EVT InVT = InOp1.getValueType();
2065 assert(InVT.isVector() && "can not widen non vector type");
2066 EVT WidenInVT = EVT::getVectorVT(*DAG.getContext(),
2067 InVT.getVectorElementType(), WidenNumElts);
2068 InOp1 = GetWidenedVector(InOp1);
2069 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2071 // Assume that the input and output will be widen appropriately. If not,
2072 // we will have to unroll it at some point.
2073 assert(InOp1.getValueType() == WidenInVT &&
2074 InOp2.getValueType() == WidenInVT &&
2075 "Input not widened to expected type!");
2077 return DAG.getNode(ISD::SETCC, N->getDebugLoc(),
2078 WidenVT, InOp1, InOp2, N->getOperand(2));
2082 //===----------------------------------------------------------------------===//
2083 // Widen Vector Operand
2084 //===----------------------------------------------------------------------===//
2085 bool DAGTypeLegalizer::WidenVectorOperand(SDNode *N, unsigned OpNo) {
2086 DEBUG(dbgs() << "Widen node operand " << OpNo << ": ";
2089 SDValue Res = SDValue();
2091 // See if the target wants to custom widen this node.
2092 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
2095 switch (N->getOpcode()) {
2098 dbgs() << "WidenVectorOperand op #" << OpNo << ": ";
2102 llvm_unreachable("Do not know how to widen this operator's operand!");
2104 case ISD::BITCAST: Res = WidenVecOp_BITCAST(N); break;
2105 case ISD::CONCAT_VECTORS: Res = WidenVecOp_CONCAT_VECTORS(N); break;
2106 case ISD::EXTRACT_SUBVECTOR: Res = WidenVecOp_EXTRACT_SUBVECTOR(N); break;
2107 case ISD::EXTRACT_VECTOR_ELT: Res = WidenVecOp_EXTRACT_VECTOR_ELT(N); break;
2108 case ISD::STORE: Res = WidenVecOp_STORE(N); break;
2109 case ISD::SETCC: Res = WidenVecOp_SETCC(N); break;
2111 case ISD::FP_EXTEND:
2112 case ISD::FP_TO_SINT:
2113 case ISD::FP_TO_UINT:
2114 case ISD::SINT_TO_FP:
2115 case ISD::UINT_TO_FP:
2117 case ISD::SIGN_EXTEND:
2118 case ISD::ZERO_EXTEND:
2119 case ISD::ANY_EXTEND:
2120 Res = WidenVecOp_Convert(N);
2124 // If Res is null, the sub-method took care of registering the result.
2125 if (!Res.getNode()) return false;
2127 // If the result is N, the sub-method updated N in place. Tell the legalizer
2129 if (Res.getNode() == N)
2133 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
2134 "Invalid operand expansion");
2136 ReplaceValueWith(SDValue(N, 0), Res);
2140 SDValue DAGTypeLegalizer::WidenVecOp_Convert(SDNode *N) {
2141 // Since the result is legal and the input is illegal, it is unlikely
2142 // that we can fix the input to a legal type so unroll the convert
2143 // into some scalar code and create a nasty build vector.
2144 EVT VT = N->getValueType(0);
2145 EVT EltVT = VT.getVectorElementType();
2146 DebugLoc dl = N->getDebugLoc();
2147 unsigned NumElts = VT.getVectorNumElements();
2148 SDValue InOp = N->getOperand(0);
2149 if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
2150 InOp = GetWidenedVector(InOp);
2151 EVT InVT = InOp.getValueType();
2152 EVT InEltVT = InVT.getVectorElementType();
2154 unsigned Opcode = N->getOpcode();
2155 SmallVector<SDValue, 16> Ops(NumElts);
2156 for (unsigned i=0; i < NumElts; ++i)
2157 Ops[i] = DAG.getNode(Opcode, dl, EltVT,
2158 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, InEltVT, InOp,
2159 DAG.getIntPtrConstant(i)));
2161 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElts);
2164 SDValue DAGTypeLegalizer::WidenVecOp_BITCAST(SDNode *N) {
2165 EVT VT = N->getValueType(0);
2166 SDValue InOp = GetWidenedVector(N->getOperand(0));
2167 EVT InWidenVT = InOp.getValueType();
2168 DebugLoc dl = N->getDebugLoc();
2170 // Check if we can convert between two legal vector types and extract.
2171 unsigned InWidenSize = InWidenVT.getSizeInBits();
2172 unsigned Size = VT.getSizeInBits();
2173 // x86mmx is not an acceptable vector element type, so don't try.
2174 if (InWidenSize % Size == 0 && !VT.isVector() && VT != MVT::x86mmx) {
2175 unsigned NewNumElts = InWidenSize / Size;
2176 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), VT, NewNumElts);
2177 if (TLI.isTypeLegal(NewVT)) {
2178 SDValue BitOp = DAG.getNode(ISD::BITCAST, dl, NewVT, InOp);
2179 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, BitOp,
2180 DAG.getIntPtrConstant(0));
2184 return CreateStackStoreLoad(InOp, VT);
2187 SDValue DAGTypeLegalizer::WidenVecOp_CONCAT_VECTORS(SDNode *N) {
2188 // If the input vector is not legal, it is likely that we will not find a
2189 // legal vector of the same size. Replace the concatenate vector with a
2190 // nasty build vector.
2191 EVT VT = N->getValueType(0);
2192 EVT EltVT = VT.getVectorElementType();
2193 DebugLoc dl = N->getDebugLoc();
2194 unsigned NumElts = VT.getVectorNumElements();
2195 SmallVector<SDValue, 16> Ops(NumElts);
2197 EVT InVT = N->getOperand(0).getValueType();
2198 unsigned NumInElts = InVT.getVectorNumElements();
2201 unsigned NumOperands = N->getNumOperands();
2202 for (unsigned i=0; i < NumOperands; ++i) {
2203 SDValue InOp = N->getOperand(i);
2204 if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
2205 InOp = GetWidenedVector(InOp);
2206 for (unsigned j=0; j < NumInElts; ++j)
2207 Ops[Idx++] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
2208 DAG.getIntPtrConstant(j));
2210 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElts);
2213 SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
2214 SDValue InOp = GetWidenedVector(N->getOperand(0));
2215 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, N->getDebugLoc(),
2216 N->getValueType(0), InOp, N->getOperand(1));
2219 SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
2220 SDValue InOp = GetWidenedVector(N->getOperand(0));
2221 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(),
2222 N->getValueType(0), InOp, N->getOperand(1));
2225 SDValue DAGTypeLegalizer::WidenVecOp_STORE(SDNode *N) {
2226 // We have to widen the value but we want only to store the original
2228 StoreSDNode *ST = cast<StoreSDNode>(N);
2230 SmallVector<SDValue, 16> StChain;
2231 if (ST->isTruncatingStore())
2232 GenWidenVectorTruncStores(StChain, ST);
2234 GenWidenVectorStores(StChain, ST);
2236 if (StChain.size() == 1)
2239 return DAG.getNode(ISD::TokenFactor, ST->getDebugLoc(),
2240 MVT::Other,&StChain[0],StChain.size());
2243 SDValue DAGTypeLegalizer::WidenVecOp_SETCC(SDNode *N) {
2244 SDValue InOp0 = GetWidenedVector(N->getOperand(0));
2245 SDValue InOp1 = GetWidenedVector(N->getOperand(1));
2246 DebugLoc dl = N->getDebugLoc();
2248 // WARNING: In this code we widen the compare instruction with garbage.
2249 // This garbage may contain denormal floats which may be slow. Is this a real
2250 // concern ? Should we zero the unused lanes if this is a float compare ?
2252 // Get a new SETCC node to compare the newly widened operands.
2253 // Only some of the compared elements are legal.
2254 EVT SVT = TLI.getSetCCResultType(InOp0.getValueType());
2255 SDValue WideSETCC = DAG.getNode(ISD::SETCC, N->getDebugLoc(),
2256 SVT, InOp0, InOp1, N->getOperand(2));
2258 // Extract the needed results from the result vector.
2259 EVT ResVT = EVT::getVectorVT(*DAG.getContext(),
2260 SVT.getVectorElementType(),
2261 N->getValueType(0).getVectorNumElements());
2262 SDValue CC = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
2263 ResVT, WideSETCC, DAG.getIntPtrConstant(0));
2265 return PromoteTargetBoolean(CC, N->getValueType(0));
2269 //===----------------------------------------------------------------------===//
2270 // Vector Widening Utilities
2271 //===----------------------------------------------------------------------===//
2273 // Utility function to find the type to chop up a widen vector for load/store
2274 // TLI: Target lowering used to determine legal types.
2275 // Width: Width left need to load/store.
2276 // WidenVT: The widen vector type to load to/store from
2277 // Align: If 0, don't allow use of a wider type
2278 // WidenEx: If Align is not 0, the amount additional we can load/store from.
2280 static EVT FindMemType(SelectionDAG& DAG, const TargetLowering &TLI,
2281 unsigned Width, EVT WidenVT,
2282 unsigned Align = 0, unsigned WidenEx = 0) {
2283 EVT WidenEltVT = WidenVT.getVectorElementType();
2284 unsigned WidenWidth = WidenVT.getSizeInBits();
2285 unsigned WidenEltWidth = WidenEltVT.getSizeInBits();
2286 unsigned AlignInBits = Align*8;
2288 // If we have one element to load/store, return it.
2289 EVT RetVT = WidenEltVT;
2290 if (Width == WidenEltWidth)
2293 // See if there is larger legal integer than the element type to load/store
2295 for (VT = (unsigned)MVT::LAST_INTEGER_VALUETYPE;
2296 VT >= (unsigned)MVT::FIRST_INTEGER_VALUETYPE; --VT) {
2297 EVT MemVT((MVT::SimpleValueType) VT);
2298 unsigned MemVTWidth = MemVT.getSizeInBits();
2299 if (MemVT.getSizeInBits() <= WidenEltWidth)
2301 if (TLI.isTypeLegal(MemVT) && (WidenWidth % MemVTWidth) == 0 &&
2302 isPowerOf2_32(WidenWidth / MemVTWidth) &&
2303 (MemVTWidth <= Width ||
2304 (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
2310 // See if there is a larger vector type to load/store that has the same vector
2311 // element type and is evenly divisible with the WidenVT.
2312 for (VT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
2313 VT >= (unsigned)MVT::FIRST_VECTOR_VALUETYPE; --VT) {
2314 EVT MemVT = (MVT::SimpleValueType) VT;
2315 unsigned MemVTWidth = MemVT.getSizeInBits();
2316 if (TLI.isTypeLegal(MemVT) && WidenEltVT == MemVT.getVectorElementType() &&
2317 (WidenWidth % MemVTWidth) == 0 &&
2318 isPowerOf2_32(WidenWidth / MemVTWidth) &&
2319 (MemVTWidth <= Width ||
2320 (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
2321 if (RetVT.getSizeInBits() < MemVTWidth || MemVT == WidenVT)
2329 // Builds a vector type from scalar loads
2330 // VecTy: Resulting Vector type
2331 // LDOps: Load operators to build a vector type
2332 // [Start,End) the list of loads to use.
2333 static SDValue BuildVectorFromScalar(SelectionDAG& DAG, EVT VecTy,
2334 SmallVector<SDValue, 16>& LdOps,
2335 unsigned Start, unsigned End) {
2336 DebugLoc dl = LdOps[Start].getDebugLoc();
2337 EVT LdTy = LdOps[Start].getValueType();
2338 unsigned Width = VecTy.getSizeInBits();
2339 unsigned NumElts = Width / LdTy.getSizeInBits();
2340 EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), LdTy, NumElts);
2343 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT,LdOps[Start]);
2345 for (unsigned i = Start + 1; i != End; ++i) {
2346 EVT NewLdTy = LdOps[i].getValueType();
2347 if (NewLdTy != LdTy) {
2348 NumElts = Width / NewLdTy.getSizeInBits();
2349 NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewLdTy, NumElts);
2350 VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, VecOp);
2351 // Readjust position and vector position based on new load type
2352 Idx = Idx * LdTy.getSizeInBits() / NewLdTy.getSizeInBits();
2355 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVecVT, VecOp, LdOps[i],
2356 DAG.getIntPtrConstant(Idx++));
2358 return DAG.getNode(ISD::BITCAST, dl, VecTy, VecOp);
2361 SDValue DAGTypeLegalizer::GenWidenVectorLoads(SmallVector<SDValue, 16> &LdChain,
2363 // The strategy assumes that we can efficiently load powers of two widths.
2364 // The routines chops the vector into the largest vector loads with the same
2365 // element type or scalar loads and then recombines it to the widen vector
2367 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),LD->getValueType(0));
2368 unsigned WidenWidth = WidenVT.getSizeInBits();
2369 EVT LdVT = LD->getMemoryVT();
2370 DebugLoc dl = LD->getDebugLoc();
2371 assert(LdVT.isVector() && WidenVT.isVector());
2372 assert(LdVT.getVectorElementType() == WidenVT.getVectorElementType());
2375 SDValue Chain = LD->getChain();
2376 SDValue BasePtr = LD->getBasePtr();
2377 unsigned Align = LD->getAlignment();
2378 bool isVolatile = LD->isVolatile();
2379 bool isNonTemporal = LD->isNonTemporal();
2380 bool isInvariant = LD->isInvariant();
2382 int LdWidth = LdVT.getSizeInBits();
2383 int WidthDiff = WidenWidth - LdWidth; // Difference
2384 unsigned LdAlign = (isVolatile) ? 0 : Align; // Allow wider loads
2386 // Find the vector type that can load from.
2387 EVT NewVT = FindMemType(DAG, TLI, LdWidth, WidenVT, LdAlign, WidthDiff);
2388 int NewVTWidth = NewVT.getSizeInBits();
2389 SDValue LdOp = DAG.getLoad(NewVT, dl, Chain, BasePtr, LD->getPointerInfo(),
2390 isVolatile, isNonTemporal, isInvariant, Align);
2391 LdChain.push_back(LdOp.getValue(1));
2393 // Check if we can load the element with one instruction
2394 if (LdWidth <= NewVTWidth) {
2395 if (!NewVT.isVector()) {
2396 unsigned NumElts = WidenWidth / NewVTWidth;
2397 EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewVT, NumElts);
2398 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT, LdOp);
2399 return DAG.getNode(ISD::BITCAST, dl, WidenVT, VecOp);
2401 if (NewVT == WidenVT)
2404 assert(WidenWidth % NewVTWidth == 0);
2405 unsigned NumConcat = WidenWidth / NewVTWidth;
2406 SmallVector<SDValue, 16> ConcatOps(NumConcat);
2407 SDValue UndefVal = DAG.getUNDEF(NewVT);
2408 ConcatOps[0] = LdOp;
2409 for (unsigned i = 1; i != NumConcat; ++i)
2410 ConcatOps[i] = UndefVal;
2411 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &ConcatOps[0],
2415 // Load vector by using multiple loads from largest vector to scalar
2416 SmallVector<SDValue, 16> LdOps;
2417 LdOps.push_back(LdOp);
2419 LdWidth -= NewVTWidth;
2420 unsigned Offset = 0;
2422 while (LdWidth > 0) {
2423 unsigned Increment = NewVTWidth / 8;
2424 Offset += Increment;
2425 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
2426 DAG.getIntPtrConstant(Increment));
2429 if (LdWidth < NewVTWidth) {
2430 // Our current type we are using is too large, find a better size
2431 NewVT = FindMemType(DAG, TLI, LdWidth, WidenVT, LdAlign, WidthDiff);
2432 NewVTWidth = NewVT.getSizeInBits();
2433 L = DAG.getLoad(NewVT, dl, Chain, BasePtr,
2434 LD->getPointerInfo().getWithOffset(Offset), isVolatile,
2435 isNonTemporal, isInvariant, MinAlign(Align, Increment));
2436 LdChain.push_back(L.getValue(1));
2437 if (L->getValueType(0).isVector()) {
2438 SmallVector<SDValue, 16> Loads;
2440 unsigned size = L->getValueSizeInBits(0);
2441 while (size < LdOp->getValueSizeInBits(0)) {
2442 Loads.push_back(DAG.getUNDEF(L->getValueType(0)));
2443 size += L->getValueSizeInBits(0);
2445 L = DAG.getNode(ISD::CONCAT_VECTORS, dl, LdOp->getValueType(0),
2446 &Loads[0], Loads.size());
2449 L = DAG.getLoad(NewVT, dl, Chain, BasePtr,
2450 LD->getPointerInfo().getWithOffset(Offset), isVolatile,
2451 isNonTemporal, isInvariant, MinAlign(Align, Increment));
2452 LdChain.push_back(L.getValue(1));
2458 LdWidth -= NewVTWidth;
2461 // Build the vector from the loads operations
2462 unsigned End = LdOps.size();
2463 if (!LdOps[0].getValueType().isVector())
2464 // All the loads are scalar loads.
2465 return BuildVectorFromScalar(DAG, WidenVT, LdOps, 0, End);
2467 // If the load contains vectors, build the vector using concat vector.
2468 // All of the vectors used to loads are power of 2 and the scalars load
2469 // can be combined to make a power of 2 vector.
2470 SmallVector<SDValue, 16> ConcatOps(End);
2473 EVT LdTy = LdOps[i].getValueType();
2474 // First combine the scalar loads to a vector
2475 if (!LdTy.isVector()) {
2476 for (--i; i >= 0; --i) {
2477 LdTy = LdOps[i].getValueType();
2478 if (LdTy.isVector())
2481 ConcatOps[--Idx] = BuildVectorFromScalar(DAG, LdTy, LdOps, i+1, End);
2483 ConcatOps[--Idx] = LdOps[i];
2484 for (--i; i >= 0; --i) {
2485 EVT NewLdTy = LdOps[i].getValueType();
2486 if (NewLdTy != LdTy) {
2487 // Create a larger vector
2488 ConcatOps[End-1] = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewLdTy,
2489 &ConcatOps[Idx], End - Idx);
2493 ConcatOps[--Idx] = LdOps[i];
2496 if (WidenWidth == LdTy.getSizeInBits()*(End - Idx))
2497 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
2498 &ConcatOps[Idx], End - Idx);
2500 // We need to fill the rest with undefs to build the vector
2501 unsigned NumOps = WidenWidth / LdTy.getSizeInBits();
2502 SmallVector<SDValue, 16> WidenOps(NumOps);
2503 SDValue UndefVal = DAG.getUNDEF(LdTy);
2506 for (; i != End-Idx; ++i)
2507 WidenOps[i] = ConcatOps[Idx+i];
2508 for (; i != NumOps; ++i)
2509 WidenOps[i] = UndefVal;
2511 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &WidenOps[0],NumOps);
2515 DAGTypeLegalizer::GenWidenVectorExtLoads(SmallVector<SDValue, 16>& LdChain,
2517 ISD::LoadExtType ExtType) {
2518 // For extension loads, it may not be more efficient to chop up the vector
2519 // and then extended it. Instead, we unroll the load and build a new vector.
2520 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),LD->getValueType(0));
2521 EVT LdVT = LD->getMemoryVT();
2522 DebugLoc dl = LD->getDebugLoc();
2523 assert(LdVT.isVector() && WidenVT.isVector());
2526 SDValue Chain = LD->getChain();
2527 SDValue BasePtr = LD->getBasePtr();
2528 unsigned Align = LD->getAlignment();
2529 bool isVolatile = LD->isVolatile();
2530 bool isNonTemporal = LD->isNonTemporal();
2532 EVT EltVT = WidenVT.getVectorElementType();
2533 EVT LdEltVT = LdVT.getVectorElementType();
2534 unsigned NumElts = LdVT.getVectorNumElements();
2536 // Load each element and widen
2537 unsigned WidenNumElts = WidenVT.getVectorNumElements();
2538 SmallVector<SDValue, 16> Ops(WidenNumElts);
2539 unsigned Increment = LdEltVT.getSizeInBits() / 8;
2540 Ops[0] = DAG.getExtLoad(ExtType, dl, EltVT, Chain, BasePtr,
2541 LD->getPointerInfo(),
2542 LdEltVT, isVolatile, isNonTemporal, Align);
2543 LdChain.push_back(Ops[0].getValue(1));
2544 unsigned i = 0, Offset = Increment;
2545 for (i=1; i < NumElts; ++i, Offset += Increment) {
2546 SDValue NewBasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
2547 BasePtr, DAG.getIntPtrConstant(Offset));
2548 Ops[i] = DAG.getExtLoad(ExtType, dl, EltVT, Chain, NewBasePtr,
2549 LD->getPointerInfo().getWithOffset(Offset), LdEltVT,
2550 isVolatile, isNonTemporal, Align);
2551 LdChain.push_back(Ops[i].getValue(1));
2554 // Fill the rest with undefs
2555 SDValue UndefVal = DAG.getUNDEF(EltVT);
2556 for (; i != WidenNumElts; ++i)
2559 return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], Ops.size());
2563 void DAGTypeLegalizer::GenWidenVectorStores(SmallVector<SDValue, 16>& StChain,
2565 // The strategy assumes that we can efficiently store powers of two widths.
2566 // The routines chops the vector into the largest vector stores with the same
2567 // element type or scalar stores.
2568 SDValue Chain = ST->getChain();
2569 SDValue BasePtr = ST->getBasePtr();
2570 unsigned Align = ST->getAlignment();
2571 bool isVolatile = ST->isVolatile();
2572 bool isNonTemporal = ST->isNonTemporal();
2573 SDValue ValOp = GetWidenedVector(ST->getValue());
2574 DebugLoc dl = ST->getDebugLoc();
2576 EVT StVT = ST->getMemoryVT();
2577 unsigned StWidth = StVT.getSizeInBits();
2578 EVT ValVT = ValOp.getValueType();
2579 unsigned ValWidth = ValVT.getSizeInBits();
2580 EVT ValEltVT = ValVT.getVectorElementType();
2581 unsigned ValEltWidth = ValEltVT.getSizeInBits();
2582 assert(StVT.getVectorElementType() == ValEltVT);
2584 int Idx = 0; // current index to store
2585 unsigned Offset = 0; // offset from base to store
2586 while (StWidth != 0) {
2587 // Find the largest vector type we can store with
2588 EVT NewVT = FindMemType(DAG, TLI, StWidth, ValVT);
2589 unsigned NewVTWidth = NewVT.getSizeInBits();
2590 unsigned Increment = NewVTWidth / 8;
2591 if (NewVT.isVector()) {
2592 unsigned NumVTElts = NewVT.getVectorNumElements();
2594 SDValue EOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT, ValOp,
2595 DAG.getIntPtrConstant(Idx));
2596 StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr,
2597 ST->getPointerInfo().getWithOffset(Offset),
2598 isVolatile, isNonTemporal,
2599 MinAlign(Align, Offset)));
2600 StWidth -= NewVTWidth;
2601 Offset += Increment;
2603 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
2604 DAG.getIntPtrConstant(Increment));
2605 } while (StWidth != 0 && StWidth >= NewVTWidth);
2607 // Cast the vector to the scalar type we can store
2608 unsigned NumElts = ValWidth / NewVTWidth;
2609 EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewVT, NumElts);
2610 SDValue VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, ValOp);
2611 // Readjust index position based on new vector type
2612 Idx = Idx * ValEltWidth / NewVTWidth;
2614 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, VecOp,
2615 DAG.getIntPtrConstant(Idx++));
2616 StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr,
2617 ST->getPointerInfo().getWithOffset(Offset),
2618 isVolatile, isNonTemporal,
2619 MinAlign(Align, Offset)));
2620 StWidth -= NewVTWidth;
2621 Offset += Increment;
2622 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
2623 DAG.getIntPtrConstant(Increment));
2624 } while (StWidth != 0 && StWidth >= NewVTWidth);
2625 // Restore index back to be relative to the original widen element type
2626 Idx = Idx * NewVTWidth / ValEltWidth;
2632 DAGTypeLegalizer::GenWidenVectorTruncStores(SmallVector<SDValue, 16>& StChain,
2634 // For extension loads, it may not be more efficient to truncate the vector
2635 // and then store it. Instead, we extract each element and then store it.
2636 SDValue Chain = ST->getChain();
2637 SDValue BasePtr = ST->getBasePtr();
2638 unsigned Align = ST->getAlignment();
2639 bool isVolatile = ST->isVolatile();
2640 bool isNonTemporal = ST->isNonTemporal();
2641 SDValue ValOp = GetWidenedVector(ST->getValue());
2642 DebugLoc dl = ST->getDebugLoc();
2644 EVT StVT = ST->getMemoryVT();
2645 EVT ValVT = ValOp.getValueType();
2647 // It must be true that we the widen vector type is bigger than where
2648 // we need to store.
2649 assert(StVT.isVector() && ValOp.getValueType().isVector());
2650 assert(StVT.bitsLT(ValOp.getValueType()));
2652 // For truncating stores, we can not play the tricks of chopping legal
2653 // vector types and bit cast it to the right type. Instead, we unroll
2655 EVT StEltVT = StVT.getVectorElementType();
2656 EVT ValEltVT = ValVT.getVectorElementType();
2657 unsigned Increment = ValEltVT.getSizeInBits() / 8;
2658 unsigned NumElts = StVT.getVectorNumElements();
2659 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ValEltVT, ValOp,
2660 DAG.getIntPtrConstant(0));
2661 StChain.push_back(DAG.getTruncStore(Chain, dl, EOp, BasePtr,
2662 ST->getPointerInfo(), StEltVT,
2663 isVolatile, isNonTemporal, Align));
2664 unsigned Offset = Increment;
2665 for (unsigned i=1; i < NumElts; ++i, Offset += Increment) {
2666 SDValue NewBasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
2667 BasePtr, DAG.getIntPtrConstant(Offset));
2668 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ValEltVT, ValOp,
2669 DAG.getIntPtrConstant(0));
2670 StChain.push_back(DAG.getTruncStore(Chain, dl, EOp, NewBasePtr,
2671 ST->getPointerInfo().getWithOffset(Offset),
2672 StEltVT, isVolatile, isNonTemporal,
2673 MinAlign(Align, Offset)));
2677 /// Modifies a vector input (widen or narrows) to a vector of NVT. The
2678 /// input vector must have the same element type as NVT.
2679 SDValue DAGTypeLegalizer::ModifyToType(SDValue InOp, EVT NVT) {
2680 // Note that InOp might have been widened so it might already have
2681 // the right width or it might need be narrowed.
2682 EVT InVT = InOp.getValueType();
2683 assert(InVT.getVectorElementType() == NVT.getVectorElementType() &&
2684 "input and widen element type must match");
2685 DebugLoc dl = InOp.getDebugLoc();
2687 // Check if InOp already has the right width.
2691 unsigned InNumElts = InVT.getVectorNumElements();
2692 unsigned WidenNumElts = NVT.getVectorNumElements();
2693 if (WidenNumElts > InNumElts && WidenNumElts % InNumElts == 0) {
2694 unsigned NumConcat = WidenNumElts / InNumElts;
2695 SmallVector<SDValue, 16> Ops(NumConcat);
2696 SDValue UndefVal = DAG.getUNDEF(InVT);
2698 for (unsigned i = 1; i != NumConcat; ++i)
2701 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, &Ops[0], NumConcat);
2704 if (WidenNumElts < InNumElts && InNumElts % WidenNumElts)
2705 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, InOp,
2706 DAG.getIntPtrConstant(0));
2708 // Fall back to extract and build.
2709 SmallVector<SDValue, 16> Ops(WidenNumElts);
2710 EVT EltVT = NVT.getVectorElementType();
2711 unsigned MinNumElts = std::min(WidenNumElts, InNumElts);
2713 for (Idx = 0; Idx < MinNumElts; ++Idx)
2714 Ops[Idx] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
2715 DAG.getIntPtrConstant(Idx));
2717 SDValue UndefVal = DAG.getUNDEF(EltVT);
2718 for ( ; Idx < WidenNumElts; ++Idx)
2719 Ops[Idx] = UndefVal;
2720 return DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &Ops[0], WidenNumElts);