Simplify SplitVecRes_EXTRACT_SUBVECTOR. This means
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeVectorTypes.cpp
1 //===------- LegalizeVectorTypes.cpp - Legalization of vector types -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
16 // types.
17 // Splitting is the act of changing a computation in an invalid vector type to
18 // be a computation in multiple vectors of a smaller type.  For example,
19 // implementing <128 x f32> operations in terms of two <64 x f32> operations.
20 //
21 //===----------------------------------------------------------------------===//
22
23 #include "LegalizeTypes.h"
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 //  Result Vector Scalarization: <1 x ty> -> ty.
28 //===----------------------------------------------------------------------===//
29
30 void DAGTypeLegalizer::ScalarizeVectorResult(SDNode *N, unsigned ResNo) {
31   DEBUG(cerr << "Scalarize node result " << ResNo << ": "; N->dump(&DAG);
32         cerr << "\n");
33   SDValue R = SDValue();
34
35   switch (N->getOpcode()) {
36   default:
37 #ifndef NDEBUG
38     cerr << "ScalarizeVectorResult #" << ResNo << ": ";
39     N->dump(&DAG); cerr << "\n";
40 #endif
41     assert(0 && "Do not know how to scalarize the result of this operator!");
42     abort();
43
44   case ISD::BIT_CONVERT:    R = ScalarizeVecRes_BIT_CONVERT(N); break;
45   case ISD::BUILD_VECTOR:   R = N->getOperand(0); break;
46   case ISD::CONVERT_RNDSAT: R = ScalarizeVecRes_CONVERT_RNDSAT(N); break;
47   case ISD::EXTRACT_SUBVECTOR: R = ScalarizeVecRes_EXTRACT_SUBVECTOR(N); break;
48   case ISD::FPOWI:          R = ScalarizeVecRes_FPOWI(N); break;
49   case ISD::INSERT_VECTOR_ELT: R = ScalarizeVecRes_INSERT_VECTOR_ELT(N); break;
50   case ISD::LOAD:           R = ScalarizeVecRes_LOAD(cast<LoadSDNode>(N));break;
51   case ISD::SELECT:         R = ScalarizeVecRes_SELECT(N); break;
52   case ISD::SELECT_CC:      R = ScalarizeVecRes_SELECT_CC(N); break;
53   case ISD::UNDEF:          R = ScalarizeVecRes_UNDEF(N); break;
54   case ISD::VECTOR_SHUFFLE: R = ScalarizeVecRes_VECTOR_SHUFFLE(N); break;
55   case ISD::VSETCC:         R = ScalarizeVecRes_VSETCC(N); break;
56
57   case ISD::CTLZ:
58   case ISD::CTPOP:
59   case ISD::CTTZ:
60   case ISD::FABS:
61   case ISD::FCOS:
62   case ISD::FNEG:
63   case ISD::FP_TO_SINT:
64   case ISD::FP_TO_UINT:
65   case ISD::FSIN:
66   case ISD::FSQRT:
67   case ISD::FTRUNC:
68   case ISD::FFLOOR:
69   case ISD::FCEIL:
70   case ISD::FRINT:
71   case ISD::FNEARBYINT:
72   case ISD::SINT_TO_FP:
73   case ISD::TRUNCATE:
74   case ISD::UINT_TO_FP: R = ScalarizeVecRes_UnaryOp(N); break;
75
76   case ISD::ADD:
77   case ISD::AND:
78   case ISD::FADD:
79   case ISD::FDIV:
80   case ISD::FMUL:
81   case ISD::FPOW:
82   case ISD::FREM:
83   case ISD::FSUB:
84   case ISD::MUL:
85   case ISD::OR:
86   case ISD::SDIV:
87   case ISD::SREM:
88   case ISD::SUB:
89   case ISD::UDIV:
90   case ISD::UREM:
91   case ISD::XOR:  R = ScalarizeVecRes_BinOp(N); break;
92   }
93
94   // If R is null, the sub-method took care of registering the result.
95   if (R.getNode())
96     SetScalarizedVector(SDValue(N, ResNo), R);
97 }
98
99 SDValue DAGTypeLegalizer::ScalarizeVecRes_BinOp(SDNode *N) {
100   SDValue LHS = GetScalarizedVector(N->getOperand(0));
101   SDValue RHS = GetScalarizedVector(N->getOperand(1));
102   return DAG.getNode(N->getOpcode(), LHS.getValueType(), LHS, RHS);
103 }
104
105 SDValue DAGTypeLegalizer::ScalarizeVecRes_BIT_CONVERT(SDNode *N) {
106   MVT NewVT = N->getValueType(0).getVectorElementType();
107   return DAG.getNode(ISD::BIT_CONVERT, NewVT, N->getOperand(0));
108 }
109
110 SDValue DAGTypeLegalizer::ScalarizeVecRes_CONVERT_RNDSAT(SDNode *N) {
111   MVT NewVT = N->getValueType(0).getVectorElementType();
112   SDValue Op0 = GetScalarizedVector(N->getOperand(0));
113   return DAG.getConvertRndSat(NewVT, Op0, DAG.getValueType(NewVT),
114                               DAG.getValueType(Op0.getValueType()),
115                               N->getOperand(3),
116                               N->getOperand(4),
117                               cast<CvtRndSatSDNode>(N)->getCvtCode());
118 }
119
120 SDValue DAGTypeLegalizer::ScalarizeVecRes_EXTRACT_SUBVECTOR(SDNode *N) {
121   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
122                      N->getValueType(0).getVectorElementType(),
123                      N->getOperand(0), N->getOperand(1));
124 }
125
126 SDValue DAGTypeLegalizer::ScalarizeVecRes_FPOWI(SDNode *N) {
127   SDValue Op = GetScalarizedVector(N->getOperand(0));
128   return DAG.getNode(ISD::FPOWI, Op.getValueType(), Op, N->getOperand(1));
129 }
130
131 SDValue DAGTypeLegalizer::ScalarizeVecRes_INSERT_VECTOR_ELT(SDNode *N) {
132   // The value to insert may have a wider type than the vector element type,
133   // so be sure to truncate it to the element type if necessary.
134   SDValue Op = N->getOperand(1);
135   MVT EltVT = N->getValueType(0).getVectorElementType();
136   if (Op.getValueType() != EltVT)
137     // FIXME: Can this happen for floating point types?
138     Op = DAG.getNode(ISD::TRUNCATE, EltVT, Op);
139   return Op;
140 }
141
142 SDValue DAGTypeLegalizer::ScalarizeVecRes_LOAD(LoadSDNode *N) {
143   assert(N->isUnindexed() && "Indexed vector load?");
144
145   SDValue Result = DAG.getLoad(ISD::UNINDEXED, N->getExtensionType(),
146                                N->getValueType(0).getVectorElementType(),
147                                N->getChain(), N->getBasePtr(),
148                                DAG.getNode(ISD::UNDEF,
149                                            N->getBasePtr().getValueType()),
150                                N->getSrcValue(), N->getSrcValueOffset(),
151                                N->getMemoryVT().getVectorElementType(),
152                                N->isVolatile(), N->getAlignment());
153
154   // Legalized the chain result - switch anything that used the old chain to
155   // use the new one.
156   ReplaceValueWith(SDValue(N, 1), Result.getValue(1));
157   return Result;
158 }
159
160 SDValue DAGTypeLegalizer::ScalarizeVecRes_UnaryOp(SDNode *N) {
161   // Get the dest type - it doesn't always match the input type, e.g. int_to_fp.
162   MVT DestVT = N->getValueType(0).getVectorElementType();
163   SDValue Op = GetScalarizedVector(N->getOperand(0));
164   return DAG.getNode(N->getOpcode(), DestVT, Op);
165 }
166
167 SDValue DAGTypeLegalizer::ScalarizeVecRes_UNDEF(SDNode *N) {
168   return DAG.getNode(ISD::UNDEF, N->getValueType(0).getVectorElementType());
169 }
170
171 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT(SDNode *N) {
172   SDValue LHS = GetScalarizedVector(N->getOperand(1));
173   return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0), LHS,
174                      GetScalarizedVector(N->getOperand(2)));
175 }
176
177 SDValue DAGTypeLegalizer::ScalarizeVecRes_SELECT_CC(SDNode *N) {
178   SDValue LHS = GetScalarizedVector(N->getOperand(2));
179   return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(),
180                      N->getOperand(0), N->getOperand(1),
181                      LHS, GetScalarizedVector(N->getOperand(3)),
182                      N->getOperand(4));
183 }
184
185 SDValue DAGTypeLegalizer::ScalarizeVecRes_VECTOR_SHUFFLE(SDNode *N) {
186   // Figure out if the scalar is the LHS or RHS and return it.
187   SDValue Arg = N->getOperand(2).getOperand(0);
188   if (Arg.getOpcode() == ISD::UNDEF)
189     return DAG.getNode(ISD::UNDEF, N->getValueType(0).getVectorElementType());
190   unsigned Op = !cast<ConstantSDNode>(Arg)->isNullValue();
191   return GetScalarizedVector(N->getOperand(Op));
192 }
193
194 SDValue DAGTypeLegalizer::ScalarizeVecRes_VSETCC(SDNode *N) {
195   SDValue LHS = GetScalarizedVector(N->getOperand(0));
196   SDValue RHS = GetScalarizedVector(N->getOperand(1));
197   MVT NVT = N->getValueType(0).getVectorElementType();
198   MVT SVT = TLI.getSetCCResultType(LHS);
199
200   // Turn it into a scalar SETCC.
201   SDValue Res = DAG.getNode(ISD::SETCC, SVT, LHS, RHS, N->getOperand(2));
202
203   // VSETCC always returns a sign-extended value, while SETCC may not.  The
204   // SETCC result type may not match the vector element type.  Correct these.
205   if (NVT.bitsLE(SVT)) {
206     // The SETCC result type is bigger than the vector element type.
207     // Ensure the SETCC result is sign-extended.
208     if (TLI.getSetCCResultContents() !=
209         TargetLowering::ZeroOrNegativeOneSetCCResult)
210       Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, SVT, Res,
211                         DAG.getValueType(MVT::i1));
212     // Truncate to the final type.
213     return DAG.getNode(ISD::TRUNCATE, NVT, Res);
214   } else {
215     // The SETCC result type is smaller than the vector element type.
216     // If the SetCC result is not sign-extended, chop it down to MVT::i1.
217     if (TLI.getSetCCResultContents() !=
218         TargetLowering::ZeroOrNegativeOneSetCCResult)
219       Res = DAG.getNode(ISD::TRUNCATE, MVT::i1, Res);
220     // Sign extend to the final type.
221     return DAG.getNode(ISD::SIGN_EXTEND, NVT, Res);
222   }
223 }
224
225
226 //===----------------------------------------------------------------------===//
227 //  Operand Vector Scalarization <1 x ty> -> ty.
228 //===----------------------------------------------------------------------===//
229
230 bool DAGTypeLegalizer::ScalarizeVectorOperand(SDNode *N, unsigned OpNo) {
231   DEBUG(cerr << "Scalarize node operand " << OpNo << ": "; N->dump(&DAG);
232         cerr << "\n");
233   SDValue Res = SDValue();
234
235   if (Res.getNode() == 0) {
236     switch (N->getOpcode()) {
237     default:
238 #ifndef NDEBUG
239       cerr << "ScalarizeVectorOperand Op #" << OpNo << ": ";
240       N->dump(&DAG); cerr << "\n";
241 #endif
242       assert(0 && "Do not know how to scalarize this operator's operand!");
243       abort();
244
245     case ISD::BIT_CONVERT:
246       Res = ScalarizeVecOp_BIT_CONVERT(N); break;
247
248     case ISD::CONCAT_VECTORS:
249       Res = ScalarizeVecOp_CONCAT_VECTORS(N); break;
250
251     case ISD::EXTRACT_VECTOR_ELT:
252       Res = ScalarizeVecOp_EXTRACT_VECTOR_ELT(N); break;
253
254     case ISD::STORE:
255       Res = ScalarizeVecOp_STORE(cast<StoreSDNode>(N), OpNo); break;
256     }
257   }
258
259   // If the result is null, the sub-method took care of registering results etc.
260   if (!Res.getNode()) return false;
261
262   // If the result is N, the sub-method updated N in place.  Check to see if any
263   // operands are new, and if so, mark them.
264   if (Res.getNode() == N) {
265     // Mark N as new and remark N and its operands.  This allows us to correctly
266     // revisit N if it needs another step of promotion and allows us to visit
267     // any new operands to N.
268     ReanalyzeNode(N);
269     return true;
270   }
271
272   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
273          "Invalid operand expansion");
274
275   ReplaceValueWith(SDValue(N, 0), Res);
276   return false;
277 }
278
279 /// ScalarizeVecOp_BIT_CONVERT - If the value to convert is a vector that needs
280 /// to be scalarized, it must be <1 x ty>.  Convert the element instead.
281 SDValue DAGTypeLegalizer::ScalarizeVecOp_BIT_CONVERT(SDNode *N) {
282   SDValue Elt = GetScalarizedVector(N->getOperand(0));
283   return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Elt);
284 }
285
286 /// ScalarizeVecOp_CONCAT_VECTORS - The vectors to concatenate have length one -
287 /// use a BUILD_VECTOR instead.
288 SDValue DAGTypeLegalizer::ScalarizeVecOp_CONCAT_VECTORS(SDNode *N) {
289   SmallVector<SDValue, 8> Ops(N->getNumOperands());
290   for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
291     Ops[i] = GetScalarizedVector(N->getOperand(i));
292   return DAG.getNode(ISD::BUILD_VECTOR, N->getValueType(0),
293                      &Ops[0], Ops.size());
294 }
295
296 /// ScalarizeVecOp_EXTRACT_VECTOR_ELT - If the input is a vector that needs to
297 /// be scalarized, it must be <1 x ty>, so just return the element, ignoring the
298 /// index.
299 SDValue DAGTypeLegalizer::ScalarizeVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
300   return GetScalarizedVector(N->getOperand(0));
301 }
302
303 /// ScalarizeVecOp_STORE - If the value to store is a vector that needs to be
304 /// scalarized, it must be <1 x ty>.  Just store the element.
305 SDValue DAGTypeLegalizer::ScalarizeVecOp_STORE(StoreSDNode *N, unsigned OpNo){
306   assert(N->isUnindexed() && "Indexed store of one-element vector?");
307   assert(OpNo == 1 && "Do not know how to scalarize this operand!");
308
309   if (N->isTruncatingStore())
310     return DAG.getTruncStore(N->getChain(),
311                              GetScalarizedVector(N->getOperand(1)),
312                              N->getBasePtr(),
313                              N->getSrcValue(), N->getSrcValueOffset(),
314                              N->getMemoryVT().getVectorElementType(),
315                              N->isVolatile(), N->getAlignment());
316
317   return DAG.getStore(N->getChain(), GetScalarizedVector(N->getOperand(1)),
318                       N->getBasePtr(), N->getSrcValue(), N->getSrcValueOffset(),
319                       N->isVolatile(), N->getAlignment());
320 }
321
322
323 //===----------------------------------------------------------------------===//
324 //  Result Vector Splitting
325 //===----------------------------------------------------------------------===//
326
327 /// SplitVectorResult - This method is called when the specified result of the
328 /// specified node is found to need vector splitting.  At this point, the node
329 /// may also have invalid operands or may have other results that need
330 /// legalization, we just know that (at least) one result needs vector
331 /// splitting.
332 void DAGTypeLegalizer::SplitVectorResult(SDNode *N, unsigned ResNo) {
333   DEBUG(cerr << "Split node result: "; N->dump(&DAG); cerr << "\n");
334   SDValue Lo, Hi;
335
336   switch (N->getOpcode()) {
337   default:
338 #ifndef NDEBUG
339     cerr << "SplitVectorResult #" << ResNo << ": ";
340     N->dump(&DAG); cerr << "\n";
341 #endif
342     assert(0 && "Do not know how to split the result of this operator!");
343     abort();
344
345   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
346   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
347   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
348   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
349
350   case ISD::BIT_CONVERT:       SplitVecRes_BIT_CONVERT(N, Lo, Hi); break;
351   case ISD::BUILD_VECTOR:      SplitVecRes_BUILD_VECTOR(N, Lo, Hi); break;
352   case ISD::CONCAT_VECTORS:    SplitVecRes_CONCAT_VECTORS(N, Lo, Hi); break;
353   case ISD::CONVERT_RNDSAT:    SplitVecRes_CONVERT_RNDSAT(N, Lo, Hi); break;
354   case ISD::EXTRACT_SUBVECTOR: SplitVecRes_EXTRACT_SUBVECTOR(N, Lo, Hi); break;
355   case ISD::FPOWI:             SplitVecRes_FPOWI(N, Lo, Hi); break;
356   case ISD::INSERT_VECTOR_ELT: SplitVecRes_INSERT_VECTOR_ELT(N, Lo, Hi); break;
357   case ISD::LOAD:           SplitVecRes_LOAD(cast<LoadSDNode>(N), Lo, Hi);break;
358   case ISD::VECTOR_SHUFFLE:    SplitVecRes_VECTOR_SHUFFLE(N, Lo, Hi); break;
359   case ISD::VSETCC:            SplitVecRes_VSETCC(N, Lo, Hi); break;
360
361   case ISD::CTTZ:
362   case ISD::CTLZ:
363   case ISD::CTPOP:
364   case ISD::FNEG:
365   case ISD::FABS:
366   case ISD::FSQRT:
367   case ISD::FSIN:
368   case ISD::FCOS:
369   case ISD::FTRUNC:
370   case ISD::FFLOOR:
371   case ISD::FCEIL:
372   case ISD::FRINT:
373   case ISD::FNEARBYINT:
374   case ISD::FP_TO_SINT:
375   case ISD::FP_TO_UINT:
376   case ISD::SINT_TO_FP:
377   case ISD::TRUNCATE:
378   case ISD::UINT_TO_FP: SplitVecRes_UnaryOp(N, Lo, Hi); break;
379
380   case ISD::ADD:
381   case ISD::SUB:
382   case ISD::MUL:
383   case ISD::FADD:
384   case ISD::FSUB:
385   case ISD::FMUL:
386   case ISD::SDIV:
387   case ISD::UDIV:
388   case ISD::FDIV:
389   case ISD::FPOW:
390   case ISD::AND:
391   case ISD::OR:
392   case ISD::XOR:
393   case ISD::UREM:
394   case ISD::SREM:
395   case ISD::FREM: SplitVecRes_BinOp(N, Lo, Hi); break;
396   }
397
398   // If Lo/Hi is null, the sub-method took care of registering results etc.
399   if (Lo.getNode())
400     SetSplitVector(SDValue(N, ResNo), Lo, Hi);
401 }
402
403 void DAGTypeLegalizer::SplitVecRes_BinOp(SDNode *N, SDValue &Lo,
404                                          SDValue &Hi) {
405   SDValue LHSLo, LHSHi;
406   GetSplitVector(N->getOperand(0), LHSLo, LHSHi);
407   SDValue RHSLo, RHSHi;
408   GetSplitVector(N->getOperand(1), RHSLo, RHSHi);
409
410   Lo = DAG.getNode(N->getOpcode(), LHSLo.getValueType(), LHSLo, RHSLo);
411   Hi = DAG.getNode(N->getOpcode(), LHSHi.getValueType(), LHSHi, RHSHi);
412 }
413
414 void DAGTypeLegalizer::SplitVecRes_BIT_CONVERT(SDNode *N, SDValue &Lo,
415                                                SDValue &Hi) {
416   // We know the result is a vector.  The input may be either a vector or a
417   // scalar value.
418   MVT LoVT, HiVT;
419   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
420
421   SDValue InOp = N->getOperand(0);
422   MVT InVT = InOp.getValueType();
423
424   // Handle some special cases efficiently.
425   switch (getTypeAction(InVT)) {
426   default:
427     assert(false && "Unknown type action!");
428   case Legal:
429   case PromoteInteger:
430   case SoftenFloat:
431   case ScalarizeVector:
432     break;
433   case ExpandInteger:
434   case ExpandFloat:
435     // A scalar to vector conversion, where the scalar needs expansion.
436     // If the vector is being split in two then we can just convert the
437     // expanded pieces.
438     if (LoVT == HiVT) {
439       GetExpandedOp(InOp, Lo, Hi);
440       if (TLI.isBigEndian())
441         std::swap(Lo, Hi);
442       Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
443       Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
444       return;
445     }
446     break;
447   case SplitVector:
448     // If the input is a vector that needs to be split, convert each split
449     // piece of the input now.
450     GetSplitVector(InOp, Lo, Hi);
451     Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
452     Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
453     return;
454   }
455
456   // In the general case, convert the input to an integer and split it by hand.
457   MVT LoIntVT = MVT::getIntegerVT(LoVT.getSizeInBits());
458   MVT HiIntVT = MVT::getIntegerVT(HiVT.getSizeInBits());
459   if (TLI.isBigEndian())
460     std::swap(LoIntVT, HiIntVT);
461
462   SplitInteger(BitConvertToInteger(InOp), LoIntVT, HiIntVT, Lo, Hi);
463
464   if (TLI.isBigEndian())
465     std::swap(Lo, Hi);
466   Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
467   Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
468 }
469
470 void DAGTypeLegalizer::SplitVecRes_BUILD_VECTOR(SDNode *N, SDValue &Lo,
471                                                 SDValue &Hi) {
472   MVT LoVT, HiVT;
473   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
474   unsigned LoNumElts = LoVT.getVectorNumElements();
475   SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+LoNumElts);
476   Lo = DAG.getNode(ISD::BUILD_VECTOR, LoVT, &LoOps[0], LoOps.size());
477
478   SmallVector<SDValue, 8> HiOps(N->op_begin()+LoNumElts, N->op_end());
479   Hi = DAG.getNode(ISD::BUILD_VECTOR, HiVT, &HiOps[0], HiOps.size());
480 }
481
482 void DAGTypeLegalizer::SplitVecRes_CONCAT_VECTORS(SDNode *N, SDValue &Lo,
483                                                   SDValue &Hi) {
484   assert(!(N->getNumOperands() & 1) && "Unsupported CONCAT_VECTORS");
485   unsigned NumSubvectors = N->getNumOperands() / 2;
486   if (NumSubvectors == 1) {
487     Lo = N->getOperand(0);
488     Hi = N->getOperand(1);
489     return;
490   }
491
492   MVT LoVT, HiVT;
493   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
494
495   SmallVector<SDValue, 8> LoOps(N->op_begin(), N->op_begin()+NumSubvectors);
496   Lo = DAG.getNode(ISD::CONCAT_VECTORS, LoVT, &LoOps[0], LoOps.size());
497
498   SmallVector<SDValue, 8> HiOps(N->op_begin()+NumSubvectors, N->op_end());
499   Hi = DAG.getNode(ISD::CONCAT_VECTORS, HiVT, &HiOps[0], HiOps.size());
500 }
501
502 void DAGTypeLegalizer::SplitVecRes_CONVERT_RNDSAT(SDNode *N, SDValue &Lo,
503                                                   SDValue &Hi) {
504   MVT LoVT, HiVT;
505   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
506   SDValue VLo, VHi;
507   GetSplitVector(N->getOperand(0), VLo, VHi);
508   SDValue DTyOpLo =  DAG.getValueType(LoVT);
509   SDValue DTyOpHi =  DAG.getValueType(HiVT);
510   SDValue STyOpLo =  DAG.getValueType(VLo.getValueType());
511   SDValue STyOpHi =  DAG.getValueType(VHi.getValueType());
512
513   SDValue RndOp = N->getOperand(3);
514   SDValue SatOp = N->getOperand(4);
515   ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
516
517   Lo = DAG.getConvertRndSat(LoVT, VLo, DTyOpLo, STyOpLo, RndOp, SatOp, CvtCode);
518   Hi = DAG.getConvertRndSat(HiVT, VHi, DTyOpHi, STyOpHi, RndOp, SatOp, CvtCode);
519 }
520
521 void DAGTypeLegalizer::SplitVecRes_EXTRACT_SUBVECTOR(SDNode *N, SDValue &Lo,
522                                                      SDValue &Hi) {
523   SDValue Vec = N->getOperand(0);
524   SDValue Idx = N->getOperand(1);
525   MVT IdxVT = Idx.getValueType();
526
527   MVT LoVT, HiVT;
528   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
529   // The indices are not guaranteed to be a multiple of the new vector
530   // size unless the original vector type was split in two.
531   assert(LoVT == HiVT && "Non power-of-two vectors not supported!");
532
533   Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, LoVT, Vec, Idx);
534   Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
535                     DAG.getConstant(LoVT.getVectorNumElements(), IdxVT));
536   Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, HiVT, Vec, Idx);
537 }
538
539 void DAGTypeLegalizer::SplitVecRes_FPOWI(SDNode *N, SDValue &Lo,
540                                          SDValue &Hi) {
541   GetSplitVector(N->getOperand(0), Lo, Hi);
542   Lo = DAG.getNode(ISD::FPOWI, Lo.getValueType(), Lo, N->getOperand(1));
543   Hi = DAG.getNode(ISD::FPOWI, Hi.getValueType(), Hi, N->getOperand(1));
544 }
545
546 void DAGTypeLegalizer::SplitVecRes_INSERT_VECTOR_ELT(SDNode *N, SDValue &Lo,
547                                                      SDValue &Hi) {
548   SDValue Vec = N->getOperand(0);
549   SDValue Elt = N->getOperand(1);
550   SDValue Idx = N->getOperand(2);
551   GetSplitVector(Vec, Lo, Hi);
552
553   if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
554     unsigned IdxVal = CIdx->getZExtValue();
555     unsigned LoNumElts = Lo.getValueType().getVectorNumElements();
556     if (IdxVal < LoNumElts)
557       Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, Lo.getValueType(), Lo, Elt, Idx);
558     else
559       Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, Hi.getValueType(), Hi, Elt,
560                        DAG.getIntPtrConstant(IdxVal - LoNumElts));
561     return;
562   }
563
564   // Spill the vector to the stack.
565   MVT VecVT = Vec.getValueType();
566   MVT EltVT = VecVT.getVectorElementType();
567   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
568   SDValue Store = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
569
570   // Store the new element.  This may be larger than the vector element type,
571   // so use a truncating store.
572   SDValue EltPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
573   Store = DAG.getTruncStore(Store, Elt, EltPtr, NULL, 0, EltVT);
574
575   // Reload the vector from the stack.
576   SDValue Load = DAG.getLoad(VecVT, Store, StackPtr, NULL, 0);
577
578   // Split it.
579   SplitVecRes_LOAD(cast<LoadSDNode>(Load.getNode()), Lo, Hi);
580 }
581
582 void DAGTypeLegalizer::SplitVecRes_LOAD(LoadSDNode *LD, SDValue &Lo,
583                                         SDValue &Hi) {
584   assert(ISD::isUNINDEXEDLoad(LD) && "Indexed load during type legalization!");
585   MVT LoVT, HiVT;
586   GetSplitDestVTs(LD->getValueType(0), LoVT, HiVT);
587
588   ISD::LoadExtType ExtType = LD->getExtensionType();
589   SDValue Ch = LD->getChain();
590   SDValue Ptr = LD->getBasePtr();
591   SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
592   const Value *SV = LD->getSrcValue();
593   int SVOffset = LD->getSrcValueOffset();
594   MVT MemoryVT = LD->getMemoryVT();
595   unsigned Alignment = LD->getAlignment();
596   bool isVolatile = LD->isVolatile();
597
598   MVT LoMemVT, HiMemVT;
599   GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
600
601   Lo = DAG.getLoad(ISD::UNINDEXED, ExtType, LoVT, Ch, Ptr, Offset,
602                    SV, SVOffset, LoMemVT, isVolatile, Alignment);
603
604   unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
605   Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
606                     DAG.getIntPtrConstant(IncrementSize));
607   SVOffset += IncrementSize;
608   Alignment = MinAlign(Alignment, IncrementSize);
609   Hi = DAG.getLoad(ISD::UNINDEXED, ExtType, HiVT, Ch, Ptr, Offset,
610                    SV, SVOffset, HiMemVT, isVolatile, Alignment);
611
612   // Build a factor node to remember that this load is independent of the
613   // other one.
614   Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
615                    Hi.getValue(1));
616
617   // Legalized the chain result - switch anything that used the old chain to
618   // use the new one.
619   ReplaceValueWith(SDValue(LD, 1), Ch);
620 }
621
622 void DAGTypeLegalizer::SplitVecRes_UnaryOp(SDNode *N, SDValue &Lo,
623                                            SDValue &Hi) {
624   // Get the dest types - they may not match the input types, e.g. int_to_fp.
625   MVT LoVT, HiVT;
626   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
627
628   // Split the input.
629   MVT InVT = N->getOperand(0).getValueType();
630   switch (getTypeAction(InVT)) {
631   default: assert(0 && "Unexpected type action!");
632   case Legal: {
633     assert(LoVT == HiVT && "Legal non-power-of-two vector type?");
634     MVT InNVT = MVT::getVectorVT(InVT.getVectorElementType(),
635                                  LoVT.getVectorNumElements());
636     Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, InNVT, N->getOperand(0),
637                      DAG.getIntPtrConstant(0));
638     Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, InNVT, N->getOperand(0),
639                      DAG.getIntPtrConstant(InNVT.getVectorNumElements()));
640     break;
641   }
642   case SplitVector:
643     GetSplitVector(N->getOperand(0), Lo, Hi);
644     break;
645   }
646
647   Lo = DAG.getNode(N->getOpcode(), LoVT, Lo);
648   Hi = DAG.getNode(N->getOpcode(), HiVT, Hi);
649 }
650
651 void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(SDNode *N, SDValue &Lo,
652                                                   SDValue &Hi) {
653   // Build the low part.
654   SDValue Mask = N->getOperand(2);
655   SmallVector<SDValue, 16> Ops;
656   MVT LoVT, HiVT;
657   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
658   MVT EltVT = LoVT.getVectorElementType();
659   unsigned LoNumElts = LoVT.getVectorNumElements();
660   unsigned NumElements = Mask.getNumOperands();
661
662   // Insert all of the elements from the input that are needed.  We use
663   // buildvector of extractelement here because the input vectors will have
664   // to be legalized, so this makes the code simpler.
665   for (unsigned i = 0; i != LoNumElts; ++i) {
666     SDValue Arg = Mask.getOperand(i);
667     if (Arg.getOpcode() == ISD::UNDEF) {
668       Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
669     } else {
670       unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getZExtValue();
671       SDValue InVec = N->getOperand(0);
672       if (Idx >= NumElements) {
673         InVec = N->getOperand(1);
674         Idx -= NumElements;
675       }
676       Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, InVec,
677                                 DAG.getIntPtrConstant(Idx)));
678     }
679   }
680   Lo = DAG.getNode(ISD::BUILD_VECTOR, LoVT, &Ops[0], Ops.size());
681   Ops.clear();
682
683   for (unsigned i = LoNumElts; i != NumElements; ++i) {
684     SDValue Arg = Mask.getOperand(i);
685     if (Arg.getOpcode() == ISD::UNDEF) {
686       Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
687     } else {
688       unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getZExtValue();
689       SDValue InVec = N->getOperand(0);
690       if (Idx >= NumElements) {
691         InVec = N->getOperand(1);
692         Idx -= NumElements;
693       }
694       Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, InVec,
695                                 DAG.getIntPtrConstant(Idx)));
696     }
697   }
698   Hi = DAG.getNode(ISD::BUILD_VECTOR, HiVT, &Ops[0], Ops.size());
699 }
700
701 void DAGTypeLegalizer::SplitVecRes_VSETCC(SDNode *N, SDValue &Lo,
702                                           SDValue &Hi) {
703   MVT LoVT, HiVT;
704   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
705
706   SDValue LL, LH, RL, RH;
707   GetSplitVector(N->getOperand(0), LL, LH);
708   GetSplitVector(N->getOperand(1), RL, RH);
709
710   Lo = DAG.getNode(ISD::VSETCC, LoVT, LL, RL, N->getOperand(2));
711   Hi = DAG.getNode(ISD::VSETCC, HiVT, LH, RH, N->getOperand(2));
712 }
713
714
715 //===----------------------------------------------------------------------===//
716 //  Operand Vector Splitting
717 //===----------------------------------------------------------------------===//
718
719 /// SplitVectorOperand - This method is called when the specified operand of the
720 /// specified node is found to need vector splitting.  At this point, all of the
721 /// result types of the node are known to be legal, but other operands of the
722 /// node may need legalization as well as the specified one.
723 bool DAGTypeLegalizer::SplitVectorOperand(SDNode *N, unsigned OpNo) {
724   DEBUG(cerr << "Split node operand: "; N->dump(&DAG); cerr << "\n");
725   SDValue Res = SDValue();
726
727   if (Res.getNode() == 0) {
728     switch (N->getOpcode()) {
729     default:
730 #ifndef NDEBUG
731       cerr << "SplitVectorOperand Op #" << OpNo << ": ";
732       N->dump(&DAG); cerr << "\n";
733 #endif
734       assert(0 && "Do not know how to split this operator's operand!");
735       abort();
736
737     case ISD::BIT_CONVERT:       Res = SplitVecOp_BIT_CONVERT(N); break;
738     case ISD::EXTRACT_SUBVECTOR: Res = SplitVecOp_EXTRACT_SUBVECTOR(N); break;
739     case ISD::EXTRACT_VECTOR_ELT:Res = SplitVecOp_EXTRACT_VECTOR_ELT(N); break;
740     case ISD::STORE:             Res = SplitVecOp_STORE(cast<StoreSDNode>(N),
741                                                         OpNo); break;
742     case ISD::VECTOR_SHUFFLE:    Res = SplitVecOp_VECTOR_SHUFFLE(N, OpNo);break;
743
744     case ISD::CTTZ:
745     case ISD::CTLZ:
746     case ISD::CTPOP:
747     case ISD::FP_TO_SINT:
748     case ISD::FP_TO_UINT:
749     case ISD::SINT_TO_FP:
750     case ISD::TRUNCATE:
751     case ISD::UINT_TO_FP: Res = SplitVecOp_UnaryOp(N); break;
752     }
753   }
754
755   // If the result is null, the sub-method took care of registering results etc.
756   if (!Res.getNode()) return false;
757
758   // If the result is N, the sub-method updated N in place.  Check to see if any
759   // operands are new, and if so, mark them.
760   if (Res.getNode() == N) {
761     // Mark N as new and remark N and its operands.  This allows us to correctly
762     // revisit N if it needs another step of promotion and allows us to visit
763     // any new operands to N.
764     ReanalyzeNode(N);
765     return true;
766   }
767
768   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
769          "Invalid operand expansion");
770
771   ReplaceValueWith(SDValue(N, 0), Res);
772   return false;
773 }
774
775 SDValue DAGTypeLegalizer::SplitVecOp_UnaryOp(SDNode *N) {
776   // The result has a legal vector type, but the input needs splitting.
777   MVT ResVT = N->getValueType(0);
778   SDValue Lo, Hi;
779   GetSplitVector(N->getOperand(0), Lo, Hi);
780   assert(Lo.getValueType() == Hi.getValueType() &&
781          "Returns legal non-power-of-two vector type?");
782   MVT InVT = Lo.getValueType();
783
784   MVT OutVT = MVT::getVectorVT(ResVT.getVectorElementType(),
785                                InVT.getVectorNumElements());
786
787   Lo = DAG.getNode(N->getOpcode(), OutVT, Lo);
788   Hi = DAG.getNode(N->getOpcode(), OutVT, Hi);
789
790   return DAG.getNode(ISD::CONCAT_VECTORS, ResVT, Lo, Hi);
791 }
792
793 SDValue DAGTypeLegalizer::SplitVecOp_BIT_CONVERT(SDNode *N) {
794   // For example, i64 = BIT_CONVERT v4i16 on alpha.  Typically the vector will
795   // end up being split all the way down to individual components.  Convert the
796   // split pieces into integers and reassemble.
797   SDValue Lo, Hi;
798   GetSplitVector(N->getOperand(0), Lo, Hi);
799   Lo = BitConvertToInteger(Lo);
800   Hi = BitConvertToInteger(Hi);
801
802   if (TLI.isBigEndian())
803     std::swap(Lo, Hi);
804
805   return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
806                      JoinIntegers(Lo, Hi));
807 }
808
809 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_SUBVECTOR(SDNode *N) {
810   // We know that the extracted result type is legal.  For now, assume the index
811   // is a constant.
812   MVT SubVT = N->getValueType(0);
813   SDValue Idx = N->getOperand(1);
814   SDValue Lo, Hi;
815   GetSplitVector(N->getOperand(0), Lo, Hi);
816
817   uint64_t LoElts = Lo.getValueType().getVectorNumElements();
818   uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
819
820   if (IdxVal < LoElts) {
821     assert(IdxVal + SubVT.getVectorNumElements() <= LoElts &&
822            "Extracted subvector crosses vector split!");
823     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SubVT, Lo, Idx);
824   } else {
825     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SubVT, Hi,
826                        DAG.getConstant(IdxVal - LoElts, Idx.getValueType()));
827   }
828 }
829
830 SDValue DAGTypeLegalizer::SplitVecOp_EXTRACT_VECTOR_ELT(SDNode *N) {
831   SDValue Vec = N->getOperand(0);
832   SDValue Idx = N->getOperand(1);
833   MVT VecVT = Vec.getValueType();
834
835   if (isa<ConstantSDNode>(Idx)) {
836     uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
837     assert(IdxVal < VecVT.getVectorNumElements() && "Invalid vector index!");
838
839     SDValue Lo, Hi;
840     GetSplitVector(Vec, Lo, Hi);
841
842     uint64_t LoElts = Lo.getValueType().getVectorNumElements();
843
844     if (IdxVal < LoElts)
845       return DAG.UpdateNodeOperands(SDValue(N, 0), Lo, Idx);
846     else
847       return DAG.UpdateNodeOperands(SDValue(N, 0), Hi,
848                                     DAG.getConstant(IdxVal - LoElts,
849                                                     Idx.getValueType()));
850   }
851
852   // Store the vector to the stack.
853   MVT EltVT = VecVT.getVectorElementType();
854   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
855   SDValue Store = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
856
857   // Load back the required element.
858   StackPtr = GetVectorElementPointer(StackPtr, EltVT, Idx);
859   return DAG.getLoad(EltVT, Store, StackPtr, NULL, 0);
860 }
861
862 SDValue DAGTypeLegalizer::SplitVecOp_STORE(StoreSDNode *N, unsigned OpNo) {
863   assert(N->isUnindexed() && "Indexed store of vector?");
864   assert(OpNo == 1 && "Can only split the stored value");
865
866   bool isTruncating = N->isTruncatingStore();
867   SDValue Ch  = N->getChain();
868   SDValue Ptr = N->getBasePtr();
869   int SVOffset = N->getSrcValueOffset();
870   MVT MemoryVT = N->getMemoryVT();
871   unsigned Alignment = N->getAlignment();
872   bool isVol = N->isVolatile();
873   SDValue Lo, Hi;
874   GetSplitVector(N->getOperand(1), Lo, Hi);
875
876   MVT LoMemVT, HiMemVT;
877   GetSplitDestVTs(MemoryVT, LoMemVT, HiMemVT);
878
879   unsigned IncrementSize = LoMemVT.getSizeInBits()/8;
880
881   if (isTruncating)
882     Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
883                            LoMemVT, isVol, Alignment);
884   else
885     Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
886                       isVol, Alignment);
887
888   // Increment the pointer to the other half.
889   Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
890                     DAG.getIntPtrConstant(IncrementSize));
891
892   if (isTruncating)
893     Hi = DAG.getTruncStore(Ch, Hi, Ptr,
894                            N->getSrcValue(), SVOffset+IncrementSize,
895                            HiMemVT,
896                            isVol, MinAlign(Alignment, IncrementSize));
897   else
898     Hi = DAG.getStore(Ch, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
899                       isVol, MinAlign(Alignment, IncrementSize));
900
901   return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
902 }
903
904 SDValue DAGTypeLegalizer::SplitVecOp_VECTOR_SHUFFLE(SDNode *N, unsigned OpNo) {
905   assert(OpNo == 2 && "Shuffle source type differs from result type?");
906   SDValue Mask = N->getOperand(2);
907   unsigned MaskLength = Mask.getValueType().getVectorNumElements();
908   unsigned LargestMaskEntryPlusOne = 2 * MaskLength;
909   unsigned MinimumBitWidth = Log2_32_Ceil(LargestMaskEntryPlusOne);
910
911   // Look for a legal vector type to place the mask values in.
912   // Note that there may not be *any* legal vector-of-integer
913   // type for which the element type is legal!
914   for (MVT::SimpleValueType EltVT = MVT::FIRST_INTEGER_VALUETYPE;
915        EltVT <= MVT::LAST_INTEGER_VALUETYPE;
916        // Integer values types are consecutively numbered.  Exploit this.
917        EltVT = MVT::SimpleValueType(EltVT + 1)) {
918
919     // Is the element type big enough to hold the values?
920     if (MVT(EltVT).getSizeInBits() < MinimumBitWidth)
921       // Nope.
922       continue;
923
924     // Is the vector type legal?
925     MVT VecVT = MVT::getVectorVT(EltVT, MaskLength);
926     if (!isTypeLegal(VecVT))
927       // Nope.
928       continue;
929
930     // If the element type is not legal, find a larger legal type to use for
931     // the BUILD_VECTOR operands.  This is an ugly hack, but seems to work!
932     // FIXME: The real solution is to change VECTOR_SHUFFLE into a variadic
933     // node where the shuffle mask is a list of integer operands, #2 .. #2+n.
934     for (MVT::SimpleValueType OpVT = EltVT; OpVT <= MVT::LAST_INTEGER_VALUETYPE;
935          // Integer values types are consecutively numbered.  Exploit this.
936          OpVT = MVT::SimpleValueType(OpVT + 1)) {
937       if (!isTypeLegal(OpVT))
938         continue;
939
940       // Success!  Rebuild the vector using the legal types.
941       SmallVector<SDValue, 16> Ops(MaskLength);
942       for (unsigned i = 0; i < MaskLength; ++i) {
943         SDValue Arg = Mask.getOperand(i);
944         if (Arg.getOpcode() == ISD::UNDEF) {
945           Ops[i] = DAG.getNode(ISD::UNDEF, OpVT);
946         } else {
947           uint64_t Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
948           Ops[i] = DAG.getConstant(Idx, OpVT);
949         }
950       }
951       return DAG.UpdateNodeOperands(SDValue(N,0),
952                                     N->getOperand(0), N->getOperand(1),
953                                     DAG.getNode(ISD::BUILD_VECTOR,
954                                                 VecVT, &Ops[0], Ops.size()));
955     }
956
957     // Continuing is pointless - failure is certain.
958     break;
959   }
960   assert(false && "Failed to find an appropriate mask type!");
961   return SDValue(N, 0);
962 }