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/IR/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;
90 case ISD::SIGN_EXTEND:
94 case ISD::ZERO_EXTEND:
95 R = ScalarizeVecRes_UnaryOp(N);
117 R = ScalarizeVecRes_BinOp(N);
120 R = ScalarizeVecRes_TernaryOp(N);
124 // If R is null, the sub-method took care of registering the result.
126 SetScalarizedVector(SDValue(N, ResNo), R);
129 SDValue DAGTypeLegalizer::ScalarizeVecRes_BinOp(SDNode *N) {
130 SDValue LHS = GetScalarizedVector(N->getOperand(0));
131 SDValue RHS = GetScalarizedVector(N->getOperand(1));
132 return DAG.getNode(N->getOpcode(), SDLoc(N),
133 LHS.getValueType(), LHS, RHS);
136 SDValue DAGTypeLegalizer::ScalarizeVecRes_TernaryOp(SDNode *N) {
137 SDValue Op0 = GetScalarizedVector(N->getOperand(0));
138 SDValue Op1 = GetScalarizedVector(N->getOperand(1));
139 SDValue Op2 = GetScalarizedVector(N->getOperand(2));
140 return DAG.getNode(N->getOpcode(), SDLoc(N),
141 Op0.getValueType(), Op0, Op1, Op2);
144 SDValue DAGTypeLegalizer::ScalarizeVecRes_MERGE_VALUES(SDNode *N,
146 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
147 return GetScalarizedVector(Op);
150 SDValue DAGTypeLegalizer::ScalarizeVecRes_BITCAST(SDNode *N) {
151 EVT NewVT = N->getValueType(0).getVectorElementType();
152 return DAG.getNode(ISD::BITCAST, SDLoc(N),
153 NewVT, N->getOperand(0));
156 SDValue DAGTypeLegalizer::ScalarizeVecRes_BUILD_VECTOR(SDNode *N) {
157 EVT EltVT = N->getValueType(0).getVectorElementType();
158 SDValue InOp = N->getOperand(0);
159 // The BUILD_VECTOR operands may be of wider element types and
160 // we may need to truncate them back to the requested return type.
161 if (EltVT.isInteger())
162 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, InOp);
166 SDValue DAGTypeLegalizer::ScalarizeVecRes_CONVERT_RNDSAT(SDNode *N) {
167 EVT NewVT = N->getValueType(0).getVectorElementType();
168 SDValue Op0 = GetScalarizedVector(N->getOperand(0));
169 return DAG.getConvertRndSat(NewVT, SDLoc(N),
170 Op0, DAG.getValueType(NewVT),
171 DAG.getValueType(Op0.getValueType()),
174 cast<CvtRndSatSDNode>(N)->getCvtCode());
177 SDValue DAGTypeLegalizer::ScalarizeVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
178 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
179 N->getValueType(0).getVectorElementType(),
180 N->getOperand(0), N->getOperand(1));
183 SDValue DAGTypeLegalizer::ScalarizeVecRes_FP_ROUND(SDNode *N) {
184 EVT NewVT = N->getValueType(0).getVectorElementType();
185 SDValue Op = GetScalarizedVector(N->getOperand(0));
186 return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
187 NewVT, Op, N->getOperand(1));
190 SDValue DAGTypeLegalizer::ScalarizeVecRes_FPOWI(SDNode *N) {
191 SDValue Op = GetScalarizedVector(N->getOperand(0));
192 return DAG.getNode(ISD::FPOWI, SDLoc(N),
193 Op.getValueType(), Op, N->getOperand(1));
196 SDValue DAGTypeLegalizer::ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N) {
197 // The value to insert may have a wider type than the vector element type,
198 // so be sure to truncate it to the element type if necessary.
199 SDValue Op = N->getOperand(1);
200 EVT EltVT = N->getValueType(0).getVectorElementType();
201 if (Op.getValueType() != EltVT)
202 // FIXME: Can this happen for floating point types?
203 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, Op);
207 SDValue DAGTypeLegalizer::ScalarizeVecRes_LOAD(LoadSDNode *N) {
208 assert(N->isUnindexed() && "Indexed vector load?");
210 SDValue Result = DAG.getLoad(ISD::UNINDEXED,
211 N->getExtensionType(),
212 N->getValueType(0).getVectorElementType(),
214 N->getChain(), N->getBasePtr(),
215 DAG.getUNDEF(N->getBasePtr().getValueType()),
217 N->getMemoryVT().getVectorElementType(),
218 N->isVolatile(), N->isNonTemporal(),
219 N->isInvariant(), N->getOriginalAlignment());
221 // Legalized the chain result - switch anything that used the old chain to
223 ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
227 SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOp(SDNode *N) {
228 // Get the dest type - it doesn't always match the input type, e.g. int_to_fp.
229 EVT DestVT = N->getValueType(0).getVectorElementType();
230 SDValue Op = GetScalarizedVector(N->getOperand(0));
231 return DAG.getNode(N->getOpcode(), SDLoc(N), DestVT, Op);
234 SDValue DAGTypeLegalizer::ScalarizeVecRes_InregOp(SDNode *N) {
235 EVT EltVT = N->getValueType(0).getVectorElementType();
236 EVT ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT().getVectorElementType();
237 SDValue LHS = GetScalarizedVector(N->getOperand(0));
238 return DAG.getNode(N->getOpcode(), SDLoc(N), EltVT,
239 LHS, DAG.getValueType(ExtVT));
242 SDValue DAGTypeLegalizer::ScalarizeVecRes_SCALAR_TO_VECTOR(SDNode *N) {
243 // If the operand is wider than the vector element type then it is implicitly
244 // truncated. Make that explicit here.
245 EVT EltVT = N->getValueType(0).getVectorElementType();
246 SDValue InOp = N->getOperand(0);
247 if (InOp.getValueType() != EltVT)
248 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), EltVT, InOp);
252 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSELECT(SDNode *N) {
253 SDValue Cond = GetScalarizedVector(N->getOperand(0));
254 SDValue LHS = GetScalarizedVector(N->getOperand(1));
255 TargetLowering::BooleanContent ScalarBool = TLI.getBooleanContents(false);
256 TargetLowering::BooleanContent VecBool = TLI.getBooleanContents(true);
257 if (ScalarBool != VecBool) {
258 EVT CondVT = Cond.getValueType();
259 switch (ScalarBool) {
260 case TargetLowering::UndefinedBooleanContent:
262 case TargetLowering::ZeroOrOneBooleanContent:
263 assert(VecBool == TargetLowering::UndefinedBooleanContent ||
264 VecBool == TargetLowering::ZeroOrNegativeOneBooleanContent);
265 // Vector read from all ones, scalar expects a single 1 so mask.
266 Cond = DAG.getNode(ISD::AND, SDLoc(N), CondVT,
267 Cond, DAG.getConstant(1, CondVT));
269 case TargetLowering::ZeroOrNegativeOneBooleanContent:
270 assert(VecBool == TargetLowering::UndefinedBooleanContent ||
271 VecBool == TargetLowering::ZeroOrOneBooleanContent);
272 // Vector reads from a one, scalar from all ones so sign extend.
273 Cond = DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), CondVT,
274 Cond, DAG.getValueType(MVT::i1));
279 return DAG.getSelect(SDLoc(N),
280 LHS.getValueType(), Cond, LHS,
281 GetScalarizedVector(N->getOperand(2)));
284 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT(SDNode *N) {
285 SDValue LHS = GetScalarizedVector(N->getOperand(1));
286 return DAG.getSelect(SDLoc(N),
287 LHS.getValueType(), N->getOperand(0), LHS,
288 GetScalarizedVector(N->getOperand(2)));
291 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT_CC(SDNode *N) {
292 SDValue LHS = GetScalarizedVector(N->getOperand(2));
293 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), LHS.getValueType(),
294 N->getOperand(0), N->getOperand(1),
295 LHS, GetScalarizedVector(N->getOperand(3)),
299 SDValue DAGTypeLegalizer::ScalarizeVecRes_SETCC(SDNode *N) {
300 assert(N->getValueType(0).isVector() ==
301 N->getOperand(0).getValueType().isVector() &&
302 "Scalar/Vector type mismatch");
304 if (N->getValueType(0).isVector()) return ScalarizeVecRes_VSETCC(N);
306 SDValue LHS = GetScalarizedVector(N->getOperand(0));
307 SDValue RHS = GetScalarizedVector(N->getOperand(1));
310 // Turn it into a scalar SETCC.
311 return DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, N->getOperand(2));
314 SDValue DAGTypeLegalizer::ScalarizeVecRes_UNDEF(SDNode *N) {
315 return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
318 SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_SHUFFLE(SDNode *N) {
319 // Figure out if the scalar is the LHS or RHS and return it.
320 SDValue Arg = N->getOperand(2).getOperand(0);
321 if (Arg.getOpcode() == ISD::UNDEF)
322 return DAG.getUNDEF(N->getValueType(0).getVectorElementType());
323 unsigned Op = !cast<ConstantSDNode>(Arg)->isNullValue();
324 return GetScalarizedVector(N->getOperand(Op));
327 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSETCC(SDNode *N) {
328 assert(N->getValueType(0).isVector() &&
329 N->getOperand(0).getValueType().isVector() &&
330 "Operand types must be vectors");
332 SDValue LHS = GetScalarizedVector(N->getOperand(0));
333 SDValue RHS = GetScalarizedVector(N->getOperand(1));
334 EVT NVT = N->getValueType(0).getVectorElementType();
337 // Turn it into a scalar SETCC.
338 SDValue Res = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS,
340 // Vectors may have a different boolean contents to scalars. Promote the
341 // value appropriately.
342 ISD::NodeType ExtendCode =
343 TargetLowering::getExtendForContent(TLI.getBooleanContents(true));
344 return DAG.getNode(ExtendCode, DL, NVT, Res);
348 //===----------------------------------------------------------------------===//
349 // Operand Vector Scalarization <1 x ty> -> ty.
350 //===----------------------------------------------------------------------===//
352 bool DAGTypeLegalizer::ScalarizeVectorOperand(SDNode *N, unsigned OpNo) {
353 DEBUG(dbgs() << "Scalarize node operand " << OpNo << ": ";
356 SDValue Res = SDValue();
358 if (Res.getNode() == 0) {
359 switch (N->getOpcode()) {
362 dbgs() << "ScalarizeVectorOperand Op #" << OpNo << ": ";
366 llvm_unreachable("Do not know how to scalarize this operator's operand!");
368 Res = ScalarizeVecOp_BITCAST(N);
370 case ISD::ANY_EXTEND:
371 case ISD::ZERO_EXTEND:
372 case ISD::SIGN_EXTEND:
373 Res = ScalarizeVecOp_EXTEND(N);
375 case ISD::CONCAT_VECTORS:
376 Res = ScalarizeVecOp_CONCAT_VECTORS(N);
378 case ISD::EXTRACT_VECTOR_ELT:
379 Res = ScalarizeVecOp_EXTRACT_VECTOR_ELT(N);
382 Res = ScalarizeVecOp_STORE(cast<StoreSDNode>(N), OpNo);
387 // If the result is null, the sub-method took care of registering results etc.
388 if (!Res.getNode()) return false;
390 // If the result is N, the sub-method updated N in place. Tell the legalizer
392 if (Res.getNode() == N)
395 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
396 "Invalid operand expansion");
398 ReplaceValueWith(SDValue(N, 0), Res);
402 /// ScalarizeVecOp_BITCAST - If the value to convert is a vector that needs
403 /// to be scalarized, it must be <1 x ty>. Convert the element instead.
404 SDValue DAGTypeLegalizer::ScalarizeVecOp_BITCAST(SDNode *N) {
405 SDValue Elt = GetScalarizedVector(N->getOperand(0));
406 return DAG.getNode(ISD::BITCAST, SDLoc(N),
407 N->getValueType(0), Elt);
410 /// ScalarizeVecOp_EXTEND - If the value to extend is a vector that needs
411 /// to be scalarized, it must be <1 x ty>. Extend the element instead.
412 SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTEND(SDNode *N) {
413 assert(N->getValueType(0).getVectorNumElements() == 1 &&
414 "Unexected vector type!");
415 SDValue Elt = GetScalarizedVector(N->getOperand(0));
416 SmallVector<SDValue, 1> Ops(1);
417 Ops[0] = DAG.getNode(N->getOpcode(), SDLoc(N),
418 N->getValueType(0).getScalarType(), Elt);
419 // Revectorize the result so the types line up with what the uses of this
420 // expression expect.
421 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), N->getValueType(0),
425 /// ScalarizeVecOp_CONCAT_VECTORS - The vectors to concatenate have length one -
426 /// use a BUILD_VECTOR instead.
427 SDValue DAGTypeLegalizer::ScalarizeVecOp_CONCAT_VECTORS(SDNode *N) {
428 SmallVector<SDValue, 8> Ops(N->getNumOperands());
429 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
430 Ops[i] = GetScalarizedVector(N->getOperand(i));
431 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), N->getValueType(0),
432 &Ops[0], Ops.size());
435 /// ScalarizeVecOp_EXTRACT_VECTOR_ELT - If the input is a vector that needs to
436 /// be scalarized, it must be <1 x ty>, so just return the element, ignoring the
438 SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
439 SDValue Res = GetScalarizedVector(N->getOperand(0));
440 if (Res.getValueType() != N->getValueType(0))
441 Res = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0),
446 /// ScalarizeVecOp_STORE - If the value to store is a vector that needs to be
447 /// scalarized, it must be <1 x ty>. Just store the element.
448 SDValue DAGTypeLegalizer::ScalarizeVecOp_STORE(StoreSDNode *N, unsigned OpNo){
449 assert(N->isUnindexed() && "Indexed store of one-element vector?");
450 assert(OpNo == 1 && "Do not know how to scalarize this operand!");
453 if (N->isTruncatingStore())
454 return DAG.getTruncStore(N->getChain(), dl,
455 GetScalarizedVector(N->getOperand(1)),
456 N->getBasePtr(), N->getPointerInfo(),
457 N->getMemoryVT().getVectorElementType(),
458 N->isVolatile(), N->isNonTemporal(),
461 return DAG.getStore(N->getChain(), dl, GetScalarizedVector(N->getOperand(1)),
462 N->getBasePtr(), N->getPointerInfo(),
463 N->isVolatile(), N->isNonTemporal(),
464 N->getOriginalAlignment());
468 //===----------------------------------------------------------------------===//
469 // Result Vector Splitting
470 //===----------------------------------------------------------------------===//
472 /// SplitVectorResult - This method is called when the specified result of the
473 /// specified node is found to need vector splitting. At this point, the node
474 /// may also have invalid operands or may have other results that need
475 /// legalization, we just know that (at least) one result needs vector
477 void DAGTypeLegalizer::SplitVectorResult(SDNode *N, unsigned ResNo) {
478 DEBUG(dbgs() << "Split node result: ";
483 // See if the target wants to custom expand this node.
484 if (CustomLowerNode(N, N->getValueType(ResNo), true))
487 switch (N->getOpcode()) {
490 dbgs() << "SplitVectorResult #" << ResNo << ": ";
494 report_fatal_error("Do not know how to split the result of this "
497 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
499 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
500 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
501 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
502 case ISD::BITCAST: SplitVecRes_BITCAST(N, Lo, Hi); break;
503 case ISD::BUILD_VECTOR: SplitVecRes_BUILD_VECTOR(N, Lo, Hi); break;
504 case ISD::CONCAT_VECTORS: SplitVecRes_CONCAT_VECTORS(N, Lo, Hi); break;
505 case ISD::EXTRACT_SUBVECTOR: SplitVecRes_EXTRACT_SUBVECTOR(N, Lo, Hi); break;
506 case ISD::FP_ROUND_INREG: SplitVecRes_InregOp(N, Lo, Hi); break;
507 case ISD::FPOWI: SplitVecRes_FPOWI(N, Lo, Hi); break;
508 case ISD::INSERT_VECTOR_ELT: SplitVecRes_INSERT_VECTOR_ELT(N, Lo, Hi); break;
509 case ISD::SCALAR_TO_VECTOR: SplitVecRes_SCALAR_TO_VECTOR(N, Lo, Hi); break;
510 case ISD::SIGN_EXTEND_INREG: SplitVecRes_InregOp(N, Lo, Hi); break;
512 SplitVecRes_LOAD(cast<LoadSDNode>(N), Lo, Hi);
515 SplitVecRes_SETCC(N, Lo, Hi);
517 case ISD::VECTOR_SHUFFLE:
518 SplitVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N), Lo, Hi);
521 case ISD::ANY_EXTEND:
522 case ISD::CONVERT_RNDSAT:
525 case ISD::CTLZ_ZERO_UNDEF:
526 case ISD::CTTZ_ZERO_UNDEF:
537 case ISD::FNEARBYINT:
541 case ISD::FP_TO_SINT:
542 case ISD::FP_TO_UINT:
548 case ISD::SIGN_EXTEND:
549 case ISD::SINT_TO_FP:
551 case ISD::UINT_TO_FP:
552 case ISD::ZERO_EXTEND:
553 SplitVecRes_UnaryOp(N, Lo, Hi);
575 SplitVecRes_BinOp(N, Lo, Hi);
578 SplitVecRes_TernaryOp(N, Lo, Hi);
582 // If Lo/Hi is null, the sub-method took care of registering results etc.
584 SetSplitVector(SDValue(N, ResNo), Lo, Hi);
587 void DAGTypeLegalizer::SplitVecRes_BinOp(SDNode *N, SDValue &Lo,
589 SDValue LHSLo, LHSHi;
590 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
591 SDValue RHSLo, RHSHi;
592 GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
595 Lo = DAG.getNode(N->getOpcode(), dl, LHSLo.getValueType(), LHSLo, RHSLo);
596 Hi = DAG.getNode(N->getOpcode(), dl, LHSHi.getValueType(), LHSHi, RHSHi);
599 void DAGTypeLegalizer::SplitVecRes_TernaryOp(SDNode *N, SDValue &Lo,
601 SDValue Op0Lo, Op0Hi;
602 GetSplitVector(N->getOperand(0), Op0Lo, Op0Hi);
603 SDValue Op1Lo, Op1Hi;
604 GetSplitVector(N->getOperand(1), Op1Lo, Op1Hi);
605 SDValue Op2Lo, Op2Hi;
606 GetSplitVector(N->getOperand(2), Op2Lo, Op2Hi);
609 Lo = DAG.getNode(N->getOpcode(), dl, Op0Lo.getValueType(),
610 Op0Lo, Op1Lo, Op2Lo);
611 Hi = DAG.getNode(N->getOpcode(), dl, Op0Hi.getValueType(),
612 Op0Hi, Op1Hi, Op2Hi);
615 void DAGTypeLegalizer::SplitVecRes_BITCAST(SDNode *N, SDValue &Lo,
617 // We know the result is a vector. The input may be either a vector or a
620 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
623 SDValue InOp = N->getOperand(0);
624 EVT InVT = InOp.getValueType();
626 // Handle some special cases efficiently.
627 switch (getTypeAction(InVT)) {
628 case TargetLowering::TypeLegal:
629 case TargetLowering::TypePromoteInteger:
630 case TargetLowering::TypeSoftenFloat:
631 case TargetLowering::TypeScalarizeVector:
632 case TargetLowering::TypeWidenVector:
634 case TargetLowering::TypeExpandInteger:
635 case TargetLowering::TypeExpandFloat:
636 // A scalar to vector conversion, where the scalar needs expansion.
637 // If the vector is being split in two then we can just convert the
640 GetExpandedOp(InOp, Lo, Hi);
641 if (TLI.isBigEndian())
643 Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
644 Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
648 case TargetLowering::TypeSplitVector:
649 // If the input is a vector that needs to be split, convert each split
650 // piece of the input now.
651 GetSplitVector(InOp, Lo, Hi);
652 Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
653 Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
657 // In the general case, convert the input to an integer and split it by hand.
658 EVT LoIntVT = EVT::getIntegerVT(*DAG.getContext(), LoVT.getSizeInBits());
659 EVT HiIntVT = EVT::getIntegerVT(*DAG.getContext(), HiVT.getSizeInBits());
660 if (TLI.isBigEndian())
661 std::swap(LoIntVT, HiIntVT);
663 SplitInteger(BitConvertToInteger(InOp), LoIntVT, HiIntVT, Lo, Hi);
665 if (TLI.isBigEndian())
667 Lo = DAG.getNode(ISD::BITCAST, dl, LoVT, Lo);
668 Hi = DAG.getNode(ISD::BITCAST, dl, HiVT, Hi);
671 void DAGTypeLegalizer::SplitVecRes_BUILD_VECTOR(SDNode *N, SDValue &Lo,
675 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
676 unsigned LoNumElts = LoVT.getVectorNumElements();
677 SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+LoNumElts);
678 Lo = DAG.getNode(ISD::BUILD_VECTOR, dl, LoVT, &LoOps[0], LoOps.size());
680 SmallVector<SDValue, 8> HiOps(N->op_begin()+LoNumElts, N->op_end());
681 Hi = DAG.getNode(ISD::BUILD_VECTOR, dl, HiVT, &HiOps[0], HiOps.size());
684 void DAGTypeLegalizer::SplitVecRes_CONCAT_VECTORS(SDNode *N, SDValue &Lo,
686 assert(!(N->getNumOperands() & 1) && "Unsupported CONCAT_VECTORS");
688 unsigned NumSubvectors = N->getNumOperands() / 2;
689 if (NumSubvectors == 1) {
690 Lo = N->getOperand(0);
691 Hi = N->getOperand(1);
696 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
698 SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+NumSubvectors);
699 Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, LoVT, &LoOps[0], LoOps.size());
701 SmallVector<SDValue, 8> HiOps(N->op_begin()+NumSubvectors, N->op_end());
702 Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, HiVT, &HiOps[0], HiOps.size());
705 void DAGTypeLegalizer::SplitVecRes_EXTRACT_SUBVECTOR(SDNode *N, SDValue &Lo,
707 SDValue Vec = N->getOperand(0);
708 SDValue Idx = N->getOperand(1);
712 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
714 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, LoVT, Vec, Idx);
715 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
716 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, HiVT, Vec,
717 DAG.getConstant(IdxVal + LoVT.getVectorNumElements(),
718 TLI.getVectorIdxTy()));
721 void DAGTypeLegalizer::SplitVecRes_FPOWI(SDNode *N, SDValue &Lo,
724 GetSplitVector(N->getOperand(0), Lo, Hi);
725 Lo = DAG.getNode(ISD::FPOWI, dl, Lo.getValueType(), Lo, N->getOperand(1));
726 Hi = DAG.getNode(ISD::FPOWI, dl, Hi.getValueType(), Hi, N->getOperand(1));
729 void DAGTypeLegalizer::SplitVecRes_InregOp(SDNode *N, SDValue &Lo,
731 SDValue LHSLo, LHSHi;
732 GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
736 GetSplitDestVTs(cast<VTSDNode>(N->getOperand(1))->getVT(), LoVT, HiVT);
738 Lo = DAG.getNode(N->getOpcode(), dl, LHSLo.getValueType(), LHSLo,
739 DAG.getValueType(LoVT));
740 Hi = DAG.getNode(N->getOpcode(), dl, LHSHi.getValueType(), LHSHi,
741 DAG.getValueType(HiVT));
744 void DAGTypeLegalizer::SplitVecRes_INSERT_VECTOR_ELT(SDNode *N, SDValue &Lo,
746 SDValue Vec = N->getOperand(0);
747 SDValue Elt = N->getOperand(1);
748 SDValue Idx = N->getOperand(2);
750 GetSplitVector(Vec, Lo, Hi);
752 if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
753 unsigned IdxVal = CIdx->getZExtValue();
754 unsigned LoNumElts = Lo.getValueType().getVectorNumElements();
755 if (IdxVal < LoNumElts)
756 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
757 Lo.getValueType(), Lo, Elt, Idx);
759 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, Hi.getValueType(), Hi, Elt,
760 DAG.getConstant(IdxVal - LoNumElts,
761 TLI.getVectorIdxTy()));
765 // Spill the vector to the stack.
766 EVT VecVT = Vec.getValueType();
767 EVT EltVT = VecVT.getVectorElementType();
768 SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
769 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
770 MachinePointerInfo(), false, false, 0);
772 // Store the new element. This may be larger than the vector element type,
773 // so use a truncating store.
774 SDValue EltPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
775 Type *VecType = VecVT.getTypeForEVT(*DAG.getContext());
777 TLI.getDataLayout()->getPrefTypeAlignment(VecType);
778 Store = DAG.getTruncStore(Store, dl, Elt, EltPtr, MachinePointerInfo(), EltVT,
781 // Load the Lo part from the stack slot.
782 Lo = DAG.getLoad(Lo.getValueType(), dl, Store, StackPtr, MachinePointerInfo(),
783 false, false, false, 0);
785 // Increment the pointer to the other part.
786 unsigned IncrementSize = Lo.getValueType().getSizeInBits() / 8;
787 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
788 DAG.getIntPtrConstant(IncrementSize));
790 // Load the Hi part from the stack slot.
791 Hi = DAG.getLoad(Hi.getValueType(), dl, Store, StackPtr, MachinePointerInfo(),
792 false, false, false, MinAlign(Alignment, IncrementSize));
795 void DAGTypeLegalizer::SplitVecRes_SCALAR_TO_VECTOR(SDNode *N, SDValue &Lo,
799 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
800 Lo = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoVT, N->getOperand(0));
801 Hi = DAG.getUNDEF(HiVT);
804 void DAGTypeLegalizer::SplitVecRes_LOAD(LoadSDNode *LD, SDValue &Lo,
806 assert(ISD::isUNINDEXEDLoad(LD) && "Indexed load during type legalization!");
809 GetSplitDestVTs(LD->getValueType(0), LoVT, HiVT);
811 ISD::LoadExtType ExtType = LD->getExtensionType();
812 SDValue Ch = LD->getChain();
813 SDValue Ptr = LD->getBasePtr();
814 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
815 EVT MemoryVT = LD->getMemoryVT();
816 unsigned Alignment = LD->getOriginalAlignment();
817 bool isVolatile = LD->isVolatile();
818 bool isNonTemporal = LD->isNonTemporal();
819 bool isInvariant = LD->isInvariant();
821 EVT LoMemVT, HiMemVT;
822 GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
824 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType, LoVT, dl, Ch, Ptr, Offset,
825 LD->getPointerInfo(), LoMemVT, isVolatile, isNonTemporal,
826 isInvariant, Alignment);
828 unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
829 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
830 DAG.getIntPtrConstant(IncrementSize));
831 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType, HiVT, dl, Ch, Ptr, Offset,
832 LD->getPointerInfo().getWithOffset(IncrementSize),
833 HiMemVT, isVolatile, isNonTemporal, isInvariant, Alignment);
835 // Build a factor node to remember that this load is independent of the
837 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
840 // Legalized the chain result - switch anything that used the old chain to
842 ReplaceValueWith(SDValue(LD, 1), Ch);
845 void DAGTypeLegalizer::SplitVecRes_SETCC(SDNode *N, SDValue &Lo, SDValue &Hi) {
846 assert(N->getValueType(0).isVector() &&
847 N->getOperand(0).getValueType().isVector() &&
848 "Operand types must be vectors");
852 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
855 EVT InVT = N->getOperand(0).getValueType();
856 SDValue LL, LH, RL, RH;
857 EVT InNVT = EVT::getVectorVT(*DAG.getContext(), InVT.getVectorElementType(),
858 LoVT.getVectorNumElements());
859 LL = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(0),
860 DAG.getConstant(0, TLI.getVectorIdxTy()));
861 LH = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(0),
862 DAG.getConstant(InNVT.getVectorNumElements(),
863 TLI.getVectorIdxTy()));
865 RL = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(1),
866 DAG.getConstant(0, TLI.getVectorIdxTy()));
867 RH = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InNVT, N->getOperand(1),
868 DAG.getConstant(InNVT.getVectorNumElements(),
869 TLI.getVectorIdxTy()));
871 Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2));
872 Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2));
875 void DAGTypeLegalizer::SplitVecRes_UnaryOp(SDNode *N, SDValue &Lo,
877 // Get the dest types - they may not match the input types, e.g. int_to_fp.
880 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
882 // If the input also splits, handle it directly for a compile time speedup.
883 // Otherwise split it by hand.
884 EVT InVT = N->getOperand(0).getValueType();
885 if (getTypeAction(InVT) == TargetLowering::TypeSplitVector) {
886 GetSplitVector(N->getOperand(0), Lo, Hi);
888 EVT InNVT = EVT::getVectorVT(*DAG.getContext(), InVT.getVectorElementType(),
889 LoVT.getVectorNumElements());
890 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InNVT, N->getOperand(0),
891 DAG.getConstant(0, TLI.getVectorIdxTy()));
892 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InNVT, N->getOperand(0),
893 DAG.getConstant(InNVT.getVectorNumElements(),
894 TLI.getVectorIdxTy()));
897 if (N->getOpcode() == ISD::FP_ROUND) {
898 Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo, N->getOperand(1));
899 Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi, N->getOperand(1));
900 } else if (N->getOpcode() == ISD::CONVERT_RNDSAT) {
901 SDValue DTyOpLo = DAG.getValueType(LoVT);
902 SDValue DTyOpHi = DAG.getValueType(HiVT);
903 SDValue STyOpLo = DAG.getValueType(Lo.getValueType());
904 SDValue STyOpHi = DAG.getValueType(Hi.getValueType());
905 SDValue RndOp = N->getOperand(3);
906 SDValue SatOp = N->getOperand(4);
907 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
908 Lo = DAG.getConvertRndSat(LoVT, dl, Lo, DTyOpLo, STyOpLo, RndOp, SatOp,
910 Hi = DAG.getConvertRndSat(HiVT, dl, Hi, DTyOpHi, STyOpHi, RndOp, SatOp,
913 Lo = DAG.getNode(N->getOpcode(), dl, LoVT, Lo);
914 Hi = DAG.getNode(N->getOpcode(), dl, HiVT, Hi);
918 void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
919 SDValue &Lo, SDValue &Hi) {
920 // The low and high parts of the original input give four input vectors.
923 GetSplitVector(N->getOperand(0), Inputs[0], Inputs[1]);
924 GetSplitVector(N->getOperand(1), Inputs[2], Inputs[3]);
925 EVT NewVT = Inputs[0].getValueType();
926 unsigned NewElts = NewVT.getVectorNumElements();
928 // If Lo or Hi uses elements from at most two of the four input vectors, then
929 // express it as a vector shuffle of those two inputs. Otherwise extract the
930 // input elements by hand and construct the Lo/Hi output using a BUILD_VECTOR.
931 SmallVector<int, 16> Ops;
932 for (unsigned High = 0; High < 2; ++High) {
933 SDValue &Output = High ? Hi : Lo;
935 // Build a shuffle mask for the output, discovering on the fly which
936 // input vectors to use as shuffle operands (recorded in InputUsed).
937 // If building a suitable shuffle vector proves too hard, then bail
938 // out with useBuildVector set.
939 unsigned InputUsed[2] = { -1U, -1U }; // Not yet discovered.
940 unsigned FirstMaskIdx = High * NewElts;
941 bool useBuildVector = false;
942 for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
943 // The mask element. This indexes into the input.
944 int Idx = N->getMaskElt(FirstMaskIdx + MaskOffset);
946 // The input vector this mask element indexes into.
947 unsigned Input = (unsigned)Idx / NewElts;
949 if (Input >= array_lengthof(Inputs)) {
950 // The mask element does not index into any input vector.
955 // Turn the index into an offset from the start of the input vector.
956 Idx -= Input * NewElts;
958 // Find or create a shuffle vector operand to hold this input.
960 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
961 if (InputUsed[OpNo] == Input) {
962 // This input vector is already an operand.
964 } else if (InputUsed[OpNo] == -1U) {
965 // Create a new operand for this input vector.
966 InputUsed[OpNo] = Input;
971 if (OpNo >= array_lengthof(InputUsed)) {
972 // More than two input vectors used! Give up on trying to create a
973 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
974 useBuildVector = true;
978 // Add the mask index for the new shuffle vector.
979 Ops.push_back(Idx + OpNo * NewElts);
982 if (useBuildVector) {
983 EVT EltVT = NewVT.getVectorElementType();
984 SmallVector<SDValue, 16> SVOps;
986 // Extract the input elements by hand.
987 for (unsigned MaskOffset = 0; MaskOffset < NewElts; ++MaskOffset) {
988 // The mask element. This indexes into the input.
989 int Idx = N->getMaskElt(FirstMaskIdx + MaskOffset);
991 // The input vector this mask element indexes into.
992 unsigned Input = (unsigned)Idx / NewElts;
994 if (Input >= array_lengthof(Inputs)) {
995 // The mask element is "undef" or indexes off the end of the input.
996 SVOps.push_back(DAG.getUNDEF(EltVT));
1000 // Turn the index into an offset from the start of the input vector.
1001 Idx -= Input * NewElts;
1003 // Extract the vector element by hand.
1004 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
1005 Inputs[Input], DAG.getConstant(Idx,
1006 TLI.getVectorIdxTy())));
1009 // Construct the Lo/Hi output using a BUILD_VECTOR.
1010 Output = DAG.getNode(ISD::BUILD_VECTOR,dl,NewVT, &SVOps[0], SVOps.size());
1011 } else if (InputUsed[0] == -1U) {
1012 // No input vectors were used! The result is undefined.
1013 Output = DAG.getUNDEF(NewVT);
1015 SDValue Op0 = Inputs[InputUsed[0]];
1016 // If only one input was used, use an undefined vector for the other.
1017 SDValue Op1 = InputUsed[1] == -1U ?
1018 DAG.getUNDEF(NewVT) : Inputs[InputUsed[1]];
1019 // At least one input vector was used. Create a new shuffle vector.
1020 Output = DAG.getVectorShuffle(NewVT, dl, Op0, Op1, &Ops[0]);
1028 //===----------------------------------------------------------------------===//
1029 // Operand Vector Splitting
1030 //===----------------------------------------------------------------------===//
1032 /// SplitVectorOperand - This method is called when the specified operand of the
1033 /// specified node is found to need vector splitting. At this point, all of the
1034 /// result types of the node are known to be legal, but other operands of the
1035 /// node may need legalization as well as the specified one.
1036 bool DAGTypeLegalizer::SplitVectorOperand(SDNode *N, unsigned OpNo) {
1037 DEBUG(dbgs() << "Split node operand: ";
1040 SDValue Res = SDValue();
1042 // See if the target wants to custom split this node.
1043 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1046 if (Res.getNode() == 0) {
1047 switch (N->getOpcode()) {
1050 dbgs() << "SplitVectorOperand Op #" << OpNo << ": ";
1054 report_fatal_error("Do not know how to split this operator's "
1057 case ISD::SETCC: Res = SplitVecOp_VSETCC(N); break;
1058 case ISD::BITCAST: Res = SplitVecOp_BITCAST(N); break;
1059 case ISD::EXTRACT_SUBVECTOR: Res = SplitVecOp_EXTRACT_SUBVECTOR(N); break;
1060 case ISD::EXTRACT_VECTOR_ELT:Res = SplitVecOp_EXTRACT_VECTOR_ELT(N); break;
1061 case ISD::CONCAT_VECTORS: Res = SplitVecOp_CONCAT_VECTORS(N); break;
1062 case ISD::TRUNCATE: Res = SplitVecOp_TRUNCATE(N); break;
1063 case ISD::FP_ROUND: Res = SplitVecOp_FP_ROUND(N); break;
1065 Res = SplitVecOp_STORE(cast<StoreSDNode>(N), OpNo);
1068 Res = SplitVecOp_VSELECT(N, OpNo);
1073 case ISD::FP_EXTEND:
1074 case ISD::FP_TO_SINT:
1075 case ISD::FP_TO_UINT:
1076 case ISD::SINT_TO_FP:
1077 case ISD::UINT_TO_FP:
1079 case ISD::SIGN_EXTEND:
1080 case ISD::ZERO_EXTEND:
1081 case ISD::ANY_EXTEND:
1082 Res = SplitVecOp_UnaryOp(N);
1087 // If the result is null, the sub-method took care of registering results etc.
1088 if (!Res.getNode()) return false;
1090 // If the result is N, the sub-method updated N in place. Tell the legalizer
1092 if (Res.getNode() == N)
1095 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1096 "Invalid operand expansion");
1098 ReplaceValueWith(SDValue(N, 0), Res);
1102 SDValue DAGTypeLegalizer::SplitVecOp_VSELECT(SDNode *N, unsigned OpNo) {
1103 // The only possibility for an illegal operand is the mask, since result type
1104 // legalization would have handled this node already otherwise.
1105 assert(OpNo == 0 && "Illegal operand must be mask");
1107 SDValue Mask = N->getOperand(0);
1108 SDValue Src0 = N->getOperand(1);
1109 SDValue Src1 = N->getOperand(2);
1111 EVT MaskVT = Mask.getValueType();
1112 assert(MaskVT.isVector() && "VSELECT without a vector mask?");
1115 GetSplitVector(N->getOperand(0), Lo, Hi);
1116 assert(Lo.getValueType() == Hi.getValueType() &&
1117 "Lo and Hi have differing types");
1119 unsigned LoNumElts = Lo.getValueType().getVectorNumElements();
1120 unsigned HiNumElts = Hi.getValueType().getVectorNumElements();
1121 assert(LoNumElts == HiNumElts && "Asymmetric vector split?");
1123 LLVMContext &Ctx = *DAG.getContext();
1124 SDValue Zero = DAG.getConstant(0, TLI.getVectorIdxTy());
1125 SDValue LoElts = DAG.getConstant(LoNumElts, TLI.getVectorIdxTy());
1126 EVT Src0VT = Src0.getValueType();
1127 EVT Src0EltTy = Src0VT.getVectorElementType();
1128 EVT MaskEltTy = MaskVT.getVectorElementType();
1130 EVT LoOpVT = EVT::getVectorVT(Ctx, Src0EltTy, LoNumElts);
1131 EVT LoMaskVT = EVT::getVectorVT(Ctx, MaskEltTy, LoNumElts);
1132 EVT HiOpVT = EVT::getVectorVT(Ctx, Src0EltTy, HiNumElts);
1133 EVT HiMaskVT = EVT::getVectorVT(Ctx, MaskEltTy, HiNumElts);
1135 SDValue LoOp0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, LoOpVT, Src0, Zero);
1136 SDValue LoOp1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, LoOpVT, Src1, Zero);
1138 SDValue HiOp0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HiOpVT, Src0, LoElts);
1139 SDValue HiOp1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HiOpVT, Src1, LoElts);
1142 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, LoMaskVT, Mask, Zero);
1144 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HiMaskVT, Mask, LoElts);
1147 DAG.getNode(ISD::VSELECT, DL, LoOpVT, LoMask, LoOp0, LoOp1);
1149 DAG.getNode(ISD::VSELECT, DL, HiOpVT, HiMask, HiOp0, HiOp1);
1151 return DAG.getNode(ISD::CONCAT_VECTORS, DL, Src0VT, LoSelect, HiSelect);
1154 SDValue DAGTypeLegalizer::SplitVecOp_UnaryOp(SDNode *N) {
1155 // The result has a legal vector type, but the input needs splitting.
1156 EVT ResVT = N->getValueType(0);
1159 GetSplitVector(N->getOperand(0), Lo, Hi);
1160 EVT InVT = Lo.getValueType();
1162 EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
1163 InVT.getVectorNumElements());
1165 Lo = DAG.getNode(N->getOpcode(), dl, OutVT, Lo);
1166 Hi = DAG.getNode(N->getOpcode(), dl, OutVT, Hi);
1168 return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi);
1171 SDValue DAGTypeLegalizer::SplitVecOp_BITCAST(SDNode *N) {
1172 // For example, i64 = BITCAST v4i16 on alpha. Typically the vector will
1173 // end up being split all the way down to individual components. Convert the
1174 // split pieces into integers and reassemble.
1176 GetSplitVector(N->getOperand(0), Lo, Hi);
1177 Lo = BitConvertToInteger(Lo);
1178 Hi = BitConvertToInteger(Hi);
1180 if (TLI.isBigEndian())
1183 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
1184 JoinIntegers(Lo, Hi));
1187 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
1188 // We know that the extracted result type is legal.
1189 EVT SubVT = N->getValueType(0);
1190 SDValue Idx = N->getOperand(1);
1193 GetSplitVector(N->getOperand(0), Lo, Hi);
1195 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1196 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1198 if (IdxVal < LoElts) {
1199 assert(IdxVal + SubVT.getVectorNumElements() <= LoElts &&
1200 "Extracted subvector crosses vector split!");
1201 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, Lo, Idx);
1203 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, Hi,
1204 DAG.getConstant(IdxVal - LoElts, Idx.getValueType()));
1208 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
1209 SDValue Vec = N->getOperand(0);
1210 SDValue Idx = N->getOperand(1);
1211 EVT VecVT = Vec.getValueType();
1213 if (isa<ConstantSDNode>(Idx)) {
1214 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1215 assert(IdxVal < VecVT.getVectorNumElements() && "Invalid vector index!");
1218 GetSplitVector(Vec, Lo, Hi);
1220 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1222 if (IdxVal < LoElts)
1223 return SDValue(DAG.UpdateNodeOperands(N, Lo, Idx), 0);
1224 return SDValue(DAG.UpdateNodeOperands(N, Hi,
1225 DAG.getConstant(IdxVal - LoElts,
1226 Idx.getValueType())), 0);
1229 // Store the vector to the stack.
1230 EVT EltVT = VecVT.getVectorElementType();
1232 SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
1233 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1234 MachinePointerInfo(), false, false, 0);
1236 // Load back the required element.
1237 StackPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
1238 return DAG.getExtLoad(ISD::EXTLOAD, dl, N->getValueType(0), Store, StackPtr,
1239 MachinePointerInfo(), EltVT, false, false, 0);
1242 SDValue DAGTypeLegalizer::SplitVecOp_STORE(StoreSDNode *N, unsigned OpNo) {
1243 assert(N->isUnindexed() && "Indexed store of vector?");
1244 assert(OpNo == 1 && "Can only split the stored value");
1247 bool isTruncating = N->isTruncatingStore();
1248 SDValue Ch = N->getChain();
1249 SDValue Ptr = N->getBasePtr();
1250 EVT MemoryVT = N->getMemoryVT();
1251 unsigned Alignment = N->getOriginalAlignment();
1252 bool isVol = N->isVolatile();
1253 bool isNT = N->isNonTemporal();
1255 GetSplitVector(N->getOperand(1), Lo, Hi);
1257 EVT LoMemVT, HiMemVT;
1258 GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
1260 unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
1263 Lo = DAG.getTruncStore(Ch, DL, Lo, Ptr, N->getPointerInfo(),
1264 LoMemVT, isVol, isNT, Alignment);
1266 Lo = DAG.getStore(Ch, DL, Lo, Ptr, N->getPointerInfo(),
1267 isVol, isNT, Alignment);
1269 // Increment the pointer to the other half.
1270 Ptr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1271 DAG.getIntPtrConstant(IncrementSize));
1274 Hi = DAG.getTruncStore(Ch, DL, Hi, Ptr,
1275 N->getPointerInfo().getWithOffset(IncrementSize),
1276 HiMemVT, isVol, isNT, Alignment);
1278 Hi = DAG.getStore(Ch, DL, Hi, Ptr,
1279 N->getPointerInfo().getWithOffset(IncrementSize),
1280 isVol, isNT, Alignment);
1282 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
1285 SDValue DAGTypeLegalizer::SplitVecOp_CONCAT_VECTORS(SDNode *N) {
1288 // The input operands all must have the same type, and we know the result
1289 // type is valid. Convert this to a buildvector which extracts all the
1291 // TODO: If the input elements are power-two vectors, we could convert this to
1292 // a new CONCAT_VECTORS node with elements that are half-wide.
1293 SmallVector<SDValue, 32> Elts;
1294 EVT EltVT = N->getValueType(0).getVectorElementType();
1295 for (unsigned op = 0, e = N->getNumOperands(); op != e; ++op) {
1296 SDValue Op = N->getOperand(op);
1297 for (unsigned i = 0, e = Op.getValueType().getVectorNumElements();
1299 Elts.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT,
1300 Op, DAG.getConstant(i, TLI.getVectorIdxTy())));
1305 return DAG.getNode(ISD::BUILD_VECTOR, DL, N->getValueType(0),
1306 &Elts[0], Elts.size());
1309 SDValue DAGTypeLegalizer::SplitVecOp_TRUNCATE(SDNode *N) {
1310 // The result type is legal, but the input type is illegal. If splitting
1311 // ends up with the result type of each half still being legal, just
1312 // do that. If, however, that would result in an illegal result type,
1313 // we can try to get more clever with power-two vectors. Specifically,
1314 // split the input type, but also widen the result element size, then
1315 // concatenate the halves and truncate again. For example, consider a target
1316 // where v8i8 is legal and v8i32 is not (ARM, which doesn't have 256-bit
1317 // vectors). To perform a "%res = v8i8 trunc v8i32 %in" we do:
1318 // %inlo = v4i32 extract_subvector %in, 0
1319 // %inhi = v4i32 extract_subvector %in, 4
1320 // %lo16 = v4i16 trunc v4i32 %inlo
1321 // %hi16 = v4i16 trunc v4i32 %inhi
1322 // %in16 = v8i16 concat_vectors v4i16 %lo16, v4i16 %hi16
1323 // %res = v8i8 trunc v8i16 %in16
1325 // Without this transform, the original truncate would end up being
1326 // scalarized, which is pretty much always a last resort.
1327 SDValue InVec = N->getOperand(0);
1328 EVT InVT = InVec->getValueType(0);
1329 EVT OutVT = N->getValueType(0);
1330 unsigned NumElements = OutVT.getVectorNumElements();
1331 // Widening should have already made sure this is a power-two vector
1332 // if we're trying to split it at all. assert() that's true, just in case.
1333 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
1335 unsigned InElementSize = InVT.getVectorElementType().getSizeInBits();
1336 unsigned OutElementSize = OutVT.getVectorElementType().getSizeInBits();
1338 // If the input elements are only 1/2 the width of the result elements,
1339 // just use the normal splitting. Our trick only work if there's room
1340 // to split more than once.
1341 if (InElementSize <= OutElementSize * 2)
1342 return SplitVecOp_UnaryOp(N);
1345 // Extract the halves of the input via extract_subvector.
1346 EVT SplitVT = EVT::getVectorVT(*DAG.getContext(),
1347 InVT.getVectorElementType(), NumElements/2);
1348 SDValue InLoVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SplitVT, InVec,
1349 DAG.getConstant(0, TLI.getVectorIdxTy()));
1350 SDValue InHiVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SplitVT, InVec,
1351 DAG.getConstant(NumElements/2,
1352 TLI.getVectorIdxTy()));
1353 // Truncate them to 1/2 the element size.
1354 EVT HalfElementVT = EVT::getIntegerVT(*DAG.getContext(), InElementSize/2);
1355 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), HalfElementVT,
1357 SDValue HalfLo = DAG.getNode(ISD::TRUNCATE, DL, HalfVT, InLoVec);
1358 SDValue HalfHi = DAG.getNode(ISD::TRUNCATE, DL, HalfVT, InHiVec);
1359 // Concatenate them to get the full intermediate truncation result.
1360 EVT InterVT = EVT::getVectorVT(*DAG.getContext(), HalfElementVT, NumElements);
1361 SDValue InterVec = DAG.getNode(ISD::CONCAT_VECTORS, DL, InterVT, HalfLo,
1363 // Now finish up by truncating all the way down to the original result
1364 // type. This should normally be something that ends up being legal directly,
1365 // but in theory if a target has very wide vectors and an annoyingly
1366 // restricted set of legal types, this split can chain to build things up.
1367 return DAG.getNode(ISD::TRUNCATE, DL, OutVT, InterVec);
1370 SDValue DAGTypeLegalizer::SplitVecOp_VSETCC(SDNode *N) {
1371 assert(N->getValueType(0).isVector() &&
1372 N->getOperand(0).getValueType().isVector() &&
1373 "Operand types must be vectors");
1374 // The result has a legal vector type, but the input needs splitting.
1375 SDValue Lo0, Hi0, Lo1, Hi1, LoRes, HiRes;
1377 GetSplitVector(N->getOperand(0), Lo0, Hi0);
1378 GetSplitVector(N->getOperand(1), Lo1, Hi1);
1379 unsigned PartElements = Lo0.getValueType().getVectorNumElements();
1380 EVT PartResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, PartElements);
1381 EVT WideResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, 2*PartElements);
1383 LoRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Lo0, Lo1, N->getOperand(2));
1384 HiRes = DAG.getNode(ISD::SETCC, DL, PartResVT, Hi0, Hi1, N->getOperand(2));
1385 SDValue Con = DAG.getNode(ISD::CONCAT_VECTORS, DL, WideResVT, LoRes, HiRes);
1386 return PromoteTargetBoolean(Con, N->getValueType(0));
1390 SDValue DAGTypeLegalizer::SplitVecOp_FP_ROUND(SDNode *N) {
1391 // The result has a legal vector type, but the input needs splitting.
1392 EVT ResVT = N->getValueType(0);
1395 GetSplitVector(N->getOperand(0), Lo, Hi);
1396 EVT InVT = Lo.getValueType();
1398 EVT OutVT = EVT::getVectorVT(*DAG.getContext(), ResVT.getVectorElementType(),
1399 InVT.getVectorNumElements());
1401 Lo = DAG.getNode(ISD::FP_ROUND, DL, OutVT, Lo, N->getOperand(1));
1402 Hi = DAG.getNode(ISD::FP_ROUND, DL, OutVT, Hi, N->getOperand(1));
1404 return DAG.getNode(ISD::CONCAT_VECTORS, DL, ResVT, Lo, Hi);
1409 //===----------------------------------------------------------------------===//
1410 // Result Vector Widening
1411 //===----------------------------------------------------------------------===//
1413 void DAGTypeLegalizer::WidenVectorResult(SDNode *N, unsigned ResNo) {
1414 DEBUG(dbgs() << "Widen node result " << ResNo << ": ";
1418 // See if the target wants to custom widen this node.
1419 if (CustomWidenLowerNode(N, N->getValueType(ResNo)))
1422 SDValue Res = SDValue();
1423 switch (N->getOpcode()) {
1426 dbgs() << "WidenVectorResult #" << ResNo << ": ";
1430 llvm_unreachable("Do not know how to widen the result of this operator!");
1432 case ISD::MERGE_VALUES: Res = WidenVecRes_MERGE_VALUES(N, ResNo); break;
1433 case ISD::BITCAST: Res = WidenVecRes_BITCAST(N); break;
1434 case ISD::BUILD_VECTOR: Res = WidenVecRes_BUILD_VECTOR(N); break;
1435 case ISD::CONCAT_VECTORS: Res = WidenVecRes_CONCAT_VECTORS(N); break;
1436 case ISD::CONVERT_RNDSAT: Res = WidenVecRes_CONVERT_RNDSAT(N); break;
1437 case ISD::EXTRACT_SUBVECTOR: Res = WidenVecRes_EXTRACT_SUBVECTOR(N); break;
1438 case ISD::FP_ROUND_INREG: Res = WidenVecRes_InregOp(N); break;
1439 case ISD::INSERT_VECTOR_ELT: Res = WidenVecRes_INSERT_VECTOR_ELT(N); break;
1440 case ISD::LOAD: Res = WidenVecRes_LOAD(N); break;
1441 case ISD::SCALAR_TO_VECTOR: Res = WidenVecRes_SCALAR_TO_VECTOR(N); break;
1442 case ISD::SIGN_EXTEND_INREG: Res = WidenVecRes_InregOp(N); break;
1444 case ISD::SELECT: Res = WidenVecRes_SELECT(N); break;
1445 case ISD::SELECT_CC: Res = WidenVecRes_SELECT_CC(N); break;
1446 case ISD::SETCC: Res = WidenVecRes_SETCC(N); break;
1447 case ISD::UNDEF: Res = WidenVecRes_UNDEF(N); break;
1448 case ISD::VECTOR_SHUFFLE:
1449 Res = WidenVecRes_VECTOR_SHUFFLE(cast<ShuffleVectorSDNode>(N));
1461 Res = WidenVecRes_Binary(N);
1465 case ISD::FCOPYSIGN:
1475 Res = WidenVecRes_BinaryCanTrap(N);
1479 Res = WidenVecRes_POWI(N);
1485 Res = WidenVecRes_Shift(N);
1488 case ISD::ANY_EXTEND:
1489 case ISD::FP_EXTEND:
1491 case ISD::FP_TO_SINT:
1492 case ISD::FP_TO_UINT:
1493 case ISD::SIGN_EXTEND:
1494 case ISD::SINT_TO_FP:
1496 case ISD::UINT_TO_FP:
1497 case ISD::ZERO_EXTEND:
1498 Res = WidenVecRes_Convert(N);
1513 case ISD::FNEARBYINT:
1520 Res = WidenVecRes_Unary(N);
1523 Res = WidenVecRes_Ternary(N);
1527 // If Res is null, the sub-method took care of registering the result.
1529 SetWidenedVector(SDValue(N, ResNo), Res);
1532 SDValue DAGTypeLegalizer::WidenVecRes_Ternary(SDNode *N) {
1533 // Ternary op widening.
1535 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1536 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
1537 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
1538 SDValue InOp3 = GetWidenedVector(N->getOperand(2));
1539 return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2, InOp3);
1542 SDValue DAGTypeLegalizer::WidenVecRes_Binary(SDNode *N) {
1543 // Binary op widening.
1545 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1546 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
1547 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
1548 return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2);
1551 SDValue DAGTypeLegalizer::WidenVecRes_BinaryCanTrap(SDNode *N) {
1552 // Binary op widening for operations that can trap.
1553 unsigned Opcode = N->getOpcode();
1555 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1556 EVT WidenEltVT = WidenVT.getVectorElementType();
1558 unsigned NumElts = VT.getVectorNumElements();
1559 while (!TLI.isTypeLegal(VT) && NumElts != 1) {
1560 NumElts = NumElts / 2;
1561 VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
1564 if (NumElts != 1 && !TLI.canOpTrap(N->getOpcode(), VT)) {
1565 // Operation doesn't trap so just widen as normal.
1566 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
1567 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
1568 return DAG.getNode(N->getOpcode(), dl, WidenVT, InOp1, InOp2);
1571 // No legal vector version so unroll the vector operation and then widen.
1573 return DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements());
1575 // Since the operation can trap, apply operation on the original vector.
1577 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
1578 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
1579 unsigned CurNumElts = N->getValueType(0).getVectorNumElements();
1581 SmallVector<SDValue, 16> ConcatOps(CurNumElts);
1582 unsigned ConcatEnd = 0; // Current ConcatOps index.
1583 int Idx = 0; // Current Idx into input vectors.
1585 // NumElts := greatest legal vector size (at most WidenVT)
1586 // while (orig. vector has unhandled elements) {
1587 // take munches of size NumElts from the beginning and add to ConcatOps
1588 // NumElts := next smaller supported vector size or 1
1590 while (CurNumElts != 0) {
1591 while (CurNumElts >= NumElts) {
1592 SDValue EOp1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, InOp1,
1593 DAG.getConstant(Idx, TLI.getVectorIdxTy()));
1594 SDValue EOp2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, InOp2,
1595 DAG.getConstant(Idx, TLI.getVectorIdxTy()));
1596 ConcatOps[ConcatEnd++] = DAG.getNode(Opcode, dl, VT, EOp1, EOp2);
1598 CurNumElts -= NumElts;
1601 NumElts = NumElts / 2;
1602 VT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NumElts);
1603 } while (!TLI.isTypeLegal(VT) && NumElts != 1);
1606 for (unsigned i = 0; i != CurNumElts; ++i, ++Idx) {
1607 SDValue EOp1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, WidenEltVT,
1608 InOp1, DAG.getConstant(Idx,
1609 TLI.getVectorIdxTy()));
1610 SDValue EOp2 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, WidenEltVT,
1611 InOp2, DAG.getConstant(Idx,
1612 TLI.getVectorIdxTy()));
1613 ConcatOps[ConcatEnd++] = DAG.getNode(Opcode, dl, WidenEltVT,
1620 // Check to see if we have a single operation with the widen type.
1621 if (ConcatEnd == 1) {
1622 VT = ConcatOps[0].getValueType();
1624 return ConcatOps[0];
1627 // while (Some element of ConcatOps is not of type MaxVT) {
1628 // From the end of ConcatOps, collect elements of the same type and put
1629 // them into an op of the next larger supported type
1631 while (ConcatOps[ConcatEnd-1].getValueType() != MaxVT) {
1632 Idx = ConcatEnd - 1;
1633 VT = ConcatOps[Idx--].getValueType();
1634 while (Idx >= 0 && ConcatOps[Idx].getValueType() == VT)
1637 int NextSize = VT.isVector() ? VT.getVectorNumElements() : 1;
1641 NextVT = EVT::getVectorVT(*DAG.getContext(), WidenEltVT, NextSize);
1642 } while (!TLI.isTypeLegal(NextVT));
1644 if (!VT.isVector()) {
1645 // Scalar type, create an INSERT_VECTOR_ELEMENT of type NextVT
1646 SDValue VecOp = DAG.getUNDEF(NextVT);
1647 unsigned NumToInsert = ConcatEnd - Idx - 1;
1648 for (unsigned i = 0, OpIdx = Idx+1; i < NumToInsert; i++, OpIdx++) {
1649 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NextVT, VecOp,
1650 ConcatOps[OpIdx], DAG.getConstant(i,
1651 TLI.getVectorIdxTy()));
1653 ConcatOps[Idx+1] = VecOp;
1654 ConcatEnd = Idx + 2;
1656 // Vector type, create a CONCAT_VECTORS of type NextVT
1657 SDValue undefVec = DAG.getUNDEF(VT);
1658 unsigned OpsToConcat = NextSize/VT.getVectorNumElements();
1659 SmallVector<SDValue, 16> SubConcatOps(OpsToConcat);
1660 unsigned RealVals = ConcatEnd - Idx - 1;
1661 unsigned SubConcatEnd = 0;
1662 unsigned SubConcatIdx = Idx + 1;
1663 while (SubConcatEnd < RealVals)
1664 SubConcatOps[SubConcatEnd++] = ConcatOps[++Idx];
1665 while (SubConcatEnd < OpsToConcat)
1666 SubConcatOps[SubConcatEnd++] = undefVec;
1667 ConcatOps[SubConcatIdx] = DAG.getNode(ISD::CONCAT_VECTORS, dl,
1668 NextVT, &SubConcatOps[0],
1670 ConcatEnd = SubConcatIdx + 1;
1674 // Check to see if we have a single operation with the widen type.
1675 if (ConcatEnd == 1) {
1676 VT = ConcatOps[0].getValueType();
1678 return ConcatOps[0];
1681 // add undefs of size MaxVT until ConcatOps grows to length of WidenVT
1682 unsigned NumOps = WidenVT.getVectorNumElements()/MaxVT.getVectorNumElements();
1683 if (NumOps != ConcatEnd ) {
1684 SDValue UndefVal = DAG.getUNDEF(MaxVT);
1685 for (unsigned j = ConcatEnd; j < NumOps; ++j)
1686 ConcatOps[j] = UndefVal;
1688 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &ConcatOps[0], NumOps);
1691 SDValue DAGTypeLegalizer::WidenVecRes_Convert(SDNode *N) {
1692 SDValue InOp = N->getOperand(0);
1695 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1696 unsigned WidenNumElts = WidenVT.getVectorNumElements();
1698 EVT InVT = InOp.getValueType();
1699 EVT InEltVT = InVT.getVectorElementType();
1700 EVT InWidenVT = EVT::getVectorVT(*DAG.getContext(), InEltVT, WidenNumElts);
1702 unsigned Opcode = N->getOpcode();
1703 unsigned InVTNumElts = InVT.getVectorNumElements();
1705 if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
1706 InOp = GetWidenedVector(N->getOperand(0));
1707 InVT = InOp.getValueType();
1708 InVTNumElts = InVT.getVectorNumElements();
1709 if (InVTNumElts == WidenNumElts) {
1710 if (N->getNumOperands() == 1)
1711 return DAG.getNode(Opcode, DL, WidenVT, InOp);
1712 return DAG.getNode(Opcode, DL, WidenVT, InOp, N->getOperand(1));
1716 if (TLI.isTypeLegal(InWidenVT)) {
1717 // Because the result and the input are different vector types, widening
1718 // the result could create a legal type but widening the input might make
1719 // it an illegal type that might lead to repeatedly splitting the input
1720 // and then widening it. To avoid this, we widen the input only if
1721 // it results in a legal type.
1722 if (WidenNumElts % InVTNumElts == 0) {
1723 // Widen the input and call convert on the widened input vector.
1724 unsigned NumConcat = WidenNumElts/InVTNumElts;
1725 SmallVector<SDValue, 16> Ops(NumConcat);
1727 SDValue UndefVal = DAG.getUNDEF(InVT);
1728 for (unsigned i = 1; i != NumConcat; ++i)
1730 SDValue InVec = DAG.getNode(ISD::CONCAT_VECTORS, DL, InWidenVT,
1731 &Ops[0], NumConcat);
1732 if (N->getNumOperands() == 1)
1733 return DAG.getNode(Opcode, DL, WidenVT, InVec);
1734 return DAG.getNode(Opcode, DL, WidenVT, InVec, N->getOperand(1));
1737 if (InVTNumElts % WidenNumElts == 0) {
1738 SDValue InVal = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InWidenVT,
1739 InOp, DAG.getConstant(0,
1740 TLI.getVectorIdxTy()));
1741 // Extract the input and convert the shorten input vector.
1742 if (N->getNumOperands() == 1)
1743 return DAG.getNode(Opcode, DL, WidenVT, InVal);
1744 return DAG.getNode(Opcode, DL, WidenVT, InVal, N->getOperand(1));
1748 // Otherwise unroll into some nasty scalar code and rebuild the vector.
1749 SmallVector<SDValue, 16> Ops(WidenNumElts);
1750 EVT EltVT = WidenVT.getVectorElementType();
1751 unsigned MinElts = std::min(InVTNumElts, WidenNumElts);
1753 for (i=0; i < MinElts; ++i) {
1754 SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, InEltVT, InOp,
1755 DAG.getConstant(i, TLI.getVectorIdxTy()));
1756 if (N->getNumOperands() == 1)
1757 Ops[i] = DAG.getNode(Opcode, DL, EltVT, Val);
1759 Ops[i] = DAG.getNode(Opcode, DL, EltVT, Val, N->getOperand(1));
1762 SDValue UndefVal = DAG.getUNDEF(EltVT);
1763 for (; i < WidenNumElts; ++i)
1766 return DAG.getNode(ISD::BUILD_VECTOR, DL, WidenVT, &Ops[0], WidenNumElts);
1769 SDValue DAGTypeLegalizer::WidenVecRes_POWI(SDNode *N) {
1770 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1771 SDValue InOp = GetWidenedVector(N->getOperand(0));
1772 SDValue ShOp = N->getOperand(1);
1773 return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp, ShOp);
1776 SDValue DAGTypeLegalizer::WidenVecRes_Shift(SDNode *N) {
1777 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1778 SDValue InOp = GetWidenedVector(N->getOperand(0));
1779 SDValue ShOp = N->getOperand(1);
1781 EVT ShVT = ShOp.getValueType();
1782 if (getTypeAction(ShVT) == TargetLowering::TypeWidenVector) {
1783 ShOp = GetWidenedVector(ShOp);
1784 ShVT = ShOp.getValueType();
1786 EVT ShWidenVT = EVT::getVectorVT(*DAG.getContext(),
1787 ShVT.getVectorElementType(),
1788 WidenVT.getVectorNumElements());
1789 if (ShVT != ShWidenVT)
1790 ShOp = ModifyToType(ShOp, ShWidenVT);
1792 return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp, ShOp);
1795 SDValue DAGTypeLegalizer::WidenVecRes_Unary(SDNode *N) {
1796 // Unary op widening.
1797 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1798 SDValue InOp = GetWidenedVector(N->getOperand(0));
1799 return DAG.getNode(N->getOpcode(), SDLoc(N), WidenVT, InOp);
1802 SDValue DAGTypeLegalizer::WidenVecRes_InregOp(SDNode *N) {
1803 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1804 EVT ExtVT = EVT::getVectorVT(*DAG.getContext(),
1805 cast<VTSDNode>(N->getOperand(1))->getVT()
1806 .getVectorElementType(),
1807 WidenVT.getVectorNumElements());
1808 SDValue WidenLHS = GetWidenedVector(N->getOperand(0));
1809 return DAG.getNode(N->getOpcode(), SDLoc(N),
1810 WidenVT, WidenLHS, DAG.getValueType(ExtVT));
1813 SDValue DAGTypeLegalizer::WidenVecRes_MERGE_VALUES(SDNode *N, unsigned ResNo) {
1814 SDValue WidenVec = DisintegrateMERGE_VALUES(N, ResNo);
1815 return GetWidenedVector(WidenVec);
1818 SDValue DAGTypeLegalizer::WidenVecRes_BITCAST(SDNode *N) {
1819 SDValue InOp = N->getOperand(0);
1820 EVT InVT = InOp.getValueType();
1821 EVT VT = N->getValueType(0);
1822 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1825 switch (getTypeAction(InVT)) {
1826 case TargetLowering::TypeLegal:
1828 case TargetLowering::TypePromoteInteger:
1829 // If the incoming type is a vector that is being promoted, then
1830 // we know that the elements are arranged differently and that we
1831 // must perform the conversion using a stack slot.
1832 if (InVT.isVector())
1835 // If the InOp is promoted to the same size, convert it. Otherwise,
1836 // fall out of the switch and widen the promoted input.
1837 InOp = GetPromotedInteger(InOp);
1838 InVT = InOp.getValueType();
1839 if (WidenVT.bitsEq(InVT))
1840 return DAG.getNode(ISD::BITCAST, dl, WidenVT, InOp);
1842 case TargetLowering::TypeSoftenFloat:
1843 case TargetLowering::TypeExpandInteger:
1844 case TargetLowering::TypeExpandFloat:
1845 case TargetLowering::TypeScalarizeVector:
1846 case TargetLowering::TypeSplitVector:
1848 case TargetLowering::TypeWidenVector:
1849 // If the InOp is widened to the same size, convert it. Otherwise, fall
1850 // out of the switch and widen the widened input.
1851 InOp = GetWidenedVector(InOp);
1852 InVT = InOp.getValueType();
1853 if (WidenVT.bitsEq(InVT))
1854 // The input widens to the same size. Convert to the widen value.
1855 return DAG.getNode(ISD::BITCAST, dl, WidenVT, InOp);
1859 unsigned WidenSize = WidenVT.getSizeInBits();
1860 unsigned InSize = InVT.getSizeInBits();
1861 // x86mmx is not an acceptable vector element type, so don't try.
1862 if (WidenSize % InSize == 0 && InVT != MVT::x86mmx) {
1863 // Determine new input vector type. The new input vector type will use
1864 // the same element type (if its a vector) or use the input type as a
1865 // vector. It is the same size as the type to widen to.
1867 unsigned NewNumElts = WidenSize / InSize;
1868 if (InVT.isVector()) {
1869 EVT InEltVT = InVT.getVectorElementType();
1870 NewInVT = EVT::getVectorVT(*DAG.getContext(), InEltVT,
1871 WidenSize / InEltVT.getSizeInBits());
1873 NewInVT = EVT::getVectorVT(*DAG.getContext(), InVT, NewNumElts);
1876 if (TLI.isTypeLegal(NewInVT)) {
1877 // Because the result and the input are different vector types, widening
1878 // the result could create a legal type but widening the input might make
1879 // it an illegal type that might lead to repeatedly splitting the input
1880 // and then widening it. To avoid this, we widen the input only if
1881 // it results in a legal type.
1882 SmallVector<SDValue, 16> Ops(NewNumElts);
1883 SDValue UndefVal = DAG.getUNDEF(InVT);
1885 for (unsigned i = 1; i < NewNumElts; ++i)
1889 if (InVT.isVector())
1890 NewVec = DAG.getNode(ISD::CONCAT_VECTORS, dl,
1891 NewInVT, &Ops[0], NewNumElts);
1893 NewVec = DAG.getNode(ISD::BUILD_VECTOR, dl,
1894 NewInVT, &Ops[0], NewNumElts);
1895 return DAG.getNode(ISD::BITCAST, dl, WidenVT, NewVec);
1899 return CreateStackStoreLoad(InOp, WidenVT);
1902 SDValue DAGTypeLegalizer::WidenVecRes_BUILD_VECTOR(SDNode *N) {
1904 // Build a vector with undefined for the new nodes.
1905 EVT VT = N->getValueType(0);
1907 // Integer BUILD_VECTOR operands may be larger than the node's vector element
1908 // type. The UNDEFs need to have the same type as the existing operands.
1909 EVT EltVT = N->getOperand(0).getValueType();
1910 unsigned NumElts = VT.getVectorNumElements();
1912 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1913 unsigned WidenNumElts = WidenVT.getVectorNumElements();
1915 SmallVector<SDValue, 16> NewOps(N->op_begin(), N->op_end());
1916 assert(WidenNumElts >= NumElts && "Shrinking vector instead of widening!");
1917 NewOps.append(WidenNumElts - NumElts, DAG.getUNDEF(EltVT));
1919 return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &NewOps[0], NewOps.size());
1922 SDValue DAGTypeLegalizer::WidenVecRes_CONCAT_VECTORS(SDNode *N) {
1923 EVT InVT = N->getOperand(0).getValueType();
1924 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1926 unsigned WidenNumElts = WidenVT.getVectorNumElements();
1927 unsigned NumInElts = InVT.getVectorNumElements();
1928 unsigned NumOperands = N->getNumOperands();
1930 bool InputWidened = false; // Indicates we need to widen the input.
1931 if (getTypeAction(InVT) != TargetLowering::TypeWidenVector) {
1932 if (WidenVT.getVectorNumElements() % InVT.getVectorNumElements() == 0) {
1933 // Add undef vectors to widen to correct length.
1934 unsigned NumConcat = WidenVT.getVectorNumElements() /
1935 InVT.getVectorNumElements();
1936 SDValue UndefVal = DAG.getUNDEF(InVT);
1937 SmallVector<SDValue, 16> Ops(NumConcat);
1938 for (unsigned i=0; i < NumOperands; ++i)
1939 Ops[i] = N->getOperand(i);
1940 for (unsigned i = NumOperands; i != NumConcat; ++i)
1942 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &Ops[0], NumConcat);
1945 InputWidened = true;
1946 if (WidenVT == TLI.getTypeToTransformTo(*DAG.getContext(), InVT)) {
1947 // The inputs and the result are widen to the same value.
1949 for (i=1; i < NumOperands; ++i)
1950 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
1953 if (i == NumOperands)
1954 // Everything but the first operand is an UNDEF so just return the
1955 // widened first operand.
1956 return GetWidenedVector(N->getOperand(0));
1958 if (NumOperands == 2) {
1959 // Replace concat of two operands with a shuffle.
1960 SmallVector<int, 16> MaskOps(WidenNumElts, -1);
1961 for (unsigned i = 0; i < NumInElts; ++i) {
1963 MaskOps[i + NumInElts] = i + WidenNumElts;
1965 return DAG.getVectorShuffle(WidenVT, dl,
1966 GetWidenedVector(N->getOperand(0)),
1967 GetWidenedVector(N->getOperand(1)),
1973 // Fall back to use extracts and build vector.
1974 EVT EltVT = WidenVT.getVectorElementType();
1975 SmallVector<SDValue, 16> Ops(WidenNumElts);
1977 for (unsigned i=0; i < NumOperands; ++i) {
1978 SDValue InOp = N->getOperand(i);
1980 InOp = GetWidenedVector(InOp);
1981 for (unsigned j=0; j < NumInElts; ++j)
1982 Ops[Idx++] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
1983 DAG.getConstant(j, TLI.getVectorIdxTy()));
1985 SDValue UndefVal = DAG.getUNDEF(EltVT);
1986 for (; Idx < WidenNumElts; ++Idx)
1987 Ops[Idx] = UndefVal;
1988 return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], WidenNumElts);
1991 SDValue DAGTypeLegalizer::WidenVecRes_CONVERT_RNDSAT(SDNode *N) {
1993 SDValue InOp = N->getOperand(0);
1994 SDValue RndOp = N->getOperand(3);
1995 SDValue SatOp = N->getOperand(4);
1997 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1998 unsigned WidenNumElts = WidenVT.getVectorNumElements();
2000 EVT InVT = InOp.getValueType();
2001 EVT InEltVT = InVT.getVectorElementType();
2002 EVT InWidenVT = EVT::getVectorVT(*DAG.getContext(), InEltVT, WidenNumElts);
2004 SDValue DTyOp = DAG.getValueType(WidenVT);
2005 SDValue STyOp = DAG.getValueType(InWidenVT);
2006 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
2008 unsigned InVTNumElts = InVT.getVectorNumElements();
2009 if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
2010 InOp = GetWidenedVector(InOp);
2011 InVT = InOp.getValueType();
2012 InVTNumElts = InVT.getVectorNumElements();
2013 if (InVTNumElts == WidenNumElts)
2014 return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
2018 if (TLI.isTypeLegal(InWidenVT)) {
2019 // Because the result and the input are different vector types, widening
2020 // the result could create a legal type but widening the input might make
2021 // it an illegal type that might lead to repeatedly splitting the input
2022 // and then widening it. To avoid this, we widen the input only if
2023 // it results in a legal type.
2024 if (WidenNumElts % InVTNumElts == 0) {
2025 // Widen the input and call convert on the widened input vector.
2026 unsigned NumConcat = WidenNumElts/InVTNumElts;
2027 SmallVector<SDValue, 16> Ops(NumConcat);
2029 SDValue UndefVal = DAG.getUNDEF(InVT);
2030 for (unsigned i = 1; i != NumConcat; ++i)
2033 InOp = DAG.getNode(ISD::CONCAT_VECTORS, dl, InWidenVT, &Ops[0],NumConcat);
2034 return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
2038 if (InVTNumElts % WidenNumElts == 0) {
2039 // Extract the input and convert the shorten input vector.
2040 InOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InWidenVT, InOp,
2041 DAG.getConstant(0, TLI.getVectorIdxTy()));
2042 return DAG.getConvertRndSat(WidenVT, dl, InOp, DTyOp, STyOp, RndOp,
2047 // Otherwise unroll into some nasty scalar code and rebuild the vector.
2048 SmallVector<SDValue, 16> Ops(WidenNumElts);
2049 EVT EltVT = WidenVT.getVectorElementType();
2050 DTyOp = DAG.getValueType(EltVT);
2051 STyOp = DAG.getValueType(InEltVT);
2053 unsigned MinElts = std::min(InVTNumElts, WidenNumElts);
2055 for (i=0; i < MinElts; ++i) {
2056 SDValue ExtVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, InEltVT, InOp,
2057 DAG.getConstant(i, TLI.getVectorIdxTy()));
2058 Ops[i] = DAG.getConvertRndSat(WidenVT, dl, ExtVal, DTyOp, STyOp, RndOp,
2062 SDValue UndefVal = DAG.getUNDEF(EltVT);
2063 for (; i < WidenNumElts; ++i)
2066 return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], WidenNumElts);
2069 SDValue DAGTypeLegalizer::WidenVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
2070 EVT VT = N->getValueType(0);
2071 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2072 unsigned WidenNumElts = WidenVT.getVectorNumElements();
2073 SDValue InOp = N->getOperand(0);
2074 SDValue Idx = N->getOperand(1);
2077 if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
2078 InOp = GetWidenedVector(InOp);
2080 EVT InVT = InOp.getValueType();
2082 // Check if we can just return the input vector after widening.
2083 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
2084 if (IdxVal == 0 && InVT == WidenVT)
2087 // Check if we can extract from the vector.
2088 unsigned InNumElts = InVT.getVectorNumElements();
2089 if (IdxVal % WidenNumElts == 0 && IdxVal + WidenNumElts < InNumElts)
2090 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, InOp, Idx);
2092 // We could try widening the input to the right length but for now, extract
2093 // the original elements, fill the rest with undefs and build a vector.
2094 SmallVector<SDValue, 16> Ops(WidenNumElts);
2095 EVT EltVT = VT.getVectorElementType();
2096 unsigned NumElts = VT.getVectorNumElements();
2098 for (i=0; i < NumElts; ++i)
2099 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
2100 DAG.getConstant(IdxVal+i, TLI.getVectorIdxTy()));
2102 SDValue UndefVal = DAG.getUNDEF(EltVT);
2103 for (; i < WidenNumElts; ++i)
2105 return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], WidenNumElts);
2108 SDValue DAGTypeLegalizer::WidenVecRes_INSERT_VECTOR_ELT(SDNode *N) {
2109 SDValue InOp = GetWidenedVector(N->getOperand(0));
2110 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N),
2111 InOp.getValueType(), InOp,
2112 N->getOperand(1), N->getOperand(2));
2115 SDValue DAGTypeLegalizer::WidenVecRes_LOAD(SDNode *N) {
2116 LoadSDNode *LD = cast<LoadSDNode>(N);
2117 ISD::LoadExtType ExtType = LD->getExtensionType();
2120 SmallVector<SDValue, 16> LdChain; // Chain for the series of load
2121 if (ExtType != ISD::NON_EXTLOAD)
2122 Result = GenWidenVectorExtLoads(LdChain, LD, ExtType);
2124 Result = GenWidenVectorLoads(LdChain, LD);
2126 // If we generate a single load, we can use that for the chain. Otherwise,
2127 // build a factor node to remember the multiple loads are independent and
2130 if (LdChain.size() == 1)
2131 NewChain = LdChain[0];
2133 NewChain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other,
2134 &LdChain[0], LdChain.size());
2136 // Modified the chain - switch anything that used the old chain to use
2138 ReplaceValueWith(SDValue(N, 1), NewChain);
2143 SDValue DAGTypeLegalizer::WidenVecRes_SCALAR_TO_VECTOR(SDNode *N) {
2144 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2145 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N),
2146 WidenVT, N->getOperand(0));
2149 SDValue DAGTypeLegalizer::WidenVecRes_SELECT(SDNode *N) {
2150 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2151 unsigned WidenNumElts = WidenVT.getVectorNumElements();
2153 SDValue Cond1 = N->getOperand(0);
2154 EVT CondVT = Cond1.getValueType();
2155 if (CondVT.isVector()) {
2156 EVT CondEltVT = CondVT.getVectorElementType();
2157 EVT CondWidenVT = EVT::getVectorVT(*DAG.getContext(),
2158 CondEltVT, WidenNumElts);
2159 if (getTypeAction(CondVT) == TargetLowering::TypeWidenVector)
2160 Cond1 = GetWidenedVector(Cond1);
2162 if (Cond1.getValueType() != CondWidenVT)
2163 Cond1 = ModifyToType(Cond1, CondWidenVT);
2166 SDValue InOp1 = GetWidenedVector(N->getOperand(1));
2167 SDValue InOp2 = GetWidenedVector(N->getOperand(2));
2168 assert(InOp1.getValueType() == WidenVT && InOp2.getValueType() == WidenVT);
2169 return DAG.getNode(N->getOpcode(), SDLoc(N),
2170 WidenVT, Cond1, InOp1, InOp2);
2173 SDValue DAGTypeLegalizer::WidenVecRes_SELECT_CC(SDNode *N) {
2174 SDValue InOp1 = GetWidenedVector(N->getOperand(2));
2175 SDValue InOp2 = GetWidenedVector(N->getOperand(3));
2176 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
2177 InOp1.getValueType(), N->getOperand(0),
2178 N->getOperand(1), InOp1, InOp2, N->getOperand(4));
2181 SDValue DAGTypeLegalizer::WidenVecRes_SETCC(SDNode *N) {
2182 assert(N->getValueType(0).isVector() ==
2183 N->getOperand(0).getValueType().isVector() &&
2184 "Scalar/Vector type mismatch");
2185 if (N->getValueType(0).isVector()) return WidenVecRes_VSETCC(N);
2187 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2188 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2189 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2190 return DAG.getNode(ISD::SETCC, SDLoc(N), WidenVT,
2191 InOp1, InOp2, N->getOperand(2));
2194 SDValue DAGTypeLegalizer::WidenVecRes_UNDEF(SDNode *N) {
2195 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2196 return DAG.getUNDEF(WidenVT);
2199 SDValue DAGTypeLegalizer::WidenVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N) {
2200 EVT VT = N->getValueType(0);
2203 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2204 unsigned NumElts = VT.getVectorNumElements();
2205 unsigned WidenNumElts = WidenVT.getVectorNumElements();
2207 SDValue InOp1 = GetWidenedVector(N->getOperand(0));
2208 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2210 // Adjust mask based on new input vector length.
2211 SmallVector<int, 16> NewMask;
2212 for (unsigned i = 0; i != NumElts; ++i) {
2213 int Idx = N->getMaskElt(i);
2214 if (Idx < (int)NumElts)
2215 NewMask.push_back(Idx);
2217 NewMask.push_back(Idx - NumElts + WidenNumElts);
2219 for (unsigned i = NumElts; i != WidenNumElts; ++i)
2220 NewMask.push_back(-1);
2221 return DAG.getVectorShuffle(WidenVT, dl, InOp1, InOp2, &NewMask[0]);
2224 SDValue DAGTypeLegalizer::WidenVecRes_VSETCC(SDNode *N) {
2225 assert(N->getValueType(0).isVector() &&
2226 N->getOperand(0).getValueType().isVector() &&
2227 "Operands must be vectors");
2228 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2229 unsigned WidenNumElts = WidenVT.getVectorNumElements();
2231 SDValue InOp1 = N->getOperand(0);
2232 EVT InVT = InOp1.getValueType();
2233 assert(InVT.isVector() && "can not widen non vector type");
2234 EVT WidenInVT = EVT::getVectorVT(*DAG.getContext(),
2235 InVT.getVectorElementType(), WidenNumElts);
2236 InOp1 = GetWidenedVector(InOp1);
2237 SDValue InOp2 = GetWidenedVector(N->getOperand(1));
2239 // Assume that the input and output will be widen appropriately. If not,
2240 // we will have to unroll it at some point.
2241 assert(InOp1.getValueType() == WidenInVT &&
2242 InOp2.getValueType() == WidenInVT &&
2243 "Input not widened to expected type!");
2245 return DAG.getNode(ISD::SETCC, SDLoc(N),
2246 WidenVT, InOp1, InOp2, N->getOperand(2));
2250 //===----------------------------------------------------------------------===//
2251 // Widen Vector Operand
2252 //===----------------------------------------------------------------------===//
2253 bool DAGTypeLegalizer::WidenVectorOperand(SDNode *N, unsigned OpNo) {
2254 DEBUG(dbgs() << "Widen node operand " << OpNo << ": ";
2257 SDValue Res = SDValue();
2259 // See if the target wants to custom widen this node.
2260 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
2263 switch (N->getOpcode()) {
2266 dbgs() << "WidenVectorOperand op #" << OpNo << ": ";
2270 llvm_unreachable("Do not know how to widen this operator's operand!");
2272 case ISD::BITCAST: Res = WidenVecOp_BITCAST(N); break;
2273 case ISD::CONCAT_VECTORS: Res = WidenVecOp_CONCAT_VECTORS(N); break;
2274 case ISD::EXTRACT_SUBVECTOR: Res = WidenVecOp_EXTRACT_SUBVECTOR(N); break;
2275 case ISD::EXTRACT_VECTOR_ELT: Res = WidenVecOp_EXTRACT_VECTOR_ELT(N); break;
2276 case ISD::STORE: Res = WidenVecOp_STORE(N); break;
2277 case ISD::SETCC: Res = WidenVecOp_SETCC(N); break;
2279 case ISD::FP_EXTEND:
2280 case ISD::FP_TO_SINT:
2281 case ISD::FP_TO_UINT:
2282 case ISD::SINT_TO_FP:
2283 case ISD::UINT_TO_FP:
2285 case ISD::SIGN_EXTEND:
2286 case ISD::ZERO_EXTEND:
2287 case ISD::ANY_EXTEND:
2288 Res = WidenVecOp_Convert(N);
2292 // If Res is null, the sub-method took care of registering the result.
2293 if (!Res.getNode()) return false;
2295 // If the result is N, the sub-method updated N in place. Tell the legalizer
2297 if (Res.getNode() == N)
2301 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
2302 "Invalid operand expansion");
2304 ReplaceValueWith(SDValue(N, 0), Res);
2308 SDValue DAGTypeLegalizer::WidenVecOp_Convert(SDNode *N) {
2309 // Since the result is legal and the input is illegal, it is unlikely
2310 // that we can fix the input to a legal type so unroll the convert
2311 // into some scalar code and create a nasty build vector.
2312 EVT VT = N->getValueType(0);
2313 EVT EltVT = VT.getVectorElementType();
2315 unsigned NumElts = VT.getVectorNumElements();
2316 SDValue InOp = N->getOperand(0);
2317 if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
2318 InOp = GetWidenedVector(InOp);
2319 EVT InVT = InOp.getValueType();
2320 EVT InEltVT = InVT.getVectorElementType();
2322 unsigned Opcode = N->getOpcode();
2323 SmallVector<SDValue, 16> Ops(NumElts);
2324 for (unsigned i=0; i < NumElts; ++i)
2325 Ops[i] = DAG.getNode(Opcode, dl, EltVT,
2326 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, InEltVT, InOp,
2327 DAG.getConstant(i, TLI.getVectorIdxTy())));
2329 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElts);
2332 SDValue DAGTypeLegalizer::WidenVecOp_BITCAST(SDNode *N) {
2333 EVT VT = N->getValueType(0);
2334 SDValue InOp = GetWidenedVector(N->getOperand(0));
2335 EVT InWidenVT = InOp.getValueType();
2338 // Check if we can convert between two legal vector types and extract.
2339 unsigned InWidenSize = InWidenVT.getSizeInBits();
2340 unsigned Size = VT.getSizeInBits();
2341 // x86mmx is not an acceptable vector element type, so don't try.
2342 if (InWidenSize % Size == 0 && !VT.isVector() && VT != MVT::x86mmx) {
2343 unsigned NewNumElts = InWidenSize / Size;
2344 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), VT, NewNumElts);
2345 if (TLI.isTypeLegal(NewVT)) {
2346 SDValue BitOp = DAG.getNode(ISD::BITCAST, dl, NewVT, InOp);
2347 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, BitOp,
2348 DAG.getConstant(0, TLI.getVectorIdxTy()));
2352 return CreateStackStoreLoad(InOp, VT);
2355 SDValue DAGTypeLegalizer::WidenVecOp_CONCAT_VECTORS(SDNode *N) {
2356 // If the input vector is not legal, it is likely that we will not find a
2357 // legal vector of the same size. Replace the concatenate vector with a
2358 // nasty build vector.
2359 EVT VT = N->getValueType(0);
2360 EVT EltVT = VT.getVectorElementType();
2362 unsigned NumElts = VT.getVectorNumElements();
2363 SmallVector<SDValue, 16> Ops(NumElts);
2365 EVT InVT = N->getOperand(0).getValueType();
2366 unsigned NumInElts = InVT.getVectorNumElements();
2369 unsigned NumOperands = N->getNumOperands();
2370 for (unsigned i=0; i < NumOperands; ++i) {
2371 SDValue InOp = N->getOperand(i);
2372 if (getTypeAction(InOp.getValueType()) == TargetLowering::TypeWidenVector)
2373 InOp = GetWidenedVector(InOp);
2374 for (unsigned j=0; j < NumInElts; ++j)
2375 Ops[Idx++] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
2376 DAG.getConstant(j, TLI.getVectorIdxTy()));
2378 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElts);
2381 SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
2382 SDValue InOp = GetWidenedVector(N->getOperand(0));
2383 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N),
2384 N->getValueType(0), InOp, N->getOperand(1));
2387 SDValue DAGTypeLegalizer::WidenVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
2388 SDValue InOp = GetWidenedVector(N->getOperand(0));
2389 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
2390 N->getValueType(0), InOp, N->getOperand(1));
2393 SDValue DAGTypeLegalizer::WidenVecOp_STORE(SDNode *N) {
2394 // We have to widen the value but we want only to store the original
2396 StoreSDNode *ST = cast<StoreSDNode>(N);
2398 SmallVector<SDValue, 16> StChain;
2399 if (ST->isTruncatingStore())
2400 GenWidenVectorTruncStores(StChain, ST);
2402 GenWidenVectorStores(StChain, ST);
2404 if (StChain.size() == 1)
2407 return DAG.getNode(ISD::TokenFactor, SDLoc(ST),
2408 MVT::Other,&StChain[0],StChain.size());
2411 SDValue DAGTypeLegalizer::WidenVecOp_SETCC(SDNode *N) {
2412 SDValue InOp0 = GetWidenedVector(N->getOperand(0));
2413 SDValue InOp1 = GetWidenedVector(N->getOperand(1));
2416 // WARNING: In this code we widen the compare instruction with garbage.
2417 // This garbage may contain denormal floats which may be slow. Is this a real
2418 // concern ? Should we zero the unused lanes if this is a float compare ?
2420 // Get a new SETCC node to compare the newly widened operands.
2421 // Only some of the compared elements are legal.
2422 EVT SVT = TLI.getSetCCResultType(*DAG.getContext(), InOp0.getValueType());
2423 SDValue WideSETCC = DAG.getNode(ISD::SETCC, SDLoc(N),
2424 SVT, InOp0, InOp1, N->getOperand(2));
2426 // Extract the needed results from the result vector.
2427 EVT ResVT = EVT::getVectorVT(*DAG.getContext(),
2428 SVT.getVectorElementType(),
2429 N->getValueType(0).getVectorNumElements());
2430 SDValue CC = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
2431 ResVT, WideSETCC, DAG.getConstant(0,
2432 TLI.getVectorIdxTy()));
2434 return PromoteTargetBoolean(CC, N->getValueType(0));
2438 //===----------------------------------------------------------------------===//
2439 // Vector Widening Utilities
2440 //===----------------------------------------------------------------------===//
2442 // Utility function to find the type to chop up a widen vector for load/store
2443 // TLI: Target lowering used to determine legal types.
2444 // Width: Width left need to load/store.
2445 // WidenVT: The widen vector type to load to/store from
2446 // Align: If 0, don't allow use of a wider type
2447 // WidenEx: If Align is not 0, the amount additional we can load/store from.
2449 static EVT FindMemType(SelectionDAG& DAG, const TargetLowering &TLI,
2450 unsigned Width, EVT WidenVT,
2451 unsigned Align = 0, unsigned WidenEx = 0) {
2452 EVT WidenEltVT = WidenVT.getVectorElementType();
2453 unsigned WidenWidth = WidenVT.getSizeInBits();
2454 unsigned WidenEltWidth = WidenEltVT.getSizeInBits();
2455 unsigned AlignInBits = Align*8;
2457 // If we have one element to load/store, return it.
2458 EVT RetVT = WidenEltVT;
2459 if (Width == WidenEltWidth)
2462 // See if there is larger legal integer than the element type to load/store
2464 for (VT = (unsigned)MVT::LAST_INTEGER_VALUETYPE;
2465 VT >= (unsigned)MVT::FIRST_INTEGER_VALUETYPE; --VT) {
2466 EVT MemVT((MVT::SimpleValueType) VT);
2467 unsigned MemVTWidth = MemVT.getSizeInBits();
2468 if (MemVT.getSizeInBits() <= WidenEltWidth)
2470 if (TLI.isTypeLegal(MemVT) && (WidenWidth % MemVTWidth) == 0 &&
2471 isPowerOf2_32(WidenWidth / MemVTWidth) &&
2472 (MemVTWidth <= Width ||
2473 (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
2479 // See if there is a larger vector type to load/store that has the same vector
2480 // element type and is evenly divisible with the WidenVT.
2481 for (VT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
2482 VT >= (unsigned)MVT::FIRST_VECTOR_VALUETYPE; --VT) {
2483 EVT MemVT = (MVT::SimpleValueType) VT;
2484 unsigned MemVTWidth = MemVT.getSizeInBits();
2485 if (TLI.isTypeLegal(MemVT) && WidenEltVT == MemVT.getVectorElementType() &&
2486 (WidenWidth % MemVTWidth) == 0 &&
2487 isPowerOf2_32(WidenWidth / MemVTWidth) &&
2488 (MemVTWidth <= Width ||
2489 (Align!=0 && MemVTWidth<=AlignInBits && MemVTWidth<=Width+WidenEx))) {
2490 if (RetVT.getSizeInBits() < MemVTWidth || MemVT == WidenVT)
2498 // Builds a vector type from scalar loads
2499 // VecTy: Resulting Vector type
2500 // LDOps: Load operators to build a vector type
2501 // [Start,End) the list of loads to use.
2502 static SDValue BuildVectorFromScalar(SelectionDAG& DAG, EVT VecTy,
2503 SmallVectorImpl<SDValue> &LdOps,
2504 unsigned Start, unsigned End) {
2505 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2506 SDLoc dl(LdOps[Start]);
2507 EVT LdTy = LdOps[Start].getValueType();
2508 unsigned Width = VecTy.getSizeInBits();
2509 unsigned NumElts = Width / LdTy.getSizeInBits();
2510 EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), LdTy, NumElts);
2513 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT,LdOps[Start]);
2515 for (unsigned i = Start + 1; i != End; ++i) {
2516 EVT NewLdTy = LdOps[i].getValueType();
2517 if (NewLdTy != LdTy) {
2518 NumElts = Width / NewLdTy.getSizeInBits();
2519 NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewLdTy, NumElts);
2520 VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, VecOp);
2521 // Readjust position and vector position based on new load type
2522 Idx = Idx * LdTy.getSizeInBits() / NewLdTy.getSizeInBits();
2525 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVecVT, VecOp, LdOps[i],
2526 DAG.getConstant(Idx++, TLI.getVectorIdxTy()));
2528 return DAG.getNode(ISD::BITCAST, dl, VecTy, VecOp);
2531 SDValue DAGTypeLegalizer::GenWidenVectorLoads(SmallVectorImpl<SDValue> &LdChain,
2533 // The strategy assumes that we can efficiently load powers of two widths.
2534 // The routines chops the vector into the largest vector loads with the same
2535 // element type or scalar loads and then recombines it to the widen vector
2537 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),LD->getValueType(0));
2538 unsigned WidenWidth = WidenVT.getSizeInBits();
2539 EVT LdVT = LD->getMemoryVT();
2541 assert(LdVT.isVector() && WidenVT.isVector());
2542 assert(LdVT.getVectorElementType() == WidenVT.getVectorElementType());
2545 SDValue Chain = LD->getChain();
2546 SDValue BasePtr = LD->getBasePtr();
2547 unsigned Align = LD->getAlignment();
2548 bool isVolatile = LD->isVolatile();
2549 bool isNonTemporal = LD->isNonTemporal();
2550 bool isInvariant = LD->isInvariant();
2552 int LdWidth = LdVT.getSizeInBits();
2553 int WidthDiff = WidenWidth - LdWidth; // Difference
2554 unsigned LdAlign = (isVolatile) ? 0 : Align; // Allow wider loads
2556 // Find the vector type that can load from.
2557 EVT NewVT = FindMemType(DAG, TLI, LdWidth, WidenVT, LdAlign, WidthDiff);
2558 int NewVTWidth = NewVT.getSizeInBits();
2559 SDValue LdOp = DAG.getLoad(NewVT, dl, Chain, BasePtr, LD->getPointerInfo(),
2560 isVolatile, isNonTemporal, isInvariant, Align);
2561 LdChain.push_back(LdOp.getValue(1));
2563 // Check if we can load the element with one instruction
2564 if (LdWidth <= NewVTWidth) {
2565 if (!NewVT.isVector()) {
2566 unsigned NumElts = WidenWidth / NewVTWidth;
2567 EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewVT, NumElts);
2568 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NewVecVT, LdOp);
2569 return DAG.getNode(ISD::BITCAST, dl, WidenVT, VecOp);
2571 if (NewVT == WidenVT)
2574 assert(WidenWidth % NewVTWidth == 0);
2575 unsigned NumConcat = WidenWidth / NewVTWidth;
2576 SmallVector<SDValue, 16> ConcatOps(NumConcat);
2577 SDValue UndefVal = DAG.getUNDEF(NewVT);
2578 ConcatOps[0] = LdOp;
2579 for (unsigned i = 1; i != NumConcat; ++i)
2580 ConcatOps[i] = UndefVal;
2581 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &ConcatOps[0],
2585 // Load vector by using multiple loads from largest vector to scalar
2586 SmallVector<SDValue, 16> LdOps;
2587 LdOps.push_back(LdOp);
2589 LdWidth -= NewVTWidth;
2590 unsigned Offset = 0;
2592 while (LdWidth > 0) {
2593 unsigned Increment = NewVTWidth / 8;
2594 Offset += Increment;
2595 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
2596 DAG.getIntPtrConstant(Increment));
2599 if (LdWidth < NewVTWidth) {
2600 // Our current type we are using is too large, find a better size
2601 NewVT = FindMemType(DAG, TLI, LdWidth, WidenVT, LdAlign, WidthDiff);
2602 NewVTWidth = NewVT.getSizeInBits();
2603 L = DAG.getLoad(NewVT, dl, Chain, BasePtr,
2604 LD->getPointerInfo().getWithOffset(Offset), isVolatile,
2605 isNonTemporal, isInvariant, MinAlign(Align, Increment));
2606 LdChain.push_back(L.getValue(1));
2607 if (L->getValueType(0).isVector()) {
2608 SmallVector<SDValue, 16> Loads;
2610 unsigned size = L->getValueSizeInBits(0);
2611 while (size < LdOp->getValueSizeInBits(0)) {
2612 Loads.push_back(DAG.getUNDEF(L->getValueType(0)));
2613 size += L->getValueSizeInBits(0);
2615 L = DAG.getNode(ISD::CONCAT_VECTORS, dl, LdOp->getValueType(0),
2616 &Loads[0], Loads.size());
2619 L = DAG.getLoad(NewVT, dl, Chain, BasePtr,
2620 LD->getPointerInfo().getWithOffset(Offset), isVolatile,
2621 isNonTemporal, isInvariant, MinAlign(Align, Increment));
2622 LdChain.push_back(L.getValue(1));
2628 LdWidth -= NewVTWidth;
2631 // Build the vector from the loads operations
2632 unsigned End = LdOps.size();
2633 if (!LdOps[0].getValueType().isVector())
2634 // All the loads are scalar loads.
2635 return BuildVectorFromScalar(DAG, WidenVT, LdOps, 0, End);
2637 // If the load contains vectors, build the vector using concat vector.
2638 // All of the vectors used to loads are power of 2 and the scalars load
2639 // can be combined to make a power of 2 vector.
2640 SmallVector<SDValue, 16> ConcatOps(End);
2643 EVT LdTy = LdOps[i].getValueType();
2644 // First combine the scalar loads to a vector
2645 if (!LdTy.isVector()) {
2646 for (--i; i >= 0; --i) {
2647 LdTy = LdOps[i].getValueType();
2648 if (LdTy.isVector())
2651 ConcatOps[--Idx] = BuildVectorFromScalar(DAG, LdTy, LdOps, i+1, End);
2653 ConcatOps[--Idx] = LdOps[i];
2654 for (--i; i >= 0; --i) {
2655 EVT NewLdTy = LdOps[i].getValueType();
2656 if (NewLdTy != LdTy) {
2657 // Create a larger vector
2658 ConcatOps[End-1] = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewLdTy,
2659 &ConcatOps[Idx], End - Idx);
2663 ConcatOps[--Idx] = LdOps[i];
2666 if (WidenWidth == LdTy.getSizeInBits()*(End - Idx))
2667 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT,
2668 &ConcatOps[Idx], End - Idx);
2670 // We need to fill the rest with undefs to build the vector
2671 unsigned NumOps = WidenWidth / LdTy.getSizeInBits();
2672 SmallVector<SDValue, 16> WidenOps(NumOps);
2673 SDValue UndefVal = DAG.getUNDEF(LdTy);
2676 for (; i != End-Idx; ++i)
2677 WidenOps[i] = ConcatOps[Idx+i];
2678 for (; i != NumOps; ++i)
2679 WidenOps[i] = UndefVal;
2681 return DAG.getNode(ISD::CONCAT_VECTORS, dl, WidenVT, &WidenOps[0],NumOps);
2685 DAGTypeLegalizer::GenWidenVectorExtLoads(SmallVectorImpl<SDValue> &LdChain,
2687 ISD::LoadExtType ExtType) {
2688 // For extension loads, it may not be more efficient to chop up the vector
2689 // and then extended it. Instead, we unroll the load and build a new vector.
2690 EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(),LD->getValueType(0));
2691 EVT LdVT = LD->getMemoryVT();
2693 assert(LdVT.isVector() && WidenVT.isVector());
2696 SDValue Chain = LD->getChain();
2697 SDValue BasePtr = LD->getBasePtr();
2698 unsigned Align = LD->getAlignment();
2699 bool isVolatile = LD->isVolatile();
2700 bool isNonTemporal = LD->isNonTemporal();
2702 EVT EltVT = WidenVT.getVectorElementType();
2703 EVT LdEltVT = LdVT.getVectorElementType();
2704 unsigned NumElts = LdVT.getVectorNumElements();
2706 // Load each element and widen
2707 unsigned WidenNumElts = WidenVT.getVectorNumElements();
2708 SmallVector<SDValue, 16> Ops(WidenNumElts);
2709 unsigned Increment = LdEltVT.getSizeInBits() / 8;
2710 Ops[0] = DAG.getExtLoad(ExtType, dl, EltVT, Chain, BasePtr,
2711 LD->getPointerInfo(),
2712 LdEltVT, isVolatile, isNonTemporal, Align);
2713 LdChain.push_back(Ops[0].getValue(1));
2714 unsigned i = 0, Offset = Increment;
2715 for (i=1; i < NumElts; ++i, Offset += Increment) {
2716 SDValue NewBasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
2717 BasePtr, DAG.getIntPtrConstant(Offset));
2718 Ops[i] = DAG.getExtLoad(ExtType, dl, EltVT, Chain, NewBasePtr,
2719 LD->getPointerInfo().getWithOffset(Offset), LdEltVT,
2720 isVolatile, isNonTemporal, Align);
2721 LdChain.push_back(Ops[i].getValue(1));
2724 // Fill the rest with undefs
2725 SDValue UndefVal = DAG.getUNDEF(EltVT);
2726 for (; i != WidenNumElts; ++i)
2729 return DAG.getNode(ISD::BUILD_VECTOR, dl, WidenVT, &Ops[0], Ops.size());
2733 void DAGTypeLegalizer::GenWidenVectorStores(SmallVectorImpl<SDValue> &StChain,
2735 // The strategy assumes that we can efficiently store powers of two widths.
2736 // The routines chops the vector into the largest vector stores with the same
2737 // element type or scalar stores.
2738 SDValue Chain = ST->getChain();
2739 SDValue BasePtr = ST->getBasePtr();
2740 unsigned Align = ST->getAlignment();
2741 bool isVolatile = ST->isVolatile();
2742 bool isNonTemporal = ST->isNonTemporal();
2743 SDValue ValOp = GetWidenedVector(ST->getValue());
2746 EVT StVT = ST->getMemoryVT();
2747 unsigned StWidth = StVT.getSizeInBits();
2748 EVT ValVT = ValOp.getValueType();
2749 unsigned ValWidth = ValVT.getSizeInBits();
2750 EVT ValEltVT = ValVT.getVectorElementType();
2751 unsigned ValEltWidth = ValEltVT.getSizeInBits();
2752 assert(StVT.getVectorElementType() == ValEltVT);
2754 int Idx = 0; // current index to store
2755 unsigned Offset = 0; // offset from base to store
2756 while (StWidth != 0) {
2757 // Find the largest vector type we can store with
2758 EVT NewVT = FindMemType(DAG, TLI, StWidth, ValVT);
2759 unsigned NewVTWidth = NewVT.getSizeInBits();
2760 unsigned Increment = NewVTWidth / 8;
2761 if (NewVT.isVector()) {
2762 unsigned NumVTElts = NewVT.getVectorNumElements();
2764 SDValue EOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NewVT, ValOp,
2765 DAG.getConstant(Idx, TLI.getVectorIdxTy()));
2766 StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr,
2767 ST->getPointerInfo().getWithOffset(Offset),
2768 isVolatile, isNonTemporal,
2769 MinAlign(Align, Offset)));
2770 StWidth -= NewVTWidth;
2771 Offset += Increment;
2773 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
2774 DAG.getIntPtrConstant(Increment));
2775 } while (StWidth != 0 && StWidth >= NewVTWidth);
2777 // Cast the vector to the scalar type we can store
2778 unsigned NumElts = ValWidth / NewVTWidth;
2779 EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewVT, NumElts);
2780 SDValue VecOp = DAG.getNode(ISD::BITCAST, dl, NewVecVT, ValOp);
2781 // Readjust index position based on new vector type
2782 Idx = Idx * ValEltWidth / NewVTWidth;
2784 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, VecOp,
2785 DAG.getConstant(Idx++, TLI.getVectorIdxTy()));
2786 StChain.push_back(DAG.getStore(Chain, dl, EOp, BasePtr,
2787 ST->getPointerInfo().getWithOffset(Offset),
2788 isVolatile, isNonTemporal,
2789 MinAlign(Align, Offset)));
2790 StWidth -= NewVTWidth;
2791 Offset += Increment;
2792 BasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(), BasePtr,
2793 DAG.getIntPtrConstant(Increment));
2794 } while (StWidth != 0 && StWidth >= NewVTWidth);
2795 // Restore index back to be relative to the original widen element type
2796 Idx = Idx * NewVTWidth / ValEltWidth;
2802 DAGTypeLegalizer::GenWidenVectorTruncStores(SmallVectorImpl<SDValue> &StChain,
2804 // For extension loads, it may not be more efficient to truncate the vector
2805 // and then store it. Instead, we extract each element and then store it.
2806 SDValue Chain = ST->getChain();
2807 SDValue BasePtr = ST->getBasePtr();
2808 unsigned Align = ST->getAlignment();
2809 bool isVolatile = ST->isVolatile();
2810 bool isNonTemporal = ST->isNonTemporal();
2811 SDValue ValOp = GetWidenedVector(ST->getValue());
2814 EVT StVT = ST->getMemoryVT();
2815 EVT ValVT = ValOp.getValueType();
2817 // It must be true that we the widen vector type is bigger than where
2818 // we need to store.
2819 assert(StVT.isVector() && ValOp.getValueType().isVector());
2820 assert(StVT.bitsLT(ValOp.getValueType()));
2822 // For truncating stores, we can not play the tricks of chopping legal
2823 // vector types and bit cast it to the right type. Instead, we unroll
2825 EVT StEltVT = StVT.getVectorElementType();
2826 EVT ValEltVT = ValVT.getVectorElementType();
2827 unsigned Increment = ValEltVT.getSizeInBits() / 8;
2828 unsigned NumElts = StVT.getVectorNumElements();
2829 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ValEltVT, ValOp,
2830 DAG.getConstant(0, TLI.getVectorIdxTy()));
2831 StChain.push_back(DAG.getTruncStore(Chain, dl, EOp, BasePtr,
2832 ST->getPointerInfo(), StEltVT,
2833 isVolatile, isNonTemporal, Align));
2834 unsigned Offset = Increment;
2835 for (unsigned i=1; i < NumElts; ++i, Offset += Increment) {
2836 SDValue NewBasePtr = DAG.getNode(ISD::ADD, dl, BasePtr.getValueType(),
2837 BasePtr, DAG.getIntPtrConstant(Offset));
2838 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ValEltVT, ValOp,
2839 DAG.getConstant(0, TLI.getVectorIdxTy()));
2840 StChain.push_back(DAG.getTruncStore(Chain, dl, EOp, NewBasePtr,
2841 ST->getPointerInfo().getWithOffset(Offset),
2842 StEltVT, isVolatile, isNonTemporal,
2843 MinAlign(Align, Offset)));
2847 /// Modifies a vector input (widen or narrows) to a vector of NVT. The
2848 /// input vector must have the same element type as NVT.
2849 SDValue DAGTypeLegalizer::ModifyToType(SDValue InOp, EVT NVT) {
2850 // Note that InOp might have been widened so it might already have
2851 // the right width or it might need be narrowed.
2852 EVT InVT = InOp.getValueType();
2853 assert(InVT.getVectorElementType() == NVT.getVectorElementType() &&
2854 "input and widen element type must match");
2857 // Check if InOp already has the right width.
2861 unsigned InNumElts = InVT.getVectorNumElements();
2862 unsigned WidenNumElts = NVT.getVectorNumElements();
2863 if (WidenNumElts > InNumElts && WidenNumElts % InNumElts == 0) {
2864 unsigned NumConcat = WidenNumElts / InNumElts;
2865 SmallVector<SDValue, 16> Ops(NumConcat);
2866 SDValue UndefVal = DAG.getUNDEF(InVT);
2868 for (unsigned i = 1; i != NumConcat; ++i)
2871 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, &Ops[0], NumConcat);
2874 if (WidenNumElts < InNumElts && InNumElts % WidenNumElts)
2875 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, InOp,
2876 DAG.getConstant(0, TLI.getVectorIdxTy()));
2878 // Fall back to extract and build.
2879 SmallVector<SDValue, 16> Ops(WidenNumElts);
2880 EVT EltVT = NVT.getVectorElementType();
2881 unsigned MinNumElts = std::min(WidenNumElts, InNumElts);
2883 for (Idx = 0; Idx < MinNumElts; ++Idx)
2884 Ops[Idx] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp,
2885 DAG.getConstant(Idx, TLI.getVectorIdxTy()));
2887 SDValue UndefVal = DAG.getUNDEF(EltVT);
2888 for ( ; Idx < WidenNumElts; ++Idx)
2889 Ops[Idx] = UndefVal;
2890 return DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &Ops[0], WidenNumElts);