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