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