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