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 } 493 494 LLVM_DEBUG(dbgs() << "\nLegalizing vector op: "; Node->dump(&DAG)); 495 496 SmallVector<SDValue, 8> ResultVals; 497 switch (Action) { 498 default: llvm_unreachable("This action is not supported yet!"); 499 case TargetLowering::Promote: 500 LLVM_DEBUG(dbgs() << "Promoting\n"); 501 Promote(Node, ResultVals); 502 assert(!ResultVals.empty() && "No results for promotion?"); 503 break; 504 case TargetLowering::Legal: 505 LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n"); 506 break; 507 case TargetLowering::Custom: 508 LLVM_DEBUG(dbgs() << "Trying custom legalization\n"); 509 if (LowerOperationWrapper(Node, ResultVals)) 510 break; 511 LLVM_DEBUG(dbgs() << "Could not custom legalize node\n"); 512 LLVM_FALLTHROUGH; 513 case TargetLowering::Expand: 514 LLVM_DEBUG(dbgs() << "Expanding\n"); 515 Expand(Node, ResultVals); 516 break; 517 } 518 519 if (ResultVals.empty()) 520 return TranslateLegalizeResults(Op, Node); 521 522 Changed = true; 523 return RecursivelyLegalizeResults(Op, ResultVals); 524 } 525 526 // FIME: This is very similar to the X86 override of 527 // TargetLowering::LowerOperationWrapper. Can we merge them somehow? 528 bool VectorLegalizer::LowerOperationWrapper(SDNode *Node, 529 SmallVectorImpl<SDValue> &Results) { 530 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG); 531 532 if (!Res.getNode()) 533 return false; 534 535 if (Res == SDValue(Node, 0)) 536 return true; 537 538 // If the original node has one result, take the return value from 539 // LowerOperation as is. It might not be result number 0. 540 if (Node->getNumValues() == 1) { 541 Results.push_back(Res); 542 return true; 543 } 544 545 // If the original node has multiple results, then the return node should 546 // have the same number of results. 547 assert((Node->getNumValues() == Res->getNumValues()) && 548 "Lowering returned the wrong number of results!"); 549 550 // Places new result values base on N result number. 551 for (unsigned I = 0, E = Node->getNumValues(); I != E; ++I) 552 Results.push_back(Res.getValue(I)); 553 554 return true; 555 } 556 557 void VectorLegalizer::Promote(SDNode *Node, SmallVectorImpl<SDValue> &Results) { 558 // For a few operations there is a specific concept for promotion based on 559 // the operand's type. 560 switch (Node->getOpcode()) { 561 case ISD::SINT_TO_FP: 562 case ISD::UINT_TO_FP: 563 case ISD::STRICT_SINT_TO_FP: 564 case ISD::STRICT_UINT_TO_FP: 565 // "Promote" the operation by extending the operand. 566 PromoteINT_TO_FP(Node, Results); 567 return; 568 case ISD::FP_TO_UINT: 569 case ISD::FP_TO_SINT: 570 case ISD::STRICT_FP_TO_UINT: 571 case ISD::STRICT_FP_TO_SINT: 572 // Promote the operation by extending the operand. 573 PromoteFP_TO_INT(Node, Results); 574 return; 575 case ISD::FP_ROUND: 576 case ISD::FP_EXTEND: 577 // These operations are used to do promotion so they can't be promoted 578 // themselves. 579 llvm_unreachable("Don't know how to promote this operation!"); 580 } 581 582 // There are currently two cases of vector promotion: 583 // 1) Bitcasting a vector of integers to a different type to a vector of the 584 // same overall length. For example, x86 promotes ISD::AND v2i32 to v1i64. 585 // 2) Extending a vector of floats to a vector of the same number of larger 586 // floats. For example, AArch64 promotes ISD::FADD on v4f16 to v4f32. 587 assert(Node->getNumValues() == 1 && 588 "Can't promote a vector with multiple results!"); 589 MVT VT = Node->getSimpleValueType(0); 590 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); 591 SDLoc dl(Node); 592 SmallVector<SDValue, 4> Operands(Node->getNumOperands()); 593 594 for (unsigned j = 0; j != Node->getNumOperands(); ++j) { 595 if (Node->getOperand(j).getValueType().isVector()) 596 if (Node->getOperand(j) 597 .getValueType() 598 .getVectorElementType() 599 .isFloatingPoint() && 600 NVT.isVector() && NVT.getVectorElementType().isFloatingPoint()) 601 Operands[j] = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(j)); 602 else 603 Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(j)); 604 else 605 Operands[j] = Node->getOperand(j); 606 } 607 608 SDValue Res = 609 DAG.getNode(Node->getOpcode(), dl, NVT, Operands, Node->getFlags()); 610 611 if ((VT.isFloatingPoint() && NVT.isFloatingPoint()) || 612 (VT.isVector() && VT.getVectorElementType().isFloatingPoint() && 613 NVT.isVector() && NVT.getVectorElementType().isFloatingPoint())) 614 Res = DAG.getNode(ISD::FP_ROUND, dl, VT, Res, DAG.getIntPtrConstant(0, dl)); 615 else 616 Res = DAG.getNode(ISD::BITCAST, dl, VT, Res); 617 618 Results.push_back(Res); 619 } 620 621 void VectorLegalizer::PromoteINT_TO_FP(SDNode *Node, 622 SmallVectorImpl<SDValue> &Results) { 623 // INT_TO_FP operations may require the input operand be promoted even 624 // when the type is otherwise legal. 625 bool IsStrict = Node->isStrictFPOpcode(); 626 MVT VT = Node->getOperand(IsStrict ? 1 : 0).getSimpleValueType(); 627 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); 628 assert(NVT.getVectorNumElements() == VT.getVectorNumElements() && 629 "Vectors have different number of elements!"); 630 631 SDLoc dl(Node); 632 SmallVector<SDValue, 4> Operands(Node->getNumOperands()); 633 634 unsigned Opc = (Node->getOpcode() == ISD::UINT_TO_FP || 635 Node->getOpcode() == ISD::STRICT_UINT_TO_FP) 636 ? ISD::ZERO_EXTEND 637 : ISD::SIGN_EXTEND; 638 for (unsigned j = 0; j != Node->getNumOperands(); ++j) { 639 if (Node->getOperand(j).getValueType().isVector()) 640 Operands[j] = DAG.getNode(Opc, dl, NVT, Node->getOperand(j)); 641 else 642 Operands[j] = Node->getOperand(j); 643 } 644 645 if (IsStrict) { 646 SDValue Res = DAG.getNode(Node->getOpcode(), dl, 647 {Node->getValueType(0), MVT::Other}, Operands); 648 Results.push_back(Res); 649 Results.push_back(Res.getValue(1)); 650 return; 651 } 652 653 SDValue Res = 654 DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Operands); 655 Results.push_back(Res); 656 } 657 658 // For FP_TO_INT we promote the result type to a vector type with wider 659 // elements and then truncate the result. This is different from the default 660 // PromoteVector which uses bitcast to promote thus assumning that the 661 // promoted vector type has the same overall size. 662 void VectorLegalizer::PromoteFP_TO_INT(SDNode *Node, 663 SmallVectorImpl<SDValue> &Results) { 664 MVT VT = Node->getSimpleValueType(0); 665 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); 666 bool IsStrict = Node->isStrictFPOpcode(); 667 assert(NVT.getVectorNumElements() == VT.getVectorNumElements() && 668 "Vectors have different number of elements!"); 669 670 unsigned NewOpc = Node->getOpcode(); 671 // Change FP_TO_UINT to FP_TO_SINT if possible. 672 // TODO: Should we only do this if FP_TO_UINT itself isn't legal? 673 if (NewOpc == ISD::FP_TO_UINT && 674 TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT)) 675 NewOpc = ISD::FP_TO_SINT; 676 677 if (NewOpc == ISD::STRICT_FP_TO_UINT && 678 TLI.isOperationLegalOrCustom(ISD::STRICT_FP_TO_SINT, NVT)) 679 NewOpc = ISD::STRICT_FP_TO_SINT; 680 681 SDLoc dl(Node); 682 SDValue Promoted, Chain; 683 if (IsStrict) { 684 Promoted = DAG.getNode(NewOpc, dl, {NVT, MVT::Other}, 685 {Node->getOperand(0), Node->getOperand(1)}); 686 Chain = Promoted.getValue(1); 687 } else 688 Promoted = DAG.getNode(NewOpc, dl, NVT, Node->getOperand(0)); 689 690 // Assert that the converted value fits in the original type. If it doesn't 691 // (eg: because the value being converted is too big), then the result of the 692 // original operation was undefined anyway, so the assert is still correct. 693 if (Node->getOpcode() == ISD::FP_TO_UINT || 694 Node->getOpcode() == ISD::STRICT_FP_TO_UINT) 695 NewOpc = ISD::AssertZext; 696 else 697 NewOpc = ISD::AssertSext; 698 699 Promoted = DAG.getNode(NewOpc, dl, NVT, Promoted, 700 DAG.getValueType(VT.getScalarType())); 701 Promoted = DAG.getNode(ISD::TRUNCATE, dl, VT, Promoted); 702 Results.push_back(Promoted); 703 if (IsStrict) 704 Results.push_back(Chain); 705 } 706 707 std::pair<SDValue, SDValue> VectorLegalizer::ExpandLoad(SDNode *N) { 708 LoadSDNode *LD = cast<LoadSDNode>(N); 709 return TLI.scalarizeVectorLoad(LD, DAG); 710 } 711 712 SDValue VectorLegalizer::ExpandStore(SDNode *N) { 713 StoreSDNode *ST = cast<StoreSDNode>(N); 714 SDValue TF = TLI.scalarizeVectorStore(ST, DAG); 715 return TF; 716 } 717 718 void VectorLegalizer::Expand(SDNode *Node, SmallVectorImpl<SDValue> &Results) { 719 SDValue Tmp; 720 switch (Node->getOpcode()) { 721 case ISD::MERGE_VALUES: 722 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 723 Results.push_back(Node->getOperand(i)); 724 return; 725 case ISD::SIGN_EXTEND_INREG: 726 Results.push_back(ExpandSEXTINREG(Node)); 727 return; 728 case ISD::ANY_EXTEND_VECTOR_INREG: 729 Results.push_back(ExpandANY_EXTEND_VECTOR_INREG(Node)); 730 return; 731 case ISD::SIGN_EXTEND_VECTOR_INREG: 732 Results.push_back(ExpandSIGN_EXTEND_VECTOR_INREG(Node)); 733 return; 734 case ISD::ZERO_EXTEND_VECTOR_INREG: 735 Results.push_back(ExpandZERO_EXTEND_VECTOR_INREG(Node)); 736 return; 737 case ISD::BSWAP: 738 Results.push_back(ExpandBSWAP(Node)); 739 return; 740 case ISD::VSELECT: 741 Results.push_back(ExpandVSELECT(Node)); 742 return; 743 case ISD::SELECT: 744 Results.push_back(ExpandSELECT(Node)); 745 return; 746 case ISD::FP_TO_UINT: 747 ExpandFP_TO_UINT(Node, Results); 748 return; 749 case ISD::UINT_TO_FP: 750 ExpandUINT_TO_FLOAT(Node, Results); 751 return; 752 case ISD::FNEG: 753 Results.push_back(ExpandFNEG(Node)); 754 return; 755 case ISD::FSUB: 756 ExpandFSUB(Node, Results); 757 return; 758 case ISD::SETCC: 759 Results.push_back(UnrollVSETCC(Node)); 760 return; 761 case ISD::ABS: 762 if (TLI.expandABS(Node, Tmp, DAG)) { 763 Results.push_back(Tmp); 764 return; 765 } 766 break; 767 case ISD::BITREVERSE: 768 ExpandBITREVERSE(Node, Results); 769 return; 770 case ISD::CTPOP: 771 if (TLI.expandCTPOP(Node, Tmp, DAG)) { 772 Results.push_back(Tmp); 773 return; 774 } 775 break; 776 case ISD::CTLZ: 777 case ISD::CTLZ_ZERO_UNDEF: 778 if (TLI.expandCTLZ(Node, Tmp, DAG)) { 779 Results.push_back(Tmp); 780 return; 781 } 782 break; 783 case ISD::CTTZ: 784 case ISD::CTTZ_ZERO_UNDEF: 785 if (TLI.expandCTTZ(Node, Tmp, DAG)) { 786 Results.push_back(Tmp); 787 return; 788 } 789 break; 790 case ISD::FSHL: 791 case ISD::FSHR: 792 if (TLI.expandFunnelShift(Node, Tmp, DAG)) { 793 Results.push_back(Tmp); 794 return; 795 } 796 break; 797 case ISD::ROTL: 798 case ISD::ROTR: 799 if (TLI.expandROT(Node, Tmp, DAG)) { 800 Results.push_back(Tmp); 801 return; 802 } 803 break; 804 case ISD::FMINNUM: 805 case ISD::FMAXNUM: 806 if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG)) { 807 Results.push_back(Expanded); 808 return; 809 } 810 break; 811 case ISD::UADDO: 812 case ISD::USUBO: 813 ExpandUADDSUBO(Node, Results); 814 return; 815 case ISD::SADDO: 816 case ISD::SSUBO: 817 ExpandSADDSUBO(Node, Results); 818 return; 819 case ISD::UMULO: 820 case ISD::SMULO: 821 ExpandMULO(Node, Results); 822 return; 823 case ISD::USUBSAT: 824 case ISD::SSUBSAT: 825 case ISD::UADDSAT: 826 case ISD::SADDSAT: 827 if (SDValue Expanded = TLI.expandAddSubSat(Node, DAG)) { 828 Results.push_back(Expanded); 829 return; 830 } 831 break; 832 case ISD::SMULFIX: 833 case ISD::UMULFIX: 834 if (SDValue Expanded = TLI.expandFixedPointMul(Node, DAG)) { 835 Results.push_back(Expanded); 836 return; 837 } 838 break; 839 case ISD::SMULFIXSAT: 840 case ISD::UMULFIXSAT: 841 // FIXME: We do not expand SMULFIXSAT/UMULFIXSAT here yet, not sure exactly 842 // why. Maybe it results in worse codegen compared to the unroll for some 843 // targets? This should probably be investigated. And if we still prefer to 844 // unroll an explanation could be helpful. 845 break; 846 case ISD::SDIVFIX: 847 case ISD::UDIVFIX: 848 ExpandFixedPointDiv(Node, Results); 849 return; 850 case ISD::SDIVFIXSAT: 851 case ISD::UDIVFIXSAT: 852 break; 853 #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \ 854 case ISD::STRICT_##DAGN: 855 #include "llvm/IR/ConstrainedOps.def" 856 ExpandStrictFPOp(Node, Results); 857 return; 858 case ISD::VECREDUCE_ADD: 859 case ISD::VECREDUCE_MUL: 860 case ISD::VECREDUCE_AND: 861 case ISD::VECREDUCE_OR: 862 case ISD::VECREDUCE_XOR: 863 case ISD::VECREDUCE_SMAX: 864 case ISD::VECREDUCE_SMIN: 865 case ISD::VECREDUCE_UMAX: 866 case ISD::VECREDUCE_UMIN: 867 case ISD::VECREDUCE_FADD: 868 case ISD::VECREDUCE_FMUL: 869 case ISD::VECREDUCE_FMAX: 870 case ISD::VECREDUCE_FMIN: 871 Results.push_back(TLI.expandVecReduce(Node, DAG)); 872 return; 873 case ISD::SREM: 874 case ISD::UREM: 875 ExpandREM(Node, Results); 876 return; 877 } 878 879 Results.push_back(DAG.UnrollVectorOp(Node)); 880 } 881 882 SDValue VectorLegalizer::ExpandSELECT(SDNode *Node) { 883 // Lower a select instruction where the condition is a scalar and the 884 // operands are vectors. Lower this select to VSELECT and implement it 885 // using XOR AND OR. The selector bit is broadcasted. 886 EVT VT = Node->getValueType(0); 887 SDLoc DL(Node); 888 889 SDValue Mask = Node->getOperand(0); 890 SDValue Op1 = Node->getOperand(1); 891 SDValue Op2 = Node->getOperand(2); 892 893 assert(VT.isVector() && !Mask.getValueType().isVector() 894 && Op1.getValueType() == Op2.getValueType() && "Invalid type"); 895 896 // If we can't even use the basic vector operations of 897 // AND,OR,XOR, we will have to scalarize the op. 898 // Notice that the operation may be 'promoted' which means that it is 899 // 'bitcasted' to another type which is handled. 900 // Also, we need to be able to construct a splat vector using BUILD_VECTOR. 901 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand || 902 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand || 903 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand || 904 TLI.getOperationAction(ISD::BUILD_VECTOR, VT) == TargetLowering::Expand) 905 return DAG.UnrollVectorOp(Node); 906 907 // Generate a mask operand. 908 EVT MaskTy = VT.changeVectorElementTypeToInteger(); 909 910 // What is the size of each element in the vector mask. 911 EVT BitTy = MaskTy.getScalarType(); 912 913 Mask = DAG.getSelect(DL, BitTy, Mask, 914 DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), DL, 915 BitTy), 916 DAG.getConstant(0, DL, BitTy)); 917 918 // Broadcast the mask so that the entire vector is all-one or all zero. 919 Mask = DAG.getSplatBuildVector(MaskTy, DL, Mask); 920 921 // Bitcast the operands to be the same type as the mask. 922 // This is needed when we select between FP types because 923 // the mask is a vector of integers. 924 Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1); 925 Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2); 926 927 SDValue AllOnes = DAG.getConstant( 928 APInt::getAllOnesValue(BitTy.getSizeInBits()), DL, MaskTy); 929 SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes); 930 931 Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask); 932 Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask); 933 SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2); 934 return DAG.getNode(ISD::BITCAST, DL, Node->getValueType(0), Val); 935 } 936 937 SDValue VectorLegalizer::ExpandSEXTINREG(SDNode *Node) { 938 EVT VT = Node->getValueType(0); 939 940 // Make sure that the SRA and SHL instructions are available. 941 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand || 942 TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand) 943 return DAG.UnrollVectorOp(Node); 944 945 SDLoc DL(Node); 946 EVT OrigTy = cast<VTSDNode>(Node->getOperand(1))->getVT(); 947 948 unsigned BW = VT.getScalarSizeInBits(); 949 unsigned OrigBW = OrigTy.getScalarSizeInBits(); 950 SDValue ShiftSz = DAG.getConstant(BW - OrigBW, DL, VT); 951 952 SDValue Op = DAG.getNode(ISD::SHL, DL, VT, Node->getOperand(0), ShiftSz); 953 return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz); 954 } 955 956 // Generically expand a vector anyext in register to a shuffle of the relevant 957 // lanes into the appropriate locations, with other lanes left undef. 958 SDValue VectorLegalizer::ExpandANY_EXTEND_VECTOR_INREG(SDNode *Node) { 959 SDLoc DL(Node); 960 EVT VT = Node->getValueType(0); 961 int NumElements = VT.getVectorNumElements(); 962 SDValue Src = Node->getOperand(0); 963 EVT SrcVT = Src.getValueType(); 964 int NumSrcElements = SrcVT.getVectorNumElements(); 965 966 // *_EXTEND_VECTOR_INREG SrcVT can be smaller than VT - so insert the vector 967 // into a larger vector type. 968 if (SrcVT.bitsLE(VT)) { 969 assert((VT.getSizeInBits() % SrcVT.getScalarSizeInBits()) == 0 && 970 "ANY_EXTEND_VECTOR_INREG vector size mismatch"); 971 NumSrcElements = VT.getSizeInBits() / SrcVT.getScalarSizeInBits(); 972 SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getScalarType(), 973 NumSrcElements); 974 Src = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, SrcVT, DAG.getUNDEF(SrcVT), 975 Src, DAG.getVectorIdxConstant(0, DL)); 976 } 977 978 // Build a base mask of undef shuffles. 979 SmallVector<int, 16> ShuffleMask; 980 ShuffleMask.resize(NumSrcElements, -1); 981 982 // Place the extended lanes into the correct locations. 983 int ExtLaneScale = NumSrcElements / NumElements; 984 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0; 985 for (int i = 0; i < NumElements; ++i) 986 ShuffleMask[i * ExtLaneScale + EndianOffset] = i; 987 988 return DAG.getNode( 989 ISD::BITCAST, DL, VT, 990 DAG.getVectorShuffle(SrcVT, DL, Src, DAG.getUNDEF(SrcVT), ShuffleMask)); 991 } 992 993 SDValue VectorLegalizer::ExpandSIGN_EXTEND_VECTOR_INREG(SDNode *Node) { 994 SDLoc DL(Node); 995 EVT VT = Node->getValueType(0); 996 SDValue Src = Node->getOperand(0); 997 EVT SrcVT = Src.getValueType(); 998 999 // First build an any-extend node which can be legalized above when we 1000 // recurse through it. 1001 SDValue Op = DAG.getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Src); 1002 1003 // Now we need sign extend. Do this by shifting the elements. Even if these 1004 // aren't legal operations, they have a better chance of being legalized 1005 // without full scalarization than the sign extension does. 1006 unsigned EltWidth = VT.getScalarSizeInBits(); 1007 unsigned SrcEltWidth = SrcVT.getScalarSizeInBits(); 1008 SDValue ShiftAmount = DAG.getConstant(EltWidth - SrcEltWidth, DL, VT); 1009 return DAG.getNode(ISD::SRA, DL, VT, 1010 DAG.getNode(ISD::SHL, DL, VT, Op, ShiftAmount), 1011 ShiftAmount); 1012 } 1013 1014 // Generically expand a vector zext in register to a shuffle of the relevant 1015 // lanes into the appropriate locations, a blend of zero into the high bits, 1016 // and a bitcast to the wider element type. 1017 SDValue VectorLegalizer::ExpandZERO_EXTEND_VECTOR_INREG(SDNode *Node) { 1018 SDLoc DL(Node); 1019 EVT VT = Node->getValueType(0); 1020 int NumElements = VT.getVectorNumElements(); 1021 SDValue Src = Node->getOperand(0); 1022 EVT SrcVT = Src.getValueType(); 1023 int NumSrcElements = SrcVT.getVectorNumElements(); 1024 1025 // *_EXTEND_VECTOR_INREG SrcVT can be smaller than VT - so insert the vector 1026 // into a larger vector type. 1027 if (SrcVT.bitsLE(VT)) { 1028 assert((VT.getSizeInBits() % SrcVT.getScalarSizeInBits()) == 0 && 1029 "ZERO_EXTEND_VECTOR_INREG vector size mismatch"); 1030 NumSrcElements = VT.getSizeInBits() / SrcVT.getScalarSizeInBits(); 1031 SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getScalarType(), 1032 NumSrcElements); 1033 Src = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, SrcVT, DAG.getUNDEF(SrcVT), 1034 Src, DAG.getVectorIdxConstant(0, DL)); 1035 } 1036 1037 // Build up a zero vector to blend into this one. 1038 SDValue Zero = DAG.getConstant(0, DL, SrcVT); 1039 1040 // Shuffle the incoming lanes into the correct position, and pull all other 1041 // lanes from the zero vector. 1042 SmallVector<int, 16> ShuffleMask; 1043 ShuffleMask.reserve(NumSrcElements); 1044 for (int i = 0; i < NumSrcElements; ++i) 1045 ShuffleMask.push_back(i); 1046 1047 int ExtLaneScale = NumSrcElements / NumElements; 1048 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0; 1049 for (int i = 0; i < NumElements; ++i) 1050 ShuffleMask[i * ExtLaneScale + EndianOffset] = NumSrcElements + i; 1051 1052 return DAG.getNode(ISD::BITCAST, DL, VT, 1053 DAG.getVectorShuffle(SrcVT, DL, Zero, Src, ShuffleMask)); 1054 } 1055 1056 static void createBSWAPShuffleMask(EVT VT, SmallVectorImpl<int> &ShuffleMask) { 1057 int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8; 1058 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) 1059 for (int J = ScalarSizeInBytes - 1; J >= 0; --J) 1060 ShuffleMask.push_back((I * ScalarSizeInBytes) + J); 1061 } 1062 1063 SDValue VectorLegalizer::ExpandBSWAP(SDNode *Node) { 1064 EVT VT = Node->getValueType(0); 1065 1066 // Generate a byte wise shuffle mask for the BSWAP. 1067 SmallVector<int, 16> ShuffleMask; 1068 createBSWAPShuffleMask(VT, ShuffleMask); 1069 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size()); 1070 1071 // Only emit a shuffle if the mask is legal. 1072 if (!TLI.isShuffleMaskLegal(ShuffleMask, ByteVT)) 1073 return DAG.UnrollVectorOp(Node); 1074 1075 SDLoc DL(Node); 1076 SDValue Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Node->getOperand(0)); 1077 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), ShuffleMask); 1078 return DAG.getNode(ISD::BITCAST, DL, VT, Op); 1079 } 1080 1081 void VectorLegalizer::ExpandBITREVERSE(SDNode *Node, 1082 SmallVectorImpl<SDValue> &Results) { 1083 EVT VT = Node->getValueType(0); 1084 1085 // If we have the scalar operation, it's probably cheaper to unroll it. 1086 if (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, VT.getScalarType())) { 1087 SDValue Tmp = DAG.UnrollVectorOp(Node); 1088 Results.push_back(Tmp); 1089 return; 1090 } 1091 1092 // If the vector element width is a whole number of bytes, test if its legal 1093 // to BSWAP shuffle the bytes and then perform the BITREVERSE on the byte 1094 // vector. This greatly reduces the number of bit shifts necessary. 1095 unsigned ScalarSizeInBits = VT.getScalarSizeInBits(); 1096 if (ScalarSizeInBits > 8 && (ScalarSizeInBits % 8) == 0) { 1097 SmallVector<int, 16> BSWAPMask; 1098 createBSWAPShuffleMask(VT, BSWAPMask); 1099 1100 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, BSWAPMask.size()); 1101 if (TLI.isShuffleMaskLegal(BSWAPMask, ByteVT) && 1102 (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, ByteVT) || 1103 (TLI.isOperationLegalOrCustom(ISD::SHL, ByteVT) && 1104 TLI.isOperationLegalOrCustom(ISD::SRL, ByteVT) && 1105 TLI.isOperationLegalOrCustomOrPromote(ISD::AND, ByteVT) && 1106 TLI.isOperationLegalOrCustomOrPromote(ISD::OR, ByteVT)))) { 1107 SDLoc DL(Node); 1108 SDValue Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Node->getOperand(0)); 1109 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), 1110 BSWAPMask); 1111 Op = DAG.getNode(ISD::BITREVERSE, DL, ByteVT, Op); 1112 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); 1113 Results.push_back(Op); 1114 return; 1115 } 1116 } 1117 1118 // If we have the appropriate vector bit operations, it is better to use them 1119 // than unrolling and expanding each component. 1120 if (TLI.isOperationLegalOrCustom(ISD::SHL, VT) && 1121 TLI.isOperationLegalOrCustom(ISD::SRL, VT) && 1122 TLI.isOperationLegalOrCustomOrPromote(ISD::AND, VT) && 1123 TLI.isOperationLegalOrCustomOrPromote(ISD::OR, VT)) 1124 // Let LegalizeDAG handle this later. 1125 return; 1126 1127 // Otherwise unroll. 1128 SDValue Tmp = DAG.UnrollVectorOp(Node); 1129 Results.push_back(Tmp); 1130 } 1131 1132 SDValue VectorLegalizer::ExpandVSELECT(SDNode *Node) { 1133 // Implement VSELECT in terms of XOR, AND, OR 1134 // on platforms which do not support blend natively. 1135 SDLoc DL(Node); 1136 1137 SDValue Mask = Node->getOperand(0); 1138 SDValue Op1 = Node->getOperand(1); 1139 SDValue Op2 = Node->getOperand(2); 1140 1141 EVT VT = Mask.getValueType(); 1142 1143 // If we can't even use the basic vector operations of 1144 // AND,OR,XOR, we will have to scalarize the op. 1145 // Notice that the operation may be 'promoted' which means that it is 1146 // 'bitcasted' to another type which is handled. 1147 // This operation also isn't safe with AND, OR, XOR when the boolean 1148 // type is 0/1 as we need an all ones vector constant to mask with. 1149 // FIXME: Sign extend 1 to all ones if thats legal on the target. 1150 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand || 1151 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand || 1152 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand || 1153 TLI.getBooleanContents(Op1.getValueType()) != 1154 TargetLowering::ZeroOrNegativeOneBooleanContent) 1155 return DAG.UnrollVectorOp(Node); 1156 1157 // If the mask and the type are different sizes, unroll the vector op. This 1158 // can occur when getSetCCResultType returns something that is different in 1159 // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8. 1160 if (VT.getSizeInBits() != Op1.getValueSizeInBits()) 1161 return DAG.UnrollVectorOp(Node); 1162 1163 // Bitcast the operands to be the same type as the mask. 1164 // This is needed when we select between FP types because 1165 // the mask is a vector of integers. 1166 Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1); 1167 Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2); 1168 1169 SDValue AllOnes = DAG.getConstant( 1170 APInt::getAllOnesValue(VT.getScalarSizeInBits()), DL, VT); 1171 SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes); 1172 1173 Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask); 1174 Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask); 1175 SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2); 1176 return DAG.getNode(ISD::BITCAST, DL, Node->getValueType(0), Val); 1177 } 1178 1179 void VectorLegalizer::ExpandFP_TO_UINT(SDNode *Node, 1180 SmallVectorImpl<SDValue> &Results) { 1181 // Attempt to expand using TargetLowering. 1182 SDValue Result, Chain; 1183 if (TLI.expandFP_TO_UINT(Node, Result, Chain, DAG)) { 1184 Results.push_back(Result); 1185 if (Node->isStrictFPOpcode()) 1186 Results.push_back(Chain); 1187 return; 1188 } 1189 1190 // Otherwise go ahead and unroll. 1191 if (Node->isStrictFPOpcode()) { 1192 UnrollStrictFPOp(Node, Results); 1193 return; 1194 } 1195 1196 Results.push_back(DAG.UnrollVectorOp(Node)); 1197 } 1198 1199 void VectorLegalizer::ExpandUINT_TO_FLOAT(SDNode *Node, 1200 SmallVectorImpl<SDValue> &Results) { 1201 bool IsStrict = Node->isStrictFPOpcode(); 1202 unsigned OpNo = IsStrict ? 1 : 0; 1203 SDValue Src = Node->getOperand(OpNo); 1204 EVT VT = Src.getValueType(); 1205 SDLoc DL(Node); 1206 1207 // Attempt to expand using TargetLowering. 1208 SDValue Result; 1209 SDValue Chain; 1210 if (TLI.expandUINT_TO_FP(Node, Result, Chain, DAG)) { 1211 Results.push_back(Result); 1212 if (IsStrict) 1213 Results.push_back(Chain); 1214 return; 1215 } 1216 1217 // Make sure that the SINT_TO_FP and SRL instructions are available. 1218 if (((!IsStrict && TLI.getOperationAction(ISD::SINT_TO_FP, VT) == 1219 TargetLowering::Expand) || 1220 (IsStrict && TLI.getOperationAction(ISD::STRICT_SINT_TO_FP, VT) == 1221 TargetLowering::Expand)) || 1222 TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand) { 1223 if (IsStrict) { 1224 UnrollStrictFPOp(Node, Results); 1225 return; 1226 } 1227 1228 Results.push_back(DAG.UnrollVectorOp(Node)); 1229 return; 1230 } 1231 1232 unsigned BW = VT.getScalarSizeInBits(); 1233 assert((BW == 64 || BW == 32) && 1234 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide"); 1235 1236 SDValue HalfWord = DAG.getConstant(BW / 2, DL, VT); 1237 1238 // Constants to clear the upper part of the word. 1239 // Notice that we can also use SHL+SHR, but using a constant is slightly 1240 // faster on x86. 1241 uint64_t HWMask = (BW == 64) ? 0x00000000FFFFFFFF : 0x0000FFFF; 1242 SDValue HalfWordMask = DAG.getConstant(HWMask, DL, VT); 1243 1244 // Two to the power of half-word-size. 1245 SDValue TWOHW = 1246 DAG.getConstantFP(1ULL << (BW / 2), DL, Node->getValueType(0)); 1247 1248 // Clear upper part of LO, lower HI 1249 SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Src, HalfWord); 1250 SDValue LO = DAG.getNode(ISD::AND, DL, VT, Src, HalfWordMask); 1251 1252 if (IsStrict) { 1253 // Convert hi and lo to floats 1254 // Convert the hi part back to the upper values 1255 // TODO: Can any fast-math-flags be set on these nodes? 1256 SDValue fHI = DAG.getNode(ISD::STRICT_SINT_TO_FP, DL, 1257 {Node->getValueType(0), MVT::Other}, 1258 {Node->getOperand(0), HI}); 1259 fHI = DAG.getNode(ISD::STRICT_FMUL, DL, {Node->getValueType(0), MVT::Other}, 1260 {fHI.getValue(1), fHI, TWOHW}); 1261 SDValue fLO = DAG.getNode(ISD::STRICT_SINT_TO_FP, DL, 1262 {Node->getValueType(0), MVT::Other}, 1263 {Node->getOperand(0), LO}); 1264 1265 SDValue TF = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, fHI.getValue(1), 1266 fLO.getValue(1)); 1267 1268 // Add the two halves 1269 SDValue Result = 1270 DAG.getNode(ISD::STRICT_FADD, DL, {Node->getValueType(0), MVT::Other}, 1271 {TF, fHI, fLO}); 1272 1273 Results.push_back(Result); 1274 Results.push_back(Result.getValue(1)); 1275 return; 1276 } 1277 1278 // Convert hi and lo to floats 1279 // Convert the hi part back to the upper values 1280 // TODO: Can any fast-math-flags be set on these nodes? 1281 SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Node->getValueType(0), HI); 1282 fHI = DAG.getNode(ISD::FMUL, DL, Node->getValueType(0), fHI, TWOHW); 1283 SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Node->getValueType(0), LO); 1284 1285 // Add the two halves 1286 Results.push_back( 1287 DAG.getNode(ISD::FADD, DL, Node->getValueType(0), fHI, fLO)); 1288 } 1289 1290 SDValue VectorLegalizer::ExpandFNEG(SDNode *Node) { 1291 if (TLI.isOperationLegalOrCustom(ISD::FSUB, Node->getValueType(0))) { 1292 SDLoc DL(Node); 1293 SDValue Zero = DAG.getConstantFP(-0.0, DL, Node->getValueType(0)); 1294 // TODO: If FNEG had fast-math-flags, they'd get propagated to this FSUB. 1295 return DAG.getNode(ISD::FSUB, DL, Node->getValueType(0), Zero, 1296 Node->getOperand(0)); 1297 } 1298 return DAG.UnrollVectorOp(Node); 1299 } 1300 1301 void VectorLegalizer::ExpandFSUB(SDNode *Node, 1302 SmallVectorImpl<SDValue> &Results) { 1303 // For floating-point values, (a-b) is the same as a+(-b). If FNEG is legal, 1304 // we can defer this to operation legalization where it will be lowered as 1305 // a+(-b). 1306 EVT VT = Node->getValueType(0); 1307 if (TLI.isOperationLegalOrCustom(ISD::FNEG, VT) && 1308 TLI.isOperationLegalOrCustom(ISD::FADD, VT)) 1309 return; // Defer to LegalizeDAG 1310 1311 SDValue Tmp = DAG.UnrollVectorOp(Node); 1312 Results.push_back(Tmp); 1313 } 1314 1315 void VectorLegalizer::ExpandUADDSUBO(SDNode *Node, 1316 SmallVectorImpl<SDValue> &Results) { 1317 SDValue Result, Overflow; 1318 TLI.expandUADDSUBO(Node, Result, Overflow, DAG); 1319 Results.push_back(Result); 1320 Results.push_back(Overflow); 1321 } 1322 1323 void VectorLegalizer::ExpandSADDSUBO(SDNode *Node, 1324 SmallVectorImpl<SDValue> &Results) { 1325 SDValue Result, Overflow; 1326 TLI.expandSADDSUBO(Node, Result, Overflow, DAG); 1327 Results.push_back(Result); 1328 Results.push_back(Overflow); 1329 } 1330 1331 void VectorLegalizer::ExpandMULO(SDNode *Node, 1332 SmallVectorImpl<SDValue> &Results) { 1333 SDValue Result, Overflow; 1334 if (!TLI.expandMULO(Node, Result, Overflow, DAG)) 1335 std::tie(Result, Overflow) = DAG.UnrollVectorOverflowOp(Node); 1336 1337 Results.push_back(Result); 1338 Results.push_back(Overflow); 1339 } 1340 1341 void VectorLegalizer::ExpandFixedPointDiv(SDNode *Node, 1342 SmallVectorImpl<SDValue> &Results) { 1343 SDNode *N = Node; 1344 if (SDValue Expanded = TLI.expandFixedPointDiv(N->getOpcode(), SDLoc(N), 1345 N->getOperand(0), N->getOperand(1), N->getConstantOperandVal(2), DAG)) 1346 Results.push_back(Expanded); 1347 } 1348 1349 void VectorLegalizer::ExpandStrictFPOp(SDNode *Node, 1350 SmallVectorImpl<SDValue> &Results) { 1351 if (Node->getOpcode() == ISD::STRICT_UINT_TO_FP) { 1352 ExpandUINT_TO_FLOAT(Node, Results); 1353 return; 1354 } 1355 if (Node->getOpcode() == ISD::STRICT_FP_TO_UINT) { 1356 ExpandFP_TO_UINT(Node, Results); 1357 return; 1358 } 1359 1360 UnrollStrictFPOp(Node, Results); 1361 } 1362 1363 void VectorLegalizer::ExpandREM(SDNode *Node, 1364 SmallVectorImpl<SDValue> &Results) { 1365 assert((Node->getOpcode() == ISD::SREM || Node->getOpcode() == ISD::UREM) && 1366 "Expected REM node"); 1367 1368 SDValue Result; 1369 if (!TLI.expandREM(Node, Result, DAG)) 1370 Result = DAG.UnrollVectorOp(Node); 1371 Results.push_back(Result); 1372 } 1373 1374 void VectorLegalizer::UnrollStrictFPOp(SDNode *Node, 1375 SmallVectorImpl<SDValue> &Results) { 1376 EVT VT = Node->getValueType(0); 1377 EVT EltVT = VT.getVectorElementType(); 1378 unsigned NumElems = VT.getVectorNumElements(); 1379 unsigned NumOpers = Node->getNumOperands(); 1380 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 1381 1382 EVT TmpEltVT = EltVT; 1383 if (Node->getOpcode() == ISD::STRICT_FSETCC || 1384 Node->getOpcode() == ISD::STRICT_FSETCCS) 1385 TmpEltVT = TLI.getSetCCResultType(DAG.getDataLayout(), 1386 *DAG.getContext(), TmpEltVT); 1387 1388 EVT ValueVTs[] = {TmpEltVT, MVT::Other}; 1389 SDValue Chain = Node->getOperand(0); 1390 SDLoc dl(Node); 1391 1392 SmallVector<SDValue, 32> OpValues; 1393 SmallVector<SDValue, 32> OpChains; 1394 for (unsigned i = 0; i < NumElems; ++i) { 1395 SmallVector<SDValue, 4> Opers; 1396 SDValue Idx = DAG.getVectorIdxConstant(i, dl); 1397 1398 // The Chain is the first operand. 1399 Opers.push_back(Chain); 1400 1401 // Now process the remaining operands. 1402 for (unsigned j = 1; j < NumOpers; ++j) { 1403 SDValue Oper = Node->getOperand(j); 1404 EVT OperVT = Oper.getValueType(); 1405 1406 if (OperVT.isVector()) 1407 Oper = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, 1408 OperVT.getVectorElementType(), Oper, Idx); 1409 1410 Opers.push_back(Oper); 1411 } 1412 1413 SDValue ScalarOp = DAG.getNode(Node->getOpcode(), dl, ValueVTs, Opers); 1414 SDValue ScalarResult = ScalarOp.getValue(0); 1415 SDValue ScalarChain = ScalarOp.getValue(1); 1416 1417 if (Node->getOpcode() == ISD::STRICT_FSETCC || 1418 Node->getOpcode() == ISD::STRICT_FSETCCS) 1419 ScalarResult = DAG.getSelect(dl, EltVT, ScalarResult, 1420 DAG.getConstant(APInt::getAllOnesValue 1421 (EltVT.getSizeInBits()), dl, EltVT), 1422 DAG.getConstant(0, dl, EltVT)); 1423 1424 OpValues.push_back(ScalarResult); 1425 OpChains.push_back(ScalarChain); 1426 } 1427 1428 SDValue Result = DAG.getBuildVector(VT, dl, OpValues); 1429 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OpChains); 1430 1431 Results.push_back(Result); 1432 Results.push_back(NewChain); 1433 } 1434 1435 SDValue VectorLegalizer::UnrollVSETCC(SDNode *Node) { 1436 EVT VT = Node->getValueType(0); 1437 unsigned NumElems = VT.getVectorNumElements(); 1438 EVT EltVT = VT.getVectorElementType(); 1439 SDValue LHS = Node->getOperand(0); 1440 SDValue RHS = Node->getOperand(1); 1441 SDValue CC = Node->getOperand(2); 1442 EVT TmpEltVT = LHS.getValueType().getVectorElementType(); 1443 SDLoc dl(Node); 1444 SmallVector<SDValue, 8> Ops(NumElems); 1445 for (unsigned i = 0; i < NumElems; ++i) { 1446 SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS, 1447 DAG.getVectorIdxConstant(i, dl)); 1448 SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS, 1449 DAG.getVectorIdxConstant(i, dl)); 1450 Ops[i] = DAG.getNode(ISD::SETCC, dl, 1451 TLI.getSetCCResultType(DAG.getDataLayout(), 1452 *DAG.getContext(), TmpEltVT), 1453 LHSElem, RHSElem, CC); 1454 Ops[i] = DAG.getSelect(dl, EltVT, Ops[i], 1455 DAG.getConstant(APInt::getAllOnesValue 1456 (EltVT.getSizeInBits()), dl, EltVT), 1457 DAG.getConstant(0, dl, EltVT)); 1458 } 1459 return DAG.getBuildVector(VT, dl, Ops); 1460 } 1461 1462 bool SelectionDAG::LegalizeVectors() { 1463 return VectorLegalizer(*this).Run(); 1464 } 1465