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