Simplify using getIntPtrConstant.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeTypesSplit.cpp
1 //===-- LegalizeTypesSplit.cpp - Vector Splitting for LegalizeTypes -------===//
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 implements vector splitting support for LegalizeTypes.  Vector
11 // splitting is the act of changing a computation in an invalid vector type to
12 // be a computation in multiple vectors of a smaller type.  For example,
13 // implementing <128 x f32> operations in terms of two <64 x f32> operations.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "LegalizeTypes.h"
18 using namespace llvm;
19
20 /// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a vector
21 /// type that needs to be split.  This handles non-power of two vectors.
22 static void GetSplitDestVTs(MVT::ValueType InVT,
23                             MVT::ValueType &Lo, MVT::ValueType &Hi) {
24   MVT::ValueType NewEltVT = MVT::getVectorElementType(InVT);
25   unsigned NumElements = MVT::getVectorNumElements(InVT);
26   if ((NumElements & (NumElements-1)) == 0) {  // Simple power of two vector.
27     NumElements >>= 1;
28     Lo = Hi =  MVT::getVectorType(NewEltVT, NumElements);
29   } else {                                     // Non-power-of-two vectors.
30     unsigned NewNumElts_Lo = 1 << Log2_32(NumElements);
31     unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
32     Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
33     Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
34   }
35 }
36
37
38 //===----------------------------------------------------------------------===//
39 //  Result Vector Splitting
40 //===----------------------------------------------------------------------===//
41
42 /// SplitResult - This method is called when the specified result of the
43 /// specified node is found to need vector splitting.  At this point, the node
44 /// may also have invalid operands or may have other results that need
45 /// legalization, we just know that (at least) one result needs vector
46 /// splitting.
47 void DAGTypeLegalizer::SplitResult(SDNode *N, unsigned ResNo) {
48   DEBUG(cerr << "Split node result: "; N->dump(&DAG); cerr << "\n");
49   SDOperand Lo, Hi;
50   
51 #if 0
52   // See if the target wants to custom expand this node.
53   if (TLI.getOperationAction(N->getOpcode(), N->getValueType(0)) == 
54       TargetLowering::Custom) {
55     // If the target wants to, allow it to lower this itself.
56     if (SDNode *P = TLI.ExpandOperationResult(N, DAG)) {
57       // Everything that once used N now uses P.  We are guaranteed that the
58       // result value types of N and the result value types of P match.
59       ReplaceNodeWith(N, P);
60       return;
61     }
62   }
63 #endif
64   
65   switch (N->getOpcode()) {
66   default:
67 #ifndef NDEBUG
68     cerr << "SplitResult #" << ResNo << ": ";
69     N->dump(&DAG); cerr << "\n";
70 #endif
71     assert(0 && "Do not know how to split the result of this operator!");
72     abort();
73     
74   case ISD::UNDEF:            SplitRes_UNDEF(N, Lo, Hi); break;
75   case ISD::LOAD:             SplitRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
76   case ISD::BUILD_PAIR:       SplitRes_BUILD_PAIR(N, Lo, Hi); break;
77   case ISD::INSERT_VECTOR_ELT:SplitRes_INSERT_VECTOR_ELT(N, Lo, Hi); break;
78   case ISD::VECTOR_SHUFFLE:   SplitRes_VECTOR_SHUFFLE(N, Lo, Hi); break;
79   case ISD::BUILD_VECTOR:     SplitRes_BUILD_VECTOR(N, Lo, Hi); break;
80   case ISD::CONCAT_VECTORS:   SplitRes_CONCAT_VECTORS(N, Lo, Hi); break;
81   case ISD::BIT_CONVERT:      SplitRes_BIT_CONVERT(N, Lo, Hi); break;
82   case ISD::CTTZ:
83   case ISD::CTLZ:
84   case ISD::CTPOP:
85   case ISD::FNEG:
86   case ISD::FABS:
87   case ISD::FSQRT:
88   case ISD::FSIN:
89   case ISD::FCOS:
90   case ISD::FP_TO_SINT:
91   case ISD::FP_TO_UINT:
92   case ISD::SINT_TO_FP:
93   case ISD::UINT_TO_FP:       SplitRes_UnOp(N, Lo, Hi); break;
94   case ISD::ADD:
95   case ISD::SUB:
96   case ISD::MUL:
97   case ISD::FADD:
98   case ISD::FSUB:
99   case ISD::FMUL:
100   case ISD::SDIV:
101   case ISD::UDIV:
102   case ISD::FDIV:
103   case ISD::FPOW:
104   case ISD::AND:
105   case ISD::OR:
106   case ISD::XOR:
107   case ISD::UREM:
108   case ISD::SREM:
109   case ISD::FREM:             SplitRes_BinOp(N, Lo, Hi); break;
110   case ISD::FPOWI:            SplitRes_FPOWI(N, Lo, Hi); break;
111   case ISD::SELECT:           SplitRes_SELECT(N, Lo, Hi); break;
112   }
113   
114   // If Lo/Hi is null, the sub-method took care of registering results etc.
115   if (Lo.Val)
116     SetSplitOp(SDOperand(N, ResNo), Lo, Hi);
117 }
118
119 void DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
120   MVT::ValueType LoVT, HiVT;
121   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
122
123   Lo = DAG.getNode(ISD::UNDEF, LoVT);
124   Hi = DAG.getNode(ISD::UNDEF, HiVT);
125 }
126
127 void DAGTypeLegalizer::SplitRes_LOAD(LoadSDNode *LD, 
128                                      SDOperand &Lo, SDOperand &Hi) {
129   MVT::ValueType LoVT, HiVT;
130   GetSplitDestVTs(LD->getValueType(0), LoVT, HiVT);
131   
132   SDOperand Ch = LD->getChain();
133   SDOperand Ptr = LD->getBasePtr();
134   const Value *SV = LD->getSrcValue();
135   int SVOffset = LD->getSrcValueOffset();
136   unsigned Alignment = LD->getAlignment();
137   bool isVolatile = LD->isVolatile();
138   
139   Lo = DAG.getLoad(LoVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
140   unsigned IncrementSize = MVT::getSizeInBits(LoVT)/8;
141   Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
142                     DAG.getIntPtrConstant(IncrementSize));
143   SVOffset += IncrementSize;
144   Alignment = MinAlign(Alignment, IncrementSize);
145   Hi = DAG.getLoad(HiVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
146   
147   // Build a factor node to remember that this load is independent of the
148   // other one.
149   SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
150                              Hi.getValue(1));
151   
152   // Legalized the chain result - switch anything that used the old chain to
153   // use the new one.
154   ReplaceValueWith(SDOperand(LD, 1), TF);
155 }
156
157 void DAGTypeLegalizer::SplitRes_BUILD_PAIR(SDNode *N, SDOperand &Lo,
158                                            SDOperand &Hi) {
159   Lo = N->getOperand(0);
160   Hi = N->getOperand(1);
161 }
162
163 void DAGTypeLegalizer::SplitRes_INSERT_VECTOR_ELT(SDNode *N, SDOperand &Lo,
164                                                   SDOperand &Hi) {
165   GetSplitOp(N->getOperand(0), Lo, Hi);
166   unsigned Index = cast<ConstantSDNode>(N->getOperand(2))->getValue();
167   SDOperand ScalarOp = N->getOperand(1);
168   unsigned LoNumElts = MVT::getVectorNumElements(Lo.getValueType());
169   if (Index < LoNumElts)
170     Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, Lo.getValueType(), Lo, ScalarOp,
171                      N->getOperand(2));
172   else
173     Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, Hi.getValueType(), Hi, ScalarOp,
174                      DAG.getIntPtrConstant(Index - LoNumElts));
175 }
176
177 void DAGTypeLegalizer::SplitRes_VECTOR_SHUFFLE(SDNode *N, 
178                                                SDOperand &Lo, SDOperand &Hi) {
179   // Build the low part.
180   SDOperand Mask = N->getOperand(2);
181   SmallVector<SDOperand, 16> Ops;
182   MVT::ValueType LoVT, HiVT;
183   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
184   MVT::ValueType EltVT = MVT::getVectorElementType(LoVT);
185   unsigned LoNumElts = MVT::getVectorNumElements(LoVT);
186   unsigned NumElements = Mask.getNumOperands();
187
188   // Insert all of the elements from the input that are needed.  We use 
189   // buildvector of extractelement here because the input vectors will have
190   // to be legalized, so this makes the code simpler.
191   for (unsigned i = 0; i != LoNumElts; ++i) {
192     unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
193     SDOperand InVec = N->getOperand(0);
194     if (Idx >= NumElements) {
195       InVec = N->getOperand(1);
196       Idx -= NumElements;
197     }
198     Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, InVec,
199                               DAG.getIntPtrConstant(Idx)));
200   }
201   Lo = DAG.getNode(ISD::BUILD_VECTOR, LoVT, &Ops[0], Ops.size());
202   Ops.clear();
203   
204   for (unsigned i = LoNumElts; i != NumElements; ++i) {
205     unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
206     SDOperand InVec = N->getOperand(0);
207     if (Idx >= NumElements) {
208       InVec = N->getOperand(1);
209       Idx -= NumElements;
210     }
211     Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, InVec,
212                               DAG.getIntPtrConstant(Idx)));
213   }
214   Hi = DAG.getNode(ISD::BUILD_VECTOR, HiVT, &Ops[0], Ops.size());
215 }
216
217 void DAGTypeLegalizer::SplitRes_BUILD_VECTOR(SDNode *N, SDOperand &Lo, 
218                                              SDOperand &Hi) {
219   MVT::ValueType LoVT, HiVT;
220   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
221   unsigned LoNumElts = MVT::getVectorNumElements(LoVT);
222   SmallVector<SDOperand, 8> LoOps(N->op_begin(), N->op_begin()+LoNumElts);
223   Lo = DAG.getNode(ISD::BUILD_VECTOR, LoVT, &LoOps[0], LoOps.size());
224   
225   SmallVector<SDOperand, 8> HiOps(N->op_begin()+LoNumElts, N->op_end());
226   Hi = DAG.getNode(ISD::BUILD_VECTOR, HiVT, &HiOps[0], HiOps.size());
227 }
228
229 void DAGTypeLegalizer::SplitRes_CONCAT_VECTORS(SDNode *N, 
230                                                SDOperand &Lo, SDOperand &Hi) {
231   // FIXME: Handle non-power-of-two vectors?
232   unsigned NumSubvectors = N->getNumOperands() / 2;
233   if (NumSubvectors == 1) {
234     Lo = N->getOperand(0);
235     Hi = N->getOperand(1);
236     return;
237   }
238
239   MVT::ValueType LoVT, HiVT;
240   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
241
242   SmallVector<SDOperand, 8> LoOps(N->op_begin(), N->op_begin()+NumSubvectors);
243   Lo = DAG.getNode(ISD::CONCAT_VECTORS, LoVT, &LoOps[0], LoOps.size());
244     
245   SmallVector<SDOperand, 8> HiOps(N->op_begin()+NumSubvectors, N->op_end());
246   Hi = DAG.getNode(ISD::CONCAT_VECTORS, HiVT, &HiOps[0], HiOps.size());
247 }
248
249 void DAGTypeLegalizer::SplitRes_BIT_CONVERT(SDNode *N, 
250                                             SDOperand &Lo, SDOperand &Hi) {
251   // We know the result is a vector.  The input may be either a vector or a
252   // scalar value.
253   MVT::ValueType LoVT, HiVT;
254   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
255
256   SDOperand InOp = N->getOperand(0);
257   MVT::ValueType InVT = InOp.getValueType();
258
259   // Handle some special cases efficiently.
260   switch (getTypeAction(InVT)) {
261   default:
262     assert(false && "Unknown type action!");
263   case Legal:
264   case FloatToInt:
265   case Promote:
266   case Scalarize:
267     break;
268   case Expand:
269     // A scalar to vector conversion, where the scalar needs expansion.
270     // If the vector is being split in two then we can just convert the
271     // expanded pieces.
272     if (LoVT == HiVT) {
273       GetExpandedOp(InOp, Lo, Hi);
274       if (TLI.isBigEndian())
275         std::swap(Lo, Hi);
276       Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
277       Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
278       return;
279     }
280     break;
281   case Split:
282     // If the input is a vector that needs to be split, convert each split
283     // piece of the input now.
284     GetSplitOp(InOp, Lo, Hi);
285     Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
286     Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
287     return;
288   }
289
290   // In the general case, convert the input to an integer and split it by hand.
291   MVT::ValueType LoIntVT = MVT::getIntegerType(MVT::getSizeInBits(LoVT));
292   MVT::ValueType HiIntVT = MVT::getIntegerType(MVT::getSizeInBits(HiVT));
293   if (TLI.isBigEndian())
294     std::swap(LoIntVT, HiIntVT);
295
296   SplitInteger(BitConvertToInteger(InOp), LoIntVT, HiIntVT, Lo, Hi);
297
298   if (TLI.isBigEndian())
299     std::swap(Lo, Hi);
300   Lo = DAG.getNode(ISD::BIT_CONVERT, LoVT, Lo);
301   Hi = DAG.getNode(ISD::BIT_CONVERT, HiVT, Hi);
302 }
303
304 void DAGTypeLegalizer::SplitRes_BinOp(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
305   SDOperand LHSLo, LHSHi;
306   GetSplitOp(N->getOperand(0), LHSLo, LHSHi);
307   SDOperand RHSLo, RHSHi;
308   GetSplitOp(N->getOperand(1), RHSLo, RHSHi);
309   
310   Lo = DAG.getNode(N->getOpcode(), LHSLo.getValueType(), LHSLo, RHSLo);
311   Hi = DAG.getNode(N->getOpcode(), LHSHi.getValueType(), LHSHi, RHSHi);
312 }
313
314 void DAGTypeLegalizer::SplitRes_UnOp(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
315   // Get the dest types.  This doesn't always match input types, e.g. int_to_fp.
316   MVT::ValueType LoVT, HiVT;
317   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
318
319   GetSplitOp(N->getOperand(0), Lo, Hi);
320   Lo = DAG.getNode(N->getOpcode(), LoVT, Lo);
321   Hi = DAG.getNode(N->getOpcode(), HiVT, Hi);
322 }
323
324 void DAGTypeLegalizer::SplitRes_FPOWI(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
325   GetSplitOp(N->getOperand(0), Lo, Hi);
326   Lo = DAG.getNode(ISD::FPOWI, Lo.getValueType(), Lo, N->getOperand(1));
327   Hi = DAG.getNode(ISD::FPOWI, Lo.getValueType(), Hi, N->getOperand(1));
328 }
329
330
331 void DAGTypeLegalizer::SplitRes_SELECT(SDNode *N, SDOperand &Lo, SDOperand &Hi){
332   SDOperand LL, LH, RL, RH;
333   GetSplitOp(N->getOperand(1), LL, LH);
334   GetSplitOp(N->getOperand(2), RL, RH);
335   
336   SDOperand Cond = N->getOperand(0);
337   Lo = DAG.getNode(ISD::SELECT, LL.getValueType(), Cond, LL, RL);
338   Hi = DAG.getNode(ISD::SELECT, LH.getValueType(), Cond, LH, RH);
339 }
340
341
342 //===----------------------------------------------------------------------===//
343 //  Operand Vector Splitting
344 //===----------------------------------------------------------------------===//
345
346 /// SplitOperand - This method is called when the specified operand of the
347 /// specified node is found to need vector splitting.  At this point, all of the
348 /// result types of the node are known to be legal, but other operands of the
349 /// node may need legalization as well as the specified one.
350 bool DAGTypeLegalizer::SplitOperand(SDNode *N, unsigned OpNo) {
351   DEBUG(cerr << "Split node operand: "; N->dump(&DAG); cerr << "\n");
352   SDOperand Res(0, 0);
353   
354 #if 0
355   if (TLI.getOperationAction(N->getOpcode(), N->getValueType(0)) == 
356       TargetLowering::Custom)
357     Res = TLI.LowerOperation(SDOperand(N, 0), DAG);
358 #endif
359   
360   if (Res.Val == 0) {
361     switch (N->getOpcode()) {
362     default:
363 #ifndef NDEBUG
364       cerr << "SplitOperand Op #" << OpNo << ": ";
365       N->dump(&DAG); cerr << "\n";
366 #endif
367       assert(0 && "Do not know how to split this operator's operand!");
368       abort();
369     case ISD::STORE: Res = SplitOp_STORE(cast<StoreSDNode>(N), OpNo); break;
370     case ISD::RET:   Res = SplitOp_RET(N, OpNo); break;
371
372     case ISD::BIT_CONVERT: Res = SplitOp_BIT_CONVERT(N); break;
373
374     case ISD::EXTRACT_VECTOR_ELT: Res = SplitOp_EXTRACT_VECTOR_ELT(N); break;
375     case ISD::EXTRACT_SUBVECTOR:  Res = SplitOp_EXTRACT_SUBVECTOR(N); break;
376     case ISD::VECTOR_SHUFFLE:     Res = SplitOp_VECTOR_SHUFFLE(N, OpNo); break;
377     }
378   }
379   
380   // If the result is null, the sub-method took care of registering results etc.
381   if (!Res.Val) return false;
382   
383   // If the result is N, the sub-method updated N in place.  Check to see if any
384   // operands are new, and if so, mark them.
385   if (Res.Val == N) {
386     // Mark N as new and remark N and its operands.  This allows us to correctly
387     // revisit N if it needs another step of promotion and allows us to visit
388     // any new operands to N.
389     ReanalyzeNode(N);
390     return true;
391   }
392
393   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
394          "Invalid operand expansion");
395   
396   ReplaceValueWith(SDOperand(N, 0), Res);
397   return false;
398 }
399
400 SDOperand DAGTypeLegalizer::SplitOp_STORE(StoreSDNode *N, unsigned OpNo) {
401   assert(OpNo == 1 && "Can only split the stored value");
402   
403   SDOperand Ch  = N->getChain();
404   SDOperand Ptr = N->getBasePtr();
405   int SVOffset = N->getSrcValueOffset();
406   unsigned Alignment = N->getAlignment();
407   bool isVol = N->isVolatile();
408   SDOperand Lo, Hi;
409   GetSplitOp(N->getOperand(1), Lo, Hi);
410
411   unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
412
413   Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset, isVol, Alignment);
414   
415   // Increment the pointer to the other half.
416   Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
417                     DAG.getIntPtrConstant(IncrementSize));
418   
419   Hi = DAG.getStore(Ch, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
420                     isVol, MinAlign(Alignment, IncrementSize));
421   return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
422 }
423
424 SDOperand DAGTypeLegalizer::SplitOp_RET(SDNode *N, unsigned OpNo) {
425   assert(N->getNumOperands() == 3 &&"Can only handle ret of one vector so far");
426   // FIXME: Returns of gcc generic vectors larger than a legal vector
427   // type should be returned by reference!
428   SDOperand Lo, Hi;
429   GetSplitOp(N->getOperand(1), Lo, Hi);
430
431   SDOperand Chain = N->getOperand(0);  // The chain.
432   SDOperand Sign = N->getOperand(2);  // Signness
433   
434   return DAG.getNode(ISD::RET, MVT::Other, Chain, Lo, Sign, Hi, Sign);
435 }
436
437 SDOperand DAGTypeLegalizer::SplitOp_BIT_CONVERT(SDNode *N) {
438   // For example, i64 = BIT_CONVERT v4i16 on alpha.  Typically the vector will
439   // end up being split all the way down to individual components.  Convert the
440   // split pieces into integers and reassemble.
441   SDOperand Lo, Hi;
442   GetSplitOp(N->getOperand(0), Lo, Hi);
443   Lo = BitConvertToInteger(Lo);
444   Hi = BitConvertToInteger(Hi);
445
446   if (TLI.isBigEndian())
447     std::swap(Lo, Hi);
448
449   return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
450                      JoinIntegers(Lo, Hi));
451 }
452
453 SDOperand DAGTypeLegalizer::SplitOp_EXTRACT_VECTOR_ELT(SDNode *N) {
454   SDOperand Vec = N->getOperand(0);
455   SDOperand Idx = N->getOperand(1);
456   MVT::ValueType VecVT = Vec.getValueType();
457
458   if (isa<ConstantSDNode>(Idx)) {
459     uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getValue();
460     assert(IdxVal < MVT::getVectorNumElements(VecVT) &&
461            "Invalid vector index!");
462
463     SDOperand Lo, Hi;
464     GetSplitOp(Vec, Lo, Hi);
465
466     uint64_t LoElts = MVT::getVectorNumElements(Lo.getValueType());
467
468     if (IdxVal < LoElts)
469       return DAG.UpdateNodeOperands(SDOperand(N, 0), Lo, Idx);
470     else
471       return DAG.UpdateNodeOperands(SDOperand(N, 0), Hi,
472                                     DAG.getConstant(IdxVal - LoElts,
473                                                     Idx.getValueType()));
474   }
475
476   // Store the vector to the stack and load back the required element.
477   SDOperand StackPtr = DAG.CreateStackTemporary(VecVT);
478   SDOperand Store = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
479
480   // Add the offset to the index.
481   MVT::ValueType EltVT = MVT::getVectorElementType(VecVT);
482   unsigned EltSize = MVT::getSizeInBits(EltVT)/8; // FIXME: should be ABI size.
483   Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
484                     DAG.getConstant(EltSize, Idx.getValueType()));
485
486   if (MVT::getSizeInBits(Idx.getValueType()) >
487       MVT::getSizeInBits(TLI.getPointerTy()))
488     Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
489   else
490     Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
491
492   StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
493   return DAG.getLoad(EltVT, Store, StackPtr, NULL, 0);
494 }
495
496 SDOperand DAGTypeLegalizer::SplitOp_EXTRACT_SUBVECTOR(SDNode *N) {
497   // We know that the extracted result type is legal.  For now, assume the index
498   // is a constant.
499   MVT::ValueType SubVT = N->getValueType(0);
500   SDOperand Idx = N->getOperand(1);
501   SDOperand Lo, Hi;
502   GetSplitOp(N->getOperand(0), Lo, Hi);
503
504   uint64_t LoElts = MVT::getVectorNumElements(Lo.getValueType());
505   uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getValue();
506
507   if (IdxVal < LoElts) {
508     assert(IdxVal + MVT::getVectorNumElements(SubVT) <= LoElts &&
509            "Extracted subvector crosses vector split!");
510     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SubVT, Lo, Idx);
511   } else {
512     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SubVT, Hi,
513                        DAG.getConstant(IdxVal - LoElts, Idx.getValueType()));
514   }
515 }
516
517 SDOperand DAGTypeLegalizer::SplitOp_VECTOR_SHUFFLE(SDNode *N, unsigned OpNo) {
518   assert(OpNo == 2 && "Shuffle source type differs from result type?");
519   SDOperand Mask = N->getOperand(2);
520   unsigned MaskLength = MVT::getVectorNumElements(Mask.getValueType());
521   unsigned LargestMaskEntryPlusOne = 2 * MaskLength;
522   unsigned MinimumBitWidth = Log2_32_Ceil(LargestMaskEntryPlusOne);
523
524   // Look for a legal vector type to place the mask values in.
525   // Note that there may not be *any* legal vector-of-integer
526   // type for which the element type is legal!
527   for (MVT::SimpleValueType EltVT = MVT::FIRST_INTEGER_VALUETYPE;
528        EltVT <= MVT::LAST_INTEGER_VALUETYPE;
529        // Integer values types are consecutively numbered.  Exploit this.
530        EltVT = MVT::SimpleValueType(EltVT + 1)) {
531
532     // Is the element type big enough to hold the values?
533     if (MVT::getSizeInBits(EltVT) < MinimumBitWidth)
534       // Nope.
535       continue;
536
537     // Is the vector type legal?
538     MVT::ValueType VecVT = MVT::getVectorType(EltVT, MaskLength);
539     if (!isTypeLegal(VecVT))
540       // Nope.
541       continue;
542
543     // If the element type is not legal, find a larger legal type to use for
544     // the BUILD_VECTOR operands.  This is an ugly hack, but seems to work!
545     // FIXME: The real solution is to change VECTOR_SHUFFLE into a variadic
546     // node where the shuffle mask is a list of integer operands, #2 .. #2+n.
547     for (MVT::SimpleValueType OpVT = EltVT; OpVT <= MVT::LAST_INTEGER_VALUETYPE;
548          // Integer values types are consecutively numbered.  Exploit this.
549          OpVT = MVT::SimpleValueType(OpVT + 1)) {
550       if (!isTypeLegal(OpVT))
551         continue;
552
553       // Success!  Rebuild the vector using the legal types.
554       SmallVector<SDOperand, 16> Ops(MaskLength);
555       for (unsigned i = 0; i < MaskLength; ++i) {
556         uint64_t Idx =
557           cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
558         Ops[i] = DAG.getConstant(Idx, OpVT);
559       }
560       return DAG.UpdateNodeOperands(SDOperand(N,0),
561                                     N->getOperand(0), N->getOperand(1),
562                                     DAG.getNode(ISD::BUILD_VECTOR,
563                                                 VecVT, &Ops[0], Ops.size()));
564     }
565
566     // Continuing is pointless - failure is certain.
567     break;
568   }
569   assert(false && "Failed to find an appropriate mask type!");
570   return SDOperand(N, 0);
571 }