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