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