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