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