LLVM API Documentation
00001 //===----- LegalizeIntegerTypes.cpp - Legalization of integer types -------===// 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 integer type expansion and promotion for LegalizeTypes. 00011 // Promotion is the act of changing a computation in an illegal type into a 00012 // computation in a larger type. For example, implementing i8 arithmetic in an 00013 // i32 register (often needed on powerpc). 00014 // Expansion is the act of changing a computation in an illegal type into a 00015 // computation in two identical registers of a smaller type. For example, 00016 // implementing i64 arithmetic in two i32 registers (often needed on 32-bit 00017 // targets). 00018 // 00019 //===----------------------------------------------------------------------===// 00020 00021 #include "LegalizeTypes.h" 00022 #include "llvm/IR/DerivedTypes.h" 00023 #include "llvm/Support/ErrorHandling.h" 00024 #include "llvm/Support/raw_ostream.h" 00025 using namespace llvm; 00026 00027 #define DEBUG_TYPE "legalize-types" 00028 00029 //===----------------------------------------------------------------------===// 00030 // Integer Result Promotion 00031 //===----------------------------------------------------------------------===// 00032 00033 /// PromoteIntegerResult - This method is called when a result of a node is 00034 /// found to be in need of promotion to a larger type. At this point, the node 00035 /// may also have invalid operands or may have other results that need 00036 /// expansion, we just know that (at least) one result needs promotion. 00037 void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) { 00038 DEBUG(dbgs() << "Promote integer result: "; N->dump(&DAG); dbgs() << "\n"); 00039 SDValue Res = SDValue(); 00040 00041 // See if the target wants to custom expand this node. 00042 if (CustomLowerNode(N, N->getValueType(ResNo), true)) 00043 return; 00044 00045 switch (N->getOpcode()) { 00046 default: 00047 #ifndef NDEBUG 00048 dbgs() << "PromoteIntegerResult #" << ResNo << ": "; 00049 N->dump(&DAG); dbgs() << "\n"; 00050 #endif 00051 llvm_unreachable("Do not know how to promote this operator!"); 00052 case ISD::MERGE_VALUES:Res = PromoteIntRes_MERGE_VALUES(N, ResNo); break; 00053 case ISD::AssertSext: Res = PromoteIntRes_AssertSext(N); break; 00054 case ISD::AssertZext: Res = PromoteIntRes_AssertZext(N); break; 00055 case ISD::BITCAST: Res = PromoteIntRes_BITCAST(N); break; 00056 case ISD::BSWAP: Res = PromoteIntRes_BSWAP(N); break; 00057 case ISD::BUILD_PAIR: Res = PromoteIntRes_BUILD_PAIR(N); break; 00058 case ISD::Constant: Res = PromoteIntRes_Constant(N); break; 00059 case ISD::CONVERT_RNDSAT: 00060 Res = PromoteIntRes_CONVERT_RNDSAT(N); break; 00061 case ISD::CTLZ_ZERO_UNDEF: 00062 case ISD::CTLZ: Res = PromoteIntRes_CTLZ(N); break; 00063 case ISD::CTPOP: Res = PromoteIntRes_CTPOP(N); break; 00064 case ISD::CTTZ_ZERO_UNDEF: 00065 case ISD::CTTZ: Res = PromoteIntRes_CTTZ(N); break; 00066 case ISD::EXTRACT_VECTOR_ELT: 00067 Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break; 00068 case ISD::LOAD: Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N));break; 00069 case ISD::SELECT: Res = PromoteIntRes_SELECT(N); break; 00070 case ISD::VSELECT: Res = PromoteIntRes_VSELECT(N); break; 00071 case ISD::SELECT_CC: Res = PromoteIntRes_SELECT_CC(N); break; 00072 case ISD::SETCC: Res = PromoteIntRes_SETCC(N); break; 00073 case ISD::SHL: Res = PromoteIntRes_SHL(N); break; 00074 case ISD::SIGN_EXTEND_INREG: 00075 Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break; 00076 case ISD::SRA: Res = PromoteIntRes_SRA(N); break; 00077 case ISD::SRL: Res = PromoteIntRes_SRL(N); break; 00078 case ISD::TRUNCATE: Res = PromoteIntRes_TRUNCATE(N); break; 00079 case ISD::UNDEF: Res = PromoteIntRes_UNDEF(N); break; 00080 case ISD::VAARG: Res = PromoteIntRes_VAARG(N); break; 00081 00082 case ISD::EXTRACT_SUBVECTOR: 00083 Res = PromoteIntRes_EXTRACT_SUBVECTOR(N); break; 00084 case ISD::VECTOR_SHUFFLE: 00085 Res = PromoteIntRes_VECTOR_SHUFFLE(N); break; 00086 case ISD::INSERT_VECTOR_ELT: 00087 Res = PromoteIntRes_INSERT_VECTOR_ELT(N); break; 00088 case ISD::BUILD_VECTOR: 00089 Res = PromoteIntRes_BUILD_VECTOR(N); break; 00090 case ISD::SCALAR_TO_VECTOR: 00091 Res = PromoteIntRes_SCALAR_TO_VECTOR(N); break; 00092 case ISD::CONCAT_VECTORS: 00093 Res = PromoteIntRes_CONCAT_VECTORS(N); break; 00094 00095 case ISD::SIGN_EXTEND: 00096 case ISD::ZERO_EXTEND: 00097 case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break; 00098 00099 case ISD::FP_TO_SINT: 00100 case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break; 00101 00102 case ISD::FP_TO_FP16: Res = PromoteIntRes_FP_TO_FP16(N); break; 00103 00104 case ISD::AND: 00105 case ISD::OR: 00106 case ISD::XOR: 00107 case ISD::ADD: 00108 case ISD::SUB: 00109 case ISD::MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break; 00110 00111 case ISD::SDIV: 00112 case ISD::SREM: Res = PromoteIntRes_SDIV(N); break; 00113 00114 case ISD::UDIV: 00115 case ISD::UREM: Res = PromoteIntRes_UDIV(N); break; 00116 00117 case ISD::SADDO: 00118 case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break; 00119 case ISD::UADDO: 00120 case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break; 00121 case ISD::SMULO: 00122 case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break; 00123 00124 case ISD::ATOMIC_LOAD: 00125 Res = PromoteIntRes_Atomic0(cast<AtomicSDNode>(N)); break; 00126 00127 case ISD::ATOMIC_LOAD_ADD: 00128 case ISD::ATOMIC_LOAD_SUB: 00129 case ISD::ATOMIC_LOAD_AND: 00130 case ISD::ATOMIC_LOAD_OR: 00131 case ISD::ATOMIC_LOAD_XOR: 00132 case ISD::ATOMIC_LOAD_NAND: 00133 case ISD::ATOMIC_LOAD_MIN: 00134 case ISD::ATOMIC_LOAD_MAX: 00135 case ISD::ATOMIC_LOAD_UMIN: 00136 case ISD::ATOMIC_LOAD_UMAX: 00137 case ISD::ATOMIC_SWAP: 00138 Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break; 00139 00140 case ISD::ATOMIC_CMP_SWAP: 00141 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: 00142 Res = PromoteIntRes_AtomicCmpSwap(cast<AtomicSDNode>(N), ResNo); 00143 break; 00144 } 00145 00146 // If the result is null then the sub-method took care of registering it. 00147 if (Res.getNode()) 00148 SetPromotedInteger(SDValue(N, ResNo), Res); 00149 } 00150 00151 SDValue DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode *N, 00152 unsigned ResNo) { 00153 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo); 00154 return GetPromotedInteger(Op); 00155 } 00156 00157 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) { 00158 // Sign-extend the new bits, and continue the assertion. 00159 SDValue Op = SExtPromotedInteger(N->getOperand(0)); 00160 return DAG.getNode(ISD::AssertSext, SDLoc(N), 00161 Op.getValueType(), Op, N->getOperand(1)); 00162 } 00163 00164 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) { 00165 // Zero the new bits, and continue the assertion. 00166 SDValue Op = ZExtPromotedInteger(N->getOperand(0)); 00167 return DAG.getNode(ISD::AssertZext, SDLoc(N), 00168 Op.getValueType(), Op, N->getOperand(1)); 00169 } 00170 00171 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode *N) { 00172 EVT ResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 00173 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N), 00174 N->getMemoryVT(), ResVT, 00175 N->getChain(), N->getBasePtr(), 00176 N->getMemOperand(), N->getOrdering(), 00177 N->getSynchScope()); 00178 // Legalized the chain result - switch anything that used the old chain to 00179 // use the new one. 00180 ReplaceValueWith(SDValue(N, 1), Res.getValue(1)); 00181 return Res; 00182 } 00183 00184 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) { 00185 SDValue Op2 = GetPromotedInteger(N->getOperand(2)); 00186 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N), 00187 N->getMemoryVT(), 00188 N->getChain(), N->getBasePtr(), 00189 Op2, N->getMemOperand(), N->getOrdering(), 00190 N->getSynchScope()); 00191 // Legalized the chain result - switch anything that used the old chain to 00192 // use the new one. 00193 ReplaceValueWith(SDValue(N, 1), Res.getValue(1)); 00194 return Res; 00195 } 00196 00197 SDValue DAGTypeLegalizer::PromoteIntRes_AtomicCmpSwap(AtomicSDNode *N, 00198 unsigned ResNo) { 00199 if (ResNo == 1) { 00200 assert(N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS); 00201 EVT SVT = getSetCCResultType(N->getOperand(2).getValueType()); 00202 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1)); 00203 00204 // Only use the result of getSetCCResultType if it is legal, 00205 // otherwise just use the promoted result type (NVT). 00206 if (!TLI.isTypeLegal(SVT)) 00207 SVT = NVT; 00208 00209 SDVTList VTs = DAG.getVTList(N->getValueType(0), SVT, MVT::Other); 00210 SDValue Res = DAG.getAtomicCmpSwap( 00211 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, SDLoc(N), N->getMemoryVT(), VTs, 00212 N->getChain(), N->getBasePtr(), N->getOperand(2), N->getOperand(3), 00213 N->getMemOperand(), N->getSuccessOrdering(), N->getFailureOrdering(), 00214 N->getSynchScope()); 00215 ReplaceValueWith(SDValue(N, 0), Res.getValue(0)); 00216 ReplaceValueWith(SDValue(N, 2), Res.getValue(2)); 00217 return Res.getValue(1); 00218 } 00219 00220 SDValue Op2 = GetPromotedInteger(N->getOperand(2)); 00221 SDValue Op3 = GetPromotedInteger(N->getOperand(3)); 00222 SDVTList VTs = 00223 DAG.getVTList(Op2.getValueType(), N->getValueType(1), MVT::Other); 00224 SDValue Res = DAG.getAtomicCmpSwap( 00225 N->getOpcode(), SDLoc(N), N->getMemoryVT(), VTs, N->getChain(), 00226 N->getBasePtr(), Op2, Op3, N->getMemOperand(), N->getSuccessOrdering(), 00227 N->getFailureOrdering(), N->getSynchScope()); 00228 // Update the use to N with the newly created Res. 00229 for (unsigned i = 1, NumResults = N->getNumValues(); i < NumResults; ++i) 00230 ReplaceValueWith(SDValue(N, i), Res.getValue(i)); 00231 return Res; 00232 } 00233 00234 SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) { 00235 SDValue InOp = N->getOperand(0); 00236 EVT InVT = InOp.getValueType(); 00237 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT); 00238 EVT OutVT = N->getValueType(0); 00239 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT); 00240 SDLoc dl(N); 00241 00242 switch (getTypeAction(InVT)) { 00243 case TargetLowering::TypeLegal: 00244 break; 00245 case TargetLowering::TypePromoteInteger: 00246 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector() && !NInVT.isVector()) 00247 // The input promotes to the same size. Convert the promoted value. 00248 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp)); 00249 break; 00250 case TargetLowering::TypeSoftenFloat: 00251 // Promote the integer operand by hand. 00252 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp)); 00253 case TargetLowering::TypeExpandInteger: 00254 case TargetLowering::TypeExpandFloat: 00255 break; 00256 case TargetLowering::TypeScalarizeVector: 00257 // Convert the element to an integer and promote it by hand. 00258 if (!NOutVT.isVector()) 00259 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, 00260 BitConvertToInteger(GetScalarizedVector(InOp))); 00261 break; 00262 case TargetLowering::TypeSplitVector: { 00263 // For example, i32 = BITCAST v2i16 on alpha. Convert the split 00264 // pieces of the input into integers and reassemble in the final type. 00265 SDValue Lo, Hi; 00266 GetSplitVector(N->getOperand(0), Lo, Hi); 00267 Lo = BitConvertToInteger(Lo); 00268 Hi = BitConvertToInteger(Hi); 00269 00270 if (TLI.isBigEndian()) 00271 std::swap(Lo, Hi); 00272 00273 InOp = DAG.getNode(ISD::ANY_EXTEND, dl, 00274 EVT::getIntegerVT(*DAG.getContext(), 00275 NOutVT.getSizeInBits()), 00276 JoinIntegers(Lo, Hi)); 00277 return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp); 00278 } 00279 case TargetLowering::TypeWidenVector: 00280 // The input is widened to the same size. Convert to the widened value. 00281 // Make sure that the outgoing value is not a vector, because this would 00282 // make us bitcast between two vectors which are legalized in different ways. 00283 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector()) 00284 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetWidenedVector(InOp)); 00285 } 00286 00287 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, 00288 CreateStackStoreLoad(InOp, OutVT)); 00289 } 00290 00291 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) { 00292 SDValue Op = GetPromotedInteger(N->getOperand(0)); 00293 EVT OVT = N->getValueType(0); 00294 EVT NVT = Op.getValueType(); 00295 SDLoc dl(N); 00296 00297 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(); 00298 return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op), 00299 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT))); 00300 } 00301 00302 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) { 00303 // The pair element type may be legal, or may not promote to the same type as 00304 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases. 00305 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), 00306 TLI.getTypeToTransformTo(*DAG.getContext(), 00307 N->getValueType(0)), JoinIntegers(N->getOperand(0), 00308 N->getOperand(1))); 00309 } 00310 00311 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) { 00312 EVT VT = N->getValueType(0); 00313 // FIXME there is no actual debug info here 00314 SDLoc dl(N); 00315 // Zero extend things like i1, sign extend everything else. It shouldn't 00316 // matter in theory which one we pick, but this tends to give better code? 00317 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 00318 SDValue Result = DAG.getNode(Opc, dl, 00319 TLI.getTypeToTransformTo(*DAG.getContext(), VT), 00320 SDValue(N, 0)); 00321 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?"); 00322 return Result; 00323 } 00324 00325 SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) { 00326 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode(); 00327 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU || 00328 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU || 00329 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) && 00330 "can only promote integers"); 00331 EVT OutVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 00332 return DAG.getConvertRndSat(OutVT, SDLoc(N), N->getOperand(0), 00333 N->getOperand(1), N->getOperand(2), 00334 N->getOperand(3), N->getOperand(4), CvtCode); 00335 } 00336 00337 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) { 00338 // Zero extend to the promoted type and do the count there. 00339 SDValue Op = ZExtPromotedInteger(N->getOperand(0)); 00340 SDLoc dl(N); 00341 EVT OVT = N->getValueType(0); 00342 EVT NVT = Op.getValueType(); 00343 Op = DAG.getNode(N->getOpcode(), dl, NVT, Op); 00344 // Subtract off the extra leading bits in the bigger type. 00345 return DAG.getNode( 00346 ISD::SUB, dl, NVT, Op, 00347 DAG.getConstant(NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(), 00348 NVT)); 00349 } 00350 00351 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) { 00352 // Zero extend to the promoted type and do the count there. 00353 SDValue Op = ZExtPromotedInteger(N->getOperand(0)); 00354 return DAG.getNode(ISD::CTPOP, SDLoc(N), Op.getValueType(), Op); 00355 } 00356 00357 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) { 00358 SDValue Op = GetPromotedInteger(N->getOperand(0)); 00359 EVT OVT = N->getValueType(0); 00360 EVT NVT = Op.getValueType(); 00361 SDLoc dl(N); 00362 if (N->getOpcode() == ISD::CTTZ) { 00363 // The count is the same in the promoted type except if the original 00364 // value was zero. This can be handled by setting the bit just off 00365 // the top of the original type. 00366 auto TopBit = APInt::getOneBitSet(NVT.getScalarSizeInBits(), 00367 OVT.getScalarSizeInBits()); 00368 Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, NVT)); 00369 } 00370 return DAG.getNode(N->getOpcode(), dl, NVT, Op); 00371 } 00372 00373 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) { 00374 SDLoc dl(N); 00375 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 00376 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, N->getOperand(0), 00377 N->getOperand(1)); 00378 } 00379 00380 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) { 00381 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 00382 unsigned NewOpc = N->getOpcode(); 00383 SDLoc dl(N); 00384 00385 // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is 00386 // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT 00387 // and SINT conversions are Custom, there is no way to tell which is 00388 // preferable. We choose SINT because that's the right thing on PPC.) 00389 if (N->getOpcode() == ISD::FP_TO_UINT && 00390 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && 00391 TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT)) 00392 NewOpc = ISD::FP_TO_SINT; 00393 00394 SDValue Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0)); 00395 00396 // Assert that the converted value fits in the original type. If it doesn't 00397 // (eg: because the value being converted is too big), then the result of the 00398 // original operation was undefined anyway, so the assert is still correct. 00399 return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ? 00400 ISD::AssertZext : ISD::AssertSext, dl, NVT, Res, 00401 DAG.getValueType(N->getValueType(0).getScalarType())); 00402 } 00403 00404 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_FP16(SDNode *N) { 00405 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 00406 SDLoc dl(N); 00407 00408 SDValue Res = DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0)); 00409 00410 return DAG.getNode(ISD::AssertZext, dl, 00411 NVT, Res, DAG.getValueType(N->getValueType(0))); 00412 } 00413 00414 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) { 00415 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 00416 SDLoc dl(N); 00417 00418 if (getTypeAction(N->getOperand(0).getValueType()) 00419 == TargetLowering::TypePromoteInteger) { 00420 SDValue Res = GetPromotedInteger(N->getOperand(0)); 00421 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!"); 00422 00423 // If the result and operand types are the same after promotion, simplify 00424 // to an in-register extension. 00425 if (NVT == Res.getValueType()) { 00426 // The high bits are not guaranteed to be anything. Insert an extend. 00427 if (N->getOpcode() == ISD::SIGN_EXTEND) 00428 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res, 00429 DAG.getValueType(N->getOperand(0).getValueType())); 00430 if (N->getOpcode() == ISD::ZERO_EXTEND) 00431 return DAG.getZeroExtendInReg(Res, dl, 00432 N->getOperand(0).getValueType().getScalarType()); 00433 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!"); 00434 return Res; 00435 } 00436 } 00437 00438 // Otherwise, just extend the original operand all the way to the larger type. 00439 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0)); 00440 } 00441 00442 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) { 00443 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!"); 00444 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 00445 ISD::LoadExtType ExtType = 00446 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType(); 00447 SDLoc dl(N); 00448 SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(), 00449 N->getMemoryVT(), N->getMemOperand()); 00450 00451 // Legalized the chain result - switch anything that used the old chain to 00452 // use the new one. 00453 ReplaceValueWith(SDValue(N, 1), Res.getValue(1)); 00454 return Res; 00455 } 00456 00457 /// Promote the overflow flag of an overflowing arithmetic node. 00458 SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) { 00459 // Simply change the return type of the boolean result. 00460 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1)); 00461 EVT ValueVTs[] = { N->getValueType(0), NVT }; 00462 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) }; 00463 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N), 00464 DAG.getVTList(ValueVTs), Ops); 00465 00466 // Modified the sum result - switch anything that used the old sum to use 00467 // the new one. 00468 ReplaceValueWith(SDValue(N, 0), Res); 00469 00470 return SDValue(Res.getNode(), 1); 00471 } 00472 00473 SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) { 00474 if (ResNo == 1) 00475 return PromoteIntRes_Overflow(N); 00476 00477 // The operation overflowed iff the result in the larger type is not the 00478 // sign extension of its truncation to the original type. 00479 SDValue LHS = SExtPromotedInteger(N->getOperand(0)); 00480 SDValue RHS = SExtPromotedInteger(N->getOperand(1)); 00481 EVT OVT = N->getOperand(0).getValueType(); 00482 EVT NVT = LHS.getValueType(); 00483 SDLoc dl(N); 00484 00485 // Do the arithmetic in the larger type. 00486 unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB; 00487 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS); 00488 00489 // Calculate the overflow flag: sign extend the arithmetic result from 00490 // the original type. 00491 SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res, 00492 DAG.getValueType(OVT)); 00493 // Overflowed if and only if this is not equal to Res. 00494 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE); 00495 00496 // Use the calculated overflow everywhere. 00497 ReplaceValueWith(SDValue(N, 1), Ofl); 00498 00499 return Res; 00500 } 00501 00502 SDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) { 00503 // Sign extend the input. 00504 SDValue LHS = SExtPromotedInteger(N->getOperand(0)); 00505 SDValue RHS = SExtPromotedInteger(N->getOperand(1)); 00506 return DAG.getNode(N->getOpcode(), SDLoc(N), 00507 LHS.getValueType(), LHS, RHS); 00508 } 00509 00510 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) { 00511 SDValue LHS = GetPromotedInteger(N->getOperand(1)); 00512 SDValue RHS = GetPromotedInteger(N->getOperand(2)); 00513 return DAG.getSelect(SDLoc(N), 00514 LHS.getValueType(), N->getOperand(0), LHS, RHS); 00515 } 00516 00517 SDValue DAGTypeLegalizer::PromoteIntRes_VSELECT(SDNode *N) { 00518 SDValue Mask = N->getOperand(0); 00519 EVT OpTy = N->getOperand(1).getValueType(); 00520 00521 // Promote all the way up to the canonical SetCC type. 00522 Mask = PromoteTargetBoolean(Mask, OpTy); 00523 SDValue LHS = GetPromotedInteger(N->getOperand(1)); 00524 SDValue RHS = GetPromotedInteger(N->getOperand(2)); 00525 return DAG.getNode(ISD::VSELECT, SDLoc(N), 00526 LHS.getValueType(), Mask, LHS, RHS); 00527 } 00528 00529 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) { 00530 SDValue LHS = GetPromotedInteger(N->getOperand(2)); 00531 SDValue RHS = GetPromotedInteger(N->getOperand(3)); 00532 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), 00533 LHS.getValueType(), N->getOperand(0), 00534 N->getOperand(1), LHS, RHS, N->getOperand(4)); 00535 } 00536 00537 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) { 00538 EVT SVT = getSetCCResultType(N->getOperand(0).getValueType()); 00539 00540 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 00541 00542 // Only use the result of getSetCCResultType if it is legal, 00543 // otherwise just use the promoted result type (NVT). 00544 if (!TLI.isTypeLegal(SVT)) 00545 SVT = NVT; 00546 00547 SDLoc dl(N); 00548 assert(SVT.isVector() == N->getOperand(0).getValueType().isVector() && 00549 "Vector compare must return a vector result!"); 00550 00551 SDValue LHS = N->getOperand(0); 00552 SDValue RHS = N->getOperand(1); 00553 if (LHS.getValueType() != RHS.getValueType()) { 00554 if (getTypeAction(LHS.getValueType()) == TargetLowering::TypePromoteInteger && 00555 !LHS.getValueType().isVector()) 00556 LHS = GetPromotedInteger(LHS); 00557 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger && 00558 !RHS.getValueType().isVector()) 00559 RHS = GetPromotedInteger(RHS); 00560 } 00561 00562 // Get the SETCC result using the canonical SETCC type. 00563 SDValue SetCC = DAG.getNode(N->getOpcode(), dl, SVT, LHS, RHS, 00564 N->getOperand(2)); 00565 00566 assert(NVT.bitsLE(SVT) && "Integer type overpromoted?"); 00567 // Convert to the expected type. 00568 return DAG.getNode(ISD::TRUNCATE, dl, NVT, SetCC); 00569 } 00570 00571 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) { 00572 SDValue Res = GetPromotedInteger(N->getOperand(0)); 00573 SDValue Amt = N->getOperand(1); 00574 Amt = Amt.getValueType().isVector() ? ZExtPromotedInteger(Amt) : Amt; 00575 return DAG.getNode(ISD::SHL, SDLoc(N), Res.getValueType(), Res, Amt); 00576 } 00577 00578 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) { 00579 SDValue Op = GetPromotedInteger(N->getOperand(0)); 00580 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), 00581 Op.getValueType(), Op, N->getOperand(1)); 00582 } 00583 00584 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) { 00585 // The input may have strange things in the top bits of the registers, but 00586 // these operations don't care. They may have weird bits going out, but 00587 // that too is okay if they are integer operations. 00588 SDValue LHS = GetPromotedInteger(N->getOperand(0)); 00589 SDValue RHS = GetPromotedInteger(N->getOperand(1)); 00590 return DAG.getNode(N->getOpcode(), SDLoc(N), 00591 LHS.getValueType(), LHS, RHS); 00592 } 00593 00594 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) { 00595 // The input value must be properly sign extended. 00596 SDValue Res = SExtPromotedInteger(N->getOperand(0)); 00597 SDValue Amt = N->getOperand(1); 00598 Amt = Amt.getValueType().isVector() ? ZExtPromotedInteger(Amt) : Amt; 00599 return DAG.getNode(ISD::SRA, SDLoc(N), Res.getValueType(), Res, Amt); 00600 } 00601 00602 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) { 00603 // The input value must be properly zero extended. 00604 SDValue Res = ZExtPromotedInteger(N->getOperand(0)); 00605 SDValue Amt = N->getOperand(1); 00606 Amt = Amt.getValueType().isVector() ? ZExtPromotedInteger(Amt) : Amt; 00607 return DAG.getNode(ISD::SRL, SDLoc(N), Res.getValueType(), Res, Amt); 00608 } 00609 00610 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) { 00611 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 00612 SDValue Res; 00613 SDValue InOp = N->getOperand(0); 00614 SDLoc dl(N); 00615 00616 switch (getTypeAction(InOp.getValueType())) { 00617 default: llvm_unreachable("Unknown type action!"); 00618 case TargetLowering::TypeLegal: 00619 case TargetLowering::TypeExpandInteger: 00620 Res = InOp; 00621 break; 00622 case TargetLowering::TypePromoteInteger: 00623 Res = GetPromotedInteger(InOp); 00624 break; 00625 case TargetLowering::TypeSplitVector: 00626 EVT InVT = InOp.getValueType(); 00627 assert(InVT.isVector() && "Cannot split scalar types"); 00628 unsigned NumElts = InVT.getVectorNumElements(); 00629 assert(NumElts == NVT.getVectorNumElements() && 00630 "Dst and Src must have the same number of elements"); 00631 assert(isPowerOf2_32(NumElts) && 00632 "Promoted vector type must be a power of two"); 00633 00634 SDValue EOp1, EOp2; 00635 GetSplitVector(InOp, EOp1, EOp2); 00636 00637 EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(), 00638 NumElts/2); 00639 EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1); 00640 EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2); 00641 00642 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2); 00643 } 00644 00645 // Truncate to NVT instead of VT 00646 return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res); 00647 } 00648 00649 SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) { 00650 if (ResNo == 1) 00651 return PromoteIntRes_Overflow(N); 00652 00653 // The operation overflowed iff the result in the larger type is not the 00654 // zero extension of its truncation to the original type. 00655 SDValue LHS = ZExtPromotedInteger(N->getOperand(0)); 00656 SDValue RHS = ZExtPromotedInteger(N->getOperand(1)); 00657 EVT OVT = N->getOperand(0).getValueType(); 00658 EVT NVT = LHS.getValueType(); 00659 SDLoc dl(N); 00660 00661 // Do the arithmetic in the larger type. 00662 unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB; 00663 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS); 00664 00665 // Calculate the overflow flag: zero extend the arithmetic result from 00666 // the original type. 00667 SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT); 00668 // Overflowed if and only if this is not equal to Res. 00669 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE); 00670 00671 // Use the calculated overflow everywhere. 00672 ReplaceValueWith(SDValue(N, 1), Ofl); 00673 00674 return Res; 00675 } 00676 00677 SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) { 00678 // Promote the overflow bit trivially. 00679 if (ResNo == 1) 00680 return PromoteIntRes_Overflow(N); 00681 00682 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1); 00683 SDLoc DL(N); 00684 EVT SmallVT = LHS.getValueType(); 00685 00686 // To determine if the result overflowed in a larger type, we extend the 00687 // input to the larger type, do the multiply (checking if it overflows), 00688 // then also check the high bits of the result to see if overflow happened 00689 // there. 00690 if (N->getOpcode() == ISD::SMULO) { 00691 LHS = SExtPromotedInteger(LHS); 00692 RHS = SExtPromotedInteger(RHS); 00693 } else { 00694 LHS = ZExtPromotedInteger(LHS); 00695 RHS = ZExtPromotedInteger(RHS); 00696 } 00697 SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1)); 00698 SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS); 00699 00700 // Overflow occurred if it occurred in the larger type, or if the high part 00701 // of the result does not zero/sign-extend the low part. Check this second 00702 // possibility first. 00703 SDValue Overflow; 00704 if (N->getOpcode() == ISD::UMULO) { 00705 // Unsigned overflow occurred if the high part is non-zero. 00706 SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul, 00707 DAG.getIntPtrConstant(SmallVT.getSizeInBits())); 00708 Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi, 00709 DAG.getConstant(0, Hi.getValueType()), ISD::SETNE); 00710 } else { 00711 // Signed overflow occurred if the high part does not sign extend the low. 00712 SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(), 00713 Mul, DAG.getValueType(SmallVT)); 00714 Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE); 00715 } 00716 00717 // The only other way for overflow to occur is if the multiplication in the 00718 // larger type itself overflowed. 00719 Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow, 00720 SDValue(Mul.getNode(), 1)); 00721 00722 // Use the calculated overflow everywhere. 00723 ReplaceValueWith(SDValue(N, 1), Overflow); 00724 return Mul; 00725 } 00726 00727 SDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) { 00728 // Zero extend the input. 00729 SDValue LHS = ZExtPromotedInteger(N->getOperand(0)); 00730 SDValue RHS = ZExtPromotedInteger(N->getOperand(1)); 00731 return DAG.getNode(N->getOpcode(), SDLoc(N), 00732 LHS.getValueType(), LHS, RHS); 00733 } 00734 00735 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) { 00736 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(), 00737 N->getValueType(0))); 00738 } 00739 00740 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) { 00741 SDValue Chain = N->getOperand(0); // Get the chain. 00742 SDValue Ptr = N->getOperand(1); // Get the pointer. 00743 EVT VT = N->getValueType(0); 00744 SDLoc dl(N); 00745 00746 MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT); 00747 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT); 00748 // The argument is passed as NumRegs registers of type RegVT. 00749 00750 SmallVector<SDValue, 8> Parts(NumRegs); 00751 for (unsigned i = 0; i < NumRegs; ++i) { 00752 Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2), 00753 N->getConstantOperandVal(3)); 00754 Chain = Parts[i].getValue(1); 00755 } 00756 00757 // Handle endianness of the load. 00758 if (TLI.isBigEndian()) 00759 std::reverse(Parts.begin(), Parts.end()); 00760 00761 // Assemble the parts in the promoted type. 00762 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 00763 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]); 00764 for (unsigned i = 1; i < NumRegs; ++i) { 00765 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]); 00766 // Shift it to the right position and "or" it in. 00767 Part = DAG.getNode(ISD::SHL, dl, NVT, Part, 00768 DAG.getConstant(i * RegVT.getSizeInBits(), 00769 TLI.getPointerTy())); 00770 Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part); 00771 } 00772 00773 // Modified the chain result - switch anything that used the old chain to 00774 // use the new one. 00775 ReplaceValueWith(SDValue(N, 1), Chain); 00776 00777 return Res; 00778 } 00779 00780 //===----------------------------------------------------------------------===// 00781 // Integer Operand Promotion 00782 //===----------------------------------------------------------------------===// 00783 00784 /// PromoteIntegerOperand - This method is called when the specified operand of 00785 /// the specified node is found to need promotion. At this point, all of the 00786 /// result types of the node are known to be legal, but other operands of the 00787 /// node may need promotion or expansion as well as the specified one. 00788 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) { 00789 DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG); dbgs() << "\n"); 00790 SDValue Res = SDValue(); 00791 00792 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) 00793 return false; 00794 00795 switch (N->getOpcode()) { 00796 default: 00797 #ifndef NDEBUG 00798 dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": "; 00799 N->dump(&DAG); dbgs() << "\n"; 00800 #endif 00801 llvm_unreachable("Do not know how to promote this operator's operand!"); 00802 00803 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break; 00804 case ISD::ATOMIC_STORE: 00805 Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N)); 00806 break; 00807 case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break; 00808 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break; 00809 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break; 00810 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break; 00811 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break; 00812 case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break; 00813 case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break; 00814 case ISD::CONVERT_RNDSAT: 00815 Res = PromoteIntOp_CONVERT_RNDSAT(N); break; 00816 case ISD::INSERT_VECTOR_ELT: 00817 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break; 00818 case ISD::SCALAR_TO_VECTOR: 00819 Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break; 00820 case ISD::VSELECT: 00821 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break; 00822 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break; 00823 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break; 00824 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break; 00825 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break; 00826 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N), 00827 OpNo); break; 00828 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break; 00829 case ISD::FP16_TO_FP: 00830 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break; 00831 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break; 00832 00833 case ISD::SHL: 00834 case ISD::SRA: 00835 case ISD::SRL: 00836 case ISD::ROTL: 00837 case ISD::ROTR: Res = PromoteIntOp_Shift(N); break; 00838 } 00839 00840 // If the result is null, the sub-method took care of registering results etc. 00841 if (!Res.getNode()) return false; 00842 00843 // If the result is N, the sub-method updated N in place. Tell the legalizer 00844 // core about this. 00845 if (Res.getNode() == N) 00846 return true; 00847 00848 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 && 00849 "Invalid operand expansion"); 00850 00851 ReplaceValueWith(SDValue(N, 0), Res); 00852 return false; 00853 } 00854 00855 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is 00856 /// shared among BR_CC, SELECT_CC, and SETCC handlers. 00857 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS, 00858 ISD::CondCode CCCode) { 00859 // We have to insert explicit sign or zero extends. Note that we could 00860 // insert sign extends for ALL conditions, but zero extend is cheaper on 00861 // many machines (an AND instead of two shifts), so prefer it. 00862 switch (CCCode) { 00863 default: llvm_unreachable("Unknown integer comparison!"); 00864 case ISD::SETEQ: 00865 case ISD::SETNE: 00866 case ISD::SETUGE: 00867 case ISD::SETUGT: 00868 case ISD::SETULE: 00869 case ISD::SETULT: 00870 // ALL of these operations will work if we either sign or zero extend 00871 // the operands (including the unsigned comparisons!). Zero extend is 00872 // usually a simpler/cheaper operation, so prefer it. 00873 NewLHS = ZExtPromotedInteger(NewLHS); 00874 NewRHS = ZExtPromotedInteger(NewRHS); 00875 break; 00876 case ISD::SETGE: 00877 case ISD::SETGT: 00878 case ISD::SETLT: 00879 case ISD::SETLE: 00880 NewLHS = SExtPromotedInteger(NewLHS); 00881 NewRHS = SExtPromotedInteger(NewRHS); 00882 break; 00883 } 00884 } 00885 00886 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) { 00887 SDValue Op = GetPromotedInteger(N->getOperand(0)); 00888 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op); 00889 } 00890 00891 SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) { 00892 SDValue Op2 = GetPromotedInteger(N->getOperand(2)); 00893 return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(), 00894 N->getChain(), N->getBasePtr(), Op2, N->getMemOperand(), 00895 N->getOrdering(), N->getSynchScope()); 00896 } 00897 00898 SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) { 00899 // This should only occur in unusual situations like bitcasting to an 00900 // x86_fp80, so just turn it into a store+load 00901 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0)); 00902 } 00903 00904 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) { 00905 assert(OpNo == 2 && "Don't know how to promote this operand!"); 00906 00907 SDValue LHS = N->getOperand(2); 00908 SDValue RHS = N->getOperand(3); 00909 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get()); 00910 00911 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always 00912 // legal types. 00913 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), 00914 N->getOperand(1), LHS, RHS, N->getOperand(4)), 00915 0); 00916 } 00917 00918 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) { 00919 assert(OpNo == 1 && "only know how to promote condition"); 00920 00921 // Promote all the way up to the canonical SetCC type. 00922 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other); 00923 00924 // The chain (Op#0) and basic block destination (Op#2) are always legal types. 00925 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond, 00926 N->getOperand(2)), 0); 00927 } 00928 00929 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) { 00930 // Since the result type is legal, the operands must promote to it. 00931 EVT OVT = N->getOperand(0).getValueType(); 00932 SDValue Lo = ZExtPromotedInteger(N->getOperand(0)); 00933 SDValue Hi = GetPromotedInteger(N->getOperand(1)); 00934 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?"); 00935 SDLoc dl(N); 00936 00937 Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi, 00938 DAG.getConstant(OVT.getSizeInBits(), TLI.getPointerTy())); 00939 return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi); 00940 } 00941 00942 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) { 00943 // The vector type is legal but the element type is not. This implies 00944 // that the vector is a power-of-two in length and that the element 00945 // type does not have a strange size (eg: it is not i1). 00946 EVT VecVT = N->getValueType(0); 00947 unsigned NumElts = VecVT.getVectorNumElements(); 00948 assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) && 00949 "Legal vector of one illegal element?"); 00950 00951 // Promote the inserted value. The type does not need to match the 00952 // vector element type. Check that any extra bits introduced will be 00953 // truncated away. 00954 assert(N->getOperand(0).getValueType().getSizeInBits() >= 00955 N->getValueType(0).getVectorElementType().getSizeInBits() && 00956 "Type of inserted value narrower than vector element type!"); 00957 00958 SmallVector<SDValue, 16> NewOps; 00959 for (unsigned i = 0; i < NumElts; ++i) 00960 NewOps.push_back(GetPromotedInteger(N->getOperand(i))); 00961 00962 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0); 00963 } 00964 00965 SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) { 00966 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode(); 00967 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU || 00968 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU || 00969 CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) && 00970 "can only promote integer arguments"); 00971 SDValue InOp = GetPromotedInteger(N->getOperand(0)); 00972 return DAG.getConvertRndSat(N->getValueType(0), SDLoc(N), InOp, 00973 N->getOperand(1), N->getOperand(2), 00974 N->getOperand(3), N->getOperand(4), CvtCode); 00975 } 00976 00977 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N, 00978 unsigned OpNo) { 00979 if (OpNo == 1) { 00980 // Promote the inserted value. This is valid because the type does not 00981 // have to match the vector element type. 00982 00983 // Check that any extra bits introduced will be truncated away. 00984 assert(N->getOperand(1).getValueType().getSizeInBits() >= 00985 N->getValueType(0).getVectorElementType().getSizeInBits() && 00986 "Type of inserted value narrower than vector element type!"); 00987 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), 00988 GetPromotedInteger(N->getOperand(1)), 00989 N->getOperand(2)), 00990 0); 00991 } 00992 00993 assert(OpNo == 2 && "Different operand and result vector types?"); 00994 00995 // Promote the index. 00996 SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N), 00997 TLI.getVectorIdxTy()); 00998 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), 00999 N->getOperand(1), Idx), 0); 01000 } 01001 01002 SDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) { 01003 // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote 01004 // the operand in place. 01005 return SDValue(DAG.UpdateNodeOperands(N, 01006 GetPromotedInteger(N->getOperand(0))), 0); 01007 } 01008 01009 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) { 01010 assert(OpNo == 0 && "Only know how to promote the condition!"); 01011 SDValue Cond = N->getOperand(0); 01012 EVT OpTy = N->getOperand(1).getValueType(); 01013 01014 // Promote all the way up to the canonical SetCC type. 01015 EVT OpVT = N->getOpcode() == ISD::SELECT ? OpTy.getScalarType() : OpTy; 01016 Cond = PromoteTargetBoolean(Cond, OpVT); 01017 01018 return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1), 01019 N->getOperand(2)), 0); 01020 } 01021 01022 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) { 01023 assert(OpNo == 0 && "Don't know how to promote this operand!"); 01024 01025 SDValue LHS = N->getOperand(0); 01026 SDValue RHS = N->getOperand(1); 01027 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get()); 01028 01029 // The CC (#4) and the possible return values (#2 and #3) have legal types. 01030 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2), 01031 N->getOperand(3), N->getOperand(4)), 0); 01032 } 01033 01034 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) { 01035 assert(OpNo == 0 && "Don't know how to promote this operand!"); 01036 01037 SDValue LHS = N->getOperand(0); 01038 SDValue RHS = N->getOperand(1); 01039 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get()); 01040 01041 // The CC (#2) is always legal. 01042 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0); 01043 } 01044 01045 SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) { 01046 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), 01047 ZExtPromotedInteger(N->getOperand(1))), 0); 01048 } 01049 01050 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) { 01051 SDValue Op = GetPromotedInteger(N->getOperand(0)); 01052 SDLoc dl(N); 01053 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op); 01054 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(), 01055 Op, DAG.getValueType(N->getOperand(0).getValueType())); 01056 } 01057 01058 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) { 01059 return SDValue(DAG.UpdateNodeOperands(N, 01060 SExtPromotedInteger(N->getOperand(0))), 0); 01061 } 01062 01063 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){ 01064 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!"); 01065 SDValue Ch = N->getChain(), Ptr = N->getBasePtr(); 01066 SDLoc dl(N); 01067 01068 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value. 01069 01070 // Truncate the value and store the result. 01071 return DAG.getTruncStore(Ch, dl, Val, Ptr, 01072 N->getMemoryVT(), N->getMemOperand()); 01073 } 01074 01075 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) { 01076 SDValue Op = GetPromotedInteger(N->getOperand(0)); 01077 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op); 01078 } 01079 01080 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) { 01081 return SDValue(DAG.UpdateNodeOperands(N, 01082 ZExtPromotedInteger(N->getOperand(0))), 0); 01083 } 01084 01085 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) { 01086 SDLoc dl(N); 01087 SDValue Op = GetPromotedInteger(N->getOperand(0)); 01088 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op); 01089 return DAG.getZeroExtendInReg(Op, dl, 01090 N->getOperand(0).getValueType().getScalarType()); 01091 } 01092 01093 01094 //===----------------------------------------------------------------------===// 01095 // Integer Result Expansion 01096 //===----------------------------------------------------------------------===// 01097 01098 /// ExpandIntegerResult - This method is called when the specified result of the 01099 /// specified node is found to need expansion. At this point, the node may also 01100 /// have invalid operands or may have other results that need promotion, we just 01101 /// know that (at least) one result needs expansion. 01102 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) { 01103 DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG); dbgs() << "\n"); 01104 SDValue Lo, Hi; 01105 Lo = Hi = SDValue(); 01106 01107 // See if the target wants to custom expand this node. 01108 if (CustomLowerNode(N, N->getValueType(ResNo), true)) 01109 return; 01110 01111 switch (N->getOpcode()) { 01112 default: 01113 #ifndef NDEBUG 01114 dbgs() << "ExpandIntegerResult #" << ResNo << ": "; 01115 N->dump(&DAG); dbgs() << "\n"; 01116 #endif 01117 llvm_unreachable("Do not know how to expand the result of this operator!"); 01118 01119 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break; 01120 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break; 01121 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break; 01122 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break; 01123 01124 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break; 01125 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break; 01126 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break; 01127 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break; 01128 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break; 01129 01130 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break; 01131 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break; 01132 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break; 01133 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break; 01134 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break; 01135 case ISD::CTLZ_ZERO_UNDEF: 01136 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break; 01137 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break; 01138 case ISD::CTTZ_ZERO_UNDEF: 01139 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break; 01140 case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break; 01141 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break; 01142 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break; 01143 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break; 01144 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break; 01145 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break; 01146 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break; 01147 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break; 01148 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break; 01149 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break; 01150 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break; 01151 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break; 01152 case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break; 01153 01154 case ISD::ATOMIC_LOAD_ADD: 01155 case ISD::ATOMIC_LOAD_SUB: 01156 case ISD::ATOMIC_LOAD_AND: 01157 case ISD::ATOMIC_LOAD_OR: 01158 case ISD::ATOMIC_LOAD_XOR: 01159 case ISD::ATOMIC_LOAD_NAND: 01160 case ISD::ATOMIC_LOAD_MIN: 01161 case ISD::ATOMIC_LOAD_MAX: 01162 case ISD::ATOMIC_LOAD_UMIN: 01163 case ISD::ATOMIC_LOAD_UMAX: 01164 case ISD::ATOMIC_SWAP: 01165 case ISD::ATOMIC_CMP_SWAP: { 01166 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N); 01167 SplitInteger(Tmp.first, Lo, Hi); 01168 ReplaceValueWith(SDValue(N, 1), Tmp.second); 01169 break; 01170 } 01171 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: { 01172 AtomicSDNode *AN = cast<AtomicSDNode>(N); 01173 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other); 01174 SDValue Tmp = DAG.getAtomicCmpSwap( 01175 ISD::ATOMIC_CMP_SWAP, SDLoc(N), AN->getMemoryVT(), VTs, 01176 N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3), 01177 AN->getMemOperand(), AN->getSuccessOrdering(), AN->getFailureOrdering(), 01178 AN->getSynchScope()); 01179 01180 // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine 01181 // success simply by comparing the loaded value against the ingoing 01182 // comparison. 01183 SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp, 01184 N->getOperand(2), ISD::SETEQ); 01185 01186 SplitInteger(Tmp, Lo, Hi); 01187 ReplaceValueWith(SDValue(N, 1), Success); 01188 ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1)); 01189 break; 01190 } 01191 01192 case ISD::AND: 01193 case ISD::OR: 01194 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break; 01195 01196 case ISD::ADD: 01197 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break; 01198 01199 case ISD::ADDC: 01200 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break; 01201 01202 case ISD::ADDE: 01203 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break; 01204 01205 case ISD::SHL: 01206 case ISD::SRA: 01207 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break; 01208 01209 case ISD::SADDO: 01210 case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break; 01211 case ISD::UADDO: 01212 case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break; 01213 case ISD::UMULO: 01214 case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break; 01215 } 01216 01217 // If Lo/Hi is null, the sub-method took care of registering results etc. 01218 if (Lo.getNode()) 01219 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi); 01220 } 01221 01222 /// Lower an atomic node to the appropriate builtin call. 01223 std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) { 01224 unsigned Opc = Node->getOpcode(); 01225 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT(); 01226 RTLIB::Libcall LC; 01227 01228 switch (Opc) { 01229 default: 01230 llvm_unreachable("Unhandled atomic intrinsic Expand!"); 01231 case ISD::ATOMIC_SWAP: 01232 switch (VT.SimpleTy) { 01233 default: llvm_unreachable("Unexpected value type for atomic!"); 01234 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break; 01235 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break; 01236 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break; 01237 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break; 01238 case MVT::i128:LC = RTLIB::SYNC_LOCK_TEST_AND_SET_16;break; 01239 } 01240 break; 01241 case ISD::ATOMIC_CMP_SWAP: 01242 switch (VT.SimpleTy) { 01243 default: llvm_unreachable("Unexpected value type for atomic!"); 01244 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break; 01245 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break; 01246 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break; 01247 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break; 01248 case MVT::i128:LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_16;break; 01249 } 01250 break; 01251 case ISD::ATOMIC_LOAD_ADD: 01252 switch (VT.SimpleTy) { 01253 default: llvm_unreachable("Unexpected value type for atomic!"); 01254 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break; 01255 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break; 01256 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break; 01257 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break; 01258 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_ADD_16;break; 01259 } 01260 break; 01261 case ISD::ATOMIC_LOAD_SUB: 01262 switch (VT.SimpleTy) { 01263 default: llvm_unreachable("Unexpected value type for atomic!"); 01264 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break; 01265 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break; 01266 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break; 01267 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break; 01268 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_SUB_16;break; 01269 } 01270 break; 01271 case ISD::ATOMIC_LOAD_AND: 01272 switch (VT.SimpleTy) { 01273 default: llvm_unreachable("Unexpected value type for atomic!"); 01274 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break; 01275 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break; 01276 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break; 01277 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break; 01278 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_AND_16;break; 01279 } 01280 break; 01281 case ISD::ATOMIC_LOAD_OR: 01282 switch (VT.SimpleTy) { 01283 default: llvm_unreachable("Unexpected value type for atomic!"); 01284 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break; 01285 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break; 01286 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break; 01287 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break; 01288 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_OR_16;break; 01289 } 01290 break; 01291 case ISD::ATOMIC_LOAD_XOR: 01292 switch (VT.SimpleTy) { 01293 default: llvm_unreachable("Unexpected value type for atomic!"); 01294 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break; 01295 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break; 01296 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break; 01297 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break; 01298 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_XOR_16;break; 01299 } 01300 break; 01301 case ISD::ATOMIC_LOAD_NAND: 01302 switch (VT.SimpleTy) { 01303 default: llvm_unreachable("Unexpected value type for atomic!"); 01304 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break; 01305 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break; 01306 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break; 01307 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break; 01308 case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_NAND_16;break; 01309 } 01310 break; 01311 } 01312 01313 return ExpandChainLibCall(LC, Node, false); 01314 } 01315 01316 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded, 01317 /// and the shift amount is a constant 'Amt'. Expand the operation. 01318 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt, 01319 SDValue &Lo, SDValue &Hi) { 01320 assert(Amt && "Expected zero shifts to be already optimized away."); 01321 SDLoc DL(N); 01322 // Expand the incoming operand to be shifted, so that we have its parts 01323 SDValue InL, InH; 01324 GetExpandedInteger(N->getOperand(0), InL, InH); 01325 01326 EVT NVT = InL.getValueType(); 01327 unsigned VTBits = N->getValueType(0).getSizeInBits(); 01328 unsigned NVTBits = NVT.getSizeInBits(); 01329 EVT ShTy = N->getOperand(1).getValueType(); 01330 01331 if (N->getOpcode() == ISD::SHL) { 01332 if (Amt > VTBits) { 01333 Lo = Hi = DAG.getConstant(0, NVT); 01334 } else if (Amt > NVTBits) { 01335 Lo = DAG.getConstant(0, NVT); 01336 Hi = DAG.getNode(ISD::SHL, DL, 01337 NVT, InL, DAG.getConstant(Amt-NVTBits, ShTy)); 01338 } else if (Amt == NVTBits) { 01339 Lo = DAG.getConstant(0, NVT); 01340 Hi = InL; 01341 } else if (Amt == 1 && 01342 TLI.isOperationLegalOrCustom(ISD::ADDC, 01343 TLI.getTypeToExpandTo(*DAG.getContext(), NVT))) { 01344 // Emit this X << 1 as X+X. 01345 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue); 01346 SDValue LoOps[2] = { InL, InL }; 01347 Lo = DAG.getNode(ISD::ADDC, DL, VTList, LoOps); 01348 SDValue HiOps[3] = { InH, InH, Lo.getValue(1) }; 01349 Hi = DAG.getNode(ISD::ADDE, DL, VTList, HiOps); 01350 } else { 01351 Lo = DAG.getNode(ISD::SHL, DL, NVT, InL, DAG.getConstant(Amt, ShTy)); 01352 Hi = DAG.getNode(ISD::OR, DL, NVT, 01353 DAG.getNode(ISD::SHL, DL, NVT, InH, 01354 DAG.getConstant(Amt, ShTy)), 01355 DAG.getNode(ISD::SRL, DL, NVT, InL, 01356 DAG.getConstant(NVTBits-Amt, ShTy))); 01357 } 01358 return; 01359 } 01360 01361 if (N->getOpcode() == ISD::SRL) { 01362 if (Amt > VTBits) { 01363 Lo = DAG.getConstant(0, NVT); 01364 Hi = DAG.getConstant(0, NVT); 01365 } else if (Amt > NVTBits) { 01366 Lo = DAG.getNode(ISD::SRL, DL, 01367 NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy)); 01368 Hi = DAG.getConstant(0, NVT); 01369 } else if (Amt == NVTBits) { 01370 Lo = InH; 01371 Hi = DAG.getConstant(0, NVT); 01372 } else { 01373 Lo = DAG.getNode(ISD::OR, DL, NVT, 01374 DAG.getNode(ISD::SRL, DL, NVT, InL, 01375 DAG.getConstant(Amt, ShTy)), 01376 DAG.getNode(ISD::SHL, DL, NVT, InH, 01377 DAG.getConstant(NVTBits-Amt, ShTy))); 01378 Hi = DAG.getNode(ISD::SRL, DL, NVT, InH, DAG.getConstant(Amt, ShTy)); 01379 } 01380 return; 01381 } 01382 01383 assert(N->getOpcode() == ISD::SRA && "Unknown shift!"); 01384 if (Amt > VTBits) { 01385 Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH, 01386 DAG.getConstant(NVTBits-1, ShTy)); 01387 } else if (Amt > NVTBits) { 01388 Lo = DAG.getNode(ISD::SRA, DL, NVT, InH, 01389 DAG.getConstant(Amt-NVTBits, ShTy)); 01390 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, 01391 DAG.getConstant(NVTBits-1, ShTy)); 01392 } else if (Amt == NVTBits) { 01393 Lo = InH; 01394 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, 01395 DAG.getConstant(NVTBits-1, ShTy)); 01396 } else { 01397 Lo = DAG.getNode(ISD::OR, DL, NVT, 01398 DAG.getNode(ISD::SRL, DL, NVT, InL, 01399 DAG.getConstant(Amt, ShTy)), 01400 DAG.getNode(ISD::SHL, DL, NVT, InH, 01401 DAG.getConstant(NVTBits-Amt, ShTy))); 01402 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, DAG.getConstant(Amt, ShTy)); 01403 } 01404 } 01405 01406 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify 01407 /// this shift based on knowledge of the high bit of the shift amount. If we 01408 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual 01409 /// shift amount. 01410 bool DAGTypeLegalizer:: 01411 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) { 01412 SDValue Amt = N->getOperand(1); 01413 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 01414 EVT ShTy = Amt.getValueType(); 01415 unsigned ShBits = ShTy.getScalarType().getSizeInBits(); 01416 unsigned NVTBits = NVT.getScalarType().getSizeInBits(); 01417 assert(isPowerOf2_32(NVTBits) && 01418 "Expanded integer type size not a power of two!"); 01419 SDLoc dl(N); 01420 01421 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits)); 01422 APInt KnownZero, KnownOne; 01423 DAG.computeKnownBits(N->getOperand(1), KnownZero, KnownOne); 01424 01425 // If we don't know anything about the high bits, exit. 01426 if (((KnownZero|KnownOne) & HighBitMask) == 0) 01427 return false; 01428 01429 // Get the incoming operand to be shifted. 01430 SDValue InL, InH; 01431 GetExpandedInteger(N->getOperand(0), InL, InH); 01432 01433 // If we know that any of the high bits of the shift amount are one, then we 01434 // can do this as a couple of simple shifts. 01435 if (KnownOne.intersects(HighBitMask)) { 01436 // Mask out the high bit, which we know is set. 01437 Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt, 01438 DAG.getConstant(~HighBitMask, ShTy)); 01439 01440 switch (N->getOpcode()) { 01441 default: llvm_unreachable("Unknown shift"); 01442 case ISD::SHL: 01443 Lo = DAG.getConstant(0, NVT); // Low part is zero. 01444 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part. 01445 return true; 01446 case ISD::SRL: 01447 Hi = DAG.getConstant(0, NVT); // Hi part is zero. 01448 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part. 01449 return true; 01450 case ISD::SRA: 01451 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part. 01452 DAG.getConstant(NVTBits-1, ShTy)); 01453 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part. 01454 return true; 01455 } 01456 } 01457 01458 // If we know that all of the high bits of the shift amount are zero, then we 01459 // can do this as a couple of simple shifts. 01460 if ((KnownZero & HighBitMask) == HighBitMask) { 01461 // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined 01462 // shift if x is zero. We can use XOR here because x is known to be smaller 01463 // than 32. 01464 SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt, 01465 DAG.getConstant(NVTBits-1, ShTy)); 01466 01467 unsigned Op1, Op2; 01468 switch (N->getOpcode()) { 01469 default: llvm_unreachable("Unknown shift"); 01470 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break; 01471 case ISD::SRL: 01472 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break; 01473 } 01474 01475 // When shifting right the arithmetic for Lo and Hi is swapped. 01476 if (N->getOpcode() != ISD::SHL) 01477 std::swap(InL, InH); 01478 01479 // Use a little trick to get the bits that move from Lo to Hi. First 01480 // shift by one bit. 01481 SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, ShTy)); 01482 // Then compute the remaining shift with amount-1. 01483 SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2); 01484 01485 Lo = DAG.getNode(N->getOpcode(), dl, NVT, InL, Amt); 01486 Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2); 01487 01488 if (N->getOpcode() != ISD::SHL) 01489 std::swap(Hi, Lo); 01490 return true; 01491 } 01492 01493 return false; 01494 } 01495 01496 /// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift 01497 /// of any size. 01498 bool DAGTypeLegalizer:: 01499 ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) { 01500 SDValue Amt = N->getOperand(1); 01501 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 01502 EVT ShTy = Amt.getValueType(); 01503 unsigned NVTBits = NVT.getSizeInBits(); 01504 assert(isPowerOf2_32(NVTBits) && 01505 "Expanded integer type size not a power of two!"); 01506 SDLoc dl(N); 01507 01508 // Get the incoming operand to be shifted. 01509 SDValue InL, InH; 01510 GetExpandedInteger(N->getOperand(0), InL, InH); 01511 01512 SDValue NVBitsNode = DAG.getConstant(NVTBits, ShTy); 01513 SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode); 01514 SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt); 01515 SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy), 01516 Amt, NVBitsNode, ISD::SETULT); 01517 01518 SDValue LoS, HiS, LoL, HiL; 01519 switch (N->getOpcode()) { 01520 default: llvm_unreachable("Unknown shift"); 01521 case ISD::SHL: 01522 // Short: ShAmt < NVTBits 01523 LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); 01524 HiS = DAG.getNode(ISD::OR, dl, NVT, 01525 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt), 01526 // FIXME: If Amt is zero, the following shift generates an undefined result 01527 // on some architectures. 01528 DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack)); 01529 01530 // Long: ShAmt >= NVTBits 01531 LoL = DAG.getConstant(0, NVT); // Lo part is zero. 01532 HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part. 01533 01534 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL); 01535 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL); 01536 return true; 01537 case ISD::SRL: 01538 // Short: ShAmt < NVTBits 01539 HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); 01540 LoS = DAG.getNode(ISD::OR, dl, NVT, 01541 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt), 01542 // FIXME: If Amt is zero, the following shift generates an undefined result 01543 // on some architectures. 01544 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack)); 01545 01546 // Long: ShAmt >= NVTBits 01547 HiL = DAG.getConstant(0, NVT); // Hi part is zero. 01548 LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part. 01549 01550 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL); 01551 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL); 01552 return true; 01553 case ISD::SRA: 01554 // Short: ShAmt < NVTBits 01555 HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); 01556 LoS = DAG.getNode(ISD::OR, dl, NVT, 01557 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt), 01558 // FIXME: If Amt is zero, the following shift generates an undefined result 01559 // on some architectures. 01560 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack)); 01561 01562 // Long: ShAmt >= NVTBits 01563 HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part. 01564 DAG.getConstant(NVTBits-1, ShTy)); 01565 LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part. 01566 01567 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL); 01568 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL); 01569 return true; 01570 } 01571 } 01572 01573 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N, 01574 SDValue &Lo, SDValue &Hi) { 01575 SDLoc dl(N); 01576 // Expand the subcomponents. 01577 SDValue LHSL, LHSH, RHSL, RHSH; 01578 GetExpandedInteger(N->getOperand(0), LHSL, LHSH); 01579 GetExpandedInteger(N->getOperand(1), RHSL, RHSH); 01580 01581 EVT NVT = LHSL.getValueType(); 01582 SDValue LoOps[2] = { LHSL, RHSL }; 01583 SDValue HiOps[3] = { LHSH, RHSH }; 01584 01585 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support 01586 // them. TODO: Teach operation legalization how to expand unsupported 01587 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate 01588 // a carry of type MVT::Glue, but there doesn't seem to be any way to 01589 // generate a value of this type in the expanded code sequence. 01590 bool hasCarry = 01591 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ? 01592 ISD::ADDC : ISD::SUBC, 01593 TLI.getTypeToExpandTo(*DAG.getContext(), NVT)); 01594 01595 if (hasCarry) { 01596 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue); 01597 if (N->getOpcode() == ISD::ADD) { 01598 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps); 01599 HiOps[2] = Lo.getValue(1); 01600 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps); 01601 } else { 01602 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps); 01603 HiOps[2] = Lo.getValue(1); 01604 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps); 01605 } 01606 return; 01607 } 01608 01609 if (N->getOpcode() == ISD::ADD) { 01610 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps); 01611 Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2)); 01612 SDValue Cmp1 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0], 01613 ISD::SETULT); 01614 SDValue Carry1 = DAG.getSelect(dl, NVT, Cmp1, 01615 DAG.getConstant(1, NVT), 01616 DAG.getConstant(0, NVT)); 01617 SDValue Cmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[1], 01618 ISD::SETULT); 01619 SDValue Carry2 = DAG.getSelect(dl, NVT, Cmp2, 01620 DAG.getConstant(1, NVT), Carry1); 01621 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2); 01622 } else { 01623 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps); 01624 Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2)); 01625 SDValue Cmp = 01626 DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()), 01627 LoOps[0], LoOps[1], ISD::SETULT); 01628 SDValue Borrow = DAG.getSelect(dl, NVT, Cmp, 01629 DAG.getConstant(1, NVT), 01630 DAG.getConstant(0, NVT)); 01631 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow); 01632 } 01633 } 01634 01635 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N, 01636 SDValue &Lo, SDValue &Hi) { 01637 // Expand the subcomponents. 01638 SDValue LHSL, LHSH, RHSL, RHSH; 01639 SDLoc dl(N); 01640 GetExpandedInteger(N->getOperand(0), LHSL, LHSH); 01641 GetExpandedInteger(N->getOperand(1), RHSL, RHSH); 01642 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue); 01643 SDValue LoOps[2] = { LHSL, RHSL }; 01644 SDValue HiOps[3] = { LHSH, RHSH }; 01645 01646 if (N->getOpcode() == ISD::ADDC) { 01647 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps); 01648 HiOps[2] = Lo.getValue(1); 01649 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps); 01650 } else { 01651 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps); 01652 HiOps[2] = Lo.getValue(1); 01653 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps); 01654 } 01655 01656 // Legalized the flag result - switch anything that used the old flag to 01657 // use the new one. 01658 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1)); 01659 } 01660 01661 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N, 01662 SDValue &Lo, SDValue &Hi) { 01663 // Expand the subcomponents. 01664 SDValue LHSL, LHSH, RHSL, RHSH; 01665 SDLoc dl(N); 01666 GetExpandedInteger(N->getOperand(0), LHSL, LHSH); 01667 GetExpandedInteger(N->getOperand(1), RHSL, RHSH); 01668 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue); 01669 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) }; 01670 SDValue HiOps[3] = { LHSH, RHSH }; 01671 01672 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps); 01673 HiOps[2] = Lo.getValue(1); 01674 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps); 01675 01676 // Legalized the flag result - switch anything that used the old flag to 01677 // use the new one. 01678 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1)); 01679 } 01680 01681 void DAGTypeLegalizer::ExpandIntRes_MERGE_VALUES(SDNode *N, unsigned ResNo, 01682 SDValue &Lo, SDValue &Hi) { 01683 SDValue Res = DisintegrateMERGE_VALUES(N, ResNo); 01684 SplitInteger(Res, Lo, Hi); 01685 } 01686 01687 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N, 01688 SDValue &Lo, SDValue &Hi) { 01689 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 01690 SDLoc dl(N); 01691 SDValue Op = N->getOperand(0); 01692 if (Op.getValueType().bitsLE(NVT)) { 01693 // The low part is any extension of the input (which degenerates to a copy). 01694 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op); 01695 Hi = DAG.getUNDEF(NVT); // The high part is undefined. 01696 } else { 01697 // For example, extension of an i48 to an i64. The operand type necessarily 01698 // promotes to the result type, so will end up being expanded too. 01699 assert(getTypeAction(Op.getValueType()) == 01700 TargetLowering::TypePromoteInteger && 01701 "Only know how to promote this result!"); 01702 SDValue Res = GetPromotedInteger(Op); 01703 assert(Res.getValueType() == N->getValueType(0) && 01704 "Operand over promoted?"); 01705 // Split the promoted operand. This will simplify when it is expanded. 01706 SplitInteger(Res, Lo, Hi); 01707 } 01708 } 01709 01710 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N, 01711 SDValue &Lo, SDValue &Hi) { 01712 SDLoc dl(N); 01713 GetExpandedInteger(N->getOperand(0), Lo, Hi); 01714 EVT NVT = Lo.getValueType(); 01715 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); 01716 unsigned NVTBits = NVT.getSizeInBits(); 01717 unsigned EVTBits = EVT.getSizeInBits(); 01718 01719 if (NVTBits < EVTBits) { 01720 Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi, 01721 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 01722 EVTBits - NVTBits))); 01723 } else { 01724 Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT)); 01725 // The high part replicates the sign bit of Lo, make it explicit. 01726 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo, 01727 DAG.getConstant(NVTBits-1, TLI.getPointerTy())); 01728 } 01729 } 01730 01731 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N, 01732 SDValue &Lo, SDValue &Hi) { 01733 SDLoc dl(N); 01734 GetExpandedInteger(N->getOperand(0), Lo, Hi); 01735 EVT NVT = Lo.getValueType(); 01736 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); 01737 unsigned NVTBits = NVT.getSizeInBits(); 01738 unsigned EVTBits = EVT.getSizeInBits(); 01739 01740 if (NVTBits < EVTBits) { 01741 Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi, 01742 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 01743 EVTBits - NVTBits))); 01744 } else { 01745 Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT)); 01746 // The high part must be zero, make it explicit. 01747 Hi = DAG.getConstant(0, NVT); 01748 } 01749 } 01750 01751 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N, 01752 SDValue &Lo, SDValue &Hi) { 01753 SDLoc dl(N); 01754 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands. 01755 Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo); 01756 Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi); 01757 } 01758 01759 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N, 01760 SDValue &Lo, SDValue &Hi) { 01761 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 01762 unsigned NBitWidth = NVT.getSizeInBits(); 01763 auto Constant = cast<ConstantSDNode>(N); 01764 const APInt &Cst = Constant->getAPIntValue(); 01765 bool IsTarget = Constant->isTargetOpcode(); 01766 bool IsOpaque = Constant->isOpaque(); 01767 Lo = DAG.getConstant(Cst.trunc(NBitWidth), NVT, IsTarget, IsOpaque); 01768 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT, IsTarget, 01769 IsOpaque); 01770 } 01771 01772 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N, 01773 SDValue &Lo, SDValue &Hi) { 01774 SDLoc dl(N); 01775 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32) 01776 GetExpandedInteger(N->getOperand(0), Lo, Hi); 01777 EVT NVT = Lo.getValueType(); 01778 01779 SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi, 01780 DAG.getConstant(0, NVT), ISD::SETNE); 01781 01782 SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo); 01783 SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, NVT, Hi); 01784 01785 Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ, 01786 DAG.getNode(ISD::ADD, dl, NVT, LoLZ, 01787 DAG.getConstant(NVT.getSizeInBits(), NVT))); 01788 Hi = DAG.getConstant(0, NVT); 01789 } 01790 01791 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N, 01792 SDValue &Lo, SDValue &Hi) { 01793 SDLoc dl(N); 01794 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo) 01795 GetExpandedInteger(N->getOperand(0), Lo, Hi); 01796 EVT NVT = Lo.getValueType(); 01797 Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo), 01798 DAG.getNode(ISD::CTPOP, dl, NVT, Hi)); 01799 Hi = DAG.getConstant(0, NVT); 01800 } 01801 01802 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N, 01803 SDValue &Lo, SDValue &Hi) { 01804 SDLoc dl(N); 01805 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32) 01806 GetExpandedInteger(N->getOperand(0), Lo, Hi); 01807 EVT NVT = Lo.getValueType(); 01808 01809 SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, 01810 DAG.getConstant(0, NVT), ISD::SETNE); 01811 01812 SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, NVT, Lo); 01813 SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi); 01814 01815 Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ, 01816 DAG.getNode(ISD::ADD, dl, NVT, HiLZ, 01817 DAG.getConstant(NVT.getSizeInBits(), NVT))); 01818 Hi = DAG.getConstant(0, NVT); 01819 } 01820 01821 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo, 01822 SDValue &Hi) { 01823 SDLoc dl(N); 01824 EVT VT = N->getValueType(0); 01825 SDValue Op = N->getOperand(0); 01826 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT); 01827 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!"); 01828 SplitInteger(TLI.makeLibCall(DAG, LC, VT, &Op, 1, true/*irrelevant*/, 01829 dl).first, 01830 Lo, Hi); 01831 } 01832 01833 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo, 01834 SDValue &Hi) { 01835 SDLoc dl(N); 01836 EVT VT = N->getValueType(0); 01837 SDValue Op = N->getOperand(0); 01838 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT); 01839 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!"); 01840 SplitInteger(TLI.makeLibCall(DAG, LC, VT, &Op, 1, false/*irrelevant*/, 01841 dl).first, 01842 Lo, Hi); 01843 } 01844 01845 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N, 01846 SDValue &Lo, SDValue &Hi) { 01847 if (ISD::isNormalLoad(N)) { 01848 ExpandRes_NormalLoad(N, Lo, Hi); 01849 return; 01850 } 01851 01852 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!"); 01853 01854 EVT VT = N->getValueType(0); 01855 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); 01856 SDValue Ch = N->getChain(); 01857 SDValue Ptr = N->getBasePtr(); 01858 ISD::LoadExtType ExtType = N->getExtensionType(); 01859 unsigned Alignment = N->getAlignment(); 01860 bool isVolatile = N->isVolatile(); 01861 bool isNonTemporal = N->isNonTemporal(); 01862 bool isInvariant = N->isInvariant(); 01863 AAMDNodes AAInfo = N->getAAInfo(); 01864 SDLoc dl(N); 01865 01866 assert(NVT.isByteSized() && "Expanded type not byte sized!"); 01867 01868 if (N->getMemoryVT().bitsLE(NVT)) { 01869 EVT MemVT = N->getMemoryVT(); 01870 01871 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(), 01872 MemVT, isVolatile, isNonTemporal, isInvariant, 01873 Alignment, AAInfo); 01874 01875 // Remember the chain. 01876 Ch = Lo.getValue(1); 01877 01878 if (ExtType == ISD::SEXTLOAD) { 01879 // The high part is obtained by SRA'ing all but one of the bits of the 01880 // lo part. 01881 unsigned LoSize = Lo.getValueType().getSizeInBits(); 01882 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo, 01883 DAG.getConstant(LoSize-1, TLI.getPointerTy())); 01884 } else if (ExtType == ISD::ZEXTLOAD) { 01885 // The high part is just a zero. 01886 Hi = DAG.getConstant(0, NVT); 01887 } else { 01888 assert(ExtType == ISD::EXTLOAD && "Unknown extload!"); 01889 // The high part is undefined. 01890 Hi = DAG.getUNDEF(NVT); 01891 } 01892 } else if (TLI.isLittleEndian()) { 01893 // Little-endian - low bits are at low addresses. 01894 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(), 01895 isVolatile, isNonTemporal, isInvariant, Alignment, 01896 AAInfo); 01897 01898 unsigned ExcessBits = 01899 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits(); 01900 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits); 01901 01902 // Increment the pointer to the other half. 01903 unsigned IncrementSize = NVT.getSizeInBits()/8; 01904 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 01905 DAG.getConstant(IncrementSize, Ptr.getValueType())); 01906 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, 01907 N->getPointerInfo().getWithOffset(IncrementSize), NEVT, 01908 isVolatile, isNonTemporal, isInvariant, 01909 MinAlign(Alignment, IncrementSize), AAInfo); 01910 01911 // Build a factor node to remember that this load is independent of the 01912 // other one. 01913 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 01914 Hi.getValue(1)); 01915 } else { 01916 // Big-endian - high bits are at low addresses. Favor aligned loads at 01917 // the cost of some bit-fiddling. 01918 EVT MemVT = N->getMemoryVT(); 01919 unsigned EBytes = MemVT.getStoreSize(); 01920 unsigned IncrementSize = NVT.getSizeInBits()/8; 01921 unsigned ExcessBits = (EBytes - IncrementSize)*8; 01922 01923 // Load both the high bits and maybe some of the low bits. 01924 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(), 01925 EVT::getIntegerVT(*DAG.getContext(), 01926 MemVT.getSizeInBits() - ExcessBits), 01927 isVolatile, isNonTemporal, isInvariant, Alignment, 01928 AAInfo); 01929 01930 // Increment the pointer to the other half. 01931 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 01932 DAG.getConstant(IncrementSize, Ptr.getValueType())); 01933 // Load the rest of the low bits. 01934 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr, 01935 N->getPointerInfo().getWithOffset(IncrementSize), 01936 EVT::getIntegerVT(*DAG.getContext(), ExcessBits), 01937 isVolatile, isNonTemporal, isInvariant, 01938 MinAlign(Alignment, IncrementSize), AAInfo); 01939 01940 // Build a factor node to remember that this load is independent of the 01941 // other one. 01942 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 01943 Hi.getValue(1)); 01944 01945 if (ExcessBits < NVT.getSizeInBits()) { 01946 // Transfer low bits from the bottom of Hi to the top of Lo. 01947 Lo = DAG.getNode(ISD::OR, dl, NVT, Lo, 01948 DAG.getNode(ISD::SHL, dl, NVT, Hi, 01949 DAG.getConstant(ExcessBits, 01950 TLI.getPointerTy()))); 01951 // Move high bits to the right position in Hi. 01952 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, 01953 NVT, Hi, 01954 DAG.getConstant(NVT.getSizeInBits() - ExcessBits, 01955 TLI.getPointerTy())); 01956 } 01957 } 01958 01959 // Legalized the chain result - switch anything that used the old chain to 01960 // use the new one. 01961 ReplaceValueWith(SDValue(N, 1), Ch); 01962 } 01963 01964 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N, 01965 SDValue &Lo, SDValue &Hi) { 01966 SDLoc dl(N); 01967 SDValue LL, LH, RL, RH; 01968 GetExpandedInteger(N->getOperand(0), LL, LH); 01969 GetExpandedInteger(N->getOperand(1), RL, RH); 01970 Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL); 01971 Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH); 01972 } 01973 01974 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N, 01975 SDValue &Lo, SDValue &Hi) { 01976 EVT VT = N->getValueType(0); 01977 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); 01978 SDLoc dl(N); 01979 01980 SDValue LL, LH, RL, RH; 01981 GetExpandedInteger(N->getOperand(0), LL, LH); 01982 GetExpandedInteger(N->getOperand(1), RL, RH); 01983 01984 if (TLI.expandMUL(N, Lo, Hi, NVT, DAG, LL, LH, RL, RH)) 01985 return; 01986 01987 // If nothing else, we can make a libcall. 01988 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 01989 if (VT == MVT::i16) 01990 LC = RTLIB::MUL_I16; 01991 else if (VT == MVT::i32) 01992 LC = RTLIB::MUL_I32; 01993 else if (VT == MVT::i64) 01994 LC = RTLIB::MUL_I64; 01995 else if (VT == MVT::i128) 01996 LC = RTLIB::MUL_I128; 01997 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!"); 01998 01999 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) }; 02000 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, true/*irrelevant*/, 02001 dl).first, 02002 Lo, Hi); 02003 } 02004 02005 void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node, 02006 SDValue &Lo, SDValue &Hi) { 02007 SDValue LHS = Node->getOperand(0); 02008 SDValue RHS = Node->getOperand(1); 02009 SDLoc dl(Node); 02010 02011 // Expand the result by simply replacing it with the equivalent 02012 // non-overflow-checking operation. 02013 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ? 02014 ISD::ADD : ISD::SUB, dl, LHS.getValueType(), 02015 LHS, RHS); 02016 SplitInteger(Sum, Lo, Hi); 02017 02018 // Compute the overflow. 02019 // 02020 // LHSSign -> LHS >= 0 02021 // RHSSign -> RHS >= 0 02022 // SumSign -> Sum >= 0 02023 // 02024 // Add: 02025 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign) 02026 // Sub: 02027 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign) 02028 // 02029 EVT OType = Node->getValueType(1); 02030 SDValue Zero = DAG.getConstant(0, LHS.getValueType()); 02031 02032 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE); 02033 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE); 02034 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign, 02035 Node->getOpcode() == ISD::SADDO ? 02036 ISD::SETEQ : ISD::SETNE); 02037 02038 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE); 02039 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE); 02040 02041 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE); 02042 02043 // Use the calculated overflow everywhere. 02044 ReplaceValueWith(SDValue(Node, 1), Cmp); 02045 } 02046 02047 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N, 02048 SDValue &Lo, SDValue &Hi) { 02049 EVT VT = N->getValueType(0); 02050 SDLoc dl(N); 02051 02052 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 02053 if (VT == MVT::i16) 02054 LC = RTLIB::SDIV_I16; 02055 else if (VT == MVT::i32) 02056 LC = RTLIB::SDIV_I32; 02057 else if (VT == MVT::i64) 02058 LC = RTLIB::SDIV_I64; 02059 else if (VT == MVT::i128) 02060 LC = RTLIB::SDIV_I128; 02061 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!"); 02062 02063 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) }; 02064 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, true, dl).first, Lo, Hi); 02065 } 02066 02067 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N, 02068 SDValue &Lo, SDValue &Hi) { 02069 EVT VT = N->getValueType(0); 02070 SDLoc dl(N); 02071 02072 // If we can emit an efficient shift operation, do so now. Check to see if 02073 // the RHS is a constant. 02074 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1))) 02075 return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi); 02076 02077 // If we can determine that the high bit of the shift is zero or one, even if 02078 // the low bits are variable, emit this shift in an optimized form. 02079 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi)) 02080 return; 02081 02082 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc. 02083 unsigned PartsOpc; 02084 if (N->getOpcode() == ISD::SHL) { 02085 PartsOpc = ISD::SHL_PARTS; 02086 } else if (N->getOpcode() == ISD::SRL) { 02087 PartsOpc = ISD::SRL_PARTS; 02088 } else { 02089 assert(N->getOpcode() == ISD::SRA && "Unknown shift!"); 02090 PartsOpc = ISD::SRA_PARTS; 02091 } 02092 02093 // Next check to see if the target supports this SHL_PARTS operation or if it 02094 // will custom expand it. 02095 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); 02096 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT); 02097 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || 02098 Action == TargetLowering::Custom) { 02099 // Expand the subcomponents. 02100 SDValue LHSL, LHSH; 02101 GetExpandedInteger(N->getOperand(0), LHSL, LHSH); 02102 EVT VT = LHSL.getValueType(); 02103 02104 // If the shift amount operand is coming from a vector legalization it may 02105 // have an illegal type. Fix that first by casting the operand, otherwise 02106 // the new SHL_PARTS operation would need further legalization. 02107 SDValue ShiftOp = N->getOperand(1); 02108 EVT ShiftTy = TLI.getShiftAmountTy(VT); 02109 assert(ShiftTy.getScalarType().getSizeInBits() >= 02110 Log2_32_Ceil(VT.getScalarType().getSizeInBits()) && 02111 "ShiftAmountTy is too small to cover the range of this type!"); 02112 if (ShiftOp.getValueType() != ShiftTy) 02113 ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy); 02114 02115 SDValue Ops[] = { LHSL, LHSH, ShiftOp }; 02116 Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops); 02117 Hi = Lo.getValue(1); 02118 return; 02119 } 02120 02121 // Otherwise, emit a libcall. 02122 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 02123 bool isSigned; 02124 if (N->getOpcode() == ISD::SHL) { 02125 isSigned = false; /*sign irrelevant*/ 02126 if (VT == MVT::i16) 02127 LC = RTLIB::SHL_I16; 02128 else if (VT == MVT::i32) 02129 LC = RTLIB::SHL_I32; 02130 else if (VT == MVT::i64) 02131 LC = RTLIB::SHL_I64; 02132 else if (VT == MVT::i128) 02133 LC = RTLIB::SHL_I128; 02134 } else if (N->getOpcode() == ISD::SRL) { 02135 isSigned = false; 02136 if (VT == MVT::i16) 02137 LC = RTLIB::SRL_I16; 02138 else if (VT == MVT::i32) 02139 LC = RTLIB::SRL_I32; 02140 else if (VT == MVT::i64) 02141 LC = RTLIB::SRL_I64; 02142 else if (VT == MVT::i128) 02143 LC = RTLIB::SRL_I128; 02144 } else { 02145 assert(N->getOpcode() == ISD::SRA && "Unknown shift!"); 02146 isSigned = true; 02147 if (VT == MVT::i16) 02148 LC = RTLIB::SRA_I16; 02149 else if (VT == MVT::i32) 02150 LC = RTLIB::SRA_I32; 02151 else if (VT == MVT::i64) 02152 LC = RTLIB::SRA_I64; 02153 else if (VT == MVT::i128) 02154 LC = RTLIB::SRA_I128; 02155 } 02156 02157 if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) { 02158 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) }; 02159 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, isSigned, dl).first, Lo, 02160 Hi); 02161 return; 02162 } 02163 02164 if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi)) 02165 llvm_unreachable("Unsupported shift!"); 02166 } 02167 02168 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N, 02169 SDValue &Lo, SDValue &Hi) { 02170 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 02171 SDLoc dl(N); 02172 SDValue Op = N->getOperand(0); 02173 if (Op.getValueType().bitsLE(NVT)) { 02174 // The low part is sign extension of the input (degenerates to a copy). 02175 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0)); 02176 // The high part is obtained by SRA'ing all but one of the bits of low part. 02177 unsigned LoSize = NVT.getSizeInBits(); 02178 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo, 02179 DAG.getConstant(LoSize-1, TLI.getPointerTy())); 02180 } else { 02181 // For example, extension of an i48 to an i64. The operand type necessarily 02182 // promotes to the result type, so will end up being expanded too. 02183 assert(getTypeAction(Op.getValueType()) == 02184 TargetLowering::TypePromoteInteger && 02185 "Only know how to promote this result!"); 02186 SDValue Res = GetPromotedInteger(Op); 02187 assert(Res.getValueType() == N->getValueType(0) && 02188 "Operand over promoted?"); 02189 // Split the promoted operand. This will simplify when it is expanded. 02190 SplitInteger(Res, Lo, Hi); 02191 unsigned ExcessBits = 02192 Op.getValueType().getSizeInBits() - NVT.getSizeInBits(); 02193 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi, 02194 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 02195 ExcessBits))); 02196 } 02197 } 02198 02199 void DAGTypeLegalizer:: 02200 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) { 02201 SDLoc dl(N); 02202 GetExpandedInteger(N->getOperand(0), Lo, Hi); 02203 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); 02204 02205 if (EVT.bitsLE(Lo.getValueType())) { 02206 // sext_inreg the low part if needed. 02207 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo, 02208 N->getOperand(1)); 02209 02210 // The high part gets the sign extension from the lo-part. This handles 02211 // things like sextinreg V:i64 from i8. 02212 Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo, 02213 DAG.getConstant(Hi.getValueType().getSizeInBits()-1, 02214 TLI.getPointerTy())); 02215 } else { 02216 // For example, extension of an i48 to an i64. Leave the low part alone, 02217 // sext_inreg the high part. 02218 unsigned ExcessBits = 02219 EVT.getSizeInBits() - Lo.getValueType().getSizeInBits(); 02220 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi, 02221 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 02222 ExcessBits))); 02223 } 02224 } 02225 02226 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N, 02227 SDValue &Lo, SDValue &Hi) { 02228 EVT VT = N->getValueType(0); 02229 SDLoc dl(N); 02230 02231 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 02232 if (VT == MVT::i16) 02233 LC = RTLIB::SREM_I16; 02234 else if (VT == MVT::i32) 02235 LC = RTLIB::SREM_I32; 02236 else if (VT == MVT::i64) 02237 LC = RTLIB::SREM_I64; 02238 else if (VT == MVT::i128) 02239 LC = RTLIB::SREM_I128; 02240 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!"); 02241 02242 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) }; 02243 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, true, dl).first, Lo, Hi); 02244 } 02245 02246 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N, 02247 SDValue &Lo, SDValue &Hi) { 02248 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 02249 SDLoc dl(N); 02250 Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0)); 02251 Hi = DAG.getNode(ISD::SRL, dl, 02252 N->getOperand(0).getValueType(), N->getOperand(0), 02253 DAG.getConstant(NVT.getSizeInBits(), TLI.getPointerTy())); 02254 Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi); 02255 } 02256 02257 void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N, 02258 SDValue &Lo, SDValue &Hi) { 02259 SDValue LHS = N->getOperand(0); 02260 SDValue RHS = N->getOperand(1); 02261 SDLoc dl(N); 02262 02263 // Expand the result by simply replacing it with the equivalent 02264 // non-overflow-checking operation. 02265 SDValue Sum = DAG.getNode(N->getOpcode() == ISD::UADDO ? 02266 ISD::ADD : ISD::SUB, dl, LHS.getValueType(), 02267 LHS, RHS); 02268 SplitInteger(Sum, Lo, Hi); 02269 02270 // Calculate the overflow: addition overflows iff a + b < a, and subtraction 02271 // overflows iff a - b > a. 02272 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS, 02273 N->getOpcode () == ISD::UADDO ? 02274 ISD::SETULT : ISD::SETUGT); 02275 02276 // Use the calculated overflow everywhere. 02277 ReplaceValueWith(SDValue(N, 1), Ofl); 02278 } 02279 02280 void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N, 02281 SDValue &Lo, SDValue &Hi) { 02282 EVT VT = N->getValueType(0); 02283 SDLoc dl(N); 02284 02285 // A divide for UMULO should be faster than a function call. 02286 if (N->getOpcode() == ISD::UMULO) { 02287 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1); 02288 02289 SDValue MUL = DAG.getNode(ISD::MUL, dl, LHS.getValueType(), LHS, RHS); 02290 SplitInteger(MUL, Lo, Hi); 02291 02292 // A divide for UMULO will be faster than a function call. Select to 02293 // make sure we aren't using 0. 02294 SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(VT), 02295 RHS, DAG.getConstant(0, VT), ISD::SETEQ); 02296 SDValue NotZero = DAG.getSelect(dl, VT, isZero, 02297 DAG.getConstant(1, VT), RHS); 02298 SDValue DIV = DAG.getNode(ISD::UDIV, dl, VT, MUL, NotZero); 02299 SDValue Overflow = DAG.getSetCC(dl, N->getValueType(1), DIV, LHS, 02300 ISD::SETNE); 02301 Overflow = DAG.getSelect(dl, N->getValueType(1), isZero, 02302 DAG.getConstant(0, N->getValueType(1)), 02303 Overflow); 02304 ReplaceValueWith(SDValue(N, 1), Overflow); 02305 return; 02306 } 02307 02308 Type *RetTy = VT.getTypeForEVT(*DAG.getContext()); 02309 EVT PtrVT = TLI.getPointerTy(); 02310 Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext()); 02311 02312 // Replace this with a libcall that will check overflow. 02313 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 02314 if (VT == MVT::i32) 02315 LC = RTLIB::MULO_I32; 02316 else if (VT == MVT::i64) 02317 LC = RTLIB::MULO_I64; 02318 else if (VT == MVT::i128) 02319 LC = RTLIB::MULO_I128; 02320 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XMULO!"); 02321 02322 SDValue Temp = DAG.CreateStackTemporary(PtrVT); 02323 // Temporary for the overflow value, default it to zero. 02324 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, 02325 DAG.getConstant(0, PtrVT), Temp, 02326 MachinePointerInfo(), false, false, 0); 02327 02328 TargetLowering::ArgListTy Args; 02329 TargetLowering::ArgListEntry Entry; 02330 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 02331 EVT ArgVT = N->getOperand(i).getValueType(); 02332 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 02333 Entry.Node = N->getOperand(i); 02334 Entry.Ty = ArgTy; 02335 Entry.isSExt = true; 02336 Entry.isZExt = false; 02337 Args.push_back(Entry); 02338 } 02339 02340 // Also pass the address of the overflow check. 02341 Entry.Node = Temp; 02342 Entry.Ty = PtrTy->getPointerTo(); 02343 Entry.isSExt = true; 02344 Entry.isZExt = false; 02345 Args.push_back(Entry); 02346 02347 SDValue Func = DAG.getExternalSymbol(TLI.getLibcallName(LC), PtrVT); 02348 02349 TargetLowering::CallLoweringInfo CLI(DAG); 02350 CLI.setDebugLoc(dl).setChain(Chain) 02351 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Func, std::move(Args), 0) 02352 .setSExtResult(); 02353 02354 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 02355 02356 SplitInteger(CallInfo.first, Lo, Hi); 02357 SDValue Temp2 = DAG.getLoad(PtrVT, dl, CallInfo.second, Temp, 02358 MachinePointerInfo(), false, false, false, 0); 02359 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2, 02360 DAG.getConstant(0, PtrVT), 02361 ISD::SETNE); 02362 // Use the overflow from the libcall everywhere. 02363 ReplaceValueWith(SDValue(N, 1), Ofl); 02364 } 02365 02366 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N, 02367 SDValue &Lo, SDValue &Hi) { 02368 EVT VT = N->getValueType(0); 02369 SDLoc dl(N); 02370 02371 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 02372 if (VT == MVT::i16) 02373 LC = RTLIB::UDIV_I16; 02374 else if (VT == MVT::i32) 02375 LC = RTLIB::UDIV_I32; 02376 else if (VT == MVT::i64) 02377 LC = RTLIB::UDIV_I64; 02378 else if (VT == MVT::i128) 02379 LC = RTLIB::UDIV_I128; 02380 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!"); 02381 02382 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) }; 02383 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, false, dl).first, Lo, Hi); 02384 } 02385 02386 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N, 02387 SDValue &Lo, SDValue &Hi) { 02388 EVT VT = N->getValueType(0); 02389 SDLoc dl(N); 02390 02391 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 02392 if (VT == MVT::i16) 02393 LC = RTLIB::UREM_I16; 02394 else if (VT == MVT::i32) 02395 LC = RTLIB::UREM_I32; 02396 else if (VT == MVT::i64) 02397 LC = RTLIB::UREM_I64; 02398 else if (VT == MVT::i128) 02399 LC = RTLIB::UREM_I128; 02400 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!"); 02401 02402 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) }; 02403 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, false, dl).first, Lo, Hi); 02404 } 02405 02406 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N, 02407 SDValue &Lo, SDValue &Hi) { 02408 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0)); 02409 SDLoc dl(N); 02410 SDValue Op = N->getOperand(0); 02411 if (Op.getValueType().bitsLE(NVT)) { 02412 // The low part is zero extension of the input (degenerates to a copy). 02413 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0)); 02414 Hi = DAG.getConstant(0, NVT); // The high part is just a zero. 02415 } else { 02416 // For example, extension of an i48 to an i64. The operand type necessarily 02417 // promotes to the result type, so will end up being expanded too. 02418 assert(getTypeAction(Op.getValueType()) == 02419 TargetLowering::TypePromoteInteger && 02420 "Only know how to promote this result!"); 02421 SDValue Res = GetPromotedInteger(Op); 02422 assert(Res.getValueType() == N->getValueType(0) && 02423 "Operand over promoted?"); 02424 // Split the promoted operand. This will simplify when it is expanded. 02425 SplitInteger(Res, Lo, Hi); 02426 unsigned ExcessBits = 02427 Op.getValueType().getSizeInBits() - NVT.getSizeInBits(); 02428 Hi = DAG.getZeroExtendInReg(Hi, dl, 02429 EVT::getIntegerVT(*DAG.getContext(), 02430 ExcessBits)); 02431 } 02432 } 02433 02434 void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N, 02435 SDValue &Lo, SDValue &Hi) { 02436 SDLoc dl(N); 02437 EVT VT = cast<AtomicSDNode>(N)->getMemoryVT(); 02438 SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other); 02439 SDValue Zero = DAG.getConstant(0, VT); 02440 SDValue Swap = DAG.getAtomicCmpSwap( 02441 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, dl, 02442 cast<AtomicSDNode>(N)->getMemoryVT(), VTs, N->getOperand(0), 02443 N->getOperand(1), Zero, Zero, cast<AtomicSDNode>(N)->getMemOperand(), 02444 cast<AtomicSDNode>(N)->getOrdering(), 02445 cast<AtomicSDNode>(N)->getOrdering(), 02446 cast<AtomicSDNode>(N)->getSynchScope()); 02447 02448 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0)); 02449 ReplaceValueWith(SDValue(N, 1), Swap.getValue(2)); 02450 } 02451 02452 //===----------------------------------------------------------------------===// 02453 // Integer Operand Expansion 02454 //===----------------------------------------------------------------------===// 02455 02456 /// ExpandIntegerOperand - This method is called when the specified operand of 02457 /// the specified node is found to need expansion. At this point, all of the 02458 /// result types of the node are known to be legal, but other operands of the 02459 /// node may need promotion or expansion as well as the specified one. 02460 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) { 02461 DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG); dbgs() << "\n"); 02462 SDValue Res = SDValue(); 02463 02464 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) 02465 return false; 02466 02467 switch (N->getOpcode()) { 02468 default: 02469 #ifndef NDEBUG 02470 dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": "; 02471 N->dump(&DAG); dbgs() << "\n"; 02472 #endif 02473 llvm_unreachable("Do not know how to expand this operator's operand!"); 02474 02475 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break; 02476 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break; 02477 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break; 02478 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break; 02479 case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break; 02480 case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break; 02481 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break; 02482 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break; 02483 case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break; 02484 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break; 02485 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break; 02486 case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break; 02487 02488 case ISD::SHL: 02489 case ISD::SRA: 02490 case ISD::SRL: 02491 case ISD::ROTL: 02492 case ISD::ROTR: Res = ExpandIntOp_Shift(N); break; 02493 case ISD::RETURNADDR: 02494 case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break; 02495 02496 case ISD::ATOMIC_STORE: Res = ExpandIntOp_ATOMIC_STORE(N); break; 02497 } 02498 02499 // If the result is null, the sub-method took care of registering results etc. 02500 if (!Res.getNode()) return false; 02501 02502 // If the result is N, the sub-method updated N in place. Tell the legalizer 02503 // core about this. 02504 if (Res.getNode() == N) 02505 return true; 02506 02507 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 && 02508 "Invalid operand expansion"); 02509 02510 ReplaceValueWith(SDValue(N, 0), Res); 02511 return false; 02512 } 02513 02514 /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code 02515 /// is shared among BR_CC, SELECT_CC, and SETCC handlers. 02516 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS, 02517 SDValue &NewRHS, 02518 ISD::CondCode &CCCode, 02519 SDLoc dl) { 02520 SDValue LHSLo, LHSHi, RHSLo, RHSHi; 02521 GetExpandedInteger(NewLHS, LHSLo, LHSHi); 02522 GetExpandedInteger(NewRHS, RHSLo, RHSHi); 02523 02524 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) { 02525 if (RHSLo == RHSHi) { 02526 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) { 02527 if (RHSCST->isAllOnesValue()) { 02528 // Equality comparison to -1. 02529 NewLHS = DAG.getNode(ISD::AND, dl, 02530 LHSLo.getValueType(), LHSLo, LHSHi); 02531 NewRHS = RHSLo; 02532 return; 02533 } 02534 } 02535 } 02536 02537 NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo); 02538 NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi); 02539 NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS); 02540 NewRHS = DAG.getConstant(0, NewLHS.getValueType()); 02541 return; 02542 } 02543 02544 // If this is a comparison of the sign bit, just look at the top part. 02545 // X > -1, x < 0 02546 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS)) 02547 if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0 02548 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1 02549 NewLHS = LHSHi; 02550 NewRHS = RHSHi; 02551 return; 02552 } 02553 02554 // FIXME: This generated code sucks. 02555 ISD::CondCode LowCC; 02556 switch (CCCode) { 02557 default: llvm_unreachable("Unknown integer setcc!"); 02558 case ISD::SETLT: 02559 case ISD::SETULT: LowCC = ISD::SETULT; break; 02560 case ISD::SETGT: 02561 case ISD::SETUGT: LowCC = ISD::SETUGT; break; 02562 case ISD::SETLE: 02563 case ISD::SETULE: LowCC = ISD::SETULE; break; 02564 case ISD::SETGE: 02565 case ISD::SETUGE: LowCC = ISD::SETUGE; break; 02566 } 02567 02568 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison 02569 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands 02570 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; 02571 02572 // NOTE: on targets without efficient SELECT of bools, we can always use 02573 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) 02574 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true, 02575 nullptr); 02576 SDValue Tmp1, Tmp2; 02577 if (TLI.isTypeLegal(LHSLo.getValueType()) && 02578 TLI.isTypeLegal(RHSLo.getValueType())) 02579 Tmp1 = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()), 02580 LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl); 02581 if (!Tmp1.getNode()) 02582 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()), 02583 LHSLo, RHSLo, LowCC); 02584 if (TLI.isTypeLegal(LHSHi.getValueType()) && 02585 TLI.isTypeLegal(RHSHi.getValueType())) 02586 Tmp2 = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()), 02587 LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl); 02588 if (!Tmp2.getNode()) 02589 Tmp2 = DAG.getNode(ISD::SETCC, dl, 02590 getSetCCResultType(LHSHi.getValueType()), 02591 LHSHi, RHSHi, DAG.getCondCode(CCCode)); 02592 02593 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode()); 02594 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode()); 02595 if ((Tmp1C && Tmp1C->isNullValue()) || 02596 (Tmp2C && Tmp2C->isNullValue() && 02597 (CCCode == ISD::SETLE || CCCode == ISD::SETGE || 02598 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) || 02599 (Tmp2C && Tmp2C->getAPIntValue() == 1 && 02600 (CCCode == ISD::SETLT || CCCode == ISD::SETGT || 02601 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) { 02602 // low part is known false, returns high part. 02603 // For LE / GE, if high part is known false, ignore the low part. 02604 // For LT / GT, if high part is known true, ignore the low part. 02605 NewLHS = Tmp2; 02606 NewRHS = SDValue(); 02607 return; 02608 } 02609 02610 NewLHS = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()), 02611 LHSHi, RHSHi, ISD::SETEQ, false, 02612 DagCombineInfo, dl); 02613 if (!NewLHS.getNode()) 02614 NewLHS = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()), 02615 LHSHi, RHSHi, ISD::SETEQ); 02616 NewLHS = DAG.getSelect(dl, Tmp1.getValueType(), 02617 NewLHS, Tmp1, Tmp2); 02618 NewRHS = SDValue(); 02619 } 02620 02621 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) { 02622 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3); 02623 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get(); 02624 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N)); 02625 02626 // If ExpandSetCCOperands returned a scalar, we need to compare the result 02627 // against zero to select between true and false values. 02628 if (!NewRHS.getNode()) { 02629 NewRHS = DAG.getConstant(0, NewLHS.getValueType()); 02630 CCCode = ISD::SETNE; 02631 } 02632 02633 // Update N to have the operands specified. 02634 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), 02635 DAG.getCondCode(CCCode), NewLHS, NewRHS, 02636 N->getOperand(4)), 0); 02637 } 02638 02639 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) { 02640 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1); 02641 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get(); 02642 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N)); 02643 02644 // If ExpandSetCCOperands returned a scalar, we need to compare the result 02645 // against zero to select between true and false values. 02646 if (!NewRHS.getNode()) { 02647 NewRHS = DAG.getConstant(0, NewLHS.getValueType()); 02648 CCCode = ISD::SETNE; 02649 } 02650 02651 // Update N to have the operands specified. 02652 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS, 02653 N->getOperand(2), N->getOperand(3), 02654 DAG.getCondCode(CCCode)), 0); 02655 } 02656 02657 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) { 02658 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1); 02659 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get(); 02660 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N)); 02661 02662 // If ExpandSetCCOperands returned a scalar, use it. 02663 if (!NewRHS.getNode()) { 02664 assert(NewLHS.getValueType() == N->getValueType(0) && 02665 "Unexpected setcc expansion!"); 02666 return NewLHS; 02667 } 02668 02669 // Otherwise, update N to have the operands specified. 02670 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS, 02671 DAG.getCondCode(CCCode)), 0); 02672 } 02673 02674 SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) { 02675 // The value being shifted is legal, but the shift amount is too big. 02676 // It follows that either the result of the shift is undefined, or the 02677 // upper half of the shift amount is zero. Just use the lower half. 02678 SDValue Lo, Hi; 02679 GetExpandedInteger(N->getOperand(1), Lo, Hi); 02680 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0); 02681 } 02682 02683 SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) { 02684 // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This 02685 // surely makes pretty nice problems on 8/16 bit targets. Just truncate this 02686 // constant to valid type. 02687 SDValue Lo, Hi; 02688 GetExpandedInteger(N->getOperand(0), Lo, Hi); 02689 return SDValue(DAG.UpdateNodeOperands(N, Lo), 0); 02690 } 02691 02692 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) { 02693 SDValue Op = N->getOperand(0); 02694 EVT DstVT = N->getValueType(0); 02695 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT); 02696 assert(LC != RTLIB::UNKNOWN_LIBCALL && 02697 "Don't know how to expand this SINT_TO_FP!"); 02698 return TLI.makeLibCall(DAG, LC, DstVT, &Op, 1, true, SDLoc(N)).first; 02699 } 02700 02701 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) { 02702 if (ISD::isNormalStore(N)) 02703 return ExpandOp_NormalStore(N, OpNo); 02704 02705 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!"); 02706 assert(OpNo == 1 && "Can only expand the stored value so far"); 02707 02708 EVT VT = N->getOperand(1).getValueType(); 02709 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); 02710 SDValue Ch = N->getChain(); 02711 SDValue Ptr = N->getBasePtr(); 02712 unsigned Alignment = N->getAlignment(); 02713 bool isVolatile = N->isVolatile(); 02714 bool isNonTemporal = N->isNonTemporal(); 02715 AAMDNodes AAInfo = N->getAAInfo(); 02716 SDLoc dl(N); 02717 SDValue Lo, Hi; 02718 02719 assert(NVT.isByteSized() && "Expanded type not byte sized!"); 02720 02721 if (N->getMemoryVT().bitsLE(NVT)) { 02722 GetExpandedInteger(N->getValue(), Lo, Hi); 02723 return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(), 02724 N->getMemoryVT(), isVolatile, isNonTemporal, 02725 Alignment, AAInfo); 02726 } 02727 02728 if (TLI.isLittleEndian()) { 02729 // Little-endian - low bits are at low addresses. 02730 GetExpandedInteger(N->getValue(), Lo, Hi); 02731 02732 Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(), 02733 isVolatile, isNonTemporal, Alignment, AAInfo); 02734 02735 unsigned ExcessBits = 02736 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits(); 02737 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits); 02738 02739 // Increment the pointer to the other half. 02740 unsigned IncrementSize = NVT.getSizeInBits()/8; 02741 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 02742 DAG.getConstant(IncrementSize, Ptr.getValueType())); 02743 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, 02744 N->getPointerInfo().getWithOffset(IncrementSize), 02745 NEVT, isVolatile, isNonTemporal, 02746 MinAlign(Alignment, IncrementSize), AAInfo); 02747 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 02748 } 02749 02750 // Big-endian - high bits are at low addresses. Favor aligned stores at 02751 // the cost of some bit-fiddling. 02752 GetExpandedInteger(N->getValue(), Lo, Hi); 02753 02754 EVT ExtVT = N->getMemoryVT(); 02755 unsigned EBytes = ExtVT.getStoreSize(); 02756 unsigned IncrementSize = NVT.getSizeInBits()/8; 02757 unsigned ExcessBits = (EBytes - IncrementSize)*8; 02758 EVT HiVT = EVT::getIntegerVT(*DAG.getContext(), 02759 ExtVT.getSizeInBits() - ExcessBits); 02760 02761 if (ExcessBits < NVT.getSizeInBits()) { 02762 // Transfer high bits from the top of Lo to the bottom of Hi. 02763 Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi, 02764 DAG.getConstant(NVT.getSizeInBits() - ExcessBits, 02765 TLI.getPointerTy())); 02766 Hi = DAG.getNode(ISD::OR, dl, NVT, Hi, 02767 DAG.getNode(ISD::SRL, dl, NVT, Lo, 02768 DAG.getConstant(ExcessBits, 02769 TLI.getPointerTy()))); 02770 } 02771 02772 // Store both the high bits and maybe some of the low bits. 02773 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(), 02774 HiVT, isVolatile, isNonTemporal, Alignment, AAInfo); 02775 02776 // Increment the pointer to the other half. 02777 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 02778 DAG.getConstant(IncrementSize, Ptr.getValueType())); 02779 // Store the lowest ExcessBits bits in the second half. 02780 Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr, 02781 N->getPointerInfo().getWithOffset(IncrementSize), 02782 EVT::getIntegerVT(*DAG.getContext(), ExcessBits), 02783 isVolatile, isNonTemporal, 02784 MinAlign(Alignment, IncrementSize), AAInfo); 02785 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 02786 } 02787 02788 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) { 02789 SDValue InL, InH; 02790 GetExpandedInteger(N->getOperand(0), InL, InH); 02791 // Just truncate the low part of the source. 02792 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL); 02793 } 02794 02795 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) { 02796 SDValue Op = N->getOperand(0); 02797 EVT SrcVT = Op.getValueType(); 02798 EVT DstVT = N->getValueType(0); 02799 SDLoc dl(N); 02800 02801 // The following optimization is valid only if every value in SrcVT (when 02802 // treated as signed) is representable in DstVT. Check that the mantissa 02803 // size of DstVT is >= than the number of bits in SrcVT -1. 02804 const fltSemantics &sem = DAG.EVTToAPFloatSemantics(DstVT); 02805 if (APFloat::semanticsPrecision(sem) >= SrcVT.getSizeInBits()-1 && 02806 TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){ 02807 // Do a signed conversion then adjust the result. 02808 SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op); 02809 SignedConv = TLI.LowerOperation(SignedConv, DAG); 02810 02811 // The result of the signed conversion needs adjusting if the 'sign bit' of 02812 // the incoming integer was set. To handle this, we dynamically test to see 02813 // if it is set, and, if so, add a fudge factor. 02814 02815 const uint64_t F32TwoE32 = 0x4F800000ULL; 02816 const uint64_t F32TwoE64 = 0x5F800000ULL; 02817 const uint64_t F32TwoE128 = 0x7F800000ULL; 02818 02819 APInt FF(32, 0); 02820 if (SrcVT == MVT::i32) 02821 FF = APInt(32, F32TwoE32); 02822 else if (SrcVT == MVT::i64) 02823 FF = APInt(32, F32TwoE64); 02824 else if (SrcVT == MVT::i128) 02825 FF = APInt(32, F32TwoE128); 02826 else 02827 llvm_unreachable("Unsupported UINT_TO_FP!"); 02828 02829 // Check whether the sign bit is set. 02830 SDValue Lo, Hi; 02831 GetExpandedInteger(Op, Lo, Hi); 02832 SDValue SignSet = DAG.getSetCC(dl, 02833 getSetCCResultType(Hi.getValueType()), 02834 Hi, DAG.getConstant(0, Hi.getValueType()), 02835 ISD::SETLT); 02836 02837 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits. 02838 SDValue FudgePtr = DAG.getConstantPool( 02839 ConstantInt::get(*DAG.getContext(), FF.zext(64)), 02840 TLI.getPointerTy()); 02841 02842 // Get a pointer to FF if the sign bit was set, or to 0 otherwise. 02843 SDValue Zero = DAG.getIntPtrConstant(0); 02844 SDValue Four = DAG.getIntPtrConstant(4); 02845 if (TLI.isBigEndian()) std::swap(Zero, Four); 02846 SDValue Offset = DAG.getSelect(dl, Zero.getValueType(), SignSet, 02847 Zero, Four); 02848 unsigned Alignment = cast<ConstantPoolSDNode>(FudgePtr)->getAlignment(); 02849 FudgePtr = DAG.getNode(ISD::ADD, dl, FudgePtr.getValueType(), 02850 FudgePtr, Offset); 02851 Alignment = std::min(Alignment, 4u); 02852 02853 // Load the value out, extending it from f32 to the destination float type. 02854 // FIXME: Avoid the extend by constructing the right constant pool? 02855 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(), 02856 FudgePtr, 02857 MachinePointerInfo::getConstantPool(), 02858 MVT::f32, 02859 false, false, false, Alignment); 02860 return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge); 02861 } 02862 02863 // Otherwise, use a libcall. 02864 RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT); 02865 assert(LC != RTLIB::UNKNOWN_LIBCALL && 02866 "Don't know how to expand this UINT_TO_FP!"); 02867 return TLI.makeLibCall(DAG, LC, DstVT, &Op, 1, true, dl).first; 02868 } 02869 02870 SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) { 02871 SDLoc dl(N); 02872 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl, 02873 cast<AtomicSDNode>(N)->getMemoryVT(), 02874 N->getOperand(0), 02875 N->getOperand(1), N->getOperand(2), 02876 cast<AtomicSDNode>(N)->getMemOperand(), 02877 cast<AtomicSDNode>(N)->getOrdering(), 02878 cast<AtomicSDNode>(N)->getSynchScope()); 02879 return Swap.getValue(1); 02880 } 02881 02882 02883 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) { 02884 SDValue InOp0 = N->getOperand(0); 02885 EVT InVT = InOp0.getValueType(); 02886 02887 EVT OutVT = N->getValueType(0); 02888 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT); 02889 assert(NOutVT.isVector() && "This type must be promoted to a vector type"); 02890 unsigned OutNumElems = OutVT.getVectorNumElements(); 02891 EVT NOutVTElem = NOutVT.getVectorElementType(); 02892 02893 SDLoc dl(N); 02894 SDValue BaseIdx = N->getOperand(1); 02895 02896 SmallVector<SDValue, 8> Ops; 02897 Ops.reserve(OutNumElems); 02898 for (unsigned i = 0; i != OutNumElems; ++i) { 02899 02900 // Extract the element from the original vector. 02901 SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(), 02902 BaseIdx, DAG.getConstant(i, BaseIdx.getValueType())); 02903 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, 02904 InVT.getVectorElementType(), N->getOperand(0), Index); 02905 02906 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, Ext); 02907 // Insert the converted element to the new vector. 02908 Ops.push_back(Op); 02909 } 02910 02911 return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, Ops); 02912 } 02913 02914 02915 SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) { 02916 ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N); 02917 EVT VT = N->getValueType(0); 02918 SDLoc dl(N); 02919 02920 unsigned NumElts = VT.getVectorNumElements(); 02921 SmallVector<int, 8> NewMask; 02922 for (unsigned i = 0; i != NumElts; ++i) { 02923 NewMask.push_back(SV->getMaskElt(i)); 02924 } 02925 02926 SDValue V0 = GetPromotedInteger(N->getOperand(0)); 02927 SDValue V1 = GetPromotedInteger(N->getOperand(1)); 02928 EVT OutVT = V0.getValueType(); 02929 02930 return DAG.getVectorShuffle(OutVT, dl, V0, V1, &NewMask[0]); 02931 } 02932 02933 02934 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) { 02935 EVT OutVT = N->getValueType(0); 02936 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT); 02937 assert(NOutVT.isVector() && "This type must be promoted to a vector type"); 02938 unsigned NumElems = N->getNumOperands(); 02939 EVT NOutVTElem = NOutVT.getVectorElementType(); 02940 02941 SDLoc dl(N); 02942 02943 SmallVector<SDValue, 8> Ops; 02944 Ops.reserve(NumElems); 02945 for (unsigned i = 0; i != NumElems; ++i) { 02946 SDValue Op; 02947 // BUILD_VECTOR integer operand types are allowed to be larger than the 02948 // result's element type. This may still be true after the promotion. For 02949 // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to 02950 // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>. 02951 if (N->getOperand(i).getValueType().bitsLT(NOutVTElem)) 02952 Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(i)); 02953 else 02954 Op = N->getOperand(i); 02955 Ops.push_back(Op); 02956 } 02957 02958 return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, Ops); 02959 } 02960 02961 SDValue DAGTypeLegalizer::PromoteIntRes_SCALAR_TO_VECTOR(SDNode *N) { 02962 02963 SDLoc dl(N); 02964 02965 assert(!N->getOperand(0).getValueType().isVector() && 02966 "Input must be a scalar"); 02967 02968 EVT OutVT = N->getValueType(0); 02969 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT); 02970 assert(NOutVT.isVector() && "This type must be promoted to a vector type"); 02971 EVT NOutVTElem = NOutVT.getVectorElementType(); 02972 02973 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(0)); 02974 02975 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NOutVT, Op); 02976 } 02977 02978 SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) { 02979 SDLoc dl(N); 02980 02981 EVT OutVT = N->getValueType(0); 02982 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT); 02983 assert(NOutVT.isVector() && "This type must be promoted to a vector type"); 02984 02985 EVT InElemTy = OutVT.getVectorElementType(); 02986 EVT OutElemTy = NOutVT.getVectorElementType(); 02987 02988 unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements(); 02989 unsigned NumOutElem = NOutVT.getVectorNumElements(); 02990 unsigned NumOperands = N->getNumOperands(); 02991 assert(NumElem * NumOperands == NumOutElem && 02992 "Unexpected number of elements"); 02993 02994 // Take the elements from the first vector. 02995 SmallVector<SDValue, 8> Ops(NumOutElem); 02996 for (unsigned i = 0; i < NumOperands; ++i) { 02997 SDValue Op = N->getOperand(i); 02998 for (unsigned j = 0; j < NumElem; ++j) { 02999 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, 03000 InElemTy, Op, DAG.getConstant(j, 03001 TLI.getVectorIdxTy())); 03002 Ops[i * NumElem + j] = DAG.getNode(ISD::ANY_EXTEND, dl, OutElemTy, Ext); 03003 } 03004 } 03005 03006 return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, Ops); 03007 } 03008 03009 SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) { 03010 EVT OutVT = N->getValueType(0); 03011 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT); 03012 assert(NOutVT.isVector() && "This type must be promoted to a vector type"); 03013 03014 EVT NOutVTElem = NOutVT.getVectorElementType(); 03015 03016 SDLoc dl(N); 03017 SDValue V0 = GetPromotedInteger(N->getOperand(0)); 03018 03019 SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl, 03020 NOutVTElem, N->getOperand(1)); 03021 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT, 03022 V0, ConvElem, N->getOperand(2)); 03023 } 03024 03025 SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) { 03026 SDLoc dl(N); 03027 SDValue V0 = GetPromotedInteger(N->getOperand(0)); 03028 SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl, TLI.getVectorIdxTy()); 03029 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, 03030 V0->getValueType(0).getScalarType(), V0, V1); 03031 03032 // EXTRACT_VECTOR_ELT can return types which are wider than the incoming 03033 // element types. If this is the case then we need to expand the outgoing 03034 // value and not truncate it. 03035 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0)); 03036 } 03037 03038 SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) { 03039 SDLoc dl(N); 03040 unsigned NumElems = N->getNumOperands(); 03041 03042 EVT RetSclrTy = N->getValueType(0).getVectorElementType(); 03043 03044 SmallVector<SDValue, 8> NewOps; 03045 NewOps.reserve(NumElems); 03046 03047 // For each incoming vector 03048 for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) { 03049 SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx)); 03050 EVT SclrTy = Incoming->getValueType(0).getVectorElementType(); 03051 unsigned NumElem = Incoming->getValueType(0).getVectorNumElements(); 03052 03053 for (unsigned i=0; i<NumElem; ++i) { 03054 // Extract element from incoming vector 03055 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, 03056 Incoming, DAG.getConstant(i, TLI.getVectorIdxTy())); 03057 SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex); 03058 NewOps.push_back(Tr); 03059 } 03060 } 03061 03062 return DAG.getNode(ISD::BUILD_VECTOR, dl, N->getValueType(0), NewOps); 03063 }