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