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