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