1 //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the SelectionDAG::Legalize method.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/CodeGen/Analysis.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineJumpTableInfo.h"
23 #include "llvm/IR/CallingConv.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DebugInfo.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/TargetFrameLowering.h"
35 #include "llvm/Target/TargetLowering.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Target/TargetSubtargetInfo.h"
38 using namespace llvm;
39 
40 #define DEBUG_TYPE "legalizedag"
41 
42 namespace {
43 
44 struct FloatSignAsInt;
45 
46 //===----------------------------------------------------------------------===//
47 /// This takes an arbitrary SelectionDAG as input and
48 /// hacks on it until the target machine can handle it.  This involves
49 /// eliminating value sizes the machine cannot handle (promoting small sizes to
50 /// large sizes or splitting up large values into small values) as well as
51 /// eliminating operations the machine cannot handle.
52 ///
53 /// This code also does a small amount of optimization and recognition of idioms
54 /// as part of its processing.  For example, if a target does not support a
55 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
56 /// will attempt merge setcc and brc instructions into brcc's.
57 ///
58 class SelectionDAGLegalize {
59   const TargetMachine &TM;
60   const TargetLowering &TLI;
61   SelectionDAG &DAG;
62 
63   /// \brief The set of nodes which have already been legalized. We hold a
64   /// reference to it in order to update as necessary on node deletion.
65   SmallPtrSetImpl<SDNode *> &LegalizedNodes;
66 
67   /// \brief A set of all the nodes updated during legalization.
68   SmallSetVector<SDNode *, 16> *UpdatedNodes;
69 
70   EVT getSetCCResultType(EVT VT) const {
71     return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
72   }
73 
74   // Libcall insertion helpers.
75 
76 public:
77   SelectionDAGLegalize(SelectionDAG &DAG,
78                        SmallPtrSetImpl<SDNode *> &LegalizedNodes,
79                        SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr)
80       : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG),
81         LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {}
82 
83   /// \brief Legalizes the given operation.
84   void LegalizeOp(SDNode *Node);
85 
86 private:
87   SDValue OptimizeFloatStore(StoreSDNode *ST);
88 
89   void LegalizeLoadOps(SDNode *Node);
90   void LegalizeStoreOps(SDNode *Node);
91 
92   /// Some targets cannot handle a variable
93   /// insertion index for the INSERT_VECTOR_ELT instruction.  In this case, it
94   /// is necessary to spill the vector being inserted into to memory, perform
95   /// the insert there, and then read the result back.
96   SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
97                                          SDValue Idx, SDLoc dl);
98   SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
99                                   SDValue Idx, SDLoc dl);
100 
101   /// Return a vector shuffle operation which
102   /// performs the same shuffe in terms of order or result bytes, but on a type
103   /// whose vector element type is narrower than the original shuffle type.
104   /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
105   SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, SDLoc dl,
106                                      SDValue N1, SDValue N2,
107                                      ArrayRef<int> Mask) const;
108 
109   bool LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
110                              bool &NeedInvert, SDLoc dl);
111 
112   SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
113   SDValue ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, const SDValue *Ops,
114                         unsigned NumOps, bool isSigned, SDLoc dl);
115 
116   std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
117                                                  SDNode *Node, bool isSigned);
118   SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
119                           RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
120                           RTLIB::Libcall Call_F128,
121                           RTLIB::Libcall Call_PPCF128);
122   SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
123                            RTLIB::Libcall Call_I8,
124                            RTLIB::Libcall Call_I16,
125                            RTLIB::Libcall Call_I32,
126                            RTLIB::Libcall Call_I64,
127                            RTLIB::Libcall Call_I128);
128   void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
129   void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
130 
131   SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, SDLoc dl);
132   SDValue ExpandBUILD_VECTOR(SDNode *Node);
133   SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
134   void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
135                                 SmallVectorImpl<SDValue> &Results);
136   void getSignAsIntValue(FloatSignAsInt &State, SDLoc DL, SDValue Value) const;
137   SDValue modifySignAsInt(const FloatSignAsInt &State, SDLoc DL,
138                           SDValue NewIntValue) const;
139   SDValue ExpandFCOPYSIGN(SDNode *Node) const;
140   SDValue ExpandFABS(SDNode *Node) const;
141   SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
142                                SDLoc dl);
143   SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
144                                 SDLoc dl);
145   SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
146                                 SDLoc dl);
147 
148   SDValue ExpandBITREVERSE(SDValue Op, SDLoc dl);
149   SDValue ExpandBSWAP(SDValue Op, SDLoc dl);
150   SDValue ExpandBitCount(unsigned Opc, SDValue Op, SDLoc dl);
151 
152   SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
153   SDValue ExpandInsertToVectorThroughStack(SDValue Op);
154   SDValue ExpandVectorBuildThroughStack(SDNode* Node);
155 
156   SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
157   SDValue ExpandConstant(ConstantSDNode *CP);
158 
159   // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall
160   bool ExpandNode(SDNode *Node);
161   void ConvertNodeToLibcall(SDNode *Node);
162   void PromoteNode(SDNode *Node);
163 
164 public:
165   // Node replacement helpers
166   void ReplacedNode(SDNode *N) {
167     LegalizedNodes.erase(N);
168     if (UpdatedNodes)
169       UpdatedNodes->insert(N);
170   }
171   void ReplaceNode(SDNode *Old, SDNode *New) {
172     DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
173           dbgs() << "     with:      "; New->dump(&DAG));
174 
175     assert(Old->getNumValues() == New->getNumValues() &&
176            "Replacing one node with another that produces a different number "
177            "of values!");
178     DAG.ReplaceAllUsesWith(Old, New);
179     for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i)
180       DAG.TransferDbgValues(SDValue(Old, i), SDValue(New, i));
181     if (UpdatedNodes)
182       UpdatedNodes->insert(New);
183     ReplacedNode(Old);
184   }
185   void ReplaceNode(SDValue Old, SDValue New) {
186     DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
187           dbgs() << "     with:      "; New->dump(&DAG));
188 
189     DAG.ReplaceAllUsesWith(Old, New);
190     DAG.TransferDbgValues(Old, New);
191     if (UpdatedNodes)
192       UpdatedNodes->insert(New.getNode());
193     ReplacedNode(Old.getNode());
194   }
195   void ReplaceNode(SDNode *Old, const SDValue *New) {
196     DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG));
197 
198     DAG.ReplaceAllUsesWith(Old, New);
199     for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) {
200       DEBUG(dbgs() << (i == 0 ? "     with:      "
201                               : "      and:      ");
202             New[i]->dump(&DAG));
203       DAG.TransferDbgValues(SDValue(Old, i), New[i]);
204       if (UpdatedNodes)
205         UpdatedNodes->insert(New[i].getNode());
206     }
207     ReplacedNode(Old);
208   }
209 };
210 }
211 
212 /// Return a vector shuffle operation which
213 /// performs the same shuffe in terms of order or result bytes, but on a type
214 /// whose vector element type is narrower than the original shuffle type.
215 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
216 SDValue
217 SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT,  SDLoc dl,
218                                                  SDValue N1, SDValue N2,
219                                                  ArrayRef<int> Mask) const {
220   unsigned NumMaskElts = VT.getVectorNumElements();
221   unsigned NumDestElts = NVT.getVectorNumElements();
222   unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
223 
224   assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
225 
226   if (NumEltsGrowth == 1)
227     return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
228 
229   SmallVector<int, 8> NewMask;
230   for (unsigned i = 0; i != NumMaskElts; ++i) {
231     int Idx = Mask[i];
232     for (unsigned j = 0; j != NumEltsGrowth; ++j) {
233       if (Idx < 0)
234         NewMask.push_back(-1);
235       else
236         NewMask.push_back(Idx * NumEltsGrowth + j);
237     }
238   }
239   assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
240   assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
241   return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
242 }
243 
244 /// Expands the ConstantFP node to an integer constant or
245 /// a load from the constant pool.
246 SDValue
247 SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
248   bool Extend = false;
249   SDLoc dl(CFP);
250 
251   // If a FP immediate is precise when represented as a float and if the
252   // target can do an extending load from float to double, we put it into
253   // the constant pool as a float, even if it's is statically typed as a
254   // double.  This shrinks FP constants and canonicalizes them for targets where
255   // an FP extending load is the same cost as a normal load (such as on the x87
256   // fp stack or PPC FP unit).
257   EVT VT = CFP->getValueType(0);
258   ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
259   if (!UseCP) {
260     assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
261     return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl,
262                            (VT == MVT::f64) ? MVT::i64 : MVT::i32);
263   }
264 
265   EVT OrigVT = VT;
266   EVT SVT = VT;
267   while (SVT != MVT::f32 && SVT != MVT::f16) {
268     SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
269     if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
270         // Only do this if the target has a native EXTLOAD instruction from
271         // smaller type.
272         TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) &&
273         TLI.ShouldShrinkFPConstant(OrigVT)) {
274       Type *SType = SVT.getTypeForEVT(*DAG.getContext());
275       LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
276       VT = SVT;
277       Extend = true;
278     }
279   }
280 
281   SDValue CPIdx =
282       DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout()));
283   unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
284   if (Extend) {
285     SDValue Result = DAG.getExtLoad(
286         ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx,
287         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT,
288         false, false, false, Alignment);
289     return Result;
290   }
291   SDValue Result =
292       DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
293                   MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
294                   false, false, false, Alignment);
295   return Result;
296 }
297 
298 /// Expands the Constant node to a load from the constant pool.
299 SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) {
300   SDLoc dl(CP);
301   EVT VT = CP->getValueType(0);
302   SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(),
303                                       TLI.getPointerTy(DAG.getDataLayout()));
304   unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
305   SDValue Result =
306     DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
307                 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
308                 false, false, false, Alignment);
309   return Result;
310 }
311 
312 /// Expands an unaligned store to 2 half-size stores.
313 static void ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
314                                  const TargetLowering &TLI,
315                                  SelectionDAGLegalize *DAGLegalize) {
316   assert(ST->getAddressingMode() == ISD::UNINDEXED &&
317          "unaligned indexed stores not implemented!");
318   SDValue Chain = ST->getChain();
319   SDValue Ptr = ST->getBasePtr();
320   SDValue Val = ST->getValue();
321   EVT VT = Val.getValueType();
322   int Alignment = ST->getAlignment();
323 
324   SDLoc dl(ST);
325   if (ST->getMemoryVT().isFloatingPoint() ||
326       ST->getMemoryVT().isVector()) {
327     EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
328     if (TLI.isTypeLegal(intVT)) {
329       // Expand to a bitconvert of the value to the integer type of the
330       // same size, then a (misaligned) int store.
331       // FIXME: Does not handle truncating floating point stores!
332       SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
333       Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
334                            ST->isVolatile(), ST->isNonTemporal(), Alignment);
335       DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
336       return;
337     }
338     // Do a (aligned) store to a stack slot, then copy from the stack slot
339     // to the final destination using (unaligned) integer loads and stores.
340     EVT StoredVT = ST->getMemoryVT();
341     MVT RegVT =
342       TLI.getRegisterType(*DAG.getContext(),
343                           EVT::getIntegerVT(*DAG.getContext(),
344                                             StoredVT.getSizeInBits()));
345     EVT PtrVT = Ptr.getValueType();
346     unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
347     unsigned RegBytes = RegVT.getSizeInBits() / 8;
348     unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
349 
350     // Make sure the stack slot is also aligned for the register type.
351     SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
352 
353     // Perform the original store, only redirected to the stack slot.
354     SDValue Store = DAG.getTruncStore(Chain, dl,
355                                       Val, StackPtr, MachinePointerInfo(),
356                                       StoredVT, false, false, 0);
357 
358     EVT StackPtrVT = StackPtr.getValueType();
359 
360     SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT);
361     SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT);
362     SmallVector<SDValue, 8> Stores;
363     unsigned Offset = 0;
364 
365     // Do all but one copies using the full register width.
366     for (unsigned i = 1; i < NumRegs; i++) {
367       // Load one integer register's worth from the stack slot.
368       SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
369                                  MachinePointerInfo(),
370                                  false, false, false, 0);
371       // Store it to the final location.  Remember the store.
372       Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
373                                   ST->getPointerInfo().getWithOffset(Offset),
374                                     ST->isVolatile(), ST->isNonTemporal(),
375                                     MinAlign(ST->getAlignment(), Offset)));
376       // Increment the pointers.
377       Offset += RegBytes;
378       StackPtr = DAG.getNode(ISD::ADD, dl, StackPtrVT,
379                              StackPtr, StackPtrIncrement);
380       Ptr = DAG.getNode(ISD::ADD, dl, PtrVT, Ptr, PtrIncrement);
381     }
382 
383     // The last store may be partial.  Do a truncating store.  On big-endian
384     // machines this requires an extending load from the stack slot to ensure
385     // that the bits are in the right place.
386     EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
387                                   8 * (StoredBytes - Offset));
388 
389     // Load from the stack slot.
390     SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
391                                   MachinePointerInfo(),
392                                   MemVT, false, false, false, 0);
393 
394     Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
395                                        ST->getPointerInfo()
396                                          .getWithOffset(Offset),
397                                        MemVT, ST->isVolatile(),
398                                        ST->isNonTemporal(),
399                                        MinAlign(ST->getAlignment(), Offset),
400                                        ST->getAAInfo()));
401     // The order of the stores doesn't matter - say it with a TokenFactor.
402     SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
403     DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
404     return;
405   }
406   assert(ST->getMemoryVT().isInteger() &&
407          !ST->getMemoryVT().isVector() &&
408          "Unaligned store of unknown type.");
409   // Get the half-size VT
410   EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
411   int NumBits = NewStoredVT.getSizeInBits();
412   int IncrementSize = NumBits / 8;
413 
414   // Divide the stored value in two parts.
415   SDValue ShiftAmount =
416       DAG.getConstant(NumBits, dl, TLI.getShiftAmountTy(Val.getValueType(),
417                                                         DAG.getDataLayout()));
418   SDValue Lo = Val;
419   SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
420 
421   // Store the two parts
422   SDValue Store1, Store2;
423   Store1 = DAG.getTruncStore(Chain, dl,
424                              DAG.getDataLayout().isLittleEndian() ? Lo : Hi,
425                              Ptr, ST->getPointerInfo(), NewStoredVT,
426                              ST->isVolatile(), ST->isNonTemporal(), Alignment);
427 
428   EVT PtrVT = Ptr.getValueType();
429   Ptr = DAG.getNode(ISD::ADD, dl, PtrVT, Ptr,
430                     DAG.getConstant(IncrementSize, dl, PtrVT));
431   Alignment = MinAlign(Alignment, IncrementSize);
432   Store2 = DAG.getTruncStore(
433       Chain, dl, DAG.getDataLayout().isLittleEndian() ? Hi : Lo, Ptr,
434       ST->getPointerInfo().getWithOffset(IncrementSize), NewStoredVT,
435       ST->isVolatile(), ST->isNonTemporal(), Alignment, ST->getAAInfo());
436 
437   SDValue Result =
438     DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
439   DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
440 }
441 
442 /// Expands an unaligned load to 2 half-size loads.
443 static void
444 ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
445                     const TargetLowering &TLI,
446                     SDValue &ValResult, SDValue &ChainResult) {
447   assert(LD->getAddressingMode() == ISD::UNINDEXED &&
448          "unaligned indexed loads not implemented!");
449   SDValue Chain = LD->getChain();
450   SDValue Ptr = LD->getBasePtr();
451   EVT VT = LD->getValueType(0);
452   EVT LoadedVT = LD->getMemoryVT();
453   SDLoc dl(LD);
454   if (VT.isFloatingPoint() || VT.isVector()) {
455     EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
456     if (TLI.isTypeLegal(intVT) && TLI.isTypeLegal(LoadedVT)) {
457       // Expand to a (misaligned) integer load of the same size,
458       // then bitconvert to floating point or vector.
459       SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr,
460                                     LD->getMemOperand());
461       SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
462       if (LoadedVT != VT)
463         Result = DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND :
464                              ISD::ANY_EXTEND, dl, VT, Result);
465 
466       ValResult = Result;
467       ChainResult = newLoad.getValue(1);
468       return;
469     }
470 
471     // Copy the value to a (aligned) stack slot using (unaligned) integer
472     // loads and stores, then do a (aligned) load from the stack slot.
473     MVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
474     unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
475     unsigned RegBytes = RegVT.getSizeInBits() / 8;
476     unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
477 
478     // Make sure the stack slot is also aligned for the register type.
479     SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
480 
481     SmallVector<SDValue, 8> Stores;
482     SDValue StackPtr = StackBase;
483     unsigned Offset = 0;
484 
485     EVT PtrVT = Ptr.getValueType();
486     EVT StackPtrVT = StackPtr.getValueType();
487 
488     SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT);
489     SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT);
490 
491     // Do all but one copies using the full register width.
492     for (unsigned i = 1; i < NumRegs; i++) {
493       // Load one integer register's worth from the original location.
494       SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
495                                  LD->getPointerInfo().getWithOffset(Offset),
496                                  LD->isVolatile(), LD->isNonTemporal(),
497                                  LD->isInvariant(),
498                                  MinAlign(LD->getAlignment(), Offset),
499                                  LD->getAAInfo());
500       // Follow the load with a store to the stack slot.  Remember the store.
501       Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
502                                     MachinePointerInfo(), false, false, 0));
503       // Increment the pointers.
504       Offset += RegBytes;
505       Ptr = DAG.getNode(ISD::ADD, dl, PtrVT, Ptr, PtrIncrement);
506       StackPtr = DAG.getNode(ISD::ADD, dl, StackPtrVT, StackPtr,
507                              StackPtrIncrement);
508     }
509 
510     // The last copy may be partial.  Do an extending load.
511     EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
512                                   8 * (LoadedBytes - Offset));
513     SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
514                                   LD->getPointerInfo().getWithOffset(Offset),
515                                   MemVT, LD->isVolatile(),
516                                   LD->isNonTemporal(),
517                                   LD->isInvariant(),
518                                   MinAlign(LD->getAlignment(), Offset),
519                                   LD->getAAInfo());
520     // Follow the load with a store to the stack slot.  Remember the store.
521     // On big-endian machines this requires a truncating store to ensure
522     // that the bits end up in the right place.
523     Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
524                                        MachinePointerInfo(), MemVT,
525                                        false, false, 0));
526 
527     // The order of the stores doesn't matter - say it with a TokenFactor.
528     SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
529 
530     // Finally, perform the original load only redirected to the stack slot.
531     Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
532                           MachinePointerInfo(), LoadedVT, false,false, false,
533                           0);
534 
535     // Callers expect a MERGE_VALUES node.
536     ValResult = Load;
537     ChainResult = TF;
538     return;
539   }
540   assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
541          "Unaligned load of unsupported type.");
542 
543   // Compute the new VT that is half the size of the old one.  This is an
544   // integer MVT.
545   unsigned NumBits = LoadedVT.getSizeInBits();
546   EVT NewLoadedVT;
547   NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
548   NumBits >>= 1;
549 
550   unsigned Alignment = LD->getAlignment();
551   unsigned IncrementSize = NumBits / 8;
552   ISD::LoadExtType HiExtType = LD->getExtensionType();
553 
554   // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
555   if (HiExtType == ISD::NON_EXTLOAD)
556     HiExtType = ISD::ZEXTLOAD;
557 
558   // Load the value in two parts
559   SDValue Lo, Hi;
560   if (DAG.getDataLayout().isLittleEndian()) {
561     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
562                         NewLoadedVT, LD->isVolatile(),
563                         LD->isNonTemporal(), LD->isInvariant(), Alignment,
564                         LD->getAAInfo());
565     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
566                       DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
567     Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
568                         LD->getPointerInfo().getWithOffset(IncrementSize),
569                         NewLoadedVT, LD->isVolatile(),
570                         LD->isNonTemporal(),LD->isInvariant(),
571                         MinAlign(Alignment, IncrementSize), LD->getAAInfo());
572   } else {
573     Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
574                         NewLoadedVT, LD->isVolatile(),
575                         LD->isNonTemporal(), LD->isInvariant(), Alignment,
576                         LD->getAAInfo());
577     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
578                       DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
579     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
580                         LD->getPointerInfo().getWithOffset(IncrementSize),
581                         NewLoadedVT, LD->isVolatile(),
582                         LD->isNonTemporal(), LD->isInvariant(),
583                         MinAlign(Alignment, IncrementSize), LD->getAAInfo());
584   }
585 
586   // aggregate the two parts
587   SDValue ShiftAmount =
588       DAG.getConstant(NumBits, dl, TLI.getShiftAmountTy(Hi.getValueType(),
589                                                         DAG.getDataLayout()));
590   SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
591   Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
592 
593   SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
594                              Hi.getValue(1));
595 
596   ValResult = Result;
597   ChainResult = TF;
598 }
599 
600 /// Some target cannot handle a variable insertion index for the
601 /// INSERT_VECTOR_ELT instruction.  In this case, it
602 /// is necessary to spill the vector being inserted into to memory, perform
603 /// the insert there, and then read the result back.
604 SDValue SelectionDAGLegalize::
605 PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
606                                SDLoc dl) {
607   SDValue Tmp1 = Vec;
608   SDValue Tmp2 = Val;
609   SDValue Tmp3 = Idx;
610 
611   // If the target doesn't support this, we have to spill the input vector
612   // to a temporary stack slot, update the element, then reload it.  This is
613   // badness.  We could also load the value into a vector register (either
614   // with a "move to register" or "extload into register" instruction, then
615   // permute it into place, if the idx is a constant and if the idx is
616   // supported by the target.
617   EVT VT    = Tmp1.getValueType();
618   EVT EltVT = VT.getVectorElementType();
619   EVT IdxVT = Tmp3.getValueType();
620   EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
621   SDValue StackPtr = DAG.CreateStackTemporary(VT);
622 
623   int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
624 
625   // Store the vector.
626   SDValue Ch = DAG.getStore(
627       DAG.getEntryNode(), dl, Tmp1, StackPtr,
628       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), false,
629       false, 0);
630 
631   // Truncate or zero extend offset to target pointer type.
632   Tmp3 = DAG.getZExtOrTrunc(Tmp3, dl, PtrVT);
633   // Add the offset to the index.
634   unsigned EltSize = EltVT.getSizeInBits()/8;
635   Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,
636                      DAG.getConstant(EltSize, dl, IdxVT));
637   SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
638   // Store the scalar value.
639   Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
640                          false, false, 0);
641   // Load the updated vector.
642   return DAG.getLoad(VT, dl, Ch, StackPtr, MachinePointerInfo::getFixedStack(
643                                                DAG.getMachineFunction(), SPFI),
644                      false, false, false, 0);
645 }
646 
647 
648 SDValue SelectionDAGLegalize::
649 ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, SDLoc dl) {
650   if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
651     // SCALAR_TO_VECTOR requires that the type of the value being inserted
652     // match the element type of the vector being created, except for
653     // integers in which case the inserted value can be over width.
654     EVT EltVT = Vec.getValueType().getVectorElementType();
655     if (Val.getValueType() == EltVT ||
656         (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
657       SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
658                                   Vec.getValueType(), Val);
659 
660       unsigned NumElts = Vec.getValueType().getVectorNumElements();
661       // We generate a shuffle of InVec and ScVec, so the shuffle mask
662       // should be 0,1,2,3,4,5... with the appropriate element replaced with
663       // elt 0 of the RHS.
664       SmallVector<int, 8> ShufOps;
665       for (unsigned i = 0; i != NumElts; ++i)
666         ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
667 
668       return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
669                                   &ShufOps[0]);
670     }
671   }
672   return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
673 }
674 
675 SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
676   // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
677   // FIXME: We shouldn't do this for TargetConstantFP's.
678   // FIXME: move this to the DAG Combiner!  Note that we can't regress due
679   // to phase ordering between legalized code and the dag combiner.  This
680   // probably means that we need to integrate dag combiner and legalizer
681   // together.
682   // We generally can't do this one for long doubles.
683   SDValue Chain = ST->getChain();
684   SDValue Ptr = ST->getBasePtr();
685   unsigned Alignment = ST->getAlignment();
686   bool isVolatile = ST->isVolatile();
687   bool isNonTemporal = ST->isNonTemporal();
688   AAMDNodes AAInfo = ST->getAAInfo();
689   SDLoc dl(ST);
690   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
691     if (CFP->getValueType(0) == MVT::f32 &&
692         TLI.isTypeLegal(MVT::i32)) {
693       SDValue Con = DAG.getConstant(CFP->getValueAPF().
694                                       bitcastToAPInt().zextOrTrunc(32),
695                                     SDLoc(CFP), MVT::i32);
696       return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
697                           isVolatile, isNonTemporal, Alignment, AAInfo);
698     }
699 
700     if (CFP->getValueType(0) == MVT::f64) {
701       // If this target supports 64-bit registers, do a single 64-bit store.
702       if (TLI.isTypeLegal(MVT::i64)) {
703         SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
704                                       zextOrTrunc(64), SDLoc(CFP), MVT::i64);
705         return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
706                             isVolatile, isNonTemporal, Alignment, AAInfo);
707       }
708 
709       if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
710         // Otherwise, if the target supports 32-bit registers, use 2 32-bit
711         // stores.  If the target supports neither 32- nor 64-bits, this
712         // xform is certainly not worth it.
713         const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt();
714         SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32);
715         SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32);
716         if (DAG.getDataLayout().isBigEndian())
717           std::swap(Lo, Hi);
718 
719         Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(), isVolatile,
720                           isNonTemporal, Alignment, AAInfo);
721         Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
722                           DAG.getConstant(4, dl, Ptr.getValueType()));
723         Hi = DAG.getStore(Chain, dl, Hi, Ptr,
724                           ST->getPointerInfo().getWithOffset(4),
725                           isVolatile, isNonTemporal, MinAlign(Alignment, 4U),
726                           AAInfo);
727 
728         return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
729       }
730     }
731   }
732   return SDValue(nullptr, 0);
733 }
734 
735 void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
736     StoreSDNode *ST = cast<StoreSDNode>(Node);
737     SDValue Chain = ST->getChain();
738     SDValue Ptr = ST->getBasePtr();
739     SDLoc dl(Node);
740 
741     unsigned Alignment = ST->getAlignment();
742     bool isVolatile = ST->isVolatile();
743     bool isNonTemporal = ST->isNonTemporal();
744     AAMDNodes AAInfo = ST->getAAInfo();
745 
746     if (!ST->isTruncatingStore()) {
747       if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
748         ReplaceNode(ST, OptStore);
749         return;
750       }
751 
752       {
753         SDValue Value = ST->getValue();
754         MVT VT = Value.getSimpleValueType();
755         switch (TLI.getOperationAction(ISD::STORE, VT)) {
756         default: llvm_unreachable("This action is not supported yet!");
757         case TargetLowering::Legal: {
758           // If this is an unaligned store and the target doesn't support it,
759           // expand it.
760           EVT MemVT = ST->getMemoryVT();
761           unsigned AS = ST->getAddressSpace();
762           unsigned Align = ST->getAlignment();
763           const DataLayout &DL = DAG.getDataLayout();
764           if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align))
765             ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this);
766           break;
767         }
768         case TargetLowering::Custom: {
769           SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
770           if (Res && Res != SDValue(Node, 0))
771             ReplaceNode(SDValue(Node, 0), Res);
772           return;
773         }
774         case TargetLowering::Promote: {
775           MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT);
776           assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
777                  "Can only promote stores to same size type");
778           Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value);
779           SDValue Result =
780             DAG.getStore(Chain, dl, Value, Ptr,
781                          ST->getPointerInfo(), isVolatile,
782                          isNonTemporal, Alignment, AAInfo);
783           ReplaceNode(SDValue(Node, 0), Result);
784           break;
785         }
786         }
787         return;
788       }
789     } else {
790       SDValue Value = ST->getValue();
791 
792       EVT StVT = ST->getMemoryVT();
793       unsigned StWidth = StVT.getSizeInBits();
794       auto &DL = DAG.getDataLayout();
795 
796       if (StWidth != StVT.getStoreSizeInBits()) {
797         // Promote to a byte-sized store with upper bits zero if not
798         // storing an integral number of bytes.  For example, promote
799         // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
800         EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
801                                     StVT.getStoreSizeInBits());
802         Value = DAG.getZeroExtendInReg(Value, dl, StVT);
803         SDValue Result =
804           DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
805                             NVT, isVolatile, isNonTemporal, Alignment, AAInfo);
806         ReplaceNode(SDValue(Node, 0), Result);
807       } else if (StWidth & (StWidth - 1)) {
808         // If not storing a power-of-2 number of bits, expand as two stores.
809         assert(!StVT.isVector() && "Unsupported truncstore!");
810         unsigned RoundWidth = 1 << Log2_32(StWidth);
811         assert(RoundWidth < StWidth);
812         unsigned ExtraWidth = StWidth - RoundWidth;
813         assert(ExtraWidth < RoundWidth);
814         assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
815                "Store size not an integral number of bytes!");
816         EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
817         EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
818         SDValue Lo, Hi;
819         unsigned IncrementSize;
820 
821         if (DL.isLittleEndian()) {
822           // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
823           // Store the bottom RoundWidth bits.
824           Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
825                                  RoundVT,
826                                  isVolatile, isNonTemporal, Alignment,
827                                  AAInfo);
828 
829           // Store the remaining ExtraWidth bits.
830           IncrementSize = RoundWidth / 8;
831           Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
832                             DAG.getConstant(IncrementSize, dl,
833                                             Ptr.getValueType()));
834           Hi = DAG.getNode(
835               ISD::SRL, dl, Value.getValueType(), Value,
836               DAG.getConstant(RoundWidth, dl,
837                               TLI.getShiftAmountTy(Value.getValueType(), DL)));
838           Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr,
839                              ST->getPointerInfo().getWithOffset(IncrementSize),
840                                  ExtraVT, isVolatile, isNonTemporal,
841                                  MinAlign(Alignment, IncrementSize), AAInfo);
842         } else {
843           // Big endian - avoid unaligned stores.
844           // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
845           // Store the top RoundWidth bits.
846           Hi = DAG.getNode(
847               ISD::SRL, dl, Value.getValueType(), Value,
848               DAG.getConstant(ExtraWidth, dl,
849                               TLI.getShiftAmountTy(Value.getValueType(), DL)));
850           Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(),
851                                  RoundVT, isVolatile, isNonTemporal, Alignment,
852                                  AAInfo);
853 
854           // Store the remaining ExtraWidth bits.
855           IncrementSize = RoundWidth / 8;
856           Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
857                             DAG.getConstant(IncrementSize, dl,
858                                             Ptr.getValueType()));
859           Lo = DAG.getTruncStore(Chain, dl, Value, Ptr,
860                               ST->getPointerInfo().getWithOffset(IncrementSize),
861                                  ExtraVT, isVolatile, isNonTemporal,
862                                  MinAlign(Alignment, IncrementSize), AAInfo);
863         }
864 
865         // The order of the stores doesn't matter.
866         SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
867         ReplaceNode(SDValue(Node, 0), Result);
868       } else {
869         switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
870         default: llvm_unreachable("This action is not supported yet!");
871         case TargetLowering::Legal: {
872           EVT MemVT = ST->getMemoryVT();
873           unsigned AS = ST->getAddressSpace();
874           unsigned Align = ST->getAlignment();
875           // If this is an unaligned store and the target doesn't support it,
876           // expand it.
877           if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align))
878             ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this);
879           break;
880         }
881         case TargetLowering::Custom: {
882           SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
883           if (Res && Res != SDValue(Node, 0))
884             ReplaceNode(SDValue(Node, 0), Res);
885           return;
886         }
887         case TargetLowering::Expand:
888           assert(!StVT.isVector() &&
889                  "Vector Stores are handled in LegalizeVectorOps");
890 
891           // TRUNCSTORE:i16 i32 -> STORE i16
892           assert(TLI.isTypeLegal(StVT) &&
893                  "Do not know how to expand this store!");
894           Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value);
895           SDValue Result =
896             DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
897                          isVolatile, isNonTemporal, Alignment, AAInfo);
898           ReplaceNode(SDValue(Node, 0), Result);
899           break;
900         }
901       }
902     }
903 }
904 
905 void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
906   LoadSDNode *LD = cast<LoadSDNode>(Node);
907   SDValue Chain = LD->getChain();  // The chain.
908   SDValue Ptr = LD->getBasePtr();  // The base pointer.
909   SDValue Value;                   // The value returned by the load op.
910   SDLoc dl(Node);
911 
912   ISD::LoadExtType ExtType = LD->getExtensionType();
913   if (ExtType == ISD::NON_EXTLOAD) {
914     MVT VT = Node->getSimpleValueType(0);
915     SDValue RVal = SDValue(Node, 0);
916     SDValue RChain = SDValue(Node, 1);
917 
918     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
919     default: llvm_unreachable("This action is not supported yet!");
920     case TargetLowering::Legal: {
921       EVT MemVT = LD->getMemoryVT();
922       unsigned AS = LD->getAddressSpace();
923       unsigned Align = LD->getAlignment();
924       const DataLayout &DL = DAG.getDataLayout();
925       // If this is an unaligned load and the target doesn't support it,
926       // expand it.
927       if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align))
928         ExpandUnalignedLoad(cast<LoadSDNode>(Node), DAG, TLI, RVal, RChain);
929       break;
930     }
931     case TargetLowering::Custom: {
932       if (SDValue Res = TLI.LowerOperation(RVal, DAG)) {
933         RVal = Res;
934         RChain = Res.getValue(1);
935       }
936       break;
937     }
938     case TargetLowering::Promote: {
939       MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
940       assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
941              "Can only promote loads to same size type");
942 
943       SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand());
944       RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res);
945       RChain = Res.getValue(1);
946       break;
947     }
948     }
949     if (RChain.getNode() != Node) {
950       assert(RVal.getNode() != Node && "Load must be completely replaced");
951       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal);
952       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
953       if (UpdatedNodes) {
954         UpdatedNodes->insert(RVal.getNode());
955         UpdatedNodes->insert(RChain.getNode());
956       }
957       ReplacedNode(Node);
958     }
959     return;
960   }
961 
962   EVT SrcVT = LD->getMemoryVT();
963   unsigned SrcWidth = SrcVT.getSizeInBits();
964   unsigned Alignment = LD->getAlignment();
965   bool isVolatile = LD->isVolatile();
966   bool isNonTemporal = LD->isNonTemporal();
967   bool isInvariant = LD->isInvariant();
968   AAMDNodes AAInfo = LD->getAAInfo();
969 
970   if (SrcWidth != SrcVT.getStoreSizeInBits() &&
971       // Some targets pretend to have an i1 loading operation, and actually
972       // load an i8.  This trick is correct for ZEXTLOAD because the top 7
973       // bits are guaranteed to be zero; it helps the optimizers understand
974       // that these bits are zero.  It is also useful for EXTLOAD, since it
975       // tells the optimizers that those bits are undefined.  It would be
976       // nice to have an effective generic way of getting these benefits...
977       // Until such a way is found, don't insist on promoting i1 here.
978       (SrcVT != MVT::i1 ||
979        TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) ==
980          TargetLowering::Promote)) {
981     // Promote to a byte-sized load if not loading an integral number of
982     // bytes.  For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
983     unsigned NewWidth = SrcVT.getStoreSizeInBits();
984     EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
985     SDValue Ch;
986 
987     // The extra bits are guaranteed to be zero, since we stored them that
988     // way.  A zext load from NVT thus automatically gives zext from SrcVT.
989 
990     ISD::LoadExtType NewExtType =
991       ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
992 
993     SDValue Result =
994       DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
995                      Chain, Ptr, LD->getPointerInfo(),
996                      NVT, isVolatile, isNonTemporal, isInvariant, Alignment,
997                      AAInfo);
998 
999     Ch = Result.getValue(1); // The chain.
1000 
1001     if (ExtType == ISD::SEXTLOAD)
1002       // Having the top bits zero doesn't help when sign extending.
1003       Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1004                            Result.getValueType(),
1005                            Result, DAG.getValueType(SrcVT));
1006     else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1007       // All the top bits are guaranteed to be zero - inform the optimizers.
1008       Result = DAG.getNode(ISD::AssertZext, dl,
1009                            Result.getValueType(), Result,
1010                            DAG.getValueType(SrcVT));
1011 
1012     Value = Result;
1013     Chain = Ch;
1014   } else if (SrcWidth & (SrcWidth - 1)) {
1015     // If not loading a power-of-2 number of bits, expand as two loads.
1016     assert(!SrcVT.isVector() && "Unsupported extload!");
1017     unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1018     assert(RoundWidth < SrcWidth);
1019     unsigned ExtraWidth = SrcWidth - RoundWidth;
1020     assert(ExtraWidth < RoundWidth);
1021     assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1022            "Load size not an integral number of bytes!");
1023     EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1024     EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1025     SDValue Lo, Hi, Ch;
1026     unsigned IncrementSize;
1027     auto &DL = DAG.getDataLayout();
1028 
1029     if (DL.isLittleEndian()) {
1030       // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1031       // Load the bottom RoundWidth bits.
1032       Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
1033                           Chain, Ptr,
1034                           LD->getPointerInfo(), RoundVT, isVolatile,
1035                           isNonTemporal, isInvariant, Alignment, AAInfo);
1036 
1037       // Load the remaining ExtraWidth bits.
1038       IncrementSize = RoundWidth / 8;
1039       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
1040                          DAG.getConstant(IncrementSize, dl,
1041                                          Ptr.getValueType()));
1042       Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
1043                           LD->getPointerInfo().getWithOffset(IncrementSize),
1044                           ExtraVT, isVolatile, isNonTemporal, isInvariant,
1045                           MinAlign(Alignment, IncrementSize), AAInfo);
1046 
1047       // Build a factor node to remember that this load is independent of
1048       // the other one.
1049       Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1050                        Hi.getValue(1));
1051 
1052       // Move the top bits to the right place.
1053       Hi = DAG.getNode(
1054           ISD::SHL, dl, Hi.getValueType(), Hi,
1055           DAG.getConstant(RoundWidth, dl,
1056                           TLI.getShiftAmountTy(Hi.getValueType(), DL)));
1057 
1058       // Join the hi and lo parts.
1059       Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
1060     } else {
1061       // Big endian - avoid unaligned loads.
1062       // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1063       // Load the top RoundWidth bits.
1064       Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
1065                           LD->getPointerInfo(), RoundVT, isVolatile,
1066                           isNonTemporal, isInvariant, Alignment, AAInfo);
1067 
1068       // Load the remaining ExtraWidth bits.
1069       IncrementSize = RoundWidth / 8;
1070       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
1071                          DAG.getConstant(IncrementSize, dl,
1072                                          Ptr.getValueType()));
1073       Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
1074                           dl, Node->getValueType(0), Chain, Ptr,
1075                           LD->getPointerInfo().getWithOffset(IncrementSize),
1076                           ExtraVT, isVolatile, isNonTemporal, isInvariant,
1077                           MinAlign(Alignment, IncrementSize), AAInfo);
1078 
1079       // Build a factor node to remember that this load is independent of
1080       // the other one.
1081       Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1082                        Hi.getValue(1));
1083 
1084       // Move the top bits to the right place.
1085       Hi = DAG.getNode(
1086           ISD::SHL, dl, Hi.getValueType(), Hi,
1087           DAG.getConstant(ExtraWidth, dl,
1088                           TLI.getShiftAmountTy(Hi.getValueType(), DL)));
1089 
1090       // Join the hi and lo parts.
1091       Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
1092     }
1093 
1094     Chain = Ch;
1095   } else {
1096     bool isCustom = false;
1097     switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0),
1098                                  SrcVT.getSimpleVT())) {
1099     default: llvm_unreachable("This action is not supported yet!");
1100     case TargetLowering::Custom:
1101       isCustom = true;
1102       // FALLTHROUGH
1103     case TargetLowering::Legal: {
1104       Value = SDValue(Node, 0);
1105       Chain = SDValue(Node, 1);
1106 
1107       if (isCustom) {
1108         if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
1109           Value = Res;
1110           Chain = Res.getValue(1);
1111         }
1112       } else {
1113         // If this is an unaligned load and the target doesn't support it,
1114         // expand it.
1115         EVT MemVT = LD->getMemoryVT();
1116         unsigned AS = LD->getAddressSpace();
1117         unsigned Align = LD->getAlignment();
1118         const DataLayout &DL = DAG.getDataLayout();
1119         if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align))
1120           ExpandUnalignedLoad(cast<LoadSDNode>(Node), DAG, TLI, Value, Chain);
1121       }
1122       break;
1123     }
1124     case TargetLowering::Expand:
1125       EVT DestVT = Node->getValueType(0);
1126       if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) {
1127         // If the source type is not legal, see if there is a legal extload to
1128         // an intermediate type that we can then extend further.
1129         EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT());
1130         if (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT?
1131             TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT)) {
1132           // If we are loading a legal type, this is a non-extload followed by a
1133           // full extend.
1134           ISD::LoadExtType MidExtType =
1135               (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType;
1136 
1137           SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr,
1138                                         SrcVT, LD->getMemOperand());
1139           unsigned ExtendOp =
1140               ISD::getExtForLoadExtType(SrcVT.isFloatingPoint(), ExtType);
1141           Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1142           Chain = Load.getValue(1);
1143           break;
1144         }
1145 
1146         // Handle the special case of fp16 extloads. EXTLOAD doesn't have the
1147         // normal undefined upper bits behavior to allow using an in-reg extend
1148         // with the illegal FP type, so load as an integer and do the
1149         // from-integer conversion.
1150         if (SrcVT.getScalarType() == MVT::f16) {
1151           EVT ISrcVT = SrcVT.changeTypeToInteger();
1152           EVT IDestVT = DestVT.changeTypeToInteger();
1153           EVT LoadVT = TLI.getRegisterType(IDestVT.getSimpleVT());
1154 
1155           SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, LoadVT,
1156                                           Chain, Ptr, ISrcVT,
1157                                           LD->getMemOperand());
1158           Value = DAG.getNode(ISD::FP16_TO_FP, dl, DestVT, Result);
1159           Chain = Result.getValue(1);
1160           break;
1161         }
1162       }
1163 
1164       assert(!SrcVT.isVector() &&
1165              "Vector Loads are handled in LegalizeVectorOps");
1166 
1167       // FIXME: This does not work for vectors on most targets.  Sign-
1168       // and zero-extend operations are currently folded into extending
1169       // loads, whether they are legal or not, and then we end up here
1170       // without any support for legalizing them.
1171       assert(ExtType != ISD::EXTLOAD &&
1172              "EXTLOAD should always be supported!");
1173       // Turn the unsupported load into an EXTLOAD followed by an
1174       // explicit zero/sign extend inreg.
1175       SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl,
1176                                       Node->getValueType(0),
1177                                       Chain, Ptr, SrcVT,
1178                                       LD->getMemOperand());
1179       SDValue ValRes;
1180       if (ExtType == ISD::SEXTLOAD)
1181         ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1182                              Result.getValueType(),
1183                              Result, DAG.getValueType(SrcVT));
1184       else
1185         ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
1186       Value = ValRes;
1187       Chain = Result.getValue(1);
1188       break;
1189     }
1190   }
1191 
1192   // Since loads produce two values, make sure to remember that we legalized
1193   // both of them.
1194   if (Chain.getNode() != Node) {
1195     assert(Value.getNode() != Node && "Load must be completely replaced");
1196     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value);
1197     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
1198     if (UpdatedNodes) {
1199       UpdatedNodes->insert(Value.getNode());
1200       UpdatedNodes->insert(Chain.getNode());
1201     }
1202     ReplacedNode(Node);
1203   }
1204 }
1205 
1206 /// Return a legal replacement for the given operation, with all legal operands.
1207 void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
1208   DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG));
1209 
1210   if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
1211     return;
1212 
1213 #ifndef NDEBUG
1214   for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1215     assert((TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
1216               TargetLowering::TypeLegal ||
1217             TLI.isTypeLegal(Node->getValueType(i))) &&
1218            "Unexpected illegal type!");
1219 
1220   for (const SDValue &Op : Node->op_values())
1221     assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) ==
1222               TargetLowering::TypeLegal ||
1223             TLI.isTypeLegal(Op.getValueType()) ||
1224             Op.getOpcode() == ISD::TargetConstant) &&
1225             "Unexpected illegal type!");
1226 #endif
1227 
1228   // Figure out the correct action; the way to query this varies by opcode
1229   TargetLowering::LegalizeAction Action = TargetLowering::Legal;
1230   bool SimpleFinishLegalizing = true;
1231   switch (Node->getOpcode()) {
1232   case ISD::INTRINSIC_W_CHAIN:
1233   case ISD::INTRINSIC_WO_CHAIN:
1234   case ISD::INTRINSIC_VOID:
1235   case ISD::STACKSAVE:
1236     Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
1237     break;
1238   case ISD::GET_DYNAMIC_AREA_OFFSET:
1239     Action = TLI.getOperationAction(Node->getOpcode(),
1240                                     Node->getValueType(0));
1241     break;
1242   case ISD::VAARG:
1243     Action = TLI.getOperationAction(Node->getOpcode(),
1244                                     Node->getValueType(0));
1245     if (Action != TargetLowering::Promote)
1246       Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
1247     break;
1248   case ISD::FP_TO_FP16:
1249   case ISD::SINT_TO_FP:
1250   case ISD::UINT_TO_FP:
1251   case ISD::EXTRACT_VECTOR_ELT:
1252     Action = TLI.getOperationAction(Node->getOpcode(),
1253                                     Node->getOperand(0).getValueType());
1254     break;
1255   case ISD::FP_ROUND_INREG:
1256   case ISD::SIGN_EXTEND_INREG: {
1257     EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
1258     Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
1259     break;
1260   }
1261   case ISD::ATOMIC_STORE: {
1262     Action = TLI.getOperationAction(Node->getOpcode(),
1263                                     Node->getOperand(2).getValueType());
1264     break;
1265   }
1266   case ISD::SELECT_CC:
1267   case ISD::SETCC:
1268   case ISD::BR_CC: {
1269     unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
1270                          Node->getOpcode() == ISD::SETCC ? 2 :
1271                          Node->getOpcode() == ISD::SETCCE ? 3 : 1;
1272     unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
1273     MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType();
1274     ISD::CondCode CCCode =
1275         cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
1276     Action = TLI.getCondCodeAction(CCCode, OpVT);
1277     if (Action == TargetLowering::Legal) {
1278       if (Node->getOpcode() == ISD::SELECT_CC)
1279         Action = TLI.getOperationAction(Node->getOpcode(),
1280                                         Node->getValueType(0));
1281       else
1282         Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
1283     }
1284     break;
1285   }
1286   case ISD::LOAD:
1287   case ISD::STORE:
1288     // FIXME: Model these properly.  LOAD and STORE are complicated, and
1289     // STORE expects the unlegalized operand in some cases.
1290     SimpleFinishLegalizing = false;
1291     break;
1292   case ISD::CALLSEQ_START:
1293   case ISD::CALLSEQ_END:
1294     // FIXME: This shouldn't be necessary.  These nodes have special properties
1295     // dealing with the recursive nature of legalization.  Removing this
1296     // special case should be done as part of making LegalizeDAG non-recursive.
1297     SimpleFinishLegalizing = false;
1298     break;
1299   case ISD::EXTRACT_ELEMENT:
1300   case ISD::FLT_ROUNDS_:
1301   case ISD::FPOWI:
1302   case ISD::MERGE_VALUES:
1303   case ISD::EH_RETURN:
1304   case ISD::FRAME_TO_ARGS_OFFSET:
1305   case ISD::EH_SJLJ_SETJMP:
1306   case ISD::EH_SJLJ_LONGJMP:
1307   case ISD::EH_SJLJ_SETUP_DISPATCH:
1308     // These operations lie about being legal: when they claim to be legal,
1309     // they should actually be expanded.
1310     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1311     if (Action == TargetLowering::Legal)
1312       Action = TargetLowering::Expand;
1313     break;
1314   case ISD::INIT_TRAMPOLINE:
1315   case ISD::ADJUST_TRAMPOLINE:
1316   case ISD::FRAMEADDR:
1317   case ISD::RETURNADDR:
1318     // These operations lie about being legal: when they claim to be legal,
1319     // they should actually be custom-lowered.
1320     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1321     if (Action == TargetLowering::Legal)
1322       Action = TargetLowering::Custom;
1323     break;
1324   case ISD::READCYCLECOUNTER:
1325     // READCYCLECOUNTER returns an i64, even if type legalization might have
1326     // expanded that to several smaller types.
1327     Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64);
1328     break;
1329   case ISD::READ_REGISTER:
1330   case ISD::WRITE_REGISTER:
1331     // Named register is legal in the DAG, but blocked by register name
1332     // selection if not implemented by target (to chose the correct register)
1333     // They'll be converted to Copy(To/From)Reg.
1334     Action = TargetLowering::Legal;
1335     break;
1336   case ISD::DEBUGTRAP:
1337     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1338     if (Action == TargetLowering::Expand) {
1339       // replace ISD::DEBUGTRAP with ISD::TRAP
1340       SDValue NewVal;
1341       NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
1342                            Node->getOperand(0));
1343       ReplaceNode(Node, NewVal.getNode());
1344       LegalizeOp(NewVal.getNode());
1345       return;
1346     }
1347     break;
1348 
1349   default:
1350     if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1351       Action = TargetLowering::Legal;
1352     } else {
1353       Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1354     }
1355     break;
1356   }
1357 
1358   if (SimpleFinishLegalizing) {
1359     SDNode *NewNode = Node;
1360     switch (Node->getOpcode()) {
1361     default: break;
1362     case ISD::SHL:
1363     case ISD::SRL:
1364     case ISD::SRA:
1365     case ISD::ROTL:
1366     case ISD::ROTR:
1367       // Legalizing shifts/rotates requires adjusting the shift amount
1368       // to the appropriate width.
1369       if (!Node->getOperand(1).getValueType().isVector()) {
1370         SDValue SAO =
1371           DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
1372                                     Node->getOperand(1));
1373         HandleSDNode Handle(SAO);
1374         LegalizeOp(SAO.getNode());
1375         NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
1376                                          Handle.getValue());
1377       }
1378       break;
1379     case ISD::SRL_PARTS:
1380     case ISD::SRA_PARTS:
1381     case ISD::SHL_PARTS:
1382       // Legalizing shifts/rotates requires adjusting the shift amount
1383       // to the appropriate width.
1384       if (!Node->getOperand(2).getValueType().isVector()) {
1385         SDValue SAO =
1386           DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
1387                                     Node->getOperand(2));
1388         HandleSDNode Handle(SAO);
1389         LegalizeOp(SAO.getNode());
1390         NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
1391                                          Node->getOperand(1),
1392                                          Handle.getValue());
1393       }
1394       break;
1395     }
1396 
1397     if (NewNode != Node) {
1398       ReplaceNode(Node, NewNode);
1399       Node = NewNode;
1400     }
1401     switch (Action) {
1402     case TargetLowering::Legal:
1403       return;
1404     case TargetLowering::Custom: {
1405       // FIXME: The handling for custom lowering with multiple results is
1406       // a complete mess.
1407       if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
1408         if (!(Res.getNode() != Node || Res.getResNo() != 0))
1409           return;
1410 
1411         if (Node->getNumValues() == 1) {
1412           // We can just directly replace this node with the lowered value.
1413           ReplaceNode(SDValue(Node, 0), Res);
1414           return;
1415         }
1416 
1417         SmallVector<SDValue, 8> ResultVals;
1418         for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1419           ResultVals.push_back(Res.getValue(i));
1420         ReplaceNode(Node, ResultVals.data());
1421         return;
1422       }
1423     }
1424       // FALL THROUGH
1425     case TargetLowering::Expand:
1426       if (ExpandNode(Node))
1427         return;
1428       // FALL THROUGH
1429     case TargetLowering::LibCall:
1430       ConvertNodeToLibcall(Node);
1431       return;
1432     case TargetLowering::Promote:
1433       PromoteNode(Node);
1434       return;
1435     }
1436   }
1437 
1438   switch (Node->getOpcode()) {
1439   default:
1440 #ifndef NDEBUG
1441     dbgs() << "NODE: ";
1442     Node->dump( &DAG);
1443     dbgs() << "\n";
1444 #endif
1445     llvm_unreachable("Do not know how to legalize this operator!");
1446 
1447   case ISD::CALLSEQ_START:
1448   case ISD::CALLSEQ_END:
1449     break;
1450   case ISD::LOAD: {
1451     return LegalizeLoadOps(Node);
1452   }
1453   case ISD::STORE: {
1454     return LegalizeStoreOps(Node);
1455   }
1456   }
1457 }
1458 
1459 SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1460   SDValue Vec = Op.getOperand(0);
1461   SDValue Idx = Op.getOperand(1);
1462   SDLoc dl(Op);
1463 
1464   // Before we generate a new store to a temporary stack slot, see if there is
1465   // already one that we can use. There often is because when we scalarize
1466   // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole
1467   // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in
1468   // the vector. If all are expanded here, we don't want one store per vector
1469   // element.
1470 
1471   // Caches for hasPredecessorHelper
1472   SmallPtrSet<const SDNode *, 32> Visited;
1473   SmallVector<const SDNode *, 16> Worklist;
1474 
1475   SDValue StackPtr, Ch;
1476   for (SDNode::use_iterator UI = Vec.getNode()->use_begin(),
1477        UE = Vec.getNode()->use_end(); UI != UE; ++UI) {
1478     SDNode *User = *UI;
1479     if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) {
1480       if (ST->isIndexed() || ST->isTruncatingStore() ||
1481           ST->getValue() != Vec)
1482         continue;
1483 
1484       // Make sure that nothing else could have stored into the destination of
1485       // this store.
1486       if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode()))
1487         continue;
1488 
1489       // If the index is dependent on the store we will introduce a cycle when
1490       // creating the load (the load uses the index, and by replacing the chain
1491       // we will make the index dependent on the load).
1492       if (Idx.getNode()->hasPredecessorHelper(ST, Visited, Worklist))
1493         continue;
1494 
1495       StackPtr = ST->getBasePtr();
1496       Ch = SDValue(ST, 0);
1497       break;
1498     }
1499   }
1500 
1501   if (!Ch.getNode()) {
1502     // Store the value to a temporary stack slot, then LOAD the returned part.
1503     StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1504     Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1505                       MachinePointerInfo(), false, false, 0);
1506   }
1507 
1508   // Add the offset to the index.
1509   unsigned EltSize =
1510       Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1511   Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1512                     DAG.getConstant(EltSize, SDLoc(Vec), Idx.getValueType()));
1513 
1514   Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy(DAG.getDataLayout()));
1515   StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1516 
1517   SDValue NewLoad;
1518 
1519   if (Op.getValueType().isVector())
1520     NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,
1521                           MachinePointerInfo(), false, false, false, 0);
1522   else
1523     NewLoad = DAG.getExtLoad(
1524         ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr, MachinePointerInfo(),
1525         Vec.getValueType().getVectorElementType(), false, false, false, 0);
1526 
1527   // Replace the chain going out of the store, by the one out of the load.
1528   DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1));
1529 
1530   // We introduced a cycle though, so update the loads operands, making sure
1531   // to use the original store's chain as an incoming chain.
1532   SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(),
1533                                           NewLoad->op_end());
1534   NewLoadOperands[0] = Ch;
1535   NewLoad =
1536       SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0);
1537   return NewLoad;
1538 }
1539 
1540 SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1541   assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1542 
1543   SDValue Vec  = Op.getOperand(0);
1544   SDValue Part = Op.getOperand(1);
1545   SDValue Idx  = Op.getOperand(2);
1546   SDLoc dl(Op);
1547 
1548   // Store the value to a temporary stack slot, then LOAD the returned part.
1549 
1550   SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1551   int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1552   MachinePointerInfo PtrInfo =
1553       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
1554 
1555   // First store the whole vector.
1556   SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1557                             false, false, 0);
1558 
1559   // Then store the inserted part.
1560 
1561   // Add the offset to the index.
1562   unsigned EltSize =
1563       Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1564 
1565   Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1566                     DAG.getConstant(EltSize, SDLoc(Vec), Idx.getValueType()));
1567   Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy(DAG.getDataLayout()));
1568 
1569   SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1570                                     StackPtr);
1571 
1572   // Store the subvector.
1573   Ch = DAG.getStore(Ch, dl, Part, SubStackPtr,
1574                     MachinePointerInfo(), false, false, 0);
1575 
1576   // Finally, load the updated vector.
1577   return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1578                      false, false, false, 0);
1579 }
1580 
1581 SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1582   // We can't handle this case efficiently.  Allocate a sufficiently
1583   // aligned object on the stack, store each element into it, then load
1584   // the result as a vector.
1585   // Create the stack frame object.
1586   EVT VT = Node->getValueType(0);
1587   EVT EltVT = VT.getVectorElementType();
1588   SDLoc dl(Node);
1589   SDValue FIPtr = DAG.CreateStackTemporary(VT);
1590   int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
1591   MachinePointerInfo PtrInfo =
1592       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
1593 
1594   // Emit a store of each element to the stack slot.
1595   SmallVector<SDValue, 8> Stores;
1596   unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
1597   // Store (in the right endianness) the elements to memory.
1598   for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1599     // Ignore undef elements.
1600     if (Node->getOperand(i).isUndef()) continue;
1601 
1602     unsigned Offset = TypeByteSize*i;
1603 
1604     SDValue Idx = DAG.getConstant(Offset, dl, FIPtr.getValueType());
1605     Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1606 
1607     // If the destination vector element type is narrower than the source
1608     // element type, only store the bits necessary.
1609     if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
1610       Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
1611                                          Node->getOperand(i), Idx,
1612                                          PtrInfo.getWithOffset(Offset),
1613                                          EltVT, false, false, 0));
1614     } else
1615       Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
1616                                     Node->getOperand(i), Idx,
1617                                     PtrInfo.getWithOffset(Offset),
1618                                     false, false, 0));
1619   }
1620 
1621   SDValue StoreChain;
1622   if (!Stores.empty())    // Not all undef elements?
1623     StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
1624   else
1625     StoreChain = DAG.getEntryNode();
1626 
1627   // Result is a load from the stack slot.
1628   return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo,
1629                      false, false, false, 0);
1630 }
1631 
1632 namespace {
1633 /// Keeps track of state when getting the sign of a floating-point value as an
1634 /// integer.
1635 struct FloatSignAsInt {
1636   EVT FloatVT;
1637   SDValue Chain;
1638   SDValue FloatPtr;
1639   SDValue IntPtr;
1640   MachinePointerInfo IntPointerInfo;
1641   MachinePointerInfo FloatPointerInfo;
1642   SDValue IntValue;
1643   APInt SignMask;
1644   uint8_t SignBit;
1645 };
1646 }
1647 
1648 /// Bitcast a floating-point value to an integer value. Only bitcast the part
1649 /// containing the sign bit if the target has no integer value capable of
1650 /// holding all bits of the floating-point value.
1651 void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State,
1652                                              SDLoc DL, SDValue Value) const {
1653   EVT FloatVT = Value.getValueType();
1654   unsigned NumBits = FloatVT.getSizeInBits();
1655   State.FloatVT = FloatVT;
1656   EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
1657   // Convert to an integer of the same size.
1658   if (TLI.isTypeLegal(IVT)) {
1659     State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value);
1660     State.SignMask = APInt::getSignBit(NumBits);
1661     State.SignBit = NumBits - 1;
1662     return;
1663   }
1664 
1665   auto &DataLayout = DAG.getDataLayout();
1666   // Store the float to memory, then load the sign part out as an integer.
1667   MVT LoadTy = TLI.getRegisterType(*DAG.getContext(), MVT::i8);
1668   // First create a temporary that is aligned for both the load and store.
1669   SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1670   int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1671   // Then store the float to it.
1672   State.FloatPtr = StackPtr;
1673   MachineFunction &MF = DAG.getMachineFunction();
1674   State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI);
1675   State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr,
1676                              State.FloatPointerInfo, false, false, 0);
1677 
1678   SDValue IntPtr;
1679   if (DataLayout.isBigEndian()) {
1680     assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1681     // Load out a legal integer with the same sign bit as the float.
1682     IntPtr = StackPtr;
1683     State.IntPointerInfo = State.FloatPointerInfo;
1684   } else {
1685     // Advance the pointer so that the loaded byte will contain the sign bit.
1686     unsigned ByteOffset = (FloatVT.getSizeInBits() / 8) - 1;
1687     IntPtr = DAG.getNode(ISD::ADD, DL, StackPtr.getValueType(), StackPtr,
1688                       DAG.getConstant(ByteOffset, DL, StackPtr.getValueType()));
1689     State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI,
1690                                                              ByteOffset);
1691   }
1692 
1693   State.IntPtr = IntPtr;
1694   State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain,
1695                                   IntPtr, State.IntPointerInfo, MVT::i8,
1696                                   false, false, false, 0);
1697   State.SignMask = APInt::getOneBitSet(LoadTy.getSizeInBits(), 7);
1698   State.SignBit = 7;
1699 }
1700 
1701 /// Replace the integer value produced by getSignAsIntValue() with a new value
1702 /// and cast the result back to a floating-point type.
1703 SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State,
1704                                           SDLoc DL, SDValue NewIntValue) const {
1705   if (!State.Chain)
1706     return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue);
1707 
1708   // Override the part containing the sign bit in the value stored on the stack.
1709   SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr,
1710                                     State.IntPointerInfo, MVT::i8, false, false,
1711                                     0);
1712   return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr,
1713                      State.FloatPointerInfo, false, false, false, 0);
1714 }
1715 
1716 SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const {
1717   SDLoc DL(Node);
1718   SDValue Mag = Node->getOperand(0);
1719   SDValue Sign = Node->getOperand(1);
1720 
1721   // Get sign bit into an integer value.
1722   FloatSignAsInt SignAsInt;
1723   getSignAsIntValue(SignAsInt, DL, Sign);
1724 
1725   EVT IntVT = SignAsInt.IntValue.getValueType();
1726   SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1727   SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue,
1728                                 SignMask);
1729 
1730   // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X)
1731   EVT FloatVT = Mag.getValueType();
1732   if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) &&
1733       TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) {
1734     SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag);
1735     SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue);
1736     SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit,
1737                                 DAG.getConstant(0, DL, IntVT), ISD::SETNE);
1738     return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue);
1739   }
1740 
1741   // Transform Mag value to integer, and clear the sign bit.
1742   FloatSignAsInt MagAsInt;
1743   getSignAsIntValue(MagAsInt, DL, Mag);
1744   EVT MagVT = MagAsInt.IntValue.getValueType();
1745   SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT);
1746   SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue,
1747                                     ClearSignMask);
1748 
1749   // Get the signbit at the right position for MagAsInt.
1750   int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit;
1751   if (SignBit.getValueSizeInBits() > ClearedSign.getValueSizeInBits()) {
1752     if (ShiftAmount > 0) {
1753       SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, IntVT);
1754       SignBit = DAG.getNode(ISD::SRL, DL, IntVT, SignBit, ShiftCnst);
1755     } else if (ShiftAmount < 0) {
1756       SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, IntVT);
1757       SignBit = DAG.getNode(ISD::SHL, DL, IntVT, SignBit, ShiftCnst);
1758     }
1759     SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit);
1760   } else if (SignBit.getValueSizeInBits() < ClearedSign.getValueSizeInBits()) {
1761     SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit);
1762     if (ShiftAmount > 0) {
1763       SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, MagVT);
1764       SignBit = DAG.getNode(ISD::SRL, DL, MagVT, SignBit, ShiftCnst);
1765     } else if (ShiftAmount < 0) {
1766       SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, MagVT);
1767       SignBit = DAG.getNode(ISD::SHL, DL, MagVT, SignBit, ShiftCnst);
1768     }
1769   }
1770 
1771   // Store the part with the modified sign and convert back to float.
1772   SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit);
1773   return modifySignAsInt(MagAsInt, DL, CopiedSign);
1774 }
1775 
1776 SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const {
1777   SDLoc DL(Node);
1778   SDValue Value = Node->getOperand(0);
1779 
1780   // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal.
1781   EVT FloatVT = Value.getValueType();
1782   if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) {
1783     SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT);
1784     return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero);
1785   }
1786 
1787   // Transform value to integer, clear the sign bit and transform back.
1788   FloatSignAsInt ValueAsInt;
1789   getSignAsIntValue(ValueAsInt, DL, Value);
1790   EVT IntVT = ValueAsInt.IntValue.getValueType();
1791   SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT);
1792   SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue,
1793                                     ClearSignMask);
1794   return modifySignAsInt(ValueAsInt, DL, ClearedSign);
1795 }
1796 
1797 void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1798                                            SmallVectorImpl<SDValue> &Results) {
1799   unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1800   assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1801           " not tell us which reg is the stack pointer!");
1802   SDLoc dl(Node);
1803   EVT VT = Node->getValueType(0);
1804   SDValue Tmp1 = SDValue(Node, 0);
1805   SDValue Tmp2 = SDValue(Node, 1);
1806   SDValue Tmp3 = Node->getOperand(2);
1807   SDValue Chain = Tmp1.getOperand(0);
1808 
1809   // Chain the dynamic stack allocation so that it doesn't modify the stack
1810   // pointer when other instructions are using the stack.
1811   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, dl, true), dl);
1812 
1813   SDValue Size  = Tmp2.getOperand(1);
1814   SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1815   Chain = SP.getValue(1);
1816   unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
1817   unsigned StackAlign =
1818       DAG.getSubtarget().getFrameLowering()->getStackAlignment();
1819   Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size);       // Value
1820   if (Align > StackAlign)
1821     Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
1822                        DAG.getConstant(-(uint64_t)Align, dl, VT));
1823   Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1);     // Output chain
1824 
1825   Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, dl, true),
1826                             DAG.getIntPtrConstant(0, dl, true), SDValue(), dl);
1827 
1828   Results.push_back(Tmp1);
1829   Results.push_back(Tmp2);
1830 }
1831 
1832 /// Legalize a SETCC with given LHS and RHS and condition code CC on the current
1833 /// target.
1834 ///
1835 /// If the SETCC has been legalized using AND / OR, then the legalized node
1836 /// will be stored in LHS. RHS and CC will be set to SDValue(). NeedInvert
1837 /// will be set to false.
1838 ///
1839 /// If the SETCC has been legalized by using getSetCCSwappedOperands(),
1840 /// then the values of LHS and RHS will be swapped, CC will be set to the
1841 /// new condition, and NeedInvert will be set to false.
1842 ///
1843 /// If the SETCC has been legalized using the inverse condcode, then LHS and
1844 /// RHS will be unchanged, CC will set to the inverted condcode, and NeedInvert
1845 /// will be set to true. The caller must invert the result of the SETCC with
1846 /// SelectionDAG::getLogicalNOT() or take equivalent action to swap the effect
1847 /// of a true/false result.
1848 ///
1849 /// \returns true if the SetCC has been legalized, false if it hasn't.
1850 bool SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
1851                                                  SDValue &LHS, SDValue &RHS,
1852                                                  SDValue &CC,
1853                                                  bool &NeedInvert,
1854                                                  SDLoc dl) {
1855   MVT OpVT = LHS.getSimpleValueType();
1856   ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1857   NeedInvert = false;
1858   switch (TLI.getCondCodeAction(CCCode, OpVT)) {
1859   default: llvm_unreachable("Unknown condition code action!");
1860   case TargetLowering::Legal:
1861     // Nothing to do.
1862     break;
1863   case TargetLowering::Expand: {
1864     ISD::CondCode InvCC = ISD::getSetCCSwappedOperands(CCCode);
1865     if (TLI.isCondCodeLegal(InvCC, OpVT)) {
1866       std::swap(LHS, RHS);
1867       CC = DAG.getCondCode(InvCC);
1868       return true;
1869     }
1870     ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1871     unsigned Opc = 0;
1872     switch (CCCode) {
1873     default: llvm_unreachable("Don't know how to expand this condition!");
1874     case ISD::SETO:
1875         assert(TLI.getCondCodeAction(ISD::SETOEQ, OpVT)
1876             == TargetLowering::Legal
1877             && "If SETO is expanded, SETOEQ must be legal!");
1878         CC1 = ISD::SETOEQ; CC2 = ISD::SETOEQ; Opc = ISD::AND; break;
1879     case ISD::SETUO:
1880         assert(TLI.getCondCodeAction(ISD::SETUNE, OpVT)
1881             == TargetLowering::Legal
1882             && "If SETUO is expanded, SETUNE must be legal!");
1883         CC1 = ISD::SETUNE; CC2 = ISD::SETUNE; Opc = ISD::OR;  break;
1884     case ISD::SETOEQ:
1885     case ISD::SETOGT:
1886     case ISD::SETOGE:
1887     case ISD::SETOLT:
1888     case ISD::SETOLE:
1889     case ISD::SETONE:
1890     case ISD::SETUEQ:
1891     case ISD::SETUNE:
1892     case ISD::SETUGT:
1893     case ISD::SETUGE:
1894     case ISD::SETULT:
1895     case ISD::SETULE:
1896         // If we are floating point, assign and break, otherwise fall through.
1897         if (!OpVT.isInteger()) {
1898           // We can use the 4th bit to tell if we are the unordered
1899           // or ordered version of the opcode.
1900           CC2 = ((unsigned)CCCode & 0x8U) ? ISD::SETUO : ISD::SETO;
1901           Opc = ((unsigned)CCCode & 0x8U) ? ISD::OR : ISD::AND;
1902           CC1 = (ISD::CondCode)(((int)CCCode & 0x7) | 0x10);
1903           break;
1904         }
1905         // Fallthrough if we are unsigned integer.
1906     case ISD::SETLE:
1907     case ISD::SETGT:
1908     case ISD::SETGE:
1909     case ISD::SETLT:
1910       // We only support using the inverted operation, which is computed above
1911       // and not a different manner of supporting expanding these cases.
1912       llvm_unreachable("Don't know how to expand this condition!");
1913     case ISD::SETNE:
1914     case ISD::SETEQ:
1915       // Try inverting the result of the inverse condition.
1916       InvCC = CCCode == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ;
1917       if (TLI.isCondCodeLegal(InvCC, OpVT)) {
1918         CC = DAG.getCondCode(InvCC);
1919         NeedInvert = true;
1920         return true;
1921       }
1922       // If inverting the condition didn't work then we have no means to expand
1923       // the condition.
1924       llvm_unreachable("Don't know how to expand this condition!");
1925     }
1926 
1927     SDValue SetCC1, SetCC2;
1928     if (CCCode != ISD::SETO && CCCode != ISD::SETUO) {
1929       // If we aren't the ordered or unorder operation,
1930       // then the pattern is (LHS CC1 RHS) Opc (LHS CC2 RHS).
1931       SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1932       SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1933     } else {
1934       // Otherwise, the pattern is (LHS CC1 LHS) Opc (RHS CC2 RHS)
1935       SetCC1 = DAG.getSetCC(dl, VT, LHS, LHS, CC1);
1936       SetCC2 = DAG.getSetCC(dl, VT, RHS, RHS, CC2);
1937     }
1938     LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
1939     RHS = SDValue();
1940     CC  = SDValue();
1941     return true;
1942   }
1943   }
1944   return false;
1945 }
1946 
1947 /// Emit a store/load combination to the stack.  This stores
1948 /// SrcOp to a stack slot of type SlotVT, truncating it if needed.  It then does
1949 /// a load from the stack slot to DestVT, extending it if needed.
1950 /// The resultant code need not be legal.
1951 SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
1952                                                EVT SlotVT,
1953                                                EVT DestVT,
1954                                                SDLoc dl) {
1955   // Create the stack frame object.
1956   unsigned SrcAlign = DAG.getDataLayout().getPrefTypeAlignment(
1957       SrcOp.getValueType().getTypeForEVT(*DAG.getContext()));
1958   SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
1959 
1960   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1961   int SPFI = StackPtrFI->getIndex();
1962   MachinePointerInfo PtrInfo =
1963       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
1964 
1965   unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1966   unsigned SlotSize = SlotVT.getSizeInBits();
1967   unsigned DestSize = DestVT.getSizeInBits();
1968   Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1969   unsigned DestAlign = DAG.getDataLayout().getPrefTypeAlignment(DestType);
1970 
1971   // Emit a store to the stack slot.  Use a truncstore if the input value is
1972   // later than DestVT.
1973   SDValue Store;
1974 
1975   if (SrcSize > SlotSize)
1976     Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
1977                               PtrInfo, SlotVT, false, false, SrcAlign);
1978   else {
1979     assert(SrcSize == SlotSize && "Invalid store");
1980     Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
1981                          PtrInfo, false, false, SrcAlign);
1982   }
1983 
1984   // Result is a load from the stack slot.
1985   if (SlotSize == DestSize)
1986     return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
1987                        false, false, false, DestAlign);
1988 
1989   assert(SlotSize < DestSize && "Unknown extension!");
1990   return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
1991                         PtrInfo, SlotVT, false, false, false, DestAlign);
1992 }
1993 
1994 SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
1995   SDLoc dl(Node);
1996   // Create a vector sized/aligned stack slot, store the value to element #0,
1997   // then load the whole vector back out.
1998   SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
1999 
2000   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
2001   int SPFI = StackPtrFI->getIndex();
2002 
2003   SDValue Ch = DAG.getTruncStore(
2004       DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr,
2005       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI),
2006       Node->getValueType(0).getVectorElementType(), false, false, 0);
2007   return DAG.getLoad(
2008       Node->getValueType(0), dl, Ch, StackPtr,
2009       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), false,
2010       false, false, 0);
2011 }
2012 
2013 static bool
2014 ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG,
2015                      const TargetLowering &TLI, SDValue &Res) {
2016   unsigned NumElems = Node->getNumOperands();
2017   SDLoc dl(Node);
2018   EVT VT = Node->getValueType(0);
2019 
2020   // Try to group the scalars into pairs, shuffle the pairs together, then
2021   // shuffle the pairs of pairs together, etc. until the vector has
2022   // been built. This will work only if all of the necessary shuffle masks
2023   // are legal.
2024 
2025   // We do this in two phases; first to check the legality of the shuffles,
2026   // and next, assuming that all shuffles are legal, to create the new nodes.
2027   for (int Phase = 0; Phase < 2; ++Phase) {
2028     SmallVector<std::pair<SDValue, SmallVector<int, 16> >, 16> IntermedVals,
2029                                                                NewIntermedVals;
2030     for (unsigned i = 0; i < NumElems; ++i) {
2031       SDValue V = Node->getOperand(i);
2032       if (V.isUndef())
2033         continue;
2034 
2035       SDValue Vec;
2036       if (Phase)
2037         Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V);
2038       IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i)));
2039     }
2040 
2041     while (IntermedVals.size() > 2) {
2042       NewIntermedVals.clear();
2043       for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) {
2044         // This vector and the next vector are shuffled together (simply to
2045         // append the one to the other).
2046         SmallVector<int, 16> ShuffleVec(NumElems, -1);
2047 
2048         SmallVector<int, 16> FinalIndices;
2049         FinalIndices.reserve(IntermedVals[i].second.size() +
2050                              IntermedVals[i+1].second.size());
2051 
2052         int k = 0;
2053         for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f;
2054              ++j, ++k) {
2055           ShuffleVec[k] = j;
2056           FinalIndices.push_back(IntermedVals[i].second[j]);
2057         }
2058         for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f;
2059              ++j, ++k) {
2060           ShuffleVec[k] = NumElems + j;
2061           FinalIndices.push_back(IntermedVals[i+1].second[j]);
2062         }
2063 
2064         SDValue Shuffle;
2065         if (Phase)
2066           Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first,
2067                                          IntermedVals[i+1].first,
2068                                          ShuffleVec.data());
2069         else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
2070           return false;
2071         NewIntermedVals.push_back(
2072             std::make_pair(Shuffle, std::move(FinalIndices)));
2073       }
2074 
2075       // If we had an odd number of defined values, then append the last
2076       // element to the array of new vectors.
2077       if ((IntermedVals.size() & 1) != 0)
2078         NewIntermedVals.push_back(IntermedVals.back());
2079 
2080       IntermedVals.swap(NewIntermedVals);
2081     }
2082 
2083     assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 &&
2084            "Invalid number of intermediate vectors");
2085     SDValue Vec1 = IntermedVals[0].first;
2086     SDValue Vec2;
2087     if (IntermedVals.size() > 1)
2088       Vec2 = IntermedVals[1].first;
2089     else if (Phase)
2090       Vec2 = DAG.getUNDEF(VT);
2091 
2092     SmallVector<int, 16> ShuffleVec(NumElems, -1);
2093     for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i)
2094       ShuffleVec[IntermedVals[0].second[i]] = i;
2095     for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i)
2096       ShuffleVec[IntermedVals[1].second[i]] = NumElems + i;
2097 
2098     if (Phase)
2099       Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
2100     else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
2101       return false;
2102   }
2103 
2104   return true;
2105 }
2106 
2107 /// Expand a BUILD_VECTOR node on targets that don't
2108 /// support the operation, but do support the resultant vector type.
2109 SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
2110   unsigned NumElems = Node->getNumOperands();
2111   SDValue Value1, Value2;
2112   SDLoc dl(Node);
2113   EVT VT = Node->getValueType(0);
2114   EVT OpVT = Node->getOperand(0).getValueType();
2115   EVT EltVT = VT.getVectorElementType();
2116 
2117   // If the only non-undef value is the low element, turn this into a
2118   // SCALAR_TO_VECTOR node.  If this is { X, X, X, X }, determine X.
2119   bool isOnlyLowElement = true;
2120   bool MoreThanTwoValues = false;
2121   bool isConstant = true;
2122   for (unsigned i = 0; i < NumElems; ++i) {
2123     SDValue V = Node->getOperand(i);
2124     if (V.isUndef())
2125       continue;
2126     if (i > 0)
2127       isOnlyLowElement = false;
2128     if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
2129       isConstant = false;
2130 
2131     if (!Value1.getNode()) {
2132       Value1 = V;
2133     } else if (!Value2.getNode()) {
2134       if (V != Value1)
2135         Value2 = V;
2136     } else if (V != Value1 && V != Value2) {
2137       MoreThanTwoValues = true;
2138     }
2139   }
2140 
2141   if (!Value1.getNode())
2142     return DAG.getUNDEF(VT);
2143 
2144   if (isOnlyLowElement)
2145     return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
2146 
2147   // If all elements are constants, create a load from the constant pool.
2148   if (isConstant) {
2149     SmallVector<Constant*, 16> CV;
2150     for (unsigned i = 0, e = NumElems; i != e; ++i) {
2151       if (ConstantFPSDNode *V =
2152           dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
2153         CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
2154       } else if (ConstantSDNode *V =
2155                  dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
2156         if (OpVT==EltVT)
2157           CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
2158         else {
2159           // If OpVT and EltVT don't match, EltVT is not legal and the
2160           // element values have been promoted/truncated earlier.  Undo this;
2161           // we don't want a v16i8 to become a v16i32 for example.
2162           const ConstantInt *CI = V->getConstantIntValue();
2163           CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
2164                                         CI->getZExtValue()));
2165         }
2166       } else {
2167         assert(Node->getOperand(i).isUndef());
2168         Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
2169         CV.push_back(UndefValue::get(OpNTy));
2170       }
2171     }
2172     Constant *CP = ConstantVector::get(CV);
2173     SDValue CPIdx =
2174         DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout()));
2175     unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2176     return DAG.getLoad(
2177         VT, dl, DAG.getEntryNode(), CPIdx,
2178         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2179         false, false, Alignment);
2180   }
2181 
2182   SmallSet<SDValue, 16> DefinedValues;
2183   for (unsigned i = 0; i < NumElems; ++i) {
2184     if (Node->getOperand(i).isUndef())
2185       continue;
2186     DefinedValues.insert(Node->getOperand(i));
2187   }
2188 
2189   if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) {
2190     if (!MoreThanTwoValues) {
2191       SmallVector<int, 8> ShuffleVec(NumElems, -1);
2192       for (unsigned i = 0; i < NumElems; ++i) {
2193         SDValue V = Node->getOperand(i);
2194         if (V.isUndef())
2195           continue;
2196         ShuffleVec[i] = V == Value1 ? 0 : NumElems;
2197       }
2198       if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
2199         // Get the splatted value into the low element of a vector register.
2200         SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
2201         SDValue Vec2;
2202         if (Value2.getNode())
2203           Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
2204         else
2205           Vec2 = DAG.getUNDEF(VT);
2206 
2207         // Return shuffle(LowValVec, undef, <0,0,0,0>)
2208         return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
2209       }
2210     } else {
2211       SDValue Res;
2212       if (ExpandBVWithShuffles(Node, DAG, TLI, Res))
2213         return Res;
2214     }
2215   }
2216 
2217   // Otherwise, we can't handle this case efficiently.
2218   return ExpandVectorBuildThroughStack(Node);
2219 }
2220 
2221 // Expand a node into a call to a libcall.  If the result value
2222 // does not fit into a register, return the lo part and set the hi part to the
2223 // by-reg argument.  If it does fit into a single register, return the result
2224 // and leave the Hi part unset.
2225 SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
2226                                             bool isSigned) {
2227   TargetLowering::ArgListTy Args;
2228   TargetLowering::ArgListEntry Entry;
2229   for (const SDValue &Op : Node->op_values()) {
2230     EVT ArgVT = Op.getValueType();
2231     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2232     Entry.Node = Op;
2233     Entry.Ty = ArgTy;
2234     Entry.isSExt = isSigned;
2235     Entry.isZExt = !isSigned;
2236     Args.push_back(Entry);
2237   }
2238   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2239                                          TLI.getPointerTy(DAG.getDataLayout()));
2240 
2241   Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
2242 
2243   // By default, the input chain to this libcall is the entry node of the
2244   // function. If the libcall is going to be emitted as a tail call then
2245   // TLI.isUsedByReturnOnly will change it to the right chain if the return
2246   // node which is being folded has a non-entry input chain.
2247   SDValue InChain = DAG.getEntryNode();
2248 
2249   // isTailCall may be true since the callee does not reference caller stack
2250   // frame. Check if it's in the right position.
2251   SDValue TCChain = InChain;
2252   bool isTailCall = TLI.isInTailCallPosition(DAG, Node, TCChain);
2253   if (isTailCall)
2254     InChain = TCChain;
2255 
2256   TargetLowering::CallLoweringInfo CLI(DAG);
2257   CLI.setDebugLoc(SDLoc(Node)).setChain(InChain)
2258     .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
2259     .setTailCall(isTailCall).setSExtResult(isSigned).setZExtResult(!isSigned);
2260 
2261   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2262 
2263   if (!CallInfo.second.getNode())
2264     // It's a tailcall, return the chain (which is the DAG root).
2265     return DAG.getRoot();
2266 
2267   return CallInfo.first;
2268 }
2269 
2270 /// Generate a libcall taking the given operands as arguments
2271 /// and returning a result of type RetVT.
2272 SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
2273                                             const SDValue *Ops, unsigned NumOps,
2274                                             bool isSigned, SDLoc dl) {
2275   TargetLowering::ArgListTy Args;
2276   Args.reserve(NumOps);
2277 
2278   TargetLowering::ArgListEntry Entry;
2279   for (unsigned i = 0; i != NumOps; ++i) {
2280     Entry.Node = Ops[i];
2281     Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
2282     Entry.isSExt = isSigned;
2283     Entry.isZExt = !isSigned;
2284     Args.push_back(Entry);
2285   }
2286   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2287                                          TLI.getPointerTy(DAG.getDataLayout()));
2288 
2289   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2290 
2291   TargetLowering::CallLoweringInfo CLI(DAG);
2292   CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
2293     .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
2294     .setSExtResult(isSigned).setZExtResult(!isSigned);
2295 
2296   std::pair<SDValue,SDValue> CallInfo = TLI.LowerCallTo(CLI);
2297 
2298   return CallInfo.first;
2299 }
2300 
2301 // Expand a node into a call to a libcall. Similar to
2302 // ExpandLibCall except that the first operand is the in-chain.
2303 std::pair<SDValue, SDValue>
2304 SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2305                                          SDNode *Node,
2306                                          bool isSigned) {
2307   SDValue InChain = Node->getOperand(0);
2308 
2309   TargetLowering::ArgListTy Args;
2310   TargetLowering::ArgListEntry Entry;
2311   for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2312     EVT ArgVT = Node->getOperand(i).getValueType();
2313     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2314     Entry.Node = Node->getOperand(i);
2315     Entry.Ty = ArgTy;
2316     Entry.isSExt = isSigned;
2317     Entry.isZExt = !isSigned;
2318     Args.push_back(Entry);
2319   }
2320   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2321                                          TLI.getPointerTy(DAG.getDataLayout()));
2322 
2323   Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
2324 
2325   TargetLowering::CallLoweringInfo CLI(DAG);
2326   CLI.setDebugLoc(SDLoc(Node)).setChain(InChain)
2327     .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
2328     .setSExtResult(isSigned).setZExtResult(!isSigned);
2329 
2330   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2331 
2332   return CallInfo;
2333 }
2334 
2335 SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2336                                               RTLIB::Libcall Call_F32,
2337                                               RTLIB::Libcall Call_F64,
2338                                               RTLIB::Libcall Call_F80,
2339                                               RTLIB::Libcall Call_F128,
2340                                               RTLIB::Libcall Call_PPCF128) {
2341   RTLIB::Libcall LC;
2342   switch (Node->getSimpleValueType(0).SimpleTy) {
2343   default: llvm_unreachable("Unexpected request for libcall!");
2344   case MVT::f32: LC = Call_F32; break;
2345   case MVT::f64: LC = Call_F64; break;
2346   case MVT::f80: LC = Call_F80; break;
2347   case MVT::f128: LC = Call_F128; break;
2348   case MVT::ppcf128: LC = Call_PPCF128; break;
2349   }
2350   return ExpandLibCall(LC, Node, false);
2351 }
2352 
2353 SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2354                                                RTLIB::Libcall Call_I8,
2355                                                RTLIB::Libcall Call_I16,
2356                                                RTLIB::Libcall Call_I32,
2357                                                RTLIB::Libcall Call_I64,
2358                                                RTLIB::Libcall Call_I128) {
2359   RTLIB::Libcall LC;
2360   switch (Node->getSimpleValueType(0).SimpleTy) {
2361   default: llvm_unreachable("Unexpected request for libcall!");
2362   case MVT::i8:   LC = Call_I8; break;
2363   case MVT::i16:  LC = Call_I16; break;
2364   case MVT::i32:  LC = Call_I32; break;
2365   case MVT::i64:  LC = Call_I64; break;
2366   case MVT::i128: LC = Call_I128; break;
2367   }
2368   return ExpandLibCall(LC, Node, isSigned);
2369 }
2370 
2371 /// Issue libcalls to __{u}divmod to compute div / rem pairs.
2372 void
2373 SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2374                                           SmallVectorImpl<SDValue> &Results) {
2375   unsigned Opcode = Node->getOpcode();
2376   bool isSigned = Opcode == ISD::SDIVREM;
2377 
2378   RTLIB::Libcall LC;
2379   switch (Node->getSimpleValueType(0).SimpleTy) {
2380   default: llvm_unreachable("Unexpected request for libcall!");
2381   case MVT::i8:   LC= isSigned ? RTLIB::SDIVREM_I8  : RTLIB::UDIVREM_I8;  break;
2382   case MVT::i16:  LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2383   case MVT::i32:  LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2384   case MVT::i64:  LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2385   case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
2386   }
2387 
2388   // The input chain to this libcall is the entry node of the function.
2389   // Legalizing the call will automatically add the previous call to the
2390   // dependence.
2391   SDValue InChain = DAG.getEntryNode();
2392 
2393   EVT RetVT = Node->getValueType(0);
2394   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2395 
2396   TargetLowering::ArgListTy Args;
2397   TargetLowering::ArgListEntry Entry;
2398   for (const SDValue &Op : Node->op_values()) {
2399     EVT ArgVT = Op.getValueType();
2400     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2401     Entry.Node = Op;
2402     Entry.Ty = ArgTy;
2403     Entry.isSExt = isSigned;
2404     Entry.isZExt = !isSigned;
2405     Args.push_back(Entry);
2406   }
2407 
2408   // Also pass the return address of the remainder.
2409   SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2410   Entry.Node = FIPtr;
2411   Entry.Ty = RetTy->getPointerTo();
2412   Entry.isSExt = isSigned;
2413   Entry.isZExt = !isSigned;
2414   Args.push_back(Entry);
2415 
2416   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2417                                          TLI.getPointerTy(DAG.getDataLayout()));
2418 
2419   SDLoc dl(Node);
2420   TargetLowering::CallLoweringInfo CLI(DAG);
2421   CLI.setDebugLoc(dl).setChain(InChain)
2422     .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
2423     .setSExtResult(isSigned).setZExtResult(!isSigned);
2424 
2425   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2426 
2427   // Remainder is loaded back from the stack frame.
2428   SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr,
2429                             MachinePointerInfo(), false, false, false, 0);
2430   Results.push_back(CallInfo.first);
2431   Results.push_back(Rem);
2432 }
2433 
2434 /// Return true if sincos libcall is available.
2435 static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) {
2436   RTLIB::Libcall LC;
2437   switch (Node->getSimpleValueType(0).SimpleTy) {
2438   default: llvm_unreachable("Unexpected request for libcall!");
2439   case MVT::f32:     LC = RTLIB::SINCOS_F32; break;
2440   case MVT::f64:     LC = RTLIB::SINCOS_F64; break;
2441   case MVT::f80:     LC = RTLIB::SINCOS_F80; break;
2442   case MVT::f128:    LC = RTLIB::SINCOS_F128; break;
2443   case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
2444   }
2445   return TLI.getLibcallName(LC) != nullptr;
2446 }
2447 
2448 /// Return true if sincos libcall is available and can be used to combine sin
2449 /// and cos.
2450 static bool canCombineSinCosLibcall(SDNode *Node, const TargetLowering &TLI,
2451                                     const TargetMachine &TM) {
2452   if (!isSinCosLibcallAvailable(Node, TLI))
2453     return false;
2454   // GNU sin/cos functions set errno while sincos does not. Therefore
2455   // combining sin and cos is only safe if unsafe-fpmath is enabled.
2456   bool isGNU = Triple(TM.getTargetTriple()).getEnvironment() == Triple::GNU;
2457   if (isGNU && !TM.Options.UnsafeFPMath)
2458     return false;
2459   return true;
2460 }
2461 
2462 /// Only issue sincos libcall if both sin and cos are needed.
2463 static bool useSinCos(SDNode *Node) {
2464   unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
2465     ? ISD::FCOS : ISD::FSIN;
2466 
2467   SDValue Op0 = Node->getOperand(0);
2468   for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
2469        UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
2470     SDNode *User = *UI;
2471     if (User == Node)
2472       continue;
2473     // The other user might have been turned into sincos already.
2474     if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
2475       return true;
2476   }
2477   return false;
2478 }
2479 
2480 /// Issue libcalls to sincos to compute sin / cos pairs.
2481 void
2482 SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node,
2483                                           SmallVectorImpl<SDValue> &Results) {
2484   RTLIB::Libcall LC;
2485   switch (Node->getSimpleValueType(0).SimpleTy) {
2486   default: llvm_unreachable("Unexpected request for libcall!");
2487   case MVT::f32:     LC = RTLIB::SINCOS_F32; break;
2488   case MVT::f64:     LC = RTLIB::SINCOS_F64; break;
2489   case MVT::f80:     LC = RTLIB::SINCOS_F80; break;
2490   case MVT::f128:    LC = RTLIB::SINCOS_F128; break;
2491   case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
2492   }
2493 
2494   // The input chain to this libcall is the entry node of the function.
2495   // Legalizing the call will automatically add the previous call to the
2496   // dependence.
2497   SDValue InChain = DAG.getEntryNode();
2498 
2499   EVT RetVT = Node->getValueType(0);
2500   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2501 
2502   TargetLowering::ArgListTy Args;
2503   TargetLowering::ArgListEntry Entry;
2504 
2505   // Pass the argument.
2506   Entry.Node = Node->getOperand(0);
2507   Entry.Ty = RetTy;
2508   Entry.isSExt = false;
2509   Entry.isZExt = false;
2510   Args.push_back(Entry);
2511 
2512   // Pass the return address of sin.
2513   SDValue SinPtr = DAG.CreateStackTemporary(RetVT);
2514   Entry.Node = SinPtr;
2515   Entry.Ty = RetTy->getPointerTo();
2516   Entry.isSExt = false;
2517   Entry.isZExt = false;
2518   Args.push_back(Entry);
2519 
2520   // Also pass the return address of the cos.
2521   SDValue CosPtr = DAG.CreateStackTemporary(RetVT);
2522   Entry.Node = CosPtr;
2523   Entry.Ty = RetTy->getPointerTo();
2524   Entry.isSExt = false;
2525   Entry.isZExt = false;
2526   Args.push_back(Entry);
2527 
2528   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2529                                          TLI.getPointerTy(DAG.getDataLayout()));
2530 
2531   SDLoc dl(Node);
2532   TargetLowering::CallLoweringInfo CLI(DAG);
2533   CLI.setDebugLoc(dl).setChain(InChain)
2534     .setCallee(TLI.getLibcallCallingConv(LC),
2535                Type::getVoidTy(*DAG.getContext()), Callee, std::move(Args), 0);
2536 
2537   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2538 
2539   Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr,
2540                                 MachinePointerInfo(), false, false, false, 0));
2541   Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr,
2542                                 MachinePointerInfo(), false, false, false, 0));
2543 }
2544 
2545 /// This function is responsible for legalizing a
2546 /// INT_TO_FP operation of the specified operand when the target requests that
2547 /// we expand it.  At this point, we know that the result and operand types are
2548 /// legal for the target.
2549 SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2550                                                    SDValue Op0,
2551                                                    EVT DestVT,
2552                                                    SDLoc dl) {
2553   // TODO: Should any fast-math-flags be set for the created nodes?
2554 
2555   if (Op0.getValueType() == MVT::i32 && TLI.isTypeLegal(MVT::f64)) {
2556     // simple 32-bit [signed|unsigned] integer to float/double expansion
2557 
2558     // Get the stack frame index of a 8 byte buffer.
2559     SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
2560 
2561     // word offset constant for Hi/Lo address computation
2562     SDValue WordOff = DAG.getConstant(sizeof(int), dl,
2563                                       StackSlot.getValueType());
2564     // set up Hi and Lo (into buffer) address based on endian
2565     SDValue Hi = StackSlot;
2566     SDValue Lo = DAG.getNode(ISD::ADD, dl, StackSlot.getValueType(),
2567                              StackSlot, WordOff);
2568     if (DAG.getDataLayout().isLittleEndian())
2569       std::swap(Hi, Lo);
2570 
2571     // if signed map to unsigned space
2572     SDValue Op0Mapped;
2573     if (isSigned) {
2574       // constant used to invert sign bit (signed to unsigned mapping)
2575       SDValue SignBit = DAG.getConstant(0x80000000u, dl, MVT::i32);
2576       Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
2577     } else {
2578       Op0Mapped = Op0;
2579     }
2580     // store the lo of the constructed double - based on integer input
2581     SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
2582                                   Op0Mapped, Lo, MachinePointerInfo(),
2583                                   false, false, 0);
2584     // initial hi portion of constructed double
2585     SDValue InitialHi = DAG.getConstant(0x43300000u, dl, MVT::i32);
2586     // store the hi of the constructed double - biased exponent
2587     SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2588                                   MachinePointerInfo(),
2589                                   false, false, 0);
2590     // load the constructed double
2591     SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2592                                MachinePointerInfo(), false, false, false, 0);
2593     // FP constant to bias correct the final result
2594     SDValue Bias = DAG.getConstantFP(isSigned ?
2595                                      BitsToDouble(0x4330000080000000ULL) :
2596                                      BitsToDouble(0x4330000000000000ULL),
2597                                      dl, MVT::f64);
2598     // subtract the bias
2599     SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
2600     // final result
2601     SDValue Result;
2602     // handle final rounding
2603     if (DestVT == MVT::f64) {
2604       // do nothing
2605       Result = Sub;
2606     } else if (DestVT.bitsLT(MVT::f64)) {
2607       Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
2608                            DAG.getIntPtrConstant(0, dl));
2609     } else if (DestVT.bitsGT(MVT::f64)) {
2610       Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
2611     }
2612     return Result;
2613   }
2614   assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
2615   // Code below here assumes !isSigned without checking again.
2616 
2617   // Implementation of unsigned i64 to f64 following the algorithm in
2618   // __floatundidf in compiler_rt. This implementation has the advantage
2619   // of performing rounding correctly, both in the default rounding mode
2620   // and in all alternate rounding modes.
2621   // TODO: Generalize this for use with other types.
2622   if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2623     SDValue TwoP52 =
2624       DAG.getConstant(UINT64_C(0x4330000000000000), dl, MVT::i64);
2625     SDValue TwoP84PlusTwoP52 =
2626       DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), dl,
2627                         MVT::f64);
2628     SDValue TwoP84 =
2629       DAG.getConstant(UINT64_C(0x4530000000000000), dl, MVT::i64);
2630 
2631     SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2632     SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2633                              DAG.getConstant(32, dl, MVT::i64));
2634     SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2635     SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
2636     SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2637     SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
2638     SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2639                                 TwoP84PlusTwoP52);
2640     return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2641   }
2642 
2643   // Implementation of unsigned i64 to f32.
2644   // TODO: Generalize this for use with other types.
2645   if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
2646     // For unsigned conversions, convert them to signed conversions using the
2647     // algorithm from the x86_64 __floatundidf in compiler_rt.
2648     if (!isSigned) {
2649       SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
2650 
2651       SDValue ShiftConst = DAG.getConstant(
2652           1, dl, TLI.getShiftAmountTy(Op0.getValueType(), DAG.getDataLayout()));
2653       SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2654       SDValue AndConst = DAG.getConstant(1, dl, MVT::i64);
2655       SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2656       SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
2657 
2658       SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2659       SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
2660 
2661       // TODO: This really should be implemented using a branch rather than a
2662       // select.  We happen to get lucky and machinesink does the right
2663       // thing most of the time.  This would be a good candidate for a
2664       //pseudo-op, or, even better, for whole-function isel.
2665       SDValue SignBitTest = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
2666         Op0, DAG.getConstant(0, dl, MVT::i64), ISD::SETLT);
2667       return DAG.getSelect(dl, MVT::f32, SignBitTest, Slow, Fast);
2668     }
2669 
2670     // Otherwise, implement the fully general conversion.
2671 
2672     SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
2673          DAG.getConstant(UINT64_C(0xfffffffffffff800), dl, MVT::i64));
2674     SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2675          DAG.getConstant(UINT64_C(0x800), dl, MVT::i64));
2676     SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
2677          DAG.getConstant(UINT64_C(0x7ff), dl, MVT::i64));
2678     SDValue Ne = DAG.getSetCC(dl, getSetCCResultType(MVT::i64), And2,
2679                               DAG.getConstant(UINT64_C(0), dl, MVT::i64),
2680                               ISD::SETNE);
2681     SDValue Sel = DAG.getSelect(dl, MVT::i64, Ne, Or, Op0);
2682     SDValue Ge = DAG.getSetCC(dl, getSetCCResultType(MVT::i64), Op0,
2683                               DAG.getConstant(UINT64_C(0x0020000000000000), dl,
2684                                               MVT::i64),
2685                               ISD::SETUGE);
2686     SDValue Sel2 = DAG.getSelect(dl, MVT::i64, Ge, Sel, Op0);
2687     EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType(), DAG.getDataLayout());
2688 
2689     SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2690                              DAG.getConstant(32, dl, SHVT));
2691     SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2692     SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2693     SDValue TwoP32 =
2694       DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), dl,
2695                         MVT::f64);
2696     SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2697     SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2698     SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2699     SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2700     return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2701                        DAG.getIntPtrConstant(0, dl));
2702   }
2703 
2704   SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
2705 
2706   SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(Op0.getValueType()),
2707                                  Op0,
2708                                  DAG.getConstant(0, dl, Op0.getValueType()),
2709                                  ISD::SETLT);
2710   SDValue Zero = DAG.getIntPtrConstant(0, dl),
2711           Four = DAG.getIntPtrConstant(4, dl);
2712   SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
2713                                     SignSet, Four, Zero);
2714 
2715   // If the sign bit of the integer is set, the large number will be treated
2716   // as a negative number.  To counteract this, the dynamic code adds an
2717   // offset depending on the data type.
2718   uint64_t FF;
2719   switch (Op0.getSimpleValueType().SimpleTy) {
2720   default: llvm_unreachable("Unsupported integer type!");
2721   case MVT::i8 : FF = 0x43800000ULL; break;  // 2^8  (as a float)
2722   case MVT::i16: FF = 0x47800000ULL; break;  // 2^16 (as a float)
2723   case MVT::i32: FF = 0x4F800000ULL; break;  // 2^32 (as a float)
2724   case MVT::i64: FF = 0x5F800000ULL; break;  // 2^64 (as a float)
2725   }
2726   if (DAG.getDataLayout().isLittleEndian())
2727     FF <<= 32;
2728   Constant *FudgeFactor = ConstantInt::get(
2729                                        Type::getInt64Ty(*DAG.getContext()), FF);
2730 
2731   SDValue CPIdx =
2732       DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()));
2733   unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2734   CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
2735   Alignment = std::min(Alignment, 4u);
2736   SDValue FudgeInReg;
2737   if (DestVT == MVT::f32)
2738     FudgeInReg = DAG.getLoad(
2739         MVT::f32, dl, DAG.getEntryNode(), CPIdx,
2740         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2741         false, false, Alignment);
2742   else {
2743     SDValue Load = DAG.getExtLoad(
2744         ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx,
2745         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
2746         false, false, false, Alignment);
2747     HandleSDNode Handle(Load);
2748     LegalizeOp(Load.getNode());
2749     FudgeInReg = Handle.getValue();
2750   }
2751 
2752   return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
2753 }
2754 
2755 /// This function is responsible for legalizing a
2756 /// *INT_TO_FP operation of the specified operand when the target requests that
2757 /// we promote it.  At this point, we know that the result and operand types are
2758 /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2759 /// operation that takes a larger input.
2760 SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
2761                                                     EVT DestVT,
2762                                                     bool isSigned,
2763                                                     SDLoc dl) {
2764   // First step, figure out the appropriate *INT_TO_FP operation to use.
2765   EVT NewInTy = LegalOp.getValueType();
2766 
2767   unsigned OpToUse = 0;
2768 
2769   // Scan for the appropriate larger type to use.
2770   while (1) {
2771     NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
2772     assert(NewInTy.isInteger() && "Ran out of possibilities!");
2773 
2774     // If the target supports SINT_TO_FP of this type, use it.
2775     if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2776       OpToUse = ISD::SINT_TO_FP;
2777       break;
2778     }
2779     if (isSigned) continue;
2780 
2781     // If the target supports UINT_TO_FP of this type, use it.
2782     if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2783       OpToUse = ISD::UINT_TO_FP;
2784       break;
2785     }
2786 
2787     // Otherwise, try a larger type.
2788   }
2789 
2790   // Okay, we found the operation and type to use.  Zero extend our input to the
2791   // desired type then run the operation on it.
2792   return DAG.getNode(OpToUse, dl, DestVT,
2793                      DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2794                                  dl, NewInTy, LegalOp));
2795 }
2796 
2797 /// This function is responsible for legalizing a
2798 /// FP_TO_*INT operation of the specified operand when the target requests that
2799 /// we promote it.  At this point, we know that the result and operand types are
2800 /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2801 /// operation that returns a larger result.
2802 SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
2803                                                     EVT DestVT,
2804                                                     bool isSigned,
2805                                                     SDLoc dl) {
2806   // First step, figure out the appropriate FP_TO*INT operation to use.
2807   EVT NewOutTy = DestVT;
2808 
2809   unsigned OpToUse = 0;
2810 
2811   // Scan for the appropriate larger type to use.
2812   while (1) {
2813     NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
2814     assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2815 
2816     // A larger signed type can hold all unsigned values of the requested type,
2817     // so using FP_TO_SINT is valid
2818     if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
2819       OpToUse = ISD::FP_TO_SINT;
2820       break;
2821     }
2822 
2823     // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
2824     if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
2825       OpToUse = ISD::FP_TO_UINT;
2826       break;
2827     }
2828 
2829     // Otherwise, try a larger type.
2830   }
2831 
2832 
2833   // Okay, we found the operation and type to use.
2834   SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
2835 
2836   // Truncate the result of the extended FP_TO_*INT operation to the desired
2837   // size.
2838   return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
2839 }
2840 
2841 /// Open code the operations for BITREVERSE.
2842 SDValue SelectionDAGLegalize::ExpandBITREVERSE(SDValue Op, SDLoc dl) {
2843   EVT VT = Op.getValueType();
2844   EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2845   unsigned Sz = VT.getScalarSizeInBits();
2846 
2847   SDValue Tmp, Tmp2;
2848   Tmp = DAG.getConstant(0, dl, VT);
2849   for (unsigned I = 0, J = Sz-1; I < Sz; ++I, --J) {
2850     if (I < J)
2851       Tmp2 =
2852           DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(J - I, dl, SHVT));
2853     else
2854       Tmp2 =
2855           DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(I - J, dl, SHVT));
2856 
2857     APInt Shift(Sz, 1);
2858     Shift = Shift.shl(J);
2859     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(Shift, dl, VT));
2860     Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp, Tmp2);
2861   }
2862 
2863   return Tmp;
2864 }
2865 
2866 /// Open code the operations for BSWAP of the specified operation.
2867 SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, SDLoc dl) {
2868   EVT VT = Op.getValueType();
2869   EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2870   SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
2871   switch (VT.getSimpleVT().SimpleTy) {
2872   default: llvm_unreachable("Unhandled Expand type in BSWAP!");
2873   case MVT::i16:
2874     Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2875     Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2876     return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2877   case MVT::i32:
2878     Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2879     Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2880     Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2881     Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2882     Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3,
2883                        DAG.getConstant(0xFF0000, dl, VT));
2884     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, dl, VT));
2885     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2886     Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2887     return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2888   case MVT::i64:
2889     Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, dl, SHVT));
2890     Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, dl, SHVT));
2891     Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2892     Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2893     Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2894     Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2895     Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, dl, SHVT));
2896     Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, dl, SHVT));
2897     Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7,
2898                        DAG.getConstant(255ULL<<48, dl, VT));
2899     Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6,
2900                        DAG.getConstant(255ULL<<40, dl, VT));
2901     Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5,
2902                        DAG.getConstant(255ULL<<32, dl, VT));
2903     Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4,
2904                        DAG.getConstant(255ULL<<24, dl, VT));
2905     Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3,
2906                        DAG.getConstant(255ULL<<16, dl, VT));
2907     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2,
2908                        DAG.getConstant(255ULL<<8 , dl, VT));
2909     Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2910     Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2911     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2912     Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2913     Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2914     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2915     return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
2916   }
2917 }
2918 
2919 /// Expand the specified bitcount instruction into operations.
2920 SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
2921                                              SDLoc dl) {
2922   switch (Opc) {
2923   default: llvm_unreachable("Cannot expand this yet!");
2924   case ISD::CTPOP: {
2925     EVT VT = Op.getValueType();
2926     EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2927     unsigned Len = VT.getSizeInBits();
2928 
2929     assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2930            "CTPOP not implemented for this type.");
2931 
2932     // This is the "best" algorithm from
2933     // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2934 
2935     SDValue Mask55 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x55)),
2936                                      dl, VT);
2937     SDValue Mask33 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x33)),
2938                                      dl, VT);
2939     SDValue Mask0F = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x0F)),
2940                                      dl, VT);
2941     SDValue Mask01 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x01)),
2942                                      dl, VT);
2943 
2944     // v = v - ((v >> 1) & 0x55555555...)
2945     Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2946                      DAG.getNode(ISD::AND, dl, VT,
2947                                  DAG.getNode(ISD::SRL, dl, VT, Op,
2948                                              DAG.getConstant(1, dl, ShVT)),
2949                                  Mask55));
2950     // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2951     Op = DAG.getNode(ISD::ADD, dl, VT,
2952                      DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2953                      DAG.getNode(ISD::AND, dl, VT,
2954                                  DAG.getNode(ISD::SRL, dl, VT, Op,
2955                                              DAG.getConstant(2, dl, ShVT)),
2956                                  Mask33));
2957     // v = (v + (v >> 4)) & 0x0F0F0F0F...
2958     Op = DAG.getNode(ISD::AND, dl, VT,
2959                      DAG.getNode(ISD::ADD, dl, VT, Op,
2960                                  DAG.getNode(ISD::SRL, dl, VT, Op,
2961                                              DAG.getConstant(4, dl, ShVT))),
2962                      Mask0F);
2963     // v = (v * 0x01010101...) >> (Len - 8)
2964     Op = DAG.getNode(ISD::SRL, dl, VT,
2965                      DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2966                      DAG.getConstant(Len - 8, dl, ShVT));
2967 
2968     return Op;
2969   }
2970   case ISD::CTLZ_ZERO_UNDEF:
2971     // This trivially expands to CTLZ.
2972     return DAG.getNode(ISD::CTLZ, dl, Op.getValueType(), Op);
2973   case ISD::CTLZ: {
2974     EVT VT = Op.getValueType();
2975     unsigned len = VT.getSizeInBits();
2976 
2977     if (TLI.isOperationLegalOrCustom(ISD::CTLZ_ZERO_UNDEF, VT)) {
2978       EVT SetCCVT = getSetCCResultType(VT);
2979       SDValue CTLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, VT, Op);
2980       SDValue Zero = DAG.getConstant(0, dl, VT);
2981       SDValue SrcIsZero = DAG.getSetCC(dl, SetCCVT, Op, Zero, ISD::SETEQ);
2982       return DAG.getNode(ISD::SELECT, dl, VT, SrcIsZero,
2983                          DAG.getConstant(len, dl, VT), CTLZ);
2984     }
2985 
2986     // for now, we do this:
2987     // x = x | (x >> 1);
2988     // x = x | (x >> 2);
2989     // ...
2990     // x = x | (x >>16);
2991     // x = x | (x >>32); // for 64-bit input
2992     // return popcount(~x);
2993     //
2994     // Ref: "Hacker's Delight" by Henry Warren
2995     EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2996     for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2997       SDValue Tmp3 = DAG.getConstant(1ULL << i, dl, ShVT);
2998       Op = DAG.getNode(ISD::OR, dl, VT, Op,
2999                        DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
3000     }
3001     Op = DAG.getNOT(dl, Op, VT);
3002     return DAG.getNode(ISD::CTPOP, dl, VT, Op);
3003   }
3004   case ISD::CTTZ_ZERO_UNDEF:
3005     // This trivially expands to CTTZ.
3006     return DAG.getNode(ISD::CTTZ, dl, Op.getValueType(), Op);
3007   case ISD::CTTZ: {
3008     // for now, we use: { return popcount(~x & (x - 1)); }
3009     // unless the target has ctlz but not ctpop, in which case we use:
3010     // { return 32 - nlz(~x & (x-1)); }
3011     // Ref: "Hacker's Delight" by Henry Warren
3012     EVT VT = Op.getValueType();
3013     SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
3014                                DAG.getNOT(dl, Op, VT),
3015                                DAG.getNode(ISD::SUB, dl, VT, Op,
3016                                            DAG.getConstant(1, dl, VT)));
3017     // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3018     if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
3019         TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
3020       return DAG.getNode(ISD::SUB, dl, VT,
3021                          DAG.getConstant(VT.getSizeInBits(), dl, VT),
3022                          DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
3023     return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
3024   }
3025   }
3026 }
3027 
3028 bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
3029   SmallVector<SDValue, 8> Results;
3030   SDLoc dl(Node);
3031   SDValue Tmp1, Tmp2, Tmp3, Tmp4;
3032   bool NeedInvert;
3033   switch (Node->getOpcode()) {
3034   case ISD::CTPOP:
3035   case ISD::CTLZ:
3036   case ISD::CTLZ_ZERO_UNDEF:
3037   case ISD::CTTZ:
3038   case ISD::CTTZ_ZERO_UNDEF:
3039     Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
3040     Results.push_back(Tmp1);
3041     break;
3042   case ISD::BITREVERSE:
3043     Results.push_back(ExpandBITREVERSE(Node->getOperand(0), dl));
3044     break;
3045   case ISD::BSWAP:
3046     Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
3047     break;
3048   case ISD::FRAMEADDR:
3049   case ISD::RETURNADDR:
3050   case ISD::FRAME_TO_ARGS_OFFSET:
3051     Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
3052     break;
3053   case ISD::FLT_ROUNDS_:
3054     Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
3055     break;
3056   case ISD::EH_RETURN:
3057   case ISD::EH_LABEL:
3058   case ISD::PREFETCH:
3059   case ISD::VAEND:
3060   case ISD::EH_SJLJ_LONGJMP:
3061     // If the target didn't expand these, there's nothing to do, so just
3062     // preserve the chain and be done.
3063     Results.push_back(Node->getOperand(0));
3064     break;
3065   case ISD::READCYCLECOUNTER:
3066     // If the target didn't expand this, just return 'zero' and preserve the
3067     // chain.
3068     Results.append(Node->getNumValues() - 1,
3069                    DAG.getConstant(0, dl, Node->getValueType(0)));
3070     Results.push_back(Node->getOperand(0));
3071     break;
3072   case ISD::EH_SJLJ_SETJMP:
3073     // If the target didn't expand this, just return 'zero' and preserve the
3074     // chain.
3075     Results.push_back(DAG.getConstant(0, dl, MVT::i32));
3076     Results.push_back(Node->getOperand(0));
3077     break;
3078   case ISD::ATOMIC_LOAD: {
3079     // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
3080     SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0));
3081     SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3082     SDValue Swap = DAG.getAtomicCmpSwap(
3083         ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3084         Node->getOperand(0), Node->getOperand(1), Zero, Zero,
3085         cast<AtomicSDNode>(Node)->getMemOperand(),
3086         cast<AtomicSDNode>(Node)->getOrdering(),
3087         cast<AtomicSDNode>(Node)->getOrdering(),
3088         cast<AtomicSDNode>(Node)->getSynchScope());
3089     Results.push_back(Swap.getValue(0));
3090     Results.push_back(Swap.getValue(1));
3091     break;
3092   }
3093   case ISD::ATOMIC_STORE: {
3094     // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
3095     SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
3096                                  cast<AtomicSDNode>(Node)->getMemoryVT(),
3097                                  Node->getOperand(0),
3098                                  Node->getOperand(1), Node->getOperand(2),
3099                                  cast<AtomicSDNode>(Node)->getMemOperand(),
3100                                  cast<AtomicSDNode>(Node)->getOrdering(),
3101                                  cast<AtomicSDNode>(Node)->getSynchScope());
3102     Results.push_back(Swap.getValue(1));
3103     break;
3104   }
3105   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
3106     // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
3107     // splits out the success value as a comparison. Expanding the resulting
3108     // ATOMIC_CMP_SWAP will produce a libcall.
3109     SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3110     SDValue Res = DAG.getAtomicCmpSwap(
3111         ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3112         Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
3113         Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand(),
3114         cast<AtomicSDNode>(Node)->getSuccessOrdering(),
3115         cast<AtomicSDNode>(Node)->getFailureOrdering(),
3116         cast<AtomicSDNode>(Node)->getSynchScope());
3117 
3118     SDValue Success = DAG.getSetCC(SDLoc(Node), Node->getValueType(1),
3119                                    Res, Node->getOperand(2), ISD::SETEQ);
3120 
3121     Results.push_back(Res.getValue(0));
3122     Results.push_back(Success);
3123     Results.push_back(Res.getValue(1));
3124     break;
3125   }
3126   case ISD::DYNAMIC_STACKALLOC:
3127     ExpandDYNAMIC_STACKALLOC(Node, Results);
3128     break;
3129   case ISD::MERGE_VALUES:
3130     for (unsigned i = 0; i < Node->getNumValues(); i++)
3131       Results.push_back(Node->getOperand(i));
3132     break;
3133   case ISD::UNDEF: {
3134     EVT VT = Node->getValueType(0);
3135     if (VT.isInteger())
3136       Results.push_back(DAG.getConstant(0, dl, VT));
3137     else {
3138       assert(VT.isFloatingPoint() && "Unknown value type!");
3139       Results.push_back(DAG.getConstantFP(0, dl, VT));
3140     }
3141     break;
3142   }
3143   case ISD::FP_ROUND:
3144   case ISD::BITCAST:
3145     Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3146                             Node->getValueType(0), dl);
3147     Results.push_back(Tmp1);
3148     break;
3149   case ISD::FP_EXTEND:
3150     Tmp1 = EmitStackConvert(Node->getOperand(0),
3151                             Node->getOperand(0).getValueType(),
3152                             Node->getValueType(0), dl);
3153     Results.push_back(Tmp1);
3154     break;
3155   case ISD::SIGN_EXTEND_INREG: {
3156     // NOTE: we could fall back on load/store here too for targets without
3157     // SAR.  However, it is doubtful that any exist.
3158     EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3159     EVT VT = Node->getValueType(0);
3160     EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
3161     if (VT.isVector())
3162       ShiftAmountTy = VT;
3163     unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
3164                         ExtraVT.getScalarType().getSizeInBits();
3165     SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy);
3166     Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3167                        Node->getOperand(0), ShiftCst);
3168     Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3169     Results.push_back(Tmp1);
3170     break;
3171   }
3172   case ISD::FP_ROUND_INREG: {
3173     // The only way we can lower this is to turn it into a TRUNCSTORE,
3174     // EXTLOAD pair, targeting a temporary location (a stack slot).
3175 
3176     // NOTE: there is a choice here between constantly creating new stack
3177     // slots and always reusing the same one.  We currently always create
3178     // new ones, as reuse may inhibit scheduling.
3179     EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3180     Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
3181                             Node->getValueType(0), dl);
3182     Results.push_back(Tmp1);
3183     break;
3184   }
3185   case ISD::SINT_TO_FP:
3186   case ISD::UINT_TO_FP:
3187     Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
3188                                 Node->getOperand(0), Node->getValueType(0), dl);
3189     Results.push_back(Tmp1);
3190     break;
3191   case ISD::FP_TO_SINT:
3192     if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
3193       Results.push_back(Tmp1);
3194     break;
3195   case ISD::FP_TO_UINT: {
3196     SDValue True, False;
3197     EVT VT =  Node->getOperand(0).getValueType();
3198     EVT NVT = Node->getValueType(0);
3199     APFloat apf(DAG.EVTToAPFloatSemantics(VT),
3200                 APInt::getNullValue(VT.getSizeInBits()));
3201     APInt x = APInt::getSignBit(NVT.getSizeInBits());
3202     (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3203     Tmp1 = DAG.getConstantFP(apf, dl, VT);
3204     Tmp2 = DAG.getSetCC(dl, getSetCCResultType(VT),
3205                         Node->getOperand(0),
3206                         Tmp1, ISD::SETLT);
3207     True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
3208     // TODO: Should any fast-math-flags be set for the FSUB?
3209     False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3210                         DAG.getNode(ISD::FSUB, dl, VT,
3211                                     Node->getOperand(0), Tmp1));
3212     False = DAG.getNode(ISD::XOR, dl, NVT, False,
3213                         DAG.getConstant(x, dl, NVT));
3214     Tmp1 = DAG.getSelect(dl, NVT, Tmp2, True, False);
3215     Results.push_back(Tmp1);
3216     break;
3217   }
3218   case ISD::VAARG:
3219     Results.push_back(DAG.expandVAArg(Node));
3220     Results.push_back(Results[0].getValue(1));
3221     break;
3222   case ISD::VACOPY:
3223     Results.push_back(DAG.expandVACopy(Node));
3224     break;
3225   case ISD::EXTRACT_VECTOR_ELT:
3226     if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
3227       // This must be an access of the only element.  Return it.
3228       Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
3229                          Node->getOperand(0));
3230     else
3231       Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3232     Results.push_back(Tmp1);
3233     break;
3234   case ISD::EXTRACT_SUBVECTOR:
3235     Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
3236     break;
3237   case ISD::INSERT_SUBVECTOR:
3238     Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
3239     break;
3240   case ISD::CONCAT_VECTORS: {
3241     Results.push_back(ExpandVectorBuildThroughStack(Node));
3242     break;
3243   }
3244   case ISD::SCALAR_TO_VECTOR:
3245     Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
3246     break;
3247   case ISD::INSERT_VECTOR_ELT:
3248     Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
3249                                               Node->getOperand(1),
3250                                               Node->getOperand(2), dl));
3251     break;
3252   case ISD::VECTOR_SHUFFLE: {
3253     SmallVector<int, 32> NewMask;
3254     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
3255 
3256     EVT VT = Node->getValueType(0);
3257     EVT EltVT = VT.getVectorElementType();
3258     SDValue Op0 = Node->getOperand(0);
3259     SDValue Op1 = Node->getOperand(1);
3260     if (!TLI.isTypeLegal(EltVT)) {
3261 
3262       EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
3263 
3264       // BUILD_VECTOR operands are allowed to be wider than the element type.
3265       // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
3266       // it.
3267       if (NewEltVT.bitsLT(EltVT)) {
3268 
3269         // Convert shuffle node.
3270         // If original node was v4i64 and the new EltVT is i32,
3271         // cast operands to v8i32 and re-build the mask.
3272 
3273         // Calculate new VT, the size of the new VT should be equal to original.
3274         EVT NewVT =
3275             EVT::getVectorVT(*DAG.getContext(), NewEltVT,
3276                              VT.getSizeInBits() / NewEltVT.getSizeInBits());
3277         assert(NewVT.bitsEq(VT));
3278 
3279         // cast operands to new VT
3280         Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
3281         Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
3282 
3283         // Convert the shuffle mask
3284         unsigned int factor =
3285                          NewVT.getVectorNumElements()/VT.getVectorNumElements();
3286 
3287         // EltVT gets smaller
3288         assert(factor > 0);
3289 
3290         for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
3291           if (Mask[i] < 0) {
3292             for (unsigned fi = 0; fi < factor; ++fi)
3293               NewMask.push_back(Mask[i]);
3294           }
3295           else {
3296             for (unsigned fi = 0; fi < factor; ++fi)
3297               NewMask.push_back(Mask[i]*factor+fi);
3298           }
3299         }
3300         Mask = NewMask;
3301         VT = NewVT;
3302       }
3303       EltVT = NewEltVT;
3304     }
3305     unsigned NumElems = VT.getVectorNumElements();
3306     SmallVector<SDValue, 16> Ops;
3307     for (unsigned i = 0; i != NumElems; ++i) {
3308       if (Mask[i] < 0) {
3309         Ops.push_back(DAG.getUNDEF(EltVT));
3310         continue;
3311       }
3312       unsigned Idx = Mask[i];
3313       if (Idx < NumElems)
3314         Ops.push_back(DAG.getNode(
3315             ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0,
3316             DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))));
3317       else
3318         Ops.push_back(DAG.getNode(
3319             ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1,
3320             DAG.getConstant(Idx - NumElems, dl,
3321                             TLI.getVectorIdxTy(DAG.getDataLayout()))));
3322     }
3323 
3324     Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
3325     // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
3326     Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
3327     Results.push_back(Tmp1);
3328     break;
3329   }
3330   case ISD::EXTRACT_ELEMENT: {
3331     EVT OpTy = Node->getOperand(0).getValueType();
3332     if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3333       // 1 -> Hi
3334       Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3335                          DAG.getConstant(OpTy.getSizeInBits() / 2, dl,
3336                                          TLI.getShiftAmountTy(
3337                                              Node->getOperand(0).getValueType(),
3338                                              DAG.getDataLayout())));
3339       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3340     } else {
3341       // 0 -> Lo
3342       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3343                          Node->getOperand(0));
3344     }
3345     Results.push_back(Tmp1);
3346     break;
3347   }
3348   case ISD::STACKSAVE:
3349     // Expand to CopyFromReg if the target set
3350     // StackPointerRegisterToSaveRestore.
3351     if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3352       Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3353                                            Node->getValueType(0)));
3354       Results.push_back(Results[0].getValue(1));
3355     } else {
3356       Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
3357       Results.push_back(Node->getOperand(0));
3358     }
3359     break;
3360   case ISD::STACKRESTORE:
3361     // Expand to CopyToReg if the target set
3362     // StackPointerRegisterToSaveRestore.
3363     if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3364       Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3365                                          Node->getOperand(1)));
3366     } else {
3367       Results.push_back(Node->getOperand(0));
3368     }
3369     break;
3370   case ISD::GET_DYNAMIC_AREA_OFFSET:
3371     Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
3372     Results.push_back(Results[0].getValue(0));
3373     break;
3374   case ISD::FCOPYSIGN:
3375     Results.push_back(ExpandFCOPYSIGN(Node));
3376     break;
3377   case ISD::FNEG:
3378     // Expand Y = FNEG(X) ->  Y = SUB -0.0, X
3379     Tmp1 = DAG.getConstantFP(-0.0, dl, Node->getValueType(0));
3380     // TODO: If FNEG has fast-math-flags, propagate them to the FSUB.
3381     Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3382                        Node->getOperand(0));
3383     Results.push_back(Tmp1);
3384     break;
3385   case ISD::FABS:
3386     Results.push_back(ExpandFABS(Node));
3387     break;
3388   case ISD::SMIN:
3389   case ISD::SMAX:
3390   case ISD::UMIN:
3391   case ISD::UMAX: {
3392     // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
3393     ISD::CondCode Pred;
3394     switch (Node->getOpcode()) {
3395     default: llvm_unreachable("How did we get here?");
3396     case ISD::SMAX: Pred = ISD::SETGT; break;
3397     case ISD::SMIN: Pred = ISD::SETLT; break;
3398     case ISD::UMAX: Pred = ISD::SETUGT; break;
3399     case ISD::UMIN: Pred = ISD::SETULT; break;
3400     }
3401     Tmp1 = Node->getOperand(0);
3402     Tmp2 = Node->getOperand(1);
3403     Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred);
3404     Results.push_back(Tmp1);
3405     break;
3406   }
3407 
3408   case ISD::FSIN:
3409   case ISD::FCOS: {
3410     EVT VT = Node->getValueType(0);
3411     // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
3412     // fcos which share the same operand and both are used.
3413     if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
3414          canCombineSinCosLibcall(Node, TLI, TM))
3415         && useSinCos(Node)) {
3416       SDVTList VTs = DAG.getVTList(VT, VT);
3417       Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
3418       if (Node->getOpcode() == ISD::FCOS)
3419         Tmp1 = Tmp1.getValue(1);
3420       Results.push_back(Tmp1);
3421     }
3422     break;
3423   }
3424   case ISD::FMAD:
3425     llvm_unreachable("Illegal fmad should never be formed");
3426 
3427   case ISD::FP16_TO_FP:
3428     if (Node->getValueType(0) != MVT::f32) {
3429       // We can extend to types bigger than f32 in two steps without changing
3430       // the result. Since "f16 -> f32" is much more commonly available, give
3431       // CodeGen the option of emitting that before resorting to a libcall.
3432       SDValue Res =
3433           DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
3434       Results.push_back(
3435           DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
3436     }
3437     break;
3438   case ISD::FP_TO_FP16:
3439     if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) {
3440       SDValue Op = Node->getOperand(0);
3441       MVT SVT = Op.getSimpleValueType();
3442       if ((SVT == MVT::f64 || SVT == MVT::f80) &&
3443           TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) {
3444         // Under fastmath, we can expand this node into a fround followed by
3445         // a float-half conversion.
3446         SDValue FloatVal = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3447                                        DAG.getIntPtrConstant(0, dl));
3448         Results.push_back(
3449             DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal));
3450       }
3451     }
3452     break;
3453   case ISD::ConstantFP: {
3454     ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
3455     // Check to see if this FP immediate is already legal.
3456     // If this is a legal constant, turn it into a TargetConstantFP node.
3457     if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
3458       Results.push_back(ExpandConstantFP(CFP, true));
3459     break;
3460   }
3461   case ISD::Constant: {
3462     ConstantSDNode *CP = cast<ConstantSDNode>(Node);
3463     Results.push_back(ExpandConstant(CP));
3464     break;
3465   }
3466   case ISD::FSUB: {
3467     EVT VT = Node->getValueType(0);
3468     if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
3469         TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) {
3470       const SDNodeFlags *Flags = &cast<BinaryWithFlagsSDNode>(Node)->Flags;
3471       Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
3472       Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags);
3473       Results.push_back(Tmp1);
3474     }
3475     break;
3476   }
3477   case ISD::SUB: {
3478     EVT VT = Node->getValueType(0);
3479     assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3480            TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3481            "Don't know how to expand this subtraction!");
3482     Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3483                DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
3484                                VT));
3485     Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT));
3486     Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
3487     break;
3488   }
3489   case ISD::UREM:
3490   case ISD::SREM: {
3491     EVT VT = Node->getValueType(0);
3492     bool isSigned = Node->getOpcode() == ISD::SREM;
3493     unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3494     unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3495     Tmp2 = Node->getOperand(0);
3496     Tmp3 = Node->getOperand(1);
3497     if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3498       SDVTList VTs = DAG.getVTList(VT, VT);
3499       Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3500       Results.push_back(Tmp1);
3501     } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
3502       // X % Y -> X-X/Y*Y
3503       Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3504       Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3505       Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
3506       Results.push_back(Tmp1);
3507     }
3508     break;
3509   }
3510   case ISD::UDIV:
3511   case ISD::SDIV: {
3512     bool isSigned = Node->getOpcode() == ISD::SDIV;
3513     unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3514     EVT VT = Node->getValueType(0);
3515     if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3516       SDVTList VTs = DAG.getVTList(VT, VT);
3517       Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3518                          Node->getOperand(1));
3519       Results.push_back(Tmp1);
3520     }
3521     break;
3522   }
3523   case ISD::MULHU:
3524   case ISD::MULHS: {
3525     unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3526                                                               ISD::SMUL_LOHI;
3527     EVT VT = Node->getValueType(0);
3528     SDVTList VTs = DAG.getVTList(VT, VT);
3529     assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3530            "If this wasn't legal, it shouldn't have been created!");
3531     Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3532                        Node->getOperand(1));
3533     Results.push_back(Tmp1.getValue(1));
3534     break;
3535   }
3536   case ISD::MUL: {
3537     EVT VT = Node->getValueType(0);
3538     SDVTList VTs = DAG.getVTList(VT, VT);
3539     // See if multiply or divide can be lowered using two-result operations.
3540     // We just need the low half of the multiply; try both the signed
3541     // and unsigned forms. If the target supports both SMUL_LOHI and
3542     // UMUL_LOHI, form a preference by checking which forms of plain
3543     // MULH it supports.
3544     bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3545     bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3546     bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3547     bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3548     unsigned OpToUse = 0;
3549     if (HasSMUL_LOHI && !HasMULHS) {
3550       OpToUse = ISD::SMUL_LOHI;
3551     } else if (HasUMUL_LOHI && !HasMULHU) {
3552       OpToUse = ISD::UMUL_LOHI;
3553     } else if (HasSMUL_LOHI) {
3554       OpToUse = ISD::SMUL_LOHI;
3555     } else if (HasUMUL_LOHI) {
3556       OpToUse = ISD::UMUL_LOHI;
3557     }
3558     if (OpToUse) {
3559       Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3560                                     Node->getOperand(1)));
3561       break;
3562     }
3563 
3564     SDValue Lo, Hi;
3565     EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
3566     if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) &&
3567         TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) &&
3568         TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
3569         TLI.isOperationLegalOrCustom(ISD::OR, VT) &&
3570         TLI.expandMUL(Node, Lo, Hi, HalfType, DAG)) {
3571       Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
3572       Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
3573       SDValue Shift =
3574           DAG.getConstant(HalfType.getSizeInBits(), dl,
3575                           TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
3576       Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
3577       Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
3578     }
3579     break;
3580   }
3581   case ISD::SADDO:
3582   case ISD::SSUBO: {
3583     SDValue LHS = Node->getOperand(0);
3584     SDValue RHS = Node->getOperand(1);
3585     SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3586                               ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3587                               LHS, RHS);
3588     Results.push_back(Sum);
3589     EVT ResultType = Node->getValueType(1);
3590     EVT OType = getSetCCResultType(Node->getValueType(0));
3591 
3592     SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType());
3593 
3594     //   LHSSign -> LHS >= 0
3595     //   RHSSign -> RHS >= 0
3596     //   SumSign -> Sum >= 0
3597     //
3598     //   Add:
3599     //   Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3600     //   Sub:
3601     //   Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3602     //
3603     SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3604     SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3605     SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3606                                       Node->getOpcode() == ISD::SADDO ?
3607                                       ISD::SETEQ : ISD::SETNE);
3608 
3609     SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3610     SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3611 
3612     SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3613     Results.push_back(DAG.getBoolExtOrTrunc(Cmp, dl, ResultType, ResultType));
3614     break;
3615   }
3616   case ISD::UADDO:
3617   case ISD::USUBO: {
3618     SDValue LHS = Node->getOperand(0);
3619     SDValue RHS = Node->getOperand(1);
3620     SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3621                               ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3622                               LHS, RHS);
3623     Results.push_back(Sum);
3624 
3625     EVT ResultType = Node->getValueType(1);
3626     EVT SetCCType = getSetCCResultType(Node->getValueType(0));
3627     ISD::CondCode CC
3628       = Node->getOpcode() == ISD::UADDO ? ISD::SETULT : ISD::SETUGT;
3629     SDValue SetCC = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
3630 
3631     Results.push_back(DAG.getBoolExtOrTrunc(SetCC, dl, ResultType, ResultType));
3632     break;
3633   }
3634   case ISD::UMULO:
3635   case ISD::SMULO: {
3636     EVT VT = Node->getValueType(0);
3637     EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
3638     SDValue LHS = Node->getOperand(0);
3639     SDValue RHS = Node->getOperand(1);
3640     SDValue BottomHalf;
3641     SDValue TopHalf;
3642     static const unsigned Ops[2][3] =
3643         { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3644           { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3645     bool isSigned = Node->getOpcode() == ISD::SMULO;
3646     if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3647       BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3648       TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3649     } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3650       BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3651                                RHS);
3652       TopHalf = BottomHalf.getValue(1);
3653     } else if (TLI.isTypeLegal(WideVT)) {
3654       LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3655       RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3656       Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3657       BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3658                                DAG.getIntPtrConstant(0, dl));
3659       TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3660                             DAG.getIntPtrConstant(1, dl));
3661     } else {
3662       // We can fall back to a libcall with an illegal type for the MUL if we
3663       // have a libcall big enough.
3664       // Also, we can fall back to a division in some cases, but that's a big
3665       // performance hit in the general case.
3666       RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3667       if (WideVT == MVT::i16)
3668         LC = RTLIB::MUL_I16;
3669       else if (WideVT == MVT::i32)
3670         LC = RTLIB::MUL_I32;
3671       else if (WideVT == MVT::i64)
3672         LC = RTLIB::MUL_I64;
3673       else if (WideVT == MVT::i128)
3674         LC = RTLIB::MUL_I128;
3675       assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
3676 
3677       // The high part is obtained by SRA'ing all but one of the bits of low
3678       // part.
3679       unsigned LoSize = VT.getSizeInBits();
3680       SDValue HiLHS =
3681           DAG.getNode(ISD::SRA, dl, VT, RHS,
3682                       DAG.getConstant(LoSize - 1, dl,
3683                                       TLI.getPointerTy(DAG.getDataLayout())));
3684       SDValue HiRHS =
3685           DAG.getNode(ISD::SRA, dl, VT, LHS,
3686                       DAG.getConstant(LoSize - 1, dl,
3687                                       TLI.getPointerTy(DAG.getDataLayout())));
3688 
3689       // Here we're passing the 2 arguments explicitly as 4 arguments that are
3690       // pre-lowered to the correct types. This all depends upon WideVT not
3691       // being a legal type for the architecture and thus has to be split to
3692       // two arguments.
3693       SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3694       SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3695       BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3696                                DAG.getIntPtrConstant(0, dl));
3697       TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3698                             DAG.getIntPtrConstant(1, dl));
3699       // Ret is a node with an illegal type. Because such things are not
3700       // generally permitted during this phase of legalization, make sure the
3701       // node has no more uses. The above EXTRACT_ELEMENT nodes should have been
3702       // folded.
3703       assert(Ret->use_empty() &&
3704              "Unexpected uses of illegally type from expanded lib call.");
3705     }
3706 
3707     if (isSigned) {
3708       Tmp1 = DAG.getConstant(
3709           VT.getSizeInBits() - 1, dl,
3710           TLI.getShiftAmountTy(BottomHalf.getValueType(), DAG.getDataLayout()));
3711       Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3712       TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf, Tmp1,
3713                              ISD::SETNE);
3714     } else {
3715       TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf,
3716                              DAG.getConstant(0, dl, VT), ISD::SETNE);
3717     }
3718     Results.push_back(BottomHalf);
3719     Results.push_back(TopHalf);
3720     break;
3721   }
3722   case ISD::BUILD_PAIR: {
3723     EVT PairTy = Node->getValueType(0);
3724     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3725     Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
3726     Tmp2 = DAG.getNode(
3727         ISD::SHL, dl, PairTy, Tmp2,
3728         DAG.getConstant(PairTy.getSizeInBits() / 2, dl,
3729                         TLI.getShiftAmountTy(PairTy, DAG.getDataLayout())));
3730     Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
3731     break;
3732   }
3733   case ISD::SELECT:
3734     Tmp1 = Node->getOperand(0);
3735     Tmp2 = Node->getOperand(1);
3736     Tmp3 = Node->getOperand(2);
3737     if (Tmp1.getOpcode() == ISD::SETCC) {
3738       Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3739                              Tmp2, Tmp3,
3740                              cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
3741     } else {
3742       Tmp1 = DAG.getSelectCC(dl, Tmp1,
3743                              DAG.getConstant(0, dl, Tmp1.getValueType()),
3744                              Tmp2, Tmp3, ISD::SETNE);
3745     }
3746     Results.push_back(Tmp1);
3747     break;
3748   case ISD::BR_JT: {
3749     SDValue Chain = Node->getOperand(0);
3750     SDValue Table = Node->getOperand(1);
3751     SDValue Index = Node->getOperand(2);
3752 
3753     EVT PTy = TLI.getPointerTy(DAG.getDataLayout());
3754 
3755     const DataLayout &TD = DAG.getDataLayout();
3756     unsigned EntrySize =
3757       DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
3758 
3759     Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
3760                         DAG.getConstant(EntrySize, dl, Index.getValueType()));
3761     SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(),
3762                                Index, Table);
3763 
3764     EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
3765     SDValue LD = DAG.getExtLoad(
3766         ISD::SEXTLOAD, dl, PTy, Chain, Addr,
3767         MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT,
3768         false, false, false, 0);
3769     Addr = LD;
3770     if (TM.getRelocationModel() == Reloc::PIC_) {
3771       // For PIC, the sequence is:
3772       // BRIND(load(Jumptable + index) + RelocBase)
3773       // RelocBase can be JumpTable, GOT or some sort of global base.
3774       Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3775                           TLI.getPICJumpTableRelocBase(Table, DAG));
3776     }
3777     Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
3778     Results.push_back(Tmp1);
3779     break;
3780   }
3781   case ISD::BRCOND:
3782     // Expand brcond's setcc into its constituent parts and create a BR_CC
3783     // Node.
3784     Tmp1 = Node->getOperand(0);
3785     Tmp2 = Node->getOperand(1);
3786     if (Tmp2.getOpcode() == ISD::SETCC) {
3787       Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
3788                          Tmp1, Tmp2.getOperand(2),
3789                          Tmp2.getOperand(0), Tmp2.getOperand(1),
3790                          Node->getOperand(2));
3791     } else {
3792       // We test only the i1 bit.  Skip the AND if UNDEF.
3793       Tmp3 = (Tmp2.isUndef()) ? Tmp2 :
3794         DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
3795                     DAG.getConstant(1, dl, Tmp2.getValueType()));
3796       Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
3797                          DAG.getCondCode(ISD::SETNE), Tmp3,
3798                          DAG.getConstant(0, dl, Tmp3.getValueType()),
3799                          Node->getOperand(2));
3800     }
3801     Results.push_back(Tmp1);
3802     break;
3803   case ISD::SETCC: {
3804     Tmp1 = Node->getOperand(0);
3805     Tmp2 = Node->getOperand(1);
3806     Tmp3 = Node->getOperand(2);
3807     bool Legalized = LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2,
3808                                            Tmp3, NeedInvert, dl);
3809 
3810     if (Legalized) {
3811       // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
3812       // condition code, create a new SETCC node.
3813       if (Tmp3.getNode())
3814         Tmp1 = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3815                            Tmp1, Tmp2, Tmp3);
3816 
3817       // If we expanded the SETCC by inverting the condition code, then wrap
3818       // the existing SETCC in a NOT to restore the intended condition.
3819       if (NeedInvert)
3820         Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
3821 
3822       Results.push_back(Tmp1);
3823       break;
3824     }
3825 
3826     // Otherwise, SETCC for the given comparison type must be completely
3827     // illegal; expand it into a SELECT_CC.
3828     EVT VT = Node->getValueType(0);
3829     int TrueValue;
3830     switch (TLI.getBooleanContents(Tmp1->getValueType(0))) {
3831     case TargetLowering::ZeroOrOneBooleanContent:
3832     case TargetLowering::UndefinedBooleanContent:
3833       TrueValue = 1;
3834       break;
3835     case TargetLowering::ZeroOrNegativeOneBooleanContent:
3836       TrueValue = -1;
3837       break;
3838     }
3839     Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3840                        DAG.getConstant(TrueValue, dl, VT),
3841                        DAG.getConstant(0, dl, VT),
3842                        Tmp3);
3843     Results.push_back(Tmp1);
3844     break;
3845   }
3846   case ISD::SELECT_CC: {
3847     Tmp1 = Node->getOperand(0);   // LHS
3848     Tmp2 = Node->getOperand(1);   // RHS
3849     Tmp3 = Node->getOperand(2);   // True
3850     Tmp4 = Node->getOperand(3);   // False
3851     EVT VT = Node->getValueType(0);
3852     SDValue CC = Node->getOperand(4);
3853     ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
3854 
3855     if (TLI.isCondCodeLegal(CCOp, Tmp1.getSimpleValueType())) {
3856       // If the condition code is legal, then we need to expand this
3857       // node using SETCC and SELECT.
3858       EVT CmpVT = Tmp1.getValueType();
3859       assert(!TLI.isOperationExpand(ISD::SELECT, VT) &&
3860              "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
3861              "expanded.");
3862       EVT CCVT =
3863           TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), CmpVT);
3864       SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC);
3865       Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4));
3866       break;
3867     }
3868 
3869     // SELECT_CC is legal, so the condition code must not be.
3870     bool Legalized = false;
3871     // Try to legalize by inverting the condition.  This is for targets that
3872     // might support an ordered version of a condition, but not the unordered
3873     // version (or vice versa).
3874     ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp,
3875                                                Tmp1.getValueType().isInteger());
3876     if (TLI.isCondCodeLegal(InvCC, Tmp1.getSimpleValueType())) {
3877       // Use the new condition code and swap true and false
3878       Legalized = true;
3879       Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC);
3880     } else {
3881       // If The inverse is not legal, then try to swap the arguments using
3882       // the inverse condition code.
3883       ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC);
3884       if (TLI.isCondCodeLegal(SwapInvCC, Tmp1.getSimpleValueType())) {
3885         // The swapped inverse condition is legal, so swap true and false,
3886         // lhs and rhs.
3887         Legalized = true;
3888         Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC);
3889       }
3890     }
3891 
3892     if (!Legalized) {
3893       Legalized = LegalizeSetCCCondCode(
3894           getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC, NeedInvert,
3895           dl);
3896 
3897       assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
3898 
3899       // If we expanded the SETCC by inverting the condition code, then swap
3900       // the True/False operands to match.
3901       if (NeedInvert)
3902         std::swap(Tmp3, Tmp4);
3903 
3904       // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
3905       // condition code, create a new SELECT_CC node.
3906       if (CC.getNode()) {
3907         Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0),
3908                            Tmp1, Tmp2, Tmp3, Tmp4, CC);
3909       } else {
3910         Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType());
3911         CC = DAG.getCondCode(ISD::SETNE);
3912         Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
3913                            Tmp2, Tmp3, Tmp4, CC);
3914       }
3915     }
3916     Results.push_back(Tmp1);
3917     break;
3918   }
3919   case ISD::BR_CC: {
3920     Tmp1 = Node->getOperand(0);              // Chain
3921     Tmp2 = Node->getOperand(2);              // LHS
3922     Tmp3 = Node->getOperand(3);              // RHS
3923     Tmp4 = Node->getOperand(1);              // CC
3924 
3925     bool Legalized = LegalizeSetCCCondCode(getSetCCResultType(
3926         Tmp2.getValueType()), Tmp2, Tmp3, Tmp4, NeedInvert, dl);
3927     (void)Legalized;
3928     assert(Legalized && "Can't legalize BR_CC with legal condition!");
3929 
3930     // If we expanded the SETCC by inverting the condition code, then wrap
3931     // the existing SETCC in a NOT to restore the intended condition.
3932     if (NeedInvert)
3933       Tmp4 = DAG.getNOT(dl, Tmp4, Tmp4->getValueType(0));
3934 
3935     // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
3936     // node.
3937     if (Tmp4.getNode()) {
3938       Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
3939                          Tmp4, Tmp2, Tmp3, Node->getOperand(4));
3940     } else {
3941       Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType());
3942       Tmp4 = DAG.getCondCode(ISD::SETNE);
3943       Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
3944                          Tmp2, Tmp3, Node->getOperand(4));
3945     }
3946     Results.push_back(Tmp1);
3947     break;
3948   }
3949   case ISD::BUILD_VECTOR:
3950     Results.push_back(ExpandBUILD_VECTOR(Node));
3951     break;
3952   case ISD::SRA:
3953   case ISD::SRL:
3954   case ISD::SHL: {
3955     // Scalarize vector SRA/SRL/SHL.
3956     EVT VT = Node->getValueType(0);
3957     assert(VT.isVector() && "Unable to legalize non-vector shift");
3958     assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
3959     unsigned NumElem = VT.getVectorNumElements();
3960 
3961     SmallVector<SDValue, 8> Scalars;
3962     for (unsigned Idx = 0; Idx < NumElem; Idx++) {
3963       SDValue Ex = DAG.getNode(
3964           ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(0),
3965           DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3966       SDValue Sh = DAG.getNode(
3967           ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(1),
3968           DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3969       Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
3970                                     VT.getScalarType(), Ex, Sh));
3971     }
3972     SDValue Result =
3973       DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0), Scalars);
3974     ReplaceNode(SDValue(Node, 0), Result);
3975     break;
3976   }
3977   case ISD::GLOBAL_OFFSET_TABLE:
3978   case ISD::GlobalAddress:
3979   case ISD::GlobalTLSAddress:
3980   case ISD::ExternalSymbol:
3981   case ISD::ConstantPool:
3982   case ISD::JumpTable:
3983   case ISD::INTRINSIC_W_CHAIN:
3984   case ISD::INTRINSIC_WO_CHAIN:
3985   case ISD::INTRINSIC_VOID:
3986     // FIXME: Custom lowering for these operations shouldn't return null!
3987     break;
3988   }
3989 
3990   // Replace the original node with the legalized result.
3991   if (Results.empty())
3992     return false;
3993 
3994   ReplaceNode(Node, Results.data());
3995   return true;
3996 }
3997 
3998 void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
3999   SmallVector<SDValue, 8> Results;
4000   SDLoc dl(Node);
4001   SDValue Tmp1, Tmp2, Tmp3, Tmp4;
4002   unsigned Opc = Node->getOpcode();
4003   switch (Opc) {
4004   case ISD::ATOMIC_FENCE: {
4005     // If the target didn't lower this, lower it to '__sync_synchronize()' call
4006     // FIXME: handle "fence singlethread" more efficiently.
4007     TargetLowering::ArgListTy Args;
4008 
4009     TargetLowering::CallLoweringInfo CLI(DAG);
4010     CLI.setDebugLoc(dl)
4011         .setChain(Node->getOperand(0))
4012         .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
4013                    DAG.getExternalSymbol("__sync_synchronize",
4014                                          TLI.getPointerTy(DAG.getDataLayout())),
4015                    std::move(Args), 0);
4016 
4017     std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
4018 
4019     Results.push_back(CallResult.second);
4020     break;
4021   }
4022   // By default, atomic intrinsics are marked Legal and lowered. Targets
4023   // which don't support them directly, however, may want libcalls, in which
4024   // case they mark them Expand, and we get here.
4025   case ISD::ATOMIC_SWAP:
4026   case ISD::ATOMIC_LOAD_ADD:
4027   case ISD::ATOMIC_LOAD_SUB:
4028   case ISD::ATOMIC_LOAD_AND:
4029   case ISD::ATOMIC_LOAD_OR:
4030   case ISD::ATOMIC_LOAD_XOR:
4031   case ISD::ATOMIC_LOAD_NAND:
4032   case ISD::ATOMIC_LOAD_MIN:
4033   case ISD::ATOMIC_LOAD_MAX:
4034   case ISD::ATOMIC_LOAD_UMIN:
4035   case ISD::ATOMIC_LOAD_UMAX:
4036   case ISD::ATOMIC_CMP_SWAP: {
4037     MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
4038     RTLIB::Libcall LC = RTLIB::getATOMIC(Opc, VT);
4039     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected atomic op or value type!");
4040 
4041     std::pair<SDValue, SDValue> Tmp = ExpandChainLibCall(LC, Node, false);
4042     Results.push_back(Tmp.first);
4043     Results.push_back(Tmp.second);
4044     break;
4045   }
4046   case ISD::TRAP: {
4047     // If this operation is not supported, lower it to 'abort()' call
4048     TargetLowering::ArgListTy Args;
4049     TargetLowering::CallLoweringInfo CLI(DAG);
4050     CLI.setDebugLoc(dl)
4051         .setChain(Node->getOperand(0))
4052         .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
4053                    DAG.getExternalSymbol("abort",
4054                                          TLI.getPointerTy(DAG.getDataLayout())),
4055                    std::move(Args), 0);
4056     std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
4057 
4058     Results.push_back(CallResult.second);
4059     break;
4060   }
4061   case ISD::FMINNUM:
4062     Results.push_back(ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64,
4063                                       RTLIB::FMIN_F80, RTLIB::FMIN_F128,
4064                                       RTLIB::FMIN_PPCF128));
4065     break;
4066   case ISD::FMAXNUM:
4067     Results.push_back(ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64,
4068                                       RTLIB::FMAX_F80, RTLIB::FMAX_F128,
4069                                       RTLIB::FMAX_PPCF128));
4070     break;
4071   case ISD::FSQRT:
4072     Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
4073                                       RTLIB::SQRT_F80, RTLIB::SQRT_F128,
4074                                       RTLIB::SQRT_PPCF128));
4075     break;
4076   case ISD::FSIN:
4077     Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
4078                                       RTLIB::SIN_F80, RTLIB::SIN_F128,
4079                                       RTLIB::SIN_PPCF128));
4080     break;
4081   case ISD::FCOS:
4082     Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
4083                                       RTLIB::COS_F80, RTLIB::COS_F128,
4084                                       RTLIB::COS_PPCF128));
4085     break;
4086   case ISD::FSINCOS:
4087     // Expand into sincos libcall.
4088     ExpandSinCosLibCall(Node, Results);
4089     break;
4090   case ISD::FLOG:
4091     Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
4092                                       RTLIB::LOG_F80, RTLIB::LOG_F128,
4093                                       RTLIB::LOG_PPCF128));
4094     break;
4095   case ISD::FLOG2:
4096     Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
4097                                       RTLIB::LOG2_F80, RTLIB::LOG2_F128,
4098                                       RTLIB::LOG2_PPCF128));
4099     break;
4100   case ISD::FLOG10:
4101     Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
4102                                       RTLIB::LOG10_F80, RTLIB::LOG10_F128,
4103                                       RTLIB::LOG10_PPCF128));
4104     break;
4105   case ISD::FEXP:
4106     Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
4107                                       RTLIB::EXP_F80, RTLIB::EXP_F128,
4108                                       RTLIB::EXP_PPCF128));
4109     break;
4110   case ISD::FEXP2:
4111     Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
4112                                       RTLIB::EXP2_F80, RTLIB::EXP2_F128,
4113                                       RTLIB::EXP2_PPCF128));
4114     break;
4115   case ISD::FTRUNC:
4116     Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
4117                                       RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
4118                                       RTLIB::TRUNC_PPCF128));
4119     break;
4120   case ISD::FFLOOR:
4121     Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
4122                                       RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
4123                                       RTLIB::FLOOR_PPCF128));
4124     break;
4125   case ISD::FCEIL:
4126     Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
4127                                       RTLIB::CEIL_F80, RTLIB::CEIL_F128,
4128                                       RTLIB::CEIL_PPCF128));
4129     break;
4130   case ISD::FRINT:
4131     Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
4132                                       RTLIB::RINT_F80, RTLIB::RINT_F128,
4133                                       RTLIB::RINT_PPCF128));
4134     break;
4135   case ISD::FNEARBYINT:
4136     Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
4137                                       RTLIB::NEARBYINT_F64,
4138                                       RTLIB::NEARBYINT_F80,
4139                                       RTLIB::NEARBYINT_F128,
4140                                       RTLIB::NEARBYINT_PPCF128));
4141     break;
4142   case ISD::FROUND:
4143     Results.push_back(ExpandFPLibCall(Node, RTLIB::ROUND_F32,
4144                                       RTLIB::ROUND_F64,
4145                                       RTLIB::ROUND_F80,
4146                                       RTLIB::ROUND_F128,
4147                                       RTLIB::ROUND_PPCF128));
4148     break;
4149   case ISD::FPOWI:
4150     Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
4151                                       RTLIB::POWI_F80, RTLIB::POWI_F128,
4152                                       RTLIB::POWI_PPCF128));
4153     break;
4154   case ISD::FPOW:
4155     Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
4156                                       RTLIB::POW_F80, RTLIB::POW_F128,
4157                                       RTLIB::POW_PPCF128));
4158     break;
4159   case ISD::FDIV:
4160     Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
4161                                       RTLIB::DIV_F80, RTLIB::DIV_F128,
4162                                       RTLIB::DIV_PPCF128));
4163     break;
4164   case ISD::FREM:
4165     Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
4166                                       RTLIB::REM_F80, RTLIB::REM_F128,
4167                                       RTLIB::REM_PPCF128));
4168     break;
4169   case ISD::FMA:
4170     Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
4171                                       RTLIB::FMA_F80, RTLIB::FMA_F128,
4172                                       RTLIB::FMA_PPCF128));
4173     break;
4174   case ISD::FADD:
4175     Results.push_back(ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64,
4176                                       RTLIB::ADD_F80, RTLIB::ADD_F128,
4177                                       RTLIB::ADD_PPCF128));
4178     break;
4179   case ISD::FMUL:
4180     Results.push_back(ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64,
4181                                       RTLIB::MUL_F80, RTLIB::MUL_F128,
4182                                       RTLIB::MUL_PPCF128));
4183     break;
4184   case ISD::FP16_TO_FP:
4185     if (Node->getValueType(0) == MVT::f32) {
4186       Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
4187     }
4188     break;
4189   case ISD::FP_TO_FP16: {
4190     RTLIB::Libcall LC =
4191         RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16);
4192     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16");
4193     Results.push_back(ExpandLibCall(LC, Node, false));
4194     break;
4195   }
4196   case ISD::FSUB:
4197     Results.push_back(ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64,
4198                                       RTLIB::SUB_F80, RTLIB::SUB_F128,
4199                                       RTLIB::SUB_PPCF128));
4200     break;
4201   case ISD::SREM:
4202     Results.push_back(ExpandIntLibCall(Node, true,
4203                                        RTLIB::SREM_I8,
4204                                        RTLIB::SREM_I16, RTLIB::SREM_I32,
4205                                        RTLIB::SREM_I64, RTLIB::SREM_I128));
4206     break;
4207   case ISD::UREM:
4208     Results.push_back(ExpandIntLibCall(Node, false,
4209                                        RTLIB::UREM_I8,
4210                                        RTLIB::UREM_I16, RTLIB::UREM_I32,
4211                                        RTLIB::UREM_I64, RTLIB::UREM_I128));
4212     break;
4213   case ISD::SDIV:
4214     Results.push_back(ExpandIntLibCall(Node, true,
4215                                        RTLIB::SDIV_I8,
4216                                        RTLIB::SDIV_I16, RTLIB::SDIV_I32,
4217                                        RTLIB::SDIV_I64, RTLIB::SDIV_I128));
4218     break;
4219   case ISD::UDIV:
4220     Results.push_back(ExpandIntLibCall(Node, false,
4221                                        RTLIB::UDIV_I8,
4222                                        RTLIB::UDIV_I16, RTLIB::UDIV_I32,
4223                                        RTLIB::UDIV_I64, RTLIB::UDIV_I128));
4224     break;
4225   case ISD::SDIVREM:
4226   case ISD::UDIVREM:
4227     // Expand into divrem libcall
4228     ExpandDivRemLibCall(Node, Results);
4229     break;
4230   case ISD::MUL:
4231     Results.push_back(ExpandIntLibCall(Node, false,
4232                                        RTLIB::MUL_I8,
4233                                        RTLIB::MUL_I16, RTLIB::MUL_I32,
4234                                        RTLIB::MUL_I64, RTLIB::MUL_I128));
4235     break;
4236   }
4237 
4238   // Replace the original node with the legalized result.
4239   if (!Results.empty())
4240     ReplaceNode(Node, Results.data());
4241 }
4242 
4243 // Determine the vector type to use in place of an original scalar element when
4244 // promoting equally sized vectors.
4245 static MVT getPromotedVectorElementType(const TargetLowering &TLI,
4246                                         MVT EltVT, MVT NewEltVT) {
4247   unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits();
4248   MVT MidVT = MVT::getVectorVT(NewEltVT, OldEltsPerNewElt);
4249   assert(TLI.isTypeLegal(MidVT) && "unexpected");
4250   return MidVT;
4251 }
4252 
4253 void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
4254   SmallVector<SDValue, 8> Results;
4255   MVT OVT = Node->getSimpleValueType(0);
4256   if (Node->getOpcode() == ISD::UINT_TO_FP ||
4257       Node->getOpcode() == ISD::SINT_TO_FP ||
4258       Node->getOpcode() == ISD::SETCC ||
4259       Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
4260       Node->getOpcode() == ISD::INSERT_VECTOR_ELT) {
4261     OVT = Node->getOperand(0).getSimpleValueType();
4262   }
4263   if (Node->getOpcode() == ISD::BR_CC)
4264     OVT = Node->getOperand(2).getSimpleValueType();
4265   MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
4266   SDLoc dl(Node);
4267   SDValue Tmp1, Tmp2, Tmp3;
4268   switch (Node->getOpcode()) {
4269   case ISD::CTTZ:
4270   case ISD::CTTZ_ZERO_UNDEF:
4271   case ISD::CTLZ:
4272   case ISD::CTLZ_ZERO_UNDEF:
4273   case ISD::CTPOP:
4274     // Zero extend the argument.
4275     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
4276     // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
4277     // already the correct result.
4278     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
4279     if (Node->getOpcode() == ISD::CTTZ) {
4280       // FIXME: This should set a bit in the zero extended value instead.
4281       Tmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT),
4282                           Tmp1, DAG.getConstant(NVT.getSizeInBits(), dl, NVT),
4283                           ISD::SETEQ);
4284       Tmp1 = DAG.getSelect(dl, NVT, Tmp2,
4285                            DAG.getConstant(OVT.getSizeInBits(), dl, NVT), Tmp1);
4286     } else if (Node->getOpcode() == ISD::CTLZ ||
4287                Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) {
4288       // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4289       Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
4290                           DAG.getConstant(NVT.getSizeInBits() -
4291                                           OVT.getSizeInBits(), dl, NVT));
4292     }
4293     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
4294     break;
4295   case ISD::BSWAP: {
4296     unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
4297     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
4298     Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
4299     Tmp1 = DAG.getNode(
4300         ISD::SRL, dl, NVT, Tmp1,
4301         DAG.getConstant(DiffBits, dl,
4302                         TLI.getShiftAmountTy(NVT, DAG.getDataLayout())));
4303     Results.push_back(Tmp1);
4304     break;
4305   }
4306   case ISD::FP_TO_UINT:
4307   case ISD::FP_TO_SINT:
4308     Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
4309                                  Node->getOpcode() == ISD::FP_TO_SINT, dl);
4310     Results.push_back(Tmp1);
4311     break;
4312   case ISD::UINT_TO_FP:
4313   case ISD::SINT_TO_FP:
4314     Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
4315                                  Node->getOpcode() == ISD::SINT_TO_FP, dl);
4316     Results.push_back(Tmp1);
4317     break;
4318   case ISD::VAARG: {
4319     SDValue Chain = Node->getOperand(0); // Get the chain.
4320     SDValue Ptr = Node->getOperand(1); // Get the pointer.
4321 
4322     unsigned TruncOp;
4323     if (OVT.isVector()) {
4324       TruncOp = ISD::BITCAST;
4325     } else {
4326       assert(OVT.isInteger()
4327         && "VAARG promotion is supported only for vectors or integer types");
4328       TruncOp = ISD::TRUNCATE;
4329     }
4330 
4331     // Perform the larger operation, then convert back
4332     Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
4333              Node->getConstantOperandVal(3));
4334     Chain = Tmp1.getValue(1);
4335 
4336     Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
4337 
4338     // Modified the chain result - switch anything that used the old chain to
4339     // use the new one.
4340     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
4341     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
4342     if (UpdatedNodes) {
4343       UpdatedNodes->insert(Tmp2.getNode());
4344       UpdatedNodes->insert(Chain.getNode());
4345     }
4346     ReplacedNode(Node);
4347     break;
4348   }
4349   case ISD::AND:
4350   case ISD::OR:
4351   case ISD::XOR: {
4352     unsigned ExtOp, TruncOp;
4353     if (OVT.isVector()) {
4354       ExtOp   = ISD::BITCAST;
4355       TruncOp = ISD::BITCAST;
4356     } else {
4357       assert(OVT.isInteger() && "Cannot promote logic operation");
4358       ExtOp   = ISD::ANY_EXTEND;
4359       TruncOp = ISD::TRUNCATE;
4360     }
4361     // Promote each of the values to the new type.
4362     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
4363     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
4364     // Perform the larger operation, then convert back
4365     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
4366     Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
4367     break;
4368   }
4369   case ISD::SELECT: {
4370     unsigned ExtOp, TruncOp;
4371     if (Node->getValueType(0).isVector() ||
4372         Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) {
4373       ExtOp   = ISD::BITCAST;
4374       TruncOp = ISD::BITCAST;
4375     } else if (Node->getValueType(0).isInteger()) {
4376       ExtOp   = ISD::ANY_EXTEND;
4377       TruncOp = ISD::TRUNCATE;
4378     } else {
4379       ExtOp   = ISD::FP_EXTEND;
4380       TruncOp = ISD::FP_ROUND;
4381     }
4382     Tmp1 = Node->getOperand(0);
4383     // Promote each of the values to the new type.
4384     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
4385     Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
4386     // Perform the larger operation, then round down.
4387     Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3);
4388     if (TruncOp != ISD::FP_ROUND)
4389       Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
4390     else
4391       Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
4392                          DAG.getIntPtrConstant(0, dl));
4393     Results.push_back(Tmp1);
4394     break;
4395   }
4396   case ISD::VECTOR_SHUFFLE: {
4397     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
4398 
4399     // Cast the two input vectors.
4400     Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
4401     Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
4402 
4403     // Convert the shuffle mask to the right # elements.
4404     Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
4405     Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
4406     Results.push_back(Tmp1);
4407     break;
4408   }
4409   case ISD::SETCC: {
4410     unsigned ExtOp = ISD::FP_EXTEND;
4411     if (NVT.isInteger()) {
4412       ISD::CondCode CCCode =
4413         cast<CondCodeSDNode>(Node->getOperand(2))->get();
4414       ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4415     }
4416     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
4417     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
4418     Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
4419                                   Tmp1, Tmp2, Node->getOperand(2)));
4420     break;
4421   }
4422   case ISD::BR_CC: {
4423     unsigned ExtOp = ISD::FP_EXTEND;
4424     if (NVT.isInteger()) {
4425       ISD::CondCode CCCode =
4426         cast<CondCodeSDNode>(Node->getOperand(1))->get();
4427       ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4428     }
4429     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
4430     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
4431     Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0),
4432                                   Node->getOperand(0), Node->getOperand(1),
4433                                   Tmp1, Tmp2, Node->getOperand(4)));
4434     break;
4435   }
4436   case ISD::FADD:
4437   case ISD::FSUB:
4438   case ISD::FMUL:
4439   case ISD::FDIV:
4440   case ISD::FREM:
4441   case ISD::FMINNUM:
4442   case ISD::FMAXNUM:
4443   case ISD::FPOW: {
4444     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4445     Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
4446     Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2,
4447                        Node->getFlags());
4448     Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
4449                                   Tmp3, DAG.getIntPtrConstant(0, dl)));
4450     break;
4451   }
4452   case ISD::FMA: {
4453     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4454     Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
4455     Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2));
4456     Results.push_back(
4457         DAG.getNode(ISD::FP_ROUND, dl, OVT,
4458                     DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3),
4459                     DAG.getIntPtrConstant(0, dl)));
4460     break;
4461   }
4462   case ISD::FCOPYSIGN:
4463   case ISD::FPOWI: {
4464     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4465     Tmp2 = Node->getOperand(1);
4466     Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
4467 
4468     // fcopysign doesn't change anything but the sign bit, so
4469     //   (fp_round (fcopysign (fpext a), b))
4470     // is as precise as
4471     //   (fp_round (fpext a))
4472     // which is a no-op. Mark it as a TRUNCating FP_ROUND.
4473     const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN);
4474     Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
4475                                   Tmp3, DAG.getIntPtrConstant(isTrunc, dl)));
4476     break;
4477   }
4478   case ISD::FFLOOR:
4479   case ISD::FCEIL:
4480   case ISD::FRINT:
4481   case ISD::FNEARBYINT:
4482   case ISD::FROUND:
4483   case ISD::FTRUNC:
4484   case ISD::FNEG:
4485   case ISD::FSQRT:
4486   case ISD::FSIN:
4487   case ISD::FCOS:
4488   case ISD::FLOG:
4489   case ISD::FLOG2:
4490   case ISD::FLOG10:
4491   case ISD::FABS:
4492   case ISD::FEXP:
4493   case ISD::FEXP2: {
4494     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4495     Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
4496     Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
4497                                   Tmp2, DAG.getIntPtrConstant(0, dl)));
4498     break;
4499   }
4500   case ISD::BUILD_VECTOR: {
4501     MVT EltVT = OVT.getVectorElementType();
4502     MVT NewEltVT = NVT.getVectorElementType();
4503 
4504     // Handle bitcasts to a different vector type with the same total bit size
4505     //
4506     // e.g. v2i64 = build_vector i64:x, i64:y => v4i32
4507     //  =>
4508     //  v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y))
4509 
4510     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4511            "Invalid promote type for build_vector");
4512     assert(NewEltVT.bitsLT(EltVT) && "not handled");
4513 
4514     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4515 
4516     SmallVector<SDValue, 8> NewOps;
4517     for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
4518       SDValue Op = Node->getOperand(I);
4519       NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op));
4520     }
4521 
4522     SDLoc SL(Node);
4523     SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewOps);
4524     SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
4525     Results.push_back(CvtVec);
4526     break;
4527   }
4528   case ISD::EXTRACT_VECTOR_ELT: {
4529     MVT EltVT = OVT.getVectorElementType();
4530     MVT NewEltVT = NVT.getVectorElementType();
4531 
4532     // Handle bitcasts to a different vector type with the same total bit size.
4533     //
4534     // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32
4535     //  =>
4536     //  v4i32:castx = bitcast x:v2i64
4537     //
4538     // i64 = bitcast
4539     //   (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))),
4540     //                       (i32 (extract_vector_elt castx, (2 * y + 1)))
4541     //
4542 
4543     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4544            "Invalid promote type for extract_vector_elt");
4545     assert(NewEltVT.bitsLT(EltVT) && "not handled");
4546 
4547     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4548     unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
4549 
4550     SDValue Idx = Node->getOperand(1);
4551     EVT IdxVT = Idx.getValueType();
4552     SDLoc SL(Node);
4553     SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT);
4554     SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
4555 
4556     SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
4557 
4558     SmallVector<SDValue, 8> NewOps;
4559     for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
4560       SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
4561       SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
4562 
4563       SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
4564                                 CastVec, TmpIdx);
4565       NewOps.push_back(Elt);
4566     }
4567 
4568     SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, SL, MidVT, NewOps);
4569 
4570     Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec));
4571     break;
4572   }
4573   case ISD::INSERT_VECTOR_ELT: {
4574     MVT EltVT = OVT.getVectorElementType();
4575     MVT NewEltVT = NVT.getVectorElementType();
4576 
4577     // Handle bitcasts to a different vector type with the same total bit size
4578     //
4579     // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32
4580     //  =>
4581     //  v4i32:castx = bitcast x:v2i64
4582     //  v2i32:casty = bitcast y:i64
4583     //
4584     // v2i64 = bitcast
4585     //   (v4i32 insert_vector_elt
4586     //       (v4i32 insert_vector_elt v4i32:castx,
4587     //                                (extract_vector_elt casty, 0), 2 * z),
4588     //        (extract_vector_elt casty, 1), (2 * z + 1))
4589 
4590     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4591            "Invalid promote type for insert_vector_elt");
4592     assert(NewEltVT.bitsLT(EltVT) && "not handled");
4593 
4594     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4595     unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
4596 
4597     SDValue Val = Node->getOperand(1);
4598     SDValue Idx = Node->getOperand(2);
4599     EVT IdxVT = Idx.getValueType();
4600     SDLoc SL(Node);
4601 
4602     SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT);
4603     SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
4604 
4605     SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
4606     SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
4607 
4608     SDValue NewVec = CastVec;
4609     for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
4610       SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
4611       SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
4612 
4613       SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
4614                                 CastVal, IdxOffset);
4615 
4616       NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT,
4617                            NewVec, Elt, InEltIdx);
4618     }
4619 
4620     Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec));
4621     break;
4622   }
4623   case ISD::SCALAR_TO_VECTOR: {
4624     MVT EltVT = OVT.getVectorElementType();
4625     MVT NewEltVT = NVT.getVectorElementType();
4626 
4627     // Handle bitcasts to different vector type with the smae total bit size.
4628     //
4629     // e.g. v2i64 = scalar_to_vector x:i64
4630     //   =>
4631     //  concat_vectors (v2i32 bitcast x:i64), (v2i32 undef)
4632     //
4633 
4634     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4635     SDValue Val = Node->getOperand(0);
4636     SDLoc SL(Node);
4637 
4638     SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
4639     SDValue Undef = DAG.getUNDEF(MidVT);
4640 
4641     SmallVector<SDValue, 8> NewElts;
4642     NewElts.push_back(CastVal);
4643     for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I)
4644       NewElts.push_back(Undef);
4645 
4646     SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts);
4647     SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
4648     Results.push_back(CvtVec);
4649     break;
4650   }
4651   }
4652 
4653   // Replace the original node with the legalized result.
4654   if (!Results.empty())
4655     ReplaceNode(Node, Results.data());
4656 }
4657 
4658 /// This is the entry point for the file.
4659 void SelectionDAG::Legalize() {
4660   AssignTopologicalOrder();
4661 
4662   SmallPtrSet<SDNode *, 16> LegalizedNodes;
4663   SelectionDAGLegalize Legalizer(*this, LegalizedNodes);
4664 
4665   // Visit all the nodes. We start in topological order, so that we see
4666   // nodes with their original operands intact. Legalization can produce
4667   // new nodes which may themselves need to be legalized. Iterate until all
4668   // nodes have been legalized.
4669   for (;;) {
4670     bool AnyLegalized = false;
4671     for (auto NI = allnodes_end(); NI != allnodes_begin();) {
4672       --NI;
4673 
4674       SDNode *N = &*NI;
4675       if (N->use_empty() && N != getRoot().getNode()) {
4676         ++NI;
4677         DeleteNode(N);
4678         continue;
4679       }
4680 
4681       if (LegalizedNodes.insert(N).second) {
4682         AnyLegalized = true;
4683         Legalizer.LegalizeOp(N);
4684 
4685         if (N->use_empty() && N != getRoot().getNode()) {
4686           ++NI;
4687           DeleteNode(N);
4688         }
4689       }
4690     }
4691     if (!AnyLegalized)
4692       break;
4693 
4694   }
4695 
4696   // Remove dead nodes now.
4697   RemoveDeadNodes();
4698 }
4699 
4700 bool SelectionDAG::LegalizeOp(SDNode *N,
4701                               SmallSetVector<SDNode *, 16> &UpdatedNodes) {
4702   SmallPtrSet<SDNode *, 16> LegalizedNodes;
4703   SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes);
4704 
4705   // Directly insert the node in question, and legalize it. This will recurse
4706   // as needed through operands.
4707   LegalizedNodes.insert(N);
4708   Legalizer.LegalizeOp(N);
4709 
4710   return LegalizedNodes.count(N);
4711 }
4712