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