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