1 //===-------- LegalizeTypesGeneric.cpp - Generic type legalization --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
18 //===----------------------------------------------------------------------===//
20 #include "LegalizeTypes.h"
21 #include "llvm/Target/TargetData.h"
24 //===----------------------------------------------------------------------===//
25 // Generic Result Expansion.
26 //===----------------------------------------------------------------------===//
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.
32 void DAGTypeLegalizer::ExpandRes_BIT_CONVERT(SDNode *N, SDValue &Lo,
34 MVT OutVT = N->getValueType(0);
35 MVT NOutVT = TLI.getTypeToTransformTo(OutVT);
36 SDValue InOp = N->getOperand(0);
37 MVT InVT = InOp.getValueType();
39 // Handle some special cases efficiently.
40 switch (getTypeAction(InVT)) {
42 assert(false && "Unknown type action!");
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);
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);
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())
65 Lo = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Lo);
66 Hi = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Hi);
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);
78 // Lower the bit-convert to a store/load from the stack.
79 assert(NOutVT.isByteSized() && "Expanded type not byte sized!");
81 // Create the stack frame object. Make sure it is aligned for both
82 // the source and expanded destination types.
84 TLI.getTargetData()->getPrefTypeAlignment(NOutVT.getTypeForMVT());
85 SDValue StackPtr = DAG.CreateStackTemporary(InVT, Alignment);
87 // Emit a store to the stack slot.
88 SDValue Store = DAG.getStore(DAG.getEntryNode(), InOp, StackPtr, NULL, 0);
90 // Load the first half from the stack slot.
91 Lo = DAG.getLoad(NOutVT, Store, StackPtr, NULL, 0);
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));
98 // Load the second half from the stack slot.
99 Hi = DAG.getLoad(NOutVT, Store, StackPtr, NULL, 0, false,
100 MinAlign(Alignment, IncrementSize));
102 // Handle endianness of the load.
103 if (TLI.isBigEndian())
107 void DAGTypeLegalizer::ExpandRes_BUILD_PAIR(SDNode *N, SDValue &Lo,
109 // Return the operands.
110 Lo = N->getOperand(0);
111 Hi = N->getOperand(1);
114 void DAGTypeLegalizer::ExpandRes_EXTRACT_ELEMENT(SDNode *N, SDValue &Lo,
116 GetExpandedOp(N->getOperand(0), Lo, Hi);
117 SDValue Part = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ?
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));
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()));
130 void DAGTypeLegalizer::ExpandRes_EXTRACT_VECTOR_ELT(SDNode *N, SDValue &Lo,
132 SDValue OldVec = N->getOperand(0);
133 unsigned OldElts = OldVec.getValueType().getVectorNumElements();
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);
140 SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT,
141 MVT::getVectorVT(NewVT, 2*OldElts),
144 // Extract the elements at 2 * Idx and 2 * Idx + 1 from the new vector.
145 SDValue Idx = N->getOperand(1);
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);
151 Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, Idx);
152 Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, Idx);
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);
158 if (TLI.isBigEndian())
162 void DAGTypeLegalizer::ExpandRes_NormalLoad(SDNode *N, SDValue &Lo,
164 assert(ISD::isNormalLoad(N) && "This routine only for normal loads!");
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();
174 assert(NVT.isByteSized() && "Expanded type not byte sized!");
176 Lo = DAG.getLoad(NVT, Chain, Ptr, LD->getSrcValue(), SVOffset,
177 isVolatile, Alignment);
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));
186 // Build a factor node to remember that this load is independent of the
188 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
191 // Handle endianness of the load.
192 if (TLI.isBigEndian())
195 // Modified the chain - switch anything that used the old chain to use
197 ReplaceValueWith(SDValue(N, 1), Chain);
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);
205 Lo = DAG.getVAArg(NVT, Chain, Ptr, N->getOperand(2));
206 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, N->getOperand(2));
208 // Handle endianness of the load.
209 if (TLI.isBigEndian())
212 // Modified the chain - switch anything that used the old chain to use
214 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
218 //===--------------------------------------------------------------------===//
219 // Generic Operand Expansion.
220 //===--------------------------------------------------------------------===//
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);
232 if (isTypeLegal(NVT)) {
234 GetExpandedOp(N->getOperand(0), Parts[0], Parts[1]);
236 if (TLI.isBigEndian())
237 std::swap(Parts[0], Parts[1]);
239 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, NVT, Parts, 2);
240 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Vec);
244 // Otherwise, store to a temporary and load out again as the new type.
245 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
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);
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);
260 for (unsigned i = 0; i < NumElts; ++i) {
262 GetExpandedOp(N->getOperand(i), Lo, Hi);
263 if (TLI.isBigEndian())
265 NewElts.push_back(Lo);
266 NewElts.push_back(Hi);
269 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR,
270 MVT::getVectorVT(NewVT, NewElts.size()),
271 &NewElts[0], NewElts.size());
273 // Convert the new vector to the old vector type.
274 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
277 SDValue DAGTypeLegalizer::ExpandOp_EXTRACT_ELEMENT(SDNode *N) {
279 GetExpandedOp(N->getOperand(0), Lo, Hi);
280 return cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? Hi : Lo;
283 SDValue DAGTypeLegalizer::ExpandOp_INSERT_VECTOR_ELT(SDNode *N) {
284 // The vector type is legal but the element type needs expansion.
285 MVT VecVT = N->getValueType(0);
286 unsigned NumElts = VecVT.getVectorNumElements();
288 SDValue Val = N->getOperand(1);
289 MVT OldEVT = Val.getValueType();
290 MVT NewEVT = TLI.getTypeToTransformTo(OldEVT);
292 assert(OldEVT == VecVT.getVectorElementType() &&
293 "Inserted element type doesn't match vector element type!");
295 // Bitconvert to a vector of twice the length with elements of the expanded
296 // type, insert the expanded vector elements, and then convert back.
297 MVT NewVecVT = MVT::getVectorVT(NewEVT, NumElts*2);
298 SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT, NewVecVT, N->getOperand(0));
301 GetExpandedOp(Val, Lo, Hi);
302 if (TLI.isBigEndian())
305 SDValue Idx = N->getOperand(2);
306 Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, Idx);
307 NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVecVT, NewVec, Lo, Idx);
308 Idx = DAG.getNode(ISD::ADD,Idx.getValueType(), Idx, DAG.getIntPtrConstant(1));
309 NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVecVT, NewVec, Hi, Idx);
311 // Convert the new vector to the old vector type.
312 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
315 SDValue DAGTypeLegalizer::ExpandOp_SCALAR_TO_VECTOR(SDNode *N) {
316 MVT VT = N->getValueType(0);
317 unsigned NumElts = VT.getVectorNumElements();
318 SmallVector<SDValue, 16> Ops(NumElts);
319 Ops[0] = N->getOperand(0);
320 SDValue UndefVal = DAG.getNode(ISD::UNDEF, Ops[0].getValueType());
321 for (unsigned i = 1; i < NumElts; ++i)
323 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], NumElts);
326 SDValue DAGTypeLegalizer::ExpandOp_NormalStore(SDNode *N, unsigned OpNo) {
327 assert(ISD::isNormalStore(N) && "This routine only for normal stores!");
328 assert(OpNo == 1 && "Can only expand the stored value so far");
330 StoreSDNode *St = cast<StoreSDNode>(N);
331 MVT NVT = TLI.getTypeToTransformTo(St->getValue().getValueType());
332 SDValue Chain = St->getChain();
333 SDValue Ptr = St->getBasePtr();
334 int SVOffset = St->getSrcValueOffset();
335 unsigned Alignment = St->getAlignment();
336 bool isVolatile = St->isVolatile();
338 assert(NVT.isByteSized() && "Expanded type not byte sized!");
339 unsigned IncrementSize = NVT.getSizeInBits() / 8;
342 GetExpandedOp(St->getValue(), Lo, Hi);
344 if (TLI.isBigEndian())
347 Lo = DAG.getStore(Chain, Lo, Ptr, St->getSrcValue(), SVOffset,
348 isVolatile, Alignment);
350 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
351 DAG.getIntPtrConstant(IncrementSize));
352 assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!");
353 Hi = DAG.getStore(Chain, Hi, Ptr, St->getSrcValue(), SVOffset + IncrementSize,
354 isVolatile, MinAlign(Alignment, IncrementSize));
356 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
360 //===--------------------------------------------------------------------===//
361 // Generic Result Splitting.
362 //===--------------------------------------------------------------------===//
364 // Be careful to make no assumptions about which of Lo/Hi is stored first in
365 // memory (for vectors it is always Lo first followed by Hi in the following
366 // bytes; for integers and floats it is Lo first if and only if the machine is
369 void DAGTypeLegalizer::SplitRes_MERGE_VALUES(SDNode *N,
370 SDValue &Lo, SDValue &Hi) {
371 // A MERGE_VALUES node can produce any number of values. We know that the
372 // first illegal one needs to be expanded into Lo/Hi.
375 // The string of legal results gets turned into input operands, which have
377 for (i = 0; isTypeLegal(N->getValueType(i)); ++i)
378 ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i)));
380 // The first illegal result must be the one that needs to be expanded.
381 GetSplitOp(N->getOperand(i), Lo, Hi);
383 // Legalize the rest of the results into the input operands whether they are
385 unsigned e = N->getNumValues();
386 for (++i; i != e; ++i)
387 ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i)));
390 void DAGTypeLegalizer::SplitRes_SELECT(SDNode *N, SDValue &Lo,
392 SDValue LL, LH, RL, RH;
393 GetSplitOp(N->getOperand(1), LL, LH);
394 GetSplitOp(N->getOperand(2), RL, RH);
396 SDValue Cond = N->getOperand(0);
397 Lo = DAG.getNode(ISD::SELECT, LL.getValueType(), Cond, LL, RL);
398 Hi = DAG.getNode(ISD::SELECT, LH.getValueType(), Cond, LH, RH);
401 void DAGTypeLegalizer::SplitRes_SELECT_CC(SDNode *N, SDValue &Lo,
403 SDValue LL, LH, RL, RH;
404 GetSplitOp(N->getOperand(2), LL, LH);
405 GetSplitOp(N->getOperand(3), RL, RH);
407 Lo = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0),
408 N->getOperand(1), LL, RL, N->getOperand(4));
409 Hi = DAG.getNode(ISD::SELECT_CC, LH.getValueType(), N->getOperand(0),
410 N->getOperand(1), LH, RH, N->getOperand(4));
413 void DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDValue &Lo, SDValue &Hi) {
415 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
416 Lo = DAG.getNode(ISD::UNDEF, LoVT);
417 Hi = DAG.getNode(ISD::UNDEF, HiVT);