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