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