LegalizeTypesGeneric.cpp revision 81311d9869e1a94fb9f044b0b921d0d2b736823e
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" 21using namespace llvm; 22 23//===----------------------------------------------------------------------===// 24// Generic Result Expansion. 25//===----------------------------------------------------------------------===// 26 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. 30 31void DAGTypeLegalizer::ExpandRes_BIT_CONVERT(SDNode *N, SDValue &Lo, 32 SDValue &Hi) { 33 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0)); 34 SDValue InOp = N->getOperand(0); 35 MVT InVT = InOp.getValueType(); 36 37 // Handle some special cases efficiently. 38 switch (getTypeAction(InVT)) { 39 default: 40 assert(false && "Unknown type action!"); 41 case Legal: 42 case PromoteInteger: 43 break; 44 case SoftenFloat: 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); 49 return; 50 case ExpandInteger: 51 case ExpandFloat: 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); 56 return; 57 case SplitVector: 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()) 62 std::swap(Lo, Hi); 63 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Lo); 64 Hi = DAG.getNode(ISD::BIT_CONVERT, NVT, Hi); 65 return; 66 } 67 break; 68 case ScalarizeVector: 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); 73 return; 74 } 75 76 // Lower the bit-convert to a store/load from the stack, then expand the load. 77 SDValue Op = CreateStackStoreLoad(InOp, N->getValueType(0)); 78 ExpandRes_NormalLoad(Op.getNode(), Lo, Hi); 79} 80 81void DAGTypeLegalizer::ExpandRes_BUILD_PAIR(SDNode *N, SDValue &Lo, 82 SDValue &Hi) { 83 // Return the operands. 84 Lo = N->getOperand(0); 85 Hi = N->getOperand(1); 86} 87 88void DAGTypeLegalizer::ExpandRes_EXTRACT_ELEMENT(SDNode *N, SDValue &Lo, 89 SDValue &Hi) { 90 GetExpandedOp(N->getOperand(0), Lo, Hi); 91 SDValue Part = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? 92 Hi : Lo; 93 94 assert(Part.getValueType() == N->getValueType(0) && 95 "Type twice as big as expanded type not itself expanded!"); 96 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0)); 97 98 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Part, 99 DAG.getConstant(0, TLI.getPointerTy())); 100 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Part, 101 DAG.getConstant(1, TLI.getPointerTy())); 102} 103 104void DAGTypeLegalizer::ExpandRes_EXTRACT_VECTOR_ELT(SDNode *N, SDValue &Lo, 105 SDValue &Hi) { 106 SDValue OldVec = N->getOperand(0); 107 unsigned OldElts = OldVec.getValueType().getVectorNumElements(); 108 109 // Convert to a vector of the expanded element type, for example 110 // <3 x i64> -> <6 x i32>. 111 MVT OldVT = N->getValueType(0); 112 MVT NewVT = TLI.getTypeToTransformTo(OldVT); 113 114 SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT, 115 MVT::getVectorVT(NewVT, 2*OldElts), 116 OldVec); 117 118 // Extract the elements at 2 * Idx and 2 * Idx + 1 from the new vector. 119 SDValue Idx = N->getOperand(1); 120 121 // Make sure the type of Idx is big enough to hold the new values. 122 if (Idx.getValueType().bitsLT(TLI.getPointerTy())) 123 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx); 124 125 Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, Idx); 126 Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, Idx); 127 128 Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, 129 DAG.getConstant(1, Idx.getValueType())); 130 Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, NewVec, Idx); 131 132 if (TLI.isBigEndian()) 133 std::swap(Lo, Hi); 134} 135 136void DAGTypeLegalizer::ExpandRes_NormalLoad(SDNode *N, SDValue &Lo, 137 SDValue &Hi) { 138 assert(ISD::isNormalLoad(N) && "This routine only for normal loads!"); 139 140 LoadSDNode *LD = cast<LoadSDNode>(N); 141 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0)); 142 SDValue Chain = LD->getChain(); 143 SDValue Ptr = LD->getBasePtr(); 144 int SVOffset = LD->getSrcValueOffset(); 145 unsigned Alignment = LD->getAlignment(); 146 bool isVolatile = LD->isVolatile(); 147 148 assert(NVT.isByteSized() && "Expanded type not byte sized!"); 149 150 Lo = DAG.getLoad(NVT, Chain, Ptr, LD->getSrcValue(), SVOffset, 151 isVolatile, Alignment); 152 153 // Increment the pointer to the other half. 154 unsigned IncrementSize = NVT.getSizeInBits() / 8; 155 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, 156 DAG.getIntPtrConstant(IncrementSize)); 157 Hi = DAG.getLoad(NVT, Chain, Ptr, LD->getSrcValue(), SVOffset+IncrementSize, 158 isVolatile, MinAlign(Alignment, IncrementSize)); 159 160 // Build a factor node to remember that this load is independent of the 161 // other one. 162 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), 163 Hi.getValue(1)); 164 165 // Handle endianness of the load. 166 if (TLI.isBigEndian()) 167 std::swap(Lo, Hi); 168 169 // Modified the chain - switch anything that used the old chain to use 170 // the new one. 171 ReplaceValueWith(SDValue(N, 1), Chain); 172} 173 174void DAGTypeLegalizer::ExpandRes_VAARG(SDNode *N, SDValue &Lo, SDValue &Hi) { 175 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0)); 176 SDValue Chain = N->getOperand(0); 177 SDValue Ptr = N->getOperand(1); 178 179 Lo = DAG.getVAArg(NVT, Chain, Ptr, N->getOperand(2)); 180 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, N->getOperand(2)); 181 182 // Handle endianness of the load. 183 if (TLI.isBigEndian()) 184 std::swap(Lo, Hi); 185 186 // Modified the chain - switch anything that used the old chain to use 187 // the new one. 188 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1)); 189} 190 191 192//===--------------------------------------------------------------------===// 193// Generic Operand Expansion. 194//===--------------------------------------------------------------------===// 195 196SDValue DAGTypeLegalizer::ExpandOp_BIT_CONVERT(SDNode *N) { 197 if (N->getValueType(0).isVector()) { 198 // An illegal expanding type is being converted to a legal vector type. 199 // Make a two element vector out of the expanded parts and convert that 200 // instead, but only if the new vector type is legal (otherwise there 201 // is no point, and it might create expansion loops). For example, on 202 // x86 this turns v1i64 = BIT_CONVERT i64 into v1i64 = BIT_CONVERT v2i32. 203 MVT OVT = N->getOperand(0).getValueType(); 204 MVT NVT = MVT::getVectorVT(TLI.getTypeToTransformTo(OVT), 2); 205 206 if (isTypeLegal(NVT)) { 207 SDValue Parts[2]; 208 GetExpandedOp(N->getOperand(0), Parts[0], Parts[1]); 209 210 if (TLI.isBigEndian()) 211 std::swap(Parts[0], Parts[1]); 212 213 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, NVT, Parts, 2); 214 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Vec); 215 } 216 } 217 218 // Otherwise, store to a temporary and load out again as the new type. 219 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0)); 220} 221 222SDValue DAGTypeLegalizer::ExpandOp_BUILD_VECTOR(SDNode *N) { 223 // The vector type is legal but the element type needs expansion. 224 MVT VecVT = N->getValueType(0); 225 unsigned NumElts = VecVT.getVectorNumElements(); 226 MVT OldVT = N->getOperand(0).getValueType(); 227 MVT NewVT = TLI.getTypeToTransformTo(OldVT); 228 229 // Build a vector of twice the length out of the expanded elements. 230 // For example <3 x i64> -> <6 x i32>. 231 std::vector<SDValue> NewElts; 232 NewElts.reserve(NumElts*2); 233 234 for (unsigned i = 0; i < NumElts; ++i) { 235 SDValue Lo, Hi; 236 GetExpandedOp(N->getOperand(i), Lo, Hi); 237 if (TLI.isBigEndian()) 238 std::swap(Lo, Hi); 239 NewElts.push_back(Lo); 240 NewElts.push_back(Hi); 241 } 242 243 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, 244 MVT::getVectorVT(NewVT, NewElts.size()), 245 &NewElts[0], NewElts.size()); 246 247 // Convert the new vector to the old vector type. 248 return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec); 249} 250 251SDValue DAGTypeLegalizer::ExpandOp_EXTRACT_ELEMENT(SDNode *N) { 252 SDValue Lo, Hi; 253 GetExpandedOp(N->getOperand(0), Lo, Hi); 254 return cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? Hi : Lo; 255} 256 257SDValue DAGTypeLegalizer::ExpandOp_NormalStore(SDNode *N, unsigned OpNo) { 258 assert(ISD::isNormalStore(N) && "This routine only for normal stores!"); 259 assert(OpNo == 1 && "Can only expand the stored value so far"); 260 261 StoreSDNode *St = cast<StoreSDNode>(N); 262 MVT NVT = TLI.getTypeToTransformTo(St->getValue().getValueType()); 263 SDValue Chain = St->getChain(); 264 SDValue Ptr = St->getBasePtr(); 265 int SVOffset = St->getSrcValueOffset(); 266 unsigned Alignment = St->getAlignment(); 267 bool isVolatile = St->isVolatile(); 268 269 assert(NVT.isByteSized() && "Expanded type not byte sized!"); 270 unsigned IncrementSize = NVT.getSizeInBits() / 8; 271 272 SDValue Lo, Hi; 273 GetExpandedOp(St->getValue(), Lo, Hi); 274 275 if (TLI.isBigEndian()) 276 std::swap(Lo, Hi); 277 278 Lo = DAG.getStore(Chain, Lo, Ptr, St->getSrcValue(), SVOffset, 279 isVolatile, Alignment); 280 281 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, 282 DAG.getIntPtrConstant(IncrementSize)); 283 assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!"); 284 Hi = DAG.getStore(Chain, Hi, Ptr, St->getSrcValue(), SVOffset + IncrementSize, 285 isVolatile, MinAlign(Alignment, IncrementSize)); 286 287 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); 288} 289 290 291//===--------------------------------------------------------------------===// 292// Generic Result Splitting. 293//===--------------------------------------------------------------------===// 294 295// Be careful to make no assumptions about which of Lo/Hi is stored first in 296// memory (for vectors it is always Lo first followed by Hi in the following 297// bytes; for integers and floats it is Lo first if and only if the machine is 298// little-endian). 299 300void DAGTypeLegalizer::SplitRes_MERGE_VALUES(SDNode *N, 301 SDValue &Lo, SDValue &Hi) { 302 // A MERGE_VALUES node can produce any number of values. We know that the 303 // first illegal one needs to be expanded into Lo/Hi. 304 unsigned i; 305 306 // The string of legal results gets turned into input operands, which have 307 // the same type. 308 for (i = 0; isTypeLegal(N->getValueType(i)); ++i) 309 ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i))); 310 311 // The first illegal result must be the one that needs to be expanded. 312 GetSplitOp(N->getOperand(i), Lo, Hi); 313 314 // Legalize the rest of the results into the input operands whether they are 315 // legal or not. 316 unsigned e = N->getNumValues(); 317 for (++i; i != e; ++i) 318 ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i))); 319} 320 321void DAGTypeLegalizer::SplitRes_SELECT(SDNode *N, SDValue &Lo, 322 SDValue &Hi) { 323 SDValue LL, LH, RL, RH; 324 GetSplitOp(N->getOperand(1), LL, LH); 325 GetSplitOp(N->getOperand(2), RL, RH); 326 327 SDValue Cond = N->getOperand(0); 328 Lo = DAG.getNode(ISD::SELECT, LL.getValueType(), Cond, LL, RL); 329 Hi = DAG.getNode(ISD::SELECT, LH.getValueType(), Cond, LH, RH); 330} 331 332void DAGTypeLegalizer::SplitRes_SELECT_CC(SDNode *N, SDValue &Lo, 333 SDValue &Hi) { 334 SDValue LL, LH, RL, RH; 335 GetSplitOp(N->getOperand(2), LL, LH); 336 GetSplitOp(N->getOperand(3), RL, RH); 337 338 Lo = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0), 339 N->getOperand(1), LL, RL, N->getOperand(4)); 340 Hi = DAG.getNode(ISD::SELECT_CC, LH.getValueType(), N->getOperand(0), 341 N->getOperand(1), LH, RH, N->getOperand(4)); 342} 343 344void DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDValue &Lo, SDValue &Hi) { 345 MVT LoVT, HiVT; 346 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT); 347 Lo = DAG.getNode(ISD::UNDEF, LoVT); 348 Hi = DAG.getNode(ISD::UNDEF, HiVT); 349} 350