1 //===- LegalizeVectorOps.cpp - Implement SelectionDAG::LegalizeVectors ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the SelectionDAG::LegalizeVectors method. 11 // 12 // The vector legalizer looks for vector operations which might need to be 13 // scalarized and legalizes them. This is a separate step from Legalize because 14 // scalarizing can introduce illegal types. For example, suppose we have an 15 // ISD::SDIV of type v2i64 on x86-32. The type is legal (for example, addition 16 // on a v2i64 is legal), but ISD::SDIV isn't legal, so we have to unroll the 17 // operation, which introduces nodes with the illegal type i64 which must be 18 // expanded. Similarly, suppose we have an ISD::SRA of type v16i8 on PowerPC; 19 // the operation must be unrolled, which introduces nodes with the illegal 20 // type i8 which must be promoted. 21 // 22 // This does not legalize vector manipulations like ISD::BUILD_VECTOR, 23 // or operations that happen to take a vector which are custom-lowered; 24 // the legalization for such operations never produces nodes 25 // with illegal types, so it's okay to put off legalizing them until 26 // SelectionDAG::Legalize runs. 27 // 28 //===----------------------------------------------------------------------===// 29 30 #include "llvm/ADT/APInt.h" 31 #include "llvm/ADT/DenseMap.h" 32 #include "llvm/ADT/SmallVector.h" 33 #include "llvm/CodeGen/ISDOpcodes.h" 34 #include "llvm/CodeGen/MachineMemOperand.h" 35 #include "llvm/CodeGen/SelectionDAG.h" 36 #include "llvm/CodeGen/SelectionDAGNodes.h" 37 #include "llvm/CodeGen/TargetLowering.h" 38 #include "llvm/CodeGen/ValueTypes.h" 39 #include "llvm/IR/DataLayout.h" 40 #include "llvm/Support/Casting.h" 41 #include "llvm/Support/Compiler.h" 42 #include "llvm/Support/ErrorHandling.h" 43 #include "llvm/Support/MachineValueType.h" 44 #include "llvm/Support/MathExtras.h" 45 #include <cassert> 46 #include <cstdint> 47 #include <iterator> 48 #include <utility> 49 50 using namespace llvm; 51 52 #define DEBUG_TYPE "legalizevectorops" 53 54 namespace { 55 56 class VectorLegalizer { 57 SelectionDAG& DAG; 58 const TargetLowering &TLI; 59 bool Changed = false; // Keep track of whether anything changed 60 61 /// For nodes that are of legal width, and that have more than one use, this 62 /// map indicates what regularized operand to use. This allows us to avoid 63 /// legalizing the same thing more than once. 64 SmallDenseMap<SDValue, SDValue, 64> LegalizedNodes; 65 66 /// Adds a node to the translation cache. 67 void AddLegalizedOperand(SDValue From, SDValue To) { 68 LegalizedNodes.insert(std::make_pair(From, To)); 69 // If someone requests legalization of the new node, return itself. 70 if (From != To) 71 LegalizedNodes.insert(std::make_pair(To, To)); 72 } 73 74 /// Legalizes the given node. 75 SDValue LegalizeOp(SDValue Op); 76 77 /// Assuming the node is legal, "legalize" the results. 78 SDValue TranslateLegalizeResults(SDValue Op, SDValue Result); 79 80 /// Implements unrolling a VSETCC. 81 SDValue UnrollVSETCC(SDValue Op); 82 83 /// Implement expand-based legalization of vector operations. 84 /// 85 /// This is just a high-level routine to dispatch to specific code paths for 86 /// operations to legalize them. 87 SDValue Expand(SDValue Op); 88 89 /// Implements expansion for FP_TO_UINT; falls back to UnrollVectorOp if 90 /// FP_TO_SINT isn't legal. 91 SDValue ExpandFP_TO_UINT(SDValue Op); 92 93 /// Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if 94 /// SINT_TO_FLOAT and SHR on vectors isn't legal. 95 SDValue ExpandUINT_TO_FLOAT(SDValue Op); 96 97 /// Implement expansion for SIGN_EXTEND_INREG using SRL and SRA. 98 SDValue ExpandSEXTINREG(SDValue Op); 99 100 /// Implement expansion for ANY_EXTEND_VECTOR_INREG. 101 /// 102 /// Shuffles the low lanes of the operand into place and bitcasts to the proper 103 /// type. The contents of the bits in the extended part of each element are 104 /// undef. 105 SDValue ExpandANY_EXTEND_VECTOR_INREG(SDValue Op); 106 107 /// Implement expansion for SIGN_EXTEND_VECTOR_INREG. 108 /// 109 /// Shuffles the low lanes of the operand into place, bitcasts to the proper 110 /// type, then shifts left and arithmetic shifts right to introduce a sign 111 /// extension. 112 SDValue ExpandSIGN_EXTEND_VECTOR_INREG(SDValue Op); 113 114 /// Implement expansion for ZERO_EXTEND_VECTOR_INREG. 115 /// 116 /// Shuffles the low lanes of the operand into place and blends zeros into 117 /// the remaining lanes, finally bitcasting to the proper type. 118 SDValue ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op); 119 120 /// Expand bswap of vectors into a shuffle if legal. 121 SDValue ExpandBSWAP(SDValue Op); 122 123 /// Implement vselect in terms of XOR, AND, OR when blend is not 124 /// supported by the target. 125 SDValue ExpandVSELECT(SDValue Op); 126 SDValue ExpandSELECT(SDValue Op); 127 SDValue ExpandLoad(SDValue Op); 128 SDValue ExpandStore(SDValue Op); 129 SDValue ExpandFNEG(SDValue Op); 130 SDValue ExpandFSUB(SDValue Op); 131 SDValue ExpandBITREVERSE(SDValue Op); 132 SDValue ExpandCTPOP(SDValue Op); 133 SDValue ExpandCTLZ(SDValue Op); 134 SDValue ExpandCTTZ(SDValue Op); 135 SDValue ExpandFMINNUM_FMAXNUM(SDValue Op); 136 SDValue ExpandStrictFPOp(SDValue Op); 137 138 /// Implements vector promotion. 139 /// 140 /// This is essentially just bitcasting the operands to a different type and 141 /// bitcasting the result back to the original type. 142 SDValue Promote(SDValue Op); 143 144 /// Implements [SU]INT_TO_FP vector promotion. 145 /// 146 /// This is a [zs]ext of the input operand to a larger integer type. 147 SDValue PromoteINT_TO_FP(SDValue Op); 148 149 /// Implements FP_TO_[SU]INT vector promotion of the result type. 150 /// 151 /// It is promoted to a larger integer type. The result is then 152 /// truncated back to the original type. 153 SDValue PromoteFP_TO_INT(SDValue Op); 154 155 public: 156 VectorLegalizer(SelectionDAG& dag) : 157 DAG(dag), TLI(dag.getTargetLoweringInfo()) {} 158 159 /// Begin legalizer the vector operations in the DAG. 160 bool Run(); 161 }; 162 163 } // end anonymous namespace 164 165 bool VectorLegalizer::Run() { 166 // Before we start legalizing vector nodes, check if there are any vectors. 167 bool HasVectors = false; 168 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), 169 E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I) { 170 // Check if the values of the nodes contain vectors. We don't need to check 171 // the operands because we are going to check their values at some point. 172 for (SDNode::value_iterator J = I->value_begin(), E = I->value_end(); 173 J != E; ++J) 174 HasVectors |= J->isVector(); 175 176 // If we found a vector node we can start the legalization. 177 if (HasVectors) 178 break; 179 } 180 181 // If this basic block has no vectors then no need to legalize vectors. 182 if (!HasVectors) 183 return false; 184 185 // The legalize process is inherently a bottom-up recursive process (users 186 // legalize their uses before themselves). Given infinite stack space, we 187 // could just start legalizing on the root and traverse the whole graph. In 188 // practice however, this causes us to run out of stack space on large basic 189 // blocks. To avoid this problem, compute an ordering of the nodes where each 190 // node is only legalized after all of its operands are legalized. 191 DAG.AssignTopologicalOrder(); 192 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), 193 E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I) 194 LegalizeOp(SDValue(&*I, 0)); 195 196 // Finally, it's possible the root changed. Get the new root. 197 SDValue OldRoot = DAG.getRoot(); 198 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); 199 DAG.setRoot(LegalizedNodes[OldRoot]); 200 201 LegalizedNodes.clear(); 202 203 // Remove dead nodes now. 204 DAG.RemoveDeadNodes(); 205 206 return Changed; 207 } 208 209 SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) { 210 // Generic legalization: just pass the operand through. 211 for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i) 212 AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); 213 return Result.getValue(Op.getResNo()); 214 } 215 216 SDValue VectorLegalizer::LegalizeOp(SDValue Op) { 217 // Note that LegalizeOp may be reentered even from single-use nodes, which 218 // means that we always must cache transformed nodes. 219 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op); 220 if (I != LegalizedNodes.end()) return I->second; 221 222 SDNode* Node = Op.getNode(); 223 224 // Legalize the operands 225 SmallVector<SDValue, 8> Ops; 226 for (const SDValue &Op : Node->op_values()) 227 Ops.push_back(LegalizeOp(Op)); 228 229 SDValue Result = SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops), 230 Op.getResNo()); 231 232 if (Op.getOpcode() == ISD::LOAD) { 233 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode()); 234 ISD::LoadExtType ExtType = LD->getExtensionType(); 235 if (LD->getMemoryVT().isVector() && ExtType != ISD::NON_EXTLOAD) { 236 LLVM_DEBUG(dbgs() << "\nLegalizing extending vector load: "; 237 Node->dump(&DAG)); 238 switch (TLI.getLoadExtAction(LD->getExtensionType(), LD->getValueType(0), 239 LD->getMemoryVT())) { 240 default: llvm_unreachable("This action is not supported yet!"); 241 case TargetLowering::Legal: 242 return TranslateLegalizeResults(Op, Result); 243 case TargetLowering::Custom: 244 if (SDValue Lowered = TLI.LowerOperation(Result, DAG)) { 245 assert(Lowered->getNumValues() == Op->getNumValues() && 246 "Unexpected number of results"); 247 if (Lowered != Result) { 248 // Make sure the new code is also legal. 249 Lowered = LegalizeOp(Lowered); 250 Changed = true; 251 } 252 return TranslateLegalizeResults(Op, Lowered); 253 } 254 LLVM_FALLTHROUGH; 255 case TargetLowering::Expand: 256 Changed = true; 257 return LegalizeOp(ExpandLoad(Op)); 258 } 259 } 260 } else if (Op.getOpcode() == ISD::STORE) { 261 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode()); 262 EVT StVT = ST->getMemoryVT(); 263 MVT ValVT = ST->getValue().getSimpleValueType(); 264 if (StVT.isVector() && ST->isTruncatingStore()) { 265 LLVM_DEBUG(dbgs() << "\nLegalizing truncating vector store: "; 266 Node->dump(&DAG)); 267 switch (TLI.getTruncStoreAction(ValVT, StVT)) { 268 default: llvm_unreachable("This action is not supported yet!"); 269 case TargetLowering::Legal: 270 return TranslateLegalizeResults(Op, Result); 271 case TargetLowering::Custom: { 272 SDValue Lowered = TLI.LowerOperation(Result, DAG); 273 if (Lowered != Result) { 274 // Make sure the new code is also legal. 275 Lowered = LegalizeOp(Lowered); 276 Changed = true; 277 } 278 return TranslateLegalizeResults(Op, Lowered); 279 } 280 case TargetLowering::Expand: 281 Changed = true; 282 return LegalizeOp(ExpandStore(Op)); 283 } 284 } 285 } 286 287 bool HasVectorValue = false; 288 for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end(); 289 J != E; 290 ++J) 291 HasVectorValue |= J->isVector(); 292 if (!HasVectorValue) 293 return TranslateLegalizeResults(Op, Result); 294 295 TargetLowering::LegalizeAction Action = TargetLowering::Legal; 296 switch (Op.getOpcode()) { 297 default: 298 return TranslateLegalizeResults(Op, Result); 299 case ISD::STRICT_FADD: 300 case ISD::STRICT_FSUB: 301 case ISD::STRICT_FMUL: 302 case ISD::STRICT_FDIV: 303 case ISD::STRICT_FREM: 304 case ISD::STRICT_FSQRT: 305 case ISD::STRICT_FMA: 306 case ISD::STRICT_FPOW: 307 case ISD::STRICT_FPOWI: 308 case ISD::STRICT_FSIN: 309 case ISD::STRICT_FCOS: 310 case ISD::STRICT_FEXP: 311 case ISD::STRICT_FEXP2: 312 case ISD::STRICT_FLOG: 313 case ISD::STRICT_FLOG10: 314 case ISD::STRICT_FLOG2: 315 case ISD::STRICT_FRINT: 316 case ISD::STRICT_FNEARBYINT: 317 case ISD::STRICT_FMAXNUM: 318 case ISD::STRICT_FMINNUM: 319 case ISD::STRICT_FCEIL: 320 case ISD::STRICT_FFLOOR: 321 case ISD::STRICT_FROUND: 322 case ISD::STRICT_FTRUNC: 323 // These pseudo-ops get legalized as if they were their non-strict 324 // equivalent. For instance, if ISD::FSQRT is legal then ISD::STRICT_FSQRT 325 // is also legal, but if ISD::FSQRT requires expansion then so does 326 // ISD::STRICT_FSQRT. 327 Action = TLI.getStrictFPOperationAction(Node->getOpcode(), 328 Node->getValueType(0)); 329 break; 330 case ISD::ADD: 331 case ISD::SUB: 332 case ISD::MUL: 333 case ISD::SDIV: 334 case ISD::UDIV: 335 case ISD::SREM: 336 case ISD::UREM: 337 case ISD::SDIVREM: 338 case ISD::UDIVREM: 339 case ISD::FADD: 340 case ISD::FSUB: 341 case ISD::FMUL: 342 case ISD::FDIV: 343 case ISD::FREM: 344 case ISD::AND: 345 case ISD::OR: 346 case ISD::XOR: 347 case ISD::SHL: 348 case ISD::SRA: 349 case ISD::SRL: 350 case ISD::ROTL: 351 case ISD::ROTR: 352 case ISD::BSWAP: 353 case ISD::BITREVERSE: 354 case ISD::CTLZ: 355 case ISD::CTTZ: 356 case ISD::CTLZ_ZERO_UNDEF: 357 case ISD::CTTZ_ZERO_UNDEF: 358 case ISD::CTPOP: 359 case ISD::SELECT: 360 case ISD::VSELECT: 361 case ISD::SELECT_CC: 362 case ISD::SETCC: 363 case ISD::ZERO_EXTEND: 364 case ISD::ANY_EXTEND: 365 case ISD::TRUNCATE: 366 case ISD::SIGN_EXTEND: 367 case ISD::FP_TO_SINT: 368 case ISD::FP_TO_UINT: 369 case ISD::FNEG: 370 case ISD::FABS: 371 case ISD::FMINNUM: 372 case ISD::FMAXNUM: 373 case ISD::FMINNUM_IEEE: 374 case ISD::FMAXNUM_IEEE: 375 case ISD::FMINIMUM: 376 case ISD::FMAXIMUM: 377 case ISD::FCOPYSIGN: 378 case ISD::FSQRT: 379 case ISD::FSIN: 380 case ISD::FCOS: 381 case ISD::FPOWI: 382 case ISD::FPOW: 383 case ISD::FLOG: 384 case ISD::FLOG2: 385 case ISD::FLOG10: 386 case ISD::FEXP: 387 case ISD::FEXP2: 388 case ISD::FCEIL: 389 case ISD::FTRUNC: 390 case ISD::FRINT: 391 case ISD::FNEARBYINT: 392 case ISD::FROUND: 393 case ISD::FFLOOR: 394 case ISD::FP_ROUND: 395 case ISD::FP_EXTEND: 396 case ISD::FMA: 397 case ISD::SIGN_EXTEND_INREG: 398 case ISD::ANY_EXTEND_VECTOR_INREG: 399 case ISD::SIGN_EXTEND_VECTOR_INREG: 400 case ISD::ZERO_EXTEND_VECTOR_INREG: 401 case ISD::SMIN: 402 case ISD::SMAX: 403 case ISD::UMIN: 404 case ISD::UMAX: 405 case ISD::SMUL_LOHI: 406 case ISD::UMUL_LOHI: 407 case ISD::FCANONICALIZE: 408 case ISD::SADDSAT: 409 case ISD::UADDSAT: 410 case ISD::SSUBSAT: 411 case ISD::USUBSAT: 412 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 413 break; 414 case ISD::FP_ROUND_INREG: 415 Action = TLI.getOperationAction(Node->getOpcode(), 416 cast<VTSDNode>(Node->getOperand(1))->getVT()); 417 break; 418 case ISD::SINT_TO_FP: 419 case ISD::UINT_TO_FP: 420 Action = TLI.getOperationAction(Node->getOpcode(), 421 Node->getOperand(0).getValueType()); 422 break; 423 } 424 425 LLVM_DEBUG(dbgs() << "\nLegalizing vector op: "; Node->dump(&DAG)); 426 427 switch (Action) { 428 default: llvm_unreachable("This action is not supported yet!"); 429 case TargetLowering::Promote: 430 Result = Promote(Op); 431 Changed = true; 432 break; 433 case TargetLowering::Legal: 434 LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n"); 435 break; 436 case TargetLowering::Custom: { 437 LLVM_DEBUG(dbgs() << "Trying custom legalization\n"); 438 if (SDValue Tmp1 = TLI.LowerOperation(Op, DAG)) { 439 LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n"); 440 Result = Tmp1; 441 break; 442 } 443 LLVM_DEBUG(dbgs() << "Could not custom legalize node\n"); 444 LLVM_FALLTHROUGH; 445 } 446 case TargetLowering::Expand: 447 Result = Expand(Op); 448 } 449 450 // Make sure that the generated code is itself legal. 451 if (Result != Op) { 452 Result = LegalizeOp(Result); 453 Changed = true; 454 } 455 456 // Note that LegalizeOp may be reentered even from single-use nodes, which 457 // means that we always must cache transformed nodes. 458 AddLegalizedOperand(Op, Result); 459 return Result; 460 } 461 462 SDValue VectorLegalizer::Promote(SDValue Op) { 463 // For a few operations there is a specific concept for promotion based on 464 // the operand's type. 465 switch (Op.getOpcode()) { 466 case ISD::SINT_TO_FP: 467 case ISD::UINT_TO_FP: 468 // "Promote" the operation by extending the operand. 469 return PromoteINT_TO_FP(Op); 470 case ISD::FP_TO_UINT: 471 case ISD::FP_TO_SINT: 472 // Promote the operation by extending the operand. 473 return PromoteFP_TO_INT(Op); 474 } 475 476 // There are currently two cases of vector promotion: 477 // 1) Bitcasting a vector of integers to a different type to a vector of the 478 // same overall length. For example, x86 promotes ISD::AND v2i32 to v1i64. 479 // 2) Extending a vector of floats to a vector of the same number of larger 480 // floats. For example, AArch64 promotes ISD::FADD on v4f16 to v4f32. 481 MVT VT = Op.getSimpleValueType(); 482 assert(Op.getNode()->getNumValues() == 1 && 483 "Can't promote a vector with multiple results!"); 484 MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT); 485 SDLoc dl(Op); 486 SmallVector<SDValue, 4> Operands(Op.getNumOperands()); 487 488 for (unsigned j = 0; j != Op.getNumOperands(); ++j) { 489 if (Op.getOperand(j).getValueType().isVector()) 490 if (Op.getOperand(j) 491 .getValueType() 492 .getVectorElementType() 493 .isFloatingPoint() && 494 NVT.isVector() && NVT.getVectorElementType().isFloatingPoint()) 495 Operands[j] = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Op.getOperand(j)); 496 else 497 Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j)); 498 else 499 Operands[j] = Op.getOperand(j); 500 } 501 502 Op = DAG.getNode(Op.getOpcode(), dl, NVT, Operands, Op.getNode()->getFlags()); 503 if ((VT.isFloatingPoint() && NVT.isFloatingPoint()) || 504 (VT.isVector() && VT.getVectorElementType().isFloatingPoint() && 505 NVT.isVector() && NVT.getVectorElementType().isFloatingPoint())) 506 return DAG.getNode(ISD::FP_ROUND, dl, VT, Op, DAG.getIntPtrConstant(0, dl)); 507 else 508 return DAG.getNode(ISD::BITCAST, dl, VT, Op); 509 } 510 511 SDValue VectorLegalizer::PromoteINT_TO_FP(SDValue Op) { 512 // INT_TO_FP operations may require the input operand be promoted even 513 // when the type is otherwise legal. 514 MVT VT = Op.getOperand(0).getSimpleValueType(); 515 MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT); 516 assert(NVT.getVectorNumElements() == VT.getVectorNumElements() && 517 "Vectors have different number of elements!"); 518 519 SDLoc dl(Op); 520 SmallVector<SDValue, 4> Operands(Op.getNumOperands()); 521 522 unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND : 523 ISD::SIGN_EXTEND; 524 for (unsigned j = 0; j != Op.getNumOperands(); ++j) { 525 if (Op.getOperand(j).getValueType().isVector()) 526 Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j)); 527 else 528 Operands[j] = Op.getOperand(j); 529 } 530 531 return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), Operands); 532 } 533 534 // For FP_TO_INT we promote the result type to a vector type with wider 535 // elements and then truncate the result. This is different from the default 536 // PromoteVector which uses bitcast to promote thus assumning that the 537 // promoted vector type has the same overall size. 538 SDValue VectorLegalizer::PromoteFP_TO_INT(SDValue Op) { 539 MVT VT = Op.getSimpleValueType(); 540 MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT); 541 assert(NVT.getVectorNumElements() == VT.getVectorNumElements() && 542 "Vectors have different number of elements!"); 543 544 unsigned NewOpc = Op->getOpcode(); 545 // Change FP_TO_UINT to FP_TO_SINT if possible. 546 // TODO: Should we only do this if FP_TO_UINT itself isn't legal? 547 if (NewOpc == ISD::FP_TO_UINT && 548 TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT)) 549 NewOpc = ISD::FP_TO_SINT; 550 551 SDLoc dl(Op); 552 SDValue Promoted = DAG.getNode(NewOpc, dl, NVT, Op.getOperand(0)); 553 554 // Assert that the converted value fits in the original type. If it doesn't 555 // (eg: because the value being converted is too big), then the result of the 556 // original operation was undefined anyway, so the assert is still correct. 557 Promoted = DAG.getNode(Op->getOpcode() == ISD::FP_TO_UINT ? ISD::AssertZext 558 : ISD::AssertSext, 559 dl, NVT, Promoted, 560 DAG.getValueType(VT.getScalarType())); 561 return DAG.getNode(ISD::TRUNCATE, dl, VT, Promoted); 562 } 563 564 SDValue VectorLegalizer::ExpandLoad(SDValue Op) { 565 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode()); 566 567 EVT SrcVT = LD->getMemoryVT(); 568 EVT SrcEltVT = SrcVT.getScalarType(); 569 unsigned NumElem = SrcVT.getVectorNumElements(); 570 571 SDValue NewChain; 572 SDValue Value; 573 if (SrcVT.getVectorNumElements() > 1 && !SrcEltVT.isByteSized()) { 574 SDLoc dl(Op); 575 576 SmallVector<SDValue, 8> Vals; 577 SmallVector<SDValue, 8> LoadChains; 578 579 EVT DstEltVT = LD->getValueType(0).getScalarType(); 580 SDValue Chain = LD->getChain(); 581 SDValue BasePTR = LD->getBasePtr(); 582 ISD::LoadExtType ExtType = LD->getExtensionType(); 583 584 // When elements in a vector is not byte-addressable, we cannot directly 585 // load each element by advancing pointer, which could only address bytes. 586 // Instead, we load all significant words, mask bits off, and concatenate 587 // them to form each element. Finally, they are extended to destination 588 // scalar type to build the destination vector. 589 EVT WideVT = TLI.getPointerTy(DAG.getDataLayout()); 590 591 assert(WideVT.isRound() && 592 "Could not handle the sophisticated case when the widest integer is" 593 " not power of 2."); 594 assert(WideVT.bitsGE(SrcEltVT) && 595 "Type is not legalized?"); 596 597 unsigned WideBytes = WideVT.getStoreSize(); 598 unsigned Offset = 0; 599 unsigned RemainingBytes = SrcVT.getStoreSize(); 600 SmallVector<SDValue, 8> LoadVals; 601 while (RemainingBytes > 0) { 602 SDValue ScalarLoad; 603 unsigned LoadBytes = WideBytes; 604 605 if (RemainingBytes >= LoadBytes) { 606 ScalarLoad = 607 DAG.getLoad(WideVT, dl, Chain, BasePTR, 608 LD->getPointerInfo().getWithOffset(Offset), 609 MinAlign(LD->getAlignment(), Offset), 610 LD->getMemOperand()->getFlags(), LD->getAAInfo()); 611 } else { 612 EVT LoadVT = WideVT; 613 while (RemainingBytes < LoadBytes) { 614 LoadBytes >>= 1; // Reduce the load size by half. 615 LoadVT = EVT::getIntegerVT(*DAG.getContext(), LoadBytes << 3); 616 } 617 ScalarLoad = 618 DAG.getExtLoad(ISD::EXTLOAD, dl, WideVT, Chain, BasePTR, 619 LD->getPointerInfo().getWithOffset(Offset), LoadVT, 620 MinAlign(LD->getAlignment(), Offset), 621 LD->getMemOperand()->getFlags(), LD->getAAInfo()); 622 } 623 624 RemainingBytes -= LoadBytes; 625 Offset += LoadBytes; 626 627 BasePTR = DAG.getObjectPtrOffset(dl, BasePTR, LoadBytes); 628 629 LoadVals.push_back(ScalarLoad.getValue(0)); 630 LoadChains.push_back(ScalarLoad.getValue(1)); 631 } 632 633 // Extract bits, pack and extend/trunc them into destination type. 634 unsigned SrcEltBits = SrcEltVT.getSizeInBits(); 635 SDValue SrcEltBitMask = DAG.getConstant((1U << SrcEltBits) - 1, dl, WideVT); 636 637 unsigned BitOffset = 0; 638 unsigned WideIdx = 0; 639 unsigned WideBits = WideVT.getSizeInBits(); 640 641 for (unsigned Idx = 0; Idx != NumElem; ++Idx) { 642 SDValue Lo, Hi, ShAmt; 643 644 if (BitOffset < WideBits) { 645 ShAmt = DAG.getConstant( 646 BitOffset, dl, TLI.getShiftAmountTy(WideVT, DAG.getDataLayout())); 647 Lo = DAG.getNode(ISD::SRL, dl, WideVT, LoadVals[WideIdx], ShAmt); 648 Lo = DAG.getNode(ISD::AND, dl, WideVT, Lo, SrcEltBitMask); 649 } 650 651 BitOffset += SrcEltBits; 652 if (BitOffset >= WideBits) { 653 WideIdx++; 654 BitOffset -= WideBits; 655 if (BitOffset > 0) { 656 ShAmt = DAG.getConstant( 657 SrcEltBits - BitOffset, dl, 658 TLI.getShiftAmountTy(WideVT, DAG.getDataLayout())); 659 Hi = DAG.getNode(ISD::SHL, dl, WideVT, LoadVals[WideIdx], ShAmt); 660 Hi = DAG.getNode(ISD::AND, dl, WideVT, Hi, SrcEltBitMask); 661 } 662 } 663 664 if (Hi.getNode()) 665 Lo = DAG.getNode(ISD::OR, dl, WideVT, Lo, Hi); 666 667 switch (ExtType) { 668 default: llvm_unreachable("Unknown extended-load op!"); 669 case ISD::EXTLOAD: 670 Lo = DAG.getAnyExtOrTrunc(Lo, dl, DstEltVT); 671 break; 672 case ISD::ZEXTLOAD: 673 Lo = DAG.getZExtOrTrunc(Lo, dl, DstEltVT); 674 break; 675 case ISD::SEXTLOAD: 676 ShAmt = 677 DAG.getConstant(WideBits - SrcEltBits, dl, 678 TLI.getShiftAmountTy(WideVT, DAG.getDataLayout())); 679 Lo = DAG.getNode(ISD::SHL, dl, WideVT, Lo, ShAmt); 680 Lo = DAG.getNode(ISD::SRA, dl, WideVT, Lo, ShAmt); 681 Lo = DAG.getSExtOrTrunc(Lo, dl, DstEltVT); 682 break; 683 } 684 Vals.push_back(Lo); 685 } 686 687 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains); 688 Value = DAG.getBuildVector(Op.getNode()->getValueType(0), dl, Vals); 689 } else { 690 SDValue Scalarized = TLI.scalarizeVectorLoad(LD, DAG); 691 // Skip past MERGE_VALUE node if known. 692 if (Scalarized->getOpcode() == ISD::MERGE_VALUES) { 693 NewChain = Scalarized.getOperand(1); 694 Value = Scalarized.getOperand(0); 695 } else { 696 NewChain = Scalarized.getValue(1); 697 Value = Scalarized.getValue(0); 698 } 699 } 700 701 AddLegalizedOperand(Op.getValue(0), Value); 702 AddLegalizedOperand(Op.getValue(1), NewChain); 703 704 return (Op.getResNo() ? NewChain : Value); 705 } 706 707 SDValue VectorLegalizer::ExpandStore(SDValue Op) { 708 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode()); 709 SDValue TF = TLI.scalarizeVectorStore(ST, DAG); 710 AddLegalizedOperand(Op, TF); 711 return TF; 712 } 713 714 SDValue VectorLegalizer::Expand(SDValue Op) { 715 switch (Op->getOpcode()) { 716 case ISD::SIGN_EXTEND_INREG: 717 return ExpandSEXTINREG(Op); 718 case ISD::ANY_EXTEND_VECTOR_INREG: 719 return ExpandANY_EXTEND_VECTOR_INREG(Op); 720 case ISD::SIGN_EXTEND_VECTOR_INREG: 721 return ExpandSIGN_EXTEND_VECTOR_INREG(Op); 722 case ISD::ZERO_EXTEND_VECTOR_INREG: 723 return ExpandZERO_EXTEND_VECTOR_INREG(Op); 724 case ISD::BSWAP: 725 return ExpandBSWAP(Op); 726 case ISD::VSELECT: 727 return ExpandVSELECT(Op); 728 case ISD::SELECT: 729 return ExpandSELECT(Op); 730 case ISD::FP_TO_UINT: 731 return ExpandFP_TO_UINT(Op); 732 case ISD::UINT_TO_FP: 733 return ExpandUINT_TO_FLOAT(Op); 734 case ISD::FNEG: 735 return ExpandFNEG(Op); 736 case ISD::FSUB: 737 return ExpandFSUB(Op); 738 case ISD::SETCC: 739 return UnrollVSETCC(Op); 740 case ISD::BITREVERSE: 741 return ExpandBITREVERSE(Op); 742 case ISD::CTPOP: 743 return ExpandCTPOP(Op); 744 case ISD::CTLZ: 745 case ISD::CTLZ_ZERO_UNDEF: 746 return ExpandCTLZ(Op); 747 case ISD::CTTZ: 748 case ISD::CTTZ_ZERO_UNDEF: 749 return ExpandCTTZ(Op); 750 case ISD::FMINNUM: 751 case ISD::FMAXNUM: 752 return ExpandFMINNUM_FMAXNUM(Op); 753 case ISD::STRICT_FADD: 754 case ISD::STRICT_FSUB: 755 case ISD::STRICT_FMUL: 756 case ISD::STRICT_FDIV: 757 case ISD::STRICT_FREM: 758 case ISD::STRICT_FSQRT: 759 case ISD::STRICT_FMA: 760 case ISD::STRICT_FPOW: 761 case ISD::STRICT_FPOWI: 762 case ISD::STRICT_FSIN: 763 case ISD::STRICT_FCOS: 764 case ISD::STRICT_FEXP: 765 case ISD::STRICT_FEXP2: 766 case ISD::STRICT_FLOG: 767 case ISD::STRICT_FLOG10: 768 case ISD::STRICT_FLOG2: 769 case ISD::STRICT_FRINT: 770 case ISD::STRICT_FNEARBYINT: 771 case ISD::STRICT_FMAXNUM: 772 case ISD::STRICT_FMINNUM: 773 case ISD::STRICT_FCEIL: 774 case ISD::STRICT_FFLOOR: 775 case ISD::STRICT_FROUND: 776 case ISD::STRICT_FTRUNC: 777 return ExpandStrictFPOp(Op); 778 default: 779 return DAG.UnrollVectorOp(Op.getNode()); 780 } 781 } 782 783 SDValue VectorLegalizer::ExpandSELECT(SDValue Op) { 784 // Lower a select instruction where the condition is a scalar and the 785 // operands are vectors. Lower this select to VSELECT and implement it 786 // using XOR AND OR. The selector bit is broadcasted. 787 EVT VT = Op.getValueType(); 788 SDLoc DL(Op); 789 790 SDValue Mask = Op.getOperand(0); 791 SDValue Op1 = Op.getOperand(1); 792 SDValue Op2 = Op.getOperand(2); 793 794 assert(VT.isVector() && !Mask.getValueType().isVector() 795 && Op1.getValueType() == Op2.getValueType() && "Invalid type"); 796 797 // If we can't even use the basic vector operations of 798 // AND,OR,XOR, we will have to scalarize the op. 799 // Notice that the operation may be 'promoted' which means that it is 800 // 'bitcasted' to another type which is handled. 801 // Also, we need to be able to construct a splat vector using BUILD_VECTOR. 802 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand || 803 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand || 804 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand || 805 TLI.getOperationAction(ISD::BUILD_VECTOR, VT) == TargetLowering::Expand) 806 return DAG.UnrollVectorOp(Op.getNode()); 807 808 // Generate a mask operand. 809 EVT MaskTy = VT.changeVectorElementTypeToInteger(); 810 811 // What is the size of each element in the vector mask. 812 EVT BitTy = MaskTy.getScalarType(); 813 814 Mask = DAG.getSelect(DL, BitTy, Mask, 815 DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), DL, 816 BitTy), 817 DAG.getConstant(0, DL, BitTy)); 818 819 // Broadcast the mask so that the entire vector is all-one or all zero. 820 Mask = DAG.getSplatBuildVector(MaskTy, DL, Mask); 821 822 // Bitcast the operands to be the same type as the mask. 823 // This is needed when we select between FP types because 824 // the mask is a vector of integers. 825 Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1); 826 Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2); 827 828 SDValue AllOnes = DAG.getConstant( 829 APInt::getAllOnesValue(BitTy.getSizeInBits()), DL, MaskTy); 830 SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes); 831 832 Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask); 833 Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask); 834 SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2); 835 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val); 836 } 837 838 SDValue VectorLegalizer::ExpandSEXTINREG(SDValue Op) { 839 EVT VT = Op.getValueType(); 840 841 // Make sure that the SRA and SHL instructions are available. 842 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand || 843 TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand) 844 return DAG.UnrollVectorOp(Op.getNode()); 845 846 SDLoc DL(Op); 847 EVT OrigTy = cast<VTSDNode>(Op->getOperand(1))->getVT(); 848 849 unsigned BW = VT.getScalarSizeInBits(); 850 unsigned OrigBW = OrigTy.getScalarSizeInBits(); 851 SDValue ShiftSz = DAG.getConstant(BW - OrigBW, DL, VT); 852 853 Op = Op.getOperand(0); 854 Op = DAG.getNode(ISD::SHL, DL, VT, Op, ShiftSz); 855 return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz); 856 } 857 858 // Generically expand a vector anyext in register to a shuffle of the relevant 859 // lanes into the appropriate locations, with other lanes left undef. 860 SDValue VectorLegalizer::ExpandANY_EXTEND_VECTOR_INREG(SDValue Op) { 861 SDLoc DL(Op); 862 EVT VT = Op.getValueType(); 863 int NumElements = VT.getVectorNumElements(); 864 SDValue Src = Op.getOperand(0); 865 EVT SrcVT = Src.getValueType(); 866 int NumSrcElements = SrcVT.getVectorNumElements(); 867 868 // Build a base mask of undef shuffles. 869 SmallVector<int, 16> ShuffleMask; 870 ShuffleMask.resize(NumSrcElements, -1); 871 872 // Place the extended lanes into the correct locations. 873 int ExtLaneScale = NumSrcElements / NumElements; 874 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0; 875 for (int i = 0; i < NumElements; ++i) 876 ShuffleMask[i * ExtLaneScale + EndianOffset] = i; 877 878 return DAG.getNode( 879 ISD::BITCAST, DL, VT, 880 DAG.getVectorShuffle(SrcVT, DL, Src, DAG.getUNDEF(SrcVT), ShuffleMask)); 881 } 882 883 SDValue VectorLegalizer::ExpandSIGN_EXTEND_VECTOR_INREG(SDValue Op) { 884 SDLoc DL(Op); 885 EVT VT = Op.getValueType(); 886 SDValue Src = Op.getOperand(0); 887 EVT SrcVT = Src.getValueType(); 888 889 // First build an any-extend node which can be legalized above when we 890 // recurse through it. 891 Op = DAG.getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Src); 892 893 // Now we need sign extend. Do this by shifting the elements. Even if these 894 // aren't legal operations, they have a better chance of being legalized 895 // without full scalarization than the sign extension does. 896 unsigned EltWidth = VT.getScalarSizeInBits(); 897 unsigned SrcEltWidth = SrcVT.getScalarSizeInBits(); 898 SDValue ShiftAmount = DAG.getConstant(EltWidth - SrcEltWidth, DL, VT); 899 return DAG.getNode(ISD::SRA, DL, VT, 900 DAG.getNode(ISD::SHL, DL, VT, Op, ShiftAmount), 901 ShiftAmount); 902 } 903 904 // Generically expand a vector zext in register to a shuffle of the relevant 905 // lanes into the appropriate locations, a blend of zero into the high bits, 906 // and a bitcast to the wider element type. 907 SDValue VectorLegalizer::ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op) { 908 SDLoc DL(Op); 909 EVT VT = Op.getValueType(); 910 int NumElements = VT.getVectorNumElements(); 911 SDValue Src = Op.getOperand(0); 912 EVT SrcVT = Src.getValueType(); 913 int NumSrcElements = SrcVT.getVectorNumElements(); 914 915 // Build up a zero vector to blend into this one. 916 SDValue Zero = DAG.getConstant(0, DL, SrcVT); 917 918 // Shuffle the incoming lanes into the correct position, and pull all other 919 // lanes from the zero vector. 920 SmallVector<int, 16> ShuffleMask; 921 ShuffleMask.reserve(NumSrcElements); 922 for (int i = 0; i < NumSrcElements; ++i) 923 ShuffleMask.push_back(i); 924 925 int ExtLaneScale = NumSrcElements / NumElements; 926 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0; 927 for (int i = 0; i < NumElements; ++i) 928 ShuffleMask[i * ExtLaneScale + EndianOffset] = NumSrcElements + i; 929 930 return DAG.getNode(ISD::BITCAST, DL, VT, 931 DAG.getVectorShuffle(SrcVT, DL, Zero, Src, ShuffleMask)); 932 } 933 934 static void createBSWAPShuffleMask(EVT VT, SmallVectorImpl<int> &ShuffleMask) { 935 int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8; 936 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) 937 for (int J = ScalarSizeInBytes - 1; J >= 0; --J) 938 ShuffleMask.push_back((I * ScalarSizeInBytes) + J); 939 } 940 941 SDValue VectorLegalizer::ExpandBSWAP(SDValue Op) { 942 EVT VT = Op.getValueType(); 943 944 // Generate a byte wise shuffle mask for the BSWAP. 945 SmallVector<int, 16> ShuffleMask; 946 createBSWAPShuffleMask(VT, ShuffleMask); 947 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size()); 948 949 // Only emit a shuffle if the mask is legal. 950 if (!TLI.isShuffleMaskLegal(ShuffleMask, ByteVT)) 951 return DAG.UnrollVectorOp(Op.getNode()); 952 953 SDLoc DL(Op); 954 Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Op.getOperand(0)); 955 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), ShuffleMask); 956 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 957 } 958 959 SDValue VectorLegalizer::ExpandBITREVERSE(SDValue Op) { 960 EVT VT = Op.getValueType(); 961 962 // If we have the scalar operation, it's probably cheaper to unroll it. 963 if (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, VT.getScalarType())) 964 return DAG.UnrollVectorOp(Op.getNode()); 965 966 // If the vector element width is a whole number of bytes, test if its legal 967 // to BSWAP shuffle the bytes and then perform the BITREVERSE on the byte 968 // vector. This greatly reduces the number of bit shifts necessary. 969 unsigned ScalarSizeInBits = VT.getScalarSizeInBits(); 970 if (ScalarSizeInBits > 8 && (ScalarSizeInBits % 8) == 0) { 971 SmallVector<int, 16> BSWAPMask; 972 createBSWAPShuffleMask(VT, BSWAPMask); 973 974 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, BSWAPMask.size()); 975 if (TLI.isShuffleMaskLegal(BSWAPMask, ByteVT) && 976 (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, ByteVT) || 977 (TLI.isOperationLegalOrCustom(ISD::SHL, ByteVT) && 978 TLI.isOperationLegalOrCustom(ISD::SRL, ByteVT) && 979 TLI.isOperationLegalOrCustomOrPromote(ISD::AND, ByteVT) && 980 TLI.isOperationLegalOrCustomOrPromote(ISD::OR, ByteVT)))) { 981 SDLoc DL(Op); 982 Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Op.getOperand(0)); 983 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), 984 BSWAPMask); 985 Op = DAG.getNode(ISD::BITREVERSE, DL, ByteVT, Op); 986 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 987 } 988 } 989 990 // If we have the appropriate vector bit operations, it is better to use them 991 // than unrolling and expanding each component. 992 if (!TLI.isOperationLegalOrCustom(ISD::SHL, VT) || 993 !TLI.isOperationLegalOrCustom(ISD::SRL, VT) || 994 !TLI.isOperationLegalOrCustomOrPromote(ISD::AND, VT) || 995 !TLI.isOperationLegalOrCustomOrPromote(ISD::OR, VT)) 996 return DAG.UnrollVectorOp(Op.getNode()); 997 998 // Let LegalizeDAG handle this later. 999 return Op; 1000 } 1001 1002 SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) { 1003 // Implement VSELECT in terms of XOR, AND, OR 1004 // on platforms which do not support blend natively. 1005 SDLoc DL(Op); 1006 1007 SDValue Mask = Op.getOperand(0); 1008 SDValue Op1 = Op.getOperand(1); 1009 SDValue Op2 = Op.getOperand(2); 1010 1011 EVT VT = Mask.getValueType(); 1012 1013 // If we can't even use the basic vector operations of 1014 // AND,OR,XOR, we will have to scalarize the op. 1015 // Notice that the operation may be 'promoted' which means that it is 1016 // 'bitcasted' to another type which is handled. 1017 // This operation also isn't safe with AND, OR, XOR when the boolean 1018 // type is 0/1 as we need an all ones vector constant to mask with. 1019 // FIXME: Sign extend 1 to all ones if thats legal on the target. 1020 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand || 1021 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand || 1022 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand || 1023 TLI.getBooleanContents(Op1.getValueType()) != 1024 TargetLowering::ZeroOrNegativeOneBooleanContent) 1025 return DAG.UnrollVectorOp(Op.getNode()); 1026 1027 // If the mask and the type are different sizes, unroll the vector op. This 1028 // can occur when getSetCCResultType returns something that is different in 1029 // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8. 1030 if (VT.getSizeInBits() != Op1.getValueSizeInBits()) 1031 return DAG.UnrollVectorOp(Op.getNode()); 1032 1033 // Bitcast the operands to be the same type as the mask. 1034 // This is needed when we select between FP types because 1035 // the mask is a vector of integers. 1036 Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1); 1037 Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2); 1038 1039 SDValue AllOnes = DAG.getConstant( 1040 APInt::getAllOnesValue(VT.getScalarSizeInBits()), DL, VT); 1041 SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes); 1042 1043 Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask); 1044 Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask); 1045 SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2); 1046 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val); 1047 } 1048 1049 SDValue VectorLegalizer::ExpandFP_TO_UINT(SDValue Op) { 1050 // Attempt to expand using TargetLowering. 1051 SDValue Result; 1052 if (TLI.expandFP_TO_UINT(Op.getNode(), Result, DAG)) 1053 return Result; 1054 1055 // Otherwise go ahead and unroll. 1056 return DAG.UnrollVectorOp(Op.getNode()); 1057 } 1058 1059 SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) { 1060 EVT VT = Op.getOperand(0).getValueType(); 1061 SDLoc DL(Op); 1062 1063 // Attempt to expand using TargetLowering. 1064 SDValue Result; 1065 if (TLI.expandUINT_TO_FP(Op.getNode(), Result, DAG)) 1066 return Result; 1067 1068 // Make sure that the SINT_TO_FP and SRL instructions are available. 1069 if (TLI.getOperationAction(ISD::SINT_TO_FP, VT) == TargetLowering::Expand || 1070 TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand) 1071 return DAG.UnrollVectorOp(Op.getNode()); 1072 1073 unsigned BW = VT.getScalarSizeInBits(); 1074 assert((BW == 64 || BW == 32) && 1075 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide"); 1076 1077 SDValue HalfWord = DAG.getConstant(BW / 2, DL, VT); 1078 1079 // Constants to clear the upper part of the word. 1080 // Notice that we can also use SHL+SHR, but using a constant is slightly 1081 // faster on x86. 1082 uint64_t HWMask = (BW == 64) ? 0x00000000FFFFFFFF : 0x0000FFFF; 1083 SDValue HalfWordMask = DAG.getConstant(HWMask, DL, VT); 1084 1085 // Two to the power of half-word-size. 1086 SDValue TWOHW = DAG.getConstantFP(1ULL << (BW / 2), DL, Op.getValueType()); 1087 1088 // Clear upper part of LO, lower HI 1089 SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord); 1090 SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask); 1091 1092 // Convert hi and lo to floats 1093 // Convert the hi part back to the upper values 1094 // TODO: Can any fast-math-flags be set on these nodes? 1095 SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI); 1096 fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW); 1097 SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO); 1098 1099 // Add the two halves 1100 return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO); 1101 } 1102 1103 SDValue VectorLegalizer::ExpandFNEG(SDValue Op) { 1104 if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) { 1105 SDLoc DL(Op); 1106 SDValue Zero = DAG.getConstantFP(-0.0, DL, Op.getValueType()); 1107 // TODO: If FNEG had fast-math-flags, they'd get propagated to this FSUB. 1108 return DAG.getNode(ISD::FSUB, DL, Op.getValueType(), 1109 Zero, Op.getOperand(0)); 1110 } 1111 return DAG.UnrollVectorOp(Op.getNode()); 1112 } 1113 1114 SDValue VectorLegalizer::ExpandFSUB(SDValue Op) { 1115 // For floating-point values, (a-b) is the same as a+(-b). If FNEG is legal, 1116 // we can defer this to operation legalization where it will be lowered as 1117 // a+(-b). 1118 EVT VT = Op.getValueType(); 1119 if (TLI.isOperationLegalOrCustom(ISD::FNEG, VT) && 1120 TLI.isOperationLegalOrCustom(ISD::FADD, VT)) 1121 return Op; // Defer to LegalizeDAG 1122 1123 return DAG.UnrollVectorOp(Op.getNode()); 1124 } 1125 1126 SDValue VectorLegalizer::ExpandCTPOP(SDValue Op) { 1127 // Attempt to expand using TargetLowering. 1128 SDValue Result; 1129 if (TLI.expandCTPOP(Op.getNode(), Result, DAG)) 1130 return Result; 1131 1132 // Otherwise go ahead and unroll. 1133 return DAG.UnrollVectorOp(Op.getNode()); 1134 } 1135 1136 SDValue VectorLegalizer::ExpandCTLZ(SDValue Op) { 1137 // Attempt to expand using TargetLowering. 1138 SDValue Result; 1139 if (TLI.expandCTLZ(Op.getNode(), Result, DAG)) 1140 return Result; 1141 1142 // Otherwise go ahead and unroll. 1143 return DAG.UnrollVectorOp(Op.getNode()); 1144 } 1145 1146 SDValue VectorLegalizer::ExpandCTTZ(SDValue Op) { 1147 // Attempt to expand using TargetLowering. 1148 SDValue Result; 1149 if (TLI.expandCTTZ(Op.getNode(), Result, DAG)) 1150 return Result; 1151 1152 // Otherwise go ahead and unroll. 1153 return DAG.UnrollVectorOp(Op.getNode()); 1154 } 1155 1156 SDValue VectorLegalizer::ExpandFMINNUM_FMAXNUM(SDValue Op) { 1157 if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Op.getNode(), DAG)) 1158 return Expanded; 1159 return DAG.UnrollVectorOp(Op.getNode()); 1160 } 1161 1162 SDValue VectorLegalizer::ExpandStrictFPOp(SDValue Op) { 1163 EVT VT = Op.getValueType(); 1164 EVT EltVT = VT.getVectorElementType(); 1165 unsigned NumElems = VT.getVectorNumElements(); 1166 unsigned NumOpers = Op.getNumOperands(); 1167 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 1168 EVT ValueVTs[] = {EltVT, MVT::Other}; 1169 SDValue Chain = Op.getOperand(0); 1170 SDLoc dl(Op); 1171 1172 SmallVector<SDValue, 32> OpValues; 1173 SmallVector<SDValue, 32> OpChains; 1174 for (unsigned i = 0; i < NumElems; ++i) { 1175 SmallVector<SDValue, 4> Opers; 1176 SDValue Idx = DAG.getConstant(i, dl, 1177 TLI.getVectorIdxTy(DAG.getDataLayout())); 1178 1179 // The Chain is the first operand. 1180 Opers.push_back(Chain); 1181 1182 // Now process the remaining operands. 1183 for (unsigned j = 1; j < NumOpers; ++j) { 1184 SDValue Oper = Op.getOperand(j); 1185 EVT OperVT = Oper.getValueType(); 1186 1187 if (OperVT.isVector()) 1188 Oper = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, 1189 EltVT, Oper, Idx); 1190 1191 Opers.push_back(Oper); 1192 } 1193 1194 SDValue ScalarOp = DAG.getNode(Op->getOpcode(), dl, ValueVTs, Opers); 1195 1196 OpValues.push_back(ScalarOp.getValue(0)); 1197 OpChains.push_back(ScalarOp.getValue(1)); 1198 } 1199 1200 SDValue Result = DAG.getBuildVector(VT, dl, OpValues); 1201 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OpChains); 1202 1203 AddLegalizedOperand(Op.getValue(0), Result); 1204 AddLegalizedOperand(Op.getValue(1), NewChain); 1205 1206 return Op.getResNo() ? NewChain : Result; 1207 } 1208 1209 SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) { 1210 EVT VT = Op.getValueType(); 1211 unsigned NumElems = VT.getVectorNumElements(); 1212 EVT EltVT = VT.getVectorElementType(); 1213 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2); 1214 EVT TmpEltVT = LHS.getValueType().getVectorElementType(); 1215 SDLoc dl(Op); 1216 SmallVector<SDValue, 8> Ops(NumElems); 1217 for (unsigned i = 0; i < NumElems; ++i) { 1218 SDValue LHSElem = DAG.getNode( 1219 ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS, 1220 DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))); 1221 SDValue RHSElem = DAG.getNode( 1222 ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS, 1223 DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))); 1224 Ops[i] = DAG.getNode(ISD::SETCC, dl, 1225 TLI.getSetCCResultType(DAG.getDataLayout(), 1226 *DAG.getContext(), TmpEltVT), 1227 LHSElem, RHSElem, CC); 1228 Ops[i] = DAG.getSelect(dl, EltVT, Ops[i], 1229 DAG.getConstant(APInt::getAllOnesValue 1230 (EltVT.getSizeInBits()), dl, EltVT), 1231 DAG.getConstant(0, dl, EltVT)); 1232 } 1233 return DAG.getBuildVector(VT, dl, Ops); 1234 } 1235 1236 bool SelectionDAG::LegalizeVectors() { 1237 return VectorLegalizer(*this).Run(); 1238 } 1239