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