LegalizeTypesGeneric.cpp revision d17c0302763cfd0b3f6657d2493147552762ac07
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" 22using namespace llvm; 23 24//===----------------------------------------------------------------------===// 25// Generic Result Expansion. 26//===----------------------------------------------------------------------===// 27 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. 31 32void DAGTypeLegalizer::ExpandRes_BIT_CONVERT(SDNode *N, SDValue &Lo, 33 SDValue &Hi) { 34 MVT OutVT = N->getValueType(0); 35 MVT NOutVT = TLI.getTypeToTransformTo(OutVT); 36 SDValue InOp = N->getOperand(0); 37 MVT InVT = InOp.getValueType(); 38 39 // Handle some special cases efficiently. 40 switch (getTypeAction(InVT)) { 41 default: 42 assert(false && "Unknown type action!"); 43 case Legal: 44 case PromoteInteger: 45 break; 46 case SoftenFloat: 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); 51 return; 52 case ExpandInteger: 53 case ExpandFloat: 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); 58 return; 59 case SplitVector: 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()) 64 std::swap(Lo, Hi); 65 Lo = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Lo); 66 Hi = DAG.getNode(ISD::BIT_CONVERT, NOutVT, Hi); 67 return; 68 } 69 break; 70 case ScalarizeVector: 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); 75 return; 76 } 77 78 // Lower the bit-convert to a store/load from the stack. 79 assert(NOutVT.isByteSized() && "Expanded type not byte sized!"); 80 81 // Create the stack frame object. Make sure it is aligned for both 82 // the source and expanded destination types. 83 unsigned Alignment = 84 TLI.getTargetData()->getPrefTypeAlignment(NOutVT.getTypeForMVT()); 85 SDValue StackPtr = DAG.CreateStackTemporary(InVT, Alignment); 86 87 // Emit a store to the stack slot. 88 SDValue Store = DAG.getStore(DAG.getEntryNode(), InOp, StackPtr, NULL, 0); 89 90 // Load the first half from the stack slot. 91 Lo = DAG.getLoad(NOutVT, Store, StackPtr, NULL, 0); 92 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)); 97 98 // Load the second half from the stack slot. 99 Hi = DAG.getLoad(NOutVT, Store, StackPtr, NULL, 0, false, 100 MinAlign(Alignment, IncrementSize)); 101 102 // Handle endianness of the load. 103 if (TLI.isBigEndian()) 104 std::swap(Lo, Hi); 105} 106 107void DAGTypeLegalizer::ExpandRes_BUILD_PAIR(SDNode *N, SDValue &Lo, 108 SDValue &Hi) { 109 // Return the operands. 110 Lo = N->getOperand(0); 111 Hi = N->getOperand(1); 112} 113 114void DAGTypeLegalizer::ExpandRes_EXTRACT_ELEMENT(SDNode *N, SDValue &Lo, 115 SDValue &Hi) { 116 GetExpandedOp(N->getOperand(0), Lo, Hi); 117 SDValue Part = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? 118 Hi : Lo; 119 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)); 123 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())); 128} 129 130void DAGTypeLegalizer::ExpandRes_EXTRACT_VECTOR_ELT(SDNode *N, SDValue &Lo, 131 SDValue &Hi) { 132 SDValue OldVec = N->getOperand(0); 133 unsigned OldElts = OldVec.getValueType().getVectorNumElements(); 134 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); 139 140 SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT, 141 MVT::getVectorVT(NewVT, 2*OldElts), 142 OldVec); 143 144 // Extract the elements at 2 * Idx and 2 * Idx + 1 from the new vector. 145 SDValue Idx = N->getOperand(1); 146 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); 150 151 Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, Idx); 152 Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, Idx); 153 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); 157 158 if (TLI.isBigEndian()) 159 std::swap(Lo, Hi); 160} 161 162void DAGTypeLegalizer::ExpandRes_NormalLoad(SDNode *N, SDValue &Lo, 163 SDValue &Hi) { 164 assert(ISD::isNormalLoad(N) && "This routine only for normal loads!"); 165 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(); 173 174 assert(NVT.isByteSized() && "Expanded type not byte sized!"); 175 176 Lo = DAG.getLoad(NVT, Chain, Ptr, LD->getSrcValue(), SVOffset, 177 isVolatile, Alignment); 178 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)); 185 186 // Build a factor node to remember that this load is independent of the 187 // other one. 188 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), 189 Hi.getValue(1)); 190 191 // Handle endianness of the load. 192 if (TLI.isBigEndian()) 193 std::swap(Lo, Hi); 194 195 // Modified the chain - switch anything that used the old chain to use 196 // the new one. 197 ReplaceValueWith(SDValue(N, 1), Chain); 198} 199 200void 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); 204 205 Lo = DAG.getVAArg(NVT, Chain, Ptr, N->getOperand(2)); 206 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, N->getOperand(2)); 207 208 // Handle endianness of the load. 209 if (TLI.isBigEndian()) 210 std::swap(Lo, Hi); 211 212 // Modified the chain - switch anything that used the old chain to use 213 // the new one. 214 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1)); 215} 216 217 218//===--------------------------------------------------------------------===// 219// Generic Operand Expansion. 220//===--------------------------------------------------------------------===// 221 222SDValue 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); 231 232 if (isTypeLegal(NVT)) { 233 SDValue Parts[2]; 234 GetExpandedOp(N->getOperand(0), Parts[0], Parts[1]); 235 236 if (TLI.isBigEndian()) 237 std::swap(Parts[0], Parts[1]); 238 239 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, NVT, Parts, 2); 240 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Vec); 241 } 242 } 243 244 // Otherwise, store to a temporary and load out again as the new type. 245 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0)); 246} 247 248SDValue 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); 254 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); 259 260 for (unsigned i = 0; i < NumElts; ++i) { 261 SDValue Lo, Hi; 262 GetExpandedOp(N->getOperand(i), Lo, Hi); 263 if (TLI.isBigEndian()) 264 std::swap(Lo, Hi); 265 NewElts.push_back(Lo); 266 NewElts.push_back(Hi); 267 } 268 269 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, 270 MVT::getVectorVT(NewVT, NewElts.size()), 271 &NewElts[0], NewElts.size()); 272 273 // Convert the new vector to the old vector type. 274 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec); 275} 276 277SDValue DAGTypeLegalizer::ExpandOp_EXTRACT_ELEMENT(SDNode *N) { 278 SDValue Lo, Hi; 279 GetExpandedOp(N->getOperand(0), Lo, Hi); 280 return cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? Hi : Lo; 281} 282 283SDValue 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(); 287 288 SDValue Val = N->getOperand(1); 289 MVT OldEVT = Val.getValueType(); 290 MVT NewEVT = TLI.getTypeToTransformTo(OldEVT); 291 292 assert(OldEVT == VecVT.getVectorElementType() && 293 "Inserted element type doesn't match vector element type!"); 294 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)); 299 300 SDValue Lo, Hi; 301 GetExpandedOp(Val, Lo, Hi); 302 if (TLI.isBigEndian()) 303 std::swap(Lo, Hi); 304 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); 310 311 // Convert the new vector to the old vector type. 312 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec); 313} 314 315SDValue 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) 322 Ops[i] = UndefVal; 323 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], NumElts); 324} 325 326SDValue 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"); 329 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(); 337 338 assert(NVT.isByteSized() && "Expanded type not byte sized!"); 339 unsigned IncrementSize = NVT.getSizeInBits() / 8; 340 341 SDValue Lo, Hi; 342 GetExpandedOp(St->getValue(), Lo, Hi); 343 344 if (TLI.isBigEndian()) 345 std::swap(Lo, Hi); 346 347 Lo = DAG.getStore(Chain, Lo, Ptr, St->getSrcValue(), SVOffset, 348 isVolatile, Alignment); 349 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)); 355 356 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); 357} 358 359 360//===--------------------------------------------------------------------===// 361// Generic Result Splitting. 362//===--------------------------------------------------------------------===// 363 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 367// little-endian). 368 369void 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. 373 unsigned i; 374 375 // The string of legal results gets turned into input operands, which have 376 // the same type. 377 for (i = 0; isTypeLegal(N->getValueType(i)); ++i) 378 ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i))); 379 380 // The first illegal result must be the one that needs to be expanded. 381 GetSplitOp(N->getOperand(i), Lo, Hi); 382 383 // Legalize the rest of the results into the input operands whether they are 384 // legal or not. 385 unsigned e = N->getNumValues(); 386 for (++i; i != e; ++i) 387 ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i))); 388} 389 390void DAGTypeLegalizer::SplitRes_SELECT(SDNode *N, SDValue &Lo, 391 SDValue &Hi) { 392 SDValue LL, LH, RL, RH; 393 GetSplitOp(N->getOperand(1), LL, LH); 394 GetSplitOp(N->getOperand(2), RL, RH); 395 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); 399} 400 401void DAGTypeLegalizer::SplitRes_SELECT_CC(SDNode *N, SDValue &Lo, 402 SDValue &Hi) { 403 SDValue LL, LH, RL, RH; 404 GetSplitOp(N->getOperand(2), LL, LH); 405 GetSplitOp(N->getOperand(3), RL, RH); 406 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)); 411} 412 413void DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDValue &Lo, SDValue &Hi) { 414 MVT LoVT, HiVT; 415 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT); 416 Lo = DAG.getNode(ISD::UNDEF, LoVT); 417 Hi = DAG.getNode(ISD::UNDEF, HiVT); 418} 419