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