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