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