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