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