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