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