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