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