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