LLVM 24.0.0git
LegalizeVectorOps.cpp
Go to the documentation of this file.
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/DenseMap.h"
40#include "llvm/IR/DataLayout.h"
43#include "llvm/Support/Debug.h"
45#include <cassert>
46#include <cstdint>
47#include <iterator>
48#include <utility>
49
50using namespace llvm;
51
52#define DEBUG_TYPE "legalizevectorops"
53
54namespace {
55
56class 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.
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,
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.
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 ExpandVP_MERGE(SDNode *Node);
137 SDValue ExpandVP_REM(SDNode *Node);
138 SDValue ExpandGET_ACTIVE_LANE_MASK(SDNode *N);
139 SDValue ExpandLOOP_DEPENDENCE_MASK(SDNode *N);
140 SDValue ExpandMaskedBinOp(SDNode *N);
141 SDValue ExpandSELECT(SDNode *Node);
142 std::pair<SDValue, SDValue> ExpandLoad(SDNode *N);
143 SDValue ExpandStore(SDNode *N);
144 SDValue ExpandFNEG(SDNode *Node);
145 SDValue ExpandFABS(SDNode *Node);
146 SDValue ExpandFCOPYSIGN(SDNode *Node);
147 void ExpandFSUB(SDNode *Node, SmallVectorImpl<SDValue> &Results);
148 void ExpandSETCC(SDNode *Node, SmallVectorImpl<SDValue> &Results);
149 SDValue ExpandBITREVERSE(SDNode *Node);
150 void ExpandUADDSUBO(SDNode *Node, SmallVectorImpl<SDValue> &Results);
151 void ExpandSADDSUBO(SDNode *Node, SmallVectorImpl<SDValue> &Results);
152 void ExpandMULO(SDNode *Node, SmallVectorImpl<SDValue> &Results);
153 void ExpandFixedPointDiv(SDNode *Node, SmallVectorImpl<SDValue> &Results);
154 void ExpandStrictFPOp(SDNode *Node, SmallVectorImpl<SDValue> &Results);
155 void ExpandREM(SDNode *Node, SmallVectorImpl<SDValue> &Results);
156
157 bool tryExpandVecMathCall(SDNode *Node,
158 function_ref<RTLIB::Libcall(EVT)> GetLibcall,
160
161 void UnrollStrictFPOp(SDNode *Node, SmallVectorImpl<SDValue> &Results);
162
163 /// Implements vector promotion.
164 ///
165 /// This is essentially just bitcasting the operands to a different type and
166 /// bitcasting the result back to the original type.
168
169 /// Implements [SU]INT_TO_FP vector promotion.
170 ///
171 /// This is a [zs]ext of the input operand to a larger integer type.
172 void PromoteINT_TO_FP(SDNode *Node, SmallVectorImpl<SDValue> &Results);
173
174 /// Implements FP_TO_[SU]INT vector promotion of the result type.
175 ///
176 /// It is promoted to a larger integer type. The result is then
177 /// truncated back to the original type.
178 void PromoteFP_TO_INT(SDNode *Node, SmallVectorImpl<SDValue> &Results);
179
180 /// Implements vector setcc operation promotion.
181 ///
182 /// All vector operands are promoted to a vector type with larger element
183 /// type.
184 void PromoteSETCC(SDNode *Node, SmallVectorImpl<SDValue> &Results);
185
186 void PromoteSTRICT(SDNode *Node, SmallVectorImpl<SDValue> &Results);
187
188 /// Calculate the reduction using a type of higher precision and round the
189 /// result to match the original type. Setting NonArithmetic signifies the
190 /// rounding of the result does not affect its value.
191 void PromoteFloatVECREDUCE(SDNode *Node, SmallVectorImpl<SDValue> &Results,
192 bool NonArithmetic);
193
194 void PromoteVECTOR_COMPRESS(SDNode *Node, SmallVectorImpl<SDValue> &Results);
195
196public:
197 VectorLegalizer(SelectionDAG& dag) :
198 DAG(dag), TLI(dag.getTargetLoweringInfo()) {}
199
200 /// Begin legalizer the vector operations in the DAG.
201 bool Run();
202};
203
204} // end anonymous namespace
205
206bool VectorLegalizer::Run() {
207 // Before we start legalizing vector nodes, check if there are any vectors.
208 bool HasVectors = false;
210 E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I) {
211 // Check if the values of the nodes contain vectors. We don't need to check
212 // the operands because we are going to check their values at some point.
213 HasVectors = llvm::any_of(I->values(), [](EVT T) { return T.isVector(); });
214
215 // If we found a vector node we can start the legalization.
216 if (HasVectors)
217 break;
218 }
219
220 // If this basic block has no vectors then no need to legalize vectors.
221 if (!HasVectors)
222 return false;
223
224 // The legalize process is inherently a bottom-up recursive process (users
225 // legalize their uses before themselves). Given infinite stack space, we
226 // could just start legalizing on the root and traverse the whole graph. In
227 // practice however, this causes us to run out of stack space on large basic
228 // blocks. To avoid this problem, compute an ordering of the nodes where each
229 // node is only legalized after all of its operands are legalized.
232 E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I)
233 LegalizeOp(SDValue(&*I, 0));
234
235 // Finally, it's possible the root changed. Get the new root.
236 SDValue OldRoot = DAG.getRoot();
237 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
238 DAG.setRoot(LegalizedNodes[OldRoot]);
239
240 LegalizedNodes.clear();
241
242 // Remove dead nodes now.
243 DAG.RemoveDeadNodes();
244
245 return Changed;
246}
247
248SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDNode *Result) {
249 assert(Op->getNumValues() == Result->getNumValues() &&
250 "Unexpected number of results");
251 // Generic legalization: just pass the operand through.
252 for (unsigned i = 0, e = Op->getNumValues(); i != e; ++i)
253 AddLegalizedOperand(Op.getValue(i), SDValue(Result, i));
254 return SDValue(Result, Op.getResNo());
255}
256
257SDValue
258VectorLegalizer::RecursivelyLegalizeResults(SDValue Op,
260 assert(Results.size() == Op->getNumValues() &&
261 "Unexpected number of results");
262 // Make sure that the generated code is itself legal.
263 for (unsigned i = 0, e = Results.size(); i != e; ++i) {
264 Results[i] = LegalizeOp(Results[i]);
265 AddLegalizedOperand(Op.getValue(i), Results[i]);
266 }
267
268 return Results[Op.getResNo()];
269}
270
271SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
272 // Note that LegalizeOp may be reentered even from single-use nodes, which
273 // means that we always must cache transformed nodes.
274 auto I = LegalizedNodes.find(Op);
275 if (I != LegalizedNodes.end()) return I->second;
276
277 // Legalize the operands
279 for (const SDValue &Oper : Op->op_values())
280 Ops.push_back(LegalizeOp(Oper));
281
282 SDNode *Node = DAG.UpdateNodeOperands(Op.getNode(), Ops);
283
284 bool HasVectorValueOrOp =
285 llvm::any_of(Node->values(), [](EVT T) { return T.isVector(); }) ||
286 llvm::any_of(Node->op_values(),
287 [](SDValue O) { return O.getValueType().isVector(); });
288 if (!HasVectorValueOrOp)
289 return TranslateLegalizeResults(Op, Node);
290
291 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
292 EVT ValVT;
293 switch (Op.getOpcode()) {
294 default:
295 return TranslateLegalizeResults(Op, Node);
296 case ISD::LOAD: {
297 LoadSDNode *LD = cast<LoadSDNode>(Node);
298 ISD::LoadExtType ExtType = LD->getExtensionType();
299 EVT LoadedVT = LD->getMemoryVT();
300 if (LoadedVT.isVector() && ExtType != ISD::NON_EXTLOAD)
301 Action = TLI.getLoadAction(LD->getValueType(0), LoadedVT, LD->getAlign(),
302 LD->getAddressSpace(), ExtType, false);
303 break;
304 }
305 case ISD::STORE: {
306 StoreSDNode *ST = cast<StoreSDNode>(Node);
307 EVT StVT = ST->getMemoryVT();
308 MVT ValVT = ST->getValue().getSimpleValueType();
309 if (StVT.isVector() && ST->isTruncatingStore())
310 Action = TLI.getTruncStoreAction(ValVT, StVT, ST->getAlign(),
311 ST->getAddressSpace());
312 break;
313 }
315 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
316 // This operation lies about being legal: when it claims to be legal,
317 // it should actually be expanded.
318 if (Action == TargetLowering::Legal)
319 Action = TargetLowering::Expand;
320 break;
321#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
322 case ISD::STRICT_##DAGN:
323#include "llvm/IR/ConstrainedOps.def"
324 ValVT = Node->getValueType(0);
325 if (Op.getOpcode() == ISD::STRICT_SINT_TO_FP ||
326 Op.getOpcode() == ISD::STRICT_UINT_TO_FP)
327 ValVT = Node->getOperand(1).getValueType();
328 if (Op.getOpcode() == ISD::STRICT_FSETCC ||
329 Op.getOpcode() == ISD::STRICT_FSETCCS) {
330 MVT OpVT = Node->getOperand(1).getSimpleValueType();
331 ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(3))->get();
332 Action = TLI.getCondCodeAction(CCCode, OpVT);
333 if (Action == TargetLowering::Legal)
334 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
335 } else {
336 Action = TLI.getOperationAction(Node->getOpcode(), ValVT);
337 }
338 // If we're asked to expand a strict vector floating-point operation,
339 // by default we're going to simply unroll it. That is usually the
340 // best approach, except in the case where the resulting strict (scalar)
341 // operations would themselves use the fallback mutation to non-strict.
342 // In that specific case, just do the fallback on the vector op.
343 if (Action == TargetLowering::Expand && !TLI.isStrictFPEnabled() &&
344 TLI.getStrictFPOperationAction(Node->getOpcode(), ValVT) ==
345 TargetLowering::Legal) {
346 EVT EltVT = ValVT.getVectorElementType();
347 if (TLI.getOperationAction(Node->getOpcode(), EltVT)
348 == TargetLowering::Expand &&
349 TLI.getStrictFPOperationAction(Node->getOpcode(), EltVT)
350 == TargetLowering::Legal)
351 Action = TargetLowering::Legal;
352 }
353 break;
354 case ISD::ADD:
355 case ISD::SUB:
356 case ISD::MUL:
357 case ISD::MULHS:
358 case ISD::MULHU:
359 case ISD::SDIV:
360 case ISD::UDIV:
361 case ISD::SREM:
362 case ISD::UREM:
363 case ISD::SDIVREM:
364 case ISD::UDIVREM:
365 case ISD::FADD:
366 case ISD::FSUB:
367 case ISD::FMUL:
368 case ISD::FDIV:
369 case ISD::FREM:
370 case ISD::AND:
371 case ISD::OR:
372 case ISD::XOR:
373 case ISD::SHL:
374 case ISD::SRA:
375 case ISD::SRL:
376 case ISD::FSHL:
377 case ISD::FSHR:
378 case ISD::ROTL:
379 case ISD::ROTR:
380 case ISD::ABS:
382 case ISD::ABDS:
383 case ISD::ABDU:
384 case ISD::AVGCEILS:
385 case ISD::AVGCEILU:
386 case ISD::AVGFLOORS:
387 case ISD::AVGFLOORU:
388 case ISD::BSWAP:
389 case ISD::BITREVERSE:
390 case ISD::CTLZ:
391 case ISD::CTTZ:
394 case ISD::CTPOP:
395 case ISD::CLMUL:
396 case ISD::CLMULH:
397 case ISD::CLMULR:
398 case ISD::SELECT:
399 case ISD::VSELECT:
400 case ISD::SELECT_CC:
401 case ISD::ZERO_EXTEND:
402 case ISD::ANY_EXTEND:
403 case ISD::TRUNCATE:
404 case ISD::SIGN_EXTEND:
405 case ISD::FP_TO_SINT:
406 case ISD::FP_TO_UINT:
407 case ISD::FNEG:
408 case ISD::FABS:
409 case ISD::FMINNUM:
410 case ISD::FMAXNUM:
413 case ISD::FMINIMUM:
414 case ISD::FMAXIMUM:
415 case ISD::FMINIMUMNUM:
416 case ISD::FMAXIMUMNUM:
417 case ISD::FCOPYSIGN:
418 case ISD::FSQRT:
419 case ISD::FSIN:
420 case ISD::FCOS:
421 case ISD::FTAN:
422 case ISD::FASIN:
423 case ISD::FACOS:
424 case ISD::FATAN:
425 case ISD::FATAN2:
426 case ISD::FSINH:
427 case ISD::FCOSH:
428 case ISD::FTANH:
429 case ISD::FLDEXP:
430 case ISD::FPOWI:
431 case ISD::FPOW:
432 case ISD::FCBRT:
433 case ISD::FLOG:
434 case ISD::FLOG2:
435 case ISD::FLOG10:
436 case ISD::FEXP:
437 case ISD::FEXP2:
438 case ISD::FEXP10:
439 case ISD::FCEIL:
440 case ISD::FTRUNC:
441 case ISD::FRINT:
442 case ISD::FNEARBYINT:
443 case ISD::FROUND:
444 case ISD::FROUNDEVEN:
445 case ISD::FFLOOR:
446 case ISD::FP_ROUND:
447 case ISD::FP_EXTEND:
449 case ISD::FMA:
454 case ISD::SMIN:
455 case ISD::SMAX:
456 case ISD::UMIN:
457 case ISD::UMAX:
458 case ISD::SMUL_LOHI:
459 case ISD::UMUL_LOHI:
460 case ISD::SADDO:
461 case ISD::UADDO:
462 case ISD::SSUBO:
463 case ISD::USUBO:
464 case ISD::SMULO:
465 case ISD::UMULO:
469 case ISD::FFREXP:
470 case ISD::FMODF:
471 case ISD::FSINCOS:
472 case ISD::FSINCOSPI:
473 case ISD::SADDSAT:
474 case ISD::UADDSAT:
475 case ISD::SSUBSAT:
476 case ISD::USUBSAT:
477 case ISD::SSHLSAT:
478 case ISD::USHLSAT:
481 case ISD::MGATHER:
483 case ISD::SCMP:
484 case ISD::UCMP:
488 case ISD::MASKED_UDIV:
489 case ISD::MASKED_SDIV:
490 case ISD::MASKED_UREM:
491 case ISD::MASKED_SREM:
493 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
494 break;
495 case ISD::SMULFIX:
496 case ISD::SMULFIXSAT:
497 case ISD::UMULFIX:
498 case ISD::UMULFIXSAT:
499 case ISD::SDIVFIX:
500 case ISD::SDIVFIXSAT:
501 case ISD::UDIVFIX:
502 case ISD::UDIVFIXSAT: {
503 unsigned Scale = Node->getConstantOperandVal(2);
504 Action = TLI.getFixedPointOperationAction(Node->getOpcode(),
505 Node->getValueType(0), Scale);
506 break;
507 }
508 case ISD::LROUND:
509 case ISD::LLROUND:
510 case ISD::LRINT:
511 case ISD::LLRINT:
512 case ISD::SINT_TO_FP:
513 case ISD::UINT_TO_FP:
531 case ISD::CTTZ_ELTS:
534 Action = TLI.getOperationAction(Node->getOpcode(),
535 Node->getOperand(0).getValueType());
536 break;
539 Action = TLI.getOperationAction(Node->getOpcode(),
540 Node->getOperand(1).getValueType());
541 break;
542 case ISD::SETCC: {
543 MVT OpVT = Node->getOperand(0).getSimpleValueType();
544 ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get();
545 Action = TLI.getCondCodeAction(CCCode, OpVT);
546 if (Action == TargetLowering::Legal)
547 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
548 break;
549 }
554 Action =
555 TLI.getPartialReduceMLAAction(Op.getOpcode(), Node->getValueType(0),
556 Node->getOperand(1).getValueType());
557 break;
558
559#define BEGIN_REGISTER_VP_SDNODE(VPID, LEGALPOS, ...) \
560 case ISD::VPID: { \
561 EVT LegalizeVT = LEGALPOS < 0 ? Node->getValueType(-(1 + LEGALPOS)) \
562 : Node->getOperand(LEGALPOS).getValueType(); \
563 /* Defer non-vector results to LegalizeDAG. */ \
564 if (!Node->getValueType(0).isVector() && \
565 Node->getValueType(0) != MVT::Other) { \
566 Action = TargetLowering::Legal; \
567 break; \
568 } \
569 Action = TLI.getOperationAction(Node->getOpcode(), LegalizeVT); \
570 } break;
571#include "llvm/IR/VPIntrinsics.def"
572 }
573
574 LLVM_DEBUG(dbgs() << "\nLegalizing vector op: "; Node->dump(&DAG));
575
576 SmallVector<SDValue, 8> ResultVals;
577 switch (Action) {
578 default: llvm_unreachable("This action is not supported yet!");
579 case TargetLowering::Promote:
580 assert((Op.getOpcode() != ISD::LOAD && Op.getOpcode() != ISD::STORE) &&
581 "This action is not supported yet!");
582 LLVM_DEBUG(dbgs() << "Promoting\n");
583 Promote(Node, ResultVals);
584 assert(!ResultVals.empty() && "No results for promotion?");
585 break;
586 case TargetLowering::Legal:
587 LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n");
588 break;
589 case TargetLowering::Custom:
590 LLVM_DEBUG(dbgs() << "Trying custom legalization\n");
591 if (LowerOperationWrapper(Node, ResultVals))
592 break;
593 LLVM_DEBUG(dbgs() << "Could not custom legalize node\n");
594 [[fallthrough]];
595 case TargetLowering::Expand:
596 LLVM_DEBUG(dbgs() << "Expanding\n");
597 Expand(Node, ResultVals);
598 break;
599 }
600
601 if (ResultVals.empty())
602 return TranslateLegalizeResults(Op, Node);
603
604 Changed = true;
605 return RecursivelyLegalizeResults(Op, ResultVals);
606}
607
608// FIXME: This is very similar to TargetLowering::LowerOperationWrapper. Can we
609// merge them somehow?
610bool VectorLegalizer::LowerOperationWrapper(SDNode *Node,
611 SmallVectorImpl<SDValue> &Results) {
612 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
613
614 if (!Res.getNode())
615 return false;
616
617 if (Res == SDValue(Node, 0))
618 return true;
619
620 // If the original node has one result, take the return value from
621 // LowerOperation as is. It might not be result number 0.
622 if (Node->getNumValues() == 1) {
623 Results.push_back(Res);
624 return true;
625 }
626
627 // If the original node has multiple results, then the return node should
628 // have the same number of results.
629 assert((Node->getNumValues() == Res->getNumValues()) &&
630 "Lowering returned the wrong number of results!");
631
632 // Places new result values base on N result number.
633 for (unsigned I = 0, E = Node->getNumValues(); I != E; ++I)
634 Results.push_back(Res.getValue(I));
635
636 return true;
637}
638
639void VectorLegalizer::PromoteSETCC(SDNode *Node,
640 SmallVectorImpl<SDValue> &Results) {
641 MVT VecVT = Node->getOperand(0).getSimpleValueType();
642 MVT NewVecVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VecVT);
643
644 unsigned ExtOp = VecVT.isFloatingPoint() ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
645
646 SDLoc DL(Node);
647 SmallVector<SDValue, 5> Operands(Node->getNumOperands());
648
649 Operands[0] = DAG.getNode(ExtOp, DL, NewVecVT, Node->getOperand(0));
650 Operands[1] = DAG.getNode(ExtOp, DL, NewVecVT, Node->getOperand(1));
651 Operands[2] = Node->getOperand(2);
652
653 EVT ResVT =
654 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), NewVecVT);
655 SDValue Res =
656 DAG.getNode(Node->getOpcode(), DL, ResVT, Operands, Node->getFlags());
657 if (ResVT != Node->getValueType(0))
658 Res = DAG.getBoolExtOrTrunc(Res, DL, Node->getValueType(0), NewVecVT);
659 Results.push_back(Res);
660}
661
662void VectorLegalizer::PromoteSTRICT(SDNode *Node,
663 SmallVectorImpl<SDValue> &Results) {
664 MVT VecVT = Node->getOperand(1).getSimpleValueType();
665 MVT NewVecVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VecVT);
666
667 assert(VecVT.isFloatingPoint());
668
669 SDLoc DL(Node);
670 SmallVector<SDValue, 5> Operands(Node->getNumOperands());
672
673 for (unsigned j = 1; j != Node->getNumOperands(); ++j)
674 if (Node->getOperand(j).getValueType().isVector() &&
675 !(ISD::isVPOpcode(Node->getOpcode()) &&
676 ISD::getVPMaskIdx(Node->getOpcode()) == j)) // Skip mask operand.
677 {
678 // promote the vector operand.
679 SDValue Ext =
680 DAG.getNode(ISD::STRICT_FP_EXTEND, DL, {NewVecVT, MVT::Other},
681 {Node->getOperand(0), Node->getOperand(j)});
682 Operands[j] = Ext.getValue(0);
683 Chains.push_back(Ext.getValue(1));
684 } else
685 Operands[j] = Node->getOperand(j); // Skip no vector operand.
686
687 SDVTList VTs = DAG.getVTList(NewVecVT, Node->getValueType(1));
688
689 Operands[0] = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
690
691 SDValue Res =
692 DAG.getNode(Node->getOpcode(), DL, VTs, Operands, Node->getFlags());
693
694 SDValue Round =
695 DAG.getNode(ISD::STRICT_FP_ROUND, DL, {VecVT, MVT::Other},
696 {Res.getValue(1), Res.getValue(0),
697 DAG.getIntPtrConstant(0, DL, /*isTarget=*/true)});
698
699 Results.push_back(Round.getValue(0));
700 Results.push_back(Round.getValue(1));
701}
702
703void VectorLegalizer::PromoteFloatVECREDUCE(SDNode *Node,
704 SmallVectorImpl<SDValue> &Results,
705 bool NonArithmetic) {
706 MVT OpVT = Node->getOperand(0).getSimpleValueType();
707 assert(OpVT.isFloatingPoint() && "Expected floating point reduction!");
708 MVT NewOpVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OpVT);
709
710 SDLoc DL(Node);
711 SDValue NewOp = DAG.getNode(ISD::FP_EXTEND, DL, NewOpVT, Node->getOperand(0));
712 SDValue Rdx =
713 DAG.getNode(Node->getOpcode(), DL, NewOpVT.getVectorElementType(), NewOp,
714 Node->getFlags());
715 SDValue Res =
716 DAG.getNode(ISD::FP_ROUND, DL, Node->getValueType(0), Rdx,
717 DAG.getIntPtrConstant(NonArithmetic, DL, /*isTarget=*/true));
718 Results.push_back(Res);
719}
720
721void VectorLegalizer::PromoteVECTOR_COMPRESS(
722 SDNode *Node, SmallVectorImpl<SDValue> &Results) {
723 SDLoc DL(Node);
724 EVT VT = Node->getValueType(0);
725 MVT PromotedVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT.getSimpleVT());
726 assert((VT.isInteger() || VT.getSizeInBits() == PromotedVT.getSizeInBits()) &&
727 "Only integer promotion or bitcasts between types is supported");
728
729 SDValue Vec = Node->getOperand(0);
730 SDValue Mask = Node->getOperand(1);
731 SDValue Passthru = Node->getOperand(2);
732 if (VT.isInteger()) {
733 Vec = DAG.getNode(ISD::ANY_EXTEND, DL, PromotedVT, Vec);
734 Mask = TLI.promoteTargetBoolean(DAG, Mask, PromotedVT);
735 Passthru = DAG.getNode(ISD::ANY_EXTEND, DL, PromotedVT, Passthru);
736 } else {
737 Vec = DAG.getBitcast(PromotedVT, Vec);
738 Passthru = DAG.getBitcast(PromotedVT, Passthru);
739 }
740
741 SDValue Result =
742 DAG.getNode(ISD::VECTOR_COMPRESS, DL, PromotedVT, Vec, Mask, Passthru);
743 Result = VT.isInteger() ? DAG.getNode(ISD::TRUNCATE, DL, VT, Result)
744 : DAG.getBitcast(VT, Result);
745 Results.push_back(Result);
746}
747
748void VectorLegalizer::Promote(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
749 // For a few operations there is a specific concept for promotion based on
750 // the operand's type.
751 switch (Node->getOpcode()) {
752 case ISD::SINT_TO_FP:
753 case ISD::UINT_TO_FP:
756 // "Promote" the operation by extending the operand.
757 PromoteINT_TO_FP(Node, Results);
758 return;
759 case ISD::FP_TO_UINT:
760 case ISD::FP_TO_SINT:
763 // Promote the operation by extending the operand.
764 PromoteFP_TO_INT(Node, Results);
765 return;
766 case ISD::SETCC:
767 // Promote the operation by extending the operand.
768 PromoteSETCC(Node, Results);
769 return;
770 case ISD::STRICT_FADD:
771 case ISD::STRICT_FSUB:
772 case ISD::STRICT_FMUL:
773 case ISD::STRICT_FDIV:
775 case ISD::STRICT_FMA:
776 PromoteSTRICT(Node, Results);
777 return;
780 PromoteFloatVECREDUCE(Node, Results, /*NonArithmetic=*/false);
781 return;
788 PromoteFloatVECREDUCE(Node, Results, /*NonArithmetic=*/true);
789 return;
791 PromoteVECTOR_COMPRESS(Node, Results);
792 return;
793
794 case ISD::FP_ROUND:
795 case ISD::FP_EXTEND:
796 // These operations are used to do promotion so they can't be promoted
797 // themselves.
798 llvm_unreachable("Don't know how to promote this operation!");
799 }
800
801 // There are currently two cases of vector promotion:
802 // 1) Bitcasting a vector of integers to a different type to a vector of the
803 // same overall length. For example, x86 promotes ISD::AND v2i32 to v1i64.
804 // 2) Extending a vector of floats to a vector of the same number of larger
805 // floats. For example, AArch64 promotes ISD::FADD on v4f16 to v4f32.
806 assert(Node->getNumValues() == 1 &&
807 "Can't promote a vector with multiple results!");
808 MVT VT = Node->getSimpleValueType(0);
809 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
810 SDLoc dl(Node);
811 SmallVector<SDValue, 4> Operands(Node->getNumOperands());
812
813 for (unsigned j = 0; j != Node->getNumOperands(); ++j) {
814 // Do not promote the mask operand of a VP OP.
815 bool SkipPromote = ISD::isVPOpcode(Node->getOpcode()) &&
816 ISD::getVPMaskIdx(Node->getOpcode()) == j;
817 if (Node->getOperand(j).getValueType().isVector() && !SkipPromote)
818 if (Node->getOperand(j)
819 .getValueType()
820 .getVectorElementType()
821 .isFloatingPoint() &&
823 Operands[j] = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(j));
824 else
825 Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(j));
826 else
827 Operands[j] = Node->getOperand(j);
828 }
829
830 SDValue Res =
831 DAG.getNode(Node->getOpcode(), dl, NVT, Operands, Node->getFlags());
832
833 if ((VT.isFloatingPoint() && NVT.isFloatingPoint()) ||
836 Res = DAG.getNode(ISD::FP_ROUND, dl, VT, Res,
837 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
838 else
839 Res = DAG.getNode(ISD::BITCAST, dl, VT, Res);
840
841 Results.push_back(Res);
842}
843
844void VectorLegalizer::PromoteINT_TO_FP(SDNode *Node,
845 SmallVectorImpl<SDValue> &Results) {
846 // INT_TO_FP operations may require the input operand be promoted even
847 // when the type is otherwise legal.
848 bool IsStrict = Node->isStrictFPOpcode();
849 MVT VT = Node->getOperand(IsStrict ? 1 : 0).getSimpleValueType();
850 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
852 "Vectors have different number of elements!");
853
854 SDLoc dl(Node);
855 SmallVector<SDValue, 4> Operands(Node->getNumOperands());
856
857 unsigned Opc = (Node->getOpcode() == ISD::UINT_TO_FP ||
858 Node->getOpcode() == ISD::STRICT_UINT_TO_FP)
861 for (unsigned j = 0; j != Node->getNumOperands(); ++j) {
862 if (Node->getOperand(j).getValueType().isVector())
863 Operands[j] = DAG.getNode(Opc, dl, NVT, Node->getOperand(j));
864 else
865 Operands[j] = Node->getOperand(j);
866 }
867
868 if (IsStrict) {
869 SDValue Res = DAG.getNode(Node->getOpcode(), dl,
870 {Node->getValueType(0), MVT::Other}, Operands);
871 Results.push_back(Res);
872 Results.push_back(Res.getValue(1));
873 return;
874 }
875
876 SDValue Res =
877 DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Operands);
878 Results.push_back(Res);
879}
880
881// For FP_TO_INT we promote the result type to a vector type with wider
882// elements and then truncate the result. This is different from the default
883// PromoteVector which uses bitcast to promote thus assumning that the
884// promoted vector type has the same overall size.
885void VectorLegalizer::PromoteFP_TO_INT(SDNode *Node,
886 SmallVectorImpl<SDValue> &Results) {
887 MVT VT = Node->getSimpleValueType(0);
888 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
889 bool IsStrict = Node->isStrictFPOpcode();
891 "Vectors have different number of elements!");
892
893 unsigned NewOpc = Node->getOpcode();
894 // Change FP_TO_UINT to FP_TO_SINT if possible.
895 // TODO: Should we only do this if FP_TO_UINT itself isn't legal?
896 if (NewOpc == ISD::FP_TO_UINT &&
898 NewOpc = ISD::FP_TO_SINT;
899
900 if (NewOpc == ISD::STRICT_FP_TO_UINT &&
902 NewOpc = ISD::STRICT_FP_TO_SINT;
903
904 SDLoc dl(Node);
905 SDValue Promoted, Chain;
906 if (IsStrict) {
907 Promoted = DAG.getNode(NewOpc, dl, {NVT, MVT::Other},
908 {Node->getOperand(0), Node->getOperand(1)});
909 Chain = Promoted.getValue(1);
910 } else
911 Promoted = DAG.getNode(NewOpc, dl, NVT, Node->getOperand(0));
912
913 // Assert that the converted value fits in the original type. If it doesn't
914 // (eg: because the value being converted is too big), then the result of the
915 // original operation was undefined anyway, so the assert is still correct.
916 if (Node->getOpcode() == ISD::FP_TO_UINT ||
917 Node->getOpcode() == ISD::STRICT_FP_TO_UINT)
918 NewOpc = ISD::AssertZext;
919 else
920 NewOpc = ISD::AssertSext;
921
922 Promoted = DAG.getNode(NewOpc, dl, NVT, Promoted,
923 DAG.getValueType(VT.getScalarType()));
924 Promoted = DAG.getNode(ISD::TRUNCATE, dl, VT, Promoted);
925 Results.push_back(Promoted);
926 if (IsStrict)
927 Results.push_back(Chain);
928}
929
930std::pair<SDValue, SDValue> VectorLegalizer::ExpandLoad(SDNode *N) {
931 LoadSDNode *LD = cast<LoadSDNode>(N);
932 return TLI.scalarizeVectorLoad(LD, DAG);
933}
934
935SDValue VectorLegalizer::ExpandStore(SDNode *N) {
936 StoreSDNode *ST = cast<StoreSDNode>(N);
937 SDValue TF = TLI.scalarizeVectorStore(ST, DAG);
938 return TF;
939}
940
941void VectorLegalizer::Expand(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
942 switch (Node->getOpcode()) {
943 case ISD::LOAD: {
944 std::pair<SDValue, SDValue> Tmp = ExpandLoad(Node);
945 Results.push_back(Tmp.first);
946 Results.push_back(Tmp.second);
947 return;
948 }
949 case ISD::STORE:
950 Results.push_back(ExpandStore(Node));
951 return;
953 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
954 Results.push_back(Node->getOperand(i));
955 return;
957 if (SDValue Expanded = ExpandSEXTINREG(Node)) {
958 Results.push_back(Expanded);
959 return;
960 }
961 break;
963 Results.push_back(ExpandANY_EXTEND_VECTOR_INREG(Node));
964 return;
966 Results.push_back(ExpandSIGN_EXTEND_VECTOR_INREG(Node));
967 return;
969 Results.push_back(ExpandZERO_EXTEND_VECTOR_INREG(Node));
970 return;
971 case ISD::BSWAP:
972 if (SDValue Expanded = ExpandBSWAP(Node)) {
973 Results.push_back(Expanded);
974 return;
975 }
976 break;
977 case ISD::VSELECT:
978 if (SDValue Expanded = ExpandVSELECT(Node)) {
979 Results.push_back(Expanded);
980 return;
981 }
982 break;
983 case ISD::VP_SREM:
984 case ISD::VP_UREM:
985 if (SDValue Expanded = ExpandVP_REM(Node)) {
986 Results.push_back(Expanded);
987 return;
988 }
989 break;
990 case ISD::SELECT:
991 if (SDValue Expanded = ExpandSELECT(Node)) {
992 Results.push_back(Expanded);
993 return;
994 }
995 break;
996 case ISD::SELECT_CC: {
997 if (Node->getValueType(0).isScalableVector()) {
998 EVT CondVT = TLI.getSetCCResultType(
999 DAG.getDataLayout(), *DAG.getContext(), Node->getValueType(0));
1000 SDValue SetCC =
1001 DAG.getNode(ISD::SETCC, SDLoc(Node), CondVT, Node->getOperand(0),
1002 Node->getOperand(1), Node->getOperand(4));
1003 Results.push_back(DAG.getSelect(SDLoc(Node), Node->getValueType(0), SetCC,
1004 Node->getOperand(2),
1005 Node->getOperand(3)));
1006 return;
1007 }
1008 break;
1009 }
1010 case ISD::FP_TO_UINT:
1011 ExpandFP_TO_UINT(Node, Results);
1012 return;
1013 case ISD::UINT_TO_FP:
1014 ExpandUINT_TO_FLOAT(Node, Results);
1015 return;
1016 case ISD::FNEG:
1017 if (SDValue Expanded = ExpandFNEG(Node)) {
1018 Results.push_back(Expanded);
1019 return;
1020 }
1021 break;
1022 case ISD::FABS:
1023 if (SDValue Expanded = ExpandFABS(Node)) {
1024 Results.push_back(Expanded);
1025 return;
1026 }
1027 break;
1028 case ISD::FCOPYSIGN:
1029 if (SDValue Expanded = ExpandFCOPYSIGN(Node)) {
1030 Results.push_back(Expanded);
1031 return;
1032 }
1033 break;
1034 case ISD::FCANONICALIZE: {
1035 // If the scalar element type has a
1036 // Legal/Custom FCANONICALIZE, don't
1037 // mess with the vector, fall back.
1038 EVT VT = Node->getValueType(0);
1039 EVT EltVT = VT.getVectorElementType();
1040 if (!VT.isScalableVector() &&
1042 TargetLowering::Expand)
1043 break;
1044 // Otherwise canonicalize the whole vector.
1045 SDValue Mul = TLI.expandFCANONICALIZE(Node, DAG);
1046 Results.push_back(Mul);
1047 return;
1048 }
1049 case ISD::FSUB:
1050 ExpandFSUB(Node, Results);
1051 return;
1052 case ISD::SETCC:
1053 ExpandSETCC(Node, Results);
1054 return;
1055 case ISD::ABS:
1057 if (SDValue Expanded = TLI.expandABS(Node, DAG)) {
1058 Results.push_back(Expanded);
1059 return;
1060 }
1061 break;
1062 case ISD::ABDS:
1063 case ISD::ABDU:
1064 if (SDValue Expanded = TLI.expandABD(Node, DAG)) {
1065 Results.push_back(Expanded);
1066 return;
1067 }
1068 break;
1069 case ISD::AVGCEILS:
1070 case ISD::AVGCEILU:
1071 case ISD::AVGFLOORS:
1072 case ISD::AVGFLOORU:
1073 if (SDValue Expanded = TLI.expandAVG(Node, DAG)) {
1074 Results.push_back(Expanded);
1075 return;
1076 }
1077 break;
1078 case ISD::BITREVERSE:
1079 if (SDValue Expanded = ExpandBITREVERSE(Node)) {
1080 Results.push_back(Expanded);
1081 return;
1082 }
1083 break;
1084 case ISD::CTPOP:
1085 if (SDValue Expanded = TLI.expandCTPOP(Node, DAG)) {
1086 Results.push_back(Expanded);
1087 return;
1088 }
1089 break;
1090 case ISD::CTLZ:
1092 if (SDValue Expanded = TLI.expandCTLZ(Node, DAG)) {
1093 Results.push_back(Expanded);
1094 return;
1095 }
1096 break;
1097 case ISD::CTTZ:
1099 if (SDValue Expanded = TLI.expandCTTZ(Node, DAG)) {
1100 Results.push_back(Expanded);
1101 return;
1102 }
1103 break;
1104 case ISD::FSHL:
1105 case ISD::FSHR:
1106 if (SDValue Expanded = TLI.expandFunnelShift(Node, DAG)) {
1107 Results.push_back(Expanded);
1108 return;
1109 }
1110 break;
1111 case ISD::CLMUL:
1112 case ISD::CLMULR:
1113 case ISD::CLMULH:
1114 if (SDValue Expanded = TLI.expandCLMUL(Node, DAG)) {
1115 Results.push_back(Expanded);
1116 return;
1117 }
1118 break;
1119 case ISD::PEXT:
1120 Results.push_back(TLI.expandPEXT(Node, DAG));
1121 return;
1122 case ISD::PDEP:
1123 Results.push_back(TLI.expandPDEP(Node, DAG));
1124 return;
1125 case ISD::ROTL:
1126 case ISD::ROTR:
1127 if (SDValue Expanded = TLI.expandROT(Node, false /*AllowVectorOps*/, DAG)) {
1128 Results.push_back(Expanded);
1129 return;
1130 }
1131 break;
1132 case ISD::FMINNUM:
1133 case ISD::FMAXNUM:
1134 if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG)) {
1135 Results.push_back(Expanded);
1136 return;
1137 }
1138 break;
1139 case ISD::FMINIMUM:
1140 case ISD::FMAXIMUM:
1141 Results.push_back(TLI.expandFMINIMUM_FMAXIMUM(Node, DAG));
1142 return;
1143 case ISD::FMINIMUMNUM:
1144 case ISD::FMAXIMUMNUM:
1145 Results.push_back(TLI.expandFMINIMUMNUM_FMAXIMUMNUM(Node, DAG));
1146 return;
1147 case ISD::SMIN:
1148 case ISD::SMAX:
1149 case ISD::UMIN:
1150 case ISD::UMAX:
1151 if (SDValue Expanded = TLI.expandIntMINMAX(Node, DAG)) {
1152 Results.push_back(Expanded);
1153 return;
1154 }
1155 break;
1156 case ISD::UADDO:
1157 case ISD::USUBO:
1158 ExpandUADDSUBO(Node, Results);
1159 return;
1160 case ISD::SADDO:
1161 case ISD::SSUBO:
1162 ExpandSADDSUBO(Node, Results);
1163 return;
1164 case ISD::UMULO:
1165 case ISD::SMULO:
1166 ExpandMULO(Node, Results);
1167 return;
1168 case ISD::MULHS:
1169 case ISD::MULHU:
1170 if (SDValue Expanded = TLI.expandMULH(Node, DAG)) {
1171 Results.push_back(Expanded);
1172 return;
1173 }
1174 break;
1175 case ISD::USUBSAT:
1176 case ISD::SSUBSAT:
1177 case ISD::UADDSAT:
1178 case ISD::SADDSAT:
1179 if (SDValue Expanded = TLI.expandAddSubSat(Node, DAG)) {
1180 Results.push_back(Expanded);
1181 return;
1182 }
1183 break;
1184 case ISD::USHLSAT:
1185 case ISD::SSHLSAT:
1186 if (SDValue Expanded = TLI.expandShlSat(Node, DAG)) {
1187 Results.push_back(Expanded);
1188 return;
1189 }
1190 break;
1193 // Expand the fpsosisat if it is scalable to prevent it from unrolling below.
1194 if (Node->getValueType(0).isScalableVector()) {
1195 if (SDValue Expanded = TLI.expandFP_TO_INT_SAT(Node, DAG)) {
1196 Results.push_back(Expanded);
1197 return;
1198 }
1199 }
1200 break;
1201 case ISD::SMULFIX:
1202 case ISD::UMULFIX:
1203 case ISD::SMULFIXSAT:
1204 case ISD::UMULFIXSAT:
1205 if (SDValue Expanded = TLI.expandFixedPointMul(Node, DAG)) {
1206 Results.push_back(Expanded);
1207 return;
1208 }
1209 break;
1210 case ISD::SDIVFIX:
1211 case ISD::UDIVFIX:
1212 ExpandFixedPointDiv(Node, Results);
1213 return;
1214 case ISD::SDIVFIXSAT:
1215 case ISD::UDIVFIXSAT:
1216 break;
1217#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
1218 case ISD::STRICT_##DAGN:
1219#include "llvm/IR/ConstrainedOps.def"
1220 ExpandStrictFPOp(Node, Results);
1221 return;
1222 case ISD::VECREDUCE_ADD:
1223 case ISD::VECREDUCE_MUL:
1224 case ISD::VECREDUCE_AND:
1225 case ISD::VECREDUCE_OR:
1226 case ISD::VECREDUCE_XOR:
1239 Results.push_back(TLI.expandVecReduce(Node, DAG));
1240 return;
1245 Results.push_back(TLI.expandPartialReduceMLA(Node, DAG));
1246 return;
1249 Results.push_back(TLI.expandVecReduceSeq(Node, DAG));
1250 return;
1251 case ISD::VECTOR_MATCH:
1252 Results.push_back(TLI.expandVectorMatch(Node, DAG));
1253 return;
1254 case ISD::SREM:
1255 case ISD::UREM:
1256 ExpandREM(Node, Results);
1257 return;
1258 case ISD::VP_MERGE:
1259 if (SDValue Expanded = ExpandVP_MERGE(Node)) {
1260 Results.push_back(Expanded);
1261 return;
1262 }
1263 break;
1264 case ISD::FREM:
1265 if (tryExpandVecMathCall(Node, RTLIB::getREM, Results))
1266 return;
1267 break;
1268 case ISD::FSINCOS:
1269 case ISD::FSINCOSPI: {
1270 EVT VT = Node->getValueType(0);
1271 RTLIB::Libcall LC = Node->getOpcode() == ISD::FSINCOS
1272 ? RTLIB::getSINCOS(VT)
1273 : RTLIB::getSINCOSPI(VT);
1274 if (LC != RTLIB::UNKNOWN_LIBCALL &&
1275 TLI.expandMultipleResultFPLibCall(DAG, LC, Node, Results))
1276 return;
1277
1278 // TODO: Try to see if there's a narrower call available to use before
1279 // scalarizing.
1280 break;
1281 }
1282 case ISD::FPOW:
1283 if (tryExpandVecMathCall(Node, RTLIB::getPOW, Results))
1284 return;
1285
1286 // TODO: Try to see if there's a narrower call available to use before
1287 // scalarizing.
1288 break;
1289 case ISD::FCBRT:
1290 if (tryExpandVecMathCall(Node, RTLIB::getCBRT, Results))
1291 return;
1292
1293 // TODO: Try to see if there's a narrower call available to use before
1294 // scalarizing.
1295 break;
1296 case ISD::FMODF: {
1297 EVT VT = Node->getValueType(0);
1298 RTLIB::Libcall LC = RTLIB::getMODF(VT);
1299 if (LC != RTLIB::UNKNOWN_LIBCALL &&
1300 TLI.expandMultipleResultFPLibCall(DAG, LC, Node, Results,
1301 /*CallRetResNo=*/0))
1302 return;
1303 break;
1304 }
1306 Results.push_back(TLI.expandVECTOR_COMPRESS(Node, DAG));
1307 return;
1308 case ISD::CTTZ_ELTS:
1310 Results.push_back(TLI.expandCttzElts(Node, DAG));
1311 return;
1313 Results.push_back(TLI.expandVectorFindLastActive(Node, DAG));
1314 return;
1315 case ISD::SCMP:
1316 case ISD::UCMP:
1317 Results.push_back(TLI.expandCMP(Node, DAG));
1318 return;
1320 if (SDValue R = ExpandGET_ACTIVE_LANE_MASK(Node))
1321 Results.push_back(R);
1322 return;
1325 Results.push_back(ExpandLOOP_DEPENDENCE_MASK(Node));
1326 return;
1327
1328 case ISD::FADD:
1329 case ISD::FMUL:
1330 case ISD::FMA:
1331 case ISD::FDIV:
1332 case ISD::FCEIL:
1333 case ISD::FFLOOR:
1334 case ISD::FNEARBYINT:
1335 case ISD::FRINT:
1336 case ISD::FROUND:
1337 case ISD::FROUNDEVEN:
1338 case ISD::FTRUNC:
1339 case ISD::FSQRT:
1340 if (SDValue Expanded = TLI.expandVectorNaryOpBySplitting(Node, DAG)) {
1341 Results.push_back(Expanded);
1342 return;
1343 }
1344 break;
1346 if (SDValue Expanded = TLI.expandCONVERT_TO_ARBITRARY_FP(Node, DAG))
1347 Results.push_back(Expanded);
1348 else
1349 Results.push_back(DAG.getPOISON(Node->getValueType(0)));
1350 return;
1352 if (SDValue Expanded = TLI.expandCONVERT_FROM_ARBITRARY_FP(Node, DAG))
1353 Results.push_back(Expanded);
1354 else
1355 Results.push_back(DAG.getPOISON(Node->getValueType(0)));
1356 return;
1357 case ISD::MASKED_UDIV:
1358 case ISD::MASKED_SDIV:
1359 case ISD::MASKED_UREM:
1360 case ISD::MASKED_SREM:
1361 Results.push_back(ExpandMaskedBinOp(Node));
1362 return;
1363 }
1364
1365 SDValue Unrolled = DAG.UnrollVectorOp(Node);
1366 if (Node->getNumValues() == 1) {
1367 Results.push_back(Unrolled);
1368 } else {
1369 assert(Node->getNumValues() == Unrolled->getNumValues() &&
1370 "VectorLegalizer Expand returned wrong number of results!");
1371 for (unsigned I = 0, E = Unrolled->getNumValues(); I != E; ++I)
1372 Results.push_back(Unrolled.getValue(I));
1373 }
1374}
1375
1376SDValue VectorLegalizer::ExpandSELECT(SDNode *Node) {
1377 // Lower a select instruction where the condition is a scalar and the
1378 // operands are vectors. Lower this select to VSELECT and implement it
1379 // using XOR AND OR. The selector bit is broadcasted.
1380 EVT VT = Node->getValueType(0);
1381 SDLoc DL(Node);
1382
1383 SDValue Mask = Node->getOperand(0);
1384 SDValue Op1 = Node->getOperand(1);
1385 SDValue Op2 = Node->getOperand(2);
1386
1387 assert(VT.isVector() && !Mask.getValueType().isVector()
1388 && Op1.getValueType() == Op2.getValueType() && "Invalid type");
1389
1390 // If we can't even use the basic vector operations of
1391 // AND,OR,XOR, we will have to scalarize the op.
1392 // Notice that the operation may be 'promoted' which means that it is
1393 // 'bitcasted' to another type which is handled.
1394 // Also, we need to be able to construct a splat vector using either
1395 // BUILD_VECTOR or SPLAT_VECTOR.
1396 // FIXME: Should we also permit fixed-length SPLAT_VECTOR as a fallback to
1397 // BUILD_VECTOR?
1398 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
1399 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
1400 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
1403 VT) == TargetLowering::Expand)
1404 return SDValue();
1405
1406 // Generate a mask operand.
1407 EVT MaskTy = VT.changeVectorElementTypeToInteger();
1408
1409 // What is the size of each element in the vector mask.
1410 EVT BitTy = MaskTy.getScalarType();
1411
1412 Mask = DAG.getSelect(DL, BitTy, Mask, DAG.getAllOnesConstant(DL, BitTy),
1413 DAG.getConstant(0, DL, BitTy));
1414
1415 // Broadcast the mask so that the entire vector is all one or all zero.
1416 Mask = DAG.getSplat(MaskTy, DL, Mask);
1417
1418 // Bitcast the operands to be the same type as the mask.
1419 // This is needed when we select between FP types because
1420 // the mask is a vector of integers.
1421 Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1);
1422 Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2);
1423
1424 SDValue NotMask = DAG.getNOT(DL, Mask, MaskTy);
1425
1426 Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask);
1427 Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask);
1428 SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2);
1429 return DAG.getNode(ISD::BITCAST, DL, Node->getValueType(0), Val);
1430}
1431
1432SDValue VectorLegalizer::ExpandSEXTINREG(SDNode *Node) {
1433 EVT VT = Node->getValueType(0);
1434
1435 // Make sure that the SRA and SHL instructions are available.
1436 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand ||
1437 TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand)
1438 return SDValue();
1439
1440 SDLoc DL(Node);
1441 EVT OrigTy = cast<VTSDNode>(Node->getOperand(1))->getVT();
1442
1443 unsigned BW = VT.getScalarSizeInBits();
1444 unsigned OrigBW = OrigTy.getScalarSizeInBits();
1445 SDValue ShiftSz = DAG.getConstant(BW - OrigBW, DL, VT);
1446
1447 SDValue Op = DAG.getNode(ISD::SHL, DL, VT, Node->getOperand(0), ShiftSz);
1448 return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz);
1449}
1450
1451// Generically expand a vector anyext in register to a shuffle of the relevant
1452// lanes into the appropriate locations, with other lanes left undef.
1453SDValue VectorLegalizer::ExpandANY_EXTEND_VECTOR_INREG(SDNode *Node) {
1454 SDLoc DL(Node);
1455 EVT VT = Node->getValueType(0);
1456 int NumElements = VT.getVectorNumElements();
1457 SDValue Src = Node->getOperand(0);
1458 EVT SrcVT = Src.getValueType();
1459 int NumSrcElements = SrcVT.getVectorNumElements();
1460
1461 // *_EXTEND_VECTOR_INREG SrcVT can be smaller than VT - so insert the vector
1462 // into a larger vector type.
1463 if (SrcVT.bitsLE(VT)) {
1464 assert((VT.getSizeInBits() % SrcVT.getScalarSizeInBits()) == 0 &&
1465 "ANY_EXTEND_VECTOR_INREG vector size mismatch");
1466 NumSrcElements = VT.getSizeInBits() / SrcVT.getScalarSizeInBits();
1467 SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getScalarType(),
1468 NumSrcElements);
1469 Src = DAG.getInsertSubvector(DL, DAG.getUNDEF(SrcVT), Src, 0);
1470 }
1471
1472 // Build a base mask of undef shuffles.
1473 SmallVector<int, 16> ShuffleMask;
1474 ShuffleMask.resize(NumSrcElements, -1);
1475
1476 // Place the extended lanes into the correct locations.
1477 int ExtLaneScale = NumSrcElements / NumElements;
1478 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
1479 for (int i = 0; i < NumElements; ++i)
1480 ShuffleMask[i * ExtLaneScale + EndianOffset] = i;
1481
1482 return DAG.getNode(
1483 ISD::BITCAST, DL, VT,
1484 DAG.getVectorShuffle(SrcVT, DL, Src, DAG.getPOISON(SrcVT), ShuffleMask));
1485}
1486
1487SDValue VectorLegalizer::ExpandSIGN_EXTEND_VECTOR_INREG(SDNode *Node) {
1488 SDLoc DL(Node);
1489 EVT VT = Node->getValueType(0);
1490 SDValue Src = Node->getOperand(0);
1491 EVT SrcVT = Src.getValueType();
1492
1493 // First build an any-extend node which can be legalized above when we
1494 // recurse through it.
1495 SDValue Op = DAG.getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Src);
1496
1497 // Now we need sign extend. This will be exanded to shifts if it isn't
1498 // supported.
1499 EVT ExtVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getVectorElementType(),
1501 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Op,
1502 DAG.getValueType(ExtVT));
1503}
1504
1505// Generically expand a vector zext in register to a shuffle of the relevant
1506// lanes into the appropriate locations, a blend of zero into the high bits,
1507// and a bitcast to the wider element type.
1508SDValue VectorLegalizer::ExpandZERO_EXTEND_VECTOR_INREG(SDNode *Node) {
1509 SDLoc DL(Node);
1510 EVT VT = Node->getValueType(0);
1511 int NumElements = VT.getVectorNumElements();
1512 SDValue Src = Node->getOperand(0);
1513 EVT SrcVT = Src.getValueType();
1514 int NumSrcElements = SrcVT.getVectorNumElements();
1515
1516 // *_EXTEND_VECTOR_INREG SrcVT can be smaller than VT - so insert the vector
1517 // into a larger vector type.
1518 if (SrcVT.bitsLE(VT)) {
1519 assert((VT.getSizeInBits() % SrcVT.getScalarSizeInBits()) == 0 &&
1520 "ZERO_EXTEND_VECTOR_INREG vector size mismatch");
1521 NumSrcElements = VT.getSizeInBits() / SrcVT.getScalarSizeInBits();
1522 SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getScalarType(),
1523 NumSrcElements);
1524 Src = DAG.getInsertSubvector(DL, DAG.getUNDEF(SrcVT), Src, 0);
1525 }
1526
1527 // Build up a zero vector to blend into this one.
1528 SDValue Zero = DAG.getConstant(0, DL, SrcVT);
1529
1530 // Shuffle the incoming lanes into the correct position, and pull all other
1531 // lanes from the zero vector.
1532 auto ShuffleMask = llvm::to_vector<16>(llvm::seq<int>(0, NumSrcElements));
1533
1534 int ExtLaneScale = NumSrcElements / NumElements;
1535 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
1536 for (int i = 0; i < NumElements; ++i)
1537 ShuffleMask[i * ExtLaneScale + EndianOffset] = NumSrcElements + i;
1538
1539 return DAG.getNode(ISD::BITCAST, DL, VT,
1540 DAG.getVectorShuffle(SrcVT, DL, Zero, Src, ShuffleMask));
1541}
1542
1543static void createBSWAPShuffleMask(EVT VT, SmallVectorImpl<int> &ShuffleMask) {
1544 int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8;
1545 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I)
1546 for (int J = ScalarSizeInBytes - 1; J >= 0; --J)
1547 ShuffleMask.push_back((I * ScalarSizeInBytes) + J);
1548}
1549
1550SDValue VectorLegalizer::ExpandBSWAP(SDNode *Node) {
1551 EVT VT = Node->getValueType(0);
1552
1553 // Scalable vectors can't use shuffle expansion.
1554 if (VT.isScalableVector())
1555 return TLI.expandBSWAP(Node, DAG);
1556
1557 // Generate a byte wise shuffle mask for the BSWAP.
1558 SmallVector<int, 16> ShuffleMask;
1559 createBSWAPShuffleMask(VT, ShuffleMask);
1560 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size());
1561
1562 // Only emit a shuffle if the mask is legal.
1563 if (TLI.isShuffleMaskLegal(ShuffleMask, ByteVT)) {
1564 SDLoc DL(Node);
1565 SDValue Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Node->getOperand(0));
1566 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getPOISON(ByteVT),
1567 ShuffleMask);
1568 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
1569 }
1570
1571 // If we have the appropriate vector bit operations, it is better to use them
1572 // than unrolling and expanding each component.
1573 if (TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
1577 return TLI.expandBSWAP(Node, DAG);
1578
1579 // Otherwise let the caller unroll.
1580 return SDValue();
1581}
1582
1583SDValue VectorLegalizer::ExpandBITREVERSE(SDNode *Node) {
1584 EVT VT = Node->getValueType(0);
1585
1586 // We can't unroll or use shuffles for scalable vectors.
1587 if (VT.isScalableVector())
1588 return TLI.expandBITREVERSE(Node, DAG);
1589
1590 // If we have the scalar operation, it's probably cheaper to unroll it.
1592 return SDValue();
1593
1594 // If the vector element width is a whole number of bytes, test if its legal
1595 // to BSWAP shuffle the bytes and then perform the BITREVERSE on the byte
1596 // vector. This greatly reduces the number of bit shifts necessary.
1597 unsigned ScalarSizeInBits = VT.getScalarSizeInBits();
1598 if (ScalarSizeInBits > 8 && (ScalarSizeInBits % 8) == 0) {
1599 SmallVector<int, 16> BSWAPMask;
1600 createBSWAPShuffleMask(VT, BSWAPMask);
1601
1602 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, BSWAPMask.size());
1603 if (TLI.isShuffleMaskLegal(BSWAPMask, ByteVT) &&
1605 (TLI.isOperationLegalOrCustom(ISD::SHL, ByteVT) &&
1606 TLI.isOperationLegalOrCustom(ISD::SRL, ByteVT) &&
1609 SDLoc DL(Node);
1610 SDValue Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Node->getOperand(0));
1611 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getPOISON(ByteVT),
1612 BSWAPMask);
1613 Op = DAG.getNode(ISD::BITREVERSE, DL, ByteVT, Op);
1614 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
1615 return Op;
1616 }
1617 }
1618
1619 // If we have the appropriate vector bit operations, it is better to use them
1620 // than unrolling and expanding each component.
1621 if (TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
1625 return TLI.expandBITREVERSE(Node, DAG);
1626
1627 // Otherwise unroll.
1628 return SDValue();
1629}
1630
1631SDValue VectorLegalizer::ExpandVSELECT(SDNode *Node) {
1632 // Implement VSELECT in terms of XOR, AND, OR
1633 // on platforms which do not support blend natively.
1634 SDLoc DL(Node);
1635
1636 SDValue Mask = Node->getOperand(0);
1637 SDValue Op1 = Node->getOperand(1);
1638 SDValue Op2 = Node->getOperand(2);
1639
1640 EVT VT = Mask.getValueType();
1641
1642 // If we can't even use the basic vector operations of
1643 // AND,OR,XOR, we will have to scalarize the op.
1644 // Notice that the operation may be 'promoted' which means that it is
1645 // 'bitcasted' to another type which is handled.
1646 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
1647 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
1648 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand)
1649 return SDValue();
1650
1651 // This operation also isn't safe with AND, OR, XOR when the boolean type is
1652 // 0/1 and the select operands aren't also booleans, as we need an all-ones
1653 // vector constant to mask with.
1654 // FIXME: Sign extend 1 to all ones if that's legal on the target.
1655 auto BoolContents = TLI.getBooleanContents(Op1.getValueType());
1656 if (BoolContents != TargetLowering::ZeroOrNegativeOneBooleanContent &&
1657 !(BoolContents == TargetLowering::ZeroOrOneBooleanContent &&
1658 Op1.getValueType().getVectorElementType() == MVT::i1))
1659 return SDValue();
1660
1661 // If the mask and the type are different sizes, unroll the vector op. This
1662 // can occur when getSetCCResultType returns something that is different in
1663 // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8.
1664 if (VT.getSizeInBits() != Op1.getValueSizeInBits())
1665 return SDValue();
1666
1667 // Bitcast the operands to be the same type as the mask.
1668 // This is needed when we select between FP types because
1669 // the mask is a vector of integers.
1670 Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
1671 Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
1672
1673 SDValue NotMask = DAG.getNOT(DL, Mask, VT);
1674
1675 Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
1676 Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
1677 SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
1678 return DAG.getNode(ISD::BITCAST, DL, Node->getValueType(0), Val);
1679}
1680
1681SDValue VectorLegalizer::ExpandVP_MERGE(SDNode *Node) {
1682 // Implement VP_MERGE in terms of VSELECT. Construct a mask where vector
1683 // indices less than the EVL/pivot are true. Combine that with the original
1684 // mask for a full-length mask. Use a full-length VSELECT to select between
1685 // the true and false values.
1686 SDLoc DL(Node);
1687
1688 SDValue Mask = Node->getOperand(0);
1689 SDValue Op1 = Node->getOperand(1);
1690 SDValue Op2 = Node->getOperand(2);
1691 SDValue EVL = Node->getOperand(3);
1692
1693 EVT MaskVT = Mask.getValueType();
1694 bool IsFixedLen = MaskVT.isFixedLengthVector();
1695
1696 EVT EVLVecVT = EVT::getVectorVT(*DAG.getContext(), EVL.getValueType(),
1697 MaskVT.getVectorElementCount());
1698
1699 // If we can't construct the EVL mask efficiently, it's better to unroll.
1700 if ((IsFixedLen &&
1702 (!IsFixedLen &&
1703 (!TLI.isOperationLegalOrCustom(ISD::STEP_VECTOR, EVLVecVT) ||
1705 return SDValue();
1706
1707 // If using a SETCC would result in a different type than the mask type,
1708 // unroll.
1709 if (TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
1710 EVLVecVT) != MaskVT)
1711 return SDValue();
1712
1713 SDValue StepVec = DAG.getStepVector(DL, EVLVecVT);
1714 SDValue SplatEVL = DAG.getSplat(EVLVecVT, DL, EVL);
1715 SDValue EVLMask =
1716 DAG.getSetCC(DL, MaskVT, StepVec, SplatEVL, ISD::CondCode::SETULT);
1717
1718 SDValue FullMask = DAG.getNode(ISD::AND, DL, MaskVT, Mask, EVLMask);
1719 return DAG.getSelect(DL, Node->getValueType(0), FullMask, Op1, Op2);
1720}
1721
1722SDValue VectorLegalizer::ExpandVP_REM(SDNode *Node) {
1723 // Implement VP_SREM/UREM in terms of VP_SDIV/VP_UDIV, MUL, SUB.
1724 EVT VT = Node->getValueType(0);
1725
1726 unsigned DivOpc = Node->getOpcode() == ISD::VP_SREM ? ISD::VP_SDIV : ISD::VP_UDIV;
1727
1728 if (!TLI.isOperationLegalOrCustom(DivOpc, VT) ||
1731 return SDValue();
1732
1733 SDLoc DL(Node);
1734
1735 SDValue Dividend = Node->getOperand(0);
1736 SDValue Divisor = Node->getOperand(1);
1737 SDValue Mask = Node->getOperand(2);
1738 SDValue EVL = Node->getOperand(3);
1739
1740 // X % Y -> X-X/Y*Y
1741 SDValue Div = DAG.getNode(DivOpc, DL, VT, Dividend, Divisor, Mask, EVL);
1742 SDValue Mul = DAG.getNode(ISD::MUL, DL, VT, Divisor, Div);
1743 return DAG.getNode(ISD::SUB, DL, VT, Dividend, Mul);
1744}
1745
1746SDValue VectorLegalizer::ExpandGET_ACTIVE_LANE_MASK(SDNode *N) {
1747 SDLoc DL(N);
1748
1749 SDValue Start = N->getOperand(0);
1750 SDValue End = N->getOperand(1);
1751 EVT VT = N->getValueType(0);
1752 EVT OpVT = Start.getValueType();
1753
1754 if (VT.isScalableVector())
1755 return SDValue();
1756
1757 // Try a promoted comparison type to simplify saturation.
1758 EVT PromoteVT = VT.changeVectorElementType(*DAG.getContext(), OpVT);
1759 if (TLI.isTypeLegal(PromoteVT) &&
1761 SDValue StartV = DAG.getSplat(PromoteVT, DL, Start);
1762 SDValue Seq = DAG.getStepVector(DL, PromoteVT);
1763 Seq = DAG.getNode(ISD::UADDSAT, DL, PromoteVT, Seq, StartV);
1764
1765 EVT MaskVT = TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
1766 PromoteVT);
1767 SDValue EndV = DAG.getSplat(PromoteVT, DL, End);
1768 SDValue Mask = DAG.getSetCC(DL, MaskVT, Seq, EndV, ISD::SETULT);
1769 return DAG.getBoolExtOrTrunc(Mask, DL, VT, PromoteVT);
1770 }
1771
1772 // Is VT's element type big enough to hold all rebased indices?
1774 return SDValue();
1775
1776 // Rebase and saturate the termination value.
1777 SDValue Max = DAG.getConstant(maxUIntN(VT.getScalarSizeInBits()), DL, OpVT);
1778 End = DAG.getNode(ISD::USUBSAT, DL, OpVT, End, Start);
1779 End = DAG.getNode(ISD::UMIN, DL, OpVT, End, Max);
1780
1781 // cmp <0, 1, 2, 3...>, End
1782 SDValue EndV = DAG.getSplat(VT, DL, End);
1783 SDValue StepVector = DAG.getStepVector(DL, VT);
1784 return DAG.getSetCC(DL, VT, StepVector, EndV, ISD::SETULT);
1785}
1786
1787SDValue VectorLegalizer::ExpandLOOP_DEPENDENCE_MASK(SDNode *N) {
1788 return TLI.expandLoopDependenceMask(N, DAG);
1789}
1790
1791SDValue VectorLegalizer::ExpandMaskedBinOp(SDNode *N) {
1792 // Masked bin ops don't have undefined behaviour when dividing by zero
1793 // on disabled lanes and produce poison instead. Replace the divisor on the
1794 // disabled lanes with 1 to avoid division by zero or overflow.
1795 SDLoc dl(N);
1796 EVT VT = N->getValueType(0);
1797 SDValue SafeDivisor = DAG.getSelect(
1798 dl, VT, N->getOperand(2), N->getOperand(1), DAG.getConstant(1, dl, VT));
1799 return DAG.getNode(ISD::getUnmaskedBinOpOpcode(N->getOpcode()), dl, VT,
1800 N->getOperand(0), SafeDivisor);
1801}
1802
1803void VectorLegalizer::ExpandFP_TO_UINT(SDNode *Node,
1804 SmallVectorImpl<SDValue> &Results) {
1805 // Attempt to expand using TargetLowering.
1806 SDValue Result, Chain;
1807 if (TLI.expandFP_TO_UINT(Node, Result, Chain, DAG)) {
1808 Results.push_back(Result);
1809 if (Node->isStrictFPOpcode())
1810 Results.push_back(Chain);
1811 return;
1812 }
1813
1814 // Otherwise go ahead and unroll.
1815 if (Node->isStrictFPOpcode()) {
1816 UnrollStrictFPOp(Node, Results);
1817 return;
1818 }
1819
1820 Results.push_back(DAG.UnrollVectorOp(Node));
1821}
1822
1823void VectorLegalizer::ExpandUINT_TO_FLOAT(SDNode *Node,
1824 SmallVectorImpl<SDValue> &Results) {
1825 bool IsStrict = Node->isStrictFPOpcode();
1826 unsigned OpNo = IsStrict ? 1 : 0;
1827 SDValue Src = Node->getOperand(OpNo);
1828 EVT SrcVT = Src.getValueType();
1829 EVT DstVT = Node->getValueType(0);
1830 SDLoc DL(Node);
1831
1832 // Attempt to expand using TargetLowering.
1833 SDValue Result;
1834 SDValue Chain;
1835 if (TLI.expandUINT_TO_FP(Node, Result, Chain, DAG)) {
1836 Results.push_back(Result);
1837 if (IsStrict)
1838 Results.push_back(Chain);
1839 return;
1840 }
1841
1842 // Make sure that the SINT_TO_FP and SRL instructions are available.
1843 if (((!IsStrict && TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) ==
1844 TargetLowering::Expand) ||
1845 (IsStrict && TLI.getOperationAction(ISD::STRICT_SINT_TO_FP, SrcVT) ==
1846 TargetLowering::Expand)) ||
1847 TLI.getOperationAction(ISD::SRL, SrcVT) == TargetLowering::Expand) {
1848 if (IsStrict) {
1849 UnrollStrictFPOp(Node, Results);
1850 return;
1851 }
1852
1853 Results.push_back(DAG.UnrollVectorOp(Node));
1854 return;
1855 }
1856
1857 unsigned BW = SrcVT.getScalarSizeInBits();
1858 assert((BW == 64 || BW == 32) &&
1859 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
1860
1861 // If STRICT_/FMUL is not supported by the target (in case of f16) replace the
1862 // UINT_TO_FP with a larger float and round to the smaller type
1863 if ((!IsStrict && !TLI.isOperationLegalOrCustom(ISD::FMUL, DstVT)) ||
1864 (IsStrict && !TLI.isOperationLegalOrCustom(ISD::STRICT_FMUL, DstVT))) {
1865 EVT FPVT = BW == 32 ? MVT::f32 : MVT::f64;
1866 SDValue UIToFP;
1867 SDValue Result;
1868 SDValue TargetZero = DAG.getIntPtrConstant(0, DL, /*isTarget=*/true);
1869 EVT FloatVecVT = SrcVT.changeVectorElementType(*DAG.getContext(), FPVT);
1870 if (IsStrict) {
1871 UIToFP = DAG.getNode(ISD::STRICT_UINT_TO_FP, DL, {FloatVecVT, MVT::Other},
1872 {Node->getOperand(0), Src});
1873 Result = DAG.getNode(ISD::STRICT_FP_ROUND, DL, {DstVT, MVT::Other},
1874 {Node->getOperand(0), UIToFP, TargetZero});
1875 Results.push_back(Result);
1876 Results.push_back(Result.getValue(1));
1877 } else {
1878 UIToFP = DAG.getNode(ISD::UINT_TO_FP, DL, FloatVecVT, Src);
1879 Result = DAG.getNode(ISD::FP_ROUND, DL, DstVT, UIToFP, TargetZero);
1880 Results.push_back(Result);
1881 }
1882
1883 return;
1884 }
1885
1886 SDValue HalfWord = DAG.getConstant(BW / 2, DL, SrcVT);
1887
1888 // Constants to clear the upper part of the word.
1889 // Notice that we can also use SHL+SHR, but using a constant is slightly
1890 // faster on x86.
1891 uint64_t HWMask = (BW == 64) ? 0x00000000FFFFFFFF : 0x0000FFFF;
1892 SDValue HalfWordMask = DAG.getConstant(HWMask, DL, SrcVT);
1893
1894 // Two to the power of half-word-size.
1895 SDValue TWOHW = DAG.getConstantFP(1ULL << (BW / 2), DL, DstVT);
1896
1897 // Clear upper part of LO, lower HI
1898 SDValue HI = DAG.getNode(ISD::SRL, DL, SrcVT, Src, HalfWord);
1899 SDValue LO = DAG.getNode(ISD::AND, DL, SrcVT, Src, HalfWordMask);
1900
1901 if (IsStrict) {
1902 // Convert hi and lo to floats
1903 // Convert the hi part back to the upper values
1904 // TODO: Can any fast-math-flags be set on these nodes?
1905 SDValue fHI = DAG.getNode(ISD::STRICT_SINT_TO_FP, DL, {DstVT, MVT::Other},
1906 {Node->getOperand(0), HI});
1907 fHI = DAG.getNode(ISD::STRICT_FMUL, DL, {DstVT, MVT::Other},
1908 {fHI.getValue(1), fHI, TWOHW});
1909 SDValue fLO = DAG.getNode(ISD::STRICT_SINT_TO_FP, DL, {DstVT, MVT::Other},
1910 {Node->getOperand(0), LO});
1911
1912 SDValue TF = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, fHI.getValue(1),
1913 fLO.getValue(1));
1914
1915 // Add the two halves
1916 SDValue Result =
1917 DAG.getNode(ISD::STRICT_FADD, DL, {DstVT, MVT::Other}, {TF, fHI, fLO});
1918
1919 Results.push_back(Result);
1920 Results.push_back(Result.getValue(1));
1921 return;
1922 }
1923
1924 // Convert hi and lo to floats
1925 // Convert the hi part back to the upper values
1926 // TODO: Can any fast-math-flags be set on these nodes?
1927 SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, DstVT, HI);
1928 fHI = DAG.getNode(ISD::FMUL, DL, DstVT, fHI, TWOHW);
1929 SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, DstVT, LO);
1930
1931 // Add the two halves
1932 Results.push_back(DAG.getNode(ISD::FADD, DL, DstVT, fHI, fLO));
1933}
1934
1935SDValue VectorLegalizer::ExpandFNEG(SDNode *Node) {
1936 EVT VT = Node->getValueType(0);
1937 EVT IntVT = VT.changeVectorElementTypeToInteger();
1938
1939 if (!TLI.isOperationLegalOrCustom(ISD::XOR, IntVT))
1940 return SDValue();
1941
1942 // Heuristic check to determine whether vector should be expanded to integer
1943 // operations or unrolled to scalar operations.
1944 // 1. Scalable vector is never unrolled.
1945 // 2. Fixed vector is unrolled if one of followings is true:
1946 // a. Vector only has 1 element and target knows how to handle scalar
1947 // FNEG (either legal or custom expand or promote).
1948 // b. Vector has more than 1 element and target supports scalar
1949 // FNEG natively and vector length <= 2(1 XOR + 1 CONST).
1950 // FIXME: Scalar construction instruction count varies in every architecture,
1951 // here we assume 1 instruction for now.
1952 if (VT.isFixedLengthVector()) {
1953 EVT EltVT = VT.getVectorElementType();
1954 unsigned NumElts = VT.getVectorNumElements();
1955 if ((NumElts == 1 &&
1957 (NumElts < 3 && TLI.isOperationLegal(ISD::FNEG, EltVT) &&
1958 TLI.isExtractVecEltCheap(VT, 0) &&
1959 (NumElts == 1 || TLI.isExtractVecEltCheap(VT, 1))))
1960 return SDValue();
1961 }
1962
1963 SDLoc DL(Node);
1964 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, IntVT, Node->getOperand(0));
1965 SDValue SignMask = DAG.getConstant(
1966 APInt::getSignMask(IntVT.getScalarSizeInBits()), DL, IntVT);
1967 SDValue Xor = DAG.getNode(ISD::XOR, DL, IntVT, Cast, SignMask);
1968 return DAG.getNode(ISD::BITCAST, DL, VT, Xor);
1969}
1970
1971SDValue VectorLegalizer::ExpandFABS(SDNode *Node) {
1972 EVT VT = Node->getValueType(0);
1973 EVT IntVT = VT.changeVectorElementTypeToInteger();
1974
1975 if (!TLI.isOperationLegalOrCustom(ISD::AND, IntVT))
1976 return SDValue();
1977
1978 // Heuristic check to determine whether vector should be expanded to integer
1979 // operations or unrolled to scalar operations.
1980 // 1. Scalable vector is never unrolled.
1981 // 2. Fixed vector is unrolled if one of followings is true:
1982 // a. Vector only has 1 element and target knows how to handle scalar
1983 // FABS(either legal or custom expand or promote).
1984 // b. Vector has more than 1 element and target supports scalar
1985 // FABS natively and vector length <= 2(1 AND + 1 CONST).
1986 // FIXME: Scalar construction instruction count varies in every architecture,
1987 // here we assume 1 instruction for now.
1988 if (VT.isFixedLengthVector()) {
1989 EVT EltVT = VT.getVectorElementType();
1990 unsigned NumElts = VT.getVectorNumElements();
1991 if ((NumElts == 1 &&
1993 (NumElts < 3 && TLI.isOperationLegal(ISD::FABS, EltVT) &&
1994 TLI.isExtractVecEltCheap(VT, 0) &&
1995 (NumElts == 1 || TLI.isExtractVecEltCheap(VT, 1))))
1996 return SDValue();
1997 }
1998
1999 SDLoc DL(Node);
2000 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, IntVT, Node->getOperand(0));
2001 SDValue ClearSignMask = DAG.getConstant(
2003 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, Cast, ClearSignMask);
2004 return DAG.getNode(ISD::BITCAST, DL, VT, ClearedSign);
2005}
2006
2007SDValue VectorLegalizer::ExpandFCOPYSIGN(SDNode *Node) {
2008 EVT VT = Node->getValueType(0);
2009 EVT IntVT = VT.changeVectorElementTypeToInteger();
2010
2011 if (VT != Node->getOperand(1).getValueType() ||
2012 !TLI.isOperationLegalOrCustom(ISD::AND, IntVT) ||
2013 !TLI.isOperationLegalOrCustom(ISD::OR, IntVT))
2014 return SDValue();
2015
2016 // Heuristic check to determine whether vector should be expanded to integer
2017 // operations or unrolled to scalar operations.
2018 // 1. Scalable vector is never unrolled.
2019 // 2. Fixed vector is unrolled if one of followings is true:
2020 // a. Vector only has 1 element and target knows how to handle scalar
2021 // FCOPYSIGN(either legal or custom expand or promote).
2022 // b. Vector has more than 1 element and target supports scalar
2023 // FCOPYSIGN natively and vector length <= 5(2 AND + 1 OR + 2 CONST).
2024 // FIXME: Scalar construction instruction count varies in every architecture,
2025 // here we assume 1 instruction for now.
2026 if (VT.isFixedLengthVector()) {
2027 EVT EltVT = VT.getVectorElementType();
2028 unsigned NumElts = VT.getVectorNumElements();
2029 if ((NumElts == 1 &&
2031 (NumElts < 6 && TLI.isOperationLegal(ISD::FCOPYSIGN, EltVT) &&
2032 TLI.isExtractVecEltCheap(VT, 0) &&
2033 (NumElts == 1 || TLI.isExtractVecEltCheap(VT, 1))))
2034 return SDValue();
2035 }
2036
2037 SDLoc DL(Node);
2038 SDValue Mag = DAG.getNode(ISD::BITCAST, DL, IntVT, Node->getOperand(0));
2039 SDValue Sign = DAG.getNode(ISD::BITCAST, DL, IntVT, Node->getOperand(1));
2040
2041 SDValue SignMask = DAG.getConstant(
2042 APInt::getSignMask(IntVT.getScalarSizeInBits()), DL, IntVT);
2043 SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, Sign, SignMask);
2044
2045 SDValue ClearSignMask = DAG.getConstant(
2047 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, Mag, ClearSignMask);
2048
2049 SDValue CopiedSign = DAG.getNode(ISD::OR, DL, IntVT, ClearedSign, SignBit,
2051
2052 return DAG.getNode(ISD::BITCAST, DL, VT, CopiedSign);
2053}
2054
2055void VectorLegalizer::ExpandFSUB(SDNode *Node,
2056 SmallVectorImpl<SDValue> &Results) {
2057 // For floating-point values, (a-b) is the same as a+(-b). If FNEG is legal,
2058 // we can defer this to operation legalization where it will be lowered as
2059 // a+(-b).
2060 EVT VT = Node->getValueType(0);
2061 if (TLI.isOperationLegalOrCustom(ISD::FNEG, VT) &&
2063 return; // Defer to LegalizeDAG
2064
2065 if (SDValue Expanded = TLI.expandVectorNaryOpBySplitting(Node, DAG)) {
2066 Results.push_back(Expanded);
2067 return;
2068 }
2069
2070 SDValue Tmp = DAG.UnrollVectorOp(Node);
2071 Results.push_back(Tmp);
2072}
2073
2074void VectorLegalizer::ExpandSETCC(SDNode *Node,
2075 SmallVectorImpl<SDValue> &Results) {
2076 bool NeedInvert = false;
2077 bool IsStrict = Node->getOpcode() == ISD::STRICT_FSETCC ||
2078 Node->getOpcode() == ISD::STRICT_FSETCCS;
2079 bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS;
2080 unsigned Offset = IsStrict ? 1 : 0;
2081
2082 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
2083 SDValue LHS = Node->getOperand(0 + Offset);
2084 SDValue RHS = Node->getOperand(1 + Offset);
2085 SDValue CC = Node->getOperand(2 + Offset);
2086
2087 MVT OpVT = LHS.getSimpleValueType();
2088 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
2089
2090 if (TLI.getCondCodeAction(CCCode, OpVT) != TargetLowering::Expand) {
2091 if (IsStrict) {
2092 UnrollStrictFPOp(Node, Results);
2093 return;
2094 }
2095 Results.push_back(UnrollVSETCC(Node));
2096 return;
2097 }
2098
2099 SDLoc dl(Node);
2100 bool Legalized =
2101 TLI.LegalizeSetCCCondCode(DAG, Node->getValueType(0), LHS, RHS, CC,
2102 NeedInvert, dl, Chain, IsSignaling);
2103
2104 if (Legalized) {
2105 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
2106 // condition code, create a new SETCC node.
2107 if (CC.getNode()) {
2108 if (IsStrict) {
2109 LHS = DAG.getNode(Node->getOpcode(), dl, Node->getVTList(),
2110 {Chain, LHS, RHS, CC}, Node->getFlags());
2111 Chain = LHS.getValue(1);
2112 } else {
2113 LHS = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), LHS, RHS, CC,
2114 Node->getFlags());
2115 }
2116 }
2117
2118 // If we expanded the SETCC by inverting the condition code, then wrap
2119 // the existing SETCC in a NOT to restore the intended condition.
2120 if (NeedInvert)
2121 LHS = DAG.getLogicalNOT(dl, LHS, LHS->getValueType(0));
2122 } else {
2123 assert(!IsStrict && "Don't know how to expand for strict nodes.");
2124
2125 // Otherwise, SETCC for the given comparison type must be completely
2126 // illegal; expand it into a SELECT_CC.
2127 EVT VT = Node->getValueType(0);
2128 LHS = DAG.getNode(ISD::SELECT_CC, dl, VT, LHS, RHS,
2129 DAG.getBoolConstant(true, dl, VT, LHS.getValueType()),
2130 DAG.getBoolConstant(false, dl, VT, LHS.getValueType()),
2131 CC, Node->getFlags());
2132 }
2133
2134 Results.push_back(LHS);
2135 if (IsStrict)
2136 Results.push_back(Chain);
2137}
2138
2139void VectorLegalizer::ExpandUADDSUBO(SDNode *Node,
2140 SmallVectorImpl<SDValue> &Results) {
2141 SDValue Result, Overflow;
2142 TLI.expandUADDSUBO(Node, Result, Overflow, DAG);
2143 Results.push_back(Result);
2144 Results.push_back(Overflow);
2145}
2146
2147void VectorLegalizer::ExpandSADDSUBO(SDNode *Node,
2148 SmallVectorImpl<SDValue> &Results) {
2149 SDValue Result, Overflow;
2150 TLI.expandSADDSUBO(Node, Result, Overflow, DAG);
2151 Results.push_back(Result);
2152 Results.push_back(Overflow);
2153}
2154
2155void VectorLegalizer::ExpandMULO(SDNode *Node,
2156 SmallVectorImpl<SDValue> &Results) {
2157 SDValue Result, Overflow;
2158 if (!TLI.expandMULO(Node, Result, Overflow, DAG))
2159 std::tie(Result, Overflow) = DAG.UnrollVectorOverflowOp(Node);
2160
2161 Results.push_back(Result);
2162 Results.push_back(Overflow);
2163}
2164
2165void VectorLegalizer::ExpandFixedPointDiv(SDNode *Node,
2166 SmallVectorImpl<SDValue> &Results) {
2167 SDNode *N = Node;
2168 if (SDValue Expanded = TLI.expandFixedPointDiv(N->getOpcode(), SDLoc(N),
2169 N->getOperand(0), N->getOperand(1), N->getConstantOperandVal(2), DAG))
2170 Results.push_back(Expanded);
2171}
2172
2173void VectorLegalizer::ExpandStrictFPOp(SDNode *Node,
2174 SmallVectorImpl<SDValue> &Results) {
2175 if (Node->getOpcode() == ISD::STRICT_UINT_TO_FP) {
2176 ExpandUINT_TO_FLOAT(Node, Results);
2177 return;
2178 }
2179 if (Node->getOpcode() == ISD::STRICT_FP_TO_UINT) {
2180 ExpandFP_TO_UINT(Node, Results);
2181 return;
2182 }
2183
2184 if (Node->getOpcode() == ISD::STRICT_FSETCC ||
2185 Node->getOpcode() == ISD::STRICT_FSETCCS) {
2186 ExpandSETCC(Node, Results);
2187 return;
2188 }
2189
2190 UnrollStrictFPOp(Node, Results);
2191}
2192
2193void VectorLegalizer::ExpandREM(SDNode *Node,
2194 SmallVectorImpl<SDValue> &Results) {
2195 assert((Node->getOpcode() == ISD::SREM || Node->getOpcode() == ISD::UREM) &&
2196 "Expected REM node");
2197
2198 SDValue Result;
2199 if (!TLI.expandREM(Node, Result, DAG))
2200 Result = DAG.UnrollVectorOp(Node);
2201 Results.push_back(Result);
2202}
2203
2204// Try to expand libm nodes into vector math routine calls. Callers provide the
2205// RTLIB::get<OP>(EVT) selector of the node's libcall family, which is used to
2206// look up mappings within RuntimeLibcallsInfo. The only mappings considered are
2207// those where the result and all operands are the same vector type. While
2208// predicated nodes are not supported, we will emit calls to masked routines by
2209// passing in a mask that is true for the lanes computed by the node.
2210bool VectorLegalizer::tryExpandVecMathCall(
2211 SDNode *Node, function_ref<RTLIB::Libcall(EVT)> GetLibcall,
2212 SmallVectorImpl<SDValue> &Results) {
2213 // Chain must be propagated but currently strict fp operations are down
2214 // converted to their none strict counterpart.
2215 assert(!Node->isStrictFPOpcode() && "Unexpected strict fp operation!");
2216
2217 EVT VT = Node->getValueType(0);
2218 LLVMContext &Ctx = *DAG.getContext();
2219 const LibcallLoweringInfo &Libcalls = DAG.getLibcalls();
2220
2221 // Try to widen the vector type when no libcall is available at that width.
2222 EVT CallVT = VT;
2223 RTLIB::LibcallImpl LCImpl = Libcalls.getLibcallImpl(GetLibcall(CallVT));
2224 if (LCImpl == RTLIB::Unsupported && VT.getVectorElementCount().isScalar())
2225 return false;
2226 while (LCImpl == RTLIB::Unsupported) {
2227 CallVT = CallVT.getDoubleNumVectorElementsVT(Ctx);
2228 if (!CallVT.isSimple())
2229 return false;
2230 if (TLI.isTypeLegal(CallVT))
2231 LCImpl = Libcalls.getLibcallImpl(GetLibcall(CallVT));
2232 }
2233
2234 const RTLIB::RuntimeLibcallsInfo &RTLCI = TLI.getRuntimeLibcallsInfo();
2235
2236 auto [FuncTy, FuncAttrs] = RTLCI.getFunctionTy(
2237 Ctx, DAG.getSubtarget().getTargetTriple(), DAG.getDataLayout(), LCImpl);
2238
2239 SDLoc DL(Node);
2240 TargetLowering::ArgListTy Args;
2241
2242 bool HasMaskArg = RTLCI.hasVectorMaskArgument(LCImpl);
2243
2244 // Sanity check just in case function has unexpected parameters.
2245 assert(FuncTy->getNumParams() == Node->getNumOperands() + HasMaskArg &&
2246 EVT::getEVT(FuncTy->getReturnType(), true) == CallVT &&
2247 "mismatch in value type and call signature type");
2248
2249 for (unsigned I = 0, E = FuncTy->getNumParams(); I != E; ++I) {
2250 Type *ParamTy = FuncTy->getParamType(I);
2251
2252 if (HasMaskArg && I == E - 1) {
2253 assert(cast<VectorType>(ParamTy)->getElementType()->isIntegerTy(1) &&
2254 cast<VectorType>(ParamTy)->getElementCount() ==
2255 CallVT.getVectorElementCount() &&
2256 "unexpected vector mask type");
2257 EVT MaskVT = EVT::getEVT(ParamTy, /*HandleUnknown=*/true);
2258 EVT SubMaskVT =
2260 SDValue Mask = DAG.getBoolConstant(true, DL, SubMaskVT, VT);
2261 // Only the lanes holding the node's elements need to be active.
2262 if (CallVT != VT)
2264 DL, DAG.getBoolConstant(false, DL, MaskVT, CallVT), Mask, 0);
2265 Args.emplace_back(Mask, ParamTy);
2266 } else {
2267 SDValue Op = Node->getOperand(I);
2268 assert(Op.getValueType() == VT && "mismatch in vector types");
2269 if (CallVT != VT) {
2270 unsigned NumConcat =
2272 SmallVector<SDValue, 4> Ops(NumConcat, Op);
2273 Op = DAG.getNode(ISD::CONCAT_VECTORS, DL, CallVT, Ops);
2274 }
2275 assert(Op.getValueType() == EVT::getEVT(ParamTy, true) &&
2276 "mismatch in value type and call argument type");
2277 Args.emplace_back(Op, ParamTy);
2278 }
2279 }
2280
2281 // Emit a call to the vector function.
2282 SDValue Callee =
2283 DAG.getExternalSymbol(LCImpl, TLI.getPointerTy(DAG.getDataLayout()));
2284 CallingConv::ID CC = RTLCI.getLibcallImplCallingConv(LCImpl);
2285
2286 TargetLowering::CallLoweringInfo CLI(DAG);
2287 CLI.setDebugLoc(DL)
2288 .setChain(DAG.getEntryNode())
2289 .setLibCallee(CC, FuncTy->getReturnType(), Callee, std::move(Args));
2290
2291 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
2292 SDValue Result = CallResult.first;
2293 if (CallVT != VT)
2294 Result = DAG.getExtractSubvector(DL, VT, Result, 0);
2295 Results.push_back(Result);
2296 return true;
2297}
2298
2299void VectorLegalizer::UnrollStrictFPOp(SDNode *Node,
2300 SmallVectorImpl<SDValue> &Results) {
2301 EVT VT = Node->getValueType(0);
2302 EVT EltVT = VT.getVectorElementType();
2303 unsigned NumElems = VT.getVectorNumElements();
2304 unsigned NumOpers = Node->getNumOperands();
2305 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2306
2307 EVT TmpEltVT = EltVT;
2308 if (Node->getOpcode() == ISD::STRICT_FSETCC ||
2309 Node->getOpcode() == ISD::STRICT_FSETCCS)
2310 TmpEltVT = TLI.getSetCCResultType(DAG.getDataLayout(),
2311 *DAG.getContext(), TmpEltVT);
2312
2313 EVT ValueVTs[] = {TmpEltVT, MVT::Other};
2314 SDValue Chain = Node->getOperand(0);
2315 SDLoc dl(Node);
2316
2317 SmallVector<SDValue, 32> OpValues;
2318 SmallVector<SDValue, 32> OpChains;
2319 for (unsigned i = 0; i < NumElems; ++i) {
2321 SDValue Idx = DAG.getVectorIdxConstant(i, dl);
2322
2323 // The Chain is the first operand.
2324 Opers.push_back(Chain);
2325
2326 // Now process the remaining operands.
2327 for (unsigned j = 1; j < NumOpers; ++j) {
2328 SDValue Oper = Node->getOperand(j);
2329 EVT OperVT = Oper.getValueType();
2330
2331 if (OperVT.isVector())
2332 Oper = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
2333 OperVT.getVectorElementType(), Oper, Idx);
2334
2335 Opers.push_back(Oper);
2336 }
2337
2338 SDValue ScalarOp = DAG.getNode(Node->getOpcode(), dl, ValueVTs, Opers);
2339 SDValue ScalarResult = ScalarOp.getValue(0);
2340 SDValue ScalarChain = ScalarOp.getValue(1);
2341
2342 if (Node->getOpcode() == ISD::STRICT_FSETCC ||
2343 Node->getOpcode() == ISD::STRICT_FSETCCS)
2344 ScalarResult = DAG.getSelect(dl, EltVT, ScalarResult,
2345 DAG.getAllOnesConstant(dl, EltVT),
2346 DAG.getConstant(0, dl, EltVT));
2347
2348 OpValues.push_back(ScalarResult);
2349 OpChains.push_back(ScalarChain);
2350 }
2351
2352 SDValue Result = DAG.getBuildVector(VT, dl, OpValues);
2353 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OpChains);
2354
2355 Results.push_back(Result);
2356 Results.push_back(NewChain);
2357}
2358
2359SDValue VectorLegalizer::UnrollVSETCC(SDNode *Node) {
2360 EVT VT = Node->getValueType(0);
2361 unsigned NumElems = VT.getVectorNumElements();
2362 EVT EltVT = VT.getVectorElementType();
2363 SDValue LHS = Node->getOperand(0);
2364 SDValue RHS = Node->getOperand(1);
2365 SDValue CC = Node->getOperand(2);
2366 EVT TmpEltVT = LHS.getValueType().getVectorElementType();
2367 SDLoc dl(Node);
2368 SmallVector<SDValue, 8> Ops(NumElems);
2369 for (unsigned i = 0; i < NumElems; ++i) {
2370 SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
2371 DAG.getVectorIdxConstant(i, dl));
2372 SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
2373 DAG.getVectorIdxConstant(i, dl));
2374 // FIXME: We should use i1 setcc + boolext here, but it causes regressions.
2375 Ops[i] = DAG.getNode(ISD::SETCC, dl,
2377 *DAG.getContext(), TmpEltVT),
2378 LHSElem, RHSElem, CC);
2379 Ops[i] = DAG.getSelect(dl, EltVT, Ops[i],
2380 DAG.getBoolConstant(true, dl, EltVT, VT),
2381 DAG.getConstant(0, dl, EltVT));
2382 }
2383 return DAG.getBuildVector(VT, dl, Ops);
2384}
2385
2387 return VectorLegalizer(*this).Run();
2388}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static void createBSWAPShuffleMask(EVT VT, SmallVectorImpl< int > &ShuffleMask)
#define I(x, y, z)
Definition MD5.cpp:57
#define T
SI Fold Operands
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
This file describes how to lower LLVM code to machine code.
Value * RHS
Value * LHS
BinaryOperator * Mul
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:225
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:205
bool isBigEndian() const
Definition DataLayout.h:218
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:319
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:316
size_t size() const
Definition Function.h:843
RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const
Return the lowering's selection of implementation call for Call.
const Triple & getTargetTriple() const
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
MVT getVectorElementType() const
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
MVT getScalarType() const
If this is a vector, return the element type, otherwise return this.
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
Represents one node in the SelectionDAG.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
const TargetSubtargetInfo & getSubtarget() const
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI bool LegalizeVectors()
This transforms the SelectionDAG into a SelectionDAG that only uses vector math operations supported ...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
SDValue getInsertSubvector(const SDLoc &DL, SDValue Vec, SDValue SubVec, unsigned Idx)
Insert SubVec at the Idx element of Vec.
LLVM_ABI SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false, SDNodeFlags Flags={})
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
const TargetLowering & getTargetLoweringInfo() const
LLVM_ABI std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
allnodes_const_iterator allnodes_end() const
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
const DataLayout & getDataLayout() const
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
LLVM_ABI SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const LibcallLoweringInfo & getLibcalls() const
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
LLVM_ABI SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
LLVMContext * getContext() const
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
ilist< SDNode >::iterator allnodes_iterator
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void resize(size_type N)
void push_back(const T &Elt)
virtual bool isShuffleMaskLegal(ArrayRef< int >, EVT) const
Targets can use this to indicate that they only support some VECTOR_SHUFFLE operations,...
SDValue promoteTargetBoolean(SelectionDAG &DAG, SDValue Bool, EVT ValVT) const
Promote the given target boolean to a target boolean of the given type.
LegalizeAction getCondCodeAction(ISD::CondCode CC, MVT VT) const
Return how the condition code should be treated: either it is legal, needs to be expanded to some oth...
LegalizeAction getTruncStoreAction(EVT ValVT, EVT MemVT, Align Alignment, unsigned AddrSpace) const
Return how this store with truncation should be treated: either it is legal, needs to be promoted to ...
virtual bool isExtractVecEltCheap(EVT VT, unsigned Index) const
Return true if extraction of a scalar element from the given vector type at the given index is cheap.
LegalizeAction getFixedPointOperationAction(unsigned Op, EVT VT, unsigned Scale) const
Some fixed point operations may be natively supported by the target but only for specific scales.
bool isStrictFPEnabled() const
Return true if the target support strict float operation.
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
bool isOperationLegalOrCustom(unsigned Op, EVT VT, bool LegalOnly=false) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
LegalizeAction getPartialReduceMLAAction(unsigned Opc, EVT AccVT, EVT InputVT) const
Return how a PARTIAL_REDUCE_U/SMLA node with Acc type AccVT and Input type InputVT should be treated.
LegalizeAction getLoadAction(EVT ValVT, EVT MemVT, Align Alignment, unsigned AddrSpace, unsigned ExtType, bool Atomic) const
Return how this load with extension should be treated: either it is legal, needs to be promoted to a ...
LegalizeAction getStrictFPOperationAction(unsigned Op, EVT VT) const
LegalizeAction getOperationAction(unsigned Op, EVT VT) const
Return how this operation should be treated: either it is legal, needs to be promoted to a larger siz...
MVT getTypeToPromoteTo(unsigned Op, MVT VT) const
If the action for this operation is to promote, this method returns the ValueType to promote to.
bool isOperationLegalOrCustomOrPromote(unsigned Op, EVT VT, bool LegalOnly=false) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
const RTLIB::RuntimeLibcallsInfo & getRuntimeLibcallsInfo() const
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
SDValue expandAddSubSat(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US][ADD|SUB]SAT.
bool expandMultipleResultFPLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, SDNode *Node, SmallVectorImpl< SDValue > &Results, std::optional< unsigned > CallRetResNo={}) const
Expands a node with multiple results to an FP or vector libcall.
bool expandMULO(SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US]MULO.
bool LegalizeSetCCCondCode(SelectionDAG &DAG, EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC, bool &NeedInvert, const SDLoc &dl, SDValue &Chain, bool IsSignaling=false) const
Legalize a SETCC with given LHS and RHS and condition code CC on the current target.
SDValue scalarizeVectorStore(StoreSDNode *ST, SelectionDAG &DAG) const
SDValue expandVecReduceSeq(SDNode *Node, SelectionDAG &DAG) const
Expand a VECREDUCE_SEQ_* into an explicit ordered calculation.
SDValue expandFCANONICALIZE(SDNode *Node, SelectionDAG &DAG) const
Expand FCANONICALIZE to FMUL with 1.
SDValue expandCTLZ(SDNode *N, SelectionDAG &DAG) const
Expand CTLZ/CTLZ_ZERO_POISON nodes.
SDValue expandBITREVERSE(SDNode *N, SelectionDAG &DAG) const
Expand BITREVERSE nodes.
SDValue expandCTTZ(SDNode *N, SelectionDAG &DAG) const
Expand CTTZ/CTTZ_ZERO_POISON nodes.
SDValue expandABD(SDNode *N, SelectionDAG &DAG) const
Expand ABDS/ABDU nodes.
SDValue expandCLMUL(SDNode *N, SelectionDAG &DAG) const
Expand carryless multiply.
SDValue expandShlSat(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US]SHLSAT.
SDValue expandFP_TO_INT_SAT(SDNode *N, SelectionDAG &DAG) const
Expand FP_TO_[US]INT_SAT into FP_TO_[US]INT and selects or min/max.
SDValue expandCttzElts(SDNode *Node, SelectionDAG &DAG) const
Expand a CTTZ_ELTS or CTTZ_ELTS_ZERO_POISON by calculating (VL - i) for each active lane (i),...
void expandSADDSUBO(SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::S(ADD|SUB)O.
SDValue expandABS(SDNode *N, SelectionDAG &DAG, bool IsNegative=false) const
Expand ABS nodes.
SDValue expandVecReduce(SDNode *Node, SelectionDAG &DAG) const
Expand a VECREDUCE_* into an explicit calculation.
SDValue expandMULH(SDNode *Node, SelectionDAG &DAG) const
bool expandFP_TO_UINT(SDNode *N, SDValue &Result, SDValue &Chain, SelectionDAG &DAG) const
Expand float to UINT conversion.
bool expandREM(SDNode *Node, SDValue &Result, SelectionDAG &DAG) const
Expand an SREM or UREM using SDIV/UDIV or SDIVREM/UDIVREM, if legal.
SDValue expandFMINIMUMNUM_FMAXIMUMNUM(SDNode *N, SelectionDAG &DAG) const
Expand fminimumnum/fmaximumnum into multiple comparison with selects.
SDValue expandLoopDependenceMask(SDNode *N, SelectionDAG &DAG) const
Expand LOOP_DEPENDENCE_MASK nodes.
SDValue expandCTPOP(SDNode *N, SelectionDAG &DAG) const
Expand CTPOP nodes.
SDValue expandVectorNaryOpBySplitting(SDNode *Node, SelectionDAG &DAG) const
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
SDValue expandBSWAP(SDNode *N, SelectionDAG &DAG) const
Expand BSWAP nodes.
SDValue expandFMINIMUM_FMAXIMUM(SDNode *N, SelectionDAG &DAG) const
Expand fminimum/fmaximum into multiple comparison with selects.
std::pair< SDValue, SDValue > scalarizeVectorLoad(LoadSDNode *LD, SelectionDAG &DAG) const
Turn load of vector type into a load of the individual elements.
SDValue expandVectorMatch(SDNode *N, SelectionDAG &DAG) const
Expand VECTOR_MATCH nodes.
SDValue expandCONVERT_TO_ARBITRARY_FP(SDNode *Node, SelectionDAG &DAG) const
Expand CONVERT_TO_ARBITRARY_FP using bit manipulation.
SDValue expandFunnelShift(SDNode *N, SelectionDAG &DAG) const
Expand funnel shift.
virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const
This callback is invoked for operations that are unsupported by the target, which are registered to u...
SDValue expandFixedPointDiv(unsigned Opcode, const SDLoc &dl, SDValue LHS, SDValue RHS, unsigned Scale, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US]DIVFIX[SAT].
SDValue expandPEXT(SDNode *N, SelectionDAG &DAG) const
Expand parallel bit extract (compress).
SDValue expandVECTOR_COMPRESS(SDNode *Node, SelectionDAG &DAG) const
Expand a vector VECTOR_COMPRESS into a sequence of extract element, store temporarily,...
SDValue expandCONVERT_FROM_ARBITRARY_FP(SDNode *Node, SelectionDAG &DAG) const
Expand CONVERT_FROM_ARBITRARY_FP using bit manipulation.
SDValue expandROT(SDNode *N, bool AllowVectorOps, SelectionDAG &DAG) const
Expand rotations.
SDValue expandFMINNUM_FMAXNUM(SDNode *N, SelectionDAG &DAG) const
Expand fminnum/fmaxnum into fminnum_ieee/fmaxnum_ieee with quieted inputs.
SDValue expandCMP(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US]CMP.
SDValue expandFixedPointMul(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[U|S]MULFIX[SAT].
SDValue expandIntMINMAX(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US][MIN|MAX].
SDValue expandVectorFindLastActive(SDNode *N, SelectionDAG &DAG) const
Expand VECTOR_FIND_LAST_ACTIVE nodes.
SDValue expandPartialReduceMLA(SDNode *Node, SelectionDAG &DAG) const
Expands PARTIAL_REDUCE_S/UMLA nodes to a series of simpler operations, consisting of zext/sext,...
void expandUADDSUBO(SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::U(ADD|SUB)O.
SDValue expandPDEP(SDNode *N, SelectionDAG &DAG) const
Expand parallel bit deposit (expand).
bool expandUINT_TO_FP(SDNode *N, SDValue &Result, SDValue &Chain, SelectionDAG &DAG) const
Expand UINT(i64) to double(f64) conversion.
SDValue expandAVG(SDNode *N, SelectionDAG &DAG) const
Expand vector/scalar AVGCEILS/AVGCEILU/AVGFLOORS/AVGFLOORU nodes.
An efficient, type-erasing, non-owning reference to a callable.
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:835
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:514
@ PARTIAL_REDUCE_SMLA
PARTIAL_REDUCE_[U|S]MLA(Accumulator, Input1, Input2) The partial reduction nodes sign or zero extend ...
@ LOOP_DEPENDENCE_RAW_MASK
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ VECREDUCE_FMINIMUMNUM
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:275
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:795
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition ISDOpcodes.h:395
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:401
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:869
@ CTTZ_ELTS
Returns the number of number of trailing (least significant) zero elements in a vector.
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:521
@ VECTOR_FIND_LAST_ACTIVE
Finds the index of the last active mask element Operands: Mask.
@ FMODF
FMODF - Decomposes the operand into integral and fractional parts, each having the same type and sign...
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ FSINCOSPI
FSINCOSPI - Compute both the sine and cosine times pi more accurately than FSINCOS(pi*x),...
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:896
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:587
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:418
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:755
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:926
@ SDIVREM
SDIVREM/UDIVREM - Divide two integers and produce both a quotient and remainder result.
Definition ISDOpcodes.h:280
@ FPTRUNC_ROUND
FPTRUNC_ROUND - This corresponds to the fptrunc_round intrinsic.
Definition ISDOpcodes.h:518
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:786
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:408
@ STRICT_FSQRT
Constrained versions of libm-equivalent floating point intrinsics.
Definition ISDOpcodes.h:439
@ CONVERT_FROM_ARBITRARY_FP
CONVERT_FROM_ARBITRARY_FP - This operator converts from an arbitrary floating-point represented as an...
@ CTLZ_ZERO_POISON
Definition ISDOpcodes.h:804
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:860
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:723
@ STRICT_UINT_TO_FP
Definition ISDOpcodes.h:488
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
@ PARTIAL_REDUCE_FMLA
@ VECREDUCE_FMAXIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM nodes do not propagate NaNs and order signed zeroes using the llvm....
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:353
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:699
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:544
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:375
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:812
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:680
@ GET_ACTIVE_LANE_MASK
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:349
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:712
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:777
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:579
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:866
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:827
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:387
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:357
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:915
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:904
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:735
@ MASKED_UDIV
Masked vector arithmetic that returns poison on disabled lanes.
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:414
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:994
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:821
@ STRICT_SINT_TO_FP
STRICT_[US]INT_TO_FP - Convert a signed or unsigned integer to a floating point value.
Definition ISDOpcodes.h:487
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
@ STRICT_FP_TO_UINT
Definition ISDOpcodes.h:481
@ PEXT
Parallel bit extract (compress) and parallel bit deposit (expand).
Definition ISDOpcodes.h:791
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:503
@ STRICT_FP_TO_SINT
STRICT_FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:480
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:942
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:508
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:747
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:743
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:718
@ VECTOR_MATCH
VECTOR_MATCH - this corresponds to the llvm.experimental.vector.match intrinsic.
@ STRICT_FADD
Constrained versions of the binary floating point operators.
Definition ISDOpcodes.h:428
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ CTTZ_ZERO_POISON
Bit counting operators with a poisoned result for zero inputs.
Definition ISDOpcodes.h:803
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:975
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:707
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:937
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:961
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:872
@ VECREDUCE_SEQ_FMUL
@ CONVERT_TO_ARBITRARY_FP
CONVERT_TO_ARBITRARY_FP - Converts a native FP value to an arbitrary floating-point format,...
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:537
@ PARTIAL_REDUCE_SUMLA
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:366
@ CTTZ_ELTS_ZERO_POISON
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:730
@ ABS_MIN_POISON
ABS with a poison result for INT_MIN.
Definition ISDOpcodes.h:759
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:559
@ LOOP_DEPENDENCE_WAR_MASK
The llvm.loop.dependence.
LLVM_ABI NodeType getUnmaskedBinOpOpcode(unsigned MaskedOpc)
Given a MaskedOpc of ISD::MASKED_(U|S)(DIV|REM), returns the unmasked ISD::(U|S)(DIV|REM).
LLVM_ABI std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
LLVM_ABI bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
constexpr uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
Definition MathExtras.h:208
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
@ Xor
Bitwise or logical XOR of integers.
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341
#define N
Extended Value Type.
Definition ValueTypes.h:35
EVT changeVectorElementTypeToInteger() const
Return a vector with the same number of elements as this vector, but with the element type converted ...
Definition ValueTypes.h:90
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:145
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:70
ElementCount getVectorElementCount() const
Definition ValueTypes.h:373
EVT getDoubleNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:494
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:396
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:382
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:408
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
EVT changeVectorElementType(LLVMContext &Context, EVT EltVT) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element type...
Definition ValueTypes.h:98
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:339
EVT changeVectorElementCount(LLVMContext &Context, ElementCount EC) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element coun...
Definition ValueTypes.h:109
bool isFixedLengthVector() const
Definition ValueTypes.h:199
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:346
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:187
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:351
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:359
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:331
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:160
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
LLVM_ABI std::pair< FunctionType *, AttributeList > getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL, RTLIB::LibcallImpl LibcallImpl) const
static LLVM_ABI bool hasVectorMaskArgument(RTLIB::LibcallImpl Impl)
Returns true if the function has a vector mask argument, which is assumed to be the last argument.