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