When splitting a VAARG, remember its alignment.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeTypesGeneric.cpp
1 //===-------- LegalizeTypesGeneric.cpp - Generic type legalization --------===//
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 generic type expansion and splitting for LegalizeTypes.
11 // The routines here perform legalization when the details of the type (such as
12 // whether it is an integer or a float) do not matter.
13 // Expansion is the act of changing a computation in an illegal type to be a
14 // computation in two identical registers of a smaller type.  The Lo/Hi part
15 // is required to be stored first in memory on little/big-endian machines.
16 // Splitting is the act of changing a computation in an illegal type to be a
17 // computation in two not necessarily identical registers of a smaller type.
18 // There are no requirements on how the type is represented in memory.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "LegalizeTypes.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/CodeGen/PseudoSourceValue.h"
25 using namespace llvm;
26
27 //===----------------------------------------------------------------------===//
28 // Generic Result Expansion.
29 //===----------------------------------------------------------------------===//
30
31 // These routines assume that the Lo/Hi part is stored first in memory on
32 // little/big-endian machines, followed by the Hi/Lo part.  This means that
33 // they cannot be used as is on vectors, for which Lo is always stored first.
34
35 void DAGTypeLegalizer::ExpandRes_BIT_CONVERT(SDNode *N, SDValue &Lo,
36                                              SDValue &Hi) {
37   EVT OutVT = N->getValueType(0);
38   EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
39   SDValue InOp = N->getOperand(0);
40   EVT InVT = InOp.getValueType();
41   DebugLoc dl = N->getDebugLoc();
42
43   // Handle some special cases efficiently.
44   switch (getTypeAction(InVT)) {
45     default:
46       assert(false && "Unknown type action!");
47     case Legal:
48     case PromoteInteger:
49       break;
50     case SoftenFloat:
51       // Convert the integer operand instead.
52       SplitInteger(GetSoftenedFloat(InOp), Lo, Hi);
53       Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo);
54       Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi);
55       return;
56     case ExpandInteger:
57     case ExpandFloat:
58       // Convert the expanded pieces of the input.
59       GetExpandedOp(InOp, Lo, Hi);
60       Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo);
61       Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi);
62       return;
63     case SplitVector:
64       GetSplitVector(InOp, Lo, Hi);
65       if (TLI.isBigEndian())
66         std::swap(Lo, Hi);
67       Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo);
68       Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi);
69       return;
70     case ScalarizeVector:
71       // Convert the element instead.
72       SplitInteger(BitConvertToInteger(GetScalarizedVector(InOp)), Lo, Hi);
73       Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo);
74       Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi);
75       return;
76     case WidenVector: {
77       assert(!(InVT.getVectorNumElements() & 1) && "Unsupported BIT_CONVERT");
78       InOp = GetWidenedVector(InOp);
79       EVT InNVT = EVT::getVectorVT(*DAG.getContext(), InVT.getVectorElementType(),
80                                    InVT.getVectorNumElements()/2);
81       Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InNVT, InOp,
82                        DAG.getIntPtrConstant(0));
83       Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InNVT, InOp,
84                        DAG.getIntPtrConstant(InNVT.getVectorNumElements()));
85       if (TLI.isBigEndian())
86         std::swap(Lo, Hi);
87       Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo);
88       Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi);
89       return;
90     }
91   }
92
93   if (InVT.isVector() && OutVT.isInteger()) {
94     // Handle cases like i64 = BIT_CONVERT v1i64 on x86, where the operand
95     // is legal but the result is not.
96     EVT NVT = EVT::getVectorVT(*DAG.getContext(), NOutVT, 2);
97
98     if (isTypeLegal(NVT)) {
99       SDValue CastInOp = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, InOp);
100       Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NOutVT, CastInOp,
101                        DAG.getIntPtrConstant(0));
102       Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NOutVT, CastInOp,
103                        DAG.getIntPtrConstant(1));
104
105       if (TLI.isBigEndian())
106         std::swap(Lo, Hi);
107
108       return;
109     }
110   }
111
112   // Lower the bit-convert to a store/load from the stack.
113   assert(NOutVT.isByteSized() && "Expanded type not byte sized!");
114
115   // Create the stack frame object.  Make sure it is aligned for both
116   // the source and expanded destination types.
117   unsigned Alignment =
118     TLI.getTargetData()->getPrefTypeAlignment(NOutVT.
119                                               getTypeForEVT(*DAG.getContext()));
120   SDValue StackPtr = DAG.CreateStackTemporary(InVT, Alignment);
121   int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
122   const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
123
124   // Emit a store to the stack slot.
125   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, InOp, StackPtr, SV, 0,
126                                false, false, 0);
127
128   // Load the first half from the stack slot.
129   Lo = DAG.getLoad(NOutVT, dl, Store, StackPtr, SV, 0, false, false, 0);
130
131   // Increment the pointer to the other half.
132   unsigned IncrementSize = NOutVT.getSizeInBits() / 8;
133   StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
134                          DAG.getIntPtrConstant(IncrementSize));
135
136   // Load the second half from the stack slot.
137   Hi = DAG.getLoad(NOutVT, dl, Store, StackPtr, SV, IncrementSize, false,
138                    false, MinAlign(Alignment, IncrementSize));
139
140   // Handle endianness of the load.
141   if (TLI.isBigEndian())
142     std::swap(Lo, Hi);
143 }
144
145 void DAGTypeLegalizer::ExpandRes_BUILD_PAIR(SDNode *N, SDValue &Lo,
146                                             SDValue &Hi) {
147   // Return the operands.
148   Lo = N->getOperand(0);
149   Hi = N->getOperand(1);
150 }
151
152 void DAGTypeLegalizer::ExpandRes_EXTRACT_ELEMENT(SDNode *N, SDValue &Lo,
153                                                  SDValue &Hi) {
154   GetExpandedOp(N->getOperand(0), Lo, Hi);
155   SDValue Part = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ?
156                    Hi : Lo;
157
158   assert(Part.getValueType() == N->getValueType(0) &&
159          "Type twice as big as expanded type not itself expanded!");
160
161   GetPairElements(Part, Lo, Hi);
162 }
163
164 void DAGTypeLegalizer::ExpandRes_EXTRACT_VECTOR_ELT(SDNode *N, SDValue &Lo,
165                                                     SDValue &Hi) {
166   SDValue OldVec = N->getOperand(0);
167   unsigned OldElts = OldVec.getValueType().getVectorNumElements();
168   DebugLoc dl = N->getDebugLoc();
169
170   // Convert to a vector of the expanded element type, for example
171   // <3 x i64> -> <6 x i32>.
172   EVT OldVT = N->getValueType(0);
173   EVT NewVT = TLI.getTypeToTransformTo(*DAG.getContext(), OldVT);
174
175   SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT, dl,
176                                EVT::getVectorVT(*DAG.getContext(),
177                                                 NewVT, 2*OldElts),
178                                OldVec);
179
180   // Extract the elements at 2 * Idx and 2 * Idx + 1 from the new vector.
181   SDValue Idx = N->getOperand(1);
182
183   // Make sure the type of Idx is big enough to hold the new values.
184   if (Idx.getValueType().bitsLT(TLI.getPointerTy()))
185     Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
186
187   Idx = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, Idx);
188   Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, NewVec, Idx);
189
190   Idx = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
191                     DAG.getConstant(1, Idx.getValueType()));
192   Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, NewVec, Idx);
193
194   if (TLI.isBigEndian())
195     std::swap(Lo, Hi);
196 }
197
198 void DAGTypeLegalizer::ExpandRes_NormalLoad(SDNode *N, SDValue &Lo,
199                                             SDValue &Hi) {
200   assert(ISD::isNormalLoad(N) && "This routine only for normal loads!");
201   DebugLoc dl = N->getDebugLoc();
202
203   LoadSDNode *LD = cast<LoadSDNode>(N);
204   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
205   SDValue Chain = LD->getChain();
206   SDValue Ptr = LD->getBasePtr();
207   int SVOffset = LD->getSrcValueOffset();
208   unsigned Alignment = LD->getAlignment();
209   bool isVolatile = LD->isVolatile();
210   bool isNonTemporal = LD->isNonTemporal();
211
212   assert(NVT.isByteSized() && "Expanded type not byte sized!");
213
214   Lo = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getSrcValue(), SVOffset,
215                    isVolatile, isNonTemporal, Alignment);
216
217   // Increment the pointer to the other half.
218   unsigned IncrementSize = NVT.getSizeInBits() / 8;
219   Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
220                     DAG.getIntPtrConstant(IncrementSize));
221   Hi = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getSrcValue(),
222                    SVOffset+IncrementSize,
223                    isVolatile, isNonTemporal,
224                    MinAlign(Alignment, IncrementSize));
225
226   // Build a factor node to remember that this load is independent of the
227   // other one.
228   Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
229                       Hi.getValue(1));
230
231   // Handle endianness of the load.
232   if (TLI.isBigEndian())
233     std::swap(Lo, Hi);
234
235   // Modified the chain - switch anything that used the old chain to use
236   // the new one.
237   ReplaceValueWith(SDValue(N, 1), Chain);
238 }
239
240 void DAGTypeLegalizer::ExpandRes_VAARG(SDNode *N, SDValue &Lo, SDValue &Hi) {
241   EVT OVT = N->getValueType(0);
242   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
243   SDValue Chain = N->getOperand(0);
244   SDValue Ptr = N->getOperand(1);
245   DebugLoc dl = N->getDebugLoc();
246   const unsigned OldAlign = N->getConstantOperandVal(3);
247   const Type *Type = OVT.getTypeForEVT(*DAG.getContext());
248   const unsigned TypeAlign = TLI.getTargetData()->getABITypeAlignment(Type);
249   const unsigned Align = std::max(OldAlign, TypeAlign);
250
251   Lo = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2), Align);
252   Hi = DAG.getVAArg(NVT, dl, Lo.getValue(1), Ptr, N->getOperand(2));
253
254   // Handle endianness of the load.
255   if (TLI.isBigEndian())
256     std::swap(Lo, Hi);
257
258   // Modified the chain - switch anything that used the old chain to use
259   // the new one.
260   ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
261 }
262
263
264 //===--------------------------------------------------------------------===//
265 // Generic Operand Expansion.
266 //===--------------------------------------------------------------------===//
267
268 SDValue DAGTypeLegalizer::ExpandOp_BIT_CONVERT(SDNode *N) {
269   DebugLoc dl = N->getDebugLoc();
270   if (N->getValueType(0).isVector()) {
271     // An illegal expanding type is being converted to a legal vector type.
272     // Make a two element vector out of the expanded parts and convert that
273     // instead, but only if the new vector type is legal (otherwise there
274     // is no point, and it might create expansion loops).  For example, on
275     // x86 this turns v1i64 = BIT_CONVERT i64 into v1i64 = BIT_CONVERT v2i32.
276     EVT OVT = N->getOperand(0).getValueType();
277     EVT NVT = EVT::getVectorVT(*DAG.getContext(),
278                                TLI.getTypeToTransformTo(*DAG.getContext(), OVT),
279                                2);
280
281     if (isTypeLegal(NVT)) {
282       SDValue Parts[2];
283       GetExpandedOp(N->getOperand(0), Parts[0], Parts[1]);
284
285       if (TLI.isBigEndian())
286         std::swap(Parts[0], Parts[1]);
287
288       SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, Parts, 2);
289       return DAG.getNode(ISD::BIT_CONVERT, dl, N->getValueType(0), Vec);
290     }
291   }
292
293   // Otherwise, store to a temporary and load out again as the new type.
294   return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
295 }
296
297 SDValue DAGTypeLegalizer::ExpandOp_BUILD_VECTOR(SDNode *N) {
298   // The vector type is legal but the element type needs expansion.
299   EVT VecVT = N->getValueType(0);
300   unsigned NumElts = VecVT.getVectorNumElements();
301   EVT OldVT = N->getOperand(0).getValueType();
302   EVT NewVT = TLI.getTypeToTransformTo(*DAG.getContext(), OldVT);
303   DebugLoc dl = N->getDebugLoc();
304
305   assert(OldVT == VecVT.getVectorElementType() &&
306          "BUILD_VECTOR operand type doesn't match vector element type!");
307
308   // Build a vector of twice the length out of the expanded elements.
309   // For example <3 x i64> -> <6 x i32>.
310   std::vector<SDValue> NewElts;
311   NewElts.reserve(NumElts*2);
312
313   for (unsigned i = 0; i < NumElts; ++i) {
314     SDValue Lo, Hi;
315     GetExpandedOp(N->getOperand(i), Lo, Hi);
316     if (TLI.isBigEndian())
317       std::swap(Lo, Hi);
318     NewElts.push_back(Lo);
319     NewElts.push_back(Hi);
320   }
321
322   SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, dl,
323                                EVT::getVectorVT(*DAG.getContext(),
324                                                 NewVT, NewElts.size()),
325                                &NewElts[0], NewElts.size());
326
327   // Convert the new vector to the old vector type.
328   return DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, NewVec);
329 }
330
331 SDValue DAGTypeLegalizer::ExpandOp_EXTRACT_ELEMENT(SDNode *N) {
332   SDValue Lo, Hi;
333   GetExpandedOp(N->getOperand(0), Lo, Hi);
334   return cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? Hi : Lo;
335 }
336
337 SDValue DAGTypeLegalizer::ExpandOp_INSERT_VECTOR_ELT(SDNode *N) {
338   // The vector type is legal but the element type needs expansion.
339   EVT VecVT = N->getValueType(0);
340   unsigned NumElts = VecVT.getVectorNumElements();
341   DebugLoc dl = N->getDebugLoc();
342
343   SDValue Val = N->getOperand(1);
344   EVT OldEVT = Val.getValueType();
345   EVT NewEVT = TLI.getTypeToTransformTo(*DAG.getContext(), OldEVT);
346
347   assert(OldEVT == VecVT.getVectorElementType() &&
348          "Inserted element type doesn't match vector element type!");
349
350   // Bitconvert to a vector of twice the length with elements of the expanded
351   // type, insert the expanded vector elements, and then convert back.
352   EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewEVT, NumElts*2);
353   SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT, dl,
354                                NewVecVT, N->getOperand(0));
355
356   SDValue Lo, Hi;
357   GetExpandedOp(Val, Lo, Hi);
358   if (TLI.isBigEndian())
359     std::swap(Lo, Hi);
360
361   SDValue Idx = N->getOperand(2);
362   Idx = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, Idx);
363   NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVecVT, NewVec, Lo, Idx);
364   Idx = DAG.getNode(ISD::ADD, dl,
365                     Idx.getValueType(), Idx, DAG.getIntPtrConstant(1));
366   NewVec =  DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVecVT, NewVec, Hi, Idx);
367
368   // Convert the new vector to the old vector type.
369   return DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, NewVec);
370 }
371
372 SDValue DAGTypeLegalizer::ExpandOp_SCALAR_TO_VECTOR(SDNode *N) {
373   DebugLoc dl = N->getDebugLoc();
374   EVT VT = N->getValueType(0);
375   assert(VT.getVectorElementType() == N->getOperand(0).getValueType() &&
376          "SCALAR_TO_VECTOR operand type doesn't match vector element type!");
377   unsigned NumElts = VT.getVectorNumElements();
378   SmallVector<SDValue, 16> Ops(NumElts);
379   Ops[0] = N->getOperand(0);
380   SDValue UndefVal = DAG.getUNDEF(Ops[0].getValueType());
381   for (unsigned i = 1; i < NumElts; ++i)
382     Ops[i] = UndefVal;
383   return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElts);
384 }
385
386 SDValue DAGTypeLegalizer::ExpandOp_NormalStore(SDNode *N, unsigned OpNo) {
387   assert(ISD::isNormalStore(N) && "This routine only for normal stores!");
388   assert(OpNo == 1 && "Can only expand the stored value so far");
389   DebugLoc dl = N->getDebugLoc();
390
391   StoreSDNode *St = cast<StoreSDNode>(N);
392   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
393                                      St->getValue().getValueType());
394   SDValue Chain = St->getChain();
395   SDValue Ptr = St->getBasePtr();
396   int SVOffset = St->getSrcValueOffset();
397   unsigned Alignment = St->getAlignment();
398   bool isVolatile = St->isVolatile();
399   bool isNonTemporal = St->isNonTemporal();
400
401   assert(NVT.isByteSized() && "Expanded type not byte sized!");
402   unsigned IncrementSize = NVT.getSizeInBits() / 8;
403
404   SDValue Lo, Hi;
405   GetExpandedOp(St->getValue(), Lo, Hi);
406
407   if (TLI.isBigEndian())
408     std::swap(Lo, Hi);
409
410   Lo = DAG.getStore(Chain, dl, Lo, Ptr, St->getSrcValue(), SVOffset,
411                     isVolatile, isNonTemporal, Alignment);
412
413   Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
414                     DAG.getIntPtrConstant(IncrementSize));
415   assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!");
416   Hi = DAG.getStore(Chain, dl, Hi, Ptr, St->getSrcValue(),
417                     SVOffset + IncrementSize,
418                     isVolatile, isNonTemporal,
419                     MinAlign(Alignment, IncrementSize));
420
421   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
422 }
423
424
425 //===--------------------------------------------------------------------===//
426 // Generic Result Splitting.
427 //===--------------------------------------------------------------------===//
428
429 // Be careful to make no assumptions about which of Lo/Hi is stored first in
430 // memory (for vectors it is always Lo first followed by Hi in the following
431 // bytes; for integers and floats it is Lo first if and only if the machine is
432 // little-endian).
433
434 void DAGTypeLegalizer::SplitRes_MERGE_VALUES(SDNode *N,
435                                              SDValue &Lo, SDValue &Hi) {
436   // A MERGE_VALUES node can produce any number of values.  We know that the
437   // first illegal one needs to be expanded into Lo/Hi.
438   unsigned i;
439
440   // The string of legal results gets turned into input operands, which have
441   // the same type.
442   for (i = 0; isTypeLegal(N->getValueType(i)); ++i)
443     ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i)));
444
445   // The first illegal result must be the one that needs to be expanded.
446   GetSplitOp(N->getOperand(i), Lo, Hi);
447
448   // Legalize the rest of the results into the input operands whether they are
449   // legal or not.
450   unsigned e = N->getNumValues();
451   for (++i; i != e; ++i)
452     ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i)));
453 }
454
455 void DAGTypeLegalizer::SplitRes_SELECT(SDNode *N, SDValue &Lo,
456                                        SDValue &Hi) {
457   SDValue LL, LH, RL, RH;
458   DebugLoc dl = N->getDebugLoc();
459   GetSplitOp(N->getOperand(1), LL, LH);
460   GetSplitOp(N->getOperand(2), RL, RH);
461
462   SDValue Cond = N->getOperand(0);
463   Lo = DAG.getNode(ISD::SELECT, dl, LL.getValueType(), Cond, LL, RL);
464   Hi = DAG.getNode(ISD::SELECT, dl, LH.getValueType(), Cond, LH, RH);
465 }
466
467 void DAGTypeLegalizer::SplitRes_SELECT_CC(SDNode *N, SDValue &Lo,
468                                           SDValue &Hi) {
469   SDValue LL, LH, RL, RH;
470   DebugLoc dl = N->getDebugLoc();
471   GetSplitOp(N->getOperand(2), LL, LH);
472   GetSplitOp(N->getOperand(3), RL, RH);
473
474   Lo = DAG.getNode(ISD::SELECT_CC, dl, LL.getValueType(), N->getOperand(0),
475                    N->getOperand(1), LL, RL, N->getOperand(4));
476   Hi = DAG.getNode(ISD::SELECT_CC, dl, LH.getValueType(), N->getOperand(0),
477                    N->getOperand(1), LH, RH, N->getOperand(4));
478 }
479
480 void DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDValue &Lo, SDValue &Hi) {
481   EVT LoVT, HiVT;
482   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
483   Lo = DAG.getUNDEF(LoVT);
484   Hi = DAG.getUNDEF(HiVT);
485 }