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