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