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