LLVM 20.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"
39#include "llvm/IR/DataLayout.h"
42#include "llvm/Support/Debug.h"
44#include <cassert>
45#include <cstdint>
46#include <iterator>
47#include <utility>
48
49using namespace llvm;
50
51#define DEBUG_TYPE "legalizevectorops"
52
53namespace {
54
55class VectorLegalizer {
56 SelectionDAG& DAG;
57 const TargetLowering &TLI;
58 bool Changed = false; // Keep track of whether anything changed
59
60 /// For nodes that are of legal width, and that have more than one use, this
61 /// map indicates what regularized operand to use. This allows us to avoid
62 /// legalizing the same thing more than once.
64
65 /// Adds a node to the translation cache.
66 void AddLegalizedOperand(SDValue From, SDValue To) {
67 LegalizedNodes.insert(std::make_pair(From, To));
68 // If someone requests legalization of the new node, return itself.
69 if (From != To)
70 LegalizedNodes.insert(std::make_pair(To, To));
71 }
72
73 /// Legalizes the given node.
74 SDValue LegalizeOp(SDValue Op);
75
76 /// Assuming the node is legal, "legalize" the results.
77 SDValue TranslateLegalizeResults(SDValue Op, SDNode *Result);
78
79 /// Make sure Results are legal and update the translation cache.
80 SDValue RecursivelyLegalizeResults(SDValue Op,
82
83 /// Wrapper to interface LowerOperation with a vector of Results.
84 /// Returns false if the target wants to use default expansion. Otherwise
85 /// returns true. If return is true and the Results are empty, then the
86 /// target wants to keep the input node as is.
87 bool LowerOperationWrapper(SDNode *N, SmallVectorImpl<SDValue> &Results);
88
89 /// Implements unrolling a VSETCC.
90 SDValue UnrollVSETCC(SDNode *Node);
91
92 /// Implement expand-based legalization of vector operations.
93 ///
94 /// This is just a high-level routine to dispatch to specific code paths for
95 /// operations to legalize them.
97
98 /// Implements expansion for FP_TO_UINT; falls back to UnrollVectorOp if
99 /// FP_TO_SINT isn't legal.
100 void ExpandFP_TO_UINT(SDNode *Node, SmallVectorImpl<SDValue> &Results);
101
102 /// Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if
103 /// SINT_TO_FLOAT and SHR on vectors isn't legal.
104 void ExpandUINT_TO_FLOAT(SDNode *Node, SmallVectorImpl<SDValue> &Results);
105
106 /// Implement expansion for SIGN_EXTEND_INREG using SRL and SRA.
107 SDValue ExpandSEXTINREG(SDNode *Node);
108
109 /// Implement expansion for ANY_EXTEND_VECTOR_INREG.
110 ///
111 /// Shuffles the low lanes of the operand into place and bitcasts to the proper
112 /// type. The contents of the bits in the extended part of each element are
113 /// undef.
114 SDValue ExpandANY_EXTEND_VECTOR_INREG(SDNode *Node);
115
116 /// Implement expansion for SIGN_EXTEND_VECTOR_INREG.
117 ///
118 /// Shuffles the low lanes of the operand into place, bitcasts to the proper
119 /// type, then shifts left and arithmetic shifts right to introduce a sign
120 /// extension.
121 SDValue ExpandSIGN_EXTEND_VECTOR_INREG(SDNode *Node);
122
123 /// Implement expansion for ZERO_EXTEND_VECTOR_INREG.
124 ///
125 /// Shuffles the low lanes of the operand into place and blends zeros into
126 /// the remaining lanes, finally bitcasting to the proper type.
127 SDValue ExpandZERO_EXTEND_VECTOR_INREG(SDNode *Node);
128
129 /// Expand bswap of vectors into a shuffle if legal.
130 SDValue ExpandBSWAP(SDNode *Node);
131
132 /// Implement vselect in terms of XOR, AND, OR when blend is not
133 /// supported by the target.
134 SDValue ExpandVSELECT(SDNode *Node);
135 SDValue ExpandVP_SELECT(SDNode *Node);
136 SDValue ExpandVP_MERGE(SDNode *Node);
137 SDValue ExpandVP_REM(SDNode *Node);
138 SDValue ExpandSELECT(SDNode *Node);
139 std::pair<SDValue, SDValue> ExpandLoad(SDNode *N);
140 SDValue ExpandStore(SDNode *N);
141 SDValue ExpandFNEG(SDNode *Node);
142 void ExpandFSUB(SDNode *Node, SmallVectorImpl<SDValue> &Results);
143 void ExpandSETCC(SDNode *Node, SmallVectorImpl<SDValue> &Results);
144 void ExpandBITREVERSE(SDNode *Node, SmallVectorImpl<SDValue> &Results);
145 void ExpandUADDSUBO(SDNode *Node, SmallVectorImpl<SDValue> &Results);
146 void ExpandSADDSUBO(SDNode *Node, SmallVectorImpl<SDValue> &Results);
147 void ExpandMULO(SDNode *Node, SmallVectorImpl<SDValue> &Results);
148 void ExpandFixedPointDiv(SDNode *Node, SmallVectorImpl<SDValue> &Results);
149 void ExpandStrictFPOp(SDNode *Node, SmallVectorImpl<SDValue> &Results);
150 void ExpandREM(SDNode *Node, SmallVectorImpl<SDValue> &Results);
151
152 bool tryExpandVecMathCall(SDNode *Node, RTLIB::Libcall LC,
154 bool tryExpandVecMathCall(SDNode *Node, RTLIB::Libcall Call_F32,
155 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
156 RTLIB::Libcall Call_F128,
157 RTLIB::Libcall Call_PPCF128,
159
160 void UnrollStrictFPOp(SDNode *Node, SmallVectorImpl<SDValue> &Results);
161
162 /// Implements vector promotion.
163 ///
164 /// This is essentially just bitcasting the operands to a different type and
165 /// bitcasting the result back to the original type.
167
168 /// Implements [SU]INT_TO_FP vector promotion.
169 ///
170 /// This is a [zs]ext of the input operand to a larger integer type.
171 void PromoteINT_TO_FP(SDNode *Node, SmallVectorImpl<SDValue> &Results);
172
173 /// Implements FP_TO_[SU]INT vector promotion of the result type.
174 ///
175 /// It is promoted to a larger integer type. The result is then
176 /// truncated back to the original type.
177 void PromoteFP_TO_INT(SDNode *Node, SmallVectorImpl<SDValue> &Results);
178
179 /// Implements vector setcc operation promotion.
180 ///
181 /// All vector operands are promoted to a vector type with larger element
182 /// type.
183 void PromoteSETCC(SDNode *Node, SmallVectorImpl<SDValue> &Results);
184
185 void PromoteSTRICT(SDNode *Node, SmallVectorImpl<SDValue> &Results);
186
187public:
188 VectorLegalizer(SelectionDAG& dag) :
189 DAG(dag), TLI(dag.getTargetLoweringInfo()) {}
190
191 /// Begin legalizer the vector operations in the DAG.
192 bool Run();
193};
194
195} // end anonymous namespace
196
197bool VectorLegalizer::Run() {
198 // Before we start legalizing vector nodes, check if there are any vectors.
199 bool HasVectors = false;
200 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
201 E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I) {
202 // Check if the values of the nodes contain vectors. We don't need to check
203 // the operands because we are going to check their values at some point.
204 HasVectors = llvm::any_of(I->values(), [](EVT T) { return T.isVector(); });
205
206 // If we found a vector node we can start the legalization.
207 if (HasVectors)
208 break;
209 }
210
211 // If this basic block has no vectors then no need to legalize vectors.
212 if (!HasVectors)
213 return false;
214
215 // The legalize process is inherently a bottom-up recursive process (users
216 // legalize their uses before themselves). Given infinite stack space, we
217 // could just start legalizing on the root and traverse the whole graph. In
218 // practice however, this causes us to run out of stack space on large basic
219 // blocks. To avoid this problem, compute an ordering of the nodes where each
220 // node is only legalized after all of its operands are legalized.
221 DAG.AssignTopologicalOrder();
222 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
223 E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I)
224 LegalizeOp(SDValue(&*I, 0));
225
226 // Finally, it's possible the root changed. Get the new root.
227 SDValue OldRoot = DAG.getRoot();
228 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
229 DAG.setRoot(LegalizedNodes[OldRoot]);
230
231 LegalizedNodes.clear();
232
233 // Remove dead nodes now.
234 DAG.RemoveDeadNodes();
235
236 return Changed;
237}
238
239SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDNode *Result) {
240 assert(Op->getNumValues() == Result->getNumValues() &&
241 "Unexpected number of results");
242 // Generic legalization: just pass the operand through.
243 for (unsigned i = 0, e = Op->getNumValues(); i != e; ++i)
244 AddLegalizedOperand(Op.getValue(i), SDValue(Result, i));
245 return SDValue(Result, Op.getResNo());
246}
247
249VectorLegalizer::RecursivelyLegalizeResults(SDValue Op,
251 assert(Results.size() == Op->getNumValues() &&
252 "Unexpected number of results");
253 // Make sure that the generated code is itself legal.
254 for (unsigned i = 0, e = Results.size(); i != e; ++i) {
255 Results[i] = LegalizeOp(Results[i]);
256 AddLegalizedOperand(Op.getValue(i), Results[i]);
257 }
258
259 return Results[Op.getResNo()];
260}
261
262SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
263 // Note that LegalizeOp may be reentered even from single-use nodes, which
264 // means that we always must cache transformed nodes.
265 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
266 if (I != LegalizedNodes.end()) return I->second;
267
268 // Legalize the operands
270 for (const SDValue &Oper : Op->op_values())
271 Ops.push_back(LegalizeOp(Oper));
272
273 SDNode *Node = DAG.UpdateNodeOperands(Op.getNode(), Ops);
274
275 bool HasVectorValueOrOp =
276 llvm::any_of(Node->values(), [](EVT T) { return T.isVector(); }) ||
277 llvm::any_of(Node->op_values(),
278 [](SDValue O) { return O.getValueType().isVector(); });
279 if (!HasVectorValueOrOp)
280 return TranslateLegalizeResults(Op, Node);
281
282 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
283 EVT ValVT;
284 switch (Op.getOpcode()) {
285 default:
286 return TranslateLegalizeResults(Op, Node);
287 case ISD::LOAD: {
288 LoadSDNode *LD = cast<LoadSDNode>(Node);
289 ISD::LoadExtType ExtType = LD->getExtensionType();
290 EVT LoadedVT = LD->getMemoryVT();
291 if (LoadedVT.isVector() && ExtType != ISD::NON_EXTLOAD)
292 Action = TLI.getLoadExtAction(ExtType, LD->getValueType(0), LoadedVT);
293 break;
294 }
295 case ISD::STORE: {
296 StoreSDNode *ST = cast<StoreSDNode>(Node);
297 EVT StVT = ST->getMemoryVT();
298 MVT ValVT = ST->getValue().getSimpleValueType();
299 if (StVT.isVector() && ST->isTruncatingStore())
300 Action = TLI.getTruncStoreAction(ValVT, StVT);
301 break;
302 }
304 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
305 // This operation lies about being legal: when it claims to be legal,
306 // it should actually be expanded.
307 if (Action == TargetLowering::Legal)
308 Action = TargetLowering::Expand;
309 break;
310#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
311 case ISD::STRICT_##DAGN:
312#include "llvm/IR/ConstrainedOps.def"
313 ValVT = Node->getValueType(0);
314 if (Op.getOpcode() == ISD::STRICT_SINT_TO_FP ||
315 Op.getOpcode() == ISD::STRICT_UINT_TO_FP)
316 ValVT = Node->getOperand(1).getValueType();
317 if (Op.getOpcode() == ISD::STRICT_FSETCC ||
318 Op.getOpcode() == ISD::STRICT_FSETCCS) {
319 MVT OpVT = Node->getOperand(1).getSimpleValueType();
320 ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(3))->get();
321 Action = TLI.getCondCodeAction(CCCode, OpVT);
322 if (Action == TargetLowering::Legal)
323 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
324 } else {
325 Action = TLI.getOperationAction(Node->getOpcode(), ValVT);
326 }
327 // If we're asked to expand a strict vector floating-point operation,
328 // by default we're going to simply unroll it. That is usually the
329 // best approach, except in the case where the resulting strict (scalar)
330 // operations would themselves use the fallback mutation to non-strict.
331 // In that specific case, just do the fallback on the vector op.
332 if (Action == TargetLowering::Expand && !TLI.isStrictFPEnabled() &&
333 TLI.getStrictFPOperationAction(Node->getOpcode(), ValVT) ==
334 TargetLowering::Legal) {
335 EVT EltVT = ValVT.getVectorElementType();
336 if (TLI.getOperationAction(Node->getOpcode(), EltVT)
337 == TargetLowering::Expand &&
338 TLI.getStrictFPOperationAction(Node->getOpcode(), EltVT)
339 == TargetLowering::Legal)
340 Action = TargetLowering::Legal;
341 }
342 break;
343 case ISD::ADD:
344 case ISD::SUB:
345 case ISD::MUL:
346 case ISD::MULHS:
347 case ISD::MULHU:
348 case ISD::SDIV:
349 case ISD::UDIV:
350 case ISD::SREM:
351 case ISD::UREM:
352 case ISD::SDIVREM:
353 case ISD::UDIVREM:
354 case ISD::FADD:
355 case ISD::FSUB:
356 case ISD::FMUL:
357 case ISD::FDIV:
358 case ISD::FREM:
359 case ISD::AND:
360 case ISD::OR:
361 case ISD::XOR:
362 case ISD::SHL:
363 case ISD::SRA:
364 case ISD::SRL:
365 case ISD::FSHL:
366 case ISD::FSHR:
367 case ISD::ROTL:
368 case ISD::ROTR:
369 case ISD::ABS:
370 case ISD::ABDS:
371 case ISD::ABDU:
372 case ISD::AVGCEILS:
373 case ISD::AVGCEILU:
374 case ISD::AVGFLOORS:
375 case ISD::AVGFLOORU:
376 case ISD::BSWAP:
377 case ISD::BITREVERSE:
378 case ISD::CTLZ:
379 case ISD::CTTZ:
382 case ISD::CTPOP:
383 case ISD::SELECT:
384 case ISD::VSELECT:
385 case ISD::SELECT_CC:
386 case ISD::ZERO_EXTEND:
387 case ISD::ANY_EXTEND:
388 case ISD::TRUNCATE:
389 case ISD::SIGN_EXTEND:
390 case ISD::FP_TO_SINT:
391 case ISD::FP_TO_UINT:
392 case ISD::FNEG:
393 case ISD::FABS:
394 case ISD::FMINNUM:
395 case ISD::FMAXNUM:
398 case ISD::FMINIMUM:
399 case ISD::FMAXIMUM:
400 case ISD::FCOPYSIGN:
401 case ISD::FSQRT:
402 case ISD::FSIN:
403 case ISD::FCOS:
404 case ISD::FTAN:
405 case ISD::FASIN:
406 case ISD::FACOS:
407 case ISD::FATAN:
408 case ISD::FSINH:
409 case ISD::FCOSH:
410 case ISD::FTANH:
411 case ISD::FLDEXP:
412 case ISD::FPOWI:
413 case ISD::FPOW:
414 case ISD::FLOG:
415 case ISD::FLOG2:
416 case ISD::FLOG10:
417 case ISD::FEXP:
418 case ISD::FEXP2:
419 case ISD::FEXP10:
420 case ISD::FCEIL:
421 case ISD::FTRUNC:
422 case ISD::FRINT:
423 case ISD::FNEARBYINT:
424 case ISD::FROUND:
425 case ISD::FROUNDEVEN:
426 case ISD::FFLOOR:
427 case ISD::FP_ROUND:
428 case ISD::FP_EXTEND:
430 case ISD::FMA:
435 case ISD::SMIN:
436 case ISD::SMAX:
437 case ISD::UMIN:
438 case ISD::UMAX:
439 case ISD::SMUL_LOHI:
440 case ISD::UMUL_LOHI:
441 case ISD::SADDO:
442 case ISD::UADDO:
443 case ISD::SSUBO:
444 case ISD::USUBO:
445 case ISD::SMULO:
446 case ISD::UMULO:
448 case ISD::FFREXP:
449 case ISD::SADDSAT:
450 case ISD::UADDSAT:
451 case ISD::SSUBSAT:
452 case ISD::USUBSAT:
453 case ISD::SSHLSAT:
454 case ISD::USHLSAT:
457 case ISD::MGATHER:
459 case ISD::SCMP:
460 case ISD::UCMP:
461 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
462 break;
463 case ISD::SMULFIX:
464 case ISD::SMULFIXSAT:
465 case ISD::UMULFIX:
466 case ISD::UMULFIXSAT:
467 case ISD::SDIVFIX:
468 case ISD::SDIVFIXSAT:
469 case ISD::UDIVFIX:
470 case ISD::UDIVFIXSAT: {
471 unsigned Scale = Node->getConstantOperandVal(2);
472 Action = TLI.getFixedPointOperationAction(Node->getOpcode(),
473 Node->getValueType(0), Scale);
474 break;
475 }
476 case ISD::LROUND:
477 case ISD::LLROUND:
478 case ISD::LRINT:
479 case ISD::LLRINT:
480 case ISD::SINT_TO_FP:
481 case ISD::UINT_TO_FP:
497 Action = TLI.getOperationAction(Node->getOpcode(),
498 Node->getOperand(0).getValueType());
499 break;
502 Action = TLI.getOperationAction(Node->getOpcode(),
503 Node->getOperand(1).getValueType());
504 break;
505 case ISD::SETCC: {
506 MVT OpVT = Node->getOperand(0).getSimpleValueType();
507 ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get();
508 Action = TLI.getCondCodeAction(CCCode, OpVT);
509 if (Action == TargetLowering::Legal)
510 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
511 break;
512 }
513
514#define BEGIN_REGISTER_VP_SDNODE(VPID, LEGALPOS, ...) \
515 case ISD::VPID: { \
516 EVT LegalizeVT = LEGALPOS < 0 ? Node->getValueType(-(1 + LEGALPOS)) \
517 : Node->getOperand(LEGALPOS).getValueType(); \
518 if (ISD::VPID == ISD::VP_SETCC) { \
519 ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get(); \
520 Action = TLI.getCondCodeAction(CCCode, LegalizeVT.getSimpleVT()); \
521 if (Action != TargetLowering::Legal) \
522 break; \
523 } \
524 /* Defer non-vector results to LegalizeDAG. */ \
525 if (!Node->getValueType(0).isVector() && \
526 Node->getValueType(0) != MVT::Other) { \
527 Action = TargetLowering::Legal; \
528 break; \
529 } \
530 Action = TLI.getOperationAction(Node->getOpcode(), LegalizeVT); \
531 } break;
532#include "llvm/IR/VPIntrinsics.def"
533 }
534
535 LLVM_DEBUG(dbgs() << "\nLegalizing vector op: "; Node->dump(&DAG));
536
537 SmallVector<SDValue, 8> ResultVals;
538 switch (Action) {
539 default: llvm_unreachable("This action is not supported yet!");
540 case TargetLowering::Promote:
541 assert((Op.getOpcode() != ISD::LOAD && Op.getOpcode() != ISD::STORE) &&
542 "This action is not supported yet!");
543 LLVM_DEBUG(dbgs() << "Promoting\n");
544 Promote(Node, ResultVals);
545 assert(!ResultVals.empty() && "No results for promotion?");
546 break;
547 case TargetLowering::Legal:
548 LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n");
549 break;
550 case TargetLowering::Custom:
551 LLVM_DEBUG(dbgs() << "Trying custom legalization\n");
552 if (LowerOperationWrapper(Node, ResultVals))
553 break;
554 LLVM_DEBUG(dbgs() << "Could not custom legalize node\n");
555 [[fallthrough]];
556 case TargetLowering::Expand:
557 LLVM_DEBUG(dbgs() << "Expanding\n");
558 Expand(Node, ResultVals);
559 break;
560 }
561
562 if (ResultVals.empty())
563 return TranslateLegalizeResults(Op, Node);
564
565 Changed = true;
566 return RecursivelyLegalizeResults(Op, ResultVals);
567}
568
569// FIXME: This is very similar to TargetLowering::LowerOperationWrapper. Can we
570// merge them somehow?
571bool VectorLegalizer::LowerOperationWrapper(SDNode *Node,
573 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
574
575 if (!Res.getNode())
576 return false;
577
578 if (Res == SDValue(Node, 0))
579 return true;
580
581 // If the original node has one result, take the return value from
582 // LowerOperation as is. It might not be result number 0.
583 if (Node->getNumValues() == 1) {
584 Results.push_back(Res);
585 return true;
586 }
587
588 // If the original node has multiple results, then the return node should
589 // have the same number of results.
590 assert((Node->getNumValues() == Res->getNumValues()) &&
591 "Lowering returned the wrong number of results!");
592
593 // Places new result values base on N result number.
594 for (unsigned I = 0, E = Node->getNumValues(); I != E; ++I)
595 Results.push_back(Res.getValue(I));
596
597 return true;
598}
599
600void VectorLegalizer::PromoteSETCC(SDNode *Node,
602 MVT VecVT = Node->getOperand(0).getSimpleValueType();
603 MVT NewVecVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VecVT);
604
605 unsigned ExtOp = VecVT.isFloatingPoint() ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
606
607 SDLoc DL(Node);
608 SmallVector<SDValue, 5> Operands(Node->getNumOperands());
609
610 Operands[0] = DAG.getNode(ExtOp, DL, NewVecVT, Node->getOperand(0));
611 Operands[1] = DAG.getNode(ExtOp, DL, NewVecVT, Node->getOperand(1));
612 Operands[2] = Node->getOperand(2);
613
614 if (Node->getOpcode() == ISD::VP_SETCC) {
615 Operands[3] = Node->getOperand(3); // mask
616 Operands[4] = Node->getOperand(4); // evl
617 }
618
619 SDValue Res = DAG.getNode(Node->getOpcode(), DL, Node->getSimpleValueType(0),
620 Operands, Node->getFlags());
621
622 Results.push_back(Res);
623}
624
625void VectorLegalizer::PromoteSTRICT(SDNode *Node,
627 MVT VecVT = Node->getOperand(1).getSimpleValueType();
628 MVT NewVecVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VecVT);
629
630 assert(VecVT.isFloatingPoint());
631
632 SDLoc DL(Node);
633 SmallVector<SDValue, 5> Operands(Node->getNumOperands());
635
636 for (unsigned j = 1; j != Node->getNumOperands(); ++j)
637 if (Node->getOperand(j).getValueType().isVector() &&
638 !(ISD::isVPOpcode(Node->getOpcode()) &&
639 ISD::getVPMaskIdx(Node->getOpcode()) == j)) // Skip mask operand.
640 {
641 // promote the vector operand.
642 SDValue Ext =
643 DAG.getNode(ISD::STRICT_FP_EXTEND, DL, {NewVecVT, MVT::Other},
644 {Node->getOperand(0), Node->getOperand(j)});
645 Operands[j] = Ext.getValue(0);
646 Chains.push_back(Ext.getValue(1));
647 } else
648 Operands[j] = Node->getOperand(j); // Skip no vector operand.
649
650 SDVTList VTs = DAG.getVTList(NewVecVT, Node->getValueType(1));
651
652 Operands[0] = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
653
654 SDValue Res =
655 DAG.getNode(Node->getOpcode(), DL, VTs, Operands, Node->getFlags());
656
657 SDValue Round =
658 DAG.getNode(ISD::STRICT_FP_ROUND, DL, {VecVT, MVT::Other},
659 {Res.getValue(1), Res.getValue(0),
660 DAG.getIntPtrConstant(0, DL, /*isTarget=*/true)});
661
662 Results.push_back(Round.getValue(0));
663 Results.push_back(Round.getValue(1));
664}
665
666void VectorLegalizer::Promote(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
667 // For a few operations there is a specific concept for promotion based on
668 // the operand's type.
669 switch (Node->getOpcode()) {
670 case ISD::SINT_TO_FP:
671 case ISD::UINT_TO_FP:
674 // "Promote" the operation by extending the operand.
675 PromoteINT_TO_FP(Node, Results);
676 return;
677 case ISD::FP_TO_UINT:
678 case ISD::FP_TO_SINT:
681 // Promote the operation by extending the operand.
682 PromoteFP_TO_INT(Node, Results);
683 return;
684 case ISD::VP_SETCC:
685 case ISD::SETCC:
686 // Promote the operation by extending the operand.
687 PromoteSETCC(Node, Results);
688 return;
689 case ISD::STRICT_FADD:
690 case ISD::STRICT_FSUB:
691 case ISD::STRICT_FMUL:
692 case ISD::STRICT_FDIV:
694 case ISD::STRICT_FMA:
695 PromoteSTRICT(Node, Results);
696 return;
697 case ISD::FP_ROUND:
698 case ISD::FP_EXTEND:
699 // These operations are used to do promotion so they can't be promoted
700 // themselves.
701 llvm_unreachable("Don't know how to promote this operation!");
702 }
703
704 // There are currently two cases of vector promotion:
705 // 1) Bitcasting a vector of integers to a different type to a vector of the
706 // same overall length. For example, x86 promotes ISD::AND v2i32 to v1i64.
707 // 2) Extending a vector of floats to a vector of the same number of larger
708 // floats. For example, AArch64 promotes ISD::FADD on v4f16 to v4f32.
709 assert(Node->getNumValues() == 1 &&
710 "Can't promote a vector with multiple results!");
711 MVT VT = Node->getSimpleValueType(0);
712 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
713 SDLoc dl(Node);
714 SmallVector<SDValue, 4> Operands(Node->getNumOperands());
715
716 for (unsigned j = 0; j != Node->getNumOperands(); ++j) {
717 // Do not promote the mask operand of a VP OP.
718 bool SkipPromote = ISD::isVPOpcode(Node->getOpcode()) &&
719 ISD::getVPMaskIdx(Node->getOpcode()) == j;
720 if (Node->getOperand(j).getValueType().isVector() && !SkipPromote)
721 if (Node->getOperand(j)
722 .getValueType()
723 .getVectorElementType()
724 .isFloatingPoint() &&
726 Operands[j] = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(j));
727 else
728 Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(j));
729 else
730 Operands[j] = Node->getOperand(j);
731 }
732
733 SDValue Res =
734 DAG.getNode(Node->getOpcode(), dl, NVT, Operands, Node->getFlags());
735
736 if ((VT.isFloatingPoint() && NVT.isFloatingPoint()) ||
739 Res = DAG.getNode(ISD::FP_ROUND, dl, VT, Res,
740 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
741 else
742 Res = DAG.getNode(ISD::BITCAST, dl, VT, Res);
743
744 Results.push_back(Res);
745}
746
747void VectorLegalizer::PromoteINT_TO_FP(SDNode *Node,
749 // INT_TO_FP operations may require the input operand be promoted even
750 // when the type is otherwise legal.
751 bool IsStrict = Node->isStrictFPOpcode();
752 MVT VT = Node->getOperand(IsStrict ? 1 : 0).getSimpleValueType();
753 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
755 "Vectors have different number of elements!");
756
757 SDLoc dl(Node);
758 SmallVector<SDValue, 4> Operands(Node->getNumOperands());
759
760 unsigned Opc = (Node->getOpcode() == ISD::UINT_TO_FP ||
761 Node->getOpcode() == ISD::STRICT_UINT_TO_FP)
764 for (unsigned j = 0; j != Node->getNumOperands(); ++j) {
765 if (Node->getOperand(j).getValueType().isVector())
766 Operands[j] = DAG.getNode(Opc, dl, NVT, Node->getOperand(j));
767 else
768 Operands[j] = Node->getOperand(j);
769 }
770
771 if (IsStrict) {
772 SDValue Res = DAG.getNode(Node->getOpcode(), dl,
773 {Node->getValueType(0), MVT::Other}, Operands);
774 Results.push_back(Res);
775 Results.push_back(Res.getValue(1));
776 return;
777 }
778
779 SDValue Res =
780 DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Operands);
781 Results.push_back(Res);
782}
783
784// For FP_TO_INT we promote the result type to a vector type with wider
785// elements and then truncate the result. This is different from the default
786// PromoteVector which uses bitcast to promote thus assumning that the
787// promoted vector type has the same overall size.
788void VectorLegalizer::PromoteFP_TO_INT(SDNode *Node,
790 MVT VT = Node->getSimpleValueType(0);
791 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
792 bool IsStrict = Node->isStrictFPOpcode();
794 "Vectors have different number of elements!");
795
796 unsigned NewOpc = Node->getOpcode();
797 // Change FP_TO_UINT to FP_TO_SINT if possible.
798 // TODO: Should we only do this if FP_TO_UINT itself isn't legal?
799 if (NewOpc == ISD::FP_TO_UINT &&
800 TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
801 NewOpc = ISD::FP_TO_SINT;
802
803 if (NewOpc == ISD::STRICT_FP_TO_UINT &&
804 TLI.isOperationLegalOrCustom(ISD::STRICT_FP_TO_SINT, NVT))
805 NewOpc = ISD::STRICT_FP_TO_SINT;
806
807 SDLoc dl(Node);
808 SDValue Promoted, Chain;
809 if (IsStrict) {
810 Promoted = DAG.getNode(NewOpc, dl, {NVT, MVT::Other},
811 {Node->getOperand(0), Node->getOperand(1)});
812 Chain = Promoted.getValue(1);
813 } else
814 Promoted = DAG.getNode(NewOpc, dl, NVT, Node->getOperand(0));
815
816 // Assert that the converted value fits in the original type. If it doesn't
817 // (eg: because the value being converted is too big), then the result of the
818 // original operation was undefined anyway, so the assert is still correct.
819 if (Node->getOpcode() == ISD::FP_TO_UINT ||
820 Node->getOpcode() == ISD::STRICT_FP_TO_UINT)
821 NewOpc = ISD::AssertZext;
822 else
823 NewOpc = ISD::AssertSext;
824
825 Promoted = DAG.getNode(NewOpc, dl, NVT, Promoted,
826 DAG.getValueType(VT.getScalarType()));
827 Promoted = DAG.getNode(ISD::TRUNCATE, dl, VT, Promoted);
828 Results.push_back(Promoted);
829 if (IsStrict)
830 Results.push_back(Chain);
831}
832
833std::pair<SDValue, SDValue> VectorLegalizer::ExpandLoad(SDNode *N) {
834 LoadSDNode *LD = cast<LoadSDNode>(N);
835 return TLI.scalarizeVectorLoad(LD, DAG);
836}
837
838SDValue VectorLegalizer::ExpandStore(SDNode *N) {
839 StoreSDNode *ST = cast<StoreSDNode>(N);
840 SDValue TF = TLI.scalarizeVectorStore(ST, DAG);
841 return TF;
842}
843
844void VectorLegalizer::Expand(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
845 switch (Node->getOpcode()) {
846 case ISD::LOAD: {
847 std::pair<SDValue, SDValue> Tmp = ExpandLoad(Node);
848 Results.push_back(Tmp.first);
849 Results.push_back(Tmp.second);
850 return;
851 }
852 case ISD::STORE:
853 Results.push_back(ExpandStore(Node));
854 return;
856 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
857 Results.push_back(Node->getOperand(i));
858 return;
860 Results.push_back(ExpandSEXTINREG(Node));
861 return;
863 Results.push_back(ExpandANY_EXTEND_VECTOR_INREG(Node));
864 return;
866 Results.push_back(ExpandSIGN_EXTEND_VECTOR_INREG(Node));
867 return;
869 Results.push_back(ExpandZERO_EXTEND_VECTOR_INREG(Node));
870 return;
871 case ISD::BSWAP:
872 Results.push_back(ExpandBSWAP(Node));
873 return;
874 case ISD::VP_BSWAP:
875 Results.push_back(TLI.expandVPBSWAP(Node, DAG));
876 return;
877 case ISD::VSELECT:
878 Results.push_back(ExpandVSELECT(Node));
879 return;
880 case ISD::VP_SELECT:
881 Results.push_back(ExpandVP_SELECT(Node));
882 return;
883 case ISD::VP_SREM:
884 case ISD::VP_UREM:
885 if (SDValue Expanded = ExpandVP_REM(Node)) {
886 Results.push_back(Expanded);
887 return;
888 }
889 break;
890 case ISD::SELECT:
891 Results.push_back(ExpandSELECT(Node));
892 return;
893 case ISD::SELECT_CC: {
894 if (Node->getValueType(0).isScalableVector()) {
895 EVT CondVT = TLI.getSetCCResultType(
896 DAG.getDataLayout(), *DAG.getContext(), Node->getValueType(0));
897 SDValue SetCC =
898 DAG.getNode(ISD::SETCC, SDLoc(Node), CondVT, Node->getOperand(0),
899 Node->getOperand(1), Node->getOperand(4));
900 Results.push_back(DAG.getSelect(SDLoc(Node), Node->getValueType(0), SetCC,
901 Node->getOperand(2),
902 Node->getOperand(3)));
903 return;
904 }
905 break;
906 }
907 case ISD::FP_TO_UINT:
908 ExpandFP_TO_UINT(Node, Results);
909 return;
910 case ISD::UINT_TO_FP:
911 ExpandUINT_TO_FLOAT(Node, Results);
912 return;
913 case ISD::FNEG:
914 Results.push_back(ExpandFNEG(Node));
915 return;
916 case ISD::FSUB:
917 ExpandFSUB(Node, Results);
918 return;
919 case ISD::SETCC:
920 case ISD::VP_SETCC:
921 ExpandSETCC(Node, Results);
922 return;
923 case ISD::ABS:
924 if (SDValue Expanded = TLI.expandABS(Node, DAG)) {
925 Results.push_back(Expanded);
926 return;
927 }
928 break;
929 case ISD::ABDS:
930 case ISD::ABDU:
931 if (SDValue Expanded = TLI.expandABD(Node, DAG)) {
932 Results.push_back(Expanded);
933 return;
934 }
935 break;
936 case ISD::AVGCEILS:
937 case ISD::AVGCEILU:
938 case ISD::AVGFLOORS:
939 case ISD::AVGFLOORU:
940 if (SDValue Expanded = TLI.expandAVG(Node, DAG)) {
941 Results.push_back(Expanded);
942 return;
943 }
944 break;
945 case ISD::BITREVERSE:
946 ExpandBITREVERSE(Node, Results);
947 return;
948 case ISD::VP_BITREVERSE:
949 if (SDValue Expanded = TLI.expandVPBITREVERSE(Node, DAG)) {
950 Results.push_back(Expanded);
951 return;
952 }
953 break;
954 case ISD::CTPOP:
955 if (SDValue Expanded = TLI.expandCTPOP(Node, DAG)) {
956 Results.push_back(Expanded);
957 return;
958 }
959 break;
960 case ISD::VP_CTPOP:
961 if (SDValue Expanded = TLI.expandVPCTPOP(Node, DAG)) {
962 Results.push_back(Expanded);
963 return;
964 }
965 break;
966 case ISD::CTLZ:
968 if (SDValue Expanded = TLI.expandCTLZ(Node, DAG)) {
969 Results.push_back(Expanded);
970 return;
971 }
972 break;
973 case ISD::VP_CTLZ:
974 case ISD::VP_CTLZ_ZERO_UNDEF:
975 if (SDValue Expanded = TLI.expandVPCTLZ(Node, DAG)) {
976 Results.push_back(Expanded);
977 return;
978 }
979 break;
980 case ISD::CTTZ:
982 if (SDValue Expanded = TLI.expandCTTZ(Node, DAG)) {
983 Results.push_back(Expanded);
984 return;
985 }
986 break;
987 case ISD::VP_CTTZ:
988 case ISD::VP_CTTZ_ZERO_UNDEF:
989 if (SDValue Expanded = TLI.expandVPCTTZ(Node, DAG)) {
990 Results.push_back(Expanded);
991 return;
992 }
993 break;
994 case ISD::FSHL:
995 case ISD::VP_FSHL:
996 case ISD::FSHR:
997 case ISD::VP_FSHR:
998 if (SDValue Expanded = TLI.expandFunnelShift(Node, DAG)) {
999 Results.push_back(Expanded);
1000 return;
1001 }
1002 break;
1003 case ISD::ROTL:
1004 case ISD::ROTR:
1005 if (SDValue Expanded = TLI.expandROT(Node, false /*AllowVectorOps*/, DAG)) {
1006 Results.push_back(Expanded);
1007 return;
1008 }
1009 break;
1010 case ISD::FMINNUM:
1011 case ISD::FMAXNUM:
1012 if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG)) {
1013 Results.push_back(Expanded);
1014 return;
1015 }
1016 break;
1017 case ISD::FMINIMUM:
1018 case ISD::FMAXIMUM:
1019 Results.push_back(TLI.expandFMINIMUM_FMAXIMUM(Node, DAG));
1020 return;
1021 case ISD::SMIN:
1022 case ISD::SMAX:
1023 case ISD::UMIN:
1024 case ISD::UMAX:
1025 if (SDValue Expanded = TLI.expandIntMINMAX(Node, DAG)) {
1026 Results.push_back(Expanded);
1027 return;
1028 }
1029 break;
1030 case ISD::UADDO:
1031 case ISD::USUBO:
1032 ExpandUADDSUBO(Node, Results);
1033 return;
1034 case ISD::SADDO:
1035 case ISD::SSUBO:
1036 ExpandSADDSUBO(Node, Results);
1037 return;
1038 case ISD::UMULO:
1039 case ISD::SMULO:
1040 ExpandMULO(Node, Results);
1041 return;
1042 case ISD::USUBSAT:
1043 case ISD::SSUBSAT:
1044 case ISD::UADDSAT:
1045 case ISD::SADDSAT:
1046 if (SDValue Expanded = TLI.expandAddSubSat(Node, DAG)) {
1047 Results.push_back(Expanded);
1048 return;
1049 }
1050 break;
1051 case ISD::USHLSAT:
1052 case ISD::SSHLSAT:
1053 if (SDValue Expanded = TLI.expandShlSat(Node, DAG)) {
1054 Results.push_back(Expanded);
1055 return;
1056 }
1057 break;
1060 // Expand the fpsosisat if it is scalable to prevent it from unrolling below.
1061 if (Node->getValueType(0).isScalableVector()) {
1062 if (SDValue Expanded = TLI.expandFP_TO_INT_SAT(Node, DAG)) {
1063 Results.push_back(Expanded);
1064 return;
1065 }
1066 }
1067 break;
1068 case ISD::SMULFIX:
1069 case ISD::UMULFIX:
1070 if (SDValue Expanded = TLI.expandFixedPointMul(Node, DAG)) {
1071 Results.push_back(Expanded);
1072 return;
1073 }
1074 break;
1075 case ISD::SMULFIXSAT:
1076 case ISD::UMULFIXSAT:
1077 // FIXME: We do not expand SMULFIXSAT/UMULFIXSAT here yet, not sure exactly
1078 // why. Maybe it results in worse codegen compared to the unroll for some
1079 // targets? This should probably be investigated. And if we still prefer to
1080 // unroll an explanation could be helpful.
1081 break;
1082 case ISD::SDIVFIX:
1083 case ISD::UDIVFIX:
1084 ExpandFixedPointDiv(Node, Results);
1085 return;
1086 case ISD::SDIVFIXSAT:
1087 case ISD::UDIVFIXSAT:
1088 break;
1089#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
1090 case ISD::STRICT_##DAGN:
1091#include "llvm/IR/ConstrainedOps.def"
1092 ExpandStrictFPOp(Node, Results);
1093 return;
1094 case ISD::VECREDUCE_ADD:
1095 case ISD::VECREDUCE_MUL:
1096 case ISD::VECREDUCE_AND:
1097 case ISD::VECREDUCE_OR:
1098 case ISD::VECREDUCE_XOR:
1109 Results.push_back(TLI.expandVecReduce(Node, DAG));
1110 return;
1113 Results.push_back(TLI.expandVecReduceSeq(Node, DAG));
1114 return;
1115 case ISD::SREM:
1116 case ISD::UREM:
1117 ExpandREM(Node, Results);
1118 return;
1119 case ISD::VP_MERGE:
1120 Results.push_back(ExpandVP_MERGE(Node));
1121 return;
1122 case ISD::FREM:
1123 if (tryExpandVecMathCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
1124 RTLIB::REM_F80, RTLIB::REM_F128,
1125 RTLIB::REM_PPCF128, Results))
1126 return;
1127
1128 break;
1130 Results.push_back(TLI.expandVECTOR_COMPRESS(Node, DAG));
1131 return;
1132 }
1133
1134 SDValue Unrolled = DAG.UnrollVectorOp(Node);
1135 if (Node->getNumValues() == 1) {
1136 Results.push_back(Unrolled);
1137 } else {
1138 assert(Node->getNumValues() == Unrolled->getNumValues() &&
1139 "VectorLegalizer Expand returned wrong number of results!");
1140 for (unsigned I = 0, E = Unrolled->getNumValues(); I != E; ++I)
1141 Results.push_back(Unrolled.getValue(I));
1142 }
1143}
1144
1145SDValue VectorLegalizer::ExpandSELECT(SDNode *Node) {
1146 // Lower a select instruction where the condition is a scalar and the
1147 // operands are vectors. Lower this select to VSELECT and implement it
1148 // using XOR AND OR. The selector bit is broadcasted.
1149 EVT VT = Node->getValueType(0);
1150 SDLoc DL(Node);
1151
1152 SDValue Mask = Node->getOperand(0);
1153 SDValue Op1 = Node->getOperand(1);
1154 SDValue Op2 = Node->getOperand(2);
1155
1156 assert(VT.isVector() && !Mask.getValueType().isVector()
1157 && Op1.getValueType() == Op2.getValueType() && "Invalid type");
1158
1159 // If we can't even use the basic vector operations of
1160 // AND,OR,XOR, we will have to scalarize the op.
1161 // Notice that the operation may be 'promoted' which means that it is
1162 // 'bitcasted' to another type which is handled.
1163 // Also, we need to be able to construct a splat vector using either
1164 // BUILD_VECTOR or SPLAT_VECTOR.
1165 // FIXME: Should we also permit fixed-length SPLAT_VECTOR as a fallback to
1166 // BUILD_VECTOR?
1167 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
1168 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
1169 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
1170 TLI.getOperationAction(VT.isFixedLengthVector() ? ISD::BUILD_VECTOR
1172 VT) == TargetLowering::Expand)
1173 return DAG.UnrollVectorOp(Node);
1174
1175 // Generate a mask operand.
1177
1178 // What is the size of each element in the vector mask.
1179 EVT BitTy = MaskTy.getScalarType();
1180
1181 Mask = DAG.getSelect(DL, BitTy, Mask, DAG.getAllOnesConstant(DL, BitTy),
1182 DAG.getConstant(0, DL, BitTy));
1183
1184 // Broadcast the mask so that the entire vector is all one or all zero.
1185 Mask = DAG.getSplat(MaskTy, DL, Mask);
1186
1187 // Bitcast the operands to be the same type as the mask.
1188 // This is needed when we select between FP types because
1189 // the mask is a vector of integers.
1190 Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1);
1191 Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2);
1192
1193 SDValue NotMask = DAG.getNOT(DL, Mask, MaskTy);
1194
1195 Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask);
1196 Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask);
1197 SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2);
1198 return DAG.getNode(ISD::BITCAST, DL, Node->getValueType(0), Val);
1199}
1200
1201SDValue VectorLegalizer::ExpandSEXTINREG(SDNode *Node) {
1202 EVT VT = Node->getValueType(0);
1203
1204 // Make sure that the SRA and SHL instructions are available.
1205 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand ||
1206 TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand)
1207 return DAG.UnrollVectorOp(Node);
1208
1209 SDLoc DL(Node);
1210 EVT OrigTy = cast<VTSDNode>(Node->getOperand(1))->getVT();
1211
1212 unsigned BW = VT.getScalarSizeInBits();
1213 unsigned OrigBW = OrigTy.getScalarSizeInBits();
1214 SDValue ShiftSz = DAG.getConstant(BW - OrigBW, DL, VT);
1215
1216 SDValue Op = DAG.getNode(ISD::SHL, DL, VT, Node->getOperand(0), ShiftSz);
1217 return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz);
1218}
1219
1220// Generically expand a vector anyext in register to a shuffle of the relevant
1221// lanes into the appropriate locations, with other lanes left undef.
1222SDValue VectorLegalizer::ExpandANY_EXTEND_VECTOR_INREG(SDNode *Node) {
1223 SDLoc DL(Node);
1224 EVT VT = Node->getValueType(0);
1225 int NumElements = VT.getVectorNumElements();
1226 SDValue Src = Node->getOperand(0);
1227 EVT SrcVT = Src.getValueType();
1228 int NumSrcElements = SrcVT.getVectorNumElements();
1229
1230 // *_EXTEND_VECTOR_INREG SrcVT can be smaller than VT - so insert the vector
1231 // into a larger vector type.
1232 if (SrcVT.bitsLE(VT)) {
1233 assert((VT.getSizeInBits() % SrcVT.getScalarSizeInBits()) == 0 &&
1234 "ANY_EXTEND_VECTOR_INREG vector size mismatch");
1235 NumSrcElements = VT.getSizeInBits() / SrcVT.getScalarSizeInBits();
1236 SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getScalarType(),
1237 NumSrcElements);
1238 Src = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, SrcVT, DAG.getUNDEF(SrcVT),
1239 Src, DAG.getVectorIdxConstant(0, DL));
1240 }
1241
1242 // Build a base mask of undef shuffles.
1243 SmallVector<int, 16> ShuffleMask;
1244 ShuffleMask.resize(NumSrcElements, -1);
1245
1246 // Place the extended lanes into the correct locations.
1247 int ExtLaneScale = NumSrcElements / NumElements;
1248 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
1249 for (int i = 0; i < NumElements; ++i)
1250 ShuffleMask[i * ExtLaneScale + EndianOffset] = i;
1251
1252 return DAG.getNode(
1253 ISD::BITCAST, DL, VT,
1254 DAG.getVectorShuffle(SrcVT, DL, Src, DAG.getUNDEF(SrcVT), ShuffleMask));
1255}
1256
1257SDValue VectorLegalizer::ExpandSIGN_EXTEND_VECTOR_INREG(SDNode *Node) {
1258 SDLoc DL(Node);
1259 EVT VT = Node->getValueType(0);
1260 SDValue Src = Node->getOperand(0);
1261 EVT SrcVT = Src.getValueType();
1262
1263 // First build an any-extend node which can be legalized above when we
1264 // recurse through it.
1265 SDValue Op = DAG.getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Src);
1266
1267 // Now we need sign extend. Do this by shifting the elements. Even if these
1268 // aren't legal operations, they have a better chance of being legalized
1269 // without full scalarization than the sign extension does.
1270 unsigned EltWidth = VT.getScalarSizeInBits();
1271 unsigned SrcEltWidth = SrcVT.getScalarSizeInBits();
1272 SDValue ShiftAmount = DAG.getConstant(EltWidth - SrcEltWidth, DL, VT);
1273 return DAG.getNode(ISD::SRA, DL, VT,
1274 DAG.getNode(ISD::SHL, DL, VT, Op, ShiftAmount),
1275 ShiftAmount);
1276}
1277
1278// Generically expand a vector zext in register to a shuffle of the relevant
1279// lanes into the appropriate locations, a blend of zero into the high bits,
1280// and a bitcast to the wider element type.
1281SDValue VectorLegalizer::ExpandZERO_EXTEND_VECTOR_INREG(SDNode *Node) {
1282 SDLoc DL(Node);
1283 EVT VT = Node->getValueType(0);
1284 int NumElements = VT.getVectorNumElements();
1285 SDValue Src = Node->getOperand(0);
1286 EVT SrcVT = Src.getValueType();
1287 int NumSrcElements = SrcVT.getVectorNumElements();
1288
1289 // *_EXTEND_VECTOR_INREG SrcVT can be smaller than VT - so insert the vector
1290 // into a larger vector type.
1291 if (SrcVT.bitsLE(VT)) {
1292 assert((VT.getSizeInBits() % SrcVT.getScalarSizeInBits()) == 0 &&
1293 "ZERO_EXTEND_VECTOR_INREG vector size mismatch");
1294 NumSrcElements = VT.getSizeInBits() / SrcVT.getScalarSizeInBits();
1295 SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getScalarType(),
1296 NumSrcElements);
1297 Src = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, SrcVT, DAG.getUNDEF(SrcVT),
1298 Src, DAG.getVectorIdxConstant(0, DL));
1299 }
1300
1301 // Build up a zero vector to blend into this one.
1302 SDValue Zero = DAG.getConstant(0, DL, SrcVT);
1303
1304 // Shuffle the incoming lanes into the correct position, and pull all other
1305 // lanes from the zero vector.
1306 auto ShuffleMask = llvm::to_vector<16>(llvm::seq<int>(0, NumSrcElements));
1307
1308 int ExtLaneScale = NumSrcElements / NumElements;
1309 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
1310 for (int i = 0; i < NumElements; ++i)
1311 ShuffleMask[i * ExtLaneScale + EndianOffset] = NumSrcElements + i;
1312
1313 return DAG.getNode(ISD::BITCAST, DL, VT,
1314 DAG.getVectorShuffle(SrcVT, DL, Zero, Src, ShuffleMask));
1315}
1316
1317static void createBSWAPShuffleMask(EVT VT, SmallVectorImpl<int> &ShuffleMask) {
1318 int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8;
1319 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I)
1320 for (int J = ScalarSizeInBytes - 1; J >= 0; --J)
1321 ShuffleMask.push_back((I * ScalarSizeInBytes) + J);
1322}
1323
1324SDValue VectorLegalizer::ExpandBSWAP(SDNode *Node) {
1325 EVT VT = Node->getValueType(0);
1326
1327 // Scalable vectors can't use shuffle expansion.
1328 if (VT.isScalableVector())
1329 return TLI.expandBSWAP(Node, DAG);
1330
1331 // Generate a byte wise shuffle mask for the BSWAP.
1332 SmallVector<int, 16> ShuffleMask;
1333 createBSWAPShuffleMask(VT, ShuffleMask);
1334 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size());
1335
1336 // Only emit a shuffle if the mask is legal.
1337 if (TLI.isShuffleMaskLegal(ShuffleMask, ByteVT)) {
1338 SDLoc DL(Node);
1339 SDValue Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Node->getOperand(0));
1340 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), ShuffleMask);
1341 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
1342 }
1343
1344 // If we have the appropriate vector bit operations, it is better to use them
1345 // than unrolling and expanding each component.
1346 if (TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
1347 TLI.isOperationLegalOrCustom(ISD::SRL, VT) &&
1348 TLI.isOperationLegalOrCustomOrPromote(ISD::AND, VT) &&
1349 TLI.isOperationLegalOrCustomOrPromote(ISD::OR, VT))
1350 return TLI.expandBSWAP(Node, DAG);
1351
1352 // Otherwise unroll.
1353 return DAG.UnrollVectorOp(Node);
1354}
1355
1356void VectorLegalizer::ExpandBITREVERSE(SDNode *Node,
1358 EVT VT = Node->getValueType(0);
1359
1360 // We can't unroll or use shuffles for scalable vectors.
1361 if (VT.isScalableVector()) {
1362 Results.push_back(TLI.expandBITREVERSE(Node, DAG));
1363 return;
1364 }
1365
1366 // If we have the scalar operation, it's probably cheaper to unroll it.
1367 if (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, VT.getScalarType())) {
1368 SDValue Tmp = DAG.UnrollVectorOp(Node);
1369 Results.push_back(Tmp);
1370 return;
1371 }
1372
1373 // If the vector element width is a whole number of bytes, test if its legal
1374 // to BSWAP shuffle the bytes and then perform the BITREVERSE on the byte
1375 // vector. This greatly reduces the number of bit shifts necessary.
1376 unsigned ScalarSizeInBits = VT.getScalarSizeInBits();
1377 if (ScalarSizeInBits > 8 && (ScalarSizeInBits % 8) == 0) {
1378 SmallVector<int, 16> BSWAPMask;
1379 createBSWAPShuffleMask(VT, BSWAPMask);
1380
1381 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, BSWAPMask.size());
1382 if (TLI.isShuffleMaskLegal(BSWAPMask, ByteVT) &&
1383 (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, ByteVT) ||
1384 (TLI.isOperationLegalOrCustom(ISD::SHL, ByteVT) &&
1385 TLI.isOperationLegalOrCustom(ISD::SRL, ByteVT) &&
1386 TLI.isOperationLegalOrCustomOrPromote(ISD::AND, ByteVT) &&
1387 TLI.isOperationLegalOrCustomOrPromote(ISD::OR, ByteVT)))) {
1388 SDLoc DL(Node);
1389 SDValue Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Node->getOperand(0));
1390 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT),
1391 BSWAPMask);
1392 Op = DAG.getNode(ISD::BITREVERSE, DL, ByteVT, Op);
1393 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
1394 Results.push_back(Op);
1395 return;
1396 }
1397 }
1398
1399 // If we have the appropriate vector bit operations, it is better to use them
1400 // than unrolling and expanding each component.
1401 if (TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
1402 TLI.isOperationLegalOrCustom(ISD::SRL, VT) &&
1403 TLI.isOperationLegalOrCustomOrPromote(ISD::AND, VT) &&
1404 TLI.isOperationLegalOrCustomOrPromote(ISD::OR, VT)) {
1405 Results.push_back(TLI.expandBITREVERSE(Node, DAG));
1406 return;
1407 }
1408
1409 // Otherwise unroll.
1410 SDValue Tmp = DAG.UnrollVectorOp(Node);
1411 Results.push_back(Tmp);
1412}
1413
1414SDValue VectorLegalizer::ExpandVSELECT(SDNode *Node) {
1415 // Implement VSELECT in terms of XOR, AND, OR
1416 // on platforms which do not support blend natively.
1417 SDLoc DL(Node);
1418
1419 SDValue Mask = Node->getOperand(0);
1420 SDValue Op1 = Node->getOperand(1);
1421 SDValue Op2 = Node->getOperand(2);
1422
1423 EVT VT = Mask.getValueType();
1424
1425 // If we can't even use the basic vector operations of
1426 // AND,OR,XOR, we will have to scalarize the op.
1427 // Notice that the operation may be 'promoted' which means that it is
1428 // 'bitcasted' to another type which is handled.
1429 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
1430 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
1431 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand)
1432 return DAG.UnrollVectorOp(Node);
1433
1434 // This operation also isn't safe with AND, OR, XOR when the boolean type is
1435 // 0/1 and the select operands aren't also booleans, as we need an all-ones
1436 // vector constant to mask with.
1437 // FIXME: Sign extend 1 to all ones if that's legal on the target.
1438 auto BoolContents = TLI.getBooleanContents(Op1.getValueType());
1439 if (BoolContents != TargetLowering::ZeroOrNegativeOneBooleanContent &&
1440 !(BoolContents == TargetLowering::ZeroOrOneBooleanContent &&
1441 Op1.getValueType().getVectorElementType() == MVT::i1))
1442 return DAG.UnrollVectorOp(Node);
1443
1444 // If the mask and the type are different sizes, unroll the vector op. This
1445 // can occur when getSetCCResultType returns something that is different in
1446 // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8.
1447 if (VT.getSizeInBits() != Op1.getValueSizeInBits())
1448 return DAG.UnrollVectorOp(Node);
1449
1450 // Bitcast the operands to be the same type as the mask.
1451 // This is needed when we select between FP types because
1452 // the mask is a vector of integers.
1453 Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
1454 Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
1455
1456 SDValue NotMask = DAG.getNOT(DL, Mask, VT);
1457
1458 Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
1459 Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
1460 SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
1461 return DAG.getNode(ISD::BITCAST, DL, Node->getValueType(0), Val);
1462}
1463
1464SDValue VectorLegalizer::ExpandVP_SELECT(SDNode *Node) {
1465 // Implement VP_SELECT in terms of VP_XOR, VP_AND and VP_OR on platforms which
1466 // do not support it natively.
1467 SDLoc DL(Node);
1468
1469 SDValue Mask = Node->getOperand(0);
1470 SDValue Op1 = Node->getOperand(1);
1471 SDValue Op2 = Node->getOperand(2);
1472 SDValue EVL = Node->getOperand(3);
1473
1474 EVT VT = Mask.getValueType();
1475
1476 // If we can't even use the basic vector operations of
1477 // VP_AND,VP_OR,VP_XOR, we will have to scalarize the op.
1478 if (TLI.getOperationAction(ISD::VP_AND, VT) == TargetLowering::Expand ||
1479 TLI.getOperationAction(ISD::VP_XOR, VT) == TargetLowering::Expand ||
1480 TLI.getOperationAction(ISD::VP_OR, VT) == TargetLowering::Expand)
1481 return DAG.UnrollVectorOp(Node);
1482
1483 // This operation also isn't safe when the operands aren't also booleans.
1484 if (Op1.getValueType().getVectorElementType() != MVT::i1)
1485 return DAG.UnrollVectorOp(Node);
1486
1487 SDValue Ones = DAG.getAllOnesConstant(DL, VT);
1488 SDValue NotMask = DAG.getNode(ISD::VP_XOR, DL, VT, Mask, Ones, Ones, EVL);
1489
1490 Op1 = DAG.getNode(ISD::VP_AND, DL, VT, Op1, Mask, Ones, EVL);
1491 Op2 = DAG.getNode(ISD::VP_AND, DL, VT, Op2, NotMask, Ones, EVL);
1492 return DAG.getNode(ISD::VP_OR, DL, VT, Op1, Op2, Ones, EVL);
1493}
1494
1495SDValue VectorLegalizer::ExpandVP_MERGE(SDNode *Node) {
1496 // Implement VP_MERGE in terms of VSELECT. Construct a mask where vector
1497 // indices less than the EVL/pivot are true. Combine that with the original
1498 // mask for a full-length mask. Use a full-length VSELECT to select between
1499 // the true and false values.
1500 SDLoc DL(Node);
1501
1502 SDValue Mask = Node->getOperand(0);
1503 SDValue Op1 = Node->getOperand(1);
1504 SDValue Op2 = Node->getOperand(2);
1505 SDValue EVL = Node->getOperand(3);
1506
1507 EVT MaskVT = Mask.getValueType();
1508 bool IsFixedLen = MaskVT.isFixedLengthVector();
1509
1510 EVT EVLVecVT = EVT::getVectorVT(*DAG.getContext(), EVL.getValueType(),
1511 MaskVT.getVectorElementCount());
1512
1513 // If we can't construct the EVL mask efficiently, it's better to unroll.
1514 if ((IsFixedLen &&
1515 !TLI.isOperationLegalOrCustom(ISD::BUILD_VECTOR, EVLVecVT)) ||
1516 (!IsFixedLen &&
1517 (!TLI.isOperationLegalOrCustom(ISD::STEP_VECTOR, EVLVecVT) ||
1518 !TLI.isOperationLegalOrCustom(ISD::SPLAT_VECTOR, EVLVecVT))))
1519 return DAG.UnrollVectorOp(Node);
1520
1521 // If using a SETCC would result in a different type than the mask type,
1522 // unroll.
1523 if (TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
1524 EVLVecVT) != MaskVT)
1525 return DAG.UnrollVectorOp(Node);
1526
1527 SDValue StepVec = DAG.getStepVector(DL, EVLVecVT);
1528 SDValue SplatEVL = DAG.getSplat(EVLVecVT, DL, EVL);
1529 SDValue EVLMask =
1530 DAG.getSetCC(DL, MaskVT, StepVec, SplatEVL, ISD::CondCode::SETULT);
1531
1532 SDValue FullMask = DAG.getNode(ISD::AND, DL, MaskVT, Mask, EVLMask);
1533 return DAG.getSelect(DL, Node->getValueType(0), FullMask, Op1, Op2);
1534}
1535
1536SDValue VectorLegalizer::ExpandVP_REM(SDNode *Node) {
1537 // Implement VP_SREM/UREM in terms of VP_SDIV/VP_UDIV, VP_MUL, VP_SUB.
1538 EVT VT = Node->getValueType(0);
1539
1540 unsigned DivOpc = Node->getOpcode() == ISD::VP_SREM ? ISD::VP_SDIV : ISD::VP_UDIV;
1541
1542 if (!TLI.isOperationLegalOrCustom(DivOpc, VT) ||
1543 !TLI.isOperationLegalOrCustom(ISD::VP_MUL, VT) ||
1544 !TLI.isOperationLegalOrCustom(ISD::VP_SUB, VT))
1545 return SDValue();
1546
1547 SDLoc DL(Node);
1548
1549 SDValue Dividend = Node->getOperand(0);
1550 SDValue Divisor = Node->getOperand(1);
1551 SDValue Mask = Node->getOperand(2);
1552 SDValue EVL = Node->getOperand(3);
1553
1554 // X % Y -> X-X/Y*Y
1555 SDValue Div = DAG.getNode(DivOpc, DL, VT, Dividend, Divisor, Mask, EVL);
1556 SDValue Mul = DAG.getNode(ISD::VP_MUL, DL, VT, Divisor, Div, Mask, EVL);
1557 return DAG.getNode(ISD::VP_SUB, DL, VT, Dividend, Mul, Mask, EVL);
1558}
1559
1560void VectorLegalizer::ExpandFP_TO_UINT(SDNode *Node,
1562 // Attempt to expand using TargetLowering.
1563 SDValue Result, Chain;
1564 if (TLI.expandFP_TO_UINT(Node, Result, Chain, DAG)) {
1565 Results.push_back(Result);
1566 if (Node->isStrictFPOpcode())
1567 Results.push_back(Chain);
1568 return;
1569 }
1570
1571 // Otherwise go ahead and unroll.
1572 if (Node->isStrictFPOpcode()) {
1573 UnrollStrictFPOp(Node, Results);
1574 return;
1575 }
1576
1577 Results.push_back(DAG.UnrollVectorOp(Node));
1578}
1579
1580void VectorLegalizer::ExpandUINT_TO_FLOAT(SDNode *Node,
1582 bool IsStrict = Node->isStrictFPOpcode();
1583 unsigned OpNo = IsStrict ? 1 : 0;
1584 SDValue Src = Node->getOperand(OpNo);
1585 EVT VT = Src.getValueType();
1586 SDLoc DL(Node);
1587
1588 // Attempt to expand using TargetLowering.
1590 SDValue Chain;
1591 if (TLI.expandUINT_TO_FP(Node, Result, Chain, DAG)) {
1592 Results.push_back(Result);
1593 if (IsStrict)
1594 Results.push_back(Chain);
1595 return;
1596 }
1597
1598 // Make sure that the SINT_TO_FP and SRL instructions are available.
1599 if (((!IsStrict && TLI.getOperationAction(ISD::SINT_TO_FP, VT) ==
1600 TargetLowering::Expand) ||
1601 (IsStrict && TLI.getOperationAction(ISD::STRICT_SINT_TO_FP, VT) ==
1602 TargetLowering::Expand)) ||
1603 TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand) {
1604 if (IsStrict) {
1605 UnrollStrictFPOp(Node, Results);
1606 return;
1607 }
1608
1609 Results.push_back(DAG.UnrollVectorOp(Node));
1610 return;
1611 }
1612
1613 unsigned BW = VT.getScalarSizeInBits();
1614 assert((BW == 64 || BW == 32) &&
1615 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
1616
1617 SDValue HalfWord = DAG.getConstant(BW / 2, DL, VT);
1618
1619 // Constants to clear the upper part of the word.
1620 // Notice that we can also use SHL+SHR, but using a constant is slightly
1621 // faster on x86.
1622 uint64_t HWMask = (BW == 64) ? 0x00000000FFFFFFFF : 0x0000FFFF;
1623 SDValue HalfWordMask = DAG.getConstant(HWMask, DL, VT);
1624
1625 // Two to the power of half-word-size.
1626 SDValue TWOHW =
1627 DAG.getConstantFP(1ULL << (BW / 2), DL, Node->getValueType(0));
1628
1629 // Clear upper part of LO, lower HI
1630 SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Src, HalfWord);
1631 SDValue LO = DAG.getNode(ISD::AND, DL, VT, Src, HalfWordMask);
1632
1633 if (IsStrict) {
1634 // Convert hi and lo to floats
1635 // Convert the hi part back to the upper values
1636 // TODO: Can any fast-math-flags be set on these nodes?
1638 {Node->getValueType(0), MVT::Other},
1639 {Node->getOperand(0), HI});
1640 fHI = DAG.getNode(ISD::STRICT_FMUL, DL, {Node->getValueType(0), MVT::Other},
1641 {fHI.getValue(1), fHI, TWOHW});
1643 {Node->getValueType(0), MVT::Other},
1644 {Node->getOperand(0), LO});
1645
1646 SDValue TF = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, fHI.getValue(1),
1647 fLO.getValue(1));
1648
1649 // Add the two halves
1650 SDValue Result =
1651 DAG.getNode(ISD::STRICT_FADD, DL, {Node->getValueType(0), MVT::Other},
1652 {TF, fHI, fLO});
1653
1654 Results.push_back(Result);
1655 Results.push_back(Result.getValue(1));
1656 return;
1657 }
1658
1659 // Convert hi and lo to floats
1660 // Convert the hi part back to the upper values
1661 // TODO: Can any fast-math-flags be set on these nodes?
1662 SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Node->getValueType(0), HI);
1663 fHI = DAG.getNode(ISD::FMUL, DL, Node->getValueType(0), fHI, TWOHW);
1664 SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Node->getValueType(0), LO);
1665
1666 // Add the two halves
1667 Results.push_back(
1668 DAG.getNode(ISD::FADD, DL, Node->getValueType(0), fHI, fLO));
1669}
1670
1671SDValue VectorLegalizer::ExpandFNEG(SDNode *Node) {
1672 if (TLI.isOperationLegalOrCustom(ISD::FSUB, Node->getValueType(0))) {
1673 SDLoc DL(Node);
1674 SDValue Zero = DAG.getConstantFP(-0.0, DL, Node->getValueType(0));
1675 // TODO: If FNEG had fast-math-flags, they'd get propagated to this FSUB.
1676 return DAG.getNode(ISD::FSUB, DL, Node->getValueType(0), Zero,
1677 Node->getOperand(0));
1678 }
1679 return DAG.UnrollVectorOp(Node);
1680}
1681
1682void VectorLegalizer::ExpandFSUB(SDNode *Node,
1684 // For floating-point values, (a-b) is the same as a+(-b). If FNEG is legal,
1685 // we can defer this to operation legalization where it will be lowered as
1686 // a+(-b).
1687 EVT VT = Node->getValueType(0);
1688 if (TLI.isOperationLegalOrCustom(ISD::FNEG, VT) &&
1689 TLI.isOperationLegalOrCustom(ISD::FADD, VT))
1690 return; // Defer to LegalizeDAG
1691
1692 SDValue Tmp = DAG.UnrollVectorOp(Node);
1693 Results.push_back(Tmp);
1694}
1695
1696void VectorLegalizer::ExpandSETCC(SDNode *Node,
1698 bool NeedInvert = false;
1699 bool IsVP = Node->getOpcode() == ISD::VP_SETCC;
1700 bool IsStrict = Node->getOpcode() == ISD::STRICT_FSETCC ||
1701 Node->getOpcode() == ISD::STRICT_FSETCCS;
1702 bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS;
1703 unsigned Offset = IsStrict ? 1 : 0;
1704
1705 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
1706 SDValue LHS = Node->getOperand(0 + Offset);
1707 SDValue RHS = Node->getOperand(1 + Offset);
1708 SDValue CC = Node->getOperand(2 + Offset);
1709
1710 MVT OpVT = LHS.getSimpleValueType();
1711 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1712
1713 if (TLI.getCondCodeAction(CCCode, OpVT) != TargetLowering::Expand) {
1714 if (IsStrict) {
1715 UnrollStrictFPOp(Node, Results);
1716 return;
1717 }
1718 Results.push_back(UnrollVSETCC(Node));
1719 return;
1720 }
1721
1722 SDValue Mask, EVL;
1723 if (IsVP) {
1724 Mask = Node->getOperand(3 + Offset);
1725 EVL = Node->getOperand(4 + Offset);
1726 }
1727
1728 SDLoc dl(Node);
1729 bool Legalized =
1730 TLI.LegalizeSetCCCondCode(DAG, Node->getValueType(0), LHS, RHS, CC, Mask,
1731 EVL, NeedInvert, dl, Chain, IsSignaling);
1732
1733 if (Legalized) {
1734 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
1735 // condition code, create a new SETCC node.
1736 if (CC.getNode()) {
1737 if (IsStrict) {
1738 LHS = DAG.getNode(Node->getOpcode(), dl, Node->getVTList(),
1739 {Chain, LHS, RHS, CC}, Node->getFlags());
1740 Chain = LHS.getValue(1);
1741 } else if (IsVP) {
1742 LHS = DAG.getNode(ISD::VP_SETCC, dl, Node->getValueType(0),
1743 {LHS, RHS, CC, Mask, EVL}, Node->getFlags());
1744 } else {
1745 LHS = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), LHS, RHS, CC,
1746 Node->getFlags());
1747 }
1748 }
1749
1750 // If we expanded the SETCC by inverting the condition code, then wrap
1751 // the existing SETCC in a NOT to restore the intended condition.
1752 if (NeedInvert) {
1753 if (!IsVP)
1754 LHS = DAG.getLogicalNOT(dl, LHS, LHS->getValueType(0));
1755 else
1756 LHS = DAG.getVPLogicalNOT(dl, LHS, Mask, EVL, LHS->getValueType(0));
1757 }
1758 } else {
1759 assert(!IsStrict && "Don't know how to expand for strict nodes.");
1760
1761 // Otherwise, SETCC for the given comparison type must be completely
1762 // illegal; expand it into a SELECT_CC.
1763 EVT VT = Node->getValueType(0);
1764 LHS =
1765 DAG.getNode(ISD::SELECT_CC, dl, VT, LHS, RHS,
1766 DAG.getBoolConstant(true, dl, VT, LHS.getValueType()),
1767 DAG.getBoolConstant(false, dl, VT, LHS.getValueType()), CC);
1768 LHS->setFlags(Node->getFlags());
1769 }
1770
1771 Results.push_back(LHS);
1772 if (IsStrict)
1773 Results.push_back(Chain);
1774}
1775
1776void VectorLegalizer::ExpandUADDSUBO(SDNode *Node,
1778 SDValue Result, Overflow;
1779 TLI.expandUADDSUBO(Node, Result, Overflow, DAG);
1780 Results.push_back(Result);
1781 Results.push_back(Overflow);
1782}
1783
1784void VectorLegalizer::ExpandSADDSUBO(SDNode *Node,
1786 SDValue Result, Overflow;
1787 TLI.expandSADDSUBO(Node, Result, Overflow, DAG);
1788 Results.push_back(Result);
1789 Results.push_back(Overflow);
1790}
1791
1792void VectorLegalizer::ExpandMULO(SDNode *Node,
1794 SDValue Result, Overflow;
1795 if (!TLI.expandMULO(Node, Result, Overflow, DAG))
1796 std::tie(Result, Overflow) = DAG.UnrollVectorOverflowOp(Node);
1797
1798 Results.push_back(Result);
1799 Results.push_back(Overflow);
1800}
1801
1802void VectorLegalizer::ExpandFixedPointDiv(SDNode *Node,
1804 SDNode *N = Node;
1805 if (SDValue Expanded = TLI.expandFixedPointDiv(N->getOpcode(), SDLoc(N),
1806 N->getOperand(0), N->getOperand(1), N->getConstantOperandVal(2), DAG))
1807 Results.push_back(Expanded);
1808}
1809
1810void VectorLegalizer::ExpandStrictFPOp(SDNode *Node,
1812 if (Node->getOpcode() == ISD::STRICT_UINT_TO_FP) {
1813 ExpandUINT_TO_FLOAT(Node, Results);
1814 return;
1815 }
1816 if (Node->getOpcode() == ISD::STRICT_FP_TO_UINT) {
1817 ExpandFP_TO_UINT(Node, Results);
1818 return;
1819 }
1820
1821 if (Node->getOpcode() == ISD::STRICT_FSETCC ||
1822 Node->getOpcode() == ISD::STRICT_FSETCCS) {
1823 ExpandSETCC(Node, Results);
1824 return;
1825 }
1826
1827 UnrollStrictFPOp(Node, Results);
1828}
1829
1830void VectorLegalizer::ExpandREM(SDNode *Node,
1832 assert((Node->getOpcode() == ISD::SREM || Node->getOpcode() == ISD::UREM) &&
1833 "Expected REM node");
1834
1836 if (!TLI.expandREM(Node, Result, DAG))
1837 Result = DAG.UnrollVectorOp(Node);
1838 Results.push_back(Result);
1839}
1840
1841// Try to expand libm nodes into vector math routine calls. Callers provide the
1842// LibFunc equivalent of the passed in Node, which is used to lookup mappings
1843// within TargetLibraryInfo. The only mappings considered are those where the
1844// result and all operands are the same vector type. While predicated nodes are
1845// not supported, we will emit calls to masked routines by passing in an all
1846// true mask.
1847bool VectorLegalizer::tryExpandVecMathCall(SDNode *Node, RTLIB::Libcall LC,
1849 // Chain must be propagated but currently strict fp operations are down
1850 // converted to their none strict counterpart.
1851 assert(!Node->isStrictFPOpcode() && "Unexpected strict fp operation!");
1852
1853 const char *LCName = TLI.getLibcallName(LC);
1854 if (!LCName)
1855 return false;
1856 LLVM_DEBUG(dbgs() << "Looking for vector variant of " << LCName << "\n");
1857
1858 EVT VT = Node->getValueType(0);
1860
1861 // Lookup a vector function equivalent to the specified libcall. Prefer
1862 // unmasked variants but we will generate a mask if need be.
1863 const TargetLibraryInfo &TLibInfo = DAG.getLibInfo();
1864 const VecDesc *VD = TLibInfo.getVectorMappingInfo(LCName, VL, false);
1865 if (!VD)
1866 VD = TLibInfo.getVectorMappingInfo(LCName, VL, /*Masked=*/true);
1867 if (!VD)
1868 return false;
1869
1870 LLVMContext *Ctx = DAG.getContext();
1871 Type *Ty = VT.getTypeForEVT(*Ctx);
1872 Type *ScalarTy = Ty->getScalarType();
1873
1874 // Construct a scalar function type based on Node's operands.
1876 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
1877 assert(Node->getOperand(i).getValueType() == VT &&
1878 "Expected matching vector types!");
1879 ArgTys.push_back(ScalarTy);
1880 }
1881 FunctionType *ScalarFTy = FunctionType::get(ScalarTy, ArgTys, false);
1882
1883 // Generate call information for the vector function.
1884 const std::string MangledName = VD->getVectorFunctionABIVariantString();
1885 auto OptVFInfo = VFABI::tryDemangleForVFABI(MangledName, ScalarFTy);
1886 if (!OptVFInfo)
1887 return false;
1888
1889 LLVM_DEBUG(dbgs() << "Found vector variant " << VD->getVectorFnName()
1890 << "\n");
1891
1892 // Sanity check just in case OptVFInfo has unexpected parameters.
1893 if (OptVFInfo->Shape.Parameters.size() !=
1894 Node->getNumOperands() + VD->isMasked())
1895 return false;
1896
1897 // Collect vector call operands.
1898
1899 SDLoc DL(Node);
1902 Entry.IsSExt = false;
1903 Entry.IsZExt = false;
1904
1905 unsigned OpNum = 0;
1906 for (auto &VFParam : OptVFInfo->Shape.Parameters) {
1907 if (VFParam.ParamKind == VFParamKind::GlobalPredicate) {
1908 EVT MaskVT = TLI.getSetCCResultType(DAG.getDataLayout(), *Ctx, VT);
1909 Entry.Node = DAG.getBoolConstant(true, DL, MaskVT, VT);
1910 Entry.Ty = MaskVT.getTypeForEVT(*Ctx);
1911 Args.push_back(Entry);
1912 continue;
1913 }
1914
1915 // Only vector operands are supported.
1916 if (VFParam.ParamKind != VFParamKind::Vector)
1917 return false;
1918
1919 Entry.Node = Node->getOperand(OpNum++);
1920 Entry.Ty = Ty;
1921 Args.push_back(Entry);
1922 }
1923
1924 // Emit a call to the vector function.
1925 SDValue Callee = DAG.getExternalSymbol(VD->getVectorFnName().data(),
1926 TLI.getPointerTy(DAG.getDataLayout()));
1928 CLI.setDebugLoc(DL)
1929 .setChain(DAG.getEntryNode())
1930 .setLibCallee(CallingConv::C, Ty, Callee, std::move(Args));
1931
1932 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
1933 Results.push_back(CallResult.first);
1934 return true;
1935}
1936
1937/// Try to expand the node to a vector libcall based on the result type.
1938bool VectorLegalizer::tryExpandVecMathCall(
1939 SDNode *Node, RTLIB::Libcall Call_F32, RTLIB::Libcall Call_F64,
1940 RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128,
1943 Node->getValueType(0).getVectorElementType(), Call_F32, Call_F64,
1944 Call_F80, Call_F128, Call_PPCF128);
1945
1946 if (LC == RTLIB::UNKNOWN_LIBCALL)
1947 return false;
1948
1949 return tryExpandVecMathCall(Node, LC, Results);
1950}
1951
1952void VectorLegalizer::UnrollStrictFPOp(SDNode *Node,
1954 EVT VT = Node->getValueType(0);
1955 EVT EltVT = VT.getVectorElementType();
1956 unsigned NumElems = VT.getVectorNumElements();
1957 unsigned NumOpers = Node->getNumOperands();
1958 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1959
1960 EVT TmpEltVT = EltVT;
1961 if (Node->getOpcode() == ISD::STRICT_FSETCC ||
1962 Node->getOpcode() == ISD::STRICT_FSETCCS)
1963 TmpEltVT = TLI.getSetCCResultType(DAG.getDataLayout(),
1964 *DAG.getContext(), TmpEltVT);
1965
1966 EVT ValueVTs[] = {TmpEltVT, MVT::Other};
1967 SDValue Chain = Node->getOperand(0);
1968 SDLoc dl(Node);
1969
1970 SmallVector<SDValue, 32> OpValues;
1971 SmallVector<SDValue, 32> OpChains;
1972 for (unsigned i = 0; i < NumElems; ++i) {
1974 SDValue Idx = DAG.getVectorIdxConstant(i, dl);
1975
1976 // The Chain is the first operand.
1977 Opers.push_back(Chain);
1978
1979 // Now process the remaining operands.
1980 for (unsigned j = 1; j < NumOpers; ++j) {
1981 SDValue Oper = Node->getOperand(j);
1982 EVT OperVT = Oper.getValueType();
1983
1984 if (OperVT.isVector())
1985 Oper = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
1986 OperVT.getVectorElementType(), Oper, Idx);
1987
1988 Opers.push_back(Oper);
1989 }
1990
1991 SDValue ScalarOp = DAG.getNode(Node->getOpcode(), dl, ValueVTs, Opers);
1992 SDValue ScalarResult = ScalarOp.getValue(0);
1993 SDValue ScalarChain = ScalarOp.getValue(1);
1994
1995 if (Node->getOpcode() == ISD::STRICT_FSETCC ||
1996 Node->getOpcode() == ISD::STRICT_FSETCCS)
1997 ScalarResult = DAG.getSelect(dl, EltVT, ScalarResult,
1998 DAG.getAllOnesConstant(dl, EltVT),
1999 DAG.getConstant(0, dl, EltVT));
2000
2001 OpValues.push_back(ScalarResult);
2002 OpChains.push_back(ScalarChain);
2003 }
2004
2005 SDValue Result = DAG.getBuildVector(VT, dl, OpValues);
2006 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OpChains);
2007
2008 Results.push_back(Result);
2009 Results.push_back(NewChain);
2010}
2011
2012SDValue VectorLegalizer::UnrollVSETCC(SDNode *Node) {
2013 EVT VT = Node->getValueType(0);
2014 unsigned NumElems = VT.getVectorNumElements();
2015 EVT EltVT = VT.getVectorElementType();
2016 SDValue LHS = Node->getOperand(0);
2017 SDValue RHS = Node->getOperand(1);
2018 SDValue CC = Node->getOperand(2);
2019 EVT TmpEltVT = LHS.getValueType().getVectorElementType();
2020 SDLoc dl(Node);
2021 SmallVector<SDValue, 8> Ops(NumElems);
2022 for (unsigned i = 0; i < NumElems; ++i) {
2023 SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
2024 DAG.getVectorIdxConstant(i, dl));
2025 SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
2026 DAG.getVectorIdxConstant(i, dl));
2027 Ops[i] = DAG.getNode(ISD::SETCC, dl,
2028 TLI.getSetCCResultType(DAG.getDataLayout(),
2029 *DAG.getContext(), TmpEltVT),
2030 LHSElem, RHSElem, CC);
2031 Ops[i] = DAG.getSelect(dl, EltVT, Ops[i], DAG.getAllOnesConstant(dl, EltVT),
2032 DAG.getConstant(0, dl, EltVT));
2033 }
2034 return DAG.getBuildVector(VT, dl, Ops);
2035}
2036
2038 return VectorLegalizer(*this).Run();
2039}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
BlockVerifier::State From
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseMap class.
static void createBSWAPShuffleMask(EVT VT, SmallVectorImpl< int > &ShuffleMask)
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This file describes how to lower LLVM code to machine code.
Value * RHS
Value * LHS
BinaryOperator * Mul
DEMANGLE_DUMP_METHOD void dump() const
This class represents an Operation in the Expression.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:211
size_t size() const
Definition: Function.h:856
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
This class is used to represent ISD::LOAD nodes.
Machine Value Type.
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
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.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
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...
Definition: SelectionDAG.h:226
bool LegalizeVectors()
This transforms the SelectionDAG into a SelectionDAG that only uses vector math operations supported ...
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:493
ilist< SDNode >::iterator allnodes_iterator
Definition: SelectionDAG.h:550
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void resize(size_type N)
Definition: SmallVector.h:651
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
This class is used to represent ISD::STORE nodes.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
Provides information about what library functions are available for the current target.
const VecDesc * getVectorMappingInfo(StringRef F, const ElementCount &VF, bool Masked) const
LegalizeAction
This enum indicates whether operations are valid for a target, and if not, what action should be used...
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
std::vector< ArgListEntry > ArgListTy
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:343
Provides info so a possible vectorization of a function can be computed.
bool isMasked() const
std::string getVectorFunctionABIVariantString() const
Returns a vector function ABI variant string on the form: ZGV<isa><mask><vlen><vparams><scalarname>(<...
StringRef getVectorFnName() const
#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.
Definition: BitmaskEnum.h:121
@ Entry
Definition: COFF.h:826
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition: ISDOpcodes.h:779
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:243
@ CTLZ_ZERO_UNDEF
Definition: ISDOpcodes.h:752
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:490
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
Definition: ISDOpcodes.h:1407
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1440
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition: ISDOpcodes.h:257
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:573
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:743
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition: ISDOpcodes.h:374
@ ADD
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:246
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1099
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition: ISDOpcodes.h:380
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:813
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:497
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:840
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1425
@ FADD
Simple binary floating point operators.
Definition: ISDOpcodes.h:397
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
Definition: ISDOpcodes.h:1429
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:716
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:870
@ SDIVREM
SDIVREM/UDIVREM - Divide two integers and produce both a quotient and remainder result.
Definition: ISDOpcodes.h:262
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1439
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:491
@ FPTRUNC_ROUND
FPTRUNC_ROUND - This corresponds to the fptrunc_round intrinsic.
Definition: ISDOpcodes.h:494
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:953
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:996
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition: ISDOpcodes.h:387
@ STRICT_FSQRT
Constrained versions of libm-equivalent floating point intrinsics.
Definition: ISDOpcodes.h:418
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:804
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition: ISDOpcodes.h:684
@ STRICT_UINT_TO_FP
Definition: ISDOpcodes.h:464
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1422
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:751
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1426
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:980
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:334
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition: ISDOpcodes.h:660
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:514
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition: ISDOpcodes.h:356
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:756
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1441
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition: ISDOpcodes.h:641
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition: ISDOpcodes.h:330
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
Definition: ISDOpcodes.h:1434
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:673
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:734
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
Definition: ISDOpcodes.h:1041
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:549
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:810
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:906
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:771
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:1028
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition: ISDOpcodes.h:366
@ SMULO
Same for multiplication.
Definition: ISDOpcodes.h:338
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition: ISDOpcodes.h:859
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:848
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:696
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition: ISDOpcodes.h:393
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:938
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:765
@ STRICT_SINT_TO_FP
STRICT_[US]INT_TO_FP - Convert a signed or unsigned integer to a floating point value.
Definition: ISDOpcodes.h:463
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
Definition: ISDOpcodes.h:1367
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1442
@ STRICT_FP_TO_UINT
Definition: ISDOpcodes.h:457
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition: ISDOpcodes.h:479
@ STRICT_FP_TO_SINT
STRICT_FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:456
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:1047
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:886
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:484
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:708
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition: ISDOpcodes.h:704
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition: ISDOpcodes.h:679
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1423
@ STRICT_FADD
Constrained versions of the binary floating point operators.
Definition: ISDOpcodes.h:407
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition: ISDOpcodes.h:52
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:1001
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:919
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition: ISDOpcodes.h:668
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:881
@ 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:905
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1430
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:816
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1408
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition: ISDOpcodes.h:61
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:507
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:347
@ AssertZext
Definition: ISDOpcodes.h:62
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition: ISDOpcodes.h:691
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition: ISDOpcodes.h:529
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,...
Definition: ISDOpcodes.h:1603
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
Definition: ISDOpcodes.h:1583
bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit.
Libcall getFPLibCall(EVT VT, Libcall Call_F32, Libcall Call_F64, Libcall Call_F80, Libcall Call_F128, Libcall Call_PPCF128)
GetFPLibCall - Helper to return the right libcall for the given floating point type,...
ManagedStatic< cl::opt< FnT >, OptCreatorT > Action
std::optional< VFInfo > tryDemangleForVFABI(StringRef MangledName, const FunctionType *FTy)
Function to construct a VFInfo out of a mangled names in the following format:
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
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:1729
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
#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:94
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:74
ElementCount getVectorElementCount() const
Definition: ValueTypes.h:341
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:371
bool isFixedLengthVector() const
Definition: ValueTypes.h:178
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:314
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:204
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition: ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:319
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:327
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:299
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
This structure contains all information that is necessary for lowering calls.