Fix PR3117: not all nodes being legalized. The
[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.
15 // Splitting is the act of changing a computation in an illegal type to be a
16 // computation in two not necessarily identical registers of a smaller type.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #include "LegalizeTypes.h"
21 #include "llvm/Target/TargetData.h"
22 using namespace llvm;
23
24 //===----------------------------------------------------------------------===//
25 // Generic Result Expansion.
26 //===----------------------------------------------------------------------===//
27
28 // These routines assume that the Lo/Hi part is stored first in memory on
29 // little/big-endian machines, followed by the Hi/Lo part.  This means that
30 // they cannot be used as is on vectors, for which Lo is always stored first.
31
32 void DAGTypeLegalizer::ExpandRes_BIT_CONVERT(SDNode *N, SDValue &Lo,
33                                              SDValue &Hi) {
34   MVT OutVT = N->getValueType(0);
35   MVT NOutVT = TLI.getTypeToTransformTo(OutVT);
36   SDValue InOp = N->getOperand(0);
37   MVT InVT = InOp.getValueType();
38
39   // Handle some special cases efficiently.
40   switch (getTypeAction(InVT)) {
41     default:
42       assert(false && "Unknown type action!");
43     case Legal:
44     case PromoteInteger:
45       break;
46     case SoftenFloat:
47       // Convert the integer operand instead.
48       SplitInteger(GetSoftenedFloat(InOp), Lo, Hi);
49       Lo = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Lo);
50       Hi = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Hi);
51       return;
52     case ExpandInteger:
53     case ExpandFloat:
54       // Convert the expanded pieces of the input.
55       GetExpandedOp(InOp, Lo, Hi);
56       Lo = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Lo);
57       Hi = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Hi);
58       return;
59     case SplitVector:
60       // Convert the split parts of the input if it was split in two.
61       GetSplitVector(InOp, Lo, Hi);
62       if (Lo.getValueType() == Hi.getValueType()) {
63         if (TLI.isBigEndian())
64           std::swap(Lo, Hi);
65         Lo = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Lo);
66         Hi = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Hi);
67         return;
68       }
69       break;
70     case ScalarizeVector:
71       // Convert the element instead.
72       SplitInteger(BitConvertToInteger(GetScalarizedVector(InOp)), Lo, Hi);
73       Lo = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Lo);
74       Hi = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Hi);
75       return;
76   }
77
78   // Lower the bit-convert to a store/load from the stack.
79   assert(NOutVT.isByteSized() && "Expanded type not byte sized!");
80
81   // Create the stack frame object.  Make sure it is aligned for both
82   // the source and expanded destination types.
83   unsigned Alignment =
84     TLI.getTargetData()->getPrefTypeAlignment(NOutVT.getTypeForMVT());
85   SDValue StackPtr = DAG.CreateStackTemporary(InVT, Alignment);
86
87   // Emit a store to the stack slot.
88   SDValue Store = DAG.getStore(DAG.getEntryNode(), InOp, StackPtr, NULL, 0);
89
90   // Load the first half from the stack slot.
91   Lo = DAG.getLoad(NOutVT, Store, StackPtr, NULL, 0);
92
93   // Increment the pointer to the other half.
94   unsigned IncrementSize = NOutVT.getSizeInBits() / 8;
95   StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
96                          DAG.getIntPtrConstant(IncrementSize));
97
98   // Load the second half from the stack slot.
99   Hi = DAG.getLoad(NOutVT, Store, StackPtr, NULL, 0, false,
100                    MinAlign(Alignment, IncrementSize));
101
102   // Handle endianness of the load.
103   if (TLI.isBigEndian())
104     std::swap(Lo, Hi);
105 }
106
107 void DAGTypeLegalizer::ExpandRes_BUILD_PAIR(SDNode *N, SDValue &Lo,
108                                             SDValue &Hi) {
109   // Return the operands.
110   Lo = N->getOperand(0);
111   Hi = N->getOperand(1);
112 }
113
114 void DAGTypeLegalizer::ExpandRes_EXTRACT_ELEMENT(SDNode *N, SDValue &Lo,
115                                                  SDValue &Hi) {
116   GetExpandedOp(N->getOperand(0), Lo, Hi);
117   SDValue Part = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ?
118                    Hi : Lo;
119
120   assert(Part.getValueType() == N->getValueType(0) &&
121          "Type twice as big as expanded type not itself expanded!");
122   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
123
124   Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Part,
125                    DAG.getConstant(0, TLI.getPointerTy()));
126   Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Part,
127                    DAG.getConstant(1, TLI.getPointerTy()));
128 }
129
130 void DAGTypeLegalizer::ExpandRes_EXTRACT_VECTOR_ELT(SDNode *N, SDValue &Lo,
131                                                     SDValue &Hi) {
132   SDValue OldVec = N->getOperand(0);
133   unsigned OldElts = OldVec.getValueType().getVectorNumElements();
134
135   // Convert to a vector of the expanded element type, for example
136   // <3 x i64> -> <6 x i32>.
137   MVT OldVT = N->getValueType(0);
138   MVT NewVT = TLI.getTypeToTransformTo(OldVT);
139
140   SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT,
141                                  MVT::getVectorVT(NewVT, 2*OldElts),
142                                  OldVec);
143
144   // Extract the elements at 2 * Idx and 2 * Idx + 1 from the new vector.
145   SDValue Idx = N->getOperand(1);
146
147   // Make sure the type of Idx is big enough to hold the new values.
148   if (Idx.getValueType().bitsLT(TLI.getPointerTy()))
149     Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
150
151   Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, Idx);
152   Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, Idx);
153
154   Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx,
155                     DAG.getConstant(1, Idx.getValueType()));
156   Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, Idx);
157
158   if (TLI.isBigEndian())
159     std::swap(Lo, Hi);
160 }
161
162 void DAGTypeLegalizer::ExpandRes_NormalLoad(SDNode *N, SDValue &Lo,
163                                             SDValue &Hi) {
164   assert(ISD::isNormalLoad(N) && "This routine only for normal loads!");
165
166   LoadSDNode *LD = cast<LoadSDNode>(N);
167   MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
168   SDValue Chain = LD->getChain();
169   SDValue Ptr = LD->getBasePtr();
170   int SVOffset = LD->getSrcValueOffset();
171   unsigned Alignment = LD->getAlignment();
172   bool isVolatile = LD->isVolatile();
173
174   assert(NVT.isByteSized() && "Expanded type not byte sized!");
175
176   Lo = DAG.getLoad(NVT, Chain, Ptr, LD->getSrcValue(), SVOffset,
177                    isVolatile, Alignment);
178
179   // Increment the pointer to the other half.
180   unsigned IncrementSize = NVT.getSizeInBits() / 8;
181   Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
182                     DAG.getIntPtrConstant(IncrementSize));
183   Hi = DAG.getLoad(NVT, Chain, Ptr, LD->getSrcValue(), SVOffset+IncrementSize,
184                    isVolatile, MinAlign(Alignment, IncrementSize));
185
186   // Build a factor node to remember that this load is independent of the
187   // other one.
188   Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
189                       Hi.getValue(1));
190
191   // Handle endianness of the load.
192   if (TLI.isBigEndian())
193     std::swap(Lo, Hi);
194
195   // Modified the chain - switch anything that used the old chain to use
196   // the new one.
197   ReplaceValueWith(SDValue(N, 1), Chain);
198 }
199
200 void DAGTypeLegalizer::ExpandRes_VAARG(SDNode *N, SDValue &Lo, SDValue &Hi) {
201   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
202   SDValue Chain = N->getOperand(0);
203   SDValue Ptr = N->getOperand(1);
204
205   Lo = DAG.getVAArg(NVT, Chain, Ptr, N->getOperand(2));
206   Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, N->getOperand(2));
207
208   // Handle endianness of the load.
209   if (TLI.isBigEndian())
210     std::swap(Lo, Hi);
211
212   // Modified the chain - switch anything that used the old chain to use
213   // the new one.
214   ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
215 }
216
217
218 //===--------------------------------------------------------------------===//
219 // Generic Operand Expansion.
220 //===--------------------------------------------------------------------===//
221
222 SDValue DAGTypeLegalizer::ExpandOp_BIT_CONVERT(SDNode *N) {
223   if (N->getValueType(0).isVector()) {
224     // An illegal expanding type is being converted to a legal vector type.
225     // Make a two element vector out of the expanded parts and convert that
226     // instead, but only if the new vector type is legal (otherwise there
227     // is no point, and it might create expansion loops).  For example, on
228     // x86 this turns v1i64 = BIT_CONVERT i64 into v1i64 = BIT_CONVERT v2i32.
229     MVT OVT = N->getOperand(0).getValueType();
230     MVT NVT = MVT::getVectorVT(TLI.getTypeToTransformTo(OVT), 2);
231
232     if (isTypeLegal(NVT)) {
233       SDValue Parts[2];
234       GetExpandedOp(N->getOperand(0), Parts[0], Parts[1]);
235
236       if (TLI.isBigEndian())
237         std::swap(Parts[0], Parts[1]);
238
239       SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, NVT, Parts, 2);
240       return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Vec);
241     }
242   }
243
244   // Otherwise, store to a temporary and load out again as the new type.
245   return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
246 }
247
248 SDValue DAGTypeLegalizer::ExpandOp_BUILD_VECTOR(SDNode *N) {
249   // The vector type is legal but the element type needs expansion.
250   MVT VecVT = N->getValueType(0);
251   unsigned NumElts = VecVT.getVectorNumElements();
252   MVT OldVT = N->getOperand(0).getValueType();
253   MVT NewVT = TLI.getTypeToTransformTo(OldVT);
254
255   // Build a vector of twice the length out of the expanded elements.
256   // For example <3 x i64> -> <6 x i32>.
257   std::vector<SDValue> NewElts;
258   NewElts.reserve(NumElts*2);
259
260   for (unsigned i = 0; i < NumElts; ++i) {
261     SDValue Lo, Hi;
262     GetExpandedOp(N->getOperand(i), Lo, Hi);
263     if (TLI.isBigEndian())
264       std::swap(Lo, Hi);
265     NewElts.push_back(Lo);
266     NewElts.push_back(Hi);
267   }
268
269   SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR,
270                                  MVT::getVectorVT(NewVT, NewElts.size()),
271                                  &NewElts[0], NewElts.size());
272
273   // Convert the new vector to the old vector type.
274   return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
275 }
276
277 SDValue DAGTypeLegalizer::ExpandOp_EXTRACT_ELEMENT(SDNode *N) {
278   SDValue Lo, Hi;
279   GetExpandedOp(N->getOperand(0), Lo, Hi);
280   return cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? Hi : Lo;
281 }
282
283 SDValue DAGTypeLegalizer::ExpandOp_NormalStore(SDNode *N, unsigned OpNo) {
284   assert(ISD::isNormalStore(N) && "This routine only for normal stores!");
285   assert(OpNo == 1 && "Can only expand the stored value so far");
286
287   StoreSDNode *St = cast<StoreSDNode>(N);
288   MVT NVT = TLI.getTypeToTransformTo(St->getValue().getValueType());
289   SDValue Chain = St->getChain();
290   SDValue Ptr = St->getBasePtr();
291   int SVOffset = St->getSrcValueOffset();
292   unsigned Alignment = St->getAlignment();
293   bool isVolatile = St->isVolatile();
294
295   assert(NVT.isByteSized() && "Expanded type not byte sized!");
296   unsigned IncrementSize = NVT.getSizeInBits() / 8;
297
298   SDValue Lo, Hi;
299   GetExpandedOp(St->getValue(), Lo, Hi);
300
301   if (TLI.isBigEndian())
302     std::swap(Lo, Hi);
303
304   Lo = DAG.getStore(Chain, Lo, Ptr, St->getSrcValue(), SVOffset,
305                     isVolatile, Alignment);
306
307   Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
308                     DAG.getIntPtrConstant(IncrementSize));
309   assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!");
310   Hi = DAG.getStore(Chain, Hi, Ptr, St->getSrcValue(), SVOffset + IncrementSize,
311                     isVolatile, MinAlign(Alignment, IncrementSize));
312
313   return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
314 }
315
316
317 //===--------------------------------------------------------------------===//
318 // Generic Result Splitting.
319 //===--------------------------------------------------------------------===//
320
321 // Be careful to make no assumptions about which of Lo/Hi is stored first in
322 // memory (for vectors it is always Lo first followed by Hi in the following
323 // bytes; for integers and floats it is Lo first if and only if the machine is
324 // little-endian).
325
326 void DAGTypeLegalizer::SplitRes_MERGE_VALUES(SDNode *N,
327                                              SDValue &Lo, SDValue &Hi) {
328   // A MERGE_VALUES node can produce any number of values.  We know that the
329   // first illegal one needs to be expanded into Lo/Hi.
330   unsigned i;
331
332   // The string of legal results gets turned into input operands, which have
333   // the same type.
334   for (i = 0; isTypeLegal(N->getValueType(i)); ++i)
335     ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i)));
336
337   // The first illegal result must be the one that needs to be expanded.
338   GetSplitOp(N->getOperand(i), Lo, Hi);
339
340   // Legalize the rest of the results into the input operands whether they are
341   // legal or not.
342   unsigned e = N->getNumValues();
343   for (++i; i != e; ++i)
344     ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i)));
345 }
346
347 void DAGTypeLegalizer::SplitRes_SELECT(SDNode *N, SDValue &Lo,
348                                        SDValue &Hi) {
349   SDValue LL, LH, RL, RH;
350   GetSplitOp(N->getOperand(1), LL, LH);
351   GetSplitOp(N->getOperand(2), RL, RH);
352
353   SDValue Cond = N->getOperand(0);
354   Lo = DAG.getNode(ISD::SELECT, LL.getValueType(), Cond, LL, RL);
355   Hi = DAG.getNode(ISD::SELECT, LH.getValueType(), Cond, LH, RH);
356 }
357
358 void DAGTypeLegalizer::SplitRes_SELECT_CC(SDNode *N, SDValue &Lo,
359                                           SDValue &Hi) {
360   SDValue LL, LH, RL, RH;
361   GetSplitOp(N->getOperand(2), LL, LH);
362   GetSplitOp(N->getOperand(3), RL, RH);
363
364   Lo = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0),
365                    N->getOperand(1), LL, RL, N->getOperand(4));
366   Hi = DAG.getNode(ISD::SELECT_CC, LH.getValueType(), N->getOperand(0),
367                    N->getOperand(1), LH, RH, N->getOperand(4));
368 }
369
370 void DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDValue &Lo, SDValue &Hi) {
371   MVT LoVT, HiVT;
372   GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
373   Lo = DAG.getNode(ISD::UNDEF, LoVT);
374   Hi = DAG.getNode(ISD::UNDEF, HiVT);
375 }