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