1 //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the SelectionDAG::Legalize method. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/SetVector.h" 15 #include "llvm/ADT/SmallPtrSet.h" 16 #include "llvm/ADT/SmallSet.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/Triple.h" 19 #include "llvm/CodeGen/Analysis.h" 20 #include "llvm/CodeGen/MachineFunction.h" 21 #include "llvm/CodeGen/MachineJumpTableInfo.h" 22 #include "llvm/CodeGen/SelectionDAG.h" 23 #include "llvm/CodeGen/SelectionDAGNodes.h" 24 #include "llvm/IR/CallingConv.h" 25 #include "llvm/IR/Constants.h" 26 #include "llvm/IR/DataLayout.h" 27 #include "llvm/IR/DebugInfo.h" 28 #include "llvm/IR/DerivedTypes.h" 29 #include "llvm/IR/Function.h" 30 #include "llvm/IR/LLVMContext.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/MathExtras.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include "llvm/Target/TargetFrameLowering.h" 36 #include "llvm/Target/TargetLowering.h" 37 #include "llvm/Target/TargetMachine.h" 38 #include "llvm/Target/TargetSubtargetInfo.h" 39 using namespace llvm; 40 41 #define DEBUG_TYPE "legalizedag" 42 43 namespace { 44 45 struct FloatSignAsInt; 46 47 //===----------------------------------------------------------------------===// 48 /// This takes an arbitrary SelectionDAG as input and 49 /// hacks on it until the target machine can handle it. This involves 50 /// eliminating value sizes the machine cannot handle (promoting small sizes to 51 /// large sizes or splitting up large values into small values) as well as 52 /// eliminating operations the machine cannot handle. 53 /// 54 /// This code also does a small amount of optimization and recognition of idioms 55 /// as part of its processing. For example, if a target does not support a 56 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this 57 /// will attempt merge setcc and brc instructions into brcc's. 58 /// 59 class SelectionDAGLegalize { 60 const TargetMachine &TM; 61 const TargetLowering &TLI; 62 SelectionDAG &DAG; 63 64 /// \brief The set of nodes which have already been legalized. We hold a 65 /// reference to it in order to update as necessary on node deletion. 66 SmallPtrSetImpl<SDNode *> &LegalizedNodes; 67 68 /// \brief A set of all the nodes updated during legalization. 69 SmallSetVector<SDNode *, 16> *UpdatedNodes; 70 71 EVT getSetCCResultType(EVT VT) const { 72 return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 73 } 74 75 // Libcall insertion helpers. 76 77 public: 78 SelectionDAGLegalize(SelectionDAG &DAG, 79 SmallPtrSetImpl<SDNode *> &LegalizedNodes, 80 SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr) 81 : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG), 82 LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {} 83 84 /// \brief Legalizes the given operation. 85 void LegalizeOp(SDNode *Node); 86 87 private: 88 SDValue OptimizeFloatStore(StoreSDNode *ST); 89 90 void LegalizeLoadOps(SDNode *Node); 91 void LegalizeStoreOps(SDNode *Node); 92 93 /// Some targets cannot handle a variable 94 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it 95 /// is necessary to spill the vector being inserted into to memory, perform 96 /// the insert there, and then read the result back. 97 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, 98 SDValue Idx, SDLoc dl); 99 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, 100 SDValue Idx, SDLoc dl); 101 102 /// Return a vector shuffle operation which 103 /// performs the same shuffe in terms of order or result bytes, but on a type 104 /// whose vector element type is narrower than the original shuffle type. 105 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> 106 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, SDLoc dl, 107 SDValue N1, SDValue N2, 108 ArrayRef<int> Mask) const; 109 110 bool LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC, 111 bool &NeedInvert, SDLoc dl); 112 113 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned); 114 SDValue ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, const SDValue *Ops, 115 unsigned NumOps, bool isSigned, SDLoc dl); 116 117 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC, 118 SDNode *Node, bool isSigned); 119 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32, 120 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80, 121 RTLIB::Libcall Call_F128, 122 RTLIB::Libcall Call_PPCF128); 123 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned, 124 RTLIB::Libcall Call_I8, 125 RTLIB::Libcall Call_I16, 126 RTLIB::Libcall Call_I32, 127 RTLIB::Libcall Call_I64, 128 RTLIB::Libcall Call_I128); 129 void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results); 130 void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results); 131 132 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, SDLoc dl); 133 SDValue ExpandBUILD_VECTOR(SDNode *Node); 134 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node); 135 void ExpandDYNAMIC_STACKALLOC(SDNode *Node, 136 SmallVectorImpl<SDValue> &Results); 137 void getSignAsIntValue(FloatSignAsInt &State, SDLoc DL, SDValue Value) const; 138 SDValue modifySignAsInt(const FloatSignAsInt &State, SDLoc DL, 139 SDValue NewIntValue) const; 140 SDValue ExpandFCOPYSIGN(SDNode *Node) const; 141 SDValue ExpandFABS(SDNode *Node) const; 142 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT, 143 SDLoc dl); 144 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned, 145 SDLoc dl); 146 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned, 147 SDLoc dl); 148 149 SDValue ExpandBITREVERSE(SDValue Op, SDLoc dl); 150 SDValue ExpandBSWAP(SDValue Op, SDLoc dl); 151 SDValue ExpandBitCount(unsigned Opc, SDValue Op, SDLoc dl); 152 153 SDValue ExpandExtractFromVectorThroughStack(SDValue Op); 154 SDValue ExpandInsertToVectorThroughStack(SDValue Op); 155 SDValue ExpandVectorBuildThroughStack(SDNode* Node); 156 157 SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP); 158 SDValue ExpandConstant(ConstantSDNode *CP); 159 160 // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall 161 bool ExpandNode(SDNode *Node); 162 void ConvertNodeToLibcall(SDNode *Node); 163 void PromoteNode(SDNode *Node); 164 165 public: 166 // Node replacement helpers 167 void ReplacedNode(SDNode *N) { 168 LegalizedNodes.erase(N); 169 if (UpdatedNodes) 170 UpdatedNodes->insert(N); 171 } 172 void ReplaceNode(SDNode *Old, SDNode *New) { 173 DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG); 174 dbgs() << " with: "; New->dump(&DAG)); 175 176 assert(Old->getNumValues() == New->getNumValues() && 177 "Replacing one node with another that produces a different number " 178 "of values!"); 179 DAG.ReplaceAllUsesWith(Old, New); 180 for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) 181 DAG.TransferDbgValues(SDValue(Old, i), SDValue(New, i)); 182 if (UpdatedNodes) 183 UpdatedNodes->insert(New); 184 ReplacedNode(Old); 185 } 186 void ReplaceNode(SDValue Old, SDValue New) { 187 DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG); 188 dbgs() << " with: "; New->dump(&DAG)); 189 190 DAG.ReplaceAllUsesWith(Old, New); 191 DAG.TransferDbgValues(Old, New); 192 if (UpdatedNodes) 193 UpdatedNodes->insert(New.getNode()); 194 ReplacedNode(Old.getNode()); 195 } 196 void ReplaceNode(SDNode *Old, const SDValue *New) { 197 DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG)); 198 199 DAG.ReplaceAllUsesWith(Old, New); 200 for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) { 201 DEBUG(dbgs() << (i == 0 ? " with: " 202 : " and: "); 203 New[i]->dump(&DAG)); 204 DAG.TransferDbgValues(SDValue(Old, i), New[i]); 205 if (UpdatedNodes) 206 UpdatedNodes->insert(New[i].getNode()); 207 } 208 ReplacedNode(Old); 209 } 210 }; 211 } 212 213 /// Return a vector shuffle operation which 214 /// performs the same shuffe in terms of order or result bytes, but on a type 215 /// whose vector element type is narrower than the original shuffle type. 216 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> 217 SDValue 218 SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, SDLoc dl, 219 SDValue N1, SDValue N2, 220 ArrayRef<int> Mask) const { 221 unsigned NumMaskElts = VT.getVectorNumElements(); 222 unsigned NumDestElts = NVT.getVectorNumElements(); 223 unsigned NumEltsGrowth = NumDestElts / NumMaskElts; 224 225 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); 226 227 if (NumEltsGrowth == 1) 228 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]); 229 230 SmallVector<int, 8> NewMask; 231 for (unsigned i = 0; i != NumMaskElts; ++i) { 232 int Idx = Mask[i]; 233 for (unsigned j = 0; j != NumEltsGrowth; ++j) { 234 if (Idx < 0) 235 NewMask.push_back(-1); 236 else 237 NewMask.push_back(Idx * NumEltsGrowth + j); 238 } 239 } 240 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?"); 241 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?"); 242 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]); 243 } 244 245 /// Expands the ConstantFP node to an integer constant or 246 /// a load from the constant pool. 247 SDValue 248 SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) { 249 bool Extend = false; 250 SDLoc dl(CFP); 251 252 // If a FP immediate is precise when represented as a float and if the 253 // target can do an extending load from float to double, we put it into 254 // the constant pool as a float, even if it's is statically typed as a 255 // double. This shrinks FP constants and canonicalizes them for targets where 256 // an FP extending load is the same cost as a normal load (such as on the x87 257 // fp stack or PPC FP unit). 258 EVT VT = CFP->getValueType(0); 259 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue()); 260 if (!UseCP) { 261 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion"); 262 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl, 263 (VT == MVT::f64) ? MVT::i64 : MVT::i32); 264 } 265 266 EVT OrigVT = VT; 267 EVT SVT = VT; 268 while (SVT != MVT::f32 && SVT != MVT::f16) { 269 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1); 270 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) && 271 // Only do this if the target has a native EXTLOAD instruction from 272 // smaller type. 273 TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) && 274 TLI.ShouldShrinkFPConstant(OrigVT)) { 275 Type *SType = SVT.getTypeForEVT(*DAG.getContext()); 276 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType)); 277 VT = SVT; 278 Extend = true; 279 } 280 } 281 282 SDValue CPIdx = 283 DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout())); 284 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 285 if (Extend) { 286 SDValue Result = DAG.getExtLoad( 287 ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx, 288 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT, 289 false, false, false, Alignment); 290 return Result; 291 } 292 SDValue Result = 293 DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx, 294 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 295 false, false, false, Alignment); 296 return Result; 297 } 298 299 /// Expands the Constant node to a load from the constant pool. 300 SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) { 301 SDLoc dl(CP); 302 EVT VT = CP->getValueType(0); 303 SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(), 304 TLI.getPointerTy(DAG.getDataLayout())); 305 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 306 SDValue Result = 307 DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, 308 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 309 false, false, false, Alignment); 310 return Result; 311 } 312 313 /// Expands an unaligned store to 2 half-size stores. 314 static void ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG, 315 const TargetLowering &TLI, 316 SelectionDAGLegalize *DAGLegalize) { 317 assert(ST->getAddressingMode() == ISD::UNINDEXED && 318 "unaligned indexed stores not implemented!"); 319 SDValue Chain = ST->getChain(); 320 SDValue Ptr = ST->getBasePtr(); 321 SDValue Val = ST->getValue(); 322 EVT VT = Val.getValueType(); 323 int Alignment = ST->getAlignment(); 324 325 SDLoc dl(ST); 326 if (ST->getMemoryVT().isFloatingPoint() || 327 ST->getMemoryVT().isVector()) { 328 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); 329 if (TLI.isTypeLegal(intVT)) { 330 // Expand to a bitconvert of the value to the integer type of the 331 // same size, then a (misaligned) int store. 332 // FIXME: Does not handle truncating floating point stores! 333 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val); 334 Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(), 335 ST->isVolatile(), ST->isNonTemporal(), Alignment); 336 DAGLegalize->ReplaceNode(SDValue(ST, 0), Result); 337 return; 338 } 339 // Do a (aligned) store to a stack slot, then copy from the stack slot 340 // to the final destination using (unaligned) integer loads and stores. 341 EVT StoredVT = ST->getMemoryVT(); 342 MVT RegVT = 343 TLI.getRegisterType(*DAG.getContext(), 344 EVT::getIntegerVT(*DAG.getContext(), 345 StoredVT.getSizeInBits())); 346 EVT PtrVT = Ptr.getValueType(); 347 unsigned StoredBytes = StoredVT.getSizeInBits() / 8; 348 unsigned RegBytes = RegVT.getSizeInBits() / 8; 349 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes; 350 351 // Make sure the stack slot is also aligned for the register type. 352 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT); 353 354 // Perform the original store, only redirected to the stack slot. 355 SDValue Store = DAG.getTruncStore(Chain, dl, 356 Val, StackPtr, MachinePointerInfo(), 357 StoredVT, false, false, 0); 358 359 EVT StackPtrVT = StackPtr.getValueType(); 360 361 SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT); 362 SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT); 363 SmallVector<SDValue, 8> Stores; 364 unsigned Offset = 0; 365 366 // Do all but one copies using the full register width. 367 for (unsigned i = 1; i < NumRegs; i++) { 368 // Load one integer register's worth from the stack slot. 369 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, 370 MachinePointerInfo(), 371 false, false, false, 0); 372 // Store it to the final location. Remember the store. 373 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr, 374 ST->getPointerInfo().getWithOffset(Offset), 375 ST->isVolatile(), ST->isNonTemporal(), 376 MinAlign(ST->getAlignment(), Offset))); 377 // Increment the pointers. 378 Offset += RegBytes; 379 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtrVT, 380 StackPtr, StackPtrIncrement); 381 Ptr = DAG.getNode(ISD::ADD, dl, PtrVT, Ptr, PtrIncrement); 382 } 383 384 // The last store may be partial. Do a truncating store. On big-endian 385 // machines this requires an extending load from the stack slot to ensure 386 // that the bits are in the right place. 387 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), 388 8 * (StoredBytes - Offset)); 389 390 // Load from the stack slot. 391 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr, 392 MachinePointerInfo(), 393 MemVT, false, false, false, 0); 394 395 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr, 396 ST->getPointerInfo() 397 .getWithOffset(Offset), 398 MemVT, ST->isVolatile(), 399 ST->isNonTemporal(), 400 MinAlign(ST->getAlignment(), Offset), 401 ST->getAAInfo())); 402 // The order of the stores doesn't matter - say it with a TokenFactor. 403 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 404 DAGLegalize->ReplaceNode(SDValue(ST, 0), Result); 405 return; 406 } 407 assert(ST->getMemoryVT().isInteger() && 408 !ST->getMemoryVT().isVector() && 409 "Unaligned store of unknown type."); 410 // Get the half-size VT 411 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext()); 412 int NumBits = NewStoredVT.getSizeInBits(); 413 int IncrementSize = NumBits / 8; 414 415 // Divide the stored value in two parts. 416 SDValue ShiftAmount = 417 DAG.getConstant(NumBits, dl, TLI.getShiftAmountTy(Val.getValueType(), 418 DAG.getDataLayout())); 419 SDValue Lo = Val; 420 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount); 421 422 // Store the two parts 423 SDValue Store1, Store2; 424 Store1 = DAG.getTruncStore(Chain, dl, 425 DAG.getDataLayout().isLittleEndian() ? Lo : Hi, 426 Ptr, ST->getPointerInfo(), NewStoredVT, 427 ST->isVolatile(), ST->isNonTemporal(), Alignment); 428 429 EVT PtrVT = Ptr.getValueType(); 430 Ptr = DAG.getNode(ISD::ADD, dl, PtrVT, Ptr, 431 DAG.getConstant(IncrementSize, dl, PtrVT)); 432 Alignment = MinAlign(Alignment, IncrementSize); 433 Store2 = DAG.getTruncStore( 434 Chain, dl, DAG.getDataLayout().isLittleEndian() ? Hi : Lo, Ptr, 435 ST->getPointerInfo().getWithOffset(IncrementSize), NewStoredVT, 436 ST->isVolatile(), ST->isNonTemporal(), Alignment, ST->getAAInfo()); 437 438 SDValue Result = 439 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2); 440 DAGLegalize->ReplaceNode(SDValue(ST, 0), Result); 441 } 442 443 /// Expands an unaligned load to 2 half-size loads. 444 static void 445 ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG, 446 const TargetLowering &TLI, 447 SDValue &ValResult, SDValue &ChainResult) { 448 assert(LD->getAddressingMode() == ISD::UNINDEXED && 449 "unaligned indexed loads not implemented!"); 450 SDValue Chain = LD->getChain(); 451 SDValue Ptr = LD->getBasePtr(); 452 EVT VT = LD->getValueType(0); 453 EVT LoadedVT = LD->getMemoryVT(); 454 SDLoc dl(LD); 455 if (VT.isFloatingPoint() || VT.isVector()) { 456 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits()); 457 if (TLI.isTypeLegal(intVT) && TLI.isTypeLegal(LoadedVT)) { 458 // Expand to a (misaligned) integer load of the same size, 459 // then bitconvert to floating point or vector. 460 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, 461 LD->getMemOperand()); 462 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad); 463 if (LoadedVT != VT) 464 Result = DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND : 465 ISD::ANY_EXTEND, dl, VT, Result); 466 467 ValResult = Result; 468 ChainResult = newLoad.getValue(1); 469 return; 470 } 471 472 // Copy the value to a (aligned) stack slot using (unaligned) integer 473 // loads and stores, then do a (aligned) load from the stack slot. 474 MVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT); 475 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8; 476 unsigned RegBytes = RegVT.getSizeInBits() / 8; 477 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes; 478 479 // Make sure the stack slot is also aligned for the register type. 480 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT); 481 482 SmallVector<SDValue, 8> Stores; 483 SDValue StackPtr = StackBase; 484 unsigned Offset = 0; 485 486 EVT PtrVT = Ptr.getValueType(); 487 EVT StackPtrVT = StackPtr.getValueType(); 488 489 SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT); 490 SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT); 491 492 // Do all but one copies using the full register width. 493 for (unsigned i = 1; i < NumRegs; i++) { 494 // Load one integer register's worth from the original location. 495 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, 496 LD->getPointerInfo().getWithOffset(Offset), 497 LD->isVolatile(), LD->isNonTemporal(), 498 LD->isInvariant(), 499 MinAlign(LD->getAlignment(), Offset), 500 LD->getAAInfo()); 501 // Follow the load with a store to the stack slot. Remember the store. 502 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr, 503 MachinePointerInfo(), false, false, 0)); 504 // Increment the pointers. 505 Offset += RegBytes; 506 Ptr = DAG.getNode(ISD::ADD, dl, PtrVT, Ptr, PtrIncrement); 507 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtrVT, StackPtr, 508 StackPtrIncrement); 509 } 510 511 // The last copy may be partial. Do an extending load. 512 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), 513 8 * (LoadedBytes - Offset)); 514 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr, 515 LD->getPointerInfo().getWithOffset(Offset), 516 MemVT, LD->isVolatile(), 517 LD->isNonTemporal(), 518 LD->isInvariant(), 519 MinAlign(LD->getAlignment(), Offset), 520 LD->getAAInfo()); 521 // Follow the load with a store to the stack slot. Remember the store. 522 // On big-endian machines this requires a truncating store to ensure 523 // that the bits end up in the right place. 524 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr, 525 MachinePointerInfo(), MemVT, 526 false, false, 0)); 527 528 // The order of the stores doesn't matter - say it with a TokenFactor. 529 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 530 531 // Finally, perform the original load only redirected to the stack slot. 532 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase, 533 MachinePointerInfo(), LoadedVT, false,false, false, 534 0); 535 536 // Callers expect a MERGE_VALUES node. 537 ValResult = Load; 538 ChainResult = TF; 539 return; 540 } 541 assert(LoadedVT.isInteger() && !LoadedVT.isVector() && 542 "Unaligned load of unsupported type."); 543 544 // Compute the new VT that is half the size of the old one. This is an 545 // integer MVT. 546 unsigned NumBits = LoadedVT.getSizeInBits(); 547 EVT NewLoadedVT; 548 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2); 549 NumBits >>= 1; 550 551 unsigned Alignment = LD->getAlignment(); 552 unsigned IncrementSize = NumBits / 8; 553 ISD::LoadExtType HiExtType = LD->getExtensionType(); 554 555 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD. 556 if (HiExtType == ISD::NON_EXTLOAD) 557 HiExtType = ISD::ZEXTLOAD; 558 559 // Load the value in two parts 560 SDValue Lo, Hi; 561 if (DAG.getDataLayout().isLittleEndian()) { 562 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(), 563 NewLoadedVT, LD->isVolatile(), 564 LD->isNonTemporal(), LD->isInvariant(), Alignment, 565 LD->getAAInfo()); 566 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 567 DAG.getConstant(IncrementSize, dl, Ptr.getValueType())); 568 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, 569 LD->getPointerInfo().getWithOffset(IncrementSize), 570 NewLoadedVT, LD->isVolatile(), 571 LD->isNonTemporal(),LD->isInvariant(), 572 MinAlign(Alignment, IncrementSize), LD->getAAInfo()); 573 } else { 574 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(), 575 NewLoadedVT, LD->isVolatile(), 576 LD->isNonTemporal(), LD->isInvariant(), Alignment, 577 LD->getAAInfo()); 578 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 579 DAG.getConstant(IncrementSize, dl, Ptr.getValueType())); 580 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, 581 LD->getPointerInfo().getWithOffset(IncrementSize), 582 NewLoadedVT, LD->isVolatile(), 583 LD->isNonTemporal(), LD->isInvariant(), 584 MinAlign(Alignment, IncrementSize), LD->getAAInfo()); 585 } 586 587 // aggregate the two parts 588 SDValue ShiftAmount = 589 DAG.getConstant(NumBits, dl, TLI.getShiftAmountTy(Hi.getValueType(), 590 DAG.getDataLayout())); 591 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount); 592 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo); 593 594 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 595 Hi.getValue(1)); 596 597 ValResult = Result; 598 ChainResult = TF; 599 } 600 601 /// Some target cannot handle a variable insertion index for the 602 /// INSERT_VECTOR_ELT instruction. In this case, it 603 /// is necessary to spill the vector being inserted into to memory, perform 604 /// the insert there, and then read the result back. 605 SDValue SelectionDAGLegalize:: 606 PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx, 607 SDLoc dl) { 608 SDValue Tmp1 = Vec; 609 SDValue Tmp2 = Val; 610 SDValue Tmp3 = Idx; 611 612 // If the target doesn't support this, we have to spill the input vector 613 // to a temporary stack slot, update the element, then reload it. This is 614 // badness. We could also load the value into a vector register (either 615 // with a "move to register" or "extload into register" instruction, then 616 // permute it into place, if the idx is a constant and if the idx is 617 // supported by the target. 618 EVT VT = Tmp1.getValueType(); 619 EVT EltVT = VT.getVectorElementType(); 620 EVT IdxVT = Tmp3.getValueType(); 621 EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout()); 622 SDValue StackPtr = DAG.CreateStackTemporary(VT); 623 624 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 625 626 // Store the vector. 627 SDValue Ch = DAG.getStore( 628 DAG.getEntryNode(), dl, Tmp1, StackPtr, 629 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), false, 630 false, 0); 631 632 // Truncate or zero extend offset to target pointer type. 633 Tmp3 = DAG.getZExtOrTrunc(Tmp3, dl, PtrVT); 634 // Add the offset to the index. 635 unsigned EltSize = EltVT.getSizeInBits()/8; 636 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3, 637 DAG.getConstant(EltSize, dl, IdxVT)); 638 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr); 639 // Store the scalar value. 640 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT, 641 false, false, 0); 642 // Load the updated vector. 643 return DAG.getLoad(VT, dl, Ch, StackPtr, MachinePointerInfo::getFixedStack( 644 DAG.getMachineFunction(), SPFI), 645 false, false, false, 0); 646 } 647 648 649 SDValue SelectionDAGLegalize:: 650 ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, SDLoc dl) { 651 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) { 652 // SCALAR_TO_VECTOR requires that the type of the value being inserted 653 // match the element type of the vector being created, except for 654 // integers in which case the inserted value can be over width. 655 EVT EltVT = Vec.getValueType().getVectorElementType(); 656 if (Val.getValueType() == EltVT || 657 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) { 658 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, 659 Vec.getValueType(), Val); 660 661 unsigned NumElts = Vec.getValueType().getVectorNumElements(); 662 // We generate a shuffle of InVec and ScVec, so the shuffle mask 663 // should be 0,1,2,3,4,5... with the appropriate element replaced with 664 // elt 0 of the RHS. 665 SmallVector<int, 8> ShufOps; 666 for (unsigned i = 0; i != NumElts; ++i) 667 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts); 668 669 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, 670 &ShufOps[0]); 671 } 672 } 673 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl); 674 } 675 676 SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) { 677 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' 678 // FIXME: We shouldn't do this for TargetConstantFP's. 679 // FIXME: move this to the DAG Combiner! Note that we can't regress due 680 // to phase ordering between legalized code and the dag combiner. This 681 // probably means that we need to integrate dag combiner and legalizer 682 // together. 683 // We generally can't do this one for long doubles. 684 SDValue Chain = ST->getChain(); 685 SDValue Ptr = ST->getBasePtr(); 686 unsigned Alignment = ST->getAlignment(); 687 bool isVolatile = ST->isVolatile(); 688 bool isNonTemporal = ST->isNonTemporal(); 689 AAMDNodes AAInfo = ST->getAAInfo(); 690 SDLoc dl(ST); 691 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) { 692 if (CFP->getValueType(0) == MVT::f32 && 693 TLI.isTypeLegal(MVT::i32)) { 694 SDValue Con = DAG.getConstant(CFP->getValueAPF(). 695 bitcastToAPInt().zextOrTrunc(32), 696 SDLoc(CFP), MVT::i32); 697 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(), 698 isVolatile, isNonTemporal, Alignment, AAInfo); 699 } 700 701 if (CFP->getValueType(0) == MVT::f64) { 702 // If this target supports 64-bit registers, do a single 64-bit store. 703 if (TLI.isTypeLegal(MVT::i64)) { 704 SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). 705 zextOrTrunc(64), SDLoc(CFP), MVT::i64); 706 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(), 707 isVolatile, isNonTemporal, Alignment, AAInfo); 708 } 709 710 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) { 711 // Otherwise, if the target supports 32-bit registers, use 2 32-bit 712 // stores. If the target supports neither 32- nor 64-bits, this 713 // xform is certainly not worth it. 714 const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt(); 715 SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32); 716 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32); 717 if (DAG.getDataLayout().isBigEndian()) 718 std::swap(Lo, Hi); 719 720 Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(), isVolatile, 721 isNonTemporal, Alignment, AAInfo); 722 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 723 DAG.getConstant(4, dl, Ptr.getValueType())); 724 Hi = DAG.getStore(Chain, dl, Hi, Ptr, 725 ST->getPointerInfo().getWithOffset(4), 726 isVolatile, isNonTemporal, MinAlign(Alignment, 4U), 727 AAInfo); 728 729 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 730 } 731 } 732 } 733 return SDValue(nullptr, 0); 734 } 735 736 void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) { 737 StoreSDNode *ST = cast<StoreSDNode>(Node); 738 SDValue Chain = ST->getChain(); 739 SDValue Ptr = ST->getBasePtr(); 740 SDLoc dl(Node); 741 742 unsigned Alignment = ST->getAlignment(); 743 bool isVolatile = ST->isVolatile(); 744 bool isNonTemporal = ST->isNonTemporal(); 745 AAMDNodes AAInfo = ST->getAAInfo(); 746 747 if (!ST->isTruncatingStore()) { 748 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) { 749 ReplaceNode(ST, OptStore); 750 return; 751 } 752 753 { 754 SDValue Value = ST->getValue(); 755 MVT VT = Value.getSimpleValueType(); 756 switch (TLI.getOperationAction(ISD::STORE, VT)) { 757 default: llvm_unreachable("This action is not supported yet!"); 758 case TargetLowering::Legal: { 759 // If this is an unaligned store and the target doesn't support it, 760 // expand it. 761 EVT MemVT = ST->getMemoryVT(); 762 unsigned AS = ST->getAddressSpace(); 763 unsigned Align = ST->getAlignment(); 764 const DataLayout &DL = DAG.getDataLayout(); 765 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) 766 ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this); 767 break; 768 } 769 case TargetLowering::Custom: { 770 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG); 771 if (Res && Res != SDValue(Node, 0)) 772 ReplaceNode(SDValue(Node, 0), Res); 773 return; 774 } 775 case TargetLowering::Promote: { 776 MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT); 777 assert(NVT.getSizeInBits() == VT.getSizeInBits() && 778 "Can only promote stores to same size type"); 779 Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value); 780 SDValue Result = 781 DAG.getStore(Chain, dl, Value, Ptr, 782 ST->getPointerInfo(), isVolatile, 783 isNonTemporal, Alignment, AAInfo); 784 ReplaceNode(SDValue(Node, 0), Result); 785 break; 786 } 787 } 788 return; 789 } 790 } else { 791 SDValue Value = ST->getValue(); 792 793 EVT StVT = ST->getMemoryVT(); 794 unsigned StWidth = StVT.getSizeInBits(); 795 auto &DL = DAG.getDataLayout(); 796 797 if (StWidth != StVT.getStoreSizeInBits()) { 798 // Promote to a byte-sized store with upper bits zero if not 799 // storing an integral number of bytes. For example, promote 800 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1) 801 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), 802 StVT.getStoreSizeInBits()); 803 Value = DAG.getZeroExtendInReg(Value, dl, StVT); 804 SDValue Result = 805 DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 806 NVT, isVolatile, isNonTemporal, Alignment, AAInfo); 807 ReplaceNode(SDValue(Node, 0), Result); 808 } else if (StWidth & (StWidth - 1)) { 809 // If not storing a power-of-2 number of bits, expand as two stores. 810 assert(!StVT.isVector() && "Unsupported truncstore!"); 811 unsigned RoundWidth = 1 << Log2_32(StWidth); 812 assert(RoundWidth < StWidth); 813 unsigned ExtraWidth = StWidth - RoundWidth; 814 assert(ExtraWidth < RoundWidth); 815 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && 816 "Store size not an integral number of bytes!"); 817 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); 818 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); 819 SDValue Lo, Hi; 820 unsigned IncrementSize; 821 822 if (DL.isLittleEndian()) { 823 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16) 824 // Store the bottom RoundWidth bits. 825 Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 826 RoundVT, 827 isVolatile, isNonTemporal, Alignment, 828 AAInfo); 829 830 // Store the remaining ExtraWidth bits. 831 IncrementSize = RoundWidth / 8; 832 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 833 DAG.getConstant(IncrementSize, dl, 834 Ptr.getValueType())); 835 Hi = DAG.getNode( 836 ISD::SRL, dl, Value.getValueType(), Value, 837 DAG.getConstant(RoundWidth, dl, 838 TLI.getShiftAmountTy(Value.getValueType(), DL))); 839 Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, 840 ST->getPointerInfo().getWithOffset(IncrementSize), 841 ExtraVT, isVolatile, isNonTemporal, 842 MinAlign(Alignment, IncrementSize), AAInfo); 843 } else { 844 // Big endian - avoid unaligned stores. 845 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X 846 // Store the top RoundWidth bits. 847 Hi = DAG.getNode( 848 ISD::SRL, dl, Value.getValueType(), Value, 849 DAG.getConstant(ExtraWidth, dl, 850 TLI.getShiftAmountTy(Value.getValueType(), DL))); 851 Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(), 852 RoundVT, isVolatile, isNonTemporal, Alignment, 853 AAInfo); 854 855 // Store the remaining ExtraWidth bits. 856 IncrementSize = RoundWidth / 8; 857 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 858 DAG.getConstant(IncrementSize, dl, 859 Ptr.getValueType())); 860 Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, 861 ST->getPointerInfo().getWithOffset(IncrementSize), 862 ExtraVT, isVolatile, isNonTemporal, 863 MinAlign(Alignment, IncrementSize), AAInfo); 864 } 865 866 // The order of the stores doesn't matter. 867 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 868 ReplaceNode(SDValue(Node, 0), Result); 869 } else { 870 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) { 871 default: llvm_unreachable("This action is not supported yet!"); 872 case TargetLowering::Legal: { 873 EVT MemVT = ST->getMemoryVT(); 874 unsigned AS = ST->getAddressSpace(); 875 unsigned Align = ST->getAlignment(); 876 // If this is an unaligned store and the target doesn't support it, 877 // expand it. 878 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) 879 ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this); 880 break; 881 } 882 case TargetLowering::Custom: { 883 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG); 884 if (Res && Res != SDValue(Node, 0)) 885 ReplaceNode(SDValue(Node, 0), Res); 886 return; 887 } 888 case TargetLowering::Expand: 889 assert(!StVT.isVector() && 890 "Vector Stores are handled in LegalizeVectorOps"); 891 892 // TRUNCSTORE:i16 i32 -> STORE i16 893 assert(TLI.isTypeLegal(StVT) && 894 "Do not know how to expand this store!"); 895 Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value); 896 SDValue Result = 897 DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 898 isVolatile, isNonTemporal, Alignment, AAInfo); 899 ReplaceNode(SDValue(Node, 0), Result); 900 break; 901 } 902 } 903 } 904 } 905 906 void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) { 907 LoadSDNode *LD = cast<LoadSDNode>(Node); 908 SDValue Chain = LD->getChain(); // The chain. 909 SDValue Ptr = LD->getBasePtr(); // The base pointer. 910 SDValue Value; // The value returned by the load op. 911 SDLoc dl(Node); 912 913 ISD::LoadExtType ExtType = LD->getExtensionType(); 914 if (ExtType == ISD::NON_EXTLOAD) { 915 MVT VT = Node->getSimpleValueType(0); 916 SDValue RVal = SDValue(Node, 0); 917 SDValue RChain = SDValue(Node, 1); 918 919 switch (TLI.getOperationAction(Node->getOpcode(), VT)) { 920 default: llvm_unreachable("This action is not supported yet!"); 921 case TargetLowering::Legal: { 922 EVT MemVT = LD->getMemoryVT(); 923 unsigned AS = LD->getAddressSpace(); 924 unsigned Align = LD->getAlignment(); 925 const DataLayout &DL = DAG.getDataLayout(); 926 // If this is an unaligned load and the target doesn't support it, 927 // expand it. 928 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) 929 ExpandUnalignedLoad(cast<LoadSDNode>(Node), DAG, TLI, RVal, RChain); 930 break; 931 } 932 case TargetLowering::Custom: { 933 if (SDValue Res = TLI.LowerOperation(RVal, DAG)) { 934 RVal = Res; 935 RChain = Res.getValue(1); 936 } 937 break; 938 } 939 case TargetLowering::Promote: { 940 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); 941 assert(NVT.getSizeInBits() == VT.getSizeInBits() && 942 "Can only promote loads to same size type"); 943 944 SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand()); 945 RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res); 946 RChain = Res.getValue(1); 947 break; 948 } 949 } 950 if (RChain.getNode() != Node) { 951 assert(RVal.getNode() != Node && "Load must be completely replaced"); 952 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal); 953 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain); 954 if (UpdatedNodes) { 955 UpdatedNodes->insert(RVal.getNode()); 956 UpdatedNodes->insert(RChain.getNode()); 957 } 958 ReplacedNode(Node); 959 } 960 return; 961 } 962 963 EVT SrcVT = LD->getMemoryVT(); 964 unsigned SrcWidth = SrcVT.getSizeInBits(); 965 unsigned Alignment = LD->getAlignment(); 966 bool isVolatile = LD->isVolatile(); 967 bool isNonTemporal = LD->isNonTemporal(); 968 bool isInvariant = LD->isInvariant(); 969 AAMDNodes AAInfo = LD->getAAInfo(); 970 971 if (SrcWidth != SrcVT.getStoreSizeInBits() && 972 // Some targets pretend to have an i1 loading operation, and actually 973 // load an i8. This trick is correct for ZEXTLOAD because the top 7 974 // bits are guaranteed to be zero; it helps the optimizers understand 975 // that these bits are zero. It is also useful for EXTLOAD, since it 976 // tells the optimizers that those bits are undefined. It would be 977 // nice to have an effective generic way of getting these benefits... 978 // Until such a way is found, don't insist on promoting i1 here. 979 (SrcVT != MVT::i1 || 980 TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) == 981 TargetLowering::Promote)) { 982 // Promote to a byte-sized load if not loading an integral number of 983 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24. 984 unsigned NewWidth = SrcVT.getStoreSizeInBits(); 985 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth); 986 SDValue Ch; 987 988 // The extra bits are guaranteed to be zero, since we stored them that 989 // way. A zext load from NVT thus automatically gives zext from SrcVT. 990 991 ISD::LoadExtType NewExtType = 992 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD; 993 994 SDValue Result = 995 DAG.getExtLoad(NewExtType, dl, Node->getValueType(0), 996 Chain, Ptr, LD->getPointerInfo(), 997 NVT, isVolatile, isNonTemporal, isInvariant, Alignment, 998 AAInfo); 999 1000 Ch = Result.getValue(1); // The chain. 1001 1002 if (ExtType == ISD::SEXTLOAD) 1003 // Having the top bits zero doesn't help when sign extending. 1004 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, 1005 Result.getValueType(), 1006 Result, DAG.getValueType(SrcVT)); 1007 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType()) 1008 // All the top bits are guaranteed to be zero - inform the optimizers. 1009 Result = DAG.getNode(ISD::AssertZext, dl, 1010 Result.getValueType(), Result, 1011 DAG.getValueType(SrcVT)); 1012 1013 Value = Result; 1014 Chain = Ch; 1015 } else if (SrcWidth & (SrcWidth - 1)) { 1016 // If not loading a power-of-2 number of bits, expand as two loads. 1017 assert(!SrcVT.isVector() && "Unsupported extload!"); 1018 unsigned RoundWidth = 1 << Log2_32(SrcWidth); 1019 assert(RoundWidth < SrcWidth); 1020 unsigned ExtraWidth = SrcWidth - RoundWidth; 1021 assert(ExtraWidth < RoundWidth); 1022 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && 1023 "Load size not an integral number of bytes!"); 1024 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); 1025 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); 1026 SDValue Lo, Hi, Ch; 1027 unsigned IncrementSize; 1028 auto &DL = DAG.getDataLayout(); 1029 1030 if (DL.isLittleEndian()) { 1031 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16) 1032 // Load the bottom RoundWidth bits. 1033 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), 1034 Chain, Ptr, 1035 LD->getPointerInfo(), RoundVT, isVolatile, 1036 isNonTemporal, isInvariant, Alignment, AAInfo); 1037 1038 // Load the remaining ExtraWidth bits. 1039 IncrementSize = RoundWidth / 8; 1040 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 1041 DAG.getConstant(IncrementSize, dl, 1042 Ptr.getValueType())); 1043 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr, 1044 LD->getPointerInfo().getWithOffset(IncrementSize), 1045 ExtraVT, isVolatile, isNonTemporal, isInvariant, 1046 MinAlign(Alignment, IncrementSize), AAInfo); 1047 1048 // Build a factor node to remember that this load is independent of 1049 // the other one. 1050 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 1051 Hi.getValue(1)); 1052 1053 // Move the top bits to the right place. 1054 Hi = DAG.getNode( 1055 ISD::SHL, dl, Hi.getValueType(), Hi, 1056 DAG.getConstant(RoundWidth, dl, 1057 TLI.getShiftAmountTy(Hi.getValueType(), DL))); 1058 1059 // Join the hi and lo parts. 1060 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); 1061 } else { 1062 // Big endian - avoid unaligned loads. 1063 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8 1064 // Load the top RoundWidth bits. 1065 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr, 1066 LD->getPointerInfo(), RoundVT, isVolatile, 1067 isNonTemporal, isInvariant, Alignment, AAInfo); 1068 1069 // Load the remaining ExtraWidth bits. 1070 IncrementSize = RoundWidth / 8; 1071 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 1072 DAG.getConstant(IncrementSize, dl, 1073 Ptr.getValueType())); 1074 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, 1075 dl, Node->getValueType(0), Chain, Ptr, 1076 LD->getPointerInfo().getWithOffset(IncrementSize), 1077 ExtraVT, isVolatile, isNonTemporal, isInvariant, 1078 MinAlign(Alignment, IncrementSize), AAInfo); 1079 1080 // Build a factor node to remember that this load is independent of 1081 // the other one. 1082 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 1083 Hi.getValue(1)); 1084 1085 // Move the top bits to the right place. 1086 Hi = DAG.getNode( 1087 ISD::SHL, dl, Hi.getValueType(), Hi, 1088 DAG.getConstant(ExtraWidth, dl, 1089 TLI.getShiftAmountTy(Hi.getValueType(), DL))); 1090 1091 // Join the hi and lo parts. 1092 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); 1093 } 1094 1095 Chain = Ch; 1096 } else { 1097 bool isCustom = false; 1098 switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0), 1099 SrcVT.getSimpleVT())) { 1100 default: llvm_unreachable("This action is not supported yet!"); 1101 case TargetLowering::Custom: 1102 isCustom = true; 1103 // FALLTHROUGH 1104 case TargetLowering::Legal: { 1105 Value = SDValue(Node, 0); 1106 Chain = SDValue(Node, 1); 1107 1108 if (isCustom) { 1109 if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) { 1110 Value = Res; 1111 Chain = Res.getValue(1); 1112 } 1113 } else { 1114 // If this is an unaligned load and the target doesn't support it, 1115 // expand it. 1116 EVT MemVT = LD->getMemoryVT(); 1117 unsigned AS = LD->getAddressSpace(); 1118 unsigned Align = LD->getAlignment(); 1119 const DataLayout &DL = DAG.getDataLayout(); 1120 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) 1121 ExpandUnalignedLoad(cast<LoadSDNode>(Node), DAG, TLI, Value, Chain); 1122 } 1123 break; 1124 } 1125 case TargetLowering::Expand: 1126 EVT DestVT = Node->getValueType(0); 1127 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) { 1128 // If the source type is not legal, see if there is a legal extload to 1129 // an intermediate type that we can then extend further. 1130 EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT()); 1131 if (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT? 1132 TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT)) { 1133 // If we are loading a legal type, this is a non-extload followed by a 1134 // full extend. 1135 ISD::LoadExtType MidExtType = 1136 (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType; 1137 1138 SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr, 1139 SrcVT, LD->getMemOperand()); 1140 unsigned ExtendOp = 1141 ISD::getExtForLoadExtType(SrcVT.isFloatingPoint(), ExtType); 1142 Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load); 1143 Chain = Load.getValue(1); 1144 break; 1145 } 1146 1147 // Handle the special case of fp16 extloads. EXTLOAD doesn't have the 1148 // normal undefined upper bits behavior to allow using an in-reg extend 1149 // with the illegal FP type, so load as an integer and do the 1150 // from-integer conversion. 1151 if (SrcVT.getScalarType() == MVT::f16) { 1152 EVT ISrcVT = SrcVT.changeTypeToInteger(); 1153 EVT IDestVT = DestVT.changeTypeToInteger(); 1154 EVT LoadVT = TLI.getRegisterType(IDestVT.getSimpleVT()); 1155 1156 SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, LoadVT, 1157 Chain, Ptr, ISrcVT, 1158 LD->getMemOperand()); 1159 Value = DAG.getNode(ISD::FP16_TO_FP, dl, DestVT, Result); 1160 Chain = Result.getValue(1); 1161 break; 1162 } 1163 } 1164 1165 assert(!SrcVT.isVector() && 1166 "Vector Loads are handled in LegalizeVectorOps"); 1167 1168 // FIXME: This does not work for vectors on most targets. Sign- 1169 // and zero-extend operations are currently folded into extending 1170 // loads, whether they are legal or not, and then we end up here 1171 // without any support for legalizing them. 1172 assert(ExtType != ISD::EXTLOAD && 1173 "EXTLOAD should always be supported!"); 1174 // Turn the unsupported load into an EXTLOAD followed by an 1175 // explicit zero/sign extend inreg. 1176 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl, 1177 Node->getValueType(0), 1178 Chain, Ptr, SrcVT, 1179 LD->getMemOperand()); 1180 SDValue ValRes; 1181 if (ExtType == ISD::SEXTLOAD) 1182 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, 1183 Result.getValueType(), 1184 Result, DAG.getValueType(SrcVT)); 1185 else 1186 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType()); 1187 Value = ValRes; 1188 Chain = Result.getValue(1); 1189 break; 1190 } 1191 } 1192 1193 // Since loads produce two values, make sure to remember that we legalized 1194 // both of them. 1195 if (Chain.getNode() != Node) { 1196 assert(Value.getNode() != Node && "Load must be completely replaced"); 1197 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value); 1198 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain); 1199 if (UpdatedNodes) { 1200 UpdatedNodes->insert(Value.getNode()); 1201 UpdatedNodes->insert(Chain.getNode()); 1202 } 1203 ReplacedNode(Node); 1204 } 1205 } 1206 1207 /// Return a legal replacement for the given operation, with all legal operands. 1208 void SelectionDAGLegalize::LegalizeOp(SDNode *Node) { 1209 DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG)); 1210 1211 if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes. 1212 return; 1213 1214 #ifndef NDEBUG 1215 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 1216 assert((TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) == 1217 TargetLowering::TypeLegal || 1218 TLI.isTypeLegal(Node->getValueType(i))) && 1219 "Unexpected illegal type!"); 1220 1221 for (const SDValue &Op : Node->op_values()) 1222 assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) == 1223 TargetLowering::TypeLegal || 1224 TLI.isTypeLegal(Op.getValueType()) || 1225 Op.getOpcode() == ISD::TargetConstant) && 1226 "Unexpected illegal type!"); 1227 #endif 1228 1229 // Figure out the correct action; the way to query this varies by opcode 1230 TargetLowering::LegalizeAction Action = TargetLowering::Legal; 1231 bool SimpleFinishLegalizing = true; 1232 switch (Node->getOpcode()) { 1233 case ISD::INTRINSIC_W_CHAIN: 1234 case ISD::INTRINSIC_WO_CHAIN: 1235 case ISD::INTRINSIC_VOID: 1236 case ISD::STACKSAVE: 1237 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); 1238 break; 1239 case ISD::GET_DYNAMIC_AREA_OFFSET: 1240 Action = TLI.getOperationAction(Node->getOpcode(), 1241 Node->getValueType(0)); 1242 break; 1243 case ISD::VAARG: 1244 Action = TLI.getOperationAction(Node->getOpcode(), 1245 Node->getValueType(0)); 1246 if (Action != TargetLowering::Promote) 1247 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); 1248 break; 1249 case ISD::FP_TO_FP16: 1250 case ISD::SINT_TO_FP: 1251 case ISD::UINT_TO_FP: 1252 case ISD::EXTRACT_VECTOR_ELT: 1253 Action = TLI.getOperationAction(Node->getOpcode(), 1254 Node->getOperand(0).getValueType()); 1255 break; 1256 case ISD::FP_ROUND_INREG: 1257 case ISD::SIGN_EXTEND_INREG: { 1258 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT(); 1259 Action = TLI.getOperationAction(Node->getOpcode(), InnerType); 1260 break; 1261 } 1262 case ISD::ATOMIC_STORE: { 1263 Action = TLI.getOperationAction(Node->getOpcode(), 1264 Node->getOperand(2).getValueType()); 1265 break; 1266 } 1267 case ISD::SELECT_CC: 1268 case ISD::SETCC: 1269 case ISD::BR_CC: { 1270 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 : 1271 Node->getOpcode() == ISD::SETCC ? 2 : 1272 Node->getOpcode() == ISD::SETCCE ? 3 : 1; 1273 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0; 1274 MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType(); 1275 ISD::CondCode CCCode = 1276 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get(); 1277 Action = TLI.getCondCodeAction(CCCode, OpVT); 1278 if (Action == TargetLowering::Legal) { 1279 if (Node->getOpcode() == ISD::SELECT_CC) 1280 Action = TLI.getOperationAction(Node->getOpcode(), 1281 Node->getValueType(0)); 1282 else 1283 Action = TLI.getOperationAction(Node->getOpcode(), OpVT); 1284 } 1285 break; 1286 } 1287 case ISD::LOAD: 1288 case ISD::STORE: 1289 // FIXME: Model these properly. LOAD and STORE are complicated, and 1290 // STORE expects the unlegalized operand in some cases. 1291 SimpleFinishLegalizing = false; 1292 break; 1293 case ISD::CALLSEQ_START: 1294 case ISD::CALLSEQ_END: 1295 // FIXME: This shouldn't be necessary. These nodes have special properties 1296 // dealing with the recursive nature of legalization. Removing this 1297 // special case should be done as part of making LegalizeDAG non-recursive. 1298 SimpleFinishLegalizing = false; 1299 break; 1300 case ISD::EXTRACT_ELEMENT: 1301 case ISD::FLT_ROUNDS_: 1302 case ISD::FPOWI: 1303 case ISD::MERGE_VALUES: 1304 case ISD::EH_RETURN: 1305 case ISD::FRAME_TO_ARGS_OFFSET: 1306 case ISD::EH_SJLJ_SETJMP: 1307 case ISD::EH_SJLJ_LONGJMP: 1308 case ISD::EH_SJLJ_SETUP_DISPATCH: 1309 // These operations lie about being legal: when they claim to be legal, 1310 // they should actually be expanded. 1311 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 1312 if (Action == TargetLowering::Legal) 1313 Action = TargetLowering::Expand; 1314 break; 1315 case ISD::INIT_TRAMPOLINE: 1316 case ISD::ADJUST_TRAMPOLINE: 1317 case ISD::FRAMEADDR: 1318 case ISD::RETURNADDR: 1319 // These operations lie about being legal: when they claim to be legal, 1320 // they should actually be custom-lowered. 1321 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 1322 if (Action == TargetLowering::Legal) 1323 Action = TargetLowering::Custom; 1324 break; 1325 case ISD::READCYCLECOUNTER: 1326 // READCYCLECOUNTER returns an i64, even if type legalization might have 1327 // expanded that to several smaller types. 1328 Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64); 1329 break; 1330 case ISD::READ_REGISTER: 1331 case ISD::WRITE_REGISTER: 1332 // Named register is legal in the DAG, but blocked by register name 1333 // selection if not implemented by target (to chose the correct register) 1334 // They'll be converted to Copy(To/From)Reg. 1335 Action = TargetLowering::Legal; 1336 break; 1337 case ISD::DEBUGTRAP: 1338 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 1339 if (Action == TargetLowering::Expand) { 1340 // replace ISD::DEBUGTRAP with ISD::TRAP 1341 SDValue NewVal; 1342 NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(), 1343 Node->getOperand(0)); 1344 ReplaceNode(Node, NewVal.getNode()); 1345 LegalizeOp(NewVal.getNode()); 1346 return; 1347 } 1348 break; 1349 1350 default: 1351 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { 1352 Action = TargetLowering::Legal; 1353 } else { 1354 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 1355 } 1356 break; 1357 } 1358 1359 if (SimpleFinishLegalizing) { 1360 SDNode *NewNode = Node; 1361 switch (Node->getOpcode()) { 1362 default: break; 1363 case ISD::SHL: 1364 case ISD::SRL: 1365 case ISD::SRA: 1366 case ISD::ROTL: 1367 case ISD::ROTR: 1368 // Legalizing shifts/rotates requires adjusting the shift amount 1369 // to the appropriate width. 1370 if (!Node->getOperand(1).getValueType().isVector()) { 1371 SDValue SAO = 1372 DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(), 1373 Node->getOperand(1)); 1374 HandleSDNode Handle(SAO); 1375 LegalizeOp(SAO.getNode()); 1376 NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0), 1377 Handle.getValue()); 1378 } 1379 break; 1380 case ISD::SRL_PARTS: 1381 case ISD::SRA_PARTS: 1382 case ISD::SHL_PARTS: 1383 // Legalizing shifts/rotates requires adjusting the shift amount 1384 // to the appropriate width. 1385 if (!Node->getOperand(2).getValueType().isVector()) { 1386 SDValue SAO = 1387 DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(), 1388 Node->getOperand(2)); 1389 HandleSDNode Handle(SAO); 1390 LegalizeOp(SAO.getNode()); 1391 NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0), 1392 Node->getOperand(1), 1393 Handle.getValue()); 1394 } 1395 break; 1396 } 1397 1398 if (NewNode != Node) { 1399 ReplaceNode(Node, NewNode); 1400 Node = NewNode; 1401 } 1402 switch (Action) { 1403 case TargetLowering::Legal: 1404 return; 1405 case TargetLowering::Custom: { 1406 // FIXME: The handling for custom lowering with multiple results is 1407 // a complete mess. 1408 if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) { 1409 if (!(Res.getNode() != Node || Res.getResNo() != 0)) 1410 return; 1411 1412 if (Node->getNumValues() == 1) { 1413 // We can just directly replace this node with the lowered value. 1414 ReplaceNode(SDValue(Node, 0), Res); 1415 return; 1416 } 1417 1418 SmallVector<SDValue, 8> ResultVals; 1419 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 1420 ResultVals.push_back(Res.getValue(i)); 1421 ReplaceNode(Node, ResultVals.data()); 1422 return; 1423 } 1424 } 1425 // FALL THROUGH 1426 case TargetLowering::Expand: 1427 if (ExpandNode(Node)) 1428 return; 1429 // FALL THROUGH 1430 case TargetLowering::LibCall: 1431 ConvertNodeToLibcall(Node); 1432 return; 1433 case TargetLowering::Promote: 1434 PromoteNode(Node); 1435 return; 1436 } 1437 } 1438 1439 switch (Node->getOpcode()) { 1440 default: 1441 #ifndef NDEBUG 1442 dbgs() << "NODE: "; 1443 Node->dump( &DAG); 1444 dbgs() << "\n"; 1445 #endif 1446 llvm_unreachable("Do not know how to legalize this operator!"); 1447 1448 case ISD::CALLSEQ_START: 1449 case ISD::CALLSEQ_END: 1450 break; 1451 case ISD::LOAD: { 1452 return LegalizeLoadOps(Node); 1453 } 1454 case ISD::STORE: { 1455 return LegalizeStoreOps(Node); 1456 } 1457 } 1458 } 1459 1460 SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) { 1461 SDValue Vec = Op.getOperand(0); 1462 SDValue Idx = Op.getOperand(1); 1463 SDLoc dl(Op); 1464 1465 // Before we generate a new store to a temporary stack slot, see if there is 1466 // already one that we can use. There often is because when we scalarize 1467 // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole 1468 // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in 1469 // the vector. If all are expanded here, we don't want one store per vector 1470 // element. 1471 1472 // Caches for hasPredecessorHelper 1473 SmallPtrSet<const SDNode *, 32> Visited; 1474 SmallVector<const SDNode *, 16> Worklist; 1475 Worklist.push_back(Idx.getNode()); 1476 SDValue StackPtr, Ch; 1477 for (SDNode::use_iterator UI = Vec.getNode()->use_begin(), 1478 UE = Vec.getNode()->use_end(); UI != UE; ++UI) { 1479 SDNode *User = *UI; 1480 if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) { 1481 if (ST->isIndexed() || ST->isTruncatingStore() || 1482 ST->getValue() != Vec) 1483 continue; 1484 1485 // Make sure that nothing else could have stored into the destination of 1486 // this store. 1487 if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode())) 1488 continue; 1489 1490 // If the index is dependent on the store we will introduce a cycle when 1491 // creating the load (the load uses the index, and by replacing the chain 1492 // we will make the index dependent on the load). 1493 if (SDNode::hasPredecessorHelper(ST, Visited, Worklist)) 1494 continue; 1495 1496 StackPtr = ST->getBasePtr(); 1497 Ch = SDValue(ST, 0); 1498 break; 1499 } 1500 } 1501 1502 if (!Ch.getNode()) { 1503 // Store the value to a temporary stack slot, then LOAD the returned part. 1504 StackPtr = DAG.CreateStackTemporary(Vec.getValueType()); 1505 Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, 1506 MachinePointerInfo(), false, false, 0); 1507 } 1508 1509 // Add the offset to the index. 1510 unsigned EltSize = 1511 Vec.getValueType().getVectorElementType().getSizeInBits()/8; 1512 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx, 1513 DAG.getConstant(EltSize, SDLoc(Vec), Idx.getValueType())); 1514 1515 Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy(DAG.getDataLayout())); 1516 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr); 1517 1518 SDValue NewLoad; 1519 1520 if (Op.getValueType().isVector()) 1521 NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, 1522 MachinePointerInfo(), false, false, false, 0); 1523 else 1524 NewLoad = DAG.getExtLoad( 1525 ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr, MachinePointerInfo(), 1526 Vec.getValueType().getVectorElementType(), false, false, false, 0); 1527 1528 // Replace the chain going out of the store, by the one out of the load. 1529 DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1)); 1530 1531 // We introduced a cycle though, so update the loads operands, making sure 1532 // to use the original store's chain as an incoming chain. 1533 SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(), 1534 NewLoad->op_end()); 1535 NewLoadOperands[0] = Ch; 1536 NewLoad = 1537 SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0); 1538 return NewLoad; 1539 } 1540 1541 SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) { 1542 assert(Op.getValueType().isVector() && "Non-vector insert subvector!"); 1543 1544 SDValue Vec = Op.getOperand(0); 1545 SDValue Part = Op.getOperand(1); 1546 SDValue Idx = Op.getOperand(2); 1547 SDLoc dl(Op); 1548 1549 // Store the value to a temporary stack slot, then LOAD the returned part. 1550 1551 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType()); 1552 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 1553 MachinePointerInfo PtrInfo = 1554 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 1555 1556 // First store the whole vector. 1557 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo, 1558 false, false, 0); 1559 1560 // Then store the inserted part. 1561 1562 // Add the offset to the index. 1563 unsigned EltSize = 1564 Vec.getValueType().getVectorElementType().getSizeInBits()/8; 1565 1566 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx, 1567 DAG.getConstant(EltSize, SDLoc(Vec), Idx.getValueType())); 1568 Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy(DAG.getDataLayout())); 1569 1570 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, 1571 StackPtr); 1572 1573 // Store the subvector. 1574 Ch = DAG.getStore(Ch, dl, Part, SubStackPtr, 1575 MachinePointerInfo(), false, false, 0); 1576 1577 // Finally, load the updated vector. 1578 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo, 1579 false, false, false, 0); 1580 } 1581 1582 SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) { 1583 // We can't handle this case efficiently. Allocate a sufficiently 1584 // aligned object on the stack, store each element into it, then load 1585 // the result as a vector. 1586 // Create the stack frame object. 1587 EVT VT = Node->getValueType(0); 1588 EVT EltVT = VT.getVectorElementType(); 1589 SDLoc dl(Node); 1590 SDValue FIPtr = DAG.CreateStackTemporary(VT); 1591 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex(); 1592 MachinePointerInfo PtrInfo = 1593 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 1594 1595 // Emit a store of each element to the stack slot. 1596 SmallVector<SDValue, 8> Stores; 1597 unsigned TypeByteSize = EltVT.getSizeInBits() / 8; 1598 // Store (in the right endianness) the elements to memory. 1599 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 1600 // Ignore undef elements. 1601 if (Node->getOperand(i).isUndef()) continue; 1602 1603 unsigned Offset = TypeByteSize*i; 1604 1605 SDValue Idx = DAG.getConstant(Offset, dl, FIPtr.getValueType()); 1606 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx); 1607 1608 // If the destination vector element type is narrower than the source 1609 // element type, only store the bits necessary. 1610 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) { 1611 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl, 1612 Node->getOperand(i), Idx, 1613 PtrInfo.getWithOffset(Offset), 1614 EltVT, false, false, 0)); 1615 } else 1616 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, 1617 Node->getOperand(i), Idx, 1618 PtrInfo.getWithOffset(Offset), 1619 false, false, 0)); 1620 } 1621 1622 SDValue StoreChain; 1623 if (!Stores.empty()) // Not all undef elements? 1624 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 1625 else 1626 StoreChain = DAG.getEntryNode(); 1627 1628 // Result is a load from the stack slot. 1629 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, 1630 false, false, false, 0); 1631 } 1632 1633 namespace { 1634 /// Keeps track of state when getting the sign of a floating-point value as an 1635 /// integer. 1636 struct FloatSignAsInt { 1637 EVT FloatVT; 1638 SDValue Chain; 1639 SDValue FloatPtr; 1640 SDValue IntPtr; 1641 MachinePointerInfo IntPointerInfo; 1642 MachinePointerInfo FloatPointerInfo; 1643 SDValue IntValue; 1644 APInt SignMask; 1645 uint8_t SignBit; 1646 }; 1647 } 1648 1649 /// Bitcast a floating-point value to an integer value. Only bitcast the part 1650 /// containing the sign bit if the target has no integer value capable of 1651 /// holding all bits of the floating-point value. 1652 void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State, 1653 SDLoc DL, SDValue Value) const { 1654 EVT FloatVT = Value.getValueType(); 1655 unsigned NumBits = FloatVT.getSizeInBits(); 1656 State.FloatVT = FloatVT; 1657 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits); 1658 // Convert to an integer of the same size. 1659 if (TLI.isTypeLegal(IVT)) { 1660 State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value); 1661 State.SignMask = APInt::getSignBit(NumBits); 1662 State.SignBit = NumBits - 1; 1663 return; 1664 } 1665 1666 auto &DataLayout = DAG.getDataLayout(); 1667 // Store the float to memory, then load the sign part out as an integer. 1668 MVT LoadTy = TLI.getRegisterType(*DAG.getContext(), MVT::i8); 1669 // First create a temporary that is aligned for both the load and store. 1670 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy); 1671 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 1672 // Then store the float to it. 1673 State.FloatPtr = StackPtr; 1674 MachineFunction &MF = DAG.getMachineFunction(); 1675 State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI); 1676 State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr, 1677 State.FloatPointerInfo, false, false, 0); 1678 1679 SDValue IntPtr; 1680 if (DataLayout.isBigEndian()) { 1681 assert(FloatVT.isByteSized() && "Unsupported floating point type!"); 1682 // Load out a legal integer with the same sign bit as the float. 1683 IntPtr = StackPtr; 1684 State.IntPointerInfo = State.FloatPointerInfo; 1685 } else { 1686 // Advance the pointer so that the loaded byte will contain the sign bit. 1687 unsigned ByteOffset = (FloatVT.getSizeInBits() / 8) - 1; 1688 IntPtr = DAG.getNode(ISD::ADD, DL, StackPtr.getValueType(), StackPtr, 1689 DAG.getConstant(ByteOffset, DL, StackPtr.getValueType())); 1690 State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI, 1691 ByteOffset); 1692 } 1693 1694 State.IntPtr = IntPtr; 1695 State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, 1696 IntPtr, State.IntPointerInfo, MVT::i8, 1697 false, false, false, 0); 1698 State.SignMask = APInt::getOneBitSet(LoadTy.getSizeInBits(), 7); 1699 State.SignBit = 7; 1700 } 1701 1702 /// Replace the integer value produced by getSignAsIntValue() with a new value 1703 /// and cast the result back to a floating-point type. 1704 SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State, 1705 SDLoc DL, SDValue NewIntValue) const { 1706 if (!State.Chain) 1707 return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue); 1708 1709 // Override the part containing the sign bit in the value stored on the stack. 1710 SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr, 1711 State.IntPointerInfo, MVT::i8, false, false, 1712 0); 1713 return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr, 1714 State.FloatPointerInfo, false, false, false, 0); 1715 } 1716 1717 SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const { 1718 SDLoc DL(Node); 1719 SDValue Mag = Node->getOperand(0); 1720 SDValue Sign = Node->getOperand(1); 1721 1722 // Get sign bit into an integer value. 1723 FloatSignAsInt SignAsInt; 1724 getSignAsIntValue(SignAsInt, DL, Sign); 1725 1726 EVT IntVT = SignAsInt.IntValue.getValueType(); 1727 SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT); 1728 SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue, 1729 SignMask); 1730 1731 // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X) 1732 EVT FloatVT = Mag.getValueType(); 1733 if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) && 1734 TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) { 1735 SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag); 1736 SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue); 1737 SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit, 1738 DAG.getConstant(0, DL, IntVT), ISD::SETNE); 1739 return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue); 1740 } 1741 1742 // Transform Mag value to integer, and clear the sign bit. 1743 FloatSignAsInt MagAsInt; 1744 getSignAsIntValue(MagAsInt, DL, Mag); 1745 EVT MagVT = MagAsInt.IntValue.getValueType(); 1746 SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT); 1747 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue, 1748 ClearSignMask); 1749 1750 // Get the signbit at the right position for MagAsInt. 1751 int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit; 1752 if (SignBit.getValueSizeInBits() > ClearedSign.getValueSizeInBits()) { 1753 if (ShiftAmount > 0) { 1754 SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, IntVT); 1755 SignBit = DAG.getNode(ISD::SRL, DL, IntVT, SignBit, ShiftCnst); 1756 } else if (ShiftAmount < 0) { 1757 SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, IntVT); 1758 SignBit = DAG.getNode(ISD::SHL, DL, IntVT, SignBit, ShiftCnst); 1759 } 1760 SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit); 1761 } else if (SignBit.getValueSizeInBits() < ClearedSign.getValueSizeInBits()) { 1762 SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit); 1763 if (ShiftAmount > 0) { 1764 SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, MagVT); 1765 SignBit = DAG.getNode(ISD::SRL, DL, MagVT, SignBit, ShiftCnst); 1766 } else if (ShiftAmount < 0) { 1767 SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, MagVT); 1768 SignBit = DAG.getNode(ISD::SHL, DL, MagVT, SignBit, ShiftCnst); 1769 } 1770 } 1771 1772 // Store the part with the modified sign and convert back to float. 1773 SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit); 1774 return modifySignAsInt(MagAsInt, DL, CopiedSign); 1775 } 1776 1777 SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const { 1778 SDLoc DL(Node); 1779 SDValue Value = Node->getOperand(0); 1780 1781 // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal. 1782 EVT FloatVT = Value.getValueType(); 1783 if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) { 1784 SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT); 1785 return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero); 1786 } 1787 1788 // Transform value to integer, clear the sign bit and transform back. 1789 FloatSignAsInt ValueAsInt; 1790 getSignAsIntValue(ValueAsInt, DL, Value); 1791 EVT IntVT = ValueAsInt.IntValue.getValueType(); 1792 SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT); 1793 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue, 1794 ClearSignMask); 1795 return modifySignAsInt(ValueAsInt, DL, ClearedSign); 1796 } 1797 1798 void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node, 1799 SmallVectorImpl<SDValue> &Results) { 1800 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); 1801 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" 1802 " not tell us which reg is the stack pointer!"); 1803 SDLoc dl(Node); 1804 EVT VT = Node->getValueType(0); 1805 SDValue Tmp1 = SDValue(Node, 0); 1806 SDValue Tmp2 = SDValue(Node, 1); 1807 SDValue Tmp3 = Node->getOperand(2); 1808 SDValue Chain = Tmp1.getOperand(0); 1809 1810 // Chain the dynamic stack allocation so that it doesn't modify the stack 1811 // pointer when other instructions are using the stack. 1812 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, dl, true), dl); 1813 1814 SDValue Size = Tmp2.getOperand(1); 1815 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT); 1816 Chain = SP.getValue(1); 1817 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue(); 1818 unsigned StackAlign = 1819 DAG.getSubtarget().getFrameLowering()->getStackAlignment(); 1820 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value 1821 if (Align > StackAlign) 1822 Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1, 1823 DAG.getConstant(-(uint64_t)Align, dl, VT)); 1824 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain 1825 1826 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, dl, true), 1827 DAG.getIntPtrConstant(0, dl, true), SDValue(), dl); 1828 1829 Results.push_back(Tmp1); 1830 Results.push_back(Tmp2); 1831 } 1832 1833 /// Legalize a SETCC with given LHS and RHS and condition code CC on the current 1834 /// target. 1835 /// 1836 /// If the SETCC has been legalized using AND / OR, then the legalized node 1837 /// will be stored in LHS. RHS and CC will be set to SDValue(). NeedInvert 1838 /// will be set to false. 1839 /// 1840 /// If the SETCC has been legalized by using getSetCCSwappedOperands(), 1841 /// then the values of LHS and RHS will be swapped, CC will be set to the 1842 /// new condition, and NeedInvert will be set to false. 1843 /// 1844 /// If the SETCC has been legalized using the inverse condcode, then LHS and 1845 /// RHS will be unchanged, CC will set to the inverted condcode, and NeedInvert 1846 /// will be set to true. The caller must invert the result of the SETCC with 1847 /// SelectionDAG::getLogicalNOT() or take equivalent action to swap the effect 1848 /// of a true/false result. 1849 /// 1850 /// \returns true if the SetCC has been legalized, false if it hasn't. 1851 bool SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT, 1852 SDValue &LHS, SDValue &RHS, 1853 SDValue &CC, 1854 bool &NeedInvert, 1855 SDLoc dl) { 1856 MVT OpVT = LHS.getSimpleValueType(); 1857 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get(); 1858 NeedInvert = false; 1859 switch (TLI.getCondCodeAction(CCCode, OpVT)) { 1860 default: llvm_unreachable("Unknown condition code action!"); 1861 case TargetLowering::Legal: 1862 // Nothing to do. 1863 break; 1864 case TargetLowering::Expand: { 1865 ISD::CondCode InvCC = ISD::getSetCCSwappedOperands(CCCode); 1866 if (TLI.isCondCodeLegal(InvCC, OpVT)) { 1867 std::swap(LHS, RHS); 1868 CC = DAG.getCondCode(InvCC); 1869 return true; 1870 } 1871 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID; 1872 unsigned Opc = 0; 1873 switch (CCCode) { 1874 default: llvm_unreachable("Don't know how to expand this condition!"); 1875 case ISD::SETO: 1876 assert(TLI.getCondCodeAction(ISD::SETOEQ, OpVT) 1877 == TargetLowering::Legal 1878 && "If SETO is expanded, SETOEQ must be legal!"); 1879 CC1 = ISD::SETOEQ; CC2 = ISD::SETOEQ; Opc = ISD::AND; break; 1880 case ISD::SETUO: 1881 assert(TLI.getCondCodeAction(ISD::SETUNE, OpVT) 1882 == TargetLowering::Legal 1883 && "If SETUO is expanded, SETUNE must be legal!"); 1884 CC1 = ISD::SETUNE; CC2 = ISD::SETUNE; Opc = ISD::OR; break; 1885 case ISD::SETOEQ: 1886 case ISD::SETOGT: 1887 case ISD::SETOGE: 1888 case ISD::SETOLT: 1889 case ISD::SETOLE: 1890 case ISD::SETONE: 1891 case ISD::SETUEQ: 1892 case ISD::SETUNE: 1893 case ISD::SETUGT: 1894 case ISD::SETUGE: 1895 case ISD::SETULT: 1896 case ISD::SETULE: 1897 // If we are floating point, assign and break, otherwise fall through. 1898 if (!OpVT.isInteger()) { 1899 // We can use the 4th bit to tell if we are the unordered 1900 // or ordered version of the opcode. 1901 CC2 = ((unsigned)CCCode & 0x8U) ? ISD::SETUO : ISD::SETO; 1902 Opc = ((unsigned)CCCode & 0x8U) ? ISD::OR : ISD::AND; 1903 CC1 = (ISD::CondCode)(((int)CCCode & 0x7) | 0x10); 1904 break; 1905 } 1906 // Fallthrough if we are unsigned integer. 1907 case ISD::SETLE: 1908 case ISD::SETGT: 1909 case ISD::SETGE: 1910 case ISD::SETLT: 1911 // We only support using the inverted operation, which is computed above 1912 // and not a different manner of supporting expanding these cases. 1913 llvm_unreachable("Don't know how to expand this condition!"); 1914 case ISD::SETNE: 1915 case ISD::SETEQ: 1916 // Try inverting the result of the inverse condition. 1917 InvCC = CCCode == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ; 1918 if (TLI.isCondCodeLegal(InvCC, OpVT)) { 1919 CC = DAG.getCondCode(InvCC); 1920 NeedInvert = true; 1921 return true; 1922 } 1923 // If inverting the condition didn't work then we have no means to expand 1924 // the condition. 1925 llvm_unreachable("Don't know how to expand this condition!"); 1926 } 1927 1928 SDValue SetCC1, SetCC2; 1929 if (CCCode != ISD::SETO && CCCode != ISD::SETUO) { 1930 // If we aren't the ordered or unorder operation, 1931 // then the pattern is (LHS CC1 RHS) Opc (LHS CC2 RHS). 1932 SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1); 1933 SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2); 1934 } else { 1935 // Otherwise, the pattern is (LHS CC1 LHS) Opc (RHS CC2 RHS) 1936 SetCC1 = DAG.getSetCC(dl, VT, LHS, LHS, CC1); 1937 SetCC2 = DAG.getSetCC(dl, VT, RHS, RHS, CC2); 1938 } 1939 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2); 1940 RHS = SDValue(); 1941 CC = SDValue(); 1942 return true; 1943 } 1944 } 1945 return false; 1946 } 1947 1948 /// Emit a store/load combination to the stack. This stores 1949 /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does 1950 /// a load from the stack slot to DestVT, extending it if needed. 1951 /// The resultant code need not be legal. 1952 SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, 1953 EVT SlotVT, 1954 EVT DestVT, 1955 SDLoc dl) { 1956 // Create the stack frame object. 1957 unsigned SrcAlign = DAG.getDataLayout().getPrefTypeAlignment( 1958 SrcOp.getValueType().getTypeForEVT(*DAG.getContext())); 1959 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign); 1960 1961 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr); 1962 int SPFI = StackPtrFI->getIndex(); 1963 MachinePointerInfo PtrInfo = 1964 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); 1965 1966 unsigned SrcSize = SrcOp.getValueType().getSizeInBits(); 1967 unsigned SlotSize = SlotVT.getSizeInBits(); 1968 unsigned DestSize = DestVT.getSizeInBits(); 1969 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext()); 1970 unsigned DestAlign = DAG.getDataLayout().getPrefTypeAlignment(DestType); 1971 1972 // Emit a store to the stack slot. Use a truncstore if the input value is 1973 // later than DestVT. 1974 SDValue Store; 1975 1976 if (SrcSize > SlotSize) 1977 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr, 1978 PtrInfo, SlotVT, false, false, SrcAlign); 1979 else { 1980 assert(SrcSize == SlotSize && "Invalid store"); 1981 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr, 1982 PtrInfo, false, false, SrcAlign); 1983 } 1984 1985 // Result is a load from the stack slot. 1986 if (SlotSize == DestSize) 1987 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, 1988 false, false, false, DestAlign); 1989 1990 assert(SlotSize < DestSize && "Unknown extension!"); 1991 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, 1992 PtrInfo, SlotVT, false, false, false, DestAlign); 1993 } 1994 1995 SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { 1996 SDLoc dl(Node); 1997 // Create a vector sized/aligned stack slot, store the value to element #0, 1998 // then load the whole vector back out. 1999 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0)); 2000 2001 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr); 2002 int SPFI = StackPtrFI->getIndex(); 2003 2004 SDValue Ch = DAG.getTruncStore( 2005 DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr, 2006 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), 2007 Node->getValueType(0).getVectorElementType(), false, false, 0); 2008 return DAG.getLoad( 2009 Node->getValueType(0), dl, Ch, StackPtr, 2010 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), false, 2011 false, false, 0); 2012 } 2013 2014 static bool 2015 ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG, 2016 const TargetLowering &TLI, SDValue &Res) { 2017 unsigned NumElems = Node->getNumOperands(); 2018 SDLoc dl(Node); 2019 EVT VT = Node->getValueType(0); 2020 2021 // Try to group the scalars into pairs, shuffle the pairs together, then 2022 // shuffle the pairs of pairs together, etc. until the vector has 2023 // been built. This will work only if all of the necessary shuffle masks 2024 // are legal. 2025 2026 // We do this in two phases; first to check the legality of the shuffles, 2027 // and next, assuming that all shuffles are legal, to create the new nodes. 2028 for (int Phase = 0; Phase < 2; ++Phase) { 2029 SmallVector<std::pair<SDValue, SmallVector<int, 16> >, 16> IntermedVals, 2030 NewIntermedVals; 2031 for (unsigned i = 0; i < NumElems; ++i) { 2032 SDValue V = Node->getOperand(i); 2033 if (V.isUndef()) 2034 continue; 2035 2036 SDValue Vec; 2037 if (Phase) 2038 Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V); 2039 IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i))); 2040 } 2041 2042 while (IntermedVals.size() > 2) { 2043 NewIntermedVals.clear(); 2044 for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) { 2045 // This vector and the next vector are shuffled together (simply to 2046 // append the one to the other). 2047 SmallVector<int, 16> ShuffleVec(NumElems, -1); 2048 2049 SmallVector<int, 16> FinalIndices; 2050 FinalIndices.reserve(IntermedVals[i].second.size() + 2051 IntermedVals[i+1].second.size()); 2052 2053 int k = 0; 2054 for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f; 2055 ++j, ++k) { 2056 ShuffleVec[k] = j; 2057 FinalIndices.push_back(IntermedVals[i].second[j]); 2058 } 2059 for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f; 2060 ++j, ++k) { 2061 ShuffleVec[k] = NumElems + j; 2062 FinalIndices.push_back(IntermedVals[i+1].second[j]); 2063 } 2064 2065 SDValue Shuffle; 2066 if (Phase) 2067 Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first, 2068 IntermedVals[i+1].first, 2069 ShuffleVec.data()); 2070 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT)) 2071 return false; 2072 NewIntermedVals.push_back( 2073 std::make_pair(Shuffle, std::move(FinalIndices))); 2074 } 2075 2076 // If we had an odd number of defined values, then append the last 2077 // element to the array of new vectors. 2078 if ((IntermedVals.size() & 1) != 0) 2079 NewIntermedVals.push_back(IntermedVals.back()); 2080 2081 IntermedVals.swap(NewIntermedVals); 2082 } 2083 2084 assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 && 2085 "Invalid number of intermediate vectors"); 2086 SDValue Vec1 = IntermedVals[0].first; 2087 SDValue Vec2; 2088 if (IntermedVals.size() > 1) 2089 Vec2 = IntermedVals[1].first; 2090 else if (Phase) 2091 Vec2 = DAG.getUNDEF(VT); 2092 2093 SmallVector<int, 16> ShuffleVec(NumElems, -1); 2094 for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i) 2095 ShuffleVec[IntermedVals[0].second[i]] = i; 2096 for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i) 2097 ShuffleVec[IntermedVals[1].second[i]] = NumElems + i; 2098 2099 if (Phase) 2100 Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data()); 2101 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT)) 2102 return false; 2103 } 2104 2105 return true; 2106 } 2107 2108 /// Expand a BUILD_VECTOR node on targets that don't 2109 /// support the operation, but do support the resultant vector type. 2110 SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { 2111 unsigned NumElems = Node->getNumOperands(); 2112 SDValue Value1, Value2; 2113 SDLoc dl(Node); 2114 EVT VT = Node->getValueType(0); 2115 EVT OpVT = Node->getOperand(0).getValueType(); 2116 EVT EltVT = VT.getVectorElementType(); 2117 2118 // If the only non-undef value is the low element, turn this into a 2119 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X. 2120 bool isOnlyLowElement = true; 2121 bool MoreThanTwoValues = false; 2122 bool isConstant = true; 2123 for (unsigned i = 0; i < NumElems; ++i) { 2124 SDValue V = Node->getOperand(i); 2125 if (V.isUndef()) 2126 continue; 2127 if (i > 0) 2128 isOnlyLowElement = false; 2129 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 2130 isConstant = false; 2131 2132 if (!Value1.getNode()) { 2133 Value1 = V; 2134 } else if (!Value2.getNode()) { 2135 if (V != Value1) 2136 Value2 = V; 2137 } else if (V != Value1 && V != Value2) { 2138 MoreThanTwoValues = true; 2139 } 2140 } 2141 2142 if (!Value1.getNode()) 2143 return DAG.getUNDEF(VT); 2144 2145 if (isOnlyLowElement) 2146 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0)); 2147 2148 // If all elements are constants, create a load from the constant pool. 2149 if (isConstant) { 2150 SmallVector<Constant*, 16> CV; 2151 for (unsigned i = 0, e = NumElems; i != e; ++i) { 2152 if (ConstantFPSDNode *V = 2153 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { 2154 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue())); 2155 } else if (ConstantSDNode *V = 2156 dyn_cast<ConstantSDNode>(Node->getOperand(i))) { 2157 if (OpVT==EltVT) 2158 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue())); 2159 else { 2160 // If OpVT and EltVT don't match, EltVT is not legal and the 2161 // element values have been promoted/truncated earlier. Undo this; 2162 // we don't want a v16i8 to become a v16i32 for example. 2163 const ConstantInt *CI = V->getConstantIntValue(); 2164 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()), 2165 CI->getZExtValue())); 2166 } 2167 } else { 2168 assert(Node->getOperand(i).isUndef()); 2169 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext()); 2170 CV.push_back(UndefValue::get(OpNTy)); 2171 } 2172 } 2173 Constant *CP = ConstantVector::get(CV); 2174 SDValue CPIdx = 2175 DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout())); 2176 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 2177 return DAG.getLoad( 2178 VT, dl, DAG.getEntryNode(), CPIdx, 2179 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2180 false, false, Alignment); 2181 } 2182 2183 SmallSet<SDValue, 16> DefinedValues; 2184 for (unsigned i = 0; i < NumElems; ++i) { 2185 if (Node->getOperand(i).isUndef()) 2186 continue; 2187 DefinedValues.insert(Node->getOperand(i)); 2188 } 2189 2190 if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) { 2191 if (!MoreThanTwoValues) { 2192 SmallVector<int, 8> ShuffleVec(NumElems, -1); 2193 for (unsigned i = 0; i < NumElems; ++i) { 2194 SDValue V = Node->getOperand(i); 2195 if (V.isUndef()) 2196 continue; 2197 ShuffleVec[i] = V == Value1 ? 0 : NumElems; 2198 } 2199 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) { 2200 // Get the splatted value into the low element of a vector register. 2201 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1); 2202 SDValue Vec2; 2203 if (Value2.getNode()) 2204 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2); 2205 else 2206 Vec2 = DAG.getUNDEF(VT); 2207 2208 // Return shuffle(LowValVec, undef, <0,0,0,0>) 2209 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data()); 2210 } 2211 } else { 2212 SDValue Res; 2213 if (ExpandBVWithShuffles(Node, DAG, TLI, Res)) 2214 return Res; 2215 } 2216 } 2217 2218 // Otherwise, we can't handle this case efficiently. 2219 return ExpandVectorBuildThroughStack(Node); 2220 } 2221 2222 // Expand a node into a call to a libcall. If the result value 2223 // does not fit into a register, return the lo part and set the hi part to the 2224 // by-reg argument. If it does fit into a single register, return the result 2225 // and leave the Hi part unset. 2226 SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, 2227 bool isSigned) { 2228 TargetLowering::ArgListTy Args; 2229 TargetLowering::ArgListEntry Entry; 2230 for (const SDValue &Op : Node->op_values()) { 2231 EVT ArgVT = Op.getValueType(); 2232 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 2233 Entry.Node = Op; 2234 Entry.Ty = ArgTy; 2235 Entry.isSExt = isSigned; 2236 Entry.isZExt = !isSigned; 2237 Args.push_back(Entry); 2238 } 2239 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 2240 TLI.getPointerTy(DAG.getDataLayout())); 2241 2242 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext()); 2243 2244 // By default, the input chain to this libcall is the entry node of the 2245 // function. If the libcall is going to be emitted as a tail call then 2246 // TLI.isUsedByReturnOnly will change it to the right chain if the return 2247 // node which is being folded has a non-entry input chain. 2248 SDValue InChain = DAG.getEntryNode(); 2249 2250 // isTailCall may be true since the callee does not reference caller stack 2251 // frame. Check if it's in the right position and that the return types match. 2252 SDValue TCChain = InChain; 2253 const Function *F = DAG.getMachineFunction().getFunction(); 2254 bool isTailCall = 2255 TLI.isInTailCallPosition(DAG, Node, TCChain) && 2256 (RetTy == F->getReturnType() || F->getReturnType()->isVoidTy()); 2257 if (isTailCall) 2258 InChain = TCChain; 2259 2260 TargetLowering::CallLoweringInfo CLI(DAG); 2261 CLI.setDebugLoc(SDLoc(Node)).setChain(InChain) 2262 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0) 2263 .setTailCall(isTailCall).setSExtResult(isSigned).setZExtResult(!isSigned); 2264 2265 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 2266 2267 if (!CallInfo.second.getNode()) 2268 // It's a tailcall, return the chain (which is the DAG root). 2269 return DAG.getRoot(); 2270 2271 return CallInfo.first; 2272 } 2273 2274 /// Generate a libcall taking the given operands as arguments 2275 /// and returning a result of type RetVT. 2276 SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, 2277 const SDValue *Ops, unsigned NumOps, 2278 bool isSigned, SDLoc dl) { 2279 TargetLowering::ArgListTy Args; 2280 Args.reserve(NumOps); 2281 2282 TargetLowering::ArgListEntry Entry; 2283 for (unsigned i = 0; i != NumOps; ++i) { 2284 Entry.Node = Ops[i]; 2285 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext()); 2286 Entry.isSExt = isSigned; 2287 Entry.isZExt = !isSigned; 2288 Args.push_back(Entry); 2289 } 2290 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 2291 TLI.getPointerTy(DAG.getDataLayout())); 2292 2293 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 2294 2295 TargetLowering::CallLoweringInfo CLI(DAG); 2296 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode()) 2297 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0) 2298 .setSExtResult(isSigned).setZExtResult(!isSigned); 2299 2300 std::pair<SDValue,SDValue> CallInfo = TLI.LowerCallTo(CLI); 2301 2302 return CallInfo.first; 2303 } 2304 2305 // Expand a node into a call to a libcall. Similar to 2306 // ExpandLibCall except that the first operand is the in-chain. 2307 std::pair<SDValue, SDValue> 2308 SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC, 2309 SDNode *Node, 2310 bool isSigned) { 2311 SDValue InChain = Node->getOperand(0); 2312 2313 TargetLowering::ArgListTy Args; 2314 TargetLowering::ArgListEntry Entry; 2315 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) { 2316 EVT ArgVT = Node->getOperand(i).getValueType(); 2317 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 2318 Entry.Node = Node->getOperand(i); 2319 Entry.Ty = ArgTy; 2320 Entry.isSExt = isSigned; 2321 Entry.isZExt = !isSigned; 2322 Args.push_back(Entry); 2323 } 2324 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 2325 TLI.getPointerTy(DAG.getDataLayout())); 2326 2327 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext()); 2328 2329 TargetLowering::CallLoweringInfo CLI(DAG); 2330 CLI.setDebugLoc(SDLoc(Node)).setChain(InChain) 2331 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0) 2332 .setSExtResult(isSigned).setZExtResult(!isSigned); 2333 2334 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 2335 2336 return CallInfo; 2337 } 2338 2339 SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node, 2340 RTLIB::Libcall Call_F32, 2341 RTLIB::Libcall Call_F64, 2342 RTLIB::Libcall Call_F80, 2343 RTLIB::Libcall Call_F128, 2344 RTLIB::Libcall Call_PPCF128) { 2345 RTLIB::Libcall LC; 2346 switch (Node->getSimpleValueType(0).SimpleTy) { 2347 default: llvm_unreachable("Unexpected request for libcall!"); 2348 case MVT::f32: LC = Call_F32; break; 2349 case MVT::f64: LC = Call_F64; break; 2350 case MVT::f80: LC = Call_F80; break; 2351 case MVT::f128: LC = Call_F128; break; 2352 case MVT::ppcf128: LC = Call_PPCF128; break; 2353 } 2354 return ExpandLibCall(LC, Node, false); 2355 } 2356 2357 SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned, 2358 RTLIB::Libcall Call_I8, 2359 RTLIB::Libcall Call_I16, 2360 RTLIB::Libcall Call_I32, 2361 RTLIB::Libcall Call_I64, 2362 RTLIB::Libcall Call_I128) { 2363 RTLIB::Libcall LC; 2364 switch (Node->getSimpleValueType(0).SimpleTy) { 2365 default: llvm_unreachable("Unexpected request for libcall!"); 2366 case MVT::i8: LC = Call_I8; break; 2367 case MVT::i16: LC = Call_I16; break; 2368 case MVT::i32: LC = Call_I32; break; 2369 case MVT::i64: LC = Call_I64; break; 2370 case MVT::i128: LC = Call_I128; break; 2371 } 2372 return ExpandLibCall(LC, Node, isSigned); 2373 } 2374 2375 /// Issue libcalls to __{u}divmod to compute div / rem pairs. 2376 void 2377 SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node, 2378 SmallVectorImpl<SDValue> &Results) { 2379 unsigned Opcode = Node->getOpcode(); 2380 bool isSigned = Opcode == ISD::SDIVREM; 2381 2382 RTLIB::Libcall LC; 2383 switch (Node->getSimpleValueType(0).SimpleTy) { 2384 default: llvm_unreachable("Unexpected request for libcall!"); 2385 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 2386 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 2387 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 2388 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 2389 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break; 2390 } 2391 2392 // The input chain to this libcall is the entry node of the function. 2393 // Legalizing the call will automatically add the previous call to the 2394 // dependence. 2395 SDValue InChain = DAG.getEntryNode(); 2396 2397 EVT RetVT = Node->getValueType(0); 2398 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 2399 2400 TargetLowering::ArgListTy Args; 2401 TargetLowering::ArgListEntry Entry; 2402 for (const SDValue &Op : Node->op_values()) { 2403 EVT ArgVT = Op.getValueType(); 2404 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 2405 Entry.Node = Op; 2406 Entry.Ty = ArgTy; 2407 Entry.isSExt = isSigned; 2408 Entry.isZExt = !isSigned; 2409 Args.push_back(Entry); 2410 } 2411 2412 // Also pass the return address of the remainder. 2413 SDValue FIPtr = DAG.CreateStackTemporary(RetVT); 2414 Entry.Node = FIPtr; 2415 Entry.Ty = RetTy->getPointerTo(); 2416 Entry.isSExt = isSigned; 2417 Entry.isZExt = !isSigned; 2418 Args.push_back(Entry); 2419 2420 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 2421 TLI.getPointerTy(DAG.getDataLayout())); 2422 2423 SDLoc dl(Node); 2424 TargetLowering::CallLoweringInfo CLI(DAG); 2425 CLI.setDebugLoc(dl).setChain(InChain) 2426 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0) 2427 .setSExtResult(isSigned).setZExtResult(!isSigned); 2428 2429 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 2430 2431 // Remainder is loaded back from the stack frame. 2432 SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, 2433 MachinePointerInfo(), false, false, false, 0); 2434 Results.push_back(CallInfo.first); 2435 Results.push_back(Rem); 2436 } 2437 2438 /// Return true if sincos libcall is available. 2439 static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) { 2440 RTLIB::Libcall LC; 2441 switch (Node->getSimpleValueType(0).SimpleTy) { 2442 default: llvm_unreachable("Unexpected request for libcall!"); 2443 case MVT::f32: LC = RTLIB::SINCOS_F32; break; 2444 case MVT::f64: LC = RTLIB::SINCOS_F64; break; 2445 case MVT::f80: LC = RTLIB::SINCOS_F80; break; 2446 case MVT::f128: LC = RTLIB::SINCOS_F128; break; 2447 case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break; 2448 } 2449 return TLI.getLibcallName(LC) != nullptr; 2450 } 2451 2452 /// Return true if sincos libcall is available and can be used to combine sin 2453 /// and cos. 2454 static bool canCombineSinCosLibcall(SDNode *Node, const TargetLowering &TLI, 2455 const TargetMachine &TM) { 2456 if (!isSinCosLibcallAvailable(Node, TLI)) 2457 return false; 2458 // GNU sin/cos functions set errno while sincos does not. Therefore 2459 // combining sin and cos is only safe if unsafe-fpmath is enabled. 2460 bool isGNU = Triple(TM.getTargetTriple()).getEnvironment() == Triple::GNU; 2461 if (isGNU && !TM.Options.UnsafeFPMath) 2462 return false; 2463 return true; 2464 } 2465 2466 /// Only issue sincos libcall if both sin and cos are needed. 2467 static bool useSinCos(SDNode *Node) { 2468 unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN 2469 ? ISD::FCOS : ISD::FSIN; 2470 2471 SDValue Op0 = Node->getOperand(0); 2472 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(), 2473 UE = Op0.getNode()->use_end(); UI != UE; ++UI) { 2474 SDNode *User = *UI; 2475 if (User == Node) 2476 continue; 2477 // The other user might have been turned into sincos already. 2478 if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS) 2479 return true; 2480 } 2481 return false; 2482 } 2483 2484 /// Issue libcalls to sincos to compute sin / cos pairs. 2485 void 2486 SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node, 2487 SmallVectorImpl<SDValue> &Results) { 2488 RTLIB::Libcall LC; 2489 switch (Node->getSimpleValueType(0).SimpleTy) { 2490 default: llvm_unreachable("Unexpected request for libcall!"); 2491 case MVT::f32: LC = RTLIB::SINCOS_F32; break; 2492 case MVT::f64: LC = RTLIB::SINCOS_F64; break; 2493 case MVT::f80: LC = RTLIB::SINCOS_F80; break; 2494 case MVT::f128: LC = RTLIB::SINCOS_F128; break; 2495 case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break; 2496 } 2497 2498 // The input chain to this libcall is the entry node of the function. 2499 // Legalizing the call will automatically add the previous call to the 2500 // dependence. 2501 SDValue InChain = DAG.getEntryNode(); 2502 2503 EVT RetVT = Node->getValueType(0); 2504 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 2505 2506 TargetLowering::ArgListTy Args; 2507 TargetLowering::ArgListEntry Entry; 2508 2509 // Pass the argument. 2510 Entry.Node = Node->getOperand(0); 2511 Entry.Ty = RetTy; 2512 Entry.isSExt = false; 2513 Entry.isZExt = false; 2514 Args.push_back(Entry); 2515 2516 // Pass the return address of sin. 2517 SDValue SinPtr = DAG.CreateStackTemporary(RetVT); 2518 Entry.Node = SinPtr; 2519 Entry.Ty = RetTy->getPointerTo(); 2520 Entry.isSExt = false; 2521 Entry.isZExt = false; 2522 Args.push_back(Entry); 2523 2524 // Also pass the return address of the cos. 2525 SDValue CosPtr = DAG.CreateStackTemporary(RetVT); 2526 Entry.Node = CosPtr; 2527 Entry.Ty = RetTy->getPointerTo(); 2528 Entry.isSExt = false; 2529 Entry.isZExt = false; 2530 Args.push_back(Entry); 2531 2532 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 2533 TLI.getPointerTy(DAG.getDataLayout())); 2534 2535 SDLoc dl(Node); 2536 TargetLowering::CallLoweringInfo CLI(DAG); 2537 CLI.setDebugLoc(dl).setChain(InChain) 2538 .setCallee(TLI.getLibcallCallingConv(LC), 2539 Type::getVoidTy(*DAG.getContext()), Callee, std::move(Args), 0); 2540 2541 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 2542 2543 Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr, 2544 MachinePointerInfo(), false, false, false, 0)); 2545 Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr, 2546 MachinePointerInfo(), false, false, false, 0)); 2547 } 2548 2549 /// This function is responsible for legalizing a 2550 /// INT_TO_FP operation of the specified operand when the target requests that 2551 /// we expand it. At this point, we know that the result and operand types are 2552 /// legal for the target. 2553 SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, 2554 SDValue Op0, 2555 EVT DestVT, 2556 SDLoc dl) { 2557 // TODO: Should any fast-math-flags be set for the created nodes? 2558 2559 if (Op0.getValueType() == MVT::i32 && TLI.isTypeLegal(MVT::f64)) { 2560 // simple 32-bit [signed|unsigned] integer to float/double expansion 2561 2562 // Get the stack frame index of a 8 byte buffer. 2563 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64); 2564 2565 // word offset constant for Hi/Lo address computation 2566 SDValue WordOff = DAG.getConstant(sizeof(int), dl, 2567 StackSlot.getValueType()); 2568 // set up Hi and Lo (into buffer) address based on endian 2569 SDValue Hi = StackSlot; 2570 SDValue Lo = DAG.getNode(ISD::ADD, dl, StackSlot.getValueType(), 2571 StackSlot, WordOff); 2572 if (DAG.getDataLayout().isLittleEndian()) 2573 std::swap(Hi, Lo); 2574 2575 // if signed map to unsigned space 2576 SDValue Op0Mapped; 2577 if (isSigned) { 2578 // constant used to invert sign bit (signed to unsigned mapping) 2579 SDValue SignBit = DAG.getConstant(0x80000000u, dl, MVT::i32); 2580 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit); 2581 } else { 2582 Op0Mapped = Op0; 2583 } 2584 // store the lo of the constructed double - based on integer input 2585 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, 2586 Op0Mapped, Lo, MachinePointerInfo(), 2587 false, false, 0); 2588 // initial hi portion of constructed double 2589 SDValue InitialHi = DAG.getConstant(0x43300000u, dl, MVT::i32); 2590 // store the hi of the constructed double - biased exponent 2591 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi, 2592 MachinePointerInfo(), 2593 false, false, 0); 2594 // load the constructed double 2595 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, 2596 MachinePointerInfo(), false, false, false, 0); 2597 // FP constant to bias correct the final result 2598 SDValue Bias = DAG.getConstantFP(isSigned ? 2599 BitsToDouble(0x4330000080000000ULL) : 2600 BitsToDouble(0x4330000000000000ULL), 2601 dl, MVT::f64); 2602 // subtract the bias 2603 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias); 2604 // final result 2605 SDValue Result; 2606 // handle final rounding 2607 if (DestVT == MVT::f64) { 2608 // do nothing 2609 Result = Sub; 2610 } else if (DestVT.bitsLT(MVT::f64)) { 2611 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub, 2612 DAG.getIntPtrConstant(0, dl)); 2613 } else if (DestVT.bitsGT(MVT::f64)) { 2614 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub); 2615 } 2616 return Result; 2617 } 2618 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); 2619 // Code below here assumes !isSigned without checking again. 2620 2621 // Implementation of unsigned i64 to f64 following the algorithm in 2622 // __floatundidf in compiler_rt. This implementation has the advantage 2623 // of performing rounding correctly, both in the default rounding mode 2624 // and in all alternate rounding modes. 2625 // TODO: Generalize this for use with other types. 2626 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) { 2627 SDValue TwoP52 = 2628 DAG.getConstant(UINT64_C(0x4330000000000000), dl, MVT::i64); 2629 SDValue TwoP84PlusTwoP52 = 2630 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), dl, 2631 MVT::f64); 2632 SDValue TwoP84 = 2633 DAG.getConstant(UINT64_C(0x4530000000000000), dl, MVT::i64); 2634 2635 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32); 2636 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, 2637 DAG.getConstant(32, dl, MVT::i64)); 2638 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52); 2639 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84); 2640 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr); 2641 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr); 2642 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt, 2643 TwoP84PlusTwoP52); 2644 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub); 2645 } 2646 2647 // Implementation of unsigned i64 to f32. 2648 // TODO: Generalize this for use with other types. 2649 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) { 2650 // For unsigned conversions, convert them to signed conversions using the 2651 // algorithm from the x86_64 __floatundidf in compiler_rt. 2652 if (!isSigned) { 2653 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0); 2654 2655 SDValue ShiftConst = DAG.getConstant( 2656 1, dl, TLI.getShiftAmountTy(Op0.getValueType(), DAG.getDataLayout())); 2657 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst); 2658 SDValue AndConst = DAG.getConstant(1, dl, MVT::i64); 2659 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst); 2660 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr); 2661 2662 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or); 2663 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt); 2664 2665 // TODO: This really should be implemented using a branch rather than a 2666 // select. We happen to get lucky and machinesink does the right 2667 // thing most of the time. This would be a good candidate for a 2668 //pseudo-op, or, even better, for whole-function isel. 2669 SDValue SignBitTest = DAG.getSetCC(dl, getSetCCResultType(MVT::i64), 2670 Op0, DAG.getConstant(0, dl, MVT::i64), ISD::SETLT); 2671 return DAG.getSelect(dl, MVT::f32, SignBitTest, Slow, Fast); 2672 } 2673 2674 // Otherwise, implement the fully general conversion. 2675 2676 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, 2677 DAG.getConstant(UINT64_C(0xfffffffffffff800), dl, MVT::i64)); 2678 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, 2679 DAG.getConstant(UINT64_C(0x800), dl, MVT::i64)); 2680 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, 2681 DAG.getConstant(UINT64_C(0x7ff), dl, MVT::i64)); 2682 SDValue Ne = DAG.getSetCC(dl, getSetCCResultType(MVT::i64), And2, 2683 DAG.getConstant(UINT64_C(0), dl, MVT::i64), 2684 ISD::SETNE); 2685 SDValue Sel = DAG.getSelect(dl, MVT::i64, Ne, Or, Op0); 2686 SDValue Ge = DAG.getSetCC(dl, getSetCCResultType(MVT::i64), Op0, 2687 DAG.getConstant(UINT64_C(0x0020000000000000), dl, 2688 MVT::i64), 2689 ISD::SETUGE); 2690 SDValue Sel2 = DAG.getSelect(dl, MVT::i64, Ge, Sel, Op0); 2691 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType(), DAG.getDataLayout()); 2692 2693 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2, 2694 DAG.getConstant(32, dl, SHVT)); 2695 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh); 2696 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc); 2697 SDValue TwoP32 = 2698 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), dl, 2699 MVT::f64); 2700 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt); 2701 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2); 2702 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo); 2703 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2); 2704 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd, 2705 DAG.getIntPtrConstant(0, dl)); 2706 } 2707 2708 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0); 2709 2710 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(Op0.getValueType()), 2711 Op0, 2712 DAG.getConstant(0, dl, Op0.getValueType()), 2713 ISD::SETLT); 2714 SDValue Zero = DAG.getIntPtrConstant(0, dl), 2715 Four = DAG.getIntPtrConstant(4, dl); 2716 SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(), 2717 SignSet, Four, Zero); 2718 2719 // If the sign bit of the integer is set, the large number will be treated 2720 // as a negative number. To counteract this, the dynamic code adds an 2721 // offset depending on the data type. 2722 uint64_t FF; 2723 switch (Op0.getSimpleValueType().SimpleTy) { 2724 default: llvm_unreachable("Unsupported integer type!"); 2725 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) 2726 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) 2727 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) 2728 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) 2729 } 2730 if (DAG.getDataLayout().isLittleEndian()) 2731 FF <<= 32; 2732 Constant *FudgeFactor = ConstantInt::get( 2733 Type::getInt64Ty(*DAG.getContext()), FF); 2734 2735 SDValue CPIdx = 2736 DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout())); 2737 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); 2738 CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset); 2739 Alignment = std::min(Alignment, 4u); 2740 SDValue FudgeInReg; 2741 if (DestVT == MVT::f32) 2742 FudgeInReg = DAG.getLoad( 2743 MVT::f32, dl, DAG.getEntryNode(), CPIdx, 2744 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2745 false, false, Alignment); 2746 else { 2747 SDValue Load = DAG.getExtLoad( 2748 ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx, 2749 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32, 2750 false, false, false, Alignment); 2751 HandleSDNode Handle(Load); 2752 LegalizeOp(Load.getNode()); 2753 FudgeInReg = Handle.getValue(); 2754 } 2755 2756 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg); 2757 } 2758 2759 /// This function is responsible for legalizing a 2760 /// *INT_TO_FP operation of the specified operand when the target requests that 2761 /// we promote it. At this point, we know that the result and operand types are 2762 /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP 2763 /// operation that takes a larger input. 2764 SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp, 2765 EVT DestVT, 2766 bool isSigned, 2767 SDLoc dl) { 2768 // First step, figure out the appropriate *INT_TO_FP operation to use. 2769 EVT NewInTy = LegalOp.getValueType(); 2770 2771 unsigned OpToUse = 0; 2772 2773 // Scan for the appropriate larger type to use. 2774 while (1) { 2775 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1); 2776 assert(NewInTy.isInteger() && "Ran out of possibilities!"); 2777 2778 // If the target supports SINT_TO_FP of this type, use it. 2779 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) { 2780 OpToUse = ISD::SINT_TO_FP; 2781 break; 2782 } 2783 if (isSigned) continue; 2784 2785 // If the target supports UINT_TO_FP of this type, use it. 2786 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) { 2787 OpToUse = ISD::UINT_TO_FP; 2788 break; 2789 } 2790 2791 // Otherwise, try a larger type. 2792 } 2793 2794 // Okay, we found the operation and type to use. Zero extend our input to the 2795 // desired type then run the operation on it. 2796 return DAG.getNode(OpToUse, dl, DestVT, 2797 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 2798 dl, NewInTy, LegalOp)); 2799 } 2800 2801 /// This function is responsible for legalizing a 2802 /// FP_TO_*INT operation of the specified operand when the target requests that 2803 /// we promote it. At this point, we know that the result and operand types are 2804 /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT 2805 /// operation that returns a larger result. 2806 SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp, 2807 EVT DestVT, 2808 bool isSigned, 2809 SDLoc dl) { 2810 // First step, figure out the appropriate FP_TO*INT operation to use. 2811 EVT NewOutTy = DestVT; 2812 2813 unsigned OpToUse = 0; 2814 2815 // Scan for the appropriate larger type to use. 2816 while (1) { 2817 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1); 2818 assert(NewOutTy.isInteger() && "Ran out of possibilities!"); 2819 2820 // A larger signed type can hold all unsigned values of the requested type, 2821 // so using FP_TO_SINT is valid 2822 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) { 2823 OpToUse = ISD::FP_TO_SINT; 2824 break; 2825 } 2826 2827 // However, if the value may be < 0.0, we *must* use some FP_TO_SINT. 2828 if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) { 2829 OpToUse = ISD::FP_TO_UINT; 2830 break; 2831 } 2832 2833 // Otherwise, try a larger type. 2834 } 2835 2836 2837 // Okay, we found the operation and type to use. 2838 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp); 2839 2840 // Truncate the result of the extended FP_TO_*INT operation to the desired 2841 // size. 2842 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation); 2843 } 2844 2845 /// Open code the operations for BITREVERSE. 2846 SDValue SelectionDAGLegalize::ExpandBITREVERSE(SDValue Op, SDLoc dl) { 2847 EVT VT = Op.getValueType(); 2848 EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 2849 unsigned Sz = VT.getScalarSizeInBits(); 2850 2851 SDValue Tmp, Tmp2; 2852 Tmp = DAG.getConstant(0, dl, VT); 2853 for (unsigned I = 0, J = Sz-1; I < Sz; ++I, --J) { 2854 if (I < J) 2855 Tmp2 = 2856 DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(J - I, dl, SHVT)); 2857 else 2858 Tmp2 = 2859 DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(I - J, dl, SHVT)); 2860 2861 APInt Shift(Sz, 1); 2862 Shift = Shift.shl(J); 2863 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(Shift, dl, VT)); 2864 Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp, Tmp2); 2865 } 2866 2867 return Tmp; 2868 } 2869 2870 /// Open code the operations for BSWAP of the specified operation. 2871 SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, SDLoc dl) { 2872 EVT VT = Op.getValueType(); 2873 EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 2874 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; 2875 switch (VT.getSimpleVT().SimpleTy) { 2876 default: llvm_unreachable("Unhandled Expand type in BSWAP!"); 2877 case MVT::i16: 2878 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); 2879 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); 2880 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 2881 case MVT::i32: 2882 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT)); 2883 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); 2884 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); 2885 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT)); 2886 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, 2887 DAG.getConstant(0xFF0000, dl, VT)); 2888 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, dl, VT)); 2889 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); 2890 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1); 2891 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2); 2892 case MVT::i64: 2893 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, dl, SHVT)); 2894 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, dl, SHVT)); 2895 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT)); 2896 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); 2897 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); 2898 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT)); 2899 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, dl, SHVT)); 2900 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, dl, SHVT)); 2901 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, 2902 DAG.getConstant(255ULL<<48, dl, VT)); 2903 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, 2904 DAG.getConstant(255ULL<<40, dl, VT)); 2905 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, 2906 DAG.getConstant(255ULL<<32, dl, VT)); 2907 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, 2908 DAG.getConstant(255ULL<<24, dl, VT)); 2909 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, 2910 DAG.getConstant(255ULL<<16, dl, VT)); 2911 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, 2912 DAG.getConstant(255ULL<<8 , dl, VT)); 2913 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7); 2914 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5); 2915 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); 2916 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1); 2917 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6); 2918 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2); 2919 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4); 2920 } 2921 } 2922 2923 /// Expand the specified bitcount instruction into operations. 2924 SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op, 2925 SDLoc dl) { 2926 switch (Opc) { 2927 default: llvm_unreachable("Cannot expand this yet!"); 2928 case ISD::CTPOP: { 2929 EVT VT = Op.getValueType(); 2930 EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 2931 unsigned Len = VT.getSizeInBits(); 2932 2933 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 && 2934 "CTPOP not implemented for this type."); 2935 2936 // This is the "best" algorithm from 2937 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel 2938 2939 SDValue Mask55 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x55)), 2940 dl, VT); 2941 SDValue Mask33 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x33)), 2942 dl, VT); 2943 SDValue Mask0F = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x0F)), 2944 dl, VT); 2945 SDValue Mask01 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x01)), 2946 dl, VT); 2947 2948 // v = v - ((v >> 1) & 0x55555555...) 2949 Op = DAG.getNode(ISD::SUB, dl, VT, Op, 2950 DAG.getNode(ISD::AND, dl, VT, 2951 DAG.getNode(ISD::SRL, dl, VT, Op, 2952 DAG.getConstant(1, dl, ShVT)), 2953 Mask55)); 2954 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...) 2955 Op = DAG.getNode(ISD::ADD, dl, VT, 2956 DAG.getNode(ISD::AND, dl, VT, Op, Mask33), 2957 DAG.getNode(ISD::AND, dl, VT, 2958 DAG.getNode(ISD::SRL, dl, VT, Op, 2959 DAG.getConstant(2, dl, ShVT)), 2960 Mask33)); 2961 // v = (v + (v >> 4)) & 0x0F0F0F0F... 2962 Op = DAG.getNode(ISD::AND, dl, VT, 2963 DAG.getNode(ISD::ADD, dl, VT, Op, 2964 DAG.getNode(ISD::SRL, dl, VT, Op, 2965 DAG.getConstant(4, dl, ShVT))), 2966 Mask0F); 2967 // v = (v * 0x01010101...) >> (Len - 8) 2968 Op = DAG.getNode(ISD::SRL, dl, VT, 2969 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01), 2970 DAG.getConstant(Len - 8, dl, ShVT)); 2971 2972 return Op; 2973 } 2974 case ISD::CTLZ_ZERO_UNDEF: 2975 // This trivially expands to CTLZ. 2976 return DAG.getNode(ISD::CTLZ, dl, Op.getValueType(), Op); 2977 case ISD::CTLZ: { 2978 EVT VT = Op.getValueType(); 2979 unsigned len = VT.getSizeInBits(); 2980 2981 if (TLI.isOperationLegalOrCustom(ISD::CTLZ_ZERO_UNDEF, VT)) { 2982 EVT SetCCVT = getSetCCResultType(VT); 2983 SDValue CTLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, VT, Op); 2984 SDValue Zero = DAG.getConstant(0, dl, VT); 2985 SDValue SrcIsZero = DAG.getSetCC(dl, SetCCVT, Op, Zero, ISD::SETEQ); 2986 return DAG.getNode(ISD::SELECT, dl, VT, SrcIsZero, 2987 DAG.getConstant(len, dl, VT), CTLZ); 2988 } 2989 2990 // for now, we do this: 2991 // x = x | (x >> 1); 2992 // x = x | (x >> 2); 2993 // ... 2994 // x = x | (x >>16); 2995 // x = x | (x >>32); // for 64-bit input 2996 // return popcount(~x); 2997 // 2998 // Ref: "Hacker's Delight" by Henry Warren 2999 EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 3000 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { 3001 SDValue Tmp3 = DAG.getConstant(1ULL << i, dl, ShVT); 3002 Op = DAG.getNode(ISD::OR, dl, VT, Op, 3003 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3)); 3004 } 3005 Op = DAG.getNOT(dl, Op, VT); 3006 return DAG.getNode(ISD::CTPOP, dl, VT, Op); 3007 } 3008 case ISD::CTTZ_ZERO_UNDEF: 3009 // This trivially expands to CTTZ. 3010 return DAG.getNode(ISD::CTTZ, dl, Op.getValueType(), Op); 3011 case ISD::CTTZ: { 3012 // for now, we use: { return popcount(~x & (x - 1)); } 3013 // unless the target has ctlz but not ctpop, in which case we use: 3014 // { return 32 - nlz(~x & (x-1)); } 3015 // Ref: "Hacker's Delight" by Henry Warren 3016 EVT VT = Op.getValueType(); 3017 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT, 3018 DAG.getNOT(dl, Op, VT), 3019 DAG.getNode(ISD::SUB, dl, VT, Op, 3020 DAG.getConstant(1, dl, VT))); 3021 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. 3022 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) && 3023 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT)) 3024 return DAG.getNode(ISD::SUB, dl, VT, 3025 DAG.getConstant(VT.getSizeInBits(), dl, VT), 3026 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3)); 3027 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3); 3028 } 3029 } 3030 } 3031 3032 bool SelectionDAGLegalize::ExpandNode(SDNode *Node) { 3033 SmallVector<SDValue, 8> Results; 3034 SDLoc dl(Node); 3035 SDValue Tmp1, Tmp2, Tmp3, Tmp4; 3036 bool NeedInvert; 3037 switch (Node->getOpcode()) { 3038 case ISD::CTPOP: 3039 case ISD::CTLZ: 3040 case ISD::CTLZ_ZERO_UNDEF: 3041 case ISD::CTTZ: 3042 case ISD::CTTZ_ZERO_UNDEF: 3043 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl); 3044 Results.push_back(Tmp1); 3045 break; 3046 case ISD::BITREVERSE: 3047 Results.push_back(ExpandBITREVERSE(Node->getOperand(0), dl)); 3048 break; 3049 case ISD::BSWAP: 3050 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl)); 3051 break; 3052 case ISD::FRAMEADDR: 3053 case ISD::RETURNADDR: 3054 case ISD::FRAME_TO_ARGS_OFFSET: 3055 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0))); 3056 break; 3057 case ISD::FLT_ROUNDS_: 3058 Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0))); 3059 break; 3060 case ISD::EH_RETURN: 3061 case ISD::EH_LABEL: 3062 case ISD::PREFETCH: 3063 case ISD::VAEND: 3064 case ISD::EH_SJLJ_LONGJMP: 3065 // If the target didn't expand these, there's nothing to do, so just 3066 // preserve the chain and be done. 3067 Results.push_back(Node->getOperand(0)); 3068 break; 3069 case ISD::READCYCLECOUNTER: 3070 // If the target didn't expand this, just return 'zero' and preserve the 3071 // chain. 3072 Results.append(Node->getNumValues() - 1, 3073 DAG.getConstant(0, dl, Node->getValueType(0))); 3074 Results.push_back(Node->getOperand(0)); 3075 break; 3076 case ISD::EH_SJLJ_SETJMP: 3077 // If the target didn't expand this, just return 'zero' and preserve the 3078 // chain. 3079 Results.push_back(DAG.getConstant(0, dl, MVT::i32)); 3080 Results.push_back(Node->getOperand(0)); 3081 break; 3082 case ISD::ATOMIC_LOAD: { 3083 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP. 3084 SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0)); 3085 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 3086 SDValue Swap = DAG.getAtomicCmpSwap( 3087 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs, 3088 Node->getOperand(0), Node->getOperand(1), Zero, Zero, 3089 cast<AtomicSDNode>(Node)->getMemOperand(), 3090 cast<AtomicSDNode>(Node)->getOrdering(), 3091 cast<AtomicSDNode>(Node)->getOrdering(), 3092 cast<AtomicSDNode>(Node)->getSynchScope()); 3093 Results.push_back(Swap.getValue(0)); 3094 Results.push_back(Swap.getValue(1)); 3095 break; 3096 } 3097 case ISD::ATOMIC_STORE: { 3098 // There is no libcall for atomic store; fake it with ATOMIC_SWAP. 3099 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl, 3100 cast<AtomicSDNode>(Node)->getMemoryVT(), 3101 Node->getOperand(0), 3102 Node->getOperand(1), Node->getOperand(2), 3103 cast<AtomicSDNode>(Node)->getMemOperand(), 3104 cast<AtomicSDNode>(Node)->getOrdering(), 3105 cast<AtomicSDNode>(Node)->getSynchScope()); 3106 Results.push_back(Swap.getValue(1)); 3107 break; 3108 } 3109 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: { 3110 // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and 3111 // splits out the success value as a comparison. Expanding the resulting 3112 // ATOMIC_CMP_SWAP will produce a libcall. 3113 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 3114 SDValue Res = DAG.getAtomicCmpSwap( 3115 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs, 3116 Node->getOperand(0), Node->getOperand(1), Node->getOperand(2), 3117 Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand(), 3118 cast<AtomicSDNode>(Node)->getSuccessOrdering(), 3119 cast<AtomicSDNode>(Node)->getFailureOrdering(), 3120 cast<AtomicSDNode>(Node)->getSynchScope()); 3121 3122 SDValue LHS = Res; 3123 SDValue RHS = Node->getOperand(1); 3124 3125 EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT(); 3126 EVT OuterType = Node->getValueType(0); 3127 if (TLI.hasSignExtendedAtomicOps()) { 3128 LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res, 3129 DAG.getValueType(AtomicType)); 3130 RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType, 3131 Node->getOperand(2), DAG.getValueType(AtomicType)); 3132 } else { 3133 LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res, DAG.getValueType(AtomicType)); 3134 RHS = DAG.getNode(ISD::ZERO_EXTEND, dl, OuterType, Node->getOperand(2)); 3135 } 3136 3137 SDValue Success = 3138 DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ); 3139 3140 Results.push_back(LHS.getValue(0)); 3141 Results.push_back(Success); 3142 Results.push_back(Res.getValue(1)); 3143 break; 3144 } 3145 case ISD::DYNAMIC_STACKALLOC: 3146 ExpandDYNAMIC_STACKALLOC(Node, Results); 3147 break; 3148 case ISD::MERGE_VALUES: 3149 for (unsigned i = 0; i < Node->getNumValues(); i++) 3150 Results.push_back(Node->getOperand(i)); 3151 break; 3152 case ISD::UNDEF: { 3153 EVT VT = Node->getValueType(0); 3154 if (VT.isInteger()) 3155 Results.push_back(DAG.getConstant(0, dl, VT)); 3156 else { 3157 assert(VT.isFloatingPoint() && "Unknown value type!"); 3158 Results.push_back(DAG.getConstantFP(0, dl, VT)); 3159 } 3160 break; 3161 } 3162 case ISD::FP_ROUND: 3163 case ISD::BITCAST: 3164 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), 3165 Node->getValueType(0), dl); 3166 Results.push_back(Tmp1); 3167 break; 3168 case ISD::FP_EXTEND: 3169 Tmp1 = EmitStackConvert(Node->getOperand(0), 3170 Node->getOperand(0).getValueType(), 3171 Node->getValueType(0), dl); 3172 Results.push_back(Tmp1); 3173 break; 3174 case ISD::SIGN_EXTEND_INREG: { 3175 // NOTE: we could fall back on load/store here too for targets without 3176 // SAR. However, it is doubtful that any exist. 3177 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 3178 EVT VT = Node->getValueType(0); 3179 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 3180 if (VT.isVector()) 3181 ShiftAmountTy = VT; 3182 unsigned BitsDiff = VT.getScalarType().getSizeInBits() - 3183 ExtraVT.getScalarType().getSizeInBits(); 3184 SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy); 3185 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0), 3186 Node->getOperand(0), ShiftCst); 3187 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst); 3188 Results.push_back(Tmp1); 3189 break; 3190 } 3191 case ISD::FP_ROUND_INREG: { 3192 // The only way we can lower this is to turn it into a TRUNCSTORE, 3193 // EXTLOAD pair, targeting a temporary location (a stack slot). 3194 3195 // NOTE: there is a choice here between constantly creating new stack 3196 // slots and always reusing the same one. We currently always create 3197 // new ones, as reuse may inhibit scheduling. 3198 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 3199 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT, 3200 Node->getValueType(0), dl); 3201 Results.push_back(Tmp1); 3202 break; 3203 } 3204 case ISD::SINT_TO_FP: 3205 case ISD::UINT_TO_FP: 3206 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP, 3207 Node->getOperand(0), Node->getValueType(0), dl); 3208 Results.push_back(Tmp1); 3209 break; 3210 case ISD::FP_TO_SINT: 3211 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) 3212 Results.push_back(Tmp1); 3213 break; 3214 case ISD::FP_TO_UINT: { 3215 SDValue True, False; 3216 EVT VT = Node->getOperand(0).getValueType(); 3217 EVT NVT = Node->getValueType(0); 3218 APFloat apf(DAG.EVTToAPFloatSemantics(VT), 3219 APInt::getNullValue(VT.getSizeInBits())); 3220 APInt x = APInt::getSignBit(NVT.getSizeInBits()); 3221 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven); 3222 Tmp1 = DAG.getConstantFP(apf, dl, VT); 3223 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(VT), 3224 Node->getOperand(0), 3225 Tmp1, ISD::SETLT); 3226 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0)); 3227 // TODO: Should any fast-math-flags be set for the FSUB? 3228 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, 3229 DAG.getNode(ISD::FSUB, dl, VT, 3230 Node->getOperand(0), Tmp1)); 3231 False = DAG.getNode(ISD::XOR, dl, NVT, False, 3232 DAG.getConstant(x, dl, NVT)); 3233 Tmp1 = DAG.getSelect(dl, NVT, Tmp2, True, False); 3234 Results.push_back(Tmp1); 3235 break; 3236 } 3237 case ISD::VAARG: 3238 Results.push_back(DAG.expandVAArg(Node)); 3239 Results.push_back(Results[0].getValue(1)); 3240 break; 3241 case ISD::VACOPY: 3242 Results.push_back(DAG.expandVACopy(Node)); 3243 break; 3244 case ISD::EXTRACT_VECTOR_ELT: 3245 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1) 3246 // This must be an access of the only element. Return it. 3247 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), 3248 Node->getOperand(0)); 3249 else 3250 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0)); 3251 Results.push_back(Tmp1); 3252 break; 3253 case ISD::EXTRACT_SUBVECTOR: 3254 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0))); 3255 break; 3256 case ISD::INSERT_SUBVECTOR: 3257 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0))); 3258 break; 3259 case ISD::CONCAT_VECTORS: { 3260 Results.push_back(ExpandVectorBuildThroughStack(Node)); 3261 break; 3262 } 3263 case ISD::SCALAR_TO_VECTOR: 3264 Results.push_back(ExpandSCALAR_TO_VECTOR(Node)); 3265 break; 3266 case ISD::INSERT_VECTOR_ELT: 3267 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0), 3268 Node->getOperand(1), 3269 Node->getOperand(2), dl)); 3270 break; 3271 case ISD::VECTOR_SHUFFLE: { 3272 SmallVector<int, 32> NewMask; 3273 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask(); 3274 3275 EVT VT = Node->getValueType(0); 3276 EVT EltVT = VT.getVectorElementType(); 3277 SDValue Op0 = Node->getOperand(0); 3278 SDValue Op1 = Node->getOperand(1); 3279 if (!TLI.isTypeLegal(EltVT)) { 3280 3281 EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT); 3282 3283 // BUILD_VECTOR operands are allowed to be wider than the element type. 3284 // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept 3285 // it. 3286 if (NewEltVT.bitsLT(EltVT)) { 3287 3288 // Convert shuffle node. 3289 // If original node was v4i64 and the new EltVT is i32, 3290 // cast operands to v8i32 and re-build the mask. 3291 3292 // Calculate new VT, the size of the new VT should be equal to original. 3293 EVT NewVT = 3294 EVT::getVectorVT(*DAG.getContext(), NewEltVT, 3295 VT.getSizeInBits() / NewEltVT.getSizeInBits()); 3296 assert(NewVT.bitsEq(VT)); 3297 3298 // cast operands to new VT 3299 Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0); 3300 Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1); 3301 3302 // Convert the shuffle mask 3303 unsigned int factor = 3304 NewVT.getVectorNumElements()/VT.getVectorNumElements(); 3305 3306 // EltVT gets smaller 3307 assert(factor > 0); 3308 3309 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) { 3310 if (Mask[i] < 0) { 3311 for (unsigned fi = 0; fi < factor; ++fi) 3312 NewMask.push_back(Mask[i]); 3313 } 3314 else { 3315 for (unsigned fi = 0; fi < factor; ++fi) 3316 NewMask.push_back(Mask[i]*factor+fi); 3317 } 3318 } 3319 Mask = NewMask; 3320 VT = NewVT; 3321 } 3322 EltVT = NewEltVT; 3323 } 3324 unsigned NumElems = VT.getVectorNumElements(); 3325 SmallVector<SDValue, 16> Ops; 3326 for (unsigned i = 0; i != NumElems; ++i) { 3327 if (Mask[i] < 0) { 3328 Ops.push_back(DAG.getUNDEF(EltVT)); 3329 continue; 3330 } 3331 unsigned Idx = Mask[i]; 3332 if (Idx < NumElems) 3333 Ops.push_back(DAG.getNode( 3334 ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0, 3335 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())))); 3336 else 3337 Ops.push_back(DAG.getNode( 3338 ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1, 3339 DAG.getConstant(Idx - NumElems, dl, 3340 TLI.getVectorIdxTy(DAG.getDataLayout())))); 3341 } 3342 3343 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops); 3344 // We may have changed the BUILD_VECTOR type. Cast it back to the Node type. 3345 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1); 3346 Results.push_back(Tmp1); 3347 break; 3348 } 3349 case ISD::EXTRACT_ELEMENT: { 3350 EVT OpTy = Node->getOperand(0).getValueType(); 3351 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) { 3352 // 1 -> Hi 3353 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0), 3354 DAG.getConstant(OpTy.getSizeInBits() / 2, dl, 3355 TLI.getShiftAmountTy( 3356 Node->getOperand(0).getValueType(), 3357 DAG.getDataLayout()))); 3358 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1); 3359 } else { 3360 // 0 -> Lo 3361 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), 3362 Node->getOperand(0)); 3363 } 3364 Results.push_back(Tmp1); 3365 break; 3366 } 3367 case ISD::STACKSAVE: 3368 // Expand to CopyFromReg if the target set 3369 // StackPointerRegisterToSaveRestore. 3370 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { 3371 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP, 3372 Node->getValueType(0))); 3373 Results.push_back(Results[0].getValue(1)); 3374 } else { 3375 Results.push_back(DAG.getUNDEF(Node->getValueType(0))); 3376 Results.push_back(Node->getOperand(0)); 3377 } 3378 break; 3379 case ISD::STACKRESTORE: 3380 // Expand to CopyToReg if the target set 3381 // StackPointerRegisterToSaveRestore. 3382 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { 3383 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP, 3384 Node->getOperand(1))); 3385 } else { 3386 Results.push_back(Node->getOperand(0)); 3387 } 3388 break; 3389 case ISD::GET_DYNAMIC_AREA_OFFSET: 3390 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0))); 3391 Results.push_back(Results[0].getValue(0)); 3392 break; 3393 case ISD::FCOPYSIGN: 3394 Results.push_back(ExpandFCOPYSIGN(Node)); 3395 break; 3396 case ISD::FNEG: 3397 // Expand Y = FNEG(X) -> Y = SUB -0.0, X 3398 Tmp1 = DAG.getConstantFP(-0.0, dl, Node->getValueType(0)); 3399 // TODO: If FNEG has fast-math-flags, propagate them to the FSUB. 3400 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1, 3401 Node->getOperand(0)); 3402 Results.push_back(Tmp1); 3403 break; 3404 case ISD::FABS: 3405 Results.push_back(ExpandFABS(Node)); 3406 break; 3407 case ISD::SMIN: 3408 case ISD::SMAX: 3409 case ISD::UMIN: 3410 case ISD::UMAX: { 3411 // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B 3412 ISD::CondCode Pred; 3413 switch (Node->getOpcode()) { 3414 default: llvm_unreachable("How did we get here?"); 3415 case ISD::SMAX: Pred = ISD::SETGT; break; 3416 case ISD::SMIN: Pred = ISD::SETLT; break; 3417 case ISD::UMAX: Pred = ISD::SETUGT; break; 3418 case ISD::UMIN: Pred = ISD::SETULT; break; 3419 } 3420 Tmp1 = Node->getOperand(0); 3421 Tmp2 = Node->getOperand(1); 3422 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred); 3423 Results.push_back(Tmp1); 3424 break; 3425 } 3426 3427 case ISD::FSIN: 3428 case ISD::FCOS: { 3429 EVT VT = Node->getValueType(0); 3430 // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin / 3431 // fcos which share the same operand and both are used. 3432 if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) || 3433 canCombineSinCosLibcall(Node, TLI, TM)) 3434 && useSinCos(Node)) { 3435 SDVTList VTs = DAG.getVTList(VT, VT); 3436 Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0)); 3437 if (Node->getOpcode() == ISD::FCOS) 3438 Tmp1 = Tmp1.getValue(1); 3439 Results.push_back(Tmp1); 3440 } 3441 break; 3442 } 3443 case ISD::FMAD: 3444 llvm_unreachable("Illegal fmad should never be formed"); 3445 3446 case ISD::FP16_TO_FP: 3447 if (Node->getValueType(0) != MVT::f32) { 3448 // We can extend to types bigger than f32 in two steps without changing 3449 // the result. Since "f16 -> f32" is much more commonly available, give 3450 // CodeGen the option of emitting that before resorting to a libcall. 3451 SDValue Res = 3452 DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0)); 3453 Results.push_back( 3454 DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res)); 3455 } 3456 break; 3457 case ISD::FP_TO_FP16: 3458 if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) { 3459 SDValue Op = Node->getOperand(0); 3460 MVT SVT = Op.getSimpleValueType(); 3461 if ((SVT == MVT::f64 || SVT == MVT::f80) && 3462 TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) { 3463 // Under fastmath, we can expand this node into a fround followed by 3464 // a float-half conversion. 3465 SDValue FloatVal = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op, 3466 DAG.getIntPtrConstant(0, dl)); 3467 Results.push_back( 3468 DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal)); 3469 } 3470 } 3471 break; 3472 case ISD::ConstantFP: { 3473 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); 3474 // Check to see if this FP immediate is already legal. 3475 // If this is a legal constant, turn it into a TargetConstantFP node. 3476 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0))) 3477 Results.push_back(ExpandConstantFP(CFP, true)); 3478 break; 3479 } 3480 case ISD::Constant: { 3481 ConstantSDNode *CP = cast<ConstantSDNode>(Node); 3482 Results.push_back(ExpandConstant(CP)); 3483 break; 3484 } 3485 case ISD::FSUB: { 3486 EVT VT = Node->getValueType(0); 3487 if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) && 3488 TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) { 3489 const SDNodeFlags *Flags = &cast<BinaryWithFlagsSDNode>(Node)->Flags; 3490 Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1)); 3491 Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags); 3492 Results.push_back(Tmp1); 3493 } 3494 break; 3495 } 3496 case ISD::SUB: { 3497 EVT VT = Node->getValueType(0); 3498 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) && 3499 TLI.isOperationLegalOrCustom(ISD::XOR, VT) && 3500 "Don't know how to expand this subtraction!"); 3501 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1), 3502 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl, 3503 VT)); 3504 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT)); 3505 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1)); 3506 break; 3507 } 3508 case ISD::UREM: 3509 case ISD::SREM: { 3510 EVT VT = Node->getValueType(0); 3511 bool isSigned = Node->getOpcode() == ISD::SREM; 3512 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV; 3513 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; 3514 Tmp2 = Node->getOperand(0); 3515 Tmp3 = Node->getOperand(1); 3516 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) { 3517 SDVTList VTs = DAG.getVTList(VT, VT); 3518 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1); 3519 Results.push_back(Tmp1); 3520 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) { 3521 // X % Y -> X-X/Y*Y 3522 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3); 3523 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3); 3524 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1); 3525 Results.push_back(Tmp1); 3526 } 3527 break; 3528 } 3529 case ISD::UDIV: 3530 case ISD::SDIV: { 3531 bool isSigned = Node->getOpcode() == ISD::SDIV; 3532 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; 3533 EVT VT = Node->getValueType(0); 3534 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) { 3535 SDVTList VTs = DAG.getVTList(VT, VT); 3536 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0), 3537 Node->getOperand(1)); 3538 Results.push_back(Tmp1); 3539 } 3540 break; 3541 } 3542 case ISD::MULHU: 3543 case ISD::MULHS: { 3544 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : 3545 ISD::SMUL_LOHI; 3546 EVT VT = Node->getValueType(0); 3547 SDVTList VTs = DAG.getVTList(VT, VT); 3548 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) && 3549 "If this wasn't legal, it shouldn't have been created!"); 3550 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0), 3551 Node->getOperand(1)); 3552 Results.push_back(Tmp1.getValue(1)); 3553 break; 3554 } 3555 case ISD::MUL: { 3556 EVT VT = Node->getValueType(0); 3557 SDVTList VTs = DAG.getVTList(VT, VT); 3558 // See if multiply or divide can be lowered using two-result operations. 3559 // We just need the low half of the multiply; try both the signed 3560 // and unsigned forms. If the target supports both SMUL_LOHI and 3561 // UMUL_LOHI, form a preference by checking which forms of plain 3562 // MULH it supports. 3563 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT); 3564 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT); 3565 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT); 3566 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT); 3567 unsigned OpToUse = 0; 3568 if (HasSMUL_LOHI && !HasMULHS) { 3569 OpToUse = ISD::SMUL_LOHI; 3570 } else if (HasUMUL_LOHI && !HasMULHU) { 3571 OpToUse = ISD::UMUL_LOHI; 3572 } else if (HasSMUL_LOHI) { 3573 OpToUse = ISD::SMUL_LOHI; 3574 } else if (HasUMUL_LOHI) { 3575 OpToUse = ISD::UMUL_LOHI; 3576 } 3577 if (OpToUse) { 3578 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0), 3579 Node->getOperand(1))); 3580 break; 3581 } 3582 3583 SDValue Lo, Hi; 3584 EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext()); 3585 if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) && 3586 TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) && 3587 TLI.isOperationLegalOrCustom(ISD::SHL, VT) && 3588 TLI.isOperationLegalOrCustom(ISD::OR, VT) && 3589 TLI.expandMUL(Node, Lo, Hi, HalfType, DAG)) { 3590 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo); 3591 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi); 3592 SDValue Shift = 3593 DAG.getConstant(HalfType.getSizeInBits(), dl, 3594 TLI.getShiftAmountTy(HalfType, DAG.getDataLayout())); 3595 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift); 3596 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi)); 3597 } 3598 break; 3599 } 3600 case ISD::SADDO: 3601 case ISD::SSUBO: { 3602 SDValue LHS = Node->getOperand(0); 3603 SDValue RHS = Node->getOperand(1); 3604 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ? 3605 ISD::ADD : ISD::SUB, dl, LHS.getValueType(), 3606 LHS, RHS); 3607 Results.push_back(Sum); 3608 EVT ResultType = Node->getValueType(1); 3609 EVT OType = getSetCCResultType(Node->getValueType(0)); 3610 3611 SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType()); 3612 3613 // LHSSign -> LHS >= 0 3614 // RHSSign -> RHS >= 0 3615 // SumSign -> Sum >= 0 3616 // 3617 // Add: 3618 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign) 3619 // Sub: 3620 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign) 3621 // 3622 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE); 3623 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE); 3624 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign, 3625 Node->getOpcode() == ISD::SADDO ? 3626 ISD::SETEQ : ISD::SETNE); 3627 3628 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE); 3629 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE); 3630 3631 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE); 3632 Results.push_back(DAG.getBoolExtOrTrunc(Cmp, dl, ResultType, ResultType)); 3633 break; 3634 } 3635 case ISD::UADDO: 3636 case ISD::USUBO: { 3637 SDValue LHS = Node->getOperand(0); 3638 SDValue RHS = Node->getOperand(1); 3639 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ? 3640 ISD::ADD : ISD::SUB, dl, LHS.getValueType(), 3641 LHS, RHS); 3642 Results.push_back(Sum); 3643 3644 EVT ResultType = Node->getValueType(1); 3645 EVT SetCCType = getSetCCResultType(Node->getValueType(0)); 3646 ISD::CondCode CC 3647 = Node->getOpcode() == ISD::UADDO ? ISD::SETULT : ISD::SETUGT; 3648 SDValue SetCC = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC); 3649 3650 Results.push_back(DAG.getBoolExtOrTrunc(SetCC, dl, ResultType, ResultType)); 3651 break; 3652 } 3653 case ISD::UMULO: 3654 case ISD::SMULO: { 3655 EVT VT = Node->getValueType(0); 3656 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2); 3657 SDValue LHS = Node->getOperand(0); 3658 SDValue RHS = Node->getOperand(1); 3659 SDValue BottomHalf; 3660 SDValue TopHalf; 3661 static const unsigned Ops[2][3] = 3662 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND }, 3663 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }}; 3664 bool isSigned = Node->getOpcode() == ISD::SMULO; 3665 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) { 3666 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS); 3667 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS); 3668 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) { 3669 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS, 3670 RHS); 3671 TopHalf = BottomHalf.getValue(1); 3672 } else if (TLI.isTypeLegal(WideVT)) { 3673 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS); 3674 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS); 3675 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS); 3676 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1, 3677 DAG.getIntPtrConstant(0, dl)); 3678 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1, 3679 DAG.getIntPtrConstant(1, dl)); 3680 } else { 3681 // We can fall back to a libcall with an illegal type for the MUL if we 3682 // have a libcall big enough. 3683 // Also, we can fall back to a division in some cases, but that's a big 3684 // performance hit in the general case. 3685 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 3686 if (WideVT == MVT::i16) 3687 LC = RTLIB::MUL_I16; 3688 else if (WideVT == MVT::i32) 3689 LC = RTLIB::MUL_I32; 3690 else if (WideVT == MVT::i64) 3691 LC = RTLIB::MUL_I64; 3692 else if (WideVT == MVT::i128) 3693 LC = RTLIB::MUL_I128; 3694 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!"); 3695 3696 // The high part is obtained by SRA'ing all but one of the bits of low 3697 // part. 3698 unsigned LoSize = VT.getSizeInBits(); 3699 SDValue HiLHS = 3700 DAG.getNode(ISD::SRA, dl, VT, RHS, 3701 DAG.getConstant(LoSize - 1, dl, 3702 TLI.getPointerTy(DAG.getDataLayout()))); 3703 SDValue HiRHS = 3704 DAG.getNode(ISD::SRA, dl, VT, LHS, 3705 DAG.getConstant(LoSize - 1, dl, 3706 TLI.getPointerTy(DAG.getDataLayout()))); 3707 3708 // Here we're passing the 2 arguments explicitly as 4 arguments that are 3709 // pre-lowered to the correct types. This all depends upon WideVT not 3710 // being a legal type for the architecture and thus has to be split to 3711 // two arguments. 3712 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS }; 3713 SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl); 3714 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret, 3715 DAG.getIntPtrConstant(0, dl)); 3716 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret, 3717 DAG.getIntPtrConstant(1, dl)); 3718 // Ret is a node with an illegal type. Because such things are not 3719 // generally permitted during this phase of legalization, make sure the 3720 // node has no more uses. The above EXTRACT_ELEMENT nodes should have been 3721 // folded. 3722 assert(Ret->use_empty() && 3723 "Unexpected uses of illegally type from expanded lib call."); 3724 } 3725 3726 if (isSigned) { 3727 Tmp1 = DAG.getConstant( 3728 VT.getSizeInBits() - 1, dl, 3729 TLI.getShiftAmountTy(BottomHalf.getValueType(), DAG.getDataLayout())); 3730 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1); 3731 TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf, Tmp1, 3732 ISD::SETNE); 3733 } else { 3734 TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf, 3735 DAG.getConstant(0, dl, VT), ISD::SETNE); 3736 } 3737 Results.push_back(BottomHalf); 3738 Results.push_back(TopHalf); 3739 break; 3740 } 3741 case ISD::BUILD_PAIR: { 3742 EVT PairTy = Node->getValueType(0); 3743 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0)); 3744 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1)); 3745 Tmp2 = DAG.getNode( 3746 ISD::SHL, dl, PairTy, Tmp2, 3747 DAG.getConstant(PairTy.getSizeInBits() / 2, dl, 3748 TLI.getShiftAmountTy(PairTy, DAG.getDataLayout()))); 3749 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2)); 3750 break; 3751 } 3752 case ISD::SELECT: 3753 Tmp1 = Node->getOperand(0); 3754 Tmp2 = Node->getOperand(1); 3755 Tmp3 = Node->getOperand(2); 3756 if (Tmp1.getOpcode() == ISD::SETCC) { 3757 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1), 3758 Tmp2, Tmp3, 3759 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); 3760 } else { 3761 Tmp1 = DAG.getSelectCC(dl, Tmp1, 3762 DAG.getConstant(0, dl, Tmp1.getValueType()), 3763 Tmp2, Tmp3, ISD::SETNE); 3764 } 3765 Results.push_back(Tmp1); 3766 break; 3767 case ISD::BR_JT: { 3768 SDValue Chain = Node->getOperand(0); 3769 SDValue Table = Node->getOperand(1); 3770 SDValue Index = Node->getOperand(2); 3771 3772 EVT PTy = TLI.getPointerTy(DAG.getDataLayout()); 3773 3774 const DataLayout &TD = DAG.getDataLayout(); 3775 unsigned EntrySize = 3776 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD); 3777 3778 Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index, 3779 DAG.getConstant(EntrySize, dl, Index.getValueType())); 3780 SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(), 3781 Index, Table); 3782 3783 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8); 3784 SDValue LD = DAG.getExtLoad( 3785 ISD::SEXTLOAD, dl, PTy, Chain, Addr, 3786 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT, 3787 false, false, false, 0); 3788 Addr = LD; 3789 if (TM.getRelocationModel() == Reloc::PIC_) { 3790 // For PIC, the sequence is: 3791 // BRIND(load(Jumptable + index) + RelocBase) 3792 // RelocBase can be JumpTable, GOT or some sort of global base. 3793 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, 3794 TLI.getPICJumpTableRelocBase(Table, DAG)); 3795 } 3796 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr); 3797 Results.push_back(Tmp1); 3798 break; 3799 } 3800 case ISD::BRCOND: 3801 // Expand brcond's setcc into its constituent parts and create a BR_CC 3802 // Node. 3803 Tmp1 = Node->getOperand(0); 3804 Tmp2 = Node->getOperand(1); 3805 if (Tmp2.getOpcode() == ISD::SETCC) { 3806 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, 3807 Tmp1, Tmp2.getOperand(2), 3808 Tmp2.getOperand(0), Tmp2.getOperand(1), 3809 Node->getOperand(2)); 3810 } else { 3811 // We test only the i1 bit. Skip the AND if UNDEF. 3812 Tmp3 = (Tmp2.isUndef()) ? Tmp2 : 3813 DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2, 3814 DAG.getConstant(1, dl, Tmp2.getValueType())); 3815 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, 3816 DAG.getCondCode(ISD::SETNE), Tmp3, 3817 DAG.getConstant(0, dl, Tmp3.getValueType()), 3818 Node->getOperand(2)); 3819 } 3820 Results.push_back(Tmp1); 3821 break; 3822 case ISD::SETCC: { 3823 Tmp1 = Node->getOperand(0); 3824 Tmp2 = Node->getOperand(1); 3825 Tmp3 = Node->getOperand(2); 3826 bool Legalized = LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, 3827 Tmp3, NeedInvert, dl); 3828 3829 if (Legalized) { 3830 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the 3831 // condition code, create a new SETCC node. 3832 if (Tmp3.getNode()) 3833 Tmp1 = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), 3834 Tmp1, Tmp2, Tmp3); 3835 3836 // If we expanded the SETCC by inverting the condition code, then wrap 3837 // the existing SETCC in a NOT to restore the intended condition. 3838 if (NeedInvert) 3839 Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0)); 3840 3841 Results.push_back(Tmp1); 3842 break; 3843 } 3844 3845 // Otherwise, SETCC for the given comparison type must be completely 3846 // illegal; expand it into a SELECT_CC. 3847 EVT VT = Node->getValueType(0); 3848 int TrueValue; 3849 switch (TLI.getBooleanContents(Tmp1->getValueType(0))) { 3850 case TargetLowering::ZeroOrOneBooleanContent: 3851 case TargetLowering::UndefinedBooleanContent: 3852 TrueValue = 1; 3853 break; 3854 case TargetLowering::ZeroOrNegativeOneBooleanContent: 3855 TrueValue = -1; 3856 break; 3857 } 3858 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2, 3859 DAG.getConstant(TrueValue, dl, VT), 3860 DAG.getConstant(0, dl, VT), 3861 Tmp3); 3862 Results.push_back(Tmp1); 3863 break; 3864 } 3865 case ISD::SELECT_CC: { 3866 Tmp1 = Node->getOperand(0); // LHS 3867 Tmp2 = Node->getOperand(1); // RHS 3868 Tmp3 = Node->getOperand(2); // True 3869 Tmp4 = Node->getOperand(3); // False 3870 EVT VT = Node->getValueType(0); 3871 SDValue CC = Node->getOperand(4); 3872 ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get(); 3873 3874 if (TLI.isCondCodeLegal(CCOp, Tmp1.getSimpleValueType())) { 3875 // If the condition code is legal, then we need to expand this 3876 // node using SETCC and SELECT. 3877 EVT CmpVT = Tmp1.getValueType(); 3878 assert(!TLI.isOperationExpand(ISD::SELECT, VT) && 3879 "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be " 3880 "expanded."); 3881 EVT CCVT = 3882 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), CmpVT); 3883 SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC); 3884 Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4)); 3885 break; 3886 } 3887 3888 // SELECT_CC is legal, so the condition code must not be. 3889 bool Legalized = false; 3890 // Try to legalize by inverting the condition. This is for targets that 3891 // might support an ordered version of a condition, but not the unordered 3892 // version (or vice versa). 3893 ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, 3894 Tmp1.getValueType().isInteger()); 3895 if (TLI.isCondCodeLegal(InvCC, Tmp1.getSimpleValueType())) { 3896 // Use the new condition code and swap true and false 3897 Legalized = true; 3898 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC); 3899 } else { 3900 // If The inverse is not legal, then try to swap the arguments using 3901 // the inverse condition code. 3902 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC); 3903 if (TLI.isCondCodeLegal(SwapInvCC, Tmp1.getSimpleValueType())) { 3904 // The swapped inverse condition is legal, so swap true and false, 3905 // lhs and rhs. 3906 Legalized = true; 3907 Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC); 3908 } 3909 } 3910 3911 if (!Legalized) { 3912 Legalized = LegalizeSetCCCondCode( 3913 getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC, NeedInvert, 3914 dl); 3915 3916 assert(Legalized && "Can't legalize SELECT_CC with legal condition!"); 3917 3918 // If we expanded the SETCC by inverting the condition code, then swap 3919 // the True/False operands to match. 3920 if (NeedInvert) 3921 std::swap(Tmp3, Tmp4); 3922 3923 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the 3924 // condition code, create a new SELECT_CC node. 3925 if (CC.getNode()) { 3926 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), 3927 Tmp1, Tmp2, Tmp3, Tmp4, CC); 3928 } else { 3929 Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType()); 3930 CC = DAG.getCondCode(ISD::SETNE); 3931 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, 3932 Tmp2, Tmp3, Tmp4, CC); 3933 } 3934 } 3935 Results.push_back(Tmp1); 3936 break; 3937 } 3938 case ISD::BR_CC: { 3939 Tmp1 = Node->getOperand(0); // Chain 3940 Tmp2 = Node->getOperand(2); // LHS 3941 Tmp3 = Node->getOperand(3); // RHS 3942 Tmp4 = Node->getOperand(1); // CC 3943 3944 bool Legalized = LegalizeSetCCCondCode(getSetCCResultType( 3945 Tmp2.getValueType()), Tmp2, Tmp3, Tmp4, NeedInvert, dl); 3946 (void)Legalized; 3947 assert(Legalized && "Can't legalize BR_CC with legal condition!"); 3948 3949 // If we expanded the SETCC by inverting the condition code, then wrap 3950 // the existing SETCC in a NOT to restore the intended condition. 3951 if (NeedInvert) 3952 Tmp4 = DAG.getNOT(dl, Tmp4, Tmp4->getValueType(0)); 3953 3954 // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC 3955 // node. 3956 if (Tmp4.getNode()) { 3957 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, 3958 Tmp4, Tmp2, Tmp3, Node->getOperand(4)); 3959 } else { 3960 Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType()); 3961 Tmp4 = DAG.getCondCode(ISD::SETNE); 3962 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, 3963 Tmp2, Tmp3, Node->getOperand(4)); 3964 } 3965 Results.push_back(Tmp1); 3966 break; 3967 } 3968 case ISD::BUILD_VECTOR: 3969 Results.push_back(ExpandBUILD_VECTOR(Node)); 3970 break; 3971 case ISD::SRA: 3972 case ISD::SRL: 3973 case ISD::SHL: { 3974 // Scalarize vector SRA/SRL/SHL. 3975 EVT VT = Node->getValueType(0); 3976 assert(VT.isVector() && "Unable to legalize non-vector shift"); 3977 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal"); 3978 unsigned NumElem = VT.getVectorNumElements(); 3979 3980 SmallVector<SDValue, 8> Scalars; 3981 for (unsigned Idx = 0; Idx < NumElem; Idx++) { 3982 SDValue Ex = DAG.getNode( 3983 ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(0), 3984 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))); 3985 SDValue Sh = DAG.getNode( 3986 ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(1), 3987 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))); 3988 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl, 3989 VT.getScalarType(), Ex, Sh)); 3990 } 3991 SDValue Result = 3992 DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0), Scalars); 3993 ReplaceNode(SDValue(Node, 0), Result); 3994 break; 3995 } 3996 case ISD::GLOBAL_OFFSET_TABLE: 3997 case ISD::GlobalAddress: 3998 case ISD::GlobalTLSAddress: 3999 case ISD::ExternalSymbol: 4000 case ISD::ConstantPool: 4001 case ISD::JumpTable: 4002 case ISD::INTRINSIC_W_CHAIN: 4003 case ISD::INTRINSIC_WO_CHAIN: 4004 case ISD::INTRINSIC_VOID: 4005 // FIXME: Custom lowering for these operations shouldn't return null! 4006 break; 4007 } 4008 4009 // Replace the original node with the legalized result. 4010 if (Results.empty()) 4011 return false; 4012 4013 ReplaceNode(Node, Results.data()); 4014 return true; 4015 } 4016 4017 void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) { 4018 SmallVector<SDValue, 8> Results; 4019 SDLoc dl(Node); 4020 SDValue Tmp1, Tmp2, Tmp3, Tmp4; 4021 unsigned Opc = Node->getOpcode(); 4022 switch (Opc) { 4023 case ISD::ATOMIC_FENCE: { 4024 // If the target didn't lower this, lower it to '__sync_synchronize()' call 4025 // FIXME: handle "fence singlethread" more efficiently. 4026 TargetLowering::ArgListTy Args; 4027 4028 TargetLowering::CallLoweringInfo CLI(DAG); 4029 CLI.setDebugLoc(dl) 4030 .setChain(Node->getOperand(0)) 4031 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), 4032 DAG.getExternalSymbol("__sync_synchronize", 4033 TLI.getPointerTy(DAG.getDataLayout())), 4034 std::move(Args), 0); 4035 4036 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI); 4037 4038 Results.push_back(CallResult.second); 4039 break; 4040 } 4041 // By default, atomic intrinsics are marked Legal and lowered. Targets 4042 // which don't support them directly, however, may want libcalls, in which 4043 // case they mark them Expand, and we get here. 4044 case ISD::ATOMIC_SWAP: 4045 case ISD::ATOMIC_LOAD_ADD: 4046 case ISD::ATOMIC_LOAD_SUB: 4047 case ISD::ATOMIC_LOAD_AND: 4048 case ISD::ATOMIC_LOAD_OR: 4049 case ISD::ATOMIC_LOAD_XOR: 4050 case ISD::ATOMIC_LOAD_NAND: 4051 case ISD::ATOMIC_LOAD_MIN: 4052 case ISD::ATOMIC_LOAD_MAX: 4053 case ISD::ATOMIC_LOAD_UMIN: 4054 case ISD::ATOMIC_LOAD_UMAX: 4055 case ISD::ATOMIC_CMP_SWAP: { 4056 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT(); 4057 RTLIB::Libcall LC = RTLIB::getSYNC(Opc, VT); 4058 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected atomic op or value type!"); 4059 4060 std::pair<SDValue, SDValue> Tmp = ExpandChainLibCall(LC, Node, false); 4061 Results.push_back(Tmp.first); 4062 Results.push_back(Tmp.second); 4063 break; 4064 } 4065 case ISD::TRAP: { 4066 // If this operation is not supported, lower it to 'abort()' call 4067 TargetLowering::ArgListTy Args; 4068 TargetLowering::CallLoweringInfo CLI(DAG); 4069 CLI.setDebugLoc(dl) 4070 .setChain(Node->getOperand(0)) 4071 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), 4072 DAG.getExternalSymbol("abort", 4073 TLI.getPointerTy(DAG.getDataLayout())), 4074 std::move(Args), 0); 4075 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI); 4076 4077 Results.push_back(CallResult.second); 4078 break; 4079 } 4080 case ISD::FMINNUM: 4081 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64, 4082 RTLIB::FMIN_F80, RTLIB::FMIN_F128, 4083 RTLIB::FMIN_PPCF128)); 4084 break; 4085 case ISD::FMAXNUM: 4086 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64, 4087 RTLIB::FMAX_F80, RTLIB::FMAX_F128, 4088 RTLIB::FMAX_PPCF128)); 4089 break; 4090 case ISD::FSQRT: 4091 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64, 4092 RTLIB::SQRT_F80, RTLIB::SQRT_F128, 4093 RTLIB::SQRT_PPCF128)); 4094 break; 4095 case ISD::FSIN: 4096 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64, 4097 RTLIB::SIN_F80, RTLIB::SIN_F128, 4098 RTLIB::SIN_PPCF128)); 4099 break; 4100 case ISD::FCOS: 4101 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64, 4102 RTLIB::COS_F80, RTLIB::COS_F128, 4103 RTLIB::COS_PPCF128)); 4104 break; 4105 case ISD::FSINCOS: 4106 // Expand into sincos libcall. 4107 ExpandSinCosLibCall(Node, Results); 4108 break; 4109 case ISD::FLOG: 4110 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, 4111 RTLIB::LOG_F80, RTLIB::LOG_F128, 4112 RTLIB::LOG_PPCF128)); 4113 break; 4114 case ISD::FLOG2: 4115 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, 4116 RTLIB::LOG2_F80, RTLIB::LOG2_F128, 4117 RTLIB::LOG2_PPCF128)); 4118 break; 4119 case ISD::FLOG10: 4120 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, 4121 RTLIB::LOG10_F80, RTLIB::LOG10_F128, 4122 RTLIB::LOG10_PPCF128)); 4123 break; 4124 case ISD::FEXP: 4125 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, 4126 RTLIB::EXP_F80, RTLIB::EXP_F128, 4127 RTLIB::EXP_PPCF128)); 4128 break; 4129 case ISD::FEXP2: 4130 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, 4131 RTLIB::EXP2_F80, RTLIB::EXP2_F128, 4132 RTLIB::EXP2_PPCF128)); 4133 break; 4134 case ISD::FTRUNC: 4135 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64, 4136 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128, 4137 RTLIB::TRUNC_PPCF128)); 4138 break; 4139 case ISD::FFLOOR: 4140 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64, 4141 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128, 4142 RTLIB::FLOOR_PPCF128)); 4143 break; 4144 case ISD::FCEIL: 4145 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64, 4146 RTLIB::CEIL_F80, RTLIB::CEIL_F128, 4147 RTLIB::CEIL_PPCF128)); 4148 break; 4149 case ISD::FRINT: 4150 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64, 4151 RTLIB::RINT_F80, RTLIB::RINT_F128, 4152 RTLIB::RINT_PPCF128)); 4153 break; 4154 case ISD::FNEARBYINT: 4155 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32, 4156 RTLIB::NEARBYINT_F64, 4157 RTLIB::NEARBYINT_F80, 4158 RTLIB::NEARBYINT_F128, 4159 RTLIB::NEARBYINT_PPCF128)); 4160 break; 4161 case ISD::FROUND: 4162 Results.push_back(ExpandFPLibCall(Node, RTLIB::ROUND_F32, 4163 RTLIB::ROUND_F64, 4164 RTLIB::ROUND_F80, 4165 RTLIB::ROUND_F128, 4166 RTLIB::ROUND_PPCF128)); 4167 break; 4168 case ISD::FPOWI: 4169 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64, 4170 RTLIB::POWI_F80, RTLIB::POWI_F128, 4171 RTLIB::POWI_PPCF128)); 4172 break; 4173 case ISD::FPOW: 4174 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, 4175 RTLIB::POW_F80, RTLIB::POW_F128, 4176 RTLIB::POW_PPCF128)); 4177 break; 4178 case ISD::FDIV: 4179 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64, 4180 RTLIB::DIV_F80, RTLIB::DIV_F128, 4181 RTLIB::DIV_PPCF128)); 4182 break; 4183 case ISD::FREM: 4184 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64, 4185 RTLIB::REM_F80, RTLIB::REM_F128, 4186 RTLIB::REM_PPCF128)); 4187 break; 4188 case ISD::FMA: 4189 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64, 4190 RTLIB::FMA_F80, RTLIB::FMA_F128, 4191 RTLIB::FMA_PPCF128)); 4192 break; 4193 case ISD::FADD: 4194 Results.push_back(ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64, 4195 RTLIB::ADD_F80, RTLIB::ADD_F128, 4196 RTLIB::ADD_PPCF128)); 4197 break; 4198 case ISD::FMUL: 4199 Results.push_back(ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64, 4200 RTLIB::MUL_F80, RTLIB::MUL_F128, 4201 RTLIB::MUL_PPCF128)); 4202 break; 4203 case ISD::FP16_TO_FP: 4204 if (Node->getValueType(0) == MVT::f32) { 4205 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false)); 4206 } 4207 break; 4208 case ISD::FP_TO_FP16: { 4209 RTLIB::Libcall LC = 4210 RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16); 4211 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16"); 4212 Results.push_back(ExpandLibCall(LC, Node, false)); 4213 break; 4214 } 4215 case ISD::FSUB: 4216 Results.push_back(ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64, 4217 RTLIB::SUB_F80, RTLIB::SUB_F128, 4218 RTLIB::SUB_PPCF128)); 4219 break; 4220 case ISD::SREM: 4221 Results.push_back(ExpandIntLibCall(Node, true, 4222 RTLIB::SREM_I8, 4223 RTLIB::SREM_I16, RTLIB::SREM_I32, 4224 RTLIB::SREM_I64, RTLIB::SREM_I128)); 4225 break; 4226 case ISD::UREM: 4227 Results.push_back(ExpandIntLibCall(Node, false, 4228 RTLIB::UREM_I8, 4229 RTLIB::UREM_I16, RTLIB::UREM_I32, 4230 RTLIB::UREM_I64, RTLIB::UREM_I128)); 4231 break; 4232 case ISD::SDIV: 4233 Results.push_back(ExpandIntLibCall(Node, true, 4234 RTLIB::SDIV_I8, 4235 RTLIB::SDIV_I16, RTLIB::SDIV_I32, 4236 RTLIB::SDIV_I64, RTLIB::SDIV_I128)); 4237 break; 4238 case ISD::UDIV: 4239 Results.push_back(ExpandIntLibCall(Node, false, 4240 RTLIB::UDIV_I8, 4241 RTLIB::UDIV_I16, RTLIB::UDIV_I32, 4242 RTLIB::UDIV_I64, RTLIB::UDIV_I128)); 4243 break; 4244 case ISD::SDIVREM: 4245 case ISD::UDIVREM: 4246 // Expand into divrem libcall 4247 ExpandDivRemLibCall(Node, Results); 4248 break; 4249 case ISD::MUL: 4250 Results.push_back(ExpandIntLibCall(Node, false, 4251 RTLIB::MUL_I8, 4252 RTLIB::MUL_I16, RTLIB::MUL_I32, 4253 RTLIB::MUL_I64, RTLIB::MUL_I128)); 4254 break; 4255 } 4256 4257 // Replace the original node with the legalized result. 4258 if (!Results.empty()) 4259 ReplaceNode(Node, Results.data()); 4260 } 4261 4262 // Determine the vector type to use in place of an original scalar element when 4263 // promoting equally sized vectors. 4264 static MVT getPromotedVectorElementType(const TargetLowering &TLI, 4265 MVT EltVT, MVT NewEltVT) { 4266 unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits(); 4267 MVT MidVT = MVT::getVectorVT(NewEltVT, OldEltsPerNewElt); 4268 assert(TLI.isTypeLegal(MidVT) && "unexpected"); 4269 return MidVT; 4270 } 4271 4272 void SelectionDAGLegalize::PromoteNode(SDNode *Node) { 4273 SmallVector<SDValue, 8> Results; 4274 MVT OVT = Node->getSimpleValueType(0); 4275 if (Node->getOpcode() == ISD::UINT_TO_FP || 4276 Node->getOpcode() == ISD::SINT_TO_FP || 4277 Node->getOpcode() == ISD::SETCC || 4278 Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT || 4279 Node->getOpcode() == ISD::INSERT_VECTOR_ELT) { 4280 OVT = Node->getOperand(0).getSimpleValueType(); 4281 } 4282 if (Node->getOpcode() == ISD::BR_CC) 4283 OVT = Node->getOperand(2).getSimpleValueType(); 4284 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); 4285 SDLoc dl(Node); 4286 SDValue Tmp1, Tmp2, Tmp3; 4287 switch (Node->getOpcode()) { 4288 case ISD::CTTZ: 4289 case ISD::CTTZ_ZERO_UNDEF: 4290 case ISD::CTLZ: 4291 case ISD::CTLZ_ZERO_UNDEF: 4292 case ISD::CTPOP: 4293 // Zero extend the argument. 4294 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 4295 // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is 4296 // already the correct result. 4297 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 4298 if (Node->getOpcode() == ISD::CTTZ) { 4299 // FIXME: This should set a bit in the zero extended value instead. 4300 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT), 4301 Tmp1, DAG.getConstant(NVT.getSizeInBits(), dl, NVT), 4302 ISD::SETEQ); 4303 Tmp1 = DAG.getSelect(dl, NVT, Tmp2, 4304 DAG.getConstant(OVT.getSizeInBits(), dl, NVT), Tmp1); 4305 } else if (Node->getOpcode() == ISD::CTLZ || 4306 Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) { 4307 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) 4308 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1, 4309 DAG.getConstant(NVT.getSizeInBits() - 4310 OVT.getSizeInBits(), dl, NVT)); 4311 } 4312 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 4313 break; 4314 case ISD::BSWAP: { 4315 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits(); 4316 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 4317 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1); 4318 Tmp1 = DAG.getNode( 4319 ISD::SRL, dl, NVT, Tmp1, 4320 DAG.getConstant(DiffBits, dl, 4321 TLI.getShiftAmountTy(NVT, DAG.getDataLayout()))); 4322 Results.push_back(Tmp1); 4323 break; 4324 } 4325 case ISD::FP_TO_UINT: 4326 case ISD::FP_TO_SINT: 4327 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0), 4328 Node->getOpcode() == ISD::FP_TO_SINT, dl); 4329 Results.push_back(Tmp1); 4330 break; 4331 case ISD::UINT_TO_FP: 4332 case ISD::SINT_TO_FP: 4333 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0), 4334 Node->getOpcode() == ISD::SINT_TO_FP, dl); 4335 Results.push_back(Tmp1); 4336 break; 4337 case ISD::VAARG: { 4338 SDValue Chain = Node->getOperand(0); // Get the chain. 4339 SDValue Ptr = Node->getOperand(1); // Get the pointer. 4340 4341 unsigned TruncOp; 4342 if (OVT.isVector()) { 4343 TruncOp = ISD::BITCAST; 4344 } else { 4345 assert(OVT.isInteger() 4346 && "VAARG promotion is supported only for vectors or integer types"); 4347 TruncOp = ISD::TRUNCATE; 4348 } 4349 4350 // Perform the larger operation, then convert back 4351 Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2), 4352 Node->getConstantOperandVal(3)); 4353 Chain = Tmp1.getValue(1); 4354 4355 Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1); 4356 4357 // Modified the chain result - switch anything that used the old chain to 4358 // use the new one. 4359 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2); 4360 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain); 4361 if (UpdatedNodes) { 4362 UpdatedNodes->insert(Tmp2.getNode()); 4363 UpdatedNodes->insert(Chain.getNode()); 4364 } 4365 ReplacedNode(Node); 4366 break; 4367 } 4368 case ISD::AND: 4369 case ISD::OR: 4370 case ISD::XOR: { 4371 unsigned ExtOp, TruncOp; 4372 if (OVT.isVector()) { 4373 ExtOp = ISD::BITCAST; 4374 TruncOp = ISD::BITCAST; 4375 } else { 4376 assert(OVT.isInteger() && "Cannot promote logic operation"); 4377 ExtOp = ISD::ANY_EXTEND; 4378 TruncOp = ISD::TRUNCATE; 4379 } 4380 // Promote each of the values to the new type. 4381 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 4382 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 4383 // Perform the larger operation, then convert back 4384 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); 4385 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1)); 4386 break; 4387 } 4388 case ISD::SELECT: { 4389 unsigned ExtOp, TruncOp; 4390 if (Node->getValueType(0).isVector() || 4391 Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) { 4392 ExtOp = ISD::BITCAST; 4393 TruncOp = ISD::BITCAST; 4394 } else if (Node->getValueType(0).isInteger()) { 4395 ExtOp = ISD::ANY_EXTEND; 4396 TruncOp = ISD::TRUNCATE; 4397 } else { 4398 ExtOp = ISD::FP_EXTEND; 4399 TruncOp = ISD::FP_ROUND; 4400 } 4401 Tmp1 = Node->getOperand(0); 4402 // Promote each of the values to the new type. 4403 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 4404 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 4405 // Perform the larger operation, then round down. 4406 Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3); 4407 if (TruncOp != ISD::FP_ROUND) 4408 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1); 4409 else 4410 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1, 4411 DAG.getIntPtrConstant(0, dl)); 4412 Results.push_back(Tmp1); 4413 break; 4414 } 4415 case ISD::VECTOR_SHUFFLE: { 4416 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask(); 4417 4418 // Cast the two input vectors. 4419 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0)); 4420 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1)); 4421 4422 // Convert the shuffle mask to the right # elements. 4423 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask); 4424 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1); 4425 Results.push_back(Tmp1); 4426 break; 4427 } 4428 case ISD::SETCC: { 4429 unsigned ExtOp = ISD::FP_EXTEND; 4430 if (NVT.isInteger()) { 4431 ISD::CondCode CCCode = 4432 cast<CondCodeSDNode>(Node->getOperand(2))->get(); 4433 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 4434 } 4435 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 4436 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 4437 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), 4438 Tmp1, Tmp2, Node->getOperand(2))); 4439 break; 4440 } 4441 case ISD::BR_CC: { 4442 unsigned ExtOp = ISD::FP_EXTEND; 4443 if (NVT.isInteger()) { 4444 ISD::CondCode CCCode = 4445 cast<CondCodeSDNode>(Node->getOperand(1))->get(); 4446 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 4447 } 4448 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 4449 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3)); 4450 Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), 4451 Node->getOperand(0), Node->getOperand(1), 4452 Tmp1, Tmp2, Node->getOperand(4))); 4453 break; 4454 } 4455 case ISD::FADD: 4456 case ISD::FSUB: 4457 case ISD::FMUL: 4458 case ISD::FDIV: 4459 case ISD::FREM: 4460 case ISD::FMINNUM: 4461 case ISD::FMAXNUM: 4462 case ISD::FPOW: { 4463 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 4464 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1)); 4465 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, 4466 Node->getFlags()); 4467 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 4468 Tmp3, DAG.getIntPtrConstant(0, dl))); 4469 break; 4470 } 4471 case ISD::FMA: { 4472 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 4473 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1)); 4474 Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2)); 4475 Results.push_back( 4476 DAG.getNode(ISD::FP_ROUND, dl, OVT, 4477 DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3), 4478 DAG.getIntPtrConstant(0, dl))); 4479 break; 4480 } 4481 case ISD::FCOPYSIGN: 4482 case ISD::FPOWI: { 4483 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 4484 Tmp2 = Node->getOperand(1); 4485 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); 4486 4487 // fcopysign doesn't change anything but the sign bit, so 4488 // (fp_round (fcopysign (fpext a), b)) 4489 // is as precise as 4490 // (fp_round (fpext a)) 4491 // which is a no-op. Mark it as a TRUNCating FP_ROUND. 4492 const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN); 4493 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 4494 Tmp3, DAG.getIntPtrConstant(isTrunc, dl))); 4495 break; 4496 } 4497 case ISD::FFLOOR: 4498 case ISD::FCEIL: 4499 case ISD::FRINT: 4500 case ISD::FNEARBYINT: 4501 case ISD::FROUND: 4502 case ISD::FTRUNC: 4503 case ISD::FNEG: 4504 case ISD::FSQRT: 4505 case ISD::FSIN: 4506 case ISD::FCOS: 4507 case ISD::FLOG: 4508 case ISD::FLOG2: 4509 case ISD::FLOG10: 4510 case ISD::FABS: 4511 case ISD::FEXP: 4512 case ISD::FEXP2: { 4513 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 4514 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 4515 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 4516 Tmp2, DAG.getIntPtrConstant(0, dl))); 4517 break; 4518 } 4519 case ISD::BUILD_VECTOR: { 4520 MVT EltVT = OVT.getVectorElementType(); 4521 MVT NewEltVT = NVT.getVectorElementType(); 4522 4523 // Handle bitcasts to a different vector type with the same total bit size 4524 // 4525 // e.g. v2i64 = build_vector i64:x, i64:y => v4i32 4526 // => 4527 // v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y)) 4528 4529 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 4530 "Invalid promote type for build_vector"); 4531 assert(NewEltVT.bitsLT(EltVT) && "not handled"); 4532 4533 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 4534 4535 SmallVector<SDValue, 8> NewOps; 4536 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) { 4537 SDValue Op = Node->getOperand(I); 4538 NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op)); 4539 } 4540 4541 SDLoc SL(Node); 4542 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewOps); 4543 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat); 4544 Results.push_back(CvtVec); 4545 break; 4546 } 4547 case ISD::EXTRACT_VECTOR_ELT: { 4548 MVT EltVT = OVT.getVectorElementType(); 4549 MVT NewEltVT = NVT.getVectorElementType(); 4550 4551 // Handle bitcasts to a different vector type with the same total bit size. 4552 // 4553 // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32 4554 // => 4555 // v4i32:castx = bitcast x:v2i64 4556 // 4557 // i64 = bitcast 4558 // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))), 4559 // (i32 (extract_vector_elt castx, (2 * y + 1))) 4560 // 4561 4562 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 4563 "Invalid promote type for extract_vector_elt"); 4564 assert(NewEltVT.bitsLT(EltVT) && "not handled"); 4565 4566 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 4567 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements(); 4568 4569 SDValue Idx = Node->getOperand(1); 4570 EVT IdxVT = Idx.getValueType(); 4571 SDLoc SL(Node); 4572 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT); 4573 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor); 4574 4575 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0)); 4576 4577 SmallVector<SDValue, 8> NewOps; 4578 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 4579 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT); 4580 SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset); 4581 4582 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT, 4583 CastVec, TmpIdx); 4584 NewOps.push_back(Elt); 4585 } 4586 4587 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, SL, MidVT, NewOps); 4588 4589 Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec)); 4590 break; 4591 } 4592 case ISD::INSERT_VECTOR_ELT: { 4593 MVT EltVT = OVT.getVectorElementType(); 4594 MVT NewEltVT = NVT.getVectorElementType(); 4595 4596 // Handle bitcasts to a different vector type with the same total bit size 4597 // 4598 // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32 4599 // => 4600 // v4i32:castx = bitcast x:v2i64 4601 // v2i32:casty = bitcast y:i64 4602 // 4603 // v2i64 = bitcast 4604 // (v4i32 insert_vector_elt 4605 // (v4i32 insert_vector_elt v4i32:castx, 4606 // (extract_vector_elt casty, 0), 2 * z), 4607 // (extract_vector_elt casty, 1), (2 * z + 1)) 4608 4609 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 4610 "Invalid promote type for insert_vector_elt"); 4611 assert(NewEltVT.bitsLT(EltVT) && "not handled"); 4612 4613 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 4614 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements(); 4615 4616 SDValue Val = Node->getOperand(1); 4617 SDValue Idx = Node->getOperand(2); 4618 EVT IdxVT = Idx.getValueType(); 4619 SDLoc SL(Node); 4620 4621 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT); 4622 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor); 4623 4624 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0)); 4625 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val); 4626 4627 SDValue NewVec = CastVec; 4628 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 4629 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT); 4630 SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset); 4631 4632 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT, 4633 CastVal, IdxOffset); 4634 4635 NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT, 4636 NewVec, Elt, InEltIdx); 4637 } 4638 4639 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec)); 4640 break; 4641 } 4642 case ISD::SCALAR_TO_VECTOR: { 4643 MVT EltVT = OVT.getVectorElementType(); 4644 MVT NewEltVT = NVT.getVectorElementType(); 4645 4646 // Handle bitcasts to different vector type with the smae total bit size. 4647 // 4648 // e.g. v2i64 = scalar_to_vector x:i64 4649 // => 4650 // concat_vectors (v2i32 bitcast x:i64), (v2i32 undef) 4651 // 4652 4653 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 4654 SDValue Val = Node->getOperand(0); 4655 SDLoc SL(Node); 4656 4657 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val); 4658 SDValue Undef = DAG.getUNDEF(MidVT); 4659 4660 SmallVector<SDValue, 8> NewElts; 4661 NewElts.push_back(CastVal); 4662 for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I) 4663 NewElts.push_back(Undef); 4664 4665 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts); 4666 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat); 4667 Results.push_back(CvtVec); 4668 break; 4669 } 4670 } 4671 4672 // Replace the original node with the legalized result. 4673 if (!Results.empty()) 4674 ReplaceNode(Node, Results.data()); 4675 } 4676 4677 /// This is the entry point for the file. 4678 void SelectionDAG::Legalize() { 4679 AssignTopologicalOrder(); 4680 4681 SmallPtrSet<SDNode *, 16> LegalizedNodes; 4682 SelectionDAGLegalize Legalizer(*this, LegalizedNodes); 4683 4684 // Visit all the nodes. We start in topological order, so that we see 4685 // nodes with their original operands intact. Legalization can produce 4686 // new nodes which may themselves need to be legalized. Iterate until all 4687 // nodes have been legalized. 4688 for (;;) { 4689 bool AnyLegalized = false; 4690 for (auto NI = allnodes_end(); NI != allnodes_begin();) { 4691 --NI; 4692 4693 SDNode *N = &*NI; 4694 if (N->use_empty() && N != getRoot().getNode()) { 4695 ++NI; 4696 DeleteNode(N); 4697 continue; 4698 } 4699 4700 if (LegalizedNodes.insert(N).second) { 4701 AnyLegalized = true; 4702 Legalizer.LegalizeOp(N); 4703 4704 if (N->use_empty() && N != getRoot().getNode()) { 4705 ++NI; 4706 DeleteNode(N); 4707 } 4708 } 4709 } 4710 if (!AnyLegalized) 4711 break; 4712 4713 } 4714 4715 // Remove dead nodes now. 4716 RemoveDeadNodes(); 4717 } 4718 4719 bool SelectionDAG::LegalizeOp(SDNode *N, 4720 SmallSetVector<SDNode *, 16> &UpdatedNodes) { 4721 SmallPtrSet<SDNode *, 16> LegalizedNodes; 4722 SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes); 4723 4724 // Directly insert the node in question, and legalize it. This will recurse 4725 // as needed through operands. 4726 LegalizedNodes.insert(N); 4727 Legalizer.LegalizeOp(N); 4728 4729 return LegalizedNodes.count(N); 4730 } 4731