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"
23 //===----------------------------------------------------------------------===//
24 // Generic Result Expansion.
25 //===----------------------------------------------------------------------===//
27 // These routines assume that the Lo/Hi part is stored first in memory on
28 // little/big-endian machines, followed by the Hi/Lo part. This means that
29 // they cannot be used as is on vectors, for which Lo is always stored first.
31 void DAGTypeLegalizer::ExpandRes_BIT_CONVERT(SDNode *N,
32 SDOperand &Lo, SDOperand &Hi) {
33 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
34 SDOperand InOp = N->getOperand(0);
35 MVT InVT = InOp.getValueType();
37 // Handle some special cases efficiently.
38 switch (getTypeAction(InVT)) {
40 assert(false && "Unknown type action!");
45 // Convert the integer operand instead.
46 SplitInteger(GetSoftenedFloat(InOp), Lo, Hi);
47 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Lo);
48 Hi = DAG.getNode(ISD::BIT_CONVERT, NVT, Hi);
52 // Convert the expanded pieces of the input.
53 GetExpandedOp(InOp, Lo, Hi);
54 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Lo);
55 Hi = DAG.getNode(ISD::BIT_CONVERT, NVT, Hi);
58 // Convert the split parts of the input if it was split in two.
59 GetSplitVector(InOp, Lo, Hi);
60 if (Lo.getValueType() == Hi.getValueType()) {
61 if (TLI.isBigEndian())
63 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Lo);
64 Hi = DAG.getNode(ISD::BIT_CONVERT, NVT, Hi);
69 // Convert the element instead.
70 SplitInteger(BitConvertToInteger(GetScalarizedVector(InOp)), Lo, Hi);
71 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Lo);
72 Hi = DAG.getNode(ISD::BIT_CONVERT, NVT, Hi);
76 // Lower the bit-convert to a store/load from the stack, then expand the load.
77 SDOperand Op = CreateStackStoreLoad(InOp, N->getValueType(0));
78 ExpandRes_NON_EXTLOAD(Op.Val, Lo, Hi);
81 void DAGTypeLegalizer::ExpandRes_BUILD_PAIR(SDNode *N,
82 SDOperand &Lo, SDOperand &Hi) {
83 // Return the operands.
84 Lo = N->getOperand(0);
85 Hi = N->getOperand(1);
88 void DAGTypeLegalizer::ExpandRes_EXTRACT_VECTOR_ELT(SDNode *N,
91 SDOperand OldVec = N->getOperand(0);
92 unsigned OldElts = OldVec.getValueType().getVectorNumElements();
94 // Convert to a vector of the expanded element type, for example
95 // <3 x i64> -> <6 x i32>.
96 MVT OldVT = N->getValueType(0);
97 MVT NewVT = TLI.getTypeToTransformTo(OldVT);
99 SDOperand NewVec = DAG.getNode(ISD::BIT_CONVERT,
100 MVT::getVectorVT(NewVT, 2*OldElts),
103 // Extract the elements at 2 * Idx and 2 * Idx + 1 from the new vector.
104 SDOperand Idx = N->getOperand(1);
106 // Make sure the type of Idx is big enough to hold the new values.
107 if (Idx.getValueType().bitsLT(TLI.getPointerTy()))
108 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
110 Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, Idx);
111 Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, Idx);
113 Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx,
114 DAG.getConstant(1, Idx.getValueType()));
115 Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, Idx);
117 if (TLI.isBigEndian())
121 void DAGTypeLegalizer::ExpandRes_NON_EXTLOAD(SDNode *N, SDOperand &Lo,
123 assert(ISD::isNON_EXTLoad(N) && "This routine is not for extending loads!");
124 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
126 LoadSDNode *LD = cast<LoadSDNode>(N);
127 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
128 SDOperand Chain = LD->getChain();
129 SDOperand Ptr = LD->getBasePtr();
130 int SVOffset = LD->getSrcValueOffset();
131 unsigned Alignment = LD->getAlignment();
132 bool isVolatile = LD->isVolatile();
134 assert(NVT.isByteSized() && "Expanded type not byte sized!");
136 Lo = DAG.getLoad(NVT, Chain, Ptr, LD->getSrcValue(), SVOffset,
137 isVolatile, Alignment);
139 // Increment the pointer to the other half.
140 unsigned IncrementSize = NVT.getSizeInBits() / 8;
141 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
142 DAG.getIntPtrConstant(IncrementSize));
143 Hi = DAG.getLoad(NVT, Chain, Ptr, LD->getSrcValue(), SVOffset+IncrementSize,
144 isVolatile, MinAlign(Alignment, IncrementSize));
146 // Build a factor node to remember that this load is independent of the
148 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
151 // Handle endianness of the load.
152 if (TLI.isBigEndian())
155 // Modified the chain - switch anything that used the old chain to use
157 ReplaceValueWith(SDOperand(N, 1), Chain);
160 //===--------------------------------------------------------------------===//
161 // Generic Operand Expansion.
162 //===--------------------------------------------------------------------===//
164 SDOperand DAGTypeLegalizer::ExpandOp_BIT_CONVERT(SDNode *N) {
165 if (N->getValueType(0).isVector()) {
166 // An illegal expanding type is being converted to a legal vector type.
167 // Make a two element vector out of the expanded parts and convert that
168 // instead, but only if the new vector type is legal (otherwise there
169 // is no point, and it might create expansion loops). For example, on
170 // x86 this turns v1i64 = BIT_CONVERT i64 into v1i64 = BIT_CONVERT v2i32.
171 MVT OVT = N->getOperand(0).getValueType();
172 MVT NVT = MVT::getVectorVT(TLI.getTypeToTransformTo(OVT), 2);
174 if (isTypeLegal(NVT)) {
176 GetExpandedOp(N->getOperand(0), Parts[0], Parts[1]);
178 if (TLI.isBigEndian())
179 std::swap(Parts[0], Parts[1]);
181 SDOperand Vec = DAG.getNode(ISD::BUILD_VECTOR, NVT, Parts, 2);
182 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Vec);
186 // Otherwise, store to a temporary and load out again as the new type.
187 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
190 SDOperand DAGTypeLegalizer::ExpandOp_BUILD_VECTOR(SDNode *N) {
191 // The vector type is legal but the element type needs expansion.
192 MVT VecVT = N->getValueType(0);
193 unsigned NumElts = VecVT.getVectorNumElements();
194 MVT OldVT = N->getOperand(0).getValueType();
195 MVT NewVT = TLI.getTypeToTransformTo(OldVT);
197 // Build a vector of twice the length out of the expanded elements.
198 // For example <3 x i64> -> <6 x i32>.
199 std::vector<SDOperand> NewElts;
200 NewElts.reserve(NumElts*2);
202 for (unsigned i = 0; i < NumElts; ++i) {
204 GetExpandedOp(N->getOperand(i), Lo, Hi);
205 if (TLI.isBigEndian())
207 NewElts.push_back(Lo);
208 NewElts.push_back(Hi);
211 SDOperand NewVec = DAG.getNode(ISD::BUILD_VECTOR,
212 MVT::getVectorVT(NewVT, NewElts.size()),
213 &NewElts[0], NewElts.size());
215 // Convert the new vector to the old vector type.
216 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
219 SDOperand DAGTypeLegalizer::ExpandOp_EXTRACT_ELEMENT(SDNode *N) {
221 GetExpandedOp(N->getOperand(0), Lo, Hi);
222 return cast<ConstantSDNode>(N->getOperand(1))->getValue() ? Hi : Lo;
225 SDOperand DAGTypeLegalizer::ExpandOp_NON_TRUNCStore(SDNode *N, unsigned OpNo) {
226 assert(ISD::isNON_TRUNCStore(N) && "This routine not for truncating stores!");
227 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
228 assert(OpNo == 1 && "Can only expand the stored value so far");
230 StoreSDNode *St = cast<StoreSDNode>(N);
231 MVT NVT = TLI.getTypeToTransformTo(St->getValue().getValueType());
232 SDOperand Chain = St->getChain();
233 SDOperand Ptr = St->getBasePtr();
234 int SVOffset = St->getSrcValueOffset();
235 unsigned Alignment = St->getAlignment();
236 bool isVolatile = St->isVolatile();
238 assert(NVT.isByteSized() && "Expanded type not byte sized!");
239 unsigned IncrementSize = NVT.getSizeInBits() / 8;
242 GetExpandedOp(St->getValue(), Lo, Hi);
244 if (TLI.isBigEndian())
247 Lo = DAG.getStore(Chain, Lo, Ptr, St->getSrcValue(), SVOffset,
248 isVolatile, Alignment);
250 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
251 DAG.getIntPtrConstant(IncrementSize));
252 assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!");
253 Hi = DAG.getStore(Chain, Hi, Ptr, St->getSrcValue(), SVOffset + IncrementSize,
254 isVolatile, MinAlign(Alignment, IncrementSize));
256 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
260 //===--------------------------------------------------------------------===//
261 // Generic Result Splitting.
262 //===--------------------------------------------------------------------===//
264 // Be careful to make no assumptions about which of Lo/Hi is stored first in
265 // memory (for vectors it is always Lo first followed by Hi in the following
266 // bytes; for integers and floats it is Lo first if and only if the machine is
269 void DAGTypeLegalizer::SplitRes_MERGE_VALUES(SDNode *N,
270 SDOperand &Lo, SDOperand &Hi) {
271 // A MERGE_VALUES node can produce any number of values. We know that the
272 // first illegal one needs to be expanded into Lo/Hi.
275 // The string of legal results gets turns into the input operands, which have
277 for (i = 0; isTypeLegal(N->getValueType(i)); ++i)
278 ReplaceValueWith(SDOperand(N, i), SDOperand(N->getOperand(i)));
280 // The first illegal result must be the one that needs to be expanded.
281 GetSplitOp(N->getOperand(i), Lo, Hi);
283 // Legalize the rest of the results into the input operands whether they are
285 unsigned e = N->getNumValues();
286 for (++i; i != e; ++i)
287 ReplaceValueWith(SDOperand(N, i), SDOperand(N->getOperand(i)));
290 void DAGTypeLegalizer::SplitRes_SELECT(SDNode *N, SDOperand &Lo,
292 SDOperand LL, LH, RL, RH;
293 GetSplitOp(N->getOperand(1), LL, LH);
294 GetSplitOp(N->getOperand(2), RL, RH);
296 SDOperand Cond = N->getOperand(0);
297 Lo = DAG.getNode(ISD::SELECT, LL.getValueType(), Cond, LL, RL);
298 Hi = DAG.getNode(ISD::SELECT, LH.getValueType(), Cond, LH, RH);
301 void DAGTypeLegalizer::SplitRes_SELECT_CC(SDNode *N, SDOperand &Lo,
303 SDOperand LL, LH, RL, RH;
304 GetSplitOp(N->getOperand(2), LL, LH);
305 GetSplitOp(N->getOperand(3), RL, RH);
307 Lo = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0),
308 N->getOperand(1), LL, RL, N->getOperand(4));
309 Hi = DAG.getNode(ISD::SELECT_CC, LH.getValueType(), N->getOperand(0),
310 N->getOperand(1), LH, RH, N->getOperand(4));
313 void DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
315 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
316 Lo = DAG.getNode(ISD::UNDEF, LoVT);
317 Hi = DAG.getNode(ISD::UNDEF, HiVT);