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