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