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