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"
22 #include "llvm/CodeGen/PseudoSourceValue.h"
25 //===----------------------------------------------------------------------===//
26 // Generic Result Expansion.
27 //===----------------------------------------------------------------------===//
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.
33 void DAGTypeLegalizer::ExpandRes_BIT_CONVERT(SDNode *N, SDValue &Lo,
35 MVT OutVT = N->getValueType(0);
36 MVT NOutVT = TLI.getTypeToTransformTo(OutVT);
37 SDValue InOp = N->getOperand(0);
38 MVT InVT = InOp.getValueType();
40 // Handle some special cases efficiently.
41 switch (getTypeAction(InVT)) {
43 assert(false && "Unknown type action!");
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);
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);
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())
66 Lo = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Lo);
67 Hi = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Hi);
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);
79 // Lower the bit-convert to a store/load from the stack.
80 assert(NOutVT.isByteSized() && "Expanded type not byte sized!");
82 // Create the stack frame object. Make sure it is aligned for both
83 // the source and expanded destination types.
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);
90 // Emit a store to the stack slot.
91 SDValue Store = DAG.getStore(DAG.getEntryNode(), InOp, StackPtr, SV, 0);
93 // Load the first half from the stack slot.
94 Lo = DAG.getLoad(NOutVT, Store, StackPtr, SV, 0);
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));
101 // Load the second half from the stack slot.
102 Hi = DAG.getLoad(NOutVT, Store, StackPtr, SV, IncrementSize, false,
103 MinAlign(Alignment, IncrementSize));
105 // Handle endianness of the load.
106 if (TLI.isBigEndian())
110 void DAGTypeLegalizer::ExpandRes_BUILD_PAIR(SDNode *N, SDValue &Lo,
112 // Return the operands.
113 Lo = N->getOperand(0);
114 Hi = N->getOperand(1);
117 void DAGTypeLegalizer::ExpandRes_EXTRACT_ELEMENT(SDNode *N, SDValue &Lo,
119 GetExpandedOp(N->getOperand(0), Lo, Hi);
120 SDValue Part = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ?
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));
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()));
133 void DAGTypeLegalizer::ExpandRes_EXTRACT_VECTOR_ELT(SDNode *N, SDValue &Lo,
135 SDValue OldVec = N->getOperand(0);
136 unsigned OldElts = OldVec.getValueType().getVectorNumElements();
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);
143 SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT,
144 MVT::getVectorVT(NewVT, 2*OldElts),
147 // Extract the elements at 2 * Idx and 2 * Idx + 1 from the new vector.
148 SDValue Idx = N->getOperand(1);
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);
154 Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, Idx);
155 Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, Idx);
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);
161 if (TLI.isBigEndian())
165 void DAGTypeLegalizer::ExpandRes_NormalLoad(SDNode *N, SDValue &Lo,
167 assert(ISD::isNormalLoad(N) && "This routine only for normal loads!");
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();
177 assert(NVT.isByteSized() && "Expanded type not byte sized!");
179 Lo = DAG.getLoad(NVT, Chain, Ptr, LD->getSrcValue(), SVOffset,
180 isVolatile, Alignment);
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));
189 // Build a factor node to remember that this load is independent of the
191 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
194 // Handle endianness of the load.
195 if (TLI.isBigEndian())
198 // Modified the chain - switch anything that used the old chain to use
200 ReplaceValueWith(SDValue(N, 1), Chain);
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);
208 Lo = DAG.getVAArg(NVT, Chain, Ptr, N->getOperand(2));
209 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, N->getOperand(2));
211 // Handle endianness of the load.
212 if (TLI.isBigEndian())
215 // Modified the chain - switch anything that used the old chain to use
217 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
221 //===--------------------------------------------------------------------===//
222 // Generic Operand Expansion.
223 //===--------------------------------------------------------------------===//
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);
235 if (isTypeLegal(NVT)) {
237 GetExpandedOp(N->getOperand(0), Parts[0], Parts[1]);
239 if (TLI.isBigEndian())
240 std::swap(Parts[0], Parts[1]);
242 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, NVT, Parts, 2);
243 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Vec);
247 // Otherwise, store to a temporary and load out again as the new type.
248 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
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);
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);
263 for (unsigned i = 0; i < NumElts; ++i) {
265 GetExpandedOp(N->getOperand(i), Lo, Hi);
266 if (TLI.isBigEndian())
268 NewElts.push_back(Lo);
269 NewElts.push_back(Hi);
272 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR,
273 MVT::getVectorVT(NewVT, NewElts.size()),
274 &NewElts[0], NewElts.size());
276 // Convert the new vector to the old vector type.
277 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
280 SDValue DAGTypeLegalizer::ExpandOp_EXTRACT_ELEMENT(SDNode *N) {
282 GetExpandedOp(N->getOperand(0), Lo, Hi);
283 return cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? Hi : Lo;
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();
291 SDValue Val = N->getOperand(1);
292 MVT OldEVT = Val.getValueType();
293 MVT NewEVT = TLI.getTypeToTransformTo(OldEVT);
295 assert(OldEVT == VecVT.getVectorElementType() &&
296 "Inserted element type doesn't match vector element type!");
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));
304 GetExpandedOp(Val, Lo, Hi);
305 if (TLI.isBigEndian())
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);
314 // Convert the new vector to the old vector type.
315 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
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)
326 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], NumElts);
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");
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();
341 assert(NVT.isByteSized() && "Expanded type not byte sized!");
342 unsigned IncrementSize = NVT.getSizeInBits() / 8;
345 GetExpandedOp(St->getValue(), Lo, Hi);
347 if (TLI.isBigEndian())
350 Lo = DAG.getStore(Chain, Lo, Ptr, St->getSrcValue(), SVOffset,
351 isVolatile, Alignment);
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));
359 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
363 //===--------------------------------------------------------------------===//
364 // Generic Result Splitting.
365 //===--------------------------------------------------------------------===//
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
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.
378 // The string of legal results gets turned into input operands, which have
380 for (i = 0; isTypeLegal(N->getValueType(i)); ++i)
381 ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i)));
383 // The first illegal result must be the one that needs to be expanded.
384 GetSplitOp(N->getOperand(i), Lo, Hi);
386 // Legalize the rest of the results into the input operands whether they are
388 unsigned e = N->getNumValues();
389 for (++i; i != e; ++i)
390 ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i)));
393 void DAGTypeLegalizer::SplitRes_SELECT(SDNode *N, SDValue &Lo,
395 SDValue LL, LH, RL, RH;
396 GetSplitOp(N->getOperand(1), LL, LH);
397 GetSplitOp(N->getOperand(2), RL, RH);
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);
404 void DAGTypeLegalizer::SplitRes_SELECT_CC(SDNode *N, SDValue &Lo,
406 SDValue LL, LH, RL, RH;
407 GetSplitOp(N->getOperand(2), LL, LH);
408 GetSplitOp(N->getOperand(3), RL, RH);
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));
416 void DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDValue &Lo, SDValue &Hi) {
418 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
419 Lo = DAG.getNode(ISD::UNDEF, LoVT);
420 Hi = DAG.getNode(ISD::UNDEF, HiVT);