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