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