LLVM API Documentation
00001 //===-------- LegalizeTypesGeneric.cpp - Generic type legalization --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements generic type expansion and splitting for LegalizeTypes. 00011 // The routines here perform legalization when the details of the type (such as 00012 // whether it is an integer or a float) do not matter. 00013 // Expansion is the act of changing a computation in an illegal type to be a 00014 // computation in two identical registers of a smaller type. 00015 // Splitting is the act of changing a computation in an illegal type to be a 00016 // computation in two not necessarily identical registers of a smaller type. 00017 // 00018 //===----------------------------------------------------------------------===// 00019 00020 #include "LegalizeTypes.h" 00021 #include "llvm/Target/TargetData.h" 00022 #include "llvm/CodeGen/PseudoSourceValue.h" 00023 using namespace llvm; 00024 00025 //===----------------------------------------------------------------------===// 00026 // Generic Result Expansion. 00027 //===----------------------------------------------------------------------===// 00028 00029 // These routines assume that the Lo/Hi part is stored first in memory on 00030 // little/big-endian machines, followed by the Hi/Lo part. This means that 00031 // they cannot be used as is on vectors, for which Lo is always stored first. 00032 00033 void DAGTypeLegalizer::ExpandRes_BIT_CONVERT(SDNode *N, SDValue &Lo, 00034 SDValue &Hi) { 00035 MVT OutVT = N->getValueType(0); 00036 MVT NOutVT = TLI.getTypeToTransformTo(OutVT); 00037 SDValue InOp = N->getOperand(0); 00038 MVT InVT = InOp.getValueType(); 00039 DebugLoc dl = N->getDebugLoc(); 00040 00041 // Handle some special cases efficiently. 00042 switch (getTypeAction(InVT)) { 00043 default: 00044 assert(false && "Unknown type action!"); 00045 case Legal: 00046 case PromoteInteger: 00047 break; 00048 case SoftenFloat: 00049 // Convert the integer operand instead. 00050 SplitInteger(GetSoftenedFloat(InOp), Lo, Hi); 00051 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo); 00052 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi); 00053 return; 00054 case ExpandInteger: 00055 case ExpandFloat: 00056 // Convert the expanded pieces of the input. 00057 GetExpandedOp(InOp, Lo, Hi); 00058 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo); 00059 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi); 00060 return; 00061 case SplitVector: 00062 // Convert the split parts of the input if it was split in two. 00063 GetSplitVector(InOp, Lo, Hi); 00064 if (Lo.getValueType() == Hi.getValueType()) { 00065 if (TLI.isBigEndian()) 00066 std::swap(Lo, Hi); 00067 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo); 00068 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi); 00069 return; 00070 } 00071 break; 00072 case ScalarizeVector: 00073 // Convert the element instead. 00074 SplitInteger(BitConvertToInteger(GetScalarizedVector(InOp)), Lo, Hi); 00075 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo); 00076 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi); 00077 return; 00078 } 00079 00080 // Lower the bit-convert to a store/load from the stack. 00081 assert(NOutVT.isByteSized() && "Expanded type not byte sized!"); 00082 00083 // Create the stack frame object. Make sure it is aligned for both 00084 // the source and expanded destination types. 00085 unsigned Alignment = 00086 TLI.getTargetData()->getPrefTypeAlignment(NOutVT.getTypeForMVT()); 00087 SDValue StackPtr = DAG.CreateStackTemporary(InVT, Alignment); 00088 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 00089 const Value *SV = PseudoSourceValue::getFixedStack(SPFI); 00090 00091 // Emit a store to the stack slot. 00092 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, InOp, StackPtr, SV, 0); 00093 00094 // Load the first half from the stack slot. 00095 Lo = DAG.getLoad(NOutVT, dl, Store, StackPtr, SV, 0); 00096 00097 // Increment the pointer to the other half. 00098 unsigned IncrementSize = NOutVT.getSizeInBits() / 8; 00099 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr, 00100 DAG.getIntPtrConstant(IncrementSize)); 00101 00102 // Load the second half from the stack slot. 00103 Hi = DAG.getLoad(NOutVT, dl, Store, StackPtr, SV, IncrementSize, false, 00104 MinAlign(Alignment, IncrementSize)); 00105 00106 // Handle endianness of the load. 00107 if (TLI.isBigEndian()) 00108 std::swap(Lo, Hi); 00109 } 00110 00111 void DAGTypeLegalizer::ExpandRes_BUILD_PAIR(SDNode *N, SDValue &Lo, 00112 SDValue &Hi) { 00113 // Return the operands. 00114 Lo = N->getOperand(0); 00115 Hi = N->getOperand(1); 00116 } 00117 00118 void DAGTypeLegalizer::ExpandRes_EXTRACT_ELEMENT(SDNode *N, SDValue &Lo, 00119 SDValue &Hi) { 00120 GetExpandedOp(N->getOperand(0), Lo, Hi); 00121 SDValue Part = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? 00122 Hi : Lo; 00123 DebugLoc dl = N->getDebugLoc(); 00124 00125 assert(Part.getValueType() == N->getValueType(0) && 00126 "Type twice as big as expanded type not itself expanded!"); 00127 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0)); 00128 00129 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NVT, Part, 00130 DAG.getConstant(0, TLI.getPointerTy())); 00131 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NVT, Part, 00132 DAG.getConstant(1, TLI.getPointerTy())); 00133 } 00134 00135 void DAGTypeLegalizer::ExpandRes_EXTRACT_VECTOR_ELT(SDNode *N, SDValue &Lo, 00136 SDValue &Hi) { 00137 SDValue OldVec = N->getOperand(0); 00138 unsigned OldElts = OldVec.getValueType().getVectorNumElements(); 00139 DebugLoc dl = N->getDebugLoc(); 00140 00141 // Convert to a vector of the expanded element type, for example 00142 // <3 x i64> -> <6 x i32>. 00143 MVT OldVT = N->getValueType(0); 00144 MVT NewVT = TLI.getTypeToTransformTo(OldVT); 00145 00146 SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT, dl, 00147 MVT::getVectorVT(NewVT, 2*OldElts), 00148 OldVec); 00149 00150 // Extract the elements at 2 * Idx and 2 * Idx + 1 from the new vector. 00151 SDValue Idx = N->getOperand(1); 00152 00153 // Make sure the type of Idx is big enough to hold the new values. 00154 if (Idx.getValueType().bitsLT(TLI.getPointerTy())) 00155 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx); 00156 00157 Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, Idx); 00158 Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, NewVec, Idx); 00159 00160 Idx = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, 00161 DAG.getConstant(1, Idx.getValueType())); 00162 Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, NewVec, Idx); 00163 00164 if (TLI.isBigEndian()) 00165 std::swap(Lo, Hi); 00166 } 00167 00168 void DAGTypeLegalizer::ExpandRes_NormalLoad(SDNode *N, SDValue &Lo, 00169 SDValue &Hi) { 00170 assert(ISD::isNormalLoad(N) && "This routine only for normal loads!"); 00171 DebugLoc dl = N->getDebugLoc(); 00172 00173 LoadSDNode *LD = cast<LoadSDNode>(N); 00174 MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0)); 00175 SDValue Chain = LD->getChain(); 00176 SDValue Ptr = LD->getBasePtr(); 00177 int SVOffset = LD->getSrcValueOffset(); 00178 unsigned Alignment = LD->getAlignment(); 00179 bool isVolatile = LD->isVolatile(); 00180 00181 assert(NVT.isByteSized() && "Expanded type not byte sized!"); 00182 00183 Lo = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getSrcValue(), SVOffset, 00184 isVolatile, Alignment); 00185 00186 // Increment the pointer to the other half. 00187 unsigned IncrementSize = NVT.getSizeInBits() / 8; 00188 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 00189 DAG.getIntPtrConstant(IncrementSize)); 00190 Hi = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getSrcValue(), 00191 SVOffset+IncrementSize, 00192 isVolatile, MinAlign(Alignment, IncrementSize)); 00193 00194 // Build a factor node to remember that this load is independent of the 00195 // other one. 00196 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 00197 Hi.getValue(1)); 00198 00199 // Handle endianness of the load. 00200 if (TLI.isBigEndian()) 00201 std::swap(Lo, Hi); 00202 00203 // Modified the chain - switch anything that used the old chain to use 00204 // the new one. 00205 ReplaceValueWith(SDValue(N, 1), Chain); 00206 } 00207 00208 void DAGTypeLegalizer::ExpandRes_VAARG(SDNode *N, SDValue &Lo, SDValue &Hi) { 00209 MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0)); 00210 SDValue Chain = N->getOperand(0); 00211 SDValue Ptr = N->getOperand(1); 00212 00213 Lo = DAG.getVAArg(NVT, Chain, Ptr, N->getOperand(2)); 00214 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, N->getOperand(2)); 00215 00216 // Handle endianness of the load. 00217 if (TLI.isBigEndian()) 00218 std::swap(Lo, Hi); 00219 00220 // Modified the chain - switch anything that used the old chain to use 00221 // the new one. 00222 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1)); 00223 } 00224 00225 00226 //===--------------------------------------------------------------------===// 00227 // Generic Operand Expansion. 00228 //===--------------------------------------------------------------------===// 00229 00230 SDValue DAGTypeLegalizer::ExpandOp_BIT_CONVERT(SDNode *N) { 00231 DebugLoc dl = N->getDebugLoc(); 00232 if (N->getValueType(0).isVector()) { 00233 // An illegal expanding type is being converted to a legal vector type. 00234 // Make a two element vector out of the expanded parts and convert that 00235 // instead, but only if the new vector type is legal (otherwise there 00236 // is no point, and it might create expansion loops). For example, on 00237 // x86 this turns v1i64 = BIT_CONVERT i64 into v1i64 = BIT_CONVERT v2i32. 00238 MVT OVT = N->getOperand(0).getValueType(); 00239 MVT NVT = MVT::getVectorVT(TLI.getTypeToTransformTo(OVT), 2); 00240 00241 if (isTypeLegal(NVT)) { 00242 SDValue Parts[2]; 00243 GetExpandedOp(N->getOperand(0), Parts[0], Parts[1]); 00244 00245 if (TLI.isBigEndian()) 00246 std::swap(Parts[0], Parts[1]); 00247 00248 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, Parts, 2); 00249 return DAG.getNode(ISD::BIT_CONVERT, dl, N->getValueType(0), Vec); 00250 } 00251 } 00252 00253 // Otherwise, store to a temporary and load out again as the new type. 00254 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0)); 00255 } 00256 00257 SDValue DAGTypeLegalizer::ExpandOp_BUILD_VECTOR(SDNode *N) { 00258 // The vector type is legal but the element type needs expansion. 00259 MVT VecVT = N->getValueType(0); 00260 unsigned NumElts = VecVT.getVectorNumElements(); 00261 MVT OldVT = N->getOperand(0).getValueType(); 00262 MVT NewVT = TLI.getTypeToTransformTo(OldVT); 00263 DebugLoc dl = N->getDebugLoc(); 00264 00265 // Build a vector of twice the length out of the expanded elements. 00266 // For example <3 x i64> -> <6 x i32>. 00267 std::vector<SDValue> NewElts; 00268 NewElts.reserve(NumElts*2); 00269 00270 for (unsigned i = 0; i < NumElts; ++i) { 00271 SDValue Lo, Hi; 00272 GetExpandedOp(N->getOperand(i), Lo, Hi); 00273 if (TLI.isBigEndian()) 00274 std::swap(Lo, Hi); 00275 NewElts.push_back(Lo); 00276 NewElts.push_back(Hi); 00277 } 00278 00279 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, dl, 00280 MVT::getVectorVT(NewVT, NewElts.size()), 00281 &NewElts[0], NewElts.size()); 00282 00283 // Convert the new vector to the old vector type. 00284 return DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, NewVec); 00285 } 00286 00287 SDValue DAGTypeLegalizer::ExpandOp_EXTRACT_ELEMENT(SDNode *N) { 00288 SDValue Lo, Hi; 00289 GetExpandedOp(N->getOperand(0), Lo, Hi); 00290 return cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? Hi : Lo; 00291 } 00292 00293 SDValue DAGTypeLegalizer::ExpandOp_INSERT_VECTOR_ELT(SDNode *N) { 00294 // The vector type is legal but the element type needs expansion. 00295 MVT VecVT = N->getValueType(0); 00296 unsigned NumElts = VecVT.getVectorNumElements(); 00297 DebugLoc dl = N->getDebugLoc(); 00298 00299 SDValue Val = N->getOperand(1); 00300 MVT OldEVT = Val.getValueType(); 00301 MVT NewEVT = TLI.getTypeToTransformTo(OldEVT); 00302 00303 assert(OldEVT == VecVT.getVectorElementType() && 00304 "Inserted element type doesn't match vector element type!"); 00305 00306 // Bitconvert to a vector of twice the length with elements of the expanded 00307 // type, insert the expanded vector elements, and then convert back. 00308 MVT NewVecVT = MVT::getVectorVT(NewEVT, NumElts*2); 00309 SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT, dl, 00310 NewVecVT, N->getOperand(0)); 00311 00312 SDValue Lo, Hi; 00313 GetExpandedOp(Val, Lo, Hi); 00314 if (TLI.isBigEndian()) 00315 std::swap(Lo, Hi); 00316 00317 SDValue Idx = N->getOperand(2); 00318 Idx = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, Idx); 00319 NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVecVT, NewVec, Lo, Idx); 00320 Idx = DAG.getNode(ISD::ADD, dl, 00321 Idx.getValueType(), Idx, DAG.getIntPtrConstant(1)); 00322 NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVecVT, NewVec, Hi, Idx); 00323 00324 // Convert the new vector to the old vector type. 00325 return DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, NewVec); 00326 } 00327 00328 SDValue DAGTypeLegalizer::ExpandOp_SCALAR_TO_VECTOR(SDNode *N) { 00329 DebugLoc dl = N->getDebugLoc(); 00330 MVT VT = N->getValueType(0); 00331 unsigned NumElts = VT.getVectorNumElements(); 00332 SmallVector<SDValue, 16> Ops(NumElts); 00333 Ops[0] = N->getOperand(0); 00334 SDValue UndefVal = DAG.getNode(ISD::UNDEF, dl, Ops[0].getValueType()); 00335 for (unsigned i = 1; i < NumElts; ++i) 00336 Ops[i] = UndefVal; 00337 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElts); 00338 } 00339 00340 SDValue DAGTypeLegalizer::ExpandOp_NormalStore(SDNode *N, unsigned OpNo) { 00341 assert(ISD::isNormalStore(N) && "This routine only for normal stores!"); 00342 assert(OpNo == 1 && "Can only expand the stored value so far"); 00343 DebugLoc dl = N->getDebugLoc(); 00344 00345 StoreSDNode *St = cast<StoreSDNode>(N); 00346 MVT NVT = TLI.getTypeToTransformTo(St->getValue().getValueType()); 00347 SDValue Chain = St->getChain(); 00348 SDValue Ptr = St->getBasePtr(); 00349 int SVOffset = St->getSrcValueOffset(); 00350 unsigned Alignment = St->getAlignment(); 00351 bool isVolatile = St->isVolatile(); 00352 00353 assert(NVT.isByteSized() && "Expanded type not byte sized!"); 00354 unsigned IncrementSize = NVT.getSizeInBits() / 8; 00355 00356 SDValue Lo, Hi; 00357 GetExpandedOp(St->getValue(), Lo, Hi); 00358 00359 if (TLI.isBigEndian()) 00360 std::swap(Lo, Hi); 00361 00362 Lo = DAG.getStore(Chain, dl, Lo, Ptr, St->getSrcValue(), SVOffset, 00363 isVolatile, Alignment); 00364 00365 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 00366 DAG.getIntPtrConstant(IncrementSize)); 00367 assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!"); 00368 Hi = DAG.getStore(Chain, dl, Hi, Ptr, St->getSrcValue(), 00369 SVOffset + IncrementSize, 00370 isVolatile, MinAlign(Alignment, IncrementSize)); 00371 00372 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 00373 } 00374 00375 00376 //===--------------------------------------------------------------------===// 00377 // Generic Result Splitting. 00378 //===--------------------------------------------------------------------===// 00379 00380 // Be careful to make no assumptions about which of Lo/Hi is stored first in 00381 // memory (for vectors it is always Lo first followed by Hi in the following 00382 // bytes; for integers and floats it is Lo first if and only if the machine is 00383 // little-endian). 00384 00385 void DAGTypeLegalizer::SplitRes_MERGE_VALUES(SDNode *N, 00386 SDValue &Lo, SDValue &Hi) { 00387 // A MERGE_VALUES node can produce any number of values. We know that the 00388 // first illegal one needs to be expanded into Lo/Hi. 00389 unsigned i; 00390 00391 // The string of legal results gets turned into input operands, which have 00392 // the same type. 00393 for (i = 0; isTypeLegal(N->getValueType(i)); ++i) 00394 ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i))); 00395 00396 // The first illegal result must be the one that needs to be expanded. 00397 GetSplitOp(N->getOperand(i), Lo, Hi); 00398 00399 // Legalize the rest of the results into the input operands whether they are 00400 // legal or not. 00401 unsigned e = N->getNumValues(); 00402 for (++i; i != e; ++i) 00403 ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i))); 00404 } 00405 00406 void DAGTypeLegalizer::SplitRes_SELECT(SDNode *N, SDValue &Lo, 00407 SDValue &Hi) { 00408 SDValue LL, LH, RL, RH; 00409 DebugLoc dl = N->getDebugLoc(); 00410 GetSplitOp(N->getOperand(1), LL, LH); 00411 GetSplitOp(N->getOperand(2), RL, RH); 00412 00413 SDValue Cond = N->getOperand(0); 00414 Lo = DAG.getNode(ISD::SELECT, dl, LL.getValueType(), Cond, LL, RL); 00415 Hi = DAG.getNode(ISD::SELECT, dl, LH.getValueType(), Cond, LH, RH); 00416 } 00417 00418 void DAGTypeLegalizer::SplitRes_SELECT_CC(SDNode *N, SDValue &Lo, 00419 SDValue &Hi) { 00420 SDValue LL, LH, RL, RH; 00421 DebugLoc dl = N->getDebugLoc(); 00422 GetSplitOp(N->getOperand(2), LL, LH); 00423 GetSplitOp(N->getOperand(3), RL, RH); 00424 00425 Lo = DAG.getNode(ISD::SELECT_CC, dl, LL.getValueType(), N->getOperand(0), 00426 N->getOperand(1), LL, RL, N->getOperand(4)); 00427 Hi = DAG.getNode(ISD::SELECT_CC, dl, LH.getValueType(), N->getOperand(0), 00428 N->getOperand(1), LH, RH, N->getOperand(4)); 00429 } 00430 00431 void DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDValue &Lo, SDValue &Hi) { 00432 MVT LoVT, HiVT; 00433 DebugLoc dl = N->getDebugLoc(); 00434 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT); 00435 Lo = DAG.getNode(ISD::UNDEF, dl, LoVT); 00436 Hi = DAG.getNode(ISD::UNDEF, dl, HiVT); 00437 }