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