LLVM API Documentation

LegalizeDAG.cpp
Go to the documentation of this file.
00001 //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the SelectionDAG::Legalize method.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/CodeGen/SelectionDAG.h"
00015 #include "llvm/ADT/SetVector.h"
00016 #include "llvm/ADT/SmallPtrSet.h"
00017 #include "llvm/ADT/SmallSet.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/ADT/Triple.h"
00020 #include "llvm/CodeGen/Analysis.h"
00021 #include "llvm/CodeGen/MachineFunction.h"
00022 #include "llvm/CodeGen/MachineJumpTableInfo.h"
00023 #include "llvm/IR/CallingConv.h"
00024 #include "llvm/IR/Constants.h"
00025 #include "llvm/IR/DataLayout.h"
00026 #include "llvm/IR/DebugInfo.h"
00027 #include "llvm/IR/DerivedTypes.h"
00028 #include "llvm/IR/Function.h"
00029 #include "llvm/IR/LLVMContext.h"
00030 #include "llvm/Support/Debug.h"
00031 #include "llvm/Support/ErrorHandling.h"
00032 #include "llvm/Support/MathExtras.h"
00033 #include "llvm/Support/raw_ostream.h"
00034 #include "llvm/Target/TargetFrameLowering.h"
00035 #include "llvm/Target/TargetLowering.h"
00036 #include "llvm/Target/TargetMachine.h"
00037 #include "llvm/Target/TargetSubtargetInfo.h"
00038 using namespace llvm;
00039 
00040 #define DEBUG_TYPE "legalizedag"
00041 
00042 //===----------------------------------------------------------------------===//
00043 /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
00044 /// hacks on it until the target machine can handle it.  This involves
00045 /// eliminating value sizes the machine cannot handle (promoting small sizes to
00046 /// large sizes or splitting up large values into small values) as well as
00047 /// eliminating operations the machine cannot handle.
00048 ///
00049 /// This code also does a small amount of optimization and recognition of idioms
00050 /// as part of its processing.  For example, if a target does not support a
00051 /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
00052 /// will attempt merge setcc and brc instructions into brcc's.
00053 ///
00054 namespace {
00055 class SelectionDAGLegalize {
00056   const TargetMachine &TM;
00057   const TargetLowering &TLI;
00058   SelectionDAG &DAG;
00059 
00060   /// \brief The set of nodes which have already been legalized. We hold a
00061   /// reference to it in order to update as necessary on node deletion.
00062   SmallPtrSetImpl<SDNode *> &LegalizedNodes;
00063 
00064   /// \brief A set of all the nodes updated during legalization.
00065   SmallSetVector<SDNode *, 16> *UpdatedNodes;
00066 
00067   EVT getSetCCResultType(EVT VT) const {
00068     return TLI.getSetCCResultType(*DAG.getContext(), VT);
00069   }
00070 
00071   // Libcall insertion helpers.
00072 
00073 public:
00074   SelectionDAGLegalize(SelectionDAG &DAG,
00075                        SmallPtrSetImpl<SDNode *> &LegalizedNodes,
00076                        SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr)
00077       : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG),
00078         LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {}
00079 
00080   /// \brief Legalizes the given operation.
00081   void LegalizeOp(SDNode *Node);
00082 
00083 private:
00084   SDValue OptimizeFloatStore(StoreSDNode *ST);
00085 
00086   void LegalizeLoadOps(SDNode *Node);
00087   void LegalizeStoreOps(SDNode *Node);
00088 
00089   /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
00090   /// insertion index for the INSERT_VECTOR_ELT instruction.  In this case, it
00091   /// is necessary to spill the vector being inserted into to memory, perform
00092   /// the insert there, and then read the result back.
00093   SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
00094                                          SDValue Idx, SDLoc dl);
00095   SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
00096                                   SDValue Idx, SDLoc dl);
00097 
00098   /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
00099   /// performs the same shuffe in terms of order or result bytes, but on a type
00100   /// whose vector element type is narrower than the original shuffle type.
00101   /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
00102   SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, SDLoc dl,
00103                                      SDValue N1, SDValue N2,
00104                                      ArrayRef<int> Mask) const;
00105 
00106   bool LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
00107                              bool &NeedInvert, SDLoc dl);
00108 
00109   SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
00110   SDValue ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, const SDValue *Ops,
00111                         unsigned NumOps, bool isSigned, SDLoc dl);
00112 
00113   std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
00114                                                  SDNode *Node, bool isSigned);
00115   SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
00116                           RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
00117                           RTLIB::Libcall Call_F128,
00118                           RTLIB::Libcall Call_PPCF128);
00119   SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
00120                            RTLIB::Libcall Call_I8,
00121                            RTLIB::Libcall Call_I16,
00122                            RTLIB::Libcall Call_I32,
00123                            RTLIB::Libcall Call_I64,
00124                            RTLIB::Libcall Call_I128);
00125   void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
00126   void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
00127 
00128   SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, SDLoc dl);
00129   SDValue ExpandBUILD_VECTOR(SDNode *Node);
00130   SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
00131   void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
00132                                 SmallVectorImpl<SDValue> &Results);
00133   SDValue ExpandFCOPYSIGN(SDNode *Node);
00134   SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
00135                                SDLoc dl);
00136   SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
00137                                 SDLoc dl);
00138   SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
00139                                 SDLoc dl);
00140 
00141   SDValue ExpandBSWAP(SDValue Op, SDLoc dl);
00142   SDValue ExpandBitCount(unsigned Opc, SDValue Op, SDLoc dl);
00143 
00144   SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
00145   SDValue ExpandInsertToVectorThroughStack(SDValue Op);
00146   SDValue ExpandVectorBuildThroughStack(SDNode* Node);
00147 
00148   SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
00149 
00150   std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
00151 
00152   void ExpandNode(SDNode *Node);
00153   void PromoteNode(SDNode *Node);
00154 
00155 public:
00156   // Node replacement helpers
00157   void ReplacedNode(SDNode *N) {
00158     LegalizedNodes.erase(N);
00159     if (UpdatedNodes)
00160       UpdatedNodes->insert(N);
00161   }
00162   void ReplaceNode(SDNode *Old, SDNode *New) {
00163     DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
00164           dbgs() << "     with:      "; New->dump(&DAG));
00165 
00166     assert(Old->getNumValues() == New->getNumValues() &&
00167            "Replacing one node with another that produces a different number "
00168            "of values!");
00169     DAG.ReplaceAllUsesWith(Old, New);
00170     for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i)
00171       DAG.TransferDbgValues(SDValue(Old, i), SDValue(New, i));
00172     if (UpdatedNodes)
00173       UpdatedNodes->insert(New);
00174     ReplacedNode(Old);
00175   }
00176   void ReplaceNode(SDValue Old, SDValue New) {
00177     DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
00178           dbgs() << "     with:      "; New->dump(&DAG));
00179 
00180     DAG.ReplaceAllUsesWith(Old, New);
00181     DAG.TransferDbgValues(Old, New);
00182     if (UpdatedNodes)
00183       UpdatedNodes->insert(New.getNode());
00184     ReplacedNode(Old.getNode());
00185   }
00186   void ReplaceNode(SDNode *Old, const SDValue *New) {
00187     DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG));
00188 
00189     DAG.ReplaceAllUsesWith(Old, New);
00190     for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) {
00191       DEBUG(dbgs() << (i == 0 ? "     with:      "
00192                               : "      and:      ");
00193             New[i]->dump(&DAG));
00194       DAG.TransferDbgValues(SDValue(Old, i), New[i]);
00195       if (UpdatedNodes)
00196         UpdatedNodes->insert(New[i].getNode());
00197     }
00198     ReplacedNode(Old);
00199   }
00200 };
00201 }
00202 
00203 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
00204 /// performs the same shuffe in terms of order or result bytes, but on a type
00205 /// whose vector element type is narrower than the original shuffle type.
00206 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
00207 SDValue
00208 SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT,  SDLoc dl,
00209                                                  SDValue N1, SDValue N2,
00210                                                  ArrayRef<int> Mask) const {
00211   unsigned NumMaskElts = VT.getVectorNumElements();
00212   unsigned NumDestElts = NVT.getVectorNumElements();
00213   unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
00214 
00215   assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
00216 
00217   if (NumEltsGrowth == 1)
00218     return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
00219 
00220   SmallVector<int, 8> NewMask;
00221   for (unsigned i = 0; i != NumMaskElts; ++i) {
00222     int Idx = Mask[i];
00223     for (unsigned j = 0; j != NumEltsGrowth; ++j) {
00224       if (Idx < 0)
00225         NewMask.push_back(-1);
00226       else
00227         NewMask.push_back(Idx * NumEltsGrowth + j);
00228     }
00229   }
00230   assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
00231   assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
00232   return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
00233 }
00234 
00235 /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
00236 /// a load from the constant pool.
00237 SDValue
00238 SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
00239   bool Extend = false;
00240   SDLoc dl(CFP);
00241 
00242   // If a FP immediate is precise when represented as a float and if the
00243   // target can do an extending load from float to double, we put it into
00244   // the constant pool as a float, even if it's is statically typed as a
00245   // double.  This shrinks FP constants and canonicalizes them for targets where
00246   // an FP extending load is the same cost as a normal load (such as on the x87
00247   // fp stack or PPC FP unit).
00248   EVT VT = CFP->getValueType(0);
00249   ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
00250   if (!UseCP) {
00251     assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
00252     return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
00253                            (VT == MVT::f64) ? MVT::i64 : MVT::i32);
00254   }
00255 
00256   EVT OrigVT = VT;
00257   EVT SVT = VT;
00258   while (SVT != MVT::f32 && SVT != MVT::f16) {
00259     SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
00260     if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
00261         // Only do this if the target has a native EXTLOAD instruction from
00262         // smaller type.
00263         TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
00264         TLI.ShouldShrinkFPConstant(OrigVT)) {
00265       Type *SType = SVT.getTypeForEVT(*DAG.getContext());
00266       LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
00267       VT = SVT;
00268       Extend = true;
00269     }
00270   }
00271 
00272   SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
00273   unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
00274   if (Extend) {
00275     SDValue Result =
00276       DAG.getExtLoad(ISD::EXTLOAD, dl, OrigVT,
00277                      DAG.getEntryNode(),
00278                      CPIdx, MachinePointerInfo::getConstantPool(),
00279                      VT, false, false, false, Alignment);
00280     return Result;
00281   }
00282   SDValue Result =
00283     DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
00284                 MachinePointerInfo::getConstantPool(), false, false, false,
00285                 Alignment);
00286   return Result;
00287 }
00288 
00289 /// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
00290 static void ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
00291                                  const TargetLowering &TLI,
00292                                  SelectionDAGLegalize *DAGLegalize) {
00293   assert(ST->getAddressingMode() == ISD::UNINDEXED &&
00294          "unaligned indexed stores not implemented!");
00295   SDValue Chain = ST->getChain();
00296   SDValue Ptr = ST->getBasePtr();
00297   SDValue Val = ST->getValue();
00298   EVT VT = Val.getValueType();
00299   int Alignment = ST->getAlignment();
00300   unsigned AS = ST->getAddressSpace();
00301 
00302   SDLoc dl(ST);
00303   if (ST->getMemoryVT().isFloatingPoint() ||
00304       ST->getMemoryVT().isVector()) {
00305     EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
00306     if (TLI.isTypeLegal(intVT)) {
00307       // Expand to a bitconvert of the value to the integer type of the
00308       // same size, then a (misaligned) int store.
00309       // FIXME: Does not handle truncating floating point stores!
00310       SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
00311       Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
00312                            ST->isVolatile(), ST->isNonTemporal(), Alignment);
00313       DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
00314       return;
00315     }
00316     // Do a (aligned) store to a stack slot, then copy from the stack slot
00317     // to the final destination using (unaligned) integer loads and stores.
00318     EVT StoredVT = ST->getMemoryVT();
00319     MVT RegVT =
00320       TLI.getRegisterType(*DAG.getContext(),
00321                           EVT::getIntegerVT(*DAG.getContext(),
00322                                             StoredVT.getSizeInBits()));
00323     unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
00324     unsigned RegBytes = RegVT.getSizeInBits() / 8;
00325     unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
00326 
00327     // Make sure the stack slot is also aligned for the register type.
00328     SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
00329 
00330     // Perform the original store, only redirected to the stack slot.
00331     SDValue Store = DAG.getTruncStore(Chain, dl,
00332                                       Val, StackPtr, MachinePointerInfo(),
00333                                       StoredVT, false, false, 0);
00334     SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy(AS));
00335     SmallVector<SDValue, 8> Stores;
00336     unsigned Offset = 0;
00337 
00338     // Do all but one copies using the full register width.
00339     for (unsigned i = 1; i < NumRegs; i++) {
00340       // Load one integer register's worth from the stack slot.
00341       SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
00342                                  MachinePointerInfo(),
00343                                  false, false, false, 0);
00344       // Store it to the final location.  Remember the store.
00345       Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
00346                                   ST->getPointerInfo().getWithOffset(Offset),
00347                                     ST->isVolatile(), ST->isNonTemporal(),
00348                                     MinAlign(ST->getAlignment(), Offset)));
00349       // Increment the pointers.
00350       Offset += RegBytes;
00351       StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
00352                              Increment);
00353       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
00354     }
00355 
00356     // The last store may be partial.  Do a truncating store.  On big-endian
00357     // machines this requires an extending load from the stack slot to ensure
00358     // that the bits are in the right place.
00359     EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
00360                                   8 * (StoredBytes - Offset));
00361 
00362     // Load from the stack slot.
00363     SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
00364                                   MachinePointerInfo(),
00365                                   MemVT, false, false, false, 0);
00366 
00367     Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
00368                                        ST->getPointerInfo()
00369                                          .getWithOffset(Offset),
00370                                        MemVT, ST->isVolatile(),
00371                                        ST->isNonTemporal(),
00372                                        MinAlign(ST->getAlignment(), Offset),
00373                                        ST->getAAInfo()));
00374     // The order of the stores doesn't matter - say it with a TokenFactor.
00375     SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
00376     DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
00377     return;
00378   }
00379   assert(ST->getMemoryVT().isInteger() &&
00380          !ST->getMemoryVT().isVector() &&
00381          "Unaligned store of unknown type.");
00382   // Get the half-size VT
00383   EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
00384   int NumBits = NewStoredVT.getSizeInBits();
00385   int IncrementSize = NumBits / 8;
00386 
00387   // Divide the stored value in two parts.
00388   SDValue ShiftAmount = DAG.getConstant(NumBits,
00389                                       TLI.getShiftAmountTy(Val.getValueType()));
00390   SDValue Lo = Val;
00391   SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
00392 
00393   // Store the two parts
00394   SDValue Store1, Store2;
00395   Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
00396                              ST->getPointerInfo(), NewStoredVT,
00397                              ST->isVolatile(), ST->isNonTemporal(), Alignment);
00398 
00399   Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
00400                     DAG.getConstant(IncrementSize, TLI.getPointerTy(AS)));
00401   Alignment = MinAlign(Alignment, IncrementSize);
00402   Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
00403                              ST->getPointerInfo().getWithOffset(IncrementSize),
00404                              NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
00405                              Alignment, ST->getAAInfo());
00406 
00407   SDValue Result =
00408     DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
00409   DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
00410 }
00411 
00412 /// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
00413 static void
00414 ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
00415                     const TargetLowering &TLI,
00416                     SDValue &ValResult, SDValue &ChainResult) {
00417   assert(LD->getAddressingMode() == ISD::UNINDEXED &&
00418          "unaligned indexed loads not implemented!");
00419   SDValue Chain = LD->getChain();
00420   SDValue Ptr = LD->getBasePtr();
00421   EVT VT = LD->getValueType(0);
00422   EVT LoadedVT = LD->getMemoryVT();
00423   SDLoc dl(LD);
00424   if (VT.isFloatingPoint() || VT.isVector()) {
00425     EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
00426     if (TLI.isTypeLegal(intVT) && TLI.isTypeLegal(LoadedVT)) {
00427       // Expand to a (misaligned) integer load of the same size,
00428       // then bitconvert to floating point or vector.
00429       SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr,
00430                                     LD->getMemOperand());
00431       SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
00432       if (LoadedVT != VT)
00433         Result = DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND :
00434                              ISD::ANY_EXTEND, dl, VT, Result);
00435 
00436       ValResult = Result;
00437       ChainResult = Chain;
00438       return;
00439     }
00440 
00441     // Copy the value to a (aligned) stack slot using (unaligned) integer
00442     // loads and stores, then do a (aligned) load from the stack slot.
00443     MVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
00444     unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
00445     unsigned RegBytes = RegVT.getSizeInBits() / 8;
00446     unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
00447 
00448     // Make sure the stack slot is also aligned for the register type.
00449     SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
00450 
00451     SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
00452     SmallVector<SDValue, 8> Stores;
00453     SDValue StackPtr = StackBase;
00454     unsigned Offset = 0;
00455 
00456     // Do all but one copies using the full register width.
00457     for (unsigned i = 1; i < NumRegs; i++) {
00458       // Load one integer register's worth from the original location.
00459       SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
00460                                  LD->getPointerInfo().getWithOffset(Offset),
00461                                  LD->isVolatile(), LD->isNonTemporal(),
00462                                  LD->isInvariant(),
00463                                  MinAlign(LD->getAlignment(), Offset),
00464                                  LD->getAAInfo());
00465       // Follow the load with a store to the stack slot.  Remember the store.
00466       Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
00467                                     MachinePointerInfo(), false, false, 0));
00468       // Increment the pointers.
00469       Offset += RegBytes;
00470       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
00471       StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
00472                              Increment);
00473     }
00474 
00475     // The last copy may be partial.  Do an extending load.
00476     EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
00477                                   8 * (LoadedBytes - Offset));
00478     SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
00479                                   LD->getPointerInfo().getWithOffset(Offset),
00480                                   MemVT, LD->isVolatile(),
00481                                   LD->isNonTemporal(),
00482                                   LD->isInvariant(),
00483                                   MinAlign(LD->getAlignment(), Offset),
00484                                   LD->getAAInfo());
00485     // Follow the load with a store to the stack slot.  Remember the store.
00486     // On big-endian machines this requires a truncating store to ensure
00487     // that the bits end up in the right place.
00488     Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
00489                                        MachinePointerInfo(), MemVT,
00490                                        false, false, 0));
00491 
00492     // The order of the stores doesn't matter - say it with a TokenFactor.
00493     SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
00494 
00495     // Finally, perform the original load only redirected to the stack slot.
00496     Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
00497                           MachinePointerInfo(), LoadedVT, false,false, false,
00498                           0);
00499 
00500     // Callers expect a MERGE_VALUES node.
00501     ValResult = Load;
00502     ChainResult = TF;
00503     return;
00504   }
00505   assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
00506          "Unaligned load of unsupported type.");
00507 
00508   // Compute the new VT that is half the size of the old one.  This is an
00509   // integer MVT.
00510   unsigned NumBits = LoadedVT.getSizeInBits();
00511   EVT NewLoadedVT;
00512   NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
00513   NumBits >>= 1;
00514 
00515   unsigned Alignment = LD->getAlignment();
00516   unsigned IncrementSize = NumBits / 8;
00517   ISD::LoadExtType HiExtType = LD->getExtensionType();
00518 
00519   // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
00520   if (HiExtType == ISD::NON_EXTLOAD)
00521     HiExtType = ISD::ZEXTLOAD;
00522 
00523   // Load the value in two parts
00524   SDValue Lo, Hi;
00525   if (TLI.isLittleEndian()) {
00526     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
00527                         NewLoadedVT, LD->isVolatile(),
00528                         LD->isNonTemporal(), LD->isInvariant(), Alignment,
00529                         LD->getAAInfo());
00530     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
00531                       DAG.getConstant(IncrementSize, Ptr.getValueType()));
00532     Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
00533                         LD->getPointerInfo().getWithOffset(IncrementSize),
00534                         NewLoadedVT, LD->isVolatile(),
00535                         LD->isNonTemporal(),LD->isInvariant(),
00536                         MinAlign(Alignment, IncrementSize), LD->getAAInfo());
00537   } else {
00538     Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
00539                         NewLoadedVT, LD->isVolatile(),
00540                         LD->isNonTemporal(), LD->isInvariant(), Alignment,
00541                         LD->getAAInfo());
00542     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
00543                       DAG.getConstant(IncrementSize, Ptr.getValueType()));
00544     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
00545                         LD->getPointerInfo().getWithOffset(IncrementSize),
00546                         NewLoadedVT, LD->isVolatile(),
00547                         LD->isNonTemporal(), LD->isInvariant(),
00548                         MinAlign(Alignment, IncrementSize), LD->getAAInfo());
00549   }
00550 
00551   // aggregate the two parts
00552   SDValue ShiftAmount = DAG.getConstant(NumBits,
00553                                        TLI.getShiftAmountTy(Hi.getValueType()));
00554   SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
00555   Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
00556 
00557   SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
00558                              Hi.getValue(1));
00559 
00560   ValResult = Result;
00561   ChainResult = TF;
00562 }
00563 
00564 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
00565 /// insertion index for the INSERT_VECTOR_ELT instruction.  In this case, it
00566 /// is necessary to spill the vector being inserted into to memory, perform
00567 /// the insert there, and then read the result back.
00568 SDValue SelectionDAGLegalize::
00569 PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
00570                                SDLoc dl) {
00571   SDValue Tmp1 = Vec;
00572   SDValue Tmp2 = Val;
00573   SDValue Tmp3 = Idx;
00574 
00575   // If the target doesn't support this, we have to spill the input vector
00576   // to a temporary stack slot, update the element, then reload it.  This is
00577   // badness.  We could also load the value into a vector register (either
00578   // with a "move to register" or "extload into register" instruction, then
00579   // permute it into place, if the idx is a constant and if the idx is
00580   // supported by the target.
00581   EVT VT    = Tmp1.getValueType();
00582   EVT EltVT = VT.getVectorElementType();
00583   EVT IdxVT = Tmp3.getValueType();
00584   EVT PtrVT = TLI.getPointerTy();
00585   SDValue StackPtr = DAG.CreateStackTemporary(VT);
00586 
00587   int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
00588 
00589   // Store the vector.
00590   SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
00591                             MachinePointerInfo::getFixedStack(SPFI),
00592                             false, false, 0);
00593 
00594   // Truncate or zero extend offset to target pointer type.
00595   unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
00596   Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
00597   // Add the offset to the index.
00598   unsigned EltSize = EltVT.getSizeInBits()/8;
00599   Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
00600   SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
00601   // Store the scalar value.
00602   Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
00603                          false, false, 0);
00604   // Load the updated vector.
00605   return DAG.getLoad(VT, dl, Ch, StackPtr,
00606                      MachinePointerInfo::getFixedStack(SPFI), false, false,
00607                      false, 0);
00608 }
00609 
00610 
00611 SDValue SelectionDAGLegalize::
00612 ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, SDLoc dl) {
00613   if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
00614     // SCALAR_TO_VECTOR requires that the type of the value being inserted
00615     // match the element type of the vector being created, except for
00616     // integers in which case the inserted value can be over width.
00617     EVT EltVT = Vec.getValueType().getVectorElementType();
00618     if (Val.getValueType() == EltVT ||
00619         (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
00620       SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
00621                                   Vec.getValueType(), Val);
00622 
00623       unsigned NumElts = Vec.getValueType().getVectorNumElements();
00624       // We generate a shuffle of InVec and ScVec, so the shuffle mask
00625       // should be 0,1,2,3,4,5... with the appropriate element replaced with
00626       // elt 0 of the RHS.
00627       SmallVector<int, 8> ShufOps;
00628       for (unsigned i = 0; i != NumElts; ++i)
00629         ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
00630 
00631       return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
00632                                   &ShufOps[0]);
00633     }
00634   }
00635   return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
00636 }
00637 
00638 SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
00639   // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
00640   // FIXME: We shouldn't do this for TargetConstantFP's.
00641   // FIXME: move this to the DAG Combiner!  Note that we can't regress due
00642   // to phase ordering between legalized code and the dag combiner.  This
00643   // probably means that we need to integrate dag combiner and legalizer
00644   // together.
00645   // We generally can't do this one for long doubles.
00646   SDValue Chain = ST->getChain();
00647   SDValue Ptr = ST->getBasePtr();
00648   unsigned Alignment = ST->getAlignment();
00649   bool isVolatile = ST->isVolatile();
00650   bool isNonTemporal = ST->isNonTemporal();
00651   AAMDNodes AAInfo = ST->getAAInfo();
00652   SDLoc dl(ST);
00653   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
00654     if (CFP->getValueType(0) == MVT::f32 &&
00655         TLI.isTypeLegal(MVT::i32)) {
00656       SDValue Con = DAG.getConstant(CFP->getValueAPF().
00657                                       bitcastToAPInt().zextOrTrunc(32),
00658                               MVT::i32);
00659       return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
00660                           isVolatile, isNonTemporal, Alignment, AAInfo);
00661     }
00662 
00663     if (CFP->getValueType(0) == MVT::f64) {
00664       // If this target supports 64-bit registers, do a single 64-bit store.
00665       if (TLI.isTypeLegal(MVT::i64)) {
00666         SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
00667                                   zextOrTrunc(64), MVT::i64);
00668         return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
00669                             isVolatile, isNonTemporal, Alignment, AAInfo);
00670       }
00671 
00672       if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
00673         // Otherwise, if the target supports 32-bit registers, use 2 32-bit
00674         // stores.  If the target supports neither 32- nor 64-bits, this
00675         // xform is certainly not worth it.
00676         const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
00677         SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
00678         SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
00679         if (TLI.isBigEndian()) std::swap(Lo, Hi);
00680 
00681         Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(), isVolatile,
00682                           isNonTemporal, Alignment, AAInfo);
00683         Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
00684                           DAG.getConstant(4, Ptr.getValueType()));
00685         Hi = DAG.getStore(Chain, dl, Hi, Ptr,
00686                           ST->getPointerInfo().getWithOffset(4),
00687                           isVolatile, isNonTemporal, MinAlign(Alignment, 4U),
00688                           AAInfo);
00689 
00690         return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
00691       }
00692     }
00693   }
00694   return SDValue(nullptr, 0);
00695 }
00696 
00697 void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
00698     StoreSDNode *ST = cast<StoreSDNode>(Node);
00699     SDValue Chain = ST->getChain();
00700     SDValue Ptr = ST->getBasePtr();
00701     SDLoc dl(Node);
00702 
00703     unsigned Alignment = ST->getAlignment();
00704     bool isVolatile = ST->isVolatile();
00705     bool isNonTemporal = ST->isNonTemporal();
00706     AAMDNodes AAInfo = ST->getAAInfo();
00707 
00708     if (!ST->isTruncatingStore()) {
00709       if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
00710         ReplaceNode(ST, OptStore);
00711         return;
00712       }
00713 
00714       {
00715         SDValue Value = ST->getValue();
00716         MVT VT = Value.getSimpleValueType();
00717         switch (TLI.getOperationAction(ISD::STORE, VT)) {
00718         default: llvm_unreachable("This action is not supported yet!");
00719         case TargetLowering::Legal: {
00720           // If this is an unaligned store and the target doesn't support it,
00721           // expand it.
00722           unsigned AS = ST->getAddressSpace();
00723           unsigned Align = ST->getAlignment();
00724           if (!TLI.allowsMisalignedMemoryAccesses(ST->getMemoryVT(), AS, Align)) {
00725             Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
00726             unsigned ABIAlignment= TLI.getDataLayout()->getABITypeAlignment(Ty);
00727             if (Align < ABIAlignment)
00728               ExpandUnalignedStore(cast<StoreSDNode>(Node),
00729                                    DAG, TLI, this);
00730           }
00731           break;
00732         }
00733         case TargetLowering::Custom: {
00734           SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
00735           if (Res.getNode())
00736             ReplaceNode(SDValue(Node, 0), Res);
00737           return;
00738         }
00739         case TargetLowering::Promote: {
00740           MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT);
00741           assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
00742                  "Can only promote stores to same size type");
00743           Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value);
00744           SDValue Result =
00745             DAG.getStore(Chain, dl, Value, Ptr,
00746                          ST->getPointerInfo(), isVolatile,
00747                          isNonTemporal, Alignment, AAInfo);
00748           ReplaceNode(SDValue(Node, 0), Result);
00749           break;
00750         }
00751         }
00752         return;
00753       }
00754     } else {
00755       SDValue Value = ST->getValue();
00756 
00757       EVT StVT = ST->getMemoryVT();
00758       unsigned StWidth = StVT.getSizeInBits();
00759 
00760       if (StWidth != StVT.getStoreSizeInBits()) {
00761         // Promote to a byte-sized store with upper bits zero if not
00762         // storing an integral number of bytes.  For example, promote
00763         // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
00764         EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
00765                                     StVT.getStoreSizeInBits());
00766         Value = DAG.getZeroExtendInReg(Value, dl, StVT);
00767         SDValue Result =
00768           DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
00769                             NVT, isVolatile, isNonTemporal, Alignment,
00770                             AAInfo);
00771         ReplaceNode(SDValue(Node, 0), Result);
00772       } else if (StWidth & (StWidth - 1)) {
00773         // If not storing a power-of-2 number of bits, expand as two stores.
00774         assert(!StVT.isVector() && "Unsupported truncstore!");
00775         unsigned RoundWidth = 1 << Log2_32(StWidth);
00776         assert(RoundWidth < StWidth);
00777         unsigned ExtraWidth = StWidth - RoundWidth;
00778         assert(ExtraWidth < RoundWidth);
00779         assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
00780                "Store size not an integral number of bytes!");
00781         EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
00782         EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
00783         SDValue Lo, Hi;
00784         unsigned IncrementSize;
00785 
00786         if (TLI.isLittleEndian()) {
00787           // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
00788           // Store the bottom RoundWidth bits.
00789           Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
00790                                  RoundVT,
00791                                  isVolatile, isNonTemporal, Alignment,
00792                                  AAInfo);
00793 
00794           // Store the remaining ExtraWidth bits.
00795           IncrementSize = RoundWidth / 8;
00796           Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
00797                             DAG.getConstant(IncrementSize, Ptr.getValueType()));
00798           Hi = DAG.getNode(ISD::SRL, dl, Value.getValueType(), Value,
00799                            DAG.getConstant(RoundWidth,
00800                                    TLI.getShiftAmountTy(Value.getValueType())));
00801           Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr,
00802                              ST->getPointerInfo().getWithOffset(IncrementSize),
00803                                  ExtraVT, isVolatile, isNonTemporal,
00804                                  MinAlign(Alignment, IncrementSize), AAInfo);
00805         } else {
00806           // Big endian - avoid unaligned stores.
00807           // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
00808           // Store the top RoundWidth bits.
00809           Hi = DAG.getNode(ISD::SRL, dl, Value.getValueType(), Value,
00810                            DAG.getConstant(ExtraWidth,
00811                                    TLI.getShiftAmountTy(Value.getValueType())));
00812           Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(),
00813                                  RoundVT, isVolatile, isNonTemporal, Alignment,
00814                                  AAInfo);
00815 
00816           // Store the remaining ExtraWidth bits.
00817           IncrementSize = RoundWidth / 8;
00818           Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
00819                             DAG.getConstant(IncrementSize, Ptr.getValueType()));
00820           Lo = DAG.getTruncStore(Chain, dl, Value, Ptr,
00821                               ST->getPointerInfo().getWithOffset(IncrementSize),
00822                                  ExtraVT, isVolatile, isNonTemporal,
00823                                  MinAlign(Alignment, IncrementSize), AAInfo);
00824         }
00825 
00826         // The order of the stores doesn't matter.
00827         SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
00828         ReplaceNode(SDValue(Node, 0), Result);
00829       } else {
00830         switch (TLI.getTruncStoreAction(ST->getValue().getSimpleValueType(),
00831                                         StVT.getSimpleVT())) {
00832         default: llvm_unreachable("This action is not supported yet!");
00833         case TargetLowering::Legal: {
00834           unsigned AS = ST->getAddressSpace();
00835           unsigned Align = ST->getAlignment();
00836           // If this is an unaligned store and the target doesn't support it,
00837           // expand it.
00838           if (!TLI.allowsMisalignedMemoryAccesses(ST->getMemoryVT(), AS, Align)) {
00839             Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
00840             unsigned ABIAlignment= TLI.getDataLayout()->getABITypeAlignment(Ty);
00841             if (Align < ABIAlignment)
00842               ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this);
00843           }
00844           break;
00845         }
00846         case TargetLowering::Custom: {
00847           SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
00848           if (Res.getNode())
00849             ReplaceNode(SDValue(Node, 0), Res);
00850           return;
00851         }
00852         case TargetLowering::Expand:
00853           assert(!StVT.isVector() &&
00854                  "Vector Stores are handled in LegalizeVectorOps");
00855 
00856           // TRUNCSTORE:i16 i32 -> STORE i16
00857           assert(TLI.isTypeLegal(StVT) &&
00858                  "Do not know how to expand this store!");
00859           Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value);
00860           SDValue Result =
00861             DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
00862                          isVolatile, isNonTemporal, Alignment, AAInfo);
00863           ReplaceNode(SDValue(Node, 0), Result);
00864           break;
00865         }
00866       }
00867     }
00868 }
00869 
00870 void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
00871   LoadSDNode *LD = cast<LoadSDNode>(Node);
00872   SDValue Chain = LD->getChain();  // The chain.
00873   SDValue Ptr = LD->getBasePtr();  // The base pointer.
00874   SDValue Value;                   // The value returned by the load op.
00875   SDLoc dl(Node);
00876 
00877   ISD::LoadExtType ExtType = LD->getExtensionType();
00878   if (ExtType == ISD::NON_EXTLOAD) {
00879     MVT VT = Node->getSimpleValueType(0);
00880     SDValue RVal = SDValue(Node, 0);
00881     SDValue RChain = SDValue(Node, 1);
00882 
00883     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
00884     default: llvm_unreachable("This action is not supported yet!");
00885     case TargetLowering::Legal: {
00886       unsigned AS = LD->getAddressSpace();
00887       unsigned Align = LD->getAlignment();
00888       // If this is an unaligned load and the target doesn't support it,
00889       // expand it.
00890       if (!TLI.allowsMisalignedMemoryAccesses(LD->getMemoryVT(), AS, Align)) {
00891         Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
00892         unsigned ABIAlignment =
00893           TLI.getDataLayout()->getABITypeAlignment(Ty);
00894         if (Align < ABIAlignment){
00895           ExpandUnalignedLoad(cast<LoadSDNode>(Node), DAG, TLI, RVal, RChain);
00896         }
00897       }
00898       break;
00899     }
00900     case TargetLowering::Custom: {
00901       SDValue Res = TLI.LowerOperation(RVal, DAG);
00902       if (Res.getNode()) {
00903         RVal = Res;
00904         RChain = Res.getValue(1);
00905       }
00906       break;
00907     }
00908     case TargetLowering::Promote: {
00909       MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
00910       assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
00911              "Can only promote loads to same size type");
00912 
00913       SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand());
00914       RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res);
00915       RChain = Res.getValue(1);
00916       break;
00917     }
00918     }
00919     if (RChain.getNode() != Node) {
00920       assert(RVal.getNode() != Node && "Load must be completely replaced");
00921       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal);
00922       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
00923       if (UpdatedNodes) {
00924         UpdatedNodes->insert(RVal.getNode());
00925         UpdatedNodes->insert(RChain.getNode());
00926       }
00927       ReplacedNode(Node);
00928     }
00929     return;
00930   }
00931 
00932   EVT SrcVT = LD->getMemoryVT();
00933   unsigned SrcWidth = SrcVT.getSizeInBits();
00934   unsigned Alignment = LD->getAlignment();
00935   bool isVolatile = LD->isVolatile();
00936   bool isNonTemporal = LD->isNonTemporal();
00937   bool isInvariant = LD->isInvariant();
00938   AAMDNodes AAInfo = LD->getAAInfo();
00939 
00940   if (SrcWidth != SrcVT.getStoreSizeInBits() &&
00941       // Some targets pretend to have an i1 loading operation, and actually
00942       // load an i8.  This trick is correct for ZEXTLOAD because the top 7
00943       // bits are guaranteed to be zero; it helps the optimizers understand
00944       // that these bits are zero.  It is also useful for EXTLOAD, since it
00945       // tells the optimizers that those bits are undefined.  It would be
00946       // nice to have an effective generic way of getting these benefits...
00947       // Until such a way is found, don't insist on promoting i1 here.
00948       (SrcVT != MVT::i1 ||
00949        TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
00950     // Promote to a byte-sized load if not loading an integral number of
00951     // bytes.  For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
00952     unsigned NewWidth = SrcVT.getStoreSizeInBits();
00953     EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
00954     SDValue Ch;
00955 
00956     // The extra bits are guaranteed to be zero, since we stored them that
00957     // way.  A zext load from NVT thus automatically gives zext from SrcVT.
00958 
00959     ISD::LoadExtType NewExtType =
00960       ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
00961 
00962     SDValue Result =
00963       DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
00964                      Chain, Ptr, LD->getPointerInfo(),
00965                      NVT, isVolatile, isNonTemporal, isInvariant, Alignment,
00966                      AAInfo);
00967 
00968     Ch = Result.getValue(1); // The chain.
00969 
00970     if (ExtType == ISD::SEXTLOAD)
00971       // Having the top bits zero doesn't help when sign extending.
00972       Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
00973                            Result.getValueType(),
00974                            Result, DAG.getValueType(SrcVT));
00975     else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
00976       // All the top bits are guaranteed to be zero - inform the optimizers.
00977       Result = DAG.getNode(ISD::AssertZext, dl,
00978                            Result.getValueType(), Result,
00979                            DAG.getValueType(SrcVT));
00980 
00981     Value = Result;
00982     Chain = Ch;
00983   } else if (SrcWidth & (SrcWidth - 1)) {
00984     // If not loading a power-of-2 number of bits, expand as two loads.
00985     assert(!SrcVT.isVector() && "Unsupported extload!");
00986     unsigned RoundWidth = 1 << Log2_32(SrcWidth);
00987     assert(RoundWidth < SrcWidth);
00988     unsigned ExtraWidth = SrcWidth - RoundWidth;
00989     assert(ExtraWidth < RoundWidth);
00990     assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
00991            "Load size not an integral number of bytes!");
00992     EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
00993     EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
00994     SDValue Lo, Hi, Ch;
00995     unsigned IncrementSize;
00996 
00997     if (TLI.isLittleEndian()) {
00998       // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
00999       // Load the bottom RoundWidth bits.
01000       Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
01001                           Chain, Ptr,
01002                           LD->getPointerInfo(), RoundVT, isVolatile,
01003                           isNonTemporal, isInvariant, Alignment, AAInfo);
01004 
01005       // Load the remaining ExtraWidth bits.
01006       IncrementSize = RoundWidth / 8;
01007       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
01008                          DAG.getConstant(IncrementSize, Ptr.getValueType()));
01009       Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
01010                           LD->getPointerInfo().getWithOffset(IncrementSize),
01011                           ExtraVT, isVolatile, isNonTemporal, isInvariant,
01012                           MinAlign(Alignment, IncrementSize), AAInfo);
01013 
01014       // Build a factor node to remember that this load is independent of
01015       // the other one.
01016       Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
01017                        Hi.getValue(1));
01018 
01019       // Move the top bits to the right place.
01020       Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
01021                        DAG.getConstant(RoundWidth,
01022                                       TLI.getShiftAmountTy(Hi.getValueType())));
01023 
01024       // Join the hi and lo parts.
01025       Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
01026     } else {
01027       // Big endian - avoid unaligned loads.
01028       // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
01029       // Load the top RoundWidth bits.
01030       Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
01031                           LD->getPointerInfo(), RoundVT, isVolatile,
01032                           isNonTemporal, isInvariant, Alignment, AAInfo);
01033 
01034       // Load the remaining ExtraWidth bits.
01035       IncrementSize = RoundWidth / 8;
01036       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
01037                          DAG.getConstant(IncrementSize, Ptr.getValueType()));
01038       Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
01039                           dl, Node->getValueType(0), Chain, Ptr,
01040                           LD->getPointerInfo().getWithOffset(IncrementSize),
01041                           ExtraVT, isVolatile, isNonTemporal, isInvariant,
01042                           MinAlign(Alignment, IncrementSize), AAInfo);
01043 
01044       // Build a factor node to remember that this load is independent of
01045       // the other one.
01046       Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
01047                        Hi.getValue(1));
01048 
01049       // Move the top bits to the right place.
01050       Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
01051                        DAG.getConstant(ExtraWidth,
01052                                       TLI.getShiftAmountTy(Hi.getValueType())));
01053 
01054       // Join the hi and lo parts.
01055       Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
01056     }
01057 
01058     Chain = Ch;
01059   } else {
01060     bool isCustom = false;
01061     switch (TLI.getLoadExtAction(ExtType, SrcVT.getSimpleVT())) {
01062     default: llvm_unreachable("This action is not supported yet!");
01063     case TargetLowering::Custom:
01064       isCustom = true;
01065       // FALLTHROUGH
01066     case TargetLowering::Legal: {
01067       Value = SDValue(Node, 0);
01068       Chain = SDValue(Node, 1);
01069 
01070       if (isCustom) {
01071         SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
01072         if (Res.getNode()) {
01073           Value = Res;
01074           Chain = Res.getValue(1);
01075         }
01076       } else {
01077         // If this is an unaligned load and the target doesn't support
01078         // it, expand it.
01079         EVT MemVT = LD->getMemoryVT();
01080         unsigned AS = LD->getAddressSpace();
01081         unsigned Align = LD->getAlignment();
01082         if (!TLI.allowsMisalignedMemoryAccesses(MemVT, AS, Align)) {
01083           Type *Ty =
01084             LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
01085           unsigned ABIAlignment =
01086             TLI.getDataLayout()->getABITypeAlignment(Ty);
01087           if (Align < ABIAlignment){
01088             ExpandUnalignedLoad(cast<LoadSDNode>(Node),
01089                                 DAG, TLI, Value, Chain);
01090           }
01091         }
01092       }
01093       break;
01094     }
01095     case TargetLowering::Expand:
01096       if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) &&
01097           TLI.isTypeLegal(SrcVT)) {
01098         SDValue Load = DAG.getLoad(SrcVT, dl, Chain, Ptr,
01099                                    LD->getMemOperand());
01100         unsigned ExtendOp;
01101         switch (ExtType) {
01102         case ISD::EXTLOAD:
01103           ExtendOp = (SrcVT.isFloatingPoint() ?
01104                       ISD::FP_EXTEND : ISD::ANY_EXTEND);
01105           break;
01106         case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
01107         case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
01108         default: llvm_unreachable("Unexpected extend load type!");
01109         }
01110         Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
01111         Chain = Load.getValue(1);
01112         break;
01113       }
01114 
01115       assert(!SrcVT.isVector() &&
01116              "Vector Loads are handled in LegalizeVectorOps");
01117 
01118       // FIXME: This does not work for vectors on most targets.  Sign-
01119       // and zero-extend operations are currently folded into extending
01120       // loads, whether they are legal or not, and then we end up here
01121       // without any support for legalizing them.
01122       assert(ExtType != ISD::EXTLOAD &&
01123              "EXTLOAD should always be supported!");
01124       // Turn the unsupported load into an EXTLOAD followed by an
01125       // explicit zero/sign extend inreg.
01126       SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl,
01127                                       Node->getValueType(0),
01128                                       Chain, Ptr, SrcVT,
01129                                       LD->getMemOperand());
01130       SDValue ValRes;
01131       if (ExtType == ISD::SEXTLOAD)
01132         ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
01133                              Result.getValueType(),
01134                              Result, DAG.getValueType(SrcVT));
01135       else
01136         ValRes = DAG.getZeroExtendInReg(Result, dl,
01137                                         SrcVT.getScalarType());
01138       Value = ValRes;
01139       Chain = Result.getValue(1);
01140       break;
01141     }
01142   }
01143 
01144   // Since loads produce two values, make sure to remember that we legalized
01145   // both of them.
01146   if (Chain.getNode() != Node) {
01147     assert(Value.getNode() != Node && "Load must be completely replaced");
01148     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value);
01149     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
01150     if (UpdatedNodes) {
01151       UpdatedNodes->insert(Value.getNode());
01152       UpdatedNodes->insert(Chain.getNode());
01153     }
01154     ReplacedNode(Node);
01155   }
01156 }
01157 
01158 /// LegalizeOp - Return a legal replacement for the given operation, with
01159 /// all legal operands.
01160 void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
01161   DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG));
01162 
01163   if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
01164     return;
01165 
01166   for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
01167     assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
01168              TargetLowering::TypeLegal &&
01169            "Unexpected illegal type!");
01170 
01171   for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
01172     assert((TLI.getTypeAction(*DAG.getContext(),
01173                               Node->getOperand(i).getValueType()) ==
01174               TargetLowering::TypeLegal ||
01175             Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
01176            "Unexpected illegal type!");
01177 
01178   // Figure out the correct action; the way to query this varies by opcode
01179   TargetLowering::LegalizeAction Action = TargetLowering::Legal;
01180   bool SimpleFinishLegalizing = true;
01181   switch (Node->getOpcode()) {
01182   case ISD::INTRINSIC_W_CHAIN:
01183   case ISD::INTRINSIC_WO_CHAIN:
01184   case ISD::INTRINSIC_VOID:
01185   case ISD::STACKSAVE:
01186     Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
01187     break;
01188   case ISD::VAARG:
01189     Action = TLI.getOperationAction(Node->getOpcode(),
01190                                     Node->getValueType(0));
01191     if (Action != TargetLowering::Promote)
01192       Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
01193     break;
01194   case ISD::FP_TO_FP16:
01195   case ISD::SINT_TO_FP:
01196   case ISD::UINT_TO_FP:
01197   case ISD::EXTRACT_VECTOR_ELT:
01198     Action = TLI.getOperationAction(Node->getOpcode(),
01199                                     Node->getOperand(0).getValueType());
01200     break;
01201   case ISD::FP_ROUND_INREG:
01202   case ISD::SIGN_EXTEND_INREG: {
01203     EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
01204     Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
01205     break;
01206   }
01207   case ISD::ATOMIC_STORE: {
01208     Action = TLI.getOperationAction(Node->getOpcode(),
01209                                     Node->getOperand(2).getValueType());
01210     break;
01211   }
01212   case ISD::SELECT_CC:
01213   case ISD::SETCC:
01214   case ISD::BR_CC: {
01215     unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
01216                          Node->getOpcode() == ISD::SETCC ? 2 : 1;
01217     unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
01218     MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType();
01219     ISD::CondCode CCCode =
01220         cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
01221     Action = TLI.getCondCodeAction(CCCode, OpVT);
01222     if (Action == TargetLowering::Legal) {
01223       if (Node->getOpcode() == ISD::SELECT_CC)
01224         Action = TLI.getOperationAction(Node->getOpcode(),
01225                                         Node->getValueType(0));
01226       else
01227         Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
01228     }
01229     break;
01230   }
01231   case ISD::LOAD:
01232   case ISD::STORE:
01233     // FIXME: Model these properly.  LOAD and STORE are complicated, and
01234     // STORE expects the unlegalized operand in some cases.
01235     SimpleFinishLegalizing = false;
01236     break;
01237   case ISD::CALLSEQ_START:
01238   case ISD::CALLSEQ_END:
01239     // FIXME: This shouldn't be necessary.  These nodes have special properties
01240     // dealing with the recursive nature of legalization.  Removing this
01241     // special case should be done as part of making LegalizeDAG non-recursive.
01242     SimpleFinishLegalizing = false;
01243     break;
01244   case ISD::EXTRACT_ELEMENT:
01245   case ISD::FLT_ROUNDS_:
01246   case ISD::SADDO:
01247   case ISD::SSUBO:
01248   case ISD::UADDO:
01249   case ISD::USUBO:
01250   case ISD::SMULO:
01251   case ISD::UMULO:
01252   case ISD::FPOWI:
01253   case ISD::MERGE_VALUES:
01254   case ISD::EH_RETURN:
01255   case ISD::FRAME_TO_ARGS_OFFSET:
01256   case ISD::EH_SJLJ_SETJMP:
01257   case ISD::EH_SJLJ_LONGJMP:
01258     // These operations lie about being legal: when they claim to be legal,
01259     // they should actually be expanded.
01260     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
01261     if (Action == TargetLowering::Legal)
01262       Action = TargetLowering::Expand;
01263     break;
01264   case ISD::INIT_TRAMPOLINE:
01265   case ISD::ADJUST_TRAMPOLINE:
01266   case ISD::FRAMEADDR:
01267   case ISD::RETURNADDR:
01268     // These operations lie about being legal: when they claim to be legal,
01269     // they should actually be custom-lowered.
01270     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
01271     if (Action == TargetLowering::Legal)
01272       Action = TargetLowering::Custom;
01273     break;
01274   case ISD::READ_REGISTER:
01275   case ISD::WRITE_REGISTER:
01276     // Named register is legal in the DAG, but blocked by register name
01277     // selection if not implemented by target (to chose the correct register)
01278     // They'll be converted to Copy(To/From)Reg.
01279     Action = TargetLowering::Legal;
01280     break;
01281   case ISD::DEBUGTRAP:
01282     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
01283     if (Action == TargetLowering::Expand) {
01284       // replace ISD::DEBUGTRAP with ISD::TRAP
01285       SDValue NewVal;
01286       NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
01287                            Node->getOperand(0));
01288       ReplaceNode(Node, NewVal.getNode());
01289       LegalizeOp(NewVal.getNode());
01290       return;
01291     }
01292     break;
01293 
01294   default:
01295     if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
01296       Action = TargetLowering::Legal;
01297     } else {
01298       Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
01299     }
01300     break;
01301   }
01302 
01303   if (SimpleFinishLegalizing) {
01304     SDNode *NewNode = Node;
01305     switch (Node->getOpcode()) {
01306     default: break;
01307     case ISD::SHL:
01308     case ISD::SRL:
01309     case ISD::SRA:
01310     case ISD::ROTL:
01311     case ISD::ROTR:
01312       // Legalizing shifts/rotates requires adjusting the shift amount
01313       // to the appropriate width.
01314       if (!Node->getOperand(1).getValueType().isVector()) {
01315         SDValue SAO =
01316           DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
01317                                     Node->getOperand(1));
01318         HandleSDNode Handle(SAO);
01319         LegalizeOp(SAO.getNode());
01320         NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
01321                                          Handle.getValue());
01322       }
01323       break;
01324     case ISD::SRL_PARTS:
01325     case ISD::SRA_PARTS:
01326     case ISD::SHL_PARTS:
01327       // Legalizing shifts/rotates requires adjusting the shift amount
01328       // to the appropriate width.
01329       if (!Node->getOperand(2).getValueType().isVector()) {
01330         SDValue SAO =
01331           DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
01332                                     Node->getOperand(2));
01333         HandleSDNode Handle(SAO);
01334         LegalizeOp(SAO.getNode());
01335         NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
01336                                          Node->getOperand(1),
01337                                          Handle.getValue());
01338       }
01339       break;
01340     }
01341 
01342     if (NewNode != Node) {
01343       ReplaceNode(Node, NewNode);
01344       Node = NewNode;
01345     }
01346     switch (Action) {
01347     case TargetLowering::Legal:
01348       return;
01349     case TargetLowering::Custom: {
01350       // FIXME: The handling for custom lowering with multiple results is
01351       // a complete mess.
01352       SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
01353       if (Res.getNode()) {
01354         if (!(Res.getNode() != Node || Res.getResNo() != 0))
01355           return;
01356 
01357         if (Node->getNumValues() == 1) {
01358           // We can just directly replace this node with the lowered value.
01359           ReplaceNode(SDValue(Node, 0), Res);
01360           return;
01361         }
01362 
01363         SmallVector<SDValue, 8> ResultVals;
01364         for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
01365           ResultVals.push_back(Res.getValue(i));
01366         ReplaceNode(Node, ResultVals.data());
01367         return;
01368       }
01369     }
01370       // FALL THROUGH
01371     case TargetLowering::Expand:
01372       ExpandNode(Node);
01373       return;
01374     case TargetLowering::Promote:
01375       PromoteNode(Node);
01376       return;
01377     }
01378   }
01379 
01380   switch (Node->getOpcode()) {
01381   default:
01382 #ifndef NDEBUG
01383     dbgs() << "NODE: ";
01384     Node->dump( &DAG);
01385     dbgs() << "\n";
01386 #endif
01387     llvm_unreachable("Do not know how to legalize this operator!");
01388 
01389   case ISD::CALLSEQ_START:
01390   case ISD::CALLSEQ_END:
01391     break;
01392   case ISD::LOAD: {
01393     return LegalizeLoadOps(Node);
01394   }
01395   case ISD::STORE: {
01396     return LegalizeStoreOps(Node);
01397   }
01398   }
01399 }
01400 
01401 SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
01402   SDValue Vec = Op.getOperand(0);
01403   SDValue Idx = Op.getOperand(1);
01404   SDLoc dl(Op);
01405 
01406   // Before we generate a new store to a temporary stack slot, see if there is
01407   // already one that we can use. There often is because when we scalarize
01408   // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole
01409   // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in
01410   // the vector. If all are expanded here, we don't want one store per vector
01411   // element.
01412   SDValue StackPtr, Ch;
01413   for (SDNode::use_iterator UI = Vec.getNode()->use_begin(),
01414        UE = Vec.getNode()->use_end(); UI != UE; ++UI) {
01415     SDNode *User = *UI;
01416     if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) {
01417       if (ST->isIndexed() || ST->isTruncatingStore() ||
01418           ST->getValue() != Vec)
01419         continue;
01420 
01421       // Make sure that nothing else could have stored into the destination of
01422       // this store.
01423       if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode()))
01424         continue;
01425 
01426       StackPtr = ST->getBasePtr();
01427       Ch = SDValue(ST, 0);
01428       break;
01429     }
01430   }
01431 
01432   if (!Ch.getNode()) {
01433     // Store the value to a temporary stack slot, then LOAD the returned part.
01434     StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
01435     Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
01436                       MachinePointerInfo(), false, false, 0);
01437   }
01438 
01439   // Add the offset to the index.
01440   unsigned EltSize =
01441       Vec.getValueType().getVectorElementType().getSizeInBits()/8;
01442   Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
01443                     DAG.getConstant(EltSize, Idx.getValueType()));
01444 
01445   Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy());
01446   StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
01447 
01448   if (Op.getValueType().isVector())
01449     return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
01450                        false, false, false, 0);
01451   return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
01452                         MachinePointerInfo(),
01453                         Vec.getValueType().getVectorElementType(),
01454                         false, false, false, 0);
01455 }
01456 
01457 SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
01458   assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
01459 
01460   SDValue Vec  = Op.getOperand(0);
01461   SDValue Part = Op.getOperand(1);
01462   SDValue Idx  = Op.getOperand(2);
01463   SDLoc dl(Op);
01464 
01465   // Store the value to a temporary stack slot, then LOAD the returned part.
01466 
01467   SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
01468   int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
01469   MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
01470 
01471   // First store the whole vector.
01472   SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
01473                             false, false, 0);
01474 
01475   // Then store the inserted part.
01476 
01477   // Add the offset to the index.
01478   unsigned EltSize =
01479       Vec.getValueType().getVectorElementType().getSizeInBits()/8;
01480 
01481   Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
01482                     DAG.getConstant(EltSize, Idx.getValueType()));
01483   Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy());
01484 
01485   SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
01486                                     StackPtr);
01487 
01488   // Store the subvector.
01489   Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
01490                     MachinePointerInfo(), false, false, 0);
01491 
01492   // Finally, load the updated vector.
01493   return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
01494                      false, false, false, 0);
01495 }
01496 
01497 SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
01498   // We can't handle this case efficiently.  Allocate a sufficiently
01499   // aligned object on the stack, store each element into it, then load
01500   // the result as a vector.
01501   // Create the stack frame object.
01502   EVT VT = Node->getValueType(0);
01503   EVT EltVT = VT.getVectorElementType();
01504   SDLoc dl(Node);
01505   SDValue FIPtr = DAG.CreateStackTemporary(VT);
01506   int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
01507   MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
01508 
01509   // Emit a store of each element to the stack slot.
01510   SmallVector<SDValue, 8> Stores;
01511   unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
01512   // Store (in the right endianness) the elements to memory.
01513   for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
01514     // Ignore undef elements.
01515     if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
01516 
01517     unsigned Offset = TypeByteSize*i;
01518 
01519     SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
01520     Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
01521 
01522     // If the destination vector element type is narrower than the source
01523     // element type, only store the bits necessary.
01524     if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
01525       Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
01526                                          Node->getOperand(i), Idx,
01527                                          PtrInfo.getWithOffset(Offset),
01528                                          EltVT, false, false, 0));
01529     } else
01530       Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
01531                                     Node->getOperand(i), Idx,
01532                                     PtrInfo.getWithOffset(Offset),
01533                                     false, false, 0));
01534   }
01535 
01536   SDValue StoreChain;
01537   if (!Stores.empty())    // Not all undef elements?
01538     StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
01539   else
01540     StoreChain = DAG.getEntryNode();
01541 
01542   // Result is a load from the stack slot.
01543   return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo,
01544                      false, false, false, 0);
01545 }
01546 
01547 SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
01548   SDLoc dl(Node);
01549   SDValue Tmp1 = Node->getOperand(0);
01550   SDValue Tmp2 = Node->getOperand(1);
01551 
01552   // Get the sign bit of the RHS.  First obtain a value that has the same
01553   // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
01554   SDValue SignBit;
01555   EVT FloatVT = Tmp2.getValueType();
01556   EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
01557   if (TLI.isTypeLegal(IVT)) {
01558     // Convert to an integer with the same sign bit.
01559     SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
01560   } else {
01561     // Store the float to memory, then load the sign part out as an integer.
01562     MVT LoadTy = TLI.getPointerTy();
01563     // First create a temporary that is aligned for both the load and store.
01564     SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
01565     // Then store the float to it.
01566     SDValue Ch =
01567       DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
01568                    false, false, 0);
01569     if (TLI.isBigEndian()) {
01570       assert(FloatVT.isByteSized() && "Unsupported floating point type!");
01571       // Load out a legal integer with the same sign bit as the float.
01572       SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
01573                             false, false, false, 0);
01574     } else { // Little endian
01575       SDValue LoadPtr = StackPtr;
01576       // The float may be wider than the integer we are going to load.  Advance
01577       // the pointer so that the loaded integer will contain the sign bit.
01578       unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
01579       unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
01580       LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(), LoadPtr,
01581                            DAG.getConstant(ByteOffset, LoadPtr.getValueType()));
01582       // Load a legal integer containing the sign bit.
01583       SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
01584                             false, false, false, 0);
01585       // Move the sign bit to the top bit of the loaded integer.
01586       unsigned BitShift = LoadTy.getSizeInBits() -
01587         (FloatVT.getSizeInBits() - 8 * ByteOffset);
01588       assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
01589       if (BitShift)
01590         SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
01591                               DAG.getConstant(BitShift,
01592                                  TLI.getShiftAmountTy(SignBit.getValueType())));
01593     }
01594   }
01595   // Now get the sign bit proper, by seeing whether the value is negative.
01596   SignBit = DAG.getSetCC(dl, getSetCCResultType(SignBit.getValueType()),
01597                          SignBit, DAG.getConstant(0, SignBit.getValueType()),
01598                          ISD::SETLT);
01599   // Get the absolute value of the result.
01600   SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
01601   // Select between the nabs and abs value based on the sign bit of
01602   // the input.
01603   return DAG.getSelect(dl, AbsVal.getValueType(), SignBit,
01604                       DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
01605                       AbsVal);
01606 }
01607 
01608 void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
01609                                            SmallVectorImpl<SDValue> &Results) {
01610   unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
01611   assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
01612           " not tell us which reg is the stack pointer!");
01613   SDLoc dl(Node);
01614   EVT VT = Node->getValueType(0);
01615   SDValue Tmp1 = SDValue(Node, 0);
01616   SDValue Tmp2 = SDValue(Node, 1);
01617   SDValue Tmp3 = Node->getOperand(2);
01618   SDValue Chain = Tmp1.getOperand(0);
01619 
01620   // Chain the dynamic stack allocation so that it doesn't modify the stack
01621   // pointer when other instructions are using the stack.
01622   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true),
01623                                SDLoc(Node));
01624 
01625   SDValue Size  = Tmp2.getOperand(1);
01626   SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
01627   Chain = SP.getValue(1);
01628   unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
01629   unsigned StackAlign =
01630       TM.getSubtargetImpl()->getFrameLowering()->getStackAlignment();
01631   Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size);       // Value
01632   if (Align > StackAlign)
01633     Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
01634                        DAG.getConstant(-(uint64_t)Align, VT));
01635   Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1);     // Output chain
01636 
01637   Tmp2 = DAG.getCALLSEQ_END(Chain,  DAG.getIntPtrConstant(0, true),
01638                             DAG.getIntPtrConstant(0, true), SDValue(),
01639                             SDLoc(Node));
01640 
01641   Results.push_back(Tmp1);
01642   Results.push_back(Tmp2);
01643 }
01644 
01645 /// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
01646 /// condition code CC on the current target.
01647 ///
01648 /// If the SETCC has been legalized using AND / OR, then the legalized node
01649 /// will be stored in LHS. RHS and CC will be set to SDValue(). NeedInvert
01650 /// will be set to false.
01651 ///
01652 /// If the SETCC has been legalized by using getSetCCSwappedOperands(),
01653 /// then the values of LHS and RHS will be swapped, CC will be set to the
01654 /// new condition, and NeedInvert will be set to false.
01655 ///
01656 /// If the SETCC has been legalized using the inverse condcode, then LHS and
01657 /// RHS will be unchanged, CC will set to the inverted condcode, and NeedInvert
01658 /// will be set to true. The caller must invert the result of the SETCC with
01659 /// SelectionDAG::getLogicalNOT() or take equivalent action to swap the effect
01660 /// of a true/false result.
01661 ///
01662 /// \returns true if the SetCC has been legalized, false if it hasn't.
01663 bool SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
01664                                                  SDValue &LHS, SDValue &RHS,
01665                                                  SDValue &CC,
01666                                                  bool &NeedInvert,
01667                                                  SDLoc dl) {
01668   MVT OpVT = LHS.getSimpleValueType();
01669   ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
01670   NeedInvert = false;
01671   switch (TLI.getCondCodeAction(CCCode, OpVT)) {
01672   default: llvm_unreachable("Unknown condition code action!");
01673   case TargetLowering::Legal:
01674     // Nothing to do.
01675     break;
01676   case TargetLowering::Expand: {
01677     ISD::CondCode InvCC = ISD::getSetCCSwappedOperands(CCCode);
01678     if (TLI.isCondCodeLegal(InvCC, OpVT)) {
01679       std::swap(LHS, RHS);
01680       CC = DAG.getCondCode(InvCC);
01681       return true;
01682     }
01683     ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
01684     unsigned Opc = 0;
01685     switch (CCCode) {
01686     default: llvm_unreachable("Don't know how to expand this condition!");
01687     case ISD::SETO:
01688         assert(TLI.getCondCodeAction(ISD::SETOEQ, OpVT)
01689             == TargetLowering::Legal
01690             && "If SETO is expanded, SETOEQ must be legal!");
01691         CC1 = ISD::SETOEQ; CC2 = ISD::SETOEQ; Opc = ISD::AND; break;
01692     case ISD::SETUO:
01693         assert(TLI.getCondCodeAction(ISD::SETUNE, OpVT)
01694             == TargetLowering::Legal
01695             && "If SETUO is expanded, SETUNE must be legal!");
01696         CC1 = ISD::SETUNE; CC2 = ISD::SETUNE; Opc = ISD::OR;  break;
01697     case ISD::SETOEQ:
01698     case ISD::SETOGT:
01699     case ISD::SETOGE:
01700     case ISD::SETOLT:
01701     case ISD::SETOLE:
01702     case ISD::SETONE:
01703     case ISD::SETUEQ:
01704     case ISD::SETUNE:
01705     case ISD::SETUGT:
01706     case ISD::SETUGE:
01707     case ISD::SETULT:
01708     case ISD::SETULE:
01709         // If we are floating point, assign and break, otherwise fall through.
01710         if (!OpVT.isInteger()) {
01711           // We can use the 4th bit to tell if we are the unordered
01712           // or ordered version of the opcode.
01713           CC2 = ((unsigned)CCCode & 0x8U) ? ISD::SETUO : ISD::SETO;
01714           Opc = ((unsigned)CCCode & 0x8U) ? ISD::OR : ISD::AND;
01715           CC1 = (ISD::CondCode)(((int)CCCode & 0x7) | 0x10);
01716           break;
01717         }
01718         // Fallthrough if we are unsigned integer.
01719     case ISD::SETLE:
01720     case ISD::SETGT:
01721     case ISD::SETGE:
01722     case ISD::SETLT:
01723       // We only support using the inverted operation, which is computed above
01724       // and not a different manner of supporting expanding these cases.
01725       llvm_unreachable("Don't know how to expand this condition!");
01726     case ISD::SETNE:
01727     case ISD::SETEQ:
01728       // Try inverting the result of the inverse condition.
01729       InvCC = CCCode == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ;
01730       if (TLI.isCondCodeLegal(InvCC, OpVT)) {
01731         CC = DAG.getCondCode(InvCC);
01732         NeedInvert = true;
01733         return true;
01734       }
01735       // If inverting the condition didn't work then we have no means to expand
01736       // the condition.
01737       llvm_unreachable("Don't know how to expand this condition!");
01738     }
01739 
01740     SDValue SetCC1, SetCC2;
01741     if (CCCode != ISD::SETO && CCCode != ISD::SETUO) {
01742       // If we aren't the ordered or unorder operation,
01743       // then the pattern is (LHS CC1 RHS) Opc (LHS CC2 RHS).
01744       SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
01745       SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
01746     } else {
01747       // Otherwise, the pattern is (LHS CC1 LHS) Opc (RHS CC2 RHS)
01748       SetCC1 = DAG.getSetCC(dl, VT, LHS, LHS, CC1);
01749       SetCC2 = DAG.getSetCC(dl, VT, RHS, RHS, CC2);
01750     }
01751     LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
01752     RHS = SDValue();
01753     CC  = SDValue();
01754     return true;
01755   }
01756   }
01757   return false;
01758 }
01759 
01760 /// EmitStackConvert - Emit a store/load combination to the stack.  This stores
01761 /// SrcOp to a stack slot of type SlotVT, truncating it if needed.  It then does
01762 /// a load from the stack slot to DestVT, extending it if needed.
01763 /// The resultant code need not be legal.
01764 SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
01765                                                EVT SlotVT,
01766                                                EVT DestVT,
01767                                                SDLoc dl) {
01768   // Create the stack frame object.
01769   unsigned SrcAlign =
01770     TLI.getDataLayout()->getPrefTypeAlignment(SrcOp.getValueType().
01771                                               getTypeForEVT(*DAG.getContext()));
01772   SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
01773 
01774   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
01775   int SPFI = StackPtrFI->getIndex();
01776   MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
01777 
01778   unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
01779   unsigned SlotSize = SlotVT.getSizeInBits();
01780   unsigned DestSize = DestVT.getSizeInBits();
01781   Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
01782   unsigned DestAlign = TLI.getDataLayout()->getPrefTypeAlignment(DestType);
01783 
01784   // Emit a store to the stack slot.  Use a truncstore if the input value is
01785   // later than DestVT.
01786   SDValue Store;
01787 
01788   if (SrcSize > SlotSize)
01789     Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
01790                               PtrInfo, SlotVT, false, false, SrcAlign);
01791   else {
01792     assert(SrcSize == SlotSize && "Invalid store");
01793     Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
01794                          PtrInfo, false, false, SrcAlign);
01795   }
01796 
01797   // Result is a load from the stack slot.
01798   if (SlotSize == DestSize)
01799     return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
01800                        false, false, false, DestAlign);
01801 
01802   assert(SlotSize < DestSize && "Unknown extension!");
01803   return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
01804                         PtrInfo, SlotVT, false, false, false, DestAlign);
01805 }
01806 
01807 SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
01808   SDLoc dl(Node);
01809   // Create a vector sized/aligned stack slot, store the value to element #0,
01810   // then load the whole vector back out.
01811   SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
01812 
01813   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
01814   int SPFI = StackPtrFI->getIndex();
01815 
01816   SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
01817                                  StackPtr,
01818                                  MachinePointerInfo::getFixedStack(SPFI),
01819                                  Node->getValueType(0).getVectorElementType(),
01820                                  false, false, 0);
01821   return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
01822                      MachinePointerInfo::getFixedStack(SPFI),
01823                      false, false, false, 0);
01824 }
01825 
01826 static bool
01827 ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG,
01828                      const TargetLowering &TLI, SDValue &Res) {
01829   unsigned NumElems = Node->getNumOperands();
01830   SDLoc dl(Node);
01831   EVT VT = Node->getValueType(0);
01832 
01833   // Try to group the scalars into pairs, shuffle the pairs together, then
01834   // shuffle the pairs of pairs together, etc. until the vector has
01835   // been built. This will work only if all of the necessary shuffle masks
01836   // are legal.
01837 
01838   // We do this in two phases; first to check the legality of the shuffles,
01839   // and next, assuming that all shuffles are legal, to create the new nodes.
01840   for (int Phase = 0; Phase < 2; ++Phase) {
01841     SmallVector<std::pair<SDValue, SmallVector<int, 16> >, 16> IntermedVals,
01842                                                                NewIntermedVals;
01843     for (unsigned i = 0; i < NumElems; ++i) {
01844       SDValue V = Node->getOperand(i);
01845       if (V.getOpcode() == ISD::UNDEF)
01846         continue;
01847 
01848       SDValue Vec;
01849       if (Phase)
01850         Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V);
01851       IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i)));
01852     }
01853 
01854     while (IntermedVals.size() > 2) {
01855       NewIntermedVals.clear();
01856       for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) {
01857         // This vector and the next vector are shuffled together (simply to
01858         // append the one to the other).
01859         SmallVector<int, 16> ShuffleVec(NumElems, -1);
01860 
01861         SmallVector<int, 16> FinalIndices;
01862         FinalIndices.reserve(IntermedVals[i].second.size() +
01863                              IntermedVals[i+1].second.size());
01864         
01865         int k = 0;
01866         for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f;
01867              ++j, ++k) {
01868           ShuffleVec[k] = j;
01869           FinalIndices.push_back(IntermedVals[i].second[j]);
01870         }
01871         for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f;
01872              ++j, ++k) {
01873           ShuffleVec[k] = NumElems + j;
01874           FinalIndices.push_back(IntermedVals[i+1].second[j]);
01875         }
01876 
01877         SDValue Shuffle;
01878         if (Phase)
01879           Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first,
01880                                          IntermedVals[i+1].first,
01881                                          ShuffleVec.data());
01882         else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
01883           return false;
01884         NewIntermedVals.push_back(std::make_pair(Shuffle, FinalIndices));
01885       }
01886 
01887       // If we had an odd number of defined values, then append the last
01888       // element to the array of new vectors.
01889       if ((IntermedVals.size() & 1) != 0)
01890         NewIntermedVals.push_back(IntermedVals.back());
01891 
01892       IntermedVals.swap(NewIntermedVals);
01893     }
01894 
01895     assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 &&
01896            "Invalid number of intermediate vectors");
01897     SDValue Vec1 = IntermedVals[0].first;
01898     SDValue Vec2;
01899     if (IntermedVals.size() > 1)
01900       Vec2 = IntermedVals[1].first;
01901     else if (Phase)
01902       Vec2 = DAG.getUNDEF(VT);
01903 
01904     SmallVector<int, 16> ShuffleVec(NumElems, -1);
01905     for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i)
01906       ShuffleVec[IntermedVals[0].second[i]] = i;
01907     for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i)
01908       ShuffleVec[IntermedVals[1].second[i]] = NumElems + i;
01909 
01910     if (Phase)
01911       Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
01912     else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
01913       return false;
01914   }
01915 
01916   return true;
01917 }
01918 
01919 /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
01920 /// support the operation, but do support the resultant vector type.
01921 SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
01922   unsigned NumElems = Node->getNumOperands();
01923   SDValue Value1, Value2;
01924   SDLoc dl(Node);
01925   EVT VT = Node->getValueType(0);
01926   EVT OpVT = Node->getOperand(0).getValueType();
01927   EVT EltVT = VT.getVectorElementType();
01928 
01929   // If the only non-undef value is the low element, turn this into a
01930   // SCALAR_TO_VECTOR node.  If this is { X, X, X, X }, determine X.
01931   bool isOnlyLowElement = true;
01932   bool MoreThanTwoValues = false;
01933   bool isConstant = true;
01934   for (unsigned i = 0; i < NumElems; ++i) {
01935     SDValue V = Node->getOperand(i);
01936     if (V.getOpcode() == ISD::UNDEF)
01937       continue;
01938     if (i > 0)
01939       isOnlyLowElement = false;
01940     if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
01941       isConstant = false;
01942 
01943     if (!Value1.getNode()) {
01944       Value1 = V;
01945     } else if (!Value2.getNode()) {
01946       if (V != Value1)
01947         Value2 = V;
01948     } else if (V != Value1 && V != Value2) {
01949       MoreThanTwoValues = true;
01950     }
01951   }
01952 
01953   if (!Value1.getNode())
01954     return DAG.getUNDEF(VT);
01955 
01956   if (isOnlyLowElement)
01957     return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
01958 
01959   // If all elements are constants, create a load from the constant pool.
01960   if (isConstant) {
01961     SmallVector<Constant*, 16> CV;
01962     for (unsigned i = 0, e = NumElems; i != e; ++i) {
01963       if (ConstantFPSDNode *V =
01964           dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
01965         CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
01966       } else if (ConstantSDNode *V =
01967                  dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
01968         if (OpVT==EltVT)
01969           CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
01970         else {
01971           // If OpVT and EltVT don't match, EltVT is not legal and the
01972           // element values have been promoted/truncated earlier.  Undo this;
01973           // we don't want a v16i8 to become a v16i32 for example.
01974           const ConstantInt *CI = V->getConstantIntValue();
01975           CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
01976                                         CI->getZExtValue()));
01977         }
01978       } else {
01979         assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
01980         Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
01981         CV.push_back(UndefValue::get(OpNTy));
01982       }
01983     }
01984     Constant *CP = ConstantVector::get(CV);
01985     SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
01986     unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
01987     return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
01988                        MachinePointerInfo::getConstantPool(),
01989                        false, false, false, Alignment);
01990   }
01991 
01992   SmallSet<SDValue, 16> DefinedValues;
01993   for (unsigned i = 0; i < NumElems; ++i) {
01994     if (Node->getOperand(i).getOpcode() == ISD::UNDEF)
01995       continue;
01996     DefinedValues.insert(Node->getOperand(i));
01997   }
01998 
01999   if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) {
02000     if (!MoreThanTwoValues) {
02001       SmallVector<int, 8> ShuffleVec(NumElems, -1);
02002       for (unsigned i = 0; i < NumElems; ++i) {
02003         SDValue V = Node->getOperand(i);
02004         if (V.getOpcode() == ISD::UNDEF)
02005           continue;
02006         ShuffleVec[i] = V == Value1 ? 0 : NumElems;
02007       }
02008       if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
02009         // Get the splatted value into the low element of a vector register.
02010         SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
02011         SDValue Vec2;
02012         if (Value2.getNode())
02013           Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
02014         else
02015           Vec2 = DAG.getUNDEF(VT);
02016 
02017         // Return shuffle(LowValVec, undef, <0,0,0,0>)
02018         return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
02019       }
02020     } else {
02021       SDValue Res;
02022       if (ExpandBVWithShuffles(Node, DAG, TLI, Res))
02023         return Res;
02024     }
02025   }
02026 
02027   // Otherwise, we can't handle this case efficiently.
02028   return ExpandVectorBuildThroughStack(Node);
02029 }
02030 
02031 // ExpandLibCall - Expand a node into a call to a libcall.  If the result value
02032 // does not fit into a register, return the lo part and set the hi part to the
02033 // by-reg argument.  If it does fit into a single register, return the result
02034 // and leave the Hi part unset.
02035 SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
02036                                             bool isSigned) {
02037   TargetLowering::ArgListTy Args;
02038   TargetLowering::ArgListEntry Entry;
02039   for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
02040     EVT ArgVT = Node->getOperand(i).getValueType();
02041     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
02042     Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
02043     Entry.isSExt = isSigned;
02044     Entry.isZExt = !isSigned;
02045     Args.push_back(Entry);
02046   }
02047   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
02048                                          TLI.getPointerTy());
02049 
02050   Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
02051 
02052   // By default, the input chain to this libcall is the entry node of the
02053   // function. If the libcall is going to be emitted as a tail call then
02054   // TLI.isUsedByReturnOnly will change it to the right chain if the return
02055   // node which is being folded has a non-entry input chain.
02056   SDValue InChain = DAG.getEntryNode();
02057 
02058   // isTailCall may be true since the callee does not reference caller stack
02059   // frame. Check if it's in the right position.
02060   SDValue TCChain = InChain;
02061   bool isTailCall = TLI.isInTailCallPosition(DAG, Node, TCChain);
02062   if (isTailCall)
02063     InChain = TCChain;
02064 
02065   TargetLowering::CallLoweringInfo CLI(DAG);
02066   CLI.setDebugLoc(SDLoc(Node)).setChain(InChain)
02067     .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
02068     .setTailCall(isTailCall).setSExtResult(isSigned).setZExtResult(!isSigned);
02069 
02070   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
02071 
02072   if (!CallInfo.second.getNode())
02073     // It's a tailcall, return the chain (which is the DAG root).
02074     return DAG.getRoot();
02075 
02076   return CallInfo.first;
02077 }
02078 
02079 /// ExpandLibCall - Generate a libcall taking the given operands as arguments
02080 /// and returning a result of type RetVT.
02081 SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
02082                                             const SDValue *Ops, unsigned NumOps,
02083                                             bool isSigned, SDLoc dl) {
02084   TargetLowering::ArgListTy Args;
02085   Args.reserve(NumOps);
02086 
02087   TargetLowering::ArgListEntry Entry;
02088   for (unsigned i = 0; i != NumOps; ++i) {
02089     Entry.Node = Ops[i];
02090     Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
02091     Entry.isSExt = isSigned;
02092     Entry.isZExt = !isSigned;
02093     Args.push_back(Entry);
02094   }
02095   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
02096                                          TLI.getPointerTy());
02097 
02098   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
02099 
02100   TargetLowering::CallLoweringInfo CLI(DAG);
02101   CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
02102     .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
02103     .setSExtResult(isSigned).setZExtResult(!isSigned);
02104 
02105   std::pair<SDValue,SDValue> CallInfo = TLI.LowerCallTo(CLI);
02106 
02107   return CallInfo.first;
02108 }
02109 
02110 // ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
02111 // ExpandLibCall except that the first operand is the in-chain.
02112 std::pair<SDValue, SDValue>
02113 SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
02114                                          SDNode *Node,
02115                                          bool isSigned) {
02116   SDValue InChain = Node->getOperand(0);
02117 
02118   TargetLowering::ArgListTy Args;
02119   TargetLowering::ArgListEntry Entry;
02120   for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
02121     EVT ArgVT = Node->getOperand(i).getValueType();
02122     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
02123     Entry.Node = Node->getOperand(i);
02124     Entry.Ty = ArgTy;
02125     Entry.isSExt = isSigned;
02126     Entry.isZExt = !isSigned;
02127     Args.push_back(Entry);
02128   }
02129   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
02130                                          TLI.getPointerTy());
02131 
02132   Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
02133 
02134   TargetLowering::CallLoweringInfo CLI(DAG);
02135   CLI.setDebugLoc(SDLoc(Node)).setChain(InChain)
02136     .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
02137     .setSExtResult(isSigned).setZExtResult(!isSigned);
02138 
02139   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
02140 
02141   return CallInfo;
02142 }
02143 
02144 SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
02145                                               RTLIB::Libcall Call_F32,
02146                                               RTLIB::Libcall Call_F64,
02147                                               RTLIB::Libcall Call_F80,
02148                                               RTLIB::Libcall Call_F128,
02149                                               RTLIB::Libcall Call_PPCF128) {
02150   RTLIB::Libcall LC;
02151   switch (Node->getSimpleValueType(0).SimpleTy) {
02152   default: llvm_unreachable("Unexpected request for libcall!");
02153   case MVT::f32: LC = Call_F32; break;
02154   case MVT::f64: LC = Call_F64; break;
02155   case MVT::f80: LC = Call_F80; break;
02156   case MVT::f128: LC = Call_F128; break;
02157   case MVT::ppcf128: LC = Call_PPCF128; break;
02158   }
02159   return ExpandLibCall(LC, Node, false);
02160 }
02161 
02162 SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
02163                                                RTLIB::Libcall Call_I8,
02164                                                RTLIB::Libcall Call_I16,
02165                                                RTLIB::Libcall Call_I32,
02166                                                RTLIB::Libcall Call_I64,
02167                                                RTLIB::Libcall Call_I128) {
02168   RTLIB::Libcall LC;
02169   switch (Node->getSimpleValueType(0).SimpleTy) {
02170   default: llvm_unreachable("Unexpected request for libcall!");
02171   case MVT::i8:   LC = Call_I8; break;
02172   case MVT::i16:  LC = Call_I16; break;
02173   case MVT::i32:  LC = Call_I32; break;
02174   case MVT::i64:  LC = Call_I64; break;
02175   case MVT::i128: LC = Call_I128; break;
02176   }
02177   return ExpandLibCall(LC, Node, isSigned);
02178 }
02179 
02180 /// isDivRemLibcallAvailable - Return true if divmod libcall is available.
02181 static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
02182                                      const TargetLowering &TLI) {
02183   RTLIB::Libcall LC;
02184   switch (Node->getSimpleValueType(0).SimpleTy) {
02185   default: llvm_unreachable("Unexpected request for libcall!");
02186   case MVT::i8:   LC= isSigned ? RTLIB::SDIVREM_I8  : RTLIB::UDIVREM_I8;  break;
02187   case MVT::i16:  LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
02188   case MVT::i32:  LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
02189   case MVT::i64:  LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
02190   case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
02191   }
02192 
02193   return TLI.getLibcallName(LC) != nullptr;
02194 }
02195 
02196 /// useDivRem - Only issue divrem libcall if both quotient and remainder are
02197 /// needed.
02198 static bool useDivRem(SDNode *Node, bool isSigned, bool isDIV) {
02199   // The other use might have been replaced with a divrem already.
02200   unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
02201   unsigned OtherOpcode = 0;
02202   if (isSigned)
02203     OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
02204   else
02205     OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
02206 
02207   SDValue Op0 = Node->getOperand(0);
02208   SDValue Op1 = Node->getOperand(1);
02209   for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
02210          UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
02211     SDNode *User = *UI;
02212     if (User == Node)
02213       continue;
02214     if ((User->getOpcode() == OtherOpcode || User->getOpcode() == DivRemOpc) &&
02215         User->getOperand(0) == Op0 &&
02216         User->getOperand(1) == Op1)
02217       return true;
02218   }
02219   return false;
02220 }
02221 
02222 /// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
02223 /// pairs.
02224 void
02225 SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
02226                                           SmallVectorImpl<SDValue> &Results) {
02227   unsigned Opcode = Node->getOpcode();
02228   bool isSigned = Opcode == ISD::SDIVREM;
02229 
02230   RTLIB::Libcall LC;
02231   switch (Node->getSimpleValueType(0).SimpleTy) {
02232   default: llvm_unreachable("Unexpected request for libcall!");
02233   case MVT::i8:   LC= isSigned ? RTLIB::SDIVREM_I8  : RTLIB::UDIVREM_I8;  break;
02234   case MVT::i16:  LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
02235   case MVT::i32:  LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
02236   case MVT::i64:  LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
02237   case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
02238   }
02239 
02240   // The input chain to this libcall is the entry node of the function.
02241   // Legalizing the call will automatically add the previous call to the
02242   // dependence.
02243   SDValue InChain = DAG.getEntryNode();
02244 
02245   EVT RetVT = Node->getValueType(0);
02246   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
02247 
02248   TargetLowering::ArgListTy Args;
02249   TargetLowering::ArgListEntry Entry;
02250   for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
02251     EVT ArgVT = Node->getOperand(i).getValueType();
02252     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
02253     Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
02254     Entry.isSExt = isSigned;
02255     Entry.isZExt = !isSigned;
02256     Args.push_back(Entry);
02257   }
02258 
02259   // Also pass the return address of the remainder.
02260   SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
02261   Entry.Node = FIPtr;
02262   Entry.Ty = RetTy->getPointerTo();
02263   Entry.isSExt = isSigned;
02264   Entry.isZExt = !isSigned;
02265   Args.push_back(Entry);
02266 
02267   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
02268                                          TLI.getPointerTy());
02269 
02270   SDLoc dl(Node);
02271   TargetLowering::CallLoweringInfo CLI(DAG);
02272   CLI.setDebugLoc(dl).setChain(InChain)
02273     .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
02274     .setSExtResult(isSigned).setZExtResult(!isSigned);
02275 
02276   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
02277 
02278   // Remainder is loaded back from the stack frame.
02279   SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr,
02280                             MachinePointerInfo(), false, false, false, 0);
02281   Results.push_back(CallInfo.first);
02282   Results.push_back(Rem);
02283 }
02284 
02285 /// isSinCosLibcallAvailable - Return true if sincos libcall is available.
02286 static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) {
02287   RTLIB::Libcall LC;
02288   switch (Node->getSimpleValueType(0).SimpleTy) {
02289   default: llvm_unreachable("Unexpected request for libcall!");
02290   case MVT::f32:     LC = RTLIB::SINCOS_F32; break;
02291   case MVT::f64:     LC = RTLIB::SINCOS_F64; break;
02292   case MVT::f80:     LC = RTLIB::SINCOS_F80; break;
02293   case MVT::f128:    LC = RTLIB::SINCOS_F128; break;
02294   case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
02295   }
02296   return TLI.getLibcallName(LC) != nullptr;
02297 }
02298 
02299 /// canCombineSinCosLibcall - Return true if sincos libcall is available and
02300 /// can be used to combine sin and cos.
02301 static bool canCombineSinCosLibcall(SDNode *Node, const TargetLowering &TLI,
02302                                     const TargetMachine &TM) {
02303   if (!isSinCosLibcallAvailable(Node, TLI))
02304     return false;
02305   // GNU sin/cos functions set errno while sincos does not. Therefore
02306   // combining sin and cos is only safe if unsafe-fpmath is enabled.
02307   bool isGNU = Triple(TM.getTargetTriple()).getEnvironment() == Triple::GNU;
02308   if (isGNU && !TM.Options.UnsafeFPMath)
02309     return false;
02310   return true;
02311 }
02312 
02313 /// useSinCos - Only issue sincos libcall if both sin and cos are
02314 /// needed.
02315 static bool useSinCos(SDNode *Node) {
02316   unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
02317     ? ISD::FCOS : ISD::FSIN;
02318 
02319   SDValue Op0 = Node->getOperand(0);
02320   for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
02321        UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
02322     SDNode *User = *UI;
02323     if (User == Node)
02324       continue;
02325     // The other user might have been turned into sincos already.
02326     if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
02327       return true;
02328   }
02329   return false;
02330 }
02331 
02332 /// ExpandSinCosLibCall - Issue libcalls to sincos to compute sin / cos
02333 /// pairs.
02334 void
02335 SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node,
02336                                           SmallVectorImpl<SDValue> &Results) {
02337   RTLIB::Libcall LC;
02338   switch (Node->getSimpleValueType(0).SimpleTy) {
02339   default: llvm_unreachable("Unexpected request for libcall!");
02340   case MVT::f32:     LC = RTLIB::SINCOS_F32; break;
02341   case MVT::f64:     LC = RTLIB::SINCOS_F64; break;
02342   case MVT::f80:     LC = RTLIB::SINCOS_F80; break;
02343   case MVT::f128:    LC = RTLIB::SINCOS_F128; break;
02344   case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
02345   }
02346 
02347   // The input chain to this libcall is the entry node of the function.
02348   // Legalizing the call will automatically add the previous call to the
02349   // dependence.
02350   SDValue InChain = DAG.getEntryNode();
02351 
02352   EVT RetVT = Node->getValueType(0);
02353   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
02354 
02355   TargetLowering::ArgListTy Args;
02356   TargetLowering::ArgListEntry Entry;
02357 
02358   // Pass the argument.
02359   Entry.Node = Node->getOperand(0);
02360   Entry.Ty = RetTy;
02361   Entry.isSExt = false;
02362   Entry.isZExt = false;
02363   Args.push_back(Entry);
02364 
02365   // Pass the return address of sin.
02366   SDValue SinPtr = DAG.CreateStackTemporary(RetVT);
02367   Entry.Node = SinPtr;
02368   Entry.Ty = RetTy->getPointerTo();
02369   Entry.isSExt = false;
02370   Entry.isZExt = false;
02371   Args.push_back(Entry);
02372 
02373   // Also pass the return address of the cos.
02374   SDValue CosPtr = DAG.CreateStackTemporary(RetVT);
02375   Entry.Node = CosPtr;
02376   Entry.Ty = RetTy->getPointerTo();
02377   Entry.isSExt = false;
02378   Entry.isZExt = false;
02379   Args.push_back(Entry);
02380 
02381   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
02382                                          TLI.getPointerTy());
02383 
02384   SDLoc dl(Node);
02385   TargetLowering::CallLoweringInfo CLI(DAG);
02386   CLI.setDebugLoc(dl).setChain(InChain)
02387     .setCallee(TLI.getLibcallCallingConv(LC),
02388                Type::getVoidTy(*DAG.getContext()), Callee, std::move(Args), 0);
02389 
02390   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
02391 
02392   Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr,
02393                                 MachinePointerInfo(), false, false, false, 0));
02394   Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr,
02395                                 MachinePointerInfo(), false, false, false, 0));
02396 }
02397 
02398 /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
02399 /// INT_TO_FP operation of the specified operand when the target requests that
02400 /// we expand it.  At this point, we know that the result and operand types are
02401 /// legal for the target.
02402 SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
02403                                                    SDValue Op0,
02404                                                    EVT DestVT,
02405                                                    SDLoc dl) {
02406   if (Op0.getValueType() == MVT::i32 && TLI.isTypeLegal(MVT::f64)) {
02407     // simple 32-bit [signed|unsigned] integer to float/double expansion
02408 
02409     // Get the stack frame index of a 8 byte buffer.
02410     SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
02411 
02412     // word offset constant for Hi/Lo address computation
02413     SDValue WordOff = DAG.getConstant(sizeof(int), StackSlot.getValueType());
02414     // set up Hi and Lo (into buffer) address based on endian
02415     SDValue Hi = StackSlot;
02416     SDValue Lo = DAG.getNode(ISD::ADD, dl, StackSlot.getValueType(),
02417                              StackSlot, WordOff);
02418     if (TLI.isLittleEndian())
02419       std::swap(Hi, Lo);
02420 
02421     // if signed map to unsigned space
02422     SDValue Op0Mapped;
02423     if (isSigned) {
02424       // constant used to invert sign bit (signed to unsigned mapping)
02425       SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
02426       Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
02427     } else {
02428       Op0Mapped = Op0;
02429     }
02430     // store the lo of the constructed double - based on integer input
02431     SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
02432                                   Op0Mapped, Lo, MachinePointerInfo(),
02433                                   false, false, 0);
02434     // initial hi portion of constructed double
02435     SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
02436     // store the hi of the constructed double - biased exponent
02437     SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
02438                                   MachinePointerInfo(),
02439                                   false, false, 0);
02440     // load the constructed double
02441     SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
02442                                MachinePointerInfo(), false, false, false, 0);
02443     // FP constant to bias correct the final result
02444     SDValue Bias = DAG.getConstantFP(isSigned ?
02445                                      BitsToDouble(0x4330000080000000ULL) :
02446                                      BitsToDouble(0x4330000000000000ULL),
02447                                      MVT::f64);
02448     // subtract the bias
02449     SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
02450     // final result
02451     SDValue Result;
02452     // handle final rounding
02453     if (DestVT == MVT::f64) {
02454       // do nothing
02455       Result = Sub;
02456     } else if (DestVT.bitsLT(MVT::f64)) {
02457       Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
02458                            DAG.getIntPtrConstant(0));
02459     } else if (DestVT.bitsGT(MVT::f64)) {
02460       Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
02461     }
02462     return Result;
02463   }
02464   assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
02465   // Code below here assumes !isSigned without checking again.
02466 
02467   // Implementation of unsigned i64 to f64 following the algorithm in
02468   // __floatundidf in compiler_rt. This implementation has the advantage
02469   // of performing rounding correctly, both in the default rounding mode
02470   // and in all alternate rounding modes.
02471   // TODO: Generalize this for use with other types.
02472   if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
02473     SDValue TwoP52 =
02474       DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
02475     SDValue TwoP84PlusTwoP52 =
02476       DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
02477     SDValue TwoP84 =
02478       DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
02479 
02480     SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
02481     SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
02482                              DAG.getConstant(32, MVT::i64));
02483     SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
02484     SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
02485     SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
02486     SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
02487     SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
02488                                 TwoP84PlusTwoP52);
02489     return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
02490   }
02491 
02492   // Implementation of unsigned i64 to f32.
02493   // TODO: Generalize this for use with other types.
02494   if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
02495     // For unsigned conversions, convert them to signed conversions using the
02496     // algorithm from the x86_64 __floatundidf in compiler_rt.
02497     if (!isSigned) {
02498       SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
02499 
02500       SDValue ShiftConst =
02501           DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
02502       SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
02503       SDValue AndConst = DAG.getConstant(1, MVT::i64);
02504       SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
02505       SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
02506 
02507       SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
02508       SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
02509 
02510       // TODO: This really should be implemented using a branch rather than a
02511       // select.  We happen to get lucky and machinesink does the right
02512       // thing most of the time.  This would be a good candidate for a
02513       //pseudo-op, or, even better, for whole-function isel.
02514       SDValue SignBitTest = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
02515         Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
02516       return DAG.getSelect(dl, MVT::f32, SignBitTest, Slow, Fast);
02517     }
02518 
02519     // Otherwise, implement the fully general conversion.
02520 
02521     SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
02522          DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
02523     SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
02524          DAG.getConstant(UINT64_C(0x800), MVT::i64));
02525     SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
02526          DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
02527     SDValue Ne = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
02528                    And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
02529     SDValue Sel = DAG.getSelect(dl, MVT::i64, Ne, Or, Op0);
02530     SDValue Ge = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
02531                    Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
02532                    ISD::SETUGE);
02533     SDValue Sel2 = DAG.getSelect(dl, MVT::i64, Ge, Sel, Op0);
02534     EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
02535 
02536     SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
02537                              DAG.getConstant(32, SHVT));
02538     SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
02539     SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
02540     SDValue TwoP32 =
02541       DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
02542     SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
02543     SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
02544     SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
02545     SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
02546     return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
02547                        DAG.getIntPtrConstant(0));
02548   }
02549 
02550   SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
02551 
02552   SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(Op0.getValueType()),
02553                                  Op0, DAG.getConstant(0, Op0.getValueType()),
02554                                  ISD::SETLT);
02555   SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
02556   SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
02557                                     SignSet, Four, Zero);
02558 
02559   // If the sign bit of the integer is set, the large number will be treated
02560   // as a negative number.  To counteract this, the dynamic code adds an
02561   // offset depending on the data type.
02562   uint64_t FF;
02563   switch (Op0.getSimpleValueType().SimpleTy) {
02564   default: llvm_unreachable("Unsupported integer type!");
02565   case MVT::i8 : FF = 0x43800000ULL; break;  // 2^8  (as a float)
02566   case MVT::i16: FF = 0x47800000ULL; break;  // 2^16 (as a float)
02567   case MVT::i32: FF = 0x4F800000ULL; break;  // 2^32 (as a float)
02568   case MVT::i64: FF = 0x5F800000ULL; break;  // 2^64 (as a float)
02569   }
02570   if (TLI.isLittleEndian()) FF <<= 32;
02571   Constant *FudgeFactor = ConstantInt::get(
02572                                        Type::getInt64Ty(*DAG.getContext()), FF);
02573 
02574   SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
02575   unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
02576   CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
02577   Alignment = std::min(Alignment, 4u);
02578   SDValue FudgeInReg;
02579   if (DestVT == MVT::f32)
02580     FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
02581                              MachinePointerInfo::getConstantPool(),
02582                              false, false, false, Alignment);
02583   else {
02584     SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
02585                                   DAG.getEntryNode(), CPIdx,
02586                                   MachinePointerInfo::getConstantPool(),
02587                                   MVT::f32, false, false, false, Alignment);
02588     HandleSDNode Handle(Load);
02589     LegalizeOp(Load.getNode());
02590     FudgeInReg = Handle.getValue();
02591   }
02592 
02593   return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
02594 }
02595 
02596 /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
02597 /// *INT_TO_FP operation of the specified operand when the target requests that
02598 /// we promote it.  At this point, we know that the result and operand types are
02599 /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
02600 /// operation that takes a larger input.
02601 SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
02602                                                     EVT DestVT,
02603                                                     bool isSigned,
02604                                                     SDLoc dl) {
02605   // First step, figure out the appropriate *INT_TO_FP operation to use.
02606   EVT NewInTy = LegalOp.getValueType();
02607 
02608   unsigned OpToUse = 0;
02609 
02610   // Scan for the appropriate larger type to use.
02611   while (1) {
02612     NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
02613     assert(NewInTy.isInteger() && "Ran out of possibilities!");
02614 
02615     // If the target supports SINT_TO_FP of this type, use it.
02616     if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
02617       OpToUse = ISD::SINT_TO_FP;
02618       break;
02619     }
02620     if (isSigned) continue;
02621 
02622     // If the target supports UINT_TO_FP of this type, use it.
02623     if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
02624       OpToUse = ISD::UINT_TO_FP;
02625       break;
02626     }
02627 
02628     // Otherwise, try a larger type.
02629   }
02630 
02631   // Okay, we found the operation and type to use.  Zero extend our input to the
02632   // desired type then run the operation on it.
02633   return DAG.getNode(OpToUse, dl, DestVT,
02634                      DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
02635                                  dl, NewInTy, LegalOp));
02636 }
02637 
02638 /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
02639 /// FP_TO_*INT operation of the specified operand when the target requests that
02640 /// we promote it.  At this point, we know that the result and operand types are
02641 /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
02642 /// operation that returns a larger result.
02643 SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
02644                                                     EVT DestVT,
02645                                                     bool isSigned,
02646                                                     SDLoc dl) {
02647   // First step, figure out the appropriate FP_TO*INT operation to use.
02648   EVT NewOutTy = DestVT;
02649 
02650   unsigned OpToUse = 0;
02651 
02652   // Scan for the appropriate larger type to use.
02653   while (1) {
02654     NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
02655     assert(NewOutTy.isInteger() && "Ran out of possibilities!");
02656 
02657     // A larger signed type can hold all unsigned values of the requested type,
02658     // so using FP_TO_SINT is valid
02659     if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
02660       OpToUse = ISD::FP_TO_SINT;
02661       break;
02662     }
02663 
02664     // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
02665     if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
02666       OpToUse = ISD::FP_TO_UINT;
02667       break;
02668     }
02669 
02670     // Otherwise, try a larger type.
02671   }
02672 
02673 
02674   // Okay, we found the operation and type to use.
02675   SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
02676 
02677   // Truncate the result of the extended FP_TO_*INT operation to the desired
02678   // size.
02679   return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
02680 }
02681 
02682 /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
02683 ///
02684 SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, SDLoc dl) {
02685   EVT VT = Op.getValueType();
02686   EVT SHVT = TLI.getShiftAmountTy(VT);
02687   SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
02688   switch (VT.getSimpleVT().SimpleTy) {
02689   default: llvm_unreachable("Unhandled Expand type in BSWAP!");
02690   case MVT::i16:
02691     Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
02692     Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
02693     return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
02694   case MVT::i32:
02695     Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
02696     Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
02697     Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
02698     Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
02699     Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
02700     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
02701     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
02702     Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
02703     return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
02704   case MVT::i64:
02705     Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
02706     Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
02707     Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
02708     Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
02709     Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
02710     Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
02711     Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
02712     Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
02713     Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
02714     Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
02715     Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
02716     Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
02717     Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
02718     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
02719     Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
02720     Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
02721     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
02722     Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
02723     Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
02724     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
02725     return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
02726   }
02727 }
02728 
02729 /// ExpandBitCount - Expand the specified bitcount instruction into operations.
02730 ///
02731 SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
02732                                              SDLoc dl) {
02733   switch (Opc) {
02734   default: llvm_unreachable("Cannot expand this yet!");
02735   case ISD::CTPOP: {
02736     EVT VT = Op.getValueType();
02737     EVT ShVT = TLI.getShiftAmountTy(VT);
02738     unsigned Len = VT.getSizeInBits();
02739 
02740     assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
02741            "CTPOP not implemented for this type.");
02742 
02743     // This is the "best" algorithm from
02744     // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
02745 
02746     SDValue Mask55 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x55)), VT);
02747     SDValue Mask33 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x33)), VT);
02748     SDValue Mask0F = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x0F)), VT);
02749     SDValue Mask01 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x01)), VT);
02750 
02751     // v = v - ((v >> 1) & 0x55555555...)
02752     Op = DAG.getNode(ISD::SUB, dl, VT, Op,
02753                      DAG.getNode(ISD::AND, dl, VT,
02754                                  DAG.getNode(ISD::SRL, dl, VT, Op,
02755                                              DAG.getConstant(1, ShVT)),
02756                                  Mask55));
02757     // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
02758     Op = DAG.getNode(ISD::ADD, dl, VT,
02759                      DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
02760                      DAG.getNode(ISD::AND, dl, VT,
02761                                  DAG.getNode(ISD::SRL, dl, VT, Op,
02762                                              DAG.getConstant(2, ShVT)),
02763                                  Mask33));
02764     // v = (v + (v >> 4)) & 0x0F0F0F0F...
02765     Op = DAG.getNode(ISD::AND, dl, VT,
02766                      DAG.getNode(ISD::ADD, dl, VT, Op,
02767                                  DAG.getNode(ISD::SRL, dl, VT, Op,
02768                                              DAG.getConstant(4, ShVT))),
02769                      Mask0F);
02770     // v = (v * 0x01010101...) >> (Len - 8)
02771     Op = DAG.getNode(ISD::SRL, dl, VT,
02772                      DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
02773                      DAG.getConstant(Len - 8, ShVT));
02774 
02775     return Op;
02776   }
02777   case ISD::CTLZ_ZERO_UNDEF:
02778     // This trivially expands to CTLZ.
02779     return DAG.getNode(ISD::CTLZ, dl, Op.getValueType(), Op);
02780   case ISD::CTLZ: {
02781     // for now, we do this:
02782     // x = x | (x >> 1);
02783     // x = x | (x >> 2);
02784     // ...
02785     // x = x | (x >>16);
02786     // x = x | (x >>32); // for 64-bit input
02787     // return popcount(~x);
02788     //
02789     // Ref: "Hacker's Delight" by Henry Warren
02790     EVT VT = Op.getValueType();
02791     EVT ShVT = TLI.getShiftAmountTy(VT);
02792     unsigned len = VT.getSizeInBits();
02793     for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
02794       SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
02795       Op = DAG.getNode(ISD::OR, dl, VT, Op,
02796                        DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
02797     }
02798     Op = DAG.getNOT(dl, Op, VT);
02799     return DAG.getNode(ISD::CTPOP, dl, VT, Op);
02800   }
02801   case ISD::CTTZ_ZERO_UNDEF:
02802     // This trivially expands to CTTZ.
02803     return DAG.getNode(ISD::CTTZ, dl, Op.getValueType(), Op);
02804   case ISD::CTTZ: {
02805     // for now, we use: { return popcount(~x & (x - 1)); }
02806     // unless the target has ctlz but not ctpop, in which case we use:
02807     // { return 32 - nlz(~x & (x-1)); }
02808     // Ref: "Hacker's Delight" by Henry Warren
02809     EVT VT = Op.getValueType();
02810     SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
02811                                DAG.getNOT(dl, Op, VT),
02812                                DAG.getNode(ISD::SUB, dl, VT, Op,
02813                                            DAG.getConstant(1, VT)));
02814     // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
02815     if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
02816         TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
02817       return DAG.getNode(ISD::SUB, dl, VT,
02818                          DAG.getConstant(VT.getSizeInBits(), VT),
02819                          DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
02820     return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
02821   }
02822   }
02823 }
02824 
02825 std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
02826   unsigned Opc = Node->getOpcode();
02827   MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
02828   RTLIB::Libcall LC;
02829 
02830   switch (Opc) {
02831   default:
02832     llvm_unreachable("Unhandled atomic intrinsic Expand!");
02833   case ISD::ATOMIC_SWAP:
02834     switch (VT.SimpleTy) {
02835     default: llvm_unreachable("Unexpected value type for atomic!");
02836     case MVT::i8:  LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
02837     case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
02838     case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
02839     case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
02840     case MVT::i128:LC = RTLIB::SYNC_LOCK_TEST_AND_SET_16;break;
02841     }
02842     break;
02843   case ISD::ATOMIC_CMP_SWAP:
02844     switch (VT.SimpleTy) {
02845     default: llvm_unreachable("Unexpected value type for atomic!");
02846     case MVT::i8:  LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
02847     case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
02848     case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
02849     case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
02850     case MVT::i128:LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_16;break;
02851     }
02852     break;
02853   case ISD::ATOMIC_LOAD_ADD:
02854     switch (VT.SimpleTy) {
02855     default: llvm_unreachable("Unexpected value type for atomic!");
02856     case MVT::i8:  LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
02857     case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
02858     case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
02859     case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
02860     case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_ADD_16;break;
02861     }
02862     break;
02863   case ISD::ATOMIC_LOAD_SUB:
02864     switch (VT.SimpleTy) {
02865     default: llvm_unreachable("Unexpected value type for atomic!");
02866     case MVT::i8:  LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
02867     case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
02868     case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
02869     case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
02870     case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_SUB_16;break;
02871     }
02872     break;
02873   case ISD::ATOMIC_LOAD_AND:
02874     switch (VT.SimpleTy) {
02875     default: llvm_unreachable("Unexpected value type for atomic!");
02876     case MVT::i8:  LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
02877     case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
02878     case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
02879     case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
02880     case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_AND_16;break;
02881     }
02882     break;
02883   case ISD::ATOMIC_LOAD_OR:
02884     switch (VT.SimpleTy) {
02885     default: llvm_unreachable("Unexpected value type for atomic!");
02886     case MVT::i8:  LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
02887     case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
02888     case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
02889     case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
02890     case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_OR_16;break;
02891     }
02892     break;
02893   case ISD::ATOMIC_LOAD_XOR:
02894     switch (VT.SimpleTy) {
02895     default: llvm_unreachable("Unexpected value type for atomic!");
02896     case MVT::i8:  LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
02897     case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
02898     case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
02899     case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
02900     case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_XOR_16;break;
02901     }
02902     break;
02903   case ISD::ATOMIC_LOAD_NAND:
02904     switch (VT.SimpleTy) {
02905     default: llvm_unreachable("Unexpected value type for atomic!");
02906     case MVT::i8:  LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
02907     case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
02908     case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
02909     case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
02910     case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_NAND_16;break;
02911     }
02912     break;
02913   case ISD::ATOMIC_LOAD_MAX:
02914     switch (VT.SimpleTy) {
02915     default: llvm_unreachable("Unexpected value type for atomic!");
02916     case MVT::i8:  LC = RTLIB::SYNC_FETCH_AND_MAX_1; break;
02917     case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_MAX_2; break;
02918     case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_MAX_4; break;
02919     case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_MAX_8; break;
02920     case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_MAX_16;break;
02921     }
02922     break;
02923   case ISD::ATOMIC_LOAD_UMAX:
02924     switch (VT.SimpleTy) {
02925     default: llvm_unreachable("Unexpected value type for atomic!");
02926     case MVT::i8:  LC = RTLIB::SYNC_FETCH_AND_UMAX_1; break;
02927     case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_UMAX_2; break;
02928     case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_UMAX_4; break;
02929     case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_UMAX_8; break;
02930     case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_UMAX_16;break;
02931     }
02932     break;
02933   case ISD::ATOMIC_LOAD_MIN:
02934     switch (VT.SimpleTy) {
02935     default: llvm_unreachable("Unexpected value type for atomic!");
02936     case MVT::i8:  LC = RTLIB::SYNC_FETCH_AND_MIN_1; break;
02937     case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_MIN_2; break;
02938     case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_MIN_4; break;
02939     case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_MIN_8; break;
02940     case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_MIN_16;break;
02941     }
02942     break;
02943   case ISD::ATOMIC_LOAD_UMIN:
02944     switch (VT.SimpleTy) {
02945     default: llvm_unreachable("Unexpected value type for atomic!");
02946     case MVT::i8:  LC = RTLIB::SYNC_FETCH_AND_UMIN_1; break;
02947     case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_UMIN_2; break;
02948     case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_UMIN_4; break;
02949     case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_UMIN_8; break;
02950     case MVT::i128:LC = RTLIB::SYNC_FETCH_AND_UMIN_16;break;
02951     }
02952     break;
02953   }
02954 
02955   return ExpandChainLibCall(LC, Node, false);
02956 }
02957 
02958 void SelectionDAGLegalize::ExpandNode(SDNode *Node) {
02959   SmallVector<SDValue, 8> Results;
02960   SDLoc dl(Node);
02961   SDValue Tmp1, Tmp2, Tmp3, Tmp4;
02962   bool NeedInvert;
02963   switch (Node->getOpcode()) {
02964   case ISD::CTPOP:
02965   case ISD::CTLZ:
02966   case ISD::CTLZ_ZERO_UNDEF:
02967   case ISD::CTTZ:
02968   case ISD::CTTZ_ZERO_UNDEF:
02969     Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
02970     Results.push_back(Tmp1);
02971     break;
02972   case ISD::BSWAP:
02973     Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
02974     break;
02975   case ISD::FRAMEADDR:
02976   case ISD::RETURNADDR:
02977   case ISD::FRAME_TO_ARGS_OFFSET:
02978     Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
02979     break;
02980   case ISD::FLT_ROUNDS_:
02981     Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
02982     break;
02983   case ISD::EH_RETURN:
02984   case ISD::EH_LABEL:
02985   case ISD::PREFETCH:
02986   case ISD::VAEND:
02987   case ISD::EH_SJLJ_LONGJMP:
02988     // If the target didn't expand these, there's nothing to do, so just
02989     // preserve the chain and be done.
02990     Results.push_back(Node->getOperand(0));
02991     break;
02992   case ISD::EH_SJLJ_SETJMP:
02993     // If the target didn't expand this, just return 'zero' and preserve the
02994     // chain.
02995     Results.push_back(DAG.getConstant(0, MVT::i32));
02996     Results.push_back(Node->getOperand(0));
02997     break;
02998   case ISD::ATOMIC_FENCE: {
02999     // If the target didn't lower this, lower it to '__sync_synchronize()' call
03000     // FIXME: handle "fence singlethread" more efficiently.
03001     TargetLowering::ArgListTy Args;
03002 
03003     TargetLowering::CallLoweringInfo CLI(DAG);
03004     CLI.setDebugLoc(dl).setChain(Node->getOperand(0))
03005       .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
03006                  DAG.getExternalSymbol("__sync_synchronize",
03007                  TLI.getPointerTy()), std::move(Args), 0);
03008 
03009     std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
03010 
03011     Results.push_back(CallResult.second);
03012     break;
03013   }
03014   case ISD::ATOMIC_LOAD: {
03015     // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
03016     SDValue Zero = DAG.getConstant(0, Node->getValueType(0));
03017     SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
03018     SDValue Swap = DAG.getAtomicCmpSwap(
03019         ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
03020         Node->getOperand(0), Node->getOperand(1), Zero, Zero,
03021         cast<AtomicSDNode>(Node)->getMemOperand(),
03022         cast<AtomicSDNode>(Node)->getOrdering(),
03023         cast<AtomicSDNode>(Node)->getOrdering(),
03024         cast<AtomicSDNode>(Node)->getSynchScope());
03025     Results.push_back(Swap.getValue(0));
03026     Results.push_back(Swap.getValue(1));
03027     break;
03028   }
03029   case ISD::ATOMIC_STORE: {
03030     // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
03031     SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
03032                                  cast<AtomicSDNode>(Node)->getMemoryVT(),
03033                                  Node->getOperand(0),
03034                                  Node->getOperand(1), Node->getOperand(2),
03035                                  cast<AtomicSDNode>(Node)->getMemOperand(),
03036                                  cast<AtomicSDNode>(Node)->getOrdering(),
03037                                  cast<AtomicSDNode>(Node)->getSynchScope());
03038     Results.push_back(Swap.getValue(1));
03039     break;
03040   }
03041   // By default, atomic intrinsics are marked Legal and lowered. Targets
03042   // which don't support them directly, however, may want libcalls, in which
03043   // case they mark them Expand, and we get here.
03044   case ISD::ATOMIC_SWAP:
03045   case ISD::ATOMIC_LOAD_ADD:
03046   case ISD::ATOMIC_LOAD_SUB:
03047   case ISD::ATOMIC_LOAD_AND:
03048   case ISD::ATOMIC_LOAD_OR:
03049   case ISD::ATOMIC_LOAD_XOR:
03050   case ISD::ATOMIC_LOAD_NAND:
03051   case ISD::ATOMIC_LOAD_MIN:
03052   case ISD::ATOMIC_LOAD_MAX:
03053   case ISD::ATOMIC_LOAD_UMIN:
03054   case ISD::ATOMIC_LOAD_UMAX:
03055   case ISD::ATOMIC_CMP_SWAP: {
03056     std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
03057     Results.push_back(Tmp.first);
03058     Results.push_back(Tmp.second);
03059     break;
03060   }
03061   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
03062     // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
03063     // splits out the success value as a comparison. Expanding the resulting
03064     // ATOMIC_CMP_SWAP will produce a libcall.
03065     SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
03066     SDValue Res = DAG.getAtomicCmpSwap(
03067         ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
03068         Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
03069         Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand(),
03070         cast<AtomicSDNode>(Node)->getSuccessOrdering(),
03071         cast<AtomicSDNode>(Node)->getFailureOrdering(),
03072         cast<AtomicSDNode>(Node)->getSynchScope());
03073 
03074     SDValue Success = DAG.getSetCC(SDLoc(Node), Node->getValueType(1),
03075                                    Res, Node->getOperand(2), ISD::SETEQ);
03076 
03077     Results.push_back(Res.getValue(0));
03078     Results.push_back(Success);
03079     Results.push_back(Res.getValue(1));
03080     break;
03081   }
03082   case ISD::DYNAMIC_STACKALLOC:
03083     ExpandDYNAMIC_STACKALLOC(Node, Results);
03084     break;
03085   case ISD::MERGE_VALUES:
03086     for (unsigned i = 0; i < Node->getNumValues(); i++)
03087       Results.push_back(Node->getOperand(i));
03088     break;
03089   case ISD::UNDEF: {
03090     EVT VT = Node->getValueType(0);
03091     if (VT.isInteger())
03092       Results.push_back(DAG.getConstant(0, VT));
03093     else {
03094       assert(VT.isFloatingPoint() && "Unknown value type!");
03095       Results.push_back(DAG.getConstantFP(0, VT));
03096     }
03097     break;
03098   }
03099   case ISD::TRAP: {
03100     // If this operation is not supported, lower it to 'abort()' call
03101     TargetLowering::ArgListTy Args;
03102     TargetLowering::CallLoweringInfo CLI(DAG);
03103     CLI.setDebugLoc(dl).setChain(Node->getOperand(0))
03104       .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
03105                  DAG.getExternalSymbol("abort", TLI.getPointerTy()),
03106                  std::move(Args), 0);
03107     std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
03108 
03109     Results.push_back(CallResult.second);
03110     break;
03111   }
03112   case ISD::FP_ROUND:
03113   case ISD::BITCAST:
03114     Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
03115                             Node->getValueType(0), dl);
03116     Results.push_back(Tmp1);
03117     break;
03118   case ISD::FP_EXTEND:
03119     Tmp1 = EmitStackConvert(Node->getOperand(0),
03120                             Node->getOperand(0).getValueType(),
03121                             Node->getValueType(0), dl);
03122     Results.push_back(Tmp1);
03123     break;
03124   case ISD::SIGN_EXTEND_INREG: {
03125     // NOTE: we could fall back on load/store here too for targets without
03126     // SAR.  However, it is doubtful that any exist.
03127     EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
03128     EVT VT = Node->getValueType(0);
03129     EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
03130     if (VT.isVector())
03131       ShiftAmountTy = VT;
03132     unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
03133                         ExtraVT.getScalarType().getSizeInBits();
03134     SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
03135     Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
03136                        Node->getOperand(0), ShiftCst);
03137     Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
03138     Results.push_back(Tmp1);
03139     break;
03140   }
03141   case ISD::FP_ROUND_INREG: {
03142     // The only way we can lower this is to turn it into a TRUNCSTORE,
03143     // EXTLOAD pair, targeting a temporary location (a stack slot).
03144 
03145     // NOTE: there is a choice here between constantly creating new stack
03146     // slots and always reusing the same one.  We currently always create
03147     // new ones, as reuse may inhibit scheduling.
03148     EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
03149     Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
03150                             Node->getValueType(0), dl);
03151     Results.push_back(Tmp1);
03152     break;
03153   }
03154   case ISD::SINT_TO_FP:
03155   case ISD::UINT_TO_FP:
03156     Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
03157                                 Node->getOperand(0), Node->getValueType(0), dl);
03158     Results.push_back(Tmp1);
03159     break;
03160   case ISD::FP_TO_SINT:
03161     if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
03162       Results.push_back(Tmp1);
03163     break;
03164   case ISD::FP_TO_UINT: {
03165     SDValue True, False;
03166     EVT VT =  Node->getOperand(0).getValueType();
03167     EVT NVT = Node->getValueType(0);
03168     APFloat apf(DAG.EVTToAPFloatSemantics(VT),
03169                 APInt::getNullValue(VT.getSizeInBits()));
03170     APInt x = APInt::getSignBit(NVT.getSizeInBits());
03171     (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
03172     Tmp1 = DAG.getConstantFP(apf, VT);
03173     Tmp2 = DAG.getSetCC(dl, getSetCCResultType(VT),
03174                         Node->getOperand(0),
03175                         Tmp1, ISD::SETLT);
03176     True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
03177     False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
03178                         DAG.getNode(ISD::FSUB, dl, VT,
03179                                     Node->getOperand(0), Tmp1));
03180     False = DAG.getNode(ISD::XOR, dl, NVT, False,
03181                         DAG.getConstant(x, NVT));
03182     Tmp1 = DAG.getSelect(dl, NVT, Tmp2, True, False);
03183     Results.push_back(Tmp1);
03184     break;
03185   }
03186   case ISD::VAARG: {
03187     const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
03188     EVT VT = Node->getValueType(0);
03189     Tmp1 = Node->getOperand(0);
03190     Tmp2 = Node->getOperand(1);
03191     unsigned Align = Node->getConstantOperandVal(3);
03192 
03193     SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
03194                                      MachinePointerInfo(V),
03195                                      false, false, false, 0);
03196     SDValue VAList = VAListLoad;
03197 
03198     if (Align > TLI.getMinStackArgumentAlignment()) {
03199       assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
03200 
03201       VAList = DAG.getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
03202                            DAG.getConstant(Align - 1,
03203                                            VAList.getValueType()));
03204 
03205       VAList = DAG.getNode(ISD::AND, dl, VAList.getValueType(), VAList,
03206                            DAG.getConstant(-(int64_t)Align,
03207                                            VAList.getValueType()));
03208     }
03209 
03210     // Increment the pointer, VAList, to the next vaarg
03211     Tmp3 = DAG.getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
03212                        DAG.getConstant(TLI.getDataLayout()->
03213                           getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
03214                                        VAList.getValueType()));
03215     // Store the incremented VAList to the legalized pointer
03216     Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
03217                         MachinePointerInfo(V), false, false, 0);
03218     // Load the actual argument out of the pointer VAList
03219     Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
03220                                   false, false, false, 0));
03221     Results.push_back(Results[0].getValue(1));
03222     break;
03223   }
03224   case ISD::VACOPY: {
03225     // This defaults to loading a pointer from the input and storing it to the
03226     // output, returning the chain.
03227     const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
03228     const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
03229     Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
03230                        Node->getOperand(2), MachinePointerInfo(VS),
03231                        false, false, false, 0);
03232     Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
03233                         MachinePointerInfo(VD), false, false, 0);
03234     Results.push_back(Tmp1);
03235     break;
03236   }
03237   case ISD::EXTRACT_VECTOR_ELT:
03238     if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
03239       // This must be an access of the only element.  Return it.
03240       Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
03241                          Node->getOperand(0));
03242     else
03243       Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
03244     Results.push_back(Tmp1);
03245     break;
03246   case ISD::EXTRACT_SUBVECTOR:
03247     Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
03248     break;
03249   case ISD::INSERT_SUBVECTOR:
03250     Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
03251     break;
03252   case ISD::CONCAT_VECTORS: {
03253     Results.push_back(ExpandVectorBuildThroughStack(Node));
03254     break;
03255   }
03256   case ISD::SCALAR_TO_VECTOR:
03257     Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
03258     break;
03259   case ISD::INSERT_VECTOR_ELT:
03260     Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
03261                                               Node->getOperand(1),
03262                                               Node->getOperand(2), dl));
03263     break;
03264   case ISD::VECTOR_SHUFFLE: {
03265     SmallVector<int, 32> NewMask;
03266     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
03267 
03268     EVT VT = Node->getValueType(0);
03269     EVT EltVT = VT.getVectorElementType();
03270     SDValue Op0 = Node->getOperand(0);
03271     SDValue Op1 = Node->getOperand(1);
03272     if (!TLI.isTypeLegal(EltVT)) {
03273 
03274       EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
03275 
03276       // BUILD_VECTOR operands are allowed to be wider than the element type.
03277       // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
03278       // it.
03279       if (NewEltVT.bitsLT(EltVT)) {
03280 
03281         // Convert shuffle node.
03282         // If original node was v4i64 and the new EltVT is i32,
03283         // cast operands to v8i32 and re-build the mask.
03284 
03285         // Calculate new VT, the size of the new VT should be equal to original.
03286         EVT NewVT =
03287             EVT::getVectorVT(*DAG.getContext(), NewEltVT,
03288                              VT.getSizeInBits() / NewEltVT.getSizeInBits());
03289         assert(NewVT.bitsEq(VT));
03290 
03291         // cast operands to new VT
03292         Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
03293         Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
03294 
03295         // Convert the shuffle mask
03296         unsigned int factor =
03297                          NewVT.getVectorNumElements()/VT.getVectorNumElements();
03298 
03299         // EltVT gets smaller
03300         assert(factor > 0);
03301 
03302         for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
03303           if (Mask[i] < 0) {
03304             for (unsigned fi = 0; fi < factor; ++fi)
03305               NewMask.push_back(Mask[i]);
03306           }
03307           else {
03308             for (unsigned fi = 0; fi < factor; ++fi)
03309               NewMask.push_back(Mask[i]*factor+fi);
03310           }
03311         }
03312         Mask = NewMask;
03313         VT = NewVT;
03314       }
03315       EltVT = NewEltVT;
03316     }
03317     unsigned NumElems = VT.getVectorNumElements();
03318     SmallVector<SDValue, 16> Ops;
03319     for (unsigned i = 0; i != NumElems; ++i) {
03320       if (Mask[i] < 0) {
03321         Ops.push_back(DAG.getUNDEF(EltVT));
03322         continue;
03323       }
03324       unsigned Idx = Mask[i];
03325       if (Idx < NumElems)
03326         Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
03327                                   Op0,
03328                                   DAG.getConstant(Idx, TLI.getVectorIdxTy())));
03329       else
03330         Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
03331                                   Op1,
03332                                   DAG.getConstant(Idx - NumElems,
03333                                                   TLI.getVectorIdxTy())));
03334     }
03335 
03336     Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
03337     // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
03338     Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
03339     Results.push_back(Tmp1);
03340     break;
03341   }
03342   case ISD::EXTRACT_ELEMENT: {
03343     EVT OpTy = Node->getOperand(0).getValueType();
03344     if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
03345       // 1 -> Hi
03346       Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
03347                          DAG.getConstant(OpTy.getSizeInBits()/2,
03348                     TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
03349       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
03350     } else {
03351       // 0 -> Lo
03352       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
03353                          Node->getOperand(0));
03354     }
03355     Results.push_back(Tmp1);
03356     break;
03357   }
03358   case ISD::STACKSAVE:
03359     // Expand to CopyFromReg if the target set
03360     // StackPointerRegisterToSaveRestore.
03361     if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
03362       Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
03363                                            Node->getValueType(0)));
03364       Results.push_back(Results[0].getValue(1));
03365     } else {
03366       Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
03367       Results.push_back(Node->getOperand(0));
03368     }
03369     break;
03370   case ISD::STACKRESTORE:
03371     // Expand to CopyToReg if the target set
03372     // StackPointerRegisterToSaveRestore.
03373     if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
03374       Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
03375                                          Node->getOperand(1)));
03376     } else {
03377       Results.push_back(Node->getOperand(0));
03378     }
03379     break;
03380   case ISD::FCOPYSIGN:
03381     Results.push_back(ExpandFCOPYSIGN(Node));
03382     break;
03383   case ISD::FNEG:
03384     // Expand Y = FNEG(X) ->  Y = SUB -0.0, X
03385     Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
03386     Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
03387                        Node->getOperand(0));
03388     Results.push_back(Tmp1);
03389     break;
03390   case ISD::FABS: {
03391     // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
03392     EVT VT = Node->getValueType(0);
03393     Tmp1 = Node->getOperand(0);
03394     Tmp2 = DAG.getConstantFP(0.0, VT);
03395     Tmp2 = DAG.getSetCC(dl, getSetCCResultType(Tmp1.getValueType()),
03396                         Tmp1, Tmp2, ISD::SETUGT);
03397     Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
03398     Tmp1 = DAG.getSelect(dl, VT, Tmp2, Tmp1, Tmp3);
03399     Results.push_back(Tmp1);
03400     break;
03401   }
03402   case ISD::FSQRT:
03403     Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
03404                                       RTLIB::SQRT_F80, RTLIB::SQRT_F128,
03405                                       RTLIB::SQRT_PPCF128));
03406     break;
03407   case ISD::FSIN:
03408   case ISD::FCOS: {
03409     EVT VT = Node->getValueType(0);
03410     bool isSIN = Node->getOpcode() == ISD::FSIN;
03411     // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
03412     // fcos which share the same operand and both are used.
03413     if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
03414          canCombineSinCosLibcall(Node, TLI, TM))
03415         && useSinCos(Node)) {
03416       SDVTList VTs = DAG.getVTList(VT, VT);
03417       Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
03418       if (!isSIN)
03419         Tmp1 = Tmp1.getValue(1);
03420       Results.push_back(Tmp1);
03421     } else if (isSIN) {
03422       Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
03423                                         RTLIB::SIN_F80, RTLIB::SIN_F128,
03424                                         RTLIB::SIN_PPCF128));
03425     } else {
03426       Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
03427                                         RTLIB::COS_F80, RTLIB::COS_F128,
03428                                         RTLIB::COS_PPCF128));
03429     }
03430     break;
03431   }
03432   case ISD::FSINCOS:
03433     // Expand into sincos libcall.
03434     ExpandSinCosLibCall(Node, Results);
03435     break;
03436   case ISD::FLOG:
03437     Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
03438                                       RTLIB::LOG_F80, RTLIB::LOG_F128,
03439                                       RTLIB::LOG_PPCF128));
03440     break;
03441   case ISD::FLOG2:
03442     Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
03443                                       RTLIB::LOG2_F80, RTLIB::LOG2_F128,
03444                                       RTLIB::LOG2_PPCF128));
03445     break;
03446   case ISD::FLOG10:
03447     Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
03448                                       RTLIB::LOG10_F80, RTLIB::LOG10_F128,
03449                                       RTLIB::LOG10_PPCF128));
03450     break;
03451   case ISD::FEXP:
03452     Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
03453                                       RTLIB::EXP_F80, RTLIB::EXP_F128,
03454                                       RTLIB::EXP_PPCF128));
03455     break;
03456   case ISD::FEXP2:
03457     Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
03458                                       RTLIB::EXP2_F80, RTLIB::EXP2_F128,
03459                                       RTLIB::EXP2_PPCF128));
03460     break;
03461   case ISD::FTRUNC:
03462     Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
03463                                       RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
03464                                       RTLIB::TRUNC_PPCF128));
03465     break;
03466   case ISD::FFLOOR:
03467     Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
03468                                       RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
03469                                       RTLIB::FLOOR_PPCF128));
03470     break;
03471   case ISD::FCEIL:
03472     Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
03473                                       RTLIB::CEIL_F80, RTLIB::CEIL_F128,
03474                                       RTLIB::CEIL_PPCF128));
03475     break;
03476   case ISD::FRINT:
03477     Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
03478                                       RTLIB::RINT_F80, RTLIB::RINT_F128,
03479                                       RTLIB::RINT_PPCF128));
03480     break;
03481   case ISD::FNEARBYINT:
03482     Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
03483                                       RTLIB::NEARBYINT_F64,
03484                                       RTLIB::NEARBYINT_F80,
03485                                       RTLIB::NEARBYINT_F128,
03486                                       RTLIB::NEARBYINT_PPCF128));
03487     break;
03488   case ISD::FROUND:
03489     Results.push_back(ExpandFPLibCall(Node, RTLIB::ROUND_F32,
03490                                       RTLIB::ROUND_F64,
03491                                       RTLIB::ROUND_F80,
03492                                       RTLIB::ROUND_F128,
03493                                       RTLIB::ROUND_PPCF128));
03494     break;
03495   case ISD::FPOWI:
03496     Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
03497                                       RTLIB::POWI_F80, RTLIB::POWI_F128,
03498                                       RTLIB::POWI_PPCF128));
03499     break;
03500   case ISD::FPOW:
03501     Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
03502                                       RTLIB::POW_F80, RTLIB::POW_F128,
03503                                       RTLIB::POW_PPCF128));
03504     break;
03505   case ISD::FDIV:
03506     Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
03507                                       RTLIB::DIV_F80, RTLIB::DIV_F128,
03508                                       RTLIB::DIV_PPCF128));
03509     break;
03510   case ISD::FREM:
03511     Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
03512                                       RTLIB::REM_F80, RTLIB::REM_F128,
03513                                       RTLIB::REM_PPCF128));
03514     break;
03515   case ISD::FMA:
03516     Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
03517                                       RTLIB::FMA_F80, RTLIB::FMA_F128,
03518                                       RTLIB::FMA_PPCF128));
03519     break;
03520   case ISD::FADD:
03521     Results.push_back(ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64,
03522                                       RTLIB::ADD_F80, RTLIB::ADD_F128,
03523                                       RTLIB::ADD_PPCF128));
03524     break;
03525   case ISD::FMUL:
03526     Results.push_back(ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64,
03527                                       RTLIB::MUL_F80, RTLIB::MUL_F128,
03528                                       RTLIB::MUL_PPCF128));
03529     break;
03530   case ISD::FP16_TO_FP: {
03531     if (Node->getValueType(0) == MVT::f32) {
03532       Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
03533       break;
03534     }
03535 
03536     // We can extend to types bigger than f32 in two steps without changing the
03537     // result. Since "f16 -> f32" is much more commonly available, give CodeGen
03538     // the option of emitting that before resorting to a libcall.
03539     SDValue Res =
03540         DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
03541     Results.push_back(
03542         DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
03543     break;
03544   }
03545   case ISD::FP_TO_FP16: {
03546     RTLIB::Libcall LC =
03547         RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16);
03548     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16");
03549     Results.push_back(ExpandLibCall(LC, Node, false));
03550     break;
03551   }
03552   case ISD::ConstantFP: {
03553     ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
03554     // Check to see if this FP immediate is already legal.
03555     // If this is a legal constant, turn it into a TargetConstantFP node.
03556     if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
03557       Results.push_back(ExpandConstantFP(CFP, true));
03558     break;
03559   }
03560   case ISD::FSUB: {
03561     EVT VT = Node->getValueType(0);
03562     if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
03563         TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) {
03564       Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
03565       Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1);
03566       Results.push_back(Tmp1);
03567     } else {
03568       Results.push_back(ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64,
03569                                         RTLIB::SUB_F80, RTLIB::SUB_F128,
03570                                         RTLIB::SUB_PPCF128));
03571     }
03572     break;
03573   }
03574   case ISD::SUB: {
03575     EVT VT = Node->getValueType(0);
03576     assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
03577            TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
03578            "Don't know how to expand this subtraction!");
03579     Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
03580                DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
03581     Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, VT));
03582     Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
03583     break;
03584   }
03585   case ISD::UREM:
03586   case ISD::SREM: {
03587     EVT VT = Node->getValueType(0);
03588     bool isSigned = Node->getOpcode() == ISD::SREM;
03589     unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
03590     unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
03591     Tmp2 = Node->getOperand(0);
03592     Tmp3 = Node->getOperand(1);
03593     if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
03594         (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
03595          // If div is legal, it's better to do the normal expansion
03596          !TLI.isOperationLegalOrCustom(DivOpc, Node->getValueType(0)) &&
03597          useDivRem(Node, isSigned, false))) {
03598       SDVTList VTs = DAG.getVTList(VT, VT);
03599       Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
03600     } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
03601       // X % Y -> X-X/Y*Y
03602       Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
03603       Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
03604       Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
03605     } else if (isSigned)
03606       Tmp1 = ExpandIntLibCall(Node, true,
03607                               RTLIB::SREM_I8,
03608                               RTLIB::SREM_I16, RTLIB::SREM_I32,
03609                               RTLIB::SREM_I64, RTLIB::SREM_I128);
03610     else
03611       Tmp1 = ExpandIntLibCall(Node, false,
03612                               RTLIB::UREM_I8,
03613                               RTLIB::UREM_I16, RTLIB::UREM_I32,
03614                               RTLIB::UREM_I64, RTLIB::UREM_I128);
03615     Results.push_back(Tmp1);
03616     break;
03617   }
03618   case ISD::UDIV:
03619   case ISD::SDIV: {
03620     bool isSigned = Node->getOpcode() == ISD::SDIV;
03621     unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
03622     EVT VT = Node->getValueType(0);
03623     SDVTList VTs = DAG.getVTList(VT, VT);
03624     if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
03625         (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
03626          useDivRem(Node, isSigned, true)))
03627       Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
03628                          Node->getOperand(1));
03629     else if (isSigned)
03630       Tmp1 = ExpandIntLibCall(Node, true,
03631                               RTLIB::SDIV_I8,
03632                               RTLIB::SDIV_I16, RTLIB::SDIV_I32,
03633                               RTLIB::SDIV_I64, RTLIB::SDIV_I128);
03634     else
03635       Tmp1 = ExpandIntLibCall(Node, false,
03636                               RTLIB::UDIV_I8,
03637                               RTLIB::UDIV_I16, RTLIB::UDIV_I32,
03638                               RTLIB::UDIV_I64, RTLIB::UDIV_I128);
03639     Results.push_back(Tmp1);
03640     break;
03641   }
03642   case ISD::MULHU:
03643   case ISD::MULHS: {
03644     unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
03645                                                               ISD::SMUL_LOHI;
03646     EVT VT = Node->getValueType(0);
03647     SDVTList VTs = DAG.getVTList(VT, VT);
03648     assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
03649            "If this wasn't legal, it shouldn't have been created!");
03650     Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
03651                        Node->getOperand(1));
03652     Results.push_back(Tmp1.getValue(1));
03653     break;
03654   }
03655   case ISD::SDIVREM:
03656   case ISD::UDIVREM:
03657     // Expand into divrem libcall
03658     ExpandDivRemLibCall(Node, Results);
03659     break;
03660   case ISD::MUL: {
03661     EVT VT = Node->getValueType(0);
03662     SDVTList VTs = DAG.getVTList(VT, VT);
03663     // See if multiply or divide can be lowered using two-result operations.
03664     // We just need the low half of the multiply; try both the signed
03665     // and unsigned forms. If the target supports both SMUL_LOHI and
03666     // UMUL_LOHI, form a preference by checking which forms of plain
03667     // MULH it supports.
03668     bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
03669     bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
03670     bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
03671     bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
03672     unsigned OpToUse = 0;
03673     if (HasSMUL_LOHI && !HasMULHS) {
03674       OpToUse = ISD::SMUL_LOHI;
03675     } else if (HasUMUL_LOHI && !HasMULHU) {
03676       OpToUse = ISD::UMUL_LOHI;
03677     } else if (HasSMUL_LOHI) {
03678       OpToUse = ISD::SMUL_LOHI;
03679     } else if (HasUMUL_LOHI) {
03680       OpToUse = ISD::UMUL_LOHI;
03681     }
03682     if (OpToUse) {
03683       Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
03684                                     Node->getOperand(1)));
03685       break;
03686     }
03687 
03688     SDValue Lo, Hi;
03689     EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
03690     if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) &&
03691         TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) &&
03692         TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
03693         TLI.isOperationLegalOrCustom(ISD::OR, VT) &&
03694         TLI.expandMUL(Node, Lo, Hi, HalfType, DAG)) {
03695       Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
03696       Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
03697       SDValue Shift = DAG.getConstant(HalfType.getSizeInBits(),
03698                                       TLI.getShiftAmountTy(HalfType));
03699       Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
03700       Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
03701       break;
03702     }
03703 
03704     Tmp1 = ExpandIntLibCall(Node, false,
03705                             RTLIB::MUL_I8,
03706                             RTLIB::MUL_I16, RTLIB::MUL_I32,
03707                             RTLIB::MUL_I64, RTLIB::MUL_I128);
03708     Results.push_back(Tmp1);
03709     break;
03710   }
03711   case ISD::SADDO:
03712   case ISD::SSUBO: {
03713     SDValue LHS = Node->getOperand(0);
03714     SDValue RHS = Node->getOperand(1);
03715     SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
03716                               ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
03717                               LHS, RHS);
03718     Results.push_back(Sum);
03719     EVT ResultType = Node->getValueType(1);
03720     EVT OType = getSetCCResultType(Node->getValueType(0));
03721 
03722     SDValue Zero = DAG.getConstant(0, LHS.getValueType());
03723 
03724     //   LHSSign -> LHS >= 0
03725     //   RHSSign -> RHS >= 0
03726     //   SumSign -> Sum >= 0
03727     //
03728     //   Add:
03729     //   Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
03730     //   Sub:
03731     //   Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
03732     //
03733     SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
03734     SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
03735     SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
03736                                       Node->getOpcode() == ISD::SADDO ?
03737                                       ISD::SETEQ : ISD::SETNE);
03738 
03739     SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
03740     SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
03741 
03742     SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
03743     Results.push_back(DAG.getBoolExtOrTrunc(Cmp, dl, ResultType, ResultType));
03744     break;
03745   }
03746   case ISD::UADDO:
03747   case ISD::USUBO: {
03748     SDValue LHS = Node->getOperand(0);
03749     SDValue RHS = Node->getOperand(1);
03750     SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
03751                               ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
03752                               LHS, RHS);
03753     Results.push_back(Sum);
03754 
03755     EVT ResultType = Node->getValueType(1);
03756     EVT SetCCType = getSetCCResultType(Node->getValueType(0));
03757     ISD::CondCode CC
03758       = Node->getOpcode() == ISD::UADDO ? ISD::SETULT : ISD::SETUGT;
03759     SDValue SetCC = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
03760 
03761     Results.push_back(DAG.getBoolExtOrTrunc(SetCC, dl, ResultType, ResultType));
03762     break;
03763   }
03764   case ISD::UMULO:
03765   case ISD::SMULO: {
03766     EVT VT = Node->getValueType(0);
03767     EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
03768     SDValue LHS = Node->getOperand(0);
03769     SDValue RHS = Node->getOperand(1);
03770     SDValue BottomHalf;
03771     SDValue TopHalf;
03772     static const unsigned Ops[2][3] =
03773         { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
03774           { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
03775     bool isSigned = Node->getOpcode() == ISD::SMULO;
03776     if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
03777       BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
03778       TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
03779     } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
03780       BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
03781                                RHS);
03782       TopHalf = BottomHalf.getValue(1);
03783     } else if (TLI.isTypeLegal(WideVT)) {
03784       LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
03785       RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
03786       Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
03787       BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
03788                                DAG.getIntPtrConstant(0));
03789       TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
03790                             DAG.getIntPtrConstant(1));
03791     } else {
03792       // We can fall back to a libcall with an illegal type for the MUL if we
03793       // have a libcall big enough.
03794       // Also, we can fall back to a division in some cases, but that's a big
03795       // performance hit in the general case.
03796       RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
03797       if (WideVT == MVT::i16)
03798         LC = RTLIB::MUL_I16;
03799       else if (WideVT == MVT::i32)
03800         LC = RTLIB::MUL_I32;
03801       else if (WideVT == MVT::i64)
03802         LC = RTLIB::MUL_I64;
03803       else if (WideVT == MVT::i128)
03804         LC = RTLIB::MUL_I128;
03805       assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
03806 
03807       // The high part is obtained by SRA'ing all but one of the bits of low
03808       // part.
03809       unsigned LoSize = VT.getSizeInBits();
03810       SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, RHS,
03811                                 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
03812       SDValue HiRHS = DAG.getNode(ISD::SRA, dl, VT, LHS,
03813                                 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
03814 
03815       // Here we're passing the 2 arguments explicitly as 4 arguments that are
03816       // pre-lowered to the correct types. This all depends upon WideVT not
03817       // being a legal type for the architecture and thus has to be split to
03818       // two arguments.
03819       SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
03820       SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
03821       BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
03822                                DAG.getIntPtrConstant(0));
03823       TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
03824                             DAG.getIntPtrConstant(1));
03825       // Ret is a node with an illegal type. Because such things are not
03826       // generally permitted during this phase of legalization, make sure the
03827       // node has no more uses. The above EXTRACT_ELEMENT nodes should have been
03828       // folded.
03829       assert(Ret->use_empty() &&
03830              "Unexpected uses of illegally type from expanded lib call.");
03831     }
03832 
03833     if (isSigned) {
03834       Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
03835                              TLI.getShiftAmountTy(BottomHalf.getValueType()));
03836       Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
03837       TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf, Tmp1,
03838                              ISD::SETNE);
03839     } else {
03840       TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf,
03841                              DAG.getConstant(0, VT), ISD::SETNE);
03842     }
03843     Results.push_back(BottomHalf);
03844     Results.push_back(TopHalf);
03845     break;
03846   }
03847   case ISD::BUILD_PAIR: {
03848     EVT PairTy = Node->getValueType(0);
03849     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
03850     Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
03851     Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
03852                        DAG.getConstant(PairTy.getSizeInBits()/2,
03853                                        TLI.getShiftAmountTy(PairTy)));
03854     Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
03855     break;
03856   }
03857   case ISD::SELECT:
03858     Tmp1 = Node->getOperand(0);
03859     Tmp2 = Node->getOperand(1);
03860     Tmp3 = Node->getOperand(2);
03861     if (Tmp1.getOpcode() == ISD::SETCC) {
03862       Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
03863                              Tmp2, Tmp3,
03864                              cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
03865     } else {
03866       Tmp1 = DAG.getSelectCC(dl, Tmp1,
03867                              DAG.getConstant(0, Tmp1.getValueType()),
03868                              Tmp2, Tmp3, ISD::SETNE);
03869     }
03870     Results.push_back(Tmp1);
03871     break;
03872   case ISD::BR_JT: {
03873     SDValue Chain = Node->getOperand(0);
03874     SDValue Table = Node->getOperand(1);
03875     SDValue Index = Node->getOperand(2);
03876 
03877     EVT PTy = TLI.getPointerTy();
03878 
03879     const DataLayout &TD = *TLI.getDataLayout();
03880     unsigned EntrySize =
03881       DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
03882 
03883     Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(),
03884                        Index, DAG.getConstant(EntrySize, Index.getValueType()));
03885     SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(),
03886                                Index, Table);
03887 
03888     EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
03889     SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
03890                                 MachinePointerInfo::getJumpTable(), MemVT,
03891                                 false, false, false, 0);
03892     Addr = LD;
03893     if (TM.getRelocationModel() == Reloc::PIC_) {
03894       // For PIC, the sequence is:
03895       // BRIND(load(Jumptable + index) + RelocBase)
03896       // RelocBase can be JumpTable, GOT or some sort of global base.
03897       Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
03898                           TLI.getPICJumpTableRelocBase(Table, DAG));
03899     }
03900     Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
03901     Results.push_back(Tmp1);
03902     break;
03903   }
03904   case ISD::BRCOND:
03905     // Expand brcond's setcc into its constituent parts and create a BR_CC
03906     // Node.
03907     Tmp1 = Node->getOperand(0);
03908     Tmp2 = Node->getOperand(1);
03909     if (Tmp2.getOpcode() == ISD::SETCC) {
03910       Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
03911                          Tmp1, Tmp2.getOperand(2),
03912                          Tmp2.getOperand(0), Tmp2.getOperand(1),
03913                          Node->getOperand(2));
03914     } else {
03915       // We test only the i1 bit.  Skip the AND if UNDEF.
03916       Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 :
03917         DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
03918                     DAG.getConstant(1, Tmp2.getValueType()));
03919       Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
03920                          DAG.getCondCode(ISD::SETNE), Tmp3,
03921                          DAG.getConstant(0, Tmp3.getValueType()),
03922                          Node->getOperand(2));
03923     }
03924     Results.push_back(Tmp1);
03925     break;
03926   case ISD::SETCC: {
03927     Tmp1 = Node->getOperand(0);
03928     Tmp2 = Node->getOperand(1);
03929     Tmp3 = Node->getOperand(2);
03930     bool Legalized = LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2,
03931                                            Tmp3, NeedInvert, dl);
03932 
03933     if (Legalized) {
03934       // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
03935       // condition code, create a new SETCC node.
03936       if (Tmp3.getNode())
03937         Tmp1 = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
03938                            Tmp1, Tmp2, Tmp3);
03939 
03940       // If we expanded the SETCC by inverting the condition code, then wrap
03941       // the existing SETCC in a NOT to restore the intended condition.
03942       if (NeedInvert)
03943         Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
03944 
03945       Results.push_back(Tmp1);
03946       break;
03947     }
03948 
03949     // Otherwise, SETCC for the given comparison type must be completely
03950     // illegal; expand it into a SELECT_CC.
03951     EVT VT = Node->getValueType(0);
03952     int TrueValue;
03953     switch (TLI.getBooleanContents(Tmp1->getValueType(0))) {
03954     case TargetLowering::ZeroOrOneBooleanContent:
03955     case TargetLowering::UndefinedBooleanContent:
03956       TrueValue = 1;
03957       break;
03958     case TargetLowering::ZeroOrNegativeOneBooleanContent:
03959       TrueValue = -1;
03960       break;
03961     }
03962     Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
03963                        DAG.getConstant(TrueValue, VT), DAG.getConstant(0, VT),
03964                        Tmp3);
03965     Results.push_back(Tmp1);
03966     break;
03967   }
03968   case ISD::SELECT_CC: {
03969     Tmp1 = Node->getOperand(0);   // LHS
03970     Tmp2 = Node->getOperand(1);   // RHS
03971     Tmp3 = Node->getOperand(2);   // True
03972     Tmp4 = Node->getOperand(3);   // False
03973     EVT VT = Node->getValueType(0);
03974     SDValue CC = Node->getOperand(4);
03975     ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
03976 
03977     if (TLI.isCondCodeLegal(CCOp, Tmp1.getSimpleValueType())) {
03978       // If the condition code is legal, then we need to expand this
03979       // node using SETCC and SELECT.
03980       EVT CmpVT = Tmp1.getValueType();
03981       assert(!TLI.isOperationExpand(ISD::SELECT, VT) &&
03982              "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
03983              "expanded.");
03984       EVT CCVT = TLI.getSetCCResultType(*DAG.getContext(), CmpVT);
03985       SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC);
03986       Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4));
03987       break;
03988     }
03989 
03990     // SELECT_CC is legal, so the condition code must not be.
03991     bool Legalized = false;
03992     // Try to legalize by inverting the condition.  This is for targets that
03993     // might support an ordered version of a condition, but not the unordered
03994     // version (or vice versa).
03995     ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp,
03996                                                Tmp1.getValueType().isInteger());
03997     if (TLI.isCondCodeLegal(InvCC, Tmp1.getSimpleValueType())) {
03998       // Use the new condition code and swap true and false
03999       Legalized = true;
04000       Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC);
04001     } else {
04002       // If The inverse is not legal, then try to swap the arguments using
04003       // the inverse condition code.
04004       ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC);
04005       if (TLI.isCondCodeLegal(SwapInvCC, Tmp1.getSimpleValueType())) {
04006         // The swapped inverse condition is legal, so swap true and false,
04007         // lhs and rhs.
04008         Legalized = true;
04009         Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC);
04010       }
04011     }
04012 
04013     if (!Legalized) {
04014       Legalized = LegalizeSetCCCondCode(
04015           getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC, NeedInvert,
04016           dl);
04017 
04018       assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
04019 
04020       // If we expanded the SETCC by inverting the condition code, then swap
04021       // the True/False operands to match.
04022       if (NeedInvert)
04023         std::swap(Tmp3, Tmp4);
04024 
04025       // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
04026       // condition code, create a new SELECT_CC node.
04027       if (CC.getNode()) {
04028         Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0),
04029                            Tmp1, Tmp2, Tmp3, Tmp4, CC);
04030       } else {
04031         Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
04032         CC = DAG.getCondCode(ISD::SETNE);
04033         Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
04034                            Tmp2, Tmp3, Tmp4, CC);
04035       }
04036     }
04037     Results.push_back(Tmp1);
04038     break;
04039   }
04040   case ISD::BR_CC: {
04041     Tmp1 = Node->getOperand(0);              // Chain
04042     Tmp2 = Node->getOperand(2);              // LHS
04043     Tmp3 = Node->getOperand(3);              // RHS
04044     Tmp4 = Node->getOperand(1);              // CC
04045 
04046     bool Legalized = LegalizeSetCCCondCode(getSetCCResultType(
04047         Tmp2.getValueType()), Tmp2, Tmp3, Tmp4, NeedInvert, dl);
04048     (void)Legalized;
04049     assert(Legalized && "Can't legalize BR_CC with legal condition!");
04050 
04051     // If we expanded the SETCC by inverting the condition code, then wrap
04052     // the existing SETCC in a NOT to restore the intended condition.
04053     if (NeedInvert)
04054       Tmp4 = DAG.getNOT(dl, Tmp4, Tmp4->getValueType(0));
04055 
04056     // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
04057     // node.
04058     if (Tmp4.getNode()) {
04059       Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
04060                          Tmp4, Tmp2, Tmp3, Node->getOperand(4));
04061     } else {
04062       Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
04063       Tmp4 = DAG.getCondCode(ISD::SETNE);
04064       Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
04065                          Tmp2, Tmp3, Node->getOperand(4));
04066     }
04067     Results.push_back(Tmp1);
04068     break;
04069   }
04070   case ISD::BUILD_VECTOR:
04071     Results.push_back(ExpandBUILD_VECTOR(Node));
04072     break;
04073   case ISD::SRA:
04074   case ISD::SRL:
04075   case ISD::SHL: {
04076     // Scalarize vector SRA/SRL/SHL.
04077     EVT VT = Node->getValueType(0);
04078     assert(VT.isVector() && "Unable to legalize non-vector shift");
04079     assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
04080     unsigned NumElem = VT.getVectorNumElements();
04081 
04082     SmallVector<SDValue, 8> Scalars;
04083     for (unsigned Idx = 0; Idx < NumElem; Idx++) {
04084       SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
04085                                VT.getScalarType(),
04086                                Node->getOperand(0), DAG.getConstant(Idx,
04087                                                     TLI.getVectorIdxTy()));
04088       SDValue Sh = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
04089                                VT.getScalarType(),
04090                                Node->getOperand(1), DAG.getConstant(Idx,
04091                                                     TLI.getVectorIdxTy()));
04092       Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
04093                                     VT.getScalarType(), Ex, Sh));
04094     }
04095     SDValue Result =
04096       DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0), Scalars);
04097     ReplaceNode(SDValue(Node, 0), Result);
04098     break;
04099   }
04100   case ISD::GLOBAL_OFFSET_TABLE:
04101   case ISD::GlobalAddress:
04102   case ISD::GlobalTLSAddress:
04103   case ISD::ExternalSymbol:
04104   case ISD::ConstantPool:
04105   case ISD::JumpTable:
04106   case ISD::INTRINSIC_W_CHAIN:
04107   case ISD::INTRINSIC_WO_CHAIN:
04108   case ISD::INTRINSIC_VOID:
04109     // FIXME: Custom lowering for these operations shouldn't return null!
04110     break;
04111   }
04112 
04113   // Replace the original node with the legalized result.
04114   if (!Results.empty())
04115     ReplaceNode(Node, Results.data());
04116 }
04117 
04118 void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
04119   SmallVector<SDValue, 8> Results;
04120   MVT OVT = Node->getSimpleValueType(0);
04121   if (Node->getOpcode() == ISD::UINT_TO_FP ||
04122       Node->getOpcode() == ISD::SINT_TO_FP ||
04123       Node->getOpcode() == ISD::SETCC) {
04124     OVT = Node->getOperand(0).getSimpleValueType();
04125   }
04126   MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
04127   SDLoc dl(Node);
04128   SDValue Tmp1, Tmp2, Tmp3;
04129   switch (Node->getOpcode()) {
04130   case ISD::CTTZ:
04131   case ISD::CTTZ_ZERO_UNDEF:
04132   case ISD::CTLZ:
04133   case ISD::CTLZ_ZERO_UNDEF:
04134   case ISD::CTPOP:
04135     // Zero extend the argument.
04136     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
04137     // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
04138     // already the correct result.
04139     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
04140     if (Node->getOpcode() == ISD::CTTZ) {
04141       // FIXME: This should set a bit in the zero extended value instead.
04142       Tmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT),
04143                           Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
04144                           ISD::SETEQ);
04145       Tmp1 = DAG.getSelect(dl, NVT, Tmp2,
04146                            DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
04147     } else if (Node->getOpcode() == ISD::CTLZ ||
04148                Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) {
04149       // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
04150       Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
04151                           DAG.getConstant(NVT.getSizeInBits() -
04152                                           OVT.getSizeInBits(), NVT));
04153     }
04154     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
04155     break;
04156   case ISD::BSWAP: {
04157     unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
04158     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
04159     Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
04160     Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
04161                           DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
04162     Results.push_back(Tmp1);
04163     break;
04164   }
04165   case ISD::FP_TO_UINT:
04166   case ISD::FP_TO_SINT:
04167     Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
04168                                  Node->getOpcode() == ISD::FP_TO_SINT, dl);
04169     Results.push_back(Tmp1);
04170     break;
04171   case ISD::UINT_TO_FP:
04172   case ISD::SINT_TO_FP:
04173     Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
04174                                  Node->getOpcode() == ISD::SINT_TO_FP, dl);
04175     Results.push_back(Tmp1);
04176     break;
04177   case ISD::VAARG: {
04178     SDValue Chain = Node->getOperand(0); // Get the chain.
04179     SDValue Ptr = Node->getOperand(1); // Get the pointer.
04180 
04181     unsigned TruncOp;
04182     if (OVT.isVector()) {
04183       TruncOp = ISD::BITCAST;
04184     } else {
04185       assert(OVT.isInteger()
04186         && "VAARG promotion is supported only for vectors or integer types");
04187       TruncOp = ISD::TRUNCATE;
04188     }
04189 
04190     // Perform the larger operation, then convert back
04191     Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
04192              Node->getConstantOperandVal(3));
04193     Chain = Tmp1.getValue(1);
04194 
04195     Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
04196 
04197     // Modified the chain result - switch anything that used the old chain to
04198     // use the new one.
04199     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
04200     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
04201     if (UpdatedNodes) {
04202       UpdatedNodes->insert(Tmp2.getNode());
04203       UpdatedNodes->insert(Chain.getNode());
04204     }
04205     ReplacedNode(Node);
04206     break;
04207   }
04208   case ISD::AND:
04209   case ISD::OR:
04210   case ISD::XOR: {
04211     unsigned ExtOp, TruncOp;
04212     if (OVT.isVector()) {
04213       ExtOp   = ISD::BITCAST;
04214       TruncOp = ISD::BITCAST;
04215     } else {
04216       assert(OVT.isInteger() && "Cannot promote logic operation");
04217       ExtOp   = ISD::ANY_EXTEND;
04218       TruncOp = ISD::TRUNCATE;
04219     }
04220     // Promote each of the values to the new type.
04221     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
04222     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
04223     // Perform the larger operation, then convert back
04224     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
04225     Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
04226     break;
04227   }
04228   case ISD::SELECT: {
04229     unsigned ExtOp, TruncOp;
04230     if (Node->getValueType(0).isVector() ||
04231         Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) {
04232       ExtOp   = ISD::BITCAST;
04233       TruncOp = ISD::BITCAST;
04234     } else if (Node->getValueType(0).isInteger()) {
04235       ExtOp   = ISD::ANY_EXTEND;
04236       TruncOp = ISD::TRUNCATE;
04237     } else {
04238       ExtOp   = ISD::FP_EXTEND;
04239       TruncOp = ISD::FP_ROUND;
04240     }
04241     Tmp1 = Node->getOperand(0);
04242     // Promote each of the values to the new type.
04243     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
04244     Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
04245     // Perform the larger operation, then round down.
04246     Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3);
04247     if (TruncOp != ISD::FP_ROUND)
04248       Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
04249     else
04250       Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
04251                          DAG.getIntPtrConstant(0));
04252     Results.push_back(Tmp1);
04253     break;
04254   }
04255   case ISD::VECTOR_SHUFFLE: {
04256     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
04257 
04258     // Cast the two input vectors.
04259     Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
04260     Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
04261 
04262     // Convert the shuffle mask to the right # elements.
04263     Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
04264     Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
04265     Results.push_back(Tmp1);
04266     break;
04267   }
04268   case ISD::SETCC: {
04269     unsigned ExtOp = ISD::FP_EXTEND;
04270     if (NVT.isInteger()) {
04271       ISD::CondCode CCCode =
04272         cast<CondCodeSDNode>(Node->getOperand(2))->get();
04273       ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
04274     }
04275     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
04276     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
04277     Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
04278                                   Tmp1, Tmp2, Node->getOperand(2)));
04279     break;
04280   }
04281   case ISD::FADD:
04282   case ISD::FSUB:
04283   case ISD::FMUL:
04284   case ISD::FDIV:
04285   case ISD::FREM:
04286   case ISD::FPOW: {
04287     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
04288     Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
04289     Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
04290     Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
04291                                   Tmp3, DAG.getIntPtrConstant(0)));
04292     break;
04293   }
04294   case ISD::FLOG2:
04295   case ISD::FEXP2:
04296   case ISD::FLOG:
04297   case ISD::FEXP: {
04298     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
04299     Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
04300     Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
04301                                   Tmp2, DAG.getIntPtrConstant(0)));
04302     break;
04303   }
04304   }
04305 
04306   // Replace the original node with the legalized result.
04307   if (!Results.empty())
04308     ReplaceNode(Node, Results.data());
04309 }
04310 
04311 // SelectionDAG::Legalize - This is the entry point for the file.
04312 //
04313 void SelectionDAG::Legalize() {
04314   AssignTopologicalOrder();
04315 
04316   SmallPtrSet<SDNode *, 16> LegalizedNodes;
04317   SelectionDAGLegalize Legalizer(*this, LegalizedNodes);
04318 
04319   // Visit all the nodes. We start in topological order, so that we see
04320   // nodes with their original operands intact. Legalization can produce
04321   // new nodes which may themselves need to be legalized. Iterate until all
04322   // nodes have been legalized.
04323   for (;;) {
04324     bool AnyLegalized = false;
04325     for (auto NI = allnodes_end(); NI != allnodes_begin();) {
04326       --NI;
04327 
04328       SDNode *N = NI;
04329       if (N->use_empty() && N != getRoot().getNode()) {
04330         ++NI;
04331         DeleteNode(N);
04332         continue;
04333       }
04334 
04335       if (LegalizedNodes.insert(N)) {
04336         AnyLegalized = true;
04337         Legalizer.LegalizeOp(N);
04338 
04339         if (N->use_empty() && N != getRoot().getNode()) {
04340           ++NI;
04341           DeleteNode(N);
04342         }
04343       }
04344     }
04345     if (!AnyLegalized)
04346       break;
04347 
04348   }
04349 
04350   // Remove dead nodes now.
04351   RemoveDeadNodes();
04352 }
04353 
04354 bool SelectionDAG::LegalizeOp(SDNode *N,
04355                               SmallSetVector<SDNode *, 16> &UpdatedNodes) {
04356   SmallPtrSet<SDNode *, 16> LegalizedNodes;
04357   SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes);
04358 
04359   // Directly insert the node in question, and legalize it. This will recurse
04360   // as needed through operands.
04361   LegalizedNodes.insert(N);
04362   Legalizer.LegalizeOp(N);
04363 
04364   return LegalizedNodes.count(N);
04365 }