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 // "Promote" the operation by extending the operand. 506 return PromoteINT_TO_FP(Op); 507 case ISD::FP_TO_UINT: 508 case ISD::FP_TO_SINT: 509 case ISD::STRICT_FP_TO_UINT: 510 case ISD::STRICT_FP_TO_SINT: 511 // Promote the operation by extending the operand. 512 return PromoteFP_TO_INT(Op); 513 } 514 515 // There are currently two cases of vector promotion: 516 // 1) Bitcasting a vector of integers to a different type to a vector of the 517 // same overall length. For example, x86 promotes ISD::AND v2i32 to v1i64. 518 // 2) Extending a vector of floats to a vector of the same number of larger 519 // floats. For example, AArch64 promotes ISD::FADD on v4f16 to v4f32. 520 MVT VT = Op.getSimpleValueType(); 521 assert(Op.getNode()->getNumValues() == 1 && 522 "Can't promote a vector with multiple results!"); 523 MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT); 524 SDLoc dl(Op); 525 SmallVector<SDValue, 4> Operands(Op.getNumOperands()); 526 527 for (unsigned j = 0; j != Op.getNumOperands(); ++j) { 528 if (Op.getOperand(j).getValueType().isVector()) 529 if (Op.getOperand(j) 530 .getValueType() 531 .getVectorElementType() 532 .isFloatingPoint() && 533 NVT.isVector() && NVT.getVectorElementType().isFloatingPoint()) 534 Operands[j] = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Op.getOperand(j)); 535 else 536 Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j)); 537 else 538 Operands[j] = Op.getOperand(j); 539 } 540 541 Op = DAG.getNode(Op.getOpcode(), dl, NVT, Operands, Op.getNode()->getFlags()); 542 if ((VT.isFloatingPoint() && NVT.isFloatingPoint()) || 543 (VT.isVector() && VT.getVectorElementType().isFloatingPoint() && 544 NVT.isVector() && NVT.getVectorElementType().isFloatingPoint())) 545 return DAG.getNode(ISD::FP_ROUND, dl, VT, Op, DAG.getIntPtrConstant(0, dl)); 546 else 547 return DAG.getNode(ISD::BITCAST, dl, VT, Op); 548 } 549 550 SDValue VectorLegalizer::PromoteINT_TO_FP(SDValue Op) { 551 // INT_TO_FP operations may require the input operand be promoted even 552 // when the type is otherwise legal. 553 MVT VT = Op.getOperand(0).getSimpleValueType(); 554 MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT); 555 assert(NVT.getVectorNumElements() == VT.getVectorNumElements() && 556 "Vectors have different number of elements!"); 557 558 SDLoc dl(Op); 559 SmallVector<SDValue, 4> Operands(Op.getNumOperands()); 560 561 unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND : 562 ISD::SIGN_EXTEND; 563 for (unsigned j = 0; j != Op.getNumOperands(); ++j) { 564 if (Op.getOperand(j).getValueType().isVector()) 565 Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j)); 566 else 567 Operands[j] = Op.getOperand(j); 568 } 569 570 return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), Operands); 571 } 572 573 // For FP_TO_INT we promote the result type to a vector type with wider 574 // elements and then truncate the result. This is different from the default 575 // PromoteVector which uses bitcast to promote thus assumning that the 576 // promoted vector type has the same overall size. 577 SDValue VectorLegalizer::PromoteFP_TO_INT(SDValue Op) { 578 MVT VT = Op.getSimpleValueType(); 579 MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT); 580 bool IsStrict = Op->isStrictFPOpcode(); 581 assert(NVT.getVectorNumElements() == VT.getVectorNumElements() && 582 "Vectors have different number of elements!"); 583 584 unsigned NewOpc = Op->getOpcode(); 585 // Change FP_TO_UINT to FP_TO_SINT if possible. 586 // TODO: Should we only do this if FP_TO_UINT itself isn't legal? 587 if (NewOpc == ISD::FP_TO_UINT && 588 TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT)) 589 NewOpc = ISD::FP_TO_SINT; 590 591 if (NewOpc == ISD::STRICT_FP_TO_UINT && 592 TLI.isOperationLegalOrCustom(ISD::STRICT_FP_TO_SINT, NVT)) 593 NewOpc = ISD::STRICT_FP_TO_SINT; 594 595 SDLoc dl(Op); 596 SDValue Promoted, Chain; 597 if (IsStrict) { 598 Promoted = DAG.getNode(NewOpc, dl, {NVT, MVT::Other}, 599 {Op.getOperand(0), Op.getOperand(1)}); 600 Chain = Promoted.getValue(1); 601 } else 602 Promoted = DAG.getNode(NewOpc, dl, NVT, Op.getOperand(0)); 603 604 // Assert that the converted value fits in the original type. If it doesn't 605 // (eg: because the value being converted is too big), then the result of the 606 // original operation was undefined anyway, so the assert is still correct. 607 if (Op->getOpcode() == ISD::FP_TO_UINT || 608 Op->getOpcode() == ISD::STRICT_FP_TO_UINT) 609 NewOpc = ISD::AssertZext; 610 else 611 NewOpc = ISD::AssertSext; 612 613 Promoted = DAG.getNode(NewOpc, dl, NVT, Promoted, 614 DAG.getValueType(VT.getScalarType())); 615 Promoted = DAG.getNode(ISD::TRUNCATE, dl, VT, Promoted); 616 if (IsStrict) 617 return DAG.getMergeValues({Promoted, Chain}, dl); 618 619 return Promoted; 620 } 621 622 SDValue VectorLegalizer::ExpandLoad(SDValue Op) { 623 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode()); 624 625 EVT SrcVT = LD->getMemoryVT(); 626 EVT SrcEltVT = SrcVT.getScalarType(); 627 unsigned NumElem = SrcVT.getVectorNumElements(); 628 629 SDValue NewChain; 630 SDValue Value; 631 if (SrcVT.getVectorNumElements() > 1 && !SrcEltVT.isByteSized()) { 632 SDLoc dl(Op); 633 634 SmallVector<SDValue, 8> Vals; 635 SmallVector<SDValue, 8> LoadChains; 636 637 EVT DstEltVT = LD->getValueType(0).getScalarType(); 638 SDValue Chain = LD->getChain(); 639 SDValue BasePTR = LD->getBasePtr(); 640 ISD::LoadExtType ExtType = LD->getExtensionType(); 641 642 // When elements in a vector is not byte-addressable, we cannot directly 643 // load each element by advancing pointer, which could only address bytes. 644 // Instead, we load all significant words, mask bits off, and concatenate 645 // them to form each element. Finally, they are extended to destination 646 // scalar type to build the destination vector. 647 EVT WideVT = TLI.getPointerTy(DAG.getDataLayout()); 648 649 assert(WideVT.isRound() && 650 "Could not handle the sophisticated case when the widest integer is" 651 " not power of 2."); 652 assert(WideVT.bitsGE(SrcEltVT) && 653 "Type is not legalized?"); 654 655 unsigned WideBytes = WideVT.getStoreSize(); 656 unsigned Offset = 0; 657 unsigned RemainingBytes = SrcVT.getStoreSize(); 658 SmallVector<SDValue, 8> LoadVals; 659 while (RemainingBytes > 0) { 660 SDValue ScalarLoad; 661 unsigned LoadBytes = WideBytes; 662 663 if (RemainingBytes >= LoadBytes) { 664 ScalarLoad = 665 DAG.getLoad(WideVT, dl, Chain, BasePTR, 666 LD->getPointerInfo().getWithOffset(Offset), 667 MinAlign(LD->getAlignment(), Offset), 668 LD->getMemOperand()->getFlags(), LD->getAAInfo()); 669 } else { 670 EVT LoadVT = WideVT; 671 while (RemainingBytes < LoadBytes) { 672 LoadBytes >>= 1; // Reduce the load size by half. 673 LoadVT = EVT::getIntegerVT(*DAG.getContext(), LoadBytes << 3); 674 } 675 ScalarLoad = 676 DAG.getExtLoad(ISD::EXTLOAD, dl, WideVT, Chain, BasePTR, 677 LD->getPointerInfo().getWithOffset(Offset), LoadVT, 678 MinAlign(LD->getAlignment(), Offset), 679 LD->getMemOperand()->getFlags(), LD->getAAInfo()); 680 } 681 682 RemainingBytes -= LoadBytes; 683 Offset += LoadBytes; 684 685 BasePTR = DAG.getObjectPtrOffset(dl, BasePTR, LoadBytes); 686 687 LoadVals.push_back(ScalarLoad.getValue(0)); 688 LoadChains.push_back(ScalarLoad.getValue(1)); 689 } 690 691 unsigned BitOffset = 0; 692 unsigned WideIdx = 0; 693 unsigned WideBits = WideVT.getSizeInBits(); 694 695 // Extract bits, pack and extend/trunc them into destination type. 696 unsigned SrcEltBits = SrcEltVT.getSizeInBits(); 697 SDValue SrcEltBitMask = DAG.getConstant( 698 APInt::getLowBitsSet(WideBits, SrcEltBits), dl, WideVT); 699 700 for (unsigned Idx = 0; Idx != NumElem; ++Idx) { 701 assert(BitOffset < WideBits && "Unexpected offset!"); 702 703 SDValue ShAmt = DAG.getConstant( 704 BitOffset, dl, TLI.getShiftAmountTy(WideVT, DAG.getDataLayout())); 705 SDValue Lo = DAG.getNode(ISD::SRL, dl, WideVT, LoadVals[WideIdx], ShAmt); 706 707 BitOffset += SrcEltBits; 708 if (BitOffset >= WideBits) { 709 WideIdx++; 710 BitOffset -= WideBits; 711 if (BitOffset > 0) { 712 ShAmt = DAG.getConstant( 713 SrcEltBits - BitOffset, dl, 714 TLI.getShiftAmountTy(WideVT, DAG.getDataLayout())); 715 SDValue Hi = 716 DAG.getNode(ISD::SHL, dl, WideVT, LoadVals[WideIdx], ShAmt); 717 Lo = DAG.getNode(ISD::OR, dl, WideVT, Lo, Hi); 718 } 719 } 720 721 Lo = DAG.getNode(ISD::AND, dl, WideVT, Lo, SrcEltBitMask); 722 723 switch (ExtType) { 724 default: llvm_unreachable("Unknown extended-load op!"); 725 case ISD::EXTLOAD: 726 Lo = DAG.getAnyExtOrTrunc(Lo, dl, DstEltVT); 727 break; 728 case ISD::ZEXTLOAD: 729 Lo = DAG.getZExtOrTrunc(Lo, dl, DstEltVT); 730 break; 731 case ISD::SEXTLOAD: 732 ShAmt = 733 DAG.getConstant(WideBits - SrcEltBits, dl, 734 TLI.getShiftAmountTy(WideVT, DAG.getDataLayout())); 735 Lo = DAG.getNode(ISD::SHL, dl, WideVT, Lo, ShAmt); 736 Lo = DAG.getNode(ISD::SRA, dl, WideVT, Lo, ShAmt); 737 Lo = DAG.getSExtOrTrunc(Lo, dl, DstEltVT); 738 break; 739 } 740 Vals.push_back(Lo); 741 } 742 743 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains); 744 Value = DAG.getBuildVector(Op.getNode()->getValueType(0), dl, Vals); 745 } else { 746 SDValue Scalarized = TLI.scalarizeVectorLoad(LD, DAG); 747 // Skip past MERGE_VALUE node if known. 748 if (Scalarized->getOpcode() == ISD::MERGE_VALUES) { 749 NewChain = Scalarized.getOperand(1); 750 Value = Scalarized.getOperand(0); 751 } else { 752 NewChain = Scalarized.getValue(1); 753 Value = Scalarized.getValue(0); 754 } 755 } 756 757 AddLegalizedOperand(Op.getValue(0), Value); 758 AddLegalizedOperand(Op.getValue(1), NewChain); 759 760 return (Op.getResNo() ? NewChain : Value); 761 } 762 763 SDValue VectorLegalizer::ExpandStore(SDValue Op) { 764 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode()); 765 SDValue TF = TLI.scalarizeVectorStore(ST, DAG); 766 AddLegalizedOperand(Op, TF); 767 return TF; 768 } 769 770 SDValue VectorLegalizer::Expand(SDValue Op) { 771 switch (Op->getOpcode()) { 772 case ISD::SIGN_EXTEND_INREG: 773 return ExpandSEXTINREG(Op); 774 case ISD::ANY_EXTEND_VECTOR_INREG: 775 return ExpandANY_EXTEND_VECTOR_INREG(Op); 776 case ISD::SIGN_EXTEND_VECTOR_INREG: 777 return ExpandSIGN_EXTEND_VECTOR_INREG(Op); 778 case ISD::ZERO_EXTEND_VECTOR_INREG: 779 return ExpandZERO_EXTEND_VECTOR_INREG(Op); 780 case ISD::BSWAP: 781 return ExpandBSWAP(Op); 782 case ISD::VSELECT: 783 return ExpandVSELECT(Op); 784 case ISD::SELECT: 785 return ExpandSELECT(Op); 786 case ISD::FP_TO_UINT: 787 return ExpandFP_TO_UINT(Op); 788 case ISD::UINT_TO_FP: 789 return ExpandUINT_TO_FLOAT(Op); 790 case ISD::FNEG: 791 return ExpandFNEG(Op); 792 case ISD::FSUB: 793 return ExpandFSUB(Op); 794 case ISD::SETCC: 795 return UnrollVSETCC(Op); 796 case ISD::ABS: 797 return ExpandABS(Op); 798 case ISD::BITREVERSE: 799 return ExpandBITREVERSE(Op); 800 case ISD::CTPOP: 801 return ExpandCTPOP(Op); 802 case ISD::CTLZ: 803 case ISD::CTLZ_ZERO_UNDEF: 804 return ExpandCTLZ(Op); 805 case ISD::CTTZ: 806 case ISD::CTTZ_ZERO_UNDEF: 807 return ExpandCTTZ(Op); 808 case ISD::FSHL: 809 case ISD::FSHR: 810 return ExpandFunnelShift(Op); 811 case ISD::ROTL: 812 case ISD::ROTR: 813 return ExpandROT(Op); 814 case ISD::FMINNUM: 815 case ISD::FMAXNUM: 816 return ExpandFMINNUM_FMAXNUM(Op); 817 case ISD::UADDO: 818 case ISD::USUBO: 819 return ExpandUADDSUBO(Op); 820 case ISD::SADDO: 821 case ISD::SSUBO: 822 return ExpandSADDSUBO(Op); 823 case ISD::UMULO: 824 case ISD::SMULO: 825 return ExpandMULO(Op); 826 case ISD::USUBSAT: 827 case ISD::SSUBSAT: 828 case ISD::UADDSAT: 829 case ISD::SADDSAT: 830 return ExpandAddSubSat(Op); 831 case ISD::SMULFIX: 832 case ISD::UMULFIX: 833 return ExpandFixedPointMul(Op); 834 case ISD::SMULFIXSAT: 835 case ISD::UMULFIXSAT: 836 // FIXME: We do not expand SMULFIXSAT/UMULFIXSAT here yet, not sure exactly 837 // why. Maybe it results in worse codegen compared to the unroll for some 838 // targets? This should probably be investigated. And if we still prefer to 839 // unroll an explanation could be helpful. 840 return DAG.UnrollVectorOp(Op.getNode()); 841 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \ 842 case ISD::STRICT_##DAGN: 843 #include "llvm/IR/ConstrainedOps.def" 844 return ExpandStrictFPOp(Op); 845 case ISD::VECREDUCE_ADD: 846 case ISD::VECREDUCE_MUL: 847 case ISD::VECREDUCE_AND: 848 case ISD::VECREDUCE_OR: 849 case ISD::VECREDUCE_XOR: 850 case ISD::VECREDUCE_SMAX: 851 case ISD::VECREDUCE_SMIN: 852 case ISD::VECREDUCE_UMAX: 853 case ISD::VECREDUCE_UMIN: 854 case ISD::VECREDUCE_FADD: 855 case ISD::VECREDUCE_FMUL: 856 case ISD::VECREDUCE_FMAX: 857 case ISD::VECREDUCE_FMIN: 858 return TLI.expandVecReduce(Op.getNode(), DAG); 859 default: 860 return DAG.UnrollVectorOp(Op.getNode()); 861 } 862 } 863 864 SDValue VectorLegalizer::ExpandSELECT(SDValue Op) { 865 // Lower a select instruction where the condition is a scalar and the 866 // operands are vectors. Lower this select to VSELECT and implement it 867 // using XOR AND OR. The selector bit is broadcasted. 868 EVT VT = Op.getValueType(); 869 SDLoc DL(Op); 870 871 SDValue Mask = Op.getOperand(0); 872 SDValue Op1 = Op.getOperand(1); 873 SDValue Op2 = Op.getOperand(2); 874 875 assert(VT.isVector() && !Mask.getValueType().isVector() 876 && Op1.getValueType() == Op2.getValueType() && "Invalid type"); 877 878 // If we can't even use the basic vector operations of 879 // AND,OR,XOR, we will have to scalarize the op. 880 // Notice that the operation may be 'promoted' which means that it is 881 // 'bitcasted' to another type which is handled. 882 // Also, we need to be able to construct a splat vector using BUILD_VECTOR. 883 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand || 884 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand || 885 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand || 886 TLI.getOperationAction(ISD::BUILD_VECTOR, VT) == TargetLowering::Expand) 887 return DAG.UnrollVectorOp(Op.getNode()); 888 889 // Generate a mask operand. 890 EVT MaskTy = VT.changeVectorElementTypeToInteger(); 891 892 // What is the size of each element in the vector mask. 893 EVT BitTy = MaskTy.getScalarType(); 894 895 Mask = DAG.getSelect(DL, BitTy, Mask, 896 DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), DL, 897 BitTy), 898 DAG.getConstant(0, DL, BitTy)); 899 900 // Broadcast the mask so that the entire vector is all-one or all zero. 901 Mask = DAG.getSplatBuildVector(MaskTy, DL, Mask); 902 903 // Bitcast the operands to be the same type as the mask. 904 // This is needed when we select between FP types because 905 // the mask is a vector of integers. 906 Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1); 907 Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2); 908 909 SDValue AllOnes = DAG.getConstant( 910 APInt::getAllOnesValue(BitTy.getSizeInBits()), DL, MaskTy); 911 SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes); 912 913 Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask); 914 Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask); 915 SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2); 916 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val); 917 } 918 919 SDValue VectorLegalizer::ExpandSEXTINREG(SDValue Op) { 920 EVT VT = Op.getValueType(); 921 922 // Make sure that the SRA and SHL instructions are available. 923 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand || 924 TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand) 925 return DAG.UnrollVectorOp(Op.getNode()); 926 927 SDLoc DL(Op); 928 EVT OrigTy = cast<VTSDNode>(Op->getOperand(1))->getVT(); 929 930 unsigned BW = VT.getScalarSizeInBits(); 931 unsigned OrigBW = OrigTy.getScalarSizeInBits(); 932 SDValue ShiftSz = DAG.getConstant(BW - OrigBW, DL, VT); 933 934 Op = Op.getOperand(0); 935 Op = DAG.getNode(ISD::SHL, DL, VT, Op, ShiftSz); 936 return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz); 937 } 938 939 // Generically expand a vector anyext in register to a shuffle of the relevant 940 // lanes into the appropriate locations, with other lanes left undef. 941 SDValue VectorLegalizer::ExpandANY_EXTEND_VECTOR_INREG(SDValue Op) { 942 SDLoc DL(Op); 943 EVT VT = Op.getValueType(); 944 int NumElements = VT.getVectorNumElements(); 945 SDValue Src = Op.getOperand(0); 946 EVT SrcVT = Src.getValueType(); 947 int NumSrcElements = SrcVT.getVectorNumElements(); 948 949 // *_EXTEND_VECTOR_INREG SrcVT can be smaller than VT - so insert the vector 950 // into a larger vector type. 951 if (SrcVT.bitsLE(VT)) { 952 assert((VT.getSizeInBits() % SrcVT.getScalarSizeInBits()) == 0 && 953 "ANY_EXTEND_VECTOR_INREG vector size mismatch"); 954 NumSrcElements = VT.getSizeInBits() / SrcVT.getScalarSizeInBits(); 955 SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getScalarType(), 956 NumSrcElements); 957 Src = DAG.getNode( 958 ISD::INSERT_SUBVECTOR, DL, SrcVT, DAG.getUNDEF(SrcVT), Src, 959 DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))); 960 } 961 962 // Build a base mask of undef shuffles. 963 SmallVector<int, 16> ShuffleMask; 964 ShuffleMask.resize(NumSrcElements, -1); 965 966 // Place the extended lanes into the correct locations. 967 int ExtLaneScale = NumSrcElements / NumElements; 968 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0; 969 for (int i = 0; i < NumElements; ++i) 970 ShuffleMask[i * ExtLaneScale + EndianOffset] = i; 971 972 return DAG.getNode( 973 ISD::BITCAST, DL, VT, 974 DAG.getVectorShuffle(SrcVT, DL, Src, DAG.getUNDEF(SrcVT), ShuffleMask)); 975 } 976 977 SDValue VectorLegalizer::ExpandSIGN_EXTEND_VECTOR_INREG(SDValue Op) { 978 SDLoc DL(Op); 979 EVT VT = Op.getValueType(); 980 SDValue Src = Op.getOperand(0); 981 EVT SrcVT = Src.getValueType(); 982 983 // First build an any-extend node which can be legalized above when we 984 // recurse through it. 985 Op = DAG.getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Src); 986 987 // Now we need sign extend. Do this by shifting the elements. Even if these 988 // aren't legal operations, they have a better chance of being legalized 989 // without full scalarization than the sign extension does. 990 unsigned EltWidth = VT.getScalarSizeInBits(); 991 unsigned SrcEltWidth = SrcVT.getScalarSizeInBits(); 992 SDValue ShiftAmount = DAG.getConstant(EltWidth - SrcEltWidth, DL, VT); 993 return DAG.getNode(ISD::SRA, DL, VT, 994 DAG.getNode(ISD::SHL, DL, VT, Op, ShiftAmount), 995 ShiftAmount); 996 } 997 998 // Generically expand a vector zext in register to a shuffle of the relevant 999 // lanes into the appropriate locations, a blend of zero into the high bits, 1000 // and a bitcast to the wider element type. 1001 SDValue VectorLegalizer::ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op) { 1002 SDLoc DL(Op); 1003 EVT VT = Op.getValueType(); 1004 int NumElements = VT.getVectorNumElements(); 1005 SDValue Src = Op.getOperand(0); 1006 EVT SrcVT = Src.getValueType(); 1007 int NumSrcElements = SrcVT.getVectorNumElements(); 1008 1009 // *_EXTEND_VECTOR_INREG SrcVT can be smaller than VT - so insert the vector 1010 // into a larger vector type. 1011 if (SrcVT.bitsLE(VT)) { 1012 assert((VT.getSizeInBits() % SrcVT.getScalarSizeInBits()) == 0 && 1013 "ZERO_EXTEND_VECTOR_INREG vector size mismatch"); 1014 NumSrcElements = VT.getSizeInBits() / SrcVT.getScalarSizeInBits(); 1015 SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getScalarType(), 1016 NumSrcElements); 1017 Src = DAG.getNode( 1018 ISD::INSERT_SUBVECTOR, DL, SrcVT, DAG.getUNDEF(SrcVT), Src, 1019 DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))); 1020 } 1021 1022 // Build up a zero vector to blend into this one. 1023 SDValue Zero = DAG.getConstant(0, DL, SrcVT); 1024 1025 // Shuffle the incoming lanes into the correct position, and pull all other 1026 // lanes from the zero vector. 1027 SmallVector<int, 16> ShuffleMask; 1028 ShuffleMask.reserve(NumSrcElements); 1029 for (int i = 0; i < NumSrcElements; ++i) 1030 ShuffleMask.push_back(i); 1031 1032 int ExtLaneScale = NumSrcElements / NumElements; 1033 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0; 1034 for (int i = 0; i < NumElements; ++i) 1035 ShuffleMask[i * ExtLaneScale + EndianOffset] = NumSrcElements + i; 1036 1037 return DAG.getNode(ISD::BITCAST, DL, VT, 1038 DAG.getVectorShuffle(SrcVT, DL, Zero, Src, ShuffleMask)); 1039 } 1040 1041 static void createBSWAPShuffleMask(EVT VT, SmallVectorImpl<int> &ShuffleMask) { 1042 int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8; 1043 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) 1044 for (int J = ScalarSizeInBytes - 1; J >= 0; --J) 1045 ShuffleMask.push_back((I * ScalarSizeInBytes) + J); 1046 } 1047 1048 SDValue VectorLegalizer::ExpandBSWAP(SDValue Op) { 1049 EVT VT = Op.getValueType(); 1050 1051 // Generate a byte wise shuffle mask for the BSWAP. 1052 SmallVector<int, 16> ShuffleMask; 1053 createBSWAPShuffleMask(VT, ShuffleMask); 1054 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size()); 1055 1056 // Only emit a shuffle if the mask is legal. 1057 if (!TLI.isShuffleMaskLegal(ShuffleMask, ByteVT)) 1058 return DAG.UnrollVectorOp(Op.getNode()); 1059 1060 SDLoc DL(Op); 1061 Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Op.getOperand(0)); 1062 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), ShuffleMask); 1063 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 1064 } 1065 1066 SDValue VectorLegalizer::ExpandBITREVERSE(SDValue Op) { 1067 EVT VT = Op.getValueType(); 1068 1069 // If we have the scalar operation, it's probably cheaper to unroll it. 1070 if (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, VT.getScalarType())) 1071 return DAG.UnrollVectorOp(Op.getNode()); 1072 1073 // If the vector element width is a whole number of bytes, test if its legal 1074 // to BSWAP shuffle the bytes and then perform the BITREVERSE on the byte 1075 // vector. This greatly reduces the number of bit shifts necessary. 1076 unsigned ScalarSizeInBits = VT.getScalarSizeInBits(); 1077 if (ScalarSizeInBits > 8 && (ScalarSizeInBits % 8) == 0) { 1078 SmallVector<int, 16> BSWAPMask; 1079 createBSWAPShuffleMask(VT, BSWAPMask); 1080 1081 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, BSWAPMask.size()); 1082 if (TLI.isShuffleMaskLegal(BSWAPMask, ByteVT) && 1083 (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, ByteVT) || 1084 (TLI.isOperationLegalOrCustom(ISD::SHL, ByteVT) && 1085 TLI.isOperationLegalOrCustom(ISD::SRL, ByteVT) && 1086 TLI.isOperationLegalOrCustomOrPromote(ISD::AND, ByteVT) && 1087 TLI.isOperationLegalOrCustomOrPromote(ISD::OR, ByteVT)))) { 1088 SDLoc DL(Op); 1089 Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Op.getOperand(0)); 1090 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), 1091 BSWAPMask); 1092 Op = DAG.getNode(ISD::BITREVERSE, DL, ByteVT, Op); 1093 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 1094 } 1095 } 1096 1097 // If we have the appropriate vector bit operations, it is better to use them 1098 // than unrolling and expanding each component. 1099 if (!TLI.isOperationLegalOrCustom(ISD::SHL, VT) || 1100 !TLI.isOperationLegalOrCustom(ISD::SRL, VT) || 1101 !TLI.isOperationLegalOrCustomOrPromote(ISD::AND, VT) || 1102 !TLI.isOperationLegalOrCustomOrPromote(ISD::OR, VT)) 1103 return DAG.UnrollVectorOp(Op.getNode()); 1104 1105 // Let LegalizeDAG handle this later. 1106 return Op; 1107 } 1108 1109 SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) { 1110 // Implement VSELECT in terms of XOR, AND, OR 1111 // on platforms which do not support blend natively. 1112 SDLoc DL(Op); 1113 1114 SDValue Mask = Op.getOperand(0); 1115 SDValue Op1 = Op.getOperand(1); 1116 SDValue Op2 = Op.getOperand(2); 1117 1118 EVT VT = Mask.getValueType(); 1119 1120 // If we can't even use the basic vector operations of 1121 // AND,OR,XOR, we will have to scalarize the op. 1122 // Notice that the operation may be 'promoted' which means that it is 1123 // 'bitcasted' to another type which is handled. 1124 // This operation also isn't safe with AND, OR, XOR when the boolean 1125 // type is 0/1 as we need an all ones vector constant to mask with. 1126 // FIXME: Sign extend 1 to all ones if thats legal on the target. 1127 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand || 1128 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand || 1129 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand || 1130 TLI.getBooleanContents(Op1.getValueType()) != 1131 TargetLowering::ZeroOrNegativeOneBooleanContent) 1132 return DAG.UnrollVectorOp(Op.getNode()); 1133 1134 // If the mask and the type are different sizes, unroll the vector op. This 1135 // can occur when getSetCCResultType returns something that is different in 1136 // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8. 1137 if (VT.getSizeInBits() != Op1.getValueSizeInBits()) 1138 return DAG.UnrollVectorOp(Op.getNode()); 1139 1140 // Bitcast the operands to be the same type as the mask. 1141 // This is needed when we select between FP types because 1142 // the mask is a vector of integers. 1143 Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1); 1144 Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2); 1145 1146 SDValue AllOnes = DAG.getConstant( 1147 APInt::getAllOnesValue(VT.getScalarSizeInBits()), DL, VT); 1148 SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes); 1149 1150 Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask); 1151 Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask); 1152 SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2); 1153 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val); 1154 } 1155 1156 SDValue VectorLegalizer::ExpandABS(SDValue Op) { 1157 // Attempt to expand using TargetLowering. 1158 SDValue Result; 1159 if (TLI.expandABS(Op.getNode(), Result, DAG)) 1160 return Result; 1161 1162 // Otherwise go ahead and unroll. 1163 return DAG.UnrollVectorOp(Op.getNode()); 1164 } 1165 1166 SDValue VectorLegalizer::ExpandFP_TO_UINT(SDValue Op) { 1167 // Attempt to expand using TargetLowering. 1168 SDValue Result, Chain; 1169 if (TLI.expandFP_TO_UINT(Op.getNode(), Result, Chain, DAG)) { 1170 if (Op.getNode()->isStrictFPOpcode()) 1171 // Relink the chain 1172 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Chain); 1173 return Result; 1174 } 1175 1176 // Otherwise go ahead and unroll. 1177 return DAG.UnrollVectorOp(Op.getNode()); 1178 } 1179 1180 SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) { 1181 bool IsStrict = Op.getNode()->isStrictFPOpcode(); 1182 unsigned OpNo = IsStrict ? 1 : 0; 1183 SDValue Src = Op.getOperand(OpNo); 1184 EVT VT = Src.getValueType(); 1185 SDLoc DL(Op); 1186 1187 // Attempt to expand using TargetLowering. 1188 SDValue Result; 1189 SDValue Chain; 1190 if (TLI.expandUINT_TO_FP(Op.getNode(), Result, Chain, DAG)) { 1191 if (IsStrict) 1192 // Relink the chain 1193 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Chain); 1194 return Result; 1195 } 1196 1197 // Make sure that the SINT_TO_FP and SRL instructions are available. 1198 if (((!IsStrict && TLI.getOperationAction(ISD::SINT_TO_FP, VT) == 1199 TargetLowering::Expand) || 1200 (IsStrict && TLI.getOperationAction(ISD::STRICT_SINT_TO_FP, VT) == 1201 TargetLowering::Expand)) || 1202 TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand) 1203 return IsStrict ? SDValue() : DAG.UnrollVectorOp(Op.getNode()); 1204 1205 unsigned BW = VT.getScalarSizeInBits(); 1206 assert((BW == 64 || BW == 32) && 1207 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide"); 1208 1209 SDValue HalfWord = DAG.getConstant(BW / 2, DL, VT); 1210 1211 // Constants to clear the upper part of the word. 1212 // Notice that we can also use SHL+SHR, but using a constant is slightly 1213 // faster on x86. 1214 uint64_t HWMask = (BW == 64) ? 0x00000000FFFFFFFF : 0x0000FFFF; 1215 SDValue HalfWordMask = DAG.getConstant(HWMask, DL, VT); 1216 1217 // Two to the power of half-word-size. 1218 SDValue TWOHW = DAG.getConstantFP(1ULL << (BW / 2), DL, Op.getValueType()); 1219 1220 // Clear upper part of LO, lower HI 1221 SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Src, HalfWord); 1222 SDValue LO = DAG.getNode(ISD::AND, DL, VT, Src, HalfWordMask); 1223 1224 if (IsStrict) { 1225 // Convert hi and lo to floats 1226 // Convert the hi part back to the upper values 1227 // TODO: Can any fast-math-flags be set on these nodes? 1228 SDValue fHI = 1229 DAG.getNode(ISD::STRICT_SINT_TO_FP, DL, {Op.getValueType(), MVT::Other}, 1230 {Op.getOperand(0), HI}); 1231 fHI = DAG.getNode(ISD::STRICT_FMUL, DL, {Op.getValueType(), MVT::Other}, 1232 {SDValue(fHI.getNode(), 1), fHI, TWOHW}); 1233 SDValue fLO = 1234 DAG.getNode(ISD::STRICT_SINT_TO_FP, DL, {Op.getValueType(), MVT::Other}, 1235 {SDValue(fHI.getNode(), 1), LO}); 1236 1237 // Add the two halves 1238 SDValue Result = 1239 DAG.getNode(ISD::STRICT_FADD, DL, {Op.getValueType(), MVT::Other}, 1240 {SDValue(fLO.getNode(), 1), fHI, fLO}); 1241 1242 // Relink the chain 1243 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), SDValue(Result.getNode(), 1)); 1244 return Result; 1245 } 1246 1247 // Convert hi and lo to floats 1248 // Convert the hi part back to the upper values 1249 // TODO: Can any fast-math-flags be set on these nodes? 1250 SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI); 1251 fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW); 1252 SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO); 1253 1254 // Add the two halves 1255 return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO); 1256 } 1257 1258 SDValue VectorLegalizer::ExpandFNEG(SDValue Op) { 1259 if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) { 1260 SDLoc DL(Op); 1261 SDValue Zero = DAG.getConstantFP(-0.0, DL, Op.getValueType()); 1262 // TODO: If FNEG had fast-math-flags, they'd get propagated to this FSUB. 1263 return DAG.getNode(ISD::FSUB, DL, Op.getValueType(), 1264 Zero, Op.getOperand(0)); 1265 } 1266 return DAG.UnrollVectorOp(Op.getNode()); 1267 } 1268 1269 SDValue VectorLegalizer::ExpandFSUB(SDValue Op) { 1270 // For floating-point values, (a-b) is the same as a+(-b). If FNEG is legal, 1271 // we can defer this to operation legalization where it will be lowered as 1272 // a+(-b). 1273 EVT VT = Op.getValueType(); 1274 if (TLI.isOperationLegalOrCustom(ISD::FNEG, VT) && 1275 TLI.isOperationLegalOrCustom(ISD::FADD, VT)) 1276 return Op; // Defer to LegalizeDAG 1277 1278 return DAG.UnrollVectorOp(Op.getNode()); 1279 } 1280 1281 SDValue VectorLegalizer::ExpandCTPOP(SDValue Op) { 1282 SDValue Result; 1283 if (TLI.expandCTPOP(Op.getNode(), Result, DAG)) 1284 return Result; 1285 1286 return DAG.UnrollVectorOp(Op.getNode()); 1287 } 1288 1289 SDValue VectorLegalizer::ExpandCTLZ(SDValue Op) { 1290 SDValue Result; 1291 if (TLI.expandCTLZ(Op.getNode(), Result, DAG)) 1292 return Result; 1293 1294 return DAG.UnrollVectorOp(Op.getNode()); 1295 } 1296 1297 SDValue VectorLegalizer::ExpandCTTZ(SDValue Op) { 1298 SDValue Result; 1299 if (TLI.expandCTTZ(Op.getNode(), Result, DAG)) 1300 return Result; 1301 1302 return DAG.UnrollVectorOp(Op.getNode()); 1303 } 1304 1305 SDValue VectorLegalizer::ExpandFunnelShift(SDValue Op) { 1306 SDValue Result; 1307 if (TLI.expandFunnelShift(Op.getNode(), Result, DAG)) 1308 return Result; 1309 1310 return DAG.UnrollVectorOp(Op.getNode()); 1311 } 1312 1313 SDValue VectorLegalizer::ExpandROT(SDValue Op) { 1314 SDValue Result; 1315 if (TLI.expandROT(Op.getNode(), Result, DAG)) 1316 return Result; 1317 1318 return DAG.UnrollVectorOp(Op.getNode()); 1319 } 1320 1321 SDValue VectorLegalizer::ExpandFMINNUM_FMAXNUM(SDValue Op) { 1322 if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Op.getNode(), DAG)) 1323 return Expanded; 1324 return DAG.UnrollVectorOp(Op.getNode()); 1325 } 1326 1327 SDValue VectorLegalizer::ExpandUADDSUBO(SDValue Op) { 1328 SDValue Result, Overflow; 1329 TLI.expandUADDSUBO(Op.getNode(), Result, Overflow, DAG); 1330 1331 if (Op.getResNo() == 0) { 1332 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Overflow)); 1333 return Result; 1334 } else { 1335 AddLegalizedOperand(Op.getValue(0), LegalizeOp(Result)); 1336 return Overflow; 1337 } 1338 } 1339 1340 SDValue VectorLegalizer::ExpandSADDSUBO(SDValue Op) { 1341 SDValue Result, Overflow; 1342 TLI.expandSADDSUBO(Op.getNode(), Result, Overflow, DAG); 1343 1344 if (Op.getResNo() == 0) { 1345 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Overflow)); 1346 return Result; 1347 } else { 1348 AddLegalizedOperand(Op.getValue(0), LegalizeOp(Result)); 1349 return Overflow; 1350 } 1351 } 1352 1353 SDValue VectorLegalizer::ExpandMULO(SDValue Op) { 1354 SDValue Result, Overflow; 1355 if (!TLI.expandMULO(Op.getNode(), Result, Overflow, DAG)) 1356 std::tie(Result, Overflow) = DAG.UnrollVectorOverflowOp(Op.getNode()); 1357 1358 if (Op.getResNo() == 0) { 1359 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Overflow)); 1360 return Result; 1361 } else { 1362 AddLegalizedOperand(Op.getValue(0), LegalizeOp(Result)); 1363 return Overflow; 1364 } 1365 } 1366 1367 SDValue VectorLegalizer::ExpandAddSubSat(SDValue Op) { 1368 if (SDValue Expanded = TLI.expandAddSubSat(Op.getNode(), DAG)) 1369 return Expanded; 1370 return DAG.UnrollVectorOp(Op.getNode()); 1371 } 1372 1373 SDValue VectorLegalizer::ExpandFixedPointMul(SDValue Op) { 1374 if (SDValue Expanded = TLI.expandFixedPointMul(Op.getNode(), DAG)) 1375 return Expanded; 1376 return DAG.UnrollVectorOp(Op.getNode()); 1377 } 1378 1379 SDValue VectorLegalizer::ExpandStrictFPOp(SDValue Op) { 1380 if (Op.getOpcode() == ISD::STRICT_UINT_TO_FP) { 1381 if (SDValue Res = ExpandUINT_TO_FLOAT(Op)) 1382 return Res; 1383 } 1384 1385 EVT VT = Op.getValue(0).getValueType(); 1386 EVT EltVT = VT.getVectorElementType(); 1387 unsigned NumElems = VT.getVectorNumElements(); 1388 unsigned NumOpers = Op.getNumOperands(); 1389 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 1390 1391 EVT TmpEltVT = EltVT; 1392 if (Op->getOpcode() == ISD::STRICT_FSETCC || 1393 Op->getOpcode() == ISD::STRICT_FSETCCS) 1394 TmpEltVT = TLI.getSetCCResultType(DAG.getDataLayout(), 1395 *DAG.getContext(), TmpEltVT); 1396 1397 EVT ValueVTs[] = {TmpEltVT, MVT::Other}; 1398 SDValue Chain = Op.getOperand(0); 1399 SDLoc dl(Op); 1400 1401 SmallVector<SDValue, 32> OpValues; 1402 SmallVector<SDValue, 32> OpChains; 1403 for (unsigned i = 0; i < NumElems; ++i) { 1404 SmallVector<SDValue, 4> Opers; 1405 SDValue Idx = DAG.getConstant(i, dl, 1406 TLI.getVectorIdxTy(DAG.getDataLayout())); 1407 1408 // The Chain is the first operand. 1409 Opers.push_back(Chain); 1410 1411 // Now process the remaining operands. 1412 for (unsigned j = 1; j < NumOpers; ++j) { 1413 SDValue Oper = Op.getOperand(j); 1414 EVT OperVT = Oper.getValueType(); 1415 1416 if (OperVT.isVector()) 1417 Oper = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, 1418 OperVT.getVectorElementType(), Oper, Idx); 1419 1420 Opers.push_back(Oper); 1421 } 1422 1423 SDValue ScalarOp = DAG.getNode(Op->getOpcode(), dl, ValueVTs, Opers); 1424 SDValue ScalarResult = ScalarOp.getValue(0); 1425 SDValue ScalarChain = ScalarOp.getValue(1); 1426 1427 if (Op->getOpcode() == ISD::STRICT_FSETCC || 1428 Op->getOpcode() == ISD::STRICT_FSETCCS) 1429 ScalarResult = DAG.getSelect(dl, EltVT, ScalarResult, 1430 DAG.getConstant(APInt::getAllOnesValue 1431 (EltVT.getSizeInBits()), dl, EltVT), 1432 DAG.getConstant(0, dl, EltVT)); 1433 1434 OpValues.push_back(ScalarResult); 1435 OpChains.push_back(ScalarChain); 1436 } 1437 1438 SDValue Result = DAG.getBuildVector(VT, dl, OpValues); 1439 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OpChains); 1440 1441 AddLegalizedOperand(Op.getValue(0), Result); 1442 AddLegalizedOperand(Op.getValue(1), NewChain); 1443 1444 return Op.getResNo() ? NewChain : Result; 1445 } 1446 1447 SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) { 1448 EVT VT = Op.getValueType(); 1449 unsigned NumElems = VT.getVectorNumElements(); 1450 EVT EltVT = VT.getVectorElementType(); 1451 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2); 1452 EVT TmpEltVT = LHS.getValueType().getVectorElementType(); 1453 SDLoc dl(Op); 1454 SmallVector<SDValue, 8> Ops(NumElems); 1455 for (unsigned i = 0; i < NumElems; ++i) { 1456 SDValue LHSElem = DAG.getNode( 1457 ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS, 1458 DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))); 1459 SDValue RHSElem = DAG.getNode( 1460 ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS, 1461 DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))); 1462 Ops[i] = DAG.getNode(ISD::SETCC, dl, 1463 TLI.getSetCCResultType(DAG.getDataLayout(), 1464 *DAG.getContext(), TmpEltVT), 1465 LHSElem, RHSElem, CC); 1466 Ops[i] = DAG.getSelect(dl, EltVT, Ops[i], 1467 DAG.getConstant(APInt::getAllOnesValue 1468 (EltVT.getSizeInBits()), dl, EltVT), 1469 DAG.getConstant(0, dl, EltVT)); 1470 } 1471 return DAG.getBuildVector(VT, dl, Ops); 1472 } 1473 1474 bool SelectionDAG::LegalizeVectors() { 1475 return VectorLegalizer(*this).Run(); 1476 } 1477