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