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